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