10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52271Scth * Common Development and Distribution License (the "License").
62271Scth * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
217627SChris.Horne@Sun.COM
220Sstevel@tonic-gate /*
23*12116SVikram.Hegde@Sun.COM * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * driver for accessing kernel devinfo tree.
280Sstevel@tonic-gate */
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/pathname.h>
310Sstevel@tonic-gate #include <sys/debug.h>
320Sstevel@tonic-gate #include <sys/autoconf.h>
337224Scth #include <sys/vmsystm.h>
340Sstevel@tonic-gate #include <sys/conf.h>
350Sstevel@tonic-gate #include <sys/file.h>
360Sstevel@tonic-gate #include <sys/kmem.h>
370Sstevel@tonic-gate #include <sys/modctl.h>
380Sstevel@tonic-gate #include <sys/stat.h>
390Sstevel@tonic-gate #include <sys/ddi.h>
400Sstevel@tonic-gate #include <sys/sunddi.h>
410Sstevel@tonic-gate #include <sys/sunldi_impl.h>
420Sstevel@tonic-gate #include <sys/sunndi.h>
430Sstevel@tonic-gate #include <sys/esunddi.h>
440Sstevel@tonic-gate #include <sys/sunmdi.h>
450Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
460Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
470Sstevel@tonic-gate #include <sys/mdi_impldefs.h>
480Sstevel@tonic-gate #include <sys/devinfo_impl.h>
490Sstevel@tonic-gate #include <sys/thread.h>
500Sstevel@tonic-gate #include <sys/modhash.h>
510Sstevel@tonic-gate #include <sys/bitmap.h>
520Sstevel@tonic-gate #include <util/qsort.h>
530Sstevel@tonic-gate #include <sys/disp.h>
540Sstevel@tonic-gate #include <sys/kobj.h>
550Sstevel@tonic-gate #include <sys/crc32.h>
5610923SEvan.Yan@Sun.COM #include <sys/ddi_hp.h>
5710923SEvan.Yan@Sun.COM #include <sys/ddi_hp_impl.h>
5810923SEvan.Yan@Sun.COM #include <sys/sysmacros.h>
5910923SEvan.Yan@Sun.COM #include <sys/list.h>
600Sstevel@tonic-gate
610Sstevel@tonic-gate
620Sstevel@tonic-gate #ifdef DEBUG
630Sstevel@tonic-gate static int di_debug;
640Sstevel@tonic-gate #define dcmn_err(args) if (di_debug >= 1) cmn_err args
650Sstevel@tonic-gate #define dcmn_err2(args) if (di_debug >= 2) cmn_err args
660Sstevel@tonic-gate #define dcmn_err3(args) if (di_debug >= 3) cmn_err args
670Sstevel@tonic-gate #else
680Sstevel@tonic-gate #define dcmn_err(args) /* nothing */
690Sstevel@tonic-gate #define dcmn_err2(args) /* nothing */
700Sstevel@tonic-gate #define dcmn_err3(args) /* nothing */
710Sstevel@tonic-gate #endif
720Sstevel@tonic-gate
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate * We partition the space of devinfo minor nodes equally between the full and
750Sstevel@tonic-gate * unprivileged versions of the driver. The even-numbered minor nodes are the
760Sstevel@tonic-gate * full version, while the odd-numbered ones are the read-only version.
770Sstevel@tonic-gate */
780Sstevel@tonic-gate static int di_max_opens = 32;
790Sstevel@tonic-gate
807224Scth static int di_prop_dyn = 1; /* enable dynamic property support */
817224Scth
820Sstevel@tonic-gate #define DI_FULL_PARENT 0
830Sstevel@tonic-gate #define DI_READONLY_PARENT 1
840Sstevel@tonic-gate #define DI_NODE_SPECIES 2
850Sstevel@tonic-gate #define DI_UNPRIVILEGED_NODE(x) (((x) % 2) != 0)
860Sstevel@tonic-gate
870Sstevel@tonic-gate #define IOC_IDLE 0 /* snapshot ioctl states */
880Sstevel@tonic-gate #define IOC_SNAP 1 /* snapshot in progress */
890Sstevel@tonic-gate #define IOC_DONE 2 /* snapshot done, but not copied out */
900Sstevel@tonic-gate #define IOC_COPY 3 /* copyout in progress */
910Sstevel@tonic-gate
920Sstevel@tonic-gate /*
937224Scth * Keep max alignment so we can move snapshot to different platforms.
947224Scth *
957224Scth * NOTE: Most callers should rely on the di_checkmem return value
967224Scth * being aligned, and reestablish *off_p with aligned value, instead
977224Scth * of trying to align size of their allocations: this approach will
987224Scth * minimize memory use.
990Sstevel@tonic-gate */
1000Sstevel@tonic-gate #define DI_ALIGN(addr) ((addr + 7l) & ~7l)
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate * To avoid wasting memory, make a linked list of memory chunks.
1040Sstevel@tonic-gate * Size of each chunk is buf_size.
1050Sstevel@tonic-gate */
1060Sstevel@tonic-gate struct di_mem {
1077224Scth struct di_mem *next; /* link to next chunk */
1087224Scth char *buf; /* contiguous kernel memory */
1097224Scth size_t buf_size; /* size of buf in bytes */
1107224Scth devmap_cookie_t cook; /* cookie from ddi_umem_alloc */
1110Sstevel@tonic-gate };
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate /*
1140Sstevel@tonic-gate * This is a stack for walking the tree without using recursion.
1150Sstevel@tonic-gate * When the devinfo tree height is above some small size, one
1160Sstevel@tonic-gate * gets watchdog resets on sun4m.
1170Sstevel@tonic-gate */
1180Sstevel@tonic-gate struct di_stack {
1190Sstevel@tonic-gate void *offset[MAX_TREE_DEPTH];
1200Sstevel@tonic-gate struct dev_info *dip[MAX_TREE_DEPTH];
1210Sstevel@tonic-gate int circ[MAX_TREE_DEPTH];
1220Sstevel@tonic-gate int depth; /* depth of current node to be copied */
1230Sstevel@tonic-gate };
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate #define TOP_OFFSET(stack) \
1260Sstevel@tonic-gate ((di_off_t *)(stack)->offset[(stack)->depth - 1])
1270Sstevel@tonic-gate #define TOP_NODE(stack) \
1280Sstevel@tonic-gate ((stack)->dip[(stack)->depth - 1])
1290Sstevel@tonic-gate #define PARENT_OFFSET(stack) \
1300Sstevel@tonic-gate ((di_off_t *)(stack)->offset[(stack)->depth - 2])
1310Sstevel@tonic-gate #define EMPTY_STACK(stack) ((stack)->depth == 0)
1320Sstevel@tonic-gate #define POP_STACK(stack) { \
1330Sstevel@tonic-gate ndi_devi_exit((dev_info_t *)TOP_NODE(stack), \
1340Sstevel@tonic-gate (stack)->circ[(stack)->depth - 1]); \
1350Sstevel@tonic-gate ((stack)->depth--); \
1360Sstevel@tonic-gate }
1377224Scth #define PUSH_STACK(stack, node, off_p) { \
1380Sstevel@tonic-gate ASSERT(node != NULL); \
1390Sstevel@tonic-gate ndi_devi_enter((dev_info_t *)node, &(stack)->circ[(stack)->depth]); \
1400Sstevel@tonic-gate (stack)->dip[(stack)->depth] = (node); \
1417224Scth (stack)->offset[(stack)->depth] = (void *)(off_p); \
1420Sstevel@tonic-gate ((stack)->depth)++; \
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate
1457224Scth #define DI_ALL_PTR(s) DI_ALL(di_mem_addr((s), 0))
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate /*
1480Sstevel@tonic-gate * With devfs, the device tree has no global locks. The device tree is
1490Sstevel@tonic-gate * dynamic and dips may come and go if they are not locked locally. Under
1500Sstevel@tonic-gate * these conditions, pointers are no longer reliable as unique IDs.
1510Sstevel@tonic-gate * Specifically, these pointers cannot be used as keys for hash tables
1520Sstevel@tonic-gate * as the same devinfo structure may be freed in one part of the tree only
1530Sstevel@tonic-gate * to be allocated as the structure for a different device in another
1540Sstevel@tonic-gate * part of the tree. This can happen if DR and the snapshot are
1550Sstevel@tonic-gate * happening concurrently.
1560Sstevel@tonic-gate * The following data structures act as keys for devinfo nodes and
1570Sstevel@tonic-gate * pathinfo nodes.
1580Sstevel@tonic-gate */
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate enum di_ktype {
1610Sstevel@tonic-gate DI_DKEY = 1,
1620Sstevel@tonic-gate DI_PKEY = 2
1630Sstevel@tonic-gate };
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate struct di_dkey {
1660Sstevel@tonic-gate dev_info_t *dk_dip;
1670Sstevel@tonic-gate major_t dk_major;
1680Sstevel@tonic-gate int dk_inst;
169789Sahrens pnode_t dk_nodeid;
1700Sstevel@tonic-gate };
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate struct di_pkey {
1730Sstevel@tonic-gate mdi_pathinfo_t *pk_pip;
1740Sstevel@tonic-gate char *pk_path_addr;
1750Sstevel@tonic-gate dev_info_t *pk_client;
1760Sstevel@tonic-gate dev_info_t *pk_phci;
1770Sstevel@tonic-gate };
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate struct di_key {
1800Sstevel@tonic-gate enum di_ktype k_type;
1810Sstevel@tonic-gate union {
1820Sstevel@tonic-gate struct di_dkey dkey;
1830Sstevel@tonic-gate struct di_pkey pkey;
1840Sstevel@tonic-gate } k_u;
1850Sstevel@tonic-gate };
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate struct i_lnode;
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate typedef struct i_link {
1910Sstevel@tonic-gate /*
1920Sstevel@tonic-gate * If a di_link struct representing this i_link struct makes it
1930Sstevel@tonic-gate * into the snapshot, then self will point to the offset of
1940Sstevel@tonic-gate * the di_link struct in the snapshot
1950Sstevel@tonic-gate */
1960Sstevel@tonic-gate di_off_t self;
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate int spec_type; /* block or char access type */
1990Sstevel@tonic-gate struct i_lnode *src_lnode; /* src i_lnode */
2000Sstevel@tonic-gate struct i_lnode *tgt_lnode; /* tgt i_lnode */
2010Sstevel@tonic-gate struct i_link *src_link_next; /* next src i_link /w same i_lnode */
2020Sstevel@tonic-gate struct i_link *tgt_link_next; /* next tgt i_link /w same i_lnode */
2030Sstevel@tonic-gate } i_link_t;
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate typedef struct i_lnode {
2060Sstevel@tonic-gate /*
2070Sstevel@tonic-gate * If a di_lnode struct representing this i_lnode struct makes it
2080Sstevel@tonic-gate * into the snapshot, then self will point to the offset of
2090Sstevel@tonic-gate * the di_lnode struct in the snapshot
2100Sstevel@tonic-gate */
2110Sstevel@tonic-gate di_off_t self;
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate /*
2140Sstevel@tonic-gate * used for hashing and comparing i_lnodes
2150Sstevel@tonic-gate */
2160Sstevel@tonic-gate int modid;
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate /*
2190Sstevel@tonic-gate * public information describing a link endpoint
2200Sstevel@tonic-gate */
2210Sstevel@tonic-gate struct di_node *di_node; /* di_node in snapshot */
2220Sstevel@tonic-gate dev_t devt; /* devt */
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate /*
2250Sstevel@tonic-gate * i_link ptr to links coming into this i_lnode node
2260Sstevel@tonic-gate * (this i_lnode is the target of these i_links)
2270Sstevel@tonic-gate */
2280Sstevel@tonic-gate i_link_t *link_in;
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate /*
2310Sstevel@tonic-gate * i_link ptr to links going out of this i_lnode node
2320Sstevel@tonic-gate * (this i_lnode is the source of these i_links)
2330Sstevel@tonic-gate */
2340Sstevel@tonic-gate i_link_t *link_out;
2350Sstevel@tonic-gate } i_lnode_t;
2360Sstevel@tonic-gate
23710923SEvan.Yan@Sun.COM typedef struct i_hp {
23810923SEvan.Yan@Sun.COM di_off_t hp_off; /* Offset of di_hp_t in snapshot */
23910923SEvan.Yan@Sun.COM dev_info_t *hp_child; /* Child devinfo node of the di_hp_t */
24010923SEvan.Yan@Sun.COM list_node_t hp_link; /* List linkage */
24110923SEvan.Yan@Sun.COM } i_hp_t;
24210923SEvan.Yan@Sun.COM
2430Sstevel@tonic-gate /*
2440Sstevel@tonic-gate * Soft state associated with each instance of driver open.
2450Sstevel@tonic-gate */
2460Sstevel@tonic-gate static struct di_state {
2477224Scth di_off_t mem_size; /* total # bytes in memlist */
2487224Scth struct di_mem *memlist; /* head of memlist */
2497224Scth uint_t command; /* command from ioctl */
2507224Scth int di_iocstate; /* snapshot ioctl state */
2517224Scth mod_hash_t *reg_dip_hash;
2527224Scth mod_hash_t *reg_pip_hash;
2537224Scth int lnode_count;
2547224Scth int link_count;
2557224Scth
2567224Scth mod_hash_t *lnode_hash;
2577224Scth mod_hash_t *link_hash;
25810923SEvan.Yan@Sun.COM
25910923SEvan.Yan@Sun.COM list_t hp_list;
2600Sstevel@tonic-gate } **di_states;
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate static kmutex_t di_lock; /* serialize instance assignment */
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate typedef enum {
2650Sstevel@tonic-gate DI_QUIET = 0, /* DI_QUIET must always be 0 */
2660Sstevel@tonic-gate DI_ERR,
2670Sstevel@tonic-gate DI_INFO,
2680Sstevel@tonic-gate DI_TRACE,
2690Sstevel@tonic-gate DI_TRACE1,
2700Sstevel@tonic-gate DI_TRACE2
2710Sstevel@tonic-gate } di_cache_debug_t;
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate static uint_t di_chunk = 32; /* I/O chunk size in pages */
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate #define DI_CACHE_LOCK(c) (mutex_enter(&(c).cache_lock))
2760Sstevel@tonic-gate #define DI_CACHE_UNLOCK(c) (mutex_exit(&(c).cache_lock))
2770Sstevel@tonic-gate #define DI_CACHE_LOCKED(c) (mutex_owned(&(c).cache_lock))
2780Sstevel@tonic-gate
279878Sramat /*
280878Sramat * Check that whole device tree is being configured as a pre-condition for
281878Sramat * cleaning up /etc/devices files.
282878Sramat */
283878Sramat #define DEVICES_FILES_CLEANABLE(st) \
284878Sramat (((st)->command & DINFOSUBTREE) && ((st)->command & DINFOFORCE) && \
285878Sramat strcmp(DI_ALL_PTR(st)->root_path, "/") == 0)
286878Sramat
2870Sstevel@tonic-gate #define CACHE_DEBUG(args) \
2880Sstevel@tonic-gate { if (di_cache_debug != DI_QUIET) di_cache_print args; }
2890Sstevel@tonic-gate
2903133Sjg typedef struct phci_walk_arg {
291893Srs135747 di_off_t off;
292893Srs135747 struct di_state *st;
293893Srs135747 } phci_walk_arg_t;
294893Srs135747
2950Sstevel@tonic-gate static int di_open(dev_t *, int, int, cred_t *);
2960Sstevel@tonic-gate static int di_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
2970Sstevel@tonic-gate static int di_close(dev_t, int, int, cred_t *);
2980Sstevel@tonic-gate static int di_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
2990Sstevel@tonic-gate static int di_attach(dev_info_t *, ddi_attach_cmd_t);
3000Sstevel@tonic-gate static int di_detach(dev_info_t *, ddi_detach_cmd_t);
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate static di_off_t di_copyformat(di_off_t, struct di_state *, intptr_t, int);
303878Sramat static di_off_t di_snapshot_and_clean(struct di_state *);
3040Sstevel@tonic-gate static di_off_t di_copydevnm(di_off_t *, struct di_state *);
3050Sstevel@tonic-gate static di_off_t di_copytree(struct dev_info *, di_off_t *, struct di_state *);
3067224Scth static di_off_t di_copynode(struct dev_info *, struct di_stack *,
3077224Scth struct di_state *);
3080Sstevel@tonic-gate static di_off_t di_getmdata(struct ddi_minor_data *, di_off_t *, di_off_t,
3090Sstevel@tonic-gate struct di_state *);
3100Sstevel@tonic-gate static di_off_t di_getppdata(struct dev_info *, di_off_t *, struct di_state *);
3110Sstevel@tonic-gate static di_off_t di_getdpdata(struct dev_info *, di_off_t *, struct di_state *);
31210923SEvan.Yan@Sun.COM static di_off_t di_gethpdata(ddi_hp_cn_handle_t *, di_off_t *,
31310923SEvan.Yan@Sun.COM struct di_state *);
3147224Scth static di_off_t di_getprop(int, struct ddi_prop **, di_off_t *,
3157224Scth struct di_state *, struct dev_info *);
3160Sstevel@tonic-gate static void di_allocmem(struct di_state *, size_t);
3170Sstevel@tonic-gate static void di_freemem(struct di_state *);
3180Sstevel@tonic-gate static void di_copymem(struct di_state *st, caddr_t buf, size_t bufsiz);
3190Sstevel@tonic-gate static di_off_t di_checkmem(struct di_state *, di_off_t, size_t);
3207224Scth static void *di_mem_addr(struct di_state *, di_off_t);
3210Sstevel@tonic-gate static int di_setstate(struct di_state *, int);
3220Sstevel@tonic-gate static void di_register_dip(struct di_state *, dev_info_t *, di_off_t);
3230Sstevel@tonic-gate static void di_register_pip(struct di_state *, mdi_pathinfo_t *, di_off_t);
3240Sstevel@tonic-gate static di_off_t di_getpath_data(dev_info_t *, di_off_t *, di_off_t,
3250Sstevel@tonic-gate struct di_state *, int);
3260Sstevel@tonic-gate static di_off_t di_getlink_data(di_off_t, struct di_state *);
3270Sstevel@tonic-gate static int di_dip_find(struct di_state *st, dev_info_t *node, di_off_t *off_p);
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate static int cache_args_valid(struct di_state *st, int *error);
3300Sstevel@tonic-gate static int snapshot_is_cacheable(struct di_state *st);
3310Sstevel@tonic-gate static int di_cache_lookup(struct di_state *st);
3320Sstevel@tonic-gate static int di_cache_update(struct di_state *st);
3330Sstevel@tonic-gate static void di_cache_print(di_cache_debug_t msglevel, char *fmt, ...);
3347224Scth static int build_vhci_list(dev_info_t *vh_devinfo, void *arg);
3357224Scth static int build_phci_list(dev_info_t *ph_devinfo, void *arg);
33610923SEvan.Yan@Sun.COM static void di_hotplug_children(struct di_state *st);
3377224Scth
3387224Scth extern int modrootloaded;
3397224Scth extern void mdi_walk_vhcis(int (*)(dev_info_t *, void *), void *);
3407224Scth extern void mdi_vhci_walk_phcis(dev_info_t *,
3417224Scth int (*)(dev_info_t *, void *), void *);
3427224Scth
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate static struct cb_ops di_cb_ops = {
3450Sstevel@tonic-gate di_open, /* open */
3460Sstevel@tonic-gate di_close, /* close */
3470Sstevel@tonic-gate nodev, /* strategy */
3480Sstevel@tonic-gate nodev, /* print */
3490Sstevel@tonic-gate nodev, /* dump */
3500Sstevel@tonic-gate nodev, /* read */
3510Sstevel@tonic-gate nodev, /* write */
3520Sstevel@tonic-gate di_ioctl, /* ioctl */
3530Sstevel@tonic-gate nodev, /* devmap */
3540Sstevel@tonic-gate nodev, /* mmap */
3550Sstevel@tonic-gate nodev, /* segmap */
3560Sstevel@tonic-gate nochpoll, /* poll */
3570Sstevel@tonic-gate ddi_prop_op, /* prop_op */
3580Sstevel@tonic-gate NULL, /* streamtab */
3590Sstevel@tonic-gate D_NEW | D_MP /* Driver compatibility flag */
3600Sstevel@tonic-gate };
3610Sstevel@tonic-gate
3620Sstevel@tonic-gate static struct dev_ops di_ops = {
3630Sstevel@tonic-gate DEVO_REV, /* devo_rev, */
3640Sstevel@tonic-gate 0, /* refcnt */
3650Sstevel@tonic-gate di_info, /* info */
3660Sstevel@tonic-gate nulldev, /* identify */
3670Sstevel@tonic-gate nulldev, /* probe */
3680Sstevel@tonic-gate di_attach, /* attach */
3690Sstevel@tonic-gate di_detach, /* detach */
3700Sstevel@tonic-gate nodev, /* reset */
3710Sstevel@tonic-gate &di_cb_ops, /* driver operations */
3720Sstevel@tonic-gate NULL /* bus operations */
3730Sstevel@tonic-gate };
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate /*
3760Sstevel@tonic-gate * Module linkage information for the kernel.
3770Sstevel@tonic-gate */
3780Sstevel@tonic-gate static struct modldrv modldrv = {
3790Sstevel@tonic-gate &mod_driverops,
3807627SChris.Horne@Sun.COM "DEVINFO Driver",
3810Sstevel@tonic-gate &di_ops
3820Sstevel@tonic-gate };
3830Sstevel@tonic-gate
3840Sstevel@tonic-gate static struct modlinkage modlinkage = {
3850Sstevel@tonic-gate MODREV_1,
3860Sstevel@tonic-gate &modldrv,
3870Sstevel@tonic-gate NULL
3880Sstevel@tonic-gate };
3890Sstevel@tonic-gate
3900Sstevel@tonic-gate int
_init(void)3910Sstevel@tonic-gate _init(void)
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate int error;
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate mutex_init(&di_lock, NULL, MUTEX_DRIVER, NULL);
3960Sstevel@tonic-gate
3970Sstevel@tonic-gate error = mod_install(&modlinkage);
3980Sstevel@tonic-gate if (error != 0) {
3990Sstevel@tonic-gate mutex_destroy(&di_lock);
4000Sstevel@tonic-gate return (error);
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate return (0);
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate int
_info(struct modinfo * modinfop)4070Sstevel@tonic-gate _info(struct modinfo *modinfop)
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate
4120Sstevel@tonic-gate int
_fini(void)4130Sstevel@tonic-gate _fini(void)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate int error;
4160Sstevel@tonic-gate
4170Sstevel@tonic-gate error = mod_remove(&modlinkage);
4180Sstevel@tonic-gate if (error != 0) {
4190Sstevel@tonic-gate return (error);
4200Sstevel@tonic-gate }
4210Sstevel@tonic-gate
4220Sstevel@tonic-gate mutex_destroy(&di_lock);
4230Sstevel@tonic-gate return (0);
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate
4260Sstevel@tonic-gate static dev_info_t *di_dip;
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate /*ARGSUSED*/
4290Sstevel@tonic-gate static int
di_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)4300Sstevel@tonic-gate di_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4310Sstevel@tonic-gate {
4327224Scth int error = DDI_FAILURE;
4330Sstevel@tonic-gate
4340Sstevel@tonic-gate switch (infocmd) {
4350Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
4360Sstevel@tonic-gate *result = (void *)di_dip;
4370Sstevel@tonic-gate error = DDI_SUCCESS;
4380Sstevel@tonic-gate break;
4390Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
4400Sstevel@tonic-gate /*
4410Sstevel@tonic-gate * All dev_t's map to the same, single instance.
4420Sstevel@tonic-gate */
4430Sstevel@tonic-gate *result = (void *)0;
4440Sstevel@tonic-gate error = DDI_SUCCESS;
4450Sstevel@tonic-gate break;
4460Sstevel@tonic-gate default:
4470Sstevel@tonic-gate break;
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate
4500Sstevel@tonic-gate return (error);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate static int
di_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)4540Sstevel@tonic-gate di_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4550Sstevel@tonic-gate {
4567224Scth int error = DDI_FAILURE;
4570Sstevel@tonic-gate
4580Sstevel@tonic-gate switch (cmd) {
4590Sstevel@tonic-gate case DDI_ATTACH:
4600Sstevel@tonic-gate di_states = kmem_zalloc(
4610Sstevel@tonic-gate di_max_opens * sizeof (struct di_state *), KM_SLEEP);
4620Sstevel@tonic-gate
4630Sstevel@tonic-gate if (ddi_create_minor_node(dip, "devinfo", S_IFCHR,
4640Sstevel@tonic-gate DI_FULL_PARENT, DDI_PSEUDO, NULL) == DDI_FAILURE ||
4650Sstevel@tonic-gate ddi_create_minor_node(dip, "devinfo,ro", S_IFCHR,
4660Sstevel@tonic-gate DI_READONLY_PARENT, DDI_PSEUDO, NULL) == DDI_FAILURE) {
4670Sstevel@tonic-gate kmem_free(di_states,
4680Sstevel@tonic-gate di_max_opens * sizeof (struct di_state *));
4690Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
4700Sstevel@tonic-gate error = DDI_FAILURE;
4710Sstevel@tonic-gate } else {
4720Sstevel@tonic-gate di_dip = dip;
4730Sstevel@tonic-gate ddi_report_dev(dip);
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate error = DDI_SUCCESS;
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate break;
4780Sstevel@tonic-gate default:
4790Sstevel@tonic-gate error = DDI_FAILURE;
4800Sstevel@tonic-gate break;
4810Sstevel@tonic-gate }
4820Sstevel@tonic-gate
4830Sstevel@tonic-gate return (error);
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate
4860Sstevel@tonic-gate static int
di_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)4870Sstevel@tonic-gate di_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4880Sstevel@tonic-gate {
4897224Scth int error = DDI_FAILURE;
4900Sstevel@tonic-gate
4910Sstevel@tonic-gate switch (cmd) {
4920Sstevel@tonic-gate case DDI_DETACH:
4930Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
4940Sstevel@tonic-gate di_dip = NULL;
4950Sstevel@tonic-gate kmem_free(di_states, di_max_opens * sizeof (struct di_state *));
4960Sstevel@tonic-gate
4970Sstevel@tonic-gate error = DDI_SUCCESS;
4980Sstevel@tonic-gate break;
4990Sstevel@tonic-gate default:
5000Sstevel@tonic-gate error = DDI_FAILURE;
5010Sstevel@tonic-gate break;
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate
5040Sstevel@tonic-gate return (error);
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate
5070Sstevel@tonic-gate /*
5080Sstevel@tonic-gate * Allow multiple opens by tweaking the dev_t such that it looks like each
5090Sstevel@tonic-gate * open is getting a different minor device. Each minor gets a separate
5100Sstevel@tonic-gate * entry in the di_states[] table. Based on the original minor number, we
5110Sstevel@tonic-gate * discriminate opens of the full and read-only nodes. If all of the instances
5120Sstevel@tonic-gate * of the selected minor node are currently open, we return EAGAIN.
5130Sstevel@tonic-gate */
5140Sstevel@tonic-gate /*ARGSUSED*/
5150Sstevel@tonic-gate static int
di_open(dev_t * devp,int flag,int otyp,cred_t * credp)5160Sstevel@tonic-gate di_open(dev_t *devp, int flag, int otyp, cred_t *credp)
5170Sstevel@tonic-gate {
5187224Scth int m;
5197224Scth minor_t minor_parent = getminor(*devp);
5200Sstevel@tonic-gate
5210Sstevel@tonic-gate if (minor_parent != DI_FULL_PARENT &&
5220Sstevel@tonic-gate minor_parent != DI_READONLY_PARENT)
5230Sstevel@tonic-gate return (ENXIO);
5240Sstevel@tonic-gate
5250Sstevel@tonic-gate mutex_enter(&di_lock);
5260Sstevel@tonic-gate
5270Sstevel@tonic-gate for (m = minor_parent; m < di_max_opens; m += DI_NODE_SPECIES) {
5280Sstevel@tonic-gate if (di_states[m] != NULL)
5290Sstevel@tonic-gate continue;
5300Sstevel@tonic-gate
5310Sstevel@tonic-gate di_states[m] = kmem_zalloc(sizeof (struct di_state), KM_SLEEP);
5320Sstevel@tonic-gate break; /* It's ours. */
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate
5350Sstevel@tonic-gate if (m >= di_max_opens) {
5360Sstevel@tonic-gate /*
5370Sstevel@tonic-gate * maximum open instance for device reached
5380Sstevel@tonic-gate */
5390Sstevel@tonic-gate mutex_exit(&di_lock);
5400Sstevel@tonic-gate dcmn_err((CE_WARN, "devinfo: maximum devinfo open reached"));
5410Sstevel@tonic-gate return (EAGAIN);
5420Sstevel@tonic-gate }
5430Sstevel@tonic-gate mutex_exit(&di_lock);
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate ASSERT(m < di_max_opens);
5460Sstevel@tonic-gate *devp = makedevice(getmajor(*devp), (minor_t)(m + DI_NODE_SPECIES));
5470Sstevel@tonic-gate
5480Sstevel@tonic-gate dcmn_err((CE_CONT, "di_open: thread = %p, assigned minor = %d\n",
5494870Sjg (void *)curthread, m + DI_NODE_SPECIES));
5500Sstevel@tonic-gate
5510Sstevel@tonic-gate return (0);
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate
5540Sstevel@tonic-gate /*ARGSUSED*/
5550Sstevel@tonic-gate static int
di_close(dev_t dev,int flag,int otype,cred_t * cred_p)5560Sstevel@tonic-gate di_close(dev_t dev, int flag, int otype, cred_t *cred_p)
5570Sstevel@tonic-gate {
5587224Scth struct di_state *st;
5597224Scth int m = (int)getminor(dev) - DI_NODE_SPECIES;
5600Sstevel@tonic-gate
5610Sstevel@tonic-gate if (m < 0) {
5620Sstevel@tonic-gate cmn_err(CE_WARN, "closing non-existent devinfo minor %d",
5630Sstevel@tonic-gate m + DI_NODE_SPECIES);
5640Sstevel@tonic-gate return (ENXIO);
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate
5670Sstevel@tonic-gate st = di_states[m];
5680Sstevel@tonic-gate ASSERT(m < di_max_opens && st != NULL);
5690Sstevel@tonic-gate
5700Sstevel@tonic-gate di_freemem(st);
5710Sstevel@tonic-gate kmem_free(st, sizeof (struct di_state));
5720Sstevel@tonic-gate
5730Sstevel@tonic-gate /*
5740Sstevel@tonic-gate * empty slot in state table
5750Sstevel@tonic-gate */
5760Sstevel@tonic-gate mutex_enter(&di_lock);
5770Sstevel@tonic-gate di_states[m] = NULL;
5780Sstevel@tonic-gate dcmn_err((CE_CONT, "di_close: thread = %p, assigned minor = %d\n",
5794870Sjg (void *)curthread, m + DI_NODE_SPECIES));
5800Sstevel@tonic-gate mutex_exit(&di_lock);
5810Sstevel@tonic-gate
5820Sstevel@tonic-gate return (0);
5830Sstevel@tonic-gate }
5840Sstevel@tonic-gate
5850Sstevel@tonic-gate
5860Sstevel@tonic-gate /*ARGSUSED*/
5870Sstevel@tonic-gate static int
di_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)5880Sstevel@tonic-gate di_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
5890Sstevel@tonic-gate {
5907224Scth int rv, error;
5917224Scth di_off_t off;
5927224Scth struct di_all *all;
5937224Scth struct di_state *st;
5947224Scth int m = (int)getminor(dev) - DI_NODE_SPECIES;
5957224Scth major_t i;
5967224Scth char *drv_name;
5977224Scth size_t map_size, size;
5987224Scth struct di_mem *dcp;
5997224Scth int ndi_flags;
6000Sstevel@tonic-gate
6010Sstevel@tonic-gate if (m < 0 || m >= di_max_opens) {
6020Sstevel@tonic-gate return (ENXIO);
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate
6050Sstevel@tonic-gate st = di_states[m];
6060Sstevel@tonic-gate ASSERT(st != NULL);
6070Sstevel@tonic-gate
6080Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_ioctl: mode = %x, cmd = %x\n", mode, cmd));
6090Sstevel@tonic-gate
6100Sstevel@tonic-gate switch (cmd) {
6110Sstevel@tonic-gate case DINFOIDENT:
6120Sstevel@tonic-gate /*
6130Sstevel@tonic-gate * This is called from di_init to verify that the driver
6140Sstevel@tonic-gate * opened is indeed devinfo. The purpose is to guard against
6150Sstevel@tonic-gate * sending ioctl to an unknown driver in case of an
6160Sstevel@tonic-gate * unresolved major number conflict during bfu.
6170Sstevel@tonic-gate */
6180Sstevel@tonic-gate *rvalp = DI_MAGIC;
6190Sstevel@tonic-gate return (0);
6200Sstevel@tonic-gate
6210Sstevel@tonic-gate case DINFOLODRV:
6220Sstevel@tonic-gate /*
6230Sstevel@tonic-gate * Hold an installed driver and return the result
6240Sstevel@tonic-gate */
6250Sstevel@tonic-gate if (DI_UNPRIVILEGED_NODE(m)) {
6260Sstevel@tonic-gate /*
6270Sstevel@tonic-gate * Only the fully enabled instances may issue
6280Sstevel@tonic-gate * DINFOLDDRV.
6290Sstevel@tonic-gate */
6300Sstevel@tonic-gate return (EACCES);
6310Sstevel@tonic-gate }
6320Sstevel@tonic-gate
6330Sstevel@tonic-gate drv_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
6340Sstevel@tonic-gate if (ddi_copyin((void *)arg, drv_name, MAXNAMELEN, mode) != 0) {
6350Sstevel@tonic-gate kmem_free(drv_name, MAXNAMELEN);
6360Sstevel@tonic-gate return (EFAULT);
6370Sstevel@tonic-gate }
6380Sstevel@tonic-gate
6390Sstevel@tonic-gate /*
6400Sstevel@tonic-gate * Some 3rd party driver's _init() walks the device tree,
6410Sstevel@tonic-gate * so we load the driver module before configuring driver.
6420Sstevel@tonic-gate */
6430Sstevel@tonic-gate i = ddi_name_to_major(drv_name);
6440Sstevel@tonic-gate if (ddi_hold_driver(i) == NULL) {
6450Sstevel@tonic-gate kmem_free(drv_name, MAXNAMELEN);
6460Sstevel@tonic-gate return (ENXIO);
6470Sstevel@tonic-gate }
6480Sstevel@tonic-gate
6490Sstevel@tonic-gate ndi_flags = NDI_DEVI_PERSIST | NDI_CONFIG | NDI_NO_EVENT;
6500Sstevel@tonic-gate
6510Sstevel@tonic-gate /*
6520Sstevel@tonic-gate * i_ddi_load_drvconf() below will trigger a reprobe
6530Sstevel@tonic-gate * via reset_nexus_flags(). NDI_DRV_CONF_REPROBE isn't
6540Sstevel@tonic-gate * needed here.
6550Sstevel@tonic-gate */
6560Sstevel@tonic-gate modunload_disable();
6570Sstevel@tonic-gate (void) i_ddi_load_drvconf(i);
6580Sstevel@tonic-gate (void) ndi_devi_config_driver(ddi_root_node(), ndi_flags, i);
6590Sstevel@tonic-gate kmem_free(drv_name, MAXNAMELEN);
6600Sstevel@tonic-gate ddi_rele_driver(i);
6610Sstevel@tonic-gate rv = i_ddi_devs_attached(i);
6620Sstevel@tonic-gate modunload_enable();
6630Sstevel@tonic-gate
66410696SDavid.Hollister@Sun.COM i_ddi_di_cache_invalidate();
6650Sstevel@tonic-gate
6660Sstevel@tonic-gate return ((rv == DDI_SUCCESS)? 0 : ENXIO);
6670Sstevel@tonic-gate
6680Sstevel@tonic-gate case DINFOUSRLD:
6690Sstevel@tonic-gate /*
6700Sstevel@tonic-gate * The case for copying snapshot to userland
6710Sstevel@tonic-gate */
6720Sstevel@tonic-gate if (di_setstate(st, IOC_COPY) == -1)
6730Sstevel@tonic-gate return (EBUSY);
6740Sstevel@tonic-gate
6757224Scth map_size = DI_ALL_PTR(st)->map_size;
6760Sstevel@tonic-gate if (map_size == 0) {
6770Sstevel@tonic-gate (void) di_setstate(st, IOC_DONE);
6780Sstevel@tonic-gate return (EFAULT);
6790Sstevel@tonic-gate }
6800Sstevel@tonic-gate
6810Sstevel@tonic-gate /*
6820Sstevel@tonic-gate * copyout the snapshot
6830Sstevel@tonic-gate */
6840Sstevel@tonic-gate map_size = (map_size + PAGEOFFSET) & PAGEMASK;
6850Sstevel@tonic-gate
6860Sstevel@tonic-gate /*
6870Sstevel@tonic-gate * Return the map size, so caller may do a sanity
6880Sstevel@tonic-gate * check against the return value of snapshot ioctl()
6890Sstevel@tonic-gate */
6900Sstevel@tonic-gate *rvalp = (int)map_size;
6910Sstevel@tonic-gate
6920Sstevel@tonic-gate /*
6930Sstevel@tonic-gate * Copy one chunk at a time
6940Sstevel@tonic-gate */
6950Sstevel@tonic-gate off = 0;
6960Sstevel@tonic-gate dcp = st->memlist;
6970Sstevel@tonic-gate while (map_size) {
6980Sstevel@tonic-gate size = dcp->buf_size;
6990Sstevel@tonic-gate if (map_size <= size) {
7000Sstevel@tonic-gate size = map_size;
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate
7030Sstevel@tonic-gate if (ddi_copyout(di_mem_addr(st, off),
7040Sstevel@tonic-gate (void *)(arg + off), size, mode) != 0) {
7050Sstevel@tonic-gate (void) di_setstate(st, IOC_DONE);
7060Sstevel@tonic-gate return (EFAULT);
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate
7090Sstevel@tonic-gate map_size -= size;
7100Sstevel@tonic-gate off += size;
7110Sstevel@tonic-gate dcp = dcp->next;
7120Sstevel@tonic-gate }
7130Sstevel@tonic-gate
7140Sstevel@tonic-gate di_freemem(st);
7150Sstevel@tonic-gate (void) di_setstate(st, IOC_IDLE);
7160Sstevel@tonic-gate return (0);
7170Sstevel@tonic-gate
7180Sstevel@tonic-gate default:
7190Sstevel@tonic-gate if ((cmd & ~DIIOC_MASK) != DIIOC) {
7200Sstevel@tonic-gate /*
7210Sstevel@tonic-gate * Invalid ioctl command
7220Sstevel@tonic-gate */
7230Sstevel@tonic-gate return (ENOTTY);
7240Sstevel@tonic-gate }
7250Sstevel@tonic-gate /*
7260Sstevel@tonic-gate * take a snapshot
7270Sstevel@tonic-gate */
7280Sstevel@tonic-gate st->command = cmd & DIIOC_MASK;
7290Sstevel@tonic-gate /*FALLTHROUGH*/
7300Sstevel@tonic-gate }
7310Sstevel@tonic-gate
7320Sstevel@tonic-gate /*
7330Sstevel@tonic-gate * Obtain enough memory to hold header + rootpath. We prevent kernel
7340Sstevel@tonic-gate * memory exhaustion by freeing any previously allocated snapshot and
7350Sstevel@tonic-gate * refusing the operation; otherwise we would be allowing ioctl(),
7360Sstevel@tonic-gate * ioctl(), ioctl(), ..., panic.
7370Sstevel@tonic-gate */
7380Sstevel@tonic-gate if (di_setstate(st, IOC_SNAP) == -1)
7390Sstevel@tonic-gate return (EBUSY);
7400Sstevel@tonic-gate
7417224Scth /*
7427224Scth * Initial memlist always holds di_all and the root_path - and
7437224Scth * is at least a page and size.
7447224Scth */
7450Sstevel@tonic-gate size = sizeof (struct di_all) +
7460Sstevel@tonic-gate sizeof (((struct dinfo_io *)(NULL))->root_path);
7470Sstevel@tonic-gate if (size < PAGESIZE)
7480Sstevel@tonic-gate size = PAGESIZE;
7497224Scth off = di_checkmem(st, 0, size);
7507224Scth all = DI_ALL_PTR(st);
7517224Scth off += sizeof (struct di_all); /* real length of di_all */
7527224Scth
7530Sstevel@tonic-gate all->devcnt = devcnt;
7540Sstevel@tonic-gate all->command = st->command;
7550Sstevel@tonic-gate all->version = DI_SNAPSHOT_VERSION;
7567224Scth all->top_vhci_devinfo = 0; /* filled by build_vhci_list. */
7570Sstevel@tonic-gate
7580Sstevel@tonic-gate /*
7590Sstevel@tonic-gate * Note the endianness in case we need to transport snapshot
7600Sstevel@tonic-gate * over the network.
7610Sstevel@tonic-gate */
7620Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
7630Sstevel@tonic-gate all->endianness = DI_LITTLE_ENDIAN;
7640Sstevel@tonic-gate #else
7650Sstevel@tonic-gate all->endianness = DI_BIG_ENDIAN;
7660Sstevel@tonic-gate #endif
7670Sstevel@tonic-gate
7680Sstevel@tonic-gate /* Copyin ioctl args, store in the snapshot. */
769*12116SVikram.Hegde@Sun.COM if (copyinstr((void *)arg, all->req_path,
7700Sstevel@tonic-gate sizeof (((struct dinfo_io *)(NULL))->root_path), &size) != 0) {
7710Sstevel@tonic-gate di_freemem(st);
7720Sstevel@tonic-gate (void) di_setstate(st, IOC_IDLE);
7730Sstevel@tonic-gate return (EFAULT);
7740Sstevel@tonic-gate }
775*12116SVikram.Hegde@Sun.COM (void) strcpy(all->root_path, all->req_path);
7767224Scth off += size; /* real length of root_path */
7770Sstevel@tonic-gate
778878Sramat if ((st->command & DINFOCLEANUP) && !DEVICES_FILES_CLEANABLE(st)) {
779878Sramat di_freemem(st);
780878Sramat (void) di_setstate(st, IOC_IDLE);
781878Sramat return (EINVAL);
782878Sramat }
783878Sramat
7840Sstevel@tonic-gate error = 0;
7850Sstevel@tonic-gate if ((st->command & DINFOCACHE) && !cache_args_valid(st, &error)) {
7860Sstevel@tonic-gate di_freemem(st);
7870Sstevel@tonic-gate (void) di_setstate(st, IOC_IDLE);
7880Sstevel@tonic-gate return (error);
7890Sstevel@tonic-gate }
7900Sstevel@tonic-gate
7910Sstevel@tonic-gate /*
7920Sstevel@tonic-gate * Only the fully enabled version may force load drivers or read
7930Sstevel@tonic-gate * the parent private data from a driver.
7940Sstevel@tonic-gate */
7950Sstevel@tonic-gate if ((st->command & (DINFOPRIVDATA | DINFOFORCE)) != 0 &&
7960Sstevel@tonic-gate DI_UNPRIVILEGED_NODE(m)) {
7970Sstevel@tonic-gate di_freemem(st);
7980Sstevel@tonic-gate (void) di_setstate(st, IOC_IDLE);
7990Sstevel@tonic-gate return (EACCES);
8000Sstevel@tonic-gate }
8010Sstevel@tonic-gate
8020Sstevel@tonic-gate /* Do we need private data? */
8030Sstevel@tonic-gate if (st->command & DINFOPRIVDATA) {
8040Sstevel@tonic-gate arg += sizeof (((struct dinfo_io *)(NULL))->root_path);
8050Sstevel@tonic-gate
8060Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
8070Sstevel@tonic-gate switch (ddi_model_convert_from(mode & FMODELS)) {
8080Sstevel@tonic-gate case DDI_MODEL_ILP32: {
8090Sstevel@tonic-gate /*
8100Sstevel@tonic-gate * Cannot copy private data from 64-bit kernel
8110Sstevel@tonic-gate * to 32-bit app
8120Sstevel@tonic-gate */
8130Sstevel@tonic-gate di_freemem(st);
8140Sstevel@tonic-gate (void) di_setstate(st, IOC_IDLE);
8150Sstevel@tonic-gate return (EINVAL);
8160Sstevel@tonic-gate }
8170Sstevel@tonic-gate case DDI_MODEL_NONE:
8180Sstevel@tonic-gate if ((off = di_copyformat(off, st, arg, mode)) == 0) {
8190Sstevel@tonic-gate di_freemem(st);
8200Sstevel@tonic-gate (void) di_setstate(st, IOC_IDLE);
8210Sstevel@tonic-gate return (EFAULT);
8220Sstevel@tonic-gate }
8230Sstevel@tonic-gate break;
8240Sstevel@tonic-gate }
8250Sstevel@tonic-gate #else /* !_MULTI_DATAMODEL */
8260Sstevel@tonic-gate if ((off = di_copyformat(off, st, arg, mode)) == 0) {
8270Sstevel@tonic-gate di_freemem(st);
8280Sstevel@tonic-gate (void) di_setstate(st, IOC_IDLE);
8290Sstevel@tonic-gate return (EFAULT);
8300Sstevel@tonic-gate }
8310Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
8320Sstevel@tonic-gate }
8330Sstevel@tonic-gate
8340Sstevel@tonic-gate all->top_devinfo = DI_ALIGN(off);
8350Sstevel@tonic-gate
8360Sstevel@tonic-gate /*
8370Sstevel@tonic-gate * For cache lookups we reallocate memory from scratch,
8380Sstevel@tonic-gate * so the value of "all" is no longer valid.
8390Sstevel@tonic-gate */
8400Sstevel@tonic-gate all = NULL;
8410Sstevel@tonic-gate
8420Sstevel@tonic-gate if (st->command & DINFOCACHE) {
8430Sstevel@tonic-gate *rvalp = di_cache_lookup(st);
8440Sstevel@tonic-gate } else if (snapshot_is_cacheable(st)) {
8450Sstevel@tonic-gate DI_CACHE_LOCK(di_cache);
8460Sstevel@tonic-gate *rvalp = di_cache_update(st);
8470Sstevel@tonic-gate DI_CACHE_UNLOCK(di_cache);
848878Sramat } else
849878Sramat *rvalp = di_snapshot_and_clean(st);
8500Sstevel@tonic-gate
8510Sstevel@tonic-gate if (*rvalp) {
8520Sstevel@tonic-gate DI_ALL_PTR(st)->map_size = *rvalp;
8530Sstevel@tonic-gate (void) di_setstate(st, IOC_DONE);
8540Sstevel@tonic-gate } else {
8550Sstevel@tonic-gate di_freemem(st);
8560Sstevel@tonic-gate (void) di_setstate(st, IOC_IDLE);
8570Sstevel@tonic-gate }
8580Sstevel@tonic-gate
8590Sstevel@tonic-gate return (0);
8600Sstevel@tonic-gate }
8610Sstevel@tonic-gate
8620Sstevel@tonic-gate /*
8630Sstevel@tonic-gate * Get a chunk of memory >= size, for the snapshot
8640Sstevel@tonic-gate */
8650Sstevel@tonic-gate static void
di_allocmem(struct di_state * st,size_t size)8660Sstevel@tonic-gate di_allocmem(struct di_state *st, size_t size)
8670Sstevel@tonic-gate {
8687224Scth struct di_mem *mem = kmem_zalloc(sizeof (struct di_mem), KM_SLEEP);
8697224Scth
8700Sstevel@tonic-gate /*
8710Sstevel@tonic-gate * Round up size to nearest power of 2. If it is less
8720Sstevel@tonic-gate * than st->mem_size, set it to st->mem_size (i.e.,
8730Sstevel@tonic-gate * the mem_size is doubled every time) to reduce the
8740Sstevel@tonic-gate * number of memory allocations.
8750Sstevel@tonic-gate */
8760Sstevel@tonic-gate size_t tmp = 1;
8770Sstevel@tonic-gate while (tmp < size) {
8780Sstevel@tonic-gate tmp <<= 1;
8790Sstevel@tonic-gate }
8800Sstevel@tonic-gate size = (tmp > st->mem_size) ? tmp : st->mem_size;
8810Sstevel@tonic-gate
8820Sstevel@tonic-gate mem->buf = ddi_umem_alloc(size, DDI_UMEM_SLEEP, &mem->cook);
8830Sstevel@tonic-gate mem->buf_size = size;
8840Sstevel@tonic-gate
8850Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_allocmem: mem_size=%x\n", st->mem_size));
8860Sstevel@tonic-gate
8870Sstevel@tonic-gate if (st->mem_size == 0) { /* first chunk */
8880Sstevel@tonic-gate st->memlist = mem;
8890Sstevel@tonic-gate } else {
8900Sstevel@tonic-gate /*
8910Sstevel@tonic-gate * locate end of linked list and add a chunk at the end
8920Sstevel@tonic-gate */
8930Sstevel@tonic-gate struct di_mem *dcp = st->memlist;
8940Sstevel@tonic-gate while (dcp->next != NULL) {
8950Sstevel@tonic-gate dcp = dcp->next;
8960Sstevel@tonic-gate }
8970Sstevel@tonic-gate
8980Sstevel@tonic-gate dcp->next = mem;
8990Sstevel@tonic-gate }
9000Sstevel@tonic-gate
9010Sstevel@tonic-gate st->mem_size += size;
9020Sstevel@tonic-gate }
9030Sstevel@tonic-gate
9040Sstevel@tonic-gate /*
9050Sstevel@tonic-gate * Copy upto bufsiz bytes of the memlist to buf
9060Sstevel@tonic-gate */
9070Sstevel@tonic-gate static void
di_copymem(struct di_state * st,caddr_t buf,size_t bufsiz)9080Sstevel@tonic-gate di_copymem(struct di_state *st, caddr_t buf, size_t bufsiz)
9090Sstevel@tonic-gate {
9107224Scth struct di_mem *dcp;
9117224Scth size_t copysz;
9120Sstevel@tonic-gate
9130Sstevel@tonic-gate if (st->mem_size == 0) {
9140Sstevel@tonic-gate ASSERT(st->memlist == NULL);
9150Sstevel@tonic-gate return;
9160Sstevel@tonic-gate }
9170Sstevel@tonic-gate
9180Sstevel@tonic-gate copysz = 0;
9190Sstevel@tonic-gate for (dcp = st->memlist; dcp; dcp = dcp->next) {
9200Sstevel@tonic-gate
9210Sstevel@tonic-gate ASSERT(bufsiz > 0);
9220Sstevel@tonic-gate
9230Sstevel@tonic-gate if (bufsiz <= dcp->buf_size)
9240Sstevel@tonic-gate copysz = bufsiz;
9250Sstevel@tonic-gate else
9260Sstevel@tonic-gate copysz = dcp->buf_size;
9270Sstevel@tonic-gate
9280Sstevel@tonic-gate bcopy(dcp->buf, buf, copysz);
9290Sstevel@tonic-gate
9300Sstevel@tonic-gate buf += copysz;
9310Sstevel@tonic-gate bufsiz -= copysz;
9320Sstevel@tonic-gate
9330Sstevel@tonic-gate if (bufsiz == 0)
9340Sstevel@tonic-gate break;
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate
9380Sstevel@tonic-gate /*
9390Sstevel@tonic-gate * Free all memory for the snapshot
9400Sstevel@tonic-gate */
9410Sstevel@tonic-gate static void
di_freemem(struct di_state * st)9420Sstevel@tonic-gate di_freemem(struct di_state *st)
9430Sstevel@tonic-gate {
9447224Scth struct di_mem *dcp, *tmp;
9450Sstevel@tonic-gate
9460Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_freemem\n"));
9470Sstevel@tonic-gate
9480Sstevel@tonic-gate if (st->mem_size) {
9490Sstevel@tonic-gate dcp = st->memlist;
9500Sstevel@tonic-gate while (dcp) { /* traverse the linked list */
9510Sstevel@tonic-gate tmp = dcp;
9520Sstevel@tonic-gate dcp = dcp->next;
9530Sstevel@tonic-gate ddi_umem_free(tmp->cook);
9540Sstevel@tonic-gate kmem_free(tmp, sizeof (struct di_mem));
9550Sstevel@tonic-gate }
9560Sstevel@tonic-gate st->mem_size = 0;
9570Sstevel@tonic-gate st->memlist = NULL;
9580Sstevel@tonic-gate }
9590Sstevel@tonic-gate
9600Sstevel@tonic-gate ASSERT(st->mem_size == 0);
9610Sstevel@tonic-gate ASSERT(st->memlist == NULL);
9620Sstevel@tonic-gate }
9630Sstevel@tonic-gate
9640Sstevel@tonic-gate /*
9650Sstevel@tonic-gate * Copies cached data to the di_state structure.
9660Sstevel@tonic-gate * Returns:
9670Sstevel@tonic-gate * - size of data copied, on SUCCESS
9680Sstevel@tonic-gate * - 0 on failure
9690Sstevel@tonic-gate */
9700Sstevel@tonic-gate static int
di_cache2mem(struct di_cache * cache,struct di_state * st)9710Sstevel@tonic-gate di_cache2mem(struct di_cache *cache, struct di_state *st)
9720Sstevel@tonic-gate {
9730Sstevel@tonic-gate caddr_t pa;
9740Sstevel@tonic-gate
9750Sstevel@tonic-gate ASSERT(st->mem_size == 0);
9760Sstevel@tonic-gate ASSERT(st->memlist == NULL);
9770Sstevel@tonic-gate ASSERT(!servicing_interrupt());
9780Sstevel@tonic-gate ASSERT(DI_CACHE_LOCKED(*cache));
9790Sstevel@tonic-gate
9800Sstevel@tonic-gate if (cache->cache_size == 0) {
9810Sstevel@tonic-gate ASSERT(cache->cache_data == NULL);
9820Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "Empty cache. Skipping copy"));
9830Sstevel@tonic-gate return (0);
9840Sstevel@tonic-gate }
9850Sstevel@tonic-gate
9860Sstevel@tonic-gate ASSERT(cache->cache_data);
9870Sstevel@tonic-gate
9880Sstevel@tonic-gate di_allocmem(st, cache->cache_size);
9890Sstevel@tonic-gate
9900Sstevel@tonic-gate pa = di_mem_addr(st, 0);
9910Sstevel@tonic-gate
9920Sstevel@tonic-gate ASSERT(pa);
9930Sstevel@tonic-gate
9940Sstevel@tonic-gate /*
9950Sstevel@tonic-gate * Verify that di_allocmem() allocates contiguous memory,
9960Sstevel@tonic-gate * so that it is safe to do straight bcopy()
9970Sstevel@tonic-gate */
9980Sstevel@tonic-gate ASSERT(st->memlist != NULL);
9990Sstevel@tonic-gate ASSERT(st->memlist->next == NULL);
10000Sstevel@tonic-gate bcopy(cache->cache_data, pa, cache->cache_size);
10010Sstevel@tonic-gate
10020Sstevel@tonic-gate return (cache->cache_size);
10030Sstevel@tonic-gate }
10040Sstevel@tonic-gate
10050Sstevel@tonic-gate /*
10060Sstevel@tonic-gate * Copies a snapshot from di_state to the cache
10070Sstevel@tonic-gate * Returns:
10080Sstevel@tonic-gate * - 0 on failure
10090Sstevel@tonic-gate * - size of copied data on success
10100Sstevel@tonic-gate */
10113133Sjg static size_t
di_mem2cache(struct di_state * st,struct di_cache * cache)10120Sstevel@tonic-gate di_mem2cache(struct di_state *st, struct di_cache *cache)
10130Sstevel@tonic-gate {
10147224Scth size_t map_size;
10150Sstevel@tonic-gate
10160Sstevel@tonic-gate ASSERT(cache->cache_size == 0);
10170Sstevel@tonic-gate ASSERT(cache->cache_data == NULL);
10180Sstevel@tonic-gate ASSERT(!servicing_interrupt());
10190Sstevel@tonic-gate ASSERT(DI_CACHE_LOCKED(*cache));
10200Sstevel@tonic-gate
10210Sstevel@tonic-gate if (st->mem_size == 0) {
10220Sstevel@tonic-gate ASSERT(st->memlist == NULL);
10230Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "Empty memlist. Skipping copy"));
10240Sstevel@tonic-gate return (0);
10250Sstevel@tonic-gate }
10260Sstevel@tonic-gate
10270Sstevel@tonic-gate ASSERT(st->memlist);
10280Sstevel@tonic-gate
10290Sstevel@tonic-gate /*
10300Sstevel@tonic-gate * The size of the memory list may be much larger than the
10310Sstevel@tonic-gate * size of valid data (map_size). Cache only the valid data
10320Sstevel@tonic-gate */
10330Sstevel@tonic-gate map_size = DI_ALL_PTR(st)->map_size;
10340Sstevel@tonic-gate if (map_size == 0 || map_size < sizeof (struct di_all) ||
10350Sstevel@tonic-gate map_size > st->mem_size) {
10360Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "cannot cache: bad size: 0x%x", map_size));
10370Sstevel@tonic-gate return (0);
10380Sstevel@tonic-gate }
10390Sstevel@tonic-gate
10400Sstevel@tonic-gate cache->cache_data = kmem_alloc(map_size, KM_SLEEP);
10410Sstevel@tonic-gate cache->cache_size = map_size;
10420Sstevel@tonic-gate di_copymem(st, cache->cache_data, cache->cache_size);
10430Sstevel@tonic-gate
10440Sstevel@tonic-gate return (map_size);
10450Sstevel@tonic-gate }
10460Sstevel@tonic-gate
10470Sstevel@tonic-gate /*
10480Sstevel@tonic-gate * Make sure there is at least "size" bytes memory left before
10490Sstevel@tonic-gate * going on. Otherwise, start on a new chunk.
10500Sstevel@tonic-gate */
10510Sstevel@tonic-gate static di_off_t
di_checkmem(struct di_state * st,di_off_t off,size_t size)10520Sstevel@tonic-gate di_checkmem(struct di_state *st, di_off_t off, size_t size)
10530Sstevel@tonic-gate {
10540Sstevel@tonic-gate dcmn_err3((CE_CONT, "di_checkmem: off=%x size=%x\n",
10554870Sjg off, (int)size));
10560Sstevel@tonic-gate
10570Sstevel@tonic-gate /*
10580Sstevel@tonic-gate * di_checkmem() shouldn't be called with a size of zero.
10590Sstevel@tonic-gate * But in case it is, we want to make sure we return a valid
10600Sstevel@tonic-gate * offset within the memlist and not an offset that points us
10610Sstevel@tonic-gate * at the end of the memlist.
10620Sstevel@tonic-gate */
10630Sstevel@tonic-gate if (size == 0) {
10640Sstevel@tonic-gate dcmn_err((CE_WARN, "di_checkmem: invalid zero size used"));
10650Sstevel@tonic-gate size = 1;
10660Sstevel@tonic-gate }
10670Sstevel@tonic-gate
10680Sstevel@tonic-gate off = DI_ALIGN(off);
10690Sstevel@tonic-gate if ((st->mem_size - off) < size) {
10700Sstevel@tonic-gate off = st->mem_size;
10710Sstevel@tonic-gate di_allocmem(st, size);
10720Sstevel@tonic-gate }
10730Sstevel@tonic-gate
10747224Scth /* verify that return value is aligned */
10757224Scth ASSERT(off == DI_ALIGN(off));
10760Sstevel@tonic-gate return (off);
10770Sstevel@tonic-gate }
10780Sstevel@tonic-gate
10790Sstevel@tonic-gate /*
10800Sstevel@tonic-gate * Copy the private data format from ioctl arg.
10810Sstevel@tonic-gate * On success, the ending offset is returned. On error 0 is returned.
10820Sstevel@tonic-gate */
10830Sstevel@tonic-gate static di_off_t
di_copyformat(di_off_t off,struct di_state * st,intptr_t arg,int mode)10840Sstevel@tonic-gate di_copyformat(di_off_t off, struct di_state *st, intptr_t arg, int mode)
10850Sstevel@tonic-gate {
10867224Scth di_off_t size;
10877224Scth struct di_priv_data *priv;
10887224Scth struct di_all *all = DI_ALL_PTR(st);
10890Sstevel@tonic-gate
10900Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_copyformat: off=%x, arg=%p mode=%x\n",
10914870Sjg off, (void *)arg, mode));
10920Sstevel@tonic-gate
10930Sstevel@tonic-gate /*
10940Sstevel@tonic-gate * Copyin data and check version.
10950Sstevel@tonic-gate * We only handle private data version 0.
10960Sstevel@tonic-gate */
10970Sstevel@tonic-gate priv = kmem_alloc(sizeof (struct di_priv_data), KM_SLEEP);
10980Sstevel@tonic-gate if ((ddi_copyin((void *)arg, priv, sizeof (struct di_priv_data),
10990Sstevel@tonic-gate mode) != 0) || (priv->version != DI_PRIVDATA_VERSION_0)) {
11000Sstevel@tonic-gate kmem_free(priv, sizeof (struct di_priv_data));
11010Sstevel@tonic-gate return (0);
11020Sstevel@tonic-gate }
11030Sstevel@tonic-gate
11040Sstevel@tonic-gate /*
11050Sstevel@tonic-gate * Save di_priv_data copied from userland in snapshot.
11060Sstevel@tonic-gate */
11070Sstevel@tonic-gate all->pd_version = priv->version;
11080Sstevel@tonic-gate all->n_ppdata = priv->n_parent;
11090Sstevel@tonic-gate all->n_dpdata = priv->n_driver;
11100Sstevel@tonic-gate
11110Sstevel@tonic-gate /*
11120Sstevel@tonic-gate * copyin private data format, modify offset accordingly
11130Sstevel@tonic-gate */
11140Sstevel@tonic-gate if (all->n_ppdata) { /* parent private data format */
11150Sstevel@tonic-gate /*
11160Sstevel@tonic-gate * check memory
11170Sstevel@tonic-gate */
11180Sstevel@tonic-gate size = all->n_ppdata * sizeof (struct di_priv_format);
11197224Scth all->ppdata_format = off = di_checkmem(st, off, size);
11200Sstevel@tonic-gate if (ddi_copyin(priv->parent, di_mem_addr(st, off), size,
11210Sstevel@tonic-gate mode) != 0) {
11220Sstevel@tonic-gate kmem_free(priv, sizeof (struct di_priv_data));
11230Sstevel@tonic-gate return (0);
11240Sstevel@tonic-gate }
11250Sstevel@tonic-gate
11260Sstevel@tonic-gate off += size;
11270Sstevel@tonic-gate }
11280Sstevel@tonic-gate
11290Sstevel@tonic-gate if (all->n_dpdata) { /* driver private data format */
11300Sstevel@tonic-gate /*
11310Sstevel@tonic-gate * check memory
11320Sstevel@tonic-gate */
11330Sstevel@tonic-gate size = all->n_dpdata * sizeof (struct di_priv_format);
11347224Scth all->dpdata_format = off = di_checkmem(st, off, size);
11350Sstevel@tonic-gate if (ddi_copyin(priv->driver, di_mem_addr(st, off), size,
11360Sstevel@tonic-gate mode) != 0) {
11370Sstevel@tonic-gate kmem_free(priv, sizeof (struct di_priv_data));
11380Sstevel@tonic-gate return (0);
11390Sstevel@tonic-gate }
11400Sstevel@tonic-gate
11410Sstevel@tonic-gate off += size;
11420Sstevel@tonic-gate }
11430Sstevel@tonic-gate
11440Sstevel@tonic-gate kmem_free(priv, sizeof (struct di_priv_data));
11450Sstevel@tonic-gate return (off);
11460Sstevel@tonic-gate }
11470Sstevel@tonic-gate
11480Sstevel@tonic-gate /*
11490Sstevel@tonic-gate * Return the real address based on the offset (off) within snapshot
11500Sstevel@tonic-gate */
11517224Scth static void *
di_mem_addr(struct di_state * st,di_off_t off)11520Sstevel@tonic-gate di_mem_addr(struct di_state *st, di_off_t off)
11530Sstevel@tonic-gate {
11547224Scth struct di_mem *dcp = st->memlist;
11550Sstevel@tonic-gate
11560Sstevel@tonic-gate dcmn_err3((CE_CONT, "di_mem_addr: dcp=%p off=%x\n",
11574870Sjg (void *)dcp, off));
11580Sstevel@tonic-gate
11590Sstevel@tonic-gate ASSERT(off < st->mem_size);
11600Sstevel@tonic-gate
11610Sstevel@tonic-gate while (off >= dcp->buf_size) {
11620Sstevel@tonic-gate off -= dcp->buf_size;
11630Sstevel@tonic-gate dcp = dcp->next;
11640Sstevel@tonic-gate }
11650Sstevel@tonic-gate
11660Sstevel@tonic-gate dcmn_err3((CE_CONT, "di_mem_addr: new off=%x, return = %p\n",
11674870Sjg off, (void *)(dcp->buf + off)));
11680Sstevel@tonic-gate
11690Sstevel@tonic-gate return (dcp->buf + off);
11700Sstevel@tonic-gate }
11710Sstevel@tonic-gate
11720Sstevel@tonic-gate /*
11730Sstevel@tonic-gate * Ideally we would use the whole key to derive the hash
11740Sstevel@tonic-gate * value. However, the probability that two keys will
11750Sstevel@tonic-gate * have the same dip (or pip) is very low, so
11760Sstevel@tonic-gate * hashing by dip (or pip) pointer should suffice.
11770Sstevel@tonic-gate */
11780Sstevel@tonic-gate static uint_t
di_hash_byptr(void * arg,mod_hash_key_t key)11790Sstevel@tonic-gate di_hash_byptr(void *arg, mod_hash_key_t key)
11800Sstevel@tonic-gate {
11817224Scth struct di_key *dik = key;
11827224Scth size_t rshift;
11837224Scth void *ptr;
11840Sstevel@tonic-gate
11850Sstevel@tonic-gate ASSERT(arg == NULL);
11860Sstevel@tonic-gate
11870Sstevel@tonic-gate switch (dik->k_type) {
11880Sstevel@tonic-gate case DI_DKEY:
11890Sstevel@tonic-gate ptr = dik->k_u.dkey.dk_dip;
11900Sstevel@tonic-gate rshift = highbit(sizeof (struct dev_info));
11910Sstevel@tonic-gate break;
11920Sstevel@tonic-gate case DI_PKEY:
11930Sstevel@tonic-gate ptr = dik->k_u.pkey.pk_pip;
11940Sstevel@tonic-gate rshift = highbit(sizeof (struct mdi_pathinfo));
11950Sstevel@tonic-gate break;
11960Sstevel@tonic-gate default:
11970Sstevel@tonic-gate panic("devinfo: unknown key type");
11980Sstevel@tonic-gate /*NOTREACHED*/
11990Sstevel@tonic-gate }
12000Sstevel@tonic-gate return (mod_hash_byptr((void *)rshift, ptr));
12010Sstevel@tonic-gate }
12020Sstevel@tonic-gate
12030Sstevel@tonic-gate static void
di_key_dtor(mod_hash_key_t key)12040Sstevel@tonic-gate di_key_dtor(mod_hash_key_t key)
12050Sstevel@tonic-gate {
12060Sstevel@tonic-gate char *path_addr;
12070Sstevel@tonic-gate struct di_key *dik = key;
12080Sstevel@tonic-gate
12090Sstevel@tonic-gate switch (dik->k_type) {
12100Sstevel@tonic-gate case DI_DKEY:
12110Sstevel@tonic-gate break;
12120Sstevel@tonic-gate case DI_PKEY:
12130Sstevel@tonic-gate path_addr = dik->k_u.pkey.pk_path_addr;
12140Sstevel@tonic-gate if (path_addr)
12150Sstevel@tonic-gate kmem_free(path_addr, strlen(path_addr) + 1);
12160Sstevel@tonic-gate break;
12170Sstevel@tonic-gate default:
12180Sstevel@tonic-gate panic("devinfo: unknown key type");
12190Sstevel@tonic-gate /*NOTREACHED*/
12200Sstevel@tonic-gate }
12210Sstevel@tonic-gate
12220Sstevel@tonic-gate kmem_free(dik, sizeof (struct di_key));
12230Sstevel@tonic-gate }
12240Sstevel@tonic-gate
12250Sstevel@tonic-gate static int
di_dkey_cmp(struct di_dkey * dk1,struct di_dkey * dk2)12260Sstevel@tonic-gate di_dkey_cmp(struct di_dkey *dk1, struct di_dkey *dk2)
12270Sstevel@tonic-gate {
12280Sstevel@tonic-gate if (dk1->dk_dip != dk2->dk_dip)
12290Sstevel@tonic-gate return (dk1->dk_dip > dk2->dk_dip ? 1 : -1);
12300Sstevel@tonic-gate
12317009Scth if (dk1->dk_major != DDI_MAJOR_T_NONE &&
12327009Scth dk2->dk_major != DDI_MAJOR_T_NONE) {
12330Sstevel@tonic-gate if (dk1->dk_major != dk2->dk_major)
12340Sstevel@tonic-gate return (dk1->dk_major > dk2->dk_major ? 1 : -1);
12350Sstevel@tonic-gate
12360Sstevel@tonic-gate if (dk1->dk_inst != dk2->dk_inst)
12370Sstevel@tonic-gate return (dk1->dk_inst > dk2->dk_inst ? 1 : -1);
12380Sstevel@tonic-gate }
12390Sstevel@tonic-gate
12400Sstevel@tonic-gate if (dk1->dk_nodeid != dk2->dk_nodeid)
12410Sstevel@tonic-gate return (dk1->dk_nodeid > dk2->dk_nodeid ? 1 : -1);
12420Sstevel@tonic-gate
12430Sstevel@tonic-gate return (0);
12440Sstevel@tonic-gate }
12450Sstevel@tonic-gate
12460Sstevel@tonic-gate static int
di_pkey_cmp(struct di_pkey * pk1,struct di_pkey * pk2)12470Sstevel@tonic-gate di_pkey_cmp(struct di_pkey *pk1, struct di_pkey *pk2)
12480Sstevel@tonic-gate {
12497224Scth char *p1, *p2;
12507224Scth int rv;
12510Sstevel@tonic-gate
12520Sstevel@tonic-gate if (pk1->pk_pip != pk2->pk_pip)
12530Sstevel@tonic-gate return (pk1->pk_pip > pk2->pk_pip ? 1 : -1);
12540Sstevel@tonic-gate
12550Sstevel@tonic-gate p1 = pk1->pk_path_addr;
12560Sstevel@tonic-gate p2 = pk2->pk_path_addr;
12570Sstevel@tonic-gate
12580Sstevel@tonic-gate p1 = p1 ? p1 : "";
12590Sstevel@tonic-gate p2 = p2 ? p2 : "";
12600Sstevel@tonic-gate
12610Sstevel@tonic-gate rv = strcmp(p1, p2);
12620Sstevel@tonic-gate if (rv)
12630Sstevel@tonic-gate return (rv > 0 ? 1 : -1);
12640Sstevel@tonic-gate
12650Sstevel@tonic-gate if (pk1->pk_client != pk2->pk_client)
12660Sstevel@tonic-gate return (pk1->pk_client > pk2->pk_client ? 1 : -1);
12670Sstevel@tonic-gate
12680Sstevel@tonic-gate if (pk1->pk_phci != pk2->pk_phci)
12690Sstevel@tonic-gate return (pk1->pk_phci > pk2->pk_phci ? 1 : -1);
12700Sstevel@tonic-gate
12710Sstevel@tonic-gate return (0);
12720Sstevel@tonic-gate }
12730Sstevel@tonic-gate
12740Sstevel@tonic-gate static int
di_key_cmp(mod_hash_key_t key1,mod_hash_key_t key2)12750Sstevel@tonic-gate di_key_cmp(mod_hash_key_t key1, mod_hash_key_t key2)
12760Sstevel@tonic-gate {
12777224Scth struct di_key *dik1, *dik2;
12780Sstevel@tonic-gate
12790Sstevel@tonic-gate dik1 = key1;
12800Sstevel@tonic-gate dik2 = key2;
12810Sstevel@tonic-gate
12820Sstevel@tonic-gate if (dik1->k_type != dik2->k_type) {
12830Sstevel@tonic-gate panic("devinfo: mismatched keys");
12840Sstevel@tonic-gate /*NOTREACHED*/
12850Sstevel@tonic-gate }
12860Sstevel@tonic-gate
12870Sstevel@tonic-gate switch (dik1->k_type) {
12880Sstevel@tonic-gate case DI_DKEY:
12890Sstevel@tonic-gate return (di_dkey_cmp(&(dik1->k_u.dkey), &(dik2->k_u.dkey)));
12900Sstevel@tonic-gate case DI_PKEY:
12910Sstevel@tonic-gate return (di_pkey_cmp(&(dik1->k_u.pkey), &(dik2->k_u.pkey)));
12920Sstevel@tonic-gate default:
12930Sstevel@tonic-gate panic("devinfo: unknown key type");
12940Sstevel@tonic-gate /*NOTREACHED*/
12950Sstevel@tonic-gate }
12960Sstevel@tonic-gate }
12970Sstevel@tonic-gate
1298*12116SVikram.Hegde@Sun.COM static void
di_copy_aliases(struct di_state * st,alias_pair_t * apair,di_off_t * offp)1299*12116SVikram.Hegde@Sun.COM di_copy_aliases(struct di_state *st, alias_pair_t *apair, di_off_t *offp)
1300*12116SVikram.Hegde@Sun.COM {
1301*12116SVikram.Hegde@Sun.COM di_off_t off;
1302*12116SVikram.Hegde@Sun.COM struct di_all *all = DI_ALL_PTR(st);
1303*12116SVikram.Hegde@Sun.COM struct di_alias *di_alias;
1304*12116SVikram.Hegde@Sun.COM di_off_t curroff;
1305*12116SVikram.Hegde@Sun.COM dev_info_t *currdip;
1306*12116SVikram.Hegde@Sun.COM size_t size;
1307*12116SVikram.Hegde@Sun.COM
1308*12116SVikram.Hegde@Sun.COM currdip = NULL;
1309*12116SVikram.Hegde@Sun.COM if (resolve_pathname(apair->pair_alias, &currdip, NULL, NULL) != 0) {
1310*12116SVikram.Hegde@Sun.COM return;
1311*12116SVikram.Hegde@Sun.COM }
1312*12116SVikram.Hegde@Sun.COM
1313*12116SVikram.Hegde@Sun.COM if (di_dip_find(st, currdip, &curroff) != 0) {
1314*12116SVikram.Hegde@Sun.COM ndi_rele_devi(currdip);
1315*12116SVikram.Hegde@Sun.COM return;
1316*12116SVikram.Hegde@Sun.COM }
1317*12116SVikram.Hegde@Sun.COM ndi_rele_devi(currdip);
1318*12116SVikram.Hegde@Sun.COM
1319*12116SVikram.Hegde@Sun.COM off = *offp;
1320*12116SVikram.Hegde@Sun.COM size = sizeof (struct di_alias);
1321*12116SVikram.Hegde@Sun.COM size += strlen(apair->pair_alias) + 1;
1322*12116SVikram.Hegde@Sun.COM off = di_checkmem(st, off, size);
1323*12116SVikram.Hegde@Sun.COM di_alias = DI_ALIAS(di_mem_addr(st, off));
1324*12116SVikram.Hegde@Sun.COM
1325*12116SVikram.Hegde@Sun.COM di_alias->self = off;
1326*12116SVikram.Hegde@Sun.COM di_alias->next = all->aliases;
1327*12116SVikram.Hegde@Sun.COM all->aliases = off;
1328*12116SVikram.Hegde@Sun.COM (void) strcpy(di_alias->alias, apair->pair_alias);
1329*12116SVikram.Hegde@Sun.COM di_alias->curroff = curroff;
1330*12116SVikram.Hegde@Sun.COM
1331*12116SVikram.Hegde@Sun.COM off += size;
1332*12116SVikram.Hegde@Sun.COM
1333*12116SVikram.Hegde@Sun.COM *offp = off;
1334*12116SVikram.Hegde@Sun.COM }
1335*12116SVikram.Hegde@Sun.COM
13360Sstevel@tonic-gate /*
13370Sstevel@tonic-gate * This is the main function that takes a snapshot
13380Sstevel@tonic-gate */
13390Sstevel@tonic-gate static di_off_t
di_snapshot(struct di_state * st)13400Sstevel@tonic-gate di_snapshot(struct di_state *st)
13410Sstevel@tonic-gate {
13427224Scth di_off_t off;
13437224Scth struct di_all *all;
13447224Scth dev_info_t *rootnode;
13457224Scth char buf[80];
13467224Scth int plen;
13477224Scth char *path;
13487224Scth vnode_t *vp;
1349*12116SVikram.Hegde@Sun.COM int i;
13507224Scth
13517224Scth all = DI_ALL_PTR(st);
13520Sstevel@tonic-gate dcmn_err((CE_CONT, "Taking a snapshot of devinfo tree...\n"));
13530Sstevel@tonic-gate
13540Sstevel@tonic-gate /*
1355*12116SVikram.Hegde@Sun.COM * Translate requested root path if an alias and snap-root != "/"
1356*12116SVikram.Hegde@Sun.COM */
1357*12116SVikram.Hegde@Sun.COM if (ddi_aliases_present == B_TRUE && strcmp(all->root_path, "/") != 0) {
1358*12116SVikram.Hegde@Sun.COM /* If there is no redirected alias, use root_path as is */
1359*12116SVikram.Hegde@Sun.COM rootnode = ddi_alias_redirect(all->root_path);
1360*12116SVikram.Hegde@Sun.COM if (rootnode) {
1361*12116SVikram.Hegde@Sun.COM (void) ddi_pathname(rootnode, all->root_path);
1362*12116SVikram.Hegde@Sun.COM goto got_root;
1363*12116SVikram.Hegde@Sun.COM }
1364*12116SVikram.Hegde@Sun.COM }
1365*12116SVikram.Hegde@Sun.COM
1366*12116SVikram.Hegde@Sun.COM /*
1367643Scth * Verify path before entrusting it to e_ddi_hold_devi_by_path because
1368643Scth * some platforms have OBP bugs where executing the NDI_PROMNAME code
1369643Scth * path against an invalid path results in panic. The lookupnameat
1370643Scth * is done relative to rootdir without a leading '/' on "devices/"
1371643Scth * to force the lookup to occur in the global zone.
1372643Scth */
1373643Scth plen = strlen("devices/") + strlen(all->root_path) + 1;
1374643Scth path = kmem_alloc(plen, KM_SLEEP);
1375643Scth (void) snprintf(path, plen, "devices/%s", all->root_path);
1376643Scth if (lookupnameat(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp, rootdir)) {
1377643Scth dcmn_err((CE_CONT, "Devinfo node %s not found\n",
1378643Scth all->root_path));
1379643Scth kmem_free(path, plen);
1380643Scth return (0);
1381643Scth }
1382643Scth kmem_free(path, plen);
1383643Scth VN_RELE(vp);
1384643Scth
1385643Scth /*
13860Sstevel@tonic-gate * Hold the devinfo node referred by the path.
13870Sstevel@tonic-gate */
13880Sstevel@tonic-gate rootnode = e_ddi_hold_devi_by_path(all->root_path, 0);
13890Sstevel@tonic-gate if (rootnode == NULL) {
13900Sstevel@tonic-gate dcmn_err((CE_CONT, "Devinfo node %s not found\n",
13910Sstevel@tonic-gate all->root_path));
13920Sstevel@tonic-gate return (0);
13930Sstevel@tonic-gate }
13940Sstevel@tonic-gate
1395*12116SVikram.Hegde@Sun.COM got_root:
13960Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf),
13970Sstevel@tonic-gate "devinfo registered dips (statep=%p)", (void *)st);
13980Sstevel@tonic-gate
13990Sstevel@tonic-gate st->reg_dip_hash = mod_hash_create_extended(buf, 64,
14000Sstevel@tonic-gate di_key_dtor, mod_hash_null_valdtor, di_hash_byptr,
14010Sstevel@tonic-gate NULL, di_key_cmp, KM_SLEEP);
14020Sstevel@tonic-gate
14030Sstevel@tonic-gate
14040Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf),
14050Sstevel@tonic-gate "devinfo registered pips (statep=%p)", (void *)st);
14060Sstevel@tonic-gate
14070Sstevel@tonic-gate st->reg_pip_hash = mod_hash_create_extended(buf, 64,
14080Sstevel@tonic-gate di_key_dtor, mod_hash_null_valdtor, di_hash_byptr,
14090Sstevel@tonic-gate NULL, di_key_cmp, KM_SLEEP);
14100Sstevel@tonic-gate
141110923SEvan.Yan@Sun.COM if (DINFOHP & st->command) {
141210923SEvan.Yan@Sun.COM list_create(&st->hp_list, sizeof (i_hp_t),
141310923SEvan.Yan@Sun.COM offsetof(i_hp_t, hp_link));
141410923SEvan.Yan@Sun.COM }
141510923SEvan.Yan@Sun.COM
14160Sstevel@tonic-gate /*
14170Sstevel@tonic-gate * copy the device tree
14180Sstevel@tonic-gate */
14190Sstevel@tonic-gate off = di_copytree(DEVI(rootnode), &all->top_devinfo, st);
14200Sstevel@tonic-gate
1421893Srs135747 if (DINFOPATH & st->command) {
1422893Srs135747 mdi_walk_vhcis(build_vhci_list, st);
1423893Srs135747 }
1424893Srs135747
142510923SEvan.Yan@Sun.COM if (DINFOHP & st->command) {
142610923SEvan.Yan@Sun.COM di_hotplug_children(st);
142710923SEvan.Yan@Sun.COM }
142810923SEvan.Yan@Sun.COM
14290Sstevel@tonic-gate ddi_release_devi(rootnode);
14300Sstevel@tonic-gate
14310Sstevel@tonic-gate /*
14320Sstevel@tonic-gate * copy the devnames array
14330Sstevel@tonic-gate */
14340Sstevel@tonic-gate all->devnames = off;
14350Sstevel@tonic-gate off = di_copydevnm(&all->devnames, st);
14360Sstevel@tonic-gate
14370Sstevel@tonic-gate
14380Sstevel@tonic-gate /* initialize the hash tables */
14390Sstevel@tonic-gate st->lnode_count = 0;
14400Sstevel@tonic-gate st->link_count = 0;
14410Sstevel@tonic-gate
14420Sstevel@tonic-gate if (DINFOLYR & st->command) {
14430Sstevel@tonic-gate off = di_getlink_data(off, st);
14440Sstevel@tonic-gate }
14450Sstevel@tonic-gate
1446*12116SVikram.Hegde@Sun.COM all->aliases = 0;
1447*12116SVikram.Hegde@Sun.COM if (ddi_aliases_present == B_FALSE)
1448*12116SVikram.Hegde@Sun.COM goto done;
1449*12116SVikram.Hegde@Sun.COM
1450*12116SVikram.Hegde@Sun.COM for (i = 0; i < ddi_aliases.dali_num_pairs; i++) {
1451*12116SVikram.Hegde@Sun.COM di_copy_aliases(st, &(ddi_aliases.dali_alias_pairs[i]), &off);
1452*12116SVikram.Hegde@Sun.COM }
1453*12116SVikram.Hegde@Sun.COM
1454*12116SVikram.Hegde@Sun.COM done:
14550Sstevel@tonic-gate /*
14560Sstevel@tonic-gate * Free up hash tables
14570Sstevel@tonic-gate */
14580Sstevel@tonic-gate mod_hash_destroy_hash(st->reg_dip_hash);
14590Sstevel@tonic-gate mod_hash_destroy_hash(st->reg_pip_hash);
14600Sstevel@tonic-gate
14610Sstevel@tonic-gate /*
14620Sstevel@tonic-gate * Record the timestamp now that we are done with snapshot.
14630Sstevel@tonic-gate *
14640Sstevel@tonic-gate * We compute the checksum later and then only if we cache
14650Sstevel@tonic-gate * the snapshot, since checksumming adds some overhead.
14660Sstevel@tonic-gate * The checksum is checked later if we read the cache file.
14670Sstevel@tonic-gate * from disk.
14680Sstevel@tonic-gate *
14690Sstevel@tonic-gate * Set checksum field to 0 as CRC is calculated with that
14700Sstevel@tonic-gate * field set to 0.
14710Sstevel@tonic-gate */
14720Sstevel@tonic-gate all->snapshot_time = ddi_get_time();
14730Sstevel@tonic-gate all->cache_checksum = 0;
14740Sstevel@tonic-gate
14753133Sjg ASSERT(all->snapshot_time != 0);
14763133Sjg
14770Sstevel@tonic-gate return (off);
14780Sstevel@tonic-gate }
14790Sstevel@tonic-gate
14800Sstevel@tonic-gate /*
1481878Sramat * Take a snapshot and clean /etc/devices files if DINFOCLEANUP is set
1482878Sramat */
1483878Sramat static di_off_t
di_snapshot_and_clean(struct di_state * st)1484878Sramat di_snapshot_and_clean(struct di_state *st)
1485878Sramat {
1486893Srs135747 di_off_t off;
1487878Sramat
1488878Sramat modunload_disable();
1489878Sramat off = di_snapshot(st);
1490878Sramat if (off != 0 && (st->command & DINFOCLEANUP)) {
1491878Sramat ASSERT(DEVICES_FILES_CLEANABLE(st));
1492878Sramat /*
1493878Sramat * Cleanup /etc/devices files:
1494878Sramat * In order to accurately account for the system configuration
1495878Sramat * in /etc/devices files, the appropriate drivers must be
1496878Sramat * fully configured before the cleanup starts.
1497878Sramat * So enable modunload only after the cleanup.
1498878Sramat */
1499878Sramat i_ddi_clean_devices_files();
15004870Sjg /*
15014870Sjg * Remove backing store nodes for unused devices,
15024870Sjg * which retain past permissions customizations
15034870Sjg * and may be undesired for newly configured devices.
15044870Sjg */
15054870Sjg dev_devices_cleanup();
1506878Sramat }
1507878Sramat modunload_enable();
1508878Sramat
1509878Sramat return (off);
1510878Sramat }
1511878Sramat
1512878Sramat /*
1513893Srs135747 * construct vhci linkage in the snapshot.
1514893Srs135747 */
15157224Scth static int
build_vhci_list(dev_info_t * vh_devinfo,void * arg)1516893Srs135747 build_vhci_list(dev_info_t *vh_devinfo, void *arg)
1517893Srs135747 {
15187224Scth struct di_all *all;
15197224Scth struct di_node *me;
15207224Scth struct di_state *st;
15217224Scth di_off_t off;
15227224Scth phci_walk_arg_t pwa;
1523893Srs135747
1524893Srs135747 dcmn_err3((CE_CONT, "build_vhci list\n"));
1525893Srs135747
15267224Scth dcmn_err3((CE_CONT, "vhci node %s%d\n",
15277224Scth ddi_driver_name(vh_devinfo), ddi_get_instance(vh_devinfo)));
1528893Srs135747
1529893Srs135747 st = (struct di_state *)arg;
1530893Srs135747 if (di_dip_find(st, vh_devinfo, &off) != 0) {
1531893Srs135747 dcmn_err((CE_WARN, "di_dip_find error for the given node\n"));
1532893Srs135747 return (DDI_WALK_TERMINATE);
1533893Srs135747 }
1534893Srs135747
1535893Srs135747 dcmn_err3((CE_CONT, "st->mem_size: %d vh_devinfo off: 0x%x\n",
15364870Sjg st->mem_size, off));
1537893Srs135747
15387224Scth all = DI_ALL_PTR(st);
1539893Srs135747 if (all->top_vhci_devinfo == 0) {
1540893Srs135747 all->top_vhci_devinfo = off;
1541893Srs135747 } else {
15427224Scth me = DI_NODE(di_mem_addr(st, all->top_vhci_devinfo));
1543893Srs135747
1544893Srs135747 while (me->next_vhci != 0) {
15457224Scth me = DI_NODE(di_mem_addr(st, me->next_vhci));
1546893Srs135747 }
1547893Srs135747
1548893Srs135747 me->next_vhci = off;
1549893Srs135747 }
1550893Srs135747
1551893Srs135747 pwa.off = off;
1552893Srs135747 pwa.st = st;
1553893Srs135747 mdi_vhci_walk_phcis(vh_devinfo, build_phci_list, &pwa);
1554893Srs135747
1555893Srs135747 return (DDI_WALK_CONTINUE);
1556893Srs135747 }
1557893Srs135747
1558893Srs135747 /*
1559893Srs135747 * construct phci linkage for the given vhci in the snapshot.
1560893Srs135747 */
15617224Scth static int
build_phci_list(dev_info_t * ph_devinfo,void * arg)1562893Srs135747 build_phci_list(dev_info_t *ph_devinfo, void *arg)
1563893Srs135747 {
15647224Scth struct di_node *vh_di_node;
15657224Scth struct di_node *me;
15667224Scth phci_walk_arg_t *pwa;
15677224Scth di_off_t off;
1568893Srs135747
15693133Sjg pwa = (phci_walk_arg_t *)arg;
1570893Srs135747
1571893Srs135747 dcmn_err3((CE_CONT, "build_phci list for vhci at offset: 0x%x\n",
15724870Sjg pwa->off));
1573893Srs135747
15747224Scth vh_di_node = DI_NODE(di_mem_addr(pwa->st, pwa->off));
1575893Srs135747 if (di_dip_find(pwa->st, ph_devinfo, &off) != 0) {
1576893Srs135747 dcmn_err((CE_WARN, "di_dip_find error for the given node\n"));
1577893Srs135747 return (DDI_WALK_TERMINATE);
1578893Srs135747 }
1579893Srs135747
15807224Scth dcmn_err3((CE_CONT, "phci node %s%d, at offset 0x%x\n",
15817224Scth ddi_driver_name(ph_devinfo), ddi_get_instance(ph_devinfo), off));
1582893Srs135747
1583893Srs135747 if (vh_di_node->top_phci == 0) {
1584893Srs135747 vh_di_node->top_phci = off;
1585893Srs135747 return (DDI_WALK_CONTINUE);
1586893Srs135747 }
1587893Srs135747
15887224Scth me = DI_NODE(di_mem_addr(pwa->st, vh_di_node->top_phci));
1589893Srs135747
1590893Srs135747 while (me->next_phci != 0) {
15917224Scth me = DI_NODE(di_mem_addr(pwa->st, me->next_phci));
1592893Srs135747 }
1593893Srs135747 me->next_phci = off;
1594893Srs135747
1595893Srs135747 return (DDI_WALK_CONTINUE);
1596893Srs135747 }
1597893Srs135747
1598893Srs135747 /*
15990Sstevel@tonic-gate * Assumes all devinfo nodes in device tree have been snapshotted
16000Sstevel@tonic-gate */
16010Sstevel@tonic-gate static void
snap_driver_list(struct di_state * st,struct devnames * dnp,di_off_t * off_p)16027224Scth snap_driver_list(struct di_state *st, struct devnames *dnp, di_off_t *off_p)
16030Sstevel@tonic-gate {
16047224Scth struct dev_info *node;
16057224Scth struct di_node *me;
16067224Scth di_off_t off;
16070Sstevel@tonic-gate
16080Sstevel@tonic-gate ASSERT(mutex_owned(&dnp->dn_lock));
16090Sstevel@tonic-gate
16100Sstevel@tonic-gate node = DEVI(dnp->dn_head);
16110Sstevel@tonic-gate for (; node; node = node->devi_next) {
16120Sstevel@tonic-gate if (di_dip_find(st, (dev_info_t *)node, &off) != 0)
16130Sstevel@tonic-gate continue;
16140Sstevel@tonic-gate
16150Sstevel@tonic-gate ASSERT(off > 0);
16167224Scth me = DI_NODE(di_mem_addr(st, off));
16170Sstevel@tonic-gate ASSERT(me->next == 0 || me->next == -1);
16180Sstevel@tonic-gate /*
16190Sstevel@tonic-gate * Only nodes which were BOUND when they were
16200Sstevel@tonic-gate * snapshotted will be added to per-driver list.
16210Sstevel@tonic-gate */
16220Sstevel@tonic-gate if (me->next != -1)
16230Sstevel@tonic-gate continue;
16240Sstevel@tonic-gate
16257224Scth *off_p = off;
16267224Scth off_p = &me->next;
16270Sstevel@tonic-gate }
16280Sstevel@tonic-gate
16297224Scth *off_p = 0;
16300Sstevel@tonic-gate }
16310Sstevel@tonic-gate
16320Sstevel@tonic-gate /*
16330Sstevel@tonic-gate * Copy the devnames array, so we have a list of drivers in the snapshot.
16340Sstevel@tonic-gate * Also makes it possible to locate the per-driver devinfo nodes.
16350Sstevel@tonic-gate */
16360Sstevel@tonic-gate static di_off_t
di_copydevnm(di_off_t * off_p,struct di_state * st)16370Sstevel@tonic-gate di_copydevnm(di_off_t *off_p, struct di_state *st)
16380Sstevel@tonic-gate {
16397224Scth int i;
16407224Scth di_off_t off;
16417224Scth size_t size;
16427224Scth struct di_devnm *dnp;
16430Sstevel@tonic-gate
16440Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_copydevnm: *off_p = %p\n", (void *)off_p));
16450Sstevel@tonic-gate
16460Sstevel@tonic-gate /*
16470Sstevel@tonic-gate * make sure there is some allocated memory
16480Sstevel@tonic-gate */
16490Sstevel@tonic-gate size = devcnt * sizeof (struct di_devnm);
16507224Scth *off_p = off = di_checkmem(st, *off_p, size);
16517224Scth dnp = DI_DEVNM(di_mem_addr(st, off));
16527224Scth off += size;
16530Sstevel@tonic-gate
16540Sstevel@tonic-gate dcmn_err((CE_CONT, "Start copying devnamesp[%d] at offset 0x%x\n",
16554870Sjg devcnt, off));
16560Sstevel@tonic-gate
16570Sstevel@tonic-gate for (i = 0; i < devcnt; i++) {
16580Sstevel@tonic-gate if (devnamesp[i].dn_name == NULL) {
16590Sstevel@tonic-gate continue;
16600Sstevel@tonic-gate }
16610Sstevel@tonic-gate
16620Sstevel@tonic-gate /*
16630Sstevel@tonic-gate * dn_name is not freed during driver unload or removal.
16640Sstevel@tonic-gate *
16650Sstevel@tonic-gate * There is a race condition when make_devname() changes
16660Sstevel@tonic-gate * dn_name during our strcpy. This should be rare since
16670Sstevel@tonic-gate * only add_drv does this. At any rate, we never had a
16680Sstevel@tonic-gate * problem with ddi_name_to_major(), which should have
16690Sstevel@tonic-gate * the same problem.
16700Sstevel@tonic-gate */
16710Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_copydevnm: %s%d, off=%x\n",
16727224Scth devnamesp[i].dn_name, devnamesp[i].dn_instance, off));
16737224Scth
16747224Scth size = strlen(devnamesp[i].dn_name) + 1;
16757224Scth dnp[i].name = off = di_checkmem(st, off, size);
16760Sstevel@tonic-gate (void) strcpy((char *)di_mem_addr(st, off),
16774870Sjg devnamesp[i].dn_name);
16787224Scth off += size;
16790Sstevel@tonic-gate
16800Sstevel@tonic-gate mutex_enter(&devnamesp[i].dn_lock);
16810Sstevel@tonic-gate
16820Sstevel@tonic-gate /*
16830Sstevel@tonic-gate * Snapshot per-driver node list
16840Sstevel@tonic-gate */
16850Sstevel@tonic-gate snap_driver_list(st, &devnamesp[i], &dnp[i].head);
16860Sstevel@tonic-gate
16870Sstevel@tonic-gate /*
16880Sstevel@tonic-gate * This is not used by libdevinfo, leave it for now
16890Sstevel@tonic-gate */
16900Sstevel@tonic-gate dnp[i].flags = devnamesp[i].dn_flags;
16910Sstevel@tonic-gate dnp[i].instance = devnamesp[i].dn_instance;
16920Sstevel@tonic-gate
16930Sstevel@tonic-gate /*
16940Sstevel@tonic-gate * get global properties
16950Sstevel@tonic-gate */
16960Sstevel@tonic-gate if ((DINFOPROP & st->command) &&
16970Sstevel@tonic-gate devnamesp[i].dn_global_prop_ptr) {
16980Sstevel@tonic-gate dnp[i].global_prop = off;
16997224Scth off = di_getprop(DI_PROP_GLB_LIST,
17007224Scth &devnamesp[i].dn_global_prop_ptr->prop_list,
17017224Scth &dnp[i].global_prop, st, NULL);
17020Sstevel@tonic-gate }
17030Sstevel@tonic-gate
17040Sstevel@tonic-gate /*
17050Sstevel@tonic-gate * Bit encode driver ops: & bus_ops, cb_ops, & cb_ops->cb_str
17060Sstevel@tonic-gate */
17070Sstevel@tonic-gate if (CB_DRV_INSTALLED(devopsp[i])) {
17080Sstevel@tonic-gate if (devopsp[i]->devo_cb_ops) {
17090Sstevel@tonic-gate dnp[i].ops |= DI_CB_OPS;
17100Sstevel@tonic-gate if (devopsp[i]->devo_cb_ops->cb_str)
17110Sstevel@tonic-gate dnp[i].ops |= DI_STREAM_OPS;
17120Sstevel@tonic-gate }
17130Sstevel@tonic-gate if (NEXUS_DRV(devopsp[i])) {
17140Sstevel@tonic-gate dnp[i].ops |= DI_BUS_OPS;
17150Sstevel@tonic-gate }
17160Sstevel@tonic-gate }
17170Sstevel@tonic-gate
17180Sstevel@tonic-gate mutex_exit(&devnamesp[i].dn_lock);
17190Sstevel@tonic-gate }
17200Sstevel@tonic-gate
17210Sstevel@tonic-gate dcmn_err((CE_CONT, "End copying devnamesp at offset 0x%x\n", off));
17220Sstevel@tonic-gate
17230Sstevel@tonic-gate return (off);
17240Sstevel@tonic-gate }
17250Sstevel@tonic-gate
17260Sstevel@tonic-gate /*
17270Sstevel@tonic-gate * Copy the kernel devinfo tree. The tree and the devnames array forms
17280Sstevel@tonic-gate * the entire snapshot (see also di_copydevnm).
17290Sstevel@tonic-gate */
17300Sstevel@tonic-gate static di_off_t
di_copytree(struct dev_info * root,di_off_t * off_p,struct di_state * st)17310Sstevel@tonic-gate di_copytree(struct dev_info *root, di_off_t *off_p, struct di_state *st)
17320Sstevel@tonic-gate {
17337224Scth di_off_t off;
17347224Scth struct dev_info *node;
17357224Scth struct di_stack *dsp = kmem_zalloc(sizeof (struct di_stack), KM_SLEEP);
17360Sstevel@tonic-gate
17370Sstevel@tonic-gate dcmn_err((CE_CONT, "di_copytree: root = %p, *off_p = %x\n",
17384870Sjg (void *)root, *off_p));
17390Sstevel@tonic-gate
17400Sstevel@tonic-gate /* force attach drivers */
17411333Scth if (i_ddi_devi_attached((dev_info_t *)root) &&
17420Sstevel@tonic-gate (st->command & DINFOSUBTREE) && (st->command & DINFOFORCE)) {
17430Sstevel@tonic-gate (void) ndi_devi_config((dev_info_t *)root,
17440Sstevel@tonic-gate NDI_CONFIG | NDI_DEVI_PERSIST | NDI_NO_EVENT |
17450Sstevel@tonic-gate NDI_DRV_CONF_REPROBE);
17460Sstevel@tonic-gate }
17470Sstevel@tonic-gate
17480Sstevel@tonic-gate /*
17490Sstevel@tonic-gate * Push top_devinfo onto a stack
17500Sstevel@tonic-gate *
17510Sstevel@tonic-gate * The stack is necessary to avoid recursion, which can overrun
17520Sstevel@tonic-gate * the kernel stack.
17530Sstevel@tonic-gate */
17540Sstevel@tonic-gate PUSH_STACK(dsp, root, off_p);
17550Sstevel@tonic-gate
17560Sstevel@tonic-gate /*
17570Sstevel@tonic-gate * As long as there is a node on the stack, copy the node.
17580Sstevel@tonic-gate * di_copynode() is responsible for pushing and popping
17590Sstevel@tonic-gate * child and sibling nodes on the stack.
17600Sstevel@tonic-gate */
17610Sstevel@tonic-gate while (!EMPTY_STACK(dsp)) {
17627224Scth node = TOP_NODE(dsp);
17637224Scth off = di_copynode(node, dsp, st);
17640Sstevel@tonic-gate }
17650Sstevel@tonic-gate
17660Sstevel@tonic-gate /*
17670Sstevel@tonic-gate * Free the stack structure
17680Sstevel@tonic-gate */
17690Sstevel@tonic-gate kmem_free(dsp, sizeof (struct di_stack));
17700Sstevel@tonic-gate
17710Sstevel@tonic-gate return (off);
17720Sstevel@tonic-gate }
17730Sstevel@tonic-gate
17740Sstevel@tonic-gate /*
17750Sstevel@tonic-gate * This is the core function, which copies all data associated with a single
17760Sstevel@tonic-gate * node into the snapshot. The amount of information is determined by the
17770Sstevel@tonic-gate * ioctl command.
17780Sstevel@tonic-gate */
17790Sstevel@tonic-gate static di_off_t
di_copynode(struct dev_info * node,struct di_stack * dsp,struct di_state * st)17807224Scth di_copynode(struct dev_info *node, struct di_stack *dsp, struct di_state *st)
17810Sstevel@tonic-gate {
17826640Scth di_off_t off;
17836640Scth struct di_node *me;
178410923SEvan.Yan@Sun.COM size_t size;
178510923SEvan.Yan@Sun.COM struct dev_info *n;
17860Sstevel@tonic-gate
17874870Sjg dcmn_err2((CE_CONT, "di_copynode: depth = %x\n", dsp->depth));
17887224Scth ASSERT((node != NULL) && (node == TOP_NODE(dsp)));
17890Sstevel@tonic-gate
17900Sstevel@tonic-gate /*
17910Sstevel@tonic-gate * check memory usage, and fix offsets accordingly.
17920Sstevel@tonic-gate */
17937224Scth size = sizeof (struct di_node);
17947224Scth *(TOP_OFFSET(dsp)) = off = di_checkmem(st, *(TOP_OFFSET(dsp)), size);
17950Sstevel@tonic-gate me = DI_NODE(di_mem_addr(st, off));
17967224Scth me->self = off;
17977224Scth off += size;
17980Sstevel@tonic-gate
17990Sstevel@tonic-gate dcmn_err((CE_CONT, "copy node %s, instance #%d, at offset 0x%x\n",
18004870Sjg node->devi_node_name, node->devi_instance, off));
18010Sstevel@tonic-gate
18020Sstevel@tonic-gate /*
18030Sstevel@tonic-gate * Node parameters:
18040Sstevel@tonic-gate * self -- offset of current node within snapshot
18050Sstevel@tonic-gate * nodeid -- pointer to PROM node (tri-valued)
18060Sstevel@tonic-gate * state -- hot plugging device state
18077224Scth * node_state -- devinfo node state
18080Sstevel@tonic-gate */
18090Sstevel@tonic-gate me->instance = node->devi_instance;
18100Sstevel@tonic-gate me->nodeid = node->devi_nodeid;
18110Sstevel@tonic-gate me->node_class = node->devi_node_class;
18120Sstevel@tonic-gate me->attributes = node->devi_node_attributes;
18130Sstevel@tonic-gate me->state = node->devi_state;
18144444Svikram me->flags = node->devi_flags;
18150Sstevel@tonic-gate me->node_state = node->devi_node_state;
1816893Srs135747 me->next_vhci = 0; /* Filled up by build_vhci_list. */
1817893Srs135747 me->top_phci = 0; /* Filled up by build_phci_list. */
1818893Srs135747 me->next_phci = 0; /* Filled up by build_phci_list. */
1819893Srs135747 me->multipath_component = MULTIPATH_COMPONENT_NONE; /* set default. */
18200Sstevel@tonic-gate me->user_private_data = NULL;
18210Sstevel@tonic-gate
18220Sstevel@tonic-gate /*
18230Sstevel@tonic-gate * Get parent's offset in snapshot from the stack
18240Sstevel@tonic-gate * and store it in the current node
18250Sstevel@tonic-gate */
18260Sstevel@tonic-gate if (dsp->depth > 1) {
18270Sstevel@tonic-gate me->parent = *(PARENT_OFFSET(dsp));
18280Sstevel@tonic-gate }
18290Sstevel@tonic-gate
18300Sstevel@tonic-gate /*
18310Sstevel@tonic-gate * Save the offset of this di_node in a hash table.
18320Sstevel@tonic-gate * This is used later to resolve references to this
18330Sstevel@tonic-gate * dip from other parts of the tree (per-driver list,
18340Sstevel@tonic-gate * multipathing linkages, layered usage linkages).
18350Sstevel@tonic-gate * The key used for the hash table is derived from
18360Sstevel@tonic-gate * information in the dip.
18370Sstevel@tonic-gate */
18380Sstevel@tonic-gate di_register_dip(st, (dev_info_t *)node, me->self);
18390Sstevel@tonic-gate
18400Sstevel@tonic-gate #ifdef DEVID_COMPATIBILITY
18410Sstevel@tonic-gate /* check for devid as property marker */
18426640Scth if (node->devi_devid_str) {
18430Sstevel@tonic-gate ddi_devid_t devid;
18440Sstevel@tonic-gate
18450Sstevel@tonic-gate /*
18466640Scth * The devid is now represented as a property. For
18476640Scth * compatibility with di_devid() interface in libdevinfo we
18486640Scth * must return it as a binary structure in the snapshot. When
18496640Scth * (if) di_devid() is removed from libdevinfo then the code
18506640Scth * related to DEVID_COMPATIBILITY can be removed.
18510Sstevel@tonic-gate */
18526640Scth if (ddi_devid_str_decode(node->devi_devid_str, &devid, NULL) ==
18536640Scth DDI_SUCCESS) {
18547224Scth size = ddi_devid_sizeof(devid);
18557224Scth off = di_checkmem(st, off, size);
18566640Scth me->devid = off;
18577224Scth bcopy(devid, di_mem_addr(st, off), size);
18587224Scth off += size;
18596640Scth ddi_devid_free(devid);
18600Sstevel@tonic-gate }
18610Sstevel@tonic-gate }
18620Sstevel@tonic-gate #endif /* DEVID_COMPATIBILITY */
18630Sstevel@tonic-gate
18640Sstevel@tonic-gate if (node->devi_node_name) {
18657224Scth size = strlen(node->devi_node_name) + 1;
18667224Scth me->node_name = off = di_checkmem(st, off, size);
18670Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), node->devi_node_name);
18687224Scth off += size;
18690Sstevel@tonic-gate }
18700Sstevel@tonic-gate
18710Sstevel@tonic-gate if (node->devi_compat_names && (node->devi_compat_length > 1)) {
18727224Scth size = node->devi_compat_length;
18737224Scth me->compat_names = off = di_checkmem(st, off, size);
18747224Scth me->compat_length = (int)size;
18757224Scth bcopy(node->devi_compat_names, di_mem_addr(st, off), size);
18767224Scth off += size;
18770Sstevel@tonic-gate }
18780Sstevel@tonic-gate
18790Sstevel@tonic-gate if (node->devi_addr) {
18807224Scth size = strlen(node->devi_addr) + 1;
18817224Scth me->address = off = di_checkmem(st, off, size);
18820Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), node->devi_addr);
18837224Scth off += size;
18840Sstevel@tonic-gate }
18850Sstevel@tonic-gate
18860Sstevel@tonic-gate if (node->devi_binding_name) {
18877224Scth size = strlen(node->devi_binding_name) + 1;
18887224Scth me->bind_name = off = di_checkmem(st, off, size);
18890Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), node->devi_binding_name);
18907224Scth off += size;
18910Sstevel@tonic-gate }
18920Sstevel@tonic-gate
18930Sstevel@tonic-gate me->drv_major = node->devi_major;
18940Sstevel@tonic-gate
18950Sstevel@tonic-gate /*
18960Sstevel@tonic-gate * If the dip is BOUND, set the next pointer of the
18970Sstevel@tonic-gate * per-instance list to -1, indicating that it is yet to be resolved.
18980Sstevel@tonic-gate * This will be resolved later in snap_driver_list().
18990Sstevel@tonic-gate */
19000Sstevel@tonic-gate if (me->drv_major != -1) {
19010Sstevel@tonic-gate me->next = -1;
19020Sstevel@tonic-gate } else {
19030Sstevel@tonic-gate me->next = 0;
19040Sstevel@tonic-gate }
19050Sstevel@tonic-gate
19060Sstevel@tonic-gate /*
19070Sstevel@tonic-gate * An optimization to skip mutex_enter when not needed.
19080Sstevel@tonic-gate */
190910923SEvan.Yan@Sun.COM if (!((DINFOMINOR | DINFOPROP | DINFOPATH | DINFOHP) & st->command)) {
19100Sstevel@tonic-gate goto priv_data;
19110Sstevel@tonic-gate }
19120Sstevel@tonic-gate
19130Sstevel@tonic-gate /*
19147224Scth * LOCKING: We already have an active ndi_devi_enter to gather the
19157224Scth * minor data, and we will take devi_lock to gather properties as
19167224Scth * needed off di_getprop.
19170Sstevel@tonic-gate */
19180Sstevel@tonic-gate if (!(DINFOMINOR & st->command)) {
19190Sstevel@tonic-gate goto path;
19200Sstevel@tonic-gate }
19210Sstevel@tonic-gate
19227224Scth ASSERT(DEVI_BUSY_OWNED(node));
19230Sstevel@tonic-gate if (node->devi_minor) { /* minor data */
19247224Scth me->minor_data = off;
19250Sstevel@tonic-gate off = di_getmdata(node->devi_minor, &me->minor_data,
19260Sstevel@tonic-gate me->self, st);
19270Sstevel@tonic-gate }
19280Sstevel@tonic-gate
19290Sstevel@tonic-gate path:
19300Sstevel@tonic-gate if (!(DINFOPATH & st->command)) {
19310Sstevel@tonic-gate goto property;
19320Sstevel@tonic-gate }
19330Sstevel@tonic-gate
1934893Srs135747 if (MDI_VHCI(node)) {
1935893Srs135747 me->multipath_component = MULTIPATH_COMPONENT_VHCI;
1936893Srs135747 }
1937893Srs135747
19380Sstevel@tonic-gate if (MDI_CLIENT(node)) {
1939893Srs135747 me->multipath_component = MULTIPATH_COMPONENT_CLIENT;
19407224Scth me->multipath_client = off;
19410Sstevel@tonic-gate off = di_getpath_data((dev_info_t *)node, &me->multipath_client,
19420Sstevel@tonic-gate me->self, st, 1);
19430Sstevel@tonic-gate dcmn_err((CE_WARN, "me->multipath_client = %x for node %p "
19440Sstevel@tonic-gate "component type = %d. off=%d",
19450Sstevel@tonic-gate me->multipath_client,
19460Sstevel@tonic-gate (void *)node, node->devi_mdi_component, off));
19470Sstevel@tonic-gate }
19480Sstevel@tonic-gate
19490Sstevel@tonic-gate if (MDI_PHCI(node)) {
1950893Srs135747 me->multipath_component = MULTIPATH_COMPONENT_PHCI;
19517224Scth me->multipath_phci = off;
19520Sstevel@tonic-gate off = di_getpath_data((dev_info_t *)node, &me->multipath_phci,
19530Sstevel@tonic-gate me->self, st, 0);
19540Sstevel@tonic-gate dcmn_err((CE_WARN, "me->multipath_phci = %x for node %p "
19550Sstevel@tonic-gate "component type = %d. off=%d",
19560Sstevel@tonic-gate me->multipath_phci,
19570Sstevel@tonic-gate (void *)node, node->devi_mdi_component, off));
19580Sstevel@tonic-gate }
19590Sstevel@tonic-gate
19600Sstevel@tonic-gate property:
19610Sstevel@tonic-gate if (!(DINFOPROP & st->command)) {
196210923SEvan.Yan@Sun.COM goto hotplug_data;
19630Sstevel@tonic-gate }
19640Sstevel@tonic-gate
19650Sstevel@tonic-gate if (node->devi_drv_prop_ptr) { /* driver property list */
19667224Scth me->drv_prop = off;
19677224Scth off = di_getprop(DI_PROP_DRV_LIST, &node->devi_drv_prop_ptr,
19687224Scth &me->drv_prop, st, node);
19690Sstevel@tonic-gate }
19700Sstevel@tonic-gate
19710Sstevel@tonic-gate if (node->devi_sys_prop_ptr) { /* system property list */
19727224Scth me->sys_prop = off;
19737224Scth off = di_getprop(DI_PROP_SYS_LIST, &node->devi_sys_prop_ptr,
19747224Scth &me->sys_prop, st, node);
19750Sstevel@tonic-gate }
19760Sstevel@tonic-gate
19770Sstevel@tonic-gate if (node->devi_hw_prop_ptr) { /* hardware property list */
19787224Scth me->hw_prop = off;
19797224Scth off = di_getprop(DI_PROP_HW_LIST, &node->devi_hw_prop_ptr,
19807224Scth &me->hw_prop, st, node);
19810Sstevel@tonic-gate }
19820Sstevel@tonic-gate
19830Sstevel@tonic-gate if (node->devi_global_prop_list == NULL) {
19840Sstevel@tonic-gate me->glob_prop = (di_off_t)-1; /* not global property */
19850Sstevel@tonic-gate } else {
19860Sstevel@tonic-gate /*
19870Sstevel@tonic-gate * Make copy of global property list if this devinfo refers
19880Sstevel@tonic-gate * global properties different from what's on the devnames
19890Sstevel@tonic-gate * array. It can happen if there has been a forced
19900Sstevel@tonic-gate * driver.conf update. See mod_drv(1M).
19910Sstevel@tonic-gate */
19920Sstevel@tonic-gate ASSERT(me->drv_major != -1);
19930Sstevel@tonic-gate if (node->devi_global_prop_list !=
19940Sstevel@tonic-gate devnamesp[me->drv_major].dn_global_prop_ptr) {
19957224Scth me->glob_prop = off;
19967224Scth off = di_getprop(DI_PROP_GLB_LIST,
19977224Scth &node->devi_global_prop_list->prop_list,
19987224Scth &me->glob_prop, st, node);
19990Sstevel@tonic-gate }
20000Sstevel@tonic-gate }
20010Sstevel@tonic-gate
200210923SEvan.Yan@Sun.COM hotplug_data:
200310923SEvan.Yan@Sun.COM if (!(DINFOHP & st->command)) {
200410923SEvan.Yan@Sun.COM goto priv_data;
200510923SEvan.Yan@Sun.COM }
200610923SEvan.Yan@Sun.COM
200710923SEvan.Yan@Sun.COM if (node->devi_hp_hdlp) { /* hotplug data */
200810923SEvan.Yan@Sun.COM me->hp_data = off;
200910923SEvan.Yan@Sun.COM off = di_gethpdata(node->devi_hp_hdlp, &me->hp_data, st);
201010923SEvan.Yan@Sun.COM }
201110923SEvan.Yan@Sun.COM
20120Sstevel@tonic-gate priv_data:
20130Sstevel@tonic-gate if (!(DINFOPRIVDATA & st->command)) {
20140Sstevel@tonic-gate goto pm_info;
20150Sstevel@tonic-gate }
20160Sstevel@tonic-gate
20170Sstevel@tonic-gate if (ddi_get_parent_data((dev_info_t *)node) != NULL) {
20187224Scth me->parent_data = off;
20190Sstevel@tonic-gate off = di_getppdata(node, &me->parent_data, st);
20200Sstevel@tonic-gate }
20210Sstevel@tonic-gate
20220Sstevel@tonic-gate if (ddi_get_driver_private((dev_info_t *)node) != NULL) {
20237224Scth me->driver_data = off;
20240Sstevel@tonic-gate off = di_getdpdata(node, &me->driver_data, st);
20250Sstevel@tonic-gate }
20260Sstevel@tonic-gate
20270Sstevel@tonic-gate pm_info: /* NOT implemented */
20280Sstevel@tonic-gate
20290Sstevel@tonic-gate subtree:
20307224Scth /* keep the stack aligned */
20317224Scth off = DI_ALIGN(off);
20327224Scth
20330Sstevel@tonic-gate if (!(DINFOSUBTREE & st->command)) {
20340Sstevel@tonic-gate POP_STACK(dsp);
20357224Scth return (off);
20360Sstevel@tonic-gate }
20370Sstevel@tonic-gate
20380Sstevel@tonic-gate child:
20390Sstevel@tonic-gate /*
20408912SChris.Horne@Sun.COM * If there is a visible child--push child onto stack.
20418912SChris.Horne@Sun.COM * Hold the parent (me) busy while doing so.
20420Sstevel@tonic-gate */
20438912SChris.Horne@Sun.COM if ((n = node->devi_child) != NULL) {
20448912SChris.Horne@Sun.COM /* skip hidden nodes */
20458912SChris.Horne@Sun.COM while (n && ndi_dev_is_hidden_node((dev_info_t *)n))
20468912SChris.Horne@Sun.COM n = n->devi_sibling;
20478912SChris.Horne@Sun.COM if (n) {
20488912SChris.Horne@Sun.COM me->child = off;
20498912SChris.Horne@Sun.COM PUSH_STACK(dsp, n, &me->child);
20508912SChris.Horne@Sun.COM return (me->child);
20518912SChris.Horne@Sun.COM }
20520Sstevel@tonic-gate }
20530Sstevel@tonic-gate
20540Sstevel@tonic-gate sibling:
20550Sstevel@tonic-gate /*
20568912SChris.Horne@Sun.COM * Done with any child nodes, unroll the stack till a visible
20578912SChris.Horne@Sun.COM * sibling of a parent node is found or root node is reached.
20580Sstevel@tonic-gate */
20590Sstevel@tonic-gate POP_STACK(dsp);
20608912SChris.Horne@Sun.COM while (!EMPTY_STACK(dsp)) {
20618912SChris.Horne@Sun.COM if ((n = node->devi_sibling) != NULL) {
20628912SChris.Horne@Sun.COM /* skip hidden nodes */
20638912SChris.Horne@Sun.COM while (n && ndi_dev_is_hidden_node((dev_info_t *)n))
20648912SChris.Horne@Sun.COM n = n->devi_sibling;
20658912SChris.Horne@Sun.COM if (n) {
20668912SChris.Horne@Sun.COM me->sibling = DI_ALIGN(off);
20678912SChris.Horne@Sun.COM PUSH_STACK(dsp, n, &me->sibling);
20688912SChris.Horne@Sun.COM return (me->sibling);
20698912SChris.Horne@Sun.COM }
20708912SChris.Horne@Sun.COM }
20710Sstevel@tonic-gate node = TOP_NODE(dsp);
20720Sstevel@tonic-gate me = DI_NODE(di_mem_addr(st, *(TOP_OFFSET(dsp))));
20730Sstevel@tonic-gate POP_STACK(dsp);
20740Sstevel@tonic-gate }
20750Sstevel@tonic-gate
20760Sstevel@tonic-gate /*
20770Sstevel@tonic-gate * DONE with all nodes
20780Sstevel@tonic-gate */
20797224Scth return (off);
20800Sstevel@tonic-gate }
20810Sstevel@tonic-gate
20820Sstevel@tonic-gate static i_lnode_t *
i_lnode_alloc(int modid)20830Sstevel@tonic-gate i_lnode_alloc(int modid)
20840Sstevel@tonic-gate {
20850Sstevel@tonic-gate i_lnode_t *i_lnode;
20860Sstevel@tonic-gate
20870Sstevel@tonic-gate i_lnode = kmem_zalloc(sizeof (i_lnode_t), KM_SLEEP);
20880Sstevel@tonic-gate
20890Sstevel@tonic-gate ASSERT(modid != -1);
20900Sstevel@tonic-gate i_lnode->modid = modid;
20910Sstevel@tonic-gate
20920Sstevel@tonic-gate return (i_lnode);
20930Sstevel@tonic-gate }
20940Sstevel@tonic-gate
20950Sstevel@tonic-gate static void
i_lnode_free(i_lnode_t * i_lnode)20960Sstevel@tonic-gate i_lnode_free(i_lnode_t *i_lnode)
20970Sstevel@tonic-gate {
20980Sstevel@tonic-gate kmem_free(i_lnode, sizeof (i_lnode_t));
20990Sstevel@tonic-gate }
21000Sstevel@tonic-gate
21010Sstevel@tonic-gate static void
i_lnode_check_free(i_lnode_t * i_lnode)21020Sstevel@tonic-gate i_lnode_check_free(i_lnode_t *i_lnode)
21030Sstevel@tonic-gate {
21040Sstevel@tonic-gate /* This lnode and its dip must have been snapshotted */
21050Sstevel@tonic-gate ASSERT(i_lnode->self > 0);
21060Sstevel@tonic-gate ASSERT(i_lnode->di_node->self > 0);
21070Sstevel@tonic-gate
21080Sstevel@tonic-gate /* at least 1 link (in or out) must exist for this lnode */
21090Sstevel@tonic-gate ASSERT(i_lnode->link_in || i_lnode->link_out);
21100Sstevel@tonic-gate
21110Sstevel@tonic-gate i_lnode_free(i_lnode);
21120Sstevel@tonic-gate }
21130Sstevel@tonic-gate
21140Sstevel@tonic-gate static i_link_t *
i_link_alloc(int spec_type)21150Sstevel@tonic-gate i_link_alloc(int spec_type)
21160Sstevel@tonic-gate {
21177224Scth i_link_t *i_link;
21180Sstevel@tonic-gate
21190Sstevel@tonic-gate i_link = kmem_zalloc(sizeof (i_link_t), KM_SLEEP);
21200Sstevel@tonic-gate i_link->spec_type = spec_type;
21210Sstevel@tonic-gate
21220Sstevel@tonic-gate return (i_link);
21230Sstevel@tonic-gate }
21240Sstevel@tonic-gate
21250Sstevel@tonic-gate static void
i_link_check_free(i_link_t * i_link)21260Sstevel@tonic-gate i_link_check_free(i_link_t *i_link)
21270Sstevel@tonic-gate {
21280Sstevel@tonic-gate /* This link must have been snapshotted */
21290Sstevel@tonic-gate ASSERT(i_link->self > 0);
21300Sstevel@tonic-gate
21310Sstevel@tonic-gate /* Both endpoint lnodes must exist for this link */
21320Sstevel@tonic-gate ASSERT(i_link->src_lnode);
21330Sstevel@tonic-gate ASSERT(i_link->tgt_lnode);
21340Sstevel@tonic-gate
21350Sstevel@tonic-gate kmem_free(i_link, sizeof (i_link_t));
21360Sstevel@tonic-gate }
21370Sstevel@tonic-gate
21380Sstevel@tonic-gate /*ARGSUSED*/
21390Sstevel@tonic-gate static uint_t
i_lnode_hashfunc(void * arg,mod_hash_key_t key)21400Sstevel@tonic-gate i_lnode_hashfunc(void *arg, mod_hash_key_t key)
21410Sstevel@tonic-gate {
21420Sstevel@tonic-gate i_lnode_t *i_lnode = (i_lnode_t *)key;
21430Sstevel@tonic-gate struct di_node *ptr;
21440Sstevel@tonic-gate dev_t dev;
21450Sstevel@tonic-gate
21460Sstevel@tonic-gate dev = i_lnode->devt;
21470Sstevel@tonic-gate if (dev != DDI_DEV_T_NONE)
21480Sstevel@tonic-gate return (i_lnode->modid + getminor(dev) + getmajor(dev));
21490Sstevel@tonic-gate
21500Sstevel@tonic-gate ptr = i_lnode->di_node;
21510Sstevel@tonic-gate ASSERT(ptr->self > 0);
21520Sstevel@tonic-gate if (ptr) {
21530Sstevel@tonic-gate uintptr_t k = (uintptr_t)ptr;
21540Sstevel@tonic-gate k >>= (int)highbit(sizeof (struct di_node));
21550Sstevel@tonic-gate return ((uint_t)k);
21560Sstevel@tonic-gate }
21570Sstevel@tonic-gate
21580Sstevel@tonic-gate return (i_lnode->modid);
21590Sstevel@tonic-gate }
21600Sstevel@tonic-gate
21610Sstevel@tonic-gate static int
i_lnode_cmp(void * arg1,void * arg2)21620Sstevel@tonic-gate i_lnode_cmp(void *arg1, void *arg2)
21630Sstevel@tonic-gate {
21640Sstevel@tonic-gate i_lnode_t *i_lnode1 = (i_lnode_t *)arg1;
21650Sstevel@tonic-gate i_lnode_t *i_lnode2 = (i_lnode_t *)arg2;
21660Sstevel@tonic-gate
21670Sstevel@tonic-gate if (i_lnode1->modid != i_lnode2->modid) {
21680Sstevel@tonic-gate return ((i_lnode1->modid < i_lnode2->modid) ? -1 : 1);
21690Sstevel@tonic-gate }
21700Sstevel@tonic-gate
21710Sstevel@tonic-gate if (i_lnode1->di_node != i_lnode2->di_node)
21720Sstevel@tonic-gate return ((i_lnode1->di_node < i_lnode2->di_node) ? -1 : 1);
21730Sstevel@tonic-gate
21740Sstevel@tonic-gate if (i_lnode1->devt != i_lnode2->devt)
21750Sstevel@tonic-gate return ((i_lnode1->devt < i_lnode2->devt) ? -1 : 1);
21760Sstevel@tonic-gate
21770Sstevel@tonic-gate return (0);
21780Sstevel@tonic-gate }
21790Sstevel@tonic-gate
21800Sstevel@tonic-gate /*
21810Sstevel@tonic-gate * An lnode represents a {dip, dev_t} tuple. A link represents a
21820Sstevel@tonic-gate * {src_lnode, tgt_lnode, spec_type} tuple.
21830Sstevel@tonic-gate * The following callback assumes that LDI framework ref-counts the
21840Sstevel@tonic-gate * src_dip and tgt_dip while invoking this callback.
21850Sstevel@tonic-gate */
21860Sstevel@tonic-gate static int
di_ldi_callback(const ldi_usage_t * ldi_usage,void * arg)21870Sstevel@tonic-gate di_ldi_callback(const ldi_usage_t *ldi_usage, void *arg)
21880Sstevel@tonic-gate {
21890Sstevel@tonic-gate struct di_state *st = (struct di_state *)arg;
21900Sstevel@tonic-gate i_lnode_t *src_lnode, *tgt_lnode, *i_lnode;
21910Sstevel@tonic-gate i_link_t **i_link_next, *i_link;
21920Sstevel@tonic-gate di_off_t soff, toff;
21930Sstevel@tonic-gate mod_hash_val_t nodep = NULL;
21940Sstevel@tonic-gate int res;
21950Sstevel@tonic-gate
21960Sstevel@tonic-gate /*
21970Sstevel@tonic-gate * if the source or target of this device usage information doesn't
21987224Scth * correspond to a device node then we don't report it via
21990Sstevel@tonic-gate * libdevinfo so return.
22000Sstevel@tonic-gate */
22010Sstevel@tonic-gate if ((ldi_usage->src_dip == NULL) || (ldi_usage->tgt_dip == NULL))
22020Sstevel@tonic-gate return (LDI_USAGE_CONTINUE);
22030Sstevel@tonic-gate
22040Sstevel@tonic-gate ASSERT(e_ddi_devi_holdcnt(ldi_usage->src_dip));
22050Sstevel@tonic-gate ASSERT(e_ddi_devi_holdcnt(ldi_usage->tgt_dip));
22060Sstevel@tonic-gate
22070Sstevel@tonic-gate /*
22080Sstevel@tonic-gate * Skip the ldi_usage if either src or tgt dip is not in the
22090Sstevel@tonic-gate * snapshot. This saves us from pruning bad lnodes/links later.
22100Sstevel@tonic-gate */
22110Sstevel@tonic-gate if (di_dip_find(st, ldi_usage->src_dip, &soff) != 0)
22120Sstevel@tonic-gate return (LDI_USAGE_CONTINUE);
22130Sstevel@tonic-gate if (di_dip_find(st, ldi_usage->tgt_dip, &toff) != 0)
22140Sstevel@tonic-gate return (LDI_USAGE_CONTINUE);
22150Sstevel@tonic-gate
22160Sstevel@tonic-gate ASSERT(soff > 0);
22170Sstevel@tonic-gate ASSERT(toff > 0);
22180Sstevel@tonic-gate
22190Sstevel@tonic-gate /*
22200Sstevel@tonic-gate * allocate an i_lnode and add it to the lnode hash
22210Sstevel@tonic-gate * if it is not already present. For this particular
22220Sstevel@tonic-gate * link the lnode is a source, but it may
22230Sstevel@tonic-gate * participate as tgt or src in any number of layered
22240Sstevel@tonic-gate * operations - so it may already be in the hash.
22250Sstevel@tonic-gate */
22260Sstevel@tonic-gate i_lnode = i_lnode_alloc(ldi_usage->src_modid);
22277224Scth i_lnode->di_node = DI_NODE(di_mem_addr(st, soff));
22280Sstevel@tonic-gate i_lnode->devt = ldi_usage->src_devt;
22290Sstevel@tonic-gate
22300Sstevel@tonic-gate res = mod_hash_find(st->lnode_hash, i_lnode, &nodep);
22310Sstevel@tonic-gate if (res == MH_ERR_NOTFOUND) {
22320Sstevel@tonic-gate /*
22330Sstevel@tonic-gate * new i_lnode
22340Sstevel@tonic-gate * add it to the hash and increment the lnode count
22350Sstevel@tonic-gate */
22360Sstevel@tonic-gate res = mod_hash_insert(st->lnode_hash, i_lnode, i_lnode);
22370Sstevel@tonic-gate ASSERT(res == 0);
22380Sstevel@tonic-gate st->lnode_count++;
22390Sstevel@tonic-gate src_lnode = i_lnode;
22400Sstevel@tonic-gate } else {
22410Sstevel@tonic-gate /* this i_lnode already exists in the lnode_hash */
22420Sstevel@tonic-gate i_lnode_free(i_lnode);
22430Sstevel@tonic-gate src_lnode = (i_lnode_t *)nodep;
22440Sstevel@tonic-gate }
22450Sstevel@tonic-gate
22460Sstevel@tonic-gate /*
22470Sstevel@tonic-gate * allocate a tgt i_lnode and add it to the lnode hash
22480Sstevel@tonic-gate */
22490Sstevel@tonic-gate i_lnode = i_lnode_alloc(ldi_usage->tgt_modid);
22507224Scth i_lnode->di_node = DI_NODE(di_mem_addr(st, toff));
22510Sstevel@tonic-gate i_lnode->devt = ldi_usage->tgt_devt;
22520Sstevel@tonic-gate
22530Sstevel@tonic-gate res = mod_hash_find(st->lnode_hash, i_lnode, &nodep);
22540Sstevel@tonic-gate if (res == MH_ERR_NOTFOUND) {
22550Sstevel@tonic-gate /*
22560Sstevel@tonic-gate * new i_lnode
22570Sstevel@tonic-gate * add it to the hash and increment the lnode count
22580Sstevel@tonic-gate */
22590Sstevel@tonic-gate res = mod_hash_insert(st->lnode_hash, i_lnode, i_lnode);
22600Sstevel@tonic-gate ASSERT(res == 0);
22610Sstevel@tonic-gate st->lnode_count++;
22620Sstevel@tonic-gate tgt_lnode = i_lnode;
22630Sstevel@tonic-gate } else {
22640Sstevel@tonic-gate /* this i_lnode already exists in the lnode_hash */
22650Sstevel@tonic-gate i_lnode_free(i_lnode);
22660Sstevel@tonic-gate tgt_lnode = (i_lnode_t *)nodep;
22670Sstevel@tonic-gate }
22680Sstevel@tonic-gate
22690Sstevel@tonic-gate /*
22700Sstevel@tonic-gate * allocate a i_link
22710Sstevel@tonic-gate */
22720Sstevel@tonic-gate i_link = i_link_alloc(ldi_usage->tgt_spec_type);
22730Sstevel@tonic-gate i_link->src_lnode = src_lnode;
22740Sstevel@tonic-gate i_link->tgt_lnode = tgt_lnode;
22750Sstevel@tonic-gate
22760Sstevel@tonic-gate /*
22770Sstevel@tonic-gate * add this link onto the src i_lnodes outbound i_link list
22780Sstevel@tonic-gate */
22790Sstevel@tonic-gate i_link_next = &(src_lnode->link_out);
22800Sstevel@tonic-gate while (*i_link_next != NULL) {
22810Sstevel@tonic-gate if ((i_lnode_cmp(tgt_lnode, (*i_link_next)->tgt_lnode) == 0) &&
22820Sstevel@tonic-gate (i_link->spec_type == (*i_link_next)->spec_type)) {
22830Sstevel@tonic-gate /* this link already exists */
22840Sstevel@tonic-gate kmem_free(i_link, sizeof (i_link_t));
22850Sstevel@tonic-gate return (LDI_USAGE_CONTINUE);
22860Sstevel@tonic-gate }
22870Sstevel@tonic-gate i_link_next = &((*i_link_next)->src_link_next);
22880Sstevel@tonic-gate }
22890Sstevel@tonic-gate *i_link_next = i_link;
22900Sstevel@tonic-gate
22910Sstevel@tonic-gate /*
22920Sstevel@tonic-gate * add this link onto the tgt i_lnodes inbound i_link list
22930Sstevel@tonic-gate */
22940Sstevel@tonic-gate i_link_next = &(tgt_lnode->link_in);
22950Sstevel@tonic-gate while (*i_link_next != NULL) {
22960Sstevel@tonic-gate ASSERT(i_lnode_cmp(src_lnode, (*i_link_next)->src_lnode) != 0);
22970Sstevel@tonic-gate i_link_next = &((*i_link_next)->tgt_link_next);
22980Sstevel@tonic-gate }
22990Sstevel@tonic-gate *i_link_next = i_link;
23000Sstevel@tonic-gate
23010Sstevel@tonic-gate /*
23020Sstevel@tonic-gate * add this i_link to the link hash
23030Sstevel@tonic-gate */
23040Sstevel@tonic-gate res = mod_hash_insert(st->link_hash, i_link, i_link);
23050Sstevel@tonic-gate ASSERT(res == 0);
23060Sstevel@tonic-gate st->link_count++;
23070Sstevel@tonic-gate
23080Sstevel@tonic-gate return (LDI_USAGE_CONTINUE);
23090Sstevel@tonic-gate }
23100Sstevel@tonic-gate
23110Sstevel@tonic-gate struct i_layer_data {
23120Sstevel@tonic-gate struct di_state *st;
23130Sstevel@tonic-gate int lnode_count;
23140Sstevel@tonic-gate int link_count;
23150Sstevel@tonic-gate di_off_t lnode_off;
23168912SChris.Horne@Sun.COM di_off_t link_off;
23170Sstevel@tonic-gate };
23180Sstevel@tonic-gate
23190Sstevel@tonic-gate /*ARGSUSED*/
23200Sstevel@tonic-gate static uint_t
i_link_walker(mod_hash_key_t key,mod_hash_val_t * val,void * arg)23210Sstevel@tonic-gate i_link_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
23220Sstevel@tonic-gate {
23230Sstevel@tonic-gate i_link_t *i_link = (i_link_t *)key;
23240Sstevel@tonic-gate struct i_layer_data *data = arg;
23250Sstevel@tonic-gate struct di_link *me;
23260Sstevel@tonic-gate struct di_lnode *melnode;
23270Sstevel@tonic-gate struct di_node *medinode;
23280Sstevel@tonic-gate
23290Sstevel@tonic-gate ASSERT(i_link->self == 0);
23300Sstevel@tonic-gate
23310Sstevel@tonic-gate i_link->self = data->link_off +
23320Sstevel@tonic-gate (data->link_count * sizeof (struct di_link));
23330Sstevel@tonic-gate data->link_count++;
23340Sstevel@tonic-gate
23350Sstevel@tonic-gate ASSERT(data->link_off > 0 && data->link_count > 0);
23360Sstevel@tonic-gate ASSERT(data->lnode_count == data->st->lnode_count); /* lnodes done */
23370Sstevel@tonic-gate ASSERT(data->link_count <= data->st->link_count);
23380Sstevel@tonic-gate
23390Sstevel@tonic-gate /* fill in fields for the di_link snapshot */
23407224Scth me = DI_LINK(di_mem_addr(data->st, i_link->self));
23410Sstevel@tonic-gate me->self = i_link->self;
23420Sstevel@tonic-gate me->spec_type = i_link->spec_type;
23430Sstevel@tonic-gate
23440Sstevel@tonic-gate /*
23450Sstevel@tonic-gate * The src_lnode and tgt_lnode i_lnode_t for this i_link_t
23460Sstevel@tonic-gate * are created during the LDI table walk. Since we are
23470Sstevel@tonic-gate * walking the link hash, the lnode hash has already been
23480Sstevel@tonic-gate * walked and the lnodes have been snapshotted. Save lnode
23490Sstevel@tonic-gate * offsets.
23500Sstevel@tonic-gate */
23510Sstevel@tonic-gate me->src_lnode = i_link->src_lnode->self;
23520Sstevel@tonic-gate me->tgt_lnode = i_link->tgt_lnode->self;
23530Sstevel@tonic-gate
23540Sstevel@tonic-gate /*
23550Sstevel@tonic-gate * Save this link's offset in the src_lnode snapshot's link_out
23560Sstevel@tonic-gate * field
23570Sstevel@tonic-gate */
23587224Scth melnode = DI_LNODE(di_mem_addr(data->st, me->src_lnode));
23590Sstevel@tonic-gate me->src_link_next = melnode->link_out;
23600Sstevel@tonic-gate melnode->link_out = me->self;
23610Sstevel@tonic-gate
23620Sstevel@tonic-gate /*
23630Sstevel@tonic-gate * Put this link on the tgt_lnode's link_in field
23640Sstevel@tonic-gate */
23657224Scth melnode = DI_LNODE(di_mem_addr(data->st, me->tgt_lnode));
23660Sstevel@tonic-gate me->tgt_link_next = melnode->link_in;
23670Sstevel@tonic-gate melnode->link_in = me->self;
23680Sstevel@tonic-gate
23690Sstevel@tonic-gate /*
23700Sstevel@tonic-gate * An i_lnode_t is only created if the corresponding dip exists
23710Sstevel@tonic-gate * in the snapshot. A pointer to the di_node is saved in the
23720Sstevel@tonic-gate * i_lnode_t when it is allocated. For this link, get the di_node
23730Sstevel@tonic-gate * for the source lnode. Then put the link on the di_node's list
23740Sstevel@tonic-gate * of src links
23750Sstevel@tonic-gate */
23760Sstevel@tonic-gate medinode = i_link->src_lnode->di_node;
23770Sstevel@tonic-gate me->src_node_next = medinode->src_links;
23780Sstevel@tonic-gate medinode->src_links = me->self;
23790Sstevel@tonic-gate
23800Sstevel@tonic-gate /*
23810Sstevel@tonic-gate * Put this link on the tgt_links list of the target
23820Sstevel@tonic-gate * dip.
23830Sstevel@tonic-gate */
23840Sstevel@tonic-gate medinode = i_link->tgt_lnode->di_node;
23850Sstevel@tonic-gate me->tgt_node_next = medinode->tgt_links;
23860Sstevel@tonic-gate medinode->tgt_links = me->self;
23870Sstevel@tonic-gate
23880Sstevel@tonic-gate return (MH_WALK_CONTINUE);
23890Sstevel@tonic-gate }
23900Sstevel@tonic-gate
23910Sstevel@tonic-gate /*ARGSUSED*/
23920Sstevel@tonic-gate static uint_t
i_lnode_walker(mod_hash_key_t key,mod_hash_val_t * val,void * arg)23930Sstevel@tonic-gate i_lnode_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
23940Sstevel@tonic-gate {
23950Sstevel@tonic-gate i_lnode_t *i_lnode = (i_lnode_t *)key;
23960Sstevel@tonic-gate struct i_layer_data *data = arg;
23970Sstevel@tonic-gate struct di_lnode *me;
23980Sstevel@tonic-gate struct di_node *medinode;
23990Sstevel@tonic-gate
24000Sstevel@tonic-gate ASSERT(i_lnode->self == 0);
24010Sstevel@tonic-gate
24020Sstevel@tonic-gate i_lnode->self = data->lnode_off +
24030Sstevel@tonic-gate (data->lnode_count * sizeof (struct di_lnode));
24040Sstevel@tonic-gate data->lnode_count++;
24050Sstevel@tonic-gate
24060Sstevel@tonic-gate ASSERT(data->lnode_off > 0 && data->lnode_count > 0);
24070Sstevel@tonic-gate ASSERT(data->link_count == 0); /* links not done yet */
24080Sstevel@tonic-gate ASSERT(data->lnode_count <= data->st->lnode_count);
24090Sstevel@tonic-gate
24100Sstevel@tonic-gate /* fill in fields for the di_lnode snapshot */
24117224Scth me = DI_LNODE(di_mem_addr(data->st, i_lnode->self));
24120Sstevel@tonic-gate me->self = i_lnode->self;
24130Sstevel@tonic-gate
24140Sstevel@tonic-gate if (i_lnode->devt == DDI_DEV_T_NONE) {
24157009Scth me->dev_major = DDI_MAJOR_T_NONE;
24167009Scth me->dev_minor = DDI_MAJOR_T_NONE;
24170Sstevel@tonic-gate } else {
24180Sstevel@tonic-gate me->dev_major = getmajor(i_lnode->devt);
24190Sstevel@tonic-gate me->dev_minor = getminor(i_lnode->devt);
24200Sstevel@tonic-gate }
24210Sstevel@tonic-gate
24220Sstevel@tonic-gate /*
24230Sstevel@tonic-gate * The dip corresponding to this lnode must exist in
24240Sstevel@tonic-gate * the snapshot or we wouldn't have created the i_lnode_t
24250Sstevel@tonic-gate * during LDI walk. Save the offset of the dip.
24260Sstevel@tonic-gate */
24270Sstevel@tonic-gate ASSERT(i_lnode->di_node && i_lnode->di_node->self > 0);
24280Sstevel@tonic-gate me->node = i_lnode->di_node->self;
24290Sstevel@tonic-gate
24300Sstevel@tonic-gate /*
24310Sstevel@tonic-gate * There must be at least one link in or out of this lnode
24320Sstevel@tonic-gate * or we wouldn't have created it. These fields will be set
24330Sstevel@tonic-gate * during the link hash walk.
24340Sstevel@tonic-gate */
24350Sstevel@tonic-gate ASSERT((i_lnode->link_in != NULL) || (i_lnode->link_out != NULL));
24360Sstevel@tonic-gate
24370Sstevel@tonic-gate /*
24380Sstevel@tonic-gate * set the offset of the devinfo node associated with this
24390Sstevel@tonic-gate * lnode. Also update the node_next next pointer. this pointer
24400Sstevel@tonic-gate * is set if there are multiple lnodes associated with the same
24410Sstevel@tonic-gate * devinfo node. (could occure when multiple minor nodes
24420Sstevel@tonic-gate * are open for one device, etc.)
24430Sstevel@tonic-gate */
24440Sstevel@tonic-gate medinode = i_lnode->di_node;
24450Sstevel@tonic-gate me->node_next = medinode->lnodes;
24460Sstevel@tonic-gate medinode->lnodes = me->self;
24470Sstevel@tonic-gate
24480Sstevel@tonic-gate return (MH_WALK_CONTINUE);
24490Sstevel@tonic-gate }
24500Sstevel@tonic-gate
24510Sstevel@tonic-gate static di_off_t
di_getlink_data(di_off_t off,struct di_state * st)24520Sstevel@tonic-gate di_getlink_data(di_off_t off, struct di_state *st)
24530Sstevel@tonic-gate {
24547224Scth struct i_layer_data data = {0};
24557224Scth size_t size;
24560Sstevel@tonic-gate
24570Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_copylyr: off = %x\n", off));
24580Sstevel@tonic-gate
24590Sstevel@tonic-gate st->lnode_hash = mod_hash_create_extended("di_lnode_hash", 32,
24600Sstevel@tonic-gate mod_hash_null_keydtor, (void (*)(mod_hash_val_t))i_lnode_check_free,
24610Sstevel@tonic-gate i_lnode_hashfunc, NULL, i_lnode_cmp, KM_SLEEP);
24620Sstevel@tonic-gate
24630Sstevel@tonic-gate st->link_hash = mod_hash_create_ptrhash("di_link_hash", 32,
24640Sstevel@tonic-gate (void (*)(mod_hash_val_t))i_link_check_free, sizeof (i_link_t));
24650Sstevel@tonic-gate
24660Sstevel@tonic-gate /* get driver layering information */
24670Sstevel@tonic-gate (void) ldi_usage_walker(st, di_ldi_callback);
24680Sstevel@tonic-gate
24690Sstevel@tonic-gate /* check if there is any link data to include in the snapshot */
24700Sstevel@tonic-gate if (st->lnode_count == 0) {
24710Sstevel@tonic-gate ASSERT(st->link_count == 0);
24720Sstevel@tonic-gate goto out;
24730Sstevel@tonic-gate }
24740Sstevel@tonic-gate
24750Sstevel@tonic-gate ASSERT(st->link_count != 0);
24760Sstevel@tonic-gate
24770Sstevel@tonic-gate /* get a pointer to snapshot memory for all the di_lnodes */
24780Sstevel@tonic-gate size = sizeof (struct di_lnode) * st->lnode_count;
24790Sstevel@tonic-gate data.lnode_off = off = di_checkmem(st, off, size);
24807224Scth off += size;
24810Sstevel@tonic-gate
24820Sstevel@tonic-gate /* get a pointer to snapshot memory for all the di_links */
24830Sstevel@tonic-gate size = sizeof (struct di_link) * st->link_count;
24840Sstevel@tonic-gate data.link_off = off = di_checkmem(st, off, size);
24857224Scth off += size;
24860Sstevel@tonic-gate
24870Sstevel@tonic-gate data.lnode_count = data.link_count = 0;
24880Sstevel@tonic-gate data.st = st;
24890Sstevel@tonic-gate
24900Sstevel@tonic-gate /*
24910Sstevel@tonic-gate * We have lnodes and links that will go into the
24920Sstevel@tonic-gate * snapshot, so let's walk the respective hashes
24930Sstevel@tonic-gate * and snapshot them. The various linkages are
24940Sstevel@tonic-gate * also set up during the walk.
24950Sstevel@tonic-gate */
24960Sstevel@tonic-gate mod_hash_walk(st->lnode_hash, i_lnode_walker, (void *)&data);
24970Sstevel@tonic-gate ASSERT(data.lnode_count == st->lnode_count);
24980Sstevel@tonic-gate
24990Sstevel@tonic-gate mod_hash_walk(st->link_hash, i_link_walker, (void *)&data);
25000Sstevel@tonic-gate ASSERT(data.link_count == st->link_count);
25010Sstevel@tonic-gate
25020Sstevel@tonic-gate out:
25030Sstevel@tonic-gate /* free up the i_lnodes and i_links used to create the snapshot */
25040Sstevel@tonic-gate mod_hash_destroy_hash(st->lnode_hash);
25050Sstevel@tonic-gate mod_hash_destroy_hash(st->link_hash);
25060Sstevel@tonic-gate st->lnode_count = 0;
25070Sstevel@tonic-gate st->link_count = 0;
25080Sstevel@tonic-gate
25090Sstevel@tonic-gate return (off);
25100Sstevel@tonic-gate }
25110Sstevel@tonic-gate
25120Sstevel@tonic-gate
25130Sstevel@tonic-gate /*
25140Sstevel@tonic-gate * Copy all minor data nodes attached to a devinfo node into the snapshot.
25157224Scth * It is called from di_copynode with active ndi_devi_enter to protect
25167224Scth * the list of minor nodes.
25170Sstevel@tonic-gate */
25180Sstevel@tonic-gate static di_off_t
di_getmdata(struct ddi_minor_data * mnode,di_off_t * off_p,di_off_t node,struct di_state * st)25190Sstevel@tonic-gate di_getmdata(struct ddi_minor_data *mnode, di_off_t *off_p, di_off_t node,
25200Sstevel@tonic-gate struct di_state *st)
25210Sstevel@tonic-gate {
25227224Scth di_off_t off;
25237224Scth struct di_minor *me;
25247224Scth size_t size;
25250Sstevel@tonic-gate
25260Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_getmdata:\n"));
25270Sstevel@tonic-gate
25280Sstevel@tonic-gate /*
25290Sstevel@tonic-gate * check memory first
25300Sstevel@tonic-gate */
25310Sstevel@tonic-gate off = di_checkmem(st, *off_p, sizeof (struct di_minor));
25320Sstevel@tonic-gate *off_p = off;
25330Sstevel@tonic-gate
25340Sstevel@tonic-gate do {
25357224Scth me = DI_MINOR(di_mem_addr(st, off));
25360Sstevel@tonic-gate me->self = off;
25370Sstevel@tonic-gate me->type = mnode->type;
25380Sstevel@tonic-gate me->node = node;
25390Sstevel@tonic-gate me->user_private_data = NULL;
25400Sstevel@tonic-gate
25417224Scth off += sizeof (struct di_minor);
25420Sstevel@tonic-gate
25430Sstevel@tonic-gate /*
25440Sstevel@tonic-gate * Split dev_t to major/minor, so it works for
25450Sstevel@tonic-gate * both ILP32 and LP64 model
25460Sstevel@tonic-gate */
25470Sstevel@tonic-gate me->dev_major = getmajor(mnode->ddm_dev);
25480Sstevel@tonic-gate me->dev_minor = getminor(mnode->ddm_dev);
25490Sstevel@tonic-gate me->spec_type = mnode->ddm_spec_type;
25500Sstevel@tonic-gate
25510Sstevel@tonic-gate if (mnode->ddm_name) {
25527224Scth size = strlen(mnode->ddm_name) + 1;
25537224Scth me->name = off = di_checkmem(st, off, size);
25540Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), mnode->ddm_name);
25557224Scth off += size;
25560Sstevel@tonic-gate }
25570Sstevel@tonic-gate
25580Sstevel@tonic-gate if (mnode->ddm_node_type) {
25597224Scth size = strlen(mnode->ddm_node_type) + 1;
25607224Scth me->node_type = off = di_checkmem(st, off, size);
25610Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off),
25624870Sjg mnode->ddm_node_type);
25637224Scth off += size;
25640Sstevel@tonic-gate }
25650Sstevel@tonic-gate
25660Sstevel@tonic-gate off = di_checkmem(st, off, sizeof (struct di_minor));
25670Sstevel@tonic-gate me->next = off;
25680Sstevel@tonic-gate mnode = mnode->next;
25690Sstevel@tonic-gate } while (mnode);
25700Sstevel@tonic-gate
25710Sstevel@tonic-gate me->next = 0;
25720Sstevel@tonic-gate
25730Sstevel@tonic-gate return (off);
25740Sstevel@tonic-gate }
25750Sstevel@tonic-gate
25760Sstevel@tonic-gate /*
25770Sstevel@tonic-gate * di_register_dip(), di_find_dip(): The dip must be protected
25780Sstevel@tonic-gate * from deallocation when using these routines - this can either
25790Sstevel@tonic-gate * be a reference count, a busy hold or a per-driver lock.
25800Sstevel@tonic-gate */
25810Sstevel@tonic-gate
25820Sstevel@tonic-gate static void
di_register_dip(struct di_state * st,dev_info_t * dip,di_off_t off)25830Sstevel@tonic-gate di_register_dip(struct di_state *st, dev_info_t *dip, di_off_t off)
25840Sstevel@tonic-gate {
25857224Scth struct dev_info *node = DEVI(dip);
25867224Scth struct di_key *key = kmem_zalloc(sizeof (*key), KM_SLEEP);
25877224Scth struct di_dkey *dk;
25880Sstevel@tonic-gate
25890Sstevel@tonic-gate ASSERT(dip);
25900Sstevel@tonic-gate ASSERT(off > 0);
25910Sstevel@tonic-gate
25920Sstevel@tonic-gate key->k_type = DI_DKEY;
25930Sstevel@tonic-gate dk = &(key->k_u.dkey);
25940Sstevel@tonic-gate
25950Sstevel@tonic-gate dk->dk_dip = dip;
25960Sstevel@tonic-gate dk->dk_major = node->devi_major;
25970Sstevel@tonic-gate dk->dk_inst = node->devi_instance;
25980Sstevel@tonic-gate dk->dk_nodeid = node->devi_nodeid;
25990Sstevel@tonic-gate
26000Sstevel@tonic-gate if (mod_hash_insert(st->reg_dip_hash, (mod_hash_key_t)key,
26010Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)off) != 0) {
26020Sstevel@tonic-gate panic(
26030Sstevel@tonic-gate "duplicate devinfo (%p) registered during device "
26040Sstevel@tonic-gate "tree walk", (void *)dip);
26050Sstevel@tonic-gate }
26060Sstevel@tonic-gate }
26070Sstevel@tonic-gate
26080Sstevel@tonic-gate
26090Sstevel@tonic-gate static int
di_dip_find(struct di_state * st,dev_info_t * dip,di_off_t * off_p)26100Sstevel@tonic-gate di_dip_find(struct di_state *st, dev_info_t *dip, di_off_t *off_p)
26110Sstevel@tonic-gate {
26120Sstevel@tonic-gate /*
26130Sstevel@tonic-gate * uintptr_t must be used because it matches the size of void *;
26140Sstevel@tonic-gate * mod_hash expects clients to place results into pointer-size
26150Sstevel@tonic-gate * containers; since di_off_t is always a 32-bit offset, alignment
26160Sstevel@tonic-gate * would otherwise be broken on 64-bit kernels.
26170Sstevel@tonic-gate */
26180Sstevel@tonic-gate uintptr_t offset;
26190Sstevel@tonic-gate struct di_key key = {0};
26200Sstevel@tonic-gate struct di_dkey *dk;
26210Sstevel@tonic-gate
26220Sstevel@tonic-gate ASSERT(st->reg_dip_hash);
26230Sstevel@tonic-gate ASSERT(dip);
26240Sstevel@tonic-gate ASSERT(off_p);
26250Sstevel@tonic-gate
26260Sstevel@tonic-gate
26270Sstevel@tonic-gate key.k_type = DI_DKEY;
26280Sstevel@tonic-gate dk = &(key.k_u.dkey);
26290Sstevel@tonic-gate
26300Sstevel@tonic-gate dk->dk_dip = dip;
26310Sstevel@tonic-gate dk->dk_major = DEVI(dip)->devi_major;
26320Sstevel@tonic-gate dk->dk_inst = DEVI(dip)->devi_instance;
26330Sstevel@tonic-gate dk->dk_nodeid = DEVI(dip)->devi_nodeid;
26340Sstevel@tonic-gate
26350Sstevel@tonic-gate if (mod_hash_find(st->reg_dip_hash, (mod_hash_key_t)&key,
26360Sstevel@tonic-gate (mod_hash_val_t *)&offset) == 0) {
26370Sstevel@tonic-gate *off_p = (di_off_t)offset;
26380Sstevel@tonic-gate return (0);
26390Sstevel@tonic-gate } else {
26400Sstevel@tonic-gate return (-1);
26410Sstevel@tonic-gate }
26420Sstevel@tonic-gate }
26430Sstevel@tonic-gate
26440Sstevel@tonic-gate /*
26450Sstevel@tonic-gate * di_register_pip(), di_find_pip(): The pip must be protected from deallocation
26460Sstevel@tonic-gate * when using these routines. The caller must do this by protecting the
26470Sstevel@tonic-gate * client(or phci)<->pip linkage while traversing the list and then holding the
26480Sstevel@tonic-gate * pip when it is found in the list.
26490Sstevel@tonic-gate */
26500Sstevel@tonic-gate
26510Sstevel@tonic-gate static void
di_register_pip(struct di_state * st,mdi_pathinfo_t * pip,di_off_t off)26520Sstevel@tonic-gate di_register_pip(struct di_state *st, mdi_pathinfo_t *pip, di_off_t off)
26530Sstevel@tonic-gate {
26540Sstevel@tonic-gate struct di_key *key = kmem_zalloc(sizeof (*key), KM_SLEEP);
26550Sstevel@tonic-gate char *path_addr;
26560Sstevel@tonic-gate struct di_pkey *pk;
26570Sstevel@tonic-gate
26580Sstevel@tonic-gate ASSERT(pip);
26590Sstevel@tonic-gate ASSERT(off > 0);
26600Sstevel@tonic-gate
26610Sstevel@tonic-gate key->k_type = DI_PKEY;
26620Sstevel@tonic-gate pk = &(key->k_u.pkey);
26630Sstevel@tonic-gate
26640Sstevel@tonic-gate pk->pk_pip = pip;
26650Sstevel@tonic-gate path_addr = mdi_pi_get_addr(pip);
26660Sstevel@tonic-gate if (path_addr)
26670Sstevel@tonic-gate pk->pk_path_addr = i_ddi_strdup(path_addr, KM_SLEEP);
26680Sstevel@tonic-gate pk->pk_client = mdi_pi_get_client(pip);
26690Sstevel@tonic-gate pk->pk_phci = mdi_pi_get_phci(pip);
26700Sstevel@tonic-gate
26710Sstevel@tonic-gate if (mod_hash_insert(st->reg_pip_hash, (mod_hash_key_t)key,
26720Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)off) != 0) {
26730Sstevel@tonic-gate panic(
26740Sstevel@tonic-gate "duplicate pathinfo (%p) registered during device "
26750Sstevel@tonic-gate "tree walk", (void *)pip);
26760Sstevel@tonic-gate }
26770Sstevel@tonic-gate }
26780Sstevel@tonic-gate
26790Sstevel@tonic-gate /*
26800Sstevel@tonic-gate * As with di_register_pip, the caller must hold or lock the pip
26810Sstevel@tonic-gate */
26820Sstevel@tonic-gate static int
di_pip_find(struct di_state * st,mdi_pathinfo_t * pip,di_off_t * off_p)26830Sstevel@tonic-gate di_pip_find(struct di_state *st, mdi_pathinfo_t *pip, di_off_t *off_p)
26840Sstevel@tonic-gate {
26850Sstevel@tonic-gate /*
26860Sstevel@tonic-gate * uintptr_t must be used because it matches the size of void *;
26870Sstevel@tonic-gate * mod_hash expects clients to place results into pointer-size
26880Sstevel@tonic-gate * containers; since di_off_t is always a 32-bit offset, alignment
26890Sstevel@tonic-gate * would otherwise be broken on 64-bit kernels.
26900Sstevel@tonic-gate */
26910Sstevel@tonic-gate uintptr_t offset;
26920Sstevel@tonic-gate struct di_key key = {0};
26930Sstevel@tonic-gate struct di_pkey *pk;
26940Sstevel@tonic-gate
26950Sstevel@tonic-gate ASSERT(st->reg_pip_hash);
26960Sstevel@tonic-gate ASSERT(off_p);
26970Sstevel@tonic-gate
26980Sstevel@tonic-gate if (pip == NULL) {
26990Sstevel@tonic-gate *off_p = 0;
27000Sstevel@tonic-gate return (0);
27010Sstevel@tonic-gate }
27020Sstevel@tonic-gate
27030Sstevel@tonic-gate key.k_type = DI_PKEY;
27040Sstevel@tonic-gate pk = &(key.k_u.pkey);
27050Sstevel@tonic-gate
27060Sstevel@tonic-gate pk->pk_pip = pip;
27070Sstevel@tonic-gate pk->pk_path_addr = mdi_pi_get_addr(pip);
27080Sstevel@tonic-gate pk->pk_client = mdi_pi_get_client(pip);
27090Sstevel@tonic-gate pk->pk_phci = mdi_pi_get_phci(pip);
27100Sstevel@tonic-gate
27110Sstevel@tonic-gate if (mod_hash_find(st->reg_pip_hash, (mod_hash_key_t)&key,
27120Sstevel@tonic-gate (mod_hash_val_t *)&offset) == 0) {
27130Sstevel@tonic-gate *off_p = (di_off_t)offset;
27140Sstevel@tonic-gate return (0);
27150Sstevel@tonic-gate } else {
27160Sstevel@tonic-gate return (-1);
27170Sstevel@tonic-gate }
27180Sstevel@tonic-gate }
27190Sstevel@tonic-gate
27200Sstevel@tonic-gate static di_path_state_t
path_state_convert(mdi_pathinfo_state_t st)27210Sstevel@tonic-gate path_state_convert(mdi_pathinfo_state_t st)
27220Sstevel@tonic-gate {
27230Sstevel@tonic-gate switch (st) {
27240Sstevel@tonic-gate case MDI_PATHINFO_STATE_ONLINE:
27250Sstevel@tonic-gate return (DI_PATH_STATE_ONLINE);
27260Sstevel@tonic-gate case MDI_PATHINFO_STATE_STANDBY:
27270Sstevel@tonic-gate return (DI_PATH_STATE_STANDBY);
27280Sstevel@tonic-gate case MDI_PATHINFO_STATE_OFFLINE:
27290Sstevel@tonic-gate return (DI_PATH_STATE_OFFLINE);
27300Sstevel@tonic-gate case MDI_PATHINFO_STATE_FAULT:
27310Sstevel@tonic-gate return (DI_PATH_STATE_FAULT);
27320Sstevel@tonic-gate default:
27330Sstevel@tonic-gate return (DI_PATH_STATE_UNKNOWN);
27340Sstevel@tonic-gate }
27350Sstevel@tonic-gate }
27360Sstevel@tonic-gate
273710696SDavid.Hollister@Sun.COM static uint_t
path_flags_convert(uint_t pi_path_flags)273810696SDavid.Hollister@Sun.COM path_flags_convert(uint_t pi_path_flags)
273910696SDavid.Hollister@Sun.COM {
274010696SDavid.Hollister@Sun.COM uint_t di_path_flags = 0;
274110696SDavid.Hollister@Sun.COM
274210696SDavid.Hollister@Sun.COM /* MDI_PATHINFO_FLAGS_HIDDEN nodes not in snapshot */
274310696SDavid.Hollister@Sun.COM
274410696SDavid.Hollister@Sun.COM if (pi_path_flags & MDI_PATHINFO_FLAGS_DEVICE_REMOVED)
274510696SDavid.Hollister@Sun.COM di_path_flags |= DI_PATH_FLAGS_DEVICE_REMOVED;
274610696SDavid.Hollister@Sun.COM
274710696SDavid.Hollister@Sun.COM return (di_path_flags);
274810696SDavid.Hollister@Sun.COM }
274910696SDavid.Hollister@Sun.COM
27500Sstevel@tonic-gate
27510Sstevel@tonic-gate static di_off_t
di_path_getprop(mdi_pathinfo_t * pip,di_off_t * off_p,struct di_state * st)27527224Scth di_path_getprop(mdi_pathinfo_t *pip, di_off_t *off_p,
27530Sstevel@tonic-gate struct di_state *st)
27540Sstevel@tonic-gate {
27557224Scth nvpair_t *prop = NULL;
27567224Scth struct di_path_prop *me;
27577224Scth int off;
27587224Scth size_t size;
27597224Scth char *str;
27607224Scth uchar_t *buf;
27617224Scth uint_t nelems;
27627224Scth
27637224Scth off = *off_p;
27640Sstevel@tonic-gate if (mdi_pi_get_next_prop(pip, NULL) == NULL) {
27650Sstevel@tonic-gate *off_p = 0;
27660Sstevel@tonic-gate return (off);
27670Sstevel@tonic-gate }
27680Sstevel@tonic-gate
27690Sstevel@tonic-gate off = di_checkmem(st, off, sizeof (struct di_path_prop));
27700Sstevel@tonic-gate *off_p = off;
27710Sstevel@tonic-gate
27720Sstevel@tonic-gate while (prop = mdi_pi_get_next_prop(pip, prop)) {
27737224Scth me = DI_PATHPROP(di_mem_addr(st, off));
27740Sstevel@tonic-gate me->self = off;
27750Sstevel@tonic-gate off += sizeof (struct di_path_prop);
27760Sstevel@tonic-gate
27770Sstevel@tonic-gate /*
27780Sstevel@tonic-gate * property name
27790Sstevel@tonic-gate */
27807224Scth size = strlen(nvpair_name(prop)) + 1;
27817224Scth me->prop_name = off = di_checkmem(st, off, size);
27820Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), nvpair_name(prop));
27837224Scth off += size;
27840Sstevel@tonic-gate
27850Sstevel@tonic-gate switch (nvpair_type(prop)) {
27860Sstevel@tonic-gate case DATA_TYPE_BYTE:
27870Sstevel@tonic-gate case DATA_TYPE_INT16:
27880Sstevel@tonic-gate case DATA_TYPE_UINT16:
27890Sstevel@tonic-gate case DATA_TYPE_INT32:
27900Sstevel@tonic-gate case DATA_TYPE_UINT32:
27910Sstevel@tonic-gate me->prop_type = DDI_PROP_TYPE_INT;
27927224Scth size = sizeof (int32_t);
27937224Scth off = di_checkmem(st, off, size);
27940Sstevel@tonic-gate (void) nvpair_value_int32(prop,
27957224Scth (int32_t *)di_mem_addr(st, off));
27960Sstevel@tonic-gate break;
27970Sstevel@tonic-gate
27980Sstevel@tonic-gate case DATA_TYPE_INT64:
27990Sstevel@tonic-gate case DATA_TYPE_UINT64:
28000Sstevel@tonic-gate me->prop_type = DDI_PROP_TYPE_INT64;
28017224Scth size = sizeof (int64_t);
28027224Scth off = di_checkmem(st, off, size);
28030Sstevel@tonic-gate (void) nvpair_value_int64(prop,
28047224Scth (int64_t *)di_mem_addr(st, off));
28050Sstevel@tonic-gate break;
28060Sstevel@tonic-gate
28070Sstevel@tonic-gate case DATA_TYPE_STRING:
28087224Scth me->prop_type = DDI_PROP_TYPE_STRING;
28090Sstevel@tonic-gate (void) nvpair_value_string(prop, &str);
28107224Scth size = strlen(str) + 1;
28117224Scth off = di_checkmem(st, off, size);
28120Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), str);
28130Sstevel@tonic-gate break;
28147224Scth
28150Sstevel@tonic-gate case DATA_TYPE_BYTE_ARRAY:
28160Sstevel@tonic-gate case DATA_TYPE_INT16_ARRAY:
28170Sstevel@tonic-gate case DATA_TYPE_UINT16_ARRAY:
28180Sstevel@tonic-gate case DATA_TYPE_INT32_ARRAY:
28190Sstevel@tonic-gate case DATA_TYPE_UINT32_ARRAY:
28200Sstevel@tonic-gate case DATA_TYPE_INT64_ARRAY:
28210Sstevel@tonic-gate case DATA_TYPE_UINT64_ARRAY:
28227224Scth me->prop_type = DDI_PROP_TYPE_BYTE;
28230Sstevel@tonic-gate (void) nvpair_value_byte_array(prop, &buf, &nelems);
28247224Scth size = nelems;
28250Sstevel@tonic-gate if (nelems != 0) {
28267224Scth off = di_checkmem(st, off, size);
28277224Scth bcopy(buf, di_mem_addr(st, off), size);
28280Sstevel@tonic-gate }
28290Sstevel@tonic-gate break;
28307224Scth
28317224Scth default: /* Unknown or unhandled type; skip it */
28327224Scth size = 0;
28337224Scth break;
28340Sstevel@tonic-gate }
28350Sstevel@tonic-gate
28367224Scth if (size > 0) {
28370Sstevel@tonic-gate me->prop_data = off;
28380Sstevel@tonic-gate }
28390Sstevel@tonic-gate
28407224Scth me->prop_len = (int)size;
28417224Scth off += size;
28420Sstevel@tonic-gate
28430Sstevel@tonic-gate off = di_checkmem(st, off, sizeof (struct di_path_prop));
28440Sstevel@tonic-gate me->prop_next = off;
28450Sstevel@tonic-gate }
28460Sstevel@tonic-gate
28470Sstevel@tonic-gate me->prop_next = 0;
28480Sstevel@tonic-gate return (off);
28490Sstevel@tonic-gate }
28500Sstevel@tonic-gate
28510Sstevel@tonic-gate
28520Sstevel@tonic-gate static void
di_path_one_endpoint(struct di_path * me,di_off_t noff,di_off_t ** off_pp,int get_client)28530Sstevel@tonic-gate di_path_one_endpoint(struct di_path *me, di_off_t noff, di_off_t **off_pp,
28540Sstevel@tonic-gate int get_client)
28550Sstevel@tonic-gate {
28560Sstevel@tonic-gate if (get_client) {
28570Sstevel@tonic-gate ASSERT(me->path_client == 0);
28580Sstevel@tonic-gate me->path_client = noff;
28590Sstevel@tonic-gate ASSERT(me->path_c_link == 0);
28600Sstevel@tonic-gate *off_pp = &me->path_c_link;
28610Sstevel@tonic-gate me->path_snap_state &=
28620Sstevel@tonic-gate ~(DI_PATH_SNAP_NOCLIENT | DI_PATH_SNAP_NOCLINK);
28630Sstevel@tonic-gate } else {
28640Sstevel@tonic-gate ASSERT(me->path_phci == 0);
28650Sstevel@tonic-gate me->path_phci = noff;
28660Sstevel@tonic-gate ASSERT(me->path_p_link == 0);
28670Sstevel@tonic-gate *off_pp = &me->path_p_link;
28680Sstevel@tonic-gate me->path_snap_state &=
28690Sstevel@tonic-gate ~(DI_PATH_SNAP_NOPHCI | DI_PATH_SNAP_NOPLINK);
28700Sstevel@tonic-gate }
28710Sstevel@tonic-gate }
28720Sstevel@tonic-gate
28730Sstevel@tonic-gate /*
28747224Scth * off_p: pointer to the linkage field. This links pips along the client|phci
28750Sstevel@tonic-gate * linkage list.
28760Sstevel@tonic-gate * noff : Offset for the endpoint dip snapshot.
28770Sstevel@tonic-gate */
28780Sstevel@tonic-gate static di_off_t
di_getpath_data(dev_info_t * dip,di_off_t * off_p,di_off_t noff,struct di_state * st,int get_client)28797224Scth di_getpath_data(dev_info_t *dip, di_off_t *off_p, di_off_t noff,
28800Sstevel@tonic-gate struct di_state *st, int get_client)
28810Sstevel@tonic-gate {
28827224Scth di_off_t off;
28837224Scth mdi_pathinfo_t *pip;
28847224Scth struct di_path *me;
28857224Scth mdi_pathinfo_t *(*next_pip)(dev_info_t *, mdi_pathinfo_t *);
28867224Scth size_t size;
28870Sstevel@tonic-gate
28880Sstevel@tonic-gate dcmn_err2((CE_WARN, "di_getpath_data: client = %d", get_client));
28890Sstevel@tonic-gate
28900Sstevel@tonic-gate /*
28910Sstevel@tonic-gate * The naming of the following mdi_xyz() is unfortunately
28920Sstevel@tonic-gate * non-intuitive. mdi_get_next_phci_path() follows the
28930Sstevel@tonic-gate * client_link i.e. the list of pip's belonging to the
28940Sstevel@tonic-gate * given client dip.
28950Sstevel@tonic-gate */
28960Sstevel@tonic-gate if (get_client)
28970Sstevel@tonic-gate next_pip = &mdi_get_next_phci_path;
28980Sstevel@tonic-gate else
28990Sstevel@tonic-gate next_pip = &mdi_get_next_client_path;
29000Sstevel@tonic-gate
29017224Scth off = *off_p;
29020Sstevel@tonic-gate
29030Sstevel@tonic-gate pip = NULL;
29040Sstevel@tonic-gate while (pip = (*next_pip)(dip, pip)) {
29050Sstevel@tonic-gate di_off_t stored_offset;
29060Sstevel@tonic-gate
29070Sstevel@tonic-gate dcmn_err((CE_WARN, "marshalling pip = %p", (void *)pip));
29080Sstevel@tonic-gate
29090Sstevel@tonic-gate mdi_pi_lock(pip);
29100Sstevel@tonic-gate
291110696SDavid.Hollister@Sun.COM /* We don't represent hidden paths in the snapshot */
291210696SDavid.Hollister@Sun.COM if (mdi_pi_ishidden(pip)) {
291310696SDavid.Hollister@Sun.COM dcmn_err((CE_WARN, "hidden, skip"));
291410696SDavid.Hollister@Sun.COM mdi_pi_unlock(pip);
291510696SDavid.Hollister@Sun.COM continue;
291610696SDavid.Hollister@Sun.COM }
291710696SDavid.Hollister@Sun.COM
29180Sstevel@tonic-gate if (di_pip_find(st, pip, &stored_offset) != -1) {
29190Sstevel@tonic-gate /*
29200Sstevel@tonic-gate * We've already seen this pathinfo node so we need to
29210Sstevel@tonic-gate * take care not to snap it again; However, one endpoint
29220Sstevel@tonic-gate * and linkage will be set here. The other endpoint
29230Sstevel@tonic-gate * and linkage has already been set when the pip was
29240Sstevel@tonic-gate * first snapshotted i.e. when the other endpoint dip
29250Sstevel@tonic-gate * was snapshotted.
29260Sstevel@tonic-gate */
29277224Scth me = DI_PATH(di_mem_addr(st, stored_offset));
29287224Scth *off_p = stored_offset;
29297224Scth
29307224Scth di_path_one_endpoint(me, noff, &off_p, get_client);
29310Sstevel@tonic-gate
29320Sstevel@tonic-gate /*
29330Sstevel@tonic-gate * The other endpoint and linkage were set when this
29340Sstevel@tonic-gate * pip was snapshotted. So we are done with both
29350Sstevel@tonic-gate * endpoints and linkages.
29360Sstevel@tonic-gate */
29370Sstevel@tonic-gate ASSERT(!(me->path_snap_state &
29380Sstevel@tonic-gate (DI_PATH_SNAP_NOCLIENT|DI_PATH_SNAP_NOPHCI)));
29390Sstevel@tonic-gate ASSERT(!(me->path_snap_state &
29400Sstevel@tonic-gate (DI_PATH_SNAP_NOCLINK|DI_PATH_SNAP_NOPLINK)));
29410Sstevel@tonic-gate
29420Sstevel@tonic-gate mdi_pi_unlock(pip);
29430Sstevel@tonic-gate continue;
29440Sstevel@tonic-gate }
29450Sstevel@tonic-gate
29460Sstevel@tonic-gate /*
29470Sstevel@tonic-gate * Now that we need to snapshot this pip, check memory
29480Sstevel@tonic-gate */
29497224Scth size = sizeof (struct di_path);
29507224Scth *off_p = off = di_checkmem(st, off, size);
29517224Scth me = DI_PATH(di_mem_addr(st, off));
29520Sstevel@tonic-gate me->self = off;
29537224Scth off += size;
29540Sstevel@tonic-gate
29550Sstevel@tonic-gate me->path_snap_state =
29560Sstevel@tonic-gate DI_PATH_SNAP_NOCLINK | DI_PATH_SNAP_NOPLINK;
29570Sstevel@tonic-gate me->path_snap_state |=
29580Sstevel@tonic-gate DI_PATH_SNAP_NOCLIENT | DI_PATH_SNAP_NOPHCI;
29590Sstevel@tonic-gate
29600Sstevel@tonic-gate /*
29610Sstevel@tonic-gate * Zero out fields as di_checkmem() doesn't guarantee
29620Sstevel@tonic-gate * zero-filled memory
29630Sstevel@tonic-gate */
29640Sstevel@tonic-gate me->path_client = me->path_phci = 0;
29650Sstevel@tonic-gate me->path_c_link = me->path_p_link = 0;
29660Sstevel@tonic-gate
29677224Scth di_path_one_endpoint(me, noff, &off_p, get_client);
29680Sstevel@tonic-gate
29690Sstevel@tonic-gate /*
29700Sstevel@tonic-gate * Note the existence of this pathinfo
29710Sstevel@tonic-gate */
29720Sstevel@tonic-gate di_register_pip(st, pip, me->self);
29730Sstevel@tonic-gate
297410696SDavid.Hollister@Sun.COM me->path_state = path_state_convert(mdi_pi_get_state(pip));
297510696SDavid.Hollister@Sun.COM me->path_flags = path_flags_convert(mdi_pi_get_flags(pip));
29760Sstevel@tonic-gate
29776640Scth me->path_instance = mdi_pi_get_path_instance(pip);
29786640Scth
29790Sstevel@tonic-gate /*
29800Sstevel@tonic-gate * Get intermediate addressing info.
29810Sstevel@tonic-gate */
29827224Scth size = strlen(mdi_pi_get_addr(pip)) + 1;
29837224Scth me->path_addr = off = di_checkmem(st, off, size);
29840Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), mdi_pi_get_addr(pip));
29857224Scth off += size;
29860Sstevel@tonic-gate
29870Sstevel@tonic-gate /*
29880Sstevel@tonic-gate * Get path properties if props are to be included in the
29890Sstevel@tonic-gate * snapshot
29900Sstevel@tonic-gate */
29910Sstevel@tonic-gate if (DINFOPROP & st->command) {
29927224Scth me->path_prop = off;
29937224Scth off = di_path_getprop(pip, &me->path_prop, st);
29940Sstevel@tonic-gate } else {
29950Sstevel@tonic-gate me->path_prop = 0;
29960Sstevel@tonic-gate }
29970Sstevel@tonic-gate
29980Sstevel@tonic-gate mdi_pi_unlock(pip);
29990Sstevel@tonic-gate }
30000Sstevel@tonic-gate
30017224Scth *off_p = 0;
30020Sstevel@tonic-gate return (off);
30030Sstevel@tonic-gate }
30040Sstevel@tonic-gate
30050Sstevel@tonic-gate /*
30067224Scth * Return driver prop_op entry point for the specified devinfo node.
30077224Scth *
30087224Scth * To return a non-NULL value:
30097224Scth * - driver must be attached and held:
30107224Scth * If driver is not attached we ignore the driver property list.
30117224Scth * No one should rely on such properties.
30127224Scth * - driver "cb_prop_op != ddi_prop_op":
30137224Scth * If "cb_prop_op == ddi_prop_op", framework does not need to call driver.
30147224Scth * XXX or parent's bus_prop_op != ddi_bus_prop_op
30157224Scth */
30167224Scth static int
di_getprop_prop_op(struct dev_info * dip)30177224Scth (*di_getprop_prop_op(struct dev_info *dip))
30187224Scth (dev_t, dev_info_t *, ddi_prop_op_t, int, char *, caddr_t, int *)
30197224Scth {
30207224Scth struct dev_ops *ops;
30217224Scth
30227224Scth /* If driver is not attached we ignore the driver property list. */
30237224Scth if ((dip == NULL) || !i_ddi_devi_attached((dev_info_t *)dip))
30247224Scth return (NULL);
30257224Scth
30267224Scth /*
30277224Scth * Some nexus drivers incorrectly set cb_prop_op to nodev, nulldev,
30287224Scth * or even NULL.
30297224Scth */
30307224Scth ops = dip->devi_ops;
30317224Scth if (ops && ops->devo_cb_ops &&
30327224Scth (ops->devo_cb_ops->cb_prop_op != ddi_prop_op) &&
30337224Scth (ops->devo_cb_ops->cb_prop_op != nodev) &&
30347224Scth (ops->devo_cb_ops->cb_prop_op != nulldev) &&
30357224Scth (ops->devo_cb_ops->cb_prop_op != NULL))
30367224Scth return (ops->devo_cb_ops->cb_prop_op);
30377224Scth return (NULL);
30387224Scth }
30397224Scth
30407224Scth static di_off_t
di_getprop_add(int list,int dyn,struct di_state * st,struct dev_info * dip,int (* prop_op)(),char * name,dev_t devt,int aflags,int alen,caddr_t aval,di_off_t off,di_off_t ** off_pp)30417224Scth di_getprop_add(int list, int dyn, struct di_state *st, struct dev_info *dip,
30427224Scth int (*prop_op)(),
30437224Scth char *name, dev_t devt, int aflags, int alen, caddr_t aval,
30447224Scth di_off_t off, di_off_t **off_pp)
30457224Scth {
30467224Scth int need_free = 0;
30477224Scth dev_t pdevt;
30487224Scth int pflags;
30497224Scth int rv;
30507224Scth caddr_t val;
30517224Scth int len;
30527224Scth size_t size;
30537224Scth struct di_prop *pp;
30547224Scth
30557224Scth /* If we have prop_op function, ask driver for latest value */
30567224Scth if (prop_op) {
30577224Scth ASSERT(dip);
30587224Scth
30597224Scth /* Must search DDI_DEV_T_NONE with DDI_DEV_T_ANY */
30607224Scth pdevt = (devt == DDI_DEV_T_NONE) ? DDI_DEV_T_ANY : devt;
30617224Scth
30627224Scth /*
30637224Scth * We have type information in flags, but are invoking an
30647224Scth * old non-typed prop_op(9E) interface. Since not all types are
30657224Scth * part of DDI_PROP_TYPE_ANY (example is DDI_PROP_TYPE_INT64),
30667224Scth * we set DDI_PROP_CONSUMER_TYPED - causing the framework to
30677224Scth * expand type bits beyond DDI_PROP_TYPE_ANY. This allows us
30687224Scth * to use the legacy prop_op(9E) interface to obtain updates
30697224Scth * non-DDI_PROP_TYPE_ANY dynamic properties.
30707224Scth */
30717224Scth pflags = aflags & ~DDI_PROP_TYPE_MASK;
30727224Scth pflags |= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
30737224Scth DDI_PROP_CONSUMER_TYPED;
30747627SChris.Horne@Sun.COM
30757627SChris.Horne@Sun.COM /*
30767627SChris.Horne@Sun.COM * Hold and exit across prop_op(9E) to avoid lock order
30777627SChris.Horne@Sun.COM * issues between
30787627SChris.Horne@Sun.COM * [ndi_devi_enter() ..prop_op(9E).. driver-lock]
30797627SChris.Horne@Sun.COM * .vs.
30807627SChris.Horne@Sun.COM * [..ioctl(9E).. driver-lock ..ddi_remove_minor_node(9F)..
30817627SChris.Horne@Sun.COM * ndi_devi_enter()]
30827627SChris.Horne@Sun.COM * ordering.
30837627SChris.Horne@Sun.COM */
30847627SChris.Horne@Sun.COM ndi_hold_devi((dev_info_t *)dip);
30857627SChris.Horne@Sun.COM ndi_devi_exit((dev_info_t *)dip, dip->devi_circular);
30867627SChris.Horne@Sun.COM rv = (*prop_op)(pdevt, (dev_info_t *)dip,
30877627SChris.Horne@Sun.COM PROP_LEN_AND_VAL_ALLOC, pflags, name, &val, &len);
30887627SChris.Horne@Sun.COM ndi_devi_enter((dev_info_t *)dip, &dip->devi_circular);
30897627SChris.Horne@Sun.COM ndi_rele_devi((dev_info_t *)dip);
30907224Scth
30917224Scth if (rv == DDI_PROP_SUCCESS) {
30927224Scth need_free = 1; /* dynamic prop obtained */
30937224Scth } else if (dyn) {
30947224Scth /*
30957224Scth * A dynamic property must succeed prop_op(9E) to show
30967224Scth * up in the snapshot - that is the only source of its
30977224Scth * value.
30987224Scth */
30997224Scth return (off); /* dynamic prop not supported */
31007224Scth } else {
31017224Scth /*
31027224Scth * In case calling the driver caused an update off
31037224Scth * prop_op(9E) of a non-dynamic property (code leading
31047224Scth * to ddi_prop_change), we defer picking up val and
31057224Scth * len informatiojn until after prop_op(9E) to ensure
31067224Scth * that we snapshot the latest value.
31077224Scth */
31087224Scth val = aval;
31097224Scth len = alen;
31107224Scth
31117224Scth }
31127224Scth } else {
31137224Scth val = aval;
31147224Scth len = alen;
31157224Scth }
31167224Scth
31177224Scth dcmn_err((CE_CONT, "di_getprop_add: list %d %s len %d val %p\n",
31187224Scth list, name ? name : "NULL", len, (void *)val));
31197224Scth
31207224Scth size = sizeof (struct di_prop);
31217224Scth **off_pp = off = di_checkmem(st, off, size);
31227224Scth pp = DI_PROP(di_mem_addr(st, off));
31237224Scth pp->self = off;
31247224Scth off += size;
31257224Scth
31267224Scth pp->dev_major = getmajor(devt);
31277224Scth pp->dev_minor = getminor(devt);
31287224Scth pp->prop_flags = aflags;
31297224Scth pp->prop_list = list;
31307224Scth
31317224Scth /* property name */
31327224Scth if (name) {
31337224Scth size = strlen(name) + 1;
31347224Scth pp->prop_name = off = di_checkmem(st, off, size);
31357224Scth (void) strcpy(di_mem_addr(st, off), name);
31367224Scth off += size;
31377224Scth } else {
31387224Scth pp->prop_name = -1;
31397224Scth }
31407224Scth
31417224Scth pp->prop_len = len;
31427224Scth if (val == NULL) {
31437224Scth pp->prop_data = -1;
31447224Scth } else if (len != 0) {
31457224Scth size = len;
31467224Scth pp->prop_data = off = di_checkmem(st, off, size);
31477224Scth bcopy(val, di_mem_addr(st, off), size);
31487224Scth off += size;
31497224Scth }
31507224Scth
31517224Scth pp->next = 0; /* assume tail for now */
31527224Scth *off_pp = &pp->next; /* return pointer to our next */
31537224Scth
31547224Scth if (need_free) /* free PROP_LEN_AND_VAL_ALLOC alloc */
31557224Scth kmem_free(val, len);
31567224Scth return (off);
31577224Scth }
31587224Scth
31597224Scth
31607224Scth /*
31610Sstevel@tonic-gate * Copy a list of properties attached to a devinfo node. Called from
31627224Scth * di_copynode with active ndi_devi_enter. The major number is passed in case
31630Sstevel@tonic-gate * we need to call driver's prop_op entry. The value of list indicates
31640Sstevel@tonic-gate * which list we are copying. Possible values are:
31650Sstevel@tonic-gate * DI_PROP_DRV_LIST, DI_PROP_SYS_LIST, DI_PROP_GLB_LIST, DI_PROP_HW_LIST
31660Sstevel@tonic-gate */
31670Sstevel@tonic-gate static di_off_t
di_getprop(int list,struct ddi_prop ** pprop,di_off_t * off_p,struct di_state * st,struct dev_info * dip)31687224Scth di_getprop(int list, struct ddi_prop **pprop, di_off_t *off_p,
31697224Scth struct di_state *st, struct dev_info *dip)
31700Sstevel@tonic-gate {
31717224Scth struct ddi_prop *prop;
31727224Scth int (*prop_op)();
31737224Scth int off;
31747224Scth struct ddi_minor_data *mn;
31757224Scth i_ddi_prop_dyn_t *dp;
31767224Scth struct plist {
31777224Scth struct plist *pl_next;
31787224Scth char *pl_name;
31797224Scth int pl_flags;
31807224Scth dev_t pl_dev;
31817224Scth int pl_len;
31827224Scth caddr_t pl_val;
31837224Scth } *pl, *pl0, **plp;
31840Sstevel@tonic-gate
31850Sstevel@tonic-gate ASSERT(st != NULL);
31860Sstevel@tonic-gate
31877224Scth off = *off_p;
31887224Scth *off_p = 0;
31897224Scth dcmn_err((CE_CONT, "di_getprop: copy property list %d at addr %p\n",
31907224Scth list, (void *)*pprop));
31917224Scth
31927224Scth /* get pointer to driver's prop_op(9E) implementation if DRV_LIST */
31937224Scth prop_op = (list == DI_PROP_DRV_LIST) ? di_getprop_prop_op(dip) : NULL;
31940Sstevel@tonic-gate
31950Sstevel@tonic-gate /*
31967224Scth * Form private list of properties, holding devi_lock for properties
31977633SChris.Horne@Sun.COM * that hang off the dip.
31980Sstevel@tonic-gate */
31997224Scth if (dip)
32007224Scth mutex_enter(&(dip->devi_lock));
32017633SChris.Horne@Sun.COM for (pl0 = NULL, plp = &pl0, prop = *pprop;
32027224Scth prop; plp = &pl->pl_next, prop = prop->prop_next) {
32037224Scth pl = kmem_alloc(sizeof (*pl), KM_SLEEP);
32047224Scth *plp = pl;
32057224Scth pl->pl_next = NULL;
32067224Scth if (prop->prop_name)
32077224Scth pl->pl_name = i_ddi_strdup(prop->prop_name, KM_SLEEP);
32087224Scth else
32097224Scth pl->pl_name = NULL;
32107224Scth pl->pl_flags = prop->prop_flags;
32117224Scth pl->pl_dev = prop->prop_dev;
32127224Scth if (prop->prop_len) {
32137224Scth pl->pl_len = prop->prop_len;
32147224Scth pl->pl_val = kmem_alloc(pl->pl_len, KM_SLEEP);
32157224Scth bcopy(prop->prop_val, pl->pl_val, pl->pl_len);
32167224Scth } else {
32177224Scth pl->pl_len = 0;
32187224Scth pl->pl_val = NULL;
32197224Scth }
32200Sstevel@tonic-gate }
32217224Scth if (dip)
32227224Scth mutex_exit(&(dip->devi_lock));
32230Sstevel@tonic-gate
32240Sstevel@tonic-gate /*
32257224Scth * Now that we have dropped devi_lock, perform a second-pass to
32267224Scth * add properties to the snapshot. We do this as a second pass
32277224Scth * because we may need to call prop_op(9E) and we can't hold
32287224Scth * devi_lock across that call.
32290Sstevel@tonic-gate */
32307224Scth for (pl = pl0; pl; pl = pl0) {
32317224Scth pl0 = pl->pl_next;
32327224Scth off = di_getprop_add(list, 0, st, dip, prop_op, pl->pl_name,
32337224Scth pl->pl_dev, pl->pl_flags, pl->pl_len, pl->pl_val,
32347224Scth off, &off_p);
32357224Scth if (pl->pl_val)
32367224Scth kmem_free(pl->pl_val, pl->pl_len);
32377224Scth if (pl->pl_name)
32387224Scth kmem_free(pl->pl_name, strlen(pl->pl_name) + 1);
32397224Scth kmem_free(pl, sizeof (*pl));
32400Sstevel@tonic-gate }
32410Sstevel@tonic-gate
32420Sstevel@tonic-gate /*
32437224Scth * If there is no prop_op or dynamic property support has been
32447224Scth * disabled, we are done.
32450Sstevel@tonic-gate */
32467224Scth if ((prop_op == NULL) || (di_prop_dyn == 0)) {
32477224Scth *off_p = 0;
32480Sstevel@tonic-gate return (off);
32490Sstevel@tonic-gate }
32500Sstevel@tonic-gate
32517224Scth /* Add dynamic driver properties to snapshot */
32527224Scth for (dp = i_ddi_prop_dyn_driver_get((dev_info_t *)dip);
32537224Scth dp && dp->dp_name; dp++) {
32547224Scth if (dp->dp_spec_type) {
32557224Scth /* if spec_type, property of matching minor */
32567224Scth ASSERT(DEVI_BUSY_OWNED(dip));
32577224Scth for (mn = dip->devi_minor; mn; mn = mn->next) {
32587224Scth if (mn->ddm_spec_type != dp->dp_spec_type)
32597224Scth continue;
32607224Scth off = di_getprop_add(list, 1, st, dip, prop_op,
32617224Scth dp->dp_name, mn->ddm_dev, dp->dp_type,
32627224Scth 0, NULL, off, &off_p);
32637224Scth }
32640Sstevel@tonic-gate } else {
32657224Scth /* property of devinfo node */
32667224Scth off = di_getprop_add(list, 1, st, dip, prop_op,
32677224Scth dp->dp_name, DDI_DEV_T_NONE, dp->dp_type,
32687224Scth 0, NULL, off, &off_p);
32690Sstevel@tonic-gate }
32707224Scth }
32717224Scth
32727224Scth /* Add dynamic parent properties to snapshot */
32737224Scth for (dp = i_ddi_prop_dyn_parent_get((dev_info_t *)dip);
32747224Scth dp && dp->dp_name; dp++) {
32757224Scth if (dp->dp_spec_type) {
32767224Scth /* if spec_type, property of matching minor */
32777224Scth ASSERT(DEVI_BUSY_OWNED(dip));
32787224Scth for (mn = dip->devi_minor; mn; mn = mn->next) {
32797224Scth if (mn->ddm_spec_type != dp->dp_spec_type)
32807224Scth continue;
32817224Scth off = di_getprop_add(list, 1, st, dip, prop_op,
32827224Scth dp->dp_name, mn->ddm_dev, dp->dp_type,
32837224Scth 0, NULL, off, &off_p);
32840Sstevel@tonic-gate }
32857224Scth } else {
32867224Scth /* property of devinfo node */
32877224Scth off = di_getprop_add(list, 1, st, dip, prop_op,
32887224Scth dp->dp_name, DDI_DEV_T_NONE, dp->dp_type,
32897224Scth 0, NULL, off, &off_p);
32900Sstevel@tonic-gate }
32917224Scth }
32927224Scth
32937224Scth *off_p = 0;
32940Sstevel@tonic-gate return (off);
32950Sstevel@tonic-gate }
32960Sstevel@tonic-gate
32970Sstevel@tonic-gate /*
32980Sstevel@tonic-gate * find private data format attached to a dip
32990Sstevel@tonic-gate * parent = 1 to match driver name of parent dip (for parent private data)
33000Sstevel@tonic-gate * 0 to match driver name of current dip (for driver private data)
33010Sstevel@tonic-gate */
33020Sstevel@tonic-gate #define DI_MATCH_DRIVER 0
33030Sstevel@tonic-gate #define DI_MATCH_PARENT 1
33040Sstevel@tonic-gate
33050Sstevel@tonic-gate struct di_priv_format *
di_match_drv_name(struct dev_info * node,struct di_state * st,int match)33060Sstevel@tonic-gate di_match_drv_name(struct dev_info *node, struct di_state *st, int match)
33070Sstevel@tonic-gate {
33087224Scth int i, count, len;
33097224Scth char *drv_name;
33107224Scth major_t major;
33117224Scth struct di_all *all;
33127224Scth struct di_priv_format *form;
33130Sstevel@tonic-gate
33140Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_match_drv_name: node = %s, match = %x\n",
33154870Sjg node->devi_node_name, match));
33160Sstevel@tonic-gate
33170Sstevel@tonic-gate if (match == DI_MATCH_PARENT) {
33180Sstevel@tonic-gate node = DEVI(node->devi_parent);
33190Sstevel@tonic-gate }
33200Sstevel@tonic-gate
33210Sstevel@tonic-gate if (node == NULL) {
33220Sstevel@tonic-gate return (NULL);
33230Sstevel@tonic-gate }
33240Sstevel@tonic-gate
33258459SJerry.Gilliam@Sun.COM major = node->devi_major;
33260Sstevel@tonic-gate if (major == (major_t)(-1)) {
33270Sstevel@tonic-gate return (NULL);
33280Sstevel@tonic-gate }
33290Sstevel@tonic-gate
33300Sstevel@tonic-gate /*
33310Sstevel@tonic-gate * Match the driver name.
33320Sstevel@tonic-gate */
33330Sstevel@tonic-gate drv_name = ddi_major_to_name(major);
33340Sstevel@tonic-gate if ((drv_name == NULL) || *drv_name == '\0') {
33350Sstevel@tonic-gate return (NULL);
33360Sstevel@tonic-gate }
33370Sstevel@tonic-gate
33380Sstevel@tonic-gate /* Now get the di_priv_format array */
33397224Scth all = DI_ALL_PTR(st);
33400Sstevel@tonic-gate if (match == DI_MATCH_PARENT) {
33410Sstevel@tonic-gate count = all->n_ppdata;
33427224Scth form = DI_PRIV_FORMAT(di_mem_addr(st, all->ppdata_format));
33430Sstevel@tonic-gate } else {
33440Sstevel@tonic-gate count = all->n_dpdata;
33457224Scth form = DI_PRIV_FORMAT(di_mem_addr(st, all->dpdata_format));
33460Sstevel@tonic-gate }
33470Sstevel@tonic-gate
33480Sstevel@tonic-gate len = strlen(drv_name);
33490Sstevel@tonic-gate for (i = 0; i < count; i++) {
33500Sstevel@tonic-gate char *tmp;
33510Sstevel@tonic-gate
33520Sstevel@tonic-gate tmp = form[i].drv_name;
33530Sstevel@tonic-gate while (tmp && (*tmp != '\0')) {
33540Sstevel@tonic-gate if (strncmp(drv_name, tmp, len) == 0) {
33550Sstevel@tonic-gate return (&form[i]);
33560Sstevel@tonic-gate }
33570Sstevel@tonic-gate /*
33580Sstevel@tonic-gate * Move to next driver name, skipping a white space
33590Sstevel@tonic-gate */
33600Sstevel@tonic-gate if (tmp = strchr(tmp, ' ')) {
33610Sstevel@tonic-gate tmp++;
33620Sstevel@tonic-gate }
33630Sstevel@tonic-gate }
33640Sstevel@tonic-gate }
33650Sstevel@tonic-gate
33660Sstevel@tonic-gate return (NULL);
33670Sstevel@tonic-gate }
33680Sstevel@tonic-gate
33690Sstevel@tonic-gate /*
33700Sstevel@tonic-gate * The following functions copy data as specified by the format passed in.
33710Sstevel@tonic-gate * To prevent invalid format from panicing the system, we call on_fault().
33720Sstevel@tonic-gate * A return value of 0 indicates an error. Otherwise, the total offset
33730Sstevel@tonic-gate * is returned.
33740Sstevel@tonic-gate */
33750Sstevel@tonic-gate #define DI_MAX_PRIVDATA (PAGESIZE >> 1) /* max private data size */
33760Sstevel@tonic-gate
33770Sstevel@tonic-gate static di_off_t
di_getprvdata(struct di_priv_format * pdp,struct dev_info * node,void * data,di_off_t * off_p,struct di_state * st)33782271Scth di_getprvdata(struct di_priv_format *pdp, struct dev_info *node,
33792271Scth void *data, di_off_t *off_p, struct di_state *st)
33800Sstevel@tonic-gate {
33817224Scth caddr_t pa;
33827224Scth void *ptr;
33837224Scth int i, size, repeat;
33847224Scth di_off_t off, off0, *tmp;
33857224Scth char *path;
33867224Scth label_t ljb;
33870Sstevel@tonic-gate
33880Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_getprvdata:\n"));
33890Sstevel@tonic-gate
33900Sstevel@tonic-gate /*
33910Sstevel@tonic-gate * check memory availability. Private data size is
33920Sstevel@tonic-gate * limited to DI_MAX_PRIVDATA.
33930Sstevel@tonic-gate */
33940Sstevel@tonic-gate off = di_checkmem(st, *off_p, DI_MAX_PRIVDATA);
33957224Scth *off_p = off;
33960Sstevel@tonic-gate
33973133Sjg if ((pdp->bytes == 0) || pdp->bytes > DI_MAX_PRIVDATA) {
33980Sstevel@tonic-gate goto failure;
33990Sstevel@tonic-gate }
34000Sstevel@tonic-gate
34010Sstevel@tonic-gate if (!on_fault(&ljb)) {
34020Sstevel@tonic-gate /* copy the struct */
34030Sstevel@tonic-gate bcopy(data, di_mem_addr(st, off), pdp->bytes);
34047224Scth off0 = DI_ALIGN(pdp->bytes); /* XXX remove DI_ALIGN */
34050Sstevel@tonic-gate
34060Sstevel@tonic-gate /* dereferencing pointers */
34070Sstevel@tonic-gate for (i = 0; i < MAX_PTR_IN_PRV; i++) {
34080Sstevel@tonic-gate
34090Sstevel@tonic-gate if (pdp->ptr[i].size == 0) {
34100Sstevel@tonic-gate goto success; /* no more ptrs */
34110Sstevel@tonic-gate }
34120Sstevel@tonic-gate
34130Sstevel@tonic-gate /*
34140Sstevel@tonic-gate * first, get the pointer content
34150Sstevel@tonic-gate */
34160Sstevel@tonic-gate if ((pdp->ptr[i].offset < 0) ||
34177224Scth (pdp->ptr[i].offset > pdp->bytes - sizeof (char *)))
34180Sstevel@tonic-gate goto failure; /* wrong offset */
34190Sstevel@tonic-gate
34200Sstevel@tonic-gate pa = di_mem_addr(st, off + pdp->ptr[i].offset);
34213133Sjg
34223133Sjg /* save a tmp ptr to store off_t later */
34233133Sjg tmp = (di_off_t *)(intptr_t)pa;
34243133Sjg
34253133Sjg /* get pointer value, if NULL continue */
34263133Sjg ptr = *((void **) (intptr_t)pa);
34273133Sjg if (ptr == NULL) {
34280Sstevel@tonic-gate continue;
34290Sstevel@tonic-gate }
34300Sstevel@tonic-gate
34310Sstevel@tonic-gate /*
34320Sstevel@tonic-gate * next, find the repeat count (array dimension)
34330Sstevel@tonic-gate */
34340Sstevel@tonic-gate repeat = pdp->ptr[i].len_offset;
34350Sstevel@tonic-gate
34360Sstevel@tonic-gate /*
34370Sstevel@tonic-gate * Positive value indicates a fixed sized array.
34380Sstevel@tonic-gate * 0 or negative value indicates variable sized array.
34390Sstevel@tonic-gate *
34400Sstevel@tonic-gate * For variable sized array, the variable must be
34410Sstevel@tonic-gate * an int member of the structure, with an offset
34420Sstevel@tonic-gate * equal to the absolution value of struct member.
34430Sstevel@tonic-gate */
34440Sstevel@tonic-gate if (repeat > pdp->bytes - sizeof (int)) {
34450Sstevel@tonic-gate goto failure; /* wrong offset */
34460Sstevel@tonic-gate }
34470Sstevel@tonic-gate
34480Sstevel@tonic-gate if (repeat >= 0) {
34493133Sjg repeat = *((int *)
34503133Sjg (intptr_t)((caddr_t)data + repeat));
34510Sstevel@tonic-gate } else {
34520Sstevel@tonic-gate repeat = -repeat;
34530Sstevel@tonic-gate }
34540Sstevel@tonic-gate
34550Sstevel@tonic-gate /*
34560Sstevel@tonic-gate * next, get the size of the object to be copied
34570Sstevel@tonic-gate */
34580Sstevel@tonic-gate size = pdp->ptr[i].size * repeat;
34590Sstevel@tonic-gate
34600Sstevel@tonic-gate /*
34610Sstevel@tonic-gate * Arbitrarily limit the total size of object to be
34620Sstevel@tonic-gate * copied (1 byte to 1/4 page).
34630Sstevel@tonic-gate */
34640Sstevel@tonic-gate if ((size <= 0) || (size > (DI_MAX_PRIVDATA - off0))) {
34650Sstevel@tonic-gate goto failure; /* wrong size or too big */
34660Sstevel@tonic-gate }
34670Sstevel@tonic-gate
34680Sstevel@tonic-gate /*
34690Sstevel@tonic-gate * Now copy the data
34700Sstevel@tonic-gate */
34710Sstevel@tonic-gate *tmp = off0;
34720Sstevel@tonic-gate bcopy(ptr, di_mem_addr(st, off + off0), size);
34737224Scth off0 += DI_ALIGN(size); /* XXX remove DI_ALIGN */
34740Sstevel@tonic-gate }
34750Sstevel@tonic-gate } else {
34760Sstevel@tonic-gate goto failure;
34770Sstevel@tonic-gate }
34780Sstevel@tonic-gate
34790Sstevel@tonic-gate success:
34800Sstevel@tonic-gate /*
34810Sstevel@tonic-gate * success if reached here
34820Sstevel@tonic-gate */
34830Sstevel@tonic-gate no_fault();
34840Sstevel@tonic-gate return (off + off0);
34850Sstevel@tonic-gate /*NOTREACHED*/
34860Sstevel@tonic-gate
34870Sstevel@tonic-gate failure:
34880Sstevel@tonic-gate /*
34890Sstevel@tonic-gate * fault occurred
34900Sstevel@tonic-gate */
34910Sstevel@tonic-gate no_fault();
34922271Scth path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
34932271Scth cmn_err(CE_WARN, "devinfo: fault on private data for '%s' at %p",
34942271Scth ddi_pathname((dev_info_t *)node, path), data);
34952271Scth kmem_free(path, MAXPATHLEN);
34960Sstevel@tonic-gate *off_p = -1; /* set private data to indicate error */
34970Sstevel@tonic-gate
34980Sstevel@tonic-gate return (off);
34990Sstevel@tonic-gate }
35000Sstevel@tonic-gate
35010Sstevel@tonic-gate /*
35020Sstevel@tonic-gate * get parent private data; on error, returns original offset
35030Sstevel@tonic-gate */
35040Sstevel@tonic-gate static di_off_t
di_getppdata(struct dev_info * node,di_off_t * off_p,struct di_state * st)35050Sstevel@tonic-gate di_getppdata(struct dev_info *node, di_off_t *off_p, struct di_state *st)
35060Sstevel@tonic-gate {
35077224Scth int off;
35087224Scth struct di_priv_format *ppdp;
35090Sstevel@tonic-gate
35100Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_getppdata:\n"));
35110Sstevel@tonic-gate
35120Sstevel@tonic-gate /* find the parent data format */
35130Sstevel@tonic-gate if ((ppdp = di_match_drv_name(node, st, DI_MATCH_PARENT)) == NULL) {
35140Sstevel@tonic-gate off = *off_p;
35150Sstevel@tonic-gate *off_p = 0; /* set parent data to none */
35160Sstevel@tonic-gate return (off);
35170Sstevel@tonic-gate }
35180Sstevel@tonic-gate
35192271Scth return (di_getprvdata(ppdp, node,
35202271Scth ddi_get_parent_data((dev_info_t *)node), off_p, st));
35210Sstevel@tonic-gate }
35220Sstevel@tonic-gate
35230Sstevel@tonic-gate /*
35240Sstevel@tonic-gate * get parent private data; returns original offset
35250Sstevel@tonic-gate */
35260Sstevel@tonic-gate static di_off_t
di_getdpdata(struct dev_info * node,di_off_t * off_p,struct di_state * st)35270Sstevel@tonic-gate di_getdpdata(struct dev_info *node, di_off_t *off_p, struct di_state *st)
35280Sstevel@tonic-gate {
35297224Scth int off;
35307224Scth struct di_priv_format *dpdp;
35310Sstevel@tonic-gate
35320Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_getdpdata:"));
35330Sstevel@tonic-gate
35340Sstevel@tonic-gate /* find the parent data format */
35350Sstevel@tonic-gate if ((dpdp = di_match_drv_name(node, st, DI_MATCH_DRIVER)) == NULL) {
35360Sstevel@tonic-gate off = *off_p;
35370Sstevel@tonic-gate *off_p = 0; /* set driver data to none */
35380Sstevel@tonic-gate return (off);
35390Sstevel@tonic-gate }
35400Sstevel@tonic-gate
35412271Scth return (di_getprvdata(dpdp, node,
35422271Scth ddi_get_driver_private((dev_info_t *)node), off_p, st));
35430Sstevel@tonic-gate }
35440Sstevel@tonic-gate
35450Sstevel@tonic-gate /*
354610923SEvan.Yan@Sun.COM * Copy hotplug data associated with a devinfo node into the snapshot.
354710923SEvan.Yan@Sun.COM */
354810923SEvan.Yan@Sun.COM static di_off_t
di_gethpdata(ddi_hp_cn_handle_t * hp_hdl,di_off_t * off_p,struct di_state * st)354910923SEvan.Yan@Sun.COM di_gethpdata(ddi_hp_cn_handle_t *hp_hdl, di_off_t *off_p,
355010923SEvan.Yan@Sun.COM struct di_state *st)
355110923SEvan.Yan@Sun.COM {
355210923SEvan.Yan@Sun.COM struct i_hp *hp;
355310923SEvan.Yan@Sun.COM struct di_hp *me;
355410923SEvan.Yan@Sun.COM size_t size;
355510923SEvan.Yan@Sun.COM di_off_t off;
355610923SEvan.Yan@Sun.COM
355710923SEvan.Yan@Sun.COM dcmn_err2((CE_CONT, "di_gethpdata:\n"));
355810923SEvan.Yan@Sun.COM
355910923SEvan.Yan@Sun.COM /*
356010923SEvan.Yan@Sun.COM * check memory first
356110923SEvan.Yan@Sun.COM */
356210923SEvan.Yan@Sun.COM off = di_checkmem(st, *off_p, sizeof (struct di_hp));
356310923SEvan.Yan@Sun.COM *off_p = off;
356410923SEvan.Yan@Sun.COM
356510923SEvan.Yan@Sun.COM do {
356610923SEvan.Yan@Sun.COM me = DI_HP(di_mem_addr(st, off));
356710923SEvan.Yan@Sun.COM me->self = off;
356810923SEvan.Yan@Sun.COM me->hp_name = 0;
356910923SEvan.Yan@Sun.COM me->hp_connection = (int)hp_hdl->cn_info.cn_num;
357010923SEvan.Yan@Sun.COM me->hp_depends_on = (int)hp_hdl->cn_info.cn_num_dpd_on;
357110923SEvan.Yan@Sun.COM (void) ddihp_cn_getstate(hp_hdl);
357210923SEvan.Yan@Sun.COM me->hp_state = (int)hp_hdl->cn_info.cn_state;
357310923SEvan.Yan@Sun.COM me->hp_type = (int)hp_hdl->cn_info.cn_type;
357410923SEvan.Yan@Sun.COM me->hp_type_str = 0;
357510923SEvan.Yan@Sun.COM me->hp_last_change = (uint32_t)hp_hdl->cn_info.cn_last_change;
357610923SEvan.Yan@Sun.COM me->hp_child = 0;
357710923SEvan.Yan@Sun.COM
357810923SEvan.Yan@Sun.COM /*
357910923SEvan.Yan@Sun.COM * Child links are resolved later by di_hotplug_children().
358010923SEvan.Yan@Sun.COM * Store a reference to this di_hp_t in the list used later
358110923SEvan.Yan@Sun.COM * by di_hotplug_children().
358210923SEvan.Yan@Sun.COM */
358310923SEvan.Yan@Sun.COM hp = kmem_zalloc(sizeof (i_hp_t), KM_SLEEP);
358410923SEvan.Yan@Sun.COM hp->hp_off = off;
358510923SEvan.Yan@Sun.COM hp->hp_child = hp_hdl->cn_info.cn_child;
358610923SEvan.Yan@Sun.COM list_insert_tail(&st->hp_list, hp);
358710923SEvan.Yan@Sun.COM
358810923SEvan.Yan@Sun.COM off += sizeof (struct di_hp);
358910923SEvan.Yan@Sun.COM
359010923SEvan.Yan@Sun.COM /* Add name of this di_hp_t to the snapshot */
359110923SEvan.Yan@Sun.COM if (hp_hdl->cn_info.cn_name) {
359210923SEvan.Yan@Sun.COM size = strlen(hp_hdl->cn_info.cn_name) + 1;
359310923SEvan.Yan@Sun.COM me->hp_name = off = di_checkmem(st, off, size);
359410923SEvan.Yan@Sun.COM (void) strcpy(di_mem_addr(st, off),
359510923SEvan.Yan@Sun.COM hp_hdl->cn_info.cn_name);
359610923SEvan.Yan@Sun.COM off += size;
359710923SEvan.Yan@Sun.COM }
359810923SEvan.Yan@Sun.COM
359910923SEvan.Yan@Sun.COM /* Add type description of this di_hp_t to the snapshot */
360010923SEvan.Yan@Sun.COM if (hp_hdl->cn_info.cn_type_str) {
360110923SEvan.Yan@Sun.COM size = strlen(hp_hdl->cn_info.cn_type_str) + 1;
360210923SEvan.Yan@Sun.COM me->hp_type_str = off = di_checkmem(st, off, size);
360310923SEvan.Yan@Sun.COM (void) strcpy(di_mem_addr(st, off),
360410923SEvan.Yan@Sun.COM hp_hdl->cn_info.cn_type_str);
360510923SEvan.Yan@Sun.COM off += size;
360610923SEvan.Yan@Sun.COM }
360710923SEvan.Yan@Sun.COM
360810923SEvan.Yan@Sun.COM /*
360910923SEvan.Yan@Sun.COM * Set link to next in the chain of di_hp_t nodes,
361010923SEvan.Yan@Sun.COM * or terminate the chain when processing the last node.
361110923SEvan.Yan@Sun.COM */
361210923SEvan.Yan@Sun.COM if (hp_hdl->next != NULL) {
361310923SEvan.Yan@Sun.COM off = di_checkmem(st, off, sizeof (struct di_hp));
361410923SEvan.Yan@Sun.COM me->next = off;
361510923SEvan.Yan@Sun.COM } else {
361610923SEvan.Yan@Sun.COM me->next = 0;
361710923SEvan.Yan@Sun.COM }
361810923SEvan.Yan@Sun.COM
361910923SEvan.Yan@Sun.COM /* Update pointer to next in the chain */
362010923SEvan.Yan@Sun.COM hp_hdl = hp_hdl->next;
362110923SEvan.Yan@Sun.COM
362210923SEvan.Yan@Sun.COM } while (hp_hdl);
362310923SEvan.Yan@Sun.COM
362410923SEvan.Yan@Sun.COM return (off);
362510923SEvan.Yan@Sun.COM }
362610923SEvan.Yan@Sun.COM
362710923SEvan.Yan@Sun.COM /*
36280Sstevel@tonic-gate * The driver is stateful across DINFOCPYALL and DINFOUSRLD.
36290Sstevel@tonic-gate * This function encapsulates the state machine:
36300Sstevel@tonic-gate *
36310Sstevel@tonic-gate * -> IOC_IDLE -> IOC_SNAP -> IOC_DONE -> IOC_COPY ->
36320Sstevel@tonic-gate * | SNAPSHOT USRLD |
36330Sstevel@tonic-gate * --------------------------------------------------
36340Sstevel@tonic-gate *
36350Sstevel@tonic-gate * Returns 0 on success and -1 on failure
36360Sstevel@tonic-gate */
36370Sstevel@tonic-gate static int
di_setstate(struct di_state * st,int new_state)36380Sstevel@tonic-gate di_setstate(struct di_state *st, int new_state)
36390Sstevel@tonic-gate {
36407224Scth int ret = 0;
36410Sstevel@tonic-gate
36420Sstevel@tonic-gate mutex_enter(&di_lock);
36430Sstevel@tonic-gate switch (new_state) {
36440Sstevel@tonic-gate case IOC_IDLE:
36450Sstevel@tonic-gate case IOC_DONE:
36460Sstevel@tonic-gate break;
36470Sstevel@tonic-gate case IOC_SNAP:
36480Sstevel@tonic-gate if (st->di_iocstate != IOC_IDLE)
36490Sstevel@tonic-gate ret = -1;
36500Sstevel@tonic-gate break;
36510Sstevel@tonic-gate case IOC_COPY:
36520Sstevel@tonic-gate if (st->di_iocstate != IOC_DONE)
36530Sstevel@tonic-gate ret = -1;
36540Sstevel@tonic-gate break;
36550Sstevel@tonic-gate default:
36560Sstevel@tonic-gate ret = -1;
36570Sstevel@tonic-gate }
36580Sstevel@tonic-gate
36590Sstevel@tonic-gate if (ret == 0)
36600Sstevel@tonic-gate st->di_iocstate = new_state;
36610Sstevel@tonic-gate else
36620Sstevel@tonic-gate cmn_err(CE_NOTE, "incorrect state transition from %d to %d",
36630Sstevel@tonic-gate st->di_iocstate, new_state);
36640Sstevel@tonic-gate mutex_exit(&di_lock);
36650Sstevel@tonic-gate return (ret);
36660Sstevel@tonic-gate }
36670Sstevel@tonic-gate
36680Sstevel@tonic-gate /*
36690Sstevel@tonic-gate * We cannot assume the presence of the entire
36700Sstevel@tonic-gate * snapshot in this routine. All we are guaranteed
36710Sstevel@tonic-gate * is the di_all struct + 1 byte (for root_path)
36720Sstevel@tonic-gate */
36730Sstevel@tonic-gate static int
header_plus_one_ok(struct di_all * all)36740Sstevel@tonic-gate header_plus_one_ok(struct di_all *all)
36750Sstevel@tonic-gate {
36760Sstevel@tonic-gate /*
36770Sstevel@tonic-gate * Refuse to read old versions
36780Sstevel@tonic-gate */
36790Sstevel@tonic-gate if (all->version != DI_SNAPSHOT_VERSION) {
36800Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "bad version: 0x%x", all->version));
36810Sstevel@tonic-gate return (0);
36820Sstevel@tonic-gate }
36830Sstevel@tonic-gate
36840Sstevel@tonic-gate if (all->cache_magic != DI_CACHE_MAGIC) {
36850Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "bad magic #: 0x%x", all->cache_magic));
36860Sstevel@tonic-gate return (0);
36870Sstevel@tonic-gate }
36880Sstevel@tonic-gate
36893133Sjg if (all->snapshot_time == 0) {
36900Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "bad timestamp: %ld", all->snapshot_time));
36910Sstevel@tonic-gate return (0);
36920Sstevel@tonic-gate }
36930Sstevel@tonic-gate
36940Sstevel@tonic-gate if (all->top_devinfo == 0) {
36950Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "NULL top devinfo"));
36960Sstevel@tonic-gate return (0);
36970Sstevel@tonic-gate }
36980Sstevel@tonic-gate
36990Sstevel@tonic-gate if (all->map_size < sizeof (*all) + 1) {
37000Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "bad map size: %u", all->map_size));
37010Sstevel@tonic-gate return (0);
37020Sstevel@tonic-gate }
37030Sstevel@tonic-gate
37040Sstevel@tonic-gate if (all->root_path[0] != '/' || all->root_path[1] != '\0') {
37050Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "bad rootpath: %c%c",
37060Sstevel@tonic-gate all->root_path[0], all->root_path[1]));
37070Sstevel@tonic-gate return (0);
37080Sstevel@tonic-gate }
37090Sstevel@tonic-gate
37100Sstevel@tonic-gate /*
37110Sstevel@tonic-gate * We can't check checksum here as we just have the header
37120Sstevel@tonic-gate */
37130Sstevel@tonic-gate
37140Sstevel@tonic-gate return (1);
37150Sstevel@tonic-gate }
37160Sstevel@tonic-gate
37170Sstevel@tonic-gate static int
chunk_write(struct vnode * vp,offset_t off,caddr_t buf,size_t len)37180Sstevel@tonic-gate chunk_write(struct vnode *vp, offset_t off, caddr_t buf, size_t len)
37190Sstevel@tonic-gate {
37200Sstevel@tonic-gate rlim64_t rlimit;
37210Sstevel@tonic-gate ssize_t resid;
37220Sstevel@tonic-gate int error = 0;
37230Sstevel@tonic-gate
37240Sstevel@tonic-gate
37250Sstevel@tonic-gate rlimit = RLIM64_INFINITY;
37260Sstevel@tonic-gate
37270Sstevel@tonic-gate while (len) {
37280Sstevel@tonic-gate resid = 0;
37290Sstevel@tonic-gate error = vn_rdwr(UIO_WRITE, vp, buf, len, off,
37300Sstevel@tonic-gate UIO_SYSSPACE, FSYNC, rlimit, kcred, &resid);
37310Sstevel@tonic-gate
37320Sstevel@tonic-gate if (error || resid < 0) {
37330Sstevel@tonic-gate error = error ? error : EIO;
37340Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "write error: %d", error));
37350Sstevel@tonic-gate break;
37360Sstevel@tonic-gate }
37370Sstevel@tonic-gate
37380Sstevel@tonic-gate /*
37390Sstevel@tonic-gate * Check if we are making progress
37400Sstevel@tonic-gate */
37410Sstevel@tonic-gate if (resid >= len) {
37420Sstevel@tonic-gate error = ENOSPC;
37430Sstevel@tonic-gate break;
37440Sstevel@tonic-gate }
37450Sstevel@tonic-gate buf += len - resid;
37460Sstevel@tonic-gate off += len - resid;
37470Sstevel@tonic-gate len = resid;
37480Sstevel@tonic-gate }
37490Sstevel@tonic-gate
37500Sstevel@tonic-gate return (error);
37510Sstevel@tonic-gate }
37520Sstevel@tonic-gate
37530Sstevel@tonic-gate static void
di_cache_write(struct di_cache * cache)37540Sstevel@tonic-gate di_cache_write(struct di_cache *cache)
37550Sstevel@tonic-gate {
37560Sstevel@tonic-gate struct di_all *all;
37570Sstevel@tonic-gate struct vnode *vp;
37580Sstevel@tonic-gate int oflags;
37590Sstevel@tonic-gate size_t map_size;
37600Sstevel@tonic-gate size_t chunk;
37610Sstevel@tonic-gate offset_t off;
37620Sstevel@tonic-gate int error;
37630Sstevel@tonic-gate char *buf;
37640Sstevel@tonic-gate
37650Sstevel@tonic-gate ASSERT(DI_CACHE_LOCKED(*cache));
37660Sstevel@tonic-gate ASSERT(!servicing_interrupt());
37670Sstevel@tonic-gate
37680Sstevel@tonic-gate if (cache->cache_size == 0) {
37690Sstevel@tonic-gate ASSERT(cache->cache_data == NULL);
37700Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "Empty cache. Skipping write"));
37710Sstevel@tonic-gate return;
37720Sstevel@tonic-gate }
37730Sstevel@tonic-gate
37740Sstevel@tonic-gate ASSERT(cache->cache_size > 0);
37750Sstevel@tonic-gate ASSERT(cache->cache_data);
37760Sstevel@tonic-gate
37770Sstevel@tonic-gate if (!modrootloaded || rootvp == NULL || vn_is_readonly(rootvp)) {
37780Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "Can't write to rootFS. Skipping write"));
37790Sstevel@tonic-gate return;
37800Sstevel@tonic-gate }
37810Sstevel@tonic-gate
37820Sstevel@tonic-gate all = (struct di_all *)cache->cache_data;
37830Sstevel@tonic-gate
37840Sstevel@tonic-gate if (!header_plus_one_ok(all)) {
37850Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "Invalid header. Skipping write"));
37860Sstevel@tonic-gate return;
37870Sstevel@tonic-gate }
37880Sstevel@tonic-gate
37890Sstevel@tonic-gate ASSERT(strcmp(all->root_path, "/") == 0);
37900Sstevel@tonic-gate
37910Sstevel@tonic-gate /*
37920Sstevel@tonic-gate * The cache_size is the total allocated memory for the cache.
37930Sstevel@tonic-gate * The map_size is the actual size of valid data in the cache.
37940Sstevel@tonic-gate * map_size may be smaller than cache_size but cannot exceed
37950Sstevel@tonic-gate * cache_size.
37960Sstevel@tonic-gate */
37970Sstevel@tonic-gate if (all->map_size > cache->cache_size) {
37980Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "map_size (0x%x) > cache_size (0x%x)."
37990Sstevel@tonic-gate " Skipping write", all->map_size, cache->cache_size));
38000Sstevel@tonic-gate return;
38010Sstevel@tonic-gate }
38020Sstevel@tonic-gate
38030Sstevel@tonic-gate /*
38040Sstevel@tonic-gate * First unlink the temp file
38050Sstevel@tonic-gate */
38060Sstevel@tonic-gate error = vn_remove(DI_CACHE_TEMP, UIO_SYSSPACE, RMFILE);
38070Sstevel@tonic-gate if (error && error != ENOENT) {
38080Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "%s: unlink failed: %d",
38090Sstevel@tonic-gate DI_CACHE_TEMP, error));
38100Sstevel@tonic-gate }
38110Sstevel@tonic-gate
38120Sstevel@tonic-gate if (error == EROFS) {
38130Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "RDONLY FS. Skipping write"));
38140Sstevel@tonic-gate return;
38150Sstevel@tonic-gate }
38160Sstevel@tonic-gate
38170Sstevel@tonic-gate vp = NULL;
38180Sstevel@tonic-gate oflags = (FCREAT|FWRITE);
38190Sstevel@tonic-gate if (error = vn_open(DI_CACHE_TEMP, UIO_SYSSPACE, oflags,
38200Sstevel@tonic-gate DI_CACHE_PERMS, &vp, CRCREAT, 0)) {
38210Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "%s: create failed: %d",
38220Sstevel@tonic-gate DI_CACHE_TEMP, error));
38230Sstevel@tonic-gate return;
38240Sstevel@tonic-gate }
38250Sstevel@tonic-gate
38260Sstevel@tonic-gate ASSERT(vp);
38270Sstevel@tonic-gate
38280Sstevel@tonic-gate /*
38290Sstevel@tonic-gate * Paranoid: Check if the file is on a read-only FS
38300Sstevel@tonic-gate */
38310Sstevel@tonic-gate if (vn_is_readonly(vp)) {
38320Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "cannot write: readonly FS"));
38330Sstevel@tonic-gate goto fail;
38340Sstevel@tonic-gate }
38350Sstevel@tonic-gate
38360Sstevel@tonic-gate /*
38370Sstevel@tonic-gate * Note that we only write map_size bytes to disk - this saves
38380Sstevel@tonic-gate * space as the actual cache size may be larger than size of
38390Sstevel@tonic-gate * valid data in the cache.
38400Sstevel@tonic-gate * Another advantage is that it makes verification of size
38410Sstevel@tonic-gate * easier when the file is read later.
38420Sstevel@tonic-gate */
38430Sstevel@tonic-gate map_size = all->map_size;
38440Sstevel@tonic-gate off = 0;
38450Sstevel@tonic-gate buf = cache->cache_data;
38460Sstevel@tonic-gate
38470Sstevel@tonic-gate while (map_size) {
38480Sstevel@tonic-gate ASSERT(map_size > 0);
38490Sstevel@tonic-gate /*
38500Sstevel@tonic-gate * Write in chunks so that VM system
38510Sstevel@tonic-gate * is not overwhelmed
38520Sstevel@tonic-gate */
38530Sstevel@tonic-gate if (map_size > di_chunk * PAGESIZE)
38540Sstevel@tonic-gate chunk = di_chunk * PAGESIZE;
38550Sstevel@tonic-gate else
38560Sstevel@tonic-gate chunk = map_size;
38570Sstevel@tonic-gate
38580Sstevel@tonic-gate error = chunk_write(vp, off, buf, chunk);
38590Sstevel@tonic-gate if (error) {
38600Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "write failed: off=0x%x: %d",
38610Sstevel@tonic-gate off, error));
38620Sstevel@tonic-gate goto fail;
38630Sstevel@tonic-gate }
38640Sstevel@tonic-gate
38650Sstevel@tonic-gate off += chunk;
38660Sstevel@tonic-gate buf += chunk;
38670Sstevel@tonic-gate map_size -= chunk;
38680Sstevel@tonic-gate
38697224Scth /* If low on memory, give pageout a chance to run */
38707224Scth if (freemem < desfree)
38717224Scth delay(1);
38720Sstevel@tonic-gate }
38730Sstevel@tonic-gate
38740Sstevel@tonic-gate /*
38750Sstevel@tonic-gate * Now sync the file and close it
38760Sstevel@tonic-gate */
38775331Samw if (error = VOP_FSYNC(vp, FSYNC, kcred, NULL)) {
38780Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "FSYNC failed: %d", error));
38790Sstevel@tonic-gate }
38800Sstevel@tonic-gate
38815331Samw if (error = VOP_CLOSE(vp, oflags, 1, (offset_t)0, kcred, NULL)) {
38820Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "close() failed: %d", error));
38830Sstevel@tonic-gate VN_RELE(vp);
38840Sstevel@tonic-gate return;
38850Sstevel@tonic-gate }
38860Sstevel@tonic-gate
38870Sstevel@tonic-gate VN_RELE(vp);
38880Sstevel@tonic-gate
38890Sstevel@tonic-gate /*
38900Sstevel@tonic-gate * Now do the rename
38910Sstevel@tonic-gate */
38920Sstevel@tonic-gate if (error = vn_rename(DI_CACHE_TEMP, DI_CACHE_FILE, UIO_SYSSPACE)) {
38930Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "rename failed: %d", error));
38940Sstevel@tonic-gate return;
38950Sstevel@tonic-gate }
38960Sstevel@tonic-gate
38970Sstevel@tonic-gate CACHE_DEBUG((DI_INFO, "Cache write successful."));
38980Sstevel@tonic-gate
38990Sstevel@tonic-gate return;
39000Sstevel@tonic-gate
39010Sstevel@tonic-gate fail:
39025331Samw (void) VOP_CLOSE(vp, oflags, 1, (offset_t)0, kcred, NULL);
39030Sstevel@tonic-gate VN_RELE(vp);
39040Sstevel@tonic-gate }
39050Sstevel@tonic-gate
39060Sstevel@tonic-gate
39070Sstevel@tonic-gate /*
39080Sstevel@tonic-gate * Since we could be called early in boot,
39090Sstevel@tonic-gate * use kobj_read_file()
39100Sstevel@tonic-gate */
39110Sstevel@tonic-gate static void
di_cache_read(struct di_cache * cache)39120Sstevel@tonic-gate di_cache_read(struct di_cache *cache)
39130Sstevel@tonic-gate {
39140Sstevel@tonic-gate struct _buf *file;
39150Sstevel@tonic-gate struct di_all *all;
39160Sstevel@tonic-gate int n;
39170Sstevel@tonic-gate size_t map_size, sz, chunk;
39180Sstevel@tonic-gate offset_t off;
39190Sstevel@tonic-gate caddr_t buf;
39200Sstevel@tonic-gate uint32_t saved_crc, crc;
39210Sstevel@tonic-gate
39220Sstevel@tonic-gate ASSERT(modrootloaded);
39230Sstevel@tonic-gate ASSERT(DI_CACHE_LOCKED(*cache));
39240Sstevel@tonic-gate ASSERT(cache->cache_data == NULL);
39250Sstevel@tonic-gate ASSERT(cache->cache_size == 0);
39260Sstevel@tonic-gate ASSERT(!servicing_interrupt());
39270Sstevel@tonic-gate
39280Sstevel@tonic-gate file = kobj_open_file(DI_CACHE_FILE);
39290Sstevel@tonic-gate if (file == (struct _buf *)-1) {
39300Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "%s: open failed: %d",
39310Sstevel@tonic-gate DI_CACHE_FILE, ENOENT));
39320Sstevel@tonic-gate return;
39330Sstevel@tonic-gate }
39340Sstevel@tonic-gate
39350Sstevel@tonic-gate /*
39360Sstevel@tonic-gate * Read in the header+root_path first. The root_path must be "/"
39370Sstevel@tonic-gate */
39380Sstevel@tonic-gate all = kmem_zalloc(sizeof (*all) + 1, KM_SLEEP);
39390Sstevel@tonic-gate n = kobj_read_file(file, (caddr_t)all, sizeof (*all) + 1, 0);
39400Sstevel@tonic-gate
39410Sstevel@tonic-gate if ((n != sizeof (*all) + 1) || !header_plus_one_ok(all)) {
39420Sstevel@tonic-gate kmem_free(all, sizeof (*all) + 1);
39430Sstevel@tonic-gate kobj_close_file(file);
39440Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "cache header: read error or invalid"));
39450Sstevel@tonic-gate return;
39460Sstevel@tonic-gate }
39470Sstevel@tonic-gate
39480Sstevel@tonic-gate map_size = all->map_size;
39490Sstevel@tonic-gate
39500Sstevel@tonic-gate kmem_free(all, sizeof (*all) + 1);
39510Sstevel@tonic-gate
39520Sstevel@tonic-gate ASSERT(map_size >= sizeof (*all) + 1);
39530Sstevel@tonic-gate
39540Sstevel@tonic-gate buf = di_cache.cache_data = kmem_alloc(map_size, KM_SLEEP);
39550Sstevel@tonic-gate sz = map_size;
39560Sstevel@tonic-gate off = 0;
39570Sstevel@tonic-gate while (sz) {
39580Sstevel@tonic-gate /* Don't overload VM with large reads */
39590Sstevel@tonic-gate chunk = (sz > di_chunk * PAGESIZE) ? di_chunk * PAGESIZE : sz;
39600Sstevel@tonic-gate n = kobj_read_file(file, buf, chunk, off);
39610Sstevel@tonic-gate if (n != chunk) {
39620Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "%s: read error at offset: %lld",
39630Sstevel@tonic-gate DI_CACHE_FILE, off));
39640Sstevel@tonic-gate goto fail;
39650Sstevel@tonic-gate }
39660Sstevel@tonic-gate off += chunk;
39670Sstevel@tonic-gate buf += chunk;
39680Sstevel@tonic-gate sz -= chunk;
39690Sstevel@tonic-gate }
39700Sstevel@tonic-gate
39710Sstevel@tonic-gate ASSERT(off == map_size);
39720Sstevel@tonic-gate
39730Sstevel@tonic-gate /*
39740Sstevel@tonic-gate * Read past expected EOF to verify size.
39750Sstevel@tonic-gate */
39760Sstevel@tonic-gate if (kobj_read_file(file, (caddr_t)&sz, 1, off) > 0) {
39770Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "%s: file size changed", DI_CACHE_FILE));
39780Sstevel@tonic-gate goto fail;
39790Sstevel@tonic-gate }
39800Sstevel@tonic-gate
39810Sstevel@tonic-gate all = (struct di_all *)di_cache.cache_data;
39820Sstevel@tonic-gate if (!header_plus_one_ok(all)) {
39830Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "%s: file header changed", DI_CACHE_FILE));
39840Sstevel@tonic-gate goto fail;
39850Sstevel@tonic-gate }
39860Sstevel@tonic-gate
39870Sstevel@tonic-gate /*
39880Sstevel@tonic-gate * Compute CRC with checksum field in the cache data set to 0
39890Sstevel@tonic-gate */
39900Sstevel@tonic-gate saved_crc = all->cache_checksum;
39910Sstevel@tonic-gate all->cache_checksum = 0;
39920Sstevel@tonic-gate CRC32(crc, di_cache.cache_data, map_size, -1U, crc32_table);
39930Sstevel@tonic-gate all->cache_checksum = saved_crc;
39940Sstevel@tonic-gate
39950Sstevel@tonic-gate if (crc != all->cache_checksum) {
39960Sstevel@tonic-gate CACHE_DEBUG((DI_ERR,
39970Sstevel@tonic-gate "%s: checksum error: expected=0x%x actual=0x%x",
39980Sstevel@tonic-gate DI_CACHE_FILE, all->cache_checksum, crc));
39990Sstevel@tonic-gate goto fail;
40000Sstevel@tonic-gate }
40010Sstevel@tonic-gate
40020Sstevel@tonic-gate if (all->map_size != map_size) {
40030Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "%s: map size changed", DI_CACHE_FILE));
40040Sstevel@tonic-gate goto fail;
40050Sstevel@tonic-gate }
40060Sstevel@tonic-gate
40070Sstevel@tonic-gate kobj_close_file(file);
40080Sstevel@tonic-gate
40090Sstevel@tonic-gate di_cache.cache_size = map_size;
40100Sstevel@tonic-gate
40110Sstevel@tonic-gate return;
40120Sstevel@tonic-gate
40130Sstevel@tonic-gate fail:
40140Sstevel@tonic-gate kmem_free(di_cache.cache_data, map_size);
40150Sstevel@tonic-gate kobj_close_file(file);
40160Sstevel@tonic-gate di_cache.cache_data = NULL;
40170Sstevel@tonic-gate di_cache.cache_size = 0;
40180Sstevel@tonic-gate }
40190Sstevel@tonic-gate
40200Sstevel@tonic-gate
40210Sstevel@tonic-gate /*
40220Sstevel@tonic-gate * Checks if arguments are valid for using the cache.
40230Sstevel@tonic-gate */
40240Sstevel@tonic-gate static int
cache_args_valid(struct di_state * st,int * error)40250Sstevel@tonic-gate cache_args_valid(struct di_state *st, int *error)
40260Sstevel@tonic-gate {
40270Sstevel@tonic-gate ASSERT(error);
40280Sstevel@tonic-gate ASSERT(st->mem_size > 0);
40290Sstevel@tonic-gate ASSERT(st->memlist != NULL);
40300Sstevel@tonic-gate
40310Sstevel@tonic-gate if (!modrootloaded || !i_ddi_io_initialized()) {
40320Sstevel@tonic-gate CACHE_DEBUG((DI_ERR,
40330Sstevel@tonic-gate "cache lookup failure: I/O subsystem not inited"));
40340Sstevel@tonic-gate *error = ENOTACTIVE;
40350Sstevel@tonic-gate return (0);
40360Sstevel@tonic-gate }
40370Sstevel@tonic-gate
40380Sstevel@tonic-gate /*
40390Sstevel@tonic-gate * No other flags allowed with DINFOCACHE
40400Sstevel@tonic-gate */
40410Sstevel@tonic-gate if (st->command != (DINFOCACHE & DIIOC_MASK)) {
40420Sstevel@tonic-gate CACHE_DEBUG((DI_ERR,
40430Sstevel@tonic-gate "cache lookup failure: bad flags: 0x%x",
40440Sstevel@tonic-gate st->command));
40450Sstevel@tonic-gate *error = EINVAL;
40460Sstevel@tonic-gate return (0);
40470Sstevel@tonic-gate }
40480Sstevel@tonic-gate
40490Sstevel@tonic-gate if (strcmp(DI_ALL_PTR(st)->root_path, "/") != 0) {
40500Sstevel@tonic-gate CACHE_DEBUG((DI_ERR,
40510Sstevel@tonic-gate "cache lookup failure: bad root: %s",
40520Sstevel@tonic-gate DI_ALL_PTR(st)->root_path));
40530Sstevel@tonic-gate *error = EINVAL;
40540Sstevel@tonic-gate return (0);
40550Sstevel@tonic-gate }
40560Sstevel@tonic-gate
40570Sstevel@tonic-gate CACHE_DEBUG((DI_INFO, "cache lookup args ok: 0x%x", st->command));
40580Sstevel@tonic-gate
40590Sstevel@tonic-gate *error = 0;
40600Sstevel@tonic-gate
40610Sstevel@tonic-gate return (1);
40620Sstevel@tonic-gate }
40630Sstevel@tonic-gate
40640Sstevel@tonic-gate static int
snapshot_is_cacheable(struct di_state * st)40650Sstevel@tonic-gate snapshot_is_cacheable(struct di_state *st)
40660Sstevel@tonic-gate {
40670Sstevel@tonic-gate ASSERT(st->mem_size > 0);
40680Sstevel@tonic-gate ASSERT(st->memlist != NULL);
40690Sstevel@tonic-gate
4070878Sramat if ((st->command & DI_CACHE_SNAPSHOT_FLAGS) !=
4071878Sramat (DI_CACHE_SNAPSHOT_FLAGS & DIIOC_MASK)) {
40720Sstevel@tonic-gate CACHE_DEBUG((DI_INFO,
40730Sstevel@tonic-gate "not cacheable: incompatible flags: 0x%x",
40740Sstevel@tonic-gate st->command));
40750Sstevel@tonic-gate return (0);
40760Sstevel@tonic-gate }
40770Sstevel@tonic-gate
40780Sstevel@tonic-gate if (strcmp(DI_ALL_PTR(st)->root_path, "/") != 0) {
40790Sstevel@tonic-gate CACHE_DEBUG((DI_INFO,
40800Sstevel@tonic-gate "not cacheable: incompatible root path: %s",
40810Sstevel@tonic-gate DI_ALL_PTR(st)->root_path));
40820Sstevel@tonic-gate return (0);
40830Sstevel@tonic-gate }
40840Sstevel@tonic-gate
40850Sstevel@tonic-gate CACHE_DEBUG((DI_INFO, "cacheable snapshot request: 0x%x", st->command));
40860Sstevel@tonic-gate
40870Sstevel@tonic-gate return (1);
40880Sstevel@tonic-gate }
40890Sstevel@tonic-gate
40900Sstevel@tonic-gate static int
di_cache_lookup(struct di_state * st)40910Sstevel@tonic-gate di_cache_lookup(struct di_state *st)
40920Sstevel@tonic-gate {
40930Sstevel@tonic-gate size_t rval;
40940Sstevel@tonic-gate int cache_valid;
40950Sstevel@tonic-gate
40960Sstevel@tonic-gate ASSERT(cache_args_valid(st, &cache_valid));
40970Sstevel@tonic-gate ASSERT(modrootloaded);
40980Sstevel@tonic-gate
40990Sstevel@tonic-gate DI_CACHE_LOCK(di_cache);
41000Sstevel@tonic-gate
41010Sstevel@tonic-gate /*
41020Sstevel@tonic-gate * The following assignment determines the validity
41030Sstevel@tonic-gate * of the cache as far as this snapshot is concerned.
41040Sstevel@tonic-gate */
41050Sstevel@tonic-gate cache_valid = di_cache.cache_valid;
41060Sstevel@tonic-gate
41070Sstevel@tonic-gate if (cache_valid && di_cache.cache_data == NULL) {
41080Sstevel@tonic-gate di_cache_read(&di_cache);
41090Sstevel@tonic-gate /* check for read or file error */
41100Sstevel@tonic-gate if (di_cache.cache_data == NULL)
41110Sstevel@tonic-gate cache_valid = 0;
41120Sstevel@tonic-gate }
41130Sstevel@tonic-gate
41140Sstevel@tonic-gate if (cache_valid) {
41150Sstevel@tonic-gate /*
41160Sstevel@tonic-gate * Ok, the cache was valid as of this particular
41170Sstevel@tonic-gate * snapshot. Copy the cached snapshot. This is safe
41180Sstevel@tonic-gate * to do as the cache cannot be freed (we hold the
41190Sstevel@tonic-gate * cache lock). Free the memory allocated in di_state
41200Sstevel@tonic-gate * up until this point - we will simply copy everything
41210Sstevel@tonic-gate * in the cache.
41220Sstevel@tonic-gate */
41230Sstevel@tonic-gate
41240Sstevel@tonic-gate ASSERT(di_cache.cache_data != NULL);
41250Sstevel@tonic-gate ASSERT(di_cache.cache_size > 0);
41260Sstevel@tonic-gate
41270Sstevel@tonic-gate di_freemem(st);
41280Sstevel@tonic-gate
41290Sstevel@tonic-gate rval = 0;
41300Sstevel@tonic-gate if (di_cache2mem(&di_cache, st) > 0) {
41310Sstevel@tonic-gate /*
41320Sstevel@tonic-gate * map_size is size of valid data in the
41330Sstevel@tonic-gate * cached snapshot and may be less than
41340Sstevel@tonic-gate * size of the cache.
41350Sstevel@tonic-gate */
41367224Scth ASSERT(DI_ALL_PTR(st));
41370Sstevel@tonic-gate rval = DI_ALL_PTR(st)->map_size;
41380Sstevel@tonic-gate
41390Sstevel@tonic-gate ASSERT(rval >= sizeof (struct di_all));
41400Sstevel@tonic-gate ASSERT(rval <= di_cache.cache_size);
41410Sstevel@tonic-gate }
41420Sstevel@tonic-gate } else {
41430Sstevel@tonic-gate /*
41440Sstevel@tonic-gate * The cache isn't valid, we need to take a snapshot.
41450Sstevel@tonic-gate * Set the command flags appropriately
41460Sstevel@tonic-gate */
41470Sstevel@tonic-gate ASSERT(st->command == (DINFOCACHE & DIIOC_MASK));
41480Sstevel@tonic-gate st->command = (DI_CACHE_SNAPSHOT_FLAGS & DIIOC_MASK);
41490Sstevel@tonic-gate rval = di_cache_update(st);
41500Sstevel@tonic-gate st->command = (DINFOCACHE & DIIOC_MASK);
41510Sstevel@tonic-gate }
41520Sstevel@tonic-gate
41530Sstevel@tonic-gate DI_CACHE_UNLOCK(di_cache);
41540Sstevel@tonic-gate
41550Sstevel@tonic-gate /*
41560Sstevel@tonic-gate * For cached snapshots, the devinfo driver always returns
41570Sstevel@tonic-gate * a snapshot rooted at "/".
41580Sstevel@tonic-gate */
41590Sstevel@tonic-gate ASSERT(rval == 0 || strcmp(DI_ALL_PTR(st)->root_path, "/") == 0);
41600Sstevel@tonic-gate
41613133Sjg return ((int)rval);
41620Sstevel@tonic-gate }
41630Sstevel@tonic-gate
41640Sstevel@tonic-gate /*
41650Sstevel@tonic-gate * This is a forced update of the cache - the previous state of the cache
41660Sstevel@tonic-gate * may be:
41670Sstevel@tonic-gate * - unpopulated
41680Sstevel@tonic-gate * - populated and invalid
41690Sstevel@tonic-gate * - populated and valid
41700Sstevel@tonic-gate */
41710Sstevel@tonic-gate static int
di_cache_update(struct di_state * st)41720Sstevel@tonic-gate di_cache_update(struct di_state *st)
41730Sstevel@tonic-gate {
41747224Scth int rval;
41757224Scth uint32_t crc;
41767224Scth struct di_all *all;
41770Sstevel@tonic-gate
41780Sstevel@tonic-gate ASSERT(DI_CACHE_LOCKED(di_cache));
41790Sstevel@tonic-gate ASSERT(snapshot_is_cacheable(st));
41800Sstevel@tonic-gate
41810Sstevel@tonic-gate /*
41820Sstevel@tonic-gate * Free the in-core cache and the on-disk file (if they exist)
41830Sstevel@tonic-gate */
41840Sstevel@tonic-gate i_ddi_di_cache_free(&di_cache);
41850Sstevel@tonic-gate
41860Sstevel@tonic-gate /*
41870Sstevel@tonic-gate * Set valid flag before taking the snapshot,
41880Sstevel@tonic-gate * so that any invalidations that arrive
41890Sstevel@tonic-gate * during or after the snapshot are not
41900Sstevel@tonic-gate * removed by us.
41910Sstevel@tonic-gate */
41920Sstevel@tonic-gate atomic_or_32(&di_cache.cache_valid, 1);
41930Sstevel@tonic-gate
4194878Sramat rval = di_snapshot_and_clean(st);
41950Sstevel@tonic-gate
41960Sstevel@tonic-gate if (rval == 0) {
41970Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "can't update cache: bad snapshot"));
41980Sstevel@tonic-gate return (0);
41990Sstevel@tonic-gate }
42000Sstevel@tonic-gate
42010Sstevel@tonic-gate DI_ALL_PTR(st)->map_size = rval;
42020Sstevel@tonic-gate if (di_mem2cache(st, &di_cache) == 0) {
42030Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "can't update cache: copy failed"));
42040Sstevel@tonic-gate return (0);
42050Sstevel@tonic-gate }
42060Sstevel@tonic-gate
42070Sstevel@tonic-gate ASSERT(di_cache.cache_data);
42080Sstevel@tonic-gate ASSERT(di_cache.cache_size > 0);
42090Sstevel@tonic-gate
42100Sstevel@tonic-gate /*
42110Sstevel@tonic-gate * Now that we have cached the snapshot, compute its checksum.
42120Sstevel@tonic-gate * The checksum is only computed over the valid data in the
42130Sstevel@tonic-gate * cache, not the entire cache.
42140Sstevel@tonic-gate * Also, set all the fields (except checksum) before computing
42150Sstevel@tonic-gate * checksum.
42160Sstevel@tonic-gate */
42170Sstevel@tonic-gate all = (struct di_all *)di_cache.cache_data;
42180Sstevel@tonic-gate all->cache_magic = DI_CACHE_MAGIC;
42190Sstevel@tonic-gate all->map_size = rval;
42200Sstevel@tonic-gate
42210Sstevel@tonic-gate ASSERT(all->cache_checksum == 0);
42220Sstevel@tonic-gate CRC32(crc, di_cache.cache_data, all->map_size, -1U, crc32_table);
42230Sstevel@tonic-gate all->cache_checksum = crc;
42240Sstevel@tonic-gate
42250Sstevel@tonic-gate di_cache_write(&di_cache);
42260Sstevel@tonic-gate
42270Sstevel@tonic-gate return (rval);
42280Sstevel@tonic-gate }
42290Sstevel@tonic-gate
42300Sstevel@tonic-gate static void
di_cache_print(di_cache_debug_t msglevel,char * fmt,...)42310Sstevel@tonic-gate di_cache_print(di_cache_debug_t msglevel, char *fmt, ...)
42320Sstevel@tonic-gate {
42330Sstevel@tonic-gate va_list ap;
42340Sstevel@tonic-gate
42350Sstevel@tonic-gate if (di_cache_debug <= DI_QUIET)
42360Sstevel@tonic-gate return;
42370Sstevel@tonic-gate
42380Sstevel@tonic-gate if (di_cache_debug < msglevel)
42390Sstevel@tonic-gate return;
42400Sstevel@tonic-gate
42410Sstevel@tonic-gate switch (msglevel) {
42420Sstevel@tonic-gate case DI_ERR:
42430Sstevel@tonic-gate msglevel = CE_WARN;
42440Sstevel@tonic-gate break;
42450Sstevel@tonic-gate case DI_INFO:
42460Sstevel@tonic-gate case DI_TRACE:
42470Sstevel@tonic-gate default:
42480Sstevel@tonic-gate msglevel = CE_NOTE;
42490Sstevel@tonic-gate break;
42500Sstevel@tonic-gate }
42510Sstevel@tonic-gate
42520Sstevel@tonic-gate va_start(ap, fmt);
42530Sstevel@tonic-gate vcmn_err(msglevel, fmt, ap);
42540Sstevel@tonic-gate va_end(ap);
42550Sstevel@tonic-gate }
425610923SEvan.Yan@Sun.COM
425710923SEvan.Yan@Sun.COM static void
di_hotplug_children(struct di_state * st)425810923SEvan.Yan@Sun.COM di_hotplug_children(struct di_state *st)
425910923SEvan.Yan@Sun.COM {
426010923SEvan.Yan@Sun.COM di_off_t off;
426110923SEvan.Yan@Sun.COM struct di_hp *hp;
426210923SEvan.Yan@Sun.COM struct i_hp *hp_list_node;
426310923SEvan.Yan@Sun.COM
426410923SEvan.Yan@Sun.COM while (hp_list_node = (struct i_hp *)list_remove_head(&st->hp_list)) {
426510923SEvan.Yan@Sun.COM
426610923SEvan.Yan@Sun.COM if ((hp_list_node->hp_child != NULL) &&
426710923SEvan.Yan@Sun.COM (di_dip_find(st, hp_list_node->hp_child, &off) == 0)) {
426810923SEvan.Yan@Sun.COM hp = DI_HP(di_mem_addr(st, hp_list_node->hp_off));
426910923SEvan.Yan@Sun.COM hp->hp_child = off;
427010923SEvan.Yan@Sun.COM }
427110923SEvan.Yan@Sun.COM
427210923SEvan.Yan@Sun.COM kmem_free(hp_list_node, sizeof (i_hp_t));
427310923SEvan.Yan@Sun.COM }
427410923SEvan.Yan@Sun.COM
427510923SEvan.Yan@Sun.COM list_destroy(&st->hp_list);
427610923SEvan.Yan@Sun.COM }
4277