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