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