17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5c3b0fe9bScth * Common Development and Distribution License (the "License").
6c3b0fe9bScth * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21f9722deaSChris Horne
227c478bd9Sstevel@tonic-gate /*
2394c894bbSVikram Hegde * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
27*3fe80ca4SDan Cross * Copyright 2023 Oxide Computer Company
28*3fe80ca4SDan Cross */
29*3fe80ca4SDan Cross
30*3fe80ca4SDan Cross /*
317c478bd9Sstevel@tonic-gate * driver for accessing kernel devinfo tree.
327c478bd9Sstevel@tonic-gate */
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
357c478bd9Sstevel@tonic-gate #include <sys/debug.h>
367c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
37b9ccdc5aScth #include <sys/vmsystm.h>
387c478bd9Sstevel@tonic-gate #include <sys/conf.h>
397c478bd9Sstevel@tonic-gate #include <sys/file.h>
407c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
417c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
447c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
457c478bd9Sstevel@tonic-gate #include <sys/sunldi_impl.h>
467c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
477c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
487c478bd9Sstevel@tonic-gate #include <sys/sunmdi.h>
497c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
507c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
517c478bd9Sstevel@tonic-gate #include <sys/mdi_impldefs.h>
527c478bd9Sstevel@tonic-gate #include <sys/devinfo_impl.h>
537c478bd9Sstevel@tonic-gate #include <sys/thread.h>
547c478bd9Sstevel@tonic-gate #include <sys/modhash.h>
557c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
567c478bd9Sstevel@tonic-gate #include <util/qsort.h>
577c478bd9Sstevel@tonic-gate #include <sys/disp.h>
587c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
597c478bd9Sstevel@tonic-gate #include <sys/crc32.h>
6026947304SEvan Yan #include <sys/ddi_hp.h>
6126947304SEvan Yan #include <sys/ddi_hp_impl.h>
6226947304SEvan Yan #include <sys/sysmacros.h>
6326947304SEvan Yan #include <sys/list.h>
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate #ifdef DEBUG
677c478bd9Sstevel@tonic-gate static int di_debug;
687c478bd9Sstevel@tonic-gate #define dcmn_err(args) if (di_debug >= 1) cmn_err args
697c478bd9Sstevel@tonic-gate #define dcmn_err2(args) if (di_debug >= 2) cmn_err args
707c478bd9Sstevel@tonic-gate #define dcmn_err3(args) if (di_debug >= 3) cmn_err args
717c478bd9Sstevel@tonic-gate #else
727c478bd9Sstevel@tonic-gate #define dcmn_err(args) /* nothing */
737c478bd9Sstevel@tonic-gate #define dcmn_err2(args) /* nothing */
747c478bd9Sstevel@tonic-gate #define dcmn_err3(args) /* nothing */
757c478bd9Sstevel@tonic-gate #endif
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate * We partition the space of devinfo minor nodes equally between the full and
797c478bd9Sstevel@tonic-gate * unprivileged versions of the driver. The even-numbered minor nodes are the
807c478bd9Sstevel@tonic-gate * full version, while the odd-numbered ones are the read-only version.
817c478bd9Sstevel@tonic-gate */
827c478bd9Sstevel@tonic-gate static int di_max_opens = 32;
837c478bd9Sstevel@tonic-gate
84b9ccdc5aScth static int di_prop_dyn = 1; /* enable dynamic property support */
85b9ccdc5aScth
867c478bd9Sstevel@tonic-gate #define DI_FULL_PARENT 0
877c478bd9Sstevel@tonic-gate #define DI_READONLY_PARENT 1
887c478bd9Sstevel@tonic-gate #define DI_NODE_SPECIES 2
897c478bd9Sstevel@tonic-gate #define DI_UNPRIVILEGED_NODE(x) (((x) % 2) != 0)
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate #define IOC_IDLE 0 /* snapshot ioctl states */
927c478bd9Sstevel@tonic-gate #define IOC_SNAP 1 /* snapshot in progress */
937c478bd9Sstevel@tonic-gate #define IOC_DONE 2 /* snapshot done, but not copied out */
947c478bd9Sstevel@tonic-gate #define IOC_COPY 3 /* copyout in progress */
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate /*
97b9ccdc5aScth * Keep max alignment so we can move snapshot to different platforms.
98b9ccdc5aScth *
99b9ccdc5aScth * NOTE: Most callers should rely on the di_checkmem return value
100b9ccdc5aScth * being aligned, and reestablish *off_p with aligned value, instead
101b9ccdc5aScth * of trying to align size of their allocations: this approach will
102b9ccdc5aScth * minimize memory use.
1037c478bd9Sstevel@tonic-gate */
1047c478bd9Sstevel@tonic-gate #define DI_ALIGN(addr) ((addr + 7l) & ~7l)
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate * To avoid wasting memory, make a linked list of memory chunks.
1087c478bd9Sstevel@tonic-gate * Size of each chunk is buf_size.
1097c478bd9Sstevel@tonic-gate */
1107c478bd9Sstevel@tonic-gate struct di_mem {
1117c478bd9Sstevel@tonic-gate struct di_mem *next; /* link to next chunk */
1127c478bd9Sstevel@tonic-gate char *buf; /* contiguous kernel memory */
1137c478bd9Sstevel@tonic-gate size_t buf_size; /* size of buf in bytes */
1147c478bd9Sstevel@tonic-gate devmap_cookie_t cook; /* cookie from ddi_umem_alloc */
1157c478bd9Sstevel@tonic-gate };
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate * This is a stack for walking the tree without using recursion.
1197c478bd9Sstevel@tonic-gate * When the devinfo tree height is above some small size, one
1207c478bd9Sstevel@tonic-gate * gets watchdog resets on sun4m.
1217c478bd9Sstevel@tonic-gate */
1227c478bd9Sstevel@tonic-gate struct di_stack {
1237c478bd9Sstevel@tonic-gate void *offset[MAX_TREE_DEPTH];
1247c478bd9Sstevel@tonic-gate struct dev_info *dip[MAX_TREE_DEPTH];
1257c478bd9Sstevel@tonic-gate int depth; /* depth of current node to be copied */
1267c478bd9Sstevel@tonic-gate };
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate #define TOP_OFFSET(stack) \
1297c478bd9Sstevel@tonic-gate ((di_off_t *)(stack)->offset[(stack)->depth - 1])
1307c478bd9Sstevel@tonic-gate #define TOP_NODE(stack) \
1317c478bd9Sstevel@tonic-gate ((stack)->dip[(stack)->depth - 1])
1327c478bd9Sstevel@tonic-gate #define PARENT_OFFSET(stack) \
1337c478bd9Sstevel@tonic-gate ((di_off_t *)(stack)->offset[(stack)->depth - 2])
1347c478bd9Sstevel@tonic-gate #define EMPTY_STACK(stack) ((stack)->depth == 0)
1357c478bd9Sstevel@tonic-gate #define POP_STACK(stack) { \
136*3fe80ca4SDan Cross ndi_devi_exit((dev_info_t *)TOP_NODE(stack)); \
1377c478bd9Sstevel@tonic-gate ((stack)->depth--); \
1387c478bd9Sstevel@tonic-gate }
139b9ccdc5aScth #define PUSH_STACK(stack, node, off_p) { \
1407c478bd9Sstevel@tonic-gate ASSERT(node != NULL); \
141*3fe80ca4SDan Cross ndi_devi_enter((dev_info_t *)node); \
1427c478bd9Sstevel@tonic-gate (stack)->dip[(stack)->depth] = (node); \
143b9ccdc5aScth (stack)->offset[(stack)->depth] = (void *)(off_p); \
1447c478bd9Sstevel@tonic-gate ((stack)->depth)++; \
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate
147b9ccdc5aScth #define DI_ALL_PTR(s) DI_ALL(di_mem_addr((s), 0))
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate /*
1507c478bd9Sstevel@tonic-gate * With devfs, the device tree has no global locks. The device tree is
1517c478bd9Sstevel@tonic-gate * dynamic and dips may come and go if they are not locked locally. Under
1527c478bd9Sstevel@tonic-gate * these conditions, pointers are no longer reliable as unique IDs.
1537c478bd9Sstevel@tonic-gate * Specifically, these pointers cannot be used as keys for hash tables
1547c478bd9Sstevel@tonic-gate * as the same devinfo structure may be freed in one part of the tree only
1557c478bd9Sstevel@tonic-gate * to be allocated as the structure for a different device in another
1567c478bd9Sstevel@tonic-gate * part of the tree. This can happen if DR and the snapshot are
1577c478bd9Sstevel@tonic-gate * happening concurrently.
1587c478bd9Sstevel@tonic-gate * The following data structures act as keys for devinfo nodes and
1597c478bd9Sstevel@tonic-gate * pathinfo nodes.
1607c478bd9Sstevel@tonic-gate */
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate enum di_ktype {
1637c478bd9Sstevel@tonic-gate DI_DKEY = 1,
1647c478bd9Sstevel@tonic-gate DI_PKEY = 2
1657c478bd9Sstevel@tonic-gate };
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate struct di_dkey {
1687c478bd9Sstevel@tonic-gate dev_info_t *dk_dip;
1697c478bd9Sstevel@tonic-gate major_t dk_major;
1707c478bd9Sstevel@tonic-gate int dk_inst;
171fa9e4066Sahrens pnode_t dk_nodeid;
1727c478bd9Sstevel@tonic-gate };
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate struct di_pkey {
1757c478bd9Sstevel@tonic-gate mdi_pathinfo_t *pk_pip;
1767c478bd9Sstevel@tonic-gate char *pk_path_addr;
1777c478bd9Sstevel@tonic-gate dev_info_t *pk_client;
1787c478bd9Sstevel@tonic-gate dev_info_t *pk_phci;
1797c478bd9Sstevel@tonic-gate };
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate struct di_key {
1827c478bd9Sstevel@tonic-gate enum di_ktype k_type;
1837c478bd9Sstevel@tonic-gate union {
1847c478bd9Sstevel@tonic-gate struct di_dkey dkey;
1857c478bd9Sstevel@tonic-gate struct di_pkey pkey;
1867c478bd9Sstevel@tonic-gate } k_u;
1877c478bd9Sstevel@tonic-gate };
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate struct i_lnode;
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate typedef struct i_link {
1937c478bd9Sstevel@tonic-gate /*
1947c478bd9Sstevel@tonic-gate * If a di_link struct representing this i_link struct makes it
1957c478bd9Sstevel@tonic-gate * into the snapshot, then self will point to the offset of
1967c478bd9Sstevel@tonic-gate * the di_link struct in the snapshot
1977c478bd9Sstevel@tonic-gate */
1987c478bd9Sstevel@tonic-gate di_off_t self;
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate int spec_type; /* block or char access type */
2017c478bd9Sstevel@tonic-gate struct i_lnode *src_lnode; /* src i_lnode */
2027c478bd9Sstevel@tonic-gate struct i_lnode *tgt_lnode; /* tgt i_lnode */
2037c478bd9Sstevel@tonic-gate struct i_link *src_link_next; /* next src i_link /w same i_lnode */
2047c478bd9Sstevel@tonic-gate struct i_link *tgt_link_next; /* next tgt i_link /w same i_lnode */
2057c478bd9Sstevel@tonic-gate } i_link_t;
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate typedef struct i_lnode {
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate * If a di_lnode struct representing this i_lnode struct makes it
2107c478bd9Sstevel@tonic-gate * into the snapshot, then self will point to the offset of
2117c478bd9Sstevel@tonic-gate * the di_lnode struct in the snapshot
2127c478bd9Sstevel@tonic-gate */
2137c478bd9Sstevel@tonic-gate di_off_t self;
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate * used for hashing and comparing i_lnodes
2177c478bd9Sstevel@tonic-gate */
2187c478bd9Sstevel@tonic-gate int modid;
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate * public information describing a link endpoint
2227c478bd9Sstevel@tonic-gate */
2237c478bd9Sstevel@tonic-gate struct di_node *di_node; /* di_node in snapshot */
2247c478bd9Sstevel@tonic-gate dev_t devt; /* devt */
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate * i_link ptr to links coming into this i_lnode node
2287c478bd9Sstevel@tonic-gate * (this i_lnode is the target of these i_links)
2297c478bd9Sstevel@tonic-gate */
2307c478bd9Sstevel@tonic-gate i_link_t *link_in;
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate /*
2337c478bd9Sstevel@tonic-gate * i_link ptr to links going out of this i_lnode node
2347c478bd9Sstevel@tonic-gate * (this i_lnode is the source of these i_links)
2357c478bd9Sstevel@tonic-gate */
2367c478bd9Sstevel@tonic-gate i_link_t *link_out;
2377c478bd9Sstevel@tonic-gate } i_lnode_t;
2387c478bd9Sstevel@tonic-gate
23926947304SEvan Yan typedef struct i_hp {
24026947304SEvan Yan di_off_t hp_off; /* Offset of di_hp_t in snapshot */
24126947304SEvan Yan dev_info_t *hp_child; /* Child devinfo node of the di_hp_t */
24226947304SEvan Yan list_node_t hp_link; /* List linkage */
24326947304SEvan Yan } i_hp_t;
24426947304SEvan Yan
2457c478bd9Sstevel@tonic-gate /*
2467c478bd9Sstevel@tonic-gate * Soft state associated with each instance of driver open.
2477c478bd9Sstevel@tonic-gate */
2487c478bd9Sstevel@tonic-gate static struct di_state {
2497c478bd9Sstevel@tonic-gate di_off_t mem_size; /* total # bytes in memlist */
2507c478bd9Sstevel@tonic-gate struct di_mem *memlist; /* head of memlist */
2517c478bd9Sstevel@tonic-gate uint_t command; /* command from ioctl */
2527c478bd9Sstevel@tonic-gate int di_iocstate; /* snapshot ioctl state */
2537c478bd9Sstevel@tonic-gate mod_hash_t *reg_dip_hash;
2547c478bd9Sstevel@tonic-gate mod_hash_t *reg_pip_hash;
2557c478bd9Sstevel@tonic-gate int lnode_count;
2567c478bd9Sstevel@tonic-gate int link_count;
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate mod_hash_t *lnode_hash;
2597c478bd9Sstevel@tonic-gate mod_hash_t *link_hash;
26026947304SEvan Yan
26126947304SEvan Yan list_t hp_list;
2627c478bd9Sstevel@tonic-gate } **di_states;
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate static kmutex_t di_lock; /* serialize instance assignment */
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate typedef enum {
2677c478bd9Sstevel@tonic-gate DI_QUIET = 0, /* DI_QUIET must always be 0 */
2687c478bd9Sstevel@tonic-gate DI_ERR,
2697c478bd9Sstevel@tonic-gate DI_INFO,
2707c478bd9Sstevel@tonic-gate DI_TRACE,
2717c478bd9Sstevel@tonic-gate DI_TRACE1,
2727c478bd9Sstevel@tonic-gate DI_TRACE2
2737c478bd9Sstevel@tonic-gate } di_cache_debug_t;
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate static uint_t di_chunk = 32; /* I/O chunk size in pages */
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate #define DI_CACHE_LOCK(c) (mutex_enter(&(c).cache_lock))
2787c478bd9Sstevel@tonic-gate #define DI_CACHE_UNLOCK(c) (mutex_exit(&(c).cache_lock))
2797c478bd9Sstevel@tonic-gate #define DI_CACHE_LOCKED(c) (mutex_owned(&(c).cache_lock))
2807c478bd9Sstevel@tonic-gate
2813c34adc5Sramat /*
2823c34adc5Sramat * Check that whole device tree is being configured as a pre-condition for
2833c34adc5Sramat * cleaning up /etc/devices files.
2843c34adc5Sramat */
2853c34adc5Sramat #define DEVICES_FILES_CLEANABLE(st) \
2863c34adc5Sramat (((st)->command & DINFOSUBTREE) && ((st)->command & DINFOFORCE) && \
2873c34adc5Sramat strcmp(DI_ALL_PTR(st)->root_path, "/") == 0)
2883c34adc5Sramat
2897c478bd9Sstevel@tonic-gate #define CACHE_DEBUG(args) \
2907c478bd9Sstevel@tonic-gate { if (di_cache_debug != DI_QUIET) di_cache_print args; }
2917c478bd9Sstevel@tonic-gate
292bc1009abSjg typedef struct phci_walk_arg {
2938c4f8890Srs135747 di_off_t off;
2948c4f8890Srs135747 struct di_state *st;
2958c4f8890Srs135747 } phci_walk_arg_t;
2968c4f8890Srs135747
2977c478bd9Sstevel@tonic-gate static int di_open(dev_t *, int, int, cred_t *);
2987c478bd9Sstevel@tonic-gate static int di_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
2997c478bd9Sstevel@tonic-gate static int di_close(dev_t, int, int, cred_t *);
3007c478bd9Sstevel@tonic-gate static int di_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
3017c478bd9Sstevel@tonic-gate static int di_attach(dev_info_t *, ddi_attach_cmd_t);
3027c478bd9Sstevel@tonic-gate static int di_detach(dev_info_t *, ddi_detach_cmd_t);
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate static di_off_t di_copyformat(di_off_t, struct di_state *, intptr_t, int);
3053c34adc5Sramat static di_off_t di_snapshot_and_clean(struct di_state *);
3067c478bd9Sstevel@tonic-gate static di_off_t di_copydevnm(di_off_t *, struct di_state *);
3077c478bd9Sstevel@tonic-gate static di_off_t di_copytree(struct dev_info *, di_off_t *, struct di_state *);
308b9ccdc5aScth static di_off_t di_copynode(struct dev_info *, struct di_stack *,
309b9ccdc5aScth struct di_state *);
3107c478bd9Sstevel@tonic-gate static di_off_t di_getmdata(struct ddi_minor_data *, di_off_t *, di_off_t,
3117c478bd9Sstevel@tonic-gate struct di_state *);
3127c478bd9Sstevel@tonic-gate static di_off_t di_getppdata(struct dev_info *, di_off_t *, struct di_state *);
3137c478bd9Sstevel@tonic-gate static di_off_t di_getdpdata(struct dev_info *, di_off_t *, struct di_state *);
31426947304SEvan Yan static di_off_t di_gethpdata(ddi_hp_cn_handle_t *, di_off_t *,
31526947304SEvan Yan struct di_state *);
316b9ccdc5aScth static di_off_t di_getprop(int, struct ddi_prop **, di_off_t *,
317b9ccdc5aScth struct di_state *, struct dev_info *);
3187c478bd9Sstevel@tonic-gate static void di_allocmem(struct di_state *, size_t);
3197c478bd9Sstevel@tonic-gate static void di_freemem(struct di_state *);
3207c478bd9Sstevel@tonic-gate static void di_copymem(struct di_state *st, caddr_t buf, size_t bufsiz);
3217c478bd9Sstevel@tonic-gate static di_off_t di_checkmem(struct di_state *, di_off_t, size_t);
322b9ccdc5aScth static void *di_mem_addr(struct di_state *, di_off_t);
3237c478bd9Sstevel@tonic-gate static int di_setstate(struct di_state *, int);
3247c478bd9Sstevel@tonic-gate static void di_register_dip(struct di_state *, dev_info_t *, di_off_t);
3257c478bd9Sstevel@tonic-gate static void di_register_pip(struct di_state *, mdi_pathinfo_t *, di_off_t);
3267c478bd9Sstevel@tonic-gate static di_off_t di_getpath_data(dev_info_t *, di_off_t *, di_off_t,
3277c478bd9Sstevel@tonic-gate struct di_state *, int);
3287c478bd9Sstevel@tonic-gate static di_off_t di_getlink_data(di_off_t, struct di_state *);
3297c478bd9Sstevel@tonic-gate static int di_dip_find(struct di_state *st, dev_info_t *node, di_off_t *off_p);
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate static int cache_args_valid(struct di_state *st, int *error);
3327c478bd9Sstevel@tonic-gate static int snapshot_is_cacheable(struct di_state *st);
3337c478bd9Sstevel@tonic-gate static int di_cache_lookup(struct di_state *st);
3347c478bd9Sstevel@tonic-gate static int di_cache_update(struct di_state *st);
3357c478bd9Sstevel@tonic-gate static void di_cache_print(di_cache_debug_t msglevel, char *fmt, ...);
336b9ccdc5aScth static int build_vhci_list(dev_info_t *vh_devinfo, void *arg);
337b9ccdc5aScth static int build_phci_list(dev_info_t *ph_devinfo, void *arg);
33826947304SEvan Yan static void di_hotplug_children(struct di_state *st);
339b9ccdc5aScth
340b9ccdc5aScth extern int modrootloaded;
341b9ccdc5aScth extern void mdi_walk_vhcis(int (*)(dev_info_t *, void *), void *);
342b9ccdc5aScth extern void mdi_vhci_walk_phcis(dev_info_t *,
343b9ccdc5aScth int (*)(dev_info_t *, void *), void *);
344b9ccdc5aScth
3457c478bd9Sstevel@tonic-gate
3467c478bd9Sstevel@tonic-gate static struct cb_ops di_cb_ops = {
3477c478bd9Sstevel@tonic-gate di_open, /* open */
3487c478bd9Sstevel@tonic-gate di_close, /* close */
3497c478bd9Sstevel@tonic-gate nodev, /* strategy */
3507c478bd9Sstevel@tonic-gate nodev, /* print */
3517c478bd9Sstevel@tonic-gate nodev, /* dump */
3527c478bd9Sstevel@tonic-gate nodev, /* read */
3537c478bd9Sstevel@tonic-gate nodev, /* write */
3547c478bd9Sstevel@tonic-gate di_ioctl, /* ioctl */
3557c478bd9Sstevel@tonic-gate nodev, /* devmap */
3567c478bd9Sstevel@tonic-gate nodev, /* mmap */
3577c478bd9Sstevel@tonic-gate nodev, /* segmap */
3587c478bd9Sstevel@tonic-gate nochpoll, /* poll */
3597c478bd9Sstevel@tonic-gate ddi_prop_op, /* prop_op */
3607c478bd9Sstevel@tonic-gate NULL, /* streamtab */
3617c478bd9Sstevel@tonic-gate D_NEW | D_MP /* Driver compatibility flag */
3627c478bd9Sstevel@tonic-gate };
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate static struct dev_ops di_ops = {
3657c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev, */
3667c478bd9Sstevel@tonic-gate 0, /* refcnt */
3677c478bd9Sstevel@tonic-gate di_info, /* info */
3687c478bd9Sstevel@tonic-gate nulldev, /* identify */
3697c478bd9Sstevel@tonic-gate nulldev, /* probe */
3707c478bd9Sstevel@tonic-gate di_attach, /* attach */
3717c478bd9Sstevel@tonic-gate di_detach, /* detach */
3727c478bd9Sstevel@tonic-gate nodev, /* reset */
3737c478bd9Sstevel@tonic-gate &di_cb_ops, /* driver operations */
3747c478bd9Sstevel@tonic-gate NULL /* bus operations */
3757c478bd9Sstevel@tonic-gate };
3767c478bd9Sstevel@tonic-gate
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate * Module linkage information for the kernel.
3797c478bd9Sstevel@tonic-gate */
3807c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
3817c478bd9Sstevel@tonic-gate &mod_driverops,
382f9722deaSChris Horne "DEVINFO Driver",
3837c478bd9Sstevel@tonic-gate &di_ops
3847c478bd9Sstevel@tonic-gate };
3857c478bd9Sstevel@tonic-gate
3867c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
3877c478bd9Sstevel@tonic-gate MODREV_1,
3887c478bd9Sstevel@tonic-gate &modldrv,
3897c478bd9Sstevel@tonic-gate NULL
3907c478bd9Sstevel@tonic-gate };
3917c478bd9Sstevel@tonic-gate
3927c478bd9Sstevel@tonic-gate int
_init(void)3937c478bd9Sstevel@tonic-gate _init(void)
3947c478bd9Sstevel@tonic-gate {
3957c478bd9Sstevel@tonic-gate int error;
3967c478bd9Sstevel@tonic-gate
3977c478bd9Sstevel@tonic-gate mutex_init(&di_lock, NULL, MUTEX_DRIVER, NULL);
3987c478bd9Sstevel@tonic-gate
3997c478bd9Sstevel@tonic-gate error = mod_install(&modlinkage);
4007c478bd9Sstevel@tonic-gate if (error != 0) {
4017c478bd9Sstevel@tonic-gate mutex_destroy(&di_lock);
4027c478bd9Sstevel@tonic-gate return (error);
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate
4057c478bd9Sstevel@tonic-gate return (0);
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate
4087c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)4097c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
4107c478bd9Sstevel@tonic-gate {
4117c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate
4147c478bd9Sstevel@tonic-gate int
_fini(void)4157c478bd9Sstevel@tonic-gate _fini(void)
4167c478bd9Sstevel@tonic-gate {
4177c478bd9Sstevel@tonic-gate int error;
4187c478bd9Sstevel@tonic-gate
4197c478bd9Sstevel@tonic-gate error = mod_remove(&modlinkage);
4207c478bd9Sstevel@tonic-gate if (error != 0) {
4217c478bd9Sstevel@tonic-gate return (error);
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate
4247c478bd9Sstevel@tonic-gate mutex_destroy(&di_lock);
4257c478bd9Sstevel@tonic-gate return (0);
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate
4287c478bd9Sstevel@tonic-gate static dev_info_t *di_dip;
4297c478bd9Sstevel@tonic-gate
4307c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4317c478bd9Sstevel@tonic-gate static int
di_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)4327c478bd9Sstevel@tonic-gate di_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate int error = DDI_FAILURE;
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate switch (infocmd) {
4377c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
4387c478bd9Sstevel@tonic-gate *result = (void *)di_dip;
4397c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
4407c478bd9Sstevel@tonic-gate break;
4417c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
4427c478bd9Sstevel@tonic-gate /*
4437c478bd9Sstevel@tonic-gate * All dev_t's map to the same, single instance.
4447c478bd9Sstevel@tonic-gate */
4457c478bd9Sstevel@tonic-gate *result = (void *)0;
4467c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
4477c478bd9Sstevel@tonic-gate break;
4487c478bd9Sstevel@tonic-gate default:
4497c478bd9Sstevel@tonic-gate break;
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate
4527c478bd9Sstevel@tonic-gate return (error);
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate
4557c478bd9Sstevel@tonic-gate static int
di_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)4567c478bd9Sstevel@tonic-gate di_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4577c478bd9Sstevel@tonic-gate {
4587c478bd9Sstevel@tonic-gate int error = DDI_FAILURE;
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate switch (cmd) {
4617c478bd9Sstevel@tonic-gate case DDI_ATTACH:
4627c478bd9Sstevel@tonic-gate di_states = kmem_zalloc(
4637c478bd9Sstevel@tonic-gate di_max_opens * sizeof (struct di_state *), KM_SLEEP);
4647c478bd9Sstevel@tonic-gate
4657c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(dip, "devinfo", S_IFCHR,
466093aa5c8SToomas Soome DI_FULL_PARENT, DDI_PSEUDO, 0) == DDI_FAILURE ||
4677c478bd9Sstevel@tonic-gate ddi_create_minor_node(dip, "devinfo,ro", S_IFCHR,
468093aa5c8SToomas Soome DI_READONLY_PARENT, DDI_PSEUDO, 0) == DDI_FAILURE) {
4697c478bd9Sstevel@tonic-gate kmem_free(di_states,
4707c478bd9Sstevel@tonic-gate di_max_opens * sizeof (struct di_state *));
4717c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
4727c478bd9Sstevel@tonic-gate error = DDI_FAILURE;
4737c478bd9Sstevel@tonic-gate } else {
4747c478bd9Sstevel@tonic-gate di_dip = dip;
4757c478bd9Sstevel@tonic-gate ddi_report_dev(dip);
4767c478bd9Sstevel@tonic-gate
4777c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate break;
4807c478bd9Sstevel@tonic-gate default:
4817c478bd9Sstevel@tonic-gate error = DDI_FAILURE;
4827c478bd9Sstevel@tonic-gate break;
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate
4857c478bd9Sstevel@tonic-gate return (error);
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate
4887c478bd9Sstevel@tonic-gate static int
di_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)4897c478bd9Sstevel@tonic-gate di_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate int error = DDI_FAILURE;
4927c478bd9Sstevel@tonic-gate
4937c478bd9Sstevel@tonic-gate switch (cmd) {
4947c478bd9Sstevel@tonic-gate case DDI_DETACH:
4957c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
4967c478bd9Sstevel@tonic-gate di_dip = NULL;
4977c478bd9Sstevel@tonic-gate kmem_free(di_states, di_max_opens * sizeof (struct di_state *));
4987c478bd9Sstevel@tonic-gate
4997c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
5007c478bd9Sstevel@tonic-gate break;
5017c478bd9Sstevel@tonic-gate default:
5027c478bd9Sstevel@tonic-gate error = DDI_FAILURE;
5037c478bd9Sstevel@tonic-gate break;
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate
5067c478bd9Sstevel@tonic-gate return (error);
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate
5097c478bd9Sstevel@tonic-gate /*
5107c478bd9Sstevel@tonic-gate * Allow multiple opens by tweaking the dev_t such that it looks like each
5117c478bd9Sstevel@tonic-gate * open is getting a different minor device. Each minor gets a separate
5127c478bd9Sstevel@tonic-gate * entry in the di_states[] table. Based on the original minor number, we
5137c478bd9Sstevel@tonic-gate * discriminate opens of the full and read-only nodes. If all of the instances
5147c478bd9Sstevel@tonic-gate * of the selected minor node are currently open, we return EAGAIN.
5157c478bd9Sstevel@tonic-gate */
5167c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5177c478bd9Sstevel@tonic-gate static int
di_open(dev_t * devp,int flag,int otyp,cred_t * credp)5187c478bd9Sstevel@tonic-gate di_open(dev_t *devp, int flag, int otyp, cred_t *credp)
5197c478bd9Sstevel@tonic-gate {
5207c478bd9Sstevel@tonic-gate int m;
5217c478bd9Sstevel@tonic-gate minor_t minor_parent = getminor(*devp);
5227c478bd9Sstevel@tonic-gate
5237c478bd9Sstevel@tonic-gate if (minor_parent != DI_FULL_PARENT &&
5247c478bd9Sstevel@tonic-gate minor_parent != DI_READONLY_PARENT)
5257c478bd9Sstevel@tonic-gate return (ENXIO);
5267c478bd9Sstevel@tonic-gate
5277c478bd9Sstevel@tonic-gate mutex_enter(&di_lock);
5287c478bd9Sstevel@tonic-gate
5297c478bd9Sstevel@tonic-gate for (m = minor_parent; m < di_max_opens; m += DI_NODE_SPECIES) {
5307c478bd9Sstevel@tonic-gate if (di_states[m] != NULL)
5317c478bd9Sstevel@tonic-gate continue;
5327c478bd9Sstevel@tonic-gate
5337c478bd9Sstevel@tonic-gate di_states[m] = kmem_zalloc(sizeof (struct di_state), KM_SLEEP);
5347c478bd9Sstevel@tonic-gate break; /* It's ours. */
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate
5377c478bd9Sstevel@tonic-gate if (m >= di_max_opens) {
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate * maximum open instance for device reached
5407c478bd9Sstevel@tonic-gate */
5417c478bd9Sstevel@tonic-gate mutex_exit(&di_lock);
5427c478bd9Sstevel@tonic-gate dcmn_err((CE_WARN, "devinfo: maximum devinfo open reached"));
5437c478bd9Sstevel@tonic-gate return (EAGAIN);
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate mutex_exit(&di_lock);
5467c478bd9Sstevel@tonic-gate
5477c478bd9Sstevel@tonic-gate ASSERT(m < di_max_opens);
5487c478bd9Sstevel@tonic-gate *devp = makedevice(getmajor(*devp), (minor_t)(m + DI_NODE_SPECIES));
5497c478bd9Sstevel@tonic-gate
5507c478bd9Sstevel@tonic-gate dcmn_err((CE_CONT, "di_open: thread = %p, assigned minor = %d\n",
5517c478bd9Sstevel@tonic-gate (void *)curthread, m + DI_NODE_SPECIES));
5527c478bd9Sstevel@tonic-gate
5537c478bd9Sstevel@tonic-gate return (0);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate
5567c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5577c478bd9Sstevel@tonic-gate static int
di_close(dev_t dev,int flag,int otype,cred_t * cred_p)5587c478bd9Sstevel@tonic-gate di_close(dev_t dev, int flag, int otype, cred_t *cred_p)
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate struct di_state *st;
5617c478bd9Sstevel@tonic-gate int m = (int)getminor(dev) - DI_NODE_SPECIES;
5627c478bd9Sstevel@tonic-gate
5637c478bd9Sstevel@tonic-gate if (m < 0) {
5647c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "closing non-existent devinfo minor %d",
5657c478bd9Sstevel@tonic-gate m + DI_NODE_SPECIES);
5667c478bd9Sstevel@tonic-gate return (ENXIO);
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate
5697c478bd9Sstevel@tonic-gate st = di_states[m];
5707c478bd9Sstevel@tonic-gate ASSERT(m < di_max_opens && st != NULL);
5717c478bd9Sstevel@tonic-gate
5727c478bd9Sstevel@tonic-gate di_freemem(st);
5737c478bd9Sstevel@tonic-gate kmem_free(st, sizeof (struct di_state));
5747c478bd9Sstevel@tonic-gate
5757c478bd9Sstevel@tonic-gate /*
5767c478bd9Sstevel@tonic-gate * empty slot in state table
5777c478bd9Sstevel@tonic-gate */
5787c478bd9Sstevel@tonic-gate mutex_enter(&di_lock);
5797c478bd9Sstevel@tonic-gate di_states[m] = NULL;
5807c478bd9Sstevel@tonic-gate dcmn_err((CE_CONT, "di_close: thread = %p, assigned minor = %d\n",
5817c478bd9Sstevel@tonic-gate (void *)curthread, m + DI_NODE_SPECIES));
5827c478bd9Sstevel@tonic-gate mutex_exit(&di_lock);
5837c478bd9Sstevel@tonic-gate
5847c478bd9Sstevel@tonic-gate return (0);
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate
5877c478bd9Sstevel@tonic-gate
5887c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5897c478bd9Sstevel@tonic-gate static int
di_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)5907c478bd9Sstevel@tonic-gate di_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
5917c478bd9Sstevel@tonic-gate {
5927c478bd9Sstevel@tonic-gate int rv, error;
5937c478bd9Sstevel@tonic-gate di_off_t off;
5947c478bd9Sstevel@tonic-gate struct di_all *all;
5957c478bd9Sstevel@tonic-gate struct di_state *st;
5967c478bd9Sstevel@tonic-gate int m = (int)getminor(dev) - DI_NODE_SPECIES;
5977c478bd9Sstevel@tonic-gate major_t i;
5987c478bd9Sstevel@tonic-gate char *drv_name;
5997c478bd9Sstevel@tonic-gate size_t map_size, size;
6007c478bd9Sstevel@tonic-gate struct di_mem *dcp;
6017c478bd9Sstevel@tonic-gate int ndi_flags;
6027c478bd9Sstevel@tonic-gate
6037c478bd9Sstevel@tonic-gate if (m < 0 || m >= di_max_opens) {
6047c478bd9Sstevel@tonic-gate return (ENXIO);
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate
6077c478bd9Sstevel@tonic-gate st = di_states[m];
6087c478bd9Sstevel@tonic-gate ASSERT(st != NULL);
6097c478bd9Sstevel@tonic-gate
6107c478bd9Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_ioctl: mode = %x, cmd = %x\n", mode, cmd));
6117c478bd9Sstevel@tonic-gate
6127c478bd9Sstevel@tonic-gate switch (cmd) {
6137c478bd9Sstevel@tonic-gate case DINFOIDENT:
6147c478bd9Sstevel@tonic-gate /*
6157c478bd9Sstevel@tonic-gate * This is called from di_init to verify that the driver
6167c478bd9Sstevel@tonic-gate * opened is indeed devinfo. The purpose is to guard against
6177c478bd9Sstevel@tonic-gate * sending ioctl to an unknown driver in case of an
6187c478bd9Sstevel@tonic-gate * unresolved major number conflict during bfu.
6197c478bd9Sstevel@tonic-gate */
6207c478bd9Sstevel@tonic-gate *rvalp = DI_MAGIC;
6217c478bd9Sstevel@tonic-gate return (0);
6227c478bd9Sstevel@tonic-gate
6237c478bd9Sstevel@tonic-gate case DINFOLODRV:
6247c478bd9Sstevel@tonic-gate /*
6257c478bd9Sstevel@tonic-gate * Hold an installed driver and return the result
6267c478bd9Sstevel@tonic-gate */
6277c478bd9Sstevel@tonic-gate if (DI_UNPRIVILEGED_NODE(m)) {
6287c478bd9Sstevel@tonic-gate /*
6297c478bd9Sstevel@tonic-gate * Only the fully enabled instances may issue
6307c478bd9Sstevel@tonic-gate * DINFOLDDRV.
6317c478bd9Sstevel@tonic-gate */
6327c478bd9Sstevel@tonic-gate return (EACCES);
6337c478bd9Sstevel@tonic-gate }
6347c478bd9Sstevel@tonic-gate
6357c478bd9Sstevel@tonic-gate drv_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
6367c478bd9Sstevel@tonic-gate if (ddi_copyin((void *)arg, drv_name, MAXNAMELEN, mode) != 0) {
6377c478bd9Sstevel@tonic-gate kmem_free(drv_name, MAXNAMELEN);
6387c478bd9Sstevel@tonic-gate return (EFAULT);
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate
6417c478bd9Sstevel@tonic-gate /*
6427c478bd9Sstevel@tonic-gate * Some 3rd party driver's _init() walks the device tree,
6437c478bd9Sstevel@tonic-gate * so we load the driver module before configuring driver.
6447c478bd9Sstevel@tonic-gate */
6457c478bd9Sstevel@tonic-gate i = ddi_name_to_major(drv_name);
6467c478bd9Sstevel@tonic-gate if (ddi_hold_driver(i) == NULL) {
6477c478bd9Sstevel@tonic-gate kmem_free(drv_name, MAXNAMELEN);
6487c478bd9Sstevel@tonic-gate return (ENXIO);
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate
6517c478bd9Sstevel@tonic-gate ndi_flags = NDI_DEVI_PERSIST | NDI_CONFIG | NDI_NO_EVENT;
6527c478bd9Sstevel@tonic-gate
6537c478bd9Sstevel@tonic-gate /*
6547c478bd9Sstevel@tonic-gate * i_ddi_load_drvconf() below will trigger a reprobe
6557c478bd9Sstevel@tonic-gate * via reset_nexus_flags(). NDI_DRV_CONF_REPROBE isn't
6567c478bd9Sstevel@tonic-gate * needed here.
6577c478bd9Sstevel@tonic-gate */
6587c478bd9Sstevel@tonic-gate modunload_disable();
6597c478bd9Sstevel@tonic-gate (void) i_ddi_load_drvconf(i);
6607c478bd9Sstevel@tonic-gate (void) ndi_devi_config_driver(ddi_root_node(), ndi_flags, i);
6617c478bd9Sstevel@tonic-gate kmem_free(drv_name, MAXNAMELEN);
6627c478bd9Sstevel@tonic-gate ddi_rele_driver(i);
6637c478bd9Sstevel@tonic-gate rv = i_ddi_devs_attached(i);
6647c478bd9Sstevel@tonic-gate modunload_enable();
6657c478bd9Sstevel@tonic-gate
6664c06356bSdh142964 i_ddi_di_cache_invalidate();
6677c478bd9Sstevel@tonic-gate
6687c478bd9Sstevel@tonic-gate return ((rv == DDI_SUCCESS)? 0 : ENXIO);
6697c478bd9Sstevel@tonic-gate
6707c478bd9Sstevel@tonic-gate case DINFOUSRLD:
6717c478bd9Sstevel@tonic-gate /*
6727c478bd9Sstevel@tonic-gate * The case for copying snapshot to userland
6737c478bd9Sstevel@tonic-gate */
6747c478bd9Sstevel@tonic-gate if (di_setstate(st, IOC_COPY) == -1)
6757c478bd9Sstevel@tonic-gate return (EBUSY);
6767c478bd9Sstevel@tonic-gate
677b9ccdc5aScth map_size = DI_ALL_PTR(st)->map_size;
6787c478bd9Sstevel@tonic-gate if (map_size == 0) {
6797c478bd9Sstevel@tonic-gate (void) di_setstate(st, IOC_DONE);
6807c478bd9Sstevel@tonic-gate return (EFAULT);
6817c478bd9Sstevel@tonic-gate }
6827c478bd9Sstevel@tonic-gate
6837c478bd9Sstevel@tonic-gate /*
6847c478bd9Sstevel@tonic-gate * copyout the snapshot
6857c478bd9Sstevel@tonic-gate */
6867c478bd9Sstevel@tonic-gate map_size = (map_size + PAGEOFFSET) & PAGEMASK;
6877c478bd9Sstevel@tonic-gate
6887c478bd9Sstevel@tonic-gate /*
6897c478bd9Sstevel@tonic-gate * Return the map size, so caller may do a sanity
6907c478bd9Sstevel@tonic-gate * check against the return value of snapshot ioctl()
6917c478bd9Sstevel@tonic-gate */
6927c478bd9Sstevel@tonic-gate *rvalp = (int)map_size;
6937c478bd9Sstevel@tonic-gate
6947c478bd9Sstevel@tonic-gate /*
6957c478bd9Sstevel@tonic-gate * Copy one chunk at a time
6967c478bd9Sstevel@tonic-gate */
6977c478bd9Sstevel@tonic-gate off = 0;
6987c478bd9Sstevel@tonic-gate dcp = st->memlist;
6997c478bd9Sstevel@tonic-gate while (map_size) {
7007c478bd9Sstevel@tonic-gate size = dcp->buf_size;
7017c478bd9Sstevel@tonic-gate if (map_size <= size) {
7027c478bd9Sstevel@tonic-gate size = map_size;
7037c478bd9Sstevel@tonic-gate }
7047c478bd9Sstevel@tonic-gate
7057c478bd9Sstevel@tonic-gate if (ddi_copyout(di_mem_addr(st, off),
7067c478bd9Sstevel@tonic-gate (void *)(arg + off), size, mode) != 0) {
7077c478bd9Sstevel@tonic-gate (void) di_setstate(st, IOC_DONE);
7087c478bd9Sstevel@tonic-gate return (EFAULT);
7097c478bd9Sstevel@tonic-gate }
7107c478bd9Sstevel@tonic-gate
7117c478bd9Sstevel@tonic-gate map_size -= size;
7127c478bd9Sstevel@tonic-gate off += size;
7137c478bd9Sstevel@tonic-gate dcp = dcp->next;
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate
7167c478bd9Sstevel@tonic-gate di_freemem(st);
7177c478bd9Sstevel@tonic-gate (void) di_setstate(st, IOC_IDLE);
7187c478bd9Sstevel@tonic-gate return (0);
7197c478bd9Sstevel@tonic-gate
7207c478bd9Sstevel@tonic-gate default:
7217c478bd9Sstevel@tonic-gate if ((cmd & ~DIIOC_MASK) != DIIOC) {
7227c478bd9Sstevel@tonic-gate /*
7237c478bd9Sstevel@tonic-gate * Invalid ioctl command
7247c478bd9Sstevel@tonic-gate */
7257c478bd9Sstevel@tonic-gate return (ENOTTY);
7267c478bd9Sstevel@tonic-gate }
7277c478bd9Sstevel@tonic-gate /*
7287c478bd9Sstevel@tonic-gate * take a snapshot
7297c478bd9Sstevel@tonic-gate */
7307c478bd9Sstevel@tonic-gate st->command = cmd & DIIOC_MASK;
7317c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate
7347c478bd9Sstevel@tonic-gate /*
7357c478bd9Sstevel@tonic-gate * Obtain enough memory to hold header + rootpath. We prevent kernel
7367c478bd9Sstevel@tonic-gate * memory exhaustion by freeing any previously allocated snapshot and
7377c478bd9Sstevel@tonic-gate * refusing the operation; otherwise we would be allowing ioctl(),
7387c478bd9Sstevel@tonic-gate * ioctl(), ioctl(), ..., panic.
7397c478bd9Sstevel@tonic-gate */
7407c478bd9Sstevel@tonic-gate if (di_setstate(st, IOC_SNAP) == -1)
7417c478bd9Sstevel@tonic-gate return (EBUSY);
7427c478bd9Sstevel@tonic-gate
743b9ccdc5aScth /*
744b9ccdc5aScth * Initial memlist always holds di_all and the root_path - and
745b9ccdc5aScth * is at least a page and size.
746b9ccdc5aScth */
7477c478bd9Sstevel@tonic-gate size = sizeof (struct di_all) +
7487c478bd9Sstevel@tonic-gate sizeof (((struct dinfo_io *)(NULL))->root_path);
7497c478bd9Sstevel@tonic-gate if (size < PAGESIZE)
7507c478bd9Sstevel@tonic-gate size = PAGESIZE;
751b9ccdc5aScth off = di_checkmem(st, 0, size);
752b9ccdc5aScth all = DI_ALL_PTR(st);
753b9ccdc5aScth off += sizeof (struct di_all); /* real length of di_all */
7547c478bd9Sstevel@tonic-gate
7557c478bd9Sstevel@tonic-gate all->devcnt = devcnt;
7567c478bd9Sstevel@tonic-gate all->command = st->command;
7577c478bd9Sstevel@tonic-gate all->version = DI_SNAPSHOT_VERSION;
758b9ccdc5aScth all->top_vhci_devinfo = 0; /* filled by build_vhci_list. */
7597c478bd9Sstevel@tonic-gate
7607c478bd9Sstevel@tonic-gate /*
7617c478bd9Sstevel@tonic-gate * Note the endianness in case we need to transport snapshot
7627c478bd9Sstevel@tonic-gate * over the network.
7637c478bd9Sstevel@tonic-gate */
7647c478bd9Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
7657c478bd9Sstevel@tonic-gate all->endianness = DI_LITTLE_ENDIAN;
7667c478bd9Sstevel@tonic-gate #else
7677c478bd9Sstevel@tonic-gate all->endianness = DI_BIG_ENDIAN;
7687c478bd9Sstevel@tonic-gate #endif
7697c478bd9Sstevel@tonic-gate
7707c478bd9Sstevel@tonic-gate /* Copyin ioctl args, store in the snapshot. */
77194c894bbSVikram Hegde if (copyinstr((void *)arg, all->req_path,
7727c478bd9Sstevel@tonic-gate sizeof (((struct dinfo_io *)(NULL))->root_path), &size) != 0) {
7737c478bd9Sstevel@tonic-gate di_freemem(st);
7747c478bd9Sstevel@tonic-gate (void) di_setstate(st, IOC_IDLE);
7757c478bd9Sstevel@tonic-gate return (EFAULT);
7767c478bd9Sstevel@tonic-gate }
77794c894bbSVikram Hegde (void) strcpy(all->root_path, all->req_path);
778b9ccdc5aScth off += size; /* real length of root_path */
7797c478bd9Sstevel@tonic-gate
7803c34adc5Sramat if ((st->command & DINFOCLEANUP) && !DEVICES_FILES_CLEANABLE(st)) {
7813c34adc5Sramat di_freemem(st);
7823c34adc5Sramat (void) di_setstate(st, IOC_IDLE);
7833c34adc5Sramat return (EINVAL);
7843c34adc5Sramat }
7853c34adc5Sramat
7867c478bd9Sstevel@tonic-gate error = 0;
7877c478bd9Sstevel@tonic-gate if ((st->command & DINFOCACHE) && !cache_args_valid(st, &error)) {
7887c478bd9Sstevel@tonic-gate di_freemem(st);
7897c478bd9Sstevel@tonic-gate (void) di_setstate(st, IOC_IDLE);
7907c478bd9Sstevel@tonic-gate return (error);
7917c478bd9Sstevel@tonic-gate }
7927c478bd9Sstevel@tonic-gate
7937c478bd9Sstevel@tonic-gate /*
7947c478bd9Sstevel@tonic-gate * Only the fully enabled version may force load drivers or read
7957c478bd9Sstevel@tonic-gate * the parent private data from a driver.
7967c478bd9Sstevel@tonic-gate */
7977c478bd9Sstevel@tonic-gate if ((st->command & (DINFOPRIVDATA | DINFOFORCE)) != 0 &&
7987c478bd9Sstevel@tonic-gate DI_UNPRIVILEGED_NODE(m)) {
7997c478bd9Sstevel@tonic-gate di_freemem(st);
8007c478bd9Sstevel@tonic-gate (void) di_setstate(st, IOC_IDLE);
8017c478bd9Sstevel@tonic-gate return (EACCES);
8027c478bd9Sstevel@tonic-gate }
8037c478bd9Sstevel@tonic-gate
8047c478bd9Sstevel@tonic-gate /* Do we need private data? */
8057c478bd9Sstevel@tonic-gate if (st->command & DINFOPRIVDATA) {
8067c478bd9Sstevel@tonic-gate arg += sizeof (((struct dinfo_io *)(NULL))->root_path);
8077c478bd9Sstevel@tonic-gate
8087c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
8097c478bd9Sstevel@tonic-gate switch (ddi_model_convert_from(mode & FMODELS)) {
8107c478bd9Sstevel@tonic-gate case DDI_MODEL_ILP32: {
8117c478bd9Sstevel@tonic-gate /*
8127c478bd9Sstevel@tonic-gate * Cannot copy private data from 64-bit kernel
8137c478bd9Sstevel@tonic-gate * to 32-bit app
8147c478bd9Sstevel@tonic-gate */
8157c478bd9Sstevel@tonic-gate di_freemem(st);
8167c478bd9Sstevel@tonic-gate (void) di_setstate(st, IOC_IDLE);
8177c478bd9Sstevel@tonic-gate return (EINVAL);
8187c478bd9Sstevel@tonic-gate }
8197c478bd9Sstevel@tonic-gate case DDI_MODEL_NONE:
8207c478bd9Sstevel@tonic-gate if ((off = di_copyformat(off, st, arg, mode)) == 0) {
8217c478bd9Sstevel@tonic-gate di_freemem(st);
8227c478bd9Sstevel@tonic-gate (void) di_setstate(st, IOC_IDLE);
8237c478bd9Sstevel@tonic-gate return (EFAULT);
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate break;
8267c478bd9Sstevel@tonic-gate }
8277c478bd9Sstevel@tonic-gate #else /* !_MULTI_DATAMODEL */
8287c478bd9Sstevel@tonic-gate if ((off = di_copyformat(off, st, arg, mode)) == 0) {
8297c478bd9Sstevel@tonic-gate di_freemem(st);
8307c478bd9Sstevel@tonic-gate (void) di_setstate(st, IOC_IDLE);
8317c478bd9Sstevel@tonic-gate return (EFAULT);
8327c478bd9Sstevel@tonic-gate }
8337c478bd9Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
8347c478bd9Sstevel@tonic-gate }
8357c478bd9Sstevel@tonic-gate
8367c478bd9Sstevel@tonic-gate all->top_devinfo = DI_ALIGN(off);
8377c478bd9Sstevel@tonic-gate
8387c478bd9Sstevel@tonic-gate /*
8397c478bd9Sstevel@tonic-gate * For cache lookups we reallocate memory from scratch,
8407c478bd9Sstevel@tonic-gate * so the value of "all" is no longer valid.
8417c478bd9Sstevel@tonic-gate */
8427c478bd9Sstevel@tonic-gate all = NULL;
8437c478bd9Sstevel@tonic-gate
8447c478bd9Sstevel@tonic-gate if (st->command & DINFOCACHE) {
8457c478bd9Sstevel@tonic-gate *rvalp = di_cache_lookup(st);
8467c478bd9Sstevel@tonic-gate } else if (snapshot_is_cacheable(st)) {
8477c478bd9Sstevel@tonic-gate DI_CACHE_LOCK(di_cache);
8487c478bd9Sstevel@tonic-gate *rvalp = di_cache_update(st);
8497c478bd9Sstevel@tonic-gate DI_CACHE_UNLOCK(di_cache);
8503c34adc5Sramat } else
8513c34adc5Sramat *rvalp = di_snapshot_and_clean(st);
8527c478bd9Sstevel@tonic-gate
8537c478bd9Sstevel@tonic-gate if (*rvalp) {
8547c478bd9Sstevel@tonic-gate DI_ALL_PTR(st)->map_size = *rvalp;
8557c478bd9Sstevel@tonic-gate (void) di_setstate(st, IOC_DONE);
8567c478bd9Sstevel@tonic-gate } else {
8577c478bd9Sstevel@tonic-gate di_freemem(st);
8587c478bd9Sstevel@tonic-gate (void) di_setstate(st, IOC_IDLE);
8597c478bd9Sstevel@tonic-gate }
8607c478bd9Sstevel@tonic-gate
8617c478bd9Sstevel@tonic-gate return (0);
8627c478bd9Sstevel@tonic-gate }
8637c478bd9Sstevel@tonic-gate
8647c478bd9Sstevel@tonic-gate /*
8657c478bd9Sstevel@tonic-gate * Get a chunk of memory >= size, for the snapshot
8667c478bd9Sstevel@tonic-gate */
8677c478bd9Sstevel@tonic-gate static void
di_allocmem(struct di_state * st,size_t size)8687c478bd9Sstevel@tonic-gate di_allocmem(struct di_state *st, size_t size)
8697c478bd9Sstevel@tonic-gate {
870b9ccdc5aScth struct di_mem *mem = kmem_zalloc(sizeof (struct di_mem), KM_SLEEP);
871b9ccdc5aScth
8727c478bd9Sstevel@tonic-gate /*
8737c478bd9Sstevel@tonic-gate * Round up size to nearest power of 2. If it is less
8747c478bd9Sstevel@tonic-gate * than st->mem_size, set it to st->mem_size (i.e.,
8757c478bd9Sstevel@tonic-gate * the mem_size is doubled every time) to reduce the
8767c478bd9Sstevel@tonic-gate * number of memory allocations.
8777c478bd9Sstevel@tonic-gate */
8787c478bd9Sstevel@tonic-gate size_t tmp = 1;
8797c478bd9Sstevel@tonic-gate while (tmp < size) {
8807c478bd9Sstevel@tonic-gate tmp <<= 1;
8817c478bd9Sstevel@tonic-gate }
8827c478bd9Sstevel@tonic-gate size = (tmp > st->mem_size) ? tmp : st->mem_size;
8837c478bd9Sstevel@tonic-gate
8847c478bd9Sstevel@tonic-gate mem->buf = ddi_umem_alloc(size, DDI_UMEM_SLEEP, &mem->cook);
8857c478bd9Sstevel@tonic-gate mem->buf_size = size;
8867c478bd9Sstevel@tonic-gate
8877c478bd9Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_allocmem: mem_size=%x\n", st->mem_size));
8887c478bd9Sstevel@tonic-gate
8897c478bd9Sstevel@tonic-gate if (st->mem_size == 0) { /* first chunk */
8907c478bd9Sstevel@tonic-gate st->memlist = mem;
8917c478bd9Sstevel@tonic-gate } else {
8927c478bd9Sstevel@tonic-gate /*
8937c478bd9Sstevel@tonic-gate * locate end of linked list and add a chunk at the end
8947c478bd9Sstevel@tonic-gate */
8957c478bd9Sstevel@tonic-gate struct di_mem *dcp = st->memlist;
8967c478bd9Sstevel@tonic-gate while (dcp->next != NULL) {
8977c478bd9Sstevel@tonic-gate dcp = dcp->next;
8987c478bd9Sstevel@tonic-gate }
8997c478bd9Sstevel@tonic-gate
9007c478bd9Sstevel@tonic-gate dcp->next = mem;
9017c478bd9Sstevel@tonic-gate }
9027c478bd9Sstevel@tonic-gate
9037c478bd9Sstevel@tonic-gate st->mem_size += size;
9047c478bd9Sstevel@tonic-gate }
9057c478bd9Sstevel@tonic-gate
9067c478bd9Sstevel@tonic-gate /*
9077c478bd9Sstevel@tonic-gate * Copy upto bufsiz bytes of the memlist to buf
9087c478bd9Sstevel@tonic-gate */
9097c478bd9Sstevel@tonic-gate static void
di_copymem(struct di_state * st,caddr_t buf,size_t bufsiz)9107c478bd9Sstevel@tonic-gate di_copymem(struct di_state *st, caddr_t buf, size_t bufsiz)
9117c478bd9Sstevel@tonic-gate {
9127c478bd9Sstevel@tonic-gate struct di_mem *dcp;
9137c478bd9Sstevel@tonic-gate size_t copysz;
9147c478bd9Sstevel@tonic-gate
9157c478bd9Sstevel@tonic-gate if (st->mem_size == 0) {
9167c478bd9Sstevel@tonic-gate ASSERT(st->memlist == NULL);
9177c478bd9Sstevel@tonic-gate return;
9187c478bd9Sstevel@tonic-gate }
9197c478bd9Sstevel@tonic-gate
9207c478bd9Sstevel@tonic-gate copysz = 0;
9217c478bd9Sstevel@tonic-gate for (dcp = st->memlist; dcp; dcp = dcp->next) {
9227c478bd9Sstevel@tonic-gate
9237c478bd9Sstevel@tonic-gate ASSERT(bufsiz > 0);
9247c478bd9Sstevel@tonic-gate
9257c478bd9Sstevel@tonic-gate if (bufsiz <= dcp->buf_size)
9267c478bd9Sstevel@tonic-gate copysz = bufsiz;
9277c478bd9Sstevel@tonic-gate else
9287c478bd9Sstevel@tonic-gate copysz = dcp->buf_size;
9297c478bd9Sstevel@tonic-gate
9307c478bd9Sstevel@tonic-gate bcopy(dcp->buf, buf, copysz);
9317c478bd9Sstevel@tonic-gate
9327c478bd9Sstevel@tonic-gate buf += copysz;
9337c478bd9Sstevel@tonic-gate bufsiz -= copysz;
9347c478bd9Sstevel@tonic-gate
9357c478bd9Sstevel@tonic-gate if (bufsiz == 0)
9367c478bd9Sstevel@tonic-gate break;
9377c478bd9Sstevel@tonic-gate }
9387c478bd9Sstevel@tonic-gate }
9397c478bd9Sstevel@tonic-gate
9407c478bd9Sstevel@tonic-gate /*
9417c478bd9Sstevel@tonic-gate * Free all memory for the snapshot
9427c478bd9Sstevel@tonic-gate */
9437c478bd9Sstevel@tonic-gate static void
di_freemem(struct di_state * st)9447c478bd9Sstevel@tonic-gate di_freemem(struct di_state *st)
9457c478bd9Sstevel@tonic-gate {
9467c478bd9Sstevel@tonic-gate struct di_mem *dcp, *tmp;
9477c478bd9Sstevel@tonic-gate
9487c478bd9Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_freemem\n"));
9497c478bd9Sstevel@tonic-gate
9507c478bd9Sstevel@tonic-gate if (st->mem_size) {
9517c478bd9Sstevel@tonic-gate dcp = st->memlist;
9527c478bd9Sstevel@tonic-gate while (dcp) { /* traverse the linked list */
9537c478bd9Sstevel@tonic-gate tmp = dcp;
9547c478bd9Sstevel@tonic-gate dcp = dcp->next;
9557c478bd9Sstevel@tonic-gate ddi_umem_free(tmp->cook);
9567c478bd9Sstevel@tonic-gate kmem_free(tmp, sizeof (struct di_mem));
9577c478bd9Sstevel@tonic-gate }
9587c478bd9Sstevel@tonic-gate st->mem_size = 0;
9597c478bd9Sstevel@tonic-gate st->memlist = NULL;
9607c478bd9Sstevel@tonic-gate }
9617c478bd9Sstevel@tonic-gate
9627c478bd9Sstevel@tonic-gate ASSERT(st->mem_size == 0);
9637c478bd9Sstevel@tonic-gate ASSERT(st->memlist == NULL);
9647c478bd9Sstevel@tonic-gate }
9657c478bd9Sstevel@tonic-gate
9667c478bd9Sstevel@tonic-gate /*
9677c478bd9Sstevel@tonic-gate * Copies cached data to the di_state structure.
9687c478bd9Sstevel@tonic-gate * Returns:
9697c478bd9Sstevel@tonic-gate * - size of data copied, on SUCCESS
9707c478bd9Sstevel@tonic-gate * - 0 on failure
9717c478bd9Sstevel@tonic-gate */
9727c478bd9Sstevel@tonic-gate static int
di_cache2mem(struct di_cache * cache,struct di_state * st)9737c478bd9Sstevel@tonic-gate di_cache2mem(struct di_cache *cache, struct di_state *st)
9747c478bd9Sstevel@tonic-gate {
9757c478bd9Sstevel@tonic-gate caddr_t pa;
9767c478bd9Sstevel@tonic-gate
9777c478bd9Sstevel@tonic-gate ASSERT(st->mem_size == 0);
9787c478bd9Sstevel@tonic-gate ASSERT(st->memlist == NULL);
9797c478bd9Sstevel@tonic-gate ASSERT(!servicing_interrupt());
9807c478bd9Sstevel@tonic-gate ASSERT(DI_CACHE_LOCKED(*cache));
9817c478bd9Sstevel@tonic-gate
9827c478bd9Sstevel@tonic-gate if (cache->cache_size == 0) {
9837c478bd9Sstevel@tonic-gate ASSERT(cache->cache_data == NULL);
9847c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "Empty cache. Skipping copy"));
9857c478bd9Sstevel@tonic-gate return (0);
9867c478bd9Sstevel@tonic-gate }
9877c478bd9Sstevel@tonic-gate
9887c478bd9Sstevel@tonic-gate ASSERT(cache->cache_data);
9897c478bd9Sstevel@tonic-gate
9907c478bd9Sstevel@tonic-gate di_allocmem(st, cache->cache_size);
9917c478bd9Sstevel@tonic-gate
9927c478bd9Sstevel@tonic-gate pa = di_mem_addr(st, 0);
9937c478bd9Sstevel@tonic-gate
9947c478bd9Sstevel@tonic-gate ASSERT(pa);
9957c478bd9Sstevel@tonic-gate
9967c478bd9Sstevel@tonic-gate /*
9977c478bd9Sstevel@tonic-gate * Verify that di_allocmem() allocates contiguous memory,
9987c478bd9Sstevel@tonic-gate * so that it is safe to do straight bcopy()
9997c478bd9Sstevel@tonic-gate */
10007c478bd9Sstevel@tonic-gate ASSERT(st->memlist != NULL);
10017c478bd9Sstevel@tonic-gate ASSERT(st->memlist->next == NULL);
10027c478bd9Sstevel@tonic-gate bcopy(cache->cache_data, pa, cache->cache_size);
10037c478bd9Sstevel@tonic-gate
10047c478bd9Sstevel@tonic-gate return (cache->cache_size);
10057c478bd9Sstevel@tonic-gate }
10067c478bd9Sstevel@tonic-gate
10077c478bd9Sstevel@tonic-gate /*
10087c478bd9Sstevel@tonic-gate * Copies a snapshot from di_state to the cache
10097c478bd9Sstevel@tonic-gate * Returns:
10107c478bd9Sstevel@tonic-gate * - 0 on failure
10117c478bd9Sstevel@tonic-gate * - size of copied data on success
10127c478bd9Sstevel@tonic-gate */
1013bc1009abSjg static size_t
di_mem2cache(struct di_state * st,struct di_cache * cache)10147c478bd9Sstevel@tonic-gate di_mem2cache(struct di_state *st, struct di_cache *cache)
10157c478bd9Sstevel@tonic-gate {
10167c478bd9Sstevel@tonic-gate size_t map_size;
10177c478bd9Sstevel@tonic-gate
10187c478bd9Sstevel@tonic-gate ASSERT(cache->cache_size == 0);
10197c478bd9Sstevel@tonic-gate ASSERT(cache->cache_data == NULL);
10207c478bd9Sstevel@tonic-gate ASSERT(!servicing_interrupt());
10217c478bd9Sstevel@tonic-gate ASSERT(DI_CACHE_LOCKED(*cache));
10227c478bd9Sstevel@tonic-gate
10237c478bd9Sstevel@tonic-gate if (st->mem_size == 0) {
10247c478bd9Sstevel@tonic-gate ASSERT(st->memlist == NULL);
10257c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "Empty memlist. Skipping copy"));
10267c478bd9Sstevel@tonic-gate return (0);
10277c478bd9Sstevel@tonic-gate }
10287c478bd9Sstevel@tonic-gate
10297c478bd9Sstevel@tonic-gate ASSERT(st->memlist);
10307c478bd9Sstevel@tonic-gate
10317c478bd9Sstevel@tonic-gate /*
10327c478bd9Sstevel@tonic-gate * The size of the memory list may be much larger than the
10337c478bd9Sstevel@tonic-gate * size of valid data (map_size). Cache only the valid data
10347c478bd9Sstevel@tonic-gate */
10357c478bd9Sstevel@tonic-gate map_size = DI_ALL_PTR(st)->map_size;
10367c478bd9Sstevel@tonic-gate if (map_size == 0 || map_size < sizeof (struct di_all) ||
10377c478bd9Sstevel@tonic-gate map_size > st->mem_size) {
10387c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "cannot cache: bad size: 0x%x", map_size));
10397c478bd9Sstevel@tonic-gate return (0);
10407c478bd9Sstevel@tonic-gate }
10417c478bd9Sstevel@tonic-gate
10427c478bd9Sstevel@tonic-gate cache->cache_data = kmem_alloc(map_size, KM_SLEEP);
10437c478bd9Sstevel@tonic-gate cache->cache_size = map_size;
10447c478bd9Sstevel@tonic-gate di_copymem(st, cache->cache_data, cache->cache_size);
10457c478bd9Sstevel@tonic-gate
10467c478bd9Sstevel@tonic-gate return (map_size);
10477c478bd9Sstevel@tonic-gate }
10487c478bd9Sstevel@tonic-gate
10497c478bd9Sstevel@tonic-gate /*
10507c478bd9Sstevel@tonic-gate * Make sure there is at least "size" bytes memory left before
10517c478bd9Sstevel@tonic-gate * going on. Otherwise, start on a new chunk.
10527c478bd9Sstevel@tonic-gate */
10537c478bd9Sstevel@tonic-gate static di_off_t
di_checkmem(struct di_state * st,di_off_t off,size_t size)10547c478bd9Sstevel@tonic-gate di_checkmem(struct di_state *st, di_off_t off, size_t size)
10557c478bd9Sstevel@tonic-gate {
10567c478bd9Sstevel@tonic-gate dcmn_err3((CE_CONT, "di_checkmem: off=%x size=%x\n",
10577c478bd9Sstevel@tonic-gate off, (int)size));
10587c478bd9Sstevel@tonic-gate
10597c478bd9Sstevel@tonic-gate /*
10607c478bd9Sstevel@tonic-gate * di_checkmem() shouldn't be called with a size of zero.
10617c478bd9Sstevel@tonic-gate * But in case it is, we want to make sure we return a valid
10627c478bd9Sstevel@tonic-gate * offset within the memlist and not an offset that points us
10637c478bd9Sstevel@tonic-gate * at the end of the memlist.
10647c478bd9Sstevel@tonic-gate */
10657c478bd9Sstevel@tonic-gate if (size == 0) {
10667c478bd9Sstevel@tonic-gate dcmn_err((CE_WARN, "di_checkmem: invalid zero size used"));
10677c478bd9Sstevel@tonic-gate size = 1;
10687c478bd9Sstevel@tonic-gate }
10697c478bd9Sstevel@tonic-gate
10707c478bd9Sstevel@tonic-gate off = DI_ALIGN(off);
10717c478bd9Sstevel@tonic-gate if ((st->mem_size - off) < size) {
10727c478bd9Sstevel@tonic-gate off = st->mem_size;
10737c478bd9Sstevel@tonic-gate di_allocmem(st, size);
10747c478bd9Sstevel@tonic-gate }
10757c478bd9Sstevel@tonic-gate
1076b9ccdc5aScth /* verify that return value is aligned */
1077b9ccdc5aScth ASSERT(off == DI_ALIGN(off));
10787c478bd9Sstevel@tonic-gate return (off);
10797c478bd9Sstevel@tonic-gate }
10807c478bd9Sstevel@tonic-gate
10817c478bd9Sstevel@tonic-gate /*
10827c478bd9Sstevel@tonic-gate * Copy the private data format from ioctl arg.
10837c478bd9Sstevel@tonic-gate * On success, the ending offset is returned. On error 0 is returned.
10847c478bd9Sstevel@tonic-gate */
10857c478bd9Sstevel@tonic-gate static di_off_t
di_copyformat(di_off_t off,struct di_state * st,intptr_t arg,int mode)10867c478bd9Sstevel@tonic-gate di_copyformat(di_off_t off, struct di_state *st, intptr_t arg, int mode)
10877c478bd9Sstevel@tonic-gate {
10887c478bd9Sstevel@tonic-gate di_off_t size;
10897c478bd9Sstevel@tonic-gate struct di_priv_data *priv;
1090b9ccdc5aScth struct di_all *all = DI_ALL_PTR(st);
10917c478bd9Sstevel@tonic-gate
10927c478bd9Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_copyformat: off=%x, arg=%p mode=%x\n",
10937c478bd9Sstevel@tonic-gate off, (void *)arg, mode));
10947c478bd9Sstevel@tonic-gate
10957c478bd9Sstevel@tonic-gate /*
10967c478bd9Sstevel@tonic-gate * Copyin data and check version.
10977c478bd9Sstevel@tonic-gate * We only handle private data version 0.
10987c478bd9Sstevel@tonic-gate */
10997c478bd9Sstevel@tonic-gate priv = kmem_alloc(sizeof (struct di_priv_data), KM_SLEEP);
11007c478bd9Sstevel@tonic-gate if ((ddi_copyin((void *)arg, priv, sizeof (struct di_priv_data),
11017c478bd9Sstevel@tonic-gate mode) != 0) || (priv->version != DI_PRIVDATA_VERSION_0)) {
11027c478bd9Sstevel@tonic-gate kmem_free(priv, sizeof (struct di_priv_data));
11037c478bd9Sstevel@tonic-gate return (0);
11047c478bd9Sstevel@tonic-gate }
11057c478bd9Sstevel@tonic-gate
11067c478bd9Sstevel@tonic-gate /*
11077c478bd9Sstevel@tonic-gate * Save di_priv_data copied from userland in snapshot.
11087c478bd9Sstevel@tonic-gate */
11097c478bd9Sstevel@tonic-gate all->pd_version = priv->version;
11107c478bd9Sstevel@tonic-gate all->n_ppdata = priv->n_parent;
11117c478bd9Sstevel@tonic-gate all->n_dpdata = priv->n_driver;
11127c478bd9Sstevel@tonic-gate
11137c478bd9Sstevel@tonic-gate /*
11147c478bd9Sstevel@tonic-gate * copyin private data format, modify offset accordingly
11157c478bd9Sstevel@tonic-gate */
11167c478bd9Sstevel@tonic-gate if (all->n_ppdata) { /* parent private data format */
11177c478bd9Sstevel@tonic-gate /*
11187c478bd9Sstevel@tonic-gate * check memory
11197c478bd9Sstevel@tonic-gate */
11207c478bd9Sstevel@tonic-gate size = all->n_ppdata * sizeof (struct di_priv_format);
1121b9ccdc5aScth all->ppdata_format = off = di_checkmem(st, off, size);
11227c478bd9Sstevel@tonic-gate if (ddi_copyin(priv->parent, di_mem_addr(st, off), size,
11237c478bd9Sstevel@tonic-gate mode) != 0) {
11247c478bd9Sstevel@tonic-gate kmem_free(priv, sizeof (struct di_priv_data));
11257c478bd9Sstevel@tonic-gate return (0);
11267c478bd9Sstevel@tonic-gate }
11277c478bd9Sstevel@tonic-gate
11287c478bd9Sstevel@tonic-gate off += size;
11297c478bd9Sstevel@tonic-gate }
11307c478bd9Sstevel@tonic-gate
11317c478bd9Sstevel@tonic-gate if (all->n_dpdata) { /* driver private data format */
11327c478bd9Sstevel@tonic-gate /*
11337c478bd9Sstevel@tonic-gate * check memory
11347c478bd9Sstevel@tonic-gate */
11357c478bd9Sstevel@tonic-gate size = all->n_dpdata * sizeof (struct di_priv_format);
1136b9ccdc5aScth all->dpdata_format = off = di_checkmem(st, off, size);
11377c478bd9Sstevel@tonic-gate if (ddi_copyin(priv->driver, di_mem_addr(st, off), size,
11387c478bd9Sstevel@tonic-gate mode) != 0) {
11397c478bd9Sstevel@tonic-gate kmem_free(priv, sizeof (struct di_priv_data));
11407c478bd9Sstevel@tonic-gate return (0);
11417c478bd9Sstevel@tonic-gate }
11427c478bd9Sstevel@tonic-gate
11437c478bd9Sstevel@tonic-gate off += size;
11447c478bd9Sstevel@tonic-gate }
11457c478bd9Sstevel@tonic-gate
11467c478bd9Sstevel@tonic-gate kmem_free(priv, sizeof (struct di_priv_data));
11477c478bd9Sstevel@tonic-gate return (off);
11487c478bd9Sstevel@tonic-gate }
11497c478bd9Sstevel@tonic-gate
11507c478bd9Sstevel@tonic-gate /*
11517c478bd9Sstevel@tonic-gate * Return the real address based on the offset (off) within snapshot
11527c478bd9Sstevel@tonic-gate */
1153b9ccdc5aScth static void *
di_mem_addr(struct di_state * st,di_off_t off)11547c478bd9Sstevel@tonic-gate di_mem_addr(struct di_state *st, di_off_t off)
11557c478bd9Sstevel@tonic-gate {
11567c478bd9Sstevel@tonic-gate struct di_mem *dcp = st->memlist;
11577c478bd9Sstevel@tonic-gate
11587c478bd9Sstevel@tonic-gate dcmn_err3((CE_CONT, "di_mem_addr: dcp=%p off=%x\n",
11597c478bd9Sstevel@tonic-gate (void *)dcp, off));
11607c478bd9Sstevel@tonic-gate
11617c478bd9Sstevel@tonic-gate ASSERT(off < st->mem_size);
11627c478bd9Sstevel@tonic-gate
11637c478bd9Sstevel@tonic-gate while (off >= dcp->buf_size) {
11647c478bd9Sstevel@tonic-gate off -= dcp->buf_size;
11657c478bd9Sstevel@tonic-gate dcp = dcp->next;
11667c478bd9Sstevel@tonic-gate }
11677c478bd9Sstevel@tonic-gate
11687c478bd9Sstevel@tonic-gate dcmn_err3((CE_CONT, "di_mem_addr: new off=%x, return = %p\n",
11697c478bd9Sstevel@tonic-gate off, (void *)(dcp->buf + off)));
11707c478bd9Sstevel@tonic-gate
11717c478bd9Sstevel@tonic-gate return (dcp->buf + off);
11727c478bd9Sstevel@tonic-gate }
11737c478bd9Sstevel@tonic-gate
11747c478bd9Sstevel@tonic-gate /*
11757c478bd9Sstevel@tonic-gate * Ideally we would use the whole key to derive the hash
11767c478bd9Sstevel@tonic-gate * value. However, the probability that two keys will
11777c478bd9Sstevel@tonic-gate * have the same dip (or pip) is very low, so
11787c478bd9Sstevel@tonic-gate * hashing by dip (or pip) pointer should suffice.
11797c478bd9Sstevel@tonic-gate */
11807c478bd9Sstevel@tonic-gate static uint_t
di_hash_byptr(void * arg,mod_hash_key_t key)11817c478bd9Sstevel@tonic-gate di_hash_byptr(void *arg, mod_hash_key_t key)
11827c478bd9Sstevel@tonic-gate {
11837c478bd9Sstevel@tonic-gate struct di_key *dik = key;
11847c478bd9Sstevel@tonic-gate size_t rshift;
11857c478bd9Sstevel@tonic-gate void *ptr;
11867c478bd9Sstevel@tonic-gate
11877c478bd9Sstevel@tonic-gate ASSERT(arg == NULL);
11887c478bd9Sstevel@tonic-gate
11897c478bd9Sstevel@tonic-gate switch (dik->k_type) {
11907c478bd9Sstevel@tonic-gate case DI_DKEY:
11917c478bd9Sstevel@tonic-gate ptr = dik->k_u.dkey.dk_dip;
11927c478bd9Sstevel@tonic-gate rshift = highbit(sizeof (struct dev_info));
11937c478bd9Sstevel@tonic-gate break;
11947c478bd9Sstevel@tonic-gate case DI_PKEY:
11957c478bd9Sstevel@tonic-gate ptr = dik->k_u.pkey.pk_pip;
11967c478bd9Sstevel@tonic-gate rshift = highbit(sizeof (struct mdi_pathinfo));
11977c478bd9Sstevel@tonic-gate break;
11987c478bd9Sstevel@tonic-gate default:
11997c478bd9Sstevel@tonic-gate panic("devinfo: unknown key type");
12007c478bd9Sstevel@tonic-gate /*NOTREACHED*/
12017c478bd9Sstevel@tonic-gate }
12027c478bd9Sstevel@tonic-gate return (mod_hash_byptr((void *)rshift, ptr));
12037c478bd9Sstevel@tonic-gate }
12047c478bd9Sstevel@tonic-gate
12057c478bd9Sstevel@tonic-gate static void
di_key_dtor(mod_hash_key_t key)12067c478bd9Sstevel@tonic-gate di_key_dtor(mod_hash_key_t key)
12077c478bd9Sstevel@tonic-gate {
12087c478bd9Sstevel@tonic-gate char *path_addr;
12097c478bd9Sstevel@tonic-gate struct di_key *dik = key;
12107c478bd9Sstevel@tonic-gate
12117c478bd9Sstevel@tonic-gate switch (dik->k_type) {
12127c478bd9Sstevel@tonic-gate case DI_DKEY:
12137c478bd9Sstevel@tonic-gate break;
12147c478bd9Sstevel@tonic-gate case DI_PKEY:
12157c478bd9Sstevel@tonic-gate path_addr = dik->k_u.pkey.pk_path_addr;
12167c478bd9Sstevel@tonic-gate if (path_addr)
12177c478bd9Sstevel@tonic-gate kmem_free(path_addr, strlen(path_addr) + 1);
12187c478bd9Sstevel@tonic-gate break;
12197c478bd9Sstevel@tonic-gate default:
12207c478bd9Sstevel@tonic-gate panic("devinfo: unknown key type");
12217c478bd9Sstevel@tonic-gate /*NOTREACHED*/
12227c478bd9Sstevel@tonic-gate }
12237c478bd9Sstevel@tonic-gate
12247c478bd9Sstevel@tonic-gate kmem_free(dik, sizeof (struct di_key));
12257c478bd9Sstevel@tonic-gate }
12267c478bd9Sstevel@tonic-gate
12277c478bd9Sstevel@tonic-gate static int
di_dkey_cmp(struct di_dkey * dk1,struct di_dkey * dk2)12287c478bd9Sstevel@tonic-gate di_dkey_cmp(struct di_dkey *dk1, struct di_dkey *dk2)
12297c478bd9Sstevel@tonic-gate {
12307c478bd9Sstevel@tonic-gate if (dk1->dk_dip != dk2->dk_dip)
12317c478bd9Sstevel@tonic-gate return (dk1->dk_dip > dk2->dk_dip ? 1 : -1);
12327c478bd9Sstevel@tonic-gate
1233a204de77Scth if (dk1->dk_major != DDI_MAJOR_T_NONE &&
1234a204de77Scth dk2->dk_major != DDI_MAJOR_T_NONE) {
12357c478bd9Sstevel@tonic-gate if (dk1->dk_major != dk2->dk_major)
12367c478bd9Sstevel@tonic-gate return (dk1->dk_major > dk2->dk_major ? 1 : -1);
12377c478bd9Sstevel@tonic-gate
12387c478bd9Sstevel@tonic-gate if (dk1->dk_inst != dk2->dk_inst)
12397c478bd9Sstevel@tonic-gate return (dk1->dk_inst > dk2->dk_inst ? 1 : -1);
12407c478bd9Sstevel@tonic-gate }
12417c478bd9Sstevel@tonic-gate
12427c478bd9Sstevel@tonic-gate if (dk1->dk_nodeid != dk2->dk_nodeid)
12437c478bd9Sstevel@tonic-gate return (dk1->dk_nodeid > dk2->dk_nodeid ? 1 : -1);
12447c478bd9Sstevel@tonic-gate
12457c478bd9Sstevel@tonic-gate return (0);
12467c478bd9Sstevel@tonic-gate }
12477c478bd9Sstevel@tonic-gate
12487c478bd9Sstevel@tonic-gate static int
di_pkey_cmp(struct di_pkey * pk1,struct di_pkey * pk2)12497c478bd9Sstevel@tonic-gate di_pkey_cmp(struct di_pkey *pk1, struct di_pkey *pk2)
12507c478bd9Sstevel@tonic-gate {
12517c478bd9Sstevel@tonic-gate char *p1, *p2;
12527c478bd9Sstevel@tonic-gate int rv;
12537c478bd9Sstevel@tonic-gate
12547c478bd9Sstevel@tonic-gate if (pk1->pk_pip != pk2->pk_pip)
12557c478bd9Sstevel@tonic-gate return (pk1->pk_pip > pk2->pk_pip ? 1 : -1);
12567c478bd9Sstevel@tonic-gate
12577c478bd9Sstevel@tonic-gate p1 = pk1->pk_path_addr;
12587c478bd9Sstevel@tonic-gate p2 = pk2->pk_path_addr;
12597c478bd9Sstevel@tonic-gate
12607c478bd9Sstevel@tonic-gate p1 = p1 ? p1 : "";
12617c478bd9Sstevel@tonic-gate p2 = p2 ? p2 : "";
12627c478bd9Sstevel@tonic-gate
12637c478bd9Sstevel@tonic-gate rv = strcmp(p1, p2);
12647c478bd9Sstevel@tonic-gate if (rv)
12657c478bd9Sstevel@tonic-gate return (rv > 0 ? 1 : -1);
12667c478bd9Sstevel@tonic-gate
12677c478bd9Sstevel@tonic-gate if (pk1->pk_client != pk2->pk_client)
12687c478bd9Sstevel@tonic-gate return (pk1->pk_client > pk2->pk_client ? 1 : -1);
12697c478bd9Sstevel@tonic-gate
12707c478bd9Sstevel@tonic-gate if (pk1->pk_phci != pk2->pk_phci)
12717c478bd9Sstevel@tonic-gate return (pk1->pk_phci > pk2->pk_phci ? 1 : -1);
12727c478bd9Sstevel@tonic-gate
12737c478bd9Sstevel@tonic-gate return (0);
12747c478bd9Sstevel@tonic-gate }
12757c478bd9Sstevel@tonic-gate
12767c478bd9Sstevel@tonic-gate static int
di_key_cmp(mod_hash_key_t key1,mod_hash_key_t key2)12777c478bd9Sstevel@tonic-gate di_key_cmp(mod_hash_key_t key1, mod_hash_key_t key2)
12787c478bd9Sstevel@tonic-gate {
12797c478bd9Sstevel@tonic-gate struct di_key *dik1, *dik2;
12807c478bd9Sstevel@tonic-gate
12817c478bd9Sstevel@tonic-gate dik1 = key1;
12827c478bd9Sstevel@tonic-gate dik2 = key2;
12837c478bd9Sstevel@tonic-gate
12847c478bd9Sstevel@tonic-gate if (dik1->k_type != dik2->k_type) {
12857c478bd9Sstevel@tonic-gate panic("devinfo: mismatched keys");
12867c478bd9Sstevel@tonic-gate /*NOTREACHED*/
12877c478bd9Sstevel@tonic-gate }
12887c478bd9Sstevel@tonic-gate
12897c478bd9Sstevel@tonic-gate switch (dik1->k_type) {
12907c478bd9Sstevel@tonic-gate case DI_DKEY:
12917c478bd9Sstevel@tonic-gate return (di_dkey_cmp(&(dik1->k_u.dkey), &(dik2->k_u.dkey)));
12927c478bd9Sstevel@tonic-gate case DI_PKEY:
12937c478bd9Sstevel@tonic-gate return (di_pkey_cmp(&(dik1->k_u.pkey), &(dik2->k_u.pkey)));
12947c478bd9Sstevel@tonic-gate default:
12957c478bd9Sstevel@tonic-gate panic("devinfo: unknown key type");
12967c478bd9Sstevel@tonic-gate /*NOTREACHED*/
12977c478bd9Sstevel@tonic-gate }
12987c478bd9Sstevel@tonic-gate }
12997c478bd9Sstevel@tonic-gate
130094c894bbSVikram Hegde static void
di_copy_aliases(struct di_state * st,alias_pair_t * apair,di_off_t * offp)130194c894bbSVikram Hegde di_copy_aliases(struct di_state *st, alias_pair_t *apair, di_off_t *offp)
130294c894bbSVikram Hegde {
130394c894bbSVikram Hegde di_off_t off;
130494c894bbSVikram Hegde struct di_all *all = DI_ALL_PTR(st);
130594c894bbSVikram Hegde struct di_alias *di_alias;
130694c894bbSVikram Hegde di_off_t curroff;
130794c894bbSVikram Hegde dev_info_t *currdip;
130894c894bbSVikram Hegde size_t size;
130994c894bbSVikram Hegde
131094c894bbSVikram Hegde currdip = NULL;
131194c894bbSVikram Hegde if (resolve_pathname(apair->pair_alias, &currdip, NULL, NULL) != 0) {
131294c894bbSVikram Hegde return;
131394c894bbSVikram Hegde }
131494c894bbSVikram Hegde
131594c894bbSVikram Hegde if (di_dip_find(st, currdip, &curroff) != 0) {
131694c894bbSVikram Hegde ndi_rele_devi(currdip);
131794c894bbSVikram Hegde return;
131894c894bbSVikram Hegde }
131994c894bbSVikram Hegde ndi_rele_devi(currdip);
132094c894bbSVikram Hegde
132194c894bbSVikram Hegde off = *offp;
132294c894bbSVikram Hegde size = sizeof (struct di_alias);
132394c894bbSVikram Hegde size += strlen(apair->pair_alias) + 1;
132494c894bbSVikram Hegde off = di_checkmem(st, off, size);
132594c894bbSVikram Hegde di_alias = DI_ALIAS(di_mem_addr(st, off));
132694c894bbSVikram Hegde
132794c894bbSVikram Hegde di_alias->self = off;
132894c894bbSVikram Hegde di_alias->next = all->aliases;
132994c894bbSVikram Hegde all->aliases = off;
133094c894bbSVikram Hegde (void) strcpy(di_alias->alias, apair->pair_alias);
133194c894bbSVikram Hegde di_alias->curroff = curroff;
133294c894bbSVikram Hegde
133394c894bbSVikram Hegde off += size;
133494c894bbSVikram Hegde
133594c894bbSVikram Hegde *offp = off;
133694c894bbSVikram Hegde }
133794c894bbSVikram Hegde
13387c478bd9Sstevel@tonic-gate /*
13397c478bd9Sstevel@tonic-gate * This is the main function that takes a snapshot
13407c478bd9Sstevel@tonic-gate */
13417c478bd9Sstevel@tonic-gate static di_off_t
di_snapshot(struct di_state * st)13427c478bd9Sstevel@tonic-gate di_snapshot(struct di_state *st)
13437c478bd9Sstevel@tonic-gate {
13447c478bd9Sstevel@tonic-gate di_off_t off;
13457c478bd9Sstevel@tonic-gate struct di_all *all;
13467c478bd9Sstevel@tonic-gate dev_info_t *rootnode;
13477c478bd9Sstevel@tonic-gate char buf[80];
1348dc03c567Scth int plen;
1349dc03c567Scth char *path;
1350dc03c567Scth vnode_t *vp;
135194c894bbSVikram Hegde int i;
13527c478bd9Sstevel@tonic-gate
1353b9ccdc5aScth all = DI_ALL_PTR(st);
13547c478bd9Sstevel@tonic-gate dcmn_err((CE_CONT, "Taking a snapshot of devinfo tree...\n"));
13557c478bd9Sstevel@tonic-gate
13567c478bd9Sstevel@tonic-gate /*
135794c894bbSVikram Hegde * Translate requested root path if an alias and snap-root != "/"
135894c894bbSVikram Hegde */
135994c894bbSVikram Hegde if (ddi_aliases_present == B_TRUE && strcmp(all->root_path, "/") != 0) {
136094c894bbSVikram Hegde /* If there is no redirected alias, use root_path as is */
136194c894bbSVikram Hegde rootnode = ddi_alias_redirect(all->root_path);
136294c894bbSVikram Hegde if (rootnode) {
136394c894bbSVikram Hegde (void) ddi_pathname(rootnode, all->root_path);
136494c894bbSVikram Hegde goto got_root;
136594c894bbSVikram Hegde }
136694c894bbSVikram Hegde }
136794c894bbSVikram Hegde
136894c894bbSVikram Hegde /*
1369dc03c567Scth * Verify path before entrusting it to e_ddi_hold_devi_by_path because
1370dc03c567Scth * some platforms have OBP bugs where executing the NDI_PROMNAME code
1371dc03c567Scth * path against an invalid path results in panic. The lookupnameat
1372dc03c567Scth * is done relative to rootdir without a leading '/' on "devices/"
1373dc03c567Scth * to force the lookup to occur in the global zone.
1374dc03c567Scth */
1375dc03c567Scth plen = strlen("devices/") + strlen(all->root_path) + 1;
1376dc03c567Scth path = kmem_alloc(plen, KM_SLEEP);
1377dc03c567Scth (void) snprintf(path, plen, "devices/%s", all->root_path);
1378dc03c567Scth if (lookupnameat(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp, rootdir)) {
1379dc03c567Scth dcmn_err((CE_CONT, "Devinfo node %s not found\n",
1380dc03c567Scth all->root_path));
1381dc03c567Scth kmem_free(path, plen);
1382dc03c567Scth return (0);
1383dc03c567Scth }
1384dc03c567Scth kmem_free(path, plen);
1385dc03c567Scth VN_RELE(vp);
1386dc03c567Scth
1387dc03c567Scth /*
13887c478bd9Sstevel@tonic-gate * Hold the devinfo node referred by the path.
13897c478bd9Sstevel@tonic-gate */
13907c478bd9Sstevel@tonic-gate rootnode = e_ddi_hold_devi_by_path(all->root_path, 0);
13917c478bd9Sstevel@tonic-gate if (rootnode == NULL) {
13927c478bd9Sstevel@tonic-gate dcmn_err((CE_CONT, "Devinfo node %s not found\n",
13937c478bd9Sstevel@tonic-gate all->root_path));
13947c478bd9Sstevel@tonic-gate return (0);
13957c478bd9Sstevel@tonic-gate }
13967c478bd9Sstevel@tonic-gate
139794c894bbSVikram Hegde got_root:
13987c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf),
13997c478bd9Sstevel@tonic-gate "devinfo registered dips (statep=%p)", (void *)st);
14007c478bd9Sstevel@tonic-gate
14017c478bd9Sstevel@tonic-gate st->reg_dip_hash = mod_hash_create_extended(buf, 64,
14027c478bd9Sstevel@tonic-gate di_key_dtor, mod_hash_null_valdtor, di_hash_byptr,
14037c478bd9Sstevel@tonic-gate NULL, di_key_cmp, KM_SLEEP);
14047c478bd9Sstevel@tonic-gate
14057c478bd9Sstevel@tonic-gate
14067c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf),
14077c478bd9Sstevel@tonic-gate "devinfo registered pips (statep=%p)", (void *)st);
14087c478bd9Sstevel@tonic-gate
14097c478bd9Sstevel@tonic-gate st->reg_pip_hash = mod_hash_create_extended(buf, 64,
14107c478bd9Sstevel@tonic-gate di_key_dtor, mod_hash_null_valdtor, di_hash_byptr,
14117c478bd9Sstevel@tonic-gate NULL, di_key_cmp, KM_SLEEP);
14127c478bd9Sstevel@tonic-gate
141326947304SEvan Yan if (DINFOHP & st->command) {
141426947304SEvan Yan list_create(&st->hp_list, sizeof (i_hp_t),
141526947304SEvan Yan offsetof(i_hp_t, hp_link));
141626947304SEvan Yan }
141726947304SEvan Yan
14187c478bd9Sstevel@tonic-gate /*
14197c478bd9Sstevel@tonic-gate * copy the device tree
14207c478bd9Sstevel@tonic-gate */
14217c478bd9Sstevel@tonic-gate off = di_copytree(DEVI(rootnode), &all->top_devinfo, st);
14227c478bd9Sstevel@tonic-gate
14238c4f8890Srs135747 if (DINFOPATH & st->command) {
14248c4f8890Srs135747 mdi_walk_vhcis(build_vhci_list, st);
14258c4f8890Srs135747 }
14268c4f8890Srs135747
142726947304SEvan Yan if (DINFOHP & st->command) {
142826947304SEvan Yan di_hotplug_children(st);
142926947304SEvan Yan }
143026947304SEvan Yan
14317c478bd9Sstevel@tonic-gate ddi_release_devi(rootnode);
14327c478bd9Sstevel@tonic-gate
14337c478bd9Sstevel@tonic-gate /*
14347c478bd9Sstevel@tonic-gate * copy the devnames array
14357c478bd9Sstevel@tonic-gate */
14367c478bd9Sstevel@tonic-gate all->devnames = off;
14377c478bd9Sstevel@tonic-gate off = di_copydevnm(&all->devnames, st);
14387c478bd9Sstevel@tonic-gate
14397c478bd9Sstevel@tonic-gate
14407c478bd9Sstevel@tonic-gate /* initialize the hash tables */
14417c478bd9Sstevel@tonic-gate st->lnode_count = 0;
14427c478bd9Sstevel@tonic-gate st->link_count = 0;
14437c478bd9Sstevel@tonic-gate
14447c478bd9Sstevel@tonic-gate if (DINFOLYR & st->command) {
14457c478bd9Sstevel@tonic-gate off = di_getlink_data(off, st);
14467c478bd9Sstevel@tonic-gate }
14477c478bd9Sstevel@tonic-gate
144894c894bbSVikram Hegde all->aliases = 0;
144994c894bbSVikram Hegde if (ddi_aliases_present == B_FALSE)
145094c894bbSVikram Hegde goto done;
145194c894bbSVikram Hegde
145294c894bbSVikram Hegde for (i = 0; i < ddi_aliases.dali_num_pairs; i++) {
145394c894bbSVikram Hegde di_copy_aliases(st, &(ddi_aliases.dali_alias_pairs[i]), &off);
145494c894bbSVikram Hegde }
145594c894bbSVikram Hegde
145694c894bbSVikram Hegde done:
14577c478bd9Sstevel@tonic-gate /*
14587c478bd9Sstevel@tonic-gate * Free up hash tables
14597c478bd9Sstevel@tonic-gate */
14607c478bd9Sstevel@tonic-gate mod_hash_destroy_hash(st->reg_dip_hash);
14617c478bd9Sstevel@tonic-gate mod_hash_destroy_hash(st->reg_pip_hash);
14627c478bd9Sstevel@tonic-gate
14637c478bd9Sstevel@tonic-gate /*
14647c478bd9Sstevel@tonic-gate * Record the timestamp now that we are done with snapshot.
14657c478bd9Sstevel@tonic-gate *
14667c478bd9Sstevel@tonic-gate * We compute the checksum later and then only if we cache
14677c478bd9Sstevel@tonic-gate * the snapshot, since checksumming adds some overhead.
14687c478bd9Sstevel@tonic-gate * The checksum is checked later if we read the cache file.
14697c478bd9Sstevel@tonic-gate * from disk.
14707c478bd9Sstevel@tonic-gate *
14717c478bd9Sstevel@tonic-gate * Set checksum field to 0 as CRC is calculated with that
14727c478bd9Sstevel@tonic-gate * field set to 0.
14737c478bd9Sstevel@tonic-gate */
14747c478bd9Sstevel@tonic-gate all->snapshot_time = ddi_get_time();
14757c478bd9Sstevel@tonic-gate all->cache_checksum = 0;
14767c478bd9Sstevel@tonic-gate
1477bc1009abSjg ASSERT(all->snapshot_time != 0);
1478bc1009abSjg
14797c478bd9Sstevel@tonic-gate return (off);
14807c478bd9Sstevel@tonic-gate }
14817c478bd9Sstevel@tonic-gate
14827c478bd9Sstevel@tonic-gate /*
14833c34adc5Sramat * Take a snapshot and clean /etc/devices files if DINFOCLEANUP is set
14843c34adc5Sramat */
14853c34adc5Sramat static di_off_t
di_snapshot_and_clean(struct di_state * st)14863c34adc5Sramat di_snapshot_and_clean(struct di_state *st)
14873c34adc5Sramat {
14883c34adc5Sramat di_off_t off;
14893c34adc5Sramat
14903c34adc5Sramat modunload_disable();
14913c34adc5Sramat off = di_snapshot(st);
14923c34adc5Sramat if (off != 0 && (st->command & DINFOCLEANUP)) {
14933c34adc5Sramat ASSERT(DEVICES_FILES_CLEANABLE(st));
14943c34adc5Sramat /*
14953c34adc5Sramat * Cleanup /etc/devices files:
14963c34adc5Sramat * In order to accurately account for the system configuration
14973c34adc5Sramat * in /etc/devices files, the appropriate drivers must be
14983c34adc5Sramat * fully configured before the cleanup starts.
14993c34adc5Sramat * So enable modunload only after the cleanup.
15003c34adc5Sramat */
15013c34adc5Sramat i_ddi_clean_devices_files();
15021aef0e11Sjg /*
15031aef0e11Sjg * Remove backing store nodes for unused devices,
15041aef0e11Sjg * which retain past permissions customizations
15051aef0e11Sjg * and may be undesired for newly configured devices.
15061aef0e11Sjg */
15071aef0e11Sjg dev_devices_cleanup();
15083c34adc5Sramat }
15093c34adc5Sramat modunload_enable();
15103c34adc5Sramat
15113c34adc5Sramat return (off);
15123c34adc5Sramat }
15133c34adc5Sramat
15143c34adc5Sramat /*
15158c4f8890Srs135747 * construct vhci linkage in the snapshot.
15168c4f8890Srs135747 */
1517b9ccdc5aScth static int
build_vhci_list(dev_info_t * vh_devinfo,void * arg)15188c4f8890Srs135747 build_vhci_list(dev_info_t *vh_devinfo, void *arg)
15198c4f8890Srs135747 {
15208c4f8890Srs135747 struct di_all *all;
15218c4f8890Srs135747 struct di_node *me;
15228c4f8890Srs135747 struct di_state *st;
15238c4f8890Srs135747 di_off_t off;
1524bc1009abSjg phci_walk_arg_t pwa;
15258c4f8890Srs135747
15268c4f8890Srs135747 dcmn_err3((CE_CONT, "build_vhci list\n"));
15278c4f8890Srs135747
1528b9ccdc5aScth dcmn_err3((CE_CONT, "vhci node %s%d\n",
1529b9ccdc5aScth ddi_driver_name(vh_devinfo), ddi_get_instance(vh_devinfo)));
15308c4f8890Srs135747
15318c4f8890Srs135747 st = (struct di_state *)arg;
15328c4f8890Srs135747 if (di_dip_find(st, vh_devinfo, &off) != 0) {
15338c4f8890Srs135747 dcmn_err((CE_WARN, "di_dip_find error for the given node\n"));
15348c4f8890Srs135747 return (DDI_WALK_TERMINATE);
15358c4f8890Srs135747 }
15368c4f8890Srs135747
15378c4f8890Srs135747 dcmn_err3((CE_CONT, "st->mem_size: %d vh_devinfo off: 0x%x\n",
15388c4f8890Srs135747 st->mem_size, off));
15398c4f8890Srs135747
1540b9ccdc5aScth all = DI_ALL_PTR(st);
15418c4f8890Srs135747 if (all->top_vhci_devinfo == 0) {
15428c4f8890Srs135747 all->top_vhci_devinfo = off;
15438c4f8890Srs135747 } else {
1544b9ccdc5aScth me = DI_NODE(di_mem_addr(st, all->top_vhci_devinfo));
15458c4f8890Srs135747
15468c4f8890Srs135747 while (me->next_vhci != 0) {
1547b9ccdc5aScth me = DI_NODE(di_mem_addr(st, me->next_vhci));
15488c4f8890Srs135747 }
15498c4f8890Srs135747
15508c4f8890Srs135747 me->next_vhci = off;
15518c4f8890Srs135747 }
15528c4f8890Srs135747
15538c4f8890Srs135747 pwa.off = off;
15548c4f8890Srs135747 pwa.st = st;
15558c4f8890Srs135747 mdi_vhci_walk_phcis(vh_devinfo, build_phci_list, &pwa);
15568c4f8890Srs135747
15578c4f8890Srs135747 return (DDI_WALK_CONTINUE);
15588c4f8890Srs135747 }
15598c4f8890Srs135747
15608c4f8890Srs135747 /*
15618c4f8890Srs135747 * construct phci linkage for the given vhci in the snapshot.
15628c4f8890Srs135747 */
1563b9ccdc5aScth static int
build_phci_list(dev_info_t * ph_devinfo,void * arg)15648c4f8890Srs135747 build_phci_list(dev_info_t *ph_devinfo, void *arg)
15658c4f8890Srs135747 {
15668c4f8890Srs135747 struct di_node *vh_di_node;
15678c4f8890Srs135747 struct di_node *me;
1568bc1009abSjg phci_walk_arg_t *pwa;
15698c4f8890Srs135747 di_off_t off;
15708c4f8890Srs135747
1571bc1009abSjg pwa = (phci_walk_arg_t *)arg;
15728c4f8890Srs135747
15738c4f8890Srs135747 dcmn_err3((CE_CONT, "build_phci list for vhci at offset: 0x%x\n",
15748c4f8890Srs135747 pwa->off));
15758c4f8890Srs135747
1576b9ccdc5aScth vh_di_node = DI_NODE(di_mem_addr(pwa->st, pwa->off));
15778c4f8890Srs135747 if (di_dip_find(pwa->st, ph_devinfo, &off) != 0) {
15788c4f8890Srs135747 dcmn_err((CE_WARN, "di_dip_find error for the given node\n"));
15798c4f8890Srs135747 return (DDI_WALK_TERMINATE);
15808c4f8890Srs135747 }
15818c4f8890Srs135747
1582b9ccdc5aScth dcmn_err3((CE_CONT, "phci node %s%d, at offset 0x%x\n",
1583b9ccdc5aScth ddi_driver_name(ph_devinfo), ddi_get_instance(ph_devinfo), off));
15848c4f8890Srs135747
15858c4f8890Srs135747 if (vh_di_node->top_phci == 0) {
15868c4f8890Srs135747 vh_di_node->top_phci = off;
15878c4f8890Srs135747 return (DDI_WALK_CONTINUE);
15888c4f8890Srs135747 }
15898c4f8890Srs135747
1590b9ccdc5aScth me = DI_NODE(di_mem_addr(pwa->st, vh_di_node->top_phci));
15918c4f8890Srs135747
15928c4f8890Srs135747 while (me->next_phci != 0) {
1593b9ccdc5aScth me = DI_NODE(di_mem_addr(pwa->st, me->next_phci));
15948c4f8890Srs135747 }
15958c4f8890Srs135747 me->next_phci = off;
15968c4f8890Srs135747
15978c4f8890Srs135747 return (DDI_WALK_CONTINUE);
15988c4f8890Srs135747 }
15998c4f8890Srs135747
16008c4f8890Srs135747 /*
16017c478bd9Sstevel@tonic-gate * Assumes all devinfo nodes in device tree have been snapshotted
16027c478bd9Sstevel@tonic-gate */
16037c478bd9Sstevel@tonic-gate static void
snap_driver_list(struct di_state * st,struct devnames * dnp,di_off_t * off_p)1604b9ccdc5aScth snap_driver_list(struct di_state *st, struct devnames *dnp, di_off_t *off_p)
16057c478bd9Sstevel@tonic-gate {
16067c478bd9Sstevel@tonic-gate struct dev_info *node;
16077c478bd9Sstevel@tonic-gate struct di_node *me;
16087c478bd9Sstevel@tonic-gate di_off_t off;
16097c478bd9Sstevel@tonic-gate
16107c478bd9Sstevel@tonic-gate ASSERT(mutex_owned(&dnp->dn_lock));
16117c478bd9Sstevel@tonic-gate
16127c478bd9Sstevel@tonic-gate node = DEVI(dnp->dn_head);
16137c478bd9Sstevel@tonic-gate for (; node; node = node->devi_next) {
16147c478bd9Sstevel@tonic-gate if (di_dip_find(st, (dev_info_t *)node, &off) != 0)
16157c478bd9Sstevel@tonic-gate continue;
16167c478bd9Sstevel@tonic-gate
16177c478bd9Sstevel@tonic-gate ASSERT(off > 0);
1618b9ccdc5aScth me = DI_NODE(di_mem_addr(st, off));
16197c478bd9Sstevel@tonic-gate ASSERT(me->next == 0 || me->next == -1);
16207c478bd9Sstevel@tonic-gate /*
16217c478bd9Sstevel@tonic-gate * Only nodes which were BOUND when they were
16227c478bd9Sstevel@tonic-gate * snapshotted will be added to per-driver list.
16237c478bd9Sstevel@tonic-gate */
16247c478bd9Sstevel@tonic-gate if (me->next != -1)
16257c478bd9Sstevel@tonic-gate continue;
16267c478bd9Sstevel@tonic-gate
1627b9ccdc5aScth *off_p = off;
1628b9ccdc5aScth off_p = &me->next;
16297c478bd9Sstevel@tonic-gate }
16307c478bd9Sstevel@tonic-gate
1631b9ccdc5aScth *off_p = 0;
16327c478bd9Sstevel@tonic-gate }
16337c478bd9Sstevel@tonic-gate
16347c478bd9Sstevel@tonic-gate /*
16357c478bd9Sstevel@tonic-gate * Copy the devnames array, so we have a list of drivers in the snapshot.
16367c478bd9Sstevel@tonic-gate * Also makes it possible to locate the per-driver devinfo nodes.
16377c478bd9Sstevel@tonic-gate */
16387c478bd9Sstevel@tonic-gate static di_off_t
di_copydevnm(di_off_t * off_p,struct di_state * st)16397c478bd9Sstevel@tonic-gate di_copydevnm(di_off_t *off_p, struct di_state *st)
16407c478bd9Sstevel@tonic-gate {
16417c478bd9Sstevel@tonic-gate int i;
16427c478bd9Sstevel@tonic-gate di_off_t off;
16437c478bd9Sstevel@tonic-gate size_t size;
16447c478bd9Sstevel@tonic-gate struct di_devnm *dnp;
16457c478bd9Sstevel@tonic-gate
16467c478bd9Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_copydevnm: *off_p = %p\n", (void *)off_p));
16477c478bd9Sstevel@tonic-gate
16487c478bd9Sstevel@tonic-gate /*
16497c478bd9Sstevel@tonic-gate * make sure there is some allocated memory
16507c478bd9Sstevel@tonic-gate */
16517c478bd9Sstevel@tonic-gate size = devcnt * sizeof (struct di_devnm);
1652b9ccdc5aScth *off_p = off = di_checkmem(st, *off_p, size);
1653b9ccdc5aScth dnp = DI_DEVNM(di_mem_addr(st, off));
1654b9ccdc5aScth off += size;
16557c478bd9Sstevel@tonic-gate
16567c478bd9Sstevel@tonic-gate dcmn_err((CE_CONT, "Start copying devnamesp[%d] at offset 0x%x\n",
16577c478bd9Sstevel@tonic-gate devcnt, off));
16587c478bd9Sstevel@tonic-gate
16597c478bd9Sstevel@tonic-gate for (i = 0; i < devcnt; i++) {
16607c478bd9Sstevel@tonic-gate if (devnamesp[i].dn_name == NULL) {
16617c478bd9Sstevel@tonic-gate continue;
16627c478bd9Sstevel@tonic-gate }
16637c478bd9Sstevel@tonic-gate
16647c478bd9Sstevel@tonic-gate /*
16657c478bd9Sstevel@tonic-gate * dn_name is not freed during driver unload or removal.
16667c478bd9Sstevel@tonic-gate *
16677c478bd9Sstevel@tonic-gate * There is a race condition when make_devname() changes
16687c478bd9Sstevel@tonic-gate * dn_name during our strcpy. This should be rare since
16697c478bd9Sstevel@tonic-gate * only add_drv does this. At any rate, we never had a
16707c478bd9Sstevel@tonic-gate * problem with ddi_name_to_major(), which should have
16717c478bd9Sstevel@tonic-gate * the same problem.
16727c478bd9Sstevel@tonic-gate */
16737c478bd9Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_copydevnm: %s%d, off=%x\n",
1674b9ccdc5aScth devnamesp[i].dn_name, devnamesp[i].dn_instance, off));
16757c478bd9Sstevel@tonic-gate
1676b9ccdc5aScth size = strlen(devnamesp[i].dn_name) + 1;
1677b9ccdc5aScth dnp[i].name = off = di_checkmem(st, off, size);
16787c478bd9Sstevel@tonic-gate (void) strcpy((char *)di_mem_addr(st, off),
16797c478bd9Sstevel@tonic-gate devnamesp[i].dn_name);
1680b9ccdc5aScth off += size;
16817c478bd9Sstevel@tonic-gate
16827c478bd9Sstevel@tonic-gate mutex_enter(&devnamesp[i].dn_lock);
16837c478bd9Sstevel@tonic-gate
16847c478bd9Sstevel@tonic-gate /*
16857c478bd9Sstevel@tonic-gate * Snapshot per-driver node list
16867c478bd9Sstevel@tonic-gate */
16877c478bd9Sstevel@tonic-gate snap_driver_list(st, &devnamesp[i], &dnp[i].head);
16887c478bd9Sstevel@tonic-gate
16897c478bd9Sstevel@tonic-gate /*
16907c478bd9Sstevel@tonic-gate * This is not used by libdevinfo, leave it for now
16917c478bd9Sstevel@tonic-gate */
16927c478bd9Sstevel@tonic-gate dnp[i].flags = devnamesp[i].dn_flags;
16937c478bd9Sstevel@tonic-gate dnp[i].instance = devnamesp[i].dn_instance;
16947c478bd9Sstevel@tonic-gate
16957c478bd9Sstevel@tonic-gate /*
16967c478bd9Sstevel@tonic-gate * get global properties
16977c478bd9Sstevel@tonic-gate */
16987c478bd9Sstevel@tonic-gate if ((DINFOPROP & st->command) &&
16997c478bd9Sstevel@tonic-gate devnamesp[i].dn_global_prop_ptr) {
17007c478bd9Sstevel@tonic-gate dnp[i].global_prop = off;
1701b9ccdc5aScth off = di_getprop(DI_PROP_GLB_LIST,
1702b9ccdc5aScth &devnamesp[i].dn_global_prop_ptr->prop_list,
1703b9ccdc5aScth &dnp[i].global_prop, st, NULL);
17047c478bd9Sstevel@tonic-gate }
17057c478bd9Sstevel@tonic-gate
17067c478bd9Sstevel@tonic-gate /*
17077c478bd9Sstevel@tonic-gate * Bit encode driver ops: & bus_ops, cb_ops, & cb_ops->cb_str
17087c478bd9Sstevel@tonic-gate */
17097c478bd9Sstevel@tonic-gate if (CB_DRV_INSTALLED(devopsp[i])) {
17107c478bd9Sstevel@tonic-gate if (devopsp[i]->devo_cb_ops) {
17117c478bd9Sstevel@tonic-gate dnp[i].ops |= DI_CB_OPS;
17127c478bd9Sstevel@tonic-gate if (devopsp[i]->devo_cb_ops->cb_str)
17137c478bd9Sstevel@tonic-gate dnp[i].ops |= DI_STREAM_OPS;
17147c478bd9Sstevel@tonic-gate }
17157c478bd9Sstevel@tonic-gate if (NEXUS_DRV(devopsp[i])) {
17167c478bd9Sstevel@tonic-gate dnp[i].ops |= DI_BUS_OPS;
17177c478bd9Sstevel@tonic-gate }
17187c478bd9Sstevel@tonic-gate }
17197c478bd9Sstevel@tonic-gate
17207c478bd9Sstevel@tonic-gate mutex_exit(&devnamesp[i].dn_lock);
17217c478bd9Sstevel@tonic-gate }
17227c478bd9Sstevel@tonic-gate
17237c478bd9Sstevel@tonic-gate dcmn_err((CE_CONT, "End copying devnamesp at offset 0x%x\n", off));
17247c478bd9Sstevel@tonic-gate
17257c478bd9Sstevel@tonic-gate return (off);
17267c478bd9Sstevel@tonic-gate }
17277c478bd9Sstevel@tonic-gate
17287c478bd9Sstevel@tonic-gate /*
17297c478bd9Sstevel@tonic-gate * Copy the kernel devinfo tree. The tree and the devnames array forms
17307c478bd9Sstevel@tonic-gate * the entire snapshot (see also di_copydevnm).
17317c478bd9Sstevel@tonic-gate */
17327c478bd9Sstevel@tonic-gate static di_off_t
di_copytree(struct dev_info * root,di_off_t * off_p,struct di_state * st)17337c478bd9Sstevel@tonic-gate di_copytree(struct dev_info *root, di_off_t *off_p, struct di_state *st)
17347c478bd9Sstevel@tonic-gate {
17357c478bd9Sstevel@tonic-gate di_off_t off;
1736b9ccdc5aScth struct dev_info *node;
17377c478bd9Sstevel@tonic-gate struct di_stack *dsp = kmem_zalloc(sizeof (struct di_stack), KM_SLEEP);
17387c478bd9Sstevel@tonic-gate
17397c478bd9Sstevel@tonic-gate dcmn_err((CE_CONT, "di_copytree: root = %p, *off_p = %x\n",
17407c478bd9Sstevel@tonic-gate (void *)root, *off_p));
17417c478bd9Sstevel@tonic-gate
17427c478bd9Sstevel@tonic-gate /* force attach drivers */
1743737d277aScth if (i_ddi_devi_attached((dev_info_t *)root) &&
17447c478bd9Sstevel@tonic-gate (st->command & DINFOSUBTREE) && (st->command & DINFOFORCE)) {
17457c478bd9Sstevel@tonic-gate (void) ndi_devi_config((dev_info_t *)root,
17467c478bd9Sstevel@tonic-gate NDI_CONFIG | NDI_DEVI_PERSIST | NDI_NO_EVENT |
17477c478bd9Sstevel@tonic-gate NDI_DRV_CONF_REPROBE);
17487c478bd9Sstevel@tonic-gate }
17497c478bd9Sstevel@tonic-gate
17507c478bd9Sstevel@tonic-gate /*
17517c478bd9Sstevel@tonic-gate * Push top_devinfo onto a stack
17527c478bd9Sstevel@tonic-gate *
17537c478bd9Sstevel@tonic-gate * The stack is necessary to avoid recursion, which can overrun
17547c478bd9Sstevel@tonic-gate * the kernel stack.
17557c478bd9Sstevel@tonic-gate */
17567c478bd9Sstevel@tonic-gate PUSH_STACK(dsp, root, off_p);
17577c478bd9Sstevel@tonic-gate
17587c478bd9Sstevel@tonic-gate /*
17597c478bd9Sstevel@tonic-gate * As long as there is a node on the stack, copy the node.
17607c478bd9Sstevel@tonic-gate * di_copynode() is responsible for pushing and popping
17617c478bd9Sstevel@tonic-gate * child and sibling nodes on the stack.
17627c478bd9Sstevel@tonic-gate */
17637c478bd9Sstevel@tonic-gate while (!EMPTY_STACK(dsp)) {
1764b9ccdc5aScth node = TOP_NODE(dsp);
1765b9ccdc5aScth off = di_copynode(node, dsp, st);
17667c478bd9Sstevel@tonic-gate }
17677c478bd9Sstevel@tonic-gate
17687c478bd9Sstevel@tonic-gate /*
17697c478bd9Sstevel@tonic-gate * Free the stack structure
17707c478bd9Sstevel@tonic-gate */
17717c478bd9Sstevel@tonic-gate kmem_free(dsp, sizeof (struct di_stack));
17727c478bd9Sstevel@tonic-gate
17737c478bd9Sstevel@tonic-gate return (off);
17747c478bd9Sstevel@tonic-gate }
17757c478bd9Sstevel@tonic-gate
17767c478bd9Sstevel@tonic-gate /*
17777c478bd9Sstevel@tonic-gate * This is the core function, which copies all data associated with a single
17787c478bd9Sstevel@tonic-gate * node into the snapshot. The amount of information is determined by the
17797c478bd9Sstevel@tonic-gate * ioctl command.
17807c478bd9Sstevel@tonic-gate */
17817c478bd9Sstevel@tonic-gate static di_off_t
di_copynode(struct dev_info * node,struct di_stack * dsp,struct di_state * st)1782b9ccdc5aScth di_copynode(struct dev_info *node, struct di_stack *dsp, struct di_state *st)
17837c478bd9Sstevel@tonic-gate {
17847c478bd9Sstevel@tonic-gate di_off_t off;
17857c478bd9Sstevel@tonic-gate struct di_node *me;
178626947304SEvan Yan size_t size;
178726947304SEvan Yan struct dev_info *n;
17887c478bd9Sstevel@tonic-gate
17891aef0e11Sjg dcmn_err2((CE_CONT, "di_copynode: depth = %x\n", dsp->depth));
1790b9ccdc5aScth ASSERT((node != NULL) && (node == TOP_NODE(dsp)));
17917c478bd9Sstevel@tonic-gate
17927c478bd9Sstevel@tonic-gate /*
17937c478bd9Sstevel@tonic-gate * check memory usage, and fix offsets accordingly.
17947c478bd9Sstevel@tonic-gate */
1795b9ccdc5aScth size = sizeof (struct di_node);
1796b9ccdc5aScth *(TOP_OFFSET(dsp)) = off = di_checkmem(st, *(TOP_OFFSET(dsp)), size);
17977c478bd9Sstevel@tonic-gate me = DI_NODE(di_mem_addr(st, off));
1798b9ccdc5aScth me->self = off;
1799b9ccdc5aScth off += size;
18007c478bd9Sstevel@tonic-gate
18017c478bd9Sstevel@tonic-gate dcmn_err((CE_CONT, "copy node %s, instance #%d, at offset 0x%x\n",
18027c478bd9Sstevel@tonic-gate node->devi_node_name, node->devi_instance, off));
18037c478bd9Sstevel@tonic-gate
18047c478bd9Sstevel@tonic-gate /*
18057c478bd9Sstevel@tonic-gate * Node parameters:
18067c478bd9Sstevel@tonic-gate * self -- offset of current node within snapshot
18077c478bd9Sstevel@tonic-gate * nodeid -- pointer to PROM node (tri-valued)
18087c478bd9Sstevel@tonic-gate * state -- hot plugging device state
1809b9ccdc5aScth * node_state -- devinfo node state
18107c478bd9Sstevel@tonic-gate */
18117c478bd9Sstevel@tonic-gate me->instance = node->devi_instance;
18127c478bd9Sstevel@tonic-gate me->nodeid = node->devi_nodeid;
18137c478bd9Sstevel@tonic-gate me->node_class = node->devi_node_class;
18147c478bd9Sstevel@tonic-gate me->attributes = node->devi_node_attributes;
18157c478bd9Sstevel@tonic-gate me->state = node->devi_state;
18163e2676e0Svikram me->flags = node->devi_flags;
18177c478bd9Sstevel@tonic-gate me->node_state = node->devi_node_state;
18188c4f8890Srs135747 me->next_vhci = 0; /* Filled up by build_vhci_list. */
18198c4f8890Srs135747 me->top_phci = 0; /* Filled up by build_phci_list. */
18208c4f8890Srs135747 me->next_phci = 0; /* Filled up by build_phci_list. */
18218c4f8890Srs135747 me->multipath_component = MULTIPATH_COMPONENT_NONE; /* set default. */
1822093aa5c8SToomas Soome me->user_private_data = 0;
18237c478bd9Sstevel@tonic-gate
18247c478bd9Sstevel@tonic-gate /*
18257c478bd9Sstevel@tonic-gate * Get parent's offset in snapshot from the stack
18267c478bd9Sstevel@tonic-gate * and store it in the current node
18277c478bd9Sstevel@tonic-gate */
18287c478bd9Sstevel@tonic-gate if (dsp->depth > 1) {
18297c478bd9Sstevel@tonic-gate me->parent = *(PARENT_OFFSET(dsp));
18307c478bd9Sstevel@tonic-gate }
18317c478bd9Sstevel@tonic-gate
18327c478bd9Sstevel@tonic-gate /*
18337c478bd9Sstevel@tonic-gate * Save the offset of this di_node in a hash table.
18347c478bd9Sstevel@tonic-gate * This is used later to resolve references to this
18357c478bd9Sstevel@tonic-gate * dip from other parts of the tree (per-driver list,
18367c478bd9Sstevel@tonic-gate * multipathing linkages, layered usage linkages).
18377c478bd9Sstevel@tonic-gate * The key used for the hash table is derived from
18387c478bd9Sstevel@tonic-gate * information in the dip.
18397c478bd9Sstevel@tonic-gate */
18407c478bd9Sstevel@tonic-gate di_register_dip(st, (dev_info_t *)node, me->self);
18417c478bd9Sstevel@tonic-gate
18427c478bd9Sstevel@tonic-gate #ifdef DEVID_COMPATIBILITY
18437c478bd9Sstevel@tonic-gate /* check for devid as property marker */
1844602ca9eaScth if (node->devi_devid_str) {
18457c478bd9Sstevel@tonic-gate ddi_devid_t devid;
18467c478bd9Sstevel@tonic-gate
18477c478bd9Sstevel@tonic-gate /*
1848602ca9eaScth * The devid is now represented as a property. For
1849602ca9eaScth * compatibility with di_devid() interface in libdevinfo we
1850602ca9eaScth * must return it as a binary structure in the snapshot. When
1851602ca9eaScth * (if) di_devid() is removed from libdevinfo then the code
1852602ca9eaScth * related to DEVID_COMPATIBILITY can be removed.
18537c478bd9Sstevel@tonic-gate */
1854602ca9eaScth if (ddi_devid_str_decode(node->devi_devid_str, &devid, NULL) ==
18557c478bd9Sstevel@tonic-gate DDI_SUCCESS) {
1856b9ccdc5aScth size = ddi_devid_sizeof(devid);
1857b9ccdc5aScth off = di_checkmem(st, off, size);
18587c478bd9Sstevel@tonic-gate me->devid = off;
1859b9ccdc5aScth bcopy(devid, di_mem_addr(st, off), size);
1860b9ccdc5aScth off += size;
18617c478bd9Sstevel@tonic-gate ddi_devid_free(devid);
18627c478bd9Sstevel@tonic-gate }
18637c478bd9Sstevel@tonic-gate }
18647c478bd9Sstevel@tonic-gate #endif /* DEVID_COMPATIBILITY */
18657c478bd9Sstevel@tonic-gate
18667c478bd9Sstevel@tonic-gate if (node->devi_node_name) {
1867b9ccdc5aScth size = strlen(node->devi_node_name) + 1;
1868b9ccdc5aScth me->node_name = off = di_checkmem(st, off, size);
18697c478bd9Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), node->devi_node_name);
1870b9ccdc5aScth off += size;
18717c478bd9Sstevel@tonic-gate }
18727c478bd9Sstevel@tonic-gate
18737c478bd9Sstevel@tonic-gate if (node->devi_compat_names && (node->devi_compat_length > 1)) {
1874b9ccdc5aScth size = node->devi_compat_length;
1875b9ccdc5aScth me->compat_names = off = di_checkmem(st, off, size);
1876b9ccdc5aScth me->compat_length = (int)size;
1877b9ccdc5aScth bcopy(node->devi_compat_names, di_mem_addr(st, off), size);
1878b9ccdc5aScth off += size;
18797c478bd9Sstevel@tonic-gate }
18807c478bd9Sstevel@tonic-gate
18817c478bd9Sstevel@tonic-gate if (node->devi_addr) {
1882b9ccdc5aScth size = strlen(node->devi_addr) + 1;
1883b9ccdc5aScth me->address = off = di_checkmem(st, off, size);
18847c478bd9Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), node->devi_addr);
1885b9ccdc5aScth off += size;
18867c478bd9Sstevel@tonic-gate }
18877c478bd9Sstevel@tonic-gate
18887c478bd9Sstevel@tonic-gate if (node->devi_binding_name) {
1889b9ccdc5aScth size = strlen(node->devi_binding_name) + 1;
1890b9ccdc5aScth me->bind_name = off = di_checkmem(st, off, size);
18917c478bd9Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), node->devi_binding_name);
1892b9ccdc5aScth off += size;
18937c478bd9Sstevel@tonic-gate }
18947c478bd9Sstevel@tonic-gate
18957c478bd9Sstevel@tonic-gate me->drv_major = node->devi_major;
18967c478bd9Sstevel@tonic-gate
18977c478bd9Sstevel@tonic-gate /*
18987c478bd9Sstevel@tonic-gate * If the dip is BOUND, set the next pointer of the
18997c478bd9Sstevel@tonic-gate * per-instance list to -1, indicating that it is yet to be resolved.
19007c478bd9Sstevel@tonic-gate * This will be resolved later in snap_driver_list().
19017c478bd9Sstevel@tonic-gate */
19027c478bd9Sstevel@tonic-gate if (me->drv_major != -1) {
19037c478bd9Sstevel@tonic-gate me->next = -1;
19047c478bd9Sstevel@tonic-gate } else {
19057c478bd9Sstevel@tonic-gate me->next = 0;
19067c478bd9Sstevel@tonic-gate }
19077c478bd9Sstevel@tonic-gate
19087c478bd9Sstevel@tonic-gate /*
19097c478bd9Sstevel@tonic-gate * An optimization to skip mutex_enter when not needed.
19107c478bd9Sstevel@tonic-gate */
191126947304SEvan Yan if (!((DINFOMINOR | DINFOPROP | DINFOPATH | DINFOHP) & st->command)) {
19127c478bd9Sstevel@tonic-gate goto priv_data;
19137c478bd9Sstevel@tonic-gate }
19147c478bd9Sstevel@tonic-gate
19157c478bd9Sstevel@tonic-gate /*
1916b9ccdc5aScth * LOCKING: We already have an active ndi_devi_enter to gather the
1917b9ccdc5aScth * minor data, and we will take devi_lock to gather properties as
1918b9ccdc5aScth * needed off di_getprop.
19197c478bd9Sstevel@tonic-gate */
19207c478bd9Sstevel@tonic-gate if (!(DINFOMINOR & st->command)) {
19217c478bd9Sstevel@tonic-gate goto path;
19227c478bd9Sstevel@tonic-gate }
19237c478bd9Sstevel@tonic-gate
1924b9ccdc5aScth ASSERT(DEVI_BUSY_OWNED(node));
19257c478bd9Sstevel@tonic-gate if (node->devi_minor) { /* minor data */
1926b9ccdc5aScth me->minor_data = off;
19277c478bd9Sstevel@tonic-gate off = di_getmdata(node->devi_minor, &me->minor_data,
19287c478bd9Sstevel@tonic-gate me->self, st);
19297c478bd9Sstevel@tonic-gate }
19307c478bd9Sstevel@tonic-gate
19317c478bd9Sstevel@tonic-gate path:
19327c478bd9Sstevel@tonic-gate if (!(DINFOPATH & st->command)) {
19337c478bd9Sstevel@tonic-gate goto property;
19347c478bd9Sstevel@tonic-gate }
19357c478bd9Sstevel@tonic-gate
19368c4f8890Srs135747 if (MDI_VHCI(node)) {
19378c4f8890Srs135747 me->multipath_component = MULTIPATH_COMPONENT_VHCI;
19388c4f8890Srs135747 }
19398c4f8890Srs135747
19407c478bd9Sstevel@tonic-gate if (MDI_CLIENT(node)) {
19418c4f8890Srs135747 me->multipath_component = MULTIPATH_COMPONENT_CLIENT;
1942b9ccdc5aScth me->multipath_client = off;
19437c478bd9Sstevel@tonic-gate off = di_getpath_data((dev_info_t *)node, &me->multipath_client,
19447c478bd9Sstevel@tonic-gate me->self, st, 1);
19457c478bd9Sstevel@tonic-gate dcmn_err((CE_WARN, "me->multipath_client = %x for node %p "
19467c478bd9Sstevel@tonic-gate "component type = %d. off=%d",
19477c478bd9Sstevel@tonic-gate me->multipath_client,
19487c478bd9Sstevel@tonic-gate (void *)node, node->devi_mdi_component, off));
19497c478bd9Sstevel@tonic-gate }
19507c478bd9Sstevel@tonic-gate
19517c478bd9Sstevel@tonic-gate if (MDI_PHCI(node)) {
19528c4f8890Srs135747 me->multipath_component = MULTIPATH_COMPONENT_PHCI;
1953b9ccdc5aScth me->multipath_phci = off;
19547c478bd9Sstevel@tonic-gate off = di_getpath_data((dev_info_t *)node, &me->multipath_phci,
19557c478bd9Sstevel@tonic-gate me->self, st, 0);
19567c478bd9Sstevel@tonic-gate dcmn_err((CE_WARN, "me->multipath_phci = %x for node %p "
19577c478bd9Sstevel@tonic-gate "component type = %d. off=%d",
19587c478bd9Sstevel@tonic-gate me->multipath_phci,
19597c478bd9Sstevel@tonic-gate (void *)node, node->devi_mdi_component, off));
19607c478bd9Sstevel@tonic-gate }
19617c478bd9Sstevel@tonic-gate
19627c478bd9Sstevel@tonic-gate property:
19637c478bd9Sstevel@tonic-gate if (!(DINFOPROP & st->command)) {
196426947304SEvan Yan goto hotplug_data;
19657c478bd9Sstevel@tonic-gate }
19667c478bd9Sstevel@tonic-gate
19677c478bd9Sstevel@tonic-gate if (node->devi_drv_prop_ptr) { /* driver property list */
1968b9ccdc5aScth me->drv_prop = off;
1969b9ccdc5aScth off = di_getprop(DI_PROP_DRV_LIST, &node->devi_drv_prop_ptr,
1970b9ccdc5aScth &me->drv_prop, st, node);
19717c478bd9Sstevel@tonic-gate }
19727c478bd9Sstevel@tonic-gate
19737c478bd9Sstevel@tonic-gate if (node->devi_sys_prop_ptr) { /* system property list */
1974b9ccdc5aScth me->sys_prop = off;
1975b9ccdc5aScth off = di_getprop(DI_PROP_SYS_LIST, &node->devi_sys_prop_ptr,
1976b9ccdc5aScth &me->sys_prop, st, node);
19777c478bd9Sstevel@tonic-gate }
19787c478bd9Sstevel@tonic-gate
19797c478bd9Sstevel@tonic-gate if (node->devi_hw_prop_ptr) { /* hardware property list */
1980b9ccdc5aScth me->hw_prop = off;
1981b9ccdc5aScth off = di_getprop(DI_PROP_HW_LIST, &node->devi_hw_prop_ptr,
1982b9ccdc5aScth &me->hw_prop, st, node);
19837c478bd9Sstevel@tonic-gate }
19847c478bd9Sstevel@tonic-gate
19857c478bd9Sstevel@tonic-gate if (node->devi_global_prop_list == NULL) {
19867c478bd9Sstevel@tonic-gate me->glob_prop = (di_off_t)-1; /* not global property */
19877c478bd9Sstevel@tonic-gate } else {
19887c478bd9Sstevel@tonic-gate /*
19897c478bd9Sstevel@tonic-gate * Make copy of global property list if this devinfo refers
19907c478bd9Sstevel@tonic-gate * global properties different from what's on the devnames
19917c478bd9Sstevel@tonic-gate * array. It can happen if there has been a forced
1992bbf21555SRichard Lowe * driver.conf update. See update_drv(8).
19937c478bd9Sstevel@tonic-gate */
19947c478bd9Sstevel@tonic-gate ASSERT(me->drv_major != -1);
19957c478bd9Sstevel@tonic-gate if (node->devi_global_prop_list !=
19967c478bd9Sstevel@tonic-gate devnamesp[me->drv_major].dn_global_prop_ptr) {
1997b9ccdc5aScth me->glob_prop = off;
1998b9ccdc5aScth off = di_getprop(DI_PROP_GLB_LIST,
1999b9ccdc5aScth &node->devi_global_prop_list->prop_list,
2000b9ccdc5aScth &me->glob_prop, st, node);
20017c478bd9Sstevel@tonic-gate }
20027c478bd9Sstevel@tonic-gate }
20037c478bd9Sstevel@tonic-gate
200426947304SEvan Yan hotplug_data:
200526947304SEvan Yan if (!(DINFOHP & st->command)) {
200626947304SEvan Yan goto priv_data;
200726947304SEvan Yan }
200826947304SEvan Yan
200926947304SEvan Yan if (node->devi_hp_hdlp) { /* hotplug data */
201026947304SEvan Yan me->hp_data = off;
201126947304SEvan Yan off = di_gethpdata(node->devi_hp_hdlp, &me->hp_data, st);
201226947304SEvan Yan }
201326947304SEvan Yan
20147c478bd9Sstevel@tonic-gate priv_data:
20157c478bd9Sstevel@tonic-gate if (!(DINFOPRIVDATA & st->command)) {
20167c478bd9Sstevel@tonic-gate goto pm_info;
20177c478bd9Sstevel@tonic-gate }
20187c478bd9Sstevel@tonic-gate
20197c478bd9Sstevel@tonic-gate if (ddi_get_parent_data((dev_info_t *)node) != NULL) {
2020b9ccdc5aScth me->parent_data = off;
20217c478bd9Sstevel@tonic-gate off = di_getppdata(node, &me->parent_data, st);
20227c478bd9Sstevel@tonic-gate }
20237c478bd9Sstevel@tonic-gate
20247c478bd9Sstevel@tonic-gate if (ddi_get_driver_private((dev_info_t *)node) != NULL) {
2025b9ccdc5aScth me->driver_data = off;
20267c478bd9Sstevel@tonic-gate off = di_getdpdata(node, &me->driver_data, st);
20277c478bd9Sstevel@tonic-gate }
20287c478bd9Sstevel@tonic-gate
20297c478bd9Sstevel@tonic-gate pm_info: /* NOT implemented */
20307c478bd9Sstevel@tonic-gate
20317c478bd9Sstevel@tonic-gate subtree:
2032b9ccdc5aScth /* keep the stack aligned */
2033b9ccdc5aScth off = DI_ALIGN(off);
2034b9ccdc5aScth
20357c478bd9Sstevel@tonic-gate if (!(DINFOSUBTREE & st->command)) {
20367c478bd9Sstevel@tonic-gate POP_STACK(dsp);
2037b9ccdc5aScth return (off);
20387c478bd9Sstevel@tonic-gate }
20397c478bd9Sstevel@tonic-gate
20407c478bd9Sstevel@tonic-gate child:
20417c478bd9Sstevel@tonic-gate /*
2042027021c7SChris Horne * If there is a visible child--push child onto stack.
2043027021c7SChris Horne * Hold the parent (me) busy while doing so.
20447c478bd9Sstevel@tonic-gate */
2045027021c7SChris Horne if ((n = node->devi_child) != NULL) {
2046027021c7SChris Horne /* skip hidden nodes */
2047027021c7SChris Horne while (n && ndi_dev_is_hidden_node((dev_info_t *)n))
2048027021c7SChris Horne n = n->devi_sibling;
2049027021c7SChris Horne if (n) {
2050b9ccdc5aScth me->child = off;
2051027021c7SChris Horne PUSH_STACK(dsp, n, &me->child);
20527c478bd9Sstevel@tonic-gate return (me->child);
20537c478bd9Sstevel@tonic-gate }
2054027021c7SChris Horne }
20557c478bd9Sstevel@tonic-gate
20567c478bd9Sstevel@tonic-gate sibling:
20577c478bd9Sstevel@tonic-gate /*
2058027021c7SChris Horne * Done with any child nodes, unroll the stack till a visible
2059027021c7SChris Horne * sibling of a parent node is found or root node is reached.
20607c478bd9Sstevel@tonic-gate */
20617c478bd9Sstevel@tonic-gate POP_STACK(dsp);
2062027021c7SChris Horne while (!EMPTY_STACK(dsp)) {
2063027021c7SChris Horne if ((n = node->devi_sibling) != NULL) {
2064027021c7SChris Horne /* skip hidden nodes */
2065027021c7SChris Horne while (n && ndi_dev_is_hidden_node((dev_info_t *)n))
2066027021c7SChris Horne n = n->devi_sibling;
2067027021c7SChris Horne if (n) {
2068027021c7SChris Horne me->sibling = DI_ALIGN(off);
2069027021c7SChris Horne PUSH_STACK(dsp, n, &me->sibling);
2070027021c7SChris Horne return (me->sibling);
2071027021c7SChris Horne }
2072027021c7SChris Horne }
20737c478bd9Sstevel@tonic-gate node = TOP_NODE(dsp);
20747c478bd9Sstevel@tonic-gate me = DI_NODE(di_mem_addr(st, *(TOP_OFFSET(dsp))));
20757c478bd9Sstevel@tonic-gate POP_STACK(dsp);
20767c478bd9Sstevel@tonic-gate }
20777c478bd9Sstevel@tonic-gate
20787c478bd9Sstevel@tonic-gate /*
20797c478bd9Sstevel@tonic-gate * DONE with all nodes
20807c478bd9Sstevel@tonic-gate */
2081b9ccdc5aScth return (off);
20827c478bd9Sstevel@tonic-gate }
20837c478bd9Sstevel@tonic-gate
20847c478bd9Sstevel@tonic-gate static i_lnode_t *
i_lnode_alloc(int modid)20857c478bd9Sstevel@tonic-gate i_lnode_alloc(int modid)
20867c478bd9Sstevel@tonic-gate {
20877c478bd9Sstevel@tonic-gate i_lnode_t *i_lnode;
20887c478bd9Sstevel@tonic-gate
20897c478bd9Sstevel@tonic-gate i_lnode = kmem_zalloc(sizeof (i_lnode_t), KM_SLEEP);
20907c478bd9Sstevel@tonic-gate
20917c478bd9Sstevel@tonic-gate ASSERT(modid != -1);
20927c478bd9Sstevel@tonic-gate i_lnode->modid = modid;
20937c478bd9Sstevel@tonic-gate
20947c478bd9Sstevel@tonic-gate return (i_lnode);
20957c478bd9Sstevel@tonic-gate }
20967c478bd9Sstevel@tonic-gate
20977c478bd9Sstevel@tonic-gate static void
i_lnode_free(i_lnode_t * i_lnode)20987c478bd9Sstevel@tonic-gate i_lnode_free(i_lnode_t *i_lnode)
20997c478bd9Sstevel@tonic-gate {
21007c478bd9Sstevel@tonic-gate kmem_free(i_lnode, sizeof (i_lnode_t));
21017c478bd9Sstevel@tonic-gate }
21027c478bd9Sstevel@tonic-gate
21037c478bd9Sstevel@tonic-gate static void
i_lnode_check_free(i_lnode_t * i_lnode)21047c478bd9Sstevel@tonic-gate i_lnode_check_free(i_lnode_t *i_lnode)
21057c478bd9Sstevel@tonic-gate {
21067c478bd9Sstevel@tonic-gate /* This lnode and its dip must have been snapshotted */
21077c478bd9Sstevel@tonic-gate ASSERT(i_lnode->self > 0);
21087c478bd9Sstevel@tonic-gate ASSERT(i_lnode->di_node->self > 0);
21097c478bd9Sstevel@tonic-gate
21107c478bd9Sstevel@tonic-gate /* at least 1 link (in or out) must exist for this lnode */
21117c478bd9Sstevel@tonic-gate ASSERT(i_lnode->link_in || i_lnode->link_out);
21127c478bd9Sstevel@tonic-gate
21137c478bd9Sstevel@tonic-gate i_lnode_free(i_lnode);
21147c478bd9Sstevel@tonic-gate }
21157c478bd9Sstevel@tonic-gate
21167c478bd9Sstevel@tonic-gate static i_link_t *
i_link_alloc(int spec_type)21177c478bd9Sstevel@tonic-gate i_link_alloc(int spec_type)
21187c478bd9Sstevel@tonic-gate {
21197c478bd9Sstevel@tonic-gate i_link_t *i_link;
21207c478bd9Sstevel@tonic-gate
21217c478bd9Sstevel@tonic-gate i_link = kmem_zalloc(sizeof (i_link_t), KM_SLEEP);
21227c478bd9Sstevel@tonic-gate i_link->spec_type = spec_type;
21237c478bd9Sstevel@tonic-gate
21247c478bd9Sstevel@tonic-gate return (i_link);
21257c478bd9Sstevel@tonic-gate }
21267c478bd9Sstevel@tonic-gate
21277c478bd9Sstevel@tonic-gate static void
i_link_check_free(i_link_t * i_link)21287c478bd9Sstevel@tonic-gate i_link_check_free(i_link_t *i_link)
21297c478bd9Sstevel@tonic-gate {
21307c478bd9Sstevel@tonic-gate /* This link must have been snapshotted */
21317c478bd9Sstevel@tonic-gate ASSERT(i_link->self > 0);
21327c478bd9Sstevel@tonic-gate
21337c478bd9Sstevel@tonic-gate /* Both endpoint lnodes must exist for this link */
21347c478bd9Sstevel@tonic-gate ASSERT(i_link->src_lnode);
21357c478bd9Sstevel@tonic-gate ASSERT(i_link->tgt_lnode);
21367c478bd9Sstevel@tonic-gate
21377c478bd9Sstevel@tonic-gate kmem_free(i_link, sizeof (i_link_t));
21387c478bd9Sstevel@tonic-gate }
21397c478bd9Sstevel@tonic-gate
21407c478bd9Sstevel@tonic-gate /*ARGSUSED*/
21417c478bd9Sstevel@tonic-gate static uint_t
i_lnode_hashfunc(void * arg,mod_hash_key_t key)21427c478bd9Sstevel@tonic-gate i_lnode_hashfunc(void *arg, mod_hash_key_t key)
21437c478bd9Sstevel@tonic-gate {
21447c478bd9Sstevel@tonic-gate i_lnode_t *i_lnode = (i_lnode_t *)key;
21457c478bd9Sstevel@tonic-gate struct di_node *ptr;
21467c478bd9Sstevel@tonic-gate dev_t dev;
21477c478bd9Sstevel@tonic-gate
21487c478bd9Sstevel@tonic-gate dev = i_lnode->devt;
21497c478bd9Sstevel@tonic-gate if (dev != DDI_DEV_T_NONE)
21507c478bd9Sstevel@tonic-gate return (i_lnode->modid + getminor(dev) + getmajor(dev));
21517c478bd9Sstevel@tonic-gate
21527c478bd9Sstevel@tonic-gate ptr = i_lnode->di_node;
21537c478bd9Sstevel@tonic-gate ASSERT(ptr->self > 0);
21547c478bd9Sstevel@tonic-gate if (ptr) {
21557c478bd9Sstevel@tonic-gate uintptr_t k = (uintptr_t)ptr;
21567c478bd9Sstevel@tonic-gate k >>= (int)highbit(sizeof (struct di_node));
21577c478bd9Sstevel@tonic-gate return ((uint_t)k);
21587c478bd9Sstevel@tonic-gate }
21597c478bd9Sstevel@tonic-gate
21607c478bd9Sstevel@tonic-gate return (i_lnode->modid);
21617c478bd9Sstevel@tonic-gate }
21627c478bd9Sstevel@tonic-gate
21637c478bd9Sstevel@tonic-gate static int
i_lnode_cmp(void * arg1,void * arg2)21647c478bd9Sstevel@tonic-gate i_lnode_cmp(void *arg1, void *arg2)
21657c478bd9Sstevel@tonic-gate {
21667c478bd9Sstevel@tonic-gate i_lnode_t *i_lnode1 = (i_lnode_t *)arg1;
21677c478bd9Sstevel@tonic-gate i_lnode_t *i_lnode2 = (i_lnode_t *)arg2;
21687c478bd9Sstevel@tonic-gate
21697c478bd9Sstevel@tonic-gate if (i_lnode1->modid != i_lnode2->modid) {
21707c478bd9Sstevel@tonic-gate return ((i_lnode1->modid < i_lnode2->modid) ? -1 : 1);
21717c478bd9Sstevel@tonic-gate }
21727c478bd9Sstevel@tonic-gate
21737c478bd9Sstevel@tonic-gate if (i_lnode1->di_node != i_lnode2->di_node)
21747c478bd9Sstevel@tonic-gate return ((i_lnode1->di_node < i_lnode2->di_node) ? -1 : 1);
21757c478bd9Sstevel@tonic-gate
21767c478bd9Sstevel@tonic-gate if (i_lnode1->devt != i_lnode2->devt)
21777c478bd9Sstevel@tonic-gate return ((i_lnode1->devt < i_lnode2->devt) ? -1 : 1);
21787c478bd9Sstevel@tonic-gate
21797c478bd9Sstevel@tonic-gate return (0);
21807c478bd9Sstevel@tonic-gate }
21817c478bd9Sstevel@tonic-gate
21827c478bd9Sstevel@tonic-gate /*
21837c478bd9Sstevel@tonic-gate * An lnode represents a {dip, dev_t} tuple. A link represents a
21847c478bd9Sstevel@tonic-gate * {src_lnode, tgt_lnode, spec_type} tuple.
21857c478bd9Sstevel@tonic-gate * The following callback assumes that LDI framework ref-counts the
21867c478bd9Sstevel@tonic-gate * src_dip and tgt_dip while invoking this callback.
21877c478bd9Sstevel@tonic-gate */
21887c478bd9Sstevel@tonic-gate static int
di_ldi_callback(const ldi_usage_t * ldi_usage,void * arg)21897c478bd9Sstevel@tonic-gate di_ldi_callback(const ldi_usage_t *ldi_usage, void *arg)
21907c478bd9Sstevel@tonic-gate {
21917c478bd9Sstevel@tonic-gate struct di_state *st = (struct di_state *)arg;
21927c478bd9Sstevel@tonic-gate i_lnode_t *src_lnode, *tgt_lnode, *i_lnode;
21937c478bd9Sstevel@tonic-gate i_link_t **i_link_next, *i_link;
21947c478bd9Sstevel@tonic-gate di_off_t soff, toff;
21957c478bd9Sstevel@tonic-gate mod_hash_val_t nodep = NULL;
21967c478bd9Sstevel@tonic-gate int res;
21977c478bd9Sstevel@tonic-gate
21987c478bd9Sstevel@tonic-gate /*
21997c478bd9Sstevel@tonic-gate * if the source or target of this device usage information doesn't
2200b9ccdc5aScth * correspond to a device node then we don't report it via
22017c478bd9Sstevel@tonic-gate * libdevinfo so return.
22027c478bd9Sstevel@tonic-gate */
22037c478bd9Sstevel@tonic-gate if ((ldi_usage->src_dip == NULL) || (ldi_usage->tgt_dip == NULL))
22047c478bd9Sstevel@tonic-gate return (LDI_USAGE_CONTINUE);
22057c478bd9Sstevel@tonic-gate
22067c478bd9Sstevel@tonic-gate ASSERT(e_ddi_devi_holdcnt(ldi_usage->src_dip));
22077c478bd9Sstevel@tonic-gate ASSERT(e_ddi_devi_holdcnt(ldi_usage->tgt_dip));
22087c478bd9Sstevel@tonic-gate
22097c478bd9Sstevel@tonic-gate /*
22107c478bd9Sstevel@tonic-gate * Skip the ldi_usage if either src or tgt dip is not in the
22117c478bd9Sstevel@tonic-gate * snapshot. This saves us from pruning bad lnodes/links later.
22127c478bd9Sstevel@tonic-gate */
22137c478bd9Sstevel@tonic-gate if (di_dip_find(st, ldi_usage->src_dip, &soff) != 0)
22147c478bd9Sstevel@tonic-gate return (LDI_USAGE_CONTINUE);
22157c478bd9Sstevel@tonic-gate if (di_dip_find(st, ldi_usage->tgt_dip, &toff) != 0)
22167c478bd9Sstevel@tonic-gate return (LDI_USAGE_CONTINUE);
22177c478bd9Sstevel@tonic-gate
22187c478bd9Sstevel@tonic-gate ASSERT(soff > 0);
22197c478bd9Sstevel@tonic-gate ASSERT(toff > 0);
22207c478bd9Sstevel@tonic-gate
22217c478bd9Sstevel@tonic-gate /*
22227c478bd9Sstevel@tonic-gate * allocate an i_lnode and add it to the lnode hash
22237c478bd9Sstevel@tonic-gate * if it is not already present. For this particular
22247c478bd9Sstevel@tonic-gate * link the lnode is a source, but it may
22257c478bd9Sstevel@tonic-gate * participate as tgt or src in any number of layered
22267c478bd9Sstevel@tonic-gate * operations - so it may already be in the hash.
22277c478bd9Sstevel@tonic-gate */
22287c478bd9Sstevel@tonic-gate i_lnode = i_lnode_alloc(ldi_usage->src_modid);
2229b9ccdc5aScth i_lnode->di_node = DI_NODE(di_mem_addr(st, soff));
22307c478bd9Sstevel@tonic-gate i_lnode->devt = ldi_usage->src_devt;
22317c478bd9Sstevel@tonic-gate
22327c478bd9Sstevel@tonic-gate res = mod_hash_find(st->lnode_hash, i_lnode, &nodep);
22337c478bd9Sstevel@tonic-gate if (res == MH_ERR_NOTFOUND) {
22347c478bd9Sstevel@tonic-gate /*
22357c478bd9Sstevel@tonic-gate * new i_lnode
22367c478bd9Sstevel@tonic-gate * add it to the hash and increment the lnode count
22377c478bd9Sstevel@tonic-gate */
22387c478bd9Sstevel@tonic-gate res = mod_hash_insert(st->lnode_hash, i_lnode, i_lnode);
22397c478bd9Sstevel@tonic-gate ASSERT(res == 0);
22407c478bd9Sstevel@tonic-gate st->lnode_count++;
22417c478bd9Sstevel@tonic-gate src_lnode = i_lnode;
22427c478bd9Sstevel@tonic-gate } else {
22437c478bd9Sstevel@tonic-gate /* this i_lnode already exists in the lnode_hash */
22447c478bd9Sstevel@tonic-gate i_lnode_free(i_lnode);
22457c478bd9Sstevel@tonic-gate src_lnode = (i_lnode_t *)nodep;
22467c478bd9Sstevel@tonic-gate }
22477c478bd9Sstevel@tonic-gate
22487c478bd9Sstevel@tonic-gate /*
22497c478bd9Sstevel@tonic-gate * allocate a tgt i_lnode and add it to the lnode hash
22507c478bd9Sstevel@tonic-gate */
22517c478bd9Sstevel@tonic-gate i_lnode = i_lnode_alloc(ldi_usage->tgt_modid);
2252b9ccdc5aScth i_lnode->di_node = DI_NODE(di_mem_addr(st, toff));
22537c478bd9Sstevel@tonic-gate i_lnode->devt = ldi_usage->tgt_devt;
22547c478bd9Sstevel@tonic-gate
22557c478bd9Sstevel@tonic-gate res = mod_hash_find(st->lnode_hash, i_lnode, &nodep);
22567c478bd9Sstevel@tonic-gate if (res == MH_ERR_NOTFOUND) {
22577c478bd9Sstevel@tonic-gate /*
22587c478bd9Sstevel@tonic-gate * new i_lnode
22597c478bd9Sstevel@tonic-gate * add it to the hash and increment the lnode count
22607c478bd9Sstevel@tonic-gate */
22617c478bd9Sstevel@tonic-gate res = mod_hash_insert(st->lnode_hash, i_lnode, i_lnode);
22627c478bd9Sstevel@tonic-gate ASSERT(res == 0);
22637c478bd9Sstevel@tonic-gate st->lnode_count++;
22647c478bd9Sstevel@tonic-gate tgt_lnode = i_lnode;
22657c478bd9Sstevel@tonic-gate } else {
22667c478bd9Sstevel@tonic-gate /* this i_lnode already exists in the lnode_hash */
22677c478bd9Sstevel@tonic-gate i_lnode_free(i_lnode);
22687c478bd9Sstevel@tonic-gate tgt_lnode = (i_lnode_t *)nodep;
22697c478bd9Sstevel@tonic-gate }
22707c478bd9Sstevel@tonic-gate
22717c478bd9Sstevel@tonic-gate /*
22727c478bd9Sstevel@tonic-gate * allocate a i_link
22737c478bd9Sstevel@tonic-gate */
22747c478bd9Sstevel@tonic-gate i_link = i_link_alloc(ldi_usage->tgt_spec_type);
22757c478bd9Sstevel@tonic-gate i_link->src_lnode = src_lnode;
22767c478bd9Sstevel@tonic-gate i_link->tgt_lnode = tgt_lnode;
22777c478bd9Sstevel@tonic-gate
22787c478bd9Sstevel@tonic-gate /*
22797c478bd9Sstevel@tonic-gate * add this link onto the src i_lnodes outbound i_link list
22807c478bd9Sstevel@tonic-gate */
22817c478bd9Sstevel@tonic-gate i_link_next = &(src_lnode->link_out);
22827c478bd9Sstevel@tonic-gate while (*i_link_next != NULL) {
22837c478bd9Sstevel@tonic-gate if ((i_lnode_cmp(tgt_lnode, (*i_link_next)->tgt_lnode) == 0) &&
22847c478bd9Sstevel@tonic-gate (i_link->spec_type == (*i_link_next)->spec_type)) {
22857c478bd9Sstevel@tonic-gate /* this link already exists */
22867c478bd9Sstevel@tonic-gate kmem_free(i_link, sizeof (i_link_t));
22877c478bd9Sstevel@tonic-gate return (LDI_USAGE_CONTINUE);
22887c478bd9Sstevel@tonic-gate }
22897c478bd9Sstevel@tonic-gate i_link_next = &((*i_link_next)->src_link_next);
22907c478bd9Sstevel@tonic-gate }
22917c478bd9Sstevel@tonic-gate *i_link_next = i_link;
22927c478bd9Sstevel@tonic-gate
22937c478bd9Sstevel@tonic-gate /*
22947c478bd9Sstevel@tonic-gate * add this link onto the tgt i_lnodes inbound i_link list
22957c478bd9Sstevel@tonic-gate */
22967c478bd9Sstevel@tonic-gate i_link_next = &(tgt_lnode->link_in);
22977c478bd9Sstevel@tonic-gate while (*i_link_next != NULL) {
22987c478bd9Sstevel@tonic-gate ASSERT(i_lnode_cmp(src_lnode, (*i_link_next)->src_lnode) != 0);
22997c478bd9Sstevel@tonic-gate i_link_next = &((*i_link_next)->tgt_link_next);
23007c478bd9Sstevel@tonic-gate }
23017c478bd9Sstevel@tonic-gate *i_link_next = i_link;
23027c478bd9Sstevel@tonic-gate
23037c478bd9Sstevel@tonic-gate /*
23047c478bd9Sstevel@tonic-gate * add this i_link to the link hash
23057c478bd9Sstevel@tonic-gate */
23067c478bd9Sstevel@tonic-gate res = mod_hash_insert(st->link_hash, i_link, i_link);
23077c478bd9Sstevel@tonic-gate ASSERT(res == 0);
23087c478bd9Sstevel@tonic-gate st->link_count++;
23097c478bd9Sstevel@tonic-gate
23107c478bd9Sstevel@tonic-gate return (LDI_USAGE_CONTINUE);
23117c478bd9Sstevel@tonic-gate }
23127c478bd9Sstevel@tonic-gate
23137c478bd9Sstevel@tonic-gate struct i_layer_data {
23147c478bd9Sstevel@tonic-gate struct di_state *st;
23157c478bd9Sstevel@tonic-gate int lnode_count;
23167c478bd9Sstevel@tonic-gate int link_count;
23177c478bd9Sstevel@tonic-gate di_off_t lnode_off;
23187c478bd9Sstevel@tonic-gate di_off_t link_off;
23197c478bd9Sstevel@tonic-gate };
23207c478bd9Sstevel@tonic-gate
23217c478bd9Sstevel@tonic-gate /*ARGSUSED*/
23227c478bd9Sstevel@tonic-gate static uint_t
i_link_walker(mod_hash_key_t key,mod_hash_val_t * val,void * arg)23237c478bd9Sstevel@tonic-gate i_link_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
23247c478bd9Sstevel@tonic-gate {
23257c478bd9Sstevel@tonic-gate i_link_t *i_link = (i_link_t *)key;
23267c478bd9Sstevel@tonic-gate struct i_layer_data *data = arg;
23277c478bd9Sstevel@tonic-gate struct di_link *me;
23287c478bd9Sstevel@tonic-gate struct di_lnode *melnode;
23297c478bd9Sstevel@tonic-gate struct di_node *medinode;
23307c478bd9Sstevel@tonic-gate
23317c478bd9Sstevel@tonic-gate ASSERT(i_link->self == 0);
23327c478bd9Sstevel@tonic-gate
23337c478bd9Sstevel@tonic-gate i_link->self = data->link_off +
23347c478bd9Sstevel@tonic-gate (data->link_count * sizeof (struct di_link));
23357c478bd9Sstevel@tonic-gate data->link_count++;
23367c478bd9Sstevel@tonic-gate
23377c478bd9Sstevel@tonic-gate ASSERT(data->link_off > 0 && data->link_count > 0);
23387c478bd9Sstevel@tonic-gate ASSERT(data->lnode_count == data->st->lnode_count); /* lnodes done */
23397c478bd9Sstevel@tonic-gate ASSERT(data->link_count <= data->st->link_count);
23407c478bd9Sstevel@tonic-gate
23417c478bd9Sstevel@tonic-gate /* fill in fields for the di_link snapshot */
2342b9ccdc5aScth me = DI_LINK(di_mem_addr(data->st, i_link->self));
23437c478bd9Sstevel@tonic-gate me->self = i_link->self;
23447c478bd9Sstevel@tonic-gate me->spec_type = i_link->spec_type;
23457c478bd9Sstevel@tonic-gate
23467c478bd9Sstevel@tonic-gate /*
23477c478bd9Sstevel@tonic-gate * The src_lnode and tgt_lnode i_lnode_t for this i_link_t
23487c478bd9Sstevel@tonic-gate * are created during the LDI table walk. Since we are
23497c478bd9Sstevel@tonic-gate * walking the link hash, the lnode hash has already been
23507c478bd9Sstevel@tonic-gate * walked and the lnodes have been snapshotted. Save lnode
23517c478bd9Sstevel@tonic-gate * offsets.
23527c478bd9Sstevel@tonic-gate */
23537c478bd9Sstevel@tonic-gate me->src_lnode = i_link->src_lnode->self;
23547c478bd9Sstevel@tonic-gate me->tgt_lnode = i_link->tgt_lnode->self;
23557c478bd9Sstevel@tonic-gate
23567c478bd9Sstevel@tonic-gate /*
23577c478bd9Sstevel@tonic-gate * Save this link's offset in the src_lnode snapshot's link_out
23587c478bd9Sstevel@tonic-gate * field
23597c478bd9Sstevel@tonic-gate */
2360b9ccdc5aScth melnode = DI_LNODE(di_mem_addr(data->st, me->src_lnode));
23617c478bd9Sstevel@tonic-gate me->src_link_next = melnode->link_out;
23627c478bd9Sstevel@tonic-gate melnode->link_out = me->self;
23637c478bd9Sstevel@tonic-gate
23647c478bd9Sstevel@tonic-gate /*
23657c478bd9Sstevel@tonic-gate * Put this link on the tgt_lnode's link_in field
23667c478bd9Sstevel@tonic-gate */
2367b9ccdc5aScth melnode = DI_LNODE(di_mem_addr(data->st, me->tgt_lnode));
23687c478bd9Sstevel@tonic-gate me->tgt_link_next = melnode->link_in;
23697c478bd9Sstevel@tonic-gate melnode->link_in = me->self;
23707c478bd9Sstevel@tonic-gate
23717c478bd9Sstevel@tonic-gate /*
23727c478bd9Sstevel@tonic-gate * An i_lnode_t is only created if the corresponding dip exists
23737c478bd9Sstevel@tonic-gate * in the snapshot. A pointer to the di_node is saved in the
23747c478bd9Sstevel@tonic-gate * i_lnode_t when it is allocated. For this link, get the di_node
23757c478bd9Sstevel@tonic-gate * for the source lnode. Then put the link on the di_node's list
23767c478bd9Sstevel@tonic-gate * of src links
23777c478bd9Sstevel@tonic-gate */
23787c478bd9Sstevel@tonic-gate medinode = i_link->src_lnode->di_node;
23797c478bd9Sstevel@tonic-gate me->src_node_next = medinode->src_links;
23807c478bd9Sstevel@tonic-gate medinode->src_links = me->self;
23817c478bd9Sstevel@tonic-gate
23827c478bd9Sstevel@tonic-gate /*
23837c478bd9Sstevel@tonic-gate * Put this link on the tgt_links list of the target
23847c478bd9Sstevel@tonic-gate * dip.
23857c478bd9Sstevel@tonic-gate */
23867c478bd9Sstevel@tonic-gate medinode = i_link->tgt_lnode->di_node;
23877c478bd9Sstevel@tonic-gate me->tgt_node_next = medinode->tgt_links;
23887c478bd9Sstevel@tonic-gate medinode->tgt_links = me->self;
23897c478bd9Sstevel@tonic-gate
23907c478bd9Sstevel@tonic-gate return (MH_WALK_CONTINUE);
23917c478bd9Sstevel@tonic-gate }
23927c478bd9Sstevel@tonic-gate
23937c478bd9Sstevel@tonic-gate /*ARGSUSED*/
23947c478bd9Sstevel@tonic-gate static uint_t
i_lnode_walker(mod_hash_key_t key,mod_hash_val_t * val,void * arg)23957c478bd9Sstevel@tonic-gate i_lnode_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
23967c478bd9Sstevel@tonic-gate {
23977c478bd9Sstevel@tonic-gate i_lnode_t *i_lnode = (i_lnode_t *)key;
23987c478bd9Sstevel@tonic-gate struct i_layer_data *data = arg;
23997c478bd9Sstevel@tonic-gate struct di_lnode *me;
24007c478bd9Sstevel@tonic-gate struct di_node *medinode;
24017c478bd9Sstevel@tonic-gate
24027c478bd9Sstevel@tonic-gate ASSERT(i_lnode->self == 0);
24037c478bd9Sstevel@tonic-gate
24047c478bd9Sstevel@tonic-gate i_lnode->self = data->lnode_off +
24057c478bd9Sstevel@tonic-gate (data->lnode_count * sizeof (struct di_lnode));
24067c478bd9Sstevel@tonic-gate data->lnode_count++;
24077c478bd9Sstevel@tonic-gate
24087c478bd9Sstevel@tonic-gate ASSERT(data->lnode_off > 0 && data->lnode_count > 0);
24097c478bd9Sstevel@tonic-gate ASSERT(data->link_count == 0); /* links not done yet */
24107c478bd9Sstevel@tonic-gate ASSERT(data->lnode_count <= data->st->lnode_count);
24117c478bd9Sstevel@tonic-gate
24127c478bd9Sstevel@tonic-gate /* fill in fields for the di_lnode snapshot */
2413b9ccdc5aScth me = DI_LNODE(di_mem_addr(data->st, i_lnode->self));
24147c478bd9Sstevel@tonic-gate me->self = i_lnode->self;
24157c478bd9Sstevel@tonic-gate
24167c478bd9Sstevel@tonic-gate if (i_lnode->devt == DDI_DEV_T_NONE) {
2417a204de77Scth me->dev_major = DDI_MAJOR_T_NONE;
2418a204de77Scth me->dev_minor = DDI_MAJOR_T_NONE;
24197c478bd9Sstevel@tonic-gate } else {
24207c478bd9Sstevel@tonic-gate me->dev_major = getmajor(i_lnode->devt);
24217c478bd9Sstevel@tonic-gate me->dev_minor = getminor(i_lnode->devt);
24227c478bd9Sstevel@tonic-gate }
24237c478bd9Sstevel@tonic-gate
24247c478bd9Sstevel@tonic-gate /*
24257c478bd9Sstevel@tonic-gate * The dip corresponding to this lnode must exist in
24267c478bd9Sstevel@tonic-gate * the snapshot or we wouldn't have created the i_lnode_t
24277c478bd9Sstevel@tonic-gate * during LDI walk. Save the offset of the dip.
24287c478bd9Sstevel@tonic-gate */
24297c478bd9Sstevel@tonic-gate ASSERT(i_lnode->di_node && i_lnode->di_node->self > 0);
24307c478bd9Sstevel@tonic-gate me->node = i_lnode->di_node->self;
24317c478bd9Sstevel@tonic-gate
24327c478bd9Sstevel@tonic-gate /*
24337c478bd9Sstevel@tonic-gate * There must be at least one link in or out of this lnode
24347c478bd9Sstevel@tonic-gate * or we wouldn't have created it. These fields will be set
24357c478bd9Sstevel@tonic-gate * during the link hash walk.
24367c478bd9Sstevel@tonic-gate */
24377c478bd9Sstevel@tonic-gate ASSERT((i_lnode->link_in != NULL) || (i_lnode->link_out != NULL));
24387c478bd9Sstevel@tonic-gate
24397c478bd9Sstevel@tonic-gate /*
24407c478bd9Sstevel@tonic-gate * set the offset of the devinfo node associated with this
24417c478bd9Sstevel@tonic-gate * lnode. Also update the node_next next pointer. this pointer
24427c478bd9Sstevel@tonic-gate * is set if there are multiple lnodes associated with the same
24437c478bd9Sstevel@tonic-gate * devinfo node. (could occure when multiple minor nodes
24447c478bd9Sstevel@tonic-gate * are open for one device, etc.)
24457c478bd9Sstevel@tonic-gate */
24467c478bd9Sstevel@tonic-gate medinode = i_lnode->di_node;
24477c478bd9Sstevel@tonic-gate me->node_next = medinode->lnodes;
24487c478bd9Sstevel@tonic-gate medinode->lnodes = me->self;
24497c478bd9Sstevel@tonic-gate
24507c478bd9Sstevel@tonic-gate return (MH_WALK_CONTINUE);
24517c478bd9Sstevel@tonic-gate }
24527c478bd9Sstevel@tonic-gate
24537c478bd9Sstevel@tonic-gate static di_off_t
di_getlink_data(di_off_t off,struct di_state * st)24547c478bd9Sstevel@tonic-gate di_getlink_data(di_off_t off, struct di_state *st)
24557c478bd9Sstevel@tonic-gate {
24567c478bd9Sstevel@tonic-gate struct i_layer_data data = {0};
24577c478bd9Sstevel@tonic-gate size_t size;
24587c478bd9Sstevel@tonic-gate
24597c478bd9Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_copylyr: off = %x\n", off));
24607c478bd9Sstevel@tonic-gate
24617c478bd9Sstevel@tonic-gate st->lnode_hash = mod_hash_create_extended("di_lnode_hash", 32,
24627c478bd9Sstevel@tonic-gate mod_hash_null_keydtor, (void (*)(mod_hash_val_t))i_lnode_check_free,
24637c478bd9Sstevel@tonic-gate i_lnode_hashfunc, NULL, i_lnode_cmp, KM_SLEEP);
24647c478bd9Sstevel@tonic-gate
24657c478bd9Sstevel@tonic-gate st->link_hash = mod_hash_create_ptrhash("di_link_hash", 32,
24667c478bd9Sstevel@tonic-gate (void (*)(mod_hash_val_t))i_link_check_free, sizeof (i_link_t));
24677c478bd9Sstevel@tonic-gate
24687c478bd9Sstevel@tonic-gate /* get driver layering information */
24697c478bd9Sstevel@tonic-gate (void) ldi_usage_walker(st, di_ldi_callback);
24707c478bd9Sstevel@tonic-gate
24717c478bd9Sstevel@tonic-gate /* check if there is any link data to include in the snapshot */
24727c478bd9Sstevel@tonic-gate if (st->lnode_count == 0) {
24737c478bd9Sstevel@tonic-gate ASSERT(st->link_count == 0);
24747c478bd9Sstevel@tonic-gate goto out;
24757c478bd9Sstevel@tonic-gate }
24767c478bd9Sstevel@tonic-gate
24777c478bd9Sstevel@tonic-gate ASSERT(st->link_count != 0);
24787c478bd9Sstevel@tonic-gate
24797c478bd9Sstevel@tonic-gate /* get a pointer to snapshot memory for all the di_lnodes */
24807c478bd9Sstevel@tonic-gate size = sizeof (struct di_lnode) * st->lnode_count;
24817c478bd9Sstevel@tonic-gate data.lnode_off = off = di_checkmem(st, off, size);
2482b9ccdc5aScth off += size;
24837c478bd9Sstevel@tonic-gate
24847c478bd9Sstevel@tonic-gate /* get a pointer to snapshot memory for all the di_links */
24857c478bd9Sstevel@tonic-gate size = sizeof (struct di_link) * st->link_count;
24867c478bd9Sstevel@tonic-gate data.link_off = off = di_checkmem(st, off, size);
2487b9ccdc5aScth off += size;
24887c478bd9Sstevel@tonic-gate
24897c478bd9Sstevel@tonic-gate data.lnode_count = data.link_count = 0;
24907c478bd9Sstevel@tonic-gate data.st = st;
24917c478bd9Sstevel@tonic-gate
24927c478bd9Sstevel@tonic-gate /*
24937c478bd9Sstevel@tonic-gate * We have lnodes and links that will go into the
24947c478bd9Sstevel@tonic-gate * snapshot, so let's walk the respective hashes
24957c478bd9Sstevel@tonic-gate * and snapshot them. The various linkages are
24967c478bd9Sstevel@tonic-gate * also set up during the walk.
24977c478bd9Sstevel@tonic-gate */
24987c478bd9Sstevel@tonic-gate mod_hash_walk(st->lnode_hash, i_lnode_walker, (void *)&data);
24997c478bd9Sstevel@tonic-gate ASSERT(data.lnode_count == st->lnode_count);
25007c478bd9Sstevel@tonic-gate
25017c478bd9Sstevel@tonic-gate mod_hash_walk(st->link_hash, i_link_walker, (void *)&data);
25027c478bd9Sstevel@tonic-gate ASSERT(data.link_count == st->link_count);
25037c478bd9Sstevel@tonic-gate
25047c478bd9Sstevel@tonic-gate out:
25057c478bd9Sstevel@tonic-gate /* free up the i_lnodes and i_links used to create the snapshot */
25067c478bd9Sstevel@tonic-gate mod_hash_destroy_hash(st->lnode_hash);
25077c478bd9Sstevel@tonic-gate mod_hash_destroy_hash(st->link_hash);
25087c478bd9Sstevel@tonic-gate st->lnode_count = 0;
25097c478bd9Sstevel@tonic-gate st->link_count = 0;
25107c478bd9Sstevel@tonic-gate
25117c478bd9Sstevel@tonic-gate return (off);
25127c478bd9Sstevel@tonic-gate }
25137c478bd9Sstevel@tonic-gate
25147c478bd9Sstevel@tonic-gate
25157c478bd9Sstevel@tonic-gate /*
25167c478bd9Sstevel@tonic-gate * Copy all minor data nodes attached to a devinfo node into the snapshot.
2517b9ccdc5aScth * It is called from di_copynode with active ndi_devi_enter to protect
2518b9ccdc5aScth * the list of minor nodes.
25197c478bd9Sstevel@tonic-gate */
25207c478bd9Sstevel@tonic-gate static di_off_t
di_getmdata(struct ddi_minor_data * mnode,di_off_t * off_p,di_off_t node,struct di_state * st)25217c478bd9Sstevel@tonic-gate di_getmdata(struct ddi_minor_data *mnode, di_off_t *off_p, di_off_t node,
25227c478bd9Sstevel@tonic-gate struct di_state *st)
25237c478bd9Sstevel@tonic-gate {
25247c478bd9Sstevel@tonic-gate di_off_t off;
25257c478bd9Sstevel@tonic-gate struct di_minor *me;
2526b9ccdc5aScth size_t size;
25277c478bd9Sstevel@tonic-gate
25287c478bd9Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_getmdata:\n"));
25297c478bd9Sstevel@tonic-gate
25307c478bd9Sstevel@tonic-gate /*
25317c478bd9Sstevel@tonic-gate * check memory first
25327c478bd9Sstevel@tonic-gate */
25337c478bd9Sstevel@tonic-gate off = di_checkmem(st, *off_p, sizeof (struct di_minor));
25347c478bd9Sstevel@tonic-gate *off_p = off;
25357c478bd9Sstevel@tonic-gate
25367c478bd9Sstevel@tonic-gate do {
2537b9ccdc5aScth me = DI_MINOR(di_mem_addr(st, off));
25387c478bd9Sstevel@tonic-gate me->self = off;
25397c478bd9Sstevel@tonic-gate me->type = mnode->type;
25407c478bd9Sstevel@tonic-gate me->node = node;
2541093aa5c8SToomas Soome me->user_private_data = 0;
25427c478bd9Sstevel@tonic-gate
2543b9ccdc5aScth off += sizeof (struct di_minor);
25447c478bd9Sstevel@tonic-gate
25457c478bd9Sstevel@tonic-gate /*
25467c478bd9Sstevel@tonic-gate * Split dev_t to major/minor, so it works for
25477c478bd9Sstevel@tonic-gate * both ILP32 and LP64 model
25487c478bd9Sstevel@tonic-gate */
25497c478bd9Sstevel@tonic-gate me->dev_major = getmajor(mnode->ddm_dev);
25507c478bd9Sstevel@tonic-gate me->dev_minor = getminor(mnode->ddm_dev);
25517c478bd9Sstevel@tonic-gate me->spec_type = mnode->ddm_spec_type;
25527c478bd9Sstevel@tonic-gate
25537c478bd9Sstevel@tonic-gate if (mnode->ddm_name) {
2554b9ccdc5aScth size = strlen(mnode->ddm_name) + 1;
2555b9ccdc5aScth me->name = off = di_checkmem(st, off, size);
25567c478bd9Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), mnode->ddm_name);
2557b9ccdc5aScth off += size;
25587c478bd9Sstevel@tonic-gate }
25597c478bd9Sstevel@tonic-gate
25607c478bd9Sstevel@tonic-gate if (mnode->ddm_node_type) {
2561b9ccdc5aScth size = strlen(mnode->ddm_node_type) + 1;
2562b9ccdc5aScth me->node_type = off = di_checkmem(st, off, size);
25637c478bd9Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off),
25647c478bd9Sstevel@tonic-gate mnode->ddm_node_type);
2565b9ccdc5aScth off += size;
25667c478bd9Sstevel@tonic-gate }
25677c478bd9Sstevel@tonic-gate
25687c478bd9Sstevel@tonic-gate off = di_checkmem(st, off, sizeof (struct di_minor));
25697c478bd9Sstevel@tonic-gate me->next = off;
25707c478bd9Sstevel@tonic-gate mnode = mnode->next;
25717c478bd9Sstevel@tonic-gate } while (mnode);
25727c478bd9Sstevel@tonic-gate
25737c478bd9Sstevel@tonic-gate me->next = 0;
25747c478bd9Sstevel@tonic-gate
25757c478bd9Sstevel@tonic-gate return (off);
25767c478bd9Sstevel@tonic-gate }
25777c478bd9Sstevel@tonic-gate
25787c478bd9Sstevel@tonic-gate /*
25797c478bd9Sstevel@tonic-gate * di_register_dip(), di_find_dip(): The dip must be protected
25807c478bd9Sstevel@tonic-gate * from deallocation when using these routines - this can either
25817c478bd9Sstevel@tonic-gate * be a reference count, a busy hold or a per-driver lock.
25827c478bd9Sstevel@tonic-gate */
25837c478bd9Sstevel@tonic-gate
25847c478bd9Sstevel@tonic-gate static void
di_register_dip(struct di_state * st,dev_info_t * dip,di_off_t off)25857c478bd9Sstevel@tonic-gate di_register_dip(struct di_state *st, dev_info_t *dip, di_off_t off)
25867c478bd9Sstevel@tonic-gate {
25877c478bd9Sstevel@tonic-gate struct dev_info *node = DEVI(dip);
25887c478bd9Sstevel@tonic-gate struct di_key *key = kmem_zalloc(sizeof (*key), KM_SLEEP);
25897c478bd9Sstevel@tonic-gate struct di_dkey *dk;
25907c478bd9Sstevel@tonic-gate
25917c478bd9Sstevel@tonic-gate ASSERT(dip);
25927c478bd9Sstevel@tonic-gate ASSERT(off > 0);
25937c478bd9Sstevel@tonic-gate
25947c478bd9Sstevel@tonic-gate key->k_type = DI_DKEY;
25957c478bd9Sstevel@tonic-gate dk = &(key->k_u.dkey);
25967c478bd9Sstevel@tonic-gate
25977c478bd9Sstevel@tonic-gate dk->dk_dip = dip;
25987c478bd9Sstevel@tonic-gate dk->dk_major = node->devi_major;
25997c478bd9Sstevel@tonic-gate dk->dk_inst = node->devi_instance;
26007c478bd9Sstevel@tonic-gate dk->dk_nodeid = node->devi_nodeid;
26017c478bd9Sstevel@tonic-gate
26027c478bd9Sstevel@tonic-gate if (mod_hash_insert(st->reg_dip_hash, (mod_hash_key_t)key,
26037c478bd9Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)off) != 0) {
26047c478bd9Sstevel@tonic-gate panic(
26057c478bd9Sstevel@tonic-gate "duplicate devinfo (%p) registered during device "
26067c478bd9Sstevel@tonic-gate "tree walk", (void *)dip);
26077c478bd9Sstevel@tonic-gate }
26087c478bd9Sstevel@tonic-gate }
26097c478bd9Sstevel@tonic-gate
26107c478bd9Sstevel@tonic-gate
26117c478bd9Sstevel@tonic-gate static int
di_dip_find(struct di_state * st,dev_info_t * dip,di_off_t * off_p)26127c478bd9Sstevel@tonic-gate di_dip_find(struct di_state *st, dev_info_t *dip, di_off_t *off_p)
26137c478bd9Sstevel@tonic-gate {
26147c478bd9Sstevel@tonic-gate /*
26157c478bd9Sstevel@tonic-gate * uintptr_t must be used because it matches the size of void *;
26167c478bd9Sstevel@tonic-gate * mod_hash expects clients to place results into pointer-size
26177c478bd9Sstevel@tonic-gate * containers; since di_off_t is always a 32-bit offset, alignment
26187c478bd9Sstevel@tonic-gate * would otherwise be broken on 64-bit kernels.
26197c478bd9Sstevel@tonic-gate */
26207c478bd9Sstevel@tonic-gate uintptr_t offset;
26217c478bd9Sstevel@tonic-gate struct di_key key = {0};
26227c478bd9Sstevel@tonic-gate struct di_dkey *dk;
26237c478bd9Sstevel@tonic-gate
26247c478bd9Sstevel@tonic-gate ASSERT(st->reg_dip_hash);
26257c478bd9Sstevel@tonic-gate ASSERT(dip);
26267c478bd9Sstevel@tonic-gate ASSERT(off_p);
26277c478bd9Sstevel@tonic-gate
26287c478bd9Sstevel@tonic-gate
26297c478bd9Sstevel@tonic-gate key.k_type = DI_DKEY;
26307c478bd9Sstevel@tonic-gate dk = &(key.k_u.dkey);
26317c478bd9Sstevel@tonic-gate
26327c478bd9Sstevel@tonic-gate dk->dk_dip = dip;
26337c478bd9Sstevel@tonic-gate dk->dk_major = DEVI(dip)->devi_major;
26347c478bd9Sstevel@tonic-gate dk->dk_inst = DEVI(dip)->devi_instance;
26357c478bd9Sstevel@tonic-gate dk->dk_nodeid = DEVI(dip)->devi_nodeid;
26367c478bd9Sstevel@tonic-gate
26377c478bd9Sstevel@tonic-gate if (mod_hash_find(st->reg_dip_hash, (mod_hash_key_t)&key,
26387c478bd9Sstevel@tonic-gate (mod_hash_val_t *)&offset) == 0) {
26397c478bd9Sstevel@tonic-gate *off_p = (di_off_t)offset;
26407c478bd9Sstevel@tonic-gate return (0);
26417c478bd9Sstevel@tonic-gate } else {
26427c478bd9Sstevel@tonic-gate return (-1);
26437c478bd9Sstevel@tonic-gate }
26447c478bd9Sstevel@tonic-gate }
26457c478bd9Sstevel@tonic-gate
26467c478bd9Sstevel@tonic-gate /*
26477c478bd9Sstevel@tonic-gate * di_register_pip(), di_find_pip(): The pip must be protected from deallocation
26487c478bd9Sstevel@tonic-gate * when using these routines. The caller must do this by protecting the
26497c478bd9Sstevel@tonic-gate * client(or phci)<->pip linkage while traversing the list and then holding the
26507c478bd9Sstevel@tonic-gate * pip when it is found in the list.
26517c478bd9Sstevel@tonic-gate */
26527c478bd9Sstevel@tonic-gate
26537c478bd9Sstevel@tonic-gate static void
di_register_pip(struct di_state * st,mdi_pathinfo_t * pip,di_off_t off)26547c478bd9Sstevel@tonic-gate di_register_pip(struct di_state *st, mdi_pathinfo_t *pip, di_off_t off)
26557c478bd9Sstevel@tonic-gate {
26567c478bd9Sstevel@tonic-gate struct di_key *key = kmem_zalloc(sizeof (*key), KM_SLEEP);
26577c478bd9Sstevel@tonic-gate char *path_addr;
26587c478bd9Sstevel@tonic-gate struct di_pkey *pk;
26597c478bd9Sstevel@tonic-gate
26607c478bd9Sstevel@tonic-gate ASSERT(pip);
26617c478bd9Sstevel@tonic-gate ASSERT(off > 0);
26627c478bd9Sstevel@tonic-gate
26637c478bd9Sstevel@tonic-gate key->k_type = DI_PKEY;
26647c478bd9Sstevel@tonic-gate pk = &(key->k_u.pkey);
26657c478bd9Sstevel@tonic-gate
26667c478bd9Sstevel@tonic-gate pk->pk_pip = pip;
26677c478bd9Sstevel@tonic-gate path_addr = mdi_pi_get_addr(pip);
26687c478bd9Sstevel@tonic-gate if (path_addr)
26697c478bd9Sstevel@tonic-gate pk->pk_path_addr = i_ddi_strdup(path_addr, KM_SLEEP);
26707c478bd9Sstevel@tonic-gate pk->pk_client = mdi_pi_get_client(pip);
26717c478bd9Sstevel@tonic-gate pk->pk_phci = mdi_pi_get_phci(pip);
26727c478bd9Sstevel@tonic-gate
26737c478bd9Sstevel@tonic-gate if (mod_hash_insert(st->reg_pip_hash, (mod_hash_key_t)key,
26747c478bd9Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)off) != 0) {
26757c478bd9Sstevel@tonic-gate panic(
26767c478bd9Sstevel@tonic-gate "duplicate pathinfo (%p) registered during device "
26777c478bd9Sstevel@tonic-gate "tree walk", (void *)pip);
26787c478bd9Sstevel@tonic-gate }
26797c478bd9Sstevel@tonic-gate }
26807c478bd9Sstevel@tonic-gate
26817c478bd9Sstevel@tonic-gate /*
26827c478bd9Sstevel@tonic-gate * As with di_register_pip, the caller must hold or lock the pip
26837c478bd9Sstevel@tonic-gate */
26847c478bd9Sstevel@tonic-gate static int
di_pip_find(struct di_state * st,mdi_pathinfo_t * pip,di_off_t * off_p)26857c478bd9Sstevel@tonic-gate di_pip_find(struct di_state *st, mdi_pathinfo_t *pip, di_off_t *off_p)
26867c478bd9Sstevel@tonic-gate {
26877c478bd9Sstevel@tonic-gate /*
26887c478bd9Sstevel@tonic-gate * uintptr_t must be used because it matches the size of void *;
26897c478bd9Sstevel@tonic-gate * mod_hash expects clients to place results into pointer-size
26907c478bd9Sstevel@tonic-gate * containers; since di_off_t is always a 32-bit offset, alignment
26917c478bd9Sstevel@tonic-gate * would otherwise be broken on 64-bit kernels.
26927c478bd9Sstevel@tonic-gate */
26937c478bd9Sstevel@tonic-gate uintptr_t offset;
26947c478bd9Sstevel@tonic-gate struct di_key key = {0};
26957c478bd9Sstevel@tonic-gate struct di_pkey *pk;
26967c478bd9Sstevel@tonic-gate
26977c478bd9Sstevel@tonic-gate ASSERT(st->reg_pip_hash);
26987c478bd9Sstevel@tonic-gate ASSERT(off_p);
26997c478bd9Sstevel@tonic-gate
27007c478bd9Sstevel@tonic-gate if (pip == NULL) {
27017c478bd9Sstevel@tonic-gate *off_p = 0;
27027c478bd9Sstevel@tonic-gate return (0);
27037c478bd9Sstevel@tonic-gate }
27047c478bd9Sstevel@tonic-gate
27057c478bd9Sstevel@tonic-gate key.k_type = DI_PKEY;
27067c478bd9Sstevel@tonic-gate pk = &(key.k_u.pkey);
27077c478bd9Sstevel@tonic-gate
27087c478bd9Sstevel@tonic-gate pk->pk_pip = pip;
27097c478bd9Sstevel@tonic-gate pk->pk_path_addr = mdi_pi_get_addr(pip);
27107c478bd9Sstevel@tonic-gate pk->pk_client = mdi_pi_get_client(pip);
27117c478bd9Sstevel@tonic-gate pk->pk_phci = mdi_pi_get_phci(pip);
27127c478bd9Sstevel@tonic-gate
27137c478bd9Sstevel@tonic-gate if (mod_hash_find(st->reg_pip_hash, (mod_hash_key_t)&key,
27147c478bd9Sstevel@tonic-gate (mod_hash_val_t *)&offset) == 0) {
27157c478bd9Sstevel@tonic-gate *off_p = (di_off_t)offset;
27167c478bd9Sstevel@tonic-gate return (0);
27177c478bd9Sstevel@tonic-gate } else {
27187c478bd9Sstevel@tonic-gate return (-1);
27197c478bd9Sstevel@tonic-gate }
27207c478bd9Sstevel@tonic-gate }
27217c478bd9Sstevel@tonic-gate
27227c478bd9Sstevel@tonic-gate static di_path_state_t
path_state_convert(mdi_pathinfo_state_t st)27237c478bd9Sstevel@tonic-gate path_state_convert(mdi_pathinfo_state_t st)
27247c478bd9Sstevel@tonic-gate {
27257c478bd9Sstevel@tonic-gate switch (st) {
27267c478bd9Sstevel@tonic-gate case MDI_PATHINFO_STATE_ONLINE:
27277c478bd9Sstevel@tonic-gate return (DI_PATH_STATE_ONLINE);
27287c478bd9Sstevel@tonic-gate case MDI_PATHINFO_STATE_STANDBY:
27297c478bd9Sstevel@tonic-gate return (DI_PATH_STATE_STANDBY);
27307c478bd9Sstevel@tonic-gate case MDI_PATHINFO_STATE_OFFLINE:
27317c478bd9Sstevel@tonic-gate return (DI_PATH_STATE_OFFLINE);
27327c478bd9Sstevel@tonic-gate case MDI_PATHINFO_STATE_FAULT:
27337c478bd9Sstevel@tonic-gate return (DI_PATH_STATE_FAULT);
27347c478bd9Sstevel@tonic-gate default:
27357c478bd9Sstevel@tonic-gate return (DI_PATH_STATE_UNKNOWN);
27367c478bd9Sstevel@tonic-gate }
27377c478bd9Sstevel@tonic-gate }
27387c478bd9Sstevel@tonic-gate
27394c06356bSdh142964 static uint_t
path_flags_convert(uint_t pi_path_flags)27404c06356bSdh142964 path_flags_convert(uint_t pi_path_flags)
27414c06356bSdh142964 {
27424c06356bSdh142964 uint_t di_path_flags = 0;
27434c06356bSdh142964
27444c06356bSdh142964 /* MDI_PATHINFO_FLAGS_HIDDEN nodes not in snapshot */
27454c06356bSdh142964
27464c06356bSdh142964 if (pi_path_flags & MDI_PATHINFO_FLAGS_DEVICE_REMOVED)
27474c06356bSdh142964 di_path_flags |= DI_PATH_FLAGS_DEVICE_REMOVED;
27484c06356bSdh142964
27494c06356bSdh142964 return (di_path_flags);
27504c06356bSdh142964 }
27514c06356bSdh142964
27527c478bd9Sstevel@tonic-gate
27537c478bd9Sstevel@tonic-gate static di_off_t
di_path_getprop(mdi_pathinfo_t * pip,di_off_t * off_p,struct di_state * st)2754b9ccdc5aScth di_path_getprop(mdi_pathinfo_t *pip, di_off_t *off_p,
27557c478bd9Sstevel@tonic-gate struct di_state *st)
27567c478bd9Sstevel@tonic-gate {
27577c478bd9Sstevel@tonic-gate nvpair_t *prop = NULL;
27587c478bd9Sstevel@tonic-gate struct di_path_prop *me;
2759b9ccdc5aScth int off;
2760b9ccdc5aScth size_t size;
2761b9ccdc5aScth char *str;
2762b9ccdc5aScth uchar_t *buf;
2763b9ccdc5aScth uint_t nelems;
27647c478bd9Sstevel@tonic-gate
2765b9ccdc5aScth off = *off_p;
27667c478bd9Sstevel@tonic-gate if (mdi_pi_get_next_prop(pip, NULL) == NULL) {
27677c478bd9Sstevel@tonic-gate *off_p = 0;
27687c478bd9Sstevel@tonic-gate return (off);
27697c478bd9Sstevel@tonic-gate }
27707c478bd9Sstevel@tonic-gate
27717c478bd9Sstevel@tonic-gate off = di_checkmem(st, off, sizeof (struct di_path_prop));
27727c478bd9Sstevel@tonic-gate *off_p = off;
27737c478bd9Sstevel@tonic-gate
27747c478bd9Sstevel@tonic-gate while (prop = mdi_pi_get_next_prop(pip, prop)) {
2775b9ccdc5aScth me = DI_PATHPROP(di_mem_addr(st, off));
27767c478bd9Sstevel@tonic-gate me->self = off;
27777c478bd9Sstevel@tonic-gate off += sizeof (struct di_path_prop);
27787c478bd9Sstevel@tonic-gate
27797c478bd9Sstevel@tonic-gate /*
27807c478bd9Sstevel@tonic-gate * property name
27817c478bd9Sstevel@tonic-gate */
2782b9ccdc5aScth size = strlen(nvpair_name(prop)) + 1;
2783b9ccdc5aScth me->prop_name = off = di_checkmem(st, off, size);
27847c478bd9Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), nvpair_name(prop));
2785b9ccdc5aScth off += size;
27867c478bd9Sstevel@tonic-gate
27877c478bd9Sstevel@tonic-gate switch (nvpair_type(prop)) {
27887c478bd9Sstevel@tonic-gate case DATA_TYPE_BYTE:
27897c478bd9Sstevel@tonic-gate case DATA_TYPE_INT16:
27907c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT16:
27917c478bd9Sstevel@tonic-gate case DATA_TYPE_INT32:
27927c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT32:
27937c478bd9Sstevel@tonic-gate me->prop_type = DDI_PROP_TYPE_INT;
2794b9ccdc5aScth size = sizeof (int32_t);
2795b9ccdc5aScth off = di_checkmem(st, off, size);
27967c478bd9Sstevel@tonic-gate (void) nvpair_value_int32(prop,
2797b9ccdc5aScth (int32_t *)di_mem_addr(st, off));
27987c478bd9Sstevel@tonic-gate break;
27997c478bd9Sstevel@tonic-gate
28007c478bd9Sstevel@tonic-gate case DATA_TYPE_INT64:
28017c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT64:
28027c478bd9Sstevel@tonic-gate me->prop_type = DDI_PROP_TYPE_INT64;
2803b9ccdc5aScth size = sizeof (int64_t);
2804b9ccdc5aScth off = di_checkmem(st, off, size);
28057c478bd9Sstevel@tonic-gate (void) nvpair_value_int64(prop,
2806b9ccdc5aScth (int64_t *)di_mem_addr(st, off));
28077c478bd9Sstevel@tonic-gate break;
28087c478bd9Sstevel@tonic-gate
28097c478bd9Sstevel@tonic-gate case DATA_TYPE_STRING:
28107c478bd9Sstevel@tonic-gate me->prop_type = DDI_PROP_TYPE_STRING;
2811b9ccdc5aScth (void) nvpair_value_string(prop, &str);
2812b9ccdc5aScth size = strlen(str) + 1;
2813b9ccdc5aScth off = di_checkmem(st, off, size);
28147c478bd9Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), str);
28157c478bd9Sstevel@tonic-gate break;
2816b9ccdc5aScth
28177c478bd9Sstevel@tonic-gate case DATA_TYPE_BYTE_ARRAY:
28187c478bd9Sstevel@tonic-gate case DATA_TYPE_INT16_ARRAY:
28197c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT16_ARRAY:
28207c478bd9Sstevel@tonic-gate case DATA_TYPE_INT32_ARRAY:
28217c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT32_ARRAY:
28227c478bd9Sstevel@tonic-gate case DATA_TYPE_INT64_ARRAY:
28237c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT64_ARRAY:
28247c478bd9Sstevel@tonic-gate me->prop_type = DDI_PROP_TYPE_BYTE;
2825b9ccdc5aScth (void) nvpair_value_byte_array(prop, &buf, &nelems);
2826b9ccdc5aScth size = nelems;
28277c478bd9Sstevel@tonic-gate if (nelems != 0) {
2828b9ccdc5aScth off = di_checkmem(st, off, size);
2829b9ccdc5aScth bcopy(buf, di_mem_addr(st, off), size);
28307c478bd9Sstevel@tonic-gate }
28317c478bd9Sstevel@tonic-gate break;
28327c478bd9Sstevel@tonic-gate
28337c478bd9Sstevel@tonic-gate default: /* Unknown or unhandled type; skip it */
2834b9ccdc5aScth size = 0;
28357c478bd9Sstevel@tonic-gate break;
28367c478bd9Sstevel@tonic-gate }
28377c478bd9Sstevel@tonic-gate
2838b9ccdc5aScth if (size > 0) {
28397c478bd9Sstevel@tonic-gate me->prop_data = off;
28407c478bd9Sstevel@tonic-gate }
28417c478bd9Sstevel@tonic-gate
2842b9ccdc5aScth me->prop_len = (int)size;
2843b9ccdc5aScth off += size;
28447c478bd9Sstevel@tonic-gate
28457c478bd9Sstevel@tonic-gate off = di_checkmem(st, off, sizeof (struct di_path_prop));
28467c478bd9Sstevel@tonic-gate me->prop_next = off;
28477c478bd9Sstevel@tonic-gate }
28487c478bd9Sstevel@tonic-gate
28497c478bd9Sstevel@tonic-gate me->prop_next = 0;
28507c478bd9Sstevel@tonic-gate return (off);
28517c478bd9Sstevel@tonic-gate }
28527c478bd9Sstevel@tonic-gate
28537c478bd9Sstevel@tonic-gate
28547c478bd9Sstevel@tonic-gate static void
di_path_one_endpoint(struct di_path * me,di_off_t noff,di_off_t ** off_pp,int get_client)28557c478bd9Sstevel@tonic-gate di_path_one_endpoint(struct di_path *me, di_off_t noff, di_off_t **off_pp,
28567c478bd9Sstevel@tonic-gate int get_client)
28577c478bd9Sstevel@tonic-gate {
28587c478bd9Sstevel@tonic-gate if (get_client) {
28597c478bd9Sstevel@tonic-gate ASSERT(me->path_client == 0);
28607c478bd9Sstevel@tonic-gate me->path_client = noff;
28617c478bd9Sstevel@tonic-gate ASSERT(me->path_c_link == 0);
28627c478bd9Sstevel@tonic-gate *off_pp = &me->path_c_link;
28637c478bd9Sstevel@tonic-gate me->path_snap_state &=
28647c478bd9Sstevel@tonic-gate ~(DI_PATH_SNAP_NOCLIENT | DI_PATH_SNAP_NOCLINK);
28657c478bd9Sstevel@tonic-gate } else {
28667c478bd9Sstevel@tonic-gate ASSERT(me->path_phci == 0);
28677c478bd9Sstevel@tonic-gate me->path_phci = noff;
28687c478bd9Sstevel@tonic-gate ASSERT(me->path_p_link == 0);
28697c478bd9Sstevel@tonic-gate *off_pp = &me->path_p_link;
28707c478bd9Sstevel@tonic-gate me->path_snap_state &=
28717c478bd9Sstevel@tonic-gate ~(DI_PATH_SNAP_NOPHCI | DI_PATH_SNAP_NOPLINK);
28727c478bd9Sstevel@tonic-gate }
28737c478bd9Sstevel@tonic-gate }
28747c478bd9Sstevel@tonic-gate
28757c478bd9Sstevel@tonic-gate /*
2876b9ccdc5aScth * off_p: pointer to the linkage field. This links pips along the client|phci
28777c478bd9Sstevel@tonic-gate * linkage list.
28787c478bd9Sstevel@tonic-gate * noff : Offset for the endpoint dip snapshot.
28797c478bd9Sstevel@tonic-gate */
28807c478bd9Sstevel@tonic-gate static di_off_t
di_getpath_data(dev_info_t * dip,di_off_t * off_p,di_off_t noff,struct di_state * st,int get_client)2881b9ccdc5aScth di_getpath_data(dev_info_t *dip, di_off_t *off_p, di_off_t noff,
28827c478bd9Sstevel@tonic-gate struct di_state *st, int get_client)
28837c478bd9Sstevel@tonic-gate {
28847c478bd9Sstevel@tonic-gate di_off_t off;
28857c478bd9Sstevel@tonic-gate mdi_pathinfo_t *pip;
28867c478bd9Sstevel@tonic-gate struct di_path *me;
28877c478bd9Sstevel@tonic-gate mdi_pathinfo_t *(*next_pip)(dev_info_t *, mdi_pathinfo_t *);
2888b9ccdc5aScth size_t size;
28897c478bd9Sstevel@tonic-gate
28907c478bd9Sstevel@tonic-gate dcmn_err2((CE_WARN, "di_getpath_data: client = %d", get_client));
28917c478bd9Sstevel@tonic-gate
28927c478bd9Sstevel@tonic-gate /*
28937c478bd9Sstevel@tonic-gate * The naming of the following mdi_xyz() is unfortunately
28947c478bd9Sstevel@tonic-gate * non-intuitive. mdi_get_next_phci_path() follows the
28957c478bd9Sstevel@tonic-gate * client_link i.e. the list of pip's belonging to the
28967c478bd9Sstevel@tonic-gate * given client dip.
28977c478bd9Sstevel@tonic-gate */
28987c478bd9Sstevel@tonic-gate if (get_client)
28997c478bd9Sstevel@tonic-gate next_pip = &mdi_get_next_phci_path;
29007c478bd9Sstevel@tonic-gate else
29017c478bd9Sstevel@tonic-gate next_pip = &mdi_get_next_client_path;
29027c478bd9Sstevel@tonic-gate
2903b9ccdc5aScth off = *off_p;
29047c478bd9Sstevel@tonic-gate
29057c478bd9Sstevel@tonic-gate pip = NULL;
29067c478bd9Sstevel@tonic-gate while (pip = (*next_pip)(dip, pip)) {
29077c478bd9Sstevel@tonic-gate di_off_t stored_offset;
29087c478bd9Sstevel@tonic-gate
29097c478bd9Sstevel@tonic-gate dcmn_err((CE_WARN, "marshalling pip = %p", (void *)pip));
29107c478bd9Sstevel@tonic-gate
29117c478bd9Sstevel@tonic-gate mdi_pi_lock(pip);
29127c478bd9Sstevel@tonic-gate
29134c06356bSdh142964 /* We don't represent hidden paths in the snapshot */
29144c06356bSdh142964 if (mdi_pi_ishidden(pip)) {
29154c06356bSdh142964 dcmn_err((CE_WARN, "hidden, skip"));
29164c06356bSdh142964 mdi_pi_unlock(pip);
29174c06356bSdh142964 continue;
29184c06356bSdh142964 }
29194c06356bSdh142964
29207c478bd9Sstevel@tonic-gate if (di_pip_find(st, pip, &stored_offset) != -1) {
29217c478bd9Sstevel@tonic-gate /*
29227c478bd9Sstevel@tonic-gate * We've already seen this pathinfo node so we need to
29237c478bd9Sstevel@tonic-gate * take care not to snap it again; However, one endpoint
29247c478bd9Sstevel@tonic-gate * and linkage will be set here. The other endpoint
29257c478bd9Sstevel@tonic-gate * and linkage has already been set when the pip was
29267c478bd9Sstevel@tonic-gate * first snapshotted i.e. when the other endpoint dip
29277c478bd9Sstevel@tonic-gate * was snapshotted.
29287c478bd9Sstevel@tonic-gate */
2929b9ccdc5aScth me = DI_PATH(di_mem_addr(st, stored_offset));
2930b9ccdc5aScth *off_p = stored_offset;
29317c478bd9Sstevel@tonic-gate
2932b9ccdc5aScth di_path_one_endpoint(me, noff, &off_p, get_client);
29337c478bd9Sstevel@tonic-gate
29347c478bd9Sstevel@tonic-gate /*
29357c478bd9Sstevel@tonic-gate * The other endpoint and linkage were set when this
29367c478bd9Sstevel@tonic-gate * pip was snapshotted. So we are done with both
29377c478bd9Sstevel@tonic-gate * endpoints and linkages.
29387c478bd9Sstevel@tonic-gate */
29397c478bd9Sstevel@tonic-gate ASSERT(!(me->path_snap_state &
29407c478bd9Sstevel@tonic-gate (DI_PATH_SNAP_NOCLIENT|DI_PATH_SNAP_NOPHCI)));
29417c478bd9Sstevel@tonic-gate ASSERT(!(me->path_snap_state &
29427c478bd9Sstevel@tonic-gate (DI_PATH_SNAP_NOCLINK|DI_PATH_SNAP_NOPLINK)));
29437c478bd9Sstevel@tonic-gate
29447c478bd9Sstevel@tonic-gate mdi_pi_unlock(pip);
29457c478bd9Sstevel@tonic-gate continue;
29467c478bd9Sstevel@tonic-gate }
29477c478bd9Sstevel@tonic-gate
29487c478bd9Sstevel@tonic-gate /*
29497c478bd9Sstevel@tonic-gate * Now that we need to snapshot this pip, check memory
29507c478bd9Sstevel@tonic-gate */
2951b9ccdc5aScth size = sizeof (struct di_path);
2952b9ccdc5aScth *off_p = off = di_checkmem(st, off, size);
2953b9ccdc5aScth me = DI_PATH(di_mem_addr(st, off));
29547c478bd9Sstevel@tonic-gate me->self = off;
2955b9ccdc5aScth off += size;
29567c478bd9Sstevel@tonic-gate
29577c478bd9Sstevel@tonic-gate me->path_snap_state =
29587c478bd9Sstevel@tonic-gate DI_PATH_SNAP_NOCLINK | DI_PATH_SNAP_NOPLINK;
29597c478bd9Sstevel@tonic-gate me->path_snap_state |=
29607c478bd9Sstevel@tonic-gate DI_PATH_SNAP_NOCLIENT | DI_PATH_SNAP_NOPHCI;
29617c478bd9Sstevel@tonic-gate
29627c478bd9Sstevel@tonic-gate /*
29637c478bd9Sstevel@tonic-gate * Zero out fields as di_checkmem() doesn't guarantee
29647c478bd9Sstevel@tonic-gate * zero-filled memory
29657c478bd9Sstevel@tonic-gate */
29667c478bd9Sstevel@tonic-gate me->path_client = me->path_phci = 0;
29677c478bd9Sstevel@tonic-gate me->path_c_link = me->path_p_link = 0;
29687c478bd9Sstevel@tonic-gate
2969b9ccdc5aScth di_path_one_endpoint(me, noff, &off_p, get_client);
29707c478bd9Sstevel@tonic-gate
29717c478bd9Sstevel@tonic-gate /*
29727c478bd9Sstevel@tonic-gate * Note the existence of this pathinfo
29737c478bd9Sstevel@tonic-gate */
29747c478bd9Sstevel@tonic-gate di_register_pip(st, pip, me->self);
29757c478bd9Sstevel@tonic-gate
29764c06356bSdh142964 me->path_state = path_state_convert(mdi_pi_get_state(pip));
29774c06356bSdh142964 me->path_flags = path_flags_convert(mdi_pi_get_flags(pip));
29787c478bd9Sstevel@tonic-gate
2979602ca9eaScth me->path_instance = mdi_pi_get_path_instance(pip);
2980602ca9eaScth
29817c478bd9Sstevel@tonic-gate /*
29827c478bd9Sstevel@tonic-gate * Get intermediate addressing info.
29837c478bd9Sstevel@tonic-gate */
2984b9ccdc5aScth size = strlen(mdi_pi_get_addr(pip)) + 1;
2985b9ccdc5aScth me->path_addr = off = di_checkmem(st, off, size);
29867c478bd9Sstevel@tonic-gate (void) strcpy(di_mem_addr(st, off), mdi_pi_get_addr(pip));
2987b9ccdc5aScth off += size;
29887c478bd9Sstevel@tonic-gate
29897c478bd9Sstevel@tonic-gate /*
29907c478bd9Sstevel@tonic-gate * Get path properties if props are to be included in the
29917c478bd9Sstevel@tonic-gate * snapshot
29927c478bd9Sstevel@tonic-gate */
29937c478bd9Sstevel@tonic-gate if (DINFOPROP & st->command) {
2994b9ccdc5aScth me->path_prop = off;
2995b9ccdc5aScth off = di_path_getprop(pip, &me->path_prop, st);
29967c478bd9Sstevel@tonic-gate } else {
29977c478bd9Sstevel@tonic-gate me->path_prop = 0;
29987c478bd9Sstevel@tonic-gate }
29997c478bd9Sstevel@tonic-gate
30007c478bd9Sstevel@tonic-gate mdi_pi_unlock(pip);
30017c478bd9Sstevel@tonic-gate }
30027c478bd9Sstevel@tonic-gate
30037c478bd9Sstevel@tonic-gate *off_p = 0;
30047c478bd9Sstevel@tonic-gate return (off);
30057c478bd9Sstevel@tonic-gate }
30067c478bd9Sstevel@tonic-gate
30077c478bd9Sstevel@tonic-gate /*
3008b9ccdc5aScth * Return driver prop_op entry point for the specified devinfo node.
3009b9ccdc5aScth *
3010b9ccdc5aScth * To return a non-NULL value:
3011b9ccdc5aScth * - driver must be attached and held:
3012b9ccdc5aScth * If driver is not attached we ignore the driver property list.
3013b9ccdc5aScth * No one should rely on such properties.
3014b9ccdc5aScth * - driver "cb_prop_op != ddi_prop_op":
3015b9ccdc5aScth * If "cb_prop_op == ddi_prop_op", framework does not need to call driver.
3016b9ccdc5aScth * XXX or parent's bus_prop_op != ddi_bus_prop_op
30177c478bd9Sstevel@tonic-gate */
3018b9ccdc5aScth static int
di_getprop_prop_op(struct dev_info * dip)3019b9ccdc5aScth (*di_getprop_prop_op(struct dev_info *dip))
3020b9ccdc5aScth (dev_t, dev_info_t *, ddi_prop_op_t, int, char *, caddr_t, int *)
3021b9ccdc5aScth {
3022b9ccdc5aScth struct dev_ops *ops;
3023b9ccdc5aScth
3024b9ccdc5aScth /* If driver is not attached we ignore the driver property list. */
3025b9ccdc5aScth if ((dip == NULL) || !i_ddi_devi_attached((dev_info_t *)dip))
3026b9ccdc5aScth return (NULL);
30277c478bd9Sstevel@tonic-gate
30287c478bd9Sstevel@tonic-gate /*
3029b9ccdc5aScth * Some nexus drivers incorrectly set cb_prop_op to nodev, nulldev,
3030b9ccdc5aScth * or even NULL.
30317c478bd9Sstevel@tonic-gate */
3032b9ccdc5aScth ops = dip->devi_ops;
30337c478bd9Sstevel@tonic-gate if (ops && ops->devo_cb_ops &&
30347c478bd9Sstevel@tonic-gate (ops->devo_cb_ops->cb_prop_op != ddi_prop_op) &&
30357c478bd9Sstevel@tonic-gate (ops->devo_cb_ops->cb_prop_op != nodev) &&
30367c478bd9Sstevel@tonic-gate (ops->devo_cb_ops->cb_prop_op != nulldev) &&
3037b9ccdc5aScth (ops->devo_cb_ops->cb_prop_op != NULL))
3038b9ccdc5aScth return (ops->devo_cb_ops->cb_prop_op);
3039b9ccdc5aScth return (NULL);
30407c478bd9Sstevel@tonic-gate }
30417c478bd9Sstevel@tonic-gate
3042b9ccdc5aScth static di_off_t
di_getprop_add(int list,int dyn,struct di_state * st,struct dev_info * dip,int (* prop_op)(),char * name,dev_t devt,int aflags,int alen,caddr_t aval,di_off_t off,di_off_t ** off_pp)3043b9ccdc5aScth di_getprop_add(int list, int dyn, struct di_state *st, struct dev_info *dip,
3044b9ccdc5aScth int (*prop_op)(),
3045b9ccdc5aScth char *name, dev_t devt, int aflags, int alen, caddr_t aval,
3046b9ccdc5aScth di_off_t off, di_off_t **off_pp)
3047b9ccdc5aScth {
3048b9ccdc5aScth int need_free = 0;
3049b9ccdc5aScth dev_t pdevt;
3050b9ccdc5aScth int pflags;
3051b9ccdc5aScth int rv;
3052b9ccdc5aScth caddr_t val;
3053b9ccdc5aScth int len;
3054b9ccdc5aScth size_t size;
3055b9ccdc5aScth struct di_prop *pp;
3056b9ccdc5aScth
3057b9ccdc5aScth /* If we have prop_op function, ask driver for latest value */
3058b9ccdc5aScth if (prop_op) {
3059b9ccdc5aScth ASSERT(dip);
3060b9ccdc5aScth
3061b9ccdc5aScth /* Must search DDI_DEV_T_NONE with DDI_DEV_T_ANY */
3062b9ccdc5aScth pdevt = (devt == DDI_DEV_T_NONE) ? DDI_DEV_T_ANY : devt;
3063b9ccdc5aScth
30647c478bd9Sstevel@tonic-gate /*
3065b9ccdc5aScth * We have type information in flags, but are invoking an
3066b9ccdc5aScth * old non-typed prop_op(9E) interface. Since not all types are
3067b9ccdc5aScth * part of DDI_PROP_TYPE_ANY (example is DDI_PROP_TYPE_INT64),
3068b9ccdc5aScth * we set DDI_PROP_CONSUMER_TYPED - causing the framework to
3069b9ccdc5aScth * expand type bits beyond DDI_PROP_TYPE_ANY. This allows us
3070b9ccdc5aScth * to use the legacy prop_op(9E) interface to obtain updates
3071b9ccdc5aScth * non-DDI_PROP_TYPE_ANY dynamic properties.
30727c478bd9Sstevel@tonic-gate */
3073b9ccdc5aScth pflags = aflags & ~DDI_PROP_TYPE_MASK;
3074b9ccdc5aScth pflags |= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
3075b9ccdc5aScth DDI_PROP_CONSUMER_TYPED;
3076f9722deaSChris Horne
3077f9722deaSChris Horne /*
3078f9722deaSChris Horne * Hold and exit across prop_op(9E) to avoid lock order
3079f9722deaSChris Horne * issues between
3080f9722deaSChris Horne * [ndi_devi_enter() ..prop_op(9E).. driver-lock]
3081f9722deaSChris Horne * .vs.
3082f9722deaSChris Horne * [..ioctl(9E).. driver-lock ..ddi_remove_minor_node(9F)..
3083f9722deaSChris Horne * ndi_devi_enter()]
3084f9722deaSChris Horne * ordering.
3085f9722deaSChris Horne */
3086f9722deaSChris Horne ndi_hold_devi((dev_info_t *)dip);
3087*3fe80ca4SDan Cross ndi_devi_exit((dev_info_t *)dip);
3088f9722deaSChris Horne rv = (*prop_op)(pdevt, (dev_info_t *)dip,
3089f9722deaSChris Horne PROP_LEN_AND_VAL_ALLOC, pflags, name, &val, &len);
3090*3fe80ca4SDan Cross ndi_devi_enter((dev_info_t *)dip);
3091f9722deaSChris Horne ndi_rele_devi((dev_info_t *)dip);
3092b9ccdc5aScth
3093b9ccdc5aScth if (rv == DDI_PROP_SUCCESS) {
3094b9ccdc5aScth need_free = 1; /* dynamic prop obtained */
3095b9ccdc5aScth } else if (dyn) {
30967c478bd9Sstevel@tonic-gate /*
3097b9ccdc5aScth * A dynamic property must succeed prop_op(9E) to show
3098b9ccdc5aScth * up in the snapshot - that is the only source of its
3099b9ccdc5aScth * value.
31007c478bd9Sstevel@tonic-gate */
3101b9ccdc5aScth return (off); /* dynamic prop not supported */
3102b9ccdc5aScth } else {
3103b9ccdc5aScth /*
3104b9ccdc5aScth * In case calling the driver caused an update off
3105b9ccdc5aScth * prop_op(9E) of a non-dynamic property (code leading
3106b9ccdc5aScth * to ddi_prop_change), we defer picking up val and
3107b9ccdc5aScth * len informatiojn until after prop_op(9E) to ensure
3108b9ccdc5aScth * that we snapshot the latest value.
3109b9ccdc5aScth */
3110b9ccdc5aScth val = aval;
3111b9ccdc5aScth len = alen;
3112b9ccdc5aScth
3113b9ccdc5aScth }
3114b9ccdc5aScth } else {
3115b9ccdc5aScth val = aval;
3116b9ccdc5aScth len = alen;
3117b9ccdc5aScth }
3118b9ccdc5aScth
3119b9ccdc5aScth dcmn_err((CE_CONT, "di_getprop_add: list %d %s len %d val %p\n",
3120b9ccdc5aScth list, name ? name : "NULL", len, (void *)val));
3121b9ccdc5aScth
3122b9ccdc5aScth size = sizeof (struct di_prop);
3123b9ccdc5aScth **off_pp = off = di_checkmem(st, off, size);
3124b9ccdc5aScth pp = DI_PROP(di_mem_addr(st, off));
31257c478bd9Sstevel@tonic-gate pp->self = off;
3126b9ccdc5aScth off += size;
3127b9ccdc5aScth
3128b9ccdc5aScth pp->dev_major = getmajor(devt);
3129b9ccdc5aScth pp->dev_minor = getminor(devt);
3130b9ccdc5aScth pp->prop_flags = aflags;
31317c478bd9Sstevel@tonic-gate pp->prop_list = list;
31327c478bd9Sstevel@tonic-gate
3133b9ccdc5aScth /* property name */
3134b9ccdc5aScth if (name) {
3135b9ccdc5aScth size = strlen(name) + 1;
3136b9ccdc5aScth pp->prop_name = off = di_checkmem(st, off, size);
3137b9ccdc5aScth (void) strcpy(di_mem_addr(st, off), name);
3138b9ccdc5aScth off += size;
3139b9ccdc5aScth } else {
3140b9ccdc5aScth pp->prop_name = -1;
31417c478bd9Sstevel@tonic-gate }
31427c478bd9Sstevel@tonic-gate
3143b9ccdc5aScth pp->prop_len = len;
3144b9ccdc5aScth if (val == NULL) {
31457c478bd9Sstevel@tonic-gate pp->prop_data = -1;
3146b9ccdc5aScth } else if (len != 0) {
3147b9ccdc5aScth size = len;
3148b9ccdc5aScth pp->prop_data = off = di_checkmem(st, off, size);
3149b9ccdc5aScth bcopy(val, di_mem_addr(st, off), size);
3150b9ccdc5aScth off += size;
31517c478bd9Sstevel@tonic-gate }
31527c478bd9Sstevel@tonic-gate
3153b9ccdc5aScth pp->next = 0; /* assume tail for now */
3154b9ccdc5aScth *off_pp = &pp->next; /* return pointer to our next */
31557c478bd9Sstevel@tonic-gate
3156b9ccdc5aScth if (need_free) /* free PROP_LEN_AND_VAL_ALLOC alloc */
3157b9ccdc5aScth kmem_free(val, len);
31587c478bd9Sstevel@tonic-gate return (off);
31597c478bd9Sstevel@tonic-gate }
31607c478bd9Sstevel@tonic-gate
3161b9ccdc5aScth
31627c478bd9Sstevel@tonic-gate /*
3163b9ccdc5aScth * Copy a list of properties attached to a devinfo node. Called from
3164b9ccdc5aScth * di_copynode with active ndi_devi_enter. The major number is passed in case
3165b9ccdc5aScth * we need to call driver's prop_op entry. The value of list indicates
3166b9ccdc5aScth * which list we are copying. Possible values are:
3167b9ccdc5aScth * DI_PROP_DRV_LIST, DI_PROP_SYS_LIST, DI_PROP_GLB_LIST, DI_PROP_HW_LIST
31687c478bd9Sstevel@tonic-gate */
3169b9ccdc5aScth static di_off_t
di_getprop(int list,struct ddi_prop ** pprop,di_off_t * off_p,struct di_state * st,struct dev_info * dip)3170b9ccdc5aScth di_getprop(int list, struct ddi_prop **pprop, di_off_t *off_p,
3171b9ccdc5aScth struct di_state *st, struct dev_info *dip)
3172b9ccdc5aScth {
3173b9ccdc5aScth struct ddi_prop *prop;
3174b9ccdc5aScth int (*prop_op)();
3175b9ccdc5aScth int off;
3176b9ccdc5aScth struct ddi_minor_data *mn;
3177b9ccdc5aScth i_ddi_prop_dyn_t *dp;
3178b9ccdc5aScth struct plist {
3179b9ccdc5aScth struct plist *pl_next;
3180b9ccdc5aScth char *pl_name;
3181b9ccdc5aScth int pl_flags;
3182b9ccdc5aScth dev_t pl_dev;
3183b9ccdc5aScth int pl_len;
3184b9ccdc5aScth caddr_t pl_val;
3185b9ccdc5aScth } *pl, *pl0, **plp;
31867c478bd9Sstevel@tonic-gate
3187b9ccdc5aScth ASSERT(st != NULL);
31887c478bd9Sstevel@tonic-gate
3189b9ccdc5aScth off = *off_p;
3190b9ccdc5aScth *off_p = 0;
3191b9ccdc5aScth dcmn_err((CE_CONT, "di_getprop: copy property list %d at addr %p\n",
3192b9ccdc5aScth list, (void *)*pprop));
31937c478bd9Sstevel@tonic-gate
3194b9ccdc5aScth /* get pointer to driver's prop_op(9E) implementation if DRV_LIST */
3195b9ccdc5aScth prop_op = (list == DI_PROP_DRV_LIST) ? di_getprop_prop_op(dip) : NULL;
31967c478bd9Sstevel@tonic-gate
3197b9ccdc5aScth /*
3198b9ccdc5aScth * Form private list of properties, holding devi_lock for properties
3199656d7645SChris Horne * that hang off the dip.
3200b9ccdc5aScth */
3201b9ccdc5aScth if (dip)
3202b9ccdc5aScth mutex_enter(&(dip->devi_lock));
3203656d7645SChris Horne for (pl0 = NULL, plp = &pl0, prop = *pprop;
3204b9ccdc5aScth prop; plp = &pl->pl_next, prop = prop->prop_next) {
3205b9ccdc5aScth pl = kmem_alloc(sizeof (*pl), KM_SLEEP);
3206b9ccdc5aScth *plp = pl;
3207b9ccdc5aScth pl->pl_next = NULL;
3208b9ccdc5aScth if (prop->prop_name)
3209b9ccdc5aScth pl->pl_name = i_ddi_strdup(prop->prop_name, KM_SLEEP);
3210b9ccdc5aScth else
3211b9ccdc5aScth pl->pl_name = NULL;
3212b9ccdc5aScth pl->pl_flags = prop->prop_flags;
3213b9ccdc5aScth pl->pl_dev = prop->prop_dev;
3214b9ccdc5aScth if (prop->prop_len) {
3215b9ccdc5aScth pl->pl_len = prop->prop_len;
3216b9ccdc5aScth pl->pl_val = kmem_alloc(pl->pl_len, KM_SLEEP);
3217b9ccdc5aScth bcopy(prop->prop_val, pl->pl_val, pl->pl_len);
32187c478bd9Sstevel@tonic-gate } else {
3219b9ccdc5aScth pl->pl_len = 0;
3220b9ccdc5aScth pl->pl_val = NULL;
3221b9ccdc5aScth }
3222b9ccdc5aScth }
3223b9ccdc5aScth if (dip)
3224b9ccdc5aScth mutex_exit(&(dip->devi_lock));
3225b9ccdc5aScth
3226b9ccdc5aScth /*
3227b9ccdc5aScth * Now that we have dropped devi_lock, perform a second-pass to
3228b9ccdc5aScth * add properties to the snapshot. We do this as a second pass
3229b9ccdc5aScth * because we may need to call prop_op(9E) and we can't hold
3230b9ccdc5aScth * devi_lock across that call.
3231b9ccdc5aScth */
3232b9ccdc5aScth for (pl = pl0; pl; pl = pl0) {
3233b9ccdc5aScth pl0 = pl->pl_next;
3234b9ccdc5aScth off = di_getprop_add(list, 0, st, dip, prop_op, pl->pl_name,
3235b9ccdc5aScth pl->pl_dev, pl->pl_flags, pl->pl_len, pl->pl_val,
3236b9ccdc5aScth off, &off_p);
3237b9ccdc5aScth if (pl->pl_val)
3238b9ccdc5aScth kmem_free(pl->pl_val, pl->pl_len);
3239b9ccdc5aScth if (pl->pl_name)
3240b9ccdc5aScth kmem_free(pl->pl_name, strlen(pl->pl_name) + 1);
3241b9ccdc5aScth kmem_free(pl, sizeof (*pl));
32427c478bd9Sstevel@tonic-gate }
32437c478bd9Sstevel@tonic-gate
32447c478bd9Sstevel@tonic-gate /*
3245b9ccdc5aScth * If there is no prop_op or dynamic property support has been
3246b9ccdc5aScth * disabled, we are done.
32477c478bd9Sstevel@tonic-gate */
3248b9ccdc5aScth if ((prop_op == NULL) || (di_prop_dyn == 0)) {
3249b9ccdc5aScth *off_p = 0;
3250b9ccdc5aScth return (off);
3251b9ccdc5aScth }
32527c478bd9Sstevel@tonic-gate
3253b9ccdc5aScth /* Add dynamic driver properties to snapshot */
3254b9ccdc5aScth for (dp = i_ddi_prop_dyn_driver_get((dev_info_t *)dip);
3255b9ccdc5aScth dp && dp->dp_name; dp++) {
3256b9ccdc5aScth if (dp->dp_spec_type) {
3257b9ccdc5aScth /* if spec_type, property of matching minor */
3258b9ccdc5aScth ASSERT(DEVI_BUSY_OWNED(dip));
3259b9ccdc5aScth for (mn = dip->devi_minor; mn; mn = mn->next) {
3260b9ccdc5aScth if (mn->ddm_spec_type != dp->dp_spec_type)
3261b9ccdc5aScth continue;
3262b9ccdc5aScth off = di_getprop_add(list, 1, st, dip, prop_op,
3263b9ccdc5aScth dp->dp_name, mn->ddm_dev, dp->dp_type,
3264b9ccdc5aScth 0, NULL, off, &off_p);
32657c478bd9Sstevel@tonic-gate }
32667c478bd9Sstevel@tonic-gate } else {
3267b9ccdc5aScth /* property of devinfo node */
3268b9ccdc5aScth off = di_getprop_add(list, 1, st, dip, prop_op,
3269b9ccdc5aScth dp->dp_name, DDI_DEV_T_NONE, dp->dp_type,
3270b9ccdc5aScth 0, NULL, off, &off_p);
32717c478bd9Sstevel@tonic-gate }
32727c478bd9Sstevel@tonic-gate }
32737c478bd9Sstevel@tonic-gate
3274b9ccdc5aScth /* Add dynamic parent properties to snapshot */
3275b9ccdc5aScth for (dp = i_ddi_prop_dyn_parent_get((dev_info_t *)dip);
3276b9ccdc5aScth dp && dp->dp_name; dp++) {
3277b9ccdc5aScth if (dp->dp_spec_type) {
3278b9ccdc5aScth /* if spec_type, property of matching minor */
3279b9ccdc5aScth ASSERT(DEVI_BUSY_OWNED(dip));
3280b9ccdc5aScth for (mn = dip->devi_minor; mn; mn = mn->next) {
3281b9ccdc5aScth if (mn->ddm_spec_type != dp->dp_spec_type)
3282b9ccdc5aScth continue;
3283b9ccdc5aScth off = di_getprop_add(list, 1, st, dip, prop_op,
3284b9ccdc5aScth dp->dp_name, mn->ddm_dev, dp->dp_type,
3285b9ccdc5aScth 0, NULL, off, &off_p);
3286b9ccdc5aScth }
3287b9ccdc5aScth } else {
3288b9ccdc5aScth /* property of devinfo node */
3289b9ccdc5aScth off = di_getprop_add(list, 1, st, dip, prop_op,
3290b9ccdc5aScth dp->dp_name, DDI_DEV_T_NONE, dp->dp_type,
3291b9ccdc5aScth 0, NULL, off, &off_p);
3292b9ccdc5aScth }
32937c478bd9Sstevel@tonic-gate }
32947c478bd9Sstevel@tonic-gate
3295b9ccdc5aScth *off_p = 0;
32967c478bd9Sstevel@tonic-gate return (off);
32977c478bd9Sstevel@tonic-gate }
32987c478bd9Sstevel@tonic-gate
32997c478bd9Sstevel@tonic-gate /*
33007c478bd9Sstevel@tonic-gate * find private data format attached to a dip
33017c478bd9Sstevel@tonic-gate * parent = 1 to match driver name of parent dip (for parent private data)
33027c478bd9Sstevel@tonic-gate * 0 to match driver name of current dip (for driver private data)
33037c478bd9Sstevel@tonic-gate */
33047c478bd9Sstevel@tonic-gate #define DI_MATCH_DRIVER 0
33057c478bd9Sstevel@tonic-gate #define DI_MATCH_PARENT 1
33067c478bd9Sstevel@tonic-gate
33077c478bd9Sstevel@tonic-gate struct di_priv_format *
di_match_drv_name(struct dev_info * node,struct di_state * st,int match)33087c478bd9Sstevel@tonic-gate di_match_drv_name(struct dev_info *node, struct di_state *st, int match)
33097c478bd9Sstevel@tonic-gate {
33107c478bd9Sstevel@tonic-gate int i, count, len;
33117c478bd9Sstevel@tonic-gate char *drv_name;
33127c478bd9Sstevel@tonic-gate major_t major;
33137c478bd9Sstevel@tonic-gate struct di_all *all;
33147c478bd9Sstevel@tonic-gate struct di_priv_format *form;
33157c478bd9Sstevel@tonic-gate
33167c478bd9Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_match_drv_name: node = %s, match = %x\n",
33177c478bd9Sstevel@tonic-gate node->devi_node_name, match));
33187c478bd9Sstevel@tonic-gate
33197c478bd9Sstevel@tonic-gate if (match == DI_MATCH_PARENT) {
33207c478bd9Sstevel@tonic-gate node = DEVI(node->devi_parent);
33217c478bd9Sstevel@tonic-gate }
33227c478bd9Sstevel@tonic-gate
33237c478bd9Sstevel@tonic-gate if (node == NULL) {
33247c478bd9Sstevel@tonic-gate return (NULL);
33257c478bd9Sstevel@tonic-gate }
33267c478bd9Sstevel@tonic-gate
33275c066ec2SJerry Gilliam major = node->devi_major;
33287c478bd9Sstevel@tonic-gate if (major == (major_t)(-1)) {
33297c478bd9Sstevel@tonic-gate return (NULL);
33307c478bd9Sstevel@tonic-gate }
33317c478bd9Sstevel@tonic-gate
33327c478bd9Sstevel@tonic-gate /*
33337c478bd9Sstevel@tonic-gate * Match the driver name.
33347c478bd9Sstevel@tonic-gate */
33357c478bd9Sstevel@tonic-gate drv_name = ddi_major_to_name(major);
33367c478bd9Sstevel@tonic-gate if ((drv_name == NULL) || *drv_name == '\0') {
33377c478bd9Sstevel@tonic-gate return (NULL);
33387c478bd9Sstevel@tonic-gate }
33397c478bd9Sstevel@tonic-gate
33407c478bd9Sstevel@tonic-gate /* Now get the di_priv_format array */
3341b9ccdc5aScth all = DI_ALL_PTR(st);
33427c478bd9Sstevel@tonic-gate if (match == DI_MATCH_PARENT) {
33437c478bd9Sstevel@tonic-gate count = all->n_ppdata;
3344b9ccdc5aScth form = DI_PRIV_FORMAT(di_mem_addr(st, all->ppdata_format));
33457c478bd9Sstevel@tonic-gate } else {
33467c478bd9Sstevel@tonic-gate count = all->n_dpdata;
3347b9ccdc5aScth form = DI_PRIV_FORMAT(di_mem_addr(st, all->dpdata_format));
33487c478bd9Sstevel@tonic-gate }
33497c478bd9Sstevel@tonic-gate
33507c478bd9Sstevel@tonic-gate len = strlen(drv_name);
33517c478bd9Sstevel@tonic-gate for (i = 0; i < count; i++) {
33527c478bd9Sstevel@tonic-gate char *tmp;
33537c478bd9Sstevel@tonic-gate
33547c478bd9Sstevel@tonic-gate tmp = form[i].drv_name;
33557c478bd9Sstevel@tonic-gate while (tmp && (*tmp != '\0')) {
33567c478bd9Sstevel@tonic-gate if (strncmp(drv_name, tmp, len) == 0) {
33577c478bd9Sstevel@tonic-gate return (&form[i]);
33587c478bd9Sstevel@tonic-gate }
33597c478bd9Sstevel@tonic-gate /*
33607c478bd9Sstevel@tonic-gate * Move to next driver name, skipping a white space
33617c478bd9Sstevel@tonic-gate */
33627c478bd9Sstevel@tonic-gate if (tmp = strchr(tmp, ' ')) {
33637c478bd9Sstevel@tonic-gate tmp++;
33647c478bd9Sstevel@tonic-gate }
33657c478bd9Sstevel@tonic-gate }
33667c478bd9Sstevel@tonic-gate }
33677c478bd9Sstevel@tonic-gate
33687c478bd9Sstevel@tonic-gate return (NULL);
33697c478bd9Sstevel@tonic-gate }
33707c478bd9Sstevel@tonic-gate
33717c478bd9Sstevel@tonic-gate /*
33727c478bd9Sstevel@tonic-gate * The following functions copy data as specified by the format passed in.
33737c478bd9Sstevel@tonic-gate * To prevent invalid format from panicing the system, we call on_fault().
33747c478bd9Sstevel@tonic-gate * A return value of 0 indicates an error. Otherwise, the total offset
33757c478bd9Sstevel@tonic-gate * is returned.
33767c478bd9Sstevel@tonic-gate */
33777c478bd9Sstevel@tonic-gate #define DI_MAX_PRIVDATA (PAGESIZE >> 1) /* max private data size */
33787c478bd9Sstevel@tonic-gate
33797c478bd9Sstevel@tonic-gate static di_off_t
di_getprvdata(struct di_priv_format * pdp,struct dev_info * node,void * data,di_off_t * off_p,struct di_state * st)3380c3b0fe9bScth di_getprvdata(struct di_priv_format *pdp, struct dev_info *node,
3381c3b0fe9bScth void *data, di_off_t *off_p, struct di_state *st)
33827c478bd9Sstevel@tonic-gate {
33837c478bd9Sstevel@tonic-gate caddr_t pa;
33847c478bd9Sstevel@tonic-gate void *ptr;
33857c478bd9Sstevel@tonic-gate int i, size, repeat;
33867c478bd9Sstevel@tonic-gate di_off_t off, off0, *tmp;
3387c3b0fe9bScth char *path;
33887c478bd9Sstevel@tonic-gate label_t ljb;
33897c478bd9Sstevel@tonic-gate
33907c478bd9Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_getprvdata:\n"));
33917c478bd9Sstevel@tonic-gate
33927c478bd9Sstevel@tonic-gate /*
33937c478bd9Sstevel@tonic-gate * check memory availability. Private data size is
33947c478bd9Sstevel@tonic-gate * limited to DI_MAX_PRIVDATA.
33957c478bd9Sstevel@tonic-gate */
33967c478bd9Sstevel@tonic-gate off = di_checkmem(st, *off_p, DI_MAX_PRIVDATA);
3397b9ccdc5aScth *off_p = off;
33987c478bd9Sstevel@tonic-gate
3399bc1009abSjg if ((pdp->bytes == 0) || pdp->bytes > DI_MAX_PRIVDATA) {
34007c478bd9Sstevel@tonic-gate goto failure;
34017c478bd9Sstevel@tonic-gate }
34027c478bd9Sstevel@tonic-gate
34037c478bd9Sstevel@tonic-gate if (!on_fault(&ljb)) {
34047c478bd9Sstevel@tonic-gate /* copy the struct */
34057c478bd9Sstevel@tonic-gate bcopy(data, di_mem_addr(st, off), pdp->bytes);
3406b9ccdc5aScth off0 = DI_ALIGN(pdp->bytes); /* XXX remove DI_ALIGN */
34077c478bd9Sstevel@tonic-gate
34087c478bd9Sstevel@tonic-gate /* dereferencing pointers */
34097c478bd9Sstevel@tonic-gate for (i = 0; i < MAX_PTR_IN_PRV; i++) {
34107c478bd9Sstevel@tonic-gate
34117c478bd9Sstevel@tonic-gate if (pdp->ptr[i].size == 0) {
34127c478bd9Sstevel@tonic-gate goto success; /* no more ptrs */
34137c478bd9Sstevel@tonic-gate }
34147c478bd9Sstevel@tonic-gate
34157c478bd9Sstevel@tonic-gate /*
34167c478bd9Sstevel@tonic-gate * first, get the pointer content
34177c478bd9Sstevel@tonic-gate */
34187c478bd9Sstevel@tonic-gate if ((pdp->ptr[i].offset < 0) ||
3419b9ccdc5aScth (pdp->ptr[i].offset > pdp->bytes - sizeof (char *)))
34207c478bd9Sstevel@tonic-gate goto failure; /* wrong offset */
34217c478bd9Sstevel@tonic-gate
34227c478bd9Sstevel@tonic-gate pa = di_mem_addr(st, off + pdp->ptr[i].offset);
34237c478bd9Sstevel@tonic-gate
3424bc1009abSjg /* save a tmp ptr to store off_t later */
3425bc1009abSjg tmp = (di_off_t *)(intptr_t)pa;
3426bc1009abSjg
3427bc1009abSjg /* get pointer value, if NULL continue */
3428bc1009abSjg ptr = *((void **) (intptr_t)pa);
3429bc1009abSjg if (ptr == NULL) {
34307c478bd9Sstevel@tonic-gate continue;
34317c478bd9Sstevel@tonic-gate }
34327c478bd9Sstevel@tonic-gate
34337c478bd9Sstevel@tonic-gate /*
34347c478bd9Sstevel@tonic-gate * next, find the repeat count (array dimension)
34357c478bd9Sstevel@tonic-gate */
34367c478bd9Sstevel@tonic-gate repeat = pdp->ptr[i].len_offset;
34377c478bd9Sstevel@tonic-gate
34387c478bd9Sstevel@tonic-gate /*
34397c478bd9Sstevel@tonic-gate * Positive value indicates a fixed sized array.
34407c478bd9Sstevel@tonic-gate * 0 or negative value indicates variable sized array.
34417c478bd9Sstevel@tonic-gate *
34427c478bd9Sstevel@tonic-gate * For variable sized array, the variable must be
34437c478bd9Sstevel@tonic-gate * an int member of the structure, with an offset
34447c478bd9Sstevel@tonic-gate * equal to the absolution value of struct member.
34457c478bd9Sstevel@tonic-gate */
34467c478bd9Sstevel@tonic-gate if (repeat > pdp->bytes - sizeof (int)) {
34477c478bd9Sstevel@tonic-gate goto failure; /* wrong offset */
34487c478bd9Sstevel@tonic-gate }
34497c478bd9Sstevel@tonic-gate
34507c478bd9Sstevel@tonic-gate if (repeat >= 0) {
3451bc1009abSjg repeat = *((int *)
3452bc1009abSjg (intptr_t)((caddr_t)data + repeat));
34537c478bd9Sstevel@tonic-gate } else {
34547c478bd9Sstevel@tonic-gate repeat = -repeat;
34557c478bd9Sstevel@tonic-gate }
34567c478bd9Sstevel@tonic-gate
34577c478bd9Sstevel@tonic-gate /*
34587c478bd9Sstevel@tonic-gate * next, get the size of the object to be copied
34597c478bd9Sstevel@tonic-gate */
34607c478bd9Sstevel@tonic-gate size = pdp->ptr[i].size * repeat;
34617c478bd9Sstevel@tonic-gate
34627c478bd9Sstevel@tonic-gate /*
34637c478bd9Sstevel@tonic-gate * Arbitrarily limit the total size of object to be
34647c478bd9Sstevel@tonic-gate * copied (1 byte to 1/4 page).
34657c478bd9Sstevel@tonic-gate */
34667c478bd9Sstevel@tonic-gate if ((size <= 0) || (size > (DI_MAX_PRIVDATA - off0))) {
34677c478bd9Sstevel@tonic-gate goto failure; /* wrong size or too big */
34687c478bd9Sstevel@tonic-gate }
34697c478bd9Sstevel@tonic-gate
34707c478bd9Sstevel@tonic-gate /*
34717c478bd9Sstevel@tonic-gate * Now copy the data
34727c478bd9Sstevel@tonic-gate */
34737c478bd9Sstevel@tonic-gate *tmp = off0;
34747c478bd9Sstevel@tonic-gate bcopy(ptr, di_mem_addr(st, off + off0), size);
3475b9ccdc5aScth off0 += DI_ALIGN(size); /* XXX remove DI_ALIGN */
34767c478bd9Sstevel@tonic-gate }
34777c478bd9Sstevel@tonic-gate } else {
34787c478bd9Sstevel@tonic-gate goto failure;
34797c478bd9Sstevel@tonic-gate }
34807c478bd9Sstevel@tonic-gate
34817c478bd9Sstevel@tonic-gate success:
34827c478bd9Sstevel@tonic-gate /*
34837c478bd9Sstevel@tonic-gate * success if reached here
34847c478bd9Sstevel@tonic-gate */
34857c478bd9Sstevel@tonic-gate no_fault();
34867c478bd9Sstevel@tonic-gate return (off + off0);
34877c478bd9Sstevel@tonic-gate /*NOTREACHED*/
34887c478bd9Sstevel@tonic-gate
34897c478bd9Sstevel@tonic-gate failure:
34907c478bd9Sstevel@tonic-gate /*
34917c478bd9Sstevel@tonic-gate * fault occurred
34927c478bd9Sstevel@tonic-gate */
34937c478bd9Sstevel@tonic-gate no_fault();
3494c3b0fe9bScth path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3495c3b0fe9bScth cmn_err(CE_WARN, "devinfo: fault on private data for '%s' at %p",
3496c3b0fe9bScth ddi_pathname((dev_info_t *)node, path), data);
3497c3b0fe9bScth kmem_free(path, MAXPATHLEN);
34987c478bd9Sstevel@tonic-gate *off_p = -1; /* set private data to indicate error */
34997c478bd9Sstevel@tonic-gate
35007c478bd9Sstevel@tonic-gate return (off);
35017c478bd9Sstevel@tonic-gate }
35027c478bd9Sstevel@tonic-gate
35037c478bd9Sstevel@tonic-gate /*
35047c478bd9Sstevel@tonic-gate * get parent private data; on error, returns original offset
35057c478bd9Sstevel@tonic-gate */
35067c478bd9Sstevel@tonic-gate static di_off_t
di_getppdata(struct dev_info * node,di_off_t * off_p,struct di_state * st)35077c478bd9Sstevel@tonic-gate di_getppdata(struct dev_info *node, di_off_t *off_p, struct di_state *st)
35087c478bd9Sstevel@tonic-gate {
35097c478bd9Sstevel@tonic-gate int off;
35107c478bd9Sstevel@tonic-gate struct di_priv_format *ppdp;
35117c478bd9Sstevel@tonic-gate
35127c478bd9Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_getppdata:\n"));
35137c478bd9Sstevel@tonic-gate
35147c478bd9Sstevel@tonic-gate /* find the parent data format */
35157c478bd9Sstevel@tonic-gate if ((ppdp = di_match_drv_name(node, st, DI_MATCH_PARENT)) == NULL) {
35167c478bd9Sstevel@tonic-gate off = *off_p;
35177c478bd9Sstevel@tonic-gate *off_p = 0; /* set parent data to none */
35187c478bd9Sstevel@tonic-gate return (off);
35197c478bd9Sstevel@tonic-gate }
35207c478bd9Sstevel@tonic-gate
3521c3b0fe9bScth return (di_getprvdata(ppdp, node,
3522c3b0fe9bScth ddi_get_parent_data((dev_info_t *)node), off_p, st));
35237c478bd9Sstevel@tonic-gate }
35247c478bd9Sstevel@tonic-gate
35257c478bd9Sstevel@tonic-gate /*
35267c478bd9Sstevel@tonic-gate * get parent private data; returns original offset
35277c478bd9Sstevel@tonic-gate */
35287c478bd9Sstevel@tonic-gate static di_off_t
di_getdpdata(struct dev_info * node,di_off_t * off_p,struct di_state * st)35297c478bd9Sstevel@tonic-gate di_getdpdata(struct dev_info *node, di_off_t *off_p, struct di_state *st)
35307c478bd9Sstevel@tonic-gate {
35317c478bd9Sstevel@tonic-gate int off;
35327c478bd9Sstevel@tonic-gate struct di_priv_format *dpdp;
35337c478bd9Sstevel@tonic-gate
35347c478bd9Sstevel@tonic-gate dcmn_err2((CE_CONT, "di_getdpdata:"));
35357c478bd9Sstevel@tonic-gate
35367c478bd9Sstevel@tonic-gate /* find the parent data format */
35377c478bd9Sstevel@tonic-gate if ((dpdp = di_match_drv_name(node, st, DI_MATCH_DRIVER)) == NULL) {
35387c478bd9Sstevel@tonic-gate off = *off_p;
35397c478bd9Sstevel@tonic-gate *off_p = 0; /* set driver data to none */
35407c478bd9Sstevel@tonic-gate return (off);
35417c478bd9Sstevel@tonic-gate }
35427c478bd9Sstevel@tonic-gate
3543c3b0fe9bScth return (di_getprvdata(dpdp, node,
3544c3b0fe9bScth ddi_get_driver_private((dev_info_t *)node), off_p, st));
35457c478bd9Sstevel@tonic-gate }
35467c478bd9Sstevel@tonic-gate
35477c478bd9Sstevel@tonic-gate /*
354826947304SEvan Yan * Copy hotplug data associated with a devinfo node into the snapshot.
354926947304SEvan Yan */
355026947304SEvan Yan static di_off_t
di_gethpdata(ddi_hp_cn_handle_t * hp_hdl,di_off_t * off_p,struct di_state * st)355126947304SEvan Yan di_gethpdata(ddi_hp_cn_handle_t *hp_hdl, di_off_t *off_p,
355226947304SEvan Yan struct di_state *st)
355326947304SEvan Yan {
355426947304SEvan Yan struct i_hp *hp;
355526947304SEvan Yan struct di_hp *me;
355626947304SEvan Yan size_t size;
355726947304SEvan Yan di_off_t off;
355826947304SEvan Yan
355926947304SEvan Yan dcmn_err2((CE_CONT, "di_gethpdata:\n"));
356026947304SEvan Yan
356126947304SEvan Yan /*
356226947304SEvan Yan * check memory first
356326947304SEvan Yan */
356426947304SEvan Yan off = di_checkmem(st, *off_p, sizeof (struct di_hp));
356526947304SEvan Yan *off_p = off;
356626947304SEvan Yan
356726947304SEvan Yan do {
356826947304SEvan Yan me = DI_HP(di_mem_addr(st, off));
356926947304SEvan Yan me->self = off;
357026947304SEvan Yan me->hp_name = 0;
357126947304SEvan Yan me->hp_connection = (int)hp_hdl->cn_info.cn_num;
357226947304SEvan Yan me->hp_depends_on = (int)hp_hdl->cn_info.cn_num_dpd_on;
357326947304SEvan Yan (void) ddihp_cn_getstate(hp_hdl);
357426947304SEvan Yan me->hp_state = (int)hp_hdl->cn_info.cn_state;
357526947304SEvan Yan me->hp_type = (int)hp_hdl->cn_info.cn_type;
357626947304SEvan Yan me->hp_type_str = 0;
357726947304SEvan Yan me->hp_last_change = (uint32_t)hp_hdl->cn_info.cn_last_change;
357826947304SEvan Yan me->hp_child = 0;
357926947304SEvan Yan
358026947304SEvan Yan /*
358126947304SEvan Yan * Child links are resolved later by di_hotplug_children().
358226947304SEvan Yan * Store a reference to this di_hp_t in the list used later
358326947304SEvan Yan * by di_hotplug_children().
358426947304SEvan Yan */
358526947304SEvan Yan hp = kmem_zalloc(sizeof (i_hp_t), KM_SLEEP);
358626947304SEvan Yan hp->hp_off = off;
358726947304SEvan Yan hp->hp_child = hp_hdl->cn_info.cn_child;
358826947304SEvan Yan list_insert_tail(&st->hp_list, hp);
358926947304SEvan Yan
359026947304SEvan Yan off += sizeof (struct di_hp);
359126947304SEvan Yan
359226947304SEvan Yan /* Add name of this di_hp_t to the snapshot */
359326947304SEvan Yan if (hp_hdl->cn_info.cn_name) {
359426947304SEvan Yan size = strlen(hp_hdl->cn_info.cn_name) + 1;
359526947304SEvan Yan me->hp_name = off = di_checkmem(st, off, size);
359626947304SEvan Yan (void) strcpy(di_mem_addr(st, off),
359726947304SEvan Yan hp_hdl->cn_info.cn_name);
359826947304SEvan Yan off += size;
359926947304SEvan Yan }
360026947304SEvan Yan
360126947304SEvan Yan /* Add type description of this di_hp_t to the snapshot */
360226947304SEvan Yan if (hp_hdl->cn_info.cn_type_str) {
360326947304SEvan Yan size = strlen(hp_hdl->cn_info.cn_type_str) + 1;
360426947304SEvan Yan me->hp_type_str = off = di_checkmem(st, off, size);
360526947304SEvan Yan (void) strcpy(di_mem_addr(st, off),
360626947304SEvan Yan hp_hdl->cn_info.cn_type_str);
360726947304SEvan Yan off += size;
360826947304SEvan Yan }
360926947304SEvan Yan
361026947304SEvan Yan /*
361126947304SEvan Yan * Set link to next in the chain of di_hp_t nodes,
361226947304SEvan Yan * or terminate the chain when processing the last node.
361326947304SEvan Yan */
361426947304SEvan Yan if (hp_hdl->next != NULL) {
361526947304SEvan Yan off = di_checkmem(st, off, sizeof (struct di_hp));
361626947304SEvan Yan me->next = off;
361726947304SEvan Yan } else {
361826947304SEvan Yan me->next = 0;
361926947304SEvan Yan }
362026947304SEvan Yan
362126947304SEvan Yan /* Update pointer to next in the chain */
362226947304SEvan Yan hp_hdl = hp_hdl->next;
362326947304SEvan Yan
362426947304SEvan Yan } while (hp_hdl);
362526947304SEvan Yan
362626947304SEvan Yan return (off);
362726947304SEvan Yan }
362826947304SEvan Yan
362926947304SEvan Yan /*
36307c478bd9Sstevel@tonic-gate * The driver is stateful across DINFOCPYALL and DINFOUSRLD.
36317c478bd9Sstevel@tonic-gate * This function encapsulates the state machine:
36327c478bd9Sstevel@tonic-gate *
36337c478bd9Sstevel@tonic-gate * -> IOC_IDLE -> IOC_SNAP -> IOC_DONE -> IOC_COPY ->
36347c478bd9Sstevel@tonic-gate * | SNAPSHOT USRLD |
36357c478bd9Sstevel@tonic-gate * --------------------------------------------------
36367c478bd9Sstevel@tonic-gate *
36377c478bd9Sstevel@tonic-gate * Returns 0 on success and -1 on failure
36387c478bd9Sstevel@tonic-gate */
36397c478bd9Sstevel@tonic-gate static int
di_setstate(struct di_state * st,int new_state)36407c478bd9Sstevel@tonic-gate di_setstate(struct di_state *st, int new_state)
36417c478bd9Sstevel@tonic-gate {
36427c478bd9Sstevel@tonic-gate int ret = 0;
36437c478bd9Sstevel@tonic-gate
36447c478bd9Sstevel@tonic-gate mutex_enter(&di_lock);
36457c478bd9Sstevel@tonic-gate switch (new_state) {
36467c478bd9Sstevel@tonic-gate case IOC_IDLE:
36477c478bd9Sstevel@tonic-gate case IOC_DONE:
36487c478bd9Sstevel@tonic-gate break;
36497c478bd9Sstevel@tonic-gate case IOC_SNAP:
36507c478bd9Sstevel@tonic-gate if (st->di_iocstate != IOC_IDLE)
36517c478bd9Sstevel@tonic-gate ret = -1;
36527c478bd9Sstevel@tonic-gate break;
36537c478bd9Sstevel@tonic-gate case IOC_COPY:
36547c478bd9Sstevel@tonic-gate if (st->di_iocstate != IOC_DONE)
36557c478bd9Sstevel@tonic-gate ret = -1;
36567c478bd9Sstevel@tonic-gate break;
36577c478bd9Sstevel@tonic-gate default:
36587c478bd9Sstevel@tonic-gate ret = -1;
36597c478bd9Sstevel@tonic-gate }
36607c478bd9Sstevel@tonic-gate
36617c478bd9Sstevel@tonic-gate if (ret == 0)
36627c478bd9Sstevel@tonic-gate st->di_iocstate = new_state;
36637c478bd9Sstevel@tonic-gate else
36647c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "incorrect state transition from %d to %d",
36657c478bd9Sstevel@tonic-gate st->di_iocstate, new_state);
36667c478bd9Sstevel@tonic-gate mutex_exit(&di_lock);
36677c478bd9Sstevel@tonic-gate return (ret);
36687c478bd9Sstevel@tonic-gate }
36697c478bd9Sstevel@tonic-gate
36707c478bd9Sstevel@tonic-gate /*
36717c478bd9Sstevel@tonic-gate * We cannot assume the presence of the entire
36727c478bd9Sstevel@tonic-gate * snapshot in this routine. All we are guaranteed
36737c478bd9Sstevel@tonic-gate * is the di_all struct + 1 byte (for root_path)
36747c478bd9Sstevel@tonic-gate */
36757c478bd9Sstevel@tonic-gate static int
header_plus_one_ok(struct di_all * all)36767c478bd9Sstevel@tonic-gate header_plus_one_ok(struct di_all *all)
36777c478bd9Sstevel@tonic-gate {
36787c478bd9Sstevel@tonic-gate /*
36797c478bd9Sstevel@tonic-gate * Refuse to read old versions
36807c478bd9Sstevel@tonic-gate */
36817c478bd9Sstevel@tonic-gate if (all->version != DI_SNAPSHOT_VERSION) {
36827c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "bad version: 0x%x", all->version));
36837c478bd9Sstevel@tonic-gate return (0);
36847c478bd9Sstevel@tonic-gate }
36857c478bd9Sstevel@tonic-gate
36867c478bd9Sstevel@tonic-gate if (all->cache_magic != DI_CACHE_MAGIC) {
36877c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "bad magic #: 0x%x", all->cache_magic));
36887c478bd9Sstevel@tonic-gate return (0);
36897c478bd9Sstevel@tonic-gate }
36907c478bd9Sstevel@tonic-gate
3691bc1009abSjg if (all->snapshot_time == 0) {
36927c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "bad timestamp: %ld", all->snapshot_time));
36937c478bd9Sstevel@tonic-gate return (0);
36947c478bd9Sstevel@tonic-gate }
36957c478bd9Sstevel@tonic-gate
36967c478bd9Sstevel@tonic-gate if (all->top_devinfo == 0) {
36977c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "NULL top devinfo"));
36987c478bd9Sstevel@tonic-gate return (0);
36997c478bd9Sstevel@tonic-gate }
37007c478bd9Sstevel@tonic-gate
37017c478bd9Sstevel@tonic-gate if (all->map_size < sizeof (*all) + 1) {
37027c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "bad map size: %u", all->map_size));
37037c478bd9Sstevel@tonic-gate return (0);
37047c478bd9Sstevel@tonic-gate }
37057c478bd9Sstevel@tonic-gate
37067c478bd9Sstevel@tonic-gate if (all->root_path[0] != '/' || all->root_path[1] != '\0') {
37077c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "bad rootpath: %c%c",
37087c478bd9Sstevel@tonic-gate all->root_path[0], all->root_path[1]));
37097c478bd9Sstevel@tonic-gate return (0);
37107c478bd9Sstevel@tonic-gate }
37117c478bd9Sstevel@tonic-gate
37127c478bd9Sstevel@tonic-gate /*
37137c478bd9Sstevel@tonic-gate * We can't check checksum here as we just have the header
37147c478bd9Sstevel@tonic-gate */
37157c478bd9Sstevel@tonic-gate
37167c478bd9Sstevel@tonic-gate return (1);
37177c478bd9Sstevel@tonic-gate }
37187c478bd9Sstevel@tonic-gate
37197c478bd9Sstevel@tonic-gate static int
chunk_write(struct vnode * vp,offset_t off,caddr_t buf,size_t len)37207c478bd9Sstevel@tonic-gate chunk_write(struct vnode *vp, offset_t off, caddr_t buf, size_t len)
37217c478bd9Sstevel@tonic-gate {
37227c478bd9Sstevel@tonic-gate rlim64_t rlimit;
37237c478bd9Sstevel@tonic-gate ssize_t resid;
37247c478bd9Sstevel@tonic-gate int error = 0;
37257c478bd9Sstevel@tonic-gate
37267c478bd9Sstevel@tonic-gate
37277c478bd9Sstevel@tonic-gate rlimit = RLIM64_INFINITY;
37287c478bd9Sstevel@tonic-gate
37297c478bd9Sstevel@tonic-gate while (len) {
37307c478bd9Sstevel@tonic-gate resid = 0;
37317c478bd9Sstevel@tonic-gate error = vn_rdwr(UIO_WRITE, vp, buf, len, off,
37327c478bd9Sstevel@tonic-gate UIO_SYSSPACE, FSYNC, rlimit, kcred, &resid);
37337c478bd9Sstevel@tonic-gate
37347c478bd9Sstevel@tonic-gate if (error || resid < 0) {
37357c478bd9Sstevel@tonic-gate error = error ? error : EIO;
37367c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "write error: %d", error));
37377c478bd9Sstevel@tonic-gate break;
37387c478bd9Sstevel@tonic-gate }
37397c478bd9Sstevel@tonic-gate
37407c478bd9Sstevel@tonic-gate /*
37417c478bd9Sstevel@tonic-gate * Check if we are making progress
37427c478bd9Sstevel@tonic-gate */
37437c478bd9Sstevel@tonic-gate if (resid >= len) {
37447c478bd9Sstevel@tonic-gate error = ENOSPC;
37457c478bd9Sstevel@tonic-gate break;
37467c478bd9Sstevel@tonic-gate }
37477c478bd9Sstevel@tonic-gate buf += len - resid;
37487c478bd9Sstevel@tonic-gate off += len - resid;
37497c478bd9Sstevel@tonic-gate len = resid;
37507c478bd9Sstevel@tonic-gate }
37517c478bd9Sstevel@tonic-gate
37527c478bd9Sstevel@tonic-gate return (error);
37537c478bd9Sstevel@tonic-gate }
37547c478bd9Sstevel@tonic-gate
37557c478bd9Sstevel@tonic-gate static void
di_cache_write(struct di_cache * cache)37567c478bd9Sstevel@tonic-gate di_cache_write(struct di_cache *cache)
37577c478bd9Sstevel@tonic-gate {
37587c478bd9Sstevel@tonic-gate struct di_all *all;
37597c478bd9Sstevel@tonic-gate struct vnode *vp;
37607c478bd9Sstevel@tonic-gate int oflags;
37617c478bd9Sstevel@tonic-gate size_t map_size;
37627c478bd9Sstevel@tonic-gate size_t chunk;
37637c478bd9Sstevel@tonic-gate offset_t off;
37647c478bd9Sstevel@tonic-gate int error;
37657c478bd9Sstevel@tonic-gate char *buf;
37667c478bd9Sstevel@tonic-gate
37677c478bd9Sstevel@tonic-gate ASSERT(DI_CACHE_LOCKED(*cache));
37687c478bd9Sstevel@tonic-gate ASSERT(!servicing_interrupt());
37697c478bd9Sstevel@tonic-gate
37707c478bd9Sstevel@tonic-gate if (cache->cache_size == 0) {
37717c478bd9Sstevel@tonic-gate ASSERT(cache->cache_data == NULL);
37727c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "Empty cache. Skipping write"));
37737c478bd9Sstevel@tonic-gate return;
37747c478bd9Sstevel@tonic-gate }
37757c478bd9Sstevel@tonic-gate
37767c478bd9Sstevel@tonic-gate ASSERT(cache->cache_size > 0);
37777c478bd9Sstevel@tonic-gate ASSERT(cache->cache_data);
37787c478bd9Sstevel@tonic-gate
37797c478bd9Sstevel@tonic-gate if (!modrootloaded || rootvp == NULL || vn_is_readonly(rootvp)) {
37807c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "Can't write to rootFS. Skipping write"));
37817c478bd9Sstevel@tonic-gate return;
37827c478bd9Sstevel@tonic-gate }
37837c478bd9Sstevel@tonic-gate
37847c478bd9Sstevel@tonic-gate all = (struct di_all *)cache->cache_data;
37857c478bd9Sstevel@tonic-gate
37867c478bd9Sstevel@tonic-gate if (!header_plus_one_ok(all)) {
37877c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "Invalid header. Skipping write"));
37887c478bd9Sstevel@tonic-gate return;
37897c478bd9Sstevel@tonic-gate }
37907c478bd9Sstevel@tonic-gate
37917c478bd9Sstevel@tonic-gate ASSERT(strcmp(all->root_path, "/") == 0);
37927c478bd9Sstevel@tonic-gate
37937c478bd9Sstevel@tonic-gate /*
37947c478bd9Sstevel@tonic-gate * The cache_size is the total allocated memory for the cache.
37957c478bd9Sstevel@tonic-gate * The map_size is the actual size of valid data in the cache.
37967c478bd9Sstevel@tonic-gate * map_size may be smaller than cache_size but cannot exceed
37977c478bd9Sstevel@tonic-gate * cache_size.
37987c478bd9Sstevel@tonic-gate */
37997c478bd9Sstevel@tonic-gate if (all->map_size > cache->cache_size) {
38007c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "map_size (0x%x) > cache_size (0x%x)."
38017c478bd9Sstevel@tonic-gate " Skipping write", all->map_size, cache->cache_size));
38027c478bd9Sstevel@tonic-gate return;
38037c478bd9Sstevel@tonic-gate }
38047c478bd9Sstevel@tonic-gate
38057c478bd9Sstevel@tonic-gate /*
38067c478bd9Sstevel@tonic-gate * First unlink the temp file
38077c478bd9Sstevel@tonic-gate */
38087c478bd9Sstevel@tonic-gate error = vn_remove(DI_CACHE_TEMP, UIO_SYSSPACE, RMFILE);
38097c478bd9Sstevel@tonic-gate if (error && error != ENOENT) {
38107c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "%s: unlink failed: %d",
38117c478bd9Sstevel@tonic-gate DI_CACHE_TEMP, error));
38127c478bd9Sstevel@tonic-gate }
38137c478bd9Sstevel@tonic-gate
38147c478bd9Sstevel@tonic-gate if (error == EROFS) {
38157c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "RDONLY FS. Skipping write"));
38167c478bd9Sstevel@tonic-gate return;
38177c478bd9Sstevel@tonic-gate }
38187c478bd9Sstevel@tonic-gate
38197c478bd9Sstevel@tonic-gate vp = NULL;
38207c478bd9Sstevel@tonic-gate oflags = (FCREAT|FWRITE);
38217c478bd9Sstevel@tonic-gate if (error = vn_open(DI_CACHE_TEMP, UIO_SYSSPACE, oflags,
38227c478bd9Sstevel@tonic-gate DI_CACHE_PERMS, &vp, CRCREAT, 0)) {
38237c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "%s: create failed: %d",
38247c478bd9Sstevel@tonic-gate DI_CACHE_TEMP, error));
38257c478bd9Sstevel@tonic-gate return;
38267c478bd9Sstevel@tonic-gate }
38277c478bd9Sstevel@tonic-gate
38287c478bd9Sstevel@tonic-gate ASSERT(vp);
38297c478bd9Sstevel@tonic-gate
38307c478bd9Sstevel@tonic-gate /*
38317c478bd9Sstevel@tonic-gate * Paranoid: Check if the file is on a read-only FS
38327c478bd9Sstevel@tonic-gate */
38337c478bd9Sstevel@tonic-gate if (vn_is_readonly(vp)) {
38347c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "cannot write: readonly FS"));
38357c478bd9Sstevel@tonic-gate goto fail;
38367c478bd9Sstevel@tonic-gate }
38377c478bd9Sstevel@tonic-gate
38387c478bd9Sstevel@tonic-gate /*
38397c478bd9Sstevel@tonic-gate * Note that we only write map_size bytes to disk - this saves
38407c478bd9Sstevel@tonic-gate * space as the actual cache size may be larger than size of
38417c478bd9Sstevel@tonic-gate * valid data in the cache.
38427c478bd9Sstevel@tonic-gate * Another advantage is that it makes verification of size
38437c478bd9Sstevel@tonic-gate * easier when the file is read later.
38447c478bd9Sstevel@tonic-gate */
38457c478bd9Sstevel@tonic-gate map_size = all->map_size;
38467c478bd9Sstevel@tonic-gate off = 0;
38477c478bd9Sstevel@tonic-gate buf = cache->cache_data;
38487c478bd9Sstevel@tonic-gate
38497c478bd9Sstevel@tonic-gate while (map_size) {
38507c478bd9Sstevel@tonic-gate ASSERT(map_size > 0);
38517c478bd9Sstevel@tonic-gate /*
38527c478bd9Sstevel@tonic-gate * Write in chunks so that VM system
38537c478bd9Sstevel@tonic-gate * is not overwhelmed
38547c478bd9Sstevel@tonic-gate */
38557c478bd9Sstevel@tonic-gate if (map_size > di_chunk * PAGESIZE)
38567c478bd9Sstevel@tonic-gate chunk = di_chunk * PAGESIZE;
38577c478bd9Sstevel@tonic-gate else
38587c478bd9Sstevel@tonic-gate chunk = map_size;
38597c478bd9Sstevel@tonic-gate
38607c478bd9Sstevel@tonic-gate error = chunk_write(vp, off, buf, chunk);
38617c478bd9Sstevel@tonic-gate if (error) {
38627c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "write failed: off=0x%x: %d",
38637c478bd9Sstevel@tonic-gate off, error));
38647c478bd9Sstevel@tonic-gate goto fail;
38657c478bd9Sstevel@tonic-gate }
38667c478bd9Sstevel@tonic-gate
38677c478bd9Sstevel@tonic-gate off += chunk;
38687c478bd9Sstevel@tonic-gate buf += chunk;
38697c478bd9Sstevel@tonic-gate map_size -= chunk;
38707c478bd9Sstevel@tonic-gate
3871b9ccdc5aScth /* If low on memory, give pageout a chance to run */
3872b9ccdc5aScth if (freemem < desfree)
38737c478bd9Sstevel@tonic-gate delay(1);
38747c478bd9Sstevel@tonic-gate }
38757c478bd9Sstevel@tonic-gate
38767c478bd9Sstevel@tonic-gate /*
38777c478bd9Sstevel@tonic-gate * Now sync the file and close it
38787c478bd9Sstevel@tonic-gate */
3879da6c28aaSamw if (error = VOP_FSYNC(vp, FSYNC, kcred, NULL)) {
38807c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "FSYNC failed: %d", error));
38817c478bd9Sstevel@tonic-gate }
38827c478bd9Sstevel@tonic-gate
3883da6c28aaSamw if (error = VOP_CLOSE(vp, oflags, 1, (offset_t)0, kcred, NULL)) {
38847c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "close() failed: %d", error));
38857c478bd9Sstevel@tonic-gate VN_RELE(vp);
38867c478bd9Sstevel@tonic-gate return;
38877c478bd9Sstevel@tonic-gate }
38887c478bd9Sstevel@tonic-gate
38897c478bd9Sstevel@tonic-gate VN_RELE(vp);
38907c478bd9Sstevel@tonic-gate
38917c478bd9Sstevel@tonic-gate /*
38927c478bd9Sstevel@tonic-gate * Now do the rename
38937c478bd9Sstevel@tonic-gate */
38947c478bd9Sstevel@tonic-gate if (error = vn_rename(DI_CACHE_TEMP, DI_CACHE_FILE, UIO_SYSSPACE)) {
38957c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "rename failed: %d", error));
38967c478bd9Sstevel@tonic-gate return;
38977c478bd9Sstevel@tonic-gate }
38987c478bd9Sstevel@tonic-gate
38997c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_INFO, "Cache write successful."));
39007c478bd9Sstevel@tonic-gate
39017c478bd9Sstevel@tonic-gate return;
39027c478bd9Sstevel@tonic-gate
39037c478bd9Sstevel@tonic-gate fail:
3904da6c28aaSamw (void) VOP_CLOSE(vp, oflags, 1, (offset_t)0, kcred, NULL);
39057c478bd9Sstevel@tonic-gate VN_RELE(vp);
39067c478bd9Sstevel@tonic-gate }
39077c478bd9Sstevel@tonic-gate
39087c478bd9Sstevel@tonic-gate
39097c478bd9Sstevel@tonic-gate /*
39107c478bd9Sstevel@tonic-gate * Since we could be called early in boot,
39117c478bd9Sstevel@tonic-gate * use kobj_read_file()
39127c478bd9Sstevel@tonic-gate */
39137c478bd9Sstevel@tonic-gate static void
di_cache_read(struct di_cache * cache)39147c478bd9Sstevel@tonic-gate di_cache_read(struct di_cache *cache)
39157c478bd9Sstevel@tonic-gate {
39167c478bd9Sstevel@tonic-gate struct _buf *file;
39177c478bd9Sstevel@tonic-gate struct di_all *all;
39187c478bd9Sstevel@tonic-gate int n;
39197c478bd9Sstevel@tonic-gate size_t map_size, sz, chunk;
39207c478bd9Sstevel@tonic-gate offset_t off;
39217c478bd9Sstevel@tonic-gate caddr_t buf;
39227c478bd9Sstevel@tonic-gate uint32_t saved_crc, crc;
39237c478bd9Sstevel@tonic-gate
39247c478bd9Sstevel@tonic-gate ASSERT(modrootloaded);
39257c478bd9Sstevel@tonic-gate ASSERT(DI_CACHE_LOCKED(*cache));
39267c478bd9Sstevel@tonic-gate ASSERT(cache->cache_data == NULL);
39277c478bd9Sstevel@tonic-gate ASSERT(cache->cache_size == 0);
39287c478bd9Sstevel@tonic-gate ASSERT(!servicing_interrupt());
39297c478bd9Sstevel@tonic-gate
39307c478bd9Sstevel@tonic-gate file = kobj_open_file(DI_CACHE_FILE);
39317c478bd9Sstevel@tonic-gate if (file == (struct _buf *)-1) {
39327c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "%s: open failed: %d",
39337c478bd9Sstevel@tonic-gate DI_CACHE_FILE, ENOENT));
39347c478bd9Sstevel@tonic-gate return;
39357c478bd9Sstevel@tonic-gate }
39367c478bd9Sstevel@tonic-gate
39377c478bd9Sstevel@tonic-gate /*
39387c478bd9Sstevel@tonic-gate * Read in the header+root_path first. The root_path must be "/"
39397c478bd9Sstevel@tonic-gate */
39407c478bd9Sstevel@tonic-gate all = kmem_zalloc(sizeof (*all) + 1, KM_SLEEP);
39417c478bd9Sstevel@tonic-gate n = kobj_read_file(file, (caddr_t)all, sizeof (*all) + 1, 0);
39427c478bd9Sstevel@tonic-gate
39437c478bd9Sstevel@tonic-gate if ((n != sizeof (*all) + 1) || !header_plus_one_ok(all)) {
39447c478bd9Sstevel@tonic-gate kmem_free(all, sizeof (*all) + 1);
39457c478bd9Sstevel@tonic-gate kobj_close_file(file);
39467c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "cache header: read error or invalid"));
39477c478bd9Sstevel@tonic-gate return;
39487c478bd9Sstevel@tonic-gate }
39497c478bd9Sstevel@tonic-gate
39507c478bd9Sstevel@tonic-gate map_size = all->map_size;
39517c478bd9Sstevel@tonic-gate
39527c478bd9Sstevel@tonic-gate kmem_free(all, sizeof (*all) + 1);
39537c478bd9Sstevel@tonic-gate
39547c478bd9Sstevel@tonic-gate ASSERT(map_size >= sizeof (*all) + 1);
39557c478bd9Sstevel@tonic-gate
39567c478bd9Sstevel@tonic-gate buf = di_cache.cache_data = kmem_alloc(map_size, KM_SLEEP);
39577c478bd9Sstevel@tonic-gate sz = map_size;
39587c478bd9Sstevel@tonic-gate off = 0;
39597c478bd9Sstevel@tonic-gate while (sz) {
39607c478bd9Sstevel@tonic-gate /* Don't overload VM with large reads */
39617c478bd9Sstevel@tonic-gate chunk = (sz > di_chunk * PAGESIZE) ? di_chunk * PAGESIZE : sz;
39627c478bd9Sstevel@tonic-gate n = kobj_read_file(file, buf, chunk, off);
39637c478bd9Sstevel@tonic-gate if (n != chunk) {
39647c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "%s: read error at offset: %lld",
39657c478bd9Sstevel@tonic-gate DI_CACHE_FILE, off));
39667c478bd9Sstevel@tonic-gate goto fail;
39677c478bd9Sstevel@tonic-gate }
39687c478bd9Sstevel@tonic-gate off += chunk;
39697c478bd9Sstevel@tonic-gate buf += chunk;
39707c478bd9Sstevel@tonic-gate sz -= chunk;
39717c478bd9Sstevel@tonic-gate }
39727c478bd9Sstevel@tonic-gate
39737c478bd9Sstevel@tonic-gate ASSERT(off == map_size);
39747c478bd9Sstevel@tonic-gate
39757c478bd9Sstevel@tonic-gate /*
39767c478bd9Sstevel@tonic-gate * Read past expected EOF to verify size.
39777c478bd9Sstevel@tonic-gate */
39787c478bd9Sstevel@tonic-gate if (kobj_read_file(file, (caddr_t)&sz, 1, off) > 0) {
39797c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "%s: file size changed", DI_CACHE_FILE));
39807c478bd9Sstevel@tonic-gate goto fail;
39817c478bd9Sstevel@tonic-gate }
39827c478bd9Sstevel@tonic-gate
39837c478bd9Sstevel@tonic-gate all = (struct di_all *)di_cache.cache_data;
39847c478bd9Sstevel@tonic-gate if (!header_plus_one_ok(all)) {
39857c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "%s: file header changed", DI_CACHE_FILE));
39867c478bd9Sstevel@tonic-gate goto fail;
39877c478bd9Sstevel@tonic-gate }
39887c478bd9Sstevel@tonic-gate
39897c478bd9Sstevel@tonic-gate /*
39907c478bd9Sstevel@tonic-gate * Compute CRC with checksum field in the cache data set to 0
39917c478bd9Sstevel@tonic-gate */
39927c478bd9Sstevel@tonic-gate saved_crc = all->cache_checksum;
39937c478bd9Sstevel@tonic-gate all->cache_checksum = 0;
39947c478bd9Sstevel@tonic-gate CRC32(crc, di_cache.cache_data, map_size, -1U, crc32_table);
39957c478bd9Sstevel@tonic-gate all->cache_checksum = saved_crc;
39967c478bd9Sstevel@tonic-gate
39977c478bd9Sstevel@tonic-gate if (crc != all->cache_checksum) {
39987c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR,
39997c478bd9Sstevel@tonic-gate "%s: checksum error: expected=0x%x actual=0x%x",
40007c478bd9Sstevel@tonic-gate DI_CACHE_FILE, all->cache_checksum, crc));
40017c478bd9Sstevel@tonic-gate goto fail;
40027c478bd9Sstevel@tonic-gate }
40037c478bd9Sstevel@tonic-gate
40047c478bd9Sstevel@tonic-gate if (all->map_size != map_size) {
40057c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "%s: map size changed", DI_CACHE_FILE));
40067c478bd9Sstevel@tonic-gate goto fail;
40077c478bd9Sstevel@tonic-gate }
40087c478bd9Sstevel@tonic-gate
40097c478bd9Sstevel@tonic-gate kobj_close_file(file);
40107c478bd9Sstevel@tonic-gate
40117c478bd9Sstevel@tonic-gate di_cache.cache_size = map_size;
40127c478bd9Sstevel@tonic-gate
40137c478bd9Sstevel@tonic-gate return;
40147c478bd9Sstevel@tonic-gate
40157c478bd9Sstevel@tonic-gate fail:
40167c478bd9Sstevel@tonic-gate kmem_free(di_cache.cache_data, map_size);
40177c478bd9Sstevel@tonic-gate kobj_close_file(file);
40187c478bd9Sstevel@tonic-gate di_cache.cache_data = NULL;
40197c478bd9Sstevel@tonic-gate di_cache.cache_size = 0;
40207c478bd9Sstevel@tonic-gate }
40217c478bd9Sstevel@tonic-gate
40227c478bd9Sstevel@tonic-gate
40237c478bd9Sstevel@tonic-gate /*
40247c478bd9Sstevel@tonic-gate * Checks if arguments are valid for using the cache.
40257c478bd9Sstevel@tonic-gate */
40267c478bd9Sstevel@tonic-gate static int
cache_args_valid(struct di_state * st,int * error)40277c478bd9Sstevel@tonic-gate cache_args_valid(struct di_state *st, int *error)
40287c478bd9Sstevel@tonic-gate {
40297c478bd9Sstevel@tonic-gate ASSERT(error);
40307c478bd9Sstevel@tonic-gate ASSERT(st->mem_size > 0);
40317c478bd9Sstevel@tonic-gate ASSERT(st->memlist != NULL);
40327c478bd9Sstevel@tonic-gate
40337c478bd9Sstevel@tonic-gate if (!modrootloaded || !i_ddi_io_initialized()) {
40347c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR,
40357c478bd9Sstevel@tonic-gate "cache lookup failure: I/O subsystem not inited"));
40367c478bd9Sstevel@tonic-gate *error = ENOTACTIVE;
40377c478bd9Sstevel@tonic-gate return (0);
40387c478bd9Sstevel@tonic-gate }
40397c478bd9Sstevel@tonic-gate
40407c478bd9Sstevel@tonic-gate /*
40417c478bd9Sstevel@tonic-gate * No other flags allowed with DINFOCACHE
40427c478bd9Sstevel@tonic-gate */
40437c478bd9Sstevel@tonic-gate if (st->command != (DINFOCACHE & DIIOC_MASK)) {
40447c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR,
40457c478bd9Sstevel@tonic-gate "cache lookup failure: bad flags: 0x%x",
40467c478bd9Sstevel@tonic-gate st->command));
40477c478bd9Sstevel@tonic-gate *error = EINVAL;
40487c478bd9Sstevel@tonic-gate return (0);
40497c478bd9Sstevel@tonic-gate }
40507c478bd9Sstevel@tonic-gate
40517c478bd9Sstevel@tonic-gate if (strcmp(DI_ALL_PTR(st)->root_path, "/") != 0) {
40527c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR,
40537c478bd9Sstevel@tonic-gate "cache lookup failure: bad root: %s",
40547c478bd9Sstevel@tonic-gate DI_ALL_PTR(st)->root_path));
40557c478bd9Sstevel@tonic-gate *error = EINVAL;
40567c478bd9Sstevel@tonic-gate return (0);
40577c478bd9Sstevel@tonic-gate }
40587c478bd9Sstevel@tonic-gate
40597c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_INFO, "cache lookup args ok: 0x%x", st->command));
40607c478bd9Sstevel@tonic-gate
40617c478bd9Sstevel@tonic-gate *error = 0;
40627c478bd9Sstevel@tonic-gate
40637c478bd9Sstevel@tonic-gate return (1);
40647c478bd9Sstevel@tonic-gate }
40657c478bd9Sstevel@tonic-gate
40667c478bd9Sstevel@tonic-gate static int
snapshot_is_cacheable(struct di_state * st)40677c478bd9Sstevel@tonic-gate snapshot_is_cacheable(struct di_state *st)
40687c478bd9Sstevel@tonic-gate {
40697c478bd9Sstevel@tonic-gate ASSERT(st->mem_size > 0);
40707c478bd9Sstevel@tonic-gate ASSERT(st->memlist != NULL);
40717c478bd9Sstevel@tonic-gate
40723c34adc5Sramat if ((st->command & DI_CACHE_SNAPSHOT_FLAGS) !=
40733c34adc5Sramat (DI_CACHE_SNAPSHOT_FLAGS & DIIOC_MASK)) {
40747c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_INFO,
40757c478bd9Sstevel@tonic-gate "not cacheable: incompatible flags: 0x%x",
40767c478bd9Sstevel@tonic-gate st->command));
40777c478bd9Sstevel@tonic-gate return (0);
40787c478bd9Sstevel@tonic-gate }
40797c478bd9Sstevel@tonic-gate
40807c478bd9Sstevel@tonic-gate if (strcmp(DI_ALL_PTR(st)->root_path, "/") != 0) {
40817c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_INFO,
40827c478bd9Sstevel@tonic-gate "not cacheable: incompatible root path: %s",
40837c478bd9Sstevel@tonic-gate DI_ALL_PTR(st)->root_path));
40847c478bd9Sstevel@tonic-gate return (0);
40857c478bd9Sstevel@tonic-gate }
40867c478bd9Sstevel@tonic-gate
40877c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_INFO, "cacheable snapshot request: 0x%x", st->command));
40887c478bd9Sstevel@tonic-gate
40897c478bd9Sstevel@tonic-gate return (1);
40907c478bd9Sstevel@tonic-gate }
40917c478bd9Sstevel@tonic-gate
40927c478bd9Sstevel@tonic-gate static int
di_cache_lookup(struct di_state * st)40937c478bd9Sstevel@tonic-gate di_cache_lookup(struct di_state *st)
40947c478bd9Sstevel@tonic-gate {
40957c478bd9Sstevel@tonic-gate size_t rval;
40967c478bd9Sstevel@tonic-gate int cache_valid;
40977c478bd9Sstevel@tonic-gate
40987c478bd9Sstevel@tonic-gate ASSERT(cache_args_valid(st, &cache_valid));
40997c478bd9Sstevel@tonic-gate ASSERT(modrootloaded);
41007c478bd9Sstevel@tonic-gate
41017c478bd9Sstevel@tonic-gate DI_CACHE_LOCK(di_cache);
41027c478bd9Sstevel@tonic-gate
41037c478bd9Sstevel@tonic-gate /*
41047c478bd9Sstevel@tonic-gate * The following assignment determines the validity
41057c478bd9Sstevel@tonic-gate * of the cache as far as this snapshot is concerned.
41067c478bd9Sstevel@tonic-gate */
41077c478bd9Sstevel@tonic-gate cache_valid = di_cache.cache_valid;
41087c478bd9Sstevel@tonic-gate
41097c478bd9Sstevel@tonic-gate if (cache_valid && di_cache.cache_data == NULL) {
41107c478bd9Sstevel@tonic-gate di_cache_read(&di_cache);
41117c478bd9Sstevel@tonic-gate /* check for read or file error */
41127c478bd9Sstevel@tonic-gate if (di_cache.cache_data == NULL)
41137c478bd9Sstevel@tonic-gate cache_valid = 0;
41147c478bd9Sstevel@tonic-gate }
41157c478bd9Sstevel@tonic-gate
41167c478bd9Sstevel@tonic-gate if (cache_valid) {
41177c478bd9Sstevel@tonic-gate /*
41187c478bd9Sstevel@tonic-gate * Ok, the cache was valid as of this particular
41197c478bd9Sstevel@tonic-gate * snapshot. Copy the cached snapshot. This is safe
41207c478bd9Sstevel@tonic-gate * to do as the cache cannot be freed (we hold the
41217c478bd9Sstevel@tonic-gate * cache lock). Free the memory allocated in di_state
41227c478bd9Sstevel@tonic-gate * up until this point - we will simply copy everything
41237c478bd9Sstevel@tonic-gate * in the cache.
41247c478bd9Sstevel@tonic-gate */
41257c478bd9Sstevel@tonic-gate
41267c478bd9Sstevel@tonic-gate ASSERT(di_cache.cache_data != NULL);
41277c478bd9Sstevel@tonic-gate ASSERT(di_cache.cache_size > 0);
41287c478bd9Sstevel@tonic-gate
41297c478bd9Sstevel@tonic-gate di_freemem(st);
41307c478bd9Sstevel@tonic-gate
41317c478bd9Sstevel@tonic-gate rval = 0;
41327c478bd9Sstevel@tonic-gate if (di_cache2mem(&di_cache, st) > 0) {
41337c478bd9Sstevel@tonic-gate /*
41347c478bd9Sstevel@tonic-gate * map_size is size of valid data in the
41357c478bd9Sstevel@tonic-gate * cached snapshot and may be less than
41367c478bd9Sstevel@tonic-gate * size of the cache.
41377c478bd9Sstevel@tonic-gate */
4138b9ccdc5aScth ASSERT(DI_ALL_PTR(st));
41397c478bd9Sstevel@tonic-gate rval = DI_ALL_PTR(st)->map_size;
41407c478bd9Sstevel@tonic-gate
41417c478bd9Sstevel@tonic-gate ASSERT(rval >= sizeof (struct di_all));
41427c478bd9Sstevel@tonic-gate ASSERT(rval <= di_cache.cache_size);
41437c478bd9Sstevel@tonic-gate }
41447c478bd9Sstevel@tonic-gate } else {
41457c478bd9Sstevel@tonic-gate /*
41467c478bd9Sstevel@tonic-gate * The cache isn't valid, we need to take a snapshot.
41477c478bd9Sstevel@tonic-gate * Set the command flags appropriately
41487c478bd9Sstevel@tonic-gate */
41497c478bd9Sstevel@tonic-gate ASSERT(st->command == (DINFOCACHE & DIIOC_MASK));
41507c478bd9Sstevel@tonic-gate st->command = (DI_CACHE_SNAPSHOT_FLAGS & DIIOC_MASK);
41517c478bd9Sstevel@tonic-gate rval = di_cache_update(st);
41527c478bd9Sstevel@tonic-gate st->command = (DINFOCACHE & DIIOC_MASK);
41537c478bd9Sstevel@tonic-gate }
41547c478bd9Sstevel@tonic-gate
41557c478bd9Sstevel@tonic-gate DI_CACHE_UNLOCK(di_cache);
41567c478bd9Sstevel@tonic-gate
41577c478bd9Sstevel@tonic-gate /*
41587c478bd9Sstevel@tonic-gate * For cached snapshots, the devinfo driver always returns
41597c478bd9Sstevel@tonic-gate * a snapshot rooted at "/".
41607c478bd9Sstevel@tonic-gate */
41617c478bd9Sstevel@tonic-gate ASSERT(rval == 0 || strcmp(DI_ALL_PTR(st)->root_path, "/") == 0);
41627c478bd9Sstevel@tonic-gate
4163bc1009abSjg return ((int)rval);
41647c478bd9Sstevel@tonic-gate }
41657c478bd9Sstevel@tonic-gate
41667c478bd9Sstevel@tonic-gate /*
41677c478bd9Sstevel@tonic-gate * This is a forced update of the cache - the previous state of the cache
41687c478bd9Sstevel@tonic-gate * may be:
41697c478bd9Sstevel@tonic-gate * - unpopulated
41707c478bd9Sstevel@tonic-gate * - populated and invalid
41717c478bd9Sstevel@tonic-gate * - populated and valid
41727c478bd9Sstevel@tonic-gate */
41737c478bd9Sstevel@tonic-gate static int
di_cache_update(struct di_state * st)41747c478bd9Sstevel@tonic-gate di_cache_update(struct di_state *st)
41757c478bd9Sstevel@tonic-gate {
41767c478bd9Sstevel@tonic-gate int rval;
41777c478bd9Sstevel@tonic-gate uint32_t crc;
41787c478bd9Sstevel@tonic-gate struct di_all *all;
41797c478bd9Sstevel@tonic-gate
41807c478bd9Sstevel@tonic-gate ASSERT(DI_CACHE_LOCKED(di_cache));
41817c478bd9Sstevel@tonic-gate ASSERT(snapshot_is_cacheable(st));
41827c478bd9Sstevel@tonic-gate
41837c478bd9Sstevel@tonic-gate /*
41847c478bd9Sstevel@tonic-gate * Free the in-core cache and the on-disk file (if they exist)
41857c478bd9Sstevel@tonic-gate */
41867c478bd9Sstevel@tonic-gate i_ddi_di_cache_free(&di_cache);
41877c478bd9Sstevel@tonic-gate
41887c478bd9Sstevel@tonic-gate /*
41897c478bd9Sstevel@tonic-gate * Set valid flag before taking the snapshot,
41907c478bd9Sstevel@tonic-gate * so that any invalidations that arrive
41917c478bd9Sstevel@tonic-gate * during or after the snapshot are not
41927c478bd9Sstevel@tonic-gate * removed by us.
41937c478bd9Sstevel@tonic-gate */
41947c478bd9Sstevel@tonic-gate atomic_or_32(&di_cache.cache_valid, 1);
41957c478bd9Sstevel@tonic-gate
41963c34adc5Sramat rval = di_snapshot_and_clean(st);
41977c478bd9Sstevel@tonic-gate
41987c478bd9Sstevel@tonic-gate if (rval == 0) {
41997c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "can't update cache: bad snapshot"));
42007c478bd9Sstevel@tonic-gate return (0);
42017c478bd9Sstevel@tonic-gate }
42027c478bd9Sstevel@tonic-gate
42037c478bd9Sstevel@tonic-gate DI_ALL_PTR(st)->map_size = rval;
42047c478bd9Sstevel@tonic-gate if (di_mem2cache(st, &di_cache) == 0) {
42057c478bd9Sstevel@tonic-gate CACHE_DEBUG((DI_ERR, "can't update cache: copy failed"));
42067c478bd9Sstevel@tonic-gate return (0);
42077c478bd9Sstevel@tonic-gate }
42087c478bd9Sstevel@tonic-gate
42097c478bd9Sstevel@tonic-gate ASSERT(di_cache.cache_data);
42107c478bd9Sstevel@tonic-gate ASSERT(di_cache.cache_size > 0);
42117c478bd9Sstevel@tonic-gate
42127c478bd9Sstevel@tonic-gate /*
42137c478bd9Sstevel@tonic-gate * Now that we have cached the snapshot, compute its checksum.
42147c478bd9Sstevel@tonic-gate * The checksum is only computed over the valid data in the
42157c478bd9Sstevel@tonic-gate * cache, not the entire cache.
42167c478bd9Sstevel@tonic-gate * Also, set all the fields (except checksum) before computing
42177c478bd9Sstevel@tonic-gate * checksum.
42187c478bd9Sstevel@tonic-gate */
42197c478bd9Sstevel@tonic-gate all = (struct di_all *)di_cache.cache_data;
42207c478bd9Sstevel@tonic-gate all->cache_magic = DI_CACHE_MAGIC;
42217c478bd9Sstevel@tonic-gate all->map_size = rval;
42227c478bd9Sstevel@tonic-gate
42237c478bd9Sstevel@tonic-gate ASSERT(all->cache_checksum == 0);
42247c478bd9Sstevel@tonic-gate CRC32(crc, di_cache.cache_data, all->map_size, -1U, crc32_table);
42257c478bd9Sstevel@tonic-gate all->cache_checksum = crc;
42267c478bd9Sstevel@tonic-gate
42277c478bd9Sstevel@tonic-gate di_cache_write(&di_cache);
42287c478bd9Sstevel@tonic-gate
42297c478bd9Sstevel@tonic-gate return (rval);
42307c478bd9Sstevel@tonic-gate }
42317c478bd9Sstevel@tonic-gate
42327c478bd9Sstevel@tonic-gate static void
di_cache_print(di_cache_debug_t msglevel,char * fmt,...)42337c478bd9Sstevel@tonic-gate di_cache_print(di_cache_debug_t msglevel, char *fmt, ...)
42347c478bd9Sstevel@tonic-gate {
42357c478bd9Sstevel@tonic-gate va_list ap;
42367c478bd9Sstevel@tonic-gate
42377c478bd9Sstevel@tonic-gate if (di_cache_debug <= DI_QUIET)
42387c478bd9Sstevel@tonic-gate return;
42397c478bd9Sstevel@tonic-gate
42407c478bd9Sstevel@tonic-gate if (di_cache_debug < msglevel)
42417c478bd9Sstevel@tonic-gate return;
42427c478bd9Sstevel@tonic-gate
42437c478bd9Sstevel@tonic-gate switch (msglevel) {
42447c478bd9Sstevel@tonic-gate case DI_ERR:
42457c478bd9Sstevel@tonic-gate msglevel = CE_WARN;
42467c478bd9Sstevel@tonic-gate break;
42477c478bd9Sstevel@tonic-gate case DI_INFO:
42487c478bd9Sstevel@tonic-gate case DI_TRACE:
42497c478bd9Sstevel@tonic-gate default:
42507c478bd9Sstevel@tonic-gate msglevel = CE_NOTE;
42517c478bd9Sstevel@tonic-gate break;
42527c478bd9Sstevel@tonic-gate }
42537c478bd9Sstevel@tonic-gate
42547c478bd9Sstevel@tonic-gate va_start(ap, fmt);
42557c478bd9Sstevel@tonic-gate vcmn_err(msglevel, fmt, ap);
42567c478bd9Sstevel@tonic-gate va_end(ap);
42577c478bd9Sstevel@tonic-gate }
425826947304SEvan Yan
425926947304SEvan Yan static void
di_hotplug_children(struct di_state * st)426026947304SEvan Yan di_hotplug_children(struct di_state *st)
426126947304SEvan Yan {
426226947304SEvan Yan di_off_t off;
426326947304SEvan Yan struct di_hp *hp;
426426947304SEvan Yan struct i_hp *hp_list_node;
426526947304SEvan Yan
426626947304SEvan Yan while (hp_list_node = (struct i_hp *)list_remove_head(&st->hp_list)) {
426726947304SEvan Yan
426826947304SEvan Yan if ((hp_list_node->hp_child != NULL) &&
426926947304SEvan Yan (di_dip_find(st, hp_list_node->hp_child, &off) == 0)) {
427026947304SEvan Yan hp = DI_HP(di_mem_addr(st, hp_list_node->hp_off));
427126947304SEvan Yan hp->hp_child = off;
427226947304SEvan Yan }
427326947304SEvan Yan
427426947304SEvan Yan kmem_free(hp_list_node, sizeof (i_hp_t));
427526947304SEvan Yan }
427626947304SEvan Yan
427726947304SEvan Yan list_destroy(&st->hp_list);
427826947304SEvan Yan }
4279