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