xref: /titanic_52/usr/src/cmd/mdb/common/modules/genunix/devinfo.c (revision 0dc974a9a2e66d676505db23524ebff105fb36a9)
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_REPORT",	DEVI_S_REPORT,		DEVI_S_REPORT },
1114 	    { "S_EVADD",	DEVI_S_EVADD,		DEVI_S_EVADD },
1115 	    { "S_EVREMOVE",	DEVI_S_EVREMOVE,	DEVI_S_EVREMOVE },
1116 	    { "S_NEED_RESET",	DEVI_S_NEED_RESET,	DEVI_S_NEED_RESET },
1117 	    { NULL,		0,			0 }
1118 	};
1119 
1120 	static const mdb_bitmask_t devi_flags_masks[] = {
1121 	    { "BUSY",		DEVI_BUSY,		DEVI_BUSY },
1122 	    { "MADE_CHILDREN",	DEVI_MADE_CHILDREN,	DEVI_MADE_CHILDREN },
1123 	    { "ATTACHED_CHILDREN",
1124 				DEVI_ATTACHED_CHILDREN,	DEVI_ATTACHED_CHILDREN},
1125 	    { "BRANCH_HELD",	DEVI_BRANCH_HELD,	DEVI_BRANCH_HELD },
1126 	    { "NO_BIND",	DEVI_NO_BIND,		DEVI_NO_BIND },
1127 	    { "DEVI_REGISTERED_DEVID",
1128 				DEVI_REGISTERED_DEVID,	DEVI_REGISTERED_DEVID },
1129 	    { "PHCI_SIGNALS_VHCI",
1130 				DEVI_PHCI_SIGNALS_VHCI,
1131 				DEVI_PHCI_SIGNALS_VHCI },
1132 	    { "REBIND",		DEVI_REBIND,		DEVI_REBIND },
1133 	    { NULL,		0,			0 }
1134 	};
1135 
1136 	data.di_flags = DEVINFO_VERBOSE;
1137 	data.di_base = addr;
1138 
1139 	if (mdb_getopts(argc, argv,
1140 	    'q', MDB_OPT_CLRBITS, DEVINFO_VERBOSE, &data.di_flags,
1141 	    's', MDB_OPT_SETBITS, DEVINFO_SUMMARY, &data.di_flags, NULL)
1142 	    != argc)
1143 		return (DCMD_USAGE);
1144 
1145 	if ((flags & DCMD_ADDRSPEC) == 0) {
1146 		mdb_warn(
1147 		    "devinfo doesn't give global information (try prtconf)\n");
1148 		return (DCMD_ERR);
1149 	}
1150 
1151 	if (DCMD_HDRSPEC(flags) && data.di_flags & DEVINFO_SUMMARY)
1152 		mdb_printf(
1153 		    "%-?s %5s %?s %-20s %-s\n"
1154 		    "%-?s %5s %?s %-20s %-s\n"
1155 		    "%<u>%-?s %5s %?s %-20s %-15s%</u>\n",
1156 		    "DEVINFO", "MAJ",  "REFCNT",   "NODENAME", "NODESTATE",
1157 		    "",        "INST", "CIRCULAR", "BINDNAME", "STATE",
1158 		    "",        "",     "THREAD",   "",         "FLAGS");
1159 
1160 	if (mdb_vread(&devi, sizeof (devi), addr) == -1) {
1161 		mdb_warn("failed to read device");
1162 		return (DCMD_ERR);
1163 	}
1164 
1165 	if (data.di_flags & DEVINFO_SUMMARY) {
1166 		*nodename = '\0';
1167 		size = sizeof (nodename);
1168 
1169 		if ((length = mdb_readstr(tmpstr, size,
1170 		    (uintptr_t)devi.devi_node_name)) > 0) {
1171 			strcat(nodename, tmpstr);
1172 			size -= length;
1173 		}
1174 
1175 		if (devi.devi_addr != NULL && mdb_readstr(tmpstr, size - 1,
1176 		    (uintptr_t)devi.devi_addr) > 0) {
1177 			strcat(nodename, "@");
1178 			strcat(nodename, tmpstr);
1179 		}
1180 
1181 		if (mdb_readstr(bindname, sizeof (bindname),
1182 		    (uintptr_t)devi.devi_binding_name) == -1)
1183 			*bindname = '\0';
1184 
1185 		mdb_printf("%0?p %5d %?d %-20s %s\n",
1186 		    addr, devi.devi_major, devi.devi_ref, nodename,
1187 		    di_state[MIN(devi.devi_node_state + 1, DI_STATE_MAX)]);
1188 		mdb_printf("%?s %5d %?d %-20s <%b>\n",
1189 		    "", devi.devi_instance, devi.devi_circular, bindname,
1190 		    devi.devi_state, devi_state_masks);
1191 		mdb_printf("%?s %5s %?p %-20s <%b>\n\n",
1192 		    "", "", devi.devi_busy_thread, "",
1193 		    devi.devi_flags, devi_flags_masks);
1194 
1195 		return (DCMD_OK);
1196 	} else {
1197 		din.din_dev = devi;
1198 		din.din_depth = 0;
1199 		return (devinfo_print(addr, (struct dev_info *)&din, &data));
1200 	}
1201 }
1202 
1203 /*ARGSUSED*/
1204 int
1205 m2d_walk_dinfo(uintptr_t addr, struct dev_info *di, char *mod_name)
1206 {
1207 	char name[MODMAXNAMELEN];
1208 
1209 	if (mdb_readstr(name, MODMAXNAMELEN,
1210 	    (uintptr_t)di->devi_binding_name) == -1) {
1211 		mdb_warn("couldn't read devi_binding_name at %p",
1212 		    di->devi_binding_name);
1213 		return (WALK_ERR);
1214 	}
1215 
1216 	if (strcmp(name, mod_name) == 0)
1217 		mdb_printf("%p\n", addr);
1218 
1219 	return (WALK_NEXT);
1220 }
1221 
1222 /*ARGSUSED*/
1223 int
1224 modctl2devinfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1225 {
1226 	struct modctl modctl;
1227 	char name[MODMAXNAMELEN];
1228 
1229 	if (!(flags & DCMD_ADDRSPEC))
1230 		return (DCMD_USAGE);
1231 
1232 	if (mdb_vread(&modctl, sizeof (modctl), addr) == -1) {
1233 		mdb_warn("couldn't read modctl at %p", addr);
1234 		return (DCMD_ERR);
1235 	}
1236 
1237 	if (mdb_readstr(name, MODMAXNAMELEN,
1238 	    (uintptr_t)modctl.mod_modname) == -1) {
1239 		mdb_warn("couldn't read modname at %p", modctl.mod_modname);
1240 		return (DCMD_ERR);
1241 	}
1242 
1243 	if (mdb_walk("devinfo", (mdb_walk_cb_t)m2d_walk_dinfo, name) == -1) {
1244 		mdb_warn("couldn't walk devinfo");
1245 		return (DCMD_ERR);
1246 	}
1247 
1248 	return (DCMD_OK);
1249 }
1250 
1251 static int
1252 major_to_addr(major_t major, uintptr_t *vaddr)
1253 {
1254 	uint_t devcnt;
1255 	uintptr_t devnamesp;
1256 
1257 	if (mdb_readvar(&devcnt, "devcnt") == -1) {
1258 		mdb_warn("failed to read 'devcnt'");
1259 		return (-1);
1260 	}
1261 
1262 	if (mdb_readvar(&devnamesp, "devnamesp") == -1) {
1263 		mdb_warn("failed to read 'devnamesp'");
1264 		return (-1);
1265 	}
1266 
1267 	if (major >= devcnt) {
1268 		mdb_warn("%x is out of range [0x0-0x%x]\n", major, devcnt - 1);
1269 		return (-1);
1270 	}
1271 
1272 	*vaddr = devnamesp + (major * sizeof (struct devnames));
1273 	return (0);
1274 }
1275 
1276 /*ARGSUSED*/
1277 int
1278 devnames(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1279 {
1280 	static const mdb_bitmask_t dn_flag_bits[] = {
1281 		{ "DN_CONF_PARSED",	DN_CONF_PARSED, DN_CONF_PARSED },
1282 		{ "DN_DRIVER_BUSY",	DN_DRIVER_BUSY, DN_DRIVER_BUSY },
1283 		{ "DN_DRIVER_HELD",	DN_DRIVER_HELD, DN_DRIVER_HELD },
1284 		{ "DN_TAKEN_GETUDEV",	DN_TAKEN_GETUDEV, DN_TAKEN_GETUDEV },
1285 		{ "DN_DRIVER_REMOVED",	DN_DRIVER_REMOVED, DN_DRIVER_REMOVED},
1286 		{ "DN_FORCE_ATTACH",	DN_FORCE_ATTACH, DN_FORCE_ATTACH},
1287 		{ "DN_LEAF_DRIVER",	DN_LEAF_DRIVER, DN_LEAF_DRIVER},
1288 		{ "DN_NETWORK_DRIVER",	DN_NETWORK_DRIVER, DN_NETWORK_DRIVER},
1289 		{ "DN_NO_AUTODETACH",	DN_NO_AUTODETACH, DN_NO_AUTODETACH },
1290 		{ "DN_GLDV3_DRIVER",	DN_GLDV3_DRIVER, DN_GLDV3_DRIVER},
1291 		{ "DN_PHCI_DRIVER",	DN_PHCI_DRIVER, DN_PHCI_DRIVER},
1292 		{ "DN_OPEN_RETURNS_EINTR", \
1293 				DN_OPEN_RETURNS_EINTR, DN_OPEN_RETURNS_EINTR},
1294 		{ "DN_SCSI_SIZE_CLEAN",	DN_SCSI_SIZE_CLEAN, DN_SCSI_SIZE_CLEAN},
1295 		{ NULL, 0, 0 }
1296 	};
1297 
1298 	const mdb_arg_t *argp = NULL;
1299 	uint_t opt_v = FALSE, opt_m = FALSE;
1300 	major_t major;
1301 	size_t i;
1302 
1303 	char name[MODMAXNAMELEN + 1];
1304 	struct devnames dn;
1305 
1306 	if ((i = mdb_getopts(argc, argv,
1307 	    'm', MDB_OPT_SETBITS, TRUE, &opt_m,
1308 	    'v', MDB_OPT_SETBITS, TRUE, &opt_v,
1309 	    NULL)) != argc) {
1310 		if (argc - i > 1)
1311 			return (DCMD_USAGE);
1312 		argp = &argv[i];
1313 	}
1314 
1315 	if (opt_m) {
1316 		if (!(flags & DCMD_ADDRSPEC))
1317 			return (DCMD_USAGE);
1318 
1319 		if (major_to_addr(addr, &addr) == -1)
1320 			return (DCMD_ERR);
1321 
1322 	} else if (!(flags & DCMD_ADDRSPEC)) {
1323 		if (argp == NULL) {
1324 			if (mdb_walk_dcmd("devnames", "devnames", argc, argv)) {
1325 				mdb_warn("failed to walk devnames");
1326 				return (DCMD_ERR);
1327 			}
1328 			return (DCMD_OK);
1329 		}
1330 
1331 		if (argp->a_type == MDB_TYPE_IMMEDIATE)
1332 			major = (major_t)argp->a_un.a_val;
1333 		else
1334 			major = (major_t)mdb_strtoull(argp->a_un.a_str);
1335 
1336 		if (major_to_addr(major, &addr) == -1)
1337 			return (DCMD_ERR);
1338 	}
1339 
1340 	if (mdb_vread(&dn, sizeof (struct devnames), addr) == -1) {
1341 		mdb_warn("failed to read devnames struct at %p", addr);
1342 		return (DCMD_ERR);
1343 	}
1344 
1345 	if (DCMD_HDRSPEC(flags)) {
1346 		if (opt_v)
1347 			mdb_printf("%<u>%-16s%</u>\n", "NAME");
1348 		else
1349 			mdb_printf("%<u>%-16s %-?s%</u>\n", "NAME", "DN_HEAD");
1350 	}
1351 
1352 	if ((flags & DCMD_LOOP) && (dn.dn_name == NULL))
1353 		return (DCMD_OK); /* Skip empty slots if we're printing table */
1354 
1355 	if (mdb_readstr(name, sizeof (name), (uintptr_t)dn.dn_name) == -1)
1356 		(void) mdb_snprintf(name, sizeof (name), "0x%p", dn.dn_name);
1357 
1358 	if (opt_v) {
1359 		ddi_prop_list_t prop_list;
1360 		mdb_printf("%<b>%-16s%</b>\n", name);
1361 		mdb_inc_indent(2);
1362 
1363 		mdb_printf("          flags %b\n", dn.dn_flags, dn_flag_bits);
1364 		mdb_printf("             pl %p\n", (void *)dn.dn_pl);
1365 		mdb_printf("           head %p\n", dn.dn_head);
1366 		mdb_printf("       instance %d\n", dn.dn_instance);
1367 		mdb_printf("         inlist %p\n", dn.dn_inlist);
1368 		mdb_printf("global_prop_ptr %p\n", dn.dn_global_prop_ptr);
1369 		if (mdb_vread(&prop_list, sizeof (ddi_prop_list_t),
1370 		    (uintptr_t)dn.dn_global_prop_ptr) != -1) {
1371 			devinfo_print_props(NULL, prop_list.prop_list);
1372 		}
1373 
1374 		mdb_dec_indent(2);
1375 	} else
1376 		mdb_printf("%-16s %-?p\n", name, dn.dn_head);
1377 
1378 	return (DCMD_OK);
1379 }
1380 
1381 /*ARGSUSED*/
1382 int
1383 name2major(uintptr_t vaddr, uint_t flags, int argc, const mdb_arg_t *argv)
1384 {
1385 	major_t major;
1386 
1387 	if (flags & DCMD_ADDRSPEC)
1388 		return (DCMD_USAGE);
1389 
1390 	if (argc != 1 || argv->a_type != MDB_TYPE_STRING)
1391 		return (DCMD_USAGE);
1392 
1393 	if (mdb_name_to_major(argv->a_un.a_str, &major) != 0) {
1394 		mdb_warn("failed to convert name to major number\n");
1395 		return (DCMD_ERR);
1396 	}
1397 
1398 	mdb_printf("0x%x\n", major);
1399 	return (DCMD_OK);
1400 }
1401 
1402 /*
1403  * Get a numerical argument of a dcmd from addr if an address is specified
1404  * or from argv if no address is specified. Return the argument in ret.
1405  */
1406 static int
1407 getarg(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv,
1408     uintptr_t *ret)
1409 {
1410 	if (argc == 0 && (flags & DCMD_ADDRSPEC)) {
1411 		*ret = addr;
1412 
1413 	} else if (argc == 1 && !(flags & DCMD_ADDRSPEC)) {
1414 		*ret = (argv[0].a_type == MDB_TYPE_IMMEDIATE) ?
1415 		    (uintptr_t)argv[0].a_un.a_val :
1416 		    (uintptr_t)mdb_strtoull(argv->a_un.a_str);
1417 
1418 	} else {
1419 		return (-1);
1420 	}
1421 
1422 	return (0);
1423 }
1424 
1425 /*ARGSUSED*/
1426 int
1427 major2name(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1428 {
1429 	uintptr_t major;
1430 	const char *name;
1431 
1432 	if (getarg(addr, flags, argc, argv, &major) < 0)
1433 		return (DCMD_USAGE);
1434 
1435 	if ((name = mdb_major_to_name((major_t)major)) == NULL) {
1436 		mdb_warn("failed to convert major number to name\n");
1437 		return (DCMD_ERR);
1438 	}
1439 
1440 	mdb_printf("%s\n", name);
1441 	return (DCMD_OK);
1442 }
1443 
1444 /*ARGSUSED*/
1445 int
1446 dev2major(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1447 {
1448 	uintptr_t dev;
1449 
1450 	if (getarg(addr, flags, argc, argv, &dev) < 0)
1451 		return (DCMD_USAGE);
1452 
1453 	if (flags & DCMD_PIPE_OUT)
1454 		mdb_printf("%x\n", getmajor(dev));
1455 	else
1456 		mdb_printf("0x%x (0t%d)\n", getmajor(dev), getmajor(dev));
1457 
1458 	return (DCMD_OK);
1459 }
1460 
1461 /*ARGSUSED*/
1462 int
1463 dev2minor(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1464 {
1465 	uintptr_t dev;
1466 
1467 	if (getarg(addr, flags, argc, argv, &dev) < 0)
1468 		return (DCMD_USAGE);
1469 
1470 	if (flags & DCMD_PIPE_OUT)
1471 		mdb_printf("%x\n", getminor(dev));
1472 	else
1473 		mdb_printf("0x%x (0t%d)\n", getminor(dev), getminor(dev));
1474 
1475 	return (DCMD_OK);
1476 }
1477 
1478 /*ARGSUSED*/
1479 int
1480 devt(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1481 {
1482 	uintptr_t dev;
1483 
1484 	if (getarg(addr, flags, argc, argv, &dev) < 0)
1485 		return (DCMD_USAGE);
1486 
1487 	if (DCMD_HDRSPEC(flags)) {
1488 		mdb_printf("%<u>%10s%</u>  %<u>%10s%</u>\n", "MAJOR",
1489 		    "MINOR");
1490 	}
1491 
1492 	mdb_printf("%10d  %10d\n", getmajor(dev), getminor(dev));
1493 
1494 	return (DCMD_OK);
1495 }
1496 
1497 /*ARGSUSED*/
1498 int
1499 softstate(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1500 {
1501 	uintptr_t statep;
1502 	int instance;
1503 
1504 
1505 	if (argc != 1) {
1506 		return (DCMD_USAGE);
1507 	}
1508 
1509 	if (argv[0].a_type == MDB_TYPE_IMMEDIATE)
1510 		instance = argv[0].a_un.a_val;
1511 	else
1512 		instance = mdb_strtoull(argv->a_un.a_str);
1513 
1514 	if (mdb_get_soft_state_byaddr(addr, instance, &statep, NULL, 0) == -1) {
1515 		if (errno == ENOENT) {
1516 			mdb_warn("instance %d unused\n", instance);
1517 		} else {
1518 			mdb_warn("couldn't determine softstate for "
1519 			    "instance %d", instance);
1520 		}
1521 
1522 		return (DCMD_ERR);
1523 	}
1524 
1525 	mdb_printf("%p\n", statep);
1526 	return (DCMD_OK);
1527 }
1528 
1529 /*
1530  * Walker for all possible pointers to a driver state struct in an
1531  * i_ddi_soft_state instance chain.  Returns all non-NULL pointers.
1532  */
1533 typedef struct soft_state_walk {
1534 	struct i_ddi_soft_state	ssw_ss;	/* Local copy of i_ddi_soft_state */
1535 	void		**ssw_pointers;	/* to driver state structs */
1536 	uint_t		ssw_index;	/* array entry we're using */
1537 } soft_state_walk_t;
1538 
1539 int
1540 soft_state_walk_init(mdb_walk_state_t *wsp)
1541 {
1542 	soft_state_walk_t *sst;
1543 
1544 
1545 	if (wsp->walk_addr == NULL)
1546 		return (WALK_DONE);
1547 
1548 	sst = mdb_zalloc(sizeof (soft_state_walk_t), UM_SLEEP|UM_GC);
1549 	wsp->walk_data = sst;
1550 
1551 
1552 	if (mdb_vread(&(sst->ssw_ss), sizeof (sst->ssw_ss), wsp->walk_addr) !=
1553 	    sizeof (sst->ssw_ss)) {
1554 		mdb_warn("failed to read i_ddi_soft_state at %p",
1555 		    wsp->walk_addr);
1556 		return (WALK_ERR);
1557 	}
1558 
1559 
1560 	/* Read array of pointers to state structs into local storage. */
1561 	sst->ssw_pointers = mdb_alloc((sst->ssw_ss.n_items * sizeof (void *)),
1562 	    UM_SLEEP|UM_GC);
1563 
1564 	if (mdb_vread(sst->ssw_pointers, (sst->ssw_ss.n_items *
1565 	    sizeof (void *)), (uintptr_t)sst->ssw_ss.array) !=
1566 	    (sst->ssw_ss.n_items * sizeof (void *))) {
1567 		mdb_warn("failed to read i_ddi_soft_state at %p",
1568 		    wsp->walk_addr);
1569 		return (WALK_ERR);
1570 	}
1571 
1572 	sst->ssw_index = 0;
1573 
1574 	return (WALK_NEXT);
1575 }
1576 
1577 int
1578 soft_state_walk_step(mdb_walk_state_t *wsp)
1579 {
1580 	soft_state_walk_t *sst = (soft_state_walk_t *)wsp->walk_data;
1581 	int status = WALK_NEXT;
1582 
1583 
1584 	/*
1585 	 * If the entry indexed has a valid pointer to a soft state struct,
1586 	 * invoke caller's callback func.
1587 	 */
1588 	if (sst->ssw_pointers[sst->ssw_index] != NULL) {
1589 		status = wsp->walk_callback(
1590 		    (uintptr_t)(sst->ssw_pointers[sst->ssw_index]), NULL,
1591 		    wsp->walk_cbdata);
1592 	}
1593 
1594 	sst->ssw_index += 1;
1595 
1596 	if (sst->ssw_index == sst->ssw_ss.n_items)
1597 		return (WALK_DONE);
1598 
1599 	return (status);
1600 }
1601 
1602 int
1603 soft_state_all_walk_step(mdb_walk_state_t *wsp)
1604 {
1605 	soft_state_walk_t *sst = (soft_state_walk_t *)wsp->walk_data;
1606 	int status = WALK_NEXT;
1607 
1608 
1609 	status = wsp->walk_callback(
1610 	    (uintptr_t)(sst->ssw_pointers[sst->ssw_index]), NULL,
1611 	    wsp->walk_cbdata);
1612 
1613 	sst->ssw_index += 1;
1614 
1615 	if (sst->ssw_index == sst->ssw_ss.n_items)
1616 		return (WALK_DONE);
1617 
1618 	return (status);
1619 }
1620 
1621 /*ARGSUSED*/
1622 int
1623 devbindings(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1624 {
1625 	const mdb_arg_t *arg;
1626 	struct devnames dn;
1627 	uintptr_t dn_addr;
1628 	major_t major;
1629 
1630 	if (!(flags & DCMD_ADDRSPEC) && argc < 1)
1631 		return (DCMD_USAGE);
1632 
1633 	if (flags & DCMD_ADDRSPEC) {
1634 		/*
1635 		 * If there's an address, then it's a major number
1636 		 */
1637 		major = addr;
1638 	} else {
1639 		/*
1640 		 * We interpret the last argument. Any other arguments are
1641 		 * forwarded to "devinfo"
1642 		 */
1643 		arg = &argv[argc - 1];
1644 		argc--;
1645 
1646 		if (arg->a_type == MDB_TYPE_IMMEDIATE) {
1647 			major = (uintptr_t)arg->a_un.a_val;
1648 
1649 		} else if (arg->a_un.a_str[0] == '-') {
1650 			/* the argument shouldn't be an option */
1651 			return (DCMD_USAGE);
1652 
1653 		} else if (isdigit(arg->a_un.a_str[0])) {
1654 			major = (uintptr_t)mdb_strtoull(arg->a_un.a_str);
1655 
1656 		} else {
1657 			if (mdb_name_to_major(arg->a_un.a_str, &major) != 0) {
1658 				mdb_warn("failed to get major number for %s\n",
1659 				    arg->a_un.a_str);
1660 				return (DCMD_ERR);
1661 			}
1662 		}
1663 	}
1664 
1665 	if (major_to_addr(major, &dn_addr) != 0)
1666 		return (DCMD_ERR);
1667 
1668 	if (mdb_vread(&dn, sizeof (struct devnames), dn_addr) == -1) {
1669 		mdb_warn("couldn't read devnames array at %p", dn_addr);
1670 		return (DCMD_ERR);
1671 	}
1672 
1673 	if (mdb_pwalk_dcmd("devi_next", "devinfo", argc, argv,
1674 	    (uintptr_t)dn.dn_head) != 0) {
1675 		mdb_warn("couldn't walk the devinfo chain at %p", dn.dn_head);
1676 		return (DCMD_ERR);
1677 	}
1678 
1679 	return (DCMD_OK);
1680 }
1681 
1682 /*
1683  * walk binding hashtable (as of of driver names (e.g., mb_hashtab))
1684  */
1685 int
1686 binding_hash_walk_init(mdb_walk_state_t *wsp)
1687 {
1688 	if (wsp->walk_addr == NULL)
1689 		return (WALK_ERR);
1690 
1691 	wsp->walk_data = mdb_alloc(sizeof (void *) * MOD_BIND_HASHSIZE,
1692 	    UM_SLEEP|UM_GC);
1693 	if (mdb_vread(wsp->walk_data, sizeof (void *) * MOD_BIND_HASHSIZE,
1694 	    wsp->walk_addr) == -1) {
1695 		mdb_warn("failed to read mb_hashtab");
1696 		return (WALK_ERR);
1697 	}
1698 
1699 	wsp->walk_arg = 0;	/* index into mb_hashtab array to start */
1700 
1701 	return (WALK_NEXT);
1702 }
1703 
1704 int
1705 binding_hash_walk_step(mdb_walk_state_t *wsp)
1706 {
1707 	int		status;
1708 	uintptr_t	bind_p;
1709 	struct bind	bind;
1710 
1711 
1712 	/*
1713 	 * Walk the singly-linked list of struct bind
1714 	 */
1715 	bind_p = ((uintptr_t *)wsp->walk_data)[(ulong_t)wsp->walk_arg];
1716 	while (bind_p != NULL) {
1717 
1718 		if (mdb_vread(&bind, sizeof (bind), bind_p) == -1) {
1719 			mdb_warn("failed to read bind struct at %p",
1720 			    wsp->walk_addr);
1721 			return (WALK_ERR);
1722 		}
1723 
1724 		if ((status = wsp->walk_callback(bind_p, &bind,
1725 		    wsp->walk_cbdata)) != WALK_NEXT) {
1726 			return (status);
1727 		}
1728 
1729 		bind_p = (uintptr_t)bind.b_next;
1730 	}
1731 
1732 	wsp->walk_arg = (void *)((char *)wsp->walk_arg + 1);
1733 
1734 	if (wsp->walk_arg == (void *)(MOD_BIND_HASHSIZE - 1))
1735 		return (WALK_DONE);
1736 
1737 	return (WALK_NEXT);
1738 }
1739 
1740 /*ARGSUSED*/
1741 int
1742 binding_hash_entry(uintptr_t addr, uint_t flags, int argc,
1743     const mdb_arg_t *argv)
1744 {
1745 	struct bind 	bind;
1746 	/* Arbitrary lengths based on output format below */
1747 	char name[MAXPATHLEN] = "???";
1748 	char bind_name[MAXPATHLEN] = "<null>";
1749 
1750 	if ((flags & DCMD_ADDRSPEC) == NULL)
1751 		return (DCMD_USAGE);
1752 
1753 	/* Allow null addresses to be passed (as from a walker) */
1754 	if (addr == NULL)
1755 		return (DCMD_OK);
1756 
1757 	if (mdb_vread(&bind, sizeof (bind), addr) == -1) {
1758 		mdb_warn("failed to read struct bind at %p", addr);
1759 		return (DCMD_ERR);
1760 	}
1761 
1762 	if (DCMD_HDRSPEC(flags)) {
1763 		mdb_printf("%<u>%?s% %-5s %s%</u>\n",
1764 		    "NEXT", "MAJOR", "NAME(S)");
1765 	}
1766 
1767 	if (mdb_readstr(name, sizeof (name), (uintptr_t)bind.b_name) == -1)
1768 		mdb_warn("failed to read 'name'");
1769 
1770 	/* There may be bind_name, so this may fail */
1771 	if (mdb_readstr(bind_name, sizeof (bind_name),
1772 	    (uintptr_t)bind.b_bind_name) == -1) {
1773 		mdb_printf("%?p %5d %s\n",
1774 		    bind.b_next, bind.b_num, name);
1775 	} else {
1776 		mdb_printf("%?p %5d %s %s\n",
1777 		    bind.b_next, bind.b_num, name, bind_name);
1778 	}
1779 
1780 	return (DCMD_OK);
1781 }
1782 
1783 typedef struct devinfo_audit_log_walk_data {
1784 	devinfo_audit_t dil_buf;	/* buffer of last entry */
1785 	uintptr_t dil_base;		/* starting address of log buffer */
1786 	int dil_max;			/* maximum index */
1787 	int dil_start;			/* starting index */
1788 	int dil_index;			/* current walking index */
1789 } devinfo_audit_log_walk_data_t;
1790 
1791 int
1792 devinfo_audit_log_walk_init(mdb_walk_state_t *wsp)
1793 {
1794 	devinfo_log_header_t header;
1795 	devinfo_audit_log_walk_data_t *dil;
1796 	uintptr_t devinfo_audit_log;
1797 
1798 	/* read in devinfo_log_header structure */
1799 	if (mdb_readvar(&devinfo_audit_log, "devinfo_audit_log") == -1) {
1800 		mdb_warn("failed to read 'devinfo_audit_log'");
1801 		return (WALK_ERR);
1802 	}
1803 
1804 	if (mdb_vread(&header, sizeof (devinfo_log_header_t),
1805 	    devinfo_audit_log) == -1) {
1806 		mdb_warn("couldn't read devinfo_log_header at %p",
1807 		    devinfo_audit_log);
1808 		return (WALK_ERR);
1809 	}
1810 
1811 	dil = mdb_zalloc(sizeof (devinfo_audit_log_walk_data_t), UM_SLEEP);
1812 	wsp->walk_data = dil;
1813 
1814 	dil->dil_start = dil->dil_index = header.dh_curr;
1815 	dil->dil_max = header.dh_max;
1816 	if (dil->dil_start < 0)		/* no log entries */
1817 		return (WALK_DONE);
1818 
1819 	dil->dil_base = devinfo_audit_log +
1820 	    offsetof(devinfo_log_header_t, dh_entry);
1821 	wsp->walk_addr = dil->dil_base +
1822 	    dil->dil_index * sizeof (devinfo_audit_t);
1823 
1824 	return (WALK_NEXT);
1825 }
1826 
1827 int
1828 devinfo_audit_log_walk_step(mdb_walk_state_t *wsp)
1829 {
1830 	uintptr_t addr = wsp->walk_addr;
1831 	devinfo_audit_log_walk_data_t *dil = wsp->walk_data;
1832 	devinfo_audit_t *da = &dil->dil_buf;
1833 	int status = WALK_NEXT;
1834 
1835 	/* read in current entry and invoke callback */
1836 	if (addr == NULL)
1837 		return (WALK_DONE);
1838 
1839 	if (mdb_vread(&dil->dil_buf, sizeof (devinfo_audit_t), addr) == -1) {
1840 		mdb_warn("failed to read devinfo_audit at %p", addr);
1841 		status = WALK_DONE;
1842 	}
1843 	status = wsp->walk_callback(wsp->walk_addr, da, wsp->walk_cbdata);
1844 
1845 	/* step to the previous log entry in time */
1846 	if (--dil->dil_index < 0)
1847 		dil->dil_index += dil->dil_max;
1848 	if (dil->dil_index == dil->dil_start) {
1849 		wsp->walk_addr = NULL;
1850 		return (WALK_DONE);
1851 	}
1852 
1853 	wsp->walk_addr = dil->dil_base +
1854 	    dil->dil_index * sizeof (devinfo_audit_t);
1855 	return (status);
1856 }
1857 
1858 void
1859 devinfo_audit_log_walk_fini(mdb_walk_state_t *wsp)
1860 {
1861 	mdb_free(wsp->walk_data, sizeof (devinfo_audit_log_walk_data_t));
1862 }
1863 
1864 /*
1865  * display devinfo_audit_t stack trace
1866  */
1867 /*ARGSUSED*/
1868 int
1869 devinfo_audit(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1870 {
1871 	uint_t verbose = FALSE;
1872 	devinfo_audit_t da;
1873 	int i, depth;
1874 
1875 	if ((flags & DCMD_ADDRSPEC) == 0)
1876 		return (DCMD_USAGE);
1877 
1878 	if (mdb_getopts(argc, argv,
1879 	    'v', MDB_OPT_SETBITS, TRUE, &verbose, NULL) != argc)
1880 		return (DCMD_USAGE);
1881 
1882 	if (DCMD_HDRSPEC(flags)) {
1883 		mdb_printf(" %-?s %16s %-?s %-?s %5s\n",
1884 		    "AUDIT", "TIMESTAMP", "THREAD", "DEVINFO", "STATE");
1885 	}
1886 
1887 	if (mdb_vread(&da, sizeof (da), addr) == -1) {
1888 		mdb_warn("couldn't read devinfo_audit at %p", addr);
1889 		return (DCMD_ERR);
1890 	}
1891 
1892 	mdb_printf(" %0?p %16llx %0?p %0?p %s\n",
1893 	    addr, da.da_timestamp, da.da_thread, da.da_devinfo,
1894 	    di_state[MIN(da.da_node_state + 1, DI_STATE_MAX)]);
1895 
1896 	if (!verbose)
1897 		return (DCMD_OK);
1898 
1899 	mdb_inc_indent(4);
1900 
1901 	/*
1902 	 * Guard against bogus da_depth in case the devinfo_audit_t
1903 	 * is corrupt or the address does not really refer to a
1904 	 * devinfo_audit_t.
1905 	 */
1906 	depth = MIN(da.da_depth, DDI_STACK_DEPTH);
1907 
1908 	for (i = 0; i < depth; i++)
1909 		mdb_printf("%a\n", da.da_stack[i]);
1910 
1911 	mdb_printf("\n");
1912 	mdb_dec_indent(4);
1913 
1914 	return (DCMD_OK);
1915 }
1916 
1917 int
1918 devinfo_audit_log(uintptr_t addr, uint_t flags, int argc,
1919     const mdb_arg_t *argv)
1920 {
1921 	if (flags & DCMD_ADDRSPEC)
1922 		return (devinfo_audit(addr, flags, argc, argv));
1923 
1924 	(void) mdb_walk_dcmd("devinfo_audit_log", "devinfo_audit", argc, argv);
1925 	return (DCMD_OK);
1926 }
1927 
1928 typedef struct devinfo_audit_node_walk_data {
1929 	devinfo_audit_t dih_buf;	/* buffer of last entry */
1930 	uintptr_t dih_dip;		/* address of dev_info */
1931 	int dih_on_devinfo;		/* devi_audit on dev_info struct */
1932 } devinfo_audit_node_walk_data_t;
1933 
1934 int
1935 devinfo_audit_node_walk_init(mdb_walk_state_t *wsp)
1936 {
1937 	devinfo_audit_node_walk_data_t *dih;
1938 	devinfo_audit_t *da;
1939 	struct dev_info devi;
1940 	uintptr_t addr = wsp->walk_addr;
1941 
1942 	/* read in devinfo structure */
1943 	if (mdb_vread(&devi, sizeof (struct dev_info), addr) == -1) {
1944 		mdb_warn("couldn't read dev_info at %p", addr);
1945 		return (WALK_ERR);
1946 	}
1947 
1948 	dih = mdb_zalloc(sizeof (devinfo_audit_node_walk_data_t), UM_SLEEP);
1949 	wsp->walk_data = dih;
1950 	da = &dih->dih_buf;
1951 
1952 	/* read in devi_audit structure */
1953 	if (mdb_vread(da, sizeof (devinfo_audit_t), (uintptr_t)devi.devi_audit)
1954 	    == -1) {
1955 		mdb_warn("couldn't read devi_audit at %p", devi.devi_audit);
1956 		return (WALK_ERR);
1957 	}
1958 	dih->dih_dip = addr;
1959 	dih->dih_on_devinfo = 1;
1960 	wsp->walk_addr = (uintptr_t)devi.devi_audit;
1961 
1962 	return (WALK_NEXT);
1963 }
1964 
1965 int
1966 devinfo_audit_node_walk_step(mdb_walk_state_t *wsp)
1967 {
1968 	uintptr_t addr;
1969 	devinfo_audit_node_walk_data_t *dih = wsp->walk_data;
1970 	devinfo_audit_t *da = &dih->dih_buf;
1971 
1972 	if (wsp->walk_addr == NULL)
1973 		return (WALK_DONE);
1974 	(void) wsp->walk_callback(wsp->walk_addr, NULL, wsp->walk_cbdata);
1975 
1976 skip:
1977 	/* read in previous entry */
1978 	if ((addr = (uintptr_t)da->da_lastlog) == 0)
1979 		return (WALK_DONE);
1980 
1981 	if (mdb_vread(&dih->dih_buf, sizeof (devinfo_audit_t), addr) == -1) {
1982 		mdb_warn("failed to read devinfo_audit at %p", addr);
1983 		return (WALK_DONE);
1984 	}
1985 
1986 	/* check if last log was over-written */
1987 	if ((uintptr_t)da->da_devinfo != dih->dih_dip)
1988 		return (WALK_DONE);
1989 
1990 	/*
1991 	 * skip the first common log entry, which is a duplicate of
1992 	 * the devi_audit buffer on the dev_info structure
1993 	 */
1994 	if (dih->dih_on_devinfo) {
1995 		dih->dih_on_devinfo = 0;
1996 		goto skip;
1997 	}
1998 	wsp->walk_addr = addr;
1999 
2000 	return (WALK_NEXT);
2001 }
2002 
2003 void
2004 devinfo_audit_node_walk_fini(mdb_walk_state_t *wsp)
2005 {
2006 	mdb_free(wsp->walk_data, sizeof (devinfo_audit_node_walk_data_t));
2007 }
2008 
2009 int
2010 devinfo_audit_node(uintptr_t addr, uint_t flags, int argc,
2011     const mdb_arg_t *argv)
2012 {
2013 	if (!(flags & DCMD_ADDRSPEC))
2014 		return (DCMD_USAGE);
2015 
2016 	(void) mdb_pwalk_dcmd("devinfo_audit_node", "devinfo_audit",
2017 	    argc, argv, addr);
2018 	return (DCMD_OK);
2019 }
2020 
2021 /*
2022  * mdb support for per-devinfo fault management data
2023  */
2024 /*ARGSUSED*/
2025 int
2026 devinfo_fm(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2027 {
2028 	struct dev_info devi;
2029 	struct i_ddi_fmhdl fhdl;
2030 
2031 	if ((flags & DCMD_ADDRSPEC) == 0)
2032 		return (DCMD_USAGE);
2033 
2034 	if (DCMD_HDRSPEC(flags)) {
2035 		mdb_printf("%<u>%-11s IPL CAPS DROP FMCFULL FMCGREW ACCERR "
2036 		    "DMAERR %11s %11s%</u>\n", "ADDR", "DMACACHE", "ACCCACHE");
2037 	}
2038 
2039 	if (mdb_vread(&devi, sizeof (devi), addr) == -1) {
2040 		mdb_warn("failed to read devinfo struct at %p", addr);
2041 		return (DCMD_ERR);
2042 	}
2043 
2044 	if (mdb_vread(&fhdl, sizeof (fhdl), (uintptr_t)devi.devi_fmhdl) == -1) {
2045 		mdb_warn("failed to read devinfo fm struct at %p",
2046 		    (uintptr_t)devi.devi_fmhdl);
2047 		return (DCMD_ERR);
2048 	}
2049 
2050 	mdb_printf("%-11p %3u %c%c%c%c %4llu %7llu %7llu %6llu %6llu %11p "
2051 	    "%11p\n",
2052 	    (uintptr_t)devi.devi_fmhdl, fhdl.fh_ibc,
2053 	    (DDI_FM_EREPORT_CAP(fhdl.fh_cap) ? 'E' : '-'),
2054 	    (DDI_FM_ERRCB_CAP(fhdl.fh_cap) ? 'C' : '-'),
2055 	    (DDI_FM_ACC_ERR_CAP(fhdl.fh_cap) ? 'A' : '-'),
2056 	    (DDI_FM_DMA_ERR_CAP(fhdl.fh_cap) ? 'D' : '-'),
2057 	    fhdl.fh_kstat.fek_erpt_dropped.value.ui64,
2058 	    fhdl.fh_kstat.fek_fmc_full.value.ui64,
2059 	    fhdl.fh_kstat.fek_fmc_grew.value.ui64,
2060 	    fhdl.fh_kstat.fek_acc_err.value.ui64,
2061 	    fhdl.fh_kstat.fek_dma_err.value.ui64,
2062 	    fhdl.fh_dma_cache, fhdl.fh_acc_cache);
2063 
2064 
2065 	return (DCMD_OK);
2066 }
2067 
2068 /*ARGSUSED*/
2069 int
2070 devinfo_fmce(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2071 {
2072 	struct i_ddi_fmc_entry fce;
2073 
2074 	if ((flags & DCMD_ADDRSPEC) == 0)
2075 		return (DCMD_USAGE);
2076 
2077 	if (DCMD_HDRSPEC(flags)) {
2078 		mdb_printf("%<u>%-11s    %11s    %11s%</u>\n", "ADDR",
2079 		    "RESOURCE", "BUS_SPECIFIC");
2080 	}
2081 
2082 	if (mdb_vread(&fce, sizeof (fce), addr) == -1) {
2083 		mdb_warn("failed to read fm cache struct at %p", addr);
2084 		return (DCMD_ERR);
2085 	}
2086 
2087 	mdb_printf("%-11p    %11p    %11p\n",
2088 	    (uintptr_t)addr, fce.fce_resource, fce.fce_bus_specific);
2089 
2090 
2091 	return (DCMD_OK);
2092 }
2093 
2094 int
2095 devinfo_fmc_walk_init(mdb_walk_state_t *wsp)
2096 {
2097 	struct i_ddi_fmc fec;
2098 	struct i_ddi_fmc_entry fe;
2099 
2100 	if (wsp->walk_addr == NULL)
2101 		return (WALK_ERR);
2102 
2103 	if (mdb_vread(&fec, sizeof (fec), wsp->walk_addr) == -1) {
2104 		mdb_warn("failed to read fm cache at %p", wsp->walk_addr);
2105 		return (WALK_ERR);
2106 	}
2107 
2108 	if (fec.fc_active == NULL)
2109 		return (WALK_DONE);
2110 
2111 	if (mdb_vread(&fe, sizeof (fe), (uintptr_t)fec.fc_active) == -1) {
2112 		mdb_warn("failed to read active fm cache list at %p",
2113 		    fec.fc_active);
2114 		return (WALK_ERR);
2115 	}
2116 
2117 	wsp->walk_data = fe.fce_next;
2118 	wsp->walk_addr = (uintptr_t)fe.fce_next;
2119 	return (WALK_NEXT);
2120 }
2121 
2122 int
2123 devinfo_fmc_walk_step(mdb_walk_state_t *wsp)
2124 {
2125 	int status;
2126 	struct i_ddi_fmc_entry fe;
2127 
2128 	if (mdb_vread(&fe, sizeof (fe), wsp->walk_addr) == -1) {
2129 		mdb_warn("failed to read active fm cache entry at %p",
2130 		    wsp->walk_addr);
2131 		return (WALK_DONE);
2132 	}
2133 
2134 	status = wsp->walk_callback(wsp->walk_addr, &fe, wsp->walk_cbdata);
2135 
2136 	if (fe.fce_next == NULL)
2137 		return (WALK_DONE);
2138 
2139 	wsp->walk_addr = (uintptr_t)fe.fce_next;
2140 	return (status);
2141 }
2142 
2143 int
2144 minornode_walk_init(mdb_walk_state_t *wsp)
2145 {
2146 	struct dev_info di;
2147 	uintptr_t addr = wsp->walk_addr;
2148 
2149 	if (addr == NULL) {
2150 		mdb_warn("a dev_info struct address must be provided\n");
2151 		return (WALK_ERR);
2152 	}
2153 
2154 	if (mdb_vread(&di, sizeof (di), wsp->walk_addr) == -1) {
2155 		mdb_warn("failed to read dev_info struct at %p", addr);
2156 		return (WALK_ERR);
2157 	}
2158 
2159 	wsp->walk_addr = (uintptr_t)di.devi_minor;
2160 	return (WALK_NEXT);
2161 }
2162 
2163 int
2164 minornode_walk_step(mdb_walk_state_t *wsp)
2165 {
2166 	struct ddi_minor_data md;
2167 	uintptr_t addr = wsp->walk_addr;
2168 
2169 	if (addr == NULL)
2170 		return (WALK_DONE);
2171 
2172 	if (mdb_vread(&md, sizeof (md), addr) == -1) {
2173 		mdb_warn("failed to read dev_info struct at %p", addr);
2174 		return (WALK_DONE);
2175 	}
2176 
2177 	wsp->walk_addr = (uintptr_t)md.next;
2178 	return (wsp->walk_callback(addr, &md, wsp->walk_cbdata));
2179 }
2180 
2181 static const char *const md_type[] = {
2182 	"DDI_MINOR",
2183 	"DDI_ALIAS",
2184 	"DDI_DEFAULT",
2185 	"DDI_I_PATH",
2186 	"?"
2187 };
2188 
2189 #define	MD_TYPE_MAX	((sizeof (md_type) / sizeof (char *)) - 1)
2190 
2191 /*ARGSUSED*/
2192 static int
2193 print_minornode(uintptr_t addr, const void *arg, void *data)
2194 {
2195 	char name[128];
2196 	char nodetype[128];
2197 	char *spectype;
2198 	struct ddi_minor_data *mdp = (struct ddi_minor_data *)arg;
2199 
2200 	if (mdb_readstr(name, sizeof (name), (uintptr_t)mdp->ddm_name) == -1)
2201 		*name = '\0';
2202 
2203 	if (mdb_readstr(nodetype, sizeof (nodetype),
2204 	    (uintptr_t)mdp->ddm_node_type) == -1)
2205 		*nodetype = '\0';
2206 
2207 	switch (mdp->ddm_spec_type) {
2208 		case S_IFCHR:	spectype = "c";	break;
2209 		case S_IFBLK:	spectype = "b";	break;
2210 		default:	spectype = "?";	break;
2211 	}
2212 
2213 	mdb_printf("%?p %16lx %-4s %-11s %-10s %s\n",
2214 	    addr, mdp->ddm_dev, spectype, md_type[MIN(mdp->type, MD_TYPE_MAX)],
2215 	    name, nodetype);
2216 
2217 	return (WALK_NEXT);
2218 }
2219 
2220 /*ARGSUSED*/
2221 int
2222 minornodes(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2223 {
2224 	if (!(flags & DCMD_ADDRSPEC) || argc != 0)
2225 		return (DCMD_USAGE);
2226 
2227 	if (DCMD_HDRSPEC(flags))
2228 		mdb_printf("%<u>%?s %16s %-4s %-11s %-10s %-16s%</u>\n",
2229 		    "ADDR", "DEV", "SPEC", "TYPE", "NAME", "NODETYPE");
2230 
2231 	if (mdb_pwalk("minornode", print_minornode, NULL, addr) == -1) {
2232 		mdb_warn("can't walk minornode");
2233 		return (DCMD_ERR);
2234 	}
2235 
2236 	return (DCMD_OK);
2237 }
2238