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