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