xref: /titanic_41/usr/src/cmd/mdb/common/modules/genunix/devinfo.c (revision b6c3f7863936abeae522e48a13887dddeb691a45)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/sysmacros.h>
30 #include <sys/dditypes.h>
31 #include <sys/ddi_impldefs.h>
32 #include <sys/ddifm.h>
33 #include <sys/ddipropdefs.h>
34 #include <sys/modctl.h>
35 #include <sys/hwconf.h>
36 #include <sys/stat.h>
37 #include <errno.h>
38 #include <sys/sunmdi.h>
39 #include <sys/mdi_impldefs.h>
40 
41 #include <ctype.h>
42 #include <mdb/mdb_modapi.h>
43 #include <mdb/mdb_ks.h>
44 
45 #include "nvpair.h"
46 
47 #define	DEVINFO_TREE_INDENT	4	/* Indent for devs one down in tree */
48 #define	DEVINFO_PROP_INDENT	4	/* Indent for properties */
49 #define	DEVINFO_PROPLIST_INDENT	8	/* Indent for properties lists */
50 
51 
52 /*
53  * Options for prtconf/devinfo dcmd.
54  */
55 #define	DEVINFO_VERBOSE	0x1
56 #define	DEVINFO_PARENT	0x2
57 #define	DEVINFO_CHILD	0x4
58 #define	DEVINFO_ALLBOLD	0x8
59 #define	DEVINFO_SUMMARY	0x10
60 
61 /*
62  * devinfo node state map. Used by devinfo() and devinfo_audit().
63  * Long words are deliberately truncated so that output
64  * fits in 80 column with 64-bit addresses.
65  */
66 static const char *const di_state[] = {
67 	"DS_INVAL",
68 	"DS_PROTO",
69 	"DS_LINKED",
70 	"DS_BOUND",
71 	"DS_INITIA",
72 	"DS_PROBED",
73 	"DS_ATTACH",
74 	"DS_READY",
75 	"?"
76 };
77 
78 #define	DI_STATE_MAX	((sizeof (di_state) / sizeof (char *)) - 1)
79 
80 void
81 prtconf_help(void)
82 {
83 	mdb_printf("Prints the devinfo tree from a given node.\n"
84 	    "Without the address of a \"struct devinfo\" given, "
85 	    "prints from the root;\n"
86 	    "with an address, prints the parents of, "
87 	    "and all children of, that address.\n\n"
88 	    "Switches:\n"
89 	    "  -v   be verbose - print device property lists\n"
90 	    "  -p   only print the ancestors of the given node\n"
91 	    "  -c   only print the children of the given node\n");
92 }
93 
94 void
95 devinfo_help(void)
96 {
97 	mdb_printf("Switches:\n"
98 	    "  -q   be quiet - don't print device property lists\n"
99 	    "  -s   print summary of dev_info structures\n");
100 }
101 
102 
103 /*
104  * Devinfo walker.
105  */
106 
107 typedef struct {
108 	/*
109 	 * The "struct dev_info" must be the first thing in this structure.
110 	 */
111 	struct dev_info din_dev;
112 
113 	/*
114 	 * This is for the benefit of prtconf().
115 	 */
116 	int din_depth;
117 } devinfo_node_t;
118 
119 typedef struct devinfo_parents_walk_data {
120 	devinfo_node_t dip_node;
121 #define	dip_dev dip_node.din_dev
122 #define	dip_depth dip_node.din_depth
123 	struct dev_info *dip_end;
124 
125 	/*
126 	 * The following three elements are for walking the parents of a node:
127 	 * "dip_base_depth" is the depth of the given node from the root.
128 	 *   This starts at 1 (if we're walking devinfo_root), because
129 	 *   it's the size of the dip_parent_{nodes,addresses} arrays,
130 	 *   and has to include the given node.
131 	 * "dip_parent_nodes" is a collection of the parent node structures,
132 	 *   already read in via mdb_vread().  dip_parent_nodes[0] is the
133 	 *   root, dip_parent_nodes[1] is a child of the root, etc.
134 	 * "dip_parent_addresses" holds the vaddrs of all the parent nodes.
135 	 */
136 	int dip_base_depth;
137 	devinfo_node_t *dip_parent_nodes;
138 	uintptr_t *dip_parent_addresses;
139 } devinfo_parents_walk_data_t;
140 
141 int
142 devinfo_parents_walk_init(mdb_walk_state_t *wsp)
143 {
144 	devinfo_parents_walk_data_t *dip;
145 	uintptr_t addr;
146 	uintptr_t devinfo_root;		/* Address of root of devinfo tree */
147 	int i;
148 
149 	if (mdb_readvar(&devinfo_root, "top_devinfo") == -1) {
150 		mdb_warn("failed to read 'top_devinfo'");
151 		return (NULL);
152 	}
153 
154 	if (wsp->walk_addr == NULL)
155 		wsp->walk_addr = devinfo_root;
156 	addr = wsp->walk_addr;
157 
158 	dip = mdb_alloc(sizeof (devinfo_parents_walk_data_t), UM_SLEEP);
159 	wsp->walk_data = dip;
160 
161 	dip->dip_end = (struct dev_info *)wsp->walk_addr;
162 	dip->dip_depth = 0;
163 	dip->dip_base_depth = 1;
164 
165 	do {
166 		if (mdb_vread(&dip->dip_dev, sizeof (dip->dip_dev),
167 		    addr) == -1) {
168 			mdb_warn("failed to read devinfo at %p", addr);
169 			mdb_free(dip, sizeof (devinfo_parents_walk_data_t));
170 			wsp->walk_data = NULL;
171 			return (WALK_ERR);
172 		}
173 		addr = (uintptr_t)dip->dip_dev.devi_parent;
174 		if (addr != 0)
175 			dip->dip_base_depth++;
176 	} while (addr != 0);
177 
178 	addr = wsp->walk_addr;
179 
180 	dip->dip_parent_nodes = mdb_alloc(
181 	    dip->dip_base_depth * sizeof (devinfo_node_t), UM_SLEEP);
182 	dip->dip_parent_addresses = mdb_alloc(
183 	    dip->dip_base_depth * sizeof (uintptr_t), UM_SLEEP);
184 	for (i = dip->dip_base_depth - 1; i >= 0; i--) {
185 		if (mdb_vread(&dip->dip_parent_nodes[i].din_dev,
186 		    sizeof (struct dev_info), addr) == -1) {
187 			mdb_warn("failed to read devinfo at %p", addr);
188 			return (WALK_ERR);
189 		}
190 		dip->dip_parent_nodes[i].din_depth = i;
191 		dip->dip_parent_addresses[i] = addr;
192 		addr = (uintptr_t)
193 		    dip->dip_parent_nodes[i].din_dev.devi_parent;
194 	}
195 
196 	return (WALK_NEXT);
197 }
198 
199 int
200 devinfo_parents_walk_step(mdb_walk_state_t *wsp)
201 {
202 	devinfo_parents_walk_data_t *dip = wsp->walk_data;
203 	int status;
204 
205 	if (dip->dip_depth == dip->dip_base_depth)
206 		return (WALK_DONE);
207 
208 	status = wsp->walk_callback(
209 	    dip->dip_parent_addresses[dip->dip_depth],
210 	    &dip->dip_parent_nodes[dip->dip_depth],
211 	    wsp->walk_cbdata);
212 
213 	dip->dip_depth++;
214 	return (status);
215 }
216 
217 void
218 devinfo_parents_walk_fini(mdb_walk_state_t *wsp)
219 {
220 	devinfo_parents_walk_data_t *dip = wsp->walk_data;
221 
222 	mdb_free(dip->dip_parent_nodes,
223 	    dip->dip_base_depth * sizeof (devinfo_node_t));
224 	mdb_free(dip->dip_parent_addresses,
225 	    dip->dip_base_depth * sizeof (uintptr_t));
226 	mdb_free(wsp->walk_data, sizeof (devinfo_parents_walk_data_t));
227 }
228 
229 
230 typedef struct devinfo_children_walk_data {
231 	devinfo_node_t dic_node;
232 #define	dic_dev dic_node.din_dev
233 #define	dic_depth dic_node.din_depth
234 	struct dev_info *dic_end;
235 	int dic_print_first_node;
236 } devinfo_children_walk_data_t;
237 
238 int
239 devinfo_children_walk_init(mdb_walk_state_t *wsp)
240 {
241 	devinfo_children_walk_data_t *dic;
242 	uintptr_t devinfo_root;		/* Address of root of devinfo tree */
243 
244 	if (mdb_readvar(&devinfo_root, "top_devinfo") == -1) {
245 		mdb_warn("failed to read 'top_devinfo'");
246 		return (NULL);
247 	}
248 
249 	if (wsp->walk_addr == NULL)
250 		wsp->walk_addr = devinfo_root;
251 
252 	dic = mdb_alloc(sizeof (devinfo_children_walk_data_t), UM_SLEEP);
253 	wsp->walk_data = dic;
254 	dic->dic_end = (struct dev_info *)wsp->walk_addr;
255 
256 	/*
257 	 * This could be set by devinfo_walk_init().
258 	 */
259 	if (wsp->walk_arg != NULL) {
260 		dic->dic_depth = (*(int *)wsp->walk_arg - 1);
261 		dic->dic_print_first_node = 0;
262 	} else {
263 		dic->dic_depth = 0;
264 		dic->dic_print_first_node = 1;
265 	}
266 
267 	return (WALK_NEXT);
268 }
269 
270 int
271 devinfo_children_walk_step(mdb_walk_state_t *wsp)
272 {
273 	devinfo_children_walk_data_t *dic = wsp->walk_data;
274 	struct dev_info *v;
275 	devinfo_node_t *cur;
276 	uintptr_t addr = wsp->walk_addr;
277 	int status = WALK_NEXT;
278 
279 	if (wsp->walk_addr == NULL)
280 		return (WALK_DONE);
281 
282 	if (mdb_vread(&dic->dic_dev, sizeof (dic->dic_dev), addr) == -1) {
283 		mdb_warn("failed to read devinfo at %p", addr);
284 		return (WALK_DONE);
285 	}
286 	cur = &dic->dic_node;
287 
288 	if (dic->dic_print_first_node == 0)
289 		dic->dic_print_first_node = 1;
290 	else
291 		status = wsp->walk_callback(addr, cur, wsp->walk_cbdata);
292 
293 	/*
294 	 * "v" is always a virtual address pointer,
295 	 *  i.e. can't be deref'ed.
296 	 */
297 	v = (struct dev_info *)addr;
298 
299 	if (dic->dic_dev.devi_child != NULL) {
300 		v = dic->dic_dev.devi_child;
301 		dic->dic_depth++;
302 	} else if (dic->dic_dev.devi_sibling != NULL && v != dic->dic_end) {
303 		v = dic->dic_dev.devi_sibling;
304 	} else {
305 		while (v != NULL && v != dic->dic_end &&
306 		    dic->dic_dev.devi_sibling == NULL) {
307 			v = dic->dic_dev.devi_parent;
308 			if (v == NULL)
309 				break;
310 
311 			mdb_vread(&dic->dic_dev,
312 			    sizeof (struct dev_info), (uintptr_t)v);
313 			dic->dic_depth--;
314 		}
315 		if (v != NULL && v != dic->dic_end)
316 			v = dic->dic_dev.devi_sibling;
317 		if (v == dic->dic_end)
318 			v = NULL;	/* Done */
319 	}
320 
321 	wsp->walk_addr = (uintptr_t)v;
322 	return (status);
323 }
324 
325 void
326 devinfo_children_walk_fini(mdb_walk_state_t *wsp)
327 {
328 	mdb_free(wsp->walk_data, sizeof (devinfo_children_walk_data_t));
329 }
330 
331 typedef struct devinfo_walk_data {
332 	mdb_walk_state_t diw_parent, diw_child;
333 	enum { DIW_PARENT, DIW_CHILD, DIW_DONE } diw_mode;
334 } devinfo_walk_data_t;
335 
336 int
337 devinfo_walk_init(mdb_walk_state_t *wsp)
338 {
339 	devinfo_walk_data_t *diw;
340 	devinfo_parents_walk_data_t *dip;
341 
342 	diw = mdb_alloc(sizeof (devinfo_walk_data_t), UM_SLEEP);
343 	diw->diw_parent = *wsp;
344 	diw->diw_child = *wsp;
345 	wsp->walk_data = diw;
346 
347 	diw->diw_mode = DIW_PARENT;
348 
349 	if (devinfo_parents_walk_init(&diw->diw_parent) == -1) {
350 		mdb_free(diw, sizeof (devinfo_walk_data_t));
351 		return (WALK_ERR);
352 	}
353 
354 	/*
355 	 * This is why the "devinfo" walker needs to be marginally
356 	 * complicated - the child walker needs this initialization
357 	 * data, and the best way to get it is out of the parent walker.
358 	 */
359 	dip = diw->diw_parent.walk_data;
360 	diw->diw_child.walk_arg = &dip->dip_base_depth;
361 
362 	if (devinfo_children_walk_init(&diw->diw_child) == -1) {
363 		devinfo_parents_walk_fini(&diw->diw_parent);
364 		mdb_free(diw, sizeof (devinfo_walk_data_t));
365 		return (WALK_ERR);
366 	}
367 
368 	return (WALK_NEXT);
369 }
370 
371 int
372 devinfo_walk_step(mdb_walk_state_t *wsp)
373 {
374 	devinfo_walk_data_t *diw = wsp->walk_data;
375 	int status = WALK_NEXT;
376 
377 	if (diw->diw_mode == DIW_PARENT) {
378 		status = devinfo_parents_walk_step(&diw->diw_parent);
379 		if (status != WALK_NEXT) {
380 			/*
381 			 * Keep on going even if the parents walk hit an error.
382 			 */
383 			diw->diw_mode = DIW_CHILD;
384 			status = WALK_NEXT;
385 		}
386 	} else if (diw->diw_mode == DIW_CHILD) {
387 		status = devinfo_children_walk_step(&diw->diw_child);
388 		if (status != WALK_NEXT) {
389 			diw->diw_mode = DIW_DONE;
390 			status = WALK_DONE;
391 		}
392 	} else
393 		status = WALK_DONE;
394 
395 	return (status);
396 }
397 
398 void
399 devinfo_walk_fini(mdb_walk_state_t *wsp)
400 {
401 	devinfo_walk_data_t *diw = wsp->walk_data;
402 
403 	devinfo_children_walk_fini(&diw->diw_child);
404 	devinfo_parents_walk_fini(&diw->diw_parent);
405 	mdb_free(diw, sizeof (devinfo_walk_data_t));
406 }
407 
408 /*
409  * Given a devinfo pointer, figure out which driver is associated
410  * with the node (by driver name, from the devnames array).
411  */
412 /*ARGSUSED*/
413 int
414 devinfo2driver(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
415 {
416 	char dname[MODMAXNAMELEN + 1];
417 	struct dev_info devi;
418 
419 
420 	if (!(flags & DCMD_ADDRSPEC))
421 		return (DCMD_USAGE);
422 
423 	if (mdb_vread(&devi, sizeof (devi), addr) == -1) {
424 		mdb_warn("failed to read devinfo struct at %p", addr);
425 		return (DCMD_ERR);
426 	}
427 
428 	if (devi.devi_node_state < DS_ATTACHED) {
429 		/* No driver attached to this devinfo - nothing to do. */
430 		mdb_warn("%p: No driver attached to this devinfo node\n", addr);
431 		return (DCMD_ERR);
432 	}
433 
434 	if (mdb_devinfo2driver(addr, dname, sizeof (dname)) != 0) {
435 		mdb_warn("failed to determine driver name");
436 		return (DCMD_ERR);
437 	}
438 
439 	mdb_printf("Driver '%s' is associated with devinfo %p.\n", dname, addr);
440 
441 	return (DCMD_OK);
442 }
443 
444 
445 typedef struct devnames_walk {
446 	struct devnames *dnw_names;
447 	int dnw_ndx;
448 	int dnw_devcnt;
449 	uintptr_t dnw_base;
450 	uintptr_t dnw_size;
451 } devnames_walk_t;
452 
453 int
454 devnames_walk_init(mdb_walk_state_t *wsp)
455 {
456 	devnames_walk_t *dnw;
457 	int devcnt;
458 	uintptr_t devnamesp;
459 
460 	if (wsp->walk_addr != NULL) {
461 		mdb_warn("devnames walker only supports global walks\n");
462 		return (WALK_ERR);
463 	}
464 
465 	if (mdb_readvar(&devcnt, "devcnt") == -1) {
466 		mdb_warn("failed to read 'devcnt'");
467 		return (WALK_ERR);
468 	}
469 
470 	if (mdb_readvar(&devnamesp, "devnamesp") == -1) {
471 		mdb_warn("failed to read 'devnamesp'");
472 		return (WALK_ERR);
473 	}
474 
475 	dnw = mdb_zalloc(sizeof (devnames_walk_t), UM_SLEEP);
476 	dnw->dnw_size = sizeof (struct devnames) * devcnt;
477 	dnw->dnw_devcnt = devcnt;
478 	dnw->dnw_base = devnamesp;
479 	dnw->dnw_names = mdb_alloc(dnw->dnw_size, UM_SLEEP);
480 
481 	if (mdb_vread(dnw->dnw_names, dnw->dnw_size, dnw->dnw_base) == -1) {
482 		mdb_warn("couldn't read devnames array at %p", devnamesp);
483 		return (WALK_ERR);
484 	}
485 
486 	wsp->walk_data = dnw;
487 	return (WALK_NEXT);
488 }
489 
490 int
491 devnames_walk_step(mdb_walk_state_t *wsp)
492 {
493 	devnames_walk_t *dnw = wsp->walk_data;
494 	int status;
495 
496 	if (dnw->dnw_ndx == dnw->dnw_devcnt)
497 		return (WALK_DONE);
498 
499 	status = wsp->walk_callback(dnw->dnw_ndx * sizeof (struct devnames) +
500 	    dnw->dnw_base, &dnw->dnw_names[dnw->dnw_ndx], wsp->walk_cbdata);
501 
502 	dnw->dnw_ndx++;
503 	return (status);
504 }
505 
506 void
507 devnames_walk_fini(mdb_walk_state_t *wsp)
508 {
509 	devnames_walk_t *dnw = wsp->walk_data;
510 
511 	mdb_free(dnw->dnw_names, dnw->dnw_size);
512 	mdb_free(dnw, sizeof (devnames_walk_t));
513 }
514 
515 int
516 devinfo_siblings_walk_init(mdb_walk_state_t *wsp)
517 {
518 	struct dev_info di;
519 	uintptr_t addr = wsp->walk_addr;
520 
521 	if (addr == NULL) {
522 		mdb_warn("a dev_info struct address must be provided\n");
523 		return (WALK_ERR);
524 	}
525 
526 	if (mdb_vread(&di, sizeof (di), addr) == -1) {
527 		mdb_warn("failed to read dev_info struct at %p", addr);
528 		return (WALK_ERR);
529 	}
530 
531 	if (di.devi_parent == NULL) {
532 		mdb_warn("no parent for devinfo at %p", addr);
533 		return (WALK_DONE);
534 	}
535 
536 	if (mdb_vread(&di, sizeof (di), (uintptr_t)di.devi_parent) == -1) {
537 		mdb_warn("failed to read parent dev_info struct at %p",
538 		    (uintptr_t)di.devi_parent);
539 		return (WALK_ERR);
540 	}
541 
542 	wsp->walk_addr = (uintptr_t)di.devi_child;
543 	return (WALK_NEXT);
544 }
545 
546 int
547 devinfo_siblings_walk_step(mdb_walk_state_t *wsp)
548 {
549 	struct dev_info di;
550 	uintptr_t addr = wsp->walk_addr;
551 
552 	if (addr == NULL)
553 		return (WALK_DONE);
554 
555 	if (mdb_vread(&di, sizeof (di), addr) == -1) {
556 		mdb_warn("failed to read dev_info struct at %p", addr);
557 		return (WALK_DONE);
558 	}
559 
560 	wsp->walk_addr = (uintptr_t)di.devi_sibling;
561 	return (wsp->walk_callback(addr, &di, wsp->walk_cbdata));
562 }
563 
564 int
565 devi_next_walk_step(mdb_walk_state_t *wsp)
566 {
567 	struct dev_info di;
568 	int status;
569 
570 	if (wsp->walk_addr == NULL)
571 		return (WALK_DONE);
572 
573 	if (mdb_vread(&di, sizeof (di), wsp->walk_addr) == -1)
574 		return (WALK_DONE);
575 
576 	status = wsp->walk_callback(wsp->walk_addr, &di, wsp->walk_cbdata);
577 	wsp->walk_addr = (uintptr_t)di.devi_next;
578 	return (status);
579 }
580 
581 /*
582  * Helper functions.
583  */
584 
585 static int
586 is_printable_string(unsigned char *prop_value)
587 {
588 	while (*prop_value != 0)
589 		if (!isprint(*prop_value++))
590 			return (0);
591 	return (1);
592 }
593 
594 static void
595 devinfo_print_props_type(int type) {
596 	char *type_str = NULL;
597 
598 	switch (type) {
599 	case DDI_PROP_TYPE_ANY:
600 		type_str = "any";
601 		break;
602 	case DDI_PROP_TYPE_COMPOSITE:
603 		type_str = "composite";
604 		break;
605 	case DDI_PROP_TYPE_INT64:
606 		type_str = "int64";
607 		break;
608 	case DDI_PROP_TYPE_INT:
609 		type_str = "int";
610 		break;
611 	case DDI_PROP_TYPE_BYTE:
612 		type_str = "byte";
613 		break;
614 	case DDI_PROP_TYPE_STRING:
615 		type_str = "string";
616 		break;
617 	}
618 
619 	if (type_str != NULL)
620 		mdb_printf("type=%s", type_str);
621 	else
622 		mdb_printf("type=0x%x", type);
623 }
624 
625 static void
626 devinfo_print_props_value(int elem_size, int nelem,
627 	unsigned char *prop_value, int prop_value_len)
628 {
629 	int i;
630 
631 	mdb_printf("value=");
632 
633 	if (elem_size == 0) {
634 		/* if elem_size == 0, then we are printing out string(s) */
635 		char *p = (char *)prop_value;
636 
637 		for (i = 0; i < nelem - 1; i++) {
638 			mdb_printf("'%s' + ", p);
639 			p += strlen(p) + 1;
640 		}
641 		mdb_printf("'%s'", p);
642 	} else {
643 		/*
644 		 * if elem_size != 0 then we are printing out an array
645 		 * where each element is of elem_size
646 		 */
647 		mdb_nhconvert(prop_value, prop_value, elem_size);
648 		mdb_printf("%02x", *prop_value);
649 		for (i = 1; i < prop_value_len; i++) {
650 			if ((i % elem_size) == 0) {
651 				mdb_nhconvert(&prop_value[i],
652 				    &prop_value[i], elem_size);
653 				mdb_printf(".");
654 			}
655 
656 			mdb_printf("%02x", prop_value[i]);
657 		}
658 	}
659 }
660 
661 /*
662  * devinfo_print_props_guess()
663  * Guesses how to interpret the value of the property
664  *
665  * Params:
666  * 	type      - Should be the type value of the property
667  * 	prop_val  - Pointer to the property value data buffer
668  * 	prop_len  - Length of the property value data buffer
669  *
670  * Return values:
671  * 	nelem     - The number of elements stored in the property value
672  * 			data buffer pointed to by prop_val.
673  * 	elem_size - The size (in bytes) of the elements stored in the property
674  * 			value data buffer pointed to by prop_val.
675  * 			Upon return if elem_size == 0 and nelem != 0 then
676  * 			the property value data buffer contains strings
677  * 	len_err   - There was an error with the length of the data buffer.
678  * 			Its size is not a multiple of the array value type.
679  * 			It will be interpreted as an array of bytes.
680  */
681 static void
682 devinfo_print_props_guess(int type, unsigned char *prop_val, int prop_len,
683     int *elem_size, int *nelem, int *len_err)
684 {
685 	*len_err = 0;
686 	if (prop_len == NULL) {
687 		*elem_size = 0;
688 		*nelem = 0;
689 		return;
690 	}
691 
692 	/* by default, assume an array of bytes */
693 	*elem_size = 1;
694 	*nelem = prop_len;
695 
696 	switch (type) {
697 	case DDI_PROP_TYPE_BYTE:
698 		/* default case, that was easy */
699 		break;
700 	case DDI_PROP_TYPE_INT64:
701 		if ((prop_len % sizeof (int64_t)) == 0) {
702 			*elem_size = sizeof (int64_t);
703 			*nelem = prop_len / *elem_size;
704 		} else {
705 			/* array is not a multiple of type size, error */
706 			*len_err = 1;
707 		}
708 		break;
709 	case DDI_PROP_TYPE_INT:
710 		if ((prop_len % sizeof (int)) == 0) {
711 			*elem_size = sizeof (int);
712 			*nelem = prop_len / *elem_size;
713 		} else {
714 			/* array is not a multiple of type size, error */
715 			*len_err = 1;
716 		}
717 		break;
718 	case DDI_PROP_TYPE_STRING:
719 	case DDI_PROP_TYPE_COMPOSITE:
720 	case DDI_PROP_TYPE_ANY:
721 	default:
722 		/*
723 		 * if we made it here the type is either unknown
724 		 * or a string.  Try to interpret is as a string
725 		 * and if that fails assume an array of bytes.
726 		 */
727 		if (prop_val[prop_len - 1] == '\0') {
728 			unsigned char	*s = prop_val;
729 			int		i;
730 
731 			/* assume an array of strings */
732 			*elem_size = 0;
733 			*nelem = 0;
734 
735 			for (i = 0; i < prop_len; i++) {
736 				if (prop_val[i] != '\0')
737 					continue;
738 
739 				/*
740 				 * If the property is typed as a string
741 				 * property, then interpret empty strings
742 				 * as strings. Otherwise default to an
743 				 * array of bytes. If there are unprintable
744 				 * characters, always default to an array of
745 				 * bytes.
746 				 */
747 				if ((*s == '\0' && type !=
748 				    DDI_PROP_TYPE_STRING) ||
749 				    !is_printable_string(s)) {
750 					*elem_size = 1;
751 					*nelem = prop_len;
752 					break;
753 				}
754 
755 				(*nelem)++;
756 				s = &prop_val[i + 1];
757 			}
758 		}
759 		break;
760 	}
761 }
762 
763 static void
764 devinfo_print_props(char *name, ddi_prop_t *p)
765 {
766 	if (p == NULL)
767 		return;
768 
769 	if (name != NULL)
770 		mdb_printf("%s ", name);
771 
772 	mdb_printf("properties at %p:\n", p);
773 	mdb_inc_indent(DEVINFO_PROP_INDENT);
774 
775 	while (p != NULL) {
776 		ddi_prop_t	prop;
777 		char		prop_name[128];
778 		unsigned char	*prop_value;
779 		int		type, elem_size, nelem, prop_len_error;
780 
781 		/* read in the property struct */
782 		if (mdb_vread(&prop, sizeof (prop), (uintptr_t)p) == -1) {
783 			mdb_warn("could not read property at 0x%p", p);
784 			break;
785 		}
786 
787 		/* print the property name */
788 		if (mdb_readstr(prop_name, sizeof (prop_name),
789 		    (uintptr_t)prop.prop_name) == -1) {
790 			mdb_warn("could not read property name at 0x%p",
791 			    prop.prop_name);
792 			goto next;
793 		}
794 		mdb_printf("name='%s' ", prop_name);
795 
796 		/* get the property type and print it out */
797 		type = (prop.prop_flags & DDI_PROP_TYPE_MASK);
798 		devinfo_print_props_type(type);
799 
800 		/* get the property value */
801 		if (prop.prop_len > 0) {
802 			prop_value = mdb_alloc(prop.prop_len, UM_SLEEP|UM_GC);
803 			if (mdb_vread(prop_value, prop.prop_len,
804 			    (uintptr_t)prop.prop_val) == -1) {
805 				mdb_warn("could not read property value at "
806 				    "0x%p", prop.prop_val);
807 				goto next;
808 			}
809 		} else {
810 			prop_value = NULL;
811 		}
812 
813 		/* take a guess at interpreting the property value */
814 		devinfo_print_props_guess(type, prop_value, prop.prop_len,
815 		    &elem_size, &nelem, &prop_len_error);
816 
817 		/* print out the number ot items */
818 		mdb_printf(" items=%d", nelem);
819 
820 		/* print out any associated device information */
821 		if (prop.prop_dev != DDI_DEV_T_NONE) {
822 			mdb_printf(" dev=");
823 			if (prop.prop_dev == DDI_DEV_T_ANY)
824 				mdb_printf("any");
825 			else if (prop.prop_dev == DDI_MAJOR_T_UNKNOWN)
826 				mdb_printf("unknown");
827 			else
828 				mdb_printf("(%u,%u)",
829 				    getmajor(prop.prop_dev),
830 				    getminor(prop.prop_dev));
831 		}
832 
833 		/* print out the property value */
834 		if (prop_value != NULL) {
835 			mdb_printf("\n");
836 			mdb_inc_indent(DEVINFO_PROP_INDENT);
837 			if (prop_len_error)
838 				mdb_printf("NOTE: prop length is not a "
839 				    "multiple of element size\n");
840 			devinfo_print_props_value(elem_size, nelem,
841 			    prop_value, prop.prop_len);
842 			mdb_dec_indent(DEVINFO_PROP_INDENT);
843 		}
844 
845 next:
846 		mdb_printf("\n");
847 		p = prop.prop_next;
848 	}
849 
850 	mdb_dec_indent(DEVINFO_PROP_INDENT);
851 }
852 
853 static void
854 devinfo_pathinfo_state(mdi_pathinfo_state_t state) {
855 	char *type_str = NULL;
856 
857 	switch (state) {
858 	case MDI_PATHINFO_STATE_INIT:
859 		type_str = "init";
860 		break;
861 	case MDI_PATHINFO_STATE_ONLINE:
862 		type_str = "online";
863 		break;
864 	case MDI_PATHINFO_STATE_STANDBY:
865 		type_str = "standby";
866 		break;
867 	case MDI_PATHINFO_STATE_FAULT:
868 		type_str = "fault";
869 		break;
870 	case MDI_PATHINFO_STATE_OFFLINE:
871 		type_str = "offline";
872 		break;
873 	}
874 	if (type_str != NULL)
875 		mdb_printf("state=%s\n", type_str);
876 	else
877 		mdb_printf("state=0x%x\n", state);
878 }
879 
880 static void
881 devinfo_print_pathing(int mdi_component, void *mdi_client) {
882 	mdi_client_t		mdi_c;
883 	struct mdi_pathinfo	*pip;
884 
885 	/* we only print out multipathing info for client nodes */
886 	if ((mdi_component & MDI_COMPONENT_CLIENT) == 0)
887 		return;
888 
889 	mdb_printf("Client multipath info at: 0x%p\n", mdi_client);
890 	mdb_inc_indent(DEVINFO_PROP_INDENT);
891 
892 	/* read in the client multipathing info */
893 	if (mdb_readstr((void*) &mdi_c, sizeof (mdi_c),
894 	    (uintptr_t)mdi_client) == -1) {
895 		mdb_warn("failed to read mdi_client at %p",
896 		    (uintptr_t)mdi_client);
897 		goto exit;
898 	}
899 
900 	/*
901 	 * walk through the clients list of pathinfo structures and print
902 	 * out the properties for each path
903 	 */
904 	pip = (struct mdi_pathinfo *)mdi_c.ct_path_head;
905 	while (pip != NULL) {
906 		char			binding_name[128];
907 		struct mdi_pathinfo	pi;
908 		mdi_phci_t		ph;
909 		struct dev_info		ph_di;
910 
911 		/* read in the pathinfo structure */
912 		if (mdb_vread((void*)&pi, sizeof (pi),
913 			    (uintptr_t)pip) == -1) {
914 			mdb_warn("failed to read mdi_pathinfo at %p",
915 			    (uintptr_t)pip);
916 			goto exit;
917 		}
918 
919 		/* read in the pchi (path host adapter) info */
920 		if (mdb_vread((void*)&ph, sizeof (ph),
921 			    (uintptr_t)pi.pi_phci) == -1) {
922 			mdb_warn("failed to read mdi_pchi at %p",
923 			    (uintptr_t)pi.pi_phci);
924 			goto exit;
925 		}
926 
927 		/* read in the dip of the phci so we can get it's name */
928 		if (mdb_vread((void*)&ph_di, sizeof (ph_di),
929 			    (uintptr_t)ph.ph_dip) == -1) {
930 			mdb_warn("failed to read mdi_pchi at %p",
931 			    (uintptr_t)ph.ph_dip);
932 			goto exit;
933 		}
934 		if (mdb_vread(binding_name, sizeof (binding_name),
935 			    (uintptr_t)ph_di.devi_binding_name) == -1) {
936 			mdb_warn("failed to read binding_name at %p",
937 			    (uintptr_t)ph_di.devi_binding_name);
938 			goto exit;
939 		}
940 
941 		mdb_printf("%s#%d, ", binding_name, ph_di.devi_instance);
942 		devinfo_pathinfo_state(pi.pi_state);
943 
944 		/* print out the pathing info */
945 		mdb_inc_indent(DEVINFO_PROP_INDENT);
946 		if (mdb_pwalk_dcmd(NVPAIR_WALKER_FQNAME, NVPAIR_DCMD_FQNAME,
947 			    0, NULL, (uintptr_t)pi.pi_prop) != 0) {
948 			mdb_dec_indent(DEVINFO_PROP_INDENT);
949 			goto exit;
950 		}
951 		mdb_dec_indent(DEVINFO_PROP_INDENT);
952 		pip = pi.pi_client_link;
953 	}
954 
955 exit:
956 	mdb_dec_indent(DEVINFO_PROP_INDENT);
957 }
958 
959 typedef struct devinfo_cb_data {
960 	uintptr_t	di_base;
961 	uint_t		di_flags;
962 } devinfo_cb_data_t;
963 
964 static int
965 devinfo_print(uintptr_t addr, struct dev_info *dev, devinfo_cb_data_t *data)
966 {
967 	/*
968 	 * We know the walker passes us extra data after the dev_info.
969 	 */
970 	char		binding_name[128];
971 	char		dname[MODMAXNAMELEN + 1];
972 	devinfo_node_t	*din = (devinfo_node_t *)dev;
973 	ddi_prop_t	*global_props = NULL;
974 
975 	if (mdb_readstr(binding_name, sizeof (binding_name),
976 	    (uintptr_t)dev->devi_binding_name) == -1) {
977 		mdb_warn("failed to read binding_name at %p",
978 		    (uintptr_t)dev->devi_binding_name);
979 		return (WALK_ERR);
980 	}
981 
982 	/* if there are any global properties, get a pointer to them */
983 	if (dev->devi_global_prop_list != NULL) {
984 		ddi_prop_list_t	plist;
985 		if (mdb_vread((void*)&plist, sizeof (plist),
986 		    (uintptr_t)dev->devi_global_prop_list) == -1) {
987 			mdb_warn("failed to read global prop_list at %p",
988 			    (uintptr_t)dev->devi_global_prop_list);
989 			return (WALK_ERR);
990 		}
991 		global_props = plist.prop_list;
992 	}
993 
994 	mdb_inc_indent(din->din_depth * DEVINFO_TREE_INDENT);
995 	if ((addr == data->di_base) || (data->di_flags & DEVINFO_ALLBOLD))
996 		mdb_printf("%<b>");
997 	mdb_printf("%-0?p %s", addr, binding_name);
998 	if ((addr == data->di_base) || (data->di_flags & DEVINFO_ALLBOLD))
999 		mdb_printf("%</b>");
1000 	if (dev->devi_instance >= 0)
1001 		mdb_printf(", instance #%d", dev->devi_instance);
1002 
1003 	if (dev->devi_node_state < DS_ATTACHED)
1004 		mdb_printf(" (driver not attached)");
1005 	else if (mdb_devinfo2driver(addr, dname, sizeof (dname)) != 0)
1006 		mdb_printf(" (could not determine driver name)");
1007 	else
1008 		mdb_printf(" (driver name: %s)", dname);
1009 
1010 	mdb_printf("\n");
1011 	if (data->di_flags & DEVINFO_VERBOSE) {
1012 		mdb_inc_indent(DEVINFO_PROPLIST_INDENT);
1013 		devinfo_print_props("System", dev->devi_sys_prop_ptr);
1014 		devinfo_print_props("Driver", dev->devi_drv_prop_ptr);
1015 		devinfo_print_props("Hardware", dev->devi_hw_prop_ptr);
1016 		devinfo_print_props("Global", global_props);
1017 
1018 		devinfo_print_pathing(dev->devi_mdi_component,
1019 		    dev->devi_mdi_client);
1020 
1021 		mdb_dec_indent(DEVINFO_PROPLIST_INDENT);
1022 	}
1023 
1024 	mdb_dec_indent(din->din_depth * DEVINFO_TREE_INDENT);
1025 	return (WALK_NEXT);
1026 }
1027 
1028 /*ARGSUSED*/
1029 int
1030 prtconf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1031 {
1032 	devinfo_cb_data_t data;
1033 	uintptr_t devinfo_root;		/* Address of root of devinfo tree */
1034 	int status;
1035 
1036 	data.di_flags = DEVINFO_PARENT | DEVINFO_CHILD;
1037 
1038 	if (mdb_getopts(argc, argv,
1039 	    'v', MDB_OPT_SETBITS, DEVINFO_VERBOSE, &data.di_flags,
1040 	    'p', MDB_OPT_CLRBITS, DEVINFO_CHILD, &data.di_flags,
1041 	    'c', MDB_OPT_CLRBITS, DEVINFO_PARENT, &data.di_flags, NULL) != argc)
1042 		return (DCMD_USAGE);
1043 
1044 	if (mdb_readvar(&devinfo_root, "top_devinfo") == -1) {
1045 		mdb_warn("failed to read 'top_devinfo'");
1046 		return (NULL);
1047 	}
1048 
1049 	if ((flags & DCMD_ADDRSPEC) == 0) {
1050 		addr = devinfo_root;
1051 		if (data.di_flags & DEVINFO_VERBOSE)
1052 			data.di_flags |= DEVINFO_ALLBOLD;
1053 	}
1054 
1055 	data.di_base = addr;
1056 	mdb_printf("%<u>%-?s %-50s%</u>\n", "DEVINFO", "NAME");
1057 
1058 	if ((data.di_flags & (DEVINFO_PARENT | DEVINFO_CHILD)) ==
1059 	    (DEVINFO_PARENT | DEVINFO_CHILD)) {
1060 		status = mdb_pwalk("devinfo",
1061 		    (mdb_walk_cb_t)devinfo_print, &data, addr);
1062 	} else if (data.di_flags & DEVINFO_PARENT) {
1063 		status = mdb_pwalk("devinfo_parents",
1064 		    (mdb_walk_cb_t)devinfo_print, &data, addr);
1065 	} else if (data.di_flags & DEVINFO_CHILD) {
1066 		status = mdb_pwalk("devinfo_children",
1067 		    (mdb_walk_cb_t)devinfo_print, &data, addr);
1068 	} else {
1069 		devinfo_node_t din;
1070 		if (mdb_vread(&din.din_dev, sizeof (din.din_dev), addr) == -1) {
1071 			mdb_warn("failed to read device");
1072 			return (DCMD_ERR);
1073 		}
1074 		din.din_depth = 0;
1075 		return (devinfo_print(addr, (struct dev_info *)&din, &data));
1076 	}
1077 
1078 	if (status == -1) {
1079 		mdb_warn("couldn't walk devinfo tree");
1080 		return (DCMD_ERR);
1081 	}
1082 
1083 	return (DCMD_OK);
1084 }
1085 
1086 /*ARGSUSED*/
1087 int
1088 devinfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1089 {
1090 	char tmpstr[MODMAXNAMELEN];
1091 	char nodename[MODMAXNAMELEN];
1092 	char bindname[MAXPATHLEN];
1093 	int size, length;
1094 	struct dev_info devi;
1095 	devinfo_node_t din;
1096 	devinfo_cb_data_t data;
1097 
1098 	static const mdb_bitmask_t devi_state_masks[] = {
1099 	    { "DEVICE_OFFLINE",	DEVI_DEVICE_OFFLINE,	DEVI_DEVICE_OFFLINE },
1100 	    { "DEVICE_DOWN",	DEVI_DEVICE_DOWN,	DEVI_DEVICE_DOWN },
1101 	    { "DEVICE_DEGRADED", DEVI_DEVICE_DEGRADED,	DEVI_DEVICE_DEGRADED },
1102 	    { "DEVICE_REMOVED", DEVI_DEVICE_REMOVED,	DEVI_DEVICE_REMOVED },
1103 	    { "BUS_QUIESCED",	DEVI_BUS_QUIESCED,	DEVI_BUS_QUIESCED },
1104 	    { "BUS_DOWN",	DEVI_BUS_DOWN,		DEVI_BUS_DOWN },
1105 	    { "NDI_CONFIG",	DEVI_NDI_CONFIG,	DEVI_NDI_CONFIG	},
1106 
1107 	    { "S_ATTACHING",	DEVI_S_ATTACHING,	DEVI_S_ATTACHING },
1108 	    { "S_DETACHING",	DEVI_S_DETACHING,	DEVI_S_DETACHING },
1109 	    { "S_ONLINING",	DEVI_S_ONLINING,	DEVI_S_ONLINING },
1110 	    { "S_OFFLINING",	DEVI_S_OFFLINING,	DEVI_S_OFFLINING },
1111 	    { "S_INVOKING_DACF", DEVI_S_INVOKING_DACF,	DEVI_S_INVOKING_DACF },
1112 	    { "S_UNBOUND",	DEVI_S_UNBOUND,		DEVI_S_UNBOUND },
1113 	    { "S_MD_UPDATE",	DEVI_S_MD_UPDATE,	DEVI_S_MD_UPDATE },
1114 	    { "S_REPORT",	DEVI_S_REPORT,		DEVI_S_REPORT },
1115 	    { "S_EVADD",	DEVI_S_EVADD,		DEVI_S_EVADD },
1116 	    { "S_EVREMOVE",	DEVI_S_EVREMOVE,	DEVI_S_EVREMOVE },
1117 	    { "S_NEED_RESET",	DEVI_S_NEED_RESET,	DEVI_S_NEED_RESET },
1118 	    { NULL,		0,			0 }
1119 	};
1120 
1121 	static const mdb_bitmask_t devi_flags_masks[] = {
1122 	    { "BUSY",		DEVI_BUSY,		DEVI_BUSY },
1123 	    { "MADE_CHILDREN",	DEVI_MADE_CHILDREN,	DEVI_MADE_CHILDREN },
1124 	    { "ATTACHED_CHILDREN",
1125 				DEVI_ATTACHED_CHILDREN,	DEVI_ATTACHED_CHILDREN},
1126 	    { "BRANCH_HELD",	DEVI_BRANCH_HELD,	DEVI_BRANCH_HELD },
1127 	    { "NO_BIND",	DEVI_NO_BIND,		DEVI_NO_BIND },
1128 	    { "DEVI_REGISTERED_DEVID",
1129 				DEVI_REGISTERED_DEVID,	DEVI_REGISTERED_DEVID },
1130 	    { "PHCI_SIGNALS_VHCI",
1131 				DEVI_PHCI_SIGNALS_VHCI,
1132 				DEVI_PHCI_SIGNALS_VHCI },
1133 	    { "REBIND",		DEVI_REBIND,		DEVI_REBIND },
1134 	    { NULL,		0,			0 }
1135 	};
1136 
1137 	data.di_flags = DEVINFO_VERBOSE;
1138 	data.di_base = addr;
1139 
1140 	if (mdb_getopts(argc, argv,
1141 	    'q', MDB_OPT_CLRBITS, DEVINFO_VERBOSE, &data.di_flags,
1142 	    's', MDB_OPT_SETBITS, DEVINFO_SUMMARY, &data.di_flags, NULL)
1143 	    != argc)
1144 		return (DCMD_USAGE);
1145 
1146 	if ((flags & DCMD_ADDRSPEC) == 0) {
1147 		mdb_warn(
1148 		    "devinfo doesn't give global information (try prtconf)\n");
1149 		return (DCMD_ERR);
1150 	}
1151 
1152 	if (DCMD_HDRSPEC(flags) && data.di_flags & DEVINFO_SUMMARY)
1153 		mdb_printf(
1154 		    "%-?s %5s %?s %-20s %-s\n"
1155 		    "%-?s %5s %?s %-20s %-s\n"
1156 		    "%<u>%-?s %5s %?s %-20s %-15s%</u>\n",
1157 		    "DEVINFO", "MAJ",  "REFCNT",   "NODENAME", "NODESTATE",
1158 		    "",        "INST", "CIRCULAR", "BINDNAME", "STATE",
1159 		    "",        "",     "THREAD",   "",         "FLAGS");
1160 
1161 	if (mdb_vread(&devi, sizeof (devi), addr) == -1) {
1162 		mdb_warn("failed to read device");
1163 		return (DCMD_ERR);
1164 	}
1165 
1166 	if (data.di_flags & DEVINFO_SUMMARY) {
1167 		*nodename = '\0';
1168 		size = sizeof (nodename);
1169 
1170 		if ((length = mdb_readstr(tmpstr, size,
1171 		    (uintptr_t)devi.devi_node_name)) > 0) {
1172 			strcat(nodename, tmpstr);
1173 			size -= length;
1174 		}
1175 
1176 		if (devi.devi_addr != NULL && mdb_readstr(tmpstr, size - 1,
1177 		    (uintptr_t)devi.devi_addr) > 0) {
1178 			strcat(nodename, "@");
1179 			strcat(nodename, tmpstr);
1180 		}
1181 
1182 		if (mdb_readstr(bindname, sizeof (bindname),
1183 		    (uintptr_t)devi.devi_binding_name) == -1)
1184 			*bindname = '\0';
1185 
1186 		mdb_printf("%0?p %5d %?d %-20s %s\n",
1187 		    addr, devi.devi_major, devi.devi_ref, nodename,
1188 		    di_state[MIN(devi.devi_node_state + 1, DI_STATE_MAX)]);
1189 		mdb_printf("%?s %5d %?d %-20s <%b>\n",
1190 		    "", devi.devi_instance, devi.devi_circular, bindname,
1191 		    devi.devi_state, devi_state_masks);
1192 		mdb_printf("%?s %5s %?p %-20s <%b>\n\n",
1193 		    "", "", devi.devi_busy_thread, "",
1194 		    devi.devi_flags, devi_flags_masks);
1195 
1196 		return (DCMD_OK);
1197 	} else {
1198 		din.din_dev = devi;
1199 		din.din_depth = 0;
1200 		return (devinfo_print(addr, (struct dev_info *)&din, &data));
1201 	}
1202 }
1203 
1204 /*ARGSUSED*/
1205 int
1206 m2d_walk_dinfo(uintptr_t addr, struct dev_info *di, char *mod_name)
1207 {
1208 	char name[MODMAXNAMELEN];
1209 
1210 	if (mdb_readstr(name, MODMAXNAMELEN,
1211 	    (uintptr_t)di->devi_binding_name) == -1) {
1212 		mdb_warn("couldn't read devi_binding_name at %p",
1213 		    di->devi_binding_name);
1214 		return (WALK_ERR);
1215 	}
1216 
1217 	if (strcmp(name, mod_name) == 0)
1218 		mdb_printf("%p\n", addr);
1219 
1220 	return (WALK_NEXT);
1221 }
1222 
1223 /*ARGSUSED*/
1224 int
1225 modctl2devinfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1226 {
1227 	struct modctl modctl;
1228 	char name[MODMAXNAMELEN];
1229 
1230 	if (!(flags & DCMD_ADDRSPEC))
1231 		return (DCMD_USAGE);
1232 
1233 	if (mdb_vread(&modctl, sizeof (modctl), addr) == -1) {
1234 		mdb_warn("couldn't read modctl at %p", addr);
1235 		return (DCMD_ERR);
1236 	}
1237 
1238 	if (mdb_readstr(name, MODMAXNAMELEN,
1239 	    (uintptr_t)modctl.mod_modname) == -1) {
1240 		mdb_warn("couldn't read modname at %p", modctl.mod_modname);
1241 		return (DCMD_ERR);
1242 	}
1243 
1244 	if (mdb_walk("devinfo", (mdb_walk_cb_t)m2d_walk_dinfo, name) == -1) {
1245 		mdb_warn("couldn't walk devinfo");
1246 		return (DCMD_ERR);
1247 	}
1248 
1249 	return (DCMD_OK);
1250 }
1251 
1252 static int
1253 major_to_addr(major_t major, uintptr_t *vaddr)
1254 {
1255 	uint_t devcnt;
1256 	uintptr_t devnamesp;
1257 
1258 	if (mdb_readvar(&devcnt, "devcnt") == -1) {
1259 		mdb_warn("failed to read 'devcnt'");
1260 		return (-1);
1261 	}
1262 
1263 	if (mdb_readvar(&devnamesp, "devnamesp") == -1) {
1264 		mdb_warn("failed to read 'devnamesp'");
1265 		return (-1);
1266 	}
1267 
1268 	if (major >= devcnt) {
1269 		mdb_warn("%x is out of range [0x0-0x%x]\n", major, devcnt - 1);
1270 		return (-1);
1271 	}
1272 
1273 	*vaddr = devnamesp + (major * sizeof (struct devnames));
1274 	return (0);
1275 }
1276 
1277 /*ARGSUSED*/
1278 int
1279 devnames(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1280 {
1281 	static const mdb_bitmask_t dn_flag_bits[] = {
1282 		{ "DN_CONF_PARSED",	DN_CONF_PARSED, DN_CONF_PARSED },
1283 		{ "DN_DRIVER_BUSY",	DN_DRIVER_BUSY, DN_DRIVER_BUSY },
1284 		{ "DN_DRIVER_HELD",	DN_DRIVER_HELD, DN_DRIVER_HELD },
1285 		{ "DN_TAKEN_GETUDEV",	DN_TAKEN_GETUDEV, DN_TAKEN_GETUDEV },
1286 		{ "DN_DRIVER_REMOVED",	DN_DRIVER_REMOVED, DN_DRIVER_REMOVED},
1287 		{ "DN_FORCE_ATTACH",	DN_FORCE_ATTACH, DN_FORCE_ATTACH},
1288 		{ "DN_LEAF_DRIVER",	DN_LEAF_DRIVER, DN_LEAF_DRIVER},
1289 		{ "DN_NETWORK_DRIVER",	DN_NETWORK_DRIVER, DN_NETWORK_DRIVER},
1290 		{ "DN_NO_AUTODETACH",	DN_NO_AUTODETACH, DN_NO_AUTODETACH },
1291 		{ "DN_GLDV3_DRIVER",	DN_GLDV3_DRIVER, DN_GLDV3_DRIVER},
1292 		{ "DN_PHCI_DRIVER",	DN_PHCI_DRIVER, DN_PHCI_DRIVER},
1293 		{ NULL, 0, 0 }
1294 	};
1295 
1296 	const mdb_arg_t *argp = NULL;
1297 	uint_t opt_v = FALSE, opt_m = FALSE;
1298 	major_t major;
1299 	size_t i;
1300 
1301 	char name[MODMAXNAMELEN + 1];
1302 	struct devnames dn;
1303 
1304 	if ((i = mdb_getopts(argc, argv,
1305 	    'm', MDB_OPT_SETBITS, TRUE, &opt_m,
1306 	    'v', MDB_OPT_SETBITS, TRUE, &opt_v,
1307 	    NULL)) != argc) {
1308 		if (argc - i > 1)
1309 			return (DCMD_USAGE);
1310 		argp = &argv[i];
1311 	}
1312 
1313 	if (opt_m) {
1314 		if (!(flags & DCMD_ADDRSPEC))
1315 			return (DCMD_USAGE);
1316 
1317 		if (major_to_addr(addr, &addr) == -1)
1318 			return (DCMD_ERR);
1319 
1320 	} else if (!(flags & DCMD_ADDRSPEC)) {
1321 		if (argp == NULL) {
1322 			if (mdb_walk_dcmd("devnames", "devnames", argc, argv)) {
1323 				mdb_warn("failed to walk devnames");
1324 				return (DCMD_ERR);
1325 			}
1326 			return (DCMD_OK);
1327 		}
1328 
1329 		if (argp->a_type == MDB_TYPE_IMMEDIATE)
1330 			major = (major_t)argp->a_un.a_val;
1331 		else
1332 			major = (major_t)mdb_strtoull(argp->a_un.a_str);
1333 
1334 		if (major_to_addr(major, &addr) == -1)
1335 			return (DCMD_ERR);
1336 	}
1337 
1338 	if (mdb_vread(&dn, sizeof (struct devnames), addr) == -1) {
1339 		mdb_warn("failed to read devnames struct at %p", addr);
1340 		return (DCMD_ERR);
1341 	}
1342 
1343 	if (DCMD_HDRSPEC(flags)) {
1344 		if (opt_v)
1345 			mdb_printf("%<u>%-16s%</u>\n", "NAME");
1346 		else
1347 			mdb_printf("%<u>%-16s %-?s%</u>\n", "NAME", "DN_HEAD");
1348 	}
1349 
1350 	if ((flags & DCMD_LOOP) && (dn.dn_name == NULL))
1351 		return (DCMD_OK); /* Skip empty slots if we're printing table */
1352 
1353 	if (mdb_readstr(name, sizeof (name), (uintptr_t)dn.dn_name) == -1)
1354 		(void) mdb_snprintf(name, sizeof (name), "0x%p", dn.dn_name);
1355 
1356 	if (opt_v) {
1357 		ddi_prop_list_t prop_list;
1358 		mdb_printf("%<b>%-16s%</b>\n", name);
1359 		mdb_inc_indent(2);
1360 
1361 		mdb_printf("          flags %b\n", dn.dn_flags, dn_flag_bits);
1362 		mdb_printf("             pl %p\n", (void *)dn.dn_pl);
1363 		mdb_printf("           head %p\n", dn.dn_head);
1364 		mdb_printf("       instance %d\n", dn.dn_instance);
1365 		mdb_printf("         inlist %p\n", dn.dn_inlist);
1366 		mdb_printf("global_prop_ptr %p\n", dn.dn_global_prop_ptr);
1367 		if (mdb_vread(&prop_list, sizeof (ddi_prop_list_t),
1368 		    (uintptr_t)dn.dn_global_prop_ptr) != -1) {
1369 			devinfo_print_props(NULL, prop_list.prop_list);
1370 		}
1371 
1372 		mdb_dec_indent(2);
1373 	} else
1374 		mdb_printf("%-16s %-?p\n", name, dn.dn_head);
1375 
1376 	return (DCMD_OK);
1377 }
1378 
1379 /*ARGSUSED*/
1380 int
1381 name2major(uintptr_t vaddr, uint_t flags, int argc, const mdb_arg_t *argv)
1382 {
1383 	major_t major;
1384 
1385 	if (flags & DCMD_ADDRSPEC)
1386 		return (DCMD_USAGE);
1387 
1388 	if (argc != 1 || argv->a_type != MDB_TYPE_STRING)
1389 		return (DCMD_USAGE);
1390 
1391 	if (mdb_name_to_major(argv->a_un.a_str, &major) != 0) {
1392 		mdb_warn("failed to convert name to major number\n");
1393 		return (DCMD_ERR);
1394 	}
1395 
1396 	mdb_printf("0x%x\n", major);
1397 	return (DCMD_OK);
1398 }
1399 
1400 /*
1401  * Get a numerical argument of a dcmd from addr if an address is specified
1402  * or from argv if no address is specified. Return the argument in ret.
1403  */
1404 static int
1405 getarg(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv,
1406     uintptr_t *ret)
1407 {
1408 	if (argc == 0 && (flags & DCMD_ADDRSPEC)) {
1409 		*ret = addr;
1410 
1411 	} else if (argc == 1 && !(flags & DCMD_ADDRSPEC)) {
1412 		*ret = (argv[0].a_type == MDB_TYPE_IMMEDIATE) ?
1413 		    (uintptr_t)argv[0].a_un.a_val :
1414 		    (uintptr_t)mdb_strtoull(argv->a_un.a_str);
1415 
1416 	} else {
1417 		return (-1);
1418 	}
1419 
1420 	return (0);
1421 }
1422 
1423 /*ARGSUSED*/
1424 int
1425 major2name(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1426 {
1427 	uintptr_t major;
1428 	const char *name;
1429 
1430 	if (getarg(addr, flags, argc, argv, &major) < 0)
1431 		return (DCMD_USAGE);
1432 
1433 	if ((name = mdb_major_to_name((major_t)major)) == NULL) {
1434 		mdb_warn("failed to convert major number to name\n");
1435 		return (DCMD_ERR);
1436 	}
1437 
1438 	mdb_printf("%s\n", name);
1439 	return (DCMD_OK);
1440 }
1441 
1442 /*ARGSUSED*/
1443 int
1444 dev2major(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1445 {
1446 	uintptr_t dev;
1447 
1448 	if (getarg(addr, flags, argc, argv, &dev) < 0)
1449 		return (DCMD_USAGE);
1450 
1451 	if (flags & DCMD_PIPE_OUT)
1452 		mdb_printf("%x\n", getmajor(dev));
1453 	else
1454 		mdb_printf("0x%x (0t%d)\n", getmajor(dev), getmajor(dev));
1455 
1456 	return (DCMD_OK);
1457 }
1458 
1459 /*ARGSUSED*/
1460 int
1461 dev2minor(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1462 {
1463 	uintptr_t dev;
1464 
1465 	if (getarg(addr, flags, argc, argv, &dev) < 0)
1466 		return (DCMD_USAGE);
1467 
1468 	if (flags & DCMD_PIPE_OUT)
1469 		mdb_printf("%x\n", getminor(dev));
1470 	else
1471 		mdb_printf("0x%x (0t%d)\n", getminor(dev), getminor(dev));
1472 
1473 	return (DCMD_OK);
1474 }
1475 
1476 /*ARGSUSED*/
1477 int
1478 devt(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1479 {
1480 	uintptr_t dev;
1481 
1482 	if (getarg(addr, flags, argc, argv, &dev) < 0)
1483 		return (DCMD_USAGE);
1484 
1485 	if (DCMD_HDRSPEC(flags)) {
1486 		mdb_printf("%<u>%10s%</u>  %<u>%10s%</u>\n", "MAJOR",
1487 		    "MINOR");
1488 	}
1489 
1490 	mdb_printf("%10d  %10d\n", getmajor(dev), getminor(dev));
1491 
1492 	return (DCMD_OK);
1493 }
1494 
1495 /*ARGSUSED*/
1496 int
1497 softstate(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1498 {
1499 	uintptr_t statep;
1500 	int instance;
1501 
1502 
1503 	if (argc != 1) {
1504 		return (DCMD_USAGE);
1505 	}
1506 
1507 	if (argv[0].a_type == MDB_TYPE_IMMEDIATE)
1508 		instance = argv[0].a_un.a_val;
1509 	else
1510 		instance = mdb_strtoull(argv->a_un.a_str);
1511 
1512 	if (mdb_get_soft_state_byaddr(addr, instance, &statep, NULL, 0) == -1) {
1513 		if (errno == ENOENT) {
1514 			mdb_warn("instance %d unused\n", instance);
1515 		} else {
1516 			mdb_warn("couldn't determine softstate for "
1517 			    "instance %d", instance);
1518 		}
1519 
1520 		return (DCMD_ERR);
1521 	}
1522 
1523 	mdb_printf("%p\n", statep);
1524 	return (DCMD_OK);
1525 }
1526 
1527 /*
1528  * Walker for all possible pointers to a driver state struct in an
1529  * i_ddi_soft_state instance chain.  Returns all non-NULL pointers.
1530  */
1531 typedef struct soft_state_walk {
1532 	struct i_ddi_soft_state	ssw_ss;	/* Local copy of i_ddi_soft_state */
1533 	void		**ssw_pointers;	/* to driver state structs */
1534 	uint_t		ssw_index;	/* array entry we're using */
1535 } soft_state_walk_t;
1536 
1537 int
1538 soft_state_walk_init(mdb_walk_state_t *wsp)
1539 {
1540 	soft_state_walk_t *sst;
1541 
1542 
1543 	if (wsp->walk_addr == NULL)
1544 		return (WALK_DONE);
1545 
1546 	sst = mdb_zalloc(sizeof (soft_state_walk_t), UM_SLEEP|UM_GC);
1547 	wsp->walk_data = sst;
1548 
1549 
1550 	if (mdb_vread(&(sst->ssw_ss), sizeof (sst->ssw_ss), wsp->walk_addr) !=
1551 	    sizeof (sst->ssw_ss)) {
1552 		mdb_warn("failed to read i_ddi_soft_state at %p",
1553 		    wsp->walk_addr);
1554 		return (WALK_ERR);
1555 	}
1556 
1557 
1558 	/* Read array of pointers to state structs into local storage. */
1559 	sst->ssw_pointers = mdb_alloc((sst->ssw_ss.n_items * sizeof (void *)),
1560 	    UM_SLEEP|UM_GC);
1561 
1562 	if (mdb_vread(sst->ssw_pointers, (sst->ssw_ss.n_items *
1563 	    sizeof (void *)), (uintptr_t)sst->ssw_ss.array) !=
1564 	    (sst->ssw_ss.n_items * sizeof (void *))) {
1565 		mdb_warn("failed to read i_ddi_soft_state at %p",
1566 		    wsp->walk_addr);
1567 		return (WALK_ERR);
1568 	}
1569 
1570 	sst->ssw_index = 0;
1571 
1572 	return (WALK_NEXT);
1573 }
1574 
1575 int
1576 soft_state_walk_step(mdb_walk_state_t *wsp)
1577 {
1578 	soft_state_walk_t *sst = (soft_state_walk_t *)wsp->walk_data;
1579 	int status = WALK_NEXT;
1580 
1581 
1582 	/*
1583 	 * If the entry indexed has a valid pointer to a soft state struct,
1584 	 * invoke caller's callback func.
1585 	 */
1586 	if (sst->ssw_pointers[sst->ssw_index] != NULL) {
1587 		status = wsp->walk_callback(
1588 		    (uintptr_t)(sst->ssw_pointers[sst->ssw_index]), NULL,
1589 		    wsp->walk_cbdata);
1590 	}
1591 
1592 	sst->ssw_index += 1;
1593 
1594 	if (sst->ssw_index == sst->ssw_ss.n_items)
1595 		return (WALK_DONE);
1596 
1597 	return (status);
1598 }
1599 
1600 int
1601 soft_state_all_walk_step(mdb_walk_state_t *wsp)
1602 {
1603 	soft_state_walk_t *sst = (soft_state_walk_t *)wsp->walk_data;
1604 	int status = WALK_NEXT;
1605 
1606 
1607 	status = wsp->walk_callback(
1608 	    (uintptr_t)(sst->ssw_pointers[sst->ssw_index]), NULL,
1609 	    wsp->walk_cbdata);
1610 
1611 	sst->ssw_index += 1;
1612 
1613 	if (sst->ssw_index == sst->ssw_ss.n_items)
1614 		return (WALK_DONE);
1615 
1616 	return (status);
1617 }
1618 
1619 /*ARGSUSED*/
1620 int
1621 devbindings(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1622 {
1623 	const mdb_arg_t *arg;
1624 	struct devnames dn;
1625 	uintptr_t dn_addr;
1626 	major_t major;
1627 
1628 	if (!(flags & DCMD_ADDRSPEC) && argc < 1)
1629 		return (DCMD_USAGE);
1630 
1631 	if (flags & DCMD_ADDRSPEC) {
1632 		/*
1633 		 * If there's an address, then it's a major number
1634 		 */
1635 		major = addr;
1636 	} else {
1637 		/*
1638 		 * We interpret the last argument. Any other arguments are
1639 		 * forwarded to "devinfo"
1640 		 */
1641 		arg = &argv[argc - 1];
1642 		argc--;
1643 
1644 		if (arg->a_type == MDB_TYPE_IMMEDIATE) {
1645 			major = (uintptr_t)arg->a_un.a_val;
1646 
1647 		} else if (arg->a_un.a_str[0] == '-') {
1648 			/* the argument shouldn't be an option */
1649 			return (DCMD_USAGE);
1650 
1651 		} else if (isdigit(arg->a_un.a_str[0])) {
1652 			major = (uintptr_t)mdb_strtoull(arg->a_un.a_str);
1653 
1654 		} else {
1655 			if (mdb_name_to_major(arg->a_un.a_str, &major) != 0) {
1656 				mdb_warn("failed to get major number for %s\n",
1657 				    arg->a_un.a_str);
1658 				return (DCMD_ERR);
1659 			}
1660 		}
1661 	}
1662 
1663 	if (major_to_addr(major, &dn_addr) != 0)
1664 		return (DCMD_ERR);
1665 
1666 	if (mdb_vread(&dn, sizeof (struct devnames), dn_addr) == -1) {
1667 		mdb_warn("couldn't read devnames array at %p", dn_addr);
1668 		return (DCMD_ERR);
1669 	}
1670 
1671 	if (mdb_pwalk_dcmd("devi_next", "devinfo", argc, argv,
1672 	    (uintptr_t)dn.dn_head) != 0) {
1673 		mdb_warn("couldn't walk the devinfo chain at %p", dn.dn_head);
1674 		return (DCMD_ERR);
1675 	}
1676 
1677 	return (DCMD_OK);
1678 }
1679 
1680 /*
1681  * walk binding hashtable (as of of driver names (e.g., mb_hashtab))
1682  */
1683 int
1684 binding_hash_walk_init(mdb_walk_state_t *wsp)
1685 {
1686 	if (wsp->walk_addr == NULL)
1687 		return (WALK_ERR);
1688 
1689 	wsp->walk_data = mdb_alloc(sizeof (void *) * MOD_BIND_HASHSIZE,
1690 	    UM_SLEEP|UM_GC);
1691 	if (mdb_vread(wsp->walk_data, sizeof (void *) * MOD_BIND_HASHSIZE,
1692 	    wsp->walk_addr) == -1) {
1693 		mdb_warn("failed to read mb_hashtab");
1694 		return (WALK_ERR);
1695 	}
1696 
1697 	wsp->walk_arg = 0;	/* index into mb_hashtab array to start */
1698 
1699 	return (WALK_NEXT);
1700 }
1701 
1702 int
1703 binding_hash_walk_step(mdb_walk_state_t *wsp)
1704 {
1705 	int		status;
1706 	uintptr_t	bind_p;
1707 	struct bind	bind;
1708 
1709 
1710 	/*
1711 	 * Walk the singly-linked list of struct bind
1712 	 */
1713 	bind_p = ((uintptr_t *)wsp->walk_data)[(ulong_t)wsp->walk_arg];
1714 	while (bind_p != NULL) {
1715 
1716 		if (mdb_vread(&bind, sizeof (bind), bind_p) == -1) {
1717 			mdb_warn("failed to read bind struct at %p",
1718 			    wsp->walk_addr);
1719 			return (WALK_ERR);
1720 		}
1721 
1722 		if ((status = wsp->walk_callback(bind_p, &bind,
1723 		    wsp->walk_cbdata)) != WALK_NEXT) {
1724 			return (status);
1725 		}
1726 
1727 		bind_p = (uintptr_t)bind.b_next;
1728 	}
1729 
1730 	wsp->walk_arg = (void *)((char *)wsp->walk_arg + 1);
1731 
1732 	if (wsp->walk_arg == (void *)(MOD_BIND_HASHSIZE - 1))
1733 		return (WALK_DONE);
1734 
1735 	return (WALK_NEXT);
1736 }
1737 
1738 /*ARGSUSED*/
1739 int
1740 binding_hash_entry(uintptr_t addr, uint_t flags, int argc,
1741     const mdb_arg_t *argv)
1742 {
1743 	struct bind 	bind;
1744 	/* Arbitrary lengths based on output format below */
1745 	char name[MAXPATHLEN] = "???";
1746 	char bind_name[MAXPATHLEN] = "<null>";
1747 
1748 	if ((flags & DCMD_ADDRSPEC) == NULL)
1749 		return (DCMD_USAGE);
1750 
1751 	/* Allow null addresses to be passed (as from a walker) */
1752 	if (addr == NULL)
1753 		return (DCMD_OK);
1754 
1755 	if (mdb_vread(&bind, sizeof (bind), addr) == -1) {
1756 		mdb_warn("failed to read struct bind at %p", addr);
1757 		return (DCMD_ERR);
1758 	}
1759 
1760 	if (DCMD_HDRSPEC(flags)) {
1761 		mdb_printf("%<u>%?s% %-5s %s%</u>\n",
1762 		    "NEXT", "MAJOR", "NAME(S)");
1763 	}
1764 
1765 	if (mdb_readstr(name, sizeof (name), (uintptr_t)bind.b_name) == -1)
1766 		mdb_warn("failed to read 'name'");
1767 
1768 	/* There may be bind_name, so this may fail */
1769 	if (mdb_readstr(bind_name, sizeof (bind_name),
1770 	    (uintptr_t)bind.b_bind_name) == -1) {
1771 		mdb_printf("%?p %5d %s\n",
1772 		    bind.b_next, bind.b_num, name);
1773 	} else {
1774 		mdb_printf("%?p %5d %s %s\n",
1775 		    bind.b_next, bind.b_num, name, bind_name);
1776 	}
1777 
1778 	return (DCMD_OK);
1779 }
1780 
1781 typedef struct devinfo_audit_log_walk_data {
1782 	devinfo_audit_t dil_buf;	/* buffer of last entry */
1783 	uintptr_t dil_base;		/* starting address of log buffer */
1784 	int dil_max;			/* maximum index */
1785 	int dil_start;			/* starting index */
1786 	int dil_index;			/* current walking index */
1787 } devinfo_audit_log_walk_data_t;
1788 
1789 int
1790 devinfo_audit_log_walk_init(mdb_walk_state_t *wsp)
1791 {
1792 	devinfo_log_header_t header;
1793 	devinfo_audit_log_walk_data_t *dil;
1794 	uintptr_t devinfo_audit_log;
1795 
1796 	/* read in devinfo_log_header structure */
1797 	if (mdb_readvar(&devinfo_audit_log, "devinfo_audit_log") == -1) {
1798 		mdb_warn("failed to read 'devinfo_audit_log'");
1799 		return (WALK_ERR);
1800 	}
1801 
1802 	if (mdb_vread(&header, sizeof (devinfo_log_header_t),
1803 	    devinfo_audit_log) == -1) {
1804 		mdb_warn("couldn't read devinfo_log_header at %p",
1805 		    devinfo_audit_log);
1806 		return (WALK_ERR);
1807 	}
1808 
1809 	dil = mdb_zalloc(sizeof (devinfo_audit_log_walk_data_t), UM_SLEEP);
1810 	wsp->walk_data = dil;
1811 
1812 	dil->dil_start = dil->dil_index = header.dh_curr;
1813 	dil->dil_max = header.dh_max;
1814 	if (dil->dil_start < 0)		/* no log entries */
1815 		return (WALK_DONE);
1816 
1817 	dil->dil_base = devinfo_audit_log +
1818 	    offsetof(devinfo_log_header_t, dh_entry);
1819 	wsp->walk_addr = dil->dil_base +
1820 	    dil->dil_index * sizeof (devinfo_audit_t);
1821 
1822 	return (WALK_NEXT);
1823 }
1824 
1825 int
1826 devinfo_audit_log_walk_step(mdb_walk_state_t *wsp)
1827 {
1828 	uintptr_t addr = wsp->walk_addr;
1829 	devinfo_audit_log_walk_data_t *dil = wsp->walk_data;
1830 	devinfo_audit_t *da = &dil->dil_buf;
1831 	int status = WALK_NEXT;
1832 
1833 	/* read in current entry and invoke callback */
1834 	if (addr == NULL)
1835 		return (WALK_DONE);
1836 
1837 	if (mdb_vread(&dil->dil_buf, sizeof (devinfo_audit_t), addr) == -1) {
1838 		mdb_warn("failed to read devinfo_audit at %p", addr);
1839 		status = WALK_DONE;
1840 	}
1841 	status = wsp->walk_callback(wsp->walk_addr, da, wsp->walk_cbdata);
1842 
1843 	/* step to the previous log entry in time */
1844 	if (--dil->dil_index < 0)
1845 		dil->dil_index += dil->dil_max;
1846 	if (dil->dil_index == dil->dil_start) {
1847 		wsp->walk_addr = NULL;
1848 		return (WALK_DONE);
1849 	}
1850 
1851 	wsp->walk_addr = dil->dil_base +
1852 	    dil->dil_index * sizeof (devinfo_audit_t);
1853 	return (status);
1854 }
1855 
1856 void
1857 devinfo_audit_log_walk_fini(mdb_walk_state_t *wsp)
1858 {
1859 	mdb_free(wsp->walk_data, sizeof (devinfo_audit_log_walk_data_t));
1860 }
1861 
1862 /*
1863  * display devinfo_audit_t stack trace
1864  */
1865 /*ARGSUSED*/
1866 int
1867 devinfo_audit(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1868 {
1869 	uint_t verbose = FALSE;
1870 	devinfo_audit_t da;
1871 	int i, depth;
1872 
1873 	if ((flags & DCMD_ADDRSPEC) == 0)
1874 		return (DCMD_USAGE);
1875 
1876 	if (mdb_getopts(argc, argv,
1877 	    'v', MDB_OPT_SETBITS, TRUE, &verbose, NULL) != argc)
1878 		return (DCMD_USAGE);
1879 
1880 	if (DCMD_HDRSPEC(flags)) {
1881 		mdb_printf(" %-?s %16s %-?s %-?s %5s\n",
1882 		    "AUDIT", "TIMESTAMP", "THREAD", "DEVINFO", "STATE");
1883 	}
1884 
1885 	if (mdb_vread(&da, sizeof (da), addr) == -1) {
1886 		mdb_warn("couldn't read devinfo_audit at %p", addr);
1887 		return (DCMD_ERR);
1888 	}
1889 
1890 	mdb_printf(" %0?p %16llx %0?p %0?p %s\n",
1891 	    addr, da.da_timestamp, da.da_thread, da.da_devinfo,
1892 	    di_state[MIN(da.da_node_state + 1, DI_STATE_MAX)]);
1893 
1894 	if (!verbose)
1895 		return (DCMD_OK);
1896 
1897 	mdb_inc_indent(4);
1898 
1899 	/*
1900 	 * Guard against bogus da_depth in case the devinfo_audit_t
1901 	 * is corrupt or the address does not really refer to a
1902 	 * devinfo_audit_t.
1903 	 */
1904 	depth = MIN(da.da_depth, DDI_STACK_DEPTH);
1905 
1906 	for (i = 0; i < depth; i++)
1907 		mdb_printf("%a\n", da.da_stack[i]);
1908 
1909 	mdb_printf("\n");
1910 	mdb_dec_indent(4);
1911 
1912 	return (DCMD_OK);
1913 }
1914 
1915 int
1916 devinfo_audit_log(uintptr_t addr, uint_t flags, int argc,
1917     const mdb_arg_t *argv)
1918 {
1919 	if (flags & DCMD_ADDRSPEC)
1920 		return (devinfo_audit(addr, flags, argc, argv));
1921 
1922 	(void) mdb_walk_dcmd("devinfo_audit_log", "devinfo_audit", argc, argv);
1923 	return (DCMD_OK);
1924 }
1925 
1926 typedef struct devinfo_audit_node_walk_data {
1927 	devinfo_audit_t dih_buf;	/* buffer of last entry */
1928 	uintptr_t dih_dip;		/* address of dev_info */
1929 	int dih_on_devinfo;		/* devi_audit on dev_info struct */
1930 } devinfo_audit_node_walk_data_t;
1931 
1932 int
1933 devinfo_audit_node_walk_init(mdb_walk_state_t *wsp)
1934 {
1935 	devinfo_audit_node_walk_data_t *dih;
1936 	devinfo_audit_t *da;
1937 	struct dev_info devi;
1938 	uintptr_t addr = wsp->walk_addr;
1939 
1940 	/* read in devinfo structure */
1941 	if (mdb_vread(&devi, sizeof (struct dev_info), addr) == -1) {
1942 		mdb_warn("couldn't read dev_info at %p", addr);
1943 		return (WALK_ERR);
1944 	}
1945 
1946 	dih = mdb_zalloc(sizeof (devinfo_audit_node_walk_data_t), UM_SLEEP);
1947 	wsp->walk_data = dih;
1948 	da = &dih->dih_buf;
1949 
1950 	/* read in devi_audit structure */
1951 	if (mdb_vread(da, sizeof (devinfo_audit_t), (uintptr_t)devi.devi_audit)
1952 	    == -1) {
1953 		mdb_warn("couldn't read devi_audit at %p", devi.devi_audit);
1954 		return (WALK_ERR);
1955 	}
1956 	dih->dih_dip = addr;
1957 	dih->dih_on_devinfo = 1;
1958 	wsp->walk_addr = (uintptr_t)devi.devi_audit;
1959 
1960 	return (WALK_NEXT);
1961 }
1962 
1963 int
1964 devinfo_audit_node_walk_step(mdb_walk_state_t *wsp)
1965 {
1966 	uintptr_t addr;
1967 	devinfo_audit_node_walk_data_t *dih = wsp->walk_data;
1968 	devinfo_audit_t *da = &dih->dih_buf;
1969 
1970 	if (wsp->walk_addr == NULL)
1971 		return (WALK_DONE);
1972 	(void) wsp->walk_callback(wsp->walk_addr, NULL, wsp->walk_cbdata);
1973 
1974 skip:
1975 	/* read in previous entry */
1976 	if ((addr = (uintptr_t)da->da_lastlog) == 0)
1977 		return (WALK_DONE);
1978 
1979 	if (mdb_vread(&dih->dih_buf, sizeof (devinfo_audit_t), addr) == -1) {
1980 		mdb_warn("failed to read devinfo_audit at %p", addr);
1981 		return (WALK_DONE);
1982 	}
1983 
1984 	/* check if last log was over-written */
1985 	if ((uintptr_t)da->da_devinfo != dih->dih_dip)
1986 		return (WALK_DONE);
1987 
1988 	/*
1989 	 * skip the first common log entry, which is a duplicate of
1990 	 * the devi_audit buffer on the dev_info structure
1991 	 */
1992 	if (dih->dih_on_devinfo) {
1993 		dih->dih_on_devinfo = 0;
1994 		goto skip;
1995 	}
1996 	wsp->walk_addr = addr;
1997 
1998 	return (WALK_NEXT);
1999 }
2000 
2001 void
2002 devinfo_audit_node_walk_fini(mdb_walk_state_t *wsp)
2003 {
2004 	mdb_free(wsp->walk_data, sizeof (devinfo_audit_node_walk_data_t));
2005 }
2006 
2007 int
2008 devinfo_audit_node(uintptr_t addr, uint_t flags, int argc,
2009     const mdb_arg_t *argv)
2010 {
2011 	if (!(flags & DCMD_ADDRSPEC))
2012 		return (DCMD_USAGE);
2013 
2014 	(void) mdb_pwalk_dcmd("devinfo_audit_node", "devinfo_audit",
2015 	    argc, argv, addr);
2016 	return (DCMD_OK);
2017 }
2018 
2019 /*
2020  * mdb support for per-devinfo fault management data
2021  */
2022 /*ARGSUSED*/
2023 int
2024 devinfo_fm(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2025 {
2026 	struct dev_info devi;
2027 	struct i_ddi_fmhdl fhdl;
2028 
2029 	if ((flags & DCMD_ADDRSPEC) == 0)
2030 		return (DCMD_USAGE);
2031 
2032 	if (DCMD_HDRSPEC(flags)) {
2033 		mdb_printf("%<u>%-11s IPL CAPS DROP FMCFULL FMCGREW ACCERR "
2034 		    "DMAERR %11s %11s%</u>\n", "ADDR", "DMACACHE", "ACCCACHE");
2035 	}
2036 
2037 	if (mdb_vread(&devi, sizeof (devi), addr) == -1) {
2038 		mdb_warn("failed to read devinfo struct at %p", addr);
2039 		return (DCMD_ERR);
2040 	}
2041 
2042 	if (mdb_vread(&fhdl, sizeof (fhdl), (uintptr_t)devi.devi_fmhdl) == -1) {
2043 		mdb_warn("failed to read devinfo fm struct at %p",
2044 		    (uintptr_t)devi.devi_fmhdl);
2045 		return (DCMD_ERR);
2046 	}
2047 
2048 	mdb_printf("%-11p %3u %c%c%c%c %4llu %7llu %7llu %6llu %6llu %11p "
2049 	    "%11p\n",
2050 	    (uintptr_t)devi.devi_fmhdl, fhdl.fh_ibc,
2051 	    (DDI_FM_EREPORT_CAP(fhdl.fh_cap) ? 'E' : '-'),
2052 	    (DDI_FM_ERRCB_CAP(fhdl.fh_cap) ? 'C' : '-'),
2053 	    (DDI_FM_ACC_ERR_CAP(fhdl.fh_cap) ? 'A' : '-'),
2054 	    (DDI_FM_DMA_ERR_CAP(fhdl.fh_cap) ? 'D' : '-'),
2055 	    fhdl.fh_kstat.fek_erpt_dropped.value.ui64,
2056 	    fhdl.fh_kstat.fek_fmc_full.value.ui64,
2057 	    fhdl.fh_kstat.fek_fmc_grew.value.ui64,
2058 	    fhdl.fh_kstat.fek_acc_err.value.ui64,
2059 	    fhdl.fh_kstat.fek_dma_err.value.ui64,
2060 	    fhdl.fh_dma_cache, fhdl.fh_acc_cache);
2061 
2062 
2063 	return (DCMD_OK);
2064 }
2065 
2066 /*ARGSUSED*/
2067 int
2068 devinfo_fmce(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2069 {
2070 	struct i_ddi_fmc_entry fce;
2071 
2072 	if ((flags & DCMD_ADDRSPEC) == 0)
2073 		return (DCMD_USAGE);
2074 
2075 	if (DCMD_HDRSPEC(flags)) {
2076 		mdb_printf("%<u>%-11s    %11s    %11s%</u>\n", "ADDR",
2077 		    "RESOURCE", "BUS_SPECIFIC");
2078 	}
2079 
2080 	if (mdb_vread(&fce, sizeof (fce), addr) == -1) {
2081 		mdb_warn("failed to read fm cache struct at %p", addr);
2082 		return (DCMD_ERR);
2083 	}
2084 
2085 	mdb_printf("%-11p    %11p    %11p\n",
2086 	    (uintptr_t)addr, fce.fce_resource, fce.fce_bus_specific);
2087 
2088 
2089 	return (DCMD_OK);
2090 }
2091 
2092 int
2093 devinfo_fmc_walk_init(mdb_walk_state_t *wsp)
2094 {
2095 	struct i_ddi_fmc fec;
2096 	struct i_ddi_fmc_entry fe;
2097 
2098 	if (wsp->walk_addr == NULL)
2099 		return (WALK_ERR);
2100 
2101 	if (mdb_vread(&fec, sizeof (fec), wsp->walk_addr) == -1) {
2102 		mdb_warn("failed to read fm cache at %p", wsp->walk_addr);
2103 		return (WALK_ERR);
2104 	}
2105 
2106 	if (fec.fc_active == NULL)
2107 		return (WALK_DONE);
2108 
2109 	if (mdb_vread(&fe, sizeof (fe), (uintptr_t)fec.fc_active) == -1) {
2110 		mdb_warn("failed to read active fm cache list at %p",
2111 		    fec.fc_active);
2112 		return (WALK_ERR);
2113 	}
2114 
2115 	wsp->walk_data = fe.fce_next;
2116 	wsp->walk_addr = (uintptr_t)fe.fce_next;
2117 	return (WALK_NEXT);
2118 }
2119 
2120 int
2121 devinfo_fmc_walk_step(mdb_walk_state_t *wsp)
2122 {
2123 	int status;
2124 	struct i_ddi_fmc_entry fe;
2125 
2126 	if (mdb_vread(&fe, sizeof (fe), wsp->walk_addr) == -1) {
2127 		mdb_warn("failed to read active fm cache entry at %p",
2128 		    wsp->walk_addr);
2129 		return (WALK_DONE);
2130 	}
2131 
2132 	status = wsp->walk_callback(wsp->walk_addr, &fe, wsp->walk_cbdata);
2133 
2134 	if (fe.fce_next == NULL)
2135 		return (WALK_DONE);
2136 
2137 	wsp->walk_addr = (uintptr_t)fe.fce_next;
2138 	return (status);
2139 }
2140 
2141 int
2142 minornode_walk_init(mdb_walk_state_t *wsp)
2143 {
2144 	struct dev_info di;
2145 	uintptr_t addr = wsp->walk_addr;
2146 
2147 	if (addr == NULL) {
2148 		mdb_warn("a dev_info struct address must be provided\n");
2149 		return (WALK_ERR);
2150 	}
2151 
2152 	if (mdb_vread(&di, sizeof (di), wsp->walk_addr) == -1) {
2153 		mdb_warn("failed to read dev_info struct at %p", addr);
2154 		return (WALK_ERR);
2155 	}
2156 
2157 	wsp->walk_addr = (uintptr_t)di.devi_minor;
2158 	return (WALK_NEXT);
2159 }
2160 
2161 int
2162 minornode_walk_step(mdb_walk_state_t *wsp)
2163 {
2164 	struct ddi_minor_data md;
2165 	uintptr_t addr = wsp->walk_addr;
2166 
2167 	if (addr == NULL)
2168 		return (WALK_DONE);
2169 
2170 	if (mdb_vread(&md, sizeof (md), addr) == -1) {
2171 		mdb_warn("failed to read dev_info struct at %p", addr);
2172 		return (WALK_DONE);
2173 	}
2174 
2175 	wsp->walk_addr = (uintptr_t)md.next;
2176 	return (wsp->walk_callback(addr, &md, wsp->walk_cbdata));
2177 }
2178 
2179 static const char *const md_type[] = {
2180 	"DDI_MINOR",
2181 	"DDI_ALIAS",
2182 	"DDI_DEFAULT",
2183 	"DDI_I_PATH",
2184 	"?"
2185 };
2186 
2187 #define	MD_TYPE_MAX	((sizeof (md_type) / sizeof (char *)) - 1)
2188 
2189 /*ARGSUSED*/
2190 static int
2191 print_minornode(uintptr_t addr, const void *arg, void *data)
2192 {
2193 	char name[128];
2194 	char nodetype[128];
2195 	char *spectype;
2196 	struct ddi_minor_data *mdp = (struct ddi_minor_data *)arg;
2197 
2198 	if (mdb_readstr(name, sizeof (name), (uintptr_t)mdp->ddm_name) == -1)
2199 		*name = '\0';
2200 
2201 	if (mdb_readstr(nodetype, sizeof (nodetype),
2202 	    (uintptr_t)mdp->ddm_node_type) == -1)
2203 		*nodetype = '\0';
2204 
2205 	switch (mdp->ddm_spec_type) {
2206 		case S_IFCHR:	spectype = "c";	break;
2207 		case S_IFBLK:	spectype = "b";	break;
2208 		default:	spectype = "?";	break;
2209 	}
2210 
2211 	mdb_printf("%?p %16lx %-4s %-11s %-10s %s\n",
2212 	    addr, mdp->ddm_dev, spectype, md_type[MIN(mdp->type, MD_TYPE_MAX)],
2213 	    name, nodetype);
2214 
2215 	return (WALK_NEXT);
2216 }
2217 
2218 /*ARGSUSED*/
2219 int
2220 minornodes(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2221 {
2222 	if (!(flags & DCMD_ADDRSPEC) || argc != 0)
2223 		return (DCMD_USAGE);
2224 
2225 	if (DCMD_HDRSPEC(flags))
2226 		mdb_printf("%<u>%?s %16s %-4s %-11s %-10s %-16s%</u>\n",
2227 		    "ADDR", "DEV", "SPEC", "TYPE", "NAME", "NODETYPE");
2228 
2229 	if (mdb_pwalk("minornode", print_minornode, NULL, addr) == -1) {
2230 		mdb_warn("can't walk minornode");
2231 		return (DCMD_ERR);
2232 	}
2233 
2234 	return (DCMD_OK);
2235 }
2236