xref: /illumos-gate/usr/src/uts/common/os/devcfg.c (revision 9151f8f1317aea334cb8686b8543b1666a2824bd)
1  /*
2   * CDDL HEADER START
3   *
4   * The contents of this file are subject to the terms of the
5   * Common Development and Distribution License (the "License").
6   * You may not use this file except in compliance with the License.
7   *
8   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9   * or http://www.opensolaris.org/os/licensing.
10   * See the License for the specific language governing permissions
11   * and limitations under the License.
12   *
13   * When distributing Covered Code, include this CDDL HEADER in each
14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15   * If applicable, add the following below this CDDL HEADER, with the
16   * fields enclosed by brackets "[]" replaced with your own identifying
17   * information: Portions Copyright [yyyy] [name of copyright owner]
18   *
19   * CDDL HEADER END
20   */
21  /*
22   * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
23   * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
24   * Copyright 2012 Garrett D'Amore <garrett@damore.org>.  All rights reserved.
25   * Copyright (c) 2013, Joyent, Inc. All rights reserved.
26   * Copyright (c) 2016 by Delphix. All rights reserved.
27   * Copyright 2020 Joshua M. Clulow <josh@sysmgr.org>
28   * Copyright 2023 Oxide Computer Company
29   */
30  
31  #include <sys/note.h>
32  #include <sys/t_lock.h>
33  #include <sys/cmn_err.h>
34  #include <sys/instance.h>
35  #include <sys/conf.h>
36  #include <sys/stat.h>
37  #include <sys/ddi.h>
38  #include <sys/hwconf.h>
39  #include <sys/sunddi.h>
40  #include <sys/sunndi.h>
41  #include <sys/ddi_impldefs.h>
42  #include <sys/ndi_impldefs.h>
43  #include <sys/modctl.h>
44  #include <sys/contract/device_impl.h>
45  #include <sys/dacf.h>
46  #include <sys/promif.h>
47  #include <sys/pci.h>
48  #include <sys/cpuvar.h>
49  #include <sys/pathname.h>
50  #include <sys/taskq.h>
51  #include <sys/sysevent.h>
52  #include <sys/sunmdi.h>
53  #include <sys/stream.h>
54  #include <sys/strsubr.h>
55  #include <sys/fs/snode.h>
56  #include <sys/fs/dv_node.h>
57  #include <sys/reboot.h>
58  #include <sys/sysmacros.h>
59  #include <sys/systm.h>
60  #include <sys/fs/sdev_impl.h>
61  #include <sys/sunldi.h>
62  #include <sys/sunldi_impl.h>
63  #include <sys/bootprops.h>
64  #include <sys/varargs.h>
65  #include <sys/modhash.h>
66  #include <sys/instance.h>
67  #include <sys/sysevent/eventdefs.h>
68  
69  #if defined(__amd64) && !defined(__xpv)
70  #include <sys/iommulib.h>
71  #endif
72  
73  #ifdef DEBUG
74  int ddidebug = DDI_AUDIT;
75  #else
76  int ddidebug = 0;
77  #endif
78  
79  #define	MT_CONFIG_OP	0
80  #define	MT_UNCONFIG_OP	1
81  
82  /* Multi-threaded configuration */
83  struct mt_config_handle {
84  	kmutex_t mtc_lock;
85  	kcondvar_t mtc_cv;
86  	int mtc_thr_count;
87  	dev_info_t *mtc_pdip;	/* parent dip for mt_config_children */
88  	dev_info_t **mtc_fdip;	/* "a" dip where unconfigure failed */
89  	major_t mtc_parmajor;	/* parent major for mt_config_driver */
90  	major_t mtc_major;
91  	int mtc_flags;
92  	int mtc_op;		/* config or unconfig */
93  	int mtc_error;		/* operation error */
94  	struct brevq_node **mtc_brevqp;	/* outstanding branch events queue */
95  #ifdef DEBUG
96  	int total_time;
97  	timestruc_t start_time;
98  #endif /* DEBUG */
99  };
100  
101  struct devi_nodeid {
102  	pnode_t nodeid;
103  	dev_info_t *dip;
104  	struct devi_nodeid *next;
105  };
106  
107  struct devi_nodeid_list {
108  	kmutex_t dno_lock;		/* Protects other fields */
109  	struct devi_nodeid *dno_head;	/* list of devi nodeid elements */
110  	struct devi_nodeid *dno_free;	/* Free list */
111  	uint_t dno_list_length;		/* number of dips in list */
112  };
113  
114  /* used to keep track of branch remove events to be generated */
115  struct brevq_node {
116  	char *brn_deviname;
117  	struct brevq_node *brn_sibling;
118  	struct brevq_node *brn_child;
119  };
120  
121  static struct devi_nodeid_list devi_nodeid_list;
122  static struct devi_nodeid_list *devimap = &devi_nodeid_list;
123  
124  /*
125   * Well known nodes which are attached first at boot time.
126   */
127  dev_info_t *top_devinfo;		/* root of device tree */
128  dev_info_t *options_dip;
129  dev_info_t *pseudo_dip;
130  dev_info_t *clone_dip;
131  dev_info_t *scsi_vhci_dip;		/* MPXIO dip */
132  major_t clone_major;
133  
134  /*
135   * A non-global zone's /dev is derived from the device tree.
136   * This generation number serves to indicate when a zone's
137   * /dev may need to be updated.
138   */
139  volatile ulong_t devtree_gen;		/* generation number */
140  
141  /* block all future dev_info state changes */
142  hrtime_t volatile devinfo_freeze = 0;
143  
144  /* number of dev_info attaches/detaches currently in progress */
145  static ulong_t devinfo_attach_detach = 0;
146  
147  extern int	sys_shutdown;
148  extern kmutex_t global_vhci_lock;
149  
150  /* bitset of DS_SYSAVAIL & DS_RECONFIG - no races, no lock */
151  static int devname_state = 0;
152  
153  /*
154   * The devinfo snapshot cache and related variables.
155   * The only field in the di_cache structure that needs initialization
156   * is the mutex (cache_lock). However, since this is an adaptive mutex
157   * (MUTEX_DEFAULT) - it is automatically initialized by being allocated
158   * in zeroed memory (static storage class). Therefore no explicit
159   * initialization of the di_cache structure is needed.
160   */
161  struct di_cache	di_cache = {1};
162  int		di_cache_debug = 0;
163  
164  /* For ddvis, which needs pseudo children under PCI */
165  int pci_allow_pseudo_children = 0;
166  
167  /* Allow path-oriented alias driver binding on driver.conf enumerated nodes */
168  int driver_conf_allow_path_alias = 1;
169  
170  /*
171   * The following switch is for service people, in case a
172   * 3rd party driver depends on identify(9e) being called.
173   */
174  int identify_9e = 0;
175  
176  /*
177   * Add flag so behaviour of preventing attach for retired persistant nodes
178   * can be disabled.
179   */
180  int retire_prevents_attach = 1;
181  
182  int mtc_off;					/* turn off mt config */
183  
184  int quiesce_debug = 0;
185  
186  boolean_t ddi_aliases_present = B_FALSE;
187  ddi_alias_t ddi_aliases;
188  uint_t tsd_ddi_redirect;
189  
190  #define	DDI_ALIAS_HASH_SIZE	(2700)
191  
192  static kmem_cache_t *ddi_node_cache;		/* devinfo node cache */
193  static devinfo_log_header_t *devinfo_audit_log;	/* devinfo log */
194  static int devinfo_log_size;			/* size in pages */
195  
196  boolean_t ddi_err_panic = B_FALSE;
197  
198  static int lookup_compatible(dev_info_t *, uint_t);
199  static char *encode_composite_string(char **, uint_t, size_t *, uint_t);
200  static void link_to_driver_list(dev_info_t *);
201  static void unlink_from_driver_list(dev_info_t *);
202  static void add_to_dn_list(struct devnames *, dev_info_t *);
203  static void remove_from_dn_list(struct devnames *, dev_info_t *);
204  static dev_info_t *find_duplicate_child();
205  static void add_global_props(dev_info_t *);
206  static void remove_global_props(dev_info_t *);
207  static int uninit_node(dev_info_t *);
208  static void da_log_init(void);
209  static void da_log_enter(dev_info_t *);
210  static int walk_devs(dev_info_t *, int (*f)(dev_info_t *, void *), void *, int);
211  static int reset_nexus_flags(dev_info_t *, void *);
212  static void ddi_optimize_dtree(dev_info_t *);
213  static int is_leaf_node(dev_info_t *);
214  static struct mt_config_handle *mt_config_init(dev_info_t *, dev_info_t **,
215      int, major_t, int, struct brevq_node **);
216  static void mt_config_children(struct mt_config_handle *);
217  static void mt_config_driver(struct mt_config_handle *);
218  static int mt_config_fini(struct mt_config_handle *);
219  static int devi_unconfig_common(dev_info_t *, dev_info_t **, int, major_t,
220      struct brevq_node **);
221  static int
222  ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
223      dev_info_t **childp, int flags);
224  static void i_link_vhci_node(dev_info_t *);
225  static void ndi_devi_exit_and_wait(dev_info_t *dip, clock_t end_time);
226  static int ndi_devi_unbind_driver(dev_info_t *dip);
227  
228  static int i_ddi_check_retire(dev_info_t *dip);
229  
230  static void quiesce_one_device(dev_info_t *, void *);
231  
232  dev_info_t *ddi_alias_redirect(char *alias);
233  char *ddi_curr_redirect(char *currpath);
234  
235  
236  /*
237   * dev_info cache and node management
238   */
239  
240  /* initialize dev_info node cache */
241  void
i_ddi_node_cache_init()242  i_ddi_node_cache_init()
243  {
244  	ASSERT(ddi_node_cache == NULL);
245  	ddi_node_cache = kmem_cache_create("dev_info_node_cache",
246  	    sizeof (struct dev_info), 0, NULL, NULL, NULL, NULL, NULL, 0);
247  
248  	if (ddidebug & DDI_AUDIT)
249  		da_log_init();
250  }
251  
252  
253  /*
254   * Allocating a dev_info node, callable from interrupt context with KM_NOSLEEP
255   * The allocated node has a reference count of 0.
256   */
257  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)258  i_ddi_alloc_node(dev_info_t *pdip, const char *node_name, pnode_t nodeid,
259      int instance, ddi_prop_t *sys_prop, int flag)
260  {
261  	struct dev_info *devi;
262  	struct devi_nodeid *elem;
263  	static char failed[] = "i_ddi_alloc_node: out of memory";
264  
265  	ASSERT(node_name != NULL);
266  
267  	if ((devi = kmem_cache_alloc(ddi_node_cache, flag)) == NULL) {
268  		cmn_err(CE_NOTE, failed);
269  		return (NULL);
270  	}
271  
272  	bzero(devi, sizeof (struct dev_info));
273  
274  	if (devinfo_audit_log) {
275  		devi->devi_audit = kmem_zalloc(sizeof (devinfo_audit_t), flag);
276  		if (devi->devi_audit == NULL)
277  			goto fail;
278  	}
279  
280  	if ((devi->devi_node_name = i_ddi_strdup(node_name, flag)) == NULL)
281  		goto fail;
282  
283  	/* default binding name is node name */
284  	devi->devi_binding_name = devi->devi_node_name;
285  	devi->devi_major = DDI_MAJOR_T_NONE;	/* unbound by default */
286  
287  	/*
288  	 * Make a copy of system property
289  	 */
290  	if (sys_prop &&
291  	    (devi->devi_sys_prop_ptr = i_ddi_prop_list_dup(sys_prop, flag))
292  	    == NULL)
293  		goto fail;
294  
295  	/*
296  	 * Assign devi_nodeid, devi_node_class, devi_node_attributes
297  	 * according to the following algorithm:
298  	 *
299  	 * nodeid arg			node class		node attributes
300  	 *
301  	 * DEVI_PSEUDO_NODEID		DDI_NC_PSEUDO		A
302  	 * DEVI_SID_NODEID		DDI_NC_PSEUDO		A,P
303  	 * DEVI_SID_HIDDEN_NODEID	DDI_NC_PSEUDO		A,P,H
304  	 * DEVI_SID_HP_NODEID		DDI_NC_PSEUDO		A,P,h
305  	 * DEVI_SID_HP_HIDDEN_NODEID	DDI_NC_PSEUDO		A,P,H,h
306  	 * other			DDI_NC_PROM		P
307  	 *
308  	 * Where A = DDI_AUTO_ASSIGNED_NODEID (auto-assign a nodeid)
309  	 * and	 P = DDI_PERSISTENT
310  	 * and	 H = DDI_HIDDEN_NODE
311  	 * and	 h = DDI_HOTPLUG_NODE
312  	 *
313  	 * auto-assigned nodeids are also auto-freed.
314  	 */
315  	devi->devi_node_attributes = 0;
316  	elem = NULL;
317  	switch (nodeid) {
318  	case DEVI_SID_HIDDEN_NODEID:
319  		devi->devi_node_attributes |= DDI_HIDDEN_NODE;
320  		goto sid;
321  
322  	case DEVI_SID_HP_NODEID:
323  		devi->devi_node_attributes |= DDI_HOTPLUG_NODE;
324  		goto sid;
325  
326  	case DEVI_SID_HP_HIDDEN_NODEID:
327  		devi->devi_node_attributes |= DDI_HIDDEN_NODE;
328  		devi->devi_node_attributes |= DDI_HOTPLUG_NODE;
329  		goto sid;
330  
331  	case DEVI_SID_NODEID:
332  sid:		devi->devi_node_attributes |= DDI_PERSISTENT;
333  		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
334  			goto fail;
335  		/*FALLTHROUGH*/
336  
337  	case DEVI_PSEUDO_NODEID:
338  		devi->devi_node_attributes |= DDI_AUTO_ASSIGNED_NODEID;
339  		devi->devi_node_class = DDI_NC_PSEUDO;
340  		if (impl_ddi_alloc_nodeid(&devi->devi_nodeid)) {
341  			panic("i_ddi_alloc_node: out of nodeids");
342  			/*NOTREACHED*/
343  		}
344  		break;
345  
346  	default:
347  		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
348  			goto fail;
349  
350  		/*
351  		 * the nodetype is 'prom', try to 'take' the nodeid now.
352  		 * This requires memory allocation, so check for failure.
353  		 */
354  		if (impl_ddi_take_nodeid(nodeid, flag) != 0) {
355  			kmem_free(elem, sizeof (*elem));
356  			goto fail;
357  		}
358  
359  		devi->devi_nodeid = nodeid;
360  		devi->devi_node_class = DDI_NC_PROM;
361  		devi->devi_node_attributes = DDI_PERSISTENT;
362  		break;
363  	}
364  
365  	if (ndi_dev_is_persistent_node((dev_info_t *)devi)) {
366  		mutex_enter(&devimap->dno_lock);
367  		elem->next = devimap->dno_free;
368  		devimap->dno_free = elem;
369  		mutex_exit(&devimap->dno_lock);
370  	} else if (elem != NULL) {
371  		kmem_free(elem, sizeof (*elem));
372  	}
373  
374  	/*
375  	 * Instance is normally initialized to -1. In a few special
376  	 * cases, the caller may specify an instance (e.g. CPU nodes).
377  	 */
378  	devi->devi_instance = instance;
379  
380  	/*
381  	 * set parent and bus_ctl parent
382  	 */
383  	devi->devi_parent = DEVI(pdip);
384  	devi->devi_bus_ctl = DEVI(pdip);
385  
386  	NDI_CONFIG_DEBUG((CE_CONT,
387  	    "i_ddi_alloc_node: name=%s id=%d\n", node_name, devi->devi_nodeid));
388  
389  	cv_init(&(devi->devi_cv), NULL, CV_DEFAULT, NULL);
390  	mutex_init(&(devi->devi_lock), NULL, MUTEX_DEFAULT, NULL);
391  	mutex_init(&(devi->devi_pm_lock), NULL, MUTEX_DEFAULT, NULL);
392  	mutex_init(&(devi->devi_pm_busy_lock), NULL, MUTEX_DEFAULT, NULL);
393  
394  	RIO_TRACE((CE_NOTE, "i_ddi_alloc_node: Initing contract fields: "
395  	    "dip=%p, name=%s", (void *)devi, node_name));
396  
397  	mutex_init(&(devi->devi_ct_lock), NULL, MUTEX_DEFAULT, NULL);
398  	cv_init(&(devi->devi_ct_cv), NULL, CV_DEFAULT, NULL);
399  	devi->devi_ct_count = -1;	/* counter not in use if -1 */
400  	list_create(&(devi->devi_ct), sizeof (cont_device_t),
401  	    offsetof(cont_device_t, cond_next));
402  	list_create(&devi->devi_unbind_cbs, sizeof (ddi_unbind_callback_t),
403  	    offsetof(ddi_unbind_callback_t, ddiub_next));
404  	mutex_init(&devi->devi_unbind_lock, NULL, MUTEX_DEFAULT, NULL);
405  
406  	i_ddi_set_node_state((dev_info_t *)devi, DS_PROTO);
407  	da_log_enter((dev_info_t *)devi);
408  	return ((dev_info_t *)devi);
409  
410  fail:
411  	if (devi->devi_sys_prop_ptr)
412  		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
413  	if (devi->devi_node_name)
414  		kmem_free(devi->devi_node_name, strlen(node_name) + 1);
415  	if (devi->devi_audit)
416  		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
417  	kmem_cache_free(ddi_node_cache, devi);
418  	cmn_err(CE_NOTE, failed);
419  	return (NULL);
420  }
421  
422  /*
423   * free a dev_info structure.
424   * NB. Not callable from interrupt since impl_ddi_free_nodeid may block.
425   */
426  void
i_ddi_free_node(dev_info_t * dip)427  i_ddi_free_node(dev_info_t *dip)
428  {
429  	struct dev_info *devi = DEVI(dip);
430  	struct devi_nodeid *elem;
431  
432  	ASSERT(devi->devi_ref == 0);
433  	ASSERT(devi->devi_addr == NULL);
434  	ASSERT(devi->devi_node_state == DS_PROTO);
435  	ASSERT(devi->devi_child == NULL);
436  	ASSERT(devi->devi_hp_hdlp == NULL);
437  
438  	/* free devi_addr_buf allocated by ddi_set_name_addr() */
439  	if (devi->devi_addr_buf)
440  		kmem_free(devi->devi_addr_buf, 2 * MAXNAMELEN);
441  
442  	if (i_ndi_dev_is_auto_assigned_node(dip))
443  		impl_ddi_free_nodeid(DEVI(dip)->devi_nodeid);
444  
445  	if (ndi_dev_is_persistent_node(dip)) {
446  		mutex_enter(&devimap->dno_lock);
447  		ASSERT(devimap->dno_free);
448  		elem = devimap->dno_free;
449  		devimap->dno_free = elem->next;
450  		mutex_exit(&devimap->dno_lock);
451  		kmem_free(elem, sizeof (*elem));
452  	}
453  
454  	if (DEVI(dip)->devi_compat_names)
455  		kmem_free(DEVI(dip)->devi_compat_names,
456  		    DEVI(dip)->devi_compat_length);
457  	if (DEVI(dip)->devi_rebinding_name)
458  		kmem_free(DEVI(dip)->devi_rebinding_name,
459  		    strlen(DEVI(dip)->devi_rebinding_name) + 1);
460  
461  	ddi_prop_remove_all(dip);	/* remove driver properties */
462  	if (devi->devi_sys_prop_ptr)
463  		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
464  	if (devi->devi_hw_prop_ptr)
465  		i_ddi_prop_list_delete(devi->devi_hw_prop_ptr);
466  
467  	if (DEVI(dip)->devi_devid_str)
468  		ddi_devid_str_free(DEVI(dip)->devi_devid_str);
469  
470  	i_ddi_set_node_state(dip, DS_INVAL);
471  	da_log_enter(dip);
472  	if (devi->devi_audit) {
473  		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
474  	}
475  	if (devi->devi_device_class)
476  		kmem_free(devi->devi_device_class,
477  		    strlen(devi->devi_device_class) + 1);
478  	cv_destroy(&(devi->devi_cv));
479  	mutex_destroy(&(devi->devi_lock));
480  	mutex_destroy(&(devi->devi_pm_lock));
481  	mutex_destroy(&(devi->devi_pm_busy_lock));
482  
483  	RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroying contract fields: "
484  	    "dip=%p", (void *)dip));
485  	contract_device_remove_dip(dip);
486  	ASSERT(devi->devi_ct_count == -1);
487  	ASSERT(list_is_empty(&(devi->devi_ct)));
488  	cv_destroy(&(devi->devi_ct_cv));
489  	list_destroy(&(devi->devi_ct));
490  	/* free this last since contract_device_remove_dip() uses it */
491  	mutex_destroy(&(devi->devi_ct_lock));
492  	RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroyed all contract fields: "
493  	    "dip=%p, name=%s", (void *)dip, devi->devi_node_name));
494  
495  	kmem_free(devi->devi_node_name, strlen(devi->devi_node_name) + 1);
496  
497  	/* free event data */
498  	if (devi->devi_ev_path)
499  		kmem_free(devi->devi_ev_path, MAXPATHLEN);
500  
501  	mutex_destroy(&devi->devi_unbind_lock);
502  	list_destroy(&devi->devi_unbind_cbs);
503  
504  	kmem_cache_free(ddi_node_cache, devi);
505  }
506  
507  
508  /*
509   * Node state transitions
510   */
511  
512  /*
513   * Change the node name
514   */
515  int
ndi_devi_set_nodename(dev_info_t * dip,char * name,int flags)516  ndi_devi_set_nodename(dev_info_t *dip, char *name, int flags)
517  {
518  	_NOTE(ARGUNUSED(flags))
519  	char *nname, *oname;
520  
521  	ASSERT(dip && name);
522  
523  	oname = DEVI(dip)->devi_node_name;
524  	if (strcmp(oname, name) == 0)
525  		return (DDI_SUCCESS);
526  
527  	/*
528  	 * pcicfg_fix_ethernet requires a name change after node
529  	 * is linked into the tree. When pcicfg is fixed, we
530  	 * should only allow name change in DS_PROTO state.
531  	 */
532  	if (i_ddi_node_state(dip) >= DS_BOUND) {
533  		/*
534  		 * Don't allow name change once node is bound
535  		 */
536  		cmn_err(CE_NOTE,
537  		    "ndi_devi_set_nodename: node already bound dip = %p,"
538  		    " %s -> %s", (void *)dip, ddi_node_name(dip), name);
539  		return (NDI_FAILURE);
540  	}
541  
542  	nname = i_ddi_strdup(name, KM_SLEEP);
543  	DEVI(dip)->devi_node_name = nname;
544  	i_ddi_set_binding_name(dip, nname);
545  	kmem_free(oname, strlen(oname) + 1);
546  
547  	da_log_enter(dip);
548  	return (NDI_SUCCESS);
549  }
550  
551  void
i_ddi_add_devimap(dev_info_t * dip)552  i_ddi_add_devimap(dev_info_t *dip)
553  {
554  	struct devi_nodeid *elem;
555  
556  	ASSERT(dip);
557  
558  	if (!ndi_dev_is_persistent_node(dip))
559  		return;
560  
561  	ASSERT(ddi_get_parent(dip) == NULL || (DEVI_VHCI_NODE(dip)) ||
562  	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
563  
564  	mutex_enter(&devimap->dno_lock);
565  
566  	ASSERT(devimap->dno_free);
567  
568  	elem = devimap->dno_free;
569  	devimap->dno_free = elem->next;
570  
571  	elem->nodeid = ddi_get_nodeid(dip);
572  	elem->dip = dip;
573  	elem->next = devimap->dno_head;
574  	devimap->dno_head = elem;
575  
576  	devimap->dno_list_length++;
577  
578  	mutex_exit(&devimap->dno_lock);
579  }
580  
581  static int
i_ddi_remove_devimap(dev_info_t * dip)582  i_ddi_remove_devimap(dev_info_t *dip)
583  {
584  	struct devi_nodeid *prev, *elem;
585  	static const char *fcn = "i_ddi_remove_devimap";
586  
587  	ASSERT(dip);
588  
589  	if (!ndi_dev_is_persistent_node(dip))
590  		return (DDI_SUCCESS);
591  
592  	mutex_enter(&devimap->dno_lock);
593  
594  	/*
595  	 * The following check is done with dno_lock held
596  	 * to prevent race between dip removal and
597  	 * e_ddi_prom_node_to_dip()
598  	 */
599  	if (e_ddi_devi_holdcnt(dip)) {
600  		mutex_exit(&devimap->dno_lock);
601  		return (DDI_FAILURE);
602  	}
603  
604  	ASSERT(devimap->dno_head);
605  	ASSERT(devimap->dno_list_length > 0);
606  
607  	prev = NULL;
608  	for (elem = devimap->dno_head; elem; elem = elem->next) {
609  		if (elem->dip == dip) {
610  			ASSERT(elem->nodeid == ddi_get_nodeid(dip));
611  			break;
612  		}
613  		prev = elem;
614  	}
615  
616  	if (elem && prev)
617  		prev->next = elem->next;
618  	else if (elem)
619  		devimap->dno_head = elem->next;
620  	else
621  		panic("%s: devinfo node(%p) not found",
622  		    fcn, (void *)dip);
623  
624  	devimap->dno_list_length--;
625  
626  	elem->nodeid = 0;
627  	elem->dip = NULL;
628  
629  	elem->next = devimap->dno_free;
630  	devimap->dno_free = elem;
631  
632  	mutex_exit(&devimap->dno_lock);
633  
634  	return (DDI_SUCCESS);
635  }
636  
637  /*
638   * Link this node into the devinfo tree and add to orphan list
639   * Not callable from interrupt context
640   */
641  static void
link_node(dev_info_t * dip)642  link_node(dev_info_t *dip)
643  {
644  	struct dev_info *devi = DEVI(dip);
645  	struct dev_info *parent = devi->devi_parent;
646  	dev_info_t **dipp;
647  
648  	ASSERT(parent);	/* never called for root node */
649  
650  	NDI_CONFIG_DEBUG((CE_CONT, "link_node: parent = %s child = %s\n",
651  	    parent->devi_node_name, devi->devi_node_name));
652  
653  	/*
654  	 * Hold the global_vhci_lock before linking any direct
655  	 * children of rootnex driver. This special lock protects
656  	 * linking and unlinking for rootnext direct children.
657  	 */
658  	if ((dev_info_t *)parent == ddi_root_node())
659  		mutex_enter(&global_vhci_lock);
660  
661  	/*
662  	 * attach the node to end of the list unless the node is already there
663  	 */
664  	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
665  	while (*dipp && (*dipp != dip)) {
666  		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
667  	}
668  	ASSERT(*dipp == NULL);	/* node is not linked */
669  
670  	/*
671  	 * Now that we are in the tree, update the devi-nodeid map.
672  	 */
673  	i_ddi_add_devimap(dip);
674  
675  	/*
676  	 * This is a temporary workaround for Bug 4618861.
677  	 * We keep the scsi_vhci nexus node on the left side of the devinfo
678  	 * tree (under the root nexus driver), so that virtual nodes under
679  	 * scsi_vhci will be SUSPENDed first and RESUMEd last.	This ensures
680  	 * that the pHCI nodes are active during times when their clients
681  	 * may be depending on them.  This workaround embodies the knowledge
682  	 * that system PM and CPR both traverse the tree left-to-right during
683  	 * SUSPEND and right-to-left during RESUME.
684  	 * Extending the workaround to IB Nexus/VHCI
685  	 * driver also.
686  	 */
687  	if (strcmp(devi->devi_binding_name, "scsi_vhci") == 0) {
688  		/* Add scsi_vhci to beginning of list */
689  		ASSERT((dev_info_t *)parent == top_devinfo);
690  		/* scsi_vhci under rootnex */
691  		devi->devi_sibling = parent->devi_child;
692  		parent->devi_child = devi;
693  	} else if (strcmp(devi->devi_binding_name, "ib") == 0) {
694  		i_link_vhci_node(dip);
695  	} else {
696  		/* Add to end of list */
697  		*dipp = dip;
698  		DEVI(dip)->devi_sibling = NULL;
699  	}
700  
701  	/*
702  	 * Release the global_vhci_lock before linking any direct
703  	 * children of rootnex driver.
704  	 */
705  	if ((dev_info_t *)parent == ddi_root_node())
706  		mutex_exit(&global_vhci_lock);
707  
708  	/* persistent nodes go on orphan list */
709  	if (ndi_dev_is_persistent_node(dip))
710  		add_to_dn_list(&orphanlist, dip);
711  }
712  
713  /*
714   * Unlink this node from the devinfo tree
715   */
716  static int
unlink_node(dev_info_t * dip)717  unlink_node(dev_info_t *dip)
718  {
719  	struct dev_info *devi = DEVI(dip);
720  	struct dev_info *parent = devi->devi_parent;
721  	dev_info_t **dipp;
722  	ddi_hp_cn_handle_t *hdlp;
723  
724  	ASSERT(parent != NULL);
725  	ASSERT(devi->devi_node_state == DS_LINKED);
726  
727  	NDI_CONFIG_DEBUG((CE_CONT, "unlink_node: name = %s\n",
728  	    ddi_node_name(dip)));
729  
730  	/* check references */
731  	if (devi->devi_ref || i_ddi_remove_devimap(dip) != DDI_SUCCESS)
732  		return (DDI_FAILURE);
733  
734  	/*
735  	 * Hold the global_vhci_lock before linking any direct
736  	 * children of rootnex driver.
737  	 */
738  	if ((dev_info_t *)parent == ddi_root_node())
739  		mutex_enter(&global_vhci_lock);
740  
741  	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
742  	while (*dipp && (*dipp != dip)) {
743  		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
744  	}
745  	if (*dipp) {
746  		*dipp = (dev_info_t *)(devi->devi_sibling);
747  		devi->devi_sibling = NULL;
748  	} else {
749  		NDI_CONFIG_DEBUG((CE_NOTE, "unlink_node: %s not linked",
750  		    devi->devi_node_name));
751  	}
752  
753  	/*
754  	 * Release the global_vhci_lock before linking any direct
755  	 * children of rootnex driver.
756  	 */
757  	if ((dev_info_t *)parent == ddi_root_node())
758  		mutex_exit(&global_vhci_lock);
759  
760  	/* Remove node from orphan list */
761  	if (ndi_dev_is_persistent_node(dip)) {
762  		remove_from_dn_list(&orphanlist, dip);
763  	}
764  
765  	/* Update parent's hotplug handle list */
766  	for (hdlp = DEVI(parent)->devi_hp_hdlp; hdlp; hdlp = hdlp->next) {
767  		if (hdlp->cn_info.cn_child == dip)
768  			hdlp->cn_info.cn_child = NULL;
769  	}
770  	return (DDI_SUCCESS);
771  }
772  
773  /*
774   * Bind this devinfo node to a driver. If compat is NON-NULL, try that first.
775   * Else, use the node-name.
776   *
777   * NOTE: IEEE1275 specifies that nodename should be tried before compatible.
778   *	Solaris implementation binds nodename after compatible.
779   *
780   * If we find a binding,
781   * - set the binding name to the string,
782   * - set major number to driver major
783   *
784   * If we don't find a binding,
785   * - return failure
786   */
787  static int
bind_node(dev_info_t * dip)788  bind_node(dev_info_t *dip)
789  {
790  	char *p = NULL;
791  	major_t major = DDI_MAJOR_T_NONE;
792  	struct dev_info *devi = DEVI(dip);
793  	dev_info_t *parent = ddi_get_parent(dip);
794  
795  	ASSERT(devi->devi_node_state == DS_LINKED);
796  
797  	NDI_CONFIG_DEBUG((CE_CONT, "bind_node: 0x%p(name = %s)\n",
798  	    (void *)dip, ddi_node_name(dip)));
799  
800  	mutex_enter(&DEVI(dip)->devi_lock);
801  	if (DEVI(dip)->devi_flags & DEVI_NO_BIND) {
802  		mutex_exit(&DEVI(dip)->devi_lock);
803  		return (DDI_FAILURE);
804  	}
805  	mutex_exit(&DEVI(dip)->devi_lock);
806  
807  	/* find the driver with most specific binding using compatible */
808  	major = ddi_compatible_driver_major(dip, &p);
809  	if (major == DDI_MAJOR_T_NONE)
810  		return (DDI_FAILURE);
811  
812  	devi->devi_major = major;
813  	if (p != NULL) {
814  		i_ddi_set_binding_name(dip, p);
815  		NDI_CONFIG_DEBUG((CE_CONT, "bind_node: %s bound to %s\n",
816  		    devi->devi_node_name, p));
817  	}
818  
819  	/* Link node to per-driver list */
820  	link_to_driver_list(dip);
821  
822  	/*
823  	 * reset parent flag so that nexus will merge .conf props
824  	 */
825  	if (ndi_dev_is_persistent_node(dip)) {
826  		mutex_enter(&DEVI(parent)->devi_lock);
827  		DEVI(parent)->devi_flags &=
828  		    ~(DEVI_ATTACHED_CHILDREN|DEVI_MADE_CHILDREN);
829  		mutex_exit(&DEVI(parent)->devi_lock);
830  	}
831  	return (DDI_SUCCESS);
832  }
833  
834  /*
835   * Unbind this devinfo node
836   * Called before the node is destroyed or driver is removed from system
837   */
838  static int
unbind_node(dev_info_t * dip)839  unbind_node(dev_info_t *dip)
840  {
841  	ddi_unbind_callback_t *cb;
842  	ASSERT(DEVI(dip)->devi_node_state == DS_BOUND);
843  	ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE);
844  
845  	/* check references */
846  	if (DEVI(dip)->devi_ref)
847  		return (DDI_FAILURE);
848  
849  	NDI_CONFIG_DEBUG((CE_CONT, "unbind_node: 0x%p(name = %s)\n",
850  	    (void *)dip, ddi_node_name(dip)));
851  
852  	unlink_from_driver_list(dip);
853  
854  	DEVI(dip)->devi_major = DDI_MAJOR_T_NONE;
855  	DEVI(dip)->devi_binding_name = DEVI(dip)->devi_node_name;
856  
857  	while ((cb = list_remove_head(&DEVI(dip)->devi_unbind_cbs)) != NULL) {
858  		cb->ddiub_cb(cb->ddiub_arg, dip);
859  	}
860  
861  	return (DDI_SUCCESS);
862  }
863  
864  /*
865   * Initialize a node: calls the parent nexus' bus_ctl ops to do the operation.
866   * Must hold parent and per-driver list while calling this function.
867   * A successful init_node() returns with an active ndi_hold_devi() hold on
868   * the parent.
869   */
870  static int
init_node(dev_info_t * dip)871  init_node(dev_info_t *dip)
872  {
873  	int error;
874  	dev_info_t *pdip = ddi_get_parent(dip);
875  	int (*f)(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
876  	char *path;
877  	major_t	major;
878  	ddi_devid_t devid = NULL;
879  
880  	ASSERT(i_ddi_node_state(dip) == DS_BOUND);
881  
882  	/* should be DS_READY except for pcmcia ... */
883  	ASSERT(i_ddi_node_state(pdip) >= DS_PROBED);
884  
885  	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
886  	(void) ddi_pathname(dip, path);
887  	NDI_CONFIG_DEBUG((CE_CONT, "init_node: entry: path %s 0x%p\n",
888  	    path, (void *)dip));
889  
890  	/*
891  	 * The parent must have a bus_ctl operation.
892  	 */
893  	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
894  	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_ctl) == NULL) {
895  		error = DDI_FAILURE;
896  		goto out;
897  	}
898  
899  	add_global_props(dip);
900  
901  	/*
902  	 * Invoke the parent's bus_ctl operation with the DDI_CTLOPS_INITCHILD
903  	 * command to transform the child to canonical form 1. If there
904  	 * is an error, ddi_remove_child should be called, to clean up.
905  	 */
906  	error = (*f)(pdip, pdip, DDI_CTLOPS_INITCHILD, dip, NULL);
907  	if (error != DDI_SUCCESS) {
908  		NDI_CONFIG_DEBUG((CE_CONT, "init_node: %s 0x%p failed\n",
909  		    path, (void *)dip));
910  		remove_global_props(dip);
911  
912  		/*
913  		 * If a nexus INITCHILD implementation calls ddi_devid_regster()
914  		 * prior to setting devi_addr, the devid is not recorded in
915  		 * the devid cache (i.e. DEVI_CACHED_DEVID is not set).
916  		 * With mpxio, while the vhci client path may be missing
917  		 * from the cache, phci pathinfo paths may have already be
918  		 * added to the cache, against the client dip, by use of
919  		 * e_devid_cache_pathinfo().  Because of this, when INITCHILD
920  		 * of the client fails, we need to purge the client dip from
921  		 * the cache even if DEVI_CACHED_DEVID is not set - if only
922  		 * devi_devid_str is set.
923  		 */
924  		mutex_enter(&DEVI(dip)->devi_lock);
925  		if ((DEVI(dip)->devi_flags & DEVI_CACHED_DEVID) ||
926  		    DEVI(dip)->devi_devid_str) {
927  			DEVI(dip)->devi_flags &= ~DEVI_CACHED_DEVID;
928  			mutex_exit(&DEVI(dip)->devi_lock);
929  			ddi_devid_unregister(dip);
930  		} else
931  			mutex_exit(&DEVI(dip)->devi_lock);
932  
933  		/* in case nexus driver didn't clear this field */
934  		ddi_set_name_addr(dip, NULL);
935  		error = DDI_FAILURE;
936  		goto out;
937  	}
938  
939  	ndi_hold_devi(pdip);			/* initial hold of parent */
940  
941  	/* recompute path after initchild for @addr information */
942  	(void) ddi_pathname(dip, path);
943  
944  	/* Check for duplicate nodes */
945  	if (find_duplicate_child(pdip, dip) != NULL) {
946  		/*
947  		 * uninit_node() the duplicate - a successful uninit_node()
948  		 * will release inital hold of parent using ndi_rele_devi().
949  		 */
950  		if ((error = uninit_node(dip)) != DDI_SUCCESS) {
951  			ndi_rele_devi(pdip);	/* release initial hold */
952  			cmn_err(CE_WARN, "init_node: uninit of duplicate "
953  			    "node %s failed", path);
954  		}
955  		NDI_CONFIG_DEBUG((CE_CONT, "init_node: duplicate uninit "
956  		    "%s 0x%p%s\n", path, (void *)dip,
957  		    (error == DDI_SUCCESS) ? "" : " failed"));
958  		error = DDI_FAILURE;
959  		goto out;
960  	}
961  
962  	/*
963  	 * If a devid was registered for a DS_BOUND node then the devid_cache
964  	 * may not have captured the path. Detect this situation and ensure that
965  	 * the path enters the cache now that devi_addr is established.
966  	 */
967  	if (!(DEVI(dip)->devi_flags & DEVI_CACHED_DEVID) &&
968  	    (ddi_devid_get(dip, &devid) == DDI_SUCCESS)) {
969  		if (e_devid_cache_register(dip, devid) == DDI_SUCCESS) {
970  			mutex_enter(&DEVI(dip)->devi_lock);
971  			DEVI(dip)->devi_flags |= DEVI_CACHED_DEVID;
972  			mutex_exit(&DEVI(dip)->devi_lock);
973  		}
974  
975  		ddi_devid_free(devid);
976  	}
977  
978  	/*
979  	 * Check to see if we have a path-oriented driver alias that overrides
980  	 * the current driver binding. If so, we need to rebind. This check
981  	 * needs to be delayed until after a successful DDI_CTLOPS_INITCHILD,
982  	 * so the unit-address is established on the last component of the path.
983  	 *
984  	 * NOTE: Allowing a path-oriented alias to change the driver binding
985  	 * of a driver.conf node results in non-intuitive property behavior.
986  	 * We provide a tunable (driver_conf_allow_path_alias) to control
987  	 * this behavior. See uninit_node() for more details.
988  	 *
989  	 * NOTE: If you are adding a path-oriented alias for the boot device,
990  	 * and there is mismatch between OBP and the kernel in regard to
991  	 * generic name use, like "disk" .vs. "ssd", then you will need
992  	 * to add a path-oriented alias for both paths.
993  	 */
994  	major = ddi_name_to_major(path);
995  	if (driver_active(major) && (major != DEVI(dip)->devi_major) &&
996  	    (ndi_dev_is_persistent_node(dip) || driver_conf_allow_path_alias)) {
997  
998  		/* Mark node for rebind processing. */
999  		mutex_enter(&DEVI(dip)->devi_lock);
1000  		DEVI(dip)->devi_flags |= DEVI_REBIND;
1001  		mutex_exit(&DEVI(dip)->devi_lock);
1002  
1003  		/*
1004  		 * Add an extra hold on the parent to prevent it from ever
1005  		 * having a zero devi_ref during the child rebind process.
1006  		 * This is necessary to ensure that the parent will never
1007  		 * detach(9E) during the rebind.
1008  		 */
1009  		ndi_hold_devi(pdip);		/* extra hold of parent */
1010  
1011  		/*
1012  		 * uninit_node() current binding - a successful uninit_node()
1013  		 * will release extra hold of parent using ndi_rele_devi().
1014  		 */
1015  		if ((error = uninit_node(dip)) != DDI_SUCCESS) {
1016  			ndi_rele_devi(pdip);	/* release extra hold */
1017  			ndi_rele_devi(pdip);	/* release initial hold */
1018  			cmn_err(CE_WARN, "init_node: uninit for rebind "
1019  			    "of node %s failed", path);
1020  			goto out;
1021  		}
1022  
1023  		/* Unbind: demote the node back to DS_LINKED.  */
1024  		if ((error = ndi_devi_unbind_driver(dip)) != DDI_SUCCESS) {
1025  			ndi_rele_devi(pdip);	/* release initial hold */
1026  			cmn_err(CE_WARN, "init_node: unbind for rebind "
1027  			    "of node %s failed", path);
1028  			goto out;
1029  		}
1030  
1031  		/* establish rebinding name */
1032  		if (DEVI(dip)->devi_rebinding_name == NULL)
1033  			DEVI(dip)->devi_rebinding_name =
1034  			    i_ddi_strdup(path, KM_SLEEP);
1035  
1036  		/*
1037  		 * Now that we are demoted and marked for rebind, repromote.
1038  		 * We need to do this in steps, instead of just calling
1039  		 * ddi_initchild, so that we can redo the merge operation
1040  		 * after we are rebound to the path-bound driver.
1041  		 *
1042  		 * Start by rebinding node to the path-bound driver.
1043  		 */
1044  		if ((error = ndi_devi_bind_driver(dip, 0)) != DDI_SUCCESS) {
1045  			ndi_rele_devi(pdip);	/* release initial hold */
1046  			cmn_err(CE_WARN, "init_node: rebind "
1047  			    "of node %s failed", path);
1048  			goto out;
1049  		}
1050  
1051  		/*
1052  		 * If the node is not a driver.conf node then merge
1053  		 * driver.conf properties from new path-bound driver.conf.
1054  		 */
1055  		if (ndi_dev_is_persistent_node(dip))
1056  			(void) i_ndi_make_spec_children(pdip, 0);
1057  
1058  		/*
1059  		 * Now that we have taken care of merge, repromote back
1060  		 * to DS_INITIALIZED.
1061  		 */
1062  		error = ddi_initchild(pdip, dip);
1063  		NDI_CONFIG_DEBUG((CE_CONT, "init_node: rebind "
1064  		    "%s 0x%p\n", path, (void *)dip));
1065  
1066  		/*
1067  		 * Release our initial hold. If ddi_initchild() was
1068  		 * successful then it will return with the active hold.
1069  		 */
1070  		ndi_rele_devi(pdip);
1071  		goto out;
1072  	}
1073  
1074  	/*
1075  	 * Apply multi-parent/deep-nexus optimization to the new node
1076  	 */
1077  	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
1078  	ddi_optimize_dtree(dip);
1079  	error = DDI_SUCCESS;		/* return with active hold */
1080  
1081  out:	if (error != DDI_SUCCESS) {
1082  		/* On failure ensure that DEVI_REBIND is cleared */
1083  		mutex_enter(&DEVI(dip)->devi_lock);
1084  		DEVI(dip)->devi_flags &= ~DEVI_REBIND;
1085  		mutex_exit(&DEVI(dip)->devi_lock);
1086  	}
1087  	kmem_free(path, MAXPATHLEN);
1088  	return (error);
1089  }
1090  
1091  /*
1092   * Uninitialize node
1093   * The per-driver list must be held busy during the call.
1094   * A successful uninit_node() releases the init_node() hold on
1095   * the parent by calling ndi_rele_devi().
1096   */
1097  static int
uninit_node(dev_info_t * dip)1098  uninit_node(dev_info_t *dip)
1099  {
1100  	int node_state_entry;
1101  	dev_info_t *pdip;
1102  	struct dev_ops *ops;
1103  	int (*f)();
1104  	int error;
1105  	char *addr;
1106  
1107  	/*
1108  	 * Don't check for references here or else a ref-counted
1109  	 * dip cannot be downgraded by the framework.
1110  	 */
1111  	node_state_entry = i_ddi_node_state(dip);
1112  	ASSERT((node_state_entry == DS_BOUND) ||
1113  	    (node_state_entry == DS_INITIALIZED));
1114  	pdip = ddi_get_parent(dip);
1115  	ASSERT(pdip);
1116  
1117  	NDI_CONFIG_DEBUG((CE_CONT, "uninit_node: 0x%p(%s%d)\n",
1118  	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1119  
1120  	if (((ops = ddi_get_driver(pdip)) == NULL) ||
1121  	    (ops->devo_bus_ops == NULL) ||
1122  	    ((f = ops->devo_bus_ops->bus_ctl) == NULL)) {
1123  		return (DDI_FAILURE);
1124  	}
1125  
1126  	/*
1127  	 * save the @addr prior to DDI_CTLOPS_UNINITCHILD for use in
1128  	 * freeing the instance if it succeeds.
1129  	 */
1130  	if (node_state_entry == DS_INITIALIZED) {
1131  		addr = ddi_get_name_addr(dip);
1132  		if (addr)
1133  			addr = i_ddi_strdup(addr, KM_SLEEP);
1134  	} else {
1135  		addr = NULL;
1136  	}
1137  
1138  	error = (*f)(pdip, pdip, DDI_CTLOPS_UNINITCHILD, dip, (void *)NULL);
1139  	if (error == DDI_SUCCESS) {
1140  		/* ensure that devids are unregistered */
1141  		mutex_enter(&DEVI(dip)->devi_lock);
1142  		if ((DEVI(dip)->devi_flags & DEVI_CACHED_DEVID)) {
1143  			DEVI(dip)->devi_flags &= ~DEVI_CACHED_DEVID;
1144  			mutex_exit(&DEVI(dip)->devi_lock);
1145  			ddi_devid_unregister(dip);
1146  		} else
1147  			mutex_exit(&DEVI(dip)->devi_lock);
1148  
1149  		/* if uninitchild forgot to set devi_addr to NULL do it now */
1150  		ddi_set_name_addr(dip, NULL);
1151  
1152  		/*
1153  		 * Free instance number. This is a no-op if instance has
1154  		 * been kept by probe_node().  Avoid free when we are called
1155  		 * from init_node (DS_BOUND) because the instance has not yet
1156  		 * been assigned.
1157  		 */
1158  		if (node_state_entry == DS_INITIALIZED) {
1159  			e_ddi_free_instance(dip, addr);
1160  			DEVI(dip)->devi_instance = -1;
1161  		}
1162  
1163  		/* release the init_node hold */
1164  		ndi_rele_devi(pdip);
1165  
1166  		remove_global_props(dip);
1167  
1168  		/*
1169  		 * NOTE: The decision on whether to allow a path-oriented
1170  		 * rebind of a driver.conf enumerated node is made by
1171  		 * init_node() based on driver_conf_allow_path_alias. The
1172  		 * rebind code below prevents deletion of system properties
1173  		 * on driver.conf nodes.
1174  		 *
1175  		 * When driver_conf_allow_path_alias is set, property behavior
1176  		 * on rebound driver.conf file is non-intuitive. For a
1177  		 * driver.conf node, the unit-address properties come from
1178  		 * the driver.conf file as system properties. Removing system
1179  		 * properties from a driver.conf node makes the node
1180  		 * useless (we get node without unit-address properties) - so
1181  		 * we leave system properties in place. The result is a node
1182  		 * where system properties come from the node being rebound,
1183  		 * and global properties come from the driver.conf file
1184  		 * of the driver we are rebinding to.  If we could determine
1185  		 * that the path-oriented alias driver.conf file defined a
1186  		 * node at the same unit address, it would be best to use
1187  		 * that node and avoid the non-intuitive property behavior.
1188  		 * Unfortunately, the current "merge" code does not support
1189  		 * this, so we live with the non-intuitive property behavior.
1190  		 */
1191  		if (!((ndi_dev_is_persistent_node(dip) == 0) &&
1192  		    (DEVI(dip)->devi_flags & DEVI_REBIND)))
1193  			e_ddi_prop_remove_all(dip);
1194  	} else {
1195  		NDI_CONFIG_DEBUG((CE_CONT, "uninit_node failed: 0x%p(%s%d)\n",
1196  		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1197  	}
1198  
1199  	if (addr)
1200  		kmem_free(addr, strlen(addr) + 1);
1201  	return (error);
1202  }
1203  
1204  /*
1205   * Invoke driver's probe entry point to probe for existence of hardware.
1206   * Keep instance permanent for successful probe and leaf nodes.
1207   *
1208   * Per-driver list must be held busy while calling this function.
1209   */
1210  static int
probe_node(dev_info_t * dip)1211  probe_node(dev_info_t *dip)
1212  {
1213  	int rv;
1214  
1215  	ASSERT(i_ddi_node_state(dip) == DS_INITIALIZED);
1216  
1217  	NDI_CONFIG_DEBUG((CE_CONT, "probe_node: 0x%p(%s%d)\n",
1218  	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1219  
1220  	/* temporarily hold the driver while we probe */
1221  	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
1222  	if (DEVI(dip)->devi_ops == NULL) {
1223  		NDI_CONFIG_DEBUG((CE_CONT,
1224  		    "probe_node: 0x%p(%s%d) cannot load driver\n",
1225  		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1226  		return (DDI_FAILURE);
1227  	}
1228  
1229  	if (identify_9e != 0)
1230  		(void) devi_identify(dip);
1231  
1232  	rv = devi_probe(dip);
1233  
1234  	/* release the driver now that probe is complete */
1235  	ndi_rele_driver(dip);
1236  	DEVI(dip)->devi_ops = NULL;
1237  
1238  	switch (rv) {
1239  	case DDI_PROBE_SUCCESS:			/* found */
1240  	case DDI_PROBE_DONTCARE:		/* ddi_dev_is_sid */
1241  		e_ddi_keep_instance(dip);	/* persist instance */
1242  		rv = DDI_SUCCESS;
1243  		break;
1244  
1245  	case DDI_PROBE_PARTIAL:			/* maybe later */
1246  	case DDI_PROBE_FAILURE:			/* not found */
1247  		NDI_CONFIG_DEBUG((CE_CONT,
1248  		    "probe_node: 0x%p(%s%d) no hardware found%s\n",
1249  		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip),
1250  		    (rv == DDI_PROBE_PARTIAL) ? " yet" : ""));
1251  		rv = DDI_FAILURE;
1252  		break;
1253  
1254  	default:
1255  #ifdef	DEBUG
1256  		cmn_err(CE_WARN, "probe_node: %s%d: illegal probe(9E) value",
1257  		    ddi_driver_name(dip), ddi_get_instance(dip));
1258  #endif	/* DEBUG */
1259  		rv = DDI_FAILURE;
1260  		break;
1261  	}
1262  	return (rv);
1263  }
1264  
1265  /*
1266   * Unprobe a node. Simply reset the node state.
1267   * Per-driver list must be held busy while calling this function.
1268   */
1269  static int
unprobe_node(dev_info_t * dip)1270  unprobe_node(dev_info_t *dip)
1271  {
1272  	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
1273  
1274  	/*
1275  	 * Don't check for references here or else a ref-counted
1276  	 * dip cannot be downgraded by the framework.
1277  	 */
1278  
1279  	NDI_CONFIG_DEBUG((CE_CONT, "unprobe_node: 0x%p(name = %s)\n",
1280  	    (void *)dip, ddi_node_name(dip)));
1281  	return (DDI_SUCCESS);
1282  }
1283  
1284  /*
1285   * Attach devinfo node.
1286   * Per-driver list must be held busy.
1287   */
1288  static int
attach_node(dev_info_t * dip)1289  attach_node(dev_info_t *dip)
1290  {
1291  	int rv;
1292  
1293  	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
1294  	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
1295  
1296  	NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d)\n",
1297  	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1298  
1299  	/*
1300  	 * Tell mpxio framework that a node is about to online.
1301  	 */
1302  	if ((rv = mdi_devi_online(dip, 0)) != NDI_SUCCESS) {
1303  		return (DDI_FAILURE);
1304  	}
1305  
1306  	/* no recursive attachment */
1307  	ASSERT(DEVI(dip)->devi_ops == NULL);
1308  
1309  	/*
1310  	 * Hold driver the node is bound to.
1311  	 */
1312  	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
1313  	if (DEVI(dip)->devi_ops == NULL) {
1314  		/*
1315  		 * We were able to load driver for probing, so we should
1316  		 * not get here unless something really bad happened.
1317  		 */
1318  		cmn_err(CE_WARN, "attach_node: no driver for major %d",
1319  		    DEVI(dip)->devi_major);
1320  		return (DDI_FAILURE);
1321  	}
1322  
1323  	if (NEXUS_DRV(DEVI(dip)->devi_ops))
1324  		DEVI(dip)->devi_taskq = ddi_taskq_create(dip,
1325  		    "nexus_enum_tq", 1,
1326  		    TASKQ_DEFAULTPRI, 0);
1327  
1328  	mutex_enter(&(DEVI(dip)->devi_lock));
1329  	DEVI_SET_ATTACHING(dip);
1330  	DEVI_SET_NEED_RESET(dip);
1331  	mutex_exit(&(DEVI(dip)->devi_lock));
1332  
1333  	rv = devi_attach(dip, DDI_ATTACH);
1334  
1335  	mutex_enter(&(DEVI(dip)->devi_lock));
1336  	DEVI_CLR_ATTACHING(dip);
1337  
1338  	if (rv != DDI_SUCCESS) {
1339  		DEVI_CLR_NEED_RESET(dip);
1340  		mutex_exit(&DEVI(dip)->devi_lock);
1341  
1342  		/*
1343  		 * Cleanup dacf reservations
1344  		 */
1345  		mutex_enter(&dacf_lock);
1346  		dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
1347  		dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
1348  		mutex_exit(&dacf_lock);
1349  		if (DEVI(dip)->devi_taskq)
1350  			ddi_taskq_destroy(DEVI(dip)->devi_taskq);
1351  		ddi_remove_minor_node(dip, NULL);
1352  
1353  		/* release the driver if attach failed */
1354  		ndi_rele_driver(dip);
1355  		DEVI(dip)->devi_ops = NULL;
1356  		NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d) failed\n",
1357  		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1358  		return (DDI_FAILURE);
1359  	} else
1360  		mutex_exit(&DEVI(dip)->devi_lock);
1361  
1362  	/* successful attach, return with driver held */
1363  
1364  	return (DDI_SUCCESS);
1365  }
1366  
1367  /*
1368   * Detach devinfo node.
1369   * Per-driver list must be held busy.
1370   */
1371  static int
detach_node(dev_info_t * dip,uint_t flag)1372  detach_node(dev_info_t *dip, uint_t flag)
1373  {
1374  	struct devnames	*dnp;
1375  	int		rv;
1376  
1377  	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
1378  	ASSERT(i_ddi_node_state(dip) == DS_ATTACHED);
1379  
1380  	/* check references */
1381  	if (DEVI(dip)->devi_ref)
1382  		return (DDI_FAILURE);
1383  
1384  	NDI_CONFIG_DEBUG((CE_CONT, "detach_node: 0x%p(%s%d)\n",
1385  	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1386  
1387  	/*
1388  	 * NOTE: If we are processing a pHCI node then the calling code
1389  	 * must detect this and ndi_devi_enter() in (vHCI, parent(pHCI))
1390  	 * order unless pHCI and vHCI are siblings.  Code paths leading
1391  	 * here that must ensure this ordering include:
1392  	 * unconfig_immediate_children(), devi_unconfig_one(),
1393  	 * ndi_devi_unconfig_one(), ndi_devi_offline().
1394  	 */
1395  	ASSERT(!MDI_PHCI(dip) ||
1396  	    (ddi_get_parent(mdi_devi_get_vdip(dip)) == ddi_get_parent(dip)) ||
1397  	    DEVI_BUSY_OWNED(mdi_devi_get_vdip(dip)));
1398  
1399  	/* Offline the device node with the mpxio framework. */
1400  	if (mdi_devi_offline(dip, flag) != NDI_SUCCESS) {
1401  		return (DDI_FAILURE);
1402  	}
1403  
1404  	/* drain the taskq */
1405  	if (DEVI(dip)->devi_taskq)
1406  		ddi_taskq_wait(DEVI(dip)->devi_taskq);
1407  
1408  	rv = devi_detach(dip, DDI_DETACH);
1409  
1410  	if (rv != DDI_SUCCESS) {
1411  		NDI_CONFIG_DEBUG((CE_CONT,
1412  		    "detach_node: 0x%p(%s%d) failed\n",
1413  		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1414  		return (DDI_FAILURE);
1415  	}
1416  
1417  	mutex_enter(&(DEVI(dip)->devi_lock));
1418  	DEVI_CLR_NEED_RESET(dip);
1419  	mutex_exit(&(DEVI(dip)->devi_lock));
1420  
1421  #if defined(__amd64) && !defined(__xpv)
1422  	/*
1423  	 * Close any iommulib mediated linkage to an IOMMU
1424  	 */
1425  	if (IOMMU_USED(dip))
1426  		iommulib_nex_close(dip);
1427  #endif
1428  
1429  	/* destroy the taskq */
1430  	if (DEVI(dip)->devi_taskq) {
1431  		ddi_taskq_destroy(DEVI(dip)->devi_taskq);
1432  		DEVI(dip)->devi_taskq = NULL;
1433  	}
1434  
1435  	/* Cleanup dacf reservations */
1436  	mutex_enter(&dacf_lock);
1437  	dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
1438  	dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
1439  	mutex_exit(&dacf_lock);
1440  
1441  	/* remove any additional flavors that were added */
1442  	if (DEVI(dip)->devi_flavorv_n > 1 && DEVI(dip)->devi_flavorv != NULL) {
1443  		kmem_free(DEVI(dip)->devi_flavorv,
1444  		    (DEVI(dip)->devi_flavorv_n - 1) * sizeof (void *));
1445  		DEVI(dip)->devi_flavorv = NULL;
1446  	}
1447  
1448  	/* Remove properties and minor nodes in case driver forgots */
1449  	ddi_remove_minor_node(dip, NULL);
1450  	ddi_prop_remove_all(dip);
1451  
1452  	/* a detached node can't have attached or .conf children */
1453  	mutex_enter(&DEVI(dip)->devi_lock);
1454  	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN|DEVI_ATTACHED_CHILDREN);
1455  	mutex_exit(&DEVI(dip)->devi_lock);
1456  
1457  	/*
1458  	 * If the instance has successfully detached in detach_driver() context,
1459  	 * clear DN_DRIVER_HELD for correct ddi_hold_installed_driver()
1460  	 * behavior. Consumers like qassociate() depend on this (via clnopen()).
1461  	 */
1462  	if (flag & NDI_DETACH_DRIVER) {
1463  		dnp = &(devnamesp[DEVI(dip)->devi_major]);
1464  		LOCK_DEV_OPS(&dnp->dn_lock);
1465  		dnp->dn_flags &= ~DN_DRIVER_HELD;
1466  		UNLOCK_DEV_OPS(&dnp->dn_lock);
1467  	}
1468  
1469  	/* successful detach, release the driver */
1470  	ndi_rele_driver(dip);
1471  	DEVI(dip)->devi_ops = NULL;
1472  	return (DDI_SUCCESS);
1473  }
1474  
1475  /*
1476   * Run dacf post_attach routines
1477   */
1478  static int
postattach_node(dev_info_t * dip)1479  postattach_node(dev_info_t *dip)
1480  {
1481  	int rval;
1482  
1483  	/*
1484  	 * For hotplug busses like USB, it's possible that devices
1485  	 * are removed but dip is still around. We don't want to
1486  	 * run dacf routines as part of detach failure recovery.
1487  	 *
1488  	 * Pretend success until we figure out how to prevent
1489  	 * access to such devinfo nodes.
1490  	 */
1491  	if (DEVI_IS_DEVICE_REMOVED(dip))
1492  		return (DDI_SUCCESS);
1493  
1494  	/*
1495  	 * if dacf_postattach failed, report it to the framework
1496  	 * so that it can be retried later at the open time.
1497  	 */
1498  	mutex_enter(&dacf_lock);
1499  	rval = dacfc_postattach(dip);
1500  	mutex_exit(&dacf_lock);
1501  
1502  	/*
1503  	 * Plumbing during postattach may fail because of the
1504  	 * underlying device is not ready. This will fail ndi_devi_config()
1505  	 * in dv_filldir().
1506  	 */
1507  	if (rval != DACF_SUCCESS) {
1508  		NDI_CONFIG_DEBUG((CE_CONT, "postattach_node: %s%d (%p) "
1509  		    "postattach failed\n", ddi_driver_name(dip),
1510  		    ddi_get_instance(dip), (void *)dip));
1511  		return (DDI_FAILURE);
1512  	}
1513  
1514  	return (DDI_SUCCESS);
1515  }
1516  
1517  /*
1518   * Run dacf pre-detach routines
1519   */
1520  static int
predetach_node(dev_info_t * dip,uint_t flag)1521  predetach_node(dev_info_t *dip, uint_t flag)
1522  {
1523  	int ret;
1524  
1525  	/*
1526  	 * Don't auto-detach if DDI_FORCEATTACH or DDI_NO_AUTODETACH
1527  	 * properties are set.
1528  	 */
1529  	if (flag & NDI_AUTODETACH) {
1530  		struct devnames *dnp;
1531  		int pflag = DDI_PROP_NOTPROM | DDI_PROP_DONTPASS;
1532  
1533  		if ((ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1534  		    pflag, DDI_FORCEATTACH, 0) == 1) ||
1535  		    (ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1536  		    pflag, DDI_NO_AUTODETACH, 0) == 1))
1537  			return (DDI_FAILURE);
1538  
1539  		/* check for driver global version of DDI_NO_AUTODETACH */
1540  		dnp = &devnamesp[DEVI(dip)->devi_major];
1541  		LOCK_DEV_OPS(&dnp->dn_lock);
1542  		if (dnp->dn_flags & DN_NO_AUTODETACH) {
1543  			UNLOCK_DEV_OPS(&dnp->dn_lock);
1544  			return (DDI_FAILURE);
1545  		}
1546  		UNLOCK_DEV_OPS(&dnp->dn_lock);
1547  	}
1548  
1549  	mutex_enter(&dacf_lock);
1550  	ret = dacfc_predetach(dip);
1551  	mutex_exit(&dacf_lock);
1552  
1553  	return (ret);
1554  }
1555  
1556  /*
1557   * Wrapper for making multiple state transitions
1558   */
1559  
1560  /*
1561   * i_ndi_config_node: upgrade dev_info node into a specified state.
1562   * It is a bit tricky because the locking protocol changes before and
1563   * after a node is bound to a driver. All locks are held external to
1564   * this function.
1565   */
1566  int
i_ndi_config_node(dev_info_t * dip,ddi_node_state_t state,uint_t flag)1567  i_ndi_config_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
1568  {
1569  	_NOTE(ARGUNUSED(flag))
1570  	int rv = DDI_SUCCESS;
1571  
1572  	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
1573  
1574  	while ((i_ddi_node_state(dip) < state) && (rv == DDI_SUCCESS)) {
1575  
1576  		/* don't allow any more changes to the device tree */
1577  		if (devinfo_freeze) {
1578  			rv = DDI_FAILURE;
1579  			break;
1580  		}
1581  
1582  		switch (i_ddi_node_state(dip)) {
1583  		case DS_PROTO:
1584  			/*
1585  			 * only caller can reference this node, no external
1586  			 * locking needed.
1587  			 */
1588  			link_node(dip);
1589  			i_ddi_set_node_state(dip, DS_LINKED);
1590  			break;
1591  		case DS_LINKED:
1592  			/*
1593  			 * Three code path may attempt to bind a node:
1594  			 * - boot code
1595  			 * - add_drv
1596  			 * - hotplug thread
1597  			 * Boot code is single threaded, add_drv synchronize
1598  			 * on a userland lock, and hotplug synchronize on
1599  			 * hotplug_lk. There could be a race between add_drv
1600  			 * and hotplug thread. We'll live with this until the
1601  			 * conversion to top-down loading.
1602  			 */
1603  			if ((rv = bind_node(dip)) == DDI_SUCCESS)
1604  				i_ddi_set_node_state(dip, DS_BOUND);
1605  
1606  			break;
1607  		case DS_BOUND:
1608  			/*
1609  			 * The following transitions synchronizes on the
1610  			 * per-driver busy changing flag, since we already
1611  			 * have a driver.
1612  			 */
1613  			if ((rv = init_node(dip)) == DDI_SUCCESS)
1614  				i_ddi_set_node_state(dip, DS_INITIALIZED);
1615  			break;
1616  		case DS_INITIALIZED:
1617  			if ((rv = probe_node(dip)) == DDI_SUCCESS)
1618  				i_ddi_set_node_state(dip, DS_PROBED);
1619  			break;
1620  		case DS_PROBED:
1621  			/*
1622  			 * If node is retired and persistent, then prevent
1623  			 * attach. We can't do this for non-persistent nodes
1624  			 * as we would lose evidence that the node existed.
1625  			 */
1626  			if (i_ddi_check_retire(dip) == 1 &&
1627  			    ndi_dev_is_persistent_node(dip) &&
1628  			    retire_prevents_attach == 1) {
1629  				rv = DDI_FAILURE;
1630  				break;
1631  			}
1632  			atomic_inc_ulong(&devinfo_attach_detach);
1633  			if ((rv = attach_node(dip)) == DDI_SUCCESS)
1634  				i_ddi_set_node_state(dip, DS_ATTACHED);
1635  			atomic_dec_ulong(&devinfo_attach_detach);
1636  			break;
1637  		case DS_ATTACHED:
1638  			if ((rv = postattach_node(dip)) == DDI_SUCCESS)
1639  				i_ddi_set_node_state(dip, DS_READY);
1640  			break;
1641  		case DS_READY:
1642  			break;
1643  		default:
1644  			/* should never reach here */
1645  			ASSERT("unknown devinfo state");
1646  		}
1647  	}
1648  
1649  	if (ddidebug & DDI_AUDIT)
1650  		da_log_enter(dip);
1651  	return (rv);
1652  }
1653  
1654  /*
1655   * i_ndi_unconfig_node: downgrade dev_info node into a specified state.
1656   */
1657  int
i_ndi_unconfig_node(dev_info_t * dip,ddi_node_state_t state,uint_t flag)1658  i_ndi_unconfig_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
1659  {
1660  	int	rv = DDI_SUCCESS;
1661  
1662  	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
1663  
1664  	while ((i_ddi_node_state(dip) > state) && (rv == DDI_SUCCESS)) {
1665  
1666  		/* don't allow any more changes to the device tree */
1667  		if (devinfo_freeze) {
1668  			rv = DDI_FAILURE;
1669  			break;
1670  		}
1671  
1672  		switch (i_ddi_node_state(dip)) {
1673  		case DS_PROTO:
1674  			break;
1675  		case DS_LINKED:
1676  			/*
1677  			 * Persistent nodes are only removed by hotplug code
1678  			 * .conf nodes synchronizes on per-driver list.
1679  			 */
1680  			if ((rv = unlink_node(dip)) == DDI_SUCCESS)
1681  				i_ddi_set_node_state(dip, DS_PROTO);
1682  			break;
1683  		case DS_BOUND:
1684  			/*
1685  			 * The following transitions synchronizes on the
1686  			 * per-driver busy changing flag, since we already
1687  			 * have a driver.
1688  			 */
1689  			if ((rv = unbind_node(dip)) == DDI_SUCCESS)
1690  				i_ddi_set_node_state(dip, DS_LINKED);
1691  			break;
1692  		case DS_INITIALIZED:
1693  			if ((rv = uninit_node(dip)) == DDI_SUCCESS)
1694  				i_ddi_set_node_state(dip, DS_BOUND);
1695  			break;
1696  		case DS_PROBED:
1697  			if ((rv = unprobe_node(dip)) == DDI_SUCCESS)
1698  				i_ddi_set_node_state(dip, DS_INITIALIZED);
1699  			break;
1700  		case DS_ATTACHED:
1701  			atomic_inc_ulong(&devinfo_attach_detach);
1702  
1703  			mutex_enter(&(DEVI(dip)->devi_lock));
1704  			DEVI_SET_DETACHING(dip);
1705  			mutex_exit(&(DEVI(dip)->devi_lock));
1706  
1707  			membar_enter();	/* ensure visibility for hold_devi */
1708  
1709  			if ((rv = detach_node(dip, flag)) == DDI_SUCCESS)
1710  				i_ddi_set_node_state(dip, DS_PROBED);
1711  
1712  			mutex_enter(&(DEVI(dip)->devi_lock));
1713  			DEVI_CLR_DETACHING(dip);
1714  			mutex_exit(&(DEVI(dip)->devi_lock));
1715  
1716  			atomic_dec_ulong(&devinfo_attach_detach);
1717  			break;
1718  		case DS_READY:
1719  			if ((rv = predetach_node(dip, flag)) == DDI_SUCCESS)
1720  				i_ddi_set_node_state(dip, DS_ATTACHED);
1721  			break;
1722  		default:
1723  			ASSERT("unknown devinfo state");
1724  		}
1725  	}
1726  	da_log_enter(dip);
1727  	return (rv);
1728  }
1729  
1730  /*
1731   * ddi_initchild: transform node to DS_INITIALIZED state
1732   */
1733  int
ddi_initchild(dev_info_t * parent,dev_info_t * proto)1734  ddi_initchild(dev_info_t *parent, dev_info_t *proto)
1735  {
1736  	int ret;
1737  
1738  	ndi_devi_enter(parent);
1739  	ret = i_ndi_config_node(proto, DS_INITIALIZED, 0);
1740  	ndi_devi_exit(parent);
1741  
1742  	return (ret);
1743  }
1744  
1745  /*
1746   * ddi_uninitchild: transform node down to DS_BOUND state
1747   */
1748  int
ddi_uninitchild(dev_info_t * dip)1749  ddi_uninitchild(dev_info_t *dip)
1750  {
1751  	int ret;
1752  	dev_info_t *parent = ddi_get_parent(dip);
1753  	ASSERT(parent);
1754  
1755  	ndi_devi_enter(parent);
1756  	ret = i_ndi_unconfig_node(dip, DS_BOUND, 0);
1757  	ndi_devi_exit(parent);
1758  
1759  	return (ret);
1760  }
1761  
1762  /*
1763   * i_ddi_attachchild: transform node to DS_READY/i_ddi_devi_attached() state
1764   */
1765  static int
i_ddi_attachchild(dev_info_t * dip)1766  i_ddi_attachchild(dev_info_t *dip)
1767  {
1768  	dev_info_t	*parent = ddi_get_parent(dip);
1769  	int		ret;
1770  
1771  	ASSERT(parent && DEVI_BUSY_OWNED(parent));
1772  
1773  	if ((i_ddi_node_state(dip) < DS_BOUND) || DEVI_IS_DEVICE_OFFLINE(dip))
1774  		return (DDI_FAILURE);
1775  
1776  	ret = i_ndi_config_node(dip, DS_READY, 0);
1777  	if (ret == NDI_SUCCESS) {
1778  		ret = DDI_SUCCESS;
1779  	} else {
1780  		/*
1781  		 * Take it down to DS_INITIALIZED so pm_pre_probe is run
1782  		 * on the next attach
1783  		 */
1784  		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
1785  		ret = DDI_FAILURE;
1786  	}
1787  
1788  	return (ret);
1789  }
1790  
1791  /*
1792   * i_ddi_detachchild: transform node down to DS_PROBED state
1793   *	If it fails, put it back to DS_READY state.
1794   * NOTE: A node that fails detach may be at DS_ATTACHED instead
1795   * of DS_READY for a small amount of time - this is the source of
1796   * transient DS_READY->DS_ATTACHED->DS_READY state changes.
1797   */
1798  static int
i_ddi_detachchild(dev_info_t * dip,uint_t flags)1799  i_ddi_detachchild(dev_info_t *dip, uint_t flags)
1800  {
1801  	dev_info_t	*parent = ddi_get_parent(dip);
1802  	int		ret;
1803  
1804  	ASSERT(parent && DEVI_BUSY_OWNED(parent));
1805  
1806  	ret = i_ndi_unconfig_node(dip, DS_PROBED, flags);
1807  	if (ret != DDI_SUCCESS)
1808  		(void) i_ndi_config_node(dip, DS_READY, 0);
1809  	else
1810  		/* allow pm_pre_probe to reestablish pm state */
1811  		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
1812  	return (ret);
1813  }
1814  
1815  /*
1816   * Add a child and bind to driver
1817   */
1818  dev_info_t *
ddi_add_child(dev_info_t * pdip,char * name,uint_t nodeid,uint_t unit)1819  ddi_add_child(dev_info_t *pdip, char *name, uint_t nodeid, uint_t unit)
1820  {
1821  	dev_info_t *dip;
1822  
1823  	/* allocate a new node */
1824  	dip = i_ddi_alloc_node(pdip, name, nodeid, (int)unit, NULL, KM_SLEEP);
1825  
1826  	ndi_devi_enter(pdip);
1827  	(void) i_ndi_config_node(dip, DS_BOUND, 0);
1828  	ndi_devi_exit(pdip);
1829  	return (dip);
1830  }
1831  
1832  /*
1833   * ddi_remove_child: remove the dip. The parent must be attached and held
1834   */
1835  int
ddi_remove_child(dev_info_t * dip,int dummy)1836  ddi_remove_child(dev_info_t *dip, int dummy)
1837  {
1838  	_NOTE(ARGUNUSED(dummy))
1839  	int ret;
1840  	dev_info_t *parent = ddi_get_parent(dip);
1841  	ASSERT(parent);
1842  
1843  	ndi_devi_enter(parent);
1844  
1845  	/*
1846  	 * If we still have children, for example SID nodes marked
1847  	 * as persistent but not attached, attempt to remove them.
1848  	 */
1849  	if (DEVI(dip)->devi_child) {
1850  		ret = ndi_devi_unconfig(dip, NDI_DEVI_REMOVE);
1851  		if (ret != NDI_SUCCESS) {
1852  			ndi_devi_exit(parent);
1853  			return (DDI_FAILURE);
1854  		}
1855  		ASSERT(DEVI(dip)->devi_child == NULL);
1856  	}
1857  
1858  	ret = i_ndi_unconfig_node(dip, DS_PROTO, 0);
1859  	ndi_devi_exit(parent);
1860  
1861  	if (ret != DDI_SUCCESS)
1862  		return (ret);
1863  
1864  	ASSERT(i_ddi_node_state(dip) == DS_PROTO);
1865  	i_ddi_free_node(dip);
1866  	return (DDI_SUCCESS);
1867  }
1868  
1869  /*
1870   * NDI wrappers for ref counting, node allocation, and transitions
1871   */
1872  
1873  /*
1874   * Hold/release the devinfo node itself.
1875   * Caller is assumed to prevent the devi from detaching during this call
1876   */
1877  void
ndi_hold_devi(dev_info_t * dip)1878  ndi_hold_devi(dev_info_t *dip)
1879  {
1880  	mutex_enter(&DEVI(dip)->devi_lock);
1881  	ASSERT(DEVI(dip)->devi_ref >= 0);
1882  	DEVI(dip)->devi_ref++;
1883  	membar_enter();			/* make sure stores are flushed */
1884  	mutex_exit(&DEVI(dip)->devi_lock);
1885  }
1886  
1887  void
ndi_rele_devi(dev_info_t * dip)1888  ndi_rele_devi(dev_info_t *dip)
1889  {
1890  	ASSERT(DEVI(dip)->devi_ref > 0);
1891  
1892  	mutex_enter(&DEVI(dip)->devi_lock);
1893  	DEVI(dip)->devi_ref--;
1894  	membar_enter();			/* make sure stores are flushed */
1895  	mutex_exit(&DEVI(dip)->devi_lock);
1896  }
1897  
1898  int
e_ddi_devi_holdcnt(dev_info_t * dip)1899  e_ddi_devi_holdcnt(dev_info_t *dip)
1900  {
1901  	return (DEVI(dip)->devi_ref);
1902  }
1903  
1904  /*
1905   * Hold/release the driver the devinfo node is bound to.
1906   */
1907  struct dev_ops *
ndi_hold_driver(dev_info_t * dip)1908  ndi_hold_driver(dev_info_t *dip)
1909  {
1910  	if (i_ddi_node_state(dip) < DS_BOUND)
1911  		return (NULL);
1912  
1913  	ASSERT(DEVI(dip)->devi_major != -1);
1914  	return (mod_hold_dev_by_major(DEVI(dip)->devi_major));
1915  }
1916  
1917  void
ndi_rele_driver(dev_info_t * dip)1918  ndi_rele_driver(dev_info_t *dip)
1919  {
1920  	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
1921  	mod_rele_dev_by_major(DEVI(dip)->devi_major);
1922  }
1923  
1924  /*
1925   * Single thread entry into devinfo node for modifying its children (devinfo,
1926   * pathinfo, and minor). To verify in ASSERTS use DEVI_BUSY_OWNED macro.
1927   */
1928  void
ndi_devi_enter(dev_info_t * dip)1929  ndi_devi_enter(dev_info_t *dip)
1930  {
1931  	struct dev_info *devi;
1932  	ASSERT(dip != NULL);
1933  
1934  	/* for vHCI, enforce (vHCI, pHCI) ndi_devi_enter() order */
1935  	ASSERT(!MDI_VHCI(dip) || (mdi_devi_pdip_entered(dip) == 0) ||
1936  	    DEVI_BUSY_OWNED(dip));
1937  
1938  	/*
1939  	 * If we're panicking, we are single-threaded and cannot
1940  	 * `mutex_enter`, so just return.
1941  	 */
1942  	if (panicstr != NULL)
1943  		return;
1944  
1945  	devi = DEVI(dip);
1946  	mutex_enter(&devi->devi_lock);
1947  	while (DEVI_BUSY_CHANGING(devi)) {
1948  		/*
1949  		 * If we are called when we are panicking, then we are
1950  		 * single-threaded, and would otherwise loop forever, so
1951  		 * we test for that here and early return if applicable.
1952  		 * Note that we also test for this in `ndi_devi_enter`;
1953  		 * regardless we must test again here in case we start
1954  		 * panicking while contended.
1955  		 */
1956  		if (panicstr != NULL) {
1957  			mutex_exit(&devi->devi_lock);
1958  			return;
1959  		}
1960  		if (devi->devi_busy_thread == curthread) {
1961  			devi->devi_circular++;
1962  			mutex_exit(&devi->devi_lock);
1963  			return;
1964  		}
1965  		cv_wait(&devi->devi_cv, &devi->devi_lock);
1966  	}
1967  	devi->devi_flags |= DEVI_BUSY;
1968  	devi->devi_busy_thread = curthread;
1969  	mutex_exit(&devi->devi_lock);
1970  }
1971  
1972  /*
1973   * Release ndi_devi_enter or successful ndi_devi_tryenter.
1974   *
1975   * Note that after we leave the critical section, if this is a pHCI exit we must
1976   * broadcast to our vHCI, if one exists, as it may be blocked on a condvar in
1977   * `ndi_devi_config_one`.
1978   *
1979   * It may seem odd that we do this after exiting the critical section, since we
1980   * are no longer protected by the conditions surrounding it, but note that
1981   * `ndi_devi_enter`/`ndi_devi_exit` and similar do not protect the `dip` itself.
1982   * Rather, the `dip` is protected by a reference count that is maintained by
1983   * calls to `ndi_hold_devi` and `ndi_rele_devi`.  If we're in this code path,
1984   * there must necessarily be such a reference, so it is safe to access our `dip`
1985   * any time here.
1986   *
1987   * Further, any pHCI or vHCI associated with this dip is effectively write-once
1988   * at setup, and the pHCI maintains a reference count on the vHCI (indeed, the
1989   * pHCI is what actually points to the vHCI), ensuring it lives at least as long
1990   * as the pHCI.
1991   *
1992   * Finally, it is safe to access the pHCI outside of the critical section for
1993   * the same reason we can access the dip: it is completely owned by the dip and
1994   * only deallocated in the detach path, and we only get there when all
1995   * references to the dip have been released.  Therefore, if we are in this code
1996   * path, the pHCI and thus the vHCI, if they exist, are both necessarily valid.
1997   */
1998  void
ndi_devi_exit(dev_info_t * dip)1999  ndi_devi_exit(dev_info_t *dip)
2000  {
2001  	struct dev_info	*devi, *vdevi;
2002  	boolean_t phci;
2003  
2004  	ASSERT(dip != NULL);
2005  
2006  	/*
2007  	 * If we're panicking, we are single threaded, so just return.
2008  	 */
2009  	if (panicstr != NULL)
2010  		return;
2011  
2012  	devi = DEVI(dip);
2013  	mutex_enter(&devi->devi_lock);
2014  	ASSERT(DEVI_BUSY_OWNED(devi));
2015  	if (devi->devi_circular > 0) {
2016  		devi->devi_circular--;
2017  		mutex_exit(&devi->devi_lock);
2018  		return;
2019  	}
2020  	devi->devi_flags &= ~DEVI_BUSY;
2021  	devi->devi_busy_thread = NULL;
2022  	cv_broadcast(&devi->devi_cv);
2023  	mutex_exit(&devi->devi_lock);
2024  
2025  	/*
2026  	 * Note that `DEVI(mdi_devi_get_vdip(dip))` will be NULL if `dip` is
2027  	 * not a pHCI or the vHCI doest not exist.
2028  	 */
2029  	vdevi = DEVI(mdi_devi_get_vdip(dip));
2030  	if (vdevi != NULL) {
2031  		mutex_enter(&vdevi->devi_lock);
2032  		if ((vdevi->devi_flags & DEVI_PHCI_SIGNALS_VHCI) != 0) {
2033  			vdevi->devi_flags &= ~DEVI_PHCI_SIGNALS_VHCI;
2034  			cv_broadcast(&vdevi->devi_cv);
2035  		}
2036  		mutex_exit(&vdevi->devi_lock);
2037  	}
2038  }
2039  
2040  /*
2041   * Release ndi_devi_enter and wait for possibility of new children, avoiding
2042   * possibility of missing broadcast before getting to cv_timedwait().
2043   */
2044  static void
ndi_devi_exit_and_wait(dev_info_t * dip,clock_t end_time)2045  ndi_devi_exit_and_wait(dev_info_t *dip, clock_t end_time)
2046  {
2047  	struct dev_info *devi;
2048  
2049  	ASSERT(dip != NULL);
2050  
2051  	/*
2052  	 * If we're panicking, we are single threaded, and cannot
2053  	 * call mutex_enter(), so just return.
2054  	 */
2055  	if (panicstr)
2056  		return;
2057  
2058  	/* like ndi_devi_exit with circular of zero */
2059  	devi = DEVI(dip);
2060  	mutex_enter(&devi->devi_lock);
2061  	/*
2062  	 * We are called to wait for a new child, and new child can
2063  	 * only be added if circular is zero.
2064  	 */
2065  	ASSERT(devi->devi_circular == 0);
2066  	ASSERT(DEVI_BUSY_OWNED(devi));
2067  	devi->devi_flags &= ~DEVI_BUSY;
2068  	devi->devi_busy_thread = NULL;
2069  	cv_broadcast(&devi->devi_cv);
2070  
2071  	/* now wait for new children while still holding devi_lock */
2072  	(void) cv_timedwait(&devi->devi_cv, &devi->devi_lock, end_time);
2073  	mutex_exit(&devi->devi_lock);
2074  }
2075  
2076  /*
2077   * Attempt to single thread entry into devinfo node for modifying its children.
2078   */
2079  int
ndi_devi_tryenter(dev_info_t * dip)2080  ndi_devi_tryenter(dev_info_t *dip)
2081  {
2082  	int entered;
2083  	struct dev_info *devi;
2084  
2085  	ASSERT(dip != NULL);
2086  
2087  	/*
2088  	 * If we're panicing, we are single threaded, and cannot
2089  	 * call mutex_enter(), so just return.
2090  	 */
2091  	if (panicstr != NULL)
2092  		return (0);
2093  
2094  	devi = DEVI(dip);
2095  	mutex_enter(&devi->devi_lock);
2096  	entered = 1;
2097  	if (!DEVI_BUSY_CHANGING(devi)) {
2098  		/* The uncontended case. */
2099  		devi->devi_flags |= DEVI_BUSY;
2100  		devi->devi_busy_thread = curthread;
2101  	} else if (devi->devi_busy_thread == curthread) {
2102  		/* Nested entry on the same thread. */
2103  		devi->devi_circular++;
2104  	} else {
2105  		/* We fail on the contended case. */
2106  		entered = 0;
2107  	}
2108  	mutex_exit(&devi->devi_lock);
2109  
2110  	return (entered);
2111  }
2112  
2113  /*
2114   * Allocate and initialize a new dev_info structure.
2115   *
2116   * This routine may be called at interrupt time by a nexus in
2117   * response to a hotplug event, therefore memory allocations are
2118   * not allowed to sleep.
2119   */
2120  int
ndi_devi_alloc(dev_info_t * parent,const char * node_name,pnode_t nodeid,dev_info_t ** ret_dip)2121  ndi_devi_alloc(dev_info_t *parent, const char *node_name, pnode_t nodeid,
2122      dev_info_t **ret_dip)
2123  {
2124  	ASSERT(node_name != NULL);
2125  	ASSERT(ret_dip != NULL);
2126  
2127  	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
2128  	    KM_NOSLEEP);
2129  	if (*ret_dip == NULL) {
2130  		return (NDI_NOMEM);
2131  	}
2132  
2133  	return (NDI_SUCCESS);
2134  }
2135  
2136  /*
2137   * Allocate and initialize a new dev_info structure
2138   * This routine may sleep and should not be called at interrupt time
2139   */
2140  void
ndi_devi_alloc_sleep(dev_info_t * parent,const char * node_name,pnode_t nodeid,dev_info_t ** ret_dip)2141  ndi_devi_alloc_sleep(dev_info_t *parent, const char *node_name, pnode_t nodeid,
2142      dev_info_t **ret_dip)
2143  {
2144  	ASSERT(node_name != NULL);
2145  	ASSERT(ret_dip != NULL);
2146  
2147  	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
2148  	    KM_SLEEP);
2149  	ASSERT(*ret_dip);
2150  }
2151  
2152  /*
2153   * Remove an initialized (but not yet attached) dev_info
2154   * node from it's parent.
2155   */
2156  int
ndi_devi_free(dev_info_t * dip)2157  ndi_devi_free(dev_info_t *dip)
2158  {
2159  	ASSERT(dip != NULL);
2160  
2161  	if (i_ddi_node_state(dip) >= DS_INITIALIZED)
2162  		return (DDI_FAILURE);
2163  
2164  	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_free: %s%d (%p)\n",
2165  	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
2166  
2167  	(void) ddi_remove_child(dip, 0);
2168  
2169  	return (NDI_SUCCESS);
2170  }
2171  
2172  /*
2173   * ndi_devi_bind_driver() binds a driver to a given device. If it fails
2174   * to bind the driver, it returns an appropriate error back. Some drivers
2175   * may want to know if the actually failed to bind.
2176   */
2177  int
ndi_devi_bind_driver(dev_info_t * dip,uint_t flags)2178  ndi_devi_bind_driver(dev_info_t *dip, uint_t flags)
2179  {
2180  	int ret = NDI_FAILURE;
2181  	dev_info_t *pdip = ddi_get_parent(dip);
2182  	ASSERT(pdip);
2183  
2184  	NDI_CONFIG_DEBUG((CE_CONT,
2185  	    "ndi_devi_bind_driver: %s%d (%p) flags: %x\n",
2186  	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
2187  
2188  	ndi_devi_enter(pdip);
2189  	if (i_ndi_config_node(dip, DS_BOUND, flags) == DDI_SUCCESS)
2190  		ret = NDI_SUCCESS;
2191  	ndi_devi_exit(pdip);
2192  
2193  	return (ret);
2194  }
2195  
2196  /*
2197   * ndi_devi_unbind_driver: unbind the dip
2198   */
2199  static int
ndi_devi_unbind_driver(dev_info_t * dip)2200  ndi_devi_unbind_driver(dev_info_t *dip)
2201  {
2202  	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
2203  
2204  	return (i_ndi_unconfig_node(dip, DS_LINKED, 0));
2205  }
2206  
2207  /*
2208   * Misc. help routines called by framework only
2209   */
2210  
2211  /*
2212   * Get the state of node
2213   */
2214  ddi_node_state_t
i_ddi_node_state(dev_info_t * dip)2215  i_ddi_node_state(dev_info_t *dip)
2216  {
2217  	return (DEVI(dip)->devi_node_state);
2218  }
2219  
2220  /*
2221   * Set the state of node
2222   */
2223  void
i_ddi_set_node_state(dev_info_t * dip,ddi_node_state_t state)2224  i_ddi_set_node_state(dev_info_t *dip, ddi_node_state_t state)
2225  {
2226  	DEVI(dip)->devi_node_state = state;
2227  	membar_enter();			/* make sure stores are flushed */
2228  }
2229  
2230  /*
2231   * Determine if node is attached. The implementation accommodates transient
2232   * DS_READY->DS_ATTACHED->DS_READY state changes.  Outside this file, this
2233   * function should be instead of i_ddi_node_state() DS_ATTACHED/DS_READY
2234   * state checks.
2235   */
2236  int
i_ddi_devi_attached(dev_info_t * dip)2237  i_ddi_devi_attached(dev_info_t *dip)
2238  {
2239  	return (DEVI(dip)->devi_node_state >= DS_ATTACHED);
2240  }
2241  
2242  /*
2243   * Common function for finding a node in a sibling list given name and addr.
2244   *
2245   * By default, name is matched with devi_node_name. The following
2246   * alternative match strategies are supported:
2247   *
2248   *	FIND_NODE_BY_NODENAME: Match on node name - typical use.
2249   *
2250   *	FIND_NODE_BY_DRIVER: A match on driver name bound to node is conducted.
2251   *		This support is used for support of OBP generic names and
2252   *		for the conversion from driver names to generic names. When
2253   *		more consistency in the generic name environment is achieved
2254   *		(and not needed for upgrade) this support can be removed.
2255   *
2256   *	FIND_NODE_BY_ADDR: Match on just the addr.
2257   *		This support is only used/needed during boot to match
2258   *		a node bound via a path-based driver alias.
2259   *
2260   * If a child is not named (dev_addr == NULL), there are three
2261   * possible actions:
2262   *
2263   *	(1) skip it
2264   *	(2) FIND_ADDR_BY_INIT: bring child to DS_INITIALIZED state
2265   *	(3) FIND_ADDR_BY_CALLBACK: use a caller-supplied callback function
2266   */
2267  #define	FIND_NODE_BY_NODENAME	0x01
2268  #define	FIND_NODE_BY_DRIVER	0x02
2269  #define	FIND_NODE_BY_ADDR	0x04
2270  #define	FIND_ADDR_BY_INIT	0x10
2271  #define	FIND_ADDR_BY_CALLBACK	0x20
2272  
2273  static dev_info_t *
find_sibling(dev_info_t * head,char * cname,char * caddr,uint_t flag,int (* callback)(dev_info_t *,char *,int))2274  find_sibling(dev_info_t *head, char *cname, char *caddr, uint_t flag,
2275      int (*callback)(dev_info_t *, char *, int))
2276  {
2277  	dev_info_t	*dip;
2278  	char		*addr, *buf;
2279  	major_t		major;
2280  	uint_t		by;
2281  
2282  	/* only one way to find a node */
2283  	by = flag &
2284  	    (FIND_NODE_BY_DRIVER | FIND_NODE_BY_NODENAME | FIND_NODE_BY_ADDR);
2285  	ASSERT(by && BIT_ONLYONESET(by));
2286  
2287  	/* only one way to name a node */
2288  	ASSERT(((flag & FIND_ADDR_BY_INIT) == 0) ||
2289  	    ((flag & FIND_ADDR_BY_CALLBACK) == 0));
2290  
2291  	if (by == FIND_NODE_BY_DRIVER) {
2292  		major = ddi_name_to_major(cname);
2293  		if (major == DDI_MAJOR_T_NONE)
2294  			return (NULL);
2295  	}
2296  
2297  	if (head == NULL)
2298  		return (NULL);
2299  
2300  	buf = NULL;
2301  	/* preallocate buffer of naming node by callback */
2302  	if (flag & FIND_ADDR_BY_CALLBACK)
2303  		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2304  
2305  	/*
2306  	 * Walk the child list to find a match
2307  	 */
2308  	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(head)));
2309  	for (dip = head; dip; dip = ddi_get_next_sibling(dip)) {
2310  		if (by == FIND_NODE_BY_NODENAME) {
2311  			/* match node name */
2312  			if (strcmp(cname, DEVI(dip)->devi_node_name) != 0)
2313  				continue;
2314  		} else if (by == FIND_NODE_BY_DRIVER) {
2315  			/* match driver major */
2316  			if (DEVI(dip)->devi_major != major)
2317  				continue;
2318  		}
2319  
2320  		if ((addr = DEVI(dip)->devi_addr) == NULL) {
2321  			/* name the child based on the flag */
2322  			if (flag & FIND_ADDR_BY_INIT) {
2323  				if (ddi_initchild(ddi_get_parent(dip), dip)
2324  				    != DDI_SUCCESS)
2325  					continue;
2326  				addr = DEVI(dip)->devi_addr;
2327  			} else if (flag & FIND_ADDR_BY_CALLBACK) {
2328  				if ((callback == NULL) || (callback(
2329  				    dip, buf, MAXNAMELEN) != DDI_SUCCESS))
2330  					continue;
2331  				addr = buf;
2332  			} else {
2333  				continue;	/* skip */
2334  			}
2335  		}
2336  
2337  		/* match addr */
2338  		ASSERT(addr != NULL);
2339  		if (strcmp(caddr, addr) == 0)
2340  			break;	/* node found */
2341  
2342  	}
2343  	if (flag & FIND_ADDR_BY_CALLBACK)
2344  		kmem_free(buf, MAXNAMELEN);
2345  	return (dip);
2346  }
2347  
2348  /*
2349   * Find child of pdip with name: cname@caddr
2350   * Called by init_node() to look for duplicate nodes
2351   */
2352  static dev_info_t *
find_duplicate_child(dev_info_t * pdip,dev_info_t * dip)2353  find_duplicate_child(dev_info_t *pdip, dev_info_t *dip)
2354  {
2355  	dev_info_t *dup;
2356  	char *cname = DEVI(dip)->devi_node_name;
2357  	char *caddr = DEVI(dip)->devi_addr;
2358  
2359  	/* search nodes before dip */
2360  	dup = find_sibling(ddi_get_child(pdip), cname, caddr,
2361  	    FIND_NODE_BY_NODENAME, NULL);
2362  	if (dup != dip)
2363  		return (dup);
2364  
2365  	/*
2366  	 * search nodes after dip; normally this is not needed,
2367  	 */
2368  	return (find_sibling(ddi_get_next_sibling(dip), cname, caddr,
2369  	    FIND_NODE_BY_NODENAME, NULL));
2370  }
2371  
2372  /*
2373   * Find a child of a given name and address, using a callback to name
2374   * unnamed children. cname is the binding name.
2375   */
2376  dev_info_t *
ndi_devi_findchild_by_callback(dev_info_t * pdip,char * dname,char * ua,int (* make_ua)(dev_info_t *,char *,int))2377  ndi_devi_findchild_by_callback(dev_info_t *pdip, char *dname, char *ua,
2378      int (*make_ua)(dev_info_t *, char *, int))
2379  {
2380  	int	by = FIND_ADDR_BY_CALLBACK;
2381  
2382  	ASSERT(DEVI_BUSY_OWNED(pdip));
2383  	by |= dname ? FIND_NODE_BY_DRIVER : FIND_NODE_BY_ADDR;
2384  	return (find_sibling(ddi_get_child(pdip), dname, ua, by, make_ua));
2385  }
2386  
2387  /*
2388   * Find a child of a given name and address, invoking initchild to name
2389   * unnamed children. cname is the node name.
2390   */
2391  static dev_info_t *
find_child_by_name(dev_info_t * pdip,char * cname,char * caddr)2392  find_child_by_name(dev_info_t *pdip, char *cname, char *caddr)
2393  {
2394  	dev_info_t	*dip;
2395  
2396  	/* attempt search without changing state of preceding siblings */
2397  	dip = find_sibling(ddi_get_child(pdip), cname, caddr,
2398  	    FIND_NODE_BY_NODENAME, NULL);
2399  	if (dip)
2400  		return (dip);
2401  
2402  	return (find_sibling(ddi_get_child(pdip), cname, caddr,
2403  	    FIND_NODE_BY_NODENAME|FIND_ADDR_BY_INIT, NULL));
2404  }
2405  
2406  /*
2407   * Find a child of a given name and address, invoking initchild to name
2408   * unnamed children. cname is the node name.
2409   */
2410  static dev_info_t *
find_child_by_driver(dev_info_t * pdip,char * cname,char * caddr)2411  find_child_by_driver(dev_info_t *pdip, char *cname, char *caddr)
2412  {
2413  	dev_info_t	*dip;
2414  
2415  	/* attempt search without changing state of preceding siblings */
2416  	dip = find_sibling(ddi_get_child(pdip), cname, caddr,
2417  	    FIND_NODE_BY_DRIVER, NULL);
2418  	if (dip)
2419  		return (dip);
2420  
2421  	return (find_sibling(ddi_get_child(pdip), cname, caddr,
2422  	    FIND_NODE_BY_DRIVER|FIND_ADDR_BY_INIT, NULL));
2423  }
2424  
2425  /*
2426   * Find a child of a given address, invoking initchild to name
2427   * unnamed children. cname is the node name.
2428   *
2429   * NOTE: This function is only used during boot. One would hope that
2430   * unique sibling unit-addresses on hardware branches of the tree would
2431   * be a requirement to avoid two drivers trying to control the same
2432   * piece of hardware. Unfortunately there are some cases where this
2433   * situation exists (/ssm@0,0/pci@1c,700000 /ssm@0,0/sghsc@1c,700000).
2434   * Until unit-address uniqueness of siblings is guaranteed, use of this
2435   * interface for purposes other than boot should be avoided.
2436   */
2437  static dev_info_t *
find_child_by_addr(dev_info_t * pdip,char * caddr)2438  find_child_by_addr(dev_info_t *pdip, char *caddr)
2439  {
2440  	dev_info_t	*dip;
2441  
2442  	/* return NULL if called without a unit-address */
2443  	if ((caddr == NULL) || (*caddr == '\0'))
2444  		return (NULL);
2445  
2446  	/* attempt search without changing state of preceding siblings */
2447  	dip = find_sibling(ddi_get_child(pdip), NULL, caddr,
2448  	    FIND_NODE_BY_ADDR, NULL);
2449  	if (dip)
2450  		return (dip);
2451  
2452  	return (find_sibling(ddi_get_child(pdip), NULL, caddr,
2453  	    FIND_NODE_BY_ADDR|FIND_ADDR_BY_INIT, NULL));
2454  }
2455  
2456  /*
2457   * Deleting a property list. Take care, since some property structures
2458   * may not be fully built.
2459   */
2460  void
i_ddi_prop_list_delete(ddi_prop_t * prop)2461  i_ddi_prop_list_delete(ddi_prop_t *prop)
2462  {
2463  	while (prop) {
2464  		ddi_prop_t *next = prop->prop_next;
2465  		if (prop->prop_name)
2466  			kmem_free(prop->prop_name, strlen(prop->prop_name) + 1);
2467  		if ((prop->prop_len != 0) && prop->prop_val)
2468  			kmem_free(prop->prop_val, prop->prop_len);
2469  		kmem_free(prop, sizeof (struct ddi_prop));
2470  		prop = next;
2471  	}
2472  }
2473  
2474  /*
2475   * Duplicate property list
2476   */
2477  ddi_prop_t *
i_ddi_prop_list_dup(ddi_prop_t * prop,uint_t flag)2478  i_ddi_prop_list_dup(ddi_prop_t *prop, uint_t flag)
2479  {
2480  	ddi_prop_t *result, *prev, *copy;
2481  
2482  	if (prop == NULL)
2483  		return (NULL);
2484  
2485  	result = prev = NULL;
2486  	for (; prop != NULL; prop = prop->prop_next) {
2487  		ASSERT(prop->prop_name != NULL);
2488  		copy = kmem_zalloc(sizeof (struct ddi_prop), flag);
2489  		if (copy == NULL)
2490  			goto fail;
2491  
2492  		copy->prop_dev = prop->prop_dev;
2493  		copy->prop_flags = prop->prop_flags;
2494  		copy->prop_name = i_ddi_strdup(prop->prop_name, flag);
2495  		if (copy->prop_name == NULL) {
2496  			kmem_free(copy, sizeof (struct ddi_prop));
2497  			goto fail;
2498  		}
2499  
2500  		if ((copy->prop_len = prop->prop_len) != 0) {
2501  			copy->prop_val = kmem_zalloc(prop->prop_len, flag);
2502  			if (copy->prop_val == NULL) {
2503  				strfree(copy->prop_name);
2504  				kmem_free(copy, sizeof (struct ddi_prop));
2505  				goto fail;
2506  			}
2507  
2508  			bcopy(prop->prop_val, copy->prop_val, prop->prop_len);
2509  		}
2510  
2511  		if (prev == NULL)
2512  			result = prev = copy;
2513  		else
2514  			prev->prop_next = copy;
2515  		prev = copy;
2516  	}
2517  	return (result);
2518  
2519  fail:
2520  	i_ddi_prop_list_delete(result);
2521  	return (NULL);
2522  }
2523  
2524  /*
2525   * Create a reference property list, currently used only for
2526   * driver global properties. Created with ref count of 1.
2527   */
2528  ddi_prop_list_t *
i_ddi_prop_list_create(ddi_prop_t * props)2529  i_ddi_prop_list_create(ddi_prop_t *props)
2530  {
2531  	ddi_prop_list_t *list = kmem_alloc(sizeof (*list), KM_SLEEP);
2532  	list->prop_list = props;
2533  	list->prop_ref = 1;
2534  	return (list);
2535  }
2536  
2537  /*
2538   * Increment/decrement reference count. The reference is
2539   * protected by dn_lock. The only interfaces modifying
2540   * dn_global_prop_ptr is in impl_make[free]_parlist().
2541   */
2542  void
i_ddi_prop_list_hold(ddi_prop_list_t * prop_list,struct devnames * dnp)2543  i_ddi_prop_list_hold(ddi_prop_list_t *prop_list, struct devnames *dnp)
2544  {
2545  	ASSERT(prop_list->prop_ref >= 0);
2546  	ASSERT(mutex_owned(&dnp->dn_lock));
2547  	prop_list->prop_ref++;
2548  }
2549  
2550  void
i_ddi_prop_list_rele(ddi_prop_list_t * prop_list,struct devnames * dnp)2551  i_ddi_prop_list_rele(ddi_prop_list_t *prop_list, struct devnames *dnp)
2552  {
2553  	ASSERT(prop_list->prop_ref > 0);
2554  	ASSERT(mutex_owned(&dnp->dn_lock));
2555  	prop_list->prop_ref--;
2556  
2557  	if (prop_list->prop_ref == 0) {
2558  		i_ddi_prop_list_delete(prop_list->prop_list);
2559  		kmem_free(prop_list, sizeof (*prop_list));
2560  	}
2561  }
2562  
2563  /*
2564   * Free table of classes by drivers
2565   */
2566  void
i_ddi_free_exported_classes(char ** classes,int n)2567  i_ddi_free_exported_classes(char **classes, int n)
2568  {
2569  	if ((n == 0) || (classes == NULL))
2570  		return;
2571  
2572  	kmem_free(classes, n * sizeof (char *));
2573  }
2574  
2575  /*
2576   * Get all classes exported by dip
2577   */
2578  int
i_ddi_get_exported_classes(dev_info_t * dip,char *** classes)2579  i_ddi_get_exported_classes(dev_info_t *dip, char ***classes)
2580  {
2581  	extern void lock_hw_class_list();
2582  	extern void unlock_hw_class_list();
2583  	extern int get_class(const char *, char **);
2584  
2585  	static char *rootclass = "root";
2586  	int n = 0, nclass = 0;
2587  	char **buf;
2588  
2589  	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
2590  
2591  	if (dip == ddi_root_node())	/* rootnode exports class "root" */
2592  		nclass = 1;
2593  	lock_hw_class_list();
2594  	nclass += get_class(ddi_driver_name(dip), NULL);
2595  	if (nclass == 0) {
2596  		unlock_hw_class_list();
2597  		return (0);		/* no class exported */
2598  	}
2599  
2600  	*classes = buf = kmem_alloc(nclass * sizeof (char *), KM_SLEEP);
2601  	if (dip == ddi_root_node()) {
2602  		*buf++ = rootclass;
2603  		n = 1;
2604  	}
2605  	n += get_class(ddi_driver_name(dip), buf);
2606  	unlock_hw_class_list();
2607  
2608  	ASSERT(n == nclass);	/* make sure buf wasn't overrun */
2609  	return (nclass);
2610  }
2611  
2612  /*
2613   * Helper functions, returns NULL if no memory.
2614   */
2615  char *
i_ddi_strdup(const char * str,uint_t flag)2616  i_ddi_strdup(const char *str, uint_t flag)
2617  {
2618  	char *copy;
2619  
2620  	if (str == NULL)
2621  		return (NULL);
2622  
2623  	copy = kmem_alloc(strlen(str) + 1, flag);
2624  	if (copy == NULL)
2625  		return (NULL);
2626  
2627  	(void) strcpy(copy, str);
2628  	return (copy);
2629  }
2630  
2631  /*
2632   * Load driver.conf file for major. Load all if major == -1.
2633   *
2634   * This is called
2635   * - early in boot after devnames array is initialized
2636   * - from vfs code when certain file systems are mounted
2637   * - from add_drv when a new driver is added
2638   */
2639  int
i_ddi_load_drvconf(major_t major)2640  i_ddi_load_drvconf(major_t major)
2641  {
2642  	extern int modrootloaded;
2643  
2644  	major_t low, high, m;
2645  
2646  	if (major == DDI_MAJOR_T_NONE) {
2647  		low = 0;
2648  		high = devcnt - 1;
2649  	} else {
2650  		if (major >= devcnt)
2651  			return (EINVAL);
2652  		low = high = major;
2653  	}
2654  
2655  	for (m = low; m <= high; m++) {
2656  		struct devnames *dnp = &devnamesp[m];
2657  		LOCK_DEV_OPS(&dnp->dn_lock);
2658  		dnp->dn_flags &= ~(DN_DRIVER_HELD|DN_DRIVER_INACTIVE);
2659  		(void) impl_make_parlist(m);
2660  		UNLOCK_DEV_OPS(&dnp->dn_lock);
2661  	}
2662  
2663  	if (modrootloaded) {
2664  		ddi_walk_devs(ddi_root_node(), reset_nexus_flags,
2665  		    (void *)(uintptr_t)major);
2666  	}
2667  
2668  	/* build dn_list from old entries in path_to_inst */
2669  	e_ddi_unorphan_instance_nos();
2670  	return (0);
2671  }
2672  
2673  /*
2674   * Unload a specific driver.conf.
2675   * Don't support unload all because it doesn't make any sense
2676   */
2677  int
i_ddi_unload_drvconf(major_t major)2678  i_ddi_unload_drvconf(major_t major)
2679  {
2680  	int error;
2681  	struct devnames *dnp;
2682  
2683  	if (major >= devcnt)
2684  		return (EINVAL);
2685  
2686  	/*
2687  	 * Take the per-driver lock while unloading driver.conf
2688  	 */
2689  	dnp = &devnamesp[major];
2690  	LOCK_DEV_OPS(&dnp->dn_lock);
2691  	error = impl_free_parlist(major);
2692  	UNLOCK_DEV_OPS(&dnp->dn_lock);
2693  	return (error);
2694  }
2695  
2696  /*
2697   * Merge a .conf node. This is called by nexus drivers to augment
2698   * hw node with properties specified in driver.conf file. This function
2699   * takes a callback routine to name nexus children.
2700   * The parent node must be held busy.
2701   *
2702   * It returns DDI_SUCCESS if the node is merged and DDI_FAILURE otherwise.
2703   */
2704  int
ndi_merge_node(dev_info_t * dip,int (* make_ua)(dev_info_t *,char *,int))2705  ndi_merge_node(dev_info_t *dip, int (*make_ua)(dev_info_t *, char *, int))
2706  {
2707  	dev_info_t *hwdip;
2708  
2709  	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
2710  	ASSERT(ddi_get_name_addr(dip) != NULL);
2711  
2712  	hwdip = ndi_devi_findchild_by_callback(ddi_get_parent(dip),
2713  	    ddi_binding_name(dip), ddi_get_name_addr(dip), make_ua);
2714  
2715  	/*
2716  	 * Look for the hardware node that is the target of the merge;
2717  	 * return failure if not found.
2718  	 */
2719  	if ((hwdip == NULL) || (hwdip == dip)) {
2720  		char *buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2721  		NDI_CONFIG_DEBUG((CE_WARN, "No HW node to merge conf node %s",
2722  		    ddi_deviname(dip, buf)));
2723  		kmem_free(buf, MAXNAMELEN);
2724  		return (DDI_FAILURE);
2725  	}
2726  
2727  	/*
2728  	 * Make sure the hardware node is uninitialized and has no property.
2729  	 * This may not be the case if new .conf files are load after some
2730  	 * hardware nodes have already been initialized and attached.
2731  	 *
2732  	 * N.B. We return success here because the node was *intended*
2733  	 *	to be a merge node because there is a hw node with the name.
2734  	 */
2735  	mutex_enter(&DEVI(hwdip)->devi_lock);
2736  	if (ndi_dev_is_persistent_node(hwdip) == 0) {
2737  		char *buf;
2738  		mutex_exit(&DEVI(hwdip)->devi_lock);
2739  
2740  		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2741  		NDI_CONFIG_DEBUG((CE_NOTE, "Duplicate .conf node %s",
2742  		    ddi_deviname(dip, buf)));
2743  		kmem_free(buf, MAXNAMELEN);
2744  		return (DDI_SUCCESS);
2745  	}
2746  
2747  	/*
2748  	 * If it is possible that the hardware has already been touched
2749  	 * then don't merge.
2750  	 */
2751  	if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
2752  	    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
2753  	    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
2754  		char *buf;
2755  		mutex_exit(&DEVI(hwdip)->devi_lock);
2756  
2757  		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2758  		NDI_CONFIG_DEBUG((CE_NOTE,
2759  		    "!Cannot merge .conf node %s with hw node %p "
2760  		    "-- not in proper state",
2761  		    ddi_deviname(dip, buf), (void *)hwdip));
2762  		kmem_free(buf, MAXNAMELEN);
2763  		return (DDI_SUCCESS);
2764  	}
2765  
2766  	mutex_enter(&DEVI(dip)->devi_lock);
2767  	DEVI(hwdip)->devi_sys_prop_ptr = DEVI(dip)->devi_sys_prop_ptr;
2768  	DEVI(hwdip)->devi_drv_prop_ptr = DEVI(dip)->devi_drv_prop_ptr;
2769  	DEVI(dip)->devi_sys_prop_ptr = NULL;
2770  	DEVI(dip)->devi_drv_prop_ptr = NULL;
2771  	mutex_exit(&DEVI(dip)->devi_lock);
2772  	mutex_exit(&DEVI(hwdip)->devi_lock);
2773  
2774  	return (DDI_SUCCESS);
2775  }
2776  
2777  /*
2778   * Merge a "wildcard" .conf node. This is called by nexus drivers to
2779   * augment a set of hw node with properties specified in driver.conf file.
2780   * The parent node must be held busy.
2781   *
2782   * There is no failure mode, since the nexus may or may not have child
2783   * node bound the driver specified by the wildcard node.
2784   */
2785  void
ndi_merge_wildcard_node(dev_info_t * dip)2786  ndi_merge_wildcard_node(dev_info_t *dip)
2787  {
2788  	dev_info_t *hwdip;
2789  	dev_info_t *pdip = ddi_get_parent(dip);
2790  	major_t major = ddi_driver_major(dip);
2791  
2792  	/* never attempt to merge a hw node */
2793  	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
2794  	/* must be bound to a driver major number */
2795  	ASSERT(major != DDI_MAJOR_T_NONE);
2796  
2797  	/*
2798  	 * Walk the child list to find all nodes bound to major
2799  	 * and copy properties.
2800  	 */
2801  	mutex_enter(&DEVI(dip)->devi_lock);
2802  	ASSERT(DEVI_BUSY_OWNED(pdip));
2803  	for (hwdip = ddi_get_child(pdip); hwdip;
2804  	    hwdip = ddi_get_next_sibling(hwdip)) {
2805  		/*
2806  		 * Skip nodes not bound to same driver
2807  		 */
2808  		if (ddi_driver_major(hwdip) != major)
2809  			continue;
2810  
2811  		/*
2812  		 * Skip .conf nodes
2813  		 */
2814  		if (ndi_dev_is_persistent_node(hwdip) == 0)
2815  			continue;
2816  
2817  		/*
2818  		 * Make sure the node is uninitialized and has no property.
2819  		 */
2820  		mutex_enter(&DEVI(hwdip)->devi_lock);
2821  		if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
2822  		    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
2823  		    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
2824  			mutex_exit(&DEVI(hwdip)->devi_lock);
2825  			NDI_CONFIG_DEBUG((CE_NOTE, "HW node %p state not "
2826  			    "suitable for merging wildcard conf node %s",
2827  			    (void *)hwdip, ddi_node_name(dip)));
2828  			continue;
2829  		}
2830  
2831  		DEVI(hwdip)->devi_sys_prop_ptr =
2832  		    i_ddi_prop_list_dup(DEVI(dip)->devi_sys_prop_ptr, KM_SLEEP);
2833  		DEVI(hwdip)->devi_drv_prop_ptr =
2834  		    i_ddi_prop_list_dup(DEVI(dip)->devi_drv_prop_ptr, KM_SLEEP);
2835  		mutex_exit(&DEVI(hwdip)->devi_lock);
2836  	}
2837  	mutex_exit(&DEVI(dip)->devi_lock);
2838  }
2839  
2840  /*
2841   * Return the major number based on the compatible property. This interface
2842   * may be used in situations where we are trying to detect if a better driver
2843   * now exists for a device, so it must use the 'compatible' property.  If
2844   * a non-NULL formp is specified and the binding was based on compatible then
2845   * return the pointer to the form used in *formp.
2846   */
2847  major_t
ddi_compatible_driver_major(dev_info_t * dip,char ** formp)2848  ddi_compatible_driver_major(dev_info_t *dip, char **formp)
2849  {
2850  	struct dev_info *devi = DEVI(dip);
2851  	void		*compat;
2852  	size_t		len;
2853  	char		*p = NULL;
2854  	major_t		major = DDI_MAJOR_T_NONE;
2855  
2856  	if (formp)
2857  		*formp = NULL;
2858  
2859  	/*
2860  	 * The "ddi-assigned" property indicates a device has been given to a
2861  	 * virtualized environment.  Prevent its use.  This is only used by
2862  	 * Xen and (previously) by sun4v LDOMs.  See pcie_init_dom().
2863  	 */
2864  	if (ddi_prop_exists(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
2865  	    "ddi-assigned")) {
2866  		major = ddi_name_to_major("nulldriver");
2867  		return (major);
2868  	}
2869  
2870  	/*
2871  	 * Highest precedence binding is a path-oriented alias. Since this
2872  	 * requires a 'path', this type of binding occurs via more obtuse
2873  	 * 'rebind'. The need for a path-oriented alias 'rebind' is detected
2874  	 * after a successful DDI_CTLOPS_INITCHILD to another driver: this is
2875  	 * is the first point at which the unit-address (or instance) of the
2876  	 * last component of the path is available (even though the path is
2877  	 * bound to the wrong driver at this point).
2878  	 */
2879  	if (devi->devi_flags & DEVI_REBIND) {
2880  		p = devi->devi_rebinding_name;
2881  		major = ddi_name_to_major(p);
2882  		if (driver_active(major)) {
2883  			if (formp)
2884  				*formp = p;
2885  			return (major);
2886  		}
2887  
2888  		/*
2889  		 * If for some reason devi_rebinding_name no longer resolves
2890  		 * to a proper driver then clear DEVI_REBIND.
2891  		 */
2892  		mutex_enter(&devi->devi_lock);
2893  		devi->devi_flags &= ~DEVI_REBIND;
2894  		mutex_exit(&devi->devi_lock);
2895  	}
2896  
2897  	/* look up compatible property */
2898  	(void) lookup_compatible(dip, KM_SLEEP);
2899  	compat = (void *)(devi->devi_compat_names);
2900  	len = devi->devi_compat_length;
2901  
2902  	/* find the highest precedence compatible form with a driver binding */
2903  	while ((p = prom_decode_composite_string(compat, len, p)) != NULL) {
2904  		major = ddi_name_to_major(p);
2905  		if (driver_active(major)) {
2906  			if (formp)
2907  				*formp = p;
2908  			return (major);
2909  		}
2910  	}
2911  
2912  	/*
2913  	 * none of the compatible forms have a driver binding, see if
2914  	 * the node name has a driver binding.
2915  	 */
2916  	major = ddi_name_to_major(ddi_node_name(dip));
2917  	if (driver_active(major))
2918  		return (major);
2919  
2920  	/* no driver */
2921  	return (DDI_MAJOR_T_NONE);
2922  }
2923  
2924  /*
2925   * Static help functions
2926   */
2927  
2928  /*
2929   * lookup the "compatible" property and cache it's contents in the
2930   * device node.
2931   */
2932  static int
lookup_compatible(dev_info_t * dip,uint_t flag)2933  lookup_compatible(dev_info_t *dip, uint_t flag)
2934  {
2935  	int rv;
2936  	int prop_flags;
2937  	uint_t ncompatstrs;
2938  	char **compatstrpp;
2939  	char *di_compat_strp;
2940  	size_t di_compat_strlen;
2941  
2942  	if (DEVI(dip)->devi_compat_names) {
2943  		return (DDI_SUCCESS);
2944  	}
2945  
2946  	prop_flags = DDI_PROP_TYPE_STRING | DDI_PROP_DONTPASS;
2947  
2948  	if (flag & KM_NOSLEEP) {
2949  		prop_flags |= DDI_PROP_DONTSLEEP;
2950  	}
2951  
2952  	if (ndi_dev_is_prom_node(dip) == 0) {
2953  		prop_flags |= DDI_PROP_NOTPROM;
2954  	}
2955  
2956  	rv = ddi_prop_lookup_common(DDI_DEV_T_ANY, dip, prop_flags,
2957  	    "compatible", &compatstrpp, &ncompatstrs,
2958  	    ddi_prop_fm_decode_strings);
2959  
2960  	if (rv == DDI_PROP_NOT_FOUND) {
2961  		return (DDI_SUCCESS);
2962  	}
2963  
2964  	if (rv != DDI_PROP_SUCCESS) {
2965  		return (DDI_FAILURE);
2966  	}
2967  
2968  	/*
2969  	 * encode the compatible property data in the dev_info node
2970  	 */
2971  	rv = DDI_SUCCESS;
2972  	if (ncompatstrs != 0) {
2973  		di_compat_strp = encode_composite_string(compatstrpp,
2974  		    ncompatstrs, &di_compat_strlen, flag);
2975  		if (di_compat_strp != NULL) {
2976  			DEVI(dip)->devi_compat_names = di_compat_strp;
2977  			DEVI(dip)->devi_compat_length = di_compat_strlen;
2978  		} else {
2979  			rv = DDI_FAILURE;
2980  		}
2981  	}
2982  	ddi_prop_free(compatstrpp);
2983  	return (rv);
2984  }
2985  
2986  /*
2987   * Create a composite string from a list of strings.
2988   *
2989   * A composite string consists of a single buffer containing one
2990   * or more NULL terminated strings.
2991   */
2992  static char *
encode_composite_string(char ** strings,uint_t nstrings,size_t * retsz,uint_t flag)2993  encode_composite_string(char **strings, uint_t nstrings, size_t *retsz,
2994      uint_t flag)
2995  {
2996  	uint_t index;
2997  	char  **strpp;
2998  	uint_t slen;
2999  	size_t cbuf_sz = 0;
3000  	char *cbuf_p;
3001  	char *cbuf_ip;
3002  
3003  	if (strings == NULL || nstrings == 0 || retsz == NULL) {
3004  		return (NULL);
3005  	}
3006  
3007  	for (index = 0, strpp = strings; index < nstrings; index++)
3008  		cbuf_sz += strlen(*(strpp++)) + 1;
3009  
3010  	if ((cbuf_p = kmem_alloc(cbuf_sz, flag)) == NULL) {
3011  		cmn_err(CE_NOTE,
3012  		    "?failed to allocate device node compatstr");
3013  		return (NULL);
3014  	}
3015  
3016  	cbuf_ip = cbuf_p;
3017  	for (index = 0, strpp = strings; index < nstrings; index++) {
3018  		slen = strlen(*strpp);
3019  		bcopy(*(strpp++), cbuf_ip, slen);
3020  		cbuf_ip += slen;
3021  		*(cbuf_ip++) = '\0';
3022  	}
3023  
3024  	*retsz = cbuf_sz;
3025  	return (cbuf_p);
3026  }
3027  
3028  static void
link_to_driver_list(dev_info_t * dip)3029  link_to_driver_list(dev_info_t *dip)
3030  {
3031  	major_t major = DEVI(dip)->devi_major;
3032  	struct devnames *dnp;
3033  
3034  	ASSERT(major != DDI_MAJOR_T_NONE);
3035  
3036  	/*
3037  	 * Remove from orphan list
3038  	 */
3039  	if (ndi_dev_is_persistent_node(dip)) {
3040  		dnp = &orphanlist;
3041  		remove_from_dn_list(dnp, dip);
3042  	}
3043  
3044  	/*
3045  	 * Add to per driver list
3046  	 */
3047  	dnp = &devnamesp[major];
3048  	add_to_dn_list(dnp, dip);
3049  }
3050  
3051  static void
unlink_from_driver_list(dev_info_t * dip)3052  unlink_from_driver_list(dev_info_t *dip)
3053  {
3054  	major_t major = DEVI(dip)->devi_major;
3055  	struct devnames *dnp;
3056  
3057  	ASSERT(major != DDI_MAJOR_T_NONE);
3058  
3059  	/*
3060  	 * Remove from per-driver list
3061  	 */
3062  	dnp = &devnamesp[major];
3063  	remove_from_dn_list(dnp, dip);
3064  
3065  	/*
3066  	 * Add to orphan list
3067  	 */
3068  	if (ndi_dev_is_persistent_node(dip)) {
3069  		dnp = &orphanlist;
3070  		add_to_dn_list(dnp, dip);
3071  	}
3072  }
3073  
3074  /*
3075   * scan the per-driver list looking for dev_info "dip"
3076   */
3077  static dev_info_t *
in_dn_list(struct devnames * dnp,dev_info_t * dip)3078  in_dn_list(struct devnames *dnp, dev_info_t *dip)
3079  {
3080  	struct dev_info *idevi;
3081  
3082  	if ((idevi = DEVI(dnp->dn_head)) == NULL)
3083  		return (NULL);
3084  
3085  	while (idevi) {
3086  		if (idevi == DEVI(dip))
3087  			return (dip);
3088  		idevi = idevi->devi_next;
3089  	}
3090  	return (NULL);
3091  }
3092  
3093  /*
3094   * insert devinfo node 'dip' into the per-driver instance list
3095   * headed by 'dnp'
3096   *
3097   * Nodes on the per-driver list are ordered: HW - SID - PSEUDO.  The order is
3098   * required for merging of .conf file data to work properly.
3099   */
3100  static void
add_to_ordered_dn_list(struct devnames * dnp,dev_info_t * dip)3101  add_to_ordered_dn_list(struct devnames *dnp, dev_info_t *dip)
3102  {
3103  	dev_info_t **dipp;
3104  
3105  	ASSERT(mutex_owned(&(dnp->dn_lock)));
3106  
3107  	dipp = &dnp->dn_head;
3108  	if (ndi_dev_is_prom_node(dip)) {
3109  		/*
3110  		 * Find the first non-prom node or end of list
3111  		 */
3112  		while (*dipp && (ndi_dev_is_prom_node(*dipp) != 0)) {
3113  			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
3114  		}
3115  	} else if (ndi_dev_is_persistent_node(dip)) {
3116  		/*
3117  		 * Find the first non-persistent node
3118  		 */
3119  		while (*dipp && (ndi_dev_is_persistent_node(*dipp) != 0)) {
3120  			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
3121  		}
3122  	} else {
3123  		/*
3124  		 * Find the end of the list
3125  		 */
3126  		while (*dipp) {
3127  			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
3128  		}
3129  	}
3130  
3131  	DEVI(dip)->devi_next = DEVI(*dipp);
3132  	*dipp = dip;
3133  }
3134  
3135  /*
3136   * add a list of device nodes to the device node list in the
3137   * devnames structure
3138   */
3139  static void
add_to_dn_list(struct devnames * dnp,dev_info_t * dip)3140  add_to_dn_list(struct devnames *dnp, dev_info_t *dip)
3141  {
3142  	/*
3143  	 * Look to see if node already exists
3144  	 */
3145  	LOCK_DEV_OPS(&(dnp->dn_lock));
3146  	if (in_dn_list(dnp, dip)) {
3147  		cmn_err(CE_NOTE, "add_to_dn_list: node %s already in list",
3148  		    DEVI(dip)->devi_node_name);
3149  	} else {
3150  		add_to_ordered_dn_list(dnp, dip);
3151  	}
3152  	UNLOCK_DEV_OPS(&(dnp->dn_lock));
3153  }
3154  
3155  static void
remove_from_dn_list(struct devnames * dnp,dev_info_t * dip)3156  remove_from_dn_list(struct devnames *dnp, dev_info_t *dip)
3157  {
3158  	dev_info_t **plist;
3159  
3160  	LOCK_DEV_OPS(&(dnp->dn_lock));
3161  
3162  	plist = (dev_info_t **)&dnp->dn_head;
3163  	while (*plist && (*plist != dip)) {
3164  		plist = (dev_info_t **)&DEVI(*plist)->devi_next;
3165  	}
3166  
3167  	if (*plist != NULL) {
3168  		ASSERT(*plist == dip);
3169  		*plist = (dev_info_t *)(DEVI(dip)->devi_next);
3170  		DEVI(dip)->devi_next = NULL;
3171  	} else {
3172  		NDI_CONFIG_DEBUG((CE_NOTE,
3173  		    "remove_from_dn_list: node %s not found in list",
3174  		    DEVI(dip)->devi_node_name));
3175  	}
3176  
3177  	UNLOCK_DEV_OPS(&(dnp->dn_lock));
3178  }
3179  
3180  /*
3181   * Add and remove reference driver global property list
3182   */
3183  static void
add_global_props(dev_info_t * dip)3184  add_global_props(dev_info_t *dip)
3185  {
3186  	struct devnames *dnp;
3187  	ddi_prop_list_t *plist;
3188  
3189  	ASSERT(DEVI(dip)->devi_global_prop_list == NULL);
3190  	ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE);
3191  
3192  	dnp = &devnamesp[DEVI(dip)->devi_major];
3193  	LOCK_DEV_OPS(&dnp->dn_lock);
3194  	plist = dnp->dn_global_prop_ptr;
3195  	if (plist == NULL) {
3196  		UNLOCK_DEV_OPS(&dnp->dn_lock);
3197  		return;
3198  	}
3199  	i_ddi_prop_list_hold(plist, dnp);
3200  	UNLOCK_DEV_OPS(&dnp->dn_lock);
3201  
3202  	mutex_enter(&DEVI(dip)->devi_lock);
3203  	DEVI(dip)->devi_global_prop_list = plist;
3204  	mutex_exit(&DEVI(dip)->devi_lock);
3205  }
3206  
3207  static void
remove_global_props(dev_info_t * dip)3208  remove_global_props(dev_info_t *dip)
3209  {
3210  	ddi_prop_list_t *proplist;
3211  
3212  	mutex_enter(&DEVI(dip)->devi_lock);
3213  	proplist = DEVI(dip)->devi_global_prop_list;
3214  	DEVI(dip)->devi_global_prop_list = NULL;
3215  	mutex_exit(&DEVI(dip)->devi_lock);
3216  
3217  	if (proplist) {
3218  		major_t major;
3219  		struct devnames *dnp;
3220  
3221  		major = ddi_driver_major(dip);
3222  		ASSERT(major != DDI_MAJOR_T_NONE);
3223  		dnp = &devnamesp[major];
3224  		LOCK_DEV_OPS(&dnp->dn_lock);
3225  		i_ddi_prop_list_rele(proplist, dnp);
3226  		UNLOCK_DEV_OPS(&dnp->dn_lock);
3227  	}
3228  }
3229  
3230  #ifdef DEBUG
3231  /*
3232   * Set this variable to '0' to disable the optimization,
3233   * and to 2 to print debug message.
3234   */
3235  static int optimize_dtree = 1;
3236  
3237  static void
debug_dtree(dev_info_t * devi,struct dev_info * adevi,char * service)3238  debug_dtree(dev_info_t *devi, struct dev_info *adevi, char *service)
3239  {
3240  	char *adeviname, *buf;
3241  
3242  	/*
3243  	 * Don't print unless optimize dtree is set to 2+
3244  	 */
3245  	if (optimize_dtree <= 1)
3246  		return;
3247  
3248  	buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
3249  	adeviname = ddi_deviname((dev_info_t *)adevi, buf);
3250  	if (*adeviname == '\0')
3251  		adeviname = "root";
3252  
3253  	cmn_err(CE_CONT, "%s %s -> %s\n",
3254  	    ddi_deviname(devi, buf), service, adeviname);
3255  
3256  	kmem_free(buf, MAXNAMELEN);
3257  }
3258  #else /* DEBUG */
3259  #define	debug_dtree(a1, a2, a3)	 /* nothing */
3260  #endif	/* DEBUG */
3261  
3262  static void
ddi_optimize_dtree(dev_info_t * devi)3263  ddi_optimize_dtree(dev_info_t *devi)
3264  {
3265  	struct dev_info *pdevi;
3266  	struct bus_ops *b;
3267  
3268  	pdevi = DEVI(devi)->devi_parent;
3269  	ASSERT(pdevi);
3270  
3271  	/*
3272  	 * Set the unoptimized values
3273  	 */
3274  	DEVI(devi)->devi_bus_map_fault = pdevi;
3275  	DEVI(devi)->devi_bus_dma_allochdl = pdevi;
3276  	DEVI(devi)->devi_bus_dma_freehdl = pdevi;
3277  	DEVI(devi)->devi_bus_dma_bindhdl = pdevi;
3278  	DEVI(devi)->devi_bus_dma_bindfunc =
3279  	    pdevi->devi_ops->devo_bus_ops->bus_dma_bindhdl;
3280  	DEVI(devi)->devi_bus_dma_unbindhdl = pdevi;
3281  	DEVI(devi)->devi_bus_dma_unbindfunc =
3282  	    pdevi->devi_ops->devo_bus_ops->bus_dma_unbindhdl;
3283  	DEVI(devi)->devi_bus_dma_flush = pdevi;
3284  	DEVI(devi)->devi_bus_dma_win = pdevi;
3285  	DEVI(devi)->devi_bus_dma_ctl = pdevi;
3286  	DEVI(devi)->devi_bus_ctl = pdevi;
3287  
3288  #ifdef DEBUG
3289  	if (optimize_dtree == 0)
3290  		return;
3291  #endif /* DEBUG */
3292  
3293  	b = pdevi->devi_ops->devo_bus_ops;
3294  
3295  	if (i_ddi_map_fault == b->bus_map_fault) {
3296  		DEVI(devi)->devi_bus_map_fault = pdevi->devi_bus_map_fault;
3297  		debug_dtree(devi, DEVI(devi)->devi_bus_map_fault,
3298  		    "bus_map_fault");
3299  	}
3300  
3301  	if (ddi_dma_allochdl == b->bus_dma_allochdl) {
3302  		DEVI(devi)->devi_bus_dma_allochdl =
3303  		    pdevi->devi_bus_dma_allochdl;
3304  		debug_dtree(devi, DEVI(devi)->devi_bus_dma_allochdl,
3305  		    "bus_dma_allochdl");
3306  	}
3307  
3308  	if (ddi_dma_freehdl == b->bus_dma_freehdl) {
3309  		DEVI(devi)->devi_bus_dma_freehdl = pdevi->devi_bus_dma_freehdl;
3310  		debug_dtree(devi, DEVI(devi)->devi_bus_dma_freehdl,
3311  		    "bus_dma_freehdl");
3312  	}
3313  
3314  	if (ddi_dma_bindhdl == b->bus_dma_bindhdl) {
3315  		DEVI(devi)->devi_bus_dma_bindhdl = pdevi->devi_bus_dma_bindhdl;
3316  		DEVI(devi)->devi_bus_dma_bindfunc =
3317  		    pdevi->devi_bus_dma_bindhdl->devi_ops->
3318  		    devo_bus_ops->bus_dma_bindhdl;
3319  		debug_dtree(devi, DEVI(devi)->devi_bus_dma_bindhdl,
3320  		    "bus_dma_bindhdl");
3321  	}
3322  
3323  	if (ddi_dma_unbindhdl == b->bus_dma_unbindhdl) {
3324  		DEVI(devi)->devi_bus_dma_unbindhdl =
3325  		    pdevi->devi_bus_dma_unbindhdl;
3326  		DEVI(devi)->devi_bus_dma_unbindfunc =
3327  		    pdevi->devi_bus_dma_unbindhdl->devi_ops->
3328  		    devo_bus_ops->bus_dma_unbindhdl;
3329  		debug_dtree(devi, DEVI(devi)->devi_bus_dma_unbindhdl,
3330  		    "bus_dma_unbindhdl");
3331  	}
3332  
3333  	if (ddi_dma_flush == b->bus_dma_flush) {
3334  		DEVI(devi)->devi_bus_dma_flush = pdevi->devi_bus_dma_flush;
3335  		debug_dtree(devi, DEVI(devi)->devi_bus_dma_flush,
3336  		    "bus_dma_flush");
3337  	}
3338  
3339  	if (ddi_dma_win == b->bus_dma_win) {
3340  		DEVI(devi)->devi_bus_dma_win = pdevi->devi_bus_dma_win;
3341  		debug_dtree(devi, DEVI(devi)->devi_bus_dma_win,
3342  		    "bus_dma_win");
3343  	}
3344  
3345  	if (ddi_dma_mctl == b->bus_dma_ctl) {
3346  		DEVI(devi)->devi_bus_dma_ctl = pdevi->devi_bus_dma_ctl;
3347  		debug_dtree(devi, DEVI(devi)->devi_bus_dma_ctl, "bus_dma_ctl");
3348  	}
3349  
3350  	if (ddi_ctlops == b->bus_ctl) {
3351  		DEVI(devi)->devi_bus_ctl = pdevi->devi_bus_ctl;
3352  		debug_dtree(devi, DEVI(devi)->devi_bus_ctl, "bus_ctl");
3353  	}
3354  }
3355  
3356  #define	MIN_DEVINFO_LOG_SIZE	max_ncpus
3357  #define	MAX_DEVINFO_LOG_SIZE	max_ncpus * 10
3358  
3359  static void
da_log_init()3360  da_log_init()
3361  {
3362  	devinfo_log_header_t *dh;
3363  	int logsize = devinfo_log_size;
3364  
3365  	if (logsize == 0)
3366  		logsize = MIN_DEVINFO_LOG_SIZE;
3367  	else if (logsize > MAX_DEVINFO_LOG_SIZE)
3368  		logsize = MAX_DEVINFO_LOG_SIZE;
3369  
3370  	dh = kmem_alloc(logsize * PAGESIZE, KM_SLEEP);
3371  	mutex_init(&dh->dh_lock, NULL, MUTEX_DEFAULT, NULL);
3372  	dh->dh_max = ((logsize * PAGESIZE) - sizeof (*dh)) /
3373  	    sizeof (devinfo_audit_t) + 1;
3374  	dh->dh_curr = -1;
3375  	dh->dh_hits = 0;
3376  
3377  	devinfo_audit_log = dh;
3378  }
3379  
3380  /*
3381   * Log the stack trace in per-devinfo audit structure and also enter
3382   * it into a system wide log for recording the time history.
3383   */
3384  static void
da_log_enter(dev_info_t * dip)3385  da_log_enter(dev_info_t *dip)
3386  {
3387  	devinfo_audit_t *da_log, *da = DEVI(dip)->devi_audit;
3388  	devinfo_log_header_t *dh = devinfo_audit_log;
3389  
3390  	if (devinfo_audit_log == NULL)
3391  		return;
3392  
3393  	ASSERT(da != NULL);
3394  
3395  	da->da_devinfo = dip;
3396  	da->da_timestamp = gethrtime();
3397  	da->da_thread = curthread;
3398  	da->da_node_state = DEVI(dip)->devi_node_state;
3399  	da->da_device_state = DEVI(dip)->devi_state;
3400  	da->da_depth = getpcstack(da->da_stack, DDI_STACK_DEPTH);
3401  
3402  	/*
3403  	 * Copy into common log and note the location for tracing history
3404  	 */
3405  	mutex_enter(&dh->dh_lock);
3406  	dh->dh_hits++;
3407  	dh->dh_curr++;
3408  	if (dh->dh_curr >= dh->dh_max)
3409  		dh->dh_curr -= dh->dh_max;
3410  	da_log = &dh->dh_entry[dh->dh_curr];
3411  	mutex_exit(&dh->dh_lock);
3412  
3413  	bcopy(da, da_log, sizeof (devinfo_audit_t));
3414  	da->da_lastlog = da_log;
3415  }
3416  
3417  static void
attach_drivers()3418  attach_drivers()
3419  {
3420  	int i;
3421  	for (i = 0; i < devcnt; i++) {
3422  		struct devnames *dnp = &devnamesp[i];
3423  		if ((dnp->dn_flags & DN_FORCE_ATTACH) &&
3424  		    (ddi_hold_installed_driver((major_t)i) != NULL))
3425  			ddi_rele_driver((major_t)i);
3426  	}
3427  }
3428  
3429  /*
3430   * Launch a thread to force attach drivers. This avoids penalty on boot time.
3431   */
3432  void
i_ddi_forceattach_drivers()3433  i_ddi_forceattach_drivers()
3434  {
3435  
3436  	/*
3437  	 * Attach IB VHCI driver before the force-attach thread attaches the
3438  	 * IB HCA driver. IB HCA driver will fail if IB Nexus has not yet
3439  	 * been attached.
3440  	 */
3441  	(void) ddi_hold_installed_driver(ddi_name_to_major("ib"));
3442  
3443  	(void) thread_create(NULL, 0, (void (*)())attach_drivers, NULL, 0, &p0,
3444  	    TS_RUN, minclsyspri);
3445  }
3446  
3447  /*
3448   * This is a private DDI interface for optimizing boot performance.
3449   * I/O subsystem initialization is considered complete when devfsadm
3450   * is executed.
3451   *
3452   * NOTE: The start of syseventd happens to be a convenient indicator
3453   *	of the completion of I/O initialization during boot.
3454   *	The implementation should be replaced by something more robust.
3455   */
3456  int
i_ddi_io_initialized()3457  i_ddi_io_initialized()
3458  {
3459  	extern int sysevent_daemon_init;
3460  	return (sysevent_daemon_init);
3461  }
3462  
3463  /*
3464   * May be used to determine system boot state
3465   * "Available" means the system is for the most part up
3466   * and initialized, with all system services either up or
3467   * capable of being started.  This state is set by devfsadm
3468   * during the boot process.  The /dev filesystem infers
3469   * from this when implicit reconfig can be performed,
3470   * ie, devfsadm can be invoked.  Please avoid making
3471   * further use of this unless it's really necessary.
3472   */
3473  int
i_ddi_sysavail()3474  i_ddi_sysavail()
3475  {
3476  	return (devname_state & DS_SYSAVAIL);
3477  }
3478  
3479  /*
3480   * May be used to determine if boot is a reconfigure boot.
3481   */
3482  int
i_ddi_reconfig()3483  i_ddi_reconfig()
3484  {
3485  	return (devname_state & DS_RECONFIG);
3486  }
3487  
3488  /*
3489   * Note system services are up, inform /dev.
3490   */
3491  void
i_ddi_set_sysavail()3492  i_ddi_set_sysavail()
3493  {
3494  	if ((devname_state & DS_SYSAVAIL) == 0) {
3495  		devname_state |= DS_SYSAVAIL;
3496  		sdev_devstate_change();
3497  	}
3498  }
3499  
3500  /*
3501   * Note reconfiguration boot, inform /dev.
3502   */
3503  void
i_ddi_set_reconfig()3504  i_ddi_set_reconfig()
3505  {
3506  	if ((devname_state & DS_RECONFIG) == 0) {
3507  		devname_state |= DS_RECONFIG;
3508  		sdev_devstate_change();
3509  	}
3510  }
3511  
3512  
3513  /*
3514   * device tree walking
3515   */
3516  
3517  struct walk_elem {
3518  	struct walk_elem *next;
3519  	dev_info_t *dip;
3520  };
3521  
3522  static void
free_list(struct walk_elem * list)3523  free_list(struct walk_elem *list)
3524  {
3525  	while (list) {
3526  		struct walk_elem *next = list->next;
3527  		kmem_free(list, sizeof (*list));
3528  		list = next;
3529  	}
3530  }
3531  
3532  static void
append_node(struct walk_elem ** list,dev_info_t * dip)3533  append_node(struct walk_elem **list, dev_info_t *dip)
3534  {
3535  	struct walk_elem *tail;
3536  	struct walk_elem *elem = kmem_alloc(sizeof (*elem), KM_SLEEP);
3537  
3538  	elem->next = NULL;
3539  	elem->dip = dip;
3540  
3541  	if (*list == NULL) {
3542  		*list = elem;
3543  		return;
3544  	}
3545  
3546  	tail = *list;
3547  	while (tail->next)
3548  		tail = tail->next;
3549  
3550  	tail->next = elem;
3551  }
3552  
3553  /*
3554   * The implementation of ddi_walk_devs().
3555   */
3556  static int
walk_devs(dev_info_t * dip,int (* f)(dev_info_t *,void *),void * arg,int do_locking)3557  walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg,
3558      int do_locking)
3559  {
3560  	struct walk_elem *head = NULL;
3561  
3562  	/*
3563  	 * Do it in two passes. First pass invoke callback on each
3564  	 * dip on the sibling list. Second pass invoke callback on
3565  	 * children of each dip.
3566  	 */
3567  	while (dip) {
3568  		switch ((*f)(dip, arg)) {
3569  		case DDI_WALK_TERMINATE:
3570  			free_list(head);
3571  			return (DDI_WALK_TERMINATE);
3572  
3573  		case DDI_WALK_PRUNESIB:
3574  			/* ignore sibling by setting dip to NULL */
3575  			append_node(&head, dip);
3576  			dip = NULL;
3577  			break;
3578  
3579  		case DDI_WALK_PRUNECHILD:
3580  			/* don't worry about children */
3581  			dip = ddi_get_next_sibling(dip);
3582  			break;
3583  
3584  		case DDI_WALK_CONTINUE:
3585  		default:
3586  			append_node(&head, dip);
3587  			dip = ddi_get_next_sibling(dip);
3588  			break;
3589  		}
3590  
3591  	}
3592  
3593  	/* second pass */
3594  	while (head) {
3595  		struct walk_elem *next = head->next;
3596  
3597  		if (do_locking)
3598  			ndi_devi_enter(head->dip);
3599  		if (walk_devs(ddi_get_child(head->dip), f, arg, do_locking) ==
3600  		    DDI_WALK_TERMINATE) {
3601  			if (do_locking)
3602  				ndi_devi_exit(head->dip);
3603  			free_list(head);
3604  			return (DDI_WALK_TERMINATE);
3605  		}
3606  		if (do_locking)
3607  			ndi_devi_exit(head->dip);
3608  		kmem_free(head, sizeof (*head));
3609  		head = next;
3610  	}
3611  
3612  	return (DDI_WALK_CONTINUE);
3613  }
3614  
3615  /*
3616   * This general-purpose routine traverses the tree of dev_info nodes,
3617   * starting from the given node, and calls the given function for each
3618   * node that it finds with the current node and the pointer arg (which
3619   * can point to a structure of information that the function
3620   * needs) as arguments.
3621   *
3622   * It does the walk a layer at a time, not depth-first. The given function
3623   * must return one of the following values:
3624   *	DDI_WALK_CONTINUE
3625   *	DDI_WALK_PRUNESIB
3626   *	DDI_WALK_PRUNECHILD
3627   *	DDI_WALK_TERMINATE
3628   *
3629   * N.B. Since we walk the sibling list, the caller must ensure that
3630   *	the parent of dip is held against changes, unless the parent
3631   *	is rootnode.  ndi_devi_enter() on the parent is sufficient.
3632   *
3633   *	To avoid deadlock situations, caller must not attempt to
3634   *	configure/unconfigure/remove device node in (*f)(), nor should
3635   *	it attempt to recurse on other nodes in the system. Any
3636   *	ndi_devi_enter() done by (*f)() must occur 'at-or-below' the
3637   *	node entered prior to ddi_walk_devs(). Furthermore, if (*f)()
3638   *	does any multi-threading (in framework *or* in driver) then the
3639   *	ndi_devi_enter() calls done by dependent threads must be
3640   *	'strictly-below'.
3641   *
3642   *	This is not callable from device autoconfiguration routines.
3643   *	They include, but not limited to, _init(9e), _fini(9e), probe(9e),
3644   *	attach(9e), and detach(9e).
3645   */
3646  void
ddi_walk_devs(dev_info_t * dip,int (* f)(dev_info_t *,void *),void * arg)3647  ddi_walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg)
3648  {
3649  
3650  	ASSERT(dip == NULL || ddi_get_parent(dip) == NULL ||
3651  	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
3652  
3653  	(void) walk_devs(dip, f, arg, 1);
3654  }
3655  
3656  /*
3657   * This is a general-purpose routine traverses the per-driver list
3658   * and calls the given function for each node. must return one of
3659   * the following values:
3660   *	DDI_WALK_CONTINUE
3661   *	DDI_WALK_TERMINATE
3662   *
3663   * N.B. The same restrictions from ddi_walk_devs() apply.
3664   */
3665  void
e_ddi_walk_driver(char * drv,int (* f)(dev_info_t *,void *),void * arg)3666  e_ddi_walk_driver(char *drv, int (*f)(dev_info_t *, void *), void *arg)
3667  {
3668  	major_t major;
3669  	struct devnames *dnp;
3670  	dev_info_t *dip;
3671  
3672  	major = ddi_name_to_major(drv);
3673  	if (major == DDI_MAJOR_T_NONE)
3674  		return;
3675  
3676  	dnp = &devnamesp[major];
3677  	LOCK_DEV_OPS(&dnp->dn_lock);
3678  	dip = dnp->dn_head;
3679  	while (dip) {
3680  		ndi_hold_devi(dip);
3681  		UNLOCK_DEV_OPS(&dnp->dn_lock);
3682  		if ((*f)(dip, arg) == DDI_WALK_TERMINATE) {
3683  			ndi_rele_devi(dip);
3684  			return;
3685  		}
3686  		LOCK_DEV_OPS(&dnp->dn_lock);
3687  		ndi_rele_devi(dip);
3688  		dip = ddi_get_next(dip);
3689  	}
3690  	UNLOCK_DEV_OPS(&dnp->dn_lock);
3691  }
3692  
3693  struct preroot_walk_block_devices_arg {
3694  	int (*prwb_func)(const char *, void *);
3695  	void *prwb_arg;
3696  };
3697  
3698  static int
preroot_walk_block_devices_walker(dev_info_t * dip,void * arg)3699  preroot_walk_block_devices_walker(dev_info_t *dip, void *arg)
3700  {
3701  	struct preroot_walk_block_devices_arg *prwb = arg;
3702  
3703  	if (i_ddi_devi_class(dip) == NULL ||
3704  	    strcmp(i_ddi_devi_class(dip), ESC_DISK) != 0) {
3705  		/*
3706  		 * We do not think that this is a disk.
3707  		 */
3708  		return (DDI_WALK_CONTINUE);
3709  	}
3710  
3711  	for (struct ddi_minor_data *md = DEVI(dip)->devi_minor; md != NULL;
3712  	    md = md->next) {
3713  		if (md->ddm_spec_type != S_IFBLK) {
3714  			/*
3715  			 * We don't want the raw version of any block device.
3716  			 */
3717  			continue;
3718  		}
3719  
3720  		/*
3721  		 * The node type taxonomy is hierarchical, with each level
3722  		 * separated by colons.  Nodes of interest are either of the
3723  		 * BLOCK type, or are prefixed with that type.
3724  		 */
3725  		if (strcmp(md->ddm_node_type, DDI_NT_BLOCK) != 0 &&
3726  		    strncmp(md->ddm_node_type, DDI_NT_BLOCK ":",
3727  		    strlen(DDI_NT_BLOCK ":")) != 0) {
3728  			/*
3729  			 * This minor node does not represent a block device.
3730  			 */
3731  			continue;
3732  		}
3733  
3734  		char buf[MAXPATHLEN];
3735  		int r;
3736  		if ((r = prwb->prwb_func(ddi_pathname_minor(md, buf),
3737  		    prwb->prwb_arg)) == PREROOT_WALK_BLOCK_DEVICES_CANCEL) {
3738  			/*
3739  			 * The consumer does not need any more minor nodes.
3740  			 */
3741  			return (DDI_WALK_TERMINATE);
3742  		}
3743  		VERIFY3S(r, ==, PREROOT_WALK_BLOCK_DEVICES_NEXT);
3744  	}
3745  
3746  	return (DDI_WALK_CONTINUE);
3747  }
3748  
3749  /*
3750   * Private routine for ZFS when it needs to attach and scan all of the block
3751   * device minors in the system while looking for vdev labels.
3752   *
3753   * The callback function accepts a physical device path and the context
3754   * argument (arg) passed to this function; it should return
3755   * PREROOT_WALK_BLOCK_DEVICES_NEXT when more devices are required and
3756   * PREROOT_WALK_BLOCK_DEVICES_CANCEL to stop the walk.
3757   */
3758  void
preroot_walk_block_devices(int (* callback)(const char *,void *),void * arg)3759  preroot_walk_block_devices(int (*callback)(const char *, void *), void *arg)
3760  {
3761  	/*
3762  	 * First, force everything which can attach to do so.  The device class
3763  	 * is not derived until at least one minor mode is created, so we
3764  	 * cannot walk the device tree looking for a device class of ESC_DISK
3765  	 * until everything is attached.
3766  	 */
3767  	(void) ndi_devi_config(ddi_root_node(), NDI_CONFIG | NDI_DEVI_PERSIST |
3768  	    NDI_NO_EVENT | NDI_DRV_CONF_REPROBE);
3769  
3770  	struct preroot_walk_block_devices_arg prwb;
3771  	prwb.prwb_func = callback;
3772  	prwb.prwb_arg = arg;
3773  
3774  	ddi_walk_devs(ddi_root_node(), preroot_walk_block_devices_walker,
3775  	    &prwb);
3776  }
3777  
3778  /*
3779   * argument to i_find_devi, a devinfo node search callback function.
3780   */
3781  struct match_info {
3782  	dev_info_t	*dip;		/* result */
3783  	char		*nodename;	/* if non-null, nodename must match */
3784  	int		instance;	/* if != -1, instance must match */
3785  	int		attached;	/* if != 0, i_ddi_devi_attached() */
3786  };
3787  
3788  static int
i_find_devi(dev_info_t * dip,void * arg)3789  i_find_devi(dev_info_t *dip, void *arg)
3790  {
3791  	struct match_info *info = (struct match_info *)arg;
3792  
3793  	if (((info->nodename == NULL) ||
3794  	    (strcmp(ddi_node_name(dip), info->nodename) == 0)) &&
3795  	    ((info->instance == -1) ||
3796  	    (ddi_get_instance(dip) == info->instance)) &&
3797  	    ((info->attached == 0) || i_ddi_devi_attached(dip))) {
3798  		info->dip = dip;
3799  		ndi_hold_devi(dip);
3800  		return (DDI_WALK_TERMINATE);
3801  	}
3802  
3803  	return (DDI_WALK_CONTINUE);
3804  }
3805  
3806  /*
3807   * Find dip with a known node name and instance and return with it held
3808   */
3809  dev_info_t *
ddi_find_devinfo(char * nodename,int instance,int attached)3810  ddi_find_devinfo(char *nodename, int instance, int attached)
3811  {
3812  	struct match_info	info;
3813  
3814  	info.nodename = nodename;
3815  	info.instance = instance;
3816  	info.attached = attached;
3817  	info.dip = NULL;
3818  
3819  	ddi_walk_devs(ddi_root_node(), i_find_devi, &info);
3820  	return (info.dip);
3821  }
3822  
3823  extern ib_boot_prop_t *iscsiboot_prop;
3824  static void
i_ddi_parse_iscsi_name(char * name,char ** nodename,char ** addrname,char ** minorname)3825  i_ddi_parse_iscsi_name(char *name, char **nodename, char **addrname,
3826      char **minorname)
3827  {
3828  	char *cp, *colon;
3829  	static char nulladdrname[] = "";
3830  
3831  	/* default values */
3832  	if (nodename)
3833  		*nodename = name;
3834  	if (addrname)
3835  		*addrname = nulladdrname;
3836  	if (minorname)
3837  		*minorname = NULL;
3838  
3839  	cp = colon = name;
3840  	while (*cp != '\0') {
3841  		if (addrname && *cp == '@') {
3842  			*addrname = cp + 1;
3843  			*cp = '\0';
3844  		} else if (minorname && *cp == ':') {
3845  			*minorname = cp + 1;
3846  			colon = cp;
3847  		}
3848  		++cp;
3849  	}
3850  	if (colon != name) {
3851  		*colon = '\0';
3852  	}
3853  }
3854  
3855  /*
3856   * Parse for name, addr, and minor names. Some args may be NULL.
3857   */
3858  void
i_ddi_parse_name(char * name,char ** nodename,char ** addrname,char ** minorname)3859  i_ddi_parse_name(char *name, char **nodename, char **addrname, char **minorname)
3860  {
3861  	char *cp;
3862  	static char nulladdrname[] = "";
3863  
3864  	/* default values */
3865  	if (nodename)
3866  		*nodename = name;
3867  	if (addrname)
3868  		*addrname = nulladdrname;
3869  	if (minorname)
3870  		*minorname = NULL;
3871  
3872  	cp = name;
3873  	while (*cp != '\0') {
3874  		if (addrname && *cp == '@') {
3875  			*addrname = cp + 1;
3876  			*cp = '\0';
3877  		} else if (minorname && *cp == ':') {
3878  			*minorname = cp + 1;
3879  			*cp = '\0';
3880  		}
3881  		++cp;
3882  	}
3883  }
3884  
3885  static char *
child_path_to_driver(dev_info_t * parent,char * child_name,char * unit_address)3886  child_path_to_driver(dev_info_t *parent, char *child_name, char *unit_address)
3887  {
3888  	char *p, *drvname = NULL;
3889  	major_t maj;
3890  
3891  	/*
3892  	 * Construct the pathname and ask the implementation
3893  	 * if it can do a driver = f(pathname) for us, if not
3894  	 * we'll just default to using the node-name that
3895  	 * was given to us.  We want to do this first to
3896  	 * allow the platform to use 'generic' names for
3897  	 * legacy device drivers.
3898  	 */
3899  	p = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
3900  	(void) ddi_pathname(parent, p);
3901  	(void) strcat(p, "/");
3902  	(void) strcat(p, child_name);
3903  	if (unit_address && *unit_address) {
3904  		(void) strcat(p, "@");
3905  		(void) strcat(p, unit_address);
3906  	}
3907  
3908  	/*
3909  	 * Get the binding. If there is none, return the child_name
3910  	 * and let the caller deal with it.
3911  	 */
3912  	maj = path_to_major(p);
3913  
3914  	kmem_free(p, MAXPATHLEN);
3915  
3916  	if (maj != DDI_MAJOR_T_NONE)
3917  		drvname = ddi_major_to_name(maj);
3918  	if (drvname == NULL)
3919  		drvname = child_name;
3920  
3921  	return (drvname);
3922  }
3923  
3924  
3925  #define	PCI_EX_CLASS	"pciexclass"
3926  #define	PCI_EX		"pciex"
3927  #define	PCI_CLASS	"pciclass"
3928  #define	PCI		"pci"
3929  
3930  int
ddi_is_pci_dip(dev_info_t * dip)3931  ddi_is_pci_dip(dev_info_t *dip)
3932  {
3933  	char	*prop = NULL;
3934  
3935  	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
3936  	    "compatible", &prop) == DDI_PROP_SUCCESS) {
3937  		ASSERT(prop);
3938  		if (strncmp(prop, PCI_EX_CLASS, sizeof (PCI_EX_CLASS) - 1)
3939  		    == 0 ||
3940  		    strncmp(prop, PCI_EX, sizeof (PCI_EX)- 1)
3941  		    == 0 ||
3942  		    strncmp(prop, PCI_CLASS, sizeof (PCI_CLASS) - 1)
3943  		    == 0 ||
3944  		    strncmp(prop, PCI, sizeof (PCI) - 1)
3945  		    == 0) {
3946  			ddi_prop_free(prop);
3947  			return (1);
3948  		}
3949  	}
3950  
3951  	if (prop != NULL) {
3952  		ddi_prop_free(prop);
3953  	}
3954  
3955  	return (0);
3956  }
3957  
3958  /*
3959   * Given the pathname of a device, fill in the dev_info_t value and/or the
3960   * dev_t value and/or the spectype, depending on which parameters are non-NULL.
3961   * If there is an error, this function returns -1.
3962   *
3963   * NOTE: If this function returns the dev_info_t structure, then it
3964   * does so with a hold on the devi. Caller should ensure that they get
3965   * decremented via ddi_release_devi() or ndi_rele_devi();
3966   *
3967   * This function can be invoked in the boot case for a pathname without
3968   * device argument (:xxxx), traditionally treated as a minor name.
3969   * In this case, we do the following
3970   * (1) search the minor node of type DDM_DEFAULT.
3971   * (2) if no DDM_DEFAULT minor exists, then the first non-alias minor is chosen.
3972   * (3) if neither exists, a dev_t is faked with minor number = instance.
3973   * As of S9 FCS, no instance of #1 exists. #2 is used by several platforms
3974   * to default the boot partition to :a possibly by other OBP definitions.
3975   * #3 is used for booting off network interfaces, most SPARC network
3976   * drivers support Style-2 only, so only DDM_ALIAS minor exists.
3977   *
3978   * It is possible for OBP to present device args at the end of the path as
3979   * well as in the middle. For example, with IB the following strings are
3980   * valid boot paths.
3981   *	a /pci@8,700000/ib@1,2:port=1,pkey=ff,dhcp,...
3982   *	b /pci@8,700000/ib@1,1:port=1/ioc@xxxxxx,yyyyyyy:dhcp
3983   * Case (a), we first look for minor node "port=1,pkey...".
3984   * Failing that, we will pass "port=1,pkey..." to the bus_config
3985   * entry point of ib (HCA) driver.
3986   * Case (b), configure ib@1,1 as usual. Then invoke ib's bus_config
3987   * with argument "ioc@xxxxxxx,yyyyyyy:port=1". After configuring
3988   * the ioc, look for minor node dhcp. If not found, pass ":dhcp"
3989   * to ioc's bus_config entry point.
3990   */
3991  int
resolve_pathname(const char * pathname,dev_info_t ** dipp,dev_t * devtp,int * spectypep)3992  resolve_pathname(const char *pathname, dev_info_t **dipp, dev_t *devtp,
3993      int *spectypep)
3994  {
3995  	int			error;
3996  	dev_info_t		*parent, *child;
3997  	struct pathname		pn;
3998  	char			*component, *config_name;
3999  	char			*minorname = NULL;
4000  	char			*prev_minor = NULL;
4001  	dev_t			devt = NODEV;
4002  	int			spectype;
4003  	struct ddi_minor_data	*dmn;
4004  
4005  	if (*pathname != '/')
4006  		return (EINVAL);
4007  	parent = ddi_root_node();	/* Begin at the top of the tree */
4008  
4009  	if (error = pn_get(pathname, UIO_SYSSPACE, &pn))
4010  		return (error);
4011  	pn_skipslash(&pn);
4012  
4013  	ASSERT(i_ddi_devi_attached(parent));
4014  	ndi_hold_devi(parent);
4015  
4016  	component = kmem_alloc(MAXNAMELEN, KM_SLEEP);
4017  	config_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
4018  
4019  	while (pn_pathleft(&pn)) {
4020  		/* remember prev minor (:xxx) in the middle of path */
4021  		if (minorname)
4022  			prev_minor = i_ddi_strdup(minorname, KM_SLEEP);
4023  
4024  		/* Get component and chop off minorname */
4025  		(void) pn_getcomponent(&pn, component);
4026  		if ((iscsiboot_prop != NULL) &&
4027  		    (strcmp((DEVI(parent)->devi_node_name), "iscsi") == 0)) {
4028  			i_ddi_parse_iscsi_name(component, NULL, NULL,
4029  			    &minorname);
4030  		} else {
4031  			i_ddi_parse_name(component, NULL, NULL, &minorname);
4032  		}
4033  		if (prev_minor == NULL) {
4034  			(void) snprintf(config_name, MAXNAMELEN, "%s",
4035  			    component);
4036  		} else {
4037  			(void) snprintf(config_name, MAXNAMELEN, "%s:%s",
4038  			    component, prev_minor);
4039  			kmem_free(prev_minor, strlen(prev_minor) + 1);
4040  			prev_minor = NULL;
4041  		}
4042  
4043  		/*
4044  		 * Find and configure the child
4045  		 */
4046  		if (ndi_devi_config_one(parent, config_name, &child,
4047  		    NDI_PROMNAME | NDI_NO_EVENT) != NDI_SUCCESS) {
4048  			ndi_rele_devi(parent);
4049  			pn_free(&pn);
4050  			kmem_free(component, MAXNAMELEN);
4051  			kmem_free(config_name, MAXNAMELEN);
4052  			return (-1);
4053  		}
4054  
4055  		ASSERT(i_ddi_devi_attached(child));
4056  		ndi_rele_devi(parent);
4057  		parent = child;
4058  		pn_skipslash(&pn);
4059  	}
4060  
4061  	/*
4062  	 * First look for a minor node matching minorname.
4063  	 * Failing that, try to pass minorname to bus_config().
4064  	 */
4065  	if (minorname && i_ddi_minorname_to_devtspectype(parent,
4066  	    minorname, &devt, &spectype) == DDI_FAILURE) {
4067  		(void) snprintf(config_name, MAXNAMELEN, "%s", minorname);
4068  		if (ndi_devi_config_obp_args(parent,
4069  		    config_name, &child, 0) != NDI_SUCCESS) {
4070  			ndi_rele_devi(parent);
4071  			pn_free(&pn);
4072  			kmem_free(component, MAXNAMELEN);
4073  			kmem_free(config_name, MAXNAMELEN);
4074  			NDI_CONFIG_DEBUG((CE_NOTE,
4075  			    "%s: minor node not found\n", pathname));
4076  			return (-1);
4077  		}
4078  		minorname = NULL;	/* look for default minor */
4079  		ASSERT(i_ddi_devi_attached(child));
4080  		ndi_rele_devi(parent);
4081  		parent = child;
4082  	}
4083  
4084  	if (devtp || spectypep) {
4085  		if (minorname == NULL) {
4086  			/*
4087  			 * Search for a default entry with an active
4088  			 * ndi_devi_enter to protect the devi_minor list.
4089  			 */
4090  			ndi_devi_enter(parent);
4091  			for (dmn = DEVI(parent)->devi_minor; dmn;
4092  			    dmn = dmn->next) {
4093  				if (dmn->type == DDM_DEFAULT) {
4094  					devt = dmn->ddm_dev;
4095  					spectype = dmn->ddm_spec_type;
4096  					break;
4097  				}
4098  			}
4099  
4100  			if (devt == NODEV) {
4101  				/*
4102  				 * No default minor node, try the first one;
4103  				 * else, assume 1-1 instance-minor mapping
4104  				 */
4105  				dmn = DEVI(parent)->devi_minor;
4106  				if (dmn && ((dmn->type == DDM_MINOR) ||
4107  				    (dmn->type == DDM_INTERNAL_PATH))) {
4108  					devt = dmn->ddm_dev;
4109  					spectype = dmn->ddm_spec_type;
4110  				} else {
4111  					devt = makedevice(
4112  					    DEVI(parent)->devi_major,
4113  					    ddi_get_instance(parent));
4114  					spectype = S_IFCHR;
4115  				}
4116  			}
4117  			ndi_devi_exit(parent);
4118  		}
4119  		if (devtp)
4120  			*devtp = devt;
4121  		if (spectypep)
4122  			*spectypep = spectype;
4123  	}
4124  
4125  	pn_free(&pn);
4126  	kmem_free(component, MAXNAMELEN);
4127  	kmem_free(config_name, MAXNAMELEN);
4128  
4129  	/*
4130  	 * If there is no error, return the appropriate parameters
4131  	 */
4132  	if (dipp != NULL)
4133  		*dipp = parent;
4134  	else {
4135  		/*
4136  		 * We should really keep the ref count to keep the node from
4137  		 * detaching but ddi_pathname_to_dev_t() specifies a NULL dipp,
4138  		 * so we have no way of passing back the held dip.  Not holding
4139  		 * the dip allows detaches to occur - which can cause problems
4140  		 * for subsystems which call ddi_pathname_to_dev_t (console).
4141  		 *
4142  		 * Instead of holding the dip, we place a ddi-no-autodetach
4143  		 * property on the node to prevent auto detaching.
4144  		 *
4145  		 * The right fix is to remove ddi_pathname_to_dev_t and replace
4146  		 * it, and all references, with a call that specifies a dipp.
4147  		 * In addition, the callers of this new interfaces would then
4148  		 * need to call ndi_rele_devi when the reference is complete.
4149  		 *
4150  		 */
4151  		(void) ddi_prop_update_int(DDI_DEV_T_NONE, parent,
4152  		    DDI_NO_AUTODETACH, 1);
4153  		ndi_rele_devi(parent);
4154  	}
4155  
4156  	return (0);
4157  }
4158  
4159  /*
4160   * Given the pathname of a device, return the dev_t of the corresponding
4161   * device.  Returns NODEV on failure.
4162   *
4163   * Note that this call sets the DDI_NO_AUTODETACH property on the devinfo node.
4164   */
4165  dev_t
ddi_pathname_to_dev_t(char * pathname)4166  ddi_pathname_to_dev_t(char *pathname)
4167  {
4168  	dev_t devt;
4169  	int error;
4170  
4171  	error = resolve_pathname(pathname, NULL, &devt, NULL);
4172  
4173  	return (error ? NODEV : devt);
4174  }
4175  
4176  /*
4177   * Translate a prom pathname to kernel devfs pathname.
4178   * Caller is assumed to allocate devfspath memory of
4179   * size at least MAXPATHLEN
4180   *
4181   * The prom pathname may not include minor name, but
4182   * devfs pathname has a minor name portion.
4183   */
4184  int
i_ddi_prompath_to_devfspath(char * prompath,char * devfspath)4185  i_ddi_prompath_to_devfspath(char *prompath, char *devfspath)
4186  {
4187  	dev_t		devt = (dev_t)NODEV;
4188  	dev_info_t	*dip = NULL;
4189  	char		*minor_name = NULL;
4190  	int		spectype;
4191  	int		error;
4192  
4193  	error = resolve_pathname(prompath, &dip, &devt, &spectype);
4194  	if (error)
4195  		return (DDI_FAILURE);
4196  	ASSERT(dip && devt != NODEV);
4197  
4198  	/*
4199  	 * Get in-kernel devfs pathname
4200  	 */
4201  	(void) ddi_pathname(dip, devfspath);
4202  
4203  	ndi_devi_enter(dip);
4204  	minor_name = i_ddi_devtspectype_to_minorname(dip, devt, spectype);
4205  	if (minor_name) {
4206  		(void) strcat(devfspath, ":");
4207  		(void) strcat(devfspath, minor_name);
4208  	} else {
4209  		/*
4210  		 * If minor_name is NULL, we have an alias minor node.
4211  		 * So manufacture a path to the corresponding clone minor.
4212  		 */
4213  		(void) snprintf(devfspath, MAXPATHLEN, "%s:%s",
4214  		    CLONE_PATH, ddi_driver_name(dip));
4215  	}
4216  	ndi_devi_exit(dip);
4217  
4218  	/* release hold from resolve_pathname() */
4219  	ndi_rele_devi(dip);
4220  	return (0);
4221  }
4222  
4223  /*
4224   * This function is intended to identify drivers that must quiesce for fast
4225   * reboot to succeed.  It does not claim to have more knowledge about the device
4226   * than its driver.  If a driver has implemented quiesce(), it will be invoked;
4227   * if a so identified driver does not manage any device that needs to be
4228   * quiesced, it must explicitly set its devo_quiesce dev_op to
4229   * ddi_quiesce_not_needed.
4230   */
4231  static int skip_pseudo = 1;	/* Skip pseudo devices */
4232  static int skip_non_hw = 1;	/* Skip devices with no hardware property */
4233  static int
should_implement_quiesce(dev_info_t * dip)4234  should_implement_quiesce(dev_info_t *dip)
4235  {
4236  	struct dev_info *devi = DEVI(dip);
4237  	dev_info_t *pdip;
4238  
4239  	/*
4240  	 * If dip is pseudo and skip_pseudo is set, driver doesn't have to
4241  	 * implement quiesce().
4242  	 */
4243  	if (skip_pseudo &&
4244  	    strncmp(ddi_binding_name(dip), "pseudo", sizeof ("pseudo")) == 0)
4245  		return (0);
4246  
4247  	/*
4248  	 * If parent dip is pseudo and skip_pseudo is set, driver doesn't have
4249  	 * to implement quiesce().
4250  	 */
4251  	if (skip_pseudo && (pdip = ddi_get_parent(dip)) != NULL &&
4252  	    strncmp(ddi_binding_name(pdip), "pseudo", sizeof ("pseudo")) == 0)
4253  		return (0);
4254  
4255  	/*
4256  	 * If not attached, driver doesn't have to implement quiesce().
4257  	 */
4258  	if (!i_ddi_devi_attached(dip))
4259  		return (0);
4260  
4261  	/*
4262  	 * If dip has no hardware property and skip_non_hw is set,
4263  	 * driver doesn't have to implement quiesce().
4264  	 */
4265  	if (skip_non_hw && devi->devi_hw_prop_ptr == NULL)
4266  		return (0);
4267  
4268  	return (1);
4269  }
4270  
4271  static int
driver_has_quiesce(struct dev_ops * ops)4272  driver_has_quiesce(struct dev_ops *ops)
4273  {
4274  	if ((ops->devo_rev >= 4) && (ops->devo_quiesce != nodev) &&
4275  	    (ops->devo_quiesce != NULL) && (ops->devo_quiesce != nulldev) &&
4276  	    (ops->devo_quiesce != ddi_quiesce_not_supported))
4277  		return (1);
4278  	else
4279  		return (0);
4280  }
4281  
4282  /*
4283   * Check to see if a driver has implemented the quiesce() DDI function.
4284   */
4285  int
check_driver_quiesce(dev_info_t * dip,void * arg)4286  check_driver_quiesce(dev_info_t *dip, void *arg)
4287  {
4288  	struct dev_ops *ops;
4289  
4290  	if (!should_implement_quiesce(dip))
4291  		return (DDI_WALK_CONTINUE);
4292  
4293  	if ((ops = ddi_get_driver(dip)) == NULL)
4294  		return (DDI_WALK_CONTINUE);
4295  
4296  	if (driver_has_quiesce(ops)) {
4297  		if ((quiesce_debug & 0x2) == 0x2) {
4298  			if (ops->devo_quiesce == ddi_quiesce_not_needed)
4299  				cmn_err(CE_CONT, "%s does not need to be "
4300  				    "quiesced", ddi_driver_name(dip));
4301  			else
4302  				cmn_err(CE_CONT, "%s has quiesce routine",
4303  				    ddi_driver_name(dip));
4304  		}
4305  	} else {
4306  		if (arg != NULL)
4307  			*((int *)arg) = -1;
4308  		cmn_err(CE_WARN, "%s has no quiesce()", ddi_driver_name(dip));
4309  	}
4310  
4311  	return (DDI_WALK_CONTINUE);
4312  }
4313  
4314  /*
4315   * Quiesce device.
4316   */
4317  static void
quiesce_one_device(dev_info_t * dip,void * arg)4318  quiesce_one_device(dev_info_t *dip, void *arg)
4319  {
4320  	struct dev_ops *ops;
4321  	int should_quiesce = 0;
4322  
4323  	/*
4324  	 * If the device is not attached it doesn't need to be quiesced.
4325  	 */
4326  	if (!i_ddi_devi_attached(dip))
4327  		return;
4328  
4329  	if ((ops = ddi_get_driver(dip)) == NULL)
4330  		return;
4331  
4332  	should_quiesce = should_implement_quiesce(dip);
4333  
4334  	/*
4335  	 * If there's an implementation of quiesce(), always call it even if
4336  	 * some of the drivers don't have quiesce() or quiesce() have failed
4337  	 * so we can do force fast reboot.  The implementation of quiesce()
4338  	 * should not negatively affect a regular reboot.
4339  	 */
4340  	if (driver_has_quiesce(ops)) {
4341  		int rc = DDI_SUCCESS;
4342  
4343  		if (ops->devo_quiesce == ddi_quiesce_not_needed)
4344  			return;
4345  
4346  		rc = devi_quiesce(dip);
4347  
4348  		if (rc != DDI_SUCCESS && should_quiesce) {
4349  #ifdef DEBUG
4350  			cmn_err(CE_WARN, "quiesce() failed for %s%d",
4351  			    ddi_driver_name(dip), ddi_get_instance(dip));
4352  #endif /* DEBUG */
4353  			if (arg != NULL)
4354  				*((int *)arg) = -1;
4355  		}
4356  	} else if (should_quiesce && arg != NULL) {
4357  		*((int *)arg) = -1;
4358  	}
4359  }
4360  
4361  /*
4362   * Traverse the dev info tree in a breadth-first manner so that we quiesce
4363   * children first.  All subtrees under the parent of dip will be quiesced.
4364   */
4365  void
quiesce_devices(dev_info_t * dip,void * arg)4366  quiesce_devices(dev_info_t *dip, void *arg)
4367  {
4368  	/*
4369  	 * if we're reached here, the device tree better not be changing.
4370  	 * so either devinfo_freeze better be set or we better be panicking.
4371  	 */
4372  	ASSERT(devinfo_freeze || panicstr);
4373  
4374  	for (; dip != NULL; dip = ddi_get_next_sibling(dip)) {
4375  		quiesce_devices(ddi_get_child(dip), arg);
4376  
4377  		quiesce_one_device(dip, arg);
4378  	}
4379  }
4380  
4381  /*
4382   * Reset all the pure leaf drivers on the system at halt time
4383   */
4384  static int
reset_leaf_device(dev_info_t * dip,void * arg)4385  reset_leaf_device(dev_info_t *dip, void *arg)
4386  {
4387  	_NOTE(ARGUNUSED(arg))
4388  	struct dev_ops *ops;
4389  
4390  	/* if the device doesn't need to be reset then there's nothing to do */
4391  	if (!DEVI_NEED_RESET(dip))
4392  		return (DDI_WALK_CONTINUE);
4393  
4394  	/*
4395  	 * if the device isn't a char/block device or doesn't have a
4396  	 * reset entry point then there's nothing to do.
4397  	 */
4398  	ops = ddi_get_driver(dip);
4399  	if ((ops == NULL) || (ops->devo_cb_ops == NULL) ||
4400  	    (ops->devo_reset == nodev) || (ops->devo_reset == nulldev) ||
4401  	    (ops->devo_reset == NULL))
4402  		return (DDI_WALK_CONTINUE);
4403  
4404  	if (DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip)) {
4405  		static char path[MAXPATHLEN];
4406  
4407  		/*
4408  		 * bad news, this device has blocked in it's attach or
4409  		 * detach routine, which means it not safe to call it's
4410  		 * devo_reset() entry point.
4411  		 */
4412  		cmn_err(CE_WARN, "unable to reset device: %s",
4413  		    ddi_pathname(dip, path));
4414  		return (DDI_WALK_CONTINUE);
4415  	}
4416  
4417  	NDI_CONFIG_DEBUG((CE_NOTE, "resetting %s%d\n",
4418  	    ddi_driver_name(dip), ddi_get_instance(dip)));
4419  
4420  	(void) devi_reset(dip, DDI_RESET_FORCE);
4421  	return (DDI_WALK_CONTINUE);
4422  }
4423  
4424  void
reset_leaves(void)4425  reset_leaves(void)
4426  {
4427  	/*
4428  	 * if we're reached here, the device tree better not be changing.
4429  	 * so either devinfo_freeze better be set or we better be panicking.
4430  	 */
4431  	ASSERT(devinfo_freeze || panicstr);
4432  
4433  	(void) walk_devs(top_devinfo, reset_leaf_device, NULL, 0);
4434  }
4435  
4436  
4437  /*
4438   * devtree_freeze() must be called before quiesce_devices() and reset_leaves()
4439   * during a normal system shutdown.  It attempts to ensure that there are no
4440   * outstanding attach or detach operations in progress when quiesce_devices() or
4441   * reset_leaves()is invoked.  It must be called before the system becomes
4442   * single-threaded because device attach and detach are multi-threaded
4443   * operations.	(note that during system shutdown the system doesn't actually
4444   * become single-thread since other threads still exist, but the shutdown thread
4445   * will disable preemption for itself, raise it's pil, and stop all the other
4446   * cpus in the system there by effectively making the system single-threaded.)
4447   */
4448  void
devtree_freeze(void)4449  devtree_freeze(void)
4450  {
4451  	int delayed = 0;
4452  
4453  	/* if we're panicking then the device tree isn't going to be changing */
4454  	if (panicstr)
4455  		return;
4456  
4457  	/* stop all dev_info state changes in the device tree */
4458  	devinfo_freeze = gethrtime();
4459  
4460  	/*
4461  	 * if we're not panicking and there are on-going attach or detach
4462  	 * operations, wait for up to 3 seconds for them to finish.  This
4463  	 * is a randomly chosen interval but this should be ok because:
4464  	 * - 3 seconds is very small relative to the deadman timer.
4465  	 * - normal attach and detach operations should be very quick.
4466  	 * - attach and detach operations are fairly rare.
4467  	 */
4468  	while (!panicstr && atomic_add_long_nv(&devinfo_attach_detach, 0) &&
4469  	    (delayed < 3)) {
4470  		delayed += 1;
4471  
4472  		/* do a sleeping wait for one second */
4473  		ASSERT(!servicing_interrupt());
4474  		delay(drv_usectohz(MICROSEC));
4475  	}
4476  }
4477  
4478  static int
bind_dip(dev_info_t * dip,void * arg)4479  bind_dip(dev_info_t *dip, void *arg)
4480  {
4481  	_NOTE(ARGUNUSED(arg))
4482  	char	*path;
4483  	major_t	major, pmajor;
4484  
4485  	/*
4486  	 * If the node is currently bound to the wrong driver, try to unbind
4487  	 * so that we can rebind to the correct driver.
4488  	 */
4489  	if (i_ddi_node_state(dip) >= DS_BOUND) {
4490  		major = ddi_compatible_driver_major(dip, NULL);
4491  		if ((DEVI(dip)->devi_major == major) &&
4492  		    (i_ddi_node_state(dip) >= DS_INITIALIZED)) {
4493  			/*
4494  			 * Check for a path-oriented driver alias that
4495  			 * takes precedence over current driver binding.
4496  			 */
4497  			path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4498  			(void) ddi_pathname(dip, path);
4499  			pmajor = ddi_name_to_major(path);
4500  			if (driver_active(pmajor))
4501  				major = pmajor;
4502  			kmem_free(path, MAXPATHLEN);
4503  		}
4504  
4505  		/* attempt unbind if current driver is incorrect */
4506  		if (driver_active(major) &&
4507  		    (major != DEVI(dip)->devi_major))
4508  			(void) ndi_devi_unbind_driver(dip);
4509  	}
4510  
4511  	/* If unbound, try to bind to a driver */
4512  	if (i_ddi_node_state(dip) < DS_BOUND)
4513  		(void) ndi_devi_bind_driver(dip, 0);
4514  
4515  	return (DDI_WALK_CONTINUE);
4516  }
4517  
4518  void
i_ddi_bind_devs(void)4519  i_ddi_bind_devs(void)
4520  {
4521  	/* flush devfs so that ndi_devi_unbind_driver will work when possible */
4522  	(void) devfs_clean(top_devinfo, NULL, 0);
4523  
4524  	ddi_walk_devs(top_devinfo, bind_dip, (void *)NULL);
4525  }
4526  
4527  /* callback data for unbind_children_by_alias() */
4528  typedef struct unbind_data {
4529  	major_t	drv_major;
4530  	char	*drv_alias;
4531  	int	ndevs_bound;
4532  	int	unbind_errors;
4533  } unbind_data_t;
4534  
4535  /*
4536   * A utility function provided for testing and support convenience
4537   * Called for each device during an upgrade_drv -d bound to the alias
4538   * that cannot be unbound due to device in use.
4539   */
4540  static void
unbind_alias_dev_in_use(dev_info_t * dip,char * alias)4541  unbind_alias_dev_in_use(dev_info_t *dip, char *alias)
4542  {
4543  	if (moddebug & MODDEBUG_BINDING) {
4544  		cmn_err(CE_CONT, "%s%d: state %d: bound to %s\n",
4545  		    ddi_driver_name(dip), ddi_get_instance(dip),
4546  		    i_ddi_node_state(dip), alias);
4547  	}
4548  }
4549  
4550  /*
4551   * walkdevs callback for unbind devices bound to specific driver
4552   * and alias.  Invoked within the context of update_drv -d <alias>.
4553   */
4554  static int
unbind_children_by_alias(dev_info_t * dip,void * arg)4555  unbind_children_by_alias(dev_info_t *dip, void *arg)
4556  {
4557  	dev_info_t	*cdip;
4558  	dev_info_t	*next;
4559  	unbind_data_t	*ub = (unbind_data_t *)(uintptr_t)arg;
4560  	int		rv;
4561  
4562  	/*
4563  	 * We are called from update_drv to try to unbind a specific
4564  	 * set of aliases for a driver.  Unbind what persistent nodes
4565  	 * we can, and return the number of nodes which cannot be unbound.
4566  	 * If not all nodes can be unbound, update_drv leaves the
4567  	 * state of the driver binding files unchanged, except in
4568  	 * the case of -f.
4569  	 */
4570  	ndi_devi_enter(dip);
4571  	for (cdip = ddi_get_child(dip); cdip; cdip = next) {
4572  		next = ddi_get_next_sibling(cdip);
4573  		if ((ddi_driver_major(cdip) != ub->drv_major) ||
4574  		    (strcmp(DEVI(cdip)->devi_node_name, ub->drv_alias) != 0))
4575  			continue;
4576  		if (i_ddi_node_state(cdip) >= DS_BOUND) {
4577  			rv = ndi_devi_unbind_driver(cdip);
4578  			if (rv != DDI_SUCCESS ||
4579  			    (i_ddi_node_state(cdip) >= DS_BOUND)) {
4580  				unbind_alias_dev_in_use(cdip, ub->drv_alias);
4581  				ub->ndevs_bound++;
4582  				continue;
4583  			}
4584  			if (ndi_dev_is_persistent_node(cdip) == 0)
4585  				(void) ddi_remove_child(cdip, 0);
4586  		}
4587  	}
4588  	ndi_devi_exit(dip);
4589  
4590  	return (DDI_WALK_CONTINUE);
4591  }
4592  
4593  /*
4594   * Unbind devices by driver & alias
4595   * Context: update_drv [-f] -d -i <alias> <driver>
4596   */
4597  int
i_ddi_unbind_devs_by_alias(major_t major,char * alias)4598  i_ddi_unbind_devs_by_alias(major_t major, char *alias)
4599  {
4600  	unbind_data_t	*ub;
4601  	int		rv;
4602  
4603  	ub = kmem_zalloc(sizeof (*ub), KM_SLEEP);
4604  	ub->drv_major = major;
4605  	ub->drv_alias = alias;
4606  	ub->ndevs_bound = 0;
4607  	ub->unbind_errors = 0;
4608  
4609  	/* flush devfs so that ndi_devi_unbind_driver will work when possible */
4610  	(void) devfs_clean(top_devinfo, NULL, 0);
4611  	ddi_walk_devs(top_devinfo, unbind_children_by_alias,
4612  	    (void *)(uintptr_t)ub);
4613  
4614  	/* return the number of devices remaining bound to the alias */
4615  	rv = ub->ndevs_bound + ub->unbind_errors;
4616  	kmem_free(ub, sizeof (*ub));
4617  	return (rv);
4618  }
4619  
4620  /*
4621   * walkdevs callback for unbind devices by driver
4622   */
4623  static int
unbind_children_by_driver(dev_info_t * dip,void * arg)4624  unbind_children_by_driver(dev_info_t *dip, void *arg)
4625  {
4626  	dev_info_t	*cdip;
4627  	dev_info_t	*next;
4628  	major_t		major = (major_t)(uintptr_t)arg;
4629  	int		rv;
4630  
4631  	/*
4632  	 * We are called either from rem_drv or update_drv when reloading
4633  	 * a driver.conf file. In either case, we unbind persistent nodes
4634  	 * and destroy .conf nodes. In the case of rem_drv, this will be
4635  	 * the final state. In the case of update_drv,	i_ddi_bind_devs()
4636  	 * may be invoked later to re-enumerate (new) driver.conf rebind
4637  	 * persistent nodes.
4638  	 */
4639  	ndi_devi_enter(dip);
4640  	for (cdip = ddi_get_child(dip); cdip; cdip = next) {
4641  		next = ddi_get_next_sibling(cdip);
4642  		if (ddi_driver_major(cdip) != major)
4643  			continue;
4644  		if (i_ddi_node_state(cdip) >= DS_BOUND) {
4645  			rv = ndi_devi_unbind_driver(cdip);
4646  			if (rv == DDI_FAILURE ||
4647  			    (i_ddi_node_state(cdip) >= DS_BOUND))
4648  				continue;
4649  			if (ndi_dev_is_persistent_node(cdip) == 0)
4650  				(void) ddi_remove_child(cdip, 0);
4651  		}
4652  	}
4653  	ndi_devi_exit(dip);
4654  
4655  	return (DDI_WALK_CONTINUE);
4656  }
4657  
4658  /*
4659   * Unbind devices by driver
4660   * Context: rem_drv or unload driver.conf
4661   */
4662  void
i_ddi_unbind_devs(major_t major)4663  i_ddi_unbind_devs(major_t major)
4664  {
4665  	/* flush devfs so that ndi_devi_unbind_driver will work when possible */
4666  	(void) devfs_clean(top_devinfo, NULL, 0);
4667  	ddi_walk_devs(top_devinfo, unbind_children_by_driver,
4668  	    (void *)(uintptr_t)major);
4669  }
4670  
4671  /*
4672   * I/O Hotplug control
4673   */
4674  
4675  /*
4676   * create and attach a dev_info node from a .conf file spec
4677   */
4678  static void
init_spec_child(dev_info_t * pdip,struct hwc_spec * specp,uint_t flags)4679  init_spec_child(dev_info_t *pdip, struct hwc_spec *specp, uint_t flags)
4680  {
4681  	_NOTE(ARGUNUSED(flags))
4682  	dev_info_t *dip;
4683  	char *node_name;
4684  
4685  	if (((node_name = specp->hwc_devi_name) == NULL) ||
4686  	    (ddi_name_to_major(node_name) == DDI_MAJOR_T_NONE)) {
4687  		char *tmp = node_name;
4688  		if (tmp == NULL)
4689  			tmp = "<none>";
4690  		cmn_err(CE_CONT,
4691  		    "init_spec_child: parent=%s, bad spec (%s)\n",
4692  		    ddi_node_name(pdip), tmp);
4693  		return;
4694  	}
4695  
4696  	dip = i_ddi_alloc_node(pdip, node_name, (pnode_t)DEVI_PSEUDO_NODEID,
4697  	    -1, specp->hwc_devi_sys_prop_ptr, KM_SLEEP);
4698  
4699  	if (dip == NULL)
4700  		return;
4701  
4702  	if (ddi_initchild(pdip, dip) != DDI_SUCCESS)
4703  		(void) ddi_remove_child(dip, 0);
4704  }
4705  
4706  /*
4707   * Lookup hwc specs from hash tables and make children from the spec
4708   * Because some .conf children are "merge" nodes, we also initialize
4709   * .conf children to merge properties onto hardware nodes.
4710   *
4711   * The pdip must be held busy.
4712   */
4713  int
i_ndi_make_spec_children(dev_info_t * pdip,uint_t flags)4714  i_ndi_make_spec_children(dev_info_t *pdip, uint_t flags)
4715  {
4716  	extern struct hwc_spec *hwc_get_child_spec(dev_info_t *, major_t);
4717  	struct hwc_spec		*list, *spec;
4718  
4719  	ndi_devi_enter(pdip);
4720  	if (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN) {
4721  		ndi_devi_exit(pdip);
4722  		return (DDI_SUCCESS);
4723  	}
4724  
4725  	list = hwc_get_child_spec(pdip, DDI_MAJOR_T_NONE);
4726  	for (spec = list; spec != NULL; spec = spec->hwc_next) {
4727  		init_spec_child(pdip, spec, flags);
4728  	}
4729  	hwc_free_spec_list(list);
4730  
4731  	mutex_enter(&DEVI(pdip)->devi_lock);
4732  	DEVI(pdip)->devi_flags |= DEVI_MADE_CHILDREN;
4733  	mutex_exit(&DEVI(pdip)->devi_lock);
4734  	ndi_devi_exit(pdip);
4735  	return (DDI_SUCCESS);
4736  }
4737  
4738  /*
4739   * Run initchild on all child nodes such that instance assignment
4740   * for multiport network cards are contiguous.
4741   *
4742   * The pdip must be held busy.
4743   */
4744  static void
i_ndi_init_hw_children(dev_info_t * pdip,uint_t flags)4745  i_ndi_init_hw_children(dev_info_t *pdip, uint_t flags)
4746  {
4747  	dev_info_t *dip;
4748  
4749  	ASSERT(DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
4750  
4751  	/* contiguous instance assignment */
4752  	e_ddi_enter_instance();
4753  	dip = ddi_get_child(pdip);
4754  	while (dip) {
4755  		if (ndi_dev_is_persistent_node(dip))
4756  			(void) i_ndi_config_node(dip, DS_INITIALIZED, flags);
4757  		dip = ddi_get_next_sibling(dip);
4758  	}
4759  	e_ddi_exit_instance();
4760  }
4761  
4762  /*
4763   * report device status
4764   */
4765  static void
i_ndi_devi_report_status_change(dev_info_t * dip,char * path)4766  i_ndi_devi_report_status_change(dev_info_t *dip, char *path)
4767  {
4768  	char *status;
4769  
4770  	if (!DEVI_NEED_REPORT(dip) ||
4771  	    (i_ddi_node_state(dip) < DS_INITIALIZED) ||
4772  	    ndi_dev_is_hidden_node(dip)) {
4773  		return;
4774  	}
4775  
4776  	/* Invalidate the devinfo snapshot cache */
4777  	i_ddi_di_cache_invalidate();
4778  
4779  	if (DEVI_IS_DEVICE_REMOVED(dip)) {
4780  		status = "removed";
4781  	} else if (DEVI_IS_DEVICE_OFFLINE(dip)) {
4782  		status = "offline";
4783  	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
4784  		status = "down";
4785  	} else if (DEVI_IS_BUS_QUIESCED(dip)) {
4786  		status = "quiesced";
4787  	} else if (DEVI_IS_BUS_DOWN(dip)) {
4788  		status = "down";
4789  	} else if (i_ddi_devi_attached(dip)) {
4790  		status = "online";
4791  	} else {
4792  		status = "unknown";
4793  	}
4794  
4795  	if (path == NULL) {
4796  		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4797  		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
4798  		    ddi_pathname(dip, path), ddi_driver_name(dip),
4799  		    ddi_get_instance(dip), status);
4800  		kmem_free(path, MAXPATHLEN);
4801  	} else {
4802  		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
4803  		    path, ddi_driver_name(dip),
4804  		    ddi_get_instance(dip), status);
4805  	}
4806  
4807  	mutex_enter(&(DEVI(dip)->devi_lock));
4808  	DEVI_REPORT_DONE(dip);
4809  	mutex_exit(&(DEVI(dip)->devi_lock));
4810  }
4811  
4812  /*
4813   * log a notification that a dev_info node has been configured.
4814   */
4815  static int
i_log_devfs_add_devinfo(dev_info_t * dip,uint_t flags)4816  i_log_devfs_add_devinfo(dev_info_t *dip, uint_t flags)
4817  {
4818  	int			se_err;
4819  	char			*pathname;
4820  	sysevent_t		*ev;
4821  	sysevent_id_t		eid;
4822  	sysevent_value_t	se_val;
4823  	sysevent_attr_list_t	*ev_attr_list = NULL;
4824  	char			*class_name;
4825  	int			no_transport = 0;
4826  
4827  	ASSERT(dip && ddi_get_parent(dip) &&
4828  	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
4829  
4830  	/* do not generate ESC_DEVFS_DEVI_ADD event during boot */
4831  	if (!i_ddi_io_initialized())
4832  		return (DDI_SUCCESS);
4833  
4834  	/* Invalidate the devinfo snapshot cache */
4835  	i_ddi_di_cache_invalidate();
4836  
4837  	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_ADD, EP_DDI, SE_SLEEP);
4838  
4839  	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4840  
4841  	(void) ddi_pathname(dip, pathname);
4842  	ASSERT(strlen(pathname));
4843  
4844  	se_val.value_type = SE_DATA_TYPE_STRING;
4845  	se_val.value.sv_string = pathname;
4846  	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
4847  	    &se_val, SE_SLEEP) != 0) {
4848  		goto fail;
4849  	}
4850  
4851  	/* add the device class attribute */
4852  	if ((class_name = i_ddi_devi_class(dip)) != NULL) {
4853  		se_val.value_type = SE_DATA_TYPE_STRING;
4854  		se_val.value.sv_string = class_name;
4855  
4856  		if (sysevent_add_attr(&ev_attr_list,
4857  		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
4858  			sysevent_free_attr(ev_attr_list);
4859  			goto fail;
4860  		}
4861  	}
4862  
4863  	/*
4864  	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
4865  	 * in which case the branch event will be logged by the caller
4866  	 * after the entire branch has been configured.
4867  	 */
4868  	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
4869  		/*
4870  		 * Instead of logging a separate branch event just add
4871  		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
4872  		 * generate a EC_DEV_BRANCH event.
4873  		 */
4874  		se_val.value_type = SE_DATA_TYPE_INT32;
4875  		se_val.value.sv_int32 = 1;
4876  		if (sysevent_add_attr(&ev_attr_list,
4877  		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
4878  			sysevent_free_attr(ev_attr_list);
4879  			goto fail;
4880  		}
4881  	}
4882  
4883  	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
4884  		sysevent_free_attr(ev_attr_list);
4885  		goto fail;
4886  	}
4887  
4888  	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
4889  		if (se_err == SE_NO_TRANSPORT)
4890  			no_transport = 1;
4891  		goto fail;
4892  	}
4893  
4894  	sysevent_free(ev);
4895  	kmem_free(pathname, MAXPATHLEN);
4896  
4897  	return (DDI_SUCCESS);
4898  
4899  fail:
4900  	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_ADD event for %s%s",
4901  	    pathname, (no_transport) ? " (syseventd not responding)" : "");
4902  
4903  	cmn_err(CE_WARN, "/dev may not be current for driver %s. "
4904  	    "Run devfsadm -i %s",
4905  	    ddi_driver_name(dip), ddi_driver_name(dip));
4906  
4907  	sysevent_free(ev);
4908  	kmem_free(pathname, MAXPATHLEN);
4909  	return (DDI_SUCCESS);
4910  }
4911  
4912  /*
4913   * log a notification that a dev_info node has been unconfigured.
4914   */
4915  static int
i_log_devfs_remove_devinfo(char * pathname,char * class_name,char * driver_name,int instance,uint_t flags)4916  i_log_devfs_remove_devinfo(char *pathname, char *class_name, char *driver_name,
4917      int instance, uint_t flags)
4918  {
4919  	sysevent_t		*ev;
4920  	sysevent_id_t		eid;
4921  	sysevent_value_t	se_val;
4922  	sysevent_attr_list_t	*ev_attr_list = NULL;
4923  	int			se_err;
4924  	int			no_transport = 0;
4925  
4926  	if (!i_ddi_io_initialized())
4927  		return (DDI_SUCCESS);
4928  
4929  	/* Invalidate the devinfo snapshot cache */
4930  	i_ddi_di_cache_invalidate();
4931  
4932  	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_REMOVE, EP_DDI, SE_SLEEP);
4933  
4934  	se_val.value_type = SE_DATA_TYPE_STRING;
4935  	se_val.value.sv_string = pathname;
4936  	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
4937  	    &se_val, SE_SLEEP) != 0) {
4938  		goto fail;
4939  	}
4940  
4941  	if (class_name) {
4942  		/* add the device class, driver name and instance attributes */
4943  
4944  		se_val.value_type = SE_DATA_TYPE_STRING;
4945  		se_val.value.sv_string = class_name;
4946  		if (sysevent_add_attr(&ev_attr_list,
4947  		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
4948  			sysevent_free_attr(ev_attr_list);
4949  			goto fail;
4950  		}
4951  
4952  		se_val.value_type = SE_DATA_TYPE_STRING;
4953  		se_val.value.sv_string = driver_name;
4954  		if (sysevent_add_attr(&ev_attr_list,
4955  		    DEVFS_DRIVER_NAME, &se_val, SE_SLEEP) != 0) {
4956  			sysevent_free_attr(ev_attr_list);
4957  			goto fail;
4958  		}
4959  
4960  		se_val.value_type = SE_DATA_TYPE_INT32;
4961  		se_val.value.sv_int32 = instance;
4962  		if (sysevent_add_attr(&ev_attr_list,
4963  		    DEVFS_INSTANCE, &se_val, SE_SLEEP) != 0) {
4964  			sysevent_free_attr(ev_attr_list);
4965  			goto fail;
4966  		}
4967  	}
4968  
4969  	/*
4970  	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
4971  	 * in which case the branch event will be logged by the caller
4972  	 * after the entire branch has been unconfigured.
4973  	 */
4974  	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
4975  		/*
4976  		 * Instead of logging a separate branch event just add
4977  		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
4978  		 * generate a EC_DEV_BRANCH event.
4979  		 */
4980  		se_val.value_type = SE_DATA_TYPE_INT32;
4981  		se_val.value.sv_int32 = 1;
4982  		if (sysevent_add_attr(&ev_attr_list,
4983  		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
4984  			sysevent_free_attr(ev_attr_list);
4985  			goto fail;
4986  		}
4987  	}
4988  
4989  	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
4990  		sysevent_free_attr(ev_attr_list);
4991  		goto fail;
4992  	}
4993  
4994  	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
4995  		if (se_err == SE_NO_TRANSPORT)
4996  			no_transport = 1;
4997  		goto fail;
4998  	}
4999  
5000  	sysevent_free(ev);
5001  	return (DDI_SUCCESS);
5002  
5003  fail:
5004  	sysevent_free(ev);
5005  	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_REMOVE event for %s%s",
5006  	    pathname, (no_transport) ? " (syseventd not responding)" : "");
5007  	return (DDI_SUCCESS);
5008  }
5009  
5010  static void
i_ddi_log_devfs_device_remove(dev_info_t * dip)5011  i_ddi_log_devfs_device_remove(dev_info_t *dip)
5012  {
5013  	char	*path;
5014  
5015  	ASSERT(dip && ddi_get_parent(dip) &&
5016  	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
5017  	ASSERT(DEVI_IS_DEVICE_REMOVED(dip));
5018  
5019  	ASSERT(i_ddi_node_state(dip) >= DS_INITIALIZED);
5020  	if (i_ddi_node_state(dip) < DS_INITIALIZED)
5021  		return;
5022  
5023  	/* Inform LDI_EV_DEVICE_REMOVE callbacks. */
5024  	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0, LDI_EV_DEVICE_REMOVE,
5025  	    LDI_EV_SUCCESS, NULL);
5026  
5027  	/* Generate EC_DEVFS_DEVI_REMOVE sysevent. */
5028  	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5029  	(void) i_log_devfs_remove_devinfo(ddi_pathname(dip, path),
5030  	    i_ddi_devi_class(dip), (char *)ddi_driver_name(dip),
5031  	    ddi_get_instance(dip), 0);
5032  	kmem_free(path, MAXPATHLEN);
5033  }
5034  
5035  static void
i_ddi_log_devfs_device_insert(dev_info_t * dip)5036  i_ddi_log_devfs_device_insert(dev_info_t *dip)
5037  {
5038  	ASSERT(dip && ddi_get_parent(dip) &&
5039  	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
5040  	ASSERT(!DEVI_IS_DEVICE_REMOVED(dip));
5041  
5042  	(void) i_log_devfs_add_devinfo(dip, 0);
5043  }
5044  
5045  
5046  /*
5047   * log an event that a dev_info branch has been configured or unconfigured.
5048   */
5049  static int
i_log_devfs_branch(char * node_path,char * subclass)5050  i_log_devfs_branch(char *node_path, char *subclass)
5051  {
5052  	int se_err;
5053  	sysevent_t *ev;
5054  	sysevent_id_t eid;
5055  	sysevent_value_t se_val;
5056  	sysevent_attr_list_t *ev_attr_list = NULL;
5057  	int no_transport = 0;
5058  
5059  	/* do not generate the event during boot */
5060  	if (!i_ddi_io_initialized())
5061  		return (DDI_SUCCESS);
5062  
5063  	/* Invalidate the devinfo snapshot cache */
5064  	i_ddi_di_cache_invalidate();
5065  
5066  	ev = sysevent_alloc(EC_DEVFS, subclass, EP_DDI, SE_SLEEP);
5067  
5068  	se_val.value_type = SE_DATA_TYPE_STRING;
5069  	se_val.value.sv_string = node_path;
5070  
5071  	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
5072  	    &se_val, SE_SLEEP) != 0) {
5073  		goto fail;
5074  	}
5075  
5076  	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
5077  		sysevent_free_attr(ev_attr_list);
5078  		goto fail;
5079  	}
5080  
5081  	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
5082  		if (se_err == SE_NO_TRANSPORT)
5083  			no_transport = 1;
5084  		goto fail;
5085  	}
5086  
5087  	sysevent_free(ev);
5088  	return (DDI_SUCCESS);
5089  
5090  fail:
5091  	cmn_err(CE_WARN, "failed to log %s branch event for %s%s",
5092  	    subclass, node_path,
5093  	    (no_transport) ? " (syseventd not responding)" : "");
5094  
5095  	sysevent_free(ev);
5096  	return (DDI_FAILURE);
5097  }
5098  
5099  /*
5100   * log an event that a dev_info tree branch has been configured.
5101   */
5102  static int
i_log_devfs_branch_add(dev_info_t * dip)5103  i_log_devfs_branch_add(dev_info_t *dip)
5104  {
5105  	char *node_path;
5106  	int rv;
5107  
5108  	node_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5109  	(void) ddi_pathname(dip, node_path);
5110  	rv = i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_ADD);
5111  	kmem_free(node_path, MAXPATHLEN);
5112  
5113  	return (rv);
5114  }
5115  
5116  /*
5117   * log an event that a dev_info tree branch has been unconfigured.
5118   */
5119  static int
i_log_devfs_branch_remove(char * node_path)5120  i_log_devfs_branch_remove(char *node_path)
5121  {
5122  	return (i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_REMOVE));
5123  }
5124  
5125  /*
5126   * enqueue the dip's deviname on the branch event queue.
5127   */
5128  static struct brevq_node *
brevq_enqueue(struct brevq_node ** brevqp,dev_info_t * dip,struct brevq_node * child)5129  brevq_enqueue(struct brevq_node **brevqp, dev_info_t *dip,
5130      struct brevq_node *child)
5131  {
5132  	struct brevq_node *brn;
5133  	char *deviname;
5134  
5135  	deviname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
5136  	(void) ddi_deviname(dip, deviname);
5137  
5138  	brn = kmem_zalloc(sizeof (*brn), KM_SLEEP);
5139  	brn->brn_deviname = i_ddi_strdup(deviname, KM_SLEEP);
5140  	kmem_free(deviname, MAXNAMELEN);
5141  	brn->brn_child = child;
5142  	brn->brn_sibling = *brevqp;
5143  	*brevqp = brn;
5144  
5145  	return (brn);
5146  }
5147  
5148  /*
5149   * free the memory allocated for the elements on the branch event queue.
5150   */
5151  static void
free_brevq(struct brevq_node * brevq)5152  free_brevq(struct brevq_node *brevq)
5153  {
5154  	struct brevq_node *brn, *next_brn;
5155  
5156  	for (brn = brevq; brn != NULL; brn = next_brn) {
5157  		next_brn = brn->brn_sibling;
5158  		ASSERT(brn->brn_child == NULL);
5159  		kmem_free(brn->brn_deviname, strlen(brn->brn_deviname) + 1);
5160  		kmem_free(brn, sizeof (*brn));
5161  	}
5162  }
5163  
5164  /*
5165   * log the events queued up on the branch event queue and free the
5166   * associated memory.
5167   *
5168   * node_path must have been allocated with at least MAXPATHLEN bytes.
5169   */
5170  static void
log_and_free_brevq(char * node_path,struct brevq_node * brevq)5171  log_and_free_brevq(char *node_path, struct brevq_node *brevq)
5172  {
5173  	struct brevq_node *brn;
5174  	char *p;
5175  
5176  	p = node_path + strlen(node_path);
5177  	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
5178  		(void) strcpy(p, brn->brn_deviname);
5179  		(void) i_log_devfs_branch_remove(node_path);
5180  	}
5181  	*p = '\0';
5182  
5183  	free_brevq(brevq);
5184  }
5185  
5186  /*
5187   * log the events queued up on the branch event queue and free the
5188   * associated memory. Same as the previous function but operates on dip.
5189   */
5190  static void
log_and_free_brevq_dip(dev_info_t * dip,struct brevq_node * brevq)5191  log_and_free_brevq_dip(dev_info_t *dip, struct brevq_node *brevq)
5192  {
5193  	char *path;
5194  
5195  	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5196  	(void) ddi_pathname(dip, path);
5197  	log_and_free_brevq(path, brevq);
5198  	kmem_free(path, MAXPATHLEN);
5199  }
5200  
5201  /*
5202   * log the outstanding branch remove events for the grand children of the dip
5203   * and free the associated memory.
5204   */
5205  static void
log_and_free_br_events_on_grand_children(dev_info_t * dip,struct brevq_node * brevq)5206  log_and_free_br_events_on_grand_children(dev_info_t *dip,
5207      struct brevq_node *brevq)
5208  {
5209  	struct brevq_node *brn;
5210  	char *path;
5211  	char *p;
5212  
5213  	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5214  	(void) ddi_pathname(dip, path);
5215  	p = path + strlen(path);
5216  	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
5217  		if (brn->brn_child) {
5218  			(void) strcpy(p, brn->brn_deviname);
5219  			/* now path contains the node path to the dip's child */
5220  			log_and_free_brevq(path, brn->brn_child);
5221  			brn->brn_child = NULL;
5222  		}
5223  	}
5224  	kmem_free(path, MAXPATHLEN);
5225  }
5226  
5227  /*
5228   * log and cleanup branch remove events for the grand children of the dip.
5229   */
5230  static void
cleanup_br_events_on_grand_children(dev_info_t * dip,struct brevq_node ** brevqp)5231  cleanup_br_events_on_grand_children(dev_info_t *dip, struct brevq_node **brevqp)
5232  {
5233  	dev_info_t *child;
5234  	struct brevq_node *brevq, *brn, *prev_brn, *next_brn;
5235  	char *path;
5236  
5237  	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5238  	prev_brn = NULL;
5239  	brevq = *brevqp;
5240  
5241  	ndi_devi_enter(dip);
5242  	for (brn = brevq; brn != NULL; brn = next_brn) {
5243  		next_brn = brn->brn_sibling;
5244  		for (child = ddi_get_child(dip); child != NULL;
5245  		    child = ddi_get_next_sibling(child)) {
5246  			if (i_ddi_node_state(child) >= DS_INITIALIZED) {
5247  				(void) ddi_deviname(child, path);
5248  				if (strcmp(path, brn->brn_deviname) == 0)
5249  					break;
5250  			}
5251  		}
5252  
5253  		if (child != NULL && !(DEVI_EVREMOVE(child))) {
5254  			/*
5255  			 * Event state is not REMOVE. So branch remove event
5256  			 * is not going be generated on brn->brn_child.
5257  			 * If any branch remove events were queued up on
5258  			 * brn->brn_child log them and remove the brn
5259  			 * from the queue.
5260  			 */
5261  			if (brn->brn_child) {
5262  				(void) ddi_pathname(dip, path);
5263  				(void) strcat(path, brn->brn_deviname);
5264  				log_and_free_brevq(path, brn->brn_child);
5265  			}
5266  
5267  			if (prev_brn)
5268  				prev_brn->brn_sibling = next_brn;
5269  			else
5270  				*brevqp = next_brn;
5271  
5272  			kmem_free(brn->brn_deviname,
5273  			    strlen(brn->brn_deviname) + 1);
5274  			kmem_free(brn, sizeof (*brn));
5275  		} else {
5276  			/*
5277  			 * Free up the outstanding branch remove events
5278  			 * queued on brn->brn_child since brn->brn_child
5279  			 * itself is eligible for branch remove event.
5280  			 */
5281  			if (brn->brn_child) {
5282  				free_brevq(brn->brn_child);
5283  				brn->brn_child = NULL;
5284  			}
5285  			prev_brn = brn;
5286  		}
5287  	}
5288  
5289  	ndi_devi_exit(dip);
5290  	kmem_free(path, MAXPATHLEN);
5291  }
5292  
5293  static int
need_remove_event(dev_info_t * dip,int flags)5294  need_remove_event(dev_info_t *dip, int flags)
5295  {
5296  	if ((flags & (NDI_NO_EVENT | NDI_AUTODETACH)) == 0 &&
5297  	    (flags & (NDI_DEVI_OFFLINE | NDI_UNCONFIG | NDI_DEVI_REMOVE)) &&
5298  	    !(DEVI_EVREMOVE(dip)))
5299  		return (1);
5300  	else
5301  		return (0);
5302  }
5303  
5304  /*
5305   * Unconfigure children/descendants of the dip.
5306   *
5307   * If the operation involves a branch event NDI_BRANCH_EVENT_OP is set
5308   * through out the unconfiguration. On successful return *brevqp is set to
5309   * a queue of dip's child devinames for which branch remove events need
5310   * to be generated.
5311   */
5312  static int
devi_unconfig_branch(dev_info_t * dip,dev_info_t ** dipp,int flags,struct brevq_node ** brevqp)5313  devi_unconfig_branch(dev_info_t *dip, dev_info_t **dipp, int flags,
5314      struct brevq_node **brevqp)
5315  {
5316  	int rval;
5317  
5318  	*brevqp = NULL;
5319  
5320  	if ((!(flags & NDI_BRANCH_EVENT_OP)) && need_remove_event(dip, flags))
5321  		flags |= NDI_BRANCH_EVENT_OP;
5322  
5323  	if (flags & NDI_BRANCH_EVENT_OP) {
5324  		rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
5325  		    brevqp);
5326  
5327  		if (rval != NDI_SUCCESS && (*brevqp)) {
5328  			log_and_free_brevq_dip(dip, *brevqp);
5329  			*brevqp = NULL;
5330  		}
5331  	} else
5332  		rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
5333  		    NULL);
5334  
5335  	return (rval);
5336  }
5337  
5338  /*
5339   * If the dip is already bound to a driver transition to DS_INITIALIZED
5340   * in order to generate an event in the case where the node was left in
5341   * DS_BOUND state since boot (never got attached) and the node is now
5342   * being offlined.
5343   */
5344  static void
init_bound_node_ev(dev_info_t * pdip,dev_info_t * dip,int flags)5345  init_bound_node_ev(dev_info_t *pdip, dev_info_t *dip, int flags)
5346  {
5347  	if (need_remove_event(dip, flags) &&
5348  	    i_ddi_node_state(dip) == DS_BOUND &&
5349  	    i_ddi_devi_attached(pdip) && !DEVI_IS_DEVICE_OFFLINE(dip))
5350  		(void) ddi_initchild(pdip, dip);
5351  }
5352  
5353  /*
5354   * attach a node/branch with parent already held busy
5355   */
5356  static int
devi_attach_node(dev_info_t * dip,uint_t flags)5357  devi_attach_node(dev_info_t *dip, uint_t flags)
5358  {
5359  	dev_info_t *pdip = ddi_get_parent(dip);
5360  
5361  	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
5362  
5363  	mutex_enter(&(DEVI(dip)->devi_lock));
5364  	if (flags & NDI_DEVI_ONLINE) {
5365  		if (!i_ddi_devi_attached(dip))
5366  			DEVI_SET_REPORT(dip);
5367  		DEVI_SET_DEVICE_ONLINE(dip);
5368  	}
5369  	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
5370  		mutex_exit(&(DEVI(dip)->devi_lock));
5371  		return (NDI_FAILURE);
5372  	}
5373  	mutex_exit(&(DEVI(dip)->devi_lock));
5374  
5375  	if (i_ddi_attachchild(dip) != DDI_SUCCESS) {
5376  		mutex_enter(&(DEVI(dip)->devi_lock));
5377  		DEVI_SET_EVUNINIT(dip);
5378  		mutex_exit(&(DEVI(dip)->devi_lock));
5379  
5380  		if (ndi_dev_is_persistent_node(dip))
5381  			(void) ddi_uninitchild(dip);
5382  		else {
5383  			/*
5384  			 * Delete .conf nodes and nodes that are not
5385  			 * well formed.
5386  			 */
5387  			(void) ddi_remove_child(dip, 0);
5388  		}
5389  		return (NDI_FAILURE);
5390  	}
5391  
5392  	i_ndi_devi_report_status_change(dip, NULL);
5393  
5394  	/*
5395  	 * log an event, but not during devfs lookups in which case
5396  	 * NDI_NO_EVENT is set.
5397  	 */
5398  	if ((flags & NDI_NO_EVENT) == 0 && !(DEVI_EVADD(dip))) {
5399  		(void) i_log_devfs_add_devinfo(dip, flags);
5400  
5401  		mutex_enter(&(DEVI(dip)->devi_lock));
5402  		DEVI_SET_EVADD(dip);
5403  		mutex_exit(&(DEVI(dip)->devi_lock));
5404  	} else if (!(flags & NDI_NO_EVENT_STATE_CHNG)) {
5405  		mutex_enter(&(DEVI(dip)->devi_lock));
5406  		DEVI_SET_EVADD(dip);
5407  		mutex_exit(&(DEVI(dip)->devi_lock));
5408  	}
5409  
5410  	return (NDI_SUCCESS);
5411  }
5412  
5413  /* internal function to config immediate children */
5414  static int
config_immediate_children(dev_info_t * pdip,uint_t flags,major_t major)5415  config_immediate_children(dev_info_t *pdip, uint_t flags, major_t major)
5416  {
5417  	dev_info_t	*child, *next;
5418  
5419  	ASSERT(i_ddi_devi_attached(pdip));
5420  
5421  	if (!NEXUS_DRV(ddi_get_driver(pdip)))
5422  		return (NDI_SUCCESS);
5423  
5424  	NDI_CONFIG_DEBUG((CE_CONT,
5425  	    "config_immediate_children: %s%d (%p), flags=%x\n",
5426  	    ddi_driver_name(pdip), ddi_get_instance(pdip),
5427  	    (void *)pdip, flags));
5428  
5429  	ndi_devi_enter(pdip);
5430  
5431  	if (flags & NDI_CONFIG_REPROBE) {
5432  		mutex_enter(&DEVI(pdip)->devi_lock);
5433  		DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
5434  		mutex_exit(&DEVI(pdip)->devi_lock);
5435  	}
5436  	(void) i_ndi_make_spec_children(pdip, flags);
5437  	i_ndi_init_hw_children(pdip, flags);
5438  
5439  	child = ddi_get_child(pdip);
5440  	while (child) {
5441  		/* NOTE: devi_attach_node() may remove the dip */
5442  		next = ddi_get_next_sibling(child);
5443  
5444  		/*
5445  		 * Configure all nexus nodes or leaf nodes with
5446  		 * matching driver major
5447  		 */
5448  		if ((major == DDI_MAJOR_T_NONE) ||
5449  		    (major == ddi_driver_major(child)) ||
5450  		    ((flags & NDI_CONFIG) && (is_leaf_node(child) == 0)))
5451  			(void) devi_attach_node(child, flags);
5452  		child = next;
5453  	}
5454  
5455  	ndi_devi_exit(pdip);
5456  
5457  	return (NDI_SUCCESS);
5458  }
5459  
5460  /* internal function to config grand children */
5461  static int
config_grand_children(dev_info_t * pdip,uint_t flags,major_t major)5462  config_grand_children(dev_info_t *pdip, uint_t flags, major_t major)
5463  {
5464  	struct mt_config_handle *hdl;
5465  
5466  	/* multi-threaded configuration of child nexus */
5467  	hdl = mt_config_init(pdip, NULL, flags, major, MT_CONFIG_OP, NULL);
5468  	mt_config_children(hdl);
5469  
5470  	return (mt_config_fini(hdl));	/* wait for threads to exit */
5471  }
5472  
5473  /*
5474   * Common function for device tree configuration,
5475   * either BUS_CONFIG_ALL or BUS_CONFIG_DRIVER.
5476   * The NDI_CONFIG flag causes recursive configuration of
5477   * grandchildren, devfs usage should not recurse.
5478   */
5479  static int
devi_config_common(dev_info_t * dip,int flags,major_t major)5480  devi_config_common(dev_info_t *dip, int flags, major_t major)
5481  {
5482  	int error;
5483  	int (*f)();
5484  
5485  	if (!i_ddi_devi_attached(dip))
5486  		return (NDI_FAILURE);
5487  
5488  	if (pm_pre_config(dip, NULL) != DDI_SUCCESS)
5489  		return (NDI_FAILURE);
5490  
5491  	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
5492  	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5493  	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
5494  		error = config_immediate_children(dip, flags, major);
5495  	} else {
5496  		/* call bus_config entry point */
5497  		ddi_bus_config_op_t bus_op = (major == DDI_MAJOR_T_NONE) ?
5498  		    BUS_CONFIG_ALL : BUS_CONFIG_DRIVER;
5499  		error = (*f)(dip,
5500  		    flags, bus_op, (void *)(uintptr_t)major, NULL, 0);
5501  	}
5502  
5503  	if (error) {
5504  		pm_post_config(dip, NULL);
5505  		return (error);
5506  	}
5507  
5508  	/*
5509  	 * Some callers, notably SCSI, need to mark the devfs cache
5510  	 * to be rebuilt together with the config operation.
5511  	 */
5512  	if (flags & NDI_DEVFS_CLEAN)
5513  		(void) devfs_clean(dip, NULL, 0);
5514  
5515  	if (flags & NDI_CONFIG)
5516  		(void) config_grand_children(dip, flags, major);
5517  
5518  	pm_post_config(dip, NULL);
5519  
5520  	return (NDI_SUCCESS);
5521  }
5522  
5523  /*
5524   * Framework entry point for BUS_CONFIG_ALL
5525   */
5526  int
ndi_devi_config(dev_info_t * dip,int flags)5527  ndi_devi_config(dev_info_t *dip, int flags)
5528  {
5529  	NDI_CONFIG_DEBUG((CE_CONT,
5530  	    "ndi_devi_config: par = %s%d (%p), flags = 0x%x\n",
5531  	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
5532  
5533  	return (devi_config_common(dip, flags, DDI_MAJOR_T_NONE));
5534  }
5535  
5536  /*
5537   * Framework entry point for BUS_CONFIG_DRIVER, bound to major
5538   */
5539  int
ndi_devi_config_driver(dev_info_t * dip,int flags,major_t major)5540  ndi_devi_config_driver(dev_info_t *dip, int flags, major_t major)
5541  {
5542  	/* don't abuse this function */
5543  	ASSERT(major != DDI_MAJOR_T_NONE);
5544  
5545  	NDI_CONFIG_DEBUG((CE_CONT,
5546  	    "ndi_devi_config_driver: par = %s%d (%p), flags = 0x%x\n",
5547  	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
5548  
5549  	return (devi_config_common(dip, flags, major));
5550  }
5551  
5552  /*
5553   * Called by nexus drivers to configure its children.
5554   */
5555  static int
devi_config_one(dev_info_t * pdip,char * devnm,dev_info_t ** cdipp,uint_t flags,clock_t timeout)5556  devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **cdipp,
5557      uint_t flags, clock_t timeout)
5558  {
5559  	dev_info_t	*vdip = NULL;
5560  	char		*drivername = NULL;
5561  	int		find_by_addr = 0;
5562  	char		*name, *addr;
5563  	clock_t		end_time;	/* 60 sec */
5564  	int		probed;
5565  	dev_info_t	*cdip;
5566  	mdi_pathinfo_t	*cpip;
5567  
5568  	*cdipp = NULL;
5569  
5570  	if (!NEXUS_DRV(ddi_get_driver(pdip)))
5571  		return (NDI_FAILURE);
5572  
5573  	/* split name into "name@addr" parts */
5574  	i_ddi_parse_name(devnm, &name, &addr, NULL);
5575  
5576  	/*
5577  	 * If the nexus is a pHCI and we are not processing a pHCI from
5578  	 * mdi bus_config code then we need to know the vHCI.
5579  	 */
5580  	if (MDI_PHCI(pdip))
5581  		vdip = mdi_devi_get_vdip(pdip);
5582  
5583  	/*
5584  	 * We may have a genericname on a system that creates drivername
5585  	 * nodes (from .conf files).  Find the drivername by nodeid. If we
5586  	 * can't find a node with devnm as the node name then we search by
5587  	 * drivername.	This allows an implementation to supply a genericly
5588  	 * named boot path (disk) and locate drivename nodes (sd).  The
5589  	 * NDI_PROMNAME flag does not apply to /devices/pseudo paths.
5590  	 */
5591  	if ((flags & NDI_PROMNAME) && (pdip != pseudo_dip)) {
5592  		drivername = child_path_to_driver(pdip, name, addr);
5593  		find_by_addr = 1;
5594  	}
5595  
5596  	/*
5597  	 * Determine end_time: This routine should *not* be called with a
5598  	 * constant non-zero timeout argument, the caller should be adjusting
5599  	 * the timeout argument relative to when it *started* its asynchronous
5600  	 * enumeration.
5601  	 */
5602  	if (timeout > 0)
5603  		end_time = ddi_get_lbolt() + timeout;
5604  
5605  	for (;;) {
5606  		/*
5607  		 * For pHCI, enter (vHCI, pHCI) and search for pathinfo/client
5608  		 * child - break out of for(;;) loop if child found.
5609  		 * NOTE: Lock order for ndi_devi_enter is (vHCI, pHCI).
5610  		 */
5611  		if (vdip) {
5612  			/* use mdi_devi_enter ordering */
5613  			ndi_devi_enter(vdip);
5614  			ndi_devi_enter(pdip);
5615  			cpip = mdi_pi_find(pdip, NULL, addr);
5616  			cdip = mdi_pi_get_client(cpip);
5617  			if (cdip)
5618  				break;
5619  		} else
5620  			ndi_devi_enter(pdip);
5621  
5622  		/*
5623  		 * When not a  vHCI or not all pHCI devices are required to
5624  		 * enumerated under the vHCI (NDI_MDI_FALLBACK) search for
5625  		 * devinfo child.
5626  		 */
5627  		if ((vdip == NULL) || (flags & NDI_MDI_FALLBACK)) {
5628  			/* determine if .conf nodes already built */
5629  			probed = (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
5630  
5631  			/*
5632  			 * Search for child by name, if not found then search
5633  			 * for a node bound to the drivername driver with the
5634  			 * specified "@addr". Break out of for(;;) loop if
5635  			 * child found.  To support path-oriented aliases
5636  			 * binding on boot-device, we do a search_by_addr too.
5637  			 */
5638  again:			(void) i_ndi_make_spec_children(pdip, flags);
5639  			cdip = find_child_by_name(pdip, name, addr);
5640  			if ((cdip == NULL) && drivername)
5641  				cdip = find_child_by_driver(pdip,
5642  				    drivername, addr);
5643  			if ((cdip == NULL) && find_by_addr)
5644  				cdip = find_child_by_addr(pdip, addr);
5645  			if (cdip)
5646  				break;
5647  
5648  			/*
5649  			 * determine if we should reenumerate .conf nodes
5650  			 * and look for child again.
5651  			 */
5652  			if (probed &&
5653  			    i_ddi_io_initialized() &&
5654  			    (flags & NDI_CONFIG_REPROBE) &&
5655  			    ((timeout <= 0) || (ddi_get_lbolt() >= end_time))) {
5656  				probed = 0;
5657  				mutex_enter(&DEVI(pdip)->devi_lock);
5658  				DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
5659  				mutex_exit(&DEVI(pdip)->devi_lock);
5660  				goto again;
5661  			}
5662  		}
5663  
5664  		/* break out of for(;;) if time expired */
5665  		if ((timeout <= 0) || (ddi_get_lbolt() >= end_time))
5666  			break;
5667  
5668  		/*
5669  		 * Child not found, exit and wait for asynchronous enumeration
5670  		 * to add child (or timeout). The addition of a new child (vhci
5671  		 * or phci) requires the asynchronous enumeration thread to
5672  		 * ndi_devi_enter/ndi_devi_exit. This exit will signal devi_cv
5673  		 * and cause us to return from ndi_devi_exit_and_wait, after
5674  		 * which we loop and search for the requested child again.
5675  		 */
5676  		NDI_DEBUG(flags, (CE_CONT,
5677  		    "%s%d: waiting for child %s@%s, timeout %ld",
5678  		    ddi_driver_name(pdip), ddi_get_instance(pdip),
5679  		    name, addr, timeout));
5680  		if (vdip) {
5681  			/*
5682  			 * Mark vHCI for pHCI ndi_devi_exit broadcast.
5683  			 */
5684  			mutex_enter(&DEVI(vdip)->devi_lock);
5685  			DEVI(vdip)->devi_flags |=
5686  			    DEVI_PHCI_SIGNALS_VHCI;
5687  			mutex_exit(&DEVI(vdip)->devi_lock);
5688  			ndi_devi_exit(pdip);
5689  
5690  			/*
5691  			 * NB: There is a small race window from above
5692  			 * ndi_devi_exit() of pdip to cv_wait() in
5693  			 * ndi_devi_exit_and_wait() which can result in
5694  			 * not immediately finding a new pHCI child
5695  			 * of a pHCI that uses NDI_MDI_FAILBACK.
5696  			 */
5697  			ndi_devi_exit_and_wait(vdip, end_time);
5698  		} else {
5699  			ndi_devi_exit_and_wait(pdip, end_time);
5700  		}
5701  	}
5702  
5703  	/* done with paddr, fixup i_ddi_parse_name '@'->'\0' change */
5704  	if (addr && *addr != '\0')
5705  		*(addr - 1) = '@';
5706  
5707  	/* attach and hold the child, returning pointer to child */
5708  	if (cdip && (devi_attach_node(cdip, flags) == NDI_SUCCESS)) {
5709  		ndi_hold_devi(cdip);
5710  		*cdipp = cdip;
5711  	}
5712  
5713  	ndi_devi_exit(pdip);
5714  	if (vdip)
5715  		ndi_devi_exit(vdip);
5716  	return (*cdipp ? NDI_SUCCESS : NDI_FAILURE);
5717  }
5718  
5719  /*
5720   * Enumerate and attach a child specified by name 'devnm'.
5721   * Called by devfs lookup and DR to perform a BUS_CONFIG_ONE.
5722   * Note: devfs does not make use of NDI_CONFIG to configure
5723   * an entire branch.
5724   */
5725  int
ndi_devi_config_one(dev_info_t * pdip,char * devnm,dev_info_t ** dipp,int flags)5726  ndi_devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **dipp, int flags)
5727  {
5728  	int error;
5729  	int (*f)();
5730  	char *nmdup;
5731  	int duplen;
5732  	int branch_event = 0;
5733  
5734  	ASSERT(pdip);
5735  	ASSERT(devnm);
5736  	ASSERT(dipp);
5737  	ASSERT(i_ddi_devi_attached(pdip));
5738  
5739  	NDI_CONFIG_DEBUG((CE_CONT,
5740  	    "ndi_devi_config_one: par = %s%d (%p), child = %s\n",
5741  	    ddi_driver_name(pdip), ddi_get_instance(pdip),
5742  	    (void *)pdip, devnm));
5743  
5744  	*dipp = NULL;
5745  
5746  	if (pm_pre_config(pdip, devnm) != DDI_SUCCESS) {
5747  		cmn_err(CE_WARN, "preconfig failed: %s", devnm);
5748  		return (NDI_FAILURE);
5749  	}
5750  
5751  	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
5752  	    (flags & NDI_CONFIG)) {
5753  		flags |= NDI_BRANCH_EVENT_OP;
5754  		branch_event = 1;
5755  	}
5756  
5757  	nmdup = strdup(devnm);
5758  	duplen = strlen(devnm) + 1;
5759  
5760  	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
5761  	    (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5762  	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
5763  		error = devi_config_one(pdip, devnm, dipp, flags, 0);
5764  	} else {
5765  		/* call bus_config entry point */
5766  		error = (*f)(pdip, flags, BUS_CONFIG_ONE, (void *)devnm, dipp);
5767  	}
5768  
5769  	if (error) {
5770  		*dipp = NULL;
5771  	}
5772  
5773  	/*
5774  	 * if we fail to lookup and this could be an alias, lookup currdip
5775  	 * To prevent recursive lookups into the same hash table, only
5776  	 * do the currdip lookups once the hash table init is complete.
5777  	 * Use tsd so that redirection doesn't recurse
5778  	 */
5779  	if (error) {
5780  		char *alias = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
5781  		if (alias == NULL) {
5782  			ddi_err(DER_PANIC, pdip, "alias alloc failed: %s",
5783  			    nmdup);
5784  		}
5785  		(void) ddi_pathname(pdip, alias);
5786  		(void) strlcat(alias, "/", MAXPATHLEN);
5787  		(void) strlcat(alias, nmdup, MAXPATHLEN);
5788  
5789  		*dipp = ddi_alias_redirect(alias);
5790  		error = (*dipp ? NDI_SUCCESS : NDI_FAILURE);
5791  
5792  		kmem_free(alias, MAXPATHLEN);
5793  	}
5794  	kmem_free(nmdup, duplen);
5795  
5796  	if (error || !(flags & NDI_CONFIG)) {
5797  		pm_post_config(pdip, devnm);
5798  		return (error);
5799  	}
5800  
5801  	/*
5802  	 * DR usage (i.e. call with NDI_CONFIG) recursively configures
5803  	 * grandchildren, performing a BUS_CONFIG_ALL from the node attached
5804  	 * by the BUS_CONFIG_ONE.
5805  	 */
5806  	ASSERT(*dipp);
5807  	error = devi_config_common(*dipp, flags, DDI_MAJOR_T_NONE);
5808  
5809  	pm_post_config(pdip, devnm);
5810  
5811  	if (branch_event)
5812  		(void) i_log_devfs_branch_add(*dipp);
5813  
5814  	return (error);
5815  }
5816  
5817  /*
5818   * Enumerate and attach a child specified by name 'devnm'.
5819   * Called during configure the OBP options. This configures
5820   * only one node.
5821   */
5822  static int
ndi_devi_config_obp_args(dev_info_t * parent,char * devnm,dev_info_t ** childp,int flags)5823  ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
5824      dev_info_t **childp, int flags)
5825  {
5826  	int error;
5827  	int (*f)();
5828  
5829  	ASSERT(childp);
5830  	ASSERT(i_ddi_devi_attached(parent));
5831  
5832  	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_config_obp_args: "
5833  	    "par = %s%d (%p), child = %s\n", ddi_driver_name(parent),
5834  	    ddi_get_instance(parent), (void *)parent, devnm));
5835  
5836  	if ((DEVI(parent)->devi_ops->devo_bus_ops == NULL) ||
5837  	    (DEVI(parent)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5838  	    (f = DEVI(parent)->devi_ops->devo_bus_ops->bus_config) == NULL) {
5839  		error = NDI_FAILURE;
5840  	} else {
5841  		/* call bus_config entry point */
5842  		error = (*f)(parent, flags,
5843  		    BUS_CONFIG_OBP_ARGS, (void *)devnm, childp);
5844  	}
5845  	return (error);
5846  }
5847  
5848  /*
5849   * Pay attention, the following is a bit tricky:
5850   * There are three possible cases when constraints are applied
5851   *
5852   *	- A constraint is applied and the offline is disallowed.
5853   *	  Simply return failure and block the offline
5854   *
5855   *	- A constraint is applied and the offline is allowed.
5856   *	  Mark the dip as having passed the constraint and allow
5857   *	  offline to proceed.
5858   *
5859   *	- A constraint is not applied. Allow the offline to proceed for now.
5860   *
5861   * In the latter two cases we allow the offline to proceed. If the
5862   * offline succeeds (no users) everything is fine. It is ok for an unused
5863   * device to be offlined even if no constraints were imposed on the offline.
5864   * If the offline fails because there are users, we look at the constraint
5865   * flag on the dip. If the constraint flag is set (implying that it passed
5866   * a constraint) we allow the dip to be retired. If not, we don't allow
5867   * the retire. This ensures that we don't allow unconstrained retire.
5868   */
5869  int
e_ddi_offline_notify(dev_info_t * dip)5870  e_ddi_offline_notify(dev_info_t *dip)
5871  {
5872  	int retval;
5873  	int constraint;
5874  	int failure;
5875  
5876  	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): entered: dip=%p",
5877  	    (void *) dip));
5878  
5879  	constraint = 0;
5880  	failure = 0;
5881  
5882  	/*
5883  	 * Start with userland constraints first - applied via device contracts
5884  	 */
5885  	retval = contract_device_offline(dip, DDI_DEV_T_ANY, 0);
5886  	switch (retval) {
5887  	case CT_NACK:
5888  		RIO_DEBUG((CE_NOTE, "Received NACK for dip=%p", (void *)dip));
5889  		failure = 1;
5890  		goto out;
5891  	case CT_ACK:
5892  		constraint = 1;
5893  		RIO_DEBUG((CE_NOTE, "Received ACK for dip=%p", (void *)dip));
5894  		break;
5895  	case CT_NONE:
5896  		/* no contracts */
5897  		RIO_DEBUG((CE_NOTE, "No contracts on dip=%p", (void *)dip));
5898  		break;
5899  	default:
5900  		ASSERT(retval == CT_NONE);
5901  	}
5902  
5903  	/*
5904  	 * Next, use LDI to impose kernel constraints
5905  	 */
5906  	retval = ldi_invoke_notify(dip, DDI_DEV_T_ANY, 0, LDI_EV_OFFLINE, NULL);
5907  	switch (retval) {
5908  	case LDI_EV_FAILURE:
5909  		contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_FAILURE);
5910  		RIO_DEBUG((CE_NOTE, "LDI callback failed on dip=%p",
5911  		    (void *)dip));
5912  		failure = 1;
5913  		goto out;
5914  	case LDI_EV_SUCCESS:
5915  		constraint = 1;
5916  		RIO_DEBUG((CE_NOTE, "LDI callback success on dip=%p",
5917  		    (void *)dip));
5918  		break;
5919  	case LDI_EV_NONE:
5920  		/* no matching LDI callbacks */
5921  		RIO_DEBUG((CE_NOTE, "No LDI callbacks for dip=%p",
5922  		    (void *)dip));
5923  		break;
5924  	default:
5925  		ASSERT(retval == LDI_EV_NONE);
5926  	}
5927  
5928  out:
5929  	mutex_enter(&(DEVI(dip)->devi_lock));
5930  	if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && failure) {
5931  		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
5932  		    "BLOCKED flag. dip=%p", (void *)dip));
5933  		DEVI(dip)->devi_flags |= DEVI_R_BLOCKED;
5934  		if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
5935  			RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): "
5936  			    "blocked. clearing RCM CONSTRAINT flag. dip=%p",
5937  			    (void *)dip));
5938  			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
5939  		}
5940  	} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && constraint) {
5941  		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
5942  		    "CONSTRAINT flag. dip=%p", (void *)dip));
5943  		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
5944  	} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) &&
5945  	    ((DEVI(dip)->devi_ops != NULL &&
5946  	    DEVI(dip)->devi_ops->devo_bus_ops != NULL) ||
5947  	    DEVI(dip)->devi_ref == 0)) {
5948  		/* also allow retire if nexus or if device is not in use */
5949  		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): device not in "
5950  		    "use. Setting CONSTRAINT flag. dip=%p", (void *)dip));
5951  		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
5952  	} else {
5953  		/*
5954  		 * Note: We cannot ASSERT here that DEVI_R_CONSTRAINT is
5955  		 * not set, since other sources (such as RCM) may have
5956  		 * set the flag.
5957  		 */
5958  		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): not setting "
5959  		    "constraint flag. dip=%p", (void *)dip));
5960  	}
5961  	mutex_exit(&(DEVI(dip)->devi_lock));
5962  
5963  
5964  	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): exit: dip=%p",
5965  	    (void *) dip));
5966  
5967  	return (failure ? DDI_FAILURE : DDI_SUCCESS);
5968  }
5969  
5970  void
e_ddi_offline_finalize(dev_info_t * dip,int result)5971  e_ddi_offline_finalize(dev_info_t *dip, int result)
5972  {
5973  	RIO_DEBUG((CE_NOTE, "e_ddi_offline_finalize(): entry: result=%s, "
5974  	    "dip=%p", result == DDI_SUCCESS ? "SUCCESS" : "FAILURE",
5975  	    (void *)dip));
5976  
5977  	contract_device_negend(dip, DDI_DEV_T_ANY, 0,  result == DDI_SUCCESS ?
5978  	    CT_EV_SUCCESS : CT_EV_FAILURE);
5979  
5980  	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0,
5981  	    LDI_EV_OFFLINE, result == DDI_SUCCESS ?
5982  	    LDI_EV_SUCCESS : LDI_EV_FAILURE, NULL);
5983  
5984  	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_finalize(): exit: dip=%p",
5985  	    (void *)dip));
5986  }
5987  
5988  void
e_ddi_degrade_finalize(dev_info_t * dip)5989  e_ddi_degrade_finalize(dev_info_t *dip)
5990  {
5991  	RIO_DEBUG((CE_NOTE, "e_ddi_degrade_finalize(): entry: "
5992  	    "result always = DDI_SUCCESS, dip=%p", (void *)dip));
5993  
5994  	contract_device_degrade(dip, DDI_DEV_T_ANY, 0);
5995  	contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
5996  
5997  	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0, LDI_EV_DEGRADE,
5998  	    LDI_EV_SUCCESS, NULL);
5999  
6000  	RIO_VERBOSE((CE_NOTE, "e_ddi_degrade_finalize(): exit: dip=%p",
6001  	    (void *)dip));
6002  }
6003  
6004  void
e_ddi_undegrade_finalize(dev_info_t * dip)6005  e_ddi_undegrade_finalize(dev_info_t *dip)
6006  {
6007  	RIO_DEBUG((CE_NOTE, "e_ddi_undegrade_finalize(): entry: "
6008  	    "result always = DDI_SUCCESS, dip=%p", (void *)dip));
6009  
6010  	contract_device_undegrade(dip, DDI_DEV_T_ANY, 0);
6011  	contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
6012  
6013  	RIO_VERBOSE((CE_NOTE, "e_ddi_undegrade_finalize(): exit: dip=%p",
6014  	    (void *)dip));
6015  }
6016  
6017  /*
6018   * detach a node with parent already held busy
6019   */
6020  static int
devi_detach_node(dev_info_t * dip,uint_t flags)6021  devi_detach_node(dev_info_t *dip, uint_t flags)
6022  {
6023  	dev_info_t *pdip = ddi_get_parent(dip);
6024  	int ret = NDI_SUCCESS;
6025  	ddi_eventcookie_t cookie;
6026  	char *path = NULL;
6027  	char *class = NULL;
6028  	char *driver = NULL;
6029  	int instance = -1;
6030  	int post_event = 0;
6031  
6032  	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
6033  
6034  	/*
6035  	 * Invoke notify if offlining
6036  	 */
6037  	if (flags & NDI_DEVI_OFFLINE) {
6038  		RIO_DEBUG((CE_NOTE, "devi_detach_node: offlining dip=%p",
6039  		    (void *)dip));
6040  		if (e_ddi_offline_notify(dip) != DDI_SUCCESS) {
6041  			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline NACKed"
6042  			    "dip=%p", (void *)dip));
6043  			return (NDI_FAILURE);
6044  		}
6045  	}
6046  
6047  	if (flags & NDI_POST_EVENT) {
6048  		if (i_ddi_devi_attached(pdip)) {
6049  			if (ddi_get_eventcookie(dip, DDI_DEVI_REMOVE_EVENT,
6050  			    &cookie) == NDI_SUCCESS)
6051  				(void) ndi_post_event(dip, dip, cookie, NULL);
6052  		}
6053  	}
6054  
6055  	/*
6056  	 * dv_mknod places a hold on the dev_info_t for each devfs node
6057  	 * created.  If we're to succeed in detaching this device, we must
6058  	 * first release all outstanding references held by devfs.
6059  	 */
6060  	(void) devfs_clean(pdip, NULL, DV_CLEAN_FORCE);
6061  
6062  	if (i_ddi_detachchild(dip, flags) != DDI_SUCCESS) {
6063  		if (flags & NDI_DEVI_OFFLINE) {
6064  			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline failed."
6065  			    " Calling e_ddi_offline_finalize with result=%d. "
6066  			    "dip=%p", DDI_FAILURE, (void *)dip));
6067  			e_ddi_offline_finalize(dip, DDI_FAILURE);
6068  		}
6069  		return (NDI_FAILURE);
6070  	}
6071  
6072  	if (flags & NDI_DEVI_OFFLINE) {
6073  		RIO_DEBUG((CE_NOTE, "devi_detach_node: offline succeeded."
6074  		    " Calling e_ddi_offline_finalize with result=%d, "
6075  		    "dip=%p", DDI_SUCCESS, (void *)dip));
6076  		e_ddi_offline_finalize(dip, DDI_SUCCESS);
6077  	}
6078  
6079  	if (flags & NDI_AUTODETACH)
6080  		return (NDI_SUCCESS);
6081  
6082  	/*
6083  	 * For DR, even bound nodes may need to have offline
6084  	 * flag set.
6085  	 */
6086  	if (flags & NDI_DEVI_OFFLINE) {
6087  		mutex_enter(&(DEVI(dip)->devi_lock));
6088  		DEVI_SET_DEVICE_OFFLINE(dip);
6089  		mutex_exit(&(DEVI(dip)->devi_lock));
6090  	}
6091  
6092  	if (i_ddi_node_state(dip) == DS_INITIALIZED) {
6093  		struct dev_info *devi = DEVI(dip);
6094  
6095  		if (devi->devi_ev_path == NULL) {
6096  			devi->devi_ev_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6097  			(void) ddi_pathname(dip, devi->devi_ev_path);
6098  		}
6099  		if (flags & NDI_DEVI_OFFLINE)
6100  			i_ndi_devi_report_status_change(dip,
6101  			    devi->devi_ev_path);
6102  
6103  		if (need_remove_event(dip, flags)) {
6104  			/*
6105  			 * instance and path data are lost in call to
6106  			 * ddi_uninitchild
6107  			 */
6108  			devi->devi_ev_instance = ddi_get_instance(dip);
6109  
6110  			mutex_enter(&(DEVI(dip)->devi_lock));
6111  			DEVI_SET_EVREMOVE(dip);
6112  			mutex_exit(&(DEVI(dip)->devi_lock));
6113  		}
6114  	}
6115  
6116  	if (flags & (NDI_UNCONFIG | NDI_DEVI_REMOVE)) {
6117  		ret = ddi_uninitchild(dip);
6118  		if (ret == NDI_SUCCESS) {
6119  			/*
6120  			 * Remove uninitialized pseudo nodes because
6121  			 * system props are lost and the node cannot be
6122  			 * reattached.
6123  			 */
6124  			if (!ndi_dev_is_persistent_node(dip))
6125  				flags |= NDI_DEVI_REMOVE;
6126  
6127  			if (flags & NDI_DEVI_REMOVE) {
6128  				/*
6129  				 * NOTE: If there is a consumer of LDI events,
6130  				 * ddi_uninitchild above would have failed
6131  				 * because of active devi_ref from ldi_open().
6132  				 */
6133  
6134  				if (DEVI_EVREMOVE(dip)) {
6135  					path = i_ddi_strdup(
6136  					    DEVI(dip)->devi_ev_path,
6137  					    KM_SLEEP);
6138  					class =
6139  					    i_ddi_strdup(i_ddi_devi_class(dip),
6140  					    KM_SLEEP);
6141  					driver =
6142  					    i_ddi_strdup(
6143  					    (char *)ddi_driver_name(dip),
6144  					    KM_SLEEP);
6145  					instance = DEVI(dip)->devi_ev_instance;
6146  					post_event = 1;
6147  				}
6148  
6149  				ret = ddi_remove_child(dip, 0);
6150  				if (post_event && ret == NDI_SUCCESS) {
6151  					/* Generate EC_DEVFS_DEVI_REMOVE */
6152  					(void) i_log_devfs_remove_devinfo(path,
6153  					    class, driver, instance, flags);
6154  				}
6155  			}
6156  
6157  		}
6158  	}
6159  
6160  	if (path)
6161  		strfree(path);
6162  	if (class)
6163  		strfree(class);
6164  	if (driver)
6165  		strfree(driver);
6166  
6167  	return (ret);
6168  }
6169  
6170  /*
6171   * unconfigure immediate children of bus nexus device
6172   */
6173  static int
unconfig_immediate_children(dev_info_t * dip,dev_info_t ** dipp,int flags,major_t major)6174  unconfig_immediate_children(
6175  	dev_info_t *dip,
6176  	dev_info_t **dipp,
6177  	int flags,
6178  	major_t major)
6179  {
6180  	int rv = NDI_SUCCESS;
6181  	dev_info_t *child;
6182  	dev_info_t *vdip = NULL;
6183  	dev_info_t *next;
6184  
6185  	ASSERT(dipp == NULL || *dipp == NULL);
6186  
6187  	/*
6188  	 * Scan forward to see if we will be processing a pHCI child. If we
6189  	 * have a child that is a pHCI and vHCI and pHCI are not siblings then
6190  	 * enter vHCI before parent(pHCI) to prevent deadlock with mpxio
6191  	 * Client power management operations.
6192  	 */
6193  	ndi_devi_enter(dip);
6194  	for (child = ddi_get_child(dip); child;
6195  	    child = ddi_get_next_sibling(child)) {
6196  		/* skip same nodes we skip below */
6197  		if (((major != DDI_MAJOR_T_NONE) &&
6198  		    (major != ddi_driver_major(child))) ||
6199  		    ((flags & NDI_AUTODETACH) && !is_leaf_node(child)))
6200  			continue;
6201  
6202  		if (MDI_PHCI(child)) {
6203  			vdip = mdi_devi_get_vdip(child);
6204  			/*
6205  			 * If vHCI and vHCI is not a sibling of pHCI
6206  			 * then enter in (vHCI, parent(pHCI)) order.
6207  			 */
6208  			if (vdip && (ddi_get_parent(vdip) != dip)) {
6209  				ndi_devi_exit(dip);
6210  
6211  				/* use mdi_devi_enter ordering */
6212  				ndi_devi_enter(vdip);
6213  				ndi_devi_enter(dip);
6214  				break;
6215  			} else
6216  				vdip = NULL;
6217  		}
6218  	}
6219  
6220  	child = ddi_get_child(dip);
6221  	while (child) {
6222  		next = ddi_get_next_sibling(child);
6223  
6224  		if ((major != DDI_MAJOR_T_NONE) &&
6225  		    (major != ddi_driver_major(child))) {
6226  			child = next;
6227  			continue;
6228  		}
6229  
6230  		/* skip nexus nodes during autodetach */
6231  		if ((flags & NDI_AUTODETACH) && !is_leaf_node(child)) {
6232  			child = next;
6233  			continue;
6234  		}
6235  
6236  		if (devi_detach_node(child, flags) != NDI_SUCCESS) {
6237  			if (dipp && *dipp == NULL) {
6238  				ndi_hold_devi(child);
6239  				*dipp = child;
6240  			}
6241  			rv = NDI_FAILURE;
6242  		}
6243  
6244  		/*
6245  		 * Continue upon failure--best effort algorithm
6246  		 */
6247  		child = next;
6248  	}
6249  
6250  	ndi_devi_exit(dip);
6251  	if (vdip)
6252  		ndi_devi_exit(vdip);
6253  
6254  	return (rv);
6255  }
6256  
6257  /*
6258   * unconfigure grand children of bus nexus device
6259   */
6260  static int
unconfig_grand_children(dev_info_t * dip,dev_info_t ** dipp,int flags,major_t major,struct brevq_node ** brevqp)6261  unconfig_grand_children(
6262  	dev_info_t *dip,
6263  	dev_info_t **dipp,
6264  	int flags,
6265  	major_t major,
6266  	struct brevq_node **brevqp)
6267  {
6268  	struct mt_config_handle *hdl;
6269  
6270  	if (brevqp)
6271  		*brevqp = NULL;
6272  
6273  	/* multi-threaded configuration of child nexus */
6274  	hdl = mt_config_init(dip, dipp, flags, major, MT_UNCONFIG_OP, brevqp);
6275  	mt_config_children(hdl);
6276  
6277  	return (mt_config_fini(hdl));	/* wait for threads to exit */
6278  }
6279  
6280  /*
6281   * Unconfigure children/descendants of the dip.
6282   *
6283   * If brevqp is not NULL, on return *brevqp is set to a queue of dip's
6284   * child devinames for which branch remove events need to be generated.
6285   */
6286  static int
devi_unconfig_common(dev_info_t * dip,dev_info_t ** dipp,int flags,major_t major,struct brevq_node ** brevqp)6287  devi_unconfig_common(
6288  	dev_info_t *dip,
6289  	dev_info_t **dipp,
6290  	int flags,
6291  	major_t major,
6292  	struct brevq_node **brevqp)
6293  {
6294  	int rv;
6295  	int pm_cookie;
6296  	int (*f)();
6297  	ddi_bus_config_op_t bus_op;
6298  
6299  	if (dipp)
6300  		*dipp = NULL;
6301  	if (brevqp)
6302  		*brevqp = NULL;
6303  
6304  	/*
6305  	 * Power up the dip if it is powered off.  If the flag bit
6306  	 * NDI_AUTODETACH is set and the dip is not at its full power,
6307  	 * skip the rest of the branch.
6308  	 */
6309  	if (pm_pre_unconfig(dip, flags, &pm_cookie, NULL) != DDI_SUCCESS)
6310  		return ((flags & NDI_AUTODETACH) ? NDI_SUCCESS :
6311  		    NDI_FAILURE);
6312  
6313  	/*
6314  	 * Some callers, notably SCSI, need to clear out the devfs
6315  	 * cache together with the unconfig to prevent stale entries.
6316  	 */
6317  	if (flags & NDI_DEVFS_CLEAN)
6318  		(void) devfs_clean(dip, NULL, 0);
6319  
6320  	rv = unconfig_grand_children(dip, dipp, flags, major, brevqp);
6321  
6322  	if ((rv != NDI_SUCCESS) && ((flags & NDI_AUTODETACH) == 0)) {
6323  		if (brevqp && *brevqp) {
6324  			log_and_free_br_events_on_grand_children(dip, *brevqp);
6325  			free_brevq(*brevqp);
6326  			*brevqp = NULL;
6327  		}
6328  		pm_post_unconfig(dip, pm_cookie, NULL);
6329  		return (rv);
6330  	}
6331  
6332  	if (dipp && *dipp) {
6333  		ndi_rele_devi(*dipp);
6334  		*dipp = NULL;
6335  	}
6336  
6337  	/*
6338  	 * It is possible to have a detached nexus with children
6339  	 * and grandchildren (for example: a branch consisting
6340  	 * entirely of bound nodes.) Since the nexus is detached
6341  	 * the bus_unconfig entry point cannot be used to remove
6342  	 * or unconfigure the descendants.
6343  	 */
6344  	if (!i_ddi_devi_attached(dip) ||
6345  	    (DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
6346  	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
6347  	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
6348  		rv = unconfig_immediate_children(dip, dipp, flags, major);
6349  	} else {
6350  		/*
6351  		 * call bus_unconfig entry point
6352  		 * It should reset nexus flags if unconfigure succeeds.
6353  		 */
6354  		bus_op = (major == DDI_MAJOR_T_NONE) ?
6355  		    BUS_UNCONFIG_ALL : BUS_UNCONFIG_DRIVER;
6356  		rv = (*f)(dip, flags, bus_op, (void *)(uintptr_t)major);
6357  	}
6358  
6359  	pm_post_unconfig(dip, pm_cookie, NULL);
6360  
6361  	if (brevqp && *brevqp)
6362  		cleanup_br_events_on_grand_children(dip, brevqp);
6363  
6364  	return (rv);
6365  }
6366  
6367  /*
6368   * called by devfs/framework to unconfigure children bound to major
6369   * If NDI_AUTODETACH is specified, this is invoked by either the
6370   * moduninstall daemon or the modunload -i 0 command.
6371   */
6372  int
ndi_devi_unconfig_driver(dev_info_t * dip,int flags,major_t major)6373  ndi_devi_unconfig_driver(dev_info_t *dip, int flags, major_t major)
6374  {
6375  	NDI_CONFIG_DEBUG((CE_CONT,
6376  	    "ndi_devi_unconfig_driver: par = %s%d (%p), flags = 0x%x\n",
6377  	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
6378  
6379  	return (devi_unconfig_common(dip, NULL, flags, major, NULL));
6380  }
6381  
6382  int
ndi_devi_unconfig(dev_info_t * dip,int flags)6383  ndi_devi_unconfig(dev_info_t *dip, int flags)
6384  {
6385  	NDI_CONFIG_DEBUG((CE_CONT,
6386  	    "ndi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
6387  	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
6388  
6389  	return (devi_unconfig_common(dip, NULL, flags, DDI_MAJOR_T_NONE, NULL));
6390  }
6391  
6392  int
e_ddi_devi_unconfig(dev_info_t * dip,dev_info_t ** dipp,int flags)6393  e_ddi_devi_unconfig(dev_info_t *dip, dev_info_t **dipp, int flags)
6394  {
6395  	NDI_CONFIG_DEBUG((CE_CONT,
6396  	    "e_ddi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
6397  	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
6398  
6399  	return (devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE, NULL));
6400  }
6401  
6402  /*
6403   * Unconfigure child by name
6404   */
6405  static int
devi_unconfig_one(dev_info_t * pdip,char * devnm,int flags)6406  devi_unconfig_one(dev_info_t *pdip, char *devnm, int flags)
6407  {
6408  	int		rv;
6409  	dev_info_t	*child;
6410  	dev_info_t	*vdip = NULL;
6411  
6412  	ndi_devi_enter(pdip);
6413  	child = ndi_devi_findchild(pdip, devnm);
6414  
6415  	/*
6416  	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
6417  	 * before parent(pHCI) to avoid deadlock with mpxio Client power
6418  	 * management operations.
6419  	 */
6420  	if (child && MDI_PHCI(child)) {
6421  		vdip = mdi_devi_get_vdip(child);
6422  		if (vdip && (ddi_get_parent(vdip) != pdip)) {
6423  			ndi_devi_exit(pdip);
6424  
6425  			/* use mdi_devi_enter ordering */
6426  			ndi_devi_enter(vdip);
6427  			ndi_devi_enter(pdip);
6428  			child = ndi_devi_findchild(pdip, devnm);
6429  		} else
6430  			vdip = NULL;
6431  	}
6432  
6433  	if (child) {
6434  		rv = devi_detach_node(child, flags);
6435  	} else {
6436  		NDI_CONFIG_DEBUG((CE_CONT,
6437  		    "devi_unconfig_one: %s not found\n", devnm));
6438  		rv = NDI_SUCCESS;
6439  	}
6440  
6441  	ndi_devi_exit(pdip);
6442  	if (vdip)
6443  		ndi_devi_exit(vdip);
6444  
6445  	return (rv);
6446  }
6447  
6448  int
ndi_devi_unconfig_one(dev_info_t * pdip,char * devnm,dev_info_t ** dipp,int flags)6449  ndi_devi_unconfig_one(
6450  	dev_info_t *pdip,
6451  	char *devnm,
6452  	dev_info_t **dipp,
6453  	int flags)
6454  {
6455  	int		(*f)();
6456  	int		rv;
6457  	int		pm_cookie;
6458  	dev_info_t	*child;
6459  	dev_info_t	*vdip = NULL;
6460  	struct brevq_node *brevq = NULL;
6461  
6462  	ASSERT(i_ddi_devi_attached(pdip));
6463  
6464  	NDI_CONFIG_DEBUG((CE_CONT,
6465  	    "ndi_devi_unconfig_one: par = %s%d (%p), child = %s\n",
6466  	    ddi_driver_name(pdip), ddi_get_instance(pdip),
6467  	    (void *)pdip, devnm));
6468  
6469  	if (pm_pre_unconfig(pdip, flags, &pm_cookie, devnm) != DDI_SUCCESS)
6470  		return (NDI_FAILURE);
6471  
6472  	if (dipp)
6473  		*dipp = NULL;
6474  
6475  	ndi_devi_enter(pdip);
6476  	child = ndi_devi_findchild(pdip, devnm);
6477  
6478  	/*
6479  	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
6480  	 * before parent(pHCI) to avoid deadlock with mpxio Client power
6481  	 * management operations.
6482  	 */
6483  	if (child && MDI_PHCI(child)) {
6484  		vdip = mdi_devi_get_vdip(child);
6485  		if (vdip && (ddi_get_parent(vdip) != pdip)) {
6486  			ndi_devi_exit(pdip);
6487  
6488  			/* use mdi_devi_enter ordering */
6489  			ndi_devi_enter(vdip);
6490  			ndi_devi_enter(pdip);
6491  			child = ndi_devi_findchild(pdip, devnm);
6492  		} else
6493  			vdip = NULL;
6494  	}
6495  
6496  	if (child == NULL) {
6497  		NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_unconfig_one: %s"
6498  		    " not found\n", devnm));
6499  		rv = NDI_SUCCESS;
6500  		goto out;
6501  	}
6502  
6503  	/*
6504  	 * Unconfigure children/descendants of named child
6505  	 */
6506  	rv = devi_unconfig_branch(child, dipp, flags | NDI_UNCONFIG, &brevq);
6507  	if (rv != NDI_SUCCESS)
6508  		goto out;
6509  
6510  	init_bound_node_ev(pdip, child, flags);
6511  
6512  	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
6513  	    (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
6514  	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
6515  		rv = devi_detach_node(child, flags);
6516  	} else {
6517  		/* call bus_config entry point */
6518  		rv = (*f)(pdip, flags, BUS_UNCONFIG_ONE, (void *)devnm);
6519  	}
6520  
6521  	if (brevq) {
6522  		if (rv != NDI_SUCCESS)
6523  			log_and_free_brevq_dip(child, brevq);
6524  		else
6525  			free_brevq(brevq);
6526  	}
6527  
6528  	if (dipp && rv != NDI_SUCCESS) {
6529  		ndi_hold_devi(child);
6530  		ASSERT(*dipp == NULL);
6531  		*dipp = child;
6532  	}
6533  
6534  out:
6535  	ndi_devi_exit(pdip);
6536  	if (vdip)
6537  		ndi_devi_exit(vdip);
6538  
6539  	pm_post_unconfig(pdip, pm_cookie, devnm);
6540  
6541  	return (rv);
6542  }
6543  
6544  struct async_arg {
6545  	dev_info_t *dip;
6546  	uint_t flags;
6547  };
6548  
6549  /*
6550   * Common async handler for:
6551   *	ndi_devi_bind_driver_async
6552   *	ndi_devi_online_async
6553   */
6554  static int
i_ndi_devi_async_common(dev_info_t * dip,uint_t flags,void (* func)())6555  i_ndi_devi_async_common(dev_info_t *dip, uint_t flags, void (*func)())
6556  {
6557  	int tqflag;
6558  	int kmflag;
6559  	struct async_arg *arg;
6560  	dev_info_t *pdip = ddi_get_parent(dip);
6561  
6562  	ASSERT(pdip);
6563  	ASSERT(DEVI(pdip)->devi_taskq);
6564  	ASSERT(ndi_dev_is_persistent_node(dip));
6565  
6566  	if (flags & NDI_NOSLEEP) {
6567  		kmflag = KM_NOSLEEP;
6568  		tqflag = TQ_NOSLEEP;
6569  	} else {
6570  		kmflag = KM_SLEEP;
6571  		tqflag = TQ_SLEEP;
6572  	}
6573  
6574  	arg = kmem_alloc(sizeof (*arg), kmflag);
6575  	if (arg == NULL)
6576  		goto fail;
6577  
6578  	arg->flags = flags;
6579  	arg->dip = dip;
6580  	if (ddi_taskq_dispatch(DEVI(pdip)->devi_taskq, func, arg, tqflag) ==
6581  	    DDI_SUCCESS) {
6582  		return (NDI_SUCCESS);
6583  	}
6584  
6585  fail:
6586  	NDI_CONFIG_DEBUG((CE_CONT, "%s%d: ddi_taskq_dispatch failed",
6587  	    ddi_driver_name(pdip), ddi_get_instance(pdip)));
6588  
6589  	if (arg)
6590  		kmem_free(arg, sizeof (*arg));
6591  	return (NDI_FAILURE);
6592  }
6593  
6594  static void
i_ndi_devi_bind_driver_cb(struct async_arg * arg)6595  i_ndi_devi_bind_driver_cb(struct async_arg *arg)
6596  {
6597  	(void) ndi_devi_bind_driver(arg->dip, arg->flags);
6598  	kmem_free(arg, sizeof (*arg));
6599  }
6600  
6601  int
ndi_devi_bind_driver_async(dev_info_t * dip,uint_t flags)6602  ndi_devi_bind_driver_async(dev_info_t *dip, uint_t flags)
6603  {
6604  	return (i_ndi_devi_async_common(dip, flags,
6605  	    (void (*)())i_ndi_devi_bind_driver_cb));
6606  }
6607  
6608  /*
6609   * place the devinfo in the ONLINE state.
6610   */
6611  int
ndi_devi_online(dev_info_t * dip,uint_t flags)6612  ndi_devi_online(dev_info_t *dip, uint_t flags)
6613  {
6614  	int rv;
6615  	dev_info_t *pdip = ddi_get_parent(dip);
6616  	int branch_event = 0;
6617  
6618  	ASSERT(pdip);
6619  
6620  	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_online: %s%d (%p)\n",
6621  	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
6622  
6623  	ndi_devi_enter(pdip);
6624  	/* bind child before merging .conf nodes */
6625  	rv = i_ndi_config_node(dip, DS_BOUND, flags);
6626  	if (rv != NDI_SUCCESS) {
6627  		ndi_devi_exit(pdip);
6628  		return (rv);
6629  	}
6630  
6631  	/* merge .conf properties */
6632  	(void) i_ndi_make_spec_children(pdip, flags);
6633  
6634  	flags |= (NDI_DEVI_ONLINE | NDI_CONFIG);
6635  
6636  	if (flags & NDI_NO_EVENT) {
6637  		/*
6638  		 * Caller is specifically asking for not to generate an event.
6639  		 * Set the following flag so that devi_attach_node() don't
6640  		 * change the event state.
6641  		 */
6642  		flags |= NDI_NO_EVENT_STATE_CHNG;
6643  	}
6644  
6645  	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
6646  	    ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip))) {
6647  		flags |= NDI_BRANCH_EVENT_OP;
6648  		branch_event = 1;
6649  	}
6650  
6651  	/*
6652  	 * devi_attach_node() may remove dip on failure
6653  	 */
6654  	if ((rv = devi_attach_node(dip, flags)) == NDI_SUCCESS) {
6655  		if ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip)) {
6656  			/*
6657  			 * Hold the attached dip, and exit the parent while
6658  			 * we drive configuration of children below the
6659  			 * attached dip.
6660  			 */
6661  			ndi_hold_devi(dip);
6662  			ndi_devi_exit(pdip);
6663  
6664  			(void) ndi_devi_config(dip, flags);
6665  
6666  			ndi_devi_enter(pdip);
6667  			ndi_rele_devi(dip);
6668  		}
6669  
6670  		if (branch_event)
6671  			(void) i_log_devfs_branch_add(dip);
6672  	}
6673  
6674  	ndi_devi_exit(pdip);
6675  
6676  	/*
6677  	 * Notify devfs that we have a new node. Devfs needs to invalidate
6678  	 * cached directory contents.
6679  	 *
6680  	 * For PCMCIA devices, it is possible the pdip is not fully
6681  	 * attached. In this case, calling back into devfs will
6682  	 * result in a loop or assertion error. Hence, the check
6683  	 * on node state.
6684  	 *
6685  	 * If we own parent lock, this is part of a branch operation.
6686  	 * We skip the devfs_clean() step because the cache invalidation
6687  	 * is done higher up in the device tree.
6688  	 */
6689  	if (rv == NDI_SUCCESS && i_ddi_devi_attached(pdip) &&
6690  	    !DEVI_BUSY_OWNED(pdip))
6691  		(void) devfs_clean(pdip, NULL, 0);
6692  	return (rv);
6693  }
6694  
6695  static void
i_ndi_devi_online_cb(struct async_arg * arg)6696  i_ndi_devi_online_cb(struct async_arg *arg)
6697  {
6698  	(void) ndi_devi_online(arg->dip, arg->flags);
6699  	kmem_free(arg, sizeof (*arg));
6700  }
6701  
6702  int
ndi_devi_online_async(dev_info_t * dip,uint_t flags)6703  ndi_devi_online_async(dev_info_t *dip, uint_t flags)
6704  {
6705  	/* mark child as need config if requested. */
6706  	if (flags & NDI_CONFIG) {
6707  		mutex_enter(&(DEVI(dip)->devi_lock));
6708  		DEVI_SET_NDI_CONFIG(dip);
6709  		mutex_exit(&(DEVI(dip)->devi_lock));
6710  	}
6711  
6712  	return (i_ndi_devi_async_common(dip, flags,
6713  	    (void (*)())i_ndi_devi_online_cb));
6714  }
6715  
6716  /*
6717   * Take a device node Offline
6718   * To take a device Offline means to detach the device instance from
6719   * the driver and prevent devfs requests from re-attaching the device
6720   * instance.
6721   *
6722   * The flag NDI_DEVI_REMOVE causes removes the device node from
6723   * the driver list and the device tree. In this case, the device
6724   * is assumed to be removed from the system.
6725   */
6726  int
ndi_devi_offline(dev_info_t * dip,uint_t flags)6727  ndi_devi_offline(dev_info_t *dip, uint_t flags)
6728  {
6729  	int		rval = 0;
6730  	dev_info_t	*pdip = ddi_get_parent(dip);
6731  	dev_info_t	*vdip = NULL;
6732  	struct brevq_node *brevq = NULL;
6733  
6734  	ASSERT(pdip);
6735  
6736  	flags |= NDI_DEVI_OFFLINE;
6737  
6738  	/*
6739  	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
6740  	 * before parent(pHCI) to avoid deadlock with mpxio Client power
6741  	 * management operations.
6742  	 */
6743  	if (MDI_PHCI(dip)) {
6744  		vdip = mdi_devi_get_vdip(dip);
6745  		if (vdip && (ddi_get_parent(vdip) != pdip))
6746  			ndi_devi_enter(vdip);
6747  		else
6748  			vdip = NULL;
6749  	}
6750  	ndi_devi_enter(pdip);
6751  
6752  	if (i_ddi_devi_attached(dip)) {
6753  		/*
6754  		 * If dip is in DS_READY state, there may be cached dv_nodes
6755  		 * referencing this dip, so we invoke devfs code path.
6756  		 * Note that we must release busy changing on pdip to
6757  		 * avoid deadlock against devfs.
6758  		 */
6759  		char *devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
6760  		(void) ddi_deviname(dip, devname);
6761  
6762  		ndi_devi_exit(pdip);
6763  		if (vdip)
6764  			ndi_devi_exit(vdip);
6765  
6766  		/*
6767  		 * If we are explictly told to clean, then clean. If we own the
6768  		 * parent lock then this is part of a branch operation, and we
6769  		 * skip the devfs_clean() step.
6770  		 *
6771  		 * NOTE: A thread performing a devfs file system lookup/
6772  		 * bus_config can't call devfs_clean to unconfig without
6773  		 * causing rwlock problems in devfs. For ndi_devi_offline, this
6774  		 * means that the NDI_DEVFS_CLEAN flag is safe from ioctl code
6775  		 * or from an async hotplug thread, but is not safe from a
6776  		 * nexus driver's bus_config implementation.
6777  		 */
6778  		if ((flags & NDI_DEVFS_CLEAN) ||
6779  		    (!DEVI_BUSY_OWNED(pdip)))
6780  			(void) devfs_clean(pdip, devname + 1, DV_CLEAN_FORCE);
6781  
6782  		kmem_free(devname, MAXNAMELEN + 1);
6783  
6784  		rval = devi_unconfig_branch(dip, NULL, flags|NDI_UNCONFIG,
6785  		    &brevq);
6786  
6787  		if (rval)
6788  			return (NDI_FAILURE);
6789  
6790  		if (vdip)
6791  			ndi_devi_enter(vdip);
6792  		ndi_devi_enter(pdip);
6793  	}
6794  
6795  	init_bound_node_ev(pdip, dip, flags);
6796  
6797  	rval = devi_detach_node(dip, flags);
6798  	if (brevq) {
6799  		if (rval != NDI_SUCCESS)
6800  			log_and_free_brevq_dip(dip, brevq);
6801  		else
6802  			free_brevq(brevq);
6803  	}
6804  
6805  	ndi_devi_exit(pdip);
6806  	if (vdip)
6807  		ndi_devi_exit(vdip);
6808  
6809  	return (rval);
6810  }
6811  
6812  /*
6813   * Find the child dev_info node of parent nexus 'p' whose unit address
6814   * matches "cname@caddr".  Recommend use of ndi_devi_findchild() instead.
6815   */
6816  dev_info_t *
ndi_devi_find(dev_info_t * pdip,char * cname,char * caddr)6817  ndi_devi_find(dev_info_t *pdip, char *cname, char *caddr)
6818  {
6819  	dev_info_t *child;
6820  
6821  	if (pdip == NULL || cname == NULL || caddr == NULL)
6822  		return ((dev_info_t *)NULL);
6823  
6824  	ndi_devi_enter(pdip);
6825  	child = find_sibling(ddi_get_child(pdip), cname, caddr,
6826  	    FIND_NODE_BY_NODENAME, NULL);
6827  	ndi_devi_exit(pdip);
6828  	return (child);
6829  }
6830  
6831  /*
6832   * Find the child dev_info node of parent nexus 'p' whose unit address
6833   * matches devname "name@addr".  Permits caller to hold the parent.
6834   */
6835  dev_info_t *
ndi_devi_findchild(dev_info_t * pdip,char * devname)6836  ndi_devi_findchild(dev_info_t *pdip, char *devname)
6837  {
6838  	dev_info_t *child;
6839  	char	*cname, *caddr;
6840  	char	*devstr;
6841  
6842  	ASSERT(DEVI_BUSY_OWNED(pdip));
6843  
6844  	devstr = i_ddi_strdup(devname, KM_SLEEP);
6845  	i_ddi_parse_name(devstr, &cname, &caddr, NULL);
6846  
6847  	if (cname == NULL || caddr == NULL) {
6848  		kmem_free(devstr, strlen(devname)+1);
6849  		return ((dev_info_t *)NULL);
6850  	}
6851  
6852  	child = find_sibling(ddi_get_child(pdip), cname, caddr,
6853  	    FIND_NODE_BY_NODENAME, NULL);
6854  	kmem_free(devstr, strlen(devname)+1);
6855  	return (child);
6856  }
6857  
6858  /*
6859   * Misc. routines called by framework only
6860   */
6861  
6862  /*
6863   * Clear the DEVI_MADE_CHILDREN/DEVI_ATTACHED_CHILDREN flags
6864   * if new child spec has been added.
6865   */
6866  static int
reset_nexus_flags(dev_info_t * dip,void * arg)6867  reset_nexus_flags(dev_info_t *dip, void *arg)
6868  {
6869  	struct hwc_spec	*list;
6870  
6871  	if (((DEVI(dip)->devi_flags & DEVI_MADE_CHILDREN) == 0) ||
6872  	    ((list = hwc_get_child_spec(dip, (major_t)(uintptr_t)arg)) == NULL))
6873  		return (DDI_WALK_CONTINUE);
6874  
6875  	hwc_free_spec_list(list);
6876  
6877  	/* coordinate child state update */
6878  	ndi_devi_enter(dip);
6879  	mutex_enter(&DEVI(dip)->devi_lock);
6880  	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN | DEVI_ATTACHED_CHILDREN);
6881  	mutex_exit(&DEVI(dip)->devi_lock);
6882  	ndi_devi_exit(dip);
6883  
6884  	return (DDI_WALK_CONTINUE);
6885  }
6886  
6887  /*
6888   * Helper functions, returns NULL if no memory.
6889   */
6890  
6891  /*
6892   * path_to_major:
6893   *
6894   * Return an alternate driver name binding for the leaf device
6895   * of the given pathname, if there is one. The purpose of this
6896   * function is to deal with generic pathnames. The default action
6897   * for platforms that can't do this (ie: x86 or any platform that
6898   * does not have prom_finddevice functionality, which matches
6899   * nodenames and unit-addresses without the drivers participation)
6900   * is to return DDI_MAJOR_T_NONE.
6901   *
6902   * Used in loadrootmodules() in the swapgeneric module to
6903   * associate a given pathname with a given leaf driver.
6904   *
6905   */
6906  major_t
path_to_major(char * path)6907  path_to_major(char *path)
6908  {
6909  	dev_info_t *dip;
6910  	char *p, *q;
6911  	pnode_t nodeid;
6912  	major_t major;
6913  
6914  	/* check for path-oriented alias */
6915  	major = ddi_name_to_major(path);
6916  	if (driver_active(major)) {
6917  		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s path bound %s\n",
6918  		    path, ddi_major_to_name(major)));
6919  		return (major);
6920  	}
6921  
6922  	/*
6923  	 * Get the nodeid of the given pathname, if such a mapping exists.
6924  	 */
6925  	dip = NULL;
6926  	nodeid = prom_finddevice(path);
6927  	if (nodeid != OBP_BADNODE) {
6928  		/*
6929  		 * Find the nodeid in our copy of the device tree and return
6930  		 * whatever name we used to bind this node to a driver.
6931  		 */
6932  		dip = e_ddi_nodeid_to_dip(nodeid);
6933  	}
6934  
6935  	if (dip == NULL) {
6936  		NDI_CONFIG_DEBUG((CE_WARN,
6937  		    "path_to_major: can't bind <%s>\n", path));
6938  		return (DDI_MAJOR_T_NONE);
6939  	}
6940  
6941  	/*
6942  	 * If we're bound to something other than the nodename,
6943  	 * note that in the message buffer and system log.
6944  	 */
6945  	p = ddi_binding_name(dip);
6946  	q = ddi_node_name(dip);
6947  	if (p && q && (strcmp(p, q) != 0))
6948  		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s bound to %s\n",
6949  		    path, p));
6950  
6951  	major = ddi_name_to_major(p);
6952  
6953  	ndi_rele_devi(dip);		/* release e_ddi_nodeid_to_dip hold */
6954  
6955  	return (major);
6956  }
6957  
6958  /*
6959   * Return the held dip for the specified major and instance, attempting to do
6960   * an attach if specified. Return NULL if the devi can't be found or put in
6961   * the proper state. The caller must release the hold via ddi_release_devi if
6962   * a non-NULL value is returned.
6963   *
6964   * Some callers expect to be able to perform a hold_devi() while in a context
6965   * where using ndi_devi_enter() to ensure the hold might cause deadlock (see
6966   * open-from-attach code in consconfig_dacf.c). Such special-case callers
6967   * must ensure that an ndi_devi_enter(parent)/ndi_hold_devi() from a safe
6968   * context is already active. The hold_devi() implementation must accommodate
6969   * these callers.
6970   */
6971  static dev_info_t *
hold_devi(major_t major,int instance,int flags)6972  hold_devi(major_t major, int instance, int flags)
6973  {
6974  	struct devnames	*dnp;
6975  	dev_info_t	*dip;
6976  	char		*path;
6977  	char		*vpath;
6978  
6979  	if ((major >= devcnt) || (instance == -1))
6980  		return (NULL);
6981  
6982  	/* try to find the instance in the per driver list */
6983  	dnp = &(devnamesp[major]);
6984  	LOCK_DEV_OPS(&(dnp->dn_lock));
6985  	for (dip = dnp->dn_head; dip;
6986  	    dip = (dev_info_t *)DEVI(dip)->devi_next) {
6987  		/* skip node if instance field is not valid */
6988  		if (i_ddi_node_state(dip) < DS_INITIALIZED)
6989  			continue;
6990  
6991  		/* look for instance match */
6992  		if (DEVI(dip)->devi_instance == instance) {
6993  			/*
6994  			 * To accommodate callers that can't block in
6995  			 * ndi_devi_enter() we do an ndi_hold_devi(), and
6996  			 * afterwards check that the node is in a state where
6997  			 * the hold prevents detach(). If we did not manage to
6998  			 * prevent detach then we ndi_rele_devi() and perform
6999  			 * the slow path below (which can result in a blocking
7000  			 * ndi_devi_enter() while driving attach top-down).
7001  			 * This code depends on the ordering of
7002  			 * DEVI_SET_DETACHING and the devi_ref check in the
7003  			 * detach_node() code path.
7004  			 */
7005  			ndi_hold_devi(dip);
7006  			if (i_ddi_devi_attached(dip) &&
7007  			    !DEVI_IS_DETACHING(dip)) {
7008  				UNLOCK_DEV_OPS(&(dnp->dn_lock));
7009  				return (dip);	/* fast-path with devi held */
7010  			}
7011  			ndi_rele_devi(dip);
7012  
7013  			/* try slow-path */
7014  			dip = NULL;
7015  			break;
7016  		}
7017  	}
7018  	ASSERT(dip == NULL);
7019  	UNLOCK_DEV_OPS(&(dnp->dn_lock));
7020  
7021  	if (flags & E_DDI_HOLD_DEVI_NOATTACH)
7022  		return (NULL);		/* told not to drive attach */
7023  
7024  	/* slow-path may block, so it should not occur from interrupt */
7025  	ASSERT(!servicing_interrupt());
7026  	if (servicing_interrupt())
7027  		return (NULL);
7028  
7029  	/* reconstruct the path and drive attach by path through devfs. */
7030  	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7031  	if (e_ddi_majorinstance_to_path(major, instance, path) == 0) {
7032  		dip = e_ddi_hold_devi_by_path(path, flags);
7033  
7034  		/*
7035  		 * Verify that we got the correct device - a path_to_inst file
7036  		 * with a bogus/corrupt path (or a nexus that changes its
7037  		 * unit-address format) could result in an incorrect answer
7038  		 *
7039  		 * Verify major, instance, and path.
7040  		 */
7041  		vpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7042  		if (dip &&
7043  		    ((DEVI(dip)->devi_major != major) ||
7044  		    ((DEVI(dip)->devi_instance != instance)) ||
7045  		    (strcmp(path, ddi_pathname(dip, vpath)) != 0))) {
7046  			ndi_rele_devi(dip);
7047  			dip = NULL;	/* no answer better than wrong answer */
7048  		}
7049  		kmem_free(vpath, MAXPATHLEN);
7050  	}
7051  	kmem_free(path, MAXPATHLEN);
7052  	return (dip);			/* with devi held */
7053  }
7054  
7055  /*
7056   * The {e_}ddi_hold_devi{_by_{instance|dev|path}} hold the devinfo node
7057   * associated with the specified arguments.  This hold should be released
7058   * by calling ddi_release_devi.
7059   *
7060   * The E_DDI_HOLD_DEVI_NOATTACH flag argument allows the caller to to specify
7061   * a failure return if the node is not already attached.
7062   *
7063   * NOTE: by the time we make e_ddi_hold_devi public, we should be able to reuse
7064   * ddi_hold_devi again.
7065   */
7066  dev_info_t *
ddi_hold_devi_by_instance(major_t major,int instance,int flags)7067  ddi_hold_devi_by_instance(major_t major, int instance, int flags)
7068  {
7069  	return (hold_devi(major, instance, flags));
7070  }
7071  
7072  dev_info_t *
e_ddi_hold_devi_by_dev(dev_t dev,int flags)7073  e_ddi_hold_devi_by_dev(dev_t dev, int flags)
7074  {
7075  	major_t	major = getmajor(dev);
7076  	dev_info_t	*dip;
7077  	struct dev_ops	*ops;
7078  	dev_info_t	*ddip = NULL;
7079  
7080  	dip = hold_devi(major, dev_to_instance(dev), flags);
7081  
7082  	/*
7083  	 * The rest of this routine is legacy support for drivers that
7084  	 * have broken DDI_INFO_DEVT2INSTANCE implementations but may have
7085  	 * functional DDI_INFO_DEVT2DEVINFO implementations.  This code will
7086  	 * diagnose inconsistency and, for maximum compatibility with legacy
7087  	 * drivers, give preference to the drivers DDI_INFO_DEVT2DEVINFO
7088  	 * implementation over the above derived dip based the driver's
7089  	 * DDI_INFO_DEVT2INSTANCE implementation. This legacy support should
7090  	 * be removed when DDI_INFO_DEVT2DEVINFO is deprecated.
7091  	 *
7092  	 * NOTE: The following code has a race condition. DEVT2DEVINFO
7093  	 *	returns a dip which is not held. By the time we ref ddip,
7094  	 *	it could have been freed. The saving grace is that for
7095  	 *	most drivers, the dip returned from hold_devi() is the
7096  	 *	same one as the one returned by DEVT2DEVINFO, so we are
7097  	 *	safe for drivers with the correct getinfo(9e) impl.
7098  	 */
7099  	if (((ops = ddi_hold_driver(major)) != NULL) &&
7100  	    CB_DRV_INSTALLED(ops) && ops->devo_getinfo)  {
7101  		if ((*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2DEVINFO,
7102  		    (void *)dev, (void **)&ddip) != DDI_SUCCESS)
7103  			ddip = NULL;
7104  	}
7105  
7106  	/* give preference to the driver returned DEVT2DEVINFO dip */
7107  	if (ddip && (dip != ddip)) {
7108  #ifdef	DEBUG
7109  		cmn_err(CE_WARN, "%s: inconsistent getinfo(9E) implementation",
7110  		    ddi_driver_name(ddip));
7111  #endif	/* DEBUG */
7112  		ndi_hold_devi(ddip);
7113  		if (dip)
7114  			ndi_rele_devi(dip);
7115  		dip = ddip;
7116  	}
7117  
7118  	if (ops)
7119  		ddi_rele_driver(major);
7120  
7121  	return (dip);
7122  }
7123  
7124  /*
7125   * For compatibility only. Do not call this function!
7126   */
7127  dev_info_t *
e_ddi_get_dev_info(dev_t dev,vtype_t type)7128  e_ddi_get_dev_info(dev_t dev, vtype_t type)
7129  {
7130  	dev_info_t *dip = NULL;
7131  	if (getmajor(dev) >= devcnt)
7132  		return (NULL);
7133  
7134  	switch (type) {
7135  	case VCHR:
7136  	case VBLK:
7137  		dip = e_ddi_hold_devi_by_dev(dev, 0);
7138  	default:
7139  		break;
7140  	}
7141  
7142  	/*
7143  	 * For compatibility reasons, we can only return the dip with
7144  	 * the driver ref count held. This is not a safe thing to do.
7145  	 * For certain broken third-party software, we are willing
7146  	 * to venture into unknown territory.
7147  	 */
7148  	if (dip) {
7149  		(void) ndi_hold_driver(dip);
7150  		ndi_rele_devi(dip);
7151  	}
7152  	return (dip);
7153  }
7154  
7155  dev_info_t *
e_ddi_hold_devi_by_path(char * path,int flags)7156  e_ddi_hold_devi_by_path(char *path, int flags)
7157  {
7158  	dev_info_t	*dip;
7159  
7160  	/* can't specify NOATTACH by path */
7161  	ASSERT(!(flags & E_DDI_HOLD_DEVI_NOATTACH));
7162  
7163  	return (resolve_pathname(path, &dip, NULL, NULL) ? NULL : dip);
7164  }
7165  
7166  void
e_ddi_hold_devi(dev_info_t * dip)7167  e_ddi_hold_devi(dev_info_t *dip)
7168  {
7169  	ndi_hold_devi(dip);
7170  }
7171  
7172  void
ddi_release_devi(dev_info_t * dip)7173  ddi_release_devi(dev_info_t *dip)
7174  {
7175  	ndi_rele_devi(dip);
7176  }
7177  
7178  /*
7179   * Associate a streams queue with a devinfo node
7180   * NOTE: This function is called by STREAM driver's put procedure.
7181   *	It cannot block.
7182   */
7183  void
ddi_assoc_queue_with_devi(queue_t * q,dev_info_t * dip)7184  ddi_assoc_queue_with_devi(queue_t *q, dev_info_t *dip)
7185  {
7186  	queue_t *rq = _RD(q);
7187  	struct stdata *stp;
7188  	vnode_t *vp;
7189  
7190  	/* set flag indicating that ddi_assoc_queue_with_devi was called */
7191  	mutex_enter(QLOCK(rq));
7192  	rq->q_flag |= _QASSOCIATED;
7193  	mutex_exit(QLOCK(rq));
7194  
7195  	/* get the vnode associated with the queue */
7196  	stp = STREAM(rq);
7197  	vp = stp->sd_vnode;
7198  	ASSERT(vp);
7199  
7200  	/* change the hardware association of the vnode */
7201  	spec_assoc_vp_with_devi(vp, dip);
7202  }
7203  
7204  /*
7205   * ddi_install_driver(name)
7206   *
7207   * Driver installation is currently a byproduct of driver loading.  This
7208   * may change.
7209   */
7210  int
ddi_install_driver(char * name)7211  ddi_install_driver(char *name)
7212  {
7213  	major_t major = ddi_name_to_major(name);
7214  
7215  	if ((major == DDI_MAJOR_T_NONE) ||
7216  	    (ddi_hold_installed_driver(major) == NULL)) {
7217  		return (DDI_FAILURE);
7218  	}
7219  	ddi_rele_driver(major);
7220  	return (DDI_SUCCESS);
7221  }
7222  
7223  struct dev_ops *
ddi_hold_driver(major_t major)7224  ddi_hold_driver(major_t major)
7225  {
7226  	return (mod_hold_dev_by_major(major));
7227  }
7228  
7229  
7230  void
ddi_rele_driver(major_t major)7231  ddi_rele_driver(major_t major)
7232  {
7233  	mod_rele_dev_by_major(major);
7234  }
7235  
7236  
7237  /*
7238   * This is called during boot to force attachment order of special dips
7239   * dip must be referenced via ndi_hold_devi()
7240   */
7241  int
i_ddi_attach_node_hierarchy(dev_info_t * dip)7242  i_ddi_attach_node_hierarchy(dev_info_t *dip)
7243  {
7244  	dev_info_t	*parent;
7245  	int		ret;
7246  
7247  	/*
7248  	 * Recurse up until attached parent is found.
7249  	 */
7250  	if (i_ddi_devi_attached(dip))
7251  		return (DDI_SUCCESS);
7252  	parent = ddi_get_parent(dip);
7253  	if (i_ddi_attach_node_hierarchy(parent) != DDI_SUCCESS)
7254  		return (DDI_FAILURE);
7255  
7256  	/*
7257  	 * Come top-down, expanding .conf nodes under this parent
7258  	 * and driving attach.
7259  	 */
7260  	ndi_devi_enter(parent);
7261  	(void) i_ndi_make_spec_children(parent, 0);
7262  	ret = i_ddi_attachchild(dip);
7263  	ndi_devi_exit(parent);
7264  
7265  	return (ret);
7266  }
7267  
7268  /* keep this function static */
7269  static int
attach_driver_nodes(major_t major)7270  attach_driver_nodes(major_t major)
7271  {
7272  	struct devnames *dnp;
7273  	dev_info_t *dip;
7274  	int error = DDI_FAILURE;
7275  
7276  	dnp = &devnamesp[major];
7277  	LOCK_DEV_OPS(&dnp->dn_lock);
7278  	dip = dnp->dn_head;
7279  	while (dip) {
7280  		ndi_hold_devi(dip);
7281  		UNLOCK_DEV_OPS(&dnp->dn_lock);
7282  		if (i_ddi_attach_node_hierarchy(dip) == DDI_SUCCESS)
7283  			error = DDI_SUCCESS;
7284  		/*
7285  		 * Set the 'ddi-config-driver-node' property on a nexus
7286  		 * node to cause attach_driver_nodes() to configure all
7287  		 * immediate children of the nexus. This property should
7288  		 * be set on nodes with immediate children that bind to
7289  		 * the same driver as parent.
7290  		 */
7291  		if ((error == DDI_SUCCESS) && (ddi_prop_exists(DDI_DEV_T_ANY,
7292  		    dip, DDI_PROP_DONTPASS, "ddi-config-driver-node"))) {
7293  			(void) ndi_devi_config(dip, NDI_NO_EVENT);
7294  		}
7295  		LOCK_DEV_OPS(&dnp->dn_lock);
7296  		ndi_rele_devi(dip);
7297  		dip = ddi_get_next(dip);
7298  	}
7299  	if (error == DDI_SUCCESS)
7300  		dnp->dn_flags |= DN_NO_AUTODETACH;
7301  	UNLOCK_DEV_OPS(&dnp->dn_lock);
7302  
7303  
7304  	return (error);
7305  }
7306  
7307  /*
7308   * i_ddi_attach_hw_nodes configures and attaches all hw nodes
7309   * bound to a specific driver. This function replaces calls to
7310   * ddi_hold_installed_driver() for drivers with no .conf
7311   * enumerated nodes.
7312   *
7313   * This facility is typically called at boot time to attach
7314   * platform-specific hardware nodes, such as ppm nodes on xcal
7315   * and grover and keyswitch nodes on cherrystone. It does not
7316   * deal with .conf enumerated node. Calling it beyond the boot
7317   * process is strongly discouraged.
7318   */
7319  int
i_ddi_attach_hw_nodes(char * driver)7320  i_ddi_attach_hw_nodes(char *driver)
7321  {
7322  	major_t major;
7323  
7324  	major = ddi_name_to_major(driver);
7325  	if (major == DDI_MAJOR_T_NONE)
7326  		return (DDI_FAILURE);
7327  
7328  	return (attach_driver_nodes(major));
7329  }
7330  
7331  /*
7332   * i_ddi_attach_pseudo_node configures pseudo drivers which
7333   * has a single node. The .conf nodes must be enumerated
7334   * before calling this interface. The dip is held attached
7335   * upon returning.
7336   *
7337   * This facility should only be called only at boot time
7338   * by the I/O framework.
7339   */
7340  dev_info_t *
i_ddi_attach_pseudo_node(char * driver)7341  i_ddi_attach_pseudo_node(char *driver)
7342  {
7343  	major_t major;
7344  	dev_info_t *dip;
7345  
7346  	major = ddi_name_to_major(driver);
7347  	if (major == DDI_MAJOR_T_NONE)
7348  		return (NULL);
7349  
7350  	if (attach_driver_nodes(major) != DDI_SUCCESS)
7351  		return (NULL);
7352  
7353  	dip = devnamesp[major].dn_head;
7354  	ASSERT(dip && ddi_get_next(dip) == NULL);
7355  	ndi_hold_devi(dip);
7356  	return (dip);
7357  }
7358  
7359  static void
diplist_to_parent_major(dev_info_t * head,char parents[])7360  diplist_to_parent_major(dev_info_t *head, char parents[])
7361  {
7362  	major_t major;
7363  	dev_info_t *dip, *pdip;
7364  
7365  	for (dip = head; dip != NULL; dip = ddi_get_next(dip)) {
7366  		pdip = ddi_get_parent(dip);
7367  		ASSERT(pdip);	/* disallow rootnex.conf nodes */
7368  		major = ddi_driver_major(pdip);
7369  		if ((major != DDI_MAJOR_T_NONE) && parents[major] == 0)
7370  			parents[major] = 1;
7371  	}
7372  }
7373  
7374  /*
7375   * Call ddi_hold_installed_driver() on each parent major
7376   * and invoke mt_config_driver() to attach child major.
7377   * This is part of the implementation of ddi_hold_installed_driver.
7378   */
7379  static int
attach_driver_by_parent(major_t child_major,char parents[])7380  attach_driver_by_parent(major_t child_major, char parents[])
7381  {
7382  	major_t par_major;
7383  	struct mt_config_handle *hdl;
7384  	int flags = NDI_DEVI_PERSIST | NDI_NO_EVENT;
7385  
7386  	hdl = mt_config_init(NULL, NULL, flags, child_major, MT_CONFIG_OP,
7387  	    NULL);
7388  	for (par_major = 0; par_major < devcnt; par_major++) {
7389  		/* disallow recursion on the same driver */
7390  		if (parents[par_major] == 0 || par_major == child_major)
7391  			continue;
7392  		if (ddi_hold_installed_driver(par_major) == NULL)
7393  			continue;
7394  		hdl->mtc_parmajor = par_major;
7395  		mt_config_driver(hdl);
7396  		ddi_rele_driver(par_major);
7397  	}
7398  	(void) mt_config_fini(hdl);
7399  
7400  	return (i_ddi_devs_attached(child_major));
7401  }
7402  
7403  int
i_ddi_devs_attached(major_t major)7404  i_ddi_devs_attached(major_t major)
7405  {
7406  	dev_info_t *dip;
7407  	struct devnames *dnp;
7408  	int error = DDI_FAILURE;
7409  
7410  	/* check for attached instances */
7411  	dnp = &devnamesp[major];
7412  	LOCK_DEV_OPS(&dnp->dn_lock);
7413  	for (dip = dnp->dn_head; dip != NULL; dip = ddi_get_next(dip)) {
7414  		if (i_ddi_devi_attached(dip)) {
7415  			error = DDI_SUCCESS;
7416  			break;
7417  		}
7418  	}
7419  	UNLOCK_DEV_OPS(&dnp->dn_lock);
7420  
7421  	return (error);
7422  }
7423  
7424  int
i_ddi_minor_node_count(dev_info_t * ddip,const char * node_type)7425  i_ddi_minor_node_count(dev_info_t *ddip, const char *node_type)
7426  {
7427  	struct ddi_minor_data	*dp;
7428  	int			count = 0;
7429  
7430  	ndi_devi_enter(ddip);
7431  	for (dp = DEVI(ddip)->devi_minor; dp != NULL; dp = dp->next) {
7432  		if (strcmp(dp->ddm_node_type, node_type) == 0)
7433  			count++;
7434  	}
7435  	ndi_devi_exit(ddip);
7436  	return (count);
7437  }
7438  
7439  /*
7440   * ddi_hold_installed_driver configures and attaches all
7441   * instances of the specified driver. To accomplish this
7442   * it configures and attaches all possible parents of
7443   * the driver, enumerated both in h/w nodes and in the
7444   * driver's .conf file.
7445   *
7446   * NOTE: This facility is for compatibility purposes only and will
7447   *	eventually go away. Its usage is strongly discouraged.
7448   */
7449  static void
enter_driver(struct devnames * dnp)7450  enter_driver(struct devnames *dnp)
7451  {
7452  	mutex_enter(&dnp->dn_lock);
7453  	ASSERT(dnp->dn_busy_thread != curthread);
7454  	while (dnp->dn_flags & DN_DRIVER_BUSY)
7455  		cv_wait(&dnp->dn_wait, &dnp->dn_lock);
7456  	dnp->dn_flags |= DN_DRIVER_BUSY;
7457  	dnp->dn_busy_thread = curthread;
7458  	mutex_exit(&dnp->dn_lock);
7459  }
7460  
7461  static void
exit_driver(struct devnames * dnp)7462  exit_driver(struct devnames *dnp)
7463  {
7464  	mutex_enter(&dnp->dn_lock);
7465  	ASSERT(dnp->dn_busy_thread == curthread);
7466  	dnp->dn_flags &= ~DN_DRIVER_BUSY;
7467  	dnp->dn_busy_thread = NULL;
7468  	cv_broadcast(&dnp->dn_wait);
7469  	mutex_exit(&dnp->dn_lock);
7470  }
7471  
7472  struct dev_ops *
ddi_hold_installed_driver(major_t major)7473  ddi_hold_installed_driver(major_t major)
7474  {
7475  	struct dev_ops *ops;
7476  	struct devnames *dnp;
7477  	char *parents;
7478  	int error;
7479  
7480  	ops = ddi_hold_driver(major);
7481  	if (ops == NULL)
7482  		return (NULL);
7483  
7484  	/*
7485  	 * Return immediately if all the attach operations associated
7486  	 * with a ddi_hold_installed_driver() call have already been done.
7487  	 */
7488  	dnp = &devnamesp[major];
7489  	enter_driver(dnp);
7490  	ASSERT(driver_active(major));
7491  
7492  	if (dnp->dn_flags & DN_DRIVER_HELD) {
7493  		exit_driver(dnp);
7494  		if (i_ddi_devs_attached(major) == DDI_SUCCESS)
7495  			return (ops);
7496  		ddi_rele_driver(major);
7497  		return (NULL);
7498  	}
7499  
7500  	LOCK_DEV_OPS(&dnp->dn_lock);
7501  	dnp->dn_flags |= (DN_DRIVER_HELD | DN_NO_AUTODETACH);
7502  	UNLOCK_DEV_OPS(&dnp->dn_lock);
7503  
7504  	DCOMPATPRINTF((CE_CONT,
7505  	    "ddi_hold_installed_driver: %s\n", dnp->dn_name));
7506  
7507  	/*
7508  	 * When the driver has no .conf children, it is sufficient
7509  	 * to attach existing nodes in the device tree. Nodes not
7510  	 * enumerated by the OBP are not attached.
7511  	 */
7512  	if (dnp->dn_pl == NULL) {
7513  		if (attach_driver_nodes(major) == DDI_SUCCESS) {
7514  			exit_driver(dnp);
7515  			return (ops);
7516  		}
7517  		exit_driver(dnp);
7518  		ddi_rele_driver(major);
7519  		return (NULL);
7520  	}
7521  
7522  	/*
7523  	 * Driver has .conf nodes. We find all possible parents
7524  	 * and recursively all ddi_hold_installed_driver on the
7525  	 * parent driver; then we invoke ndi_config_driver()
7526  	 * on all possible parent node in parallel to speed up
7527  	 * performance.
7528  	 */
7529  	parents = kmem_zalloc(devcnt * sizeof (char), KM_SLEEP);
7530  
7531  	LOCK_DEV_OPS(&dnp->dn_lock);
7532  	/* find .conf parents */
7533  	(void) impl_parlist_to_major(dnp->dn_pl, parents);
7534  	/* find hw node parents */
7535  	diplist_to_parent_major(dnp->dn_head, parents);
7536  	UNLOCK_DEV_OPS(&dnp->dn_lock);
7537  
7538  	error = attach_driver_by_parent(major, parents);
7539  	kmem_free(parents, devcnt * sizeof (char));
7540  	if (error == DDI_SUCCESS) {
7541  		exit_driver(dnp);
7542  		return (ops);
7543  	}
7544  
7545  	exit_driver(dnp);
7546  	ddi_rele_driver(major);
7547  	return (NULL);
7548  }
7549  
7550  /*
7551   * Default bus_config entry point for nexus drivers
7552   */
7553  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)7554  ndi_busop_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
7555      void *arg, dev_info_t **child, clock_t timeout)
7556  {
7557  	major_t major;
7558  
7559  	/*
7560  	 * A timeout of 30 minutes or more is probably a mistake
7561  	 * This is intended to catch uses where timeout is in
7562  	 * the wrong units.  timeout must be in units of ticks.
7563  	 */
7564  	ASSERT(timeout < SEC_TO_TICK(1800));
7565  
7566  	major = DDI_MAJOR_T_NONE;
7567  	switch (op) {
7568  	case BUS_CONFIG_ONE:
7569  		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config %s timeout=%ld\n",
7570  		    ddi_driver_name(pdip), ddi_get_instance(pdip),
7571  		    (char *)arg, timeout));
7572  		return (devi_config_one(pdip, (char *)arg, child, flags,
7573  		    timeout));
7574  
7575  	case BUS_CONFIG_DRIVER:
7576  		major = (major_t)(uintptr_t)arg;
7577  		/*FALLTHROUGH*/
7578  	case BUS_CONFIG_ALL:
7579  		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config timeout=%ld\n",
7580  		    ddi_driver_name(pdip), ddi_get_instance(pdip),
7581  		    timeout));
7582  		if (timeout > 0) {
7583  			NDI_DEBUG(flags, (CE_CONT,
7584  			    "%s%d: bus config all timeout=%ld\n",
7585  			    ddi_driver_name(pdip), ddi_get_instance(pdip),
7586  			    timeout));
7587  			delay(timeout);
7588  		}
7589  		return (config_immediate_children(pdip, flags, major));
7590  
7591  	default:
7592  		return (NDI_FAILURE);
7593  	}
7594  	/*NOTREACHED*/
7595  }
7596  
7597  /*
7598   * Default busop bus_unconfig handler for nexus drivers
7599   */
7600  int
ndi_busop_bus_unconfig(dev_info_t * pdip,uint_t flags,ddi_bus_config_op_t op,void * arg)7601  ndi_busop_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
7602      void *arg)
7603  {
7604  	major_t major;
7605  
7606  	major = DDI_MAJOR_T_NONE;
7607  	switch (op) {
7608  	case BUS_UNCONFIG_ONE:
7609  		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig %s\n",
7610  		    ddi_driver_name(pdip), ddi_get_instance(pdip),
7611  		    (char *)arg));
7612  		return (devi_unconfig_one(pdip, (char *)arg, flags));
7613  
7614  	case BUS_UNCONFIG_DRIVER:
7615  		major = (major_t)(uintptr_t)arg;
7616  		/*FALLTHROUGH*/
7617  	case BUS_UNCONFIG_ALL:
7618  		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig all\n",
7619  		    ddi_driver_name(pdip), ddi_get_instance(pdip)));
7620  		return (unconfig_immediate_children(pdip, NULL, flags, major));
7621  
7622  	default:
7623  		return (NDI_FAILURE);
7624  	}
7625  	/*NOTREACHED*/
7626  }
7627  
7628  /*
7629   * dummy functions to be removed
7630   */
7631  void
impl_rem_dev_props(dev_info_t * dip)7632  impl_rem_dev_props(dev_info_t *dip)
7633  {
7634  	_NOTE(ARGUNUSED(dip))
7635  	/* do nothing */
7636  }
7637  
7638  /*
7639   * Determine if a node is a leaf node. If not sure, return false (0).
7640   */
7641  static int
is_leaf_node(dev_info_t * dip)7642  is_leaf_node(dev_info_t *dip)
7643  {
7644  	major_t major = ddi_driver_major(dip);
7645  
7646  	if (major == DDI_MAJOR_T_NONE)
7647  		return (0);
7648  
7649  	return (devnamesp[major].dn_flags & DN_LEAF_DRIVER);
7650  }
7651  
7652  /*
7653   * Multithreaded [un]configuration
7654   */
7655  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)7656  mt_config_init(dev_info_t *pdip, dev_info_t **dipp, int flags,
7657      major_t major, int op, struct brevq_node **brevqp)
7658  {
7659  	struct mt_config_handle	*hdl = kmem_alloc(sizeof (*hdl), KM_SLEEP);
7660  
7661  	mutex_init(&hdl->mtc_lock, NULL, MUTEX_DEFAULT, NULL);
7662  	cv_init(&hdl->mtc_cv, NULL, CV_DEFAULT, NULL);
7663  	hdl->mtc_pdip = pdip;
7664  	hdl->mtc_fdip = dipp;
7665  	hdl->mtc_parmajor = DDI_MAJOR_T_NONE;
7666  	hdl->mtc_flags = flags;
7667  	hdl->mtc_major = major;
7668  	hdl->mtc_thr_count = 0;
7669  	hdl->mtc_op = op;
7670  	hdl->mtc_error = 0;
7671  	hdl->mtc_brevqp = brevqp;
7672  
7673  #ifdef DEBUG
7674  	gethrestime(&hdl->start_time);
7675  	hdl->total_time = 0;
7676  #endif /* DEBUG */
7677  
7678  	return (hdl);
7679  }
7680  
7681  #ifdef DEBUG
7682  static int
time_diff_in_msec(timestruc_t start,timestruc_t end)7683  time_diff_in_msec(timestruc_t start, timestruc_t end)
7684  {
7685  	int	nsec, sec;
7686  
7687  	sec = end.tv_sec - start.tv_sec;
7688  	nsec = end.tv_nsec - start.tv_nsec;
7689  	if (nsec < 0) {
7690  		nsec += NANOSEC;
7691  		sec -= 1;
7692  	}
7693  
7694  	return (sec * (NANOSEC >> 20) + (nsec >> 20));
7695  }
7696  
7697  #endif	/* DEBUG */
7698  
7699  static int
mt_config_fini(struct mt_config_handle * hdl)7700  mt_config_fini(struct mt_config_handle *hdl)
7701  {
7702  	int		rv;
7703  #ifdef DEBUG
7704  	int		real_time;
7705  	timestruc_t	end_time;
7706  #endif /* DEBUG */
7707  
7708  	mutex_enter(&hdl->mtc_lock);
7709  	while (hdl->mtc_thr_count > 0)
7710  		cv_wait(&hdl->mtc_cv, &hdl->mtc_lock);
7711  	rv = hdl->mtc_error;
7712  	mutex_exit(&hdl->mtc_lock);
7713  
7714  #ifdef DEBUG
7715  	gethrestime(&end_time);
7716  	real_time = time_diff_in_msec(hdl->start_time, end_time);
7717  	if ((ddidebug & DDI_MTCONFIG) && hdl->mtc_pdip)
7718  		cmn_err(CE_NOTE,
7719  		    "config %s%d: total time %d msec, real time %d msec",
7720  		    ddi_driver_name(hdl->mtc_pdip),
7721  		    ddi_get_instance(hdl->mtc_pdip),
7722  		    hdl->total_time, real_time);
7723  #endif /* DEBUG */
7724  
7725  	cv_destroy(&hdl->mtc_cv);
7726  	mutex_destroy(&hdl->mtc_lock);
7727  	kmem_free(hdl, sizeof (*hdl));
7728  
7729  	return (rv);
7730  }
7731  
7732  struct mt_config_data {
7733  	struct mt_config_handle	*mtc_hdl;
7734  	dev_info_t		*mtc_dip;
7735  	major_t			mtc_major;
7736  	int			mtc_flags;
7737  	struct brevq_node	*mtc_brn;
7738  	struct mt_config_data	*mtc_next;
7739  };
7740  
7741  static void
mt_config_thread(void * arg)7742  mt_config_thread(void *arg)
7743  {
7744  	struct mt_config_data	*mcd = (struct mt_config_data *)arg;
7745  	struct mt_config_handle	*hdl = mcd->mtc_hdl;
7746  	dev_info_t		*dip = mcd->mtc_dip;
7747  	dev_info_t		*rdip, **dipp;
7748  	major_t			major = mcd->mtc_major;
7749  	int			flags = mcd->mtc_flags;
7750  	int			rv = 0;
7751  
7752  #ifdef DEBUG
7753  	timestruc_t start_time, end_time;
7754  	gethrestime(&start_time);
7755  #endif /* DEBUG */
7756  
7757  	rdip = NULL;
7758  	dipp = hdl->mtc_fdip ? &rdip : NULL;
7759  
7760  	switch (hdl->mtc_op) {
7761  	case MT_CONFIG_OP:
7762  		rv = devi_config_common(dip, flags, major);
7763  		break;
7764  	case MT_UNCONFIG_OP:
7765  		if (mcd->mtc_brn) {
7766  			struct brevq_node *brevq = NULL;
7767  			rv = devi_unconfig_common(dip, dipp, flags, major,
7768  			    &brevq);
7769  			mcd->mtc_brn->brn_child = brevq;
7770  		} else
7771  			rv = devi_unconfig_common(dip, dipp, flags, major,
7772  			    NULL);
7773  		break;
7774  	}
7775  
7776  	mutex_enter(&hdl->mtc_lock);
7777  #ifdef DEBUG
7778  	gethrestime(&end_time);
7779  	hdl->total_time += time_diff_in_msec(start_time, end_time);
7780  #endif /* DEBUG */
7781  
7782  	if ((rv != NDI_SUCCESS) && (hdl->mtc_error == 0)) {
7783  		hdl->mtc_error = rv;
7784  #ifdef	DEBUG
7785  		if ((ddidebug & DDI_DEBUG) && (major != DDI_MAJOR_T_NONE)) {
7786  			char	*path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7787  
7788  			(void) ddi_pathname(dip, path);
7789  			cmn_err(CE_NOTE, "mt_config_thread: "
7790  			    "op %d.%d.%x at %s failed %d",
7791  			    hdl->mtc_op, major, flags, path, rv);
7792  			kmem_free(path, MAXPATHLEN);
7793  		}
7794  #endif	/* DEBUG */
7795  	}
7796  
7797  	if (hdl->mtc_fdip && *hdl->mtc_fdip == NULL) {
7798  		*hdl->mtc_fdip = rdip;
7799  		rdip = NULL;
7800  	}
7801  
7802  	if (rdip) {
7803  		ASSERT(rv != NDI_SUCCESS);
7804  		ndi_rele_devi(rdip);
7805  	}
7806  
7807  	ndi_rele_devi(dip);
7808  
7809  	if (--hdl->mtc_thr_count == 0)
7810  		cv_broadcast(&hdl->mtc_cv);
7811  	mutex_exit(&hdl->mtc_lock);
7812  	kmem_free(mcd, sizeof (*mcd));
7813  }
7814  
7815  /*
7816   * Multi-threaded config/unconfig of child nexus
7817   */
7818  static void
mt_config_children(struct mt_config_handle * hdl)7819  mt_config_children(struct mt_config_handle *hdl)
7820  {
7821  	dev_info_t		*pdip = hdl->mtc_pdip;
7822  	major_t			major = hdl->mtc_major;
7823  	dev_info_t		*dip;
7824  	struct brevq_node	*brn;
7825  	struct mt_config_data	*mcd_head = NULL;
7826  	struct mt_config_data	*mcd_tail = NULL;
7827  	struct mt_config_data	*mcd;
7828  #ifdef DEBUG
7829  	timestruc_t		end_time;
7830  
7831  	/* Update total_time in handle */
7832  	gethrestime(&end_time);
7833  	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
7834  #endif
7835  
7836  	ndi_devi_enter(pdip);
7837  	dip = ddi_get_child(pdip);
7838  	while (dip) {
7839  		if (hdl->mtc_op == MT_UNCONFIG_OP && hdl->mtc_brevqp &&
7840  		    !(DEVI_EVREMOVE(dip)) &&
7841  		    i_ddi_node_state(dip) >= DS_INITIALIZED) {
7842  			/*
7843  			 * Enqueue this dip's deviname.
7844  			 * No need to hold a lock while enqueuing since this
7845  			 * is the only thread doing the enqueue and no one
7846  			 * walks the queue while we are in multithreaded
7847  			 * unconfiguration.
7848  			 */
7849  			brn = brevq_enqueue(hdl->mtc_brevqp, dip, NULL);
7850  		} else
7851  			brn = NULL;
7852  
7853  		/*
7854  		 * Hold the child that we are processing so it does not get
7855  		 * removed. The corrisponding ndi_rele_devi() for children
7856  		 * that are not being skipped is done at the end of
7857  		 * mt_config_thread().
7858  		 */
7859  		ndi_hold_devi(dip);
7860  
7861  		/*
7862  		 * skip leaf nodes and (for configure) nodes not
7863  		 * fully attached.
7864  		 */
7865  		if (is_leaf_node(dip) ||
7866  		    (hdl->mtc_op == MT_CONFIG_OP &&
7867  		    i_ddi_node_state(dip) < DS_READY)) {
7868  			ndi_rele_devi(dip);
7869  			dip = ddi_get_next_sibling(dip);
7870  			continue;
7871  		}
7872  
7873  		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
7874  		mcd->mtc_dip = dip;
7875  		mcd->mtc_hdl = hdl;
7876  		mcd->mtc_brn = brn;
7877  
7878  		/*
7879  		 * Switch a 'driver' operation to an 'all' operation below a
7880  		 * node bound to the driver.
7881  		 */
7882  		if ((major == DDI_MAJOR_T_NONE) ||
7883  		    (major == ddi_driver_major(dip)))
7884  			mcd->mtc_major = DDI_MAJOR_T_NONE;
7885  		else
7886  			mcd->mtc_major = major;
7887  
7888  		/*
7889  		 * The unconfig-driver to unconfig-all conversion above
7890  		 * constitutes an autodetach for NDI_DETACH_DRIVER calls,
7891  		 * set NDI_AUTODETACH.
7892  		 */
7893  		mcd->mtc_flags = hdl->mtc_flags;
7894  		if ((mcd->mtc_flags & NDI_DETACH_DRIVER) &&
7895  		    (hdl->mtc_op == MT_UNCONFIG_OP) &&
7896  		    (major == ddi_driver_major(pdip)))
7897  			mcd->mtc_flags |= NDI_AUTODETACH;
7898  
7899  		mutex_enter(&hdl->mtc_lock);
7900  		hdl->mtc_thr_count++;
7901  		mutex_exit(&hdl->mtc_lock);
7902  
7903  		/*
7904  		 * Add to end of list to process after ndi_devi_exit to avoid
7905  		 * locking differences depending on value of mtc_off.
7906  		 */
7907  		mcd->mtc_next = NULL;
7908  		if (mcd_head == NULL)
7909  			mcd_head = mcd;
7910  		else
7911  			mcd_tail->mtc_next = mcd;
7912  		mcd_tail = mcd;
7913  
7914  		dip = ddi_get_next_sibling(dip);
7915  	}
7916  	ndi_devi_exit(pdip);
7917  
7918  	/* go through the list of held children */
7919  	for (mcd = mcd_head; mcd; mcd = mcd_head) {
7920  		mcd_head = mcd->mtc_next;
7921  		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
7922  			mt_config_thread(mcd);
7923  		else
7924  			(void) thread_create(NULL, 0, mt_config_thread, mcd,
7925  			    0, &p0, TS_RUN, minclsyspri);
7926  	}
7927  }
7928  
7929  static void
mt_config_driver(struct mt_config_handle * hdl)7930  mt_config_driver(struct mt_config_handle *hdl)
7931  {
7932  	major_t			par_major = hdl->mtc_parmajor;
7933  	major_t			major = hdl->mtc_major;
7934  	struct devnames		*dnp = &devnamesp[par_major];
7935  	dev_info_t		*dip;
7936  	struct mt_config_data	*mcd_head = NULL;
7937  	struct mt_config_data	*mcd_tail = NULL;
7938  	struct mt_config_data	*mcd;
7939  #ifdef DEBUG
7940  	timestruc_t		end_time;
7941  
7942  	/* Update total_time in handle */
7943  	gethrestime(&end_time);
7944  	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
7945  #endif
7946  	ASSERT(par_major != DDI_MAJOR_T_NONE);
7947  	ASSERT(major != DDI_MAJOR_T_NONE);
7948  
7949  	LOCK_DEV_OPS(&dnp->dn_lock);
7950  	dip = devnamesp[par_major].dn_head;
7951  	while (dip) {
7952  		/*
7953  		 * Hold the child that we are processing so it does not get
7954  		 * removed. The corrisponding ndi_rele_devi() for children
7955  		 * that are not being skipped is done at the end of
7956  		 * mt_config_thread().
7957  		 */
7958  		ndi_hold_devi(dip);
7959  
7960  		/* skip leaf nodes and nodes not fully attached */
7961  		if (!i_ddi_devi_attached(dip) || is_leaf_node(dip)) {
7962  			ndi_rele_devi(dip);
7963  			dip = ddi_get_next(dip);
7964  			continue;
7965  		}
7966  
7967  		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
7968  		mcd->mtc_dip = dip;
7969  		mcd->mtc_hdl = hdl;
7970  		mcd->mtc_major = major;
7971  		mcd->mtc_flags = hdl->mtc_flags;
7972  
7973  		mutex_enter(&hdl->mtc_lock);
7974  		hdl->mtc_thr_count++;
7975  		mutex_exit(&hdl->mtc_lock);
7976  
7977  		/*
7978  		 * Add to end of list to process after UNLOCK_DEV_OPS to avoid
7979  		 * locking differences depending on value of mtc_off.
7980  		 */
7981  		mcd->mtc_next = NULL;
7982  		if (mcd_head == NULL)
7983  			mcd_head = mcd;
7984  		else
7985  			mcd_tail->mtc_next = mcd;
7986  		mcd_tail = mcd;
7987  
7988  		dip = ddi_get_next(dip);
7989  	}
7990  	UNLOCK_DEV_OPS(&dnp->dn_lock);
7991  
7992  	/* go through the list of held children */
7993  	for (mcd = mcd_head; mcd; mcd = mcd_head) {
7994  		mcd_head = mcd->mtc_next;
7995  		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
7996  			mt_config_thread(mcd);
7997  		else
7998  			(void) thread_create(NULL, 0, mt_config_thread, mcd,
7999  			    0, &p0, TS_RUN, minclsyspri);
8000  	}
8001  }
8002  
8003  /*
8004   * Given the nodeid for a persistent (PROM or SID) node, return
8005   * the corresponding devinfo node
8006   * NOTE: This function will return NULL for .conf nodeids.
8007   */
8008  dev_info_t *
e_ddi_nodeid_to_dip(pnode_t nodeid)8009  e_ddi_nodeid_to_dip(pnode_t nodeid)
8010  {
8011  	dev_info_t		*dip = NULL;
8012  	struct devi_nodeid	*prev, *elem;
8013  
8014  	mutex_enter(&devimap->dno_lock);
8015  
8016  	prev = NULL;
8017  	for (elem = devimap->dno_head; elem; elem = elem->next) {
8018  		if (elem->nodeid == nodeid) {
8019  			ndi_hold_devi(elem->dip);
8020  			dip = elem->dip;
8021  			break;
8022  		}
8023  		prev = elem;
8024  	}
8025  
8026  	/*
8027  	 * Move to head for faster lookup next time
8028  	 */
8029  	if (elem && prev) {
8030  		prev->next = elem->next;
8031  		elem->next = devimap->dno_head;
8032  		devimap->dno_head = elem;
8033  	}
8034  
8035  	mutex_exit(&devimap->dno_lock);
8036  	return (dip);
8037  }
8038  
8039  static void
free_cache_task(void * arg)8040  free_cache_task(void *arg)
8041  {
8042  	ASSERT(arg == NULL);
8043  
8044  	mutex_enter(&di_cache.cache_lock);
8045  
8046  	/*
8047  	 * The cache can be invalidated without holding the lock
8048  	 * but it can be made valid again only while the lock is held.
8049  	 * So if the cache is invalid when the lock is held, it will
8050  	 * stay invalid until lock is released.
8051  	 */
8052  	if (!di_cache.cache_valid)
8053  		i_ddi_di_cache_free(&di_cache);
8054  
8055  	mutex_exit(&di_cache.cache_lock);
8056  
8057  	if (di_cache_debug)
8058  		cmn_err(CE_NOTE, "system_taskq: di_cache freed");
8059  }
8060  
8061  extern int modrootloaded;
8062  
8063  void
i_ddi_di_cache_free(struct di_cache * cache)8064  i_ddi_di_cache_free(struct di_cache *cache)
8065  {
8066  	int	error;
8067  	extern int sys_shutdown;
8068  
8069  	ASSERT(mutex_owned(&cache->cache_lock));
8070  
8071  	if (cache->cache_size) {
8072  		ASSERT(cache->cache_size > 0);
8073  		ASSERT(cache->cache_data);
8074  
8075  		kmem_free(cache->cache_data, cache->cache_size);
8076  		cache->cache_data = NULL;
8077  		cache->cache_size = 0;
8078  
8079  		if (di_cache_debug)
8080  			cmn_err(CE_NOTE, "i_ddi_di_cache_free: freed cachemem");
8081  	} else {
8082  		ASSERT(cache->cache_data == NULL);
8083  		if (di_cache_debug)
8084  			cmn_err(CE_NOTE, "i_ddi_di_cache_free: NULL cache");
8085  	}
8086  
8087  	if (!modrootloaded || rootvp == NULL ||
8088  	    vn_is_readonly(rootvp) || sys_shutdown) {
8089  		if (di_cache_debug) {
8090  			cmn_err(CE_WARN, "/ not mounted/RDONLY. Skip unlink");
8091  		}
8092  		return;
8093  	}
8094  
8095  	error = vn_remove(DI_CACHE_FILE, UIO_SYSSPACE, RMFILE);
8096  	if (di_cache_debug && error && error != ENOENT) {
8097  		cmn_err(CE_WARN, "%s: unlink failed: %d", DI_CACHE_FILE, error);
8098  	} else if (di_cache_debug && !error) {
8099  		cmn_err(CE_NOTE, "i_ddi_di_cache_free: unlinked cache file");
8100  	}
8101  }
8102  
8103  void
i_ddi_di_cache_invalidate()8104  i_ddi_di_cache_invalidate()
8105  {
8106  	int	cache_valid;
8107  
8108  	if (!modrootloaded || !i_ddi_io_initialized()) {
8109  		if (di_cache_debug)
8110  			cmn_err(CE_NOTE, "I/O not inited. Skipping invalidate");
8111  		return;
8112  	}
8113  
8114  	/* Increment devtree generation number. */
8115  	atomic_inc_ulong(&devtree_gen);
8116  
8117  	/* Invalidate the in-core cache and dispatch free on valid->invalid */
8118  	cache_valid = atomic_swap_uint(&di_cache.cache_valid, 0);
8119  	if (cache_valid) {
8120  		/*
8121  		 * This is an optimization to start cleaning up a cached
8122  		 * snapshot early.  For this reason, it is OK for
8123  		 * taskq_dispatach to fail (and it is OK to not track calling
8124  		 * context relative to sleep, and assume NOSLEEP).
8125  		 */
8126  		(void) taskq_dispatch(system_taskq, free_cache_task, NULL,
8127  		    TQ_NOSLEEP);
8128  	}
8129  
8130  	if (di_cache_debug) {
8131  		cmn_err(CE_NOTE, "invalidation");
8132  	}
8133  }
8134  
8135  
8136  static void
i_bind_vhci_node(dev_info_t * dip)8137  i_bind_vhci_node(dev_info_t *dip)
8138  {
8139  	DEVI(dip)->devi_major = ddi_name_to_major(ddi_node_name(dip));
8140  	i_ddi_set_node_state(dip, DS_BOUND);
8141  }
8142  
8143  static char vhci_node_addr[2];
8144  
8145  static int
i_init_vhci_node(dev_info_t * dip)8146  i_init_vhci_node(dev_info_t *dip)
8147  {
8148  	add_global_props(dip);
8149  	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
8150  	if (DEVI(dip)->devi_ops == NULL)
8151  		return (-1);
8152  
8153  	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
8154  	e_ddi_keep_instance(dip);
8155  	vhci_node_addr[0]	= '\0';
8156  	ddi_set_name_addr(dip, vhci_node_addr);
8157  	i_ddi_set_node_state(dip, DS_INITIALIZED);
8158  	return (0);
8159  }
8160  
8161  static void
i_link_vhci_node(dev_info_t * dip)8162  i_link_vhci_node(dev_info_t *dip)
8163  {
8164  	ASSERT(MUTEX_HELD(&global_vhci_lock));
8165  
8166  	/*
8167  	 * scsi_vhci should be kept left most of the device tree.
8168  	 */
8169  	if (scsi_vhci_dip) {
8170  		DEVI(dip)->devi_sibling = DEVI(scsi_vhci_dip)->devi_sibling;
8171  		DEVI(scsi_vhci_dip)->devi_sibling = DEVI(dip);
8172  	} else {
8173  		DEVI(dip)->devi_sibling = DEVI(top_devinfo)->devi_child;
8174  		DEVI(top_devinfo)->devi_child = DEVI(dip);
8175  	}
8176  }
8177  
8178  
8179  /*
8180   * This a special routine to enumerate vhci node (child of rootnex
8181   * node) without holding the ndi_devi_enter() lock. The device node
8182   * is allocated, initialized and brought into DS_READY state before
8183   * inserting into the device tree. The VHCI node is handcrafted
8184   * here to bring the node to DS_READY, similar to rootnex node.
8185   *
8186   * The global_vhci_lock protects linking the node into the device
8187   * as same lock is held before linking/unlinking any direct child
8188   * of rootnex children.
8189   *
8190   * This routine is a workaround to handle a possible deadlock
8191   * that occurs while trying to enumerate node in a different sub-tree
8192   * during _init/_attach entry points.
8193   */
8194  /*ARGSUSED*/
8195  dev_info_t *
ndi_devi_config_vhci(char * drvname,int flags)8196  ndi_devi_config_vhci(char *drvname, int flags)
8197  {
8198  	struct devnames		*dnp;
8199  	dev_info_t		*dip;
8200  	major_t			major = ddi_name_to_major(drvname);
8201  
8202  	if (major == -1)
8203  		return (NULL);
8204  
8205  	/* Make sure we create the VHCI node only once */
8206  	dnp = &devnamesp[major];
8207  	LOCK_DEV_OPS(&dnp->dn_lock);
8208  	if (dnp->dn_head) {
8209  		dip = dnp->dn_head;
8210  		UNLOCK_DEV_OPS(&dnp->dn_lock);
8211  		return (dip);
8212  	}
8213  	UNLOCK_DEV_OPS(&dnp->dn_lock);
8214  
8215  	/* Allocate the VHCI node */
8216  	ndi_devi_alloc_sleep(top_devinfo, drvname, DEVI_SID_NODEID, &dip);
8217  	ndi_hold_devi(dip);
8218  
8219  	/* Mark the node as VHCI */
8220  	DEVI(dip)->devi_node_attributes |= DDI_VHCI_NODE;
8221  
8222  	i_ddi_add_devimap(dip);
8223  	i_bind_vhci_node(dip);
8224  	if (i_init_vhci_node(dip) == -1) {
8225  		ndi_rele_devi(dip);
8226  		(void) ndi_devi_free(dip);
8227  		return (NULL);
8228  	}
8229  
8230  	mutex_enter(&(DEVI(dip)->devi_lock));
8231  	DEVI_SET_ATTACHING(dip);
8232  	mutex_exit(&(DEVI(dip)->devi_lock));
8233  
8234  	if (devi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
8235  		cmn_err(CE_CONT, "Could not attach %s driver", drvname);
8236  		e_ddi_free_instance(dip, vhci_node_addr);
8237  		ndi_rele_devi(dip);
8238  		(void) ndi_devi_free(dip);
8239  		return (NULL);
8240  	}
8241  	mutex_enter(&(DEVI(dip)->devi_lock));
8242  	DEVI_CLR_ATTACHING(dip);
8243  	mutex_exit(&(DEVI(dip)->devi_lock));
8244  
8245  	mutex_enter(&global_vhci_lock);
8246  	i_link_vhci_node(dip);
8247  	mutex_exit(&global_vhci_lock);
8248  	i_ddi_set_node_state(dip, DS_READY);
8249  
8250  	LOCK_DEV_OPS(&dnp->dn_lock);
8251  	dnp->dn_flags |= DN_DRIVER_HELD;
8252  	dnp->dn_head = dip;
8253  	UNLOCK_DEV_OPS(&dnp->dn_lock);
8254  
8255  	i_ndi_devi_report_status_change(dip, NULL);
8256  
8257  	return (dip);
8258  }
8259  
8260  /*
8261   * Maintain DEVI_DEVICE_REMOVED hotplug devi_state for remove/reinsert hotplug
8262   * of open devices. Currently, because of tight coupling between the devfs file
8263   * system and the Solaris device tree, a driver can't always make the device
8264   * tree state (esp devi_node_state) match device hardware hotplug state. Until
8265   * resolved, to overcome this deficiency we use the following interfaces that
8266   * maintain the DEVI_DEVICE_REMOVED devi_state status bit.  These interface
8267   * report current state, and drive operation (like events and cache
8268   * invalidation) when a driver changes remove/insert state of an open device.
8269   *
8270   * The ndi_devi_device_isremoved() returns 1 if the device is currently removed.
8271   *
8272   * The ndi_devi_device_remove() interface declares the device as removed, and
8273   * returns 1 if there was a state change associated with this declaration.
8274   *
8275   * The ndi_devi_device_insert() declares the device as inserted, and returns 1
8276   * if there was a state change associated with this declaration.
8277   */
8278  int
ndi_devi_device_isremoved(dev_info_t * dip)8279  ndi_devi_device_isremoved(dev_info_t *dip)
8280  {
8281  	return (DEVI_IS_DEVICE_REMOVED(dip));
8282  }
8283  
8284  int
ndi_devi_device_remove(dev_info_t * dip)8285  ndi_devi_device_remove(dev_info_t *dip)
8286  {
8287  	ASSERT(dip && ddi_get_parent(dip) &&
8288  	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
8289  
8290  	/* Return if already marked removed. */
8291  	if (ndi_devi_device_isremoved(dip))
8292  		return (0);
8293  
8294  	/* Mark the device as having been physically removed. */
8295  	mutex_enter(&(DEVI(dip)->devi_lock));
8296  	ndi_devi_set_hidden(dip);	/* invisible: lookup/snapshot */
8297  	DEVI_SET_DEVICE_REMOVED(dip);
8298  	DEVI_SET_EVREMOVE(dip);		/* this clears EVADD too */
8299  	mutex_exit(&(DEVI(dip)->devi_lock));
8300  
8301  	/* report remove (as 'removed') */
8302  	i_ndi_devi_report_status_change(dip, NULL);
8303  
8304  	/*
8305  	 * Invalidate the cache to ensure accurate
8306  	 * (di_state() & DI_DEVICE_REMOVED).
8307  	 */
8308  	i_ddi_di_cache_invalidate();
8309  
8310  	/*
8311  	 * Generate sysevent for those interested in removal (either
8312  	 * directly via private EC_DEVFS or indirectly via devfsadmd
8313  	 * generated EC_DEV). This will generate LDI DEVICE_REMOVE
8314  	 * event too.
8315  	 */
8316  	i_ddi_log_devfs_device_remove(dip);
8317  
8318  	return (1);		/* DEVICE_REMOVED state changed */
8319  }
8320  
8321  int
ndi_devi_device_insert(dev_info_t * dip)8322  ndi_devi_device_insert(dev_info_t *dip)
8323  {
8324  	ASSERT(dip && ddi_get_parent(dip) &&
8325  	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
8326  
8327  	/* Return if not marked removed. */
8328  	if (!ndi_devi_device_isremoved(dip))
8329  		return (0);
8330  
8331  	/* Mark the device as having been physically reinserted. */
8332  	mutex_enter(&(DEVI(dip)->devi_lock));
8333  	ndi_devi_clr_hidden(dip);	/* visible: lookup/snapshot */
8334  	DEVI_SET_DEVICE_REINSERTED(dip);
8335  	DEVI_SET_EVADD(dip);		/* this clears EVREMOVE too */
8336  	mutex_exit(&(DEVI(dip)->devi_lock));
8337  
8338  	/* report insert (as 'online') */
8339  	i_ndi_devi_report_status_change(dip, NULL);
8340  
8341  	/*
8342  	 * Invalidate the cache to ensure accurate
8343  	 * (di_state() & DI_DEVICE_REMOVED).
8344  	 */
8345  	i_ddi_di_cache_invalidate();
8346  
8347  	/*
8348  	 * Generate sysevent for those interested in removal (either directly
8349  	 * via EC_DEVFS or indirectly via devfsadmd generated EC_DEV).
8350  	 */
8351  	i_ddi_log_devfs_device_insert(dip);
8352  
8353  	return (1);		/* DEVICE_REMOVED state changed */
8354  }
8355  
8356  /*
8357   * ibt_hw_is_present() returns 0 when there is no IB hardware actively
8358   * running.  This is primarily useful for modules like rpcmod which
8359   * needs a quick check to decide whether or not it should try to use
8360   * InfiniBand
8361   */
8362  int ib_hw_status = 0;
8363  int
ibt_hw_is_present()8364  ibt_hw_is_present()
8365  {
8366  	return (ib_hw_status);
8367  }
8368  
8369  /*
8370   * ASSERT that constraint flag is not set and then set the "retire attempt"
8371   * flag.
8372   */
8373  int
e_ddi_mark_retiring(dev_info_t * dip,void * arg)8374  e_ddi_mark_retiring(dev_info_t *dip, void *arg)
8375  {
8376  	char	**cons_array = (char **)arg;
8377  	char	*path;
8378  	int	constraint;
8379  	int	i;
8380  
8381  	constraint = 0;
8382  	if (cons_array) {
8383  		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8384  		(void) ddi_pathname(dip, path);
8385  		for (i = 0; cons_array[i] != NULL; i++) {
8386  			if (strcmp(path, cons_array[i]) == 0) {
8387  				constraint = 1;
8388  				break;
8389  			}
8390  		}
8391  		kmem_free(path, MAXPATHLEN);
8392  	}
8393  
8394  	mutex_enter(&DEVI(dip)->devi_lock);
8395  	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8396  	DEVI(dip)->devi_flags |= DEVI_RETIRING;
8397  	if (constraint)
8398  		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
8399  	mutex_exit(&DEVI(dip)->devi_lock);
8400  
8401  	RIO_VERBOSE((CE_NOTE, "marked dip as undergoing retire process dip=%p",
8402  	    (void *)dip));
8403  
8404  	if (constraint)
8405  		RIO_DEBUG((CE_NOTE, "marked dip as constrained, dip=%p",
8406  		    (void *)dip));
8407  
8408  	if (MDI_PHCI(dip))
8409  		mdi_phci_mark_retiring(dip, cons_array);
8410  
8411  	return (DDI_WALK_CONTINUE);
8412  }
8413  
8414  static void
free_array(char ** cons_array)8415  free_array(char **cons_array)
8416  {
8417  	int	i;
8418  
8419  	if (cons_array == NULL)
8420  		return;
8421  
8422  	for (i = 0; cons_array[i] != NULL; i++) {
8423  		kmem_free(cons_array[i], strlen(cons_array[i]) + 1);
8424  	}
8425  	kmem_free(cons_array, (i+1) * sizeof (char *));
8426  }
8427  
8428  /*
8429   * Walk *every* node in subtree and check if it blocks, allows or has no
8430   * comment on a proposed retire.
8431   */
8432  int
e_ddi_retire_notify(dev_info_t * dip,void * arg)8433  e_ddi_retire_notify(dev_info_t *dip, void *arg)
8434  {
8435  	int	*constraint = (int *)arg;
8436  
8437  	RIO_DEBUG((CE_NOTE, "retire notify: dip = %p", (void *)dip));
8438  
8439  	(void) e_ddi_offline_notify(dip);
8440  
8441  	mutex_enter(&(DEVI(dip)->devi_lock));
8442  	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
8443  		RIO_DEBUG((CE_WARN, "retire notify: dip in retire "
8444  		    "subtree is not marked: dip = %p", (void *)dip));
8445  		*constraint = 0;
8446  	} else if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
8447  		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8448  		RIO_DEBUG((CE_NOTE, "retire notify: BLOCKED: dip = %p",
8449  		    (void *)dip));
8450  		*constraint = 0;
8451  	} else if (!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT)) {
8452  		RIO_DEBUG((CE_NOTE, "retire notify: NO CONSTRAINT: "
8453  		    "dip = %p", (void *)dip));
8454  		*constraint = 0;
8455  	} else {
8456  		RIO_DEBUG((CE_NOTE, "retire notify: CONSTRAINT set: "
8457  		    "dip = %p", (void *)dip));
8458  	}
8459  	mutex_exit(&DEVI(dip)->devi_lock);
8460  
8461  	if (MDI_PHCI(dip))
8462  		mdi_phci_retire_notify(dip, constraint);
8463  
8464  	return (DDI_WALK_CONTINUE);
8465  }
8466  
8467  int
e_ddi_retire_finalize(dev_info_t * dip,void * arg)8468  e_ddi_retire_finalize(dev_info_t *dip, void *arg)
8469  {
8470  	int constraint = *(int *)arg;
8471  	int finalize;
8472  	int phci_only;
8473  
8474  	mutex_enter(&DEVI(dip)->devi_lock);
8475  	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
8476  		RIO_DEBUG((CE_WARN,
8477  		    "retire: unmarked dip(%p) in retire subtree",
8478  		    (void *)dip));
8479  		ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRED));
8480  		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8481  		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
8482  		mutex_exit(&DEVI(dip)->devi_lock);
8483  		return (DDI_WALK_CONTINUE);
8484  	}
8485  
8486  	/*
8487  	 * retire the device if constraints have been applied
8488  	 * or if the device is not in use
8489  	 */
8490  	finalize = 0;
8491  	if (constraint) {
8492  		ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
8493  
8494  		ASSERT(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT);
8495  		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
8496  		DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
8497  		DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8498  		DEVI(dip)->devi_flags |= DEVI_RETIRED;
8499  		mutex_exit(&DEVI(dip)->devi_lock);
8500  		(void) spec_fence_snode(dip, NULL);
8501  		RIO_DEBUG((CE_NOTE, "Fenced off: dip = %p", (void *)dip));
8502  		e_ddi_offline_finalize(dip, DDI_SUCCESS);
8503  	} else {
8504  		if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
8505  			ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8506  			DEVI(dip)->devi_flags &= ~DEVI_R_BLOCKED;
8507  			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8508  			/* we have already finalized during notify */
8509  		} else if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
8510  			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
8511  			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8512  			finalize = 1;
8513  		} else {
8514  			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8515  			/*
8516  			 * even if no contracts, need to call finalize
8517  			 * to clear the contract barrier on the dip
8518  			 */
8519  			finalize = 1;
8520  		}
8521  		mutex_exit(&DEVI(dip)->devi_lock);
8522  		RIO_DEBUG((CE_NOTE, "finalize: NOT retired: dip = %p",
8523  		    (void *)dip));
8524  		if (finalize)
8525  			e_ddi_offline_finalize(dip, DDI_FAILURE);
8526  	}
8527  
8528  	/*
8529  	 * phci_only variable indicates no client checking, just
8530  	 * offline the PHCI. We set that to 0 to enable client
8531  	 * checking
8532  	 */
8533  	phci_only = 0;
8534  	if (MDI_PHCI(dip))
8535  		mdi_phci_retire_finalize(dip, phci_only, arg);
8536  
8537  	return (DDI_WALK_CONTINUE);
8538  }
8539  
8540  /*
8541   * Returns
8542   *	DDI_SUCCESS if constraints allow retire
8543   *	DDI_FAILURE if constraints don't allow retire.
8544   * cons_array is a NULL terminated array of node paths for
8545   * which constraints have already been applied.
8546   */
8547  int
e_ddi_retire_device(char * path,char ** cons_array)8548  e_ddi_retire_device(char *path, char **cons_array)
8549  {
8550  	dev_info_t	*dip;
8551  	dev_info_t	*pdip;
8552  	int		constraint;
8553  	char		*devnm;
8554  
8555  	/*
8556  	 * First, lookup the device
8557  	 */
8558  	dip = e_ddi_hold_devi_by_path(path, 0);
8559  	if (dip == NULL) {
8560  		/*
8561  		 * device does not exist. This device cannot be
8562  		 * a critical device since it is not in use. Thus
8563  		 * this device is always retireable. Return DDI_SUCCESS
8564  		 * to indicate this. If this device is ever
8565  		 * instantiated, I/O framework will consult the
8566  		 * the persistent retire store, mark it as
8567  		 * retired and fence it off.
8568  		 */
8569  		RIO_DEBUG((CE_NOTE, "Retire device: device doesn't exist."
8570  		    " NOP. Just returning SUCCESS. path=%s", path));
8571  		free_array(cons_array);
8572  		return (DDI_SUCCESS);
8573  	}
8574  
8575  	RIO_DEBUG((CE_NOTE, "Retire device: found dip = %p.", (void *)dip));
8576  
8577  	pdip = ddi_get_parent(dip);
8578  	ndi_hold_devi(pdip);
8579  
8580  	/*
8581  	 * Run devfs_clean() in case dip has no constraints and is
8582  	 * not in use, so is retireable but there are dv_nodes holding
8583  	 * ref-count on the dip. Note that devfs_clean() always returns
8584  	 * success.
8585  	 */
8586  	devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
8587  	(void) ddi_deviname(dip, devnm);
8588  	(void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
8589  	kmem_free(devnm, MAXNAMELEN + 1);
8590  
8591  	ndi_devi_enter(pdip);
8592  
8593  	/* release hold from e_ddi_hold_devi_by_path */
8594  	ndi_rele_devi(dip);
8595  
8596  	/*
8597  	 * If it cannot make a determination, is_leaf_node() assumes
8598  	 * dip is a nexus.
8599  	 */
8600  	(void) e_ddi_mark_retiring(dip, cons_array);
8601  	if (!is_leaf_node(dip)) {
8602  		ndi_devi_enter(dip);
8603  		ddi_walk_devs(ddi_get_child(dip), e_ddi_mark_retiring,
8604  		    cons_array);
8605  		ndi_devi_exit(dip);
8606  	}
8607  	free_array(cons_array);
8608  
8609  	/*
8610  	 * apply constraints
8611  	 */
8612  	RIO_DEBUG((CE_NOTE, "retire: subtree retire notify: path = %s", path));
8613  
8614  	constraint = 1;	/* assume constraints allow retire */
8615  	(void) e_ddi_retire_notify(dip, &constraint);
8616  	if (!is_leaf_node(dip)) {
8617  		ndi_devi_enter(dip);
8618  		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_notify,
8619  		    &constraint);
8620  		ndi_devi_exit(dip);
8621  	}
8622  
8623  	/*
8624  	 * Now finalize the retire
8625  	 */
8626  	(void) e_ddi_retire_finalize(dip, &constraint);
8627  	if (!is_leaf_node(dip)) {
8628  		ndi_devi_enter(dip);
8629  		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_finalize,
8630  		    &constraint);
8631  		ndi_devi_exit(dip);
8632  	}
8633  
8634  	if (!constraint) {
8635  		RIO_DEBUG((CE_WARN, "retire failed: path = %s", path));
8636  	} else {
8637  		RIO_DEBUG((CE_NOTE, "retire succeeded: path = %s", path));
8638  	}
8639  
8640  	ndi_devi_exit(pdip);
8641  	ndi_rele_devi(pdip);
8642  	return (constraint ? DDI_SUCCESS : DDI_FAILURE);
8643  }
8644  
8645  static int
unmark_and_unfence(dev_info_t * dip,void * arg)8646  unmark_and_unfence(dev_info_t *dip, void *arg)
8647  {
8648  	char	*path = (char *)arg;
8649  
8650  	ASSERT(path);
8651  
8652  	(void) ddi_pathname(dip, path);
8653  
8654  	mutex_enter(&DEVI(dip)->devi_lock);
8655  	DEVI(dip)->devi_flags &= ~DEVI_RETIRED;
8656  	DEVI_SET_DEVICE_ONLINE(dip);
8657  	mutex_exit(&DEVI(dip)->devi_lock);
8658  
8659  	RIO_VERBOSE((CE_NOTE, "Cleared RETIRED flag: dip=%p, path=%s",
8660  	    (void *)dip, path));
8661  
8662  	(void) spec_unfence_snode(dip);
8663  	RIO_DEBUG((CE_NOTE, "Unfenced device: %s", path));
8664  
8665  	if (MDI_PHCI(dip))
8666  		mdi_phci_unretire(dip);
8667  
8668  	return (DDI_WALK_CONTINUE);
8669  }
8670  
8671  struct find_dip {
8672  	char	*fd_buf;
8673  	char	*fd_path;
8674  	dev_info_t *fd_dip;
8675  };
8676  
8677  static int
find_dip_fcn(dev_info_t * dip,void * arg)8678  find_dip_fcn(dev_info_t *dip, void *arg)
8679  {
8680  	struct find_dip *findp = (struct find_dip *)arg;
8681  
8682  	(void) ddi_pathname(dip, findp->fd_buf);
8683  
8684  	if (strcmp(findp->fd_path, findp->fd_buf) != 0)
8685  		return (DDI_WALK_CONTINUE);
8686  
8687  	ndi_hold_devi(dip);
8688  	findp->fd_dip = dip;
8689  
8690  	return (DDI_WALK_TERMINATE);
8691  }
8692  
8693  int
e_ddi_unretire_device(char * path)8694  e_ddi_unretire_device(char *path)
8695  {
8696  	char		*path2;
8697  	dev_info_t	*pdip;
8698  	dev_info_t	*dip;
8699  	struct find_dip	 find_dip;
8700  
8701  	ASSERT(path);
8702  	ASSERT(*path == '/');
8703  
8704  	if (strcmp(path, "/") == 0) {
8705  		cmn_err(CE_WARN, "Root node cannot be retired. Skipping "
8706  		    "device unretire: %s", path);
8707  		return (0);
8708  	}
8709  
8710  	/*
8711  	 * We can't lookup the dip (corresponding to path) via
8712  	 * e_ddi_hold_devi_by_path() because the dip may be offline
8713  	 * and may not attach. Use ddi_walk_devs() instead;
8714  	 */
8715  	find_dip.fd_buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8716  	find_dip.fd_path = path;
8717  	find_dip.fd_dip = NULL;
8718  
8719  	pdip = ddi_root_node();
8720  
8721  	ndi_devi_enter(pdip);
8722  	ddi_walk_devs(ddi_get_child(pdip), find_dip_fcn, &find_dip);
8723  	ndi_devi_exit(pdip);
8724  
8725  	kmem_free(find_dip.fd_buf, MAXPATHLEN);
8726  
8727  	if (find_dip.fd_dip == NULL) {
8728  		cmn_err(CE_WARN, "Device not found in device tree. Skipping "
8729  		    "device unretire: %s", path);
8730  		return (0);
8731  	}
8732  
8733  	dip = find_dip.fd_dip;
8734  
8735  	pdip = ddi_get_parent(dip);
8736  
8737  	ndi_hold_devi(pdip);
8738  
8739  	ndi_devi_enter(pdip);
8740  
8741  	path2 = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8742  
8743  	(void) unmark_and_unfence(dip, path2);
8744  	if (!is_leaf_node(dip)) {
8745  		ndi_devi_enter(dip);
8746  		ddi_walk_devs(ddi_get_child(dip), unmark_and_unfence, path2);
8747  		ndi_devi_exit(dip);
8748  	}
8749  
8750  	kmem_free(path2, MAXPATHLEN);
8751  
8752  	/* release hold from find_dip_fcn() */
8753  	ndi_rele_devi(dip);
8754  
8755  	ndi_devi_exit(pdip);
8756  
8757  	ndi_rele_devi(pdip);
8758  
8759  	return (0);
8760  }
8761  
8762  /*
8763   * Called before attach on a dip that has been retired.
8764   */
8765  static int
mark_and_fence(dev_info_t * dip,void * arg)8766  mark_and_fence(dev_info_t *dip, void *arg)
8767  {
8768  	char	*fencepath = (char *)arg;
8769  
8770  	/*
8771  	 * We have already decided to retire this device. The various
8772  	 * constraint checking should not be set.
8773  	 * NOTE that the retire flag may already be set due to
8774  	 * fenced -> detach -> fenced transitions.
8775  	 */
8776  	mutex_enter(&DEVI(dip)->devi_lock);
8777  	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8778  	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
8779  	ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRING));
8780  	DEVI(dip)->devi_flags |= DEVI_RETIRED;
8781  	mutex_exit(&DEVI(dip)->devi_lock);
8782  	RIO_VERBOSE((CE_NOTE, "marked as RETIRED dip=%p", (void *)dip));
8783  
8784  	if (fencepath) {
8785  		(void) spec_fence_snode(dip, NULL);
8786  		RIO_DEBUG((CE_NOTE, "Fenced: %s",
8787  		    ddi_pathname(dip, fencepath)));
8788  	}
8789  
8790  	return (DDI_WALK_CONTINUE);
8791  }
8792  
8793  /*
8794   * Checks the retire database and:
8795   *
8796   * - if device is present in the retire database, marks the device retired
8797   *   and fences it off.
8798   * - if device is not in retire database, allows the device to attach normally
8799   *
8800   * To be called only by framework attach code on first attach attempt.
8801   *
8802   */
8803  static int
i_ddi_check_retire(dev_info_t * dip)8804  i_ddi_check_retire(dev_info_t *dip)
8805  {
8806  	char		*path;
8807  	dev_info_t	*pdip;
8808  	int		phci_only;
8809  	int		constraint;
8810  
8811  	pdip = ddi_get_parent(dip);
8812  
8813  	/*
8814  	 * Root dip is treated special and doesn't take this code path.
8815  	 * Also root can never be retired.
8816  	 */
8817  	ASSERT(pdip);
8818  	ASSERT(DEVI_BUSY_OWNED(pdip));
8819  	ASSERT(i_ddi_node_state(dip) < DS_ATTACHED);
8820  
8821  	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8822  
8823  	(void) ddi_pathname(dip, path);
8824  
8825  	RIO_VERBOSE((CE_NOTE, "Checking if dip should attach: dip=%p, path=%s",
8826  	    (void *)dip, path));
8827  
8828  	/*
8829  	 * Check if this device is in the "retired" store i.e.	should
8830  	 * be retired. If not, we have nothing to do.
8831  	 */
8832  	if (e_ddi_device_retired(path) == 0) {
8833  		RIO_VERBOSE((CE_NOTE, "device is NOT retired: path=%s", path));
8834  		if (DEVI(dip)->devi_flags & DEVI_RETIRED)
8835  			(void) e_ddi_unretire_device(path);
8836  		kmem_free(path, MAXPATHLEN);
8837  		return (0);
8838  	}
8839  
8840  	RIO_DEBUG((CE_NOTE, "attach: device is retired: path=%s", path));
8841  
8842  	/*
8843  	 * Mark dips and fence off snodes (if any)
8844  	 */
8845  	RIO_DEBUG((CE_NOTE, "attach: Mark and fence subtree: path=%s", path));
8846  	(void) mark_and_fence(dip, path);
8847  	if (!is_leaf_node(dip)) {
8848  		ndi_devi_enter(dip);
8849  		ddi_walk_devs(ddi_get_child(dip), mark_and_fence, path);
8850  		ndi_devi_exit(dip);
8851  	}
8852  
8853  	kmem_free(path, MAXPATHLEN);
8854  
8855  	/*
8856  	 * We don't want to check the client. We just want to
8857  	 * offline the PHCI
8858  	 */
8859  	phci_only = 1;
8860  	constraint = 1;
8861  	if (MDI_PHCI(dip))
8862  		mdi_phci_retire_finalize(dip, phci_only, &constraint);
8863  	return (1);
8864  }
8865  
8866  
8867  #define	VAL_ALIAS(array, x)	(strlen(array[x].pair_alias))
8868  #define	VAL_CURR(array, x)	(strlen(array[x].pair_curr))
8869  #define	SWAP(array, x, y)			\
8870  {						\
8871  	alias_pair_t tmpair = array[x];		\
8872  	array[x] = array[y];			\
8873  	array[y] = tmpair;			\
8874  }
8875  
8876  static int
partition_curr(alias_pair_t * array,int start,int end)8877  partition_curr(alias_pair_t *array, int start, int end)
8878  {
8879  	int	i = start - 1;
8880  	int	j = end + 1;
8881  	int	pivot = start;
8882  
8883  	for (;;) {
8884  		do {
8885  			j--;
8886  		} while (VAL_CURR(array, j) > VAL_CURR(array, pivot));
8887  
8888  		do {
8889  			i++;
8890  		} while (VAL_CURR(array, i) < VAL_CURR(array, pivot));
8891  
8892  		if (i < j)
8893  			SWAP(array, i, j)
8894  		else
8895  			return (j);
8896  	}
8897  }
8898  
8899  static int
partition_aliases(alias_pair_t * array,int start,int end)8900  partition_aliases(alias_pair_t *array, int start, int end)
8901  {
8902  	int	i = start - 1;
8903  	int	j = end + 1;
8904  	int	pivot = start;
8905  
8906  	for (;;) {
8907  		do {
8908  			j--;
8909  		} while (VAL_ALIAS(array, j) > VAL_ALIAS(array, pivot));
8910  
8911  		do {
8912  			i++;
8913  		} while (VAL_ALIAS(array, i) < VAL_ALIAS(array, pivot));
8914  
8915  		if (i < j)
8916  			SWAP(array, i, j)
8917  		else
8918  			return (j);
8919  	}
8920  }
8921  static void
sort_alias_pairs(alias_pair_t * array,int start,int end)8922  sort_alias_pairs(alias_pair_t *array, int start, int end)
8923  {
8924  	int mid;
8925  
8926  	if (start < end) {
8927  		mid = partition_aliases(array, start, end);
8928  		sort_alias_pairs(array, start, mid);
8929  		sort_alias_pairs(array, mid + 1, end);
8930  	}
8931  }
8932  
8933  static void
sort_curr_pairs(alias_pair_t * array,int start,int end)8934  sort_curr_pairs(alias_pair_t *array, int start, int end)
8935  {
8936  	int mid;
8937  
8938  	if (start < end) {
8939  		mid = partition_curr(array, start, end);
8940  		sort_curr_pairs(array, start, mid);
8941  		sort_curr_pairs(array, mid + 1, end);
8942  	}
8943  }
8944  
8945  static void
create_sorted_pairs(plat_alias_t * pali,int npali)8946  create_sorted_pairs(plat_alias_t *pali, int npali)
8947  {
8948  	int		i;
8949  	int		j;
8950  	int		k;
8951  	int		count;
8952  
8953  	count = 0;
8954  	for (i = 0; i < npali; i++) {
8955  		count += pali[i].pali_naliases;
8956  	}
8957  
8958  	ddi_aliases.dali_alias_pairs = kmem_zalloc(
8959  	    (sizeof (alias_pair_t)) * count, KM_NOSLEEP);
8960  	if (ddi_aliases.dali_alias_pairs == NULL) {
8961  		cmn_err(CE_PANIC, "alias path-pair alloc failed");
8962  		/*NOTREACHED*/
8963  	}
8964  
8965  	ddi_aliases.dali_curr_pairs = kmem_zalloc(
8966  	    (sizeof (alias_pair_t)) * count, KM_NOSLEEP);
8967  	if (ddi_aliases.dali_curr_pairs == NULL) {
8968  		cmn_err(CE_PANIC, "curr path-pair alloc failed");
8969  		/*NOTREACHED*/
8970  	}
8971  
8972  	for (i = 0, k = 0; i < npali; i++) {
8973  		for (j = 0; j < pali[i].pali_naliases; j++, k++) {
8974  			ddi_aliases.dali_alias_pairs[k].pair_curr =
8975  			    ddi_aliases.dali_curr_pairs[k].pair_curr =
8976  			    pali[i].pali_current;
8977  			ddi_aliases.dali_alias_pairs[k].pair_alias =
8978  			    ddi_aliases.dali_curr_pairs[k].pair_alias =
8979  			    pali[i].pali_aliases[j];
8980  		}
8981  	}
8982  
8983  	ASSERT(k == count);
8984  
8985  	ddi_aliases.dali_num_pairs = count;
8986  
8987  	/* Now sort the array based on length of pair_alias */
8988  	sort_alias_pairs(ddi_aliases.dali_alias_pairs, 0, count - 1);
8989  	sort_curr_pairs(ddi_aliases.dali_curr_pairs, 0, count - 1);
8990  }
8991  
8992  void
ddi_register_aliases(plat_alias_t * pali,uint64_t npali)8993  ddi_register_aliases(plat_alias_t *pali, uint64_t npali)
8994  {
8995  
8996  	ASSERT((pali == NULL) ^ (npali != 0));
8997  
8998  	if (npali == 0) {
8999  		ddi_err(DER_PANIC, NULL, "npali == 0");
9000  		/*NOTREACHED*/
9001  	}
9002  
9003  	if (ddi_aliases_present == B_TRUE) {
9004  		ddi_err(DER_PANIC, NULL, "multiple init");
9005  		/*NOTREACHED*/
9006  	}
9007  
9008  	ddi_aliases.dali_alias_TLB = mod_hash_create_strhash(
9009  	    "ddi-alias-tlb", DDI_ALIAS_HASH_SIZE, mod_hash_null_valdtor);
9010  	if (ddi_aliases.dali_alias_TLB == NULL) {
9011  		ddi_err(DER_PANIC, NULL, "alias TLB hash alloc failed");
9012  		/*NOTREACHED*/
9013  	}
9014  
9015  	ddi_aliases.dali_curr_TLB = mod_hash_create_strhash(
9016  	    "ddi-curr-tlb", DDI_ALIAS_HASH_SIZE, mod_hash_null_valdtor);
9017  	if (ddi_aliases.dali_curr_TLB == NULL) {
9018  		ddi_err(DER_PANIC, NULL, "curr TLB hash alloc failed");
9019  		/*NOTREACHED*/
9020  	}
9021  
9022  	create_sorted_pairs(pali, npali);
9023  
9024  	tsd_create(&tsd_ddi_redirect, NULL);
9025  
9026  	ddi_aliases_present = B_TRUE;
9027  }
9028  
9029  static dev_info_t *
path_to_dip(char * path)9030  path_to_dip(char *path)
9031  {
9032  	dev_info_t	*currdip;
9033  	int		error;
9034  	char		*pdup;
9035  
9036  	pdup = ddi_strdup(path, KM_NOSLEEP);
9037  	if (pdup == NULL) {
9038  		cmn_err(CE_PANIC, "path strdup failed: %s", path);
9039  		/*NOTREACHED*/
9040  	}
9041  
9042  	error = resolve_pathname(pdup, &currdip, NULL, NULL);
9043  
9044  	kmem_free(pdup, strlen(path) + 1);
9045  
9046  	return (error ? NULL : currdip);
9047  }
9048  
9049  dev_info_t *
ddi_alias_to_currdip(char * alias,int i)9050  ddi_alias_to_currdip(char *alias, int i)
9051  {
9052  	alias_pair_t *pair;
9053  	char *curr;
9054  	dev_info_t *currdip = NULL;
9055  	char *aliasdup;
9056  	int rv, len;
9057  
9058  	pair = &(ddi_aliases.dali_alias_pairs[i]);
9059  	len = strlen(pair->pair_alias);
9060  
9061  	curr = NULL;
9062  	aliasdup = ddi_strdup(alias, KM_NOSLEEP);
9063  	if (aliasdup == NULL) {
9064  		cmn_err(CE_PANIC, "aliasdup alloc failed");
9065  		/*NOTREACHED*/
9066  	}
9067  
9068  	if (strncmp(alias, pair->pair_alias, len)  != 0)
9069  		goto out;
9070  
9071  	if (alias[len] != '/' && alias[len] != '\0')
9072  		goto out;
9073  
9074  	curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
9075  	if (curr == NULL) {
9076  		cmn_err(CE_PANIC, "curr alloc failed");
9077  		/*NOTREACHED*/
9078  	}
9079  	(void) strlcpy(curr, pair->pair_curr, MAXPATHLEN);
9080  	if (alias[len] == '/') {
9081  		(void) strlcat(curr, "/", MAXPATHLEN);
9082  		(void) strlcat(curr, &alias[len + 1], MAXPATHLEN);
9083  	}
9084  
9085  	currdip = path_to_dip(curr);
9086  
9087  out:
9088  	if (currdip) {
9089  		rv = mod_hash_insert(ddi_aliases.dali_alias_TLB,
9090  		    (mod_hash_key_t)aliasdup, (mod_hash_val_t)curr);
9091  		if (rv != 0) {
9092  			kmem_free(curr, MAXPATHLEN);
9093  			strfree(aliasdup);
9094  		}
9095  	} else {
9096  		rv = mod_hash_insert(ddi_aliases.dali_alias_TLB,
9097  		    (mod_hash_key_t)aliasdup, (mod_hash_val_t)NULL);
9098  		if (rv != 0) {
9099  			strfree(aliasdup);
9100  		}
9101  		if (curr)
9102  			kmem_free(curr, MAXPATHLEN);
9103  	}
9104  
9105  	return (currdip);
9106  }
9107  
9108  char *
ddi_curr_to_alias(char * curr,int i)9109  ddi_curr_to_alias(char *curr, int i)
9110  {
9111  	alias_pair_t	*pair;
9112  	char		*alias;
9113  	char		*currdup;
9114  	int		len;
9115  	int		rv;
9116  
9117  	pair = &(ddi_aliases.dali_curr_pairs[i]);
9118  
9119  	len = strlen(pair->pair_curr);
9120  
9121  	alias = NULL;
9122  
9123  	currdup = ddi_strdup(curr, KM_NOSLEEP);
9124  	if (currdup == NULL) {
9125  		cmn_err(CE_PANIC, "currdup alloc failed");
9126  		/*NOTREACHED*/
9127  	}
9128  
9129  	if (strncmp(curr, pair->pair_curr, len) != 0)
9130  		goto out;
9131  
9132  	if (curr[len] != '/' && curr[len] != '\0')
9133  		goto out;
9134  
9135  	alias = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
9136  	if (alias == NULL) {
9137  		cmn_err(CE_PANIC, "alias alloc failed");
9138  		/*NOTREACHED*/
9139  	}
9140  
9141  	(void) strlcpy(alias, pair->pair_alias, MAXPATHLEN);
9142  	if (curr[len] == '/') {
9143  		(void) strlcat(alias, "/", MAXPATHLEN);
9144  		(void) strlcat(alias, &curr[len + 1], MAXPATHLEN);
9145  	}
9146  
9147  	if (e_ddi_path_to_instance(alias) == NULL) {
9148  		kmem_free(alias, MAXPATHLEN);
9149  		alias = NULL;
9150  	}
9151  
9152  out:
9153  	rv = mod_hash_insert(ddi_aliases.dali_curr_TLB,
9154  	    (mod_hash_key_t)currdup, (mod_hash_val_t)alias);
9155  	if (rv != 0) {
9156  		strfree(currdup);
9157  	}
9158  
9159  	return (alias);
9160  }
9161  
9162  dev_info_t *
ddi_alias_redirect(char * alias)9163  ddi_alias_redirect(char *alias)
9164  {
9165  	char		*curr;
9166  	dev_info_t	*currdip;
9167  	int		i;
9168  
9169  	if (ddi_aliases_present == B_FALSE)
9170  		return (NULL);
9171  
9172  	if (tsd_get(tsd_ddi_redirect))
9173  		return (NULL);
9174  
9175  	(void) tsd_set(tsd_ddi_redirect, (void *)1);
9176  
9177  	ASSERT(ddi_aliases.dali_alias_TLB);
9178  	ASSERT(ddi_aliases.dali_alias_pairs);
9179  
9180  	curr = NULL;
9181  	if (mod_hash_find(ddi_aliases.dali_alias_TLB,
9182  	    (mod_hash_key_t)alias, (mod_hash_val_t *)&curr) == 0) {
9183  		currdip = curr ? path_to_dip(curr) : NULL;
9184  		goto out;
9185  	}
9186  
9187  	/* The TLB has no translation, do it the hard way */
9188  	currdip = NULL;
9189  	for (i = ddi_aliases.dali_num_pairs - 1; i >= 0; i--) {
9190  		currdip = ddi_alias_to_currdip(alias, i);
9191  		if (currdip)
9192  			break;
9193  	}
9194  out:
9195  	(void) tsd_set(tsd_ddi_redirect, NULL);
9196  
9197  	return (currdip);
9198  }
9199  
9200  char *
ddi_curr_redirect(char * curr)9201  ddi_curr_redirect(char *curr)
9202  {
9203  	char *alias;
9204  	int i;
9205  
9206  	if (ddi_aliases_present == B_FALSE)
9207  		return (NULL);
9208  
9209  	if (tsd_get(tsd_ddi_redirect))
9210  		return (NULL);
9211  
9212  	(void) tsd_set(tsd_ddi_redirect, (void *)1);
9213  
9214  	ASSERT(ddi_aliases.dali_curr_TLB);
9215  	ASSERT(ddi_aliases.dali_curr_pairs);
9216  
9217  	alias = NULL;
9218  	if (mod_hash_find(ddi_aliases.dali_curr_TLB,
9219  	    (mod_hash_key_t)curr, (mod_hash_val_t *)&alias) == 0) {
9220  		goto out;
9221  	}
9222  
9223  
9224  	/* The TLB has no translation, do it the slow way */
9225  	alias = NULL;
9226  	for (i = ddi_aliases.dali_num_pairs - 1; i >= 0; i--) {
9227  		alias = ddi_curr_to_alias(curr, i);
9228  		if (alias)
9229  			break;
9230  	}
9231  
9232  out:
9233  	(void) tsd_set(tsd_ddi_redirect, NULL);
9234  
9235  	return (alias);
9236  }
9237  
9238  void
ddi_err(ddi_err_t ade,dev_info_t * rdip,const char * fmt,...)9239  ddi_err(ddi_err_t ade, dev_info_t *rdip, const char *fmt, ...)
9240  {
9241  	va_list ap;
9242  	char strbuf[256];
9243  	char *buf;
9244  	size_t buflen, tlen;
9245  	int ce;
9246  	int de;
9247  	const char *fmtbad = "Invalid arguments to ddi_err()";
9248  
9249  	de = DER_CONT;
9250  	strbuf[1] = '\0';
9251  
9252  	switch (ade) {
9253  	case DER_CONS:
9254  		strbuf[0] = '^';
9255  		break;
9256  	case DER_LOG:
9257  		strbuf[0] = '!';
9258  		break;
9259  	case DER_VERB:
9260  		strbuf[0] = '?';
9261  		break;
9262  	default:
9263  		strbuf[0] = '\0';
9264  		de = ade;
9265  		break;
9266  	}
9267  
9268  	tlen = strlen(strbuf);
9269  	buf = strbuf + tlen;
9270  	buflen = sizeof (strbuf) - tlen;
9271  
9272  	if (rdip && ddi_get_instance(rdip) == -1) {
9273  		(void) snprintf(buf, buflen, "%s: ",
9274  		    ddi_driver_name(rdip));
9275  	} else if (rdip) {
9276  		(void) snprintf(buf, buflen, "%s%d: ",
9277  		    ddi_driver_name(rdip), ddi_get_instance(rdip));
9278  	}
9279  
9280  	tlen = strlen(strbuf);
9281  	buf = strbuf + tlen;
9282  	buflen = sizeof (strbuf) - tlen;
9283  
9284  	va_start(ap, fmt);
9285  	switch (de) {
9286  	case DER_CONT:
9287  		(void) vsnprintf(buf, buflen, fmt, ap);
9288  		if (ade != DER_CONT) {
9289  			(void) strlcat(strbuf, "\n", sizeof (strbuf));
9290  		}
9291  		ce = CE_CONT;
9292  		break;
9293  	case DER_NOTE:
9294  		(void) vsnprintf(buf, buflen, fmt, ap);
9295  		ce = CE_NOTE;
9296  		break;
9297  	case DER_WARN:
9298  		(void) vsnprintf(buf, buflen, fmt, ap);
9299  		ce = CE_WARN;
9300  		break;
9301  	case DER_MODE:
9302  		(void) vsnprintf(buf, buflen, fmt, ap);
9303  		if (ddi_err_panic == B_TRUE) {
9304  			ce = CE_PANIC;
9305  		} else {
9306  			ce = CE_WARN;
9307  		}
9308  		break;
9309  	case DER_DEBUG:
9310  		(void) snprintf(buf, buflen, "DEBUG: ");
9311  		tlen = strlen("DEBUG: ");
9312  		(void) vsnprintf(buf + tlen, buflen - tlen, fmt, ap);
9313  		ce = CE_CONT;
9314  		break;
9315  	case DER_PANIC:
9316  		(void) vsnprintf(buf, buflen, fmt, ap);
9317  		ce = CE_PANIC;
9318  		break;
9319  	case DER_INVALID:
9320  	default:
9321  		(void) snprintf(buf, buflen, fmtbad);
9322  		tlen = strlen(fmtbad);
9323  		(void) vsnprintf(buf + tlen, buflen - tlen, fmt, ap);
9324  		ce = CE_PANIC;
9325  		break;
9326  	}
9327  	va_end(ap);
9328  
9329  	cmn_err(ce, strbuf);
9330  }
9331  
9332  /*ARGSUSED*/
9333  void
ddi_mem_update(uint64_t addr,uint64_t size)9334  ddi_mem_update(uint64_t addr, uint64_t size)
9335  {
9336  #if defined(__x86) && !defined(__xpv)
9337  	extern void immu_physmem_update(uint64_t addr, uint64_t size);
9338  	immu_physmem_update(addr, size);
9339  #else
9340  	/*LINTED*/
9341  	;
9342  #endif
9343  }
9344  
9345  void
e_ddi_register_unbind_callback(dev_info_t * dip,ddi_unbind_callback_t * cb)9346  e_ddi_register_unbind_callback(dev_info_t *dip, ddi_unbind_callback_t *cb)
9347  {
9348  	struct dev_info *devi = DEVI(dip);
9349  
9350  	mutex_enter(&devi->devi_unbind_lock);
9351  	list_insert_tail(&devi->devi_unbind_cbs, cb);
9352  	mutex_exit(&devi->devi_unbind_lock);
9353  }
9354