xref: /onnv-gate/usr/src/cmd/prtconf/pdevinfo.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * For machines that support the openprom, fetch and print the list
31*0Sstevel@tonic-gate  * of devices that the kernel has fetched from the prom or conjured up.
32*0Sstevel@tonic-gate  */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include <stdio.h>
35*0Sstevel@tonic-gate #include <stdarg.h>
36*0Sstevel@tonic-gate #include <stdlib.h>
37*0Sstevel@tonic-gate #include <fcntl.h>
38*0Sstevel@tonic-gate #include <ctype.h>
39*0Sstevel@tonic-gate #include <strings.h>
40*0Sstevel@tonic-gate #include <unistd.h>
41*0Sstevel@tonic-gate #include <stropts.h>
42*0Sstevel@tonic-gate #include <sys/types.h>
43*0Sstevel@tonic-gate #include <sys/mkdev.h>
44*0Sstevel@tonic-gate #include <sys/sunddi.h>
45*0Sstevel@tonic-gate #include <sys/openpromio.h>
46*0Sstevel@tonic-gate #include <sys/modctl.h>
47*0Sstevel@tonic-gate #include <sys/stat.h>
48*0Sstevel@tonic-gate #include <zone.h>
49*0Sstevel@tonic-gate #include <libnvpair.h>
50*0Sstevel@tonic-gate #include "prtconf.h"
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate typedef char *(*dump_propname_t)(void *);
54*0Sstevel@tonic-gate typedef int (*dump_proptype_t)(void *);
55*0Sstevel@tonic-gate typedef int (*dump_propints_t)(void *, int **);
56*0Sstevel@tonic-gate typedef int (*dump_propint64_t)(void *, int64_t **);
57*0Sstevel@tonic-gate typedef int (*dump_propstrings_t)(void *, char **);
58*0Sstevel@tonic-gate typedef int (*dump_propbytes_t)(void *, uchar_t **);
59*0Sstevel@tonic-gate typedef int (*dump_proprawdata_t)(void *, uchar_t **);
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate typedef struct dumpops_common {
62*0Sstevel@tonic-gate 	dump_propname_t doc_propname;
63*0Sstevel@tonic-gate 	dump_proptype_t doc_proptype;
64*0Sstevel@tonic-gate 	dump_propints_t doc_propints;
65*0Sstevel@tonic-gate 	dump_propint64_t doc_propint64;
66*0Sstevel@tonic-gate 	dump_propstrings_t doc_propstrings;
67*0Sstevel@tonic-gate 	dump_propbytes_t doc_propbytes;
68*0Sstevel@tonic-gate 	dump_proprawdata_t doc_proprawdata;
69*0Sstevel@tonic-gate } dumpops_common_t;
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate static const dumpops_common_t prop_dumpops = {
72*0Sstevel@tonic-gate 	(dump_propname_t)di_prop_name,
73*0Sstevel@tonic-gate 	(dump_proptype_t)di_prop_type,
74*0Sstevel@tonic-gate 	(dump_propints_t)di_prop_ints,
75*0Sstevel@tonic-gate 	(dump_propint64_t)di_prop_int64,
76*0Sstevel@tonic-gate 	(dump_propstrings_t)di_prop_strings,
77*0Sstevel@tonic-gate 	(dump_propbytes_t)di_prop_bytes,
78*0Sstevel@tonic-gate 	(dump_proprawdata_t)di_prop_rawdata
79*0Sstevel@tonic-gate }, pathprop_common_dumpops = {
80*0Sstevel@tonic-gate 	(dump_propname_t)di_path_prop_name,
81*0Sstevel@tonic-gate 	(dump_proptype_t)di_path_prop_type,
82*0Sstevel@tonic-gate 	(dump_propints_t)di_path_prop_ints,
83*0Sstevel@tonic-gate 	(dump_propint64_t)di_path_prop_int64s,
84*0Sstevel@tonic-gate 	(dump_propstrings_t)di_path_prop_strings,
85*0Sstevel@tonic-gate 	(dump_propbytes_t)di_path_prop_bytes,
86*0Sstevel@tonic-gate 	(dump_proprawdata_t)di_path_prop_bytes
87*0Sstevel@tonic-gate };
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate typedef void *(*dump_nextprop_t)(void *, void *);
90*0Sstevel@tonic-gate typedef dev_t (*dump_propdevt_t)(void *);
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate typedef struct dumpops {
93*0Sstevel@tonic-gate 	const dumpops_common_t *dop_common;
94*0Sstevel@tonic-gate 	dump_nextprop_t dop_nextprop;
95*0Sstevel@tonic-gate 	dump_propdevt_t dop_propdevt;
96*0Sstevel@tonic-gate } dumpops_t;
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate static const dumpops_t sysprop_dumpops = {
99*0Sstevel@tonic-gate 	&prop_dumpops,
100*0Sstevel@tonic-gate 	(dump_nextprop_t)di_prop_sys_next,
101*0Sstevel@tonic-gate 	NULL
102*0Sstevel@tonic-gate }, globprop_dumpops = {
103*0Sstevel@tonic-gate 	&prop_dumpops,
104*0Sstevel@tonic-gate 	(dump_nextprop_t)di_prop_global_next,
105*0Sstevel@tonic-gate 	NULL
106*0Sstevel@tonic-gate }, drvprop_dumpops = {
107*0Sstevel@tonic-gate 	&prop_dumpops,
108*0Sstevel@tonic-gate 	(dump_nextprop_t)di_prop_drv_next,
109*0Sstevel@tonic-gate 	(dump_propdevt_t)di_prop_devt
110*0Sstevel@tonic-gate }, hwprop_dumpops = {
111*0Sstevel@tonic-gate 	&prop_dumpops,
112*0Sstevel@tonic-gate 	(dump_nextprop_t)di_prop_hw_next,
113*0Sstevel@tonic-gate 	NULL
114*0Sstevel@tonic-gate }, pathprop_dumpops = {
115*0Sstevel@tonic-gate 	&pathprop_common_dumpops,
116*0Sstevel@tonic-gate 	(dump_nextprop_t)di_path_prop_next,
117*0Sstevel@tonic-gate 	NULL
118*0Sstevel@tonic-gate };
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate #define	PROPNAME(ops) (ops->dop_common->doc_propname)
121*0Sstevel@tonic-gate #define	PROPTYPE(ops) (ops->dop_common->doc_proptype)
122*0Sstevel@tonic-gate #define	PROPINTS(ops) (ops->dop_common->doc_propints)
123*0Sstevel@tonic-gate #define	PROPINT64(ops) (ops->dop_common->doc_propint64)
124*0Sstevel@tonic-gate #define	PROPSTRINGS(ops) (ops->dop_common->doc_propstrings)
125*0Sstevel@tonic-gate #define	PROPBYTES(ops) (ops->dop_common->doc_propbytes)
126*0Sstevel@tonic-gate #define	PROPRAWDATA(ops) (ops->dop_common->doc_proprawdata)
127*0Sstevel@tonic-gate #define	NEXTPROP(ops) (ops->dop_nextprop)
128*0Sstevel@tonic-gate #define	PROPDEVT(ops) (ops->dop_propdevt)
129*0Sstevel@tonic-gate #define	NUM_ELEMENTS(A) (sizeof (A) / sizeof (A[0]))
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate static int prop_type_guess(const dumpops_t *, void *, void **, int *);
132*0Sstevel@tonic-gate static void dump_prop_list_common(const dumpops_t *, int, void *);
133*0Sstevel@tonic-gate static void walk_driver(di_node_t, di_devlink_handle_t);
134*0Sstevel@tonic-gate static int dump_devs(di_node_t, void *);
135*0Sstevel@tonic-gate static int dump_prop_list(const dumpops_t *, const char *, int, di_node_t);
136*0Sstevel@tonic-gate static int _error(const char *, ...);
137*0Sstevel@tonic-gate static int is_openprom();
138*0Sstevel@tonic-gate static void walk(uchar_t *, uint_t, int);
139*0Sstevel@tonic-gate static void dump_node(nvlist_t *, int);
140*0Sstevel@tonic-gate static void dump_prodinfo(di_prom_handle_t, di_node_t, const char **,
141*0Sstevel@tonic-gate 				char *, int);
142*0Sstevel@tonic-gate static di_node_t find_node_by_name(di_prom_handle_t, di_node_t, char *);
143*0Sstevel@tonic-gate static int get_propval_by_name(di_prom_handle_t, di_node_t,
144*0Sstevel@tonic-gate 				const char *, uchar_t **);
145*0Sstevel@tonic-gate static void dump_pathing_data(int, di_node_t);
146*0Sstevel@tonic-gate static void dump_minor_data(int, di_node_t, di_devlink_handle_t);
147*0Sstevel@tonic-gate static void dump_link_data(int, di_node_t, di_devlink_handle_t);
148*0Sstevel@tonic-gate static int print_composite_string(const char *, char *, int);
149*0Sstevel@tonic-gate static void print_one(nvpair_t *, int);
150*0Sstevel@tonic-gate static int unprintable(char *, int);
151*0Sstevel@tonic-gate static int promopen(int);
152*0Sstevel@tonic-gate static void promclose();
153*0Sstevel@tonic-gate static di_node_t find_target_node(di_node_t);
154*0Sstevel@tonic-gate static void node_display_set(di_node_t);
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate void
157*0Sstevel@tonic-gate prtconf_devinfo(void)
158*0Sstevel@tonic-gate {
159*0Sstevel@tonic-gate 	struct di_priv_data	fetch;
160*0Sstevel@tonic-gate 	di_devlink_handle_t	devlink_hdl = NULL;
161*0Sstevel@tonic-gate 	di_node_t		root_node;
162*0Sstevel@tonic-gate 	uint_t			flag;
163*0Sstevel@tonic-gate 	char			*rootpath;
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	dprintf("verbosemode %s\n", opts.o_verbose ? "on" : "off");
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	/* determine what info we need to get from kernel */
168*0Sstevel@tonic-gate 	flag = DINFOSUBTREE;
169*0Sstevel@tonic-gate 	rootpath = "/";
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	if (opts.o_target) {
172*0Sstevel@tonic-gate 		flag |= (DINFOMINOR | DINFOPATH);
173*0Sstevel@tonic-gate 	}
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	if (opts.o_forcecache) {
176*0Sstevel@tonic-gate 		if (opts.o_verbose || dbg.d_forceload) {
177*0Sstevel@tonic-gate 			exit(_error(NULL, "option combination not supported"));
178*0Sstevel@tonic-gate 		}
179*0Sstevel@tonic-gate 		if (strcmp(rootpath, "/") != 0) {
180*0Sstevel@tonic-gate 			exit(_error(NULL, "invalid root path for option"));
181*0Sstevel@tonic-gate 		}
182*0Sstevel@tonic-gate 		flag = DINFOCACHE;
183*0Sstevel@tonic-gate 	}
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	if (dbg.d_forceload) {
186*0Sstevel@tonic-gate 		flag |= DINFOFORCE;
187*0Sstevel@tonic-gate 	}
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	if (opts.o_verbose) {
190*0Sstevel@tonic-gate 		flag |= (DINFOPROP | DINFOMINOR | DINFOPRIVDATA | DINFOPATH | \
191*0Sstevel@tonic-gate 		    DINFOLYR);
192*0Sstevel@tonic-gate 		init_priv_data(&fetch);
193*0Sstevel@tonic-gate 		root_node = di_init_impl(rootpath, flag, &fetch);
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 		/* get devlink (aka aliases) data */
196*0Sstevel@tonic-gate 		if ((devlink_hdl = di_devlink_init(NULL, 0)) == NULL)
197*0Sstevel@tonic-gate 			exit(_error("di_devlink_init() failed."));
198*0Sstevel@tonic-gate 	} else
199*0Sstevel@tonic-gate 		root_node = di_init(rootpath, flag);
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	if (root_node == DI_NODE_NIL) {
202*0Sstevel@tonic-gate 		(void) _error(NULL, "devinfo facility not available");
203*0Sstevel@tonic-gate 		/* not an error if this isn't the global zone */
204*0Sstevel@tonic-gate 		if (getzoneid() == GLOBAL_ZONEID)
205*0Sstevel@tonic-gate 			exit(-1);
206*0Sstevel@tonic-gate 		else
207*0Sstevel@tonic-gate 			exit(0);
208*0Sstevel@tonic-gate 	}
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	/*
211*0Sstevel@tonic-gate 	 * ...and walk all nodes to report them out...
212*0Sstevel@tonic-gate 	 */
213*0Sstevel@tonic-gate 	if (dbg.d_bydriver) {
214*0Sstevel@tonic-gate 		opts.o_target = 0;
215*0Sstevel@tonic-gate 		walk_driver(root_node, devlink_hdl);
216*0Sstevel@tonic-gate 		if (devlink_hdl != NULL)
217*0Sstevel@tonic-gate 			(void) di_devlink_fini(&devlink_hdl);
218*0Sstevel@tonic-gate 		di_fini(root_node);
219*0Sstevel@tonic-gate 		return;
220*0Sstevel@tonic-gate 	}
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	if (opts.o_target) {
223*0Sstevel@tonic-gate 		di_node_t target_node, node;
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 		target_node = find_target_node(root_node);
226*0Sstevel@tonic-gate 		if (target_node == DI_NODE_NIL) {
227*0Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: "
228*0Sstevel@tonic-gate 					"invalid device path specified\n",
229*0Sstevel@tonic-gate 					opts.o_progname);
230*0Sstevel@tonic-gate 			exit(1);
231*0Sstevel@tonic-gate 		}
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 		/* mark the target node so we display it */
234*0Sstevel@tonic-gate 		node_display_set(target_node);
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 		if (opts.o_ancestors) {
237*0Sstevel@tonic-gate 			/*
238*0Sstevel@tonic-gate 			 * mark the ancestors of this node so we display
239*0Sstevel@tonic-gate 			 * them as well
240*0Sstevel@tonic-gate 			 */
241*0Sstevel@tonic-gate 			node = target_node;
242*0Sstevel@tonic-gate 			while (node = di_parent_node(node))
243*0Sstevel@tonic-gate 				node_display_set(node);
244*0Sstevel@tonic-gate 		} else {
245*0Sstevel@tonic-gate 			/*
246*0Sstevel@tonic-gate 			 * when we display device tree nodes the indentation
247*0Sstevel@tonic-gate 			 * level is based off of tree depth.
248*0Sstevel@tonic-gate 			 *
249*0Sstevel@tonic-gate 			 * here we increment o_target to reflect the
250*0Sstevel@tonic-gate 			 * depth of the target node in the tree.  we do
251*0Sstevel@tonic-gate 			 * this so that when we calculate the indentation
252*0Sstevel@tonic-gate 			 * level we can subtract o_target so that the
253*0Sstevel@tonic-gate 			 * target node starts with an indentation of zero.
254*0Sstevel@tonic-gate 			 */
255*0Sstevel@tonic-gate 			node = target_node;
256*0Sstevel@tonic-gate 			while (node = di_parent_node(node))
257*0Sstevel@tonic-gate 				opts.o_target++;
258*0Sstevel@tonic-gate 		}
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 		if (opts.o_children) {
261*0Sstevel@tonic-gate 			/*
262*0Sstevel@tonic-gate 			 * mark the children of this node so we display
263*0Sstevel@tonic-gate 			 * them as well
264*0Sstevel@tonic-gate 			 */
265*0Sstevel@tonic-gate 			(void) di_walk_node(target_node, DI_WALK_CLDFIRST,
266*0Sstevel@tonic-gate 					    (void *)1,
267*0Sstevel@tonic-gate 					    (int (*)(di_node_t, void *))
268*0Sstevel@tonic-gate 					    node_display_set);
269*0Sstevel@tonic-gate 		}
270*0Sstevel@tonic-gate 	}
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	(void) di_walk_node(root_node, DI_WALK_CLDFIRST, devlink_hdl,
273*0Sstevel@tonic-gate 				dump_devs);
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	if (devlink_hdl != NULL)
276*0Sstevel@tonic-gate 		(void) di_devlink_fini(&devlink_hdl);
277*0Sstevel@tonic-gate 	di_fini(root_node);
278*0Sstevel@tonic-gate }
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate /*
281*0Sstevel@tonic-gate  * utility routines
282*0Sstevel@tonic-gate  */
283*0Sstevel@tonic-gate static int
284*0Sstevel@tonic-gate i_find_target_node(di_node_t node, void *arg)
285*0Sstevel@tonic-gate {
286*0Sstevel@tonic-gate 	di_node_t *target = (di_node_t *)arg;
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 	if (opts.o_devices_path != NULL) {
289*0Sstevel@tonic-gate 		char *path;
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 		if ((path = di_devfs_path(node)) == NULL)
292*0Sstevel@tonic-gate 			exit(_error("failed to allocate memory"));
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 		if (strcmp(opts.o_devices_path, path) == 0) {
295*0Sstevel@tonic-gate 			di_devfs_path_free(path);
296*0Sstevel@tonic-gate 			*target = node;
297*0Sstevel@tonic-gate 			return (DI_WALK_TERMINATE);
298*0Sstevel@tonic-gate 		}
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 		di_devfs_path_free(path);
301*0Sstevel@tonic-gate 	} else if (opts.o_devt != DDI_DEV_T_NONE) {
302*0Sstevel@tonic-gate 		di_minor_t	minor = DI_MINOR_NIL;
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 		while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
305*0Sstevel@tonic-gate 			if (opts.o_devt == di_minor_devt(minor)) {
306*0Sstevel@tonic-gate 				*target = node;
307*0Sstevel@tonic-gate 				return (DI_WALK_TERMINATE);
308*0Sstevel@tonic-gate 			}
309*0Sstevel@tonic-gate 		}
310*0Sstevel@tonic-gate 	} else {
311*0Sstevel@tonic-gate 		/* we should never get here */
312*0Sstevel@tonic-gate 		exit(_error(NULL, "internal error"));
313*0Sstevel@tonic-gate 	}
314*0Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
315*0Sstevel@tonic-gate }
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate static di_node_t
318*0Sstevel@tonic-gate find_target_node(di_node_t root_node)
319*0Sstevel@tonic-gate {
320*0Sstevel@tonic-gate 	di_node_t target = DI_NODE_NIL;
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 	/* special case to allow displaying of the root node */
323*0Sstevel@tonic-gate 	if (opts.o_devices_path != NULL) {
324*0Sstevel@tonic-gate 		if (strlen(opts.o_devices_path) == 0)
325*0Sstevel@tonic-gate 			return (root_node);
326*0Sstevel@tonic-gate 		if (strcmp(opts.o_devices_path, ".") == 0)
327*0Sstevel@tonic-gate 			return (root_node);
328*0Sstevel@tonic-gate 	}
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 	(void) di_walk_node(root_node, DI_WALK_CLDFIRST, &target,
331*0Sstevel@tonic-gate 				i_find_target_node);
332*0Sstevel@tonic-gate 	return (target);
333*0Sstevel@tonic-gate }
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate #define	NODE_DISPLAY		(1<<0)
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate static long
338*0Sstevel@tonic-gate node_display(di_node_t node)
339*0Sstevel@tonic-gate {
340*0Sstevel@tonic-gate 	long data = (long)di_node_private_get(node);
341*0Sstevel@tonic-gate 	return (data & NODE_DISPLAY);
342*0Sstevel@tonic-gate }
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate static void
345*0Sstevel@tonic-gate node_display_set(di_node_t node)
346*0Sstevel@tonic-gate {
347*0Sstevel@tonic-gate 	long data = (long)di_node_private_get(node);
348*0Sstevel@tonic-gate 	data |= NODE_DISPLAY;
349*0Sstevel@tonic-gate 	di_node_private_set(node, (void *)data);
350*0Sstevel@tonic-gate }
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate #define	LNODE_DISPLAYED		(1<<0)
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate static long
355*0Sstevel@tonic-gate lnode_displayed(di_lnode_t lnode)
356*0Sstevel@tonic-gate {
357*0Sstevel@tonic-gate 	long data = (long)di_lnode_private_get(lnode);
358*0Sstevel@tonic-gate 	return (data & LNODE_DISPLAYED);
359*0Sstevel@tonic-gate }
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate static void
362*0Sstevel@tonic-gate lnode_displayed_set(di_lnode_t lnode)
363*0Sstevel@tonic-gate {
364*0Sstevel@tonic-gate 	long data = (long)di_lnode_private_get(lnode);
365*0Sstevel@tonic-gate 	data |= LNODE_DISPLAYED;
366*0Sstevel@tonic-gate 	di_lnode_private_set(lnode, (void *)data);
367*0Sstevel@tonic-gate }
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate static void
370*0Sstevel@tonic-gate lnode_displayed_clear(di_lnode_t lnode)
371*0Sstevel@tonic-gate {
372*0Sstevel@tonic-gate 	long data = (long)di_lnode_private_get(lnode);
373*0Sstevel@tonic-gate 	data &= ~LNODE_DISPLAYED;
374*0Sstevel@tonic-gate 	di_lnode_private_set(lnode, (void *)data);
375*0Sstevel@tonic-gate }
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate #define	MINOR_DISPLAYED		(1<<0)
378*0Sstevel@tonic-gate #define	MINOR_PTR		(~(0x3))
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate static long
381*0Sstevel@tonic-gate minor_displayed(di_minor_t minor)
382*0Sstevel@tonic-gate {
383*0Sstevel@tonic-gate 	long data = (long)di_minor_private_get(minor);
384*0Sstevel@tonic-gate 	return (data & MINOR_DISPLAYED);
385*0Sstevel@tonic-gate }
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate static void
388*0Sstevel@tonic-gate minor_displayed_set(di_minor_t minor)
389*0Sstevel@tonic-gate {
390*0Sstevel@tonic-gate 	long data = (long)di_minor_private_get(minor);
391*0Sstevel@tonic-gate 	data |= MINOR_DISPLAYED;
392*0Sstevel@tonic-gate 	di_minor_private_set(minor, (void *)data);
393*0Sstevel@tonic-gate }
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate static void
396*0Sstevel@tonic-gate minor_displayed_clear(di_minor_t minor)
397*0Sstevel@tonic-gate {
398*0Sstevel@tonic-gate 	long data = (long)di_minor_private_get(minor);
399*0Sstevel@tonic-gate 	data &= ~MINOR_DISPLAYED;
400*0Sstevel@tonic-gate 	di_minor_private_set(minor, (void *)data);
401*0Sstevel@tonic-gate }
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate static void *
404*0Sstevel@tonic-gate minor_ptr(di_minor_t minor)
405*0Sstevel@tonic-gate {
406*0Sstevel@tonic-gate 	long data = (long)di_minor_private_get(minor);
407*0Sstevel@tonic-gate 	return ((void *)(data & MINOR_PTR));
408*0Sstevel@tonic-gate }
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate static void
411*0Sstevel@tonic-gate minor_ptr_set(di_minor_t minor, void *ptr)
412*0Sstevel@tonic-gate {
413*0Sstevel@tonic-gate 	long data = (long)di_minor_private_get(minor);
414*0Sstevel@tonic-gate 	data = (data & ~MINOR_PTR) | (((long)ptr) & MINOR_PTR);
415*0Sstevel@tonic-gate 	di_minor_private_set(minor, (void *)data);
416*0Sstevel@tonic-gate }
417*0Sstevel@tonic-gate 
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate /*
420*0Sstevel@tonic-gate  * In this comment typed properties are those of type DI_PROP_TYPE_UNDEF_IT,
421*0Sstevel@tonic-gate  * DI_PROP_TYPE_BOOLEAN, DI_PROP_TYPE_INT, DI_PROP_TYPE_INT64,
422*0Sstevel@tonic-gate  * DI_PROP_TYPE_BYTE, and DI_PROP_TYPE_STRING.
423*0Sstevel@tonic-gate  *
424*0Sstevel@tonic-gate  * The guessing algorithm is:
425*0Sstevel@tonic-gate  * 1. If the property is typed and the type is consistent with the value of
426*0Sstevel@tonic-gate  *    the property, then the property is of that type. If the type is not
427*0Sstevel@tonic-gate  *    consistent with value of the property, then the type is treated as
428*0Sstevel@tonic-gate  *    alien to prtconf.
429*0Sstevel@tonic-gate  * 2. If the property is of type DI_PROP_TYPE_UNKNOWN the following steps
430*0Sstevel@tonic-gate  *    are carried out.
431*0Sstevel@tonic-gate  *    a. If the value of the property is consistent with a string property,
432*0Sstevel@tonic-gate  *       the type of the property is DI_PROP_TYPE_STRING.
433*0Sstevel@tonic-gate  *    b. Otherwise, if the value of the property is consistent with an integer
434*0Sstevel@tonic-gate  *       property, the type of the property is DI_PROP_TYPE_INT.
435*0Sstevel@tonic-gate  *    c. Otherwise, the property type is treated as alien to prtconf.
436*0Sstevel@tonic-gate  * 3. If the property type is alien to prtconf, then the property value is
437*0Sstevel@tonic-gate  *    read by the appropriate routine for untyped properties and the following
438*0Sstevel@tonic-gate  *    steps are carried out.
439*0Sstevel@tonic-gate  *    a. If the length that the property routine returned is zero, the
440*0Sstevel@tonic-gate  *       property is of type DI_PROP_TYPE_BOOLEAN.
441*0Sstevel@tonic-gate  *    b. Otherwise, if the length that the property routine returned is
442*0Sstevel@tonic-gate  *       positive, then the property value is treated as raw data of type
443*0Sstevel@tonic-gate  *       DI_PROP_TYPE_UNKNOWN.
444*0Sstevel@tonic-gate  *    c. Otherwise, if the length that the property routine returned is
445*0Sstevel@tonic-gate  *       negative, then there is some internal inconsistency and this is
446*0Sstevel@tonic-gate  *       treated as an error and no type is determined.
447*0Sstevel@tonic-gate  */
448*0Sstevel@tonic-gate static int
449*0Sstevel@tonic-gate prop_type_guess(const dumpops_t *propops, void *prop, void **prop_data,
450*0Sstevel@tonic-gate     int *prop_type)
451*0Sstevel@tonic-gate {
452*0Sstevel@tonic-gate 	int len, type;
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate 	type = PROPTYPE(propops)(prop);
455*0Sstevel@tonic-gate 	switch (type) {
456*0Sstevel@tonic-gate 	case DI_PROP_TYPE_UNDEF_IT:
457*0Sstevel@tonic-gate 	case DI_PROP_TYPE_BOOLEAN:
458*0Sstevel@tonic-gate 		*prop_data = NULL;
459*0Sstevel@tonic-gate 		*prop_type = type;
460*0Sstevel@tonic-gate 		return (0);
461*0Sstevel@tonic-gate 	case DI_PROP_TYPE_INT:
462*0Sstevel@tonic-gate 		len = PROPINTS(propops)(prop, (int **)prop_data);
463*0Sstevel@tonic-gate 		break;
464*0Sstevel@tonic-gate 	case DI_PROP_TYPE_INT64:
465*0Sstevel@tonic-gate 		len = PROPINT64(propops)(prop, (int64_t **)prop_data);
466*0Sstevel@tonic-gate 		break;
467*0Sstevel@tonic-gate 	case DI_PROP_TYPE_BYTE:
468*0Sstevel@tonic-gate 		len = PROPBYTES(propops)(prop, (uchar_t **)prop_data);
469*0Sstevel@tonic-gate 		break;
470*0Sstevel@tonic-gate 	case DI_PROP_TYPE_STRING:
471*0Sstevel@tonic-gate 		len = PROPSTRINGS(propops)(prop, (char **)prop_data);
472*0Sstevel@tonic-gate 		break;
473*0Sstevel@tonic-gate 	case DI_PROP_TYPE_UNKNOWN:
474*0Sstevel@tonic-gate 		len = PROPSTRINGS(propops)(prop, (char **)prop_data);
475*0Sstevel@tonic-gate 		if ((len > 0) && ((*(char **)prop_data)[0] != 0)) {
476*0Sstevel@tonic-gate 			*prop_type = DI_PROP_TYPE_STRING;
477*0Sstevel@tonic-gate 			return (len);
478*0Sstevel@tonic-gate 		}
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate 		len = PROPINTS(propops)(prop, (int **)prop_data);
481*0Sstevel@tonic-gate 		type = DI_PROP_TYPE_INT;
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate 		break;
484*0Sstevel@tonic-gate 	default:
485*0Sstevel@tonic-gate 		len = -1;
486*0Sstevel@tonic-gate 	}
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate 	if (len > 0) {
489*0Sstevel@tonic-gate 		*prop_type = type;
490*0Sstevel@tonic-gate 		return (len);
491*0Sstevel@tonic-gate 	}
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate 	len = PROPRAWDATA(propops)(prop, (uchar_t **)prop_data);
494*0Sstevel@tonic-gate 	if (len < 0) {
495*0Sstevel@tonic-gate 		return (-1);
496*0Sstevel@tonic-gate 	} else if (len == 0) {
497*0Sstevel@tonic-gate 		*prop_type = DI_PROP_TYPE_BOOLEAN;
498*0Sstevel@tonic-gate 		return (0);
499*0Sstevel@tonic-gate 	}
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 	*prop_type = DI_PROP_TYPE_UNKNOWN;
502*0Sstevel@tonic-gate 	return (len);
503*0Sstevel@tonic-gate }
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate static void
506*0Sstevel@tonic-gate dump_prop_list_common(const dumpops_t *dumpops, int ilev, void *node)
507*0Sstevel@tonic-gate {
508*0Sstevel@tonic-gate 	void *prop = DI_PROP_NIL, *prop_data;
509*0Sstevel@tonic-gate 	char *p;
510*0Sstevel@tonic-gate 	int i, prop_type, nitems;
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate 	while ((prop = NEXTPROP(dumpops)(node, prop)) != DI_PROP_NIL) {
513*0Sstevel@tonic-gate 		nitems = prop_type_guess(dumpops, prop, &prop_data, &prop_type);
514*0Sstevel@tonic-gate 		if (nitems < 0)
515*0Sstevel@tonic-gate 			continue;
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate 		indent_to_level(ilev);
518*0Sstevel@tonic-gate 		(void) printf("name='%s' type=", PROPNAME(dumpops)(prop));
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate 		switch (prop_type) {
521*0Sstevel@tonic-gate 		case DI_PROP_TYPE_UNDEF_IT:
522*0Sstevel@tonic-gate 			(void) printf("undef");
523*0Sstevel@tonic-gate 			break;
524*0Sstevel@tonic-gate 		case DI_PROP_TYPE_BOOLEAN:
525*0Sstevel@tonic-gate 			(void) printf("boolean");
526*0Sstevel@tonic-gate 			break;
527*0Sstevel@tonic-gate 		case DI_PROP_TYPE_INT:
528*0Sstevel@tonic-gate 			(void) printf("int");
529*0Sstevel@tonic-gate 			break;
530*0Sstevel@tonic-gate 		case DI_PROP_TYPE_INT64:
531*0Sstevel@tonic-gate 			(void) printf("int64");
532*0Sstevel@tonic-gate 			break;
533*0Sstevel@tonic-gate 		case DI_PROP_TYPE_BYTE:
534*0Sstevel@tonic-gate 			(void) printf("byte");
535*0Sstevel@tonic-gate 			break;
536*0Sstevel@tonic-gate 		case DI_PROP_TYPE_STRING:
537*0Sstevel@tonic-gate 			(void) printf("string");
538*0Sstevel@tonic-gate 			break;
539*0Sstevel@tonic-gate 		case DI_PROP_TYPE_UNKNOWN:
540*0Sstevel@tonic-gate 			(void) printf("unknown");
541*0Sstevel@tonic-gate 			break;
542*0Sstevel@tonic-gate 		default:
543*0Sstevel@tonic-gate 			/* Should never be here */
544*0Sstevel@tonic-gate 			(void) printf("0x%x", prop_type);
545*0Sstevel@tonic-gate 		}
546*0Sstevel@tonic-gate 
547*0Sstevel@tonic-gate 		if (nitems != 0)
548*0Sstevel@tonic-gate 			(void) printf(" items=%i", nitems);
549*0Sstevel@tonic-gate 
550*0Sstevel@tonic-gate 		/* print the major and minor numbers for a device property */
551*0Sstevel@tonic-gate 		if (PROPDEVT(dumpops) != NULL) {
552*0Sstevel@tonic-gate 			dev_t dev;
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate 			dev = PROPDEVT(dumpops)(prop);
555*0Sstevel@tonic-gate 			if (dev != DDI_DEV_T_NONE) {
556*0Sstevel@tonic-gate 				(void) printf(" dev=(%u,%u)",
557*0Sstevel@tonic-gate 				    (uint_t)major(dev), (uint_t)minor(dev));
558*0Sstevel@tonic-gate 			} else {
559*0Sstevel@tonic-gate 				(void) printf(" dev=none");
560*0Sstevel@tonic-gate 			}
561*0Sstevel@tonic-gate 		}
562*0Sstevel@tonic-gate 
563*0Sstevel@tonic-gate 		(void) putchar('\n');
564*0Sstevel@tonic-gate 
565*0Sstevel@tonic-gate 		if (nitems == 0)
566*0Sstevel@tonic-gate 			continue;
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 		indent_to_level(ilev);
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate 		(void) printf("    value=");
571*0Sstevel@tonic-gate 
572*0Sstevel@tonic-gate 		switch (prop_type) {
573*0Sstevel@tonic-gate 		case DI_PROP_TYPE_INT:
574*0Sstevel@tonic-gate 			for (i = 0; i < nitems - 1; i++)
575*0Sstevel@tonic-gate 				(void) printf("%8.8x.", ((int *)prop_data)[i]);
576*0Sstevel@tonic-gate 			(void) printf("%8.8x", ((int *)prop_data)[i]);
577*0Sstevel@tonic-gate 			break;
578*0Sstevel@tonic-gate 		case DI_PROP_TYPE_INT64:
579*0Sstevel@tonic-gate 			for (i = 0; i < nitems - 1; i++)
580*0Sstevel@tonic-gate 				(void) printf("%16.16llx.",
581*0Sstevel@tonic-gate 				    ((long long *)prop_data)[i]);
582*0Sstevel@tonic-gate 			(void) printf("%16.16llx", ((long long *)prop_data)[i]);
583*0Sstevel@tonic-gate 			break;
584*0Sstevel@tonic-gate 		case DI_PROP_TYPE_STRING:
585*0Sstevel@tonic-gate 			p = (char *)prop_data;
586*0Sstevel@tonic-gate 			for (i = 0; i < nitems - 1; i++) {
587*0Sstevel@tonic-gate 				(void) printf("'%s' + ", p);
588*0Sstevel@tonic-gate 				p += strlen(p) + 1;
589*0Sstevel@tonic-gate 			}
590*0Sstevel@tonic-gate 			(void) printf("'%s'", p);
591*0Sstevel@tonic-gate 			break;
592*0Sstevel@tonic-gate 		default:
593*0Sstevel@tonic-gate 			for (i = 0; i < nitems - 1; i++)
594*0Sstevel@tonic-gate 				(void) printf("%2.2x.",
595*0Sstevel@tonic-gate 				    ((uint8_t *)prop_data)[i]);
596*0Sstevel@tonic-gate 			(void) printf("%2.2x", ((uint8_t *)prop_data)[i]);
597*0Sstevel@tonic-gate 		}
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 		(void) putchar('\n');
600*0Sstevel@tonic-gate 	}
601*0Sstevel@tonic-gate }
602*0Sstevel@tonic-gate 
603*0Sstevel@tonic-gate /*
604*0Sstevel@tonic-gate  * walk_driver is a debugging facility.
605*0Sstevel@tonic-gate  */
606*0Sstevel@tonic-gate static void
607*0Sstevel@tonic-gate walk_driver(di_node_t root, di_devlink_handle_t devlink_hdl)
608*0Sstevel@tonic-gate {
609*0Sstevel@tonic-gate 	di_node_t node;
610*0Sstevel@tonic-gate 
611*0Sstevel@tonic-gate 	node = di_drv_first_node(dbg.d_drivername, root);
612*0Sstevel@tonic-gate 
613*0Sstevel@tonic-gate 	while (node != DI_NODE_NIL) {
614*0Sstevel@tonic-gate 		(void) dump_devs(node, devlink_hdl);
615*0Sstevel@tonic-gate 		node = di_drv_next_node(node);
616*0Sstevel@tonic-gate 	}
617*0Sstevel@tonic-gate }
618*0Sstevel@tonic-gate 
619*0Sstevel@tonic-gate /*
620*0Sstevel@tonic-gate  * print out information about this node, returns appropriate code.
621*0Sstevel@tonic-gate  */
622*0Sstevel@tonic-gate /*ARGSUSED1*/
623*0Sstevel@tonic-gate static int
624*0Sstevel@tonic-gate dump_devs(di_node_t node, void *arg)
625*0Sstevel@tonic-gate {
626*0Sstevel@tonic-gate 	di_devlink_handle_t	devlink_hdl = (di_devlink_handle_t)arg;
627*0Sstevel@tonic-gate 	int			ilev = 0;	/* indentation level */
628*0Sstevel@tonic-gate 	char			*driver_name;
629*0Sstevel@tonic-gate 	di_node_t		root_node, tmp;
630*0Sstevel@tonic-gate 
631*0Sstevel@tonic-gate 	if (dbg.d_debug) {
632*0Sstevel@tonic-gate 		char *path = di_devfs_path(node);
633*0Sstevel@tonic-gate 		dprintf("Dump node %s\n", path);
634*0Sstevel@tonic-gate 		di_devfs_path_free(path);
635*0Sstevel@tonic-gate 	}
636*0Sstevel@tonic-gate 
637*0Sstevel@tonic-gate 	if (dbg.d_bydriver) {
638*0Sstevel@tonic-gate 		ilev = 1;
639*0Sstevel@tonic-gate 	} else {
640*0Sstevel@tonic-gate 		/* figure out indentation level */
641*0Sstevel@tonic-gate 		tmp = node;
642*0Sstevel@tonic-gate 		while ((tmp = di_parent_node(tmp)) != DI_NODE_NIL)
643*0Sstevel@tonic-gate 			ilev++;
644*0Sstevel@tonic-gate 
645*0Sstevel@tonic-gate 		if (opts.o_target && !opts.o_ancestors) {
646*0Sstevel@tonic-gate 			ilev -= opts.o_target - 1;
647*0Sstevel@tonic-gate 		}
648*0Sstevel@tonic-gate 	}
649*0Sstevel@tonic-gate 
650*0Sstevel@tonic-gate 	if (opts.o_target && !node_display(node)) {
651*0Sstevel@tonic-gate 		/*
652*0Sstevel@tonic-gate 		 * if we're only displaying certain nodes and this one
653*0Sstevel@tonic-gate 		 * isn't flagged, skip it.
654*0Sstevel@tonic-gate 		 */
655*0Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
656*0Sstevel@tonic-gate 	}
657*0Sstevel@tonic-gate 
658*0Sstevel@tonic-gate 	indent_to_level(ilev);
659*0Sstevel@tonic-gate 
660*0Sstevel@tonic-gate 	(void) printf("%s", di_node_name(node));
661*0Sstevel@tonic-gate 
662*0Sstevel@tonic-gate 	/*
663*0Sstevel@tonic-gate 	 * if this node does not have an instance number or is the
664*0Sstevel@tonic-gate 	 * root node (1229946), we don't print an instance number
665*0Sstevel@tonic-gate 	 */
666*0Sstevel@tonic-gate 	root_node = tmp = node;
667*0Sstevel@tonic-gate 	while ((tmp = di_parent_node(tmp)) != DI_NODE_NIL)
668*0Sstevel@tonic-gate 		root_node = tmp;
669*0Sstevel@tonic-gate 	if ((di_instance(node) >= 0) && (node != root_node))
670*0Sstevel@tonic-gate 		(void) printf(", instance #%d", di_instance(node));
671*0Sstevel@tonic-gate 
672*0Sstevel@tonic-gate 	if (opts.o_drv_name) {
673*0Sstevel@tonic-gate 		driver_name = di_driver_name(node);
674*0Sstevel@tonic-gate 		if (driver_name != NULL)
675*0Sstevel@tonic-gate 			(void) printf(" (driver name: %s)", driver_name);
676*0Sstevel@tonic-gate 	} else if (di_state(node) & DI_DRIVER_DETACHED)
677*0Sstevel@tonic-gate 		(void) printf(" (driver not attached)");
678*0Sstevel@tonic-gate 
679*0Sstevel@tonic-gate 	(void) printf("\n");
680*0Sstevel@tonic-gate 
681*0Sstevel@tonic-gate 	if (opts.o_verbose)  {
682*0Sstevel@tonic-gate 		if (dump_prop_list(&sysprop_dumpops, "System", ilev + 1,
683*0Sstevel@tonic-gate 		    node)) {
684*0Sstevel@tonic-gate 			(void) dump_prop_list(&globprop_dumpops, NULL, ilev + 1,
685*0Sstevel@tonic-gate 			    node);
686*0Sstevel@tonic-gate 		} else {
687*0Sstevel@tonic-gate 			(void) dump_prop_list(&globprop_dumpops,
688*0Sstevel@tonic-gate 			    "System software", ilev + 1, node);
689*0Sstevel@tonic-gate 		}
690*0Sstevel@tonic-gate 		(void) dump_prop_list(&drvprop_dumpops, "Driver", ilev + 1,
691*0Sstevel@tonic-gate 		    node);
692*0Sstevel@tonic-gate 		(void) dump_prop_list(&hwprop_dumpops, "Hardware", ilev + 1,
693*0Sstevel@tonic-gate 		    node);
694*0Sstevel@tonic-gate 		dump_priv_data(ilev + 1, node);
695*0Sstevel@tonic-gate 		dump_pathing_data(ilev + 1, node);
696*0Sstevel@tonic-gate 		dump_link_data(ilev + 1, node, devlink_hdl);
697*0Sstevel@tonic-gate 		dump_minor_data(ilev + 1, node, devlink_hdl);
698*0Sstevel@tonic-gate 	}
699*0Sstevel@tonic-gate 
700*0Sstevel@tonic-gate 	if (opts.o_target)
701*0Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
702*0Sstevel@tonic-gate 
703*0Sstevel@tonic-gate 	if (!opts.o_pseudodevs && (strcmp(di_node_name(node), "pseudo") == 0))
704*0Sstevel@tonic-gate 		return (DI_WALK_PRUNECHILD);
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
707*0Sstevel@tonic-gate }
708*0Sstevel@tonic-gate 
709*0Sstevel@tonic-gate /*
710*0Sstevel@tonic-gate  * Returns 0 if nothing is printed, 1 otherwise
711*0Sstevel@tonic-gate  */
712*0Sstevel@tonic-gate static int
713*0Sstevel@tonic-gate dump_prop_list(const dumpops_t *dumpops, const char *name, int ilev,
714*0Sstevel@tonic-gate     di_node_t node)
715*0Sstevel@tonic-gate {
716*0Sstevel@tonic-gate 	if (NEXTPROP(dumpops)(node, DI_PROP_NIL) == DI_PROP_NIL)
717*0Sstevel@tonic-gate 		return (0);
718*0Sstevel@tonic-gate 
719*0Sstevel@tonic-gate 	if (name != NULL)  {
720*0Sstevel@tonic-gate 		indent_to_level(ilev);
721*0Sstevel@tonic-gate 		(void) printf("%s properties:\n", name);
722*0Sstevel@tonic-gate 	}
723*0Sstevel@tonic-gate 
724*0Sstevel@tonic-gate 	dump_prop_list_common(dumpops, ilev + 1, node);
725*0Sstevel@tonic-gate 	return (1);
726*0Sstevel@tonic-gate }
727*0Sstevel@tonic-gate 
728*0Sstevel@tonic-gate 
729*0Sstevel@tonic-gate /* _error([no_perror, ] fmt [, arg ...]) */
730*0Sstevel@tonic-gate static int
731*0Sstevel@tonic-gate _error(const char *opt_noperror, ...)
732*0Sstevel@tonic-gate {
733*0Sstevel@tonic-gate 	int saved_errno;
734*0Sstevel@tonic-gate 	va_list ap;
735*0Sstevel@tonic-gate 	int no_perror = 0;
736*0Sstevel@tonic-gate 	const char *fmt;
737*0Sstevel@tonic-gate 
738*0Sstevel@tonic-gate 	saved_errno = errno;
739*0Sstevel@tonic-gate 
740*0Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", opts.o_progname);
741*0Sstevel@tonic-gate 
742*0Sstevel@tonic-gate 	va_start(ap, opt_noperror);
743*0Sstevel@tonic-gate 	if (opt_noperror == NULL) {
744*0Sstevel@tonic-gate 		no_perror = 1;
745*0Sstevel@tonic-gate 		fmt = va_arg(ap, char *);
746*0Sstevel@tonic-gate 	} else
747*0Sstevel@tonic-gate 		fmt = opt_noperror;
748*0Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
749*0Sstevel@tonic-gate 	va_end(ap);
750*0Sstevel@tonic-gate 
751*0Sstevel@tonic-gate 	if (no_perror)
752*0Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
753*0Sstevel@tonic-gate 	else {
754*0Sstevel@tonic-gate 		(void) fprintf(stderr, ": ");
755*0Sstevel@tonic-gate 		errno = saved_errno;
756*0Sstevel@tonic-gate 		perror("");
757*0Sstevel@tonic-gate 	}
758*0Sstevel@tonic-gate 
759*0Sstevel@tonic-gate 	return (-1);
760*0Sstevel@tonic-gate }
761*0Sstevel@tonic-gate 
762*0Sstevel@tonic-gate 
763*0Sstevel@tonic-gate /*
764*0Sstevel@tonic-gate  * The rest of the routines handle printing the raw prom devinfo (-p option).
765*0Sstevel@tonic-gate  *
766*0Sstevel@tonic-gate  * 128 is the size of the largest (currently) property name
767*0Sstevel@tonic-gate  * 16k - MAXNAMESZ - sizeof (int) is the size of the largest
768*0Sstevel@tonic-gate  * (currently) property value that is allowed.
769*0Sstevel@tonic-gate  * the sizeof (uint_t) is from struct openpromio
770*0Sstevel@tonic-gate  */
771*0Sstevel@tonic-gate 
772*0Sstevel@tonic-gate #define	MAXNAMESZ	128
773*0Sstevel@tonic-gate #define	MAXVALSIZE	(16384 - MAXNAMESZ - sizeof (uint_t))
774*0Sstevel@tonic-gate #define	BUFSIZE		(MAXNAMESZ + MAXVALSIZE + sizeof (uint_t))
775*0Sstevel@tonic-gate typedef union {
776*0Sstevel@tonic-gate 	char buf[BUFSIZE];
777*0Sstevel@tonic-gate 	struct openpromio opp;
778*0Sstevel@tonic-gate } Oppbuf;
779*0Sstevel@tonic-gate 
780*0Sstevel@tonic-gate static int prom_fd;
781*0Sstevel@tonic-gate static uchar_t *prom_snapshot;
782*0Sstevel@tonic-gate 
783*0Sstevel@tonic-gate static int
784*0Sstevel@tonic-gate is_openprom(void)
785*0Sstevel@tonic-gate {
786*0Sstevel@tonic-gate 	Oppbuf	oppbuf;
787*0Sstevel@tonic-gate 	struct openpromio *opp = &(oppbuf.opp);
788*0Sstevel@tonic-gate 	unsigned int i;
789*0Sstevel@tonic-gate 
790*0Sstevel@tonic-gate 	opp->oprom_size = MAXVALSIZE;
791*0Sstevel@tonic-gate 	if (ioctl(prom_fd, OPROMGETCONS, opp) < 0)
792*0Sstevel@tonic-gate 		exit(_error("OPROMGETCONS"));
793*0Sstevel@tonic-gate 
794*0Sstevel@tonic-gate 	i = (unsigned int)((unsigned char)opp->oprom_array[0]);
795*0Sstevel@tonic-gate 	return ((i & OPROMCONS_OPENPROM) == OPROMCONS_OPENPROM);
796*0Sstevel@tonic-gate }
797*0Sstevel@tonic-gate 
798*0Sstevel@tonic-gate int
799*0Sstevel@tonic-gate do_prominfo(void)
800*0Sstevel@tonic-gate {
801*0Sstevel@tonic-gate 	uint_t arg = opts.o_verbose;
802*0Sstevel@tonic-gate 
803*0Sstevel@tonic-gate 	if (promopen(O_RDONLY))  {
804*0Sstevel@tonic-gate 		exit(_error("openeepr device open failed"));
805*0Sstevel@tonic-gate 	}
806*0Sstevel@tonic-gate 
807*0Sstevel@tonic-gate 	if (is_openprom() == 0)  {
808*0Sstevel@tonic-gate 		(void) fprintf(stderr, "System architecture does not "
809*0Sstevel@tonic-gate 		    "support this option of this command.\n");
810*0Sstevel@tonic-gate 		return (1);
811*0Sstevel@tonic-gate 	}
812*0Sstevel@tonic-gate 
813*0Sstevel@tonic-gate 	/* OPROMSNAPSHOT returns size in arg */
814*0Sstevel@tonic-gate 	if (ioctl(prom_fd, OPROMSNAPSHOT, &arg) < 0)
815*0Sstevel@tonic-gate 		exit(_error("OPROMSNAPSHOT"));
816*0Sstevel@tonic-gate 
817*0Sstevel@tonic-gate 	if (arg == 0)
818*0Sstevel@tonic-gate 		return (1);
819*0Sstevel@tonic-gate 
820*0Sstevel@tonic-gate 	if ((prom_snapshot = malloc(arg)) == NULL)
821*0Sstevel@tonic-gate 		exit(_error("failed to allocate memory"));
822*0Sstevel@tonic-gate 
823*0Sstevel@tonic-gate 	/* copy out the snapshot for printing */
824*0Sstevel@tonic-gate 	/*LINTED*/
825*0Sstevel@tonic-gate 	*(uint_t *)prom_snapshot = arg;
826*0Sstevel@tonic-gate 	if (ioctl(prom_fd, OPROMCOPYOUT, prom_snapshot) < 0)
827*0Sstevel@tonic-gate 		exit(_error("OPROMCOPYOUT"));
828*0Sstevel@tonic-gate 
829*0Sstevel@tonic-gate 	promclose();
830*0Sstevel@tonic-gate 
831*0Sstevel@tonic-gate 	/* print out information */
832*0Sstevel@tonic-gate 	walk(prom_snapshot, arg, 0);
833*0Sstevel@tonic-gate 	free(prom_snapshot);
834*0Sstevel@tonic-gate 
835*0Sstevel@tonic-gate 	return (0);
836*0Sstevel@tonic-gate }
837*0Sstevel@tonic-gate 
838*0Sstevel@tonic-gate static void
839*0Sstevel@tonic-gate walk(uchar_t *buf, uint_t size, int level)
840*0Sstevel@tonic-gate {
841*0Sstevel@tonic-gate 	int error;
842*0Sstevel@tonic-gate 	nvlist_t *nvl, *cnvl;
843*0Sstevel@tonic-gate 	nvpair_t *child = NULL;
844*0Sstevel@tonic-gate 	uchar_t *cbuf = NULL;
845*0Sstevel@tonic-gate 	uint_t csize;
846*0Sstevel@tonic-gate 
847*0Sstevel@tonic-gate 	/* Expand to an nvlist */
848*0Sstevel@tonic-gate 	if (nvlist_unpack((char *)buf, size, &nvl, 0))
849*0Sstevel@tonic-gate 		exit(_error("error processing snapshot"));
850*0Sstevel@tonic-gate 
851*0Sstevel@tonic-gate 	/* print current node */
852*0Sstevel@tonic-gate 	dump_node(nvl, level);
853*0Sstevel@tonic-gate 
854*0Sstevel@tonic-gate 	/* print children */
855*0Sstevel@tonic-gate 	error = nvlist_lookup_byte_array(nvl, "@child", &cbuf, &csize);
856*0Sstevel@tonic-gate 	if ((error == ENOENT) || (cbuf == NULL))
857*0Sstevel@tonic-gate 		return;		/* no child exists */
858*0Sstevel@tonic-gate 
859*0Sstevel@tonic-gate 	if (error || nvlist_unpack((char *)cbuf, csize, &cnvl, 0))
860*0Sstevel@tonic-gate 		exit(_error("error processing snapshot"));
861*0Sstevel@tonic-gate 
862*0Sstevel@tonic-gate 	while (child = nvlist_next_nvpair(cnvl, child)) {
863*0Sstevel@tonic-gate 		char *name = nvpair_name(child);
864*0Sstevel@tonic-gate 		data_type_t type = nvpair_type(child);
865*0Sstevel@tonic-gate 		uchar_t *nodebuf;
866*0Sstevel@tonic-gate 		uint_t nodesize;
867*0Sstevel@tonic-gate 		if (strcmp("node", name) != 0) {
868*0Sstevel@tonic-gate 			dprintf("unexpected nvpair name %s != name\n", name);
869*0Sstevel@tonic-gate 			continue;
870*0Sstevel@tonic-gate 		}
871*0Sstevel@tonic-gate 		if (type != DATA_TYPE_BYTE_ARRAY) {
872*0Sstevel@tonic-gate 			dprintf("unexpected nvpair type %d, not byte array \n",
873*0Sstevel@tonic-gate 			    type);
874*0Sstevel@tonic-gate 			continue;
875*0Sstevel@tonic-gate 		}
876*0Sstevel@tonic-gate 
877*0Sstevel@tonic-gate 		(void) nvpair_value_byte_array(child,
878*0Sstevel@tonic-gate 		    (uchar_t **)&nodebuf, &nodesize);
879*0Sstevel@tonic-gate 		walk(nodebuf, nodesize, level + 1);
880*0Sstevel@tonic-gate 	}
881*0Sstevel@tonic-gate 
882*0Sstevel@tonic-gate 	nvlist_free(nvl);
883*0Sstevel@tonic-gate }
884*0Sstevel@tonic-gate 
885*0Sstevel@tonic-gate /*
886*0Sstevel@tonic-gate  * Print all properties and values
887*0Sstevel@tonic-gate  */
888*0Sstevel@tonic-gate static void
889*0Sstevel@tonic-gate dump_node(nvlist_t *nvl, int level)
890*0Sstevel@tonic-gate {
891*0Sstevel@tonic-gate 	int id = 0;
892*0Sstevel@tonic-gate 	char *name = NULL;
893*0Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
894*0Sstevel@tonic-gate 
895*0Sstevel@tonic-gate 	indent_to_level(level);
896*0Sstevel@tonic-gate 	(void) printf("Node");
897*0Sstevel@tonic-gate 	if (!opts.o_verbose) {
898*0Sstevel@tonic-gate 		if (nvlist_lookup_string(nvl, "name", &name))
899*0Sstevel@tonic-gate 			(void) printf("data not available");
900*0Sstevel@tonic-gate 		else
901*0Sstevel@tonic-gate 			(void) printf(" '%s'", name);
902*0Sstevel@tonic-gate 		(void) putchar('\n');
903*0Sstevel@tonic-gate 		return;
904*0Sstevel@tonic-gate 	}
905*0Sstevel@tonic-gate 	(void) nvlist_lookup_int32(nvl, "@nodeid", &id);
906*0Sstevel@tonic-gate 	(void) printf(" %#08x\n", id);
907*0Sstevel@tonic-gate 
908*0Sstevel@tonic-gate 	while (nvp = nvlist_next_nvpair(nvl, nvp)) {
909*0Sstevel@tonic-gate 		name = nvpair_name(nvp);
910*0Sstevel@tonic-gate 		if (name[0] == '@')
911*0Sstevel@tonic-gate 			continue;
912*0Sstevel@tonic-gate 
913*0Sstevel@tonic-gate 		print_one(nvp, level + 1);
914*0Sstevel@tonic-gate 	}
915*0Sstevel@tonic-gate 	(void) putchar('\n');
916*0Sstevel@tonic-gate }
917*0Sstevel@tonic-gate 
918*0Sstevel@tonic-gate static const char *
919*0Sstevel@tonic-gate path_state_name(di_path_state_t st)
920*0Sstevel@tonic-gate {
921*0Sstevel@tonic-gate 	switch (st) {
922*0Sstevel@tonic-gate 		case DI_PATH_STATE_ONLINE:
923*0Sstevel@tonic-gate 			return ("online");
924*0Sstevel@tonic-gate 		case DI_PATH_STATE_STANDBY:
925*0Sstevel@tonic-gate 			return ("standby");
926*0Sstevel@tonic-gate 		case DI_PATH_STATE_OFFLINE:
927*0Sstevel@tonic-gate 			return ("offline");
928*0Sstevel@tonic-gate 		case DI_PATH_STATE_FAULT:
929*0Sstevel@tonic-gate 			return ("faulted");
930*0Sstevel@tonic-gate 	}
931*0Sstevel@tonic-gate 	return ("unknown");
932*0Sstevel@tonic-gate }
933*0Sstevel@tonic-gate 
934*0Sstevel@tonic-gate /*
935*0Sstevel@tonic-gate  * Print all phci's each client is connected to.
936*0Sstevel@tonic-gate  */
937*0Sstevel@tonic-gate static void
938*0Sstevel@tonic-gate dump_pathing_data(int ilev, di_node_t node)
939*0Sstevel@tonic-gate {
940*0Sstevel@tonic-gate 	di_path_t pi = DI_PATH_NIL;
941*0Sstevel@tonic-gate 	int firsttime = 1;
942*0Sstevel@tonic-gate 
943*0Sstevel@tonic-gate 	if (node == DI_PATH_NIL)
944*0Sstevel@tonic-gate 		return;
945*0Sstevel@tonic-gate 
946*0Sstevel@tonic-gate 	while ((pi = di_path_next_phci(node, pi)) != DI_PATH_NIL) {
947*0Sstevel@tonic-gate 		di_node_t phci_node = di_path_phci_node(pi);
948*0Sstevel@tonic-gate 
949*0Sstevel@tonic-gate 		if (firsttime) {
950*0Sstevel@tonic-gate 			indent_to_level(ilev);
951*0Sstevel@tonic-gate 			firsttime = 0;
952*0Sstevel@tonic-gate 			ilev++;
953*0Sstevel@tonic-gate 			(void) printf("Paths from multipath bus adapters:\n");
954*0Sstevel@tonic-gate 		}
955*0Sstevel@tonic-gate 
956*0Sstevel@tonic-gate 		indent_to_level(ilev);
957*0Sstevel@tonic-gate 		(void) printf("%s#%d (%s)\n", di_driver_name(phci_node),
958*0Sstevel@tonic-gate 		    di_instance(phci_node), path_state_name(di_path_state(pi)));
959*0Sstevel@tonic-gate 		dump_prop_list_common(&pathprop_dumpops, ilev + 1, pi);
960*0Sstevel@tonic-gate 	}
961*0Sstevel@tonic-gate }
962*0Sstevel@tonic-gate 
963*0Sstevel@tonic-gate static int
964*0Sstevel@tonic-gate dump_minor_data_links(di_devlink_t devlink, void *arg)
965*0Sstevel@tonic-gate {
966*0Sstevel@tonic-gate 	int ilev = (intptr_t)arg;
967*0Sstevel@tonic-gate 	indent_to_level(ilev);
968*0Sstevel@tonic-gate 	(void) printf("dev_link=%s\n", di_devlink_path(devlink));
969*0Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
970*0Sstevel@tonic-gate }
971*0Sstevel@tonic-gate 
972*0Sstevel@tonic-gate static void
973*0Sstevel@tonic-gate dump_minor_data_paths(int ilev, di_minor_t minor,
974*0Sstevel@tonic-gate     di_devlink_handle_t devlink_hdl)
975*0Sstevel@tonic-gate {
976*0Sstevel@tonic-gate 	char	*path, *type;
977*0Sstevel@tonic-gate 	int	spec_type;
978*0Sstevel@tonic-gate 
979*0Sstevel@tonic-gate 	/* get the path to the device and the minor node name */
980*0Sstevel@tonic-gate 	if ((path = di_devfs_minor_path(minor)) == NULL)
981*0Sstevel@tonic-gate 		exit(_error("failed to allocate memory"));
982*0Sstevel@tonic-gate 
983*0Sstevel@tonic-gate 	/* display the path to this minor node */
984*0Sstevel@tonic-gate 	indent_to_level(ilev);
985*0Sstevel@tonic-gate 	(void) printf("dev_path=%s\n", path);
986*0Sstevel@tonic-gate 
987*0Sstevel@tonic-gate 	if (devlink_hdl != NULL) {
988*0Sstevel@tonic-gate 
989*0Sstevel@tonic-gate 		/* get the device minor node information */
990*0Sstevel@tonic-gate 		spec_type = di_minor_spectype(minor);
991*0Sstevel@tonic-gate 		switch (di_minor_type(minor)) {
992*0Sstevel@tonic-gate 			case DDM_MINOR:
993*0Sstevel@tonic-gate 				type = "minor";
994*0Sstevel@tonic-gate 				break;
995*0Sstevel@tonic-gate 			case DDM_ALIAS:
996*0Sstevel@tonic-gate 				type = "alias";
997*0Sstevel@tonic-gate 				break;
998*0Sstevel@tonic-gate 			case DDM_DEFAULT:
999*0Sstevel@tonic-gate 				type = "default";
1000*0Sstevel@tonic-gate 				break;
1001*0Sstevel@tonic-gate 			case DDM_INTERNAL_PATH:
1002*0Sstevel@tonic-gate 				type = "internal";
1003*0Sstevel@tonic-gate 				break;
1004*0Sstevel@tonic-gate 			default:
1005*0Sstevel@tonic-gate 				type = "unknown";
1006*0Sstevel@tonic-gate 				break;
1007*0Sstevel@tonic-gate 		}
1008*0Sstevel@tonic-gate 
1009*0Sstevel@tonic-gate 		/* display the device minor node information */
1010*0Sstevel@tonic-gate 		indent_to_level(ilev + 1);
1011*0Sstevel@tonic-gate 		(void) printf("spectype=%s type=%s\n",
1012*0Sstevel@tonic-gate 				(spec_type == S_IFBLK) ? "blk" : "chr", type);
1013*0Sstevel@tonic-gate 
1014*0Sstevel@tonic-gate 		/* display all the devlinks for this device minor node */
1015*0Sstevel@tonic-gate 		(void) di_devlink_walk(devlink_hdl, NULL, path,
1016*0Sstevel@tonic-gate 		    0, (void *)(intptr_t)(ilev + 1), dump_minor_data_links);
1017*0Sstevel@tonic-gate 	}
1018*0Sstevel@tonic-gate 
1019*0Sstevel@tonic-gate 	di_devfs_path_free(path);
1020*0Sstevel@tonic-gate }
1021*0Sstevel@tonic-gate 
1022*0Sstevel@tonic-gate static void
1023*0Sstevel@tonic-gate create_minor_list(di_node_t node)
1024*0Sstevel@tonic-gate {
1025*0Sstevel@tonic-gate 	di_minor_t	minor, minor_head, minor_tail, minor_prev, minor_walk;
1026*0Sstevel@tonic-gate 	int		major;
1027*0Sstevel@tonic-gate 
1028*0Sstevel@tonic-gate 	/* if there are no minor nodes, bail */
1029*0Sstevel@tonic-gate 	if (di_minor_next(node, DI_MINOR_NIL) == DI_MINOR_NIL)
1030*0Sstevel@tonic-gate 		return;
1031*0Sstevel@tonic-gate 
1032*0Sstevel@tonic-gate 	/*
1033*0Sstevel@tonic-gate 	 * here we want to create lists of minor nodes with the same
1034*0Sstevel@tonic-gate 	 * dev_t.  to do this we first sort all the minor nodes by devt.
1035*0Sstevel@tonic-gate 	 *
1036*0Sstevel@tonic-gate 	 * the algorithm used here is a bubble sort, so performance sucks.
1037*0Sstevel@tonic-gate 	 * but it's probably ok here because most device instances don't
1038*0Sstevel@tonic-gate 	 * have that many minor nodes.  also we're doing this as we're
1039*0Sstevel@tonic-gate 	 * displaying each node so it doesn't look like we're pausing
1040*0Sstevel@tonic-gate 	 * output for a long time.
1041*0Sstevel@tonic-gate 	 */
1042*0Sstevel@tonic-gate 	major = di_driver_major(node);
1043*0Sstevel@tonic-gate 	minor_head = minor_tail = minor = DI_MINOR_NIL;
1044*0Sstevel@tonic-gate 	while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
1045*0Sstevel@tonic-gate 		dev_t	dev = di_minor_devt(minor);
1046*0Sstevel@tonic-gate 
1047*0Sstevel@tonic-gate 		/* skip /pseudo/clone@0 minor nodes */
1048*0Sstevel@tonic-gate 		if (major != major(dev))
1049*0Sstevel@tonic-gate 			continue;
1050*0Sstevel@tonic-gate 
1051*0Sstevel@tonic-gate 		minor_ptr_set(minor, DI_MINOR_NIL);
1052*0Sstevel@tonic-gate 		if (minor_head == DI_MINOR_NIL) {
1053*0Sstevel@tonic-gate 			/* this is the first minor node we're looking at */
1054*0Sstevel@tonic-gate 			minor_head = minor_tail = minor;
1055*0Sstevel@tonic-gate 			continue;
1056*0Sstevel@tonic-gate 		}
1057*0Sstevel@tonic-gate 
1058*0Sstevel@tonic-gate 		/*
1059*0Sstevel@tonic-gate 		 * if the new dev is less than the old dev, update minor_head
1060*0Sstevel@tonic-gate 		 * so it points to the beginning of the list.  ie it points
1061*0Sstevel@tonic-gate 		 * to the node with the lowest dev value
1062*0Sstevel@tonic-gate 		 */
1063*0Sstevel@tonic-gate 		if (dev <= di_minor_devt(minor_head)) {
1064*0Sstevel@tonic-gate 			minor_ptr_set(minor, minor_head);
1065*0Sstevel@tonic-gate 			minor_head = minor;
1066*0Sstevel@tonic-gate 			continue;
1067*0Sstevel@tonic-gate 		}
1068*0Sstevel@tonic-gate 
1069*0Sstevel@tonic-gate 		minor_prev = minor_head;
1070*0Sstevel@tonic-gate 		minor_walk = minor_ptr(minor_head);
1071*0Sstevel@tonic-gate 		while ((minor_walk != DI_MINOR_NIL) &&
1072*0Sstevel@tonic-gate 			(dev > di_minor_devt(minor_walk))) {
1073*0Sstevel@tonic-gate 			minor_prev = minor_walk;
1074*0Sstevel@tonic-gate 			minor_walk = minor_ptr(minor_walk);
1075*0Sstevel@tonic-gate 		}
1076*0Sstevel@tonic-gate 		minor_ptr_set(minor, minor_walk);
1077*0Sstevel@tonic-gate 		minor_ptr_set(minor_prev, minor);
1078*0Sstevel@tonic-gate 		if (minor_walk == NULL)
1079*0Sstevel@tonic-gate 			minor_tail = minor;
1080*0Sstevel@tonic-gate 	}
1081*0Sstevel@tonic-gate 
1082*0Sstevel@tonic-gate 	/* check if there were any non /pseudo/clone@0 nodes.  if not, bail */
1083*0Sstevel@tonic-gate 	if (minor_head == DI_MINOR_NIL)
1084*0Sstevel@tonic-gate 		return;
1085*0Sstevel@tonic-gate 
1086*0Sstevel@tonic-gate 	/*
1087*0Sstevel@tonic-gate 	 * now that we have a list of minor nodes sorted by devt
1088*0Sstevel@tonic-gate 	 * we walk through the list and break apart the entire list
1089*0Sstevel@tonic-gate 	 * to create circular lists of minor nodes with matching devts.
1090*0Sstevel@tonic-gate 	 */
1091*0Sstevel@tonic-gate 	minor_prev = minor_head;
1092*0Sstevel@tonic-gate 	minor_walk = minor_ptr(minor_head);
1093*0Sstevel@tonic-gate 	while (minor_walk != DI_MINOR_NIL) {
1094*0Sstevel@tonic-gate 		if (di_minor_devt(minor_prev) != di_minor_devt(minor_walk)) {
1095*0Sstevel@tonic-gate 			minor_ptr_set(minor_prev, minor_head);
1096*0Sstevel@tonic-gate 			minor_head = minor_walk;
1097*0Sstevel@tonic-gate 		}
1098*0Sstevel@tonic-gate 		minor_prev = minor_walk;
1099*0Sstevel@tonic-gate 		minor_walk = minor_ptr(minor_walk);
1100*0Sstevel@tonic-gate 	}
1101*0Sstevel@tonic-gate 	minor_ptr_set(minor_tail, minor_head);
1102*0Sstevel@tonic-gate }
1103*0Sstevel@tonic-gate 
1104*0Sstevel@tonic-gate static void
1105*0Sstevel@tonic-gate link_lnode_disp(di_link_t link, uint_t endpoint, int ilev,
1106*0Sstevel@tonic-gate     di_devlink_handle_t devlink_hdl)
1107*0Sstevel@tonic-gate {
1108*0Sstevel@tonic-gate 	di_lnode_t	lnode;
1109*0Sstevel@tonic-gate 	char		*name, *path;
1110*0Sstevel@tonic-gate 	int		displayed_path, spec_type;
1111*0Sstevel@tonic-gate 	di_node_t	node = DI_NODE_NIL;
1112*0Sstevel@tonic-gate 	dev_t		devt = DDI_DEV_T_NONE;
1113*0Sstevel@tonic-gate 
1114*0Sstevel@tonic-gate 	lnode = di_link_to_lnode(link, endpoint);
1115*0Sstevel@tonic-gate 
1116*0Sstevel@tonic-gate 	indent_to_level(ilev);
1117*0Sstevel@tonic-gate 	name = di_lnode_name(lnode);
1118*0Sstevel@tonic-gate 	spec_type = di_link_spectype(link);
1119*0Sstevel@tonic-gate 
1120*0Sstevel@tonic-gate 	(void) printf("mod=%s", name);
1121*0Sstevel@tonic-gate 
1122*0Sstevel@tonic-gate 	/*
1123*0Sstevel@tonic-gate 	 * if we're displaying the source of a link, we should display
1124*0Sstevel@tonic-gate 	 * the target access mode.  (either block or char.)
1125*0Sstevel@tonic-gate 	 */
1126*0Sstevel@tonic-gate 	if (endpoint == DI_LINK_SRC)
1127*0Sstevel@tonic-gate 		(void) printf(" accesstype=%s",
1128*0Sstevel@tonic-gate 		    (spec_type == S_IFBLK) ? "blk" : "chr");
1129*0Sstevel@tonic-gate 
1130*0Sstevel@tonic-gate 	/*
1131*0Sstevel@tonic-gate 	 * check if the lnode is bound to a specific device
1132*0Sstevel@tonic-gate 	 * minor node (i.e.  if it's bound to a dev_t) and
1133*0Sstevel@tonic-gate 	 * if so display the dev_t value and any possible
1134*0Sstevel@tonic-gate 	 * minor node pathing information.
1135*0Sstevel@tonic-gate 	 */
1136*0Sstevel@tonic-gate 	displayed_path = 0;
1137*0Sstevel@tonic-gate 	if (di_lnode_devt(lnode, &devt) == 0) {
1138*0Sstevel@tonic-gate 		di_minor_t	minor = DI_MINOR_NIL;
1139*0Sstevel@tonic-gate 
1140*0Sstevel@tonic-gate 		(void) printf(" dev=(%u,%u)\n",
1141*0Sstevel@tonic-gate 		    (uint_t)major(devt), (uint_t)minor(devt));
1142*0Sstevel@tonic-gate 
1143*0Sstevel@tonic-gate 		/* display paths to the src devt minor node */
1144*0Sstevel@tonic-gate 		while (minor = di_minor_next(node, minor)) {
1145*0Sstevel@tonic-gate 			if (devt != di_minor_devt(minor))
1146*0Sstevel@tonic-gate 				continue;
1147*0Sstevel@tonic-gate 
1148*0Sstevel@tonic-gate 			if ((endpoint == DI_LINK_TGT) &&
1149*0Sstevel@tonic-gate 			    (spec_type != di_minor_spectype(minor)))
1150*0Sstevel@tonic-gate 				continue;
1151*0Sstevel@tonic-gate 
1152*0Sstevel@tonic-gate 			dump_minor_data_paths(ilev + 1, minor, devlink_hdl);
1153*0Sstevel@tonic-gate 			displayed_path = 1;
1154*0Sstevel@tonic-gate 		}
1155*0Sstevel@tonic-gate 	} else {
1156*0Sstevel@tonic-gate 		(void) printf("\n");
1157*0Sstevel@tonic-gate 	}
1158*0Sstevel@tonic-gate 
1159*0Sstevel@tonic-gate 	if (displayed_path)
1160*0Sstevel@tonic-gate 		return;
1161*0Sstevel@tonic-gate 
1162*0Sstevel@tonic-gate 	/*
1163*0Sstevel@tonic-gate 	 * This device lnode is not did not have any minor node
1164*0Sstevel@tonic-gate 	 * pathing information so display the path to device node.
1165*0Sstevel@tonic-gate 	 */
1166*0Sstevel@tonic-gate 	node = di_lnode_devinfo(lnode);
1167*0Sstevel@tonic-gate 	if ((path = di_devfs_path(node)) == NULL)
1168*0Sstevel@tonic-gate 		exit(_error("failed to allocate memory"));
1169*0Sstevel@tonic-gate 
1170*0Sstevel@tonic-gate 	indent_to_level(ilev + 1);
1171*0Sstevel@tonic-gate 	(void) printf("dev_path=%s\n", path);
1172*0Sstevel@tonic-gate 	di_devfs_path_free(path);
1173*0Sstevel@tonic-gate }
1174*0Sstevel@tonic-gate 
1175*0Sstevel@tonic-gate static void
1176*0Sstevel@tonic-gate dump_minor_link_data(int ilev, di_node_t node, dev_t devt,
1177*0Sstevel@tonic-gate     di_devlink_handle_t devlink_hdl)
1178*0Sstevel@tonic-gate {
1179*0Sstevel@tonic-gate 	int		first = 1;
1180*0Sstevel@tonic-gate 	di_link_t	link;
1181*0Sstevel@tonic-gate 
1182*0Sstevel@tonic-gate 	link = DI_LINK_NIL;
1183*0Sstevel@tonic-gate 	while (link = di_link_next_by_node(node, link, DI_LINK_TGT)) {
1184*0Sstevel@tonic-gate 		di_lnode_t	tgt_lnode;
1185*0Sstevel@tonic-gate 		dev_t		tgt_devt = DDI_DEV_T_NONE;
1186*0Sstevel@tonic-gate 
1187*0Sstevel@tonic-gate 		tgt_lnode = di_link_to_lnode(link, DI_LINK_TGT);
1188*0Sstevel@tonic-gate 
1189*0Sstevel@tonic-gate 		if (di_lnode_devt(tgt_lnode, &tgt_devt) != 0)
1190*0Sstevel@tonic-gate 			continue;
1191*0Sstevel@tonic-gate 
1192*0Sstevel@tonic-gate 		if (devt != tgt_devt)
1193*0Sstevel@tonic-gate 			continue;
1194*0Sstevel@tonic-gate 
1195*0Sstevel@tonic-gate 		if (first) {
1196*0Sstevel@tonic-gate 			first = 0;
1197*0Sstevel@tonic-gate 			indent_to_level(ilev);
1198*0Sstevel@tonic-gate 			(void) printf("Device Minor Layered Under:\n");
1199*0Sstevel@tonic-gate 		}
1200*0Sstevel@tonic-gate 
1201*0Sstevel@tonic-gate 		/* displayed this lnode */
1202*0Sstevel@tonic-gate 		lnode_displayed_set(tgt_lnode);
1203*0Sstevel@tonic-gate 		link_lnode_disp(link, DI_LINK_SRC, ilev + 1, devlink_hdl);
1204*0Sstevel@tonic-gate 	}
1205*0Sstevel@tonic-gate 
1206*0Sstevel@tonic-gate 	link = DI_LINK_NIL;
1207*0Sstevel@tonic-gate 	while (link = di_link_next_by_node(node, link, DI_LINK_SRC)) {
1208*0Sstevel@tonic-gate 		di_lnode_t	src_lnode;
1209*0Sstevel@tonic-gate 		dev_t		src_devt = DDI_DEV_T_NONE;
1210*0Sstevel@tonic-gate 
1211*0Sstevel@tonic-gate 		src_lnode = di_link_to_lnode(link, DI_LINK_SRC);
1212*0Sstevel@tonic-gate 
1213*0Sstevel@tonic-gate 		if (di_lnode_devt(src_lnode, &src_devt) != 0)
1214*0Sstevel@tonic-gate 			continue;
1215*0Sstevel@tonic-gate 
1216*0Sstevel@tonic-gate 		if (devt != src_devt)
1217*0Sstevel@tonic-gate 			continue;
1218*0Sstevel@tonic-gate 
1219*0Sstevel@tonic-gate 		if (first) {
1220*0Sstevel@tonic-gate 			first = 0;
1221*0Sstevel@tonic-gate 			indent_to_level(ilev);
1222*0Sstevel@tonic-gate 			(void) printf("Device Minor Layered Over:\n");
1223*0Sstevel@tonic-gate 		}
1224*0Sstevel@tonic-gate 
1225*0Sstevel@tonic-gate 		/* displayed this lnode */
1226*0Sstevel@tonic-gate 		lnode_displayed_set(src_lnode);
1227*0Sstevel@tonic-gate 		link_lnode_disp(link, DI_LINK_TGT, ilev + 1, devlink_hdl);
1228*0Sstevel@tonic-gate 	}
1229*0Sstevel@tonic-gate }
1230*0Sstevel@tonic-gate 
1231*0Sstevel@tonic-gate static void
1232*0Sstevel@tonic-gate dump_minor_data(int ilev, di_node_t node, di_devlink_handle_t devlink_hdl)
1233*0Sstevel@tonic-gate {
1234*0Sstevel@tonic-gate 	di_minor_t	minor, minor_next;
1235*0Sstevel@tonic-gate 	di_lnode_t	lnode;
1236*0Sstevel@tonic-gate 	di_link_t	link;
1237*0Sstevel@tonic-gate 	int		major, firstminor = 1;
1238*0Sstevel@tonic-gate 
1239*0Sstevel@tonic-gate 	/*
1240*0Sstevel@tonic-gate 	 * first go through and mark all lnodes and minor nodes for this
1241*0Sstevel@tonic-gate 	 * node as undisplayed
1242*0Sstevel@tonic-gate 	 */
1243*0Sstevel@tonic-gate 	lnode = DI_LNODE_NIL;
1244*0Sstevel@tonic-gate 	while (lnode = di_lnode_next(node, lnode))
1245*0Sstevel@tonic-gate 		lnode_displayed_clear(lnode);
1246*0Sstevel@tonic-gate 	minor = DI_MINOR_NIL;
1247*0Sstevel@tonic-gate 	while (minor = di_minor_next(node, minor)) {
1248*0Sstevel@tonic-gate 		minor_displayed_clear(minor);
1249*0Sstevel@tonic-gate 	}
1250*0Sstevel@tonic-gate 
1251*0Sstevel@tonic-gate 	/*
1252*0Sstevel@tonic-gate 	 * when we display the minor nodes we want to coalesce nodes
1253*0Sstevel@tonic-gate 	 * that have the same dev_t.  we do this by creating circular
1254*0Sstevel@tonic-gate 	 * lists of minor nodes with the same devt.
1255*0Sstevel@tonic-gate 	 */
1256*0Sstevel@tonic-gate 	create_minor_list(node);
1257*0Sstevel@tonic-gate 
1258*0Sstevel@tonic-gate 	/* now we display the driver defined minor nodes */
1259*0Sstevel@tonic-gate 	major = di_driver_major(node);
1260*0Sstevel@tonic-gate 	minor = DI_MINOR_NIL;
1261*0Sstevel@tonic-gate 	while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
1262*0Sstevel@tonic-gate 		dev_t	devt;
1263*0Sstevel@tonic-gate 
1264*0Sstevel@tonic-gate 		/*
1265*0Sstevel@tonic-gate 		 * skip /pseudo/clone@0 minor nodes.
1266*0Sstevel@tonic-gate 		 * these are only created for DLPIv2 network devices.
1267*0Sstevel@tonic-gate 		 * since these minor nodes are associated with a driver
1268*0Sstevel@tonic-gate 		 * and are only bound to a device instance after they
1269*0Sstevel@tonic-gate 		 * are opened and attached we don't print them out
1270*0Sstevel@tonic-gate 		 * here.
1271*0Sstevel@tonic-gate 		 */
1272*0Sstevel@tonic-gate 		devt = di_minor_devt(minor);
1273*0Sstevel@tonic-gate 		if (major != major(devt))
1274*0Sstevel@tonic-gate 			continue;
1275*0Sstevel@tonic-gate 
1276*0Sstevel@tonic-gate 		/* skip nodes that may have already been displayed */
1277*0Sstevel@tonic-gate 		if (minor_displayed(minor))
1278*0Sstevel@tonic-gate 			continue;
1279*0Sstevel@tonic-gate 
1280*0Sstevel@tonic-gate 		if (firstminor) {
1281*0Sstevel@tonic-gate 			firstminor = 0;
1282*0Sstevel@tonic-gate 			indent_to_level(ilev++);
1283*0Sstevel@tonic-gate 			(void) printf("Device Minor Nodes:\n");
1284*0Sstevel@tonic-gate 		}
1285*0Sstevel@tonic-gate 
1286*0Sstevel@tonic-gate 		/* display the device minor node information */
1287*0Sstevel@tonic-gate 		indent_to_level(ilev);
1288*0Sstevel@tonic-gate 		(void) printf("dev=(%u,%u)\n",
1289*0Sstevel@tonic-gate 				(uint_t)major(devt), (uint_t)minor(devt));
1290*0Sstevel@tonic-gate 
1291*0Sstevel@tonic-gate 		minor_next = minor;
1292*0Sstevel@tonic-gate 		do {
1293*0Sstevel@tonic-gate 			/* display device minor node path info */
1294*0Sstevel@tonic-gate 			minor_displayed_set(minor_next);
1295*0Sstevel@tonic-gate 			dump_minor_data_paths(ilev + 1, minor_next,
1296*0Sstevel@tonic-gate 						devlink_hdl);
1297*0Sstevel@tonic-gate 
1298*0Sstevel@tonic-gate 			/* get a pointer to the next node */
1299*0Sstevel@tonic-gate 			minor_next = minor_ptr(minor_next);
1300*0Sstevel@tonic-gate 		} while (minor_next != minor);
1301*0Sstevel@tonic-gate 
1302*0Sstevel@tonic-gate 		/* display who has this device minor node open */
1303*0Sstevel@tonic-gate 		dump_minor_link_data(ilev + 1, node, devt, devlink_hdl);
1304*0Sstevel@tonic-gate 	}
1305*0Sstevel@tonic-gate 
1306*0Sstevel@tonic-gate 	/*
1307*0Sstevel@tonic-gate 	 * now go through all the target lnodes for this node and
1308*0Sstevel@tonic-gate 	 * if they haven't yet been displayed, display them now.
1309*0Sstevel@tonic-gate 	 *
1310*0Sstevel@tonic-gate 	 * this happens in the case of clone opens when an "official"
1311*0Sstevel@tonic-gate 	 * minor node does not exist for the opened devt
1312*0Sstevel@tonic-gate 	 */
1313*0Sstevel@tonic-gate 	link = DI_LINK_NIL;
1314*0Sstevel@tonic-gate 	while (link = di_link_next_by_node(node, link, DI_LINK_TGT)) {
1315*0Sstevel@tonic-gate 		dev_t		devt;
1316*0Sstevel@tonic-gate 
1317*0Sstevel@tonic-gate 		lnode = di_link_to_lnode(link, DI_LINK_TGT);
1318*0Sstevel@tonic-gate 
1319*0Sstevel@tonic-gate 		/* if we've already displayed this target lnode, skip it */
1320*0Sstevel@tonic-gate 		if (lnode_displayed(lnode))
1321*0Sstevel@tonic-gate 			continue;
1322*0Sstevel@tonic-gate 
1323*0Sstevel@tonic-gate 		if (firstminor) {
1324*0Sstevel@tonic-gate 			firstminor = 0;
1325*0Sstevel@tonic-gate 			indent_to_level(ilev++);
1326*0Sstevel@tonic-gate 			(void) printf("Device Minor Nodes:\n");
1327*0Sstevel@tonic-gate 		}
1328*0Sstevel@tonic-gate 
1329*0Sstevel@tonic-gate 		/* display the device minor node information */
1330*0Sstevel@tonic-gate 		indent_to_level(ilev);
1331*0Sstevel@tonic-gate 		(void) di_lnode_devt(lnode, &devt);
1332*0Sstevel@tonic-gate 		(void) printf("dev=(%u,%u)\n",
1333*0Sstevel@tonic-gate 				(uint_t)major(devt), (uint_t)minor(devt));
1334*0Sstevel@tonic-gate 
1335*0Sstevel@tonic-gate 		indent_to_level(ilev + 1);
1336*0Sstevel@tonic-gate 		(void) printf("dev_path=<clone>\n");
1337*0Sstevel@tonic-gate 
1338*0Sstevel@tonic-gate 		/* display who has this cloned device minor node open */
1339*0Sstevel@tonic-gate 		dump_minor_link_data(ilev + 1, node, devt, devlink_hdl);
1340*0Sstevel@tonic-gate 
1341*0Sstevel@tonic-gate 		/* mark node as displayed */
1342*0Sstevel@tonic-gate 		lnode_displayed_set(lnode);
1343*0Sstevel@tonic-gate 	}
1344*0Sstevel@tonic-gate }
1345*0Sstevel@tonic-gate 
1346*0Sstevel@tonic-gate static void
1347*0Sstevel@tonic-gate dump_link_data(int ilev, di_node_t node, di_devlink_handle_t devlink_hdl)
1348*0Sstevel@tonic-gate {
1349*0Sstevel@tonic-gate 	int		first = 1;
1350*0Sstevel@tonic-gate 	di_link_t	link;
1351*0Sstevel@tonic-gate 
1352*0Sstevel@tonic-gate 	link = DI_LINK_NIL;
1353*0Sstevel@tonic-gate 	while (link = di_link_next_by_node(node, link, DI_LINK_SRC)) {
1354*0Sstevel@tonic-gate 		di_lnode_t	src_lnode;
1355*0Sstevel@tonic-gate 		dev_t		src_devt = DDI_DEV_T_NONE;
1356*0Sstevel@tonic-gate 
1357*0Sstevel@tonic-gate 		src_lnode = di_link_to_lnode(link, DI_LINK_SRC);
1358*0Sstevel@tonic-gate 
1359*0Sstevel@tonic-gate 		/*
1360*0Sstevel@tonic-gate 		 * here we only want to print out layering information
1361*0Sstevel@tonic-gate 		 * if we are the source and our source lnode is not
1362*0Sstevel@tonic-gate 		 * associated with any particular dev_t.  (which means
1363*0Sstevel@tonic-gate 		 * we won't display this link while dumping minor node
1364*0Sstevel@tonic-gate 		 * info.)
1365*0Sstevel@tonic-gate 		 */
1366*0Sstevel@tonic-gate 		if (di_lnode_devt(src_lnode, &src_devt) != -1)
1367*0Sstevel@tonic-gate 			continue;
1368*0Sstevel@tonic-gate 
1369*0Sstevel@tonic-gate 		if (first) {
1370*0Sstevel@tonic-gate 			first = 0;
1371*0Sstevel@tonic-gate 			indent_to_level(ilev);
1372*0Sstevel@tonic-gate 			(void) printf("Device Layered Over:\n");
1373*0Sstevel@tonic-gate 		}
1374*0Sstevel@tonic-gate 
1375*0Sstevel@tonic-gate 		/* displayed this lnode */
1376*0Sstevel@tonic-gate 		link_lnode_disp(link, DI_LINK_TGT, ilev + 1, devlink_hdl);
1377*0Sstevel@tonic-gate 	}
1378*0Sstevel@tonic-gate }
1379*0Sstevel@tonic-gate 
1380*0Sstevel@tonic-gate /*
1381*0Sstevel@tonic-gate  * certain 'known' property names may contain 'composite' strings.
1382*0Sstevel@tonic-gate  * Handle them here, and print them as 'string1' + 'string2' ...
1383*0Sstevel@tonic-gate  */
1384*0Sstevel@tonic-gate static int
1385*0Sstevel@tonic-gate print_composite_string(const char *var, char *value, int size)
1386*0Sstevel@tonic-gate {
1387*0Sstevel@tonic-gate 	char *p, *q;
1388*0Sstevel@tonic-gate 	char *firstp;
1389*0Sstevel@tonic-gate 
1390*0Sstevel@tonic-gate 	if ((strcmp(var, "version") != 0) &&
1391*0Sstevel@tonic-gate 	    (strcmp(var, "compatible") != 0))
1392*0Sstevel@tonic-gate 		return (0);	/* Not a known composite string */
1393*0Sstevel@tonic-gate 
1394*0Sstevel@tonic-gate 	/*
1395*0Sstevel@tonic-gate 	 * Verify that each string in the composite string is non-NULL,
1396*0Sstevel@tonic-gate 	 * is within the bounds of the property length, and contains
1397*0Sstevel@tonic-gate 	 * printable characters or white space. Otherwise let the
1398*0Sstevel@tonic-gate 	 * caller deal with it.
1399*0Sstevel@tonic-gate 	 */
1400*0Sstevel@tonic-gate 	for (firstp = p = value; p < (value + size); p += strlen(p) + 1) {
1401*0Sstevel@tonic-gate 		if (strlen(p) == 0)
1402*0Sstevel@tonic-gate 			return (0);		/* NULL string */
1403*0Sstevel@tonic-gate 		for (q = p; *q; q++) {
1404*0Sstevel@tonic-gate 			if (!(isascii(*q) && (isprint(*q) || isspace(*q))))
1405*0Sstevel@tonic-gate 				return (0);	/* Not printable or space */
1406*0Sstevel@tonic-gate 		}
1407*0Sstevel@tonic-gate 		if (q > (firstp + size))
1408*0Sstevel@tonic-gate 			return (0);		/* Out of bounds */
1409*0Sstevel@tonic-gate 	}
1410*0Sstevel@tonic-gate 
1411*0Sstevel@tonic-gate 	for (firstp = p = value; p < (value + size); p += strlen(p) + 1) {
1412*0Sstevel@tonic-gate 		if (p == firstp)
1413*0Sstevel@tonic-gate 			(void) printf("'%s'", p);
1414*0Sstevel@tonic-gate 		else
1415*0Sstevel@tonic-gate 			(void) printf(" + '%s'", p);
1416*0Sstevel@tonic-gate 	}
1417*0Sstevel@tonic-gate 	(void) putchar('\n');
1418*0Sstevel@tonic-gate 	return (1);
1419*0Sstevel@tonic-gate }
1420*0Sstevel@tonic-gate 
1421*0Sstevel@tonic-gate /*
1422*0Sstevel@tonic-gate  * Print one property and its value. Handle the verbose case.
1423*0Sstevel@tonic-gate  */
1424*0Sstevel@tonic-gate static void
1425*0Sstevel@tonic-gate print_one(nvpair_t *nvp, int level)
1426*0Sstevel@tonic-gate {
1427*0Sstevel@tonic-gate 	int i;
1428*0Sstevel@tonic-gate 	int endswap = 0;
1429*0Sstevel@tonic-gate 	uint_t valsize;
1430*0Sstevel@tonic-gate 	char *value;
1431*0Sstevel@tonic-gate 	char *var = nvpair_name(nvp);
1432*0Sstevel@tonic-gate 
1433*0Sstevel@tonic-gate 	indent_to_level(level);
1434*0Sstevel@tonic-gate 	(void) printf("%s: ", var);
1435*0Sstevel@tonic-gate 
1436*0Sstevel@tonic-gate 	switch (nvpair_type(nvp)) {
1437*0Sstevel@tonic-gate 	case DATA_TYPE_BOOLEAN:
1438*0Sstevel@tonic-gate 		(void) printf(" \n");
1439*0Sstevel@tonic-gate 		return;
1440*0Sstevel@tonic-gate 	case DATA_TYPE_BYTE_ARRAY:
1441*0Sstevel@tonic-gate 		if (nvpair_value_byte_array(nvp, (uchar_t **)&value,
1442*0Sstevel@tonic-gate 		    &valsize)) {
1443*0Sstevel@tonic-gate 			(void) printf("data not available.\n");
1444*0Sstevel@tonic-gate 			return;
1445*0Sstevel@tonic-gate 		}
1446*0Sstevel@tonic-gate 		valsize--;	/* take out null added by driver */
1447*0Sstevel@tonic-gate 
1448*0Sstevel@tonic-gate 		/*
1449*0Sstevel@tonic-gate 		 * Do not print valsize > MAXVALSIZE, to be compatible
1450*0Sstevel@tonic-gate 		 * with old behavior. E.g. intel's eisa-nvram property
1451*0Sstevel@tonic-gate 		 * has a size of 65 K.
1452*0Sstevel@tonic-gate 		 */
1453*0Sstevel@tonic-gate 		if (valsize > MAXVALSIZE) {
1454*0Sstevel@tonic-gate 			(void) printf(" \n");
1455*0Sstevel@tonic-gate 			return;
1456*0Sstevel@tonic-gate 		}
1457*0Sstevel@tonic-gate 		break;
1458*0Sstevel@tonic-gate 	default:
1459*0Sstevel@tonic-gate 		(void) printf("data type unexpected.\n");
1460*0Sstevel@tonic-gate 		return;
1461*0Sstevel@tonic-gate 	}
1462*0Sstevel@tonic-gate 
1463*0Sstevel@tonic-gate 	/*
1464*0Sstevel@tonic-gate 	 * Handle printing verbosely
1465*0Sstevel@tonic-gate 	 */
1466*0Sstevel@tonic-gate 	if (print_composite_string(var, value, valsize)) {
1467*0Sstevel@tonic-gate 		return;
1468*0Sstevel@tonic-gate 	}
1469*0Sstevel@tonic-gate 
1470*0Sstevel@tonic-gate 	if (!unprintable(value, valsize)) {
1471*0Sstevel@tonic-gate 		(void) printf(" '%s'\n", value);
1472*0Sstevel@tonic-gate 		return;
1473*0Sstevel@tonic-gate 	}
1474*0Sstevel@tonic-gate 
1475*0Sstevel@tonic-gate 	(void) printf(" ");
1476*0Sstevel@tonic-gate #ifdef	__x86
1477*0Sstevel@tonic-gate 	/*
1478*0Sstevel@tonic-gate 	 * Due to backwards compatibility constraints x86 int
1479*0Sstevel@tonic-gate 	 * properties are not in big-endian (ieee 1275) byte order.
1480*0Sstevel@tonic-gate 	 * If we have a property that is a multiple of 4 bytes,
1481*0Sstevel@tonic-gate 	 * let's assume it is an array of ints and print the bytes
1482*0Sstevel@tonic-gate 	 * in little endian order to make things look nicer for
1483*0Sstevel@tonic-gate 	 * the user.
1484*0Sstevel@tonic-gate 	 */
1485*0Sstevel@tonic-gate 	endswap = (valsize % 4) == 0;
1486*0Sstevel@tonic-gate #endif	/* __x86 */
1487*0Sstevel@tonic-gate 	for (i = 0; i < valsize; i++) {
1488*0Sstevel@tonic-gate 		int out;
1489*0Sstevel@tonic-gate 		if (i && (i % 4 == 0))
1490*0Sstevel@tonic-gate 			(void) putchar('.');
1491*0Sstevel@tonic-gate 		if (endswap)
1492*0Sstevel@tonic-gate 			out = value[i + (3 - 2 * (i % 4))] & 0xff;
1493*0Sstevel@tonic-gate 		else
1494*0Sstevel@tonic-gate 			out = value[i] & 0xff;
1495*0Sstevel@tonic-gate 
1496*0Sstevel@tonic-gate 		(void) printf("%02x", out);
1497*0Sstevel@tonic-gate 	}
1498*0Sstevel@tonic-gate 	(void) putchar('\n');
1499*0Sstevel@tonic-gate }
1500*0Sstevel@tonic-gate 
1501*0Sstevel@tonic-gate static int
1502*0Sstevel@tonic-gate unprintable(char *value, int size)
1503*0Sstevel@tonic-gate {
1504*0Sstevel@tonic-gate 	int i;
1505*0Sstevel@tonic-gate 
1506*0Sstevel@tonic-gate 	/*
1507*0Sstevel@tonic-gate 	 * Is this just a zero?
1508*0Sstevel@tonic-gate 	 */
1509*0Sstevel@tonic-gate 	if (size == 0 || value[0] == '\0')
1510*0Sstevel@tonic-gate 		return (1);
1511*0Sstevel@tonic-gate 	/*
1512*0Sstevel@tonic-gate 	 * If any character is unprintable, or if a null appears
1513*0Sstevel@tonic-gate 	 * anywhere except at the end of a string, the whole
1514*0Sstevel@tonic-gate 	 * property is "unprintable".
1515*0Sstevel@tonic-gate 	 */
1516*0Sstevel@tonic-gate 	for (i = 0; i < size; ++i) {
1517*0Sstevel@tonic-gate 		if (value[i] == '\0')
1518*0Sstevel@tonic-gate 			return (i != (size - 1));
1519*0Sstevel@tonic-gate 		if (!isascii(value[i]) || iscntrl(value[i]))
1520*0Sstevel@tonic-gate 			return (1);
1521*0Sstevel@tonic-gate 	}
1522*0Sstevel@tonic-gate 	return (0);
1523*0Sstevel@tonic-gate }
1524*0Sstevel@tonic-gate 
1525*0Sstevel@tonic-gate static int
1526*0Sstevel@tonic-gate promopen(int oflag)
1527*0Sstevel@tonic-gate {
1528*0Sstevel@tonic-gate 	for (;;)  {
1529*0Sstevel@tonic-gate 		if ((prom_fd = open(opts.o_promdev, oflag)) < 0)  {
1530*0Sstevel@tonic-gate 			if (errno == EAGAIN)   {
1531*0Sstevel@tonic-gate 				(void) sleep(5);
1532*0Sstevel@tonic-gate 				continue;
1533*0Sstevel@tonic-gate 			}
1534*0Sstevel@tonic-gate 			if (errno == ENXIO)
1535*0Sstevel@tonic-gate 				return (-1);
1536*0Sstevel@tonic-gate 			if (getzoneid() == GLOBAL_ZONEID) {
1537*0Sstevel@tonic-gate 				_exit(_error("cannot open %s",
1538*0Sstevel@tonic-gate 				    opts.o_promdev));
1539*0Sstevel@tonic-gate 			}
1540*0Sstevel@tonic-gate 			/* not an error if this isn't the global zone */
1541*0Sstevel@tonic-gate 			(void) _error(NULL, "openprom facility not available");
1542*0Sstevel@tonic-gate 			exit(0);
1543*0Sstevel@tonic-gate 		} else
1544*0Sstevel@tonic-gate 			return (0);
1545*0Sstevel@tonic-gate 	}
1546*0Sstevel@tonic-gate }
1547*0Sstevel@tonic-gate 
1548*0Sstevel@tonic-gate static void
1549*0Sstevel@tonic-gate promclose(void)
1550*0Sstevel@tonic-gate {
1551*0Sstevel@tonic-gate 	if (close(prom_fd) < 0)
1552*0Sstevel@tonic-gate 		exit(_error("close error on %s", opts.o_promdev));
1553*0Sstevel@tonic-gate }
1554*0Sstevel@tonic-gate 
1555*0Sstevel@tonic-gate /*
1556*0Sstevel@tonic-gate  * Get and print the name of the frame buffer device.
1557*0Sstevel@tonic-gate  */
1558*0Sstevel@tonic-gate int
1559*0Sstevel@tonic-gate do_fbname(void)
1560*0Sstevel@tonic-gate {
1561*0Sstevel@tonic-gate 	int	retval;
1562*0Sstevel@tonic-gate 	char fbuf_path[MAXPATHLEN];
1563*0Sstevel@tonic-gate 
1564*0Sstevel@tonic-gate 	retval =  modctl(MODGETFBNAME, (caddr_t)fbuf_path);
1565*0Sstevel@tonic-gate 
1566*0Sstevel@tonic-gate 	if (retval == 0) {
1567*0Sstevel@tonic-gate 		(void) printf("%s\n", fbuf_path);
1568*0Sstevel@tonic-gate 	} else {
1569*0Sstevel@tonic-gate 		if (retval == EFAULT) {
1570*0Sstevel@tonic-gate 			(void) fprintf(stderr,
1571*0Sstevel@tonic-gate 			"Error copying fb path to userland\n");
1572*0Sstevel@tonic-gate 		} else {
1573*0Sstevel@tonic-gate 			(void) fprintf(stderr,
1574*0Sstevel@tonic-gate 			"Console output device is not a frame buffer\n");
1575*0Sstevel@tonic-gate 		}
1576*0Sstevel@tonic-gate 		return (1);
1577*0Sstevel@tonic-gate 	}
1578*0Sstevel@tonic-gate 	return (0);
1579*0Sstevel@tonic-gate }
1580*0Sstevel@tonic-gate 
1581*0Sstevel@tonic-gate /*
1582*0Sstevel@tonic-gate  * Get and print the PROM version.
1583*0Sstevel@tonic-gate  */
1584*0Sstevel@tonic-gate int
1585*0Sstevel@tonic-gate do_promversion(void)
1586*0Sstevel@tonic-gate {
1587*0Sstevel@tonic-gate 	Oppbuf	oppbuf;
1588*0Sstevel@tonic-gate 	struct openpromio *opp = &(oppbuf.opp);
1589*0Sstevel@tonic-gate 
1590*0Sstevel@tonic-gate 	if (promopen(O_RDONLY))  {
1591*0Sstevel@tonic-gate 		(void) fprintf(stderr, "Cannot open openprom device\n");
1592*0Sstevel@tonic-gate 		return (1);
1593*0Sstevel@tonic-gate 	}
1594*0Sstevel@tonic-gate 
1595*0Sstevel@tonic-gate 	opp->oprom_size = MAXVALSIZE;
1596*0Sstevel@tonic-gate 	if (ioctl(prom_fd, OPROMGETVERSION, opp) < 0)
1597*0Sstevel@tonic-gate 		exit(_error("OPROMGETVERSION"));
1598*0Sstevel@tonic-gate 
1599*0Sstevel@tonic-gate 	(void) printf("%s\n", opp->oprom_array);
1600*0Sstevel@tonic-gate 	promclose();
1601*0Sstevel@tonic-gate 	return (0);
1602*0Sstevel@tonic-gate }
1603*0Sstevel@tonic-gate 
1604*0Sstevel@tonic-gate int
1605*0Sstevel@tonic-gate do_prom_version64(void)
1606*0Sstevel@tonic-gate {
1607*0Sstevel@tonic-gate #ifdef	sparc
1608*0Sstevel@tonic-gate 	Oppbuf	oppbuf;
1609*0Sstevel@tonic-gate 	struct openpromio *opp = &(oppbuf.opp);
1610*0Sstevel@tonic-gate 	/*LINTED*/
1611*0Sstevel@tonic-gate 	struct openprom_opr64 *opr = (struct openprom_opr64 *)opp->oprom_array;
1612*0Sstevel@tonic-gate 
1613*0Sstevel@tonic-gate 	static const char msg[] =
1614*0Sstevel@tonic-gate 		"NOTICE: The firmware on this system does not support the "
1615*0Sstevel@tonic-gate 		"64-bit OS.\n"
1616*0Sstevel@tonic-gate 		"\tPlease upgrade to at least the following version:\n"
1617*0Sstevel@tonic-gate 		"\t\t%s\n\n";
1618*0Sstevel@tonic-gate 
1619*0Sstevel@tonic-gate 	if (promopen(O_RDONLY))  {
1620*0Sstevel@tonic-gate 		(void) fprintf(stderr, "Cannot open openprom device\n");
1621*0Sstevel@tonic-gate 		return (-1);
1622*0Sstevel@tonic-gate 	}
1623*0Sstevel@tonic-gate 
1624*0Sstevel@tonic-gate 	opp->oprom_size = MAXVALSIZE;
1625*0Sstevel@tonic-gate 	if (ioctl(prom_fd, OPROMREADY64, opp) < 0)
1626*0Sstevel@tonic-gate 		exit(_error("OPROMREADY64"));
1627*0Sstevel@tonic-gate 
1628*0Sstevel@tonic-gate 	if (opr->return_code == 0)
1629*0Sstevel@tonic-gate 		return (0);
1630*0Sstevel@tonic-gate 
1631*0Sstevel@tonic-gate 	(void) printf(msg, opr->message);
1632*0Sstevel@tonic-gate 
1633*0Sstevel@tonic-gate 	promclose();
1634*0Sstevel@tonic-gate 	return (opr->return_code);
1635*0Sstevel@tonic-gate #else
1636*0Sstevel@tonic-gate 	return (0);
1637*0Sstevel@tonic-gate #endif
1638*0Sstevel@tonic-gate }
1639*0Sstevel@tonic-gate 
1640*0Sstevel@tonic-gate int
1641*0Sstevel@tonic-gate do_productinfo(void)
1642*0Sstevel@tonic-gate {
1643*0Sstevel@tonic-gate 	di_node_t root, next_node;
1644*0Sstevel@tonic-gate 	di_prom_handle_t promh;
1645*0Sstevel@tonic-gate 	static const char *root_prop[] = { "name", "model", "banner-name",
1646*0Sstevel@tonic-gate 					"compatible" };
1647*0Sstevel@tonic-gate 	static const char *root_propv[] = { "name", "model", "banner-name",
1648*0Sstevel@tonic-gate 					"compatible", "idprom" };
1649*0Sstevel@tonic-gate 	static const char *oprom_prop[] = { "model", "version" };
1650*0Sstevel@tonic-gate 
1651*0Sstevel@tonic-gate 
1652*0Sstevel@tonic-gate 	root = di_init("/", DINFOCPYALL);
1653*0Sstevel@tonic-gate 
1654*0Sstevel@tonic-gate 	if (root == DI_NODE_NIL) {
1655*0Sstevel@tonic-gate 		(void) fprintf(stderr, "di_init() failed\n");
1656*0Sstevel@tonic-gate 		return (1);
1657*0Sstevel@tonic-gate 	}
1658*0Sstevel@tonic-gate 
1659*0Sstevel@tonic-gate 	promh = di_prom_init();
1660*0Sstevel@tonic-gate 
1661*0Sstevel@tonic-gate 	if (promh == DI_PROM_HANDLE_NIL) {
1662*0Sstevel@tonic-gate 		(void) fprintf(stderr, "di_prom_init() failed\n");
1663*0Sstevel@tonic-gate 		return (1);
1664*0Sstevel@tonic-gate 	}
1665*0Sstevel@tonic-gate 
1666*0Sstevel@tonic-gate 	if (opts.o_verbose) {
1667*0Sstevel@tonic-gate 		dump_prodinfo(promh, root, root_propv, "root",
1668*0Sstevel@tonic-gate 				NUM_ELEMENTS(root_propv));
1669*0Sstevel@tonic-gate 
1670*0Sstevel@tonic-gate 		/* Get model and version properties under node "openprom" */
1671*0Sstevel@tonic-gate 		next_node = find_node_by_name(promh, root, "openprom");
1672*0Sstevel@tonic-gate 		if (next_node != DI_NODE_NIL)
1673*0Sstevel@tonic-gate 			dump_prodinfo(promh, next_node, oprom_prop,
1674*0Sstevel@tonic-gate 				"openprom", NUM_ELEMENTS(oprom_prop));
1675*0Sstevel@tonic-gate 
1676*0Sstevel@tonic-gate 	} else
1677*0Sstevel@tonic-gate 		dump_prodinfo(promh, root, root_prop, "root",
1678*0Sstevel@tonic-gate 				NUM_ELEMENTS(root_prop));
1679*0Sstevel@tonic-gate 	di_prom_fini(promh);
1680*0Sstevel@tonic-gate 	di_fini(root);
1681*0Sstevel@tonic-gate 	return (0);
1682*0Sstevel@tonic-gate }
1683*0Sstevel@tonic-gate 
1684*0Sstevel@tonic-gate di_node_t
1685*0Sstevel@tonic-gate find_node_by_name(di_prom_handle_t promh, di_node_t parent,
1686*0Sstevel@tonic-gate 		char *node_name)
1687*0Sstevel@tonic-gate {
1688*0Sstevel@tonic-gate 	di_node_t next_node;
1689*0Sstevel@tonic-gate 	uchar_t *prop_valp;
1690*0Sstevel@tonic-gate 
1691*0Sstevel@tonic-gate 	next_node = di_child_node(parent);
1692*0Sstevel@tonic-gate 	while (next_node != DI_NODE_NIL) {
1693*0Sstevel@tonic-gate 		next_node = di_sibling_node(next_node);
1694*0Sstevel@tonic-gate 		(void) get_propval_by_name(promh, next_node, "name",
1695*0Sstevel@tonic-gate 						&prop_valp);
1696*0Sstevel@tonic-gate 		if (strcmp((char *)prop_valp, node_name) == 0)
1697*0Sstevel@tonic-gate 			return (next_node);
1698*0Sstevel@tonic-gate 	}
1699*0Sstevel@tonic-gate 	return (DI_NODE_NIL);
1700*0Sstevel@tonic-gate }
1701*0Sstevel@tonic-gate 
1702*0Sstevel@tonic-gate 
1703*0Sstevel@tonic-gate int
1704*0Sstevel@tonic-gate get_propval_by_name(di_prom_handle_t promh, di_node_t node, const char *name,
1705*0Sstevel@tonic-gate 			uchar_t **valp)
1706*0Sstevel@tonic-gate {
1707*0Sstevel@tonic-gate 	int len;
1708*0Sstevel@tonic-gate 	uchar_t *bufp;
1709*0Sstevel@tonic-gate 
1710*0Sstevel@tonic-gate 	len = di_prom_prop_lookup_bytes(promh, node, name,
1711*0Sstevel@tonic-gate 		(uchar_t **)&bufp);
1712*0Sstevel@tonic-gate 	if (len != -1) {
1713*0Sstevel@tonic-gate 		*valp = (uchar_t *)malloc(len);
1714*0Sstevel@tonic-gate 		(void) memcpy(*valp, bufp, len);
1715*0Sstevel@tonic-gate 	}
1716*0Sstevel@tonic-gate 	return (len);
1717*0Sstevel@tonic-gate }
1718*0Sstevel@tonic-gate 
1719*0Sstevel@tonic-gate 
1720*0Sstevel@tonic-gate static void
1721*0Sstevel@tonic-gate dump_prodinfo(di_prom_handle_t promh, di_node_t node, const char **propstr,
1722*0Sstevel@tonic-gate 		char *node_name, int num)
1723*0Sstevel@tonic-gate {
1724*0Sstevel@tonic-gate 	int out, len, index1, index, endswap = 0;
1725*0Sstevel@tonic-gate 	uchar_t *prop_valp;
1726*0Sstevel@tonic-gate 
1727*0Sstevel@tonic-gate 	for (index1 = 0; index1 < num; index1++) {
1728*0Sstevel@tonic-gate 		len = get_propval_by_name(promh, node, propstr[index1],
1729*0Sstevel@tonic-gate 					&prop_valp);
1730*0Sstevel@tonic-gate 		if (len != -1) {
1731*0Sstevel@tonic-gate 			if (strcmp(node_name, "root"))
1732*0Sstevel@tonic-gate 				(void) printf("%s ", node_name);
1733*0Sstevel@tonic-gate 
1734*0Sstevel@tonic-gate 			(void) printf("%s: ", propstr[index1]);
1735*0Sstevel@tonic-gate 
1736*0Sstevel@tonic-gate 			if (print_composite_string((const char *)
1737*0Sstevel@tonic-gate 				propstr[index1], (char *)prop_valp, len)) {
1738*0Sstevel@tonic-gate 				free(prop_valp);
1739*0Sstevel@tonic-gate 				continue;
1740*0Sstevel@tonic-gate 			}
1741*0Sstevel@tonic-gate 
1742*0Sstevel@tonic-gate 			if (!unprintable((char *)prop_valp, len)) {
1743*0Sstevel@tonic-gate 				(void) printf(" %s\n", (char *)prop_valp);
1744*0Sstevel@tonic-gate 				free(prop_valp);
1745*0Sstevel@tonic-gate 				continue;
1746*0Sstevel@tonic-gate 			}
1747*0Sstevel@tonic-gate 
1748*0Sstevel@tonic-gate 			(void) printf(" ");
1749*0Sstevel@tonic-gate #ifdef  __x86
1750*0Sstevel@tonic-gate 			endswap = (len % 4) == 0;
1751*0Sstevel@tonic-gate #endif  /* __x86 */
1752*0Sstevel@tonic-gate 			for (index = 0; index < len; index++) {
1753*0Sstevel@tonic-gate 				if (index && (index % 4 == 0))
1754*0Sstevel@tonic-gate 					(void) putchar('.');
1755*0Sstevel@tonic-gate 				if (endswap)
1756*0Sstevel@tonic-gate 					out = prop_valp[index +
1757*0Sstevel@tonic-gate 					(3 - 2 * (index % 4))] & 0xff;
1758*0Sstevel@tonic-gate 				else
1759*0Sstevel@tonic-gate 					out = prop_valp[index] & 0xff;
1760*0Sstevel@tonic-gate 				(void) printf("%02x", out);
1761*0Sstevel@tonic-gate 			}
1762*0Sstevel@tonic-gate 			(void) putchar('\n');
1763*0Sstevel@tonic-gate 			free(prop_valp);
1764*0Sstevel@tonic-gate 		}
1765*0Sstevel@tonic-gate 	}
1766*0Sstevel@tonic-gate }
1767