xref: /titanic_44/usr/src/uts/common/io/devinfo.c (revision c3b0fe9b0b861ed2fc3439edc2fc9527a48fcd29)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*c3b0fe9bScth  * Common Development and Distribution License (the "License").
6*c3b0fe9bScth  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22737d277aScth  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * driver for accessing kernel devinfo tree.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
337c478bd9Sstevel@tonic-gate #include <sys/debug.h>
347c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
357c478bd9Sstevel@tonic-gate #include <sys/conf.h>
367c478bd9Sstevel@tonic-gate #include <sys/file.h>
377c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
387c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
417c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
427c478bd9Sstevel@tonic-gate #include <sys/sunldi_impl.h>
437c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
447c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
457c478bd9Sstevel@tonic-gate #include <sys/sunmdi.h>
467c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
477c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
487c478bd9Sstevel@tonic-gate #include <sys/mdi_impldefs.h>
497c478bd9Sstevel@tonic-gate #include <sys/devinfo_impl.h>
507c478bd9Sstevel@tonic-gate #include <sys/thread.h>
517c478bd9Sstevel@tonic-gate #include <sys/modhash.h>
527c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
537c478bd9Sstevel@tonic-gate #include <util/qsort.h>
547c478bd9Sstevel@tonic-gate #include <sys/disp.h>
557c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
567c478bd9Sstevel@tonic-gate #include <sys/crc32.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #ifdef DEBUG
607c478bd9Sstevel@tonic-gate static int di_debug;
617c478bd9Sstevel@tonic-gate #define	dcmn_err(args) if (di_debug >= 1) cmn_err args
627c478bd9Sstevel@tonic-gate #define	dcmn_err2(args) if (di_debug >= 2) cmn_err args
637c478bd9Sstevel@tonic-gate #define	dcmn_err3(args) if (di_debug >= 3) cmn_err args
647c478bd9Sstevel@tonic-gate #else
657c478bd9Sstevel@tonic-gate #define	dcmn_err(args) /* nothing */
667c478bd9Sstevel@tonic-gate #define	dcmn_err2(args) /* nothing */
677c478bd9Sstevel@tonic-gate #define	dcmn_err3(args) /* nothing */
687c478bd9Sstevel@tonic-gate #endif
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * We partition the space of devinfo minor nodes equally between the full and
727c478bd9Sstevel@tonic-gate  * unprivileged versions of the driver.  The even-numbered minor nodes are the
737c478bd9Sstevel@tonic-gate  * full version, while the odd-numbered ones are the read-only version.
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate static int di_max_opens = 32;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #define	DI_FULL_PARENT		0
787c478bd9Sstevel@tonic-gate #define	DI_READONLY_PARENT	1
797c478bd9Sstevel@tonic-gate #define	DI_NODE_SPECIES		2
807c478bd9Sstevel@tonic-gate #define	DI_UNPRIVILEGED_NODE(x)	(((x) % 2) != 0)
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #define	IOC_IDLE	0	/* snapshot ioctl states */
837c478bd9Sstevel@tonic-gate #define	IOC_SNAP	1	/* snapshot in progress */
847c478bd9Sstevel@tonic-gate #define	IOC_DONE	2	/* snapshot done, but not copied out */
857c478bd9Sstevel@tonic-gate #define	IOC_COPY	3	/* copyout in progress */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  * Keep max alignment so we can move snapshot to different platforms
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate #define	DI_ALIGN(addr)	((addr + 7l) & ~7l)
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate  * To avoid wasting memory, make a linked list of memory chunks.
947c478bd9Sstevel@tonic-gate  * Size of each chunk is buf_size.
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate struct di_mem {
977c478bd9Sstevel@tonic-gate 	struct di_mem *next;	/* link to next chunk */
987c478bd9Sstevel@tonic-gate 	char *buf;		/* contiguous kernel memory */
997c478bd9Sstevel@tonic-gate 	size_t buf_size;	/* size of buf in bytes */
1007c478bd9Sstevel@tonic-gate 	devmap_cookie_t cook;	/* cookie from ddi_umem_alloc */
1017c478bd9Sstevel@tonic-gate };
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  * This is a stack for walking the tree without using recursion.
1057c478bd9Sstevel@tonic-gate  * When the devinfo tree height is above some small size, one
1067c478bd9Sstevel@tonic-gate  * gets watchdog resets on sun4m.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate struct di_stack {
1097c478bd9Sstevel@tonic-gate 	void		*offset[MAX_TREE_DEPTH];
1107c478bd9Sstevel@tonic-gate 	struct dev_info *dip[MAX_TREE_DEPTH];
1117c478bd9Sstevel@tonic-gate 	int		circ[MAX_TREE_DEPTH];
1127c478bd9Sstevel@tonic-gate 	int		depth;	/* depth of current node to be copied */
1137c478bd9Sstevel@tonic-gate };
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate #define	TOP_OFFSET(stack)	\
1167c478bd9Sstevel@tonic-gate 	((di_off_t *)(stack)->offset[(stack)->depth - 1])
1177c478bd9Sstevel@tonic-gate #define	TOP_NODE(stack)		\
1187c478bd9Sstevel@tonic-gate 	((stack)->dip[(stack)->depth - 1])
1197c478bd9Sstevel@tonic-gate #define	PARENT_OFFSET(stack)	\
1207c478bd9Sstevel@tonic-gate 	((di_off_t *)(stack)->offset[(stack)->depth - 2])
1217c478bd9Sstevel@tonic-gate #define	EMPTY_STACK(stack)	((stack)->depth == 0)
1227c478bd9Sstevel@tonic-gate #define	POP_STACK(stack)	{ \
1237c478bd9Sstevel@tonic-gate 	ndi_devi_exit((dev_info_t *)TOP_NODE(stack), \
1247c478bd9Sstevel@tonic-gate 		(stack)->circ[(stack)->depth - 1]); \
1257c478bd9Sstevel@tonic-gate 	((stack)->depth--); \
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate #define	PUSH_STACK(stack, node, offp)	{ \
1287c478bd9Sstevel@tonic-gate 	ASSERT(node != NULL); \
1297c478bd9Sstevel@tonic-gate 	ndi_devi_enter((dev_info_t *)node, &(stack)->circ[(stack)->depth]); \
1307c478bd9Sstevel@tonic-gate 	(stack)->dip[(stack)->depth] = (node); \
1317c478bd9Sstevel@tonic-gate 	(stack)->offset[(stack)->depth] = (void *)(offp); \
1327c478bd9Sstevel@tonic-gate 	((stack)->depth)++; \
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate #define	DI_ALL_PTR(s)	((struct di_all *)di_mem_addr((s), 0))
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * With devfs, the device tree has no global locks. The device tree is
1397c478bd9Sstevel@tonic-gate  * dynamic and dips may come and go if they are not locked locally. Under
1407c478bd9Sstevel@tonic-gate  * these conditions, pointers are no longer reliable as unique IDs.
1417c478bd9Sstevel@tonic-gate  * Specifically, these pointers cannot be used as keys for hash tables
1427c478bd9Sstevel@tonic-gate  * as the same devinfo structure may be freed in one part of the tree only
1437c478bd9Sstevel@tonic-gate  * to be allocated as the structure for a different device in another
1447c478bd9Sstevel@tonic-gate  * part of the tree. This can happen if DR and the snapshot are
1457c478bd9Sstevel@tonic-gate  * happening concurrently.
1467c478bd9Sstevel@tonic-gate  * The following data structures act as keys for devinfo nodes and
1477c478bd9Sstevel@tonic-gate  * pathinfo nodes.
1487c478bd9Sstevel@tonic-gate  */
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate enum di_ktype {
1517c478bd9Sstevel@tonic-gate 	DI_DKEY = 1,
1527c478bd9Sstevel@tonic-gate 	DI_PKEY = 2
1537c478bd9Sstevel@tonic-gate };
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate struct di_dkey {
1567c478bd9Sstevel@tonic-gate 	dev_info_t	*dk_dip;
1577c478bd9Sstevel@tonic-gate 	major_t		dk_major;
1587c478bd9Sstevel@tonic-gate 	int		dk_inst;
159fa9e4066Sahrens 	pnode_t		dk_nodeid;
1607c478bd9Sstevel@tonic-gate };
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate struct di_pkey {
1637c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pk_pip;
1647c478bd9Sstevel@tonic-gate 	char		*pk_path_addr;
1657c478bd9Sstevel@tonic-gate 	dev_info_t	*pk_client;
1667c478bd9Sstevel@tonic-gate 	dev_info_t	*pk_phci;
1677c478bd9Sstevel@tonic-gate };
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate struct di_key {
1707c478bd9Sstevel@tonic-gate 	enum di_ktype	k_type;
1717c478bd9Sstevel@tonic-gate 	union {
1727c478bd9Sstevel@tonic-gate 		struct di_dkey dkey;
1737c478bd9Sstevel@tonic-gate 		struct di_pkey pkey;
1747c478bd9Sstevel@tonic-gate 	} k_u;
1757c478bd9Sstevel@tonic-gate };
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate struct i_lnode;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate typedef struct i_link {
1817c478bd9Sstevel@tonic-gate 	/*
1827c478bd9Sstevel@tonic-gate 	 * If a di_link struct representing this i_link struct makes it
1837c478bd9Sstevel@tonic-gate 	 * into the snapshot, then self will point to the offset of
1847c478bd9Sstevel@tonic-gate 	 * the di_link struct in the snapshot
1857c478bd9Sstevel@tonic-gate 	 */
1867c478bd9Sstevel@tonic-gate 	di_off_t	self;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	int		spec_type;	/* block or char access type */
1897c478bd9Sstevel@tonic-gate 	struct i_lnode	*src_lnode;	/* src i_lnode */
1907c478bd9Sstevel@tonic-gate 	struct i_lnode	*tgt_lnode;	/* tgt i_lnode */
1917c478bd9Sstevel@tonic-gate 	struct i_link	*src_link_next;	/* next src i_link /w same i_lnode */
1927c478bd9Sstevel@tonic-gate 	struct i_link	*tgt_link_next;	/* next tgt i_link /w same i_lnode */
1937c478bd9Sstevel@tonic-gate } i_link_t;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate typedef struct i_lnode {
1967c478bd9Sstevel@tonic-gate 	/*
1977c478bd9Sstevel@tonic-gate 	 * If a di_lnode struct representing this i_lnode struct makes it
1987c478bd9Sstevel@tonic-gate 	 * into the snapshot, then self will point to the offset of
1997c478bd9Sstevel@tonic-gate 	 * the di_lnode struct in the snapshot
2007c478bd9Sstevel@tonic-gate 	 */
2017c478bd9Sstevel@tonic-gate 	di_off_t	self;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/*
2047c478bd9Sstevel@tonic-gate 	 * used for hashing and comparing i_lnodes
2057c478bd9Sstevel@tonic-gate 	 */
2067c478bd9Sstevel@tonic-gate 	int		modid;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * public information describing a link endpoint
2107c478bd9Sstevel@tonic-gate 	 */
2117c478bd9Sstevel@tonic-gate 	struct di_node	*di_node;	/* di_node in snapshot */
2127c478bd9Sstevel@tonic-gate 	dev_t		devt;		/* devt */
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/*
2157c478bd9Sstevel@tonic-gate 	 * i_link ptr to links coming into this i_lnode node
2167c478bd9Sstevel@tonic-gate 	 * (this i_lnode is the target of these i_links)
2177c478bd9Sstevel@tonic-gate 	 */
2187c478bd9Sstevel@tonic-gate 	i_link_t	*link_in;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	/*
2217c478bd9Sstevel@tonic-gate 	 * i_link ptr to links going out of this i_lnode node
2227c478bd9Sstevel@tonic-gate 	 * (this i_lnode is the source of these i_links)
2237c478bd9Sstevel@tonic-gate 	 */
2247c478bd9Sstevel@tonic-gate 	i_link_t	*link_out;
2257c478bd9Sstevel@tonic-gate } i_lnode_t;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate /*
2287c478bd9Sstevel@tonic-gate  * Soft state associated with each instance of driver open.
2297c478bd9Sstevel@tonic-gate  */
2307c478bd9Sstevel@tonic-gate static struct di_state {
2317c478bd9Sstevel@tonic-gate 	di_off_t mem_size;	/* total # bytes in memlist	*/
2327c478bd9Sstevel@tonic-gate 	struct di_mem *memlist;	/* head of memlist		*/
2337c478bd9Sstevel@tonic-gate 	uint_t command;		/* command from ioctl		*/
2347c478bd9Sstevel@tonic-gate 	int di_iocstate;	/* snapshot ioctl state		*/
2357c478bd9Sstevel@tonic-gate 	mod_hash_t *reg_dip_hash;
2367c478bd9Sstevel@tonic-gate 	mod_hash_t *reg_pip_hash;
2377c478bd9Sstevel@tonic-gate 	int lnode_count;
2387c478bd9Sstevel@tonic-gate 	int link_count;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	mod_hash_t *lnode_hash;
2417c478bd9Sstevel@tonic-gate 	mod_hash_t *link_hash;
2427c478bd9Sstevel@tonic-gate } **di_states;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate static kmutex_t di_lock;	/* serialize instance assignment */
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate typedef enum {
2477c478bd9Sstevel@tonic-gate 	DI_QUIET = 0,	/* DI_QUIET must always be 0 */
2487c478bd9Sstevel@tonic-gate 	DI_ERR,
2497c478bd9Sstevel@tonic-gate 	DI_INFO,
2507c478bd9Sstevel@tonic-gate 	DI_TRACE,
2517c478bd9Sstevel@tonic-gate 	DI_TRACE1,
2527c478bd9Sstevel@tonic-gate 	DI_TRACE2
2537c478bd9Sstevel@tonic-gate } di_cache_debug_t;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate static uint_t	di_chunk = 32;		/* I/O chunk size in pages */
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate #define	DI_CACHE_LOCK(c)	(mutex_enter(&(c).cache_lock))
2587c478bd9Sstevel@tonic-gate #define	DI_CACHE_UNLOCK(c)	(mutex_exit(&(c).cache_lock))
2597c478bd9Sstevel@tonic-gate #define	DI_CACHE_LOCKED(c)	(mutex_owned(&(c).cache_lock))
2607c478bd9Sstevel@tonic-gate 
2613c34adc5Sramat /*
2623c34adc5Sramat  * Check that whole device tree is being configured as a pre-condition for
2633c34adc5Sramat  * cleaning up /etc/devices files.
2643c34adc5Sramat  */
2653c34adc5Sramat #define	DEVICES_FILES_CLEANABLE(st)	\
2663c34adc5Sramat 	(((st)->command & DINFOSUBTREE) && ((st)->command & DINFOFORCE) && \
2673c34adc5Sramat 	strcmp(DI_ALL_PTR(st)->root_path, "/") == 0)
2683c34adc5Sramat 
2697c478bd9Sstevel@tonic-gate #define	CACHE_DEBUG(args)	\
2707c478bd9Sstevel@tonic-gate 	{ if (di_cache_debug != DI_QUIET) di_cache_print args; }
2717c478bd9Sstevel@tonic-gate 
2728c4f8890Srs135747 static struct phci_walk_arg {
2738c4f8890Srs135747 	di_off_t	off;
2748c4f8890Srs135747 	struct di_state	*st;
2758c4f8890Srs135747 } phci_walk_arg_t;
2768c4f8890Srs135747 
2777c478bd9Sstevel@tonic-gate static int di_open(dev_t *, int, int, cred_t *);
2787c478bd9Sstevel@tonic-gate static int di_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
2797c478bd9Sstevel@tonic-gate static int di_close(dev_t, int, int, cred_t *);
2807c478bd9Sstevel@tonic-gate static int di_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
2817c478bd9Sstevel@tonic-gate static int di_attach(dev_info_t *, ddi_attach_cmd_t);
2827c478bd9Sstevel@tonic-gate static int di_detach(dev_info_t *, ddi_detach_cmd_t);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate static di_off_t di_copyformat(di_off_t, struct di_state *, intptr_t, int);
2853c34adc5Sramat static di_off_t di_snapshot_and_clean(struct di_state *);
2867c478bd9Sstevel@tonic-gate static di_off_t di_copydevnm(di_off_t *, struct di_state *);
2877c478bd9Sstevel@tonic-gate static di_off_t di_copytree(struct dev_info *, di_off_t *, struct di_state *);
2887c478bd9Sstevel@tonic-gate static di_off_t di_copynode(struct di_stack *, struct di_state *);
2897c478bd9Sstevel@tonic-gate static di_off_t di_getmdata(struct ddi_minor_data *, di_off_t *, di_off_t,
2907c478bd9Sstevel@tonic-gate     struct di_state *);
2917c478bd9Sstevel@tonic-gate static di_off_t di_getppdata(struct dev_info *, di_off_t *, struct di_state *);
2927c478bd9Sstevel@tonic-gate static di_off_t di_getdpdata(struct dev_info *, di_off_t *, struct di_state *);
2937c478bd9Sstevel@tonic-gate static di_off_t di_getprop(struct ddi_prop *, di_off_t *,
2947c478bd9Sstevel@tonic-gate     struct di_state *, struct dev_info *, int);
2957c478bd9Sstevel@tonic-gate static void di_allocmem(struct di_state *, size_t);
2967c478bd9Sstevel@tonic-gate static void di_freemem(struct di_state *);
2977c478bd9Sstevel@tonic-gate static void di_copymem(struct di_state *st, caddr_t buf, size_t bufsiz);
2987c478bd9Sstevel@tonic-gate static di_off_t di_checkmem(struct di_state *, di_off_t, size_t);
2997c478bd9Sstevel@tonic-gate static caddr_t di_mem_addr(struct di_state *, di_off_t);
3007c478bd9Sstevel@tonic-gate static int di_setstate(struct di_state *, int);
3017c478bd9Sstevel@tonic-gate static void di_register_dip(struct di_state *, dev_info_t *, di_off_t);
3027c478bd9Sstevel@tonic-gate static void di_register_pip(struct di_state *, mdi_pathinfo_t *, di_off_t);
3037c478bd9Sstevel@tonic-gate static di_off_t di_getpath_data(dev_info_t *, di_off_t *, di_off_t,
3047c478bd9Sstevel@tonic-gate     struct di_state *, int);
3057c478bd9Sstevel@tonic-gate static di_off_t di_getlink_data(di_off_t, struct di_state *);
3067c478bd9Sstevel@tonic-gate static int di_dip_find(struct di_state *st, dev_info_t *node, di_off_t *off_p);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate static int cache_args_valid(struct di_state *st, int *error);
3097c478bd9Sstevel@tonic-gate static int snapshot_is_cacheable(struct di_state *st);
3107c478bd9Sstevel@tonic-gate static int di_cache_lookup(struct di_state *st);
3117c478bd9Sstevel@tonic-gate static int di_cache_update(struct di_state *st);
3127c478bd9Sstevel@tonic-gate static void di_cache_print(di_cache_debug_t msglevel, char *fmt, ...);
3138c4f8890Srs135747 int build_vhci_list(dev_info_t *vh_devinfo, void *arg);
3148c4f8890Srs135747 int build_phci_list(dev_info_t *ph_devinfo, void *arg);
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate static struct cb_ops di_cb_ops = {
3177c478bd9Sstevel@tonic-gate 	di_open,		/* open */
3187c478bd9Sstevel@tonic-gate 	di_close,		/* close */
3197c478bd9Sstevel@tonic-gate 	nodev,			/* strategy */
3207c478bd9Sstevel@tonic-gate 	nodev,			/* print */
3217c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
3227c478bd9Sstevel@tonic-gate 	nodev,			/* read */
3237c478bd9Sstevel@tonic-gate 	nodev,			/* write */
3247c478bd9Sstevel@tonic-gate 	di_ioctl,		/* ioctl */
3257c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
3267c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
3277c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
3287c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
3297c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
3307c478bd9Sstevel@tonic-gate 	NULL,			/* streamtab  */
3317c478bd9Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
3327c478bd9Sstevel@tonic-gate };
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate static struct dev_ops di_ops = {
3357c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
3367c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
3377c478bd9Sstevel@tonic-gate 	di_info,		/* info */
3387c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
3397c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
3407c478bd9Sstevel@tonic-gate 	di_attach,		/* attach */
3417c478bd9Sstevel@tonic-gate 	di_detach,		/* detach */
3427c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
3437c478bd9Sstevel@tonic-gate 	&di_cb_ops,		/* driver operations */
3447c478bd9Sstevel@tonic-gate 	NULL			/* bus operations */
3457c478bd9Sstevel@tonic-gate };
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate /*
3487c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
3497c478bd9Sstevel@tonic-gate  */
3507c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
3517c478bd9Sstevel@tonic-gate 	&mod_driverops,
3527c478bd9Sstevel@tonic-gate 	"DEVINFO Driver %I%",
3537c478bd9Sstevel@tonic-gate 	&di_ops
3547c478bd9Sstevel@tonic-gate };
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
3577c478bd9Sstevel@tonic-gate 	MODREV_1,
3587c478bd9Sstevel@tonic-gate 	&modldrv,
3597c478bd9Sstevel@tonic-gate 	NULL
3607c478bd9Sstevel@tonic-gate };
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate int
3637c478bd9Sstevel@tonic-gate _init(void)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	int	error;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	mutex_init(&di_lock, NULL, MUTEX_DRIVER, NULL);
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	error = mod_install(&modlinkage);
3707c478bd9Sstevel@tonic-gate 	if (error != 0) {
3717c478bd9Sstevel@tonic-gate 		mutex_destroy(&di_lock);
3727c478bd9Sstevel@tonic-gate 		return (error);
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	return (0);
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate int
3797c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate int
3857c478bd9Sstevel@tonic-gate _fini(void)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate 	int	error;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
3907c478bd9Sstevel@tonic-gate 	if (error != 0) {
3917c478bd9Sstevel@tonic-gate 		return (error);
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	mutex_destroy(&di_lock);
3957c478bd9Sstevel@tonic-gate 	return (0);
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate static dev_info_t *di_dip;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4017c478bd9Sstevel@tonic-gate static int
4027c478bd9Sstevel@tonic-gate di_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	int error = DDI_FAILURE;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	switch (infocmd) {
4077c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
4087c478bd9Sstevel@tonic-gate 		*result = (void *)di_dip;
4097c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
4107c478bd9Sstevel@tonic-gate 		break;
4117c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
4127c478bd9Sstevel@tonic-gate 		/*
4137c478bd9Sstevel@tonic-gate 		 * All dev_t's map to the same, single instance.
4147c478bd9Sstevel@tonic-gate 		 */
4157c478bd9Sstevel@tonic-gate 		*result = (void *)0;
4167c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
4177c478bd9Sstevel@tonic-gate 		break;
4187c478bd9Sstevel@tonic-gate 	default:
4197c478bd9Sstevel@tonic-gate 		break;
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	return (error);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate static int
4267c478bd9Sstevel@tonic-gate di_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4277c478bd9Sstevel@tonic-gate {
4287c478bd9Sstevel@tonic-gate 	int error = DDI_FAILURE;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	switch (cmd) {
4317c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
4327c478bd9Sstevel@tonic-gate 		di_states = kmem_zalloc(
4337c478bd9Sstevel@tonic-gate 		    di_max_opens * sizeof (struct di_state *), KM_SLEEP);
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, "devinfo", S_IFCHR,
4367c478bd9Sstevel@tonic-gate 		    DI_FULL_PARENT, DDI_PSEUDO, NULL) == DDI_FAILURE ||
4377c478bd9Sstevel@tonic-gate 		    ddi_create_minor_node(dip, "devinfo,ro", S_IFCHR,
4387c478bd9Sstevel@tonic-gate 		    DI_READONLY_PARENT, DDI_PSEUDO, NULL) == DDI_FAILURE) {
4397c478bd9Sstevel@tonic-gate 			kmem_free(di_states,
4407c478bd9Sstevel@tonic-gate 			    di_max_opens * sizeof (struct di_state *));
4417c478bd9Sstevel@tonic-gate 			ddi_remove_minor_node(dip, NULL);
4427c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
4437c478bd9Sstevel@tonic-gate 		} else {
4447c478bd9Sstevel@tonic-gate 			di_dip = dip;
4457c478bd9Sstevel@tonic-gate 			ddi_report_dev(dip);
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
4487c478bd9Sstevel@tonic-gate 		}
4497c478bd9Sstevel@tonic-gate 		break;
4507c478bd9Sstevel@tonic-gate 	default:
4517c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
4527c478bd9Sstevel@tonic-gate 		break;
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	return (error);
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate static int
4597c478bd9Sstevel@tonic-gate di_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate 	int error = DDI_FAILURE;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	switch (cmd) {
4647c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
4657c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
4667c478bd9Sstevel@tonic-gate 		di_dip = NULL;
4677c478bd9Sstevel@tonic-gate 		kmem_free(di_states, di_max_opens * sizeof (struct di_state *));
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
4707c478bd9Sstevel@tonic-gate 		break;
4717c478bd9Sstevel@tonic-gate 	default:
4727c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
4737c478bd9Sstevel@tonic-gate 		break;
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	return (error);
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate /*
4807c478bd9Sstevel@tonic-gate  * Allow multiple opens by tweaking the dev_t such that it looks like each
4817c478bd9Sstevel@tonic-gate  * open is getting a different minor device.  Each minor gets a separate
4827c478bd9Sstevel@tonic-gate  * entry in the di_states[] table.  Based on the original minor number, we
4837c478bd9Sstevel@tonic-gate  * discriminate opens of the full and read-only nodes.  If all of the instances
4847c478bd9Sstevel@tonic-gate  * of the selected minor node are currently open, we return EAGAIN.
4857c478bd9Sstevel@tonic-gate  */
4867c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4877c478bd9Sstevel@tonic-gate static int
4887c478bd9Sstevel@tonic-gate di_open(dev_t *devp, int flag, int otyp, cred_t *credp)
4897c478bd9Sstevel@tonic-gate {
4907c478bd9Sstevel@tonic-gate 	int m;
4917c478bd9Sstevel@tonic-gate 	minor_t minor_parent = getminor(*devp);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	if (minor_parent != DI_FULL_PARENT &&
4947c478bd9Sstevel@tonic-gate 	    minor_parent != DI_READONLY_PARENT)
4957c478bd9Sstevel@tonic-gate 		return (ENXIO);
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	mutex_enter(&di_lock);
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	for (m = minor_parent; m < di_max_opens; m += DI_NODE_SPECIES) {
5007c478bd9Sstevel@tonic-gate 		if (di_states[m] != NULL)
5017c478bd9Sstevel@tonic-gate 			continue;
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 		di_states[m] = kmem_zalloc(sizeof (struct di_state), KM_SLEEP);
5047c478bd9Sstevel@tonic-gate 		break;	/* It's ours. */
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	if (m >= di_max_opens) {
5087c478bd9Sstevel@tonic-gate 		/*
5097c478bd9Sstevel@tonic-gate 		 * maximum open instance for device reached
5107c478bd9Sstevel@tonic-gate 		 */
5117c478bd9Sstevel@tonic-gate 		mutex_exit(&di_lock);
5127c478bd9Sstevel@tonic-gate 		dcmn_err((CE_WARN, "devinfo: maximum devinfo open reached"));
5137c478bd9Sstevel@tonic-gate 		return (EAGAIN);
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 	mutex_exit(&di_lock);
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	ASSERT(m < di_max_opens);
5187c478bd9Sstevel@tonic-gate 	*devp = makedevice(getmajor(*devp), (minor_t)(m + DI_NODE_SPECIES));
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	dcmn_err((CE_CONT, "di_open: thread = %p, assigned minor = %d\n",
5217c478bd9Sstevel@tonic-gate 		(void *)curthread, m + DI_NODE_SPECIES));
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	return (0);
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5277c478bd9Sstevel@tonic-gate static int
5287c478bd9Sstevel@tonic-gate di_close(dev_t dev, int flag, int otype, cred_t *cred_p)
5297c478bd9Sstevel@tonic-gate {
5307c478bd9Sstevel@tonic-gate 	struct di_state *st;
5317c478bd9Sstevel@tonic-gate 	int m = (int)getminor(dev) - DI_NODE_SPECIES;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	if (m < 0) {
5347c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "closing non-existent devinfo minor %d",
5357c478bd9Sstevel@tonic-gate 		    m + DI_NODE_SPECIES);
5367c478bd9Sstevel@tonic-gate 		return (ENXIO);
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	st = di_states[m];
5407c478bd9Sstevel@tonic-gate 	ASSERT(m < di_max_opens && st != NULL);
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	di_freemem(st);
5437c478bd9Sstevel@tonic-gate 	kmem_free(st, sizeof (struct di_state));
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	/*
5467c478bd9Sstevel@tonic-gate 	 * empty slot in state table
5477c478bd9Sstevel@tonic-gate 	 */
5487c478bd9Sstevel@tonic-gate 	mutex_enter(&di_lock);
5497c478bd9Sstevel@tonic-gate 	di_states[m] = NULL;
5507c478bd9Sstevel@tonic-gate 	dcmn_err((CE_CONT, "di_close: thread = %p, assigned minor = %d\n",
5517c478bd9Sstevel@tonic-gate 		(void *)curthread, m + DI_NODE_SPECIES));
5527c478bd9Sstevel@tonic-gate 	mutex_exit(&di_lock);
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	return (0);
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5597c478bd9Sstevel@tonic-gate static int
5607c478bd9Sstevel@tonic-gate di_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate 	int rv, error;
5637c478bd9Sstevel@tonic-gate 	di_off_t off;
5647c478bd9Sstevel@tonic-gate 	struct di_all *all;
5657c478bd9Sstevel@tonic-gate 	struct di_state *st;
5667c478bd9Sstevel@tonic-gate 	int m = (int)getminor(dev) - DI_NODE_SPECIES;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	major_t i;
5697c478bd9Sstevel@tonic-gate 	char *drv_name;
5707c478bd9Sstevel@tonic-gate 	size_t map_size, size;
5717c478bd9Sstevel@tonic-gate 	struct di_mem *dcp;
5727c478bd9Sstevel@tonic-gate 	int ndi_flags;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	if (m < 0 || m >= di_max_opens) {
5757c478bd9Sstevel@tonic-gate 		return (ENXIO);
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	st = di_states[m];
5797c478bd9Sstevel@tonic-gate 	ASSERT(st != NULL);
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_ioctl: mode = %x, cmd = %x\n", mode, cmd));
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	switch (cmd) {
5847c478bd9Sstevel@tonic-gate 	case DINFOIDENT:
5857c478bd9Sstevel@tonic-gate 		/*
5867c478bd9Sstevel@tonic-gate 		 * This is called from di_init to verify that the driver
5877c478bd9Sstevel@tonic-gate 		 * opened is indeed devinfo. The purpose is to guard against
5887c478bd9Sstevel@tonic-gate 		 * sending ioctl to an unknown driver in case of an
5897c478bd9Sstevel@tonic-gate 		 * unresolved major number conflict during bfu.
5907c478bd9Sstevel@tonic-gate 		 */
5917c478bd9Sstevel@tonic-gate 		*rvalp = DI_MAGIC;
5927c478bd9Sstevel@tonic-gate 		return (0);
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	case DINFOLODRV:
5957c478bd9Sstevel@tonic-gate 		/*
5967c478bd9Sstevel@tonic-gate 		 * Hold an installed driver and return the result
5977c478bd9Sstevel@tonic-gate 		 */
5987c478bd9Sstevel@tonic-gate 		if (DI_UNPRIVILEGED_NODE(m)) {
5997c478bd9Sstevel@tonic-gate 			/*
6007c478bd9Sstevel@tonic-gate 			 * Only the fully enabled instances may issue
6017c478bd9Sstevel@tonic-gate 			 * DINFOLDDRV.
6027c478bd9Sstevel@tonic-gate 			 */
6037c478bd9Sstevel@tonic-gate 			return (EACCES);
6047c478bd9Sstevel@tonic-gate 		}
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 		drv_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
6077c478bd9Sstevel@tonic-gate 		if (ddi_copyin((void *)arg, drv_name, MAXNAMELEN, mode) != 0) {
6087c478bd9Sstevel@tonic-gate 			kmem_free(drv_name, MAXNAMELEN);
6097c478bd9Sstevel@tonic-gate 			return (EFAULT);
6107c478bd9Sstevel@tonic-gate 		}
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 		/*
6137c478bd9Sstevel@tonic-gate 		 * Some 3rd party driver's _init() walks the device tree,
6147c478bd9Sstevel@tonic-gate 		 * so we load the driver module before configuring driver.
6157c478bd9Sstevel@tonic-gate 		 */
6167c478bd9Sstevel@tonic-gate 		i = ddi_name_to_major(drv_name);
6177c478bd9Sstevel@tonic-gate 		if (ddi_hold_driver(i) == NULL) {
6187c478bd9Sstevel@tonic-gate 			kmem_free(drv_name, MAXNAMELEN);
6197c478bd9Sstevel@tonic-gate 			return (ENXIO);
6207c478bd9Sstevel@tonic-gate 		}
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 		ndi_flags = NDI_DEVI_PERSIST | NDI_CONFIG | NDI_NO_EVENT;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 		/*
6257c478bd9Sstevel@tonic-gate 		 * i_ddi_load_drvconf() below will trigger a reprobe
6267c478bd9Sstevel@tonic-gate 		 * via reset_nexus_flags(). NDI_DRV_CONF_REPROBE isn't
6277c478bd9Sstevel@tonic-gate 		 * needed here.
6287c478bd9Sstevel@tonic-gate 		 */
6297c478bd9Sstevel@tonic-gate 		modunload_disable();
6307c478bd9Sstevel@tonic-gate 		(void) i_ddi_load_drvconf(i);
6317c478bd9Sstevel@tonic-gate 		(void) ndi_devi_config_driver(ddi_root_node(), ndi_flags, i);
6327c478bd9Sstevel@tonic-gate 		kmem_free(drv_name, MAXNAMELEN);
6337c478bd9Sstevel@tonic-gate 		ddi_rele_driver(i);
6347c478bd9Sstevel@tonic-gate 		rv = i_ddi_devs_attached(i);
6357c478bd9Sstevel@tonic-gate 		modunload_enable();
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 		i_ddi_di_cache_invalidate(KM_SLEEP);
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 		return ((rv == DDI_SUCCESS)? 0 : ENXIO);
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	case DINFOUSRLD:
6427c478bd9Sstevel@tonic-gate 		/*
6437c478bd9Sstevel@tonic-gate 		 * The case for copying snapshot to userland
6447c478bd9Sstevel@tonic-gate 		 */
6457c478bd9Sstevel@tonic-gate 		if (di_setstate(st, IOC_COPY) == -1)
6467c478bd9Sstevel@tonic-gate 			return (EBUSY);
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 		map_size = ((struct di_all *)di_mem_addr(st, 0))->map_size;
6497c478bd9Sstevel@tonic-gate 		if (map_size == 0) {
6507c478bd9Sstevel@tonic-gate 			(void) di_setstate(st, IOC_DONE);
6517c478bd9Sstevel@tonic-gate 			return (EFAULT);
6527c478bd9Sstevel@tonic-gate 		}
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 		/*
6557c478bd9Sstevel@tonic-gate 		 * copyout the snapshot
6567c478bd9Sstevel@tonic-gate 		 */
6577c478bd9Sstevel@tonic-gate 		map_size = (map_size + PAGEOFFSET) & PAGEMASK;
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 		/*
6607c478bd9Sstevel@tonic-gate 		 * Return the map size, so caller may do a sanity
6617c478bd9Sstevel@tonic-gate 		 * check against the return value of snapshot ioctl()
6627c478bd9Sstevel@tonic-gate 		 */
6637c478bd9Sstevel@tonic-gate 		*rvalp = (int)map_size;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 		/*
6667c478bd9Sstevel@tonic-gate 		 * Copy one chunk at a time
6677c478bd9Sstevel@tonic-gate 		 */
6687c478bd9Sstevel@tonic-gate 		off = 0;
6697c478bd9Sstevel@tonic-gate 		dcp = st->memlist;
6707c478bd9Sstevel@tonic-gate 		while (map_size) {
6717c478bd9Sstevel@tonic-gate 			size = dcp->buf_size;
6727c478bd9Sstevel@tonic-gate 			if (map_size <= size) {
6737c478bd9Sstevel@tonic-gate 				size = map_size;
6747c478bd9Sstevel@tonic-gate 			}
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 			if (ddi_copyout(di_mem_addr(st, off),
6777c478bd9Sstevel@tonic-gate 			    (void *)(arg + off), size, mode) != 0) {
6787c478bd9Sstevel@tonic-gate 				(void) di_setstate(st, IOC_DONE);
6797c478bd9Sstevel@tonic-gate 				return (EFAULT);
6807c478bd9Sstevel@tonic-gate 			}
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 			map_size -= size;
6837c478bd9Sstevel@tonic-gate 			off += size;
6847c478bd9Sstevel@tonic-gate 			dcp = dcp->next;
6857c478bd9Sstevel@tonic-gate 		}
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 		di_freemem(st);
6887c478bd9Sstevel@tonic-gate 		(void) di_setstate(st, IOC_IDLE);
6897c478bd9Sstevel@tonic-gate 		return (0);
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	default:
6927c478bd9Sstevel@tonic-gate 		if ((cmd & ~DIIOC_MASK) != DIIOC) {
6937c478bd9Sstevel@tonic-gate 			/*
6947c478bd9Sstevel@tonic-gate 			 * Invalid ioctl command
6957c478bd9Sstevel@tonic-gate 			 */
6967c478bd9Sstevel@tonic-gate 			return (ENOTTY);
6977c478bd9Sstevel@tonic-gate 		}
6987c478bd9Sstevel@tonic-gate 		/*
6997c478bd9Sstevel@tonic-gate 		 * take a snapshot
7007c478bd9Sstevel@tonic-gate 		 */
7017c478bd9Sstevel@tonic-gate 		st->command = cmd & DIIOC_MASK;
7027c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	/*
7067c478bd9Sstevel@tonic-gate 	 * Obtain enough memory to hold header + rootpath.  We prevent kernel
7077c478bd9Sstevel@tonic-gate 	 * memory exhaustion by freeing any previously allocated snapshot and
7087c478bd9Sstevel@tonic-gate 	 * refusing the operation; otherwise we would be allowing ioctl(),
7097c478bd9Sstevel@tonic-gate 	 * ioctl(), ioctl(), ..., panic.
7107c478bd9Sstevel@tonic-gate 	 */
7117c478bd9Sstevel@tonic-gate 	if (di_setstate(st, IOC_SNAP) == -1)
7127c478bd9Sstevel@tonic-gate 		return (EBUSY);
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	size = sizeof (struct di_all) +
7157c478bd9Sstevel@tonic-gate 	    sizeof (((struct dinfo_io *)(NULL))->root_path);
7167c478bd9Sstevel@tonic-gate 	if (size < PAGESIZE)
7177c478bd9Sstevel@tonic-gate 		size = PAGESIZE;
7187c478bd9Sstevel@tonic-gate 	di_allocmem(st, size);
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	all = (struct di_all *)di_mem_addr(st, 0);
7217c478bd9Sstevel@tonic-gate 	all->devcnt = devcnt;
7227c478bd9Sstevel@tonic-gate 	all->command = st->command;
7237c478bd9Sstevel@tonic-gate 	all->version = DI_SNAPSHOT_VERSION;
7248c4f8890Srs135747 	all->top_vhci_devinfo = 0;	/* filled up by build_vhci_list. */
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	/*
7277c478bd9Sstevel@tonic-gate 	 * Note the endianness in case we need to transport snapshot
7287c478bd9Sstevel@tonic-gate 	 * over the network.
7297c478bd9Sstevel@tonic-gate 	 */
7307c478bd9Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
7317c478bd9Sstevel@tonic-gate 	all->endianness = DI_LITTLE_ENDIAN;
7327c478bd9Sstevel@tonic-gate #else
7337c478bd9Sstevel@tonic-gate 	all->endianness = DI_BIG_ENDIAN;
7347c478bd9Sstevel@tonic-gate #endif
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	/* Copyin ioctl args, store in the snapshot. */
7377c478bd9Sstevel@tonic-gate 	if (copyinstr((void *)arg, all->root_path,
7387c478bd9Sstevel@tonic-gate 	    sizeof (((struct dinfo_io *)(NULL))->root_path), &size) != 0) {
7397c478bd9Sstevel@tonic-gate 		di_freemem(st);
7407c478bd9Sstevel@tonic-gate 		(void) di_setstate(st, IOC_IDLE);
7417c478bd9Sstevel@tonic-gate 		return (EFAULT);
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 
7443c34adc5Sramat 	if ((st->command & DINFOCLEANUP) && !DEVICES_FILES_CLEANABLE(st)) {
7453c34adc5Sramat 		di_freemem(st);
7463c34adc5Sramat 		(void) di_setstate(st, IOC_IDLE);
7473c34adc5Sramat 		return (EINVAL);
7483c34adc5Sramat 	}
7493c34adc5Sramat 
7507c478bd9Sstevel@tonic-gate 	error = 0;
7517c478bd9Sstevel@tonic-gate 	if ((st->command & DINFOCACHE) && !cache_args_valid(st, &error)) {
7527c478bd9Sstevel@tonic-gate 		di_freemem(st);
7537c478bd9Sstevel@tonic-gate 		(void) di_setstate(st, IOC_IDLE);
7547c478bd9Sstevel@tonic-gate 		return (error);
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	off = DI_ALIGN(sizeof (struct di_all) + size);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	/*
7607c478bd9Sstevel@tonic-gate 	 * Only the fully enabled version may force load drivers or read
7617c478bd9Sstevel@tonic-gate 	 * the parent private data from a driver.
7627c478bd9Sstevel@tonic-gate 	 */
7637c478bd9Sstevel@tonic-gate 	if ((st->command & (DINFOPRIVDATA | DINFOFORCE)) != 0 &&
7647c478bd9Sstevel@tonic-gate 	    DI_UNPRIVILEGED_NODE(m)) {
7657c478bd9Sstevel@tonic-gate 		di_freemem(st);
7667c478bd9Sstevel@tonic-gate 		(void) di_setstate(st, IOC_IDLE);
7677c478bd9Sstevel@tonic-gate 		return (EACCES);
7687c478bd9Sstevel@tonic-gate 	}
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	/* Do we need private data? */
7717c478bd9Sstevel@tonic-gate 	if (st->command & DINFOPRIVDATA) {
7727c478bd9Sstevel@tonic-gate 		arg += sizeof (((struct dinfo_io *)(NULL))->root_path);
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
7757c478bd9Sstevel@tonic-gate 		switch (ddi_model_convert_from(mode & FMODELS)) {
7767c478bd9Sstevel@tonic-gate 		case DDI_MODEL_ILP32: {
7777c478bd9Sstevel@tonic-gate 			/*
7787c478bd9Sstevel@tonic-gate 			 * Cannot copy private data from 64-bit kernel
7797c478bd9Sstevel@tonic-gate 			 * to 32-bit app
7807c478bd9Sstevel@tonic-gate 			 */
7817c478bd9Sstevel@tonic-gate 			di_freemem(st);
7827c478bd9Sstevel@tonic-gate 			(void) di_setstate(st, IOC_IDLE);
7837c478bd9Sstevel@tonic-gate 			return (EINVAL);
7847c478bd9Sstevel@tonic-gate 		}
7857c478bd9Sstevel@tonic-gate 		case DDI_MODEL_NONE:
7867c478bd9Sstevel@tonic-gate 			if ((off = di_copyformat(off, st, arg, mode)) == 0) {
7877c478bd9Sstevel@tonic-gate 				di_freemem(st);
7887c478bd9Sstevel@tonic-gate 				(void) di_setstate(st, IOC_IDLE);
7897c478bd9Sstevel@tonic-gate 				return (EFAULT);
7907c478bd9Sstevel@tonic-gate 			}
7917c478bd9Sstevel@tonic-gate 			break;
7927c478bd9Sstevel@tonic-gate 		}
7937c478bd9Sstevel@tonic-gate #else /* !_MULTI_DATAMODEL */
7947c478bd9Sstevel@tonic-gate 		if ((off = di_copyformat(off, st, arg, mode)) == 0) {
7957c478bd9Sstevel@tonic-gate 			di_freemem(st);
7967c478bd9Sstevel@tonic-gate 			(void) di_setstate(st, IOC_IDLE);
7977c478bd9Sstevel@tonic-gate 			return (EFAULT);
7987c478bd9Sstevel@tonic-gate 		}
7997c478bd9Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
8007c478bd9Sstevel@tonic-gate 	}
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	all->top_devinfo = DI_ALIGN(off);
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	/*
8057c478bd9Sstevel@tonic-gate 	 * For cache lookups we reallocate memory from scratch,
8067c478bd9Sstevel@tonic-gate 	 * so the value of "all" is no longer valid.
8077c478bd9Sstevel@tonic-gate 	 */
8087c478bd9Sstevel@tonic-gate 	all = NULL;
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	if (st->command & DINFOCACHE) {
8117c478bd9Sstevel@tonic-gate 		*rvalp = di_cache_lookup(st);
8127c478bd9Sstevel@tonic-gate 	} else if (snapshot_is_cacheable(st)) {
8137c478bd9Sstevel@tonic-gate 		DI_CACHE_LOCK(di_cache);
8147c478bd9Sstevel@tonic-gate 		*rvalp = di_cache_update(st);
8157c478bd9Sstevel@tonic-gate 		DI_CACHE_UNLOCK(di_cache);
8163c34adc5Sramat 	} else
8173c34adc5Sramat 		*rvalp = di_snapshot_and_clean(st);
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	if (*rvalp) {
8207c478bd9Sstevel@tonic-gate 		DI_ALL_PTR(st)->map_size = *rvalp;
8217c478bd9Sstevel@tonic-gate 		(void) di_setstate(st, IOC_DONE);
8227c478bd9Sstevel@tonic-gate 	} else {
8237c478bd9Sstevel@tonic-gate 		di_freemem(st);
8247c478bd9Sstevel@tonic-gate 		(void) di_setstate(st, IOC_IDLE);
8257c478bd9Sstevel@tonic-gate 	}
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	return (0);
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate /*
8317c478bd9Sstevel@tonic-gate  * Get a chunk of memory >= size, for the snapshot
8327c478bd9Sstevel@tonic-gate  */
8337c478bd9Sstevel@tonic-gate static void
8347c478bd9Sstevel@tonic-gate di_allocmem(struct di_state *st, size_t size)
8357c478bd9Sstevel@tonic-gate {
8367c478bd9Sstevel@tonic-gate 	struct di_mem *mem = kmem_zalloc(sizeof (struct di_mem),
8377c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
8387c478bd9Sstevel@tonic-gate 	/*
8397c478bd9Sstevel@tonic-gate 	 * Round up size to nearest power of 2. If it is less
8407c478bd9Sstevel@tonic-gate 	 * than st->mem_size, set it to st->mem_size (i.e.,
8417c478bd9Sstevel@tonic-gate 	 * the mem_size is doubled every time) to reduce the
8427c478bd9Sstevel@tonic-gate 	 * number of memory allocations.
8437c478bd9Sstevel@tonic-gate 	 */
8447c478bd9Sstevel@tonic-gate 	size_t tmp = 1;
8457c478bd9Sstevel@tonic-gate 	while (tmp < size) {
8467c478bd9Sstevel@tonic-gate 		tmp <<= 1;
8477c478bd9Sstevel@tonic-gate 	}
8487c478bd9Sstevel@tonic-gate 	size = (tmp > st->mem_size) ? tmp : st->mem_size;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	mem->buf = ddi_umem_alloc(size, DDI_UMEM_SLEEP, &mem->cook);
8517c478bd9Sstevel@tonic-gate 	mem->buf_size = size;
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_allocmem: mem_size=%x\n", st->mem_size));
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	if (st->mem_size == 0) {	/* first chunk */
8567c478bd9Sstevel@tonic-gate 		st->memlist = mem;
8577c478bd9Sstevel@tonic-gate 	} else {
8587c478bd9Sstevel@tonic-gate 		/*
8597c478bd9Sstevel@tonic-gate 		 * locate end of linked list and add a chunk at the end
8607c478bd9Sstevel@tonic-gate 		 */
8617c478bd9Sstevel@tonic-gate 		struct di_mem *dcp = st->memlist;
8627c478bd9Sstevel@tonic-gate 		while (dcp->next != NULL) {
8637c478bd9Sstevel@tonic-gate 			dcp = dcp->next;
8647c478bd9Sstevel@tonic-gate 		}
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 		dcp->next = mem;
8677c478bd9Sstevel@tonic-gate 	}
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	st->mem_size += size;
8707c478bd9Sstevel@tonic-gate }
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate /*
8737c478bd9Sstevel@tonic-gate  * Copy upto bufsiz bytes of the memlist to buf
8747c478bd9Sstevel@tonic-gate  */
8757c478bd9Sstevel@tonic-gate static void
8767c478bd9Sstevel@tonic-gate di_copymem(struct di_state *st, caddr_t buf, size_t bufsiz)
8777c478bd9Sstevel@tonic-gate {
8787c478bd9Sstevel@tonic-gate 	struct di_mem *dcp;
8797c478bd9Sstevel@tonic-gate 	size_t copysz;
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	if (st->mem_size == 0) {
8827c478bd9Sstevel@tonic-gate 		ASSERT(st->memlist == NULL);
8837c478bd9Sstevel@tonic-gate 		return;
8847c478bd9Sstevel@tonic-gate 	}
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	copysz = 0;
8877c478bd9Sstevel@tonic-gate 	for (dcp = st->memlist; dcp; dcp = dcp->next) {
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 		ASSERT(bufsiz > 0);
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 		if (bufsiz <= dcp->buf_size)
8927c478bd9Sstevel@tonic-gate 			copysz = bufsiz;
8937c478bd9Sstevel@tonic-gate 		else
8947c478bd9Sstevel@tonic-gate 			copysz = dcp->buf_size;
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 		bcopy(dcp->buf, buf, copysz);
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 		buf += copysz;
8997c478bd9Sstevel@tonic-gate 		bufsiz -= copysz;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 		if (bufsiz == 0)
9027c478bd9Sstevel@tonic-gate 			break;
9037c478bd9Sstevel@tonic-gate 	}
9047c478bd9Sstevel@tonic-gate }
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate /*
9077c478bd9Sstevel@tonic-gate  * Free all memory for the snapshot
9087c478bd9Sstevel@tonic-gate  */
9097c478bd9Sstevel@tonic-gate static void
9107c478bd9Sstevel@tonic-gate di_freemem(struct di_state *st)
9117c478bd9Sstevel@tonic-gate {
9127c478bd9Sstevel@tonic-gate 	struct di_mem *dcp, *tmp;
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_freemem\n"));
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	if (st->mem_size) {
9177c478bd9Sstevel@tonic-gate 		dcp = st->memlist;
9187c478bd9Sstevel@tonic-gate 		while (dcp) {	/* traverse the linked list */
9197c478bd9Sstevel@tonic-gate 			tmp = dcp;
9207c478bd9Sstevel@tonic-gate 			dcp = dcp->next;
9217c478bd9Sstevel@tonic-gate 			ddi_umem_free(tmp->cook);
9227c478bd9Sstevel@tonic-gate 			kmem_free(tmp, sizeof (struct di_mem));
9237c478bd9Sstevel@tonic-gate 		}
9247c478bd9Sstevel@tonic-gate 		st->mem_size = 0;
9257c478bd9Sstevel@tonic-gate 		st->memlist = NULL;
9267c478bd9Sstevel@tonic-gate 	}
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	ASSERT(st->mem_size == 0);
9297c478bd9Sstevel@tonic-gate 	ASSERT(st->memlist == NULL);
9307c478bd9Sstevel@tonic-gate }
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate /*
9337c478bd9Sstevel@tonic-gate  * Copies cached data to the di_state structure.
9347c478bd9Sstevel@tonic-gate  * Returns:
9357c478bd9Sstevel@tonic-gate  *	- size of data copied, on SUCCESS
9367c478bd9Sstevel@tonic-gate  *	- 0 on failure
9377c478bd9Sstevel@tonic-gate  */
9387c478bd9Sstevel@tonic-gate static int
9397c478bd9Sstevel@tonic-gate di_cache2mem(struct di_cache *cache, struct di_state *st)
9407c478bd9Sstevel@tonic-gate {
9417c478bd9Sstevel@tonic-gate 	caddr_t	pa;
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	ASSERT(st->mem_size == 0);
9447c478bd9Sstevel@tonic-gate 	ASSERT(st->memlist == NULL);
9457c478bd9Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
9467c478bd9Sstevel@tonic-gate 	ASSERT(DI_CACHE_LOCKED(*cache));
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	if (cache->cache_size == 0) {
9497c478bd9Sstevel@tonic-gate 		ASSERT(cache->cache_data == NULL);
9507c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "Empty cache. Skipping copy"));
9517c478bd9Sstevel@tonic-gate 		return (0);
9527c478bd9Sstevel@tonic-gate 	}
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	ASSERT(cache->cache_data);
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	di_allocmem(st, cache->cache_size);
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	pa = di_mem_addr(st, 0);
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	ASSERT(pa);
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	/*
9637c478bd9Sstevel@tonic-gate 	 * Verify that di_allocmem() allocates contiguous memory,
9647c478bd9Sstevel@tonic-gate 	 * so that it is safe to do straight bcopy()
9657c478bd9Sstevel@tonic-gate 	 */
9667c478bd9Sstevel@tonic-gate 	ASSERT(st->memlist != NULL);
9677c478bd9Sstevel@tonic-gate 	ASSERT(st->memlist->next == NULL);
9687c478bd9Sstevel@tonic-gate 	bcopy(cache->cache_data, pa, cache->cache_size);
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	return (cache->cache_size);
9717c478bd9Sstevel@tonic-gate }
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate /*
9747c478bd9Sstevel@tonic-gate  * Copies a snapshot from di_state to the cache
9757c478bd9Sstevel@tonic-gate  * Returns:
9767c478bd9Sstevel@tonic-gate  *	- 0 on failure
9777c478bd9Sstevel@tonic-gate  *	- size of copied data on success
9787c478bd9Sstevel@tonic-gate  */
9797c478bd9Sstevel@tonic-gate static int
9807c478bd9Sstevel@tonic-gate di_mem2cache(struct di_state *st, struct di_cache *cache)
9817c478bd9Sstevel@tonic-gate {
9827c478bd9Sstevel@tonic-gate 	size_t map_size;
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	ASSERT(cache->cache_size == 0);
9857c478bd9Sstevel@tonic-gate 	ASSERT(cache->cache_data == NULL);
9867c478bd9Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
9877c478bd9Sstevel@tonic-gate 	ASSERT(DI_CACHE_LOCKED(*cache));
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	if (st->mem_size == 0) {
9907c478bd9Sstevel@tonic-gate 		ASSERT(st->memlist == NULL);
9917c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "Empty memlist. Skipping copy"));
9927c478bd9Sstevel@tonic-gate 		return (0);
9937c478bd9Sstevel@tonic-gate 	}
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	ASSERT(st->memlist);
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	/*
9987c478bd9Sstevel@tonic-gate 	 * The size of the memory list may be much larger than the
9997c478bd9Sstevel@tonic-gate 	 * size of valid data (map_size). Cache only the valid data
10007c478bd9Sstevel@tonic-gate 	 */
10017c478bd9Sstevel@tonic-gate 	map_size = DI_ALL_PTR(st)->map_size;
10027c478bd9Sstevel@tonic-gate 	if (map_size == 0 || map_size < sizeof (struct di_all) ||
10037c478bd9Sstevel@tonic-gate 	    map_size > st->mem_size) {
10047c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "cannot cache: bad size: 0x%x", map_size));
10057c478bd9Sstevel@tonic-gate 		return (0);
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	cache->cache_data = kmem_alloc(map_size, KM_SLEEP);
10097c478bd9Sstevel@tonic-gate 	cache->cache_size = map_size;
10107c478bd9Sstevel@tonic-gate 	di_copymem(st, cache->cache_data, cache->cache_size);
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	return (map_size);
10137c478bd9Sstevel@tonic-gate }
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate /*
10167c478bd9Sstevel@tonic-gate  * Make sure there is at least "size" bytes memory left before
10177c478bd9Sstevel@tonic-gate  * going on. Otherwise, start on a new chunk.
10187c478bd9Sstevel@tonic-gate  */
10197c478bd9Sstevel@tonic-gate static di_off_t
10207c478bd9Sstevel@tonic-gate di_checkmem(struct di_state *st, di_off_t off, size_t size)
10217c478bd9Sstevel@tonic-gate {
10227c478bd9Sstevel@tonic-gate 	dcmn_err3((CE_CONT, "di_checkmem: off=%x size=%x\n",
10237c478bd9Sstevel@tonic-gate 			off, (int)size));
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	/*
10267c478bd9Sstevel@tonic-gate 	 * di_checkmem() shouldn't be called with a size of zero.
10277c478bd9Sstevel@tonic-gate 	 * But in case it is, we want to make sure we return a valid
10287c478bd9Sstevel@tonic-gate 	 * offset within the memlist and not an offset that points us
10297c478bd9Sstevel@tonic-gate 	 * at the end of the memlist.
10307c478bd9Sstevel@tonic-gate 	 */
10317c478bd9Sstevel@tonic-gate 	if (size == 0) {
10327c478bd9Sstevel@tonic-gate 		dcmn_err((CE_WARN, "di_checkmem: invalid zero size used"));
10337c478bd9Sstevel@tonic-gate 		size = 1;
10347c478bd9Sstevel@tonic-gate 	}
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	off = DI_ALIGN(off);
10377c478bd9Sstevel@tonic-gate 	if ((st->mem_size - off) < size) {
10387c478bd9Sstevel@tonic-gate 		off = st->mem_size;
10397c478bd9Sstevel@tonic-gate 		di_allocmem(st, size);
10407c478bd9Sstevel@tonic-gate 	}
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	return (off);
10437c478bd9Sstevel@tonic-gate }
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate /*
10467c478bd9Sstevel@tonic-gate  * Copy the private data format from ioctl arg.
10477c478bd9Sstevel@tonic-gate  * On success, the ending offset is returned. On error 0 is returned.
10487c478bd9Sstevel@tonic-gate  */
10497c478bd9Sstevel@tonic-gate static di_off_t
10507c478bd9Sstevel@tonic-gate di_copyformat(di_off_t off, struct di_state *st, intptr_t arg, int mode)
10517c478bd9Sstevel@tonic-gate {
10527c478bd9Sstevel@tonic-gate 	di_off_t size;
10537c478bd9Sstevel@tonic-gate 	struct di_priv_data *priv;
10547c478bd9Sstevel@tonic-gate 	struct di_all *all = (struct di_all *)di_mem_addr(st, 0);
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_copyformat: off=%x, arg=%p mode=%x\n",
10577c478bd9Sstevel@tonic-gate 		off, (void *)arg, mode));
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	/*
10607c478bd9Sstevel@tonic-gate 	 * Copyin data and check version.
10617c478bd9Sstevel@tonic-gate 	 * We only handle private data version 0.
10627c478bd9Sstevel@tonic-gate 	 */
10637c478bd9Sstevel@tonic-gate 	priv = kmem_alloc(sizeof (struct di_priv_data), KM_SLEEP);
10647c478bd9Sstevel@tonic-gate 	if ((ddi_copyin((void *)arg, priv, sizeof (struct di_priv_data),
10657c478bd9Sstevel@tonic-gate 	    mode) != 0) || (priv->version != DI_PRIVDATA_VERSION_0)) {
10667c478bd9Sstevel@tonic-gate 		kmem_free(priv, sizeof (struct di_priv_data));
10677c478bd9Sstevel@tonic-gate 		return (0);
10687c478bd9Sstevel@tonic-gate 	}
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	/*
10717c478bd9Sstevel@tonic-gate 	 * Save di_priv_data copied from userland in snapshot.
10727c478bd9Sstevel@tonic-gate 	 */
10737c478bd9Sstevel@tonic-gate 	all->pd_version = priv->version;
10747c478bd9Sstevel@tonic-gate 	all->n_ppdata = priv->n_parent;
10757c478bd9Sstevel@tonic-gate 	all->n_dpdata = priv->n_driver;
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	/*
10787c478bd9Sstevel@tonic-gate 	 * copyin private data format, modify offset accordingly
10797c478bd9Sstevel@tonic-gate 	 */
10807c478bd9Sstevel@tonic-gate 	if (all->n_ppdata) {	/* parent private data format */
10817c478bd9Sstevel@tonic-gate 		/*
10827c478bd9Sstevel@tonic-gate 		 * check memory
10837c478bd9Sstevel@tonic-gate 		 */
10847c478bd9Sstevel@tonic-gate 		size = all->n_ppdata * sizeof (struct di_priv_format);
10857c478bd9Sstevel@tonic-gate 		off = di_checkmem(st, off, size);
10867c478bd9Sstevel@tonic-gate 		all->ppdata_format = off;
10877c478bd9Sstevel@tonic-gate 		if (ddi_copyin(priv->parent, di_mem_addr(st, off), size,
10887c478bd9Sstevel@tonic-gate 		    mode) != 0) {
10897c478bd9Sstevel@tonic-gate 			kmem_free(priv, sizeof (struct di_priv_data));
10907c478bd9Sstevel@tonic-gate 			return (0);
10917c478bd9Sstevel@tonic-gate 		}
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 		off += size;
10947c478bd9Sstevel@tonic-gate 	}
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	if (all->n_dpdata) {	/* driver private data format */
10977c478bd9Sstevel@tonic-gate 		/*
10987c478bd9Sstevel@tonic-gate 		 * check memory
10997c478bd9Sstevel@tonic-gate 		 */
11007c478bd9Sstevel@tonic-gate 		size = all->n_dpdata * sizeof (struct di_priv_format);
11017c478bd9Sstevel@tonic-gate 		off = di_checkmem(st, off, size);
11027c478bd9Sstevel@tonic-gate 		all->dpdata_format = off;
11037c478bd9Sstevel@tonic-gate 		if (ddi_copyin(priv->driver, di_mem_addr(st, off), size,
11047c478bd9Sstevel@tonic-gate 		    mode) != 0) {
11057c478bd9Sstevel@tonic-gate 			kmem_free(priv, sizeof (struct di_priv_data));
11067c478bd9Sstevel@tonic-gate 			return (0);
11077c478bd9Sstevel@tonic-gate 		}
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 		off += size;
11107c478bd9Sstevel@tonic-gate 	}
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	kmem_free(priv, sizeof (struct di_priv_data));
11137c478bd9Sstevel@tonic-gate 	return (off);
11147c478bd9Sstevel@tonic-gate }
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate /*
11177c478bd9Sstevel@tonic-gate  * Return the real address based on the offset (off) within snapshot
11187c478bd9Sstevel@tonic-gate  */
11197c478bd9Sstevel@tonic-gate static caddr_t
11207c478bd9Sstevel@tonic-gate di_mem_addr(struct di_state *st, di_off_t off)
11217c478bd9Sstevel@tonic-gate {
11227c478bd9Sstevel@tonic-gate 	struct di_mem *dcp = st->memlist;
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	dcmn_err3((CE_CONT, "di_mem_addr: dcp=%p off=%x\n",
11257c478bd9Sstevel@tonic-gate 		(void *)dcp, off));
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	ASSERT(off < st->mem_size);
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	while (off >= dcp->buf_size) {
11307c478bd9Sstevel@tonic-gate 		off -= dcp->buf_size;
11317c478bd9Sstevel@tonic-gate 		dcp = dcp->next;
11327c478bd9Sstevel@tonic-gate 	}
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	dcmn_err3((CE_CONT, "di_mem_addr: new off=%x, return = %p\n",
11357c478bd9Sstevel@tonic-gate 		off, (void *)(dcp->buf + off)));
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	return (dcp->buf + off);
11387c478bd9Sstevel@tonic-gate }
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate /*
11417c478bd9Sstevel@tonic-gate  * Ideally we would use the whole key to derive the hash
11427c478bd9Sstevel@tonic-gate  * value. However, the probability that two keys will
11437c478bd9Sstevel@tonic-gate  * have the same dip (or pip) is very low, so
11447c478bd9Sstevel@tonic-gate  * hashing by dip (or pip) pointer should suffice.
11457c478bd9Sstevel@tonic-gate  */
11467c478bd9Sstevel@tonic-gate static uint_t
11477c478bd9Sstevel@tonic-gate di_hash_byptr(void *arg, mod_hash_key_t key)
11487c478bd9Sstevel@tonic-gate {
11497c478bd9Sstevel@tonic-gate 	struct di_key *dik = key;
11507c478bd9Sstevel@tonic-gate 	size_t rshift;
11517c478bd9Sstevel@tonic-gate 	void *ptr;
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	ASSERT(arg == NULL);
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	switch (dik->k_type) {
11567c478bd9Sstevel@tonic-gate 	case DI_DKEY:
11577c478bd9Sstevel@tonic-gate 		ptr = dik->k_u.dkey.dk_dip;
11587c478bd9Sstevel@tonic-gate 		rshift = highbit(sizeof (struct dev_info));
11597c478bd9Sstevel@tonic-gate 		break;
11607c478bd9Sstevel@tonic-gate 	case DI_PKEY:
11617c478bd9Sstevel@tonic-gate 		ptr = dik->k_u.pkey.pk_pip;
11627c478bd9Sstevel@tonic-gate 		rshift = highbit(sizeof (struct mdi_pathinfo));
11637c478bd9Sstevel@tonic-gate 		break;
11647c478bd9Sstevel@tonic-gate 	default:
11657c478bd9Sstevel@tonic-gate 		panic("devinfo: unknown key type");
11667c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
11677c478bd9Sstevel@tonic-gate 	}
11687c478bd9Sstevel@tonic-gate 	return (mod_hash_byptr((void *)rshift, ptr));
11697c478bd9Sstevel@tonic-gate }
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate static void
11727c478bd9Sstevel@tonic-gate di_key_dtor(mod_hash_key_t key)
11737c478bd9Sstevel@tonic-gate {
11747c478bd9Sstevel@tonic-gate 	char		*path_addr;
11757c478bd9Sstevel@tonic-gate 	struct di_key	*dik = key;
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	switch (dik->k_type) {
11787c478bd9Sstevel@tonic-gate 	case DI_DKEY:
11797c478bd9Sstevel@tonic-gate 		break;
11807c478bd9Sstevel@tonic-gate 	case DI_PKEY:
11817c478bd9Sstevel@tonic-gate 		path_addr = dik->k_u.pkey.pk_path_addr;
11827c478bd9Sstevel@tonic-gate 		if (path_addr)
11837c478bd9Sstevel@tonic-gate 			kmem_free(path_addr, strlen(path_addr) + 1);
11847c478bd9Sstevel@tonic-gate 		break;
11857c478bd9Sstevel@tonic-gate 	default:
11867c478bd9Sstevel@tonic-gate 		panic("devinfo: unknown key type");
11877c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
11887c478bd9Sstevel@tonic-gate 	}
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	kmem_free(dik, sizeof (struct di_key));
11917c478bd9Sstevel@tonic-gate }
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate static int
11947c478bd9Sstevel@tonic-gate di_dkey_cmp(struct di_dkey *dk1, struct di_dkey *dk2)
11957c478bd9Sstevel@tonic-gate {
11967c478bd9Sstevel@tonic-gate 	if (dk1->dk_dip !=  dk2->dk_dip)
11977c478bd9Sstevel@tonic-gate 		return (dk1->dk_dip > dk2->dk_dip ? 1 : -1);
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	if (dk1->dk_major != -1 && dk2->dk_major != -1) {
12007c478bd9Sstevel@tonic-gate 		if (dk1->dk_major !=  dk2->dk_major)
12017c478bd9Sstevel@tonic-gate 			return (dk1->dk_major > dk2->dk_major ? 1 : -1);
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 		if (dk1->dk_inst !=  dk2->dk_inst)
12047c478bd9Sstevel@tonic-gate 			return (dk1->dk_inst > dk2->dk_inst ? 1 : -1);
12057c478bd9Sstevel@tonic-gate 	}
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	if (dk1->dk_nodeid != dk2->dk_nodeid)
12087c478bd9Sstevel@tonic-gate 		return (dk1->dk_nodeid > dk2->dk_nodeid ? 1 : -1);
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 	return (0);
12117c478bd9Sstevel@tonic-gate }
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate static int
12147c478bd9Sstevel@tonic-gate di_pkey_cmp(struct di_pkey *pk1, struct di_pkey *pk2)
12157c478bd9Sstevel@tonic-gate {
12167c478bd9Sstevel@tonic-gate 	char *p1, *p2;
12177c478bd9Sstevel@tonic-gate 	int rv;
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	if (pk1->pk_pip !=  pk2->pk_pip)
12207c478bd9Sstevel@tonic-gate 		return (pk1->pk_pip > pk2->pk_pip ? 1 : -1);
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 	p1 = pk1->pk_path_addr;
12237c478bd9Sstevel@tonic-gate 	p2 = pk2->pk_path_addr;
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	p1 = p1 ? p1 : "";
12267c478bd9Sstevel@tonic-gate 	p2 = p2 ? p2 : "";
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	rv = strcmp(p1, p2);
12297c478bd9Sstevel@tonic-gate 	if (rv)
12307c478bd9Sstevel@tonic-gate 		return (rv > 0  ? 1 : -1);
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	if (pk1->pk_client !=  pk2->pk_client)
12337c478bd9Sstevel@tonic-gate 		return (pk1->pk_client > pk2->pk_client ? 1 : -1);
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	if (pk1->pk_phci !=  pk2->pk_phci)
12367c478bd9Sstevel@tonic-gate 		return (pk1->pk_phci > pk2->pk_phci ? 1 : -1);
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	return (0);
12397c478bd9Sstevel@tonic-gate }
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate static int
12427c478bd9Sstevel@tonic-gate di_key_cmp(mod_hash_key_t key1, mod_hash_key_t key2)
12437c478bd9Sstevel@tonic-gate {
12447c478bd9Sstevel@tonic-gate 	struct di_key *dik1, *dik2;
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 	dik1 = key1;
12477c478bd9Sstevel@tonic-gate 	dik2 = key2;
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 	if (dik1->k_type != dik2->k_type) {
12507c478bd9Sstevel@tonic-gate 		panic("devinfo: mismatched keys");
12517c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
12527c478bd9Sstevel@tonic-gate 	}
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	switch (dik1->k_type) {
12557c478bd9Sstevel@tonic-gate 	case DI_DKEY:
12567c478bd9Sstevel@tonic-gate 		return (di_dkey_cmp(&(dik1->k_u.dkey), &(dik2->k_u.dkey)));
12577c478bd9Sstevel@tonic-gate 	case DI_PKEY:
12587c478bd9Sstevel@tonic-gate 		return (di_pkey_cmp(&(dik1->k_u.pkey), &(dik2->k_u.pkey)));
12597c478bd9Sstevel@tonic-gate 	default:
12607c478bd9Sstevel@tonic-gate 		panic("devinfo: unknown key type");
12617c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
12627c478bd9Sstevel@tonic-gate 	}
12637c478bd9Sstevel@tonic-gate }
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate /*
12667c478bd9Sstevel@tonic-gate  * This is the main function that takes a snapshot
12677c478bd9Sstevel@tonic-gate  */
12687c478bd9Sstevel@tonic-gate static di_off_t
12697c478bd9Sstevel@tonic-gate di_snapshot(struct di_state *st)
12707c478bd9Sstevel@tonic-gate {
12717c478bd9Sstevel@tonic-gate 	di_off_t off;
12727c478bd9Sstevel@tonic-gate 	struct di_all *all;
12737c478bd9Sstevel@tonic-gate 	dev_info_t *rootnode;
12747c478bd9Sstevel@tonic-gate 	char buf[80];
1275dc03c567Scth 	int plen;
1276dc03c567Scth 	char *path;
1277dc03c567Scth 	vnode_t *vp;
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	all = (struct di_all *)di_mem_addr(st, 0);
12807c478bd9Sstevel@tonic-gate 	dcmn_err((CE_CONT, "Taking a snapshot of devinfo tree...\n"));
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	/*
1283dc03c567Scth 	 * Verify path before entrusting it to e_ddi_hold_devi_by_path because
1284dc03c567Scth 	 * some platforms have OBP bugs where executing the NDI_PROMNAME code
1285dc03c567Scth 	 * path against an invalid path results in panic.  The lookupnameat
1286dc03c567Scth 	 * is done relative to rootdir without a leading '/' on "devices/"
1287dc03c567Scth 	 * to force the lookup to occur in the global zone.
1288dc03c567Scth 	 */
1289dc03c567Scth 	plen = strlen("devices/") + strlen(all->root_path) + 1;
1290dc03c567Scth 	path = kmem_alloc(plen, KM_SLEEP);
1291dc03c567Scth 	(void) snprintf(path, plen, "devices/%s", all->root_path);
1292dc03c567Scth 	if (lookupnameat(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp, rootdir)) {
1293dc03c567Scth 		dcmn_err((CE_CONT, "Devinfo node %s not found\n",
1294dc03c567Scth 		    all->root_path));
1295dc03c567Scth 		kmem_free(path, plen);
1296dc03c567Scth 		return (0);
1297dc03c567Scth 	}
1298dc03c567Scth 	kmem_free(path, plen);
1299dc03c567Scth 	VN_RELE(vp);
1300dc03c567Scth 
1301dc03c567Scth 	/*
13027c478bd9Sstevel@tonic-gate 	 * Hold the devinfo node referred by the path.
13037c478bd9Sstevel@tonic-gate 	 */
13047c478bd9Sstevel@tonic-gate 	rootnode = e_ddi_hold_devi_by_path(all->root_path, 0);
13057c478bd9Sstevel@tonic-gate 	if (rootnode == NULL) {
13067c478bd9Sstevel@tonic-gate 		dcmn_err((CE_CONT, "Devinfo node %s not found\n",
13077c478bd9Sstevel@tonic-gate 		    all->root_path));
13087c478bd9Sstevel@tonic-gate 		return (0);
13097c478bd9Sstevel@tonic-gate 	}
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf),
13127c478bd9Sstevel@tonic-gate 	    "devinfo registered dips (statep=%p)", (void *)st);
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 	st->reg_dip_hash = mod_hash_create_extended(buf, 64,
13157c478bd9Sstevel@tonic-gate 	    di_key_dtor, mod_hash_null_valdtor, di_hash_byptr,
13167c478bd9Sstevel@tonic-gate 	    NULL, di_key_cmp, KM_SLEEP);
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf),
13207c478bd9Sstevel@tonic-gate 	    "devinfo registered pips (statep=%p)", (void *)st);
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	st->reg_pip_hash = mod_hash_create_extended(buf, 64,
13237c478bd9Sstevel@tonic-gate 	    di_key_dtor, mod_hash_null_valdtor, di_hash_byptr,
13247c478bd9Sstevel@tonic-gate 	    NULL, di_key_cmp, KM_SLEEP);
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	/*
13277c478bd9Sstevel@tonic-gate 	 * copy the device tree
13287c478bd9Sstevel@tonic-gate 	 */
13297c478bd9Sstevel@tonic-gate 	off = di_copytree(DEVI(rootnode), &all->top_devinfo, st);
13307c478bd9Sstevel@tonic-gate 
13318c4f8890Srs135747 	if (DINFOPATH & st->command) {
13328c4f8890Srs135747 		mdi_walk_vhcis(build_vhci_list, st);
13338c4f8890Srs135747 	}
13348c4f8890Srs135747 
13357c478bd9Sstevel@tonic-gate 	ddi_release_devi(rootnode);
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	/*
13387c478bd9Sstevel@tonic-gate 	 * copy the devnames array
13397c478bd9Sstevel@tonic-gate 	 */
13407c478bd9Sstevel@tonic-gate 	all->devnames = off;
13417c478bd9Sstevel@tonic-gate 	off = di_copydevnm(&all->devnames, st);
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	/* initialize the hash tables */
13457c478bd9Sstevel@tonic-gate 	st->lnode_count = 0;
13467c478bd9Sstevel@tonic-gate 	st->link_count = 0;
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	if (DINFOLYR & st->command) {
13497c478bd9Sstevel@tonic-gate 		off = di_getlink_data(off, st);
13507c478bd9Sstevel@tonic-gate 	}
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	/*
13537c478bd9Sstevel@tonic-gate 	 * Free up hash tables
13547c478bd9Sstevel@tonic-gate 	 */
13557c478bd9Sstevel@tonic-gate 	mod_hash_destroy_hash(st->reg_dip_hash);
13567c478bd9Sstevel@tonic-gate 	mod_hash_destroy_hash(st->reg_pip_hash);
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	/*
13597c478bd9Sstevel@tonic-gate 	 * Record the timestamp now that we are done with snapshot.
13607c478bd9Sstevel@tonic-gate 	 *
13617c478bd9Sstevel@tonic-gate 	 * We compute the checksum later and then only if we cache
13627c478bd9Sstevel@tonic-gate 	 * the snapshot, since checksumming adds some overhead.
13637c478bd9Sstevel@tonic-gate 	 * The checksum is checked later if we read the cache file.
13647c478bd9Sstevel@tonic-gate 	 * from disk.
13657c478bd9Sstevel@tonic-gate 	 *
13667c478bd9Sstevel@tonic-gate 	 * Set checksum field to 0 as CRC is calculated with that
13677c478bd9Sstevel@tonic-gate 	 * field set to 0.
13687c478bd9Sstevel@tonic-gate 	 */
13697c478bd9Sstevel@tonic-gate 	all->snapshot_time = ddi_get_time();
13707c478bd9Sstevel@tonic-gate 	all->cache_checksum = 0;
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	return (off);
13737c478bd9Sstevel@tonic-gate }
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate /*
13763c34adc5Sramat  * Take a snapshot and clean /etc/devices files if DINFOCLEANUP is set
13773c34adc5Sramat  */
13783c34adc5Sramat static di_off_t
13793c34adc5Sramat di_snapshot_and_clean(struct di_state *st)
13803c34adc5Sramat {
13813c34adc5Sramat 	di_off_t	off;
13823c34adc5Sramat 
13833c34adc5Sramat 	modunload_disable();
13843c34adc5Sramat 	off = di_snapshot(st);
13853c34adc5Sramat 	if (off != 0 && (st->command & DINFOCLEANUP)) {
13863c34adc5Sramat 		ASSERT(DEVICES_FILES_CLEANABLE(st));
13873c34adc5Sramat 		/*
13883c34adc5Sramat 		 * Cleanup /etc/devices files:
13893c34adc5Sramat 		 * In order to accurately account for the system configuration
13903c34adc5Sramat 		 * in /etc/devices files, the appropriate drivers must be
13913c34adc5Sramat 		 * fully configured before the cleanup starts.
13923c34adc5Sramat 		 * So enable modunload only after the cleanup.
13933c34adc5Sramat 		 */
13943c34adc5Sramat 		i_ddi_clean_devices_files();
13953c34adc5Sramat 	}
13963c34adc5Sramat 	modunload_enable();
13973c34adc5Sramat 
13983c34adc5Sramat 	return (off);
13993c34adc5Sramat }
14003c34adc5Sramat 
14013c34adc5Sramat /*
14028c4f8890Srs135747  * construct vhci linkage in the snapshot.
14038c4f8890Srs135747  */
14048c4f8890Srs135747 int
14058c4f8890Srs135747 build_vhci_list(dev_info_t *vh_devinfo, void *arg)
14068c4f8890Srs135747 {
14078c4f8890Srs135747 	struct di_all *all;
14088c4f8890Srs135747 	struct di_node *me;
14098c4f8890Srs135747 	struct di_state *st;
14108c4f8890Srs135747 	di_off_t off;
14118c4f8890Srs135747 	struct phci_walk_arg pwa;
14128c4f8890Srs135747 
14138c4f8890Srs135747 	dcmn_err3((CE_CONT, "build_vhci list\n"));
14148c4f8890Srs135747 
14158c4f8890Srs135747 	dcmn_err3((CE_CONT, "vhci node %s, instance #%d\n",
14168c4f8890Srs135747 		DEVI(vh_devinfo)->devi_node_name,
14178c4f8890Srs135747 		DEVI(vh_devinfo)->devi_instance));
14188c4f8890Srs135747 
14198c4f8890Srs135747 	st = (struct di_state *)arg;
14208c4f8890Srs135747 	if (di_dip_find(st, vh_devinfo, &off) != 0) {
14218c4f8890Srs135747 		dcmn_err((CE_WARN, "di_dip_find error for the given node\n"));
14228c4f8890Srs135747 		return (DDI_WALK_TERMINATE);
14238c4f8890Srs135747 	}
14248c4f8890Srs135747 
14258c4f8890Srs135747 	dcmn_err3((CE_CONT, "st->mem_size: %d vh_devinfo off: 0x%x\n",
14268c4f8890Srs135747 		st->mem_size, off));
14278c4f8890Srs135747 
14288c4f8890Srs135747 	all = (struct di_all *)di_mem_addr(st, 0);
14298c4f8890Srs135747 	if (all->top_vhci_devinfo == 0) {
14308c4f8890Srs135747 		all->top_vhci_devinfo = off;
14318c4f8890Srs135747 	} else {
14328c4f8890Srs135747 		me = (struct di_node *)di_mem_addr(st, all->top_vhci_devinfo);
14338c4f8890Srs135747 
14348c4f8890Srs135747 		while (me->next_vhci != 0) {
14358c4f8890Srs135747 			me = (struct di_node *)di_mem_addr(st, me->next_vhci);
14368c4f8890Srs135747 		}
14378c4f8890Srs135747 
14388c4f8890Srs135747 		me->next_vhci = off;
14398c4f8890Srs135747 	}
14408c4f8890Srs135747 
14418c4f8890Srs135747 	pwa.off = off;
14428c4f8890Srs135747 	pwa.st = st;
14438c4f8890Srs135747 	mdi_vhci_walk_phcis(vh_devinfo, build_phci_list, &pwa);
14448c4f8890Srs135747 
14458c4f8890Srs135747 	return (DDI_WALK_CONTINUE);
14468c4f8890Srs135747 }
14478c4f8890Srs135747 
14488c4f8890Srs135747 /*
14498c4f8890Srs135747  * construct phci linkage for the given vhci in the snapshot.
14508c4f8890Srs135747  */
14518c4f8890Srs135747 int
14528c4f8890Srs135747 build_phci_list(dev_info_t *ph_devinfo, void *arg)
14538c4f8890Srs135747 {
14548c4f8890Srs135747 	struct di_node *vh_di_node;
14558c4f8890Srs135747 	struct di_node *me;
14568c4f8890Srs135747 	struct phci_walk_arg *pwa;
14578c4f8890Srs135747 	di_off_t off;
14588c4f8890Srs135747 
14598c4f8890Srs135747 	pwa = (struct phci_walk_arg *)arg;
14608c4f8890Srs135747 
14618c4f8890Srs135747 	dcmn_err3((CE_CONT, "build_phci list for vhci at offset: 0x%x\n",
14628c4f8890Srs135747 		pwa->off));
14638c4f8890Srs135747 
14648c4f8890Srs135747 	vh_di_node = (struct di_node *)di_mem_addr(pwa->st, pwa->off);
14658c4f8890Srs135747 
14668c4f8890Srs135747 	if (di_dip_find(pwa->st, ph_devinfo, &off) != 0) {
14678c4f8890Srs135747 		dcmn_err((CE_WARN, "di_dip_find error for the given node\n"));
14688c4f8890Srs135747 		return (DDI_WALK_TERMINATE);
14698c4f8890Srs135747 	}
14708c4f8890Srs135747 
14718c4f8890Srs135747 	dcmn_err3((CE_CONT, "phci node %s, instance #%d, at offset 0x%x\n",
14728c4f8890Srs135747 		DEVI(ph_devinfo)->devi_node_name,
14738c4f8890Srs135747 		DEVI(ph_devinfo)->devi_instance, off));
14748c4f8890Srs135747 
14758c4f8890Srs135747 	if (vh_di_node->top_phci == 0) {
14768c4f8890Srs135747 		vh_di_node->top_phci = off;
14778c4f8890Srs135747 		return (DDI_WALK_CONTINUE);
14788c4f8890Srs135747 	}
14798c4f8890Srs135747 
14808c4f8890Srs135747 	me = (struct di_node *)di_mem_addr(pwa->st, vh_di_node->top_phci);
14818c4f8890Srs135747 
14828c4f8890Srs135747 	while (me->next_phci != 0) {
14838c4f8890Srs135747 		me = (struct di_node *)di_mem_addr(pwa->st, me->next_phci);
14848c4f8890Srs135747 	}
14858c4f8890Srs135747 	me->next_phci = off;
14868c4f8890Srs135747 
14878c4f8890Srs135747 	return (DDI_WALK_CONTINUE);
14888c4f8890Srs135747 }
14898c4f8890Srs135747 
14908c4f8890Srs135747 /*
14917c478bd9Sstevel@tonic-gate  * Assumes all devinfo nodes in device tree have been snapshotted
14927c478bd9Sstevel@tonic-gate  */
14937c478bd9Sstevel@tonic-gate static void
14947c478bd9Sstevel@tonic-gate snap_driver_list(struct di_state *st, struct devnames *dnp, di_off_t *poff_p)
14957c478bd9Sstevel@tonic-gate {
14967c478bd9Sstevel@tonic-gate 	struct dev_info *node;
14977c478bd9Sstevel@tonic-gate 	struct di_node *me;
14987c478bd9Sstevel@tonic-gate 	di_off_t off;
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 	node = DEVI(dnp->dn_head);
15037c478bd9Sstevel@tonic-gate 	for (; node; node = node->devi_next) {
15047c478bd9Sstevel@tonic-gate 		if (di_dip_find(st, (dev_info_t *)node, &off) != 0)
15057c478bd9Sstevel@tonic-gate 			continue;
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate 		ASSERT(off > 0);
15087c478bd9Sstevel@tonic-gate 		me = (struct di_node *)di_mem_addr(st, off);
15097c478bd9Sstevel@tonic-gate 		ASSERT(me->next == 0 || me->next == -1);
15107c478bd9Sstevel@tonic-gate 		/*
15117c478bd9Sstevel@tonic-gate 		 * Only nodes which were BOUND when they were
15127c478bd9Sstevel@tonic-gate 		 * snapshotted will be added to per-driver list.
15137c478bd9Sstevel@tonic-gate 		 */
15147c478bd9Sstevel@tonic-gate 		if (me->next != -1)
15157c478bd9Sstevel@tonic-gate 			continue;
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate 		*poff_p = off;
15187c478bd9Sstevel@tonic-gate 		poff_p = &me->next;
15197c478bd9Sstevel@tonic-gate 	}
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 	*poff_p = 0;
15227c478bd9Sstevel@tonic-gate }
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate /*
15257c478bd9Sstevel@tonic-gate  * Copy the devnames array, so we have a list of drivers in the snapshot.
15267c478bd9Sstevel@tonic-gate  * Also makes it possible to locate the per-driver devinfo nodes.
15277c478bd9Sstevel@tonic-gate  */
15287c478bd9Sstevel@tonic-gate static di_off_t
15297c478bd9Sstevel@tonic-gate di_copydevnm(di_off_t *off_p, struct di_state *st)
15307c478bd9Sstevel@tonic-gate {
15317c478bd9Sstevel@tonic-gate 	int i;
15327c478bd9Sstevel@tonic-gate 	di_off_t off;
15337c478bd9Sstevel@tonic-gate 	size_t size;
15347c478bd9Sstevel@tonic-gate 	struct di_devnm *dnp;
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_copydevnm: *off_p = %p\n", (void *)off_p));
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	/*
15397c478bd9Sstevel@tonic-gate 	 * make sure there is some allocated memory
15407c478bd9Sstevel@tonic-gate 	 */
15417c478bd9Sstevel@tonic-gate 	size = devcnt * sizeof (struct di_devnm);
15427c478bd9Sstevel@tonic-gate 	off = di_checkmem(st, *off_p, size);
15437c478bd9Sstevel@tonic-gate 	*off_p = off;
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 	dcmn_err((CE_CONT, "Start copying devnamesp[%d] at offset 0x%x\n",
15467c478bd9Sstevel@tonic-gate 		devcnt, off));
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	dnp = (struct di_devnm *)di_mem_addr(st, off);
15497c478bd9Sstevel@tonic-gate 	off += size;
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate 	for (i = 0; i < devcnt; i++) {
15527c478bd9Sstevel@tonic-gate 		if (devnamesp[i].dn_name == NULL) {
15537c478bd9Sstevel@tonic-gate 			continue;
15547c478bd9Sstevel@tonic-gate 		}
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 		/*
15577c478bd9Sstevel@tonic-gate 		 * dn_name is not freed during driver unload or removal.
15587c478bd9Sstevel@tonic-gate 		 *
15597c478bd9Sstevel@tonic-gate 		 * There is a race condition when make_devname() changes
15607c478bd9Sstevel@tonic-gate 		 * dn_name during our strcpy. This should be rare since
15617c478bd9Sstevel@tonic-gate 		 * only add_drv does this. At any rate, we never had a
15627c478bd9Sstevel@tonic-gate 		 * problem with ddi_name_to_major(), which should have
15637c478bd9Sstevel@tonic-gate 		 * the same problem.
15647c478bd9Sstevel@tonic-gate 		 */
15657c478bd9Sstevel@tonic-gate 		dcmn_err2((CE_CONT, "di_copydevnm: %s%d, off=%x\n",
15667c478bd9Sstevel@tonic-gate 			devnamesp[i].dn_name, devnamesp[i].dn_instance,
15677c478bd9Sstevel@tonic-gate 			off));
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 		off = di_checkmem(st, off, strlen(devnamesp[i].dn_name) + 1);
15707c478bd9Sstevel@tonic-gate 		dnp[i].name = off;
15717c478bd9Sstevel@tonic-gate 		(void) strcpy((char *)di_mem_addr(st, off),
15727c478bd9Sstevel@tonic-gate 			devnamesp[i].dn_name);
15737c478bd9Sstevel@tonic-gate 		off += DI_ALIGN(strlen(devnamesp[i].dn_name) + 1);
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 		mutex_enter(&devnamesp[i].dn_lock);
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 		/*
15787c478bd9Sstevel@tonic-gate 		 * Snapshot per-driver node list
15797c478bd9Sstevel@tonic-gate 		 */
15807c478bd9Sstevel@tonic-gate 		snap_driver_list(st, &devnamesp[i], &dnp[i].head);
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate 		/*
15837c478bd9Sstevel@tonic-gate 		 * This is not used by libdevinfo, leave it for now
15847c478bd9Sstevel@tonic-gate 		 */
15857c478bd9Sstevel@tonic-gate 		dnp[i].flags = devnamesp[i].dn_flags;
15867c478bd9Sstevel@tonic-gate 		dnp[i].instance = devnamesp[i].dn_instance;
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 		/*
15897c478bd9Sstevel@tonic-gate 		 * get global properties
15907c478bd9Sstevel@tonic-gate 		 */
15917c478bd9Sstevel@tonic-gate 		if ((DINFOPROP & st->command) &&
15927c478bd9Sstevel@tonic-gate 		    devnamesp[i].dn_global_prop_ptr) {
15937c478bd9Sstevel@tonic-gate 			dnp[i].global_prop = off;
15947c478bd9Sstevel@tonic-gate 			off = di_getprop(
15957c478bd9Sstevel@tonic-gate 			    devnamesp[i].dn_global_prop_ptr->prop_list,
15967c478bd9Sstevel@tonic-gate 			    &dnp[i].global_prop, st, NULL, DI_PROP_GLB_LIST);
15977c478bd9Sstevel@tonic-gate 		}
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 		/*
16007c478bd9Sstevel@tonic-gate 		 * Bit encode driver ops: & bus_ops, cb_ops, & cb_ops->cb_str
16017c478bd9Sstevel@tonic-gate 		 */
16027c478bd9Sstevel@tonic-gate 		if (CB_DRV_INSTALLED(devopsp[i])) {
16037c478bd9Sstevel@tonic-gate 			if (devopsp[i]->devo_cb_ops) {
16047c478bd9Sstevel@tonic-gate 				dnp[i].ops |= DI_CB_OPS;
16057c478bd9Sstevel@tonic-gate 				if (devopsp[i]->devo_cb_ops->cb_str)
16067c478bd9Sstevel@tonic-gate 					dnp[i].ops |= DI_STREAM_OPS;
16077c478bd9Sstevel@tonic-gate 			}
16087c478bd9Sstevel@tonic-gate 			if (NEXUS_DRV(devopsp[i])) {
16097c478bd9Sstevel@tonic-gate 				dnp[i].ops |= DI_BUS_OPS;
16107c478bd9Sstevel@tonic-gate 			}
16117c478bd9Sstevel@tonic-gate 		}
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 		mutex_exit(&devnamesp[i].dn_lock);
16147c478bd9Sstevel@tonic-gate 	}
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 	dcmn_err((CE_CONT, "End copying devnamesp at offset 0x%x\n", off));
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 	return (off);
16197c478bd9Sstevel@tonic-gate }
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate /*
16227c478bd9Sstevel@tonic-gate  * Copy the kernel devinfo tree. The tree and the devnames array forms
16237c478bd9Sstevel@tonic-gate  * the entire snapshot (see also di_copydevnm).
16247c478bd9Sstevel@tonic-gate  */
16257c478bd9Sstevel@tonic-gate static di_off_t
16267c478bd9Sstevel@tonic-gate di_copytree(struct dev_info *root, di_off_t *off_p, struct di_state *st)
16277c478bd9Sstevel@tonic-gate {
16287c478bd9Sstevel@tonic-gate 	di_off_t off;
16297c478bd9Sstevel@tonic-gate 	struct di_stack *dsp = kmem_zalloc(sizeof (struct di_stack), KM_SLEEP);
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 	dcmn_err((CE_CONT, "di_copytree: root = %p, *off_p = %x\n",
16327c478bd9Sstevel@tonic-gate 		(void *)root, *off_p));
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 	/* force attach drivers */
1635737d277aScth 	if (i_ddi_devi_attached((dev_info_t *)root) &&
16367c478bd9Sstevel@tonic-gate 	    (st->command & DINFOSUBTREE) && (st->command & DINFOFORCE)) {
16377c478bd9Sstevel@tonic-gate 		(void) ndi_devi_config((dev_info_t *)root,
16387c478bd9Sstevel@tonic-gate 		    NDI_CONFIG | NDI_DEVI_PERSIST | NDI_NO_EVENT |
16397c478bd9Sstevel@tonic-gate 		    NDI_DRV_CONF_REPROBE);
16407c478bd9Sstevel@tonic-gate 	}
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 	/*
16437c478bd9Sstevel@tonic-gate 	 * Push top_devinfo onto a stack
16447c478bd9Sstevel@tonic-gate 	 *
16457c478bd9Sstevel@tonic-gate 	 * The stack is necessary to avoid recursion, which can overrun
16467c478bd9Sstevel@tonic-gate 	 * the kernel stack.
16477c478bd9Sstevel@tonic-gate 	 */
16487c478bd9Sstevel@tonic-gate 	PUSH_STACK(dsp, root, off_p);
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	/*
16517c478bd9Sstevel@tonic-gate 	 * As long as there is a node on the stack, copy the node.
16527c478bd9Sstevel@tonic-gate 	 * di_copynode() is responsible for pushing and popping
16537c478bd9Sstevel@tonic-gate 	 * child and sibling nodes on the stack.
16547c478bd9Sstevel@tonic-gate 	 */
16557c478bd9Sstevel@tonic-gate 	while (!EMPTY_STACK(dsp)) {
16567c478bd9Sstevel@tonic-gate 		off = di_copynode(dsp, st);
16577c478bd9Sstevel@tonic-gate 	}
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 	/*
16607c478bd9Sstevel@tonic-gate 	 * Free the stack structure
16617c478bd9Sstevel@tonic-gate 	 */
16627c478bd9Sstevel@tonic-gate 	kmem_free(dsp, sizeof (struct di_stack));
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	return (off);
16657c478bd9Sstevel@tonic-gate }
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate /*
16687c478bd9Sstevel@tonic-gate  * This is the core function, which copies all data associated with a single
16697c478bd9Sstevel@tonic-gate  * node into the snapshot. The amount of information is determined by the
16707c478bd9Sstevel@tonic-gate  * ioctl command.
16717c478bd9Sstevel@tonic-gate  */
16727c478bd9Sstevel@tonic-gate static di_off_t
16737c478bd9Sstevel@tonic-gate di_copynode(struct di_stack *dsp, struct di_state *st)
16747c478bd9Sstevel@tonic-gate {
16757c478bd9Sstevel@tonic-gate 	di_off_t off;
16767c478bd9Sstevel@tonic-gate 	struct di_node *me;
16777c478bd9Sstevel@tonic-gate 	struct dev_info *node;
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_copynode: depth = %x\n",
16807c478bd9Sstevel@tonic-gate 			dsp->depth));
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate 	node = TOP_NODE(dsp);
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 	ASSERT(node != NULL);
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 	/*
16877c478bd9Sstevel@tonic-gate 	 * check memory usage, and fix offsets accordingly.
16887c478bd9Sstevel@tonic-gate 	 */
16897c478bd9Sstevel@tonic-gate 	off = di_checkmem(st, *(TOP_OFFSET(dsp)), sizeof (struct di_node));
16907c478bd9Sstevel@tonic-gate 	*(TOP_OFFSET(dsp)) = off;
16917c478bd9Sstevel@tonic-gate 	me = DI_NODE(di_mem_addr(st, off));
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 	dcmn_err((CE_CONT, "copy node %s, instance #%d, at offset 0x%x\n",
16947c478bd9Sstevel@tonic-gate 			node->devi_node_name, node->devi_instance, off));
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 	/*
16977c478bd9Sstevel@tonic-gate 	 * Node parameters:
16987c478bd9Sstevel@tonic-gate 	 * self		-- offset of current node within snapshot
16997c478bd9Sstevel@tonic-gate 	 * nodeid	-- pointer to PROM node (tri-valued)
17007c478bd9Sstevel@tonic-gate 	 * state	-- hot plugging device state
17017c478bd9Sstevel@tonic-gate 	 * node_state	-- devinfo node state (CF1, CF2, etc.)
17027c478bd9Sstevel@tonic-gate 	 */
17037c478bd9Sstevel@tonic-gate 	me->self = off;
17047c478bd9Sstevel@tonic-gate 	me->instance = node->devi_instance;
17057c478bd9Sstevel@tonic-gate 	me->nodeid = node->devi_nodeid;
17067c478bd9Sstevel@tonic-gate 	me->node_class = node->devi_node_class;
17077c478bd9Sstevel@tonic-gate 	me->attributes = node->devi_node_attributes;
17087c478bd9Sstevel@tonic-gate 	me->state = node->devi_state;
17097c478bd9Sstevel@tonic-gate 	me->node_state = node->devi_node_state;
17108c4f8890Srs135747 	me->next_vhci = 0;		/* Filled up by build_vhci_list. */
17118c4f8890Srs135747 	me->top_phci = 0;		/* Filled up by build_phci_list. */
17128c4f8890Srs135747 	me->next_phci = 0;		/* Filled up by build_phci_list. */
17138c4f8890Srs135747 	me->multipath_component = MULTIPATH_COMPONENT_NONE; /* set default. */
17147c478bd9Sstevel@tonic-gate 	me->user_private_data = NULL;
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 	/*
17177c478bd9Sstevel@tonic-gate 	 * Get parent's offset in snapshot from the stack
17187c478bd9Sstevel@tonic-gate 	 * and store it in the current node
17197c478bd9Sstevel@tonic-gate 	 */
17207c478bd9Sstevel@tonic-gate 	if (dsp->depth > 1) {
17217c478bd9Sstevel@tonic-gate 		me->parent = *(PARENT_OFFSET(dsp));
17227c478bd9Sstevel@tonic-gate 	}
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 	/*
17257c478bd9Sstevel@tonic-gate 	 * Save the offset of this di_node in a hash table.
17267c478bd9Sstevel@tonic-gate 	 * This is used later to resolve references to this
17277c478bd9Sstevel@tonic-gate 	 * dip from other parts of the tree (per-driver list,
17287c478bd9Sstevel@tonic-gate 	 * multipathing linkages, layered usage linkages).
17297c478bd9Sstevel@tonic-gate 	 * The key used for the hash table is derived from
17307c478bd9Sstevel@tonic-gate 	 * information in the dip.
17317c478bd9Sstevel@tonic-gate 	 */
17327c478bd9Sstevel@tonic-gate 	di_register_dip(st, (dev_info_t *)node, me->self);
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 	/*
17357c478bd9Sstevel@tonic-gate 	 * increment offset
17367c478bd9Sstevel@tonic-gate 	 */
17377c478bd9Sstevel@tonic-gate 	off += sizeof (struct di_node);
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate #ifdef	DEVID_COMPATIBILITY
17407c478bd9Sstevel@tonic-gate 	/* check for devid as property marker */
17417c478bd9Sstevel@tonic-gate 	if (node->devi_devid) {
17427c478bd9Sstevel@tonic-gate 		ddi_devid_t	devid;
17437c478bd9Sstevel@tonic-gate 		char 		*devidstr;
17447c478bd9Sstevel@tonic-gate 		int		devid_size;
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 		/*
17477c478bd9Sstevel@tonic-gate 		 * The devid is now represented as a property.
17487c478bd9Sstevel@tonic-gate 		 * For micro release compatibility with di_devid interface
17497c478bd9Sstevel@tonic-gate 		 * in libdevinfo we must return it as a binary structure in'
17507c478bd9Sstevel@tonic-gate 		 * the snapshot.  When di_devid is removed from libdevinfo
17517c478bd9Sstevel@tonic-gate 		 * in a future release (and devi_devid is deleted) then
17527c478bd9Sstevel@tonic-gate 		 * code related to DEVID_COMPATIBILITY can be removed.
17537c478bd9Sstevel@tonic-gate 		 */
17547c478bd9Sstevel@tonic-gate 		ASSERT(node->devi_devid == DEVID_COMPATIBILITY);
17557c478bd9Sstevel@tonic-gate /* XXX should be DDI_DEV_T_NONE! */
17567c478bd9Sstevel@tonic-gate 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, (dev_info_t *)node,
17577c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, DEVID_PROP_NAME, &devidstr) ==
17587c478bd9Sstevel@tonic-gate 		    DDI_PROP_SUCCESS) {
17597c478bd9Sstevel@tonic-gate 			if (ddi_devid_str_decode(devidstr, &devid, NULL) ==
17607c478bd9Sstevel@tonic-gate 			    DDI_SUCCESS) {
17617c478bd9Sstevel@tonic-gate 				devid_size = ddi_devid_sizeof(devid);
17627c478bd9Sstevel@tonic-gate 				off = di_checkmem(st, off, devid_size);
17637c478bd9Sstevel@tonic-gate 				me->devid = off;
17647c478bd9Sstevel@tonic-gate 				bcopy(devid,
17657c478bd9Sstevel@tonic-gate 				    di_mem_addr(st, off), devid_size);
17667c478bd9Sstevel@tonic-gate 				off += devid_size;
17677c478bd9Sstevel@tonic-gate 				ddi_devid_free(devid);
17687c478bd9Sstevel@tonic-gate 			}
17697c478bd9Sstevel@tonic-gate 			ddi_prop_free(devidstr);
17707c478bd9Sstevel@tonic-gate 		}
17717c478bd9Sstevel@tonic-gate 	}
17727c478bd9Sstevel@tonic-gate #endif	/* DEVID_COMPATIBILITY */
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 	if (node->devi_node_name) {
17757c478bd9Sstevel@tonic-gate 		off = di_checkmem(st, off, strlen(node->devi_node_name) + 1);
17767c478bd9Sstevel@tonic-gate 		me->node_name = off;
17777c478bd9Sstevel@tonic-gate 		(void) strcpy(di_mem_addr(st, off), node->devi_node_name);
17787c478bd9Sstevel@tonic-gate 		off += strlen(node->devi_node_name) + 1;
17797c478bd9Sstevel@tonic-gate 	}
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 	if (node->devi_compat_names && (node->devi_compat_length > 1)) {
17827c478bd9Sstevel@tonic-gate 		off = di_checkmem(st, off, node->devi_compat_length);
17837c478bd9Sstevel@tonic-gate 		me->compat_names = off;
17847c478bd9Sstevel@tonic-gate 		me->compat_length = node->devi_compat_length;
17857c478bd9Sstevel@tonic-gate 		bcopy(node->devi_compat_names, di_mem_addr(st, off),
17867c478bd9Sstevel@tonic-gate 			node->devi_compat_length);
17877c478bd9Sstevel@tonic-gate 		off += node->devi_compat_length;
17887c478bd9Sstevel@tonic-gate 	}
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 	if (node->devi_addr) {
17917c478bd9Sstevel@tonic-gate 		off = di_checkmem(st, off, strlen(node->devi_addr) + 1);
17927c478bd9Sstevel@tonic-gate 		me->address = off;
17937c478bd9Sstevel@tonic-gate 		(void) strcpy(di_mem_addr(st, off), node->devi_addr);
17947c478bd9Sstevel@tonic-gate 		off += strlen(node->devi_addr) + 1;
17957c478bd9Sstevel@tonic-gate 	}
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 	if (node->devi_binding_name) {
17987c478bd9Sstevel@tonic-gate 		off = di_checkmem(st, off, strlen(node->devi_binding_name) + 1);
17997c478bd9Sstevel@tonic-gate 		me->bind_name = off;
18007c478bd9Sstevel@tonic-gate 		(void) strcpy(di_mem_addr(st, off), node->devi_binding_name);
18017c478bd9Sstevel@tonic-gate 		off += strlen(node->devi_binding_name) + 1;
18027c478bd9Sstevel@tonic-gate 	}
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	me->drv_major = node->devi_major;
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	/*
18077c478bd9Sstevel@tonic-gate 	 * If the dip is BOUND, set the next pointer of the
18087c478bd9Sstevel@tonic-gate 	 * per-instance list to -1, indicating that it is yet to be resolved.
18097c478bd9Sstevel@tonic-gate 	 * This will be resolved later in snap_driver_list().
18107c478bd9Sstevel@tonic-gate 	 */
18117c478bd9Sstevel@tonic-gate 	if (me->drv_major != -1) {
18127c478bd9Sstevel@tonic-gate 		me->next = -1;
18137c478bd9Sstevel@tonic-gate 	} else {
18147c478bd9Sstevel@tonic-gate 		me->next = 0;
18157c478bd9Sstevel@tonic-gate 	}
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	/*
18187c478bd9Sstevel@tonic-gate 	 * An optimization to skip mutex_enter when not needed.
18197c478bd9Sstevel@tonic-gate 	 */
18207c478bd9Sstevel@tonic-gate 	if (!((DINFOMINOR | DINFOPROP | DINFOPATH) & st->command)) {
18217c478bd9Sstevel@tonic-gate 		goto priv_data;
18227c478bd9Sstevel@tonic-gate 	}
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	/*
18257c478bd9Sstevel@tonic-gate 	 * Grab current per dev_info node lock to
18267c478bd9Sstevel@tonic-gate 	 * get minor data and properties.
18277c478bd9Sstevel@tonic-gate 	 */
18287c478bd9Sstevel@tonic-gate 	mutex_enter(&(node->devi_lock));
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 	if (!(DINFOMINOR & st->command)) {
18317c478bd9Sstevel@tonic-gate 		goto path;
18327c478bd9Sstevel@tonic-gate 	}
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	if (node->devi_minor) {		/* minor data */
18357c478bd9Sstevel@tonic-gate 		me->minor_data = DI_ALIGN(off);
18367c478bd9Sstevel@tonic-gate 		off = di_getmdata(node->devi_minor, &me->minor_data,
18377c478bd9Sstevel@tonic-gate 		    me->self, st);
18387c478bd9Sstevel@tonic-gate 	}
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate path:
18417c478bd9Sstevel@tonic-gate 	if (!(DINFOPATH & st->command)) {
18427c478bd9Sstevel@tonic-gate 		goto property;
18437c478bd9Sstevel@tonic-gate 	}
18447c478bd9Sstevel@tonic-gate 
18458c4f8890Srs135747 	if (MDI_VHCI(node)) {
18468c4f8890Srs135747 		me->multipath_component = MULTIPATH_COMPONENT_VHCI;
18478c4f8890Srs135747 	}
18488c4f8890Srs135747 
18497c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(node)) {
18508c4f8890Srs135747 		me->multipath_component = MULTIPATH_COMPONENT_CLIENT;
18517c478bd9Sstevel@tonic-gate 		me->multipath_client = DI_ALIGN(off);
18527c478bd9Sstevel@tonic-gate 		off = di_getpath_data((dev_info_t *)node, &me->multipath_client,
18537c478bd9Sstevel@tonic-gate 		    me->self, st, 1);
18547c478bd9Sstevel@tonic-gate 		dcmn_err((CE_WARN, "me->multipath_client = %x for node %p "
18557c478bd9Sstevel@tonic-gate 		    "component type = %d.  off=%d",
18567c478bd9Sstevel@tonic-gate 		    me->multipath_client,
18577c478bd9Sstevel@tonic-gate 		    (void *)node, node->devi_mdi_component, off));
18587c478bd9Sstevel@tonic-gate 	}
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(node)) {
18618c4f8890Srs135747 		me->multipath_component = MULTIPATH_COMPONENT_PHCI;
18627c478bd9Sstevel@tonic-gate 		me->multipath_phci = DI_ALIGN(off);
18637c478bd9Sstevel@tonic-gate 		off = di_getpath_data((dev_info_t *)node, &me->multipath_phci,
18647c478bd9Sstevel@tonic-gate 		    me->self, st, 0);
18657c478bd9Sstevel@tonic-gate 		dcmn_err((CE_WARN, "me->multipath_phci = %x for node %p "
18667c478bd9Sstevel@tonic-gate 		    "component type = %d.  off=%d",
18677c478bd9Sstevel@tonic-gate 		    me->multipath_phci,
18687c478bd9Sstevel@tonic-gate 		    (void *)node, node->devi_mdi_component, off));
18697c478bd9Sstevel@tonic-gate 	}
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate property:
18727c478bd9Sstevel@tonic-gate 	if (!(DINFOPROP & st->command)) {
18737c478bd9Sstevel@tonic-gate 		goto unlock;
18747c478bd9Sstevel@tonic-gate 	}
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 	if (node->devi_drv_prop_ptr) {	/* driver property list */
18777c478bd9Sstevel@tonic-gate 		me->drv_prop = DI_ALIGN(off);
18787c478bd9Sstevel@tonic-gate 		off = di_getprop(node->devi_drv_prop_ptr, &me->drv_prop, st,
18797c478bd9Sstevel@tonic-gate 			node, DI_PROP_DRV_LIST);
18807c478bd9Sstevel@tonic-gate 	}
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 	if (node->devi_sys_prop_ptr) {	/* system property list */
18837c478bd9Sstevel@tonic-gate 		me->sys_prop = DI_ALIGN(off);
18847c478bd9Sstevel@tonic-gate 		off = di_getprop(node->devi_sys_prop_ptr, &me->sys_prop, st,
18857c478bd9Sstevel@tonic-gate 			node, DI_PROP_SYS_LIST);
18867c478bd9Sstevel@tonic-gate 	}
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	if (node->devi_hw_prop_ptr) {	/* hardware property list */
18897c478bd9Sstevel@tonic-gate 		me->hw_prop = DI_ALIGN(off);
18907c478bd9Sstevel@tonic-gate 		off = di_getprop(node->devi_hw_prop_ptr, &me->hw_prop, st,
18917c478bd9Sstevel@tonic-gate 			node, DI_PROP_HW_LIST);
18927c478bd9Sstevel@tonic-gate 	}
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate 	if (node->devi_global_prop_list == NULL) {
18957c478bd9Sstevel@tonic-gate 		me->glob_prop = (di_off_t)-1;	/* not global property */
18967c478bd9Sstevel@tonic-gate 	} else {
18977c478bd9Sstevel@tonic-gate 		/*
18987c478bd9Sstevel@tonic-gate 		 * Make copy of global property list if this devinfo refers
18997c478bd9Sstevel@tonic-gate 		 * global properties different from what's on the devnames
19007c478bd9Sstevel@tonic-gate 		 * array. It can happen if there has been a forced
19017c478bd9Sstevel@tonic-gate 		 * driver.conf update. See mod_drv(1M).
19027c478bd9Sstevel@tonic-gate 		 */
19037c478bd9Sstevel@tonic-gate 		ASSERT(me->drv_major != -1);
19047c478bd9Sstevel@tonic-gate 		if (node->devi_global_prop_list !=
19057c478bd9Sstevel@tonic-gate 		    devnamesp[me->drv_major].dn_global_prop_ptr) {
19067c478bd9Sstevel@tonic-gate 			me->glob_prop = DI_ALIGN(off);
19077c478bd9Sstevel@tonic-gate 			off = di_getprop(node->devi_global_prop_list->prop_list,
19087c478bd9Sstevel@tonic-gate 			    &me->glob_prop, st, node, DI_PROP_GLB_LIST);
19097c478bd9Sstevel@tonic-gate 		}
19107c478bd9Sstevel@tonic-gate 	}
19117c478bd9Sstevel@tonic-gate 
19127c478bd9Sstevel@tonic-gate unlock:
19137c478bd9Sstevel@tonic-gate 	/*
19147c478bd9Sstevel@tonic-gate 	 * release current per dev_info node lock
19157c478bd9Sstevel@tonic-gate 	 */
19167c478bd9Sstevel@tonic-gate 	mutex_exit(&(node->devi_lock));
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate priv_data:
19197c478bd9Sstevel@tonic-gate 	if (!(DINFOPRIVDATA & st->command)) {
19207c478bd9Sstevel@tonic-gate 		goto pm_info;
19217c478bd9Sstevel@tonic-gate 	}
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate 	if (ddi_get_parent_data((dev_info_t *)node) != NULL) {
19247c478bd9Sstevel@tonic-gate 		me->parent_data = DI_ALIGN(off);
19257c478bd9Sstevel@tonic-gate 		off = di_getppdata(node, &me->parent_data, st);
19267c478bd9Sstevel@tonic-gate 	}
19277c478bd9Sstevel@tonic-gate 
19287c478bd9Sstevel@tonic-gate 	if (ddi_get_driver_private((dev_info_t *)node) != NULL) {
19297c478bd9Sstevel@tonic-gate 		me->driver_data = DI_ALIGN(off);
19307c478bd9Sstevel@tonic-gate 		off = di_getdpdata(node, &me->driver_data, st);
19317c478bd9Sstevel@tonic-gate 	}
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate pm_info: /* NOT implemented */
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate subtree:
19367c478bd9Sstevel@tonic-gate 	if (!(DINFOSUBTREE & st->command)) {
19377c478bd9Sstevel@tonic-gate 		POP_STACK(dsp);
19387c478bd9Sstevel@tonic-gate 		return (DI_ALIGN(off));
19397c478bd9Sstevel@tonic-gate 	}
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate child:
19427c478bd9Sstevel@tonic-gate 	/*
19437c478bd9Sstevel@tonic-gate 	 * If there is a child--push child onto stack.
19447c478bd9Sstevel@tonic-gate 	 * Hold the parent busy while doing so.
19457c478bd9Sstevel@tonic-gate 	 */
19467c478bd9Sstevel@tonic-gate 	if (node->devi_child) {
19477c478bd9Sstevel@tonic-gate 		me->child = DI_ALIGN(off);
19487c478bd9Sstevel@tonic-gate 		PUSH_STACK(dsp, node->devi_child, &me->child);
19497c478bd9Sstevel@tonic-gate 		return (me->child);
19507c478bd9Sstevel@tonic-gate 	}
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate sibling:
19537c478bd9Sstevel@tonic-gate 	/*
19547c478bd9Sstevel@tonic-gate 	 * no child node, unroll the stack till a sibling of
19557c478bd9Sstevel@tonic-gate 	 * a parent node is found or root node is reached
19567c478bd9Sstevel@tonic-gate 	 */
19577c478bd9Sstevel@tonic-gate 	POP_STACK(dsp);
19587c478bd9Sstevel@tonic-gate 	while (!EMPTY_STACK(dsp) && (node->devi_sibling == NULL)) {
19597c478bd9Sstevel@tonic-gate 		node = TOP_NODE(dsp);
19607c478bd9Sstevel@tonic-gate 		me = DI_NODE(di_mem_addr(st, *(TOP_OFFSET(dsp))));
19617c478bd9Sstevel@tonic-gate 		POP_STACK(dsp);
19627c478bd9Sstevel@tonic-gate 	}
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 	if (!EMPTY_STACK(dsp)) {
19657c478bd9Sstevel@tonic-gate 		/*
19667c478bd9Sstevel@tonic-gate 		 * a sibling is found, replace top of stack by its sibling
19677c478bd9Sstevel@tonic-gate 		 */
19687c478bd9Sstevel@tonic-gate 		me->sibling = DI_ALIGN(off);
19697c478bd9Sstevel@tonic-gate 		PUSH_STACK(dsp, node->devi_sibling, &me->sibling);
19707c478bd9Sstevel@tonic-gate 		return (me->sibling);
19717c478bd9Sstevel@tonic-gate 	}
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 	/*
19747c478bd9Sstevel@tonic-gate 	 * DONE with all nodes
19757c478bd9Sstevel@tonic-gate 	 */
19767c478bd9Sstevel@tonic-gate 	return (DI_ALIGN(off));
19777c478bd9Sstevel@tonic-gate }
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate static i_lnode_t *
19807c478bd9Sstevel@tonic-gate i_lnode_alloc(int modid)
19817c478bd9Sstevel@tonic-gate {
19827c478bd9Sstevel@tonic-gate 	i_lnode_t	*i_lnode;
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 	i_lnode = kmem_zalloc(sizeof (i_lnode_t), KM_SLEEP);
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate 	ASSERT(modid != -1);
19877c478bd9Sstevel@tonic-gate 	i_lnode->modid = modid;
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate 	return (i_lnode);
19907c478bd9Sstevel@tonic-gate }
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate static void
19937c478bd9Sstevel@tonic-gate i_lnode_free(i_lnode_t *i_lnode)
19947c478bd9Sstevel@tonic-gate {
19957c478bd9Sstevel@tonic-gate 	kmem_free(i_lnode, sizeof (i_lnode_t));
19967c478bd9Sstevel@tonic-gate }
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate static void
19997c478bd9Sstevel@tonic-gate i_lnode_check_free(i_lnode_t *i_lnode)
20007c478bd9Sstevel@tonic-gate {
20017c478bd9Sstevel@tonic-gate 	/* This lnode and its dip must have been snapshotted */
20027c478bd9Sstevel@tonic-gate 	ASSERT(i_lnode->self > 0);
20037c478bd9Sstevel@tonic-gate 	ASSERT(i_lnode->di_node->self > 0);
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 	/* at least 1 link (in or out) must exist for this lnode */
20067c478bd9Sstevel@tonic-gate 	ASSERT(i_lnode->link_in || i_lnode->link_out);
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 	i_lnode_free(i_lnode);
20097c478bd9Sstevel@tonic-gate }
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate static i_link_t *
20127c478bd9Sstevel@tonic-gate i_link_alloc(int spec_type)
20137c478bd9Sstevel@tonic-gate {
20147c478bd9Sstevel@tonic-gate 	i_link_t *i_link;
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	i_link = kmem_zalloc(sizeof (i_link_t), KM_SLEEP);
20177c478bd9Sstevel@tonic-gate 	i_link->spec_type = spec_type;
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate 	return (i_link);
20207c478bd9Sstevel@tonic-gate }
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate static void
20237c478bd9Sstevel@tonic-gate i_link_check_free(i_link_t *i_link)
20247c478bd9Sstevel@tonic-gate {
20257c478bd9Sstevel@tonic-gate 	/* This link must have been snapshotted */
20267c478bd9Sstevel@tonic-gate 	ASSERT(i_link->self > 0);
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 	/* Both endpoint lnodes must exist for this link */
20297c478bd9Sstevel@tonic-gate 	ASSERT(i_link->src_lnode);
20307c478bd9Sstevel@tonic-gate 	ASSERT(i_link->tgt_lnode);
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 	kmem_free(i_link, sizeof (i_link_t));
20337c478bd9Sstevel@tonic-gate }
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
20367c478bd9Sstevel@tonic-gate static uint_t
20377c478bd9Sstevel@tonic-gate i_lnode_hashfunc(void *arg, mod_hash_key_t key)
20387c478bd9Sstevel@tonic-gate {
20397c478bd9Sstevel@tonic-gate 	i_lnode_t	*i_lnode = (i_lnode_t *)key;
20407c478bd9Sstevel@tonic-gate 	struct di_node	*ptr;
20417c478bd9Sstevel@tonic-gate 	dev_t		dev;
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate 	dev = i_lnode->devt;
20447c478bd9Sstevel@tonic-gate 	if (dev != DDI_DEV_T_NONE)
20457c478bd9Sstevel@tonic-gate 		return (i_lnode->modid + getminor(dev) + getmajor(dev));
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate 	ptr = i_lnode->di_node;
20487c478bd9Sstevel@tonic-gate 	ASSERT(ptr->self > 0);
20497c478bd9Sstevel@tonic-gate 	if (ptr) {
20507c478bd9Sstevel@tonic-gate 		uintptr_t k = (uintptr_t)ptr;
20517c478bd9Sstevel@tonic-gate 		k >>= (int)highbit(sizeof (struct di_node));
20527c478bd9Sstevel@tonic-gate 		return ((uint_t)k);
20537c478bd9Sstevel@tonic-gate 	}
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate 	return (i_lnode->modid);
20567c478bd9Sstevel@tonic-gate }
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate static int
20597c478bd9Sstevel@tonic-gate i_lnode_cmp(void *arg1, void *arg2)
20607c478bd9Sstevel@tonic-gate {
20617c478bd9Sstevel@tonic-gate 	i_lnode_t	*i_lnode1 = (i_lnode_t *)arg1;
20627c478bd9Sstevel@tonic-gate 	i_lnode_t	*i_lnode2 = (i_lnode_t *)arg2;
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 	if (i_lnode1->modid != i_lnode2->modid) {
20657c478bd9Sstevel@tonic-gate 		return ((i_lnode1->modid < i_lnode2->modid) ? -1 : 1);
20667c478bd9Sstevel@tonic-gate 	}
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 	if (i_lnode1->di_node != i_lnode2->di_node)
20697c478bd9Sstevel@tonic-gate 		return ((i_lnode1->di_node < i_lnode2->di_node) ? -1 : 1);
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 	if (i_lnode1->devt != i_lnode2->devt)
20727c478bd9Sstevel@tonic-gate 		return ((i_lnode1->devt < i_lnode2->devt) ? -1 : 1);
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 	return (0);
20757c478bd9Sstevel@tonic-gate }
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate /*
20787c478bd9Sstevel@tonic-gate  * An lnode represents a {dip, dev_t} tuple. A link represents a
20797c478bd9Sstevel@tonic-gate  * {src_lnode, tgt_lnode, spec_type} tuple.
20807c478bd9Sstevel@tonic-gate  * The following callback assumes that LDI framework ref-counts the
20817c478bd9Sstevel@tonic-gate  * src_dip and tgt_dip while invoking this callback.
20827c478bd9Sstevel@tonic-gate  */
20837c478bd9Sstevel@tonic-gate static int
20847c478bd9Sstevel@tonic-gate di_ldi_callback(const ldi_usage_t *ldi_usage, void *arg)
20857c478bd9Sstevel@tonic-gate {
20867c478bd9Sstevel@tonic-gate 	struct di_state	*st = (struct di_state *)arg;
20877c478bd9Sstevel@tonic-gate 	i_lnode_t	*src_lnode, *tgt_lnode, *i_lnode;
20887c478bd9Sstevel@tonic-gate 	i_link_t	**i_link_next, *i_link;
20897c478bd9Sstevel@tonic-gate 	di_off_t	soff, toff;
20907c478bd9Sstevel@tonic-gate 	mod_hash_val_t	nodep = NULL;
20917c478bd9Sstevel@tonic-gate 	int		res;
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	/*
20947c478bd9Sstevel@tonic-gate 	 * if the source or target of this device usage information doesn't
20957c478bd9Sstevel@tonic-gate 	 * corrospond to a device node then we don't report it via
20967c478bd9Sstevel@tonic-gate 	 * libdevinfo so return.
20977c478bd9Sstevel@tonic-gate 	 */
20987c478bd9Sstevel@tonic-gate 	if ((ldi_usage->src_dip == NULL) || (ldi_usage->tgt_dip == NULL))
20997c478bd9Sstevel@tonic-gate 		return (LDI_USAGE_CONTINUE);
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_devi_holdcnt(ldi_usage->src_dip));
21027c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_devi_holdcnt(ldi_usage->tgt_dip));
21037c478bd9Sstevel@tonic-gate 
21047c478bd9Sstevel@tonic-gate 	/*
21057c478bd9Sstevel@tonic-gate 	 * Skip the ldi_usage if either src or tgt dip is not in the
21067c478bd9Sstevel@tonic-gate 	 * snapshot. This saves us from pruning bad lnodes/links later.
21077c478bd9Sstevel@tonic-gate 	 */
21087c478bd9Sstevel@tonic-gate 	if (di_dip_find(st, ldi_usage->src_dip, &soff) != 0)
21097c478bd9Sstevel@tonic-gate 		return (LDI_USAGE_CONTINUE);
21107c478bd9Sstevel@tonic-gate 	if (di_dip_find(st, ldi_usage->tgt_dip, &toff) != 0)
21117c478bd9Sstevel@tonic-gate 		return (LDI_USAGE_CONTINUE);
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate 	ASSERT(soff > 0);
21147c478bd9Sstevel@tonic-gate 	ASSERT(toff > 0);
21157c478bd9Sstevel@tonic-gate 
21167c478bd9Sstevel@tonic-gate 	/*
21177c478bd9Sstevel@tonic-gate 	 * allocate an i_lnode and add it to the lnode hash
21187c478bd9Sstevel@tonic-gate 	 * if it is not already present. For this particular
21197c478bd9Sstevel@tonic-gate 	 * link the lnode is a source, but it may
21207c478bd9Sstevel@tonic-gate 	 * participate as tgt or src in any number of layered
21217c478bd9Sstevel@tonic-gate 	 * operations - so it may already be in the hash.
21227c478bd9Sstevel@tonic-gate 	 */
21237c478bd9Sstevel@tonic-gate 	i_lnode = i_lnode_alloc(ldi_usage->src_modid);
21247c478bd9Sstevel@tonic-gate 	i_lnode->di_node = (struct di_node *)di_mem_addr(st, soff);
21257c478bd9Sstevel@tonic-gate 	i_lnode->devt = ldi_usage->src_devt;
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 	res = mod_hash_find(st->lnode_hash, i_lnode, &nodep);
21287c478bd9Sstevel@tonic-gate 	if (res == MH_ERR_NOTFOUND) {
21297c478bd9Sstevel@tonic-gate 		/*
21307c478bd9Sstevel@tonic-gate 		 * new i_lnode
21317c478bd9Sstevel@tonic-gate 		 * add it to the hash and increment the lnode count
21327c478bd9Sstevel@tonic-gate 		 */
21337c478bd9Sstevel@tonic-gate 		res = mod_hash_insert(st->lnode_hash, i_lnode, i_lnode);
21347c478bd9Sstevel@tonic-gate 		ASSERT(res == 0);
21357c478bd9Sstevel@tonic-gate 		st->lnode_count++;
21367c478bd9Sstevel@tonic-gate 		src_lnode = i_lnode;
21377c478bd9Sstevel@tonic-gate 	} else {
21387c478bd9Sstevel@tonic-gate 		/* this i_lnode already exists in the lnode_hash */
21397c478bd9Sstevel@tonic-gate 		i_lnode_free(i_lnode);
21407c478bd9Sstevel@tonic-gate 		src_lnode = (i_lnode_t *)nodep;
21417c478bd9Sstevel@tonic-gate 	}
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate 	/*
21447c478bd9Sstevel@tonic-gate 	 * allocate a tgt i_lnode and add it to the lnode hash
21457c478bd9Sstevel@tonic-gate 	 */
21467c478bd9Sstevel@tonic-gate 	i_lnode = i_lnode_alloc(ldi_usage->tgt_modid);
21477c478bd9Sstevel@tonic-gate 	i_lnode->di_node = (struct di_node *)di_mem_addr(st, toff);
21487c478bd9Sstevel@tonic-gate 	i_lnode->devt = ldi_usage->tgt_devt;
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 	res = mod_hash_find(st->lnode_hash, i_lnode, &nodep);
21517c478bd9Sstevel@tonic-gate 	if (res == MH_ERR_NOTFOUND) {
21527c478bd9Sstevel@tonic-gate 		/*
21537c478bd9Sstevel@tonic-gate 		 * new i_lnode
21547c478bd9Sstevel@tonic-gate 		 * add it to the hash and increment the lnode count
21557c478bd9Sstevel@tonic-gate 		 */
21567c478bd9Sstevel@tonic-gate 		res = mod_hash_insert(st->lnode_hash, i_lnode, i_lnode);
21577c478bd9Sstevel@tonic-gate 		ASSERT(res == 0);
21587c478bd9Sstevel@tonic-gate 		st->lnode_count++;
21597c478bd9Sstevel@tonic-gate 		tgt_lnode = i_lnode;
21607c478bd9Sstevel@tonic-gate 	} else {
21617c478bd9Sstevel@tonic-gate 		/* this i_lnode already exists in the lnode_hash */
21627c478bd9Sstevel@tonic-gate 		i_lnode_free(i_lnode);
21637c478bd9Sstevel@tonic-gate 		tgt_lnode = (i_lnode_t *)nodep;
21647c478bd9Sstevel@tonic-gate 	}
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate 	/*
21677c478bd9Sstevel@tonic-gate 	 * allocate a i_link
21687c478bd9Sstevel@tonic-gate 	 */
21697c478bd9Sstevel@tonic-gate 	i_link = i_link_alloc(ldi_usage->tgt_spec_type);
21707c478bd9Sstevel@tonic-gate 	i_link->src_lnode = src_lnode;
21717c478bd9Sstevel@tonic-gate 	i_link->tgt_lnode = tgt_lnode;
21727c478bd9Sstevel@tonic-gate 
21737c478bd9Sstevel@tonic-gate 	/*
21747c478bd9Sstevel@tonic-gate 	 * add this link onto the src i_lnodes outbound i_link list
21757c478bd9Sstevel@tonic-gate 	 */
21767c478bd9Sstevel@tonic-gate 	i_link_next = &(src_lnode->link_out);
21777c478bd9Sstevel@tonic-gate 	while (*i_link_next != NULL) {
21787c478bd9Sstevel@tonic-gate 		if ((i_lnode_cmp(tgt_lnode, (*i_link_next)->tgt_lnode) == 0) &&
21797c478bd9Sstevel@tonic-gate 		    (i_link->spec_type == (*i_link_next)->spec_type)) {
21807c478bd9Sstevel@tonic-gate 			/* this link already exists */
21817c478bd9Sstevel@tonic-gate 			kmem_free(i_link, sizeof (i_link_t));
21827c478bd9Sstevel@tonic-gate 			return (LDI_USAGE_CONTINUE);
21837c478bd9Sstevel@tonic-gate 		}
21847c478bd9Sstevel@tonic-gate 		i_link_next = &((*i_link_next)->src_link_next);
21857c478bd9Sstevel@tonic-gate 	}
21867c478bd9Sstevel@tonic-gate 	*i_link_next = i_link;
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 	/*
21897c478bd9Sstevel@tonic-gate 	 * add this link onto the tgt i_lnodes inbound i_link list
21907c478bd9Sstevel@tonic-gate 	 */
21917c478bd9Sstevel@tonic-gate 	i_link_next = &(tgt_lnode->link_in);
21927c478bd9Sstevel@tonic-gate 	while (*i_link_next != NULL) {
21937c478bd9Sstevel@tonic-gate 		ASSERT(i_lnode_cmp(src_lnode, (*i_link_next)->src_lnode) != 0);
21947c478bd9Sstevel@tonic-gate 		i_link_next = &((*i_link_next)->tgt_link_next);
21957c478bd9Sstevel@tonic-gate 	}
21967c478bd9Sstevel@tonic-gate 	*i_link_next = i_link;
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate 	/*
21997c478bd9Sstevel@tonic-gate 	 * add this i_link to the link hash
22007c478bd9Sstevel@tonic-gate 	 */
22017c478bd9Sstevel@tonic-gate 	res = mod_hash_insert(st->link_hash, i_link, i_link);
22027c478bd9Sstevel@tonic-gate 	ASSERT(res == 0);
22037c478bd9Sstevel@tonic-gate 	st->link_count++;
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 	return (LDI_USAGE_CONTINUE);
22067c478bd9Sstevel@tonic-gate }
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate struct i_layer_data {
22097c478bd9Sstevel@tonic-gate 	struct di_state	*st;
22107c478bd9Sstevel@tonic-gate 	int		lnode_count;
22117c478bd9Sstevel@tonic-gate 	int		link_count;
22127c478bd9Sstevel@tonic-gate 	di_off_t	lnode_off;
22137c478bd9Sstevel@tonic-gate 	di_off_t 	link_off;
22147c478bd9Sstevel@tonic-gate };
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate /*ARGSUSED*/
22177c478bd9Sstevel@tonic-gate static uint_t
22187c478bd9Sstevel@tonic-gate i_link_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
22197c478bd9Sstevel@tonic-gate {
22207c478bd9Sstevel@tonic-gate 	i_link_t		*i_link  = (i_link_t *)key;
22217c478bd9Sstevel@tonic-gate 	struct i_layer_data	*data = arg;
22227c478bd9Sstevel@tonic-gate 	struct di_link		*me;
22237c478bd9Sstevel@tonic-gate 	struct di_lnode		*melnode;
22247c478bd9Sstevel@tonic-gate 	struct di_node		*medinode;
22257c478bd9Sstevel@tonic-gate 
22267c478bd9Sstevel@tonic-gate 	ASSERT(i_link->self == 0);
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate 	i_link->self = data->link_off +
22297c478bd9Sstevel@tonic-gate 	    (data->link_count * sizeof (struct di_link));
22307c478bd9Sstevel@tonic-gate 	data->link_count++;
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate 	ASSERT(data->link_off > 0 && data->link_count > 0);
22337c478bd9Sstevel@tonic-gate 	ASSERT(data->lnode_count == data->st->lnode_count); /* lnodes done */
22347c478bd9Sstevel@tonic-gate 	ASSERT(data->link_count <= data->st->link_count);
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate 	/* fill in fields for the di_link snapshot */
22377c478bd9Sstevel@tonic-gate 	me = (struct di_link *)di_mem_addr(data->st, i_link->self);
22387c478bd9Sstevel@tonic-gate 	me->self = i_link->self;
22397c478bd9Sstevel@tonic-gate 	me->spec_type = i_link->spec_type;
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate 	/*
22427c478bd9Sstevel@tonic-gate 	 * The src_lnode and tgt_lnode i_lnode_t for this i_link_t
22437c478bd9Sstevel@tonic-gate 	 * are created during the LDI table walk. Since we are
22447c478bd9Sstevel@tonic-gate 	 * walking the link hash, the lnode hash has already been
22457c478bd9Sstevel@tonic-gate 	 * walked and the lnodes have been snapshotted. Save lnode
22467c478bd9Sstevel@tonic-gate 	 * offsets.
22477c478bd9Sstevel@tonic-gate 	 */
22487c478bd9Sstevel@tonic-gate 	me->src_lnode = i_link->src_lnode->self;
22497c478bd9Sstevel@tonic-gate 	me->tgt_lnode = i_link->tgt_lnode->self;
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate 	/*
22527c478bd9Sstevel@tonic-gate 	 * Save this link's offset in the src_lnode snapshot's link_out
22537c478bd9Sstevel@tonic-gate 	 * field
22547c478bd9Sstevel@tonic-gate 	 */
22557c478bd9Sstevel@tonic-gate 	melnode = (struct di_lnode *)di_mem_addr(data->st, me->src_lnode);
22567c478bd9Sstevel@tonic-gate 	me->src_link_next = melnode->link_out;
22577c478bd9Sstevel@tonic-gate 	melnode->link_out = me->self;
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate 	/*
22607c478bd9Sstevel@tonic-gate 	 * Put this link on the tgt_lnode's link_in field
22617c478bd9Sstevel@tonic-gate 	 */
22627c478bd9Sstevel@tonic-gate 	melnode = (struct di_lnode *)di_mem_addr(data->st, me->tgt_lnode);
22637c478bd9Sstevel@tonic-gate 	me->tgt_link_next = melnode->link_in;
22647c478bd9Sstevel@tonic-gate 	melnode->link_in = me->self;
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate 	/*
22677c478bd9Sstevel@tonic-gate 	 * An i_lnode_t is only created if the corresponding dip exists
22687c478bd9Sstevel@tonic-gate 	 * in the snapshot. A pointer to the di_node is saved in the
22697c478bd9Sstevel@tonic-gate 	 * i_lnode_t when it is allocated. For this link, get the di_node
22707c478bd9Sstevel@tonic-gate 	 * for the source lnode. Then put the link on the di_node's list
22717c478bd9Sstevel@tonic-gate 	 * of src links
22727c478bd9Sstevel@tonic-gate 	 */
22737c478bd9Sstevel@tonic-gate 	medinode = i_link->src_lnode->di_node;
22747c478bd9Sstevel@tonic-gate 	me->src_node_next = medinode->src_links;
22757c478bd9Sstevel@tonic-gate 	medinode->src_links = me->self;
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate 	/*
22787c478bd9Sstevel@tonic-gate 	 * Put this link on the tgt_links list of the target
22797c478bd9Sstevel@tonic-gate 	 * dip.
22807c478bd9Sstevel@tonic-gate 	 */
22817c478bd9Sstevel@tonic-gate 	medinode = i_link->tgt_lnode->di_node;
22827c478bd9Sstevel@tonic-gate 	me->tgt_node_next = medinode->tgt_links;
22837c478bd9Sstevel@tonic-gate 	medinode->tgt_links = me->self;
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 	return (MH_WALK_CONTINUE);
22867c478bd9Sstevel@tonic-gate }
22877c478bd9Sstevel@tonic-gate 
22887c478bd9Sstevel@tonic-gate /*ARGSUSED*/
22897c478bd9Sstevel@tonic-gate static uint_t
22907c478bd9Sstevel@tonic-gate i_lnode_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
22917c478bd9Sstevel@tonic-gate {
22927c478bd9Sstevel@tonic-gate 	i_lnode_t		*i_lnode = (i_lnode_t *)key;
22937c478bd9Sstevel@tonic-gate 	struct i_layer_data	*data = arg;
22947c478bd9Sstevel@tonic-gate 	struct di_lnode		*me;
22957c478bd9Sstevel@tonic-gate 	struct di_node		*medinode;
22967c478bd9Sstevel@tonic-gate 
22977c478bd9Sstevel@tonic-gate 	ASSERT(i_lnode->self == 0);
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate 	i_lnode->self = data->lnode_off +
23007c478bd9Sstevel@tonic-gate 	    (data->lnode_count * sizeof (struct di_lnode));
23017c478bd9Sstevel@tonic-gate 	data->lnode_count++;
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate 	ASSERT(data->lnode_off > 0 && data->lnode_count > 0);
23047c478bd9Sstevel@tonic-gate 	ASSERT(data->link_count == 0); /* links not done yet */
23057c478bd9Sstevel@tonic-gate 	ASSERT(data->lnode_count <= data->st->lnode_count);
23067c478bd9Sstevel@tonic-gate 
23077c478bd9Sstevel@tonic-gate 	/* fill in fields for the di_lnode snapshot */
23087c478bd9Sstevel@tonic-gate 	me = (struct di_lnode *)di_mem_addr(data->st, i_lnode->self);
23097c478bd9Sstevel@tonic-gate 	me->self = i_lnode->self;
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate 	if (i_lnode->devt == DDI_DEV_T_NONE) {
23127c478bd9Sstevel@tonic-gate 		me->dev_major = (major_t)-1;
23137c478bd9Sstevel@tonic-gate 		me->dev_minor = (minor_t)-1;
23147c478bd9Sstevel@tonic-gate 	} else {
23157c478bd9Sstevel@tonic-gate 		me->dev_major = getmajor(i_lnode->devt);
23167c478bd9Sstevel@tonic-gate 		me->dev_minor = getminor(i_lnode->devt);
23177c478bd9Sstevel@tonic-gate 	}
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 	/*
23207c478bd9Sstevel@tonic-gate 	 * The dip corresponding to this lnode must exist in
23217c478bd9Sstevel@tonic-gate 	 * the snapshot or we wouldn't have created the i_lnode_t
23227c478bd9Sstevel@tonic-gate 	 * during LDI walk. Save the offset of the dip.
23237c478bd9Sstevel@tonic-gate 	 */
23247c478bd9Sstevel@tonic-gate 	ASSERT(i_lnode->di_node && i_lnode->di_node->self > 0);
23257c478bd9Sstevel@tonic-gate 	me->node = i_lnode->di_node->self;
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate 	/*
23287c478bd9Sstevel@tonic-gate 	 * There must be at least one link in or out of this lnode
23297c478bd9Sstevel@tonic-gate 	 * or we wouldn't have created it. These fields will be set
23307c478bd9Sstevel@tonic-gate 	 * during the link hash walk.
23317c478bd9Sstevel@tonic-gate 	 */
23327c478bd9Sstevel@tonic-gate 	ASSERT((i_lnode->link_in != NULL) || (i_lnode->link_out != NULL));
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate 	/*
23357c478bd9Sstevel@tonic-gate 	 * set the offset of the devinfo node associated with this
23367c478bd9Sstevel@tonic-gate 	 * lnode. Also update the node_next next pointer.  this pointer
23377c478bd9Sstevel@tonic-gate 	 * is set if there are multiple lnodes associated with the same
23387c478bd9Sstevel@tonic-gate 	 * devinfo node.  (could occure when multiple minor nodes
23397c478bd9Sstevel@tonic-gate 	 * are open for one device, etc.)
23407c478bd9Sstevel@tonic-gate 	 */
23417c478bd9Sstevel@tonic-gate 	medinode = i_lnode->di_node;
23427c478bd9Sstevel@tonic-gate 	me->node_next = medinode->lnodes;
23437c478bd9Sstevel@tonic-gate 	medinode->lnodes = me->self;
23447c478bd9Sstevel@tonic-gate 
23457c478bd9Sstevel@tonic-gate 	return (MH_WALK_CONTINUE);
23467c478bd9Sstevel@tonic-gate }
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate static di_off_t
23497c478bd9Sstevel@tonic-gate di_getlink_data(di_off_t off, struct di_state *st)
23507c478bd9Sstevel@tonic-gate {
23517c478bd9Sstevel@tonic-gate 	struct i_layer_data data = {0};
23527c478bd9Sstevel@tonic-gate 	size_t size;
23537c478bd9Sstevel@tonic-gate 
23547c478bd9Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_copylyr: off = %x\n", off));
23557c478bd9Sstevel@tonic-gate 
23567c478bd9Sstevel@tonic-gate 	st->lnode_hash = mod_hash_create_extended("di_lnode_hash", 32,
23577c478bd9Sstevel@tonic-gate 	    mod_hash_null_keydtor, (void (*)(mod_hash_val_t))i_lnode_check_free,
23587c478bd9Sstevel@tonic-gate 	    i_lnode_hashfunc, NULL, i_lnode_cmp, KM_SLEEP);
23597c478bd9Sstevel@tonic-gate 
23607c478bd9Sstevel@tonic-gate 	st->link_hash = mod_hash_create_ptrhash("di_link_hash", 32,
23617c478bd9Sstevel@tonic-gate 	    (void (*)(mod_hash_val_t))i_link_check_free, sizeof (i_link_t));
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 	/* get driver layering information */
23647c478bd9Sstevel@tonic-gate 	(void) ldi_usage_walker(st, di_ldi_callback);
23657c478bd9Sstevel@tonic-gate 
23667c478bd9Sstevel@tonic-gate 	/* check if there is any link data to include in the snapshot */
23677c478bd9Sstevel@tonic-gate 	if (st->lnode_count == 0) {
23687c478bd9Sstevel@tonic-gate 		ASSERT(st->link_count == 0);
23697c478bd9Sstevel@tonic-gate 		goto out;
23707c478bd9Sstevel@tonic-gate 	}
23717c478bd9Sstevel@tonic-gate 
23727c478bd9Sstevel@tonic-gate 	ASSERT(st->link_count != 0);
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate 	/* get a pointer to snapshot memory for all the di_lnodes */
23757c478bd9Sstevel@tonic-gate 	size = sizeof (struct di_lnode) * st->lnode_count;
23767c478bd9Sstevel@tonic-gate 	data.lnode_off = off = di_checkmem(st, off, size);
23777c478bd9Sstevel@tonic-gate 	off += DI_ALIGN(size);
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 	/* get a pointer to snapshot memory for all the di_links */
23807c478bd9Sstevel@tonic-gate 	size = sizeof (struct di_link) * st->link_count;
23817c478bd9Sstevel@tonic-gate 	data.link_off = off = di_checkmem(st, off, size);
23827c478bd9Sstevel@tonic-gate 	off += DI_ALIGN(size);
23837c478bd9Sstevel@tonic-gate 
23847c478bd9Sstevel@tonic-gate 	data.lnode_count = data.link_count = 0;
23857c478bd9Sstevel@tonic-gate 	data.st = st;
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate 	/*
23887c478bd9Sstevel@tonic-gate 	 * We have lnodes and links that will go into the
23897c478bd9Sstevel@tonic-gate 	 * snapshot, so let's walk the respective hashes
23907c478bd9Sstevel@tonic-gate 	 * and snapshot them. The various linkages are
23917c478bd9Sstevel@tonic-gate 	 * also set up during the walk.
23927c478bd9Sstevel@tonic-gate 	 */
23937c478bd9Sstevel@tonic-gate 	mod_hash_walk(st->lnode_hash, i_lnode_walker, (void *)&data);
23947c478bd9Sstevel@tonic-gate 	ASSERT(data.lnode_count == st->lnode_count);
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 	mod_hash_walk(st->link_hash, i_link_walker, (void *)&data);
23977c478bd9Sstevel@tonic-gate 	ASSERT(data.link_count == st->link_count);
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate out:
24007c478bd9Sstevel@tonic-gate 	/* free up the i_lnodes and i_links used to create the snapshot */
24017c478bd9Sstevel@tonic-gate 	mod_hash_destroy_hash(st->lnode_hash);
24027c478bd9Sstevel@tonic-gate 	mod_hash_destroy_hash(st->link_hash);
24037c478bd9Sstevel@tonic-gate 	st->lnode_count = 0;
24047c478bd9Sstevel@tonic-gate 	st->link_count = 0;
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 	return (off);
24077c478bd9Sstevel@tonic-gate }
24087c478bd9Sstevel@tonic-gate 
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate /*
24117c478bd9Sstevel@tonic-gate  * Copy all minor data nodes attached to a devinfo node into the snapshot.
24127c478bd9Sstevel@tonic-gate  * It is called from di_copynode with devi_lock held.
24137c478bd9Sstevel@tonic-gate  */
24147c478bd9Sstevel@tonic-gate static di_off_t
24157c478bd9Sstevel@tonic-gate di_getmdata(struct ddi_minor_data *mnode, di_off_t *off_p, di_off_t node,
24167c478bd9Sstevel@tonic-gate 	struct di_state *st)
24177c478bd9Sstevel@tonic-gate {
24187c478bd9Sstevel@tonic-gate 	di_off_t off;
24197c478bd9Sstevel@tonic-gate 	struct di_minor *me;
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_getmdata:\n"));
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate 	/*
24247c478bd9Sstevel@tonic-gate 	 * check memory first
24257c478bd9Sstevel@tonic-gate 	 */
24267c478bd9Sstevel@tonic-gate 	off = di_checkmem(st, *off_p, sizeof (struct di_minor));
24277c478bd9Sstevel@tonic-gate 	*off_p = off;
24287c478bd9Sstevel@tonic-gate 
24297c478bd9Sstevel@tonic-gate 	do {
24307c478bd9Sstevel@tonic-gate 		me = (struct di_minor *)di_mem_addr(st, off);
24317c478bd9Sstevel@tonic-gate 		me->self = off;
24327c478bd9Sstevel@tonic-gate 		me->type = mnode->type;
24337c478bd9Sstevel@tonic-gate 		me->node = node;
24347c478bd9Sstevel@tonic-gate 		me->user_private_data = NULL;
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate 		off += DI_ALIGN(sizeof (struct di_minor));
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate 		/*
24397c478bd9Sstevel@tonic-gate 		 * Split dev_t to major/minor, so it works for
24407c478bd9Sstevel@tonic-gate 		 * both ILP32 and LP64 model
24417c478bd9Sstevel@tonic-gate 		 */
24427c478bd9Sstevel@tonic-gate 		me->dev_major = getmajor(mnode->ddm_dev);
24437c478bd9Sstevel@tonic-gate 		me->dev_minor = getminor(mnode->ddm_dev);
24447c478bd9Sstevel@tonic-gate 		me->spec_type = mnode->ddm_spec_type;
24457c478bd9Sstevel@tonic-gate 
24467c478bd9Sstevel@tonic-gate 		if (mnode->ddm_name) {
24477c478bd9Sstevel@tonic-gate 			off = di_checkmem(st, off,
24487c478bd9Sstevel@tonic-gate 				strlen(mnode->ddm_name) + 1);
24497c478bd9Sstevel@tonic-gate 			me->name = off;
24507c478bd9Sstevel@tonic-gate 			(void) strcpy(di_mem_addr(st, off), mnode->ddm_name);
24517c478bd9Sstevel@tonic-gate 			off += DI_ALIGN(strlen(mnode->ddm_name) + 1);
24527c478bd9Sstevel@tonic-gate 		}
24537c478bd9Sstevel@tonic-gate 
24547c478bd9Sstevel@tonic-gate 		if (mnode->ddm_node_type) {
24557c478bd9Sstevel@tonic-gate 			off = di_checkmem(st, off,
24567c478bd9Sstevel@tonic-gate 				strlen(mnode->ddm_node_type) + 1);
24577c478bd9Sstevel@tonic-gate 			me->node_type = off;
24587c478bd9Sstevel@tonic-gate 			(void) strcpy(di_mem_addr(st, off),
24597c478bd9Sstevel@tonic-gate 					mnode->ddm_node_type);
24607c478bd9Sstevel@tonic-gate 			off += DI_ALIGN(strlen(mnode->ddm_node_type) + 1);
24617c478bd9Sstevel@tonic-gate 		}
24627c478bd9Sstevel@tonic-gate 
24637c478bd9Sstevel@tonic-gate 		off = di_checkmem(st, off, sizeof (struct di_minor));
24647c478bd9Sstevel@tonic-gate 		me->next = off;
24657c478bd9Sstevel@tonic-gate 		mnode = mnode->next;
24667c478bd9Sstevel@tonic-gate 	} while (mnode);
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate 	me->next = 0;
24697c478bd9Sstevel@tonic-gate 
24707c478bd9Sstevel@tonic-gate 	return (off);
24717c478bd9Sstevel@tonic-gate }
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate /*
24747c478bd9Sstevel@tonic-gate  * di_register_dip(), di_find_dip(): The dip must be protected
24757c478bd9Sstevel@tonic-gate  * from deallocation when using these routines - this can either
24767c478bd9Sstevel@tonic-gate  * be a reference count, a busy hold or a per-driver lock.
24777c478bd9Sstevel@tonic-gate  */
24787c478bd9Sstevel@tonic-gate 
24797c478bd9Sstevel@tonic-gate static void
24807c478bd9Sstevel@tonic-gate di_register_dip(struct di_state *st, dev_info_t *dip, di_off_t off)
24817c478bd9Sstevel@tonic-gate {
24827c478bd9Sstevel@tonic-gate 	struct dev_info *node = DEVI(dip);
24837c478bd9Sstevel@tonic-gate 	struct di_key *key = kmem_zalloc(sizeof (*key), KM_SLEEP);
24847c478bd9Sstevel@tonic-gate 	struct di_dkey *dk;
24857c478bd9Sstevel@tonic-gate 
24867c478bd9Sstevel@tonic-gate 	ASSERT(dip);
24877c478bd9Sstevel@tonic-gate 	ASSERT(off > 0);
24887c478bd9Sstevel@tonic-gate 
24897c478bd9Sstevel@tonic-gate 	key->k_type = DI_DKEY;
24907c478bd9Sstevel@tonic-gate 	dk = &(key->k_u.dkey);
24917c478bd9Sstevel@tonic-gate 
24927c478bd9Sstevel@tonic-gate 	dk->dk_dip = dip;
24937c478bd9Sstevel@tonic-gate 	dk->dk_major = node->devi_major;
24947c478bd9Sstevel@tonic-gate 	dk->dk_inst = node->devi_instance;
24957c478bd9Sstevel@tonic-gate 	dk->dk_nodeid = node->devi_nodeid;
24967c478bd9Sstevel@tonic-gate 
24977c478bd9Sstevel@tonic-gate 	if (mod_hash_insert(st->reg_dip_hash, (mod_hash_key_t)key,
24987c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)off) != 0) {
24997c478bd9Sstevel@tonic-gate 		panic(
25007c478bd9Sstevel@tonic-gate 		    "duplicate devinfo (%p) registered during device "
25017c478bd9Sstevel@tonic-gate 		    "tree walk", (void *)dip);
25027c478bd9Sstevel@tonic-gate 	}
25037c478bd9Sstevel@tonic-gate }
25047c478bd9Sstevel@tonic-gate 
25057c478bd9Sstevel@tonic-gate 
25067c478bd9Sstevel@tonic-gate static int
25077c478bd9Sstevel@tonic-gate di_dip_find(struct di_state *st, dev_info_t *dip, di_off_t *off_p)
25087c478bd9Sstevel@tonic-gate {
25097c478bd9Sstevel@tonic-gate 	/*
25107c478bd9Sstevel@tonic-gate 	 * uintptr_t must be used because it matches the size of void *;
25117c478bd9Sstevel@tonic-gate 	 * mod_hash expects clients to place results into pointer-size
25127c478bd9Sstevel@tonic-gate 	 * containers; since di_off_t is always a 32-bit offset, alignment
25137c478bd9Sstevel@tonic-gate 	 * would otherwise be broken on 64-bit kernels.
25147c478bd9Sstevel@tonic-gate 	 */
25157c478bd9Sstevel@tonic-gate 	uintptr_t	offset;
25167c478bd9Sstevel@tonic-gate 	struct		di_key key = {0};
25177c478bd9Sstevel@tonic-gate 	struct		di_dkey *dk;
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate 	ASSERT(st->reg_dip_hash);
25207c478bd9Sstevel@tonic-gate 	ASSERT(dip);
25217c478bd9Sstevel@tonic-gate 	ASSERT(off_p);
25227c478bd9Sstevel@tonic-gate 
25237c478bd9Sstevel@tonic-gate 
25247c478bd9Sstevel@tonic-gate 	key.k_type = DI_DKEY;
25257c478bd9Sstevel@tonic-gate 	dk = &(key.k_u.dkey);
25267c478bd9Sstevel@tonic-gate 
25277c478bd9Sstevel@tonic-gate 	dk->dk_dip = dip;
25287c478bd9Sstevel@tonic-gate 	dk->dk_major = DEVI(dip)->devi_major;
25297c478bd9Sstevel@tonic-gate 	dk->dk_inst = DEVI(dip)->devi_instance;
25307c478bd9Sstevel@tonic-gate 	dk->dk_nodeid = DEVI(dip)->devi_nodeid;
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate 	if (mod_hash_find(st->reg_dip_hash, (mod_hash_key_t)&key,
25337c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t *)&offset) == 0) {
25347c478bd9Sstevel@tonic-gate 		*off_p = (di_off_t)offset;
25357c478bd9Sstevel@tonic-gate 		return (0);
25367c478bd9Sstevel@tonic-gate 	} else {
25377c478bd9Sstevel@tonic-gate 		return (-1);
25387c478bd9Sstevel@tonic-gate 	}
25397c478bd9Sstevel@tonic-gate }
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate /*
25427c478bd9Sstevel@tonic-gate  * di_register_pip(), di_find_pip(): The pip must be protected from deallocation
25437c478bd9Sstevel@tonic-gate  * when using these routines. The caller must do this by protecting the
25447c478bd9Sstevel@tonic-gate  * client(or phci)<->pip linkage while traversing the list and then holding the
25457c478bd9Sstevel@tonic-gate  * pip when it is found in the list.
25467c478bd9Sstevel@tonic-gate  */
25477c478bd9Sstevel@tonic-gate 
25487c478bd9Sstevel@tonic-gate static void
25497c478bd9Sstevel@tonic-gate di_register_pip(struct di_state *st, mdi_pathinfo_t *pip, di_off_t off)
25507c478bd9Sstevel@tonic-gate {
25517c478bd9Sstevel@tonic-gate 	struct di_key	*key = kmem_zalloc(sizeof (*key), KM_SLEEP);
25527c478bd9Sstevel@tonic-gate 	char		*path_addr;
25537c478bd9Sstevel@tonic-gate 	struct di_pkey	*pk;
25547c478bd9Sstevel@tonic-gate 
25557c478bd9Sstevel@tonic-gate 	ASSERT(pip);
25567c478bd9Sstevel@tonic-gate 	ASSERT(off > 0);
25577c478bd9Sstevel@tonic-gate 
25587c478bd9Sstevel@tonic-gate 	key->k_type = DI_PKEY;
25597c478bd9Sstevel@tonic-gate 	pk = &(key->k_u.pkey);
25607c478bd9Sstevel@tonic-gate 
25617c478bd9Sstevel@tonic-gate 	pk->pk_pip = pip;
25627c478bd9Sstevel@tonic-gate 	path_addr = mdi_pi_get_addr(pip);
25637c478bd9Sstevel@tonic-gate 	if (path_addr)
25647c478bd9Sstevel@tonic-gate 		pk->pk_path_addr = i_ddi_strdup(path_addr, KM_SLEEP);
25657c478bd9Sstevel@tonic-gate 	pk->pk_client = mdi_pi_get_client(pip);
25667c478bd9Sstevel@tonic-gate 	pk->pk_phci = mdi_pi_get_phci(pip);
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate 	if (mod_hash_insert(st->reg_pip_hash, (mod_hash_key_t)key,
25697c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)off) != 0) {
25707c478bd9Sstevel@tonic-gate 		panic(
25717c478bd9Sstevel@tonic-gate 		    "duplicate pathinfo (%p) registered during device "
25727c478bd9Sstevel@tonic-gate 		    "tree walk", (void *)pip);
25737c478bd9Sstevel@tonic-gate 	}
25747c478bd9Sstevel@tonic-gate }
25757c478bd9Sstevel@tonic-gate 
25767c478bd9Sstevel@tonic-gate /*
25777c478bd9Sstevel@tonic-gate  * As with di_register_pip, the caller must hold or lock the pip
25787c478bd9Sstevel@tonic-gate  */
25797c478bd9Sstevel@tonic-gate static int
25807c478bd9Sstevel@tonic-gate di_pip_find(struct di_state *st, mdi_pathinfo_t *pip, di_off_t *off_p)
25817c478bd9Sstevel@tonic-gate {
25827c478bd9Sstevel@tonic-gate 	/*
25837c478bd9Sstevel@tonic-gate 	 * uintptr_t must be used because it matches the size of void *;
25847c478bd9Sstevel@tonic-gate 	 * mod_hash expects clients to place results into pointer-size
25857c478bd9Sstevel@tonic-gate 	 * containers; since di_off_t is always a 32-bit offset, alignment
25867c478bd9Sstevel@tonic-gate 	 * would otherwise be broken on 64-bit kernels.
25877c478bd9Sstevel@tonic-gate 	 */
25887c478bd9Sstevel@tonic-gate 	uintptr_t	offset;
25897c478bd9Sstevel@tonic-gate 	struct di_key	key = {0};
25907c478bd9Sstevel@tonic-gate 	struct di_pkey	*pk;
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 	ASSERT(st->reg_pip_hash);
25937c478bd9Sstevel@tonic-gate 	ASSERT(off_p);
25947c478bd9Sstevel@tonic-gate 
25957c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
25967c478bd9Sstevel@tonic-gate 		*off_p = 0;
25977c478bd9Sstevel@tonic-gate 		return (0);
25987c478bd9Sstevel@tonic-gate 	}
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate 	key.k_type = DI_PKEY;
26017c478bd9Sstevel@tonic-gate 	pk = &(key.k_u.pkey);
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate 	pk->pk_pip = pip;
26047c478bd9Sstevel@tonic-gate 	pk->pk_path_addr = mdi_pi_get_addr(pip);
26057c478bd9Sstevel@tonic-gate 	pk->pk_client = mdi_pi_get_client(pip);
26067c478bd9Sstevel@tonic-gate 	pk->pk_phci = mdi_pi_get_phci(pip);
26077c478bd9Sstevel@tonic-gate 
26087c478bd9Sstevel@tonic-gate 	if (mod_hash_find(st->reg_pip_hash, (mod_hash_key_t)&key,
26097c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t *)&offset) == 0) {
26107c478bd9Sstevel@tonic-gate 		*off_p = (di_off_t)offset;
26117c478bd9Sstevel@tonic-gate 		return (0);
26127c478bd9Sstevel@tonic-gate 	} else {
26137c478bd9Sstevel@tonic-gate 		return (-1);
26147c478bd9Sstevel@tonic-gate 	}
26157c478bd9Sstevel@tonic-gate }
26167c478bd9Sstevel@tonic-gate 
26177c478bd9Sstevel@tonic-gate static di_path_state_t
26187c478bd9Sstevel@tonic-gate path_state_convert(mdi_pathinfo_state_t st)
26197c478bd9Sstevel@tonic-gate {
26207c478bd9Sstevel@tonic-gate 	switch (st) {
26217c478bd9Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_ONLINE:
26227c478bd9Sstevel@tonic-gate 		return (DI_PATH_STATE_ONLINE);
26237c478bd9Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_STANDBY:
26247c478bd9Sstevel@tonic-gate 		return (DI_PATH_STATE_STANDBY);
26257c478bd9Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_OFFLINE:
26267c478bd9Sstevel@tonic-gate 		return (DI_PATH_STATE_OFFLINE);
26277c478bd9Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_FAULT:
26287c478bd9Sstevel@tonic-gate 		return (DI_PATH_STATE_FAULT);
26297c478bd9Sstevel@tonic-gate 	default:
26307c478bd9Sstevel@tonic-gate 		return (DI_PATH_STATE_UNKNOWN);
26317c478bd9Sstevel@tonic-gate 	}
26327c478bd9Sstevel@tonic-gate }
26337c478bd9Sstevel@tonic-gate 
26347c478bd9Sstevel@tonic-gate 
26357c478bd9Sstevel@tonic-gate static di_off_t
26367c478bd9Sstevel@tonic-gate di_path_getprop(mdi_pathinfo_t *pip, di_off_t off, di_off_t *off_p,
26377c478bd9Sstevel@tonic-gate     struct di_state *st)
26387c478bd9Sstevel@tonic-gate {
26397c478bd9Sstevel@tonic-gate 	nvpair_t *prop = NULL;
26407c478bd9Sstevel@tonic-gate 	struct di_path_prop *me;
26417c478bd9Sstevel@tonic-gate 
26427c478bd9Sstevel@tonic-gate 	if (mdi_pi_get_next_prop(pip, NULL) == NULL) {
26437c478bd9Sstevel@tonic-gate 		*off_p = 0;
26447c478bd9Sstevel@tonic-gate 		return (off);
26457c478bd9Sstevel@tonic-gate 	}
26467c478bd9Sstevel@tonic-gate 
26477c478bd9Sstevel@tonic-gate 	off = di_checkmem(st, off, sizeof (struct di_path_prop));
26487c478bd9Sstevel@tonic-gate 	*off_p = off;
26497c478bd9Sstevel@tonic-gate 
26507c478bd9Sstevel@tonic-gate 	while (prop = mdi_pi_get_next_prop(pip, prop)) {
26517c478bd9Sstevel@tonic-gate 		int delta = 0;
26527c478bd9Sstevel@tonic-gate 
26537c478bd9Sstevel@tonic-gate 		me = (struct di_path_prop *)di_mem_addr(st, off);
26547c478bd9Sstevel@tonic-gate 		me->self = off;
26557c478bd9Sstevel@tonic-gate 		off += sizeof (struct di_path_prop);
26567c478bd9Sstevel@tonic-gate 
26577c478bd9Sstevel@tonic-gate 		/*
26587c478bd9Sstevel@tonic-gate 		 * property name
26597c478bd9Sstevel@tonic-gate 		 */
26607c478bd9Sstevel@tonic-gate 		off = di_checkmem(st, off, strlen(nvpair_name(prop)) + 1);
26617c478bd9Sstevel@tonic-gate 		me->prop_name = off;
26627c478bd9Sstevel@tonic-gate 		(void) strcpy(di_mem_addr(st, off), nvpair_name(prop));
26637c478bd9Sstevel@tonic-gate 		off += strlen(nvpair_name(prop)) + 1;
26647c478bd9Sstevel@tonic-gate 
26657c478bd9Sstevel@tonic-gate 		switch (nvpair_type(prop)) {
26667c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BYTE:
26677c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT16:
26687c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT16:
26697c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT32:
26707c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT32:
26717c478bd9Sstevel@tonic-gate 			delta = sizeof (int32_t);
26727c478bd9Sstevel@tonic-gate 			me->prop_type = DDI_PROP_TYPE_INT;
26737c478bd9Sstevel@tonic-gate 			off = di_checkmem(st, off, delta);
26747c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int32(prop,
26757c478bd9Sstevel@tonic-gate 			    (int32_t *)di_mem_addr(st, off));
26767c478bd9Sstevel@tonic-gate 			break;
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT64:
26797c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT64:
26807c478bd9Sstevel@tonic-gate 			delta = sizeof (int64_t);
26817c478bd9Sstevel@tonic-gate 			me->prop_type = DDI_PROP_TYPE_INT64;
26827c478bd9Sstevel@tonic-gate 			off = di_checkmem(st, off, delta);
26837c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int64(prop,
26847c478bd9Sstevel@tonic-gate 			    (int64_t *)di_mem_addr(st, off));
26857c478bd9Sstevel@tonic-gate 			break;
26867c478bd9Sstevel@tonic-gate 
26877c478bd9Sstevel@tonic-gate 		case DATA_TYPE_STRING:
26887c478bd9Sstevel@tonic-gate 		{
26897c478bd9Sstevel@tonic-gate 			char *str;
26907c478bd9Sstevel@tonic-gate 			(void) nvpair_value_string(prop, &str);
26917c478bd9Sstevel@tonic-gate 			delta = strlen(str) + 1;
26927c478bd9Sstevel@tonic-gate 			me->prop_type = DDI_PROP_TYPE_STRING;
26937c478bd9Sstevel@tonic-gate 			off = di_checkmem(st, off, delta);
26947c478bd9Sstevel@tonic-gate 			(void) strcpy(di_mem_addr(st, off), str);
26957c478bd9Sstevel@tonic-gate 			break;
26967c478bd9Sstevel@tonic-gate 		}
26977c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BYTE_ARRAY:
26987c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT16_ARRAY:
26997c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT16_ARRAY:
27007c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT32_ARRAY:
27017c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT32_ARRAY:
27027c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT64_ARRAY:
27037c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT64_ARRAY:
27047c478bd9Sstevel@tonic-gate 		{
27057c478bd9Sstevel@tonic-gate 			uchar_t *buf;
27067c478bd9Sstevel@tonic-gate 			uint_t nelems;
27077c478bd9Sstevel@tonic-gate 			(void) nvpair_value_byte_array(prop, &buf, &nelems);
27087c478bd9Sstevel@tonic-gate 			delta = nelems;
27097c478bd9Sstevel@tonic-gate 			me->prop_type = DDI_PROP_TYPE_BYTE;
27107c478bd9Sstevel@tonic-gate 			if (nelems != 0) {
27117c478bd9Sstevel@tonic-gate 				off = di_checkmem(st, off, delta);
27127c478bd9Sstevel@tonic-gate 				bcopy(buf, di_mem_addr(st, off), nelems);
27137c478bd9Sstevel@tonic-gate 			}
27147c478bd9Sstevel@tonic-gate 			break;
27157c478bd9Sstevel@tonic-gate 		}
27167c478bd9Sstevel@tonic-gate 
27177c478bd9Sstevel@tonic-gate 		default:	/* Unknown or unhandled type; skip it */
27187c478bd9Sstevel@tonic-gate 			delta = 0;
27197c478bd9Sstevel@tonic-gate 			break;
27207c478bd9Sstevel@tonic-gate 		}
27217c478bd9Sstevel@tonic-gate 
27227c478bd9Sstevel@tonic-gate 		if (delta > 0) {
27237c478bd9Sstevel@tonic-gate 			me->prop_data = off;
27247c478bd9Sstevel@tonic-gate 		}
27257c478bd9Sstevel@tonic-gate 
27267c478bd9Sstevel@tonic-gate 		me->prop_len = delta;
27277c478bd9Sstevel@tonic-gate 		off += delta;
27287c478bd9Sstevel@tonic-gate 
27297c478bd9Sstevel@tonic-gate 		off = di_checkmem(st, off, sizeof (struct di_path_prop));
27307c478bd9Sstevel@tonic-gate 		me->prop_next = off;
27317c478bd9Sstevel@tonic-gate 	}
27327c478bd9Sstevel@tonic-gate 
27337c478bd9Sstevel@tonic-gate 	me->prop_next = 0;
27347c478bd9Sstevel@tonic-gate 	return (off);
27357c478bd9Sstevel@tonic-gate }
27367c478bd9Sstevel@tonic-gate 
27377c478bd9Sstevel@tonic-gate 
27387c478bd9Sstevel@tonic-gate static void
27397c478bd9Sstevel@tonic-gate di_path_one_endpoint(struct di_path *me, di_off_t noff, di_off_t **off_pp,
27407c478bd9Sstevel@tonic-gate     int get_client)
27417c478bd9Sstevel@tonic-gate {
27427c478bd9Sstevel@tonic-gate 	if (get_client) {
27437c478bd9Sstevel@tonic-gate 		ASSERT(me->path_client == 0);
27447c478bd9Sstevel@tonic-gate 		me->path_client = noff;
27457c478bd9Sstevel@tonic-gate 		ASSERT(me->path_c_link == 0);
27467c478bd9Sstevel@tonic-gate 		*off_pp = &me->path_c_link;
27477c478bd9Sstevel@tonic-gate 		me->path_snap_state &=
27487c478bd9Sstevel@tonic-gate 		    ~(DI_PATH_SNAP_NOCLIENT | DI_PATH_SNAP_NOCLINK);
27497c478bd9Sstevel@tonic-gate 	} else {
27507c478bd9Sstevel@tonic-gate 		ASSERT(me->path_phci == 0);
27517c478bd9Sstevel@tonic-gate 		me->path_phci = noff;
27527c478bd9Sstevel@tonic-gate 		ASSERT(me->path_p_link == 0);
27537c478bd9Sstevel@tonic-gate 		*off_pp = &me->path_p_link;
27547c478bd9Sstevel@tonic-gate 		me->path_snap_state &=
27557c478bd9Sstevel@tonic-gate 		    ~(DI_PATH_SNAP_NOPHCI | DI_PATH_SNAP_NOPLINK);
27567c478bd9Sstevel@tonic-gate 	}
27577c478bd9Sstevel@tonic-gate }
27587c478bd9Sstevel@tonic-gate 
27597c478bd9Sstevel@tonic-gate /*
27607c478bd9Sstevel@tonic-gate  * poff_p: pointer to the linkage field. This links pips along the client|phci
27617c478bd9Sstevel@tonic-gate  *	   linkage list.
27627c478bd9Sstevel@tonic-gate  * noff  : Offset for the endpoint dip snapshot.
27637c478bd9Sstevel@tonic-gate  */
27647c478bd9Sstevel@tonic-gate static di_off_t
27657c478bd9Sstevel@tonic-gate di_getpath_data(dev_info_t *dip, di_off_t *poff_p, di_off_t noff,
27667c478bd9Sstevel@tonic-gate     struct di_state *st, int get_client)
27677c478bd9Sstevel@tonic-gate {
27687c478bd9Sstevel@tonic-gate 	di_off_t off;
27697c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t *pip;
27707c478bd9Sstevel@tonic-gate 	struct di_path *me;
27717c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t *(*next_pip)(dev_info_t *, mdi_pathinfo_t *);
27727c478bd9Sstevel@tonic-gate 
27737c478bd9Sstevel@tonic-gate 	dcmn_err2((CE_WARN, "di_getpath_data: client = %d", get_client));
27747c478bd9Sstevel@tonic-gate 
27757c478bd9Sstevel@tonic-gate 	/*
27767c478bd9Sstevel@tonic-gate 	 * The naming of the following mdi_xyz() is unfortunately
27777c478bd9Sstevel@tonic-gate 	 * non-intuitive. mdi_get_next_phci_path() follows the
27787c478bd9Sstevel@tonic-gate 	 * client_link i.e. the list of pip's belonging to the
27797c478bd9Sstevel@tonic-gate 	 * given client dip.
27807c478bd9Sstevel@tonic-gate 	 */
27817c478bd9Sstevel@tonic-gate 	if (get_client)
27827c478bd9Sstevel@tonic-gate 		next_pip = &mdi_get_next_phci_path;
27837c478bd9Sstevel@tonic-gate 	else
27847c478bd9Sstevel@tonic-gate 		next_pip = &mdi_get_next_client_path;
27857c478bd9Sstevel@tonic-gate 
27867c478bd9Sstevel@tonic-gate 	off = *poff_p;
27877c478bd9Sstevel@tonic-gate 
27887c478bd9Sstevel@tonic-gate 	pip = NULL;
27897c478bd9Sstevel@tonic-gate 	while (pip = (*next_pip)(dip, pip)) {
27907c478bd9Sstevel@tonic-gate 		mdi_pathinfo_state_t state;
27917c478bd9Sstevel@tonic-gate 		di_off_t stored_offset;
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate 		dcmn_err((CE_WARN, "marshalling pip = %p", (void *)pip));
27947c478bd9Sstevel@tonic-gate 
27957c478bd9Sstevel@tonic-gate 		mdi_pi_lock(pip);
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate 		if (di_pip_find(st, pip, &stored_offset) != -1) {
27987c478bd9Sstevel@tonic-gate 			/*
27997c478bd9Sstevel@tonic-gate 			 * We've already seen this pathinfo node so we need to
28007c478bd9Sstevel@tonic-gate 			 * take care not to snap it again; However, one endpoint
28017c478bd9Sstevel@tonic-gate 			 * and linkage will be set here. The other endpoint
28027c478bd9Sstevel@tonic-gate 			 * and linkage has already been set when the pip was
28037c478bd9Sstevel@tonic-gate 			 * first snapshotted i.e. when the other endpoint dip
28047c478bd9Sstevel@tonic-gate 			 * was snapshotted.
28057c478bd9Sstevel@tonic-gate 			 */
28067c478bd9Sstevel@tonic-gate 			me = (struct di_path *)di_mem_addr(st, stored_offset);
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 			*poff_p = stored_offset;
28097c478bd9Sstevel@tonic-gate 
28107c478bd9Sstevel@tonic-gate 			di_path_one_endpoint(me, noff, &poff_p, get_client);
28117c478bd9Sstevel@tonic-gate 
28127c478bd9Sstevel@tonic-gate 			/*
28137c478bd9Sstevel@tonic-gate 			 * The other endpoint and linkage were set when this
28147c478bd9Sstevel@tonic-gate 			 * pip was snapshotted. So we are done with both
28157c478bd9Sstevel@tonic-gate 			 * endpoints and linkages.
28167c478bd9Sstevel@tonic-gate 			 */
28177c478bd9Sstevel@tonic-gate 			ASSERT(!(me->path_snap_state &
28187c478bd9Sstevel@tonic-gate 			    (DI_PATH_SNAP_NOCLIENT|DI_PATH_SNAP_NOPHCI)));
28197c478bd9Sstevel@tonic-gate 			ASSERT(!(me->path_snap_state &
28207c478bd9Sstevel@tonic-gate 			    (DI_PATH_SNAP_NOCLINK|DI_PATH_SNAP_NOPLINK)));
28217c478bd9Sstevel@tonic-gate 
28227c478bd9Sstevel@tonic-gate 			mdi_pi_unlock(pip);
28237c478bd9Sstevel@tonic-gate 			continue;
28247c478bd9Sstevel@tonic-gate 		}
28257c478bd9Sstevel@tonic-gate 
28267c478bd9Sstevel@tonic-gate 		/*
28277c478bd9Sstevel@tonic-gate 		 * Now that we need to snapshot this pip, check memory
28287c478bd9Sstevel@tonic-gate 		 */
28297c478bd9Sstevel@tonic-gate 		off = di_checkmem(st, off, sizeof (struct di_path));
28307c478bd9Sstevel@tonic-gate 		me = (struct di_path *)di_mem_addr(st, off);
28317c478bd9Sstevel@tonic-gate 		me->self = off;
28327c478bd9Sstevel@tonic-gate 		*poff_p = off;
28337c478bd9Sstevel@tonic-gate 		off += sizeof (struct di_path);
28347c478bd9Sstevel@tonic-gate 
28357c478bd9Sstevel@tonic-gate 		me->path_snap_state =
28367c478bd9Sstevel@tonic-gate 		    DI_PATH_SNAP_NOCLINK | DI_PATH_SNAP_NOPLINK;
28377c478bd9Sstevel@tonic-gate 		me->path_snap_state |=
28387c478bd9Sstevel@tonic-gate 		    DI_PATH_SNAP_NOCLIENT | DI_PATH_SNAP_NOPHCI;
28397c478bd9Sstevel@tonic-gate 
28407c478bd9Sstevel@tonic-gate 		/*
28417c478bd9Sstevel@tonic-gate 		 * Zero out fields as di_checkmem() doesn't guarantee
28427c478bd9Sstevel@tonic-gate 		 * zero-filled memory
28437c478bd9Sstevel@tonic-gate 		 */
28447c478bd9Sstevel@tonic-gate 		me->path_client = me->path_phci = 0;
28457c478bd9Sstevel@tonic-gate 		me->path_c_link = me->path_p_link = 0;
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate 		di_path_one_endpoint(me, noff, &poff_p, get_client);
28487c478bd9Sstevel@tonic-gate 
28497c478bd9Sstevel@tonic-gate 		/*
28507c478bd9Sstevel@tonic-gate 		 * Note the existence of this pathinfo
28517c478bd9Sstevel@tonic-gate 		 */
28527c478bd9Sstevel@tonic-gate 		di_register_pip(st, pip, me->self);
28537c478bd9Sstevel@tonic-gate 
28547c478bd9Sstevel@tonic-gate 		state = mdi_pi_get_state(pip);
28557c478bd9Sstevel@tonic-gate 		me->path_state = path_state_convert(state);
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate 		/*
28587c478bd9Sstevel@tonic-gate 		 * Get intermediate addressing info.
28597c478bd9Sstevel@tonic-gate 		 */
28607c478bd9Sstevel@tonic-gate 		off = di_checkmem(st, off, strlen(mdi_pi_get_addr(pip)) + 1);
28617c478bd9Sstevel@tonic-gate 		me->path_addr = off;
28627c478bd9Sstevel@tonic-gate 		(void) strcpy(di_mem_addr(st, off), mdi_pi_get_addr(pip));
28637c478bd9Sstevel@tonic-gate 		off += strlen(mdi_pi_get_addr(pip)) + 1;
28647c478bd9Sstevel@tonic-gate 
28657c478bd9Sstevel@tonic-gate 		/*
28667c478bd9Sstevel@tonic-gate 		 * Get path properties if props are to be included in the
28677c478bd9Sstevel@tonic-gate 		 * snapshot
28687c478bd9Sstevel@tonic-gate 		 */
28697c478bd9Sstevel@tonic-gate 		if (DINFOPROP & st->command) {
28707c478bd9Sstevel@tonic-gate 			off = di_path_getprop(pip, off, &me->path_prop, st);
28717c478bd9Sstevel@tonic-gate 		} else {
28727c478bd9Sstevel@tonic-gate 			me->path_prop = 0;
28737c478bd9Sstevel@tonic-gate 		}
28747c478bd9Sstevel@tonic-gate 
28757c478bd9Sstevel@tonic-gate 		mdi_pi_unlock(pip);
28767c478bd9Sstevel@tonic-gate 	}
28777c478bd9Sstevel@tonic-gate 
28787c478bd9Sstevel@tonic-gate 	*poff_p = 0;
28797c478bd9Sstevel@tonic-gate 
28807c478bd9Sstevel@tonic-gate 	return (off);
28817c478bd9Sstevel@tonic-gate }
28827c478bd9Sstevel@tonic-gate 
28837c478bd9Sstevel@tonic-gate /*
28847c478bd9Sstevel@tonic-gate  * Copy a list of properties attached to a devinfo node. Called from
28857c478bd9Sstevel@tonic-gate  * di_copynode with devi_lock held. The major number is passed in case
28867c478bd9Sstevel@tonic-gate  * we need to call driver's prop_op entry. The value of list indicates
28877c478bd9Sstevel@tonic-gate  * which list we are copying. Possible values are:
28887c478bd9Sstevel@tonic-gate  * DI_PROP_DRV_LIST, DI_PROP_SYS_LIST, DI_PROP_GLB_LIST, DI_PROP_HW_LIST
28897c478bd9Sstevel@tonic-gate  */
28907c478bd9Sstevel@tonic-gate static di_off_t
28917c478bd9Sstevel@tonic-gate di_getprop(struct ddi_prop *prop, di_off_t *off_p, struct di_state *st,
28927c478bd9Sstevel@tonic-gate 	struct dev_info *dip, int list)
28937c478bd9Sstevel@tonic-gate {
28947c478bd9Sstevel@tonic-gate 	dev_t dev;
28957c478bd9Sstevel@tonic-gate 	int (*prop_op)();
28967c478bd9Sstevel@tonic-gate 	int off, need_prop_op = 0;
28977c478bd9Sstevel@tonic-gate 	int prop_op_fail = 0;
28987c478bd9Sstevel@tonic-gate 	ddi_prop_t *propp = NULL;
28997c478bd9Sstevel@tonic-gate 	struct di_prop *pp;
29007c478bd9Sstevel@tonic-gate 	struct dev_ops *ops = NULL;
29017c478bd9Sstevel@tonic-gate 	int prop_len;
29027c478bd9Sstevel@tonic-gate 	caddr_t prop_val;
29037c478bd9Sstevel@tonic-gate 
29047c478bd9Sstevel@tonic-gate 
29057c478bd9Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_getprop:\n"));
29067c478bd9Sstevel@tonic-gate 
29077c478bd9Sstevel@tonic-gate 	ASSERT(st != NULL);
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate 	dcmn_err((CE_CONT, "copy property list at addr %p\n", (void *)prop));
29107c478bd9Sstevel@tonic-gate 
29117c478bd9Sstevel@tonic-gate 	/*
29127c478bd9Sstevel@tonic-gate 	 * Figure out if we need to call driver's prop_op entry point.
29137c478bd9Sstevel@tonic-gate 	 * The conditions are:
29147c478bd9Sstevel@tonic-gate 	 *	-- driver property list
29157c478bd9Sstevel@tonic-gate 	 *	-- driver must be attached and held
29167c478bd9Sstevel@tonic-gate 	 *	-- driver's cb_prop_op != ddi_prop_op
29177c478bd9Sstevel@tonic-gate 	 *		or parent's bus_prop_op != ddi_bus_prop_op
29187c478bd9Sstevel@tonic-gate 	 */
29197c478bd9Sstevel@tonic-gate 
29207c478bd9Sstevel@tonic-gate 	if (list != DI_PROP_DRV_LIST) {
29217c478bd9Sstevel@tonic-gate 		goto getprop;
29227c478bd9Sstevel@tonic-gate 	}
29237c478bd9Sstevel@tonic-gate 
29247c478bd9Sstevel@tonic-gate 	/*
29257c478bd9Sstevel@tonic-gate 	 * If driver is not attached or if major is -1, we ignore
29267c478bd9Sstevel@tonic-gate 	 * the driver property list. No one should rely on such
29277c478bd9Sstevel@tonic-gate 	 * properties.
29287c478bd9Sstevel@tonic-gate 	 */
2929737d277aScth 	if (!i_ddi_devi_attached((dev_info_t *)dip)) {
29307c478bd9Sstevel@tonic-gate 		off = *off_p;
29317c478bd9Sstevel@tonic-gate 		*off_p = 0;
29327c478bd9Sstevel@tonic-gate 		return (off);
29337c478bd9Sstevel@tonic-gate 	}
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate 	/*
29367c478bd9Sstevel@tonic-gate 	 * Now we have a driver which is held. We can examine entry points
29377c478bd9Sstevel@tonic-gate 	 * and check the condition listed above.
29387c478bd9Sstevel@tonic-gate 	 */
29397c478bd9Sstevel@tonic-gate 	ops = dip->devi_ops;
29407c478bd9Sstevel@tonic-gate 
29417c478bd9Sstevel@tonic-gate 	/*
29427c478bd9Sstevel@tonic-gate 	 * Some nexus drivers incorrectly set cb_prop_op to nodev,
29437c478bd9Sstevel@tonic-gate 	 * nulldev or even NULL.
29447c478bd9Sstevel@tonic-gate 	 */
29457c478bd9Sstevel@tonic-gate 	if (ops && ops->devo_cb_ops &&
29467c478bd9Sstevel@tonic-gate 	    (ops->devo_cb_ops->cb_prop_op != ddi_prop_op) &&
29477c478bd9Sstevel@tonic-gate 	    (ops->devo_cb_ops->cb_prop_op != nodev) &&
29487c478bd9Sstevel@tonic-gate 	    (ops->devo_cb_ops->cb_prop_op != nulldev) &&
29497c478bd9Sstevel@tonic-gate 	    (ops->devo_cb_ops->cb_prop_op != NULL)) {
29507c478bd9Sstevel@tonic-gate 		need_prop_op = 1;
29517c478bd9Sstevel@tonic-gate 	}
29527c478bd9Sstevel@tonic-gate 
29537c478bd9Sstevel@tonic-gate getprop:
29547c478bd9Sstevel@tonic-gate 	/*
29557c478bd9Sstevel@tonic-gate 	 * check memory availability
29567c478bd9Sstevel@tonic-gate 	 */
29577c478bd9Sstevel@tonic-gate 	off = di_checkmem(st, *off_p, sizeof (struct di_prop));
29587c478bd9Sstevel@tonic-gate 	*off_p = off;
29597c478bd9Sstevel@tonic-gate 	/*
29607c478bd9Sstevel@tonic-gate 	 * Now copy properties
29617c478bd9Sstevel@tonic-gate 	 */
29627c478bd9Sstevel@tonic-gate 	do {
29637c478bd9Sstevel@tonic-gate 		pp = (struct di_prop *)di_mem_addr(st, off);
29647c478bd9Sstevel@tonic-gate 		pp->self = off;
29657c478bd9Sstevel@tonic-gate 		/*
29667c478bd9Sstevel@tonic-gate 		 * Split dev_t to major/minor, so it works for
29677c478bd9Sstevel@tonic-gate 		 * both ILP32 and LP64 model
29687c478bd9Sstevel@tonic-gate 		 */
29697c478bd9Sstevel@tonic-gate 		pp->dev_major = getmajor(prop->prop_dev);
29707c478bd9Sstevel@tonic-gate 		pp->dev_minor = getminor(prop->prop_dev);
29717c478bd9Sstevel@tonic-gate 		pp->prop_flags = prop->prop_flags;
29727c478bd9Sstevel@tonic-gate 		pp->prop_list = list;
29737c478bd9Sstevel@tonic-gate 
29747c478bd9Sstevel@tonic-gate 		/*
29757c478bd9Sstevel@tonic-gate 		 * property name
29767c478bd9Sstevel@tonic-gate 		 */
29777c478bd9Sstevel@tonic-gate 		off += sizeof (struct di_prop);
29787c478bd9Sstevel@tonic-gate 		if (prop->prop_name) {
29797c478bd9Sstevel@tonic-gate 			off = di_checkmem(st, off, strlen(prop->prop_name)
29807c478bd9Sstevel@tonic-gate 			    + 1);
29817c478bd9Sstevel@tonic-gate 			pp->prop_name = off;
29827c478bd9Sstevel@tonic-gate 			(void) strcpy(di_mem_addr(st, off), prop->prop_name);
29837c478bd9Sstevel@tonic-gate 			off += strlen(prop->prop_name) + 1;
29847c478bd9Sstevel@tonic-gate 		}
29857c478bd9Sstevel@tonic-gate 
29867c478bd9Sstevel@tonic-gate 		/*
29877c478bd9Sstevel@tonic-gate 		 * Set prop_len here. This may change later
29887c478bd9Sstevel@tonic-gate 		 * if cb_prop_op returns a different length.
29897c478bd9Sstevel@tonic-gate 		 */
29907c478bd9Sstevel@tonic-gate 		pp->prop_len = prop->prop_len;
29917c478bd9Sstevel@tonic-gate 		if (!need_prop_op) {
29927c478bd9Sstevel@tonic-gate 			if (prop->prop_val == NULL) {
29937c478bd9Sstevel@tonic-gate 				dcmn_err((CE_WARN,
29947c478bd9Sstevel@tonic-gate 				    "devinfo: property fault at %p",
29957c478bd9Sstevel@tonic-gate 				    (void *)prop));
29967c478bd9Sstevel@tonic-gate 				pp->prop_data = -1;
29977c478bd9Sstevel@tonic-gate 			} else if (prop->prop_len != 0) {
29987c478bd9Sstevel@tonic-gate 				off = di_checkmem(st, off, prop->prop_len);
29997c478bd9Sstevel@tonic-gate 				pp->prop_data = off;
30007c478bd9Sstevel@tonic-gate 				bcopy(prop->prop_val, di_mem_addr(st, off),
30017c478bd9Sstevel@tonic-gate 				    prop->prop_len);
30027c478bd9Sstevel@tonic-gate 				off += DI_ALIGN(pp->prop_len);
30037c478bd9Sstevel@tonic-gate 			}
30047c478bd9Sstevel@tonic-gate 		}
30057c478bd9Sstevel@tonic-gate 
30067c478bd9Sstevel@tonic-gate 		off = di_checkmem(st, off, sizeof (struct di_prop));
30077c478bd9Sstevel@tonic-gate 		pp->next = off;
30087c478bd9Sstevel@tonic-gate 		prop = prop->prop_next;
30097c478bd9Sstevel@tonic-gate 	} while (prop);
30107c478bd9Sstevel@tonic-gate 
30117c478bd9Sstevel@tonic-gate 	pp->next = 0;
30127c478bd9Sstevel@tonic-gate 
30137c478bd9Sstevel@tonic-gate 	if (!need_prop_op) {
30147c478bd9Sstevel@tonic-gate 		dcmn_err((CE_CONT, "finished property "
30157c478bd9Sstevel@tonic-gate 		    "list at offset 0x%x\n", off));
30167c478bd9Sstevel@tonic-gate 		return (off);
30177c478bd9Sstevel@tonic-gate 	}
30187c478bd9Sstevel@tonic-gate 
30197c478bd9Sstevel@tonic-gate 	/*
30207c478bd9Sstevel@tonic-gate 	 * If there is a need to call driver's prop_op entry,
30217c478bd9Sstevel@tonic-gate 	 * we must release driver's devi_lock, because the
30227c478bd9Sstevel@tonic-gate 	 * cb_prop_op entry point will grab it.
30237c478bd9Sstevel@tonic-gate 	 *
30247c478bd9Sstevel@tonic-gate 	 * The snapshot memory has already been allocated above,
30257c478bd9Sstevel@tonic-gate 	 * which means the length of an active property should
30267c478bd9Sstevel@tonic-gate 	 * remain fixed for this implementation to work.
30277c478bd9Sstevel@tonic-gate 	 */
30287c478bd9Sstevel@tonic-gate 
30297c478bd9Sstevel@tonic-gate 
30307c478bd9Sstevel@tonic-gate 	prop_op = ops->devo_cb_ops->cb_prop_op;
30317c478bd9Sstevel@tonic-gate 	pp = (struct di_prop *)di_mem_addr(st, *off_p);
30327c478bd9Sstevel@tonic-gate 
30337c478bd9Sstevel@tonic-gate 	mutex_exit(&dip->devi_lock);
30347c478bd9Sstevel@tonic-gate 
30357c478bd9Sstevel@tonic-gate 	do {
30367c478bd9Sstevel@tonic-gate 		int err;
30377c478bd9Sstevel@tonic-gate 		struct di_prop *tmp;
30387c478bd9Sstevel@tonic-gate 
30397c478bd9Sstevel@tonic-gate 		if (pp->next) {
30407c478bd9Sstevel@tonic-gate 			tmp = (struct di_prop *)
30417c478bd9Sstevel@tonic-gate 			    di_mem_addr(st, pp->next);
30427c478bd9Sstevel@tonic-gate 		} else {
30437c478bd9Sstevel@tonic-gate 			tmp = NULL;
30447c478bd9Sstevel@tonic-gate 		}
30457c478bd9Sstevel@tonic-gate 
30467c478bd9Sstevel@tonic-gate 		/*
30477c478bd9Sstevel@tonic-gate 		 * call into driver's prop_op entry point
30487c478bd9Sstevel@tonic-gate 		 *
30497c478bd9Sstevel@tonic-gate 		 * Must search DDI_DEV_T_NONE with DDI_DEV_T_ANY
30507c478bd9Sstevel@tonic-gate 		 */
30517c478bd9Sstevel@tonic-gate 		dev = makedevice(pp->dev_major, pp->dev_minor);
30527c478bd9Sstevel@tonic-gate 		if (dev == DDI_DEV_T_NONE)
30537c478bd9Sstevel@tonic-gate 			dev = DDI_DEV_T_ANY;
30547c478bd9Sstevel@tonic-gate 
30557c478bd9Sstevel@tonic-gate 		dcmn_err((CE_CONT, "call prop_op"
30567c478bd9Sstevel@tonic-gate 		    "(%lx, %p, PROP_LEN_AND_VAL_BUF, "
30577c478bd9Sstevel@tonic-gate 		    "DDI_PROP_DONTPASS, \"%s\", %p, &%d)\n",
30587c478bd9Sstevel@tonic-gate 		    dev,
30597c478bd9Sstevel@tonic-gate 		    (void *)dip,
30607c478bd9Sstevel@tonic-gate 		    (char *)di_mem_addr(st, pp->prop_name),
30617c478bd9Sstevel@tonic-gate 		    (void *)di_mem_addr(st, pp->prop_data),
30627c478bd9Sstevel@tonic-gate 		    pp->prop_len));
30637c478bd9Sstevel@tonic-gate 
30647c478bd9Sstevel@tonic-gate 		if ((err = (*prop_op)(dev, (dev_info_t)dip,
30657c478bd9Sstevel@tonic-gate 		    PROP_LEN_AND_VAL_ALLOC, DDI_PROP_DONTPASS,
30667c478bd9Sstevel@tonic-gate 		    (char *)di_mem_addr(st, pp->prop_name),
30677c478bd9Sstevel@tonic-gate 		    &prop_val, &prop_len)) != DDI_PROP_SUCCESS) {
30687c478bd9Sstevel@tonic-gate 			if ((propp = i_ddi_prop_search(dev,
30697c478bd9Sstevel@tonic-gate 			    (char *)di_mem_addr(st, pp->prop_name),
30707c478bd9Sstevel@tonic-gate 			    (uint_t)pp->prop_flags,
30717c478bd9Sstevel@tonic-gate 			    &(DEVI(dip)->devi_drv_prop_ptr))) != NULL) {
30727c478bd9Sstevel@tonic-gate 				pp->prop_len = propp->prop_len;
30737c478bd9Sstevel@tonic-gate 				if (pp->prop_len != 0) {
30747c478bd9Sstevel@tonic-gate 					off = di_checkmem(st, off,
30757c478bd9Sstevel@tonic-gate 					    pp->prop_len);
30767c478bd9Sstevel@tonic-gate 					pp->prop_data = off;
30777c478bd9Sstevel@tonic-gate 					bcopy(propp->prop_val, di_mem_addr(st,
30787c478bd9Sstevel@tonic-gate 					    pp->prop_data), propp->prop_len);
30797c478bd9Sstevel@tonic-gate 					off += DI_ALIGN(pp->prop_len);
30807c478bd9Sstevel@tonic-gate 				}
30817c478bd9Sstevel@tonic-gate 			} else {
30827c478bd9Sstevel@tonic-gate 				prop_op_fail = 1;
30837c478bd9Sstevel@tonic-gate 			}
30847c478bd9Sstevel@tonic-gate 		} else if (prop_len != 0) {
30857c478bd9Sstevel@tonic-gate 			pp->prop_len = prop_len;
30867c478bd9Sstevel@tonic-gate 			off = di_checkmem(st, off, prop_len);
30877c478bd9Sstevel@tonic-gate 			pp->prop_data = off;
30887c478bd9Sstevel@tonic-gate 			bcopy(prop_val, di_mem_addr(st, off), prop_len);
30897c478bd9Sstevel@tonic-gate 			off += DI_ALIGN(prop_len);
30907c478bd9Sstevel@tonic-gate 			kmem_free(prop_val, prop_len);
30917c478bd9Sstevel@tonic-gate 		}
30927c478bd9Sstevel@tonic-gate 
30937c478bd9Sstevel@tonic-gate 		if (prop_op_fail) {
30947c478bd9Sstevel@tonic-gate 			pp->prop_data = -1;
30957c478bd9Sstevel@tonic-gate 			dcmn_err((CE_WARN, "devinfo: prop_op failure "
30967c478bd9Sstevel@tonic-gate 			    "for \"%s\" err %d",
30977c478bd9Sstevel@tonic-gate 			    di_mem_addr(st, pp->prop_name), err));
30987c478bd9Sstevel@tonic-gate 		}
30997c478bd9Sstevel@tonic-gate 
31007c478bd9Sstevel@tonic-gate 		pp = tmp;
31017c478bd9Sstevel@tonic-gate 
31027c478bd9Sstevel@tonic-gate 	} while (pp);
31037c478bd9Sstevel@tonic-gate 
31047c478bd9Sstevel@tonic-gate 	mutex_enter(&dip->devi_lock);
31057c478bd9Sstevel@tonic-gate 	dcmn_err((CE_CONT, "finished property list at offset 0x%x\n", off));
31067c478bd9Sstevel@tonic-gate 	return (off);
31077c478bd9Sstevel@tonic-gate }
31087c478bd9Sstevel@tonic-gate 
31097c478bd9Sstevel@tonic-gate /*
31107c478bd9Sstevel@tonic-gate  * find private data format attached to a dip
31117c478bd9Sstevel@tonic-gate  * parent = 1 to match driver name of parent dip (for parent private data)
31127c478bd9Sstevel@tonic-gate  *	0 to match driver name of current dip (for driver private data)
31137c478bd9Sstevel@tonic-gate  */
31147c478bd9Sstevel@tonic-gate #define	DI_MATCH_DRIVER	0
31157c478bd9Sstevel@tonic-gate #define	DI_MATCH_PARENT	1
31167c478bd9Sstevel@tonic-gate 
31177c478bd9Sstevel@tonic-gate struct di_priv_format *
31187c478bd9Sstevel@tonic-gate di_match_drv_name(struct dev_info *node, struct di_state *st, int match)
31197c478bd9Sstevel@tonic-gate {
31207c478bd9Sstevel@tonic-gate 	int i, count, len;
31217c478bd9Sstevel@tonic-gate 	char *drv_name;
31227c478bd9Sstevel@tonic-gate 	major_t major;
31237c478bd9Sstevel@tonic-gate 	struct di_all *all;
31247c478bd9Sstevel@tonic-gate 	struct di_priv_format *form;
31257c478bd9Sstevel@tonic-gate 
31267c478bd9Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_match_drv_name: node = %s, match = %x\n",
31277c478bd9Sstevel@tonic-gate 		node->devi_node_name, match));
31287c478bd9Sstevel@tonic-gate 
31297c478bd9Sstevel@tonic-gate 	if (match == DI_MATCH_PARENT) {
31307c478bd9Sstevel@tonic-gate 		node = DEVI(node->devi_parent);
31317c478bd9Sstevel@tonic-gate 	}
31327c478bd9Sstevel@tonic-gate 
31337c478bd9Sstevel@tonic-gate 	if (node == NULL) {
31347c478bd9Sstevel@tonic-gate 		return (NULL);
31357c478bd9Sstevel@tonic-gate 	}
31367c478bd9Sstevel@tonic-gate 
31377c478bd9Sstevel@tonic-gate 	major = ddi_name_to_major(node->devi_binding_name);
31387c478bd9Sstevel@tonic-gate 	if (major == (major_t)(-1)) {
31397c478bd9Sstevel@tonic-gate 		return (NULL);
31407c478bd9Sstevel@tonic-gate 	}
31417c478bd9Sstevel@tonic-gate 
31427c478bd9Sstevel@tonic-gate 	/*
31437c478bd9Sstevel@tonic-gate 	 * Match the driver name.
31447c478bd9Sstevel@tonic-gate 	 */
31457c478bd9Sstevel@tonic-gate 	drv_name = ddi_major_to_name(major);
31467c478bd9Sstevel@tonic-gate 	if ((drv_name == NULL) || *drv_name == '\0') {
31477c478bd9Sstevel@tonic-gate 		return (NULL);
31487c478bd9Sstevel@tonic-gate 	}
31497c478bd9Sstevel@tonic-gate 
31507c478bd9Sstevel@tonic-gate 	/* Now get the di_priv_format array */
31517c478bd9Sstevel@tonic-gate 	all = (struct di_all *)di_mem_addr(st, 0);
31527c478bd9Sstevel@tonic-gate 
31537c478bd9Sstevel@tonic-gate 	if (match == DI_MATCH_PARENT) {
31547c478bd9Sstevel@tonic-gate 		count = all->n_ppdata;
31557c478bd9Sstevel@tonic-gate 		form = (struct di_priv_format *)
31567c478bd9Sstevel@tonic-gate 			(di_mem_addr(st, 0) + all->ppdata_format);
31577c478bd9Sstevel@tonic-gate 	} else {
31587c478bd9Sstevel@tonic-gate 		count = all->n_dpdata;
31597c478bd9Sstevel@tonic-gate 		form = (struct di_priv_format *)
31607c478bd9Sstevel@tonic-gate 			((caddr_t)all + all->dpdata_format);
31617c478bd9Sstevel@tonic-gate 	}
31627c478bd9Sstevel@tonic-gate 
31637c478bd9Sstevel@tonic-gate 	len = strlen(drv_name);
31647c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
31657c478bd9Sstevel@tonic-gate 		char *tmp;
31667c478bd9Sstevel@tonic-gate 
31677c478bd9Sstevel@tonic-gate 		tmp = form[i].drv_name;
31687c478bd9Sstevel@tonic-gate 		while (tmp && (*tmp != '\0')) {
31697c478bd9Sstevel@tonic-gate 			if (strncmp(drv_name, tmp, len) == 0) {
31707c478bd9Sstevel@tonic-gate 				return (&form[i]);
31717c478bd9Sstevel@tonic-gate 			}
31727c478bd9Sstevel@tonic-gate 			/*
31737c478bd9Sstevel@tonic-gate 			 * Move to next driver name, skipping a white space
31747c478bd9Sstevel@tonic-gate 			 */
31757c478bd9Sstevel@tonic-gate 			if (tmp = strchr(tmp, ' ')) {
31767c478bd9Sstevel@tonic-gate 				tmp++;
31777c478bd9Sstevel@tonic-gate 			}
31787c478bd9Sstevel@tonic-gate 		}
31797c478bd9Sstevel@tonic-gate 	}
31807c478bd9Sstevel@tonic-gate 
31817c478bd9Sstevel@tonic-gate 	return (NULL);
31827c478bd9Sstevel@tonic-gate }
31837c478bd9Sstevel@tonic-gate 
31847c478bd9Sstevel@tonic-gate /*
31857c478bd9Sstevel@tonic-gate  * The following functions copy data as specified by the format passed in.
31867c478bd9Sstevel@tonic-gate  * To prevent invalid format from panicing the system, we call on_fault().
31877c478bd9Sstevel@tonic-gate  * A return value of 0 indicates an error. Otherwise, the total offset
31887c478bd9Sstevel@tonic-gate  * is returned.
31897c478bd9Sstevel@tonic-gate  */
31907c478bd9Sstevel@tonic-gate #define	DI_MAX_PRIVDATA	(PAGESIZE >> 1)	/* max private data size */
31917c478bd9Sstevel@tonic-gate 
31927c478bd9Sstevel@tonic-gate static di_off_t
3193*c3b0fe9bScth di_getprvdata(struct di_priv_format *pdp, struct dev_info *node,
3194*c3b0fe9bScth     void *data, di_off_t *off_p, struct di_state *st)
31957c478bd9Sstevel@tonic-gate {
31967c478bd9Sstevel@tonic-gate 	caddr_t pa;
31977c478bd9Sstevel@tonic-gate 	void *ptr;
31987c478bd9Sstevel@tonic-gate 	int i, size, repeat;
31997c478bd9Sstevel@tonic-gate 	di_off_t off, off0, *tmp;
3200*c3b0fe9bScth 	char *path;
32017c478bd9Sstevel@tonic-gate 
32027c478bd9Sstevel@tonic-gate 	label_t ljb;
32037c478bd9Sstevel@tonic-gate 
32047c478bd9Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_getprvdata:\n"));
32057c478bd9Sstevel@tonic-gate 
32067c478bd9Sstevel@tonic-gate 	/*
32077c478bd9Sstevel@tonic-gate 	 * check memory availability. Private data size is
32087c478bd9Sstevel@tonic-gate 	 * limited to DI_MAX_PRIVDATA.
32097c478bd9Sstevel@tonic-gate 	 */
32107c478bd9Sstevel@tonic-gate 	off = di_checkmem(st, *off_p, DI_MAX_PRIVDATA);
32117c478bd9Sstevel@tonic-gate 
32127c478bd9Sstevel@tonic-gate 	if ((pdp->bytes <= 0) || pdp->bytes > DI_MAX_PRIVDATA) {
32137c478bd9Sstevel@tonic-gate 		goto failure;
32147c478bd9Sstevel@tonic-gate 	}
32157c478bd9Sstevel@tonic-gate 
32167c478bd9Sstevel@tonic-gate 	if (!on_fault(&ljb)) {
32177c478bd9Sstevel@tonic-gate 		/* copy the struct */
32187c478bd9Sstevel@tonic-gate 		bcopy(data, di_mem_addr(st, off), pdp->bytes);
32197c478bd9Sstevel@tonic-gate 		off0 = DI_ALIGN(pdp->bytes);
32207c478bd9Sstevel@tonic-gate 
32217c478bd9Sstevel@tonic-gate 		/* dereferencing pointers */
32227c478bd9Sstevel@tonic-gate 		for (i = 0; i < MAX_PTR_IN_PRV; i++) {
32237c478bd9Sstevel@tonic-gate 
32247c478bd9Sstevel@tonic-gate 			if (pdp->ptr[i].size == 0) {
32257c478bd9Sstevel@tonic-gate 				goto success;	/* no more ptrs */
32267c478bd9Sstevel@tonic-gate 			}
32277c478bd9Sstevel@tonic-gate 
32287c478bd9Sstevel@tonic-gate 			/*
32297c478bd9Sstevel@tonic-gate 			 * first, get the pointer content
32307c478bd9Sstevel@tonic-gate 			 */
32317c478bd9Sstevel@tonic-gate 			if ((pdp->ptr[i].offset < 0) ||
32327c478bd9Sstevel@tonic-gate 				(pdp->ptr[i].offset >
32337c478bd9Sstevel@tonic-gate 				pdp->bytes - sizeof (char *)))
32347c478bd9Sstevel@tonic-gate 				goto failure;	/* wrong offset */
32357c478bd9Sstevel@tonic-gate 
32367c478bd9Sstevel@tonic-gate 			pa = di_mem_addr(st, off + pdp->ptr[i].offset);
32377c478bd9Sstevel@tonic-gate 			tmp = (di_off_t *)pa;	/* to store off_t later */
32387c478bd9Sstevel@tonic-gate 
32397c478bd9Sstevel@tonic-gate 			ptr = *((void **) pa);	/* get pointer value */
32407c478bd9Sstevel@tonic-gate 			if (ptr == NULL) {	/* if NULL pointer, go on */
32417c478bd9Sstevel@tonic-gate 				continue;
32427c478bd9Sstevel@tonic-gate 			}
32437c478bd9Sstevel@tonic-gate 
32447c478bd9Sstevel@tonic-gate 			/*
32457c478bd9Sstevel@tonic-gate 			 * next, find the repeat count (array dimension)
32467c478bd9Sstevel@tonic-gate 			 */
32477c478bd9Sstevel@tonic-gate 			repeat = pdp->ptr[i].len_offset;
32487c478bd9Sstevel@tonic-gate 
32497c478bd9Sstevel@tonic-gate 			/*
32507c478bd9Sstevel@tonic-gate 			 * Positive value indicates a fixed sized array.
32517c478bd9Sstevel@tonic-gate 			 * 0 or negative value indicates variable sized array.
32527c478bd9Sstevel@tonic-gate 			 *
32537c478bd9Sstevel@tonic-gate 			 * For variable sized array, the variable must be
32547c478bd9Sstevel@tonic-gate 			 * an int member of the structure, with an offset
32557c478bd9Sstevel@tonic-gate 			 * equal to the absolution value of struct member.
32567c478bd9Sstevel@tonic-gate 			 */
32577c478bd9Sstevel@tonic-gate 			if (repeat > pdp->bytes - sizeof (int)) {
32587c478bd9Sstevel@tonic-gate 				goto failure;	/* wrong offset */
32597c478bd9Sstevel@tonic-gate 			}
32607c478bd9Sstevel@tonic-gate 
32617c478bd9Sstevel@tonic-gate 			if (repeat >= 0) {
32627c478bd9Sstevel@tonic-gate 				repeat = *((int *)((caddr_t)data + repeat));
32637c478bd9Sstevel@tonic-gate 			} else {
32647c478bd9Sstevel@tonic-gate 				repeat = -repeat;
32657c478bd9Sstevel@tonic-gate 			}
32667c478bd9Sstevel@tonic-gate 
32677c478bd9Sstevel@tonic-gate 			/*
32687c478bd9Sstevel@tonic-gate 			 * next, get the size of the object to be copied
32697c478bd9Sstevel@tonic-gate 			 */
32707c478bd9Sstevel@tonic-gate 			size = pdp->ptr[i].size * repeat;
32717c478bd9Sstevel@tonic-gate 
32727c478bd9Sstevel@tonic-gate 			/*
32737c478bd9Sstevel@tonic-gate 			 * Arbitrarily limit the total size of object to be
32747c478bd9Sstevel@tonic-gate 			 * copied (1 byte to 1/4 page).
32757c478bd9Sstevel@tonic-gate 			 */
32767c478bd9Sstevel@tonic-gate 			if ((size <= 0) || (size > (DI_MAX_PRIVDATA - off0))) {
32777c478bd9Sstevel@tonic-gate 				goto failure;	/* wrong size or too big */
32787c478bd9Sstevel@tonic-gate 			}
32797c478bd9Sstevel@tonic-gate 
32807c478bd9Sstevel@tonic-gate 			/*
32817c478bd9Sstevel@tonic-gate 			 * Now copy the data
32827c478bd9Sstevel@tonic-gate 			 */
32837c478bd9Sstevel@tonic-gate 			*tmp = off0;
32847c478bd9Sstevel@tonic-gate 			bcopy(ptr, di_mem_addr(st, off + off0), size);
32857c478bd9Sstevel@tonic-gate 			off0 += DI_ALIGN(size);
32867c478bd9Sstevel@tonic-gate 		}
32877c478bd9Sstevel@tonic-gate 	} else {
32887c478bd9Sstevel@tonic-gate 		goto failure;
32897c478bd9Sstevel@tonic-gate 	}
32907c478bd9Sstevel@tonic-gate 
32917c478bd9Sstevel@tonic-gate success:
32927c478bd9Sstevel@tonic-gate 	/*
32937c478bd9Sstevel@tonic-gate 	 * success if reached here
32947c478bd9Sstevel@tonic-gate 	 */
32957c478bd9Sstevel@tonic-gate 	no_fault();
32967c478bd9Sstevel@tonic-gate 	*off_p = off;
32977c478bd9Sstevel@tonic-gate 
32987c478bd9Sstevel@tonic-gate 	return (off + off0);
32997c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
33007c478bd9Sstevel@tonic-gate 
33017c478bd9Sstevel@tonic-gate failure:
33027c478bd9Sstevel@tonic-gate 	/*
33037c478bd9Sstevel@tonic-gate 	 * fault occurred
33047c478bd9Sstevel@tonic-gate 	 */
33057c478bd9Sstevel@tonic-gate 	no_fault();
3306*c3b0fe9bScth 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3307*c3b0fe9bScth 	cmn_err(CE_WARN, "devinfo: fault on private data for '%s' at %p",
3308*c3b0fe9bScth 	    ddi_pathname((dev_info_t *)node, path), data);
3309*c3b0fe9bScth 	kmem_free(path, MAXPATHLEN);
33107c478bd9Sstevel@tonic-gate 	*off_p = -1;	/* set private data to indicate error */
33117c478bd9Sstevel@tonic-gate 
33127c478bd9Sstevel@tonic-gate 	return (off);
33137c478bd9Sstevel@tonic-gate }
33147c478bd9Sstevel@tonic-gate 
33157c478bd9Sstevel@tonic-gate /*
33167c478bd9Sstevel@tonic-gate  * get parent private data; on error, returns original offset
33177c478bd9Sstevel@tonic-gate  */
33187c478bd9Sstevel@tonic-gate static di_off_t
33197c478bd9Sstevel@tonic-gate di_getppdata(struct dev_info *node, di_off_t *off_p, struct di_state *st)
33207c478bd9Sstevel@tonic-gate {
33217c478bd9Sstevel@tonic-gate 	int off;
33227c478bd9Sstevel@tonic-gate 	struct di_priv_format *ppdp;
33237c478bd9Sstevel@tonic-gate 
33247c478bd9Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_getppdata:\n"));
33257c478bd9Sstevel@tonic-gate 
33267c478bd9Sstevel@tonic-gate 	/* find the parent data format */
33277c478bd9Sstevel@tonic-gate 	if ((ppdp = di_match_drv_name(node, st, DI_MATCH_PARENT)) == NULL) {
33287c478bd9Sstevel@tonic-gate 		off = *off_p;
33297c478bd9Sstevel@tonic-gate 		*off_p = 0;	/* set parent data to none */
33307c478bd9Sstevel@tonic-gate 		return (off);
33317c478bd9Sstevel@tonic-gate 	}
33327c478bd9Sstevel@tonic-gate 
3333*c3b0fe9bScth 	return (di_getprvdata(ppdp, node,
3334*c3b0fe9bScth 	    ddi_get_parent_data((dev_info_t *)node), off_p, st));
33357c478bd9Sstevel@tonic-gate }
33367c478bd9Sstevel@tonic-gate 
33377c478bd9Sstevel@tonic-gate /*
33387c478bd9Sstevel@tonic-gate  * get parent private data; returns original offset
33397c478bd9Sstevel@tonic-gate  */
33407c478bd9Sstevel@tonic-gate static di_off_t
33417c478bd9Sstevel@tonic-gate di_getdpdata(struct dev_info *node, di_off_t *off_p, struct di_state *st)
33427c478bd9Sstevel@tonic-gate {
33437c478bd9Sstevel@tonic-gate 	int off;
33447c478bd9Sstevel@tonic-gate 	struct di_priv_format *dpdp;
33457c478bd9Sstevel@tonic-gate 
33467c478bd9Sstevel@tonic-gate 	dcmn_err2((CE_CONT, "di_getdpdata:"));
33477c478bd9Sstevel@tonic-gate 
33487c478bd9Sstevel@tonic-gate 	/* find the parent data format */
33497c478bd9Sstevel@tonic-gate 	if ((dpdp = di_match_drv_name(node, st, DI_MATCH_DRIVER)) == NULL) {
33507c478bd9Sstevel@tonic-gate 		off = *off_p;
33517c478bd9Sstevel@tonic-gate 		*off_p = 0;	/* set driver data to none */
33527c478bd9Sstevel@tonic-gate 		return (off);
33537c478bd9Sstevel@tonic-gate 	}
33547c478bd9Sstevel@tonic-gate 
3355*c3b0fe9bScth 	return (di_getprvdata(dpdp, node,
3356*c3b0fe9bScth 	    ddi_get_driver_private((dev_info_t *)node), off_p, st));
33577c478bd9Sstevel@tonic-gate }
33587c478bd9Sstevel@tonic-gate 
33597c478bd9Sstevel@tonic-gate /*
33607c478bd9Sstevel@tonic-gate  * The driver is stateful across DINFOCPYALL and DINFOUSRLD.
33617c478bd9Sstevel@tonic-gate  * This function encapsulates the state machine:
33627c478bd9Sstevel@tonic-gate  *
33637c478bd9Sstevel@tonic-gate  *	-> IOC_IDLE -> IOC_SNAP -> IOC_DONE -> IOC_COPY ->
33647c478bd9Sstevel@tonic-gate  *	|		SNAPSHOT		USRLD	 |
33657c478bd9Sstevel@tonic-gate  *	--------------------------------------------------
33667c478bd9Sstevel@tonic-gate  *
33677c478bd9Sstevel@tonic-gate  * Returns 0 on success and -1 on failure
33687c478bd9Sstevel@tonic-gate  */
33697c478bd9Sstevel@tonic-gate static int
33707c478bd9Sstevel@tonic-gate di_setstate(struct di_state *st, int new_state)
33717c478bd9Sstevel@tonic-gate {
33727c478bd9Sstevel@tonic-gate 	int ret = 0;
33737c478bd9Sstevel@tonic-gate 
33747c478bd9Sstevel@tonic-gate 	mutex_enter(&di_lock);
33757c478bd9Sstevel@tonic-gate 	switch (new_state) {
33767c478bd9Sstevel@tonic-gate 	case IOC_IDLE:
33777c478bd9Sstevel@tonic-gate 	case IOC_DONE:
33787c478bd9Sstevel@tonic-gate 		break;
33797c478bd9Sstevel@tonic-gate 	case IOC_SNAP:
33807c478bd9Sstevel@tonic-gate 		if (st->di_iocstate != IOC_IDLE)
33817c478bd9Sstevel@tonic-gate 			ret = -1;
33827c478bd9Sstevel@tonic-gate 		break;
33837c478bd9Sstevel@tonic-gate 	case IOC_COPY:
33847c478bd9Sstevel@tonic-gate 		if (st->di_iocstate != IOC_DONE)
33857c478bd9Sstevel@tonic-gate 			ret = -1;
33867c478bd9Sstevel@tonic-gate 		break;
33877c478bd9Sstevel@tonic-gate 	default:
33887c478bd9Sstevel@tonic-gate 		ret = -1;
33897c478bd9Sstevel@tonic-gate 	}
33907c478bd9Sstevel@tonic-gate 
33917c478bd9Sstevel@tonic-gate 	if (ret == 0)
33927c478bd9Sstevel@tonic-gate 		st->di_iocstate = new_state;
33937c478bd9Sstevel@tonic-gate 	else
33947c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "incorrect state transition from %d to %d",
33957c478bd9Sstevel@tonic-gate 		    st->di_iocstate, new_state);
33967c478bd9Sstevel@tonic-gate 	mutex_exit(&di_lock);
33977c478bd9Sstevel@tonic-gate 	return (ret);
33987c478bd9Sstevel@tonic-gate }
33997c478bd9Sstevel@tonic-gate 
34007c478bd9Sstevel@tonic-gate /*
34017c478bd9Sstevel@tonic-gate  * We cannot assume the presence of the entire
34027c478bd9Sstevel@tonic-gate  * snapshot in this routine. All we are guaranteed
34037c478bd9Sstevel@tonic-gate  * is the di_all struct + 1 byte (for root_path)
34047c478bd9Sstevel@tonic-gate  */
34057c478bd9Sstevel@tonic-gate static int
34067c478bd9Sstevel@tonic-gate header_plus_one_ok(struct di_all *all)
34077c478bd9Sstevel@tonic-gate {
34087c478bd9Sstevel@tonic-gate 	/*
34097c478bd9Sstevel@tonic-gate 	 * Refuse to read old versions
34107c478bd9Sstevel@tonic-gate 	 */
34117c478bd9Sstevel@tonic-gate 	if (all->version != DI_SNAPSHOT_VERSION) {
34127c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "bad version: 0x%x", all->version));
34137c478bd9Sstevel@tonic-gate 		return (0);
34147c478bd9Sstevel@tonic-gate 	}
34157c478bd9Sstevel@tonic-gate 
34167c478bd9Sstevel@tonic-gate 	if (all->cache_magic != DI_CACHE_MAGIC) {
34177c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "bad magic #: 0x%x", all->cache_magic));
34187c478bd9Sstevel@tonic-gate 		return (0);
34197c478bd9Sstevel@tonic-gate 	}
34207c478bd9Sstevel@tonic-gate 
34217c478bd9Sstevel@tonic-gate 	if (all->snapshot_time <= 0) {
34227c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "bad timestamp: %ld", all->snapshot_time));
34237c478bd9Sstevel@tonic-gate 		return (0);
34247c478bd9Sstevel@tonic-gate 	}
34257c478bd9Sstevel@tonic-gate 
34267c478bd9Sstevel@tonic-gate 	if (all->top_devinfo == 0) {
34277c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "NULL top devinfo"));
34287c478bd9Sstevel@tonic-gate 		return (0);
34297c478bd9Sstevel@tonic-gate 	}
34307c478bd9Sstevel@tonic-gate 
34317c478bd9Sstevel@tonic-gate 	if (all->map_size < sizeof (*all) + 1) {
34327c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "bad map size: %u", all->map_size));
34337c478bd9Sstevel@tonic-gate 		return (0);
34347c478bd9Sstevel@tonic-gate 	}
34357c478bd9Sstevel@tonic-gate 
34367c478bd9Sstevel@tonic-gate 	if (all->root_path[0] != '/' || all->root_path[1] != '\0') {
34377c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "bad rootpath: %c%c",
34387c478bd9Sstevel@tonic-gate 		    all->root_path[0], all->root_path[1]));
34397c478bd9Sstevel@tonic-gate 		return (0);
34407c478bd9Sstevel@tonic-gate 	}
34417c478bd9Sstevel@tonic-gate 
34427c478bd9Sstevel@tonic-gate 	/*
34437c478bd9Sstevel@tonic-gate 	 * We can't check checksum here as we just have the header
34447c478bd9Sstevel@tonic-gate 	 */
34457c478bd9Sstevel@tonic-gate 
34467c478bd9Sstevel@tonic-gate 	return (1);
34477c478bd9Sstevel@tonic-gate }
34487c478bd9Sstevel@tonic-gate 
34497c478bd9Sstevel@tonic-gate static int
34507c478bd9Sstevel@tonic-gate chunk_write(struct vnode *vp, offset_t off, caddr_t buf, size_t len)
34517c478bd9Sstevel@tonic-gate {
34527c478bd9Sstevel@tonic-gate 	rlim64_t	rlimit;
34537c478bd9Sstevel@tonic-gate 	ssize_t		resid;
34547c478bd9Sstevel@tonic-gate 	int		error = 0;
34557c478bd9Sstevel@tonic-gate 
34567c478bd9Sstevel@tonic-gate 
34577c478bd9Sstevel@tonic-gate 	rlimit = RLIM64_INFINITY;
34587c478bd9Sstevel@tonic-gate 
34597c478bd9Sstevel@tonic-gate 	while (len) {
34607c478bd9Sstevel@tonic-gate 		resid = 0;
34617c478bd9Sstevel@tonic-gate 		error = vn_rdwr(UIO_WRITE, vp, buf, len, off,
34627c478bd9Sstevel@tonic-gate 		    UIO_SYSSPACE, FSYNC, rlimit, kcred, &resid);
34637c478bd9Sstevel@tonic-gate 
34647c478bd9Sstevel@tonic-gate 		if (error || resid < 0) {
34657c478bd9Sstevel@tonic-gate 			error = error ? error : EIO;
34667c478bd9Sstevel@tonic-gate 			CACHE_DEBUG((DI_ERR, "write error: %d", error));
34677c478bd9Sstevel@tonic-gate 			break;
34687c478bd9Sstevel@tonic-gate 		}
34697c478bd9Sstevel@tonic-gate 
34707c478bd9Sstevel@tonic-gate 		/*
34717c478bd9Sstevel@tonic-gate 		 * Check if we are making progress
34727c478bd9Sstevel@tonic-gate 		 */
34737c478bd9Sstevel@tonic-gate 		if (resid >= len) {
34747c478bd9Sstevel@tonic-gate 			error = ENOSPC;
34757c478bd9Sstevel@tonic-gate 			break;
34767c478bd9Sstevel@tonic-gate 		}
34777c478bd9Sstevel@tonic-gate 		buf += len - resid;
34787c478bd9Sstevel@tonic-gate 		off += len - resid;
34797c478bd9Sstevel@tonic-gate 		len = resid;
34807c478bd9Sstevel@tonic-gate 	}
34817c478bd9Sstevel@tonic-gate 
34827c478bd9Sstevel@tonic-gate 	return (error);
34837c478bd9Sstevel@tonic-gate }
34847c478bd9Sstevel@tonic-gate 
34857c478bd9Sstevel@tonic-gate extern int modrootloaded;
34868c4f8890Srs135747 extern void mdi_walk_vhcis(int (*)(dev_info_t *, void *), void *);
34878c4f8890Srs135747 extern void mdi_vhci_walk_phcis(dev_info_t *,
34888c4f8890Srs135747 	int (*)(dev_info_t *, void *), void *);
34897c478bd9Sstevel@tonic-gate 
34907c478bd9Sstevel@tonic-gate static void
34917c478bd9Sstevel@tonic-gate di_cache_write(struct di_cache *cache)
34927c478bd9Sstevel@tonic-gate {
34937c478bd9Sstevel@tonic-gate 	struct di_all	*all;
34947c478bd9Sstevel@tonic-gate 	struct vnode	*vp;
34957c478bd9Sstevel@tonic-gate 	int		oflags;
34967c478bd9Sstevel@tonic-gate 	size_t		map_size;
34977c478bd9Sstevel@tonic-gate 	size_t		chunk;
34987c478bd9Sstevel@tonic-gate 	offset_t	off;
34997c478bd9Sstevel@tonic-gate 	int		error;
35007c478bd9Sstevel@tonic-gate 	char		*buf;
35017c478bd9Sstevel@tonic-gate 
35027c478bd9Sstevel@tonic-gate 	ASSERT(DI_CACHE_LOCKED(*cache));
35037c478bd9Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
35047c478bd9Sstevel@tonic-gate 
35057c478bd9Sstevel@tonic-gate 	if (cache->cache_size == 0) {
35067c478bd9Sstevel@tonic-gate 		ASSERT(cache->cache_data == NULL);
35077c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "Empty cache. Skipping write"));
35087c478bd9Sstevel@tonic-gate 		return;
35097c478bd9Sstevel@tonic-gate 	}
35107c478bd9Sstevel@tonic-gate 
35117c478bd9Sstevel@tonic-gate 	ASSERT(cache->cache_size > 0);
35127c478bd9Sstevel@tonic-gate 	ASSERT(cache->cache_data);
35137c478bd9Sstevel@tonic-gate 
35147c478bd9Sstevel@tonic-gate 	if (!modrootloaded || rootvp == NULL || vn_is_readonly(rootvp)) {
35157c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "Can't write to rootFS. Skipping write"));
35167c478bd9Sstevel@tonic-gate 		return;
35177c478bd9Sstevel@tonic-gate 	}
35187c478bd9Sstevel@tonic-gate 
35197c478bd9Sstevel@tonic-gate 	all = (struct di_all *)cache->cache_data;
35207c478bd9Sstevel@tonic-gate 
35217c478bd9Sstevel@tonic-gate 	if (!header_plus_one_ok(all)) {
35227c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "Invalid header. Skipping write"));
35237c478bd9Sstevel@tonic-gate 		return;
35247c478bd9Sstevel@tonic-gate 	}
35257c478bd9Sstevel@tonic-gate 
35267c478bd9Sstevel@tonic-gate 	ASSERT(strcmp(all->root_path, "/") == 0);
35277c478bd9Sstevel@tonic-gate 
35287c478bd9Sstevel@tonic-gate 	/*
35297c478bd9Sstevel@tonic-gate 	 * The cache_size is the total allocated memory for the cache.
35307c478bd9Sstevel@tonic-gate 	 * The map_size is the actual size of valid data in the cache.
35317c478bd9Sstevel@tonic-gate 	 * map_size may be smaller than cache_size but cannot exceed
35327c478bd9Sstevel@tonic-gate 	 * cache_size.
35337c478bd9Sstevel@tonic-gate 	 */
35347c478bd9Sstevel@tonic-gate 	if (all->map_size > cache->cache_size) {
35357c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "map_size (0x%x) > cache_size (0x%x)."
35367c478bd9Sstevel@tonic-gate 		    " Skipping write", all->map_size, cache->cache_size));
35377c478bd9Sstevel@tonic-gate 		return;
35387c478bd9Sstevel@tonic-gate 	}
35397c478bd9Sstevel@tonic-gate 
35407c478bd9Sstevel@tonic-gate 	/*
35417c478bd9Sstevel@tonic-gate 	 * First unlink the temp file
35427c478bd9Sstevel@tonic-gate 	 */
35437c478bd9Sstevel@tonic-gate 	error = vn_remove(DI_CACHE_TEMP, UIO_SYSSPACE, RMFILE);
35447c478bd9Sstevel@tonic-gate 	if (error && error != ENOENT) {
35457c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "%s: unlink failed: %d",
35467c478bd9Sstevel@tonic-gate 		    DI_CACHE_TEMP, error));
35477c478bd9Sstevel@tonic-gate 	}
35487c478bd9Sstevel@tonic-gate 
35497c478bd9Sstevel@tonic-gate 	if (error == EROFS) {
35507c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "RDONLY FS. Skipping write"));
35517c478bd9Sstevel@tonic-gate 		return;
35527c478bd9Sstevel@tonic-gate 	}
35537c478bd9Sstevel@tonic-gate 
35547c478bd9Sstevel@tonic-gate 	vp = NULL;
35557c478bd9Sstevel@tonic-gate 	oflags = (FCREAT|FWRITE);
35567c478bd9Sstevel@tonic-gate 	if (error = vn_open(DI_CACHE_TEMP, UIO_SYSSPACE, oflags,
35577c478bd9Sstevel@tonic-gate 	    DI_CACHE_PERMS, &vp, CRCREAT, 0)) {
35587c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "%s: create failed: %d",
35597c478bd9Sstevel@tonic-gate 		    DI_CACHE_TEMP, error));
35607c478bd9Sstevel@tonic-gate 		return;
35617c478bd9Sstevel@tonic-gate 	}
35627c478bd9Sstevel@tonic-gate 
35637c478bd9Sstevel@tonic-gate 	ASSERT(vp);
35647c478bd9Sstevel@tonic-gate 
35657c478bd9Sstevel@tonic-gate 	/*
35667c478bd9Sstevel@tonic-gate 	 * Paranoid: Check if the file is on a read-only FS
35677c478bd9Sstevel@tonic-gate 	 */
35687c478bd9Sstevel@tonic-gate 	if (vn_is_readonly(vp)) {
35697c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "cannot write: readonly FS"));
35707c478bd9Sstevel@tonic-gate 		goto fail;
35717c478bd9Sstevel@tonic-gate 	}
35727c478bd9Sstevel@tonic-gate 
35737c478bd9Sstevel@tonic-gate 	/*
35747c478bd9Sstevel@tonic-gate 	 * Note that we only write map_size bytes to disk - this saves
35757c478bd9Sstevel@tonic-gate 	 * space as the actual cache size may be larger than size of
35767c478bd9Sstevel@tonic-gate 	 * valid data in the cache.
35777c478bd9Sstevel@tonic-gate 	 * Another advantage is that it makes verification of size
35787c478bd9Sstevel@tonic-gate 	 * easier when the file is read later.
35797c478bd9Sstevel@tonic-gate 	 */
35807c478bd9Sstevel@tonic-gate 	map_size = all->map_size;
35817c478bd9Sstevel@tonic-gate 	off = 0;
35827c478bd9Sstevel@tonic-gate 	buf = cache->cache_data;
35837c478bd9Sstevel@tonic-gate 
35847c478bd9Sstevel@tonic-gate 	while (map_size) {
35857c478bd9Sstevel@tonic-gate 		ASSERT(map_size > 0);
35867c478bd9Sstevel@tonic-gate 		/*
35877c478bd9Sstevel@tonic-gate 		 * Write in chunks so that VM system
35887c478bd9Sstevel@tonic-gate 		 * is not overwhelmed
35897c478bd9Sstevel@tonic-gate 		 */
35907c478bd9Sstevel@tonic-gate 		if (map_size > di_chunk * PAGESIZE)
35917c478bd9Sstevel@tonic-gate 			chunk = di_chunk * PAGESIZE;
35927c478bd9Sstevel@tonic-gate 		else
35937c478bd9Sstevel@tonic-gate 			chunk = map_size;
35947c478bd9Sstevel@tonic-gate 
35957c478bd9Sstevel@tonic-gate 		error = chunk_write(vp, off, buf, chunk);
35967c478bd9Sstevel@tonic-gate 		if (error) {
35977c478bd9Sstevel@tonic-gate 			CACHE_DEBUG((DI_ERR, "write failed: off=0x%x: %d",
35987c478bd9Sstevel@tonic-gate 			    off, error));
35997c478bd9Sstevel@tonic-gate 			goto fail;
36007c478bd9Sstevel@tonic-gate 		}
36017c478bd9Sstevel@tonic-gate 
36027c478bd9Sstevel@tonic-gate 		off += chunk;
36037c478bd9Sstevel@tonic-gate 		buf += chunk;
36047c478bd9Sstevel@tonic-gate 		map_size -= chunk;
36057c478bd9Sstevel@tonic-gate 
36067c478bd9Sstevel@tonic-gate 		/* Give pageout a chance to run */
36077c478bd9Sstevel@tonic-gate 		delay(1);
36087c478bd9Sstevel@tonic-gate 	}
36097c478bd9Sstevel@tonic-gate 
36107c478bd9Sstevel@tonic-gate 	/*
36117c478bd9Sstevel@tonic-gate 	 * Now sync the file and close it
36127c478bd9Sstevel@tonic-gate 	 */
36137c478bd9Sstevel@tonic-gate 	if (error = VOP_FSYNC(vp, FSYNC, kcred)) {
36147c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "FSYNC failed: %d", error));
36157c478bd9Sstevel@tonic-gate 	}
36167c478bd9Sstevel@tonic-gate 
36177c478bd9Sstevel@tonic-gate 	if (error = VOP_CLOSE(vp, oflags, 1, (offset_t)0, kcred)) {
36187c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "close() failed: %d", error));
36197c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
36207c478bd9Sstevel@tonic-gate 		return;
36217c478bd9Sstevel@tonic-gate 	}
36227c478bd9Sstevel@tonic-gate 
36237c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
36247c478bd9Sstevel@tonic-gate 
36257c478bd9Sstevel@tonic-gate 	/*
36267c478bd9Sstevel@tonic-gate 	 * Now do the rename
36277c478bd9Sstevel@tonic-gate 	 */
36287c478bd9Sstevel@tonic-gate 	if (error = vn_rename(DI_CACHE_TEMP, DI_CACHE_FILE, UIO_SYSSPACE)) {
36297c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "rename failed: %d", error));
36307c478bd9Sstevel@tonic-gate 		return;
36317c478bd9Sstevel@tonic-gate 	}
36327c478bd9Sstevel@tonic-gate 
36337c478bd9Sstevel@tonic-gate 	CACHE_DEBUG((DI_INFO, "Cache write successful."));
36347c478bd9Sstevel@tonic-gate 
36357c478bd9Sstevel@tonic-gate 	return;
36367c478bd9Sstevel@tonic-gate 
36377c478bd9Sstevel@tonic-gate fail:
36387c478bd9Sstevel@tonic-gate 	(void) VOP_CLOSE(vp, oflags, 1, (offset_t)0, kcred);
36397c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
36407c478bd9Sstevel@tonic-gate }
36417c478bd9Sstevel@tonic-gate 
36427c478bd9Sstevel@tonic-gate 
36437c478bd9Sstevel@tonic-gate /*
36447c478bd9Sstevel@tonic-gate  * Since we could be called early in boot,
36457c478bd9Sstevel@tonic-gate  * use kobj_read_file()
36467c478bd9Sstevel@tonic-gate  */
36477c478bd9Sstevel@tonic-gate static void
36487c478bd9Sstevel@tonic-gate di_cache_read(struct di_cache *cache)
36497c478bd9Sstevel@tonic-gate {
36507c478bd9Sstevel@tonic-gate 	struct _buf	*file;
36517c478bd9Sstevel@tonic-gate 	struct di_all	*all;
36527c478bd9Sstevel@tonic-gate 	int		n;
36537c478bd9Sstevel@tonic-gate 	size_t		map_size, sz, chunk;
36547c478bd9Sstevel@tonic-gate 	offset_t	off;
36557c478bd9Sstevel@tonic-gate 	caddr_t		buf;
36567c478bd9Sstevel@tonic-gate 	uint32_t	saved_crc, crc;
36577c478bd9Sstevel@tonic-gate 
36587c478bd9Sstevel@tonic-gate 	ASSERT(modrootloaded);
36597c478bd9Sstevel@tonic-gate 	ASSERT(DI_CACHE_LOCKED(*cache));
36607c478bd9Sstevel@tonic-gate 	ASSERT(cache->cache_data == NULL);
36617c478bd9Sstevel@tonic-gate 	ASSERT(cache->cache_size == 0);
36627c478bd9Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
36637c478bd9Sstevel@tonic-gate 
36647c478bd9Sstevel@tonic-gate 	file = kobj_open_file(DI_CACHE_FILE);
36657c478bd9Sstevel@tonic-gate 	if (file == (struct _buf *)-1) {
36667c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "%s: open failed: %d",
36677c478bd9Sstevel@tonic-gate 		    DI_CACHE_FILE, ENOENT));
36687c478bd9Sstevel@tonic-gate 		return;
36697c478bd9Sstevel@tonic-gate 	}
36707c478bd9Sstevel@tonic-gate 
36717c478bd9Sstevel@tonic-gate 	/*
36727c478bd9Sstevel@tonic-gate 	 * Read in the header+root_path first. The root_path must be "/"
36737c478bd9Sstevel@tonic-gate 	 */
36747c478bd9Sstevel@tonic-gate 	all = kmem_zalloc(sizeof (*all) + 1, KM_SLEEP);
36757c478bd9Sstevel@tonic-gate 	n = kobj_read_file(file, (caddr_t)all, sizeof (*all) + 1, 0);
36767c478bd9Sstevel@tonic-gate 
36777c478bd9Sstevel@tonic-gate 	if ((n != sizeof (*all) + 1) || !header_plus_one_ok(all)) {
36787c478bd9Sstevel@tonic-gate 		kmem_free(all, sizeof (*all) + 1);
36797c478bd9Sstevel@tonic-gate 		kobj_close_file(file);
36807c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "cache header: read error or invalid"));
36817c478bd9Sstevel@tonic-gate 		return;
36827c478bd9Sstevel@tonic-gate 	}
36837c478bd9Sstevel@tonic-gate 
36847c478bd9Sstevel@tonic-gate 	map_size = all->map_size;
36857c478bd9Sstevel@tonic-gate 
36867c478bd9Sstevel@tonic-gate 	kmem_free(all, sizeof (*all) + 1);
36877c478bd9Sstevel@tonic-gate 
36887c478bd9Sstevel@tonic-gate 	ASSERT(map_size >= sizeof (*all) + 1);
36897c478bd9Sstevel@tonic-gate 
36907c478bd9Sstevel@tonic-gate 	buf = di_cache.cache_data = kmem_alloc(map_size, KM_SLEEP);
36917c478bd9Sstevel@tonic-gate 	sz = map_size;
36927c478bd9Sstevel@tonic-gate 	off = 0;
36937c478bd9Sstevel@tonic-gate 	while (sz) {
36947c478bd9Sstevel@tonic-gate 		/* Don't overload VM with large reads */
36957c478bd9Sstevel@tonic-gate 		chunk = (sz > di_chunk * PAGESIZE) ? di_chunk * PAGESIZE : sz;
36967c478bd9Sstevel@tonic-gate 		n = kobj_read_file(file, buf, chunk, off);
36977c478bd9Sstevel@tonic-gate 		if (n != chunk) {
36987c478bd9Sstevel@tonic-gate 			CACHE_DEBUG((DI_ERR, "%s: read error at offset: %lld",
36997c478bd9Sstevel@tonic-gate 			    DI_CACHE_FILE, off));
37007c478bd9Sstevel@tonic-gate 			goto fail;
37017c478bd9Sstevel@tonic-gate 		}
37027c478bd9Sstevel@tonic-gate 		off += chunk;
37037c478bd9Sstevel@tonic-gate 		buf += chunk;
37047c478bd9Sstevel@tonic-gate 		sz -= chunk;
37057c478bd9Sstevel@tonic-gate 	}
37067c478bd9Sstevel@tonic-gate 
37077c478bd9Sstevel@tonic-gate 	ASSERT(off == map_size);
37087c478bd9Sstevel@tonic-gate 
37097c478bd9Sstevel@tonic-gate 	/*
37107c478bd9Sstevel@tonic-gate 	 * Read past expected EOF to verify size.
37117c478bd9Sstevel@tonic-gate 	 */
37127c478bd9Sstevel@tonic-gate 	if (kobj_read_file(file, (caddr_t)&sz, 1, off) > 0) {
37137c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "%s: file size changed", DI_CACHE_FILE));
37147c478bd9Sstevel@tonic-gate 		goto fail;
37157c478bd9Sstevel@tonic-gate 	}
37167c478bd9Sstevel@tonic-gate 
37177c478bd9Sstevel@tonic-gate 	all = (struct di_all *)di_cache.cache_data;
37187c478bd9Sstevel@tonic-gate 	if (!header_plus_one_ok(all)) {
37197c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "%s: file header changed", DI_CACHE_FILE));
37207c478bd9Sstevel@tonic-gate 		goto fail;
37217c478bd9Sstevel@tonic-gate 	}
37227c478bd9Sstevel@tonic-gate 
37237c478bd9Sstevel@tonic-gate 	/*
37247c478bd9Sstevel@tonic-gate 	 * Compute CRC with checksum field in the cache data set to 0
37257c478bd9Sstevel@tonic-gate 	 */
37267c478bd9Sstevel@tonic-gate 	saved_crc = all->cache_checksum;
37277c478bd9Sstevel@tonic-gate 	all->cache_checksum = 0;
37287c478bd9Sstevel@tonic-gate 	CRC32(crc, di_cache.cache_data, map_size, -1U, crc32_table);
37297c478bd9Sstevel@tonic-gate 	all->cache_checksum = saved_crc;
37307c478bd9Sstevel@tonic-gate 
37317c478bd9Sstevel@tonic-gate 	if (crc != all->cache_checksum) {
37327c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR,
37337c478bd9Sstevel@tonic-gate 		    "%s: checksum error: expected=0x%x actual=0x%x",
37347c478bd9Sstevel@tonic-gate 		    DI_CACHE_FILE, all->cache_checksum, crc));
37357c478bd9Sstevel@tonic-gate 		goto fail;
37367c478bd9Sstevel@tonic-gate 	}
37377c478bd9Sstevel@tonic-gate 
37387c478bd9Sstevel@tonic-gate 	if (all->map_size != map_size) {
37397c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "%s: map size changed", DI_CACHE_FILE));
37407c478bd9Sstevel@tonic-gate 		goto fail;
37417c478bd9Sstevel@tonic-gate 	}
37427c478bd9Sstevel@tonic-gate 
37437c478bd9Sstevel@tonic-gate 	kobj_close_file(file);
37447c478bd9Sstevel@tonic-gate 
37457c478bd9Sstevel@tonic-gate 	di_cache.cache_size = map_size;
37467c478bd9Sstevel@tonic-gate 
37477c478bd9Sstevel@tonic-gate 	return;
37487c478bd9Sstevel@tonic-gate 
37497c478bd9Sstevel@tonic-gate fail:
37507c478bd9Sstevel@tonic-gate 	kmem_free(di_cache.cache_data, map_size);
37517c478bd9Sstevel@tonic-gate 	kobj_close_file(file);
37527c478bd9Sstevel@tonic-gate 	di_cache.cache_data = NULL;
37537c478bd9Sstevel@tonic-gate 	di_cache.cache_size = 0;
37547c478bd9Sstevel@tonic-gate }
37557c478bd9Sstevel@tonic-gate 
37567c478bd9Sstevel@tonic-gate 
37577c478bd9Sstevel@tonic-gate /*
37587c478bd9Sstevel@tonic-gate  * Checks if arguments are valid for using the cache.
37597c478bd9Sstevel@tonic-gate  */
37607c478bd9Sstevel@tonic-gate static int
37617c478bd9Sstevel@tonic-gate cache_args_valid(struct di_state *st, int *error)
37627c478bd9Sstevel@tonic-gate {
37637c478bd9Sstevel@tonic-gate 	ASSERT(error);
37647c478bd9Sstevel@tonic-gate 	ASSERT(st->mem_size > 0);
37657c478bd9Sstevel@tonic-gate 	ASSERT(st->memlist != NULL);
37667c478bd9Sstevel@tonic-gate 
37677c478bd9Sstevel@tonic-gate 	if (!modrootloaded || !i_ddi_io_initialized()) {
37687c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR,
37697c478bd9Sstevel@tonic-gate 		    "cache lookup failure: I/O subsystem not inited"));
37707c478bd9Sstevel@tonic-gate 		*error = ENOTACTIVE;
37717c478bd9Sstevel@tonic-gate 		return (0);
37727c478bd9Sstevel@tonic-gate 	}
37737c478bd9Sstevel@tonic-gate 
37747c478bd9Sstevel@tonic-gate 	/*
37757c478bd9Sstevel@tonic-gate 	 * No other flags allowed with DINFOCACHE
37767c478bd9Sstevel@tonic-gate 	 */
37777c478bd9Sstevel@tonic-gate 	if (st->command != (DINFOCACHE & DIIOC_MASK)) {
37787c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR,
37797c478bd9Sstevel@tonic-gate 		    "cache lookup failure: bad flags: 0x%x",
37807c478bd9Sstevel@tonic-gate 		    st->command));
37817c478bd9Sstevel@tonic-gate 		*error = EINVAL;
37827c478bd9Sstevel@tonic-gate 		return (0);
37837c478bd9Sstevel@tonic-gate 	}
37847c478bd9Sstevel@tonic-gate 
37857c478bd9Sstevel@tonic-gate 	if (strcmp(DI_ALL_PTR(st)->root_path, "/") != 0) {
37867c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR,
37877c478bd9Sstevel@tonic-gate 		    "cache lookup failure: bad root: %s",
37887c478bd9Sstevel@tonic-gate 		    DI_ALL_PTR(st)->root_path));
37897c478bd9Sstevel@tonic-gate 		*error = EINVAL;
37907c478bd9Sstevel@tonic-gate 		return (0);
37917c478bd9Sstevel@tonic-gate 	}
37927c478bd9Sstevel@tonic-gate 
37937c478bd9Sstevel@tonic-gate 	CACHE_DEBUG((DI_INFO, "cache lookup args ok: 0x%x", st->command));
37947c478bd9Sstevel@tonic-gate 
37957c478bd9Sstevel@tonic-gate 	*error = 0;
37967c478bd9Sstevel@tonic-gate 
37977c478bd9Sstevel@tonic-gate 	return (1);
37987c478bd9Sstevel@tonic-gate }
37997c478bd9Sstevel@tonic-gate 
38007c478bd9Sstevel@tonic-gate static int
38017c478bd9Sstevel@tonic-gate snapshot_is_cacheable(struct di_state *st)
38027c478bd9Sstevel@tonic-gate {
38037c478bd9Sstevel@tonic-gate 	ASSERT(st->mem_size > 0);
38047c478bd9Sstevel@tonic-gate 	ASSERT(st->memlist != NULL);
38057c478bd9Sstevel@tonic-gate 
38063c34adc5Sramat 	if ((st->command & DI_CACHE_SNAPSHOT_FLAGS) !=
38073c34adc5Sramat 	    (DI_CACHE_SNAPSHOT_FLAGS & DIIOC_MASK)) {
38087c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_INFO,
38097c478bd9Sstevel@tonic-gate 		    "not cacheable: incompatible flags: 0x%x",
38107c478bd9Sstevel@tonic-gate 		    st->command));
38117c478bd9Sstevel@tonic-gate 		return (0);
38127c478bd9Sstevel@tonic-gate 	}
38137c478bd9Sstevel@tonic-gate 
38147c478bd9Sstevel@tonic-gate 	if (strcmp(DI_ALL_PTR(st)->root_path, "/") != 0) {
38157c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_INFO,
38167c478bd9Sstevel@tonic-gate 		    "not cacheable: incompatible root path: %s",
38177c478bd9Sstevel@tonic-gate 		    DI_ALL_PTR(st)->root_path));
38187c478bd9Sstevel@tonic-gate 		return (0);
38197c478bd9Sstevel@tonic-gate 	}
38207c478bd9Sstevel@tonic-gate 
38217c478bd9Sstevel@tonic-gate 	CACHE_DEBUG((DI_INFO, "cacheable snapshot request: 0x%x", st->command));
38227c478bd9Sstevel@tonic-gate 
38237c478bd9Sstevel@tonic-gate 	return (1);
38247c478bd9Sstevel@tonic-gate }
38257c478bd9Sstevel@tonic-gate 
38267c478bd9Sstevel@tonic-gate static int
38277c478bd9Sstevel@tonic-gate di_cache_lookup(struct di_state *st)
38287c478bd9Sstevel@tonic-gate {
38297c478bd9Sstevel@tonic-gate 	size_t	rval;
38307c478bd9Sstevel@tonic-gate 	int	cache_valid;
38317c478bd9Sstevel@tonic-gate 
38327c478bd9Sstevel@tonic-gate 	ASSERT(cache_args_valid(st, &cache_valid));
38337c478bd9Sstevel@tonic-gate 	ASSERT(modrootloaded);
38347c478bd9Sstevel@tonic-gate 
38357c478bd9Sstevel@tonic-gate 	DI_CACHE_LOCK(di_cache);
38367c478bd9Sstevel@tonic-gate 
38377c478bd9Sstevel@tonic-gate 	/*
38387c478bd9Sstevel@tonic-gate 	 * The following assignment determines the validity
38397c478bd9Sstevel@tonic-gate 	 * of the cache as far as this snapshot is concerned.
38407c478bd9Sstevel@tonic-gate 	 */
38417c478bd9Sstevel@tonic-gate 	cache_valid = di_cache.cache_valid;
38427c478bd9Sstevel@tonic-gate 
38437c478bd9Sstevel@tonic-gate 	if (cache_valid && di_cache.cache_data == NULL) {
38447c478bd9Sstevel@tonic-gate 		di_cache_read(&di_cache);
38457c478bd9Sstevel@tonic-gate 		/* check for read or file error */
38467c478bd9Sstevel@tonic-gate 		if (di_cache.cache_data == NULL)
38477c478bd9Sstevel@tonic-gate 			cache_valid = 0;
38487c478bd9Sstevel@tonic-gate 	}
38497c478bd9Sstevel@tonic-gate 
38507c478bd9Sstevel@tonic-gate 	if (cache_valid) {
38517c478bd9Sstevel@tonic-gate 		/*
38527c478bd9Sstevel@tonic-gate 		 * Ok, the cache was valid as of this particular
38537c478bd9Sstevel@tonic-gate 		 * snapshot. Copy the cached snapshot. This is safe
38547c478bd9Sstevel@tonic-gate 		 * to do as the cache cannot be freed (we hold the
38557c478bd9Sstevel@tonic-gate 		 * cache lock). Free the memory allocated in di_state
38567c478bd9Sstevel@tonic-gate 		 * up until this point - we will simply copy everything
38577c478bd9Sstevel@tonic-gate 		 * in the cache.
38587c478bd9Sstevel@tonic-gate 		 */
38597c478bd9Sstevel@tonic-gate 
38607c478bd9Sstevel@tonic-gate 		ASSERT(di_cache.cache_data != NULL);
38617c478bd9Sstevel@tonic-gate 		ASSERT(di_cache.cache_size > 0);
38627c478bd9Sstevel@tonic-gate 
38637c478bd9Sstevel@tonic-gate 		di_freemem(st);
38647c478bd9Sstevel@tonic-gate 
38657c478bd9Sstevel@tonic-gate 		rval = 0;
38667c478bd9Sstevel@tonic-gate 		if (di_cache2mem(&di_cache, st) > 0) {
38677c478bd9Sstevel@tonic-gate 
38687c478bd9Sstevel@tonic-gate 			ASSERT(DI_ALL_PTR(st));
38697c478bd9Sstevel@tonic-gate 
38707c478bd9Sstevel@tonic-gate 			/*
38717c478bd9Sstevel@tonic-gate 			 * map_size is size of valid data in the
38727c478bd9Sstevel@tonic-gate 			 * cached snapshot and may be less than
38737c478bd9Sstevel@tonic-gate 			 * size of the cache.
38747c478bd9Sstevel@tonic-gate 			 */
38757c478bd9Sstevel@tonic-gate 			rval = DI_ALL_PTR(st)->map_size;
38767c478bd9Sstevel@tonic-gate 
38777c478bd9Sstevel@tonic-gate 			ASSERT(rval >= sizeof (struct di_all));
38787c478bd9Sstevel@tonic-gate 			ASSERT(rval <= di_cache.cache_size);
38797c478bd9Sstevel@tonic-gate 		}
38807c478bd9Sstevel@tonic-gate 	} else {
38817c478bd9Sstevel@tonic-gate 		/*
38827c478bd9Sstevel@tonic-gate 		 * The cache isn't valid, we need to take a snapshot.
38837c478bd9Sstevel@tonic-gate 		 * Set the command flags appropriately
38847c478bd9Sstevel@tonic-gate 		 */
38857c478bd9Sstevel@tonic-gate 		ASSERT(st->command == (DINFOCACHE & DIIOC_MASK));
38867c478bd9Sstevel@tonic-gate 		st->command = (DI_CACHE_SNAPSHOT_FLAGS & DIIOC_MASK);
38877c478bd9Sstevel@tonic-gate 		rval = di_cache_update(st);
38887c478bd9Sstevel@tonic-gate 		st->command = (DINFOCACHE & DIIOC_MASK);
38897c478bd9Sstevel@tonic-gate 	}
38907c478bd9Sstevel@tonic-gate 
38917c478bd9Sstevel@tonic-gate 	DI_CACHE_UNLOCK(di_cache);
38927c478bd9Sstevel@tonic-gate 
38937c478bd9Sstevel@tonic-gate 	/*
38947c478bd9Sstevel@tonic-gate 	 * For cached snapshots, the devinfo driver always returns
38957c478bd9Sstevel@tonic-gate 	 * a snapshot rooted at "/".
38967c478bd9Sstevel@tonic-gate 	 */
38977c478bd9Sstevel@tonic-gate 	ASSERT(rval == 0 || strcmp(DI_ALL_PTR(st)->root_path, "/") == 0);
38987c478bd9Sstevel@tonic-gate 
38997c478bd9Sstevel@tonic-gate 	return (rval);
39007c478bd9Sstevel@tonic-gate }
39017c478bd9Sstevel@tonic-gate 
39027c478bd9Sstevel@tonic-gate /*
39037c478bd9Sstevel@tonic-gate  * This is a forced update of the cache  - the previous state of the cache
39047c478bd9Sstevel@tonic-gate  * may be:
39057c478bd9Sstevel@tonic-gate  *	- unpopulated
39067c478bd9Sstevel@tonic-gate  *	- populated and invalid
39077c478bd9Sstevel@tonic-gate  *	- populated and valid
39087c478bd9Sstevel@tonic-gate  */
39097c478bd9Sstevel@tonic-gate static int
39107c478bd9Sstevel@tonic-gate di_cache_update(struct di_state *st)
39117c478bd9Sstevel@tonic-gate {
39127c478bd9Sstevel@tonic-gate 	int rval;
39137c478bd9Sstevel@tonic-gate 	uint32_t crc;
39147c478bd9Sstevel@tonic-gate 	struct di_all *all;
39157c478bd9Sstevel@tonic-gate 
39167c478bd9Sstevel@tonic-gate 	ASSERT(DI_CACHE_LOCKED(di_cache));
39177c478bd9Sstevel@tonic-gate 	ASSERT(snapshot_is_cacheable(st));
39187c478bd9Sstevel@tonic-gate 
39197c478bd9Sstevel@tonic-gate 	/*
39207c478bd9Sstevel@tonic-gate 	 * Free the in-core cache and the on-disk file (if they exist)
39217c478bd9Sstevel@tonic-gate 	 */
39227c478bd9Sstevel@tonic-gate 	i_ddi_di_cache_free(&di_cache);
39237c478bd9Sstevel@tonic-gate 
39247c478bd9Sstevel@tonic-gate 	/*
39257c478bd9Sstevel@tonic-gate 	 * Set valid flag before taking the snapshot,
39267c478bd9Sstevel@tonic-gate 	 * so that any invalidations that arrive
39277c478bd9Sstevel@tonic-gate 	 * during or after the snapshot are not
39287c478bd9Sstevel@tonic-gate 	 * removed by us.
39297c478bd9Sstevel@tonic-gate 	 */
39307c478bd9Sstevel@tonic-gate 	atomic_or_32(&di_cache.cache_valid, 1);
39317c478bd9Sstevel@tonic-gate 
39323c34adc5Sramat 	rval = di_snapshot_and_clean(st);
39337c478bd9Sstevel@tonic-gate 
39347c478bd9Sstevel@tonic-gate 	if (rval == 0) {
39357c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "can't update cache: bad snapshot"));
39367c478bd9Sstevel@tonic-gate 		return (0);
39377c478bd9Sstevel@tonic-gate 	}
39387c478bd9Sstevel@tonic-gate 
39397c478bd9Sstevel@tonic-gate 	DI_ALL_PTR(st)->map_size = rval;
39407c478bd9Sstevel@tonic-gate 
39417c478bd9Sstevel@tonic-gate 	if (di_mem2cache(st, &di_cache) == 0) {
39427c478bd9Sstevel@tonic-gate 		CACHE_DEBUG((DI_ERR, "can't update cache: copy failed"));
39437c478bd9Sstevel@tonic-gate 		return (0);
39447c478bd9Sstevel@tonic-gate 	}
39457c478bd9Sstevel@tonic-gate 
39467c478bd9Sstevel@tonic-gate 	ASSERT(di_cache.cache_data);
39477c478bd9Sstevel@tonic-gate 	ASSERT(di_cache.cache_size > 0);
39487c478bd9Sstevel@tonic-gate 
39497c478bd9Sstevel@tonic-gate 	/*
39507c478bd9Sstevel@tonic-gate 	 * Now that we have cached the snapshot, compute its checksum.
39517c478bd9Sstevel@tonic-gate 	 * The checksum is only computed over the valid data in the
39527c478bd9Sstevel@tonic-gate 	 * cache, not the entire cache.
39537c478bd9Sstevel@tonic-gate 	 * Also, set all the fields (except checksum) before computing
39547c478bd9Sstevel@tonic-gate 	 * checksum.
39557c478bd9Sstevel@tonic-gate 	 */
39567c478bd9Sstevel@tonic-gate 	all = (struct di_all *)di_cache.cache_data;
39577c478bd9Sstevel@tonic-gate 	all->cache_magic = DI_CACHE_MAGIC;
39587c478bd9Sstevel@tonic-gate 	all->map_size = rval;
39597c478bd9Sstevel@tonic-gate 
39607c478bd9Sstevel@tonic-gate 	ASSERT(all->cache_checksum == 0);
39617c478bd9Sstevel@tonic-gate 	CRC32(crc, di_cache.cache_data, all->map_size, -1U, crc32_table);
39627c478bd9Sstevel@tonic-gate 	all->cache_checksum = crc;
39637c478bd9Sstevel@tonic-gate 
39647c478bd9Sstevel@tonic-gate 	di_cache_write(&di_cache);
39657c478bd9Sstevel@tonic-gate 
39667c478bd9Sstevel@tonic-gate 	return (rval);
39677c478bd9Sstevel@tonic-gate }
39687c478bd9Sstevel@tonic-gate 
39697c478bd9Sstevel@tonic-gate static void
39707c478bd9Sstevel@tonic-gate di_cache_print(di_cache_debug_t msglevel, char *fmt, ...)
39717c478bd9Sstevel@tonic-gate {
39727c478bd9Sstevel@tonic-gate 	va_list	ap;
39737c478bd9Sstevel@tonic-gate 
39747c478bd9Sstevel@tonic-gate 	if (di_cache_debug <= DI_QUIET)
39757c478bd9Sstevel@tonic-gate 		return;
39767c478bd9Sstevel@tonic-gate 
39777c478bd9Sstevel@tonic-gate 	if (di_cache_debug < msglevel)
39787c478bd9Sstevel@tonic-gate 		return;
39797c478bd9Sstevel@tonic-gate 
39807c478bd9Sstevel@tonic-gate 	switch (msglevel) {
39817c478bd9Sstevel@tonic-gate 		case DI_ERR:
39827c478bd9Sstevel@tonic-gate 			msglevel = CE_WARN;
39837c478bd9Sstevel@tonic-gate 			break;
39847c478bd9Sstevel@tonic-gate 		case DI_INFO:
39857c478bd9Sstevel@tonic-gate 		case DI_TRACE:
39867c478bd9Sstevel@tonic-gate 		default:
39877c478bd9Sstevel@tonic-gate 			msglevel = CE_NOTE;
39887c478bd9Sstevel@tonic-gate 			break;
39897c478bd9Sstevel@tonic-gate 	}
39907c478bd9Sstevel@tonic-gate 
39917c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
39927c478bd9Sstevel@tonic-gate 	vcmn_err(msglevel, fmt, ap);
39937c478bd9Sstevel@tonic-gate 	va_end(ap);
39947c478bd9Sstevel@tonic-gate }
3995