xref: /onnv-gate/usr/src/cmd/mdb/common/modules/genunix/devinfo.c (revision 6640:c92ca9b95b9c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51993Sramat  * Common Development and Distribution License (the "License").
61993Sramat  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
225839Sdmick  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/sysmacros.h>
300Sstevel@tonic-gate #include <sys/dditypes.h>
310Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
320Sstevel@tonic-gate #include <sys/ddifm.h>
330Sstevel@tonic-gate #include <sys/ddipropdefs.h>
340Sstevel@tonic-gate #include <sys/modctl.h>
350Sstevel@tonic-gate #include <sys/hwconf.h>
360Sstevel@tonic-gate #include <sys/stat.h>
370Sstevel@tonic-gate #include <errno.h>
380Sstevel@tonic-gate #include <sys/sunmdi.h>
390Sstevel@tonic-gate #include <sys/mdi_impldefs.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <ctype.h>
420Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
430Sstevel@tonic-gate #include <mdb/mdb_ks.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include "nvpair.h"
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #define	DEVINFO_TREE_INDENT	4	/* Indent for devs one down in tree */
480Sstevel@tonic-gate #define	DEVINFO_PROP_INDENT	4	/* Indent for properties */
490Sstevel@tonic-gate #define	DEVINFO_PROPLIST_INDENT	8	/* Indent for properties lists */
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * Options for prtconf/devinfo dcmd.
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate #define	DEVINFO_VERBOSE	0x1
560Sstevel@tonic-gate #define	DEVINFO_PARENT	0x2
570Sstevel@tonic-gate #define	DEVINFO_CHILD	0x4
580Sstevel@tonic-gate #define	DEVINFO_ALLBOLD	0x8
590Sstevel@tonic-gate #define	DEVINFO_SUMMARY	0x10
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate  * devinfo node state map. Used by devinfo() and devinfo_audit().
630Sstevel@tonic-gate  * Long words are deliberately truncated so that output
640Sstevel@tonic-gate  * fits in 80 column with 64-bit addresses.
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate static const char *const di_state[] = {
670Sstevel@tonic-gate 	"DS_INVAL",
680Sstevel@tonic-gate 	"DS_PROTO",
690Sstevel@tonic-gate 	"DS_LINKED",
700Sstevel@tonic-gate 	"DS_BOUND",
710Sstevel@tonic-gate 	"DS_INITIA",
720Sstevel@tonic-gate 	"DS_PROBED",
730Sstevel@tonic-gate 	"DS_ATTACH",
740Sstevel@tonic-gate 	"DS_READY",
750Sstevel@tonic-gate 	"?"
760Sstevel@tonic-gate };
770Sstevel@tonic-gate 
780Sstevel@tonic-gate #define	DI_STATE_MAX	((sizeof (di_state) / sizeof (char *)) - 1)
790Sstevel@tonic-gate 
800Sstevel@tonic-gate void
810Sstevel@tonic-gate prtconf_help(void)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate 	mdb_printf("Prints the devinfo tree from a given node.\n"
840Sstevel@tonic-gate 	    "Without the address of a \"struct devinfo\" given, "
850Sstevel@tonic-gate 	    "prints from the root;\n"
860Sstevel@tonic-gate 	    "with an address, prints the parents of, "
870Sstevel@tonic-gate 	    "and all children of, that address.\n\n"
880Sstevel@tonic-gate 	    "Switches:\n"
890Sstevel@tonic-gate 	    "  -v   be verbose - print device property lists\n"
900Sstevel@tonic-gate 	    "  -p   only print the ancestors of the given node\n"
910Sstevel@tonic-gate 	    "  -c   only print the children of the given node\n");
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
940Sstevel@tonic-gate void
950Sstevel@tonic-gate devinfo_help(void)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	mdb_printf("Switches:\n"
980Sstevel@tonic-gate 	    "  -q   be quiet - don't print device property lists\n"
990Sstevel@tonic-gate 	    "  -s   print summary of dev_info structures\n");
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * Devinfo walker.
1050Sstevel@tonic-gate  */
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate typedef struct {
1080Sstevel@tonic-gate 	/*
1090Sstevel@tonic-gate 	 * The "struct dev_info" must be the first thing in this structure.
1100Sstevel@tonic-gate 	 */
1110Sstevel@tonic-gate 	struct dev_info din_dev;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	/*
1140Sstevel@tonic-gate 	 * This is for the benefit of prtconf().
1150Sstevel@tonic-gate 	 */
1160Sstevel@tonic-gate 	int din_depth;
1170Sstevel@tonic-gate } devinfo_node_t;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate typedef struct devinfo_parents_walk_data {
1200Sstevel@tonic-gate 	devinfo_node_t dip_node;
1210Sstevel@tonic-gate #define	dip_dev dip_node.din_dev
1220Sstevel@tonic-gate #define	dip_depth dip_node.din_depth
1230Sstevel@tonic-gate 	struct dev_info *dip_end;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	/*
1260Sstevel@tonic-gate 	 * The following three elements are for walking the parents of a node:
1270Sstevel@tonic-gate 	 * "dip_base_depth" is the depth of the given node from the root.
1280Sstevel@tonic-gate 	 *   This starts at 1 (if we're walking devinfo_root), because
1290Sstevel@tonic-gate 	 *   it's the size of the dip_parent_{nodes,addresses} arrays,
1300Sstevel@tonic-gate 	 *   and has to include the given node.
1310Sstevel@tonic-gate 	 * "dip_parent_nodes" is a collection of the parent node structures,
1320Sstevel@tonic-gate 	 *   already read in via mdb_vread().  dip_parent_nodes[0] is the
1330Sstevel@tonic-gate 	 *   root, dip_parent_nodes[1] is a child of the root, etc.
1340Sstevel@tonic-gate 	 * "dip_parent_addresses" holds the vaddrs of all the parent nodes.
1350Sstevel@tonic-gate 	 */
1360Sstevel@tonic-gate 	int dip_base_depth;
1370Sstevel@tonic-gate 	devinfo_node_t *dip_parent_nodes;
1380Sstevel@tonic-gate 	uintptr_t *dip_parent_addresses;
1390Sstevel@tonic-gate } devinfo_parents_walk_data_t;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate int
1420Sstevel@tonic-gate devinfo_parents_walk_init(mdb_walk_state_t *wsp)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate 	devinfo_parents_walk_data_t *dip;
1450Sstevel@tonic-gate 	uintptr_t addr;
1465839Sdmick 	uintptr_t devinfo_root;		/* Address of root of devinfo tree */
1470Sstevel@tonic-gate 	int i;
1480Sstevel@tonic-gate 
1495839Sdmick 	if (mdb_readvar(&devinfo_root, "top_devinfo") == -1) {
1505839Sdmick 		mdb_warn("failed to read 'top_devinfo'");
1515839Sdmick 		return (NULL);
1525839Sdmick 	}
1535839Sdmick 
1540Sstevel@tonic-gate 	if (wsp->walk_addr == NULL)
1550Sstevel@tonic-gate 		wsp->walk_addr = devinfo_root;
1560Sstevel@tonic-gate 	addr = wsp->walk_addr;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	dip = mdb_alloc(sizeof (devinfo_parents_walk_data_t), UM_SLEEP);
1590Sstevel@tonic-gate 	wsp->walk_data = dip;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	dip->dip_end = (struct dev_info *)wsp->walk_addr;
1620Sstevel@tonic-gate 	dip->dip_depth = 0;
1630Sstevel@tonic-gate 	dip->dip_base_depth = 1;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	do {
1660Sstevel@tonic-gate 		if (mdb_vread(&dip->dip_dev, sizeof (dip->dip_dev),
1670Sstevel@tonic-gate 		    addr) == -1) {
1680Sstevel@tonic-gate 			mdb_warn("failed to read devinfo at %p", addr);
1690Sstevel@tonic-gate 			mdb_free(dip, sizeof (devinfo_parents_walk_data_t));
1700Sstevel@tonic-gate 			wsp->walk_data = NULL;
1710Sstevel@tonic-gate 			return (WALK_ERR);
1720Sstevel@tonic-gate 		}
1730Sstevel@tonic-gate 		addr = (uintptr_t)dip->dip_dev.devi_parent;
1740Sstevel@tonic-gate 		if (addr != 0)
1750Sstevel@tonic-gate 			dip->dip_base_depth++;
1760Sstevel@tonic-gate 	} while (addr != 0);
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	addr = wsp->walk_addr;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	dip->dip_parent_nodes = mdb_alloc(
1810Sstevel@tonic-gate 	    dip->dip_base_depth * sizeof (devinfo_node_t), UM_SLEEP);
1820Sstevel@tonic-gate 	dip->dip_parent_addresses = mdb_alloc(
1830Sstevel@tonic-gate 	    dip->dip_base_depth * sizeof (uintptr_t), UM_SLEEP);
1840Sstevel@tonic-gate 	for (i = dip->dip_base_depth - 1; i >= 0; i--) {
1850Sstevel@tonic-gate 		if (mdb_vread(&dip->dip_parent_nodes[i].din_dev,
1860Sstevel@tonic-gate 		    sizeof (struct dev_info), addr) == -1) {
1870Sstevel@tonic-gate 			mdb_warn("failed to read devinfo at %p", addr);
1880Sstevel@tonic-gate 			return (WALK_ERR);
1890Sstevel@tonic-gate 		}
1900Sstevel@tonic-gate 		dip->dip_parent_nodes[i].din_depth = i;
1910Sstevel@tonic-gate 		dip->dip_parent_addresses[i] = addr;
1920Sstevel@tonic-gate 		addr = (uintptr_t)
1930Sstevel@tonic-gate 		    dip->dip_parent_nodes[i].din_dev.devi_parent;
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	return (WALK_NEXT);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate int
2000Sstevel@tonic-gate devinfo_parents_walk_step(mdb_walk_state_t *wsp)
2010Sstevel@tonic-gate {
2020Sstevel@tonic-gate 	devinfo_parents_walk_data_t *dip = wsp->walk_data;
2030Sstevel@tonic-gate 	int status;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	if (dip->dip_depth == dip->dip_base_depth)
2060Sstevel@tonic-gate 		return (WALK_DONE);
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	status = wsp->walk_callback(
2090Sstevel@tonic-gate 	    dip->dip_parent_addresses[dip->dip_depth],
2100Sstevel@tonic-gate 	    &dip->dip_parent_nodes[dip->dip_depth],
2110Sstevel@tonic-gate 	    wsp->walk_cbdata);
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	dip->dip_depth++;
2140Sstevel@tonic-gate 	return (status);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate void
2180Sstevel@tonic-gate devinfo_parents_walk_fini(mdb_walk_state_t *wsp)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate 	devinfo_parents_walk_data_t *dip = wsp->walk_data;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	mdb_free(dip->dip_parent_nodes,
2230Sstevel@tonic-gate 	    dip->dip_base_depth * sizeof (devinfo_node_t));
2240Sstevel@tonic-gate 	mdb_free(dip->dip_parent_addresses,
2250Sstevel@tonic-gate 	    dip->dip_base_depth * sizeof (uintptr_t));
2260Sstevel@tonic-gate 	mdb_free(wsp->walk_data, sizeof (devinfo_parents_walk_data_t));
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate typedef struct devinfo_children_walk_data {
2310Sstevel@tonic-gate 	devinfo_node_t dic_node;
2320Sstevel@tonic-gate #define	dic_dev dic_node.din_dev
2330Sstevel@tonic-gate #define	dic_depth dic_node.din_depth
2340Sstevel@tonic-gate 	struct dev_info *dic_end;
2350Sstevel@tonic-gate 	int dic_print_first_node;
2360Sstevel@tonic-gate } devinfo_children_walk_data_t;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate int
2390Sstevel@tonic-gate devinfo_children_walk_init(mdb_walk_state_t *wsp)
2400Sstevel@tonic-gate {
2410Sstevel@tonic-gate 	devinfo_children_walk_data_t *dic;
2425839Sdmick 	uintptr_t devinfo_root;		/* Address of root of devinfo tree */
2435839Sdmick 
2445839Sdmick 	if (mdb_readvar(&devinfo_root, "top_devinfo") == -1) {
2455839Sdmick 		mdb_warn("failed to read 'top_devinfo'");
2465839Sdmick 		return (NULL);
2475839Sdmick 	}
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	if (wsp->walk_addr == NULL)
2500Sstevel@tonic-gate 		wsp->walk_addr = devinfo_root;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	dic = mdb_alloc(sizeof (devinfo_children_walk_data_t), UM_SLEEP);
2530Sstevel@tonic-gate 	wsp->walk_data = dic;
2540Sstevel@tonic-gate 	dic->dic_end = (struct dev_info *)wsp->walk_addr;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	/*
2570Sstevel@tonic-gate 	 * This could be set by devinfo_walk_init().
2580Sstevel@tonic-gate 	 */
2590Sstevel@tonic-gate 	if (wsp->walk_arg != NULL) {
2600Sstevel@tonic-gate 		dic->dic_depth = (*(int *)wsp->walk_arg - 1);
2610Sstevel@tonic-gate 		dic->dic_print_first_node = 0;
2620Sstevel@tonic-gate 	} else {
2630Sstevel@tonic-gate 		dic->dic_depth = 0;
2640Sstevel@tonic-gate 		dic->dic_print_first_node = 1;
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	return (WALK_NEXT);
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate int
2710Sstevel@tonic-gate devinfo_children_walk_step(mdb_walk_state_t *wsp)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate 	devinfo_children_walk_data_t *dic = wsp->walk_data;
2740Sstevel@tonic-gate 	struct dev_info *v;
2750Sstevel@tonic-gate 	devinfo_node_t *cur;
2760Sstevel@tonic-gate 	uintptr_t addr = wsp->walk_addr;
2770Sstevel@tonic-gate 	int status = WALK_NEXT;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	if (wsp->walk_addr == NULL)
2800Sstevel@tonic-gate 		return (WALK_DONE);
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	if (mdb_vread(&dic->dic_dev, sizeof (dic->dic_dev), addr) == -1) {
2830Sstevel@tonic-gate 		mdb_warn("failed to read devinfo at %p", addr);
2840Sstevel@tonic-gate 		return (WALK_DONE);
2850Sstevel@tonic-gate 	}
2860Sstevel@tonic-gate 	cur = &dic->dic_node;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	if (dic->dic_print_first_node == 0)
2890Sstevel@tonic-gate 		dic->dic_print_first_node = 1;
2900Sstevel@tonic-gate 	else
2910Sstevel@tonic-gate 		status = wsp->walk_callback(addr, cur, wsp->walk_cbdata);
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	/*
2940Sstevel@tonic-gate 	 * "v" is always a virtual address pointer,
2950Sstevel@tonic-gate 	 *  i.e. can't be deref'ed.
2960Sstevel@tonic-gate 	 */
2970Sstevel@tonic-gate 	v = (struct dev_info *)addr;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	if (dic->dic_dev.devi_child != NULL) {
3000Sstevel@tonic-gate 		v = dic->dic_dev.devi_child;
3010Sstevel@tonic-gate 		dic->dic_depth++;
3020Sstevel@tonic-gate 	} else if (dic->dic_dev.devi_sibling != NULL && v != dic->dic_end) {
3030Sstevel@tonic-gate 		v = dic->dic_dev.devi_sibling;
3040Sstevel@tonic-gate 	} else {
3050Sstevel@tonic-gate 		while (v != NULL && v != dic->dic_end &&
3060Sstevel@tonic-gate 		    dic->dic_dev.devi_sibling == NULL) {
3070Sstevel@tonic-gate 			v = dic->dic_dev.devi_parent;
3080Sstevel@tonic-gate 			if (v == NULL)
3090Sstevel@tonic-gate 				break;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 			mdb_vread(&dic->dic_dev,
3120Sstevel@tonic-gate 			    sizeof (struct dev_info), (uintptr_t)v);
3130Sstevel@tonic-gate 			dic->dic_depth--;
3140Sstevel@tonic-gate 		}
3150Sstevel@tonic-gate 		if (v != NULL && v != dic->dic_end)
3160Sstevel@tonic-gate 			v = dic->dic_dev.devi_sibling;
3170Sstevel@tonic-gate 		if (v == dic->dic_end)
3180Sstevel@tonic-gate 			v = NULL;	/* Done */
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)v;
3220Sstevel@tonic-gate 	return (status);
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate void
3260Sstevel@tonic-gate devinfo_children_walk_fini(mdb_walk_state_t *wsp)
3270Sstevel@tonic-gate {
3280Sstevel@tonic-gate 	mdb_free(wsp->walk_data, sizeof (devinfo_children_walk_data_t));
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate typedef struct devinfo_walk_data {
3320Sstevel@tonic-gate 	mdb_walk_state_t diw_parent, diw_child;
3330Sstevel@tonic-gate 	enum { DIW_PARENT, DIW_CHILD, DIW_DONE } diw_mode;
3340Sstevel@tonic-gate } devinfo_walk_data_t;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate int
3370Sstevel@tonic-gate devinfo_walk_init(mdb_walk_state_t *wsp)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate 	devinfo_walk_data_t *diw;
3400Sstevel@tonic-gate 	devinfo_parents_walk_data_t *dip;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	diw = mdb_alloc(sizeof (devinfo_walk_data_t), UM_SLEEP);
3430Sstevel@tonic-gate 	diw->diw_parent = *wsp;
3440Sstevel@tonic-gate 	diw->diw_child = *wsp;
3450Sstevel@tonic-gate 	wsp->walk_data = diw;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	diw->diw_mode = DIW_PARENT;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	if (devinfo_parents_walk_init(&diw->diw_parent) == -1) {
3500Sstevel@tonic-gate 		mdb_free(diw, sizeof (devinfo_walk_data_t));
3510Sstevel@tonic-gate 		return (WALK_ERR);
3520Sstevel@tonic-gate 	}
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	/*
3550Sstevel@tonic-gate 	 * This is why the "devinfo" walker needs to be marginally
3560Sstevel@tonic-gate 	 * complicated - the child walker needs this initialization
3570Sstevel@tonic-gate 	 * data, and the best way to get it is out of the parent walker.
3580Sstevel@tonic-gate 	 */
3590Sstevel@tonic-gate 	dip = diw->diw_parent.walk_data;
3600Sstevel@tonic-gate 	diw->diw_child.walk_arg = &dip->dip_base_depth;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	if (devinfo_children_walk_init(&diw->diw_child) == -1) {
3630Sstevel@tonic-gate 		devinfo_parents_walk_fini(&diw->diw_parent);
3640Sstevel@tonic-gate 		mdb_free(diw, sizeof (devinfo_walk_data_t));
3650Sstevel@tonic-gate 		return (WALK_ERR);
3660Sstevel@tonic-gate 	}
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	return (WALK_NEXT);
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate int
3720Sstevel@tonic-gate devinfo_walk_step(mdb_walk_state_t *wsp)
3730Sstevel@tonic-gate {
3740Sstevel@tonic-gate 	devinfo_walk_data_t *diw = wsp->walk_data;
3750Sstevel@tonic-gate 	int status = WALK_NEXT;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	if (diw->diw_mode == DIW_PARENT) {
3780Sstevel@tonic-gate 		status = devinfo_parents_walk_step(&diw->diw_parent);
3790Sstevel@tonic-gate 		if (status != WALK_NEXT) {
3800Sstevel@tonic-gate 			/*
3810Sstevel@tonic-gate 			 * Keep on going even if the parents walk hit an error.
3820Sstevel@tonic-gate 			 */
3830Sstevel@tonic-gate 			diw->diw_mode = DIW_CHILD;
3840Sstevel@tonic-gate 			status = WALK_NEXT;
3850Sstevel@tonic-gate 		}
3860Sstevel@tonic-gate 	} else if (diw->diw_mode == DIW_CHILD) {
3870Sstevel@tonic-gate 		status = devinfo_children_walk_step(&diw->diw_child);
3880Sstevel@tonic-gate 		if (status != WALK_NEXT) {
3890Sstevel@tonic-gate 			diw->diw_mode = DIW_DONE;
3900Sstevel@tonic-gate 			status = WALK_DONE;
3910Sstevel@tonic-gate 		}
3920Sstevel@tonic-gate 	} else
3930Sstevel@tonic-gate 		status = WALK_DONE;
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	return (status);
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate void
3990Sstevel@tonic-gate devinfo_walk_fini(mdb_walk_state_t *wsp)
4000Sstevel@tonic-gate {
4010Sstevel@tonic-gate 	devinfo_walk_data_t *diw = wsp->walk_data;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	devinfo_children_walk_fini(&diw->diw_child);
4040Sstevel@tonic-gate 	devinfo_parents_walk_fini(&diw->diw_parent);
4050Sstevel@tonic-gate 	mdb_free(diw, sizeof (devinfo_walk_data_t));
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate /*
4090Sstevel@tonic-gate  * Given a devinfo pointer, figure out which driver is associated
4100Sstevel@tonic-gate  * with the node (by driver name, from the devnames array).
4110Sstevel@tonic-gate  */
4120Sstevel@tonic-gate /*ARGSUSED*/
4130Sstevel@tonic-gate int
4140Sstevel@tonic-gate devinfo2driver(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate 	char dname[MODMAXNAMELEN + 1];
4170Sstevel@tonic-gate 	struct dev_info devi;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
4210Sstevel@tonic-gate 		return (DCMD_USAGE);
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	if (mdb_vread(&devi, sizeof (devi), addr) == -1) {
4240Sstevel@tonic-gate 		mdb_warn("failed to read devinfo struct at %p", addr);
4250Sstevel@tonic-gate 		return (DCMD_ERR);
4260Sstevel@tonic-gate 	}
4270Sstevel@tonic-gate 
4281125Seschrock 	if (devi.devi_node_state < DS_ATTACHED) {
4290Sstevel@tonic-gate 		/* No driver attached to this devinfo - nothing to do. */
4300Sstevel@tonic-gate 		mdb_warn("%p: No driver attached to this devinfo node\n", addr);
4310Sstevel@tonic-gate 		return (DCMD_ERR);
4320Sstevel@tonic-gate 	}
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	if (mdb_devinfo2driver(addr, dname, sizeof (dname)) != 0) {
4350Sstevel@tonic-gate 		mdb_warn("failed to determine driver name");
4360Sstevel@tonic-gate 		return (DCMD_ERR);
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	mdb_printf("Driver '%s' is associated with devinfo %p.\n", dname, addr);
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	return (DCMD_OK);
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate typedef struct devnames_walk {
4460Sstevel@tonic-gate 	struct devnames *dnw_names;
4470Sstevel@tonic-gate 	int dnw_ndx;
4480Sstevel@tonic-gate 	int dnw_devcnt;
4490Sstevel@tonic-gate 	uintptr_t dnw_base;
4500Sstevel@tonic-gate 	uintptr_t dnw_size;
4510Sstevel@tonic-gate } devnames_walk_t;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate int
4540Sstevel@tonic-gate devnames_walk_init(mdb_walk_state_t *wsp)
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate 	devnames_walk_t *dnw;
4570Sstevel@tonic-gate 	int devcnt;
4580Sstevel@tonic-gate 	uintptr_t devnamesp;
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	if (wsp->walk_addr != NULL) {
4610Sstevel@tonic-gate 		mdb_warn("devnames walker only supports global walks\n");
4620Sstevel@tonic-gate 		return (WALK_ERR);
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	if (mdb_readvar(&devcnt, "devcnt") == -1) {
4660Sstevel@tonic-gate 		mdb_warn("failed to read 'devcnt'");
4670Sstevel@tonic-gate 		return (WALK_ERR);
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	if (mdb_readvar(&devnamesp, "devnamesp") == -1) {
4710Sstevel@tonic-gate 		mdb_warn("failed to read 'devnamesp'");
4720Sstevel@tonic-gate 		return (WALK_ERR);
4730Sstevel@tonic-gate 	}
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	dnw = mdb_zalloc(sizeof (devnames_walk_t), UM_SLEEP);
4760Sstevel@tonic-gate 	dnw->dnw_size = sizeof (struct devnames) * devcnt;
4770Sstevel@tonic-gate 	dnw->dnw_devcnt = devcnt;
4780Sstevel@tonic-gate 	dnw->dnw_base = devnamesp;
4790Sstevel@tonic-gate 	dnw->dnw_names = mdb_alloc(dnw->dnw_size, UM_SLEEP);
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	if (mdb_vread(dnw->dnw_names, dnw->dnw_size, dnw->dnw_base) == -1) {
4820Sstevel@tonic-gate 		mdb_warn("couldn't read devnames array at %p", devnamesp);
4830Sstevel@tonic-gate 		return (WALK_ERR);
4840Sstevel@tonic-gate 	}
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	wsp->walk_data = dnw;
4870Sstevel@tonic-gate 	return (WALK_NEXT);
4880Sstevel@tonic-gate }
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate int
4910Sstevel@tonic-gate devnames_walk_step(mdb_walk_state_t *wsp)
4920Sstevel@tonic-gate {
4930Sstevel@tonic-gate 	devnames_walk_t *dnw = wsp->walk_data;
4940Sstevel@tonic-gate 	int status;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	if (dnw->dnw_ndx == dnw->dnw_devcnt)
4970Sstevel@tonic-gate 		return (WALK_DONE);
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	status = wsp->walk_callback(dnw->dnw_ndx * sizeof (struct devnames) +
5000Sstevel@tonic-gate 	    dnw->dnw_base, &dnw->dnw_names[dnw->dnw_ndx], wsp->walk_cbdata);
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	dnw->dnw_ndx++;
5030Sstevel@tonic-gate 	return (status);
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate void
5070Sstevel@tonic-gate devnames_walk_fini(mdb_walk_state_t *wsp)
5080Sstevel@tonic-gate {
5090Sstevel@tonic-gate 	devnames_walk_t *dnw = wsp->walk_data;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	mdb_free(dnw->dnw_names, dnw->dnw_size);
5120Sstevel@tonic-gate 	mdb_free(dnw, sizeof (devnames_walk_t));
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate int
5160Sstevel@tonic-gate devinfo_siblings_walk_init(mdb_walk_state_t *wsp)
5170Sstevel@tonic-gate {
5180Sstevel@tonic-gate 	struct dev_info di;
5190Sstevel@tonic-gate 	uintptr_t addr = wsp->walk_addr;
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	if (addr == NULL) {
5220Sstevel@tonic-gate 		mdb_warn("a dev_info struct address must be provided\n");
5230Sstevel@tonic-gate 		return (WALK_ERR);
5240Sstevel@tonic-gate 	}
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	if (mdb_vread(&di, sizeof (di), addr) == -1) {
5270Sstevel@tonic-gate 		mdb_warn("failed to read dev_info struct at %p", addr);
5280Sstevel@tonic-gate 		return (WALK_ERR);
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	if (di.devi_parent == NULL) {
5320Sstevel@tonic-gate 		mdb_warn("no parent for devinfo at %p", addr);
5330Sstevel@tonic-gate 		return (WALK_DONE);
5340Sstevel@tonic-gate 	}
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	if (mdb_vread(&di, sizeof (di), (uintptr_t)di.devi_parent) == -1) {
5370Sstevel@tonic-gate 		mdb_warn("failed to read parent dev_info struct at %p",
5380Sstevel@tonic-gate 		    (uintptr_t)di.devi_parent);
5390Sstevel@tonic-gate 		return (WALK_ERR);
5400Sstevel@tonic-gate 	}
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)di.devi_child;
5430Sstevel@tonic-gate 	return (WALK_NEXT);
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate int
5470Sstevel@tonic-gate devinfo_siblings_walk_step(mdb_walk_state_t *wsp)
5480Sstevel@tonic-gate {
5490Sstevel@tonic-gate 	struct dev_info di;
5500Sstevel@tonic-gate 	uintptr_t addr = wsp->walk_addr;
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	if (addr == NULL)
5530Sstevel@tonic-gate 		return (WALK_DONE);
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	if (mdb_vread(&di, sizeof (di), addr) == -1) {
5560Sstevel@tonic-gate 		mdb_warn("failed to read dev_info struct at %p", addr);
5570Sstevel@tonic-gate 		return (WALK_DONE);
5580Sstevel@tonic-gate 	}
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)di.devi_sibling;
5610Sstevel@tonic-gate 	return (wsp->walk_callback(addr, &di, wsp->walk_cbdata));
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate int
5650Sstevel@tonic-gate devi_next_walk_step(mdb_walk_state_t *wsp)
5660Sstevel@tonic-gate {
5670Sstevel@tonic-gate 	struct dev_info di;
5680Sstevel@tonic-gate 	int status;
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	if (wsp->walk_addr == NULL)
5710Sstevel@tonic-gate 		return (WALK_DONE);
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	if (mdb_vread(&di, sizeof (di), wsp->walk_addr) == -1)
5740Sstevel@tonic-gate 		return (WALK_DONE);
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	status = wsp->walk_callback(wsp->walk_addr, &di, wsp->walk_cbdata);
5770Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)di.devi_next;
5780Sstevel@tonic-gate 	return (status);
5790Sstevel@tonic-gate }
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate /*
5820Sstevel@tonic-gate  * Helper functions.
5830Sstevel@tonic-gate  */
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate static int
5860Sstevel@tonic-gate is_printable_string(unsigned char *prop_value)
5870Sstevel@tonic-gate {
5880Sstevel@tonic-gate 	while (*prop_value != 0)
5890Sstevel@tonic-gate 		if (!isprint(*prop_value++))
5900Sstevel@tonic-gate 			return (0);
5910Sstevel@tonic-gate 	return (1);
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate static void
5950Sstevel@tonic-gate devinfo_print_props_type(int type) {
5960Sstevel@tonic-gate 	char *type_str = NULL;
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	switch (type) {
5990Sstevel@tonic-gate 	case DDI_PROP_TYPE_ANY:
6000Sstevel@tonic-gate 		type_str = "any";
6010Sstevel@tonic-gate 		break;
6020Sstevel@tonic-gate 	case DDI_PROP_TYPE_COMPOSITE:
6030Sstevel@tonic-gate 		type_str = "composite";
6040Sstevel@tonic-gate 		break;
6050Sstevel@tonic-gate 	case DDI_PROP_TYPE_INT64:
6060Sstevel@tonic-gate 		type_str = "int64";
6070Sstevel@tonic-gate 		break;
6080Sstevel@tonic-gate 	case DDI_PROP_TYPE_INT:
6090Sstevel@tonic-gate 		type_str = "int";
6100Sstevel@tonic-gate 		break;
6110Sstevel@tonic-gate 	case DDI_PROP_TYPE_BYTE:
6120Sstevel@tonic-gate 		type_str = "byte";
6130Sstevel@tonic-gate 		break;
6140Sstevel@tonic-gate 	case DDI_PROP_TYPE_STRING:
6150Sstevel@tonic-gate 		type_str = "string";
6160Sstevel@tonic-gate 		break;
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	if (type_str != NULL)
6200Sstevel@tonic-gate 		mdb_printf("type=%s", type_str);
6210Sstevel@tonic-gate 	else
6220Sstevel@tonic-gate 		mdb_printf("type=0x%x", type);
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate static void
6260Sstevel@tonic-gate devinfo_print_props_value(int elem_size, int nelem,
6270Sstevel@tonic-gate 	unsigned char *prop_value, int prop_value_len)
6280Sstevel@tonic-gate {
6290Sstevel@tonic-gate 	int i;
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	mdb_printf("value=");
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	if (elem_size == 0) {
6340Sstevel@tonic-gate 		/* if elem_size == 0, then we are printing out string(s) */
6350Sstevel@tonic-gate 		char *p = (char *)prop_value;
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 		for (i = 0; i < nelem - 1; i++) {
6380Sstevel@tonic-gate 			mdb_printf("'%s' + ", p);
6390Sstevel@tonic-gate 			p += strlen(p) + 1;
6400Sstevel@tonic-gate 		}
6410Sstevel@tonic-gate 		mdb_printf("'%s'", p);
6420Sstevel@tonic-gate 	} else {
6430Sstevel@tonic-gate 		/*
6440Sstevel@tonic-gate 		 * if elem_size != 0 then we are printing out an array
6450Sstevel@tonic-gate 		 * where each element is of elem_size
6460Sstevel@tonic-gate 		 */
6470Sstevel@tonic-gate 		mdb_nhconvert(prop_value, prop_value, elem_size);
6480Sstevel@tonic-gate 		mdb_printf("%02x", *prop_value);
6490Sstevel@tonic-gate 		for (i = 1; i < prop_value_len; i++) {
6500Sstevel@tonic-gate 			if ((i % elem_size) == 0) {
6510Sstevel@tonic-gate 				mdb_nhconvert(&prop_value[i],
6520Sstevel@tonic-gate 				    &prop_value[i], elem_size);
6530Sstevel@tonic-gate 				mdb_printf(".");
6540Sstevel@tonic-gate 			}
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 			mdb_printf("%02x", prop_value[i]);
6570Sstevel@tonic-gate 		}
6580Sstevel@tonic-gate 	}
6590Sstevel@tonic-gate }
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate /*
6620Sstevel@tonic-gate  * devinfo_print_props_guess()
6630Sstevel@tonic-gate  * Guesses how to interpret the value of the property
6640Sstevel@tonic-gate  *
6650Sstevel@tonic-gate  * Params:
6660Sstevel@tonic-gate  * 	type      - Should be the type value of the property
6670Sstevel@tonic-gate  * 	prop_val  - Pointer to the property value data buffer
6680Sstevel@tonic-gate  * 	prop_len  - Length of the property value data buffer
6690Sstevel@tonic-gate  *
6700Sstevel@tonic-gate  * Return values:
6710Sstevel@tonic-gate  * 	nelem     - The number of elements stored in the property value
6720Sstevel@tonic-gate  * 			data buffer pointed to by prop_val.
6730Sstevel@tonic-gate  * 	elem_size - The size (in bytes) of the elements stored in the property
6740Sstevel@tonic-gate  * 			value data buffer pointed to by prop_val.
6750Sstevel@tonic-gate  * 			Upon return if elem_size == 0 and nelem != 0 then
6760Sstevel@tonic-gate  * 			the property value data buffer contains strings
6770Sstevel@tonic-gate  * 	len_err   - There was an error with the length of the data buffer.
6780Sstevel@tonic-gate  * 			Its size is not a multiple of the array value type.
6790Sstevel@tonic-gate  * 			It will be interpreted as an array of bytes.
6800Sstevel@tonic-gate  */
6810Sstevel@tonic-gate static void
6820Sstevel@tonic-gate devinfo_print_props_guess(int type, unsigned char *prop_val, int prop_len,
6830Sstevel@tonic-gate     int *elem_size, int *nelem, int *len_err)
6840Sstevel@tonic-gate {
6850Sstevel@tonic-gate 	*len_err = 0;
6860Sstevel@tonic-gate 	if (prop_len == NULL) {
6870Sstevel@tonic-gate 		*elem_size = 0;
6880Sstevel@tonic-gate 		*nelem = 0;
6890Sstevel@tonic-gate 		return;
6900Sstevel@tonic-gate 	}
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	/* by default, assume an array of bytes */
6930Sstevel@tonic-gate 	*elem_size = 1;
6940Sstevel@tonic-gate 	*nelem = prop_len;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	switch (type) {
6970Sstevel@tonic-gate 	case DDI_PROP_TYPE_BYTE:
6980Sstevel@tonic-gate 		/* default case, that was easy */
6990Sstevel@tonic-gate 		break;
7000Sstevel@tonic-gate 	case DDI_PROP_TYPE_INT64:
7010Sstevel@tonic-gate 		if ((prop_len % sizeof (int64_t)) == 0) {
7020Sstevel@tonic-gate 			*elem_size = sizeof (int64_t);
7030Sstevel@tonic-gate 			*nelem = prop_len / *elem_size;
7040Sstevel@tonic-gate 		} else {
7050Sstevel@tonic-gate 			/* array is not a multiple of type size, error */
7060Sstevel@tonic-gate 			*len_err = 1;
7070Sstevel@tonic-gate 		}
7080Sstevel@tonic-gate 		break;
7090Sstevel@tonic-gate 	case DDI_PROP_TYPE_INT:
7100Sstevel@tonic-gate 		if ((prop_len % sizeof (int)) == 0) {
7110Sstevel@tonic-gate 			*elem_size = sizeof (int);
7120Sstevel@tonic-gate 			*nelem = prop_len / *elem_size;
7130Sstevel@tonic-gate 		} else {
7140Sstevel@tonic-gate 			/* array is not a multiple of type size, error */
7150Sstevel@tonic-gate 			*len_err = 1;
7160Sstevel@tonic-gate 		}
7170Sstevel@tonic-gate 		break;
7180Sstevel@tonic-gate 	case DDI_PROP_TYPE_STRING:
7190Sstevel@tonic-gate 	case DDI_PROP_TYPE_COMPOSITE:
7200Sstevel@tonic-gate 	case DDI_PROP_TYPE_ANY:
7210Sstevel@tonic-gate 	default:
7220Sstevel@tonic-gate 		/*
7230Sstevel@tonic-gate 		 * if we made it here the type is either unknown
7240Sstevel@tonic-gate 		 * or a string.  Try to interpret is as a string
7250Sstevel@tonic-gate 		 * and if that fails assume an array of bytes.
7260Sstevel@tonic-gate 		 */
7270Sstevel@tonic-gate 		if (prop_val[prop_len - 1] == '\0') {
7280Sstevel@tonic-gate 			unsigned char	*s = prop_val;
7290Sstevel@tonic-gate 			int		i;
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 			/* assume an array of strings */
7320Sstevel@tonic-gate 			*elem_size = 0;
7330Sstevel@tonic-gate 			*nelem = 0;
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 			for (i = 0; i < prop_len; i++) {
7360Sstevel@tonic-gate 				if (prop_val[i] != '\0')
7370Sstevel@tonic-gate 					continue;
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 				/*
7400Sstevel@tonic-gate 				 * If the property is typed as a string
7410Sstevel@tonic-gate 				 * property, then interpret empty strings
7420Sstevel@tonic-gate 				 * as strings. Otherwise default to an
7430Sstevel@tonic-gate 				 * array of bytes. If there are unprintable
7440Sstevel@tonic-gate 				 * characters, always default to an array of
7450Sstevel@tonic-gate 				 * bytes.
7460Sstevel@tonic-gate 				 */
7470Sstevel@tonic-gate 				if ((*s == '\0' && type !=
7480Sstevel@tonic-gate 				    DDI_PROP_TYPE_STRING) ||
7490Sstevel@tonic-gate 				    !is_printable_string(s)) {
7500Sstevel@tonic-gate 					*elem_size = 1;
7510Sstevel@tonic-gate 					*nelem = prop_len;
7520Sstevel@tonic-gate 					break;
7530Sstevel@tonic-gate 				}
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 				(*nelem)++;
7560Sstevel@tonic-gate 				s = &prop_val[i + 1];
7570Sstevel@tonic-gate 			}
7580Sstevel@tonic-gate 		}
7590Sstevel@tonic-gate 		break;
7600Sstevel@tonic-gate 	}
7610Sstevel@tonic-gate }
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate static void
7640Sstevel@tonic-gate devinfo_print_props(char *name, ddi_prop_t *p)
7650Sstevel@tonic-gate {
7660Sstevel@tonic-gate 	if (p == NULL)
7670Sstevel@tonic-gate 		return;
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	if (name != NULL)
7700Sstevel@tonic-gate 		mdb_printf("%s ", name);
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	mdb_printf("properties at %p:\n", p);
7730Sstevel@tonic-gate 	mdb_inc_indent(DEVINFO_PROP_INDENT);
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	while (p != NULL) {
7760Sstevel@tonic-gate 		ddi_prop_t	prop;
7770Sstevel@tonic-gate 		char		prop_name[128];
7780Sstevel@tonic-gate 		unsigned char	*prop_value;
7790Sstevel@tonic-gate 		int		type, elem_size, nelem, prop_len_error;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 		/* read in the property struct */
7820Sstevel@tonic-gate 		if (mdb_vread(&prop, sizeof (prop), (uintptr_t)p) == -1) {
7830Sstevel@tonic-gate 			mdb_warn("could not read property at 0x%p", p);
7840Sstevel@tonic-gate 			break;
7850Sstevel@tonic-gate 		}
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 		/* print the property name */
7880Sstevel@tonic-gate 		if (mdb_readstr(prop_name, sizeof (prop_name),
7890Sstevel@tonic-gate 		    (uintptr_t)prop.prop_name) == -1) {
7900Sstevel@tonic-gate 			mdb_warn("could not read property name at 0x%p",
7910Sstevel@tonic-gate 			    prop.prop_name);
7920Sstevel@tonic-gate 			goto next;
7930Sstevel@tonic-gate 		}
7940Sstevel@tonic-gate 		mdb_printf("name='%s' ", prop_name);
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 		/* get the property type and print it out */
7970Sstevel@tonic-gate 		type = (prop.prop_flags & DDI_PROP_TYPE_MASK);
7980Sstevel@tonic-gate 		devinfo_print_props_type(type);
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 		/* get the property value */
8010Sstevel@tonic-gate 		if (prop.prop_len > 0) {
8020Sstevel@tonic-gate 			prop_value = mdb_alloc(prop.prop_len, UM_SLEEP|UM_GC);
8030Sstevel@tonic-gate 			if (mdb_vread(prop_value, prop.prop_len,
8045839Sdmick 			    (uintptr_t)prop.prop_val) == -1) {
8050Sstevel@tonic-gate 				mdb_warn("could not read property value at "
8060Sstevel@tonic-gate 				    "0x%p", prop.prop_val);
8070Sstevel@tonic-gate 				goto next;
8080Sstevel@tonic-gate 			}
8090Sstevel@tonic-gate 		} else {
8100Sstevel@tonic-gate 			prop_value = NULL;
8110Sstevel@tonic-gate 		}
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 		/* take a guess at interpreting the property value */
8140Sstevel@tonic-gate 		devinfo_print_props_guess(type, prop_value, prop.prop_len,
8150Sstevel@tonic-gate 		    &elem_size, &nelem, &prop_len_error);
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 		/* print out the number ot items */
8180Sstevel@tonic-gate 		mdb_printf(" items=%d", nelem);
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 		/* print out any associated device information */
8210Sstevel@tonic-gate 		if (prop.prop_dev != DDI_DEV_T_NONE) {
8220Sstevel@tonic-gate 			mdb_printf(" dev=");
8230Sstevel@tonic-gate 			if (prop.prop_dev == DDI_DEV_T_ANY)
8240Sstevel@tonic-gate 				mdb_printf("any");
8250Sstevel@tonic-gate 			else if (prop.prop_dev == DDI_MAJOR_T_UNKNOWN)
8260Sstevel@tonic-gate 				mdb_printf("unknown");
8270Sstevel@tonic-gate 			else
8280Sstevel@tonic-gate 				mdb_printf("(%u,%u)",
8290Sstevel@tonic-gate 				    getmajor(prop.prop_dev),
8300Sstevel@tonic-gate 				    getminor(prop.prop_dev));
8310Sstevel@tonic-gate 		}
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 		/* print out the property value */
8340Sstevel@tonic-gate 		if (prop_value != NULL) {
8350Sstevel@tonic-gate 			mdb_printf("\n");
8360Sstevel@tonic-gate 			mdb_inc_indent(DEVINFO_PROP_INDENT);
8370Sstevel@tonic-gate 			if (prop_len_error)
8380Sstevel@tonic-gate 				mdb_printf("NOTE: prop length is not a "
8390Sstevel@tonic-gate 				    "multiple of element size\n");
8400Sstevel@tonic-gate 			devinfo_print_props_value(elem_size, nelem,
8410Sstevel@tonic-gate 			    prop_value, prop.prop_len);
8420Sstevel@tonic-gate 			mdb_dec_indent(DEVINFO_PROP_INDENT);
8430Sstevel@tonic-gate 		}
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate next:
8460Sstevel@tonic-gate 		mdb_printf("\n");
8470Sstevel@tonic-gate 		p = prop.prop_next;
8480Sstevel@tonic-gate 	}
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	mdb_dec_indent(DEVINFO_PROP_INDENT);
8510Sstevel@tonic-gate }
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate static void
8540Sstevel@tonic-gate devinfo_pathinfo_state(mdi_pathinfo_state_t state) {
8550Sstevel@tonic-gate 	char *type_str = NULL;
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	switch (state) {
8580Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_INIT:
8590Sstevel@tonic-gate 		type_str = "init";
8600Sstevel@tonic-gate 		break;
8610Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_ONLINE:
8620Sstevel@tonic-gate 		type_str = "online";
8630Sstevel@tonic-gate 		break;
8640Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_STANDBY:
8650Sstevel@tonic-gate 		type_str = "standby";
8660Sstevel@tonic-gate 		break;
8670Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_FAULT:
8680Sstevel@tonic-gate 		type_str = "fault";
8690Sstevel@tonic-gate 		break;
8700Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_OFFLINE:
8710Sstevel@tonic-gate 		type_str = "offline";
8720Sstevel@tonic-gate 		break;
8730Sstevel@tonic-gate 	}
8740Sstevel@tonic-gate 	if (type_str != NULL)
8750Sstevel@tonic-gate 		mdb_printf("state=%s\n", type_str);
8760Sstevel@tonic-gate 	else
8770Sstevel@tonic-gate 		mdb_printf("state=0x%x\n", state);
8780Sstevel@tonic-gate }
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate static void
8810Sstevel@tonic-gate devinfo_print_pathing(int mdi_component, void *mdi_client) {
8820Sstevel@tonic-gate 	mdi_client_t		mdi_c;
8830Sstevel@tonic-gate 	struct mdi_pathinfo	*pip;
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	/* we only print out multipathing info for client nodes */
8860Sstevel@tonic-gate 	if ((mdi_component & MDI_COMPONENT_CLIENT) == 0)
8870Sstevel@tonic-gate 		return;
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	mdb_printf("Client multipath info at: 0x%p\n", mdi_client);
8900Sstevel@tonic-gate 	mdb_inc_indent(DEVINFO_PROP_INDENT);
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	/* read in the client multipathing info */
8930Sstevel@tonic-gate 	if (mdb_readstr((void*) &mdi_c, sizeof (mdi_c),
8940Sstevel@tonic-gate 	    (uintptr_t)mdi_client) == -1) {
8950Sstevel@tonic-gate 		mdb_warn("failed to read mdi_client at %p",
8960Sstevel@tonic-gate 		    (uintptr_t)mdi_client);
8970Sstevel@tonic-gate 		goto exit;
8980Sstevel@tonic-gate 	}
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	/*
9010Sstevel@tonic-gate 	 * walk through the clients list of pathinfo structures and print
9020Sstevel@tonic-gate 	 * out the properties for each path
9030Sstevel@tonic-gate 	 */
9040Sstevel@tonic-gate 	pip = (struct mdi_pathinfo *)mdi_c.ct_path_head;
9050Sstevel@tonic-gate 	while (pip != NULL) {
9060Sstevel@tonic-gate 		char			binding_name[128];
9070Sstevel@tonic-gate 		struct mdi_pathinfo	pi;
9080Sstevel@tonic-gate 		mdi_phci_t		ph;
9090Sstevel@tonic-gate 		struct dev_info		ph_di;
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 		/* read in the pathinfo structure */
9120Sstevel@tonic-gate 		if (mdb_vread((void*)&pi, sizeof (pi),
9130Sstevel@tonic-gate 			    (uintptr_t)pip) == -1) {
9140Sstevel@tonic-gate 			mdb_warn("failed to read mdi_pathinfo at %p",
9150Sstevel@tonic-gate 			    (uintptr_t)pip);
9160Sstevel@tonic-gate 			goto exit;
9170Sstevel@tonic-gate 		}
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 		/* read in the pchi (path host adapter) info */
9200Sstevel@tonic-gate 		if (mdb_vread((void*)&ph, sizeof (ph),
9210Sstevel@tonic-gate 			    (uintptr_t)pi.pi_phci) == -1) {
9220Sstevel@tonic-gate 			mdb_warn("failed to read mdi_pchi at %p",
9230Sstevel@tonic-gate 			    (uintptr_t)pi.pi_phci);
9240Sstevel@tonic-gate 			goto exit;
9250Sstevel@tonic-gate 		}
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 		/* read in the dip of the phci so we can get it's name */
9280Sstevel@tonic-gate 		if (mdb_vread((void*)&ph_di, sizeof (ph_di),
9290Sstevel@tonic-gate 			    (uintptr_t)ph.ph_dip) == -1) {
9300Sstevel@tonic-gate 			mdb_warn("failed to read mdi_pchi at %p",
9310Sstevel@tonic-gate 			    (uintptr_t)ph.ph_dip);
9320Sstevel@tonic-gate 			goto exit;
9330Sstevel@tonic-gate 		}
9340Sstevel@tonic-gate 		if (mdb_vread(binding_name, sizeof (binding_name),
9350Sstevel@tonic-gate 			    (uintptr_t)ph_di.devi_binding_name) == -1) {
9360Sstevel@tonic-gate 			mdb_warn("failed to read binding_name at %p",
9370Sstevel@tonic-gate 			    (uintptr_t)ph_di.devi_binding_name);
9380Sstevel@tonic-gate 			goto exit;
9390Sstevel@tonic-gate 		}
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 		mdb_printf("%s#%d, ", binding_name, ph_di.devi_instance);
9420Sstevel@tonic-gate 		devinfo_pathinfo_state(pi.pi_state);
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 		/* print out the pathing info */
9450Sstevel@tonic-gate 		mdb_inc_indent(DEVINFO_PROP_INDENT);
9460Sstevel@tonic-gate 		if (mdb_pwalk_dcmd(NVPAIR_WALKER_FQNAME, NVPAIR_DCMD_FQNAME,
9470Sstevel@tonic-gate 			    0, NULL, (uintptr_t)pi.pi_prop) != 0) {
9480Sstevel@tonic-gate 			mdb_dec_indent(DEVINFO_PROP_INDENT);
9490Sstevel@tonic-gate 			goto exit;
9500Sstevel@tonic-gate 		}
9510Sstevel@tonic-gate 		mdb_dec_indent(DEVINFO_PROP_INDENT);
9520Sstevel@tonic-gate 		pip = pi.pi_client_link;
9530Sstevel@tonic-gate 	}
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate exit:
9560Sstevel@tonic-gate 	mdb_dec_indent(DEVINFO_PROP_INDENT);
9570Sstevel@tonic-gate }
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate typedef struct devinfo_cb_data {
9600Sstevel@tonic-gate 	uintptr_t	di_base;
9610Sstevel@tonic-gate 	uint_t		di_flags;
9620Sstevel@tonic-gate } devinfo_cb_data_t;
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate static int
9650Sstevel@tonic-gate devinfo_print(uintptr_t addr, struct dev_info *dev, devinfo_cb_data_t *data)
9660Sstevel@tonic-gate {
9670Sstevel@tonic-gate 	/*
9680Sstevel@tonic-gate 	 * We know the walker passes us extra data after the dev_info.
9690Sstevel@tonic-gate 	 */
9700Sstevel@tonic-gate 	char		binding_name[128];
9711125Seschrock 	char		dname[MODMAXNAMELEN + 1];
9720Sstevel@tonic-gate 	devinfo_node_t	*din = (devinfo_node_t *)dev;
9730Sstevel@tonic-gate 	ddi_prop_t	*global_props = NULL;
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 	if (mdb_readstr(binding_name, sizeof (binding_name),
9760Sstevel@tonic-gate 	    (uintptr_t)dev->devi_binding_name) == -1) {
9770Sstevel@tonic-gate 		mdb_warn("failed to read binding_name at %p",
9780Sstevel@tonic-gate 		    (uintptr_t)dev->devi_binding_name);
9790Sstevel@tonic-gate 		return (WALK_ERR);
9800Sstevel@tonic-gate 	}
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	/* if there are any global properties, get a pointer to them */
9830Sstevel@tonic-gate 	if (dev->devi_global_prop_list != NULL) {
9840Sstevel@tonic-gate 		ddi_prop_list_t	plist;
9850Sstevel@tonic-gate 		if (mdb_vread((void*)&plist, sizeof (plist),
9865839Sdmick 		    (uintptr_t)dev->devi_global_prop_list) == -1) {
9870Sstevel@tonic-gate 			mdb_warn("failed to read global prop_list at %p",
9880Sstevel@tonic-gate 			    (uintptr_t)dev->devi_global_prop_list);
9890Sstevel@tonic-gate 			return (WALK_ERR);
9900Sstevel@tonic-gate 		}
9910Sstevel@tonic-gate 		global_props = plist.prop_list;
9920Sstevel@tonic-gate 	}
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	mdb_inc_indent(din->din_depth * DEVINFO_TREE_INDENT);
9950Sstevel@tonic-gate 	if ((addr == data->di_base) || (data->di_flags & DEVINFO_ALLBOLD))
9960Sstevel@tonic-gate 		mdb_printf("%<b>");
9970Sstevel@tonic-gate 	mdb_printf("%-0?p %s", addr, binding_name);
9980Sstevel@tonic-gate 	if ((addr == data->di_base) || (data->di_flags & DEVINFO_ALLBOLD))
9990Sstevel@tonic-gate 		mdb_printf("%</b>");
10000Sstevel@tonic-gate 	if (dev->devi_instance >= 0)
10010Sstevel@tonic-gate 		mdb_printf(", instance #%d", dev->devi_instance);
10021125Seschrock 
10031125Seschrock 	if (dev->devi_node_state < DS_ATTACHED)
10040Sstevel@tonic-gate 		mdb_printf(" (driver not attached)");
10051125Seschrock 	else if (mdb_devinfo2driver(addr, dname, sizeof (dname)) != 0)
10061125Seschrock 		mdb_printf(" (could not determine driver name)");
10071125Seschrock 	else
10081125Seschrock 		mdb_printf(" (driver name: %s)", dname);
10091125Seschrock 
10100Sstevel@tonic-gate 	mdb_printf("\n");
10110Sstevel@tonic-gate 	if (data->di_flags & DEVINFO_VERBOSE) {
10120Sstevel@tonic-gate 		mdb_inc_indent(DEVINFO_PROPLIST_INDENT);
10130Sstevel@tonic-gate 		devinfo_print_props("System", dev->devi_sys_prop_ptr);
10140Sstevel@tonic-gate 		devinfo_print_props("Driver", dev->devi_drv_prop_ptr);
10150Sstevel@tonic-gate 		devinfo_print_props("Hardware", dev->devi_hw_prop_ptr);
10160Sstevel@tonic-gate 		devinfo_print_props("Global", global_props);
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 		devinfo_print_pathing(dev->devi_mdi_component,
10190Sstevel@tonic-gate 		    dev->devi_mdi_client);
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 		mdb_dec_indent(DEVINFO_PROPLIST_INDENT);
10220Sstevel@tonic-gate 	}
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 	mdb_dec_indent(din->din_depth * DEVINFO_TREE_INDENT);
10250Sstevel@tonic-gate 	return (WALK_NEXT);
10260Sstevel@tonic-gate }
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate /*ARGSUSED*/
10290Sstevel@tonic-gate int
10300Sstevel@tonic-gate prtconf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
10310Sstevel@tonic-gate {
10320Sstevel@tonic-gate 	devinfo_cb_data_t data;
10335839Sdmick 	uintptr_t devinfo_root;		/* Address of root of devinfo tree */
10340Sstevel@tonic-gate 	int status;
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 	data.di_flags = DEVINFO_PARENT | DEVINFO_CHILD;
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 	if (mdb_getopts(argc, argv,
10390Sstevel@tonic-gate 	    'v', MDB_OPT_SETBITS, DEVINFO_VERBOSE, &data.di_flags,
10400Sstevel@tonic-gate 	    'p', MDB_OPT_CLRBITS, DEVINFO_CHILD, &data.di_flags,
10410Sstevel@tonic-gate 	    'c', MDB_OPT_CLRBITS, DEVINFO_PARENT, &data.di_flags, NULL) != argc)
10420Sstevel@tonic-gate 		return (DCMD_USAGE);
10430Sstevel@tonic-gate 
10445839Sdmick 	if (mdb_readvar(&devinfo_root, "top_devinfo") == -1) {
10455839Sdmick 		mdb_warn("failed to read 'top_devinfo'");
10465839Sdmick 		return (NULL);
10475839Sdmick 	}
10485839Sdmick 
10490Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) == 0) {
10500Sstevel@tonic-gate 		addr = devinfo_root;
10510Sstevel@tonic-gate 		if (data.di_flags & DEVINFO_VERBOSE)
10520Sstevel@tonic-gate 			data.di_flags |= DEVINFO_ALLBOLD;
10530Sstevel@tonic-gate 	}
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	data.di_base = addr;
10560Sstevel@tonic-gate 	mdb_printf("%<u>%-?s %-50s%</u>\n", "DEVINFO", "NAME");
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	if ((data.di_flags & (DEVINFO_PARENT | DEVINFO_CHILD)) ==
10590Sstevel@tonic-gate 	    (DEVINFO_PARENT | DEVINFO_CHILD)) {
10600Sstevel@tonic-gate 		status = mdb_pwalk("devinfo",
10610Sstevel@tonic-gate 		    (mdb_walk_cb_t)devinfo_print, &data, addr);
10620Sstevel@tonic-gate 	} else if (data.di_flags & DEVINFO_PARENT) {
10630Sstevel@tonic-gate 		status = mdb_pwalk("devinfo_parents",
10640Sstevel@tonic-gate 		    (mdb_walk_cb_t)devinfo_print, &data, addr);
10650Sstevel@tonic-gate 	} else if (data.di_flags & DEVINFO_CHILD) {
10660Sstevel@tonic-gate 		status = mdb_pwalk("devinfo_children",
10670Sstevel@tonic-gate 		    (mdb_walk_cb_t)devinfo_print, &data, addr);
10680Sstevel@tonic-gate 	} else {
10690Sstevel@tonic-gate 		devinfo_node_t din;
10700Sstevel@tonic-gate 		if (mdb_vread(&din.din_dev, sizeof (din.din_dev), addr) == -1) {
10710Sstevel@tonic-gate 			mdb_warn("failed to read device");
10720Sstevel@tonic-gate 			return (DCMD_ERR);
10730Sstevel@tonic-gate 		}
10740Sstevel@tonic-gate 		din.din_depth = 0;
10750Sstevel@tonic-gate 		return (devinfo_print(addr, (struct dev_info *)&din, &data));
10760Sstevel@tonic-gate 	}
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 	if (status == -1) {
10790Sstevel@tonic-gate 		mdb_warn("couldn't walk devinfo tree");
10800Sstevel@tonic-gate 		return (DCMD_ERR);
10810Sstevel@tonic-gate 	}
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	return (DCMD_OK);
10840Sstevel@tonic-gate }
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate /*ARGSUSED*/
10870Sstevel@tonic-gate int
10880Sstevel@tonic-gate devinfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
10890Sstevel@tonic-gate {
10900Sstevel@tonic-gate 	char tmpstr[MODMAXNAMELEN];
10910Sstevel@tonic-gate 	char nodename[MODMAXNAMELEN];
10924145Scth 	char bindname[MAXPATHLEN];
10930Sstevel@tonic-gate 	int size, length;
10940Sstevel@tonic-gate 	struct dev_info devi;
10950Sstevel@tonic-gate 	devinfo_node_t din;
10960Sstevel@tonic-gate 	devinfo_cb_data_t data;
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	static const mdb_bitmask_t devi_state_masks[] = {
10990Sstevel@tonic-gate 	    { "DEVICE_OFFLINE",	DEVI_DEVICE_OFFLINE,	DEVI_DEVICE_OFFLINE },
11000Sstevel@tonic-gate 	    { "DEVICE_DOWN",	DEVI_DEVICE_DOWN,	DEVI_DEVICE_DOWN },
11010Sstevel@tonic-gate 	    { "DEVICE_DEGRADED", DEVI_DEVICE_DEGRADED,	DEVI_DEVICE_DEGRADED },
11024145Scth 	    { "DEVICE_REMOVED", DEVI_DEVICE_REMOVED,	DEVI_DEVICE_REMOVED },
11030Sstevel@tonic-gate 	    { "BUS_QUIESCED",	DEVI_BUS_QUIESCED,	DEVI_BUS_QUIESCED },
11040Sstevel@tonic-gate 	    { "BUS_DOWN",	DEVI_BUS_DOWN,		DEVI_BUS_DOWN },
11050Sstevel@tonic-gate 	    { "NDI_CONFIG",	DEVI_NDI_CONFIG,	DEVI_NDI_CONFIG	},
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 	    { "S_ATTACHING",	DEVI_S_ATTACHING,	DEVI_S_ATTACHING },
11080Sstevel@tonic-gate 	    { "S_DETACHING",	DEVI_S_DETACHING,	DEVI_S_DETACHING },
11090Sstevel@tonic-gate 	    { "S_ONLINING",	DEVI_S_ONLINING,	DEVI_S_ONLINING },
11100Sstevel@tonic-gate 	    { "S_OFFLINING",	DEVI_S_OFFLINING,	DEVI_S_OFFLINING },
11110Sstevel@tonic-gate 	    { "S_INVOKING_DACF", DEVI_S_INVOKING_DACF,	DEVI_S_INVOKING_DACF },
11120Sstevel@tonic-gate 	    { "S_UNBOUND",	DEVI_S_UNBOUND,		DEVI_S_UNBOUND },
11130Sstevel@tonic-gate 	    { "S_MD_UPDATE",	DEVI_S_MD_UPDATE,	DEVI_S_MD_UPDATE },
11140Sstevel@tonic-gate 	    { "S_REPORT",	DEVI_S_REPORT,		DEVI_S_REPORT },
11150Sstevel@tonic-gate 	    { "S_EVADD",	DEVI_S_EVADD,		DEVI_S_EVADD },
11160Sstevel@tonic-gate 	    { "S_EVREMOVE",	DEVI_S_EVREMOVE,	DEVI_S_EVREMOVE },
11174145Scth 	    { "S_NEED_RESET",	DEVI_S_NEED_RESET,	DEVI_S_NEED_RESET },
11180Sstevel@tonic-gate 	    { NULL,		0,			0 }
11190Sstevel@tonic-gate 	};
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	static const mdb_bitmask_t devi_flags_masks[] = {
11220Sstevel@tonic-gate 	    { "BUSY",		DEVI_BUSY,		DEVI_BUSY },
11230Sstevel@tonic-gate 	    { "MADE_CHILDREN",	DEVI_MADE_CHILDREN,	DEVI_MADE_CHILDREN },
11240Sstevel@tonic-gate 	    { "ATTACHED_CHILDREN",
11250Sstevel@tonic-gate 				DEVI_ATTACHED_CHILDREN,	DEVI_ATTACHED_CHILDREN},
11260Sstevel@tonic-gate 	    { "BRANCH_HELD",	DEVI_BRANCH_HELD,	DEVI_BRANCH_HELD },
11274145Scth 	    { "NO_BIND",	DEVI_NO_BIND,		DEVI_NO_BIND },
11284145Scth 	    { "DEVI_REGISTERED_DEVID",
11294145Scth 				DEVI_REGISTERED_DEVID,	DEVI_REGISTERED_DEVID },
11304145Scth 	    { "PHCI_SIGNALS_VHCI",
11314145Scth 				DEVI_PHCI_SIGNALS_VHCI,
11324145Scth 				DEVI_PHCI_SIGNALS_VHCI },
11334145Scth 	    { "REBIND",		DEVI_REBIND,		DEVI_REBIND },
11340Sstevel@tonic-gate 	    { NULL,		0,			0 }
11350Sstevel@tonic-gate 	};
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	data.di_flags = DEVINFO_VERBOSE;
11380Sstevel@tonic-gate 	data.di_base = addr;
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 	if (mdb_getopts(argc, argv,
11410Sstevel@tonic-gate 	    'q', MDB_OPT_CLRBITS, DEVINFO_VERBOSE, &data.di_flags,
11420Sstevel@tonic-gate 	    's', MDB_OPT_SETBITS, DEVINFO_SUMMARY, &data.di_flags, NULL)
11430Sstevel@tonic-gate 	    != argc)
11440Sstevel@tonic-gate 		return (DCMD_USAGE);
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) == 0) {
11470Sstevel@tonic-gate 		mdb_warn(
11480Sstevel@tonic-gate 		    "devinfo doesn't give global information (try prtconf)\n");
11490Sstevel@tonic-gate 		return (DCMD_ERR);
11500Sstevel@tonic-gate 	}
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags) && data.di_flags & DEVINFO_SUMMARY)
11530Sstevel@tonic-gate 		mdb_printf(
11540Sstevel@tonic-gate 		    "%-?s %5s %?s %-20s %-s\n"
11550Sstevel@tonic-gate 		    "%-?s %5s %?s %-20s %-s\n"
11560Sstevel@tonic-gate 		    "%<u>%-?s %5s %?s %-20s %-15s%</u>\n",
11570Sstevel@tonic-gate 		    "DEVINFO", "MAJ",  "REFCNT",   "NODENAME", "NODESTATE",
11580Sstevel@tonic-gate 		    "",        "INST", "CIRCULAR", "BINDNAME", "STATE",
11590Sstevel@tonic-gate 		    "",        "",     "THREAD",   "",         "FLAGS");
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	if (mdb_vread(&devi, sizeof (devi), addr) == -1) {
11620Sstevel@tonic-gate 		mdb_warn("failed to read device");
11630Sstevel@tonic-gate 		return (DCMD_ERR);
11640Sstevel@tonic-gate 	}
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 	if (data.di_flags & DEVINFO_SUMMARY) {
11670Sstevel@tonic-gate 		*nodename = '\0';
11680Sstevel@tonic-gate 		size = sizeof (nodename);
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 		if ((length = mdb_readstr(tmpstr, size,
11710Sstevel@tonic-gate 		    (uintptr_t)devi.devi_node_name)) > 0) {
11720Sstevel@tonic-gate 			strcat(nodename, tmpstr);
11730Sstevel@tonic-gate 			size -= length;
11740Sstevel@tonic-gate 		}
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 		if (devi.devi_addr != NULL && mdb_readstr(tmpstr, size - 1,
11770Sstevel@tonic-gate 		    (uintptr_t)devi.devi_addr) > 0) {
11780Sstevel@tonic-gate 			strcat(nodename, "@");
11790Sstevel@tonic-gate 			strcat(nodename, tmpstr);
11800Sstevel@tonic-gate 		}
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 		if (mdb_readstr(bindname, sizeof (bindname),
11830Sstevel@tonic-gate 		    (uintptr_t)devi.devi_binding_name) == -1)
11840Sstevel@tonic-gate 			*bindname = '\0';
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 		mdb_printf("%0?p %5d %?d %-20s %s\n",
11870Sstevel@tonic-gate 		    addr, devi.devi_major, devi.devi_ref, nodename,
11880Sstevel@tonic-gate 		    di_state[MIN(devi.devi_node_state + 1, DI_STATE_MAX)]);
11890Sstevel@tonic-gate 		mdb_printf("%?s %5d %?d %-20s <%b>\n",
11900Sstevel@tonic-gate 		    "", devi.devi_instance, devi.devi_circular, bindname,
11910Sstevel@tonic-gate 		    devi.devi_state, devi_state_masks);
11920Sstevel@tonic-gate 		mdb_printf("%?s %5s %?p %-20s <%b>\n\n",
11930Sstevel@tonic-gate 		    "", "", devi.devi_busy_thread, "",
11940Sstevel@tonic-gate 		    devi.devi_flags, devi_flags_masks);
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 		return (DCMD_OK);
11970Sstevel@tonic-gate 	} else {
11980Sstevel@tonic-gate 		din.din_dev = devi;
11990Sstevel@tonic-gate 		din.din_depth = 0;
12000Sstevel@tonic-gate 		return (devinfo_print(addr, (struct dev_info *)&din, &data));
12010Sstevel@tonic-gate 	}
12020Sstevel@tonic-gate }
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate /*ARGSUSED*/
12050Sstevel@tonic-gate int
12060Sstevel@tonic-gate m2d_walk_dinfo(uintptr_t addr, struct dev_info *di, char *mod_name)
12070Sstevel@tonic-gate {
12080Sstevel@tonic-gate 	char name[MODMAXNAMELEN];
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	if (mdb_readstr(name, MODMAXNAMELEN,
12110Sstevel@tonic-gate 	    (uintptr_t)di->devi_binding_name) == -1) {
12120Sstevel@tonic-gate 		mdb_warn("couldn't read devi_binding_name at %p",
12130Sstevel@tonic-gate 		    di->devi_binding_name);
12140Sstevel@tonic-gate 		return (WALK_ERR);
12150Sstevel@tonic-gate 	}
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 	if (strcmp(name, mod_name) == 0)
12180Sstevel@tonic-gate 		mdb_printf("%p\n", addr);
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 	return (WALK_NEXT);
12210Sstevel@tonic-gate }
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate /*ARGSUSED*/
12240Sstevel@tonic-gate int
12250Sstevel@tonic-gate modctl2devinfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
12260Sstevel@tonic-gate {
12270Sstevel@tonic-gate 	struct modctl modctl;
12280Sstevel@tonic-gate 	char name[MODMAXNAMELEN];
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
12310Sstevel@tonic-gate 		return (DCMD_USAGE);
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	if (mdb_vread(&modctl, sizeof (modctl), addr) == -1) {
12340Sstevel@tonic-gate 		mdb_warn("couldn't read modctl at %p", addr);
12350Sstevel@tonic-gate 		return (DCMD_ERR);
12360Sstevel@tonic-gate 	}
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	if (mdb_readstr(name, MODMAXNAMELEN,
12390Sstevel@tonic-gate 	    (uintptr_t)modctl.mod_modname) == -1) {
12400Sstevel@tonic-gate 		mdb_warn("couldn't read modname at %p", modctl.mod_modname);
12410Sstevel@tonic-gate 		return (DCMD_ERR);
12420Sstevel@tonic-gate 	}
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 	if (mdb_walk("devinfo", (mdb_walk_cb_t)m2d_walk_dinfo, name) == -1) {
12450Sstevel@tonic-gate 		mdb_warn("couldn't walk devinfo");
12460Sstevel@tonic-gate 		return (DCMD_ERR);
12470Sstevel@tonic-gate 	}
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 	return (DCMD_OK);
12500Sstevel@tonic-gate }
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate static int
12530Sstevel@tonic-gate major_to_addr(major_t major, uintptr_t *vaddr)
12540Sstevel@tonic-gate {
12550Sstevel@tonic-gate 	uint_t devcnt;
12560Sstevel@tonic-gate 	uintptr_t devnamesp;
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	if (mdb_readvar(&devcnt, "devcnt") == -1) {
12590Sstevel@tonic-gate 		mdb_warn("failed to read 'devcnt'");
12600Sstevel@tonic-gate 		return (-1);
12610Sstevel@tonic-gate 	}
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 	if (mdb_readvar(&devnamesp, "devnamesp") == -1) {
12640Sstevel@tonic-gate 		mdb_warn("failed to read 'devnamesp'");
12650Sstevel@tonic-gate 		return (-1);
12660Sstevel@tonic-gate 	}
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 	if (major >= devcnt) {
12690Sstevel@tonic-gate 		mdb_warn("%x is out of range [0x0-0x%x]\n", major, devcnt - 1);
12700Sstevel@tonic-gate 		return (-1);
12710Sstevel@tonic-gate 	}
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	*vaddr = devnamesp + (major * sizeof (struct devnames));
12740Sstevel@tonic-gate 	return (0);
12750Sstevel@tonic-gate }
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate /*ARGSUSED*/
12780Sstevel@tonic-gate int
12790Sstevel@tonic-gate devnames(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
12800Sstevel@tonic-gate {
12810Sstevel@tonic-gate 	static const mdb_bitmask_t dn_flag_bits[] = {
12820Sstevel@tonic-gate 		{ "DN_CONF_PARSED",	DN_CONF_PARSED, DN_CONF_PARSED },
12830Sstevel@tonic-gate 		{ "DN_DRIVER_BUSY",	DN_DRIVER_BUSY, DN_DRIVER_BUSY },
12840Sstevel@tonic-gate 		{ "DN_DRIVER_HELD",	DN_DRIVER_HELD, DN_DRIVER_HELD },
12850Sstevel@tonic-gate 		{ "DN_TAKEN_GETUDEV",	DN_TAKEN_GETUDEV, DN_TAKEN_GETUDEV },
12860Sstevel@tonic-gate 		{ "DN_DRIVER_REMOVED",	DN_DRIVER_REMOVED, DN_DRIVER_REMOVED},
12870Sstevel@tonic-gate 		{ "DN_FORCE_ATTACH",	DN_FORCE_ATTACH, DN_FORCE_ATTACH},
12880Sstevel@tonic-gate 		{ "DN_LEAF_DRIVER",	DN_LEAF_DRIVER, DN_LEAF_DRIVER},
12890Sstevel@tonic-gate 		{ "DN_NETWORK_DRIVER",	DN_NETWORK_DRIVER, DN_NETWORK_DRIVER},
12900Sstevel@tonic-gate 		{ "DN_NO_AUTODETACH",	DN_NO_AUTODETACH, DN_NO_AUTODETACH },
1291385Scth 		{ "DN_GLDV3_DRIVER",	DN_GLDV3_DRIVER, DN_GLDV3_DRIVER},
12921993Sramat 		{ "DN_PHCI_DRIVER",	DN_PHCI_DRIVER, DN_PHCI_DRIVER},
1293*6640Scth 		{ "DN_OPEN_RETURNS_EINTR", \
1294*6640Scth 				DN_OPEN_RETURNS_EINTR, DN_OPEN_RETURNS_EINTR},
1295*6640Scth 		{ "DN_SCSI_SIZE_CLEAN",	DN_SCSI_SIZE_CLEAN, DN_SCSI_SIZE_CLEAN},
12960Sstevel@tonic-gate 		{ NULL, 0, 0 }
12970Sstevel@tonic-gate 	};
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 	const mdb_arg_t *argp = NULL;
13000Sstevel@tonic-gate 	uint_t opt_v = FALSE, opt_m = FALSE;
13010Sstevel@tonic-gate 	major_t major;
13020Sstevel@tonic-gate 	size_t i;
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 	char name[MODMAXNAMELEN + 1];
13050Sstevel@tonic-gate 	struct devnames dn;
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 	if ((i = mdb_getopts(argc, argv,
13080Sstevel@tonic-gate 	    'm', MDB_OPT_SETBITS, TRUE, &opt_m,
13090Sstevel@tonic-gate 	    'v', MDB_OPT_SETBITS, TRUE, &opt_v,
13100Sstevel@tonic-gate 	    NULL)) != argc) {
13110Sstevel@tonic-gate 		if (argc - i > 1)
13120Sstevel@tonic-gate 			return (DCMD_USAGE);
13130Sstevel@tonic-gate 		argp = &argv[i];
13140Sstevel@tonic-gate 	}
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 	if (opt_m) {
13170Sstevel@tonic-gate 		if (!(flags & DCMD_ADDRSPEC))
13180Sstevel@tonic-gate 			return (DCMD_USAGE);
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 		if (major_to_addr(addr, &addr) == -1)
13210Sstevel@tonic-gate 			return (DCMD_ERR);
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	} else if (!(flags & DCMD_ADDRSPEC)) {
13240Sstevel@tonic-gate 		if (argp == NULL) {
13250Sstevel@tonic-gate 			if (mdb_walk_dcmd("devnames", "devnames", argc, argv)) {
13260Sstevel@tonic-gate 				mdb_warn("failed to walk devnames");
13270Sstevel@tonic-gate 				return (DCMD_ERR);
13280Sstevel@tonic-gate 			}
13290Sstevel@tonic-gate 			return (DCMD_OK);
13300Sstevel@tonic-gate 		}
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 		if (argp->a_type == MDB_TYPE_IMMEDIATE)
13330Sstevel@tonic-gate 			major = (major_t)argp->a_un.a_val;
13340Sstevel@tonic-gate 		else
13350Sstevel@tonic-gate 			major = (major_t)mdb_strtoull(argp->a_un.a_str);
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 		if (major_to_addr(major, &addr) == -1)
13380Sstevel@tonic-gate 			return (DCMD_ERR);
13390Sstevel@tonic-gate 	}
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 	if (mdb_vread(&dn, sizeof (struct devnames), addr) == -1) {
13420Sstevel@tonic-gate 		mdb_warn("failed to read devnames struct at %p", addr);
13430Sstevel@tonic-gate 		return (DCMD_ERR);
13440Sstevel@tonic-gate 	}
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags)) {
13470Sstevel@tonic-gate 		if (opt_v)
13480Sstevel@tonic-gate 			mdb_printf("%<u>%-16s%</u>\n", "NAME");
13490Sstevel@tonic-gate 		else
13500Sstevel@tonic-gate 			mdb_printf("%<u>%-16s %-?s%</u>\n", "NAME", "DN_HEAD");
13510Sstevel@tonic-gate 	}
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 	if ((flags & DCMD_LOOP) && (dn.dn_name == NULL))
13540Sstevel@tonic-gate 		return (DCMD_OK); /* Skip empty slots if we're printing table */
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	if (mdb_readstr(name, sizeof (name), (uintptr_t)dn.dn_name) == -1)
13570Sstevel@tonic-gate 		(void) mdb_snprintf(name, sizeof (name), "0x%p", dn.dn_name);
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 	if (opt_v) {
13600Sstevel@tonic-gate 		ddi_prop_list_t prop_list;
13610Sstevel@tonic-gate 		mdb_printf("%<b>%-16s%</b>\n", name);
13620Sstevel@tonic-gate 		mdb_inc_indent(2);
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 		mdb_printf("          flags %b\n", dn.dn_flags, dn_flag_bits);
13650Sstevel@tonic-gate 		mdb_printf("             pl %p\n", (void *)dn.dn_pl);
13660Sstevel@tonic-gate 		mdb_printf("           head %p\n", dn.dn_head);
13670Sstevel@tonic-gate 		mdb_printf("       instance %d\n", dn.dn_instance);
13680Sstevel@tonic-gate 		mdb_printf("         inlist %p\n", dn.dn_inlist);
13690Sstevel@tonic-gate 		mdb_printf("global_prop_ptr %p\n", dn.dn_global_prop_ptr);
13700Sstevel@tonic-gate 		if (mdb_vread(&prop_list, sizeof (ddi_prop_list_t),
13710Sstevel@tonic-gate 		    (uintptr_t)dn.dn_global_prop_ptr) != -1) {
13720Sstevel@tonic-gate 			devinfo_print_props(NULL, prop_list.prop_list);
13730Sstevel@tonic-gate 		}
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 		mdb_dec_indent(2);
13760Sstevel@tonic-gate 	} else
13770Sstevel@tonic-gate 		mdb_printf("%-16s %-?p\n", name, dn.dn_head);
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate 	return (DCMD_OK);
13800Sstevel@tonic-gate }
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate /*ARGSUSED*/
13830Sstevel@tonic-gate int
13840Sstevel@tonic-gate name2major(uintptr_t vaddr, uint_t flags, int argc, const mdb_arg_t *argv)
13850Sstevel@tonic-gate {
13860Sstevel@tonic-gate 	major_t major;
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 	if (flags & DCMD_ADDRSPEC)
13890Sstevel@tonic-gate 		return (DCMD_USAGE);
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	if (argc != 1 || argv->a_type != MDB_TYPE_STRING)
13920Sstevel@tonic-gate 		return (DCMD_USAGE);
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 	if (mdb_name_to_major(argv->a_un.a_str, &major) != 0) {
13950Sstevel@tonic-gate 		mdb_warn("failed to convert name to major number\n");
13960Sstevel@tonic-gate 		return (DCMD_ERR);
13970Sstevel@tonic-gate 	}
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 	mdb_printf("0x%x\n", major);
14000Sstevel@tonic-gate 	return (DCMD_OK);
14010Sstevel@tonic-gate }
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate /*
14040Sstevel@tonic-gate  * Get a numerical argument of a dcmd from addr if an address is specified
14050Sstevel@tonic-gate  * or from argv if no address is specified. Return the argument in ret.
14060Sstevel@tonic-gate  */
14070Sstevel@tonic-gate static int
14080Sstevel@tonic-gate getarg(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv,
14090Sstevel@tonic-gate     uintptr_t *ret)
14100Sstevel@tonic-gate {
14110Sstevel@tonic-gate 	if (argc == 0 && (flags & DCMD_ADDRSPEC)) {
14120Sstevel@tonic-gate 		*ret = addr;
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 	} else if (argc == 1 && !(flags & DCMD_ADDRSPEC)) {
14150Sstevel@tonic-gate 		*ret = (argv[0].a_type == MDB_TYPE_IMMEDIATE) ?
14160Sstevel@tonic-gate 		    (uintptr_t)argv[0].a_un.a_val :
14170Sstevel@tonic-gate 		    (uintptr_t)mdb_strtoull(argv->a_un.a_str);
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	} else {
14200Sstevel@tonic-gate 		return (-1);
14210Sstevel@tonic-gate 	}
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	return (0);
14240Sstevel@tonic-gate }
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate /*ARGSUSED*/
14270Sstevel@tonic-gate int
14280Sstevel@tonic-gate major2name(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
14290Sstevel@tonic-gate {
14300Sstevel@tonic-gate 	uintptr_t major;
14310Sstevel@tonic-gate 	const char *name;
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate 	if (getarg(addr, flags, argc, argv, &major) < 0)
14340Sstevel@tonic-gate 		return (DCMD_USAGE);
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate 	if ((name = mdb_major_to_name((major_t)major)) == NULL) {
14370Sstevel@tonic-gate 		mdb_warn("failed to convert major number to name\n");
14380Sstevel@tonic-gate 		return (DCMD_ERR);
14390Sstevel@tonic-gate 	}
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate 	mdb_printf("%s\n", name);
14420Sstevel@tonic-gate 	return (DCMD_OK);
14430Sstevel@tonic-gate }
14440Sstevel@tonic-gate 
14450Sstevel@tonic-gate /*ARGSUSED*/
14460Sstevel@tonic-gate int
14470Sstevel@tonic-gate dev2major(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
14480Sstevel@tonic-gate {
14490Sstevel@tonic-gate 	uintptr_t dev;
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	if (getarg(addr, flags, argc, argv, &dev) < 0)
14520Sstevel@tonic-gate 		return (DCMD_USAGE);
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 	if (flags & DCMD_PIPE_OUT)
14550Sstevel@tonic-gate 		mdb_printf("%x\n", getmajor(dev));
14560Sstevel@tonic-gate 	else
14570Sstevel@tonic-gate 		mdb_printf("0x%x (0t%d)\n", getmajor(dev), getmajor(dev));
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 	return (DCMD_OK);
14600Sstevel@tonic-gate }
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate /*ARGSUSED*/
14630Sstevel@tonic-gate int
14640Sstevel@tonic-gate dev2minor(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
14650Sstevel@tonic-gate {
14660Sstevel@tonic-gate 	uintptr_t dev;
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 	if (getarg(addr, flags, argc, argv, &dev) < 0)
14690Sstevel@tonic-gate 		return (DCMD_USAGE);
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 	if (flags & DCMD_PIPE_OUT)
14720Sstevel@tonic-gate 		mdb_printf("%x\n", getminor(dev));
14730Sstevel@tonic-gate 	else
14740Sstevel@tonic-gate 		mdb_printf("0x%x (0t%d)\n", getminor(dev), getminor(dev));
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 	return (DCMD_OK);
14770Sstevel@tonic-gate }
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate /*ARGSUSED*/
14800Sstevel@tonic-gate int
14810Sstevel@tonic-gate devt(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
14820Sstevel@tonic-gate {
14830Sstevel@tonic-gate 	uintptr_t dev;
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 	if (getarg(addr, flags, argc, argv, &dev) < 0)
14860Sstevel@tonic-gate 		return (DCMD_USAGE);
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags)) {
14890Sstevel@tonic-gate 		mdb_printf("%<u>%10s%</u>  %<u>%10s%</u>\n", "MAJOR",
14900Sstevel@tonic-gate 		    "MINOR");
14910Sstevel@tonic-gate 	}
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 	mdb_printf("%10d  %10d\n", getmajor(dev), getminor(dev));
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 	return (DCMD_OK);
14960Sstevel@tonic-gate }
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate /*ARGSUSED*/
14990Sstevel@tonic-gate int
15000Sstevel@tonic-gate softstate(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
15010Sstevel@tonic-gate {
15020Sstevel@tonic-gate 	uintptr_t statep;
15030Sstevel@tonic-gate 	int instance;
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 	if (argc != 1) {
15070Sstevel@tonic-gate 		return (DCMD_USAGE);
15080Sstevel@tonic-gate 	}
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 	if (argv[0].a_type == MDB_TYPE_IMMEDIATE)
15110Sstevel@tonic-gate 		instance = argv[0].a_un.a_val;
15120Sstevel@tonic-gate 	else
15130Sstevel@tonic-gate 		instance = mdb_strtoull(argv->a_un.a_str);
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 	if (mdb_get_soft_state_byaddr(addr, instance, &statep, NULL, 0) == -1) {
15160Sstevel@tonic-gate 		if (errno == ENOENT) {
15170Sstevel@tonic-gate 			mdb_warn("instance %d unused\n", instance);
15180Sstevel@tonic-gate 		} else {
15190Sstevel@tonic-gate 			mdb_warn("couldn't determine softstate for "
15200Sstevel@tonic-gate 			    "instance %d", instance);
15210Sstevel@tonic-gate 		}
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 		return (DCMD_ERR);
15240Sstevel@tonic-gate 	}
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate 	mdb_printf("%p\n", statep);
15270Sstevel@tonic-gate 	return (DCMD_OK);
15280Sstevel@tonic-gate }
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate /*
15310Sstevel@tonic-gate  * Walker for all possible pointers to a driver state struct in an
15320Sstevel@tonic-gate  * i_ddi_soft_state instance chain.  Returns all non-NULL pointers.
15330Sstevel@tonic-gate  */
15340Sstevel@tonic-gate typedef struct soft_state_walk {
15350Sstevel@tonic-gate 	struct i_ddi_soft_state	ssw_ss;	/* Local copy of i_ddi_soft_state */
15360Sstevel@tonic-gate 	void		**ssw_pointers;	/* to driver state structs */
15370Sstevel@tonic-gate 	uint_t		ssw_index;	/* array entry we're using */
15380Sstevel@tonic-gate } soft_state_walk_t;
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate int
15410Sstevel@tonic-gate soft_state_walk_init(mdb_walk_state_t *wsp)
15420Sstevel@tonic-gate {
15430Sstevel@tonic-gate 	soft_state_walk_t *sst;
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 	if (wsp->walk_addr == NULL)
15470Sstevel@tonic-gate 		return (WALK_DONE);
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	sst = mdb_zalloc(sizeof (soft_state_walk_t), UM_SLEEP|UM_GC);
15500Sstevel@tonic-gate 	wsp->walk_data = sst;
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	if (mdb_vread(&(sst->ssw_ss), sizeof (sst->ssw_ss), wsp->walk_addr) !=
15540Sstevel@tonic-gate 	    sizeof (sst->ssw_ss)) {
15550Sstevel@tonic-gate 		mdb_warn("failed to read i_ddi_soft_state at %p",
15560Sstevel@tonic-gate 		    wsp->walk_addr);
15570Sstevel@tonic-gate 		return (WALK_ERR);
15580Sstevel@tonic-gate 	}
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 	/* Read array of pointers to state structs into local storage. */
15620Sstevel@tonic-gate 	sst->ssw_pointers = mdb_alloc((sst->ssw_ss.n_items * sizeof (void *)),
15630Sstevel@tonic-gate 	    UM_SLEEP|UM_GC);
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 	if (mdb_vread(sst->ssw_pointers, (sst->ssw_ss.n_items *
15660Sstevel@tonic-gate 	    sizeof (void *)), (uintptr_t)sst->ssw_ss.array) !=
15670Sstevel@tonic-gate 	    (sst->ssw_ss.n_items * sizeof (void *))) {
15680Sstevel@tonic-gate 		mdb_warn("failed to read i_ddi_soft_state at %p",
15690Sstevel@tonic-gate 		    wsp->walk_addr);
15700Sstevel@tonic-gate 		return (WALK_ERR);
15710Sstevel@tonic-gate 	}
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 	sst->ssw_index = 0;
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 	return (WALK_NEXT);
15760Sstevel@tonic-gate }
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate int
15790Sstevel@tonic-gate soft_state_walk_step(mdb_walk_state_t *wsp)
15800Sstevel@tonic-gate {
15810Sstevel@tonic-gate 	soft_state_walk_t *sst = (soft_state_walk_t *)wsp->walk_data;
15820Sstevel@tonic-gate 	int status = WALK_NEXT;
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 
15850Sstevel@tonic-gate 	/*
15860Sstevel@tonic-gate 	 * If the entry indexed has a valid pointer to a soft state struct,
15870Sstevel@tonic-gate 	 * invoke caller's callback func.
15880Sstevel@tonic-gate 	 */
15890Sstevel@tonic-gate 	if (sst->ssw_pointers[sst->ssw_index] != NULL) {
15900Sstevel@tonic-gate 		status = wsp->walk_callback(
15910Sstevel@tonic-gate 		    (uintptr_t)(sst->ssw_pointers[sst->ssw_index]), NULL,
15920Sstevel@tonic-gate 		    wsp->walk_cbdata);
15930Sstevel@tonic-gate 	}
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 	sst->ssw_index += 1;
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	if (sst->ssw_index == sst->ssw_ss.n_items)
15980Sstevel@tonic-gate 		return (WALK_DONE);
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 	return (status);
16010Sstevel@tonic-gate }
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate int
16040Sstevel@tonic-gate soft_state_all_walk_step(mdb_walk_state_t *wsp)
16050Sstevel@tonic-gate {
16060Sstevel@tonic-gate 	soft_state_walk_t *sst = (soft_state_walk_t *)wsp->walk_data;
16070Sstevel@tonic-gate 	int status = WALK_NEXT;
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate 	status = wsp->walk_callback(
16110Sstevel@tonic-gate 	    (uintptr_t)(sst->ssw_pointers[sst->ssw_index]), NULL,
16120Sstevel@tonic-gate 	    wsp->walk_cbdata);
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	sst->ssw_index += 1;
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate 	if (sst->ssw_index == sst->ssw_ss.n_items)
16170Sstevel@tonic-gate 		return (WALK_DONE);
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	return (status);
16200Sstevel@tonic-gate }
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate /*ARGSUSED*/
16230Sstevel@tonic-gate int
16240Sstevel@tonic-gate devbindings(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
16250Sstevel@tonic-gate {
16260Sstevel@tonic-gate 	const mdb_arg_t *arg;
16270Sstevel@tonic-gate 	struct devnames dn;
16280Sstevel@tonic-gate 	uintptr_t dn_addr;
16290Sstevel@tonic-gate 	major_t major;
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC) && argc < 1)
16320Sstevel@tonic-gate 		return (DCMD_USAGE);
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 	if (flags & DCMD_ADDRSPEC) {
16350Sstevel@tonic-gate 		/*
16360Sstevel@tonic-gate 		 * If there's an address, then it's a major number
16370Sstevel@tonic-gate 		 */
16380Sstevel@tonic-gate 		major = addr;
16390Sstevel@tonic-gate 	} else {
16400Sstevel@tonic-gate 		/*
16410Sstevel@tonic-gate 		 * We interpret the last argument. Any other arguments are
16420Sstevel@tonic-gate 		 * forwarded to "devinfo"
16430Sstevel@tonic-gate 		 */
16440Sstevel@tonic-gate 		arg = &argv[argc - 1];
16450Sstevel@tonic-gate 		argc--;
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 		if (arg->a_type == MDB_TYPE_IMMEDIATE) {
16480Sstevel@tonic-gate 			major = (uintptr_t)arg->a_un.a_val;
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate 		} else if (arg->a_un.a_str[0] == '-') {
16510Sstevel@tonic-gate 			/* the argument shouldn't be an option */
16520Sstevel@tonic-gate 			return (DCMD_USAGE);
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate 		} else if (isdigit(arg->a_un.a_str[0])) {
16550Sstevel@tonic-gate 			major = (uintptr_t)mdb_strtoull(arg->a_un.a_str);
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 		} else {
16580Sstevel@tonic-gate 			if (mdb_name_to_major(arg->a_un.a_str, &major) != 0) {
16590Sstevel@tonic-gate 				mdb_warn("failed to get major number for %s\n",
16600Sstevel@tonic-gate 				    arg->a_un.a_str);
16610Sstevel@tonic-gate 				return (DCMD_ERR);
16620Sstevel@tonic-gate 			}
16630Sstevel@tonic-gate 		}
16640Sstevel@tonic-gate 	}
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 	if (major_to_addr(major, &dn_addr) != 0)
16670Sstevel@tonic-gate 		return (DCMD_ERR);
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate 	if (mdb_vread(&dn, sizeof (struct devnames), dn_addr) == -1) {
16700Sstevel@tonic-gate 		mdb_warn("couldn't read devnames array at %p", dn_addr);
16710Sstevel@tonic-gate 		return (DCMD_ERR);
16720Sstevel@tonic-gate 	}
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 	if (mdb_pwalk_dcmd("devi_next", "devinfo", argc, argv,
16750Sstevel@tonic-gate 	    (uintptr_t)dn.dn_head) != 0) {
16760Sstevel@tonic-gate 		mdb_warn("couldn't walk the devinfo chain at %p", dn.dn_head);
16770Sstevel@tonic-gate 		return (DCMD_ERR);
16780Sstevel@tonic-gate 	}
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 	return (DCMD_OK);
16810Sstevel@tonic-gate }
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate /*
16840Sstevel@tonic-gate  * walk binding hashtable (as of of driver names (e.g., mb_hashtab))
16850Sstevel@tonic-gate  */
16860Sstevel@tonic-gate int
16870Sstevel@tonic-gate binding_hash_walk_init(mdb_walk_state_t *wsp)
16880Sstevel@tonic-gate {
16890Sstevel@tonic-gate 	if (wsp->walk_addr == NULL)
16900Sstevel@tonic-gate 		return (WALK_ERR);
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate 	wsp->walk_data = mdb_alloc(sizeof (void *) * MOD_BIND_HASHSIZE,
16930Sstevel@tonic-gate 	    UM_SLEEP|UM_GC);
16940Sstevel@tonic-gate 	if (mdb_vread(wsp->walk_data, sizeof (void *) * MOD_BIND_HASHSIZE,
16950Sstevel@tonic-gate 	    wsp->walk_addr) == -1) {
16960Sstevel@tonic-gate 		mdb_warn("failed to read mb_hashtab");
16970Sstevel@tonic-gate 		return (WALK_ERR);
16980Sstevel@tonic-gate 	}
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	wsp->walk_arg = 0;	/* index into mb_hashtab array to start */
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 	return (WALK_NEXT);
17030Sstevel@tonic-gate }
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate int
17060Sstevel@tonic-gate binding_hash_walk_step(mdb_walk_state_t *wsp)
17070Sstevel@tonic-gate {
17080Sstevel@tonic-gate 	int		status;
17090Sstevel@tonic-gate 	uintptr_t	bind_p;
17100Sstevel@tonic-gate 	struct bind	bind;
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate 	/*
17140Sstevel@tonic-gate 	 * Walk the singly-linked list of struct bind
17150Sstevel@tonic-gate 	 */
17160Sstevel@tonic-gate 	bind_p = ((uintptr_t *)wsp->walk_data)[(ulong_t)wsp->walk_arg];
17170Sstevel@tonic-gate 	while (bind_p != NULL) {
17180Sstevel@tonic-gate 
17190Sstevel@tonic-gate 		if (mdb_vread(&bind, sizeof (bind), bind_p) == -1) {
17200Sstevel@tonic-gate 			mdb_warn("failed to read bind struct at %p",
17210Sstevel@tonic-gate 			    wsp->walk_addr);
17220Sstevel@tonic-gate 			return (WALK_ERR);
17230Sstevel@tonic-gate 		}
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 		if ((status = wsp->walk_callback(bind_p, &bind,
17260Sstevel@tonic-gate 		    wsp->walk_cbdata)) != WALK_NEXT) {
17270Sstevel@tonic-gate 			return (status);
17280Sstevel@tonic-gate 		}
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate 		bind_p = (uintptr_t)bind.b_next;
17310Sstevel@tonic-gate 	}
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 	wsp->walk_arg = (void *)((char *)wsp->walk_arg + 1);
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 	if (wsp->walk_arg == (void *)(MOD_BIND_HASHSIZE - 1))
17360Sstevel@tonic-gate 		return (WALK_DONE);
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate 	return (WALK_NEXT);
17390Sstevel@tonic-gate }
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate /*ARGSUSED*/
17420Sstevel@tonic-gate int
17430Sstevel@tonic-gate binding_hash_entry(uintptr_t addr, uint_t flags, int argc,
17440Sstevel@tonic-gate     const mdb_arg_t *argv)
17450Sstevel@tonic-gate {
17460Sstevel@tonic-gate 	struct bind 	bind;
17470Sstevel@tonic-gate 	/* Arbitrary lengths based on output format below */
17484145Scth 	char name[MAXPATHLEN] = "???";
17494145Scth 	char bind_name[MAXPATHLEN] = "<null>";
17500Sstevel@tonic-gate 
17510Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) == NULL)
17520Sstevel@tonic-gate 		return (DCMD_USAGE);
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 	/* Allow null addresses to be passed (as from a walker) */
17550Sstevel@tonic-gate 	if (addr == NULL)
17560Sstevel@tonic-gate 		return (DCMD_OK);
17570Sstevel@tonic-gate 
17580Sstevel@tonic-gate 	if (mdb_vread(&bind, sizeof (bind), addr) == -1) {
17590Sstevel@tonic-gate 		mdb_warn("failed to read struct bind at %p", addr);
17600Sstevel@tonic-gate 		return (DCMD_ERR);
17610Sstevel@tonic-gate 	}
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags)) {
17644145Scth 		mdb_printf("%<u>%?s% %-5s %s%</u>\n",
17654145Scth 		    "NEXT", "MAJOR", "NAME(S)");
17660Sstevel@tonic-gate 	}
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate 	if (mdb_readstr(name, sizeof (name), (uintptr_t)bind.b_name) == -1)
17690Sstevel@tonic-gate 		mdb_warn("failed to read 'name'");
17700Sstevel@tonic-gate 
17714145Scth 	/* There may be bind_name, so this may fail */
17724145Scth 	if (mdb_readstr(bind_name, sizeof (bind_name),
17734145Scth 	    (uintptr_t)bind.b_bind_name) == -1) {
17744145Scth 		mdb_printf("%?p %5d %s\n",
17754145Scth 		    bind.b_next, bind.b_num, name);
17764145Scth 	} else {
17774145Scth 		mdb_printf("%?p %5d %s %s\n",
17784145Scth 		    bind.b_next, bind.b_num, name, bind_name);
17794145Scth 	}
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate 	return (DCMD_OK);
17820Sstevel@tonic-gate }
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate typedef struct devinfo_audit_log_walk_data {
17850Sstevel@tonic-gate 	devinfo_audit_t dil_buf;	/* buffer of last entry */
17860Sstevel@tonic-gate 	uintptr_t dil_base;		/* starting address of log buffer */
17870Sstevel@tonic-gate 	int dil_max;			/* maximum index */
17880Sstevel@tonic-gate 	int dil_start;			/* starting index */
17890Sstevel@tonic-gate 	int dil_index;			/* current walking index */
17900Sstevel@tonic-gate } devinfo_audit_log_walk_data_t;
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate int
17930Sstevel@tonic-gate devinfo_audit_log_walk_init(mdb_walk_state_t *wsp)
17940Sstevel@tonic-gate {
17950Sstevel@tonic-gate 	devinfo_log_header_t header;
17960Sstevel@tonic-gate 	devinfo_audit_log_walk_data_t *dil;
17970Sstevel@tonic-gate 	uintptr_t devinfo_audit_log;
17980Sstevel@tonic-gate 
17990Sstevel@tonic-gate 	/* read in devinfo_log_header structure */
18000Sstevel@tonic-gate 	if (mdb_readvar(&devinfo_audit_log, "devinfo_audit_log") == -1) {
18010Sstevel@tonic-gate 		mdb_warn("failed to read 'devinfo_audit_log'");
18020Sstevel@tonic-gate 		return (WALK_ERR);
18030Sstevel@tonic-gate 	}
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate 	if (mdb_vread(&header, sizeof (devinfo_log_header_t),
18060Sstevel@tonic-gate 	    devinfo_audit_log) == -1) {
18070Sstevel@tonic-gate 		mdb_warn("couldn't read devinfo_log_header at %p",
18080Sstevel@tonic-gate 		    devinfo_audit_log);
18090Sstevel@tonic-gate 		return (WALK_ERR);
18100Sstevel@tonic-gate 	}
18110Sstevel@tonic-gate 
18120Sstevel@tonic-gate 	dil = mdb_zalloc(sizeof (devinfo_audit_log_walk_data_t), UM_SLEEP);
18130Sstevel@tonic-gate 	wsp->walk_data = dil;
18140Sstevel@tonic-gate 
18150Sstevel@tonic-gate 	dil->dil_start = dil->dil_index = header.dh_curr;
18160Sstevel@tonic-gate 	dil->dil_max = header.dh_max;
18170Sstevel@tonic-gate 	if (dil->dil_start < 0)		/* no log entries */
18180Sstevel@tonic-gate 		return (WALK_DONE);
18190Sstevel@tonic-gate 
18200Sstevel@tonic-gate 	dil->dil_base = devinfo_audit_log +
18210Sstevel@tonic-gate 	    offsetof(devinfo_log_header_t, dh_entry);
18220Sstevel@tonic-gate 	wsp->walk_addr = dil->dil_base +
18230Sstevel@tonic-gate 	    dil->dil_index * sizeof (devinfo_audit_t);
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 	return (WALK_NEXT);
18260Sstevel@tonic-gate }
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate int
18290Sstevel@tonic-gate devinfo_audit_log_walk_step(mdb_walk_state_t *wsp)
18300Sstevel@tonic-gate {
18310Sstevel@tonic-gate 	uintptr_t addr = wsp->walk_addr;
18320Sstevel@tonic-gate 	devinfo_audit_log_walk_data_t *dil = wsp->walk_data;
18330Sstevel@tonic-gate 	devinfo_audit_t *da = &dil->dil_buf;
18340Sstevel@tonic-gate 	int status = WALK_NEXT;
18350Sstevel@tonic-gate 
18360Sstevel@tonic-gate 	/* read in current entry and invoke callback */
18370Sstevel@tonic-gate 	if (addr == NULL)
18380Sstevel@tonic-gate 		return (WALK_DONE);
18390Sstevel@tonic-gate 
18400Sstevel@tonic-gate 	if (mdb_vread(&dil->dil_buf, sizeof (devinfo_audit_t), addr) == -1) {
18410Sstevel@tonic-gate 		mdb_warn("failed to read devinfo_audit at %p", addr);
18420Sstevel@tonic-gate 		status = WALK_DONE;
18430Sstevel@tonic-gate 	}
18440Sstevel@tonic-gate 	status = wsp->walk_callback(wsp->walk_addr, da, wsp->walk_cbdata);
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 	/* step to the previous log entry in time */
18470Sstevel@tonic-gate 	if (--dil->dil_index < 0)
18480Sstevel@tonic-gate 		dil->dil_index += dil->dil_max;
18490Sstevel@tonic-gate 	if (dil->dil_index == dil->dil_start) {
18500Sstevel@tonic-gate 		wsp->walk_addr = NULL;
18510Sstevel@tonic-gate 		return (WALK_DONE);
18520Sstevel@tonic-gate 	}
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate 	wsp->walk_addr = dil->dil_base +
18550Sstevel@tonic-gate 	    dil->dil_index * sizeof (devinfo_audit_t);
18560Sstevel@tonic-gate 	return (status);
18570Sstevel@tonic-gate }
18580Sstevel@tonic-gate 
18590Sstevel@tonic-gate void
18600Sstevel@tonic-gate devinfo_audit_log_walk_fini(mdb_walk_state_t *wsp)
18610Sstevel@tonic-gate {
18620Sstevel@tonic-gate 	mdb_free(wsp->walk_data, sizeof (devinfo_audit_log_walk_data_t));
18630Sstevel@tonic-gate }
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate /*
18660Sstevel@tonic-gate  * display devinfo_audit_t stack trace
18670Sstevel@tonic-gate  */
18680Sstevel@tonic-gate /*ARGSUSED*/
18690Sstevel@tonic-gate int
18700Sstevel@tonic-gate devinfo_audit(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
18710Sstevel@tonic-gate {
18720Sstevel@tonic-gate 	uint_t verbose = FALSE;
18730Sstevel@tonic-gate 	devinfo_audit_t da;
18740Sstevel@tonic-gate 	int i, depth;
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) == 0)
18770Sstevel@tonic-gate 		return (DCMD_USAGE);
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate 	if (mdb_getopts(argc, argv,
18800Sstevel@tonic-gate 	    'v', MDB_OPT_SETBITS, TRUE, &verbose, NULL) != argc)
18810Sstevel@tonic-gate 		return (DCMD_USAGE);
18820Sstevel@tonic-gate 
18830Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags)) {
18840Sstevel@tonic-gate 		mdb_printf(" %-?s %16s %-?s %-?s %5s\n",
18850Sstevel@tonic-gate 		    "AUDIT", "TIMESTAMP", "THREAD", "DEVINFO", "STATE");
18860Sstevel@tonic-gate 	}
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate 	if (mdb_vread(&da, sizeof (da), addr) == -1) {
18890Sstevel@tonic-gate 		mdb_warn("couldn't read devinfo_audit at %p", addr);
18900Sstevel@tonic-gate 		return (DCMD_ERR);
18910Sstevel@tonic-gate 	}
18920Sstevel@tonic-gate 
18930Sstevel@tonic-gate 	mdb_printf(" %0?p %16llx %0?p %0?p %s\n",
18940Sstevel@tonic-gate 	    addr, da.da_timestamp, da.da_thread, da.da_devinfo,
18950Sstevel@tonic-gate 	    di_state[MIN(da.da_node_state + 1, DI_STATE_MAX)]);
18960Sstevel@tonic-gate 
18970Sstevel@tonic-gate 	if (!verbose)
18980Sstevel@tonic-gate 		return (DCMD_OK);
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate 	mdb_inc_indent(4);
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate 	/*
19030Sstevel@tonic-gate 	 * Guard against bogus da_depth in case the devinfo_audit_t
19040Sstevel@tonic-gate 	 * is corrupt or the address does not really refer to a
19050Sstevel@tonic-gate 	 * devinfo_audit_t.
19060Sstevel@tonic-gate 	 */
19070Sstevel@tonic-gate 	depth = MIN(da.da_depth, DDI_STACK_DEPTH);
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 	for (i = 0; i < depth; i++)
19100Sstevel@tonic-gate 		mdb_printf("%a\n", da.da_stack[i]);
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 	mdb_printf("\n");
19130Sstevel@tonic-gate 	mdb_dec_indent(4);
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 	return (DCMD_OK);
19160Sstevel@tonic-gate }
19170Sstevel@tonic-gate 
19180Sstevel@tonic-gate int
19190Sstevel@tonic-gate devinfo_audit_log(uintptr_t addr, uint_t flags, int argc,
19200Sstevel@tonic-gate     const mdb_arg_t *argv)
19210Sstevel@tonic-gate {
19220Sstevel@tonic-gate 	if (flags & DCMD_ADDRSPEC)
19230Sstevel@tonic-gate 		return (devinfo_audit(addr, flags, argc, argv));
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate 	(void) mdb_walk_dcmd("devinfo_audit_log", "devinfo_audit", argc, argv);
19260Sstevel@tonic-gate 	return (DCMD_OK);
19270Sstevel@tonic-gate }
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate typedef struct devinfo_audit_node_walk_data {
19300Sstevel@tonic-gate 	devinfo_audit_t dih_buf;	/* buffer of last entry */
19310Sstevel@tonic-gate 	uintptr_t dih_dip;		/* address of dev_info */
19320Sstevel@tonic-gate 	int dih_on_devinfo;		/* devi_audit on dev_info struct */
19330Sstevel@tonic-gate } devinfo_audit_node_walk_data_t;
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate int
19360Sstevel@tonic-gate devinfo_audit_node_walk_init(mdb_walk_state_t *wsp)
19370Sstevel@tonic-gate {
19380Sstevel@tonic-gate 	devinfo_audit_node_walk_data_t *dih;
19390Sstevel@tonic-gate 	devinfo_audit_t *da;
19400Sstevel@tonic-gate 	struct dev_info devi;
19410Sstevel@tonic-gate 	uintptr_t addr = wsp->walk_addr;
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate 	/* read in devinfo structure */
19440Sstevel@tonic-gate 	if (mdb_vread(&devi, sizeof (struct dev_info), addr) == -1) {
19450Sstevel@tonic-gate 		mdb_warn("couldn't read dev_info at %p", addr);
19460Sstevel@tonic-gate 		return (WALK_ERR);
19470Sstevel@tonic-gate 	}
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate 	dih = mdb_zalloc(sizeof (devinfo_audit_node_walk_data_t), UM_SLEEP);
19500Sstevel@tonic-gate 	wsp->walk_data = dih;
19510Sstevel@tonic-gate 	da = &dih->dih_buf;
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate 	/* read in devi_audit structure */
19540Sstevel@tonic-gate 	if (mdb_vread(da, sizeof (devinfo_audit_t), (uintptr_t)devi.devi_audit)
19550Sstevel@tonic-gate 	    == -1) {
19560Sstevel@tonic-gate 		mdb_warn("couldn't read devi_audit at %p", devi.devi_audit);
19570Sstevel@tonic-gate 		return (WALK_ERR);
19580Sstevel@tonic-gate 	}
19590Sstevel@tonic-gate 	dih->dih_dip = addr;
19600Sstevel@tonic-gate 	dih->dih_on_devinfo = 1;
19610Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)devi.devi_audit;
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate 	return (WALK_NEXT);
19640Sstevel@tonic-gate }
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate int
19670Sstevel@tonic-gate devinfo_audit_node_walk_step(mdb_walk_state_t *wsp)
19680Sstevel@tonic-gate {
19690Sstevel@tonic-gate 	uintptr_t addr;
19700Sstevel@tonic-gate 	devinfo_audit_node_walk_data_t *dih = wsp->walk_data;
19710Sstevel@tonic-gate 	devinfo_audit_t *da = &dih->dih_buf;
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 	if (wsp->walk_addr == NULL)
19740Sstevel@tonic-gate 		return (WALK_DONE);
19750Sstevel@tonic-gate 	(void) wsp->walk_callback(wsp->walk_addr, NULL, wsp->walk_cbdata);
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate skip:
19780Sstevel@tonic-gate 	/* read in previous entry */
19790Sstevel@tonic-gate 	if ((addr = (uintptr_t)da->da_lastlog) == 0)
19800Sstevel@tonic-gate 		return (WALK_DONE);
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate 	if (mdb_vread(&dih->dih_buf, sizeof (devinfo_audit_t), addr) == -1) {
19830Sstevel@tonic-gate 		mdb_warn("failed to read devinfo_audit at %p", addr);
19840Sstevel@tonic-gate 		return (WALK_DONE);
19850Sstevel@tonic-gate 	}
19860Sstevel@tonic-gate 
19870Sstevel@tonic-gate 	/* check if last log was over-written */
19880Sstevel@tonic-gate 	if ((uintptr_t)da->da_devinfo != dih->dih_dip)
19890Sstevel@tonic-gate 		return (WALK_DONE);
19900Sstevel@tonic-gate 
19910Sstevel@tonic-gate 	/*
19920Sstevel@tonic-gate 	 * skip the first common log entry, which is a duplicate of
19930Sstevel@tonic-gate 	 * the devi_audit buffer on the dev_info structure
19940Sstevel@tonic-gate 	 */
19950Sstevel@tonic-gate 	if (dih->dih_on_devinfo) {
19960Sstevel@tonic-gate 		dih->dih_on_devinfo = 0;
19970Sstevel@tonic-gate 		goto skip;
19980Sstevel@tonic-gate 	}
19990Sstevel@tonic-gate 	wsp->walk_addr = addr;
20000Sstevel@tonic-gate 
20010Sstevel@tonic-gate 	return (WALK_NEXT);
20020Sstevel@tonic-gate }
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate void
20050Sstevel@tonic-gate devinfo_audit_node_walk_fini(mdb_walk_state_t *wsp)
20060Sstevel@tonic-gate {
20070Sstevel@tonic-gate 	mdb_free(wsp->walk_data, sizeof (devinfo_audit_node_walk_data_t));
20080Sstevel@tonic-gate }
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate int
20110Sstevel@tonic-gate devinfo_audit_node(uintptr_t addr, uint_t flags, int argc,
20120Sstevel@tonic-gate     const mdb_arg_t *argv)
20130Sstevel@tonic-gate {
20140Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
20150Sstevel@tonic-gate 		return (DCMD_USAGE);
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 	(void) mdb_pwalk_dcmd("devinfo_audit_node", "devinfo_audit",
20180Sstevel@tonic-gate 	    argc, argv, addr);
20190Sstevel@tonic-gate 	return (DCMD_OK);
20200Sstevel@tonic-gate }
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate /*
20230Sstevel@tonic-gate  * mdb support for per-devinfo fault management data
20240Sstevel@tonic-gate  */
20250Sstevel@tonic-gate /*ARGSUSED*/
20260Sstevel@tonic-gate int
20270Sstevel@tonic-gate devinfo_fm(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
20280Sstevel@tonic-gate {
20290Sstevel@tonic-gate 	struct dev_info devi;
20300Sstevel@tonic-gate 	struct i_ddi_fmhdl fhdl;
20310Sstevel@tonic-gate 
20320Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) == 0)
20330Sstevel@tonic-gate 		return (DCMD_USAGE);
20340Sstevel@tonic-gate 
20350Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags)) {
20360Sstevel@tonic-gate 		mdb_printf("%<u>%-11s IPL CAPS DROP FMCFULL FMCGREW ACCERR "
20370Sstevel@tonic-gate 		    "DMAERR %11s %11s%</u>\n", "ADDR", "DMACACHE", "ACCCACHE");
20380Sstevel@tonic-gate 	}
20390Sstevel@tonic-gate 
20400Sstevel@tonic-gate 	if (mdb_vread(&devi, sizeof (devi), addr) == -1) {
20410Sstevel@tonic-gate 		mdb_warn("failed to read devinfo struct at %p", addr);
20420Sstevel@tonic-gate 		return (DCMD_ERR);
20430Sstevel@tonic-gate 	}
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate 	if (mdb_vread(&fhdl, sizeof (fhdl), (uintptr_t)devi.devi_fmhdl) == -1) {
20460Sstevel@tonic-gate 		mdb_warn("failed to read devinfo fm struct at %p",
20470Sstevel@tonic-gate 		    (uintptr_t)devi.devi_fmhdl);
20480Sstevel@tonic-gate 		return (DCMD_ERR);
20490Sstevel@tonic-gate 	}
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate 	mdb_printf("%-11p %3u %c%c%c%c %4llu %7llu %7llu %6llu %6llu %11p "
20520Sstevel@tonic-gate 	    "%11p\n",
20530Sstevel@tonic-gate 	    (uintptr_t)devi.devi_fmhdl, fhdl.fh_ibc,
20540Sstevel@tonic-gate 	    (DDI_FM_EREPORT_CAP(fhdl.fh_cap) ? 'E' : '-'),
20550Sstevel@tonic-gate 	    (DDI_FM_ERRCB_CAP(fhdl.fh_cap) ? 'C' : '-'),
20560Sstevel@tonic-gate 	    (DDI_FM_ACC_ERR_CAP(fhdl.fh_cap) ? 'A' : '-'),
20570Sstevel@tonic-gate 	    (DDI_FM_DMA_ERR_CAP(fhdl.fh_cap) ? 'D' : '-'),
20580Sstevel@tonic-gate 	    fhdl.fh_kstat.fek_erpt_dropped.value.ui64,
20590Sstevel@tonic-gate 	    fhdl.fh_kstat.fek_fmc_full.value.ui64,
20600Sstevel@tonic-gate 	    fhdl.fh_kstat.fek_fmc_grew.value.ui64,
20610Sstevel@tonic-gate 	    fhdl.fh_kstat.fek_acc_err.value.ui64,
20620Sstevel@tonic-gate 	    fhdl.fh_kstat.fek_dma_err.value.ui64,
20630Sstevel@tonic-gate 	    fhdl.fh_dma_cache, fhdl.fh_acc_cache);
20640Sstevel@tonic-gate 
20650Sstevel@tonic-gate 
20660Sstevel@tonic-gate 	return (DCMD_OK);
20670Sstevel@tonic-gate }
20680Sstevel@tonic-gate 
20690Sstevel@tonic-gate /*ARGSUSED*/
20700Sstevel@tonic-gate int
20710Sstevel@tonic-gate devinfo_fmce(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
20720Sstevel@tonic-gate {
20730Sstevel@tonic-gate 	struct i_ddi_fmc_entry fce;
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) == 0)
20760Sstevel@tonic-gate 		return (DCMD_USAGE);
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags)) {
20790Sstevel@tonic-gate 		mdb_printf("%<u>%-11s    %11s    %11s%</u>\n", "ADDR",
20800Sstevel@tonic-gate 		    "RESOURCE", "BUS_SPECIFIC");
20810Sstevel@tonic-gate 	}
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate 	if (mdb_vread(&fce, sizeof (fce), addr) == -1) {
20840Sstevel@tonic-gate 		mdb_warn("failed to read fm cache struct at %p", addr);
20850Sstevel@tonic-gate 		return (DCMD_ERR);
20860Sstevel@tonic-gate 	}
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate 	mdb_printf("%-11p    %11p    %11p\n",
20890Sstevel@tonic-gate 	    (uintptr_t)addr, fce.fce_resource, fce.fce_bus_specific);
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate 
20920Sstevel@tonic-gate 	return (DCMD_OK);
20930Sstevel@tonic-gate }
20940Sstevel@tonic-gate 
20950Sstevel@tonic-gate int
20960Sstevel@tonic-gate devinfo_fmc_walk_init(mdb_walk_state_t *wsp)
20970Sstevel@tonic-gate {
20980Sstevel@tonic-gate 	struct i_ddi_fmc fec;
20990Sstevel@tonic-gate 	struct i_ddi_fmc_entry fe;
21000Sstevel@tonic-gate 
21010Sstevel@tonic-gate 	if (wsp->walk_addr == NULL)
21020Sstevel@tonic-gate 		return (WALK_ERR);
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate 	if (mdb_vread(&fec, sizeof (fec), wsp->walk_addr) == -1) {
21050Sstevel@tonic-gate 		mdb_warn("failed to read fm cache at %p", wsp->walk_addr);
21060Sstevel@tonic-gate 		return (WALK_ERR);
21070Sstevel@tonic-gate 	}
21080Sstevel@tonic-gate 
21090Sstevel@tonic-gate 	if (fec.fc_active == NULL)
21100Sstevel@tonic-gate 		return (WALK_DONE);
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate 	if (mdb_vread(&fe, sizeof (fe), (uintptr_t)fec.fc_active) == -1) {
21130Sstevel@tonic-gate 		mdb_warn("failed to read active fm cache list at %p",
21140Sstevel@tonic-gate 		    fec.fc_active);
21150Sstevel@tonic-gate 		return (WALK_ERR);
21160Sstevel@tonic-gate 	}
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate 	wsp->walk_data = fe.fce_next;
21190Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)fe.fce_next;
21200Sstevel@tonic-gate 	return (WALK_NEXT);
21210Sstevel@tonic-gate }
21220Sstevel@tonic-gate 
21230Sstevel@tonic-gate int
21240Sstevel@tonic-gate devinfo_fmc_walk_step(mdb_walk_state_t *wsp)
21250Sstevel@tonic-gate {
21260Sstevel@tonic-gate 	int status;
21270Sstevel@tonic-gate 	struct i_ddi_fmc_entry fe;
21280Sstevel@tonic-gate 
21290Sstevel@tonic-gate 	if (mdb_vread(&fe, sizeof (fe), wsp->walk_addr) == -1) {
21300Sstevel@tonic-gate 		mdb_warn("failed to read active fm cache entry at %p",
21310Sstevel@tonic-gate 		    wsp->walk_addr);
21320Sstevel@tonic-gate 		return (WALK_DONE);
21330Sstevel@tonic-gate 	}
21340Sstevel@tonic-gate 
21350Sstevel@tonic-gate 	status = wsp->walk_callback(wsp->walk_addr, &fe, wsp->walk_cbdata);
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate 	if (fe.fce_next == NULL)
21380Sstevel@tonic-gate 		return (WALK_DONE);
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)fe.fce_next;
21410Sstevel@tonic-gate 	return (status);
21420Sstevel@tonic-gate }
21430Sstevel@tonic-gate 
21440Sstevel@tonic-gate int
21450Sstevel@tonic-gate minornode_walk_init(mdb_walk_state_t *wsp)
21460Sstevel@tonic-gate {
21470Sstevel@tonic-gate 	struct dev_info di;
21480Sstevel@tonic-gate 	uintptr_t addr = wsp->walk_addr;
21490Sstevel@tonic-gate 
21500Sstevel@tonic-gate 	if (addr == NULL) {
21510Sstevel@tonic-gate 		mdb_warn("a dev_info struct address must be provided\n");
21520Sstevel@tonic-gate 		return (WALK_ERR);
21530Sstevel@tonic-gate 	}
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate 	if (mdb_vread(&di, sizeof (di), wsp->walk_addr) == -1) {
21560Sstevel@tonic-gate 		mdb_warn("failed to read dev_info struct at %p", addr);
21570Sstevel@tonic-gate 		return (WALK_ERR);
21580Sstevel@tonic-gate 	}
21590Sstevel@tonic-gate 
21600Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)di.devi_minor;
21610Sstevel@tonic-gate 	return (WALK_NEXT);
21620Sstevel@tonic-gate }
21630Sstevel@tonic-gate 
21640Sstevel@tonic-gate int
21650Sstevel@tonic-gate minornode_walk_step(mdb_walk_state_t *wsp)
21660Sstevel@tonic-gate {
21670Sstevel@tonic-gate 	struct ddi_minor_data md;
21680Sstevel@tonic-gate 	uintptr_t addr = wsp->walk_addr;
21690Sstevel@tonic-gate 
21700Sstevel@tonic-gate 	if (addr == NULL)
21710Sstevel@tonic-gate 		return (WALK_DONE);
21720Sstevel@tonic-gate 
21730Sstevel@tonic-gate 	if (mdb_vread(&md, sizeof (md), addr) == -1) {
21740Sstevel@tonic-gate 		mdb_warn("failed to read dev_info struct at %p", addr);
21750Sstevel@tonic-gate 		return (WALK_DONE);
21760Sstevel@tonic-gate 	}
21770Sstevel@tonic-gate 
21780Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)md.next;
21790Sstevel@tonic-gate 	return (wsp->walk_callback(addr, &md, wsp->walk_cbdata));
21800Sstevel@tonic-gate }
21810Sstevel@tonic-gate 
21820Sstevel@tonic-gate static const char *const md_type[] = {
21830Sstevel@tonic-gate 	"DDI_MINOR",
21840Sstevel@tonic-gate 	"DDI_ALIAS",
21850Sstevel@tonic-gate 	"DDI_DEFAULT",
21860Sstevel@tonic-gate 	"DDI_I_PATH",
21870Sstevel@tonic-gate 	"?"
21880Sstevel@tonic-gate };
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate #define	MD_TYPE_MAX	((sizeof (md_type) / sizeof (char *)) - 1)
21910Sstevel@tonic-gate 
21920Sstevel@tonic-gate /*ARGSUSED*/
21930Sstevel@tonic-gate static int
21940Sstevel@tonic-gate print_minornode(uintptr_t addr, const void *arg, void *data)
21950Sstevel@tonic-gate {
21960Sstevel@tonic-gate 	char name[128];
21970Sstevel@tonic-gate 	char nodetype[128];
21980Sstevel@tonic-gate 	char *spectype;
21990Sstevel@tonic-gate 	struct ddi_minor_data *mdp = (struct ddi_minor_data *)arg;
22000Sstevel@tonic-gate 
22010Sstevel@tonic-gate 	if (mdb_readstr(name, sizeof (name), (uintptr_t)mdp->ddm_name) == -1)
22020Sstevel@tonic-gate 		*name = '\0';
22030Sstevel@tonic-gate 
22040Sstevel@tonic-gate 	if (mdb_readstr(nodetype, sizeof (nodetype),
22050Sstevel@tonic-gate 	    (uintptr_t)mdp->ddm_node_type) == -1)
22060Sstevel@tonic-gate 		*nodetype = '\0';
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate 	switch (mdp->ddm_spec_type) {
22090Sstevel@tonic-gate 		case S_IFCHR:	spectype = "c";	break;
22100Sstevel@tonic-gate 		case S_IFBLK:	spectype = "b";	break;
22110Sstevel@tonic-gate 		default:	spectype = "?";	break;
22120Sstevel@tonic-gate 	}
22130Sstevel@tonic-gate 
22140Sstevel@tonic-gate 	mdb_printf("%?p %16lx %-4s %-11s %-10s %s\n",
22150Sstevel@tonic-gate 	    addr, mdp->ddm_dev, spectype, md_type[MIN(mdp->type, MD_TYPE_MAX)],
22160Sstevel@tonic-gate 	    name, nodetype);
22170Sstevel@tonic-gate 
22180Sstevel@tonic-gate 	return (WALK_NEXT);
22190Sstevel@tonic-gate }
22200Sstevel@tonic-gate 
22210Sstevel@tonic-gate /*ARGSUSED*/
22220Sstevel@tonic-gate int
22230Sstevel@tonic-gate minornodes(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
22240Sstevel@tonic-gate {
22250Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC) || argc != 0)
22260Sstevel@tonic-gate 		return (DCMD_USAGE);
22270Sstevel@tonic-gate 
22280Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags))
22290Sstevel@tonic-gate 		mdb_printf("%<u>%?s %16s %-4s %-11s %-10s %-16s%</u>\n",
22300Sstevel@tonic-gate 		    "ADDR", "DEV", "SPEC", "TYPE", "NAME", "NODETYPE");
22310Sstevel@tonic-gate 
22320Sstevel@tonic-gate 	if (mdb_pwalk("minornode", print_minornode, NULL, addr) == -1) {
22330Sstevel@tonic-gate 		mdb_warn("can't walk minornode");
22340Sstevel@tonic-gate 		return (DCMD_ERR);
22350Sstevel@tonic-gate 	}
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 	return (DCMD_OK);
22380Sstevel@tonic-gate }
2239