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 /* 238912SChris.Horne@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* 280Sstevel@tonic-gate * driver for accessing kernel devinfo tree. 290Sstevel@tonic-gate */ 300Sstevel@tonic-gate #include <sys/types.h> 310Sstevel@tonic-gate #include <sys/pathname.h> 320Sstevel@tonic-gate #include <sys/debug.h> 330Sstevel@tonic-gate #include <sys/autoconf.h> 347224Scth #include <sys/vmsystm.h> 350Sstevel@tonic-gate #include <sys/conf.h> 360Sstevel@tonic-gate #include <sys/file.h> 370Sstevel@tonic-gate #include <sys/kmem.h> 380Sstevel@tonic-gate #include <sys/modctl.h> 390Sstevel@tonic-gate #include <sys/stat.h> 400Sstevel@tonic-gate #include <sys/ddi.h> 410Sstevel@tonic-gate #include <sys/sunddi.h> 420Sstevel@tonic-gate #include <sys/sunldi_impl.h> 430Sstevel@tonic-gate #include <sys/sunndi.h> 440Sstevel@tonic-gate #include <sys/esunddi.h> 450Sstevel@tonic-gate #include <sys/sunmdi.h> 460Sstevel@tonic-gate #include <sys/ddi_impldefs.h> 470Sstevel@tonic-gate #include <sys/ndi_impldefs.h> 480Sstevel@tonic-gate #include <sys/mdi_impldefs.h> 490Sstevel@tonic-gate #include <sys/devinfo_impl.h> 500Sstevel@tonic-gate #include <sys/thread.h> 510Sstevel@tonic-gate #include <sys/modhash.h> 520Sstevel@tonic-gate #include <sys/bitmap.h> 530Sstevel@tonic-gate #include <util/qsort.h> 540Sstevel@tonic-gate #include <sys/disp.h> 550Sstevel@tonic-gate #include <sys/kobj.h> 560Sstevel@tonic-gate #include <sys/crc32.h> 57*10923SEvan.Yan@Sun.COM #include <sys/ddi_hp.h> 58*10923SEvan.Yan@Sun.COM #include <sys/ddi_hp_impl.h> 59*10923SEvan.Yan@Sun.COM #include <sys/sysmacros.h> 60*10923SEvan.Yan@Sun.COM #include <sys/list.h> 610Sstevel@tonic-gate 620Sstevel@tonic-gate 630Sstevel@tonic-gate #ifdef DEBUG 640Sstevel@tonic-gate static int di_debug; 650Sstevel@tonic-gate #define dcmn_err(args) if (di_debug >= 1) cmn_err args 660Sstevel@tonic-gate #define dcmn_err2(args) if (di_debug >= 2) cmn_err args 670Sstevel@tonic-gate #define dcmn_err3(args) if (di_debug >= 3) cmn_err args 680Sstevel@tonic-gate #else 690Sstevel@tonic-gate #define dcmn_err(args) /* nothing */ 700Sstevel@tonic-gate #define dcmn_err2(args) /* nothing */ 710Sstevel@tonic-gate #define dcmn_err3(args) /* nothing */ 720Sstevel@tonic-gate #endif 730Sstevel@tonic-gate 740Sstevel@tonic-gate /* 750Sstevel@tonic-gate * We partition the space of devinfo minor nodes equally between the full and 760Sstevel@tonic-gate * unprivileged versions of the driver. The even-numbered minor nodes are the 770Sstevel@tonic-gate * full version, while the odd-numbered ones are the read-only version. 780Sstevel@tonic-gate */ 790Sstevel@tonic-gate static int di_max_opens = 32; 800Sstevel@tonic-gate 817224Scth static int di_prop_dyn = 1; /* enable dynamic property support */ 827224Scth 830Sstevel@tonic-gate #define DI_FULL_PARENT 0 840Sstevel@tonic-gate #define DI_READONLY_PARENT 1 850Sstevel@tonic-gate #define DI_NODE_SPECIES 2 860Sstevel@tonic-gate #define DI_UNPRIVILEGED_NODE(x) (((x) % 2) != 0) 870Sstevel@tonic-gate 880Sstevel@tonic-gate #define IOC_IDLE 0 /* snapshot ioctl states */ 890Sstevel@tonic-gate #define IOC_SNAP 1 /* snapshot in progress */ 900Sstevel@tonic-gate #define IOC_DONE 2 /* snapshot done, but not copied out */ 910Sstevel@tonic-gate #define IOC_COPY 3 /* copyout in progress */ 920Sstevel@tonic-gate 930Sstevel@tonic-gate /* 947224Scth * Keep max alignment so we can move snapshot to different platforms. 957224Scth * 967224Scth * NOTE: Most callers should rely on the di_checkmem return value 977224Scth * being aligned, and reestablish *off_p with aligned value, instead 987224Scth * of trying to align size of their allocations: this approach will 997224Scth * minimize memory use. 1000Sstevel@tonic-gate */ 1010Sstevel@tonic-gate #define DI_ALIGN(addr) ((addr + 7l) & ~7l) 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate /* 1040Sstevel@tonic-gate * To avoid wasting memory, make a linked list of memory chunks. 1050Sstevel@tonic-gate * Size of each chunk is buf_size. 1060Sstevel@tonic-gate */ 1070Sstevel@tonic-gate struct di_mem { 1087224Scth struct di_mem *next; /* link to next chunk */ 1097224Scth char *buf; /* contiguous kernel memory */ 1107224Scth size_t buf_size; /* size of buf in bytes */ 1117224Scth devmap_cookie_t cook; /* cookie from ddi_umem_alloc */ 1120Sstevel@tonic-gate }; 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate /* 1150Sstevel@tonic-gate * This is a stack for walking the tree without using recursion. 1160Sstevel@tonic-gate * When the devinfo tree height is above some small size, one 1170Sstevel@tonic-gate * gets watchdog resets on sun4m. 1180Sstevel@tonic-gate */ 1190Sstevel@tonic-gate struct di_stack { 1200Sstevel@tonic-gate void *offset[MAX_TREE_DEPTH]; 1210Sstevel@tonic-gate struct dev_info *dip[MAX_TREE_DEPTH]; 1220Sstevel@tonic-gate int circ[MAX_TREE_DEPTH]; 1230Sstevel@tonic-gate int depth; /* depth of current node to be copied */ 1240Sstevel@tonic-gate }; 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate #define TOP_OFFSET(stack) \ 1270Sstevel@tonic-gate ((di_off_t *)(stack)->offset[(stack)->depth - 1]) 1280Sstevel@tonic-gate #define TOP_NODE(stack) \ 1290Sstevel@tonic-gate ((stack)->dip[(stack)->depth - 1]) 1300Sstevel@tonic-gate #define PARENT_OFFSET(stack) \ 1310Sstevel@tonic-gate ((di_off_t *)(stack)->offset[(stack)->depth - 2]) 1320Sstevel@tonic-gate #define EMPTY_STACK(stack) ((stack)->depth == 0) 1330Sstevel@tonic-gate #define POP_STACK(stack) { \ 1340Sstevel@tonic-gate ndi_devi_exit((dev_info_t *)TOP_NODE(stack), \ 1350Sstevel@tonic-gate (stack)->circ[(stack)->depth - 1]); \ 1360Sstevel@tonic-gate ((stack)->depth--); \ 1370Sstevel@tonic-gate } 1387224Scth #define PUSH_STACK(stack, node, off_p) { \ 1390Sstevel@tonic-gate ASSERT(node != NULL); \ 1400Sstevel@tonic-gate ndi_devi_enter((dev_info_t *)node, &(stack)->circ[(stack)->depth]); \ 1410Sstevel@tonic-gate (stack)->dip[(stack)->depth] = (node); \ 1427224Scth (stack)->offset[(stack)->depth] = (void *)(off_p); \ 1430Sstevel@tonic-gate ((stack)->depth)++; \ 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate 1467224Scth #define DI_ALL_PTR(s) DI_ALL(di_mem_addr((s), 0)) 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate /* 1490Sstevel@tonic-gate * With devfs, the device tree has no global locks. The device tree is 1500Sstevel@tonic-gate * dynamic and dips may come and go if they are not locked locally. Under 1510Sstevel@tonic-gate * these conditions, pointers are no longer reliable as unique IDs. 1520Sstevel@tonic-gate * Specifically, these pointers cannot be used as keys for hash tables 1530Sstevel@tonic-gate * as the same devinfo structure may be freed in one part of the tree only 1540Sstevel@tonic-gate * to be allocated as the structure for a different device in another 1550Sstevel@tonic-gate * part of the tree. This can happen if DR and the snapshot are 1560Sstevel@tonic-gate * happening concurrently. 1570Sstevel@tonic-gate * The following data structures act as keys for devinfo nodes and 1580Sstevel@tonic-gate * pathinfo nodes. 1590Sstevel@tonic-gate */ 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate enum di_ktype { 1620Sstevel@tonic-gate DI_DKEY = 1, 1630Sstevel@tonic-gate DI_PKEY = 2 1640Sstevel@tonic-gate }; 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate struct di_dkey { 1670Sstevel@tonic-gate dev_info_t *dk_dip; 1680Sstevel@tonic-gate major_t dk_major; 1690Sstevel@tonic-gate int dk_inst; 170789Sahrens pnode_t dk_nodeid; 1710Sstevel@tonic-gate }; 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate struct di_pkey { 1740Sstevel@tonic-gate mdi_pathinfo_t *pk_pip; 1750Sstevel@tonic-gate char *pk_path_addr; 1760Sstevel@tonic-gate dev_info_t *pk_client; 1770Sstevel@tonic-gate dev_info_t *pk_phci; 1780Sstevel@tonic-gate }; 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate struct di_key { 1810Sstevel@tonic-gate enum di_ktype k_type; 1820Sstevel@tonic-gate union { 1830Sstevel@tonic-gate struct di_dkey dkey; 1840Sstevel@tonic-gate struct di_pkey pkey; 1850Sstevel@tonic-gate } k_u; 1860Sstevel@tonic-gate }; 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate struct i_lnode; 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate typedef struct i_link { 1920Sstevel@tonic-gate /* 1930Sstevel@tonic-gate * If a di_link struct representing this i_link struct makes it 1940Sstevel@tonic-gate * into the snapshot, then self will point to the offset of 1950Sstevel@tonic-gate * the di_link struct in the snapshot 1960Sstevel@tonic-gate */ 1970Sstevel@tonic-gate di_off_t self; 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate int spec_type; /* block or char access type */ 2000Sstevel@tonic-gate struct i_lnode *src_lnode; /* src i_lnode */ 2010Sstevel@tonic-gate struct i_lnode *tgt_lnode; /* tgt i_lnode */ 2020Sstevel@tonic-gate struct i_link *src_link_next; /* next src i_link /w same i_lnode */ 2030Sstevel@tonic-gate struct i_link *tgt_link_next; /* next tgt i_link /w same i_lnode */ 2040Sstevel@tonic-gate } i_link_t; 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate typedef struct i_lnode { 2070Sstevel@tonic-gate /* 2080Sstevel@tonic-gate * If a di_lnode struct representing this i_lnode struct makes it 2090Sstevel@tonic-gate * into the snapshot, then self will point to the offset of 2100Sstevel@tonic-gate * the di_lnode struct in the snapshot 2110Sstevel@tonic-gate */ 2120Sstevel@tonic-gate di_off_t self; 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate /* 2150Sstevel@tonic-gate * used for hashing and comparing i_lnodes 2160Sstevel@tonic-gate */ 2170Sstevel@tonic-gate int modid; 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate /* 2200Sstevel@tonic-gate * public information describing a link endpoint 2210Sstevel@tonic-gate */ 2220Sstevel@tonic-gate struct di_node *di_node; /* di_node in snapshot */ 2230Sstevel@tonic-gate dev_t devt; /* devt */ 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate /* 2260Sstevel@tonic-gate * i_link ptr to links coming into this i_lnode node 2270Sstevel@tonic-gate * (this i_lnode is the target of these i_links) 2280Sstevel@tonic-gate */ 2290Sstevel@tonic-gate i_link_t *link_in; 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate /* 2320Sstevel@tonic-gate * i_link ptr to links going out of this i_lnode node 2330Sstevel@tonic-gate * (this i_lnode is the source of these i_links) 2340Sstevel@tonic-gate */ 2350Sstevel@tonic-gate i_link_t *link_out; 2360Sstevel@tonic-gate } i_lnode_t; 2370Sstevel@tonic-gate 238*10923SEvan.Yan@Sun.COM typedef struct i_hp { 239*10923SEvan.Yan@Sun.COM di_off_t hp_off; /* Offset of di_hp_t in snapshot */ 240*10923SEvan.Yan@Sun.COM dev_info_t *hp_child; /* Child devinfo node of the di_hp_t */ 241*10923SEvan.Yan@Sun.COM list_node_t hp_link; /* List linkage */ 242*10923SEvan.Yan@Sun.COM } i_hp_t; 243*10923SEvan.Yan@Sun.COM 2440Sstevel@tonic-gate /* 2450Sstevel@tonic-gate * Soft state associated with each instance of driver open. 2460Sstevel@tonic-gate */ 2470Sstevel@tonic-gate static struct di_state { 2487224Scth di_off_t mem_size; /* total # bytes in memlist */ 2497224Scth struct di_mem *memlist; /* head of memlist */ 2507224Scth uint_t command; /* command from ioctl */ 2517224Scth int di_iocstate; /* snapshot ioctl state */ 2527224Scth mod_hash_t *reg_dip_hash; 2537224Scth mod_hash_t *reg_pip_hash; 2547224Scth int lnode_count; 2557224Scth int link_count; 2567224Scth 2577224Scth mod_hash_t *lnode_hash; 2587224Scth mod_hash_t *link_hash; 259*10923SEvan.Yan@Sun.COM 260*10923SEvan.Yan@Sun.COM list_t hp_list; 2610Sstevel@tonic-gate } **di_states; 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate static kmutex_t di_lock; /* serialize instance assignment */ 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate typedef enum { 2660Sstevel@tonic-gate DI_QUIET = 0, /* DI_QUIET must always be 0 */ 2670Sstevel@tonic-gate DI_ERR, 2680Sstevel@tonic-gate DI_INFO, 2690Sstevel@tonic-gate DI_TRACE, 2700Sstevel@tonic-gate DI_TRACE1, 2710Sstevel@tonic-gate DI_TRACE2 2720Sstevel@tonic-gate } di_cache_debug_t; 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate static uint_t di_chunk = 32; /* I/O chunk size in pages */ 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate #define DI_CACHE_LOCK(c) (mutex_enter(&(c).cache_lock)) 2770Sstevel@tonic-gate #define DI_CACHE_UNLOCK(c) (mutex_exit(&(c).cache_lock)) 2780Sstevel@tonic-gate #define DI_CACHE_LOCKED(c) (mutex_owned(&(c).cache_lock)) 2790Sstevel@tonic-gate 280878Sramat /* 281878Sramat * Check that whole device tree is being configured as a pre-condition for 282878Sramat * cleaning up /etc/devices files. 283878Sramat */ 284878Sramat #define DEVICES_FILES_CLEANABLE(st) \ 285878Sramat (((st)->command & DINFOSUBTREE) && ((st)->command & DINFOFORCE) && \ 286878Sramat strcmp(DI_ALL_PTR(st)->root_path, "/") == 0) 287878Sramat 2880Sstevel@tonic-gate #define CACHE_DEBUG(args) \ 2890Sstevel@tonic-gate { if (di_cache_debug != DI_QUIET) di_cache_print args; } 2900Sstevel@tonic-gate 2913133Sjg typedef struct phci_walk_arg { 292893Srs135747 di_off_t off; 293893Srs135747 struct di_state *st; 294893Srs135747 } phci_walk_arg_t; 295893Srs135747 2960Sstevel@tonic-gate static int di_open(dev_t *, int, int, cred_t *); 2970Sstevel@tonic-gate static int di_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 2980Sstevel@tonic-gate static int di_close(dev_t, int, int, cred_t *); 2990Sstevel@tonic-gate static int di_info(dev_info_t *, ddi_info_cmd_t, void *, void **); 3000Sstevel@tonic-gate static int di_attach(dev_info_t *, ddi_attach_cmd_t); 3010Sstevel@tonic-gate static int di_detach(dev_info_t *, ddi_detach_cmd_t); 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate static di_off_t di_copyformat(di_off_t, struct di_state *, intptr_t, int); 304878Sramat static di_off_t di_snapshot_and_clean(struct di_state *); 3050Sstevel@tonic-gate static di_off_t di_copydevnm(di_off_t *, struct di_state *); 3060Sstevel@tonic-gate static di_off_t di_copytree(struct dev_info *, di_off_t *, struct di_state *); 3077224Scth static di_off_t di_copynode(struct dev_info *, struct di_stack *, 3087224Scth struct di_state *); 3090Sstevel@tonic-gate static di_off_t di_getmdata(struct ddi_minor_data *, di_off_t *, di_off_t, 3100Sstevel@tonic-gate struct di_state *); 3110Sstevel@tonic-gate static di_off_t di_getppdata(struct dev_info *, di_off_t *, struct di_state *); 3120Sstevel@tonic-gate static di_off_t di_getdpdata(struct dev_info *, di_off_t *, struct di_state *); 313*10923SEvan.Yan@Sun.COM static di_off_t di_gethpdata(ddi_hp_cn_handle_t *, di_off_t *, 314*10923SEvan.Yan@Sun.COM struct di_state *); 3157224Scth static di_off_t di_getprop(int, struct ddi_prop **, di_off_t *, 3167224Scth struct di_state *, struct dev_info *); 3170Sstevel@tonic-gate static void di_allocmem(struct di_state *, size_t); 3180Sstevel@tonic-gate static void di_freemem(struct di_state *); 3190Sstevel@tonic-gate static void di_copymem(struct di_state *st, caddr_t buf, size_t bufsiz); 3200Sstevel@tonic-gate static di_off_t di_checkmem(struct di_state *, di_off_t, size_t); 3217224Scth static void *di_mem_addr(struct di_state *, di_off_t); 3220Sstevel@tonic-gate static int di_setstate(struct di_state *, int); 3230Sstevel@tonic-gate static void di_register_dip(struct di_state *, dev_info_t *, di_off_t); 3240Sstevel@tonic-gate static void di_register_pip(struct di_state *, mdi_pathinfo_t *, di_off_t); 3250Sstevel@tonic-gate static di_off_t di_getpath_data(dev_info_t *, di_off_t *, di_off_t, 3260Sstevel@tonic-gate struct di_state *, int); 3270Sstevel@tonic-gate static di_off_t di_getlink_data(di_off_t, struct di_state *); 3280Sstevel@tonic-gate static int di_dip_find(struct di_state *st, dev_info_t *node, di_off_t *off_p); 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate static int cache_args_valid(struct di_state *st, int *error); 3310Sstevel@tonic-gate static int snapshot_is_cacheable(struct di_state *st); 3320Sstevel@tonic-gate static int di_cache_lookup(struct di_state *st); 3330Sstevel@tonic-gate static int di_cache_update(struct di_state *st); 3340Sstevel@tonic-gate static void di_cache_print(di_cache_debug_t msglevel, char *fmt, ...); 3357224Scth static int build_vhci_list(dev_info_t *vh_devinfo, void *arg); 3367224Scth static int build_phci_list(dev_info_t *ph_devinfo, void *arg); 337*10923SEvan.Yan@Sun.COM static void di_hotplug_children(struct di_state *st); 3387224Scth 3397224Scth extern int modrootloaded; 3407224Scth extern void mdi_walk_vhcis(int (*)(dev_info_t *, void *), void *); 3417224Scth extern void mdi_vhci_walk_phcis(dev_info_t *, 3427224Scth int (*)(dev_info_t *, void *), void *); 3437224Scth 3440Sstevel@tonic-gate 3450Sstevel@tonic-gate static struct cb_ops di_cb_ops = { 3460Sstevel@tonic-gate di_open, /* open */ 3470Sstevel@tonic-gate di_close, /* close */ 3480Sstevel@tonic-gate nodev, /* strategy */ 3490Sstevel@tonic-gate nodev, /* print */ 3500Sstevel@tonic-gate nodev, /* dump */ 3510Sstevel@tonic-gate nodev, /* read */ 3520Sstevel@tonic-gate nodev, /* write */ 3530Sstevel@tonic-gate di_ioctl, /* ioctl */ 3540Sstevel@tonic-gate nodev, /* devmap */ 3550Sstevel@tonic-gate nodev, /* mmap */ 3560Sstevel@tonic-gate nodev, /* segmap */ 3570Sstevel@tonic-gate nochpoll, /* poll */ 3580Sstevel@tonic-gate ddi_prop_op, /* prop_op */ 3590Sstevel@tonic-gate NULL, /* streamtab */ 3600Sstevel@tonic-gate D_NEW | D_MP /* Driver compatibility flag */ 3610Sstevel@tonic-gate }; 3620Sstevel@tonic-gate 3630Sstevel@tonic-gate static struct dev_ops di_ops = { 3640Sstevel@tonic-gate DEVO_REV, /* devo_rev, */ 3650Sstevel@tonic-gate 0, /* refcnt */ 3660Sstevel@tonic-gate di_info, /* info */ 3670Sstevel@tonic-gate nulldev, /* identify */ 3680Sstevel@tonic-gate nulldev, /* probe */ 3690Sstevel@tonic-gate di_attach, /* attach */ 3700Sstevel@tonic-gate di_detach, /* detach */ 3710Sstevel@tonic-gate nodev, /* reset */ 3720Sstevel@tonic-gate &di_cb_ops, /* driver operations */ 3730Sstevel@tonic-gate NULL /* bus operations */ 3740Sstevel@tonic-gate }; 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate /* 3770Sstevel@tonic-gate * Module linkage information for the kernel. 3780Sstevel@tonic-gate */ 3790Sstevel@tonic-gate static struct modldrv modldrv = { 3800Sstevel@tonic-gate &mod_driverops, 3817627SChris.Horne@Sun.COM "DEVINFO Driver", 3820Sstevel@tonic-gate &di_ops 3830Sstevel@tonic-gate }; 3840Sstevel@tonic-gate 3850Sstevel@tonic-gate static struct modlinkage modlinkage = { 3860Sstevel@tonic-gate MODREV_1, 3870Sstevel@tonic-gate &modldrv, 3880Sstevel@tonic-gate NULL 3890Sstevel@tonic-gate }; 3900Sstevel@tonic-gate 3910Sstevel@tonic-gate int 3920Sstevel@tonic-gate _init(void) 3930Sstevel@tonic-gate { 3940Sstevel@tonic-gate int error; 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate mutex_init(&di_lock, NULL, MUTEX_DRIVER, NULL); 3970Sstevel@tonic-gate 3980Sstevel@tonic-gate error = mod_install(&modlinkage); 3990Sstevel@tonic-gate if (error != 0) { 4000Sstevel@tonic-gate mutex_destroy(&di_lock); 4010Sstevel@tonic-gate return (error); 4020Sstevel@tonic-gate } 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate return (0); 4050Sstevel@tonic-gate } 4060Sstevel@tonic-gate 4070Sstevel@tonic-gate int 4080Sstevel@tonic-gate _info(struct modinfo *modinfop) 4090Sstevel@tonic-gate { 4100Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 4110Sstevel@tonic-gate } 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate int 4140Sstevel@tonic-gate _fini(void) 4150Sstevel@tonic-gate { 4160Sstevel@tonic-gate int error; 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate error = mod_remove(&modlinkage); 4190Sstevel@tonic-gate if (error != 0) { 4200Sstevel@tonic-gate return (error); 4210Sstevel@tonic-gate } 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate mutex_destroy(&di_lock); 4240Sstevel@tonic-gate return (0); 4250Sstevel@tonic-gate } 4260Sstevel@tonic-gate 4270Sstevel@tonic-gate static dev_info_t *di_dip; 4280Sstevel@tonic-gate 4290Sstevel@tonic-gate /*ARGSUSED*/ 4300Sstevel@tonic-gate static int 4310Sstevel@tonic-gate di_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 4320Sstevel@tonic-gate { 4337224Scth int error = DDI_FAILURE; 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate switch (infocmd) { 4360Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 4370Sstevel@tonic-gate *result = (void *)di_dip; 4380Sstevel@tonic-gate error = DDI_SUCCESS; 4390Sstevel@tonic-gate break; 4400Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 4410Sstevel@tonic-gate /* 4420Sstevel@tonic-gate * All dev_t's map to the same, single instance. 4430Sstevel@tonic-gate */ 4440Sstevel@tonic-gate *result = (void *)0; 4450Sstevel@tonic-gate error = DDI_SUCCESS; 4460Sstevel@tonic-gate break; 4470Sstevel@tonic-gate default: 4480Sstevel@tonic-gate break; 4490Sstevel@tonic-gate } 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate return (error); 4520Sstevel@tonic-gate } 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate static int 4550Sstevel@tonic-gate di_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 4560Sstevel@tonic-gate { 4577224Scth int error = DDI_FAILURE; 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate switch (cmd) { 4600Sstevel@tonic-gate case DDI_ATTACH: 4610Sstevel@tonic-gate di_states = kmem_zalloc( 4620Sstevel@tonic-gate di_max_opens * sizeof (struct di_state *), KM_SLEEP); 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate if (ddi_create_minor_node(dip, "devinfo", S_IFCHR, 4650Sstevel@tonic-gate DI_FULL_PARENT, DDI_PSEUDO, NULL) == DDI_FAILURE || 4660Sstevel@tonic-gate ddi_create_minor_node(dip, "devinfo,ro", S_IFCHR, 4670Sstevel@tonic-gate DI_READONLY_PARENT, DDI_PSEUDO, NULL) == DDI_FAILURE) { 4680Sstevel@tonic-gate kmem_free(di_states, 4690Sstevel@tonic-gate di_max_opens * sizeof (struct di_state *)); 4700Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 4710Sstevel@tonic-gate error = DDI_FAILURE; 4720Sstevel@tonic-gate } else { 4730Sstevel@tonic-gate di_dip = dip; 4740Sstevel@tonic-gate ddi_report_dev(dip); 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate error = DDI_SUCCESS; 4770Sstevel@tonic-gate } 4780Sstevel@tonic-gate break; 4790Sstevel@tonic-gate default: 4800Sstevel@tonic-gate error = DDI_FAILURE; 4810Sstevel@tonic-gate break; 4820Sstevel@tonic-gate } 4830Sstevel@tonic-gate 4840Sstevel@tonic-gate return (error); 4850Sstevel@tonic-gate } 4860Sstevel@tonic-gate 4870Sstevel@tonic-gate static int 4880Sstevel@tonic-gate di_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 4890Sstevel@tonic-gate { 4907224Scth int error = DDI_FAILURE; 4910Sstevel@tonic-gate 4920Sstevel@tonic-gate switch (cmd) { 4930Sstevel@tonic-gate case DDI_DETACH: 4940Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 4950Sstevel@tonic-gate di_dip = NULL; 4960Sstevel@tonic-gate kmem_free(di_states, di_max_opens * sizeof (struct di_state *)); 4970Sstevel@tonic-gate 4980Sstevel@tonic-gate error = DDI_SUCCESS; 4990Sstevel@tonic-gate break; 5000Sstevel@tonic-gate default: 5010Sstevel@tonic-gate error = DDI_FAILURE; 5020Sstevel@tonic-gate break; 5030Sstevel@tonic-gate } 5040Sstevel@tonic-gate 5050Sstevel@tonic-gate return (error); 5060Sstevel@tonic-gate } 5070Sstevel@tonic-gate 5080Sstevel@tonic-gate /* 5090Sstevel@tonic-gate * Allow multiple opens by tweaking the dev_t such that it looks like each 5100Sstevel@tonic-gate * open is getting a different minor device. Each minor gets a separate 5110Sstevel@tonic-gate * entry in the di_states[] table. Based on the original minor number, we 5120Sstevel@tonic-gate * discriminate opens of the full and read-only nodes. If all of the instances 5130Sstevel@tonic-gate * of the selected minor node are currently open, we return EAGAIN. 5140Sstevel@tonic-gate */ 5150Sstevel@tonic-gate /*ARGSUSED*/ 5160Sstevel@tonic-gate static int 5170Sstevel@tonic-gate di_open(dev_t *devp, int flag, int otyp, cred_t *credp) 5180Sstevel@tonic-gate { 5197224Scth int m; 5207224Scth minor_t minor_parent = getminor(*devp); 5210Sstevel@tonic-gate 5220Sstevel@tonic-gate if (minor_parent != DI_FULL_PARENT && 5230Sstevel@tonic-gate minor_parent != DI_READONLY_PARENT) 5240Sstevel@tonic-gate return (ENXIO); 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate mutex_enter(&di_lock); 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate for (m = minor_parent; m < di_max_opens; m += DI_NODE_SPECIES) { 5290Sstevel@tonic-gate if (di_states[m] != NULL) 5300Sstevel@tonic-gate continue; 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate di_states[m] = kmem_zalloc(sizeof (struct di_state), KM_SLEEP); 5330Sstevel@tonic-gate break; /* It's ours. */ 5340Sstevel@tonic-gate } 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate if (m >= di_max_opens) { 5370Sstevel@tonic-gate /* 5380Sstevel@tonic-gate * maximum open instance for device reached 5390Sstevel@tonic-gate */ 5400Sstevel@tonic-gate mutex_exit(&di_lock); 5410Sstevel@tonic-gate dcmn_err((CE_WARN, "devinfo: maximum devinfo open reached")); 5420Sstevel@tonic-gate return (EAGAIN); 5430Sstevel@tonic-gate } 5440Sstevel@tonic-gate mutex_exit(&di_lock); 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate ASSERT(m < di_max_opens); 5470Sstevel@tonic-gate *devp = makedevice(getmajor(*devp), (minor_t)(m + DI_NODE_SPECIES)); 5480Sstevel@tonic-gate 5490Sstevel@tonic-gate dcmn_err((CE_CONT, "di_open: thread = %p, assigned minor = %d\n", 5504870Sjg (void *)curthread, m + DI_NODE_SPECIES)); 5510Sstevel@tonic-gate 5520Sstevel@tonic-gate return (0); 5530Sstevel@tonic-gate } 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate /*ARGSUSED*/ 5560Sstevel@tonic-gate static int 5570Sstevel@tonic-gate di_close(dev_t dev, int flag, int otype, cred_t *cred_p) 5580Sstevel@tonic-gate { 5597224Scth struct di_state *st; 5607224Scth int m = (int)getminor(dev) - DI_NODE_SPECIES; 5610Sstevel@tonic-gate 5620Sstevel@tonic-gate if (m < 0) { 5630Sstevel@tonic-gate cmn_err(CE_WARN, "closing non-existent devinfo minor %d", 5640Sstevel@tonic-gate m + DI_NODE_SPECIES); 5650Sstevel@tonic-gate return (ENXIO); 5660Sstevel@tonic-gate } 5670Sstevel@tonic-gate 5680Sstevel@tonic-gate st = di_states[m]; 5690Sstevel@tonic-gate ASSERT(m < di_max_opens && st != NULL); 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate di_freemem(st); 5720Sstevel@tonic-gate kmem_free(st, sizeof (struct di_state)); 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate /* 5750Sstevel@tonic-gate * empty slot in state table 5760Sstevel@tonic-gate */ 5770Sstevel@tonic-gate mutex_enter(&di_lock); 5780Sstevel@tonic-gate di_states[m] = NULL; 5790Sstevel@tonic-gate dcmn_err((CE_CONT, "di_close: thread = %p, assigned minor = %d\n", 5804870Sjg (void *)curthread, m + DI_NODE_SPECIES)); 5810Sstevel@tonic-gate mutex_exit(&di_lock); 5820Sstevel@tonic-gate 5830Sstevel@tonic-gate return (0); 5840Sstevel@tonic-gate } 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate 5870Sstevel@tonic-gate /*ARGSUSED*/ 5880Sstevel@tonic-gate static int 5890Sstevel@tonic-gate di_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp) 5900Sstevel@tonic-gate { 5917224Scth int rv, error; 5927224Scth di_off_t off; 5937224Scth struct di_all *all; 5947224Scth struct di_state *st; 5957224Scth int m = (int)getminor(dev) - DI_NODE_SPECIES; 5967224Scth major_t i; 5977224Scth char *drv_name; 5987224Scth size_t map_size, size; 5997224Scth struct di_mem *dcp; 6007224Scth int ndi_flags; 6010Sstevel@tonic-gate 6020Sstevel@tonic-gate if (m < 0 || m >= di_max_opens) { 6030Sstevel@tonic-gate return (ENXIO); 6040Sstevel@tonic-gate } 6050Sstevel@tonic-gate 6060Sstevel@tonic-gate st = di_states[m]; 6070Sstevel@tonic-gate ASSERT(st != NULL); 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_ioctl: mode = %x, cmd = %x\n", mode, cmd)); 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate switch (cmd) { 6120Sstevel@tonic-gate case DINFOIDENT: 6130Sstevel@tonic-gate /* 6140Sstevel@tonic-gate * This is called from di_init to verify that the driver 6150Sstevel@tonic-gate * opened is indeed devinfo. The purpose is to guard against 6160Sstevel@tonic-gate * sending ioctl to an unknown driver in case of an 6170Sstevel@tonic-gate * unresolved major number conflict during bfu. 6180Sstevel@tonic-gate */ 6190Sstevel@tonic-gate *rvalp = DI_MAGIC; 6200Sstevel@tonic-gate return (0); 6210Sstevel@tonic-gate 6220Sstevel@tonic-gate case DINFOLODRV: 6230Sstevel@tonic-gate /* 6240Sstevel@tonic-gate * Hold an installed driver and return the result 6250Sstevel@tonic-gate */ 6260Sstevel@tonic-gate if (DI_UNPRIVILEGED_NODE(m)) { 6270Sstevel@tonic-gate /* 6280Sstevel@tonic-gate * Only the fully enabled instances may issue 6290Sstevel@tonic-gate * DINFOLDDRV. 6300Sstevel@tonic-gate */ 6310Sstevel@tonic-gate return (EACCES); 6320Sstevel@tonic-gate } 6330Sstevel@tonic-gate 6340Sstevel@tonic-gate drv_name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 6350Sstevel@tonic-gate if (ddi_copyin((void *)arg, drv_name, MAXNAMELEN, mode) != 0) { 6360Sstevel@tonic-gate kmem_free(drv_name, MAXNAMELEN); 6370Sstevel@tonic-gate return (EFAULT); 6380Sstevel@tonic-gate } 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate /* 6410Sstevel@tonic-gate * Some 3rd party driver's _init() walks the device tree, 6420Sstevel@tonic-gate * so we load the driver module before configuring driver. 6430Sstevel@tonic-gate */ 6440Sstevel@tonic-gate i = ddi_name_to_major(drv_name); 6450Sstevel@tonic-gate if (ddi_hold_driver(i) == NULL) { 6460Sstevel@tonic-gate kmem_free(drv_name, MAXNAMELEN); 6470Sstevel@tonic-gate return (ENXIO); 6480Sstevel@tonic-gate } 6490Sstevel@tonic-gate 6500Sstevel@tonic-gate ndi_flags = NDI_DEVI_PERSIST | NDI_CONFIG | NDI_NO_EVENT; 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate /* 6530Sstevel@tonic-gate * i_ddi_load_drvconf() below will trigger a reprobe 6540Sstevel@tonic-gate * via reset_nexus_flags(). NDI_DRV_CONF_REPROBE isn't 6550Sstevel@tonic-gate * needed here. 6560Sstevel@tonic-gate */ 6570Sstevel@tonic-gate modunload_disable(); 6580Sstevel@tonic-gate (void) i_ddi_load_drvconf(i); 6590Sstevel@tonic-gate (void) ndi_devi_config_driver(ddi_root_node(), ndi_flags, i); 6600Sstevel@tonic-gate kmem_free(drv_name, MAXNAMELEN); 6610Sstevel@tonic-gate ddi_rele_driver(i); 6620Sstevel@tonic-gate rv = i_ddi_devs_attached(i); 6630Sstevel@tonic-gate modunload_enable(); 6640Sstevel@tonic-gate 66510696SDavid.Hollister@Sun.COM i_ddi_di_cache_invalidate(); 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate return ((rv == DDI_SUCCESS)? 0 : ENXIO); 6680Sstevel@tonic-gate 6690Sstevel@tonic-gate case DINFOUSRLD: 6700Sstevel@tonic-gate /* 6710Sstevel@tonic-gate * The case for copying snapshot to userland 6720Sstevel@tonic-gate */ 6730Sstevel@tonic-gate if (di_setstate(st, IOC_COPY) == -1) 6740Sstevel@tonic-gate return (EBUSY); 6750Sstevel@tonic-gate 6767224Scth map_size = DI_ALL_PTR(st)->map_size; 6770Sstevel@tonic-gate if (map_size == 0) { 6780Sstevel@tonic-gate (void) di_setstate(st, IOC_DONE); 6790Sstevel@tonic-gate return (EFAULT); 6800Sstevel@tonic-gate } 6810Sstevel@tonic-gate 6820Sstevel@tonic-gate /* 6830Sstevel@tonic-gate * copyout the snapshot 6840Sstevel@tonic-gate */ 6850Sstevel@tonic-gate map_size = (map_size + PAGEOFFSET) & PAGEMASK; 6860Sstevel@tonic-gate 6870Sstevel@tonic-gate /* 6880Sstevel@tonic-gate * Return the map size, so caller may do a sanity 6890Sstevel@tonic-gate * check against the return value of snapshot ioctl() 6900Sstevel@tonic-gate */ 6910Sstevel@tonic-gate *rvalp = (int)map_size; 6920Sstevel@tonic-gate 6930Sstevel@tonic-gate /* 6940Sstevel@tonic-gate * Copy one chunk at a time 6950Sstevel@tonic-gate */ 6960Sstevel@tonic-gate off = 0; 6970Sstevel@tonic-gate dcp = st->memlist; 6980Sstevel@tonic-gate while (map_size) { 6990Sstevel@tonic-gate size = dcp->buf_size; 7000Sstevel@tonic-gate if (map_size <= size) { 7010Sstevel@tonic-gate size = map_size; 7020Sstevel@tonic-gate } 7030Sstevel@tonic-gate 7040Sstevel@tonic-gate if (ddi_copyout(di_mem_addr(st, off), 7050Sstevel@tonic-gate (void *)(arg + off), size, mode) != 0) { 7060Sstevel@tonic-gate (void) di_setstate(st, IOC_DONE); 7070Sstevel@tonic-gate return (EFAULT); 7080Sstevel@tonic-gate } 7090Sstevel@tonic-gate 7100Sstevel@tonic-gate map_size -= size; 7110Sstevel@tonic-gate off += size; 7120Sstevel@tonic-gate dcp = dcp->next; 7130Sstevel@tonic-gate } 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate di_freemem(st); 7160Sstevel@tonic-gate (void) di_setstate(st, IOC_IDLE); 7170Sstevel@tonic-gate return (0); 7180Sstevel@tonic-gate 7190Sstevel@tonic-gate default: 7200Sstevel@tonic-gate if ((cmd & ~DIIOC_MASK) != DIIOC) { 7210Sstevel@tonic-gate /* 7220Sstevel@tonic-gate * Invalid ioctl command 7230Sstevel@tonic-gate */ 7240Sstevel@tonic-gate return (ENOTTY); 7250Sstevel@tonic-gate } 7260Sstevel@tonic-gate /* 7270Sstevel@tonic-gate * take a snapshot 7280Sstevel@tonic-gate */ 7290Sstevel@tonic-gate st->command = cmd & DIIOC_MASK; 7300Sstevel@tonic-gate /*FALLTHROUGH*/ 7310Sstevel@tonic-gate } 7320Sstevel@tonic-gate 7330Sstevel@tonic-gate /* 7340Sstevel@tonic-gate * Obtain enough memory to hold header + rootpath. We prevent kernel 7350Sstevel@tonic-gate * memory exhaustion by freeing any previously allocated snapshot and 7360Sstevel@tonic-gate * refusing the operation; otherwise we would be allowing ioctl(), 7370Sstevel@tonic-gate * ioctl(), ioctl(), ..., panic. 7380Sstevel@tonic-gate */ 7390Sstevel@tonic-gate if (di_setstate(st, IOC_SNAP) == -1) 7400Sstevel@tonic-gate return (EBUSY); 7410Sstevel@tonic-gate 7427224Scth /* 7437224Scth * Initial memlist always holds di_all and the root_path - and 7447224Scth * is at least a page and size. 7457224Scth */ 7460Sstevel@tonic-gate size = sizeof (struct di_all) + 7470Sstevel@tonic-gate sizeof (((struct dinfo_io *)(NULL))->root_path); 7480Sstevel@tonic-gate if (size < PAGESIZE) 7490Sstevel@tonic-gate size = PAGESIZE; 7507224Scth off = di_checkmem(st, 0, size); 7517224Scth all = DI_ALL_PTR(st); 7527224Scth off += sizeof (struct di_all); /* real length of di_all */ 7537224Scth 7540Sstevel@tonic-gate all->devcnt = devcnt; 7550Sstevel@tonic-gate all->command = st->command; 7560Sstevel@tonic-gate all->version = DI_SNAPSHOT_VERSION; 7577224Scth all->top_vhci_devinfo = 0; /* filled by build_vhci_list. */ 7580Sstevel@tonic-gate 7590Sstevel@tonic-gate /* 7600Sstevel@tonic-gate * Note the endianness in case we need to transport snapshot 7610Sstevel@tonic-gate * over the network. 7620Sstevel@tonic-gate */ 7630Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN) 7640Sstevel@tonic-gate all->endianness = DI_LITTLE_ENDIAN; 7650Sstevel@tonic-gate #else 7660Sstevel@tonic-gate all->endianness = DI_BIG_ENDIAN; 7670Sstevel@tonic-gate #endif 7680Sstevel@tonic-gate 7690Sstevel@tonic-gate /* Copyin ioctl args, store in the snapshot. */ 7700Sstevel@tonic-gate if (copyinstr((void *)arg, all->root_path, 7710Sstevel@tonic-gate sizeof (((struct dinfo_io *)(NULL))->root_path), &size) != 0) { 7720Sstevel@tonic-gate di_freemem(st); 7730Sstevel@tonic-gate (void) di_setstate(st, IOC_IDLE); 7740Sstevel@tonic-gate return (EFAULT); 7750Sstevel@tonic-gate } 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 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 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 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 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 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 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 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 * 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 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 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 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 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 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 12980Sstevel@tonic-gate /* 12990Sstevel@tonic-gate * This is the main function that takes a snapshot 13000Sstevel@tonic-gate */ 13010Sstevel@tonic-gate static di_off_t 13020Sstevel@tonic-gate di_snapshot(struct di_state *st) 13030Sstevel@tonic-gate { 13047224Scth di_off_t off; 13057224Scth struct di_all *all; 13067224Scth dev_info_t *rootnode; 13077224Scth char buf[80]; 13087224Scth int plen; 13097224Scth char *path; 13107224Scth vnode_t *vp; 13117224Scth 13127224Scth all = DI_ALL_PTR(st); 13130Sstevel@tonic-gate dcmn_err((CE_CONT, "Taking a snapshot of devinfo tree...\n")); 13140Sstevel@tonic-gate 13150Sstevel@tonic-gate /* 1316643Scth * Verify path before entrusting it to e_ddi_hold_devi_by_path because 1317643Scth * some platforms have OBP bugs where executing the NDI_PROMNAME code 1318643Scth * path against an invalid path results in panic. The lookupnameat 1319643Scth * is done relative to rootdir without a leading '/' on "devices/" 1320643Scth * to force the lookup to occur in the global zone. 1321643Scth */ 1322643Scth plen = strlen("devices/") + strlen(all->root_path) + 1; 1323643Scth path = kmem_alloc(plen, KM_SLEEP); 1324643Scth (void) snprintf(path, plen, "devices/%s", all->root_path); 1325643Scth if (lookupnameat(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp, rootdir)) { 1326643Scth dcmn_err((CE_CONT, "Devinfo node %s not found\n", 1327643Scth all->root_path)); 1328643Scth kmem_free(path, plen); 1329643Scth return (0); 1330643Scth } 1331643Scth kmem_free(path, plen); 1332643Scth VN_RELE(vp); 1333643Scth 1334643Scth /* 13350Sstevel@tonic-gate * Hold the devinfo node referred by the path. 13360Sstevel@tonic-gate */ 13370Sstevel@tonic-gate rootnode = e_ddi_hold_devi_by_path(all->root_path, 0); 13380Sstevel@tonic-gate if (rootnode == NULL) { 13390Sstevel@tonic-gate dcmn_err((CE_CONT, "Devinfo node %s not found\n", 13400Sstevel@tonic-gate all->root_path)); 13410Sstevel@tonic-gate return (0); 13420Sstevel@tonic-gate } 13430Sstevel@tonic-gate 13440Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), 13450Sstevel@tonic-gate "devinfo registered dips (statep=%p)", (void *)st); 13460Sstevel@tonic-gate 13470Sstevel@tonic-gate st->reg_dip_hash = mod_hash_create_extended(buf, 64, 13480Sstevel@tonic-gate di_key_dtor, mod_hash_null_valdtor, di_hash_byptr, 13490Sstevel@tonic-gate NULL, di_key_cmp, KM_SLEEP); 13500Sstevel@tonic-gate 13510Sstevel@tonic-gate 13520Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), 13530Sstevel@tonic-gate "devinfo registered pips (statep=%p)", (void *)st); 13540Sstevel@tonic-gate 13550Sstevel@tonic-gate st->reg_pip_hash = mod_hash_create_extended(buf, 64, 13560Sstevel@tonic-gate di_key_dtor, mod_hash_null_valdtor, di_hash_byptr, 13570Sstevel@tonic-gate NULL, di_key_cmp, KM_SLEEP); 13580Sstevel@tonic-gate 1359*10923SEvan.Yan@Sun.COM if (DINFOHP & st->command) { 1360*10923SEvan.Yan@Sun.COM list_create(&st->hp_list, sizeof (i_hp_t), 1361*10923SEvan.Yan@Sun.COM offsetof(i_hp_t, hp_link)); 1362*10923SEvan.Yan@Sun.COM } 1363*10923SEvan.Yan@Sun.COM 13640Sstevel@tonic-gate /* 13650Sstevel@tonic-gate * copy the device tree 13660Sstevel@tonic-gate */ 13670Sstevel@tonic-gate off = di_copytree(DEVI(rootnode), &all->top_devinfo, st); 13680Sstevel@tonic-gate 1369893Srs135747 if (DINFOPATH & st->command) { 1370893Srs135747 mdi_walk_vhcis(build_vhci_list, st); 1371893Srs135747 } 1372893Srs135747 1373*10923SEvan.Yan@Sun.COM if (DINFOHP & st->command) { 1374*10923SEvan.Yan@Sun.COM di_hotplug_children(st); 1375*10923SEvan.Yan@Sun.COM } 1376*10923SEvan.Yan@Sun.COM 13770Sstevel@tonic-gate ddi_release_devi(rootnode); 13780Sstevel@tonic-gate 13790Sstevel@tonic-gate /* 13800Sstevel@tonic-gate * copy the devnames array 13810Sstevel@tonic-gate */ 13820Sstevel@tonic-gate all->devnames = off; 13830Sstevel@tonic-gate off = di_copydevnm(&all->devnames, st); 13840Sstevel@tonic-gate 13850Sstevel@tonic-gate 13860Sstevel@tonic-gate /* initialize the hash tables */ 13870Sstevel@tonic-gate st->lnode_count = 0; 13880Sstevel@tonic-gate st->link_count = 0; 13890Sstevel@tonic-gate 13900Sstevel@tonic-gate if (DINFOLYR & st->command) { 13910Sstevel@tonic-gate off = di_getlink_data(off, st); 13920Sstevel@tonic-gate } 13930Sstevel@tonic-gate 13940Sstevel@tonic-gate /* 13950Sstevel@tonic-gate * Free up hash tables 13960Sstevel@tonic-gate */ 13970Sstevel@tonic-gate mod_hash_destroy_hash(st->reg_dip_hash); 13980Sstevel@tonic-gate mod_hash_destroy_hash(st->reg_pip_hash); 13990Sstevel@tonic-gate 14000Sstevel@tonic-gate /* 14010Sstevel@tonic-gate * Record the timestamp now that we are done with snapshot. 14020Sstevel@tonic-gate * 14030Sstevel@tonic-gate * We compute the checksum later and then only if we cache 14040Sstevel@tonic-gate * the snapshot, since checksumming adds some overhead. 14050Sstevel@tonic-gate * The checksum is checked later if we read the cache file. 14060Sstevel@tonic-gate * from disk. 14070Sstevel@tonic-gate * 14080Sstevel@tonic-gate * Set checksum field to 0 as CRC is calculated with that 14090Sstevel@tonic-gate * field set to 0. 14100Sstevel@tonic-gate */ 14110Sstevel@tonic-gate all->snapshot_time = ddi_get_time(); 14120Sstevel@tonic-gate all->cache_checksum = 0; 14130Sstevel@tonic-gate 14143133Sjg ASSERT(all->snapshot_time != 0); 14153133Sjg 14160Sstevel@tonic-gate return (off); 14170Sstevel@tonic-gate } 14180Sstevel@tonic-gate 14190Sstevel@tonic-gate /* 1420878Sramat * Take a snapshot and clean /etc/devices files if DINFOCLEANUP is set 1421878Sramat */ 1422878Sramat static di_off_t 1423878Sramat di_snapshot_and_clean(struct di_state *st) 1424878Sramat { 1425893Srs135747 di_off_t off; 1426878Sramat 1427878Sramat modunload_disable(); 1428878Sramat off = di_snapshot(st); 1429878Sramat if (off != 0 && (st->command & DINFOCLEANUP)) { 1430878Sramat ASSERT(DEVICES_FILES_CLEANABLE(st)); 1431878Sramat /* 1432878Sramat * Cleanup /etc/devices files: 1433878Sramat * In order to accurately account for the system configuration 1434878Sramat * in /etc/devices files, the appropriate drivers must be 1435878Sramat * fully configured before the cleanup starts. 1436878Sramat * So enable modunload only after the cleanup. 1437878Sramat */ 1438878Sramat i_ddi_clean_devices_files(); 14394870Sjg /* 14404870Sjg * Remove backing store nodes for unused devices, 14414870Sjg * which retain past permissions customizations 14424870Sjg * and may be undesired for newly configured devices. 14434870Sjg */ 14444870Sjg dev_devices_cleanup(); 1445878Sramat } 1446878Sramat modunload_enable(); 1447878Sramat 1448878Sramat return (off); 1449878Sramat } 1450878Sramat 1451878Sramat /* 1452893Srs135747 * construct vhci linkage in the snapshot. 1453893Srs135747 */ 14547224Scth static int 1455893Srs135747 build_vhci_list(dev_info_t *vh_devinfo, void *arg) 1456893Srs135747 { 14577224Scth struct di_all *all; 14587224Scth struct di_node *me; 14597224Scth struct di_state *st; 14607224Scth di_off_t off; 14617224Scth phci_walk_arg_t pwa; 1462893Srs135747 1463893Srs135747 dcmn_err3((CE_CONT, "build_vhci list\n")); 1464893Srs135747 14657224Scth dcmn_err3((CE_CONT, "vhci node %s%d\n", 14667224Scth ddi_driver_name(vh_devinfo), ddi_get_instance(vh_devinfo))); 1467893Srs135747 1468893Srs135747 st = (struct di_state *)arg; 1469893Srs135747 if (di_dip_find(st, vh_devinfo, &off) != 0) { 1470893Srs135747 dcmn_err((CE_WARN, "di_dip_find error for the given node\n")); 1471893Srs135747 return (DDI_WALK_TERMINATE); 1472893Srs135747 } 1473893Srs135747 1474893Srs135747 dcmn_err3((CE_CONT, "st->mem_size: %d vh_devinfo off: 0x%x\n", 14754870Sjg st->mem_size, off)); 1476893Srs135747 14777224Scth all = DI_ALL_PTR(st); 1478893Srs135747 if (all->top_vhci_devinfo == 0) { 1479893Srs135747 all->top_vhci_devinfo = off; 1480893Srs135747 } else { 14817224Scth me = DI_NODE(di_mem_addr(st, all->top_vhci_devinfo)); 1482893Srs135747 1483893Srs135747 while (me->next_vhci != 0) { 14847224Scth me = DI_NODE(di_mem_addr(st, me->next_vhci)); 1485893Srs135747 } 1486893Srs135747 1487893Srs135747 me->next_vhci = off; 1488893Srs135747 } 1489893Srs135747 1490893Srs135747 pwa.off = off; 1491893Srs135747 pwa.st = st; 1492893Srs135747 mdi_vhci_walk_phcis(vh_devinfo, build_phci_list, &pwa); 1493893Srs135747 1494893Srs135747 return (DDI_WALK_CONTINUE); 1495893Srs135747 } 1496893Srs135747 1497893Srs135747 /* 1498893Srs135747 * construct phci linkage for the given vhci in the snapshot. 1499893Srs135747 */ 15007224Scth static int 1501893Srs135747 build_phci_list(dev_info_t *ph_devinfo, void *arg) 1502893Srs135747 { 15037224Scth struct di_node *vh_di_node; 15047224Scth struct di_node *me; 15057224Scth phci_walk_arg_t *pwa; 15067224Scth di_off_t off; 1507893Srs135747 15083133Sjg pwa = (phci_walk_arg_t *)arg; 1509893Srs135747 1510893Srs135747 dcmn_err3((CE_CONT, "build_phci list for vhci at offset: 0x%x\n", 15114870Sjg pwa->off)); 1512893Srs135747 15137224Scth vh_di_node = DI_NODE(di_mem_addr(pwa->st, pwa->off)); 1514893Srs135747 if (di_dip_find(pwa->st, ph_devinfo, &off) != 0) { 1515893Srs135747 dcmn_err((CE_WARN, "di_dip_find error for the given node\n")); 1516893Srs135747 return (DDI_WALK_TERMINATE); 1517893Srs135747 } 1518893Srs135747 15197224Scth dcmn_err3((CE_CONT, "phci node %s%d, at offset 0x%x\n", 15207224Scth ddi_driver_name(ph_devinfo), ddi_get_instance(ph_devinfo), off)); 1521893Srs135747 1522893Srs135747 if (vh_di_node->top_phci == 0) { 1523893Srs135747 vh_di_node->top_phci = off; 1524893Srs135747 return (DDI_WALK_CONTINUE); 1525893Srs135747 } 1526893Srs135747 15277224Scth me = DI_NODE(di_mem_addr(pwa->st, vh_di_node->top_phci)); 1528893Srs135747 1529893Srs135747 while (me->next_phci != 0) { 15307224Scth me = DI_NODE(di_mem_addr(pwa->st, me->next_phci)); 1531893Srs135747 } 1532893Srs135747 me->next_phci = off; 1533893Srs135747 1534893Srs135747 return (DDI_WALK_CONTINUE); 1535893Srs135747 } 1536893Srs135747 1537893Srs135747 /* 15380Sstevel@tonic-gate * Assumes all devinfo nodes in device tree have been snapshotted 15390Sstevel@tonic-gate */ 15400Sstevel@tonic-gate static void 15417224Scth snap_driver_list(struct di_state *st, struct devnames *dnp, di_off_t *off_p) 15420Sstevel@tonic-gate { 15437224Scth struct dev_info *node; 15447224Scth struct di_node *me; 15457224Scth di_off_t off; 15460Sstevel@tonic-gate 15470Sstevel@tonic-gate ASSERT(mutex_owned(&dnp->dn_lock)); 15480Sstevel@tonic-gate 15490Sstevel@tonic-gate node = DEVI(dnp->dn_head); 15500Sstevel@tonic-gate for (; node; node = node->devi_next) { 15510Sstevel@tonic-gate if (di_dip_find(st, (dev_info_t *)node, &off) != 0) 15520Sstevel@tonic-gate continue; 15530Sstevel@tonic-gate 15540Sstevel@tonic-gate ASSERT(off > 0); 15557224Scth me = DI_NODE(di_mem_addr(st, off)); 15560Sstevel@tonic-gate ASSERT(me->next == 0 || me->next == -1); 15570Sstevel@tonic-gate /* 15580Sstevel@tonic-gate * Only nodes which were BOUND when they were 15590Sstevel@tonic-gate * snapshotted will be added to per-driver list. 15600Sstevel@tonic-gate */ 15610Sstevel@tonic-gate if (me->next != -1) 15620Sstevel@tonic-gate continue; 15630Sstevel@tonic-gate 15647224Scth *off_p = off; 15657224Scth off_p = &me->next; 15660Sstevel@tonic-gate } 15670Sstevel@tonic-gate 15687224Scth *off_p = 0; 15690Sstevel@tonic-gate } 15700Sstevel@tonic-gate 15710Sstevel@tonic-gate /* 15720Sstevel@tonic-gate * Copy the devnames array, so we have a list of drivers in the snapshot. 15730Sstevel@tonic-gate * Also makes it possible to locate the per-driver devinfo nodes. 15740Sstevel@tonic-gate */ 15750Sstevel@tonic-gate static di_off_t 15760Sstevel@tonic-gate di_copydevnm(di_off_t *off_p, struct di_state *st) 15770Sstevel@tonic-gate { 15787224Scth int i; 15797224Scth di_off_t off; 15807224Scth size_t size; 15817224Scth struct di_devnm *dnp; 15820Sstevel@tonic-gate 15830Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_copydevnm: *off_p = %p\n", (void *)off_p)); 15840Sstevel@tonic-gate 15850Sstevel@tonic-gate /* 15860Sstevel@tonic-gate * make sure there is some allocated memory 15870Sstevel@tonic-gate */ 15880Sstevel@tonic-gate size = devcnt * sizeof (struct di_devnm); 15897224Scth *off_p = off = di_checkmem(st, *off_p, size); 15907224Scth dnp = DI_DEVNM(di_mem_addr(st, off)); 15917224Scth off += size; 15920Sstevel@tonic-gate 15930Sstevel@tonic-gate dcmn_err((CE_CONT, "Start copying devnamesp[%d] at offset 0x%x\n", 15944870Sjg devcnt, off)); 15950Sstevel@tonic-gate 15960Sstevel@tonic-gate for (i = 0; i < devcnt; i++) { 15970Sstevel@tonic-gate if (devnamesp[i].dn_name == NULL) { 15980Sstevel@tonic-gate continue; 15990Sstevel@tonic-gate } 16000Sstevel@tonic-gate 16010Sstevel@tonic-gate /* 16020Sstevel@tonic-gate * dn_name is not freed during driver unload or removal. 16030Sstevel@tonic-gate * 16040Sstevel@tonic-gate * There is a race condition when make_devname() changes 16050Sstevel@tonic-gate * dn_name during our strcpy. This should be rare since 16060Sstevel@tonic-gate * only add_drv does this. At any rate, we never had a 16070Sstevel@tonic-gate * problem with ddi_name_to_major(), which should have 16080Sstevel@tonic-gate * the same problem. 16090Sstevel@tonic-gate */ 16100Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_copydevnm: %s%d, off=%x\n", 16117224Scth devnamesp[i].dn_name, devnamesp[i].dn_instance, off)); 16127224Scth 16137224Scth size = strlen(devnamesp[i].dn_name) + 1; 16147224Scth dnp[i].name = off = di_checkmem(st, off, size); 16150Sstevel@tonic-gate (void) strcpy((char *)di_mem_addr(st, off), 16164870Sjg devnamesp[i].dn_name); 16177224Scth off += size; 16180Sstevel@tonic-gate 16190Sstevel@tonic-gate mutex_enter(&devnamesp[i].dn_lock); 16200Sstevel@tonic-gate 16210Sstevel@tonic-gate /* 16220Sstevel@tonic-gate * Snapshot per-driver node list 16230Sstevel@tonic-gate */ 16240Sstevel@tonic-gate snap_driver_list(st, &devnamesp[i], &dnp[i].head); 16250Sstevel@tonic-gate 16260Sstevel@tonic-gate /* 16270Sstevel@tonic-gate * This is not used by libdevinfo, leave it for now 16280Sstevel@tonic-gate */ 16290Sstevel@tonic-gate dnp[i].flags = devnamesp[i].dn_flags; 16300Sstevel@tonic-gate dnp[i].instance = devnamesp[i].dn_instance; 16310Sstevel@tonic-gate 16320Sstevel@tonic-gate /* 16330Sstevel@tonic-gate * get global properties 16340Sstevel@tonic-gate */ 16350Sstevel@tonic-gate if ((DINFOPROP & st->command) && 16360Sstevel@tonic-gate devnamesp[i].dn_global_prop_ptr) { 16370Sstevel@tonic-gate dnp[i].global_prop = off; 16387224Scth off = di_getprop(DI_PROP_GLB_LIST, 16397224Scth &devnamesp[i].dn_global_prop_ptr->prop_list, 16407224Scth &dnp[i].global_prop, st, NULL); 16410Sstevel@tonic-gate } 16420Sstevel@tonic-gate 16430Sstevel@tonic-gate /* 16440Sstevel@tonic-gate * Bit encode driver ops: & bus_ops, cb_ops, & cb_ops->cb_str 16450Sstevel@tonic-gate */ 16460Sstevel@tonic-gate if (CB_DRV_INSTALLED(devopsp[i])) { 16470Sstevel@tonic-gate if (devopsp[i]->devo_cb_ops) { 16480Sstevel@tonic-gate dnp[i].ops |= DI_CB_OPS; 16490Sstevel@tonic-gate if (devopsp[i]->devo_cb_ops->cb_str) 16500Sstevel@tonic-gate dnp[i].ops |= DI_STREAM_OPS; 16510Sstevel@tonic-gate } 16520Sstevel@tonic-gate if (NEXUS_DRV(devopsp[i])) { 16530Sstevel@tonic-gate dnp[i].ops |= DI_BUS_OPS; 16540Sstevel@tonic-gate } 16550Sstevel@tonic-gate } 16560Sstevel@tonic-gate 16570Sstevel@tonic-gate mutex_exit(&devnamesp[i].dn_lock); 16580Sstevel@tonic-gate } 16590Sstevel@tonic-gate 16600Sstevel@tonic-gate dcmn_err((CE_CONT, "End copying devnamesp at offset 0x%x\n", off)); 16610Sstevel@tonic-gate 16620Sstevel@tonic-gate return (off); 16630Sstevel@tonic-gate } 16640Sstevel@tonic-gate 16650Sstevel@tonic-gate /* 16660Sstevel@tonic-gate * Copy the kernel devinfo tree. The tree and the devnames array forms 16670Sstevel@tonic-gate * the entire snapshot (see also di_copydevnm). 16680Sstevel@tonic-gate */ 16690Sstevel@tonic-gate static di_off_t 16700Sstevel@tonic-gate di_copytree(struct dev_info *root, di_off_t *off_p, struct di_state *st) 16710Sstevel@tonic-gate { 16727224Scth di_off_t off; 16737224Scth struct dev_info *node; 16747224Scth struct di_stack *dsp = kmem_zalloc(sizeof (struct di_stack), KM_SLEEP); 16750Sstevel@tonic-gate 16760Sstevel@tonic-gate dcmn_err((CE_CONT, "di_copytree: root = %p, *off_p = %x\n", 16774870Sjg (void *)root, *off_p)); 16780Sstevel@tonic-gate 16790Sstevel@tonic-gate /* force attach drivers */ 16801333Scth if (i_ddi_devi_attached((dev_info_t *)root) && 16810Sstevel@tonic-gate (st->command & DINFOSUBTREE) && (st->command & DINFOFORCE)) { 16820Sstevel@tonic-gate (void) ndi_devi_config((dev_info_t *)root, 16830Sstevel@tonic-gate NDI_CONFIG | NDI_DEVI_PERSIST | NDI_NO_EVENT | 16840Sstevel@tonic-gate NDI_DRV_CONF_REPROBE); 16850Sstevel@tonic-gate } 16860Sstevel@tonic-gate 16870Sstevel@tonic-gate /* 16880Sstevel@tonic-gate * Push top_devinfo onto a stack 16890Sstevel@tonic-gate * 16900Sstevel@tonic-gate * The stack is necessary to avoid recursion, which can overrun 16910Sstevel@tonic-gate * the kernel stack. 16920Sstevel@tonic-gate */ 16930Sstevel@tonic-gate PUSH_STACK(dsp, root, off_p); 16940Sstevel@tonic-gate 16950Sstevel@tonic-gate /* 16960Sstevel@tonic-gate * As long as there is a node on the stack, copy the node. 16970Sstevel@tonic-gate * di_copynode() is responsible for pushing and popping 16980Sstevel@tonic-gate * child and sibling nodes on the stack. 16990Sstevel@tonic-gate */ 17000Sstevel@tonic-gate while (!EMPTY_STACK(dsp)) { 17017224Scth node = TOP_NODE(dsp); 17027224Scth off = di_copynode(node, dsp, st); 17030Sstevel@tonic-gate } 17040Sstevel@tonic-gate 17050Sstevel@tonic-gate /* 17060Sstevel@tonic-gate * Free the stack structure 17070Sstevel@tonic-gate */ 17080Sstevel@tonic-gate kmem_free(dsp, sizeof (struct di_stack)); 17090Sstevel@tonic-gate 17100Sstevel@tonic-gate return (off); 17110Sstevel@tonic-gate } 17120Sstevel@tonic-gate 17130Sstevel@tonic-gate /* 17140Sstevel@tonic-gate * This is the core function, which copies all data associated with a single 17150Sstevel@tonic-gate * node into the snapshot. The amount of information is determined by the 17160Sstevel@tonic-gate * ioctl command. 17170Sstevel@tonic-gate */ 17180Sstevel@tonic-gate static di_off_t 17197224Scth di_copynode(struct dev_info *node, struct di_stack *dsp, struct di_state *st) 17200Sstevel@tonic-gate { 17216640Scth di_off_t off; 17226640Scth struct di_node *me; 1723*10923SEvan.Yan@Sun.COM size_t size; 1724*10923SEvan.Yan@Sun.COM struct dev_info *n; 17250Sstevel@tonic-gate 17264870Sjg dcmn_err2((CE_CONT, "di_copynode: depth = %x\n", dsp->depth)); 17277224Scth ASSERT((node != NULL) && (node == TOP_NODE(dsp))); 17280Sstevel@tonic-gate 17290Sstevel@tonic-gate /* 17300Sstevel@tonic-gate * check memory usage, and fix offsets accordingly. 17310Sstevel@tonic-gate */ 17327224Scth size = sizeof (struct di_node); 17337224Scth *(TOP_OFFSET(dsp)) = off = di_checkmem(st, *(TOP_OFFSET(dsp)), size); 17340Sstevel@tonic-gate me = DI_NODE(di_mem_addr(st, off)); 17357224Scth me->self = off; 17367224Scth off += size; 17370Sstevel@tonic-gate 17380Sstevel@tonic-gate dcmn_err((CE_CONT, "copy node %s, instance #%d, at offset 0x%x\n", 17394870Sjg node->devi_node_name, node->devi_instance, off)); 17400Sstevel@tonic-gate 17410Sstevel@tonic-gate /* 17420Sstevel@tonic-gate * Node parameters: 17430Sstevel@tonic-gate * self -- offset of current node within snapshot 17440Sstevel@tonic-gate * nodeid -- pointer to PROM node (tri-valued) 17450Sstevel@tonic-gate * state -- hot plugging device state 17467224Scth * node_state -- devinfo node state 17470Sstevel@tonic-gate */ 17480Sstevel@tonic-gate me->instance = node->devi_instance; 17490Sstevel@tonic-gate me->nodeid = node->devi_nodeid; 17500Sstevel@tonic-gate me->node_class = node->devi_node_class; 17510Sstevel@tonic-gate me->attributes = node->devi_node_attributes; 17520Sstevel@tonic-gate me->state = node->devi_state; 17534444Svikram me->flags = node->devi_flags; 17540Sstevel@tonic-gate me->node_state = node->devi_node_state; 1755893Srs135747 me->next_vhci = 0; /* Filled up by build_vhci_list. */ 1756893Srs135747 me->top_phci = 0; /* Filled up by build_phci_list. */ 1757893Srs135747 me->next_phci = 0; /* Filled up by build_phci_list. */ 1758893Srs135747 me->multipath_component = MULTIPATH_COMPONENT_NONE; /* set default. */ 17590Sstevel@tonic-gate me->user_private_data = NULL; 17600Sstevel@tonic-gate 17610Sstevel@tonic-gate /* 17620Sstevel@tonic-gate * Get parent's offset in snapshot from the stack 17630Sstevel@tonic-gate * and store it in the current node 17640Sstevel@tonic-gate */ 17650Sstevel@tonic-gate if (dsp->depth > 1) { 17660Sstevel@tonic-gate me->parent = *(PARENT_OFFSET(dsp)); 17670Sstevel@tonic-gate } 17680Sstevel@tonic-gate 17690Sstevel@tonic-gate /* 17700Sstevel@tonic-gate * Save the offset of this di_node in a hash table. 17710Sstevel@tonic-gate * This is used later to resolve references to this 17720Sstevel@tonic-gate * dip from other parts of the tree (per-driver list, 17730Sstevel@tonic-gate * multipathing linkages, layered usage linkages). 17740Sstevel@tonic-gate * The key used for the hash table is derived from 17750Sstevel@tonic-gate * information in the dip. 17760Sstevel@tonic-gate */ 17770Sstevel@tonic-gate di_register_dip(st, (dev_info_t *)node, me->self); 17780Sstevel@tonic-gate 17790Sstevel@tonic-gate #ifdef DEVID_COMPATIBILITY 17800Sstevel@tonic-gate /* check for devid as property marker */ 17816640Scth if (node->devi_devid_str) { 17820Sstevel@tonic-gate ddi_devid_t devid; 17830Sstevel@tonic-gate 17840Sstevel@tonic-gate /* 17856640Scth * The devid is now represented as a property. For 17866640Scth * compatibility with di_devid() interface in libdevinfo we 17876640Scth * must return it as a binary structure in the snapshot. When 17886640Scth * (if) di_devid() is removed from libdevinfo then the code 17896640Scth * related to DEVID_COMPATIBILITY can be removed. 17900Sstevel@tonic-gate */ 17916640Scth if (ddi_devid_str_decode(node->devi_devid_str, &devid, NULL) == 17926640Scth DDI_SUCCESS) { 17937224Scth size = ddi_devid_sizeof(devid); 17947224Scth off = di_checkmem(st, off, size); 17956640Scth me->devid = off; 17967224Scth bcopy(devid, di_mem_addr(st, off), size); 17977224Scth off += size; 17986640Scth ddi_devid_free(devid); 17990Sstevel@tonic-gate } 18000Sstevel@tonic-gate } 18010Sstevel@tonic-gate #endif /* DEVID_COMPATIBILITY */ 18020Sstevel@tonic-gate 18030Sstevel@tonic-gate if (node->devi_node_name) { 18047224Scth size = strlen(node->devi_node_name) + 1; 18057224Scth me->node_name = off = di_checkmem(st, off, size); 18060Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), node->devi_node_name); 18077224Scth off += size; 18080Sstevel@tonic-gate } 18090Sstevel@tonic-gate 18100Sstevel@tonic-gate if (node->devi_compat_names && (node->devi_compat_length > 1)) { 18117224Scth size = node->devi_compat_length; 18127224Scth me->compat_names = off = di_checkmem(st, off, size); 18137224Scth me->compat_length = (int)size; 18147224Scth bcopy(node->devi_compat_names, di_mem_addr(st, off), size); 18157224Scth off += size; 18160Sstevel@tonic-gate } 18170Sstevel@tonic-gate 18180Sstevel@tonic-gate if (node->devi_addr) { 18197224Scth size = strlen(node->devi_addr) + 1; 18207224Scth me->address = off = di_checkmem(st, off, size); 18210Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), node->devi_addr); 18227224Scth off += size; 18230Sstevel@tonic-gate } 18240Sstevel@tonic-gate 18250Sstevel@tonic-gate if (node->devi_binding_name) { 18267224Scth size = strlen(node->devi_binding_name) + 1; 18277224Scth me->bind_name = off = di_checkmem(st, off, size); 18280Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), node->devi_binding_name); 18297224Scth off += size; 18300Sstevel@tonic-gate } 18310Sstevel@tonic-gate 18320Sstevel@tonic-gate me->drv_major = node->devi_major; 18330Sstevel@tonic-gate 18340Sstevel@tonic-gate /* 18350Sstevel@tonic-gate * If the dip is BOUND, set the next pointer of the 18360Sstevel@tonic-gate * per-instance list to -1, indicating that it is yet to be resolved. 18370Sstevel@tonic-gate * This will be resolved later in snap_driver_list(). 18380Sstevel@tonic-gate */ 18390Sstevel@tonic-gate if (me->drv_major != -1) { 18400Sstevel@tonic-gate me->next = -1; 18410Sstevel@tonic-gate } else { 18420Sstevel@tonic-gate me->next = 0; 18430Sstevel@tonic-gate } 18440Sstevel@tonic-gate 18450Sstevel@tonic-gate /* 18460Sstevel@tonic-gate * An optimization to skip mutex_enter when not needed. 18470Sstevel@tonic-gate */ 1848*10923SEvan.Yan@Sun.COM if (!((DINFOMINOR | DINFOPROP | DINFOPATH | DINFOHP) & st->command)) { 18490Sstevel@tonic-gate goto priv_data; 18500Sstevel@tonic-gate } 18510Sstevel@tonic-gate 18520Sstevel@tonic-gate /* 18537224Scth * LOCKING: We already have an active ndi_devi_enter to gather the 18547224Scth * minor data, and we will take devi_lock to gather properties as 18557224Scth * needed off di_getprop. 18560Sstevel@tonic-gate */ 18570Sstevel@tonic-gate if (!(DINFOMINOR & st->command)) { 18580Sstevel@tonic-gate goto path; 18590Sstevel@tonic-gate } 18600Sstevel@tonic-gate 18617224Scth ASSERT(DEVI_BUSY_OWNED(node)); 18620Sstevel@tonic-gate if (node->devi_minor) { /* minor data */ 18637224Scth me->minor_data = off; 18640Sstevel@tonic-gate off = di_getmdata(node->devi_minor, &me->minor_data, 18650Sstevel@tonic-gate me->self, st); 18660Sstevel@tonic-gate } 18670Sstevel@tonic-gate 18680Sstevel@tonic-gate path: 18690Sstevel@tonic-gate if (!(DINFOPATH & st->command)) { 18700Sstevel@tonic-gate goto property; 18710Sstevel@tonic-gate } 18720Sstevel@tonic-gate 1873893Srs135747 if (MDI_VHCI(node)) { 1874893Srs135747 me->multipath_component = MULTIPATH_COMPONENT_VHCI; 1875893Srs135747 } 1876893Srs135747 18770Sstevel@tonic-gate if (MDI_CLIENT(node)) { 1878893Srs135747 me->multipath_component = MULTIPATH_COMPONENT_CLIENT; 18797224Scth me->multipath_client = off; 18800Sstevel@tonic-gate off = di_getpath_data((dev_info_t *)node, &me->multipath_client, 18810Sstevel@tonic-gate me->self, st, 1); 18820Sstevel@tonic-gate dcmn_err((CE_WARN, "me->multipath_client = %x for node %p " 18830Sstevel@tonic-gate "component type = %d. off=%d", 18840Sstevel@tonic-gate me->multipath_client, 18850Sstevel@tonic-gate (void *)node, node->devi_mdi_component, off)); 18860Sstevel@tonic-gate } 18870Sstevel@tonic-gate 18880Sstevel@tonic-gate if (MDI_PHCI(node)) { 1889893Srs135747 me->multipath_component = MULTIPATH_COMPONENT_PHCI; 18907224Scth me->multipath_phci = off; 18910Sstevel@tonic-gate off = di_getpath_data((dev_info_t *)node, &me->multipath_phci, 18920Sstevel@tonic-gate me->self, st, 0); 18930Sstevel@tonic-gate dcmn_err((CE_WARN, "me->multipath_phci = %x for node %p " 18940Sstevel@tonic-gate "component type = %d. off=%d", 18950Sstevel@tonic-gate me->multipath_phci, 18960Sstevel@tonic-gate (void *)node, node->devi_mdi_component, off)); 18970Sstevel@tonic-gate } 18980Sstevel@tonic-gate 18990Sstevel@tonic-gate property: 19000Sstevel@tonic-gate if (!(DINFOPROP & st->command)) { 1901*10923SEvan.Yan@Sun.COM goto hotplug_data; 19020Sstevel@tonic-gate } 19030Sstevel@tonic-gate 19040Sstevel@tonic-gate if (node->devi_drv_prop_ptr) { /* driver property list */ 19057224Scth me->drv_prop = off; 19067224Scth off = di_getprop(DI_PROP_DRV_LIST, &node->devi_drv_prop_ptr, 19077224Scth &me->drv_prop, st, node); 19080Sstevel@tonic-gate } 19090Sstevel@tonic-gate 19100Sstevel@tonic-gate if (node->devi_sys_prop_ptr) { /* system property list */ 19117224Scth me->sys_prop = off; 19127224Scth off = di_getprop(DI_PROP_SYS_LIST, &node->devi_sys_prop_ptr, 19137224Scth &me->sys_prop, st, node); 19140Sstevel@tonic-gate } 19150Sstevel@tonic-gate 19160Sstevel@tonic-gate if (node->devi_hw_prop_ptr) { /* hardware property list */ 19177224Scth me->hw_prop = off; 19187224Scth off = di_getprop(DI_PROP_HW_LIST, &node->devi_hw_prop_ptr, 19197224Scth &me->hw_prop, st, node); 19200Sstevel@tonic-gate } 19210Sstevel@tonic-gate 19220Sstevel@tonic-gate if (node->devi_global_prop_list == NULL) { 19230Sstevel@tonic-gate me->glob_prop = (di_off_t)-1; /* not global property */ 19240Sstevel@tonic-gate } else { 19250Sstevel@tonic-gate /* 19260Sstevel@tonic-gate * Make copy of global property list if this devinfo refers 19270Sstevel@tonic-gate * global properties different from what's on the devnames 19280Sstevel@tonic-gate * array. It can happen if there has been a forced 19290Sstevel@tonic-gate * driver.conf update. See mod_drv(1M). 19300Sstevel@tonic-gate */ 19310Sstevel@tonic-gate ASSERT(me->drv_major != -1); 19320Sstevel@tonic-gate if (node->devi_global_prop_list != 19330Sstevel@tonic-gate devnamesp[me->drv_major].dn_global_prop_ptr) { 19347224Scth me->glob_prop = off; 19357224Scth off = di_getprop(DI_PROP_GLB_LIST, 19367224Scth &node->devi_global_prop_list->prop_list, 19377224Scth &me->glob_prop, st, node); 19380Sstevel@tonic-gate } 19390Sstevel@tonic-gate } 19400Sstevel@tonic-gate 1941*10923SEvan.Yan@Sun.COM hotplug_data: 1942*10923SEvan.Yan@Sun.COM if (!(DINFOHP & st->command)) { 1943*10923SEvan.Yan@Sun.COM goto priv_data; 1944*10923SEvan.Yan@Sun.COM } 1945*10923SEvan.Yan@Sun.COM 1946*10923SEvan.Yan@Sun.COM if (node->devi_hp_hdlp) { /* hotplug data */ 1947*10923SEvan.Yan@Sun.COM me->hp_data = off; 1948*10923SEvan.Yan@Sun.COM off = di_gethpdata(node->devi_hp_hdlp, &me->hp_data, st); 1949*10923SEvan.Yan@Sun.COM } 1950*10923SEvan.Yan@Sun.COM 19510Sstevel@tonic-gate priv_data: 19520Sstevel@tonic-gate if (!(DINFOPRIVDATA & st->command)) { 19530Sstevel@tonic-gate goto pm_info; 19540Sstevel@tonic-gate } 19550Sstevel@tonic-gate 19560Sstevel@tonic-gate if (ddi_get_parent_data((dev_info_t *)node) != NULL) { 19577224Scth me->parent_data = off; 19580Sstevel@tonic-gate off = di_getppdata(node, &me->parent_data, st); 19590Sstevel@tonic-gate } 19600Sstevel@tonic-gate 19610Sstevel@tonic-gate if (ddi_get_driver_private((dev_info_t *)node) != NULL) { 19627224Scth me->driver_data = off; 19630Sstevel@tonic-gate off = di_getdpdata(node, &me->driver_data, st); 19640Sstevel@tonic-gate } 19650Sstevel@tonic-gate 19660Sstevel@tonic-gate pm_info: /* NOT implemented */ 19670Sstevel@tonic-gate 19680Sstevel@tonic-gate subtree: 19697224Scth /* keep the stack aligned */ 19707224Scth off = DI_ALIGN(off); 19717224Scth 19720Sstevel@tonic-gate if (!(DINFOSUBTREE & st->command)) { 19730Sstevel@tonic-gate POP_STACK(dsp); 19747224Scth return (off); 19750Sstevel@tonic-gate } 19760Sstevel@tonic-gate 19770Sstevel@tonic-gate child: 19780Sstevel@tonic-gate /* 19798912SChris.Horne@Sun.COM * If there is a visible child--push child onto stack. 19808912SChris.Horne@Sun.COM * Hold the parent (me) busy while doing so. 19810Sstevel@tonic-gate */ 19828912SChris.Horne@Sun.COM if ((n = node->devi_child) != NULL) { 19838912SChris.Horne@Sun.COM /* skip hidden nodes */ 19848912SChris.Horne@Sun.COM while (n && ndi_dev_is_hidden_node((dev_info_t *)n)) 19858912SChris.Horne@Sun.COM n = n->devi_sibling; 19868912SChris.Horne@Sun.COM if (n) { 19878912SChris.Horne@Sun.COM me->child = off; 19888912SChris.Horne@Sun.COM PUSH_STACK(dsp, n, &me->child); 19898912SChris.Horne@Sun.COM return (me->child); 19908912SChris.Horne@Sun.COM } 19910Sstevel@tonic-gate } 19920Sstevel@tonic-gate 19930Sstevel@tonic-gate sibling: 19940Sstevel@tonic-gate /* 19958912SChris.Horne@Sun.COM * Done with any child nodes, unroll the stack till a visible 19968912SChris.Horne@Sun.COM * sibling of a parent node is found or root node is reached. 19970Sstevel@tonic-gate */ 19980Sstevel@tonic-gate POP_STACK(dsp); 19998912SChris.Horne@Sun.COM while (!EMPTY_STACK(dsp)) { 20008912SChris.Horne@Sun.COM if ((n = node->devi_sibling) != NULL) { 20018912SChris.Horne@Sun.COM /* skip hidden nodes */ 20028912SChris.Horne@Sun.COM while (n && ndi_dev_is_hidden_node((dev_info_t *)n)) 20038912SChris.Horne@Sun.COM n = n->devi_sibling; 20048912SChris.Horne@Sun.COM if (n) { 20058912SChris.Horne@Sun.COM me->sibling = DI_ALIGN(off); 20068912SChris.Horne@Sun.COM PUSH_STACK(dsp, n, &me->sibling); 20078912SChris.Horne@Sun.COM return (me->sibling); 20088912SChris.Horne@Sun.COM } 20098912SChris.Horne@Sun.COM } 20100Sstevel@tonic-gate node = TOP_NODE(dsp); 20110Sstevel@tonic-gate me = DI_NODE(di_mem_addr(st, *(TOP_OFFSET(dsp)))); 20120Sstevel@tonic-gate POP_STACK(dsp); 20130Sstevel@tonic-gate } 20140Sstevel@tonic-gate 20150Sstevel@tonic-gate /* 20160Sstevel@tonic-gate * DONE with all nodes 20170Sstevel@tonic-gate */ 20187224Scth return (off); 20190Sstevel@tonic-gate } 20200Sstevel@tonic-gate 20210Sstevel@tonic-gate static i_lnode_t * 20220Sstevel@tonic-gate i_lnode_alloc(int modid) 20230Sstevel@tonic-gate { 20240Sstevel@tonic-gate i_lnode_t *i_lnode; 20250Sstevel@tonic-gate 20260Sstevel@tonic-gate i_lnode = kmem_zalloc(sizeof (i_lnode_t), KM_SLEEP); 20270Sstevel@tonic-gate 20280Sstevel@tonic-gate ASSERT(modid != -1); 20290Sstevel@tonic-gate i_lnode->modid = modid; 20300Sstevel@tonic-gate 20310Sstevel@tonic-gate return (i_lnode); 20320Sstevel@tonic-gate } 20330Sstevel@tonic-gate 20340Sstevel@tonic-gate static void 20350Sstevel@tonic-gate i_lnode_free(i_lnode_t *i_lnode) 20360Sstevel@tonic-gate { 20370Sstevel@tonic-gate kmem_free(i_lnode, sizeof (i_lnode_t)); 20380Sstevel@tonic-gate } 20390Sstevel@tonic-gate 20400Sstevel@tonic-gate static void 20410Sstevel@tonic-gate i_lnode_check_free(i_lnode_t *i_lnode) 20420Sstevel@tonic-gate { 20430Sstevel@tonic-gate /* This lnode and its dip must have been snapshotted */ 20440Sstevel@tonic-gate ASSERT(i_lnode->self > 0); 20450Sstevel@tonic-gate ASSERT(i_lnode->di_node->self > 0); 20460Sstevel@tonic-gate 20470Sstevel@tonic-gate /* at least 1 link (in or out) must exist for this lnode */ 20480Sstevel@tonic-gate ASSERT(i_lnode->link_in || i_lnode->link_out); 20490Sstevel@tonic-gate 20500Sstevel@tonic-gate i_lnode_free(i_lnode); 20510Sstevel@tonic-gate } 20520Sstevel@tonic-gate 20530Sstevel@tonic-gate static i_link_t * 20540Sstevel@tonic-gate i_link_alloc(int spec_type) 20550Sstevel@tonic-gate { 20567224Scth i_link_t *i_link; 20570Sstevel@tonic-gate 20580Sstevel@tonic-gate i_link = kmem_zalloc(sizeof (i_link_t), KM_SLEEP); 20590Sstevel@tonic-gate i_link->spec_type = spec_type; 20600Sstevel@tonic-gate 20610Sstevel@tonic-gate return (i_link); 20620Sstevel@tonic-gate } 20630Sstevel@tonic-gate 20640Sstevel@tonic-gate static void 20650Sstevel@tonic-gate i_link_check_free(i_link_t *i_link) 20660Sstevel@tonic-gate { 20670Sstevel@tonic-gate /* This link must have been snapshotted */ 20680Sstevel@tonic-gate ASSERT(i_link->self > 0); 20690Sstevel@tonic-gate 20700Sstevel@tonic-gate /* Both endpoint lnodes must exist for this link */ 20710Sstevel@tonic-gate ASSERT(i_link->src_lnode); 20720Sstevel@tonic-gate ASSERT(i_link->tgt_lnode); 20730Sstevel@tonic-gate 20740Sstevel@tonic-gate kmem_free(i_link, sizeof (i_link_t)); 20750Sstevel@tonic-gate } 20760Sstevel@tonic-gate 20770Sstevel@tonic-gate /*ARGSUSED*/ 20780Sstevel@tonic-gate static uint_t 20790Sstevel@tonic-gate i_lnode_hashfunc(void *arg, mod_hash_key_t key) 20800Sstevel@tonic-gate { 20810Sstevel@tonic-gate i_lnode_t *i_lnode = (i_lnode_t *)key; 20820Sstevel@tonic-gate struct di_node *ptr; 20830Sstevel@tonic-gate dev_t dev; 20840Sstevel@tonic-gate 20850Sstevel@tonic-gate dev = i_lnode->devt; 20860Sstevel@tonic-gate if (dev != DDI_DEV_T_NONE) 20870Sstevel@tonic-gate return (i_lnode->modid + getminor(dev) + getmajor(dev)); 20880Sstevel@tonic-gate 20890Sstevel@tonic-gate ptr = i_lnode->di_node; 20900Sstevel@tonic-gate ASSERT(ptr->self > 0); 20910Sstevel@tonic-gate if (ptr) { 20920Sstevel@tonic-gate uintptr_t k = (uintptr_t)ptr; 20930Sstevel@tonic-gate k >>= (int)highbit(sizeof (struct di_node)); 20940Sstevel@tonic-gate return ((uint_t)k); 20950Sstevel@tonic-gate } 20960Sstevel@tonic-gate 20970Sstevel@tonic-gate return (i_lnode->modid); 20980Sstevel@tonic-gate } 20990Sstevel@tonic-gate 21000Sstevel@tonic-gate static int 21010Sstevel@tonic-gate i_lnode_cmp(void *arg1, void *arg2) 21020Sstevel@tonic-gate { 21030Sstevel@tonic-gate i_lnode_t *i_lnode1 = (i_lnode_t *)arg1; 21040Sstevel@tonic-gate i_lnode_t *i_lnode2 = (i_lnode_t *)arg2; 21050Sstevel@tonic-gate 21060Sstevel@tonic-gate if (i_lnode1->modid != i_lnode2->modid) { 21070Sstevel@tonic-gate return ((i_lnode1->modid < i_lnode2->modid) ? -1 : 1); 21080Sstevel@tonic-gate } 21090Sstevel@tonic-gate 21100Sstevel@tonic-gate if (i_lnode1->di_node != i_lnode2->di_node) 21110Sstevel@tonic-gate return ((i_lnode1->di_node < i_lnode2->di_node) ? -1 : 1); 21120Sstevel@tonic-gate 21130Sstevel@tonic-gate if (i_lnode1->devt != i_lnode2->devt) 21140Sstevel@tonic-gate return ((i_lnode1->devt < i_lnode2->devt) ? -1 : 1); 21150Sstevel@tonic-gate 21160Sstevel@tonic-gate return (0); 21170Sstevel@tonic-gate } 21180Sstevel@tonic-gate 21190Sstevel@tonic-gate /* 21200Sstevel@tonic-gate * An lnode represents a {dip, dev_t} tuple. A link represents a 21210Sstevel@tonic-gate * {src_lnode, tgt_lnode, spec_type} tuple. 21220Sstevel@tonic-gate * The following callback assumes that LDI framework ref-counts the 21230Sstevel@tonic-gate * src_dip and tgt_dip while invoking this callback. 21240Sstevel@tonic-gate */ 21250Sstevel@tonic-gate static int 21260Sstevel@tonic-gate di_ldi_callback(const ldi_usage_t *ldi_usage, void *arg) 21270Sstevel@tonic-gate { 21280Sstevel@tonic-gate struct di_state *st = (struct di_state *)arg; 21290Sstevel@tonic-gate i_lnode_t *src_lnode, *tgt_lnode, *i_lnode; 21300Sstevel@tonic-gate i_link_t **i_link_next, *i_link; 21310Sstevel@tonic-gate di_off_t soff, toff; 21320Sstevel@tonic-gate mod_hash_val_t nodep = NULL; 21330Sstevel@tonic-gate int res; 21340Sstevel@tonic-gate 21350Sstevel@tonic-gate /* 21360Sstevel@tonic-gate * if the source or target of this device usage information doesn't 21377224Scth * correspond to a device node then we don't report it via 21380Sstevel@tonic-gate * libdevinfo so return. 21390Sstevel@tonic-gate */ 21400Sstevel@tonic-gate if ((ldi_usage->src_dip == NULL) || (ldi_usage->tgt_dip == NULL)) 21410Sstevel@tonic-gate return (LDI_USAGE_CONTINUE); 21420Sstevel@tonic-gate 21430Sstevel@tonic-gate ASSERT(e_ddi_devi_holdcnt(ldi_usage->src_dip)); 21440Sstevel@tonic-gate ASSERT(e_ddi_devi_holdcnt(ldi_usage->tgt_dip)); 21450Sstevel@tonic-gate 21460Sstevel@tonic-gate /* 21470Sstevel@tonic-gate * Skip the ldi_usage if either src or tgt dip is not in the 21480Sstevel@tonic-gate * snapshot. This saves us from pruning bad lnodes/links later. 21490Sstevel@tonic-gate */ 21500Sstevel@tonic-gate if (di_dip_find(st, ldi_usage->src_dip, &soff) != 0) 21510Sstevel@tonic-gate return (LDI_USAGE_CONTINUE); 21520Sstevel@tonic-gate if (di_dip_find(st, ldi_usage->tgt_dip, &toff) != 0) 21530Sstevel@tonic-gate return (LDI_USAGE_CONTINUE); 21540Sstevel@tonic-gate 21550Sstevel@tonic-gate ASSERT(soff > 0); 21560Sstevel@tonic-gate ASSERT(toff > 0); 21570Sstevel@tonic-gate 21580Sstevel@tonic-gate /* 21590Sstevel@tonic-gate * allocate an i_lnode and add it to the lnode hash 21600Sstevel@tonic-gate * if it is not already present. For this particular 21610Sstevel@tonic-gate * link the lnode is a source, but it may 21620Sstevel@tonic-gate * participate as tgt or src in any number of layered 21630Sstevel@tonic-gate * operations - so it may already be in the hash. 21640Sstevel@tonic-gate */ 21650Sstevel@tonic-gate i_lnode = i_lnode_alloc(ldi_usage->src_modid); 21667224Scth i_lnode->di_node = DI_NODE(di_mem_addr(st, soff)); 21670Sstevel@tonic-gate i_lnode->devt = ldi_usage->src_devt; 21680Sstevel@tonic-gate 21690Sstevel@tonic-gate res = mod_hash_find(st->lnode_hash, i_lnode, &nodep); 21700Sstevel@tonic-gate if (res == MH_ERR_NOTFOUND) { 21710Sstevel@tonic-gate /* 21720Sstevel@tonic-gate * new i_lnode 21730Sstevel@tonic-gate * add it to the hash and increment the lnode count 21740Sstevel@tonic-gate */ 21750Sstevel@tonic-gate res = mod_hash_insert(st->lnode_hash, i_lnode, i_lnode); 21760Sstevel@tonic-gate ASSERT(res == 0); 21770Sstevel@tonic-gate st->lnode_count++; 21780Sstevel@tonic-gate src_lnode = i_lnode; 21790Sstevel@tonic-gate } else { 21800Sstevel@tonic-gate /* this i_lnode already exists in the lnode_hash */ 21810Sstevel@tonic-gate i_lnode_free(i_lnode); 21820Sstevel@tonic-gate src_lnode = (i_lnode_t *)nodep; 21830Sstevel@tonic-gate } 21840Sstevel@tonic-gate 21850Sstevel@tonic-gate /* 21860Sstevel@tonic-gate * allocate a tgt i_lnode and add it to the lnode hash 21870Sstevel@tonic-gate */ 21880Sstevel@tonic-gate i_lnode = i_lnode_alloc(ldi_usage->tgt_modid); 21897224Scth i_lnode->di_node = DI_NODE(di_mem_addr(st, toff)); 21900Sstevel@tonic-gate i_lnode->devt = ldi_usage->tgt_devt; 21910Sstevel@tonic-gate 21920Sstevel@tonic-gate res = mod_hash_find(st->lnode_hash, i_lnode, &nodep); 21930Sstevel@tonic-gate if (res == MH_ERR_NOTFOUND) { 21940Sstevel@tonic-gate /* 21950Sstevel@tonic-gate * new i_lnode 21960Sstevel@tonic-gate * add it to the hash and increment the lnode count 21970Sstevel@tonic-gate */ 21980Sstevel@tonic-gate res = mod_hash_insert(st->lnode_hash, i_lnode, i_lnode); 21990Sstevel@tonic-gate ASSERT(res == 0); 22000Sstevel@tonic-gate st->lnode_count++; 22010Sstevel@tonic-gate tgt_lnode = i_lnode; 22020Sstevel@tonic-gate } else { 22030Sstevel@tonic-gate /* this i_lnode already exists in the lnode_hash */ 22040Sstevel@tonic-gate i_lnode_free(i_lnode); 22050Sstevel@tonic-gate tgt_lnode = (i_lnode_t *)nodep; 22060Sstevel@tonic-gate } 22070Sstevel@tonic-gate 22080Sstevel@tonic-gate /* 22090Sstevel@tonic-gate * allocate a i_link 22100Sstevel@tonic-gate */ 22110Sstevel@tonic-gate i_link = i_link_alloc(ldi_usage->tgt_spec_type); 22120Sstevel@tonic-gate i_link->src_lnode = src_lnode; 22130Sstevel@tonic-gate i_link->tgt_lnode = tgt_lnode; 22140Sstevel@tonic-gate 22150Sstevel@tonic-gate /* 22160Sstevel@tonic-gate * add this link onto the src i_lnodes outbound i_link list 22170Sstevel@tonic-gate */ 22180Sstevel@tonic-gate i_link_next = &(src_lnode->link_out); 22190Sstevel@tonic-gate while (*i_link_next != NULL) { 22200Sstevel@tonic-gate if ((i_lnode_cmp(tgt_lnode, (*i_link_next)->tgt_lnode) == 0) && 22210Sstevel@tonic-gate (i_link->spec_type == (*i_link_next)->spec_type)) { 22220Sstevel@tonic-gate /* this link already exists */ 22230Sstevel@tonic-gate kmem_free(i_link, sizeof (i_link_t)); 22240Sstevel@tonic-gate return (LDI_USAGE_CONTINUE); 22250Sstevel@tonic-gate } 22260Sstevel@tonic-gate i_link_next = &((*i_link_next)->src_link_next); 22270Sstevel@tonic-gate } 22280Sstevel@tonic-gate *i_link_next = i_link; 22290Sstevel@tonic-gate 22300Sstevel@tonic-gate /* 22310Sstevel@tonic-gate * add this link onto the tgt i_lnodes inbound i_link list 22320Sstevel@tonic-gate */ 22330Sstevel@tonic-gate i_link_next = &(tgt_lnode->link_in); 22340Sstevel@tonic-gate while (*i_link_next != NULL) { 22350Sstevel@tonic-gate ASSERT(i_lnode_cmp(src_lnode, (*i_link_next)->src_lnode) != 0); 22360Sstevel@tonic-gate i_link_next = &((*i_link_next)->tgt_link_next); 22370Sstevel@tonic-gate } 22380Sstevel@tonic-gate *i_link_next = i_link; 22390Sstevel@tonic-gate 22400Sstevel@tonic-gate /* 22410Sstevel@tonic-gate * add this i_link to the link hash 22420Sstevel@tonic-gate */ 22430Sstevel@tonic-gate res = mod_hash_insert(st->link_hash, i_link, i_link); 22440Sstevel@tonic-gate ASSERT(res == 0); 22450Sstevel@tonic-gate st->link_count++; 22460Sstevel@tonic-gate 22470Sstevel@tonic-gate return (LDI_USAGE_CONTINUE); 22480Sstevel@tonic-gate } 22490Sstevel@tonic-gate 22500Sstevel@tonic-gate struct i_layer_data { 22510Sstevel@tonic-gate struct di_state *st; 22520Sstevel@tonic-gate int lnode_count; 22530Sstevel@tonic-gate int link_count; 22540Sstevel@tonic-gate di_off_t lnode_off; 22558912SChris.Horne@Sun.COM di_off_t link_off; 22560Sstevel@tonic-gate }; 22570Sstevel@tonic-gate 22580Sstevel@tonic-gate /*ARGSUSED*/ 22590Sstevel@tonic-gate static uint_t 22600Sstevel@tonic-gate i_link_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg) 22610Sstevel@tonic-gate { 22620Sstevel@tonic-gate i_link_t *i_link = (i_link_t *)key; 22630Sstevel@tonic-gate struct i_layer_data *data = arg; 22640Sstevel@tonic-gate struct di_link *me; 22650Sstevel@tonic-gate struct di_lnode *melnode; 22660Sstevel@tonic-gate struct di_node *medinode; 22670Sstevel@tonic-gate 22680Sstevel@tonic-gate ASSERT(i_link->self == 0); 22690Sstevel@tonic-gate 22700Sstevel@tonic-gate i_link->self = data->link_off + 22710Sstevel@tonic-gate (data->link_count * sizeof (struct di_link)); 22720Sstevel@tonic-gate data->link_count++; 22730Sstevel@tonic-gate 22740Sstevel@tonic-gate ASSERT(data->link_off > 0 && data->link_count > 0); 22750Sstevel@tonic-gate ASSERT(data->lnode_count == data->st->lnode_count); /* lnodes done */ 22760Sstevel@tonic-gate ASSERT(data->link_count <= data->st->link_count); 22770Sstevel@tonic-gate 22780Sstevel@tonic-gate /* fill in fields for the di_link snapshot */ 22797224Scth me = DI_LINK(di_mem_addr(data->st, i_link->self)); 22800Sstevel@tonic-gate me->self = i_link->self; 22810Sstevel@tonic-gate me->spec_type = i_link->spec_type; 22820Sstevel@tonic-gate 22830Sstevel@tonic-gate /* 22840Sstevel@tonic-gate * The src_lnode and tgt_lnode i_lnode_t for this i_link_t 22850Sstevel@tonic-gate * are created during the LDI table walk. Since we are 22860Sstevel@tonic-gate * walking the link hash, the lnode hash has already been 22870Sstevel@tonic-gate * walked and the lnodes have been snapshotted. Save lnode 22880Sstevel@tonic-gate * offsets. 22890Sstevel@tonic-gate */ 22900Sstevel@tonic-gate me->src_lnode = i_link->src_lnode->self; 22910Sstevel@tonic-gate me->tgt_lnode = i_link->tgt_lnode->self; 22920Sstevel@tonic-gate 22930Sstevel@tonic-gate /* 22940Sstevel@tonic-gate * Save this link's offset in the src_lnode snapshot's link_out 22950Sstevel@tonic-gate * field 22960Sstevel@tonic-gate */ 22977224Scth melnode = DI_LNODE(di_mem_addr(data->st, me->src_lnode)); 22980Sstevel@tonic-gate me->src_link_next = melnode->link_out; 22990Sstevel@tonic-gate melnode->link_out = me->self; 23000Sstevel@tonic-gate 23010Sstevel@tonic-gate /* 23020Sstevel@tonic-gate * Put this link on the tgt_lnode's link_in field 23030Sstevel@tonic-gate */ 23047224Scth melnode = DI_LNODE(di_mem_addr(data->st, me->tgt_lnode)); 23050Sstevel@tonic-gate me->tgt_link_next = melnode->link_in; 23060Sstevel@tonic-gate melnode->link_in = me->self; 23070Sstevel@tonic-gate 23080Sstevel@tonic-gate /* 23090Sstevel@tonic-gate * An i_lnode_t is only created if the corresponding dip exists 23100Sstevel@tonic-gate * in the snapshot. A pointer to the di_node is saved in the 23110Sstevel@tonic-gate * i_lnode_t when it is allocated. For this link, get the di_node 23120Sstevel@tonic-gate * for the source lnode. Then put the link on the di_node's list 23130Sstevel@tonic-gate * of src links 23140Sstevel@tonic-gate */ 23150Sstevel@tonic-gate medinode = i_link->src_lnode->di_node; 23160Sstevel@tonic-gate me->src_node_next = medinode->src_links; 23170Sstevel@tonic-gate medinode->src_links = me->self; 23180Sstevel@tonic-gate 23190Sstevel@tonic-gate /* 23200Sstevel@tonic-gate * Put this link on the tgt_links list of the target 23210Sstevel@tonic-gate * dip. 23220Sstevel@tonic-gate */ 23230Sstevel@tonic-gate medinode = i_link->tgt_lnode->di_node; 23240Sstevel@tonic-gate me->tgt_node_next = medinode->tgt_links; 23250Sstevel@tonic-gate medinode->tgt_links = me->self; 23260Sstevel@tonic-gate 23270Sstevel@tonic-gate return (MH_WALK_CONTINUE); 23280Sstevel@tonic-gate } 23290Sstevel@tonic-gate 23300Sstevel@tonic-gate /*ARGSUSED*/ 23310Sstevel@tonic-gate static uint_t 23320Sstevel@tonic-gate i_lnode_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg) 23330Sstevel@tonic-gate { 23340Sstevel@tonic-gate i_lnode_t *i_lnode = (i_lnode_t *)key; 23350Sstevel@tonic-gate struct i_layer_data *data = arg; 23360Sstevel@tonic-gate struct di_lnode *me; 23370Sstevel@tonic-gate struct di_node *medinode; 23380Sstevel@tonic-gate 23390Sstevel@tonic-gate ASSERT(i_lnode->self == 0); 23400Sstevel@tonic-gate 23410Sstevel@tonic-gate i_lnode->self = data->lnode_off + 23420Sstevel@tonic-gate (data->lnode_count * sizeof (struct di_lnode)); 23430Sstevel@tonic-gate data->lnode_count++; 23440Sstevel@tonic-gate 23450Sstevel@tonic-gate ASSERT(data->lnode_off > 0 && data->lnode_count > 0); 23460Sstevel@tonic-gate ASSERT(data->link_count == 0); /* links not done yet */ 23470Sstevel@tonic-gate ASSERT(data->lnode_count <= data->st->lnode_count); 23480Sstevel@tonic-gate 23490Sstevel@tonic-gate /* fill in fields for the di_lnode snapshot */ 23507224Scth me = DI_LNODE(di_mem_addr(data->st, i_lnode->self)); 23510Sstevel@tonic-gate me->self = i_lnode->self; 23520Sstevel@tonic-gate 23530Sstevel@tonic-gate if (i_lnode->devt == DDI_DEV_T_NONE) { 23547009Scth me->dev_major = DDI_MAJOR_T_NONE; 23557009Scth me->dev_minor = DDI_MAJOR_T_NONE; 23560Sstevel@tonic-gate } else { 23570Sstevel@tonic-gate me->dev_major = getmajor(i_lnode->devt); 23580Sstevel@tonic-gate me->dev_minor = getminor(i_lnode->devt); 23590Sstevel@tonic-gate } 23600Sstevel@tonic-gate 23610Sstevel@tonic-gate /* 23620Sstevel@tonic-gate * The dip corresponding to this lnode must exist in 23630Sstevel@tonic-gate * the snapshot or we wouldn't have created the i_lnode_t 23640Sstevel@tonic-gate * during LDI walk. Save the offset of the dip. 23650Sstevel@tonic-gate */ 23660Sstevel@tonic-gate ASSERT(i_lnode->di_node && i_lnode->di_node->self > 0); 23670Sstevel@tonic-gate me->node = i_lnode->di_node->self; 23680Sstevel@tonic-gate 23690Sstevel@tonic-gate /* 23700Sstevel@tonic-gate * There must be at least one link in or out of this lnode 23710Sstevel@tonic-gate * or we wouldn't have created it. These fields will be set 23720Sstevel@tonic-gate * during the link hash walk. 23730Sstevel@tonic-gate */ 23740Sstevel@tonic-gate ASSERT((i_lnode->link_in != NULL) || (i_lnode->link_out != NULL)); 23750Sstevel@tonic-gate 23760Sstevel@tonic-gate /* 23770Sstevel@tonic-gate * set the offset of the devinfo node associated with this 23780Sstevel@tonic-gate * lnode. Also update the node_next next pointer. this pointer 23790Sstevel@tonic-gate * is set if there are multiple lnodes associated with the same 23800Sstevel@tonic-gate * devinfo node. (could occure when multiple minor nodes 23810Sstevel@tonic-gate * are open for one device, etc.) 23820Sstevel@tonic-gate */ 23830Sstevel@tonic-gate medinode = i_lnode->di_node; 23840Sstevel@tonic-gate me->node_next = medinode->lnodes; 23850Sstevel@tonic-gate medinode->lnodes = me->self; 23860Sstevel@tonic-gate 23870Sstevel@tonic-gate return (MH_WALK_CONTINUE); 23880Sstevel@tonic-gate } 23890Sstevel@tonic-gate 23900Sstevel@tonic-gate static di_off_t 23910Sstevel@tonic-gate di_getlink_data(di_off_t off, struct di_state *st) 23920Sstevel@tonic-gate { 23937224Scth struct i_layer_data data = {0}; 23947224Scth size_t size; 23950Sstevel@tonic-gate 23960Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_copylyr: off = %x\n", off)); 23970Sstevel@tonic-gate 23980Sstevel@tonic-gate st->lnode_hash = mod_hash_create_extended("di_lnode_hash", 32, 23990Sstevel@tonic-gate mod_hash_null_keydtor, (void (*)(mod_hash_val_t))i_lnode_check_free, 24000Sstevel@tonic-gate i_lnode_hashfunc, NULL, i_lnode_cmp, KM_SLEEP); 24010Sstevel@tonic-gate 24020Sstevel@tonic-gate st->link_hash = mod_hash_create_ptrhash("di_link_hash", 32, 24030Sstevel@tonic-gate (void (*)(mod_hash_val_t))i_link_check_free, sizeof (i_link_t)); 24040Sstevel@tonic-gate 24050Sstevel@tonic-gate /* get driver layering information */ 24060Sstevel@tonic-gate (void) ldi_usage_walker(st, di_ldi_callback); 24070Sstevel@tonic-gate 24080Sstevel@tonic-gate /* check if there is any link data to include in the snapshot */ 24090Sstevel@tonic-gate if (st->lnode_count == 0) { 24100Sstevel@tonic-gate ASSERT(st->link_count == 0); 24110Sstevel@tonic-gate goto out; 24120Sstevel@tonic-gate } 24130Sstevel@tonic-gate 24140Sstevel@tonic-gate ASSERT(st->link_count != 0); 24150Sstevel@tonic-gate 24160Sstevel@tonic-gate /* get a pointer to snapshot memory for all the di_lnodes */ 24170Sstevel@tonic-gate size = sizeof (struct di_lnode) * st->lnode_count; 24180Sstevel@tonic-gate data.lnode_off = off = di_checkmem(st, off, size); 24197224Scth off += size; 24200Sstevel@tonic-gate 24210Sstevel@tonic-gate /* get a pointer to snapshot memory for all the di_links */ 24220Sstevel@tonic-gate size = sizeof (struct di_link) * st->link_count; 24230Sstevel@tonic-gate data.link_off = off = di_checkmem(st, off, size); 24247224Scth off += size; 24250Sstevel@tonic-gate 24260Sstevel@tonic-gate data.lnode_count = data.link_count = 0; 24270Sstevel@tonic-gate data.st = st; 24280Sstevel@tonic-gate 24290Sstevel@tonic-gate /* 24300Sstevel@tonic-gate * We have lnodes and links that will go into the 24310Sstevel@tonic-gate * snapshot, so let's walk the respective hashes 24320Sstevel@tonic-gate * and snapshot them. The various linkages are 24330Sstevel@tonic-gate * also set up during the walk. 24340Sstevel@tonic-gate */ 24350Sstevel@tonic-gate mod_hash_walk(st->lnode_hash, i_lnode_walker, (void *)&data); 24360Sstevel@tonic-gate ASSERT(data.lnode_count == st->lnode_count); 24370Sstevel@tonic-gate 24380Sstevel@tonic-gate mod_hash_walk(st->link_hash, i_link_walker, (void *)&data); 24390Sstevel@tonic-gate ASSERT(data.link_count == st->link_count); 24400Sstevel@tonic-gate 24410Sstevel@tonic-gate out: 24420Sstevel@tonic-gate /* free up the i_lnodes and i_links used to create the snapshot */ 24430Sstevel@tonic-gate mod_hash_destroy_hash(st->lnode_hash); 24440Sstevel@tonic-gate mod_hash_destroy_hash(st->link_hash); 24450Sstevel@tonic-gate st->lnode_count = 0; 24460Sstevel@tonic-gate st->link_count = 0; 24470Sstevel@tonic-gate 24480Sstevel@tonic-gate return (off); 24490Sstevel@tonic-gate } 24500Sstevel@tonic-gate 24510Sstevel@tonic-gate 24520Sstevel@tonic-gate /* 24530Sstevel@tonic-gate * Copy all minor data nodes attached to a devinfo node into the snapshot. 24547224Scth * It is called from di_copynode with active ndi_devi_enter to protect 24557224Scth * the list of minor nodes. 24560Sstevel@tonic-gate */ 24570Sstevel@tonic-gate static di_off_t 24580Sstevel@tonic-gate di_getmdata(struct ddi_minor_data *mnode, di_off_t *off_p, di_off_t node, 24590Sstevel@tonic-gate struct di_state *st) 24600Sstevel@tonic-gate { 24617224Scth di_off_t off; 24627224Scth struct di_minor *me; 24637224Scth size_t size; 24640Sstevel@tonic-gate 24650Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_getmdata:\n")); 24660Sstevel@tonic-gate 24670Sstevel@tonic-gate /* 24680Sstevel@tonic-gate * check memory first 24690Sstevel@tonic-gate */ 24700Sstevel@tonic-gate off = di_checkmem(st, *off_p, sizeof (struct di_minor)); 24710Sstevel@tonic-gate *off_p = off; 24720Sstevel@tonic-gate 24730Sstevel@tonic-gate do { 24747224Scth me = DI_MINOR(di_mem_addr(st, off)); 24750Sstevel@tonic-gate me->self = off; 24760Sstevel@tonic-gate me->type = mnode->type; 24770Sstevel@tonic-gate me->node = node; 24780Sstevel@tonic-gate me->user_private_data = NULL; 24790Sstevel@tonic-gate 24807224Scth off += sizeof (struct di_minor); 24810Sstevel@tonic-gate 24820Sstevel@tonic-gate /* 24830Sstevel@tonic-gate * Split dev_t to major/minor, so it works for 24840Sstevel@tonic-gate * both ILP32 and LP64 model 24850Sstevel@tonic-gate */ 24860Sstevel@tonic-gate me->dev_major = getmajor(mnode->ddm_dev); 24870Sstevel@tonic-gate me->dev_minor = getminor(mnode->ddm_dev); 24880Sstevel@tonic-gate me->spec_type = mnode->ddm_spec_type; 24890Sstevel@tonic-gate 24900Sstevel@tonic-gate if (mnode->ddm_name) { 24917224Scth size = strlen(mnode->ddm_name) + 1; 24927224Scth me->name = off = di_checkmem(st, off, size); 24930Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), mnode->ddm_name); 24947224Scth off += size; 24950Sstevel@tonic-gate } 24960Sstevel@tonic-gate 24970Sstevel@tonic-gate if (mnode->ddm_node_type) { 24987224Scth size = strlen(mnode->ddm_node_type) + 1; 24997224Scth me->node_type = off = di_checkmem(st, off, size); 25000Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), 25014870Sjg mnode->ddm_node_type); 25027224Scth off += size; 25030Sstevel@tonic-gate } 25040Sstevel@tonic-gate 25050Sstevel@tonic-gate off = di_checkmem(st, off, sizeof (struct di_minor)); 25060Sstevel@tonic-gate me->next = off; 25070Sstevel@tonic-gate mnode = mnode->next; 25080Sstevel@tonic-gate } while (mnode); 25090Sstevel@tonic-gate 25100Sstevel@tonic-gate me->next = 0; 25110Sstevel@tonic-gate 25120Sstevel@tonic-gate return (off); 25130Sstevel@tonic-gate } 25140Sstevel@tonic-gate 25150Sstevel@tonic-gate /* 25160Sstevel@tonic-gate * di_register_dip(), di_find_dip(): The dip must be protected 25170Sstevel@tonic-gate * from deallocation when using these routines - this can either 25180Sstevel@tonic-gate * be a reference count, a busy hold or a per-driver lock. 25190Sstevel@tonic-gate */ 25200Sstevel@tonic-gate 25210Sstevel@tonic-gate static void 25220Sstevel@tonic-gate di_register_dip(struct di_state *st, dev_info_t *dip, di_off_t off) 25230Sstevel@tonic-gate { 25247224Scth struct dev_info *node = DEVI(dip); 25257224Scth struct di_key *key = kmem_zalloc(sizeof (*key), KM_SLEEP); 25267224Scth struct di_dkey *dk; 25270Sstevel@tonic-gate 25280Sstevel@tonic-gate ASSERT(dip); 25290Sstevel@tonic-gate ASSERT(off > 0); 25300Sstevel@tonic-gate 25310Sstevel@tonic-gate key->k_type = DI_DKEY; 25320Sstevel@tonic-gate dk = &(key->k_u.dkey); 25330Sstevel@tonic-gate 25340Sstevel@tonic-gate dk->dk_dip = dip; 25350Sstevel@tonic-gate dk->dk_major = node->devi_major; 25360Sstevel@tonic-gate dk->dk_inst = node->devi_instance; 25370Sstevel@tonic-gate dk->dk_nodeid = node->devi_nodeid; 25380Sstevel@tonic-gate 25390Sstevel@tonic-gate if (mod_hash_insert(st->reg_dip_hash, (mod_hash_key_t)key, 25400Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)off) != 0) { 25410Sstevel@tonic-gate panic( 25420Sstevel@tonic-gate "duplicate devinfo (%p) registered during device " 25430Sstevel@tonic-gate "tree walk", (void *)dip); 25440Sstevel@tonic-gate } 25450Sstevel@tonic-gate } 25460Sstevel@tonic-gate 25470Sstevel@tonic-gate 25480Sstevel@tonic-gate static int 25490Sstevel@tonic-gate di_dip_find(struct di_state *st, dev_info_t *dip, di_off_t *off_p) 25500Sstevel@tonic-gate { 25510Sstevel@tonic-gate /* 25520Sstevel@tonic-gate * uintptr_t must be used because it matches the size of void *; 25530Sstevel@tonic-gate * mod_hash expects clients to place results into pointer-size 25540Sstevel@tonic-gate * containers; since di_off_t is always a 32-bit offset, alignment 25550Sstevel@tonic-gate * would otherwise be broken on 64-bit kernels. 25560Sstevel@tonic-gate */ 25570Sstevel@tonic-gate uintptr_t offset; 25580Sstevel@tonic-gate struct di_key key = {0}; 25590Sstevel@tonic-gate struct di_dkey *dk; 25600Sstevel@tonic-gate 25610Sstevel@tonic-gate ASSERT(st->reg_dip_hash); 25620Sstevel@tonic-gate ASSERT(dip); 25630Sstevel@tonic-gate ASSERT(off_p); 25640Sstevel@tonic-gate 25650Sstevel@tonic-gate 25660Sstevel@tonic-gate key.k_type = DI_DKEY; 25670Sstevel@tonic-gate dk = &(key.k_u.dkey); 25680Sstevel@tonic-gate 25690Sstevel@tonic-gate dk->dk_dip = dip; 25700Sstevel@tonic-gate dk->dk_major = DEVI(dip)->devi_major; 25710Sstevel@tonic-gate dk->dk_inst = DEVI(dip)->devi_instance; 25720Sstevel@tonic-gate dk->dk_nodeid = DEVI(dip)->devi_nodeid; 25730Sstevel@tonic-gate 25740Sstevel@tonic-gate if (mod_hash_find(st->reg_dip_hash, (mod_hash_key_t)&key, 25750Sstevel@tonic-gate (mod_hash_val_t *)&offset) == 0) { 25760Sstevel@tonic-gate *off_p = (di_off_t)offset; 25770Sstevel@tonic-gate return (0); 25780Sstevel@tonic-gate } else { 25790Sstevel@tonic-gate return (-1); 25800Sstevel@tonic-gate } 25810Sstevel@tonic-gate } 25820Sstevel@tonic-gate 25830Sstevel@tonic-gate /* 25840Sstevel@tonic-gate * di_register_pip(), di_find_pip(): The pip must be protected from deallocation 25850Sstevel@tonic-gate * when using these routines. The caller must do this by protecting the 25860Sstevel@tonic-gate * client(or phci)<->pip linkage while traversing the list and then holding the 25870Sstevel@tonic-gate * pip when it is found in the list. 25880Sstevel@tonic-gate */ 25890Sstevel@tonic-gate 25900Sstevel@tonic-gate static void 25910Sstevel@tonic-gate di_register_pip(struct di_state *st, mdi_pathinfo_t *pip, di_off_t off) 25920Sstevel@tonic-gate { 25930Sstevel@tonic-gate struct di_key *key = kmem_zalloc(sizeof (*key), KM_SLEEP); 25940Sstevel@tonic-gate char *path_addr; 25950Sstevel@tonic-gate struct di_pkey *pk; 25960Sstevel@tonic-gate 25970Sstevel@tonic-gate ASSERT(pip); 25980Sstevel@tonic-gate ASSERT(off > 0); 25990Sstevel@tonic-gate 26000Sstevel@tonic-gate key->k_type = DI_PKEY; 26010Sstevel@tonic-gate pk = &(key->k_u.pkey); 26020Sstevel@tonic-gate 26030Sstevel@tonic-gate pk->pk_pip = pip; 26040Sstevel@tonic-gate path_addr = mdi_pi_get_addr(pip); 26050Sstevel@tonic-gate if (path_addr) 26060Sstevel@tonic-gate pk->pk_path_addr = i_ddi_strdup(path_addr, KM_SLEEP); 26070Sstevel@tonic-gate pk->pk_client = mdi_pi_get_client(pip); 26080Sstevel@tonic-gate pk->pk_phci = mdi_pi_get_phci(pip); 26090Sstevel@tonic-gate 26100Sstevel@tonic-gate if (mod_hash_insert(st->reg_pip_hash, (mod_hash_key_t)key, 26110Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)off) != 0) { 26120Sstevel@tonic-gate panic( 26130Sstevel@tonic-gate "duplicate pathinfo (%p) registered during device " 26140Sstevel@tonic-gate "tree walk", (void *)pip); 26150Sstevel@tonic-gate } 26160Sstevel@tonic-gate } 26170Sstevel@tonic-gate 26180Sstevel@tonic-gate /* 26190Sstevel@tonic-gate * As with di_register_pip, the caller must hold or lock the pip 26200Sstevel@tonic-gate */ 26210Sstevel@tonic-gate static int 26220Sstevel@tonic-gate di_pip_find(struct di_state *st, mdi_pathinfo_t *pip, di_off_t *off_p) 26230Sstevel@tonic-gate { 26240Sstevel@tonic-gate /* 26250Sstevel@tonic-gate * uintptr_t must be used because it matches the size of void *; 26260Sstevel@tonic-gate * mod_hash expects clients to place results into pointer-size 26270Sstevel@tonic-gate * containers; since di_off_t is always a 32-bit offset, alignment 26280Sstevel@tonic-gate * would otherwise be broken on 64-bit kernels. 26290Sstevel@tonic-gate */ 26300Sstevel@tonic-gate uintptr_t offset; 26310Sstevel@tonic-gate struct di_key key = {0}; 26320Sstevel@tonic-gate struct di_pkey *pk; 26330Sstevel@tonic-gate 26340Sstevel@tonic-gate ASSERT(st->reg_pip_hash); 26350Sstevel@tonic-gate ASSERT(off_p); 26360Sstevel@tonic-gate 26370Sstevel@tonic-gate if (pip == NULL) { 26380Sstevel@tonic-gate *off_p = 0; 26390Sstevel@tonic-gate return (0); 26400Sstevel@tonic-gate } 26410Sstevel@tonic-gate 26420Sstevel@tonic-gate key.k_type = DI_PKEY; 26430Sstevel@tonic-gate pk = &(key.k_u.pkey); 26440Sstevel@tonic-gate 26450Sstevel@tonic-gate pk->pk_pip = pip; 26460Sstevel@tonic-gate pk->pk_path_addr = mdi_pi_get_addr(pip); 26470Sstevel@tonic-gate pk->pk_client = mdi_pi_get_client(pip); 26480Sstevel@tonic-gate pk->pk_phci = mdi_pi_get_phci(pip); 26490Sstevel@tonic-gate 26500Sstevel@tonic-gate if (mod_hash_find(st->reg_pip_hash, (mod_hash_key_t)&key, 26510Sstevel@tonic-gate (mod_hash_val_t *)&offset) == 0) { 26520Sstevel@tonic-gate *off_p = (di_off_t)offset; 26530Sstevel@tonic-gate return (0); 26540Sstevel@tonic-gate } else { 26550Sstevel@tonic-gate return (-1); 26560Sstevel@tonic-gate } 26570Sstevel@tonic-gate } 26580Sstevel@tonic-gate 26590Sstevel@tonic-gate static di_path_state_t 26600Sstevel@tonic-gate path_state_convert(mdi_pathinfo_state_t st) 26610Sstevel@tonic-gate { 26620Sstevel@tonic-gate switch (st) { 26630Sstevel@tonic-gate case MDI_PATHINFO_STATE_ONLINE: 26640Sstevel@tonic-gate return (DI_PATH_STATE_ONLINE); 26650Sstevel@tonic-gate case MDI_PATHINFO_STATE_STANDBY: 26660Sstevel@tonic-gate return (DI_PATH_STATE_STANDBY); 26670Sstevel@tonic-gate case MDI_PATHINFO_STATE_OFFLINE: 26680Sstevel@tonic-gate return (DI_PATH_STATE_OFFLINE); 26690Sstevel@tonic-gate case MDI_PATHINFO_STATE_FAULT: 26700Sstevel@tonic-gate return (DI_PATH_STATE_FAULT); 26710Sstevel@tonic-gate default: 26720Sstevel@tonic-gate return (DI_PATH_STATE_UNKNOWN); 26730Sstevel@tonic-gate } 26740Sstevel@tonic-gate } 26750Sstevel@tonic-gate 267610696SDavid.Hollister@Sun.COM static uint_t 267710696SDavid.Hollister@Sun.COM path_flags_convert(uint_t pi_path_flags) 267810696SDavid.Hollister@Sun.COM { 267910696SDavid.Hollister@Sun.COM uint_t di_path_flags = 0; 268010696SDavid.Hollister@Sun.COM 268110696SDavid.Hollister@Sun.COM /* MDI_PATHINFO_FLAGS_HIDDEN nodes not in snapshot */ 268210696SDavid.Hollister@Sun.COM 268310696SDavid.Hollister@Sun.COM if (pi_path_flags & MDI_PATHINFO_FLAGS_DEVICE_REMOVED) 268410696SDavid.Hollister@Sun.COM di_path_flags |= DI_PATH_FLAGS_DEVICE_REMOVED; 268510696SDavid.Hollister@Sun.COM 268610696SDavid.Hollister@Sun.COM return (di_path_flags); 268710696SDavid.Hollister@Sun.COM } 268810696SDavid.Hollister@Sun.COM 26890Sstevel@tonic-gate 26900Sstevel@tonic-gate static di_off_t 26917224Scth di_path_getprop(mdi_pathinfo_t *pip, di_off_t *off_p, 26920Sstevel@tonic-gate struct di_state *st) 26930Sstevel@tonic-gate { 26947224Scth nvpair_t *prop = NULL; 26957224Scth struct di_path_prop *me; 26967224Scth int off; 26977224Scth size_t size; 26987224Scth char *str; 26997224Scth uchar_t *buf; 27007224Scth uint_t nelems; 27017224Scth 27027224Scth off = *off_p; 27030Sstevel@tonic-gate if (mdi_pi_get_next_prop(pip, NULL) == NULL) { 27040Sstevel@tonic-gate *off_p = 0; 27050Sstevel@tonic-gate return (off); 27060Sstevel@tonic-gate } 27070Sstevel@tonic-gate 27080Sstevel@tonic-gate off = di_checkmem(st, off, sizeof (struct di_path_prop)); 27090Sstevel@tonic-gate *off_p = off; 27100Sstevel@tonic-gate 27110Sstevel@tonic-gate while (prop = mdi_pi_get_next_prop(pip, prop)) { 27127224Scth me = DI_PATHPROP(di_mem_addr(st, off)); 27130Sstevel@tonic-gate me->self = off; 27140Sstevel@tonic-gate off += sizeof (struct di_path_prop); 27150Sstevel@tonic-gate 27160Sstevel@tonic-gate /* 27170Sstevel@tonic-gate * property name 27180Sstevel@tonic-gate */ 27197224Scth size = strlen(nvpair_name(prop)) + 1; 27207224Scth me->prop_name = off = di_checkmem(st, off, size); 27210Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), nvpair_name(prop)); 27227224Scth off += size; 27230Sstevel@tonic-gate 27240Sstevel@tonic-gate switch (nvpair_type(prop)) { 27250Sstevel@tonic-gate case DATA_TYPE_BYTE: 27260Sstevel@tonic-gate case DATA_TYPE_INT16: 27270Sstevel@tonic-gate case DATA_TYPE_UINT16: 27280Sstevel@tonic-gate case DATA_TYPE_INT32: 27290Sstevel@tonic-gate case DATA_TYPE_UINT32: 27300Sstevel@tonic-gate me->prop_type = DDI_PROP_TYPE_INT; 27317224Scth size = sizeof (int32_t); 27327224Scth off = di_checkmem(st, off, size); 27330Sstevel@tonic-gate (void) nvpair_value_int32(prop, 27347224Scth (int32_t *)di_mem_addr(st, off)); 27350Sstevel@tonic-gate break; 27360Sstevel@tonic-gate 27370Sstevel@tonic-gate case DATA_TYPE_INT64: 27380Sstevel@tonic-gate case DATA_TYPE_UINT64: 27390Sstevel@tonic-gate me->prop_type = DDI_PROP_TYPE_INT64; 27407224Scth size = sizeof (int64_t); 27417224Scth off = di_checkmem(st, off, size); 27420Sstevel@tonic-gate (void) nvpair_value_int64(prop, 27437224Scth (int64_t *)di_mem_addr(st, off)); 27440Sstevel@tonic-gate break; 27450Sstevel@tonic-gate 27460Sstevel@tonic-gate case DATA_TYPE_STRING: 27477224Scth me->prop_type = DDI_PROP_TYPE_STRING; 27480Sstevel@tonic-gate (void) nvpair_value_string(prop, &str); 27497224Scth size = strlen(str) + 1; 27507224Scth off = di_checkmem(st, off, size); 27510Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), str); 27520Sstevel@tonic-gate break; 27537224Scth 27540Sstevel@tonic-gate case DATA_TYPE_BYTE_ARRAY: 27550Sstevel@tonic-gate case DATA_TYPE_INT16_ARRAY: 27560Sstevel@tonic-gate case DATA_TYPE_UINT16_ARRAY: 27570Sstevel@tonic-gate case DATA_TYPE_INT32_ARRAY: 27580Sstevel@tonic-gate case DATA_TYPE_UINT32_ARRAY: 27590Sstevel@tonic-gate case DATA_TYPE_INT64_ARRAY: 27600Sstevel@tonic-gate case DATA_TYPE_UINT64_ARRAY: 27617224Scth me->prop_type = DDI_PROP_TYPE_BYTE; 27620Sstevel@tonic-gate (void) nvpair_value_byte_array(prop, &buf, &nelems); 27637224Scth size = nelems; 27640Sstevel@tonic-gate if (nelems != 0) { 27657224Scth off = di_checkmem(st, off, size); 27667224Scth bcopy(buf, di_mem_addr(st, off), size); 27670Sstevel@tonic-gate } 27680Sstevel@tonic-gate break; 27697224Scth 27707224Scth default: /* Unknown or unhandled type; skip it */ 27717224Scth size = 0; 27727224Scth break; 27730Sstevel@tonic-gate } 27740Sstevel@tonic-gate 27757224Scth if (size > 0) { 27760Sstevel@tonic-gate me->prop_data = off; 27770Sstevel@tonic-gate } 27780Sstevel@tonic-gate 27797224Scth me->prop_len = (int)size; 27807224Scth off += size; 27810Sstevel@tonic-gate 27820Sstevel@tonic-gate off = di_checkmem(st, off, sizeof (struct di_path_prop)); 27830Sstevel@tonic-gate me->prop_next = off; 27840Sstevel@tonic-gate } 27850Sstevel@tonic-gate 27860Sstevel@tonic-gate me->prop_next = 0; 27870Sstevel@tonic-gate return (off); 27880Sstevel@tonic-gate } 27890Sstevel@tonic-gate 27900Sstevel@tonic-gate 27910Sstevel@tonic-gate static void 27920Sstevel@tonic-gate di_path_one_endpoint(struct di_path *me, di_off_t noff, di_off_t **off_pp, 27930Sstevel@tonic-gate int get_client) 27940Sstevel@tonic-gate { 27950Sstevel@tonic-gate if (get_client) { 27960Sstevel@tonic-gate ASSERT(me->path_client == 0); 27970Sstevel@tonic-gate me->path_client = noff; 27980Sstevel@tonic-gate ASSERT(me->path_c_link == 0); 27990Sstevel@tonic-gate *off_pp = &me->path_c_link; 28000Sstevel@tonic-gate me->path_snap_state &= 28010Sstevel@tonic-gate ~(DI_PATH_SNAP_NOCLIENT | DI_PATH_SNAP_NOCLINK); 28020Sstevel@tonic-gate } else { 28030Sstevel@tonic-gate ASSERT(me->path_phci == 0); 28040Sstevel@tonic-gate me->path_phci = noff; 28050Sstevel@tonic-gate ASSERT(me->path_p_link == 0); 28060Sstevel@tonic-gate *off_pp = &me->path_p_link; 28070Sstevel@tonic-gate me->path_snap_state &= 28080Sstevel@tonic-gate ~(DI_PATH_SNAP_NOPHCI | DI_PATH_SNAP_NOPLINK); 28090Sstevel@tonic-gate } 28100Sstevel@tonic-gate } 28110Sstevel@tonic-gate 28120Sstevel@tonic-gate /* 28137224Scth * off_p: pointer to the linkage field. This links pips along the client|phci 28140Sstevel@tonic-gate * linkage list. 28150Sstevel@tonic-gate * noff : Offset for the endpoint dip snapshot. 28160Sstevel@tonic-gate */ 28170Sstevel@tonic-gate static di_off_t 28187224Scth di_getpath_data(dev_info_t *dip, di_off_t *off_p, di_off_t noff, 28190Sstevel@tonic-gate struct di_state *st, int get_client) 28200Sstevel@tonic-gate { 28217224Scth di_off_t off; 28227224Scth mdi_pathinfo_t *pip; 28237224Scth struct di_path *me; 28247224Scth mdi_pathinfo_t *(*next_pip)(dev_info_t *, mdi_pathinfo_t *); 28257224Scth size_t size; 28260Sstevel@tonic-gate 28270Sstevel@tonic-gate dcmn_err2((CE_WARN, "di_getpath_data: client = %d", get_client)); 28280Sstevel@tonic-gate 28290Sstevel@tonic-gate /* 28300Sstevel@tonic-gate * The naming of the following mdi_xyz() is unfortunately 28310Sstevel@tonic-gate * non-intuitive. mdi_get_next_phci_path() follows the 28320Sstevel@tonic-gate * client_link i.e. the list of pip's belonging to the 28330Sstevel@tonic-gate * given client dip. 28340Sstevel@tonic-gate */ 28350Sstevel@tonic-gate if (get_client) 28360Sstevel@tonic-gate next_pip = &mdi_get_next_phci_path; 28370Sstevel@tonic-gate else 28380Sstevel@tonic-gate next_pip = &mdi_get_next_client_path; 28390Sstevel@tonic-gate 28407224Scth off = *off_p; 28410Sstevel@tonic-gate 28420Sstevel@tonic-gate pip = NULL; 28430Sstevel@tonic-gate while (pip = (*next_pip)(dip, pip)) { 28440Sstevel@tonic-gate di_off_t stored_offset; 28450Sstevel@tonic-gate 28460Sstevel@tonic-gate dcmn_err((CE_WARN, "marshalling pip = %p", (void *)pip)); 28470Sstevel@tonic-gate 28480Sstevel@tonic-gate mdi_pi_lock(pip); 28490Sstevel@tonic-gate 285010696SDavid.Hollister@Sun.COM /* We don't represent hidden paths in the snapshot */ 285110696SDavid.Hollister@Sun.COM if (mdi_pi_ishidden(pip)) { 285210696SDavid.Hollister@Sun.COM dcmn_err((CE_WARN, "hidden, skip")); 285310696SDavid.Hollister@Sun.COM mdi_pi_unlock(pip); 285410696SDavid.Hollister@Sun.COM continue; 285510696SDavid.Hollister@Sun.COM } 285610696SDavid.Hollister@Sun.COM 28570Sstevel@tonic-gate if (di_pip_find(st, pip, &stored_offset) != -1) { 28580Sstevel@tonic-gate /* 28590Sstevel@tonic-gate * We've already seen this pathinfo node so we need to 28600Sstevel@tonic-gate * take care not to snap it again; However, one endpoint 28610Sstevel@tonic-gate * and linkage will be set here. The other endpoint 28620Sstevel@tonic-gate * and linkage has already been set when the pip was 28630Sstevel@tonic-gate * first snapshotted i.e. when the other endpoint dip 28640Sstevel@tonic-gate * was snapshotted. 28650Sstevel@tonic-gate */ 28667224Scth me = DI_PATH(di_mem_addr(st, stored_offset)); 28677224Scth *off_p = stored_offset; 28687224Scth 28697224Scth di_path_one_endpoint(me, noff, &off_p, get_client); 28700Sstevel@tonic-gate 28710Sstevel@tonic-gate /* 28720Sstevel@tonic-gate * The other endpoint and linkage were set when this 28730Sstevel@tonic-gate * pip was snapshotted. So we are done with both 28740Sstevel@tonic-gate * endpoints and linkages. 28750Sstevel@tonic-gate */ 28760Sstevel@tonic-gate ASSERT(!(me->path_snap_state & 28770Sstevel@tonic-gate (DI_PATH_SNAP_NOCLIENT|DI_PATH_SNAP_NOPHCI))); 28780Sstevel@tonic-gate ASSERT(!(me->path_snap_state & 28790Sstevel@tonic-gate (DI_PATH_SNAP_NOCLINK|DI_PATH_SNAP_NOPLINK))); 28800Sstevel@tonic-gate 28810Sstevel@tonic-gate mdi_pi_unlock(pip); 28820Sstevel@tonic-gate continue; 28830Sstevel@tonic-gate } 28840Sstevel@tonic-gate 28850Sstevel@tonic-gate /* 28860Sstevel@tonic-gate * Now that we need to snapshot this pip, check memory 28870Sstevel@tonic-gate */ 28887224Scth size = sizeof (struct di_path); 28897224Scth *off_p = off = di_checkmem(st, off, size); 28907224Scth me = DI_PATH(di_mem_addr(st, off)); 28910Sstevel@tonic-gate me->self = off; 28927224Scth off += size; 28930Sstevel@tonic-gate 28940Sstevel@tonic-gate me->path_snap_state = 28950Sstevel@tonic-gate DI_PATH_SNAP_NOCLINK | DI_PATH_SNAP_NOPLINK; 28960Sstevel@tonic-gate me->path_snap_state |= 28970Sstevel@tonic-gate DI_PATH_SNAP_NOCLIENT | DI_PATH_SNAP_NOPHCI; 28980Sstevel@tonic-gate 28990Sstevel@tonic-gate /* 29000Sstevel@tonic-gate * Zero out fields as di_checkmem() doesn't guarantee 29010Sstevel@tonic-gate * zero-filled memory 29020Sstevel@tonic-gate */ 29030Sstevel@tonic-gate me->path_client = me->path_phci = 0; 29040Sstevel@tonic-gate me->path_c_link = me->path_p_link = 0; 29050Sstevel@tonic-gate 29067224Scth di_path_one_endpoint(me, noff, &off_p, get_client); 29070Sstevel@tonic-gate 29080Sstevel@tonic-gate /* 29090Sstevel@tonic-gate * Note the existence of this pathinfo 29100Sstevel@tonic-gate */ 29110Sstevel@tonic-gate di_register_pip(st, pip, me->self); 29120Sstevel@tonic-gate 291310696SDavid.Hollister@Sun.COM me->path_state = path_state_convert(mdi_pi_get_state(pip)); 291410696SDavid.Hollister@Sun.COM me->path_flags = path_flags_convert(mdi_pi_get_flags(pip)); 29150Sstevel@tonic-gate 29166640Scth me->path_instance = mdi_pi_get_path_instance(pip); 29176640Scth 29180Sstevel@tonic-gate /* 29190Sstevel@tonic-gate * Get intermediate addressing info. 29200Sstevel@tonic-gate */ 29217224Scth size = strlen(mdi_pi_get_addr(pip)) + 1; 29227224Scth me->path_addr = off = di_checkmem(st, off, size); 29230Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), mdi_pi_get_addr(pip)); 29247224Scth off += size; 29250Sstevel@tonic-gate 29260Sstevel@tonic-gate /* 29270Sstevel@tonic-gate * Get path properties if props are to be included in the 29280Sstevel@tonic-gate * snapshot 29290Sstevel@tonic-gate */ 29300Sstevel@tonic-gate if (DINFOPROP & st->command) { 29317224Scth me->path_prop = off; 29327224Scth off = di_path_getprop(pip, &me->path_prop, st); 29330Sstevel@tonic-gate } else { 29340Sstevel@tonic-gate me->path_prop = 0; 29350Sstevel@tonic-gate } 29360Sstevel@tonic-gate 29370Sstevel@tonic-gate mdi_pi_unlock(pip); 29380Sstevel@tonic-gate } 29390Sstevel@tonic-gate 29407224Scth *off_p = 0; 29410Sstevel@tonic-gate return (off); 29420Sstevel@tonic-gate } 29430Sstevel@tonic-gate 29440Sstevel@tonic-gate /* 29457224Scth * Return driver prop_op entry point for the specified devinfo node. 29467224Scth * 29477224Scth * To return a non-NULL value: 29487224Scth * - driver must be attached and held: 29497224Scth * If driver is not attached we ignore the driver property list. 29507224Scth * No one should rely on such properties. 29517224Scth * - driver "cb_prop_op != ddi_prop_op": 29527224Scth * If "cb_prop_op == ddi_prop_op", framework does not need to call driver. 29537224Scth * XXX or parent's bus_prop_op != ddi_bus_prop_op 29547224Scth */ 29557224Scth static int 29567224Scth (*di_getprop_prop_op(struct dev_info *dip)) 29577224Scth (dev_t, dev_info_t *, ddi_prop_op_t, int, char *, caddr_t, int *) 29587224Scth { 29597224Scth struct dev_ops *ops; 29607224Scth 29617224Scth /* If driver is not attached we ignore the driver property list. */ 29627224Scth if ((dip == NULL) || !i_ddi_devi_attached((dev_info_t *)dip)) 29637224Scth return (NULL); 29647224Scth 29657224Scth /* 29667224Scth * Some nexus drivers incorrectly set cb_prop_op to nodev, nulldev, 29677224Scth * or even NULL. 29687224Scth */ 29697224Scth ops = dip->devi_ops; 29707224Scth if (ops && ops->devo_cb_ops && 29717224Scth (ops->devo_cb_ops->cb_prop_op != ddi_prop_op) && 29727224Scth (ops->devo_cb_ops->cb_prop_op != nodev) && 29737224Scth (ops->devo_cb_ops->cb_prop_op != nulldev) && 29747224Scth (ops->devo_cb_ops->cb_prop_op != NULL)) 29757224Scth return (ops->devo_cb_ops->cb_prop_op); 29767224Scth return (NULL); 29777224Scth } 29787224Scth 29797224Scth static di_off_t 29807224Scth di_getprop_add(int list, int dyn, struct di_state *st, struct dev_info *dip, 29817224Scth int (*prop_op)(), 29827224Scth char *name, dev_t devt, int aflags, int alen, caddr_t aval, 29837224Scth di_off_t off, di_off_t **off_pp) 29847224Scth { 29857224Scth int need_free = 0; 29867224Scth dev_t pdevt; 29877224Scth int pflags; 29887224Scth int rv; 29897224Scth caddr_t val; 29907224Scth int len; 29917224Scth size_t size; 29927224Scth struct di_prop *pp; 29937224Scth 29947224Scth /* If we have prop_op function, ask driver for latest value */ 29957224Scth if (prop_op) { 29967224Scth ASSERT(dip); 29977224Scth 29987224Scth /* Must search DDI_DEV_T_NONE with DDI_DEV_T_ANY */ 29997224Scth pdevt = (devt == DDI_DEV_T_NONE) ? DDI_DEV_T_ANY : devt; 30007224Scth 30017224Scth /* 30027224Scth * We have type information in flags, but are invoking an 30037224Scth * old non-typed prop_op(9E) interface. Since not all types are 30047224Scth * part of DDI_PROP_TYPE_ANY (example is DDI_PROP_TYPE_INT64), 30057224Scth * we set DDI_PROP_CONSUMER_TYPED - causing the framework to 30067224Scth * expand type bits beyond DDI_PROP_TYPE_ANY. This allows us 30077224Scth * to use the legacy prop_op(9E) interface to obtain updates 30087224Scth * non-DDI_PROP_TYPE_ANY dynamic properties. 30097224Scth */ 30107224Scth pflags = aflags & ~DDI_PROP_TYPE_MASK; 30117224Scth pflags |= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM | 30127224Scth DDI_PROP_CONSUMER_TYPED; 30137627SChris.Horne@Sun.COM 30147627SChris.Horne@Sun.COM /* 30157627SChris.Horne@Sun.COM * Hold and exit across prop_op(9E) to avoid lock order 30167627SChris.Horne@Sun.COM * issues between 30177627SChris.Horne@Sun.COM * [ndi_devi_enter() ..prop_op(9E).. driver-lock] 30187627SChris.Horne@Sun.COM * .vs. 30197627SChris.Horne@Sun.COM * [..ioctl(9E).. driver-lock ..ddi_remove_minor_node(9F).. 30207627SChris.Horne@Sun.COM * ndi_devi_enter()] 30217627SChris.Horne@Sun.COM * ordering. 30227627SChris.Horne@Sun.COM */ 30237627SChris.Horne@Sun.COM ndi_hold_devi((dev_info_t *)dip); 30247627SChris.Horne@Sun.COM ndi_devi_exit((dev_info_t *)dip, dip->devi_circular); 30257627SChris.Horne@Sun.COM rv = (*prop_op)(pdevt, (dev_info_t *)dip, 30267627SChris.Horne@Sun.COM PROP_LEN_AND_VAL_ALLOC, pflags, name, &val, &len); 30277627SChris.Horne@Sun.COM ndi_devi_enter((dev_info_t *)dip, &dip->devi_circular); 30287627SChris.Horne@Sun.COM ndi_rele_devi((dev_info_t *)dip); 30297224Scth 30307224Scth if (rv == DDI_PROP_SUCCESS) { 30317224Scth need_free = 1; /* dynamic prop obtained */ 30327224Scth } else if (dyn) { 30337224Scth /* 30347224Scth * A dynamic property must succeed prop_op(9E) to show 30357224Scth * up in the snapshot - that is the only source of its 30367224Scth * value. 30377224Scth */ 30387224Scth return (off); /* dynamic prop not supported */ 30397224Scth } else { 30407224Scth /* 30417224Scth * In case calling the driver caused an update off 30427224Scth * prop_op(9E) of a non-dynamic property (code leading 30437224Scth * to ddi_prop_change), we defer picking up val and 30447224Scth * len informatiojn until after prop_op(9E) to ensure 30457224Scth * that we snapshot the latest value. 30467224Scth */ 30477224Scth val = aval; 30487224Scth len = alen; 30497224Scth 30507224Scth } 30517224Scth } else { 30527224Scth val = aval; 30537224Scth len = alen; 30547224Scth } 30557224Scth 30567224Scth dcmn_err((CE_CONT, "di_getprop_add: list %d %s len %d val %p\n", 30577224Scth list, name ? name : "NULL", len, (void *)val)); 30587224Scth 30597224Scth size = sizeof (struct di_prop); 30607224Scth **off_pp = off = di_checkmem(st, off, size); 30617224Scth pp = DI_PROP(di_mem_addr(st, off)); 30627224Scth pp->self = off; 30637224Scth off += size; 30647224Scth 30657224Scth pp->dev_major = getmajor(devt); 30667224Scth pp->dev_minor = getminor(devt); 30677224Scth pp->prop_flags = aflags; 30687224Scth pp->prop_list = list; 30697224Scth 30707224Scth /* property name */ 30717224Scth if (name) { 30727224Scth size = strlen(name) + 1; 30737224Scth pp->prop_name = off = di_checkmem(st, off, size); 30747224Scth (void) strcpy(di_mem_addr(st, off), name); 30757224Scth off += size; 30767224Scth } else { 30777224Scth pp->prop_name = -1; 30787224Scth } 30797224Scth 30807224Scth pp->prop_len = len; 30817224Scth if (val == NULL) { 30827224Scth pp->prop_data = -1; 30837224Scth } else if (len != 0) { 30847224Scth size = len; 30857224Scth pp->prop_data = off = di_checkmem(st, off, size); 30867224Scth bcopy(val, di_mem_addr(st, off), size); 30877224Scth off += size; 30887224Scth } 30897224Scth 30907224Scth pp->next = 0; /* assume tail for now */ 30917224Scth *off_pp = &pp->next; /* return pointer to our next */ 30927224Scth 30937224Scth if (need_free) /* free PROP_LEN_AND_VAL_ALLOC alloc */ 30947224Scth kmem_free(val, len); 30957224Scth return (off); 30967224Scth } 30977224Scth 30987224Scth 30997224Scth /* 31000Sstevel@tonic-gate * Copy a list of properties attached to a devinfo node. Called from 31017224Scth * di_copynode with active ndi_devi_enter. The major number is passed in case 31020Sstevel@tonic-gate * we need to call driver's prop_op entry. The value of list indicates 31030Sstevel@tonic-gate * which list we are copying. Possible values are: 31040Sstevel@tonic-gate * DI_PROP_DRV_LIST, DI_PROP_SYS_LIST, DI_PROP_GLB_LIST, DI_PROP_HW_LIST 31050Sstevel@tonic-gate */ 31060Sstevel@tonic-gate static di_off_t 31077224Scth di_getprop(int list, struct ddi_prop **pprop, di_off_t *off_p, 31087224Scth struct di_state *st, struct dev_info *dip) 31090Sstevel@tonic-gate { 31107224Scth struct ddi_prop *prop; 31117224Scth int (*prop_op)(); 31127224Scth int off; 31137224Scth struct ddi_minor_data *mn; 31147224Scth i_ddi_prop_dyn_t *dp; 31157224Scth struct plist { 31167224Scth struct plist *pl_next; 31177224Scth char *pl_name; 31187224Scth int pl_flags; 31197224Scth dev_t pl_dev; 31207224Scth int pl_len; 31217224Scth caddr_t pl_val; 31227224Scth } *pl, *pl0, **plp; 31230Sstevel@tonic-gate 31240Sstevel@tonic-gate ASSERT(st != NULL); 31250Sstevel@tonic-gate 31267224Scth off = *off_p; 31277224Scth *off_p = 0; 31287224Scth dcmn_err((CE_CONT, "di_getprop: copy property list %d at addr %p\n", 31297224Scth list, (void *)*pprop)); 31307224Scth 31317224Scth /* get pointer to driver's prop_op(9E) implementation if DRV_LIST */ 31327224Scth prop_op = (list == DI_PROP_DRV_LIST) ? di_getprop_prop_op(dip) : NULL; 31330Sstevel@tonic-gate 31340Sstevel@tonic-gate /* 31357224Scth * Form private list of properties, holding devi_lock for properties 31367633SChris.Horne@Sun.COM * that hang off the dip. 31370Sstevel@tonic-gate */ 31387224Scth if (dip) 31397224Scth mutex_enter(&(dip->devi_lock)); 31407633SChris.Horne@Sun.COM for (pl0 = NULL, plp = &pl0, prop = *pprop; 31417224Scth prop; plp = &pl->pl_next, prop = prop->prop_next) { 31427224Scth pl = kmem_alloc(sizeof (*pl), KM_SLEEP); 31437224Scth *plp = pl; 31447224Scth pl->pl_next = NULL; 31457224Scth if (prop->prop_name) 31467224Scth pl->pl_name = i_ddi_strdup(prop->prop_name, KM_SLEEP); 31477224Scth else 31487224Scth pl->pl_name = NULL; 31497224Scth pl->pl_flags = prop->prop_flags; 31507224Scth pl->pl_dev = prop->prop_dev; 31517224Scth if (prop->prop_len) { 31527224Scth pl->pl_len = prop->prop_len; 31537224Scth pl->pl_val = kmem_alloc(pl->pl_len, KM_SLEEP); 31547224Scth bcopy(prop->prop_val, pl->pl_val, pl->pl_len); 31557224Scth } else { 31567224Scth pl->pl_len = 0; 31577224Scth pl->pl_val = NULL; 31587224Scth } 31590Sstevel@tonic-gate } 31607224Scth if (dip) 31617224Scth mutex_exit(&(dip->devi_lock)); 31620Sstevel@tonic-gate 31630Sstevel@tonic-gate /* 31647224Scth * Now that we have dropped devi_lock, perform a second-pass to 31657224Scth * add properties to the snapshot. We do this as a second pass 31667224Scth * because we may need to call prop_op(9E) and we can't hold 31677224Scth * devi_lock across that call. 31680Sstevel@tonic-gate */ 31697224Scth for (pl = pl0; pl; pl = pl0) { 31707224Scth pl0 = pl->pl_next; 31717224Scth off = di_getprop_add(list, 0, st, dip, prop_op, pl->pl_name, 31727224Scth pl->pl_dev, pl->pl_flags, pl->pl_len, pl->pl_val, 31737224Scth off, &off_p); 31747224Scth if (pl->pl_val) 31757224Scth kmem_free(pl->pl_val, pl->pl_len); 31767224Scth if (pl->pl_name) 31777224Scth kmem_free(pl->pl_name, strlen(pl->pl_name) + 1); 31787224Scth kmem_free(pl, sizeof (*pl)); 31790Sstevel@tonic-gate } 31800Sstevel@tonic-gate 31810Sstevel@tonic-gate /* 31827224Scth * If there is no prop_op or dynamic property support has been 31837224Scth * disabled, we are done. 31840Sstevel@tonic-gate */ 31857224Scth if ((prop_op == NULL) || (di_prop_dyn == 0)) { 31867224Scth *off_p = 0; 31870Sstevel@tonic-gate return (off); 31880Sstevel@tonic-gate } 31890Sstevel@tonic-gate 31907224Scth /* Add dynamic driver properties to snapshot */ 31917224Scth for (dp = i_ddi_prop_dyn_driver_get((dev_info_t *)dip); 31927224Scth dp && dp->dp_name; dp++) { 31937224Scth if (dp->dp_spec_type) { 31947224Scth /* if spec_type, property of matching minor */ 31957224Scth ASSERT(DEVI_BUSY_OWNED(dip)); 31967224Scth for (mn = dip->devi_minor; mn; mn = mn->next) { 31977224Scth if (mn->ddm_spec_type != dp->dp_spec_type) 31987224Scth continue; 31997224Scth off = di_getprop_add(list, 1, st, dip, prop_op, 32007224Scth dp->dp_name, mn->ddm_dev, dp->dp_type, 32017224Scth 0, NULL, off, &off_p); 32027224Scth } 32030Sstevel@tonic-gate } else { 32047224Scth /* property of devinfo node */ 32057224Scth off = di_getprop_add(list, 1, st, dip, prop_op, 32067224Scth dp->dp_name, DDI_DEV_T_NONE, dp->dp_type, 32077224Scth 0, NULL, off, &off_p); 32080Sstevel@tonic-gate } 32097224Scth } 32107224Scth 32117224Scth /* Add dynamic parent properties to snapshot */ 32127224Scth for (dp = i_ddi_prop_dyn_parent_get((dev_info_t *)dip); 32137224Scth dp && dp->dp_name; dp++) { 32147224Scth if (dp->dp_spec_type) { 32157224Scth /* if spec_type, property of matching minor */ 32167224Scth ASSERT(DEVI_BUSY_OWNED(dip)); 32177224Scth for (mn = dip->devi_minor; mn; mn = mn->next) { 32187224Scth if (mn->ddm_spec_type != dp->dp_spec_type) 32197224Scth continue; 32207224Scth off = di_getprop_add(list, 1, st, dip, prop_op, 32217224Scth dp->dp_name, mn->ddm_dev, dp->dp_type, 32227224Scth 0, NULL, off, &off_p); 32230Sstevel@tonic-gate } 32247224Scth } else { 32257224Scth /* property of devinfo node */ 32267224Scth off = di_getprop_add(list, 1, st, dip, prop_op, 32277224Scth dp->dp_name, DDI_DEV_T_NONE, dp->dp_type, 32287224Scth 0, NULL, off, &off_p); 32290Sstevel@tonic-gate } 32307224Scth } 32317224Scth 32327224Scth *off_p = 0; 32330Sstevel@tonic-gate return (off); 32340Sstevel@tonic-gate } 32350Sstevel@tonic-gate 32360Sstevel@tonic-gate /* 32370Sstevel@tonic-gate * find private data format attached to a dip 32380Sstevel@tonic-gate * parent = 1 to match driver name of parent dip (for parent private data) 32390Sstevel@tonic-gate * 0 to match driver name of current dip (for driver private data) 32400Sstevel@tonic-gate */ 32410Sstevel@tonic-gate #define DI_MATCH_DRIVER 0 32420Sstevel@tonic-gate #define DI_MATCH_PARENT 1 32430Sstevel@tonic-gate 32440Sstevel@tonic-gate struct di_priv_format * 32450Sstevel@tonic-gate di_match_drv_name(struct dev_info *node, struct di_state *st, int match) 32460Sstevel@tonic-gate { 32477224Scth int i, count, len; 32487224Scth char *drv_name; 32497224Scth major_t major; 32507224Scth struct di_all *all; 32517224Scth struct di_priv_format *form; 32520Sstevel@tonic-gate 32530Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_match_drv_name: node = %s, match = %x\n", 32544870Sjg node->devi_node_name, match)); 32550Sstevel@tonic-gate 32560Sstevel@tonic-gate if (match == DI_MATCH_PARENT) { 32570Sstevel@tonic-gate node = DEVI(node->devi_parent); 32580Sstevel@tonic-gate } 32590Sstevel@tonic-gate 32600Sstevel@tonic-gate if (node == NULL) { 32610Sstevel@tonic-gate return (NULL); 32620Sstevel@tonic-gate } 32630Sstevel@tonic-gate 32648459SJerry.Gilliam@Sun.COM major = node->devi_major; 32650Sstevel@tonic-gate if (major == (major_t)(-1)) { 32660Sstevel@tonic-gate return (NULL); 32670Sstevel@tonic-gate } 32680Sstevel@tonic-gate 32690Sstevel@tonic-gate /* 32700Sstevel@tonic-gate * Match the driver name. 32710Sstevel@tonic-gate */ 32720Sstevel@tonic-gate drv_name = ddi_major_to_name(major); 32730Sstevel@tonic-gate if ((drv_name == NULL) || *drv_name == '\0') { 32740Sstevel@tonic-gate return (NULL); 32750Sstevel@tonic-gate } 32760Sstevel@tonic-gate 32770Sstevel@tonic-gate /* Now get the di_priv_format array */ 32787224Scth all = DI_ALL_PTR(st); 32790Sstevel@tonic-gate if (match == DI_MATCH_PARENT) { 32800Sstevel@tonic-gate count = all->n_ppdata; 32817224Scth form = DI_PRIV_FORMAT(di_mem_addr(st, all->ppdata_format)); 32820Sstevel@tonic-gate } else { 32830Sstevel@tonic-gate count = all->n_dpdata; 32847224Scth form = DI_PRIV_FORMAT(di_mem_addr(st, all->dpdata_format)); 32850Sstevel@tonic-gate } 32860Sstevel@tonic-gate 32870Sstevel@tonic-gate len = strlen(drv_name); 32880Sstevel@tonic-gate for (i = 0; i < count; i++) { 32890Sstevel@tonic-gate char *tmp; 32900Sstevel@tonic-gate 32910Sstevel@tonic-gate tmp = form[i].drv_name; 32920Sstevel@tonic-gate while (tmp && (*tmp != '\0')) { 32930Sstevel@tonic-gate if (strncmp(drv_name, tmp, len) == 0) { 32940Sstevel@tonic-gate return (&form[i]); 32950Sstevel@tonic-gate } 32960Sstevel@tonic-gate /* 32970Sstevel@tonic-gate * Move to next driver name, skipping a white space 32980Sstevel@tonic-gate */ 32990Sstevel@tonic-gate if (tmp = strchr(tmp, ' ')) { 33000Sstevel@tonic-gate tmp++; 33010Sstevel@tonic-gate } 33020Sstevel@tonic-gate } 33030Sstevel@tonic-gate } 33040Sstevel@tonic-gate 33050Sstevel@tonic-gate return (NULL); 33060Sstevel@tonic-gate } 33070Sstevel@tonic-gate 33080Sstevel@tonic-gate /* 33090Sstevel@tonic-gate * The following functions copy data as specified by the format passed in. 33100Sstevel@tonic-gate * To prevent invalid format from panicing the system, we call on_fault(). 33110Sstevel@tonic-gate * A return value of 0 indicates an error. Otherwise, the total offset 33120Sstevel@tonic-gate * is returned. 33130Sstevel@tonic-gate */ 33140Sstevel@tonic-gate #define DI_MAX_PRIVDATA (PAGESIZE >> 1) /* max private data size */ 33150Sstevel@tonic-gate 33160Sstevel@tonic-gate static di_off_t 33172271Scth di_getprvdata(struct di_priv_format *pdp, struct dev_info *node, 33182271Scth void *data, di_off_t *off_p, struct di_state *st) 33190Sstevel@tonic-gate { 33207224Scth caddr_t pa; 33217224Scth void *ptr; 33227224Scth int i, size, repeat; 33237224Scth di_off_t off, off0, *tmp; 33247224Scth char *path; 33257224Scth label_t ljb; 33260Sstevel@tonic-gate 33270Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_getprvdata:\n")); 33280Sstevel@tonic-gate 33290Sstevel@tonic-gate /* 33300Sstevel@tonic-gate * check memory availability. Private data size is 33310Sstevel@tonic-gate * limited to DI_MAX_PRIVDATA. 33320Sstevel@tonic-gate */ 33330Sstevel@tonic-gate off = di_checkmem(st, *off_p, DI_MAX_PRIVDATA); 33347224Scth *off_p = off; 33350Sstevel@tonic-gate 33363133Sjg if ((pdp->bytes == 0) || pdp->bytes > DI_MAX_PRIVDATA) { 33370Sstevel@tonic-gate goto failure; 33380Sstevel@tonic-gate } 33390Sstevel@tonic-gate 33400Sstevel@tonic-gate if (!on_fault(&ljb)) { 33410Sstevel@tonic-gate /* copy the struct */ 33420Sstevel@tonic-gate bcopy(data, di_mem_addr(st, off), pdp->bytes); 33437224Scth off0 = DI_ALIGN(pdp->bytes); /* XXX remove DI_ALIGN */ 33440Sstevel@tonic-gate 33450Sstevel@tonic-gate /* dereferencing pointers */ 33460Sstevel@tonic-gate for (i = 0; i < MAX_PTR_IN_PRV; i++) { 33470Sstevel@tonic-gate 33480Sstevel@tonic-gate if (pdp->ptr[i].size == 0) { 33490Sstevel@tonic-gate goto success; /* no more ptrs */ 33500Sstevel@tonic-gate } 33510Sstevel@tonic-gate 33520Sstevel@tonic-gate /* 33530Sstevel@tonic-gate * first, get the pointer content 33540Sstevel@tonic-gate */ 33550Sstevel@tonic-gate if ((pdp->ptr[i].offset < 0) || 33567224Scth (pdp->ptr[i].offset > pdp->bytes - sizeof (char *))) 33570Sstevel@tonic-gate goto failure; /* wrong offset */ 33580Sstevel@tonic-gate 33590Sstevel@tonic-gate pa = di_mem_addr(st, off + pdp->ptr[i].offset); 33603133Sjg 33613133Sjg /* save a tmp ptr to store off_t later */ 33623133Sjg tmp = (di_off_t *)(intptr_t)pa; 33633133Sjg 33643133Sjg /* get pointer value, if NULL continue */ 33653133Sjg ptr = *((void **) (intptr_t)pa); 33663133Sjg if (ptr == NULL) { 33670Sstevel@tonic-gate continue; 33680Sstevel@tonic-gate } 33690Sstevel@tonic-gate 33700Sstevel@tonic-gate /* 33710Sstevel@tonic-gate * next, find the repeat count (array dimension) 33720Sstevel@tonic-gate */ 33730Sstevel@tonic-gate repeat = pdp->ptr[i].len_offset; 33740Sstevel@tonic-gate 33750Sstevel@tonic-gate /* 33760Sstevel@tonic-gate * Positive value indicates a fixed sized array. 33770Sstevel@tonic-gate * 0 or negative value indicates variable sized array. 33780Sstevel@tonic-gate * 33790Sstevel@tonic-gate * For variable sized array, the variable must be 33800Sstevel@tonic-gate * an int member of the structure, with an offset 33810Sstevel@tonic-gate * equal to the absolution value of struct member. 33820Sstevel@tonic-gate */ 33830Sstevel@tonic-gate if (repeat > pdp->bytes - sizeof (int)) { 33840Sstevel@tonic-gate goto failure; /* wrong offset */ 33850Sstevel@tonic-gate } 33860Sstevel@tonic-gate 33870Sstevel@tonic-gate if (repeat >= 0) { 33883133Sjg repeat = *((int *) 33893133Sjg (intptr_t)((caddr_t)data + repeat)); 33900Sstevel@tonic-gate } else { 33910Sstevel@tonic-gate repeat = -repeat; 33920Sstevel@tonic-gate } 33930Sstevel@tonic-gate 33940Sstevel@tonic-gate /* 33950Sstevel@tonic-gate * next, get the size of the object to be copied 33960Sstevel@tonic-gate */ 33970Sstevel@tonic-gate size = pdp->ptr[i].size * repeat; 33980Sstevel@tonic-gate 33990Sstevel@tonic-gate /* 34000Sstevel@tonic-gate * Arbitrarily limit the total size of object to be 34010Sstevel@tonic-gate * copied (1 byte to 1/4 page). 34020Sstevel@tonic-gate */ 34030Sstevel@tonic-gate if ((size <= 0) || (size > (DI_MAX_PRIVDATA - off0))) { 34040Sstevel@tonic-gate goto failure; /* wrong size or too big */ 34050Sstevel@tonic-gate } 34060Sstevel@tonic-gate 34070Sstevel@tonic-gate /* 34080Sstevel@tonic-gate * Now copy the data 34090Sstevel@tonic-gate */ 34100Sstevel@tonic-gate *tmp = off0; 34110Sstevel@tonic-gate bcopy(ptr, di_mem_addr(st, off + off0), size); 34127224Scth off0 += DI_ALIGN(size); /* XXX remove DI_ALIGN */ 34130Sstevel@tonic-gate } 34140Sstevel@tonic-gate } else { 34150Sstevel@tonic-gate goto failure; 34160Sstevel@tonic-gate } 34170Sstevel@tonic-gate 34180Sstevel@tonic-gate success: 34190Sstevel@tonic-gate /* 34200Sstevel@tonic-gate * success if reached here 34210Sstevel@tonic-gate */ 34220Sstevel@tonic-gate no_fault(); 34230Sstevel@tonic-gate return (off + off0); 34240Sstevel@tonic-gate /*NOTREACHED*/ 34250Sstevel@tonic-gate 34260Sstevel@tonic-gate failure: 34270Sstevel@tonic-gate /* 34280Sstevel@tonic-gate * fault occurred 34290Sstevel@tonic-gate */ 34300Sstevel@tonic-gate no_fault(); 34312271Scth path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 34322271Scth cmn_err(CE_WARN, "devinfo: fault on private data for '%s' at %p", 34332271Scth ddi_pathname((dev_info_t *)node, path), data); 34342271Scth kmem_free(path, MAXPATHLEN); 34350Sstevel@tonic-gate *off_p = -1; /* set private data to indicate error */ 34360Sstevel@tonic-gate 34370Sstevel@tonic-gate return (off); 34380Sstevel@tonic-gate } 34390Sstevel@tonic-gate 34400Sstevel@tonic-gate /* 34410Sstevel@tonic-gate * get parent private data; on error, returns original offset 34420Sstevel@tonic-gate */ 34430Sstevel@tonic-gate static di_off_t 34440Sstevel@tonic-gate di_getppdata(struct dev_info *node, di_off_t *off_p, struct di_state *st) 34450Sstevel@tonic-gate { 34467224Scth int off; 34477224Scth struct di_priv_format *ppdp; 34480Sstevel@tonic-gate 34490Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_getppdata:\n")); 34500Sstevel@tonic-gate 34510Sstevel@tonic-gate /* find the parent data format */ 34520Sstevel@tonic-gate if ((ppdp = di_match_drv_name(node, st, DI_MATCH_PARENT)) == NULL) { 34530Sstevel@tonic-gate off = *off_p; 34540Sstevel@tonic-gate *off_p = 0; /* set parent data to none */ 34550Sstevel@tonic-gate return (off); 34560Sstevel@tonic-gate } 34570Sstevel@tonic-gate 34582271Scth return (di_getprvdata(ppdp, node, 34592271Scth ddi_get_parent_data((dev_info_t *)node), off_p, st)); 34600Sstevel@tonic-gate } 34610Sstevel@tonic-gate 34620Sstevel@tonic-gate /* 34630Sstevel@tonic-gate * get parent private data; returns original offset 34640Sstevel@tonic-gate */ 34650Sstevel@tonic-gate static di_off_t 34660Sstevel@tonic-gate di_getdpdata(struct dev_info *node, di_off_t *off_p, struct di_state *st) 34670Sstevel@tonic-gate { 34687224Scth int off; 34697224Scth struct di_priv_format *dpdp; 34700Sstevel@tonic-gate 34710Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_getdpdata:")); 34720Sstevel@tonic-gate 34730Sstevel@tonic-gate /* find the parent data format */ 34740Sstevel@tonic-gate if ((dpdp = di_match_drv_name(node, st, DI_MATCH_DRIVER)) == NULL) { 34750Sstevel@tonic-gate off = *off_p; 34760Sstevel@tonic-gate *off_p = 0; /* set driver data to none */ 34770Sstevel@tonic-gate return (off); 34780Sstevel@tonic-gate } 34790Sstevel@tonic-gate 34802271Scth return (di_getprvdata(dpdp, node, 34812271Scth ddi_get_driver_private((dev_info_t *)node), off_p, st)); 34820Sstevel@tonic-gate } 34830Sstevel@tonic-gate 34840Sstevel@tonic-gate /* 3485*10923SEvan.Yan@Sun.COM * Copy hotplug data associated with a devinfo node into the snapshot. 3486*10923SEvan.Yan@Sun.COM */ 3487*10923SEvan.Yan@Sun.COM static di_off_t 3488*10923SEvan.Yan@Sun.COM di_gethpdata(ddi_hp_cn_handle_t *hp_hdl, di_off_t *off_p, 3489*10923SEvan.Yan@Sun.COM struct di_state *st) 3490*10923SEvan.Yan@Sun.COM { 3491*10923SEvan.Yan@Sun.COM struct i_hp *hp; 3492*10923SEvan.Yan@Sun.COM struct di_hp *me; 3493*10923SEvan.Yan@Sun.COM size_t size; 3494*10923SEvan.Yan@Sun.COM di_off_t off; 3495*10923SEvan.Yan@Sun.COM 3496*10923SEvan.Yan@Sun.COM dcmn_err2((CE_CONT, "di_gethpdata:\n")); 3497*10923SEvan.Yan@Sun.COM 3498*10923SEvan.Yan@Sun.COM /* 3499*10923SEvan.Yan@Sun.COM * check memory first 3500*10923SEvan.Yan@Sun.COM */ 3501*10923SEvan.Yan@Sun.COM off = di_checkmem(st, *off_p, sizeof (struct di_hp)); 3502*10923SEvan.Yan@Sun.COM *off_p = off; 3503*10923SEvan.Yan@Sun.COM 3504*10923SEvan.Yan@Sun.COM do { 3505*10923SEvan.Yan@Sun.COM me = DI_HP(di_mem_addr(st, off)); 3506*10923SEvan.Yan@Sun.COM me->self = off; 3507*10923SEvan.Yan@Sun.COM me->hp_name = 0; 3508*10923SEvan.Yan@Sun.COM me->hp_connection = (int)hp_hdl->cn_info.cn_num; 3509*10923SEvan.Yan@Sun.COM me->hp_depends_on = (int)hp_hdl->cn_info.cn_num_dpd_on; 3510*10923SEvan.Yan@Sun.COM (void) ddihp_cn_getstate(hp_hdl); 3511*10923SEvan.Yan@Sun.COM me->hp_state = (int)hp_hdl->cn_info.cn_state; 3512*10923SEvan.Yan@Sun.COM me->hp_type = (int)hp_hdl->cn_info.cn_type; 3513*10923SEvan.Yan@Sun.COM me->hp_type_str = 0; 3514*10923SEvan.Yan@Sun.COM me->hp_last_change = (uint32_t)hp_hdl->cn_info.cn_last_change; 3515*10923SEvan.Yan@Sun.COM me->hp_child = 0; 3516*10923SEvan.Yan@Sun.COM 3517*10923SEvan.Yan@Sun.COM /* 3518*10923SEvan.Yan@Sun.COM * Child links are resolved later by di_hotplug_children(). 3519*10923SEvan.Yan@Sun.COM * Store a reference to this di_hp_t in the list used later 3520*10923SEvan.Yan@Sun.COM * by di_hotplug_children(). 3521*10923SEvan.Yan@Sun.COM */ 3522*10923SEvan.Yan@Sun.COM hp = kmem_zalloc(sizeof (i_hp_t), KM_SLEEP); 3523*10923SEvan.Yan@Sun.COM hp->hp_off = off; 3524*10923SEvan.Yan@Sun.COM hp->hp_child = hp_hdl->cn_info.cn_child; 3525*10923SEvan.Yan@Sun.COM list_insert_tail(&st->hp_list, hp); 3526*10923SEvan.Yan@Sun.COM 3527*10923SEvan.Yan@Sun.COM off += sizeof (struct di_hp); 3528*10923SEvan.Yan@Sun.COM 3529*10923SEvan.Yan@Sun.COM /* Add name of this di_hp_t to the snapshot */ 3530*10923SEvan.Yan@Sun.COM if (hp_hdl->cn_info.cn_name) { 3531*10923SEvan.Yan@Sun.COM size = strlen(hp_hdl->cn_info.cn_name) + 1; 3532*10923SEvan.Yan@Sun.COM me->hp_name = off = di_checkmem(st, off, size); 3533*10923SEvan.Yan@Sun.COM (void) strcpy(di_mem_addr(st, off), 3534*10923SEvan.Yan@Sun.COM hp_hdl->cn_info.cn_name); 3535*10923SEvan.Yan@Sun.COM off += size; 3536*10923SEvan.Yan@Sun.COM } 3537*10923SEvan.Yan@Sun.COM 3538*10923SEvan.Yan@Sun.COM /* Add type description of this di_hp_t to the snapshot */ 3539*10923SEvan.Yan@Sun.COM if (hp_hdl->cn_info.cn_type_str) { 3540*10923SEvan.Yan@Sun.COM size = strlen(hp_hdl->cn_info.cn_type_str) + 1; 3541*10923SEvan.Yan@Sun.COM me->hp_type_str = off = di_checkmem(st, off, size); 3542*10923SEvan.Yan@Sun.COM (void) strcpy(di_mem_addr(st, off), 3543*10923SEvan.Yan@Sun.COM hp_hdl->cn_info.cn_type_str); 3544*10923SEvan.Yan@Sun.COM off += size; 3545*10923SEvan.Yan@Sun.COM } 3546*10923SEvan.Yan@Sun.COM 3547*10923SEvan.Yan@Sun.COM /* 3548*10923SEvan.Yan@Sun.COM * Set link to next in the chain of di_hp_t nodes, 3549*10923SEvan.Yan@Sun.COM * or terminate the chain when processing the last node. 3550*10923SEvan.Yan@Sun.COM */ 3551*10923SEvan.Yan@Sun.COM if (hp_hdl->next != NULL) { 3552*10923SEvan.Yan@Sun.COM off = di_checkmem(st, off, sizeof (struct di_hp)); 3553*10923SEvan.Yan@Sun.COM me->next = off; 3554*10923SEvan.Yan@Sun.COM } else { 3555*10923SEvan.Yan@Sun.COM me->next = 0; 3556*10923SEvan.Yan@Sun.COM } 3557*10923SEvan.Yan@Sun.COM 3558*10923SEvan.Yan@Sun.COM /* Update pointer to next in the chain */ 3559*10923SEvan.Yan@Sun.COM hp_hdl = hp_hdl->next; 3560*10923SEvan.Yan@Sun.COM 3561*10923SEvan.Yan@Sun.COM } while (hp_hdl); 3562*10923SEvan.Yan@Sun.COM 3563*10923SEvan.Yan@Sun.COM return (off); 3564*10923SEvan.Yan@Sun.COM } 3565*10923SEvan.Yan@Sun.COM 3566*10923SEvan.Yan@Sun.COM /* 35670Sstevel@tonic-gate * The driver is stateful across DINFOCPYALL and DINFOUSRLD. 35680Sstevel@tonic-gate * This function encapsulates the state machine: 35690Sstevel@tonic-gate * 35700Sstevel@tonic-gate * -> IOC_IDLE -> IOC_SNAP -> IOC_DONE -> IOC_COPY -> 35710Sstevel@tonic-gate * | SNAPSHOT USRLD | 35720Sstevel@tonic-gate * -------------------------------------------------- 35730Sstevel@tonic-gate * 35740Sstevel@tonic-gate * Returns 0 on success and -1 on failure 35750Sstevel@tonic-gate */ 35760Sstevel@tonic-gate static int 35770Sstevel@tonic-gate di_setstate(struct di_state *st, int new_state) 35780Sstevel@tonic-gate { 35797224Scth int ret = 0; 35800Sstevel@tonic-gate 35810Sstevel@tonic-gate mutex_enter(&di_lock); 35820Sstevel@tonic-gate switch (new_state) { 35830Sstevel@tonic-gate case IOC_IDLE: 35840Sstevel@tonic-gate case IOC_DONE: 35850Sstevel@tonic-gate break; 35860Sstevel@tonic-gate case IOC_SNAP: 35870Sstevel@tonic-gate if (st->di_iocstate != IOC_IDLE) 35880Sstevel@tonic-gate ret = -1; 35890Sstevel@tonic-gate break; 35900Sstevel@tonic-gate case IOC_COPY: 35910Sstevel@tonic-gate if (st->di_iocstate != IOC_DONE) 35920Sstevel@tonic-gate ret = -1; 35930Sstevel@tonic-gate break; 35940Sstevel@tonic-gate default: 35950Sstevel@tonic-gate ret = -1; 35960Sstevel@tonic-gate } 35970Sstevel@tonic-gate 35980Sstevel@tonic-gate if (ret == 0) 35990Sstevel@tonic-gate st->di_iocstate = new_state; 36000Sstevel@tonic-gate else 36010Sstevel@tonic-gate cmn_err(CE_NOTE, "incorrect state transition from %d to %d", 36020Sstevel@tonic-gate st->di_iocstate, new_state); 36030Sstevel@tonic-gate mutex_exit(&di_lock); 36040Sstevel@tonic-gate return (ret); 36050Sstevel@tonic-gate } 36060Sstevel@tonic-gate 36070Sstevel@tonic-gate /* 36080Sstevel@tonic-gate * We cannot assume the presence of the entire 36090Sstevel@tonic-gate * snapshot in this routine. All we are guaranteed 36100Sstevel@tonic-gate * is the di_all struct + 1 byte (for root_path) 36110Sstevel@tonic-gate */ 36120Sstevel@tonic-gate static int 36130Sstevel@tonic-gate header_plus_one_ok(struct di_all *all) 36140Sstevel@tonic-gate { 36150Sstevel@tonic-gate /* 36160Sstevel@tonic-gate * Refuse to read old versions 36170Sstevel@tonic-gate */ 36180Sstevel@tonic-gate if (all->version != DI_SNAPSHOT_VERSION) { 36190Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "bad version: 0x%x", all->version)); 36200Sstevel@tonic-gate return (0); 36210Sstevel@tonic-gate } 36220Sstevel@tonic-gate 36230Sstevel@tonic-gate if (all->cache_magic != DI_CACHE_MAGIC) { 36240Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "bad magic #: 0x%x", all->cache_magic)); 36250Sstevel@tonic-gate return (0); 36260Sstevel@tonic-gate } 36270Sstevel@tonic-gate 36283133Sjg if (all->snapshot_time == 0) { 36290Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "bad timestamp: %ld", all->snapshot_time)); 36300Sstevel@tonic-gate return (0); 36310Sstevel@tonic-gate } 36320Sstevel@tonic-gate 36330Sstevel@tonic-gate if (all->top_devinfo == 0) { 36340Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "NULL top devinfo")); 36350Sstevel@tonic-gate return (0); 36360Sstevel@tonic-gate } 36370Sstevel@tonic-gate 36380Sstevel@tonic-gate if (all->map_size < sizeof (*all) + 1) { 36390Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "bad map size: %u", all->map_size)); 36400Sstevel@tonic-gate return (0); 36410Sstevel@tonic-gate } 36420Sstevel@tonic-gate 36430Sstevel@tonic-gate if (all->root_path[0] != '/' || all->root_path[1] != '\0') { 36440Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "bad rootpath: %c%c", 36450Sstevel@tonic-gate all->root_path[0], all->root_path[1])); 36460Sstevel@tonic-gate return (0); 36470Sstevel@tonic-gate } 36480Sstevel@tonic-gate 36490Sstevel@tonic-gate /* 36500Sstevel@tonic-gate * We can't check checksum here as we just have the header 36510Sstevel@tonic-gate */ 36520Sstevel@tonic-gate 36530Sstevel@tonic-gate return (1); 36540Sstevel@tonic-gate } 36550Sstevel@tonic-gate 36560Sstevel@tonic-gate static int 36570Sstevel@tonic-gate chunk_write(struct vnode *vp, offset_t off, caddr_t buf, size_t len) 36580Sstevel@tonic-gate { 36590Sstevel@tonic-gate rlim64_t rlimit; 36600Sstevel@tonic-gate ssize_t resid; 36610Sstevel@tonic-gate int error = 0; 36620Sstevel@tonic-gate 36630Sstevel@tonic-gate 36640Sstevel@tonic-gate rlimit = RLIM64_INFINITY; 36650Sstevel@tonic-gate 36660Sstevel@tonic-gate while (len) { 36670Sstevel@tonic-gate resid = 0; 36680Sstevel@tonic-gate error = vn_rdwr(UIO_WRITE, vp, buf, len, off, 36690Sstevel@tonic-gate UIO_SYSSPACE, FSYNC, rlimit, kcred, &resid); 36700Sstevel@tonic-gate 36710Sstevel@tonic-gate if (error || resid < 0) { 36720Sstevel@tonic-gate error = error ? error : EIO; 36730Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "write error: %d", error)); 36740Sstevel@tonic-gate break; 36750Sstevel@tonic-gate } 36760Sstevel@tonic-gate 36770Sstevel@tonic-gate /* 36780Sstevel@tonic-gate * Check if we are making progress 36790Sstevel@tonic-gate */ 36800Sstevel@tonic-gate if (resid >= len) { 36810Sstevel@tonic-gate error = ENOSPC; 36820Sstevel@tonic-gate break; 36830Sstevel@tonic-gate } 36840Sstevel@tonic-gate buf += len - resid; 36850Sstevel@tonic-gate off += len - resid; 36860Sstevel@tonic-gate len = resid; 36870Sstevel@tonic-gate } 36880Sstevel@tonic-gate 36890Sstevel@tonic-gate return (error); 36900Sstevel@tonic-gate } 36910Sstevel@tonic-gate 36920Sstevel@tonic-gate static void 36930Sstevel@tonic-gate di_cache_write(struct di_cache *cache) 36940Sstevel@tonic-gate { 36950Sstevel@tonic-gate struct di_all *all; 36960Sstevel@tonic-gate struct vnode *vp; 36970Sstevel@tonic-gate int oflags; 36980Sstevel@tonic-gate size_t map_size; 36990Sstevel@tonic-gate size_t chunk; 37000Sstevel@tonic-gate offset_t off; 37010Sstevel@tonic-gate int error; 37020Sstevel@tonic-gate char *buf; 37030Sstevel@tonic-gate 37040Sstevel@tonic-gate ASSERT(DI_CACHE_LOCKED(*cache)); 37050Sstevel@tonic-gate ASSERT(!servicing_interrupt()); 37060Sstevel@tonic-gate 37070Sstevel@tonic-gate if (cache->cache_size == 0) { 37080Sstevel@tonic-gate ASSERT(cache->cache_data == NULL); 37090Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "Empty cache. Skipping write")); 37100Sstevel@tonic-gate return; 37110Sstevel@tonic-gate } 37120Sstevel@tonic-gate 37130Sstevel@tonic-gate ASSERT(cache->cache_size > 0); 37140Sstevel@tonic-gate ASSERT(cache->cache_data); 37150Sstevel@tonic-gate 37160Sstevel@tonic-gate if (!modrootloaded || rootvp == NULL || vn_is_readonly(rootvp)) { 37170Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "Can't write to rootFS. Skipping write")); 37180Sstevel@tonic-gate return; 37190Sstevel@tonic-gate } 37200Sstevel@tonic-gate 37210Sstevel@tonic-gate all = (struct di_all *)cache->cache_data; 37220Sstevel@tonic-gate 37230Sstevel@tonic-gate if (!header_plus_one_ok(all)) { 37240Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "Invalid header. Skipping write")); 37250Sstevel@tonic-gate return; 37260Sstevel@tonic-gate } 37270Sstevel@tonic-gate 37280Sstevel@tonic-gate ASSERT(strcmp(all->root_path, "/") == 0); 37290Sstevel@tonic-gate 37300Sstevel@tonic-gate /* 37310Sstevel@tonic-gate * The cache_size is the total allocated memory for the cache. 37320Sstevel@tonic-gate * The map_size is the actual size of valid data in the cache. 37330Sstevel@tonic-gate * map_size may be smaller than cache_size but cannot exceed 37340Sstevel@tonic-gate * cache_size. 37350Sstevel@tonic-gate */ 37360Sstevel@tonic-gate if (all->map_size > cache->cache_size) { 37370Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "map_size (0x%x) > cache_size (0x%x)." 37380Sstevel@tonic-gate " Skipping write", all->map_size, cache->cache_size)); 37390Sstevel@tonic-gate return; 37400Sstevel@tonic-gate } 37410Sstevel@tonic-gate 37420Sstevel@tonic-gate /* 37430Sstevel@tonic-gate * First unlink the temp file 37440Sstevel@tonic-gate */ 37450Sstevel@tonic-gate error = vn_remove(DI_CACHE_TEMP, UIO_SYSSPACE, RMFILE); 37460Sstevel@tonic-gate if (error && error != ENOENT) { 37470Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "%s: unlink failed: %d", 37480Sstevel@tonic-gate DI_CACHE_TEMP, error)); 37490Sstevel@tonic-gate } 37500Sstevel@tonic-gate 37510Sstevel@tonic-gate if (error == EROFS) { 37520Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "RDONLY FS. Skipping write")); 37530Sstevel@tonic-gate return; 37540Sstevel@tonic-gate } 37550Sstevel@tonic-gate 37560Sstevel@tonic-gate vp = NULL; 37570Sstevel@tonic-gate oflags = (FCREAT|FWRITE); 37580Sstevel@tonic-gate if (error = vn_open(DI_CACHE_TEMP, UIO_SYSSPACE, oflags, 37590Sstevel@tonic-gate DI_CACHE_PERMS, &vp, CRCREAT, 0)) { 37600Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "%s: create failed: %d", 37610Sstevel@tonic-gate DI_CACHE_TEMP, error)); 37620Sstevel@tonic-gate return; 37630Sstevel@tonic-gate } 37640Sstevel@tonic-gate 37650Sstevel@tonic-gate ASSERT(vp); 37660Sstevel@tonic-gate 37670Sstevel@tonic-gate /* 37680Sstevel@tonic-gate * Paranoid: Check if the file is on a read-only FS 37690Sstevel@tonic-gate */ 37700Sstevel@tonic-gate if (vn_is_readonly(vp)) { 37710Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "cannot write: readonly FS")); 37720Sstevel@tonic-gate goto fail; 37730Sstevel@tonic-gate } 37740Sstevel@tonic-gate 37750Sstevel@tonic-gate /* 37760Sstevel@tonic-gate * Note that we only write map_size bytes to disk - this saves 37770Sstevel@tonic-gate * space as the actual cache size may be larger than size of 37780Sstevel@tonic-gate * valid data in the cache. 37790Sstevel@tonic-gate * Another advantage is that it makes verification of size 37800Sstevel@tonic-gate * easier when the file is read later. 37810Sstevel@tonic-gate */ 37820Sstevel@tonic-gate map_size = all->map_size; 37830Sstevel@tonic-gate off = 0; 37840Sstevel@tonic-gate buf = cache->cache_data; 37850Sstevel@tonic-gate 37860Sstevel@tonic-gate while (map_size) { 37870Sstevel@tonic-gate ASSERT(map_size > 0); 37880Sstevel@tonic-gate /* 37890Sstevel@tonic-gate * Write in chunks so that VM system 37900Sstevel@tonic-gate * is not overwhelmed 37910Sstevel@tonic-gate */ 37920Sstevel@tonic-gate if (map_size > di_chunk * PAGESIZE) 37930Sstevel@tonic-gate chunk = di_chunk * PAGESIZE; 37940Sstevel@tonic-gate else 37950Sstevel@tonic-gate chunk = map_size; 37960Sstevel@tonic-gate 37970Sstevel@tonic-gate error = chunk_write(vp, off, buf, chunk); 37980Sstevel@tonic-gate if (error) { 37990Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "write failed: off=0x%x: %d", 38000Sstevel@tonic-gate off, error)); 38010Sstevel@tonic-gate goto fail; 38020Sstevel@tonic-gate } 38030Sstevel@tonic-gate 38040Sstevel@tonic-gate off += chunk; 38050Sstevel@tonic-gate buf += chunk; 38060Sstevel@tonic-gate map_size -= chunk; 38070Sstevel@tonic-gate 38087224Scth /* If low on memory, give pageout a chance to run */ 38097224Scth if (freemem < desfree) 38107224Scth delay(1); 38110Sstevel@tonic-gate } 38120Sstevel@tonic-gate 38130Sstevel@tonic-gate /* 38140Sstevel@tonic-gate * Now sync the file and close it 38150Sstevel@tonic-gate */ 38165331Samw if (error = VOP_FSYNC(vp, FSYNC, kcred, NULL)) { 38170Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "FSYNC failed: %d", error)); 38180Sstevel@tonic-gate } 38190Sstevel@tonic-gate 38205331Samw if (error = VOP_CLOSE(vp, oflags, 1, (offset_t)0, kcred, NULL)) { 38210Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "close() failed: %d", error)); 38220Sstevel@tonic-gate VN_RELE(vp); 38230Sstevel@tonic-gate return; 38240Sstevel@tonic-gate } 38250Sstevel@tonic-gate 38260Sstevel@tonic-gate VN_RELE(vp); 38270Sstevel@tonic-gate 38280Sstevel@tonic-gate /* 38290Sstevel@tonic-gate * Now do the rename 38300Sstevel@tonic-gate */ 38310Sstevel@tonic-gate if (error = vn_rename(DI_CACHE_TEMP, DI_CACHE_FILE, UIO_SYSSPACE)) { 38320Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "rename failed: %d", error)); 38330Sstevel@tonic-gate return; 38340Sstevel@tonic-gate } 38350Sstevel@tonic-gate 38360Sstevel@tonic-gate CACHE_DEBUG((DI_INFO, "Cache write successful.")); 38370Sstevel@tonic-gate 38380Sstevel@tonic-gate return; 38390Sstevel@tonic-gate 38400Sstevel@tonic-gate fail: 38415331Samw (void) VOP_CLOSE(vp, oflags, 1, (offset_t)0, kcred, NULL); 38420Sstevel@tonic-gate VN_RELE(vp); 38430Sstevel@tonic-gate } 38440Sstevel@tonic-gate 38450Sstevel@tonic-gate 38460Sstevel@tonic-gate /* 38470Sstevel@tonic-gate * Since we could be called early in boot, 38480Sstevel@tonic-gate * use kobj_read_file() 38490Sstevel@tonic-gate */ 38500Sstevel@tonic-gate static void 38510Sstevel@tonic-gate di_cache_read(struct di_cache *cache) 38520Sstevel@tonic-gate { 38530Sstevel@tonic-gate struct _buf *file; 38540Sstevel@tonic-gate struct di_all *all; 38550Sstevel@tonic-gate int n; 38560Sstevel@tonic-gate size_t map_size, sz, chunk; 38570Sstevel@tonic-gate offset_t off; 38580Sstevel@tonic-gate caddr_t buf; 38590Sstevel@tonic-gate uint32_t saved_crc, crc; 38600Sstevel@tonic-gate 38610Sstevel@tonic-gate ASSERT(modrootloaded); 38620Sstevel@tonic-gate ASSERT(DI_CACHE_LOCKED(*cache)); 38630Sstevel@tonic-gate ASSERT(cache->cache_data == NULL); 38640Sstevel@tonic-gate ASSERT(cache->cache_size == 0); 38650Sstevel@tonic-gate ASSERT(!servicing_interrupt()); 38660Sstevel@tonic-gate 38670Sstevel@tonic-gate file = kobj_open_file(DI_CACHE_FILE); 38680Sstevel@tonic-gate if (file == (struct _buf *)-1) { 38690Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "%s: open failed: %d", 38700Sstevel@tonic-gate DI_CACHE_FILE, ENOENT)); 38710Sstevel@tonic-gate return; 38720Sstevel@tonic-gate } 38730Sstevel@tonic-gate 38740Sstevel@tonic-gate /* 38750Sstevel@tonic-gate * Read in the header+root_path first. The root_path must be "/" 38760Sstevel@tonic-gate */ 38770Sstevel@tonic-gate all = kmem_zalloc(sizeof (*all) + 1, KM_SLEEP); 38780Sstevel@tonic-gate n = kobj_read_file(file, (caddr_t)all, sizeof (*all) + 1, 0); 38790Sstevel@tonic-gate 38800Sstevel@tonic-gate if ((n != sizeof (*all) + 1) || !header_plus_one_ok(all)) { 38810Sstevel@tonic-gate kmem_free(all, sizeof (*all) + 1); 38820Sstevel@tonic-gate kobj_close_file(file); 38830Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "cache header: read error or invalid")); 38840Sstevel@tonic-gate return; 38850Sstevel@tonic-gate } 38860Sstevel@tonic-gate 38870Sstevel@tonic-gate map_size = all->map_size; 38880Sstevel@tonic-gate 38890Sstevel@tonic-gate kmem_free(all, sizeof (*all) + 1); 38900Sstevel@tonic-gate 38910Sstevel@tonic-gate ASSERT(map_size >= sizeof (*all) + 1); 38920Sstevel@tonic-gate 38930Sstevel@tonic-gate buf = di_cache.cache_data = kmem_alloc(map_size, KM_SLEEP); 38940Sstevel@tonic-gate sz = map_size; 38950Sstevel@tonic-gate off = 0; 38960Sstevel@tonic-gate while (sz) { 38970Sstevel@tonic-gate /* Don't overload VM with large reads */ 38980Sstevel@tonic-gate chunk = (sz > di_chunk * PAGESIZE) ? di_chunk * PAGESIZE : sz; 38990Sstevel@tonic-gate n = kobj_read_file(file, buf, chunk, off); 39000Sstevel@tonic-gate if (n != chunk) { 39010Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "%s: read error at offset: %lld", 39020Sstevel@tonic-gate DI_CACHE_FILE, off)); 39030Sstevel@tonic-gate goto fail; 39040Sstevel@tonic-gate } 39050Sstevel@tonic-gate off += chunk; 39060Sstevel@tonic-gate buf += chunk; 39070Sstevel@tonic-gate sz -= chunk; 39080Sstevel@tonic-gate } 39090Sstevel@tonic-gate 39100Sstevel@tonic-gate ASSERT(off == map_size); 39110Sstevel@tonic-gate 39120Sstevel@tonic-gate /* 39130Sstevel@tonic-gate * Read past expected EOF to verify size. 39140Sstevel@tonic-gate */ 39150Sstevel@tonic-gate if (kobj_read_file(file, (caddr_t)&sz, 1, off) > 0) { 39160Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "%s: file size changed", DI_CACHE_FILE)); 39170Sstevel@tonic-gate goto fail; 39180Sstevel@tonic-gate } 39190Sstevel@tonic-gate 39200Sstevel@tonic-gate all = (struct di_all *)di_cache.cache_data; 39210Sstevel@tonic-gate if (!header_plus_one_ok(all)) { 39220Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "%s: file header changed", DI_CACHE_FILE)); 39230Sstevel@tonic-gate goto fail; 39240Sstevel@tonic-gate } 39250Sstevel@tonic-gate 39260Sstevel@tonic-gate /* 39270Sstevel@tonic-gate * Compute CRC with checksum field in the cache data set to 0 39280Sstevel@tonic-gate */ 39290Sstevel@tonic-gate saved_crc = all->cache_checksum; 39300Sstevel@tonic-gate all->cache_checksum = 0; 39310Sstevel@tonic-gate CRC32(crc, di_cache.cache_data, map_size, -1U, crc32_table); 39320Sstevel@tonic-gate all->cache_checksum = saved_crc; 39330Sstevel@tonic-gate 39340Sstevel@tonic-gate if (crc != all->cache_checksum) { 39350Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, 39360Sstevel@tonic-gate "%s: checksum error: expected=0x%x actual=0x%x", 39370Sstevel@tonic-gate DI_CACHE_FILE, all->cache_checksum, crc)); 39380Sstevel@tonic-gate goto fail; 39390Sstevel@tonic-gate } 39400Sstevel@tonic-gate 39410Sstevel@tonic-gate if (all->map_size != map_size) { 39420Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "%s: map size changed", DI_CACHE_FILE)); 39430Sstevel@tonic-gate goto fail; 39440Sstevel@tonic-gate } 39450Sstevel@tonic-gate 39460Sstevel@tonic-gate kobj_close_file(file); 39470Sstevel@tonic-gate 39480Sstevel@tonic-gate di_cache.cache_size = map_size; 39490Sstevel@tonic-gate 39500Sstevel@tonic-gate return; 39510Sstevel@tonic-gate 39520Sstevel@tonic-gate fail: 39530Sstevel@tonic-gate kmem_free(di_cache.cache_data, map_size); 39540Sstevel@tonic-gate kobj_close_file(file); 39550Sstevel@tonic-gate di_cache.cache_data = NULL; 39560Sstevel@tonic-gate di_cache.cache_size = 0; 39570Sstevel@tonic-gate } 39580Sstevel@tonic-gate 39590Sstevel@tonic-gate 39600Sstevel@tonic-gate /* 39610Sstevel@tonic-gate * Checks if arguments are valid for using the cache. 39620Sstevel@tonic-gate */ 39630Sstevel@tonic-gate static int 39640Sstevel@tonic-gate cache_args_valid(struct di_state *st, int *error) 39650Sstevel@tonic-gate { 39660Sstevel@tonic-gate ASSERT(error); 39670Sstevel@tonic-gate ASSERT(st->mem_size > 0); 39680Sstevel@tonic-gate ASSERT(st->memlist != NULL); 39690Sstevel@tonic-gate 39700Sstevel@tonic-gate if (!modrootloaded || !i_ddi_io_initialized()) { 39710Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, 39720Sstevel@tonic-gate "cache lookup failure: I/O subsystem not inited")); 39730Sstevel@tonic-gate *error = ENOTACTIVE; 39740Sstevel@tonic-gate return (0); 39750Sstevel@tonic-gate } 39760Sstevel@tonic-gate 39770Sstevel@tonic-gate /* 39780Sstevel@tonic-gate * No other flags allowed with DINFOCACHE 39790Sstevel@tonic-gate */ 39800Sstevel@tonic-gate if (st->command != (DINFOCACHE & DIIOC_MASK)) { 39810Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, 39820Sstevel@tonic-gate "cache lookup failure: bad flags: 0x%x", 39830Sstevel@tonic-gate st->command)); 39840Sstevel@tonic-gate *error = EINVAL; 39850Sstevel@tonic-gate return (0); 39860Sstevel@tonic-gate } 39870Sstevel@tonic-gate 39880Sstevel@tonic-gate if (strcmp(DI_ALL_PTR(st)->root_path, "/") != 0) { 39890Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, 39900Sstevel@tonic-gate "cache lookup failure: bad root: %s", 39910Sstevel@tonic-gate DI_ALL_PTR(st)->root_path)); 39920Sstevel@tonic-gate *error = EINVAL; 39930Sstevel@tonic-gate return (0); 39940Sstevel@tonic-gate } 39950Sstevel@tonic-gate 39960Sstevel@tonic-gate CACHE_DEBUG((DI_INFO, "cache lookup args ok: 0x%x", st->command)); 39970Sstevel@tonic-gate 39980Sstevel@tonic-gate *error = 0; 39990Sstevel@tonic-gate 40000Sstevel@tonic-gate return (1); 40010Sstevel@tonic-gate } 40020Sstevel@tonic-gate 40030Sstevel@tonic-gate static int 40040Sstevel@tonic-gate snapshot_is_cacheable(struct di_state *st) 40050Sstevel@tonic-gate { 40060Sstevel@tonic-gate ASSERT(st->mem_size > 0); 40070Sstevel@tonic-gate ASSERT(st->memlist != NULL); 40080Sstevel@tonic-gate 4009878Sramat if ((st->command & DI_CACHE_SNAPSHOT_FLAGS) != 4010878Sramat (DI_CACHE_SNAPSHOT_FLAGS & DIIOC_MASK)) { 40110Sstevel@tonic-gate CACHE_DEBUG((DI_INFO, 40120Sstevel@tonic-gate "not cacheable: incompatible flags: 0x%x", 40130Sstevel@tonic-gate st->command)); 40140Sstevel@tonic-gate return (0); 40150Sstevel@tonic-gate } 40160Sstevel@tonic-gate 40170Sstevel@tonic-gate if (strcmp(DI_ALL_PTR(st)->root_path, "/") != 0) { 40180Sstevel@tonic-gate CACHE_DEBUG((DI_INFO, 40190Sstevel@tonic-gate "not cacheable: incompatible root path: %s", 40200Sstevel@tonic-gate DI_ALL_PTR(st)->root_path)); 40210Sstevel@tonic-gate return (0); 40220Sstevel@tonic-gate } 40230Sstevel@tonic-gate 40240Sstevel@tonic-gate CACHE_DEBUG((DI_INFO, "cacheable snapshot request: 0x%x", st->command)); 40250Sstevel@tonic-gate 40260Sstevel@tonic-gate return (1); 40270Sstevel@tonic-gate } 40280Sstevel@tonic-gate 40290Sstevel@tonic-gate static int 40300Sstevel@tonic-gate di_cache_lookup(struct di_state *st) 40310Sstevel@tonic-gate { 40320Sstevel@tonic-gate size_t rval; 40330Sstevel@tonic-gate int cache_valid; 40340Sstevel@tonic-gate 40350Sstevel@tonic-gate ASSERT(cache_args_valid(st, &cache_valid)); 40360Sstevel@tonic-gate ASSERT(modrootloaded); 40370Sstevel@tonic-gate 40380Sstevel@tonic-gate DI_CACHE_LOCK(di_cache); 40390Sstevel@tonic-gate 40400Sstevel@tonic-gate /* 40410Sstevel@tonic-gate * The following assignment determines the validity 40420Sstevel@tonic-gate * of the cache as far as this snapshot is concerned. 40430Sstevel@tonic-gate */ 40440Sstevel@tonic-gate cache_valid = di_cache.cache_valid; 40450Sstevel@tonic-gate 40460Sstevel@tonic-gate if (cache_valid && di_cache.cache_data == NULL) { 40470Sstevel@tonic-gate di_cache_read(&di_cache); 40480Sstevel@tonic-gate /* check for read or file error */ 40490Sstevel@tonic-gate if (di_cache.cache_data == NULL) 40500Sstevel@tonic-gate cache_valid = 0; 40510Sstevel@tonic-gate } 40520Sstevel@tonic-gate 40530Sstevel@tonic-gate if (cache_valid) { 40540Sstevel@tonic-gate /* 40550Sstevel@tonic-gate * Ok, the cache was valid as of this particular 40560Sstevel@tonic-gate * snapshot. Copy the cached snapshot. This is safe 40570Sstevel@tonic-gate * to do as the cache cannot be freed (we hold the 40580Sstevel@tonic-gate * cache lock). Free the memory allocated in di_state 40590Sstevel@tonic-gate * up until this point - we will simply copy everything 40600Sstevel@tonic-gate * in the cache. 40610Sstevel@tonic-gate */ 40620Sstevel@tonic-gate 40630Sstevel@tonic-gate ASSERT(di_cache.cache_data != NULL); 40640Sstevel@tonic-gate ASSERT(di_cache.cache_size > 0); 40650Sstevel@tonic-gate 40660Sstevel@tonic-gate di_freemem(st); 40670Sstevel@tonic-gate 40680Sstevel@tonic-gate rval = 0; 40690Sstevel@tonic-gate if (di_cache2mem(&di_cache, st) > 0) { 40700Sstevel@tonic-gate /* 40710Sstevel@tonic-gate * map_size is size of valid data in the 40720Sstevel@tonic-gate * cached snapshot and may be less than 40730Sstevel@tonic-gate * size of the cache. 40740Sstevel@tonic-gate */ 40757224Scth ASSERT(DI_ALL_PTR(st)); 40760Sstevel@tonic-gate rval = DI_ALL_PTR(st)->map_size; 40770Sstevel@tonic-gate 40780Sstevel@tonic-gate ASSERT(rval >= sizeof (struct di_all)); 40790Sstevel@tonic-gate ASSERT(rval <= di_cache.cache_size); 40800Sstevel@tonic-gate } 40810Sstevel@tonic-gate } else { 40820Sstevel@tonic-gate /* 40830Sstevel@tonic-gate * The cache isn't valid, we need to take a snapshot. 40840Sstevel@tonic-gate * Set the command flags appropriately 40850Sstevel@tonic-gate */ 40860Sstevel@tonic-gate ASSERT(st->command == (DINFOCACHE & DIIOC_MASK)); 40870Sstevel@tonic-gate st->command = (DI_CACHE_SNAPSHOT_FLAGS & DIIOC_MASK); 40880Sstevel@tonic-gate rval = di_cache_update(st); 40890Sstevel@tonic-gate st->command = (DINFOCACHE & DIIOC_MASK); 40900Sstevel@tonic-gate } 40910Sstevel@tonic-gate 40920Sstevel@tonic-gate DI_CACHE_UNLOCK(di_cache); 40930Sstevel@tonic-gate 40940Sstevel@tonic-gate /* 40950Sstevel@tonic-gate * For cached snapshots, the devinfo driver always returns 40960Sstevel@tonic-gate * a snapshot rooted at "/". 40970Sstevel@tonic-gate */ 40980Sstevel@tonic-gate ASSERT(rval == 0 || strcmp(DI_ALL_PTR(st)->root_path, "/") == 0); 40990Sstevel@tonic-gate 41003133Sjg return ((int)rval); 41010Sstevel@tonic-gate } 41020Sstevel@tonic-gate 41030Sstevel@tonic-gate /* 41040Sstevel@tonic-gate * This is a forced update of the cache - the previous state of the cache 41050Sstevel@tonic-gate * may be: 41060Sstevel@tonic-gate * - unpopulated 41070Sstevel@tonic-gate * - populated and invalid 41080Sstevel@tonic-gate * - populated and valid 41090Sstevel@tonic-gate */ 41100Sstevel@tonic-gate static int 41110Sstevel@tonic-gate di_cache_update(struct di_state *st) 41120Sstevel@tonic-gate { 41137224Scth int rval; 41147224Scth uint32_t crc; 41157224Scth struct di_all *all; 41160Sstevel@tonic-gate 41170Sstevel@tonic-gate ASSERT(DI_CACHE_LOCKED(di_cache)); 41180Sstevel@tonic-gate ASSERT(snapshot_is_cacheable(st)); 41190Sstevel@tonic-gate 41200Sstevel@tonic-gate /* 41210Sstevel@tonic-gate * Free the in-core cache and the on-disk file (if they exist) 41220Sstevel@tonic-gate */ 41230Sstevel@tonic-gate i_ddi_di_cache_free(&di_cache); 41240Sstevel@tonic-gate 41250Sstevel@tonic-gate /* 41260Sstevel@tonic-gate * Set valid flag before taking the snapshot, 41270Sstevel@tonic-gate * so that any invalidations that arrive 41280Sstevel@tonic-gate * during or after the snapshot are not 41290Sstevel@tonic-gate * removed by us. 41300Sstevel@tonic-gate */ 41310Sstevel@tonic-gate atomic_or_32(&di_cache.cache_valid, 1); 41320Sstevel@tonic-gate 4133878Sramat rval = di_snapshot_and_clean(st); 41340Sstevel@tonic-gate 41350Sstevel@tonic-gate if (rval == 0) { 41360Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "can't update cache: bad snapshot")); 41370Sstevel@tonic-gate return (0); 41380Sstevel@tonic-gate } 41390Sstevel@tonic-gate 41400Sstevel@tonic-gate DI_ALL_PTR(st)->map_size = rval; 41410Sstevel@tonic-gate if (di_mem2cache(st, &di_cache) == 0) { 41420Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "can't update cache: copy failed")); 41430Sstevel@tonic-gate return (0); 41440Sstevel@tonic-gate } 41450Sstevel@tonic-gate 41460Sstevel@tonic-gate ASSERT(di_cache.cache_data); 41470Sstevel@tonic-gate ASSERT(di_cache.cache_size > 0); 41480Sstevel@tonic-gate 41490Sstevel@tonic-gate /* 41500Sstevel@tonic-gate * Now that we have cached the snapshot, compute its checksum. 41510Sstevel@tonic-gate * The checksum is only computed over the valid data in the 41520Sstevel@tonic-gate * cache, not the entire cache. 41530Sstevel@tonic-gate * Also, set all the fields (except checksum) before computing 41540Sstevel@tonic-gate * checksum. 41550Sstevel@tonic-gate */ 41560Sstevel@tonic-gate all = (struct di_all *)di_cache.cache_data; 41570Sstevel@tonic-gate all->cache_magic = DI_CACHE_MAGIC; 41580Sstevel@tonic-gate all->map_size = rval; 41590Sstevel@tonic-gate 41600Sstevel@tonic-gate ASSERT(all->cache_checksum == 0); 41610Sstevel@tonic-gate CRC32(crc, di_cache.cache_data, all->map_size, -1U, crc32_table); 41620Sstevel@tonic-gate all->cache_checksum = crc; 41630Sstevel@tonic-gate 41640Sstevel@tonic-gate di_cache_write(&di_cache); 41650Sstevel@tonic-gate 41660Sstevel@tonic-gate return (rval); 41670Sstevel@tonic-gate } 41680Sstevel@tonic-gate 41690Sstevel@tonic-gate static void 41700Sstevel@tonic-gate di_cache_print(di_cache_debug_t msglevel, char *fmt, ...) 41710Sstevel@tonic-gate { 41720Sstevel@tonic-gate va_list ap; 41730Sstevel@tonic-gate 41740Sstevel@tonic-gate if (di_cache_debug <= DI_QUIET) 41750Sstevel@tonic-gate return; 41760Sstevel@tonic-gate 41770Sstevel@tonic-gate if (di_cache_debug < msglevel) 41780Sstevel@tonic-gate return; 41790Sstevel@tonic-gate 41800Sstevel@tonic-gate switch (msglevel) { 41810Sstevel@tonic-gate case DI_ERR: 41820Sstevel@tonic-gate msglevel = CE_WARN; 41830Sstevel@tonic-gate break; 41840Sstevel@tonic-gate case DI_INFO: 41850Sstevel@tonic-gate case DI_TRACE: 41860Sstevel@tonic-gate default: 41870Sstevel@tonic-gate msglevel = CE_NOTE; 41880Sstevel@tonic-gate break; 41890Sstevel@tonic-gate } 41900Sstevel@tonic-gate 41910Sstevel@tonic-gate va_start(ap, fmt); 41920Sstevel@tonic-gate vcmn_err(msglevel, fmt, ap); 41930Sstevel@tonic-gate va_end(ap); 41940Sstevel@tonic-gate } 4195*10923SEvan.Yan@Sun.COM 4196*10923SEvan.Yan@Sun.COM static void 4197*10923SEvan.Yan@Sun.COM di_hotplug_children(struct di_state *st) 4198*10923SEvan.Yan@Sun.COM { 4199*10923SEvan.Yan@Sun.COM di_off_t off; 4200*10923SEvan.Yan@Sun.COM struct di_hp *hp; 4201*10923SEvan.Yan@Sun.COM struct i_hp *hp_list_node; 4202*10923SEvan.Yan@Sun.COM 4203*10923SEvan.Yan@Sun.COM while (hp_list_node = (struct i_hp *)list_remove_head(&st->hp_list)) { 4204*10923SEvan.Yan@Sun.COM 4205*10923SEvan.Yan@Sun.COM if ((hp_list_node->hp_child != NULL) && 4206*10923SEvan.Yan@Sun.COM (di_dip_find(st, hp_list_node->hp_child, &off) == 0)) { 4207*10923SEvan.Yan@Sun.COM hp = DI_HP(di_mem_addr(st, hp_list_node->hp_off)); 4208*10923SEvan.Yan@Sun.COM hp->hp_child = off; 4209*10923SEvan.Yan@Sun.COM } 4210*10923SEvan.Yan@Sun.COM 4211*10923SEvan.Yan@Sun.COM kmem_free(hp_list_node, sizeof (i_hp_t)); 4212*10923SEvan.Yan@Sun.COM } 4213*10923SEvan.Yan@Sun.COM 4214*10923SEvan.Yan@Sun.COM list_destroy(&st->hp_list); 4215*10923SEvan.Yan@Sun.COM } 4216