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
5144dfaa9Scth * Common Development and Distribution License (the "License").
6144dfaa9Scth * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22bf002425SStephen Hanson * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
231c79aca5SAlexander Eremin * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
24cd21e7c5SGarrett D'Amore * Copyright 2012 Garrett D'Amore <garrett@damore.org>. All rights reserved.
2539cddb10SJoshua M. Clulow * Copyright (c) 2013, Joyent, Inc. All rights reserved.
2648bbca81SDaniel Hoffman * Copyright (c) 2016 by Delphix. All rights reserved.
2730c304d9SJoshua M. Clulow * Copyright 2020 Joshua M. Clulow <josh@sysmgr.org>
28d27aea3fSDan Cross * Copyright 2023 Oxide Computer Company
291c79aca5SAlexander Eremin */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include <sys/note.h>
327c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
337c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
347c478bd9Sstevel@tonic-gate #include <sys/instance.h>
357c478bd9Sstevel@tonic-gate #include <sys/conf.h>
367c478bd9Sstevel@tonic-gate #include <sys/stat.h>
377c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/hwconf.h>
397c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
407c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
417c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
427c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
437c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
4425e8c5aaSvikram #include <sys/contract/device_impl.h>
457c478bd9Sstevel@tonic-gate #include <sys/dacf.h>
467c478bd9Sstevel@tonic-gate #include <sys/promif.h>
4719397407SSherry Moore #include <sys/pci.h>
487c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
497c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
507c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
517c478bd9Sstevel@tonic-gate #include <sys/sysevent.h>
527c478bd9Sstevel@tonic-gate #include <sys/sunmdi.h>
537c478bd9Sstevel@tonic-gate #include <sys/stream.h>
547c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
557c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h>
567c478bd9Sstevel@tonic-gate #include <sys/fs/dv_node.h>
57facf4a8dSllai1 #include <sys/reboot.h>
5825e8c5aaSvikram #include <sys/sysmacros.h>
5919397407SSherry Moore #include <sys/systm.h>
603c5e027bSEric Taylor #include <sys/fs/sdev_impl.h>
6125e8c5aaSvikram #include <sys/sunldi.h>
6225e8c5aaSvikram #include <sys/sunldi_impl.h>
63dedec472SJack Meng #include <sys/bootprops.h>
6494c894bbSVikram Hegde #include <sys/varargs.h>
6594c894bbSVikram Hegde #include <sys/modhash.h>
6694c894bbSVikram Hegde #include <sys/instance.h>
6730c304d9SJoshua M. Clulow #include <sys/sysevent/eventdefs.h>
68dedec472SJack Meng
693a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
7009011d40SVikram Hegde #include <sys/iommulib.h>
7109011d40SVikram Hegde #endif
723a634bfcSVikram Hegde
737c478bd9Sstevel@tonic-gate #ifdef DEBUG
747c478bd9Sstevel@tonic-gate int ddidebug = DDI_AUDIT;
757c478bd9Sstevel@tonic-gate #else
767c478bd9Sstevel@tonic-gate int ddidebug = 0;
777c478bd9Sstevel@tonic-gate #endif
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate #define MT_CONFIG_OP 0
807c478bd9Sstevel@tonic-gate #define MT_UNCONFIG_OP 1
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate /* Multi-threaded configuration */
837c478bd9Sstevel@tonic-gate struct mt_config_handle {
847c478bd9Sstevel@tonic-gate kmutex_t mtc_lock;
857c478bd9Sstevel@tonic-gate kcondvar_t mtc_cv;
867c478bd9Sstevel@tonic-gate int mtc_thr_count;
877c478bd9Sstevel@tonic-gate dev_info_t *mtc_pdip; /* parent dip for mt_config_children */
887c478bd9Sstevel@tonic-gate dev_info_t **mtc_fdip; /* "a" dip where unconfigure failed */
897c478bd9Sstevel@tonic-gate major_t mtc_parmajor; /* parent major for mt_config_driver */
907c478bd9Sstevel@tonic-gate major_t mtc_major;
917c478bd9Sstevel@tonic-gate int mtc_flags;
927c478bd9Sstevel@tonic-gate int mtc_op; /* config or unconfig */
937c478bd9Sstevel@tonic-gate int mtc_error; /* operation error */
947c478bd9Sstevel@tonic-gate struct brevq_node **mtc_brevqp; /* outstanding branch events queue */
957c478bd9Sstevel@tonic-gate #ifdef DEBUG
967c478bd9Sstevel@tonic-gate int total_time;
977c478bd9Sstevel@tonic-gate timestruc_t start_time;
987c478bd9Sstevel@tonic-gate #endif /* DEBUG */
997c478bd9Sstevel@tonic-gate };
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate struct devi_nodeid {
102fa9e4066Sahrens pnode_t nodeid;
1037c478bd9Sstevel@tonic-gate dev_info_t *dip;
1047c478bd9Sstevel@tonic-gate struct devi_nodeid *next;
1057c478bd9Sstevel@tonic-gate };
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate struct devi_nodeid_list {
1087c478bd9Sstevel@tonic-gate kmutex_t dno_lock; /* Protects other fields */
1097c478bd9Sstevel@tonic-gate struct devi_nodeid *dno_head; /* list of devi nodeid elements */
1107c478bd9Sstevel@tonic-gate struct devi_nodeid *dno_free; /* Free list */
1117c478bd9Sstevel@tonic-gate uint_t dno_list_length; /* number of dips in list */
1127c478bd9Sstevel@tonic-gate };
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate /* used to keep track of branch remove events to be generated */
1157c478bd9Sstevel@tonic-gate struct brevq_node {
116245c82d9Scth char *brn_deviname;
117245c82d9Scth struct brevq_node *brn_sibling;
118245c82d9Scth struct brevq_node *brn_child;
1197c478bd9Sstevel@tonic-gate };
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate static struct devi_nodeid_list devi_nodeid_list;
1227c478bd9Sstevel@tonic-gate static struct devi_nodeid_list *devimap = &devi_nodeid_list;
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate * Well known nodes which are attached first at boot time.
1267c478bd9Sstevel@tonic-gate */
1277c478bd9Sstevel@tonic-gate dev_info_t *top_devinfo; /* root of device tree */
1287c478bd9Sstevel@tonic-gate dev_info_t *options_dip;
1297c478bd9Sstevel@tonic-gate dev_info_t *pseudo_dip;
1307c478bd9Sstevel@tonic-gate dev_info_t *clone_dip;
1317c478bd9Sstevel@tonic-gate dev_info_t *scsi_vhci_dip; /* MPXIO dip */
1327c478bd9Sstevel@tonic-gate major_t clone_major;
1337c478bd9Sstevel@tonic-gate
134facf4a8dSllai1 /*
135facf4a8dSllai1 * A non-global zone's /dev is derived from the device tree.
136facf4a8dSllai1 * This generation number serves to indicate when a zone's
137facf4a8dSllai1 * /dev may need to be updated.
138facf4a8dSllai1 */
139facf4a8dSllai1 volatile ulong_t devtree_gen; /* generation number */
140facf4a8dSllai1
1417c478bd9Sstevel@tonic-gate /* block all future dev_info state changes */
14219397407SSherry Moore hrtime_t volatile devinfo_freeze = 0;
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate /* number of dev_info attaches/detaches currently in progress */
1457c478bd9Sstevel@tonic-gate static ulong_t devinfo_attach_detach = 0;
1467c478bd9Sstevel@tonic-gate
147c3b4ae18SJerry Gilliam extern int sys_shutdown;
1487c478bd9Sstevel@tonic-gate extern kmutex_t global_vhci_lock;
1497c478bd9Sstevel@tonic-gate
150facf4a8dSllai1 /* bitset of DS_SYSAVAIL & DS_RECONFIG - no races, no lock */
151facf4a8dSllai1 static int devname_state = 0;
152facf4a8dSllai1
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate * The devinfo snapshot cache and related variables.
1557c478bd9Sstevel@tonic-gate * The only field in the di_cache structure that needs initialization
1567c478bd9Sstevel@tonic-gate * is the mutex (cache_lock). However, since this is an adaptive mutex
1577c478bd9Sstevel@tonic-gate * (MUTEX_DEFAULT) - it is automatically initialized by being allocated
1587c478bd9Sstevel@tonic-gate * in zeroed memory (static storage class). Therefore no explicit
1597c478bd9Sstevel@tonic-gate * initialization of the di_cache structure is needed.
1607c478bd9Sstevel@tonic-gate */
1617c478bd9Sstevel@tonic-gate struct di_cache di_cache = {1};
1627c478bd9Sstevel@tonic-gate int di_cache_debug = 0;
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate /* For ddvis, which needs pseudo children under PCI */
1657c478bd9Sstevel@tonic-gate int pci_allow_pseudo_children = 0;
1667c478bd9Sstevel@tonic-gate
167f4da9be0Scth /* Allow path-oriented alias driver binding on driver.conf enumerated nodes */
168f4da9be0Scth int driver_conf_allow_path_alias = 1;
169f4da9be0Scth
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate * The following switch is for service people, in case a
1727c478bd9Sstevel@tonic-gate * 3rd party driver depends on identify(9e) being called.
1737c478bd9Sstevel@tonic-gate */
1747c478bd9Sstevel@tonic-gate int identify_9e = 0;
1757c478bd9Sstevel@tonic-gate
176e58a33b6SStephen Hanson /*
177e58a33b6SStephen Hanson * Add flag so behaviour of preventing attach for retired persistant nodes
178e58a33b6SStephen Hanson * can be disabled.
179e58a33b6SStephen Hanson */
180e58a33b6SStephen Hanson int retire_prevents_attach = 1;
181e58a33b6SStephen Hanson
1827c478bd9Sstevel@tonic-gate int mtc_off; /* turn off mt config */
1837c478bd9Sstevel@tonic-gate
18419397407SSherry Moore int quiesce_debug = 0;
18519397407SSherry Moore
18694c894bbSVikram Hegde boolean_t ddi_aliases_present = B_FALSE;
18794c894bbSVikram Hegde ddi_alias_t ddi_aliases;
18894c894bbSVikram Hegde uint_t tsd_ddi_redirect;
18994c894bbSVikram Hegde
19094c894bbSVikram Hegde #define DDI_ALIAS_HASH_SIZE (2700)
19194c894bbSVikram Hegde
1927c478bd9Sstevel@tonic-gate static kmem_cache_t *ddi_node_cache; /* devinfo node cache */
1937c478bd9Sstevel@tonic-gate static devinfo_log_header_t *devinfo_audit_log; /* devinfo log */
1947c478bd9Sstevel@tonic-gate static int devinfo_log_size; /* size in pages */
1957c478bd9Sstevel@tonic-gate
19694c894bbSVikram Hegde boolean_t ddi_err_panic = B_FALSE;
19794c894bbSVikram Hegde
1987c478bd9Sstevel@tonic-gate static int lookup_compatible(dev_info_t *, uint_t);
1997c478bd9Sstevel@tonic-gate static char *encode_composite_string(char **, uint_t, size_t *, uint_t);
2007c478bd9Sstevel@tonic-gate static void link_to_driver_list(dev_info_t *);
2017c478bd9Sstevel@tonic-gate static void unlink_from_driver_list(dev_info_t *);
2027c478bd9Sstevel@tonic-gate static void add_to_dn_list(struct devnames *, dev_info_t *);
2037c478bd9Sstevel@tonic-gate static void remove_from_dn_list(struct devnames *, dev_info_t *);
2047c478bd9Sstevel@tonic-gate static dev_info_t *find_duplicate_child();
2057c478bd9Sstevel@tonic-gate static void add_global_props(dev_info_t *);
2067c478bd9Sstevel@tonic-gate static void remove_global_props(dev_info_t *);
2077c478bd9Sstevel@tonic-gate static int uninit_node(dev_info_t *);
2087c478bd9Sstevel@tonic-gate static void da_log_init(void);
2097c478bd9Sstevel@tonic-gate static void da_log_enter(dev_info_t *);
2107c478bd9Sstevel@tonic-gate static int walk_devs(dev_info_t *, int (*f)(dev_info_t *, void *), void *, int);
2117c478bd9Sstevel@tonic-gate static int reset_nexus_flags(dev_info_t *, void *);
2127c478bd9Sstevel@tonic-gate static void ddi_optimize_dtree(dev_info_t *);
2137c478bd9Sstevel@tonic-gate static int is_leaf_node(dev_info_t *);
2147c478bd9Sstevel@tonic-gate static struct mt_config_handle *mt_config_init(dev_info_t *, dev_info_t **,
2157c478bd9Sstevel@tonic-gate int, major_t, int, struct brevq_node **);
2167c478bd9Sstevel@tonic-gate static void mt_config_children(struct mt_config_handle *);
2177c478bd9Sstevel@tonic-gate static void mt_config_driver(struct mt_config_handle *);
2187c478bd9Sstevel@tonic-gate static int mt_config_fini(struct mt_config_handle *);
2197c478bd9Sstevel@tonic-gate static int devi_unconfig_common(dev_info_t *, dev_info_t **, int, major_t,
2207c478bd9Sstevel@tonic-gate struct brevq_node **);
2217c478bd9Sstevel@tonic-gate static int
2227c478bd9Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
2237c478bd9Sstevel@tonic-gate dev_info_t **childp, int flags);
2249d3d2ed0Shiremath static void i_link_vhci_node(dev_info_t *);
225d27aea3fSDan Cross static void ndi_devi_exit_and_wait(dev_info_t *dip, clock_t end_time);
226f4da9be0Scth static int ndi_devi_unbind_driver(dev_info_t *dip);
2277c478bd9Sstevel@tonic-gate
228e58a33b6SStephen Hanson static int i_ddi_check_retire(dev_info_t *dip);
22925e8c5aaSvikram
23019397407SSherry Moore static void quiesce_one_device(dev_info_t *, void *);
23125e8c5aaSvikram
23294c894bbSVikram Hegde dev_info_t *ddi_alias_redirect(char *alias);
23394c894bbSVikram Hegde char *ddi_curr_redirect(char *currpath);
23494c894bbSVikram Hegde
23594c894bbSVikram Hegde
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate * dev_info cache and node management
2387c478bd9Sstevel@tonic-gate */
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate /* initialize dev_info node cache */
2417c478bd9Sstevel@tonic-gate void
i_ddi_node_cache_init()2427c478bd9Sstevel@tonic-gate i_ddi_node_cache_init()
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate ASSERT(ddi_node_cache == NULL);
2457c478bd9Sstevel@tonic-gate ddi_node_cache = kmem_cache_create("dev_info_node_cache",
2467c478bd9Sstevel@tonic-gate sizeof (struct dev_info), 0, NULL, NULL, NULL, NULL, NULL, 0);
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate if (ddidebug & DDI_AUDIT)
2497c478bd9Sstevel@tonic-gate da_log_init();
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate
252fc256490SJason Beloro
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate * Allocating a dev_info node, callable from interrupt context with KM_NOSLEEP
2557c478bd9Sstevel@tonic-gate * The allocated node has a reference count of 0.
2567c478bd9Sstevel@tonic-gate */
2577c478bd9Sstevel@tonic-gate dev_info_t *
i_ddi_alloc_node(dev_info_t * pdip,const char * node_name,pnode_t nodeid,int instance,ddi_prop_t * sys_prop,int flag)2586205b5c4SRobert Mustacchi i_ddi_alloc_node(dev_info_t *pdip, const char *node_name, pnode_t nodeid,
2597c478bd9Sstevel@tonic-gate int instance, ddi_prop_t *sys_prop, int flag)
2607c478bd9Sstevel@tonic-gate {
2617c478bd9Sstevel@tonic-gate struct dev_info *devi;
2627c478bd9Sstevel@tonic-gate struct devi_nodeid *elem;
2637c478bd9Sstevel@tonic-gate static char failed[] = "i_ddi_alloc_node: out of memory";
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate ASSERT(node_name != NULL);
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate if ((devi = kmem_cache_alloc(ddi_node_cache, flag)) == NULL) {
2687c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, failed);
2697c478bd9Sstevel@tonic-gate return (NULL);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate
2727c478bd9Sstevel@tonic-gate bzero(devi, sizeof (struct dev_info));
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate if (devinfo_audit_log) {
2757c478bd9Sstevel@tonic-gate devi->devi_audit = kmem_zalloc(sizeof (devinfo_audit_t), flag);
2767c478bd9Sstevel@tonic-gate if (devi->devi_audit == NULL)
2777c478bd9Sstevel@tonic-gate goto fail;
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate if ((devi->devi_node_name = i_ddi_strdup(node_name, flag)) == NULL)
2817c478bd9Sstevel@tonic-gate goto fail;
282f4da9be0Scth
2837c478bd9Sstevel@tonic-gate /* default binding name is node name */
2847c478bd9Sstevel@tonic-gate devi->devi_binding_name = devi->devi_node_name;
285a204de77Scth devi->devi_major = DDI_MAJOR_T_NONE; /* unbound by default */
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate /*
2887c478bd9Sstevel@tonic-gate * Make a copy of system property
2897c478bd9Sstevel@tonic-gate */
2907c478bd9Sstevel@tonic-gate if (sys_prop &&
2917c478bd9Sstevel@tonic-gate (devi->devi_sys_prop_ptr = i_ddi_prop_list_dup(sys_prop, flag))
2927c478bd9Sstevel@tonic-gate == NULL)
2937c478bd9Sstevel@tonic-gate goto fail;
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate /*
2967c478bd9Sstevel@tonic-gate * Assign devi_nodeid, devi_node_class, devi_node_attributes
2977c478bd9Sstevel@tonic-gate * according to the following algorithm:
2987c478bd9Sstevel@tonic-gate *
2997c478bd9Sstevel@tonic-gate * nodeid arg node class node attributes
3007c478bd9Sstevel@tonic-gate *
3017c478bd9Sstevel@tonic-gate * DEVI_PSEUDO_NODEID DDI_NC_PSEUDO A
3027c478bd9Sstevel@tonic-gate * DEVI_SID_NODEID DDI_NC_PSEUDO A,P
303027021c7SChris Horne * DEVI_SID_HIDDEN_NODEID DDI_NC_PSEUDO A,P,H
3044c06356bSdh142964 * DEVI_SID_HP_NODEID DDI_NC_PSEUDO A,P,h
3054c06356bSdh142964 * DEVI_SID_HP_HIDDEN_NODEID DDI_NC_PSEUDO A,P,H,h
3067c478bd9Sstevel@tonic-gate * other DDI_NC_PROM P
3077c478bd9Sstevel@tonic-gate *
3087c478bd9Sstevel@tonic-gate * Where A = DDI_AUTO_ASSIGNED_NODEID (auto-assign a nodeid)
3097c478bd9Sstevel@tonic-gate * and P = DDI_PERSISTENT
310027021c7SChris Horne * and H = DDI_HIDDEN_NODE
3114c06356bSdh142964 * and h = DDI_HOTPLUG_NODE
3127c478bd9Sstevel@tonic-gate *
3137c478bd9Sstevel@tonic-gate * auto-assigned nodeids are also auto-freed.
3147c478bd9Sstevel@tonic-gate */
315027021c7SChris Horne devi->devi_node_attributes = 0;
316c6f039c7SToomas Soome elem = NULL;
3177c478bd9Sstevel@tonic-gate switch (nodeid) {
318027021c7SChris Horne case DEVI_SID_HIDDEN_NODEID:
319027021c7SChris Horne devi->devi_node_attributes |= DDI_HIDDEN_NODE;
3204c06356bSdh142964 goto sid;
3214c06356bSdh142964
3224c06356bSdh142964 case DEVI_SID_HP_NODEID:
3234c06356bSdh142964 devi->devi_node_attributes |= DDI_HOTPLUG_NODE;
3244c06356bSdh142964 goto sid;
3254c06356bSdh142964
3264c06356bSdh142964 case DEVI_SID_HP_HIDDEN_NODEID:
3274c06356bSdh142964 devi->devi_node_attributes |= DDI_HIDDEN_NODE;
3284c06356bSdh142964 devi->devi_node_attributes |= DDI_HOTPLUG_NODE;
3294c06356bSdh142964 goto sid;
3304c06356bSdh142964
3317c478bd9Sstevel@tonic-gate case DEVI_SID_NODEID:
3324c06356bSdh142964 sid: devi->devi_node_attributes |= DDI_PERSISTENT;
3337c478bd9Sstevel@tonic-gate if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
3347c478bd9Sstevel@tonic-gate goto fail;
3357c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/
3364c06356bSdh142964
3377c478bd9Sstevel@tonic-gate case DEVI_PSEUDO_NODEID:
3387c478bd9Sstevel@tonic-gate devi->devi_node_attributes |= DDI_AUTO_ASSIGNED_NODEID;
3397c478bd9Sstevel@tonic-gate devi->devi_node_class = DDI_NC_PSEUDO;
3407c478bd9Sstevel@tonic-gate if (impl_ddi_alloc_nodeid(&devi->devi_nodeid)) {
3417c478bd9Sstevel@tonic-gate panic("i_ddi_alloc_node: out of nodeids");
3427c478bd9Sstevel@tonic-gate /*NOTREACHED*/
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate break;
3454c06356bSdh142964
3467c478bd9Sstevel@tonic-gate default:
3477c478bd9Sstevel@tonic-gate if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
3487c478bd9Sstevel@tonic-gate goto fail;
3494c06356bSdh142964
3507c478bd9Sstevel@tonic-gate /*
3517c478bd9Sstevel@tonic-gate * the nodetype is 'prom', try to 'take' the nodeid now.
3527c478bd9Sstevel@tonic-gate * This requires memory allocation, so check for failure.
3537c478bd9Sstevel@tonic-gate */
3547c478bd9Sstevel@tonic-gate if (impl_ddi_take_nodeid(nodeid, flag) != 0) {
3557c478bd9Sstevel@tonic-gate kmem_free(elem, sizeof (*elem));
3567c478bd9Sstevel@tonic-gate goto fail;
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate
3597c478bd9Sstevel@tonic-gate devi->devi_nodeid = nodeid;
3607c478bd9Sstevel@tonic-gate devi->devi_node_class = DDI_NC_PROM;
3617c478bd9Sstevel@tonic-gate devi->devi_node_attributes = DDI_PERSISTENT;
3624c06356bSdh142964 break;
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node((dev_info_t *)devi)) {
3667c478bd9Sstevel@tonic-gate mutex_enter(&devimap->dno_lock);
3677c478bd9Sstevel@tonic-gate elem->next = devimap->dno_free;
3687c478bd9Sstevel@tonic-gate devimap->dno_free = elem;
3697c478bd9Sstevel@tonic-gate mutex_exit(&devimap->dno_lock);
370694256ddSToomas Soome } else if (elem != NULL) {
371694256ddSToomas Soome kmem_free(elem, sizeof (*elem));
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate
3747c478bd9Sstevel@tonic-gate /*
3757c478bd9Sstevel@tonic-gate * Instance is normally initialized to -1. In a few special
3767c478bd9Sstevel@tonic-gate * cases, the caller may specify an instance (e.g. CPU nodes).
3777c478bd9Sstevel@tonic-gate */
3787c478bd9Sstevel@tonic-gate devi->devi_instance = instance;
3797c478bd9Sstevel@tonic-gate
3807c478bd9Sstevel@tonic-gate /*
3817c478bd9Sstevel@tonic-gate * set parent and bus_ctl parent
3827c478bd9Sstevel@tonic-gate */
3837c478bd9Sstevel@tonic-gate devi->devi_parent = DEVI(pdip);
3847c478bd9Sstevel@tonic-gate devi->devi_bus_ctl = DEVI(pdip);
3857c478bd9Sstevel@tonic-gate
3867c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT,
3877c478bd9Sstevel@tonic-gate "i_ddi_alloc_node: name=%s id=%d\n", node_name, devi->devi_nodeid));
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate cv_init(&(devi->devi_cv), NULL, CV_DEFAULT, NULL);
3907c478bd9Sstevel@tonic-gate mutex_init(&(devi->devi_lock), NULL, MUTEX_DEFAULT, NULL);
3917c478bd9Sstevel@tonic-gate mutex_init(&(devi->devi_pm_lock), NULL, MUTEX_DEFAULT, NULL);
3927c478bd9Sstevel@tonic-gate mutex_init(&(devi->devi_pm_busy_lock), NULL, MUTEX_DEFAULT, NULL);
3937c478bd9Sstevel@tonic-gate
39425e8c5aaSvikram RIO_TRACE((CE_NOTE, "i_ddi_alloc_node: Initing contract fields: "
39525e8c5aaSvikram "dip=%p, name=%s", (void *)devi, node_name));
39625e8c5aaSvikram
39725e8c5aaSvikram mutex_init(&(devi->devi_ct_lock), NULL, MUTEX_DEFAULT, NULL);
39825e8c5aaSvikram cv_init(&(devi->devi_ct_cv), NULL, CV_DEFAULT, NULL);
39925e8c5aaSvikram devi->devi_ct_count = -1; /* counter not in use if -1 */
40025e8c5aaSvikram list_create(&(devi->devi_ct), sizeof (cont_device_t),
40125e8c5aaSvikram offsetof(cont_device_t, cond_next));
4023ce53722SRobert Mustacchi list_create(&devi->devi_unbind_cbs, sizeof (ddi_unbind_callback_t),
4033ce53722SRobert Mustacchi offsetof(ddi_unbind_callback_t, ddiub_next));
4043ce53722SRobert Mustacchi mutex_init(&devi->devi_unbind_lock, NULL, MUTEX_DEFAULT, NULL);
40525e8c5aaSvikram
4067c478bd9Sstevel@tonic-gate i_ddi_set_node_state((dev_info_t *)devi, DS_PROTO);
4077c478bd9Sstevel@tonic-gate da_log_enter((dev_info_t *)devi);
4087c478bd9Sstevel@tonic-gate return ((dev_info_t *)devi);
4097c478bd9Sstevel@tonic-gate
4107c478bd9Sstevel@tonic-gate fail:
4117c478bd9Sstevel@tonic-gate if (devi->devi_sys_prop_ptr)
4127c478bd9Sstevel@tonic-gate i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
4137c478bd9Sstevel@tonic-gate if (devi->devi_node_name)
4147c478bd9Sstevel@tonic-gate kmem_free(devi->devi_node_name, strlen(node_name) + 1);
4157c478bd9Sstevel@tonic-gate if (devi->devi_audit)
4167c478bd9Sstevel@tonic-gate kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
4177c478bd9Sstevel@tonic-gate kmem_cache_free(ddi_node_cache, devi);
4187c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, failed);
4197c478bd9Sstevel@tonic-gate return (NULL);
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate
4227c478bd9Sstevel@tonic-gate /*
4237c478bd9Sstevel@tonic-gate * free a dev_info structure.
4247c478bd9Sstevel@tonic-gate * NB. Not callable from interrupt since impl_ddi_free_nodeid may block.
4257c478bd9Sstevel@tonic-gate */
4267c478bd9Sstevel@tonic-gate void
i_ddi_free_node(dev_info_t * dip)4277c478bd9Sstevel@tonic-gate i_ddi_free_node(dev_info_t *dip)
4287c478bd9Sstevel@tonic-gate {
4297c478bd9Sstevel@tonic-gate struct dev_info *devi = DEVI(dip);
4307c478bd9Sstevel@tonic-gate struct devi_nodeid *elem;
4317c478bd9Sstevel@tonic-gate
4327c478bd9Sstevel@tonic-gate ASSERT(devi->devi_ref == 0);
4337c478bd9Sstevel@tonic-gate ASSERT(devi->devi_addr == NULL);
4347c478bd9Sstevel@tonic-gate ASSERT(devi->devi_node_state == DS_PROTO);
4357c478bd9Sstevel@tonic-gate ASSERT(devi->devi_child == NULL);
43626947304SEvan Yan ASSERT(devi->devi_hp_hdlp == NULL);
4377c478bd9Sstevel@tonic-gate
438fe9fe9fbScth /* free devi_addr_buf allocated by ddi_set_name_addr() */
439fe9fe9fbScth if (devi->devi_addr_buf)
440fe9fe9fbScth kmem_free(devi->devi_addr_buf, 2 * MAXNAMELEN);
4417c478bd9Sstevel@tonic-gate
4427c478bd9Sstevel@tonic-gate if (i_ndi_dev_is_auto_assigned_node(dip))
4437c478bd9Sstevel@tonic-gate impl_ddi_free_nodeid(DEVI(dip)->devi_nodeid);
4447c478bd9Sstevel@tonic-gate
4457c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(dip)) {
4467c478bd9Sstevel@tonic-gate mutex_enter(&devimap->dno_lock);
4477c478bd9Sstevel@tonic-gate ASSERT(devimap->dno_free);
4487c478bd9Sstevel@tonic-gate elem = devimap->dno_free;
4497c478bd9Sstevel@tonic-gate devimap->dno_free = elem->next;
4507c478bd9Sstevel@tonic-gate mutex_exit(&devimap->dno_lock);
4517c478bd9Sstevel@tonic-gate kmem_free(elem, sizeof (*elem));
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate
4547c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_compat_names)
4557c478bd9Sstevel@tonic-gate kmem_free(DEVI(dip)->devi_compat_names,
4567c478bd9Sstevel@tonic-gate DEVI(dip)->devi_compat_length);
457f4da9be0Scth if (DEVI(dip)->devi_rebinding_name)
458f4da9be0Scth kmem_free(DEVI(dip)->devi_rebinding_name,
459f4da9be0Scth strlen(DEVI(dip)->devi_rebinding_name) + 1);
4607c478bd9Sstevel@tonic-gate
4617c478bd9Sstevel@tonic-gate ddi_prop_remove_all(dip); /* remove driver properties */
4627c478bd9Sstevel@tonic-gate if (devi->devi_sys_prop_ptr)
4637c478bd9Sstevel@tonic-gate i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
4647c478bd9Sstevel@tonic-gate if (devi->devi_hw_prop_ptr)
4657c478bd9Sstevel@tonic-gate i_ddi_prop_list_delete(devi->devi_hw_prop_ptr);
4667c478bd9Sstevel@tonic-gate
467602ca9eaScth if (DEVI(dip)->devi_devid_str)
468602ca9eaScth ddi_devid_str_free(DEVI(dip)->devi_devid_str);
469602ca9eaScth
4707c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_INVAL);
4717c478bd9Sstevel@tonic-gate da_log_enter(dip);
4727c478bd9Sstevel@tonic-gate if (devi->devi_audit) {
4737c478bd9Sstevel@tonic-gate kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate if (devi->devi_device_class)
4767c478bd9Sstevel@tonic-gate kmem_free(devi->devi_device_class,
4777c478bd9Sstevel@tonic-gate strlen(devi->devi_device_class) + 1);
4787c478bd9Sstevel@tonic-gate cv_destroy(&(devi->devi_cv));
4797c478bd9Sstevel@tonic-gate mutex_destroy(&(devi->devi_lock));
4807c478bd9Sstevel@tonic-gate mutex_destroy(&(devi->devi_pm_lock));
4817c478bd9Sstevel@tonic-gate mutex_destroy(&(devi->devi_pm_busy_lock));
4827c478bd9Sstevel@tonic-gate
48325e8c5aaSvikram RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroying contract fields: "
48425e8c5aaSvikram "dip=%p", (void *)dip));
48525e8c5aaSvikram contract_device_remove_dip(dip);
48625e8c5aaSvikram ASSERT(devi->devi_ct_count == -1);
48725e8c5aaSvikram ASSERT(list_is_empty(&(devi->devi_ct)));
48825e8c5aaSvikram cv_destroy(&(devi->devi_ct_cv));
48925e8c5aaSvikram list_destroy(&(devi->devi_ct));
49025e8c5aaSvikram /* free this last since contract_device_remove_dip() uses it */
49125e8c5aaSvikram mutex_destroy(&(devi->devi_ct_lock));
49225e8c5aaSvikram RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroyed all contract fields: "
49325e8c5aaSvikram "dip=%p, name=%s", (void *)dip, devi->devi_node_name));
49425e8c5aaSvikram
49525e8c5aaSvikram kmem_free(devi->devi_node_name, strlen(devi->devi_node_name) + 1);
49625e8c5aaSvikram
497779f1d69SMichael Bergknoff /* free event data */
498779f1d69SMichael Bergknoff if (devi->devi_ev_path)
499779f1d69SMichael Bergknoff kmem_free(devi->devi_ev_path, MAXPATHLEN);
500779f1d69SMichael Bergknoff
5013ce53722SRobert Mustacchi mutex_destroy(&devi->devi_unbind_lock);
5023ce53722SRobert Mustacchi list_destroy(&devi->devi_unbind_cbs);
5033ce53722SRobert Mustacchi
5047c478bd9Sstevel@tonic-gate kmem_cache_free(ddi_node_cache, devi);
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate
5077c478bd9Sstevel@tonic-gate
5087c478bd9Sstevel@tonic-gate /*
5097c478bd9Sstevel@tonic-gate * Node state transitions
5107c478bd9Sstevel@tonic-gate */
5117c478bd9Sstevel@tonic-gate
5127c478bd9Sstevel@tonic-gate /*
5137c478bd9Sstevel@tonic-gate * Change the node name
5147c478bd9Sstevel@tonic-gate */
5157c478bd9Sstevel@tonic-gate int
ndi_devi_set_nodename(dev_info_t * dip,char * name,int flags)5167c478bd9Sstevel@tonic-gate ndi_devi_set_nodename(dev_info_t *dip, char *name, int flags)
5177c478bd9Sstevel@tonic-gate {
5187c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(flags))
5197c478bd9Sstevel@tonic-gate char *nname, *oname;
5207c478bd9Sstevel@tonic-gate
5217c478bd9Sstevel@tonic-gate ASSERT(dip && name);
5227c478bd9Sstevel@tonic-gate
5237c478bd9Sstevel@tonic-gate oname = DEVI(dip)->devi_node_name;
5247c478bd9Sstevel@tonic-gate if (strcmp(oname, name) == 0)
5257c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
5267c478bd9Sstevel@tonic-gate
5277c478bd9Sstevel@tonic-gate /*
5287c478bd9Sstevel@tonic-gate * pcicfg_fix_ethernet requires a name change after node
5297c478bd9Sstevel@tonic-gate * is linked into the tree. When pcicfg is fixed, we
5307c478bd9Sstevel@tonic-gate * should only allow name change in DS_PROTO state.
5317c478bd9Sstevel@tonic-gate */
5327c478bd9Sstevel@tonic-gate if (i_ddi_node_state(dip) >= DS_BOUND) {
5337c478bd9Sstevel@tonic-gate /*
5347c478bd9Sstevel@tonic-gate * Don't allow name change once node is bound
5357c478bd9Sstevel@tonic-gate */
5367c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE,
5377c478bd9Sstevel@tonic-gate "ndi_devi_set_nodename: node already bound dip = %p,"
5387c478bd9Sstevel@tonic-gate " %s -> %s", (void *)dip, ddi_node_name(dip), name);
5397c478bd9Sstevel@tonic-gate return (NDI_FAILURE);
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate
5427c478bd9Sstevel@tonic-gate nname = i_ddi_strdup(name, KM_SLEEP);
5437c478bd9Sstevel@tonic-gate DEVI(dip)->devi_node_name = nname;
5447c478bd9Sstevel@tonic-gate i_ddi_set_binding_name(dip, nname);
5457c478bd9Sstevel@tonic-gate kmem_free(oname, strlen(oname) + 1);
5467c478bd9Sstevel@tonic-gate
5477c478bd9Sstevel@tonic-gate da_log_enter(dip);
5487c478bd9Sstevel@tonic-gate return (NDI_SUCCESS);
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate
5517c478bd9Sstevel@tonic-gate void
i_ddi_add_devimap(dev_info_t * dip)5527c478bd9Sstevel@tonic-gate i_ddi_add_devimap(dev_info_t *dip)
5537c478bd9Sstevel@tonic-gate {
5547c478bd9Sstevel@tonic-gate struct devi_nodeid *elem;
5557c478bd9Sstevel@tonic-gate
5567c478bd9Sstevel@tonic-gate ASSERT(dip);
5577c478bd9Sstevel@tonic-gate
5587c478bd9Sstevel@tonic-gate if (!ndi_dev_is_persistent_node(dip))
5597c478bd9Sstevel@tonic-gate return;
5607c478bd9Sstevel@tonic-gate
5617c478bd9Sstevel@tonic-gate ASSERT(ddi_get_parent(dip) == NULL || (DEVI_VHCI_NODE(dip)) ||
5627c478bd9Sstevel@tonic-gate DEVI_BUSY_OWNED(ddi_get_parent(dip)));
5637c478bd9Sstevel@tonic-gate
5647c478bd9Sstevel@tonic-gate mutex_enter(&devimap->dno_lock);
5657c478bd9Sstevel@tonic-gate
5667c478bd9Sstevel@tonic-gate ASSERT(devimap->dno_free);
5677c478bd9Sstevel@tonic-gate
5687c478bd9Sstevel@tonic-gate elem = devimap->dno_free;
5697c478bd9Sstevel@tonic-gate devimap->dno_free = elem->next;
5707c478bd9Sstevel@tonic-gate
5717c478bd9Sstevel@tonic-gate elem->nodeid = ddi_get_nodeid(dip);
5727c478bd9Sstevel@tonic-gate elem->dip = dip;
5737c478bd9Sstevel@tonic-gate elem->next = devimap->dno_head;
5747c478bd9Sstevel@tonic-gate devimap->dno_head = elem;
5757c478bd9Sstevel@tonic-gate
5767c478bd9Sstevel@tonic-gate devimap->dno_list_length++;
5777c478bd9Sstevel@tonic-gate
5787c478bd9Sstevel@tonic-gate mutex_exit(&devimap->dno_lock);
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate
5817c478bd9Sstevel@tonic-gate static int
i_ddi_remove_devimap(dev_info_t * dip)5827c478bd9Sstevel@tonic-gate i_ddi_remove_devimap(dev_info_t *dip)
5837c478bd9Sstevel@tonic-gate {
5847c478bd9Sstevel@tonic-gate struct devi_nodeid *prev, *elem;
5857c478bd9Sstevel@tonic-gate static const char *fcn = "i_ddi_remove_devimap";
5867c478bd9Sstevel@tonic-gate
5877c478bd9Sstevel@tonic-gate ASSERT(dip);
5887c478bd9Sstevel@tonic-gate
5897c478bd9Sstevel@tonic-gate if (!ndi_dev_is_persistent_node(dip))
5907c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
5917c478bd9Sstevel@tonic-gate
5927c478bd9Sstevel@tonic-gate mutex_enter(&devimap->dno_lock);
5937c478bd9Sstevel@tonic-gate
5947c478bd9Sstevel@tonic-gate /*
5957c478bd9Sstevel@tonic-gate * The following check is done with dno_lock held
5967c478bd9Sstevel@tonic-gate * to prevent race between dip removal and
5977c478bd9Sstevel@tonic-gate * e_ddi_prom_node_to_dip()
5987c478bd9Sstevel@tonic-gate */
5997c478bd9Sstevel@tonic-gate if (e_ddi_devi_holdcnt(dip)) {
6007c478bd9Sstevel@tonic-gate mutex_exit(&devimap->dno_lock);
6017c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate
6047c478bd9Sstevel@tonic-gate ASSERT(devimap->dno_head);
6057c478bd9Sstevel@tonic-gate ASSERT(devimap->dno_list_length > 0);
6067c478bd9Sstevel@tonic-gate
6077c478bd9Sstevel@tonic-gate prev = NULL;
6087c478bd9Sstevel@tonic-gate for (elem = devimap->dno_head; elem; elem = elem->next) {
6097c478bd9Sstevel@tonic-gate if (elem->dip == dip) {
6107c478bd9Sstevel@tonic-gate ASSERT(elem->nodeid == ddi_get_nodeid(dip));
6117c478bd9Sstevel@tonic-gate break;
6127c478bd9Sstevel@tonic-gate }
6137c478bd9Sstevel@tonic-gate prev = elem;
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate
6167c478bd9Sstevel@tonic-gate if (elem && prev)
6177c478bd9Sstevel@tonic-gate prev->next = elem->next;
6187c478bd9Sstevel@tonic-gate else if (elem)
6197c478bd9Sstevel@tonic-gate devimap->dno_head = elem->next;
6207c478bd9Sstevel@tonic-gate else
6217c478bd9Sstevel@tonic-gate panic("%s: devinfo node(%p) not found",
6227c478bd9Sstevel@tonic-gate fcn, (void *)dip);
6237c478bd9Sstevel@tonic-gate
6247c478bd9Sstevel@tonic-gate devimap->dno_list_length--;
6257c478bd9Sstevel@tonic-gate
6267c478bd9Sstevel@tonic-gate elem->nodeid = 0;
6277c478bd9Sstevel@tonic-gate elem->dip = NULL;
6287c478bd9Sstevel@tonic-gate
6297c478bd9Sstevel@tonic-gate elem->next = devimap->dno_free;
6307c478bd9Sstevel@tonic-gate devimap->dno_free = elem;
6317c478bd9Sstevel@tonic-gate
6327c478bd9Sstevel@tonic-gate mutex_exit(&devimap->dno_lock);
6337c478bd9Sstevel@tonic-gate
6347c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate
6377c478bd9Sstevel@tonic-gate /*
6387c478bd9Sstevel@tonic-gate * Link this node into the devinfo tree and add to orphan list
6397c478bd9Sstevel@tonic-gate * Not callable from interrupt context
6407c478bd9Sstevel@tonic-gate */
6417c478bd9Sstevel@tonic-gate static void
link_node(dev_info_t * dip)6427c478bd9Sstevel@tonic-gate link_node(dev_info_t *dip)
6437c478bd9Sstevel@tonic-gate {
6447c478bd9Sstevel@tonic-gate struct dev_info *devi = DEVI(dip);
6457c478bd9Sstevel@tonic-gate struct dev_info *parent = devi->devi_parent;
6467c478bd9Sstevel@tonic-gate dev_info_t **dipp;
6477c478bd9Sstevel@tonic-gate
6487c478bd9Sstevel@tonic-gate ASSERT(parent); /* never called for root node */
6497c478bd9Sstevel@tonic-gate
6507c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "link_node: parent = %s child = %s\n",
6517c478bd9Sstevel@tonic-gate parent->devi_node_name, devi->devi_node_name));
6527c478bd9Sstevel@tonic-gate
6537c478bd9Sstevel@tonic-gate /*
6547c478bd9Sstevel@tonic-gate * Hold the global_vhci_lock before linking any direct
6557c478bd9Sstevel@tonic-gate * children of rootnex driver. This special lock protects
6567c478bd9Sstevel@tonic-gate * linking and unlinking for rootnext direct children.
6577c478bd9Sstevel@tonic-gate */
6587c478bd9Sstevel@tonic-gate if ((dev_info_t *)parent == ddi_root_node())
6597c478bd9Sstevel@tonic-gate mutex_enter(&global_vhci_lock);
6607c478bd9Sstevel@tonic-gate
6617c478bd9Sstevel@tonic-gate /*
6627c478bd9Sstevel@tonic-gate * attach the node to end of the list unless the node is already there
6637c478bd9Sstevel@tonic-gate */
6647c478bd9Sstevel@tonic-gate dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
6657c478bd9Sstevel@tonic-gate while (*dipp && (*dipp != dip)) {
6667c478bd9Sstevel@tonic-gate dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
6677c478bd9Sstevel@tonic-gate }
6687c478bd9Sstevel@tonic-gate ASSERT(*dipp == NULL); /* node is not linked */
6697c478bd9Sstevel@tonic-gate
6707c478bd9Sstevel@tonic-gate /*
6717c478bd9Sstevel@tonic-gate * Now that we are in the tree, update the devi-nodeid map.
6727c478bd9Sstevel@tonic-gate */
6737c478bd9Sstevel@tonic-gate i_ddi_add_devimap(dip);
6747c478bd9Sstevel@tonic-gate
6757c478bd9Sstevel@tonic-gate /*
6767c478bd9Sstevel@tonic-gate * This is a temporary workaround for Bug 4618861.
6777c478bd9Sstevel@tonic-gate * We keep the scsi_vhci nexus node on the left side of the devinfo
6787c478bd9Sstevel@tonic-gate * tree (under the root nexus driver), so that virtual nodes under
6797c478bd9Sstevel@tonic-gate * scsi_vhci will be SUSPENDed first and RESUMEd last. This ensures
6807c478bd9Sstevel@tonic-gate * that the pHCI nodes are active during times when their clients
6817c478bd9Sstevel@tonic-gate * may be depending on them. This workaround embodies the knowledge
6827c478bd9Sstevel@tonic-gate * that system PM and CPR both traverse the tree left-to-right during
6837c478bd9Sstevel@tonic-gate * SUSPEND and right-to-left during RESUME.
6849d3d2ed0Shiremath * Extending the workaround to IB Nexus/VHCI
6859d3d2ed0Shiremath * driver also.
6867c478bd9Sstevel@tonic-gate */
687f4da9be0Scth if (strcmp(devi->devi_binding_name, "scsi_vhci") == 0) {
6887c478bd9Sstevel@tonic-gate /* Add scsi_vhci to beginning of list */
6897c478bd9Sstevel@tonic-gate ASSERT((dev_info_t *)parent == top_devinfo);
6907c478bd9Sstevel@tonic-gate /* scsi_vhci under rootnex */
6917c478bd9Sstevel@tonic-gate devi->devi_sibling = parent->devi_child;
6927c478bd9Sstevel@tonic-gate parent->devi_child = devi;
693f4da9be0Scth } else if (strcmp(devi->devi_binding_name, "ib") == 0) {
6949d3d2ed0Shiremath i_link_vhci_node(dip);
6957c478bd9Sstevel@tonic-gate } else {
6967c478bd9Sstevel@tonic-gate /* Add to end of list */
6977c478bd9Sstevel@tonic-gate *dipp = dip;
6987c478bd9Sstevel@tonic-gate DEVI(dip)->devi_sibling = NULL;
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate
7017c478bd9Sstevel@tonic-gate /*
7027c478bd9Sstevel@tonic-gate * Release the global_vhci_lock before linking any direct
7037c478bd9Sstevel@tonic-gate * children of rootnex driver.
7047c478bd9Sstevel@tonic-gate */
7057c478bd9Sstevel@tonic-gate if ((dev_info_t *)parent == ddi_root_node())
7067c478bd9Sstevel@tonic-gate mutex_exit(&global_vhci_lock);
7077c478bd9Sstevel@tonic-gate
7087c478bd9Sstevel@tonic-gate /* persistent nodes go on orphan list */
7097c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(dip))
7107c478bd9Sstevel@tonic-gate add_to_dn_list(&orphanlist, dip);
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate
7137c478bd9Sstevel@tonic-gate /*
7147c478bd9Sstevel@tonic-gate * Unlink this node from the devinfo tree
7157c478bd9Sstevel@tonic-gate */
7167c478bd9Sstevel@tonic-gate static int
unlink_node(dev_info_t * dip)7177c478bd9Sstevel@tonic-gate unlink_node(dev_info_t *dip)
7187c478bd9Sstevel@tonic-gate {
7197c478bd9Sstevel@tonic-gate struct dev_info *devi = DEVI(dip);
7207c478bd9Sstevel@tonic-gate struct dev_info *parent = devi->devi_parent;
7217c478bd9Sstevel@tonic-gate dev_info_t **dipp;
72226947304SEvan Yan ddi_hp_cn_handle_t *hdlp;
7237c478bd9Sstevel@tonic-gate
7247c478bd9Sstevel@tonic-gate ASSERT(parent != NULL);
7257c478bd9Sstevel@tonic-gate ASSERT(devi->devi_node_state == DS_LINKED);
7267c478bd9Sstevel@tonic-gate
7277c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "unlink_node: name = %s\n",
7287c478bd9Sstevel@tonic-gate ddi_node_name(dip)));
7297c478bd9Sstevel@tonic-gate
7307c478bd9Sstevel@tonic-gate /* check references */
7317c478bd9Sstevel@tonic-gate if (devi->devi_ref || i_ddi_remove_devimap(dip) != DDI_SUCCESS)
7327c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
7337c478bd9Sstevel@tonic-gate
7347c478bd9Sstevel@tonic-gate /*
7357c478bd9Sstevel@tonic-gate * Hold the global_vhci_lock before linking any direct
7367c478bd9Sstevel@tonic-gate * children of rootnex driver.
7377c478bd9Sstevel@tonic-gate */
7387c478bd9Sstevel@tonic-gate if ((dev_info_t *)parent == ddi_root_node())
7397c478bd9Sstevel@tonic-gate mutex_enter(&global_vhci_lock);
7407c478bd9Sstevel@tonic-gate
7417c478bd9Sstevel@tonic-gate dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
7427c478bd9Sstevel@tonic-gate while (*dipp && (*dipp != dip)) {
7437c478bd9Sstevel@tonic-gate dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
7447c478bd9Sstevel@tonic-gate }
7457c478bd9Sstevel@tonic-gate if (*dipp) {
7467c478bd9Sstevel@tonic-gate *dipp = (dev_info_t *)(devi->devi_sibling);
7477c478bd9Sstevel@tonic-gate devi->devi_sibling = NULL;
7487c478bd9Sstevel@tonic-gate } else {
7497c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_NOTE, "unlink_node: %s not linked",
7507c478bd9Sstevel@tonic-gate devi->devi_node_name));
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate
7537c478bd9Sstevel@tonic-gate /*
7547c478bd9Sstevel@tonic-gate * Release the global_vhci_lock before linking any direct
7557c478bd9Sstevel@tonic-gate * children of rootnex driver.
7567c478bd9Sstevel@tonic-gate */
7577c478bd9Sstevel@tonic-gate if ((dev_info_t *)parent == ddi_root_node())
7587c478bd9Sstevel@tonic-gate mutex_exit(&global_vhci_lock);
7597c478bd9Sstevel@tonic-gate
7607c478bd9Sstevel@tonic-gate /* Remove node from orphan list */
7617c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(dip)) {
7627c478bd9Sstevel@tonic-gate remove_from_dn_list(&orphanlist, dip);
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate
76526947304SEvan Yan /* Update parent's hotplug handle list */
76626947304SEvan Yan for (hdlp = DEVI(parent)->devi_hp_hdlp; hdlp; hdlp = hdlp->next) {
76726947304SEvan Yan if (hdlp->cn_info.cn_child == dip)
76826947304SEvan Yan hdlp->cn_info.cn_child = NULL;
76926947304SEvan Yan }
7707c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
7717c478bd9Sstevel@tonic-gate }
7727c478bd9Sstevel@tonic-gate
7737c478bd9Sstevel@tonic-gate /*
7747c478bd9Sstevel@tonic-gate * Bind this devinfo node to a driver. If compat is NON-NULL, try that first.
7757c478bd9Sstevel@tonic-gate * Else, use the node-name.
7767c478bd9Sstevel@tonic-gate *
7777c478bd9Sstevel@tonic-gate * NOTE: IEEE1275 specifies that nodename should be tried before compatible.
7787c478bd9Sstevel@tonic-gate * Solaris implementation binds nodename after compatible.
7797c478bd9Sstevel@tonic-gate *
7807c478bd9Sstevel@tonic-gate * If we find a binding,
781027021c7SChris Horne * - set the binding name to the string,
7827c478bd9Sstevel@tonic-gate * - set major number to driver major
7837c478bd9Sstevel@tonic-gate *
7847c478bd9Sstevel@tonic-gate * If we don't find a binding,
7857c478bd9Sstevel@tonic-gate * - return failure
7867c478bd9Sstevel@tonic-gate */
7877c478bd9Sstevel@tonic-gate static int
bind_node(dev_info_t * dip)7887c478bd9Sstevel@tonic-gate bind_node(dev_info_t *dip)
7897c478bd9Sstevel@tonic-gate {
7907c478bd9Sstevel@tonic-gate char *p = NULL;
791a204de77Scth major_t major = DDI_MAJOR_T_NONE;
7927c478bd9Sstevel@tonic-gate struct dev_info *devi = DEVI(dip);
7937c478bd9Sstevel@tonic-gate dev_info_t *parent = ddi_get_parent(dip);
7947c478bd9Sstevel@tonic-gate
7957c478bd9Sstevel@tonic-gate ASSERT(devi->devi_node_state == DS_LINKED);
7967c478bd9Sstevel@tonic-gate
7977c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "bind_node: 0x%p(name = %s)\n",
7987c478bd9Sstevel@tonic-gate (void *)dip, ddi_node_name(dip)));
7997c478bd9Sstevel@tonic-gate
8007c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(dip)->devi_lock);
8017c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_flags & DEVI_NO_BIND) {
8027c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(dip)->devi_lock);
8037c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(dip)->devi_lock);
8067c478bd9Sstevel@tonic-gate
8077c478bd9Sstevel@tonic-gate /* find the driver with most specific binding using compatible */
8087c478bd9Sstevel@tonic-gate major = ddi_compatible_driver_major(dip, &p);
809a204de77Scth if (major == DDI_MAJOR_T_NONE)
8107c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
8117c478bd9Sstevel@tonic-gate
8127c478bd9Sstevel@tonic-gate devi->devi_major = major;
8137c478bd9Sstevel@tonic-gate if (p != NULL) {
8147c478bd9Sstevel@tonic-gate i_ddi_set_binding_name(dip, p);
8157c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "bind_node: %s bound to %s\n",
8167c478bd9Sstevel@tonic-gate devi->devi_node_name, p));
8177c478bd9Sstevel@tonic-gate }
8187c478bd9Sstevel@tonic-gate
8197c478bd9Sstevel@tonic-gate /* Link node to per-driver list */
8207c478bd9Sstevel@tonic-gate link_to_driver_list(dip);
8217c478bd9Sstevel@tonic-gate
8227c478bd9Sstevel@tonic-gate /*
8237c478bd9Sstevel@tonic-gate * reset parent flag so that nexus will merge .conf props
8247c478bd9Sstevel@tonic-gate */
8257c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(dip)) {
8267c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(parent)->devi_lock);
8277c478bd9Sstevel@tonic-gate DEVI(parent)->devi_flags &=
8287c478bd9Sstevel@tonic-gate ~(DEVI_ATTACHED_CHILDREN|DEVI_MADE_CHILDREN);
8297c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(parent)->devi_lock);
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
8327c478bd9Sstevel@tonic-gate }
8337c478bd9Sstevel@tonic-gate
8347c478bd9Sstevel@tonic-gate /*
8357c478bd9Sstevel@tonic-gate * Unbind this devinfo node
8367c478bd9Sstevel@tonic-gate * Called before the node is destroyed or driver is removed from system
8377c478bd9Sstevel@tonic-gate */
8387c478bd9Sstevel@tonic-gate static int
unbind_node(dev_info_t * dip)8397c478bd9Sstevel@tonic-gate unbind_node(dev_info_t *dip)
8407c478bd9Sstevel@tonic-gate {
8413ce53722SRobert Mustacchi ddi_unbind_callback_t *cb;
8427c478bd9Sstevel@tonic-gate ASSERT(DEVI(dip)->devi_node_state == DS_BOUND);
843a204de77Scth ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE);
8447c478bd9Sstevel@tonic-gate
8457c478bd9Sstevel@tonic-gate /* check references */
8467c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_ref)
8477c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
8487c478bd9Sstevel@tonic-gate
8497c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "unbind_node: 0x%p(name = %s)\n",
8507c478bd9Sstevel@tonic-gate (void *)dip, ddi_node_name(dip)));
8517c478bd9Sstevel@tonic-gate
8527c478bd9Sstevel@tonic-gate unlink_from_driver_list(dip);
853f4da9be0Scth
854a204de77Scth DEVI(dip)->devi_major = DDI_MAJOR_T_NONE;
855f4da9be0Scth DEVI(dip)->devi_binding_name = DEVI(dip)->devi_node_name;
8563ce53722SRobert Mustacchi
8573ce53722SRobert Mustacchi while ((cb = list_remove_head(&DEVI(dip)->devi_unbind_cbs)) != NULL) {
8583ce53722SRobert Mustacchi cb->ddiub_cb(cb->ddiub_arg, dip);
8593ce53722SRobert Mustacchi }
8603ce53722SRobert Mustacchi
8617c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
8627c478bd9Sstevel@tonic-gate }
8637c478bd9Sstevel@tonic-gate
8647c478bd9Sstevel@tonic-gate /*
8657c478bd9Sstevel@tonic-gate * Initialize a node: calls the parent nexus' bus_ctl ops to do the operation.
8667c478bd9Sstevel@tonic-gate * Must hold parent and per-driver list while calling this function.
8677c478bd9Sstevel@tonic-gate * A successful init_node() returns with an active ndi_hold_devi() hold on
8687c478bd9Sstevel@tonic-gate * the parent.
8697c478bd9Sstevel@tonic-gate */
8707c478bd9Sstevel@tonic-gate static int
init_node(dev_info_t * dip)8717c478bd9Sstevel@tonic-gate init_node(dev_info_t *dip)
8727c478bd9Sstevel@tonic-gate {
8737c478bd9Sstevel@tonic-gate int error;
8747c478bd9Sstevel@tonic-gate dev_info_t *pdip = ddi_get_parent(dip);
8757c478bd9Sstevel@tonic-gate int (*f)(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
8767c478bd9Sstevel@tonic-gate char *path;
877f4da9be0Scth major_t major;
8784f1e984dSReed ddi_devid_t devid = NULL;
8797c478bd9Sstevel@tonic-gate
8807c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(dip) == DS_BOUND);
8817c478bd9Sstevel@tonic-gate
8827c478bd9Sstevel@tonic-gate /* should be DS_READY except for pcmcia ... */
8837c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(pdip) >= DS_PROBED);
8847c478bd9Sstevel@tonic-gate
8857c478bd9Sstevel@tonic-gate path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8867c478bd9Sstevel@tonic-gate (void) ddi_pathname(dip, path);
8877c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "init_node: entry: path %s 0x%p\n",
8887c478bd9Sstevel@tonic-gate path, (void *)dip));
8897c478bd9Sstevel@tonic-gate
8907c478bd9Sstevel@tonic-gate /*
8917c478bd9Sstevel@tonic-gate * The parent must have a bus_ctl operation.
8927c478bd9Sstevel@tonic-gate */
8937c478bd9Sstevel@tonic-gate if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
8947c478bd9Sstevel@tonic-gate (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_ctl) == NULL) {
8957c478bd9Sstevel@tonic-gate error = DDI_FAILURE;
8967c478bd9Sstevel@tonic-gate goto out;
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate
8997c478bd9Sstevel@tonic-gate add_global_props(dip);
9007c478bd9Sstevel@tonic-gate
9017c478bd9Sstevel@tonic-gate /*
9027c478bd9Sstevel@tonic-gate * Invoke the parent's bus_ctl operation with the DDI_CTLOPS_INITCHILD
9037c478bd9Sstevel@tonic-gate * command to transform the child to canonical form 1. If there
9047c478bd9Sstevel@tonic-gate * is an error, ddi_remove_child should be called, to clean up.
9057c478bd9Sstevel@tonic-gate */
9067c478bd9Sstevel@tonic-gate error = (*f)(pdip, pdip, DDI_CTLOPS_INITCHILD, dip, NULL);
9077c478bd9Sstevel@tonic-gate if (error != DDI_SUCCESS) {
9087c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "init_node: %s 0x%p failed\n",
9097c478bd9Sstevel@tonic-gate path, (void *)dip));
9107c478bd9Sstevel@tonic-gate remove_global_props(dip);
9118451e9c3SGavin Maltby
9128451e9c3SGavin Maltby /*
9138451e9c3SGavin Maltby * If a nexus INITCHILD implementation calls ddi_devid_regster()
9148451e9c3SGavin Maltby * prior to setting devi_addr, the devid is not recorded in
9158451e9c3SGavin Maltby * the devid cache (i.e. DEVI_CACHED_DEVID is not set).
9168451e9c3SGavin Maltby * With mpxio, while the vhci client path may be missing
9178451e9c3SGavin Maltby * from the cache, phci pathinfo paths may have already be
9188451e9c3SGavin Maltby * added to the cache, against the client dip, by use of
9198451e9c3SGavin Maltby * e_devid_cache_pathinfo(). Because of this, when INITCHILD
9208451e9c3SGavin Maltby * of the client fails, we need to purge the client dip from
9218451e9c3SGavin Maltby * the cache even if DEVI_CACHED_DEVID is not set - if only
9228451e9c3SGavin Maltby * devi_devid_str is set.
9238451e9c3SGavin Maltby */
9248451e9c3SGavin Maltby mutex_enter(&DEVI(dip)->devi_lock);
9258451e9c3SGavin Maltby if ((DEVI(dip)->devi_flags & DEVI_CACHED_DEVID) ||
9268451e9c3SGavin Maltby DEVI(dip)->devi_devid_str) {
9278451e9c3SGavin Maltby DEVI(dip)->devi_flags &= ~DEVI_CACHED_DEVID;
9288451e9c3SGavin Maltby mutex_exit(&DEVI(dip)->devi_lock);
9298451e9c3SGavin Maltby ddi_devid_unregister(dip);
9308451e9c3SGavin Maltby } else
9318451e9c3SGavin Maltby mutex_exit(&DEVI(dip)->devi_lock);
9328451e9c3SGavin Maltby
9337c478bd9Sstevel@tonic-gate /* in case nexus driver didn't clear this field */
9347c478bd9Sstevel@tonic-gate ddi_set_name_addr(dip, NULL);
9357c478bd9Sstevel@tonic-gate error = DDI_FAILURE;
9367c478bd9Sstevel@tonic-gate goto out;
9377c478bd9Sstevel@tonic-gate }
9387c478bd9Sstevel@tonic-gate
939d6ae180bScth ndi_hold_devi(pdip); /* initial hold of parent */
9407c478bd9Sstevel@tonic-gate
9417c478bd9Sstevel@tonic-gate /* recompute path after initchild for @addr information */
9427c478bd9Sstevel@tonic-gate (void) ddi_pathname(dip, path);
9437c478bd9Sstevel@tonic-gate
944f4da9be0Scth /* Check for duplicate nodes */
945f4da9be0Scth if (find_duplicate_child(pdip, dip) != NULL) {
9467c478bd9Sstevel@tonic-gate /*
9477c478bd9Sstevel@tonic-gate * uninit_node() the duplicate - a successful uninit_node()
948d6ae180bScth * will release inital hold of parent using ndi_rele_devi().
9497c478bd9Sstevel@tonic-gate */
9507c478bd9Sstevel@tonic-gate if ((error = uninit_node(dip)) != DDI_SUCCESS) {
951d6ae180bScth ndi_rele_devi(pdip); /* release initial hold */
9527c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "init_node: uninit of duplicate "
9537c478bd9Sstevel@tonic-gate "node %s failed", path);
9547c478bd9Sstevel@tonic-gate }
9557c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "init_node: duplicate uninit "
9567c478bd9Sstevel@tonic-gate "%s 0x%p%s\n", path, (void *)dip,
9577c478bd9Sstevel@tonic-gate (error == DDI_SUCCESS) ? "" : " failed"));
9587c478bd9Sstevel@tonic-gate error = DDI_FAILURE;
9597c478bd9Sstevel@tonic-gate goto out;
9607c478bd9Sstevel@tonic-gate }
9617c478bd9Sstevel@tonic-gate
9627c478bd9Sstevel@tonic-gate /*
9634f1e984dSReed * If a devid was registered for a DS_BOUND node then the devid_cache
9644f1e984dSReed * may not have captured the path. Detect this situation and ensure that
9654f1e984dSReed * the path enters the cache now that devi_addr is established.
9664f1e984dSReed */
9678451e9c3SGavin Maltby if (!(DEVI(dip)->devi_flags & DEVI_CACHED_DEVID) &&
9684f1e984dSReed (ddi_devid_get(dip, &devid) == DDI_SUCCESS)) {
9694f1e984dSReed if (e_devid_cache_register(dip, devid) == DDI_SUCCESS) {
9708451e9c3SGavin Maltby mutex_enter(&DEVI(dip)->devi_lock);
9718451e9c3SGavin Maltby DEVI(dip)->devi_flags |= DEVI_CACHED_DEVID;
9728451e9c3SGavin Maltby mutex_exit(&DEVI(dip)->devi_lock);
9734f1e984dSReed }
9744f1e984dSReed
9754f1e984dSReed ddi_devid_free(devid);
9764f1e984dSReed }
9774f1e984dSReed
9784f1e984dSReed /*
979f4da9be0Scth * Check to see if we have a path-oriented driver alias that overrides
980f4da9be0Scth * the current driver binding. If so, we need to rebind. This check
981f4da9be0Scth * needs to be delayed until after a successful DDI_CTLOPS_INITCHILD,
982f4da9be0Scth * so the unit-address is established on the last component of the path.
983f4da9be0Scth *
984f4da9be0Scth * NOTE: Allowing a path-oriented alias to change the driver binding
985f4da9be0Scth * of a driver.conf node results in non-intuitive property behavior.
986f4da9be0Scth * We provide a tunable (driver_conf_allow_path_alias) to control
987f4da9be0Scth * this behavior. See uninit_node() for more details.
988f4da9be0Scth *
989f4da9be0Scth * NOTE: If you are adding a path-oriented alias for the boot device,
990f4da9be0Scth * and there is mismatch between OBP and the kernel in regard to
991f4da9be0Scth * generic name use, like "disk" .vs. "ssd", then you will need
992f4da9be0Scth * to add a path-oriented alias for both paths.
993f4da9be0Scth */
994f4da9be0Scth major = ddi_name_to_major(path);
995c8742f64SJerry Gilliam if (driver_active(major) && (major != DEVI(dip)->devi_major) &&
996f4da9be0Scth (ndi_dev_is_persistent_node(dip) || driver_conf_allow_path_alias)) {
997f4da9be0Scth
998f4da9be0Scth /* Mark node for rebind processing. */
999f4da9be0Scth mutex_enter(&DEVI(dip)->devi_lock);
1000f4da9be0Scth DEVI(dip)->devi_flags |= DEVI_REBIND;
1001f4da9be0Scth mutex_exit(&DEVI(dip)->devi_lock);
1002f4da9be0Scth
1003f4da9be0Scth /*
1004d6ae180bScth * Add an extra hold on the parent to prevent it from ever
1005d6ae180bScth * having a zero devi_ref during the child rebind process.
1006d6ae180bScth * This is necessary to ensure that the parent will never
1007d6ae180bScth * detach(9E) during the rebind.
1008d6ae180bScth */
1009d6ae180bScth ndi_hold_devi(pdip); /* extra hold of parent */
1010d6ae180bScth
1011d6ae180bScth /*
1012f4da9be0Scth * uninit_node() current binding - a successful uninit_node()
1013d6ae180bScth * will release extra hold of parent using ndi_rele_devi().
1014f4da9be0Scth */
1015f4da9be0Scth if ((error = uninit_node(dip)) != DDI_SUCCESS) {
1016d6ae180bScth ndi_rele_devi(pdip); /* release extra hold */
1017d6ae180bScth ndi_rele_devi(pdip); /* release initial hold */
1018f4da9be0Scth cmn_err(CE_WARN, "init_node: uninit for rebind "
1019f4da9be0Scth "of node %s failed", path);
1020f4da9be0Scth goto out;
1021f4da9be0Scth }
1022f4da9be0Scth
1023f4da9be0Scth /* Unbind: demote the node back to DS_LINKED. */
1024f4da9be0Scth if ((error = ndi_devi_unbind_driver(dip)) != DDI_SUCCESS) {
1025027021c7SChris Horne ndi_rele_devi(pdip); /* release initial hold */
1026f4da9be0Scth cmn_err(CE_WARN, "init_node: unbind for rebind "
1027f4da9be0Scth "of node %s failed", path);
1028f4da9be0Scth goto out;
1029f4da9be0Scth }
1030f4da9be0Scth
1031f4da9be0Scth /* establish rebinding name */
1032f4da9be0Scth if (DEVI(dip)->devi_rebinding_name == NULL)
1033f4da9be0Scth DEVI(dip)->devi_rebinding_name =
1034f4da9be0Scth i_ddi_strdup(path, KM_SLEEP);
1035f4da9be0Scth
1036f4da9be0Scth /*
1037f4da9be0Scth * Now that we are demoted and marked for rebind, repromote.
1038f4da9be0Scth * We need to do this in steps, instead of just calling
1039f4da9be0Scth * ddi_initchild, so that we can redo the merge operation
1040f4da9be0Scth * after we are rebound to the path-bound driver.
1041f4da9be0Scth *
1042f4da9be0Scth * Start by rebinding node to the path-bound driver.
1043f4da9be0Scth */
1044f4da9be0Scth if ((error = ndi_devi_bind_driver(dip, 0)) != DDI_SUCCESS) {
1045027021c7SChris Horne ndi_rele_devi(pdip); /* release initial hold */
1046f4da9be0Scth cmn_err(CE_WARN, "init_node: rebind "
1047f4da9be0Scth "of node %s failed", path);
1048f4da9be0Scth goto out;
1049f4da9be0Scth }
1050f4da9be0Scth
1051f4da9be0Scth /*
1052f4da9be0Scth * If the node is not a driver.conf node then merge
1053f4da9be0Scth * driver.conf properties from new path-bound driver.conf.
1054f4da9be0Scth */
1055f4da9be0Scth if (ndi_dev_is_persistent_node(dip))
1056f4da9be0Scth (void) i_ndi_make_spec_children(pdip, 0);
1057f4da9be0Scth
1058f4da9be0Scth /*
1059f4da9be0Scth * Now that we have taken care of merge, repromote back
1060f4da9be0Scth * to DS_INITIALIZED.
1061f4da9be0Scth */
1062f4da9be0Scth error = ddi_initchild(pdip, dip);
1063f4da9be0Scth NDI_CONFIG_DEBUG((CE_CONT, "init_node: rebind "
1064f4da9be0Scth "%s 0x%p\n", path, (void *)dip));
1065d6ae180bScth
1066d6ae180bScth /*
1067d6ae180bScth * Release our initial hold. If ddi_initchild() was
1068b9ccdc5aScth * successful then it will return with the active hold.
1069d6ae180bScth */
1070d6ae180bScth ndi_rele_devi(pdip);
1071f4da9be0Scth goto out;
1072f4da9be0Scth }
1073f4da9be0Scth
1074f4da9be0Scth /*
10757c478bd9Sstevel@tonic-gate * Apply multi-parent/deep-nexus optimization to the new node
10767c478bd9Sstevel@tonic-gate */
10777c478bd9Sstevel@tonic-gate DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
10787c478bd9Sstevel@tonic-gate ddi_optimize_dtree(dip);
1079d6ae180bScth error = DDI_SUCCESS; /* return with active hold */
10807c478bd9Sstevel@tonic-gate
1081f4da9be0Scth out: if (error != DDI_SUCCESS) {
1082f4da9be0Scth /* On failure ensure that DEVI_REBIND is cleared */
1083f4da9be0Scth mutex_enter(&DEVI(dip)->devi_lock);
1084f4da9be0Scth DEVI(dip)->devi_flags &= ~DEVI_REBIND;
1085f4da9be0Scth mutex_exit(&DEVI(dip)->devi_lock);
1086f4da9be0Scth }
1087f4da9be0Scth kmem_free(path, MAXPATHLEN);
10887c478bd9Sstevel@tonic-gate return (error);
10897c478bd9Sstevel@tonic-gate }
10907c478bd9Sstevel@tonic-gate
10917c478bd9Sstevel@tonic-gate /*
10927c478bd9Sstevel@tonic-gate * Uninitialize node
10937c478bd9Sstevel@tonic-gate * The per-driver list must be held busy during the call.
10947c478bd9Sstevel@tonic-gate * A successful uninit_node() releases the init_node() hold on
10957c478bd9Sstevel@tonic-gate * the parent by calling ndi_rele_devi().
10967c478bd9Sstevel@tonic-gate */
10977c478bd9Sstevel@tonic-gate static int
uninit_node(dev_info_t * dip)10987c478bd9Sstevel@tonic-gate uninit_node(dev_info_t *dip)
10997c478bd9Sstevel@tonic-gate {
11007c478bd9Sstevel@tonic-gate int node_state_entry;
11017c478bd9Sstevel@tonic-gate dev_info_t *pdip;
11027c478bd9Sstevel@tonic-gate struct dev_ops *ops;
11037c478bd9Sstevel@tonic-gate int (*f)();
11047c478bd9Sstevel@tonic-gate int error;
11057c478bd9Sstevel@tonic-gate char *addr;
11067c478bd9Sstevel@tonic-gate
11077c478bd9Sstevel@tonic-gate /*
11087c478bd9Sstevel@tonic-gate * Don't check for references here or else a ref-counted
11097c478bd9Sstevel@tonic-gate * dip cannot be downgraded by the framework.
11107c478bd9Sstevel@tonic-gate */
11117c478bd9Sstevel@tonic-gate node_state_entry = i_ddi_node_state(dip);
11127c478bd9Sstevel@tonic-gate ASSERT((node_state_entry == DS_BOUND) ||
11137c478bd9Sstevel@tonic-gate (node_state_entry == DS_INITIALIZED));
11147c478bd9Sstevel@tonic-gate pdip = ddi_get_parent(dip);
11157c478bd9Sstevel@tonic-gate ASSERT(pdip);
11167c478bd9Sstevel@tonic-gate
11177c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "uninit_node: 0x%p(%s%d)\n",
11187c478bd9Sstevel@tonic-gate (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
11197c478bd9Sstevel@tonic-gate
11207c478bd9Sstevel@tonic-gate if (((ops = ddi_get_driver(pdip)) == NULL) ||
11217c478bd9Sstevel@tonic-gate (ops->devo_bus_ops == NULL) ||
11227c478bd9Sstevel@tonic-gate ((f = ops->devo_bus_ops->bus_ctl) == NULL)) {
11237c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
11247c478bd9Sstevel@tonic-gate }
11257c478bd9Sstevel@tonic-gate
11267c478bd9Sstevel@tonic-gate /*
11277c478bd9Sstevel@tonic-gate * save the @addr prior to DDI_CTLOPS_UNINITCHILD for use in
11287c478bd9Sstevel@tonic-gate * freeing the instance if it succeeds.
11297c478bd9Sstevel@tonic-gate */
11307c478bd9Sstevel@tonic-gate if (node_state_entry == DS_INITIALIZED) {
11317c478bd9Sstevel@tonic-gate addr = ddi_get_name_addr(dip);
11327c478bd9Sstevel@tonic-gate if (addr)
11337c478bd9Sstevel@tonic-gate addr = i_ddi_strdup(addr, KM_SLEEP);
11347c478bd9Sstevel@tonic-gate } else {
11357c478bd9Sstevel@tonic-gate addr = NULL;
11367c478bd9Sstevel@tonic-gate }
11377c478bd9Sstevel@tonic-gate
11387c478bd9Sstevel@tonic-gate error = (*f)(pdip, pdip, DDI_CTLOPS_UNINITCHILD, dip, (void *)NULL);
11397c478bd9Sstevel@tonic-gate if (error == DDI_SUCCESS) {
1140470452aaSPrasad Singamsetty /* ensure that devids are unregistered */
11418451e9c3SGavin Maltby mutex_enter(&DEVI(dip)->devi_lock);
11428451e9c3SGavin Maltby if ((DEVI(dip)->devi_flags & DEVI_CACHED_DEVID)) {
11438451e9c3SGavin Maltby DEVI(dip)->devi_flags &= ~DEVI_CACHED_DEVID;
11448451e9c3SGavin Maltby mutex_exit(&DEVI(dip)->devi_lock);
1145470452aaSPrasad Singamsetty ddi_devid_unregister(dip);
11468451e9c3SGavin Maltby } else
11478451e9c3SGavin Maltby mutex_exit(&DEVI(dip)->devi_lock);
1148470452aaSPrasad Singamsetty
11497c478bd9Sstevel@tonic-gate /* if uninitchild forgot to set devi_addr to NULL do it now */
11507c478bd9Sstevel@tonic-gate ddi_set_name_addr(dip, NULL);
11517c478bd9Sstevel@tonic-gate
11527c478bd9Sstevel@tonic-gate /*
11537c478bd9Sstevel@tonic-gate * Free instance number. This is a no-op if instance has
11547c478bd9Sstevel@tonic-gate * been kept by probe_node(). Avoid free when we are called
11557c478bd9Sstevel@tonic-gate * from init_node (DS_BOUND) because the instance has not yet
11567c478bd9Sstevel@tonic-gate * been assigned.
11577c478bd9Sstevel@tonic-gate */
11587c478bd9Sstevel@tonic-gate if (node_state_entry == DS_INITIALIZED) {
11597c478bd9Sstevel@tonic-gate e_ddi_free_instance(dip, addr);
11607c478bd9Sstevel@tonic-gate DEVI(dip)->devi_instance = -1;
11617c478bd9Sstevel@tonic-gate }
11627c478bd9Sstevel@tonic-gate
11637c478bd9Sstevel@tonic-gate /* release the init_node hold */
11647c478bd9Sstevel@tonic-gate ndi_rele_devi(pdip);
11657c478bd9Sstevel@tonic-gate
11667c478bd9Sstevel@tonic-gate remove_global_props(dip);
1167f4da9be0Scth
1168f4da9be0Scth /*
1169f4da9be0Scth * NOTE: The decision on whether to allow a path-oriented
1170f4da9be0Scth * rebind of a driver.conf enumerated node is made by
1171f4da9be0Scth * init_node() based on driver_conf_allow_path_alias. The
1172f4da9be0Scth * rebind code below prevents deletion of system properties
1173f4da9be0Scth * on driver.conf nodes.
1174f4da9be0Scth *
1175f4da9be0Scth * When driver_conf_allow_path_alias is set, property behavior
1176f4da9be0Scth * on rebound driver.conf file is non-intuitive. For a
1177f4da9be0Scth * driver.conf node, the unit-address properties come from
1178f4da9be0Scth * the driver.conf file as system properties. Removing system
1179f4da9be0Scth * properties from a driver.conf node makes the node
1180f4da9be0Scth * useless (we get node without unit-address properties) - so
1181f4da9be0Scth * we leave system properties in place. The result is a node
1182f4da9be0Scth * where system properties come from the node being rebound,
1183f4da9be0Scth * and global properties come from the driver.conf file
1184f4da9be0Scth * of the driver we are rebinding to. If we could determine
1185f4da9be0Scth * that the path-oriented alias driver.conf file defined a
1186f4da9be0Scth * node at the same unit address, it would be best to use
1187f4da9be0Scth * that node and avoid the non-intuitive property behavior.
1188f4da9be0Scth * Unfortunately, the current "merge" code does not support
1189f4da9be0Scth * this, so we live with the non-intuitive property behavior.
1190f4da9be0Scth */
1191f4da9be0Scth if (!((ndi_dev_is_persistent_node(dip) == 0) &&
1192f4da9be0Scth (DEVI(dip)->devi_flags & DEVI_REBIND)))
11937c478bd9Sstevel@tonic-gate e_ddi_prop_remove_all(dip);
11947c478bd9Sstevel@tonic-gate } else {
11957c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "uninit_node failed: 0x%p(%s%d)\n",
11967c478bd9Sstevel@tonic-gate (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
11977c478bd9Sstevel@tonic-gate }
11987c478bd9Sstevel@tonic-gate
11997c478bd9Sstevel@tonic-gate if (addr)
12007c478bd9Sstevel@tonic-gate kmem_free(addr, strlen(addr) + 1);
12017c478bd9Sstevel@tonic-gate return (error);
12027c478bd9Sstevel@tonic-gate }
12037c478bd9Sstevel@tonic-gate
12047c478bd9Sstevel@tonic-gate /*
12057c478bd9Sstevel@tonic-gate * Invoke driver's probe entry point to probe for existence of hardware.
12067c478bd9Sstevel@tonic-gate * Keep instance permanent for successful probe and leaf nodes.
12077c478bd9Sstevel@tonic-gate *
12087c478bd9Sstevel@tonic-gate * Per-driver list must be held busy while calling this function.
12097c478bd9Sstevel@tonic-gate */
12107c478bd9Sstevel@tonic-gate static int
probe_node(dev_info_t * dip)12117c478bd9Sstevel@tonic-gate probe_node(dev_info_t *dip)
12127c478bd9Sstevel@tonic-gate {
12137c478bd9Sstevel@tonic-gate int rv;
12147c478bd9Sstevel@tonic-gate
12157c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(dip) == DS_INITIALIZED);
12167c478bd9Sstevel@tonic-gate
12177c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "probe_node: 0x%p(%s%d)\n",
12187c478bd9Sstevel@tonic-gate (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
12197c478bd9Sstevel@tonic-gate
12207c478bd9Sstevel@tonic-gate /* temporarily hold the driver while we probe */
12217c478bd9Sstevel@tonic-gate DEVI(dip)->devi_ops = ndi_hold_driver(dip);
12227c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_ops == NULL) {
12237c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT,
12247c478bd9Sstevel@tonic-gate "probe_node: 0x%p(%s%d) cannot load driver\n",
12257c478bd9Sstevel@tonic-gate (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
12267c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
12277c478bd9Sstevel@tonic-gate }
12287c478bd9Sstevel@tonic-gate
12297c478bd9Sstevel@tonic-gate if (identify_9e != 0)
12307c478bd9Sstevel@tonic-gate (void) devi_identify(dip);
12317c478bd9Sstevel@tonic-gate
12327c478bd9Sstevel@tonic-gate rv = devi_probe(dip);
12337c478bd9Sstevel@tonic-gate
12347c478bd9Sstevel@tonic-gate /* release the driver now that probe is complete */
12357c478bd9Sstevel@tonic-gate ndi_rele_driver(dip);
12367c478bd9Sstevel@tonic-gate DEVI(dip)->devi_ops = NULL;
12377c478bd9Sstevel@tonic-gate
12387c478bd9Sstevel@tonic-gate switch (rv) {
12397c478bd9Sstevel@tonic-gate case DDI_PROBE_SUCCESS: /* found */
12407c478bd9Sstevel@tonic-gate case DDI_PROBE_DONTCARE: /* ddi_dev_is_sid */
12417c478bd9Sstevel@tonic-gate e_ddi_keep_instance(dip); /* persist instance */
12427c478bd9Sstevel@tonic-gate rv = DDI_SUCCESS;
12437c478bd9Sstevel@tonic-gate break;
12447c478bd9Sstevel@tonic-gate
12457c478bd9Sstevel@tonic-gate case DDI_PROBE_PARTIAL: /* maybe later */
12467c478bd9Sstevel@tonic-gate case DDI_PROBE_FAILURE: /* not found */
12477c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT,
12487c478bd9Sstevel@tonic-gate "probe_node: 0x%p(%s%d) no hardware found%s\n",
12497c478bd9Sstevel@tonic-gate (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip),
12507c478bd9Sstevel@tonic-gate (rv == DDI_PROBE_PARTIAL) ? " yet" : ""));
12517c478bd9Sstevel@tonic-gate rv = DDI_FAILURE;
12527c478bd9Sstevel@tonic-gate break;
12537c478bd9Sstevel@tonic-gate
12547c478bd9Sstevel@tonic-gate default:
12557c478bd9Sstevel@tonic-gate #ifdef DEBUG
12567c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "probe_node: %s%d: illegal probe(9E) value",
12577c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip));
12587c478bd9Sstevel@tonic-gate #endif /* DEBUG */
12597c478bd9Sstevel@tonic-gate rv = DDI_FAILURE;
12607c478bd9Sstevel@tonic-gate break;
12617c478bd9Sstevel@tonic-gate }
12627c478bd9Sstevel@tonic-gate return (rv);
12637c478bd9Sstevel@tonic-gate }
12647c478bd9Sstevel@tonic-gate
12657c478bd9Sstevel@tonic-gate /*
12667c478bd9Sstevel@tonic-gate * Unprobe a node. Simply reset the node state.
12677c478bd9Sstevel@tonic-gate * Per-driver list must be held busy while calling this function.
12687c478bd9Sstevel@tonic-gate */
12697c478bd9Sstevel@tonic-gate static int
unprobe_node(dev_info_t * dip)12707c478bd9Sstevel@tonic-gate unprobe_node(dev_info_t *dip)
12717c478bd9Sstevel@tonic-gate {
12727c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(dip) == DS_PROBED);
12737c478bd9Sstevel@tonic-gate
12747c478bd9Sstevel@tonic-gate /*
12757c478bd9Sstevel@tonic-gate * Don't check for references here or else a ref-counted
12767c478bd9Sstevel@tonic-gate * dip cannot be downgraded by the framework.
12777c478bd9Sstevel@tonic-gate */
12787c478bd9Sstevel@tonic-gate
12797c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "unprobe_node: 0x%p(name = %s)\n",
12807c478bd9Sstevel@tonic-gate (void *)dip, ddi_node_name(dip)));
12817c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
12827c478bd9Sstevel@tonic-gate }
12837c478bd9Sstevel@tonic-gate
12847c478bd9Sstevel@tonic-gate /*
12857c478bd9Sstevel@tonic-gate * Attach devinfo node.
12867c478bd9Sstevel@tonic-gate * Per-driver list must be held busy.
12877c478bd9Sstevel@tonic-gate */
12887c478bd9Sstevel@tonic-gate static int
attach_node(dev_info_t * dip)12897c478bd9Sstevel@tonic-gate attach_node(dev_info_t *dip)
12907c478bd9Sstevel@tonic-gate {
12917c478bd9Sstevel@tonic-gate int rv;
12927c478bd9Sstevel@tonic-gate
12935e3986cbScth ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
12947c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(dip) == DS_PROBED);
12957c478bd9Sstevel@tonic-gate
12967c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d)\n",
12977c478bd9Sstevel@tonic-gate (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
12987c478bd9Sstevel@tonic-gate
12997c478bd9Sstevel@tonic-gate /*
13007c478bd9Sstevel@tonic-gate * Tell mpxio framework that a node is about to online.
13017c478bd9Sstevel@tonic-gate */
13027c478bd9Sstevel@tonic-gate if ((rv = mdi_devi_online(dip, 0)) != NDI_SUCCESS) {
13037c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
13047c478bd9Sstevel@tonic-gate }
13057c478bd9Sstevel@tonic-gate
13067c478bd9Sstevel@tonic-gate /* no recursive attachment */
13077c478bd9Sstevel@tonic-gate ASSERT(DEVI(dip)->devi_ops == NULL);
13087c478bd9Sstevel@tonic-gate
13097c478bd9Sstevel@tonic-gate /*
13107c478bd9Sstevel@tonic-gate * Hold driver the node is bound to.
13117c478bd9Sstevel@tonic-gate */
13127c478bd9Sstevel@tonic-gate DEVI(dip)->devi_ops = ndi_hold_driver(dip);
13137c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_ops == NULL) {
13147c478bd9Sstevel@tonic-gate /*
13157c478bd9Sstevel@tonic-gate * We were able to load driver for probing, so we should
13167c478bd9Sstevel@tonic-gate * not get here unless something really bad happened.
13177c478bd9Sstevel@tonic-gate */
13187c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "attach_node: no driver for major %d",
13197c478bd9Sstevel@tonic-gate DEVI(dip)->devi_major);
13207c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
13217c478bd9Sstevel@tonic-gate }
13227c478bd9Sstevel@tonic-gate
13237c478bd9Sstevel@tonic-gate if (NEXUS_DRV(DEVI(dip)->devi_ops))
13247c478bd9Sstevel@tonic-gate DEVI(dip)->devi_taskq = ddi_taskq_create(dip,
13257c478bd9Sstevel@tonic-gate "nexus_enum_tq", 1,
13267c478bd9Sstevel@tonic-gate TASKQ_DEFAULTPRI, 0);
13277c478bd9Sstevel@tonic-gate
132816747f41Scth mutex_enter(&(DEVI(dip)->devi_lock));
1329c73a93f2Sdm120769 DEVI_SET_ATTACHING(dip);
13307c478bd9Sstevel@tonic-gate DEVI_SET_NEED_RESET(dip);
133116747f41Scth mutex_exit(&(DEVI(dip)->devi_lock));
133216747f41Scth
13337c478bd9Sstevel@tonic-gate rv = devi_attach(dip, DDI_ATTACH);
133416747f41Scth
1335144dfaa9Scth mutex_enter(&(DEVI(dip)->devi_lock));
1336c73a93f2Sdm120769 DEVI_CLR_ATTACHING(dip);
1337144dfaa9Scth
1338c73a93f2Sdm120769 if (rv != DDI_SUCCESS) {
13395e3986cbScth DEVI_CLR_NEED_RESET(dip);
1340225b11cdScth mutex_exit(&DEVI(dip)->devi_lock);
1341225b11cdScth
13427c478bd9Sstevel@tonic-gate /*
13437c478bd9Sstevel@tonic-gate * Cleanup dacf reservations
13447c478bd9Sstevel@tonic-gate */
13457c478bd9Sstevel@tonic-gate mutex_enter(&dacf_lock);
13467c478bd9Sstevel@tonic-gate dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
13477c478bd9Sstevel@tonic-gate dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
13487c478bd9Sstevel@tonic-gate mutex_exit(&dacf_lock);
13497c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_taskq)
13507c478bd9Sstevel@tonic-gate ddi_taskq_destroy(DEVI(dip)->devi_taskq);
13517c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
13527c478bd9Sstevel@tonic-gate
13537c478bd9Sstevel@tonic-gate /* release the driver if attach failed */
13547c478bd9Sstevel@tonic-gate ndi_rele_driver(dip);
13557c478bd9Sstevel@tonic-gate DEVI(dip)->devi_ops = NULL;
13567c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d) failed\n",
13577c478bd9Sstevel@tonic-gate (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
13587c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
1359c73a93f2Sdm120769 } else
1360c73a93f2Sdm120769 mutex_exit(&DEVI(dip)->devi_lock);
13617c478bd9Sstevel@tonic-gate
13627c478bd9Sstevel@tonic-gate /* successful attach, return with driver held */
136316747f41Scth
13647c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
13657c478bd9Sstevel@tonic-gate }
13667c478bd9Sstevel@tonic-gate
13677c478bd9Sstevel@tonic-gate /*
13687c478bd9Sstevel@tonic-gate * Detach devinfo node.
13697c478bd9Sstevel@tonic-gate * Per-driver list must be held busy.
13707c478bd9Sstevel@tonic-gate */
13717c478bd9Sstevel@tonic-gate static int
detach_node(dev_info_t * dip,uint_t flag)13727c478bd9Sstevel@tonic-gate detach_node(dev_info_t *dip, uint_t flag)
13737c478bd9Sstevel@tonic-gate {
1374cfbaf6c3Scth struct devnames *dnp;
13757c478bd9Sstevel@tonic-gate int rv;
1376cfbaf6c3Scth
13775e3986cbScth ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
13787c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(dip) == DS_ATTACHED);
13797c478bd9Sstevel@tonic-gate
13807c478bd9Sstevel@tonic-gate /* check references */
13817c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_ref)
13827c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
13837c478bd9Sstevel@tonic-gate
13847c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "detach_node: 0x%p(%s%d)\n",
13857c478bd9Sstevel@tonic-gate (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
13867c478bd9Sstevel@tonic-gate
13875e3986cbScth /*
13885e3986cbScth * NOTE: If we are processing a pHCI node then the calling code
13895e3986cbScth * must detect this and ndi_devi_enter() in (vHCI, parent(pHCI))
13905e3986cbScth * order unless pHCI and vHCI are siblings. Code paths leading
13915e3986cbScth * here that must ensure this ordering include:
13925e3986cbScth * unconfig_immediate_children(), devi_unconfig_one(),
13935e3986cbScth * ndi_devi_unconfig_one(), ndi_devi_offline().
13945e3986cbScth */
13955e3986cbScth ASSERT(!MDI_PHCI(dip) ||
13965e3986cbScth (ddi_get_parent(mdi_devi_get_vdip(dip)) == ddi_get_parent(dip)) ||
13975e3986cbScth DEVI_BUSY_OWNED(mdi_devi_get_vdip(dip)));
13985e3986cbScth
13997c478bd9Sstevel@tonic-gate /* Offline the device node with the mpxio framework. */
14007c478bd9Sstevel@tonic-gate if (mdi_devi_offline(dip, flag) != NDI_SUCCESS) {
14017c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
14027c478bd9Sstevel@tonic-gate }
14037c478bd9Sstevel@tonic-gate
14047c478bd9Sstevel@tonic-gate /* drain the taskq */
14057c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_taskq)
14067c478bd9Sstevel@tonic-gate ddi_taskq_wait(DEVI(dip)->devi_taskq);
14077c478bd9Sstevel@tonic-gate
14087c478bd9Sstevel@tonic-gate rv = devi_detach(dip, DDI_DETACH);
14097c478bd9Sstevel@tonic-gate
14107c478bd9Sstevel@tonic-gate if (rv != DDI_SUCCESS) {
14117c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT,
14127c478bd9Sstevel@tonic-gate "detach_node: 0x%p(%s%d) failed\n",
14137c478bd9Sstevel@tonic-gate (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
14147c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
14157c478bd9Sstevel@tonic-gate }
14167c478bd9Sstevel@tonic-gate
14175e3986cbScth mutex_enter(&(DEVI(dip)->devi_lock));
14185e3986cbScth DEVI_CLR_NEED_RESET(dip);
14195e3986cbScth mutex_exit(&(DEVI(dip)->devi_lock));
14205e3986cbScth
14213a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
142209011d40SVikram Hegde /*
142309011d40SVikram Hegde * Close any iommulib mediated linkage to an IOMMU
142409011d40SVikram Hegde */
142550200e77SFrank Van Der Linden if (IOMMU_USED(dip))
142609011d40SVikram Hegde iommulib_nex_close(dip);
142709011d40SVikram Hegde #endif
142809011d40SVikram Hegde
14297c478bd9Sstevel@tonic-gate /* destroy the taskq */
14307c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_taskq) {
14317c478bd9Sstevel@tonic-gate ddi_taskq_destroy(DEVI(dip)->devi_taskq);
14327c478bd9Sstevel@tonic-gate DEVI(dip)->devi_taskq = NULL;
14337c478bd9Sstevel@tonic-gate }
14347c478bd9Sstevel@tonic-gate
14357c478bd9Sstevel@tonic-gate /* Cleanup dacf reservations */
14367c478bd9Sstevel@tonic-gate mutex_enter(&dacf_lock);
14377c478bd9Sstevel@tonic-gate dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
14387c478bd9Sstevel@tonic-gate dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
14397c478bd9Sstevel@tonic-gate mutex_exit(&dacf_lock);
14407c478bd9Sstevel@tonic-gate
1441255a2d50SMatthew Jacob /* remove any additional flavors that were added */
1442255a2d50SMatthew Jacob if (DEVI(dip)->devi_flavorv_n > 1 && DEVI(dip)->devi_flavorv != NULL) {
1443255a2d50SMatthew Jacob kmem_free(DEVI(dip)->devi_flavorv,
1444255a2d50SMatthew Jacob (DEVI(dip)->devi_flavorv_n - 1) * sizeof (void *));
1445255a2d50SMatthew Jacob DEVI(dip)->devi_flavorv = NULL;
1446255a2d50SMatthew Jacob }
1447255a2d50SMatthew Jacob
14487c478bd9Sstevel@tonic-gate /* Remove properties and minor nodes in case driver forgots */
14497c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
14507c478bd9Sstevel@tonic-gate ddi_prop_remove_all(dip);
14517c478bd9Sstevel@tonic-gate
14527c478bd9Sstevel@tonic-gate /* a detached node can't have attached or .conf children */
14537c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(dip)->devi_lock);
14547c478bd9Sstevel@tonic-gate DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN|DEVI_ATTACHED_CHILDREN);
14557c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(dip)->devi_lock);
14567c478bd9Sstevel@tonic-gate
1457cfbaf6c3Scth /*
1458cfbaf6c3Scth * If the instance has successfully detached in detach_driver() context,
1459cfbaf6c3Scth * clear DN_DRIVER_HELD for correct ddi_hold_installed_driver()
1460cfbaf6c3Scth * behavior. Consumers like qassociate() depend on this (via clnopen()).
1461cfbaf6c3Scth */
1462cfbaf6c3Scth if (flag & NDI_DETACH_DRIVER) {
1463cfbaf6c3Scth dnp = &(devnamesp[DEVI(dip)->devi_major]);
1464cfbaf6c3Scth LOCK_DEV_OPS(&dnp->dn_lock);
1465cfbaf6c3Scth dnp->dn_flags &= ~DN_DRIVER_HELD;
1466cfbaf6c3Scth UNLOCK_DEV_OPS(&dnp->dn_lock);
1467cfbaf6c3Scth }
1468cfbaf6c3Scth
14697c478bd9Sstevel@tonic-gate /* successful detach, release the driver */
14707c478bd9Sstevel@tonic-gate ndi_rele_driver(dip);
14717c478bd9Sstevel@tonic-gate DEVI(dip)->devi_ops = NULL;
14727c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
14737c478bd9Sstevel@tonic-gate }
14747c478bd9Sstevel@tonic-gate
14757c478bd9Sstevel@tonic-gate /*
14767c478bd9Sstevel@tonic-gate * Run dacf post_attach routines
14777c478bd9Sstevel@tonic-gate */
14787c478bd9Sstevel@tonic-gate static int
postattach_node(dev_info_t * dip)14797c478bd9Sstevel@tonic-gate postattach_node(dev_info_t *dip)
14807c478bd9Sstevel@tonic-gate {
14817c478bd9Sstevel@tonic-gate int rval;
14827c478bd9Sstevel@tonic-gate
14837c478bd9Sstevel@tonic-gate /*
14847c478bd9Sstevel@tonic-gate * For hotplug busses like USB, it's possible that devices
14857c478bd9Sstevel@tonic-gate * are removed but dip is still around. We don't want to
14867c478bd9Sstevel@tonic-gate * run dacf routines as part of detach failure recovery.
14877c478bd9Sstevel@tonic-gate *
14887c478bd9Sstevel@tonic-gate * Pretend success until we figure out how to prevent
14897c478bd9Sstevel@tonic-gate * access to such devinfo nodes.
14907c478bd9Sstevel@tonic-gate */
14917c478bd9Sstevel@tonic-gate if (DEVI_IS_DEVICE_REMOVED(dip))
14927c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
14937c478bd9Sstevel@tonic-gate
14947c478bd9Sstevel@tonic-gate /*
14957c478bd9Sstevel@tonic-gate * if dacf_postattach failed, report it to the framework
14967c478bd9Sstevel@tonic-gate * so that it can be retried later at the open time.
14977c478bd9Sstevel@tonic-gate */
14987c478bd9Sstevel@tonic-gate mutex_enter(&dacf_lock);
14997c478bd9Sstevel@tonic-gate rval = dacfc_postattach(dip);
15007c478bd9Sstevel@tonic-gate mutex_exit(&dacf_lock);
15017c478bd9Sstevel@tonic-gate
15027c478bd9Sstevel@tonic-gate /*
15037c478bd9Sstevel@tonic-gate * Plumbing during postattach may fail because of the
15047c478bd9Sstevel@tonic-gate * underlying device is not ready. This will fail ndi_devi_config()
150530c304d9SJoshua M. Clulow * in dv_filldir().
15067c478bd9Sstevel@tonic-gate */
15077c478bd9Sstevel@tonic-gate if (rval != DACF_SUCCESS) {
150830c304d9SJoshua M. Clulow NDI_CONFIG_DEBUG((CE_CONT, "postattach_node: %s%d (%p) "
150930c304d9SJoshua M. Clulow "postattach failed\n", ddi_driver_name(dip),
151030c304d9SJoshua M. Clulow ddi_get_instance(dip), (void *)dip));
15117c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
15127c478bd9Sstevel@tonic-gate }
15137c478bd9Sstevel@tonic-gate
15147c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
15157c478bd9Sstevel@tonic-gate }
15167c478bd9Sstevel@tonic-gate
15177c478bd9Sstevel@tonic-gate /*
15187c478bd9Sstevel@tonic-gate * Run dacf pre-detach routines
15197c478bd9Sstevel@tonic-gate */
15207c478bd9Sstevel@tonic-gate static int
predetach_node(dev_info_t * dip,uint_t flag)15217c478bd9Sstevel@tonic-gate predetach_node(dev_info_t *dip, uint_t flag)
15227c478bd9Sstevel@tonic-gate {
15237c478bd9Sstevel@tonic-gate int ret;
15247c478bd9Sstevel@tonic-gate
15257c478bd9Sstevel@tonic-gate /*
15267c478bd9Sstevel@tonic-gate * Don't auto-detach if DDI_FORCEATTACH or DDI_NO_AUTODETACH
15277c478bd9Sstevel@tonic-gate * properties are set.
15287c478bd9Sstevel@tonic-gate */
15297c478bd9Sstevel@tonic-gate if (flag & NDI_AUTODETACH) {
15307c478bd9Sstevel@tonic-gate struct devnames *dnp;
15317c478bd9Sstevel@tonic-gate int pflag = DDI_PROP_NOTPROM | DDI_PROP_DONTPASS;
15327c478bd9Sstevel@tonic-gate
15337c478bd9Sstevel@tonic-gate if ((ddi_prop_get_int(DDI_DEV_T_ANY, dip,
15347c478bd9Sstevel@tonic-gate pflag, DDI_FORCEATTACH, 0) == 1) ||
15357c478bd9Sstevel@tonic-gate (ddi_prop_get_int(DDI_DEV_T_ANY, dip,
15367c478bd9Sstevel@tonic-gate pflag, DDI_NO_AUTODETACH, 0) == 1))
15377c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
15387c478bd9Sstevel@tonic-gate
15397c478bd9Sstevel@tonic-gate /* check for driver global version of DDI_NO_AUTODETACH */
15407c478bd9Sstevel@tonic-gate dnp = &devnamesp[DEVI(dip)->devi_major];
15417c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock);
15427c478bd9Sstevel@tonic-gate if (dnp->dn_flags & DN_NO_AUTODETACH) {
15437c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
15447c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
15457c478bd9Sstevel@tonic-gate }
15467c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
15477c478bd9Sstevel@tonic-gate }
15487c478bd9Sstevel@tonic-gate
15497c478bd9Sstevel@tonic-gate mutex_enter(&dacf_lock);
15507c478bd9Sstevel@tonic-gate ret = dacfc_predetach(dip);
15517c478bd9Sstevel@tonic-gate mutex_exit(&dacf_lock);
15527c478bd9Sstevel@tonic-gate
15537c478bd9Sstevel@tonic-gate return (ret);
15547c478bd9Sstevel@tonic-gate }
15557c478bd9Sstevel@tonic-gate
15567c478bd9Sstevel@tonic-gate /*
15577c478bd9Sstevel@tonic-gate * Wrapper for making multiple state transitions
15587c478bd9Sstevel@tonic-gate */
15597c478bd9Sstevel@tonic-gate
15607c478bd9Sstevel@tonic-gate /*
15617c478bd9Sstevel@tonic-gate * i_ndi_config_node: upgrade dev_info node into a specified state.
15627c478bd9Sstevel@tonic-gate * It is a bit tricky because the locking protocol changes before and
15637c478bd9Sstevel@tonic-gate * after a node is bound to a driver. All locks are held external to
15647c478bd9Sstevel@tonic-gate * this function.
15657c478bd9Sstevel@tonic-gate */
15667c478bd9Sstevel@tonic-gate int
i_ndi_config_node(dev_info_t * dip,ddi_node_state_t state,uint_t flag)15677c478bd9Sstevel@tonic-gate i_ndi_config_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
15687c478bd9Sstevel@tonic-gate {
15697c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(flag))
15707c478bd9Sstevel@tonic-gate int rv = DDI_SUCCESS;
15717c478bd9Sstevel@tonic-gate
15727c478bd9Sstevel@tonic-gate ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
15737c478bd9Sstevel@tonic-gate
15747c478bd9Sstevel@tonic-gate while ((i_ddi_node_state(dip) < state) && (rv == DDI_SUCCESS)) {
15757c478bd9Sstevel@tonic-gate
15767c478bd9Sstevel@tonic-gate /* don't allow any more changes to the device tree */
15777c478bd9Sstevel@tonic-gate if (devinfo_freeze) {
15787c478bd9Sstevel@tonic-gate rv = DDI_FAILURE;
15797c478bd9Sstevel@tonic-gate break;
15807c478bd9Sstevel@tonic-gate }
15817c478bd9Sstevel@tonic-gate
15827c478bd9Sstevel@tonic-gate switch (i_ddi_node_state(dip)) {
15837c478bd9Sstevel@tonic-gate case DS_PROTO:
15847c478bd9Sstevel@tonic-gate /*
15857c478bd9Sstevel@tonic-gate * only caller can reference this node, no external
15867c478bd9Sstevel@tonic-gate * locking needed.
15877c478bd9Sstevel@tonic-gate */
15887c478bd9Sstevel@tonic-gate link_node(dip);
15897c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_LINKED);
15907c478bd9Sstevel@tonic-gate break;
15917c478bd9Sstevel@tonic-gate case DS_LINKED:
15927c478bd9Sstevel@tonic-gate /*
15937c478bd9Sstevel@tonic-gate * Three code path may attempt to bind a node:
15947c478bd9Sstevel@tonic-gate * - boot code
15957c478bd9Sstevel@tonic-gate * - add_drv
15967c478bd9Sstevel@tonic-gate * - hotplug thread
15977c478bd9Sstevel@tonic-gate * Boot code is single threaded, add_drv synchronize
15987c478bd9Sstevel@tonic-gate * on a userland lock, and hotplug synchronize on
15997c478bd9Sstevel@tonic-gate * hotplug_lk. There could be a race between add_drv
16007c478bd9Sstevel@tonic-gate * and hotplug thread. We'll live with this until the
16017c478bd9Sstevel@tonic-gate * conversion to top-down loading.
16027c478bd9Sstevel@tonic-gate */
16037c478bd9Sstevel@tonic-gate if ((rv = bind_node(dip)) == DDI_SUCCESS)
16047c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_BOUND);
1605f4da9be0Scth
16067c478bd9Sstevel@tonic-gate break;
16077c478bd9Sstevel@tonic-gate case DS_BOUND:
16087c478bd9Sstevel@tonic-gate /*
16097c478bd9Sstevel@tonic-gate * The following transitions synchronizes on the
16107c478bd9Sstevel@tonic-gate * per-driver busy changing flag, since we already
16117c478bd9Sstevel@tonic-gate * have a driver.
16127c478bd9Sstevel@tonic-gate */
16137c478bd9Sstevel@tonic-gate if ((rv = init_node(dip)) == DDI_SUCCESS)
16147c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_INITIALIZED);
16157c478bd9Sstevel@tonic-gate break;
16167c478bd9Sstevel@tonic-gate case DS_INITIALIZED:
16177c478bd9Sstevel@tonic-gate if ((rv = probe_node(dip)) == DDI_SUCCESS)
16187c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_PROBED);
16197c478bd9Sstevel@tonic-gate break;
16207c478bd9Sstevel@tonic-gate case DS_PROBED:
1621e58a33b6SStephen Hanson /*
1622e58a33b6SStephen Hanson * If node is retired and persistent, then prevent
1623e58a33b6SStephen Hanson * attach. We can't do this for non-persistent nodes
1624e58a33b6SStephen Hanson * as we would lose evidence that the node existed.
1625e58a33b6SStephen Hanson */
1626e58a33b6SStephen Hanson if (i_ddi_check_retire(dip) == 1 &&
1627e58a33b6SStephen Hanson ndi_dev_is_persistent_node(dip) &&
1628e58a33b6SStephen Hanson retire_prevents_attach == 1) {
1629e58a33b6SStephen Hanson rv = DDI_FAILURE;
1630e58a33b6SStephen Hanson break;
1631e58a33b6SStephen Hanson }
16321a5e258fSJosef 'Jeff' Sipek atomic_inc_ulong(&devinfo_attach_detach);
16337c478bd9Sstevel@tonic-gate if ((rv = attach_node(dip)) == DDI_SUCCESS)
16347c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_ATTACHED);
16351a5e258fSJosef 'Jeff' Sipek atomic_dec_ulong(&devinfo_attach_detach);
16367c478bd9Sstevel@tonic-gate break;
16377c478bd9Sstevel@tonic-gate case DS_ATTACHED:
16387c478bd9Sstevel@tonic-gate if ((rv = postattach_node(dip)) == DDI_SUCCESS)
16397c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_READY);
16407c478bd9Sstevel@tonic-gate break;
16417c478bd9Sstevel@tonic-gate case DS_READY:
16427c478bd9Sstevel@tonic-gate break;
16437c478bd9Sstevel@tonic-gate default:
16447c478bd9Sstevel@tonic-gate /* should never reach here */
16457c478bd9Sstevel@tonic-gate ASSERT("unknown devinfo state");
16467c478bd9Sstevel@tonic-gate }
16477c478bd9Sstevel@tonic-gate }
16487c478bd9Sstevel@tonic-gate
16497c478bd9Sstevel@tonic-gate if (ddidebug & DDI_AUDIT)
16507c478bd9Sstevel@tonic-gate da_log_enter(dip);
16517c478bd9Sstevel@tonic-gate return (rv);
16527c478bd9Sstevel@tonic-gate }
16537c478bd9Sstevel@tonic-gate
16547c478bd9Sstevel@tonic-gate /*
16557c478bd9Sstevel@tonic-gate * i_ndi_unconfig_node: downgrade dev_info node into a specified state.
16567c478bd9Sstevel@tonic-gate */
16577c478bd9Sstevel@tonic-gate int
i_ndi_unconfig_node(dev_info_t * dip,ddi_node_state_t state,uint_t flag)16587c478bd9Sstevel@tonic-gate i_ndi_unconfig_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
16597c478bd9Sstevel@tonic-gate {
16607c478bd9Sstevel@tonic-gate int rv = DDI_SUCCESS;
16617c478bd9Sstevel@tonic-gate
16627c478bd9Sstevel@tonic-gate ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
16637c478bd9Sstevel@tonic-gate
16647c478bd9Sstevel@tonic-gate while ((i_ddi_node_state(dip) > state) && (rv == DDI_SUCCESS)) {
16657c478bd9Sstevel@tonic-gate
16667c478bd9Sstevel@tonic-gate /* don't allow any more changes to the device tree */
16677c478bd9Sstevel@tonic-gate if (devinfo_freeze) {
16687c478bd9Sstevel@tonic-gate rv = DDI_FAILURE;
16697c478bd9Sstevel@tonic-gate break;
16707c478bd9Sstevel@tonic-gate }
16717c478bd9Sstevel@tonic-gate
16727c478bd9Sstevel@tonic-gate switch (i_ddi_node_state(dip)) {
16737c478bd9Sstevel@tonic-gate case DS_PROTO:
16747c478bd9Sstevel@tonic-gate break;
16757c478bd9Sstevel@tonic-gate case DS_LINKED:
16767c478bd9Sstevel@tonic-gate /*
16777c478bd9Sstevel@tonic-gate * Persistent nodes are only removed by hotplug code
16787c478bd9Sstevel@tonic-gate * .conf nodes synchronizes on per-driver list.
16797c478bd9Sstevel@tonic-gate */
16807c478bd9Sstevel@tonic-gate if ((rv = unlink_node(dip)) == DDI_SUCCESS)
16817c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_PROTO);
16827c478bd9Sstevel@tonic-gate break;
16837c478bd9Sstevel@tonic-gate case DS_BOUND:
16847c478bd9Sstevel@tonic-gate /*
16857c478bd9Sstevel@tonic-gate * The following transitions synchronizes on the
16867c478bd9Sstevel@tonic-gate * per-driver busy changing flag, since we already
16877c478bd9Sstevel@tonic-gate * have a driver.
16887c478bd9Sstevel@tonic-gate */
16897c478bd9Sstevel@tonic-gate if ((rv = unbind_node(dip)) == DDI_SUCCESS)
16907c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_LINKED);
16917c478bd9Sstevel@tonic-gate break;
16927c478bd9Sstevel@tonic-gate case DS_INITIALIZED:
16937c478bd9Sstevel@tonic-gate if ((rv = uninit_node(dip)) == DDI_SUCCESS)
16947c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_BOUND);
16957c478bd9Sstevel@tonic-gate break;
16967c478bd9Sstevel@tonic-gate case DS_PROBED:
16977c478bd9Sstevel@tonic-gate if ((rv = unprobe_node(dip)) == DDI_SUCCESS)
16987c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_INITIALIZED);
16997c478bd9Sstevel@tonic-gate break;
17007c478bd9Sstevel@tonic-gate case DS_ATTACHED:
17011a5e258fSJosef 'Jeff' Sipek atomic_inc_ulong(&devinfo_attach_detach);
170216747f41Scth
170316747f41Scth mutex_enter(&(DEVI(dip)->devi_lock));
17047c478bd9Sstevel@tonic-gate DEVI_SET_DETACHING(dip);
170516747f41Scth mutex_exit(&(DEVI(dip)->devi_lock));
170616747f41Scth
17077c478bd9Sstevel@tonic-gate membar_enter(); /* ensure visibility for hold_devi */
17087c478bd9Sstevel@tonic-gate
17097c478bd9Sstevel@tonic-gate if ((rv = detach_node(dip, flag)) == DDI_SUCCESS)
17107c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_PROBED);
171116747f41Scth
171216747f41Scth mutex_enter(&(DEVI(dip)->devi_lock));
17137c478bd9Sstevel@tonic-gate DEVI_CLR_DETACHING(dip);
171416747f41Scth mutex_exit(&(DEVI(dip)->devi_lock));
171516747f41Scth
17161a5e258fSJosef 'Jeff' Sipek atomic_dec_ulong(&devinfo_attach_detach);
17177c478bd9Sstevel@tonic-gate break;
17187c478bd9Sstevel@tonic-gate case DS_READY:
17197c478bd9Sstevel@tonic-gate if ((rv = predetach_node(dip, flag)) == DDI_SUCCESS)
17207c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_ATTACHED);
17217c478bd9Sstevel@tonic-gate break;
17227c478bd9Sstevel@tonic-gate default:
17237c478bd9Sstevel@tonic-gate ASSERT("unknown devinfo state");
17247c478bd9Sstevel@tonic-gate }
17257c478bd9Sstevel@tonic-gate }
17267c478bd9Sstevel@tonic-gate da_log_enter(dip);
17277c478bd9Sstevel@tonic-gate return (rv);
17287c478bd9Sstevel@tonic-gate }
17297c478bd9Sstevel@tonic-gate
17307c478bd9Sstevel@tonic-gate /*
17317c478bd9Sstevel@tonic-gate * ddi_initchild: transform node to DS_INITIALIZED state
17327c478bd9Sstevel@tonic-gate */
17337c478bd9Sstevel@tonic-gate int
ddi_initchild(dev_info_t * parent,dev_info_t * proto)17347c478bd9Sstevel@tonic-gate ddi_initchild(dev_info_t *parent, dev_info_t *proto)
17357c478bd9Sstevel@tonic-gate {
17363fe80ca4SDan Cross int ret;
17377c478bd9Sstevel@tonic-gate
17383fe80ca4SDan Cross ndi_devi_enter(parent);
17397c478bd9Sstevel@tonic-gate ret = i_ndi_config_node(proto, DS_INITIALIZED, 0);
17403fe80ca4SDan Cross ndi_devi_exit(parent);
17417c478bd9Sstevel@tonic-gate
17427c478bd9Sstevel@tonic-gate return (ret);
17437c478bd9Sstevel@tonic-gate }
17447c478bd9Sstevel@tonic-gate
17457c478bd9Sstevel@tonic-gate /*
17467c478bd9Sstevel@tonic-gate * ddi_uninitchild: transform node down to DS_BOUND state
17477c478bd9Sstevel@tonic-gate */
17487c478bd9Sstevel@tonic-gate int
ddi_uninitchild(dev_info_t * dip)17497c478bd9Sstevel@tonic-gate ddi_uninitchild(dev_info_t *dip)
17507c478bd9Sstevel@tonic-gate {
17513fe80ca4SDan Cross int ret;
17527c478bd9Sstevel@tonic-gate dev_info_t *parent = ddi_get_parent(dip);
17537c478bd9Sstevel@tonic-gate ASSERT(parent);
17547c478bd9Sstevel@tonic-gate
17553fe80ca4SDan Cross ndi_devi_enter(parent);
17567c478bd9Sstevel@tonic-gate ret = i_ndi_unconfig_node(dip, DS_BOUND, 0);
17573fe80ca4SDan Cross ndi_devi_exit(parent);
17587c478bd9Sstevel@tonic-gate
17597c478bd9Sstevel@tonic-gate return (ret);
17607c478bd9Sstevel@tonic-gate }
17617c478bd9Sstevel@tonic-gate
17627c478bd9Sstevel@tonic-gate /*
1763737d277aScth * i_ddi_attachchild: transform node to DS_READY/i_ddi_devi_attached() state
17647c478bd9Sstevel@tonic-gate */
17657c478bd9Sstevel@tonic-gate static int
i_ddi_attachchild(dev_info_t * dip)17667c478bd9Sstevel@tonic-gate i_ddi_attachchild(dev_info_t *dip)
17677c478bd9Sstevel@tonic-gate {
17687c478bd9Sstevel@tonic-gate dev_info_t *parent = ddi_get_parent(dip);
17695e3986cbScth int ret;
17705e3986cbScth
17715e3986cbScth ASSERT(parent && DEVI_BUSY_OWNED(parent));
17727c478bd9Sstevel@tonic-gate
17737c478bd9Sstevel@tonic-gate if ((i_ddi_node_state(dip) < DS_BOUND) || DEVI_IS_DEVICE_OFFLINE(dip))
17747c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
17757c478bd9Sstevel@tonic-gate
17767c478bd9Sstevel@tonic-gate ret = i_ndi_config_node(dip, DS_READY, 0);
17777c478bd9Sstevel@tonic-gate if (ret == NDI_SUCCESS) {
17787c478bd9Sstevel@tonic-gate ret = DDI_SUCCESS;
17797c478bd9Sstevel@tonic-gate } else {
17807c478bd9Sstevel@tonic-gate /*
17817c478bd9Sstevel@tonic-gate * Take it down to DS_INITIALIZED so pm_pre_probe is run
17827c478bd9Sstevel@tonic-gate * on the next attach
17837c478bd9Sstevel@tonic-gate */
17847c478bd9Sstevel@tonic-gate (void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
17857c478bd9Sstevel@tonic-gate ret = DDI_FAILURE;
17867c478bd9Sstevel@tonic-gate }
17877c478bd9Sstevel@tonic-gate
17887c478bd9Sstevel@tonic-gate return (ret);
17897c478bd9Sstevel@tonic-gate }
17907c478bd9Sstevel@tonic-gate
17917c478bd9Sstevel@tonic-gate /*
17927c478bd9Sstevel@tonic-gate * i_ddi_detachchild: transform node down to DS_PROBED state
17937c478bd9Sstevel@tonic-gate * If it fails, put it back to DS_READY state.
17947c478bd9Sstevel@tonic-gate * NOTE: A node that fails detach may be at DS_ATTACHED instead
1795737d277aScth * of DS_READY for a small amount of time - this is the source of
1796737d277aScth * transient DS_READY->DS_ATTACHED->DS_READY state changes.
17977c478bd9Sstevel@tonic-gate */
17987c478bd9Sstevel@tonic-gate static int
i_ddi_detachchild(dev_info_t * dip,uint_t flags)17997c478bd9Sstevel@tonic-gate i_ddi_detachchild(dev_info_t *dip, uint_t flags)
18007c478bd9Sstevel@tonic-gate {
18017c478bd9Sstevel@tonic-gate dev_info_t *parent = ddi_get_parent(dip);
18025e3986cbScth int ret;
18037c478bd9Sstevel@tonic-gate
18045e3986cbScth ASSERT(parent && DEVI_BUSY_OWNED(parent));
18055e3986cbScth
18067c478bd9Sstevel@tonic-gate ret = i_ndi_unconfig_node(dip, DS_PROBED, flags);
18077c478bd9Sstevel@tonic-gate if (ret != DDI_SUCCESS)
18087c478bd9Sstevel@tonic-gate (void) i_ndi_config_node(dip, DS_READY, 0);
18097c478bd9Sstevel@tonic-gate else
18107c478bd9Sstevel@tonic-gate /* allow pm_pre_probe to reestablish pm state */
18117c478bd9Sstevel@tonic-gate (void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
18127c478bd9Sstevel@tonic-gate return (ret);
18137c478bd9Sstevel@tonic-gate }
18147c478bd9Sstevel@tonic-gate
18157c478bd9Sstevel@tonic-gate /*
18167c478bd9Sstevel@tonic-gate * Add a child and bind to driver
18177c478bd9Sstevel@tonic-gate */
18187c478bd9Sstevel@tonic-gate dev_info_t *
ddi_add_child(dev_info_t * pdip,char * name,uint_t nodeid,uint_t unit)18197c478bd9Sstevel@tonic-gate ddi_add_child(dev_info_t *pdip, char *name, uint_t nodeid, uint_t unit)
18207c478bd9Sstevel@tonic-gate {
18217c478bd9Sstevel@tonic-gate dev_info_t *dip;
18227c478bd9Sstevel@tonic-gate
18237c478bd9Sstevel@tonic-gate /* allocate a new node */
18247c478bd9Sstevel@tonic-gate dip = i_ddi_alloc_node(pdip, name, nodeid, (int)unit, NULL, KM_SLEEP);
18257c478bd9Sstevel@tonic-gate
18263fe80ca4SDan Cross ndi_devi_enter(pdip);
18277c478bd9Sstevel@tonic-gate (void) i_ndi_config_node(dip, DS_BOUND, 0);
18283fe80ca4SDan Cross ndi_devi_exit(pdip);
18297c478bd9Sstevel@tonic-gate return (dip);
18307c478bd9Sstevel@tonic-gate }
18317c478bd9Sstevel@tonic-gate
18327c478bd9Sstevel@tonic-gate /*
18337c478bd9Sstevel@tonic-gate * ddi_remove_child: remove the dip. The parent must be attached and held
18347c478bd9Sstevel@tonic-gate */
18357c478bd9Sstevel@tonic-gate int
ddi_remove_child(dev_info_t * dip,int dummy)18367c478bd9Sstevel@tonic-gate ddi_remove_child(dev_info_t *dip, int dummy)
18377c478bd9Sstevel@tonic-gate {
18387c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(dummy))
18393fe80ca4SDan Cross int ret;
18407c478bd9Sstevel@tonic-gate dev_info_t *parent = ddi_get_parent(dip);
18417c478bd9Sstevel@tonic-gate ASSERT(parent);
18427c478bd9Sstevel@tonic-gate
18433fe80ca4SDan Cross ndi_devi_enter(parent);
18447c478bd9Sstevel@tonic-gate
18457c478bd9Sstevel@tonic-gate /*
18467c478bd9Sstevel@tonic-gate * If we still have children, for example SID nodes marked
18477c478bd9Sstevel@tonic-gate * as persistent but not attached, attempt to remove them.
18487c478bd9Sstevel@tonic-gate */
18497c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_child) {
18507c478bd9Sstevel@tonic-gate ret = ndi_devi_unconfig(dip, NDI_DEVI_REMOVE);
18517c478bd9Sstevel@tonic-gate if (ret != NDI_SUCCESS) {
18523fe80ca4SDan Cross ndi_devi_exit(parent);
18537c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
18547c478bd9Sstevel@tonic-gate }
18557c478bd9Sstevel@tonic-gate ASSERT(DEVI(dip)->devi_child == NULL);
18567c478bd9Sstevel@tonic-gate }
18577c478bd9Sstevel@tonic-gate
18587c478bd9Sstevel@tonic-gate ret = i_ndi_unconfig_node(dip, DS_PROTO, 0);
18593fe80ca4SDan Cross ndi_devi_exit(parent);
18607c478bd9Sstevel@tonic-gate
18617c478bd9Sstevel@tonic-gate if (ret != DDI_SUCCESS)
18627c478bd9Sstevel@tonic-gate return (ret);
18637c478bd9Sstevel@tonic-gate
18647c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(dip) == DS_PROTO);
18657c478bd9Sstevel@tonic-gate i_ddi_free_node(dip);
18667c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
18677c478bd9Sstevel@tonic-gate }
18687c478bd9Sstevel@tonic-gate
18697c478bd9Sstevel@tonic-gate /*
18707c478bd9Sstevel@tonic-gate * NDI wrappers for ref counting, node allocation, and transitions
18717c478bd9Sstevel@tonic-gate */
18727c478bd9Sstevel@tonic-gate
18737c478bd9Sstevel@tonic-gate /*
18747c478bd9Sstevel@tonic-gate * Hold/release the devinfo node itself.
18757c478bd9Sstevel@tonic-gate * Caller is assumed to prevent the devi from detaching during this call
18767c478bd9Sstevel@tonic-gate */
18777c478bd9Sstevel@tonic-gate void
ndi_hold_devi(dev_info_t * dip)18787c478bd9Sstevel@tonic-gate ndi_hold_devi(dev_info_t *dip)
18797c478bd9Sstevel@tonic-gate {
18807c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(dip)->devi_lock);
18817c478bd9Sstevel@tonic-gate ASSERT(DEVI(dip)->devi_ref >= 0);
18827c478bd9Sstevel@tonic-gate DEVI(dip)->devi_ref++;
18837c478bd9Sstevel@tonic-gate membar_enter(); /* make sure stores are flushed */
18847c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(dip)->devi_lock);
18857c478bd9Sstevel@tonic-gate }
18867c478bd9Sstevel@tonic-gate
18877c478bd9Sstevel@tonic-gate void
ndi_rele_devi(dev_info_t * dip)18887c478bd9Sstevel@tonic-gate ndi_rele_devi(dev_info_t *dip)
18897c478bd9Sstevel@tonic-gate {
18907c478bd9Sstevel@tonic-gate ASSERT(DEVI(dip)->devi_ref > 0);
18917c478bd9Sstevel@tonic-gate
18927c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(dip)->devi_lock);
18937c478bd9Sstevel@tonic-gate DEVI(dip)->devi_ref--;
18947c478bd9Sstevel@tonic-gate membar_enter(); /* make sure stores are flushed */
18957c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(dip)->devi_lock);
18967c478bd9Sstevel@tonic-gate }
18977c478bd9Sstevel@tonic-gate
18987c478bd9Sstevel@tonic-gate int
e_ddi_devi_holdcnt(dev_info_t * dip)18997c478bd9Sstevel@tonic-gate e_ddi_devi_holdcnt(dev_info_t *dip)
19007c478bd9Sstevel@tonic-gate {
19017c478bd9Sstevel@tonic-gate return (DEVI(dip)->devi_ref);
19027c478bd9Sstevel@tonic-gate }
19037c478bd9Sstevel@tonic-gate
19047c478bd9Sstevel@tonic-gate /*
19057c478bd9Sstevel@tonic-gate * Hold/release the driver the devinfo node is bound to.
19067c478bd9Sstevel@tonic-gate */
19077c478bd9Sstevel@tonic-gate struct dev_ops *
ndi_hold_driver(dev_info_t * dip)19087c478bd9Sstevel@tonic-gate ndi_hold_driver(dev_info_t *dip)
19097c478bd9Sstevel@tonic-gate {
19107c478bd9Sstevel@tonic-gate if (i_ddi_node_state(dip) < DS_BOUND)
19117c478bd9Sstevel@tonic-gate return (NULL);
19127c478bd9Sstevel@tonic-gate
19137c478bd9Sstevel@tonic-gate ASSERT(DEVI(dip)->devi_major != -1);
19147c478bd9Sstevel@tonic-gate return (mod_hold_dev_by_major(DEVI(dip)->devi_major));
19157c478bd9Sstevel@tonic-gate }
19167c478bd9Sstevel@tonic-gate
19177c478bd9Sstevel@tonic-gate void
ndi_rele_driver(dev_info_t * dip)19187c478bd9Sstevel@tonic-gate ndi_rele_driver(dev_info_t *dip)
19197c478bd9Sstevel@tonic-gate {
19207c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
19217c478bd9Sstevel@tonic-gate mod_rele_dev_by_major(DEVI(dip)->devi_major);
19227c478bd9Sstevel@tonic-gate }
19237c478bd9Sstevel@tonic-gate
19247c478bd9Sstevel@tonic-gate /*
1925b9ccdc5aScth * Single thread entry into devinfo node for modifying its children (devinfo,
1926b9ccdc5aScth * pathinfo, and minor). To verify in ASSERTS use DEVI_BUSY_OWNED macro.
19277c478bd9Sstevel@tonic-gate */
19287c478bd9Sstevel@tonic-gate void
ndi_devi_enter(dev_info_t * dip)19293fe80ca4SDan Cross ndi_devi_enter(dev_info_t *dip)
19307c478bd9Sstevel@tonic-gate {
1931d27aea3fSDan Cross struct dev_info *devi;
19327c478bd9Sstevel@tonic-gate ASSERT(dip != NULL);
19337c478bd9Sstevel@tonic-gate
1934b31f5cf7SDan Cross /* for vHCI, enforce (vHCI, pHCI) ndi_devi_enter() order */
19355e3986cbScth ASSERT(!MDI_VHCI(dip) || (mdi_devi_pdip_entered(dip) == 0) ||
19365e3986cbScth DEVI_BUSY_OWNED(dip));
19375e3986cbScth
1938d27aea3fSDan Cross /*
1939d27aea3fSDan Cross * If we're panicking, we are single-threaded and cannot
1940d27aea3fSDan Cross * `mutex_enter`, so just return.
1941d27aea3fSDan Cross */
1942d27aea3fSDan Cross if (panicstr != NULL)
1943d27aea3fSDan Cross return;
1944d27aea3fSDan Cross
1945d27aea3fSDan Cross devi = DEVI(dip);
19467c478bd9Sstevel@tonic-gate mutex_enter(&devi->devi_lock);
1947d27aea3fSDan Cross while (DEVI_BUSY_CHANGING(devi)) {
1948d27aea3fSDan Cross /*
1949d27aea3fSDan Cross * If we are called when we are panicking, then we are
1950d27aea3fSDan Cross * single-threaded, and would otherwise loop forever, so
1951d27aea3fSDan Cross * we test for that here and early return if applicable.
1952d27aea3fSDan Cross * Note that we also test for this in `ndi_devi_enter`;
1953d27aea3fSDan Cross * regardless we must test again here in case we start
1954d27aea3fSDan Cross * panicking while contended.
1955d27aea3fSDan Cross */
1956d27aea3fSDan Cross if (panicstr != NULL) {
19577c478bd9Sstevel@tonic-gate mutex_exit(&devi->devi_lock);
19587c478bd9Sstevel@tonic-gate return;
19597c478bd9Sstevel@tonic-gate }
1960d27aea3fSDan Cross if (devi->devi_busy_thread == curthread) {
1961d27aea3fSDan Cross devi->devi_circular++;
1962d27aea3fSDan Cross mutex_exit(&devi->devi_lock);
1963d27aea3fSDan Cross return;
1964d27aea3fSDan Cross }
1965d27aea3fSDan Cross cv_wait(&devi->devi_cv, &devi->devi_lock);
1966d27aea3fSDan Cross }
19677c478bd9Sstevel@tonic-gate devi->devi_flags |= DEVI_BUSY;
19687c478bd9Sstevel@tonic-gate devi->devi_busy_thread = curthread;
19697c478bd9Sstevel@tonic-gate mutex_exit(&devi->devi_lock);
19707c478bd9Sstevel@tonic-gate }
19717c478bd9Sstevel@tonic-gate
19727c478bd9Sstevel@tonic-gate /*
19737c478bd9Sstevel@tonic-gate * Release ndi_devi_enter or successful ndi_devi_tryenter.
1974d27aea3fSDan Cross *
1975d27aea3fSDan Cross * Note that after we leave the critical section, if this is a pHCI exit we must
1976d27aea3fSDan Cross * broadcast to our vHCI, if one exists, as it may be blocked on a condvar in
1977d27aea3fSDan Cross * `ndi_devi_config_one`.
1978d27aea3fSDan Cross *
1979d27aea3fSDan Cross * It may seem odd that we do this after exiting the critical section, since we
1980d27aea3fSDan Cross * are no longer protected by the conditions surrounding it, but note that
1981d27aea3fSDan Cross * `ndi_devi_enter`/`ndi_devi_exit` and similar do not protect the `dip` itself.
1982d27aea3fSDan Cross * Rather, the `dip` is protected by a reference count that is maintained by
1983d27aea3fSDan Cross * calls to `ndi_hold_devi` and `ndi_rele_devi`. If we're in this code path,
1984d27aea3fSDan Cross * there must necessarily be such a reference, so it is safe to access our `dip`
1985d27aea3fSDan Cross * any time here.
1986d27aea3fSDan Cross *
1987d27aea3fSDan Cross * Further, any pHCI or vHCI associated with this dip is effectively write-once
1988d27aea3fSDan Cross * at setup, and the pHCI maintains a reference count on the vHCI (indeed, the
1989d27aea3fSDan Cross * pHCI is what actually points to the vHCI), ensuring it lives at least as long
1990d27aea3fSDan Cross * as the pHCI.
1991d27aea3fSDan Cross *
1992d27aea3fSDan Cross * Finally, it is safe to access the pHCI outside of the critical section for
1993d27aea3fSDan Cross * the same reason we can access the dip: it is completely owned by the dip and
1994d27aea3fSDan Cross * only deallocated in the detach path, and we only get there when all
1995d27aea3fSDan Cross * references to the dip have been released. Therefore, if we are in this code
1996d27aea3fSDan Cross * path, the pHCI and thus the vHCI, if they exist, are both necessarily valid.
19977c478bd9Sstevel@tonic-gate */
19987c478bd9Sstevel@tonic-gate void
ndi_devi_exit(dev_info_t * dip)19993fe80ca4SDan Cross ndi_devi_exit(dev_info_t *dip)
20007c478bd9Sstevel@tonic-gate {
2001d27aea3fSDan Cross struct dev_info *devi, *vdevi;
2002d27aea3fSDan Cross boolean_t phci;
2003d27aea3fSDan Cross
20047c478bd9Sstevel@tonic-gate ASSERT(dip != NULL);
20057c478bd9Sstevel@tonic-gate
2006d27aea3fSDan Cross /*
2007d27aea3fSDan Cross * If we're panicking, we are single threaded, so just return.
2008d27aea3fSDan Cross */
2009d27aea3fSDan Cross if (panicstr != NULL)
20107c478bd9Sstevel@tonic-gate return;
20117c478bd9Sstevel@tonic-gate
2012d27aea3fSDan Cross devi = DEVI(dip);
2013d27aea3fSDan Cross mutex_enter(&devi->devi_lock);
2014d27aea3fSDan Cross ASSERT(DEVI_BUSY_OWNED(devi));
2015d27aea3fSDan Cross if (devi->devi_circular > 0) {
20167c478bd9Sstevel@tonic-gate devi->devi_circular--;
2017d27aea3fSDan Cross mutex_exit(&devi->devi_lock);
2018d27aea3fSDan Cross return;
2019d27aea3fSDan Cross }
20207c478bd9Sstevel@tonic-gate devi->devi_flags &= ~DEVI_BUSY;
20217c478bd9Sstevel@tonic-gate devi->devi_busy_thread = NULL;
2022d27aea3fSDan Cross cv_broadcast(&devi->devi_cv);
2023d27aea3fSDan Cross mutex_exit(&devi->devi_lock);
20245e3986cbScth
20253fe80ca4SDan Cross /*
20263fe80ca4SDan Cross * Note that `DEVI(mdi_devi_get_vdip(dip))` will be NULL if `dip` is
20273fe80ca4SDan Cross * not a pHCI or the vHCI doest not exist.
20283fe80ca4SDan Cross */
20295e3986cbScth vdevi = DEVI(mdi_devi_get_vdip(dip));
2030d27aea3fSDan Cross if (vdevi != NULL) {
2031d27aea3fSDan Cross mutex_enter(&vdevi->devi_lock);
2032d27aea3fSDan Cross if ((vdevi->devi_flags & DEVI_PHCI_SIGNALS_VHCI) != 0) {
20335e3986cbScth vdevi->devi_flags &= ~DEVI_PHCI_SIGNALS_VHCI;
2034d27aea3fSDan Cross cv_broadcast(&vdevi->devi_cv);
20355e3986cbScth }
2036d27aea3fSDan Cross mutex_exit(&vdevi->devi_lock);
20375e3986cbScth }
20385e3986cbScth }
20395e3986cbScth
20405e3986cbScth /*
20415e3986cbScth * Release ndi_devi_enter and wait for possibility of new children, avoiding
20425e3986cbScth * possibility of missing broadcast before getting to cv_timedwait().
20435e3986cbScth */
20445e3986cbScth static void
ndi_devi_exit_and_wait(dev_info_t * dip,clock_t end_time)2045d27aea3fSDan Cross ndi_devi_exit_and_wait(dev_info_t *dip, clock_t end_time)
20465e3986cbScth {
2047d27aea3fSDan Cross struct dev_info *devi;
2048d27aea3fSDan Cross
20495e3986cbScth ASSERT(dip != NULL);
20505e3986cbScth
2051d27aea3fSDan Cross /*
2052d27aea3fSDan Cross * If we're panicking, we are single threaded, and cannot
2053d27aea3fSDan Cross * call mutex_enter(), so just return.
2054d27aea3fSDan Cross */
20555e3986cbScth if (panicstr)
20565e3986cbScth return;
20575e3986cbScth
2058d27aea3fSDan Cross /* like ndi_devi_exit with circular of zero */
2059d27aea3fSDan Cross devi = DEVI(dip);
2060d27aea3fSDan Cross mutex_enter(&devi->devi_lock);
20615e3986cbScth /*
2062d27aea3fSDan Cross * We are called to wait for a new child, and new child can
20635e3986cbScth * only be added if circular is zero.
20645e3986cbScth */
2065d27aea3fSDan Cross ASSERT(devi->devi_circular == 0);
2066d27aea3fSDan Cross ASSERT(DEVI_BUSY_OWNED(devi));
20675e3986cbScth devi->devi_flags &= ~DEVI_BUSY;
20685e3986cbScth devi->devi_busy_thread = NULL;
2069d27aea3fSDan Cross cv_broadcast(&devi->devi_cv);
20705e3986cbScth
20715e3986cbScth /* now wait for new children while still holding devi_lock */
2072d27aea3fSDan Cross (void) cv_timedwait(&devi->devi_cv, &devi->devi_lock, end_time);
2073d27aea3fSDan Cross mutex_exit(&devi->devi_lock);
20747c478bd9Sstevel@tonic-gate }
20757c478bd9Sstevel@tonic-gate
20767c478bd9Sstevel@tonic-gate /*
20777c478bd9Sstevel@tonic-gate * Attempt to single thread entry into devinfo node for modifying its children.
20787c478bd9Sstevel@tonic-gate */
20797c478bd9Sstevel@tonic-gate int
ndi_devi_tryenter(dev_info_t * dip)20803fe80ca4SDan Cross ndi_devi_tryenter(dev_info_t *dip)
20817c478bd9Sstevel@tonic-gate {
2082d27aea3fSDan Cross int entered;
2083d27aea3fSDan Cross struct dev_info *devi;
2084d27aea3fSDan Cross
20857c478bd9Sstevel@tonic-gate ASSERT(dip != NULL);
20867c478bd9Sstevel@tonic-gate
2087d27aea3fSDan Cross /*
20883fe80ca4SDan Cross * If we're panicing, we are single threaded, and cannot
2089d27aea3fSDan Cross * call mutex_enter(), so just return.
2090d27aea3fSDan Cross */
2091d27aea3fSDan Cross if (panicstr != NULL)
2092d27aea3fSDan Cross return (0);
2093d27aea3fSDan Cross
2094d27aea3fSDan Cross devi = DEVI(dip);
20957c478bd9Sstevel@tonic-gate mutex_enter(&devi->devi_lock);
2096d27aea3fSDan Cross entered = 1;
2097d27aea3fSDan Cross if (!DEVI_BUSY_CHANGING(devi)) {
2098d27aea3fSDan Cross /* The uncontended case. */
2099d27aea3fSDan Cross devi->devi_flags |= DEVI_BUSY;
2100d27aea3fSDan Cross devi->devi_busy_thread = curthread;
2101d27aea3fSDan Cross } else if (devi->devi_busy_thread == curthread) {
2102d27aea3fSDan Cross /* Nested entry on the same thread. */
21037c478bd9Sstevel@tonic-gate devi->devi_circular++;
21047c478bd9Sstevel@tonic-gate } else {
2105d27aea3fSDan Cross /* We fail on the contended case. */
2106d27aea3fSDan Cross entered = 0;
21077c478bd9Sstevel@tonic-gate }
21087c478bd9Sstevel@tonic-gate mutex_exit(&devi->devi_lock);
2109d27aea3fSDan Cross
2110d27aea3fSDan Cross return (entered);
21117c478bd9Sstevel@tonic-gate }
21127c478bd9Sstevel@tonic-gate
21137c478bd9Sstevel@tonic-gate /*
21147c478bd9Sstevel@tonic-gate * Allocate and initialize a new dev_info structure.
21157c478bd9Sstevel@tonic-gate *
21167c478bd9Sstevel@tonic-gate * This routine may be called at interrupt time by a nexus in
21177c478bd9Sstevel@tonic-gate * response to a hotplug event, therefore memory allocations are
21187c478bd9Sstevel@tonic-gate * not allowed to sleep.
21197c478bd9Sstevel@tonic-gate */
21207c478bd9Sstevel@tonic-gate int
ndi_devi_alloc(dev_info_t * parent,const char * node_name,pnode_t nodeid,dev_info_t ** ret_dip)21216205b5c4SRobert Mustacchi ndi_devi_alloc(dev_info_t *parent, const char *node_name, pnode_t nodeid,
21227c478bd9Sstevel@tonic-gate dev_info_t **ret_dip)
21237c478bd9Sstevel@tonic-gate {
21247c478bd9Sstevel@tonic-gate ASSERT(node_name != NULL);
21257c478bd9Sstevel@tonic-gate ASSERT(ret_dip != NULL);
21267c478bd9Sstevel@tonic-gate
21277c478bd9Sstevel@tonic-gate *ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
21287c478bd9Sstevel@tonic-gate KM_NOSLEEP);
21297c478bd9Sstevel@tonic-gate if (*ret_dip == NULL) {
21307c478bd9Sstevel@tonic-gate return (NDI_NOMEM);
21317c478bd9Sstevel@tonic-gate }
21327c478bd9Sstevel@tonic-gate
21337c478bd9Sstevel@tonic-gate return (NDI_SUCCESS);
21347c478bd9Sstevel@tonic-gate }
21357c478bd9Sstevel@tonic-gate
21367c478bd9Sstevel@tonic-gate /*
21377c478bd9Sstevel@tonic-gate * Allocate and initialize a new dev_info structure
21387c478bd9Sstevel@tonic-gate * This routine may sleep and should not be called at interrupt time
21397c478bd9Sstevel@tonic-gate */
21407c478bd9Sstevel@tonic-gate void
ndi_devi_alloc_sleep(dev_info_t * parent,const char * node_name,pnode_t nodeid,dev_info_t ** ret_dip)21416205b5c4SRobert Mustacchi ndi_devi_alloc_sleep(dev_info_t *parent, const char *node_name, pnode_t nodeid,
21427c478bd9Sstevel@tonic-gate dev_info_t **ret_dip)
21437c478bd9Sstevel@tonic-gate {
21447c478bd9Sstevel@tonic-gate ASSERT(node_name != NULL);
21457c478bd9Sstevel@tonic-gate ASSERT(ret_dip != NULL);
21467c478bd9Sstevel@tonic-gate
21477c478bd9Sstevel@tonic-gate *ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
21487c478bd9Sstevel@tonic-gate KM_SLEEP);
21497c478bd9Sstevel@tonic-gate ASSERT(*ret_dip);
21507c478bd9Sstevel@tonic-gate }
21517c478bd9Sstevel@tonic-gate
21527c478bd9Sstevel@tonic-gate /*
21537c478bd9Sstevel@tonic-gate * Remove an initialized (but not yet attached) dev_info
21547c478bd9Sstevel@tonic-gate * node from it's parent.
21557c478bd9Sstevel@tonic-gate */
21567c478bd9Sstevel@tonic-gate int
ndi_devi_free(dev_info_t * dip)21577c478bd9Sstevel@tonic-gate ndi_devi_free(dev_info_t *dip)
21587c478bd9Sstevel@tonic-gate {
21597c478bd9Sstevel@tonic-gate ASSERT(dip != NULL);
21607c478bd9Sstevel@tonic-gate
21617c478bd9Sstevel@tonic-gate if (i_ddi_node_state(dip) >= DS_INITIALIZED)
21627c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
21637c478bd9Sstevel@tonic-gate
21647c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_free: %s%d (%p)\n",
21657c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
21667c478bd9Sstevel@tonic-gate
21677c478bd9Sstevel@tonic-gate (void) ddi_remove_child(dip, 0);
21687c478bd9Sstevel@tonic-gate
21697c478bd9Sstevel@tonic-gate return (NDI_SUCCESS);
21707c478bd9Sstevel@tonic-gate }
21717c478bd9Sstevel@tonic-gate
21727c478bd9Sstevel@tonic-gate /*
21737c478bd9Sstevel@tonic-gate * ndi_devi_bind_driver() binds a driver to a given device. If it fails
21747c478bd9Sstevel@tonic-gate * to bind the driver, it returns an appropriate error back. Some drivers
21757c478bd9Sstevel@tonic-gate * may want to know if the actually failed to bind.
21767c478bd9Sstevel@tonic-gate */
21777c478bd9Sstevel@tonic-gate int
ndi_devi_bind_driver(dev_info_t * dip,uint_t flags)21787c478bd9Sstevel@tonic-gate ndi_devi_bind_driver(dev_info_t *dip, uint_t flags)
21797c478bd9Sstevel@tonic-gate {
21807c478bd9Sstevel@tonic-gate int ret = NDI_FAILURE;
21817c478bd9Sstevel@tonic-gate dev_info_t *pdip = ddi_get_parent(dip);
21827c478bd9Sstevel@tonic-gate ASSERT(pdip);
21837c478bd9Sstevel@tonic-gate
21847c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT,
21857c478bd9Sstevel@tonic-gate "ndi_devi_bind_driver: %s%d (%p) flags: %x\n",
21867c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
21877c478bd9Sstevel@tonic-gate
21883fe80ca4SDan Cross ndi_devi_enter(pdip);
21897c478bd9Sstevel@tonic-gate if (i_ndi_config_node(dip, DS_BOUND, flags) == DDI_SUCCESS)
21907c478bd9Sstevel@tonic-gate ret = NDI_SUCCESS;
21913fe80ca4SDan Cross ndi_devi_exit(pdip);
21927c478bd9Sstevel@tonic-gate
21937c478bd9Sstevel@tonic-gate return (ret);
21947c478bd9Sstevel@tonic-gate }
21957c478bd9Sstevel@tonic-gate
21967c478bd9Sstevel@tonic-gate /*
21977c478bd9Sstevel@tonic-gate * ndi_devi_unbind_driver: unbind the dip
21987c478bd9Sstevel@tonic-gate */
21997c478bd9Sstevel@tonic-gate static int
ndi_devi_unbind_driver(dev_info_t * dip)22007c478bd9Sstevel@tonic-gate ndi_devi_unbind_driver(dev_info_t *dip)
22017c478bd9Sstevel@tonic-gate {
22027c478bd9Sstevel@tonic-gate ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
22037c478bd9Sstevel@tonic-gate
22047c478bd9Sstevel@tonic-gate return (i_ndi_unconfig_node(dip, DS_LINKED, 0));
22057c478bd9Sstevel@tonic-gate }
22067c478bd9Sstevel@tonic-gate
22077c478bd9Sstevel@tonic-gate /*
22087c478bd9Sstevel@tonic-gate * Misc. help routines called by framework only
22097c478bd9Sstevel@tonic-gate */
22107c478bd9Sstevel@tonic-gate
22117c478bd9Sstevel@tonic-gate /*
22127c478bd9Sstevel@tonic-gate * Get the state of node
22137c478bd9Sstevel@tonic-gate */
22147c478bd9Sstevel@tonic-gate ddi_node_state_t
i_ddi_node_state(dev_info_t * dip)22157c478bd9Sstevel@tonic-gate i_ddi_node_state(dev_info_t *dip)
22167c478bd9Sstevel@tonic-gate {
22177c478bd9Sstevel@tonic-gate return (DEVI(dip)->devi_node_state);
22187c478bd9Sstevel@tonic-gate }
22197c478bd9Sstevel@tonic-gate
22207c478bd9Sstevel@tonic-gate /*
22217c478bd9Sstevel@tonic-gate * Set the state of node
22227c478bd9Sstevel@tonic-gate */
22237c478bd9Sstevel@tonic-gate void
i_ddi_set_node_state(dev_info_t * dip,ddi_node_state_t state)22247c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dev_info_t *dip, ddi_node_state_t state)
22257c478bd9Sstevel@tonic-gate {
22267c478bd9Sstevel@tonic-gate DEVI(dip)->devi_node_state = state;
22277c478bd9Sstevel@tonic-gate membar_enter(); /* make sure stores are flushed */
22287c478bd9Sstevel@tonic-gate }
22297c478bd9Sstevel@tonic-gate
22307c478bd9Sstevel@tonic-gate /*
2231737d277aScth * Determine if node is attached. The implementation accommodates transient
2232737d277aScth * DS_READY->DS_ATTACHED->DS_READY state changes. Outside this file, this
2233737d277aScth * function should be instead of i_ddi_node_state() DS_ATTACHED/DS_READY
2234737d277aScth * state checks.
2235737d277aScth */
2236737d277aScth int
i_ddi_devi_attached(dev_info_t * dip)2237737d277aScth i_ddi_devi_attached(dev_info_t *dip)
2238737d277aScth {
2239737d277aScth return (DEVI(dip)->devi_node_state >= DS_ATTACHED);
2240737d277aScth }
2241737d277aScth
2242737d277aScth /*
22437c478bd9Sstevel@tonic-gate * Common function for finding a node in a sibling list given name and addr.
22447c478bd9Sstevel@tonic-gate *
22457c478bd9Sstevel@tonic-gate * By default, name is matched with devi_node_name. The following
22467c478bd9Sstevel@tonic-gate * alternative match strategies are supported:
22477c478bd9Sstevel@tonic-gate *
2248f4da9be0Scth * FIND_NODE_BY_NODENAME: Match on node name - typical use.
2249027021c7SChris Horne *
2250f4da9be0Scth * FIND_NODE_BY_DRIVER: A match on driver name bound to node is conducted.
22517c478bd9Sstevel@tonic-gate * This support is used for support of OBP generic names and
22527c478bd9Sstevel@tonic-gate * for the conversion from driver names to generic names. When
22537c478bd9Sstevel@tonic-gate * more consistency in the generic name environment is achieved
22547c478bd9Sstevel@tonic-gate * (and not needed for upgrade) this support can be removed.
2255027021c7SChris Horne *
2256f4da9be0Scth * FIND_NODE_BY_ADDR: Match on just the addr.
2257f4da9be0Scth * This support is only used/needed during boot to match
2258f4da9be0Scth * a node bound via a path-based driver alias.
22597c478bd9Sstevel@tonic-gate *
22607c478bd9Sstevel@tonic-gate * If a child is not named (dev_addr == NULL), there are three
22617c478bd9Sstevel@tonic-gate * possible actions:
22627c478bd9Sstevel@tonic-gate *
22637c478bd9Sstevel@tonic-gate * (1) skip it
22647c478bd9Sstevel@tonic-gate * (2) FIND_ADDR_BY_INIT: bring child to DS_INITIALIZED state
22657c478bd9Sstevel@tonic-gate * (3) FIND_ADDR_BY_CALLBACK: use a caller-supplied callback function
22667c478bd9Sstevel@tonic-gate */
2267f4da9be0Scth #define FIND_NODE_BY_NODENAME 0x01
2268f4da9be0Scth #define FIND_NODE_BY_DRIVER 0x02
2269f4da9be0Scth #define FIND_NODE_BY_ADDR 0x04
22707c478bd9Sstevel@tonic-gate #define FIND_ADDR_BY_INIT 0x10
22717c478bd9Sstevel@tonic-gate #define FIND_ADDR_BY_CALLBACK 0x20
22727c478bd9Sstevel@tonic-gate
22737c478bd9Sstevel@tonic-gate static dev_info_t *
find_sibling(dev_info_t * head,char * cname,char * caddr,uint_t flag,int (* callback)(dev_info_t *,char *,int))22747c478bd9Sstevel@tonic-gate find_sibling(dev_info_t *head, char *cname, char *caddr, uint_t flag,
22757c478bd9Sstevel@tonic-gate int (*callback)(dev_info_t *, char *, int))
22767c478bd9Sstevel@tonic-gate {
22777c478bd9Sstevel@tonic-gate dev_info_t *dip;
22787c478bd9Sstevel@tonic-gate char *addr, *buf;
22797c478bd9Sstevel@tonic-gate major_t major;
2280f4da9be0Scth uint_t by;
2281f4da9be0Scth
2282f4da9be0Scth /* only one way to find a node */
2283f4da9be0Scth by = flag &
2284f4da9be0Scth (FIND_NODE_BY_DRIVER | FIND_NODE_BY_NODENAME | FIND_NODE_BY_ADDR);
2285f4da9be0Scth ASSERT(by && BIT_ONLYONESET(by));
22867c478bd9Sstevel@tonic-gate
22877c478bd9Sstevel@tonic-gate /* only one way to name a node */
22887c478bd9Sstevel@tonic-gate ASSERT(((flag & FIND_ADDR_BY_INIT) == 0) ||
22897c478bd9Sstevel@tonic-gate ((flag & FIND_ADDR_BY_CALLBACK) == 0));
22907c478bd9Sstevel@tonic-gate
2291f4da9be0Scth if (by == FIND_NODE_BY_DRIVER) {
22927c478bd9Sstevel@tonic-gate major = ddi_name_to_major(cname);
2293a204de77Scth if (major == DDI_MAJOR_T_NONE)
22947c478bd9Sstevel@tonic-gate return (NULL);
22957c478bd9Sstevel@tonic-gate }
22967c478bd9Sstevel@tonic-gate
2297694256ddSToomas Soome if (head == NULL)
2298694256ddSToomas Soome return (NULL);
2299694256ddSToomas Soome
2300c6f039c7SToomas Soome buf = NULL;
23017c478bd9Sstevel@tonic-gate /* preallocate buffer of naming node by callback */
23027c478bd9Sstevel@tonic-gate if (flag & FIND_ADDR_BY_CALLBACK)
23037c478bd9Sstevel@tonic-gate buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
23047c478bd9Sstevel@tonic-gate
23057c478bd9Sstevel@tonic-gate /*
23067c478bd9Sstevel@tonic-gate * Walk the child list to find a match
23077c478bd9Sstevel@tonic-gate */
23084c06356bSdh142964 ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(head)));
23097c478bd9Sstevel@tonic-gate for (dip = head; dip; dip = ddi_get_next_sibling(dip)) {
2310f4da9be0Scth if (by == FIND_NODE_BY_NODENAME) {
23117c478bd9Sstevel@tonic-gate /* match node name */
23127c478bd9Sstevel@tonic-gate if (strcmp(cname, DEVI(dip)->devi_node_name) != 0)
23137c478bd9Sstevel@tonic-gate continue;
2314f4da9be0Scth } else if (by == FIND_NODE_BY_DRIVER) {
2315f4da9be0Scth /* match driver major */
2316f4da9be0Scth if (DEVI(dip)->devi_major != major)
2317f4da9be0Scth continue;
23187c478bd9Sstevel@tonic-gate }
23197c478bd9Sstevel@tonic-gate
23207c478bd9Sstevel@tonic-gate if ((addr = DEVI(dip)->devi_addr) == NULL) {
23217c478bd9Sstevel@tonic-gate /* name the child based on the flag */
23227c478bd9Sstevel@tonic-gate if (flag & FIND_ADDR_BY_INIT) {
23237c478bd9Sstevel@tonic-gate if (ddi_initchild(ddi_get_parent(dip), dip)
23247c478bd9Sstevel@tonic-gate != DDI_SUCCESS)
23257c478bd9Sstevel@tonic-gate continue;
23267c478bd9Sstevel@tonic-gate addr = DEVI(dip)->devi_addr;
23277c478bd9Sstevel@tonic-gate } else if (flag & FIND_ADDR_BY_CALLBACK) {
23287c478bd9Sstevel@tonic-gate if ((callback == NULL) || (callback(
23297c478bd9Sstevel@tonic-gate dip, buf, MAXNAMELEN) != DDI_SUCCESS))
23307c478bd9Sstevel@tonic-gate continue;
23317c478bd9Sstevel@tonic-gate addr = buf;
23327c478bd9Sstevel@tonic-gate } else {
23337c478bd9Sstevel@tonic-gate continue; /* skip */
23347c478bd9Sstevel@tonic-gate }
23357c478bd9Sstevel@tonic-gate }
23367c478bd9Sstevel@tonic-gate
23377c478bd9Sstevel@tonic-gate /* match addr */
23387c478bd9Sstevel@tonic-gate ASSERT(addr != NULL);
23397c478bd9Sstevel@tonic-gate if (strcmp(caddr, addr) == 0)
23407c478bd9Sstevel@tonic-gate break; /* node found */
23417c478bd9Sstevel@tonic-gate
23427c478bd9Sstevel@tonic-gate }
23437c478bd9Sstevel@tonic-gate if (flag & FIND_ADDR_BY_CALLBACK)
23447c478bd9Sstevel@tonic-gate kmem_free(buf, MAXNAMELEN);
23457c478bd9Sstevel@tonic-gate return (dip);
23467c478bd9Sstevel@tonic-gate }
23477c478bd9Sstevel@tonic-gate
23487c478bd9Sstevel@tonic-gate /*
23497c478bd9Sstevel@tonic-gate * Find child of pdip with name: cname@caddr
23507c478bd9Sstevel@tonic-gate * Called by init_node() to look for duplicate nodes
23517c478bd9Sstevel@tonic-gate */
23527c478bd9Sstevel@tonic-gate static dev_info_t *
find_duplicate_child(dev_info_t * pdip,dev_info_t * dip)23537c478bd9Sstevel@tonic-gate find_duplicate_child(dev_info_t *pdip, dev_info_t *dip)
23547c478bd9Sstevel@tonic-gate {
23557c478bd9Sstevel@tonic-gate dev_info_t *dup;
23567c478bd9Sstevel@tonic-gate char *cname = DEVI(dip)->devi_node_name;
23577c478bd9Sstevel@tonic-gate char *caddr = DEVI(dip)->devi_addr;
23587c478bd9Sstevel@tonic-gate
23597c478bd9Sstevel@tonic-gate /* search nodes before dip */
2360f4da9be0Scth dup = find_sibling(ddi_get_child(pdip), cname, caddr,
2361f4da9be0Scth FIND_NODE_BY_NODENAME, NULL);
23627c478bd9Sstevel@tonic-gate if (dup != dip)
23637c478bd9Sstevel@tonic-gate return (dup);
23647c478bd9Sstevel@tonic-gate
23657c478bd9Sstevel@tonic-gate /*
23667c478bd9Sstevel@tonic-gate * search nodes after dip; normally this is not needed,
23677c478bd9Sstevel@tonic-gate */
23687c478bd9Sstevel@tonic-gate return (find_sibling(ddi_get_next_sibling(dip), cname, caddr,
2369f4da9be0Scth FIND_NODE_BY_NODENAME, NULL));
23707c478bd9Sstevel@tonic-gate }
23717c478bd9Sstevel@tonic-gate
23727c478bd9Sstevel@tonic-gate /*
23737c478bd9Sstevel@tonic-gate * Find a child of a given name and address, using a callback to name
23747c478bd9Sstevel@tonic-gate * unnamed children. cname is the binding name.
23757c478bd9Sstevel@tonic-gate */
23764c06356bSdh142964 dev_info_t *
ndi_devi_findchild_by_callback(dev_info_t * pdip,char * dname,char * ua,int (* make_ua)(dev_info_t *,char *,int))23774c06356bSdh142964 ndi_devi_findchild_by_callback(dev_info_t *pdip, char *dname, char *ua,
23784c06356bSdh142964 int (*make_ua)(dev_info_t *, char *, int))
23797c478bd9Sstevel@tonic-gate {
23804c06356bSdh142964 int by = FIND_ADDR_BY_CALLBACK;
23814c06356bSdh142964
23824c06356bSdh142964 ASSERT(DEVI_BUSY_OWNED(pdip));
23834c06356bSdh142964 by |= dname ? FIND_NODE_BY_DRIVER : FIND_NODE_BY_ADDR;
23844c06356bSdh142964 return (find_sibling(ddi_get_child(pdip), dname, ua, by, make_ua));
23857c478bd9Sstevel@tonic-gate }
23867c478bd9Sstevel@tonic-gate
23877c478bd9Sstevel@tonic-gate /*
23887c478bd9Sstevel@tonic-gate * Find a child of a given name and address, invoking initchild to name
23897c478bd9Sstevel@tonic-gate * unnamed children. cname is the node name.
23907c478bd9Sstevel@tonic-gate */
23917c478bd9Sstevel@tonic-gate static dev_info_t *
find_child_by_name(dev_info_t * pdip,char * cname,char * caddr)23927c478bd9Sstevel@tonic-gate find_child_by_name(dev_info_t *pdip, char *cname, char *caddr)
23937c478bd9Sstevel@tonic-gate {
23947c478bd9Sstevel@tonic-gate dev_info_t *dip;
23957c478bd9Sstevel@tonic-gate
2396f4da9be0Scth /* attempt search without changing state of preceding siblings */
2397f4da9be0Scth dip = find_sibling(ddi_get_child(pdip), cname, caddr,
2398f4da9be0Scth FIND_NODE_BY_NODENAME, NULL);
23997c478bd9Sstevel@tonic-gate if (dip)
24007c478bd9Sstevel@tonic-gate return (dip);
24017c478bd9Sstevel@tonic-gate
24027c478bd9Sstevel@tonic-gate return (find_sibling(ddi_get_child(pdip), cname, caddr,
2403f4da9be0Scth FIND_NODE_BY_NODENAME|FIND_ADDR_BY_INIT, NULL));
24047c478bd9Sstevel@tonic-gate }
24057c478bd9Sstevel@tonic-gate
24067c478bd9Sstevel@tonic-gate /*
24077c478bd9Sstevel@tonic-gate * Find a child of a given name and address, invoking initchild to name
24087c478bd9Sstevel@tonic-gate * unnamed children. cname is the node name.
24097c478bd9Sstevel@tonic-gate */
24107c478bd9Sstevel@tonic-gate static dev_info_t *
find_child_by_driver(dev_info_t * pdip,char * cname,char * caddr)24117c478bd9Sstevel@tonic-gate find_child_by_driver(dev_info_t *pdip, char *cname, char *caddr)
24127c478bd9Sstevel@tonic-gate {
24137c478bd9Sstevel@tonic-gate dev_info_t *dip;
24147c478bd9Sstevel@tonic-gate
2415f4da9be0Scth /* attempt search without changing state of preceding siblings */
24167c478bd9Sstevel@tonic-gate dip = find_sibling(ddi_get_child(pdip), cname, caddr,
2417f4da9be0Scth FIND_NODE_BY_DRIVER, NULL);
24187c478bd9Sstevel@tonic-gate if (dip)
24197c478bd9Sstevel@tonic-gate return (dip);
24207c478bd9Sstevel@tonic-gate
24217c478bd9Sstevel@tonic-gate return (find_sibling(ddi_get_child(pdip), cname, caddr,
2422f4da9be0Scth FIND_NODE_BY_DRIVER|FIND_ADDR_BY_INIT, NULL));
2423f4da9be0Scth }
2424f4da9be0Scth
2425f4da9be0Scth /*
2426f4da9be0Scth * Find a child of a given address, invoking initchild to name
2427f4da9be0Scth * unnamed children. cname is the node name.
2428f4da9be0Scth *
2429f4da9be0Scth * NOTE: This function is only used during boot. One would hope that
2430f4da9be0Scth * unique sibling unit-addresses on hardware branches of the tree would
2431f4da9be0Scth * be a requirement to avoid two drivers trying to control the same
2432f4da9be0Scth * piece of hardware. Unfortunately there are some cases where this
2433f4da9be0Scth * situation exists (/ssm@0,0/pci@1c,700000 /ssm@0,0/sghsc@1c,700000).
2434f4da9be0Scth * Until unit-address uniqueness of siblings is guaranteed, use of this
2435f4da9be0Scth * interface for purposes other than boot should be avoided.
2436f4da9be0Scth */
2437f4da9be0Scth static dev_info_t *
find_child_by_addr(dev_info_t * pdip,char * caddr)2438f4da9be0Scth find_child_by_addr(dev_info_t *pdip, char *caddr)
2439f4da9be0Scth {
2440f4da9be0Scth dev_info_t *dip;
2441f4da9be0Scth
24422486dd1dScth /* return NULL if called without a unit-address */
24432486dd1dScth if ((caddr == NULL) || (*caddr == '\0'))
24442486dd1dScth return (NULL);
24452486dd1dScth
2446f4da9be0Scth /* attempt search without changing state of preceding siblings */
2447f4da9be0Scth dip = find_sibling(ddi_get_child(pdip), NULL, caddr,
2448f4da9be0Scth FIND_NODE_BY_ADDR, NULL);
2449f4da9be0Scth if (dip)
2450f4da9be0Scth return (dip);
2451f4da9be0Scth
2452f4da9be0Scth return (find_sibling(ddi_get_child(pdip), NULL, caddr,
2453f4da9be0Scth FIND_NODE_BY_ADDR|FIND_ADDR_BY_INIT, NULL));
24547c478bd9Sstevel@tonic-gate }
24557c478bd9Sstevel@tonic-gate
24567c478bd9Sstevel@tonic-gate /*
24577c478bd9Sstevel@tonic-gate * Deleting a property list. Take care, since some property structures
24587c478bd9Sstevel@tonic-gate * may not be fully built.
24597c478bd9Sstevel@tonic-gate */
24607c478bd9Sstevel@tonic-gate void
i_ddi_prop_list_delete(ddi_prop_t * prop)24617c478bd9Sstevel@tonic-gate i_ddi_prop_list_delete(ddi_prop_t *prop)
24627c478bd9Sstevel@tonic-gate {
24637c478bd9Sstevel@tonic-gate while (prop) {
24647c478bd9Sstevel@tonic-gate ddi_prop_t *next = prop->prop_next;
24657c478bd9Sstevel@tonic-gate if (prop->prop_name)
24667c478bd9Sstevel@tonic-gate kmem_free(prop->prop_name, strlen(prop->prop_name) + 1);
24677c478bd9Sstevel@tonic-gate if ((prop->prop_len != 0) && prop->prop_val)
24687c478bd9Sstevel@tonic-gate kmem_free(prop->prop_val, prop->prop_len);
24697c478bd9Sstevel@tonic-gate kmem_free(prop, sizeof (struct ddi_prop));
24707c478bd9Sstevel@tonic-gate prop = next;
24717c478bd9Sstevel@tonic-gate }
24727c478bd9Sstevel@tonic-gate }
24737c478bd9Sstevel@tonic-gate
24747c478bd9Sstevel@tonic-gate /*
24757c478bd9Sstevel@tonic-gate * Duplicate property list
24767c478bd9Sstevel@tonic-gate */
24777c478bd9Sstevel@tonic-gate ddi_prop_t *
i_ddi_prop_list_dup(ddi_prop_t * prop,uint_t flag)24787c478bd9Sstevel@tonic-gate i_ddi_prop_list_dup(ddi_prop_t *prop, uint_t flag)
24797c478bd9Sstevel@tonic-gate {
24807c478bd9Sstevel@tonic-gate ddi_prop_t *result, *prev, *copy;
24817c478bd9Sstevel@tonic-gate
24827c478bd9Sstevel@tonic-gate if (prop == NULL)
24837c478bd9Sstevel@tonic-gate return (NULL);
24847c478bd9Sstevel@tonic-gate
24857c478bd9Sstevel@tonic-gate result = prev = NULL;
24867c478bd9Sstevel@tonic-gate for (; prop != NULL; prop = prop->prop_next) {
24877c478bd9Sstevel@tonic-gate ASSERT(prop->prop_name != NULL);
24887c478bd9Sstevel@tonic-gate copy = kmem_zalloc(sizeof (struct ddi_prop), flag);
24897c478bd9Sstevel@tonic-gate if (copy == NULL)
24907c478bd9Sstevel@tonic-gate goto fail;
24917c478bd9Sstevel@tonic-gate
24927c478bd9Sstevel@tonic-gate copy->prop_dev = prop->prop_dev;
24937c478bd9Sstevel@tonic-gate copy->prop_flags = prop->prop_flags;
24947c478bd9Sstevel@tonic-gate copy->prop_name = i_ddi_strdup(prop->prop_name, flag);
2495694256ddSToomas Soome if (copy->prop_name == NULL) {
2496694256ddSToomas Soome kmem_free(copy, sizeof (struct ddi_prop));
24977c478bd9Sstevel@tonic-gate goto fail;
2498694256ddSToomas Soome }
24997c478bd9Sstevel@tonic-gate
25007c478bd9Sstevel@tonic-gate if ((copy->prop_len = prop->prop_len) != 0) {
25017c478bd9Sstevel@tonic-gate copy->prop_val = kmem_zalloc(prop->prop_len, flag);
2502694256ddSToomas Soome if (copy->prop_val == NULL) {
2503694256ddSToomas Soome strfree(copy->prop_name);
2504694256ddSToomas Soome kmem_free(copy, sizeof (struct ddi_prop));
25057c478bd9Sstevel@tonic-gate goto fail;
2506694256ddSToomas Soome }
25077c478bd9Sstevel@tonic-gate
25087c478bd9Sstevel@tonic-gate bcopy(prop->prop_val, copy->prop_val, prop->prop_len);
25097c478bd9Sstevel@tonic-gate }
25107c478bd9Sstevel@tonic-gate
25117c478bd9Sstevel@tonic-gate if (prev == NULL)
25127c478bd9Sstevel@tonic-gate result = prev = copy;
25137c478bd9Sstevel@tonic-gate else
25147c478bd9Sstevel@tonic-gate prev->prop_next = copy;
25157c478bd9Sstevel@tonic-gate prev = copy;
25167c478bd9Sstevel@tonic-gate }
25177c478bd9Sstevel@tonic-gate return (result);
25187c478bd9Sstevel@tonic-gate
25197c478bd9Sstevel@tonic-gate fail:
25207c478bd9Sstevel@tonic-gate i_ddi_prop_list_delete(result);
25217c478bd9Sstevel@tonic-gate return (NULL);
25227c478bd9Sstevel@tonic-gate }
25237c478bd9Sstevel@tonic-gate
25247c478bd9Sstevel@tonic-gate /*
25257c478bd9Sstevel@tonic-gate * Create a reference property list, currently used only for
25267c478bd9Sstevel@tonic-gate * driver global properties. Created with ref count of 1.
25277c478bd9Sstevel@tonic-gate */
25287c478bd9Sstevel@tonic-gate ddi_prop_list_t *
i_ddi_prop_list_create(ddi_prop_t * props)25297c478bd9Sstevel@tonic-gate i_ddi_prop_list_create(ddi_prop_t *props)
25307c478bd9Sstevel@tonic-gate {
25317c478bd9Sstevel@tonic-gate ddi_prop_list_t *list = kmem_alloc(sizeof (*list), KM_SLEEP);
25327c478bd9Sstevel@tonic-gate list->prop_list = props;
25337c478bd9Sstevel@tonic-gate list->prop_ref = 1;
25347c478bd9Sstevel@tonic-gate return (list);
25357c478bd9Sstevel@tonic-gate }
25367c478bd9Sstevel@tonic-gate
25377c478bd9Sstevel@tonic-gate /*
25387c478bd9Sstevel@tonic-gate * Increment/decrement reference count. The reference is
25397c478bd9Sstevel@tonic-gate * protected by dn_lock. The only interfaces modifying
25407c478bd9Sstevel@tonic-gate * dn_global_prop_ptr is in impl_make[free]_parlist().
25417c478bd9Sstevel@tonic-gate */
25427c478bd9Sstevel@tonic-gate void
i_ddi_prop_list_hold(ddi_prop_list_t * prop_list,struct devnames * dnp)25437c478bd9Sstevel@tonic-gate i_ddi_prop_list_hold(ddi_prop_list_t *prop_list, struct devnames *dnp)
25447c478bd9Sstevel@tonic-gate {
25457c478bd9Sstevel@tonic-gate ASSERT(prop_list->prop_ref >= 0);
25467c478bd9Sstevel@tonic-gate ASSERT(mutex_owned(&dnp->dn_lock));
25477c478bd9Sstevel@tonic-gate prop_list->prop_ref++;
25487c478bd9Sstevel@tonic-gate }
25497c478bd9Sstevel@tonic-gate
25507c478bd9Sstevel@tonic-gate void
i_ddi_prop_list_rele(ddi_prop_list_t * prop_list,struct devnames * dnp)25517c478bd9Sstevel@tonic-gate i_ddi_prop_list_rele(ddi_prop_list_t *prop_list, struct devnames *dnp)
25527c478bd9Sstevel@tonic-gate {
25537c478bd9Sstevel@tonic-gate ASSERT(prop_list->prop_ref > 0);
25547c478bd9Sstevel@tonic-gate ASSERT(mutex_owned(&dnp->dn_lock));
25557c478bd9Sstevel@tonic-gate prop_list->prop_ref--;
25567c478bd9Sstevel@tonic-gate
25577c478bd9Sstevel@tonic-gate if (prop_list->prop_ref == 0) {
25587c478bd9Sstevel@tonic-gate i_ddi_prop_list_delete(prop_list->prop_list);
25597c478bd9Sstevel@tonic-gate kmem_free(prop_list, sizeof (*prop_list));
25607c478bd9Sstevel@tonic-gate }
25617c478bd9Sstevel@tonic-gate }
25627c478bd9Sstevel@tonic-gate
25637c478bd9Sstevel@tonic-gate /*
25647c478bd9Sstevel@tonic-gate * Free table of classes by drivers
25657c478bd9Sstevel@tonic-gate */
25667c478bd9Sstevel@tonic-gate void
i_ddi_free_exported_classes(char ** classes,int n)25677c478bd9Sstevel@tonic-gate i_ddi_free_exported_classes(char **classes, int n)
25687c478bd9Sstevel@tonic-gate {
25697c478bd9Sstevel@tonic-gate if ((n == 0) || (classes == NULL))
25707c478bd9Sstevel@tonic-gate return;
25717c478bd9Sstevel@tonic-gate
25727c478bd9Sstevel@tonic-gate kmem_free(classes, n * sizeof (char *));
25737c478bd9Sstevel@tonic-gate }
25747c478bd9Sstevel@tonic-gate
25757c478bd9Sstevel@tonic-gate /*
25767c478bd9Sstevel@tonic-gate * Get all classes exported by dip
25777c478bd9Sstevel@tonic-gate */
25787c478bd9Sstevel@tonic-gate int
i_ddi_get_exported_classes(dev_info_t * dip,char *** classes)25797c478bd9Sstevel@tonic-gate i_ddi_get_exported_classes(dev_info_t *dip, char ***classes)
25807c478bd9Sstevel@tonic-gate {
25817c478bd9Sstevel@tonic-gate extern void lock_hw_class_list();
25827c478bd9Sstevel@tonic-gate extern void unlock_hw_class_list();
25837c478bd9Sstevel@tonic-gate extern int get_class(const char *, char **);
25847c478bd9Sstevel@tonic-gate
25857c478bd9Sstevel@tonic-gate static char *rootclass = "root";
25867c478bd9Sstevel@tonic-gate int n = 0, nclass = 0;
25877c478bd9Sstevel@tonic-gate char **buf;
25887c478bd9Sstevel@tonic-gate
25897c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
25907c478bd9Sstevel@tonic-gate
25917c478bd9Sstevel@tonic-gate if (dip == ddi_root_node()) /* rootnode exports class "root" */
25927c478bd9Sstevel@tonic-gate nclass = 1;
25937c478bd9Sstevel@tonic-gate lock_hw_class_list();
25947c478bd9Sstevel@tonic-gate nclass += get_class(ddi_driver_name(dip), NULL);
25957c478bd9Sstevel@tonic-gate if (nclass == 0) {
25967c478bd9Sstevel@tonic-gate unlock_hw_class_list();
25977c478bd9Sstevel@tonic-gate return (0); /* no class exported */
25987c478bd9Sstevel@tonic-gate }
25997c478bd9Sstevel@tonic-gate
26007c478bd9Sstevel@tonic-gate *classes = buf = kmem_alloc(nclass * sizeof (char *), KM_SLEEP);
26017c478bd9Sstevel@tonic-gate if (dip == ddi_root_node()) {
26027c478bd9Sstevel@tonic-gate *buf++ = rootclass;
26037c478bd9Sstevel@tonic-gate n = 1;
26047c478bd9Sstevel@tonic-gate }
26057c478bd9Sstevel@tonic-gate n += get_class(ddi_driver_name(dip), buf);
26067c478bd9Sstevel@tonic-gate unlock_hw_class_list();
26077c478bd9Sstevel@tonic-gate
26087c478bd9Sstevel@tonic-gate ASSERT(n == nclass); /* make sure buf wasn't overrun */
26097c478bd9Sstevel@tonic-gate return (nclass);
26107c478bd9Sstevel@tonic-gate }
26117c478bd9Sstevel@tonic-gate
26127c478bd9Sstevel@tonic-gate /*
26137c478bd9Sstevel@tonic-gate * Helper functions, returns NULL if no memory.
26147c478bd9Sstevel@tonic-gate */
26157c478bd9Sstevel@tonic-gate char *
i_ddi_strdup(const char * str,uint_t flag)26166205b5c4SRobert Mustacchi i_ddi_strdup(const char *str, uint_t flag)
26177c478bd9Sstevel@tonic-gate {
26187c478bd9Sstevel@tonic-gate char *copy;
26197c478bd9Sstevel@tonic-gate
26207c478bd9Sstevel@tonic-gate if (str == NULL)
26217c478bd9Sstevel@tonic-gate return (NULL);
26227c478bd9Sstevel@tonic-gate
26237c478bd9Sstevel@tonic-gate copy = kmem_alloc(strlen(str) + 1, flag);
26247c478bd9Sstevel@tonic-gate if (copy == NULL)
26257c478bd9Sstevel@tonic-gate return (NULL);
26267c478bd9Sstevel@tonic-gate
26277c478bd9Sstevel@tonic-gate (void) strcpy(copy, str);
26287c478bd9Sstevel@tonic-gate return (copy);
26297c478bd9Sstevel@tonic-gate }
26307c478bd9Sstevel@tonic-gate
26317c478bd9Sstevel@tonic-gate /*
26327c478bd9Sstevel@tonic-gate * Load driver.conf file for major. Load all if major == -1.
26337c478bd9Sstevel@tonic-gate *
26347c478bd9Sstevel@tonic-gate * This is called
26357c478bd9Sstevel@tonic-gate * - early in boot after devnames array is initialized
26367c478bd9Sstevel@tonic-gate * - from vfs code when certain file systems are mounted
26377c478bd9Sstevel@tonic-gate * - from add_drv when a new driver is added
26387c478bd9Sstevel@tonic-gate */
26397c478bd9Sstevel@tonic-gate int
i_ddi_load_drvconf(major_t major)26407c478bd9Sstevel@tonic-gate i_ddi_load_drvconf(major_t major)
26417c478bd9Sstevel@tonic-gate {
26427c478bd9Sstevel@tonic-gate extern int modrootloaded;
26437c478bd9Sstevel@tonic-gate
26447c478bd9Sstevel@tonic-gate major_t low, high, m;
26457c478bd9Sstevel@tonic-gate
2646a204de77Scth if (major == DDI_MAJOR_T_NONE) {
26477c478bd9Sstevel@tonic-gate low = 0;
26487c478bd9Sstevel@tonic-gate high = devcnt - 1;
26497c478bd9Sstevel@tonic-gate } else {
26507c478bd9Sstevel@tonic-gate if (major >= devcnt)
26517c478bd9Sstevel@tonic-gate return (EINVAL);
26527c478bd9Sstevel@tonic-gate low = high = major;
26537c478bd9Sstevel@tonic-gate }
26547c478bd9Sstevel@tonic-gate
26557c478bd9Sstevel@tonic-gate for (m = low; m <= high; m++) {
26567c478bd9Sstevel@tonic-gate struct devnames *dnp = &devnamesp[m];
26577c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock);
2658c9cc1492SJerry Gilliam dnp->dn_flags &= ~(DN_DRIVER_HELD|DN_DRIVER_INACTIVE);
26597c478bd9Sstevel@tonic-gate (void) impl_make_parlist(m);
26607c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
26617c478bd9Sstevel@tonic-gate }
26627c478bd9Sstevel@tonic-gate
26637c478bd9Sstevel@tonic-gate if (modrootloaded) {
26647c478bd9Sstevel@tonic-gate ddi_walk_devs(ddi_root_node(), reset_nexus_flags,
26657c478bd9Sstevel@tonic-gate (void *)(uintptr_t)major);
26667c478bd9Sstevel@tonic-gate }
26677c478bd9Sstevel@tonic-gate
26687c478bd9Sstevel@tonic-gate /* build dn_list from old entries in path_to_inst */
26697c478bd9Sstevel@tonic-gate e_ddi_unorphan_instance_nos();
26707c478bd9Sstevel@tonic-gate return (0);
26717c478bd9Sstevel@tonic-gate }
26727c478bd9Sstevel@tonic-gate
26737c478bd9Sstevel@tonic-gate /*
26747c478bd9Sstevel@tonic-gate * Unload a specific driver.conf.
26757c478bd9Sstevel@tonic-gate * Don't support unload all because it doesn't make any sense
26767c478bd9Sstevel@tonic-gate */
26777c478bd9Sstevel@tonic-gate int
i_ddi_unload_drvconf(major_t major)26787c478bd9Sstevel@tonic-gate i_ddi_unload_drvconf(major_t major)
26797c478bd9Sstevel@tonic-gate {
26807c478bd9Sstevel@tonic-gate int error;
26817c478bd9Sstevel@tonic-gate struct devnames *dnp;
26827c478bd9Sstevel@tonic-gate
26837c478bd9Sstevel@tonic-gate if (major >= devcnt)
26847c478bd9Sstevel@tonic-gate return (EINVAL);
26857c478bd9Sstevel@tonic-gate
26867c478bd9Sstevel@tonic-gate /*
26877c478bd9Sstevel@tonic-gate * Take the per-driver lock while unloading driver.conf
26887c478bd9Sstevel@tonic-gate */
26897c478bd9Sstevel@tonic-gate dnp = &devnamesp[major];
26907c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock);
26917c478bd9Sstevel@tonic-gate error = impl_free_parlist(major);
26927c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
26937c478bd9Sstevel@tonic-gate return (error);
26947c478bd9Sstevel@tonic-gate }
26957c478bd9Sstevel@tonic-gate
26967c478bd9Sstevel@tonic-gate /*
26977c478bd9Sstevel@tonic-gate * Merge a .conf node. This is called by nexus drivers to augment
26987c478bd9Sstevel@tonic-gate * hw node with properties specified in driver.conf file. This function
26997c478bd9Sstevel@tonic-gate * takes a callback routine to name nexus children.
27007c478bd9Sstevel@tonic-gate * The parent node must be held busy.
27017c478bd9Sstevel@tonic-gate *
27027c478bd9Sstevel@tonic-gate * It returns DDI_SUCCESS if the node is merged and DDI_FAILURE otherwise.
27037c478bd9Sstevel@tonic-gate */
27047c478bd9Sstevel@tonic-gate int
ndi_merge_node(dev_info_t * dip,int (* make_ua)(dev_info_t *,char *,int))27054c06356bSdh142964 ndi_merge_node(dev_info_t *dip, int (*make_ua)(dev_info_t *, char *, int))
27067c478bd9Sstevel@tonic-gate {
27077c478bd9Sstevel@tonic-gate dev_info_t *hwdip;
27087c478bd9Sstevel@tonic-gate
27097c478bd9Sstevel@tonic-gate ASSERT(ndi_dev_is_persistent_node(dip) == 0);
27107c478bd9Sstevel@tonic-gate ASSERT(ddi_get_name_addr(dip) != NULL);
27117c478bd9Sstevel@tonic-gate
27124c06356bSdh142964 hwdip = ndi_devi_findchild_by_callback(ddi_get_parent(dip),
27134c06356bSdh142964 ddi_binding_name(dip), ddi_get_name_addr(dip), make_ua);
27147c478bd9Sstevel@tonic-gate
27157c478bd9Sstevel@tonic-gate /*
27167c478bd9Sstevel@tonic-gate * Look for the hardware node that is the target of the merge;
27177c478bd9Sstevel@tonic-gate * return failure if not found.
27187c478bd9Sstevel@tonic-gate */
27197c478bd9Sstevel@tonic-gate if ((hwdip == NULL) || (hwdip == dip)) {
27207c478bd9Sstevel@tonic-gate char *buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
27217c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_WARN, "No HW node to merge conf node %s",
27227c478bd9Sstevel@tonic-gate ddi_deviname(dip, buf)));
27237c478bd9Sstevel@tonic-gate kmem_free(buf, MAXNAMELEN);
27247c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
27257c478bd9Sstevel@tonic-gate }
27267c478bd9Sstevel@tonic-gate
27277c478bd9Sstevel@tonic-gate /*
27287c478bd9Sstevel@tonic-gate * Make sure the hardware node is uninitialized and has no property.
27297c478bd9Sstevel@tonic-gate * This may not be the case if new .conf files are load after some
27307c478bd9Sstevel@tonic-gate * hardware nodes have already been initialized and attached.
27317c478bd9Sstevel@tonic-gate *
27327c478bd9Sstevel@tonic-gate * N.B. We return success here because the node was *intended*
27337c478bd9Sstevel@tonic-gate * to be a merge node because there is a hw node with the name.
27347c478bd9Sstevel@tonic-gate */
27357c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(hwdip)->devi_lock);
27367c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(hwdip) == 0) {
27377c478bd9Sstevel@tonic-gate char *buf;
27387c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(hwdip)->devi_lock);
27397c478bd9Sstevel@tonic-gate
27407c478bd9Sstevel@tonic-gate buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
27417c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_NOTE, "Duplicate .conf node %s",
27427c478bd9Sstevel@tonic-gate ddi_deviname(dip, buf)));
27437c478bd9Sstevel@tonic-gate kmem_free(buf, MAXNAMELEN);
27447c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
27457c478bd9Sstevel@tonic-gate }
27467c478bd9Sstevel@tonic-gate
27477c478bd9Sstevel@tonic-gate /*
27487c478bd9Sstevel@tonic-gate * If it is possible that the hardware has already been touched
27497c478bd9Sstevel@tonic-gate * then don't merge.
27507c478bd9Sstevel@tonic-gate */
27517c478bd9Sstevel@tonic-gate if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
27527c478bd9Sstevel@tonic-gate (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
27537c478bd9Sstevel@tonic-gate (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
27547c478bd9Sstevel@tonic-gate char *buf;
27557c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(hwdip)->devi_lock);
27567c478bd9Sstevel@tonic-gate
27577c478bd9Sstevel@tonic-gate buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
27587c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_NOTE,
27597c478bd9Sstevel@tonic-gate "!Cannot merge .conf node %s with hw node %p "
27607c478bd9Sstevel@tonic-gate "-- not in proper state",
27617c478bd9Sstevel@tonic-gate ddi_deviname(dip, buf), (void *)hwdip));
27627c478bd9Sstevel@tonic-gate kmem_free(buf, MAXNAMELEN);
27637c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
27647c478bd9Sstevel@tonic-gate }
27657c478bd9Sstevel@tonic-gate
27667c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(dip)->devi_lock);
27677c478bd9Sstevel@tonic-gate DEVI(hwdip)->devi_sys_prop_ptr = DEVI(dip)->devi_sys_prop_ptr;
27687c478bd9Sstevel@tonic-gate DEVI(hwdip)->devi_drv_prop_ptr = DEVI(dip)->devi_drv_prop_ptr;
27697c478bd9Sstevel@tonic-gate DEVI(dip)->devi_sys_prop_ptr = NULL;
27707c478bd9Sstevel@tonic-gate DEVI(dip)->devi_drv_prop_ptr = NULL;
27717c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(dip)->devi_lock);
27727c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(hwdip)->devi_lock);
27737c478bd9Sstevel@tonic-gate
27747c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
27757c478bd9Sstevel@tonic-gate }
27767c478bd9Sstevel@tonic-gate
27777c478bd9Sstevel@tonic-gate /*
27787c478bd9Sstevel@tonic-gate * Merge a "wildcard" .conf node. This is called by nexus drivers to
27797c478bd9Sstevel@tonic-gate * augment a set of hw node with properties specified in driver.conf file.
27807c478bd9Sstevel@tonic-gate * The parent node must be held busy.
27817c478bd9Sstevel@tonic-gate *
27827c478bd9Sstevel@tonic-gate * There is no failure mode, since the nexus may or may not have child
27837c478bd9Sstevel@tonic-gate * node bound the driver specified by the wildcard node.
27847c478bd9Sstevel@tonic-gate */
27857c478bd9Sstevel@tonic-gate void
ndi_merge_wildcard_node(dev_info_t * dip)27867c478bd9Sstevel@tonic-gate ndi_merge_wildcard_node(dev_info_t *dip)
27877c478bd9Sstevel@tonic-gate {
27887c478bd9Sstevel@tonic-gate dev_info_t *hwdip;
27897c478bd9Sstevel@tonic-gate dev_info_t *pdip = ddi_get_parent(dip);
27907c478bd9Sstevel@tonic-gate major_t major = ddi_driver_major(dip);
27917c478bd9Sstevel@tonic-gate
27927c478bd9Sstevel@tonic-gate /* never attempt to merge a hw node */
27937c478bd9Sstevel@tonic-gate ASSERT(ndi_dev_is_persistent_node(dip) == 0);
27947c478bd9Sstevel@tonic-gate /* must be bound to a driver major number */
2795a204de77Scth ASSERT(major != DDI_MAJOR_T_NONE);
27967c478bd9Sstevel@tonic-gate
27977c478bd9Sstevel@tonic-gate /*
27987c478bd9Sstevel@tonic-gate * Walk the child list to find all nodes bound to major
27997c478bd9Sstevel@tonic-gate * and copy properties.
28007c478bd9Sstevel@tonic-gate */
28017c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(dip)->devi_lock);
2802b9ccdc5aScth ASSERT(DEVI_BUSY_OWNED(pdip));
28037c478bd9Sstevel@tonic-gate for (hwdip = ddi_get_child(pdip); hwdip;
28047c478bd9Sstevel@tonic-gate hwdip = ddi_get_next_sibling(hwdip)) {
28057c478bd9Sstevel@tonic-gate /*
28067c478bd9Sstevel@tonic-gate * Skip nodes not bound to same driver
28077c478bd9Sstevel@tonic-gate */
28087c478bd9Sstevel@tonic-gate if (ddi_driver_major(hwdip) != major)
28097c478bd9Sstevel@tonic-gate continue;
28107c478bd9Sstevel@tonic-gate
28117c478bd9Sstevel@tonic-gate /*
28127c478bd9Sstevel@tonic-gate * Skip .conf nodes
28137c478bd9Sstevel@tonic-gate */
28147c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(hwdip) == 0)
28157c478bd9Sstevel@tonic-gate continue;
28167c478bd9Sstevel@tonic-gate
28177c478bd9Sstevel@tonic-gate /*
28187c478bd9Sstevel@tonic-gate * Make sure the node is uninitialized and has no property.
28197c478bd9Sstevel@tonic-gate */
28207c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(hwdip)->devi_lock);
28217c478bd9Sstevel@tonic-gate if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
28227c478bd9Sstevel@tonic-gate (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
28237c478bd9Sstevel@tonic-gate (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
28247c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(hwdip)->devi_lock);
28257c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_NOTE, "HW node %p state not "
28267c478bd9Sstevel@tonic-gate "suitable for merging wildcard conf node %s",
28277c478bd9Sstevel@tonic-gate (void *)hwdip, ddi_node_name(dip)));
28287c478bd9Sstevel@tonic-gate continue;
28297c478bd9Sstevel@tonic-gate }
28307c478bd9Sstevel@tonic-gate
28317c478bd9Sstevel@tonic-gate DEVI(hwdip)->devi_sys_prop_ptr =
28327c478bd9Sstevel@tonic-gate i_ddi_prop_list_dup(DEVI(dip)->devi_sys_prop_ptr, KM_SLEEP);
28337c478bd9Sstevel@tonic-gate DEVI(hwdip)->devi_drv_prop_ptr =
28347c478bd9Sstevel@tonic-gate i_ddi_prop_list_dup(DEVI(dip)->devi_drv_prop_ptr, KM_SLEEP);
28357c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(hwdip)->devi_lock);
28367c478bd9Sstevel@tonic-gate }
28377c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(dip)->devi_lock);
28387c478bd9Sstevel@tonic-gate }
28397c478bd9Sstevel@tonic-gate
28407c478bd9Sstevel@tonic-gate /*
28417c478bd9Sstevel@tonic-gate * Return the major number based on the compatible property. This interface
28427c478bd9Sstevel@tonic-gate * may be used in situations where we are trying to detect if a better driver
28437c478bd9Sstevel@tonic-gate * now exists for a device, so it must use the 'compatible' property. If
28447c478bd9Sstevel@tonic-gate * a non-NULL formp is specified and the binding was based on compatible then
28457c478bd9Sstevel@tonic-gate * return the pointer to the form used in *formp.
28467c478bd9Sstevel@tonic-gate */
28477c478bd9Sstevel@tonic-gate major_t
ddi_compatible_driver_major(dev_info_t * dip,char ** formp)28487c478bd9Sstevel@tonic-gate ddi_compatible_driver_major(dev_info_t *dip, char **formp)
28497c478bd9Sstevel@tonic-gate {
28507c478bd9Sstevel@tonic-gate struct dev_info *devi = DEVI(dip);
28517c478bd9Sstevel@tonic-gate void *compat;
28527c478bd9Sstevel@tonic-gate size_t len;
28537c478bd9Sstevel@tonic-gate char *p = NULL;
2854a204de77Scth major_t major = DDI_MAJOR_T_NONE;
28557c478bd9Sstevel@tonic-gate
28567c478bd9Sstevel@tonic-gate if (formp)
28577c478bd9Sstevel@tonic-gate *formp = NULL;
28587c478bd9Sstevel@tonic-gate
2859*9151f8f1SRichard Lowe /*
2860*9151f8f1SRichard Lowe * The "ddi-assigned" property indicates a device has been given to a
2861*9151f8f1SRichard Lowe * virtualized environment. Prevent its use. This is only used by
2862*9151f8f1SRichard Lowe * Xen and (previously) by sun4v LDOMs. See pcie_init_dom().
2863*9151f8f1SRichard Lowe */
2864fc256490SJason Beloro if (ddi_prop_exists(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
2865fc256490SJason Beloro "ddi-assigned")) {
2866fc256490SJason Beloro major = ddi_name_to_major("nulldriver");
2867fc256490SJason Beloro return (major);
2868fc256490SJason Beloro }
2869fc256490SJason Beloro
2870f4da9be0Scth /*
2871f4da9be0Scth * Highest precedence binding is a path-oriented alias. Since this
2872f4da9be0Scth * requires a 'path', this type of binding occurs via more obtuse
2873f4da9be0Scth * 'rebind'. The need for a path-oriented alias 'rebind' is detected
2874f4da9be0Scth * after a successful DDI_CTLOPS_INITCHILD to another driver: this is
2875f4da9be0Scth * is the first point at which the unit-address (or instance) of the
2876f4da9be0Scth * last component of the path is available (even though the path is
2877f4da9be0Scth * bound to the wrong driver at this point).
2878f4da9be0Scth */
2879f4da9be0Scth if (devi->devi_flags & DEVI_REBIND) {
2880f4da9be0Scth p = devi->devi_rebinding_name;
2881f4da9be0Scth major = ddi_name_to_major(p);
2882c8742f64SJerry Gilliam if (driver_active(major)) {
2883f4da9be0Scth if (formp)
2884f4da9be0Scth *formp = p;
2885f4da9be0Scth return (major);
2886f4da9be0Scth }
2887f4da9be0Scth
2888f4da9be0Scth /*
2889f4da9be0Scth * If for some reason devi_rebinding_name no longer resolves
2890f4da9be0Scth * to a proper driver then clear DEVI_REBIND.
2891f4da9be0Scth */
2892f4da9be0Scth mutex_enter(&devi->devi_lock);
2893f4da9be0Scth devi->devi_flags &= ~DEVI_REBIND;
2894f4da9be0Scth mutex_exit(&devi->devi_lock);
2895f4da9be0Scth }
2896f4da9be0Scth
28977c478bd9Sstevel@tonic-gate /* look up compatible property */
28987c478bd9Sstevel@tonic-gate (void) lookup_compatible(dip, KM_SLEEP);
28997c478bd9Sstevel@tonic-gate compat = (void *)(devi->devi_compat_names);
29007c478bd9Sstevel@tonic-gate len = devi->devi_compat_length;
29017c478bd9Sstevel@tonic-gate
29027c478bd9Sstevel@tonic-gate /* find the highest precedence compatible form with a driver binding */
29037c478bd9Sstevel@tonic-gate while ((p = prom_decode_composite_string(compat, len, p)) != NULL) {
29047c478bd9Sstevel@tonic-gate major = ddi_name_to_major(p);
2905c8742f64SJerry Gilliam if (driver_active(major)) {
29067c478bd9Sstevel@tonic-gate if (formp)
29077c478bd9Sstevel@tonic-gate *formp = p;
29087c478bd9Sstevel@tonic-gate return (major);
29097c478bd9Sstevel@tonic-gate }
29107c478bd9Sstevel@tonic-gate }
29117c478bd9Sstevel@tonic-gate
29127c478bd9Sstevel@tonic-gate /*
29137c478bd9Sstevel@tonic-gate * none of the compatible forms have a driver binding, see if
29147c478bd9Sstevel@tonic-gate * the node name has a driver binding.
29157c478bd9Sstevel@tonic-gate */
29167c478bd9Sstevel@tonic-gate major = ddi_name_to_major(ddi_node_name(dip));
2917c8742f64SJerry Gilliam if (driver_active(major))
29187c478bd9Sstevel@tonic-gate return (major);
29197c478bd9Sstevel@tonic-gate
29207c478bd9Sstevel@tonic-gate /* no driver */
2921a204de77Scth return (DDI_MAJOR_T_NONE);
29227c478bd9Sstevel@tonic-gate }
29237c478bd9Sstevel@tonic-gate
29247c478bd9Sstevel@tonic-gate /*
29257c478bd9Sstevel@tonic-gate * Static help functions
29267c478bd9Sstevel@tonic-gate */
29277c478bd9Sstevel@tonic-gate
29287c478bd9Sstevel@tonic-gate /*
29297c478bd9Sstevel@tonic-gate * lookup the "compatible" property and cache it's contents in the
29307c478bd9Sstevel@tonic-gate * device node.
29317c478bd9Sstevel@tonic-gate */
29327c478bd9Sstevel@tonic-gate static int
lookup_compatible(dev_info_t * dip,uint_t flag)29337c478bd9Sstevel@tonic-gate lookup_compatible(dev_info_t *dip, uint_t flag)
29347c478bd9Sstevel@tonic-gate {
29357c478bd9Sstevel@tonic-gate int rv;
29367c478bd9Sstevel@tonic-gate int prop_flags;
29377c478bd9Sstevel@tonic-gate uint_t ncompatstrs;
29387c478bd9Sstevel@tonic-gate char **compatstrpp;
29397c478bd9Sstevel@tonic-gate char *di_compat_strp;
29407c478bd9Sstevel@tonic-gate size_t di_compat_strlen;
29417c478bd9Sstevel@tonic-gate
29427c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_compat_names) {
29437c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
29447c478bd9Sstevel@tonic-gate }
29457c478bd9Sstevel@tonic-gate
29467c478bd9Sstevel@tonic-gate prop_flags = DDI_PROP_TYPE_STRING | DDI_PROP_DONTPASS;
29477c478bd9Sstevel@tonic-gate
29487c478bd9Sstevel@tonic-gate if (flag & KM_NOSLEEP) {
29497c478bd9Sstevel@tonic-gate prop_flags |= DDI_PROP_DONTSLEEP;
29507c478bd9Sstevel@tonic-gate }
29517c478bd9Sstevel@tonic-gate
29527c478bd9Sstevel@tonic-gate if (ndi_dev_is_prom_node(dip) == 0) {
29537c478bd9Sstevel@tonic-gate prop_flags |= DDI_PROP_NOTPROM;
29547c478bd9Sstevel@tonic-gate }
29557c478bd9Sstevel@tonic-gate
29567c478bd9Sstevel@tonic-gate rv = ddi_prop_lookup_common(DDI_DEV_T_ANY, dip, prop_flags,
29577c478bd9Sstevel@tonic-gate "compatible", &compatstrpp, &ncompatstrs,
29587c478bd9Sstevel@tonic-gate ddi_prop_fm_decode_strings);
29597c478bd9Sstevel@tonic-gate
29607c478bd9Sstevel@tonic-gate if (rv == DDI_PROP_NOT_FOUND) {
29617c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
29627c478bd9Sstevel@tonic-gate }
29637c478bd9Sstevel@tonic-gate
29647c478bd9Sstevel@tonic-gate if (rv != DDI_PROP_SUCCESS) {
29657c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
29667c478bd9Sstevel@tonic-gate }
29677c478bd9Sstevel@tonic-gate
29687c478bd9Sstevel@tonic-gate /*
29697c478bd9Sstevel@tonic-gate * encode the compatible property data in the dev_info node
29707c478bd9Sstevel@tonic-gate */
29717c478bd9Sstevel@tonic-gate rv = DDI_SUCCESS;
29727c478bd9Sstevel@tonic-gate if (ncompatstrs != 0) {
29737c478bd9Sstevel@tonic-gate di_compat_strp = encode_composite_string(compatstrpp,
29747c478bd9Sstevel@tonic-gate ncompatstrs, &di_compat_strlen, flag);
29757c478bd9Sstevel@tonic-gate if (di_compat_strp != NULL) {
29767c478bd9Sstevel@tonic-gate DEVI(dip)->devi_compat_names = di_compat_strp;
29777c478bd9Sstevel@tonic-gate DEVI(dip)->devi_compat_length = di_compat_strlen;
29787c478bd9Sstevel@tonic-gate } else {
29797c478bd9Sstevel@tonic-gate rv = DDI_FAILURE;
29807c478bd9Sstevel@tonic-gate }
29817c478bd9Sstevel@tonic-gate }
29827c478bd9Sstevel@tonic-gate ddi_prop_free(compatstrpp);
29837c478bd9Sstevel@tonic-gate return (rv);
29847c478bd9Sstevel@tonic-gate }
29857c478bd9Sstevel@tonic-gate
29867c478bd9Sstevel@tonic-gate /*
29877c478bd9Sstevel@tonic-gate * Create a composite string from a list of strings.
29887c478bd9Sstevel@tonic-gate *
29897c478bd9Sstevel@tonic-gate * A composite string consists of a single buffer containing one
29907c478bd9Sstevel@tonic-gate * or more NULL terminated strings.
29917c478bd9Sstevel@tonic-gate */
29927c478bd9Sstevel@tonic-gate static char *
encode_composite_string(char ** strings,uint_t nstrings,size_t * retsz,uint_t flag)29937c478bd9Sstevel@tonic-gate encode_composite_string(char **strings, uint_t nstrings, size_t *retsz,
29947c478bd9Sstevel@tonic-gate uint_t flag)
29957c478bd9Sstevel@tonic-gate {
29967c478bd9Sstevel@tonic-gate uint_t index;
29977c478bd9Sstevel@tonic-gate char **strpp;
29987c478bd9Sstevel@tonic-gate uint_t slen;
29997c478bd9Sstevel@tonic-gate size_t cbuf_sz = 0;
30007c478bd9Sstevel@tonic-gate char *cbuf_p;
30017c478bd9Sstevel@tonic-gate char *cbuf_ip;
30027c478bd9Sstevel@tonic-gate
30037c478bd9Sstevel@tonic-gate if (strings == NULL || nstrings == 0 || retsz == NULL) {
30047c478bd9Sstevel@tonic-gate return (NULL);
30057c478bd9Sstevel@tonic-gate }
30067c478bd9Sstevel@tonic-gate
30077c478bd9Sstevel@tonic-gate for (index = 0, strpp = strings; index < nstrings; index++)
30087c478bd9Sstevel@tonic-gate cbuf_sz += strlen(*(strpp++)) + 1;
30097c478bd9Sstevel@tonic-gate
30107c478bd9Sstevel@tonic-gate if ((cbuf_p = kmem_alloc(cbuf_sz, flag)) == NULL) {
30117c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE,
30127c478bd9Sstevel@tonic-gate "?failed to allocate device node compatstr");
30137c478bd9Sstevel@tonic-gate return (NULL);
30147c478bd9Sstevel@tonic-gate }
30157c478bd9Sstevel@tonic-gate
30167c478bd9Sstevel@tonic-gate cbuf_ip = cbuf_p;
30177c478bd9Sstevel@tonic-gate for (index = 0, strpp = strings; index < nstrings; index++) {
30187c478bd9Sstevel@tonic-gate slen = strlen(*strpp);
30197c478bd9Sstevel@tonic-gate bcopy(*(strpp++), cbuf_ip, slen);
30207c478bd9Sstevel@tonic-gate cbuf_ip += slen;
30217c478bd9Sstevel@tonic-gate *(cbuf_ip++) = '\0';
30227c478bd9Sstevel@tonic-gate }
30237c478bd9Sstevel@tonic-gate
30247c478bd9Sstevel@tonic-gate *retsz = cbuf_sz;
30257c478bd9Sstevel@tonic-gate return (cbuf_p);
30267c478bd9Sstevel@tonic-gate }
30277c478bd9Sstevel@tonic-gate
30287c478bd9Sstevel@tonic-gate static void
link_to_driver_list(dev_info_t * dip)30297c478bd9Sstevel@tonic-gate link_to_driver_list(dev_info_t *dip)
30307c478bd9Sstevel@tonic-gate {
30317c478bd9Sstevel@tonic-gate major_t major = DEVI(dip)->devi_major;
30327c478bd9Sstevel@tonic-gate struct devnames *dnp;
30337c478bd9Sstevel@tonic-gate
3034a204de77Scth ASSERT(major != DDI_MAJOR_T_NONE);
30357c478bd9Sstevel@tonic-gate
30367c478bd9Sstevel@tonic-gate /*
30377c478bd9Sstevel@tonic-gate * Remove from orphan list
30387c478bd9Sstevel@tonic-gate */
30397c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(dip)) {
30407c478bd9Sstevel@tonic-gate dnp = &orphanlist;
30417c478bd9Sstevel@tonic-gate remove_from_dn_list(dnp, dip);
30427c478bd9Sstevel@tonic-gate }
30437c478bd9Sstevel@tonic-gate
30447c478bd9Sstevel@tonic-gate /*
30457c478bd9Sstevel@tonic-gate * Add to per driver list
30467c478bd9Sstevel@tonic-gate */
30477c478bd9Sstevel@tonic-gate dnp = &devnamesp[major];
30487c478bd9Sstevel@tonic-gate add_to_dn_list(dnp, dip);
30497c478bd9Sstevel@tonic-gate }
30507c478bd9Sstevel@tonic-gate
30517c478bd9Sstevel@tonic-gate static void
unlink_from_driver_list(dev_info_t * dip)30527c478bd9Sstevel@tonic-gate unlink_from_driver_list(dev_info_t *dip)
30537c478bd9Sstevel@tonic-gate {
30547c478bd9Sstevel@tonic-gate major_t major = DEVI(dip)->devi_major;
30557c478bd9Sstevel@tonic-gate struct devnames *dnp;
30567c478bd9Sstevel@tonic-gate
3057a204de77Scth ASSERT(major != DDI_MAJOR_T_NONE);
30587c478bd9Sstevel@tonic-gate
30597c478bd9Sstevel@tonic-gate /*
30607c478bd9Sstevel@tonic-gate * Remove from per-driver list
30617c478bd9Sstevel@tonic-gate */
30627c478bd9Sstevel@tonic-gate dnp = &devnamesp[major];
30637c478bd9Sstevel@tonic-gate remove_from_dn_list(dnp, dip);
30647c478bd9Sstevel@tonic-gate
30657c478bd9Sstevel@tonic-gate /*
30667c478bd9Sstevel@tonic-gate * Add to orphan list
30677c478bd9Sstevel@tonic-gate */
30687c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(dip)) {
30697c478bd9Sstevel@tonic-gate dnp = &orphanlist;
30707c478bd9Sstevel@tonic-gate add_to_dn_list(dnp, dip);
30717c478bd9Sstevel@tonic-gate }
30727c478bd9Sstevel@tonic-gate }
30737c478bd9Sstevel@tonic-gate
30747c478bd9Sstevel@tonic-gate /*
30757c478bd9Sstevel@tonic-gate * scan the per-driver list looking for dev_info "dip"
30767c478bd9Sstevel@tonic-gate */
30777c478bd9Sstevel@tonic-gate static dev_info_t *
in_dn_list(struct devnames * dnp,dev_info_t * dip)30787c478bd9Sstevel@tonic-gate in_dn_list(struct devnames *dnp, dev_info_t *dip)
30797c478bd9Sstevel@tonic-gate {
30807c478bd9Sstevel@tonic-gate struct dev_info *idevi;
30817c478bd9Sstevel@tonic-gate
30827c478bd9Sstevel@tonic-gate if ((idevi = DEVI(dnp->dn_head)) == NULL)
30837c478bd9Sstevel@tonic-gate return (NULL);
30847c478bd9Sstevel@tonic-gate
30857c478bd9Sstevel@tonic-gate while (idevi) {
30867c478bd9Sstevel@tonic-gate if (idevi == DEVI(dip))
30877c478bd9Sstevel@tonic-gate return (dip);
30887c478bd9Sstevel@tonic-gate idevi = idevi->devi_next;
30897c478bd9Sstevel@tonic-gate }
30907c478bd9Sstevel@tonic-gate return (NULL);
30917c478bd9Sstevel@tonic-gate }
30927c478bd9Sstevel@tonic-gate
30937c478bd9Sstevel@tonic-gate /*
30947c478bd9Sstevel@tonic-gate * insert devinfo node 'dip' into the per-driver instance list
30957c478bd9Sstevel@tonic-gate * headed by 'dnp'
30967c478bd9Sstevel@tonic-gate *
30977c478bd9Sstevel@tonic-gate * Nodes on the per-driver list are ordered: HW - SID - PSEUDO. The order is
30987c478bd9Sstevel@tonic-gate * required for merging of .conf file data to work properly.
30997c478bd9Sstevel@tonic-gate */
31007c478bd9Sstevel@tonic-gate static void
add_to_ordered_dn_list(struct devnames * dnp,dev_info_t * dip)31017c478bd9Sstevel@tonic-gate add_to_ordered_dn_list(struct devnames *dnp, dev_info_t *dip)
31027c478bd9Sstevel@tonic-gate {
31037c478bd9Sstevel@tonic-gate dev_info_t **dipp;
31047c478bd9Sstevel@tonic-gate
31057c478bd9Sstevel@tonic-gate ASSERT(mutex_owned(&(dnp->dn_lock)));
31067c478bd9Sstevel@tonic-gate
31077c478bd9Sstevel@tonic-gate dipp = &dnp->dn_head;
31087c478bd9Sstevel@tonic-gate if (ndi_dev_is_prom_node(dip)) {
31097c478bd9Sstevel@tonic-gate /*
31107c478bd9Sstevel@tonic-gate * Find the first non-prom node or end of list
31117c478bd9Sstevel@tonic-gate */
31127c478bd9Sstevel@tonic-gate while (*dipp && (ndi_dev_is_prom_node(*dipp) != 0)) {
31137c478bd9Sstevel@tonic-gate dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
31147c478bd9Sstevel@tonic-gate }
31157c478bd9Sstevel@tonic-gate } else if (ndi_dev_is_persistent_node(dip)) {
31167c478bd9Sstevel@tonic-gate /*
31177c478bd9Sstevel@tonic-gate * Find the first non-persistent node
31187c478bd9Sstevel@tonic-gate */
31197c478bd9Sstevel@tonic-gate while (*dipp && (ndi_dev_is_persistent_node(*dipp) != 0)) {
31207c478bd9Sstevel@tonic-gate dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
31217c478bd9Sstevel@tonic-gate }
31227c478bd9Sstevel@tonic-gate } else {
31237c478bd9Sstevel@tonic-gate /*
31247c478bd9Sstevel@tonic-gate * Find the end of the list
31257c478bd9Sstevel@tonic-gate */
31267c478bd9Sstevel@tonic-gate while (*dipp) {
31277c478bd9Sstevel@tonic-gate dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
31287c478bd9Sstevel@tonic-gate }
31297c478bd9Sstevel@tonic-gate }
31307c478bd9Sstevel@tonic-gate
31317c478bd9Sstevel@tonic-gate DEVI(dip)->devi_next = DEVI(*dipp);
31327c478bd9Sstevel@tonic-gate *dipp = dip;
31337c478bd9Sstevel@tonic-gate }
31347c478bd9Sstevel@tonic-gate
31357c478bd9Sstevel@tonic-gate /*
31367c478bd9Sstevel@tonic-gate * add a list of device nodes to the device node list in the
31377c478bd9Sstevel@tonic-gate * devnames structure
31387c478bd9Sstevel@tonic-gate */
31397c478bd9Sstevel@tonic-gate static void
add_to_dn_list(struct devnames * dnp,dev_info_t * dip)31407c478bd9Sstevel@tonic-gate add_to_dn_list(struct devnames *dnp, dev_info_t *dip)
31417c478bd9Sstevel@tonic-gate {
31427c478bd9Sstevel@tonic-gate /*
31437c478bd9Sstevel@tonic-gate * Look to see if node already exists
31447c478bd9Sstevel@tonic-gate */
31457c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&(dnp->dn_lock));
31467c478bd9Sstevel@tonic-gate if (in_dn_list(dnp, dip)) {
31477c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "add_to_dn_list: node %s already in list",
31487c478bd9Sstevel@tonic-gate DEVI(dip)->devi_node_name);
31497c478bd9Sstevel@tonic-gate } else {
31507c478bd9Sstevel@tonic-gate add_to_ordered_dn_list(dnp, dip);
31517c478bd9Sstevel@tonic-gate }
31527c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&(dnp->dn_lock));
31537c478bd9Sstevel@tonic-gate }
31547c478bd9Sstevel@tonic-gate
31557c478bd9Sstevel@tonic-gate static void
remove_from_dn_list(struct devnames * dnp,dev_info_t * dip)31567c478bd9Sstevel@tonic-gate remove_from_dn_list(struct devnames *dnp, dev_info_t *dip)
31577c478bd9Sstevel@tonic-gate {
31587c478bd9Sstevel@tonic-gate dev_info_t **plist;
31597c478bd9Sstevel@tonic-gate
31607c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&(dnp->dn_lock));
31617c478bd9Sstevel@tonic-gate
31627c478bd9Sstevel@tonic-gate plist = (dev_info_t **)&dnp->dn_head;
31637c478bd9Sstevel@tonic-gate while (*plist && (*plist != dip)) {
31647c478bd9Sstevel@tonic-gate plist = (dev_info_t **)&DEVI(*plist)->devi_next;
31657c478bd9Sstevel@tonic-gate }
31667c478bd9Sstevel@tonic-gate
31677c478bd9Sstevel@tonic-gate if (*plist != NULL) {
31687c478bd9Sstevel@tonic-gate ASSERT(*plist == dip);
31697c478bd9Sstevel@tonic-gate *plist = (dev_info_t *)(DEVI(dip)->devi_next);
31707c478bd9Sstevel@tonic-gate DEVI(dip)->devi_next = NULL;
31717c478bd9Sstevel@tonic-gate } else {
31727c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_NOTE,
31737c478bd9Sstevel@tonic-gate "remove_from_dn_list: node %s not found in list",
31747c478bd9Sstevel@tonic-gate DEVI(dip)->devi_node_name));
31757c478bd9Sstevel@tonic-gate }
31767c478bd9Sstevel@tonic-gate
31777c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&(dnp->dn_lock));
31787c478bd9Sstevel@tonic-gate }
31797c478bd9Sstevel@tonic-gate
31807c478bd9Sstevel@tonic-gate /*
31817c478bd9Sstevel@tonic-gate * Add and remove reference driver global property list
31827c478bd9Sstevel@tonic-gate */
31837c478bd9Sstevel@tonic-gate static void
add_global_props(dev_info_t * dip)31847c478bd9Sstevel@tonic-gate add_global_props(dev_info_t *dip)
31857c478bd9Sstevel@tonic-gate {
31867c478bd9Sstevel@tonic-gate struct devnames *dnp;
31877c478bd9Sstevel@tonic-gate ddi_prop_list_t *plist;
31887c478bd9Sstevel@tonic-gate
31897c478bd9Sstevel@tonic-gate ASSERT(DEVI(dip)->devi_global_prop_list == NULL);
3190a204de77Scth ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE);
31917c478bd9Sstevel@tonic-gate
31927c478bd9Sstevel@tonic-gate dnp = &devnamesp[DEVI(dip)->devi_major];
31937c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock);
31947c478bd9Sstevel@tonic-gate plist = dnp->dn_global_prop_ptr;
31957c478bd9Sstevel@tonic-gate if (plist == NULL) {
31967c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
31977c478bd9Sstevel@tonic-gate return;
31987c478bd9Sstevel@tonic-gate }
31997c478bd9Sstevel@tonic-gate i_ddi_prop_list_hold(plist, dnp);
32007c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
32017c478bd9Sstevel@tonic-gate
32027c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(dip)->devi_lock);
32037c478bd9Sstevel@tonic-gate DEVI(dip)->devi_global_prop_list = plist;
32047c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(dip)->devi_lock);
32057c478bd9Sstevel@tonic-gate }
32067c478bd9Sstevel@tonic-gate
32077c478bd9Sstevel@tonic-gate static void
remove_global_props(dev_info_t * dip)32087c478bd9Sstevel@tonic-gate remove_global_props(dev_info_t *dip)
32097c478bd9Sstevel@tonic-gate {
32107c478bd9Sstevel@tonic-gate ddi_prop_list_t *proplist;
32117c478bd9Sstevel@tonic-gate
32127c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(dip)->devi_lock);
32137c478bd9Sstevel@tonic-gate proplist = DEVI(dip)->devi_global_prop_list;
32147c478bd9Sstevel@tonic-gate DEVI(dip)->devi_global_prop_list = NULL;
32157c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(dip)->devi_lock);
32167c478bd9Sstevel@tonic-gate
32177c478bd9Sstevel@tonic-gate if (proplist) {
32187c478bd9Sstevel@tonic-gate major_t major;
32197c478bd9Sstevel@tonic-gate struct devnames *dnp;
32207c478bd9Sstevel@tonic-gate
32217c478bd9Sstevel@tonic-gate major = ddi_driver_major(dip);
3222a204de77Scth ASSERT(major != DDI_MAJOR_T_NONE);
32237c478bd9Sstevel@tonic-gate dnp = &devnamesp[major];
32247c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock);
32257c478bd9Sstevel@tonic-gate i_ddi_prop_list_rele(proplist, dnp);
32267c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
32277c478bd9Sstevel@tonic-gate }
32287c478bd9Sstevel@tonic-gate }
32297c478bd9Sstevel@tonic-gate
32307c478bd9Sstevel@tonic-gate #ifdef DEBUG
32317c478bd9Sstevel@tonic-gate /*
32327c478bd9Sstevel@tonic-gate * Set this variable to '0' to disable the optimization,
32337c478bd9Sstevel@tonic-gate * and to 2 to print debug message.
32347c478bd9Sstevel@tonic-gate */
32357c478bd9Sstevel@tonic-gate static int optimize_dtree = 1;
32367c478bd9Sstevel@tonic-gate
32377c478bd9Sstevel@tonic-gate static void
debug_dtree(dev_info_t * devi,struct dev_info * adevi,char * service)32387c478bd9Sstevel@tonic-gate debug_dtree(dev_info_t *devi, struct dev_info *adevi, char *service)
32397c478bd9Sstevel@tonic-gate {
32407c478bd9Sstevel@tonic-gate char *adeviname, *buf;
32417c478bd9Sstevel@tonic-gate
32427c478bd9Sstevel@tonic-gate /*
32437c478bd9Sstevel@tonic-gate * Don't print unless optimize dtree is set to 2+
32447c478bd9Sstevel@tonic-gate */
32457c478bd9Sstevel@tonic-gate if (optimize_dtree <= 1)
32467c478bd9Sstevel@tonic-gate return;
32477c478bd9Sstevel@tonic-gate
32487c478bd9Sstevel@tonic-gate buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
32497c478bd9Sstevel@tonic-gate adeviname = ddi_deviname((dev_info_t *)adevi, buf);
32507c478bd9Sstevel@tonic-gate if (*adeviname == '\0')
32517c478bd9Sstevel@tonic-gate adeviname = "root";
32527c478bd9Sstevel@tonic-gate
32537c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, "%s %s -> %s\n",
32547c478bd9Sstevel@tonic-gate ddi_deviname(devi, buf), service, adeviname);
32557c478bd9Sstevel@tonic-gate
32567c478bd9Sstevel@tonic-gate kmem_free(buf, MAXNAMELEN);
32577c478bd9Sstevel@tonic-gate }
32587c478bd9Sstevel@tonic-gate #else /* DEBUG */
32597c478bd9Sstevel@tonic-gate #define debug_dtree(a1, a2, a3) /* nothing */
32607c478bd9Sstevel@tonic-gate #endif /* DEBUG */
32617c478bd9Sstevel@tonic-gate
32627c478bd9Sstevel@tonic-gate static void
ddi_optimize_dtree(dev_info_t * devi)32637c478bd9Sstevel@tonic-gate ddi_optimize_dtree(dev_info_t *devi)
32647c478bd9Sstevel@tonic-gate {
32657c478bd9Sstevel@tonic-gate struct dev_info *pdevi;
32667c478bd9Sstevel@tonic-gate struct bus_ops *b;
32677c478bd9Sstevel@tonic-gate
32687c478bd9Sstevel@tonic-gate pdevi = DEVI(devi)->devi_parent;
32697c478bd9Sstevel@tonic-gate ASSERT(pdevi);
32707c478bd9Sstevel@tonic-gate
32717c478bd9Sstevel@tonic-gate /*
32727c478bd9Sstevel@tonic-gate * Set the unoptimized values
32737c478bd9Sstevel@tonic-gate */
32747c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_map_fault = pdevi;
32757c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_allochdl = pdevi;
32767c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_freehdl = pdevi;
32777c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_bindhdl = pdevi;
32787c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_bindfunc =
32797c478bd9Sstevel@tonic-gate pdevi->devi_ops->devo_bus_ops->bus_dma_bindhdl;
32807c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_unbindhdl = pdevi;
32817c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_unbindfunc =
32827c478bd9Sstevel@tonic-gate pdevi->devi_ops->devo_bus_ops->bus_dma_unbindhdl;
32837c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_flush = pdevi;
32847c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_win = pdevi;
32857c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_ctl = pdevi;
32867c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_ctl = pdevi;
32877c478bd9Sstevel@tonic-gate
32887c478bd9Sstevel@tonic-gate #ifdef DEBUG
32897c478bd9Sstevel@tonic-gate if (optimize_dtree == 0)
32907c478bd9Sstevel@tonic-gate return;
32917c478bd9Sstevel@tonic-gate #endif /* DEBUG */
32927c478bd9Sstevel@tonic-gate
32937c478bd9Sstevel@tonic-gate b = pdevi->devi_ops->devo_bus_ops;
32947c478bd9Sstevel@tonic-gate
32957c478bd9Sstevel@tonic-gate if (i_ddi_map_fault == b->bus_map_fault) {
32967c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_map_fault = pdevi->devi_bus_map_fault;
32977c478bd9Sstevel@tonic-gate debug_dtree(devi, DEVI(devi)->devi_bus_map_fault,
32987c478bd9Sstevel@tonic-gate "bus_map_fault");
32997c478bd9Sstevel@tonic-gate }
33007c478bd9Sstevel@tonic-gate
33017c478bd9Sstevel@tonic-gate if (ddi_dma_allochdl == b->bus_dma_allochdl) {
33027c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_allochdl =
33037c478bd9Sstevel@tonic-gate pdevi->devi_bus_dma_allochdl;
33047c478bd9Sstevel@tonic-gate debug_dtree(devi, DEVI(devi)->devi_bus_dma_allochdl,
33057c478bd9Sstevel@tonic-gate "bus_dma_allochdl");
33067c478bd9Sstevel@tonic-gate }
33077c478bd9Sstevel@tonic-gate
33087c478bd9Sstevel@tonic-gate if (ddi_dma_freehdl == b->bus_dma_freehdl) {
33097c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_freehdl = pdevi->devi_bus_dma_freehdl;
33107c478bd9Sstevel@tonic-gate debug_dtree(devi, DEVI(devi)->devi_bus_dma_freehdl,
33117c478bd9Sstevel@tonic-gate "bus_dma_freehdl");
33127c478bd9Sstevel@tonic-gate }
33137c478bd9Sstevel@tonic-gate
33147c478bd9Sstevel@tonic-gate if (ddi_dma_bindhdl == b->bus_dma_bindhdl) {
33157c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_bindhdl = pdevi->devi_bus_dma_bindhdl;
33167c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_bindfunc =
33177c478bd9Sstevel@tonic-gate pdevi->devi_bus_dma_bindhdl->devi_ops->
33187c478bd9Sstevel@tonic-gate devo_bus_ops->bus_dma_bindhdl;
33197c478bd9Sstevel@tonic-gate debug_dtree(devi, DEVI(devi)->devi_bus_dma_bindhdl,
33207c478bd9Sstevel@tonic-gate "bus_dma_bindhdl");
33217c478bd9Sstevel@tonic-gate }
33227c478bd9Sstevel@tonic-gate
33237c478bd9Sstevel@tonic-gate if (ddi_dma_unbindhdl == b->bus_dma_unbindhdl) {
33247c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_unbindhdl =
33257c478bd9Sstevel@tonic-gate pdevi->devi_bus_dma_unbindhdl;
33267c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_unbindfunc =
33277c478bd9Sstevel@tonic-gate pdevi->devi_bus_dma_unbindhdl->devi_ops->
33287c478bd9Sstevel@tonic-gate devo_bus_ops->bus_dma_unbindhdl;
33297c478bd9Sstevel@tonic-gate debug_dtree(devi, DEVI(devi)->devi_bus_dma_unbindhdl,
33307c478bd9Sstevel@tonic-gate "bus_dma_unbindhdl");
33317c478bd9Sstevel@tonic-gate }
33327c478bd9Sstevel@tonic-gate
33337c478bd9Sstevel@tonic-gate if (ddi_dma_flush == b->bus_dma_flush) {
33347c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_flush = pdevi->devi_bus_dma_flush;
33357c478bd9Sstevel@tonic-gate debug_dtree(devi, DEVI(devi)->devi_bus_dma_flush,
33367c478bd9Sstevel@tonic-gate "bus_dma_flush");
33377c478bd9Sstevel@tonic-gate }
33387c478bd9Sstevel@tonic-gate
33397c478bd9Sstevel@tonic-gate if (ddi_dma_win == b->bus_dma_win) {
33407c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_win = pdevi->devi_bus_dma_win;
33417c478bd9Sstevel@tonic-gate debug_dtree(devi, DEVI(devi)->devi_bus_dma_win,
33427c478bd9Sstevel@tonic-gate "bus_dma_win");
33437c478bd9Sstevel@tonic-gate }
33447c478bd9Sstevel@tonic-gate
33457c478bd9Sstevel@tonic-gate if (ddi_dma_mctl == b->bus_dma_ctl) {
33467c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_ctl = pdevi->devi_bus_dma_ctl;
33477c478bd9Sstevel@tonic-gate debug_dtree(devi, DEVI(devi)->devi_bus_dma_ctl, "bus_dma_ctl");
33487c478bd9Sstevel@tonic-gate }
33497c478bd9Sstevel@tonic-gate
33507c478bd9Sstevel@tonic-gate if (ddi_ctlops == b->bus_ctl) {
33517c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_ctl = pdevi->devi_bus_ctl;
33527c478bd9Sstevel@tonic-gate debug_dtree(devi, DEVI(devi)->devi_bus_ctl, "bus_ctl");
33537c478bd9Sstevel@tonic-gate }
33547c478bd9Sstevel@tonic-gate }
33557c478bd9Sstevel@tonic-gate
33567c478bd9Sstevel@tonic-gate #define MIN_DEVINFO_LOG_SIZE max_ncpus
33577c478bd9Sstevel@tonic-gate #define MAX_DEVINFO_LOG_SIZE max_ncpus * 10
33587c478bd9Sstevel@tonic-gate
33597c478bd9Sstevel@tonic-gate static void
da_log_init()33607c478bd9Sstevel@tonic-gate da_log_init()
33617c478bd9Sstevel@tonic-gate {
33627c478bd9Sstevel@tonic-gate devinfo_log_header_t *dh;
33637c478bd9Sstevel@tonic-gate int logsize = devinfo_log_size;
33647c478bd9Sstevel@tonic-gate
33657c478bd9Sstevel@tonic-gate if (logsize == 0)
33667c478bd9Sstevel@tonic-gate logsize = MIN_DEVINFO_LOG_SIZE;
33677c478bd9Sstevel@tonic-gate else if (logsize > MAX_DEVINFO_LOG_SIZE)
33687c478bd9Sstevel@tonic-gate logsize = MAX_DEVINFO_LOG_SIZE;
33697c478bd9Sstevel@tonic-gate
33707c478bd9Sstevel@tonic-gate dh = kmem_alloc(logsize * PAGESIZE, KM_SLEEP);
33717c478bd9Sstevel@tonic-gate mutex_init(&dh->dh_lock, NULL, MUTEX_DEFAULT, NULL);
33727c478bd9Sstevel@tonic-gate dh->dh_max = ((logsize * PAGESIZE) - sizeof (*dh)) /
33737c478bd9Sstevel@tonic-gate sizeof (devinfo_audit_t) + 1;
33747c478bd9Sstevel@tonic-gate dh->dh_curr = -1;
33757c478bd9Sstevel@tonic-gate dh->dh_hits = 0;
33767c478bd9Sstevel@tonic-gate
33777c478bd9Sstevel@tonic-gate devinfo_audit_log = dh;
33787c478bd9Sstevel@tonic-gate }
33797c478bd9Sstevel@tonic-gate
33807c478bd9Sstevel@tonic-gate /*
33817c478bd9Sstevel@tonic-gate * Log the stack trace in per-devinfo audit structure and also enter
33827c478bd9Sstevel@tonic-gate * it into a system wide log for recording the time history.
33837c478bd9Sstevel@tonic-gate */
33847c478bd9Sstevel@tonic-gate static void
da_log_enter(dev_info_t * dip)33857c478bd9Sstevel@tonic-gate da_log_enter(dev_info_t *dip)
33867c478bd9Sstevel@tonic-gate {
33877c478bd9Sstevel@tonic-gate devinfo_audit_t *da_log, *da = DEVI(dip)->devi_audit;
33887c478bd9Sstevel@tonic-gate devinfo_log_header_t *dh = devinfo_audit_log;
33897c478bd9Sstevel@tonic-gate
33907c478bd9Sstevel@tonic-gate if (devinfo_audit_log == NULL)
33917c478bd9Sstevel@tonic-gate return;
33927c478bd9Sstevel@tonic-gate
33937c478bd9Sstevel@tonic-gate ASSERT(da != NULL);
33947c478bd9Sstevel@tonic-gate
33957c478bd9Sstevel@tonic-gate da->da_devinfo = dip;
33967c478bd9Sstevel@tonic-gate da->da_timestamp = gethrtime();
33977c478bd9Sstevel@tonic-gate da->da_thread = curthread;
33987c478bd9Sstevel@tonic-gate da->da_node_state = DEVI(dip)->devi_node_state;
33997c478bd9Sstevel@tonic-gate da->da_device_state = DEVI(dip)->devi_state;
34007c478bd9Sstevel@tonic-gate da->da_depth = getpcstack(da->da_stack, DDI_STACK_DEPTH);
34017c478bd9Sstevel@tonic-gate
34027c478bd9Sstevel@tonic-gate /*
34037c478bd9Sstevel@tonic-gate * Copy into common log and note the location for tracing history
34047c478bd9Sstevel@tonic-gate */
34057c478bd9Sstevel@tonic-gate mutex_enter(&dh->dh_lock);
34067c478bd9Sstevel@tonic-gate dh->dh_hits++;
34077c478bd9Sstevel@tonic-gate dh->dh_curr++;
34087c478bd9Sstevel@tonic-gate if (dh->dh_curr >= dh->dh_max)
34097c478bd9Sstevel@tonic-gate dh->dh_curr -= dh->dh_max;
34107c478bd9Sstevel@tonic-gate da_log = &dh->dh_entry[dh->dh_curr];
34117c478bd9Sstevel@tonic-gate mutex_exit(&dh->dh_lock);
34127c478bd9Sstevel@tonic-gate
34137c478bd9Sstevel@tonic-gate bcopy(da, da_log, sizeof (devinfo_audit_t));
34147c478bd9Sstevel@tonic-gate da->da_lastlog = da_log;
34157c478bd9Sstevel@tonic-gate }
34167c478bd9Sstevel@tonic-gate
34177c478bd9Sstevel@tonic-gate static void
attach_drivers()34187c478bd9Sstevel@tonic-gate attach_drivers()
34197c478bd9Sstevel@tonic-gate {
34207c478bd9Sstevel@tonic-gate int i;
34217c478bd9Sstevel@tonic-gate for (i = 0; i < devcnt; i++) {
34227c478bd9Sstevel@tonic-gate struct devnames *dnp = &devnamesp[i];
34237c478bd9Sstevel@tonic-gate if ((dnp->dn_flags & DN_FORCE_ATTACH) &&
34247c478bd9Sstevel@tonic-gate (ddi_hold_installed_driver((major_t)i) != NULL))
34257c478bd9Sstevel@tonic-gate ddi_rele_driver((major_t)i);
34267c478bd9Sstevel@tonic-gate }
34277c478bd9Sstevel@tonic-gate }
34287c478bd9Sstevel@tonic-gate
34297c478bd9Sstevel@tonic-gate /*
34307c478bd9Sstevel@tonic-gate * Launch a thread to force attach drivers. This avoids penalty on boot time.
34317c478bd9Sstevel@tonic-gate */
34327c478bd9Sstevel@tonic-gate void
i_ddi_forceattach_drivers()34337c478bd9Sstevel@tonic-gate i_ddi_forceattach_drivers()
34347c478bd9Sstevel@tonic-gate {
34357c478bd9Sstevel@tonic-gate
34369d3d2ed0Shiremath /*
34379d3d2ed0Shiremath * Attach IB VHCI driver before the force-attach thread attaches the
34389d3d2ed0Shiremath * IB HCA driver. IB HCA driver will fail if IB Nexus has not yet
34399d3d2ed0Shiremath * been attached.
34409d3d2ed0Shiremath */
34419d3d2ed0Shiremath (void) ddi_hold_installed_driver(ddi_name_to_major("ib"));
34429d3d2ed0Shiremath
34437c478bd9Sstevel@tonic-gate (void) thread_create(NULL, 0, (void (*)())attach_drivers, NULL, 0, &p0,
34447c478bd9Sstevel@tonic-gate TS_RUN, minclsyspri);
34457c478bd9Sstevel@tonic-gate }
34467c478bd9Sstevel@tonic-gate
34477c478bd9Sstevel@tonic-gate /*
34487c478bd9Sstevel@tonic-gate * This is a private DDI interface for optimizing boot performance.
34497c478bd9Sstevel@tonic-gate * I/O subsystem initialization is considered complete when devfsadm
34507c478bd9Sstevel@tonic-gate * is executed.
34517c478bd9Sstevel@tonic-gate *
3452facf4a8dSllai1 * NOTE: The start of syseventd happens to be a convenient indicator
3453facf4a8dSllai1 * of the completion of I/O initialization during boot.
34547c478bd9Sstevel@tonic-gate * The implementation should be replaced by something more robust.
34557c478bd9Sstevel@tonic-gate */
34567c478bd9Sstevel@tonic-gate int
i_ddi_io_initialized()34577c478bd9Sstevel@tonic-gate i_ddi_io_initialized()
34587c478bd9Sstevel@tonic-gate {
34597c478bd9Sstevel@tonic-gate extern int sysevent_daemon_init;
34607c478bd9Sstevel@tonic-gate return (sysevent_daemon_init);
34617c478bd9Sstevel@tonic-gate }
34627c478bd9Sstevel@tonic-gate
3463facf4a8dSllai1 /*
3464facf4a8dSllai1 * May be used to determine system boot state
3465facf4a8dSllai1 * "Available" means the system is for the most part up
3466facf4a8dSllai1 * and initialized, with all system services either up or
3467facf4a8dSllai1 * capable of being started. This state is set by devfsadm
3468facf4a8dSllai1 * during the boot process. The /dev filesystem infers
3469facf4a8dSllai1 * from this when implicit reconfig can be performed,
3470facf4a8dSllai1 * ie, devfsadm can be invoked. Please avoid making
3471facf4a8dSllai1 * further use of this unless it's really necessary.
3472facf4a8dSllai1 */
3473facf4a8dSllai1 int
i_ddi_sysavail()3474facf4a8dSllai1 i_ddi_sysavail()
3475facf4a8dSllai1 {
3476facf4a8dSllai1 return (devname_state & DS_SYSAVAIL);
3477facf4a8dSllai1 }
3478facf4a8dSllai1
3479facf4a8dSllai1 /*
3480facf4a8dSllai1 * May be used to determine if boot is a reconfigure boot.
3481facf4a8dSllai1 */
3482facf4a8dSllai1 int
i_ddi_reconfig()3483facf4a8dSllai1 i_ddi_reconfig()
3484facf4a8dSllai1 {
3485facf4a8dSllai1 return (devname_state & DS_RECONFIG);
3486facf4a8dSllai1 }
3487facf4a8dSllai1
3488facf4a8dSllai1 /*
3489facf4a8dSllai1 * Note system services are up, inform /dev.
3490facf4a8dSllai1 */
3491facf4a8dSllai1 void
i_ddi_set_sysavail()3492facf4a8dSllai1 i_ddi_set_sysavail()
3493facf4a8dSllai1 {
3494facf4a8dSllai1 if ((devname_state & DS_SYSAVAIL) == 0) {
3495facf4a8dSllai1 devname_state |= DS_SYSAVAIL;
3496facf4a8dSllai1 sdev_devstate_change();
3497facf4a8dSllai1 }
3498facf4a8dSllai1 }
3499facf4a8dSllai1
3500facf4a8dSllai1 /*
3501facf4a8dSllai1 * Note reconfiguration boot, inform /dev.
3502facf4a8dSllai1 */
3503facf4a8dSllai1 void
i_ddi_set_reconfig()3504facf4a8dSllai1 i_ddi_set_reconfig()
3505facf4a8dSllai1 {
3506facf4a8dSllai1 if ((devname_state & DS_RECONFIG) == 0) {
3507facf4a8dSllai1 devname_state |= DS_RECONFIG;
3508facf4a8dSllai1 sdev_devstate_change();
3509facf4a8dSllai1 }
3510facf4a8dSllai1 }
3511facf4a8dSllai1
35127c478bd9Sstevel@tonic-gate
35137c478bd9Sstevel@tonic-gate /*
35147c478bd9Sstevel@tonic-gate * device tree walking
35157c478bd9Sstevel@tonic-gate */
35167c478bd9Sstevel@tonic-gate
35177c478bd9Sstevel@tonic-gate struct walk_elem {
35187c478bd9Sstevel@tonic-gate struct walk_elem *next;
35197c478bd9Sstevel@tonic-gate dev_info_t *dip;
35207c478bd9Sstevel@tonic-gate };
35217c478bd9Sstevel@tonic-gate
35227c478bd9Sstevel@tonic-gate static void
free_list(struct walk_elem * list)35237c478bd9Sstevel@tonic-gate free_list(struct walk_elem *list)
35247c478bd9Sstevel@tonic-gate {
35257c478bd9Sstevel@tonic-gate while (list) {
35267c478bd9Sstevel@tonic-gate struct walk_elem *next = list->next;
35277c478bd9Sstevel@tonic-gate kmem_free(list, sizeof (*list));
35287c478bd9Sstevel@tonic-gate list = next;
35297c478bd9Sstevel@tonic-gate }
35307c478bd9Sstevel@tonic-gate }
35317c478bd9Sstevel@tonic-gate
35327c478bd9Sstevel@tonic-gate static void
append_node(struct walk_elem ** list,dev_info_t * dip)35337c478bd9Sstevel@tonic-gate append_node(struct walk_elem **list, dev_info_t *dip)
35347c478bd9Sstevel@tonic-gate {
35357c478bd9Sstevel@tonic-gate struct walk_elem *tail;
35367c478bd9Sstevel@tonic-gate struct walk_elem *elem = kmem_alloc(sizeof (*elem), KM_SLEEP);
35377c478bd9Sstevel@tonic-gate
35387c478bd9Sstevel@tonic-gate elem->next = NULL;
35397c478bd9Sstevel@tonic-gate elem->dip = dip;
35407c478bd9Sstevel@tonic-gate
35417c478bd9Sstevel@tonic-gate if (*list == NULL) {
35427c478bd9Sstevel@tonic-gate *list = elem;
35437c478bd9Sstevel@tonic-gate return;
35447c478bd9Sstevel@tonic-gate }
35457c478bd9Sstevel@tonic-gate
35467c478bd9Sstevel@tonic-gate tail = *list;
35477c478bd9Sstevel@tonic-gate while (tail->next)
35487c478bd9Sstevel@tonic-gate tail = tail->next;
35497c478bd9Sstevel@tonic-gate
35507c478bd9Sstevel@tonic-gate tail->next = elem;
35517c478bd9Sstevel@tonic-gate }
35527c478bd9Sstevel@tonic-gate
35537c478bd9Sstevel@tonic-gate /*
35547c478bd9Sstevel@tonic-gate * The implementation of ddi_walk_devs().
35557c478bd9Sstevel@tonic-gate */
35567c478bd9Sstevel@tonic-gate static int
walk_devs(dev_info_t * dip,int (* f)(dev_info_t *,void *),void * arg,int do_locking)35577c478bd9Sstevel@tonic-gate walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg,
35587c478bd9Sstevel@tonic-gate int do_locking)
35597c478bd9Sstevel@tonic-gate {
35607c478bd9Sstevel@tonic-gate struct walk_elem *head = NULL;
35617c478bd9Sstevel@tonic-gate
35627c478bd9Sstevel@tonic-gate /*
35637c478bd9Sstevel@tonic-gate * Do it in two passes. First pass invoke callback on each
35647c478bd9Sstevel@tonic-gate * dip on the sibling list. Second pass invoke callback on
35657c478bd9Sstevel@tonic-gate * children of each dip.
35667c478bd9Sstevel@tonic-gate */
35677c478bd9Sstevel@tonic-gate while (dip) {
35687c478bd9Sstevel@tonic-gate switch ((*f)(dip, arg)) {
35697c478bd9Sstevel@tonic-gate case DDI_WALK_TERMINATE:
35707c478bd9Sstevel@tonic-gate free_list(head);
35717c478bd9Sstevel@tonic-gate return (DDI_WALK_TERMINATE);
35727c478bd9Sstevel@tonic-gate
35737c478bd9Sstevel@tonic-gate case DDI_WALK_PRUNESIB:
35747c478bd9Sstevel@tonic-gate /* ignore sibling by setting dip to NULL */
35757c478bd9Sstevel@tonic-gate append_node(&head, dip);
35767c478bd9Sstevel@tonic-gate dip = NULL;
35777c478bd9Sstevel@tonic-gate break;
35787c478bd9Sstevel@tonic-gate
35797c478bd9Sstevel@tonic-gate case DDI_WALK_PRUNECHILD:
35807c478bd9Sstevel@tonic-gate /* don't worry about children */
35817c478bd9Sstevel@tonic-gate dip = ddi_get_next_sibling(dip);
35827c478bd9Sstevel@tonic-gate break;
35837c478bd9Sstevel@tonic-gate
35847c478bd9Sstevel@tonic-gate case DDI_WALK_CONTINUE:
35857c478bd9Sstevel@tonic-gate default:
35867c478bd9Sstevel@tonic-gate append_node(&head, dip);
35877c478bd9Sstevel@tonic-gate dip = ddi_get_next_sibling(dip);
35887c478bd9Sstevel@tonic-gate break;
35897c478bd9Sstevel@tonic-gate }
35907c478bd9Sstevel@tonic-gate
35917c478bd9Sstevel@tonic-gate }
35927c478bd9Sstevel@tonic-gate
35937c478bd9Sstevel@tonic-gate /* second pass */
35947c478bd9Sstevel@tonic-gate while (head) {
35957c478bd9Sstevel@tonic-gate struct walk_elem *next = head->next;
35967c478bd9Sstevel@tonic-gate
35977c478bd9Sstevel@tonic-gate if (do_locking)
35983fe80ca4SDan Cross ndi_devi_enter(head->dip);
35997c478bd9Sstevel@tonic-gate if (walk_devs(ddi_get_child(head->dip), f, arg, do_locking) ==
36007c478bd9Sstevel@tonic-gate DDI_WALK_TERMINATE) {
36017c478bd9Sstevel@tonic-gate if (do_locking)
36023fe80ca4SDan Cross ndi_devi_exit(head->dip);
36037c478bd9Sstevel@tonic-gate free_list(head);
36047c478bd9Sstevel@tonic-gate return (DDI_WALK_TERMINATE);
36057c478bd9Sstevel@tonic-gate }
36067c478bd9Sstevel@tonic-gate if (do_locking)
36073fe80ca4SDan Cross ndi_devi_exit(head->dip);
36087c478bd9Sstevel@tonic-gate kmem_free(head, sizeof (*head));
36097c478bd9Sstevel@tonic-gate head = next;
36107c478bd9Sstevel@tonic-gate }
36117c478bd9Sstevel@tonic-gate
36127c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE);
36137c478bd9Sstevel@tonic-gate }
36147c478bd9Sstevel@tonic-gate
36157c478bd9Sstevel@tonic-gate /*
36167c478bd9Sstevel@tonic-gate * This general-purpose routine traverses the tree of dev_info nodes,
36177c478bd9Sstevel@tonic-gate * starting from the given node, and calls the given function for each
36187c478bd9Sstevel@tonic-gate * node that it finds with the current node and the pointer arg (which
36197c478bd9Sstevel@tonic-gate * can point to a structure of information that the function
36207c478bd9Sstevel@tonic-gate * needs) as arguments.
36217c478bd9Sstevel@tonic-gate *
36227c478bd9Sstevel@tonic-gate * It does the walk a layer at a time, not depth-first. The given function
36237c478bd9Sstevel@tonic-gate * must return one of the following values:
36247c478bd9Sstevel@tonic-gate * DDI_WALK_CONTINUE
36257c478bd9Sstevel@tonic-gate * DDI_WALK_PRUNESIB
36267c478bd9Sstevel@tonic-gate * DDI_WALK_PRUNECHILD
36277c478bd9Sstevel@tonic-gate * DDI_WALK_TERMINATE
36287c478bd9Sstevel@tonic-gate *
36297c478bd9Sstevel@tonic-gate * N.B. Since we walk the sibling list, the caller must ensure that
36307c478bd9Sstevel@tonic-gate * the parent of dip is held against changes, unless the parent
36317c478bd9Sstevel@tonic-gate * is rootnode. ndi_devi_enter() on the parent is sufficient.
36327c478bd9Sstevel@tonic-gate *
36337c478bd9Sstevel@tonic-gate * To avoid deadlock situations, caller must not attempt to
36347c478bd9Sstevel@tonic-gate * configure/unconfigure/remove device node in (*f)(), nor should
36355e3986cbScth * it attempt to recurse on other nodes in the system. Any
36365e3986cbScth * ndi_devi_enter() done by (*f)() must occur 'at-or-below' the
36375e3986cbScth * node entered prior to ddi_walk_devs(). Furthermore, if (*f)()
36385e3986cbScth * does any multi-threading (in framework *or* in driver) then the
36395e3986cbScth * ndi_devi_enter() calls done by dependent threads must be
36405e3986cbScth * 'strictly-below'.
36417c478bd9Sstevel@tonic-gate *
36427c478bd9Sstevel@tonic-gate * This is not callable from device autoconfiguration routines.
36437c478bd9Sstevel@tonic-gate * They include, but not limited to, _init(9e), _fini(9e), probe(9e),
36447c478bd9Sstevel@tonic-gate * attach(9e), and detach(9e).
36457c478bd9Sstevel@tonic-gate */
36467c478bd9Sstevel@tonic-gate void
ddi_walk_devs(dev_info_t * dip,int (* f)(dev_info_t *,void *),void * arg)36477c478bd9Sstevel@tonic-gate ddi_walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg)
36487c478bd9Sstevel@tonic-gate {
36497c478bd9Sstevel@tonic-gate
36507c478bd9Sstevel@tonic-gate ASSERT(dip == NULL || ddi_get_parent(dip) == NULL ||
36517c478bd9Sstevel@tonic-gate DEVI_BUSY_OWNED(ddi_get_parent(dip)));
36527c478bd9Sstevel@tonic-gate
36537c478bd9Sstevel@tonic-gate (void) walk_devs(dip, f, arg, 1);
36547c478bd9Sstevel@tonic-gate }
36557c478bd9Sstevel@tonic-gate
36567c478bd9Sstevel@tonic-gate /*
36577c478bd9Sstevel@tonic-gate * This is a general-purpose routine traverses the per-driver list
36587c478bd9Sstevel@tonic-gate * and calls the given function for each node. must return one of
36597c478bd9Sstevel@tonic-gate * the following values:
36607c478bd9Sstevel@tonic-gate * DDI_WALK_CONTINUE
36617c478bd9Sstevel@tonic-gate * DDI_WALK_TERMINATE
36627c478bd9Sstevel@tonic-gate *
36637c478bd9Sstevel@tonic-gate * N.B. The same restrictions from ddi_walk_devs() apply.
36647c478bd9Sstevel@tonic-gate */
36657c478bd9Sstevel@tonic-gate void
e_ddi_walk_driver(char * drv,int (* f)(dev_info_t *,void *),void * arg)36667c478bd9Sstevel@tonic-gate e_ddi_walk_driver(char *drv, int (*f)(dev_info_t *, void *), void *arg)
36677c478bd9Sstevel@tonic-gate {
36687c478bd9Sstevel@tonic-gate major_t major;
36697c478bd9Sstevel@tonic-gate struct devnames *dnp;
36707c478bd9Sstevel@tonic-gate dev_info_t *dip;
36717c478bd9Sstevel@tonic-gate
36727c478bd9Sstevel@tonic-gate major = ddi_name_to_major(drv);
3673a204de77Scth if (major == DDI_MAJOR_T_NONE)
36747c478bd9Sstevel@tonic-gate return;
36757c478bd9Sstevel@tonic-gate
36767c478bd9Sstevel@tonic-gate dnp = &devnamesp[major];
36777c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock);
36787c478bd9Sstevel@tonic-gate dip = dnp->dn_head;
36797c478bd9Sstevel@tonic-gate while (dip) {
36807c478bd9Sstevel@tonic-gate ndi_hold_devi(dip);
36817c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
36827c478bd9Sstevel@tonic-gate if ((*f)(dip, arg) == DDI_WALK_TERMINATE) {
36837c478bd9Sstevel@tonic-gate ndi_rele_devi(dip);
36847c478bd9Sstevel@tonic-gate return;
36857c478bd9Sstevel@tonic-gate }
36867c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock);
36877c478bd9Sstevel@tonic-gate ndi_rele_devi(dip);
36887c478bd9Sstevel@tonic-gate dip = ddi_get_next(dip);
36897c478bd9Sstevel@tonic-gate }
36907c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
36917c478bd9Sstevel@tonic-gate }
36927c478bd9Sstevel@tonic-gate
369330c304d9SJoshua M. Clulow struct preroot_walk_block_devices_arg {
369430c304d9SJoshua M. Clulow int (*prwb_func)(const char *, void *);
369530c304d9SJoshua M. Clulow void *prwb_arg;
369630c304d9SJoshua M. Clulow };
369730c304d9SJoshua M. Clulow
369830c304d9SJoshua M. Clulow static int
preroot_walk_block_devices_walker(dev_info_t * dip,void * arg)369930c304d9SJoshua M. Clulow preroot_walk_block_devices_walker(dev_info_t *dip, void *arg)
370030c304d9SJoshua M. Clulow {
370130c304d9SJoshua M. Clulow struct preroot_walk_block_devices_arg *prwb = arg;
370230c304d9SJoshua M. Clulow
370330c304d9SJoshua M. Clulow if (i_ddi_devi_class(dip) == NULL ||
370430c304d9SJoshua M. Clulow strcmp(i_ddi_devi_class(dip), ESC_DISK) != 0) {
370530c304d9SJoshua M. Clulow /*
370630c304d9SJoshua M. Clulow * We do not think that this is a disk.
370730c304d9SJoshua M. Clulow */
370830c304d9SJoshua M. Clulow return (DDI_WALK_CONTINUE);
370930c304d9SJoshua M. Clulow }
371030c304d9SJoshua M. Clulow
371130c304d9SJoshua M. Clulow for (struct ddi_minor_data *md = DEVI(dip)->devi_minor; md != NULL;
371230c304d9SJoshua M. Clulow md = md->next) {
371330c304d9SJoshua M. Clulow if (md->ddm_spec_type != S_IFBLK) {
371430c304d9SJoshua M. Clulow /*
371530c304d9SJoshua M. Clulow * We don't want the raw version of any block device.
371630c304d9SJoshua M. Clulow */
371730c304d9SJoshua M. Clulow continue;
371830c304d9SJoshua M. Clulow }
371930c304d9SJoshua M. Clulow
372030c304d9SJoshua M. Clulow /*
372130c304d9SJoshua M. Clulow * The node type taxonomy is hierarchical, with each level
372230c304d9SJoshua M. Clulow * separated by colons. Nodes of interest are either of the
372330c304d9SJoshua M. Clulow * BLOCK type, or are prefixed with that type.
372430c304d9SJoshua M. Clulow */
372530c304d9SJoshua M. Clulow if (strcmp(md->ddm_node_type, DDI_NT_BLOCK) != 0 &&
372630c304d9SJoshua M. Clulow strncmp(md->ddm_node_type, DDI_NT_BLOCK ":",
372730c304d9SJoshua M. Clulow strlen(DDI_NT_BLOCK ":")) != 0) {
372830c304d9SJoshua M. Clulow /*
372930c304d9SJoshua M. Clulow * This minor node does not represent a block device.
373030c304d9SJoshua M. Clulow */
373130c304d9SJoshua M. Clulow continue;
373230c304d9SJoshua M. Clulow }
373330c304d9SJoshua M. Clulow
373430c304d9SJoshua M. Clulow char buf[MAXPATHLEN];
373530c304d9SJoshua M. Clulow int r;
373630c304d9SJoshua M. Clulow if ((r = prwb->prwb_func(ddi_pathname_minor(md, buf),
373730c304d9SJoshua M. Clulow prwb->prwb_arg)) == PREROOT_WALK_BLOCK_DEVICES_CANCEL) {
373830c304d9SJoshua M. Clulow /*
373930c304d9SJoshua M. Clulow * The consumer does not need any more minor nodes.
374030c304d9SJoshua M. Clulow */
374130c304d9SJoshua M. Clulow return (DDI_WALK_TERMINATE);
374230c304d9SJoshua M. Clulow }
374330c304d9SJoshua M. Clulow VERIFY3S(r, ==, PREROOT_WALK_BLOCK_DEVICES_NEXT);
374430c304d9SJoshua M. Clulow }
374530c304d9SJoshua M. Clulow
374630c304d9SJoshua M. Clulow return (DDI_WALK_CONTINUE);
374730c304d9SJoshua M. Clulow }
374830c304d9SJoshua M. Clulow
374930c304d9SJoshua M. Clulow /*
375030c304d9SJoshua M. Clulow * Private routine for ZFS when it needs to attach and scan all of the block
375130c304d9SJoshua M. Clulow * device minors in the system while looking for vdev labels.
375230c304d9SJoshua M. Clulow *
375330c304d9SJoshua M. Clulow * The callback function accepts a physical device path and the context
375430c304d9SJoshua M. Clulow * argument (arg) passed to this function; it should return
375530c304d9SJoshua M. Clulow * PREROOT_WALK_BLOCK_DEVICES_NEXT when more devices are required and
375630c304d9SJoshua M. Clulow * PREROOT_WALK_BLOCK_DEVICES_CANCEL to stop the walk.
375730c304d9SJoshua M. Clulow */
375830c304d9SJoshua M. Clulow void
preroot_walk_block_devices(int (* callback)(const char *,void *),void * arg)375930c304d9SJoshua M. Clulow preroot_walk_block_devices(int (*callback)(const char *, void *), void *arg)
376030c304d9SJoshua M. Clulow {
376130c304d9SJoshua M. Clulow /*
376230c304d9SJoshua M. Clulow * First, force everything which can attach to do so. The device class
376330c304d9SJoshua M. Clulow * is not derived until at least one minor mode is created, so we
376430c304d9SJoshua M. Clulow * cannot walk the device tree looking for a device class of ESC_DISK
376530c304d9SJoshua M. Clulow * until everything is attached.
376630c304d9SJoshua M. Clulow */
376730c304d9SJoshua M. Clulow (void) ndi_devi_config(ddi_root_node(), NDI_CONFIG | NDI_DEVI_PERSIST |
376830c304d9SJoshua M. Clulow NDI_NO_EVENT | NDI_DRV_CONF_REPROBE);
376930c304d9SJoshua M. Clulow
377030c304d9SJoshua M. Clulow struct preroot_walk_block_devices_arg prwb;
377130c304d9SJoshua M. Clulow prwb.prwb_func = callback;
377230c304d9SJoshua M. Clulow prwb.prwb_arg = arg;
377330c304d9SJoshua M. Clulow
377430c304d9SJoshua M. Clulow ddi_walk_devs(ddi_root_node(), preroot_walk_block_devices_walker,
377530c304d9SJoshua M. Clulow &prwb);
377630c304d9SJoshua M. Clulow }
377730c304d9SJoshua M. Clulow
37787c478bd9Sstevel@tonic-gate /*
37797c478bd9Sstevel@tonic-gate * argument to i_find_devi, a devinfo node search callback function.
37807c478bd9Sstevel@tonic-gate */
37817c478bd9Sstevel@tonic-gate struct match_info {
37827c478bd9Sstevel@tonic-gate dev_info_t *dip; /* result */
37837c478bd9Sstevel@tonic-gate char *nodename; /* if non-null, nodename must match */
37847c478bd9Sstevel@tonic-gate int instance; /* if != -1, instance must match */
3785737d277aScth int attached; /* if != 0, i_ddi_devi_attached() */
37867c478bd9Sstevel@tonic-gate };
37877c478bd9Sstevel@tonic-gate
37887c478bd9Sstevel@tonic-gate static int
i_find_devi(dev_info_t * dip,void * arg)37897c478bd9Sstevel@tonic-gate i_find_devi(dev_info_t *dip, void *arg)
37907c478bd9Sstevel@tonic-gate {
37917c478bd9Sstevel@tonic-gate struct match_info *info = (struct match_info *)arg;
37927c478bd9Sstevel@tonic-gate
37937c478bd9Sstevel@tonic-gate if (((info->nodename == NULL) ||
37947c478bd9Sstevel@tonic-gate (strcmp(ddi_node_name(dip), info->nodename) == 0)) &&
37957c478bd9Sstevel@tonic-gate ((info->instance == -1) ||
37967c478bd9Sstevel@tonic-gate (ddi_get_instance(dip) == info->instance)) &&
3797737d277aScth ((info->attached == 0) || i_ddi_devi_attached(dip))) {
37987c478bd9Sstevel@tonic-gate info->dip = dip;
37997c478bd9Sstevel@tonic-gate ndi_hold_devi(dip);
38007c478bd9Sstevel@tonic-gate return (DDI_WALK_TERMINATE);
38017c478bd9Sstevel@tonic-gate }
38027c478bd9Sstevel@tonic-gate
38037c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE);
38047c478bd9Sstevel@tonic-gate }
38057c478bd9Sstevel@tonic-gate
38067c478bd9Sstevel@tonic-gate /*
38077c478bd9Sstevel@tonic-gate * Find dip with a known node name and instance and return with it held
38087c478bd9Sstevel@tonic-gate */
38097c478bd9Sstevel@tonic-gate dev_info_t *
ddi_find_devinfo(char * nodename,int instance,int attached)38107c478bd9Sstevel@tonic-gate ddi_find_devinfo(char *nodename, int instance, int attached)
38117c478bd9Sstevel@tonic-gate {
38127c478bd9Sstevel@tonic-gate struct match_info info;
38137c478bd9Sstevel@tonic-gate
38147c478bd9Sstevel@tonic-gate info.nodename = nodename;
38157c478bd9Sstevel@tonic-gate info.instance = instance;
38167c478bd9Sstevel@tonic-gate info.attached = attached;
38177c478bd9Sstevel@tonic-gate info.dip = NULL;
38187c478bd9Sstevel@tonic-gate
38197c478bd9Sstevel@tonic-gate ddi_walk_devs(ddi_root_node(), i_find_devi, &info);
38207c478bd9Sstevel@tonic-gate return (info.dip);
38217c478bd9Sstevel@tonic-gate }
38227c478bd9Sstevel@tonic-gate
3823dedec472SJack Meng extern ib_boot_prop_t *iscsiboot_prop;
3824dedec472SJack Meng static void
i_ddi_parse_iscsi_name(char * name,char ** nodename,char ** addrname,char ** minorname)3825dedec472SJack Meng i_ddi_parse_iscsi_name(char *name, char **nodename, char **addrname,
3826dedec472SJack Meng char **minorname)
3827dedec472SJack Meng {
3828dedec472SJack Meng char *cp, *colon;
3829dedec472SJack Meng static char nulladdrname[] = "";
3830dedec472SJack Meng
3831dedec472SJack Meng /* default values */
3832dedec472SJack Meng if (nodename)
3833dedec472SJack Meng *nodename = name;
3834dedec472SJack Meng if (addrname)
3835dedec472SJack Meng *addrname = nulladdrname;
3836dedec472SJack Meng if (minorname)
3837dedec472SJack Meng *minorname = NULL;
3838dedec472SJack Meng
3839dedec472SJack Meng cp = colon = name;
3840dedec472SJack Meng while (*cp != '\0') {
3841dedec472SJack Meng if (addrname && *cp == '@') {
3842dedec472SJack Meng *addrname = cp + 1;
3843dedec472SJack Meng *cp = '\0';
3844dedec472SJack Meng } else if (minorname && *cp == ':') {
3845dedec472SJack Meng *minorname = cp + 1;
3846dedec472SJack Meng colon = cp;
3847dedec472SJack Meng }
3848dedec472SJack Meng ++cp;
3849dedec472SJack Meng }
3850dedec472SJack Meng if (colon != name) {
3851dedec472SJack Meng *colon = '\0';
3852dedec472SJack Meng }
3853dedec472SJack Meng }
3854dedec472SJack Meng
38557c478bd9Sstevel@tonic-gate /*
38567c478bd9Sstevel@tonic-gate * Parse for name, addr, and minor names. Some args may be NULL.
38577c478bd9Sstevel@tonic-gate */
38587c478bd9Sstevel@tonic-gate void
i_ddi_parse_name(char * name,char ** nodename,char ** addrname,char ** minorname)38597c478bd9Sstevel@tonic-gate i_ddi_parse_name(char *name, char **nodename, char **addrname, char **minorname)
38607c478bd9Sstevel@tonic-gate {
38617c478bd9Sstevel@tonic-gate char *cp;
38627c478bd9Sstevel@tonic-gate static char nulladdrname[] = "";
38637c478bd9Sstevel@tonic-gate
38647c478bd9Sstevel@tonic-gate /* default values */
38657c478bd9Sstevel@tonic-gate if (nodename)
38667c478bd9Sstevel@tonic-gate *nodename = name;
38677c478bd9Sstevel@tonic-gate if (addrname)
38687c478bd9Sstevel@tonic-gate *addrname = nulladdrname;
38697c478bd9Sstevel@tonic-gate if (minorname)
38707c478bd9Sstevel@tonic-gate *minorname = NULL;
38717c478bd9Sstevel@tonic-gate
38727c478bd9Sstevel@tonic-gate cp = name;
38737c478bd9Sstevel@tonic-gate while (*cp != '\0') {
38747c478bd9Sstevel@tonic-gate if (addrname && *cp == '@') {
38757c478bd9Sstevel@tonic-gate *addrname = cp + 1;
38767c478bd9Sstevel@tonic-gate *cp = '\0';
38777c478bd9Sstevel@tonic-gate } else if (minorname && *cp == ':') {
38787c478bd9Sstevel@tonic-gate *minorname = cp + 1;
38797c478bd9Sstevel@tonic-gate *cp = '\0';
38807c478bd9Sstevel@tonic-gate }
38817c478bd9Sstevel@tonic-gate ++cp;
38827c478bd9Sstevel@tonic-gate }
38837c478bd9Sstevel@tonic-gate }
38847c478bd9Sstevel@tonic-gate
38857c478bd9Sstevel@tonic-gate static char *
child_path_to_driver(dev_info_t * parent,char * child_name,char * unit_address)38867c478bd9Sstevel@tonic-gate child_path_to_driver(dev_info_t *parent, char *child_name, char *unit_address)
38877c478bd9Sstevel@tonic-gate {
38887c478bd9Sstevel@tonic-gate char *p, *drvname = NULL;
38897c478bd9Sstevel@tonic-gate major_t maj;
38907c478bd9Sstevel@tonic-gate
38917c478bd9Sstevel@tonic-gate /*
38927c478bd9Sstevel@tonic-gate * Construct the pathname and ask the implementation
38937c478bd9Sstevel@tonic-gate * if it can do a driver = f(pathname) for us, if not
38947c478bd9Sstevel@tonic-gate * we'll just default to using the node-name that
38957c478bd9Sstevel@tonic-gate * was given to us. We want to do this first to
38967c478bd9Sstevel@tonic-gate * allow the platform to use 'generic' names for
38977c478bd9Sstevel@tonic-gate * legacy device drivers.
38987c478bd9Sstevel@tonic-gate */
38997c478bd9Sstevel@tonic-gate p = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
39007c478bd9Sstevel@tonic-gate (void) ddi_pathname(parent, p);
39017c478bd9Sstevel@tonic-gate (void) strcat(p, "/");
39027c478bd9Sstevel@tonic-gate (void) strcat(p, child_name);
39037c478bd9Sstevel@tonic-gate if (unit_address && *unit_address) {
39047c478bd9Sstevel@tonic-gate (void) strcat(p, "@");
39057c478bd9Sstevel@tonic-gate (void) strcat(p, unit_address);
39067c478bd9Sstevel@tonic-gate }
39077c478bd9Sstevel@tonic-gate
39087c478bd9Sstevel@tonic-gate /*
39097c478bd9Sstevel@tonic-gate * Get the binding. If there is none, return the child_name
39107c478bd9Sstevel@tonic-gate * and let the caller deal with it.
39117c478bd9Sstevel@tonic-gate */
39127c478bd9Sstevel@tonic-gate maj = path_to_major(p);
39137c478bd9Sstevel@tonic-gate
39147c478bd9Sstevel@tonic-gate kmem_free(p, MAXPATHLEN);
39157c478bd9Sstevel@tonic-gate
3916a204de77Scth if (maj != DDI_MAJOR_T_NONE)
39177c478bd9Sstevel@tonic-gate drvname = ddi_major_to_name(maj);
39187c478bd9Sstevel@tonic-gate if (drvname == NULL)
39197c478bd9Sstevel@tonic-gate drvname = child_name;
39207c478bd9Sstevel@tonic-gate
39217c478bd9Sstevel@tonic-gate return (drvname);
39227c478bd9Sstevel@tonic-gate }
39237c478bd9Sstevel@tonic-gate
39247c478bd9Sstevel@tonic-gate
392520906b23SVikram Hegde #define PCI_EX_CLASS "pciexclass"
392620906b23SVikram Hegde #define PCI_EX "pciex"
392720906b23SVikram Hegde #define PCI_CLASS "pciclass"
392820906b23SVikram Hegde #define PCI "pci"
392920906b23SVikram Hegde
393020906b23SVikram Hegde int
ddi_is_pci_dip(dev_info_t * dip)393120906b23SVikram Hegde ddi_is_pci_dip(dev_info_t *dip)
393220906b23SVikram Hegde {
393320906b23SVikram Hegde char *prop = NULL;
393420906b23SVikram Hegde
393520906b23SVikram Hegde if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
393620906b23SVikram Hegde "compatible", &prop) == DDI_PROP_SUCCESS) {
393720906b23SVikram Hegde ASSERT(prop);
393820906b23SVikram Hegde if (strncmp(prop, PCI_EX_CLASS, sizeof (PCI_EX_CLASS) - 1)
393920906b23SVikram Hegde == 0 ||
394020906b23SVikram Hegde strncmp(prop, PCI_EX, sizeof (PCI_EX)- 1)
394120906b23SVikram Hegde == 0 ||
394220906b23SVikram Hegde strncmp(prop, PCI_CLASS, sizeof (PCI_CLASS) - 1)
394320906b23SVikram Hegde == 0 ||
394420906b23SVikram Hegde strncmp(prop, PCI, sizeof (PCI) - 1)
394520906b23SVikram Hegde == 0) {
394620906b23SVikram Hegde ddi_prop_free(prop);
394720906b23SVikram Hegde return (1);
394820906b23SVikram Hegde }
394920906b23SVikram Hegde }
395020906b23SVikram Hegde
395120906b23SVikram Hegde if (prop != NULL) {
395220906b23SVikram Hegde ddi_prop_free(prop);
395320906b23SVikram Hegde }
395420906b23SVikram Hegde
395520906b23SVikram Hegde return (0);
395620906b23SVikram Hegde }
395720906b23SVikram Hegde
39587c478bd9Sstevel@tonic-gate /*
39597c478bd9Sstevel@tonic-gate * Given the pathname of a device, fill in the dev_info_t value and/or the
39607c478bd9Sstevel@tonic-gate * dev_t value and/or the spectype, depending on which parameters are non-NULL.
39617c478bd9Sstevel@tonic-gate * If there is an error, this function returns -1.
39627c478bd9Sstevel@tonic-gate *
39637c478bd9Sstevel@tonic-gate * NOTE: If this function returns the dev_info_t structure, then it
39647c478bd9Sstevel@tonic-gate * does so with a hold on the devi. Caller should ensure that they get
39657c478bd9Sstevel@tonic-gate * decremented via ddi_release_devi() or ndi_rele_devi();
39667c478bd9Sstevel@tonic-gate *
39677c478bd9Sstevel@tonic-gate * This function can be invoked in the boot case for a pathname without
39687c478bd9Sstevel@tonic-gate * device argument (:xxxx), traditionally treated as a minor name.
39697c478bd9Sstevel@tonic-gate * In this case, we do the following
39707c478bd9Sstevel@tonic-gate * (1) search the minor node of type DDM_DEFAULT.
39717c478bd9Sstevel@tonic-gate * (2) if no DDM_DEFAULT minor exists, then the first non-alias minor is chosen.
39727c478bd9Sstevel@tonic-gate * (3) if neither exists, a dev_t is faked with minor number = instance.
39737c478bd9Sstevel@tonic-gate * As of S9 FCS, no instance of #1 exists. #2 is used by several platforms
39747c478bd9Sstevel@tonic-gate * to default the boot partition to :a possibly by other OBP definitions.
39757c478bd9Sstevel@tonic-gate * #3 is used for booting off network interfaces, most SPARC network
39767c478bd9Sstevel@tonic-gate * drivers support Style-2 only, so only DDM_ALIAS minor exists.
39777c478bd9Sstevel@tonic-gate *
39787c478bd9Sstevel@tonic-gate * It is possible for OBP to present device args at the end of the path as
39797c478bd9Sstevel@tonic-gate * well as in the middle. For example, with IB the following strings are
39807c478bd9Sstevel@tonic-gate * valid boot paths.
39817c478bd9Sstevel@tonic-gate * a /pci@8,700000/ib@1,2:port=1,pkey=ff,dhcp,...
39827c478bd9Sstevel@tonic-gate * b /pci@8,700000/ib@1,1:port=1/ioc@xxxxxx,yyyyyyy:dhcp
39837c478bd9Sstevel@tonic-gate * Case (a), we first look for minor node "port=1,pkey...".
39847c478bd9Sstevel@tonic-gate * Failing that, we will pass "port=1,pkey..." to the bus_config
39857c478bd9Sstevel@tonic-gate * entry point of ib (HCA) driver.
39867c478bd9Sstevel@tonic-gate * Case (b), configure ib@1,1 as usual. Then invoke ib's bus_config
39877c478bd9Sstevel@tonic-gate * with argument "ioc@xxxxxxx,yyyyyyy:port=1". After configuring
39887c478bd9Sstevel@tonic-gate * the ioc, look for minor node dhcp. If not found, pass ":dhcp"
39897c478bd9Sstevel@tonic-gate * to ioc's bus_config entry point.
39907c478bd9Sstevel@tonic-gate */
3991bde7aadfSVikram Hegde int
resolve_pathname(const char * pathname,dev_info_t ** dipp,dev_t * devtp,int * spectypep)39925f61829aSRobert Mustacchi resolve_pathname(const char *pathname, dev_info_t **dipp, dev_t *devtp,
399330c304d9SJoshua M. Clulow int *spectypep)
39947c478bd9Sstevel@tonic-gate {
39957c478bd9Sstevel@tonic-gate int error;
39967c478bd9Sstevel@tonic-gate dev_info_t *parent, *child;
39977c478bd9Sstevel@tonic-gate struct pathname pn;
39987c478bd9Sstevel@tonic-gate char *component, *config_name;
39997c478bd9Sstevel@tonic-gate char *minorname = NULL;
40007c478bd9Sstevel@tonic-gate char *prev_minor = NULL;
40017c478bd9Sstevel@tonic-gate dev_t devt = NODEV;
40027c478bd9Sstevel@tonic-gate int spectype;
40037c478bd9Sstevel@tonic-gate struct ddi_minor_data *dmn;
40047c478bd9Sstevel@tonic-gate
40057c478bd9Sstevel@tonic-gate if (*pathname != '/')
40067c478bd9Sstevel@tonic-gate return (EINVAL);
40077c478bd9Sstevel@tonic-gate parent = ddi_root_node(); /* Begin at the top of the tree */
40087c478bd9Sstevel@tonic-gate
40097c478bd9Sstevel@tonic-gate if (error = pn_get(pathname, UIO_SYSSPACE, &pn))
40107c478bd9Sstevel@tonic-gate return (error);
40117c478bd9Sstevel@tonic-gate pn_skipslash(&pn);
40127c478bd9Sstevel@tonic-gate
4013737d277aScth ASSERT(i_ddi_devi_attached(parent));
40147c478bd9Sstevel@tonic-gate ndi_hold_devi(parent);
40157c478bd9Sstevel@tonic-gate
40167c478bd9Sstevel@tonic-gate component = kmem_alloc(MAXNAMELEN, KM_SLEEP);
40177c478bd9Sstevel@tonic-gate config_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
40187c478bd9Sstevel@tonic-gate
40197c478bd9Sstevel@tonic-gate while (pn_pathleft(&pn)) {
40207c478bd9Sstevel@tonic-gate /* remember prev minor (:xxx) in the middle of path */
40217c478bd9Sstevel@tonic-gate if (minorname)
40227c478bd9Sstevel@tonic-gate prev_minor = i_ddi_strdup(minorname, KM_SLEEP);
40237c478bd9Sstevel@tonic-gate
40247c478bd9Sstevel@tonic-gate /* Get component and chop off minorname */
40257c478bd9Sstevel@tonic-gate (void) pn_getcomponent(&pn, component);
4026dedec472SJack Meng if ((iscsiboot_prop != NULL) &&
4027dedec472SJack Meng (strcmp((DEVI(parent)->devi_node_name), "iscsi") == 0)) {
4028dedec472SJack Meng i_ddi_parse_iscsi_name(component, NULL, NULL,
4029dedec472SJack Meng &minorname);
4030dedec472SJack Meng } else {
40317c478bd9Sstevel@tonic-gate i_ddi_parse_name(component, NULL, NULL, &minorname);
4032dedec472SJack Meng }
40337c478bd9Sstevel@tonic-gate if (prev_minor == NULL) {
40347c478bd9Sstevel@tonic-gate (void) snprintf(config_name, MAXNAMELEN, "%s",
40357c478bd9Sstevel@tonic-gate component);
40367c478bd9Sstevel@tonic-gate } else {
40377c478bd9Sstevel@tonic-gate (void) snprintf(config_name, MAXNAMELEN, "%s:%s",
40387c478bd9Sstevel@tonic-gate component, prev_minor);
40397c478bd9Sstevel@tonic-gate kmem_free(prev_minor, strlen(prev_minor) + 1);
40407c478bd9Sstevel@tonic-gate prev_minor = NULL;
40417c478bd9Sstevel@tonic-gate }
40427c478bd9Sstevel@tonic-gate
40437c478bd9Sstevel@tonic-gate /*
40447c478bd9Sstevel@tonic-gate * Find and configure the child
40457c478bd9Sstevel@tonic-gate */
40467c478bd9Sstevel@tonic-gate if (ndi_devi_config_one(parent, config_name, &child,
40477c478bd9Sstevel@tonic-gate NDI_PROMNAME | NDI_NO_EVENT) != NDI_SUCCESS) {
40487c478bd9Sstevel@tonic-gate ndi_rele_devi(parent);
40497c478bd9Sstevel@tonic-gate pn_free(&pn);
40507c478bd9Sstevel@tonic-gate kmem_free(component, MAXNAMELEN);
40517c478bd9Sstevel@tonic-gate kmem_free(config_name, MAXNAMELEN);
40527c478bd9Sstevel@tonic-gate return (-1);
40537c478bd9Sstevel@tonic-gate }
40547c478bd9Sstevel@tonic-gate
4055737d277aScth ASSERT(i_ddi_devi_attached(child));
40567c478bd9Sstevel@tonic-gate ndi_rele_devi(parent);
40577c478bd9Sstevel@tonic-gate parent = child;
40587c478bd9Sstevel@tonic-gate pn_skipslash(&pn);
40597c478bd9Sstevel@tonic-gate }
40607c478bd9Sstevel@tonic-gate
40617c478bd9Sstevel@tonic-gate /*
40627c478bd9Sstevel@tonic-gate * First look for a minor node matching minorname.
40637c478bd9Sstevel@tonic-gate * Failing that, try to pass minorname to bus_config().
40647c478bd9Sstevel@tonic-gate */
40657c478bd9Sstevel@tonic-gate if (minorname && i_ddi_minorname_to_devtspectype(parent,
40667c478bd9Sstevel@tonic-gate minorname, &devt, &spectype) == DDI_FAILURE) {
40677c478bd9Sstevel@tonic-gate (void) snprintf(config_name, MAXNAMELEN, "%s", minorname);
40687c478bd9Sstevel@tonic-gate if (ndi_devi_config_obp_args(parent,
40697c478bd9Sstevel@tonic-gate config_name, &child, 0) != NDI_SUCCESS) {
40707c478bd9Sstevel@tonic-gate ndi_rele_devi(parent);
40717c478bd9Sstevel@tonic-gate pn_free(&pn);
40727c478bd9Sstevel@tonic-gate kmem_free(component, MAXNAMELEN);
40737c478bd9Sstevel@tonic-gate kmem_free(config_name, MAXNAMELEN);
40747c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_NOTE,
40757c478bd9Sstevel@tonic-gate "%s: minor node not found\n", pathname));
40767c478bd9Sstevel@tonic-gate return (-1);
40777c478bd9Sstevel@tonic-gate }
40787c478bd9Sstevel@tonic-gate minorname = NULL; /* look for default minor */
4079737d277aScth ASSERT(i_ddi_devi_attached(child));
40807c478bd9Sstevel@tonic-gate ndi_rele_devi(parent);
40817c478bd9Sstevel@tonic-gate parent = child;
40827c478bd9Sstevel@tonic-gate }
40837c478bd9Sstevel@tonic-gate
40847c478bd9Sstevel@tonic-gate if (devtp || spectypep) {
40857c478bd9Sstevel@tonic-gate if (minorname == NULL) {
4086b9ccdc5aScth /*
4087b9ccdc5aScth * Search for a default entry with an active
4088b9ccdc5aScth * ndi_devi_enter to protect the devi_minor list.
4089b9ccdc5aScth */
40903fe80ca4SDan Cross ndi_devi_enter(parent);
40917c478bd9Sstevel@tonic-gate for (dmn = DEVI(parent)->devi_minor; dmn;
40927c478bd9Sstevel@tonic-gate dmn = dmn->next) {
40937c478bd9Sstevel@tonic-gate if (dmn->type == DDM_DEFAULT) {
40947c478bd9Sstevel@tonic-gate devt = dmn->ddm_dev;
40957c478bd9Sstevel@tonic-gate spectype = dmn->ddm_spec_type;
40967c478bd9Sstevel@tonic-gate break;
40977c478bd9Sstevel@tonic-gate }
40987c478bd9Sstevel@tonic-gate }
40997c478bd9Sstevel@tonic-gate
41007c478bd9Sstevel@tonic-gate if (devt == NODEV) {
41017c478bd9Sstevel@tonic-gate /*
41027c478bd9Sstevel@tonic-gate * No default minor node, try the first one;
41037c478bd9Sstevel@tonic-gate * else, assume 1-1 instance-minor mapping
41047c478bd9Sstevel@tonic-gate */
41057c478bd9Sstevel@tonic-gate dmn = DEVI(parent)->devi_minor;
41067c478bd9Sstevel@tonic-gate if (dmn && ((dmn->type == DDM_MINOR) ||
41077c478bd9Sstevel@tonic-gate (dmn->type == DDM_INTERNAL_PATH))) {
41087c478bd9Sstevel@tonic-gate devt = dmn->ddm_dev;
41097c478bd9Sstevel@tonic-gate spectype = dmn->ddm_spec_type;
41107c478bd9Sstevel@tonic-gate } else {
41117c478bd9Sstevel@tonic-gate devt = makedevice(
41127c478bd9Sstevel@tonic-gate DEVI(parent)->devi_major,
41137c478bd9Sstevel@tonic-gate ddi_get_instance(parent));
41147c478bd9Sstevel@tonic-gate spectype = S_IFCHR;
41157c478bd9Sstevel@tonic-gate }
41167c478bd9Sstevel@tonic-gate }
41173fe80ca4SDan Cross ndi_devi_exit(parent);
41187c478bd9Sstevel@tonic-gate }
41197c478bd9Sstevel@tonic-gate if (devtp)
41207c478bd9Sstevel@tonic-gate *devtp = devt;
41217c478bd9Sstevel@tonic-gate if (spectypep)
41227c478bd9Sstevel@tonic-gate *spectypep = spectype;
41237c478bd9Sstevel@tonic-gate }
41247c478bd9Sstevel@tonic-gate
41257c478bd9Sstevel@tonic-gate pn_free(&pn);
41267c478bd9Sstevel@tonic-gate kmem_free(component, MAXNAMELEN);
41277c478bd9Sstevel@tonic-gate kmem_free(config_name, MAXNAMELEN);
41287c478bd9Sstevel@tonic-gate
41297c478bd9Sstevel@tonic-gate /*
41307c478bd9Sstevel@tonic-gate * If there is no error, return the appropriate parameters
41317c478bd9Sstevel@tonic-gate */
41327c478bd9Sstevel@tonic-gate if (dipp != NULL)
41337c478bd9Sstevel@tonic-gate *dipp = parent;
4134bde7aadfSVikram Hegde else {
41357c478bd9Sstevel@tonic-gate /*
41367c478bd9Sstevel@tonic-gate * We should really keep the ref count to keep the node from
41377c478bd9Sstevel@tonic-gate * detaching but ddi_pathname_to_dev_t() specifies a NULL dipp,
41387c478bd9Sstevel@tonic-gate * so we have no way of passing back the held dip. Not holding
41397c478bd9Sstevel@tonic-gate * the dip allows detaches to occur - which can cause problems
41407c478bd9Sstevel@tonic-gate * for subsystems which call ddi_pathname_to_dev_t (console).
41417c478bd9Sstevel@tonic-gate *
41427c478bd9Sstevel@tonic-gate * Instead of holding the dip, we place a ddi-no-autodetach
41437c478bd9Sstevel@tonic-gate * property on the node to prevent auto detaching.
41447c478bd9Sstevel@tonic-gate *
41457c478bd9Sstevel@tonic-gate * The right fix is to remove ddi_pathname_to_dev_t and replace
41467c478bd9Sstevel@tonic-gate * it, and all references, with a call that specifies a dipp.
41477c478bd9Sstevel@tonic-gate * In addition, the callers of this new interfaces would then
41487c478bd9Sstevel@tonic-gate * need to call ndi_rele_devi when the reference is complete.
414920906b23SVikram Hegde *
41507c478bd9Sstevel@tonic-gate */
41517c478bd9Sstevel@tonic-gate (void) ddi_prop_update_int(DDI_DEV_T_NONE, parent,
41527c478bd9Sstevel@tonic-gate DDI_NO_AUTODETACH, 1);
41537c478bd9Sstevel@tonic-gate ndi_rele_devi(parent);
41547c478bd9Sstevel@tonic-gate }
41557c478bd9Sstevel@tonic-gate
41567c478bd9Sstevel@tonic-gate return (0);
41577c478bd9Sstevel@tonic-gate }
41587c478bd9Sstevel@tonic-gate
41597c478bd9Sstevel@tonic-gate /*
41607c478bd9Sstevel@tonic-gate * Given the pathname of a device, return the dev_t of the corresponding
41617c478bd9Sstevel@tonic-gate * device. Returns NODEV on failure.
41627c478bd9Sstevel@tonic-gate *
41637c478bd9Sstevel@tonic-gate * Note that this call sets the DDI_NO_AUTODETACH property on the devinfo node.
41647c478bd9Sstevel@tonic-gate */
41657c478bd9Sstevel@tonic-gate dev_t
ddi_pathname_to_dev_t(char * pathname)41667c478bd9Sstevel@tonic-gate ddi_pathname_to_dev_t(char *pathname)
41677c478bd9Sstevel@tonic-gate {
41687c478bd9Sstevel@tonic-gate dev_t devt;
41697c478bd9Sstevel@tonic-gate int error;
41707c478bd9Sstevel@tonic-gate
41717c478bd9Sstevel@tonic-gate error = resolve_pathname(pathname, NULL, &devt, NULL);
41727c478bd9Sstevel@tonic-gate
41737c478bd9Sstevel@tonic-gate return (error ? NODEV : devt);
41747c478bd9Sstevel@tonic-gate }
41757c478bd9Sstevel@tonic-gate
41767c478bd9Sstevel@tonic-gate /*
41777c478bd9Sstevel@tonic-gate * Translate a prom pathname to kernel devfs pathname.
41787c478bd9Sstevel@tonic-gate * Caller is assumed to allocate devfspath memory of
41797c478bd9Sstevel@tonic-gate * size at least MAXPATHLEN
41807c478bd9Sstevel@tonic-gate *
41817c478bd9Sstevel@tonic-gate * The prom pathname may not include minor name, but
41827c478bd9Sstevel@tonic-gate * devfs pathname has a minor name portion.
41837c478bd9Sstevel@tonic-gate */
41847c478bd9Sstevel@tonic-gate int
i_ddi_prompath_to_devfspath(char * prompath,char * devfspath)41857c478bd9Sstevel@tonic-gate i_ddi_prompath_to_devfspath(char *prompath, char *devfspath)
41867c478bd9Sstevel@tonic-gate {
41877c478bd9Sstevel@tonic-gate dev_t devt = (dev_t)NODEV;
41887c478bd9Sstevel@tonic-gate dev_info_t *dip = NULL;
41897c478bd9Sstevel@tonic-gate char *minor_name = NULL;
41907c478bd9Sstevel@tonic-gate int spectype;
41917c478bd9Sstevel@tonic-gate int error;
41927c478bd9Sstevel@tonic-gate
41937c478bd9Sstevel@tonic-gate error = resolve_pathname(prompath, &dip, &devt, &spectype);
41947c478bd9Sstevel@tonic-gate if (error)
41957c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
41967c478bd9Sstevel@tonic-gate ASSERT(dip && devt != NODEV);
41977c478bd9Sstevel@tonic-gate
41987c478bd9Sstevel@tonic-gate /*
41997c478bd9Sstevel@tonic-gate * Get in-kernel devfs pathname
42007c478bd9Sstevel@tonic-gate */
42017c478bd9Sstevel@tonic-gate (void) ddi_pathname(dip, devfspath);
42027c478bd9Sstevel@tonic-gate
42033fe80ca4SDan Cross ndi_devi_enter(dip);
42047c478bd9Sstevel@tonic-gate minor_name = i_ddi_devtspectype_to_minorname(dip, devt, spectype);
42057c478bd9Sstevel@tonic-gate if (minor_name) {
42067c478bd9Sstevel@tonic-gate (void) strcat(devfspath, ":");
42077c478bd9Sstevel@tonic-gate (void) strcat(devfspath, minor_name);
42087c478bd9Sstevel@tonic-gate } else {
42097c478bd9Sstevel@tonic-gate /*
42107c478bd9Sstevel@tonic-gate * If minor_name is NULL, we have an alias minor node.
42117c478bd9Sstevel@tonic-gate * So manufacture a path to the corresponding clone minor.
42127c478bd9Sstevel@tonic-gate */
42137c478bd9Sstevel@tonic-gate (void) snprintf(devfspath, MAXPATHLEN, "%s:%s",
42147c478bd9Sstevel@tonic-gate CLONE_PATH, ddi_driver_name(dip));
42157c478bd9Sstevel@tonic-gate }
42163fe80ca4SDan Cross ndi_devi_exit(dip);
42177c478bd9Sstevel@tonic-gate
42187c478bd9Sstevel@tonic-gate /* release hold from resolve_pathname() */
42197c478bd9Sstevel@tonic-gate ndi_rele_devi(dip);
42207c478bd9Sstevel@tonic-gate return (0);
42217c478bd9Sstevel@tonic-gate }
42227c478bd9Sstevel@tonic-gate
42237c478bd9Sstevel@tonic-gate /*
422419397407SSherry Moore * This function is intended to identify drivers that must quiesce for fast
422519397407SSherry Moore * reboot to succeed. It does not claim to have more knowledge about the device
422619397407SSherry Moore * than its driver. If a driver has implemented quiesce(), it will be invoked;
422719397407SSherry Moore * if a so identified driver does not manage any device that needs to be
422819397407SSherry Moore * quiesced, it must explicitly set its devo_quiesce dev_op to
422919397407SSherry Moore * ddi_quiesce_not_needed.
423019397407SSherry Moore */
423119397407SSherry Moore static int skip_pseudo = 1; /* Skip pseudo devices */
423219397407SSherry Moore static int skip_non_hw = 1; /* Skip devices with no hardware property */
423319397407SSherry Moore static int
should_implement_quiesce(dev_info_t * dip)423419397407SSherry Moore should_implement_quiesce(dev_info_t *dip)
423519397407SSherry Moore {
423619397407SSherry Moore struct dev_info *devi = DEVI(dip);
423719397407SSherry Moore dev_info_t *pdip;
423819397407SSherry Moore
423919397407SSherry Moore /*
424019397407SSherry Moore * If dip is pseudo and skip_pseudo is set, driver doesn't have to
424119397407SSherry Moore * implement quiesce().
424219397407SSherry Moore */
424319397407SSherry Moore if (skip_pseudo &&
424419397407SSherry Moore strncmp(ddi_binding_name(dip), "pseudo", sizeof ("pseudo")) == 0)
424519397407SSherry Moore return (0);
424619397407SSherry Moore
424719397407SSherry Moore /*
424819397407SSherry Moore * If parent dip is pseudo and skip_pseudo is set, driver doesn't have
424919397407SSherry Moore * to implement quiesce().
425019397407SSherry Moore */
425119397407SSherry Moore if (skip_pseudo && (pdip = ddi_get_parent(dip)) != NULL &&
425219397407SSherry Moore strncmp(ddi_binding_name(pdip), "pseudo", sizeof ("pseudo")) == 0)
425319397407SSherry Moore return (0);
425419397407SSherry Moore
425519397407SSherry Moore /*
425619397407SSherry Moore * If not attached, driver doesn't have to implement quiesce().
425719397407SSherry Moore */
425819397407SSherry Moore if (!i_ddi_devi_attached(dip))
425919397407SSherry Moore return (0);
426019397407SSherry Moore
426119397407SSherry Moore /*
426219397407SSherry Moore * If dip has no hardware property and skip_non_hw is set,
426319397407SSherry Moore * driver doesn't have to implement quiesce().
426419397407SSherry Moore */
426519397407SSherry Moore if (skip_non_hw && devi->devi_hw_prop_ptr == NULL)
426619397407SSherry Moore return (0);
426719397407SSherry Moore
426819397407SSherry Moore return (1);
426919397407SSherry Moore }
427019397407SSherry Moore
427119397407SSherry Moore static int
driver_has_quiesce(struct dev_ops * ops)427219397407SSherry Moore driver_has_quiesce(struct dev_ops *ops)
427319397407SSherry Moore {
427419397407SSherry Moore if ((ops->devo_rev >= 4) && (ops->devo_quiesce != nodev) &&
427519397407SSherry Moore (ops->devo_quiesce != NULL) && (ops->devo_quiesce != nulldev) &&
427619397407SSherry Moore (ops->devo_quiesce != ddi_quiesce_not_supported))
427719397407SSherry Moore return (1);
427819397407SSherry Moore else
427919397407SSherry Moore return (0);
428019397407SSherry Moore }
428119397407SSherry Moore
428219397407SSherry Moore /*
428319397407SSherry Moore * Check to see if a driver has implemented the quiesce() DDI function.
428419397407SSherry Moore */
428519397407SSherry Moore int
check_driver_quiesce(dev_info_t * dip,void * arg)428619397407SSherry Moore check_driver_quiesce(dev_info_t *dip, void *arg)
428719397407SSherry Moore {
428819397407SSherry Moore struct dev_ops *ops;
428919397407SSherry Moore
429019397407SSherry Moore if (!should_implement_quiesce(dip))
429119397407SSherry Moore return (DDI_WALK_CONTINUE);
429219397407SSherry Moore
429319397407SSherry Moore if ((ops = ddi_get_driver(dip)) == NULL)
429419397407SSherry Moore return (DDI_WALK_CONTINUE);
429519397407SSherry Moore
429619397407SSherry Moore if (driver_has_quiesce(ops)) {
429719397407SSherry Moore if ((quiesce_debug & 0x2) == 0x2) {
429819397407SSherry Moore if (ops->devo_quiesce == ddi_quiesce_not_needed)
429919397407SSherry Moore cmn_err(CE_CONT, "%s does not need to be "
430019397407SSherry Moore "quiesced", ddi_driver_name(dip));
430119397407SSherry Moore else
430219397407SSherry Moore cmn_err(CE_CONT, "%s has quiesce routine",
430319397407SSherry Moore ddi_driver_name(dip));
430419397407SSherry Moore }
430519397407SSherry Moore } else {
430619397407SSherry Moore if (arg != NULL)
430719397407SSherry Moore *((int *)arg) = -1;
430819397407SSherry Moore cmn_err(CE_WARN, "%s has no quiesce()", ddi_driver_name(dip));
430919397407SSherry Moore }
431019397407SSherry Moore
431119397407SSherry Moore return (DDI_WALK_CONTINUE);
431219397407SSherry Moore }
431319397407SSherry Moore
431419397407SSherry Moore /*
431519397407SSherry Moore * Quiesce device.
431619397407SSherry Moore */
431719397407SSherry Moore static void
quiesce_one_device(dev_info_t * dip,void * arg)431819397407SSherry Moore quiesce_one_device(dev_info_t *dip, void *arg)
431919397407SSherry Moore {
432019397407SSherry Moore struct dev_ops *ops;
432119397407SSherry Moore int should_quiesce = 0;
432219397407SSherry Moore
432319397407SSherry Moore /*
432419397407SSherry Moore * If the device is not attached it doesn't need to be quiesced.
432519397407SSherry Moore */
432619397407SSherry Moore if (!i_ddi_devi_attached(dip))
432719397407SSherry Moore return;
432819397407SSherry Moore
432919397407SSherry Moore if ((ops = ddi_get_driver(dip)) == NULL)
433019397407SSherry Moore return;
433119397407SSherry Moore
433219397407SSherry Moore should_quiesce = should_implement_quiesce(dip);
433319397407SSherry Moore
433419397407SSherry Moore /*
433519397407SSherry Moore * If there's an implementation of quiesce(), always call it even if
433619397407SSherry Moore * some of the drivers don't have quiesce() or quiesce() have failed
433719397407SSherry Moore * so we can do force fast reboot. The implementation of quiesce()
433819397407SSherry Moore * should not negatively affect a regular reboot.
433919397407SSherry Moore */
434019397407SSherry Moore if (driver_has_quiesce(ops)) {
434119397407SSherry Moore int rc = DDI_SUCCESS;
434219397407SSherry Moore
434319397407SSherry Moore if (ops->devo_quiesce == ddi_quiesce_not_needed)
434419397407SSherry Moore return;
434519397407SSherry Moore
434619397407SSherry Moore rc = devi_quiesce(dip);
434719397407SSherry Moore
434819397407SSherry Moore if (rc != DDI_SUCCESS && should_quiesce) {
43491c79aca5SAlexander Eremin #ifdef DEBUG
43501c79aca5SAlexander Eremin cmn_err(CE_WARN, "quiesce() failed for %s%d",
43511c79aca5SAlexander Eremin ddi_driver_name(dip), ddi_get_instance(dip));
43521c79aca5SAlexander Eremin #endif /* DEBUG */
435319397407SSherry Moore if (arg != NULL)
435419397407SSherry Moore *((int *)arg) = -1;
435519397407SSherry Moore }
435619397407SSherry Moore } else if (should_quiesce && arg != NULL) {
435719397407SSherry Moore *((int *)arg) = -1;
435819397407SSherry Moore }
435919397407SSherry Moore }
436019397407SSherry Moore
436119397407SSherry Moore /*
436219397407SSherry Moore * Traverse the dev info tree in a breadth-first manner so that we quiesce
436319397407SSherry Moore * children first. All subtrees under the parent of dip will be quiesced.
436419397407SSherry Moore */
436519397407SSherry Moore void
quiesce_devices(dev_info_t * dip,void * arg)436619397407SSherry Moore quiesce_devices(dev_info_t *dip, void *arg)
436719397407SSherry Moore {
436819397407SSherry Moore /*
436919397407SSherry Moore * if we're reached here, the device tree better not be changing.
4370d27aea3fSDan Cross * so either devinfo_freeze better be set or we better be panicking.
437119397407SSherry Moore */
437219397407SSherry Moore ASSERT(devinfo_freeze || panicstr);
437319397407SSherry Moore
437419397407SSherry Moore for (; dip != NULL; dip = ddi_get_next_sibling(dip)) {
437519397407SSherry Moore quiesce_devices(ddi_get_child(dip), arg);
437619397407SSherry Moore
437719397407SSherry Moore quiesce_one_device(dip, arg);
437819397407SSherry Moore }
437919397407SSherry Moore }
438019397407SSherry Moore
438119397407SSherry Moore /*
43827c478bd9Sstevel@tonic-gate * Reset all the pure leaf drivers on the system at halt time
43837c478bd9Sstevel@tonic-gate */
43847c478bd9Sstevel@tonic-gate static int
reset_leaf_device(dev_info_t * dip,void * arg)43857c478bd9Sstevel@tonic-gate reset_leaf_device(dev_info_t *dip, void *arg)
43867c478bd9Sstevel@tonic-gate {
43877c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(arg))
43887c478bd9Sstevel@tonic-gate struct dev_ops *ops;
43897c478bd9Sstevel@tonic-gate
43907c478bd9Sstevel@tonic-gate /* if the device doesn't need to be reset then there's nothing to do */
43917c478bd9Sstevel@tonic-gate if (!DEVI_NEED_RESET(dip))
43927c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE);
43937c478bd9Sstevel@tonic-gate
43947c478bd9Sstevel@tonic-gate /*
43957c478bd9Sstevel@tonic-gate * if the device isn't a char/block device or doesn't have a
43967c478bd9Sstevel@tonic-gate * reset entry point then there's nothing to do.
43977c478bd9Sstevel@tonic-gate */
43987c478bd9Sstevel@tonic-gate ops = ddi_get_driver(dip);
43997c478bd9Sstevel@tonic-gate if ((ops == NULL) || (ops->devo_cb_ops == NULL) ||
44007c478bd9Sstevel@tonic-gate (ops->devo_reset == nodev) || (ops->devo_reset == nulldev) ||
44017c478bd9Sstevel@tonic-gate (ops->devo_reset == NULL))
44027c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE);
44037c478bd9Sstevel@tonic-gate
44047c478bd9Sstevel@tonic-gate if (DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip)) {
44057c478bd9Sstevel@tonic-gate static char path[MAXPATHLEN];
44067c478bd9Sstevel@tonic-gate
44077c478bd9Sstevel@tonic-gate /*
44087c478bd9Sstevel@tonic-gate * bad news, this device has blocked in it's attach or
44097c478bd9Sstevel@tonic-gate * detach routine, which means it not safe to call it's
44107c478bd9Sstevel@tonic-gate * devo_reset() entry point.
44117c478bd9Sstevel@tonic-gate */
44127c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "unable to reset device: %s",
44137c478bd9Sstevel@tonic-gate ddi_pathname(dip, path));
44147c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE);
44157c478bd9Sstevel@tonic-gate }
44167c478bd9Sstevel@tonic-gate
44177c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_NOTE, "resetting %s%d\n",
44187c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip)));
44197c478bd9Sstevel@tonic-gate
44207c478bd9Sstevel@tonic-gate (void) devi_reset(dip, DDI_RESET_FORCE);
44217c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE);
44227c478bd9Sstevel@tonic-gate }
44237c478bd9Sstevel@tonic-gate
44247c478bd9Sstevel@tonic-gate void
reset_leaves(void)44257c478bd9Sstevel@tonic-gate reset_leaves(void)
44267c478bd9Sstevel@tonic-gate {
44277c478bd9Sstevel@tonic-gate /*
44287c478bd9Sstevel@tonic-gate * if we're reached here, the device tree better not be changing.
4429d27aea3fSDan Cross * so either devinfo_freeze better be set or we better be panicking.
44307c478bd9Sstevel@tonic-gate */
44317c478bd9Sstevel@tonic-gate ASSERT(devinfo_freeze || panicstr);
44327c478bd9Sstevel@tonic-gate
44337c478bd9Sstevel@tonic-gate (void) walk_devs(top_devinfo, reset_leaf_device, NULL, 0);
44347c478bd9Sstevel@tonic-gate }
44357c478bd9Sstevel@tonic-gate
443619397407SSherry Moore
44377c478bd9Sstevel@tonic-gate /*
443819397407SSherry Moore * devtree_freeze() must be called before quiesce_devices() and reset_leaves()
443919397407SSherry Moore * during a normal system shutdown. It attempts to ensure that there are no
444019397407SSherry Moore * outstanding attach or detach operations in progress when quiesce_devices() or
444119397407SSherry Moore * reset_leaves()is invoked. It must be called before the system becomes
444219397407SSherry Moore * single-threaded because device attach and detach are multi-threaded
444319397407SSherry Moore * operations. (note that during system shutdown the system doesn't actually
444419397407SSherry Moore * become single-thread since other threads still exist, but the shutdown thread
444519397407SSherry Moore * will disable preemption for itself, raise it's pil, and stop all the other
444619397407SSherry Moore * cpus in the system there by effectively making the system single-threaded.)
44477c478bd9Sstevel@tonic-gate */
44487c478bd9Sstevel@tonic-gate void
devtree_freeze(void)44497c478bd9Sstevel@tonic-gate devtree_freeze(void)
44507c478bd9Sstevel@tonic-gate {
44517c478bd9Sstevel@tonic-gate int delayed = 0;
44527c478bd9Sstevel@tonic-gate
4453d27aea3fSDan Cross /* if we're panicking then the device tree isn't going to be changing */
44547c478bd9Sstevel@tonic-gate if (panicstr)
44557c478bd9Sstevel@tonic-gate return;
44567c478bd9Sstevel@tonic-gate
44577c478bd9Sstevel@tonic-gate /* stop all dev_info state changes in the device tree */
44587c478bd9Sstevel@tonic-gate devinfo_freeze = gethrtime();
44597c478bd9Sstevel@tonic-gate
44607c478bd9Sstevel@tonic-gate /*
4461d27aea3fSDan Cross * if we're not panicking and there are on-going attach or detach
44627c478bd9Sstevel@tonic-gate * operations, wait for up to 3 seconds for them to finish. This
44637c478bd9Sstevel@tonic-gate * is a randomly chosen interval but this should be ok because:
44647c478bd9Sstevel@tonic-gate * - 3 seconds is very small relative to the deadman timer.
44657c478bd9Sstevel@tonic-gate * - normal attach and detach operations should be very quick.
44667c478bd9Sstevel@tonic-gate * - attach and detach operations are fairly rare.
44677c478bd9Sstevel@tonic-gate */
44687c478bd9Sstevel@tonic-gate while (!panicstr && atomic_add_long_nv(&devinfo_attach_detach, 0) &&
44697c478bd9Sstevel@tonic-gate (delayed < 3)) {
44707c478bd9Sstevel@tonic-gate delayed += 1;
44717c478bd9Sstevel@tonic-gate
44727c478bd9Sstevel@tonic-gate /* do a sleeping wait for one second */
44737c478bd9Sstevel@tonic-gate ASSERT(!servicing_interrupt());
44747c478bd9Sstevel@tonic-gate delay(drv_usectohz(MICROSEC));
44757c478bd9Sstevel@tonic-gate }
44767c478bd9Sstevel@tonic-gate }
44777c478bd9Sstevel@tonic-gate
44787c478bd9Sstevel@tonic-gate static int
bind_dip(dev_info_t * dip,void * arg)44797c478bd9Sstevel@tonic-gate bind_dip(dev_info_t *dip, void *arg)
44807c478bd9Sstevel@tonic-gate {
44817c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(arg))
4482f4da9be0Scth char *path;
4483f4da9be0Scth major_t major, pmajor;
4484f4da9be0Scth
4485f4da9be0Scth /*
4486f4da9be0Scth * If the node is currently bound to the wrong driver, try to unbind
4487f4da9be0Scth * so that we can rebind to the correct driver.
4488f4da9be0Scth */
4489f4da9be0Scth if (i_ddi_node_state(dip) >= DS_BOUND) {
4490f4da9be0Scth major = ddi_compatible_driver_major(dip, NULL);
4491f4da9be0Scth if ((DEVI(dip)->devi_major == major) &&
4492f4da9be0Scth (i_ddi_node_state(dip) >= DS_INITIALIZED)) {
4493f4da9be0Scth /*
4494f4da9be0Scth * Check for a path-oriented driver alias that
4495f4da9be0Scth * takes precedence over current driver binding.
4496f4da9be0Scth */
4497f4da9be0Scth path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4498f4da9be0Scth (void) ddi_pathname(dip, path);
4499f4da9be0Scth pmajor = ddi_name_to_major(path);
4500c8742f64SJerry Gilliam if (driver_active(pmajor))
4501f4da9be0Scth major = pmajor;
4502f4da9be0Scth kmem_free(path, MAXPATHLEN);
4503f4da9be0Scth }
4504f4da9be0Scth
4505f4da9be0Scth /* attempt unbind if current driver is incorrect */
4506c8742f64SJerry Gilliam if (driver_active(major) &&
4507f4da9be0Scth (major != DEVI(dip)->devi_major))
4508f4da9be0Scth (void) ndi_devi_unbind_driver(dip);
4509f4da9be0Scth }
4510f4da9be0Scth
4511f4da9be0Scth /* If unbound, try to bind to a driver */
45127c478bd9Sstevel@tonic-gate if (i_ddi_node_state(dip) < DS_BOUND)
45137c478bd9Sstevel@tonic-gate (void) ndi_devi_bind_driver(dip, 0);
45147c478bd9Sstevel@tonic-gate
45157c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE);
45167c478bd9Sstevel@tonic-gate }
45177c478bd9Sstevel@tonic-gate
45187c478bd9Sstevel@tonic-gate void
i_ddi_bind_devs(void)45197c478bd9Sstevel@tonic-gate i_ddi_bind_devs(void)
45207c478bd9Sstevel@tonic-gate {
4521f4da9be0Scth /* flush devfs so that ndi_devi_unbind_driver will work when possible */
4522f4da9be0Scth (void) devfs_clean(top_devinfo, NULL, 0);
4523f4da9be0Scth
45247c478bd9Sstevel@tonic-gate ddi_walk_devs(top_devinfo, bind_dip, (void *)NULL);
45257c478bd9Sstevel@tonic-gate }
45267c478bd9Sstevel@tonic-gate
45276532b960SJerry Gilliam /* callback data for unbind_children_by_alias() */
45286532b960SJerry Gilliam typedef struct unbind_data {
45296532b960SJerry Gilliam major_t drv_major;
45306532b960SJerry Gilliam char *drv_alias;
45316532b960SJerry Gilliam int ndevs_bound;
45326532b960SJerry Gilliam int unbind_errors;
45336532b960SJerry Gilliam } unbind_data_t;
45346532b960SJerry Gilliam
45356532b960SJerry Gilliam /*
45366532b960SJerry Gilliam * A utility function provided for testing and support convenience
45376532b960SJerry Gilliam * Called for each device during an upgrade_drv -d bound to the alias
45386532b960SJerry Gilliam * that cannot be unbound due to device in use.
45396532b960SJerry Gilliam */
45406532b960SJerry Gilliam static void
unbind_alias_dev_in_use(dev_info_t * dip,char * alias)45416532b960SJerry Gilliam unbind_alias_dev_in_use(dev_info_t *dip, char *alias)
45426532b960SJerry Gilliam {
45436532b960SJerry Gilliam if (moddebug & MODDEBUG_BINDING) {
45446532b960SJerry Gilliam cmn_err(CE_CONT, "%s%d: state %d: bound to %s\n",
45456532b960SJerry Gilliam ddi_driver_name(dip), ddi_get_instance(dip),
45466532b960SJerry Gilliam i_ddi_node_state(dip), alias);
45476532b960SJerry Gilliam }
45486532b960SJerry Gilliam }
45496532b960SJerry Gilliam
45506532b960SJerry Gilliam /*
45516532b960SJerry Gilliam * walkdevs callback for unbind devices bound to specific driver
45526532b960SJerry Gilliam * and alias. Invoked within the context of update_drv -d <alias>.
45536532b960SJerry Gilliam */
45547c478bd9Sstevel@tonic-gate static int
unbind_children_by_alias(dev_info_t * dip,void * arg)45556532b960SJerry Gilliam unbind_children_by_alias(dev_info_t *dip, void *arg)
45567c478bd9Sstevel@tonic-gate {
45577c478bd9Sstevel@tonic-gate dev_info_t *cdip;
45586532b960SJerry Gilliam dev_info_t *next;
45596532b960SJerry Gilliam unbind_data_t *ub = (unbind_data_t *)(uintptr_t)arg;
45606532b960SJerry Gilliam int rv;
45617c478bd9Sstevel@tonic-gate
45627c478bd9Sstevel@tonic-gate /*
45636532b960SJerry Gilliam * We are called from update_drv to try to unbind a specific
45646532b960SJerry Gilliam * set of aliases for a driver. Unbind what persistent nodes
45656532b960SJerry Gilliam * we can, and return the number of nodes which cannot be unbound.
45666532b960SJerry Gilliam * If not all nodes can be unbound, update_drv leaves the
45676532b960SJerry Gilliam * state of the driver binding files unchanged, except in
45686532b960SJerry Gilliam * the case of -f.
45697c478bd9Sstevel@tonic-gate */
45703fe80ca4SDan Cross ndi_devi_enter(dip);
45716532b960SJerry Gilliam for (cdip = ddi_get_child(dip); cdip; cdip = next) {
45726532b960SJerry Gilliam next = ddi_get_next_sibling(cdip);
45736532b960SJerry Gilliam if ((ddi_driver_major(cdip) != ub->drv_major) ||
45746532b960SJerry Gilliam (strcmp(DEVI(cdip)->devi_node_name, ub->drv_alias) != 0))
45756532b960SJerry Gilliam continue;
45766532b960SJerry Gilliam if (i_ddi_node_state(cdip) >= DS_BOUND) {
45776532b960SJerry Gilliam rv = ndi_devi_unbind_driver(cdip);
45786532b960SJerry Gilliam if (rv != DDI_SUCCESS ||
45796532b960SJerry Gilliam (i_ddi_node_state(cdip) >= DS_BOUND)) {
45806532b960SJerry Gilliam unbind_alias_dev_in_use(cdip, ub->drv_alias);
45816532b960SJerry Gilliam ub->ndevs_bound++;
45827c478bd9Sstevel@tonic-gate continue;
45837c478bd9Sstevel@tonic-gate }
45847c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(cdip) == 0)
45857c478bd9Sstevel@tonic-gate (void) ddi_remove_child(cdip, 0);
45866532b960SJerry Gilliam }
45877c478bd9Sstevel@tonic-gate }
45883fe80ca4SDan Cross ndi_devi_exit(dip);
45897c478bd9Sstevel@tonic-gate
45907c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE);
45917c478bd9Sstevel@tonic-gate }
45927c478bd9Sstevel@tonic-gate
45936532b960SJerry Gilliam /*
45946532b960SJerry Gilliam * Unbind devices by driver & alias
45956532b960SJerry Gilliam * Context: update_drv [-f] -d -i <alias> <driver>
45966532b960SJerry Gilliam */
45976532b960SJerry Gilliam int
i_ddi_unbind_devs_by_alias(major_t major,char * alias)45986532b960SJerry Gilliam i_ddi_unbind_devs_by_alias(major_t major, char *alias)
45996532b960SJerry Gilliam {
46006532b960SJerry Gilliam unbind_data_t *ub;
46016532b960SJerry Gilliam int rv;
46026532b960SJerry Gilliam
46036532b960SJerry Gilliam ub = kmem_zalloc(sizeof (*ub), KM_SLEEP);
46046532b960SJerry Gilliam ub->drv_major = major;
46056532b960SJerry Gilliam ub->drv_alias = alias;
46066532b960SJerry Gilliam ub->ndevs_bound = 0;
46076532b960SJerry Gilliam ub->unbind_errors = 0;
46086532b960SJerry Gilliam
46096532b960SJerry Gilliam /* flush devfs so that ndi_devi_unbind_driver will work when possible */
461000931d94SJerry Gilliam (void) devfs_clean(top_devinfo, NULL, 0);
46116532b960SJerry Gilliam ddi_walk_devs(top_devinfo, unbind_children_by_alias,
46126532b960SJerry Gilliam (void *)(uintptr_t)ub);
46136532b960SJerry Gilliam
46146532b960SJerry Gilliam /* return the number of devices remaining bound to the alias */
46156532b960SJerry Gilliam rv = ub->ndevs_bound + ub->unbind_errors;
46166532b960SJerry Gilliam kmem_free(ub, sizeof (*ub));
46176532b960SJerry Gilliam return (rv);
46186532b960SJerry Gilliam }
46196532b960SJerry Gilliam
46206532b960SJerry Gilliam /*
46216532b960SJerry Gilliam * walkdevs callback for unbind devices by driver
46226532b960SJerry Gilliam */
46236532b960SJerry Gilliam static int
unbind_children_by_driver(dev_info_t * dip,void * arg)46246532b960SJerry Gilliam unbind_children_by_driver(dev_info_t *dip, void *arg)
46256532b960SJerry Gilliam {
46266532b960SJerry Gilliam dev_info_t *cdip;
46276532b960SJerry Gilliam dev_info_t *next;
46286532b960SJerry Gilliam major_t major = (major_t)(uintptr_t)arg;
46296532b960SJerry Gilliam int rv;
46306532b960SJerry Gilliam
46316532b960SJerry Gilliam /*
46326532b960SJerry Gilliam * We are called either from rem_drv or update_drv when reloading
46336532b960SJerry Gilliam * a driver.conf file. In either case, we unbind persistent nodes
46346532b960SJerry Gilliam * and destroy .conf nodes. In the case of rem_drv, this will be
46356532b960SJerry Gilliam * the final state. In the case of update_drv, i_ddi_bind_devs()
46366532b960SJerry Gilliam * may be invoked later to re-enumerate (new) driver.conf rebind
46376532b960SJerry Gilliam * persistent nodes.
46386532b960SJerry Gilliam */
46393fe80ca4SDan Cross ndi_devi_enter(dip);
46406532b960SJerry Gilliam for (cdip = ddi_get_child(dip); cdip; cdip = next) {
46416532b960SJerry Gilliam next = ddi_get_next_sibling(cdip);
46426532b960SJerry Gilliam if (ddi_driver_major(cdip) != major)
46436532b960SJerry Gilliam continue;
46446532b960SJerry Gilliam if (i_ddi_node_state(cdip) >= DS_BOUND) {
46456532b960SJerry Gilliam rv = ndi_devi_unbind_driver(cdip);
46466532b960SJerry Gilliam if (rv == DDI_FAILURE ||
46476532b960SJerry Gilliam (i_ddi_node_state(cdip) >= DS_BOUND))
46486532b960SJerry Gilliam continue;
46496532b960SJerry Gilliam if (ndi_dev_is_persistent_node(cdip) == 0)
46506532b960SJerry Gilliam (void) ddi_remove_child(cdip, 0);
46516532b960SJerry Gilliam }
46526532b960SJerry Gilliam }
46533fe80ca4SDan Cross ndi_devi_exit(dip);
46546532b960SJerry Gilliam
46556532b960SJerry Gilliam return (DDI_WALK_CONTINUE);
46566532b960SJerry Gilliam }
46576532b960SJerry Gilliam
46586532b960SJerry Gilliam /*
46596532b960SJerry Gilliam * Unbind devices by driver
46606532b960SJerry Gilliam * Context: rem_drv or unload driver.conf
46616532b960SJerry Gilliam */
46627c478bd9Sstevel@tonic-gate void
i_ddi_unbind_devs(major_t major)46637c478bd9Sstevel@tonic-gate i_ddi_unbind_devs(major_t major)
46647c478bd9Sstevel@tonic-gate {
46656532b960SJerry Gilliam /* flush devfs so that ndi_devi_unbind_driver will work when possible */
466600931d94SJerry Gilliam (void) devfs_clean(top_devinfo, NULL, 0);
46676532b960SJerry Gilliam ddi_walk_devs(top_devinfo, unbind_children_by_driver,
46686532b960SJerry Gilliam (void *)(uintptr_t)major);
46697c478bd9Sstevel@tonic-gate }
46707c478bd9Sstevel@tonic-gate
46717c478bd9Sstevel@tonic-gate /*
46727c478bd9Sstevel@tonic-gate * I/O Hotplug control
46737c478bd9Sstevel@tonic-gate */
46747c478bd9Sstevel@tonic-gate
46757c478bd9Sstevel@tonic-gate /*
46767c478bd9Sstevel@tonic-gate * create and attach a dev_info node from a .conf file spec
46777c478bd9Sstevel@tonic-gate */
46787c478bd9Sstevel@tonic-gate static void
init_spec_child(dev_info_t * pdip,struct hwc_spec * specp,uint_t flags)46797c478bd9Sstevel@tonic-gate init_spec_child(dev_info_t *pdip, struct hwc_spec *specp, uint_t flags)
46807c478bd9Sstevel@tonic-gate {
46817c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(flags))
46827c478bd9Sstevel@tonic-gate dev_info_t *dip;
46837c478bd9Sstevel@tonic-gate char *node_name;
46847c478bd9Sstevel@tonic-gate
46857c478bd9Sstevel@tonic-gate if (((node_name = specp->hwc_devi_name) == NULL) ||
4686a204de77Scth (ddi_name_to_major(node_name) == DDI_MAJOR_T_NONE)) {
46877c478bd9Sstevel@tonic-gate char *tmp = node_name;
46887c478bd9Sstevel@tonic-gate if (tmp == NULL)
46897c478bd9Sstevel@tonic-gate tmp = "<none>";
46907c478bd9Sstevel@tonic-gate cmn_err(CE_CONT,
46917c478bd9Sstevel@tonic-gate "init_spec_child: parent=%s, bad spec (%s)\n",
46927c478bd9Sstevel@tonic-gate ddi_node_name(pdip), tmp);
46937c478bd9Sstevel@tonic-gate return;
46947c478bd9Sstevel@tonic-gate }
46957c478bd9Sstevel@tonic-gate
4696fa9e4066Sahrens dip = i_ddi_alloc_node(pdip, node_name, (pnode_t)DEVI_PSEUDO_NODEID,
46977c478bd9Sstevel@tonic-gate -1, specp->hwc_devi_sys_prop_ptr, KM_SLEEP);
46987c478bd9Sstevel@tonic-gate
46997c478bd9Sstevel@tonic-gate if (dip == NULL)
47007c478bd9Sstevel@tonic-gate return;
47017c478bd9Sstevel@tonic-gate
47027c478bd9Sstevel@tonic-gate if (ddi_initchild(pdip, dip) != DDI_SUCCESS)
47037c478bd9Sstevel@tonic-gate (void) ddi_remove_child(dip, 0);
47047c478bd9Sstevel@tonic-gate }
47057c478bd9Sstevel@tonic-gate
47067c478bd9Sstevel@tonic-gate /*
47077c478bd9Sstevel@tonic-gate * Lookup hwc specs from hash tables and make children from the spec
47087c478bd9Sstevel@tonic-gate * Because some .conf children are "merge" nodes, we also initialize
47097c478bd9Sstevel@tonic-gate * .conf children to merge properties onto hardware nodes.
47107c478bd9Sstevel@tonic-gate *
47117c478bd9Sstevel@tonic-gate * The pdip must be held busy.
47127c478bd9Sstevel@tonic-gate */
47137c478bd9Sstevel@tonic-gate int
i_ndi_make_spec_children(dev_info_t * pdip,uint_t flags)47147c478bd9Sstevel@tonic-gate i_ndi_make_spec_children(dev_info_t *pdip, uint_t flags)
47157c478bd9Sstevel@tonic-gate {
47167c478bd9Sstevel@tonic-gate extern struct hwc_spec *hwc_get_child_spec(dev_info_t *, major_t);
47177c478bd9Sstevel@tonic-gate struct hwc_spec *list, *spec;
47187c478bd9Sstevel@tonic-gate
47193fe80ca4SDan Cross ndi_devi_enter(pdip);
47206a41d557Scth if (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN) {
47213fe80ca4SDan Cross ndi_devi_exit(pdip);
47227c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
47236a41d557Scth }
47247c478bd9Sstevel@tonic-gate
4725a204de77Scth list = hwc_get_child_spec(pdip, DDI_MAJOR_T_NONE);
47267c478bd9Sstevel@tonic-gate for (spec = list; spec != NULL; spec = spec->hwc_next) {
47277c478bd9Sstevel@tonic-gate init_spec_child(pdip, spec, flags);
47287c478bd9Sstevel@tonic-gate }
47297c478bd9Sstevel@tonic-gate hwc_free_spec_list(list);
47307c478bd9Sstevel@tonic-gate
47317c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(pdip)->devi_lock);
47327c478bd9Sstevel@tonic-gate DEVI(pdip)->devi_flags |= DEVI_MADE_CHILDREN;
47337c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(pdip)->devi_lock);
47343fe80ca4SDan Cross ndi_devi_exit(pdip);
47357c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
47367c478bd9Sstevel@tonic-gate }
47377c478bd9Sstevel@tonic-gate
47387c478bd9Sstevel@tonic-gate /*
47397c478bd9Sstevel@tonic-gate * Run initchild on all child nodes such that instance assignment
47407c478bd9Sstevel@tonic-gate * for multiport network cards are contiguous.
47417c478bd9Sstevel@tonic-gate *
47427c478bd9Sstevel@tonic-gate * The pdip must be held busy.
47437c478bd9Sstevel@tonic-gate */
47447c478bd9Sstevel@tonic-gate static void
i_ndi_init_hw_children(dev_info_t * pdip,uint_t flags)47457c478bd9Sstevel@tonic-gate i_ndi_init_hw_children(dev_info_t *pdip, uint_t flags)
47467c478bd9Sstevel@tonic-gate {
47477c478bd9Sstevel@tonic-gate dev_info_t *dip;
47487c478bd9Sstevel@tonic-gate
47497c478bd9Sstevel@tonic-gate ASSERT(DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
47507c478bd9Sstevel@tonic-gate
47517c478bd9Sstevel@tonic-gate /* contiguous instance assignment */
47527c478bd9Sstevel@tonic-gate e_ddi_enter_instance();
47537c478bd9Sstevel@tonic-gate dip = ddi_get_child(pdip);
47547c478bd9Sstevel@tonic-gate while (dip) {
47557c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(dip))
47567c478bd9Sstevel@tonic-gate (void) i_ndi_config_node(dip, DS_INITIALIZED, flags);
47577c478bd9Sstevel@tonic-gate dip = ddi_get_next_sibling(dip);
47587c478bd9Sstevel@tonic-gate }
47597c478bd9Sstevel@tonic-gate e_ddi_exit_instance();
47607c478bd9Sstevel@tonic-gate }
47617c478bd9Sstevel@tonic-gate
47627c478bd9Sstevel@tonic-gate /*
47637c478bd9Sstevel@tonic-gate * report device status
47647c478bd9Sstevel@tonic-gate */
47657c478bd9Sstevel@tonic-gate static void
i_ndi_devi_report_status_change(dev_info_t * dip,char * path)47667c478bd9Sstevel@tonic-gate i_ndi_devi_report_status_change(dev_info_t *dip, char *path)
47677c478bd9Sstevel@tonic-gate {
47687c478bd9Sstevel@tonic-gate char *status;
47697c478bd9Sstevel@tonic-gate
47707c478bd9Sstevel@tonic-gate if (!DEVI_NEED_REPORT(dip) ||
47714c06356bSdh142964 (i_ddi_node_state(dip) < DS_INITIALIZED) ||
47724c06356bSdh142964 ndi_dev_is_hidden_node(dip)) {
47737c478bd9Sstevel@tonic-gate return;
47747c478bd9Sstevel@tonic-gate }
47757c478bd9Sstevel@tonic-gate
47764c06356bSdh142964 /* Invalidate the devinfo snapshot cache */
47774c06356bSdh142964 i_ddi_di_cache_invalidate();
47784c06356bSdh142964
47794c06356bSdh142964 if (DEVI_IS_DEVICE_REMOVED(dip)) {
47804c06356bSdh142964 status = "removed";
47814c06356bSdh142964 } else if (DEVI_IS_DEVICE_OFFLINE(dip)) {
47827c478bd9Sstevel@tonic-gate status = "offline";
47837c478bd9Sstevel@tonic-gate } else if (DEVI_IS_DEVICE_DOWN(dip)) {
47847c478bd9Sstevel@tonic-gate status = "down";
47857c478bd9Sstevel@tonic-gate } else if (DEVI_IS_BUS_QUIESCED(dip)) {
47867c478bd9Sstevel@tonic-gate status = "quiesced";
47877c478bd9Sstevel@tonic-gate } else if (DEVI_IS_BUS_DOWN(dip)) {
47887c478bd9Sstevel@tonic-gate status = "down";
4789737d277aScth } else if (i_ddi_devi_attached(dip)) {
47907c478bd9Sstevel@tonic-gate status = "online";
47917c478bd9Sstevel@tonic-gate } else {
47927c478bd9Sstevel@tonic-gate status = "unknown";
47937c478bd9Sstevel@tonic-gate }
47947c478bd9Sstevel@tonic-gate
47957c478bd9Sstevel@tonic-gate if (path == NULL) {
47967c478bd9Sstevel@tonic-gate path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
47977c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, "?%s (%s%d) %s\n",
47987c478bd9Sstevel@tonic-gate ddi_pathname(dip, path), ddi_driver_name(dip),
47997c478bd9Sstevel@tonic-gate ddi_get_instance(dip), status);
48007c478bd9Sstevel@tonic-gate kmem_free(path, MAXPATHLEN);
48017c478bd9Sstevel@tonic-gate } else {
48027c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, "?%s (%s%d) %s\n",
48037c478bd9Sstevel@tonic-gate path, ddi_driver_name(dip),
48047c478bd9Sstevel@tonic-gate ddi_get_instance(dip), status);
48057c478bd9Sstevel@tonic-gate }
48067c478bd9Sstevel@tonic-gate
480716747f41Scth mutex_enter(&(DEVI(dip)->devi_lock));
48087c478bd9Sstevel@tonic-gate DEVI_REPORT_DONE(dip);
480916747f41Scth mutex_exit(&(DEVI(dip)->devi_lock));
48107c478bd9Sstevel@tonic-gate }
48117c478bd9Sstevel@tonic-gate
48127c478bd9Sstevel@tonic-gate /*
48137c478bd9Sstevel@tonic-gate * log a notification that a dev_info node has been configured.
48147c478bd9Sstevel@tonic-gate */
48157c478bd9Sstevel@tonic-gate static int
i_log_devfs_add_devinfo(dev_info_t * dip,uint_t flags)48167c478bd9Sstevel@tonic-gate i_log_devfs_add_devinfo(dev_info_t *dip, uint_t flags)
48177c478bd9Sstevel@tonic-gate {
48187c478bd9Sstevel@tonic-gate int se_err;
48197c478bd9Sstevel@tonic-gate char *pathname;
48207c478bd9Sstevel@tonic-gate sysevent_t *ev;
48217c478bd9Sstevel@tonic-gate sysevent_id_t eid;
48227c478bd9Sstevel@tonic-gate sysevent_value_t se_val;
48237c478bd9Sstevel@tonic-gate sysevent_attr_list_t *ev_attr_list = NULL;
48247c478bd9Sstevel@tonic-gate char *class_name;
48257c478bd9Sstevel@tonic-gate int no_transport = 0;
48267c478bd9Sstevel@tonic-gate
48274c06356bSdh142964 ASSERT(dip && ddi_get_parent(dip) &&
48284c06356bSdh142964 DEVI_BUSY_OWNED(ddi_get_parent(dip)));
48297c478bd9Sstevel@tonic-gate
48307c478bd9Sstevel@tonic-gate /* do not generate ESC_DEVFS_DEVI_ADD event during boot */
48317c478bd9Sstevel@tonic-gate if (!i_ddi_io_initialized())
48327c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
48337c478bd9Sstevel@tonic-gate
48344c06356bSdh142964 /* Invalidate the devinfo snapshot cache */
48354c06356bSdh142964 i_ddi_di_cache_invalidate();
48364c06356bSdh142964
48377c478bd9Sstevel@tonic-gate ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_ADD, EP_DDI, SE_SLEEP);
48387c478bd9Sstevel@tonic-gate
48397c478bd9Sstevel@tonic-gate pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
48407c478bd9Sstevel@tonic-gate
48417c478bd9Sstevel@tonic-gate (void) ddi_pathname(dip, pathname);
48427c478bd9Sstevel@tonic-gate ASSERT(strlen(pathname));
48437c478bd9Sstevel@tonic-gate
48447c478bd9Sstevel@tonic-gate se_val.value_type = SE_DATA_TYPE_STRING;
48457c478bd9Sstevel@tonic-gate se_val.value.sv_string = pathname;
48467c478bd9Sstevel@tonic-gate if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
48477c478bd9Sstevel@tonic-gate &se_val, SE_SLEEP) != 0) {
48487c478bd9Sstevel@tonic-gate goto fail;
48497c478bd9Sstevel@tonic-gate }
48507c478bd9Sstevel@tonic-gate
48517c478bd9Sstevel@tonic-gate /* add the device class attribute */
48527c478bd9Sstevel@tonic-gate if ((class_name = i_ddi_devi_class(dip)) != NULL) {
48537c478bd9Sstevel@tonic-gate se_val.value_type = SE_DATA_TYPE_STRING;
48547c478bd9Sstevel@tonic-gate se_val.value.sv_string = class_name;
48557c478bd9Sstevel@tonic-gate
48567c478bd9Sstevel@tonic-gate if (sysevent_add_attr(&ev_attr_list,
48577c478bd9Sstevel@tonic-gate DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
48587c478bd9Sstevel@tonic-gate sysevent_free_attr(ev_attr_list);
48597c478bd9Sstevel@tonic-gate goto fail;
48607c478bd9Sstevel@tonic-gate }
48617c478bd9Sstevel@tonic-gate }
48627c478bd9Sstevel@tonic-gate
48637c478bd9Sstevel@tonic-gate /*
48647c478bd9Sstevel@tonic-gate * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
48657c478bd9Sstevel@tonic-gate * in which case the branch event will be logged by the caller
48667c478bd9Sstevel@tonic-gate * after the entire branch has been configured.
48677c478bd9Sstevel@tonic-gate */
48687c478bd9Sstevel@tonic-gate if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
48697c478bd9Sstevel@tonic-gate /*
48707c478bd9Sstevel@tonic-gate * Instead of logging a separate branch event just add
48717c478bd9Sstevel@tonic-gate * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
48727c478bd9Sstevel@tonic-gate * generate a EC_DEV_BRANCH event.
48737c478bd9Sstevel@tonic-gate */
48747c478bd9Sstevel@tonic-gate se_val.value_type = SE_DATA_TYPE_INT32;
48757c478bd9Sstevel@tonic-gate se_val.value.sv_int32 = 1;
48767c478bd9Sstevel@tonic-gate if (sysevent_add_attr(&ev_attr_list,
48777c478bd9Sstevel@tonic-gate DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
48787c478bd9Sstevel@tonic-gate sysevent_free_attr(ev_attr_list);
48797c478bd9Sstevel@tonic-gate goto fail;
48807c478bd9Sstevel@tonic-gate }
48817c478bd9Sstevel@tonic-gate }
48827c478bd9Sstevel@tonic-gate
48837c478bd9Sstevel@tonic-gate if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
48847c478bd9Sstevel@tonic-gate sysevent_free_attr(ev_attr_list);
48857c478bd9Sstevel@tonic-gate goto fail;
48867c478bd9Sstevel@tonic-gate }
48877c478bd9Sstevel@tonic-gate
48887c478bd9Sstevel@tonic-gate if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
48897c478bd9Sstevel@tonic-gate if (se_err == SE_NO_TRANSPORT)
48907c478bd9Sstevel@tonic-gate no_transport = 1;
48917c478bd9Sstevel@tonic-gate goto fail;
48927c478bd9Sstevel@tonic-gate }
48937c478bd9Sstevel@tonic-gate
48947c478bd9Sstevel@tonic-gate sysevent_free(ev);
48957c478bd9Sstevel@tonic-gate kmem_free(pathname, MAXPATHLEN);
48967c478bd9Sstevel@tonic-gate
48977c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
48987c478bd9Sstevel@tonic-gate
48997c478bd9Sstevel@tonic-gate fail:
49007c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_ADD event for %s%s",
49017c478bd9Sstevel@tonic-gate pathname, (no_transport) ? " (syseventd not responding)" : "");
49027c478bd9Sstevel@tonic-gate
49037c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "/dev may not be current for driver %s. "
49047c478bd9Sstevel@tonic-gate "Run devfsadm -i %s",
49057c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_driver_name(dip));
49067c478bd9Sstevel@tonic-gate
49077c478bd9Sstevel@tonic-gate sysevent_free(ev);
49087c478bd9Sstevel@tonic-gate kmem_free(pathname, MAXPATHLEN);
49097c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
49107c478bd9Sstevel@tonic-gate }
49117c478bd9Sstevel@tonic-gate
49127c478bd9Sstevel@tonic-gate /*
49137c478bd9Sstevel@tonic-gate * log a notification that a dev_info node has been unconfigured.
49147c478bd9Sstevel@tonic-gate */
49157c478bd9Sstevel@tonic-gate static int
i_log_devfs_remove_devinfo(char * pathname,char * class_name,char * driver_name,int instance,uint_t flags)49167c478bd9Sstevel@tonic-gate i_log_devfs_remove_devinfo(char *pathname, char *class_name, char *driver_name,
49177c478bd9Sstevel@tonic-gate int instance, uint_t flags)
49187c478bd9Sstevel@tonic-gate {
49197c478bd9Sstevel@tonic-gate sysevent_t *ev;
49207c478bd9Sstevel@tonic-gate sysevent_id_t eid;
49217c478bd9Sstevel@tonic-gate sysevent_value_t se_val;
49227c478bd9Sstevel@tonic-gate sysevent_attr_list_t *ev_attr_list = NULL;
49237c478bd9Sstevel@tonic-gate int se_err;
49247c478bd9Sstevel@tonic-gate int no_transport = 0;
49257c478bd9Sstevel@tonic-gate
49267c478bd9Sstevel@tonic-gate if (!i_ddi_io_initialized())
49277c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
49287c478bd9Sstevel@tonic-gate
49294c06356bSdh142964 /* Invalidate the devinfo snapshot cache */
49304c06356bSdh142964 i_ddi_di_cache_invalidate();
49314c06356bSdh142964
49327c478bd9Sstevel@tonic-gate ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_REMOVE, EP_DDI, SE_SLEEP);
49337c478bd9Sstevel@tonic-gate
49347c478bd9Sstevel@tonic-gate se_val.value_type = SE_DATA_TYPE_STRING;
49357c478bd9Sstevel@tonic-gate se_val.value.sv_string = pathname;
49367c478bd9Sstevel@tonic-gate if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
49377c478bd9Sstevel@tonic-gate &se_val, SE_SLEEP) != 0) {
49387c478bd9Sstevel@tonic-gate goto fail;
49397c478bd9Sstevel@tonic-gate }
49407c478bd9Sstevel@tonic-gate
49417c478bd9Sstevel@tonic-gate if (class_name) {
49427c478bd9Sstevel@tonic-gate /* add the device class, driver name and instance attributes */
49437c478bd9Sstevel@tonic-gate
49447c478bd9Sstevel@tonic-gate se_val.value_type = SE_DATA_TYPE_STRING;
49457c478bd9Sstevel@tonic-gate se_val.value.sv_string = class_name;
49467c478bd9Sstevel@tonic-gate if (sysevent_add_attr(&ev_attr_list,
49477c478bd9Sstevel@tonic-gate DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
49487c478bd9Sstevel@tonic-gate sysevent_free_attr(ev_attr_list);
49497c478bd9Sstevel@tonic-gate goto fail;
49507c478bd9Sstevel@tonic-gate }
49517c478bd9Sstevel@tonic-gate
49527c478bd9Sstevel@tonic-gate se_val.value_type = SE_DATA_TYPE_STRING;
49537c478bd9Sstevel@tonic-gate se_val.value.sv_string = driver_name;
49547c478bd9Sstevel@tonic-gate if (sysevent_add_attr(&ev_attr_list,
49557c478bd9Sstevel@tonic-gate DEVFS_DRIVER_NAME, &se_val, SE_SLEEP) != 0) {
49567c478bd9Sstevel@tonic-gate sysevent_free_attr(ev_attr_list);
49577c478bd9Sstevel@tonic-gate goto fail;
49587c478bd9Sstevel@tonic-gate }
49597c478bd9Sstevel@tonic-gate
49607c478bd9Sstevel@tonic-gate se_val.value_type = SE_DATA_TYPE_INT32;
49617c478bd9Sstevel@tonic-gate se_val.value.sv_int32 = instance;
49627c478bd9Sstevel@tonic-gate if (sysevent_add_attr(&ev_attr_list,
49637c478bd9Sstevel@tonic-gate DEVFS_INSTANCE, &se_val, SE_SLEEP) != 0) {
49647c478bd9Sstevel@tonic-gate sysevent_free_attr(ev_attr_list);
49657c478bd9Sstevel@tonic-gate goto fail;
49667c478bd9Sstevel@tonic-gate }
49677c478bd9Sstevel@tonic-gate }
49687c478bd9Sstevel@tonic-gate
49697c478bd9Sstevel@tonic-gate /*
49707c478bd9Sstevel@tonic-gate * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
49717c478bd9Sstevel@tonic-gate * in which case the branch event will be logged by the caller
49727c478bd9Sstevel@tonic-gate * after the entire branch has been unconfigured.
49737c478bd9Sstevel@tonic-gate */
49747c478bd9Sstevel@tonic-gate if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
49757c478bd9Sstevel@tonic-gate /*
49767c478bd9Sstevel@tonic-gate * Instead of logging a separate branch event just add
49777c478bd9Sstevel@tonic-gate * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
49787c478bd9Sstevel@tonic-gate * generate a EC_DEV_BRANCH event.
49797c478bd9Sstevel@tonic-gate */
49807c478bd9Sstevel@tonic-gate se_val.value_type = SE_DATA_TYPE_INT32;
49817c478bd9Sstevel@tonic-gate se_val.value.sv_int32 = 1;
49827c478bd9Sstevel@tonic-gate if (sysevent_add_attr(&ev_attr_list,
49837c478bd9Sstevel@tonic-gate DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
49847c478bd9Sstevel@tonic-gate sysevent_free_attr(ev_attr_list);
49857c478bd9Sstevel@tonic-gate goto fail;
49867c478bd9Sstevel@tonic-gate }
49877c478bd9Sstevel@tonic-gate }
49887c478bd9Sstevel@tonic-gate
49897c478bd9Sstevel@tonic-gate if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
49907c478bd9Sstevel@tonic-gate sysevent_free_attr(ev_attr_list);
49917c478bd9Sstevel@tonic-gate goto fail;
49927c478bd9Sstevel@tonic-gate }
49937c478bd9Sstevel@tonic-gate
49947c478bd9Sstevel@tonic-gate if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
49957c478bd9Sstevel@tonic-gate if (se_err == SE_NO_TRANSPORT)
49967c478bd9Sstevel@tonic-gate no_transport = 1;
49977c478bd9Sstevel@tonic-gate goto fail;
49987c478bd9Sstevel@tonic-gate }
49997c478bd9Sstevel@tonic-gate
50007c478bd9Sstevel@tonic-gate sysevent_free(ev);
50017c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
50027c478bd9Sstevel@tonic-gate
50037c478bd9Sstevel@tonic-gate fail:
50047c478bd9Sstevel@tonic-gate sysevent_free(ev);
50057c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_REMOVE event for %s%s",
50067c478bd9Sstevel@tonic-gate pathname, (no_transport) ? " (syseventd not responding)" : "");
50077c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
50087c478bd9Sstevel@tonic-gate }
50097c478bd9Sstevel@tonic-gate
50104c06356bSdh142964 static void
i_ddi_log_devfs_device_remove(dev_info_t * dip)50114c06356bSdh142964 i_ddi_log_devfs_device_remove(dev_info_t *dip)
50124c06356bSdh142964 {
50134c06356bSdh142964 char *path;
50144c06356bSdh142964
50154c06356bSdh142964 ASSERT(dip && ddi_get_parent(dip) &&
50164c06356bSdh142964 DEVI_BUSY_OWNED(ddi_get_parent(dip)));
50174c06356bSdh142964 ASSERT(DEVI_IS_DEVICE_REMOVED(dip));
50184c06356bSdh142964
50194c06356bSdh142964 ASSERT(i_ddi_node_state(dip) >= DS_INITIALIZED);
50204c06356bSdh142964 if (i_ddi_node_state(dip) < DS_INITIALIZED)
50214c06356bSdh142964 return;
50224c06356bSdh142964
5023328d222bSChris Horne /* Inform LDI_EV_DEVICE_REMOVE callbacks. */
5024328d222bSChris Horne ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0, LDI_EV_DEVICE_REMOVE,
5025328d222bSChris Horne LDI_EV_SUCCESS, NULL);
5026328d222bSChris Horne
5027328d222bSChris Horne /* Generate EC_DEVFS_DEVI_REMOVE sysevent. */
50284c06356bSdh142964 path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
50294c06356bSdh142964 (void) i_log_devfs_remove_devinfo(ddi_pathname(dip, path),
50304c06356bSdh142964 i_ddi_devi_class(dip), (char *)ddi_driver_name(dip),
50314c06356bSdh142964 ddi_get_instance(dip), 0);
50324c06356bSdh142964 kmem_free(path, MAXPATHLEN);
50334c06356bSdh142964 }
50344c06356bSdh142964
50354c06356bSdh142964 static void
i_ddi_log_devfs_device_insert(dev_info_t * dip)50364c06356bSdh142964 i_ddi_log_devfs_device_insert(dev_info_t *dip)
50374c06356bSdh142964 {
50384c06356bSdh142964 ASSERT(dip && ddi_get_parent(dip) &&
50394c06356bSdh142964 DEVI_BUSY_OWNED(ddi_get_parent(dip)));
50404c06356bSdh142964 ASSERT(!DEVI_IS_DEVICE_REMOVED(dip));
50414c06356bSdh142964
50424c06356bSdh142964 (void) i_log_devfs_add_devinfo(dip, 0);
50434c06356bSdh142964 }
50444c06356bSdh142964
50454c06356bSdh142964
50467c478bd9Sstevel@tonic-gate /*
50477c478bd9Sstevel@tonic-gate * log an event that a dev_info branch has been configured or unconfigured.
50487c478bd9Sstevel@tonic-gate */
50497c478bd9Sstevel@tonic-gate static int
i_log_devfs_branch(char * node_path,char * subclass)50507c478bd9Sstevel@tonic-gate i_log_devfs_branch(char *node_path, char *subclass)
50517c478bd9Sstevel@tonic-gate {
50527c478bd9Sstevel@tonic-gate int se_err;
50537c478bd9Sstevel@tonic-gate sysevent_t *ev;
50547c478bd9Sstevel@tonic-gate sysevent_id_t eid;
50557c478bd9Sstevel@tonic-gate sysevent_value_t se_val;
50567c478bd9Sstevel@tonic-gate sysevent_attr_list_t *ev_attr_list = NULL;
50577c478bd9Sstevel@tonic-gate int no_transport = 0;
50587c478bd9Sstevel@tonic-gate
50597c478bd9Sstevel@tonic-gate /* do not generate the event during boot */
50607c478bd9Sstevel@tonic-gate if (!i_ddi_io_initialized())
50617c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
50627c478bd9Sstevel@tonic-gate
50634c06356bSdh142964 /* Invalidate the devinfo snapshot cache */
50644c06356bSdh142964 i_ddi_di_cache_invalidate();
50654c06356bSdh142964
50667c478bd9Sstevel@tonic-gate ev = sysevent_alloc(EC_DEVFS, subclass, EP_DDI, SE_SLEEP);
50677c478bd9Sstevel@tonic-gate
50687c478bd9Sstevel@tonic-gate se_val.value_type = SE_DATA_TYPE_STRING;
50697c478bd9Sstevel@tonic-gate se_val.value.sv_string = node_path;
50707c478bd9Sstevel@tonic-gate
50717c478bd9Sstevel@tonic-gate if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
50727c478bd9Sstevel@tonic-gate &se_val, SE_SLEEP) != 0) {
50737c478bd9Sstevel@tonic-gate goto fail;
50747c478bd9Sstevel@tonic-gate }
50757c478bd9Sstevel@tonic-gate
50767c478bd9Sstevel@tonic-gate if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
50777c478bd9Sstevel@tonic-gate sysevent_free_attr(ev_attr_list);
50787c478bd9Sstevel@tonic-gate goto fail;
50797c478bd9Sstevel@tonic-gate }
50807c478bd9Sstevel@tonic-gate
50817c478bd9Sstevel@tonic-gate if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
50827c478bd9Sstevel@tonic-gate if (se_err == SE_NO_TRANSPORT)
50837c478bd9Sstevel@tonic-gate no_transport = 1;
50847c478bd9Sstevel@tonic-gate goto fail;
50857c478bd9Sstevel@tonic-gate }
50867c478bd9Sstevel@tonic-gate
50877c478bd9Sstevel@tonic-gate sysevent_free(ev);
50887c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
50897c478bd9Sstevel@tonic-gate
50907c478bd9Sstevel@tonic-gate fail:
50917c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "failed to log %s branch event for %s%s",
50927c478bd9Sstevel@tonic-gate subclass, node_path,
50937c478bd9Sstevel@tonic-gate (no_transport) ? " (syseventd not responding)" : "");
50947c478bd9Sstevel@tonic-gate
50957c478bd9Sstevel@tonic-gate sysevent_free(ev);
50967c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
50977c478bd9Sstevel@tonic-gate }
50987c478bd9Sstevel@tonic-gate
50997c478bd9Sstevel@tonic-gate /*
51007c478bd9Sstevel@tonic-gate * log an event that a dev_info tree branch has been configured.
51017c478bd9Sstevel@tonic-gate */
51027c478bd9Sstevel@tonic-gate static int
i_log_devfs_branch_add(dev_info_t * dip)51037c478bd9Sstevel@tonic-gate i_log_devfs_branch_add(dev_info_t *dip)
51047c478bd9Sstevel@tonic-gate {
51057c478bd9Sstevel@tonic-gate char *node_path;
51067c478bd9Sstevel@tonic-gate int rv;
51077c478bd9Sstevel@tonic-gate
51087c478bd9Sstevel@tonic-gate node_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
51097c478bd9Sstevel@tonic-gate (void) ddi_pathname(dip, node_path);
51107c478bd9Sstevel@tonic-gate rv = i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_ADD);
51117c478bd9Sstevel@tonic-gate kmem_free(node_path, MAXPATHLEN);
51127c478bd9Sstevel@tonic-gate
51137c478bd9Sstevel@tonic-gate return (rv);
51147c478bd9Sstevel@tonic-gate }
51157c478bd9Sstevel@tonic-gate
51167c478bd9Sstevel@tonic-gate /*
51177c478bd9Sstevel@tonic-gate * log an event that a dev_info tree branch has been unconfigured.
51187c478bd9Sstevel@tonic-gate */
51197c478bd9Sstevel@tonic-gate static int
i_log_devfs_branch_remove(char * node_path)51207c478bd9Sstevel@tonic-gate i_log_devfs_branch_remove(char *node_path)
51217c478bd9Sstevel@tonic-gate {
51227c478bd9Sstevel@tonic-gate return (i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_REMOVE));
51237c478bd9Sstevel@tonic-gate }
51247c478bd9Sstevel@tonic-gate
51257c478bd9Sstevel@tonic-gate /*
51267c478bd9Sstevel@tonic-gate * enqueue the dip's deviname on the branch event queue.
51277c478bd9Sstevel@tonic-gate */
51287c478bd9Sstevel@tonic-gate static struct brevq_node *
brevq_enqueue(struct brevq_node ** brevqp,dev_info_t * dip,struct brevq_node * child)51297c478bd9Sstevel@tonic-gate brevq_enqueue(struct brevq_node **brevqp, dev_info_t *dip,
51307c478bd9Sstevel@tonic-gate struct brevq_node *child)
51317c478bd9Sstevel@tonic-gate {
51327c478bd9Sstevel@tonic-gate struct brevq_node *brn;
51337c478bd9Sstevel@tonic-gate char *deviname;
51347c478bd9Sstevel@tonic-gate
51357c478bd9Sstevel@tonic-gate deviname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
51367c478bd9Sstevel@tonic-gate (void) ddi_deviname(dip, deviname);
51377c478bd9Sstevel@tonic-gate
51387c478bd9Sstevel@tonic-gate brn = kmem_zalloc(sizeof (*brn), KM_SLEEP);
5139245c82d9Scth brn->brn_deviname = i_ddi_strdup(deviname, KM_SLEEP);
51407c478bd9Sstevel@tonic-gate kmem_free(deviname, MAXNAMELEN);
5141245c82d9Scth brn->brn_child = child;
5142245c82d9Scth brn->brn_sibling = *brevqp;
51437c478bd9Sstevel@tonic-gate *brevqp = brn;
51447c478bd9Sstevel@tonic-gate
51457c478bd9Sstevel@tonic-gate return (brn);
51467c478bd9Sstevel@tonic-gate }
51477c478bd9Sstevel@tonic-gate
51487c478bd9Sstevel@tonic-gate /*
51497c478bd9Sstevel@tonic-gate * free the memory allocated for the elements on the branch event queue.
51507c478bd9Sstevel@tonic-gate */
51517c478bd9Sstevel@tonic-gate static void
free_brevq(struct brevq_node * brevq)51527c478bd9Sstevel@tonic-gate free_brevq(struct brevq_node *brevq)
51537c478bd9Sstevel@tonic-gate {
51547c478bd9Sstevel@tonic-gate struct brevq_node *brn, *next_brn;
51557c478bd9Sstevel@tonic-gate
51567c478bd9Sstevel@tonic-gate for (brn = brevq; brn != NULL; brn = next_brn) {
5157245c82d9Scth next_brn = brn->brn_sibling;
5158245c82d9Scth ASSERT(brn->brn_child == NULL);
5159245c82d9Scth kmem_free(brn->brn_deviname, strlen(brn->brn_deviname) + 1);
51607c478bd9Sstevel@tonic-gate kmem_free(brn, sizeof (*brn));
51617c478bd9Sstevel@tonic-gate }
51627c478bd9Sstevel@tonic-gate }
51637c478bd9Sstevel@tonic-gate
51647c478bd9Sstevel@tonic-gate /*
51657c478bd9Sstevel@tonic-gate * log the events queued up on the branch event queue and free the
51667c478bd9Sstevel@tonic-gate * associated memory.
51677c478bd9Sstevel@tonic-gate *
51687c478bd9Sstevel@tonic-gate * node_path must have been allocated with at least MAXPATHLEN bytes.
51697c478bd9Sstevel@tonic-gate */
51707c478bd9Sstevel@tonic-gate static void
log_and_free_brevq(char * node_path,struct brevq_node * brevq)51717c478bd9Sstevel@tonic-gate log_and_free_brevq(char *node_path, struct brevq_node *brevq)
51727c478bd9Sstevel@tonic-gate {
51737c478bd9Sstevel@tonic-gate struct brevq_node *brn;
51747c478bd9Sstevel@tonic-gate char *p;
51757c478bd9Sstevel@tonic-gate
51767c478bd9Sstevel@tonic-gate p = node_path + strlen(node_path);
5177245c82d9Scth for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
5178245c82d9Scth (void) strcpy(p, brn->brn_deviname);
51797c478bd9Sstevel@tonic-gate (void) i_log_devfs_branch_remove(node_path);
51807c478bd9Sstevel@tonic-gate }
51817c478bd9Sstevel@tonic-gate *p = '\0';
51827c478bd9Sstevel@tonic-gate
51837c478bd9Sstevel@tonic-gate free_brevq(brevq);
51847c478bd9Sstevel@tonic-gate }
51857c478bd9Sstevel@tonic-gate
51867c478bd9Sstevel@tonic-gate /*
51877c478bd9Sstevel@tonic-gate * log the events queued up on the branch event queue and free the
51887c478bd9Sstevel@tonic-gate * associated memory. Same as the previous function but operates on dip.
51897c478bd9Sstevel@tonic-gate */
51907c478bd9Sstevel@tonic-gate static void
log_and_free_brevq_dip(dev_info_t * dip,struct brevq_node * brevq)51917c478bd9Sstevel@tonic-gate log_and_free_brevq_dip(dev_info_t *dip, struct brevq_node *brevq)
51927c478bd9Sstevel@tonic-gate {
51937c478bd9Sstevel@tonic-gate char *path;
51947c478bd9Sstevel@tonic-gate
51957c478bd9Sstevel@tonic-gate path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
51967c478bd9Sstevel@tonic-gate (void) ddi_pathname(dip, path);
51977c478bd9Sstevel@tonic-gate log_and_free_brevq(path, brevq);
51987c478bd9Sstevel@tonic-gate kmem_free(path, MAXPATHLEN);
51997c478bd9Sstevel@tonic-gate }
52007c478bd9Sstevel@tonic-gate
52017c478bd9Sstevel@tonic-gate /*
52027c478bd9Sstevel@tonic-gate * log the outstanding branch remove events for the grand children of the dip
52037c478bd9Sstevel@tonic-gate * and free the associated memory.
52047c478bd9Sstevel@tonic-gate */
52057c478bd9Sstevel@tonic-gate static void
log_and_free_br_events_on_grand_children(dev_info_t * dip,struct brevq_node * brevq)52067c478bd9Sstevel@tonic-gate log_and_free_br_events_on_grand_children(dev_info_t *dip,
52077c478bd9Sstevel@tonic-gate struct brevq_node *brevq)
52087c478bd9Sstevel@tonic-gate {
52097c478bd9Sstevel@tonic-gate struct brevq_node *brn;
52107c478bd9Sstevel@tonic-gate char *path;
52117c478bd9Sstevel@tonic-gate char *p;
52127c478bd9Sstevel@tonic-gate
52137c478bd9Sstevel@tonic-gate path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
52147c478bd9Sstevel@tonic-gate (void) ddi_pathname(dip, path);
52157c478bd9Sstevel@tonic-gate p = path + strlen(path);
5216245c82d9Scth for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
5217245c82d9Scth if (brn->brn_child) {
5218245c82d9Scth (void) strcpy(p, brn->brn_deviname);
52197c478bd9Sstevel@tonic-gate /* now path contains the node path to the dip's child */
5220245c82d9Scth log_and_free_brevq(path, brn->brn_child);
5221245c82d9Scth brn->brn_child = NULL;
52227c478bd9Sstevel@tonic-gate }
52237c478bd9Sstevel@tonic-gate }
52247c478bd9Sstevel@tonic-gate kmem_free(path, MAXPATHLEN);
52257c478bd9Sstevel@tonic-gate }
52267c478bd9Sstevel@tonic-gate
52277c478bd9Sstevel@tonic-gate /*
52287c478bd9Sstevel@tonic-gate * log and cleanup branch remove events for the grand children of the dip.
52297c478bd9Sstevel@tonic-gate */
52307c478bd9Sstevel@tonic-gate static void
cleanup_br_events_on_grand_children(dev_info_t * dip,struct brevq_node ** brevqp)52317c478bd9Sstevel@tonic-gate cleanup_br_events_on_grand_children(dev_info_t *dip, struct brevq_node **brevqp)
52327c478bd9Sstevel@tonic-gate {
52337c478bd9Sstevel@tonic-gate dev_info_t *child;
52347c478bd9Sstevel@tonic-gate struct brevq_node *brevq, *brn, *prev_brn, *next_brn;
52357c478bd9Sstevel@tonic-gate char *path;
52367c478bd9Sstevel@tonic-gate
52377c478bd9Sstevel@tonic-gate path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
52387c478bd9Sstevel@tonic-gate prev_brn = NULL;
52397c478bd9Sstevel@tonic-gate brevq = *brevqp;
52407c478bd9Sstevel@tonic-gate
52413fe80ca4SDan Cross ndi_devi_enter(dip);
52427c478bd9Sstevel@tonic-gate for (brn = brevq; brn != NULL; brn = next_brn) {
5243245c82d9Scth next_brn = brn->brn_sibling;
52447c478bd9Sstevel@tonic-gate for (child = ddi_get_child(dip); child != NULL;
52457c478bd9Sstevel@tonic-gate child = ddi_get_next_sibling(child)) {
52467c478bd9Sstevel@tonic-gate if (i_ddi_node_state(child) >= DS_INITIALIZED) {
52477c478bd9Sstevel@tonic-gate (void) ddi_deviname(child, path);
5248245c82d9Scth if (strcmp(path, brn->brn_deviname) == 0)
52497c478bd9Sstevel@tonic-gate break;
52507c478bd9Sstevel@tonic-gate }
52517c478bd9Sstevel@tonic-gate }
52527c478bd9Sstevel@tonic-gate
52537c478bd9Sstevel@tonic-gate if (child != NULL && !(DEVI_EVREMOVE(child))) {
52547c478bd9Sstevel@tonic-gate /*
52557c478bd9Sstevel@tonic-gate * Event state is not REMOVE. So branch remove event
5256245c82d9Scth * is not going be generated on brn->brn_child.
52577c478bd9Sstevel@tonic-gate * If any branch remove events were queued up on
5258245c82d9Scth * brn->brn_child log them and remove the brn
52597c478bd9Sstevel@tonic-gate * from the queue.
52607c478bd9Sstevel@tonic-gate */
5261245c82d9Scth if (brn->brn_child) {
52627c478bd9Sstevel@tonic-gate (void) ddi_pathname(dip, path);
5263245c82d9Scth (void) strcat(path, brn->brn_deviname);
5264245c82d9Scth log_and_free_brevq(path, brn->brn_child);
52657c478bd9Sstevel@tonic-gate }
52667c478bd9Sstevel@tonic-gate
52677c478bd9Sstevel@tonic-gate if (prev_brn)
5268245c82d9Scth prev_brn->brn_sibling = next_brn;
52697c478bd9Sstevel@tonic-gate else
52707c478bd9Sstevel@tonic-gate *brevqp = next_brn;
52717c478bd9Sstevel@tonic-gate
5272245c82d9Scth kmem_free(brn->brn_deviname,
5273245c82d9Scth strlen(brn->brn_deviname) + 1);
52747c478bd9Sstevel@tonic-gate kmem_free(brn, sizeof (*brn));
52757c478bd9Sstevel@tonic-gate } else {
52767c478bd9Sstevel@tonic-gate /*
52777c478bd9Sstevel@tonic-gate * Free up the outstanding branch remove events
5278245c82d9Scth * queued on brn->brn_child since brn->brn_child
52797c478bd9Sstevel@tonic-gate * itself is eligible for branch remove event.
52807c478bd9Sstevel@tonic-gate */
5281245c82d9Scth if (brn->brn_child) {
5282245c82d9Scth free_brevq(brn->brn_child);
5283245c82d9Scth brn->brn_child = NULL;
52847c478bd9Sstevel@tonic-gate }
52857c478bd9Sstevel@tonic-gate prev_brn = brn;
52867c478bd9Sstevel@tonic-gate }
52877c478bd9Sstevel@tonic-gate }
52887c478bd9Sstevel@tonic-gate
52893fe80ca4SDan Cross ndi_devi_exit(dip);
52907c478bd9Sstevel@tonic-gate kmem_free(path, MAXPATHLEN);
52917c478bd9Sstevel@tonic-gate }
52927c478bd9Sstevel@tonic-gate
52937c478bd9Sstevel@tonic-gate static int
need_remove_event(dev_info_t * dip,int flags)52947c478bd9Sstevel@tonic-gate need_remove_event(dev_info_t *dip, int flags)
52957c478bd9Sstevel@tonic-gate {
52967c478bd9Sstevel@tonic-gate if ((flags & (NDI_NO_EVENT | NDI_AUTODETACH)) == 0 &&
52977c478bd9Sstevel@tonic-gate (flags & (NDI_DEVI_OFFLINE | NDI_UNCONFIG | NDI_DEVI_REMOVE)) &&
52987c478bd9Sstevel@tonic-gate !(DEVI_EVREMOVE(dip)))
52997c478bd9Sstevel@tonic-gate return (1);
53007c478bd9Sstevel@tonic-gate else
53017c478bd9Sstevel@tonic-gate return (0);
53027c478bd9Sstevel@tonic-gate }
53037c478bd9Sstevel@tonic-gate
53047c478bd9Sstevel@tonic-gate /*
53057c478bd9Sstevel@tonic-gate * Unconfigure children/descendants of the dip.
53067c478bd9Sstevel@tonic-gate *
53077c478bd9Sstevel@tonic-gate * If the operation involves a branch event NDI_BRANCH_EVENT_OP is set
53087c478bd9Sstevel@tonic-gate * through out the unconfiguration. On successful return *brevqp is set to
53097c478bd9Sstevel@tonic-gate * a queue of dip's child devinames for which branch remove events need
53107c478bd9Sstevel@tonic-gate * to be generated.
53117c478bd9Sstevel@tonic-gate */
53127c478bd9Sstevel@tonic-gate static int
devi_unconfig_branch(dev_info_t * dip,dev_info_t ** dipp,int flags,struct brevq_node ** brevqp)53137c478bd9Sstevel@tonic-gate devi_unconfig_branch(dev_info_t *dip, dev_info_t **dipp, int flags,
53147c478bd9Sstevel@tonic-gate struct brevq_node **brevqp)
53157c478bd9Sstevel@tonic-gate {
53167c478bd9Sstevel@tonic-gate int rval;
53177c478bd9Sstevel@tonic-gate
53187c478bd9Sstevel@tonic-gate *brevqp = NULL;
53197c478bd9Sstevel@tonic-gate
53207c478bd9Sstevel@tonic-gate if ((!(flags & NDI_BRANCH_EVENT_OP)) && need_remove_event(dip, flags))
53217c478bd9Sstevel@tonic-gate flags |= NDI_BRANCH_EVENT_OP;
53227c478bd9Sstevel@tonic-gate
53237c478bd9Sstevel@tonic-gate if (flags & NDI_BRANCH_EVENT_OP) {
5324a204de77Scth rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
53257c478bd9Sstevel@tonic-gate brevqp);
53267c478bd9Sstevel@tonic-gate
53277c478bd9Sstevel@tonic-gate if (rval != NDI_SUCCESS && (*brevqp)) {
53287c478bd9Sstevel@tonic-gate log_and_free_brevq_dip(dip, *brevqp);
53297c478bd9Sstevel@tonic-gate *brevqp = NULL;
53307c478bd9Sstevel@tonic-gate }
53317c478bd9Sstevel@tonic-gate } else
5332a204de77Scth rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
53337c478bd9Sstevel@tonic-gate NULL);
53347c478bd9Sstevel@tonic-gate
53357c478bd9Sstevel@tonic-gate return (rval);
53367c478bd9Sstevel@tonic-gate }
53377c478bd9Sstevel@tonic-gate
53387c478bd9Sstevel@tonic-gate /*
53397c478bd9Sstevel@tonic-gate * If the dip is already bound to a driver transition to DS_INITIALIZED
53407c478bd9Sstevel@tonic-gate * in order to generate an event in the case where the node was left in
53417c478bd9Sstevel@tonic-gate * DS_BOUND state since boot (never got attached) and the node is now
53427c478bd9Sstevel@tonic-gate * being offlined.
53437c478bd9Sstevel@tonic-gate */
53447c478bd9Sstevel@tonic-gate static void
init_bound_node_ev(dev_info_t * pdip,dev_info_t * dip,int flags)53457c478bd9Sstevel@tonic-gate init_bound_node_ev(dev_info_t *pdip, dev_info_t *dip, int flags)
53467c478bd9Sstevel@tonic-gate {
53477c478bd9Sstevel@tonic-gate if (need_remove_event(dip, flags) &&
53487c478bd9Sstevel@tonic-gate i_ddi_node_state(dip) == DS_BOUND &&
5349737d277aScth i_ddi_devi_attached(pdip) && !DEVI_IS_DEVICE_OFFLINE(dip))
53507c478bd9Sstevel@tonic-gate (void) ddi_initchild(pdip, dip);
53517c478bd9Sstevel@tonic-gate }
53527c478bd9Sstevel@tonic-gate
53537c478bd9Sstevel@tonic-gate /*
53547c478bd9Sstevel@tonic-gate * attach a node/branch with parent already held busy
53557c478bd9Sstevel@tonic-gate */
53567c478bd9Sstevel@tonic-gate static int
devi_attach_node(dev_info_t * dip,uint_t flags)53577c478bd9Sstevel@tonic-gate devi_attach_node(dev_info_t *dip, uint_t flags)
53587c478bd9Sstevel@tonic-gate {
53595e3986cbScth dev_info_t *pdip = ddi_get_parent(dip);
53605e3986cbScth
53615e3986cbScth ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
53625e3986cbScth
536316747f41Scth mutex_enter(&(DEVI(dip)->devi_lock));
53647c478bd9Sstevel@tonic-gate if (flags & NDI_DEVI_ONLINE) {
5365737d277aScth if (!i_ddi_devi_attached(dip))
536616747f41Scth DEVI_SET_REPORT(dip);
53677c478bd9Sstevel@tonic-gate DEVI_SET_DEVICE_ONLINE(dip);
53687c478bd9Sstevel@tonic-gate }
53697c478bd9Sstevel@tonic-gate if (DEVI_IS_DEVICE_OFFLINE(dip)) {
537016747f41Scth mutex_exit(&(DEVI(dip)->devi_lock));
53717c478bd9Sstevel@tonic-gate return (NDI_FAILURE);
53727c478bd9Sstevel@tonic-gate }
537316747f41Scth mutex_exit(&(DEVI(dip)->devi_lock));
53747c478bd9Sstevel@tonic-gate
53757c478bd9Sstevel@tonic-gate if (i_ddi_attachchild(dip) != DDI_SUCCESS) {
537616747f41Scth mutex_enter(&(DEVI(dip)->devi_lock));
53777c478bd9Sstevel@tonic-gate DEVI_SET_EVUNINIT(dip);
537816747f41Scth mutex_exit(&(DEVI(dip)->devi_lock));
537916747f41Scth
53807c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(dip))
53817c478bd9Sstevel@tonic-gate (void) ddi_uninitchild(dip);
53827c478bd9Sstevel@tonic-gate else {
53837c478bd9Sstevel@tonic-gate /*
53847c478bd9Sstevel@tonic-gate * Delete .conf nodes and nodes that are not
53857c478bd9Sstevel@tonic-gate * well formed.
53867c478bd9Sstevel@tonic-gate */
53877c478bd9Sstevel@tonic-gate (void) ddi_remove_child(dip, 0);
53887c478bd9Sstevel@tonic-gate }
53897c478bd9Sstevel@tonic-gate return (NDI_FAILURE);
53907c478bd9Sstevel@tonic-gate }
53917c478bd9Sstevel@tonic-gate
53927c478bd9Sstevel@tonic-gate i_ndi_devi_report_status_change(dip, NULL);
53937c478bd9Sstevel@tonic-gate
53947c478bd9Sstevel@tonic-gate /*
53957c478bd9Sstevel@tonic-gate * log an event, but not during devfs lookups in which case
53967c478bd9Sstevel@tonic-gate * NDI_NO_EVENT is set.
53977c478bd9Sstevel@tonic-gate */
53987c478bd9Sstevel@tonic-gate if ((flags & NDI_NO_EVENT) == 0 && !(DEVI_EVADD(dip))) {
53997c478bd9Sstevel@tonic-gate (void) i_log_devfs_add_devinfo(dip, flags);
540016747f41Scth
540116747f41Scth mutex_enter(&(DEVI(dip)->devi_lock));
54027c478bd9Sstevel@tonic-gate DEVI_SET_EVADD(dip);
540316747f41Scth mutex_exit(&(DEVI(dip)->devi_lock));
540416747f41Scth } else if (!(flags & NDI_NO_EVENT_STATE_CHNG)) {
540516747f41Scth mutex_enter(&(DEVI(dip)->devi_lock));
54067c478bd9Sstevel@tonic-gate DEVI_SET_EVADD(dip);
540716747f41Scth mutex_exit(&(DEVI(dip)->devi_lock));
540816747f41Scth }
54097c478bd9Sstevel@tonic-gate
54107c478bd9Sstevel@tonic-gate return (NDI_SUCCESS);
54117c478bd9Sstevel@tonic-gate }
54127c478bd9Sstevel@tonic-gate
54137c478bd9Sstevel@tonic-gate /* internal function to config immediate children */
54147c478bd9Sstevel@tonic-gate static int
config_immediate_children(dev_info_t * pdip,uint_t flags,major_t major)54157c478bd9Sstevel@tonic-gate config_immediate_children(dev_info_t *pdip, uint_t flags, major_t major)
54167c478bd9Sstevel@tonic-gate {
54175e3986cbScth dev_info_t *child, *next;
54185e3986cbScth
5419737d277aScth ASSERT(i_ddi_devi_attached(pdip));
54207c478bd9Sstevel@tonic-gate
54217c478bd9Sstevel@tonic-gate if (!NEXUS_DRV(ddi_get_driver(pdip)))
54227c478bd9Sstevel@tonic-gate return (NDI_SUCCESS);
54237c478bd9Sstevel@tonic-gate
54247c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT,
54257c478bd9Sstevel@tonic-gate "config_immediate_children: %s%d (%p), flags=%x\n",
54267c478bd9Sstevel@tonic-gate ddi_driver_name(pdip), ddi_get_instance(pdip),
54277c478bd9Sstevel@tonic-gate (void *)pdip, flags));
54287c478bd9Sstevel@tonic-gate
54293fe80ca4SDan Cross ndi_devi_enter(pdip);
54307c478bd9Sstevel@tonic-gate
54317c478bd9Sstevel@tonic-gate if (flags & NDI_CONFIG_REPROBE) {
54327c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(pdip)->devi_lock);
54337c478bd9Sstevel@tonic-gate DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
54347c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(pdip)->devi_lock);
54357c478bd9Sstevel@tonic-gate }
54367c478bd9Sstevel@tonic-gate (void) i_ndi_make_spec_children(pdip, flags);
54377c478bd9Sstevel@tonic-gate i_ndi_init_hw_children(pdip, flags);
54385e3986cbScth
54395e3986cbScth child = ddi_get_child(pdip);
54405e3986cbScth while (child) {
54415e3986cbScth /* NOTE: devi_attach_node() may remove the dip */
54425e3986cbScth next = ddi_get_next_sibling(child);
54435e3986cbScth
54445e3986cbScth /*
54455e3986cbScth * Configure all nexus nodes or leaf nodes with
54465e3986cbScth * matching driver major
54475e3986cbScth */
5448a204de77Scth if ((major == DDI_MAJOR_T_NONE) ||
54495e3986cbScth (major == ddi_driver_major(child)) ||
54505e3986cbScth ((flags & NDI_CONFIG) && (is_leaf_node(child) == 0)))
54515e3986cbScth (void) devi_attach_node(child, flags);
54525e3986cbScth child = next;
54535e3986cbScth }
54547c478bd9Sstevel@tonic-gate
54553fe80ca4SDan Cross ndi_devi_exit(pdip);
54567c478bd9Sstevel@tonic-gate
54577c478bd9Sstevel@tonic-gate return (NDI_SUCCESS);
54587c478bd9Sstevel@tonic-gate }
54597c478bd9Sstevel@tonic-gate
54607c478bd9Sstevel@tonic-gate /* internal function to config grand children */
54617c478bd9Sstevel@tonic-gate static int
config_grand_children(dev_info_t * pdip,uint_t flags,major_t major)54627c478bd9Sstevel@tonic-gate config_grand_children(dev_info_t *pdip, uint_t flags, major_t major)
54637c478bd9Sstevel@tonic-gate {
54647c478bd9Sstevel@tonic-gate struct mt_config_handle *hdl;
54657c478bd9Sstevel@tonic-gate
54667c478bd9Sstevel@tonic-gate /* multi-threaded configuration of child nexus */
54677c478bd9Sstevel@tonic-gate hdl = mt_config_init(pdip, NULL, flags, major, MT_CONFIG_OP, NULL);
54687c478bd9Sstevel@tonic-gate mt_config_children(hdl);
54697c478bd9Sstevel@tonic-gate
54707c478bd9Sstevel@tonic-gate return (mt_config_fini(hdl)); /* wait for threads to exit */
54717c478bd9Sstevel@tonic-gate }
54727c478bd9Sstevel@tonic-gate
54737c478bd9Sstevel@tonic-gate /*
54747c478bd9Sstevel@tonic-gate * Common function for device tree configuration,
54757c478bd9Sstevel@tonic-gate * either BUS_CONFIG_ALL or BUS_CONFIG_DRIVER.
54767c478bd9Sstevel@tonic-gate * The NDI_CONFIG flag causes recursive configuration of
54777c478bd9Sstevel@tonic-gate * grandchildren, devfs usage should not recurse.
54787c478bd9Sstevel@tonic-gate */
54797c478bd9Sstevel@tonic-gate static int
devi_config_common(dev_info_t * dip,int flags,major_t major)54807c478bd9Sstevel@tonic-gate devi_config_common(dev_info_t *dip, int flags, major_t major)
54817c478bd9Sstevel@tonic-gate {
54827c478bd9Sstevel@tonic-gate int error;
54837c478bd9Sstevel@tonic-gate int (*f)();
54847c478bd9Sstevel@tonic-gate
5485737d277aScth if (!i_ddi_devi_attached(dip))
54867c478bd9Sstevel@tonic-gate return (NDI_FAILURE);
54877c478bd9Sstevel@tonic-gate
54887c478bd9Sstevel@tonic-gate if (pm_pre_config(dip, NULL) != DDI_SUCCESS)
54897c478bd9Sstevel@tonic-gate return (NDI_FAILURE);
54907c478bd9Sstevel@tonic-gate
54917c478bd9Sstevel@tonic-gate if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
54927c478bd9Sstevel@tonic-gate (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
54937c478bd9Sstevel@tonic-gate (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
54947c478bd9Sstevel@tonic-gate error = config_immediate_children(dip, flags, major);
54957c478bd9Sstevel@tonic-gate } else {
54967c478bd9Sstevel@tonic-gate /* call bus_config entry point */
5497a204de77Scth ddi_bus_config_op_t bus_op = (major == DDI_MAJOR_T_NONE) ?
54987c478bd9Sstevel@tonic-gate BUS_CONFIG_ALL : BUS_CONFIG_DRIVER;
54997c478bd9Sstevel@tonic-gate error = (*f)(dip,
55007c478bd9Sstevel@tonic-gate flags, bus_op, (void *)(uintptr_t)major, NULL, 0);
55017c478bd9Sstevel@tonic-gate }
55027c478bd9Sstevel@tonic-gate
55037c478bd9Sstevel@tonic-gate if (error) {
55047c478bd9Sstevel@tonic-gate pm_post_config(dip, NULL);
55057c478bd9Sstevel@tonic-gate return (error);
55067c478bd9Sstevel@tonic-gate }
55077c478bd9Sstevel@tonic-gate
55087c478bd9Sstevel@tonic-gate /*
55097c478bd9Sstevel@tonic-gate * Some callers, notably SCSI, need to mark the devfs cache
55107c478bd9Sstevel@tonic-gate * to be rebuilt together with the config operation.
55117c478bd9Sstevel@tonic-gate */
55127c478bd9Sstevel@tonic-gate if (flags & NDI_DEVFS_CLEAN)
55137c478bd9Sstevel@tonic-gate (void) devfs_clean(dip, NULL, 0);
55147c478bd9Sstevel@tonic-gate
55157c478bd9Sstevel@tonic-gate if (flags & NDI_CONFIG)
55167c478bd9Sstevel@tonic-gate (void) config_grand_children(dip, flags, major);
55177c478bd9Sstevel@tonic-gate
55187c478bd9Sstevel@tonic-gate pm_post_config(dip, NULL);
55197c478bd9Sstevel@tonic-gate
55207c478bd9Sstevel@tonic-gate return (NDI_SUCCESS);
55217c478bd9Sstevel@tonic-gate }
55227c478bd9Sstevel@tonic-gate
55237c478bd9Sstevel@tonic-gate /*
55247c478bd9Sstevel@tonic-gate * Framework entry point for BUS_CONFIG_ALL
55257c478bd9Sstevel@tonic-gate */
55267c478bd9Sstevel@tonic-gate int
ndi_devi_config(dev_info_t * dip,int flags)55277c478bd9Sstevel@tonic-gate ndi_devi_config(dev_info_t *dip, int flags)
55287c478bd9Sstevel@tonic-gate {
55297c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT,
55307c478bd9Sstevel@tonic-gate "ndi_devi_config: par = %s%d (%p), flags = 0x%x\n",
55317c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
55327c478bd9Sstevel@tonic-gate
5533a204de77Scth return (devi_config_common(dip, flags, DDI_MAJOR_T_NONE));
55347c478bd9Sstevel@tonic-gate }
55357c478bd9Sstevel@tonic-gate
55367c478bd9Sstevel@tonic-gate /*
55377c478bd9Sstevel@tonic-gate * Framework entry point for BUS_CONFIG_DRIVER, bound to major
55387c478bd9Sstevel@tonic-gate */
55397c478bd9Sstevel@tonic-gate int
ndi_devi_config_driver(dev_info_t * dip,int flags,major_t major)55407c478bd9Sstevel@tonic-gate ndi_devi_config_driver(dev_info_t *dip, int flags, major_t major)
55417c478bd9Sstevel@tonic-gate {
55427c478bd9Sstevel@tonic-gate /* don't abuse this function */
5543a204de77Scth ASSERT(major != DDI_MAJOR_T_NONE);
55447c478bd9Sstevel@tonic-gate
55457c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT,
55467c478bd9Sstevel@tonic-gate "ndi_devi_config_driver: par = %s%d (%p), flags = 0x%x\n",
55477c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
55487c478bd9Sstevel@tonic-gate
55497c478bd9Sstevel@tonic-gate return (devi_config_common(dip, flags, major));
55507c478bd9Sstevel@tonic-gate }
55517c478bd9Sstevel@tonic-gate
55527c478bd9Sstevel@tonic-gate /*
55535e3986cbScth * Called by nexus drivers to configure its children.
55547c478bd9Sstevel@tonic-gate */
55557c478bd9Sstevel@tonic-gate static int
devi_config_one(dev_info_t * pdip,char * devnm,dev_info_t ** cdipp,uint_t flags,clock_t timeout)55565e3986cbScth devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **cdipp,
55577c478bd9Sstevel@tonic-gate uint_t flags, clock_t timeout)
55587c478bd9Sstevel@tonic-gate {
55595e3986cbScth dev_info_t *vdip = NULL;
55605e3986cbScth char *drivername = NULL;
5561f4da9be0Scth int find_by_addr = 0;
55625e3986cbScth char *name, *addr;
55637c478bd9Sstevel@tonic-gate clock_t end_time; /* 60 sec */
55645e3986cbScth int probed;
55655e3986cbScth dev_info_t *cdip;
55665e3986cbScth mdi_pathinfo_t *cpip;
55675e3986cbScth
55685e3986cbScth *cdipp = NULL;
55697c478bd9Sstevel@tonic-gate
55707c478bd9Sstevel@tonic-gate if (!NEXUS_DRV(ddi_get_driver(pdip)))
55717c478bd9Sstevel@tonic-gate return (NDI_FAILURE);
55727c478bd9Sstevel@tonic-gate
55737c478bd9Sstevel@tonic-gate /* split name into "name@addr" parts */
55747c478bd9Sstevel@tonic-gate i_ddi_parse_name(devnm, &name, &addr, NULL);
55757c478bd9Sstevel@tonic-gate
55767c478bd9Sstevel@tonic-gate /*
55775e3986cbScth * If the nexus is a pHCI and we are not processing a pHCI from
55785e3986cbScth * mdi bus_config code then we need to know the vHCI.
55797c478bd9Sstevel@tonic-gate */
55805e3986cbScth if (MDI_PHCI(pdip))
55815e3986cbScth vdip = mdi_devi_get_vdip(pdip);
55825e3986cbScth
55835e3986cbScth /*
55845e3986cbScth * We may have a genericname on a system that creates drivername
55855e3986cbScth * nodes (from .conf files). Find the drivername by nodeid. If we
55865e3986cbScth * can't find a node with devnm as the node name then we search by
55875e3986cbScth * drivername. This allows an implementation to supply a genericly
55884c96bfe1Scth * named boot path (disk) and locate drivename nodes (sd). The
55894c96bfe1Scth * NDI_PROMNAME flag does not apply to /devices/pseudo paths.
55905e3986cbScth */
55914c96bfe1Scth if ((flags & NDI_PROMNAME) && (pdip != pseudo_dip)) {
55927c478bd9Sstevel@tonic-gate drivername = child_path_to_driver(pdip, name, addr);
5593f4da9be0Scth find_by_addr = 1;
5594f4da9be0Scth }
55957c478bd9Sstevel@tonic-gate
5596144dfaa9Scth /*
55975e3986cbScth * Determine end_time: This routine should *not* be called with a
55985e3986cbScth * constant non-zero timeout argument, the caller should be adjusting
55995e3986cbScth * the timeout argument relative to when it *started* its asynchronous
56005e3986cbScth * enumeration.
5601144dfaa9Scth */
56025e3986cbScth if (timeout > 0)
56035e3986cbScth end_time = ddi_get_lbolt() + timeout;
5604c73a93f2Sdm120769
56055e3986cbScth for (;;) {
56065e3986cbScth /*
56075e3986cbScth * For pHCI, enter (vHCI, pHCI) and search for pathinfo/client
56085e3986cbScth * child - break out of for(;;) loop if child found.
56095e3986cbScth * NOTE: Lock order for ndi_devi_enter is (vHCI, pHCI).
56105e3986cbScth */
56115e3986cbScth if (vdip) {
56125e3986cbScth /* use mdi_devi_enter ordering */
56133fe80ca4SDan Cross ndi_devi_enter(vdip);
56143fe80ca4SDan Cross ndi_devi_enter(pdip);
56155e3986cbScth cpip = mdi_pi_find(pdip, NULL, addr);
56165e3986cbScth cdip = mdi_pi_get_client(cpip);
56175e3986cbScth if (cdip)
56185e3986cbScth break;
56195e3986cbScth } else
56203fe80ca4SDan Cross ndi_devi_enter(pdip);
56215e3986cbScth
56225e3986cbScth /*
56235e3986cbScth * When not a vHCI or not all pHCI devices are required to
56245e3986cbScth * enumerated under the vHCI (NDI_MDI_FALLBACK) search for
56255e3986cbScth * devinfo child.
56265e3986cbScth */
56275e3986cbScth if ((vdip == NULL) || (flags & NDI_MDI_FALLBACK)) {
56285e3986cbScth /* determine if .conf nodes already built */
56295e3986cbScth probed = (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
56305e3986cbScth
56315e3986cbScth /*
56325e3986cbScth * Search for child by name, if not found then search
56335e3986cbScth * for a node bound to the drivername driver with the
56345e3986cbScth * specified "@addr". Break out of for(;;) loop if
5635f4da9be0Scth * child found. To support path-oriented aliases
5636f4da9be0Scth * binding on boot-device, we do a search_by_addr too.
56375e3986cbScth */
56385e3986cbScth again: (void) i_ndi_make_spec_children(pdip, flags);
56395e3986cbScth cdip = find_child_by_name(pdip, name, addr);
56405e3986cbScth if ((cdip == NULL) && drivername)
56415e3986cbScth cdip = find_child_by_driver(pdip,
56425e3986cbScth drivername, addr);
5643f4da9be0Scth if ((cdip == NULL) && find_by_addr)
5644f4da9be0Scth cdip = find_child_by_addr(pdip, addr);
56455e3986cbScth if (cdip)
56467c478bd9Sstevel@tonic-gate break;
56477c478bd9Sstevel@tonic-gate
56487c478bd9Sstevel@tonic-gate /*
56495e3986cbScth * determine if we should reenumerate .conf nodes
56505e3986cbScth * and look for child again.
56517c478bd9Sstevel@tonic-gate */
56525e3986cbScth if (probed &&
56535e3986cbScth i_ddi_io_initialized() &&
56545e3986cbScth (flags & NDI_CONFIG_REPROBE) &&
56555e3986cbScth ((timeout <= 0) || (ddi_get_lbolt() >= end_time))) {
56565e3986cbScth probed = 0;
56575e3986cbScth mutex_enter(&DEVI(pdip)->devi_lock);
56585e3986cbScth DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
56595e3986cbScth mutex_exit(&DEVI(pdip)->devi_lock);
56605e3986cbScth goto again;
56615e3986cbScth }
56625e3986cbScth }
56635e3986cbScth
56645e3986cbScth /* break out of for(;;) if time expired */
56655e3986cbScth if ((timeout <= 0) || (ddi_get_lbolt() >= end_time))
56665e3986cbScth break;
56675e3986cbScth
56685e3986cbScth /*
56695e3986cbScth * Child not found, exit and wait for asynchronous enumeration
56705e3986cbScth * to add child (or timeout). The addition of a new child (vhci
56715e3986cbScth * or phci) requires the asynchronous enumeration thread to
56725e3986cbScth * ndi_devi_enter/ndi_devi_exit. This exit will signal devi_cv
56735e3986cbScth * and cause us to return from ndi_devi_exit_and_wait, after
56745e3986cbScth * which we loop and search for the requested child again.
56755e3986cbScth */
56767c478bd9Sstevel@tonic-gate NDI_DEBUG(flags, (CE_CONT,
56777c478bd9Sstevel@tonic-gate "%s%d: waiting for child %s@%s, timeout %ld",
56787c478bd9Sstevel@tonic-gate ddi_driver_name(pdip), ddi_get_instance(pdip),
56797c478bd9Sstevel@tonic-gate name, addr, timeout));
56805e3986cbScth if (vdip) {
5681c73a93f2Sdm120769 /*
56825e3986cbScth * Mark vHCI for pHCI ndi_devi_exit broadcast.
5683c73a93f2Sdm120769 */
56845e3986cbScth mutex_enter(&DEVI(vdip)->devi_lock);
56855e3986cbScth DEVI(vdip)->devi_flags |=
56865e3986cbScth DEVI_PHCI_SIGNALS_VHCI;
56875e3986cbScth mutex_exit(&DEVI(vdip)->devi_lock);
56883fe80ca4SDan Cross ndi_devi_exit(pdip);
56895e3986cbScth
56905e3986cbScth /*
56915e3986cbScth * NB: There is a small race window from above
56925e3986cbScth * ndi_devi_exit() of pdip to cv_wait() in
56935e3986cbScth * ndi_devi_exit_and_wait() which can result in
56945e3986cbScth * not immediately finding a new pHCI child
56955e3986cbScth * of a pHCI that uses NDI_MDI_FAILBACK.
56965e3986cbScth */
5697d27aea3fSDan Cross ndi_devi_exit_and_wait(vdip, end_time);
56985e3986cbScth } else {
5699d27aea3fSDan Cross ndi_devi_exit_and_wait(pdip, end_time);
57005e3986cbScth }
5701c73a93f2Sdm120769 }
5702c73a93f2Sdm120769
57035e3986cbScth /* done with paddr, fixup i_ddi_parse_name '@'->'\0' change */
57045e3986cbScth if (addr && *addr != '\0')
57057c478bd9Sstevel@tonic-gate *(addr - 1) = '@';
57067c478bd9Sstevel@tonic-gate
57075e3986cbScth /* attach and hold the child, returning pointer to child */
57085e3986cbScth if (cdip && (devi_attach_node(cdip, flags) == NDI_SUCCESS)) {
57095e3986cbScth ndi_hold_devi(cdip);
57105e3986cbScth *cdipp = cdip;
57117c478bd9Sstevel@tonic-gate }
57127c478bd9Sstevel@tonic-gate
57133fe80ca4SDan Cross ndi_devi_exit(pdip);
57145e3986cbScth if (vdip)
57153fe80ca4SDan Cross ndi_devi_exit(vdip);
57165e3986cbScth return (*cdipp ? NDI_SUCCESS : NDI_FAILURE);
57177c478bd9Sstevel@tonic-gate }
57187c478bd9Sstevel@tonic-gate
57197c478bd9Sstevel@tonic-gate /*
57207c478bd9Sstevel@tonic-gate * Enumerate and attach a child specified by name 'devnm'.
57217c478bd9Sstevel@tonic-gate * Called by devfs lookup and DR to perform a BUS_CONFIG_ONE.
57227c478bd9Sstevel@tonic-gate * Note: devfs does not make use of NDI_CONFIG to configure
57237c478bd9Sstevel@tonic-gate * an entire branch.
57247c478bd9Sstevel@tonic-gate */
57257c478bd9Sstevel@tonic-gate int
ndi_devi_config_one(dev_info_t * pdip,char * devnm,dev_info_t ** dipp,int flags)572694c894bbSVikram Hegde ndi_devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **dipp, int flags)
57277c478bd9Sstevel@tonic-gate {
57287c478bd9Sstevel@tonic-gate int error;
57297c478bd9Sstevel@tonic-gate int (*f)();
573094c894bbSVikram Hegde char *nmdup;
573194c894bbSVikram Hegde int duplen;
57327c478bd9Sstevel@tonic-gate int branch_event = 0;
57337c478bd9Sstevel@tonic-gate
573494c894bbSVikram Hegde ASSERT(pdip);
573594c894bbSVikram Hegde ASSERT(devnm);
57367c478bd9Sstevel@tonic-gate ASSERT(dipp);
573794c894bbSVikram Hegde ASSERT(i_ddi_devi_attached(pdip));
57387c478bd9Sstevel@tonic-gate
57397c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT,
57407c478bd9Sstevel@tonic-gate "ndi_devi_config_one: par = %s%d (%p), child = %s\n",
574194c894bbSVikram Hegde ddi_driver_name(pdip), ddi_get_instance(pdip),
574294c894bbSVikram Hegde (void *)pdip, devnm));
57437c478bd9Sstevel@tonic-gate
574494c894bbSVikram Hegde *dipp = NULL;
574594c894bbSVikram Hegde
574694c894bbSVikram Hegde if (pm_pre_config(pdip, devnm) != DDI_SUCCESS) {
574794c894bbSVikram Hegde cmn_err(CE_WARN, "preconfig failed: %s", devnm);
57487c478bd9Sstevel@tonic-gate return (NDI_FAILURE);
574994c894bbSVikram Hegde }
57507c478bd9Sstevel@tonic-gate
57517c478bd9Sstevel@tonic-gate if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
57527c478bd9Sstevel@tonic-gate (flags & NDI_CONFIG)) {
57537c478bd9Sstevel@tonic-gate flags |= NDI_BRANCH_EVENT_OP;
57547c478bd9Sstevel@tonic-gate branch_event = 1;
57557c478bd9Sstevel@tonic-gate }
57567c478bd9Sstevel@tonic-gate
575794c894bbSVikram Hegde nmdup = strdup(devnm);
575894c894bbSVikram Hegde duplen = strlen(devnm) + 1;
575994c894bbSVikram Hegde
576094c894bbSVikram Hegde if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
576194c894bbSVikram Hegde (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
576294c894bbSVikram Hegde (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
576394c894bbSVikram Hegde error = devi_config_one(pdip, devnm, dipp, flags, 0);
57647c478bd9Sstevel@tonic-gate } else {
57657c478bd9Sstevel@tonic-gate /* call bus_config entry point */
576694c894bbSVikram Hegde error = (*f)(pdip, flags, BUS_CONFIG_ONE, (void *)devnm, dipp);
57677c478bd9Sstevel@tonic-gate }
57687c478bd9Sstevel@tonic-gate
576994c894bbSVikram Hegde if (error) {
577094c894bbSVikram Hegde *dipp = NULL;
577194c894bbSVikram Hegde }
577294c894bbSVikram Hegde
577394c894bbSVikram Hegde /*
577494c894bbSVikram Hegde * if we fail to lookup and this could be an alias, lookup currdip
577594c894bbSVikram Hegde * To prevent recursive lookups into the same hash table, only
577694c894bbSVikram Hegde * do the currdip lookups once the hash table init is complete.
577794c894bbSVikram Hegde * Use tsd so that redirection doesn't recurse
577894c894bbSVikram Hegde */
577994c894bbSVikram Hegde if (error) {
578094c894bbSVikram Hegde char *alias = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
578194c894bbSVikram Hegde if (alias == NULL) {
578294c894bbSVikram Hegde ddi_err(DER_PANIC, pdip, "alias alloc failed: %s",
578394c894bbSVikram Hegde nmdup);
578494c894bbSVikram Hegde }
578594c894bbSVikram Hegde (void) ddi_pathname(pdip, alias);
578694c894bbSVikram Hegde (void) strlcat(alias, "/", MAXPATHLEN);
578794c894bbSVikram Hegde (void) strlcat(alias, nmdup, MAXPATHLEN);
578894c894bbSVikram Hegde
578994c894bbSVikram Hegde *dipp = ddi_alias_redirect(alias);
579094c894bbSVikram Hegde error = (*dipp ? NDI_SUCCESS : NDI_FAILURE);
579194c894bbSVikram Hegde
579294c894bbSVikram Hegde kmem_free(alias, MAXPATHLEN);
579394c894bbSVikram Hegde }
579494c894bbSVikram Hegde kmem_free(nmdup, duplen);
579594c894bbSVikram Hegde
579694c894bbSVikram Hegde if (error || !(flags & NDI_CONFIG)) {
579794c894bbSVikram Hegde pm_post_config(pdip, devnm);
57987c478bd9Sstevel@tonic-gate return (error);
57997c478bd9Sstevel@tonic-gate }
58007c478bd9Sstevel@tonic-gate
58017c478bd9Sstevel@tonic-gate /*
5802f4da9be0Scth * DR usage (i.e. call with NDI_CONFIG) recursively configures
58037c478bd9Sstevel@tonic-gate * grandchildren, performing a BUS_CONFIG_ALL from the node attached
58047c478bd9Sstevel@tonic-gate * by the BUS_CONFIG_ONE.
58057c478bd9Sstevel@tonic-gate */
58067c478bd9Sstevel@tonic-gate ASSERT(*dipp);
5807a204de77Scth error = devi_config_common(*dipp, flags, DDI_MAJOR_T_NONE);
58087c478bd9Sstevel@tonic-gate
580994c894bbSVikram Hegde pm_post_config(pdip, devnm);
58107c478bd9Sstevel@tonic-gate
58117c478bd9Sstevel@tonic-gate if (branch_event)
58127c478bd9Sstevel@tonic-gate (void) i_log_devfs_branch_add(*dipp);
58137c478bd9Sstevel@tonic-gate
58147c478bd9Sstevel@tonic-gate return (error);
58157c478bd9Sstevel@tonic-gate }
58167c478bd9Sstevel@tonic-gate
58177c478bd9Sstevel@tonic-gate /*
58187c478bd9Sstevel@tonic-gate * Enumerate and attach a child specified by name 'devnm'.
58197c478bd9Sstevel@tonic-gate * Called during configure the OBP options. This configures
58207c478bd9Sstevel@tonic-gate * only one node.
58217c478bd9Sstevel@tonic-gate */
58227c478bd9Sstevel@tonic-gate static int
ndi_devi_config_obp_args(dev_info_t * parent,char * devnm,dev_info_t ** childp,int flags)58237c478bd9Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
58247c478bd9Sstevel@tonic-gate dev_info_t **childp, int flags)
58257c478bd9Sstevel@tonic-gate {
58267c478bd9Sstevel@tonic-gate int error;
58277c478bd9Sstevel@tonic-gate int (*f)();
58287c478bd9Sstevel@tonic-gate
58297c478bd9Sstevel@tonic-gate ASSERT(childp);
5830737d277aScth ASSERT(i_ddi_devi_attached(parent));
58317c478bd9Sstevel@tonic-gate
58327c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_config_obp_args: "
58337c478bd9Sstevel@tonic-gate "par = %s%d (%p), child = %s\n", ddi_driver_name(parent),
58347c478bd9Sstevel@tonic-gate ddi_get_instance(parent), (void *)parent, devnm));
58357c478bd9Sstevel@tonic-gate
58367c478bd9Sstevel@tonic-gate if ((DEVI(parent)->devi_ops->devo_bus_ops == NULL) ||
58377c478bd9Sstevel@tonic-gate (DEVI(parent)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
58387c478bd9Sstevel@tonic-gate (f = DEVI(parent)->devi_ops->devo_bus_ops->bus_config) == NULL) {
58397c478bd9Sstevel@tonic-gate error = NDI_FAILURE;
58407c478bd9Sstevel@tonic-gate } else {
58417c478bd9Sstevel@tonic-gate /* call bus_config entry point */
58427c478bd9Sstevel@tonic-gate error = (*f)(parent, flags,
58437c478bd9Sstevel@tonic-gate BUS_CONFIG_OBP_ARGS, (void *)devnm, childp);
58447c478bd9Sstevel@tonic-gate }
58457c478bd9Sstevel@tonic-gate return (error);
58467c478bd9Sstevel@tonic-gate }
58477c478bd9Sstevel@tonic-gate
584825e8c5aaSvikram /*
584925e8c5aaSvikram * Pay attention, the following is a bit tricky:
585025e8c5aaSvikram * There are three possible cases when constraints are applied
585125e8c5aaSvikram *
585225e8c5aaSvikram * - A constraint is applied and the offline is disallowed.
585325e8c5aaSvikram * Simply return failure and block the offline
585425e8c5aaSvikram *
585525e8c5aaSvikram * - A constraint is applied and the offline is allowed.
585625e8c5aaSvikram * Mark the dip as having passed the constraint and allow
585725e8c5aaSvikram * offline to proceed.
585825e8c5aaSvikram *
585925e8c5aaSvikram * - A constraint is not applied. Allow the offline to proceed for now.
586025e8c5aaSvikram *
586125e8c5aaSvikram * In the latter two cases we allow the offline to proceed. If the
586225e8c5aaSvikram * offline succeeds (no users) everything is fine. It is ok for an unused
586325e8c5aaSvikram * device to be offlined even if no constraints were imposed on the offline.
586425e8c5aaSvikram * If the offline fails because there are users, we look at the constraint
586525e8c5aaSvikram * flag on the dip. If the constraint flag is set (implying that it passed
586625e8c5aaSvikram * a constraint) we allow the dip to be retired. If not, we don't allow
586725e8c5aaSvikram * the retire. This ensures that we don't allow unconstrained retire.
586825e8c5aaSvikram */
586925e8c5aaSvikram int
e_ddi_offline_notify(dev_info_t * dip)587025e8c5aaSvikram e_ddi_offline_notify(dev_info_t *dip)
587125e8c5aaSvikram {
587225e8c5aaSvikram int retval;
587325e8c5aaSvikram int constraint;
587425e8c5aaSvikram int failure;
587525e8c5aaSvikram
587625e8c5aaSvikram RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): entered: dip=%p",
587725e8c5aaSvikram (void *) dip));
587825e8c5aaSvikram
587925e8c5aaSvikram constraint = 0;
588025e8c5aaSvikram failure = 0;
588125e8c5aaSvikram
588225e8c5aaSvikram /*
588325e8c5aaSvikram * Start with userland constraints first - applied via device contracts
588425e8c5aaSvikram */
588525e8c5aaSvikram retval = contract_device_offline(dip, DDI_DEV_T_ANY, 0);
588625e8c5aaSvikram switch (retval) {
588725e8c5aaSvikram case CT_NACK:
588825e8c5aaSvikram RIO_DEBUG((CE_NOTE, "Received NACK for dip=%p", (void *)dip));
588925e8c5aaSvikram failure = 1;
589025e8c5aaSvikram goto out;
589125e8c5aaSvikram case CT_ACK:
589225e8c5aaSvikram constraint = 1;
589325e8c5aaSvikram RIO_DEBUG((CE_NOTE, "Received ACK for dip=%p", (void *)dip));
589425e8c5aaSvikram break;
589525e8c5aaSvikram case CT_NONE:
589625e8c5aaSvikram /* no contracts */
589725e8c5aaSvikram RIO_DEBUG((CE_NOTE, "No contracts on dip=%p", (void *)dip));
589825e8c5aaSvikram break;
589925e8c5aaSvikram default:
590025e8c5aaSvikram ASSERT(retval == CT_NONE);
590125e8c5aaSvikram }
590225e8c5aaSvikram
590325e8c5aaSvikram /*
590425e8c5aaSvikram * Next, use LDI to impose kernel constraints
590525e8c5aaSvikram */
590625e8c5aaSvikram retval = ldi_invoke_notify(dip, DDI_DEV_T_ANY, 0, LDI_EV_OFFLINE, NULL);
590725e8c5aaSvikram switch (retval) {
590825e8c5aaSvikram case LDI_EV_FAILURE:
590925e8c5aaSvikram contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_FAILURE);
591025e8c5aaSvikram RIO_DEBUG((CE_NOTE, "LDI callback failed on dip=%p",
591125e8c5aaSvikram (void *)dip));
591225e8c5aaSvikram failure = 1;
591325e8c5aaSvikram goto out;
591425e8c5aaSvikram case LDI_EV_SUCCESS:
591525e8c5aaSvikram constraint = 1;
591625e8c5aaSvikram RIO_DEBUG((CE_NOTE, "LDI callback success on dip=%p",
591725e8c5aaSvikram (void *)dip));
591825e8c5aaSvikram break;
591925e8c5aaSvikram case LDI_EV_NONE:
592025e8c5aaSvikram /* no matching LDI callbacks */
592125e8c5aaSvikram RIO_DEBUG((CE_NOTE, "No LDI callbacks for dip=%p",
592225e8c5aaSvikram (void *)dip));
592325e8c5aaSvikram break;
592425e8c5aaSvikram default:
592525e8c5aaSvikram ASSERT(retval == LDI_EV_NONE);
592625e8c5aaSvikram }
592725e8c5aaSvikram
592825e8c5aaSvikram out:
592925e8c5aaSvikram mutex_enter(&(DEVI(dip)->devi_lock));
593025e8c5aaSvikram if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && failure) {
593125e8c5aaSvikram RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
593225e8c5aaSvikram "BLOCKED flag. dip=%p", (void *)dip));
593325e8c5aaSvikram DEVI(dip)->devi_flags |= DEVI_R_BLOCKED;
593425e8c5aaSvikram if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
593525e8c5aaSvikram RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): "
593625e8c5aaSvikram "blocked. clearing RCM CONSTRAINT flag. dip=%p",
593725e8c5aaSvikram (void *)dip));
593825e8c5aaSvikram DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
593925e8c5aaSvikram }
594025e8c5aaSvikram } else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && constraint) {
594125e8c5aaSvikram RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
594225e8c5aaSvikram "CONSTRAINT flag. dip=%p", (void *)dip));
594325e8c5aaSvikram DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
594425e8c5aaSvikram } else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) &&
5945bf002425SStephen Hanson ((DEVI(dip)->devi_ops != NULL &&
5946bf002425SStephen Hanson DEVI(dip)->devi_ops->devo_bus_ops != NULL) ||
5947bf002425SStephen Hanson DEVI(dip)->devi_ref == 0)) {
5948bf002425SStephen Hanson /* also allow retire if nexus or if device is not in use */
594925e8c5aaSvikram RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): device not in "
595025e8c5aaSvikram "use. Setting CONSTRAINT flag. dip=%p", (void *)dip));
595125e8c5aaSvikram DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
595225e8c5aaSvikram } else {
595325e8c5aaSvikram /*
595425e8c5aaSvikram * Note: We cannot ASSERT here that DEVI_R_CONSTRAINT is
595525e8c5aaSvikram * not set, since other sources (such as RCM) may have
595625e8c5aaSvikram * set the flag.
595725e8c5aaSvikram */
595825e8c5aaSvikram RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): not setting "
595925e8c5aaSvikram "constraint flag. dip=%p", (void *)dip));
596025e8c5aaSvikram }
596125e8c5aaSvikram mutex_exit(&(DEVI(dip)->devi_lock));
596225e8c5aaSvikram
596325e8c5aaSvikram
596425e8c5aaSvikram RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): exit: dip=%p",
596525e8c5aaSvikram (void *) dip));
596625e8c5aaSvikram
596725e8c5aaSvikram return (failure ? DDI_FAILURE : DDI_SUCCESS);
596825e8c5aaSvikram }
596925e8c5aaSvikram
597025e8c5aaSvikram void
e_ddi_offline_finalize(dev_info_t * dip,int result)597125e8c5aaSvikram e_ddi_offline_finalize(dev_info_t *dip, int result)
597225e8c5aaSvikram {
597325e8c5aaSvikram RIO_DEBUG((CE_NOTE, "e_ddi_offline_finalize(): entry: result=%s, "
597425e8c5aaSvikram "dip=%p", result == DDI_SUCCESS ? "SUCCESS" : "FAILURE",
597525e8c5aaSvikram (void *)dip));
597625e8c5aaSvikram
597725e8c5aaSvikram contract_device_negend(dip, DDI_DEV_T_ANY, 0, result == DDI_SUCCESS ?
597825e8c5aaSvikram CT_EV_SUCCESS : CT_EV_FAILURE);
597925e8c5aaSvikram
598025e8c5aaSvikram ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0,
598125e8c5aaSvikram LDI_EV_OFFLINE, result == DDI_SUCCESS ?
598225e8c5aaSvikram LDI_EV_SUCCESS : LDI_EV_FAILURE, NULL);
598325e8c5aaSvikram
598425e8c5aaSvikram RIO_VERBOSE((CE_NOTE, "e_ddi_offline_finalize(): exit: dip=%p",
598525e8c5aaSvikram (void *)dip));
598625e8c5aaSvikram }
598725e8c5aaSvikram
598825e8c5aaSvikram void
e_ddi_degrade_finalize(dev_info_t * dip)598925e8c5aaSvikram e_ddi_degrade_finalize(dev_info_t *dip)
599025e8c5aaSvikram {
599125e8c5aaSvikram RIO_DEBUG((CE_NOTE, "e_ddi_degrade_finalize(): entry: "
599225e8c5aaSvikram "result always = DDI_SUCCESS, dip=%p", (void *)dip));
599325e8c5aaSvikram
599425e8c5aaSvikram contract_device_degrade(dip, DDI_DEV_T_ANY, 0);
599525e8c5aaSvikram contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
599625e8c5aaSvikram
599725e8c5aaSvikram ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0, LDI_EV_DEGRADE,
599825e8c5aaSvikram LDI_EV_SUCCESS, NULL);
599925e8c5aaSvikram
600025e8c5aaSvikram RIO_VERBOSE((CE_NOTE, "e_ddi_degrade_finalize(): exit: dip=%p",
600125e8c5aaSvikram (void *)dip));
600225e8c5aaSvikram }
600325e8c5aaSvikram
600425e8c5aaSvikram void
e_ddi_undegrade_finalize(dev_info_t * dip)600525e8c5aaSvikram e_ddi_undegrade_finalize(dev_info_t *dip)
600625e8c5aaSvikram {
600725e8c5aaSvikram RIO_DEBUG((CE_NOTE, "e_ddi_undegrade_finalize(): entry: "
600825e8c5aaSvikram "result always = DDI_SUCCESS, dip=%p", (void *)dip));
600925e8c5aaSvikram
601025e8c5aaSvikram contract_device_undegrade(dip, DDI_DEV_T_ANY, 0);
601125e8c5aaSvikram contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
601225e8c5aaSvikram
601325e8c5aaSvikram RIO_VERBOSE((CE_NOTE, "e_ddi_undegrade_finalize(): exit: dip=%p",
601425e8c5aaSvikram (void *)dip));
601525e8c5aaSvikram }
60167c478bd9Sstevel@tonic-gate
60177c478bd9Sstevel@tonic-gate /*
60187c478bd9Sstevel@tonic-gate * detach a node with parent already held busy
60197c478bd9Sstevel@tonic-gate */
60207c478bd9Sstevel@tonic-gate static int
devi_detach_node(dev_info_t * dip,uint_t flags)60217c478bd9Sstevel@tonic-gate devi_detach_node(dev_info_t *dip, uint_t flags)
60227c478bd9Sstevel@tonic-gate {
60237c478bd9Sstevel@tonic-gate dev_info_t *pdip = ddi_get_parent(dip);
60247c478bd9Sstevel@tonic-gate int ret = NDI_SUCCESS;
60257c478bd9Sstevel@tonic-gate ddi_eventcookie_t cookie;
60266c75e108SVikram Hegde char *path = NULL;
60276c75e108SVikram Hegde char *class = NULL;
60286c75e108SVikram Hegde char *driver = NULL;
60296c75e108SVikram Hegde int instance = -1;
60306c75e108SVikram Hegde int post_event = 0;
60317c478bd9Sstevel@tonic-gate
60325e3986cbScth ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
60335e3986cbScth
603425e8c5aaSvikram /*
603525e8c5aaSvikram * Invoke notify if offlining
603625e8c5aaSvikram */
603725e8c5aaSvikram if (flags & NDI_DEVI_OFFLINE) {
603825e8c5aaSvikram RIO_DEBUG((CE_NOTE, "devi_detach_node: offlining dip=%p",
603925e8c5aaSvikram (void *)dip));
604025e8c5aaSvikram if (e_ddi_offline_notify(dip) != DDI_SUCCESS) {
604125e8c5aaSvikram RIO_DEBUG((CE_NOTE, "devi_detach_node: offline NACKed"
604225e8c5aaSvikram "dip=%p", (void *)dip));
604325e8c5aaSvikram return (NDI_FAILURE);
604425e8c5aaSvikram }
604525e8c5aaSvikram }
604625e8c5aaSvikram
60477c478bd9Sstevel@tonic-gate if (flags & NDI_POST_EVENT) {
60485e3986cbScth if (i_ddi_devi_attached(pdip)) {
60497c478bd9Sstevel@tonic-gate if (ddi_get_eventcookie(dip, DDI_DEVI_REMOVE_EVENT,
60507c478bd9Sstevel@tonic-gate &cookie) == NDI_SUCCESS)
60517c478bd9Sstevel@tonic-gate (void) ndi_post_event(dip, dip, cookie, NULL);
60527c478bd9Sstevel@tonic-gate }
60537c478bd9Sstevel@tonic-gate }
60547c478bd9Sstevel@tonic-gate
605539cddb10SJoshua M. Clulow /*
605639cddb10SJoshua M. Clulow * dv_mknod places a hold on the dev_info_t for each devfs node
605739cddb10SJoshua M. Clulow * created. If we're to succeed in detaching this device, we must
605839cddb10SJoshua M. Clulow * first release all outstanding references held by devfs.
605939cddb10SJoshua M. Clulow */
606039cddb10SJoshua M. Clulow (void) devfs_clean(pdip, NULL, DV_CLEAN_FORCE);
606139cddb10SJoshua M. Clulow
606225e8c5aaSvikram if (i_ddi_detachchild(dip, flags) != DDI_SUCCESS) {
606325e8c5aaSvikram if (flags & NDI_DEVI_OFFLINE) {
606425e8c5aaSvikram RIO_DEBUG((CE_NOTE, "devi_detach_node: offline failed."
606525e8c5aaSvikram " Calling e_ddi_offline_finalize with result=%d. "
606625e8c5aaSvikram "dip=%p", DDI_FAILURE, (void *)dip));
606725e8c5aaSvikram e_ddi_offline_finalize(dip, DDI_FAILURE);
606825e8c5aaSvikram }
60697c478bd9Sstevel@tonic-gate return (NDI_FAILURE);
607025e8c5aaSvikram }
607125e8c5aaSvikram
607225e8c5aaSvikram if (flags & NDI_DEVI_OFFLINE) {
607325e8c5aaSvikram RIO_DEBUG((CE_NOTE, "devi_detach_node: offline succeeded."
607425e8c5aaSvikram " Calling e_ddi_offline_finalize with result=%d, "
607525e8c5aaSvikram "dip=%p", DDI_SUCCESS, (void *)dip));
607625e8c5aaSvikram e_ddi_offline_finalize(dip, DDI_SUCCESS);
607725e8c5aaSvikram }
60787c478bd9Sstevel@tonic-gate
60797c478bd9Sstevel@tonic-gate if (flags & NDI_AUTODETACH)
60807c478bd9Sstevel@tonic-gate return (NDI_SUCCESS);
60817c478bd9Sstevel@tonic-gate
60827c478bd9Sstevel@tonic-gate /*
60837c478bd9Sstevel@tonic-gate * For DR, even bound nodes may need to have offline
60847c478bd9Sstevel@tonic-gate * flag set.
60857c478bd9Sstevel@tonic-gate */
60867c478bd9Sstevel@tonic-gate if (flags & NDI_DEVI_OFFLINE) {
608716747f41Scth mutex_enter(&(DEVI(dip)->devi_lock));
60887c478bd9Sstevel@tonic-gate DEVI_SET_DEVICE_OFFLINE(dip);
608916747f41Scth mutex_exit(&(DEVI(dip)->devi_lock));
60907c478bd9Sstevel@tonic-gate }
60917c478bd9Sstevel@tonic-gate
60927c478bd9Sstevel@tonic-gate if (i_ddi_node_state(dip) == DS_INITIALIZED) {
6093779f1d69SMichael Bergknoff struct dev_info *devi = DEVI(dip);
6094779f1d69SMichael Bergknoff
6095779f1d69SMichael Bergknoff if (devi->devi_ev_path == NULL) {
6096779f1d69SMichael Bergknoff devi->devi_ev_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6097779f1d69SMichael Bergknoff (void) ddi_pathname(dip, devi->devi_ev_path);
6098779f1d69SMichael Bergknoff }
60997c478bd9Sstevel@tonic-gate if (flags & NDI_DEVI_OFFLINE)
6100779f1d69SMichael Bergknoff i_ndi_devi_report_status_change(dip,
6101779f1d69SMichael Bergknoff devi->devi_ev_path);
61027c478bd9Sstevel@tonic-gate
61037c478bd9Sstevel@tonic-gate if (need_remove_event(dip, flags)) {
6104779f1d69SMichael Bergknoff /*
6105779f1d69SMichael Bergknoff * instance and path data are lost in call to
6106779f1d69SMichael Bergknoff * ddi_uninitchild
6107779f1d69SMichael Bergknoff */
6108779f1d69SMichael Bergknoff devi->devi_ev_instance = ddi_get_instance(dip);
61096c75e108SVikram Hegde
611016747f41Scth mutex_enter(&(DEVI(dip)->devi_lock));
61117c478bd9Sstevel@tonic-gate DEVI_SET_EVREMOVE(dip);
611216747f41Scth mutex_exit(&(DEVI(dip)->devi_lock));
61137c478bd9Sstevel@tonic-gate }
61147c478bd9Sstevel@tonic-gate }
61157c478bd9Sstevel@tonic-gate
61167c478bd9Sstevel@tonic-gate if (flags & (NDI_UNCONFIG | NDI_DEVI_REMOVE)) {
61177c478bd9Sstevel@tonic-gate ret = ddi_uninitchild(dip);
61187c478bd9Sstevel@tonic-gate if (ret == NDI_SUCCESS) {
61197c478bd9Sstevel@tonic-gate /*
61207c478bd9Sstevel@tonic-gate * Remove uninitialized pseudo nodes because
61217c478bd9Sstevel@tonic-gate * system props are lost and the node cannot be
61227c478bd9Sstevel@tonic-gate * reattached.
61237c478bd9Sstevel@tonic-gate */
61247c478bd9Sstevel@tonic-gate if (!ndi_dev_is_persistent_node(dip))
61257c478bd9Sstevel@tonic-gate flags |= NDI_DEVI_REMOVE;
61267c478bd9Sstevel@tonic-gate
61276c75e108SVikram Hegde if (flags & NDI_DEVI_REMOVE) {
6128328d222bSChris Horne /*
6129328d222bSChris Horne * NOTE: If there is a consumer of LDI events,
6130328d222bSChris Horne * ddi_uninitchild above would have failed
6131328d222bSChris Horne * because of active devi_ref from ldi_open().
6132328d222bSChris Horne */
6133328d222bSChris Horne
6134779f1d69SMichael Bergknoff if (DEVI_EVREMOVE(dip)) {
6135779f1d69SMichael Bergknoff path = i_ddi_strdup(
6136779f1d69SMichael Bergknoff DEVI(dip)->devi_ev_path,
6137779f1d69SMichael Bergknoff KM_SLEEP);
6138779f1d69SMichael Bergknoff class =
6139779f1d69SMichael Bergknoff i_ddi_strdup(i_ddi_devi_class(dip),
6140779f1d69SMichael Bergknoff KM_SLEEP);
6141779f1d69SMichael Bergknoff driver =
6142779f1d69SMichael Bergknoff i_ddi_strdup(
6143779f1d69SMichael Bergknoff (char *)ddi_driver_name(dip),
6144779f1d69SMichael Bergknoff KM_SLEEP);
6145779f1d69SMichael Bergknoff instance = DEVI(dip)->devi_ev_instance;
6146779f1d69SMichael Bergknoff post_event = 1;
6147779f1d69SMichael Bergknoff }
6148779f1d69SMichael Bergknoff
61497c478bd9Sstevel@tonic-gate ret = ddi_remove_child(dip, 0);
61506c75e108SVikram Hegde if (post_event && ret == NDI_SUCCESS) {
6151328d222bSChris Horne /* Generate EC_DEVFS_DEVI_REMOVE */
61526c75e108SVikram Hegde (void) i_log_devfs_remove_devinfo(path,
61536c75e108SVikram Hegde class, driver, instance, flags);
61547c478bd9Sstevel@tonic-gate }
61557c478bd9Sstevel@tonic-gate }
61567c478bd9Sstevel@tonic-gate
61576c75e108SVikram Hegde }
61586c75e108SVikram Hegde }
61596c75e108SVikram Hegde
61606c75e108SVikram Hegde if (path)
6161779f1d69SMichael Bergknoff strfree(path);
61626c75e108SVikram Hegde if (class)
6163779f1d69SMichael Bergknoff strfree(class);
61646c75e108SVikram Hegde if (driver)
6165779f1d69SMichael Bergknoff strfree(driver);
61666c75e108SVikram Hegde
61677c478bd9Sstevel@tonic-gate return (ret);
61687c478bd9Sstevel@tonic-gate }
61697c478bd9Sstevel@tonic-gate
61707c478bd9Sstevel@tonic-gate /*
61717c478bd9Sstevel@tonic-gate * unconfigure immediate children of bus nexus device
61727c478bd9Sstevel@tonic-gate */
61737c478bd9Sstevel@tonic-gate static int
unconfig_immediate_children(dev_info_t * dip,dev_info_t ** dipp,int flags,major_t major)61747c478bd9Sstevel@tonic-gate unconfig_immediate_children(
61757c478bd9Sstevel@tonic-gate dev_info_t *dip,
61767c478bd9Sstevel@tonic-gate dev_info_t **dipp,
61777c478bd9Sstevel@tonic-gate int flags,
61787c478bd9Sstevel@tonic-gate major_t major)
61797c478bd9Sstevel@tonic-gate {
61805e3986cbScth int rv = NDI_SUCCESS;
61817c478bd9Sstevel@tonic-gate dev_info_t *child;
61825e3986cbScth dev_info_t *vdip = NULL;
61835e3986cbScth dev_info_t *next;
61847c478bd9Sstevel@tonic-gate
61857c478bd9Sstevel@tonic-gate ASSERT(dipp == NULL || *dipp == NULL);
61867c478bd9Sstevel@tonic-gate
61875e3986cbScth /*
61885e3986cbScth * Scan forward to see if we will be processing a pHCI child. If we
61895e3986cbScth * have a child that is a pHCI and vHCI and pHCI are not siblings then
61905e3986cbScth * enter vHCI before parent(pHCI) to prevent deadlock with mpxio
61915e3986cbScth * Client power management operations.
61925e3986cbScth */
61933fe80ca4SDan Cross ndi_devi_enter(dip);
61945e3986cbScth for (child = ddi_get_child(dip); child;
61955e3986cbScth child = ddi_get_next_sibling(child)) {
61965e3986cbScth /* skip same nodes we skip below */
6197a204de77Scth if (((major != DDI_MAJOR_T_NONE) &&
61985e3986cbScth (major != ddi_driver_major(child))) ||
61995e3986cbScth ((flags & NDI_AUTODETACH) && !is_leaf_node(child)))
62005e3986cbScth continue;
62015e3986cbScth
62025e3986cbScth if (MDI_PHCI(child)) {
62035e3986cbScth vdip = mdi_devi_get_vdip(child);
62045e3986cbScth /*
62055e3986cbScth * If vHCI and vHCI is not a sibling of pHCI
62065e3986cbScth * then enter in (vHCI, parent(pHCI)) order.
62075e3986cbScth */
62085e3986cbScth if (vdip && (ddi_get_parent(vdip) != dip)) {
62093fe80ca4SDan Cross ndi_devi_exit(dip);
62105e3986cbScth
62115e3986cbScth /* use mdi_devi_enter ordering */
62123fe80ca4SDan Cross ndi_devi_enter(vdip);
62133fe80ca4SDan Cross ndi_devi_enter(dip);
62145e3986cbScth break;
62155e3986cbScth } else
62165e3986cbScth vdip = NULL;
62175e3986cbScth }
62185e3986cbScth }
62195e3986cbScth
62207c478bd9Sstevel@tonic-gate child = ddi_get_child(dip);
62217c478bd9Sstevel@tonic-gate while (child) {
62225e3986cbScth next = ddi_get_next_sibling(child);
62235e3986cbScth
6224a204de77Scth if ((major != DDI_MAJOR_T_NONE) &&
62257c478bd9Sstevel@tonic-gate (major != ddi_driver_major(child))) {
62267c478bd9Sstevel@tonic-gate child = next;
62277c478bd9Sstevel@tonic-gate continue;
62287c478bd9Sstevel@tonic-gate }
62297c478bd9Sstevel@tonic-gate
62307c478bd9Sstevel@tonic-gate /* skip nexus nodes during autodetach */
62317c478bd9Sstevel@tonic-gate if ((flags & NDI_AUTODETACH) && !is_leaf_node(child)) {
62327c478bd9Sstevel@tonic-gate child = next;
62337c478bd9Sstevel@tonic-gate continue;
62347c478bd9Sstevel@tonic-gate }
62357c478bd9Sstevel@tonic-gate
62367c478bd9Sstevel@tonic-gate if (devi_detach_node(child, flags) != NDI_SUCCESS) {
62377c478bd9Sstevel@tonic-gate if (dipp && *dipp == NULL) {
62387c478bd9Sstevel@tonic-gate ndi_hold_devi(child);
62397c478bd9Sstevel@tonic-gate *dipp = child;
62407c478bd9Sstevel@tonic-gate }
62417c478bd9Sstevel@tonic-gate rv = NDI_FAILURE;
62427c478bd9Sstevel@tonic-gate }
62437c478bd9Sstevel@tonic-gate
62447c478bd9Sstevel@tonic-gate /*
62457c478bd9Sstevel@tonic-gate * Continue upon failure--best effort algorithm
62467c478bd9Sstevel@tonic-gate */
62477c478bd9Sstevel@tonic-gate child = next;
62487c478bd9Sstevel@tonic-gate }
62495e3986cbScth
62503fe80ca4SDan Cross ndi_devi_exit(dip);
62515e3986cbScth if (vdip)
62523fe80ca4SDan Cross ndi_devi_exit(vdip);
62535e3986cbScth
62547c478bd9Sstevel@tonic-gate return (rv);
62557c478bd9Sstevel@tonic-gate }
62567c478bd9Sstevel@tonic-gate
62577c478bd9Sstevel@tonic-gate /*
62587c478bd9Sstevel@tonic-gate * unconfigure grand children of bus nexus device
62597c478bd9Sstevel@tonic-gate */
62607c478bd9Sstevel@tonic-gate static int
unconfig_grand_children(dev_info_t * dip,dev_info_t ** dipp,int flags,major_t major,struct brevq_node ** brevqp)62617c478bd9Sstevel@tonic-gate unconfig_grand_children(
62627c478bd9Sstevel@tonic-gate dev_info_t *dip,
62637c478bd9Sstevel@tonic-gate dev_info_t **dipp,
62647c478bd9Sstevel@tonic-gate int flags,
62657c478bd9Sstevel@tonic-gate major_t major,
62667c478bd9Sstevel@tonic-gate struct brevq_node **brevqp)
62677c478bd9Sstevel@tonic-gate {
62687c478bd9Sstevel@tonic-gate struct mt_config_handle *hdl;
62697c478bd9Sstevel@tonic-gate
62707c478bd9Sstevel@tonic-gate if (brevqp)
62717c478bd9Sstevel@tonic-gate *brevqp = NULL;
62727c478bd9Sstevel@tonic-gate
62737c478bd9Sstevel@tonic-gate /* multi-threaded configuration of child nexus */
62747c478bd9Sstevel@tonic-gate hdl = mt_config_init(dip, dipp, flags, major, MT_UNCONFIG_OP, brevqp);
62757c478bd9Sstevel@tonic-gate mt_config_children(hdl);
62767c478bd9Sstevel@tonic-gate
62777c478bd9Sstevel@tonic-gate return (mt_config_fini(hdl)); /* wait for threads to exit */
62787c478bd9Sstevel@tonic-gate }
62797c478bd9Sstevel@tonic-gate
62807c478bd9Sstevel@tonic-gate /*
62817c478bd9Sstevel@tonic-gate * Unconfigure children/descendants of the dip.
62827c478bd9Sstevel@tonic-gate *
62837c478bd9Sstevel@tonic-gate * If brevqp is not NULL, on return *brevqp is set to a queue of dip's
62847c478bd9Sstevel@tonic-gate * child devinames for which branch remove events need to be generated.
62857c478bd9Sstevel@tonic-gate */
62867c478bd9Sstevel@tonic-gate static int
devi_unconfig_common(dev_info_t * dip,dev_info_t ** dipp,int flags,major_t major,struct brevq_node ** brevqp)62877c478bd9Sstevel@tonic-gate devi_unconfig_common(
62887c478bd9Sstevel@tonic-gate dev_info_t *dip,
62897c478bd9Sstevel@tonic-gate dev_info_t **dipp,
62907c478bd9Sstevel@tonic-gate int flags,
62917c478bd9Sstevel@tonic-gate major_t major,
62927c478bd9Sstevel@tonic-gate struct brevq_node **brevqp)
62937c478bd9Sstevel@tonic-gate {
62947c478bd9Sstevel@tonic-gate int rv;
62957c478bd9Sstevel@tonic-gate int pm_cookie;
62967c478bd9Sstevel@tonic-gate int (*f)();
62977c478bd9Sstevel@tonic-gate ddi_bus_config_op_t bus_op;
62987c478bd9Sstevel@tonic-gate
62997c478bd9Sstevel@tonic-gate if (dipp)
63007c478bd9Sstevel@tonic-gate *dipp = NULL;
63017c478bd9Sstevel@tonic-gate if (brevqp)
63027c478bd9Sstevel@tonic-gate *brevqp = NULL;
63037c478bd9Sstevel@tonic-gate
63047c478bd9Sstevel@tonic-gate /*
63057c478bd9Sstevel@tonic-gate * Power up the dip if it is powered off. If the flag bit
63067c478bd9Sstevel@tonic-gate * NDI_AUTODETACH is set and the dip is not at its full power,
63077c478bd9Sstevel@tonic-gate * skip the rest of the branch.
63087c478bd9Sstevel@tonic-gate */
63097c478bd9Sstevel@tonic-gate if (pm_pre_unconfig(dip, flags, &pm_cookie, NULL) != DDI_SUCCESS)
63107c478bd9Sstevel@tonic-gate return ((flags & NDI_AUTODETACH) ? NDI_SUCCESS :
63117c478bd9Sstevel@tonic-gate NDI_FAILURE);
63127c478bd9Sstevel@tonic-gate
63137c478bd9Sstevel@tonic-gate /*
63147c478bd9Sstevel@tonic-gate * Some callers, notably SCSI, need to clear out the devfs
63157c478bd9Sstevel@tonic-gate * cache together with the unconfig to prevent stale entries.
63167c478bd9Sstevel@tonic-gate */
63177c478bd9Sstevel@tonic-gate if (flags & NDI_DEVFS_CLEAN)
63187c478bd9Sstevel@tonic-gate (void) devfs_clean(dip, NULL, 0);
63197c478bd9Sstevel@tonic-gate
63207c478bd9Sstevel@tonic-gate rv = unconfig_grand_children(dip, dipp, flags, major, brevqp);
63217c478bd9Sstevel@tonic-gate
63227c478bd9Sstevel@tonic-gate if ((rv != NDI_SUCCESS) && ((flags & NDI_AUTODETACH) == 0)) {
63237c478bd9Sstevel@tonic-gate if (brevqp && *brevqp) {
63247c478bd9Sstevel@tonic-gate log_and_free_br_events_on_grand_children(dip, *brevqp);
63257c478bd9Sstevel@tonic-gate free_brevq(*brevqp);
63267c478bd9Sstevel@tonic-gate *brevqp = NULL;
63277c478bd9Sstevel@tonic-gate }
63287c478bd9Sstevel@tonic-gate pm_post_unconfig(dip, pm_cookie, NULL);
63297c478bd9Sstevel@tonic-gate return (rv);
63307c478bd9Sstevel@tonic-gate }
63317c478bd9Sstevel@tonic-gate
63327c478bd9Sstevel@tonic-gate if (dipp && *dipp) {
63337c478bd9Sstevel@tonic-gate ndi_rele_devi(*dipp);
63347c478bd9Sstevel@tonic-gate *dipp = NULL;
63357c478bd9Sstevel@tonic-gate }
63367c478bd9Sstevel@tonic-gate
63377c478bd9Sstevel@tonic-gate /*
63387c478bd9Sstevel@tonic-gate * It is possible to have a detached nexus with children
63397c478bd9Sstevel@tonic-gate * and grandchildren (for example: a branch consisting
63407c478bd9Sstevel@tonic-gate * entirely of bound nodes.) Since the nexus is detached
63417c478bd9Sstevel@tonic-gate * the bus_unconfig entry point cannot be used to remove
63427c478bd9Sstevel@tonic-gate * or unconfigure the descendants.
63437c478bd9Sstevel@tonic-gate */
6344737d277aScth if (!i_ddi_devi_attached(dip) ||
63457c478bd9Sstevel@tonic-gate (DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
63467c478bd9Sstevel@tonic-gate (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
63477c478bd9Sstevel@tonic-gate (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
63487c478bd9Sstevel@tonic-gate rv = unconfig_immediate_children(dip, dipp, flags, major);
63497c478bd9Sstevel@tonic-gate } else {
63507c478bd9Sstevel@tonic-gate /*
63517c478bd9Sstevel@tonic-gate * call bus_unconfig entry point
63527c478bd9Sstevel@tonic-gate * It should reset nexus flags if unconfigure succeeds.
63537c478bd9Sstevel@tonic-gate */
6354a204de77Scth bus_op = (major == DDI_MAJOR_T_NONE) ?
63557c478bd9Sstevel@tonic-gate BUS_UNCONFIG_ALL : BUS_UNCONFIG_DRIVER;
63567c478bd9Sstevel@tonic-gate rv = (*f)(dip, flags, bus_op, (void *)(uintptr_t)major);
63577c478bd9Sstevel@tonic-gate }
63587c478bd9Sstevel@tonic-gate
63597c478bd9Sstevel@tonic-gate pm_post_unconfig(dip, pm_cookie, NULL);
63607c478bd9Sstevel@tonic-gate
63617c478bd9Sstevel@tonic-gate if (brevqp && *brevqp)
63627c478bd9Sstevel@tonic-gate cleanup_br_events_on_grand_children(dip, brevqp);
63637c478bd9Sstevel@tonic-gate
63647c478bd9Sstevel@tonic-gate return (rv);
63657c478bd9Sstevel@tonic-gate }
63667c478bd9Sstevel@tonic-gate
63677c478bd9Sstevel@tonic-gate /*
63687c478bd9Sstevel@tonic-gate * called by devfs/framework to unconfigure children bound to major
63697c478bd9Sstevel@tonic-gate * If NDI_AUTODETACH is specified, this is invoked by either the
63707c478bd9Sstevel@tonic-gate * moduninstall daemon or the modunload -i 0 command.
63717c478bd9Sstevel@tonic-gate */
63727c478bd9Sstevel@tonic-gate int
ndi_devi_unconfig_driver(dev_info_t * dip,int flags,major_t major)63737c478bd9Sstevel@tonic-gate ndi_devi_unconfig_driver(dev_info_t *dip, int flags, major_t major)
63747c478bd9Sstevel@tonic-gate {
63757c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT,
63767c478bd9Sstevel@tonic-gate "ndi_devi_unconfig_driver: par = %s%d (%p), flags = 0x%x\n",
63777c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
63787c478bd9Sstevel@tonic-gate
63797c478bd9Sstevel@tonic-gate return (devi_unconfig_common(dip, NULL, flags, major, NULL));
63807c478bd9Sstevel@tonic-gate }
63817c478bd9Sstevel@tonic-gate
63827c478bd9Sstevel@tonic-gate int
ndi_devi_unconfig(dev_info_t * dip,int flags)63837c478bd9Sstevel@tonic-gate ndi_devi_unconfig(dev_info_t *dip, int flags)
63847c478bd9Sstevel@tonic-gate {
63857c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT,
63867c478bd9Sstevel@tonic-gate "ndi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
63877c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
63887c478bd9Sstevel@tonic-gate
6389a204de77Scth return (devi_unconfig_common(dip, NULL, flags, DDI_MAJOR_T_NONE, NULL));
63907c478bd9Sstevel@tonic-gate }
63917c478bd9Sstevel@tonic-gate
63927c478bd9Sstevel@tonic-gate int
e_ddi_devi_unconfig(dev_info_t * dip,dev_info_t ** dipp,int flags)63937c478bd9Sstevel@tonic-gate e_ddi_devi_unconfig(dev_info_t *dip, dev_info_t **dipp, int flags)
63947c478bd9Sstevel@tonic-gate {
63957c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT,
63967c478bd9Sstevel@tonic-gate "e_ddi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
63977c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
63987c478bd9Sstevel@tonic-gate
6399a204de77Scth return (devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE, NULL));
64007c478bd9Sstevel@tonic-gate }
64017c478bd9Sstevel@tonic-gate
64027c478bd9Sstevel@tonic-gate /*
64037c478bd9Sstevel@tonic-gate * Unconfigure child by name
64047c478bd9Sstevel@tonic-gate */
64057c478bd9Sstevel@tonic-gate static int
devi_unconfig_one(dev_info_t * pdip,char * devnm,int flags)64067c478bd9Sstevel@tonic-gate devi_unconfig_one(dev_info_t *pdip, char *devnm, int flags)
64077c478bd9Sstevel@tonic-gate {
64083fe80ca4SDan Cross int rv;
64097c478bd9Sstevel@tonic-gate dev_info_t *child;
64105e3986cbScth dev_info_t *vdip = NULL;
64117c478bd9Sstevel@tonic-gate
64123fe80ca4SDan Cross ndi_devi_enter(pdip);
64137c478bd9Sstevel@tonic-gate child = ndi_devi_findchild(pdip, devnm);
64145e3986cbScth
64155e3986cbScth /*
64165e3986cbScth * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
64175e3986cbScth * before parent(pHCI) to avoid deadlock with mpxio Client power
64185e3986cbScth * management operations.
64195e3986cbScth */
64205e3986cbScth if (child && MDI_PHCI(child)) {
64215e3986cbScth vdip = mdi_devi_get_vdip(child);
64225e3986cbScth if (vdip && (ddi_get_parent(vdip) != pdip)) {
64233fe80ca4SDan Cross ndi_devi_exit(pdip);
64245e3986cbScth
64255e3986cbScth /* use mdi_devi_enter ordering */
64263fe80ca4SDan Cross ndi_devi_enter(vdip);
64273fe80ca4SDan Cross ndi_devi_enter(pdip);
64285e3986cbScth child = ndi_devi_findchild(pdip, devnm);
64295e3986cbScth } else
64305e3986cbScth vdip = NULL;
64315e3986cbScth }
64325e3986cbScth
64335e3986cbScth if (child) {
64345e3986cbScth rv = devi_detach_node(child, flags);
64355e3986cbScth } else {
64367c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT,
64377c478bd9Sstevel@tonic-gate "devi_unconfig_one: %s not found\n", devnm));
64385e3986cbScth rv = NDI_SUCCESS;
6439c73a93f2Sdm120769 }
64405e3986cbScth
64413fe80ca4SDan Cross ndi_devi_exit(pdip);
64425e3986cbScth if (vdip)
64433fe80ca4SDan Cross ndi_devi_exit(vdip);
64445e3986cbScth
64457c478bd9Sstevel@tonic-gate return (rv);
64467c478bd9Sstevel@tonic-gate }
64477c478bd9Sstevel@tonic-gate
64487c478bd9Sstevel@tonic-gate int
ndi_devi_unconfig_one(dev_info_t * pdip,char * devnm,dev_info_t ** dipp,int flags)64497c478bd9Sstevel@tonic-gate ndi_devi_unconfig_one(
64507c478bd9Sstevel@tonic-gate dev_info_t *pdip,
64517c478bd9Sstevel@tonic-gate char *devnm,
64527c478bd9Sstevel@tonic-gate dev_info_t **dipp,
64537c478bd9Sstevel@tonic-gate int flags)
64547c478bd9Sstevel@tonic-gate {
64557c478bd9Sstevel@tonic-gate int (*f)();
64563fe80ca4SDan Cross int rv;
64577c478bd9Sstevel@tonic-gate int pm_cookie;
64587c478bd9Sstevel@tonic-gate dev_info_t *child;
64595e3986cbScth dev_info_t *vdip = NULL;
64607c478bd9Sstevel@tonic-gate struct brevq_node *brevq = NULL;
64617c478bd9Sstevel@tonic-gate
6462737d277aScth ASSERT(i_ddi_devi_attached(pdip));
64637c478bd9Sstevel@tonic-gate
64647c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT,
64657c478bd9Sstevel@tonic-gate "ndi_devi_unconfig_one: par = %s%d (%p), child = %s\n",
64667c478bd9Sstevel@tonic-gate ddi_driver_name(pdip), ddi_get_instance(pdip),
64677c478bd9Sstevel@tonic-gate (void *)pdip, devnm));
64687c478bd9Sstevel@tonic-gate
64697c478bd9Sstevel@tonic-gate if (pm_pre_unconfig(pdip, flags, &pm_cookie, devnm) != DDI_SUCCESS)
64707c478bd9Sstevel@tonic-gate return (NDI_FAILURE);
64717c478bd9Sstevel@tonic-gate
64727c478bd9Sstevel@tonic-gate if (dipp)
64737c478bd9Sstevel@tonic-gate *dipp = NULL;
64747c478bd9Sstevel@tonic-gate
64753fe80ca4SDan Cross ndi_devi_enter(pdip);
64767c478bd9Sstevel@tonic-gate child = ndi_devi_findchild(pdip, devnm);
64775e3986cbScth
64785e3986cbScth /*
64795e3986cbScth * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
64805e3986cbScth * before parent(pHCI) to avoid deadlock with mpxio Client power
64815e3986cbScth * management operations.
64825e3986cbScth */
64835e3986cbScth if (child && MDI_PHCI(child)) {
64845e3986cbScth vdip = mdi_devi_get_vdip(child);
64855e3986cbScth if (vdip && (ddi_get_parent(vdip) != pdip)) {
64863fe80ca4SDan Cross ndi_devi_exit(pdip);
64875e3986cbScth
64885e3986cbScth /* use mdi_devi_enter ordering */
64893fe80ca4SDan Cross ndi_devi_enter(vdip);
64903fe80ca4SDan Cross ndi_devi_enter(pdip);
64915e3986cbScth child = ndi_devi_findchild(pdip, devnm);
64925e3986cbScth } else
64935e3986cbScth vdip = NULL;
64945e3986cbScth }
64955e3986cbScth
64967c478bd9Sstevel@tonic-gate if (child == NULL) {
64977c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_unconfig_one: %s"
64987c478bd9Sstevel@tonic-gate " not found\n", devnm));
64995e3986cbScth rv = NDI_SUCCESS;
65005e3986cbScth goto out;
65017c478bd9Sstevel@tonic-gate }
65027c478bd9Sstevel@tonic-gate
65037c478bd9Sstevel@tonic-gate /*
65047c478bd9Sstevel@tonic-gate * Unconfigure children/descendants of named child
65057c478bd9Sstevel@tonic-gate */
65067c478bd9Sstevel@tonic-gate rv = devi_unconfig_branch(child, dipp, flags | NDI_UNCONFIG, &brevq);
65077c478bd9Sstevel@tonic-gate if (rv != NDI_SUCCESS)
65087c478bd9Sstevel@tonic-gate goto out;
65097c478bd9Sstevel@tonic-gate
65107c478bd9Sstevel@tonic-gate init_bound_node_ev(pdip, child, flags);
65117c478bd9Sstevel@tonic-gate
65127c478bd9Sstevel@tonic-gate if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
65137c478bd9Sstevel@tonic-gate (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
65147c478bd9Sstevel@tonic-gate (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
65157c478bd9Sstevel@tonic-gate rv = devi_detach_node(child, flags);
65167c478bd9Sstevel@tonic-gate } else {
65177c478bd9Sstevel@tonic-gate /* call bus_config entry point */
65187c478bd9Sstevel@tonic-gate rv = (*f)(pdip, flags, BUS_UNCONFIG_ONE, (void *)devnm);
65197c478bd9Sstevel@tonic-gate }
65207c478bd9Sstevel@tonic-gate
65217c478bd9Sstevel@tonic-gate if (brevq) {
65227c478bd9Sstevel@tonic-gate if (rv != NDI_SUCCESS)
65237c478bd9Sstevel@tonic-gate log_and_free_brevq_dip(child, brevq);
65247c478bd9Sstevel@tonic-gate else
65257c478bd9Sstevel@tonic-gate free_brevq(brevq);
65267c478bd9Sstevel@tonic-gate }
65277c478bd9Sstevel@tonic-gate
65287c478bd9Sstevel@tonic-gate if (dipp && rv != NDI_SUCCESS) {
65297c478bd9Sstevel@tonic-gate ndi_hold_devi(child);
65307c478bd9Sstevel@tonic-gate ASSERT(*dipp == NULL);
65317c478bd9Sstevel@tonic-gate *dipp = child;
65327c478bd9Sstevel@tonic-gate }
65337c478bd9Sstevel@tonic-gate
65347c478bd9Sstevel@tonic-gate out:
65353fe80ca4SDan Cross ndi_devi_exit(pdip);
65365e3986cbScth if (vdip)
65373fe80ca4SDan Cross ndi_devi_exit(vdip);
65385e3986cbScth
65397c478bd9Sstevel@tonic-gate pm_post_unconfig(pdip, pm_cookie, devnm);
65407c478bd9Sstevel@tonic-gate
65417c478bd9Sstevel@tonic-gate return (rv);
65427c478bd9Sstevel@tonic-gate }
65437c478bd9Sstevel@tonic-gate
65447c478bd9Sstevel@tonic-gate struct async_arg {
65457c478bd9Sstevel@tonic-gate dev_info_t *dip;
65467c478bd9Sstevel@tonic-gate uint_t flags;
65477c478bd9Sstevel@tonic-gate };
65487c478bd9Sstevel@tonic-gate
65497c478bd9Sstevel@tonic-gate /*
65507c478bd9Sstevel@tonic-gate * Common async handler for:
65517c478bd9Sstevel@tonic-gate * ndi_devi_bind_driver_async
65527c478bd9Sstevel@tonic-gate * ndi_devi_online_async
65537c478bd9Sstevel@tonic-gate */
65547c478bd9Sstevel@tonic-gate static int
i_ndi_devi_async_common(dev_info_t * dip,uint_t flags,void (* func)())65557c478bd9Sstevel@tonic-gate i_ndi_devi_async_common(dev_info_t *dip, uint_t flags, void (*func)())
65567c478bd9Sstevel@tonic-gate {
65577c478bd9Sstevel@tonic-gate int tqflag;
65587c478bd9Sstevel@tonic-gate int kmflag;
65597c478bd9Sstevel@tonic-gate struct async_arg *arg;
65607c478bd9Sstevel@tonic-gate dev_info_t *pdip = ddi_get_parent(dip);
65617c478bd9Sstevel@tonic-gate
65627c478bd9Sstevel@tonic-gate ASSERT(pdip);
65637c478bd9Sstevel@tonic-gate ASSERT(DEVI(pdip)->devi_taskq);
65647c478bd9Sstevel@tonic-gate ASSERT(ndi_dev_is_persistent_node(dip));
65657c478bd9Sstevel@tonic-gate
65667c478bd9Sstevel@tonic-gate if (flags & NDI_NOSLEEP) {
65677c478bd9Sstevel@tonic-gate kmflag = KM_NOSLEEP;
65687c478bd9Sstevel@tonic-gate tqflag = TQ_NOSLEEP;
65697c478bd9Sstevel@tonic-gate } else {
65707c478bd9Sstevel@tonic-gate kmflag = KM_SLEEP;
65717c478bd9Sstevel@tonic-gate tqflag = TQ_SLEEP;
65727c478bd9Sstevel@tonic-gate }
65737c478bd9Sstevel@tonic-gate
65747c478bd9Sstevel@tonic-gate arg = kmem_alloc(sizeof (*arg), kmflag);
65757c478bd9Sstevel@tonic-gate if (arg == NULL)
65767c478bd9Sstevel@tonic-gate goto fail;
65777c478bd9Sstevel@tonic-gate
65787c478bd9Sstevel@tonic-gate arg->flags = flags;
65797c478bd9Sstevel@tonic-gate arg->dip = dip;
65807c478bd9Sstevel@tonic-gate if (ddi_taskq_dispatch(DEVI(pdip)->devi_taskq, func, arg, tqflag) ==
65817c478bd9Sstevel@tonic-gate DDI_SUCCESS) {
65827c478bd9Sstevel@tonic-gate return (NDI_SUCCESS);
65837c478bd9Sstevel@tonic-gate }
65847c478bd9Sstevel@tonic-gate
65857c478bd9Sstevel@tonic-gate fail:
65867c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "%s%d: ddi_taskq_dispatch failed",
65877c478bd9Sstevel@tonic-gate ddi_driver_name(pdip), ddi_get_instance(pdip)));
65887c478bd9Sstevel@tonic-gate
65897c478bd9Sstevel@tonic-gate if (arg)
65907c478bd9Sstevel@tonic-gate kmem_free(arg, sizeof (*arg));
65917c478bd9Sstevel@tonic-gate return (NDI_FAILURE);
65927c478bd9Sstevel@tonic-gate }
65937c478bd9Sstevel@tonic-gate
65947c478bd9Sstevel@tonic-gate static void
i_ndi_devi_bind_driver_cb(struct async_arg * arg)65957c478bd9Sstevel@tonic-gate i_ndi_devi_bind_driver_cb(struct async_arg *arg)
65967c478bd9Sstevel@tonic-gate {
65977c478bd9Sstevel@tonic-gate (void) ndi_devi_bind_driver(arg->dip, arg->flags);
65987c478bd9Sstevel@tonic-gate kmem_free(arg, sizeof (*arg));
65997c478bd9Sstevel@tonic-gate }
66007c478bd9Sstevel@tonic-gate
66017c478bd9Sstevel@tonic-gate int
ndi_devi_bind_driver_async(dev_info_t * dip,uint_t flags)66027c478bd9Sstevel@tonic-gate ndi_devi_bind_driver_async(dev_info_t *dip, uint_t flags)
66037c478bd9Sstevel@tonic-gate {
66047c478bd9Sstevel@tonic-gate return (i_ndi_devi_async_common(dip, flags,
66057c478bd9Sstevel@tonic-gate (void (*)())i_ndi_devi_bind_driver_cb));
66067c478bd9Sstevel@tonic-gate }
66077c478bd9Sstevel@tonic-gate
66087c478bd9Sstevel@tonic-gate /*
66097c478bd9Sstevel@tonic-gate * place the devinfo in the ONLINE state.
66107c478bd9Sstevel@tonic-gate */
66117c478bd9Sstevel@tonic-gate int
ndi_devi_online(dev_info_t * dip,uint_t flags)66127c478bd9Sstevel@tonic-gate ndi_devi_online(dev_info_t *dip, uint_t flags)
66137c478bd9Sstevel@tonic-gate {
66143fe80ca4SDan Cross int rv;
66157c478bd9Sstevel@tonic-gate dev_info_t *pdip = ddi_get_parent(dip);
66167c478bd9Sstevel@tonic-gate int branch_event = 0;
66177c478bd9Sstevel@tonic-gate
66187c478bd9Sstevel@tonic-gate ASSERT(pdip);
66197c478bd9Sstevel@tonic-gate
66207c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_online: %s%d (%p)\n",
66217c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
66227c478bd9Sstevel@tonic-gate
66233fe80ca4SDan Cross ndi_devi_enter(pdip);
66247c478bd9Sstevel@tonic-gate /* bind child before merging .conf nodes */
66257c478bd9Sstevel@tonic-gate rv = i_ndi_config_node(dip, DS_BOUND, flags);
66267c478bd9Sstevel@tonic-gate if (rv != NDI_SUCCESS) {
66273fe80ca4SDan Cross ndi_devi_exit(pdip);
66287c478bd9Sstevel@tonic-gate return (rv);
66297c478bd9Sstevel@tonic-gate }
66307c478bd9Sstevel@tonic-gate
66317c478bd9Sstevel@tonic-gate /* merge .conf properties */
66327c478bd9Sstevel@tonic-gate (void) i_ndi_make_spec_children(pdip, flags);
66337c478bd9Sstevel@tonic-gate
6634c73a93f2Sdm120769 flags |= (NDI_DEVI_ONLINE | NDI_CONFIG);
66357c478bd9Sstevel@tonic-gate
66367c478bd9Sstevel@tonic-gate if (flags & NDI_NO_EVENT) {
66377c478bd9Sstevel@tonic-gate /*
66387c478bd9Sstevel@tonic-gate * Caller is specifically asking for not to generate an event.
66397c478bd9Sstevel@tonic-gate * Set the following flag so that devi_attach_node() don't
66407c478bd9Sstevel@tonic-gate * change the event state.
66417c478bd9Sstevel@tonic-gate */
66427c478bd9Sstevel@tonic-gate flags |= NDI_NO_EVENT_STATE_CHNG;
66437c478bd9Sstevel@tonic-gate }
66447c478bd9Sstevel@tonic-gate
66457c478bd9Sstevel@tonic-gate if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
66467c478bd9Sstevel@tonic-gate ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip))) {
66477c478bd9Sstevel@tonic-gate flags |= NDI_BRANCH_EVENT_OP;
66487c478bd9Sstevel@tonic-gate branch_event = 1;
66497c478bd9Sstevel@tonic-gate }
66507c478bd9Sstevel@tonic-gate
66517c478bd9Sstevel@tonic-gate /*
66527c478bd9Sstevel@tonic-gate * devi_attach_node() may remove dip on failure
66537c478bd9Sstevel@tonic-gate */
66547c478bd9Sstevel@tonic-gate if ((rv = devi_attach_node(dip, flags)) == NDI_SUCCESS) {
66557c478bd9Sstevel@tonic-gate if ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip)) {
665639f19891SReed /*
665739f19891SReed * Hold the attached dip, and exit the parent while
665839f19891SReed * we drive configuration of children below the
665939f19891SReed * attached dip.
666039f19891SReed */
666139f19891SReed ndi_hold_devi(dip);
66623fe80ca4SDan Cross ndi_devi_exit(pdip);
666339f19891SReed
66647c478bd9Sstevel@tonic-gate (void) ndi_devi_config(dip, flags);
666539f19891SReed
66663fe80ca4SDan Cross ndi_devi_enter(pdip);
666739f19891SReed ndi_rele_devi(dip);
66687c478bd9Sstevel@tonic-gate }
66697c478bd9Sstevel@tonic-gate
66707c478bd9Sstevel@tonic-gate if (branch_event)
66717c478bd9Sstevel@tonic-gate (void) i_log_devfs_branch_add(dip);
66727c478bd9Sstevel@tonic-gate }
66737c478bd9Sstevel@tonic-gate
66743fe80ca4SDan Cross ndi_devi_exit(pdip);
66757c478bd9Sstevel@tonic-gate
66767c478bd9Sstevel@tonic-gate /*
66777c478bd9Sstevel@tonic-gate * Notify devfs that we have a new node. Devfs needs to invalidate
66787c478bd9Sstevel@tonic-gate * cached directory contents.
66797c478bd9Sstevel@tonic-gate *
66807c478bd9Sstevel@tonic-gate * For PCMCIA devices, it is possible the pdip is not fully
66817c478bd9Sstevel@tonic-gate * attached. In this case, calling back into devfs will
66827c478bd9Sstevel@tonic-gate * result in a loop or assertion error. Hence, the check
66837c478bd9Sstevel@tonic-gate * on node state.
66847c478bd9Sstevel@tonic-gate *
66857c478bd9Sstevel@tonic-gate * If we own parent lock, this is part of a branch operation.
66867c478bd9Sstevel@tonic-gate * We skip the devfs_clean() step because the cache invalidation
66877c478bd9Sstevel@tonic-gate * is done higher up in the device tree.
66887c478bd9Sstevel@tonic-gate */
6689737d277aScth if (rv == NDI_SUCCESS && i_ddi_devi_attached(pdip) &&
66907c478bd9Sstevel@tonic-gate !DEVI_BUSY_OWNED(pdip))
66917c478bd9Sstevel@tonic-gate (void) devfs_clean(pdip, NULL, 0);
66927c478bd9Sstevel@tonic-gate return (rv);
66937c478bd9Sstevel@tonic-gate }
66947c478bd9Sstevel@tonic-gate
66957c478bd9Sstevel@tonic-gate static void
i_ndi_devi_online_cb(struct async_arg * arg)66967c478bd9Sstevel@tonic-gate i_ndi_devi_online_cb(struct async_arg *arg)
66977c478bd9Sstevel@tonic-gate {
66987c478bd9Sstevel@tonic-gate (void) ndi_devi_online(arg->dip, arg->flags);
66997c478bd9Sstevel@tonic-gate kmem_free(arg, sizeof (*arg));
67007c478bd9Sstevel@tonic-gate }
67017c478bd9Sstevel@tonic-gate
67027c478bd9Sstevel@tonic-gate int
ndi_devi_online_async(dev_info_t * dip,uint_t flags)67037c478bd9Sstevel@tonic-gate ndi_devi_online_async(dev_info_t *dip, uint_t flags)
67047c478bd9Sstevel@tonic-gate {
67057c478bd9Sstevel@tonic-gate /* mark child as need config if requested. */
670616747f41Scth if (flags & NDI_CONFIG) {
670716747f41Scth mutex_enter(&(DEVI(dip)->devi_lock));
67087c478bd9Sstevel@tonic-gate DEVI_SET_NDI_CONFIG(dip);
670916747f41Scth mutex_exit(&(DEVI(dip)->devi_lock));
671016747f41Scth }
67117c478bd9Sstevel@tonic-gate
67127c478bd9Sstevel@tonic-gate return (i_ndi_devi_async_common(dip, flags,
67137c478bd9Sstevel@tonic-gate (void (*)())i_ndi_devi_online_cb));
67147c478bd9Sstevel@tonic-gate }
67157c478bd9Sstevel@tonic-gate
67167c478bd9Sstevel@tonic-gate /*
67177c478bd9Sstevel@tonic-gate * Take a device node Offline
67187c478bd9Sstevel@tonic-gate * To take a device Offline means to detach the device instance from
67197c478bd9Sstevel@tonic-gate * the driver and prevent devfs requests from re-attaching the device
67207c478bd9Sstevel@tonic-gate * instance.
67217c478bd9Sstevel@tonic-gate *
67227c478bd9Sstevel@tonic-gate * The flag NDI_DEVI_REMOVE causes removes the device node from
67237c478bd9Sstevel@tonic-gate * the driver list and the device tree. In this case, the device
67247c478bd9Sstevel@tonic-gate * is assumed to be removed from the system.
67257c478bd9Sstevel@tonic-gate */
67267c478bd9Sstevel@tonic-gate int
ndi_devi_offline(dev_info_t * dip,uint_t flags)67277c478bd9Sstevel@tonic-gate ndi_devi_offline(dev_info_t *dip, uint_t flags)
67287c478bd9Sstevel@tonic-gate {
67293fe80ca4SDan Cross int rval = 0;
67307c478bd9Sstevel@tonic-gate dev_info_t *pdip = ddi_get_parent(dip);
67315e3986cbScth dev_info_t *vdip = NULL;
67327c478bd9Sstevel@tonic-gate struct brevq_node *brevq = NULL;
67337c478bd9Sstevel@tonic-gate
67347c478bd9Sstevel@tonic-gate ASSERT(pdip);
67357c478bd9Sstevel@tonic-gate
67367c478bd9Sstevel@tonic-gate flags |= NDI_DEVI_OFFLINE;
67375e3986cbScth
67385e3986cbScth /*
67395e3986cbScth * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
67405e3986cbScth * before parent(pHCI) to avoid deadlock with mpxio Client power
67415e3986cbScth * management operations.
67425e3986cbScth */
67435e3986cbScth if (MDI_PHCI(dip)) {
67445e3986cbScth vdip = mdi_devi_get_vdip(dip);
67455e3986cbScth if (vdip && (ddi_get_parent(vdip) != pdip))
67463fe80ca4SDan Cross ndi_devi_enter(vdip);
67475e3986cbScth else
67485e3986cbScth vdip = NULL;
67495e3986cbScth }
67503fe80ca4SDan Cross ndi_devi_enter(pdip);
67515e3986cbScth
67524c06356bSdh142964 if (i_ddi_devi_attached(dip)) {
67537c478bd9Sstevel@tonic-gate /*
67547c478bd9Sstevel@tonic-gate * If dip is in DS_READY state, there may be cached dv_nodes
67557c478bd9Sstevel@tonic-gate * referencing this dip, so we invoke devfs code path.
67567c478bd9Sstevel@tonic-gate * Note that we must release busy changing on pdip to
67577c478bd9Sstevel@tonic-gate * avoid deadlock against devfs.
67587c478bd9Sstevel@tonic-gate */
67597c478bd9Sstevel@tonic-gate char *devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
67607c478bd9Sstevel@tonic-gate (void) ddi_deviname(dip, devname);
67615e3986cbScth
67623fe80ca4SDan Cross ndi_devi_exit(pdip);
67635e3986cbScth if (vdip)
67643fe80ca4SDan Cross ndi_devi_exit(vdip);
67657c478bd9Sstevel@tonic-gate
67667c478bd9Sstevel@tonic-gate /*
67674c06356bSdh142964 * If we are explictly told to clean, then clean. If we own the
67684c06356bSdh142964 * parent lock then this is part of a branch operation, and we
67694c06356bSdh142964 * skip the devfs_clean() step.
67704c06356bSdh142964 *
67714c06356bSdh142964 * NOTE: A thread performing a devfs file system lookup/
67724c06356bSdh142964 * bus_config can't call devfs_clean to unconfig without
67734c06356bSdh142964 * causing rwlock problems in devfs. For ndi_devi_offline, this
67744c06356bSdh142964 * means that the NDI_DEVFS_CLEAN flag is safe from ioctl code
67754c06356bSdh142964 * or from an async hotplug thread, but is not safe from a
67764c06356bSdh142964 * nexus driver's bus_config implementation.
67777c478bd9Sstevel@tonic-gate */
67784c06356bSdh142964 if ((flags & NDI_DEVFS_CLEAN) ||
67794c06356bSdh142964 (!DEVI_BUSY_OWNED(pdip)))
678019174f18Svikram (void) devfs_clean(pdip, devname + 1, DV_CLEAN_FORCE);
67814c06356bSdh142964
67827c478bd9Sstevel@tonic-gate kmem_free(devname, MAXNAMELEN + 1);
67837c478bd9Sstevel@tonic-gate
678419174f18Svikram rval = devi_unconfig_branch(dip, NULL, flags|NDI_UNCONFIG,
678519174f18Svikram &brevq);
678619174f18Svikram
67877c478bd9Sstevel@tonic-gate if (rval)
67887c478bd9Sstevel@tonic-gate return (NDI_FAILURE);
67897c478bd9Sstevel@tonic-gate
67905e3986cbScth if (vdip)
67913fe80ca4SDan Cross ndi_devi_enter(vdip);
67923fe80ca4SDan Cross ndi_devi_enter(pdip);
67937c478bd9Sstevel@tonic-gate }
67947c478bd9Sstevel@tonic-gate
67957c478bd9Sstevel@tonic-gate init_bound_node_ev(pdip, dip, flags);
67967c478bd9Sstevel@tonic-gate
67977c478bd9Sstevel@tonic-gate rval = devi_detach_node(dip, flags);
67987c478bd9Sstevel@tonic-gate if (brevq) {
67997c478bd9Sstevel@tonic-gate if (rval != NDI_SUCCESS)
68007c478bd9Sstevel@tonic-gate log_and_free_brevq_dip(dip, brevq);
68017c478bd9Sstevel@tonic-gate else
68027c478bd9Sstevel@tonic-gate free_brevq(brevq);
68037c478bd9Sstevel@tonic-gate }
68047c478bd9Sstevel@tonic-gate
68053fe80ca4SDan Cross ndi_devi_exit(pdip);
68065e3986cbScth if (vdip)
68073fe80ca4SDan Cross ndi_devi_exit(vdip);
68087c478bd9Sstevel@tonic-gate
68097c478bd9Sstevel@tonic-gate return (rval);
68107c478bd9Sstevel@tonic-gate }
68117c478bd9Sstevel@tonic-gate
68127c478bd9Sstevel@tonic-gate /*
68134c06356bSdh142964 * Find the child dev_info node of parent nexus 'p' whose unit address
68147c478bd9Sstevel@tonic-gate * matches "cname@caddr". Recommend use of ndi_devi_findchild() instead.
68157c478bd9Sstevel@tonic-gate */
68167c478bd9Sstevel@tonic-gate dev_info_t *
ndi_devi_find(dev_info_t * pdip,char * cname,char * caddr)68177c478bd9Sstevel@tonic-gate ndi_devi_find(dev_info_t *pdip, char *cname, char *caddr)
68187c478bd9Sstevel@tonic-gate {
68197c478bd9Sstevel@tonic-gate dev_info_t *child;
68207c478bd9Sstevel@tonic-gate
68217c478bd9Sstevel@tonic-gate if (pdip == NULL || cname == NULL || caddr == NULL)
68227c478bd9Sstevel@tonic-gate return ((dev_info_t *)NULL);
68237c478bd9Sstevel@tonic-gate
68243fe80ca4SDan Cross ndi_devi_enter(pdip);
6825f4da9be0Scth child = find_sibling(ddi_get_child(pdip), cname, caddr,
6826f4da9be0Scth FIND_NODE_BY_NODENAME, NULL);
68273fe80ca4SDan Cross ndi_devi_exit(pdip);
68287c478bd9Sstevel@tonic-gate return (child);
68297c478bd9Sstevel@tonic-gate }
68307c478bd9Sstevel@tonic-gate
68317c478bd9Sstevel@tonic-gate /*
68324c06356bSdh142964 * Find the child dev_info node of parent nexus 'p' whose unit address
68337c478bd9Sstevel@tonic-gate * matches devname "name@addr". Permits caller to hold the parent.
68347c478bd9Sstevel@tonic-gate */
68357c478bd9Sstevel@tonic-gate dev_info_t *
ndi_devi_findchild(dev_info_t * pdip,char * devname)68367c478bd9Sstevel@tonic-gate ndi_devi_findchild(dev_info_t *pdip, char *devname)
68377c478bd9Sstevel@tonic-gate {
68387c478bd9Sstevel@tonic-gate dev_info_t *child;
68397c478bd9Sstevel@tonic-gate char *cname, *caddr;
68407c478bd9Sstevel@tonic-gate char *devstr;
68417c478bd9Sstevel@tonic-gate
68427c478bd9Sstevel@tonic-gate ASSERT(DEVI_BUSY_OWNED(pdip));
68437c478bd9Sstevel@tonic-gate
68447c478bd9Sstevel@tonic-gate devstr = i_ddi_strdup(devname, KM_SLEEP);
68457c478bd9Sstevel@tonic-gate i_ddi_parse_name(devstr, &cname, &caddr, NULL);
68467c478bd9Sstevel@tonic-gate
68477c478bd9Sstevel@tonic-gate if (cname == NULL || caddr == NULL) {
68487c478bd9Sstevel@tonic-gate kmem_free(devstr, strlen(devname)+1);
68497c478bd9Sstevel@tonic-gate return ((dev_info_t *)NULL);
68507c478bd9Sstevel@tonic-gate }
68517c478bd9Sstevel@tonic-gate
6852f4da9be0Scth child = find_sibling(ddi_get_child(pdip), cname, caddr,
6853f4da9be0Scth FIND_NODE_BY_NODENAME, NULL);
68547c478bd9Sstevel@tonic-gate kmem_free(devstr, strlen(devname)+1);
68557c478bd9Sstevel@tonic-gate return (child);
68567c478bd9Sstevel@tonic-gate }
68577c478bd9Sstevel@tonic-gate
68587c478bd9Sstevel@tonic-gate /*
68597c478bd9Sstevel@tonic-gate * Misc. routines called by framework only
68607c478bd9Sstevel@tonic-gate */
68617c478bd9Sstevel@tonic-gate
68627c478bd9Sstevel@tonic-gate /*
68637c478bd9Sstevel@tonic-gate * Clear the DEVI_MADE_CHILDREN/DEVI_ATTACHED_CHILDREN flags
68647c478bd9Sstevel@tonic-gate * if new child spec has been added.
68657c478bd9Sstevel@tonic-gate */
68667c478bd9Sstevel@tonic-gate static int
reset_nexus_flags(dev_info_t * dip,void * arg)68677c478bd9Sstevel@tonic-gate reset_nexus_flags(dev_info_t *dip, void *arg)
68687c478bd9Sstevel@tonic-gate {
68697c478bd9Sstevel@tonic-gate struct hwc_spec *list;
68707c478bd9Sstevel@tonic-gate
68717c478bd9Sstevel@tonic-gate if (((DEVI(dip)->devi_flags & DEVI_MADE_CHILDREN) == 0) ||
68727c478bd9Sstevel@tonic-gate ((list = hwc_get_child_spec(dip, (major_t)(uintptr_t)arg)) == NULL))
68737c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE);
68747c478bd9Sstevel@tonic-gate
68757c478bd9Sstevel@tonic-gate hwc_free_spec_list(list);
68766a41d557Scth
68776a41d557Scth /* coordinate child state update */
68783fe80ca4SDan Cross ndi_devi_enter(dip);
68797c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(dip)->devi_lock);
68807c478bd9Sstevel@tonic-gate DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN | DEVI_ATTACHED_CHILDREN);
68817c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(dip)->devi_lock);
68823fe80ca4SDan Cross ndi_devi_exit(dip);
68837c478bd9Sstevel@tonic-gate
68847c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE);
68857c478bd9Sstevel@tonic-gate }
68867c478bd9Sstevel@tonic-gate
68877c478bd9Sstevel@tonic-gate /*
68887c478bd9Sstevel@tonic-gate * Helper functions, returns NULL if no memory.
68897c478bd9Sstevel@tonic-gate */
68907c478bd9Sstevel@tonic-gate
68917c478bd9Sstevel@tonic-gate /*
68927c478bd9Sstevel@tonic-gate * path_to_major:
68937c478bd9Sstevel@tonic-gate *
68947c478bd9Sstevel@tonic-gate * Return an alternate driver name binding for the leaf device
68957c478bd9Sstevel@tonic-gate * of the given pathname, if there is one. The purpose of this
68967c478bd9Sstevel@tonic-gate * function is to deal with generic pathnames. The default action
68977c478bd9Sstevel@tonic-gate * for platforms that can't do this (ie: x86 or any platform that
68987c478bd9Sstevel@tonic-gate * does not have prom_finddevice functionality, which matches
68997c478bd9Sstevel@tonic-gate * nodenames and unit-addresses without the drivers participation)
6900a204de77Scth * is to return DDI_MAJOR_T_NONE.
69017c478bd9Sstevel@tonic-gate *
69027c478bd9Sstevel@tonic-gate * Used in loadrootmodules() in the swapgeneric module to
69037c478bd9Sstevel@tonic-gate * associate a given pathname with a given leaf driver.
69047c478bd9Sstevel@tonic-gate *
69057c478bd9Sstevel@tonic-gate */
69067c478bd9Sstevel@tonic-gate major_t
path_to_major(char * path)69077c478bd9Sstevel@tonic-gate path_to_major(char *path)
69087c478bd9Sstevel@tonic-gate {
69097c478bd9Sstevel@tonic-gate dev_info_t *dip;
69107c478bd9Sstevel@tonic-gate char *p, *q;
6911fa9e4066Sahrens pnode_t nodeid;
6912f4da9be0Scth major_t major;
6913f4da9be0Scth
6914f4da9be0Scth /* check for path-oriented alias */
6915f4da9be0Scth major = ddi_name_to_major(path);
6916c8742f64SJerry Gilliam if (driver_active(major)) {
6917f4da9be0Scth NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s path bound %s\n",
6918f4da9be0Scth path, ddi_major_to_name(major)));
6919f4da9be0Scth return (major);
6920f4da9be0Scth }
69217c478bd9Sstevel@tonic-gate
69227c478bd9Sstevel@tonic-gate /*
69237c478bd9Sstevel@tonic-gate * Get the nodeid of the given pathname, if such a mapping exists.
69247c478bd9Sstevel@tonic-gate */
69257c478bd9Sstevel@tonic-gate dip = NULL;
69267c478bd9Sstevel@tonic-gate nodeid = prom_finddevice(path);
69277c478bd9Sstevel@tonic-gate if (nodeid != OBP_BADNODE) {
69287c478bd9Sstevel@tonic-gate /*
69297c478bd9Sstevel@tonic-gate * Find the nodeid in our copy of the device tree and return
69307c478bd9Sstevel@tonic-gate * whatever name we used to bind this node to a driver.
69317c478bd9Sstevel@tonic-gate */
69327c478bd9Sstevel@tonic-gate dip = e_ddi_nodeid_to_dip(nodeid);
69337c478bd9Sstevel@tonic-gate }
69347c478bd9Sstevel@tonic-gate
69357c478bd9Sstevel@tonic-gate if (dip == NULL) {
69367c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_WARN,
69377c478bd9Sstevel@tonic-gate "path_to_major: can't bind <%s>\n", path));
6938a204de77Scth return (DDI_MAJOR_T_NONE);
69397c478bd9Sstevel@tonic-gate }
69407c478bd9Sstevel@tonic-gate
69417c478bd9Sstevel@tonic-gate /*
69427c478bd9Sstevel@tonic-gate * If we're bound to something other than the nodename,
69437c478bd9Sstevel@tonic-gate * note that in the message buffer and system log.
69447c478bd9Sstevel@tonic-gate */
69457c478bd9Sstevel@tonic-gate p = ddi_binding_name(dip);
69467c478bd9Sstevel@tonic-gate q = ddi_node_name(dip);
69477c478bd9Sstevel@tonic-gate if (p && q && (strcmp(p, q) != 0))
69487c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s bound to %s\n",
69497c478bd9Sstevel@tonic-gate path, p));
69507c478bd9Sstevel@tonic-gate
6951f4da9be0Scth major = ddi_name_to_major(p);
69527c478bd9Sstevel@tonic-gate
6953f4da9be0Scth ndi_rele_devi(dip); /* release e_ddi_nodeid_to_dip hold */
69547c478bd9Sstevel@tonic-gate
6955f4da9be0Scth return (major);
69567c478bd9Sstevel@tonic-gate }
69577c478bd9Sstevel@tonic-gate
69587c478bd9Sstevel@tonic-gate /*
69597c478bd9Sstevel@tonic-gate * Return the held dip for the specified major and instance, attempting to do
69607c478bd9Sstevel@tonic-gate * an attach if specified. Return NULL if the devi can't be found or put in
69617c478bd9Sstevel@tonic-gate * the proper state. The caller must release the hold via ddi_release_devi if
69627c478bd9Sstevel@tonic-gate * a non-NULL value is returned.
69637c478bd9Sstevel@tonic-gate *
69647c478bd9Sstevel@tonic-gate * Some callers expect to be able to perform a hold_devi() while in a context
69657c478bd9Sstevel@tonic-gate * where using ndi_devi_enter() to ensure the hold might cause deadlock (see
69667c478bd9Sstevel@tonic-gate * open-from-attach code in consconfig_dacf.c). Such special-case callers
69674c06356bSdh142964 * must ensure that an ndi_devi_enter(parent)/ndi_hold_devi() from a safe
69687c478bd9Sstevel@tonic-gate * context is already active. The hold_devi() implementation must accommodate
69697c478bd9Sstevel@tonic-gate * these callers.
69707c478bd9Sstevel@tonic-gate */
69717c478bd9Sstevel@tonic-gate static dev_info_t *
hold_devi(major_t major,int instance,int flags)69727c478bd9Sstevel@tonic-gate hold_devi(major_t major, int instance, int flags)
69737c478bd9Sstevel@tonic-gate {
69747c478bd9Sstevel@tonic-gate struct devnames *dnp;
69757c478bd9Sstevel@tonic-gate dev_info_t *dip;
69767c478bd9Sstevel@tonic-gate char *path;
697710b58182SChris Horne char *vpath;
69787c478bd9Sstevel@tonic-gate
69797c478bd9Sstevel@tonic-gate if ((major >= devcnt) || (instance == -1))
69807c478bd9Sstevel@tonic-gate return (NULL);
69817c478bd9Sstevel@tonic-gate
69827c478bd9Sstevel@tonic-gate /* try to find the instance in the per driver list */
69837c478bd9Sstevel@tonic-gate dnp = &(devnamesp[major]);
69847c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&(dnp->dn_lock));
69857c478bd9Sstevel@tonic-gate for (dip = dnp->dn_head; dip;
69867c478bd9Sstevel@tonic-gate dip = (dev_info_t *)DEVI(dip)->devi_next) {
69877c478bd9Sstevel@tonic-gate /* skip node if instance field is not valid */
69887c478bd9Sstevel@tonic-gate if (i_ddi_node_state(dip) < DS_INITIALIZED)
69897c478bd9Sstevel@tonic-gate continue;
69907c478bd9Sstevel@tonic-gate
69917c478bd9Sstevel@tonic-gate /* look for instance match */
69927c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_instance == instance) {
69937c478bd9Sstevel@tonic-gate /*
69947c478bd9Sstevel@tonic-gate * To accommodate callers that can't block in
69954c06356bSdh142964 * ndi_devi_enter() we do an ndi_hold_devi(), and
69967c478bd9Sstevel@tonic-gate * afterwards check that the node is in a state where
69977c478bd9Sstevel@tonic-gate * the hold prevents detach(). If we did not manage to
69987c478bd9Sstevel@tonic-gate * prevent detach then we ndi_rele_devi() and perform
69997c478bd9Sstevel@tonic-gate * the slow path below (which can result in a blocking
70007c478bd9Sstevel@tonic-gate * ndi_devi_enter() while driving attach top-down).
70017c478bd9Sstevel@tonic-gate * This code depends on the ordering of
70027c478bd9Sstevel@tonic-gate * DEVI_SET_DETACHING and the devi_ref check in the
70037c478bd9Sstevel@tonic-gate * detach_node() code path.
70047c478bd9Sstevel@tonic-gate */
70057c478bd9Sstevel@tonic-gate ndi_hold_devi(dip);
7006737d277aScth if (i_ddi_devi_attached(dip) &&
70077c478bd9Sstevel@tonic-gate !DEVI_IS_DETACHING(dip)) {
70087c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&(dnp->dn_lock));
70097c478bd9Sstevel@tonic-gate return (dip); /* fast-path with devi held */
70107c478bd9Sstevel@tonic-gate }
70117c478bd9Sstevel@tonic-gate ndi_rele_devi(dip);
70127c478bd9Sstevel@tonic-gate
70137c478bd9Sstevel@tonic-gate /* try slow-path */
70147c478bd9Sstevel@tonic-gate dip = NULL;
70157c478bd9Sstevel@tonic-gate break;
70167c478bd9Sstevel@tonic-gate }
70177c478bd9Sstevel@tonic-gate }
70187c478bd9Sstevel@tonic-gate ASSERT(dip == NULL);
70197c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&(dnp->dn_lock));
70207c478bd9Sstevel@tonic-gate
70217c478bd9Sstevel@tonic-gate if (flags & E_DDI_HOLD_DEVI_NOATTACH)
70227c478bd9Sstevel@tonic-gate return (NULL); /* told not to drive attach */
70237c478bd9Sstevel@tonic-gate
70247c478bd9Sstevel@tonic-gate /* slow-path may block, so it should not occur from interrupt */
70257c478bd9Sstevel@tonic-gate ASSERT(!servicing_interrupt());
70267c478bd9Sstevel@tonic-gate if (servicing_interrupt())
70277c478bd9Sstevel@tonic-gate return (NULL);
70287c478bd9Sstevel@tonic-gate
70297c478bd9Sstevel@tonic-gate /* reconstruct the path and drive attach by path through devfs. */
70307c478bd9Sstevel@tonic-gate path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
703110b58182SChris Horne if (e_ddi_majorinstance_to_path(major, instance, path) == 0) {
70327c478bd9Sstevel@tonic-gate dip = e_ddi_hold_devi_by_path(path, flags);
703310b58182SChris Horne
703410b58182SChris Horne /*
703510b58182SChris Horne * Verify that we got the correct device - a path_to_inst file
703610b58182SChris Horne * with a bogus/corrupt path (or a nexus that changes its
703710b58182SChris Horne * unit-address format) could result in an incorrect answer
703810b58182SChris Horne *
703910b58182SChris Horne * Verify major, instance, and path.
704010b58182SChris Horne */
704110b58182SChris Horne vpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
704210b58182SChris Horne if (dip &&
704310b58182SChris Horne ((DEVI(dip)->devi_major != major) ||
704410b58182SChris Horne ((DEVI(dip)->devi_instance != instance)) ||
704510b58182SChris Horne (strcmp(path, ddi_pathname(dip, vpath)) != 0))) {
704610b58182SChris Horne ndi_rele_devi(dip);
704710b58182SChris Horne dip = NULL; /* no answer better than wrong answer */
704810b58182SChris Horne }
704910b58182SChris Horne kmem_free(vpath, MAXPATHLEN);
705010b58182SChris Horne }
70517c478bd9Sstevel@tonic-gate kmem_free(path, MAXPATHLEN);
70527c478bd9Sstevel@tonic-gate return (dip); /* with devi held */
70537c478bd9Sstevel@tonic-gate }
70547c478bd9Sstevel@tonic-gate
70557c478bd9Sstevel@tonic-gate /*
70567c478bd9Sstevel@tonic-gate * The {e_}ddi_hold_devi{_by_{instance|dev|path}} hold the devinfo node
70577c478bd9Sstevel@tonic-gate * associated with the specified arguments. This hold should be released
70587c478bd9Sstevel@tonic-gate * by calling ddi_release_devi.
70597c478bd9Sstevel@tonic-gate *
70607c478bd9Sstevel@tonic-gate * The E_DDI_HOLD_DEVI_NOATTACH flag argument allows the caller to to specify
70617c478bd9Sstevel@tonic-gate * a failure return if the node is not already attached.
70627c478bd9Sstevel@tonic-gate *
70637c478bd9Sstevel@tonic-gate * NOTE: by the time we make e_ddi_hold_devi public, we should be able to reuse
70647c478bd9Sstevel@tonic-gate * ddi_hold_devi again.
70657c478bd9Sstevel@tonic-gate */
70667c478bd9Sstevel@tonic-gate dev_info_t *
ddi_hold_devi_by_instance(major_t major,int instance,int flags)70677c478bd9Sstevel@tonic-gate ddi_hold_devi_by_instance(major_t major, int instance, int flags)
70687c478bd9Sstevel@tonic-gate {
70697c478bd9Sstevel@tonic-gate return (hold_devi(major, instance, flags));
70707c478bd9Sstevel@tonic-gate }
70717c478bd9Sstevel@tonic-gate
70727c478bd9Sstevel@tonic-gate dev_info_t *
e_ddi_hold_devi_by_dev(dev_t dev,int flags)70737c478bd9Sstevel@tonic-gate e_ddi_hold_devi_by_dev(dev_t dev, int flags)
70747c478bd9Sstevel@tonic-gate {
70757c478bd9Sstevel@tonic-gate major_t major = getmajor(dev);
70767c478bd9Sstevel@tonic-gate dev_info_t *dip;
70777c478bd9Sstevel@tonic-gate struct dev_ops *ops;
70787c478bd9Sstevel@tonic-gate dev_info_t *ddip = NULL;
70797c478bd9Sstevel@tonic-gate
70807c478bd9Sstevel@tonic-gate dip = hold_devi(major, dev_to_instance(dev), flags);
70817c478bd9Sstevel@tonic-gate
70827c478bd9Sstevel@tonic-gate /*
70837c478bd9Sstevel@tonic-gate * The rest of this routine is legacy support for drivers that
70847c478bd9Sstevel@tonic-gate * have broken DDI_INFO_DEVT2INSTANCE implementations but may have
70857c478bd9Sstevel@tonic-gate * functional DDI_INFO_DEVT2DEVINFO implementations. This code will
70867c478bd9Sstevel@tonic-gate * diagnose inconsistency and, for maximum compatibility with legacy
70877c478bd9Sstevel@tonic-gate * drivers, give preference to the drivers DDI_INFO_DEVT2DEVINFO
70887c478bd9Sstevel@tonic-gate * implementation over the above derived dip based the driver's
70897c478bd9Sstevel@tonic-gate * DDI_INFO_DEVT2INSTANCE implementation. This legacy support should
70907c478bd9Sstevel@tonic-gate * be removed when DDI_INFO_DEVT2DEVINFO is deprecated.
70917c478bd9Sstevel@tonic-gate *
70927c478bd9Sstevel@tonic-gate * NOTE: The following code has a race condition. DEVT2DEVINFO
70937c478bd9Sstevel@tonic-gate * returns a dip which is not held. By the time we ref ddip,
70947c478bd9Sstevel@tonic-gate * it could have been freed. The saving grace is that for
70957c478bd9Sstevel@tonic-gate * most drivers, the dip returned from hold_devi() is the
70967c478bd9Sstevel@tonic-gate * same one as the one returned by DEVT2DEVINFO, so we are
70977c478bd9Sstevel@tonic-gate * safe for drivers with the correct getinfo(9e) impl.
70987c478bd9Sstevel@tonic-gate */
70997c478bd9Sstevel@tonic-gate if (((ops = ddi_hold_driver(major)) != NULL) &&
71007c478bd9Sstevel@tonic-gate CB_DRV_INSTALLED(ops) && ops->devo_getinfo) {
71017c478bd9Sstevel@tonic-gate if ((*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2DEVINFO,
71027c478bd9Sstevel@tonic-gate (void *)dev, (void **)&ddip) != DDI_SUCCESS)
71037c478bd9Sstevel@tonic-gate ddip = NULL;
71047c478bd9Sstevel@tonic-gate }
71057c478bd9Sstevel@tonic-gate
71067c478bd9Sstevel@tonic-gate /* give preference to the driver returned DEVT2DEVINFO dip */
71077c478bd9Sstevel@tonic-gate if (ddip && (dip != ddip)) {
71087c478bd9Sstevel@tonic-gate #ifdef DEBUG
71097c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s: inconsistent getinfo(9E) implementation",
71107c478bd9Sstevel@tonic-gate ddi_driver_name(ddip));
71117c478bd9Sstevel@tonic-gate #endif /* DEBUG */
71127c478bd9Sstevel@tonic-gate ndi_hold_devi(ddip);
71137c478bd9Sstevel@tonic-gate if (dip)
71147c478bd9Sstevel@tonic-gate ndi_rele_devi(dip);
71157c478bd9Sstevel@tonic-gate dip = ddip;
71167c478bd9Sstevel@tonic-gate }
71177c478bd9Sstevel@tonic-gate
71187c478bd9Sstevel@tonic-gate if (ops)
71197c478bd9Sstevel@tonic-gate ddi_rele_driver(major);
71207c478bd9Sstevel@tonic-gate
71217c478bd9Sstevel@tonic-gate return (dip);
71227c478bd9Sstevel@tonic-gate }
71237c478bd9Sstevel@tonic-gate
71247c478bd9Sstevel@tonic-gate /*
71257c478bd9Sstevel@tonic-gate * For compatibility only. Do not call this function!
71267c478bd9Sstevel@tonic-gate */
71277c478bd9Sstevel@tonic-gate dev_info_t *
e_ddi_get_dev_info(dev_t dev,vtype_t type)71287c478bd9Sstevel@tonic-gate e_ddi_get_dev_info(dev_t dev, vtype_t type)
71297c478bd9Sstevel@tonic-gate {
71307c478bd9Sstevel@tonic-gate dev_info_t *dip = NULL;
71317c478bd9Sstevel@tonic-gate if (getmajor(dev) >= devcnt)
71327c478bd9Sstevel@tonic-gate return (NULL);
71337c478bd9Sstevel@tonic-gate
71347c478bd9Sstevel@tonic-gate switch (type) {
71357c478bd9Sstevel@tonic-gate case VCHR:
71367c478bd9Sstevel@tonic-gate case VBLK:
71377c478bd9Sstevel@tonic-gate dip = e_ddi_hold_devi_by_dev(dev, 0);
71387c478bd9Sstevel@tonic-gate default:
71397c478bd9Sstevel@tonic-gate break;
71407c478bd9Sstevel@tonic-gate }
71417c478bd9Sstevel@tonic-gate
71427c478bd9Sstevel@tonic-gate /*
71437c478bd9Sstevel@tonic-gate * For compatibility reasons, we can only return the dip with
71447c478bd9Sstevel@tonic-gate * the driver ref count held. This is not a safe thing to do.
71457c478bd9Sstevel@tonic-gate * For certain broken third-party software, we are willing
71467c478bd9Sstevel@tonic-gate * to venture into unknown territory.
71477c478bd9Sstevel@tonic-gate */
71487c478bd9Sstevel@tonic-gate if (dip) {
71497c478bd9Sstevel@tonic-gate (void) ndi_hold_driver(dip);
71507c478bd9Sstevel@tonic-gate ndi_rele_devi(dip);
71517c478bd9Sstevel@tonic-gate }
71527c478bd9Sstevel@tonic-gate return (dip);
71537c478bd9Sstevel@tonic-gate }
71547c478bd9Sstevel@tonic-gate
71557c478bd9Sstevel@tonic-gate dev_info_t *
e_ddi_hold_devi_by_path(char * path,int flags)71567c478bd9Sstevel@tonic-gate e_ddi_hold_devi_by_path(char *path, int flags)
71577c478bd9Sstevel@tonic-gate {
71587c478bd9Sstevel@tonic-gate dev_info_t *dip;
71597c478bd9Sstevel@tonic-gate
71607c478bd9Sstevel@tonic-gate /* can't specify NOATTACH by path */
71617c478bd9Sstevel@tonic-gate ASSERT(!(flags & E_DDI_HOLD_DEVI_NOATTACH));
71627c478bd9Sstevel@tonic-gate
71637c478bd9Sstevel@tonic-gate return (resolve_pathname(path, &dip, NULL, NULL) ? NULL : dip);
71647c478bd9Sstevel@tonic-gate }
71657c478bd9Sstevel@tonic-gate
71667c478bd9Sstevel@tonic-gate void
e_ddi_hold_devi(dev_info_t * dip)71677c478bd9Sstevel@tonic-gate e_ddi_hold_devi(dev_info_t *dip)
71687c478bd9Sstevel@tonic-gate {
71697c478bd9Sstevel@tonic-gate ndi_hold_devi(dip);
71707c478bd9Sstevel@tonic-gate }
71717c478bd9Sstevel@tonic-gate
71727c478bd9Sstevel@tonic-gate void
ddi_release_devi(dev_info_t * dip)71737c478bd9Sstevel@tonic-gate ddi_release_devi(dev_info_t *dip)
71747c478bd9Sstevel@tonic-gate {
71757c478bd9Sstevel@tonic-gate ndi_rele_devi(dip);
71767c478bd9Sstevel@tonic-gate }
71777c478bd9Sstevel@tonic-gate
71787c478bd9Sstevel@tonic-gate /*
71797c478bd9Sstevel@tonic-gate * Associate a streams queue with a devinfo node
71807c478bd9Sstevel@tonic-gate * NOTE: This function is called by STREAM driver's put procedure.
71817c478bd9Sstevel@tonic-gate * It cannot block.
71827c478bd9Sstevel@tonic-gate */
71837c478bd9Sstevel@tonic-gate void
ddi_assoc_queue_with_devi(queue_t * q,dev_info_t * dip)71847c478bd9Sstevel@tonic-gate ddi_assoc_queue_with_devi(queue_t *q, dev_info_t *dip)
71857c478bd9Sstevel@tonic-gate {
71867c478bd9Sstevel@tonic-gate queue_t *rq = _RD(q);
71877c478bd9Sstevel@tonic-gate struct stdata *stp;
71887c478bd9Sstevel@tonic-gate vnode_t *vp;
71897c478bd9Sstevel@tonic-gate
71907c478bd9Sstevel@tonic-gate /* set flag indicating that ddi_assoc_queue_with_devi was called */
71917c478bd9Sstevel@tonic-gate mutex_enter(QLOCK(rq));
71927c478bd9Sstevel@tonic-gate rq->q_flag |= _QASSOCIATED;
71937c478bd9Sstevel@tonic-gate mutex_exit(QLOCK(rq));
71947c478bd9Sstevel@tonic-gate
71957c478bd9Sstevel@tonic-gate /* get the vnode associated with the queue */
71967c478bd9Sstevel@tonic-gate stp = STREAM(rq);
71977c478bd9Sstevel@tonic-gate vp = stp->sd_vnode;
71987c478bd9Sstevel@tonic-gate ASSERT(vp);
71997c478bd9Sstevel@tonic-gate
72007c478bd9Sstevel@tonic-gate /* change the hardware association of the vnode */
72017c478bd9Sstevel@tonic-gate spec_assoc_vp_with_devi(vp, dip);
72027c478bd9Sstevel@tonic-gate }
72037c478bd9Sstevel@tonic-gate
72047c478bd9Sstevel@tonic-gate /*
72057c478bd9Sstevel@tonic-gate * ddi_install_driver(name)
72067c478bd9Sstevel@tonic-gate *
72077c478bd9Sstevel@tonic-gate * Driver installation is currently a byproduct of driver loading. This
72087c478bd9Sstevel@tonic-gate * may change.
72097c478bd9Sstevel@tonic-gate */
72107c478bd9Sstevel@tonic-gate int
ddi_install_driver(char * name)72117c478bd9Sstevel@tonic-gate ddi_install_driver(char *name)
72127c478bd9Sstevel@tonic-gate {
72137c478bd9Sstevel@tonic-gate major_t major = ddi_name_to_major(name);
72147c478bd9Sstevel@tonic-gate
7215a204de77Scth if ((major == DDI_MAJOR_T_NONE) ||
72167c478bd9Sstevel@tonic-gate (ddi_hold_installed_driver(major) == NULL)) {
72177c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
72187c478bd9Sstevel@tonic-gate }
72197c478bd9Sstevel@tonic-gate ddi_rele_driver(major);
72207c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
72217c478bd9Sstevel@tonic-gate }
72227c478bd9Sstevel@tonic-gate
72237c478bd9Sstevel@tonic-gate struct dev_ops *
ddi_hold_driver(major_t major)72247c478bd9Sstevel@tonic-gate ddi_hold_driver(major_t major)
72257c478bd9Sstevel@tonic-gate {
72267c478bd9Sstevel@tonic-gate return (mod_hold_dev_by_major(major));
72277c478bd9Sstevel@tonic-gate }
72287c478bd9Sstevel@tonic-gate
72297c478bd9Sstevel@tonic-gate
72307c478bd9Sstevel@tonic-gate void
ddi_rele_driver(major_t major)72317c478bd9Sstevel@tonic-gate ddi_rele_driver(major_t major)
72327c478bd9Sstevel@tonic-gate {
72337c478bd9Sstevel@tonic-gate mod_rele_dev_by_major(major);
72347c478bd9Sstevel@tonic-gate }
72357c478bd9Sstevel@tonic-gate
72367c478bd9Sstevel@tonic-gate
72377c478bd9Sstevel@tonic-gate /*
72387c478bd9Sstevel@tonic-gate * This is called during boot to force attachment order of special dips
72397c478bd9Sstevel@tonic-gate * dip must be referenced via ndi_hold_devi()
72407c478bd9Sstevel@tonic-gate */
72417c478bd9Sstevel@tonic-gate int
i_ddi_attach_node_hierarchy(dev_info_t * dip)72427c478bd9Sstevel@tonic-gate i_ddi_attach_node_hierarchy(dev_info_t *dip)
72437c478bd9Sstevel@tonic-gate {
72447c478bd9Sstevel@tonic-gate dev_info_t *parent;
72453fe80ca4SDan Cross int ret;
7246c73a93f2Sdm120769
7247c73a93f2Sdm120769 /*
72485e3986cbScth * Recurse up until attached parent is found.
7249c73a93f2Sdm120769 */
72505e3986cbScth if (i_ddi_devi_attached(dip))
72515e3986cbScth return (DDI_SUCCESS);
72527c478bd9Sstevel@tonic-gate parent = ddi_get_parent(dip);
72537c478bd9Sstevel@tonic-gate if (i_ddi_attach_node_hierarchy(parent) != DDI_SUCCESS)
72547c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
72557c478bd9Sstevel@tonic-gate
72567c478bd9Sstevel@tonic-gate /*
72575e3986cbScth * Come top-down, expanding .conf nodes under this parent
72585e3986cbScth * and driving attach.
72597c478bd9Sstevel@tonic-gate */
72603fe80ca4SDan Cross ndi_devi_enter(parent);
72617c478bd9Sstevel@tonic-gate (void) i_ndi_make_spec_children(parent, 0);
72625e3986cbScth ret = i_ddi_attachchild(dip);
72633fe80ca4SDan Cross ndi_devi_exit(parent);
72645e3986cbScth
72655e3986cbScth return (ret);
72667c478bd9Sstevel@tonic-gate }
72677c478bd9Sstevel@tonic-gate
72687c478bd9Sstevel@tonic-gate /* keep this function static */
72697c478bd9Sstevel@tonic-gate static int
attach_driver_nodes(major_t major)72707c478bd9Sstevel@tonic-gate attach_driver_nodes(major_t major)
72717c478bd9Sstevel@tonic-gate {
72727c478bd9Sstevel@tonic-gate struct devnames *dnp;
72737c478bd9Sstevel@tonic-gate dev_info_t *dip;
72747c478bd9Sstevel@tonic-gate int error = DDI_FAILURE;
72757c478bd9Sstevel@tonic-gate
72767c478bd9Sstevel@tonic-gate dnp = &devnamesp[major];
72777c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock);
72787c478bd9Sstevel@tonic-gate dip = dnp->dn_head;
72797c478bd9Sstevel@tonic-gate while (dip) {
72807c478bd9Sstevel@tonic-gate ndi_hold_devi(dip);
72817c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
72827c478bd9Sstevel@tonic-gate if (i_ddi_attach_node_hierarchy(dip) == DDI_SUCCESS)
72837c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
7284fa0d09b9Sjiang wu - Sun Microsystems - Beijing China /*
7285fa0d09b9Sjiang wu - Sun Microsystems - Beijing China * Set the 'ddi-config-driver-node' property on a nexus
7286fa0d09b9Sjiang wu - Sun Microsystems - Beijing China * node to cause attach_driver_nodes() to configure all
7287fa0d09b9Sjiang wu - Sun Microsystems - Beijing China * immediate children of the nexus. This property should
7288fa0d09b9Sjiang wu - Sun Microsystems - Beijing China * be set on nodes with immediate children that bind to
7289fa0d09b9Sjiang wu - Sun Microsystems - Beijing China * the same driver as parent.
7290fa0d09b9Sjiang wu - Sun Microsystems - Beijing China */
7291fa0d09b9Sjiang wu - Sun Microsystems - Beijing China if ((error == DDI_SUCCESS) && (ddi_prop_exists(DDI_DEV_T_ANY,
7292fa0d09b9Sjiang wu - Sun Microsystems - Beijing China dip, DDI_PROP_DONTPASS, "ddi-config-driver-node"))) {
7293fa0d09b9Sjiang wu - Sun Microsystems - Beijing China (void) ndi_devi_config(dip, NDI_NO_EVENT);
7294fa0d09b9Sjiang wu - Sun Microsystems - Beijing China }
72957c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock);
72967c478bd9Sstevel@tonic-gate ndi_rele_devi(dip);
72977c478bd9Sstevel@tonic-gate dip = ddi_get_next(dip);
72987c478bd9Sstevel@tonic-gate }
72997c478bd9Sstevel@tonic-gate if (error == DDI_SUCCESS)
73007c478bd9Sstevel@tonic-gate dnp->dn_flags |= DN_NO_AUTODETACH;
73017c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
73027c478bd9Sstevel@tonic-gate
73037c478bd9Sstevel@tonic-gate
73047c478bd9Sstevel@tonic-gate return (error);
73057c478bd9Sstevel@tonic-gate }
73067c478bd9Sstevel@tonic-gate
73077c478bd9Sstevel@tonic-gate /*
73087c478bd9Sstevel@tonic-gate * i_ddi_attach_hw_nodes configures and attaches all hw nodes
73097c478bd9Sstevel@tonic-gate * bound to a specific driver. This function replaces calls to
73107c478bd9Sstevel@tonic-gate * ddi_hold_installed_driver() for drivers with no .conf
73117c478bd9Sstevel@tonic-gate * enumerated nodes.
73127c478bd9Sstevel@tonic-gate *
73137c478bd9Sstevel@tonic-gate * This facility is typically called at boot time to attach
73147c478bd9Sstevel@tonic-gate * platform-specific hardware nodes, such as ppm nodes on xcal
73157c478bd9Sstevel@tonic-gate * and grover and keyswitch nodes on cherrystone. It does not
73167c478bd9Sstevel@tonic-gate * deal with .conf enumerated node. Calling it beyond the boot
73177c478bd9Sstevel@tonic-gate * process is strongly discouraged.
73187c478bd9Sstevel@tonic-gate */
73197c478bd9Sstevel@tonic-gate int
i_ddi_attach_hw_nodes(char * driver)73207c478bd9Sstevel@tonic-gate i_ddi_attach_hw_nodes(char *driver)
73217c478bd9Sstevel@tonic-gate {
73227c478bd9Sstevel@tonic-gate major_t major;
73237c478bd9Sstevel@tonic-gate
73247c478bd9Sstevel@tonic-gate major = ddi_name_to_major(driver);
7325a204de77Scth if (major == DDI_MAJOR_T_NONE)
73267c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
73277c478bd9Sstevel@tonic-gate
73287c478bd9Sstevel@tonic-gate return (attach_driver_nodes(major));
73297c478bd9Sstevel@tonic-gate }
73307c478bd9Sstevel@tonic-gate
73317c478bd9Sstevel@tonic-gate /*
73327c478bd9Sstevel@tonic-gate * i_ddi_attach_pseudo_node configures pseudo drivers which
73337c478bd9Sstevel@tonic-gate * has a single node. The .conf nodes must be enumerated
73347c478bd9Sstevel@tonic-gate * before calling this interface. The dip is held attached
73357c478bd9Sstevel@tonic-gate * upon returning.
73367c478bd9Sstevel@tonic-gate *
73377c478bd9Sstevel@tonic-gate * This facility should only be called only at boot time
73387c478bd9Sstevel@tonic-gate * by the I/O framework.
73397c478bd9Sstevel@tonic-gate */
73407c478bd9Sstevel@tonic-gate dev_info_t *
i_ddi_attach_pseudo_node(char * driver)73417c478bd9Sstevel@tonic-gate i_ddi_attach_pseudo_node(char *driver)
73427c478bd9Sstevel@tonic-gate {
73437c478bd9Sstevel@tonic-gate major_t major;
73447c478bd9Sstevel@tonic-gate dev_info_t *dip;
73457c478bd9Sstevel@tonic-gate
73467c478bd9Sstevel@tonic-gate major = ddi_name_to_major(driver);
7347a204de77Scth if (major == DDI_MAJOR_T_NONE)
73487c478bd9Sstevel@tonic-gate return (NULL);
73497c478bd9Sstevel@tonic-gate
73507c478bd9Sstevel@tonic-gate if (attach_driver_nodes(major) != DDI_SUCCESS)
73517c478bd9Sstevel@tonic-gate return (NULL);
73527c478bd9Sstevel@tonic-gate
73537c478bd9Sstevel@tonic-gate dip = devnamesp[major].dn_head;
73547c478bd9Sstevel@tonic-gate ASSERT(dip && ddi_get_next(dip) == NULL);
73557c478bd9Sstevel@tonic-gate ndi_hold_devi(dip);
73567c478bd9Sstevel@tonic-gate return (dip);
73577c478bd9Sstevel@tonic-gate }
73587c478bd9Sstevel@tonic-gate
73597c478bd9Sstevel@tonic-gate static void
diplist_to_parent_major(dev_info_t * head,char parents[])73607c478bd9Sstevel@tonic-gate diplist_to_parent_major(dev_info_t *head, char parents[])
73617c478bd9Sstevel@tonic-gate {
73627c478bd9Sstevel@tonic-gate major_t major;
73637c478bd9Sstevel@tonic-gate dev_info_t *dip, *pdip;
73647c478bd9Sstevel@tonic-gate
73657c478bd9Sstevel@tonic-gate for (dip = head; dip != NULL; dip = ddi_get_next(dip)) {
73667c478bd9Sstevel@tonic-gate pdip = ddi_get_parent(dip);
73677c478bd9Sstevel@tonic-gate ASSERT(pdip); /* disallow rootnex.conf nodes */
73687c478bd9Sstevel@tonic-gate major = ddi_driver_major(pdip);
7369a204de77Scth if ((major != DDI_MAJOR_T_NONE) && parents[major] == 0)
73707c478bd9Sstevel@tonic-gate parents[major] = 1;
73717c478bd9Sstevel@tonic-gate }
73727c478bd9Sstevel@tonic-gate }
73737c478bd9Sstevel@tonic-gate
73747c478bd9Sstevel@tonic-gate /*
73757c478bd9Sstevel@tonic-gate * Call ddi_hold_installed_driver() on each parent major
73767c478bd9Sstevel@tonic-gate * and invoke mt_config_driver() to attach child major.
73777c478bd9Sstevel@tonic-gate * This is part of the implementation of ddi_hold_installed_driver.
73787c478bd9Sstevel@tonic-gate */
73797c478bd9Sstevel@tonic-gate static int
attach_driver_by_parent(major_t child_major,char parents[])73807c478bd9Sstevel@tonic-gate attach_driver_by_parent(major_t child_major, char parents[])
73817c478bd9Sstevel@tonic-gate {
73827c478bd9Sstevel@tonic-gate major_t par_major;
73837c478bd9Sstevel@tonic-gate struct mt_config_handle *hdl;
73847c478bd9Sstevel@tonic-gate int flags = NDI_DEVI_PERSIST | NDI_NO_EVENT;
73857c478bd9Sstevel@tonic-gate
73867c478bd9Sstevel@tonic-gate hdl = mt_config_init(NULL, NULL, flags, child_major, MT_CONFIG_OP,
73877c478bd9Sstevel@tonic-gate NULL);
73887c478bd9Sstevel@tonic-gate for (par_major = 0; par_major < devcnt; par_major++) {
73897c478bd9Sstevel@tonic-gate /* disallow recursion on the same driver */
73907c478bd9Sstevel@tonic-gate if (parents[par_major] == 0 || par_major == child_major)
73917c478bd9Sstevel@tonic-gate continue;
73927c478bd9Sstevel@tonic-gate if (ddi_hold_installed_driver(par_major) == NULL)
73937c478bd9Sstevel@tonic-gate continue;
73947c478bd9Sstevel@tonic-gate hdl->mtc_parmajor = par_major;
73957c478bd9Sstevel@tonic-gate mt_config_driver(hdl);
73967c478bd9Sstevel@tonic-gate ddi_rele_driver(par_major);
73977c478bd9Sstevel@tonic-gate }
73987c478bd9Sstevel@tonic-gate (void) mt_config_fini(hdl);
73997c478bd9Sstevel@tonic-gate
74007c478bd9Sstevel@tonic-gate return (i_ddi_devs_attached(child_major));
74017c478bd9Sstevel@tonic-gate }
74027c478bd9Sstevel@tonic-gate
74037c478bd9Sstevel@tonic-gate int
i_ddi_devs_attached(major_t major)74047c478bd9Sstevel@tonic-gate i_ddi_devs_attached(major_t major)
74057c478bd9Sstevel@tonic-gate {
74067c478bd9Sstevel@tonic-gate dev_info_t *dip;
74077c478bd9Sstevel@tonic-gate struct devnames *dnp;
74087c478bd9Sstevel@tonic-gate int error = DDI_FAILURE;
74097c478bd9Sstevel@tonic-gate
74107c478bd9Sstevel@tonic-gate /* check for attached instances */
74117c478bd9Sstevel@tonic-gate dnp = &devnamesp[major];
74127c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock);
74137c478bd9Sstevel@tonic-gate for (dip = dnp->dn_head; dip != NULL; dip = ddi_get_next(dip)) {
7414737d277aScth if (i_ddi_devi_attached(dip)) {
74157c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
74167c478bd9Sstevel@tonic-gate break;
74177c478bd9Sstevel@tonic-gate }
74187c478bd9Sstevel@tonic-gate }
74197c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
74207c478bd9Sstevel@tonic-gate
74217c478bd9Sstevel@tonic-gate return (error);
74227c478bd9Sstevel@tonic-gate }
74237c478bd9Sstevel@tonic-gate
7424d62bc4baSyz147064 int
i_ddi_minor_node_count(dev_info_t * ddip,const char * node_type)7425d62bc4baSyz147064 i_ddi_minor_node_count(dev_info_t *ddip, const char *node_type)
7426d62bc4baSyz147064 {
7427d62bc4baSyz147064 struct ddi_minor_data *dp;
7428d62bc4baSyz147064 int count = 0;
7429d62bc4baSyz147064
74303fe80ca4SDan Cross ndi_devi_enter(ddip);
7431b9ccdc5aScth for (dp = DEVI(ddip)->devi_minor; dp != NULL; dp = dp->next) {
7432d62bc4baSyz147064 if (strcmp(dp->ddm_node_type, node_type) == 0)
7433d62bc4baSyz147064 count++;
7434b9ccdc5aScth }
74353fe80ca4SDan Cross ndi_devi_exit(ddip);
7436d62bc4baSyz147064 return (count);
7437d62bc4baSyz147064 }
7438d62bc4baSyz147064
74397c478bd9Sstevel@tonic-gate /*
74407c478bd9Sstevel@tonic-gate * ddi_hold_installed_driver configures and attaches all
74417c478bd9Sstevel@tonic-gate * instances of the specified driver. To accomplish this
74427c478bd9Sstevel@tonic-gate * it configures and attaches all possible parents of
74437c478bd9Sstevel@tonic-gate * the driver, enumerated both in h/w nodes and in the
74447c478bd9Sstevel@tonic-gate * driver's .conf file.
74457c478bd9Sstevel@tonic-gate *
74467c478bd9Sstevel@tonic-gate * NOTE: This facility is for compatibility purposes only and will
74477c478bd9Sstevel@tonic-gate * eventually go away. Its usage is strongly discouraged.
74487c478bd9Sstevel@tonic-gate */
74497c478bd9Sstevel@tonic-gate static void
enter_driver(struct devnames * dnp)74507c478bd9Sstevel@tonic-gate enter_driver(struct devnames *dnp)
74517c478bd9Sstevel@tonic-gate {
74527c478bd9Sstevel@tonic-gate mutex_enter(&dnp->dn_lock);
74537c478bd9Sstevel@tonic-gate ASSERT(dnp->dn_busy_thread != curthread);
74547c478bd9Sstevel@tonic-gate while (dnp->dn_flags & DN_DRIVER_BUSY)
74557c478bd9Sstevel@tonic-gate cv_wait(&dnp->dn_wait, &dnp->dn_lock);
74567c478bd9Sstevel@tonic-gate dnp->dn_flags |= DN_DRIVER_BUSY;
74577c478bd9Sstevel@tonic-gate dnp->dn_busy_thread = curthread;
74587c478bd9Sstevel@tonic-gate mutex_exit(&dnp->dn_lock);
74597c478bd9Sstevel@tonic-gate }
74607c478bd9Sstevel@tonic-gate
74617c478bd9Sstevel@tonic-gate static void
exit_driver(struct devnames * dnp)74627c478bd9Sstevel@tonic-gate exit_driver(struct devnames *dnp)
74637c478bd9Sstevel@tonic-gate {
74647c478bd9Sstevel@tonic-gate mutex_enter(&dnp->dn_lock);
74657c478bd9Sstevel@tonic-gate ASSERT(dnp->dn_busy_thread == curthread);
74667c478bd9Sstevel@tonic-gate dnp->dn_flags &= ~DN_DRIVER_BUSY;
74677c478bd9Sstevel@tonic-gate dnp->dn_busy_thread = NULL;
74687c478bd9Sstevel@tonic-gate cv_broadcast(&dnp->dn_wait);
74697c478bd9Sstevel@tonic-gate mutex_exit(&dnp->dn_lock);
74707c478bd9Sstevel@tonic-gate }
74717c478bd9Sstevel@tonic-gate
74727c478bd9Sstevel@tonic-gate struct dev_ops *
ddi_hold_installed_driver(major_t major)74737c478bd9Sstevel@tonic-gate ddi_hold_installed_driver(major_t major)
74747c478bd9Sstevel@tonic-gate {
74757c478bd9Sstevel@tonic-gate struct dev_ops *ops;
74767c478bd9Sstevel@tonic-gate struct devnames *dnp;
74777c478bd9Sstevel@tonic-gate char *parents;
74787c478bd9Sstevel@tonic-gate int error;
74797c478bd9Sstevel@tonic-gate
74807c478bd9Sstevel@tonic-gate ops = ddi_hold_driver(major);
74817c478bd9Sstevel@tonic-gate if (ops == NULL)
74827c478bd9Sstevel@tonic-gate return (NULL);
74837c478bd9Sstevel@tonic-gate
74847c478bd9Sstevel@tonic-gate /*
74857c478bd9Sstevel@tonic-gate * Return immediately if all the attach operations associated
74867c478bd9Sstevel@tonic-gate * with a ddi_hold_installed_driver() call have already been done.
74877c478bd9Sstevel@tonic-gate */
74887c478bd9Sstevel@tonic-gate dnp = &devnamesp[major];
74897c478bd9Sstevel@tonic-gate enter_driver(dnp);
7490c8742f64SJerry Gilliam ASSERT(driver_active(major));
7491c9cc1492SJerry Gilliam
74927c478bd9Sstevel@tonic-gate if (dnp->dn_flags & DN_DRIVER_HELD) {
74937c478bd9Sstevel@tonic-gate exit_driver(dnp);
74947c478bd9Sstevel@tonic-gate if (i_ddi_devs_attached(major) == DDI_SUCCESS)
74957c478bd9Sstevel@tonic-gate return (ops);
74967c478bd9Sstevel@tonic-gate ddi_rele_driver(major);
74977c478bd9Sstevel@tonic-gate return (NULL);
74987c478bd9Sstevel@tonic-gate }
74997c478bd9Sstevel@tonic-gate
75007c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock);
75017c478bd9Sstevel@tonic-gate dnp->dn_flags |= (DN_DRIVER_HELD | DN_NO_AUTODETACH);
75027c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
75037c478bd9Sstevel@tonic-gate
75047c478bd9Sstevel@tonic-gate DCOMPATPRINTF((CE_CONT,
75057c478bd9Sstevel@tonic-gate "ddi_hold_installed_driver: %s\n", dnp->dn_name));
75067c478bd9Sstevel@tonic-gate
75077c478bd9Sstevel@tonic-gate /*
75087c478bd9Sstevel@tonic-gate * When the driver has no .conf children, it is sufficient
75097c478bd9Sstevel@tonic-gate * to attach existing nodes in the device tree. Nodes not
75107c478bd9Sstevel@tonic-gate * enumerated by the OBP are not attached.
75117c478bd9Sstevel@tonic-gate */
75127c478bd9Sstevel@tonic-gate if (dnp->dn_pl == NULL) {
75137c478bd9Sstevel@tonic-gate if (attach_driver_nodes(major) == DDI_SUCCESS) {
75147c478bd9Sstevel@tonic-gate exit_driver(dnp);
75157c478bd9Sstevel@tonic-gate return (ops);
75167c478bd9Sstevel@tonic-gate }
75177c478bd9Sstevel@tonic-gate exit_driver(dnp);
75187c478bd9Sstevel@tonic-gate ddi_rele_driver(major);
75197c478bd9Sstevel@tonic-gate return (NULL);
75207c478bd9Sstevel@tonic-gate }
75217c478bd9Sstevel@tonic-gate
75227c478bd9Sstevel@tonic-gate /*
75237c478bd9Sstevel@tonic-gate * Driver has .conf nodes. We find all possible parents
75247c478bd9Sstevel@tonic-gate * and recursively all ddi_hold_installed_driver on the
75257c478bd9Sstevel@tonic-gate * parent driver; then we invoke ndi_config_driver()
75267c478bd9Sstevel@tonic-gate * on all possible parent node in parallel to speed up
75277c478bd9Sstevel@tonic-gate * performance.
75287c478bd9Sstevel@tonic-gate */
75297c478bd9Sstevel@tonic-gate parents = kmem_zalloc(devcnt * sizeof (char), KM_SLEEP);
75307c478bd9Sstevel@tonic-gate
75317c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock);
75327c478bd9Sstevel@tonic-gate /* find .conf parents */
75337c478bd9Sstevel@tonic-gate (void) impl_parlist_to_major(dnp->dn_pl, parents);
75347c478bd9Sstevel@tonic-gate /* find hw node parents */
75357c478bd9Sstevel@tonic-gate diplist_to_parent_major(dnp->dn_head, parents);
75367c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
75377c478bd9Sstevel@tonic-gate
75387c478bd9Sstevel@tonic-gate error = attach_driver_by_parent(major, parents);
75397c478bd9Sstevel@tonic-gate kmem_free(parents, devcnt * sizeof (char));
75407c478bd9Sstevel@tonic-gate if (error == DDI_SUCCESS) {
75417c478bd9Sstevel@tonic-gate exit_driver(dnp);
75427c478bd9Sstevel@tonic-gate return (ops);
75437c478bd9Sstevel@tonic-gate }
75447c478bd9Sstevel@tonic-gate
75457c478bd9Sstevel@tonic-gate exit_driver(dnp);
75467c478bd9Sstevel@tonic-gate ddi_rele_driver(major);
75477c478bd9Sstevel@tonic-gate return (NULL);
75487c478bd9Sstevel@tonic-gate }
75497c478bd9Sstevel@tonic-gate
75507c478bd9Sstevel@tonic-gate /*
75517c478bd9Sstevel@tonic-gate * Default bus_config entry point for nexus drivers
75527c478bd9Sstevel@tonic-gate */
75537c478bd9Sstevel@tonic-gate int
ndi_busop_bus_config(dev_info_t * pdip,uint_t flags,ddi_bus_config_op_t op,void * arg,dev_info_t ** child,clock_t timeout)75547c478bd9Sstevel@tonic-gate ndi_busop_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
75557c478bd9Sstevel@tonic-gate void *arg, dev_info_t **child, clock_t timeout)
75567c478bd9Sstevel@tonic-gate {
75577c478bd9Sstevel@tonic-gate major_t major;
75587c478bd9Sstevel@tonic-gate
75597c478bd9Sstevel@tonic-gate /*
75607c478bd9Sstevel@tonic-gate * A timeout of 30 minutes or more is probably a mistake
75617c478bd9Sstevel@tonic-gate * This is intended to catch uses where timeout is in
75627c478bd9Sstevel@tonic-gate * the wrong units. timeout must be in units of ticks.
75637c478bd9Sstevel@tonic-gate */
75647c478bd9Sstevel@tonic-gate ASSERT(timeout < SEC_TO_TICK(1800));
75657c478bd9Sstevel@tonic-gate
7566a204de77Scth major = DDI_MAJOR_T_NONE;
75677c478bd9Sstevel@tonic-gate switch (op) {
75687c478bd9Sstevel@tonic-gate case BUS_CONFIG_ONE:
75697c478bd9Sstevel@tonic-gate NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config %s timeout=%ld\n",
75707c478bd9Sstevel@tonic-gate ddi_driver_name(pdip), ddi_get_instance(pdip),
75717c478bd9Sstevel@tonic-gate (char *)arg, timeout));
75727c478bd9Sstevel@tonic-gate return (devi_config_one(pdip, (char *)arg, child, flags,
75737c478bd9Sstevel@tonic-gate timeout));
75747c478bd9Sstevel@tonic-gate
75757c478bd9Sstevel@tonic-gate case BUS_CONFIG_DRIVER:
75767c478bd9Sstevel@tonic-gate major = (major_t)(uintptr_t)arg;
75777c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/
75787c478bd9Sstevel@tonic-gate case BUS_CONFIG_ALL:
75797c478bd9Sstevel@tonic-gate NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config timeout=%ld\n",
75807c478bd9Sstevel@tonic-gate ddi_driver_name(pdip), ddi_get_instance(pdip),
75817c478bd9Sstevel@tonic-gate timeout));
75827c478bd9Sstevel@tonic-gate if (timeout > 0) {
75837c478bd9Sstevel@tonic-gate NDI_DEBUG(flags, (CE_CONT,
75847c478bd9Sstevel@tonic-gate "%s%d: bus config all timeout=%ld\n",
75857c478bd9Sstevel@tonic-gate ddi_driver_name(pdip), ddi_get_instance(pdip),
75867c478bd9Sstevel@tonic-gate timeout));
75877c478bd9Sstevel@tonic-gate delay(timeout);
75887c478bd9Sstevel@tonic-gate }
75897c478bd9Sstevel@tonic-gate return (config_immediate_children(pdip, flags, major));
75907c478bd9Sstevel@tonic-gate
75917c478bd9Sstevel@tonic-gate default:
75927c478bd9Sstevel@tonic-gate return (NDI_FAILURE);
75937c478bd9Sstevel@tonic-gate }
75947c478bd9Sstevel@tonic-gate /*NOTREACHED*/
75957c478bd9Sstevel@tonic-gate }
75967c478bd9Sstevel@tonic-gate
75977c478bd9Sstevel@tonic-gate /*
75987c478bd9Sstevel@tonic-gate * Default busop bus_unconfig handler for nexus drivers
75997c478bd9Sstevel@tonic-gate */
76007c478bd9Sstevel@tonic-gate int
ndi_busop_bus_unconfig(dev_info_t * pdip,uint_t flags,ddi_bus_config_op_t op,void * arg)76017c478bd9Sstevel@tonic-gate ndi_busop_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
76027c478bd9Sstevel@tonic-gate void *arg)
76037c478bd9Sstevel@tonic-gate {
76047c478bd9Sstevel@tonic-gate major_t major;
76057c478bd9Sstevel@tonic-gate
7606a204de77Scth major = DDI_MAJOR_T_NONE;
76077c478bd9Sstevel@tonic-gate switch (op) {
76087c478bd9Sstevel@tonic-gate case BUS_UNCONFIG_ONE:
76097c478bd9Sstevel@tonic-gate NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig %s\n",
76107c478bd9Sstevel@tonic-gate ddi_driver_name(pdip), ddi_get_instance(pdip),
76117c478bd9Sstevel@tonic-gate (char *)arg));
76127c478bd9Sstevel@tonic-gate return (devi_unconfig_one(pdip, (char *)arg, flags));
76137c478bd9Sstevel@tonic-gate
76147c478bd9Sstevel@tonic-gate case BUS_UNCONFIG_DRIVER:
76157c478bd9Sstevel@tonic-gate major = (major_t)(uintptr_t)arg;
76167c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/
76177c478bd9Sstevel@tonic-gate case BUS_UNCONFIG_ALL:
76187c478bd9Sstevel@tonic-gate NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig all\n",
76197c478bd9Sstevel@tonic-gate ddi_driver_name(pdip), ddi_get_instance(pdip)));
76207c478bd9Sstevel@tonic-gate return (unconfig_immediate_children(pdip, NULL, flags, major));
76217c478bd9Sstevel@tonic-gate
76227c478bd9Sstevel@tonic-gate default:
76237c478bd9Sstevel@tonic-gate return (NDI_FAILURE);
76247c478bd9Sstevel@tonic-gate }
76257c478bd9Sstevel@tonic-gate /*NOTREACHED*/
76267c478bd9Sstevel@tonic-gate }
76277c478bd9Sstevel@tonic-gate
76287c478bd9Sstevel@tonic-gate /*
76297c478bd9Sstevel@tonic-gate * dummy functions to be removed
76307c478bd9Sstevel@tonic-gate */
76317c478bd9Sstevel@tonic-gate void
impl_rem_dev_props(dev_info_t * dip)76327c478bd9Sstevel@tonic-gate impl_rem_dev_props(dev_info_t *dip)
76337c478bd9Sstevel@tonic-gate {
76347c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(dip))
76357c478bd9Sstevel@tonic-gate /* do nothing */
76367c478bd9Sstevel@tonic-gate }
76377c478bd9Sstevel@tonic-gate
76387c478bd9Sstevel@tonic-gate /*
76397c478bd9Sstevel@tonic-gate * Determine if a node is a leaf node. If not sure, return false (0).
76407c478bd9Sstevel@tonic-gate */
76417c478bd9Sstevel@tonic-gate static int
is_leaf_node(dev_info_t * dip)76427c478bd9Sstevel@tonic-gate is_leaf_node(dev_info_t *dip)
76437c478bd9Sstevel@tonic-gate {
76447c478bd9Sstevel@tonic-gate major_t major = ddi_driver_major(dip);
76457c478bd9Sstevel@tonic-gate
7646a204de77Scth if (major == DDI_MAJOR_T_NONE)
76477c478bd9Sstevel@tonic-gate return (0);
76487c478bd9Sstevel@tonic-gate
76497c478bd9Sstevel@tonic-gate return (devnamesp[major].dn_flags & DN_LEAF_DRIVER);
76507c478bd9Sstevel@tonic-gate }
76517c478bd9Sstevel@tonic-gate
76527c478bd9Sstevel@tonic-gate /*
76537c478bd9Sstevel@tonic-gate * Multithreaded [un]configuration
76547c478bd9Sstevel@tonic-gate */
76557c478bd9Sstevel@tonic-gate static struct mt_config_handle *
mt_config_init(dev_info_t * pdip,dev_info_t ** dipp,int flags,major_t major,int op,struct brevq_node ** brevqp)76567c478bd9Sstevel@tonic-gate mt_config_init(dev_info_t *pdip, dev_info_t **dipp, int flags,
76577c478bd9Sstevel@tonic-gate major_t major, int op, struct brevq_node **brevqp)
76587c478bd9Sstevel@tonic-gate {
76597c478bd9Sstevel@tonic-gate struct mt_config_handle *hdl = kmem_alloc(sizeof (*hdl), KM_SLEEP);
76607c478bd9Sstevel@tonic-gate
76617c478bd9Sstevel@tonic-gate mutex_init(&hdl->mtc_lock, NULL, MUTEX_DEFAULT, NULL);
76627c478bd9Sstevel@tonic-gate cv_init(&hdl->mtc_cv, NULL, CV_DEFAULT, NULL);
76637c478bd9Sstevel@tonic-gate hdl->mtc_pdip = pdip;
76647c478bd9Sstevel@tonic-gate hdl->mtc_fdip = dipp;
7665a204de77Scth hdl->mtc_parmajor = DDI_MAJOR_T_NONE;
76667c478bd9Sstevel@tonic-gate hdl->mtc_flags = flags;
76677c478bd9Sstevel@tonic-gate hdl->mtc_major = major;
76687c478bd9Sstevel@tonic-gate hdl->mtc_thr_count = 0;
76697c478bd9Sstevel@tonic-gate hdl->mtc_op = op;
76707c478bd9Sstevel@tonic-gate hdl->mtc_error = 0;
76717c478bd9Sstevel@tonic-gate hdl->mtc_brevqp = brevqp;
76727c478bd9Sstevel@tonic-gate
76737c478bd9Sstevel@tonic-gate #ifdef DEBUG
76747c478bd9Sstevel@tonic-gate gethrestime(&hdl->start_time);
76757c478bd9Sstevel@tonic-gate hdl->total_time = 0;
76767c478bd9Sstevel@tonic-gate #endif /* DEBUG */
76777c478bd9Sstevel@tonic-gate
76787c478bd9Sstevel@tonic-gate return (hdl);
76797c478bd9Sstevel@tonic-gate }
76807c478bd9Sstevel@tonic-gate
76817c478bd9Sstevel@tonic-gate #ifdef DEBUG
76827c478bd9Sstevel@tonic-gate static int
time_diff_in_msec(timestruc_t start,timestruc_t end)76837c478bd9Sstevel@tonic-gate time_diff_in_msec(timestruc_t start, timestruc_t end)
76847c478bd9Sstevel@tonic-gate {
76857c478bd9Sstevel@tonic-gate int nsec, sec;
76867c478bd9Sstevel@tonic-gate
76877c478bd9Sstevel@tonic-gate sec = end.tv_sec - start.tv_sec;
76887c478bd9Sstevel@tonic-gate nsec = end.tv_nsec - start.tv_nsec;
76897c478bd9Sstevel@tonic-gate if (nsec < 0) {
76907c478bd9Sstevel@tonic-gate nsec += NANOSEC;
76917c478bd9Sstevel@tonic-gate sec -= 1;
76927c478bd9Sstevel@tonic-gate }
76937c478bd9Sstevel@tonic-gate
76947c478bd9Sstevel@tonic-gate return (sec * (NANOSEC >> 20) + (nsec >> 20));
76957c478bd9Sstevel@tonic-gate }
76967c478bd9Sstevel@tonic-gate
76977c478bd9Sstevel@tonic-gate #endif /* DEBUG */
76987c478bd9Sstevel@tonic-gate
76997c478bd9Sstevel@tonic-gate static int
mt_config_fini(struct mt_config_handle * hdl)77007c478bd9Sstevel@tonic-gate mt_config_fini(struct mt_config_handle *hdl)
77017c478bd9Sstevel@tonic-gate {
77027c478bd9Sstevel@tonic-gate int rv;
77037c478bd9Sstevel@tonic-gate #ifdef DEBUG
77047c478bd9Sstevel@tonic-gate int real_time;
77057c478bd9Sstevel@tonic-gate timestruc_t end_time;
77067c478bd9Sstevel@tonic-gate #endif /* DEBUG */
77077c478bd9Sstevel@tonic-gate
77087c478bd9Sstevel@tonic-gate mutex_enter(&hdl->mtc_lock);
77097c478bd9Sstevel@tonic-gate while (hdl->mtc_thr_count > 0)
77107c478bd9Sstevel@tonic-gate cv_wait(&hdl->mtc_cv, &hdl->mtc_lock);
77117c478bd9Sstevel@tonic-gate rv = hdl->mtc_error;
77127c478bd9Sstevel@tonic-gate mutex_exit(&hdl->mtc_lock);
77137c478bd9Sstevel@tonic-gate
77147c478bd9Sstevel@tonic-gate #ifdef DEBUG
77157c478bd9Sstevel@tonic-gate gethrestime(&end_time);
77167c478bd9Sstevel@tonic-gate real_time = time_diff_in_msec(hdl->start_time, end_time);
77177c478bd9Sstevel@tonic-gate if ((ddidebug & DDI_MTCONFIG) && hdl->mtc_pdip)
77187c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE,
77197c478bd9Sstevel@tonic-gate "config %s%d: total time %d msec, real time %d msec",
77207c478bd9Sstevel@tonic-gate ddi_driver_name(hdl->mtc_pdip),
77217c478bd9Sstevel@tonic-gate ddi_get_instance(hdl->mtc_pdip),
77227c478bd9Sstevel@tonic-gate hdl->total_time, real_time);
77237c478bd9Sstevel@tonic-gate #endif /* DEBUG */
77247c478bd9Sstevel@tonic-gate
77257c478bd9Sstevel@tonic-gate cv_destroy(&hdl->mtc_cv);
77267c478bd9Sstevel@tonic-gate mutex_destroy(&hdl->mtc_lock);
77277c478bd9Sstevel@tonic-gate kmem_free(hdl, sizeof (*hdl));
77287c478bd9Sstevel@tonic-gate
77297c478bd9Sstevel@tonic-gate return (rv);
77307c478bd9Sstevel@tonic-gate }
77317c478bd9Sstevel@tonic-gate
77327c478bd9Sstevel@tonic-gate struct mt_config_data {
77337c478bd9Sstevel@tonic-gate struct mt_config_handle *mtc_hdl;
77347c478bd9Sstevel@tonic-gate dev_info_t *mtc_dip;
77357c478bd9Sstevel@tonic-gate major_t mtc_major;
77367c478bd9Sstevel@tonic-gate int mtc_flags;
77377c478bd9Sstevel@tonic-gate struct brevq_node *mtc_brn;
77387c478bd9Sstevel@tonic-gate struct mt_config_data *mtc_next;
77397c478bd9Sstevel@tonic-gate };
77407c478bd9Sstevel@tonic-gate
77417c478bd9Sstevel@tonic-gate static void
mt_config_thread(void * arg)77427c478bd9Sstevel@tonic-gate mt_config_thread(void *arg)
77437c478bd9Sstevel@tonic-gate {
77447c478bd9Sstevel@tonic-gate struct mt_config_data *mcd = (struct mt_config_data *)arg;
77457c478bd9Sstevel@tonic-gate struct mt_config_handle *hdl = mcd->mtc_hdl;
77467c478bd9Sstevel@tonic-gate dev_info_t *dip = mcd->mtc_dip;
77477c478bd9Sstevel@tonic-gate dev_info_t *rdip, **dipp;
77487c478bd9Sstevel@tonic-gate major_t major = mcd->mtc_major;
77497c478bd9Sstevel@tonic-gate int flags = mcd->mtc_flags;
77507c478bd9Sstevel@tonic-gate int rv = 0;
77517c478bd9Sstevel@tonic-gate
77527c478bd9Sstevel@tonic-gate #ifdef DEBUG
77537c478bd9Sstevel@tonic-gate timestruc_t start_time, end_time;
77547c478bd9Sstevel@tonic-gate gethrestime(&start_time);
77557c478bd9Sstevel@tonic-gate #endif /* DEBUG */
77567c478bd9Sstevel@tonic-gate
77577c478bd9Sstevel@tonic-gate rdip = NULL;
77587c478bd9Sstevel@tonic-gate dipp = hdl->mtc_fdip ? &rdip : NULL;
77597c478bd9Sstevel@tonic-gate
77607c478bd9Sstevel@tonic-gate switch (hdl->mtc_op) {
77617c478bd9Sstevel@tonic-gate case MT_CONFIG_OP:
77627c478bd9Sstevel@tonic-gate rv = devi_config_common(dip, flags, major);
77637c478bd9Sstevel@tonic-gate break;
77647c478bd9Sstevel@tonic-gate case MT_UNCONFIG_OP:
77657c478bd9Sstevel@tonic-gate if (mcd->mtc_brn) {
77667c478bd9Sstevel@tonic-gate struct brevq_node *brevq = NULL;
77677c478bd9Sstevel@tonic-gate rv = devi_unconfig_common(dip, dipp, flags, major,
77687c478bd9Sstevel@tonic-gate &brevq);
7769245c82d9Scth mcd->mtc_brn->brn_child = brevq;
77707c478bd9Sstevel@tonic-gate } else
77717c478bd9Sstevel@tonic-gate rv = devi_unconfig_common(dip, dipp, flags, major,
77727c478bd9Sstevel@tonic-gate NULL);
77737c478bd9Sstevel@tonic-gate break;
77747c478bd9Sstevel@tonic-gate }
77757c478bd9Sstevel@tonic-gate
77767c478bd9Sstevel@tonic-gate mutex_enter(&hdl->mtc_lock);
77777c478bd9Sstevel@tonic-gate #ifdef DEBUG
77787c478bd9Sstevel@tonic-gate gethrestime(&end_time);
77797c478bd9Sstevel@tonic-gate hdl->total_time += time_diff_in_msec(start_time, end_time);
77807c478bd9Sstevel@tonic-gate #endif /* DEBUG */
77815e3986cbScth
77825e3986cbScth if ((rv != NDI_SUCCESS) && (hdl->mtc_error == 0)) {
77837c478bd9Sstevel@tonic-gate hdl->mtc_error = rv;
77845e3986cbScth #ifdef DEBUG
7785a204de77Scth if ((ddidebug & DDI_DEBUG) && (major != DDI_MAJOR_T_NONE)) {
77865e3986cbScth char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
77875e3986cbScth
77885e3986cbScth (void) ddi_pathname(dip, path);
77895e3986cbScth cmn_err(CE_NOTE, "mt_config_thread: "
77905e3986cbScth "op %d.%d.%x at %s failed %d",
77915e3986cbScth hdl->mtc_op, major, flags, path, rv);
77925e3986cbScth kmem_free(path, MAXPATHLEN);
77935e3986cbScth }
77945e3986cbScth #endif /* DEBUG */
77955e3986cbScth }
77965e3986cbScth
77977c478bd9Sstevel@tonic-gate if (hdl->mtc_fdip && *hdl->mtc_fdip == NULL) {
77987c478bd9Sstevel@tonic-gate *hdl->mtc_fdip = rdip;
77997c478bd9Sstevel@tonic-gate rdip = NULL;
78007c478bd9Sstevel@tonic-gate }
78017c478bd9Sstevel@tonic-gate
78027c478bd9Sstevel@tonic-gate if (rdip) {
78037c478bd9Sstevel@tonic-gate ASSERT(rv != NDI_SUCCESS);
78047c478bd9Sstevel@tonic-gate ndi_rele_devi(rdip);
78057c478bd9Sstevel@tonic-gate }
78067c478bd9Sstevel@tonic-gate
78077c478bd9Sstevel@tonic-gate ndi_rele_devi(dip);
78083b3b7f33Sbm42561
78093b3b7f33Sbm42561 if (--hdl->mtc_thr_count == 0)
78103b3b7f33Sbm42561 cv_broadcast(&hdl->mtc_cv);
78113b3b7f33Sbm42561 mutex_exit(&hdl->mtc_lock);
78127c478bd9Sstevel@tonic-gate kmem_free(mcd, sizeof (*mcd));
78137c478bd9Sstevel@tonic-gate }
78147c478bd9Sstevel@tonic-gate
78157c478bd9Sstevel@tonic-gate /*
78167c478bd9Sstevel@tonic-gate * Multi-threaded config/unconfig of child nexus
78177c478bd9Sstevel@tonic-gate */
78187c478bd9Sstevel@tonic-gate static void
mt_config_children(struct mt_config_handle * hdl)78197c478bd9Sstevel@tonic-gate mt_config_children(struct mt_config_handle *hdl)
78207c478bd9Sstevel@tonic-gate {
78217c478bd9Sstevel@tonic-gate dev_info_t *pdip = hdl->mtc_pdip;
78227c478bd9Sstevel@tonic-gate major_t major = hdl->mtc_major;
78237c478bd9Sstevel@tonic-gate dev_info_t *dip;
7824245c82d9Scth struct brevq_node *brn;
78257c478bd9Sstevel@tonic-gate struct mt_config_data *mcd_head = NULL;
78267c478bd9Sstevel@tonic-gate struct mt_config_data *mcd_tail = NULL;
78277c478bd9Sstevel@tonic-gate struct mt_config_data *mcd;
78287c478bd9Sstevel@tonic-gate #ifdef DEBUG
78297c478bd9Sstevel@tonic-gate timestruc_t end_time;
78307c478bd9Sstevel@tonic-gate
78317c478bd9Sstevel@tonic-gate /* Update total_time in handle */
78327c478bd9Sstevel@tonic-gate gethrestime(&end_time);
78337c478bd9Sstevel@tonic-gate hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
78347c478bd9Sstevel@tonic-gate #endif
78357c478bd9Sstevel@tonic-gate
78363fe80ca4SDan Cross ndi_devi_enter(pdip);
78377c478bd9Sstevel@tonic-gate dip = ddi_get_child(pdip);
78387c478bd9Sstevel@tonic-gate while (dip) {
78397c478bd9Sstevel@tonic-gate if (hdl->mtc_op == MT_UNCONFIG_OP && hdl->mtc_brevqp &&
78407c478bd9Sstevel@tonic-gate !(DEVI_EVREMOVE(dip)) &&
78417c478bd9Sstevel@tonic-gate i_ddi_node_state(dip) >= DS_INITIALIZED) {
78427c478bd9Sstevel@tonic-gate /*
78437c478bd9Sstevel@tonic-gate * Enqueue this dip's deviname.
78447c478bd9Sstevel@tonic-gate * No need to hold a lock while enqueuing since this
78457c478bd9Sstevel@tonic-gate * is the only thread doing the enqueue and no one
78467c478bd9Sstevel@tonic-gate * walks the queue while we are in multithreaded
78477c478bd9Sstevel@tonic-gate * unconfiguration.
78487c478bd9Sstevel@tonic-gate */
78497c478bd9Sstevel@tonic-gate brn = brevq_enqueue(hdl->mtc_brevqp, dip, NULL);
7850245c82d9Scth } else
7851245c82d9Scth brn = NULL;
78527c478bd9Sstevel@tonic-gate
78537c478bd9Sstevel@tonic-gate /*
785448bbca81SDaniel Hoffman * Hold the child that we are processing so it does not get
78557c478bd9Sstevel@tonic-gate * removed. The corrisponding ndi_rele_devi() for children
78567c478bd9Sstevel@tonic-gate * that are not being skipped is done at the end of
78577c478bd9Sstevel@tonic-gate * mt_config_thread().
78587c478bd9Sstevel@tonic-gate */
78597c478bd9Sstevel@tonic-gate ndi_hold_devi(dip);
78607c478bd9Sstevel@tonic-gate
78617c478bd9Sstevel@tonic-gate /*
78627c478bd9Sstevel@tonic-gate * skip leaf nodes and (for configure) nodes not
78637c478bd9Sstevel@tonic-gate * fully attached.
78647c478bd9Sstevel@tonic-gate */
78657c478bd9Sstevel@tonic-gate if (is_leaf_node(dip) ||
78667c478bd9Sstevel@tonic-gate (hdl->mtc_op == MT_CONFIG_OP &&
78677c478bd9Sstevel@tonic-gate i_ddi_node_state(dip) < DS_READY)) {
78687c478bd9Sstevel@tonic-gate ndi_rele_devi(dip);
78697c478bd9Sstevel@tonic-gate dip = ddi_get_next_sibling(dip);
78707c478bd9Sstevel@tonic-gate continue;
78717c478bd9Sstevel@tonic-gate }
78727c478bd9Sstevel@tonic-gate
78737c478bd9Sstevel@tonic-gate mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
78747c478bd9Sstevel@tonic-gate mcd->mtc_dip = dip;
78757c478bd9Sstevel@tonic-gate mcd->mtc_hdl = hdl;
78767c478bd9Sstevel@tonic-gate mcd->mtc_brn = brn;
78777c478bd9Sstevel@tonic-gate
78787c478bd9Sstevel@tonic-gate /*
78797c478bd9Sstevel@tonic-gate * Switch a 'driver' operation to an 'all' operation below a
78807c478bd9Sstevel@tonic-gate * node bound to the driver.
78817c478bd9Sstevel@tonic-gate */
7882a204de77Scth if ((major == DDI_MAJOR_T_NONE) ||
7883a204de77Scth (major == ddi_driver_major(dip)))
7884a204de77Scth mcd->mtc_major = DDI_MAJOR_T_NONE;
78857c478bd9Sstevel@tonic-gate else
78867c478bd9Sstevel@tonic-gate mcd->mtc_major = major;
78877c478bd9Sstevel@tonic-gate
78887c478bd9Sstevel@tonic-gate /*
78897c478bd9Sstevel@tonic-gate * The unconfig-driver to unconfig-all conversion above
78907c478bd9Sstevel@tonic-gate * constitutes an autodetach for NDI_DETACH_DRIVER calls,
78917c478bd9Sstevel@tonic-gate * set NDI_AUTODETACH.
78927c478bd9Sstevel@tonic-gate */
78937c478bd9Sstevel@tonic-gate mcd->mtc_flags = hdl->mtc_flags;
78947c478bd9Sstevel@tonic-gate if ((mcd->mtc_flags & NDI_DETACH_DRIVER) &&
78957c478bd9Sstevel@tonic-gate (hdl->mtc_op == MT_UNCONFIG_OP) &&
78967c478bd9Sstevel@tonic-gate (major == ddi_driver_major(pdip)))
78977c478bd9Sstevel@tonic-gate mcd->mtc_flags |= NDI_AUTODETACH;
78987c478bd9Sstevel@tonic-gate
78997c478bd9Sstevel@tonic-gate mutex_enter(&hdl->mtc_lock);
79007c478bd9Sstevel@tonic-gate hdl->mtc_thr_count++;
79017c478bd9Sstevel@tonic-gate mutex_exit(&hdl->mtc_lock);
79027c478bd9Sstevel@tonic-gate
79037c478bd9Sstevel@tonic-gate /*
79047c478bd9Sstevel@tonic-gate * Add to end of list to process after ndi_devi_exit to avoid
79057c478bd9Sstevel@tonic-gate * locking differences depending on value of mtc_off.
79067c478bd9Sstevel@tonic-gate */
79077c478bd9Sstevel@tonic-gate mcd->mtc_next = NULL;
79087c478bd9Sstevel@tonic-gate if (mcd_head == NULL)
79097c478bd9Sstevel@tonic-gate mcd_head = mcd;
79107c478bd9Sstevel@tonic-gate else
79117c478bd9Sstevel@tonic-gate mcd_tail->mtc_next = mcd;
79127c478bd9Sstevel@tonic-gate mcd_tail = mcd;
79137c478bd9Sstevel@tonic-gate
79147c478bd9Sstevel@tonic-gate dip = ddi_get_next_sibling(dip);
79157c478bd9Sstevel@tonic-gate }
79163fe80ca4SDan Cross ndi_devi_exit(pdip);
79177c478bd9Sstevel@tonic-gate
79187c478bd9Sstevel@tonic-gate /* go through the list of held children */
79197c478bd9Sstevel@tonic-gate for (mcd = mcd_head; mcd; mcd = mcd_head) {
79207c478bd9Sstevel@tonic-gate mcd_head = mcd->mtc_next;
79215e3986cbScth if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
79227c478bd9Sstevel@tonic-gate mt_config_thread(mcd);
79237c478bd9Sstevel@tonic-gate else
79247c478bd9Sstevel@tonic-gate (void) thread_create(NULL, 0, mt_config_thread, mcd,
79257c478bd9Sstevel@tonic-gate 0, &p0, TS_RUN, minclsyspri);
79267c478bd9Sstevel@tonic-gate }
79277c478bd9Sstevel@tonic-gate }
79287c478bd9Sstevel@tonic-gate
79297c478bd9Sstevel@tonic-gate static void
mt_config_driver(struct mt_config_handle * hdl)79307c478bd9Sstevel@tonic-gate mt_config_driver(struct mt_config_handle *hdl)
79317c478bd9Sstevel@tonic-gate {
79327c478bd9Sstevel@tonic-gate major_t par_major = hdl->mtc_parmajor;
79337c478bd9Sstevel@tonic-gate major_t major = hdl->mtc_major;
79347c478bd9Sstevel@tonic-gate struct devnames *dnp = &devnamesp[par_major];
79357c478bd9Sstevel@tonic-gate dev_info_t *dip;
79367c478bd9Sstevel@tonic-gate struct mt_config_data *mcd_head = NULL;
79377c478bd9Sstevel@tonic-gate struct mt_config_data *mcd_tail = NULL;
79387c478bd9Sstevel@tonic-gate struct mt_config_data *mcd;
79397c478bd9Sstevel@tonic-gate #ifdef DEBUG
79407c478bd9Sstevel@tonic-gate timestruc_t end_time;
79417c478bd9Sstevel@tonic-gate
79427c478bd9Sstevel@tonic-gate /* Update total_time in handle */
79437c478bd9Sstevel@tonic-gate gethrestime(&end_time);
79447c478bd9Sstevel@tonic-gate hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
79457c478bd9Sstevel@tonic-gate #endif
7946a204de77Scth ASSERT(par_major != DDI_MAJOR_T_NONE);
7947a204de77Scth ASSERT(major != DDI_MAJOR_T_NONE);
79487c478bd9Sstevel@tonic-gate
79497c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock);
79507c478bd9Sstevel@tonic-gate dip = devnamesp[par_major].dn_head;
79517c478bd9Sstevel@tonic-gate while (dip) {
79527c478bd9Sstevel@tonic-gate /*
795348bbca81SDaniel Hoffman * Hold the child that we are processing so it does not get
79547c478bd9Sstevel@tonic-gate * removed. The corrisponding ndi_rele_devi() for children
79557c478bd9Sstevel@tonic-gate * that are not being skipped is done at the end of
79567c478bd9Sstevel@tonic-gate * mt_config_thread().
79577c478bd9Sstevel@tonic-gate */
79587c478bd9Sstevel@tonic-gate ndi_hold_devi(dip);
79597c478bd9Sstevel@tonic-gate
79607c478bd9Sstevel@tonic-gate /* skip leaf nodes and nodes not fully attached */
7961737d277aScth if (!i_ddi_devi_attached(dip) || is_leaf_node(dip)) {
79627c478bd9Sstevel@tonic-gate ndi_rele_devi(dip);
79637c478bd9Sstevel@tonic-gate dip = ddi_get_next(dip);
79647c478bd9Sstevel@tonic-gate continue;
79657c478bd9Sstevel@tonic-gate }
79667c478bd9Sstevel@tonic-gate
79677c478bd9Sstevel@tonic-gate mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
79687c478bd9Sstevel@tonic-gate mcd->mtc_dip = dip;
79697c478bd9Sstevel@tonic-gate mcd->mtc_hdl = hdl;
79707c478bd9Sstevel@tonic-gate mcd->mtc_major = major;
79717c478bd9Sstevel@tonic-gate mcd->mtc_flags = hdl->mtc_flags;
79727c478bd9Sstevel@tonic-gate
79737c478bd9Sstevel@tonic-gate mutex_enter(&hdl->mtc_lock);
79747c478bd9Sstevel@tonic-gate hdl->mtc_thr_count++;
79757c478bd9Sstevel@tonic-gate mutex_exit(&hdl->mtc_lock);
79767c478bd9Sstevel@tonic-gate
79777c478bd9Sstevel@tonic-gate /*
79787c478bd9Sstevel@tonic-gate * Add to end of list to process after UNLOCK_DEV_OPS to avoid
79797c478bd9Sstevel@tonic-gate * locking differences depending on value of mtc_off.
79807c478bd9Sstevel@tonic-gate */
79817c478bd9Sstevel@tonic-gate mcd->mtc_next = NULL;
79827c478bd9Sstevel@tonic-gate if (mcd_head == NULL)
79837c478bd9Sstevel@tonic-gate mcd_head = mcd;
79847c478bd9Sstevel@tonic-gate else
79857c478bd9Sstevel@tonic-gate mcd_tail->mtc_next = mcd;
79867c478bd9Sstevel@tonic-gate mcd_tail = mcd;
79877c478bd9Sstevel@tonic-gate
79887c478bd9Sstevel@tonic-gate dip = ddi_get_next(dip);
79897c478bd9Sstevel@tonic-gate }
79907c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
79917c478bd9Sstevel@tonic-gate
79927c478bd9Sstevel@tonic-gate /* go through the list of held children */
79937c478bd9Sstevel@tonic-gate for (mcd = mcd_head; mcd; mcd = mcd_head) {
79947c478bd9Sstevel@tonic-gate mcd_head = mcd->mtc_next;
79955e3986cbScth if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
79967c478bd9Sstevel@tonic-gate mt_config_thread(mcd);
79977c478bd9Sstevel@tonic-gate else
79987c478bd9Sstevel@tonic-gate (void) thread_create(NULL, 0, mt_config_thread, mcd,
79997c478bd9Sstevel@tonic-gate 0, &p0, TS_RUN, minclsyspri);
80007c478bd9Sstevel@tonic-gate }
80017c478bd9Sstevel@tonic-gate }
80027c478bd9Sstevel@tonic-gate
80037c478bd9Sstevel@tonic-gate /*
80047c478bd9Sstevel@tonic-gate * Given the nodeid for a persistent (PROM or SID) node, return
80057c478bd9Sstevel@tonic-gate * the corresponding devinfo node
80067c478bd9Sstevel@tonic-gate * NOTE: This function will return NULL for .conf nodeids.
80077c478bd9Sstevel@tonic-gate */
80087c478bd9Sstevel@tonic-gate dev_info_t *
e_ddi_nodeid_to_dip(pnode_t nodeid)8009fa9e4066Sahrens e_ddi_nodeid_to_dip(pnode_t nodeid)
80107c478bd9Sstevel@tonic-gate {
80117c478bd9Sstevel@tonic-gate dev_info_t *dip = NULL;
80127c478bd9Sstevel@tonic-gate struct devi_nodeid *prev, *elem;
80137c478bd9Sstevel@tonic-gate
80147c478bd9Sstevel@tonic-gate mutex_enter(&devimap->dno_lock);
80157c478bd9Sstevel@tonic-gate
80167c478bd9Sstevel@tonic-gate prev = NULL;
80177c478bd9Sstevel@tonic-gate for (elem = devimap->dno_head; elem; elem = elem->next) {
80187c478bd9Sstevel@tonic-gate if (elem->nodeid == nodeid) {
80197c478bd9Sstevel@tonic-gate ndi_hold_devi(elem->dip);
80207c478bd9Sstevel@tonic-gate dip = elem->dip;
80217c478bd9Sstevel@tonic-gate break;
80227c478bd9Sstevel@tonic-gate }
80237c478bd9Sstevel@tonic-gate prev = elem;
80247c478bd9Sstevel@tonic-gate }
80257c478bd9Sstevel@tonic-gate
80267c478bd9Sstevel@tonic-gate /*
80277c478bd9Sstevel@tonic-gate * Move to head for faster lookup next time
80287c478bd9Sstevel@tonic-gate */
80297c478bd9Sstevel@tonic-gate if (elem && prev) {
80307c478bd9Sstevel@tonic-gate prev->next = elem->next;
80317c478bd9Sstevel@tonic-gate elem->next = devimap->dno_head;
80327c478bd9Sstevel@tonic-gate devimap->dno_head = elem;
80337c478bd9Sstevel@tonic-gate }
80347c478bd9Sstevel@tonic-gate
80357c478bd9Sstevel@tonic-gate mutex_exit(&devimap->dno_lock);
80367c478bd9Sstevel@tonic-gate return (dip);
80377c478bd9Sstevel@tonic-gate }
80387c478bd9Sstevel@tonic-gate
80397c478bd9Sstevel@tonic-gate static void
free_cache_task(void * arg)80407c478bd9Sstevel@tonic-gate free_cache_task(void *arg)
80417c478bd9Sstevel@tonic-gate {
80427c478bd9Sstevel@tonic-gate ASSERT(arg == NULL);
80437c478bd9Sstevel@tonic-gate
80447c478bd9Sstevel@tonic-gate mutex_enter(&di_cache.cache_lock);
80457c478bd9Sstevel@tonic-gate
80467c478bd9Sstevel@tonic-gate /*
80477c478bd9Sstevel@tonic-gate * The cache can be invalidated without holding the lock
80487c478bd9Sstevel@tonic-gate * but it can be made valid again only while the lock is held.
80497c478bd9Sstevel@tonic-gate * So if the cache is invalid when the lock is held, it will
80507c478bd9Sstevel@tonic-gate * stay invalid until lock is released.
80517c478bd9Sstevel@tonic-gate */
80527c478bd9Sstevel@tonic-gate if (!di_cache.cache_valid)
80537c478bd9Sstevel@tonic-gate i_ddi_di_cache_free(&di_cache);
80547c478bd9Sstevel@tonic-gate
80557c478bd9Sstevel@tonic-gate mutex_exit(&di_cache.cache_lock);
80567c478bd9Sstevel@tonic-gate
80577c478bd9Sstevel@tonic-gate if (di_cache_debug)
80587c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "system_taskq: di_cache freed");
80597c478bd9Sstevel@tonic-gate }
80607c478bd9Sstevel@tonic-gate
80617c478bd9Sstevel@tonic-gate extern int modrootloaded;
80627c478bd9Sstevel@tonic-gate
80637c478bd9Sstevel@tonic-gate void
i_ddi_di_cache_free(struct di_cache * cache)80647c478bd9Sstevel@tonic-gate i_ddi_di_cache_free(struct di_cache *cache)
80657c478bd9Sstevel@tonic-gate {
80667c478bd9Sstevel@tonic-gate int error;
806719397407SSherry Moore extern int sys_shutdown;
80687c478bd9Sstevel@tonic-gate
80697c478bd9Sstevel@tonic-gate ASSERT(mutex_owned(&cache->cache_lock));
80707c478bd9Sstevel@tonic-gate
80717c478bd9Sstevel@tonic-gate if (cache->cache_size) {
80727c478bd9Sstevel@tonic-gate ASSERT(cache->cache_size > 0);
80737c478bd9Sstevel@tonic-gate ASSERT(cache->cache_data);
80747c478bd9Sstevel@tonic-gate
80757c478bd9Sstevel@tonic-gate kmem_free(cache->cache_data, cache->cache_size);
80767c478bd9Sstevel@tonic-gate cache->cache_data = NULL;
80777c478bd9Sstevel@tonic-gate cache->cache_size = 0;
80787c478bd9Sstevel@tonic-gate
80797c478bd9Sstevel@tonic-gate if (di_cache_debug)
80807c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "i_ddi_di_cache_free: freed cachemem");
80817c478bd9Sstevel@tonic-gate } else {
80827c478bd9Sstevel@tonic-gate ASSERT(cache->cache_data == NULL);
80837c478bd9Sstevel@tonic-gate if (di_cache_debug)
80847c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "i_ddi_di_cache_free: NULL cache");
80857c478bd9Sstevel@tonic-gate }
80867c478bd9Sstevel@tonic-gate
8087c3b4ae18SJerry Gilliam if (!modrootloaded || rootvp == NULL ||
8088c3b4ae18SJerry Gilliam vn_is_readonly(rootvp) || sys_shutdown) {
80897c478bd9Sstevel@tonic-gate if (di_cache_debug) {
80907c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "/ not mounted/RDONLY. Skip unlink");
80917c478bd9Sstevel@tonic-gate }
80927c478bd9Sstevel@tonic-gate return;
80937c478bd9Sstevel@tonic-gate }
80947c478bd9Sstevel@tonic-gate
80957c478bd9Sstevel@tonic-gate error = vn_remove(DI_CACHE_FILE, UIO_SYSSPACE, RMFILE);
80967c478bd9Sstevel@tonic-gate if (di_cache_debug && error && error != ENOENT) {
80977c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s: unlink failed: %d", DI_CACHE_FILE, error);
80987c478bd9Sstevel@tonic-gate } else if (di_cache_debug && !error) {
80997c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "i_ddi_di_cache_free: unlinked cache file");
81007c478bd9Sstevel@tonic-gate }
81017c478bd9Sstevel@tonic-gate }
81027c478bd9Sstevel@tonic-gate
81037c478bd9Sstevel@tonic-gate void
i_ddi_di_cache_invalidate()81044c06356bSdh142964 i_ddi_di_cache_invalidate()
81057c478bd9Sstevel@tonic-gate {
8106e37c6c37Scth int cache_valid;
81077c478bd9Sstevel@tonic-gate
81087c478bd9Sstevel@tonic-gate if (!modrootloaded || !i_ddi_io_initialized()) {
81097c478bd9Sstevel@tonic-gate if (di_cache_debug)
81107c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "I/O not inited. Skipping invalidate");
81117c478bd9Sstevel@tonic-gate return;
81127c478bd9Sstevel@tonic-gate }
81137c478bd9Sstevel@tonic-gate
8114e37c6c37Scth /* Increment devtree generation number. */
8115facf4a8dSllai1 atomic_inc_ulong(&devtree_gen);
81167c478bd9Sstevel@tonic-gate
8117e37c6c37Scth /* Invalidate the in-core cache and dispatch free on valid->invalid */
8118e37c6c37Scth cache_valid = atomic_swap_uint(&di_cache.cache_valid, 0);
8119e37c6c37Scth if (cache_valid) {
81204c06356bSdh142964 /*
81214c06356bSdh142964 * This is an optimization to start cleaning up a cached
81224c06356bSdh142964 * snapshot early. For this reason, it is OK for
81234c06356bSdh142964 * taskq_dispatach to fail (and it is OK to not track calling
81244c06356bSdh142964 * context relative to sleep, and assume NOSLEEP).
81254c06356bSdh142964 */
8126e37c6c37Scth (void) taskq_dispatch(system_taskq, free_cache_task, NULL,
81274c06356bSdh142964 TQ_NOSLEEP);
8128e37c6c37Scth }
81297c478bd9Sstevel@tonic-gate
81307c478bd9Sstevel@tonic-gate if (di_cache_debug) {
81314c06356bSdh142964 cmn_err(CE_NOTE, "invalidation");
81327c478bd9Sstevel@tonic-gate }
81337c478bd9Sstevel@tonic-gate }
81347c478bd9Sstevel@tonic-gate
81357c478bd9Sstevel@tonic-gate
81367c478bd9Sstevel@tonic-gate static void
i_bind_vhci_node(dev_info_t * dip)81377c478bd9Sstevel@tonic-gate i_bind_vhci_node(dev_info_t *dip)
81387c478bd9Sstevel@tonic-gate {
8139f4da9be0Scth DEVI(dip)->devi_major = ddi_name_to_major(ddi_node_name(dip));
81407c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_BOUND);
81417c478bd9Sstevel@tonic-gate }
81427c478bd9Sstevel@tonic-gate
81437c478bd9Sstevel@tonic-gate static char vhci_node_addr[2];
81447c478bd9Sstevel@tonic-gate
81457c478bd9Sstevel@tonic-gate static int
i_init_vhci_node(dev_info_t * dip)81467c478bd9Sstevel@tonic-gate i_init_vhci_node(dev_info_t *dip)
81477c478bd9Sstevel@tonic-gate {
81487c478bd9Sstevel@tonic-gate add_global_props(dip);
81497c478bd9Sstevel@tonic-gate DEVI(dip)->devi_ops = ndi_hold_driver(dip);
81507c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_ops == NULL)
81517c478bd9Sstevel@tonic-gate return (-1);
81527c478bd9Sstevel@tonic-gate
81537c478bd9Sstevel@tonic-gate DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
81547c478bd9Sstevel@tonic-gate e_ddi_keep_instance(dip);
81557c478bd9Sstevel@tonic-gate vhci_node_addr[0] = '\0';
81567c478bd9Sstevel@tonic-gate ddi_set_name_addr(dip, vhci_node_addr);
81577c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_INITIALIZED);
81587c478bd9Sstevel@tonic-gate return (0);
81597c478bd9Sstevel@tonic-gate }
81607c478bd9Sstevel@tonic-gate
81617c478bd9Sstevel@tonic-gate static void
i_link_vhci_node(dev_info_t * dip)81627c478bd9Sstevel@tonic-gate i_link_vhci_node(dev_info_t *dip)
81637c478bd9Sstevel@tonic-gate {
81649d3d2ed0Shiremath ASSERT(MUTEX_HELD(&global_vhci_lock));
81659d3d2ed0Shiremath
81667c478bd9Sstevel@tonic-gate /*
81677c478bd9Sstevel@tonic-gate * scsi_vhci should be kept left most of the device tree.
81687c478bd9Sstevel@tonic-gate */
81697c478bd9Sstevel@tonic-gate if (scsi_vhci_dip) {
81707c478bd9Sstevel@tonic-gate DEVI(dip)->devi_sibling = DEVI(scsi_vhci_dip)->devi_sibling;
81717c478bd9Sstevel@tonic-gate DEVI(scsi_vhci_dip)->devi_sibling = DEVI(dip);
81727c478bd9Sstevel@tonic-gate } else {
81737c478bd9Sstevel@tonic-gate DEVI(dip)->devi_sibling = DEVI(top_devinfo)->devi_child;
81747c478bd9Sstevel@tonic-gate DEVI(top_devinfo)->devi_child = DEVI(dip);
81757c478bd9Sstevel@tonic-gate }
81767c478bd9Sstevel@tonic-gate }
81777c478bd9Sstevel@tonic-gate
81787c478bd9Sstevel@tonic-gate
81797c478bd9Sstevel@tonic-gate /*
81807c478bd9Sstevel@tonic-gate * This a special routine to enumerate vhci node (child of rootnex
81817c478bd9Sstevel@tonic-gate * node) without holding the ndi_devi_enter() lock. The device node
81827c478bd9Sstevel@tonic-gate * is allocated, initialized and brought into DS_READY state before
81837c478bd9Sstevel@tonic-gate * inserting into the device tree. The VHCI node is handcrafted
81847c478bd9Sstevel@tonic-gate * here to bring the node to DS_READY, similar to rootnex node.
81857c478bd9Sstevel@tonic-gate *
81867c478bd9Sstevel@tonic-gate * The global_vhci_lock protects linking the node into the device
81877c478bd9Sstevel@tonic-gate * as same lock is held before linking/unlinking any direct child
81887c478bd9Sstevel@tonic-gate * of rootnex children.
81897c478bd9Sstevel@tonic-gate *
81907c478bd9Sstevel@tonic-gate * This routine is a workaround to handle a possible deadlock
81917c478bd9Sstevel@tonic-gate * that occurs while trying to enumerate node in a different sub-tree
81927c478bd9Sstevel@tonic-gate * during _init/_attach entry points.
81937c478bd9Sstevel@tonic-gate */
81947c478bd9Sstevel@tonic-gate /*ARGSUSED*/
81957c478bd9Sstevel@tonic-gate dev_info_t *
ndi_devi_config_vhci(char * drvname,int flags)81967c478bd9Sstevel@tonic-gate ndi_devi_config_vhci(char *drvname, int flags)
81977c478bd9Sstevel@tonic-gate {
81987c478bd9Sstevel@tonic-gate struct devnames *dnp;
81997c478bd9Sstevel@tonic-gate dev_info_t *dip;
82007c478bd9Sstevel@tonic-gate major_t major = ddi_name_to_major(drvname);
82017c478bd9Sstevel@tonic-gate
82027c478bd9Sstevel@tonic-gate if (major == -1)
82037c478bd9Sstevel@tonic-gate return (NULL);
82047c478bd9Sstevel@tonic-gate
82057c478bd9Sstevel@tonic-gate /* Make sure we create the VHCI node only once */
82067c478bd9Sstevel@tonic-gate dnp = &devnamesp[major];
82077c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock);
82087c478bd9Sstevel@tonic-gate if (dnp->dn_head) {
82097c478bd9Sstevel@tonic-gate dip = dnp->dn_head;
82107c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
82117c478bd9Sstevel@tonic-gate return (dip);
82127c478bd9Sstevel@tonic-gate }
82137c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
82147c478bd9Sstevel@tonic-gate
82157c478bd9Sstevel@tonic-gate /* Allocate the VHCI node */
82167c478bd9Sstevel@tonic-gate ndi_devi_alloc_sleep(top_devinfo, drvname, DEVI_SID_NODEID, &dip);
82177c478bd9Sstevel@tonic-gate ndi_hold_devi(dip);
82187c478bd9Sstevel@tonic-gate
82197c478bd9Sstevel@tonic-gate /* Mark the node as VHCI */
82207c478bd9Sstevel@tonic-gate DEVI(dip)->devi_node_attributes |= DDI_VHCI_NODE;
82217c478bd9Sstevel@tonic-gate
82227c478bd9Sstevel@tonic-gate i_ddi_add_devimap(dip);
82237c478bd9Sstevel@tonic-gate i_bind_vhci_node(dip);
82247c478bd9Sstevel@tonic-gate if (i_init_vhci_node(dip) == -1) {
82257c478bd9Sstevel@tonic-gate ndi_rele_devi(dip);
82267c478bd9Sstevel@tonic-gate (void) ndi_devi_free(dip);
82277c478bd9Sstevel@tonic-gate return (NULL);
82287c478bd9Sstevel@tonic-gate }
82297c478bd9Sstevel@tonic-gate
823016747f41Scth mutex_enter(&(DEVI(dip)->devi_lock));
82317c478bd9Sstevel@tonic-gate DEVI_SET_ATTACHING(dip);
823216747f41Scth mutex_exit(&(DEVI(dip)->devi_lock));
823316747f41Scth
82347c478bd9Sstevel@tonic-gate if (devi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
82357c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, "Could not attach %s driver", drvname);
82367c478bd9Sstevel@tonic-gate e_ddi_free_instance(dip, vhci_node_addr);
82377c478bd9Sstevel@tonic-gate ndi_rele_devi(dip);
82387c478bd9Sstevel@tonic-gate (void) ndi_devi_free(dip);
82397c478bd9Sstevel@tonic-gate return (NULL);
82407c478bd9Sstevel@tonic-gate }
824116747f41Scth mutex_enter(&(DEVI(dip)->devi_lock));
82427c478bd9Sstevel@tonic-gate DEVI_CLR_ATTACHING(dip);
824316747f41Scth mutex_exit(&(DEVI(dip)->devi_lock));
82447c478bd9Sstevel@tonic-gate
82459d3d2ed0Shiremath mutex_enter(&global_vhci_lock);
82467c478bd9Sstevel@tonic-gate i_link_vhci_node(dip);
82479d3d2ed0Shiremath mutex_exit(&global_vhci_lock);
82487c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_READY);
82497c478bd9Sstevel@tonic-gate
82507c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock);
82517c478bd9Sstevel@tonic-gate dnp->dn_flags |= DN_DRIVER_HELD;
82527c478bd9Sstevel@tonic-gate dnp->dn_head = dip;
82537c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
82547c478bd9Sstevel@tonic-gate
82557c478bd9Sstevel@tonic-gate i_ndi_devi_report_status_change(dip, NULL);
82567c478bd9Sstevel@tonic-gate
82577c478bd9Sstevel@tonic-gate return (dip);
82587c478bd9Sstevel@tonic-gate }
82597c478bd9Sstevel@tonic-gate
82607c478bd9Sstevel@tonic-gate /*
82614c06356bSdh142964 * Maintain DEVI_DEVICE_REMOVED hotplug devi_state for remove/reinsert hotplug
82624c06356bSdh142964 * of open devices. Currently, because of tight coupling between the devfs file
82634c06356bSdh142964 * system and the Solaris device tree, a driver can't always make the device
82644c06356bSdh142964 * tree state (esp devi_node_state) match device hardware hotplug state. Until
82654c06356bSdh142964 * resolved, to overcome this deficiency we use the following interfaces that
82664c06356bSdh142964 * maintain the DEVI_DEVICE_REMOVED devi_state status bit. These interface
82674c06356bSdh142964 * report current state, and drive operation (like events and cache
82684c06356bSdh142964 * invalidation) when a driver changes remove/insert state of an open device.
82694c06356bSdh142964 *
82704c06356bSdh142964 * The ndi_devi_device_isremoved() returns 1 if the device is currently removed.
82714c06356bSdh142964 *
82724c06356bSdh142964 * The ndi_devi_device_remove() interface declares the device as removed, and
82734c06356bSdh142964 * returns 1 if there was a state change associated with this declaration.
82744c06356bSdh142964 *
82754c06356bSdh142964 * The ndi_devi_device_insert() declares the device as inserted, and returns 1
82764c06356bSdh142964 * if there was a state change associated with this declaration.
82774c06356bSdh142964 */
82784c06356bSdh142964 int
ndi_devi_device_isremoved(dev_info_t * dip)82794c06356bSdh142964 ndi_devi_device_isremoved(dev_info_t *dip)
82804c06356bSdh142964 {
82814c06356bSdh142964 return (DEVI_IS_DEVICE_REMOVED(dip));
82824c06356bSdh142964 }
82834c06356bSdh142964
82844c06356bSdh142964 int
ndi_devi_device_remove(dev_info_t * dip)82854c06356bSdh142964 ndi_devi_device_remove(dev_info_t *dip)
82864c06356bSdh142964 {
82874c06356bSdh142964 ASSERT(dip && ddi_get_parent(dip) &&
82884c06356bSdh142964 DEVI_BUSY_OWNED(ddi_get_parent(dip)));
82894c06356bSdh142964
82904c06356bSdh142964 /* Return if already marked removed. */
82914c06356bSdh142964 if (ndi_devi_device_isremoved(dip))
82924c06356bSdh142964 return (0);
82934c06356bSdh142964
82944c06356bSdh142964 /* Mark the device as having been physically removed. */
82954c06356bSdh142964 mutex_enter(&(DEVI(dip)->devi_lock));
82964c06356bSdh142964 ndi_devi_set_hidden(dip); /* invisible: lookup/snapshot */
82974c06356bSdh142964 DEVI_SET_DEVICE_REMOVED(dip);
82984c06356bSdh142964 DEVI_SET_EVREMOVE(dip); /* this clears EVADD too */
82994c06356bSdh142964 mutex_exit(&(DEVI(dip)->devi_lock));
83004c06356bSdh142964
83014c06356bSdh142964 /* report remove (as 'removed') */
83024c06356bSdh142964 i_ndi_devi_report_status_change(dip, NULL);
83034c06356bSdh142964
83044c06356bSdh142964 /*
83054c06356bSdh142964 * Invalidate the cache to ensure accurate
83064c06356bSdh142964 * (di_state() & DI_DEVICE_REMOVED).
83074c06356bSdh142964 */
83084c06356bSdh142964 i_ddi_di_cache_invalidate();
83094c06356bSdh142964
83104c06356bSdh142964 /*
8311328d222bSChris Horne * Generate sysevent for those interested in removal (either
8312328d222bSChris Horne * directly via private EC_DEVFS or indirectly via devfsadmd
8313328d222bSChris Horne * generated EC_DEV). This will generate LDI DEVICE_REMOVE
8314328d222bSChris Horne * event too.
83154c06356bSdh142964 */
83164c06356bSdh142964 i_ddi_log_devfs_device_remove(dip);
83174c06356bSdh142964
83184c06356bSdh142964 return (1); /* DEVICE_REMOVED state changed */
83194c06356bSdh142964 }
83204c06356bSdh142964
83214c06356bSdh142964 int
ndi_devi_device_insert(dev_info_t * dip)83224c06356bSdh142964 ndi_devi_device_insert(dev_info_t *dip)
83234c06356bSdh142964 {
83244c06356bSdh142964 ASSERT(dip && ddi_get_parent(dip) &&
83254c06356bSdh142964 DEVI_BUSY_OWNED(ddi_get_parent(dip)));
83264c06356bSdh142964
83274c06356bSdh142964 /* Return if not marked removed. */
83284c06356bSdh142964 if (!ndi_devi_device_isremoved(dip))
83294c06356bSdh142964 return (0);
83304c06356bSdh142964
83314c06356bSdh142964 /* Mark the device as having been physically reinserted. */
83324c06356bSdh142964 mutex_enter(&(DEVI(dip)->devi_lock));
83334c06356bSdh142964 ndi_devi_clr_hidden(dip); /* visible: lookup/snapshot */
83344c06356bSdh142964 DEVI_SET_DEVICE_REINSERTED(dip);
83354c06356bSdh142964 DEVI_SET_EVADD(dip); /* this clears EVREMOVE too */
83364c06356bSdh142964 mutex_exit(&(DEVI(dip)->devi_lock));
83374c06356bSdh142964
83384c06356bSdh142964 /* report insert (as 'online') */
83394c06356bSdh142964 i_ndi_devi_report_status_change(dip, NULL);
83404c06356bSdh142964
83414c06356bSdh142964 /*
83424c06356bSdh142964 * Invalidate the cache to ensure accurate
83434c06356bSdh142964 * (di_state() & DI_DEVICE_REMOVED).
83444c06356bSdh142964 */
83454c06356bSdh142964 i_ddi_di_cache_invalidate();
83464c06356bSdh142964
83474c06356bSdh142964 /*
83484c06356bSdh142964 * Generate sysevent for those interested in removal (either directly
83494c06356bSdh142964 * via EC_DEVFS or indirectly via devfsadmd generated EC_DEV).
83504c06356bSdh142964 */
83514c06356bSdh142964 i_ddi_log_devfs_device_insert(dip);
83524c06356bSdh142964
83534c06356bSdh142964 return (1); /* DEVICE_REMOVED state changed */
83544c06356bSdh142964 }
83554c06356bSdh142964
83564c06356bSdh142964 /*
83577c478bd9Sstevel@tonic-gate * ibt_hw_is_present() returns 0 when there is no IB hardware actively
83587c478bd9Sstevel@tonic-gate * running. This is primarily useful for modules like rpcmod which
83597c478bd9Sstevel@tonic-gate * needs a quick check to decide whether or not it should try to use
83607c478bd9Sstevel@tonic-gate * InfiniBand
83617c478bd9Sstevel@tonic-gate */
83627c478bd9Sstevel@tonic-gate int ib_hw_status = 0;
83637c478bd9Sstevel@tonic-gate int
ibt_hw_is_present()83647c478bd9Sstevel@tonic-gate ibt_hw_is_present()
83657c478bd9Sstevel@tonic-gate {
83667c478bd9Sstevel@tonic-gate return (ib_hw_status);
83677c478bd9Sstevel@tonic-gate }
836825e8c5aaSvikram
836925e8c5aaSvikram /*
837025e8c5aaSvikram * ASSERT that constraint flag is not set and then set the "retire attempt"
837125e8c5aaSvikram * flag.
837225e8c5aaSvikram */
837325e8c5aaSvikram int
e_ddi_mark_retiring(dev_info_t * dip,void * arg)837425e8c5aaSvikram e_ddi_mark_retiring(dev_info_t *dip, void *arg)
837525e8c5aaSvikram {
837625e8c5aaSvikram char **cons_array = (char **)arg;
837725e8c5aaSvikram char *path;
837825e8c5aaSvikram int constraint;
837925e8c5aaSvikram int i;
838025e8c5aaSvikram
838125e8c5aaSvikram constraint = 0;
838225e8c5aaSvikram if (cons_array) {
838325e8c5aaSvikram path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
838425e8c5aaSvikram (void) ddi_pathname(dip, path);
838525e8c5aaSvikram for (i = 0; cons_array[i] != NULL; i++) {
838625e8c5aaSvikram if (strcmp(path, cons_array[i]) == 0) {
838725e8c5aaSvikram constraint = 1;
838825e8c5aaSvikram break;
838925e8c5aaSvikram }
839025e8c5aaSvikram }
839125e8c5aaSvikram kmem_free(path, MAXPATHLEN);
839225e8c5aaSvikram }
839325e8c5aaSvikram
839425e8c5aaSvikram mutex_enter(&DEVI(dip)->devi_lock);
839525e8c5aaSvikram ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
839625e8c5aaSvikram DEVI(dip)->devi_flags |= DEVI_RETIRING;
839725e8c5aaSvikram if (constraint)
839825e8c5aaSvikram DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
839925e8c5aaSvikram mutex_exit(&DEVI(dip)->devi_lock);
840025e8c5aaSvikram
840125e8c5aaSvikram RIO_VERBOSE((CE_NOTE, "marked dip as undergoing retire process dip=%p",
840225e8c5aaSvikram (void *)dip));
840325e8c5aaSvikram
840425e8c5aaSvikram if (constraint)
840525e8c5aaSvikram RIO_DEBUG((CE_NOTE, "marked dip as constrained, dip=%p",
840625e8c5aaSvikram (void *)dip));
840725e8c5aaSvikram
840825e8c5aaSvikram if (MDI_PHCI(dip))
840925e8c5aaSvikram mdi_phci_mark_retiring(dip, cons_array);
841025e8c5aaSvikram
841125e8c5aaSvikram return (DDI_WALK_CONTINUE);
841225e8c5aaSvikram }
841325e8c5aaSvikram
841425e8c5aaSvikram static void
free_array(char ** cons_array)841525e8c5aaSvikram free_array(char **cons_array)
841625e8c5aaSvikram {
841725e8c5aaSvikram int i;
841825e8c5aaSvikram
841925e8c5aaSvikram if (cons_array == NULL)
842025e8c5aaSvikram return;
842125e8c5aaSvikram
842225e8c5aaSvikram for (i = 0; cons_array[i] != NULL; i++) {
842325e8c5aaSvikram kmem_free(cons_array[i], strlen(cons_array[i]) + 1);
842425e8c5aaSvikram }
842525e8c5aaSvikram kmem_free(cons_array, (i+1) * sizeof (char *));
842625e8c5aaSvikram }
842725e8c5aaSvikram
842825e8c5aaSvikram /*
842925e8c5aaSvikram * Walk *every* node in subtree and check if it blocks, allows or has no
843025e8c5aaSvikram * comment on a proposed retire.
843125e8c5aaSvikram */
843225e8c5aaSvikram int
e_ddi_retire_notify(dev_info_t * dip,void * arg)843325e8c5aaSvikram e_ddi_retire_notify(dev_info_t *dip, void *arg)
843425e8c5aaSvikram {
843525e8c5aaSvikram int *constraint = (int *)arg;
843625e8c5aaSvikram
843725e8c5aaSvikram RIO_DEBUG((CE_NOTE, "retire notify: dip = %p", (void *)dip));
843825e8c5aaSvikram
843925e8c5aaSvikram (void) e_ddi_offline_notify(dip);
844025e8c5aaSvikram
844125e8c5aaSvikram mutex_enter(&(DEVI(dip)->devi_lock));
844225e8c5aaSvikram if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
844325e8c5aaSvikram RIO_DEBUG((CE_WARN, "retire notify: dip in retire "
844425e8c5aaSvikram "subtree is not marked: dip = %p", (void *)dip));
844525e8c5aaSvikram *constraint = 0;
844625e8c5aaSvikram } else if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
844725e8c5aaSvikram ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
844825e8c5aaSvikram RIO_DEBUG((CE_NOTE, "retire notify: BLOCKED: dip = %p",
844925e8c5aaSvikram (void *)dip));
845025e8c5aaSvikram *constraint = 0;
845125e8c5aaSvikram } else if (!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT)) {
845225e8c5aaSvikram RIO_DEBUG((CE_NOTE, "retire notify: NO CONSTRAINT: "
845325e8c5aaSvikram "dip = %p", (void *)dip));
845425e8c5aaSvikram *constraint = 0;
845525e8c5aaSvikram } else {
845625e8c5aaSvikram RIO_DEBUG((CE_NOTE, "retire notify: CONSTRAINT set: "
845725e8c5aaSvikram "dip = %p", (void *)dip));
845825e8c5aaSvikram }
845925e8c5aaSvikram mutex_exit(&DEVI(dip)->devi_lock);
846025e8c5aaSvikram
846125e8c5aaSvikram if (MDI_PHCI(dip))
846225e8c5aaSvikram mdi_phci_retire_notify(dip, constraint);
846325e8c5aaSvikram
846425e8c5aaSvikram return (DDI_WALK_CONTINUE);
846525e8c5aaSvikram }
846625e8c5aaSvikram
846725e8c5aaSvikram int
e_ddi_retire_finalize(dev_info_t * dip,void * arg)846825e8c5aaSvikram e_ddi_retire_finalize(dev_info_t *dip, void *arg)
846925e8c5aaSvikram {
847025e8c5aaSvikram int constraint = *(int *)arg;
847125e8c5aaSvikram int finalize;
847225e8c5aaSvikram int phci_only;
847325e8c5aaSvikram
847425e8c5aaSvikram mutex_enter(&DEVI(dip)->devi_lock);
847525e8c5aaSvikram if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
847625e8c5aaSvikram RIO_DEBUG((CE_WARN,
847725e8c5aaSvikram "retire: unmarked dip(%p) in retire subtree",
847825e8c5aaSvikram (void *)dip));
847925e8c5aaSvikram ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRED));
848025e8c5aaSvikram ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
848125e8c5aaSvikram ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
848225e8c5aaSvikram mutex_exit(&DEVI(dip)->devi_lock);
848325e8c5aaSvikram return (DDI_WALK_CONTINUE);
848425e8c5aaSvikram }
848525e8c5aaSvikram
848625e8c5aaSvikram /*
848725e8c5aaSvikram * retire the device if constraints have been applied
848825e8c5aaSvikram * or if the device is not in use
848925e8c5aaSvikram */
849025e8c5aaSvikram finalize = 0;
849125e8c5aaSvikram if (constraint) {
8492bf002425SStephen Hanson ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
8493bf002425SStephen Hanson
849425e8c5aaSvikram ASSERT(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT);
849525e8c5aaSvikram ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
849625e8c5aaSvikram DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
849725e8c5aaSvikram DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
849825e8c5aaSvikram DEVI(dip)->devi_flags |= DEVI_RETIRED;
849925e8c5aaSvikram mutex_exit(&DEVI(dip)->devi_lock);
850025e8c5aaSvikram (void) spec_fence_snode(dip, NULL);
850125e8c5aaSvikram RIO_DEBUG((CE_NOTE, "Fenced off: dip = %p", (void *)dip));
850225e8c5aaSvikram e_ddi_offline_finalize(dip, DDI_SUCCESS);
850325e8c5aaSvikram } else {
850425e8c5aaSvikram if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
850525e8c5aaSvikram ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
850625e8c5aaSvikram DEVI(dip)->devi_flags &= ~DEVI_R_BLOCKED;
850725e8c5aaSvikram DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
850825e8c5aaSvikram /* we have already finalized during notify */
850925e8c5aaSvikram } else if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
851025e8c5aaSvikram DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
851125e8c5aaSvikram DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
851225e8c5aaSvikram finalize = 1;
851325e8c5aaSvikram } else {
851425e8c5aaSvikram DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
851525e8c5aaSvikram /*
851625e8c5aaSvikram * even if no contracts, need to call finalize
851725e8c5aaSvikram * to clear the contract barrier on the dip
851825e8c5aaSvikram */
851925e8c5aaSvikram finalize = 1;
852025e8c5aaSvikram }
852125e8c5aaSvikram mutex_exit(&DEVI(dip)->devi_lock);
852225e8c5aaSvikram RIO_DEBUG((CE_NOTE, "finalize: NOT retired: dip = %p",
852325e8c5aaSvikram (void *)dip));
852425e8c5aaSvikram if (finalize)
852525e8c5aaSvikram e_ddi_offline_finalize(dip, DDI_FAILURE);
852625e8c5aaSvikram }
852725e8c5aaSvikram
852825e8c5aaSvikram /*
852925e8c5aaSvikram * phci_only variable indicates no client checking, just
853025e8c5aaSvikram * offline the PHCI. We set that to 0 to enable client
853125e8c5aaSvikram * checking
853225e8c5aaSvikram */
853325e8c5aaSvikram phci_only = 0;
853425e8c5aaSvikram if (MDI_PHCI(dip))
8535bf002425SStephen Hanson mdi_phci_retire_finalize(dip, phci_only, arg);
853625e8c5aaSvikram
853725e8c5aaSvikram return (DDI_WALK_CONTINUE);
853825e8c5aaSvikram }
853925e8c5aaSvikram
854025e8c5aaSvikram /*
854125e8c5aaSvikram * Returns
854225e8c5aaSvikram * DDI_SUCCESS if constraints allow retire
854325e8c5aaSvikram * DDI_FAILURE if constraints don't allow retire.
854425e8c5aaSvikram * cons_array is a NULL terminated array of node paths for
854525e8c5aaSvikram * which constraints have already been applied.
854625e8c5aaSvikram */
854725e8c5aaSvikram int
e_ddi_retire_device(char * path,char ** cons_array)854825e8c5aaSvikram e_ddi_retire_device(char *path, char **cons_array)
854925e8c5aaSvikram {
855025e8c5aaSvikram dev_info_t *dip;
855125e8c5aaSvikram dev_info_t *pdip;
855225e8c5aaSvikram int constraint;
855325e8c5aaSvikram char *devnm;
855425e8c5aaSvikram
855525e8c5aaSvikram /*
855625e8c5aaSvikram * First, lookup the device
855725e8c5aaSvikram */
855825e8c5aaSvikram dip = e_ddi_hold_devi_by_path(path, 0);
855925e8c5aaSvikram if (dip == NULL) {
856025e8c5aaSvikram /*
856125e8c5aaSvikram * device does not exist. This device cannot be
856225e8c5aaSvikram * a critical device since it is not in use. Thus
856325e8c5aaSvikram * this device is always retireable. Return DDI_SUCCESS
856425e8c5aaSvikram * to indicate this. If this device is ever
856525e8c5aaSvikram * instantiated, I/O framework will consult the
856625e8c5aaSvikram * the persistent retire store, mark it as
856725e8c5aaSvikram * retired and fence it off.
856825e8c5aaSvikram */
856925e8c5aaSvikram RIO_DEBUG((CE_NOTE, "Retire device: device doesn't exist."
857025e8c5aaSvikram " NOP. Just returning SUCCESS. path=%s", path));
857125e8c5aaSvikram free_array(cons_array);
857225e8c5aaSvikram return (DDI_SUCCESS);
857325e8c5aaSvikram }
857425e8c5aaSvikram
857525e8c5aaSvikram RIO_DEBUG((CE_NOTE, "Retire device: found dip = %p.", (void *)dip));
857625e8c5aaSvikram
857725e8c5aaSvikram pdip = ddi_get_parent(dip);
857825e8c5aaSvikram ndi_hold_devi(pdip);
857925e8c5aaSvikram
858025e8c5aaSvikram /*
858125e8c5aaSvikram * Run devfs_clean() in case dip has no constraints and is
858225e8c5aaSvikram * not in use, so is retireable but there are dv_nodes holding
858325e8c5aaSvikram * ref-count on the dip. Note that devfs_clean() always returns
858425e8c5aaSvikram * success.
858525e8c5aaSvikram */
858625e8c5aaSvikram devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
858725e8c5aaSvikram (void) ddi_deviname(dip, devnm);
858825e8c5aaSvikram (void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
858925e8c5aaSvikram kmem_free(devnm, MAXNAMELEN + 1);
859025e8c5aaSvikram
85913fe80ca4SDan Cross ndi_devi_enter(pdip);
859225e8c5aaSvikram
859325e8c5aaSvikram /* release hold from e_ddi_hold_devi_by_path */
859425e8c5aaSvikram ndi_rele_devi(dip);
859525e8c5aaSvikram
859625e8c5aaSvikram /*
859725e8c5aaSvikram * If it cannot make a determination, is_leaf_node() assumes
859825e8c5aaSvikram * dip is a nexus.
859925e8c5aaSvikram */
860025e8c5aaSvikram (void) e_ddi_mark_retiring(dip, cons_array);
860125e8c5aaSvikram if (!is_leaf_node(dip)) {
86023fe80ca4SDan Cross ndi_devi_enter(dip);
860325e8c5aaSvikram ddi_walk_devs(ddi_get_child(dip), e_ddi_mark_retiring,
860425e8c5aaSvikram cons_array);
86053fe80ca4SDan Cross ndi_devi_exit(dip);
860625e8c5aaSvikram }
860725e8c5aaSvikram free_array(cons_array);
860825e8c5aaSvikram
860925e8c5aaSvikram /*
861025e8c5aaSvikram * apply constraints
861125e8c5aaSvikram */
861225e8c5aaSvikram RIO_DEBUG((CE_NOTE, "retire: subtree retire notify: path = %s", path));
861325e8c5aaSvikram
861425e8c5aaSvikram constraint = 1; /* assume constraints allow retire */
861525e8c5aaSvikram (void) e_ddi_retire_notify(dip, &constraint);
861625e8c5aaSvikram if (!is_leaf_node(dip)) {
86173fe80ca4SDan Cross ndi_devi_enter(dip);
861825e8c5aaSvikram ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_notify,
861925e8c5aaSvikram &constraint);
86203fe80ca4SDan Cross ndi_devi_exit(dip);
862125e8c5aaSvikram }
862225e8c5aaSvikram
862325e8c5aaSvikram /*
862425e8c5aaSvikram * Now finalize the retire
862525e8c5aaSvikram */
862625e8c5aaSvikram (void) e_ddi_retire_finalize(dip, &constraint);
862725e8c5aaSvikram if (!is_leaf_node(dip)) {
86283fe80ca4SDan Cross ndi_devi_enter(dip);
862925e8c5aaSvikram ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_finalize,
863025e8c5aaSvikram &constraint);
86313fe80ca4SDan Cross ndi_devi_exit(dip);
863225e8c5aaSvikram }
863325e8c5aaSvikram
863425e8c5aaSvikram if (!constraint) {
863525e8c5aaSvikram RIO_DEBUG((CE_WARN, "retire failed: path = %s", path));
863625e8c5aaSvikram } else {
863725e8c5aaSvikram RIO_DEBUG((CE_NOTE, "retire succeeded: path = %s", path));
863825e8c5aaSvikram }
863925e8c5aaSvikram
86403fe80ca4SDan Cross ndi_devi_exit(pdip);
864125e8c5aaSvikram ndi_rele_devi(pdip);
864225e8c5aaSvikram return (constraint ? DDI_SUCCESS : DDI_FAILURE);
864325e8c5aaSvikram }
864425e8c5aaSvikram
864525e8c5aaSvikram static int
unmark_and_unfence(dev_info_t * dip,void * arg)864625e8c5aaSvikram unmark_and_unfence(dev_info_t *dip, void *arg)
864725e8c5aaSvikram {
864825e8c5aaSvikram char *path = (char *)arg;
864925e8c5aaSvikram
865025e8c5aaSvikram ASSERT(path);
865125e8c5aaSvikram
865225e8c5aaSvikram (void) ddi_pathname(dip, path);
865325e8c5aaSvikram
865425e8c5aaSvikram mutex_enter(&DEVI(dip)->devi_lock);
865525e8c5aaSvikram DEVI(dip)->devi_flags &= ~DEVI_RETIRED;
865625e8c5aaSvikram DEVI_SET_DEVICE_ONLINE(dip);
865725e8c5aaSvikram mutex_exit(&DEVI(dip)->devi_lock);
865825e8c5aaSvikram
865925e8c5aaSvikram RIO_VERBOSE((CE_NOTE, "Cleared RETIRED flag: dip=%p, path=%s",
866025e8c5aaSvikram (void *)dip, path));
866125e8c5aaSvikram
866225e8c5aaSvikram (void) spec_unfence_snode(dip);
866325e8c5aaSvikram RIO_DEBUG((CE_NOTE, "Unfenced device: %s", path));
866425e8c5aaSvikram
866525e8c5aaSvikram if (MDI_PHCI(dip))
866625e8c5aaSvikram mdi_phci_unretire(dip);
866725e8c5aaSvikram
866825e8c5aaSvikram return (DDI_WALK_CONTINUE);
866925e8c5aaSvikram }
867025e8c5aaSvikram
867125e8c5aaSvikram struct find_dip {
867225e8c5aaSvikram char *fd_buf;
867325e8c5aaSvikram char *fd_path;
867425e8c5aaSvikram dev_info_t *fd_dip;
867525e8c5aaSvikram };
867625e8c5aaSvikram
867725e8c5aaSvikram static int
find_dip_fcn(dev_info_t * dip,void * arg)867825e8c5aaSvikram find_dip_fcn(dev_info_t *dip, void *arg)
867925e8c5aaSvikram {
868025e8c5aaSvikram struct find_dip *findp = (struct find_dip *)arg;
868125e8c5aaSvikram
868225e8c5aaSvikram (void) ddi_pathname(dip, findp->fd_buf);
868325e8c5aaSvikram
868425e8c5aaSvikram if (strcmp(findp->fd_path, findp->fd_buf) != 0)
868525e8c5aaSvikram return (DDI_WALK_CONTINUE);
868625e8c5aaSvikram
868725e8c5aaSvikram ndi_hold_devi(dip);
868825e8c5aaSvikram findp->fd_dip = dip;
868925e8c5aaSvikram
869025e8c5aaSvikram return (DDI_WALK_TERMINATE);
869125e8c5aaSvikram }
869225e8c5aaSvikram
869325e8c5aaSvikram int
e_ddi_unretire_device(char * path)869425e8c5aaSvikram e_ddi_unretire_device(char *path)
869525e8c5aaSvikram {
869625e8c5aaSvikram char *path2;
869725e8c5aaSvikram dev_info_t *pdip;
869825e8c5aaSvikram dev_info_t *dip;
869925e8c5aaSvikram struct find_dip find_dip;
870025e8c5aaSvikram
870125e8c5aaSvikram ASSERT(path);
870225e8c5aaSvikram ASSERT(*path == '/');
870325e8c5aaSvikram
870425e8c5aaSvikram if (strcmp(path, "/") == 0) {
870525e8c5aaSvikram cmn_err(CE_WARN, "Root node cannot be retired. Skipping "
870625e8c5aaSvikram "device unretire: %s", path);
870725e8c5aaSvikram return (0);
870825e8c5aaSvikram }
870925e8c5aaSvikram
871025e8c5aaSvikram /*
871125e8c5aaSvikram * We can't lookup the dip (corresponding to path) via
871225e8c5aaSvikram * e_ddi_hold_devi_by_path() because the dip may be offline
871325e8c5aaSvikram * and may not attach. Use ddi_walk_devs() instead;
871425e8c5aaSvikram */
871525e8c5aaSvikram find_dip.fd_buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
871625e8c5aaSvikram find_dip.fd_path = path;
871725e8c5aaSvikram find_dip.fd_dip = NULL;
871825e8c5aaSvikram
871925e8c5aaSvikram pdip = ddi_root_node();
872025e8c5aaSvikram
87213fe80ca4SDan Cross ndi_devi_enter(pdip);
872225e8c5aaSvikram ddi_walk_devs(ddi_get_child(pdip), find_dip_fcn, &find_dip);
87233fe80ca4SDan Cross ndi_devi_exit(pdip);
872425e8c5aaSvikram
872525e8c5aaSvikram kmem_free(find_dip.fd_buf, MAXPATHLEN);
872625e8c5aaSvikram
872725e8c5aaSvikram if (find_dip.fd_dip == NULL) {
872825e8c5aaSvikram cmn_err(CE_WARN, "Device not found in device tree. Skipping "
872925e8c5aaSvikram "device unretire: %s", path);
873025e8c5aaSvikram return (0);
873125e8c5aaSvikram }
873225e8c5aaSvikram
873325e8c5aaSvikram dip = find_dip.fd_dip;
873425e8c5aaSvikram
873525e8c5aaSvikram pdip = ddi_get_parent(dip);
873625e8c5aaSvikram
873725e8c5aaSvikram ndi_hold_devi(pdip);
873825e8c5aaSvikram
87393fe80ca4SDan Cross ndi_devi_enter(pdip);
874025e8c5aaSvikram
874125e8c5aaSvikram path2 = kmem_alloc(MAXPATHLEN, KM_SLEEP);
874225e8c5aaSvikram
874325e8c5aaSvikram (void) unmark_and_unfence(dip, path2);
874425e8c5aaSvikram if (!is_leaf_node(dip)) {
87453fe80ca4SDan Cross ndi_devi_enter(dip);
874625e8c5aaSvikram ddi_walk_devs(ddi_get_child(dip), unmark_and_unfence, path2);
87473fe80ca4SDan Cross ndi_devi_exit(dip);
874825e8c5aaSvikram }
874925e8c5aaSvikram
875025e8c5aaSvikram kmem_free(path2, MAXPATHLEN);
875125e8c5aaSvikram
875225e8c5aaSvikram /* release hold from find_dip_fcn() */
875325e8c5aaSvikram ndi_rele_devi(dip);
875425e8c5aaSvikram
87553fe80ca4SDan Cross ndi_devi_exit(pdip);
875625e8c5aaSvikram
875725e8c5aaSvikram ndi_rele_devi(pdip);
875825e8c5aaSvikram
875925e8c5aaSvikram return (0);
876025e8c5aaSvikram }
876125e8c5aaSvikram
876225e8c5aaSvikram /*
876325e8c5aaSvikram * Called before attach on a dip that has been retired.
876425e8c5aaSvikram */
876525e8c5aaSvikram static int
mark_and_fence(dev_info_t * dip,void * arg)876625e8c5aaSvikram mark_and_fence(dev_info_t *dip, void *arg)
876725e8c5aaSvikram {
876825e8c5aaSvikram char *fencepath = (char *)arg;
876925e8c5aaSvikram
877025e8c5aaSvikram /*
877125e8c5aaSvikram * We have already decided to retire this device. The various
877225e8c5aaSvikram * constraint checking should not be set.
877325e8c5aaSvikram * NOTE that the retire flag may already be set due to
877425e8c5aaSvikram * fenced -> detach -> fenced transitions.
877525e8c5aaSvikram */
877625e8c5aaSvikram mutex_enter(&DEVI(dip)->devi_lock);
877725e8c5aaSvikram ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
877825e8c5aaSvikram ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
877925e8c5aaSvikram ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRING));
878025e8c5aaSvikram DEVI(dip)->devi_flags |= DEVI_RETIRED;
878125e8c5aaSvikram mutex_exit(&DEVI(dip)->devi_lock);
878225e8c5aaSvikram RIO_VERBOSE((CE_NOTE, "marked as RETIRED dip=%p", (void *)dip));
878325e8c5aaSvikram
878425e8c5aaSvikram if (fencepath) {
878525e8c5aaSvikram (void) spec_fence_snode(dip, NULL);
878625e8c5aaSvikram RIO_DEBUG((CE_NOTE, "Fenced: %s",
878725e8c5aaSvikram ddi_pathname(dip, fencepath)));
878825e8c5aaSvikram }
878925e8c5aaSvikram
879025e8c5aaSvikram return (DDI_WALK_CONTINUE);
879125e8c5aaSvikram }
879225e8c5aaSvikram
879325e8c5aaSvikram /*
879425e8c5aaSvikram * Checks the retire database and:
879525e8c5aaSvikram *
879625e8c5aaSvikram * - if device is present in the retire database, marks the device retired
879725e8c5aaSvikram * and fences it off.
879825e8c5aaSvikram * - if device is not in retire database, allows the device to attach normally
879925e8c5aaSvikram *
880025e8c5aaSvikram * To be called only by framework attach code on first attach attempt.
880125e8c5aaSvikram *
880225e8c5aaSvikram */
8803e58a33b6SStephen Hanson static int
i_ddi_check_retire(dev_info_t * dip)880425e8c5aaSvikram i_ddi_check_retire(dev_info_t *dip)
880525e8c5aaSvikram {
880625e8c5aaSvikram char *path;
880725e8c5aaSvikram dev_info_t *pdip;
880825e8c5aaSvikram int phci_only;
8809bf002425SStephen Hanson int constraint;
881025e8c5aaSvikram
881125e8c5aaSvikram pdip = ddi_get_parent(dip);
881225e8c5aaSvikram
881325e8c5aaSvikram /*
881425e8c5aaSvikram * Root dip is treated special and doesn't take this code path.
881525e8c5aaSvikram * Also root can never be retired.
881625e8c5aaSvikram */
881725e8c5aaSvikram ASSERT(pdip);
881825e8c5aaSvikram ASSERT(DEVI_BUSY_OWNED(pdip));
881925e8c5aaSvikram ASSERT(i_ddi_node_state(dip) < DS_ATTACHED);
882025e8c5aaSvikram
882125e8c5aaSvikram path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
882225e8c5aaSvikram
882325e8c5aaSvikram (void) ddi_pathname(dip, path);
882425e8c5aaSvikram
882525e8c5aaSvikram RIO_VERBOSE((CE_NOTE, "Checking if dip should attach: dip=%p, path=%s",
882625e8c5aaSvikram (void *)dip, path));
882725e8c5aaSvikram
882825e8c5aaSvikram /*
882925e8c5aaSvikram * Check if this device is in the "retired" store i.e. should
883025e8c5aaSvikram * be retired. If not, we have nothing to do.
883125e8c5aaSvikram */
883225e8c5aaSvikram if (e_ddi_device_retired(path) == 0) {
883325e8c5aaSvikram RIO_VERBOSE((CE_NOTE, "device is NOT retired: path=%s", path));
8834e58a33b6SStephen Hanson if (DEVI(dip)->devi_flags & DEVI_RETIRED)
8835e58a33b6SStephen Hanson (void) e_ddi_unretire_device(path);
883625e8c5aaSvikram kmem_free(path, MAXPATHLEN);
8837e58a33b6SStephen Hanson return (0);
883825e8c5aaSvikram }
883925e8c5aaSvikram
884025e8c5aaSvikram RIO_DEBUG((CE_NOTE, "attach: device is retired: path=%s", path));
884125e8c5aaSvikram
884225e8c5aaSvikram /*
884325e8c5aaSvikram * Mark dips and fence off snodes (if any)
884425e8c5aaSvikram */
884525e8c5aaSvikram RIO_DEBUG((CE_NOTE, "attach: Mark and fence subtree: path=%s", path));
884625e8c5aaSvikram (void) mark_and_fence(dip, path);
884725e8c5aaSvikram if (!is_leaf_node(dip)) {
88483fe80ca4SDan Cross ndi_devi_enter(dip);
884925e8c5aaSvikram ddi_walk_devs(ddi_get_child(dip), mark_and_fence, path);
88503fe80ca4SDan Cross ndi_devi_exit(dip);
885125e8c5aaSvikram }
885225e8c5aaSvikram
885325e8c5aaSvikram kmem_free(path, MAXPATHLEN);
885425e8c5aaSvikram
885525e8c5aaSvikram /*
885625e8c5aaSvikram * We don't want to check the client. We just want to
885725e8c5aaSvikram * offline the PHCI
885825e8c5aaSvikram */
885925e8c5aaSvikram phci_only = 1;
8860bf002425SStephen Hanson constraint = 1;
886125e8c5aaSvikram if (MDI_PHCI(dip))
8862bf002425SStephen Hanson mdi_phci_retire_finalize(dip, phci_only, &constraint);
8863e58a33b6SStephen Hanson return (1);
886425e8c5aaSvikram }
88653a634bfcSVikram Hegde
886694c894bbSVikram Hegde
886794c894bbSVikram Hegde #define VAL_ALIAS(array, x) (strlen(array[x].pair_alias))
886894c894bbSVikram Hegde #define VAL_CURR(array, x) (strlen(array[x].pair_curr))
886994c894bbSVikram Hegde #define SWAP(array, x, y) \
887094c894bbSVikram Hegde { \
887194c894bbSVikram Hegde alias_pair_t tmpair = array[x]; \
887294c894bbSVikram Hegde array[x] = array[y]; \
887394c894bbSVikram Hegde array[y] = tmpair; \
887494c894bbSVikram Hegde }
887594c894bbSVikram Hegde
887694c894bbSVikram Hegde static int
partition_curr(alias_pair_t * array,int start,int end)887794c894bbSVikram Hegde partition_curr(alias_pair_t *array, int start, int end)
887894c894bbSVikram Hegde {
887994c894bbSVikram Hegde int i = start - 1;
888094c894bbSVikram Hegde int j = end + 1;
888194c894bbSVikram Hegde int pivot = start;
888294c894bbSVikram Hegde
888394c894bbSVikram Hegde for (;;) {
888494c894bbSVikram Hegde do {
888594c894bbSVikram Hegde j--;
888694c894bbSVikram Hegde } while (VAL_CURR(array, j) > VAL_CURR(array, pivot));
888794c894bbSVikram Hegde
888894c894bbSVikram Hegde do {
888994c894bbSVikram Hegde i++;
889094c894bbSVikram Hegde } while (VAL_CURR(array, i) < VAL_CURR(array, pivot));
889194c894bbSVikram Hegde
889294c894bbSVikram Hegde if (i < j)
889394c894bbSVikram Hegde SWAP(array, i, j)
889494c894bbSVikram Hegde else
889594c894bbSVikram Hegde return (j);
889694c894bbSVikram Hegde }
889794c894bbSVikram Hegde }
889894c894bbSVikram Hegde
889994c894bbSVikram Hegde static int
partition_aliases(alias_pair_t * array,int start,int end)890094c894bbSVikram Hegde partition_aliases(alias_pair_t *array, int start, int end)
890194c894bbSVikram Hegde {
890294c894bbSVikram Hegde int i = start - 1;
890394c894bbSVikram Hegde int j = end + 1;
890494c894bbSVikram Hegde int pivot = start;
890594c894bbSVikram Hegde
890694c894bbSVikram Hegde for (;;) {
890794c894bbSVikram Hegde do {
890894c894bbSVikram Hegde j--;
890994c894bbSVikram Hegde } while (VAL_ALIAS(array, j) > VAL_ALIAS(array, pivot));
891094c894bbSVikram Hegde
891194c894bbSVikram Hegde do {
891294c894bbSVikram Hegde i++;
891394c894bbSVikram Hegde } while (VAL_ALIAS(array, i) < VAL_ALIAS(array, pivot));
891494c894bbSVikram Hegde
891594c894bbSVikram Hegde if (i < j)
891694c894bbSVikram Hegde SWAP(array, i, j)
891794c894bbSVikram Hegde else
891894c894bbSVikram Hegde return (j);
891994c894bbSVikram Hegde }
892094c894bbSVikram Hegde }
892194c894bbSVikram Hegde static void
sort_alias_pairs(alias_pair_t * array,int start,int end)892294c894bbSVikram Hegde sort_alias_pairs(alias_pair_t *array, int start, int end)
892394c894bbSVikram Hegde {
892494c894bbSVikram Hegde int mid;
892594c894bbSVikram Hegde
892694c894bbSVikram Hegde if (start < end) {
892794c894bbSVikram Hegde mid = partition_aliases(array, start, end);
892894c894bbSVikram Hegde sort_alias_pairs(array, start, mid);
892994c894bbSVikram Hegde sort_alias_pairs(array, mid + 1, end);
893094c894bbSVikram Hegde }
893194c894bbSVikram Hegde }
893294c894bbSVikram Hegde
893394c894bbSVikram Hegde static void
sort_curr_pairs(alias_pair_t * array,int start,int end)893494c894bbSVikram Hegde sort_curr_pairs(alias_pair_t *array, int start, int end)
893594c894bbSVikram Hegde {
893694c894bbSVikram Hegde int mid;
893794c894bbSVikram Hegde
893894c894bbSVikram Hegde if (start < end) {
893994c894bbSVikram Hegde mid = partition_curr(array, start, end);
894094c894bbSVikram Hegde sort_curr_pairs(array, start, mid);
894194c894bbSVikram Hegde sort_curr_pairs(array, mid + 1, end);
894294c894bbSVikram Hegde }
894394c894bbSVikram Hegde }
894494c894bbSVikram Hegde
894594c894bbSVikram Hegde static void
create_sorted_pairs(plat_alias_t * pali,int npali)894694c894bbSVikram Hegde create_sorted_pairs(plat_alias_t *pali, int npali)
894794c894bbSVikram Hegde {
894894c894bbSVikram Hegde int i;
894994c894bbSVikram Hegde int j;
895094c894bbSVikram Hegde int k;
895194c894bbSVikram Hegde int count;
895294c894bbSVikram Hegde
895394c894bbSVikram Hegde count = 0;
895494c894bbSVikram Hegde for (i = 0; i < npali; i++) {
895594c894bbSVikram Hegde count += pali[i].pali_naliases;
895694c894bbSVikram Hegde }
895794c894bbSVikram Hegde
895894c894bbSVikram Hegde ddi_aliases.dali_alias_pairs = kmem_zalloc(
895994c894bbSVikram Hegde (sizeof (alias_pair_t)) * count, KM_NOSLEEP);
896094c894bbSVikram Hegde if (ddi_aliases.dali_alias_pairs == NULL) {
896194c894bbSVikram Hegde cmn_err(CE_PANIC, "alias path-pair alloc failed");
896294c894bbSVikram Hegde /*NOTREACHED*/
896394c894bbSVikram Hegde }
896494c894bbSVikram Hegde
896594c894bbSVikram Hegde ddi_aliases.dali_curr_pairs = kmem_zalloc(
896694c894bbSVikram Hegde (sizeof (alias_pair_t)) * count, KM_NOSLEEP);
896794c894bbSVikram Hegde if (ddi_aliases.dali_curr_pairs == NULL) {
896894c894bbSVikram Hegde cmn_err(CE_PANIC, "curr path-pair alloc failed");
896994c894bbSVikram Hegde /*NOTREACHED*/
897094c894bbSVikram Hegde }
897194c894bbSVikram Hegde
897294c894bbSVikram Hegde for (i = 0, k = 0; i < npali; i++) {
897394c894bbSVikram Hegde for (j = 0; j < pali[i].pali_naliases; j++, k++) {
897494c894bbSVikram Hegde ddi_aliases.dali_alias_pairs[k].pair_curr =
897594c894bbSVikram Hegde ddi_aliases.dali_curr_pairs[k].pair_curr =
897694c894bbSVikram Hegde pali[i].pali_current;
897794c894bbSVikram Hegde ddi_aliases.dali_alias_pairs[k].pair_alias =
897894c894bbSVikram Hegde ddi_aliases.dali_curr_pairs[k].pair_alias =
897994c894bbSVikram Hegde pali[i].pali_aliases[j];
898094c894bbSVikram Hegde }
898194c894bbSVikram Hegde }
898294c894bbSVikram Hegde
898394c894bbSVikram Hegde ASSERT(k == count);
898494c894bbSVikram Hegde
898594c894bbSVikram Hegde ddi_aliases.dali_num_pairs = count;
898694c894bbSVikram Hegde
898794c894bbSVikram Hegde /* Now sort the array based on length of pair_alias */
898894c894bbSVikram Hegde sort_alias_pairs(ddi_aliases.dali_alias_pairs, 0, count - 1);
898994c894bbSVikram Hegde sort_curr_pairs(ddi_aliases.dali_curr_pairs, 0, count - 1);
899094c894bbSVikram Hegde }
899194c894bbSVikram Hegde
899294c894bbSVikram Hegde void
ddi_register_aliases(plat_alias_t * pali,uint64_t npali)899394c894bbSVikram Hegde ddi_register_aliases(plat_alias_t *pali, uint64_t npali)
899494c894bbSVikram Hegde {
899594c894bbSVikram Hegde
899694c894bbSVikram Hegde ASSERT((pali == NULL) ^ (npali != 0));
899794c894bbSVikram Hegde
899894c894bbSVikram Hegde if (npali == 0) {
899994c894bbSVikram Hegde ddi_err(DER_PANIC, NULL, "npali == 0");
900094c894bbSVikram Hegde /*NOTREACHED*/
900194c894bbSVikram Hegde }
900294c894bbSVikram Hegde
900394c894bbSVikram Hegde if (ddi_aliases_present == B_TRUE) {
900494c894bbSVikram Hegde ddi_err(DER_PANIC, NULL, "multiple init");
900594c894bbSVikram Hegde /*NOTREACHED*/
900694c894bbSVikram Hegde }
900794c894bbSVikram Hegde
900894c894bbSVikram Hegde ddi_aliases.dali_alias_TLB = mod_hash_create_strhash(
900994c894bbSVikram Hegde "ddi-alias-tlb", DDI_ALIAS_HASH_SIZE, mod_hash_null_valdtor);
901094c894bbSVikram Hegde if (ddi_aliases.dali_alias_TLB == NULL) {
901194c894bbSVikram Hegde ddi_err(DER_PANIC, NULL, "alias TLB hash alloc failed");
901294c894bbSVikram Hegde /*NOTREACHED*/
901394c894bbSVikram Hegde }
901494c894bbSVikram Hegde
901594c894bbSVikram Hegde ddi_aliases.dali_curr_TLB = mod_hash_create_strhash(
901694c894bbSVikram Hegde "ddi-curr-tlb", DDI_ALIAS_HASH_SIZE, mod_hash_null_valdtor);
901794c894bbSVikram Hegde if (ddi_aliases.dali_curr_TLB == NULL) {
901894c894bbSVikram Hegde ddi_err(DER_PANIC, NULL, "curr TLB hash alloc failed");
901994c894bbSVikram Hegde /*NOTREACHED*/
902094c894bbSVikram Hegde }
902194c894bbSVikram Hegde
902294c894bbSVikram Hegde create_sorted_pairs(pali, npali);
902394c894bbSVikram Hegde
902494c894bbSVikram Hegde tsd_create(&tsd_ddi_redirect, NULL);
902594c894bbSVikram Hegde
902694c894bbSVikram Hegde ddi_aliases_present = B_TRUE;
902794c894bbSVikram Hegde }
902894c894bbSVikram Hegde
902994c894bbSVikram Hegde static dev_info_t *
path_to_dip(char * path)903094c894bbSVikram Hegde path_to_dip(char *path)
903194c894bbSVikram Hegde {
903294c894bbSVikram Hegde dev_info_t *currdip;
903394c894bbSVikram Hegde int error;
903494c894bbSVikram Hegde char *pdup;
903594c894bbSVikram Hegde
903694c894bbSVikram Hegde pdup = ddi_strdup(path, KM_NOSLEEP);
903794c894bbSVikram Hegde if (pdup == NULL) {
903894c894bbSVikram Hegde cmn_err(CE_PANIC, "path strdup failed: %s", path);
903994c894bbSVikram Hegde /*NOTREACHED*/
904094c894bbSVikram Hegde }
904194c894bbSVikram Hegde
904294c894bbSVikram Hegde error = resolve_pathname(pdup, &currdip, NULL, NULL);
904394c894bbSVikram Hegde
904494c894bbSVikram Hegde kmem_free(pdup, strlen(path) + 1);
904594c894bbSVikram Hegde
904694c894bbSVikram Hegde return (error ? NULL : currdip);
904794c894bbSVikram Hegde }
904894c894bbSVikram Hegde
904994c894bbSVikram Hegde dev_info_t *
ddi_alias_to_currdip(char * alias,int i)905094c894bbSVikram Hegde ddi_alias_to_currdip(char *alias, int i)
905194c894bbSVikram Hegde {
905294c894bbSVikram Hegde alias_pair_t *pair;
905394c894bbSVikram Hegde char *curr;
905494c894bbSVikram Hegde dev_info_t *currdip = NULL;
905594c894bbSVikram Hegde char *aliasdup;
9056bccb2643SJerry Gilliam int rv, len;
905794c894bbSVikram Hegde
905894c894bbSVikram Hegde pair = &(ddi_aliases.dali_alias_pairs[i]);
905994c894bbSVikram Hegde len = strlen(pair->pair_alias);
906094c894bbSVikram Hegde
906194c894bbSVikram Hegde curr = NULL;
906294c894bbSVikram Hegde aliasdup = ddi_strdup(alias, KM_NOSLEEP);
906394c894bbSVikram Hegde if (aliasdup == NULL) {
906494c894bbSVikram Hegde cmn_err(CE_PANIC, "aliasdup alloc failed");
906594c894bbSVikram Hegde /*NOTREACHED*/
906694c894bbSVikram Hegde }
906794c894bbSVikram Hegde
906894c894bbSVikram Hegde if (strncmp(alias, pair->pair_alias, len) != 0)
906994c894bbSVikram Hegde goto out;
907094c894bbSVikram Hegde
907194c894bbSVikram Hegde if (alias[len] != '/' && alias[len] != '\0')
907294c894bbSVikram Hegde goto out;
907394c894bbSVikram Hegde
907494c894bbSVikram Hegde curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
907594c894bbSVikram Hegde if (curr == NULL) {
907694c894bbSVikram Hegde cmn_err(CE_PANIC, "curr alloc failed");
907794c894bbSVikram Hegde /*NOTREACHED*/
907894c894bbSVikram Hegde }
907994c894bbSVikram Hegde (void) strlcpy(curr, pair->pair_curr, MAXPATHLEN);
908094c894bbSVikram Hegde if (alias[len] == '/') {
908194c894bbSVikram Hegde (void) strlcat(curr, "/", MAXPATHLEN);
908294c894bbSVikram Hegde (void) strlcat(curr, &alias[len + 1], MAXPATHLEN);
908394c894bbSVikram Hegde }
908494c894bbSVikram Hegde
908594c894bbSVikram Hegde currdip = path_to_dip(curr);
908694c894bbSVikram Hegde
908794c894bbSVikram Hegde out:
908894c894bbSVikram Hegde if (currdip) {
9089bccb2643SJerry Gilliam rv = mod_hash_insert(ddi_aliases.dali_alias_TLB,
909094c894bbSVikram Hegde (mod_hash_key_t)aliasdup, (mod_hash_val_t)curr);
9091bccb2643SJerry Gilliam if (rv != 0) {
9092bccb2643SJerry Gilliam kmem_free(curr, MAXPATHLEN);
9093bccb2643SJerry Gilliam strfree(aliasdup);
9094bccb2643SJerry Gilliam }
909594c894bbSVikram Hegde } else {
9096bccb2643SJerry Gilliam rv = mod_hash_insert(ddi_aliases.dali_alias_TLB,
909794c894bbSVikram Hegde (mod_hash_key_t)aliasdup, (mod_hash_val_t)NULL);
9098bccb2643SJerry Gilliam if (rv != 0) {
9099bccb2643SJerry Gilliam strfree(aliasdup);
9100bccb2643SJerry Gilliam }
910194c894bbSVikram Hegde if (curr)
910294c894bbSVikram Hegde kmem_free(curr, MAXPATHLEN);
910394c894bbSVikram Hegde }
910494c894bbSVikram Hegde
910594c894bbSVikram Hegde return (currdip);
910694c894bbSVikram Hegde }
910794c894bbSVikram Hegde
910894c894bbSVikram Hegde char *
ddi_curr_to_alias(char * curr,int i)910994c894bbSVikram Hegde ddi_curr_to_alias(char *curr, int i)
911094c894bbSVikram Hegde {
911194c894bbSVikram Hegde alias_pair_t *pair;
911294c894bbSVikram Hegde char *alias;
911394c894bbSVikram Hegde char *currdup;
911494c894bbSVikram Hegde int len;
9115bccb2643SJerry Gilliam int rv;
911694c894bbSVikram Hegde
911794c894bbSVikram Hegde pair = &(ddi_aliases.dali_curr_pairs[i]);
911894c894bbSVikram Hegde
911994c894bbSVikram Hegde len = strlen(pair->pair_curr);
912094c894bbSVikram Hegde
912194c894bbSVikram Hegde alias = NULL;
912294c894bbSVikram Hegde
912394c894bbSVikram Hegde currdup = ddi_strdup(curr, KM_NOSLEEP);
912494c894bbSVikram Hegde if (currdup == NULL) {
912594c894bbSVikram Hegde cmn_err(CE_PANIC, "currdup alloc failed");
912694c894bbSVikram Hegde /*NOTREACHED*/
912794c894bbSVikram Hegde }
912894c894bbSVikram Hegde
912994c894bbSVikram Hegde if (strncmp(curr, pair->pair_curr, len) != 0)
913094c894bbSVikram Hegde goto out;
913194c894bbSVikram Hegde
913294c894bbSVikram Hegde if (curr[len] != '/' && curr[len] != '\0')
913394c894bbSVikram Hegde goto out;
913494c894bbSVikram Hegde
913594c894bbSVikram Hegde alias = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
913694c894bbSVikram Hegde if (alias == NULL) {
913794c894bbSVikram Hegde cmn_err(CE_PANIC, "alias alloc failed");
913894c894bbSVikram Hegde /*NOTREACHED*/
913994c894bbSVikram Hegde }
914094c894bbSVikram Hegde
914194c894bbSVikram Hegde (void) strlcpy(alias, pair->pair_alias, MAXPATHLEN);
914294c894bbSVikram Hegde if (curr[len] == '/') {
914394c894bbSVikram Hegde (void) strlcat(alias, "/", MAXPATHLEN);
914494c894bbSVikram Hegde (void) strlcat(alias, &curr[len + 1], MAXPATHLEN);
914594c894bbSVikram Hegde }
914694c894bbSVikram Hegde
914794c894bbSVikram Hegde if (e_ddi_path_to_instance(alias) == NULL) {
914894c894bbSVikram Hegde kmem_free(alias, MAXPATHLEN);
914994c894bbSVikram Hegde alias = NULL;
915094c894bbSVikram Hegde }
915194c894bbSVikram Hegde
915294c894bbSVikram Hegde out:
9153bccb2643SJerry Gilliam rv = mod_hash_insert(ddi_aliases.dali_curr_TLB,
915494c894bbSVikram Hegde (mod_hash_key_t)currdup, (mod_hash_val_t)alias);
9155bccb2643SJerry Gilliam if (rv != 0) {
9156bccb2643SJerry Gilliam strfree(currdup);
9157bccb2643SJerry Gilliam }
915894c894bbSVikram Hegde
915994c894bbSVikram Hegde return (alias);
916094c894bbSVikram Hegde }
916194c894bbSVikram Hegde
916294c894bbSVikram Hegde dev_info_t *
ddi_alias_redirect(char * alias)916394c894bbSVikram Hegde ddi_alias_redirect(char *alias)
916494c894bbSVikram Hegde {
916594c894bbSVikram Hegde char *curr;
916694c894bbSVikram Hegde dev_info_t *currdip;
916794c894bbSVikram Hegde int i;
916894c894bbSVikram Hegde
916994c894bbSVikram Hegde if (ddi_aliases_present == B_FALSE)
917094c894bbSVikram Hegde return (NULL);
917194c894bbSVikram Hegde
917294c894bbSVikram Hegde if (tsd_get(tsd_ddi_redirect))
917394c894bbSVikram Hegde return (NULL);
917494c894bbSVikram Hegde
917594c894bbSVikram Hegde (void) tsd_set(tsd_ddi_redirect, (void *)1);
917694c894bbSVikram Hegde
917794c894bbSVikram Hegde ASSERT(ddi_aliases.dali_alias_TLB);
917894c894bbSVikram Hegde ASSERT(ddi_aliases.dali_alias_pairs);
917994c894bbSVikram Hegde
918094c894bbSVikram Hegde curr = NULL;
918194c894bbSVikram Hegde if (mod_hash_find(ddi_aliases.dali_alias_TLB,
918294c894bbSVikram Hegde (mod_hash_key_t)alias, (mod_hash_val_t *)&curr) == 0) {
918394c894bbSVikram Hegde currdip = curr ? path_to_dip(curr) : NULL;
918494c894bbSVikram Hegde goto out;
918594c894bbSVikram Hegde }
918694c894bbSVikram Hegde
918794c894bbSVikram Hegde /* The TLB has no translation, do it the hard way */
918894c894bbSVikram Hegde currdip = NULL;
918994c894bbSVikram Hegde for (i = ddi_aliases.dali_num_pairs - 1; i >= 0; i--) {
919094c894bbSVikram Hegde currdip = ddi_alias_to_currdip(alias, i);
919194c894bbSVikram Hegde if (currdip)
919294c894bbSVikram Hegde break;
919394c894bbSVikram Hegde }
919494c894bbSVikram Hegde out:
919594c894bbSVikram Hegde (void) tsd_set(tsd_ddi_redirect, NULL);
919694c894bbSVikram Hegde
919794c894bbSVikram Hegde return (currdip);
919894c894bbSVikram Hegde }
919994c894bbSVikram Hegde
920094c894bbSVikram Hegde char *
ddi_curr_redirect(char * curr)920194c894bbSVikram Hegde ddi_curr_redirect(char *curr)
920294c894bbSVikram Hegde {
920394c894bbSVikram Hegde char *alias;
920494c894bbSVikram Hegde int i;
920594c894bbSVikram Hegde
920694c894bbSVikram Hegde if (ddi_aliases_present == B_FALSE)
920794c894bbSVikram Hegde return (NULL);
920894c894bbSVikram Hegde
920994c894bbSVikram Hegde if (tsd_get(tsd_ddi_redirect))
921094c894bbSVikram Hegde return (NULL);
921194c894bbSVikram Hegde
921294c894bbSVikram Hegde (void) tsd_set(tsd_ddi_redirect, (void *)1);
921394c894bbSVikram Hegde
921494c894bbSVikram Hegde ASSERT(ddi_aliases.dali_curr_TLB);
921594c894bbSVikram Hegde ASSERT(ddi_aliases.dali_curr_pairs);
921694c894bbSVikram Hegde
921794c894bbSVikram Hegde alias = NULL;
921894c894bbSVikram Hegde if (mod_hash_find(ddi_aliases.dali_curr_TLB,
921994c894bbSVikram Hegde (mod_hash_key_t)curr, (mod_hash_val_t *)&alias) == 0) {
922094c894bbSVikram Hegde goto out;
922194c894bbSVikram Hegde }
922294c894bbSVikram Hegde
922394c894bbSVikram Hegde
922494c894bbSVikram Hegde /* The TLB has no translation, do it the slow way */
922594c894bbSVikram Hegde alias = NULL;
922694c894bbSVikram Hegde for (i = ddi_aliases.dali_num_pairs - 1; i >= 0; i--) {
922794c894bbSVikram Hegde alias = ddi_curr_to_alias(curr, i);
922894c894bbSVikram Hegde if (alias)
922994c894bbSVikram Hegde break;
923094c894bbSVikram Hegde }
923194c894bbSVikram Hegde
923294c894bbSVikram Hegde out:
923394c894bbSVikram Hegde (void) tsd_set(tsd_ddi_redirect, NULL);
923494c894bbSVikram Hegde
923594c894bbSVikram Hegde return (alias);
923694c894bbSVikram Hegde }
923794c894bbSVikram Hegde
92383a634bfcSVikram Hegde void
ddi_err(ddi_err_t ade,dev_info_t * rdip,const char * fmt,...)92393a634bfcSVikram Hegde ddi_err(ddi_err_t ade, dev_info_t *rdip, const char *fmt, ...)
92403a634bfcSVikram Hegde {
92413a634bfcSVikram Hegde va_list ap;
92423a634bfcSVikram Hegde char strbuf[256];
92433a634bfcSVikram Hegde char *buf;
92443a634bfcSVikram Hegde size_t buflen, tlen;
92453a634bfcSVikram Hegde int ce;
92463a634bfcSVikram Hegde int de;
92473a634bfcSVikram Hegde const char *fmtbad = "Invalid arguments to ddi_err()";
92483a634bfcSVikram Hegde
92493a634bfcSVikram Hegde de = DER_CONT;
92503a634bfcSVikram Hegde strbuf[1] = '\0';
92513a634bfcSVikram Hegde
92523a634bfcSVikram Hegde switch (ade) {
92533a634bfcSVikram Hegde case DER_CONS:
92543a634bfcSVikram Hegde strbuf[0] = '^';
92553a634bfcSVikram Hegde break;
92563a634bfcSVikram Hegde case DER_LOG:
92573a634bfcSVikram Hegde strbuf[0] = '!';
92583a634bfcSVikram Hegde break;
92593a634bfcSVikram Hegde case DER_VERB:
92603a634bfcSVikram Hegde strbuf[0] = '?';
92613a634bfcSVikram Hegde break;
92623a634bfcSVikram Hegde default:
92633a634bfcSVikram Hegde strbuf[0] = '\0';
92643a634bfcSVikram Hegde de = ade;
92653a634bfcSVikram Hegde break;
92663a634bfcSVikram Hegde }
92673a634bfcSVikram Hegde
92683a634bfcSVikram Hegde tlen = strlen(strbuf);
92693a634bfcSVikram Hegde buf = strbuf + tlen;
92703a634bfcSVikram Hegde buflen = sizeof (strbuf) - tlen;
92713a634bfcSVikram Hegde
92723a634bfcSVikram Hegde if (rdip && ddi_get_instance(rdip) == -1) {
92733a634bfcSVikram Hegde (void) snprintf(buf, buflen, "%s: ",
92743a634bfcSVikram Hegde ddi_driver_name(rdip));
92753a634bfcSVikram Hegde } else if (rdip) {
92763a634bfcSVikram Hegde (void) snprintf(buf, buflen, "%s%d: ",
92773a634bfcSVikram Hegde ddi_driver_name(rdip), ddi_get_instance(rdip));
92783a634bfcSVikram Hegde }
92793a634bfcSVikram Hegde
92803a634bfcSVikram Hegde tlen = strlen(strbuf);
92813a634bfcSVikram Hegde buf = strbuf + tlen;
92823a634bfcSVikram Hegde buflen = sizeof (strbuf) - tlen;
92833a634bfcSVikram Hegde
92843a634bfcSVikram Hegde va_start(ap, fmt);
92853a634bfcSVikram Hegde switch (de) {
92863a634bfcSVikram Hegde case DER_CONT:
92873a634bfcSVikram Hegde (void) vsnprintf(buf, buflen, fmt, ap);
92883a634bfcSVikram Hegde if (ade != DER_CONT) {
92893a634bfcSVikram Hegde (void) strlcat(strbuf, "\n", sizeof (strbuf));
92903a634bfcSVikram Hegde }
92913a634bfcSVikram Hegde ce = CE_CONT;
92923a634bfcSVikram Hegde break;
92933a634bfcSVikram Hegde case DER_NOTE:
92943a634bfcSVikram Hegde (void) vsnprintf(buf, buflen, fmt, ap);
92953a634bfcSVikram Hegde ce = CE_NOTE;
92963a634bfcSVikram Hegde break;
92973a634bfcSVikram Hegde case DER_WARN:
92983a634bfcSVikram Hegde (void) vsnprintf(buf, buflen, fmt, ap);
92993a634bfcSVikram Hegde ce = CE_WARN;
93003a634bfcSVikram Hegde break;
93013a634bfcSVikram Hegde case DER_MODE:
93023a634bfcSVikram Hegde (void) vsnprintf(buf, buflen, fmt, ap);
93033a634bfcSVikram Hegde if (ddi_err_panic == B_TRUE) {
93043a634bfcSVikram Hegde ce = CE_PANIC;
93053a634bfcSVikram Hegde } else {
93063a634bfcSVikram Hegde ce = CE_WARN;
93073a634bfcSVikram Hegde }
93083a634bfcSVikram Hegde break;
93093a634bfcSVikram Hegde case DER_DEBUG:
93103a634bfcSVikram Hegde (void) snprintf(buf, buflen, "DEBUG: ");
93113a634bfcSVikram Hegde tlen = strlen("DEBUG: ");
93123a634bfcSVikram Hegde (void) vsnprintf(buf + tlen, buflen - tlen, fmt, ap);
93133a634bfcSVikram Hegde ce = CE_CONT;
93143a634bfcSVikram Hegde break;
93153a634bfcSVikram Hegde case DER_PANIC:
93163a634bfcSVikram Hegde (void) vsnprintf(buf, buflen, fmt, ap);
93173a634bfcSVikram Hegde ce = CE_PANIC;
93183a634bfcSVikram Hegde break;
93193a634bfcSVikram Hegde case DER_INVALID:
93203a634bfcSVikram Hegde default:
93213a634bfcSVikram Hegde (void) snprintf(buf, buflen, fmtbad);
93223a634bfcSVikram Hegde tlen = strlen(fmtbad);
93233a634bfcSVikram Hegde (void) vsnprintf(buf + tlen, buflen - tlen, fmt, ap);
93243a634bfcSVikram Hegde ce = CE_PANIC;
93253a634bfcSVikram Hegde break;
93263a634bfcSVikram Hegde }
93273a634bfcSVikram Hegde va_end(ap);
93283a634bfcSVikram Hegde
93293a634bfcSVikram Hegde cmn_err(ce, strbuf);
93303a634bfcSVikram Hegde }
93313a634bfcSVikram Hegde
93323a634bfcSVikram Hegde /*ARGSUSED*/
93333a634bfcSVikram Hegde void
ddi_mem_update(uint64_t addr,uint64_t size)93343a634bfcSVikram Hegde ddi_mem_update(uint64_t addr, uint64_t size)
93353a634bfcSVikram Hegde {
93363a634bfcSVikram Hegde #if defined(__x86) && !defined(__xpv)
93373a634bfcSVikram Hegde extern void immu_physmem_update(uint64_t addr, uint64_t size);
93383a634bfcSVikram Hegde immu_physmem_update(addr, size);
93393a634bfcSVikram Hegde #else
93403a634bfcSVikram Hegde /*LINTED*/
93413a634bfcSVikram Hegde ;
93423a634bfcSVikram Hegde #endif
93433a634bfcSVikram Hegde }
93443ce53722SRobert Mustacchi
93453ce53722SRobert Mustacchi void
e_ddi_register_unbind_callback(dev_info_t * dip,ddi_unbind_callback_t * cb)93463ce53722SRobert Mustacchi e_ddi_register_unbind_callback(dev_info_t *dip, ddi_unbind_callback_t *cb)
93473ce53722SRobert Mustacchi {
93483ce53722SRobert Mustacchi struct dev_info *devi = DEVI(dip);
93493ce53722SRobert Mustacchi
93503ce53722SRobert Mustacchi mutex_enter(&devi->devi_unbind_lock);
93513ce53722SRobert Mustacchi list_insert_tail(&devi->devi_unbind_cbs, cb);
93523ce53722SRobert Mustacchi mutex_exit(&devi->devi_unbind_lock);
93533ce53722SRobert Mustacchi }
9354