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