xref: /onnv-gate/usr/src/uts/common/io/devinfo.c (revision 643:3db9125ff8d6)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*643Scth  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * driver for accessing kernel devinfo tree.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/pathname.h>
340Sstevel@tonic-gate #include <sys/debug.h>
350Sstevel@tonic-gate #include <sys/autoconf.h>
360Sstevel@tonic-gate #include <sys/conf.h>
370Sstevel@tonic-gate #include <sys/file.h>
380Sstevel@tonic-gate #include <sys/kmem.h>
390Sstevel@tonic-gate #include <sys/modctl.h>
400Sstevel@tonic-gate #include <sys/stat.h>
410Sstevel@tonic-gate #include <sys/ddi.h>
420Sstevel@tonic-gate #include <sys/sunddi.h>
430Sstevel@tonic-gate #include <sys/sunldi_impl.h>
440Sstevel@tonic-gate #include <sys/sunndi.h>
450Sstevel@tonic-gate #include <sys/esunddi.h>
460Sstevel@tonic-gate #include <sys/sunmdi.h>
470Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
480Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
490Sstevel@tonic-gate #include <sys/mdi_impldefs.h>
500Sstevel@tonic-gate #include <sys/devinfo_impl.h>
510Sstevel@tonic-gate #include <sys/thread.h>
520Sstevel@tonic-gate #include <sys/modhash.h>
530Sstevel@tonic-gate #include <sys/bitmap.h>
540Sstevel@tonic-gate #include <util/qsort.h>
550Sstevel@tonic-gate #include <sys/disp.h>
560Sstevel@tonic-gate #include <sys/kobj.h>
570Sstevel@tonic-gate #include <sys/crc32.h>
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #ifdef DEBUG
610Sstevel@tonic-gate static int di_debug;
620Sstevel@tonic-gate #define	dcmn_err(args) if (di_debug >= 1) cmn_err args
630Sstevel@tonic-gate #define	dcmn_err2(args) if (di_debug >= 2) cmn_err args
640Sstevel@tonic-gate #define	dcmn_err3(args) if (di_debug >= 3) cmn_err args
650Sstevel@tonic-gate #else
660Sstevel@tonic-gate #define	dcmn_err(args) /* nothing */
670Sstevel@tonic-gate #define	dcmn_err2(args) /* nothing */
680Sstevel@tonic-gate #define	dcmn_err3(args) /* nothing */
690Sstevel@tonic-gate #endif
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate  * We partition the space of devinfo minor nodes equally between the full and
730Sstevel@tonic-gate  * unprivileged versions of the driver.  The even-numbered minor nodes are the
740Sstevel@tonic-gate  * full version, while the odd-numbered ones are the read-only version.
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate static int di_max_opens = 32;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate #define	DI_FULL_PARENT		0
790Sstevel@tonic-gate #define	DI_READONLY_PARENT	1
800Sstevel@tonic-gate #define	DI_NODE_SPECIES		2
810Sstevel@tonic-gate #define	DI_UNPRIVILEGED_NODE(x)	(((x) % 2) != 0)
820Sstevel@tonic-gate 
830Sstevel@tonic-gate #define	IOC_IDLE	0	/* snapshot ioctl states */
840Sstevel@tonic-gate #define	IOC_SNAP	1	/* snapshot in progress */
850Sstevel@tonic-gate #define	IOC_DONE	2	/* snapshot done, but not copied out */
860Sstevel@tonic-gate #define	IOC_COPY	3	/* copyout in progress */
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate  * Keep max alignment so we can move snapshot to different platforms
900Sstevel@tonic-gate  */
910Sstevel@tonic-gate #define	DI_ALIGN(addr)	((addr + 7l) & ~7l)
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * To avoid wasting memory, make a linked list of memory chunks.
950Sstevel@tonic-gate  * Size of each chunk is buf_size.
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate struct di_mem {
980Sstevel@tonic-gate 	struct di_mem *next;	/* link to next chunk */
990Sstevel@tonic-gate 	char *buf;		/* contiguous kernel memory */
1000Sstevel@tonic-gate 	size_t buf_size;	/* size of buf in bytes */
1010Sstevel@tonic-gate 	devmap_cookie_t cook;	/* cookie from ddi_umem_alloc */
1020Sstevel@tonic-gate };
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  * This is a stack for walking the tree without using recursion.
1060Sstevel@tonic-gate  * When the devinfo tree height is above some small size, one
1070Sstevel@tonic-gate  * gets watchdog resets on sun4m.
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate struct di_stack {
1100Sstevel@tonic-gate 	void		*offset[MAX_TREE_DEPTH];
1110Sstevel@tonic-gate 	struct dev_info *dip[MAX_TREE_DEPTH];
1120Sstevel@tonic-gate 	int		circ[MAX_TREE_DEPTH];
1130Sstevel@tonic-gate 	int		depth;	/* depth of current node to be copied */
1140Sstevel@tonic-gate };
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate #define	TOP_OFFSET(stack)	\
1170Sstevel@tonic-gate 	((di_off_t *)(stack)->offset[(stack)->depth - 1])
1180Sstevel@tonic-gate #define	TOP_NODE(stack)		\
1190Sstevel@tonic-gate 	((stack)->dip[(stack)->depth - 1])
1200Sstevel@tonic-gate #define	PARENT_OFFSET(stack)	\
1210Sstevel@tonic-gate 	((di_off_t *)(stack)->offset[(stack)->depth - 2])
1220Sstevel@tonic-gate #define	EMPTY_STACK(stack)	((stack)->depth == 0)
1230Sstevel@tonic-gate #define	POP_STACK(stack)	{ \
1240Sstevel@tonic-gate 	ndi_devi_exit((dev_info_t *)TOP_NODE(stack), \
1250Sstevel@tonic-gate 		(stack)->circ[(stack)->depth - 1]); \
1260Sstevel@tonic-gate 	((stack)->depth--); \
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate #define	PUSH_STACK(stack, node, offp)	{ \
1290Sstevel@tonic-gate 	ASSERT(node != NULL); \
1300Sstevel@tonic-gate 	ndi_devi_enter((dev_info_t *)node, &(stack)->circ[(stack)->depth]); \
1310Sstevel@tonic-gate 	(stack)->dip[(stack)->depth] = (node); \
1320Sstevel@tonic-gate 	(stack)->offset[(stack)->depth] = (void *)(offp); \
1330Sstevel@tonic-gate 	((stack)->depth)++; \
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate #define	DI_ALL_PTR(s)	((struct di_all *)di_mem_addr((s), 0))
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate  * With devfs, the device tree has no global locks. The device tree is
1400Sstevel@tonic-gate  * dynamic and dips may come and go if they are not locked locally. Under
1410Sstevel@tonic-gate  * these conditions, pointers are no longer reliable as unique IDs.
1420Sstevel@tonic-gate  * Specifically, these pointers cannot be used as keys for hash tables
1430Sstevel@tonic-gate  * as the same devinfo structure may be freed in one part of the tree only
1440Sstevel@tonic-gate  * to be allocated as the structure for a different device in another
1450Sstevel@tonic-gate  * part of the tree. This can happen if DR and the snapshot are
1460Sstevel@tonic-gate  * happening concurrently.
1470Sstevel@tonic-gate  * The following data structures act as keys for devinfo nodes and
1480Sstevel@tonic-gate  * pathinfo nodes.
1490Sstevel@tonic-gate  */
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate enum di_ktype {
1520Sstevel@tonic-gate 	DI_DKEY = 1,
1530Sstevel@tonic-gate 	DI_PKEY = 2
1540Sstevel@tonic-gate };
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate struct di_dkey {
1570Sstevel@tonic-gate 	dev_info_t	*dk_dip;
1580Sstevel@tonic-gate 	major_t		dk_major;
1590Sstevel@tonic-gate 	int		dk_inst;
1600Sstevel@tonic-gate 	dnode_t		dk_nodeid;
1610Sstevel@tonic-gate };
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate struct di_pkey {
1640Sstevel@tonic-gate 	mdi_pathinfo_t	*pk_pip;
1650Sstevel@tonic-gate 	char		*pk_path_addr;
1660Sstevel@tonic-gate 	dev_info_t	*pk_client;
1670Sstevel@tonic-gate 	dev_info_t	*pk_phci;
1680Sstevel@tonic-gate };
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate struct di_key {
1710Sstevel@tonic-gate 	enum di_ktype	k_type;
1720Sstevel@tonic-gate 	union {
1730Sstevel@tonic-gate 		struct di_dkey dkey;
1740Sstevel@tonic-gate 		struct di_pkey pkey;
1750Sstevel@tonic-gate 	} k_u;
1760Sstevel@tonic-gate };
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate struct i_lnode;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate typedef struct i_link {
1820Sstevel@tonic-gate 	/*
1830Sstevel@tonic-gate 	 * If a di_link struct representing this i_link struct makes it
1840Sstevel@tonic-gate 	 * into the snapshot, then self will point to the offset of
1850Sstevel@tonic-gate 	 * the di_link struct in the snapshot
1860Sstevel@tonic-gate 	 */
1870Sstevel@tonic-gate 	di_off_t	self;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	int		spec_type;	/* block or char access type */
1900Sstevel@tonic-gate 	struct i_lnode	*src_lnode;	/* src i_lnode */
1910Sstevel@tonic-gate 	struct i_lnode	*tgt_lnode;	/* tgt i_lnode */
1920Sstevel@tonic-gate 	struct i_link	*src_link_next;	/* next src i_link /w same i_lnode */
1930Sstevel@tonic-gate 	struct i_link	*tgt_link_next;	/* next tgt i_link /w same i_lnode */
1940Sstevel@tonic-gate } i_link_t;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate typedef struct i_lnode {
1970Sstevel@tonic-gate 	/*
1980Sstevel@tonic-gate 	 * If a di_lnode struct representing this i_lnode struct makes it
1990Sstevel@tonic-gate 	 * into the snapshot, then self will point to the offset of
2000Sstevel@tonic-gate 	 * the di_lnode struct in the snapshot
2010Sstevel@tonic-gate 	 */
2020Sstevel@tonic-gate 	di_off_t	self;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	/*
2050Sstevel@tonic-gate 	 * used for hashing and comparing i_lnodes
2060Sstevel@tonic-gate 	 */
2070Sstevel@tonic-gate 	int		modid;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	/*
2100Sstevel@tonic-gate 	 * public information describing a link endpoint
2110Sstevel@tonic-gate 	 */
2120Sstevel@tonic-gate 	struct di_node	*di_node;	/* di_node in snapshot */
2130Sstevel@tonic-gate 	dev_t		devt;		/* devt */
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	/*
2160Sstevel@tonic-gate 	 * i_link ptr to links coming into this i_lnode node
2170Sstevel@tonic-gate 	 * (this i_lnode is the target of these i_links)
2180Sstevel@tonic-gate 	 */
2190Sstevel@tonic-gate 	i_link_t	*link_in;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	/*
2220Sstevel@tonic-gate 	 * i_link ptr to links going out of this i_lnode node
2230Sstevel@tonic-gate 	 * (this i_lnode is the source of these i_links)
2240Sstevel@tonic-gate 	 */
2250Sstevel@tonic-gate 	i_link_t	*link_out;
2260Sstevel@tonic-gate } i_lnode_t;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate /*
2290Sstevel@tonic-gate  * Soft state associated with each instance of driver open.
2300Sstevel@tonic-gate  */
2310Sstevel@tonic-gate static struct di_state {
2320Sstevel@tonic-gate 	di_off_t mem_size;	/* total # bytes in memlist	*/
2330Sstevel@tonic-gate 	struct di_mem *memlist;	/* head of memlist		*/
2340Sstevel@tonic-gate 	uint_t command;		/* command from ioctl		*/
2350Sstevel@tonic-gate 	int di_iocstate;	/* snapshot ioctl state		*/
2360Sstevel@tonic-gate 	mod_hash_t *reg_dip_hash;
2370Sstevel@tonic-gate 	mod_hash_t *reg_pip_hash;
2380Sstevel@tonic-gate 	int lnode_count;
2390Sstevel@tonic-gate 	int link_count;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	mod_hash_t *lnode_hash;
2420Sstevel@tonic-gate 	mod_hash_t *link_hash;
2430Sstevel@tonic-gate } **di_states;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate static kmutex_t di_lock;	/* serialize instance assignment */
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate typedef enum {
2480Sstevel@tonic-gate 	DI_QUIET = 0,	/* DI_QUIET must always be 0 */
2490Sstevel@tonic-gate 	DI_ERR,
2500Sstevel@tonic-gate 	DI_INFO,
2510Sstevel@tonic-gate 	DI_TRACE,
2520Sstevel@tonic-gate 	DI_TRACE1,
2530Sstevel@tonic-gate 	DI_TRACE2
2540Sstevel@tonic-gate } di_cache_debug_t;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate static uint_t	di_chunk = 32;		/* I/O chunk size in pages */
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate #define	DI_CACHE_LOCK(c)	(mutex_enter(&(c).cache_lock))
2590Sstevel@tonic-gate #define	DI_CACHE_UNLOCK(c)	(mutex_exit(&(c).cache_lock))
2600Sstevel@tonic-gate #define	DI_CACHE_LOCKED(c)	(mutex_owned(&(c).cache_lock))
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate #define	CACHE_DEBUG(args)	\
2630Sstevel@tonic-gate 	{ if (di_cache_debug != DI_QUIET) di_cache_print args; }
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate static int di_open(dev_t *, int, int, cred_t *);
2660Sstevel@tonic-gate static int di_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
2670Sstevel@tonic-gate static int di_close(dev_t, int, int, cred_t *);
2680Sstevel@tonic-gate static int di_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
2690Sstevel@tonic-gate static int di_attach(dev_info_t *, ddi_attach_cmd_t);
2700Sstevel@tonic-gate static int di_detach(dev_info_t *, ddi_detach_cmd_t);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate static di_off_t di_copyformat(di_off_t, struct di_state *, intptr_t, int);
2730Sstevel@tonic-gate static di_off_t di_snapshot(struct di_state *);
2740Sstevel@tonic-gate static di_off_t di_copydevnm(di_off_t *, struct di_state *);
2750Sstevel@tonic-gate static di_off_t di_copytree(struct dev_info *, di_off_t *, struct di_state *);
2760Sstevel@tonic-gate static di_off_t di_copynode(struct di_stack *, struct di_state *);
2770Sstevel@tonic-gate static di_off_t di_getmdata(struct ddi_minor_data *, di_off_t *, di_off_t,
2780Sstevel@tonic-gate     struct di_state *);
2790Sstevel@tonic-gate static di_off_t di_getppdata(struct dev_info *, di_off_t *, struct di_state *);
2800Sstevel@tonic-gate static di_off_t di_getdpdata(struct dev_info *, di_off_t *, struct di_state *);
2810Sstevel@tonic-gate static di_off_t di_getprop(struct ddi_prop *, di_off_t *,
2820Sstevel@tonic-gate     struct di_state *, struct dev_info *, int);
2830Sstevel@tonic-gate static void di_allocmem(struct di_state *, size_t);
2840Sstevel@tonic-gate static void di_freemem(struct di_state *);
2850Sstevel@tonic-gate static void di_copymem(struct di_state *st, caddr_t buf, size_t bufsiz);
2860Sstevel@tonic-gate static di_off_t di_checkmem(struct di_state *, di_off_t, size_t);
2870Sstevel@tonic-gate static caddr_t di_mem_addr(struct di_state *, di_off_t);
2880Sstevel@tonic-gate static int di_setstate(struct di_state *, int);
2890Sstevel@tonic-gate static void di_register_dip(struct di_state *, dev_info_t *, di_off_t);
2900Sstevel@tonic-gate static void di_register_pip(struct di_state *, mdi_pathinfo_t *, di_off_t);
2910Sstevel@tonic-gate static di_off_t di_getpath_data(dev_info_t *, di_off_t *, di_off_t,
2920Sstevel@tonic-gate     struct di_state *, int);
2930Sstevel@tonic-gate static di_off_t di_getlink_data(di_off_t, struct di_state *);
2940Sstevel@tonic-gate static int di_dip_find(struct di_state *st, dev_info_t *node, di_off_t *off_p);
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate static int cache_args_valid(struct di_state *st, int *error);
2970Sstevel@tonic-gate static int snapshot_is_cacheable(struct di_state *st);
2980Sstevel@tonic-gate static int di_cache_lookup(struct di_state *st);
2990Sstevel@tonic-gate static int di_cache_update(struct di_state *st);
3000Sstevel@tonic-gate static void di_cache_print(di_cache_debug_t msglevel, char *fmt, ...);
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate static struct cb_ops di_cb_ops = {
3030Sstevel@tonic-gate 	di_open,		/* open */
3040Sstevel@tonic-gate 	di_close,		/* close */
3050Sstevel@tonic-gate 	nodev,			/* strategy */
3060Sstevel@tonic-gate 	nodev,			/* print */
3070Sstevel@tonic-gate 	nodev,			/* dump */
3080Sstevel@tonic-gate 	nodev,			/* read */
3090Sstevel@tonic-gate 	nodev,			/* write */
3100Sstevel@tonic-gate 	di_ioctl,		/* ioctl */
3110Sstevel@tonic-gate 	nodev,			/* devmap */
3120Sstevel@tonic-gate 	nodev,			/* mmap */
3130Sstevel@tonic-gate 	nodev,			/* segmap */
3140Sstevel@tonic-gate 	nochpoll,		/* poll */
3150Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
3160Sstevel@tonic-gate 	NULL,			/* streamtab  */
3170Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
3180Sstevel@tonic-gate };
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate static struct dev_ops di_ops = {
3210Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
3220Sstevel@tonic-gate 	0,			/* refcnt  */
3230Sstevel@tonic-gate 	di_info,		/* info */
3240Sstevel@tonic-gate 	nulldev,		/* identify */
3250Sstevel@tonic-gate 	nulldev,		/* probe */
3260Sstevel@tonic-gate 	di_attach,		/* attach */
3270Sstevel@tonic-gate 	di_detach,		/* detach */
3280Sstevel@tonic-gate 	nodev,			/* reset */
3290Sstevel@tonic-gate 	&di_cb_ops,		/* driver operations */
3300Sstevel@tonic-gate 	NULL			/* bus operations */
3310Sstevel@tonic-gate };
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate /*
3340Sstevel@tonic-gate  * Module linkage information for the kernel.
3350Sstevel@tonic-gate  */
3360Sstevel@tonic-gate static struct modldrv modldrv = {
3370Sstevel@tonic-gate 	&mod_driverops,
3380Sstevel@tonic-gate 	"DEVINFO Driver %I%",
3390Sstevel@tonic-gate 	&di_ops
3400Sstevel@tonic-gate };
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate static struct modlinkage modlinkage = {
3430Sstevel@tonic-gate 	MODREV_1,
3440Sstevel@tonic-gate 	&modldrv,
3450Sstevel@tonic-gate 	NULL
3460Sstevel@tonic-gate };
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate int
3490Sstevel@tonic-gate _init(void)
3500Sstevel@tonic-gate {
3510Sstevel@tonic-gate 	int	error;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	mutex_init(&di_lock, NULL, MUTEX_DRIVER, NULL);
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	error = mod_install(&modlinkage);
3560Sstevel@tonic-gate 	if (error != 0) {
3570Sstevel@tonic-gate 		mutex_destroy(&di_lock);
3580Sstevel@tonic-gate 		return (error);
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	return (0);
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate int
3650Sstevel@tonic-gate _info(struct modinfo *modinfop)
3660Sstevel@tonic-gate {
3670Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate int
3710Sstevel@tonic-gate _fini(void)
3720Sstevel@tonic-gate {
3730Sstevel@tonic-gate 	int	error;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
3760Sstevel@tonic-gate 	if (error != 0) {
3770Sstevel@tonic-gate 		return (error);
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	mutex_destroy(&di_lock);
3810Sstevel@tonic-gate 	return (0);
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate static dev_info_t *di_dip;
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate /*ARGSUSED*/
3870Sstevel@tonic-gate static int
3880Sstevel@tonic-gate di_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
3890Sstevel@tonic-gate {
3900Sstevel@tonic-gate 	int error = DDI_FAILURE;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	switch (infocmd) {
3930Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
3940Sstevel@tonic-gate 		*result = (void *)di_dip;
3950Sstevel@tonic-gate 		error = DDI_SUCCESS;
3960Sstevel@tonic-gate 		break;
3970Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
3980Sstevel@tonic-gate 		/*
3990Sstevel@tonic-gate 		 * All dev_t's map to the same, single instance.
4000Sstevel@tonic-gate 		 */
4010Sstevel@tonic-gate 		*result = (void *)0;
4020Sstevel@tonic-gate 		error = DDI_SUCCESS;
4030Sstevel@tonic-gate 		break;
4040Sstevel@tonic-gate 	default:
4050Sstevel@tonic-gate 		break;
4060Sstevel@tonic-gate 	}
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	return (error);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate static int
4120Sstevel@tonic-gate di_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4130Sstevel@tonic-gate {
4140Sstevel@tonic-gate 	int error = DDI_FAILURE;
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	switch (cmd) {
4170Sstevel@tonic-gate 	case DDI_ATTACH:
4180Sstevel@tonic-gate 		di_states = kmem_zalloc(
4190Sstevel@tonic-gate 		    di_max_opens * sizeof (struct di_state *), KM_SLEEP);
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, "devinfo", S_IFCHR,
4220Sstevel@tonic-gate 		    DI_FULL_PARENT, DDI_PSEUDO, NULL) == DDI_FAILURE ||
4230Sstevel@tonic-gate 		    ddi_create_minor_node(dip, "devinfo,ro", S_IFCHR,
4240Sstevel@tonic-gate 		    DI_READONLY_PARENT, DDI_PSEUDO, NULL) == DDI_FAILURE) {
4250Sstevel@tonic-gate 			kmem_free(di_states,
4260Sstevel@tonic-gate 			    di_max_opens * sizeof (struct di_state *));
4270Sstevel@tonic-gate 			ddi_remove_minor_node(dip, NULL);
4280Sstevel@tonic-gate 			error = DDI_FAILURE;
4290Sstevel@tonic-gate 		} else {
4300Sstevel@tonic-gate 			di_dip = dip;
4310Sstevel@tonic-gate 			ddi_report_dev(dip);
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 			error = DDI_SUCCESS;
4340Sstevel@tonic-gate 		}
4350Sstevel@tonic-gate 		break;
4360Sstevel@tonic-gate 	default:
4370Sstevel@tonic-gate 		error = DDI_FAILURE;
4380Sstevel@tonic-gate 		break;
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	return (error);
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate static int
4450Sstevel@tonic-gate di_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4460Sstevel@tonic-gate {
4470Sstevel@tonic-gate 	int error = DDI_FAILURE;
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	switch (cmd) {
4500Sstevel@tonic-gate 	case DDI_DETACH:
4510Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
4520Sstevel@tonic-gate 		di_dip = NULL;
4530Sstevel@tonic-gate 		kmem_free(di_states, di_max_opens * sizeof (struct di_state *));
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 		error = DDI_SUCCESS;
4560Sstevel@tonic-gate 		break;
4570Sstevel@tonic-gate 	default:
4580Sstevel@tonic-gate 		error = DDI_FAILURE;
4590Sstevel@tonic-gate 		break;
4600Sstevel@tonic-gate 	}
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	return (error);
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate /*
4660Sstevel@tonic-gate  * Allow multiple opens by tweaking the dev_t such that it looks like each
4670Sstevel@tonic-gate  * open is getting a different minor device.  Each minor gets a separate
4680Sstevel@tonic-gate  * entry in the di_states[] table.  Based on the original minor number, we
4690Sstevel@tonic-gate  * discriminate opens of the full and read-only nodes.  If all of the instances
4700Sstevel@tonic-gate  * of the selected minor node are currently open, we return EAGAIN.
4710Sstevel@tonic-gate  */
4720Sstevel@tonic-gate /*ARGSUSED*/
4730Sstevel@tonic-gate static int
4740Sstevel@tonic-gate di_open(dev_t *devp, int flag, int otyp, cred_t *credp)
4750Sstevel@tonic-gate {
4760Sstevel@tonic-gate 	int m;
4770Sstevel@tonic-gate 	minor_t minor_parent = getminor(*devp);
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	if (minor_parent != DI_FULL_PARENT &&
4800Sstevel@tonic-gate 	    minor_parent != DI_READONLY_PARENT)
4810Sstevel@tonic-gate 		return (ENXIO);
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	mutex_enter(&di_lock);
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	for (m = minor_parent; m < di_max_opens; m += DI_NODE_SPECIES) {
4860Sstevel@tonic-gate 		if (di_states[m] != NULL)
4870Sstevel@tonic-gate 			continue;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 		di_states[m] = kmem_zalloc(sizeof (struct di_state), KM_SLEEP);
4900Sstevel@tonic-gate 		break;	/* It's ours. */
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	if (m >= di_max_opens) {
4940Sstevel@tonic-gate 		/*
4950Sstevel@tonic-gate 		 * maximum open instance for device reached
4960Sstevel@tonic-gate 		 */
4970Sstevel@tonic-gate 		mutex_exit(&di_lock);
4980Sstevel@tonic-gate 		dcmn_err((CE_WARN, "devinfo: maximum devinfo open reached"));
4990Sstevel@tonic-gate 		return (EAGAIN);
5000Sstevel@tonic-gate 	}
5010Sstevel@tonic-gate 	mutex_exit(&di_lock);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	ASSERT(m < di_max_opens);
5040Sstevel@tonic-gate 	*devp = makedevice(getmajor(*devp), (minor_t)(m + DI_NODE_SPECIES));
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	dcmn_err((CE_CONT, "di_open: thread = %p, assigned minor = %d\n",
5070Sstevel@tonic-gate 		(void *)curthread, m + DI_NODE_SPECIES));
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	return (0);
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate /*ARGSUSED*/
5130Sstevel@tonic-gate static int
5140Sstevel@tonic-gate di_close(dev_t dev, int flag, int otype, cred_t *cred_p)
5150Sstevel@tonic-gate {
5160Sstevel@tonic-gate 	struct di_state *st;
5170Sstevel@tonic-gate 	int m = (int)getminor(dev) - DI_NODE_SPECIES;
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	if (m < 0) {
5200Sstevel@tonic-gate 		cmn_err(CE_WARN, "closing non-existent devinfo minor %d",
5210Sstevel@tonic-gate 		    m + DI_NODE_SPECIES);
5220Sstevel@tonic-gate 		return (ENXIO);
5230Sstevel@tonic-gate 	}
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	st = di_states[m];
5260Sstevel@tonic-gate 	ASSERT(m < di_max_opens && st != NULL);
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	di_freemem(st);
5290Sstevel@tonic-gate 	kmem_free(st, sizeof (struct di_state));
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	/*
5320Sstevel@tonic-gate 	 * empty slot in state table
5330Sstevel@tonic-gate 	 */
5340Sstevel@tonic-gate 	mutex_enter(&di_lock);
5350Sstevel@tonic-gate 	di_states[m] = NULL;
5360Sstevel@tonic-gate 	dcmn_err((CE_CONT, "di_close: thread = %p, assigned minor = %d\n",
5370Sstevel@tonic-gate 		(void *)curthread, m + DI_NODE_SPECIES));
5380Sstevel@tonic-gate 	mutex_exit(&di_lock);
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	return (0);
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate /*ARGSUSED*/
5450Sstevel@tonic-gate static int
5460Sstevel@tonic-gate di_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
5470Sstevel@tonic-gate {
5480Sstevel@tonic-gate 	int rv, error;
5490Sstevel@tonic-gate 	di_off_t off;
5500Sstevel@tonic-gate 	struct di_all *all;
5510Sstevel@tonic-gate 	struct di_state *st;
5520Sstevel@tonic-gate 	int m = (int)getminor(dev) - DI_NODE_SPECIES;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	major_t i;
5550Sstevel@tonic-gate 	char *drv_name;
5560Sstevel@tonic-gate 	size_t map_size, size;
5570Sstevel@tonic-gate 	struct di_mem *dcp;
5580Sstevel@tonic-gate 	int ndi_flags;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	if (m < 0 || m >= di_max_opens) {
5610Sstevel@tonic-gate 		return (ENXIO);
5620Sstevel@tonic-gate 	}
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	st = di_states[m];
5650Sstevel@tonic-gate 	ASSERT(st != NULL);
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_ioctl: mode = %x, cmd = %x\n", mode, cmd));
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	switch (cmd) {
5700Sstevel@tonic-gate 	case DINFOIDENT:
5710Sstevel@tonic-gate 		/*
5720Sstevel@tonic-gate 		 * This is called from di_init to verify that the driver
5730Sstevel@tonic-gate 		 * opened is indeed devinfo. The purpose is to guard against
5740Sstevel@tonic-gate 		 * sending ioctl to an unknown driver in case of an
5750Sstevel@tonic-gate 		 * unresolved major number conflict during bfu.
5760Sstevel@tonic-gate 		 */
5770Sstevel@tonic-gate 		*rvalp = DI_MAGIC;
5780Sstevel@tonic-gate 		return (0);
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	case DINFOLODRV:
5810Sstevel@tonic-gate 		/*
5820Sstevel@tonic-gate 		 * Hold an installed driver and return the result
5830Sstevel@tonic-gate 		 */
5840Sstevel@tonic-gate 		if (DI_UNPRIVILEGED_NODE(m)) {
5850Sstevel@tonic-gate 			/*
5860Sstevel@tonic-gate 			 * Only the fully enabled instances may issue
5870Sstevel@tonic-gate 			 * DINFOLDDRV.
5880Sstevel@tonic-gate 			 */
5890Sstevel@tonic-gate 			return (EACCES);
5900Sstevel@tonic-gate 		}
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 		drv_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
5930Sstevel@tonic-gate 		if (ddi_copyin((void *)arg, drv_name, MAXNAMELEN, mode) != 0) {
5940Sstevel@tonic-gate 			kmem_free(drv_name, MAXNAMELEN);
5950Sstevel@tonic-gate 			return (EFAULT);
5960Sstevel@tonic-gate 		}
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 		/*
5990Sstevel@tonic-gate 		 * Some 3rd party driver's _init() walks the device tree,
6000Sstevel@tonic-gate 		 * so we load the driver module before configuring driver.
6010Sstevel@tonic-gate 		 */
6020Sstevel@tonic-gate 		i = ddi_name_to_major(drv_name);
6030Sstevel@tonic-gate 		if (ddi_hold_driver(i) == NULL) {
6040Sstevel@tonic-gate 			kmem_free(drv_name, MAXNAMELEN);
6050Sstevel@tonic-gate 			return (ENXIO);
6060Sstevel@tonic-gate 		}
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 		ndi_flags = NDI_DEVI_PERSIST | NDI_CONFIG | NDI_NO_EVENT;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 		/*
6110Sstevel@tonic-gate 		 * i_ddi_load_drvconf() below will trigger a reprobe
6120Sstevel@tonic-gate 		 * via reset_nexus_flags(). NDI_DRV_CONF_REPROBE isn't
6130Sstevel@tonic-gate 		 * needed here.
6140Sstevel@tonic-gate 		 */
6150Sstevel@tonic-gate 		modunload_disable();
6160Sstevel@tonic-gate 		(void) i_ddi_load_drvconf(i);
6170Sstevel@tonic-gate 		(void) ndi_devi_config_driver(ddi_root_node(), ndi_flags, i);
6180Sstevel@tonic-gate 		kmem_free(drv_name, MAXNAMELEN);
6190Sstevel@tonic-gate 		ddi_rele_driver(i);
6200Sstevel@tonic-gate 		rv = i_ddi_devs_attached(i);
6210Sstevel@tonic-gate 		modunload_enable();
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 		i_ddi_di_cache_invalidate(KM_SLEEP);
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 		return ((rv == DDI_SUCCESS)? 0 : ENXIO);
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	case DINFOUSRLD:
6280Sstevel@tonic-gate 		/*
6290Sstevel@tonic-gate 		 * The case for copying snapshot to userland
6300Sstevel@tonic-gate 		 */
6310Sstevel@tonic-gate 		if (di_setstate(st, IOC_COPY) == -1)
6320Sstevel@tonic-gate 			return (EBUSY);
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 		map_size = ((struct di_all *)di_mem_addr(st, 0))->map_size;
6350Sstevel@tonic-gate 		if (map_size == 0) {
6360Sstevel@tonic-gate 			(void) di_setstate(st, IOC_DONE);
6370Sstevel@tonic-gate 			return (EFAULT);
6380Sstevel@tonic-gate 		}
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 		/*
6410Sstevel@tonic-gate 		 * copyout the snapshot
6420Sstevel@tonic-gate 		 */
6430Sstevel@tonic-gate 		map_size = (map_size + PAGEOFFSET) & PAGEMASK;
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 		/*
6460Sstevel@tonic-gate 		 * Return the map size, so caller may do a sanity
6470Sstevel@tonic-gate 		 * check against the return value of snapshot ioctl()
6480Sstevel@tonic-gate 		 */
6490Sstevel@tonic-gate 		*rvalp = (int)map_size;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 		/*
6520Sstevel@tonic-gate 		 * Copy one chunk at a time
6530Sstevel@tonic-gate 		 */
6540Sstevel@tonic-gate 		off = 0;
6550Sstevel@tonic-gate 		dcp = st->memlist;
6560Sstevel@tonic-gate 		while (map_size) {
6570Sstevel@tonic-gate 			size = dcp->buf_size;
6580Sstevel@tonic-gate 			if (map_size <= size) {
6590Sstevel@tonic-gate 				size = map_size;
6600Sstevel@tonic-gate 			}
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 			if (ddi_copyout(di_mem_addr(st, off),
6630Sstevel@tonic-gate 			    (void *)(arg + off), size, mode) != 0) {
6640Sstevel@tonic-gate 				(void) di_setstate(st, IOC_DONE);
6650Sstevel@tonic-gate 				return (EFAULT);
6660Sstevel@tonic-gate 			}
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 			map_size -= size;
6690Sstevel@tonic-gate 			off += size;
6700Sstevel@tonic-gate 			dcp = dcp->next;
6710Sstevel@tonic-gate 		}
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 		di_freemem(st);
6740Sstevel@tonic-gate 		(void) di_setstate(st, IOC_IDLE);
6750Sstevel@tonic-gate 		return (0);
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	default:
6780Sstevel@tonic-gate 		if ((cmd & ~DIIOC_MASK) != DIIOC) {
6790Sstevel@tonic-gate 			/*
6800Sstevel@tonic-gate 			 * Invalid ioctl command
6810Sstevel@tonic-gate 			 */
6820Sstevel@tonic-gate 			return (ENOTTY);
6830Sstevel@tonic-gate 		}
6840Sstevel@tonic-gate 		/*
6850Sstevel@tonic-gate 		 * take a snapshot
6860Sstevel@tonic-gate 		 */
6870Sstevel@tonic-gate 		st->command = cmd & DIIOC_MASK;
6880Sstevel@tonic-gate 		/*FALLTHROUGH*/
6890Sstevel@tonic-gate 	}
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	/*
6920Sstevel@tonic-gate 	 * Obtain enough memory to hold header + rootpath.  We prevent kernel
6930Sstevel@tonic-gate 	 * memory exhaustion by freeing any previously allocated snapshot and
6940Sstevel@tonic-gate 	 * refusing the operation; otherwise we would be allowing ioctl(),
6950Sstevel@tonic-gate 	 * ioctl(), ioctl(), ..., panic.
6960Sstevel@tonic-gate 	 */
6970Sstevel@tonic-gate 	if (di_setstate(st, IOC_SNAP) == -1)
6980Sstevel@tonic-gate 		return (EBUSY);
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	size = sizeof (struct di_all) +
7010Sstevel@tonic-gate 	    sizeof (((struct dinfo_io *)(NULL))->root_path);
7020Sstevel@tonic-gate 	if (size < PAGESIZE)
7030Sstevel@tonic-gate 		size = PAGESIZE;
7040Sstevel@tonic-gate 	di_allocmem(st, size);
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	all = (struct di_all *)di_mem_addr(st, 0);
7070Sstevel@tonic-gate 	all->devcnt = devcnt;
7080Sstevel@tonic-gate 	all->command = st->command;
7090Sstevel@tonic-gate 	all->version = DI_SNAPSHOT_VERSION;
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	/*
7120Sstevel@tonic-gate 	 * Note the endianness in case we need to transport snapshot
7130Sstevel@tonic-gate 	 * over the network.
7140Sstevel@tonic-gate 	 */
7150Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
7160Sstevel@tonic-gate 	all->endianness = DI_LITTLE_ENDIAN;
7170Sstevel@tonic-gate #else
7180Sstevel@tonic-gate 	all->endianness = DI_BIG_ENDIAN;
7190Sstevel@tonic-gate #endif
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	/* Copyin ioctl args, store in the snapshot. */
7220Sstevel@tonic-gate 	if (copyinstr((void *)arg, all->root_path,
7230Sstevel@tonic-gate 	    sizeof (((struct dinfo_io *)(NULL))->root_path), &size) != 0) {
7240Sstevel@tonic-gate 		di_freemem(st);
7250Sstevel@tonic-gate 		(void) di_setstate(st, IOC_IDLE);
7260Sstevel@tonic-gate 		return (EFAULT);
7270Sstevel@tonic-gate 	}
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	error = 0;
7300Sstevel@tonic-gate 	if ((st->command & DINFOCACHE) && !cache_args_valid(st, &error)) {
7310Sstevel@tonic-gate 		di_freemem(st);
7320Sstevel@tonic-gate 		(void) di_setstate(st, IOC_IDLE);
7330Sstevel@tonic-gate 		return (error);
7340Sstevel@tonic-gate 	}
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	off = DI_ALIGN(sizeof (struct di_all) + size);
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	/*
7390Sstevel@tonic-gate 	 * Only the fully enabled version may force load drivers or read
7400Sstevel@tonic-gate 	 * the parent private data from a driver.
7410Sstevel@tonic-gate 	 */
7420Sstevel@tonic-gate 	if ((st->command & (DINFOPRIVDATA | DINFOFORCE)) != 0 &&
7430Sstevel@tonic-gate 	    DI_UNPRIVILEGED_NODE(m)) {
7440Sstevel@tonic-gate 		di_freemem(st);
7450Sstevel@tonic-gate 		(void) di_setstate(st, IOC_IDLE);
7460Sstevel@tonic-gate 		return (EACCES);
7470Sstevel@tonic-gate 	}
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	/* Do we need private data? */
7500Sstevel@tonic-gate 	if (st->command & DINFOPRIVDATA) {
7510Sstevel@tonic-gate 		arg += sizeof (((struct dinfo_io *)(NULL))->root_path);
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
7540Sstevel@tonic-gate 		switch (ddi_model_convert_from(mode & FMODELS)) {
7550Sstevel@tonic-gate 		case DDI_MODEL_ILP32: {
7560Sstevel@tonic-gate 			/*
7570Sstevel@tonic-gate 			 * Cannot copy private data from 64-bit kernel
7580Sstevel@tonic-gate 			 * to 32-bit app
7590Sstevel@tonic-gate 			 */
7600Sstevel@tonic-gate 			di_freemem(st);
7610Sstevel@tonic-gate 			(void) di_setstate(st, IOC_IDLE);
7620Sstevel@tonic-gate 			return (EINVAL);
7630Sstevel@tonic-gate 		}
7640Sstevel@tonic-gate 		case DDI_MODEL_NONE:
7650Sstevel@tonic-gate 			if ((off = di_copyformat(off, st, arg, mode)) == 0) {
7660Sstevel@tonic-gate 				di_freemem(st);
7670Sstevel@tonic-gate 				(void) di_setstate(st, IOC_IDLE);
7680Sstevel@tonic-gate 				return (EFAULT);
7690Sstevel@tonic-gate 			}
7700Sstevel@tonic-gate 			break;
7710Sstevel@tonic-gate 		}
7720Sstevel@tonic-gate #else /* !_MULTI_DATAMODEL */
7730Sstevel@tonic-gate 		if ((off = di_copyformat(off, st, arg, mode)) == 0) {
7740Sstevel@tonic-gate 			di_freemem(st);
7750Sstevel@tonic-gate 			(void) di_setstate(st, IOC_IDLE);
7760Sstevel@tonic-gate 			return (EFAULT);
7770Sstevel@tonic-gate 		}
7780Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
7790Sstevel@tonic-gate 	}
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	all->top_devinfo = DI_ALIGN(off);
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	/*
7840Sstevel@tonic-gate 	 * For cache lookups we reallocate memory from scratch,
7850Sstevel@tonic-gate 	 * so the value of "all" is no longer valid.
7860Sstevel@tonic-gate 	 */
7870Sstevel@tonic-gate 	all = NULL;
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	if (st->command & DINFOCACHE) {
7900Sstevel@tonic-gate 		*rvalp = di_cache_lookup(st);
7910Sstevel@tonic-gate 	} else if (snapshot_is_cacheable(st)) {
7920Sstevel@tonic-gate 		DI_CACHE_LOCK(di_cache);
7930Sstevel@tonic-gate 		*rvalp = di_cache_update(st);
7940Sstevel@tonic-gate 		DI_CACHE_UNLOCK(di_cache);
7950Sstevel@tonic-gate 	} else {
7960Sstevel@tonic-gate 		modunload_disable();
7970Sstevel@tonic-gate 		*rvalp = di_snapshot(st);
7980Sstevel@tonic-gate 		modunload_enable();
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	if (*rvalp) {
8020Sstevel@tonic-gate 		DI_ALL_PTR(st)->map_size = *rvalp;
8030Sstevel@tonic-gate 		(void) di_setstate(st, IOC_DONE);
8040Sstevel@tonic-gate 	} else {
8050Sstevel@tonic-gate 		di_freemem(st);
8060Sstevel@tonic-gate 		(void) di_setstate(st, IOC_IDLE);
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	return (0);
8100Sstevel@tonic-gate }
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate /*
8130Sstevel@tonic-gate  * Get a chunk of memory >= size, for the snapshot
8140Sstevel@tonic-gate  */
8150Sstevel@tonic-gate static void
8160Sstevel@tonic-gate di_allocmem(struct di_state *st, size_t size)
8170Sstevel@tonic-gate {
8180Sstevel@tonic-gate 	struct di_mem *mem = kmem_zalloc(sizeof (struct di_mem),
8190Sstevel@tonic-gate 	    KM_SLEEP);
8200Sstevel@tonic-gate 	/*
8210Sstevel@tonic-gate 	 * Round up size to nearest power of 2. If it is less
8220Sstevel@tonic-gate 	 * than st->mem_size, set it to st->mem_size (i.e.,
8230Sstevel@tonic-gate 	 * the mem_size is doubled every time) to reduce the
8240Sstevel@tonic-gate 	 * number of memory allocations.
8250Sstevel@tonic-gate 	 */
8260Sstevel@tonic-gate 	size_t tmp = 1;
8270Sstevel@tonic-gate 	while (tmp < size) {
8280Sstevel@tonic-gate 		tmp <<= 1;
8290Sstevel@tonic-gate 	}
8300Sstevel@tonic-gate 	size = (tmp > st->mem_size) ? tmp : st->mem_size;
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	mem->buf = ddi_umem_alloc(size, DDI_UMEM_SLEEP, &mem->cook);
8330Sstevel@tonic-gate 	mem->buf_size = size;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_allocmem: mem_size=%x\n", st->mem_size));
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	if (st->mem_size == 0) {	/* first chunk */
8380Sstevel@tonic-gate 		st->memlist = mem;
8390Sstevel@tonic-gate 	} else {
8400Sstevel@tonic-gate 		/*
8410Sstevel@tonic-gate 		 * locate end of linked list and add a chunk at the end
8420Sstevel@tonic-gate 		 */
8430Sstevel@tonic-gate 		struct di_mem *dcp = st->memlist;
8440Sstevel@tonic-gate 		while (dcp->next != NULL) {
8450Sstevel@tonic-gate 			dcp = dcp->next;
8460Sstevel@tonic-gate 		}
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 		dcp->next = mem;
8490Sstevel@tonic-gate 	}
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	st->mem_size += size;
8520Sstevel@tonic-gate }
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate /*
8550Sstevel@tonic-gate  * Copy upto bufsiz bytes of the memlist to buf
8560Sstevel@tonic-gate  */
8570Sstevel@tonic-gate static void
8580Sstevel@tonic-gate di_copymem(struct di_state *st, caddr_t buf, size_t bufsiz)
8590Sstevel@tonic-gate {
8600Sstevel@tonic-gate 	struct di_mem *dcp;
8610Sstevel@tonic-gate 	size_t copysz;
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	if (st->mem_size == 0) {
8640Sstevel@tonic-gate 		ASSERT(st->memlist == NULL);
8650Sstevel@tonic-gate 		return;
8660Sstevel@tonic-gate 	}
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	copysz = 0;
8690Sstevel@tonic-gate 	for (dcp = st->memlist; dcp; dcp = dcp->next) {
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 		ASSERT(bufsiz > 0);
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 		if (bufsiz <= dcp->buf_size)
8740Sstevel@tonic-gate 			copysz = bufsiz;
8750Sstevel@tonic-gate 		else
8760Sstevel@tonic-gate 			copysz = dcp->buf_size;
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 		bcopy(dcp->buf, buf, copysz);
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 		buf += copysz;
8810Sstevel@tonic-gate 		bufsiz -= copysz;
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 		if (bufsiz == 0)
8840Sstevel@tonic-gate 			break;
8850Sstevel@tonic-gate 	}
8860Sstevel@tonic-gate }
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate /*
8890Sstevel@tonic-gate  * Free all memory for the snapshot
8900Sstevel@tonic-gate  */
8910Sstevel@tonic-gate static void
8920Sstevel@tonic-gate di_freemem(struct di_state *st)
8930Sstevel@tonic-gate {
8940Sstevel@tonic-gate 	struct di_mem *dcp, *tmp;
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_freemem\n"));
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	if (st->mem_size) {
8990Sstevel@tonic-gate 		dcp = st->memlist;
9000Sstevel@tonic-gate 		while (dcp) {	/* traverse the linked list */
9010Sstevel@tonic-gate 			tmp = dcp;
9020Sstevel@tonic-gate 			dcp = dcp->next;
9030Sstevel@tonic-gate 			ddi_umem_free(tmp->cook);
9040Sstevel@tonic-gate 			kmem_free(tmp, sizeof (struct di_mem));
9050Sstevel@tonic-gate 		}
9060Sstevel@tonic-gate 		st->mem_size = 0;
9070Sstevel@tonic-gate 		st->memlist = NULL;
9080Sstevel@tonic-gate 	}
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	ASSERT(st->mem_size == 0);
9110Sstevel@tonic-gate 	ASSERT(st->memlist == NULL);
9120Sstevel@tonic-gate }
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate /*
9150Sstevel@tonic-gate  * Copies cached data to the di_state structure.
9160Sstevel@tonic-gate  * Returns:
9170Sstevel@tonic-gate  *	- size of data copied, on SUCCESS
9180Sstevel@tonic-gate  *	- 0 on failure
9190Sstevel@tonic-gate  */
9200Sstevel@tonic-gate static int
9210Sstevel@tonic-gate di_cache2mem(struct di_cache *cache, struct di_state *st)
9220Sstevel@tonic-gate {
9230Sstevel@tonic-gate 	caddr_t	pa;
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	ASSERT(st->mem_size == 0);
9260Sstevel@tonic-gate 	ASSERT(st->memlist == NULL);
9270Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
9280Sstevel@tonic-gate 	ASSERT(DI_CACHE_LOCKED(*cache));
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	if (cache->cache_size == 0) {
9310Sstevel@tonic-gate 		ASSERT(cache->cache_data == NULL);
9320Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "Empty cache. Skipping copy"));
9330Sstevel@tonic-gate 		return (0);
9340Sstevel@tonic-gate 	}
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	ASSERT(cache->cache_data);
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	di_allocmem(st, cache->cache_size);
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 	pa = di_mem_addr(st, 0);
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 	ASSERT(pa);
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	/*
9450Sstevel@tonic-gate 	 * Verify that di_allocmem() allocates contiguous memory,
9460Sstevel@tonic-gate 	 * so that it is safe to do straight bcopy()
9470Sstevel@tonic-gate 	 */
9480Sstevel@tonic-gate 	ASSERT(st->memlist != NULL);
9490Sstevel@tonic-gate 	ASSERT(st->memlist->next == NULL);
9500Sstevel@tonic-gate 	bcopy(cache->cache_data, pa, cache->cache_size);
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	return (cache->cache_size);
9530Sstevel@tonic-gate }
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate /*
9560Sstevel@tonic-gate  * Copies a snapshot from di_state to the cache
9570Sstevel@tonic-gate  * Returns:
9580Sstevel@tonic-gate  *	- 0 on failure
9590Sstevel@tonic-gate  *	- size of copied data on success
9600Sstevel@tonic-gate  */
9610Sstevel@tonic-gate static int
9620Sstevel@tonic-gate di_mem2cache(struct di_state *st, struct di_cache *cache)
9630Sstevel@tonic-gate {
9640Sstevel@tonic-gate 	size_t map_size;
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	ASSERT(cache->cache_size == 0);
9670Sstevel@tonic-gate 	ASSERT(cache->cache_data == NULL);
9680Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
9690Sstevel@tonic-gate 	ASSERT(DI_CACHE_LOCKED(*cache));
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	if (st->mem_size == 0) {
9720Sstevel@tonic-gate 		ASSERT(st->memlist == NULL);
9730Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "Empty memlist. Skipping copy"));
9740Sstevel@tonic-gate 		return (0);
9750Sstevel@tonic-gate 	}
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	ASSERT(st->memlist);
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 	/*
9800Sstevel@tonic-gate 	 * The size of the memory list may be much larger than the
9810Sstevel@tonic-gate 	 * size of valid data (map_size). Cache only the valid data
9820Sstevel@tonic-gate 	 */
9830Sstevel@tonic-gate 	map_size = DI_ALL_PTR(st)->map_size;
9840Sstevel@tonic-gate 	if (map_size == 0 || map_size < sizeof (struct di_all) ||
9850Sstevel@tonic-gate 	    map_size > st->mem_size) {
9860Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "cannot cache: bad size: 0x%x", map_size));
9870Sstevel@tonic-gate 		return (0);
9880Sstevel@tonic-gate 	}
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	cache->cache_data = kmem_alloc(map_size, KM_SLEEP);
9910Sstevel@tonic-gate 	cache->cache_size = map_size;
9920Sstevel@tonic-gate 	di_copymem(st, cache->cache_data, cache->cache_size);
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	return (map_size);
9950Sstevel@tonic-gate }
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate /*
9980Sstevel@tonic-gate  * Make sure there is at least "size" bytes memory left before
9990Sstevel@tonic-gate  * going on. Otherwise, start on a new chunk.
10000Sstevel@tonic-gate  */
10010Sstevel@tonic-gate static di_off_t
10020Sstevel@tonic-gate di_checkmem(struct di_state *st, di_off_t off, size_t size)
10030Sstevel@tonic-gate {
10040Sstevel@tonic-gate 	dcmn_err3((CE_CONT, "di_checkmem: off=%x size=%x\n",
10050Sstevel@tonic-gate 			off, (int)size));
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 	/*
10080Sstevel@tonic-gate 	 * di_checkmem() shouldn't be called with a size of zero.
10090Sstevel@tonic-gate 	 * But in case it is, we want to make sure we return a valid
10100Sstevel@tonic-gate 	 * offset within the memlist and not an offset that points us
10110Sstevel@tonic-gate 	 * at the end of the memlist.
10120Sstevel@tonic-gate 	 */
10130Sstevel@tonic-gate 	if (size == 0) {
10140Sstevel@tonic-gate 		dcmn_err((CE_WARN, "di_checkmem: invalid zero size used"));
10150Sstevel@tonic-gate 		size = 1;
10160Sstevel@tonic-gate 	}
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	off = DI_ALIGN(off);
10190Sstevel@tonic-gate 	if ((st->mem_size - off) < size) {
10200Sstevel@tonic-gate 		off = st->mem_size;
10210Sstevel@tonic-gate 		di_allocmem(st, size);
10220Sstevel@tonic-gate 	}
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 	return (off);
10250Sstevel@tonic-gate }
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate /*
10280Sstevel@tonic-gate  * Copy the private data format from ioctl arg.
10290Sstevel@tonic-gate  * On success, the ending offset is returned. On error 0 is returned.
10300Sstevel@tonic-gate  */
10310Sstevel@tonic-gate static di_off_t
10320Sstevel@tonic-gate di_copyformat(di_off_t off, struct di_state *st, intptr_t arg, int mode)
10330Sstevel@tonic-gate {
10340Sstevel@tonic-gate 	di_off_t size;
10350Sstevel@tonic-gate 	struct di_priv_data *priv;
10360Sstevel@tonic-gate 	struct di_all *all = (struct di_all *)di_mem_addr(st, 0);
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_copyformat: off=%x, arg=%p mode=%x\n",
10390Sstevel@tonic-gate 		off, (void *)arg, mode));
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	/*
10420Sstevel@tonic-gate 	 * Copyin data and check version.
10430Sstevel@tonic-gate 	 * We only handle private data version 0.
10440Sstevel@tonic-gate 	 */
10450Sstevel@tonic-gate 	priv = kmem_alloc(sizeof (struct di_priv_data), KM_SLEEP);
10460Sstevel@tonic-gate 	if ((ddi_copyin((void *)arg, priv, sizeof (struct di_priv_data),
10470Sstevel@tonic-gate 	    mode) != 0) || (priv->version != DI_PRIVDATA_VERSION_0)) {
10480Sstevel@tonic-gate 		kmem_free(priv, sizeof (struct di_priv_data));
10490Sstevel@tonic-gate 		return (0);
10500Sstevel@tonic-gate 	}
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	/*
10530Sstevel@tonic-gate 	 * Save di_priv_data copied from userland in snapshot.
10540Sstevel@tonic-gate 	 */
10550Sstevel@tonic-gate 	all->pd_version = priv->version;
10560Sstevel@tonic-gate 	all->n_ppdata = priv->n_parent;
10570Sstevel@tonic-gate 	all->n_dpdata = priv->n_driver;
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	/*
10600Sstevel@tonic-gate 	 * copyin private data format, modify offset accordingly
10610Sstevel@tonic-gate 	 */
10620Sstevel@tonic-gate 	if (all->n_ppdata) {	/* parent private data format */
10630Sstevel@tonic-gate 		/*
10640Sstevel@tonic-gate 		 * check memory
10650Sstevel@tonic-gate 		 */
10660Sstevel@tonic-gate 		size = all->n_ppdata * sizeof (struct di_priv_format);
10670Sstevel@tonic-gate 		off = di_checkmem(st, off, size);
10680Sstevel@tonic-gate 		all->ppdata_format = off;
10690Sstevel@tonic-gate 		if (ddi_copyin(priv->parent, di_mem_addr(st, off), size,
10700Sstevel@tonic-gate 		    mode) != 0) {
10710Sstevel@tonic-gate 			kmem_free(priv, sizeof (struct di_priv_data));
10720Sstevel@tonic-gate 			return (0);
10730Sstevel@tonic-gate 		}
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 		off += size;
10760Sstevel@tonic-gate 	}
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 	if (all->n_dpdata) {	/* driver private data format */
10790Sstevel@tonic-gate 		/*
10800Sstevel@tonic-gate 		 * check memory
10810Sstevel@tonic-gate 		 */
10820Sstevel@tonic-gate 		size = all->n_dpdata * sizeof (struct di_priv_format);
10830Sstevel@tonic-gate 		off = di_checkmem(st, off, size);
10840Sstevel@tonic-gate 		all->dpdata_format = off;
10850Sstevel@tonic-gate 		if (ddi_copyin(priv->driver, di_mem_addr(st, off), size,
10860Sstevel@tonic-gate 		    mode) != 0) {
10870Sstevel@tonic-gate 			kmem_free(priv, sizeof (struct di_priv_data));
10880Sstevel@tonic-gate 			return (0);
10890Sstevel@tonic-gate 		}
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 		off += size;
10920Sstevel@tonic-gate 	}
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	kmem_free(priv, sizeof (struct di_priv_data));
10950Sstevel@tonic-gate 	return (off);
10960Sstevel@tonic-gate }
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate /*
10990Sstevel@tonic-gate  * Return the real address based on the offset (off) within snapshot
11000Sstevel@tonic-gate  */
11010Sstevel@tonic-gate static caddr_t
11020Sstevel@tonic-gate di_mem_addr(struct di_state *st, di_off_t off)
11030Sstevel@tonic-gate {
11040Sstevel@tonic-gate 	struct di_mem *dcp = st->memlist;
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	dcmn_err3((CE_CONT, "di_mem_addr: dcp=%p off=%x\n",
11070Sstevel@tonic-gate 		(void *)dcp, off));
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	ASSERT(off < st->mem_size);
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	while (off >= dcp->buf_size) {
11120Sstevel@tonic-gate 		off -= dcp->buf_size;
11130Sstevel@tonic-gate 		dcp = dcp->next;
11140Sstevel@tonic-gate 	}
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	dcmn_err3((CE_CONT, "di_mem_addr: new off=%x, return = %p\n",
11170Sstevel@tonic-gate 		off, (void *)(dcp->buf + off)));
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	return (dcp->buf + off);
11200Sstevel@tonic-gate }
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate /*
11230Sstevel@tonic-gate  * Ideally we would use the whole key to derive the hash
11240Sstevel@tonic-gate  * value. However, the probability that two keys will
11250Sstevel@tonic-gate  * have the same dip (or pip) is very low, so
11260Sstevel@tonic-gate  * hashing by dip (or pip) pointer should suffice.
11270Sstevel@tonic-gate  */
11280Sstevel@tonic-gate static uint_t
11290Sstevel@tonic-gate di_hash_byptr(void *arg, mod_hash_key_t key)
11300Sstevel@tonic-gate {
11310Sstevel@tonic-gate 	struct di_key *dik = key;
11320Sstevel@tonic-gate 	size_t rshift;
11330Sstevel@tonic-gate 	void *ptr;
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 	ASSERT(arg == NULL);
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	switch (dik->k_type) {
11380Sstevel@tonic-gate 	case DI_DKEY:
11390Sstevel@tonic-gate 		ptr = dik->k_u.dkey.dk_dip;
11400Sstevel@tonic-gate 		rshift = highbit(sizeof (struct dev_info));
11410Sstevel@tonic-gate 		break;
11420Sstevel@tonic-gate 	case DI_PKEY:
11430Sstevel@tonic-gate 		ptr = dik->k_u.pkey.pk_pip;
11440Sstevel@tonic-gate 		rshift = highbit(sizeof (struct mdi_pathinfo));
11450Sstevel@tonic-gate 		break;
11460Sstevel@tonic-gate 	default:
11470Sstevel@tonic-gate 		panic("devinfo: unknown key type");
11480Sstevel@tonic-gate 		/*NOTREACHED*/
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate 	return (mod_hash_byptr((void *)rshift, ptr));
11510Sstevel@tonic-gate }
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate static void
11540Sstevel@tonic-gate di_key_dtor(mod_hash_key_t key)
11550Sstevel@tonic-gate {
11560Sstevel@tonic-gate 	char		*path_addr;
11570Sstevel@tonic-gate 	struct di_key	*dik = key;
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	switch (dik->k_type) {
11600Sstevel@tonic-gate 	case DI_DKEY:
11610Sstevel@tonic-gate 		break;
11620Sstevel@tonic-gate 	case DI_PKEY:
11630Sstevel@tonic-gate 		path_addr = dik->k_u.pkey.pk_path_addr;
11640Sstevel@tonic-gate 		if (path_addr)
11650Sstevel@tonic-gate 			kmem_free(path_addr, strlen(path_addr) + 1);
11660Sstevel@tonic-gate 		break;
11670Sstevel@tonic-gate 	default:
11680Sstevel@tonic-gate 		panic("devinfo: unknown key type");
11690Sstevel@tonic-gate 		/*NOTREACHED*/
11700Sstevel@tonic-gate 	}
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 	kmem_free(dik, sizeof (struct di_key));
11730Sstevel@tonic-gate }
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate static int
11760Sstevel@tonic-gate di_dkey_cmp(struct di_dkey *dk1, struct di_dkey *dk2)
11770Sstevel@tonic-gate {
11780Sstevel@tonic-gate 	if (dk1->dk_dip !=  dk2->dk_dip)
11790Sstevel@tonic-gate 		return (dk1->dk_dip > dk2->dk_dip ? 1 : -1);
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	if (dk1->dk_major != -1 && dk2->dk_major != -1) {
11820Sstevel@tonic-gate 		if (dk1->dk_major !=  dk2->dk_major)
11830Sstevel@tonic-gate 			return (dk1->dk_major > dk2->dk_major ? 1 : -1);
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 		if (dk1->dk_inst !=  dk2->dk_inst)
11860Sstevel@tonic-gate 			return (dk1->dk_inst > dk2->dk_inst ? 1 : -1);
11870Sstevel@tonic-gate 	}
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	if (dk1->dk_nodeid != dk2->dk_nodeid)
11900Sstevel@tonic-gate 		return (dk1->dk_nodeid > dk2->dk_nodeid ? 1 : -1);
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	return (0);
11930Sstevel@tonic-gate }
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate static int
11960Sstevel@tonic-gate di_pkey_cmp(struct di_pkey *pk1, struct di_pkey *pk2)
11970Sstevel@tonic-gate {
11980Sstevel@tonic-gate 	char *p1, *p2;
11990Sstevel@tonic-gate 	int rv;
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 	if (pk1->pk_pip !=  pk2->pk_pip)
12020Sstevel@tonic-gate 		return (pk1->pk_pip > pk2->pk_pip ? 1 : -1);
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	p1 = pk1->pk_path_addr;
12050Sstevel@tonic-gate 	p2 = pk2->pk_path_addr;
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 	p1 = p1 ? p1 : "";
12080Sstevel@tonic-gate 	p2 = p2 ? p2 : "";
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	rv = strcmp(p1, p2);
12110Sstevel@tonic-gate 	if (rv)
12120Sstevel@tonic-gate 		return (rv > 0  ? 1 : -1);
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 	if (pk1->pk_client !=  pk2->pk_client)
12150Sstevel@tonic-gate 		return (pk1->pk_client > pk2->pk_client ? 1 : -1);
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 	if (pk1->pk_phci !=  pk2->pk_phci)
12180Sstevel@tonic-gate 		return (pk1->pk_phci > pk2->pk_phci ? 1 : -1);
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 	return (0);
12210Sstevel@tonic-gate }
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate static int
12240Sstevel@tonic-gate di_key_cmp(mod_hash_key_t key1, mod_hash_key_t key2)
12250Sstevel@tonic-gate {
12260Sstevel@tonic-gate 	struct di_key *dik1, *dik2;
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	dik1 = key1;
12290Sstevel@tonic-gate 	dik2 = key2;
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	if (dik1->k_type != dik2->k_type) {
12320Sstevel@tonic-gate 		panic("devinfo: mismatched keys");
12330Sstevel@tonic-gate 		/*NOTREACHED*/
12340Sstevel@tonic-gate 	}
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	switch (dik1->k_type) {
12370Sstevel@tonic-gate 	case DI_DKEY:
12380Sstevel@tonic-gate 		return (di_dkey_cmp(&(dik1->k_u.dkey), &(dik2->k_u.dkey)));
12390Sstevel@tonic-gate 	case DI_PKEY:
12400Sstevel@tonic-gate 		return (di_pkey_cmp(&(dik1->k_u.pkey), &(dik2->k_u.pkey)));
12410Sstevel@tonic-gate 	default:
12420Sstevel@tonic-gate 		panic("devinfo: unknown key type");
12430Sstevel@tonic-gate 		/*NOTREACHED*/
12440Sstevel@tonic-gate 	}
12450Sstevel@tonic-gate }
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate /*
12480Sstevel@tonic-gate  * This is the main function that takes a snapshot
12490Sstevel@tonic-gate  */
12500Sstevel@tonic-gate static di_off_t
12510Sstevel@tonic-gate di_snapshot(struct di_state *st)
12520Sstevel@tonic-gate {
12530Sstevel@tonic-gate 	di_off_t off;
12540Sstevel@tonic-gate 	struct di_all *all;
12550Sstevel@tonic-gate 	dev_info_t *rootnode;
12560Sstevel@tonic-gate 	char buf[80];
1257*643Scth 	int plen;
1258*643Scth 	char *path;
1259*643Scth 	vnode_t *vp;
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 	all = (struct di_all *)di_mem_addr(st, 0);
12620Sstevel@tonic-gate 	dcmn_err((CE_CONT, "Taking a snapshot of devinfo tree...\n"));
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 	/*
1265*643Scth 	 * Verify path before entrusting it to e_ddi_hold_devi_by_path because
1266*643Scth 	 * some platforms have OBP bugs where executing the NDI_PROMNAME code
1267*643Scth 	 * path against an invalid path results in panic.  The lookupnameat
1268*643Scth 	 * is done relative to rootdir without a leading '/' on "devices/"
1269*643Scth 	 * to force the lookup to occur in the global zone.
1270*643Scth 	 */
1271*643Scth 	plen = strlen("devices/") + strlen(all->root_path) + 1;
1272*643Scth 	path = kmem_alloc(plen, KM_SLEEP);
1273*643Scth 	(void) snprintf(path, plen, "devices/%s", all->root_path);
1274*643Scth 	if (lookupnameat(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp, rootdir)) {
1275*643Scth 		dcmn_err((CE_CONT, "Devinfo node %s not found\n",
1276*643Scth 		    all->root_path));
1277*643Scth 		kmem_free(path, plen);
1278*643Scth 		return (0);
1279*643Scth 	}
1280*643Scth 	kmem_free(path, plen);
1281*643Scth 	VN_RELE(vp);
1282*643Scth 
1283*643Scth 	/*
12840Sstevel@tonic-gate 	 * Hold the devinfo node referred by the path.
12850Sstevel@tonic-gate 	 */
12860Sstevel@tonic-gate 	rootnode = e_ddi_hold_devi_by_path(all->root_path, 0);
12870Sstevel@tonic-gate 	if (rootnode == NULL) {
12880Sstevel@tonic-gate 		dcmn_err((CE_CONT, "Devinfo node %s not found\n",
12890Sstevel@tonic-gate 		    all->root_path));
12900Sstevel@tonic-gate 		return (0);
12910Sstevel@tonic-gate 	}
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf),
12940Sstevel@tonic-gate 	    "devinfo registered dips (statep=%p)", (void *)st);
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 	st->reg_dip_hash = mod_hash_create_extended(buf, 64,
12970Sstevel@tonic-gate 	    di_key_dtor, mod_hash_null_valdtor, di_hash_byptr,
12980Sstevel@tonic-gate 	    NULL, di_key_cmp, KM_SLEEP);
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf),
13020Sstevel@tonic-gate 	    "devinfo registered pips (statep=%p)", (void *)st);
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 	st->reg_pip_hash = mod_hash_create_extended(buf, 64,
13050Sstevel@tonic-gate 	    di_key_dtor, mod_hash_null_valdtor, di_hash_byptr,
13060Sstevel@tonic-gate 	    NULL, di_key_cmp, KM_SLEEP);
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	/*
13090Sstevel@tonic-gate 	 * copy the device tree
13100Sstevel@tonic-gate 	 */
13110Sstevel@tonic-gate 	off = di_copytree(DEVI(rootnode), &all->top_devinfo, st);
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	ddi_release_devi(rootnode);
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 	/*
13160Sstevel@tonic-gate 	 * copy the devnames array
13170Sstevel@tonic-gate 	 */
13180Sstevel@tonic-gate 	all->devnames = off;
13190Sstevel@tonic-gate 	off = di_copydevnm(&all->devnames, st);
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	/* initialize the hash tables */
13230Sstevel@tonic-gate 	st->lnode_count = 0;
13240Sstevel@tonic-gate 	st->link_count = 0;
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	if (DINFOLYR & st->command) {
13270Sstevel@tonic-gate 		off = di_getlink_data(off, st);
13280Sstevel@tonic-gate 	}
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate 	/*
13310Sstevel@tonic-gate 	 * Free up hash tables
13320Sstevel@tonic-gate 	 */
13330Sstevel@tonic-gate 	mod_hash_destroy_hash(st->reg_dip_hash);
13340Sstevel@tonic-gate 	mod_hash_destroy_hash(st->reg_pip_hash);
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	/*
13370Sstevel@tonic-gate 	 * Record the timestamp now that we are done with snapshot.
13380Sstevel@tonic-gate 	 *
13390Sstevel@tonic-gate 	 * We compute the checksum later and then only if we cache
13400Sstevel@tonic-gate 	 * the snapshot, since checksumming adds some overhead.
13410Sstevel@tonic-gate 	 * The checksum is checked later if we read the cache file.
13420Sstevel@tonic-gate 	 * from disk.
13430Sstevel@tonic-gate 	 *
13440Sstevel@tonic-gate 	 * Set checksum field to 0 as CRC is calculated with that
13450Sstevel@tonic-gate 	 * field set to 0.
13460Sstevel@tonic-gate 	 */
13470Sstevel@tonic-gate 	all->snapshot_time = ddi_get_time();
13480Sstevel@tonic-gate 	all->cache_checksum = 0;
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 	return (off);
13510Sstevel@tonic-gate }
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate /*
13540Sstevel@tonic-gate  * Assumes all devinfo nodes in device tree have been snapshotted
13550Sstevel@tonic-gate  */
13560Sstevel@tonic-gate static void
13570Sstevel@tonic-gate snap_driver_list(struct di_state *st, struct devnames *dnp, di_off_t *poff_p)
13580Sstevel@tonic-gate {
13590Sstevel@tonic-gate 	struct dev_info *node;
13600Sstevel@tonic-gate 	struct di_node *me;
13610Sstevel@tonic-gate 	di_off_t off;
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 	node = DEVI(dnp->dn_head);
13660Sstevel@tonic-gate 	for (; node; node = node->devi_next) {
13670Sstevel@tonic-gate 		if (di_dip_find(st, (dev_info_t *)node, &off) != 0)
13680Sstevel@tonic-gate 			continue;
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 		ASSERT(off > 0);
13710Sstevel@tonic-gate 		me = (struct di_node *)di_mem_addr(st, off);
13720Sstevel@tonic-gate 		ASSERT(me->next == 0 || me->next == -1);
13730Sstevel@tonic-gate 		/*
13740Sstevel@tonic-gate 		 * Only nodes which were BOUND when they were
13750Sstevel@tonic-gate 		 * snapshotted will be added to per-driver list.
13760Sstevel@tonic-gate 		 */
13770Sstevel@tonic-gate 		if (me->next != -1)
13780Sstevel@tonic-gate 			continue;
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 		*poff_p = off;
13810Sstevel@tonic-gate 		poff_p = &me->next;
13820Sstevel@tonic-gate 	}
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 	*poff_p = 0;
13850Sstevel@tonic-gate }
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate /*
13880Sstevel@tonic-gate  * Copy the devnames array, so we have a list of drivers in the snapshot.
13890Sstevel@tonic-gate  * Also makes it possible to locate the per-driver devinfo nodes.
13900Sstevel@tonic-gate  */
13910Sstevel@tonic-gate static di_off_t
13920Sstevel@tonic-gate di_copydevnm(di_off_t *off_p, struct di_state *st)
13930Sstevel@tonic-gate {
13940Sstevel@tonic-gate 	int i;
13950Sstevel@tonic-gate 	di_off_t off;
13960Sstevel@tonic-gate 	size_t size;
13970Sstevel@tonic-gate 	struct di_devnm *dnp;
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_copydevnm: *off_p = %p\n", (void *)off_p));
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate 	/*
14020Sstevel@tonic-gate 	 * make sure there is some allocated memory
14030Sstevel@tonic-gate 	 */
14040Sstevel@tonic-gate 	size = devcnt * sizeof (struct di_devnm);
14050Sstevel@tonic-gate 	off = di_checkmem(st, *off_p, size);
14060Sstevel@tonic-gate 	*off_p = off;
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	dcmn_err((CE_CONT, "Start copying devnamesp[%d] at offset 0x%x\n",
14090Sstevel@tonic-gate 		devcnt, off));
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	dnp = (struct di_devnm *)di_mem_addr(st, off);
14120Sstevel@tonic-gate 	off += size;
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 	for (i = 0; i < devcnt; i++) {
14150Sstevel@tonic-gate 		if (devnamesp[i].dn_name == NULL) {
14160Sstevel@tonic-gate 			continue;
14170Sstevel@tonic-gate 		}
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 		/*
14200Sstevel@tonic-gate 		 * dn_name is not freed during driver unload or removal.
14210Sstevel@tonic-gate 		 *
14220Sstevel@tonic-gate 		 * There is a race condition when make_devname() changes
14230Sstevel@tonic-gate 		 * dn_name during our strcpy. This should be rare since
14240Sstevel@tonic-gate 		 * only add_drv does this. At any rate, we never had a
14250Sstevel@tonic-gate 		 * problem with ddi_name_to_major(), which should have
14260Sstevel@tonic-gate 		 * the same problem.
14270Sstevel@tonic-gate 		 */
14280Sstevel@tonic-gate 		dcmn_err2((CE_CONT, "di_copydevnm: %s%d, off=%x\n",
14290Sstevel@tonic-gate 			devnamesp[i].dn_name, devnamesp[i].dn_instance,
14300Sstevel@tonic-gate 			off));
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 		off = di_checkmem(st, off, strlen(devnamesp[i].dn_name) + 1);
14330Sstevel@tonic-gate 		dnp[i].name = off;
14340Sstevel@tonic-gate 		(void) strcpy((char *)di_mem_addr(st, off),
14350Sstevel@tonic-gate 			devnamesp[i].dn_name);
14360Sstevel@tonic-gate 		off += DI_ALIGN(strlen(devnamesp[i].dn_name) + 1);
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 		mutex_enter(&devnamesp[i].dn_lock);
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 		/*
14410Sstevel@tonic-gate 		 * Snapshot per-driver node list
14420Sstevel@tonic-gate 		 */
14430Sstevel@tonic-gate 		snap_driver_list(st, &devnamesp[i], &dnp[i].head);
14440Sstevel@tonic-gate 
14450Sstevel@tonic-gate 		/*
14460Sstevel@tonic-gate 		 * This is not used by libdevinfo, leave it for now
14470Sstevel@tonic-gate 		 */
14480Sstevel@tonic-gate 		dnp[i].flags = devnamesp[i].dn_flags;
14490Sstevel@tonic-gate 		dnp[i].instance = devnamesp[i].dn_instance;
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 		/*
14520Sstevel@tonic-gate 		 * get global properties
14530Sstevel@tonic-gate 		 */
14540Sstevel@tonic-gate 		if ((DINFOPROP & st->command) &&
14550Sstevel@tonic-gate 		    devnamesp[i].dn_global_prop_ptr) {
14560Sstevel@tonic-gate 			dnp[i].global_prop = off;
14570Sstevel@tonic-gate 			off = di_getprop(
14580Sstevel@tonic-gate 			    devnamesp[i].dn_global_prop_ptr->prop_list,
14590Sstevel@tonic-gate 			    &dnp[i].global_prop, st, NULL, DI_PROP_GLB_LIST);
14600Sstevel@tonic-gate 		}
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 		/*
14630Sstevel@tonic-gate 		 * Bit encode driver ops: & bus_ops, cb_ops, & cb_ops->cb_str
14640Sstevel@tonic-gate 		 */
14650Sstevel@tonic-gate 		if (CB_DRV_INSTALLED(devopsp[i])) {
14660Sstevel@tonic-gate 			if (devopsp[i]->devo_cb_ops) {
14670Sstevel@tonic-gate 				dnp[i].ops |= DI_CB_OPS;
14680Sstevel@tonic-gate 				if (devopsp[i]->devo_cb_ops->cb_str)
14690Sstevel@tonic-gate 					dnp[i].ops |= DI_STREAM_OPS;
14700Sstevel@tonic-gate 			}
14710Sstevel@tonic-gate 			if (NEXUS_DRV(devopsp[i])) {
14720Sstevel@tonic-gate 				dnp[i].ops |= DI_BUS_OPS;
14730Sstevel@tonic-gate 			}
14740Sstevel@tonic-gate 		}
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 		mutex_exit(&devnamesp[i].dn_lock);
14770Sstevel@tonic-gate 	}
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 	dcmn_err((CE_CONT, "End copying devnamesp at offset 0x%x\n", off));
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate 	return (off);
14820Sstevel@tonic-gate }
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate /*
14850Sstevel@tonic-gate  * Copy the kernel devinfo tree. The tree and the devnames array forms
14860Sstevel@tonic-gate  * the entire snapshot (see also di_copydevnm).
14870Sstevel@tonic-gate  */
14880Sstevel@tonic-gate static di_off_t
14890Sstevel@tonic-gate di_copytree(struct dev_info *root, di_off_t *off_p, struct di_state *st)
14900Sstevel@tonic-gate {
14910Sstevel@tonic-gate 	di_off_t off;
14920Sstevel@tonic-gate 	struct di_stack *dsp = kmem_zalloc(sizeof (struct di_stack), KM_SLEEP);
14930Sstevel@tonic-gate 
14940Sstevel@tonic-gate 	dcmn_err((CE_CONT, "di_copytree: root = %p, *off_p = %x\n",
14950Sstevel@tonic-gate 		(void *)root, *off_p));
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate 	/* force attach drivers */
14980Sstevel@tonic-gate 	if ((i_ddi_node_state((dev_info_t *)root) == DS_READY) &&
14990Sstevel@tonic-gate 	    (st->command & DINFOSUBTREE) && (st->command & DINFOFORCE)) {
15000Sstevel@tonic-gate 		(void) ndi_devi_config((dev_info_t *)root,
15010Sstevel@tonic-gate 		    NDI_CONFIG | NDI_DEVI_PERSIST | NDI_NO_EVENT |
15020Sstevel@tonic-gate 		    NDI_DRV_CONF_REPROBE);
15030Sstevel@tonic-gate 	}
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 	/*
15060Sstevel@tonic-gate 	 * Push top_devinfo onto a stack
15070Sstevel@tonic-gate 	 *
15080Sstevel@tonic-gate 	 * The stack is necessary to avoid recursion, which can overrun
15090Sstevel@tonic-gate 	 * the kernel stack.
15100Sstevel@tonic-gate 	 */
15110Sstevel@tonic-gate 	PUSH_STACK(dsp, root, off_p);
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 	/*
15140Sstevel@tonic-gate 	 * As long as there is a node on the stack, copy the node.
15150Sstevel@tonic-gate 	 * di_copynode() is responsible for pushing and popping
15160Sstevel@tonic-gate 	 * child and sibling nodes on the stack.
15170Sstevel@tonic-gate 	 */
15180Sstevel@tonic-gate 	while (!EMPTY_STACK(dsp)) {
15190Sstevel@tonic-gate 		off = di_copynode(dsp, st);
15200Sstevel@tonic-gate 	}
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 	/*
15230Sstevel@tonic-gate 	 * Free the stack structure
15240Sstevel@tonic-gate 	 */
15250Sstevel@tonic-gate 	kmem_free(dsp, sizeof (struct di_stack));
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	return (off);
15280Sstevel@tonic-gate }
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate /*
15310Sstevel@tonic-gate  * This is the core function, which copies all data associated with a single
15320Sstevel@tonic-gate  * node into the snapshot. The amount of information is determined by the
15330Sstevel@tonic-gate  * ioctl command.
15340Sstevel@tonic-gate  */
15350Sstevel@tonic-gate static di_off_t
15360Sstevel@tonic-gate di_copynode(struct di_stack *dsp, struct di_state *st)
15370Sstevel@tonic-gate {
15380Sstevel@tonic-gate 	di_off_t off;
15390Sstevel@tonic-gate 	struct di_node *me;
15400Sstevel@tonic-gate 	struct dev_info *node;
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_copynode: depth = %x\n",
15430Sstevel@tonic-gate 			dsp->depth));
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 	node = TOP_NODE(dsp);
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 	ASSERT(node != NULL);
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	/*
15500Sstevel@tonic-gate 	 * check memory usage, and fix offsets accordingly.
15510Sstevel@tonic-gate 	 */
15520Sstevel@tonic-gate 	off = di_checkmem(st, *(TOP_OFFSET(dsp)), sizeof (struct di_node));
15530Sstevel@tonic-gate 	*(TOP_OFFSET(dsp)) = off;
15540Sstevel@tonic-gate 	me = DI_NODE(di_mem_addr(st, off));
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 	dcmn_err((CE_CONT, "copy node %s, instance #%d, at offset 0x%x\n",
15570Sstevel@tonic-gate 			node->devi_node_name, node->devi_instance, off));
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 	/*
15600Sstevel@tonic-gate 	 * Node parameters:
15610Sstevel@tonic-gate 	 * self		-- offset of current node within snapshot
15620Sstevel@tonic-gate 	 * nodeid	-- pointer to PROM node (tri-valued)
15630Sstevel@tonic-gate 	 * state	-- hot plugging device state
15640Sstevel@tonic-gate 	 * node_state	-- devinfo node state (CF1, CF2, etc.)
15650Sstevel@tonic-gate 	 */
15660Sstevel@tonic-gate 	me->self = off;
15670Sstevel@tonic-gate 	me->instance = node->devi_instance;
15680Sstevel@tonic-gate 	me->nodeid = node->devi_nodeid;
15690Sstevel@tonic-gate 	me->node_class = node->devi_node_class;
15700Sstevel@tonic-gate 	me->attributes = node->devi_node_attributes;
15710Sstevel@tonic-gate 	me->state = node->devi_state;
15720Sstevel@tonic-gate 	me->node_state = node->devi_node_state;
15730Sstevel@tonic-gate 	me->user_private_data = NULL;
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 	/*
15760Sstevel@tonic-gate 	 * Get parent's offset in snapshot from the stack
15770Sstevel@tonic-gate 	 * and store it in the current node
15780Sstevel@tonic-gate 	 */
15790Sstevel@tonic-gate 	if (dsp->depth > 1) {
15800Sstevel@tonic-gate 		me->parent = *(PARENT_OFFSET(dsp));
15810Sstevel@tonic-gate 	}
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate 	/*
15840Sstevel@tonic-gate 	 * Save the offset of this di_node in a hash table.
15850Sstevel@tonic-gate 	 * This is used later to resolve references to this
15860Sstevel@tonic-gate 	 * dip from other parts of the tree (per-driver list,
15870Sstevel@tonic-gate 	 * multipathing linkages, layered usage linkages).
15880Sstevel@tonic-gate 	 * The key used for the hash table is derived from
15890Sstevel@tonic-gate 	 * information in the dip.
15900Sstevel@tonic-gate 	 */
15910Sstevel@tonic-gate 	di_register_dip(st, (dev_info_t *)node, me->self);
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 	/*
15940Sstevel@tonic-gate 	 * increment offset
15950Sstevel@tonic-gate 	 */
15960Sstevel@tonic-gate 	off += sizeof (struct di_node);
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate #ifdef	DEVID_COMPATIBILITY
15990Sstevel@tonic-gate 	/* check for devid as property marker */
16000Sstevel@tonic-gate 	if (node->devi_devid) {
16010Sstevel@tonic-gate 		ddi_devid_t	devid;
16020Sstevel@tonic-gate 		char 		*devidstr;
16030Sstevel@tonic-gate 		int		devid_size;
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate 		/*
16060Sstevel@tonic-gate 		 * The devid is now represented as a property.
16070Sstevel@tonic-gate 		 * For micro release compatibility with di_devid interface
16080Sstevel@tonic-gate 		 * in libdevinfo we must return it as a binary structure in'
16090Sstevel@tonic-gate 		 * the snapshot.  When di_devid is removed from libdevinfo
16100Sstevel@tonic-gate 		 * in a future release (and devi_devid is deleted) then
16110Sstevel@tonic-gate 		 * code related to DEVID_COMPATIBILITY can be removed.
16120Sstevel@tonic-gate 		 */
16130Sstevel@tonic-gate 		ASSERT(node->devi_devid == DEVID_COMPATIBILITY);
16140Sstevel@tonic-gate /* XXX should be DDI_DEV_T_NONE! */
16150Sstevel@tonic-gate 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, (dev_info_t *)node,
16160Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, DEVID_PROP_NAME, &devidstr) ==
16170Sstevel@tonic-gate 		    DDI_PROP_SUCCESS) {
16180Sstevel@tonic-gate 			if (ddi_devid_str_decode(devidstr, &devid, NULL) ==
16190Sstevel@tonic-gate 			    DDI_SUCCESS) {
16200Sstevel@tonic-gate 				devid_size = ddi_devid_sizeof(devid);
16210Sstevel@tonic-gate 				off = di_checkmem(st, off, devid_size);
16220Sstevel@tonic-gate 				me->devid = off;
16230Sstevel@tonic-gate 				bcopy(devid,
16240Sstevel@tonic-gate 				    di_mem_addr(st, off), devid_size);
16250Sstevel@tonic-gate 				off += devid_size;
16260Sstevel@tonic-gate 				ddi_devid_free(devid);
16270Sstevel@tonic-gate 			}
16280Sstevel@tonic-gate 			ddi_prop_free(devidstr);
16290Sstevel@tonic-gate 		}
16300Sstevel@tonic-gate 	}
16310Sstevel@tonic-gate #endif	/* DEVID_COMPATIBILITY */
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 	if (node->devi_node_name) {
16340Sstevel@tonic-gate 		off = di_checkmem(st, off, strlen(node->devi_node_name) + 1);
16350Sstevel@tonic-gate 		me->node_name = off;
16360Sstevel@tonic-gate 		(void) strcpy(di_mem_addr(st, off), node->devi_node_name);
16370Sstevel@tonic-gate 		off += strlen(node->devi_node_name) + 1;
16380Sstevel@tonic-gate 	}
16390Sstevel@tonic-gate 
16400Sstevel@tonic-gate 	if (node->devi_compat_names && (node->devi_compat_length > 1)) {
16410Sstevel@tonic-gate 		off = di_checkmem(st, off, node->devi_compat_length);
16420Sstevel@tonic-gate 		me->compat_names = off;
16430Sstevel@tonic-gate 		me->compat_length = node->devi_compat_length;
16440Sstevel@tonic-gate 		bcopy(node->devi_compat_names, di_mem_addr(st, off),
16450Sstevel@tonic-gate 			node->devi_compat_length);
16460Sstevel@tonic-gate 		off += node->devi_compat_length;
16470Sstevel@tonic-gate 	}
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate 	if (node->devi_addr) {
16500Sstevel@tonic-gate 		off = di_checkmem(st, off, strlen(node->devi_addr) + 1);
16510Sstevel@tonic-gate 		me->address = off;
16520Sstevel@tonic-gate 		(void) strcpy(di_mem_addr(st, off), node->devi_addr);
16530Sstevel@tonic-gate 		off += strlen(node->devi_addr) + 1;
16540Sstevel@tonic-gate 	}
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate 	if (node->devi_binding_name) {
16570Sstevel@tonic-gate 		off = di_checkmem(st, off, strlen(node->devi_binding_name) + 1);
16580Sstevel@tonic-gate 		me->bind_name = off;
16590Sstevel@tonic-gate 		(void) strcpy(di_mem_addr(st, off), node->devi_binding_name);
16600Sstevel@tonic-gate 		off += strlen(node->devi_binding_name) + 1;
16610Sstevel@tonic-gate 	}
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 	me->drv_major = node->devi_major;
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 	/*
16660Sstevel@tonic-gate 	 * If the dip is BOUND, set the next pointer of the
16670Sstevel@tonic-gate 	 * per-instance list to -1, indicating that it is yet to be resolved.
16680Sstevel@tonic-gate 	 * This will be resolved later in snap_driver_list().
16690Sstevel@tonic-gate 	 */
16700Sstevel@tonic-gate 	if (me->drv_major != -1) {
16710Sstevel@tonic-gate 		me->next = -1;
16720Sstevel@tonic-gate 	} else {
16730Sstevel@tonic-gate 		me->next = 0;
16740Sstevel@tonic-gate 	}
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate 	/*
16770Sstevel@tonic-gate 	 * An optimization to skip mutex_enter when not needed.
16780Sstevel@tonic-gate 	 */
16790Sstevel@tonic-gate 	if (!((DINFOMINOR | DINFOPROP | DINFOPATH) & st->command)) {
16800Sstevel@tonic-gate 		goto priv_data;
16810Sstevel@tonic-gate 	}
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 	/*
16840Sstevel@tonic-gate 	 * Grab current per dev_info node lock to
16850Sstevel@tonic-gate 	 * get minor data and properties.
16860Sstevel@tonic-gate 	 */
16870Sstevel@tonic-gate 	mutex_enter(&(node->devi_lock));
16880Sstevel@tonic-gate 
16890Sstevel@tonic-gate 	if (!(DINFOMINOR & st->command)) {
16900Sstevel@tonic-gate 		goto path;
16910Sstevel@tonic-gate 	}
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 	if (node->devi_minor) {		/* minor data */
16940Sstevel@tonic-gate 		me->minor_data = DI_ALIGN(off);
16950Sstevel@tonic-gate 		off = di_getmdata(node->devi_minor, &me->minor_data,
16960Sstevel@tonic-gate 		    me->self, st);
16970Sstevel@tonic-gate 	}
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate path:
17000Sstevel@tonic-gate 	if (!(DINFOPATH & st->command)) {
17010Sstevel@tonic-gate 		goto property;
17020Sstevel@tonic-gate 	}
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 	if (MDI_CLIENT(node)) {
17050Sstevel@tonic-gate 		me->multipath_client = DI_ALIGN(off);
17060Sstevel@tonic-gate 		off = di_getpath_data((dev_info_t *)node, &me->multipath_client,
17070Sstevel@tonic-gate 		    me->self, st, 1);
17080Sstevel@tonic-gate 		dcmn_err((CE_WARN, "me->multipath_client = %x for node %p "
17090Sstevel@tonic-gate 		    "component type = %d.  off=%d",
17100Sstevel@tonic-gate 		    me->multipath_client,
17110Sstevel@tonic-gate 		    (void *)node, node->devi_mdi_component, off));
17120Sstevel@tonic-gate 	}
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 	if (MDI_PHCI(node)) {
17150Sstevel@tonic-gate 		me->multipath_phci = DI_ALIGN(off);
17160Sstevel@tonic-gate 		off = di_getpath_data((dev_info_t *)node, &me->multipath_phci,
17170Sstevel@tonic-gate 		    me->self, st, 0);
17180Sstevel@tonic-gate 		dcmn_err((CE_WARN, "me->multipath_phci = %x for node %p "
17190Sstevel@tonic-gate 		    "component type = %d.  off=%d",
17200Sstevel@tonic-gate 		    me->multipath_phci,
17210Sstevel@tonic-gate 		    (void *)node, node->devi_mdi_component, off));
17220Sstevel@tonic-gate 	}
17230Sstevel@tonic-gate 
17240Sstevel@tonic-gate property:
17250Sstevel@tonic-gate 	if (!(DINFOPROP & st->command)) {
17260Sstevel@tonic-gate 		goto unlock;
17270Sstevel@tonic-gate 	}
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	if (node->devi_drv_prop_ptr) {	/* driver property list */
17300Sstevel@tonic-gate 		me->drv_prop = DI_ALIGN(off);
17310Sstevel@tonic-gate 		off = di_getprop(node->devi_drv_prop_ptr, &me->drv_prop, st,
17320Sstevel@tonic-gate 			node, DI_PROP_DRV_LIST);
17330Sstevel@tonic-gate 	}
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 	if (node->devi_sys_prop_ptr) {	/* system property list */
17360Sstevel@tonic-gate 		me->sys_prop = DI_ALIGN(off);
17370Sstevel@tonic-gate 		off = di_getprop(node->devi_sys_prop_ptr, &me->sys_prop, st,
17380Sstevel@tonic-gate 			node, DI_PROP_SYS_LIST);
17390Sstevel@tonic-gate 	}
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate 	if (node->devi_hw_prop_ptr) {	/* hardware property list */
17420Sstevel@tonic-gate 		me->hw_prop = DI_ALIGN(off);
17430Sstevel@tonic-gate 		off = di_getprop(node->devi_hw_prop_ptr, &me->hw_prop, st,
17440Sstevel@tonic-gate 			node, DI_PROP_HW_LIST);
17450Sstevel@tonic-gate 	}
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate 	if (node->devi_global_prop_list == NULL) {
17480Sstevel@tonic-gate 		me->glob_prop = (di_off_t)-1;	/* not global property */
17490Sstevel@tonic-gate 	} else {
17500Sstevel@tonic-gate 		/*
17510Sstevel@tonic-gate 		 * Make copy of global property list if this devinfo refers
17520Sstevel@tonic-gate 		 * global properties different from what's on the devnames
17530Sstevel@tonic-gate 		 * array. It can happen if there has been a forced
17540Sstevel@tonic-gate 		 * driver.conf update. See mod_drv(1M).
17550Sstevel@tonic-gate 		 */
17560Sstevel@tonic-gate 		ASSERT(me->drv_major != -1);
17570Sstevel@tonic-gate 		if (node->devi_global_prop_list !=
17580Sstevel@tonic-gate 		    devnamesp[me->drv_major].dn_global_prop_ptr) {
17590Sstevel@tonic-gate 			me->glob_prop = DI_ALIGN(off);
17600Sstevel@tonic-gate 			off = di_getprop(node->devi_global_prop_list->prop_list,
17610Sstevel@tonic-gate 			    &me->glob_prop, st, node, DI_PROP_GLB_LIST);
17620Sstevel@tonic-gate 		}
17630Sstevel@tonic-gate 	}
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate unlock:
17660Sstevel@tonic-gate 	/*
17670Sstevel@tonic-gate 	 * release current per dev_info node lock
17680Sstevel@tonic-gate 	 */
17690Sstevel@tonic-gate 	mutex_exit(&(node->devi_lock));
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate priv_data:
17720Sstevel@tonic-gate 	if (!(DINFOPRIVDATA & st->command)) {
17730Sstevel@tonic-gate 		goto pm_info;
17740Sstevel@tonic-gate 	}
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 	if (ddi_get_parent_data((dev_info_t *)node) != NULL) {
17770Sstevel@tonic-gate 		me->parent_data = DI_ALIGN(off);
17780Sstevel@tonic-gate 		off = di_getppdata(node, &me->parent_data, st);
17790Sstevel@tonic-gate 	}
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate 	if (ddi_get_driver_private((dev_info_t *)node) != NULL) {
17820Sstevel@tonic-gate 		me->driver_data = DI_ALIGN(off);
17830Sstevel@tonic-gate 		off = di_getdpdata(node, &me->driver_data, st);
17840Sstevel@tonic-gate 	}
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate pm_info: /* NOT implemented */
17870Sstevel@tonic-gate 
17880Sstevel@tonic-gate subtree:
17890Sstevel@tonic-gate 	if (!(DINFOSUBTREE & st->command)) {
17900Sstevel@tonic-gate 		POP_STACK(dsp);
17910Sstevel@tonic-gate 		return (DI_ALIGN(off));
17920Sstevel@tonic-gate 	}
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate child:
17950Sstevel@tonic-gate 	/*
17960Sstevel@tonic-gate 	 * If there is a child--push child onto stack.
17970Sstevel@tonic-gate 	 * Hold the parent busy while doing so.
17980Sstevel@tonic-gate 	 */
17990Sstevel@tonic-gate 	if (node->devi_child) {
18000Sstevel@tonic-gate 		me->child = DI_ALIGN(off);
18010Sstevel@tonic-gate 		PUSH_STACK(dsp, node->devi_child, &me->child);
18020Sstevel@tonic-gate 		return (me->child);
18030Sstevel@tonic-gate 	}
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate sibling:
18060Sstevel@tonic-gate 	/*
18070Sstevel@tonic-gate 	 * no child node, unroll the stack till a sibling of
18080Sstevel@tonic-gate 	 * a parent node is found or root node is reached
18090Sstevel@tonic-gate 	 */
18100Sstevel@tonic-gate 	POP_STACK(dsp);
18110Sstevel@tonic-gate 	while (!EMPTY_STACK(dsp) && (node->devi_sibling == NULL)) {
18120Sstevel@tonic-gate 		node = TOP_NODE(dsp);
18130Sstevel@tonic-gate 		me = DI_NODE(di_mem_addr(st, *(TOP_OFFSET(dsp))));
18140Sstevel@tonic-gate 		POP_STACK(dsp);
18150Sstevel@tonic-gate 	}
18160Sstevel@tonic-gate 
18170Sstevel@tonic-gate 	if (!EMPTY_STACK(dsp)) {
18180Sstevel@tonic-gate 		/*
18190Sstevel@tonic-gate 		 * a sibling is found, replace top of stack by its sibling
18200Sstevel@tonic-gate 		 */
18210Sstevel@tonic-gate 		me->sibling = DI_ALIGN(off);
18220Sstevel@tonic-gate 		PUSH_STACK(dsp, node->devi_sibling, &me->sibling);
18230Sstevel@tonic-gate 		return (me->sibling);
18240Sstevel@tonic-gate 	}
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate 	/*
18270Sstevel@tonic-gate 	 * DONE with all nodes
18280Sstevel@tonic-gate 	 */
18290Sstevel@tonic-gate 	return (DI_ALIGN(off));
18300Sstevel@tonic-gate }
18310Sstevel@tonic-gate 
18320Sstevel@tonic-gate static i_lnode_t *
18330Sstevel@tonic-gate i_lnode_alloc(int modid)
18340Sstevel@tonic-gate {
18350Sstevel@tonic-gate 	i_lnode_t	*i_lnode;
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate 	i_lnode = kmem_zalloc(sizeof (i_lnode_t), KM_SLEEP);
18380Sstevel@tonic-gate 
18390Sstevel@tonic-gate 	ASSERT(modid != -1);
18400Sstevel@tonic-gate 	i_lnode->modid = modid;
18410Sstevel@tonic-gate 
18420Sstevel@tonic-gate 	return (i_lnode);
18430Sstevel@tonic-gate }
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate static void
18460Sstevel@tonic-gate i_lnode_free(i_lnode_t *i_lnode)
18470Sstevel@tonic-gate {
18480Sstevel@tonic-gate 	kmem_free(i_lnode, sizeof (i_lnode_t));
18490Sstevel@tonic-gate }
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate static void
18520Sstevel@tonic-gate i_lnode_check_free(i_lnode_t *i_lnode)
18530Sstevel@tonic-gate {
18540Sstevel@tonic-gate 	/* This lnode and its dip must have been snapshotted */
18550Sstevel@tonic-gate 	ASSERT(i_lnode->self > 0);
18560Sstevel@tonic-gate 	ASSERT(i_lnode->di_node->self > 0);
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate 	/* at least 1 link (in or out) must exist for this lnode */
18590Sstevel@tonic-gate 	ASSERT(i_lnode->link_in || i_lnode->link_out);
18600Sstevel@tonic-gate 
18610Sstevel@tonic-gate 	i_lnode_free(i_lnode);
18620Sstevel@tonic-gate }
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate static i_link_t *
18650Sstevel@tonic-gate i_link_alloc(int spec_type)
18660Sstevel@tonic-gate {
18670Sstevel@tonic-gate 	i_link_t *i_link;
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate 	i_link = kmem_zalloc(sizeof (i_link_t), KM_SLEEP);
18700Sstevel@tonic-gate 	i_link->spec_type = spec_type;
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate 	return (i_link);
18730Sstevel@tonic-gate }
18740Sstevel@tonic-gate 
18750Sstevel@tonic-gate static void
18760Sstevel@tonic-gate i_link_check_free(i_link_t *i_link)
18770Sstevel@tonic-gate {
18780Sstevel@tonic-gate 	/* This link must have been snapshotted */
18790Sstevel@tonic-gate 	ASSERT(i_link->self > 0);
18800Sstevel@tonic-gate 
18810Sstevel@tonic-gate 	/* Both endpoint lnodes must exist for this link */
18820Sstevel@tonic-gate 	ASSERT(i_link->src_lnode);
18830Sstevel@tonic-gate 	ASSERT(i_link->tgt_lnode);
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate 	kmem_free(i_link, sizeof (i_link_t));
18860Sstevel@tonic-gate }
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate /*ARGSUSED*/
18890Sstevel@tonic-gate static uint_t
18900Sstevel@tonic-gate i_lnode_hashfunc(void *arg, mod_hash_key_t key)
18910Sstevel@tonic-gate {
18920Sstevel@tonic-gate 	i_lnode_t	*i_lnode = (i_lnode_t *)key;
18930Sstevel@tonic-gate 	struct di_node	*ptr;
18940Sstevel@tonic-gate 	dev_t		dev;
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 	dev = i_lnode->devt;
18970Sstevel@tonic-gate 	if (dev != DDI_DEV_T_NONE)
18980Sstevel@tonic-gate 		return (i_lnode->modid + getminor(dev) + getmajor(dev));
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate 	ptr = i_lnode->di_node;
19010Sstevel@tonic-gate 	ASSERT(ptr->self > 0);
19020Sstevel@tonic-gate 	if (ptr) {
19030Sstevel@tonic-gate 		uintptr_t k = (uintptr_t)ptr;
19040Sstevel@tonic-gate 		k >>= (int)highbit(sizeof (struct di_node));
19050Sstevel@tonic-gate 		return ((uint_t)k);
19060Sstevel@tonic-gate 	}
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate 	return (i_lnode->modid);
19090Sstevel@tonic-gate }
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate static int
19120Sstevel@tonic-gate i_lnode_cmp(void *arg1, void *arg2)
19130Sstevel@tonic-gate {
19140Sstevel@tonic-gate 	i_lnode_t	*i_lnode1 = (i_lnode_t *)arg1;
19150Sstevel@tonic-gate 	i_lnode_t	*i_lnode2 = (i_lnode_t *)arg2;
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 	if (i_lnode1->modid != i_lnode2->modid) {
19180Sstevel@tonic-gate 		return ((i_lnode1->modid < i_lnode2->modid) ? -1 : 1);
19190Sstevel@tonic-gate 	}
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 	if (i_lnode1->di_node != i_lnode2->di_node)
19220Sstevel@tonic-gate 		return ((i_lnode1->di_node < i_lnode2->di_node) ? -1 : 1);
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate 	if (i_lnode1->devt != i_lnode2->devt)
19250Sstevel@tonic-gate 		return ((i_lnode1->devt < i_lnode2->devt) ? -1 : 1);
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate 	return (0);
19280Sstevel@tonic-gate }
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate /*
19310Sstevel@tonic-gate  * An lnode represents a {dip, dev_t} tuple. A link represents a
19320Sstevel@tonic-gate  * {src_lnode, tgt_lnode, spec_type} tuple.
19330Sstevel@tonic-gate  * The following callback assumes that LDI framework ref-counts the
19340Sstevel@tonic-gate  * src_dip and tgt_dip while invoking this callback.
19350Sstevel@tonic-gate  */
19360Sstevel@tonic-gate static int
19370Sstevel@tonic-gate di_ldi_callback(const ldi_usage_t *ldi_usage, void *arg)
19380Sstevel@tonic-gate {
19390Sstevel@tonic-gate 	struct di_state	*st = (struct di_state *)arg;
19400Sstevel@tonic-gate 	i_lnode_t	*src_lnode, *tgt_lnode, *i_lnode;
19410Sstevel@tonic-gate 	i_link_t	**i_link_next, *i_link;
19420Sstevel@tonic-gate 	di_off_t	soff, toff;
19430Sstevel@tonic-gate 	mod_hash_val_t	nodep = NULL;
19440Sstevel@tonic-gate 	int		res;
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate 	/*
19470Sstevel@tonic-gate 	 * if the source or target of this device usage information doesn't
19480Sstevel@tonic-gate 	 * corrospond to a device node then we don't report it via
19490Sstevel@tonic-gate 	 * libdevinfo so return.
19500Sstevel@tonic-gate 	 */
19510Sstevel@tonic-gate 	if ((ldi_usage->src_dip == NULL) || (ldi_usage->tgt_dip == NULL))
19520Sstevel@tonic-gate 		return (LDI_USAGE_CONTINUE);
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate 	ASSERT(e_ddi_devi_holdcnt(ldi_usage->src_dip));
19550Sstevel@tonic-gate 	ASSERT(e_ddi_devi_holdcnt(ldi_usage->tgt_dip));
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate 	/*
19580Sstevel@tonic-gate 	 * Skip the ldi_usage if either src or tgt dip is not in the
19590Sstevel@tonic-gate 	 * snapshot. This saves us from pruning bad lnodes/links later.
19600Sstevel@tonic-gate 	 */
19610Sstevel@tonic-gate 	if (di_dip_find(st, ldi_usage->src_dip, &soff) != 0)
19620Sstevel@tonic-gate 		return (LDI_USAGE_CONTINUE);
19630Sstevel@tonic-gate 	if (di_dip_find(st, ldi_usage->tgt_dip, &toff) != 0)
19640Sstevel@tonic-gate 		return (LDI_USAGE_CONTINUE);
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate 	ASSERT(soff > 0);
19670Sstevel@tonic-gate 	ASSERT(toff > 0);
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate 	/*
19700Sstevel@tonic-gate 	 * allocate an i_lnode and add it to the lnode hash
19710Sstevel@tonic-gate 	 * if it is not already present. For this particular
19720Sstevel@tonic-gate 	 * link the lnode is a source, but it may
19730Sstevel@tonic-gate 	 * participate as tgt or src in any number of layered
19740Sstevel@tonic-gate 	 * operations - so it may already be in the hash.
19750Sstevel@tonic-gate 	 */
19760Sstevel@tonic-gate 	i_lnode = i_lnode_alloc(ldi_usage->src_modid);
19770Sstevel@tonic-gate 	i_lnode->di_node = (struct di_node *)di_mem_addr(st, soff);
19780Sstevel@tonic-gate 	i_lnode->devt = ldi_usage->src_devt;
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate 	res = mod_hash_find(st->lnode_hash, i_lnode, &nodep);
19810Sstevel@tonic-gate 	if (res == MH_ERR_NOTFOUND) {
19820Sstevel@tonic-gate 		/*
19830Sstevel@tonic-gate 		 * new i_lnode
19840Sstevel@tonic-gate 		 * add it to the hash and increment the lnode count
19850Sstevel@tonic-gate 		 */
19860Sstevel@tonic-gate 		res = mod_hash_insert(st->lnode_hash, i_lnode, i_lnode);
19870Sstevel@tonic-gate 		ASSERT(res == 0);
19880Sstevel@tonic-gate 		st->lnode_count++;
19890Sstevel@tonic-gate 		src_lnode = i_lnode;
19900Sstevel@tonic-gate 	} else {
19910Sstevel@tonic-gate 		/* this i_lnode already exists in the lnode_hash */
19920Sstevel@tonic-gate 		i_lnode_free(i_lnode);
19930Sstevel@tonic-gate 		src_lnode = (i_lnode_t *)nodep;
19940Sstevel@tonic-gate 	}
19950Sstevel@tonic-gate 
19960Sstevel@tonic-gate 	/*
19970Sstevel@tonic-gate 	 * allocate a tgt i_lnode and add it to the lnode hash
19980Sstevel@tonic-gate 	 */
19990Sstevel@tonic-gate 	i_lnode = i_lnode_alloc(ldi_usage->tgt_modid);
20000Sstevel@tonic-gate 	i_lnode->di_node = (struct di_node *)di_mem_addr(st, toff);
20010Sstevel@tonic-gate 	i_lnode->devt = ldi_usage->tgt_devt;
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 	res = mod_hash_find(st->lnode_hash, i_lnode, &nodep);
20040Sstevel@tonic-gate 	if (res == MH_ERR_NOTFOUND) {
20050Sstevel@tonic-gate 		/*
20060Sstevel@tonic-gate 		 * new i_lnode
20070Sstevel@tonic-gate 		 * add it to the hash and increment the lnode count
20080Sstevel@tonic-gate 		 */
20090Sstevel@tonic-gate 		res = mod_hash_insert(st->lnode_hash, i_lnode, i_lnode);
20100Sstevel@tonic-gate 		ASSERT(res == 0);
20110Sstevel@tonic-gate 		st->lnode_count++;
20120Sstevel@tonic-gate 		tgt_lnode = i_lnode;
20130Sstevel@tonic-gate 	} else {
20140Sstevel@tonic-gate 		/* this i_lnode already exists in the lnode_hash */
20150Sstevel@tonic-gate 		i_lnode_free(i_lnode);
20160Sstevel@tonic-gate 		tgt_lnode = (i_lnode_t *)nodep;
20170Sstevel@tonic-gate 	}
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 	/*
20200Sstevel@tonic-gate 	 * allocate a i_link
20210Sstevel@tonic-gate 	 */
20220Sstevel@tonic-gate 	i_link = i_link_alloc(ldi_usage->tgt_spec_type);
20230Sstevel@tonic-gate 	i_link->src_lnode = src_lnode;
20240Sstevel@tonic-gate 	i_link->tgt_lnode = tgt_lnode;
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate 	/*
20270Sstevel@tonic-gate 	 * add this link onto the src i_lnodes outbound i_link list
20280Sstevel@tonic-gate 	 */
20290Sstevel@tonic-gate 	i_link_next = &(src_lnode->link_out);
20300Sstevel@tonic-gate 	while (*i_link_next != NULL) {
20310Sstevel@tonic-gate 		if ((i_lnode_cmp(tgt_lnode, (*i_link_next)->tgt_lnode) == 0) &&
20320Sstevel@tonic-gate 		    (i_link->spec_type == (*i_link_next)->spec_type)) {
20330Sstevel@tonic-gate 			/* this link already exists */
20340Sstevel@tonic-gate 			kmem_free(i_link, sizeof (i_link_t));
20350Sstevel@tonic-gate 			return (LDI_USAGE_CONTINUE);
20360Sstevel@tonic-gate 		}
20370Sstevel@tonic-gate 		i_link_next = &((*i_link_next)->src_link_next);
20380Sstevel@tonic-gate 	}
20390Sstevel@tonic-gate 	*i_link_next = i_link;
20400Sstevel@tonic-gate 
20410Sstevel@tonic-gate 	/*
20420Sstevel@tonic-gate 	 * add this link onto the tgt i_lnodes inbound i_link list
20430Sstevel@tonic-gate 	 */
20440Sstevel@tonic-gate 	i_link_next = &(tgt_lnode->link_in);
20450Sstevel@tonic-gate 	while (*i_link_next != NULL) {
20460Sstevel@tonic-gate 		ASSERT(i_lnode_cmp(src_lnode, (*i_link_next)->src_lnode) != 0);
20470Sstevel@tonic-gate 		i_link_next = &((*i_link_next)->tgt_link_next);
20480Sstevel@tonic-gate 	}
20490Sstevel@tonic-gate 	*i_link_next = i_link;
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate 	/*
20520Sstevel@tonic-gate 	 * add this i_link to the link hash
20530Sstevel@tonic-gate 	 */
20540Sstevel@tonic-gate 	res = mod_hash_insert(st->link_hash, i_link, i_link);
20550Sstevel@tonic-gate 	ASSERT(res == 0);
20560Sstevel@tonic-gate 	st->link_count++;
20570Sstevel@tonic-gate 
20580Sstevel@tonic-gate 	return (LDI_USAGE_CONTINUE);
20590Sstevel@tonic-gate }
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate struct i_layer_data {
20620Sstevel@tonic-gate 	struct di_state	*st;
20630Sstevel@tonic-gate 	int		lnode_count;
20640Sstevel@tonic-gate 	int		link_count;
20650Sstevel@tonic-gate 	di_off_t	lnode_off;
20660Sstevel@tonic-gate 	di_off_t 	link_off;
20670Sstevel@tonic-gate };
20680Sstevel@tonic-gate 
20690Sstevel@tonic-gate /*ARGSUSED*/
20700Sstevel@tonic-gate static uint_t
20710Sstevel@tonic-gate i_link_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
20720Sstevel@tonic-gate {
20730Sstevel@tonic-gate 	i_link_t		*i_link  = (i_link_t *)key;
20740Sstevel@tonic-gate 	struct i_layer_data	*data = arg;
20750Sstevel@tonic-gate 	struct di_link		*me;
20760Sstevel@tonic-gate 	struct di_lnode		*melnode;
20770Sstevel@tonic-gate 	struct di_node		*medinode;
20780Sstevel@tonic-gate 
20790Sstevel@tonic-gate 	ASSERT(i_link->self == 0);
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate 	i_link->self = data->link_off +
20820Sstevel@tonic-gate 	    (data->link_count * sizeof (struct di_link));
20830Sstevel@tonic-gate 	data->link_count++;
20840Sstevel@tonic-gate 
20850Sstevel@tonic-gate 	ASSERT(data->link_off > 0 && data->link_count > 0);
20860Sstevel@tonic-gate 	ASSERT(data->lnode_count == data->st->lnode_count); /* lnodes done */
20870Sstevel@tonic-gate 	ASSERT(data->link_count <= data->st->link_count);
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate 	/* fill in fields for the di_link snapshot */
20900Sstevel@tonic-gate 	me = (struct di_link *)di_mem_addr(data->st, i_link->self);
20910Sstevel@tonic-gate 	me->self = i_link->self;
20920Sstevel@tonic-gate 	me->spec_type = i_link->spec_type;
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate 	/*
20950Sstevel@tonic-gate 	 * The src_lnode and tgt_lnode i_lnode_t for this i_link_t
20960Sstevel@tonic-gate 	 * are created during the LDI table walk. Since we are
20970Sstevel@tonic-gate 	 * walking the link hash, the lnode hash has already been
20980Sstevel@tonic-gate 	 * walked and the lnodes have been snapshotted. Save lnode
20990Sstevel@tonic-gate 	 * offsets.
21000Sstevel@tonic-gate 	 */
21010Sstevel@tonic-gate 	me->src_lnode = i_link->src_lnode->self;
21020Sstevel@tonic-gate 	me->tgt_lnode = i_link->tgt_lnode->self;
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate 	/*
21050Sstevel@tonic-gate 	 * Save this link's offset in the src_lnode snapshot's link_out
21060Sstevel@tonic-gate 	 * field
21070Sstevel@tonic-gate 	 */
21080Sstevel@tonic-gate 	melnode = (struct di_lnode *)di_mem_addr(data->st, me->src_lnode);
21090Sstevel@tonic-gate 	me->src_link_next = melnode->link_out;
21100Sstevel@tonic-gate 	melnode->link_out = me->self;
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate 	/*
21130Sstevel@tonic-gate 	 * Put this link on the tgt_lnode's link_in field
21140Sstevel@tonic-gate 	 */
21150Sstevel@tonic-gate 	melnode = (struct di_lnode *)di_mem_addr(data->st, me->tgt_lnode);
21160Sstevel@tonic-gate 	me->tgt_link_next = melnode->link_in;
21170Sstevel@tonic-gate 	melnode->link_in = me->self;
21180Sstevel@tonic-gate 
21190Sstevel@tonic-gate 	/*
21200Sstevel@tonic-gate 	 * An i_lnode_t is only created if the corresponding dip exists
21210Sstevel@tonic-gate 	 * in the snapshot. A pointer to the di_node is saved in the
21220Sstevel@tonic-gate 	 * i_lnode_t when it is allocated. For this link, get the di_node
21230Sstevel@tonic-gate 	 * for the source lnode. Then put the link on the di_node's list
21240Sstevel@tonic-gate 	 * of src links
21250Sstevel@tonic-gate 	 */
21260Sstevel@tonic-gate 	medinode = i_link->src_lnode->di_node;
21270Sstevel@tonic-gate 	me->src_node_next = medinode->src_links;
21280Sstevel@tonic-gate 	medinode->src_links = me->self;
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 	/*
21310Sstevel@tonic-gate 	 * Put this link on the tgt_links list of the target
21320Sstevel@tonic-gate 	 * dip.
21330Sstevel@tonic-gate 	 */
21340Sstevel@tonic-gate 	medinode = i_link->tgt_lnode->di_node;
21350Sstevel@tonic-gate 	me->tgt_node_next = medinode->tgt_links;
21360Sstevel@tonic-gate 	medinode->tgt_links = me->self;
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate 	return (MH_WALK_CONTINUE);
21390Sstevel@tonic-gate }
21400Sstevel@tonic-gate 
21410Sstevel@tonic-gate /*ARGSUSED*/
21420Sstevel@tonic-gate static uint_t
21430Sstevel@tonic-gate i_lnode_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
21440Sstevel@tonic-gate {
21450Sstevel@tonic-gate 	i_lnode_t		*i_lnode = (i_lnode_t *)key;
21460Sstevel@tonic-gate 	struct i_layer_data	*data = arg;
21470Sstevel@tonic-gate 	struct di_lnode		*me;
21480Sstevel@tonic-gate 	struct di_node		*medinode;
21490Sstevel@tonic-gate 
21500Sstevel@tonic-gate 	ASSERT(i_lnode->self == 0);
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate 	i_lnode->self = data->lnode_off +
21530Sstevel@tonic-gate 	    (data->lnode_count * sizeof (struct di_lnode));
21540Sstevel@tonic-gate 	data->lnode_count++;
21550Sstevel@tonic-gate 
21560Sstevel@tonic-gate 	ASSERT(data->lnode_off > 0 && data->lnode_count > 0);
21570Sstevel@tonic-gate 	ASSERT(data->link_count == 0); /* links not done yet */
21580Sstevel@tonic-gate 	ASSERT(data->lnode_count <= data->st->lnode_count);
21590Sstevel@tonic-gate 
21600Sstevel@tonic-gate 	/* fill in fields for the di_lnode snapshot */
21610Sstevel@tonic-gate 	me = (struct di_lnode *)di_mem_addr(data->st, i_lnode->self);
21620Sstevel@tonic-gate 	me->self = i_lnode->self;
21630Sstevel@tonic-gate 
21640Sstevel@tonic-gate 	if (i_lnode->devt == DDI_DEV_T_NONE) {
21650Sstevel@tonic-gate 		me->dev_major = (major_t)-1;
21660Sstevel@tonic-gate 		me->dev_minor = (minor_t)-1;
21670Sstevel@tonic-gate 	} else {
21680Sstevel@tonic-gate 		me->dev_major = getmajor(i_lnode->devt);
21690Sstevel@tonic-gate 		me->dev_minor = getminor(i_lnode->devt);
21700Sstevel@tonic-gate 	}
21710Sstevel@tonic-gate 
21720Sstevel@tonic-gate 	/*
21730Sstevel@tonic-gate 	 * The dip corresponding to this lnode must exist in
21740Sstevel@tonic-gate 	 * the snapshot or we wouldn't have created the i_lnode_t
21750Sstevel@tonic-gate 	 * during LDI walk. Save the offset of the dip.
21760Sstevel@tonic-gate 	 */
21770Sstevel@tonic-gate 	ASSERT(i_lnode->di_node && i_lnode->di_node->self > 0);
21780Sstevel@tonic-gate 	me->node = i_lnode->di_node->self;
21790Sstevel@tonic-gate 
21800Sstevel@tonic-gate 	/*
21810Sstevel@tonic-gate 	 * There must be at least one link in or out of this lnode
21820Sstevel@tonic-gate 	 * or we wouldn't have created it. These fields will be set
21830Sstevel@tonic-gate 	 * during the link hash walk.
21840Sstevel@tonic-gate 	 */
21850Sstevel@tonic-gate 	ASSERT((i_lnode->link_in != NULL) || (i_lnode->link_out != NULL));
21860Sstevel@tonic-gate 
21870Sstevel@tonic-gate 	/*
21880Sstevel@tonic-gate 	 * set the offset of the devinfo node associated with this
21890Sstevel@tonic-gate 	 * lnode. Also update the node_next next pointer.  this pointer
21900Sstevel@tonic-gate 	 * is set if there are multiple lnodes associated with the same
21910Sstevel@tonic-gate 	 * devinfo node.  (could occure when multiple minor nodes
21920Sstevel@tonic-gate 	 * are open for one device, etc.)
21930Sstevel@tonic-gate 	 */
21940Sstevel@tonic-gate 	medinode = i_lnode->di_node;
21950Sstevel@tonic-gate 	me->node_next = medinode->lnodes;
21960Sstevel@tonic-gate 	medinode->lnodes = me->self;
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate 	return (MH_WALK_CONTINUE);
21990Sstevel@tonic-gate }
22000Sstevel@tonic-gate 
22010Sstevel@tonic-gate static di_off_t
22020Sstevel@tonic-gate di_getlink_data(di_off_t off, struct di_state *st)
22030Sstevel@tonic-gate {
22040Sstevel@tonic-gate 	struct i_layer_data data = {0};
22050Sstevel@tonic-gate 	size_t size;
22060Sstevel@tonic-gate 
22070Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_copylyr: off = %x\n", off));
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate 	st->lnode_hash = mod_hash_create_extended("di_lnode_hash", 32,
22100Sstevel@tonic-gate 	    mod_hash_null_keydtor, (void (*)(mod_hash_val_t))i_lnode_check_free,
22110Sstevel@tonic-gate 	    i_lnode_hashfunc, NULL, i_lnode_cmp, KM_SLEEP);
22120Sstevel@tonic-gate 
22130Sstevel@tonic-gate 	st->link_hash = mod_hash_create_ptrhash("di_link_hash", 32,
22140Sstevel@tonic-gate 	    (void (*)(mod_hash_val_t))i_link_check_free, sizeof (i_link_t));
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate 	/* get driver layering information */
22170Sstevel@tonic-gate 	(void) ldi_usage_walker(st, di_ldi_callback);
22180Sstevel@tonic-gate 
22190Sstevel@tonic-gate 	/* check if there is any link data to include in the snapshot */
22200Sstevel@tonic-gate 	if (st->lnode_count == 0) {
22210Sstevel@tonic-gate 		ASSERT(st->link_count == 0);
22220Sstevel@tonic-gate 		goto out;
22230Sstevel@tonic-gate 	}
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate 	ASSERT(st->link_count != 0);
22260Sstevel@tonic-gate 
22270Sstevel@tonic-gate 	/* get a pointer to snapshot memory for all the di_lnodes */
22280Sstevel@tonic-gate 	size = sizeof (struct di_lnode) * st->lnode_count;
22290Sstevel@tonic-gate 	data.lnode_off = off = di_checkmem(st, off, size);
22300Sstevel@tonic-gate 	off += DI_ALIGN(size);
22310Sstevel@tonic-gate 
22320Sstevel@tonic-gate 	/* get a pointer to snapshot memory for all the di_links */
22330Sstevel@tonic-gate 	size = sizeof (struct di_link) * st->link_count;
22340Sstevel@tonic-gate 	data.link_off = off = di_checkmem(st, off, size);
22350Sstevel@tonic-gate 	off += DI_ALIGN(size);
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 	data.lnode_count = data.link_count = 0;
22380Sstevel@tonic-gate 	data.st = st;
22390Sstevel@tonic-gate 
22400Sstevel@tonic-gate 	/*
22410Sstevel@tonic-gate 	 * We have lnodes and links that will go into the
22420Sstevel@tonic-gate 	 * snapshot, so let's walk the respective hashes
22430Sstevel@tonic-gate 	 * and snapshot them. The various linkages are
22440Sstevel@tonic-gate 	 * also set up during the walk.
22450Sstevel@tonic-gate 	 */
22460Sstevel@tonic-gate 	mod_hash_walk(st->lnode_hash, i_lnode_walker, (void *)&data);
22470Sstevel@tonic-gate 	ASSERT(data.lnode_count == st->lnode_count);
22480Sstevel@tonic-gate 
22490Sstevel@tonic-gate 	mod_hash_walk(st->link_hash, i_link_walker, (void *)&data);
22500Sstevel@tonic-gate 	ASSERT(data.link_count == st->link_count);
22510Sstevel@tonic-gate 
22520Sstevel@tonic-gate out:
22530Sstevel@tonic-gate 	/* free up the i_lnodes and i_links used to create the snapshot */
22540Sstevel@tonic-gate 	mod_hash_destroy_hash(st->lnode_hash);
22550Sstevel@tonic-gate 	mod_hash_destroy_hash(st->link_hash);
22560Sstevel@tonic-gate 	st->lnode_count = 0;
22570Sstevel@tonic-gate 	st->link_count = 0;
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 	return (off);
22600Sstevel@tonic-gate }
22610Sstevel@tonic-gate 
22620Sstevel@tonic-gate 
22630Sstevel@tonic-gate /*
22640Sstevel@tonic-gate  * Copy all minor data nodes attached to a devinfo node into the snapshot.
22650Sstevel@tonic-gate  * It is called from di_copynode with devi_lock held.
22660Sstevel@tonic-gate  */
22670Sstevel@tonic-gate static di_off_t
22680Sstevel@tonic-gate di_getmdata(struct ddi_minor_data *mnode, di_off_t *off_p, di_off_t node,
22690Sstevel@tonic-gate 	struct di_state *st)
22700Sstevel@tonic-gate {
22710Sstevel@tonic-gate 	di_off_t off;
22720Sstevel@tonic-gate 	struct di_minor *me;
22730Sstevel@tonic-gate 
22740Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_getmdata:\n"));
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate 	/*
22770Sstevel@tonic-gate 	 * check memory first
22780Sstevel@tonic-gate 	 */
22790Sstevel@tonic-gate 	off = di_checkmem(st, *off_p, sizeof (struct di_minor));
22800Sstevel@tonic-gate 	*off_p = off;
22810Sstevel@tonic-gate 
22820Sstevel@tonic-gate 	do {
22830Sstevel@tonic-gate 		me = (struct di_minor *)di_mem_addr(st, off);
22840Sstevel@tonic-gate 		me->self = off;
22850Sstevel@tonic-gate 		me->type = mnode->type;
22860Sstevel@tonic-gate 		me->node = node;
22870Sstevel@tonic-gate 		me->user_private_data = NULL;
22880Sstevel@tonic-gate 
22890Sstevel@tonic-gate 		off += DI_ALIGN(sizeof (struct di_minor));
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate 		/*
22920Sstevel@tonic-gate 		 * Split dev_t to major/minor, so it works for
22930Sstevel@tonic-gate 		 * both ILP32 and LP64 model
22940Sstevel@tonic-gate 		 */
22950Sstevel@tonic-gate 		me->dev_major = getmajor(mnode->ddm_dev);
22960Sstevel@tonic-gate 		me->dev_minor = getminor(mnode->ddm_dev);
22970Sstevel@tonic-gate 		me->spec_type = mnode->ddm_spec_type;
22980Sstevel@tonic-gate 
22990Sstevel@tonic-gate 		if (mnode->ddm_name) {
23000Sstevel@tonic-gate 			off = di_checkmem(st, off,
23010Sstevel@tonic-gate 				strlen(mnode->ddm_name) + 1);
23020Sstevel@tonic-gate 			me->name = off;
23030Sstevel@tonic-gate 			(void) strcpy(di_mem_addr(st, off), mnode->ddm_name);
23040Sstevel@tonic-gate 			off += DI_ALIGN(strlen(mnode->ddm_name) + 1);
23050Sstevel@tonic-gate 		}
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate 		if (mnode->ddm_node_type) {
23080Sstevel@tonic-gate 			off = di_checkmem(st, off,
23090Sstevel@tonic-gate 				strlen(mnode->ddm_node_type) + 1);
23100Sstevel@tonic-gate 			me->node_type = off;
23110Sstevel@tonic-gate 			(void) strcpy(di_mem_addr(st, off),
23120Sstevel@tonic-gate 					mnode->ddm_node_type);
23130Sstevel@tonic-gate 			off += DI_ALIGN(strlen(mnode->ddm_node_type) + 1);
23140Sstevel@tonic-gate 		}
23150Sstevel@tonic-gate 
23160Sstevel@tonic-gate 		off = di_checkmem(st, off, sizeof (struct di_minor));
23170Sstevel@tonic-gate 		me->next = off;
23180Sstevel@tonic-gate 		mnode = mnode->next;
23190Sstevel@tonic-gate 	} while (mnode);
23200Sstevel@tonic-gate 
23210Sstevel@tonic-gate 	me->next = 0;
23220Sstevel@tonic-gate 
23230Sstevel@tonic-gate 	return (off);
23240Sstevel@tonic-gate }
23250Sstevel@tonic-gate 
23260Sstevel@tonic-gate /*
23270Sstevel@tonic-gate  * di_register_dip(), di_find_dip(): The dip must be protected
23280Sstevel@tonic-gate  * from deallocation when using these routines - this can either
23290Sstevel@tonic-gate  * be a reference count, a busy hold or a per-driver lock.
23300Sstevel@tonic-gate  */
23310Sstevel@tonic-gate 
23320Sstevel@tonic-gate static void
23330Sstevel@tonic-gate di_register_dip(struct di_state *st, dev_info_t *dip, di_off_t off)
23340Sstevel@tonic-gate {
23350Sstevel@tonic-gate 	struct dev_info *node = DEVI(dip);
23360Sstevel@tonic-gate 	struct di_key *key = kmem_zalloc(sizeof (*key), KM_SLEEP);
23370Sstevel@tonic-gate 	struct di_dkey *dk;
23380Sstevel@tonic-gate 
23390Sstevel@tonic-gate 	ASSERT(dip);
23400Sstevel@tonic-gate 	ASSERT(off > 0);
23410Sstevel@tonic-gate 
23420Sstevel@tonic-gate 	key->k_type = DI_DKEY;
23430Sstevel@tonic-gate 	dk = &(key->k_u.dkey);
23440Sstevel@tonic-gate 
23450Sstevel@tonic-gate 	dk->dk_dip = dip;
23460Sstevel@tonic-gate 	dk->dk_major = node->devi_major;
23470Sstevel@tonic-gate 	dk->dk_inst = node->devi_instance;
23480Sstevel@tonic-gate 	dk->dk_nodeid = node->devi_nodeid;
23490Sstevel@tonic-gate 
23500Sstevel@tonic-gate 	if (mod_hash_insert(st->reg_dip_hash, (mod_hash_key_t)key,
23510Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)off) != 0) {
23520Sstevel@tonic-gate 		panic(
23530Sstevel@tonic-gate 		    "duplicate devinfo (%p) registered during device "
23540Sstevel@tonic-gate 		    "tree walk", (void *)dip);
23550Sstevel@tonic-gate 	}
23560Sstevel@tonic-gate }
23570Sstevel@tonic-gate 
23580Sstevel@tonic-gate 
23590Sstevel@tonic-gate static int
23600Sstevel@tonic-gate di_dip_find(struct di_state *st, dev_info_t *dip, di_off_t *off_p)
23610Sstevel@tonic-gate {
23620Sstevel@tonic-gate 	/*
23630Sstevel@tonic-gate 	 * uintptr_t must be used because it matches the size of void *;
23640Sstevel@tonic-gate 	 * mod_hash expects clients to place results into pointer-size
23650Sstevel@tonic-gate 	 * containers; since di_off_t is always a 32-bit offset, alignment
23660Sstevel@tonic-gate 	 * would otherwise be broken on 64-bit kernels.
23670Sstevel@tonic-gate 	 */
23680Sstevel@tonic-gate 	uintptr_t	offset;
23690Sstevel@tonic-gate 	struct		di_key key = {0};
23700Sstevel@tonic-gate 	struct		di_dkey *dk;
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate 	ASSERT(st->reg_dip_hash);
23730Sstevel@tonic-gate 	ASSERT(dip);
23740Sstevel@tonic-gate 	ASSERT(off_p);
23750Sstevel@tonic-gate 
23760Sstevel@tonic-gate 
23770Sstevel@tonic-gate 	key.k_type = DI_DKEY;
23780Sstevel@tonic-gate 	dk = &(key.k_u.dkey);
23790Sstevel@tonic-gate 
23800Sstevel@tonic-gate 	dk->dk_dip = dip;
23810Sstevel@tonic-gate 	dk->dk_major = DEVI(dip)->devi_major;
23820Sstevel@tonic-gate 	dk->dk_inst = DEVI(dip)->devi_instance;
23830Sstevel@tonic-gate 	dk->dk_nodeid = DEVI(dip)->devi_nodeid;
23840Sstevel@tonic-gate 
23850Sstevel@tonic-gate 	if (mod_hash_find(st->reg_dip_hash, (mod_hash_key_t)&key,
23860Sstevel@tonic-gate 	    (mod_hash_val_t *)&offset) == 0) {
23870Sstevel@tonic-gate 		*off_p = (di_off_t)offset;
23880Sstevel@tonic-gate 		return (0);
23890Sstevel@tonic-gate 	} else {
23900Sstevel@tonic-gate 		return (-1);
23910Sstevel@tonic-gate 	}
23920Sstevel@tonic-gate }
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate /*
23950Sstevel@tonic-gate  * di_register_pip(), di_find_pip(): The pip must be protected from deallocation
23960Sstevel@tonic-gate  * when using these routines. The caller must do this by protecting the
23970Sstevel@tonic-gate  * client(or phci)<->pip linkage while traversing the list and then holding the
23980Sstevel@tonic-gate  * pip when it is found in the list.
23990Sstevel@tonic-gate  */
24000Sstevel@tonic-gate 
24010Sstevel@tonic-gate static void
24020Sstevel@tonic-gate di_register_pip(struct di_state *st, mdi_pathinfo_t *pip, di_off_t off)
24030Sstevel@tonic-gate {
24040Sstevel@tonic-gate 	struct di_key	*key = kmem_zalloc(sizeof (*key), KM_SLEEP);
24050Sstevel@tonic-gate 	char		*path_addr;
24060Sstevel@tonic-gate 	struct di_pkey	*pk;
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 	ASSERT(pip);
24090Sstevel@tonic-gate 	ASSERT(off > 0);
24100Sstevel@tonic-gate 
24110Sstevel@tonic-gate 	key->k_type = DI_PKEY;
24120Sstevel@tonic-gate 	pk = &(key->k_u.pkey);
24130Sstevel@tonic-gate 
24140Sstevel@tonic-gate 	pk->pk_pip = pip;
24150Sstevel@tonic-gate 	path_addr = mdi_pi_get_addr(pip);
24160Sstevel@tonic-gate 	if (path_addr)
24170Sstevel@tonic-gate 		pk->pk_path_addr = i_ddi_strdup(path_addr, KM_SLEEP);
24180Sstevel@tonic-gate 	pk->pk_client = mdi_pi_get_client(pip);
24190Sstevel@tonic-gate 	pk->pk_phci = mdi_pi_get_phci(pip);
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate 	if (mod_hash_insert(st->reg_pip_hash, (mod_hash_key_t)key,
24220Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)off) != 0) {
24230Sstevel@tonic-gate 		panic(
24240Sstevel@tonic-gate 		    "duplicate pathinfo (%p) registered during device "
24250Sstevel@tonic-gate 		    "tree walk", (void *)pip);
24260Sstevel@tonic-gate 	}
24270Sstevel@tonic-gate }
24280Sstevel@tonic-gate 
24290Sstevel@tonic-gate /*
24300Sstevel@tonic-gate  * As with di_register_pip, the caller must hold or lock the pip
24310Sstevel@tonic-gate  */
24320Sstevel@tonic-gate static int
24330Sstevel@tonic-gate di_pip_find(struct di_state *st, mdi_pathinfo_t *pip, di_off_t *off_p)
24340Sstevel@tonic-gate {
24350Sstevel@tonic-gate 	/*
24360Sstevel@tonic-gate 	 * uintptr_t must be used because it matches the size of void *;
24370Sstevel@tonic-gate 	 * mod_hash expects clients to place results into pointer-size
24380Sstevel@tonic-gate 	 * containers; since di_off_t is always a 32-bit offset, alignment
24390Sstevel@tonic-gate 	 * would otherwise be broken on 64-bit kernels.
24400Sstevel@tonic-gate 	 */
24410Sstevel@tonic-gate 	uintptr_t	offset;
24420Sstevel@tonic-gate 	struct di_key	key = {0};
24430Sstevel@tonic-gate 	struct di_pkey	*pk;
24440Sstevel@tonic-gate 
24450Sstevel@tonic-gate 	ASSERT(st->reg_pip_hash);
24460Sstevel@tonic-gate 	ASSERT(off_p);
24470Sstevel@tonic-gate 
24480Sstevel@tonic-gate 	if (pip == NULL) {
24490Sstevel@tonic-gate 		*off_p = 0;
24500Sstevel@tonic-gate 		return (0);
24510Sstevel@tonic-gate 	}
24520Sstevel@tonic-gate 
24530Sstevel@tonic-gate 	key.k_type = DI_PKEY;
24540Sstevel@tonic-gate 	pk = &(key.k_u.pkey);
24550Sstevel@tonic-gate 
24560Sstevel@tonic-gate 	pk->pk_pip = pip;
24570Sstevel@tonic-gate 	pk->pk_path_addr = mdi_pi_get_addr(pip);
24580Sstevel@tonic-gate 	pk->pk_client = mdi_pi_get_client(pip);
24590Sstevel@tonic-gate 	pk->pk_phci = mdi_pi_get_phci(pip);
24600Sstevel@tonic-gate 
24610Sstevel@tonic-gate 	if (mod_hash_find(st->reg_pip_hash, (mod_hash_key_t)&key,
24620Sstevel@tonic-gate 	    (mod_hash_val_t *)&offset) == 0) {
24630Sstevel@tonic-gate 		*off_p = (di_off_t)offset;
24640Sstevel@tonic-gate 		return (0);
24650Sstevel@tonic-gate 	} else {
24660Sstevel@tonic-gate 		return (-1);
24670Sstevel@tonic-gate 	}
24680Sstevel@tonic-gate }
24690Sstevel@tonic-gate 
24700Sstevel@tonic-gate static di_path_state_t
24710Sstevel@tonic-gate path_state_convert(mdi_pathinfo_state_t st)
24720Sstevel@tonic-gate {
24730Sstevel@tonic-gate 	switch (st) {
24740Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_ONLINE:
24750Sstevel@tonic-gate 		return (DI_PATH_STATE_ONLINE);
24760Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_STANDBY:
24770Sstevel@tonic-gate 		return (DI_PATH_STATE_STANDBY);
24780Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_OFFLINE:
24790Sstevel@tonic-gate 		return (DI_PATH_STATE_OFFLINE);
24800Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_FAULT:
24810Sstevel@tonic-gate 		return (DI_PATH_STATE_FAULT);
24820Sstevel@tonic-gate 	default:
24830Sstevel@tonic-gate 		return (DI_PATH_STATE_UNKNOWN);
24840Sstevel@tonic-gate 	}
24850Sstevel@tonic-gate }
24860Sstevel@tonic-gate 
24870Sstevel@tonic-gate 
24880Sstevel@tonic-gate static di_off_t
24890Sstevel@tonic-gate di_path_getprop(mdi_pathinfo_t *pip, di_off_t off, di_off_t *off_p,
24900Sstevel@tonic-gate     struct di_state *st)
24910Sstevel@tonic-gate {
24920Sstevel@tonic-gate 	nvpair_t *prop = NULL;
24930Sstevel@tonic-gate 	struct di_path_prop *me;
24940Sstevel@tonic-gate 
24950Sstevel@tonic-gate 	if (mdi_pi_get_next_prop(pip, NULL) == NULL) {
24960Sstevel@tonic-gate 		*off_p = 0;
24970Sstevel@tonic-gate 		return (off);
24980Sstevel@tonic-gate 	}
24990Sstevel@tonic-gate 
25000Sstevel@tonic-gate 	off = di_checkmem(st, off, sizeof (struct di_path_prop));
25010Sstevel@tonic-gate 	*off_p = off;
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate 	while (prop = mdi_pi_get_next_prop(pip, prop)) {
25040Sstevel@tonic-gate 		int delta = 0;
25050Sstevel@tonic-gate 
25060Sstevel@tonic-gate 		me = (struct di_path_prop *)di_mem_addr(st, off);
25070Sstevel@tonic-gate 		me->self = off;
25080Sstevel@tonic-gate 		off += sizeof (struct di_path_prop);
25090Sstevel@tonic-gate 
25100Sstevel@tonic-gate 		/*
25110Sstevel@tonic-gate 		 * property name
25120Sstevel@tonic-gate 		 */
25130Sstevel@tonic-gate 		off = di_checkmem(st, off, strlen(nvpair_name(prop)) + 1);
25140Sstevel@tonic-gate 		me->prop_name = off;
25150Sstevel@tonic-gate 		(void) strcpy(di_mem_addr(st, off), nvpair_name(prop));
25160Sstevel@tonic-gate 		off += strlen(nvpair_name(prop)) + 1;
25170Sstevel@tonic-gate 
25180Sstevel@tonic-gate 		switch (nvpair_type(prop)) {
25190Sstevel@tonic-gate 		case DATA_TYPE_BYTE:
25200Sstevel@tonic-gate 		case DATA_TYPE_INT16:
25210Sstevel@tonic-gate 		case DATA_TYPE_UINT16:
25220Sstevel@tonic-gate 		case DATA_TYPE_INT32:
25230Sstevel@tonic-gate 		case DATA_TYPE_UINT32:
25240Sstevel@tonic-gate 			delta = sizeof (int32_t);
25250Sstevel@tonic-gate 			me->prop_type = DDI_PROP_TYPE_INT;
25260Sstevel@tonic-gate 			off = di_checkmem(st, off, delta);
25270Sstevel@tonic-gate 			(void) nvpair_value_int32(prop,
25280Sstevel@tonic-gate 			    (int32_t *)di_mem_addr(st, off));
25290Sstevel@tonic-gate 			break;
25300Sstevel@tonic-gate 
25310Sstevel@tonic-gate 		case DATA_TYPE_INT64:
25320Sstevel@tonic-gate 		case DATA_TYPE_UINT64:
25330Sstevel@tonic-gate 			delta = sizeof (int64_t);
25340Sstevel@tonic-gate 			me->prop_type = DDI_PROP_TYPE_INT64;
25350Sstevel@tonic-gate 			off = di_checkmem(st, off, delta);
25360Sstevel@tonic-gate 			(void) nvpair_value_int64(prop,
25370Sstevel@tonic-gate 			    (int64_t *)di_mem_addr(st, off));
25380Sstevel@tonic-gate 			break;
25390Sstevel@tonic-gate 
25400Sstevel@tonic-gate 		case DATA_TYPE_STRING:
25410Sstevel@tonic-gate 		{
25420Sstevel@tonic-gate 			char *str;
25430Sstevel@tonic-gate 			(void) nvpair_value_string(prop, &str);
25440Sstevel@tonic-gate 			delta = strlen(str) + 1;
25450Sstevel@tonic-gate 			me->prop_type = DDI_PROP_TYPE_STRING;
25460Sstevel@tonic-gate 			off = di_checkmem(st, off, delta);
25470Sstevel@tonic-gate 			(void) strcpy(di_mem_addr(st, off), str);
25480Sstevel@tonic-gate 			break;
25490Sstevel@tonic-gate 		}
25500Sstevel@tonic-gate 		case DATA_TYPE_BYTE_ARRAY:
25510Sstevel@tonic-gate 		case DATA_TYPE_INT16_ARRAY:
25520Sstevel@tonic-gate 		case DATA_TYPE_UINT16_ARRAY:
25530Sstevel@tonic-gate 		case DATA_TYPE_INT32_ARRAY:
25540Sstevel@tonic-gate 		case DATA_TYPE_UINT32_ARRAY:
25550Sstevel@tonic-gate 		case DATA_TYPE_INT64_ARRAY:
25560Sstevel@tonic-gate 		case DATA_TYPE_UINT64_ARRAY:
25570Sstevel@tonic-gate 		{
25580Sstevel@tonic-gate 			uchar_t *buf;
25590Sstevel@tonic-gate 			uint_t nelems;
25600Sstevel@tonic-gate 			(void) nvpair_value_byte_array(prop, &buf, &nelems);
25610Sstevel@tonic-gate 			delta = nelems;
25620Sstevel@tonic-gate 			me->prop_type = DDI_PROP_TYPE_BYTE;
25630Sstevel@tonic-gate 			if (nelems != 0) {
25640Sstevel@tonic-gate 				off = di_checkmem(st, off, delta);
25650Sstevel@tonic-gate 				bcopy(buf, di_mem_addr(st, off), nelems);
25660Sstevel@tonic-gate 			}
25670Sstevel@tonic-gate 			break;
25680Sstevel@tonic-gate 		}
25690Sstevel@tonic-gate 
25700Sstevel@tonic-gate 		default:	/* Unknown or unhandled type; skip it */
25710Sstevel@tonic-gate 			delta = 0;
25720Sstevel@tonic-gate 			break;
25730Sstevel@tonic-gate 		}
25740Sstevel@tonic-gate 
25750Sstevel@tonic-gate 		if (delta > 0) {
25760Sstevel@tonic-gate 			me->prop_data = off;
25770Sstevel@tonic-gate 		}
25780Sstevel@tonic-gate 
25790Sstevel@tonic-gate 		me->prop_len = delta;
25800Sstevel@tonic-gate 		off += delta;
25810Sstevel@tonic-gate 
25820Sstevel@tonic-gate 		off = di_checkmem(st, off, sizeof (struct di_path_prop));
25830Sstevel@tonic-gate 		me->prop_next = off;
25840Sstevel@tonic-gate 	}
25850Sstevel@tonic-gate 
25860Sstevel@tonic-gate 	me->prop_next = 0;
25870Sstevel@tonic-gate 	return (off);
25880Sstevel@tonic-gate }
25890Sstevel@tonic-gate 
25900Sstevel@tonic-gate 
25910Sstevel@tonic-gate static void
25920Sstevel@tonic-gate di_path_one_endpoint(struct di_path *me, di_off_t noff, di_off_t **off_pp,
25930Sstevel@tonic-gate     int get_client)
25940Sstevel@tonic-gate {
25950Sstevel@tonic-gate 	if (get_client) {
25960Sstevel@tonic-gate 		ASSERT(me->path_client == 0);
25970Sstevel@tonic-gate 		me->path_client = noff;
25980Sstevel@tonic-gate 		ASSERT(me->path_c_link == 0);
25990Sstevel@tonic-gate 		*off_pp = &me->path_c_link;
26000Sstevel@tonic-gate 		me->path_snap_state &=
26010Sstevel@tonic-gate 		    ~(DI_PATH_SNAP_NOCLIENT | DI_PATH_SNAP_NOCLINK);
26020Sstevel@tonic-gate 	} else {
26030Sstevel@tonic-gate 		ASSERT(me->path_phci == 0);
26040Sstevel@tonic-gate 		me->path_phci = noff;
26050Sstevel@tonic-gate 		ASSERT(me->path_p_link == 0);
26060Sstevel@tonic-gate 		*off_pp = &me->path_p_link;
26070Sstevel@tonic-gate 		me->path_snap_state &=
26080Sstevel@tonic-gate 		    ~(DI_PATH_SNAP_NOPHCI | DI_PATH_SNAP_NOPLINK);
26090Sstevel@tonic-gate 	}
26100Sstevel@tonic-gate }
26110Sstevel@tonic-gate 
26120Sstevel@tonic-gate /*
26130Sstevel@tonic-gate  * poff_p: pointer to the linkage field. This links pips along the client|phci
26140Sstevel@tonic-gate  *	   linkage list.
26150Sstevel@tonic-gate  * noff  : Offset for the endpoint dip snapshot.
26160Sstevel@tonic-gate  */
26170Sstevel@tonic-gate static di_off_t
26180Sstevel@tonic-gate di_getpath_data(dev_info_t *dip, di_off_t *poff_p, di_off_t noff,
26190Sstevel@tonic-gate     struct di_state *st, int get_client)
26200Sstevel@tonic-gate {
26210Sstevel@tonic-gate 	di_off_t off;
26220Sstevel@tonic-gate 	mdi_pathinfo_t *pip;
26230Sstevel@tonic-gate 	struct di_path *me;
26240Sstevel@tonic-gate 	mdi_pathinfo_t *(*next_pip)(dev_info_t *, mdi_pathinfo_t *);
26250Sstevel@tonic-gate 
26260Sstevel@tonic-gate 	dcmn_err2((CE_WARN, "di_getpath_data: client = %d", get_client));
26270Sstevel@tonic-gate 
26280Sstevel@tonic-gate 	/*
26290Sstevel@tonic-gate 	 * The naming of the following mdi_xyz() is unfortunately
26300Sstevel@tonic-gate 	 * non-intuitive. mdi_get_next_phci_path() follows the
26310Sstevel@tonic-gate 	 * client_link i.e. the list of pip's belonging to the
26320Sstevel@tonic-gate 	 * given client dip.
26330Sstevel@tonic-gate 	 */
26340Sstevel@tonic-gate 	if (get_client)
26350Sstevel@tonic-gate 		next_pip = &mdi_get_next_phci_path;
26360Sstevel@tonic-gate 	else
26370Sstevel@tonic-gate 		next_pip = &mdi_get_next_client_path;
26380Sstevel@tonic-gate 
26390Sstevel@tonic-gate 	off = *poff_p;
26400Sstevel@tonic-gate 
26410Sstevel@tonic-gate 	pip = NULL;
26420Sstevel@tonic-gate 	while (pip = (*next_pip)(dip, pip)) {
26430Sstevel@tonic-gate 		mdi_pathinfo_state_t state;
26440Sstevel@tonic-gate 		di_off_t stored_offset;
26450Sstevel@tonic-gate 
26460Sstevel@tonic-gate 		dcmn_err((CE_WARN, "marshalling pip = %p", (void *)pip));
26470Sstevel@tonic-gate 
26480Sstevel@tonic-gate 		mdi_pi_lock(pip);
26490Sstevel@tonic-gate 
26500Sstevel@tonic-gate 		if (di_pip_find(st, pip, &stored_offset) != -1) {
26510Sstevel@tonic-gate 			/*
26520Sstevel@tonic-gate 			 * We've already seen this pathinfo node so we need to
26530Sstevel@tonic-gate 			 * take care not to snap it again; However, one endpoint
26540Sstevel@tonic-gate 			 * and linkage will be set here. The other endpoint
26550Sstevel@tonic-gate 			 * and linkage has already been set when the pip was
26560Sstevel@tonic-gate 			 * first snapshotted i.e. when the other endpoint dip
26570Sstevel@tonic-gate 			 * was snapshotted.
26580Sstevel@tonic-gate 			 */
26590Sstevel@tonic-gate 			me = (struct di_path *)di_mem_addr(st, stored_offset);
26600Sstevel@tonic-gate 
26610Sstevel@tonic-gate 			*poff_p = stored_offset;
26620Sstevel@tonic-gate 
26630Sstevel@tonic-gate 			di_path_one_endpoint(me, noff, &poff_p, get_client);
26640Sstevel@tonic-gate 
26650Sstevel@tonic-gate 			/*
26660Sstevel@tonic-gate 			 * The other endpoint and linkage were set when this
26670Sstevel@tonic-gate 			 * pip was snapshotted. So we are done with both
26680Sstevel@tonic-gate 			 * endpoints and linkages.
26690Sstevel@tonic-gate 			 */
26700Sstevel@tonic-gate 			ASSERT(!(me->path_snap_state &
26710Sstevel@tonic-gate 			    (DI_PATH_SNAP_NOCLIENT|DI_PATH_SNAP_NOPHCI)));
26720Sstevel@tonic-gate 			ASSERT(!(me->path_snap_state &
26730Sstevel@tonic-gate 			    (DI_PATH_SNAP_NOCLINK|DI_PATH_SNAP_NOPLINK)));
26740Sstevel@tonic-gate 
26750Sstevel@tonic-gate 			mdi_pi_unlock(pip);
26760Sstevel@tonic-gate 			continue;
26770Sstevel@tonic-gate 		}
26780Sstevel@tonic-gate 
26790Sstevel@tonic-gate 		/*
26800Sstevel@tonic-gate 		 * Now that we need to snapshot this pip, check memory
26810Sstevel@tonic-gate 		 */
26820Sstevel@tonic-gate 		off = di_checkmem(st, off, sizeof (struct di_path));
26830Sstevel@tonic-gate 		me = (struct di_path *)di_mem_addr(st, off);
26840Sstevel@tonic-gate 		me->self = off;
26850Sstevel@tonic-gate 		*poff_p = off;
26860Sstevel@tonic-gate 		off += sizeof (struct di_path);
26870Sstevel@tonic-gate 
26880Sstevel@tonic-gate 		me->path_snap_state =
26890Sstevel@tonic-gate 		    DI_PATH_SNAP_NOCLINK | DI_PATH_SNAP_NOPLINK;
26900Sstevel@tonic-gate 		me->path_snap_state |=
26910Sstevel@tonic-gate 		    DI_PATH_SNAP_NOCLIENT | DI_PATH_SNAP_NOPHCI;
26920Sstevel@tonic-gate 
26930Sstevel@tonic-gate 		/*
26940Sstevel@tonic-gate 		 * Zero out fields as di_checkmem() doesn't guarantee
26950Sstevel@tonic-gate 		 * zero-filled memory
26960Sstevel@tonic-gate 		 */
26970Sstevel@tonic-gate 		me->path_client = me->path_phci = 0;
26980Sstevel@tonic-gate 		me->path_c_link = me->path_p_link = 0;
26990Sstevel@tonic-gate 
27000Sstevel@tonic-gate 		di_path_one_endpoint(me, noff, &poff_p, get_client);
27010Sstevel@tonic-gate 
27020Sstevel@tonic-gate 		/*
27030Sstevel@tonic-gate 		 * Note the existence of this pathinfo
27040Sstevel@tonic-gate 		 */
27050Sstevel@tonic-gate 		di_register_pip(st, pip, me->self);
27060Sstevel@tonic-gate 
27070Sstevel@tonic-gate 		state = mdi_pi_get_state(pip);
27080Sstevel@tonic-gate 		me->path_state = path_state_convert(state);
27090Sstevel@tonic-gate 
27100Sstevel@tonic-gate 		/*
27110Sstevel@tonic-gate 		 * Get intermediate addressing info.
27120Sstevel@tonic-gate 		 */
27130Sstevel@tonic-gate 		off = di_checkmem(st, off, strlen(mdi_pi_get_addr(pip)) + 1);
27140Sstevel@tonic-gate 		me->path_addr = off;
27150Sstevel@tonic-gate 		(void) strcpy(di_mem_addr(st, off), mdi_pi_get_addr(pip));
27160Sstevel@tonic-gate 		off += strlen(mdi_pi_get_addr(pip)) + 1;
27170Sstevel@tonic-gate 
27180Sstevel@tonic-gate 		/*
27190Sstevel@tonic-gate 		 * Get path properties if props are to be included in the
27200Sstevel@tonic-gate 		 * snapshot
27210Sstevel@tonic-gate 		 */
27220Sstevel@tonic-gate 		if (DINFOPROP & st->command) {
27230Sstevel@tonic-gate 			off = di_path_getprop(pip, off, &me->path_prop, st);
27240Sstevel@tonic-gate 		} else {
27250Sstevel@tonic-gate 			me->path_prop = 0;
27260Sstevel@tonic-gate 		}
27270Sstevel@tonic-gate 
27280Sstevel@tonic-gate 		mdi_pi_unlock(pip);
27290Sstevel@tonic-gate 	}
27300Sstevel@tonic-gate 
27310Sstevel@tonic-gate 	*poff_p = 0;
27320Sstevel@tonic-gate 
27330Sstevel@tonic-gate 	return (off);
27340Sstevel@tonic-gate }
27350Sstevel@tonic-gate 
27360Sstevel@tonic-gate /*
27370Sstevel@tonic-gate  * Copy a list of properties attached to a devinfo node. Called from
27380Sstevel@tonic-gate  * di_copynode with devi_lock held. The major number is passed in case
27390Sstevel@tonic-gate  * we need to call driver's prop_op entry. The value of list indicates
27400Sstevel@tonic-gate  * which list we are copying. Possible values are:
27410Sstevel@tonic-gate  * DI_PROP_DRV_LIST, DI_PROP_SYS_LIST, DI_PROP_GLB_LIST, DI_PROP_HW_LIST
27420Sstevel@tonic-gate  */
27430Sstevel@tonic-gate static di_off_t
27440Sstevel@tonic-gate di_getprop(struct ddi_prop *prop, di_off_t *off_p, struct di_state *st,
27450Sstevel@tonic-gate 	struct dev_info *dip, int list)
27460Sstevel@tonic-gate {
27470Sstevel@tonic-gate 	dev_t dev;
27480Sstevel@tonic-gate 	int (*prop_op)();
27490Sstevel@tonic-gate 	int off, need_prop_op = 0;
27500Sstevel@tonic-gate 	int prop_op_fail = 0;
27510Sstevel@tonic-gate 	ddi_prop_t *propp = NULL;
27520Sstevel@tonic-gate 	struct di_prop *pp;
27530Sstevel@tonic-gate 	struct dev_ops *ops = NULL;
27540Sstevel@tonic-gate 	int prop_len;
27550Sstevel@tonic-gate 	caddr_t prop_val;
27560Sstevel@tonic-gate 
27570Sstevel@tonic-gate 
27580Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_getprop:\n"));
27590Sstevel@tonic-gate 
27600Sstevel@tonic-gate 	ASSERT(st != NULL);
27610Sstevel@tonic-gate 
27620Sstevel@tonic-gate 	dcmn_err((CE_CONT, "copy property list at addr %p\n", (void *)prop));
27630Sstevel@tonic-gate 
27640Sstevel@tonic-gate 	/*
27650Sstevel@tonic-gate 	 * Figure out if we need to call driver's prop_op entry point.
27660Sstevel@tonic-gate 	 * The conditions are:
27670Sstevel@tonic-gate 	 *	-- driver property list
27680Sstevel@tonic-gate 	 *	-- driver must be attached and held
27690Sstevel@tonic-gate 	 *	-- driver's cb_prop_op != ddi_prop_op
27700Sstevel@tonic-gate 	 *		or parent's bus_prop_op != ddi_bus_prop_op
27710Sstevel@tonic-gate 	 */
27720Sstevel@tonic-gate 
27730Sstevel@tonic-gate 	if (list != DI_PROP_DRV_LIST) {
27740Sstevel@tonic-gate 		goto getprop;
27750Sstevel@tonic-gate 	}
27760Sstevel@tonic-gate 
27770Sstevel@tonic-gate 	/*
27780Sstevel@tonic-gate 	 * If driver is not attached or if major is -1, we ignore
27790Sstevel@tonic-gate 	 * the driver property list. No one should rely on such
27800Sstevel@tonic-gate 	 * properties.
27810Sstevel@tonic-gate 	 */
27820Sstevel@tonic-gate 	if (i_ddi_node_state((dev_info_t *)dip) < DS_ATTACHED) {
27830Sstevel@tonic-gate 		off = *off_p;
27840Sstevel@tonic-gate 		*off_p = 0;
27850Sstevel@tonic-gate 		return (off);
27860Sstevel@tonic-gate 	}
27870Sstevel@tonic-gate 
27880Sstevel@tonic-gate 	/*
27890Sstevel@tonic-gate 	 * Now we have a driver which is held. We can examine entry points
27900Sstevel@tonic-gate 	 * and check the condition listed above.
27910Sstevel@tonic-gate 	 */
27920Sstevel@tonic-gate 	ops = dip->devi_ops;
27930Sstevel@tonic-gate 
27940Sstevel@tonic-gate 	/*
27950Sstevel@tonic-gate 	 * Some nexus drivers incorrectly set cb_prop_op to nodev,
27960Sstevel@tonic-gate 	 * nulldev or even NULL.
27970Sstevel@tonic-gate 	 */
27980Sstevel@tonic-gate 	if (ops && ops->devo_cb_ops &&
27990Sstevel@tonic-gate 	    (ops->devo_cb_ops->cb_prop_op != ddi_prop_op) &&
28000Sstevel@tonic-gate 	    (ops->devo_cb_ops->cb_prop_op != nodev) &&
28010Sstevel@tonic-gate 	    (ops->devo_cb_ops->cb_prop_op != nulldev) &&
28020Sstevel@tonic-gate 	    (ops->devo_cb_ops->cb_prop_op != NULL)) {
28030Sstevel@tonic-gate 		need_prop_op = 1;
28040Sstevel@tonic-gate 	}
28050Sstevel@tonic-gate 
28060Sstevel@tonic-gate getprop:
28070Sstevel@tonic-gate 	/*
28080Sstevel@tonic-gate 	 * check memory availability
28090Sstevel@tonic-gate 	 */
28100Sstevel@tonic-gate 	off = di_checkmem(st, *off_p, sizeof (struct di_prop));
28110Sstevel@tonic-gate 	*off_p = off;
28120Sstevel@tonic-gate 	/*
28130Sstevel@tonic-gate 	 * Now copy properties
28140Sstevel@tonic-gate 	 */
28150Sstevel@tonic-gate 	do {
28160Sstevel@tonic-gate 		pp = (struct di_prop *)di_mem_addr(st, off);
28170Sstevel@tonic-gate 		pp->self = off;
28180Sstevel@tonic-gate 		/*
28190Sstevel@tonic-gate 		 * Split dev_t to major/minor, so it works for
28200Sstevel@tonic-gate 		 * both ILP32 and LP64 model
28210Sstevel@tonic-gate 		 */
28220Sstevel@tonic-gate 		pp->dev_major = getmajor(prop->prop_dev);
28230Sstevel@tonic-gate 		pp->dev_minor = getminor(prop->prop_dev);
28240Sstevel@tonic-gate 		pp->prop_flags = prop->prop_flags;
28250Sstevel@tonic-gate 		pp->prop_list = list;
28260Sstevel@tonic-gate 
28270Sstevel@tonic-gate 		/*
28280Sstevel@tonic-gate 		 * property name
28290Sstevel@tonic-gate 		 */
28300Sstevel@tonic-gate 		off += sizeof (struct di_prop);
28310Sstevel@tonic-gate 		if (prop->prop_name) {
28320Sstevel@tonic-gate 			off = di_checkmem(st, off, strlen(prop->prop_name)
28330Sstevel@tonic-gate 			    + 1);
28340Sstevel@tonic-gate 			pp->prop_name = off;
28350Sstevel@tonic-gate 			(void) strcpy(di_mem_addr(st, off), prop->prop_name);
28360Sstevel@tonic-gate 			off += strlen(prop->prop_name) + 1;
28370Sstevel@tonic-gate 		}
28380Sstevel@tonic-gate 
28390Sstevel@tonic-gate 		/*
28400Sstevel@tonic-gate 		 * Set prop_len here. This may change later
28410Sstevel@tonic-gate 		 * if cb_prop_op returns a different length.
28420Sstevel@tonic-gate 		 */
28430Sstevel@tonic-gate 		pp->prop_len = prop->prop_len;
28440Sstevel@tonic-gate 		if (!need_prop_op) {
28450Sstevel@tonic-gate 			if (prop->prop_val == NULL) {
28460Sstevel@tonic-gate 				dcmn_err((CE_WARN,
28470Sstevel@tonic-gate 				    "devinfo: property fault at %p",
28480Sstevel@tonic-gate 				    (void *)prop));
28490Sstevel@tonic-gate 				pp->prop_data = -1;
28500Sstevel@tonic-gate 			} else if (prop->prop_len != 0) {
28510Sstevel@tonic-gate 				off = di_checkmem(st, off, prop->prop_len);
28520Sstevel@tonic-gate 				pp->prop_data = off;
28530Sstevel@tonic-gate 				bcopy(prop->prop_val, di_mem_addr(st, off),
28540Sstevel@tonic-gate 				    prop->prop_len);
28550Sstevel@tonic-gate 				off += DI_ALIGN(pp->prop_len);
28560Sstevel@tonic-gate 			}
28570Sstevel@tonic-gate 		}
28580Sstevel@tonic-gate 
28590Sstevel@tonic-gate 		off = di_checkmem(st, off, sizeof (struct di_prop));
28600Sstevel@tonic-gate 		pp->next = off;
28610Sstevel@tonic-gate 		prop = prop->prop_next;
28620Sstevel@tonic-gate 	} while (prop);
28630Sstevel@tonic-gate 
28640Sstevel@tonic-gate 	pp->next = 0;
28650Sstevel@tonic-gate 
28660Sstevel@tonic-gate 	if (!need_prop_op) {
28670Sstevel@tonic-gate 		dcmn_err((CE_CONT, "finished property "
28680Sstevel@tonic-gate 		    "list at offset 0x%x\n", off));
28690Sstevel@tonic-gate 		return (off);
28700Sstevel@tonic-gate 	}
28710Sstevel@tonic-gate 
28720Sstevel@tonic-gate 	/*
28730Sstevel@tonic-gate 	 * If there is a need to call driver's prop_op entry,
28740Sstevel@tonic-gate 	 * we must release driver's devi_lock, because the
28750Sstevel@tonic-gate 	 * cb_prop_op entry point will grab it.
28760Sstevel@tonic-gate 	 *
28770Sstevel@tonic-gate 	 * The snapshot memory has already been allocated above,
28780Sstevel@tonic-gate 	 * which means the length of an active property should
28790Sstevel@tonic-gate 	 * remain fixed for this implementation to work.
28800Sstevel@tonic-gate 	 */
28810Sstevel@tonic-gate 
28820Sstevel@tonic-gate 
28830Sstevel@tonic-gate 	prop_op = ops->devo_cb_ops->cb_prop_op;
28840Sstevel@tonic-gate 	pp = (struct di_prop *)di_mem_addr(st, *off_p);
28850Sstevel@tonic-gate 
28860Sstevel@tonic-gate 	mutex_exit(&dip->devi_lock);
28870Sstevel@tonic-gate 
28880Sstevel@tonic-gate 	do {
28890Sstevel@tonic-gate 		int err;
28900Sstevel@tonic-gate 		struct di_prop *tmp;
28910Sstevel@tonic-gate 
28920Sstevel@tonic-gate 		if (pp->next) {
28930Sstevel@tonic-gate 			tmp = (struct di_prop *)
28940Sstevel@tonic-gate 			    di_mem_addr(st, pp->next);
28950Sstevel@tonic-gate 		} else {
28960Sstevel@tonic-gate 			tmp = NULL;
28970Sstevel@tonic-gate 		}
28980Sstevel@tonic-gate 
28990Sstevel@tonic-gate 		/*
29000Sstevel@tonic-gate 		 * call into driver's prop_op entry point
29010Sstevel@tonic-gate 		 *
29020Sstevel@tonic-gate 		 * Must search DDI_DEV_T_NONE with DDI_DEV_T_ANY
29030Sstevel@tonic-gate 		 */
29040Sstevel@tonic-gate 		dev = makedevice(pp->dev_major, pp->dev_minor);
29050Sstevel@tonic-gate 		if (dev == DDI_DEV_T_NONE)
29060Sstevel@tonic-gate 			dev = DDI_DEV_T_ANY;
29070Sstevel@tonic-gate 
29080Sstevel@tonic-gate 		dcmn_err((CE_CONT, "call prop_op"
29090Sstevel@tonic-gate 		    "(%lx, %p, PROP_LEN_AND_VAL_BUF, "
29100Sstevel@tonic-gate 		    "DDI_PROP_DONTPASS, \"%s\", %p, &%d)\n",
29110Sstevel@tonic-gate 		    dev,
29120Sstevel@tonic-gate 		    (void *)dip,
29130Sstevel@tonic-gate 		    (char *)di_mem_addr(st, pp->prop_name),
29140Sstevel@tonic-gate 		    (void *)di_mem_addr(st, pp->prop_data),
29150Sstevel@tonic-gate 		    pp->prop_len));
29160Sstevel@tonic-gate 
29170Sstevel@tonic-gate 		if ((err = (*prop_op)(dev, (dev_info_t)dip,
29180Sstevel@tonic-gate 		    PROP_LEN_AND_VAL_ALLOC, DDI_PROP_DONTPASS,
29190Sstevel@tonic-gate 		    (char *)di_mem_addr(st, pp->prop_name),
29200Sstevel@tonic-gate 		    &prop_val, &prop_len)) != DDI_PROP_SUCCESS) {
29210Sstevel@tonic-gate 			if ((propp = i_ddi_prop_search(dev,
29220Sstevel@tonic-gate 			    (char *)di_mem_addr(st, pp->prop_name),
29230Sstevel@tonic-gate 			    (uint_t)pp->prop_flags,
29240Sstevel@tonic-gate 			    &(DEVI(dip)->devi_drv_prop_ptr))) != NULL) {
29250Sstevel@tonic-gate 				pp->prop_len = propp->prop_len;
29260Sstevel@tonic-gate 				if (pp->prop_len != 0) {
29270Sstevel@tonic-gate 					off = di_checkmem(st, off,
29280Sstevel@tonic-gate 					    pp->prop_len);
29290Sstevel@tonic-gate 					pp->prop_data = off;
29300Sstevel@tonic-gate 					bcopy(propp->prop_val, di_mem_addr(st,
29310Sstevel@tonic-gate 					    pp->prop_data), propp->prop_len);
29320Sstevel@tonic-gate 					off += DI_ALIGN(pp->prop_len);
29330Sstevel@tonic-gate 				}
29340Sstevel@tonic-gate 			} else {
29350Sstevel@tonic-gate 				prop_op_fail = 1;
29360Sstevel@tonic-gate 			}
29370Sstevel@tonic-gate 		} else if (prop_len != 0) {
29380Sstevel@tonic-gate 			pp->prop_len = prop_len;
29390Sstevel@tonic-gate 			off = di_checkmem(st, off, prop_len);
29400Sstevel@tonic-gate 			pp->prop_data = off;
29410Sstevel@tonic-gate 			bcopy(prop_val, di_mem_addr(st, off), prop_len);
29420Sstevel@tonic-gate 			off += DI_ALIGN(prop_len);
29430Sstevel@tonic-gate 			kmem_free(prop_val, prop_len);
29440Sstevel@tonic-gate 		}
29450Sstevel@tonic-gate 
29460Sstevel@tonic-gate 		if (prop_op_fail) {
29470Sstevel@tonic-gate 			pp->prop_data = -1;
29480Sstevel@tonic-gate 			dcmn_err((CE_WARN, "devinfo: prop_op failure "
29490Sstevel@tonic-gate 			    "for \"%s\" err %d",
29500Sstevel@tonic-gate 			    di_mem_addr(st, pp->prop_name), err));
29510Sstevel@tonic-gate 		}
29520Sstevel@tonic-gate 
29530Sstevel@tonic-gate 		pp = tmp;
29540Sstevel@tonic-gate 
29550Sstevel@tonic-gate 	} while (pp);
29560Sstevel@tonic-gate 
29570Sstevel@tonic-gate 	mutex_enter(&dip->devi_lock);
29580Sstevel@tonic-gate 	dcmn_err((CE_CONT, "finished property list at offset 0x%x\n", off));
29590Sstevel@tonic-gate 	return (off);
29600Sstevel@tonic-gate }
29610Sstevel@tonic-gate 
29620Sstevel@tonic-gate /*
29630Sstevel@tonic-gate  * find private data format attached to a dip
29640Sstevel@tonic-gate  * parent = 1 to match driver name of parent dip (for parent private data)
29650Sstevel@tonic-gate  *	0 to match driver name of current dip (for driver private data)
29660Sstevel@tonic-gate  */
29670Sstevel@tonic-gate #define	DI_MATCH_DRIVER	0
29680Sstevel@tonic-gate #define	DI_MATCH_PARENT	1
29690Sstevel@tonic-gate 
29700Sstevel@tonic-gate struct di_priv_format *
29710Sstevel@tonic-gate di_match_drv_name(struct dev_info *node, struct di_state *st, int match)
29720Sstevel@tonic-gate {
29730Sstevel@tonic-gate 	int i, count, len;
29740Sstevel@tonic-gate 	char *drv_name;
29750Sstevel@tonic-gate 	major_t major;
29760Sstevel@tonic-gate 	struct di_all *all;
29770Sstevel@tonic-gate 	struct di_priv_format *form;
29780Sstevel@tonic-gate 
29790Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_match_drv_name: node = %s, match = %x\n",
29800Sstevel@tonic-gate 		node->devi_node_name, match));
29810Sstevel@tonic-gate 
29820Sstevel@tonic-gate 	if (match == DI_MATCH_PARENT) {
29830Sstevel@tonic-gate 		node = DEVI(node->devi_parent);
29840Sstevel@tonic-gate 	}
29850Sstevel@tonic-gate 
29860Sstevel@tonic-gate 	if (node == NULL) {
29870Sstevel@tonic-gate 		return (NULL);
29880Sstevel@tonic-gate 	}
29890Sstevel@tonic-gate 
29900Sstevel@tonic-gate 	major = ddi_name_to_major(node->devi_binding_name);
29910Sstevel@tonic-gate 	if (major == (major_t)(-1)) {
29920Sstevel@tonic-gate 		return (NULL);
29930Sstevel@tonic-gate 	}
29940Sstevel@tonic-gate 
29950Sstevel@tonic-gate 	/*
29960Sstevel@tonic-gate 	 * Match the driver name.
29970Sstevel@tonic-gate 	 */
29980Sstevel@tonic-gate 	drv_name = ddi_major_to_name(major);
29990Sstevel@tonic-gate 	if ((drv_name == NULL) || *drv_name == '\0') {
30000Sstevel@tonic-gate 		return (NULL);
30010Sstevel@tonic-gate 	}
30020Sstevel@tonic-gate 
30030Sstevel@tonic-gate 	/* Now get the di_priv_format array */
30040Sstevel@tonic-gate 	all = (struct di_all *)di_mem_addr(st, 0);
30050Sstevel@tonic-gate 
30060Sstevel@tonic-gate 	if (match == DI_MATCH_PARENT) {
30070Sstevel@tonic-gate 		count = all->n_ppdata;
30080Sstevel@tonic-gate 		form = (struct di_priv_format *)
30090Sstevel@tonic-gate 			(di_mem_addr(st, 0) + all->ppdata_format);
30100Sstevel@tonic-gate 	} else {
30110Sstevel@tonic-gate 		count = all->n_dpdata;
30120Sstevel@tonic-gate 		form = (struct di_priv_format *)
30130Sstevel@tonic-gate 			((caddr_t)all + all->dpdata_format);
30140Sstevel@tonic-gate 	}
30150Sstevel@tonic-gate 
30160Sstevel@tonic-gate 	len = strlen(drv_name);
30170Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
30180Sstevel@tonic-gate 		char *tmp;
30190Sstevel@tonic-gate 
30200Sstevel@tonic-gate 		tmp = form[i].drv_name;
30210Sstevel@tonic-gate 		while (tmp && (*tmp != '\0')) {
30220Sstevel@tonic-gate 			if (strncmp(drv_name, tmp, len) == 0) {
30230Sstevel@tonic-gate 				return (&form[i]);
30240Sstevel@tonic-gate 			}
30250Sstevel@tonic-gate 			/*
30260Sstevel@tonic-gate 			 * Move to next driver name, skipping a white space
30270Sstevel@tonic-gate 			 */
30280Sstevel@tonic-gate 			if (tmp = strchr(tmp, ' ')) {
30290Sstevel@tonic-gate 				tmp++;
30300Sstevel@tonic-gate 			}
30310Sstevel@tonic-gate 		}
30320Sstevel@tonic-gate 	}
30330Sstevel@tonic-gate 
30340Sstevel@tonic-gate 	return (NULL);
30350Sstevel@tonic-gate }
30360Sstevel@tonic-gate 
30370Sstevel@tonic-gate /*
30380Sstevel@tonic-gate  * The following functions copy data as specified by the format passed in.
30390Sstevel@tonic-gate  * To prevent invalid format from panicing the system, we call on_fault().
30400Sstevel@tonic-gate  * A return value of 0 indicates an error. Otherwise, the total offset
30410Sstevel@tonic-gate  * is returned.
30420Sstevel@tonic-gate  */
30430Sstevel@tonic-gate #define	DI_MAX_PRIVDATA	(PAGESIZE >> 1)	/* max private data size */
30440Sstevel@tonic-gate 
30450Sstevel@tonic-gate static di_off_t
30460Sstevel@tonic-gate di_getprvdata(struct di_priv_format *pdp, void *data, di_off_t *off_p,
30470Sstevel@tonic-gate 	struct di_state *st)
30480Sstevel@tonic-gate {
30490Sstevel@tonic-gate 	caddr_t pa;
30500Sstevel@tonic-gate 	void *ptr;
30510Sstevel@tonic-gate 	int i, size, repeat;
30520Sstevel@tonic-gate 	di_off_t off, off0, *tmp;
30530Sstevel@tonic-gate 
30540Sstevel@tonic-gate 	label_t ljb;
30550Sstevel@tonic-gate 
30560Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_getprvdata:\n"));
30570Sstevel@tonic-gate 
30580Sstevel@tonic-gate 	/*
30590Sstevel@tonic-gate 	 * check memory availability. Private data size is
30600Sstevel@tonic-gate 	 * limited to DI_MAX_PRIVDATA.
30610Sstevel@tonic-gate 	 */
30620Sstevel@tonic-gate 	off = di_checkmem(st, *off_p, DI_MAX_PRIVDATA);
30630Sstevel@tonic-gate 
30640Sstevel@tonic-gate 	if ((pdp->bytes <= 0) || pdp->bytes > DI_MAX_PRIVDATA) {
30650Sstevel@tonic-gate 		goto failure;
30660Sstevel@tonic-gate 	}
30670Sstevel@tonic-gate 
30680Sstevel@tonic-gate 	if (!on_fault(&ljb)) {
30690Sstevel@tonic-gate 		/* copy the struct */
30700Sstevel@tonic-gate 		bcopy(data, di_mem_addr(st, off), pdp->bytes);
30710Sstevel@tonic-gate 		off0 = DI_ALIGN(pdp->bytes);
30720Sstevel@tonic-gate 
30730Sstevel@tonic-gate 		/* dereferencing pointers */
30740Sstevel@tonic-gate 		for (i = 0; i < MAX_PTR_IN_PRV; i++) {
30750Sstevel@tonic-gate 
30760Sstevel@tonic-gate 			if (pdp->ptr[i].size == 0) {
30770Sstevel@tonic-gate 				goto success;	/* no more ptrs */
30780Sstevel@tonic-gate 			}
30790Sstevel@tonic-gate 
30800Sstevel@tonic-gate 			/*
30810Sstevel@tonic-gate 			 * first, get the pointer content
30820Sstevel@tonic-gate 			 */
30830Sstevel@tonic-gate 			if ((pdp->ptr[i].offset < 0) ||
30840Sstevel@tonic-gate 				(pdp->ptr[i].offset >
30850Sstevel@tonic-gate 				pdp->bytes - sizeof (char *)))
30860Sstevel@tonic-gate 				goto failure;	/* wrong offset */
30870Sstevel@tonic-gate 
30880Sstevel@tonic-gate 			pa = di_mem_addr(st, off + pdp->ptr[i].offset);
30890Sstevel@tonic-gate 			tmp = (di_off_t *)pa;	/* to store off_t later */
30900Sstevel@tonic-gate 
30910Sstevel@tonic-gate 			ptr = *((void **) pa);	/* get pointer value */
30920Sstevel@tonic-gate 			if (ptr == NULL) {	/* if NULL pointer, go on */
30930Sstevel@tonic-gate 				continue;
30940Sstevel@tonic-gate 			}
30950Sstevel@tonic-gate 
30960Sstevel@tonic-gate 			/*
30970Sstevel@tonic-gate 			 * next, find the repeat count (array dimension)
30980Sstevel@tonic-gate 			 */
30990Sstevel@tonic-gate 			repeat = pdp->ptr[i].len_offset;
31000Sstevel@tonic-gate 
31010Sstevel@tonic-gate 			/*
31020Sstevel@tonic-gate 			 * Positive value indicates a fixed sized array.
31030Sstevel@tonic-gate 			 * 0 or negative value indicates variable sized array.
31040Sstevel@tonic-gate 			 *
31050Sstevel@tonic-gate 			 * For variable sized array, the variable must be
31060Sstevel@tonic-gate 			 * an int member of the structure, with an offset
31070Sstevel@tonic-gate 			 * equal to the absolution value of struct member.
31080Sstevel@tonic-gate 			 */
31090Sstevel@tonic-gate 			if (repeat > pdp->bytes - sizeof (int)) {
31100Sstevel@tonic-gate 				goto failure;	/* wrong offset */
31110Sstevel@tonic-gate 			}
31120Sstevel@tonic-gate 
31130Sstevel@tonic-gate 			if (repeat >= 0) {
31140Sstevel@tonic-gate 				repeat = *((int *)((caddr_t)data + repeat));
31150Sstevel@tonic-gate 			} else {
31160Sstevel@tonic-gate 				repeat = -repeat;
31170Sstevel@tonic-gate 			}
31180Sstevel@tonic-gate 
31190Sstevel@tonic-gate 			/*
31200Sstevel@tonic-gate 			 * next, get the size of the object to be copied
31210Sstevel@tonic-gate 			 */
31220Sstevel@tonic-gate 			size = pdp->ptr[i].size * repeat;
31230Sstevel@tonic-gate 
31240Sstevel@tonic-gate 			/*
31250Sstevel@tonic-gate 			 * Arbitrarily limit the total size of object to be
31260Sstevel@tonic-gate 			 * copied (1 byte to 1/4 page).
31270Sstevel@tonic-gate 			 */
31280Sstevel@tonic-gate 			if ((size <= 0) || (size > (DI_MAX_PRIVDATA - off0))) {
31290Sstevel@tonic-gate 				goto failure;	/* wrong size or too big */
31300Sstevel@tonic-gate 			}
31310Sstevel@tonic-gate 
31320Sstevel@tonic-gate 			/*
31330Sstevel@tonic-gate 			 * Now copy the data
31340Sstevel@tonic-gate 			 */
31350Sstevel@tonic-gate 			*tmp = off0;
31360Sstevel@tonic-gate 			bcopy(ptr, di_mem_addr(st, off + off0), size);
31370Sstevel@tonic-gate 			off0 += DI_ALIGN(size);
31380Sstevel@tonic-gate 		}
31390Sstevel@tonic-gate 	} else {
31400Sstevel@tonic-gate 		goto failure;
31410Sstevel@tonic-gate 	}
31420Sstevel@tonic-gate 
31430Sstevel@tonic-gate success:
31440Sstevel@tonic-gate 	/*
31450Sstevel@tonic-gate 	 * success if reached here
31460Sstevel@tonic-gate 	 */
31470Sstevel@tonic-gate 	no_fault();
31480Sstevel@tonic-gate 	*off_p = off;
31490Sstevel@tonic-gate 
31500Sstevel@tonic-gate 	return (off + off0);
31510Sstevel@tonic-gate 	/*NOTREACHED*/
31520Sstevel@tonic-gate 
31530Sstevel@tonic-gate failure:
31540Sstevel@tonic-gate 	/*
31550Sstevel@tonic-gate 	 * fault occurred
31560Sstevel@tonic-gate 	 */
31570Sstevel@tonic-gate 	no_fault();
31580Sstevel@tonic-gate 	cmn_err(CE_WARN, "devinfo: fault in private data at %p", data);
31590Sstevel@tonic-gate 	*off_p = -1;	/* set private data to indicate error */
31600Sstevel@tonic-gate 
31610Sstevel@tonic-gate 	return (off);
31620Sstevel@tonic-gate }
31630Sstevel@tonic-gate 
31640Sstevel@tonic-gate /*
31650Sstevel@tonic-gate  * get parent private data; on error, returns original offset
31660Sstevel@tonic-gate  */
31670Sstevel@tonic-gate static di_off_t
31680Sstevel@tonic-gate di_getppdata(struct dev_info *node, di_off_t *off_p, struct di_state *st)
31690Sstevel@tonic-gate {
31700Sstevel@tonic-gate 	int off;
31710Sstevel@tonic-gate 	struct di_priv_format *ppdp;
31720Sstevel@tonic-gate 
31730Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_getppdata:\n"));
31740Sstevel@tonic-gate 
31750Sstevel@tonic-gate 	/* find the parent data format */
31760Sstevel@tonic-gate 	if ((ppdp = di_match_drv_name(node, st, DI_MATCH_PARENT)) == NULL) {
31770Sstevel@tonic-gate 		off = *off_p;
31780Sstevel@tonic-gate 		*off_p = 0;	/* set parent data to none */
31790Sstevel@tonic-gate 		return (off);
31800Sstevel@tonic-gate 	}
31810Sstevel@tonic-gate 
31820Sstevel@tonic-gate 	return (di_getprvdata(ppdp, ddi_get_parent_data((dev_info_t *)node),
31830Sstevel@tonic-gate 	    off_p, st));
31840Sstevel@tonic-gate }
31850Sstevel@tonic-gate 
31860Sstevel@tonic-gate /*
31870Sstevel@tonic-gate  * get parent private data; returns original offset
31880Sstevel@tonic-gate  */
31890Sstevel@tonic-gate static di_off_t
31900Sstevel@tonic-gate di_getdpdata(struct dev_info *node, di_off_t *off_p, struct di_state *st)
31910Sstevel@tonic-gate {
31920Sstevel@tonic-gate 	int off;
31930Sstevel@tonic-gate 	struct di_priv_format *dpdp;
31940Sstevel@tonic-gate 
31950Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_getdpdata:"));
31960Sstevel@tonic-gate 
31970Sstevel@tonic-gate 	/* find the parent data format */
31980Sstevel@tonic-gate 	if ((dpdp = di_match_drv_name(node, st, DI_MATCH_DRIVER)) == NULL) {
31990Sstevel@tonic-gate 		off = *off_p;
32000Sstevel@tonic-gate 		*off_p = 0;	/* set driver data to none */
32010Sstevel@tonic-gate 		return (off);
32020Sstevel@tonic-gate 	}
32030Sstevel@tonic-gate 
32040Sstevel@tonic-gate 	return (di_getprvdata(dpdp, ddi_get_driver_private((dev_info_t *)node),
32050Sstevel@tonic-gate 	    off_p, st));
32060Sstevel@tonic-gate }
32070Sstevel@tonic-gate 
32080Sstevel@tonic-gate /*
32090Sstevel@tonic-gate  * The driver is stateful across DINFOCPYALL and DINFOUSRLD.
32100Sstevel@tonic-gate  * This function encapsulates the state machine:
32110Sstevel@tonic-gate  *
32120Sstevel@tonic-gate  *	-> IOC_IDLE -> IOC_SNAP -> IOC_DONE -> IOC_COPY ->
32130Sstevel@tonic-gate  *	|		SNAPSHOT		USRLD	 |
32140Sstevel@tonic-gate  *	--------------------------------------------------
32150Sstevel@tonic-gate  *
32160Sstevel@tonic-gate  * Returns 0 on success and -1 on failure
32170Sstevel@tonic-gate  */
32180Sstevel@tonic-gate static int
32190Sstevel@tonic-gate di_setstate(struct di_state *st, int new_state)
32200Sstevel@tonic-gate {
32210Sstevel@tonic-gate 	int ret = 0;
32220Sstevel@tonic-gate 
32230Sstevel@tonic-gate 	mutex_enter(&di_lock);
32240Sstevel@tonic-gate 	switch (new_state) {
32250Sstevel@tonic-gate 	case IOC_IDLE:
32260Sstevel@tonic-gate 	case IOC_DONE:
32270Sstevel@tonic-gate 		break;
32280Sstevel@tonic-gate 	case IOC_SNAP:
32290Sstevel@tonic-gate 		if (st->di_iocstate != IOC_IDLE)
32300Sstevel@tonic-gate 			ret = -1;
32310Sstevel@tonic-gate 		break;
32320Sstevel@tonic-gate 	case IOC_COPY:
32330Sstevel@tonic-gate 		if (st->di_iocstate != IOC_DONE)
32340Sstevel@tonic-gate 			ret = -1;
32350Sstevel@tonic-gate 		break;
32360Sstevel@tonic-gate 	default:
32370Sstevel@tonic-gate 		ret = -1;
32380Sstevel@tonic-gate 	}
32390Sstevel@tonic-gate 
32400Sstevel@tonic-gate 	if (ret == 0)
32410Sstevel@tonic-gate 		st->di_iocstate = new_state;
32420Sstevel@tonic-gate 	else
32430Sstevel@tonic-gate 		cmn_err(CE_NOTE, "incorrect state transition from %d to %d",
32440Sstevel@tonic-gate 		    st->di_iocstate, new_state);
32450Sstevel@tonic-gate 	mutex_exit(&di_lock);
32460Sstevel@tonic-gate 	return (ret);
32470Sstevel@tonic-gate }
32480Sstevel@tonic-gate 
32490Sstevel@tonic-gate /*
32500Sstevel@tonic-gate  * We cannot assume the presence of the entire
32510Sstevel@tonic-gate  * snapshot in this routine. All we are guaranteed
32520Sstevel@tonic-gate  * is the di_all struct + 1 byte (for root_path)
32530Sstevel@tonic-gate  */
32540Sstevel@tonic-gate static int
32550Sstevel@tonic-gate header_plus_one_ok(struct di_all *all)
32560Sstevel@tonic-gate {
32570Sstevel@tonic-gate 	/*
32580Sstevel@tonic-gate 	 * Refuse to read old versions
32590Sstevel@tonic-gate 	 */
32600Sstevel@tonic-gate 	if (all->version != DI_SNAPSHOT_VERSION) {
32610Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "bad version: 0x%x", all->version));
32620Sstevel@tonic-gate 		return (0);
32630Sstevel@tonic-gate 	}
32640Sstevel@tonic-gate 
32650Sstevel@tonic-gate 	if (all->cache_magic != DI_CACHE_MAGIC) {
32660Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "bad magic #: 0x%x", all->cache_magic));
32670Sstevel@tonic-gate 		return (0);
32680Sstevel@tonic-gate 	}
32690Sstevel@tonic-gate 
32700Sstevel@tonic-gate 	if (all->snapshot_time <= 0) {
32710Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "bad timestamp: %ld", all->snapshot_time));
32720Sstevel@tonic-gate 		return (0);
32730Sstevel@tonic-gate 	}
32740Sstevel@tonic-gate 
32750Sstevel@tonic-gate 	if (all->top_devinfo == 0) {
32760Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "NULL top devinfo"));
32770Sstevel@tonic-gate 		return (0);
32780Sstevel@tonic-gate 	}
32790Sstevel@tonic-gate 
32800Sstevel@tonic-gate 	if (all->map_size < sizeof (*all) + 1) {
32810Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "bad map size: %u", all->map_size));
32820Sstevel@tonic-gate 		return (0);
32830Sstevel@tonic-gate 	}
32840Sstevel@tonic-gate 
32850Sstevel@tonic-gate 	if (all->root_path[0] != '/' || all->root_path[1] != '\0') {
32860Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "bad rootpath: %c%c",
32870Sstevel@tonic-gate 		    all->root_path[0], all->root_path[1]));
32880Sstevel@tonic-gate 		return (0);
32890Sstevel@tonic-gate 	}
32900Sstevel@tonic-gate 
32910Sstevel@tonic-gate 	/*
32920Sstevel@tonic-gate 	 * We can't check checksum here as we just have the header
32930Sstevel@tonic-gate 	 */
32940Sstevel@tonic-gate 
32950Sstevel@tonic-gate 	return (1);
32960Sstevel@tonic-gate }
32970Sstevel@tonic-gate 
32980Sstevel@tonic-gate static int
32990Sstevel@tonic-gate chunk_write(struct vnode *vp, offset_t off, caddr_t buf, size_t len)
33000Sstevel@tonic-gate {
33010Sstevel@tonic-gate 	rlim64_t	rlimit;
33020Sstevel@tonic-gate 	ssize_t		resid;
33030Sstevel@tonic-gate 	int		error = 0;
33040Sstevel@tonic-gate 
33050Sstevel@tonic-gate 
33060Sstevel@tonic-gate 	rlimit = RLIM64_INFINITY;
33070Sstevel@tonic-gate 
33080Sstevel@tonic-gate 	while (len) {
33090Sstevel@tonic-gate 		resid = 0;
33100Sstevel@tonic-gate 		error = vn_rdwr(UIO_WRITE, vp, buf, len, off,
33110Sstevel@tonic-gate 		    UIO_SYSSPACE, FSYNC, rlimit, kcred, &resid);
33120Sstevel@tonic-gate 
33130Sstevel@tonic-gate 		if (error || resid < 0) {
33140Sstevel@tonic-gate 			error = error ? error : EIO;
33150Sstevel@tonic-gate 			CACHE_DEBUG((DI_ERR, "write error: %d", error));
33160Sstevel@tonic-gate 			break;
33170Sstevel@tonic-gate 		}
33180Sstevel@tonic-gate 
33190Sstevel@tonic-gate 		/*
33200Sstevel@tonic-gate 		 * Check if we are making progress
33210Sstevel@tonic-gate 		 */
33220Sstevel@tonic-gate 		if (resid >= len) {
33230Sstevel@tonic-gate 			error = ENOSPC;
33240Sstevel@tonic-gate 			break;
33250Sstevel@tonic-gate 		}
33260Sstevel@tonic-gate 		buf += len - resid;
33270Sstevel@tonic-gate 		off += len - resid;
33280Sstevel@tonic-gate 		len = resid;
33290Sstevel@tonic-gate 	}
33300Sstevel@tonic-gate 
33310Sstevel@tonic-gate 	return (error);
33320Sstevel@tonic-gate }
33330Sstevel@tonic-gate 
33340Sstevel@tonic-gate extern int modrootloaded;
33350Sstevel@tonic-gate 
33360Sstevel@tonic-gate static void
33370Sstevel@tonic-gate di_cache_write(struct di_cache *cache)
33380Sstevel@tonic-gate {
33390Sstevel@tonic-gate 	struct di_all	*all;
33400Sstevel@tonic-gate 	struct vnode	*vp;
33410Sstevel@tonic-gate 	int		oflags;
33420Sstevel@tonic-gate 	size_t		map_size;
33430Sstevel@tonic-gate 	size_t		chunk;
33440Sstevel@tonic-gate 	offset_t	off;
33450Sstevel@tonic-gate 	int		error;
33460Sstevel@tonic-gate 	char		*buf;
33470Sstevel@tonic-gate 
33480Sstevel@tonic-gate 	ASSERT(DI_CACHE_LOCKED(*cache));
33490Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
33500Sstevel@tonic-gate 
33510Sstevel@tonic-gate 	if (cache->cache_size == 0) {
33520Sstevel@tonic-gate 		ASSERT(cache->cache_data == NULL);
33530Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "Empty cache. Skipping write"));
33540Sstevel@tonic-gate 		return;
33550Sstevel@tonic-gate 	}
33560Sstevel@tonic-gate 
33570Sstevel@tonic-gate 	ASSERT(cache->cache_size > 0);
33580Sstevel@tonic-gate 	ASSERT(cache->cache_data);
33590Sstevel@tonic-gate 
33600Sstevel@tonic-gate 	if (!modrootloaded || rootvp == NULL || vn_is_readonly(rootvp)) {
33610Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "Can't write to rootFS. Skipping write"));
33620Sstevel@tonic-gate 		return;
33630Sstevel@tonic-gate 	}
33640Sstevel@tonic-gate 
33650Sstevel@tonic-gate 	all = (struct di_all *)cache->cache_data;
33660Sstevel@tonic-gate 
33670Sstevel@tonic-gate 	if (!header_plus_one_ok(all)) {
33680Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "Invalid header. Skipping write"));
33690Sstevel@tonic-gate 		return;
33700Sstevel@tonic-gate 	}
33710Sstevel@tonic-gate 
33720Sstevel@tonic-gate 	ASSERT(strcmp(all->root_path, "/") == 0);
33730Sstevel@tonic-gate 
33740Sstevel@tonic-gate 	/*
33750Sstevel@tonic-gate 	 * The cache_size is the total allocated memory for the cache.
33760Sstevel@tonic-gate 	 * The map_size is the actual size of valid data in the cache.
33770Sstevel@tonic-gate 	 * map_size may be smaller than cache_size but cannot exceed
33780Sstevel@tonic-gate 	 * cache_size.
33790Sstevel@tonic-gate 	 */
33800Sstevel@tonic-gate 	if (all->map_size > cache->cache_size) {
33810Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "map_size (0x%x) > cache_size (0x%x)."
33820Sstevel@tonic-gate 		    " Skipping write", all->map_size, cache->cache_size));
33830Sstevel@tonic-gate 		return;
33840Sstevel@tonic-gate 	}
33850Sstevel@tonic-gate 
33860Sstevel@tonic-gate 	/*
33870Sstevel@tonic-gate 	 * First unlink the temp file
33880Sstevel@tonic-gate 	 */
33890Sstevel@tonic-gate 	error = vn_remove(DI_CACHE_TEMP, UIO_SYSSPACE, RMFILE);
33900Sstevel@tonic-gate 	if (error && error != ENOENT) {
33910Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "%s: unlink failed: %d",
33920Sstevel@tonic-gate 		    DI_CACHE_TEMP, error));
33930Sstevel@tonic-gate 	}
33940Sstevel@tonic-gate 
33950Sstevel@tonic-gate 	if (error == EROFS) {
33960Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "RDONLY FS. Skipping write"));
33970Sstevel@tonic-gate 		return;
33980Sstevel@tonic-gate 	}
33990Sstevel@tonic-gate 
34000Sstevel@tonic-gate 	vp = NULL;
34010Sstevel@tonic-gate 	oflags = (FCREAT|FWRITE);
34020Sstevel@tonic-gate 	if (error = vn_open(DI_CACHE_TEMP, UIO_SYSSPACE, oflags,
34030Sstevel@tonic-gate 	    DI_CACHE_PERMS, &vp, CRCREAT, 0)) {
34040Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "%s: create failed: %d",
34050Sstevel@tonic-gate 		    DI_CACHE_TEMP, error));
34060Sstevel@tonic-gate 		return;
34070Sstevel@tonic-gate 	}
34080Sstevel@tonic-gate 
34090Sstevel@tonic-gate 	ASSERT(vp);
34100Sstevel@tonic-gate 
34110Sstevel@tonic-gate 	/*
34120Sstevel@tonic-gate 	 * Paranoid: Check if the file is on a read-only FS
34130Sstevel@tonic-gate 	 */
34140Sstevel@tonic-gate 	if (vn_is_readonly(vp)) {
34150Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "cannot write: readonly FS"));
34160Sstevel@tonic-gate 		goto fail;
34170Sstevel@tonic-gate 	}
34180Sstevel@tonic-gate 
34190Sstevel@tonic-gate 	/*
34200Sstevel@tonic-gate 	 * Note that we only write map_size bytes to disk - this saves
34210Sstevel@tonic-gate 	 * space as the actual cache size may be larger than size of
34220Sstevel@tonic-gate 	 * valid data in the cache.
34230Sstevel@tonic-gate 	 * Another advantage is that it makes verification of size
34240Sstevel@tonic-gate 	 * easier when the file is read later.
34250Sstevel@tonic-gate 	 */
34260Sstevel@tonic-gate 	map_size = all->map_size;
34270Sstevel@tonic-gate 	off = 0;
34280Sstevel@tonic-gate 	buf = cache->cache_data;
34290Sstevel@tonic-gate 
34300Sstevel@tonic-gate 	while (map_size) {
34310Sstevel@tonic-gate 		ASSERT(map_size > 0);
34320Sstevel@tonic-gate 		/*
34330Sstevel@tonic-gate 		 * Write in chunks so that VM system
34340Sstevel@tonic-gate 		 * is not overwhelmed
34350Sstevel@tonic-gate 		 */
34360Sstevel@tonic-gate 		if (map_size > di_chunk * PAGESIZE)
34370Sstevel@tonic-gate 			chunk = di_chunk * PAGESIZE;
34380Sstevel@tonic-gate 		else
34390Sstevel@tonic-gate 			chunk = map_size;
34400Sstevel@tonic-gate 
34410Sstevel@tonic-gate 		error = chunk_write(vp, off, buf, chunk);
34420Sstevel@tonic-gate 		if (error) {
34430Sstevel@tonic-gate 			CACHE_DEBUG((DI_ERR, "write failed: off=0x%x: %d",
34440Sstevel@tonic-gate 			    off, error));
34450Sstevel@tonic-gate 			goto fail;
34460Sstevel@tonic-gate 		}
34470Sstevel@tonic-gate 
34480Sstevel@tonic-gate 		off += chunk;
34490Sstevel@tonic-gate 		buf += chunk;
34500Sstevel@tonic-gate 		map_size -= chunk;
34510Sstevel@tonic-gate 
34520Sstevel@tonic-gate 		/* Give pageout a chance to run */
34530Sstevel@tonic-gate 		delay(1);
34540Sstevel@tonic-gate 	}
34550Sstevel@tonic-gate 
34560Sstevel@tonic-gate 	/*
34570Sstevel@tonic-gate 	 * Now sync the file and close it
34580Sstevel@tonic-gate 	 */
34590Sstevel@tonic-gate 	if (error = VOP_FSYNC(vp, FSYNC, kcred)) {
34600Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "FSYNC failed: %d", error));
34610Sstevel@tonic-gate 	}
34620Sstevel@tonic-gate 
34630Sstevel@tonic-gate 	if (error = VOP_CLOSE(vp, oflags, 1, (offset_t)0, kcred)) {
34640Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "close() failed: %d", error));
34650Sstevel@tonic-gate 		VN_RELE(vp);
34660Sstevel@tonic-gate 		return;
34670Sstevel@tonic-gate 	}
34680Sstevel@tonic-gate 
34690Sstevel@tonic-gate 	VN_RELE(vp);
34700Sstevel@tonic-gate 
34710Sstevel@tonic-gate 	/*
34720Sstevel@tonic-gate 	 * Now do the rename
34730Sstevel@tonic-gate 	 */
34740Sstevel@tonic-gate 	if (error = vn_rename(DI_CACHE_TEMP, DI_CACHE_FILE, UIO_SYSSPACE)) {
34750Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "rename failed: %d", error));
34760Sstevel@tonic-gate 		return;
34770Sstevel@tonic-gate 	}
34780Sstevel@tonic-gate 
34790Sstevel@tonic-gate 	CACHE_DEBUG((DI_INFO, "Cache write successful."));
34800Sstevel@tonic-gate 
34810Sstevel@tonic-gate 	return;
34820Sstevel@tonic-gate 
34830Sstevel@tonic-gate fail:
34840Sstevel@tonic-gate 	(void) VOP_CLOSE(vp, oflags, 1, (offset_t)0, kcred);
34850Sstevel@tonic-gate 	VN_RELE(vp);
34860Sstevel@tonic-gate }
34870Sstevel@tonic-gate 
34880Sstevel@tonic-gate 
34890Sstevel@tonic-gate /*
34900Sstevel@tonic-gate  * Since we could be called early in boot,
34910Sstevel@tonic-gate  * use kobj_read_file()
34920Sstevel@tonic-gate  */
34930Sstevel@tonic-gate static void
34940Sstevel@tonic-gate di_cache_read(struct di_cache *cache)
34950Sstevel@tonic-gate {
34960Sstevel@tonic-gate 	struct _buf	*file;
34970Sstevel@tonic-gate 	struct di_all	*all;
34980Sstevel@tonic-gate 	int		n;
34990Sstevel@tonic-gate 	size_t		map_size, sz, chunk;
35000Sstevel@tonic-gate 	offset_t	off;
35010Sstevel@tonic-gate 	caddr_t		buf;
35020Sstevel@tonic-gate 	uint32_t	saved_crc, crc;
35030Sstevel@tonic-gate 
35040Sstevel@tonic-gate 	ASSERT(modrootloaded);
35050Sstevel@tonic-gate 	ASSERT(DI_CACHE_LOCKED(*cache));
35060Sstevel@tonic-gate 	ASSERT(cache->cache_data == NULL);
35070Sstevel@tonic-gate 	ASSERT(cache->cache_size == 0);
35080Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
35090Sstevel@tonic-gate 
35100Sstevel@tonic-gate 	file = kobj_open_file(DI_CACHE_FILE);
35110Sstevel@tonic-gate 	if (file == (struct _buf *)-1) {
35120Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "%s: open failed: %d",
35130Sstevel@tonic-gate 		    DI_CACHE_FILE, ENOENT));
35140Sstevel@tonic-gate 		return;
35150Sstevel@tonic-gate 	}
35160Sstevel@tonic-gate 
35170Sstevel@tonic-gate 	/*
35180Sstevel@tonic-gate 	 * Read in the header+root_path first. The root_path must be "/"
35190Sstevel@tonic-gate 	 */
35200Sstevel@tonic-gate 	all = kmem_zalloc(sizeof (*all) + 1, KM_SLEEP);
35210Sstevel@tonic-gate 	n = kobj_read_file(file, (caddr_t)all, sizeof (*all) + 1, 0);
35220Sstevel@tonic-gate 
35230Sstevel@tonic-gate 	if ((n != sizeof (*all) + 1) || !header_plus_one_ok(all)) {
35240Sstevel@tonic-gate 		kmem_free(all, sizeof (*all) + 1);
35250Sstevel@tonic-gate 		kobj_close_file(file);
35260Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "cache header: read error or invalid"));
35270Sstevel@tonic-gate 		return;
35280Sstevel@tonic-gate 	}
35290Sstevel@tonic-gate 
35300Sstevel@tonic-gate 	map_size = all->map_size;
35310Sstevel@tonic-gate 
35320Sstevel@tonic-gate 	kmem_free(all, sizeof (*all) + 1);
35330Sstevel@tonic-gate 
35340Sstevel@tonic-gate 	ASSERT(map_size >= sizeof (*all) + 1);
35350Sstevel@tonic-gate 
35360Sstevel@tonic-gate 	buf = di_cache.cache_data = kmem_alloc(map_size, KM_SLEEP);
35370Sstevel@tonic-gate 	sz = map_size;
35380Sstevel@tonic-gate 	off = 0;
35390Sstevel@tonic-gate 	while (sz) {
35400Sstevel@tonic-gate 		/* Don't overload VM with large reads */
35410Sstevel@tonic-gate 		chunk = (sz > di_chunk * PAGESIZE) ? di_chunk * PAGESIZE : sz;
35420Sstevel@tonic-gate 		n = kobj_read_file(file, buf, chunk, off);
35430Sstevel@tonic-gate 		if (n != chunk) {
35440Sstevel@tonic-gate 			CACHE_DEBUG((DI_ERR, "%s: read error at offset: %lld",
35450Sstevel@tonic-gate 			    DI_CACHE_FILE, off));
35460Sstevel@tonic-gate 			goto fail;
35470Sstevel@tonic-gate 		}
35480Sstevel@tonic-gate 		off += chunk;
35490Sstevel@tonic-gate 		buf += chunk;
35500Sstevel@tonic-gate 		sz -= chunk;
35510Sstevel@tonic-gate 	}
35520Sstevel@tonic-gate 
35530Sstevel@tonic-gate 	ASSERT(off == map_size);
35540Sstevel@tonic-gate 
35550Sstevel@tonic-gate 	/*
35560Sstevel@tonic-gate 	 * Read past expected EOF to verify size.
35570Sstevel@tonic-gate 	 */
35580Sstevel@tonic-gate 	if (kobj_read_file(file, (caddr_t)&sz, 1, off) > 0) {
35590Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "%s: file size changed", DI_CACHE_FILE));
35600Sstevel@tonic-gate 		goto fail;
35610Sstevel@tonic-gate 	}
35620Sstevel@tonic-gate 
35630Sstevel@tonic-gate 	all = (struct di_all *)di_cache.cache_data;
35640Sstevel@tonic-gate 	if (!header_plus_one_ok(all)) {
35650Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "%s: file header changed", DI_CACHE_FILE));
35660Sstevel@tonic-gate 		goto fail;
35670Sstevel@tonic-gate 	}
35680Sstevel@tonic-gate 
35690Sstevel@tonic-gate 	/*
35700Sstevel@tonic-gate 	 * Compute CRC with checksum field in the cache data set to 0
35710Sstevel@tonic-gate 	 */
35720Sstevel@tonic-gate 	saved_crc = all->cache_checksum;
35730Sstevel@tonic-gate 	all->cache_checksum = 0;
35740Sstevel@tonic-gate 	CRC32(crc, di_cache.cache_data, map_size, -1U, crc32_table);
35750Sstevel@tonic-gate 	all->cache_checksum = saved_crc;
35760Sstevel@tonic-gate 
35770Sstevel@tonic-gate 	if (crc != all->cache_checksum) {
35780Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR,
35790Sstevel@tonic-gate 		    "%s: checksum error: expected=0x%x actual=0x%x",
35800Sstevel@tonic-gate 		    DI_CACHE_FILE, all->cache_checksum, crc));
35810Sstevel@tonic-gate 		goto fail;
35820Sstevel@tonic-gate 	}
35830Sstevel@tonic-gate 
35840Sstevel@tonic-gate 	if (all->map_size != map_size) {
35850Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "%s: map size changed", DI_CACHE_FILE));
35860Sstevel@tonic-gate 		goto fail;
35870Sstevel@tonic-gate 	}
35880Sstevel@tonic-gate 
35890Sstevel@tonic-gate 	kobj_close_file(file);
35900Sstevel@tonic-gate 
35910Sstevel@tonic-gate 	di_cache.cache_size = map_size;
35920Sstevel@tonic-gate 
35930Sstevel@tonic-gate 	return;
35940Sstevel@tonic-gate 
35950Sstevel@tonic-gate fail:
35960Sstevel@tonic-gate 	kmem_free(di_cache.cache_data, map_size);
35970Sstevel@tonic-gate 	kobj_close_file(file);
35980Sstevel@tonic-gate 	di_cache.cache_data = NULL;
35990Sstevel@tonic-gate 	di_cache.cache_size = 0;
36000Sstevel@tonic-gate }
36010Sstevel@tonic-gate 
36020Sstevel@tonic-gate 
36030Sstevel@tonic-gate /*
36040Sstevel@tonic-gate  * Checks if arguments are valid for using the cache.
36050Sstevel@tonic-gate  */
36060Sstevel@tonic-gate static int
36070Sstevel@tonic-gate cache_args_valid(struct di_state *st, int *error)
36080Sstevel@tonic-gate {
36090Sstevel@tonic-gate 	ASSERT(error);
36100Sstevel@tonic-gate 	ASSERT(st->mem_size > 0);
36110Sstevel@tonic-gate 	ASSERT(st->memlist != NULL);
36120Sstevel@tonic-gate 
36130Sstevel@tonic-gate 	if (!modrootloaded || !i_ddi_io_initialized()) {
36140Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR,
36150Sstevel@tonic-gate 		    "cache lookup failure: I/O subsystem not inited"));
36160Sstevel@tonic-gate 		*error = ENOTACTIVE;
36170Sstevel@tonic-gate 		return (0);
36180Sstevel@tonic-gate 	}
36190Sstevel@tonic-gate 
36200Sstevel@tonic-gate 	/*
36210Sstevel@tonic-gate 	 * No other flags allowed with DINFOCACHE
36220Sstevel@tonic-gate 	 */
36230Sstevel@tonic-gate 	if (st->command != (DINFOCACHE & DIIOC_MASK)) {
36240Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR,
36250Sstevel@tonic-gate 		    "cache lookup failure: bad flags: 0x%x",
36260Sstevel@tonic-gate 		    st->command));
36270Sstevel@tonic-gate 		*error = EINVAL;
36280Sstevel@tonic-gate 		return (0);
36290Sstevel@tonic-gate 	}
36300Sstevel@tonic-gate 
36310Sstevel@tonic-gate 	if (strcmp(DI_ALL_PTR(st)->root_path, "/") != 0) {
36320Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR,
36330Sstevel@tonic-gate 		    "cache lookup failure: bad root: %s",
36340Sstevel@tonic-gate 		    DI_ALL_PTR(st)->root_path));
36350Sstevel@tonic-gate 		*error = EINVAL;
36360Sstevel@tonic-gate 		return (0);
36370Sstevel@tonic-gate 	}
36380Sstevel@tonic-gate 
36390Sstevel@tonic-gate 	CACHE_DEBUG((DI_INFO, "cache lookup args ok: 0x%x", st->command));
36400Sstevel@tonic-gate 
36410Sstevel@tonic-gate 	*error = 0;
36420Sstevel@tonic-gate 
36430Sstevel@tonic-gate 	return (1);
36440Sstevel@tonic-gate }
36450Sstevel@tonic-gate 
36460Sstevel@tonic-gate static int
36470Sstevel@tonic-gate snapshot_is_cacheable(struct di_state *st)
36480Sstevel@tonic-gate {
36490Sstevel@tonic-gate 	ASSERT(st->mem_size > 0);
36500Sstevel@tonic-gate 	ASSERT(st->memlist != NULL);
36510Sstevel@tonic-gate 
36520Sstevel@tonic-gate 	if (st->command != (DI_CACHE_SNAPSHOT_FLAGS & DIIOC_MASK)) {
36530Sstevel@tonic-gate 		CACHE_DEBUG((DI_INFO,
36540Sstevel@tonic-gate 		    "not cacheable: incompatible flags: 0x%x",
36550Sstevel@tonic-gate 		    st->command));
36560Sstevel@tonic-gate 		return (0);
36570Sstevel@tonic-gate 	}
36580Sstevel@tonic-gate 
36590Sstevel@tonic-gate 	if (strcmp(DI_ALL_PTR(st)->root_path, "/") != 0) {
36600Sstevel@tonic-gate 		CACHE_DEBUG((DI_INFO,
36610Sstevel@tonic-gate 		    "not cacheable: incompatible root path: %s",
36620Sstevel@tonic-gate 		    DI_ALL_PTR(st)->root_path));
36630Sstevel@tonic-gate 		return (0);
36640Sstevel@tonic-gate 	}
36650Sstevel@tonic-gate 
36660Sstevel@tonic-gate 	CACHE_DEBUG((DI_INFO, "cacheable snapshot request: 0x%x", st->command));
36670Sstevel@tonic-gate 
36680Sstevel@tonic-gate 	return (1);
36690Sstevel@tonic-gate }
36700Sstevel@tonic-gate 
36710Sstevel@tonic-gate static int
36720Sstevel@tonic-gate di_cache_lookup(struct di_state *st)
36730Sstevel@tonic-gate {
36740Sstevel@tonic-gate 	size_t	rval;
36750Sstevel@tonic-gate 	int	cache_valid;
36760Sstevel@tonic-gate 
36770Sstevel@tonic-gate 	ASSERT(cache_args_valid(st, &cache_valid));
36780Sstevel@tonic-gate 	ASSERT(modrootloaded);
36790Sstevel@tonic-gate 
36800Sstevel@tonic-gate 	DI_CACHE_LOCK(di_cache);
36810Sstevel@tonic-gate 
36820Sstevel@tonic-gate 	/*
36830Sstevel@tonic-gate 	 * The following assignment determines the validity
36840Sstevel@tonic-gate 	 * of the cache as far as this snapshot is concerned.
36850Sstevel@tonic-gate 	 */
36860Sstevel@tonic-gate 	cache_valid = di_cache.cache_valid;
36870Sstevel@tonic-gate 
36880Sstevel@tonic-gate 	if (cache_valid && di_cache.cache_data == NULL) {
36890Sstevel@tonic-gate 		di_cache_read(&di_cache);
36900Sstevel@tonic-gate 		/* check for read or file error */
36910Sstevel@tonic-gate 		if (di_cache.cache_data == NULL)
36920Sstevel@tonic-gate 			cache_valid = 0;
36930Sstevel@tonic-gate 	}
36940Sstevel@tonic-gate 
36950Sstevel@tonic-gate 	if (cache_valid) {
36960Sstevel@tonic-gate 		/*
36970Sstevel@tonic-gate 		 * Ok, the cache was valid as of this particular
36980Sstevel@tonic-gate 		 * snapshot. Copy the cached snapshot. This is safe
36990Sstevel@tonic-gate 		 * to do as the cache cannot be freed (we hold the
37000Sstevel@tonic-gate 		 * cache lock). Free the memory allocated in di_state
37010Sstevel@tonic-gate 		 * up until this point - we will simply copy everything
37020Sstevel@tonic-gate 		 * in the cache.
37030Sstevel@tonic-gate 		 */
37040Sstevel@tonic-gate 
37050Sstevel@tonic-gate 		ASSERT(di_cache.cache_data != NULL);
37060Sstevel@tonic-gate 		ASSERT(di_cache.cache_size > 0);
37070Sstevel@tonic-gate 
37080Sstevel@tonic-gate 		di_freemem(st);
37090Sstevel@tonic-gate 
37100Sstevel@tonic-gate 		rval = 0;
37110Sstevel@tonic-gate 		if (di_cache2mem(&di_cache, st) > 0) {
37120Sstevel@tonic-gate 
37130Sstevel@tonic-gate 			ASSERT(DI_ALL_PTR(st));
37140Sstevel@tonic-gate 
37150Sstevel@tonic-gate 			/*
37160Sstevel@tonic-gate 			 * map_size is size of valid data in the
37170Sstevel@tonic-gate 			 * cached snapshot and may be less than
37180Sstevel@tonic-gate 			 * size of the cache.
37190Sstevel@tonic-gate 			 */
37200Sstevel@tonic-gate 			rval = DI_ALL_PTR(st)->map_size;
37210Sstevel@tonic-gate 
37220Sstevel@tonic-gate 			ASSERT(rval >= sizeof (struct di_all));
37230Sstevel@tonic-gate 			ASSERT(rval <= di_cache.cache_size);
37240Sstevel@tonic-gate 		}
37250Sstevel@tonic-gate 	} else {
37260Sstevel@tonic-gate 		/*
37270Sstevel@tonic-gate 		 * The cache isn't valid, we need to take a snapshot.
37280Sstevel@tonic-gate 		 * Set the command flags appropriately
37290Sstevel@tonic-gate 		 */
37300Sstevel@tonic-gate 		ASSERT(st->command == (DINFOCACHE & DIIOC_MASK));
37310Sstevel@tonic-gate 		st->command = (DI_CACHE_SNAPSHOT_FLAGS & DIIOC_MASK);
37320Sstevel@tonic-gate 		rval = di_cache_update(st);
37330Sstevel@tonic-gate 		st->command = (DINFOCACHE & DIIOC_MASK);
37340Sstevel@tonic-gate 	}
37350Sstevel@tonic-gate 
37360Sstevel@tonic-gate 	DI_CACHE_UNLOCK(di_cache);
37370Sstevel@tonic-gate 
37380Sstevel@tonic-gate 	/*
37390Sstevel@tonic-gate 	 * For cached snapshots, the devinfo driver always returns
37400Sstevel@tonic-gate 	 * a snapshot rooted at "/".
37410Sstevel@tonic-gate 	 */
37420Sstevel@tonic-gate 	ASSERT(rval == 0 || strcmp(DI_ALL_PTR(st)->root_path, "/") == 0);
37430Sstevel@tonic-gate 
37440Sstevel@tonic-gate 	return (rval);
37450Sstevel@tonic-gate }
37460Sstevel@tonic-gate 
37470Sstevel@tonic-gate /*
37480Sstevel@tonic-gate  * This is a forced update of the cache  - the previous state of the cache
37490Sstevel@tonic-gate  * may be:
37500Sstevel@tonic-gate  *	- unpopulated
37510Sstevel@tonic-gate  *	- populated and invalid
37520Sstevel@tonic-gate  *	- populated and valid
37530Sstevel@tonic-gate  */
37540Sstevel@tonic-gate static int
37550Sstevel@tonic-gate di_cache_update(struct di_state *st)
37560Sstevel@tonic-gate {
37570Sstevel@tonic-gate 	int rval;
37580Sstevel@tonic-gate 	uint32_t crc;
37590Sstevel@tonic-gate 	struct di_all *all;
37600Sstevel@tonic-gate 
37610Sstevel@tonic-gate 	ASSERT(DI_CACHE_LOCKED(di_cache));
37620Sstevel@tonic-gate 	ASSERT(snapshot_is_cacheable(st));
37630Sstevel@tonic-gate 
37640Sstevel@tonic-gate 	/*
37650Sstevel@tonic-gate 	 * Free the in-core cache and the on-disk file (if they exist)
37660Sstevel@tonic-gate 	 */
37670Sstevel@tonic-gate 	i_ddi_di_cache_free(&di_cache);
37680Sstevel@tonic-gate 
37690Sstevel@tonic-gate 	/*
37700Sstevel@tonic-gate 	 * Set valid flag before taking the snapshot,
37710Sstevel@tonic-gate 	 * so that any invalidations that arrive
37720Sstevel@tonic-gate 	 * during or after the snapshot are not
37730Sstevel@tonic-gate 	 * removed by us.
37740Sstevel@tonic-gate 	 */
37750Sstevel@tonic-gate 	atomic_or_32(&di_cache.cache_valid, 1);
37760Sstevel@tonic-gate 
37770Sstevel@tonic-gate 	modunload_disable();
37780Sstevel@tonic-gate 	rval = di_snapshot(st);
37790Sstevel@tonic-gate 	modunload_enable();
37800Sstevel@tonic-gate 
37810Sstevel@tonic-gate 	if (rval == 0) {
37820Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "can't update cache: bad snapshot"));
37830Sstevel@tonic-gate 		return (0);
37840Sstevel@tonic-gate 	}
37850Sstevel@tonic-gate 
37860Sstevel@tonic-gate 	DI_ALL_PTR(st)->map_size = rval;
37870Sstevel@tonic-gate 
37880Sstevel@tonic-gate 	if (di_mem2cache(st, &di_cache) == 0) {
37890Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "can't update cache: copy failed"));
37900Sstevel@tonic-gate 		return (0);
37910Sstevel@tonic-gate 	}
37920Sstevel@tonic-gate 
37930Sstevel@tonic-gate 	ASSERT(di_cache.cache_data);
37940Sstevel@tonic-gate 	ASSERT(di_cache.cache_size > 0);
37950Sstevel@tonic-gate 
37960Sstevel@tonic-gate 	/*
37970Sstevel@tonic-gate 	 * Now that we have cached the snapshot, compute its checksum.
37980Sstevel@tonic-gate 	 * The checksum is only computed over the valid data in the
37990Sstevel@tonic-gate 	 * cache, not the entire cache.
38000Sstevel@tonic-gate 	 * Also, set all the fields (except checksum) before computing
38010Sstevel@tonic-gate 	 * checksum.
38020Sstevel@tonic-gate 	 */
38030Sstevel@tonic-gate 	all = (struct di_all *)di_cache.cache_data;
38040Sstevel@tonic-gate 	all->cache_magic = DI_CACHE_MAGIC;
38050Sstevel@tonic-gate 	all->map_size = rval;
38060Sstevel@tonic-gate 
38070Sstevel@tonic-gate 	ASSERT(all->cache_checksum == 0);
38080Sstevel@tonic-gate 	CRC32(crc, di_cache.cache_data, all->map_size, -1U, crc32_table);
38090Sstevel@tonic-gate 	all->cache_checksum = crc;
38100Sstevel@tonic-gate 
38110Sstevel@tonic-gate 	di_cache_write(&di_cache);
38120Sstevel@tonic-gate 
38130Sstevel@tonic-gate 	return (rval);
38140Sstevel@tonic-gate }
38150Sstevel@tonic-gate 
38160Sstevel@tonic-gate static void
38170Sstevel@tonic-gate di_cache_print(di_cache_debug_t msglevel, char *fmt, ...)
38180Sstevel@tonic-gate {
38190Sstevel@tonic-gate 	va_list	ap;
38200Sstevel@tonic-gate 
38210Sstevel@tonic-gate 	if (di_cache_debug <= DI_QUIET)
38220Sstevel@tonic-gate 		return;
38230Sstevel@tonic-gate 
38240Sstevel@tonic-gate 	if (di_cache_debug < msglevel)
38250Sstevel@tonic-gate 		return;
38260Sstevel@tonic-gate 
38270Sstevel@tonic-gate 	switch (msglevel) {
38280Sstevel@tonic-gate 		case DI_ERR:
38290Sstevel@tonic-gate 			msglevel = CE_WARN;
38300Sstevel@tonic-gate 			break;
38310Sstevel@tonic-gate 		case DI_INFO:
38320Sstevel@tonic-gate 		case DI_TRACE:
38330Sstevel@tonic-gate 		default:
38340Sstevel@tonic-gate 			msglevel = CE_NOTE;
38350Sstevel@tonic-gate 			break;
38360Sstevel@tonic-gate 	}
38370Sstevel@tonic-gate 
38380Sstevel@tonic-gate 	va_start(ap, fmt);
38390Sstevel@tonic-gate 	vcmn_err(msglevel, fmt, ap);
38400Sstevel@tonic-gate 	va_end(ap);
38410Sstevel@tonic-gate }
3842