4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <sys/t_lock.h>
27 #include <sys/cmn_err.h>
28 #include <sys/instance.h>
32 #include <sys/hwconf.h>
33 #include <sys/sunddi.h>
34 #include <sys/sunndi.h>
35 #include <sys/sunmdi.h>
36 #include <sys/ddi_impldefs.h>
37 #include <sys/ndi_impldefs.h>
39 #include <sys/devcache.h>
40 #include <sys/devid_cache.h>
41 #include <sys/sysmacros.h>
44 * Discovery refers to the heroic effort made to discover a device which
45 * cannot be accessed at the physical path where it once resided. Discovery
46 * involves walking the entire device tree attaching all possible disk
47 * instances, to search for the device referenced by a devid. Obviously,
48 * full device discovery is something to be avoided where possible.
49 * Note that simply invoking devfsadm(1M) is equivalent to running full
50 * discovery at the devid cache level.
52 * Reasons why a disk may not be accessible:
54 * disk removed or cable disconnected
55 * disk or adapter broken
57 * Note that discovery is not needed and cannot succeed in any of these
60 * When discovery may succeed:
61 * Discovery will result in success when a device has been moved
62 * to a different address. Note that it's recommended that
63 * devfsadm(1M) be invoked (no arguments required) whenever a system's
64 * h/w configuration has been updated. Alternatively, a
65 * reconfiguration boot can be used to accomplish the same result.
67 * Note that discovery is not necessary to be able to correct an access
68 * failure for a device which was powered off. Assuming the cache has an
69 * entry for such a device, simply powering it on should permit the system
70 * to access it. If problems persist after powering it on, invoke
73 * Discovery prior to mounting root is only of interest when booting
74 * from a filesystem which accesses devices by device id, which of
79 * devid_discovery_boot (default 1)
80 * Number of times discovery will be attempted prior to mounting root.
81 * Must be done at least once to recover from corrupted or missing
82 * devid cache backing store. Probably there's no reason to ever
83 * set this to greater than one as a missing device will remain
84 * unavailable no matter how often the system searches for it.
86 * devid_discovery_postboot (default 1)
87 * Number of times discovery will be attempted after mounting root.
88 * This must be performed at least once to discover any devices
89 * needed after root is mounted which may have been powered
90 * off and moved before booting.
91 * Setting this to a larger positive number will introduce
92 * some inconsistency in system operation. Searching for a device
93 * will take an indeterminate amount of time, sometimes slower,
94 * sometimes faster. In addition, the system will sometimes
95 * discover a newly powered on device, sometimes it won't.
96 * Use of this option is not therefore recommended.
98 * devid_discovery_postboot_always (default 0)
99 * Set to 1, the system will always attempt full discovery.
101 * devid_discovery_secs (default 0)
102 * Set to a positive value, the system will attempt full discovery
103 * but with a minimum delay between attempts. A device search
104 * within the period of time specified will result in failure.
106 * devid_cache_read_disable (default 0)
107 * Set to 1 to disable reading /etc/devices/devid_cache.
108 * Devid cache will continue to operate normally but
109 * at least one discovery attempt will be required.
111 * devid_cache_write_disable (default 0)
112 * Set to 1 to disable updates to /etc/devices/devid_cache.
113 * Any updates to the devid cache will not be preserved across a reboot.
115 * devid_report_error (default 0)
116 * Set to 1 to enable some error messages related to devid
119 * The devid is packed in the cache file as a byte array. For
120 * portability, this could be done in the encoded string format.
124 int devid_discovery_boot
= 1;
125 int devid_discovery_postboot
= 1;
126 int devid_discovery_postboot_always
= 0;
127 int devid_discovery_secs
= 0;
129 int devid_cache_read_disable
= 0;
130 int devid_cache_write_disable
= 0;
132 int devid_report_error
= 0;
136 * State to manage discovery of devices providing a devid
138 static int devid_discovery_busy
= 0;
139 static kmutex_t devid_discovery_mutex
;
140 static kcondvar_t devid_discovery_cv
;
141 static clock_t devid_last_discovery
= 0;
145 int nvp_devid_debug
= 0;
147 int devid_log_registers
= 0;
148 int devid_log_finds
= 0;
149 int devid_log_lookups
= 0;
150 int devid_log_discovery
= 0;
151 int devid_log_matches
= 0;
152 int devid_log_paths
= 0;
153 int devid_log_failures
= 0;
154 int devid_log_hold
= 0;
155 int devid_log_unregisters
= 0;
156 int devid_log_removes
= 0;
157 int devid_register_debug
= 0;
158 int devid_log_stale
= 0;
159 int devid_log_detaches
= 0;
163 * devid cache file registration for cache reads and updates
165 static nvf_ops_t devid_cache_ops
= {
166 "/etc/devices/devid_cache", /* path to cache */
167 devid_cache_unpack_nvlist
, /* read: nvlist to nvp */
168 devid_cache_pack_list
, /* write: nvp to nvlist */
169 devid_list_free
, /* free data list */
170 NULL
/* write complete callback */
174 * handle to registered devid cache handlers
176 nvf_handle_t dcfd_handle
;
180 * Initialize devid cache file management
183 devid_cache_init(void)
185 dcfd_handle
= nvf_register_file(&devid_cache_ops
);
188 list_create(nvf_list(dcfd_handle
), sizeof (nvp_devid_t
),
189 offsetof(nvp_devid_t
, nvp_link
));
191 mutex_init(&devid_discovery_mutex
, NULL
, MUTEX_DEFAULT
, NULL
);
192 cv_init(&devid_discovery_cv
, NULL
, CV_DRIVER
, NULL
);
196 * Read and initialize the devid cache from the persistent store
199 devid_cache_read(void)
201 if (!devid_cache_read_disable
) {
202 rw_enter(nvf_lock(dcfd_handle
), RW_WRITER
);
203 ASSERT(list_head(nvf_list(dcfd_handle
)) == NULL
);
204 (void) nvf_read_file(dcfd_handle
);
205 rw_exit(nvf_lock(dcfd_handle
));
210 devid_nvp_free(nvp_devid_t
*dp
)
213 kmem_free(dp
->nvp_devpath
, strlen(dp
->nvp_devpath
)+1);
215 kmem_free(dp
->nvp_devid
, ddi_devid_sizeof(dp
->nvp_devid
));
217 kmem_free(dp
, sizeof (nvp_devid_t
));
221 devid_list_free(nvf_handle_t fd
)
226 ASSERT(RW_WRITE_HELD(nvf_lock(dcfd_handle
)));
228 listp
= nvf_list(fd
);
229 while (np
= list_head(listp
)) {
230 list_remove(listp
, np
);
236 * Free an nvp element in a list
239 devid_nvp_unlink_and_free(nvf_handle_t fd
, nvp_devid_t
*np
)
241 list_remove(nvf_list(fd
), np
);
246 * Unpack a device path/nvlist pair to the list of devid cache elements.
247 * Used to parse the nvlist format when reading
248 * /etc/devices/devid_cache
251 devid_cache_unpack_nvlist(nvf_handle_t fd
, nvlist_t
*nvl
, char *name
)
258 NVP_DEVID_DEBUG_PATH((name
));
259 ASSERT(RW_WRITE_HELD(nvf_lock(dcfd_handle
)));
262 * check path for a devid
264 rval
= nvlist_lookup_byte_array(nvl
,
265 DP_DEVID_ID
, (uchar_t
**)&devidp
, &n
);
267 if (ddi_devid_valid(devidp
) == DDI_SUCCESS
) {
268 ASSERT(n
== ddi_devid_sizeof(devidp
));
269 np
= kmem_zalloc(sizeof (nvp_devid_t
), KM_SLEEP
);
270 np
->nvp_devpath
= i_ddi_strdup(name
, KM_SLEEP
);
271 np
->nvp_devid
= kmem_alloc(n
, KM_SLEEP
);
272 (void) bcopy(devidp
, np
->nvp_devid
, n
);
273 list_insert_tail(nvf_list(fd
), np
);
274 NVP_DEVID_DEBUG_DEVID((np
->nvp_devid
));
277 "%s: invalid devid\n", name
));
281 "%s: devid not available\n", name
));
288 * Pack the list of devid cache elements into a single nvlist
289 * Used when writing the nvlist file.
292 devid_cache_pack_list(nvf_handle_t fd
, nvlist_t
**ret_nvl
)
294 nvlist_t
*nvl
, *sub_nvl
;
299 ASSERT(RW_WRITE_HELD(nvf_lock(dcfd_handle
)));
301 rval
= nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
);
303 nvf_error("%s: nvlist alloc error %d\n",
304 nvf_cache_name(fd
), rval
);
305 return (DDI_FAILURE
);
308 listp
= nvf_list(fd
);
309 for (np
= list_head(listp
); np
; np
= list_next(listp
, np
)) {
310 if (np
->nvp_devid
== NULL
)
312 NVP_DEVID_DEBUG_PATH(np
->nvp_devpath
);
313 rval
= nvlist_alloc(&sub_nvl
, NV_UNIQUE_NAME
, KM_SLEEP
);
315 nvf_error("%s: nvlist alloc error %d\n",
316 nvf_cache_name(fd
), rval
);
321 rval
= nvlist_add_byte_array(sub_nvl
, DP_DEVID_ID
,
322 (uchar_t
*)np
->nvp_devid
,
323 ddi_devid_sizeof(np
->nvp_devid
));
325 NVP_DEVID_DEBUG_DEVID(np
->nvp_devid
);
328 "%s: nvlist add error %d (devid)\n",
329 nvf_cache_name(fd
), rval
);
333 rval
= nvlist_add_nvlist(nvl
, np
->nvp_devpath
, sub_nvl
);
335 nvf_error("%s: nvlist add error %d (sublist)\n",
336 nvf_cache_name(fd
), rval
);
339 nvlist_free(sub_nvl
);
343 return (DDI_SUCCESS
);
347 nvlist_free(sub_nvl
);
350 return (DDI_FAILURE
);
354 e_devid_do_discovery(void)
356 ASSERT(mutex_owned(&devid_discovery_mutex
));
358 if (i_ddi_io_initialized() == 0) {
359 if (devid_discovery_boot
> 0) {
360 devid_discovery_boot
--;
364 if (devid_discovery_postboot_always
> 0)
366 if (devid_discovery_postboot
> 0) {
367 devid_discovery_postboot
--;
370 if (devid_discovery_secs
> 0) {
371 if ((ddi_get_lbolt() - devid_last_discovery
) >
372 drv_usectohz(devid_discovery_secs
* MICROSEC
)) {
378 DEVID_LOG_DISC((CE_CONT
, "devid_discovery: no discovery\n"));
383 e_ddi_devid_hold_by_major(major_t major
)
385 DEVID_LOG_DISC((CE_CONT
,
386 "devid_discovery: ddi_hold_installed_driver %d\n", major
));
388 if (ddi_hold_installed_driver(major
) == NULL
)
391 ddi_rele_driver(major
);
394 /* legacy support - see below */
395 static char *e_ddi_devid_hold_driver_list
[] = { "sd", "ssd" };
397 #define N_DRIVERS_TO_HOLD \
398 (sizeof (e_ddi_devid_hold_driver_list) / sizeof (char *))
401 e_ddi_devid_hold_installed_driver(ddi_devid_t devid
)
403 impl_devid_t
*id
= (impl_devid_t
*)devid
;
404 major_t major
, hint_major
;
405 char hint
[DEVID_HINT_SIZE
+ 1];
406 struct devnames
*dnp
;
410 /* Count non-null bytes */
411 for (i
= 0; i
< DEVID_HINT_SIZE
; i
++)
412 if (id
->did_driver
[i
] == '\0')
415 /* Make a copy of the driver hint */
416 bcopy(id
->did_driver
, hint
, i
);
419 /* search for the devid using the hint driver */
420 hint_major
= ddi_name_to_major(hint
);
421 if (hint_major
!= DDI_MAJOR_T_NONE
) {
422 e_ddi_devid_hold_by_major(hint_major
);
426 * search for the devid with each driver declaring
427 * itself as a devid registrant.
429 for (major
= 0; major
< devcnt
; major
++) {
430 if (major
== hint_major
)
432 dnp
= &devnamesp
[major
];
433 if (dnp
->dn_flags
& DN_DEVID_REGISTRANT
) {
434 e_ddi_devid_hold_by_major(major
);
439 * Legacy support: may be removed once an upgrade mechanism
440 * for driver conf files is available.
442 drvp
= e_ddi_devid_hold_driver_list
;
443 for (i
= 0; i
< N_DRIVERS_TO_HOLD
; i
++, drvp
++) {
444 major
= ddi_name_to_major(*drvp
);
445 if (major
!= DDI_MAJOR_T_NONE
&& major
!= hint_major
) {
446 e_ddi_devid_hold_by_major(major
);
452 * Return success if discovery was attempted, to indicate
453 * that the desired device may now be available.
456 e_ddi_devid_discovery(ddi_devid_t devid
)
459 int rval
= DDI_SUCCESS
;
461 mutex_enter(&devid_discovery_mutex
);
463 if (devid_discovery_busy
) {
464 DEVID_LOG_DISC((CE_CONT
, "devid_discovery: busy\n"));
465 while (devid_discovery_busy
) {
466 cv_wait(&devid_discovery_cv
, &devid_discovery_mutex
);
468 } else if (e_devid_do_discovery()) {
469 devid_discovery_busy
= 1;
470 mutex_exit(&devid_discovery_mutex
);
472 if (i_ddi_io_initialized() == 0) {
473 e_ddi_devid_hold_installed_driver(devid
);
475 DEVID_LOG_DISC((CE_CONT
,
476 "devid_discovery: ndi_devi_config\n"));
477 flags
= NDI_DEVI_PERSIST
| NDI_CONFIG
| NDI_NO_EVENT
;
478 if (i_ddi_io_initialized())
479 flags
|= NDI_DRV_CONF_REPROBE
;
480 (void) ndi_devi_config(ddi_root_node(), flags
);
483 mutex_enter(&devid_discovery_mutex
);
484 devid_discovery_busy
= 0;
485 cv_broadcast(&devid_discovery_cv
);
486 if (devid_discovery_secs
> 0)
487 devid_last_discovery
= ddi_get_lbolt();
488 DEVID_LOG_DISC((CE_CONT
, "devid_discovery: done\n"));
491 DEVID_LOG_DISC((CE_CONT
, "no devid discovery\n"));
494 mutex_exit(&devid_discovery_mutex
);
500 * As part of registering a devid for a device,
501 * update the devid cache with this device/devid pair
502 * or note that this combination has registered.
504 * If a devpath is provided it will be used as the path to register the
505 * devid against, otherwise we use ddi_pathname(dip). In both cases
506 * we duplicate the path string so that it can be cached/freed indepdently
507 * of the original owner.
510 e_devid_cache_register_cmn(dev_info_t
*dip
, ddi_devid_t devid
, char *devpath
)
513 nvp_devid_t
*new_nvp
;
514 ddi_devid_t new_devid
;
516 char *path
, *fullpath
;
517 ddi_devid_t free_devid
= NULL
;
523 ASSERT(ddi_devid_valid(devid
) == DDI_SUCCESS
);
526 pathlen
= strlen(devpath
) + 1;
527 path
= kmem_alloc(pathlen
, KM_SLEEP
);
528 bcopy(devpath
, path
, pathlen
);
531 * We are willing to accept DS_BOUND nodes if we can form a full
532 * ddi_pathname (i.e. the node is part way to becomming
533 * DS_INITIALIZED and devi_addr/ddi_get_name_addr are non-NULL).
535 if (ddi_get_name_addr(dip
) == NULL
)
536 return (DDI_FAILURE
);
538 fullpath
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
539 (void) ddi_pathname(dip
, fullpath
);
540 pathlen
= strlen(fullpath
) + 1;
541 path
= kmem_alloc(pathlen
, KM_SLEEP
);
542 bcopy(fullpath
, path
, pathlen
);
543 kmem_free(fullpath
, MAXPATHLEN
);
546 DEVID_LOG_REG(("register", devid
, path
));
548 new_nvp
= kmem_zalloc(sizeof (nvp_devid_t
), KM_SLEEP
);
549 new_devid_size
= ddi_devid_sizeof(devid
);
550 new_devid
= kmem_alloc(new_devid_size
, KM_SLEEP
);
551 (void) bcopy(devid
, new_devid
, new_devid_size
);
553 rw_enter(nvf_lock(dcfd_handle
), RW_WRITER
);
555 listp
= nvf_list(dcfd_handle
);
556 for (np
= list_head(listp
); np
; np
= list_next(listp
, np
)) {
557 if (strcmp(path
, np
->nvp_devpath
) == 0) {
558 DEVID_DEBUG2((CE_CONT
,
559 "register: %s path match\n", path
));
560 if (np
->nvp_devid
== NULL
) {
561 replace
: np
->nvp_devid
= new_devid
;
563 NVP_DEVID_DIP
| NVP_DEVID_REGISTERED
;
565 if (!devid_cache_write_disable
) {
566 nvf_mark_dirty(dcfd_handle
);
569 rw_exit(nvf_lock(dcfd_handle
));
570 kmem_free(new_nvp
, sizeof (nvp_devid_t
));
571 kmem_free(path
, pathlen
);
574 if (ddi_devid_valid(np
->nvp_devid
) != DDI_SUCCESS
) {
575 /* replace invalid devid */
576 free_devid
= np
->nvp_devid
;
580 * We're registering an already-cached path
581 * Does the device's devid match the cache?
583 if (ddi_devid_compare(devid
, np
->nvp_devid
) != 0) {
584 DEVID_DEBUG((CE_CONT
, "devid register: "
585 "devid %s does not match\n", path
));
587 * Replace cached devid for this path
588 * with newly registered devid. A devid
589 * may map to multiple paths but one path
590 * should only map to one devid.
592 devid_nvp_unlink_and_free(dcfd_handle
, np
);
596 DEVID_DEBUG2((CE_CONT
,
597 "devid register: %s devid match\n", path
));
599 NVP_DEVID_DIP
| NVP_DEVID_REGISTERED
;
601 rw_exit(nvf_lock(dcfd_handle
));
602 kmem_free(new_nvp
, sizeof (nvp_devid_t
));
603 kmem_free(path
, pathlen
);
604 kmem_free(new_devid
, new_devid_size
);
605 return (DDI_SUCCESS
);
611 * Add newly registered devid to the cache
615 new_nvp
->nvp_devpath
= path
;
616 new_nvp
->nvp_flags
= NVP_DEVID_DIP
| NVP_DEVID_REGISTERED
;
617 new_nvp
->nvp_dip
= dip
;
618 new_nvp
->nvp_devid
= new_devid
;
620 if (!devid_cache_write_disable
) {
622 nvf_mark_dirty(dcfd_handle
);
624 list_insert_tail(nvf_list(dcfd_handle
), new_nvp
);
626 rw_exit(nvf_lock(dcfd_handle
));
630 kmem_free(free_devid
, ddi_devid_sizeof(free_devid
));
635 return (DDI_SUCCESS
);
639 e_devid_cache_register(dev_info_t
*dip
, ddi_devid_t devid
)
641 return (e_devid_cache_register_cmn(dip
, devid
, NULL
));
645 * Unregister a device's devid; the devinfo may hit on multiple entries
646 * arising from both pHCI and vHCI paths.
647 * Called as an instance detachs.
648 * Invalidate the devid's devinfo reference.
649 * Devid-path remains in the cache.
653 e_devid_cache_unregister(dev_info_t
*dip
)
658 rw_enter(nvf_lock(dcfd_handle
), RW_WRITER
);
660 listp
= nvf_list(dcfd_handle
);
661 for (np
= list_head(listp
); np
; np
= list_next(listp
, np
)) {
662 if (np
->nvp_devid
== NULL
)
664 if ((np
->nvp_flags
& NVP_DEVID_DIP
) && np
->nvp_dip
== dip
) {
665 DEVID_LOG_UNREG((CE_CONT
,
666 "unregister: %s\n", np
->nvp_devpath
));
667 np
->nvp_flags
&= ~NVP_DEVID_DIP
;
672 rw_exit(nvf_lock(dcfd_handle
));
676 e_devid_cache_pathinfo(mdi_pathinfo_t
*pip
, ddi_devid_t devid
)
678 char *path
= mdi_pi_pathname(pip
);
680 return (e_devid_cache_register_cmn(mdi_pi_get_client(pip
), devid
,
685 * Purge devid cache of stale devids
688 devid_cache_cleanup(void)
690 nvp_devid_t
*np
, *next
;
694 rw_enter(nvf_lock(dcfd_handle
), RW_WRITER
);
696 listp
= nvf_list(dcfd_handle
);
697 for (np
= list_head(listp
); np
; np
= next
) {
698 next
= list_next(listp
, np
);
699 if (np
->nvp_devid
== NULL
)
701 if ((np
->nvp_flags
& NVP_DEVID_REGISTERED
) == 0) {
702 DEVID_LOG_REMOVE((CE_CONT
,
703 "cleanup: %s\n", np
->nvp_devpath
));
704 if (!devid_cache_write_disable
) {
705 nvf_mark_dirty(dcfd_handle
);
708 devid_nvp_unlink_and_free(dcfd_handle
, np
);
712 rw_exit(nvf_lock(dcfd_handle
));
720 * Build a list of dev_t's for a device/devid
722 * The effect of this function is cumulative, adding dev_t's
723 * for the device to the list of all dev_t's for a given
727 e_devid_minor_to_devlist(
735 struct ddi_minor_data
*dmdp
;
737 int ndevts
= *devtcntp
;
739 ASSERT(i_ddi_devi_attached(dip
));
741 /* are we looking for a set of minor nodes? */
742 if ((minor_name
== DEVID_MINOR_NAME_ALL
) ||
743 (minor_name
== DEVID_MINOR_NAME_ALL_CHR
) ||
744 (minor_name
== DEVID_MINOR_NAME_ALL_BLK
))
747 /* Find matching minor names */
748 ndi_devi_enter(dip
, &circ
);
749 for (dmdp
= DEVI(dip
)->devi_minor
; dmdp
; dmdp
= dmdp
->next
) {
751 /* Skip non-minors, and non matching minor names */
752 if ((dmdp
->type
!= DDM_MINOR
) || ((minor_all
== 0) &&
753 strcmp(dmdp
->ddm_name
, minor_name
)))
756 /* filter out minor_all mismatches */
758 (((minor_name
== DEVID_MINOR_NAME_ALL_CHR
) &&
759 (dmdp
->ddm_spec_type
!= S_IFCHR
)) ||
760 ((minor_name
== DEVID_MINOR_NAME_ALL_BLK
) &&
761 (dmdp
->ddm_spec_type
!= S_IFBLK
))))
764 if (ndevts
< ndevts_alloced
)
765 devtsp
[ndevts
] = dmdp
->ddm_dev
;
768 ndi_devi_exit(dip
, circ
);
774 * Search for cached entries matching a devid
776 * a list of dev_info nodes, for those devices in the attached state
777 * a list of pathnames whose instances registered the given devid
778 * If the lists passed in are not sufficient to return the matching
779 * references, return the size of lists required.
780 * The dev_info nodes are returned with a hold that the caller must release.
783 e_devid_cache_devi_path_lists(ddi_devid_t devid
, int retmax
,
784 int *retndevis
, dev_info_t
**retdevis
, int *retnpaths
, char **retpaths
)
788 dev_info_t
*dip
, *pdip
;
796 listp
= nvf_list(dcfd_handle
);
797 for (np
= list_head(listp
); np
; np
= list_next(listp
, np
)) {
798 if (np
->nvp_devid
== NULL
)
800 if (ddi_devid_valid(np
->nvp_devid
) != DDI_SUCCESS
) {
802 "find: invalid devid %s\n",
806 if (ddi_devid_compare(devid
, np
->nvp_devid
) == 0) {
807 DEVID_DEBUG2((CE_CONT
,
808 "find: devid match: %s 0x%x\n",
809 np
->nvp_devpath
, np
->nvp_flags
));
810 DEVID_LOG_MATCH(("find", devid
, np
->nvp_devpath
));
811 DEVID_LOG_PATHS((CE_CONT
, "%s\n", np
->nvp_devpath
));
814 * Check if we have a cached devinfo reference for this
815 * devid. Place a hold on it to prevent detach
816 * Otherwise, use the path instead.
817 * Note: returns with a hold on each dev_info
821 if (np
->nvp_flags
& NVP_DEVID_DIP
) {
822 pdip
= ddi_get_parent(np
->nvp_dip
);
823 if (ndi_devi_tryenter(pdip
, &circ
)) {
826 ndi_devi_exit(pdip
, circ
);
827 ASSERT(!DEVI_IS_ATTACHING(dip
));
828 ASSERT(!DEVI_IS_DETACHING(dip
));
830 DEVID_LOG_DETACH((CE_CONT
,
831 "may be detaching: %s\n",
837 if (ndevis
< retmax
) {
838 retdevis
[ndevis
++] = dip
;
845 retpaths
[npaths
++] = np
->nvp_devpath
;
853 return (maxdevis
> maxpaths
? maxdevis
: maxpaths
);
858 * Search the devid cache, returning dev_t list for all
859 * device paths mapping to the device identified by the
862 * Primary interface used by ddi_lyr_devid_to_devlist()
865 e_devid_cache_to_devt_list(ddi_devid_t devid
, char *minor_name
,
866 int *retndevts
, dev_t
**retdevts
)
870 dev_t
*devts
, *udevts
;
872 int ndevts
, undevts
, ndevts_alloced
;
873 dev_info_t
*devi
, **devis
;
874 int ndevis
, npaths
, nalloced
;
875 ddi_devid_t match_devid
;
877 DEVID_LOG_FIND(("find", devid
, NULL
));
879 ASSERT(ddi_devid_valid(devid
) == DDI_SUCCESS
);
880 if (ddi_devid_valid(devid
) != DDI_SUCCESS
) {
881 DEVID_LOG_ERR(("invalid devid", devid
, NULL
));
882 return (DDI_FAILURE
);
888 paths
= kmem_zalloc(nalloced
* sizeof (char *), KM_SLEEP
);
889 devis
= kmem_zalloc(nalloced
* sizeof (dev_info_t
*), KM_SLEEP
);
891 rw_enter(nvf_lock(dcfd_handle
), RW_READER
);
892 n
= e_devid_cache_devi_path_lists(devid
, nalloced
,
893 &ndevis
, devis
, &npaths
, paths
);
896 rw_exit(nvf_lock(dcfd_handle
));
897 for (i
= 0; i
< ndevis
; i
++)
898 ndi_rele_devi(devis
[i
]);
899 kmem_free(paths
, nalloced
* sizeof (char *));
900 kmem_free(devis
, nalloced
* sizeof (dev_info_t
*));
904 for (i
= 0; i
< npaths
; i
++) {
905 path
= i_ddi_strdup(paths
[i
], KM_SLEEP
);
908 rw_exit(nvf_lock(dcfd_handle
));
910 if (ndevis
== 0 && npaths
== 0) {
911 DEVID_LOG_ERR(("no devid found", devid
, NULL
));
912 kmem_free(paths
, nalloced
* sizeof (char *));
913 kmem_free(devis
, nalloced
* sizeof (dev_info_t
*));
914 return (DDI_FAILURE
);
917 ndevts_alloced
= 128;
920 devts
= kmem_alloc(ndevts_alloced
* sizeof (dev_t
), KM_SLEEP
);
921 for (i
= 0; i
< ndevis
; i
++) {
922 ASSERT(!DEVI_IS_ATTACHING(devis
[i
]));
923 ASSERT(!DEVI_IS_DETACHING(devis
[i
]));
924 e_devid_minor_to_devlist(devis
[i
], minor_name
,
925 ndevts_alloced
, &ndevts
, devts
);
926 if (ndevts
> ndevts_alloced
) {
927 kmem_free(devts
, ndevts_alloced
* sizeof (dev_t
));
928 ndevts_alloced
+= 128;
932 for (i
= 0; i
< npaths
; i
++) {
933 DEVID_LOG_LOOKUP((CE_CONT
, "lookup %s\n", paths
[i
]));
934 devi
= e_ddi_hold_devi_by_path(paths
[i
], 0);
936 DEVID_LOG_STALE(("stale device reference",
941 * Verify the newly attached device registered a matching devid
943 if (i_ddi_devi_get_devid(DDI_DEV_T_ANY
, devi
,
944 &match_devid
) != DDI_SUCCESS
) {
946 "%s: no devid registered on attach\n",
948 ddi_release_devi(devi
);
952 if (ddi_devid_compare(devid
, match_devid
) != 0) {
953 DEVID_LOG_STALE(("new devid registered",
955 ddi_release_devi(devi
);
956 ddi_devid_free(match_devid
);
959 ddi_devid_free(match_devid
);
961 e_devid_minor_to_devlist(devi
, minor_name
,
962 ndevts_alloced
, &ndevts
, devts
);
963 ddi_release_devi(devi
);
964 if (ndevts
> ndevts_alloced
) {
966 ndevts_alloced
* sizeof (dev_t
));
967 ndevts_alloced
+= 128;
972 /* drop hold from e_devid_cache_devi_path_lists */
973 for (i
= 0; i
< ndevis
; i
++) {
974 ndi_rele_devi(devis
[i
]);
976 for (i
= 0; i
< npaths
; i
++) {
977 kmem_free(paths
[i
], strlen(paths
[i
]) + 1);
979 kmem_free(paths
, nalloced
* sizeof (char *));
980 kmem_free(devis
, nalloced
* sizeof (dev_info_t
*));
983 DEVID_LOG_ERR(("no devid found", devid
, NULL
));
984 kmem_free(devts
, ndevts_alloced
* sizeof (dev_t
));
985 return (DDI_FAILURE
);
989 * Build the final list of sorted dev_t's with duplicates collapsed so
990 * returned results are consistent. This prevents implementation
991 * artifacts from causing unnecessary changes in SVM namespace.
994 for (i
= 0; i
< (ndevts
- 1); i
++) {
995 for (j
= 0; j
< ((ndevts
- 1) - i
); j
++) {
996 if (devts
[j
+ 1] < devts
[j
]) {
998 devts
[j
] = devts
[j
+ 1];
999 devts
[j
+ 1] = tdevt
;
1004 /* determine number of unique values */
1005 for (undevts
= ndevts
, i
= 1; i
< ndevts
; i
++) {
1006 if (devts
[i
- 1] == devts
[i
])
1010 /* allocate unique */
1011 udevts
= kmem_alloc(undevts
* sizeof (dev_t
), KM_SLEEP
);
1014 udevts
[0] = devts
[0];
1015 for (i
= 1, j
= 1; i
< ndevts
; i
++) {
1016 if (devts
[i
- 1] != devts
[i
])
1017 udevts
[j
++] = devts
[i
];
1019 ASSERT(j
== undevts
);
1021 kmem_free(devts
, ndevts_alloced
* sizeof (dev_t
));
1023 *retndevts
= undevts
;
1026 return (DDI_SUCCESS
);
1030 e_devid_cache_free_devt_list(int ndevts
, dev_t
*devt_list
)
1032 kmem_free(devt_list
, ndevts
* sizeof (dev_t
*));
1036 * If given a full path and NULL ua, search for a cache entry
1037 * whose path matches the full path. On a cache hit duplicate the
1038 * devid of the matched entry into the given devid (caller
1039 * must free); nodenamebuf is not touched for this usage.
1041 * Given a path and a non-NULL unit address, search the cache for any entry
1042 * matching "<path>/%@<unit-address>" where '%' is a wildcard meaning
1043 * any node name. The path should not end a '/'. On a cache hit
1044 * duplicate the devid as before (caller must free) and copy into
1045 * the caller-provided nodenamebuf (if not NULL) the nodename of the
1048 * We must not make use of nvp_dip since that may be NULL for cached
1049 * entries that are not present in the current tree.
1052 e_devid_cache_path_to_devid(char *path
, char *ua
,
1053 char *nodenamebuf
, ddi_devid_t
*devidp
)
1055 size_t pathlen
, ualen
;
1056 int rv
= DDI_FAILURE
;
1061 if (path
== NULL
|| *path
== '\0' || (ua
&& *ua
== '\0') ||
1063 return (DDI_FAILURE
);
1068 pathlen
= strlen(path
);
1072 rw_enter(nvf_lock(dcfd_handle
), RW_READER
);
1074 listp
= nvf_list(dcfd_handle
);
1075 for (np
= list_head(listp
); np
; np
= list_next(listp
, np
)) {
1076 size_t nodelen
, candlen
, n
;
1077 ddi_devid_t devid_dup
;
1080 if (np
->nvp_devid
== NULL
)
1083 if (ddi_devid_valid(np
->nvp_devid
) != DDI_SUCCESS
) {
1085 "pathsearch: invalid devid %s\n",
1090 cand
= np
->nvp_devpath
; /* candidate path */
1092 /* If a full pathname was provided the compare is easy */
1094 if (strcmp(cand
, path
) == 0)
1101 * The compare for initial path plus ua and unknown nodename
1104 * Does the initial path component match 'path'?
1106 if (strncmp(path
, cand
, pathlen
) != 0)
1109 candlen
= strlen(cand
);
1112 * The next character must be a '/' and there must be no
1113 * further '/' thereafter. Begin by checking that the
1114 * candidate is long enough to include at mininum a
1115 * "/<nodename>@<ua>" after the initial portion already
1116 * matched assuming a nodename length of 1.
1118 if (candlen
< pathlen
+ 1 + 1 + 1 + ualen
||
1119 cand
[pathlen
] != '/' ||
1120 strchr(cand
+ pathlen
+ 1, '/') != NULL
)
1123 node
= cand
+ pathlen
+ 1; /* <node>@<ua> string */
1126 * Find the '@' before the unit address. Check for
1127 * unit address match.
1129 if ((uasep
= strchr(node
, '@')) == NULL
)
1133 * Check we still have enough length and that ua matches
1135 nodelen
= (uintptr_t)uasep
- (uintptr_t)node
;
1136 if (candlen
< pathlen
+ 1 + nodelen
+ 1 + ualen
||
1137 strncmp(ua
, uasep
+ 1, ualen
) != 0)
1140 n
= ddi_devid_sizeof(np
->nvp_devid
);
1141 devid_dup
= kmem_alloc(n
, KM_SLEEP
); /* caller must free */
1142 (void) bcopy(np
->nvp_devid
, devid_dup
, n
);
1143 *devidp
= devid_dup
;
1145 if (ua
&& nodenamebuf
) {
1146 (void) strncpy(nodenamebuf
, node
, nodelen
);
1147 nodenamebuf
[nodelen
] = '\0';
1154 rw_exit(nvf_lock(dcfd_handle
));
1161 devid_log(char *fmt
, ddi_devid_t devid
, char *path
)
1163 char *devidstr
= ddi_devid_str_encode(devid
, NULL
);
1165 cmn_err(CE_CONT
, "%s: %s %s\n", fmt
, path
, devidstr
);
1167 cmn_err(CE_CONT
, "%s: %s\n", fmt
, devidstr
);
1169 ddi_devid_str_free(devidstr
);