Print this page
7127 remove -Wno-missing-braces from Makefile.uts
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/io/devinfo.c
+++ new/usr/src/uts/common/io/devinfo.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 */
25 25
26 26 /*
27 27 * driver for accessing kernel devinfo tree.
28 28 */
29 29 #include <sys/types.h>
30 30 #include <sys/pathname.h>
31 31 #include <sys/debug.h>
32 32 #include <sys/autoconf.h>
33 33 #include <sys/vmsystm.h>
34 34 #include <sys/conf.h>
35 35 #include <sys/file.h>
36 36 #include <sys/kmem.h>
37 37 #include <sys/modctl.h>
38 38 #include <sys/stat.h>
39 39 #include <sys/ddi.h>
40 40 #include <sys/sunddi.h>
41 41 #include <sys/sunldi_impl.h>
42 42 #include <sys/sunndi.h>
43 43 #include <sys/esunddi.h>
44 44 #include <sys/sunmdi.h>
45 45 #include <sys/ddi_impldefs.h>
46 46 #include <sys/ndi_impldefs.h>
47 47 #include <sys/mdi_impldefs.h>
48 48 #include <sys/devinfo_impl.h>
49 49 #include <sys/thread.h>
50 50 #include <sys/modhash.h>
51 51 #include <sys/bitmap.h>
52 52 #include <util/qsort.h>
53 53 #include <sys/disp.h>
54 54 #include <sys/kobj.h>
55 55 #include <sys/crc32.h>
56 56 #include <sys/ddi_hp.h>
57 57 #include <sys/ddi_hp_impl.h>
58 58 #include <sys/sysmacros.h>
59 59 #include <sys/list.h>
60 60
61 61
62 62 #ifdef DEBUG
63 63 static int di_debug;
64 64 #define dcmn_err(args) if (di_debug >= 1) cmn_err args
65 65 #define dcmn_err2(args) if (di_debug >= 2) cmn_err args
66 66 #define dcmn_err3(args) if (di_debug >= 3) cmn_err args
67 67 #else
68 68 #define dcmn_err(args) /* nothing */
69 69 #define dcmn_err2(args) /* nothing */
70 70 #define dcmn_err3(args) /* nothing */
71 71 #endif
72 72
73 73 /*
74 74 * We partition the space of devinfo minor nodes equally between the full and
75 75 * unprivileged versions of the driver. The even-numbered minor nodes are the
76 76 * full version, while the odd-numbered ones are the read-only version.
77 77 */
78 78 static int di_max_opens = 32;
79 79
80 80 static int di_prop_dyn = 1; /* enable dynamic property support */
81 81
82 82 #define DI_FULL_PARENT 0
83 83 #define DI_READONLY_PARENT 1
84 84 #define DI_NODE_SPECIES 2
85 85 #define DI_UNPRIVILEGED_NODE(x) (((x) % 2) != 0)
86 86
87 87 #define IOC_IDLE 0 /* snapshot ioctl states */
88 88 #define IOC_SNAP 1 /* snapshot in progress */
89 89 #define IOC_DONE 2 /* snapshot done, but not copied out */
90 90 #define IOC_COPY 3 /* copyout in progress */
91 91
92 92 /*
93 93 * Keep max alignment so we can move snapshot to different platforms.
94 94 *
95 95 * NOTE: Most callers should rely on the di_checkmem return value
96 96 * being aligned, and reestablish *off_p with aligned value, instead
97 97 * of trying to align size of their allocations: this approach will
98 98 * minimize memory use.
99 99 */
100 100 #define DI_ALIGN(addr) ((addr + 7l) & ~7l)
101 101
102 102 /*
103 103 * To avoid wasting memory, make a linked list of memory chunks.
104 104 * Size of each chunk is buf_size.
105 105 */
106 106 struct di_mem {
107 107 struct di_mem *next; /* link to next chunk */
108 108 char *buf; /* contiguous kernel memory */
109 109 size_t buf_size; /* size of buf in bytes */
110 110 devmap_cookie_t cook; /* cookie from ddi_umem_alloc */
111 111 };
112 112
113 113 /*
114 114 * This is a stack for walking the tree without using recursion.
115 115 * When the devinfo tree height is above some small size, one
116 116 * gets watchdog resets on sun4m.
117 117 */
118 118 struct di_stack {
119 119 void *offset[MAX_TREE_DEPTH];
120 120 struct dev_info *dip[MAX_TREE_DEPTH];
121 121 int circ[MAX_TREE_DEPTH];
122 122 int depth; /* depth of current node to be copied */
123 123 };
124 124
125 125 #define TOP_OFFSET(stack) \
126 126 ((di_off_t *)(stack)->offset[(stack)->depth - 1])
127 127 #define TOP_NODE(stack) \
128 128 ((stack)->dip[(stack)->depth - 1])
129 129 #define PARENT_OFFSET(stack) \
130 130 ((di_off_t *)(stack)->offset[(stack)->depth - 2])
131 131 #define EMPTY_STACK(stack) ((stack)->depth == 0)
132 132 #define POP_STACK(stack) { \
133 133 ndi_devi_exit((dev_info_t *)TOP_NODE(stack), \
134 134 (stack)->circ[(stack)->depth - 1]); \
135 135 ((stack)->depth--); \
136 136 }
137 137 #define PUSH_STACK(stack, node, off_p) { \
138 138 ASSERT(node != NULL); \
139 139 ndi_devi_enter((dev_info_t *)node, &(stack)->circ[(stack)->depth]); \
140 140 (stack)->dip[(stack)->depth] = (node); \
141 141 (stack)->offset[(stack)->depth] = (void *)(off_p); \
142 142 ((stack)->depth)++; \
143 143 }
144 144
145 145 #define DI_ALL_PTR(s) DI_ALL(di_mem_addr((s), 0))
146 146
147 147 /*
148 148 * With devfs, the device tree has no global locks. The device tree is
149 149 * dynamic and dips may come and go if they are not locked locally. Under
150 150 * these conditions, pointers are no longer reliable as unique IDs.
151 151 * Specifically, these pointers cannot be used as keys for hash tables
152 152 * as the same devinfo structure may be freed in one part of the tree only
153 153 * to be allocated as the structure for a different device in another
154 154 * part of the tree. This can happen if DR and the snapshot are
155 155 * happening concurrently.
156 156 * The following data structures act as keys for devinfo nodes and
157 157 * pathinfo nodes.
158 158 */
159 159
160 160 enum di_ktype {
161 161 DI_DKEY = 1,
162 162 DI_PKEY = 2
163 163 };
164 164
165 165 struct di_dkey {
166 166 dev_info_t *dk_dip;
167 167 major_t dk_major;
168 168 int dk_inst;
169 169 pnode_t dk_nodeid;
170 170 };
171 171
172 172 struct di_pkey {
173 173 mdi_pathinfo_t *pk_pip;
174 174 char *pk_path_addr;
175 175 dev_info_t *pk_client;
176 176 dev_info_t *pk_phci;
177 177 };
178 178
179 179 struct di_key {
180 180 enum di_ktype k_type;
181 181 union {
182 182 struct di_dkey dkey;
183 183 struct di_pkey pkey;
184 184 } k_u;
185 185 };
186 186
187 187
188 188 struct i_lnode;
189 189
190 190 typedef struct i_link {
191 191 /*
192 192 * If a di_link struct representing this i_link struct makes it
193 193 * into the snapshot, then self will point to the offset of
194 194 * the di_link struct in the snapshot
195 195 */
196 196 di_off_t self;
197 197
198 198 int spec_type; /* block or char access type */
199 199 struct i_lnode *src_lnode; /* src i_lnode */
200 200 struct i_lnode *tgt_lnode; /* tgt i_lnode */
201 201 struct i_link *src_link_next; /* next src i_link /w same i_lnode */
202 202 struct i_link *tgt_link_next; /* next tgt i_link /w same i_lnode */
203 203 } i_link_t;
204 204
205 205 typedef struct i_lnode {
206 206 /*
207 207 * If a di_lnode struct representing this i_lnode struct makes it
208 208 * into the snapshot, then self will point to the offset of
209 209 * the di_lnode struct in the snapshot
210 210 */
211 211 di_off_t self;
212 212
213 213 /*
214 214 * used for hashing and comparing i_lnodes
215 215 */
216 216 int modid;
217 217
218 218 /*
219 219 * public information describing a link endpoint
220 220 */
221 221 struct di_node *di_node; /* di_node in snapshot */
222 222 dev_t devt; /* devt */
223 223
224 224 /*
225 225 * i_link ptr to links coming into this i_lnode node
226 226 * (this i_lnode is the target of these i_links)
227 227 */
228 228 i_link_t *link_in;
229 229
230 230 /*
231 231 * i_link ptr to links going out of this i_lnode node
232 232 * (this i_lnode is the source of these i_links)
233 233 */
234 234 i_link_t *link_out;
235 235 } i_lnode_t;
236 236
237 237 typedef struct i_hp {
238 238 di_off_t hp_off; /* Offset of di_hp_t in snapshot */
239 239 dev_info_t *hp_child; /* Child devinfo node of the di_hp_t */
240 240 list_node_t hp_link; /* List linkage */
241 241 } i_hp_t;
242 242
243 243 /*
244 244 * Soft state associated with each instance of driver open.
245 245 */
246 246 static struct di_state {
247 247 di_off_t mem_size; /* total # bytes in memlist */
248 248 struct di_mem *memlist; /* head of memlist */
249 249 uint_t command; /* command from ioctl */
250 250 int di_iocstate; /* snapshot ioctl state */
251 251 mod_hash_t *reg_dip_hash;
252 252 mod_hash_t *reg_pip_hash;
253 253 int lnode_count;
254 254 int link_count;
255 255
256 256 mod_hash_t *lnode_hash;
257 257 mod_hash_t *link_hash;
258 258
259 259 list_t hp_list;
260 260 } **di_states;
261 261
262 262 static kmutex_t di_lock; /* serialize instance assignment */
263 263
264 264 typedef enum {
265 265 DI_QUIET = 0, /* DI_QUIET must always be 0 */
266 266 DI_ERR,
267 267 DI_INFO,
268 268 DI_TRACE,
269 269 DI_TRACE1,
270 270 DI_TRACE2
271 271 } di_cache_debug_t;
272 272
273 273 static uint_t di_chunk = 32; /* I/O chunk size in pages */
274 274
275 275 #define DI_CACHE_LOCK(c) (mutex_enter(&(c).cache_lock))
276 276 #define DI_CACHE_UNLOCK(c) (mutex_exit(&(c).cache_lock))
277 277 #define DI_CACHE_LOCKED(c) (mutex_owned(&(c).cache_lock))
278 278
279 279 /*
280 280 * Check that whole device tree is being configured as a pre-condition for
281 281 * cleaning up /etc/devices files.
282 282 */
283 283 #define DEVICES_FILES_CLEANABLE(st) \
284 284 (((st)->command & DINFOSUBTREE) && ((st)->command & DINFOFORCE) && \
285 285 strcmp(DI_ALL_PTR(st)->root_path, "/") == 0)
286 286
287 287 #define CACHE_DEBUG(args) \
288 288 { if (di_cache_debug != DI_QUIET) di_cache_print args; }
289 289
290 290 typedef struct phci_walk_arg {
291 291 di_off_t off;
292 292 struct di_state *st;
293 293 } phci_walk_arg_t;
294 294
295 295 static int di_open(dev_t *, int, int, cred_t *);
296 296 static int di_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
297 297 static int di_close(dev_t, int, int, cred_t *);
298 298 static int di_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
299 299 static int di_attach(dev_info_t *, ddi_attach_cmd_t);
300 300 static int di_detach(dev_info_t *, ddi_detach_cmd_t);
301 301
302 302 static di_off_t di_copyformat(di_off_t, struct di_state *, intptr_t, int);
303 303 static di_off_t di_snapshot_and_clean(struct di_state *);
304 304 static di_off_t di_copydevnm(di_off_t *, struct di_state *);
305 305 static di_off_t di_copytree(struct dev_info *, di_off_t *, struct di_state *);
306 306 static di_off_t di_copynode(struct dev_info *, struct di_stack *,
307 307 struct di_state *);
308 308 static di_off_t di_getmdata(struct ddi_minor_data *, di_off_t *, di_off_t,
309 309 struct di_state *);
310 310 static di_off_t di_getppdata(struct dev_info *, di_off_t *, struct di_state *);
311 311 static di_off_t di_getdpdata(struct dev_info *, di_off_t *, struct di_state *);
312 312 static di_off_t di_gethpdata(ddi_hp_cn_handle_t *, di_off_t *,
313 313 struct di_state *);
314 314 static di_off_t di_getprop(int, struct ddi_prop **, di_off_t *,
315 315 struct di_state *, struct dev_info *);
316 316 static void di_allocmem(struct di_state *, size_t);
317 317 static void di_freemem(struct di_state *);
318 318 static void di_copymem(struct di_state *st, caddr_t buf, size_t bufsiz);
319 319 static di_off_t di_checkmem(struct di_state *, di_off_t, size_t);
320 320 static void *di_mem_addr(struct di_state *, di_off_t);
321 321 static int di_setstate(struct di_state *, int);
322 322 static void di_register_dip(struct di_state *, dev_info_t *, di_off_t);
323 323 static void di_register_pip(struct di_state *, mdi_pathinfo_t *, di_off_t);
324 324 static di_off_t di_getpath_data(dev_info_t *, di_off_t *, di_off_t,
325 325 struct di_state *, int);
326 326 static di_off_t di_getlink_data(di_off_t, struct di_state *);
327 327 static int di_dip_find(struct di_state *st, dev_info_t *node, di_off_t *off_p);
328 328
329 329 static int cache_args_valid(struct di_state *st, int *error);
330 330 static int snapshot_is_cacheable(struct di_state *st);
331 331 static int di_cache_lookup(struct di_state *st);
332 332 static int di_cache_update(struct di_state *st);
333 333 static void di_cache_print(di_cache_debug_t msglevel, char *fmt, ...);
334 334 static int build_vhci_list(dev_info_t *vh_devinfo, void *arg);
335 335 static int build_phci_list(dev_info_t *ph_devinfo, void *arg);
336 336 static void di_hotplug_children(struct di_state *st);
337 337
338 338 extern int modrootloaded;
339 339 extern void mdi_walk_vhcis(int (*)(dev_info_t *, void *), void *);
340 340 extern void mdi_vhci_walk_phcis(dev_info_t *,
341 341 int (*)(dev_info_t *, void *), void *);
342 342
343 343
344 344 static struct cb_ops di_cb_ops = {
345 345 di_open, /* open */
346 346 di_close, /* close */
347 347 nodev, /* strategy */
348 348 nodev, /* print */
349 349 nodev, /* dump */
350 350 nodev, /* read */
351 351 nodev, /* write */
352 352 di_ioctl, /* ioctl */
353 353 nodev, /* devmap */
354 354 nodev, /* mmap */
355 355 nodev, /* segmap */
356 356 nochpoll, /* poll */
357 357 ddi_prop_op, /* prop_op */
358 358 NULL, /* streamtab */
359 359 D_NEW | D_MP /* Driver compatibility flag */
360 360 };
361 361
362 362 static struct dev_ops di_ops = {
363 363 DEVO_REV, /* devo_rev, */
364 364 0, /* refcnt */
365 365 di_info, /* info */
366 366 nulldev, /* identify */
367 367 nulldev, /* probe */
368 368 di_attach, /* attach */
369 369 di_detach, /* detach */
370 370 nodev, /* reset */
371 371 &di_cb_ops, /* driver operations */
372 372 NULL /* bus operations */
373 373 };
374 374
375 375 /*
↓ open down ↓ |
375 lines elided |
↑ open up ↑ |
376 376 * Module linkage information for the kernel.
377 377 */
378 378 static struct modldrv modldrv = {
379 379 &mod_driverops,
380 380 "DEVINFO Driver",
381 381 &di_ops
382 382 };
383 383
384 384 static struct modlinkage modlinkage = {
385 385 MODREV_1,
386 - &modldrv,
387 - NULL
386 + { &modldrv, NULL }
388 387 };
389 388
390 389 int
391 390 _init(void)
392 391 {
393 392 int error;
394 393
395 394 mutex_init(&di_lock, NULL, MUTEX_DRIVER, NULL);
396 395
397 396 error = mod_install(&modlinkage);
398 397 if (error != 0) {
399 398 mutex_destroy(&di_lock);
400 399 return (error);
401 400 }
402 401
403 402 return (0);
404 403 }
405 404
406 405 int
407 406 _info(struct modinfo *modinfop)
408 407 {
409 408 return (mod_info(&modlinkage, modinfop));
410 409 }
411 410
412 411 int
413 412 _fini(void)
414 413 {
415 414 int error;
416 415
417 416 error = mod_remove(&modlinkage);
418 417 if (error != 0) {
419 418 return (error);
420 419 }
421 420
422 421 mutex_destroy(&di_lock);
423 422 return (0);
424 423 }
425 424
426 425 static dev_info_t *di_dip;
427 426
428 427 /*ARGSUSED*/
429 428 static int
430 429 di_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
431 430 {
432 431 int error = DDI_FAILURE;
433 432
434 433 switch (infocmd) {
435 434 case DDI_INFO_DEVT2DEVINFO:
436 435 *result = (void *)di_dip;
437 436 error = DDI_SUCCESS;
438 437 break;
439 438 case DDI_INFO_DEVT2INSTANCE:
440 439 /*
441 440 * All dev_t's map to the same, single instance.
442 441 */
443 442 *result = (void *)0;
444 443 error = DDI_SUCCESS;
445 444 break;
446 445 default:
447 446 break;
448 447 }
449 448
450 449 return (error);
451 450 }
452 451
453 452 static int
454 453 di_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
455 454 {
456 455 int error = DDI_FAILURE;
457 456
458 457 switch (cmd) {
459 458 case DDI_ATTACH:
460 459 di_states = kmem_zalloc(
461 460 di_max_opens * sizeof (struct di_state *), KM_SLEEP);
462 461
463 462 if (ddi_create_minor_node(dip, "devinfo", S_IFCHR,
464 463 DI_FULL_PARENT, DDI_PSEUDO, NULL) == DDI_FAILURE ||
465 464 ddi_create_minor_node(dip, "devinfo,ro", S_IFCHR,
466 465 DI_READONLY_PARENT, DDI_PSEUDO, NULL) == DDI_FAILURE) {
467 466 kmem_free(di_states,
468 467 di_max_opens * sizeof (struct di_state *));
469 468 ddi_remove_minor_node(dip, NULL);
470 469 error = DDI_FAILURE;
471 470 } else {
472 471 di_dip = dip;
473 472 ddi_report_dev(dip);
474 473
475 474 error = DDI_SUCCESS;
476 475 }
477 476 break;
478 477 default:
479 478 error = DDI_FAILURE;
480 479 break;
481 480 }
482 481
483 482 return (error);
484 483 }
485 484
486 485 static int
487 486 di_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
488 487 {
489 488 int error = DDI_FAILURE;
490 489
491 490 switch (cmd) {
492 491 case DDI_DETACH:
493 492 ddi_remove_minor_node(dip, NULL);
494 493 di_dip = NULL;
495 494 kmem_free(di_states, di_max_opens * sizeof (struct di_state *));
496 495
497 496 error = DDI_SUCCESS;
498 497 break;
499 498 default:
500 499 error = DDI_FAILURE;
501 500 break;
502 501 }
503 502
504 503 return (error);
505 504 }
506 505
507 506 /*
508 507 * Allow multiple opens by tweaking the dev_t such that it looks like each
509 508 * open is getting a different minor device. Each minor gets a separate
510 509 * entry in the di_states[] table. Based on the original minor number, we
511 510 * discriminate opens of the full and read-only nodes. If all of the instances
512 511 * of the selected minor node are currently open, we return EAGAIN.
513 512 */
514 513 /*ARGSUSED*/
515 514 static int
516 515 di_open(dev_t *devp, int flag, int otyp, cred_t *credp)
517 516 {
518 517 int m;
519 518 minor_t minor_parent = getminor(*devp);
520 519
521 520 if (minor_parent != DI_FULL_PARENT &&
522 521 minor_parent != DI_READONLY_PARENT)
523 522 return (ENXIO);
524 523
525 524 mutex_enter(&di_lock);
526 525
527 526 for (m = minor_parent; m < di_max_opens; m += DI_NODE_SPECIES) {
528 527 if (di_states[m] != NULL)
529 528 continue;
530 529
531 530 di_states[m] = kmem_zalloc(sizeof (struct di_state), KM_SLEEP);
532 531 break; /* It's ours. */
533 532 }
534 533
535 534 if (m >= di_max_opens) {
536 535 /*
537 536 * maximum open instance for device reached
538 537 */
539 538 mutex_exit(&di_lock);
540 539 dcmn_err((CE_WARN, "devinfo: maximum devinfo open reached"));
541 540 return (EAGAIN);
542 541 }
543 542 mutex_exit(&di_lock);
544 543
545 544 ASSERT(m < di_max_opens);
546 545 *devp = makedevice(getmajor(*devp), (minor_t)(m + DI_NODE_SPECIES));
547 546
548 547 dcmn_err((CE_CONT, "di_open: thread = %p, assigned minor = %d\n",
549 548 (void *)curthread, m + DI_NODE_SPECIES));
550 549
551 550 return (0);
552 551 }
553 552
554 553 /*ARGSUSED*/
555 554 static int
556 555 di_close(dev_t dev, int flag, int otype, cred_t *cred_p)
557 556 {
558 557 struct di_state *st;
559 558 int m = (int)getminor(dev) - DI_NODE_SPECIES;
560 559
561 560 if (m < 0) {
562 561 cmn_err(CE_WARN, "closing non-existent devinfo minor %d",
563 562 m + DI_NODE_SPECIES);
564 563 return (ENXIO);
565 564 }
566 565
567 566 st = di_states[m];
568 567 ASSERT(m < di_max_opens && st != NULL);
569 568
570 569 di_freemem(st);
571 570 kmem_free(st, sizeof (struct di_state));
572 571
573 572 /*
574 573 * empty slot in state table
575 574 */
576 575 mutex_enter(&di_lock);
577 576 di_states[m] = NULL;
578 577 dcmn_err((CE_CONT, "di_close: thread = %p, assigned minor = %d\n",
579 578 (void *)curthread, m + DI_NODE_SPECIES));
580 579 mutex_exit(&di_lock);
581 580
582 581 return (0);
583 582 }
584 583
585 584
586 585 /*ARGSUSED*/
587 586 static int
588 587 di_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
589 588 {
590 589 int rv, error;
591 590 di_off_t off;
592 591 struct di_all *all;
593 592 struct di_state *st;
594 593 int m = (int)getminor(dev) - DI_NODE_SPECIES;
595 594 major_t i;
596 595 char *drv_name;
597 596 size_t map_size, size;
598 597 struct di_mem *dcp;
599 598 int ndi_flags;
600 599
601 600 if (m < 0 || m >= di_max_opens) {
602 601 return (ENXIO);
603 602 }
604 603
605 604 st = di_states[m];
606 605 ASSERT(st != NULL);
607 606
608 607 dcmn_err2((CE_CONT, "di_ioctl: mode = %x, cmd = %x\n", mode, cmd));
609 608
610 609 switch (cmd) {
611 610 case DINFOIDENT:
612 611 /*
613 612 * This is called from di_init to verify that the driver
614 613 * opened is indeed devinfo. The purpose is to guard against
615 614 * sending ioctl to an unknown driver in case of an
616 615 * unresolved major number conflict during bfu.
617 616 */
618 617 *rvalp = DI_MAGIC;
619 618 return (0);
620 619
621 620 case DINFOLODRV:
622 621 /*
623 622 * Hold an installed driver and return the result
624 623 */
625 624 if (DI_UNPRIVILEGED_NODE(m)) {
626 625 /*
627 626 * Only the fully enabled instances may issue
628 627 * DINFOLDDRV.
629 628 */
630 629 return (EACCES);
631 630 }
632 631
633 632 drv_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
634 633 if (ddi_copyin((void *)arg, drv_name, MAXNAMELEN, mode) != 0) {
635 634 kmem_free(drv_name, MAXNAMELEN);
636 635 return (EFAULT);
637 636 }
638 637
639 638 /*
640 639 * Some 3rd party driver's _init() walks the device tree,
641 640 * so we load the driver module before configuring driver.
642 641 */
643 642 i = ddi_name_to_major(drv_name);
644 643 if (ddi_hold_driver(i) == NULL) {
645 644 kmem_free(drv_name, MAXNAMELEN);
646 645 return (ENXIO);
647 646 }
648 647
649 648 ndi_flags = NDI_DEVI_PERSIST | NDI_CONFIG | NDI_NO_EVENT;
650 649
651 650 /*
652 651 * i_ddi_load_drvconf() below will trigger a reprobe
653 652 * via reset_nexus_flags(). NDI_DRV_CONF_REPROBE isn't
654 653 * needed here.
655 654 */
656 655 modunload_disable();
657 656 (void) i_ddi_load_drvconf(i);
658 657 (void) ndi_devi_config_driver(ddi_root_node(), ndi_flags, i);
659 658 kmem_free(drv_name, MAXNAMELEN);
660 659 ddi_rele_driver(i);
661 660 rv = i_ddi_devs_attached(i);
662 661 modunload_enable();
663 662
664 663 i_ddi_di_cache_invalidate();
665 664
666 665 return ((rv == DDI_SUCCESS)? 0 : ENXIO);
667 666
668 667 case DINFOUSRLD:
669 668 /*
670 669 * The case for copying snapshot to userland
671 670 */
672 671 if (di_setstate(st, IOC_COPY) == -1)
673 672 return (EBUSY);
674 673
675 674 map_size = DI_ALL_PTR(st)->map_size;
676 675 if (map_size == 0) {
677 676 (void) di_setstate(st, IOC_DONE);
678 677 return (EFAULT);
679 678 }
680 679
681 680 /*
682 681 * copyout the snapshot
683 682 */
684 683 map_size = (map_size + PAGEOFFSET) & PAGEMASK;
685 684
686 685 /*
687 686 * Return the map size, so caller may do a sanity
688 687 * check against the return value of snapshot ioctl()
689 688 */
690 689 *rvalp = (int)map_size;
691 690
692 691 /*
693 692 * Copy one chunk at a time
694 693 */
695 694 off = 0;
696 695 dcp = st->memlist;
697 696 while (map_size) {
698 697 size = dcp->buf_size;
699 698 if (map_size <= size) {
700 699 size = map_size;
701 700 }
702 701
703 702 if (ddi_copyout(di_mem_addr(st, off),
704 703 (void *)(arg + off), size, mode) != 0) {
705 704 (void) di_setstate(st, IOC_DONE);
706 705 return (EFAULT);
707 706 }
708 707
709 708 map_size -= size;
710 709 off += size;
711 710 dcp = dcp->next;
712 711 }
713 712
714 713 di_freemem(st);
715 714 (void) di_setstate(st, IOC_IDLE);
716 715 return (0);
717 716
718 717 default:
719 718 if ((cmd & ~DIIOC_MASK) != DIIOC) {
720 719 /*
721 720 * Invalid ioctl command
722 721 */
723 722 return (ENOTTY);
724 723 }
725 724 /*
726 725 * take a snapshot
727 726 */
728 727 st->command = cmd & DIIOC_MASK;
729 728 /*FALLTHROUGH*/
730 729 }
731 730
732 731 /*
733 732 * Obtain enough memory to hold header + rootpath. We prevent kernel
734 733 * memory exhaustion by freeing any previously allocated snapshot and
735 734 * refusing the operation; otherwise we would be allowing ioctl(),
736 735 * ioctl(), ioctl(), ..., panic.
737 736 */
738 737 if (di_setstate(st, IOC_SNAP) == -1)
739 738 return (EBUSY);
740 739
741 740 /*
742 741 * Initial memlist always holds di_all and the root_path - and
743 742 * is at least a page and size.
744 743 */
745 744 size = sizeof (struct di_all) +
746 745 sizeof (((struct dinfo_io *)(NULL))->root_path);
747 746 if (size < PAGESIZE)
748 747 size = PAGESIZE;
749 748 off = di_checkmem(st, 0, size);
750 749 all = DI_ALL_PTR(st);
751 750 off += sizeof (struct di_all); /* real length of di_all */
752 751
753 752 all->devcnt = devcnt;
754 753 all->command = st->command;
755 754 all->version = DI_SNAPSHOT_VERSION;
756 755 all->top_vhci_devinfo = 0; /* filled by build_vhci_list. */
757 756
758 757 /*
759 758 * Note the endianness in case we need to transport snapshot
760 759 * over the network.
761 760 */
762 761 #if defined(_LITTLE_ENDIAN)
763 762 all->endianness = DI_LITTLE_ENDIAN;
764 763 #else
765 764 all->endianness = DI_BIG_ENDIAN;
766 765 #endif
767 766
768 767 /* Copyin ioctl args, store in the snapshot. */
769 768 if (copyinstr((void *)arg, all->req_path,
770 769 sizeof (((struct dinfo_io *)(NULL))->root_path), &size) != 0) {
771 770 di_freemem(st);
772 771 (void) di_setstate(st, IOC_IDLE);
773 772 return (EFAULT);
774 773 }
775 774 (void) strcpy(all->root_path, all->req_path);
776 775 off += size; /* real length of root_path */
777 776
778 777 if ((st->command & DINFOCLEANUP) && !DEVICES_FILES_CLEANABLE(st)) {
779 778 di_freemem(st);
780 779 (void) di_setstate(st, IOC_IDLE);
781 780 return (EINVAL);
782 781 }
783 782
784 783 error = 0;
785 784 if ((st->command & DINFOCACHE) && !cache_args_valid(st, &error)) {
786 785 di_freemem(st);
787 786 (void) di_setstate(st, IOC_IDLE);
788 787 return (error);
789 788 }
790 789
791 790 /*
792 791 * Only the fully enabled version may force load drivers or read
793 792 * the parent private data from a driver.
794 793 */
795 794 if ((st->command & (DINFOPRIVDATA | DINFOFORCE)) != 0 &&
796 795 DI_UNPRIVILEGED_NODE(m)) {
797 796 di_freemem(st);
798 797 (void) di_setstate(st, IOC_IDLE);
799 798 return (EACCES);
800 799 }
801 800
802 801 /* Do we need private data? */
803 802 if (st->command & DINFOPRIVDATA) {
804 803 arg += sizeof (((struct dinfo_io *)(NULL))->root_path);
805 804
806 805 #ifdef _MULTI_DATAMODEL
807 806 switch (ddi_model_convert_from(mode & FMODELS)) {
808 807 case DDI_MODEL_ILP32: {
809 808 /*
810 809 * Cannot copy private data from 64-bit kernel
811 810 * to 32-bit app
812 811 */
813 812 di_freemem(st);
814 813 (void) di_setstate(st, IOC_IDLE);
815 814 return (EINVAL);
816 815 }
817 816 case DDI_MODEL_NONE:
818 817 if ((off = di_copyformat(off, st, arg, mode)) == 0) {
819 818 di_freemem(st);
820 819 (void) di_setstate(st, IOC_IDLE);
821 820 return (EFAULT);
822 821 }
823 822 break;
824 823 }
825 824 #else /* !_MULTI_DATAMODEL */
826 825 if ((off = di_copyformat(off, st, arg, mode)) == 0) {
827 826 di_freemem(st);
828 827 (void) di_setstate(st, IOC_IDLE);
829 828 return (EFAULT);
830 829 }
831 830 #endif /* _MULTI_DATAMODEL */
832 831 }
833 832
834 833 all->top_devinfo = DI_ALIGN(off);
835 834
836 835 /*
837 836 * For cache lookups we reallocate memory from scratch,
838 837 * so the value of "all" is no longer valid.
839 838 */
840 839 all = NULL;
841 840
842 841 if (st->command & DINFOCACHE) {
843 842 *rvalp = di_cache_lookup(st);
844 843 } else if (snapshot_is_cacheable(st)) {
845 844 DI_CACHE_LOCK(di_cache);
846 845 *rvalp = di_cache_update(st);
847 846 DI_CACHE_UNLOCK(di_cache);
848 847 } else
849 848 *rvalp = di_snapshot_and_clean(st);
850 849
851 850 if (*rvalp) {
852 851 DI_ALL_PTR(st)->map_size = *rvalp;
853 852 (void) di_setstate(st, IOC_DONE);
854 853 } else {
855 854 di_freemem(st);
856 855 (void) di_setstate(st, IOC_IDLE);
857 856 }
858 857
859 858 return (0);
860 859 }
861 860
862 861 /*
863 862 * Get a chunk of memory >= size, for the snapshot
864 863 */
865 864 static void
866 865 di_allocmem(struct di_state *st, size_t size)
867 866 {
868 867 struct di_mem *mem = kmem_zalloc(sizeof (struct di_mem), KM_SLEEP);
869 868
870 869 /*
871 870 * Round up size to nearest power of 2. If it is less
872 871 * than st->mem_size, set it to st->mem_size (i.e.,
873 872 * the mem_size is doubled every time) to reduce the
874 873 * number of memory allocations.
875 874 */
876 875 size_t tmp = 1;
877 876 while (tmp < size) {
878 877 tmp <<= 1;
879 878 }
880 879 size = (tmp > st->mem_size) ? tmp : st->mem_size;
881 880
882 881 mem->buf = ddi_umem_alloc(size, DDI_UMEM_SLEEP, &mem->cook);
883 882 mem->buf_size = size;
884 883
885 884 dcmn_err2((CE_CONT, "di_allocmem: mem_size=%x\n", st->mem_size));
886 885
887 886 if (st->mem_size == 0) { /* first chunk */
888 887 st->memlist = mem;
889 888 } else {
890 889 /*
891 890 * locate end of linked list and add a chunk at the end
892 891 */
893 892 struct di_mem *dcp = st->memlist;
894 893 while (dcp->next != NULL) {
895 894 dcp = dcp->next;
896 895 }
897 896
898 897 dcp->next = mem;
899 898 }
900 899
901 900 st->mem_size += size;
902 901 }
903 902
904 903 /*
905 904 * Copy upto bufsiz bytes of the memlist to buf
906 905 */
907 906 static void
908 907 di_copymem(struct di_state *st, caddr_t buf, size_t bufsiz)
909 908 {
910 909 struct di_mem *dcp;
911 910 size_t copysz;
912 911
913 912 if (st->mem_size == 0) {
914 913 ASSERT(st->memlist == NULL);
915 914 return;
916 915 }
917 916
918 917 copysz = 0;
919 918 for (dcp = st->memlist; dcp; dcp = dcp->next) {
920 919
921 920 ASSERT(bufsiz > 0);
922 921
923 922 if (bufsiz <= dcp->buf_size)
924 923 copysz = bufsiz;
925 924 else
926 925 copysz = dcp->buf_size;
927 926
928 927 bcopy(dcp->buf, buf, copysz);
929 928
930 929 buf += copysz;
931 930 bufsiz -= copysz;
932 931
933 932 if (bufsiz == 0)
934 933 break;
935 934 }
936 935 }
937 936
938 937 /*
939 938 * Free all memory for the snapshot
940 939 */
941 940 static void
942 941 di_freemem(struct di_state *st)
943 942 {
944 943 struct di_mem *dcp, *tmp;
945 944
946 945 dcmn_err2((CE_CONT, "di_freemem\n"));
947 946
948 947 if (st->mem_size) {
949 948 dcp = st->memlist;
950 949 while (dcp) { /* traverse the linked list */
951 950 tmp = dcp;
952 951 dcp = dcp->next;
953 952 ddi_umem_free(tmp->cook);
954 953 kmem_free(tmp, sizeof (struct di_mem));
955 954 }
956 955 st->mem_size = 0;
957 956 st->memlist = NULL;
958 957 }
959 958
960 959 ASSERT(st->mem_size == 0);
961 960 ASSERT(st->memlist == NULL);
962 961 }
963 962
964 963 /*
965 964 * Copies cached data to the di_state structure.
966 965 * Returns:
967 966 * - size of data copied, on SUCCESS
968 967 * - 0 on failure
969 968 */
970 969 static int
971 970 di_cache2mem(struct di_cache *cache, struct di_state *st)
972 971 {
973 972 caddr_t pa;
974 973
975 974 ASSERT(st->mem_size == 0);
976 975 ASSERT(st->memlist == NULL);
977 976 ASSERT(!servicing_interrupt());
978 977 ASSERT(DI_CACHE_LOCKED(*cache));
979 978
980 979 if (cache->cache_size == 0) {
981 980 ASSERT(cache->cache_data == NULL);
982 981 CACHE_DEBUG((DI_ERR, "Empty cache. Skipping copy"));
983 982 return (0);
984 983 }
985 984
986 985 ASSERT(cache->cache_data);
987 986
988 987 di_allocmem(st, cache->cache_size);
989 988
990 989 pa = di_mem_addr(st, 0);
991 990
992 991 ASSERT(pa);
993 992
994 993 /*
995 994 * Verify that di_allocmem() allocates contiguous memory,
996 995 * so that it is safe to do straight bcopy()
997 996 */
998 997 ASSERT(st->memlist != NULL);
999 998 ASSERT(st->memlist->next == NULL);
1000 999 bcopy(cache->cache_data, pa, cache->cache_size);
1001 1000
1002 1001 return (cache->cache_size);
1003 1002 }
1004 1003
1005 1004 /*
1006 1005 * Copies a snapshot from di_state to the cache
1007 1006 * Returns:
1008 1007 * - 0 on failure
1009 1008 * - size of copied data on success
1010 1009 */
1011 1010 static size_t
1012 1011 di_mem2cache(struct di_state *st, struct di_cache *cache)
1013 1012 {
1014 1013 size_t map_size;
1015 1014
1016 1015 ASSERT(cache->cache_size == 0);
1017 1016 ASSERT(cache->cache_data == NULL);
1018 1017 ASSERT(!servicing_interrupt());
1019 1018 ASSERT(DI_CACHE_LOCKED(*cache));
1020 1019
1021 1020 if (st->mem_size == 0) {
1022 1021 ASSERT(st->memlist == NULL);
1023 1022 CACHE_DEBUG((DI_ERR, "Empty memlist. Skipping copy"));
1024 1023 return (0);
1025 1024 }
1026 1025
1027 1026 ASSERT(st->memlist);
1028 1027
1029 1028 /*
1030 1029 * The size of the memory list may be much larger than the
1031 1030 * size of valid data (map_size). Cache only the valid data
1032 1031 */
1033 1032 map_size = DI_ALL_PTR(st)->map_size;
1034 1033 if (map_size == 0 || map_size < sizeof (struct di_all) ||
1035 1034 map_size > st->mem_size) {
1036 1035 CACHE_DEBUG((DI_ERR, "cannot cache: bad size: 0x%x", map_size));
1037 1036 return (0);
1038 1037 }
1039 1038
1040 1039 cache->cache_data = kmem_alloc(map_size, KM_SLEEP);
1041 1040 cache->cache_size = map_size;
1042 1041 di_copymem(st, cache->cache_data, cache->cache_size);
1043 1042
1044 1043 return (map_size);
1045 1044 }
1046 1045
1047 1046 /*
1048 1047 * Make sure there is at least "size" bytes memory left before
1049 1048 * going on. Otherwise, start on a new chunk.
1050 1049 */
1051 1050 static di_off_t
1052 1051 di_checkmem(struct di_state *st, di_off_t off, size_t size)
1053 1052 {
1054 1053 dcmn_err3((CE_CONT, "di_checkmem: off=%x size=%x\n",
1055 1054 off, (int)size));
1056 1055
1057 1056 /*
1058 1057 * di_checkmem() shouldn't be called with a size of zero.
1059 1058 * But in case it is, we want to make sure we return a valid
1060 1059 * offset within the memlist and not an offset that points us
1061 1060 * at the end of the memlist.
1062 1061 */
1063 1062 if (size == 0) {
1064 1063 dcmn_err((CE_WARN, "di_checkmem: invalid zero size used"));
1065 1064 size = 1;
1066 1065 }
1067 1066
1068 1067 off = DI_ALIGN(off);
1069 1068 if ((st->mem_size - off) < size) {
1070 1069 off = st->mem_size;
1071 1070 di_allocmem(st, size);
1072 1071 }
1073 1072
1074 1073 /* verify that return value is aligned */
1075 1074 ASSERT(off == DI_ALIGN(off));
1076 1075 return (off);
1077 1076 }
1078 1077
1079 1078 /*
1080 1079 * Copy the private data format from ioctl arg.
1081 1080 * On success, the ending offset is returned. On error 0 is returned.
1082 1081 */
1083 1082 static di_off_t
1084 1083 di_copyformat(di_off_t off, struct di_state *st, intptr_t arg, int mode)
1085 1084 {
1086 1085 di_off_t size;
1087 1086 struct di_priv_data *priv;
1088 1087 struct di_all *all = DI_ALL_PTR(st);
1089 1088
1090 1089 dcmn_err2((CE_CONT, "di_copyformat: off=%x, arg=%p mode=%x\n",
1091 1090 off, (void *)arg, mode));
1092 1091
1093 1092 /*
1094 1093 * Copyin data and check version.
1095 1094 * We only handle private data version 0.
1096 1095 */
1097 1096 priv = kmem_alloc(sizeof (struct di_priv_data), KM_SLEEP);
1098 1097 if ((ddi_copyin((void *)arg, priv, sizeof (struct di_priv_data),
1099 1098 mode) != 0) || (priv->version != DI_PRIVDATA_VERSION_0)) {
1100 1099 kmem_free(priv, sizeof (struct di_priv_data));
1101 1100 return (0);
1102 1101 }
1103 1102
1104 1103 /*
1105 1104 * Save di_priv_data copied from userland in snapshot.
1106 1105 */
1107 1106 all->pd_version = priv->version;
1108 1107 all->n_ppdata = priv->n_parent;
1109 1108 all->n_dpdata = priv->n_driver;
1110 1109
1111 1110 /*
1112 1111 * copyin private data format, modify offset accordingly
1113 1112 */
1114 1113 if (all->n_ppdata) { /* parent private data format */
1115 1114 /*
1116 1115 * check memory
1117 1116 */
1118 1117 size = all->n_ppdata * sizeof (struct di_priv_format);
1119 1118 all->ppdata_format = off = di_checkmem(st, off, size);
1120 1119 if (ddi_copyin(priv->parent, di_mem_addr(st, off), size,
1121 1120 mode) != 0) {
1122 1121 kmem_free(priv, sizeof (struct di_priv_data));
1123 1122 return (0);
1124 1123 }
1125 1124
1126 1125 off += size;
1127 1126 }
1128 1127
1129 1128 if (all->n_dpdata) { /* driver private data format */
1130 1129 /*
1131 1130 * check memory
1132 1131 */
1133 1132 size = all->n_dpdata * sizeof (struct di_priv_format);
1134 1133 all->dpdata_format = off = di_checkmem(st, off, size);
1135 1134 if (ddi_copyin(priv->driver, di_mem_addr(st, off), size,
1136 1135 mode) != 0) {
1137 1136 kmem_free(priv, sizeof (struct di_priv_data));
1138 1137 return (0);
1139 1138 }
1140 1139
1141 1140 off += size;
1142 1141 }
1143 1142
1144 1143 kmem_free(priv, sizeof (struct di_priv_data));
1145 1144 return (off);
1146 1145 }
1147 1146
1148 1147 /*
1149 1148 * Return the real address based on the offset (off) within snapshot
1150 1149 */
1151 1150 static void *
1152 1151 di_mem_addr(struct di_state *st, di_off_t off)
1153 1152 {
1154 1153 struct di_mem *dcp = st->memlist;
1155 1154
1156 1155 dcmn_err3((CE_CONT, "di_mem_addr: dcp=%p off=%x\n",
1157 1156 (void *)dcp, off));
1158 1157
1159 1158 ASSERT(off < st->mem_size);
1160 1159
1161 1160 while (off >= dcp->buf_size) {
1162 1161 off -= dcp->buf_size;
1163 1162 dcp = dcp->next;
1164 1163 }
1165 1164
1166 1165 dcmn_err3((CE_CONT, "di_mem_addr: new off=%x, return = %p\n",
1167 1166 off, (void *)(dcp->buf + off)));
1168 1167
1169 1168 return (dcp->buf + off);
1170 1169 }
1171 1170
1172 1171 /*
1173 1172 * Ideally we would use the whole key to derive the hash
1174 1173 * value. However, the probability that two keys will
1175 1174 * have the same dip (or pip) is very low, so
1176 1175 * hashing by dip (or pip) pointer should suffice.
1177 1176 */
1178 1177 static uint_t
1179 1178 di_hash_byptr(void *arg, mod_hash_key_t key)
1180 1179 {
1181 1180 struct di_key *dik = key;
1182 1181 size_t rshift;
1183 1182 void *ptr;
1184 1183
1185 1184 ASSERT(arg == NULL);
1186 1185
1187 1186 switch (dik->k_type) {
1188 1187 case DI_DKEY:
1189 1188 ptr = dik->k_u.dkey.dk_dip;
1190 1189 rshift = highbit(sizeof (struct dev_info));
1191 1190 break;
1192 1191 case DI_PKEY:
1193 1192 ptr = dik->k_u.pkey.pk_pip;
1194 1193 rshift = highbit(sizeof (struct mdi_pathinfo));
1195 1194 break;
1196 1195 default:
1197 1196 panic("devinfo: unknown key type");
1198 1197 /*NOTREACHED*/
1199 1198 }
1200 1199 return (mod_hash_byptr((void *)rshift, ptr));
1201 1200 }
1202 1201
1203 1202 static void
1204 1203 di_key_dtor(mod_hash_key_t key)
1205 1204 {
1206 1205 char *path_addr;
1207 1206 struct di_key *dik = key;
1208 1207
1209 1208 switch (dik->k_type) {
1210 1209 case DI_DKEY:
1211 1210 break;
1212 1211 case DI_PKEY:
1213 1212 path_addr = dik->k_u.pkey.pk_path_addr;
1214 1213 if (path_addr)
1215 1214 kmem_free(path_addr, strlen(path_addr) + 1);
1216 1215 break;
1217 1216 default:
1218 1217 panic("devinfo: unknown key type");
1219 1218 /*NOTREACHED*/
1220 1219 }
1221 1220
1222 1221 kmem_free(dik, sizeof (struct di_key));
1223 1222 }
1224 1223
1225 1224 static int
1226 1225 di_dkey_cmp(struct di_dkey *dk1, struct di_dkey *dk2)
1227 1226 {
1228 1227 if (dk1->dk_dip != dk2->dk_dip)
1229 1228 return (dk1->dk_dip > dk2->dk_dip ? 1 : -1);
1230 1229
1231 1230 if (dk1->dk_major != DDI_MAJOR_T_NONE &&
1232 1231 dk2->dk_major != DDI_MAJOR_T_NONE) {
1233 1232 if (dk1->dk_major != dk2->dk_major)
1234 1233 return (dk1->dk_major > dk2->dk_major ? 1 : -1);
1235 1234
1236 1235 if (dk1->dk_inst != dk2->dk_inst)
1237 1236 return (dk1->dk_inst > dk2->dk_inst ? 1 : -1);
1238 1237 }
1239 1238
1240 1239 if (dk1->dk_nodeid != dk2->dk_nodeid)
1241 1240 return (dk1->dk_nodeid > dk2->dk_nodeid ? 1 : -1);
1242 1241
1243 1242 return (0);
1244 1243 }
1245 1244
1246 1245 static int
1247 1246 di_pkey_cmp(struct di_pkey *pk1, struct di_pkey *pk2)
1248 1247 {
1249 1248 char *p1, *p2;
1250 1249 int rv;
1251 1250
1252 1251 if (pk1->pk_pip != pk2->pk_pip)
1253 1252 return (pk1->pk_pip > pk2->pk_pip ? 1 : -1);
1254 1253
1255 1254 p1 = pk1->pk_path_addr;
1256 1255 p2 = pk2->pk_path_addr;
1257 1256
1258 1257 p1 = p1 ? p1 : "";
1259 1258 p2 = p2 ? p2 : "";
1260 1259
1261 1260 rv = strcmp(p1, p2);
1262 1261 if (rv)
1263 1262 return (rv > 0 ? 1 : -1);
1264 1263
1265 1264 if (pk1->pk_client != pk2->pk_client)
1266 1265 return (pk1->pk_client > pk2->pk_client ? 1 : -1);
1267 1266
1268 1267 if (pk1->pk_phci != pk2->pk_phci)
1269 1268 return (pk1->pk_phci > pk2->pk_phci ? 1 : -1);
1270 1269
1271 1270 return (0);
1272 1271 }
1273 1272
1274 1273 static int
1275 1274 di_key_cmp(mod_hash_key_t key1, mod_hash_key_t key2)
1276 1275 {
1277 1276 struct di_key *dik1, *dik2;
1278 1277
1279 1278 dik1 = key1;
1280 1279 dik2 = key2;
1281 1280
1282 1281 if (dik1->k_type != dik2->k_type) {
1283 1282 panic("devinfo: mismatched keys");
1284 1283 /*NOTREACHED*/
1285 1284 }
1286 1285
1287 1286 switch (dik1->k_type) {
1288 1287 case DI_DKEY:
1289 1288 return (di_dkey_cmp(&(dik1->k_u.dkey), &(dik2->k_u.dkey)));
1290 1289 case DI_PKEY:
1291 1290 return (di_pkey_cmp(&(dik1->k_u.pkey), &(dik2->k_u.pkey)));
1292 1291 default:
1293 1292 panic("devinfo: unknown key type");
1294 1293 /*NOTREACHED*/
1295 1294 }
1296 1295 }
1297 1296
1298 1297 static void
1299 1298 di_copy_aliases(struct di_state *st, alias_pair_t *apair, di_off_t *offp)
1300 1299 {
1301 1300 di_off_t off;
1302 1301 struct di_all *all = DI_ALL_PTR(st);
1303 1302 struct di_alias *di_alias;
1304 1303 di_off_t curroff;
1305 1304 dev_info_t *currdip;
1306 1305 size_t size;
1307 1306
1308 1307 currdip = NULL;
1309 1308 if (resolve_pathname(apair->pair_alias, &currdip, NULL, NULL) != 0) {
1310 1309 return;
1311 1310 }
1312 1311
1313 1312 if (di_dip_find(st, currdip, &curroff) != 0) {
1314 1313 ndi_rele_devi(currdip);
1315 1314 return;
1316 1315 }
1317 1316 ndi_rele_devi(currdip);
1318 1317
1319 1318 off = *offp;
1320 1319 size = sizeof (struct di_alias);
1321 1320 size += strlen(apair->pair_alias) + 1;
1322 1321 off = di_checkmem(st, off, size);
1323 1322 di_alias = DI_ALIAS(di_mem_addr(st, off));
1324 1323
1325 1324 di_alias->self = off;
1326 1325 di_alias->next = all->aliases;
1327 1326 all->aliases = off;
1328 1327 (void) strcpy(di_alias->alias, apair->pair_alias);
1329 1328 di_alias->curroff = curroff;
1330 1329
1331 1330 off += size;
1332 1331
1333 1332 *offp = off;
1334 1333 }
1335 1334
1336 1335 /*
1337 1336 * This is the main function that takes a snapshot
1338 1337 */
1339 1338 static di_off_t
1340 1339 di_snapshot(struct di_state *st)
1341 1340 {
1342 1341 di_off_t off;
1343 1342 struct di_all *all;
1344 1343 dev_info_t *rootnode;
1345 1344 char buf[80];
1346 1345 int plen;
1347 1346 char *path;
1348 1347 vnode_t *vp;
1349 1348 int i;
1350 1349
1351 1350 all = DI_ALL_PTR(st);
1352 1351 dcmn_err((CE_CONT, "Taking a snapshot of devinfo tree...\n"));
1353 1352
1354 1353 /*
1355 1354 * Translate requested root path if an alias and snap-root != "/"
1356 1355 */
1357 1356 if (ddi_aliases_present == B_TRUE && strcmp(all->root_path, "/") != 0) {
1358 1357 /* If there is no redirected alias, use root_path as is */
1359 1358 rootnode = ddi_alias_redirect(all->root_path);
1360 1359 if (rootnode) {
1361 1360 (void) ddi_pathname(rootnode, all->root_path);
1362 1361 goto got_root;
1363 1362 }
1364 1363 }
1365 1364
1366 1365 /*
1367 1366 * Verify path before entrusting it to e_ddi_hold_devi_by_path because
1368 1367 * some platforms have OBP bugs where executing the NDI_PROMNAME code
1369 1368 * path against an invalid path results in panic. The lookupnameat
1370 1369 * is done relative to rootdir without a leading '/' on "devices/"
1371 1370 * to force the lookup to occur in the global zone.
1372 1371 */
1373 1372 plen = strlen("devices/") + strlen(all->root_path) + 1;
1374 1373 path = kmem_alloc(plen, KM_SLEEP);
1375 1374 (void) snprintf(path, plen, "devices/%s", all->root_path);
1376 1375 if (lookupnameat(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp, rootdir)) {
1377 1376 dcmn_err((CE_CONT, "Devinfo node %s not found\n",
1378 1377 all->root_path));
1379 1378 kmem_free(path, plen);
1380 1379 return (0);
1381 1380 }
1382 1381 kmem_free(path, plen);
1383 1382 VN_RELE(vp);
1384 1383
1385 1384 /*
1386 1385 * Hold the devinfo node referred by the path.
1387 1386 */
1388 1387 rootnode = e_ddi_hold_devi_by_path(all->root_path, 0);
1389 1388 if (rootnode == NULL) {
1390 1389 dcmn_err((CE_CONT, "Devinfo node %s not found\n",
1391 1390 all->root_path));
1392 1391 return (0);
1393 1392 }
1394 1393
1395 1394 got_root:
1396 1395 (void) snprintf(buf, sizeof (buf),
1397 1396 "devinfo registered dips (statep=%p)", (void *)st);
1398 1397
1399 1398 st->reg_dip_hash = mod_hash_create_extended(buf, 64,
1400 1399 di_key_dtor, mod_hash_null_valdtor, di_hash_byptr,
1401 1400 NULL, di_key_cmp, KM_SLEEP);
1402 1401
1403 1402
1404 1403 (void) snprintf(buf, sizeof (buf),
1405 1404 "devinfo registered pips (statep=%p)", (void *)st);
1406 1405
1407 1406 st->reg_pip_hash = mod_hash_create_extended(buf, 64,
1408 1407 di_key_dtor, mod_hash_null_valdtor, di_hash_byptr,
1409 1408 NULL, di_key_cmp, KM_SLEEP);
1410 1409
1411 1410 if (DINFOHP & st->command) {
1412 1411 list_create(&st->hp_list, sizeof (i_hp_t),
1413 1412 offsetof(i_hp_t, hp_link));
1414 1413 }
1415 1414
1416 1415 /*
1417 1416 * copy the device tree
1418 1417 */
1419 1418 off = di_copytree(DEVI(rootnode), &all->top_devinfo, st);
1420 1419
1421 1420 if (DINFOPATH & st->command) {
1422 1421 mdi_walk_vhcis(build_vhci_list, st);
1423 1422 }
1424 1423
1425 1424 if (DINFOHP & st->command) {
1426 1425 di_hotplug_children(st);
1427 1426 }
1428 1427
1429 1428 ddi_release_devi(rootnode);
1430 1429
1431 1430 /*
1432 1431 * copy the devnames array
1433 1432 */
1434 1433 all->devnames = off;
1435 1434 off = di_copydevnm(&all->devnames, st);
1436 1435
1437 1436
1438 1437 /* initialize the hash tables */
1439 1438 st->lnode_count = 0;
1440 1439 st->link_count = 0;
1441 1440
1442 1441 if (DINFOLYR & st->command) {
1443 1442 off = di_getlink_data(off, st);
1444 1443 }
1445 1444
1446 1445 all->aliases = 0;
1447 1446 if (ddi_aliases_present == B_FALSE)
1448 1447 goto done;
1449 1448
1450 1449 for (i = 0; i < ddi_aliases.dali_num_pairs; i++) {
1451 1450 di_copy_aliases(st, &(ddi_aliases.dali_alias_pairs[i]), &off);
1452 1451 }
1453 1452
1454 1453 done:
1455 1454 /*
1456 1455 * Free up hash tables
1457 1456 */
1458 1457 mod_hash_destroy_hash(st->reg_dip_hash);
1459 1458 mod_hash_destroy_hash(st->reg_pip_hash);
1460 1459
1461 1460 /*
1462 1461 * Record the timestamp now that we are done with snapshot.
1463 1462 *
1464 1463 * We compute the checksum later and then only if we cache
1465 1464 * the snapshot, since checksumming adds some overhead.
1466 1465 * The checksum is checked later if we read the cache file.
1467 1466 * from disk.
1468 1467 *
1469 1468 * Set checksum field to 0 as CRC is calculated with that
1470 1469 * field set to 0.
1471 1470 */
1472 1471 all->snapshot_time = ddi_get_time();
1473 1472 all->cache_checksum = 0;
1474 1473
1475 1474 ASSERT(all->snapshot_time != 0);
1476 1475
1477 1476 return (off);
1478 1477 }
1479 1478
1480 1479 /*
1481 1480 * Take a snapshot and clean /etc/devices files if DINFOCLEANUP is set
1482 1481 */
1483 1482 static di_off_t
1484 1483 di_snapshot_and_clean(struct di_state *st)
1485 1484 {
1486 1485 di_off_t off;
1487 1486
1488 1487 modunload_disable();
1489 1488 off = di_snapshot(st);
1490 1489 if (off != 0 && (st->command & DINFOCLEANUP)) {
1491 1490 ASSERT(DEVICES_FILES_CLEANABLE(st));
1492 1491 /*
1493 1492 * Cleanup /etc/devices files:
1494 1493 * In order to accurately account for the system configuration
1495 1494 * in /etc/devices files, the appropriate drivers must be
1496 1495 * fully configured before the cleanup starts.
1497 1496 * So enable modunload only after the cleanup.
1498 1497 */
1499 1498 i_ddi_clean_devices_files();
1500 1499 /*
1501 1500 * Remove backing store nodes for unused devices,
1502 1501 * which retain past permissions customizations
1503 1502 * and may be undesired for newly configured devices.
1504 1503 */
1505 1504 dev_devices_cleanup();
1506 1505 }
1507 1506 modunload_enable();
1508 1507
1509 1508 return (off);
1510 1509 }
1511 1510
1512 1511 /*
1513 1512 * construct vhci linkage in the snapshot.
1514 1513 */
1515 1514 static int
1516 1515 build_vhci_list(dev_info_t *vh_devinfo, void *arg)
1517 1516 {
1518 1517 struct di_all *all;
1519 1518 struct di_node *me;
1520 1519 struct di_state *st;
1521 1520 di_off_t off;
1522 1521 phci_walk_arg_t pwa;
1523 1522
1524 1523 dcmn_err3((CE_CONT, "build_vhci list\n"));
1525 1524
1526 1525 dcmn_err3((CE_CONT, "vhci node %s%d\n",
1527 1526 ddi_driver_name(vh_devinfo), ddi_get_instance(vh_devinfo)));
1528 1527
1529 1528 st = (struct di_state *)arg;
1530 1529 if (di_dip_find(st, vh_devinfo, &off) != 0) {
1531 1530 dcmn_err((CE_WARN, "di_dip_find error for the given node\n"));
1532 1531 return (DDI_WALK_TERMINATE);
1533 1532 }
1534 1533
1535 1534 dcmn_err3((CE_CONT, "st->mem_size: %d vh_devinfo off: 0x%x\n",
1536 1535 st->mem_size, off));
1537 1536
1538 1537 all = DI_ALL_PTR(st);
1539 1538 if (all->top_vhci_devinfo == 0) {
1540 1539 all->top_vhci_devinfo = off;
1541 1540 } else {
1542 1541 me = DI_NODE(di_mem_addr(st, all->top_vhci_devinfo));
1543 1542
1544 1543 while (me->next_vhci != 0) {
1545 1544 me = DI_NODE(di_mem_addr(st, me->next_vhci));
1546 1545 }
1547 1546
1548 1547 me->next_vhci = off;
1549 1548 }
1550 1549
1551 1550 pwa.off = off;
1552 1551 pwa.st = st;
1553 1552 mdi_vhci_walk_phcis(vh_devinfo, build_phci_list, &pwa);
1554 1553
1555 1554 return (DDI_WALK_CONTINUE);
1556 1555 }
1557 1556
1558 1557 /*
1559 1558 * construct phci linkage for the given vhci in the snapshot.
1560 1559 */
1561 1560 static int
1562 1561 build_phci_list(dev_info_t *ph_devinfo, void *arg)
1563 1562 {
1564 1563 struct di_node *vh_di_node;
1565 1564 struct di_node *me;
1566 1565 phci_walk_arg_t *pwa;
1567 1566 di_off_t off;
1568 1567
1569 1568 pwa = (phci_walk_arg_t *)arg;
1570 1569
1571 1570 dcmn_err3((CE_CONT, "build_phci list for vhci at offset: 0x%x\n",
1572 1571 pwa->off));
1573 1572
1574 1573 vh_di_node = DI_NODE(di_mem_addr(pwa->st, pwa->off));
1575 1574 if (di_dip_find(pwa->st, ph_devinfo, &off) != 0) {
1576 1575 dcmn_err((CE_WARN, "di_dip_find error for the given node\n"));
1577 1576 return (DDI_WALK_TERMINATE);
1578 1577 }
1579 1578
1580 1579 dcmn_err3((CE_CONT, "phci node %s%d, at offset 0x%x\n",
1581 1580 ddi_driver_name(ph_devinfo), ddi_get_instance(ph_devinfo), off));
1582 1581
1583 1582 if (vh_di_node->top_phci == 0) {
1584 1583 vh_di_node->top_phci = off;
1585 1584 return (DDI_WALK_CONTINUE);
1586 1585 }
1587 1586
1588 1587 me = DI_NODE(di_mem_addr(pwa->st, vh_di_node->top_phci));
1589 1588
1590 1589 while (me->next_phci != 0) {
1591 1590 me = DI_NODE(di_mem_addr(pwa->st, me->next_phci));
1592 1591 }
1593 1592 me->next_phci = off;
1594 1593
1595 1594 return (DDI_WALK_CONTINUE);
1596 1595 }
1597 1596
1598 1597 /*
1599 1598 * Assumes all devinfo nodes in device tree have been snapshotted
1600 1599 */
1601 1600 static void
1602 1601 snap_driver_list(struct di_state *st, struct devnames *dnp, di_off_t *off_p)
1603 1602 {
1604 1603 struct dev_info *node;
1605 1604 struct di_node *me;
1606 1605 di_off_t off;
1607 1606
1608 1607 ASSERT(mutex_owned(&dnp->dn_lock));
1609 1608
1610 1609 node = DEVI(dnp->dn_head);
1611 1610 for (; node; node = node->devi_next) {
1612 1611 if (di_dip_find(st, (dev_info_t *)node, &off) != 0)
1613 1612 continue;
1614 1613
1615 1614 ASSERT(off > 0);
1616 1615 me = DI_NODE(di_mem_addr(st, off));
1617 1616 ASSERT(me->next == 0 || me->next == -1);
1618 1617 /*
1619 1618 * Only nodes which were BOUND when they were
1620 1619 * snapshotted will be added to per-driver list.
1621 1620 */
1622 1621 if (me->next != -1)
1623 1622 continue;
1624 1623
1625 1624 *off_p = off;
1626 1625 off_p = &me->next;
1627 1626 }
1628 1627
1629 1628 *off_p = 0;
1630 1629 }
1631 1630
1632 1631 /*
1633 1632 * Copy the devnames array, so we have a list of drivers in the snapshot.
1634 1633 * Also makes it possible to locate the per-driver devinfo nodes.
1635 1634 */
1636 1635 static di_off_t
1637 1636 di_copydevnm(di_off_t *off_p, struct di_state *st)
1638 1637 {
1639 1638 int i;
1640 1639 di_off_t off;
1641 1640 size_t size;
1642 1641 struct di_devnm *dnp;
1643 1642
1644 1643 dcmn_err2((CE_CONT, "di_copydevnm: *off_p = %p\n", (void *)off_p));
1645 1644
1646 1645 /*
1647 1646 * make sure there is some allocated memory
1648 1647 */
1649 1648 size = devcnt * sizeof (struct di_devnm);
1650 1649 *off_p = off = di_checkmem(st, *off_p, size);
1651 1650 dnp = DI_DEVNM(di_mem_addr(st, off));
1652 1651 off += size;
1653 1652
1654 1653 dcmn_err((CE_CONT, "Start copying devnamesp[%d] at offset 0x%x\n",
1655 1654 devcnt, off));
1656 1655
1657 1656 for (i = 0; i < devcnt; i++) {
1658 1657 if (devnamesp[i].dn_name == NULL) {
1659 1658 continue;
1660 1659 }
1661 1660
1662 1661 /*
1663 1662 * dn_name is not freed during driver unload or removal.
1664 1663 *
1665 1664 * There is a race condition when make_devname() changes
1666 1665 * dn_name during our strcpy. This should be rare since
1667 1666 * only add_drv does this. At any rate, we never had a
1668 1667 * problem with ddi_name_to_major(), which should have
1669 1668 * the same problem.
1670 1669 */
1671 1670 dcmn_err2((CE_CONT, "di_copydevnm: %s%d, off=%x\n",
1672 1671 devnamesp[i].dn_name, devnamesp[i].dn_instance, off));
1673 1672
1674 1673 size = strlen(devnamesp[i].dn_name) + 1;
1675 1674 dnp[i].name = off = di_checkmem(st, off, size);
1676 1675 (void) strcpy((char *)di_mem_addr(st, off),
1677 1676 devnamesp[i].dn_name);
1678 1677 off += size;
1679 1678
1680 1679 mutex_enter(&devnamesp[i].dn_lock);
1681 1680
1682 1681 /*
1683 1682 * Snapshot per-driver node list
1684 1683 */
1685 1684 snap_driver_list(st, &devnamesp[i], &dnp[i].head);
1686 1685
1687 1686 /*
1688 1687 * This is not used by libdevinfo, leave it for now
1689 1688 */
1690 1689 dnp[i].flags = devnamesp[i].dn_flags;
1691 1690 dnp[i].instance = devnamesp[i].dn_instance;
1692 1691
1693 1692 /*
1694 1693 * get global properties
1695 1694 */
1696 1695 if ((DINFOPROP & st->command) &&
1697 1696 devnamesp[i].dn_global_prop_ptr) {
1698 1697 dnp[i].global_prop = off;
1699 1698 off = di_getprop(DI_PROP_GLB_LIST,
1700 1699 &devnamesp[i].dn_global_prop_ptr->prop_list,
1701 1700 &dnp[i].global_prop, st, NULL);
1702 1701 }
1703 1702
1704 1703 /*
1705 1704 * Bit encode driver ops: & bus_ops, cb_ops, & cb_ops->cb_str
1706 1705 */
1707 1706 if (CB_DRV_INSTALLED(devopsp[i])) {
1708 1707 if (devopsp[i]->devo_cb_ops) {
1709 1708 dnp[i].ops |= DI_CB_OPS;
1710 1709 if (devopsp[i]->devo_cb_ops->cb_str)
1711 1710 dnp[i].ops |= DI_STREAM_OPS;
1712 1711 }
1713 1712 if (NEXUS_DRV(devopsp[i])) {
1714 1713 dnp[i].ops |= DI_BUS_OPS;
1715 1714 }
1716 1715 }
1717 1716
1718 1717 mutex_exit(&devnamesp[i].dn_lock);
1719 1718 }
1720 1719
1721 1720 dcmn_err((CE_CONT, "End copying devnamesp at offset 0x%x\n", off));
1722 1721
1723 1722 return (off);
1724 1723 }
1725 1724
1726 1725 /*
1727 1726 * Copy the kernel devinfo tree. The tree and the devnames array forms
1728 1727 * the entire snapshot (see also di_copydevnm).
1729 1728 */
1730 1729 static di_off_t
1731 1730 di_copytree(struct dev_info *root, di_off_t *off_p, struct di_state *st)
1732 1731 {
1733 1732 di_off_t off;
1734 1733 struct dev_info *node;
1735 1734 struct di_stack *dsp = kmem_zalloc(sizeof (struct di_stack), KM_SLEEP);
1736 1735
1737 1736 dcmn_err((CE_CONT, "di_copytree: root = %p, *off_p = %x\n",
1738 1737 (void *)root, *off_p));
1739 1738
1740 1739 /* force attach drivers */
1741 1740 if (i_ddi_devi_attached((dev_info_t *)root) &&
1742 1741 (st->command & DINFOSUBTREE) && (st->command & DINFOFORCE)) {
1743 1742 (void) ndi_devi_config((dev_info_t *)root,
1744 1743 NDI_CONFIG | NDI_DEVI_PERSIST | NDI_NO_EVENT |
1745 1744 NDI_DRV_CONF_REPROBE);
1746 1745 }
1747 1746
1748 1747 /*
1749 1748 * Push top_devinfo onto a stack
1750 1749 *
1751 1750 * The stack is necessary to avoid recursion, which can overrun
1752 1751 * the kernel stack.
1753 1752 */
1754 1753 PUSH_STACK(dsp, root, off_p);
1755 1754
1756 1755 /*
1757 1756 * As long as there is a node on the stack, copy the node.
1758 1757 * di_copynode() is responsible for pushing and popping
1759 1758 * child and sibling nodes on the stack.
1760 1759 */
1761 1760 while (!EMPTY_STACK(dsp)) {
1762 1761 node = TOP_NODE(dsp);
1763 1762 off = di_copynode(node, dsp, st);
1764 1763 }
1765 1764
1766 1765 /*
1767 1766 * Free the stack structure
1768 1767 */
1769 1768 kmem_free(dsp, sizeof (struct di_stack));
1770 1769
1771 1770 return (off);
1772 1771 }
1773 1772
1774 1773 /*
1775 1774 * This is the core function, which copies all data associated with a single
1776 1775 * node into the snapshot. The amount of information is determined by the
1777 1776 * ioctl command.
1778 1777 */
1779 1778 static di_off_t
1780 1779 di_copynode(struct dev_info *node, struct di_stack *dsp, struct di_state *st)
1781 1780 {
1782 1781 di_off_t off;
1783 1782 struct di_node *me;
1784 1783 size_t size;
1785 1784 struct dev_info *n;
1786 1785
1787 1786 dcmn_err2((CE_CONT, "di_copynode: depth = %x\n", dsp->depth));
1788 1787 ASSERT((node != NULL) && (node == TOP_NODE(dsp)));
1789 1788
1790 1789 /*
1791 1790 * check memory usage, and fix offsets accordingly.
1792 1791 */
1793 1792 size = sizeof (struct di_node);
1794 1793 *(TOP_OFFSET(dsp)) = off = di_checkmem(st, *(TOP_OFFSET(dsp)), size);
1795 1794 me = DI_NODE(di_mem_addr(st, off));
1796 1795 me->self = off;
1797 1796 off += size;
1798 1797
1799 1798 dcmn_err((CE_CONT, "copy node %s, instance #%d, at offset 0x%x\n",
1800 1799 node->devi_node_name, node->devi_instance, off));
1801 1800
1802 1801 /*
1803 1802 * Node parameters:
1804 1803 * self -- offset of current node within snapshot
1805 1804 * nodeid -- pointer to PROM node (tri-valued)
1806 1805 * state -- hot plugging device state
1807 1806 * node_state -- devinfo node state
1808 1807 */
1809 1808 me->instance = node->devi_instance;
1810 1809 me->nodeid = node->devi_nodeid;
1811 1810 me->node_class = node->devi_node_class;
1812 1811 me->attributes = node->devi_node_attributes;
1813 1812 me->state = node->devi_state;
1814 1813 me->flags = node->devi_flags;
1815 1814 me->node_state = node->devi_node_state;
1816 1815 me->next_vhci = 0; /* Filled up by build_vhci_list. */
1817 1816 me->top_phci = 0; /* Filled up by build_phci_list. */
1818 1817 me->next_phci = 0; /* Filled up by build_phci_list. */
1819 1818 me->multipath_component = MULTIPATH_COMPONENT_NONE; /* set default. */
1820 1819 me->user_private_data = NULL;
1821 1820
1822 1821 /*
1823 1822 * Get parent's offset in snapshot from the stack
1824 1823 * and store it in the current node
1825 1824 */
1826 1825 if (dsp->depth > 1) {
1827 1826 me->parent = *(PARENT_OFFSET(dsp));
1828 1827 }
1829 1828
1830 1829 /*
1831 1830 * Save the offset of this di_node in a hash table.
1832 1831 * This is used later to resolve references to this
1833 1832 * dip from other parts of the tree (per-driver list,
1834 1833 * multipathing linkages, layered usage linkages).
1835 1834 * The key used for the hash table is derived from
1836 1835 * information in the dip.
1837 1836 */
1838 1837 di_register_dip(st, (dev_info_t *)node, me->self);
1839 1838
1840 1839 #ifdef DEVID_COMPATIBILITY
1841 1840 /* check for devid as property marker */
1842 1841 if (node->devi_devid_str) {
1843 1842 ddi_devid_t devid;
1844 1843
1845 1844 /*
1846 1845 * The devid is now represented as a property. For
1847 1846 * compatibility with di_devid() interface in libdevinfo we
1848 1847 * must return it as a binary structure in the snapshot. When
1849 1848 * (if) di_devid() is removed from libdevinfo then the code
1850 1849 * related to DEVID_COMPATIBILITY can be removed.
1851 1850 */
1852 1851 if (ddi_devid_str_decode(node->devi_devid_str, &devid, NULL) ==
1853 1852 DDI_SUCCESS) {
1854 1853 size = ddi_devid_sizeof(devid);
1855 1854 off = di_checkmem(st, off, size);
1856 1855 me->devid = off;
1857 1856 bcopy(devid, di_mem_addr(st, off), size);
1858 1857 off += size;
1859 1858 ddi_devid_free(devid);
1860 1859 }
1861 1860 }
1862 1861 #endif /* DEVID_COMPATIBILITY */
1863 1862
1864 1863 if (node->devi_node_name) {
1865 1864 size = strlen(node->devi_node_name) + 1;
1866 1865 me->node_name = off = di_checkmem(st, off, size);
1867 1866 (void) strcpy(di_mem_addr(st, off), node->devi_node_name);
1868 1867 off += size;
1869 1868 }
1870 1869
1871 1870 if (node->devi_compat_names && (node->devi_compat_length > 1)) {
1872 1871 size = node->devi_compat_length;
1873 1872 me->compat_names = off = di_checkmem(st, off, size);
1874 1873 me->compat_length = (int)size;
1875 1874 bcopy(node->devi_compat_names, di_mem_addr(st, off), size);
1876 1875 off += size;
1877 1876 }
1878 1877
1879 1878 if (node->devi_addr) {
1880 1879 size = strlen(node->devi_addr) + 1;
1881 1880 me->address = off = di_checkmem(st, off, size);
1882 1881 (void) strcpy(di_mem_addr(st, off), node->devi_addr);
1883 1882 off += size;
1884 1883 }
1885 1884
1886 1885 if (node->devi_binding_name) {
1887 1886 size = strlen(node->devi_binding_name) + 1;
1888 1887 me->bind_name = off = di_checkmem(st, off, size);
1889 1888 (void) strcpy(di_mem_addr(st, off), node->devi_binding_name);
1890 1889 off += size;
1891 1890 }
1892 1891
1893 1892 me->drv_major = node->devi_major;
1894 1893
1895 1894 /*
1896 1895 * If the dip is BOUND, set the next pointer of the
1897 1896 * per-instance list to -1, indicating that it is yet to be resolved.
1898 1897 * This will be resolved later in snap_driver_list().
1899 1898 */
1900 1899 if (me->drv_major != -1) {
1901 1900 me->next = -1;
1902 1901 } else {
1903 1902 me->next = 0;
1904 1903 }
1905 1904
1906 1905 /*
1907 1906 * An optimization to skip mutex_enter when not needed.
1908 1907 */
1909 1908 if (!((DINFOMINOR | DINFOPROP | DINFOPATH | DINFOHP) & st->command)) {
1910 1909 goto priv_data;
1911 1910 }
1912 1911
1913 1912 /*
1914 1913 * LOCKING: We already have an active ndi_devi_enter to gather the
1915 1914 * minor data, and we will take devi_lock to gather properties as
1916 1915 * needed off di_getprop.
1917 1916 */
1918 1917 if (!(DINFOMINOR & st->command)) {
1919 1918 goto path;
1920 1919 }
1921 1920
1922 1921 ASSERT(DEVI_BUSY_OWNED(node));
1923 1922 if (node->devi_minor) { /* minor data */
1924 1923 me->minor_data = off;
1925 1924 off = di_getmdata(node->devi_minor, &me->minor_data,
1926 1925 me->self, st);
1927 1926 }
1928 1927
1929 1928 path:
1930 1929 if (!(DINFOPATH & st->command)) {
1931 1930 goto property;
1932 1931 }
1933 1932
1934 1933 if (MDI_VHCI(node)) {
1935 1934 me->multipath_component = MULTIPATH_COMPONENT_VHCI;
1936 1935 }
1937 1936
1938 1937 if (MDI_CLIENT(node)) {
1939 1938 me->multipath_component = MULTIPATH_COMPONENT_CLIENT;
1940 1939 me->multipath_client = off;
1941 1940 off = di_getpath_data((dev_info_t *)node, &me->multipath_client,
1942 1941 me->self, st, 1);
1943 1942 dcmn_err((CE_WARN, "me->multipath_client = %x for node %p "
1944 1943 "component type = %d. off=%d",
1945 1944 me->multipath_client,
1946 1945 (void *)node, node->devi_mdi_component, off));
1947 1946 }
1948 1947
1949 1948 if (MDI_PHCI(node)) {
1950 1949 me->multipath_component = MULTIPATH_COMPONENT_PHCI;
1951 1950 me->multipath_phci = off;
1952 1951 off = di_getpath_data((dev_info_t *)node, &me->multipath_phci,
1953 1952 me->self, st, 0);
1954 1953 dcmn_err((CE_WARN, "me->multipath_phci = %x for node %p "
1955 1954 "component type = %d. off=%d",
1956 1955 me->multipath_phci,
1957 1956 (void *)node, node->devi_mdi_component, off));
1958 1957 }
1959 1958
1960 1959 property:
1961 1960 if (!(DINFOPROP & st->command)) {
1962 1961 goto hotplug_data;
1963 1962 }
1964 1963
1965 1964 if (node->devi_drv_prop_ptr) { /* driver property list */
1966 1965 me->drv_prop = off;
1967 1966 off = di_getprop(DI_PROP_DRV_LIST, &node->devi_drv_prop_ptr,
1968 1967 &me->drv_prop, st, node);
1969 1968 }
1970 1969
1971 1970 if (node->devi_sys_prop_ptr) { /* system property list */
1972 1971 me->sys_prop = off;
1973 1972 off = di_getprop(DI_PROP_SYS_LIST, &node->devi_sys_prop_ptr,
1974 1973 &me->sys_prop, st, node);
1975 1974 }
1976 1975
1977 1976 if (node->devi_hw_prop_ptr) { /* hardware property list */
1978 1977 me->hw_prop = off;
1979 1978 off = di_getprop(DI_PROP_HW_LIST, &node->devi_hw_prop_ptr,
1980 1979 &me->hw_prop, st, node);
1981 1980 }
1982 1981
1983 1982 if (node->devi_global_prop_list == NULL) {
1984 1983 me->glob_prop = (di_off_t)-1; /* not global property */
1985 1984 } else {
1986 1985 /*
1987 1986 * Make copy of global property list if this devinfo refers
1988 1987 * global properties different from what's on the devnames
1989 1988 * array. It can happen if there has been a forced
1990 1989 * driver.conf update. See mod_drv(1M).
1991 1990 */
1992 1991 ASSERT(me->drv_major != -1);
1993 1992 if (node->devi_global_prop_list !=
1994 1993 devnamesp[me->drv_major].dn_global_prop_ptr) {
1995 1994 me->glob_prop = off;
1996 1995 off = di_getprop(DI_PROP_GLB_LIST,
1997 1996 &node->devi_global_prop_list->prop_list,
1998 1997 &me->glob_prop, st, node);
1999 1998 }
2000 1999 }
2001 2000
2002 2001 hotplug_data:
2003 2002 if (!(DINFOHP & st->command)) {
2004 2003 goto priv_data;
2005 2004 }
2006 2005
2007 2006 if (node->devi_hp_hdlp) { /* hotplug data */
2008 2007 me->hp_data = off;
2009 2008 off = di_gethpdata(node->devi_hp_hdlp, &me->hp_data, st);
2010 2009 }
2011 2010
2012 2011 priv_data:
2013 2012 if (!(DINFOPRIVDATA & st->command)) {
2014 2013 goto pm_info;
2015 2014 }
2016 2015
2017 2016 if (ddi_get_parent_data((dev_info_t *)node) != NULL) {
2018 2017 me->parent_data = off;
2019 2018 off = di_getppdata(node, &me->parent_data, st);
2020 2019 }
2021 2020
2022 2021 if (ddi_get_driver_private((dev_info_t *)node) != NULL) {
2023 2022 me->driver_data = off;
2024 2023 off = di_getdpdata(node, &me->driver_data, st);
2025 2024 }
2026 2025
2027 2026 pm_info: /* NOT implemented */
2028 2027
2029 2028 subtree:
2030 2029 /* keep the stack aligned */
2031 2030 off = DI_ALIGN(off);
2032 2031
2033 2032 if (!(DINFOSUBTREE & st->command)) {
2034 2033 POP_STACK(dsp);
2035 2034 return (off);
2036 2035 }
2037 2036
2038 2037 child:
2039 2038 /*
2040 2039 * If there is a visible child--push child onto stack.
2041 2040 * Hold the parent (me) busy while doing so.
2042 2041 */
2043 2042 if ((n = node->devi_child) != NULL) {
2044 2043 /* skip hidden nodes */
2045 2044 while (n && ndi_dev_is_hidden_node((dev_info_t *)n))
2046 2045 n = n->devi_sibling;
2047 2046 if (n) {
2048 2047 me->child = off;
2049 2048 PUSH_STACK(dsp, n, &me->child);
2050 2049 return (me->child);
2051 2050 }
2052 2051 }
2053 2052
2054 2053 sibling:
2055 2054 /*
2056 2055 * Done with any child nodes, unroll the stack till a visible
2057 2056 * sibling of a parent node is found or root node is reached.
2058 2057 */
2059 2058 POP_STACK(dsp);
2060 2059 while (!EMPTY_STACK(dsp)) {
2061 2060 if ((n = node->devi_sibling) != NULL) {
2062 2061 /* skip hidden nodes */
2063 2062 while (n && ndi_dev_is_hidden_node((dev_info_t *)n))
2064 2063 n = n->devi_sibling;
2065 2064 if (n) {
2066 2065 me->sibling = DI_ALIGN(off);
2067 2066 PUSH_STACK(dsp, n, &me->sibling);
2068 2067 return (me->sibling);
2069 2068 }
2070 2069 }
2071 2070 node = TOP_NODE(dsp);
2072 2071 me = DI_NODE(di_mem_addr(st, *(TOP_OFFSET(dsp))));
2073 2072 POP_STACK(dsp);
2074 2073 }
2075 2074
2076 2075 /*
2077 2076 * DONE with all nodes
2078 2077 */
2079 2078 return (off);
2080 2079 }
2081 2080
2082 2081 static i_lnode_t *
2083 2082 i_lnode_alloc(int modid)
2084 2083 {
2085 2084 i_lnode_t *i_lnode;
2086 2085
2087 2086 i_lnode = kmem_zalloc(sizeof (i_lnode_t), KM_SLEEP);
2088 2087
2089 2088 ASSERT(modid != -1);
2090 2089 i_lnode->modid = modid;
2091 2090
2092 2091 return (i_lnode);
2093 2092 }
2094 2093
2095 2094 static void
2096 2095 i_lnode_free(i_lnode_t *i_lnode)
2097 2096 {
2098 2097 kmem_free(i_lnode, sizeof (i_lnode_t));
2099 2098 }
2100 2099
2101 2100 static void
2102 2101 i_lnode_check_free(i_lnode_t *i_lnode)
2103 2102 {
2104 2103 /* This lnode and its dip must have been snapshotted */
2105 2104 ASSERT(i_lnode->self > 0);
2106 2105 ASSERT(i_lnode->di_node->self > 0);
2107 2106
2108 2107 /* at least 1 link (in or out) must exist for this lnode */
2109 2108 ASSERT(i_lnode->link_in || i_lnode->link_out);
2110 2109
2111 2110 i_lnode_free(i_lnode);
2112 2111 }
2113 2112
2114 2113 static i_link_t *
2115 2114 i_link_alloc(int spec_type)
2116 2115 {
2117 2116 i_link_t *i_link;
2118 2117
2119 2118 i_link = kmem_zalloc(sizeof (i_link_t), KM_SLEEP);
2120 2119 i_link->spec_type = spec_type;
2121 2120
2122 2121 return (i_link);
2123 2122 }
2124 2123
2125 2124 static void
2126 2125 i_link_check_free(i_link_t *i_link)
2127 2126 {
2128 2127 /* This link must have been snapshotted */
2129 2128 ASSERT(i_link->self > 0);
2130 2129
2131 2130 /* Both endpoint lnodes must exist for this link */
2132 2131 ASSERT(i_link->src_lnode);
2133 2132 ASSERT(i_link->tgt_lnode);
2134 2133
2135 2134 kmem_free(i_link, sizeof (i_link_t));
2136 2135 }
2137 2136
2138 2137 /*ARGSUSED*/
2139 2138 static uint_t
2140 2139 i_lnode_hashfunc(void *arg, mod_hash_key_t key)
2141 2140 {
2142 2141 i_lnode_t *i_lnode = (i_lnode_t *)key;
2143 2142 struct di_node *ptr;
2144 2143 dev_t dev;
2145 2144
2146 2145 dev = i_lnode->devt;
2147 2146 if (dev != DDI_DEV_T_NONE)
2148 2147 return (i_lnode->modid + getminor(dev) + getmajor(dev));
2149 2148
2150 2149 ptr = i_lnode->di_node;
2151 2150 ASSERT(ptr->self > 0);
2152 2151 if (ptr) {
2153 2152 uintptr_t k = (uintptr_t)ptr;
2154 2153 k >>= (int)highbit(sizeof (struct di_node));
2155 2154 return ((uint_t)k);
2156 2155 }
2157 2156
2158 2157 return (i_lnode->modid);
2159 2158 }
2160 2159
2161 2160 static int
2162 2161 i_lnode_cmp(void *arg1, void *arg2)
2163 2162 {
2164 2163 i_lnode_t *i_lnode1 = (i_lnode_t *)arg1;
2165 2164 i_lnode_t *i_lnode2 = (i_lnode_t *)arg2;
2166 2165
2167 2166 if (i_lnode1->modid != i_lnode2->modid) {
2168 2167 return ((i_lnode1->modid < i_lnode2->modid) ? -1 : 1);
2169 2168 }
2170 2169
2171 2170 if (i_lnode1->di_node != i_lnode2->di_node)
2172 2171 return ((i_lnode1->di_node < i_lnode2->di_node) ? -1 : 1);
2173 2172
2174 2173 if (i_lnode1->devt != i_lnode2->devt)
2175 2174 return ((i_lnode1->devt < i_lnode2->devt) ? -1 : 1);
2176 2175
2177 2176 return (0);
2178 2177 }
2179 2178
2180 2179 /*
2181 2180 * An lnode represents a {dip, dev_t} tuple. A link represents a
2182 2181 * {src_lnode, tgt_lnode, spec_type} tuple.
2183 2182 * The following callback assumes that LDI framework ref-counts the
2184 2183 * src_dip and tgt_dip while invoking this callback.
2185 2184 */
2186 2185 static int
2187 2186 di_ldi_callback(const ldi_usage_t *ldi_usage, void *arg)
2188 2187 {
2189 2188 struct di_state *st = (struct di_state *)arg;
2190 2189 i_lnode_t *src_lnode, *tgt_lnode, *i_lnode;
2191 2190 i_link_t **i_link_next, *i_link;
2192 2191 di_off_t soff, toff;
2193 2192 mod_hash_val_t nodep = NULL;
2194 2193 int res;
2195 2194
2196 2195 /*
2197 2196 * if the source or target of this device usage information doesn't
2198 2197 * correspond to a device node then we don't report it via
2199 2198 * libdevinfo so return.
2200 2199 */
2201 2200 if ((ldi_usage->src_dip == NULL) || (ldi_usage->tgt_dip == NULL))
2202 2201 return (LDI_USAGE_CONTINUE);
2203 2202
2204 2203 ASSERT(e_ddi_devi_holdcnt(ldi_usage->src_dip));
2205 2204 ASSERT(e_ddi_devi_holdcnt(ldi_usage->tgt_dip));
2206 2205
2207 2206 /*
2208 2207 * Skip the ldi_usage if either src or tgt dip is not in the
2209 2208 * snapshot. This saves us from pruning bad lnodes/links later.
2210 2209 */
2211 2210 if (di_dip_find(st, ldi_usage->src_dip, &soff) != 0)
2212 2211 return (LDI_USAGE_CONTINUE);
2213 2212 if (di_dip_find(st, ldi_usage->tgt_dip, &toff) != 0)
2214 2213 return (LDI_USAGE_CONTINUE);
2215 2214
2216 2215 ASSERT(soff > 0);
2217 2216 ASSERT(toff > 0);
2218 2217
2219 2218 /*
2220 2219 * allocate an i_lnode and add it to the lnode hash
2221 2220 * if it is not already present. For this particular
2222 2221 * link the lnode is a source, but it may
2223 2222 * participate as tgt or src in any number of layered
2224 2223 * operations - so it may already be in the hash.
2225 2224 */
2226 2225 i_lnode = i_lnode_alloc(ldi_usage->src_modid);
2227 2226 i_lnode->di_node = DI_NODE(di_mem_addr(st, soff));
2228 2227 i_lnode->devt = ldi_usage->src_devt;
2229 2228
2230 2229 res = mod_hash_find(st->lnode_hash, i_lnode, &nodep);
2231 2230 if (res == MH_ERR_NOTFOUND) {
2232 2231 /*
2233 2232 * new i_lnode
2234 2233 * add it to the hash and increment the lnode count
2235 2234 */
2236 2235 res = mod_hash_insert(st->lnode_hash, i_lnode, i_lnode);
2237 2236 ASSERT(res == 0);
2238 2237 st->lnode_count++;
2239 2238 src_lnode = i_lnode;
2240 2239 } else {
2241 2240 /* this i_lnode already exists in the lnode_hash */
2242 2241 i_lnode_free(i_lnode);
2243 2242 src_lnode = (i_lnode_t *)nodep;
2244 2243 }
2245 2244
2246 2245 /*
2247 2246 * allocate a tgt i_lnode and add it to the lnode hash
2248 2247 */
2249 2248 i_lnode = i_lnode_alloc(ldi_usage->tgt_modid);
2250 2249 i_lnode->di_node = DI_NODE(di_mem_addr(st, toff));
2251 2250 i_lnode->devt = ldi_usage->tgt_devt;
2252 2251
2253 2252 res = mod_hash_find(st->lnode_hash, i_lnode, &nodep);
2254 2253 if (res == MH_ERR_NOTFOUND) {
2255 2254 /*
2256 2255 * new i_lnode
2257 2256 * add it to the hash and increment the lnode count
2258 2257 */
2259 2258 res = mod_hash_insert(st->lnode_hash, i_lnode, i_lnode);
2260 2259 ASSERT(res == 0);
2261 2260 st->lnode_count++;
2262 2261 tgt_lnode = i_lnode;
2263 2262 } else {
2264 2263 /* this i_lnode already exists in the lnode_hash */
2265 2264 i_lnode_free(i_lnode);
2266 2265 tgt_lnode = (i_lnode_t *)nodep;
2267 2266 }
2268 2267
2269 2268 /*
2270 2269 * allocate a i_link
2271 2270 */
2272 2271 i_link = i_link_alloc(ldi_usage->tgt_spec_type);
2273 2272 i_link->src_lnode = src_lnode;
2274 2273 i_link->tgt_lnode = tgt_lnode;
2275 2274
2276 2275 /*
2277 2276 * add this link onto the src i_lnodes outbound i_link list
2278 2277 */
2279 2278 i_link_next = &(src_lnode->link_out);
2280 2279 while (*i_link_next != NULL) {
2281 2280 if ((i_lnode_cmp(tgt_lnode, (*i_link_next)->tgt_lnode) == 0) &&
2282 2281 (i_link->spec_type == (*i_link_next)->spec_type)) {
2283 2282 /* this link already exists */
2284 2283 kmem_free(i_link, sizeof (i_link_t));
2285 2284 return (LDI_USAGE_CONTINUE);
2286 2285 }
2287 2286 i_link_next = &((*i_link_next)->src_link_next);
2288 2287 }
2289 2288 *i_link_next = i_link;
2290 2289
2291 2290 /*
2292 2291 * add this link onto the tgt i_lnodes inbound i_link list
2293 2292 */
2294 2293 i_link_next = &(tgt_lnode->link_in);
2295 2294 while (*i_link_next != NULL) {
2296 2295 ASSERT(i_lnode_cmp(src_lnode, (*i_link_next)->src_lnode) != 0);
2297 2296 i_link_next = &((*i_link_next)->tgt_link_next);
2298 2297 }
2299 2298 *i_link_next = i_link;
2300 2299
2301 2300 /*
2302 2301 * add this i_link to the link hash
2303 2302 */
2304 2303 res = mod_hash_insert(st->link_hash, i_link, i_link);
2305 2304 ASSERT(res == 0);
2306 2305 st->link_count++;
2307 2306
2308 2307 return (LDI_USAGE_CONTINUE);
2309 2308 }
2310 2309
2311 2310 struct i_layer_data {
2312 2311 struct di_state *st;
2313 2312 int lnode_count;
2314 2313 int link_count;
2315 2314 di_off_t lnode_off;
2316 2315 di_off_t link_off;
2317 2316 };
2318 2317
2319 2318 /*ARGSUSED*/
2320 2319 static uint_t
2321 2320 i_link_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
2322 2321 {
2323 2322 i_link_t *i_link = (i_link_t *)key;
2324 2323 struct i_layer_data *data = arg;
2325 2324 struct di_link *me;
2326 2325 struct di_lnode *melnode;
2327 2326 struct di_node *medinode;
2328 2327
2329 2328 ASSERT(i_link->self == 0);
2330 2329
2331 2330 i_link->self = data->link_off +
2332 2331 (data->link_count * sizeof (struct di_link));
2333 2332 data->link_count++;
2334 2333
2335 2334 ASSERT(data->link_off > 0 && data->link_count > 0);
2336 2335 ASSERT(data->lnode_count == data->st->lnode_count); /* lnodes done */
2337 2336 ASSERT(data->link_count <= data->st->link_count);
2338 2337
2339 2338 /* fill in fields for the di_link snapshot */
2340 2339 me = DI_LINK(di_mem_addr(data->st, i_link->self));
2341 2340 me->self = i_link->self;
2342 2341 me->spec_type = i_link->spec_type;
2343 2342
2344 2343 /*
2345 2344 * The src_lnode and tgt_lnode i_lnode_t for this i_link_t
2346 2345 * are created during the LDI table walk. Since we are
2347 2346 * walking the link hash, the lnode hash has already been
2348 2347 * walked and the lnodes have been snapshotted. Save lnode
2349 2348 * offsets.
2350 2349 */
2351 2350 me->src_lnode = i_link->src_lnode->self;
2352 2351 me->tgt_lnode = i_link->tgt_lnode->self;
2353 2352
2354 2353 /*
2355 2354 * Save this link's offset in the src_lnode snapshot's link_out
2356 2355 * field
2357 2356 */
2358 2357 melnode = DI_LNODE(di_mem_addr(data->st, me->src_lnode));
2359 2358 me->src_link_next = melnode->link_out;
2360 2359 melnode->link_out = me->self;
2361 2360
2362 2361 /*
2363 2362 * Put this link on the tgt_lnode's link_in field
2364 2363 */
2365 2364 melnode = DI_LNODE(di_mem_addr(data->st, me->tgt_lnode));
2366 2365 me->tgt_link_next = melnode->link_in;
2367 2366 melnode->link_in = me->self;
2368 2367
2369 2368 /*
2370 2369 * An i_lnode_t is only created if the corresponding dip exists
2371 2370 * in the snapshot. A pointer to the di_node is saved in the
2372 2371 * i_lnode_t when it is allocated. For this link, get the di_node
2373 2372 * for the source lnode. Then put the link on the di_node's list
2374 2373 * of src links
2375 2374 */
2376 2375 medinode = i_link->src_lnode->di_node;
2377 2376 me->src_node_next = medinode->src_links;
2378 2377 medinode->src_links = me->self;
2379 2378
2380 2379 /*
2381 2380 * Put this link on the tgt_links list of the target
2382 2381 * dip.
2383 2382 */
2384 2383 medinode = i_link->tgt_lnode->di_node;
2385 2384 me->tgt_node_next = medinode->tgt_links;
2386 2385 medinode->tgt_links = me->self;
2387 2386
2388 2387 return (MH_WALK_CONTINUE);
2389 2388 }
2390 2389
2391 2390 /*ARGSUSED*/
2392 2391 static uint_t
2393 2392 i_lnode_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
2394 2393 {
2395 2394 i_lnode_t *i_lnode = (i_lnode_t *)key;
2396 2395 struct i_layer_data *data = arg;
2397 2396 struct di_lnode *me;
2398 2397 struct di_node *medinode;
2399 2398
2400 2399 ASSERT(i_lnode->self == 0);
2401 2400
2402 2401 i_lnode->self = data->lnode_off +
2403 2402 (data->lnode_count * sizeof (struct di_lnode));
2404 2403 data->lnode_count++;
2405 2404
2406 2405 ASSERT(data->lnode_off > 0 && data->lnode_count > 0);
2407 2406 ASSERT(data->link_count == 0); /* links not done yet */
2408 2407 ASSERT(data->lnode_count <= data->st->lnode_count);
2409 2408
2410 2409 /* fill in fields for the di_lnode snapshot */
2411 2410 me = DI_LNODE(di_mem_addr(data->st, i_lnode->self));
2412 2411 me->self = i_lnode->self;
2413 2412
2414 2413 if (i_lnode->devt == DDI_DEV_T_NONE) {
2415 2414 me->dev_major = DDI_MAJOR_T_NONE;
2416 2415 me->dev_minor = DDI_MAJOR_T_NONE;
2417 2416 } else {
2418 2417 me->dev_major = getmajor(i_lnode->devt);
2419 2418 me->dev_minor = getminor(i_lnode->devt);
2420 2419 }
2421 2420
2422 2421 /*
2423 2422 * The dip corresponding to this lnode must exist in
2424 2423 * the snapshot or we wouldn't have created the i_lnode_t
2425 2424 * during LDI walk. Save the offset of the dip.
2426 2425 */
2427 2426 ASSERT(i_lnode->di_node && i_lnode->di_node->self > 0);
2428 2427 me->node = i_lnode->di_node->self;
2429 2428
2430 2429 /*
2431 2430 * There must be at least one link in or out of this lnode
2432 2431 * or we wouldn't have created it. These fields will be set
2433 2432 * during the link hash walk.
2434 2433 */
2435 2434 ASSERT((i_lnode->link_in != NULL) || (i_lnode->link_out != NULL));
2436 2435
2437 2436 /*
2438 2437 * set the offset of the devinfo node associated with this
2439 2438 * lnode. Also update the node_next next pointer. this pointer
2440 2439 * is set if there are multiple lnodes associated with the same
2441 2440 * devinfo node. (could occure when multiple minor nodes
2442 2441 * are open for one device, etc.)
2443 2442 */
2444 2443 medinode = i_lnode->di_node;
2445 2444 me->node_next = medinode->lnodes;
2446 2445 medinode->lnodes = me->self;
2447 2446
2448 2447 return (MH_WALK_CONTINUE);
2449 2448 }
2450 2449
2451 2450 static di_off_t
2452 2451 di_getlink_data(di_off_t off, struct di_state *st)
2453 2452 {
2454 2453 struct i_layer_data data = {0};
2455 2454 size_t size;
2456 2455
2457 2456 dcmn_err2((CE_CONT, "di_copylyr: off = %x\n", off));
2458 2457
2459 2458 st->lnode_hash = mod_hash_create_extended("di_lnode_hash", 32,
2460 2459 mod_hash_null_keydtor, (void (*)(mod_hash_val_t))i_lnode_check_free,
2461 2460 i_lnode_hashfunc, NULL, i_lnode_cmp, KM_SLEEP);
2462 2461
2463 2462 st->link_hash = mod_hash_create_ptrhash("di_link_hash", 32,
2464 2463 (void (*)(mod_hash_val_t))i_link_check_free, sizeof (i_link_t));
2465 2464
2466 2465 /* get driver layering information */
2467 2466 (void) ldi_usage_walker(st, di_ldi_callback);
2468 2467
2469 2468 /* check if there is any link data to include in the snapshot */
2470 2469 if (st->lnode_count == 0) {
2471 2470 ASSERT(st->link_count == 0);
2472 2471 goto out;
2473 2472 }
2474 2473
2475 2474 ASSERT(st->link_count != 0);
2476 2475
2477 2476 /* get a pointer to snapshot memory for all the di_lnodes */
2478 2477 size = sizeof (struct di_lnode) * st->lnode_count;
2479 2478 data.lnode_off = off = di_checkmem(st, off, size);
2480 2479 off += size;
2481 2480
2482 2481 /* get a pointer to snapshot memory for all the di_links */
2483 2482 size = sizeof (struct di_link) * st->link_count;
2484 2483 data.link_off = off = di_checkmem(st, off, size);
2485 2484 off += size;
2486 2485
2487 2486 data.lnode_count = data.link_count = 0;
2488 2487 data.st = st;
2489 2488
2490 2489 /*
2491 2490 * We have lnodes and links that will go into the
2492 2491 * snapshot, so let's walk the respective hashes
2493 2492 * and snapshot them. The various linkages are
2494 2493 * also set up during the walk.
2495 2494 */
2496 2495 mod_hash_walk(st->lnode_hash, i_lnode_walker, (void *)&data);
2497 2496 ASSERT(data.lnode_count == st->lnode_count);
2498 2497
2499 2498 mod_hash_walk(st->link_hash, i_link_walker, (void *)&data);
2500 2499 ASSERT(data.link_count == st->link_count);
2501 2500
2502 2501 out:
2503 2502 /* free up the i_lnodes and i_links used to create the snapshot */
2504 2503 mod_hash_destroy_hash(st->lnode_hash);
2505 2504 mod_hash_destroy_hash(st->link_hash);
2506 2505 st->lnode_count = 0;
2507 2506 st->link_count = 0;
2508 2507
2509 2508 return (off);
2510 2509 }
2511 2510
2512 2511
2513 2512 /*
2514 2513 * Copy all minor data nodes attached to a devinfo node into the snapshot.
2515 2514 * It is called from di_copynode with active ndi_devi_enter to protect
2516 2515 * the list of minor nodes.
2517 2516 */
2518 2517 static di_off_t
2519 2518 di_getmdata(struct ddi_minor_data *mnode, di_off_t *off_p, di_off_t node,
2520 2519 struct di_state *st)
2521 2520 {
2522 2521 di_off_t off;
2523 2522 struct di_minor *me;
2524 2523 size_t size;
2525 2524
2526 2525 dcmn_err2((CE_CONT, "di_getmdata:\n"));
2527 2526
2528 2527 /*
2529 2528 * check memory first
2530 2529 */
2531 2530 off = di_checkmem(st, *off_p, sizeof (struct di_minor));
2532 2531 *off_p = off;
2533 2532
2534 2533 do {
2535 2534 me = DI_MINOR(di_mem_addr(st, off));
2536 2535 me->self = off;
2537 2536 me->type = mnode->type;
2538 2537 me->node = node;
2539 2538 me->user_private_data = NULL;
2540 2539
2541 2540 off += sizeof (struct di_minor);
2542 2541
2543 2542 /*
2544 2543 * Split dev_t to major/minor, so it works for
2545 2544 * both ILP32 and LP64 model
2546 2545 */
2547 2546 me->dev_major = getmajor(mnode->ddm_dev);
2548 2547 me->dev_minor = getminor(mnode->ddm_dev);
2549 2548 me->spec_type = mnode->ddm_spec_type;
2550 2549
2551 2550 if (mnode->ddm_name) {
2552 2551 size = strlen(mnode->ddm_name) + 1;
2553 2552 me->name = off = di_checkmem(st, off, size);
2554 2553 (void) strcpy(di_mem_addr(st, off), mnode->ddm_name);
2555 2554 off += size;
2556 2555 }
2557 2556
2558 2557 if (mnode->ddm_node_type) {
2559 2558 size = strlen(mnode->ddm_node_type) + 1;
2560 2559 me->node_type = off = di_checkmem(st, off, size);
2561 2560 (void) strcpy(di_mem_addr(st, off),
2562 2561 mnode->ddm_node_type);
2563 2562 off += size;
2564 2563 }
2565 2564
2566 2565 off = di_checkmem(st, off, sizeof (struct di_minor));
2567 2566 me->next = off;
2568 2567 mnode = mnode->next;
2569 2568 } while (mnode);
2570 2569
2571 2570 me->next = 0;
2572 2571
2573 2572 return (off);
2574 2573 }
2575 2574
2576 2575 /*
2577 2576 * di_register_dip(), di_find_dip(): The dip must be protected
2578 2577 * from deallocation when using these routines - this can either
2579 2578 * be a reference count, a busy hold or a per-driver lock.
2580 2579 */
2581 2580
2582 2581 static void
2583 2582 di_register_dip(struct di_state *st, dev_info_t *dip, di_off_t off)
2584 2583 {
2585 2584 struct dev_info *node = DEVI(dip);
2586 2585 struct di_key *key = kmem_zalloc(sizeof (*key), KM_SLEEP);
2587 2586 struct di_dkey *dk;
2588 2587
2589 2588 ASSERT(dip);
2590 2589 ASSERT(off > 0);
2591 2590
2592 2591 key->k_type = DI_DKEY;
2593 2592 dk = &(key->k_u.dkey);
2594 2593
2595 2594 dk->dk_dip = dip;
2596 2595 dk->dk_major = node->devi_major;
2597 2596 dk->dk_inst = node->devi_instance;
2598 2597 dk->dk_nodeid = node->devi_nodeid;
2599 2598
2600 2599 if (mod_hash_insert(st->reg_dip_hash, (mod_hash_key_t)key,
2601 2600 (mod_hash_val_t)(uintptr_t)off) != 0) {
2602 2601 panic(
2603 2602 "duplicate devinfo (%p) registered during device "
2604 2603 "tree walk", (void *)dip);
2605 2604 }
2606 2605 }
2607 2606
2608 2607
2609 2608 static int
2610 2609 di_dip_find(struct di_state *st, dev_info_t *dip, di_off_t *off_p)
2611 2610 {
2612 2611 /*
2613 2612 * uintptr_t must be used because it matches the size of void *;
2614 2613 * mod_hash expects clients to place results into pointer-size
2615 2614 * containers; since di_off_t is always a 32-bit offset, alignment
2616 2615 * would otherwise be broken on 64-bit kernels.
2617 2616 */
2618 2617 uintptr_t offset;
2619 2618 struct di_key key = {0};
2620 2619 struct di_dkey *dk;
2621 2620
2622 2621 ASSERT(st->reg_dip_hash);
2623 2622 ASSERT(dip);
2624 2623 ASSERT(off_p);
2625 2624
2626 2625
2627 2626 key.k_type = DI_DKEY;
2628 2627 dk = &(key.k_u.dkey);
2629 2628
2630 2629 dk->dk_dip = dip;
2631 2630 dk->dk_major = DEVI(dip)->devi_major;
2632 2631 dk->dk_inst = DEVI(dip)->devi_instance;
2633 2632 dk->dk_nodeid = DEVI(dip)->devi_nodeid;
2634 2633
2635 2634 if (mod_hash_find(st->reg_dip_hash, (mod_hash_key_t)&key,
2636 2635 (mod_hash_val_t *)&offset) == 0) {
2637 2636 *off_p = (di_off_t)offset;
2638 2637 return (0);
2639 2638 } else {
2640 2639 return (-1);
2641 2640 }
2642 2641 }
2643 2642
2644 2643 /*
2645 2644 * di_register_pip(), di_find_pip(): The pip must be protected from deallocation
2646 2645 * when using these routines. The caller must do this by protecting the
2647 2646 * client(or phci)<->pip linkage while traversing the list and then holding the
2648 2647 * pip when it is found in the list.
2649 2648 */
2650 2649
2651 2650 static void
2652 2651 di_register_pip(struct di_state *st, mdi_pathinfo_t *pip, di_off_t off)
2653 2652 {
2654 2653 struct di_key *key = kmem_zalloc(sizeof (*key), KM_SLEEP);
2655 2654 char *path_addr;
2656 2655 struct di_pkey *pk;
2657 2656
2658 2657 ASSERT(pip);
2659 2658 ASSERT(off > 0);
2660 2659
2661 2660 key->k_type = DI_PKEY;
2662 2661 pk = &(key->k_u.pkey);
2663 2662
2664 2663 pk->pk_pip = pip;
2665 2664 path_addr = mdi_pi_get_addr(pip);
2666 2665 if (path_addr)
2667 2666 pk->pk_path_addr = i_ddi_strdup(path_addr, KM_SLEEP);
2668 2667 pk->pk_client = mdi_pi_get_client(pip);
2669 2668 pk->pk_phci = mdi_pi_get_phci(pip);
2670 2669
2671 2670 if (mod_hash_insert(st->reg_pip_hash, (mod_hash_key_t)key,
2672 2671 (mod_hash_val_t)(uintptr_t)off) != 0) {
2673 2672 panic(
2674 2673 "duplicate pathinfo (%p) registered during device "
2675 2674 "tree walk", (void *)pip);
2676 2675 }
2677 2676 }
2678 2677
2679 2678 /*
2680 2679 * As with di_register_pip, the caller must hold or lock the pip
2681 2680 */
2682 2681 static int
2683 2682 di_pip_find(struct di_state *st, mdi_pathinfo_t *pip, di_off_t *off_p)
2684 2683 {
2685 2684 /*
2686 2685 * uintptr_t must be used because it matches the size of void *;
2687 2686 * mod_hash expects clients to place results into pointer-size
2688 2687 * containers; since di_off_t is always a 32-bit offset, alignment
2689 2688 * would otherwise be broken on 64-bit kernels.
2690 2689 */
2691 2690 uintptr_t offset;
2692 2691 struct di_key key = {0};
2693 2692 struct di_pkey *pk;
2694 2693
2695 2694 ASSERT(st->reg_pip_hash);
2696 2695 ASSERT(off_p);
2697 2696
2698 2697 if (pip == NULL) {
2699 2698 *off_p = 0;
2700 2699 return (0);
2701 2700 }
2702 2701
2703 2702 key.k_type = DI_PKEY;
2704 2703 pk = &(key.k_u.pkey);
2705 2704
2706 2705 pk->pk_pip = pip;
2707 2706 pk->pk_path_addr = mdi_pi_get_addr(pip);
2708 2707 pk->pk_client = mdi_pi_get_client(pip);
2709 2708 pk->pk_phci = mdi_pi_get_phci(pip);
2710 2709
2711 2710 if (mod_hash_find(st->reg_pip_hash, (mod_hash_key_t)&key,
2712 2711 (mod_hash_val_t *)&offset) == 0) {
2713 2712 *off_p = (di_off_t)offset;
2714 2713 return (0);
2715 2714 } else {
2716 2715 return (-1);
2717 2716 }
2718 2717 }
2719 2718
2720 2719 static di_path_state_t
2721 2720 path_state_convert(mdi_pathinfo_state_t st)
2722 2721 {
2723 2722 switch (st) {
2724 2723 case MDI_PATHINFO_STATE_ONLINE:
2725 2724 return (DI_PATH_STATE_ONLINE);
2726 2725 case MDI_PATHINFO_STATE_STANDBY:
2727 2726 return (DI_PATH_STATE_STANDBY);
2728 2727 case MDI_PATHINFO_STATE_OFFLINE:
2729 2728 return (DI_PATH_STATE_OFFLINE);
2730 2729 case MDI_PATHINFO_STATE_FAULT:
2731 2730 return (DI_PATH_STATE_FAULT);
2732 2731 default:
2733 2732 return (DI_PATH_STATE_UNKNOWN);
2734 2733 }
2735 2734 }
2736 2735
2737 2736 static uint_t
2738 2737 path_flags_convert(uint_t pi_path_flags)
2739 2738 {
2740 2739 uint_t di_path_flags = 0;
2741 2740
2742 2741 /* MDI_PATHINFO_FLAGS_HIDDEN nodes not in snapshot */
2743 2742
2744 2743 if (pi_path_flags & MDI_PATHINFO_FLAGS_DEVICE_REMOVED)
2745 2744 di_path_flags |= DI_PATH_FLAGS_DEVICE_REMOVED;
2746 2745
2747 2746 return (di_path_flags);
2748 2747 }
2749 2748
2750 2749
2751 2750 static di_off_t
2752 2751 di_path_getprop(mdi_pathinfo_t *pip, di_off_t *off_p,
2753 2752 struct di_state *st)
2754 2753 {
2755 2754 nvpair_t *prop = NULL;
2756 2755 struct di_path_prop *me;
2757 2756 int off;
2758 2757 size_t size;
2759 2758 char *str;
2760 2759 uchar_t *buf;
2761 2760 uint_t nelems;
2762 2761
2763 2762 off = *off_p;
2764 2763 if (mdi_pi_get_next_prop(pip, NULL) == NULL) {
2765 2764 *off_p = 0;
2766 2765 return (off);
2767 2766 }
2768 2767
2769 2768 off = di_checkmem(st, off, sizeof (struct di_path_prop));
2770 2769 *off_p = off;
2771 2770
2772 2771 while (prop = mdi_pi_get_next_prop(pip, prop)) {
2773 2772 me = DI_PATHPROP(di_mem_addr(st, off));
2774 2773 me->self = off;
2775 2774 off += sizeof (struct di_path_prop);
2776 2775
2777 2776 /*
2778 2777 * property name
2779 2778 */
2780 2779 size = strlen(nvpair_name(prop)) + 1;
2781 2780 me->prop_name = off = di_checkmem(st, off, size);
2782 2781 (void) strcpy(di_mem_addr(st, off), nvpair_name(prop));
2783 2782 off += size;
2784 2783
2785 2784 switch (nvpair_type(prop)) {
2786 2785 case DATA_TYPE_BYTE:
2787 2786 case DATA_TYPE_INT16:
2788 2787 case DATA_TYPE_UINT16:
2789 2788 case DATA_TYPE_INT32:
2790 2789 case DATA_TYPE_UINT32:
2791 2790 me->prop_type = DDI_PROP_TYPE_INT;
2792 2791 size = sizeof (int32_t);
2793 2792 off = di_checkmem(st, off, size);
2794 2793 (void) nvpair_value_int32(prop,
2795 2794 (int32_t *)di_mem_addr(st, off));
2796 2795 break;
2797 2796
2798 2797 case DATA_TYPE_INT64:
2799 2798 case DATA_TYPE_UINT64:
2800 2799 me->prop_type = DDI_PROP_TYPE_INT64;
2801 2800 size = sizeof (int64_t);
2802 2801 off = di_checkmem(st, off, size);
2803 2802 (void) nvpair_value_int64(prop,
2804 2803 (int64_t *)di_mem_addr(st, off));
2805 2804 break;
2806 2805
2807 2806 case DATA_TYPE_STRING:
2808 2807 me->prop_type = DDI_PROP_TYPE_STRING;
2809 2808 (void) nvpair_value_string(prop, &str);
2810 2809 size = strlen(str) + 1;
2811 2810 off = di_checkmem(st, off, size);
2812 2811 (void) strcpy(di_mem_addr(st, off), str);
2813 2812 break;
2814 2813
2815 2814 case DATA_TYPE_BYTE_ARRAY:
2816 2815 case DATA_TYPE_INT16_ARRAY:
2817 2816 case DATA_TYPE_UINT16_ARRAY:
2818 2817 case DATA_TYPE_INT32_ARRAY:
2819 2818 case DATA_TYPE_UINT32_ARRAY:
2820 2819 case DATA_TYPE_INT64_ARRAY:
2821 2820 case DATA_TYPE_UINT64_ARRAY:
2822 2821 me->prop_type = DDI_PROP_TYPE_BYTE;
2823 2822 (void) nvpair_value_byte_array(prop, &buf, &nelems);
2824 2823 size = nelems;
2825 2824 if (nelems != 0) {
2826 2825 off = di_checkmem(st, off, size);
2827 2826 bcopy(buf, di_mem_addr(st, off), size);
2828 2827 }
2829 2828 break;
2830 2829
2831 2830 default: /* Unknown or unhandled type; skip it */
2832 2831 size = 0;
2833 2832 break;
2834 2833 }
2835 2834
2836 2835 if (size > 0) {
2837 2836 me->prop_data = off;
2838 2837 }
2839 2838
2840 2839 me->prop_len = (int)size;
2841 2840 off += size;
2842 2841
2843 2842 off = di_checkmem(st, off, sizeof (struct di_path_prop));
2844 2843 me->prop_next = off;
2845 2844 }
2846 2845
2847 2846 me->prop_next = 0;
2848 2847 return (off);
2849 2848 }
2850 2849
2851 2850
2852 2851 static void
2853 2852 di_path_one_endpoint(struct di_path *me, di_off_t noff, di_off_t **off_pp,
2854 2853 int get_client)
2855 2854 {
2856 2855 if (get_client) {
2857 2856 ASSERT(me->path_client == 0);
2858 2857 me->path_client = noff;
2859 2858 ASSERT(me->path_c_link == 0);
2860 2859 *off_pp = &me->path_c_link;
2861 2860 me->path_snap_state &=
2862 2861 ~(DI_PATH_SNAP_NOCLIENT | DI_PATH_SNAP_NOCLINK);
2863 2862 } else {
2864 2863 ASSERT(me->path_phci == 0);
2865 2864 me->path_phci = noff;
2866 2865 ASSERT(me->path_p_link == 0);
2867 2866 *off_pp = &me->path_p_link;
2868 2867 me->path_snap_state &=
2869 2868 ~(DI_PATH_SNAP_NOPHCI | DI_PATH_SNAP_NOPLINK);
2870 2869 }
2871 2870 }
2872 2871
2873 2872 /*
2874 2873 * off_p: pointer to the linkage field. This links pips along the client|phci
2875 2874 * linkage list.
2876 2875 * noff : Offset for the endpoint dip snapshot.
2877 2876 */
2878 2877 static di_off_t
2879 2878 di_getpath_data(dev_info_t *dip, di_off_t *off_p, di_off_t noff,
2880 2879 struct di_state *st, int get_client)
2881 2880 {
2882 2881 di_off_t off;
2883 2882 mdi_pathinfo_t *pip;
2884 2883 struct di_path *me;
2885 2884 mdi_pathinfo_t *(*next_pip)(dev_info_t *, mdi_pathinfo_t *);
2886 2885 size_t size;
2887 2886
2888 2887 dcmn_err2((CE_WARN, "di_getpath_data: client = %d", get_client));
2889 2888
2890 2889 /*
2891 2890 * The naming of the following mdi_xyz() is unfortunately
2892 2891 * non-intuitive. mdi_get_next_phci_path() follows the
2893 2892 * client_link i.e. the list of pip's belonging to the
2894 2893 * given client dip.
2895 2894 */
2896 2895 if (get_client)
2897 2896 next_pip = &mdi_get_next_phci_path;
2898 2897 else
2899 2898 next_pip = &mdi_get_next_client_path;
2900 2899
2901 2900 off = *off_p;
2902 2901
2903 2902 pip = NULL;
2904 2903 while (pip = (*next_pip)(dip, pip)) {
2905 2904 di_off_t stored_offset;
2906 2905
2907 2906 dcmn_err((CE_WARN, "marshalling pip = %p", (void *)pip));
2908 2907
2909 2908 mdi_pi_lock(pip);
2910 2909
2911 2910 /* We don't represent hidden paths in the snapshot */
2912 2911 if (mdi_pi_ishidden(pip)) {
2913 2912 dcmn_err((CE_WARN, "hidden, skip"));
2914 2913 mdi_pi_unlock(pip);
2915 2914 continue;
2916 2915 }
2917 2916
2918 2917 if (di_pip_find(st, pip, &stored_offset) != -1) {
2919 2918 /*
2920 2919 * We've already seen this pathinfo node so we need to
2921 2920 * take care not to snap it again; However, one endpoint
2922 2921 * and linkage will be set here. The other endpoint
2923 2922 * and linkage has already been set when the pip was
2924 2923 * first snapshotted i.e. when the other endpoint dip
2925 2924 * was snapshotted.
2926 2925 */
2927 2926 me = DI_PATH(di_mem_addr(st, stored_offset));
2928 2927 *off_p = stored_offset;
2929 2928
2930 2929 di_path_one_endpoint(me, noff, &off_p, get_client);
2931 2930
2932 2931 /*
2933 2932 * The other endpoint and linkage were set when this
2934 2933 * pip was snapshotted. So we are done with both
2935 2934 * endpoints and linkages.
2936 2935 */
2937 2936 ASSERT(!(me->path_snap_state &
2938 2937 (DI_PATH_SNAP_NOCLIENT|DI_PATH_SNAP_NOPHCI)));
2939 2938 ASSERT(!(me->path_snap_state &
2940 2939 (DI_PATH_SNAP_NOCLINK|DI_PATH_SNAP_NOPLINK)));
2941 2940
2942 2941 mdi_pi_unlock(pip);
2943 2942 continue;
2944 2943 }
2945 2944
2946 2945 /*
2947 2946 * Now that we need to snapshot this pip, check memory
2948 2947 */
2949 2948 size = sizeof (struct di_path);
2950 2949 *off_p = off = di_checkmem(st, off, size);
2951 2950 me = DI_PATH(di_mem_addr(st, off));
2952 2951 me->self = off;
2953 2952 off += size;
2954 2953
2955 2954 me->path_snap_state =
2956 2955 DI_PATH_SNAP_NOCLINK | DI_PATH_SNAP_NOPLINK;
2957 2956 me->path_snap_state |=
2958 2957 DI_PATH_SNAP_NOCLIENT | DI_PATH_SNAP_NOPHCI;
2959 2958
2960 2959 /*
2961 2960 * Zero out fields as di_checkmem() doesn't guarantee
2962 2961 * zero-filled memory
2963 2962 */
2964 2963 me->path_client = me->path_phci = 0;
2965 2964 me->path_c_link = me->path_p_link = 0;
2966 2965
2967 2966 di_path_one_endpoint(me, noff, &off_p, get_client);
2968 2967
2969 2968 /*
2970 2969 * Note the existence of this pathinfo
2971 2970 */
2972 2971 di_register_pip(st, pip, me->self);
2973 2972
2974 2973 me->path_state = path_state_convert(mdi_pi_get_state(pip));
2975 2974 me->path_flags = path_flags_convert(mdi_pi_get_flags(pip));
2976 2975
2977 2976 me->path_instance = mdi_pi_get_path_instance(pip);
2978 2977
2979 2978 /*
2980 2979 * Get intermediate addressing info.
2981 2980 */
2982 2981 size = strlen(mdi_pi_get_addr(pip)) + 1;
2983 2982 me->path_addr = off = di_checkmem(st, off, size);
2984 2983 (void) strcpy(di_mem_addr(st, off), mdi_pi_get_addr(pip));
2985 2984 off += size;
2986 2985
2987 2986 /*
2988 2987 * Get path properties if props are to be included in the
2989 2988 * snapshot
2990 2989 */
2991 2990 if (DINFOPROP & st->command) {
2992 2991 me->path_prop = off;
2993 2992 off = di_path_getprop(pip, &me->path_prop, st);
2994 2993 } else {
2995 2994 me->path_prop = 0;
2996 2995 }
2997 2996
2998 2997 mdi_pi_unlock(pip);
2999 2998 }
3000 2999
3001 3000 *off_p = 0;
3002 3001 return (off);
3003 3002 }
3004 3003
3005 3004 /*
3006 3005 * Return driver prop_op entry point for the specified devinfo node.
3007 3006 *
3008 3007 * To return a non-NULL value:
3009 3008 * - driver must be attached and held:
3010 3009 * If driver is not attached we ignore the driver property list.
3011 3010 * No one should rely on such properties.
3012 3011 * - driver "cb_prop_op != ddi_prop_op":
3013 3012 * If "cb_prop_op == ddi_prop_op", framework does not need to call driver.
3014 3013 * XXX or parent's bus_prop_op != ddi_bus_prop_op
3015 3014 */
3016 3015 static int
3017 3016 (*di_getprop_prop_op(struct dev_info *dip))
3018 3017 (dev_t, dev_info_t *, ddi_prop_op_t, int, char *, caddr_t, int *)
3019 3018 {
3020 3019 struct dev_ops *ops;
3021 3020
3022 3021 /* If driver is not attached we ignore the driver property list. */
3023 3022 if ((dip == NULL) || !i_ddi_devi_attached((dev_info_t *)dip))
3024 3023 return (NULL);
3025 3024
3026 3025 /*
3027 3026 * Some nexus drivers incorrectly set cb_prop_op to nodev, nulldev,
3028 3027 * or even NULL.
3029 3028 */
3030 3029 ops = dip->devi_ops;
3031 3030 if (ops && ops->devo_cb_ops &&
3032 3031 (ops->devo_cb_ops->cb_prop_op != ddi_prop_op) &&
3033 3032 (ops->devo_cb_ops->cb_prop_op != nodev) &&
3034 3033 (ops->devo_cb_ops->cb_prop_op != nulldev) &&
3035 3034 (ops->devo_cb_ops->cb_prop_op != NULL))
3036 3035 return (ops->devo_cb_ops->cb_prop_op);
3037 3036 return (NULL);
3038 3037 }
3039 3038
3040 3039 static di_off_t
3041 3040 di_getprop_add(int list, int dyn, struct di_state *st, struct dev_info *dip,
3042 3041 int (*prop_op)(),
3043 3042 char *name, dev_t devt, int aflags, int alen, caddr_t aval,
3044 3043 di_off_t off, di_off_t **off_pp)
3045 3044 {
3046 3045 int need_free = 0;
3047 3046 dev_t pdevt;
3048 3047 int pflags;
3049 3048 int rv;
3050 3049 caddr_t val;
3051 3050 int len;
3052 3051 size_t size;
3053 3052 struct di_prop *pp;
3054 3053
3055 3054 /* If we have prop_op function, ask driver for latest value */
3056 3055 if (prop_op) {
3057 3056 ASSERT(dip);
3058 3057
3059 3058 /* Must search DDI_DEV_T_NONE with DDI_DEV_T_ANY */
3060 3059 pdevt = (devt == DDI_DEV_T_NONE) ? DDI_DEV_T_ANY : devt;
3061 3060
3062 3061 /*
3063 3062 * We have type information in flags, but are invoking an
3064 3063 * old non-typed prop_op(9E) interface. Since not all types are
3065 3064 * part of DDI_PROP_TYPE_ANY (example is DDI_PROP_TYPE_INT64),
3066 3065 * we set DDI_PROP_CONSUMER_TYPED - causing the framework to
3067 3066 * expand type bits beyond DDI_PROP_TYPE_ANY. This allows us
3068 3067 * to use the legacy prop_op(9E) interface to obtain updates
3069 3068 * non-DDI_PROP_TYPE_ANY dynamic properties.
3070 3069 */
3071 3070 pflags = aflags & ~DDI_PROP_TYPE_MASK;
3072 3071 pflags |= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
3073 3072 DDI_PROP_CONSUMER_TYPED;
3074 3073
3075 3074 /*
3076 3075 * Hold and exit across prop_op(9E) to avoid lock order
3077 3076 * issues between
3078 3077 * [ndi_devi_enter() ..prop_op(9E).. driver-lock]
3079 3078 * .vs.
3080 3079 * [..ioctl(9E).. driver-lock ..ddi_remove_minor_node(9F)..
3081 3080 * ndi_devi_enter()]
3082 3081 * ordering.
3083 3082 */
3084 3083 ndi_hold_devi((dev_info_t *)dip);
3085 3084 ndi_devi_exit((dev_info_t *)dip, dip->devi_circular);
3086 3085 rv = (*prop_op)(pdevt, (dev_info_t *)dip,
3087 3086 PROP_LEN_AND_VAL_ALLOC, pflags, name, &val, &len);
3088 3087 ndi_devi_enter((dev_info_t *)dip, &dip->devi_circular);
3089 3088 ndi_rele_devi((dev_info_t *)dip);
3090 3089
3091 3090 if (rv == DDI_PROP_SUCCESS) {
3092 3091 need_free = 1; /* dynamic prop obtained */
3093 3092 } else if (dyn) {
3094 3093 /*
3095 3094 * A dynamic property must succeed prop_op(9E) to show
3096 3095 * up in the snapshot - that is the only source of its
3097 3096 * value.
3098 3097 */
3099 3098 return (off); /* dynamic prop not supported */
3100 3099 } else {
3101 3100 /*
3102 3101 * In case calling the driver caused an update off
3103 3102 * prop_op(9E) of a non-dynamic property (code leading
3104 3103 * to ddi_prop_change), we defer picking up val and
3105 3104 * len informatiojn until after prop_op(9E) to ensure
3106 3105 * that we snapshot the latest value.
3107 3106 */
3108 3107 val = aval;
3109 3108 len = alen;
3110 3109
3111 3110 }
3112 3111 } else {
3113 3112 val = aval;
3114 3113 len = alen;
3115 3114 }
3116 3115
3117 3116 dcmn_err((CE_CONT, "di_getprop_add: list %d %s len %d val %p\n",
3118 3117 list, name ? name : "NULL", len, (void *)val));
3119 3118
3120 3119 size = sizeof (struct di_prop);
3121 3120 **off_pp = off = di_checkmem(st, off, size);
3122 3121 pp = DI_PROP(di_mem_addr(st, off));
3123 3122 pp->self = off;
3124 3123 off += size;
3125 3124
3126 3125 pp->dev_major = getmajor(devt);
3127 3126 pp->dev_minor = getminor(devt);
3128 3127 pp->prop_flags = aflags;
3129 3128 pp->prop_list = list;
3130 3129
3131 3130 /* property name */
3132 3131 if (name) {
3133 3132 size = strlen(name) + 1;
3134 3133 pp->prop_name = off = di_checkmem(st, off, size);
3135 3134 (void) strcpy(di_mem_addr(st, off), name);
3136 3135 off += size;
3137 3136 } else {
3138 3137 pp->prop_name = -1;
3139 3138 }
3140 3139
3141 3140 pp->prop_len = len;
3142 3141 if (val == NULL) {
3143 3142 pp->prop_data = -1;
3144 3143 } else if (len != 0) {
3145 3144 size = len;
3146 3145 pp->prop_data = off = di_checkmem(st, off, size);
3147 3146 bcopy(val, di_mem_addr(st, off), size);
3148 3147 off += size;
3149 3148 }
3150 3149
3151 3150 pp->next = 0; /* assume tail for now */
3152 3151 *off_pp = &pp->next; /* return pointer to our next */
3153 3152
3154 3153 if (need_free) /* free PROP_LEN_AND_VAL_ALLOC alloc */
3155 3154 kmem_free(val, len);
3156 3155 return (off);
3157 3156 }
3158 3157
3159 3158
3160 3159 /*
3161 3160 * Copy a list of properties attached to a devinfo node. Called from
3162 3161 * di_copynode with active ndi_devi_enter. The major number is passed in case
3163 3162 * we need to call driver's prop_op entry. The value of list indicates
3164 3163 * which list we are copying. Possible values are:
3165 3164 * DI_PROP_DRV_LIST, DI_PROP_SYS_LIST, DI_PROP_GLB_LIST, DI_PROP_HW_LIST
3166 3165 */
3167 3166 static di_off_t
3168 3167 di_getprop(int list, struct ddi_prop **pprop, di_off_t *off_p,
3169 3168 struct di_state *st, struct dev_info *dip)
3170 3169 {
3171 3170 struct ddi_prop *prop;
3172 3171 int (*prop_op)();
3173 3172 int off;
3174 3173 struct ddi_minor_data *mn;
3175 3174 i_ddi_prop_dyn_t *dp;
3176 3175 struct plist {
3177 3176 struct plist *pl_next;
3178 3177 char *pl_name;
3179 3178 int pl_flags;
3180 3179 dev_t pl_dev;
3181 3180 int pl_len;
3182 3181 caddr_t pl_val;
3183 3182 } *pl, *pl0, **plp;
3184 3183
3185 3184 ASSERT(st != NULL);
3186 3185
3187 3186 off = *off_p;
3188 3187 *off_p = 0;
3189 3188 dcmn_err((CE_CONT, "di_getprop: copy property list %d at addr %p\n",
3190 3189 list, (void *)*pprop));
3191 3190
3192 3191 /* get pointer to driver's prop_op(9E) implementation if DRV_LIST */
3193 3192 prop_op = (list == DI_PROP_DRV_LIST) ? di_getprop_prop_op(dip) : NULL;
3194 3193
3195 3194 /*
3196 3195 * Form private list of properties, holding devi_lock for properties
3197 3196 * that hang off the dip.
3198 3197 */
3199 3198 if (dip)
3200 3199 mutex_enter(&(dip->devi_lock));
3201 3200 for (pl0 = NULL, plp = &pl0, prop = *pprop;
3202 3201 prop; plp = &pl->pl_next, prop = prop->prop_next) {
3203 3202 pl = kmem_alloc(sizeof (*pl), KM_SLEEP);
3204 3203 *plp = pl;
3205 3204 pl->pl_next = NULL;
3206 3205 if (prop->prop_name)
3207 3206 pl->pl_name = i_ddi_strdup(prop->prop_name, KM_SLEEP);
3208 3207 else
3209 3208 pl->pl_name = NULL;
3210 3209 pl->pl_flags = prop->prop_flags;
3211 3210 pl->pl_dev = prop->prop_dev;
3212 3211 if (prop->prop_len) {
3213 3212 pl->pl_len = prop->prop_len;
3214 3213 pl->pl_val = kmem_alloc(pl->pl_len, KM_SLEEP);
3215 3214 bcopy(prop->prop_val, pl->pl_val, pl->pl_len);
3216 3215 } else {
3217 3216 pl->pl_len = 0;
3218 3217 pl->pl_val = NULL;
3219 3218 }
3220 3219 }
3221 3220 if (dip)
3222 3221 mutex_exit(&(dip->devi_lock));
3223 3222
3224 3223 /*
3225 3224 * Now that we have dropped devi_lock, perform a second-pass to
3226 3225 * add properties to the snapshot. We do this as a second pass
3227 3226 * because we may need to call prop_op(9E) and we can't hold
3228 3227 * devi_lock across that call.
3229 3228 */
3230 3229 for (pl = pl0; pl; pl = pl0) {
3231 3230 pl0 = pl->pl_next;
3232 3231 off = di_getprop_add(list, 0, st, dip, prop_op, pl->pl_name,
3233 3232 pl->pl_dev, pl->pl_flags, pl->pl_len, pl->pl_val,
3234 3233 off, &off_p);
3235 3234 if (pl->pl_val)
3236 3235 kmem_free(pl->pl_val, pl->pl_len);
3237 3236 if (pl->pl_name)
3238 3237 kmem_free(pl->pl_name, strlen(pl->pl_name) + 1);
3239 3238 kmem_free(pl, sizeof (*pl));
3240 3239 }
3241 3240
3242 3241 /*
3243 3242 * If there is no prop_op or dynamic property support has been
3244 3243 * disabled, we are done.
3245 3244 */
3246 3245 if ((prop_op == NULL) || (di_prop_dyn == 0)) {
3247 3246 *off_p = 0;
3248 3247 return (off);
3249 3248 }
3250 3249
3251 3250 /* Add dynamic driver properties to snapshot */
3252 3251 for (dp = i_ddi_prop_dyn_driver_get((dev_info_t *)dip);
3253 3252 dp && dp->dp_name; dp++) {
3254 3253 if (dp->dp_spec_type) {
3255 3254 /* if spec_type, property of matching minor */
3256 3255 ASSERT(DEVI_BUSY_OWNED(dip));
3257 3256 for (mn = dip->devi_minor; mn; mn = mn->next) {
3258 3257 if (mn->ddm_spec_type != dp->dp_spec_type)
3259 3258 continue;
3260 3259 off = di_getprop_add(list, 1, st, dip, prop_op,
3261 3260 dp->dp_name, mn->ddm_dev, dp->dp_type,
3262 3261 0, NULL, off, &off_p);
3263 3262 }
3264 3263 } else {
3265 3264 /* property of devinfo node */
3266 3265 off = di_getprop_add(list, 1, st, dip, prop_op,
3267 3266 dp->dp_name, DDI_DEV_T_NONE, dp->dp_type,
3268 3267 0, NULL, off, &off_p);
3269 3268 }
3270 3269 }
3271 3270
3272 3271 /* Add dynamic parent properties to snapshot */
3273 3272 for (dp = i_ddi_prop_dyn_parent_get((dev_info_t *)dip);
3274 3273 dp && dp->dp_name; dp++) {
3275 3274 if (dp->dp_spec_type) {
3276 3275 /* if spec_type, property of matching minor */
3277 3276 ASSERT(DEVI_BUSY_OWNED(dip));
3278 3277 for (mn = dip->devi_minor; mn; mn = mn->next) {
3279 3278 if (mn->ddm_spec_type != dp->dp_spec_type)
3280 3279 continue;
3281 3280 off = di_getprop_add(list, 1, st, dip, prop_op,
3282 3281 dp->dp_name, mn->ddm_dev, dp->dp_type,
3283 3282 0, NULL, off, &off_p);
3284 3283 }
3285 3284 } else {
3286 3285 /* property of devinfo node */
3287 3286 off = di_getprop_add(list, 1, st, dip, prop_op,
3288 3287 dp->dp_name, DDI_DEV_T_NONE, dp->dp_type,
3289 3288 0, NULL, off, &off_p);
3290 3289 }
3291 3290 }
3292 3291
3293 3292 *off_p = 0;
3294 3293 return (off);
3295 3294 }
3296 3295
3297 3296 /*
3298 3297 * find private data format attached to a dip
3299 3298 * parent = 1 to match driver name of parent dip (for parent private data)
3300 3299 * 0 to match driver name of current dip (for driver private data)
3301 3300 */
3302 3301 #define DI_MATCH_DRIVER 0
3303 3302 #define DI_MATCH_PARENT 1
3304 3303
3305 3304 struct di_priv_format *
3306 3305 di_match_drv_name(struct dev_info *node, struct di_state *st, int match)
3307 3306 {
3308 3307 int i, count, len;
3309 3308 char *drv_name;
3310 3309 major_t major;
3311 3310 struct di_all *all;
3312 3311 struct di_priv_format *form;
3313 3312
3314 3313 dcmn_err2((CE_CONT, "di_match_drv_name: node = %s, match = %x\n",
3315 3314 node->devi_node_name, match));
3316 3315
3317 3316 if (match == DI_MATCH_PARENT) {
3318 3317 node = DEVI(node->devi_parent);
3319 3318 }
3320 3319
3321 3320 if (node == NULL) {
3322 3321 return (NULL);
3323 3322 }
3324 3323
3325 3324 major = node->devi_major;
3326 3325 if (major == (major_t)(-1)) {
3327 3326 return (NULL);
3328 3327 }
3329 3328
3330 3329 /*
3331 3330 * Match the driver name.
3332 3331 */
3333 3332 drv_name = ddi_major_to_name(major);
3334 3333 if ((drv_name == NULL) || *drv_name == '\0') {
3335 3334 return (NULL);
3336 3335 }
3337 3336
3338 3337 /* Now get the di_priv_format array */
3339 3338 all = DI_ALL_PTR(st);
3340 3339 if (match == DI_MATCH_PARENT) {
3341 3340 count = all->n_ppdata;
3342 3341 form = DI_PRIV_FORMAT(di_mem_addr(st, all->ppdata_format));
3343 3342 } else {
3344 3343 count = all->n_dpdata;
3345 3344 form = DI_PRIV_FORMAT(di_mem_addr(st, all->dpdata_format));
3346 3345 }
3347 3346
3348 3347 len = strlen(drv_name);
3349 3348 for (i = 0; i < count; i++) {
3350 3349 char *tmp;
3351 3350
3352 3351 tmp = form[i].drv_name;
3353 3352 while (tmp && (*tmp != '\0')) {
3354 3353 if (strncmp(drv_name, tmp, len) == 0) {
3355 3354 return (&form[i]);
3356 3355 }
3357 3356 /*
3358 3357 * Move to next driver name, skipping a white space
3359 3358 */
3360 3359 if (tmp = strchr(tmp, ' ')) {
3361 3360 tmp++;
3362 3361 }
3363 3362 }
3364 3363 }
3365 3364
3366 3365 return (NULL);
3367 3366 }
3368 3367
3369 3368 /*
3370 3369 * The following functions copy data as specified by the format passed in.
3371 3370 * To prevent invalid format from panicing the system, we call on_fault().
3372 3371 * A return value of 0 indicates an error. Otherwise, the total offset
3373 3372 * is returned.
3374 3373 */
3375 3374 #define DI_MAX_PRIVDATA (PAGESIZE >> 1) /* max private data size */
3376 3375
3377 3376 static di_off_t
3378 3377 di_getprvdata(struct di_priv_format *pdp, struct dev_info *node,
3379 3378 void *data, di_off_t *off_p, struct di_state *st)
3380 3379 {
3381 3380 caddr_t pa;
3382 3381 void *ptr;
3383 3382 int i, size, repeat;
3384 3383 di_off_t off, off0, *tmp;
3385 3384 char *path;
3386 3385 label_t ljb;
3387 3386
3388 3387 dcmn_err2((CE_CONT, "di_getprvdata:\n"));
3389 3388
3390 3389 /*
3391 3390 * check memory availability. Private data size is
3392 3391 * limited to DI_MAX_PRIVDATA.
3393 3392 */
3394 3393 off = di_checkmem(st, *off_p, DI_MAX_PRIVDATA);
3395 3394 *off_p = off;
3396 3395
3397 3396 if ((pdp->bytes == 0) || pdp->bytes > DI_MAX_PRIVDATA) {
3398 3397 goto failure;
3399 3398 }
3400 3399
3401 3400 if (!on_fault(&ljb)) {
3402 3401 /* copy the struct */
3403 3402 bcopy(data, di_mem_addr(st, off), pdp->bytes);
3404 3403 off0 = DI_ALIGN(pdp->bytes); /* XXX remove DI_ALIGN */
3405 3404
3406 3405 /* dereferencing pointers */
3407 3406 for (i = 0; i < MAX_PTR_IN_PRV; i++) {
3408 3407
3409 3408 if (pdp->ptr[i].size == 0) {
3410 3409 goto success; /* no more ptrs */
3411 3410 }
3412 3411
3413 3412 /*
3414 3413 * first, get the pointer content
3415 3414 */
3416 3415 if ((pdp->ptr[i].offset < 0) ||
3417 3416 (pdp->ptr[i].offset > pdp->bytes - sizeof (char *)))
3418 3417 goto failure; /* wrong offset */
3419 3418
3420 3419 pa = di_mem_addr(st, off + pdp->ptr[i].offset);
3421 3420
3422 3421 /* save a tmp ptr to store off_t later */
3423 3422 tmp = (di_off_t *)(intptr_t)pa;
3424 3423
3425 3424 /* get pointer value, if NULL continue */
3426 3425 ptr = *((void **) (intptr_t)pa);
3427 3426 if (ptr == NULL) {
3428 3427 continue;
3429 3428 }
3430 3429
3431 3430 /*
3432 3431 * next, find the repeat count (array dimension)
3433 3432 */
3434 3433 repeat = pdp->ptr[i].len_offset;
3435 3434
3436 3435 /*
3437 3436 * Positive value indicates a fixed sized array.
3438 3437 * 0 or negative value indicates variable sized array.
3439 3438 *
3440 3439 * For variable sized array, the variable must be
3441 3440 * an int member of the structure, with an offset
3442 3441 * equal to the absolution value of struct member.
3443 3442 */
3444 3443 if (repeat > pdp->bytes - sizeof (int)) {
3445 3444 goto failure; /* wrong offset */
3446 3445 }
3447 3446
3448 3447 if (repeat >= 0) {
3449 3448 repeat = *((int *)
3450 3449 (intptr_t)((caddr_t)data + repeat));
3451 3450 } else {
3452 3451 repeat = -repeat;
3453 3452 }
3454 3453
3455 3454 /*
3456 3455 * next, get the size of the object to be copied
3457 3456 */
3458 3457 size = pdp->ptr[i].size * repeat;
3459 3458
3460 3459 /*
3461 3460 * Arbitrarily limit the total size of object to be
3462 3461 * copied (1 byte to 1/4 page).
3463 3462 */
3464 3463 if ((size <= 0) || (size > (DI_MAX_PRIVDATA - off0))) {
3465 3464 goto failure; /* wrong size or too big */
3466 3465 }
3467 3466
3468 3467 /*
3469 3468 * Now copy the data
3470 3469 */
3471 3470 *tmp = off0;
3472 3471 bcopy(ptr, di_mem_addr(st, off + off0), size);
3473 3472 off0 += DI_ALIGN(size); /* XXX remove DI_ALIGN */
3474 3473 }
3475 3474 } else {
3476 3475 goto failure;
3477 3476 }
3478 3477
3479 3478 success:
3480 3479 /*
3481 3480 * success if reached here
3482 3481 */
3483 3482 no_fault();
3484 3483 return (off + off0);
3485 3484 /*NOTREACHED*/
3486 3485
3487 3486 failure:
3488 3487 /*
3489 3488 * fault occurred
3490 3489 */
3491 3490 no_fault();
3492 3491 path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3493 3492 cmn_err(CE_WARN, "devinfo: fault on private data for '%s' at %p",
3494 3493 ddi_pathname((dev_info_t *)node, path), data);
3495 3494 kmem_free(path, MAXPATHLEN);
3496 3495 *off_p = -1; /* set private data to indicate error */
3497 3496
3498 3497 return (off);
3499 3498 }
3500 3499
3501 3500 /*
3502 3501 * get parent private data; on error, returns original offset
3503 3502 */
3504 3503 static di_off_t
3505 3504 di_getppdata(struct dev_info *node, di_off_t *off_p, struct di_state *st)
3506 3505 {
3507 3506 int off;
3508 3507 struct di_priv_format *ppdp;
3509 3508
3510 3509 dcmn_err2((CE_CONT, "di_getppdata:\n"));
3511 3510
3512 3511 /* find the parent data format */
3513 3512 if ((ppdp = di_match_drv_name(node, st, DI_MATCH_PARENT)) == NULL) {
3514 3513 off = *off_p;
3515 3514 *off_p = 0; /* set parent data to none */
3516 3515 return (off);
3517 3516 }
3518 3517
3519 3518 return (di_getprvdata(ppdp, node,
3520 3519 ddi_get_parent_data((dev_info_t *)node), off_p, st));
3521 3520 }
3522 3521
3523 3522 /*
3524 3523 * get parent private data; returns original offset
3525 3524 */
3526 3525 static di_off_t
3527 3526 di_getdpdata(struct dev_info *node, di_off_t *off_p, struct di_state *st)
3528 3527 {
3529 3528 int off;
3530 3529 struct di_priv_format *dpdp;
3531 3530
3532 3531 dcmn_err2((CE_CONT, "di_getdpdata:"));
3533 3532
3534 3533 /* find the parent data format */
3535 3534 if ((dpdp = di_match_drv_name(node, st, DI_MATCH_DRIVER)) == NULL) {
3536 3535 off = *off_p;
3537 3536 *off_p = 0; /* set driver data to none */
3538 3537 return (off);
3539 3538 }
3540 3539
3541 3540 return (di_getprvdata(dpdp, node,
3542 3541 ddi_get_driver_private((dev_info_t *)node), off_p, st));
3543 3542 }
3544 3543
3545 3544 /*
3546 3545 * Copy hotplug data associated with a devinfo node into the snapshot.
3547 3546 */
3548 3547 static di_off_t
3549 3548 di_gethpdata(ddi_hp_cn_handle_t *hp_hdl, di_off_t *off_p,
3550 3549 struct di_state *st)
3551 3550 {
3552 3551 struct i_hp *hp;
3553 3552 struct di_hp *me;
3554 3553 size_t size;
3555 3554 di_off_t off;
3556 3555
3557 3556 dcmn_err2((CE_CONT, "di_gethpdata:\n"));
3558 3557
3559 3558 /*
3560 3559 * check memory first
3561 3560 */
3562 3561 off = di_checkmem(st, *off_p, sizeof (struct di_hp));
3563 3562 *off_p = off;
3564 3563
3565 3564 do {
3566 3565 me = DI_HP(di_mem_addr(st, off));
3567 3566 me->self = off;
3568 3567 me->hp_name = 0;
3569 3568 me->hp_connection = (int)hp_hdl->cn_info.cn_num;
3570 3569 me->hp_depends_on = (int)hp_hdl->cn_info.cn_num_dpd_on;
3571 3570 (void) ddihp_cn_getstate(hp_hdl);
3572 3571 me->hp_state = (int)hp_hdl->cn_info.cn_state;
3573 3572 me->hp_type = (int)hp_hdl->cn_info.cn_type;
3574 3573 me->hp_type_str = 0;
3575 3574 me->hp_last_change = (uint32_t)hp_hdl->cn_info.cn_last_change;
3576 3575 me->hp_child = 0;
3577 3576
3578 3577 /*
3579 3578 * Child links are resolved later by di_hotplug_children().
3580 3579 * Store a reference to this di_hp_t in the list used later
3581 3580 * by di_hotplug_children().
3582 3581 */
3583 3582 hp = kmem_zalloc(sizeof (i_hp_t), KM_SLEEP);
3584 3583 hp->hp_off = off;
3585 3584 hp->hp_child = hp_hdl->cn_info.cn_child;
3586 3585 list_insert_tail(&st->hp_list, hp);
3587 3586
3588 3587 off += sizeof (struct di_hp);
3589 3588
3590 3589 /* Add name of this di_hp_t to the snapshot */
3591 3590 if (hp_hdl->cn_info.cn_name) {
3592 3591 size = strlen(hp_hdl->cn_info.cn_name) + 1;
3593 3592 me->hp_name = off = di_checkmem(st, off, size);
3594 3593 (void) strcpy(di_mem_addr(st, off),
3595 3594 hp_hdl->cn_info.cn_name);
3596 3595 off += size;
3597 3596 }
3598 3597
3599 3598 /* Add type description of this di_hp_t to the snapshot */
3600 3599 if (hp_hdl->cn_info.cn_type_str) {
3601 3600 size = strlen(hp_hdl->cn_info.cn_type_str) + 1;
3602 3601 me->hp_type_str = off = di_checkmem(st, off, size);
3603 3602 (void) strcpy(di_mem_addr(st, off),
3604 3603 hp_hdl->cn_info.cn_type_str);
3605 3604 off += size;
3606 3605 }
3607 3606
3608 3607 /*
3609 3608 * Set link to next in the chain of di_hp_t nodes,
3610 3609 * or terminate the chain when processing the last node.
3611 3610 */
3612 3611 if (hp_hdl->next != NULL) {
3613 3612 off = di_checkmem(st, off, sizeof (struct di_hp));
3614 3613 me->next = off;
3615 3614 } else {
3616 3615 me->next = 0;
3617 3616 }
3618 3617
3619 3618 /* Update pointer to next in the chain */
3620 3619 hp_hdl = hp_hdl->next;
3621 3620
3622 3621 } while (hp_hdl);
3623 3622
3624 3623 return (off);
3625 3624 }
3626 3625
3627 3626 /*
3628 3627 * The driver is stateful across DINFOCPYALL and DINFOUSRLD.
3629 3628 * This function encapsulates the state machine:
3630 3629 *
3631 3630 * -> IOC_IDLE -> IOC_SNAP -> IOC_DONE -> IOC_COPY ->
3632 3631 * | SNAPSHOT USRLD |
3633 3632 * --------------------------------------------------
3634 3633 *
3635 3634 * Returns 0 on success and -1 on failure
3636 3635 */
3637 3636 static int
3638 3637 di_setstate(struct di_state *st, int new_state)
3639 3638 {
3640 3639 int ret = 0;
3641 3640
3642 3641 mutex_enter(&di_lock);
3643 3642 switch (new_state) {
3644 3643 case IOC_IDLE:
3645 3644 case IOC_DONE:
3646 3645 break;
3647 3646 case IOC_SNAP:
3648 3647 if (st->di_iocstate != IOC_IDLE)
3649 3648 ret = -1;
3650 3649 break;
3651 3650 case IOC_COPY:
3652 3651 if (st->di_iocstate != IOC_DONE)
3653 3652 ret = -1;
3654 3653 break;
3655 3654 default:
3656 3655 ret = -1;
3657 3656 }
3658 3657
3659 3658 if (ret == 0)
3660 3659 st->di_iocstate = new_state;
3661 3660 else
3662 3661 cmn_err(CE_NOTE, "incorrect state transition from %d to %d",
3663 3662 st->di_iocstate, new_state);
3664 3663 mutex_exit(&di_lock);
3665 3664 return (ret);
3666 3665 }
3667 3666
3668 3667 /*
3669 3668 * We cannot assume the presence of the entire
3670 3669 * snapshot in this routine. All we are guaranteed
3671 3670 * is the di_all struct + 1 byte (for root_path)
3672 3671 */
3673 3672 static int
3674 3673 header_plus_one_ok(struct di_all *all)
3675 3674 {
3676 3675 /*
3677 3676 * Refuse to read old versions
3678 3677 */
3679 3678 if (all->version != DI_SNAPSHOT_VERSION) {
3680 3679 CACHE_DEBUG((DI_ERR, "bad version: 0x%x", all->version));
3681 3680 return (0);
3682 3681 }
3683 3682
3684 3683 if (all->cache_magic != DI_CACHE_MAGIC) {
3685 3684 CACHE_DEBUG((DI_ERR, "bad magic #: 0x%x", all->cache_magic));
3686 3685 return (0);
3687 3686 }
3688 3687
3689 3688 if (all->snapshot_time == 0) {
3690 3689 CACHE_DEBUG((DI_ERR, "bad timestamp: %ld", all->snapshot_time));
3691 3690 return (0);
3692 3691 }
3693 3692
3694 3693 if (all->top_devinfo == 0) {
3695 3694 CACHE_DEBUG((DI_ERR, "NULL top devinfo"));
3696 3695 return (0);
3697 3696 }
3698 3697
3699 3698 if (all->map_size < sizeof (*all) + 1) {
3700 3699 CACHE_DEBUG((DI_ERR, "bad map size: %u", all->map_size));
3701 3700 return (0);
3702 3701 }
3703 3702
3704 3703 if (all->root_path[0] != '/' || all->root_path[1] != '\0') {
3705 3704 CACHE_DEBUG((DI_ERR, "bad rootpath: %c%c",
3706 3705 all->root_path[0], all->root_path[1]));
3707 3706 return (0);
3708 3707 }
3709 3708
3710 3709 /*
3711 3710 * We can't check checksum here as we just have the header
3712 3711 */
3713 3712
3714 3713 return (1);
3715 3714 }
3716 3715
3717 3716 static int
3718 3717 chunk_write(struct vnode *vp, offset_t off, caddr_t buf, size_t len)
3719 3718 {
3720 3719 rlim64_t rlimit;
3721 3720 ssize_t resid;
3722 3721 int error = 0;
3723 3722
3724 3723
3725 3724 rlimit = RLIM64_INFINITY;
3726 3725
3727 3726 while (len) {
3728 3727 resid = 0;
3729 3728 error = vn_rdwr(UIO_WRITE, vp, buf, len, off,
3730 3729 UIO_SYSSPACE, FSYNC, rlimit, kcred, &resid);
3731 3730
3732 3731 if (error || resid < 0) {
3733 3732 error = error ? error : EIO;
3734 3733 CACHE_DEBUG((DI_ERR, "write error: %d", error));
3735 3734 break;
3736 3735 }
3737 3736
3738 3737 /*
3739 3738 * Check if we are making progress
3740 3739 */
3741 3740 if (resid >= len) {
3742 3741 error = ENOSPC;
3743 3742 break;
3744 3743 }
3745 3744 buf += len - resid;
3746 3745 off += len - resid;
3747 3746 len = resid;
3748 3747 }
3749 3748
3750 3749 return (error);
3751 3750 }
3752 3751
3753 3752 static void
3754 3753 di_cache_write(struct di_cache *cache)
3755 3754 {
3756 3755 struct di_all *all;
3757 3756 struct vnode *vp;
3758 3757 int oflags;
3759 3758 size_t map_size;
3760 3759 size_t chunk;
3761 3760 offset_t off;
3762 3761 int error;
3763 3762 char *buf;
3764 3763
3765 3764 ASSERT(DI_CACHE_LOCKED(*cache));
3766 3765 ASSERT(!servicing_interrupt());
3767 3766
3768 3767 if (cache->cache_size == 0) {
3769 3768 ASSERT(cache->cache_data == NULL);
3770 3769 CACHE_DEBUG((DI_ERR, "Empty cache. Skipping write"));
3771 3770 return;
3772 3771 }
3773 3772
3774 3773 ASSERT(cache->cache_size > 0);
3775 3774 ASSERT(cache->cache_data);
3776 3775
3777 3776 if (!modrootloaded || rootvp == NULL || vn_is_readonly(rootvp)) {
3778 3777 CACHE_DEBUG((DI_ERR, "Can't write to rootFS. Skipping write"));
3779 3778 return;
3780 3779 }
3781 3780
3782 3781 all = (struct di_all *)cache->cache_data;
3783 3782
3784 3783 if (!header_plus_one_ok(all)) {
3785 3784 CACHE_DEBUG((DI_ERR, "Invalid header. Skipping write"));
3786 3785 return;
3787 3786 }
3788 3787
3789 3788 ASSERT(strcmp(all->root_path, "/") == 0);
3790 3789
3791 3790 /*
3792 3791 * The cache_size is the total allocated memory for the cache.
3793 3792 * The map_size is the actual size of valid data in the cache.
3794 3793 * map_size may be smaller than cache_size but cannot exceed
3795 3794 * cache_size.
3796 3795 */
3797 3796 if (all->map_size > cache->cache_size) {
3798 3797 CACHE_DEBUG((DI_ERR, "map_size (0x%x) > cache_size (0x%x)."
3799 3798 " Skipping write", all->map_size, cache->cache_size));
3800 3799 return;
3801 3800 }
3802 3801
3803 3802 /*
3804 3803 * First unlink the temp file
3805 3804 */
3806 3805 error = vn_remove(DI_CACHE_TEMP, UIO_SYSSPACE, RMFILE);
3807 3806 if (error && error != ENOENT) {
3808 3807 CACHE_DEBUG((DI_ERR, "%s: unlink failed: %d",
3809 3808 DI_CACHE_TEMP, error));
3810 3809 }
3811 3810
3812 3811 if (error == EROFS) {
3813 3812 CACHE_DEBUG((DI_ERR, "RDONLY FS. Skipping write"));
3814 3813 return;
3815 3814 }
3816 3815
3817 3816 vp = NULL;
3818 3817 oflags = (FCREAT|FWRITE);
3819 3818 if (error = vn_open(DI_CACHE_TEMP, UIO_SYSSPACE, oflags,
3820 3819 DI_CACHE_PERMS, &vp, CRCREAT, 0)) {
3821 3820 CACHE_DEBUG((DI_ERR, "%s: create failed: %d",
3822 3821 DI_CACHE_TEMP, error));
3823 3822 return;
3824 3823 }
3825 3824
3826 3825 ASSERT(vp);
3827 3826
3828 3827 /*
3829 3828 * Paranoid: Check if the file is on a read-only FS
3830 3829 */
3831 3830 if (vn_is_readonly(vp)) {
3832 3831 CACHE_DEBUG((DI_ERR, "cannot write: readonly FS"));
3833 3832 goto fail;
3834 3833 }
3835 3834
3836 3835 /*
3837 3836 * Note that we only write map_size bytes to disk - this saves
3838 3837 * space as the actual cache size may be larger than size of
3839 3838 * valid data in the cache.
3840 3839 * Another advantage is that it makes verification of size
3841 3840 * easier when the file is read later.
3842 3841 */
3843 3842 map_size = all->map_size;
3844 3843 off = 0;
3845 3844 buf = cache->cache_data;
3846 3845
3847 3846 while (map_size) {
3848 3847 ASSERT(map_size > 0);
3849 3848 /*
3850 3849 * Write in chunks so that VM system
3851 3850 * is not overwhelmed
3852 3851 */
3853 3852 if (map_size > di_chunk * PAGESIZE)
3854 3853 chunk = di_chunk * PAGESIZE;
3855 3854 else
3856 3855 chunk = map_size;
3857 3856
3858 3857 error = chunk_write(vp, off, buf, chunk);
3859 3858 if (error) {
3860 3859 CACHE_DEBUG((DI_ERR, "write failed: off=0x%x: %d",
3861 3860 off, error));
3862 3861 goto fail;
3863 3862 }
3864 3863
3865 3864 off += chunk;
3866 3865 buf += chunk;
3867 3866 map_size -= chunk;
3868 3867
3869 3868 /* If low on memory, give pageout a chance to run */
3870 3869 if (freemem < desfree)
3871 3870 delay(1);
3872 3871 }
3873 3872
3874 3873 /*
3875 3874 * Now sync the file and close it
3876 3875 */
3877 3876 if (error = VOP_FSYNC(vp, FSYNC, kcred, NULL)) {
3878 3877 CACHE_DEBUG((DI_ERR, "FSYNC failed: %d", error));
3879 3878 }
3880 3879
3881 3880 if (error = VOP_CLOSE(vp, oflags, 1, (offset_t)0, kcred, NULL)) {
3882 3881 CACHE_DEBUG((DI_ERR, "close() failed: %d", error));
3883 3882 VN_RELE(vp);
3884 3883 return;
3885 3884 }
3886 3885
3887 3886 VN_RELE(vp);
3888 3887
3889 3888 /*
3890 3889 * Now do the rename
3891 3890 */
3892 3891 if (error = vn_rename(DI_CACHE_TEMP, DI_CACHE_FILE, UIO_SYSSPACE)) {
3893 3892 CACHE_DEBUG((DI_ERR, "rename failed: %d", error));
3894 3893 return;
3895 3894 }
3896 3895
3897 3896 CACHE_DEBUG((DI_INFO, "Cache write successful."));
3898 3897
3899 3898 return;
3900 3899
3901 3900 fail:
3902 3901 (void) VOP_CLOSE(vp, oflags, 1, (offset_t)0, kcred, NULL);
3903 3902 VN_RELE(vp);
3904 3903 }
3905 3904
3906 3905
3907 3906 /*
3908 3907 * Since we could be called early in boot,
3909 3908 * use kobj_read_file()
3910 3909 */
3911 3910 static void
3912 3911 di_cache_read(struct di_cache *cache)
3913 3912 {
3914 3913 struct _buf *file;
3915 3914 struct di_all *all;
3916 3915 int n;
3917 3916 size_t map_size, sz, chunk;
3918 3917 offset_t off;
3919 3918 caddr_t buf;
3920 3919 uint32_t saved_crc, crc;
3921 3920
3922 3921 ASSERT(modrootloaded);
3923 3922 ASSERT(DI_CACHE_LOCKED(*cache));
3924 3923 ASSERT(cache->cache_data == NULL);
3925 3924 ASSERT(cache->cache_size == 0);
3926 3925 ASSERT(!servicing_interrupt());
3927 3926
3928 3927 file = kobj_open_file(DI_CACHE_FILE);
3929 3928 if (file == (struct _buf *)-1) {
3930 3929 CACHE_DEBUG((DI_ERR, "%s: open failed: %d",
3931 3930 DI_CACHE_FILE, ENOENT));
3932 3931 return;
3933 3932 }
3934 3933
3935 3934 /*
3936 3935 * Read in the header+root_path first. The root_path must be "/"
3937 3936 */
3938 3937 all = kmem_zalloc(sizeof (*all) + 1, KM_SLEEP);
3939 3938 n = kobj_read_file(file, (caddr_t)all, sizeof (*all) + 1, 0);
3940 3939
3941 3940 if ((n != sizeof (*all) + 1) || !header_plus_one_ok(all)) {
3942 3941 kmem_free(all, sizeof (*all) + 1);
3943 3942 kobj_close_file(file);
3944 3943 CACHE_DEBUG((DI_ERR, "cache header: read error or invalid"));
3945 3944 return;
3946 3945 }
3947 3946
3948 3947 map_size = all->map_size;
3949 3948
3950 3949 kmem_free(all, sizeof (*all) + 1);
3951 3950
3952 3951 ASSERT(map_size >= sizeof (*all) + 1);
3953 3952
3954 3953 buf = di_cache.cache_data = kmem_alloc(map_size, KM_SLEEP);
3955 3954 sz = map_size;
3956 3955 off = 0;
3957 3956 while (sz) {
3958 3957 /* Don't overload VM with large reads */
3959 3958 chunk = (sz > di_chunk * PAGESIZE) ? di_chunk * PAGESIZE : sz;
3960 3959 n = kobj_read_file(file, buf, chunk, off);
3961 3960 if (n != chunk) {
3962 3961 CACHE_DEBUG((DI_ERR, "%s: read error at offset: %lld",
3963 3962 DI_CACHE_FILE, off));
3964 3963 goto fail;
3965 3964 }
3966 3965 off += chunk;
3967 3966 buf += chunk;
3968 3967 sz -= chunk;
3969 3968 }
3970 3969
3971 3970 ASSERT(off == map_size);
3972 3971
3973 3972 /*
3974 3973 * Read past expected EOF to verify size.
3975 3974 */
3976 3975 if (kobj_read_file(file, (caddr_t)&sz, 1, off) > 0) {
3977 3976 CACHE_DEBUG((DI_ERR, "%s: file size changed", DI_CACHE_FILE));
3978 3977 goto fail;
3979 3978 }
3980 3979
3981 3980 all = (struct di_all *)di_cache.cache_data;
3982 3981 if (!header_plus_one_ok(all)) {
3983 3982 CACHE_DEBUG((DI_ERR, "%s: file header changed", DI_CACHE_FILE));
3984 3983 goto fail;
3985 3984 }
3986 3985
3987 3986 /*
3988 3987 * Compute CRC with checksum field in the cache data set to 0
3989 3988 */
3990 3989 saved_crc = all->cache_checksum;
3991 3990 all->cache_checksum = 0;
3992 3991 CRC32(crc, di_cache.cache_data, map_size, -1U, crc32_table);
3993 3992 all->cache_checksum = saved_crc;
3994 3993
3995 3994 if (crc != all->cache_checksum) {
3996 3995 CACHE_DEBUG((DI_ERR,
3997 3996 "%s: checksum error: expected=0x%x actual=0x%x",
3998 3997 DI_CACHE_FILE, all->cache_checksum, crc));
3999 3998 goto fail;
4000 3999 }
4001 4000
4002 4001 if (all->map_size != map_size) {
4003 4002 CACHE_DEBUG((DI_ERR, "%s: map size changed", DI_CACHE_FILE));
4004 4003 goto fail;
4005 4004 }
4006 4005
4007 4006 kobj_close_file(file);
4008 4007
4009 4008 di_cache.cache_size = map_size;
4010 4009
4011 4010 return;
4012 4011
4013 4012 fail:
4014 4013 kmem_free(di_cache.cache_data, map_size);
4015 4014 kobj_close_file(file);
4016 4015 di_cache.cache_data = NULL;
4017 4016 di_cache.cache_size = 0;
4018 4017 }
4019 4018
4020 4019
4021 4020 /*
4022 4021 * Checks if arguments are valid for using the cache.
4023 4022 */
4024 4023 static int
4025 4024 cache_args_valid(struct di_state *st, int *error)
4026 4025 {
4027 4026 ASSERT(error);
4028 4027 ASSERT(st->mem_size > 0);
4029 4028 ASSERT(st->memlist != NULL);
4030 4029
4031 4030 if (!modrootloaded || !i_ddi_io_initialized()) {
4032 4031 CACHE_DEBUG((DI_ERR,
4033 4032 "cache lookup failure: I/O subsystem not inited"));
4034 4033 *error = ENOTACTIVE;
4035 4034 return (0);
4036 4035 }
4037 4036
4038 4037 /*
4039 4038 * No other flags allowed with DINFOCACHE
4040 4039 */
4041 4040 if (st->command != (DINFOCACHE & DIIOC_MASK)) {
4042 4041 CACHE_DEBUG((DI_ERR,
4043 4042 "cache lookup failure: bad flags: 0x%x",
4044 4043 st->command));
4045 4044 *error = EINVAL;
4046 4045 return (0);
4047 4046 }
4048 4047
4049 4048 if (strcmp(DI_ALL_PTR(st)->root_path, "/") != 0) {
4050 4049 CACHE_DEBUG((DI_ERR,
4051 4050 "cache lookup failure: bad root: %s",
4052 4051 DI_ALL_PTR(st)->root_path));
4053 4052 *error = EINVAL;
4054 4053 return (0);
4055 4054 }
4056 4055
4057 4056 CACHE_DEBUG((DI_INFO, "cache lookup args ok: 0x%x", st->command));
4058 4057
4059 4058 *error = 0;
4060 4059
4061 4060 return (1);
4062 4061 }
4063 4062
4064 4063 static int
4065 4064 snapshot_is_cacheable(struct di_state *st)
4066 4065 {
4067 4066 ASSERT(st->mem_size > 0);
4068 4067 ASSERT(st->memlist != NULL);
4069 4068
4070 4069 if ((st->command & DI_CACHE_SNAPSHOT_FLAGS) !=
4071 4070 (DI_CACHE_SNAPSHOT_FLAGS & DIIOC_MASK)) {
4072 4071 CACHE_DEBUG((DI_INFO,
4073 4072 "not cacheable: incompatible flags: 0x%x",
4074 4073 st->command));
4075 4074 return (0);
4076 4075 }
4077 4076
4078 4077 if (strcmp(DI_ALL_PTR(st)->root_path, "/") != 0) {
4079 4078 CACHE_DEBUG((DI_INFO,
4080 4079 "not cacheable: incompatible root path: %s",
4081 4080 DI_ALL_PTR(st)->root_path));
4082 4081 return (0);
4083 4082 }
4084 4083
4085 4084 CACHE_DEBUG((DI_INFO, "cacheable snapshot request: 0x%x", st->command));
4086 4085
4087 4086 return (1);
4088 4087 }
4089 4088
4090 4089 static int
4091 4090 di_cache_lookup(struct di_state *st)
4092 4091 {
4093 4092 size_t rval;
4094 4093 int cache_valid;
4095 4094
4096 4095 ASSERT(cache_args_valid(st, &cache_valid));
4097 4096 ASSERT(modrootloaded);
4098 4097
4099 4098 DI_CACHE_LOCK(di_cache);
4100 4099
4101 4100 /*
4102 4101 * The following assignment determines the validity
4103 4102 * of the cache as far as this snapshot is concerned.
4104 4103 */
4105 4104 cache_valid = di_cache.cache_valid;
4106 4105
4107 4106 if (cache_valid && di_cache.cache_data == NULL) {
4108 4107 di_cache_read(&di_cache);
4109 4108 /* check for read or file error */
4110 4109 if (di_cache.cache_data == NULL)
4111 4110 cache_valid = 0;
4112 4111 }
4113 4112
4114 4113 if (cache_valid) {
4115 4114 /*
4116 4115 * Ok, the cache was valid as of this particular
4117 4116 * snapshot. Copy the cached snapshot. This is safe
4118 4117 * to do as the cache cannot be freed (we hold the
4119 4118 * cache lock). Free the memory allocated in di_state
4120 4119 * up until this point - we will simply copy everything
4121 4120 * in the cache.
4122 4121 */
4123 4122
4124 4123 ASSERT(di_cache.cache_data != NULL);
4125 4124 ASSERT(di_cache.cache_size > 0);
4126 4125
4127 4126 di_freemem(st);
4128 4127
4129 4128 rval = 0;
4130 4129 if (di_cache2mem(&di_cache, st) > 0) {
4131 4130 /*
4132 4131 * map_size is size of valid data in the
4133 4132 * cached snapshot and may be less than
4134 4133 * size of the cache.
4135 4134 */
4136 4135 ASSERT(DI_ALL_PTR(st));
4137 4136 rval = DI_ALL_PTR(st)->map_size;
4138 4137
4139 4138 ASSERT(rval >= sizeof (struct di_all));
4140 4139 ASSERT(rval <= di_cache.cache_size);
4141 4140 }
4142 4141 } else {
4143 4142 /*
4144 4143 * The cache isn't valid, we need to take a snapshot.
4145 4144 * Set the command flags appropriately
4146 4145 */
4147 4146 ASSERT(st->command == (DINFOCACHE & DIIOC_MASK));
4148 4147 st->command = (DI_CACHE_SNAPSHOT_FLAGS & DIIOC_MASK);
4149 4148 rval = di_cache_update(st);
4150 4149 st->command = (DINFOCACHE & DIIOC_MASK);
4151 4150 }
4152 4151
4153 4152 DI_CACHE_UNLOCK(di_cache);
4154 4153
4155 4154 /*
4156 4155 * For cached snapshots, the devinfo driver always returns
4157 4156 * a snapshot rooted at "/".
4158 4157 */
4159 4158 ASSERT(rval == 0 || strcmp(DI_ALL_PTR(st)->root_path, "/") == 0);
4160 4159
4161 4160 return ((int)rval);
4162 4161 }
4163 4162
4164 4163 /*
4165 4164 * This is a forced update of the cache - the previous state of the cache
4166 4165 * may be:
4167 4166 * - unpopulated
4168 4167 * - populated and invalid
4169 4168 * - populated and valid
4170 4169 */
4171 4170 static int
4172 4171 di_cache_update(struct di_state *st)
4173 4172 {
4174 4173 int rval;
4175 4174 uint32_t crc;
4176 4175 struct di_all *all;
4177 4176
4178 4177 ASSERT(DI_CACHE_LOCKED(di_cache));
4179 4178 ASSERT(snapshot_is_cacheable(st));
4180 4179
4181 4180 /*
4182 4181 * Free the in-core cache and the on-disk file (if they exist)
4183 4182 */
4184 4183 i_ddi_di_cache_free(&di_cache);
4185 4184
4186 4185 /*
4187 4186 * Set valid flag before taking the snapshot,
4188 4187 * so that any invalidations that arrive
4189 4188 * during or after the snapshot are not
4190 4189 * removed by us.
4191 4190 */
4192 4191 atomic_or_32(&di_cache.cache_valid, 1);
4193 4192
4194 4193 rval = di_snapshot_and_clean(st);
4195 4194
4196 4195 if (rval == 0) {
4197 4196 CACHE_DEBUG((DI_ERR, "can't update cache: bad snapshot"));
4198 4197 return (0);
4199 4198 }
4200 4199
4201 4200 DI_ALL_PTR(st)->map_size = rval;
4202 4201 if (di_mem2cache(st, &di_cache) == 0) {
4203 4202 CACHE_DEBUG((DI_ERR, "can't update cache: copy failed"));
4204 4203 return (0);
4205 4204 }
4206 4205
4207 4206 ASSERT(di_cache.cache_data);
4208 4207 ASSERT(di_cache.cache_size > 0);
4209 4208
4210 4209 /*
4211 4210 * Now that we have cached the snapshot, compute its checksum.
4212 4211 * The checksum is only computed over the valid data in the
4213 4212 * cache, not the entire cache.
4214 4213 * Also, set all the fields (except checksum) before computing
4215 4214 * checksum.
4216 4215 */
4217 4216 all = (struct di_all *)di_cache.cache_data;
4218 4217 all->cache_magic = DI_CACHE_MAGIC;
4219 4218 all->map_size = rval;
4220 4219
4221 4220 ASSERT(all->cache_checksum == 0);
4222 4221 CRC32(crc, di_cache.cache_data, all->map_size, -1U, crc32_table);
4223 4222 all->cache_checksum = crc;
4224 4223
4225 4224 di_cache_write(&di_cache);
4226 4225
4227 4226 return (rval);
4228 4227 }
4229 4228
4230 4229 static void
4231 4230 di_cache_print(di_cache_debug_t msglevel, char *fmt, ...)
4232 4231 {
4233 4232 va_list ap;
4234 4233
4235 4234 if (di_cache_debug <= DI_QUIET)
4236 4235 return;
4237 4236
4238 4237 if (di_cache_debug < msglevel)
4239 4238 return;
4240 4239
4241 4240 switch (msglevel) {
4242 4241 case DI_ERR:
4243 4242 msglevel = CE_WARN;
4244 4243 break;
4245 4244 case DI_INFO:
4246 4245 case DI_TRACE:
4247 4246 default:
4248 4247 msglevel = CE_NOTE;
4249 4248 break;
4250 4249 }
4251 4250
4252 4251 va_start(ap, fmt);
4253 4252 vcmn_err(msglevel, fmt, ap);
4254 4253 va_end(ap);
4255 4254 }
4256 4255
4257 4256 static void
4258 4257 di_hotplug_children(struct di_state *st)
4259 4258 {
4260 4259 di_off_t off;
4261 4260 struct di_hp *hp;
4262 4261 struct i_hp *hp_list_node;
4263 4262
4264 4263 while (hp_list_node = (struct i_hp *)list_remove_head(&st->hp_list)) {
4265 4264
4266 4265 if ((hp_list_node->hp_child != NULL) &&
4267 4266 (di_dip_find(st, hp_list_node->hp_child, &off) == 0)) {
4268 4267 hp = DI_HP(di_mem_addr(st, hp_list_node->hp_off));
4269 4268 hp->hp_child = off;
4270 4269 }
4271 4270
4272 4271 kmem_free(hp_list_node, sizeof (i_hp_t));
4273 4272 }
4274 4273
4275 4274 list_destroy(&st->hp_list);
4276 4275 }
↓ open down ↓ |
3879 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX