xref: /illumos-gate/usr/src/uts/common/os/driver_lyr.c (revision 279209130920025623a312e02980ec58c0f39430)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 /*
25  * Copyright 2019 Joyent, Inc.
26  */
27 
28 /*
29  * Layered driver support.
30  */
31 
32 #include <sys/atomic.h>
33 #include <sys/types.h>
34 #include <sys/t_lock.h>
35 #include <sys/param.h>
36 #include <sys/conf.h>
37 #include <sys/systm.h>
38 #include <sys/sysmacros.h>
39 #include <sys/buf.h>
40 #include <sys/cred.h>
41 #include <sys/uio.h>
42 #include <sys/vnode.h>
43 #include <sys/fs/snode.h>
44 #include <sys/open.h>
45 #include <sys/kmem.h>
46 #include <sys/file.h>
47 #include <sys/bootconf.h>
48 #include <sys/pathname.h>
49 #include <sys/bitmap.h>
50 #include <sys/stat.h>
51 #include <sys/dditypes.h>
52 #include <sys/ddi_impldefs.h>
53 #include <sys/ddi.h>
54 #include <sys/sunddi.h>
55 #include <sys/sunndi.h>
56 #include <sys/esunddi.h>
57 #include <sys/autoconf.h>
58 #include <sys/sunldi.h>
59 #include <sys/sunldi_impl.h>
60 #include <sys/errno.h>
61 #include <sys/debug.h>
62 #include <sys/modctl.h>
63 #include <sys/var.h>
64 #include <vm/seg_vn.h>
65 
66 #include <sys/stropts.h>
67 #include <sys/strsubr.h>
68 #include <sys/socket.h>
69 #include <sys/socketvar.h>
70 #include <sys/kstr.h>
71 
72 /*
73  * Device contract related
74  */
75 #include <sys/contract_impl.h>
76 #include <sys/contract/device_impl.h>
77 
78 /*
79  * Define macros to manipulate snode, vnode, and open device flags
80  */
81 #define	VTYP_VALID(i)	(((i) == VCHR) || ((i) == VBLK))
82 #define	VTYP_TO_OTYP(i)	(((i) == VCHR) ? OTYP_CHR : OTYP_BLK)
83 #define	VTYP_TO_STYP(i)	(((i) == VCHR) ? S_IFCHR : S_IFBLK)
84 
85 #define	OTYP_VALID(i)	(((i) == OTYP_CHR) || ((i) == OTYP_BLK))
86 #define	OTYP_TO_VTYP(i)	(((i) == OTYP_CHR) ? VCHR : VBLK)
87 #define	OTYP_TO_STYP(i)	(((i) == OTYP_CHR) ? S_IFCHR : S_IFBLK)
88 
89 #define	STYP_VALID(i)	(((i) == S_IFCHR) || ((i) == S_IFBLK))
90 #define	STYP_TO_VTYP(i)	(((i) == S_IFCHR) ? VCHR : VBLK)
91 
92 /*
93  * Define macros for accessing layered driver hash structures
94  */
95 #define	LH_HASH(vp)		(handle_hash_func(vp) % LH_HASH_SZ)
96 #define	LI_HASH(mid, dip, dev)	(ident_hash_func(mid, dip, dev) % LI_HASH_SZ)
97 
98 /*
99  * Define layered handle flags used in the lh_type field
100  */
101 #define	LH_STREAM	(0x1)	/* handle to a streams device */
102 #define	LH_CBDEV	(0x2)	/* handle to a char/block device */
103 
104 /*
105  * Define macro for devid property lookups
106  */
107 #define	DEVID_PROP_FLAGS	(DDI_PROP_DONTPASS | \
108 				DDI_PROP_TYPE_STRING|DDI_PROP_CANSLEEP)
109 
110 /*
111  * Dummy string for NDI events
112  */
113 #define	NDI_EVENT_SERVICE	"NDI_EVENT_SERVICE"
114 
115 static void ldi_ev_lock(void);
116 static void ldi_ev_unlock(void);
117 
118 #ifdef	LDI_OBSOLETE_EVENT
119 int ldi_remove_event_handler(ldi_handle_t lh, ldi_callback_id_t id);
120 #endif
121 
122 
123 /*
124  * globals
125  */
126 static kmutex_t			ldi_ident_hash_lock[LI_HASH_SZ];
127 static struct ldi_ident		*ldi_ident_hash[LI_HASH_SZ];
128 
129 static kmutex_t			ldi_handle_hash_lock[LH_HASH_SZ];
130 static struct ldi_handle	*ldi_handle_hash[LH_HASH_SZ];
131 static size_t			ldi_handle_hash_count;
132 
133 /*
134  * Use of "ldi_ev_callback_list" must be protected by ldi_ev_lock()
135  * and ldi_ev_unlock().
136  */
137 static struct ldi_ev_callback_list ldi_ev_callback_list;
138 
139 static uint32_t ldi_ev_id_pool = 0;
140 
141 struct ldi_ev_cookie {
142 	char *ck_evname;
143 	uint_t ck_sync;
144 	uint_t ck_ctype;
145 };
146 
147 static struct ldi_ev_cookie ldi_ev_cookies[] = {
148 	{ LDI_EV_OFFLINE, 1, CT_DEV_EV_OFFLINE},
149 	{ LDI_EV_DEGRADE, 0, CT_DEV_EV_DEGRADED},
150 	{ LDI_EV_DEVICE_REMOVE, 0, 0},
151 	{ NULL}			/* must terminate list */
152 };
153 
154 void
155 ldi_init(void)
156 {
157 	int i;
158 
159 	ldi_handle_hash_count = 0;
160 	for (i = 0; i < LH_HASH_SZ; i++) {
161 		mutex_init(&ldi_handle_hash_lock[i], NULL, MUTEX_DEFAULT, NULL);
162 		ldi_handle_hash[i] = NULL;
163 	}
164 	for (i = 0; i < LI_HASH_SZ; i++) {
165 		mutex_init(&ldi_ident_hash_lock[i], NULL, MUTEX_DEFAULT, NULL);
166 		ldi_ident_hash[i] = NULL;
167 	}
168 
169 	/*
170 	 * Initialize the LDI event subsystem
171 	 */
172 	mutex_init(&ldi_ev_callback_list.le_lock, NULL, MUTEX_DEFAULT, NULL);
173 	cv_init(&ldi_ev_callback_list.le_cv, NULL, CV_DEFAULT, NULL);
174 	ldi_ev_callback_list.le_busy = 0;
175 	ldi_ev_callback_list.le_thread = NULL;
176 	ldi_ev_callback_list.le_walker_next = NULL;
177 	ldi_ev_callback_list.le_walker_prev = NULL;
178 	list_create(&ldi_ev_callback_list.le_head,
179 	    sizeof (ldi_ev_callback_impl_t),
180 	    offsetof(ldi_ev_callback_impl_t, lec_list));
181 }
182 
183 /*
184  * LDI ident manipulation functions
185  */
186 static uint_t
187 ident_hash_func(modid_t modid, dev_info_t *dip, dev_t dev)
188 {
189 	if (dip != NULL) {
190 		uintptr_t k = (uintptr_t)dip;
191 		k >>= (int)highbit(sizeof (struct dev_info));
192 		return ((uint_t)k);
193 	} else if (dev != DDI_DEV_T_NONE) {
194 		return (modid + getminor(dev) + getmajor(dev));
195 	} else {
196 		return (modid);
197 	}
198 }
199 
200 static struct ldi_ident **
201 ident_find_ref_nolock(modid_t modid, dev_info_t *dip, dev_t dev, major_t major)
202 {
203 	struct ldi_ident	**lipp = NULL;
204 	uint_t			index = LI_HASH(modid, dip, dev);
205 
206 	ASSERT(MUTEX_HELD(&ldi_ident_hash_lock[index]));
207 
208 	for (lipp = &(ldi_ident_hash[index]);
209 	    (*lipp != NULL);
210 	    lipp = &((*lipp)->li_next)) {
211 		if (((*lipp)->li_modid == modid) &&
212 		    ((*lipp)->li_major == major) &&
213 		    ((*lipp)->li_dip == dip) &&
214 		    ((*lipp)->li_dev == dev))
215 			break;
216 	}
217 
218 	ASSERT(lipp != NULL);
219 	return (lipp);
220 }
221 
222 static struct ldi_ident *
223 ident_alloc(char *mod_name, dev_info_t *dip, dev_t dev, major_t major)
224 {
225 	struct ldi_ident	*lip, **lipp, *retlip;
226 	modid_t			modid;
227 	uint_t			index;
228 
229 	ASSERT(mod_name != NULL);
230 
231 	/* get the module id */
232 	modid = mod_name_to_modid(mod_name);
233 	ASSERT(modid != -1);
234 
235 	/* allocate a new ident in case we need it */
236 	lip = kmem_zalloc(sizeof (*lip), KM_SLEEP);
237 
238 	/* search the hash for a matching ident */
239 	index = LI_HASH(modid, dip, dev);
240 	mutex_enter(&ldi_ident_hash_lock[index]);
241 	lipp = ident_find_ref_nolock(modid, dip, dev, major);
242 
243 	if (*lipp != NULL) {
244 		/* we found an ident in the hash */
245 		ASSERT(strcmp((*lipp)->li_modname, mod_name) == 0);
246 		(*lipp)->li_ref++;
247 		retlip = *lipp;
248 		mutex_exit(&ldi_ident_hash_lock[index]);
249 		kmem_free(lip, sizeof (struct ldi_ident));
250 		return (retlip);
251 	}
252 
253 	/* initialize the new ident */
254 	lip->li_next = NULL;
255 	lip->li_ref = 1;
256 	lip->li_modid = modid;
257 	lip->li_major = major;
258 	lip->li_dip = dip;
259 	lip->li_dev = dev;
260 	(void) strncpy(lip->li_modname, mod_name, sizeof (lip->li_modname) - 1);
261 
262 	/* add it to the ident hash */
263 	lip->li_next = ldi_ident_hash[index];
264 	ldi_ident_hash[index] = lip;
265 
266 	mutex_exit(&ldi_ident_hash_lock[index]);
267 	return (lip);
268 }
269 
270 static void
271 ident_hold(struct ldi_ident *lip)
272 {
273 	uint_t			index;
274 
275 	ASSERT(lip != NULL);
276 	index = LI_HASH(lip->li_modid, lip->li_dip, lip->li_dev);
277 	mutex_enter(&ldi_ident_hash_lock[index]);
278 	ASSERT(lip->li_ref > 0);
279 	lip->li_ref++;
280 	mutex_exit(&ldi_ident_hash_lock[index]);
281 }
282 
283 static void
284 ident_release(struct ldi_ident *lip)
285 {
286 	struct ldi_ident	**lipp;
287 	uint_t			index;
288 
289 	ASSERT(lip != NULL);
290 	index = LI_HASH(lip->li_modid, lip->li_dip, lip->li_dev);
291 	mutex_enter(&ldi_ident_hash_lock[index]);
292 
293 	ASSERT(lip->li_ref > 0);
294 	if (--lip->li_ref > 0) {
295 		/* there are more references to this ident */
296 		mutex_exit(&ldi_ident_hash_lock[index]);
297 		return;
298 	}
299 
300 	/* this was the last reference/open for this ident.  free it. */
301 	lipp = ident_find_ref_nolock(
302 	    lip->li_modid, lip->li_dip, lip->li_dev, lip->li_major);
303 
304 	ASSERT((lipp != NULL) && (*lipp != NULL));
305 	*lipp = lip->li_next;
306 	mutex_exit(&ldi_ident_hash_lock[index]);
307 	kmem_free(lip, sizeof (struct ldi_ident));
308 }
309 
310 /*
311  * LDI handle manipulation functions
312  */
313 static uint_t
314 handle_hash_func(void *vp)
315 {
316 	uintptr_t k = (uintptr_t)vp;
317 	k >>= (int)highbit(sizeof (vnode_t));
318 	return ((uint_t)k);
319 }
320 
321 static struct ldi_handle **
322 handle_find_ref_nolock(vnode_t *vp, struct ldi_ident *ident)
323 {
324 	struct ldi_handle	**lhpp = NULL;
325 	uint_t			index = LH_HASH(vp);
326 
327 	ASSERT(MUTEX_HELD(&ldi_handle_hash_lock[index]));
328 
329 	for (lhpp = &(ldi_handle_hash[index]);
330 	    (*lhpp != NULL);
331 	    lhpp = &((*lhpp)->lh_next)) {
332 		if (((*lhpp)->lh_ident == ident) &&
333 		    ((*lhpp)->lh_vp == vp))
334 			break;
335 	}
336 
337 	ASSERT(lhpp != NULL);
338 	return (lhpp);
339 }
340 
341 static struct ldi_handle *
342 handle_find(vnode_t *vp, struct ldi_ident *ident)
343 {
344 	struct ldi_handle	**lhpp, *retlhp;
345 	int			index = LH_HASH(vp);
346 
347 	mutex_enter(&ldi_handle_hash_lock[index]);
348 	lhpp = handle_find_ref_nolock(vp, ident);
349 	retlhp = *lhpp;
350 	mutex_exit(&ldi_handle_hash_lock[index]);
351 	return (retlhp);
352 }
353 
354 static struct ldi_handle *
355 handle_alloc(vnode_t *vp, struct ldi_ident *ident)
356 {
357 	struct ldi_handle	*lhp, **lhpp, *retlhp;
358 	uint_t			index;
359 
360 	ASSERT((vp != NULL) && (ident != NULL));
361 
362 	/* allocate a new handle in case we need it */
363 	lhp = kmem_zalloc(sizeof (*lhp), KM_SLEEP);
364 
365 	/* search the hash for a matching handle */
366 	index = LH_HASH(vp);
367 	mutex_enter(&ldi_handle_hash_lock[index]);
368 	lhpp = handle_find_ref_nolock(vp, ident);
369 
370 	if (*lhpp != NULL) {
371 		/* we found a handle in the hash */
372 		(*lhpp)->lh_ref++;
373 		retlhp = *lhpp;
374 		mutex_exit(&ldi_handle_hash_lock[index]);
375 
376 		LDI_ALLOCFREE((CE_WARN, "ldi handle alloc: dup "
377 		    "lh=0x%p, ident=0x%p, vp=0x%p, drv=%s, minor=0x%x",
378 		    (void *)retlhp, (void *)ident, (void *)vp,
379 		    mod_major_to_name(getmajor(vp->v_rdev)),
380 		    getminor(vp->v_rdev)));
381 
382 		kmem_free(lhp, sizeof (struct ldi_handle));
383 		return (retlhp);
384 	}
385 
386 	/* initialize the new handle */
387 	lhp->lh_ref = 1;
388 	lhp->lh_vp = vp;
389 	lhp->lh_ident = ident;
390 #ifdef	LDI_OBSOLETE_EVENT
391 	mutex_init(lhp->lh_lock, NULL, MUTEX_DEFAULT, NULL);
392 #endif
393 
394 	/* set the device type for this handle */
395 	lhp->lh_type = 0;
396 	if (vp->v_stream) {
397 		ASSERT(vp->v_type == VCHR);
398 		lhp->lh_type |= LH_STREAM;
399 	} else {
400 		lhp->lh_type |= LH_CBDEV;
401 	}
402 
403 	/* get holds on other objects */
404 	ident_hold(ident);
405 	ASSERT(vp->v_count >= 1);
406 	VN_HOLD(vp);
407 
408 	/* add it to the handle hash */
409 	lhp->lh_next = ldi_handle_hash[index];
410 	ldi_handle_hash[index] = lhp;
411 	atomic_inc_ulong(&ldi_handle_hash_count);
412 
413 	LDI_ALLOCFREE((CE_WARN, "ldi handle alloc: new "
414 	    "lh=0x%p, ident=0x%p, vp=0x%p, drv=%s, minor=0x%x",
415 	    (void *)lhp, (void *)ident, (void *)vp,
416 	    mod_major_to_name(getmajor(vp->v_rdev)),
417 	    getminor(vp->v_rdev)));
418 
419 	mutex_exit(&ldi_handle_hash_lock[index]);
420 	return (lhp);
421 }
422 
423 static void
424 handle_release(struct ldi_handle *lhp)
425 {
426 	struct ldi_handle	**lhpp;
427 	uint_t			index;
428 
429 	ASSERT(lhp != NULL);
430 
431 	index = LH_HASH(lhp->lh_vp);
432 	mutex_enter(&ldi_handle_hash_lock[index]);
433 
434 	LDI_ALLOCFREE((CE_WARN, "ldi handle release: "
435 	    "lh=0x%p, ident=0x%p, vp=0x%p, drv=%s, minor=0x%x",
436 	    (void *)lhp, (void *)lhp->lh_ident, (void *)lhp->lh_vp,
437 	    mod_major_to_name(getmajor(lhp->lh_vp->v_rdev)),
438 	    getminor(lhp->lh_vp->v_rdev)));
439 
440 	ASSERT(lhp->lh_ref > 0);
441 	if (--lhp->lh_ref > 0) {
442 		/* there are more references to this handle */
443 		mutex_exit(&ldi_handle_hash_lock[index]);
444 		return;
445 	}
446 
447 	/* this was the last reference/open for this handle.  free it. */
448 	lhpp = handle_find_ref_nolock(lhp->lh_vp, lhp->lh_ident);
449 	ASSERT((lhpp != NULL) && (*lhpp != NULL));
450 	*lhpp = lhp->lh_next;
451 	atomic_dec_ulong(&ldi_handle_hash_count);
452 	mutex_exit(&ldi_handle_hash_lock[index]);
453 
454 	VN_RELE(lhp->lh_vp);
455 	ident_release(lhp->lh_ident);
456 #ifdef	LDI_OBSOLETE_EVENT
457 	mutex_destroy(lhp->lh_lock);
458 #endif
459 	kmem_free(lhp, sizeof (struct ldi_handle));
460 }
461 
462 #ifdef	LDI_OBSOLETE_EVENT
463 /*
464  * LDI event manipulation functions
465  */
466 static void
467 handle_event_add(ldi_event_t *lep)
468 {
469 	struct ldi_handle *lhp = lep->le_lhp;
470 
471 	ASSERT(lhp != NULL);
472 
473 	mutex_enter(lhp->lh_lock);
474 	if (lhp->lh_events == NULL) {
475 		lhp->lh_events = lep;
476 		mutex_exit(lhp->lh_lock);
477 		return;
478 	}
479 
480 	lep->le_next = lhp->lh_events;
481 	lhp->lh_events->le_prev = lep;
482 	lhp->lh_events = lep;
483 	mutex_exit(lhp->lh_lock);
484 }
485 
486 static void
487 handle_event_remove(ldi_event_t *lep)
488 {
489 	struct ldi_handle *lhp = lep->le_lhp;
490 
491 	ASSERT(lhp != NULL);
492 
493 	mutex_enter(lhp->lh_lock);
494 	if (lep->le_prev)
495 		lep->le_prev->le_next = lep->le_next;
496 	if (lep->le_next)
497 		lep->le_next->le_prev = lep->le_prev;
498 	if (lhp->lh_events == lep)
499 		lhp->lh_events = lep->le_next;
500 	mutex_exit(lhp->lh_lock);
501 
502 }
503 
504 static void
505 i_ldi_callback(dev_info_t *dip, ddi_eventcookie_t event_cookie,
506     void *arg, void *bus_impldata)
507 {
508 	ldi_event_t *lep = (ldi_event_t *)arg;
509 
510 	ASSERT(lep != NULL);
511 
512 	LDI_EVENTCB((CE_NOTE, "%s: dip=0x%p, "
513 	    "event_cookie=0x%p, ldi_eventp=0x%p", "i_ldi_callback",
514 	    (void *)dip, (void *)event_cookie, (void *)lep));
515 
516 	lep->le_handler(lep->le_lhp, event_cookie, lep->le_arg, bus_impldata);
517 }
518 #endif
519 
520 /*
521  * LDI open helper functions
522  */
523 
524 /* get a vnode to a device by dev_t and otyp */
525 static int
526 ldi_vp_from_dev(dev_t dev, int otyp, vnode_t **vpp)
527 {
528 	dev_info_t		*dip;
529 	vnode_t			*vp;
530 
531 	/* sanity check required input parameters */
532 	if ((dev == DDI_DEV_T_NONE) || (!OTYP_VALID(otyp)) || (vpp == NULL))
533 		return (EINVAL);
534 
535 	if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
536 		return (ENODEV);
537 
538 	vp = makespecvp(dev, OTYP_TO_VTYP(otyp));
539 	spec_assoc_vp_with_devi(vp, dip);
540 	ddi_release_devi(dip);  /* from e_ddi_hold_devi_by_dev */
541 
542 	*vpp = vp;
543 	return (0);
544 }
545 
546 /* get a vnode to a device by pathname */
547 int
548 ldi_vp_from_name(char *path, vnode_t **vpp)
549 {
550 	vnode_t			*vp = NULL;
551 	int			ret;
552 
553 	/* sanity check required input parameters */
554 	if ((path == NULL) || (vpp == NULL))
555 		return (EINVAL);
556 
557 	if (modrootloaded) {
558 		cred_t *saved_cred = curthread->t_cred;
559 
560 		/* we don't want lookupname to fail because of credentials */
561 		curthread->t_cred = kcred;
562 
563 		/*
564 		 * all lookups should be done in the global zone.  but
565 		 * lookupnameat() won't actually do this if an absolute
566 		 * path is passed in.  since the ldi interfaces require an
567 		 * absolute path we pass lookupnameat() a pointer to
568 		 * the character after the leading '/' and tell it to
569 		 * start searching at the current system root directory.
570 		 */
571 		ASSERT(*path == '/');
572 		ret = lookupnameat(path + 1, UIO_SYSSPACE, FOLLOW, NULLVPP,
573 		    &vp, rootdir);
574 
575 		/* restore this threads credentials */
576 		curthread->t_cred = saved_cred;
577 
578 		if (ret == 0) {
579 			if (!vn_matchops(vp, spec_getvnodeops()) ||
580 			    !VTYP_VALID(vp->v_type)) {
581 				VN_RELE(vp);
582 				return (ENXIO);
583 			}
584 		}
585 	}
586 
587 	if (vp == NULL) {
588 		dev_info_t	*dip;
589 		dev_t		dev;
590 		int		spec_type;
591 
592 		/*
593 		 * Root is not mounted, the minor node is not specified,
594 		 * or an OBP path has been specified.
595 		 */
596 
597 		/*
598 		 * Determine if path can be pruned to produce an
599 		 * OBP or devfs path for resolve_pathname.
600 		 */
601 		if (strncmp(path, "/devices/", 9) == 0)
602 			path += strlen("/devices");
603 
604 		/*
605 		 * if no minor node was specified the DEFAULT minor node
606 		 * will be returned.  if there is no DEFAULT minor node
607 		 * one will be fabricated of type S_IFCHR with the minor
608 		 * number equal to the instance number.
609 		 */
610 		ret = resolve_pathname(path, &dip, &dev, &spec_type);
611 		if (ret != 0)
612 			return (ENODEV);
613 
614 		ASSERT(STYP_VALID(spec_type));
615 		vp = makespecvp(dev, STYP_TO_VTYP(spec_type));
616 		spec_assoc_vp_with_devi(vp, dip);
617 		ddi_release_devi(dip);
618 	}
619 
620 	*vpp = vp;
621 	return (0);
622 }
623 
624 static int
625 ldi_devid_match(ddi_devid_t devid, dev_info_t *dip, dev_t dev)
626 {
627 	char		*devidstr;
628 	ddi_prop_t	*propp;
629 
630 	/* convert devid as a string property */
631 	if ((devidstr = ddi_devid_str_encode(devid, NULL)) == NULL)
632 		return (0);
633 
634 	/*
635 	 * Search for the devid.  For speed and ease in locking this
636 	 * code directly uses the property implementation.  See
637 	 * ddi_common_devid_to_devlist() for a comment as to why.
638 	 */
639 	mutex_enter(&(DEVI(dip)->devi_lock));
640 
641 	/* check if there is a DDI_DEV_T_NONE devid property */
642 	propp = i_ddi_prop_search(DDI_DEV_T_NONE,
643 	    DEVID_PROP_NAME, DEVID_PROP_FLAGS, &DEVI(dip)->devi_hw_prop_ptr);
644 	if (propp != NULL) {
645 		if (ddi_devid_str_compare(propp->prop_val, devidstr) == 0) {
646 			/* a DDI_DEV_T_NONE devid exists and matchs */
647 			mutex_exit(&(DEVI(dip)->devi_lock));
648 			ddi_devid_str_free(devidstr);
649 			return (1);
650 		} else {
651 			/* a DDI_DEV_T_NONE devid exists and doesn't match */
652 			mutex_exit(&(DEVI(dip)->devi_lock));
653 			ddi_devid_str_free(devidstr);
654 			return (0);
655 		}
656 	}
657 
658 	/* check if there is a devt specific devid property */
659 	propp = i_ddi_prop_search(dev,
660 	    DEVID_PROP_NAME, DEVID_PROP_FLAGS, &(DEVI(dip)->devi_hw_prop_ptr));
661 	if (propp != NULL) {
662 		if (ddi_devid_str_compare(propp->prop_val, devidstr) == 0) {
663 			/* a devt specific devid exists and matchs */
664 			mutex_exit(&(DEVI(dip)->devi_lock));
665 			ddi_devid_str_free(devidstr);
666 			return (1);
667 		} else {
668 			/* a devt specific devid exists and doesn't match */
669 			mutex_exit(&(DEVI(dip)->devi_lock));
670 			ddi_devid_str_free(devidstr);
671 			return (0);
672 		}
673 	}
674 
675 	/* we didn't find any devids associated with the device */
676 	mutex_exit(&(DEVI(dip)->devi_lock));
677 	ddi_devid_str_free(devidstr);
678 	return (0);
679 }
680 
681 /* get a handle to a device by devid and minor name */
682 int
683 ldi_vp_from_devid(ddi_devid_t devid, char *minor_name, vnode_t **vpp)
684 {
685 	dev_info_t		*dip;
686 	vnode_t			*vp;
687 	int			ret, i, ndevs, styp;
688 	dev_t			dev, *devs;
689 
690 	/* sanity check required input parameters */
691 	if ((devid == NULL) || (minor_name == NULL) || (vpp == NULL))
692 		return (EINVAL);
693 
694 	ret = ddi_lyr_devid_to_devlist(devid, minor_name, &ndevs, &devs);
695 	if ((ret != DDI_SUCCESS) || (ndevs <= 0))
696 		return (ENODEV);
697 
698 	for (i = 0; i < ndevs; i++) {
699 		dev = devs[i];
700 
701 		if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
702 			continue;
703 
704 		/*
705 		 * now we have to verify that the devid of the disk
706 		 * still matches what was requested.
707 		 *
708 		 * we have to do this because the devid could have
709 		 * changed between the call to ddi_lyr_devid_to_devlist()
710 		 * and e_ddi_hold_devi_by_dev().  this is because when
711 		 * ddi_lyr_devid_to_devlist() returns a list of devts
712 		 * there is no kind of hold on those devts so a device
713 		 * could have been replaced out from under us in the
714 		 * interim.
715 		 */
716 		if ((i_ddi_minorname_to_devtspectype(dip, minor_name,
717 		    NULL, &styp) == DDI_SUCCESS) &&
718 		    ldi_devid_match(devid, dip, dev))
719 			break;
720 
721 		ddi_release_devi(dip);	/* from e_ddi_hold_devi_by_dev() */
722 	}
723 
724 	ddi_lyr_free_devlist(devs, ndevs);
725 
726 	if (i == ndevs)
727 		return (ENODEV);
728 
729 	ASSERT(STYP_VALID(styp));
730 	vp = makespecvp(dev, STYP_TO_VTYP(styp));
731 	spec_assoc_vp_with_devi(vp, dip);
732 	ddi_release_devi(dip);		/* from e_ddi_hold_devi_by_dev */
733 
734 	*vpp = vp;
735 	return (0);
736 }
737 
738 /* given a vnode, open a device */
739 static int
740 ldi_open_by_vp(vnode_t **vpp, int flag, cred_t *cr,
741     ldi_handle_t *lhp, struct ldi_ident *li)
742 {
743 	struct ldi_handle	*nlhp;
744 	vnode_t			*vp;
745 	int			err;
746 
747 	ASSERT((vpp != NULL) && (*vpp != NULL));
748 	ASSERT((lhp != NULL) && (li != NULL));
749 
750 	vp = *vpp;
751 	/* if the vnode passed in is not a device, then bail */
752 	if (!vn_matchops(vp, spec_getvnodeops()) || !VTYP_VALID(vp->v_type))
753 		return (ENXIO);
754 
755 	/*
756 	 * the caller may have specified a node that
757 	 * doesn't have cb_ops defined.  the ldi doesn't yet
758 	 * support opening devices without a valid cb_ops.
759 	 */
760 	if (devopsp[getmajor(vp->v_rdev)]->devo_cb_ops == NULL)
761 		return (ENXIO);
762 
763 	/* open the device */
764 	if ((err = VOP_OPEN(&vp, flag | FKLYR, cr, NULL)) != 0)
765 		return (err);
766 
767 	/* possible clone open, make sure that we still have a spec node */
768 	ASSERT(vn_matchops(vp, spec_getvnodeops()));
769 
770 	nlhp = handle_alloc(vp, li);
771 
772 	if (vp != *vpp) {
773 		/*
774 		 * allocating the layered handle took a new hold on the vnode
775 		 * so we can release the hold that was returned by the clone
776 		 * open
777 		 */
778 		LDI_OPENCLOSE((CE_WARN, "%s: lh=0x%p",
779 		    "ldi clone open", (void *)nlhp));
780 	} else {
781 		LDI_OPENCLOSE((CE_WARN, "%s: lh=0x%p",
782 		    "ldi open", (void *)nlhp));
783 	}
784 
785 	*vpp = vp;
786 	*lhp = (ldi_handle_t)nlhp;
787 	return (0);
788 }
789 
790 /* Call a drivers prop_op(9E) interface */
791 static int
792 i_ldi_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
793     int flags, char *name, caddr_t valuep, int *lengthp)
794 {
795 	struct dev_ops	*ops = NULL;
796 	int		res;
797 
798 	ASSERT((dip != NULL) && (name != NULL));
799 	ASSERT((prop_op == PROP_LEN) || (valuep != NULL));
800 	ASSERT(lengthp != NULL);
801 
802 	/*
803 	 * we can only be invoked after a driver has been opened and
804 	 * someone has a layered handle to it, so there had better be
805 	 * a valid ops vector.
806 	 */
807 	ops = DEVI(dip)->devi_ops;
808 	ASSERT(ops && ops->devo_cb_ops);
809 
810 	/*
811 	 * Some nexus drivers incorrectly set cb_prop_op to nodev,
812 	 * nulldev or even NULL.
813 	 */
814 	if ((ops->devo_cb_ops->cb_prop_op == nodev) ||
815 	    (ops->devo_cb_ops->cb_prop_op == nulldev) ||
816 	    (ops->devo_cb_ops->cb_prop_op == NULL)) {
817 		return (DDI_PROP_NOT_FOUND);
818 	}
819 
820 	/* check if this is actually DDI_DEV_T_ANY query */
821 	if (flags & LDI_DEV_T_ANY) {
822 		flags &= ~LDI_DEV_T_ANY;
823 		dev = DDI_DEV_T_ANY;
824 	}
825 
826 	res = cdev_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp);
827 	return (res);
828 }
829 
830 static void
831 i_ldi_prop_op_free(struct prop_driver_data *pdd)
832 {
833 	kmem_free(pdd, pdd->pdd_size);
834 }
835 
836 static caddr_t
837 i_ldi_prop_op_alloc(int prop_len)
838 {
839 	struct prop_driver_data	*pdd;
840 	int			pdd_size;
841 
842 	pdd_size = sizeof (struct prop_driver_data) + prop_len;
843 	pdd = kmem_alloc(pdd_size, KM_SLEEP);
844 	pdd->pdd_size = pdd_size;
845 	pdd->pdd_prop_free = i_ldi_prop_op_free;
846 	return ((caddr_t)&pdd[1]);
847 }
848 
849 /*
850  * i_ldi_prop_op_typed() is a wrapper for i_ldi_prop_op that is used
851  * by the typed ldi property lookup interfaces.
852  */
853 static int
854 i_ldi_prop_op_typed(dev_t dev, dev_info_t *dip, int flags, char *name,
855     caddr_t *datap, int *lengthp, int elem_size)
856 {
857 	caddr_t	prop_val;
858 	int	prop_len, res;
859 
860 	ASSERT((dip != NULL) && (name != NULL));
861 	ASSERT((datap != NULL) && (lengthp != NULL));
862 
863 	/*
864 	 * first call the drivers prop_op() interface to allow it
865 	 * it to override default property values.
866 	 */
867 	res = i_ldi_prop_op(dev, dip, PROP_LEN,
868 	    flags | DDI_PROP_DYNAMIC, name, NULL, &prop_len);
869 	if (res != DDI_PROP_SUCCESS)
870 		return (DDI_PROP_NOT_FOUND);
871 
872 	/* sanity check the property length */
873 	if (prop_len == 0) {
874 		/*
875 		 * the ddi typed interfaces don't allow a drivers to
876 		 * create properties with a length of 0.  so we should
877 		 * prevent drivers from returning 0 length dynamic
878 		 * properties for typed property lookups.
879 		 */
880 		return (DDI_PROP_NOT_FOUND);
881 	}
882 
883 	/* sanity check the property length against the element size */
884 	if (elem_size && ((prop_len % elem_size) != 0))
885 		return (DDI_PROP_NOT_FOUND);
886 
887 	/*
888 	 * got it.  now allocate a prop_driver_data struct so that the
889 	 * user can free the property via ddi_prop_free().
890 	 */
891 	prop_val = i_ldi_prop_op_alloc(prop_len);
892 
893 	/* lookup the property again, this time get the value */
894 	res = i_ldi_prop_op(dev, dip, PROP_LEN_AND_VAL_BUF,
895 	    flags | DDI_PROP_DYNAMIC, name, prop_val, &prop_len);
896 	if (res != DDI_PROP_SUCCESS) {
897 		ddi_prop_free(prop_val);
898 		return (DDI_PROP_NOT_FOUND);
899 	}
900 
901 	/* sanity check the property length */
902 	if (prop_len == 0) {
903 		ddi_prop_free(prop_val);
904 		return (DDI_PROP_NOT_FOUND);
905 	}
906 
907 	/* sanity check the property length against the element size */
908 	if (elem_size && ((prop_len % elem_size) != 0)) {
909 		ddi_prop_free(prop_val);
910 		return (DDI_PROP_NOT_FOUND);
911 	}
912 
913 	/*
914 	 * return the prop_driver_data struct and, optionally, the length
915 	 * of the data.
916 	 */
917 	*datap = prop_val;
918 	*lengthp = prop_len;
919 
920 	return (DDI_PROP_SUCCESS);
921 }
922 
923 /*
924  * i_check_string looks at a string property and makes sure its
925  * a valid null terminated string
926  */
927 static int
928 i_check_string(char *str, int prop_len)
929 {
930 	int i;
931 
932 	ASSERT(str != NULL);
933 
934 	for (i = 0; i < prop_len; i++) {
935 		if (str[i] == '\0')
936 			return (0);
937 	}
938 	return (1);
939 }
940 
941 /*
942  * i_pack_string_array takes a a string array property that is represented
943  * as a concatenation of strings (with the NULL character included for
944  * each string) and converts it into a format that can be returned by
945  * ldi_prop_lookup_string_array.
946  */
947 static int
948 i_pack_string_array(char *str_concat, int prop_len,
949     char ***str_arrayp, int *nelemp)
950 {
951 	int i, nelem, pack_size;
952 	char **str_array, *strptr;
953 
954 	/*
955 	 * first we need to sanity check the input string array.
956 	 * in essence this can be done my making sure that the last
957 	 * character of the array passed in is null.  (meaning the last
958 	 * string in the array is NULL terminated.
959 	 */
960 	if (str_concat[prop_len - 1] != '\0')
961 		return (1);
962 
963 	/* now let's count the number of strings in the array */
964 	for (nelem = i = 0; i < prop_len; i++)
965 		if (str_concat[i] == '\0')
966 			nelem++;
967 	ASSERT(nelem >= 1);
968 
969 	/* now let's allocate memory for the new packed property */
970 	pack_size = (sizeof (char *) * (nelem + 1)) + prop_len;
971 	str_array = (char **)i_ldi_prop_op_alloc(pack_size);
972 
973 	/* let's copy the actual string data into the new property */
974 	strptr = (char *)&(str_array[nelem + 1]);
975 	bcopy(str_concat, strptr, prop_len);
976 
977 	/* now initialize the string array pointers */
978 	for (i = 0; i < nelem; i++) {
979 		str_array[i] = strptr;
980 		strptr += strlen(strptr) + 1;
981 	}
982 	str_array[nelem] = NULL;
983 
984 	/* set the return values */
985 	*str_arrayp = str_array;
986 	*nelemp = nelem;
987 
988 	return (0);
989 }
990 
991 
992 /*
993  * LDI Project private device usage interfaces
994  */
995 
996 /*
997  * Get a count of how many devices are currentl open by different consumers
998  */
999 int
1000 ldi_usage_count()
1001 {
1002 	return (ldi_handle_hash_count);
1003 }
1004 
1005 static void
1006 ldi_usage_walker_tgt_helper(ldi_usage_t *ldi_usage, vnode_t *vp)
1007 {
1008 	dev_info_t	*dip;
1009 	dev_t		dev;
1010 
1011 	ASSERT(STYP_VALID(VTYP_TO_STYP(vp->v_type)));
1012 
1013 	/* get the target devt */
1014 	dev = vp->v_rdev;
1015 
1016 	/* try to get the target dip */
1017 	dip = VTOCS(vp)->s_dip;
1018 	if (dip != NULL) {
1019 		e_ddi_hold_devi(dip);
1020 	} else if (dev != DDI_DEV_T_NONE) {
1021 		dip = e_ddi_hold_devi_by_dev(dev, 0);
1022 	}
1023 
1024 	/* set the target information */
1025 	ldi_usage->tgt_name = mod_major_to_name(getmajor(dev));
1026 	ldi_usage->tgt_modid = mod_name_to_modid(ldi_usage->tgt_name);
1027 	ldi_usage->tgt_devt = dev;
1028 	ldi_usage->tgt_spec_type = VTYP_TO_STYP(vp->v_type);
1029 	ldi_usage->tgt_dip = dip;
1030 }
1031 
1032 
1033 static int
1034 ldi_usage_walker_helper(struct ldi_ident *lip, vnode_t *vp,
1035     void *arg, int (*callback)(const ldi_usage_t *, void *))
1036 {
1037 	ldi_usage_t	ldi_usage;
1038 	struct devnames	*dnp;
1039 	dev_info_t	*dip;
1040 	major_t		major;
1041 	dev_t		dev;
1042 	int		ret = LDI_USAGE_CONTINUE;
1043 
1044 	/* set the target device information */
1045 	ldi_usage_walker_tgt_helper(&ldi_usage, vp);
1046 
1047 	/* get the source devt */
1048 	dev = lip->li_dev;
1049 
1050 	/* try to get the source dip */
1051 	dip = lip->li_dip;
1052 	if (dip != NULL) {
1053 		e_ddi_hold_devi(dip);
1054 	} else if (dev != DDI_DEV_T_NONE) {
1055 		dip = e_ddi_hold_devi_by_dev(dev, 0);
1056 	}
1057 
1058 	/* set the valid source information */
1059 	ldi_usage.src_modid = lip->li_modid;
1060 	ldi_usage.src_name = lip->li_modname;
1061 	ldi_usage.src_devt = dev;
1062 	ldi_usage.src_dip = dip;
1063 
1064 	/*
1065 	 * if the source ident represents either:
1066 	 *
1067 	 * - a kernel module (and not a device or device driver)
1068 	 * - a device node
1069 	 *
1070 	 * then we currently have all the info we need to report the
1071 	 * usage information so invoke the callback function.
1072 	 */
1073 	if (((lip->li_major == -1) && (dev == DDI_DEV_T_NONE)) ||
1074 	    (dip != NULL)) {
1075 		ret = callback(&ldi_usage, arg);
1076 		if (dip != NULL)
1077 			ddi_release_devi(dip);
1078 		if (ldi_usage.tgt_dip != NULL)
1079 			ddi_release_devi(ldi_usage.tgt_dip);
1080 		return (ret);
1081 	}
1082 
1083 	/*
1084 	 * now this is kinda gross.
1085 	 *
1086 	 * what we do here is attempt to associate every device instance
1087 	 * of the source driver on the system with the open target driver.
1088 	 * we do this because we don't know which instance of the device
1089 	 * could potentially access the lower device so we assume that all
1090 	 * the instances could access it.
1091 	 *
1092 	 * there are two ways we could have gotten here:
1093 	 *
1094 	 * 1) this layered ident represents one created using only a
1095 	 *    major number or a driver module name.  this means that when
1096 	 *    it was created we could not associate it with a particular
1097 	 *    dev_t or device instance.
1098 	 *
1099 	 *    when could this possibly happen you ask?
1100 	 *
1101 	 *    a perfect example of this is streams persistent links.
1102 	 *    when a persistant streams link is formed we can't associate
1103 	 *    the lower device stream with any particular upper device
1104 	 *    stream or instance.  this is because any particular upper
1105 	 *    device stream could be closed, then another could be
1106 	 *    opened with a different dev_t and device instance, and it
1107 	 *    would still have access to the lower linked stream.
1108 	 *
1109 	 *    since any instance of the upper streams driver could
1110 	 *    potentially access the lower stream whenever it wants,
1111 	 *    we represent that here by associating the opened lower
1112 	 *    device with every existing device instance of the upper
1113 	 *    streams driver.
1114 	 *
1115 	 * 2) This case should really never happen but we'll include it
1116 	 *    for completeness.
1117 	 *
1118 	 *    it's possible that we could have gotten here because we
1119 	 *    have a dev_t for the upper device but we couldn't find a
1120 	 *    dip associated with that dev_t.
1121 	 *
1122 	 *    the only types of devices that have dev_t without an
1123 	 *    associated dip are unbound DLPIv2 network devices.  These
1124 	 *    types of devices exist to be able to attach a stream to any
1125 	 *    instance of a hardware network device.  since these types of
1126 	 *    devices are usually hardware devices they should never
1127 	 *    really have other devices open.
1128 	 */
1129 	if (dev != DDI_DEV_T_NONE)
1130 		major = getmajor(dev);
1131 	else
1132 		major = lip->li_major;
1133 
1134 	ASSERT((major >= 0) && (major < devcnt));
1135 
1136 	dnp = &devnamesp[major];
1137 	LOCK_DEV_OPS(&dnp->dn_lock);
1138 	dip = dnp->dn_head;
1139 	while ((dip) && (ret == LDI_USAGE_CONTINUE)) {
1140 		e_ddi_hold_devi(dip);
1141 		UNLOCK_DEV_OPS(&dnp->dn_lock);
1142 
1143 		/* set the source dip */
1144 		ldi_usage.src_dip = dip;
1145 
1146 		/* invoke the callback function */
1147 		ret = callback(&ldi_usage, arg);
1148 
1149 		LOCK_DEV_OPS(&dnp->dn_lock);
1150 		ddi_release_devi(dip);
1151 		dip = ddi_get_next(dip);
1152 	}
1153 	UNLOCK_DEV_OPS(&dnp->dn_lock);
1154 
1155 	/* if there was a target dip, release it */
1156 	if (ldi_usage.tgt_dip != NULL)
1157 		ddi_release_devi(ldi_usage.tgt_dip);
1158 
1159 	return (ret);
1160 }
1161 
1162 /*
1163  * ldi_usage_walker() - this walker reports LDI kernel device usage
1164  * information via the callback() callback function.  the LDI keeps track
1165  * of what devices are being accessed in its own internal data structures.
1166  * this function walks those data structures to determine device usage.
1167  */
1168 void
1169 ldi_usage_walker(void *arg, int (*callback)(const ldi_usage_t *, void *))
1170 {
1171 	struct ldi_handle	*lhp;
1172 	struct ldi_ident	*lip;
1173 	vnode_t			*vp;
1174 	int			i;
1175 	int			ret = LDI_USAGE_CONTINUE;
1176 
1177 	for (i = 0; i < LH_HASH_SZ; i++) {
1178 		mutex_enter(&ldi_handle_hash_lock[i]);
1179 
1180 		lhp = ldi_handle_hash[i];
1181 		while ((lhp != NULL) && (ret == LDI_USAGE_CONTINUE)) {
1182 			lip = lhp->lh_ident;
1183 			vp = lhp->lh_vp;
1184 
1185 			/* invoke the devinfo callback function */
1186 			ret = ldi_usage_walker_helper(lip, vp, arg, callback);
1187 
1188 			lhp = lhp->lh_next;
1189 		}
1190 		mutex_exit(&ldi_handle_hash_lock[i]);
1191 
1192 		if (ret != LDI_USAGE_CONTINUE)
1193 			break;
1194 	}
1195 }
1196 
1197 /*
1198  * LDI Project private interfaces (streams linking interfaces)
1199  *
1200  * Streams supports a type of built in device layering via linking.
1201  * Certain types of streams drivers can be streams multiplexors.
1202  * A streams multiplexor supports the I_LINK/I_PLINK operation.
1203  * These operations allows other streams devices to be linked under the
1204  * multiplexor.  By definition all streams multiplexors are devices
1205  * so this linking is a type of device layering where the multiplexor
1206  * device is layered on top of the device linked below it.
1207  */
1208 
1209 /*
1210  * ldi_mlink_lh() is invoked when streams are linked using LDI handles.
1211  * It is not used for normal I_LINKs and I_PLINKs using file descriptors.
1212  *
1213  * The streams framework keeps track of links via the file_t of the lower
1214  * stream.  The LDI keeps track of devices using a vnode.  In the case
1215  * of a streams link created via an LDI handle, fnk_lh() allocates
1216  * a file_t that the streams framework can use to track the linkage.
1217  */
1218 int
1219 ldi_mlink_lh(vnode_t *vp, int cmd, intptr_t arg, cred_t *crp, int *rvalp)
1220 {
1221 	struct ldi_handle	*lhp = (struct ldi_handle *)arg;
1222 	vnode_t			*vpdown;
1223 	file_t			*fpdown;
1224 	int			err;
1225 
1226 	if (lhp == NULL)
1227 		return (EINVAL);
1228 
1229 	vpdown = lhp->lh_vp;
1230 	ASSERT(vn_matchops(vpdown, spec_getvnodeops()));
1231 	ASSERT(cmd == _I_PLINK_LH);
1232 
1233 	/*
1234 	 * create a new lower vnode and a file_t that points to it,
1235 	 * streams linking requires a file_t.  falloc() returns with
1236 	 * fpdown locked.
1237 	 */
1238 	VN_HOLD(vpdown);
1239 	(void) falloc(vpdown, FREAD|FWRITE, &fpdown, NULL);
1240 	mutex_exit(&fpdown->f_tlock);
1241 
1242 	/* try to establish the link */
1243 	err = mlink_file(vp, I_PLINK, fpdown, crp, rvalp, 1);
1244 
1245 	if (err != 0) {
1246 		/* the link failed, free the file_t and release the vnode */
1247 		mutex_enter(&fpdown->f_tlock);
1248 		unfalloc(fpdown);
1249 		VN_RELE(vpdown);
1250 	}
1251 
1252 	return (err);
1253 }
1254 
1255 /*
1256  * ldi_mlink_fp() is invoked for all successful streams linkages created
1257  * via I_LINK and I_PLINK.  ldi_mlink_fp() records the linkage information
1258  * in its internal state so that the devinfo snapshot code has some
1259  * observability into streams device linkage information.
1260  */
1261 int
1262 ldi_mlink_fp(struct stdata *stp, file_t *fpdown, int lhlink, int type)
1263 {
1264 	vnode_t			*vp = fpdown->f_vnode;
1265 	struct snode		*sp, *csp;
1266 	ldi_ident_t		li;
1267 	major_t			major;
1268 	int			ret;
1269 
1270 	/* if the lower stream is not a device then return */
1271 	if (!vn_matchops(vp, spec_getvnodeops()))
1272 		return (EINVAL);
1273 
1274 	ASSERT(!servicing_interrupt());
1275 
1276 	LDI_STREAMS_LNK((CE_NOTE, "%s: linking streams "
1277 	    "stp=0x%p, fpdown=0x%p", "ldi_mlink_fp",
1278 	    (void *)stp, (void *)fpdown));
1279 
1280 	sp = VTOS(vp);
1281 	csp = VTOS(sp->s_commonvp);
1282 
1283 	/* get a layered ident for the upper stream */
1284 	if (type == LINKNORMAL) {
1285 		/*
1286 		 * if the link is not persistant then we can associate
1287 		 * the upper stream with a dev_t.  this is because the
1288 		 * upper stream is associated with a vnode, which is
1289 		 * associated with a dev_t and this binding can't change
1290 		 * during the life of the stream.  since the link isn't
1291 		 * persistant once the stream is destroyed the link is
1292 		 * destroyed.  so the dev_t will be valid for the life
1293 		 * of the link.
1294 		 */
1295 		ret = ldi_ident_from_stream(getendq(stp->sd_wrq), &li);
1296 	} else {
1297 		/*
1298 		 * if the link is persistant we can only associate the
1299 		 * link with a driver (and not a dev_t.)  this is
1300 		 * because subsequent opens of the upper device may result
1301 		 * in a different stream (and dev_t) having access to
1302 		 * the lower stream.
1303 		 *
1304 		 * for example, if the upper stream is closed after the
1305 		 * persistant link operation is completed, a subsequent
1306 		 * open of the upper device will create a new stream which
1307 		 * may have a different dev_t and an unlink operation
1308 		 * can be performed using this new upper stream.
1309 		 */
1310 		VERIFY3S(type, ==, LINKPERSIST);
1311 		major = getmajor(stp->sd_vnode->v_rdev);
1312 		ret = ldi_ident_from_major(major, &li);
1313 	}
1314 
1315 	if (ret != 0)
1316 		return (ret);
1317 
1318 	/* check if this was a plink via a layered handle */
1319 	if (lhlink) {
1320 		/*
1321 		 * increment the common snode s_count.
1322 		 *
1323 		 * this is done because after the link operation there
1324 		 * are two ways that s_count can be decremented.
1325 		 *
1326 		 * when the layered handle used to create the link is
1327 		 * closed, spec_close() is called and it will decrement
1328 		 * s_count in the common snode.  if we don't increment
1329 		 * s_count here then this could cause spec_close() to
1330 		 * actually close the device while it's still linked
1331 		 * under a multiplexer.
1332 		 *
1333 		 * also, when the lower stream is unlinked, closef() is
1334 		 * called for the file_t associated with this snode.
1335 		 * closef() will call spec_close(), which will decrement
1336 		 * s_count.  if we dont't increment s_count here then this
1337 		 * could cause spec_close() to actually close the device
1338 		 * while there may still be valid layered handles
1339 		 * pointing to it.
1340 		 */
1341 		VERIFY3S(type, ==, LINKPERSIST);
1342 
1343 		mutex_enter(&csp->s_lock);
1344 		VERIFY(csp->s_count >= 1);
1345 		csp->s_count++;
1346 		mutex_exit(&csp->s_lock);
1347 
1348 		/*
1349 		 * decrement the f_count.
1350 		 * this is done because the layered driver framework does
1351 		 * not actually cache a copy of the file_t allocated to
1352 		 * do the link.  this is done here instead of in ldi_mlink_lh()
1353 		 * because there is a window in ldi_mlink_lh() between where
1354 		 * milnk_file() returns and we would decrement the f_count
1355 		 * when the stream could be unlinked.
1356 		 */
1357 		mutex_enter(&fpdown->f_tlock);
1358 		fpdown->f_count--;
1359 		mutex_exit(&fpdown->f_tlock);
1360 	}
1361 
1362 	/*
1363 	 * NOTE: here we rely on the streams subsystem not allowing
1364 	 * a stream to be multiplexed more than once.  if this
1365 	 * changes, we break.
1366 	 *
1367 	 * mark the snode/stream as multiplexed
1368 	 */
1369 	mutex_enter(&sp->s_lock);
1370 	VERIFY(!(sp->s_flag & SMUXED));
1371 	sp->s_flag |= SMUXED;
1372 	mutex_exit(&sp->s_lock);
1373 
1374 	(void) handle_alloc(vp, (struct ldi_ident *)li);
1375 	ldi_ident_release(li);
1376 
1377 	return (0);
1378 }
1379 
1380 int
1381 ldi_munlink_fp(struct stdata *stp, file_t *fpdown, int type)
1382 {
1383 	struct ldi_handle	*lhp;
1384 	vnode_t			*vp = (vnode_t *)fpdown->f_vnode;
1385 	struct snode		*sp;
1386 	ldi_ident_t		li;
1387 	major_t			major;
1388 	int			ret;
1389 
1390 	/* if the lower stream is not a device then return */
1391 	if (!vn_matchops(vp, spec_getvnodeops()))
1392 		return (EINVAL);
1393 
1394 	ASSERT(!servicing_interrupt());
1395 
1396 	LDI_STREAMS_LNK((CE_NOTE, "%s: unlinking streams "
1397 	    "stp=0x%p, fpdown=0x%p", "ldi_munlink_fp",
1398 	    (void *)stp, (void *)fpdown));
1399 
1400 	/*
1401 	 * clear the owner for this snode
1402 	 * see the comment in ldi_mlink_fp() for information about how
1403 	 * the ident is allocated
1404 	 */
1405 	if (type == LINKNORMAL) {
1406 		ret = ldi_ident_from_stream(getendq(stp->sd_wrq), &li);
1407 	} else {
1408 		VERIFY3S(type, ==, LINKPERSIST);
1409 		major = getmajor(stp->sd_vnode->v_rdev);
1410 		ret = ldi_ident_from_major(major, &li);
1411 	}
1412 
1413 	if (ret != 0)
1414 		return (ret);
1415 
1416 	/*
1417 	 * NOTE: here we rely on the streams subsystem not allowing
1418 	 * a stream to be multiplexed more than once.  if this
1419 	 * changes, we break.
1420 	 *
1421 	 * mark the snode/stream as not multiplexed
1422 	 */
1423 	sp = VTOS(vp);
1424 	mutex_enter(&sp->s_lock);
1425 	VERIFY(sp->s_flag & SMUXED);
1426 	sp->s_flag &= ~SMUXED;
1427 	mutex_exit(&sp->s_lock);
1428 
1429 	lhp = handle_find(vp, (struct ldi_ident *)li);
1430 	handle_release(lhp);
1431 	ldi_ident_release(li);
1432 
1433 	return (0);
1434 }
1435 
1436 /*
1437  * LDI Consolidation private interfaces
1438  */
1439 int
1440 ldi_ident_from_mod(struct modlinkage *modlp, ldi_ident_t *lip)
1441 {
1442 	struct modctl		*modp;
1443 	major_t			major;
1444 	char			*name;
1445 
1446 	if ((modlp == NULL) || (lip == NULL))
1447 		return (EINVAL);
1448 
1449 	ASSERT(!servicing_interrupt());
1450 
1451 	modp = mod_getctl(modlp);
1452 	if (modp == NULL)
1453 		return (EINVAL);
1454 	name = modp->mod_modname;
1455 	if (name == NULL)
1456 		return (EINVAL);
1457 	major = mod_name_to_major(name);
1458 
1459 	*lip = (ldi_ident_t)ident_alloc(name, NULL, DDI_DEV_T_NONE, major);
1460 
1461 	LDI_ALLOCFREE((CE_WARN, "%s: li=0x%p, mod=%s",
1462 	    "ldi_ident_from_mod", (void *)*lip, name));
1463 
1464 	return (0);
1465 }
1466 
1467 ldi_ident_t
1468 ldi_ident_from_anon()
1469 {
1470 	ldi_ident_t	lip;
1471 
1472 	ASSERT(!servicing_interrupt());
1473 
1474 	lip = (ldi_ident_t)ident_alloc("genunix", NULL, DDI_DEV_T_NONE, -1);
1475 
1476 	LDI_ALLOCFREE((CE_WARN, "%s: li=0x%p, mod=%s",
1477 	    "ldi_ident_from_anon", (void *)lip, "genunix"));
1478 
1479 	return (lip);
1480 }
1481 
1482 
1483 /*
1484  * LDI Public interfaces
1485  */
1486 int
1487 ldi_ident_from_stream(struct queue *sq, ldi_ident_t *lip)
1488 {
1489 	struct stdata		*stp;
1490 	dev_t			dev;
1491 	char			*name;
1492 
1493 	if ((sq == NULL) || (lip == NULL))
1494 		return (EINVAL);
1495 
1496 	ASSERT(!servicing_interrupt());
1497 
1498 	stp = sq->q_stream;
1499 	if (!vn_matchops(stp->sd_vnode, spec_getvnodeops()))
1500 		return (EINVAL);
1501 
1502 	dev = stp->sd_vnode->v_rdev;
1503 	name = mod_major_to_name(getmajor(dev));
1504 	if (name == NULL)
1505 		return (EINVAL);
1506 	*lip = (ldi_ident_t)ident_alloc(name, NULL, dev, -1);
1507 
1508 	LDI_ALLOCFREE((CE_WARN,
1509 	    "%s: li=0x%p, mod=%s, minor=0x%x, stp=0x%p",
1510 	    "ldi_ident_from_stream", (void *)*lip, name, getminor(dev),
1511 	    (void *)stp));
1512 
1513 	return (0);
1514 }
1515 
1516 int
1517 ldi_ident_from_dev(dev_t dev, ldi_ident_t *lip)
1518 {
1519 	char			*name;
1520 
1521 	if (lip == NULL)
1522 		return (EINVAL);
1523 
1524 	ASSERT(!servicing_interrupt());
1525 
1526 	name = mod_major_to_name(getmajor(dev));
1527 	if (name == NULL)
1528 		return (EINVAL);
1529 	*lip = (ldi_ident_t)ident_alloc(name, NULL, dev, -1);
1530 
1531 	LDI_ALLOCFREE((CE_WARN,
1532 	    "%s: li=0x%p, mod=%s, minor=0x%x",
1533 	    "ldi_ident_from_dev", (void *)*lip, name, getminor(dev)));
1534 
1535 	return (0);
1536 }
1537 
1538 int
1539 ldi_ident_from_dip(dev_info_t *dip, ldi_ident_t *lip)
1540 {
1541 	struct dev_info		*devi = (struct dev_info *)dip;
1542 	char			*name;
1543 
1544 	if ((dip == NULL) || (lip == NULL))
1545 		return (EINVAL);
1546 
1547 	ASSERT(!servicing_interrupt());
1548 
1549 	name = mod_major_to_name(devi->devi_major);
1550 	if (name == NULL)
1551 		return (EINVAL);
1552 	*lip = (ldi_ident_t)ident_alloc(name, dip, DDI_DEV_T_NONE, -1);
1553 
1554 	LDI_ALLOCFREE((CE_WARN,
1555 	    "%s: li=0x%p, mod=%s, dip=0x%p",
1556 	    "ldi_ident_from_dip", (void *)*lip, name, (void *)devi));
1557 
1558 	return (0);
1559 }
1560 
1561 int
1562 ldi_ident_from_major(major_t major, ldi_ident_t *lip)
1563 {
1564 	char			*name;
1565 
1566 	if (lip == NULL)
1567 		return (EINVAL);
1568 
1569 	ASSERT(!servicing_interrupt());
1570 
1571 	name = mod_major_to_name(major);
1572 	if (name == NULL)
1573 		return (EINVAL);
1574 	*lip = (ldi_ident_t)ident_alloc(name, NULL, DDI_DEV_T_NONE, major);
1575 
1576 	LDI_ALLOCFREE((CE_WARN,
1577 	    "%s: li=0x%p, mod=%s",
1578 	    "ldi_ident_from_major", (void *)*lip, name));
1579 
1580 	return (0);
1581 }
1582 
1583 void
1584 ldi_ident_release(ldi_ident_t li)
1585 {
1586 	struct ldi_ident	*ident = (struct ldi_ident *)li;
1587 	char			*name;
1588 
1589 	if (li == NULL)
1590 		return;
1591 
1592 	ASSERT(!servicing_interrupt());
1593 
1594 	name = ident->li_modname;
1595 
1596 	LDI_ALLOCFREE((CE_WARN,
1597 	    "%s: li=0x%p, mod=%s",
1598 	    "ldi_ident_release", (void *)li, name));
1599 
1600 	ident_release((struct ldi_ident *)li);
1601 }
1602 
1603 /* get a handle to a device by dev_t and otyp */
1604 int
1605 ldi_open_by_dev(dev_t *devp, int otyp, int flag, cred_t *cr,
1606     ldi_handle_t *lhp, ldi_ident_t li)
1607 {
1608 	struct ldi_ident	*lip = (struct ldi_ident *)li;
1609 	int			ret;
1610 	vnode_t			*vp;
1611 
1612 	/* sanity check required input parameters */
1613 	if ((devp == NULL) || (!OTYP_VALID(otyp)) || (cr == NULL) ||
1614 	    (lhp == NULL) || (lip == NULL))
1615 		return (EINVAL);
1616 
1617 	ASSERT(!servicing_interrupt());
1618 
1619 	if ((ret = ldi_vp_from_dev(*devp, otyp, &vp)) != 0)
1620 		return (ret);
1621 
1622 	if ((ret = ldi_open_by_vp(&vp, flag, cr, lhp, lip)) == 0) {
1623 		*devp = vp->v_rdev;
1624 	}
1625 	VN_RELE(vp);
1626 
1627 	return (ret);
1628 }
1629 
1630 /* get a handle to a device by pathname */
1631 int
1632 ldi_open_by_name(char *pathname, int flag, cred_t *cr,
1633     ldi_handle_t *lhp, ldi_ident_t li)
1634 {
1635 	struct ldi_ident	*lip = (struct ldi_ident *)li;
1636 	int			ret;
1637 	vnode_t			*vp;
1638 
1639 	/* sanity check required input parameters */
1640 	if ((pathname == NULL) || (*pathname != '/') ||
1641 	    (cr == NULL) || (lhp == NULL) || (lip == NULL))
1642 		return (EINVAL);
1643 
1644 	ASSERT(!servicing_interrupt());
1645 
1646 	if ((ret = ldi_vp_from_name(pathname, &vp)) != 0)
1647 		return (ret);
1648 
1649 	ret = ldi_open_by_vp(&vp, flag, cr, lhp, lip);
1650 	VN_RELE(vp);
1651 
1652 	return (ret);
1653 }
1654 
1655 /* get a handle to a device by devid and minor_name */
1656 int
1657 ldi_open_by_devid(ddi_devid_t devid, char *minor_name,
1658     int flag, cred_t *cr, ldi_handle_t *lhp, ldi_ident_t li)
1659 {
1660 	struct ldi_ident	*lip = (struct ldi_ident *)li;
1661 	int			ret;
1662 	vnode_t			*vp;
1663 
1664 	/* sanity check required input parameters */
1665 	if ((minor_name == NULL) || (cr == NULL) ||
1666 	    (lhp == NULL) || (lip == NULL))
1667 		return (EINVAL);
1668 
1669 	ASSERT(!servicing_interrupt());
1670 
1671 	if ((ret = ldi_vp_from_devid(devid, minor_name, &vp)) != 0)
1672 		return (ret);
1673 
1674 	ret = ldi_open_by_vp(&vp, flag, cr, lhp, lip);
1675 	VN_RELE(vp);
1676 
1677 	return (ret);
1678 }
1679 
1680 int
1681 ldi_close(ldi_handle_t lh, int flag, cred_t *cr)
1682 {
1683 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
1684 	struct ldi_event	*lep;
1685 	int			err = 0;
1686 	int			notify = 0;
1687 	list_t			*listp;
1688 	ldi_ev_callback_impl_t	*lecp;
1689 
1690 	if (lh == NULL)
1691 		return (EINVAL);
1692 
1693 	ASSERT(!servicing_interrupt());
1694 
1695 #ifdef	LDI_OBSOLETE_EVENT
1696 
1697 	/*
1698 	 * Any event handlers should have been unregistered by the
1699 	 * time ldi_close() is called.  If they haven't then it's a
1700 	 * bug.
1701 	 *
1702 	 * In a debug kernel we'll panic to make the problem obvious.
1703 	 */
1704 	ASSERT(handlep->lh_events == NULL);
1705 
1706 	/*
1707 	 * On a production kernel we'll "do the right thing" (unregister
1708 	 * the event handlers) and then complain about having to do the
1709 	 * work ourselves.
1710 	 */
1711 	while ((lep = handlep->lh_events) != NULL) {
1712 		err = 1;
1713 		(void) ldi_remove_event_handler(lh, (ldi_callback_id_t)lep);
1714 	}
1715 	if (err) {
1716 		struct ldi_ident *lip = handlep->lh_ident;
1717 		ASSERT(lip != NULL);
1718 		cmn_err(CE_NOTE, "ldi err: %s "
1719 		    "failed to unregister layered event handlers before "
1720 		    "closing devices", lip->li_modname);
1721 	}
1722 #endif
1723 
1724 	/* do a layered close on the device */
1725 	err = VOP_CLOSE(handlep->lh_vp, flag | FKLYR, 1, (offset_t)0, cr, NULL);
1726 
1727 	LDI_OPENCLOSE((CE_WARN, "%s: lh=0x%p", "ldi close", (void *)lh));
1728 
1729 	/*
1730 	 * Search the event callback list for callbacks with this
1731 	 * handle. There are 2 cases
1732 	 * 1. Called in the context of a notify. The handle consumer
1733 	 *    is releasing its hold on the device to allow a reconfiguration
1734 	 *    of the device. Simply NULL out the handle and the notify callback.
1735 	 *    The finalize callback is still available so that the consumer
1736 	 *    knows of the final disposition of the device.
1737 	 * 2. Not called in the context of notify. NULL out the handle as well
1738 	 *    as the notify and finalize callbacks. Since the consumer has
1739 	 *    closed the handle, we assume it is not interested in the
1740 	 *    notify and finalize callbacks.
1741 	 */
1742 	ldi_ev_lock();
1743 
1744 	if (handlep->lh_flags & LH_FLAGS_NOTIFY)
1745 		notify = 1;
1746 	listp = &ldi_ev_callback_list.le_head;
1747 	for (lecp = list_head(listp); lecp; lecp = list_next(listp, lecp)) {
1748 		if (lecp->lec_lhp != handlep)
1749 			continue;
1750 		lecp->lec_lhp = NULL;
1751 		lecp->lec_notify = NULL;
1752 		LDI_EVDBG((CE_NOTE, "ldi_close: NULLed lh and notify"));
1753 		if (!notify) {
1754 			LDI_EVDBG((CE_NOTE, "ldi_close: NULLed finalize"));
1755 			lecp->lec_finalize = NULL;
1756 		}
1757 	}
1758 
1759 	if (notify)
1760 		handlep->lh_flags &= ~LH_FLAGS_NOTIFY;
1761 	ldi_ev_unlock();
1762 
1763 	/*
1764 	 * Free the handle even if the device close failed.  why?
1765 	 *
1766 	 * If the device close failed we can't really make assumptions
1767 	 * about the devices state so we shouldn't allow access to the
1768 	 * device via this handle any more.  If the device consumer wants
1769 	 * to access the device again they should open it again.
1770 	 *
1771 	 * This is the same way file/device close failures are handled
1772 	 * in other places like spec_close() and closeandsetf().
1773 	 */
1774 	handle_release(handlep);
1775 	return (err);
1776 }
1777 
1778 int
1779 ldi_read(ldi_handle_t lh, struct uio *uiop, cred_t *credp)
1780 {
1781 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
1782 	vnode_t			*vp;
1783 	dev_t			dev;
1784 	int			ret;
1785 
1786 	if (lh == NULL)
1787 		return (EINVAL);
1788 
1789 	vp = handlep->lh_vp;
1790 	dev = vp->v_rdev;
1791 	if (handlep->lh_type & LH_CBDEV) {
1792 		ret = cdev_read(dev, uiop, credp);
1793 	} else if (handlep->lh_type & LH_STREAM) {
1794 		ret = strread(vp, uiop, credp);
1795 	} else {
1796 		return (ENOTSUP);
1797 	}
1798 	return (ret);
1799 }
1800 
1801 int
1802 ldi_write(ldi_handle_t lh, struct uio *uiop, cred_t *credp)
1803 {
1804 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
1805 	vnode_t			*vp;
1806 	dev_t			dev;
1807 	int			ret;
1808 
1809 	if (lh == NULL)
1810 		return (EINVAL);
1811 
1812 	vp = handlep->lh_vp;
1813 	dev = vp->v_rdev;
1814 	if (handlep->lh_type & LH_CBDEV) {
1815 		ret = cdev_write(dev, uiop, credp);
1816 	} else if (handlep->lh_type & LH_STREAM) {
1817 		ret = strwrite(vp, uiop, credp);
1818 	} else {
1819 		return (ENOTSUP);
1820 	}
1821 	return (ret);
1822 }
1823 
1824 int
1825 ldi_get_size(ldi_handle_t lh, uint64_t *sizep)
1826 {
1827 	int			otyp;
1828 	uint_t			value;
1829 	int64_t			drv_prop64;
1830 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
1831 	uint_t			blksize;
1832 	int			blkshift;
1833 
1834 
1835 	if ((lh == NULL) || (sizep == NULL))
1836 		return (DDI_FAILURE);
1837 
1838 	if (handlep->lh_type & LH_STREAM)
1839 		return (DDI_FAILURE);
1840 
1841 	/*
1842 	 * Determine device type (char or block).
1843 	 * Character devices support Size/size
1844 	 * property value. Block devices may support
1845 	 * Nblocks/nblocks or Size/size property value.
1846 	 */
1847 	if ((ldi_get_otyp(lh, &otyp)) != 0)
1848 		return (DDI_FAILURE);
1849 
1850 	if (otyp == OTYP_BLK) {
1851 		if (ldi_prop_exists(lh,
1852 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "Nblocks")) {
1853 
1854 			drv_prop64 = ldi_prop_get_int64(lh,
1855 			    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1856 			    "Nblocks", 0);
1857 			blksize = ldi_prop_get_int(lh,
1858 			    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1859 			    "blksize", DEV_BSIZE);
1860 			if (blksize == DEV_BSIZE)
1861 				blksize = ldi_prop_get_int(lh, LDI_DEV_T_ANY |
1862 				    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1863 				    "device-blksize", DEV_BSIZE);
1864 
1865 			/* blksize must be a power of two */
1866 			ASSERT(BIT_ONLYONESET(blksize));
1867 			blkshift = highbit(blksize) - 1;
1868 
1869 			/*
1870 			 * We don't support Nblocks values that don't have
1871 			 * an accurate uint64_t byte count representation.
1872 			 */
1873 			if ((uint64_t)drv_prop64 >= (UINT64_MAX >> blkshift))
1874 				return (DDI_FAILURE);
1875 
1876 			*sizep = (uint64_t)
1877 			    (((u_offset_t)drv_prop64) << blkshift);
1878 			return (DDI_SUCCESS);
1879 		}
1880 
1881 		if (ldi_prop_exists(lh,
1882 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "nblocks")) {
1883 
1884 			value = ldi_prop_get_int(lh,
1885 			    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1886 			    "nblocks", 0);
1887 			blksize = ldi_prop_get_int(lh,
1888 			    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1889 			    "blksize", DEV_BSIZE);
1890 			if (blksize == DEV_BSIZE)
1891 				blksize = ldi_prop_get_int(lh, LDI_DEV_T_ANY |
1892 				    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1893 				    "device-blksize", DEV_BSIZE);
1894 
1895 			/* blksize must be a power of two */
1896 			ASSERT(BIT_ONLYONESET(blksize));
1897 			blkshift = highbit(blksize) - 1;
1898 
1899 			/*
1900 			 * We don't support nblocks values that don't have an
1901 			 * accurate uint64_t byte count representation.
1902 			 */
1903 			if ((uint64_t)value >= (UINT64_MAX >> blkshift))
1904 				return (DDI_FAILURE);
1905 
1906 			*sizep = (uint64_t)
1907 			    (((u_offset_t)value) << blkshift);
1908 			return (DDI_SUCCESS);
1909 		}
1910 	}
1911 
1912 	if (ldi_prop_exists(lh,
1913 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "Size")) {
1914 
1915 		drv_prop64 = ldi_prop_get_int64(lh,
1916 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "Size", 0);
1917 		*sizep = (uint64_t)drv_prop64;
1918 		return (DDI_SUCCESS);
1919 	}
1920 
1921 	if (ldi_prop_exists(lh,
1922 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "size")) {
1923 
1924 		value = ldi_prop_get_int(lh,
1925 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "size", 0);
1926 		*sizep = (uint64_t)value;
1927 		return (DDI_SUCCESS);
1928 	}
1929 
1930 	/* unable to determine device size */
1931 	return (DDI_FAILURE);
1932 }
1933 
1934 int
1935 ldi_ioctl(ldi_handle_t lh, int cmd, intptr_t arg, int mode,
1936     cred_t *cr, int *rvalp)
1937 {
1938 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
1939 	vnode_t			*vp;
1940 	dev_t			dev;
1941 	int			ret, copymode, unused;
1942 
1943 	if (lh == NULL)
1944 		return (EINVAL);
1945 
1946 	/*
1947 	 * if the data pointed to by arg is located in the kernel then
1948 	 * make sure the FNATIVE flag is set.
1949 	 */
1950 	if (mode & FKIOCTL)
1951 		mode = (mode & ~FMODELS) | FNATIVE | FKIOCTL;
1952 
1953 	/*
1954 	 * Some drivers assume that rvalp will always be non-NULL, so in
1955 	 * an attempt to avoid panics if the caller passed in a NULL
1956 	 * value, update rvalp to point to a temporary variable.
1957 	 */
1958 	if (rvalp == NULL)
1959 		rvalp = &unused;
1960 	vp = handlep->lh_vp;
1961 	dev = vp->v_rdev;
1962 	if (handlep->lh_type & LH_CBDEV) {
1963 		ret = cdev_ioctl(dev, cmd, arg, mode, cr, rvalp);
1964 	} else if (handlep->lh_type & LH_STREAM) {
1965 		copymode = (mode & FKIOCTL) ? K_TO_K : U_TO_K;
1966 
1967 		/*
1968 		 * if we get an I_PLINK from within the kernel the
1969 		 * arg is a layered handle pointer instead of
1970 		 * a file descriptor, so we translate this ioctl
1971 		 * into a private one that can handle this.
1972 		 */
1973 		if ((mode & FKIOCTL) && (cmd == I_PLINK))
1974 			cmd = _I_PLINK_LH;
1975 
1976 		ret = strioctl(vp, cmd, arg, mode, copymode, cr, rvalp);
1977 	} else {
1978 		return (ENOTSUP);
1979 	}
1980 
1981 	return (ret);
1982 }
1983 
1984 int
1985 ldi_poll(ldi_handle_t lh, short events, int anyyet, short *reventsp,
1986     struct pollhead **phpp)
1987 {
1988 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
1989 	vnode_t			*vp;
1990 	dev_t			dev;
1991 	int			ret;
1992 
1993 	if (lh == NULL)
1994 		return (EINVAL);
1995 
1996 	vp = handlep->lh_vp;
1997 	dev = vp->v_rdev;
1998 	if (handlep->lh_type & LH_CBDEV) {
1999 		ret = cdev_poll(dev, events, anyyet, reventsp, phpp);
2000 	} else if (handlep->lh_type & LH_STREAM) {
2001 		ret = strpoll(vp->v_stream, events, anyyet, reventsp, phpp);
2002 	} else {
2003 		return (ENOTSUP);
2004 	}
2005 
2006 	return (ret);
2007 }
2008 
2009 int
2010 ldi_prop_op(ldi_handle_t lh, ddi_prop_op_t prop_op,
2011     int flags, char *name, caddr_t valuep, int *length)
2012 {
2013 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2014 	dev_t			dev;
2015 	dev_info_t		*dip;
2016 	int			ret;
2017 	struct snode		*csp;
2018 
2019 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
2020 		return (DDI_PROP_INVAL_ARG);
2021 
2022 	if ((prop_op != PROP_LEN) && (valuep == NULL))
2023 		return (DDI_PROP_INVAL_ARG);
2024 
2025 	if (length == NULL)
2026 		return (DDI_PROP_INVAL_ARG);
2027 
2028 	/*
2029 	 * try to find the associated dip,
2030 	 * this places a hold on the driver
2031 	 */
2032 	dev = handlep->lh_vp->v_rdev;
2033 
2034 	csp = VTOCS(handlep->lh_vp);
2035 	mutex_enter(&csp->s_lock);
2036 	if ((dip = csp->s_dip) != NULL)
2037 		e_ddi_hold_devi(dip);
2038 	mutex_exit(&csp->s_lock);
2039 	if (dip == NULL)
2040 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2041 
2042 	if (dip == NULL)
2043 		return (DDI_PROP_NOT_FOUND);
2044 
2045 	ret = i_ldi_prop_op(dev, dip, prop_op, flags, name, valuep, length);
2046 	ddi_release_devi(dip);
2047 
2048 	return (ret);
2049 }
2050 
2051 int
2052 ldi_strategy(ldi_handle_t lh, struct buf *bp)
2053 {
2054 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2055 	dev_t			dev;
2056 
2057 	if ((lh == NULL) || (bp == NULL))
2058 		return (EINVAL);
2059 
2060 	/* this entry point is only supported for cb devices */
2061 	dev = handlep->lh_vp->v_rdev;
2062 	if (!(handlep->lh_type & LH_CBDEV))
2063 		return (ENOTSUP);
2064 
2065 	bp->b_edev = dev;
2066 	bp->b_dev = cmpdev(dev);
2067 	return (bdev_strategy(bp));
2068 }
2069 
2070 int
2071 ldi_dump(ldi_handle_t lh, caddr_t addr, daddr_t blkno, int nblk)
2072 {
2073 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2074 	dev_t			dev;
2075 
2076 	if (lh == NULL)
2077 		return (EINVAL);
2078 
2079 	/* this entry point is only supported for cb devices */
2080 	dev = handlep->lh_vp->v_rdev;
2081 	if (!(handlep->lh_type & LH_CBDEV))
2082 		return (ENOTSUP);
2083 
2084 	return (bdev_dump(dev, addr, blkno, nblk));
2085 }
2086 
2087 int
2088 ldi_devmap(ldi_handle_t lh, devmap_cookie_t dhp, offset_t off,
2089     size_t len, size_t *maplen, uint_t model)
2090 {
2091 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2092 	dev_t			dev;
2093 
2094 	if (lh == NULL)
2095 		return (EINVAL);
2096 
2097 	/* this entry point is only supported for cb devices */
2098 	dev = handlep->lh_vp->v_rdev;
2099 	if (!(handlep->lh_type & LH_CBDEV))
2100 		return (ENOTSUP);
2101 
2102 	return (cdev_devmap(dev, dhp, off, len, maplen, model));
2103 }
2104 
2105 int
2106 ldi_aread(ldi_handle_t lh, struct aio_req *aio_reqp, cred_t *cr)
2107 {
2108 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2109 	dev_t			dev;
2110 	struct cb_ops		*cb;
2111 
2112 	if (lh == NULL)
2113 		return (EINVAL);
2114 
2115 	/* this entry point is only supported for cb devices */
2116 	if (!(handlep->lh_type & LH_CBDEV))
2117 		return (ENOTSUP);
2118 
2119 	/*
2120 	 * Kaio is only supported on block devices.
2121 	 */
2122 	dev = handlep->lh_vp->v_rdev;
2123 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
2124 	if (cb->cb_strategy == nodev || cb->cb_strategy == NULL)
2125 		return (ENOTSUP);
2126 
2127 	if (cb->cb_aread == NULL)
2128 		return (ENOTSUP);
2129 
2130 	return (cb->cb_aread(dev, aio_reqp, cr));
2131 }
2132 
2133 int
2134 ldi_awrite(ldi_handle_t lh, struct aio_req *aio_reqp, cred_t *cr)
2135 {
2136 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2137 	struct cb_ops		*cb;
2138 	dev_t			dev;
2139 
2140 	if (lh == NULL)
2141 		return (EINVAL);
2142 
2143 	/* this entry point is only supported for cb devices */
2144 	if (!(handlep->lh_type & LH_CBDEV))
2145 		return (ENOTSUP);
2146 
2147 	/*
2148 	 * Kaio is only supported on block devices.
2149 	 */
2150 	dev = handlep->lh_vp->v_rdev;
2151 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
2152 	if (cb->cb_strategy == nodev || cb->cb_strategy == NULL)
2153 		return (ENOTSUP);
2154 
2155 	if (cb->cb_awrite == NULL)
2156 		return (ENOTSUP);
2157 
2158 	return (cb->cb_awrite(dev, aio_reqp, cr));
2159 }
2160 
2161 int
2162 ldi_putmsg(ldi_handle_t lh, mblk_t *smp)
2163 {
2164 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2165 	int			ret;
2166 
2167 	if ((lh == NULL) || (smp == NULL))
2168 		return (EINVAL);
2169 
2170 	if (!(handlep->lh_type & LH_STREAM)) {
2171 		freemsg(smp);
2172 		return (ENOTSUP);
2173 	}
2174 
2175 	/*
2176 	 * If we don't have db_credp, set it. Note that we can not be called
2177 	 * from interrupt context.
2178 	 */
2179 	if (msg_getcred(smp, NULL) == NULL)
2180 		mblk_setcred(smp, CRED(), curproc->p_pid);
2181 
2182 	/* Send message while honoring flow control */
2183 	ret = kstrputmsg(handlep->lh_vp, smp, NULL, 0, 0,
2184 	    MSG_BAND | MSG_HOLDSIG | MSG_IGNERROR, 0);
2185 
2186 	return (ret);
2187 }
2188 
2189 int
2190 ldi_getmsg(ldi_handle_t lh, mblk_t **rmp, timestruc_t *timeo)
2191 {
2192 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2193 	clock_t			timout; /* milliseconds */
2194 	uchar_t			pri;
2195 	rval_t			rval;
2196 	int			ret, pflag;
2197 
2198 
2199 	if (lh == NULL)
2200 		return (EINVAL);
2201 
2202 	if (!(handlep->lh_type & LH_STREAM))
2203 		return (ENOTSUP);
2204 
2205 	/* Convert from nanoseconds to milliseconds */
2206 	if (timeo != NULL) {
2207 		timout = timeo->tv_sec * 1000 + timeo->tv_nsec / 1000000;
2208 		if (timout > INT_MAX)
2209 			return (EINVAL);
2210 	} else
2211 		timout = -1;
2212 
2213 	/* Wait for timeout millseconds for a message */
2214 	pflag = MSG_ANY;
2215 	pri = 0;
2216 	*rmp = NULL;
2217 	ret = kstrgetmsg(handlep->lh_vp,
2218 	    rmp, NULL, &pri, &pflag, timout, &rval);
2219 	return (ret);
2220 }
2221 
2222 int
2223 ldi_get_dev(ldi_handle_t lh, dev_t *devp)
2224 {
2225 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2226 
2227 	if ((lh == NULL) || (devp == NULL))
2228 		return (EINVAL);
2229 
2230 	*devp = handlep->lh_vp->v_rdev;
2231 	return (0);
2232 }
2233 
2234 int
2235 ldi_get_otyp(ldi_handle_t lh, int *otyp)
2236 {
2237 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2238 
2239 	if ((lh == NULL) || (otyp == NULL))
2240 		return (EINVAL);
2241 
2242 	*otyp = VTYP_TO_OTYP(handlep->lh_vp->v_type);
2243 	return (0);
2244 }
2245 
2246 int
2247 ldi_get_devid(ldi_handle_t lh, ddi_devid_t *devid)
2248 {
2249 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2250 	int			ret;
2251 	dev_t			dev;
2252 
2253 	if ((lh == NULL) || (devid == NULL))
2254 		return (EINVAL);
2255 
2256 	dev = handlep->lh_vp->v_rdev;
2257 
2258 	ret = ddi_lyr_get_devid(dev, devid);
2259 	if (ret != DDI_SUCCESS)
2260 		return (ENOTSUP);
2261 
2262 	return (0);
2263 }
2264 
2265 int
2266 ldi_get_minor_name(ldi_handle_t lh, char **minor_name)
2267 {
2268 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2269 	int			ret, otyp;
2270 	dev_t			dev;
2271 
2272 	if ((lh == NULL) || (minor_name == NULL))
2273 		return (EINVAL);
2274 
2275 	dev = handlep->lh_vp->v_rdev;
2276 	otyp = VTYP_TO_OTYP(handlep->lh_vp->v_type);
2277 
2278 	ret = ddi_lyr_get_minor_name(dev, OTYP_TO_STYP(otyp), minor_name);
2279 	if (ret != DDI_SUCCESS)
2280 		return (ENOTSUP);
2281 
2282 	return (0);
2283 }
2284 
2285 int
2286 ldi_prop_lookup_int_array(ldi_handle_t lh,
2287     uint_t flags, char *name, int **data, uint_t *nelements)
2288 {
2289 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2290 	dev_info_t		*dip;
2291 	dev_t			dev;
2292 	int			res;
2293 	struct snode		*csp;
2294 
2295 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
2296 		return (DDI_PROP_INVAL_ARG);
2297 
2298 	dev = handlep->lh_vp->v_rdev;
2299 
2300 	csp = VTOCS(handlep->lh_vp);
2301 	mutex_enter(&csp->s_lock);
2302 	if ((dip = csp->s_dip) != NULL)
2303 		e_ddi_hold_devi(dip);
2304 	mutex_exit(&csp->s_lock);
2305 	if (dip == NULL)
2306 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2307 
2308 	if (dip == NULL) {
2309 		flags |= DDI_UNBND_DLPI2;
2310 	} else if (flags & LDI_DEV_T_ANY) {
2311 		flags &= ~LDI_DEV_T_ANY;
2312 		dev = DDI_DEV_T_ANY;
2313 	}
2314 
2315 	if (dip != NULL) {
2316 		int *prop_val, prop_len;
2317 
2318 		res = i_ldi_prop_op_typed(dev, dip, flags, name,
2319 		    (caddr_t *)&prop_val, &prop_len, sizeof (int));
2320 
2321 		/* if we got it then return it */
2322 		if (res == DDI_PROP_SUCCESS) {
2323 			*nelements = prop_len / sizeof (int);
2324 			*data = prop_val;
2325 
2326 			ddi_release_devi(dip);
2327 			return (res);
2328 		}
2329 	}
2330 
2331 	/* call the normal property interfaces */
2332 	res = ddi_prop_lookup_int_array(dev, dip, flags,
2333 	    name, data, nelements);
2334 
2335 	if (dip != NULL)
2336 		ddi_release_devi(dip);
2337 
2338 	return (res);
2339 }
2340 
2341 int
2342 ldi_prop_lookup_int64_array(ldi_handle_t lh,
2343     uint_t flags, char *name, int64_t **data, uint_t *nelements)
2344 {
2345 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2346 	dev_info_t		*dip;
2347 	dev_t			dev;
2348 	int			res;
2349 	struct snode		*csp;
2350 
2351 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
2352 		return (DDI_PROP_INVAL_ARG);
2353 
2354 	dev = handlep->lh_vp->v_rdev;
2355 
2356 	csp = VTOCS(handlep->lh_vp);
2357 	mutex_enter(&csp->s_lock);
2358 	if ((dip = csp->s_dip) != NULL)
2359 		e_ddi_hold_devi(dip);
2360 	mutex_exit(&csp->s_lock);
2361 	if (dip == NULL)
2362 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2363 
2364 	if (dip == NULL) {
2365 		flags |= DDI_UNBND_DLPI2;
2366 	} else if (flags & LDI_DEV_T_ANY) {
2367 		flags &= ~LDI_DEV_T_ANY;
2368 		dev = DDI_DEV_T_ANY;
2369 	}
2370 
2371 	if (dip != NULL) {
2372 		int64_t	*prop_val;
2373 		int	prop_len;
2374 
2375 		res = i_ldi_prop_op_typed(dev, dip, flags, name,
2376 		    (caddr_t *)&prop_val, &prop_len, sizeof (int64_t));
2377 
2378 		/* if we got it then return it */
2379 		if (res == DDI_PROP_SUCCESS) {
2380 			*nelements = prop_len / sizeof (int64_t);
2381 			*data = prop_val;
2382 
2383 			ddi_release_devi(dip);
2384 			return (res);
2385 		}
2386 	}
2387 
2388 	/* call the normal property interfaces */
2389 	res = ddi_prop_lookup_int64_array(dev, dip, flags,
2390 	    name, data, nelements);
2391 
2392 	if (dip != NULL)
2393 		ddi_release_devi(dip);
2394 
2395 	return (res);
2396 }
2397 
2398 int
2399 ldi_prop_lookup_string_array(ldi_handle_t lh,
2400     uint_t flags, char *name, char ***data, uint_t *nelements)
2401 {
2402 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2403 	dev_info_t		*dip;
2404 	dev_t			dev;
2405 	int			res;
2406 	struct snode		*csp;
2407 
2408 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
2409 		return (DDI_PROP_INVAL_ARG);
2410 
2411 	dev = handlep->lh_vp->v_rdev;
2412 
2413 	csp = VTOCS(handlep->lh_vp);
2414 	mutex_enter(&csp->s_lock);
2415 	if ((dip = csp->s_dip) != NULL)
2416 		e_ddi_hold_devi(dip);
2417 	mutex_exit(&csp->s_lock);
2418 	if (dip == NULL)
2419 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2420 
2421 	if (dip == NULL) {
2422 		flags |= DDI_UNBND_DLPI2;
2423 	} else if (flags & LDI_DEV_T_ANY) {
2424 		flags &= ~LDI_DEV_T_ANY;
2425 		dev = DDI_DEV_T_ANY;
2426 	}
2427 
2428 	if (dip != NULL) {
2429 		char	*prop_val;
2430 		int	prop_len;
2431 
2432 		res = i_ldi_prop_op_typed(dev, dip, flags, name,
2433 		    (caddr_t *)&prop_val, &prop_len, 0);
2434 
2435 		/* if we got it then return it */
2436 		if (res == DDI_PROP_SUCCESS) {
2437 			char	**str_array;
2438 			int	nelem;
2439 
2440 			/*
2441 			 * pack the returned string array into the format
2442 			 * our callers expect
2443 			 */
2444 			if (i_pack_string_array(prop_val, prop_len,
2445 			    &str_array, &nelem) == 0) {
2446 
2447 				*data = str_array;
2448 				*nelements = nelem;
2449 
2450 				ddi_prop_free(prop_val);
2451 				ddi_release_devi(dip);
2452 				return (res);
2453 			}
2454 
2455 			/*
2456 			 * the format of the returned property must have
2457 			 * been bad so throw it out
2458 			 */
2459 			ddi_prop_free(prop_val);
2460 		}
2461 	}
2462 
2463 	/* call the normal property interfaces */
2464 	res = ddi_prop_lookup_string_array(dev, dip, flags,
2465 	    name, data, nelements);
2466 
2467 	if (dip != NULL)
2468 		ddi_release_devi(dip);
2469 
2470 	return (res);
2471 }
2472 
2473 int
2474 ldi_prop_lookup_string(ldi_handle_t lh,
2475     uint_t flags, char *name, char **data)
2476 {
2477 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2478 	dev_info_t		*dip;
2479 	dev_t			dev;
2480 	int			res;
2481 	struct snode		*csp;
2482 
2483 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
2484 		return (DDI_PROP_INVAL_ARG);
2485 
2486 	dev = handlep->lh_vp->v_rdev;
2487 
2488 	csp = VTOCS(handlep->lh_vp);
2489 	mutex_enter(&csp->s_lock);
2490 	if ((dip = csp->s_dip) != NULL)
2491 		e_ddi_hold_devi(dip);
2492 	mutex_exit(&csp->s_lock);
2493 	if (dip == NULL)
2494 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2495 
2496 	if (dip == NULL) {
2497 		flags |= DDI_UNBND_DLPI2;
2498 	} else if (flags & LDI_DEV_T_ANY) {
2499 		flags &= ~LDI_DEV_T_ANY;
2500 		dev = DDI_DEV_T_ANY;
2501 	}
2502 
2503 	if (dip != NULL) {
2504 		char	*prop_val;
2505 		int	prop_len;
2506 
2507 		res = i_ldi_prop_op_typed(dev, dip, flags, name,
2508 		    (caddr_t *)&prop_val, &prop_len, 0);
2509 
2510 		/* if we got it then return it */
2511 		if (res == DDI_PROP_SUCCESS) {
2512 			/*
2513 			 * sanity check the vaule returned.
2514 			 */
2515 			if (i_check_string(prop_val, prop_len)) {
2516 				ddi_prop_free(prop_val);
2517 			} else {
2518 				*data = prop_val;
2519 				ddi_release_devi(dip);
2520 				return (res);
2521 			}
2522 		}
2523 	}
2524 
2525 	/* call the normal property interfaces */
2526 	res = ddi_prop_lookup_string(dev, dip, flags, name, data);
2527 
2528 	if (dip != NULL)
2529 		ddi_release_devi(dip);
2530 
2531 #ifdef DEBUG
2532 	if (res == DDI_PROP_SUCCESS) {
2533 		/*
2534 		 * keep ourselves honest
2535 		 * make sure the framework returns strings in the
2536 		 * same format as we're demanding from drivers.
2537 		 */
2538 		struct prop_driver_data	*pdd;
2539 		int			pdd_prop_size;
2540 
2541 		pdd = ((struct prop_driver_data *)(*data)) - 1;
2542 		pdd_prop_size = pdd->pdd_size -
2543 		    sizeof (struct prop_driver_data);
2544 		ASSERT(i_check_string(*data, pdd_prop_size) == 0);
2545 	}
2546 #endif /* DEBUG */
2547 
2548 	return (res);
2549 }
2550 
2551 int
2552 ldi_prop_lookup_byte_array(ldi_handle_t lh,
2553     uint_t flags, char *name, uchar_t **data, uint_t *nelements)
2554 {
2555 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2556 	dev_info_t		*dip;
2557 	dev_t			dev;
2558 	int			res;
2559 	struct snode		*csp;
2560 
2561 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
2562 		return (DDI_PROP_INVAL_ARG);
2563 
2564 	dev = handlep->lh_vp->v_rdev;
2565 
2566 	csp = VTOCS(handlep->lh_vp);
2567 	mutex_enter(&csp->s_lock);
2568 	if ((dip = csp->s_dip) != NULL)
2569 		e_ddi_hold_devi(dip);
2570 	mutex_exit(&csp->s_lock);
2571 	if (dip == NULL)
2572 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2573 
2574 	if (dip == NULL) {
2575 		flags |= DDI_UNBND_DLPI2;
2576 	} else if (flags & LDI_DEV_T_ANY) {
2577 		flags &= ~LDI_DEV_T_ANY;
2578 		dev = DDI_DEV_T_ANY;
2579 	}
2580 
2581 	if (dip != NULL) {
2582 		uchar_t	*prop_val;
2583 		int	prop_len;
2584 
2585 		res = i_ldi_prop_op_typed(dev, dip, flags, name,
2586 		    (caddr_t *)&prop_val, &prop_len, sizeof (uchar_t));
2587 
2588 		/* if we got it then return it */
2589 		if (res == DDI_PROP_SUCCESS) {
2590 			*nelements = prop_len / sizeof (uchar_t);
2591 			*data = prop_val;
2592 
2593 			ddi_release_devi(dip);
2594 			return (res);
2595 		}
2596 	}
2597 
2598 	/* call the normal property interfaces */
2599 	res = ddi_prop_lookup_byte_array(dev, dip, flags,
2600 	    name, data, nelements);
2601 
2602 	if (dip != NULL)
2603 		ddi_release_devi(dip);
2604 
2605 	return (res);
2606 }
2607 
2608 int
2609 ldi_prop_get_int(ldi_handle_t lh,
2610     uint_t flags, char *name, int defvalue)
2611 {
2612 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2613 	dev_info_t		*dip;
2614 	dev_t			dev;
2615 	int			res;
2616 	struct snode		*csp;
2617 
2618 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
2619 		return (defvalue);
2620 
2621 	dev = handlep->lh_vp->v_rdev;
2622 
2623 	csp = VTOCS(handlep->lh_vp);
2624 	mutex_enter(&csp->s_lock);
2625 	if ((dip = csp->s_dip) != NULL)
2626 		e_ddi_hold_devi(dip);
2627 	mutex_exit(&csp->s_lock);
2628 	if (dip == NULL)
2629 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2630 
2631 	if (dip == NULL) {
2632 		flags |= DDI_UNBND_DLPI2;
2633 	} else if (flags & LDI_DEV_T_ANY) {
2634 		flags &= ~LDI_DEV_T_ANY;
2635 		dev = DDI_DEV_T_ANY;
2636 	}
2637 
2638 	if (dip != NULL) {
2639 		int	prop_val;
2640 		int	prop_len;
2641 
2642 		/*
2643 		 * first call the drivers prop_op interface to allow it
2644 		 * it to override default property values.
2645 		 */
2646 		prop_len = sizeof (int);
2647 		res = i_ldi_prop_op(dev, dip, PROP_LEN_AND_VAL_BUF,
2648 		    flags | DDI_PROP_DYNAMIC, name,
2649 		    (caddr_t)&prop_val, &prop_len);
2650 
2651 		/* if we got it then return it */
2652 		if ((res == DDI_PROP_SUCCESS) &&
2653 		    (prop_len == sizeof (int))) {
2654 			res = prop_val;
2655 			ddi_release_devi(dip);
2656 			return (res);
2657 		}
2658 	}
2659 
2660 	/* call the normal property interfaces */
2661 	res = ddi_prop_get_int(dev, dip, flags, name, defvalue);
2662 
2663 	if (dip != NULL)
2664 		ddi_release_devi(dip);
2665 
2666 	return (res);
2667 }
2668 
2669 int64_t
2670 ldi_prop_get_int64(ldi_handle_t lh,
2671     uint_t flags, char *name, int64_t defvalue)
2672 {
2673 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2674 	dev_info_t		*dip;
2675 	dev_t			dev;
2676 	int64_t			res;
2677 	struct snode		*csp;
2678 
2679 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
2680 		return (defvalue);
2681 
2682 	dev = handlep->lh_vp->v_rdev;
2683 
2684 	csp = VTOCS(handlep->lh_vp);
2685 	mutex_enter(&csp->s_lock);
2686 	if ((dip = csp->s_dip) != NULL)
2687 		e_ddi_hold_devi(dip);
2688 	mutex_exit(&csp->s_lock);
2689 	if (dip == NULL)
2690 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2691 
2692 	if (dip == NULL) {
2693 		flags |= DDI_UNBND_DLPI2;
2694 	} else if (flags & LDI_DEV_T_ANY) {
2695 		flags &= ~LDI_DEV_T_ANY;
2696 		dev = DDI_DEV_T_ANY;
2697 	}
2698 
2699 	if (dip != NULL) {
2700 		int64_t	prop_val;
2701 		int	prop_len;
2702 
2703 		/*
2704 		 * first call the drivers prop_op interface to allow it
2705 		 * it to override default property values.
2706 		 */
2707 		prop_len = sizeof (int64_t);
2708 		res = i_ldi_prop_op(dev, dip, PROP_LEN_AND_VAL_BUF,
2709 		    flags | DDI_PROP_DYNAMIC, name,
2710 		    (caddr_t)&prop_val, &prop_len);
2711 
2712 		/* if we got it then return it */
2713 		if ((res == DDI_PROP_SUCCESS) &&
2714 		    (prop_len == sizeof (int64_t))) {
2715 			res = prop_val;
2716 			ddi_release_devi(dip);
2717 			return (res);
2718 		}
2719 	}
2720 
2721 	/* call the normal property interfaces */
2722 	res = ddi_prop_get_int64(dev, dip, flags, name, defvalue);
2723 
2724 	if (dip != NULL)
2725 		ddi_release_devi(dip);
2726 
2727 	return (res);
2728 }
2729 
2730 int
2731 ldi_prop_exists(ldi_handle_t lh, uint_t flags, char *name)
2732 {
2733 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2734 	dev_info_t		*dip;
2735 	dev_t			dev;
2736 	int			res, prop_len;
2737 	struct snode		*csp;
2738 
2739 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
2740 		return (0);
2741 
2742 	dev = handlep->lh_vp->v_rdev;
2743 
2744 	csp = VTOCS(handlep->lh_vp);
2745 	mutex_enter(&csp->s_lock);
2746 	if ((dip = csp->s_dip) != NULL)
2747 		e_ddi_hold_devi(dip);
2748 	mutex_exit(&csp->s_lock);
2749 	if (dip == NULL)
2750 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2751 
2752 	/* if NULL dip, prop does NOT exist */
2753 	if (dip == NULL)
2754 		return (0);
2755 
2756 	if (flags & LDI_DEV_T_ANY) {
2757 		flags &= ~LDI_DEV_T_ANY;
2758 		dev = DDI_DEV_T_ANY;
2759 	}
2760 
2761 	/*
2762 	 * first call the drivers prop_op interface to allow it
2763 	 * it to override default property values.
2764 	 */
2765 	res = i_ldi_prop_op(dev, dip, PROP_LEN,
2766 	    flags | DDI_PROP_DYNAMIC, name, NULL, &prop_len);
2767 
2768 	if (res == DDI_PROP_SUCCESS) {
2769 		ddi_release_devi(dip);
2770 		return (1);
2771 	}
2772 
2773 	/* call the normal property interfaces */
2774 	res = ddi_prop_exists(dev, dip, flags, name);
2775 
2776 	ddi_release_devi(dip);
2777 	return (res);
2778 }
2779 
2780 #ifdef	LDI_OBSOLETE_EVENT
2781 
2782 int
2783 ldi_get_eventcookie(ldi_handle_t lh, char *name, ddi_eventcookie_t *ecp)
2784 {
2785 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2786 	dev_info_t		*dip;
2787 	dev_t			dev;
2788 	int			res;
2789 	struct snode		*csp;
2790 
2791 	if ((lh == NULL) || (name == NULL) ||
2792 	    (strlen(name) == 0) || (ecp == NULL)) {
2793 		return (DDI_FAILURE);
2794 	}
2795 
2796 	ASSERT(!servicing_interrupt());
2797 
2798 	dev = handlep->lh_vp->v_rdev;
2799 
2800 	csp = VTOCS(handlep->lh_vp);
2801 	mutex_enter(&csp->s_lock);
2802 	if ((dip = csp->s_dip) != NULL)
2803 		e_ddi_hold_devi(dip);
2804 	mutex_exit(&csp->s_lock);
2805 	if (dip == NULL)
2806 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2807 
2808 	if (dip == NULL)
2809 		return (DDI_FAILURE);
2810 
2811 	LDI_EVENTCB((CE_NOTE, "%s: event_name=%s, "
2812 	    "dip=0x%p, event_cookiep=0x%p", "ldi_get_eventcookie",
2813 	    name, (void *)dip, (void *)ecp));
2814 
2815 	res = ddi_get_eventcookie(dip, name, ecp);
2816 
2817 	ddi_release_devi(dip);
2818 	return (res);
2819 }
2820 
2821 int
2822 ldi_add_event_handler(ldi_handle_t lh, ddi_eventcookie_t ec,
2823     void (*handler)(ldi_handle_t, ddi_eventcookie_t, void *, void *),
2824     void *arg, ldi_callback_id_t *id)
2825 {
2826 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2827 	struct ldi_event	*lep;
2828 	dev_info_t		*dip;
2829 	dev_t			dev;
2830 	int			res;
2831 	struct snode		*csp;
2832 
2833 	if ((lh == NULL) || (ec == NULL) || (handler == NULL) || (id == NULL))
2834 		return (DDI_FAILURE);
2835 
2836 	ASSERT(!servicing_interrupt());
2837 
2838 	dev = handlep->lh_vp->v_rdev;
2839 
2840 	csp = VTOCS(handlep->lh_vp);
2841 	mutex_enter(&csp->s_lock);
2842 	if ((dip = csp->s_dip) != NULL)
2843 		e_ddi_hold_devi(dip);
2844 	mutex_exit(&csp->s_lock);
2845 	if (dip == NULL)
2846 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2847 
2848 	if (dip == NULL)
2849 		return (DDI_FAILURE);
2850 
2851 	lep = kmem_zalloc(sizeof (struct ldi_event), KM_SLEEP);
2852 	lep->le_lhp = handlep;
2853 	lep->le_arg = arg;
2854 	lep->le_handler = handler;
2855 
2856 	if ((res = ddi_add_event_handler(dip, ec, i_ldi_callback,
2857 	    (void *)lep, &lep->le_id)) != DDI_SUCCESS) {
2858 		LDI_EVENTCB((CE_WARN, "%s: unable to add"
2859 		    "event callback", "ldi_add_event_handler"));
2860 		ddi_release_devi(dip);
2861 		kmem_free(lep, sizeof (struct ldi_event));
2862 		return (res);
2863 	}
2864 
2865 	*id = (ldi_callback_id_t)lep;
2866 
2867 	LDI_EVENTCB((CE_NOTE, "%s: dip=0x%p, event=0x%p, "
2868 	    "ldi_eventp=0x%p, cb_id=0x%p", "ldi_add_event_handler",
2869 	    (void *)dip, (void *)ec, (void *)lep, (void *)id));
2870 
2871 	handle_event_add(lep);
2872 	ddi_release_devi(dip);
2873 	return (res);
2874 }
2875 
2876 int
2877 ldi_remove_event_handler(ldi_handle_t lh, ldi_callback_id_t id)
2878 {
2879 	ldi_event_t		*lep = (ldi_event_t *)id;
2880 	int			res;
2881 
2882 	if ((lh == NULL) || (id == NULL))
2883 		return (DDI_FAILURE);
2884 
2885 	ASSERT(!servicing_interrupt());
2886 
2887 	if ((res = ddi_remove_event_handler(lep->le_id))
2888 	    != DDI_SUCCESS) {
2889 		LDI_EVENTCB((CE_WARN, "%s: unable to remove "
2890 		    "event callback", "ldi_remove_event_handler"));
2891 		return (res);
2892 	}
2893 
2894 	handle_event_remove(lep);
2895 	kmem_free(lep, sizeof (struct ldi_event));
2896 	return (res);
2897 }
2898 
2899 #endif
2900 
2901 /*
2902  * Here are some definitions of terms used in the following LDI events
2903  * code:
2904  *
2905  * "LDI events" AKA "native events": These are events defined by the
2906  * "new" LDI event framework. These events are serviced by the LDI event
2907  * framework itself and thus are native to it.
2908  *
2909  * "LDI contract events": These are contract events that correspond to the
2910  *  LDI events. This mapping of LDI events to contract events is defined by
2911  * the ldi_ev_cookies[] array above.
2912  *
2913  * NDI events: These are events which are serviced by the NDI event subsystem.
2914  * LDI subsystem just provides a thin wrapper around the NDI event interfaces
2915  * These events are therefore *not* native events.
2916  */
2917 
2918 static int
2919 ldi_native_event(const char *evname)
2920 {
2921 	int i;
2922 
2923 	LDI_EVTRC((CE_NOTE, "ldi_native_event: entered: ev=%s", evname));
2924 
2925 	for (i = 0; ldi_ev_cookies[i].ck_evname != NULL; i++) {
2926 		if (strcmp(ldi_ev_cookies[i].ck_evname, evname) == 0)
2927 			return (1);
2928 	}
2929 
2930 	return (0);
2931 }
2932 
2933 static uint_t
2934 ldi_ev_sync_event(const char *evname)
2935 {
2936 	int i;
2937 
2938 	ASSERT(ldi_native_event(evname));
2939 
2940 	LDI_EVTRC((CE_NOTE, "ldi_ev_sync_event: entered: %s", evname));
2941 
2942 	for (i = 0; ldi_ev_cookies[i].ck_evname != NULL; i++) {
2943 		if (strcmp(ldi_ev_cookies[i].ck_evname, evname) == 0)
2944 			return (ldi_ev_cookies[i].ck_sync);
2945 	}
2946 
2947 	/*
2948 	 * This should never happen until non-contract based
2949 	 * LDI events are introduced. If that happens, we will
2950 	 * use a "special" token to indicate that there are no
2951 	 * contracts corresponding to this LDI event.
2952 	 */
2953 	cmn_err(CE_PANIC, "Unknown LDI event: %s", evname);
2954 
2955 	return (0);
2956 }
2957 
2958 static uint_t
2959 ldi_contract_event(const char *evname)
2960 {
2961 	int i;
2962 
2963 	ASSERT(ldi_native_event(evname));
2964 
2965 	LDI_EVTRC((CE_NOTE, "ldi_contract_event: entered: %s", evname));
2966 
2967 	for (i = 0; ldi_ev_cookies[i].ck_evname != NULL; i++) {
2968 		if (strcmp(ldi_ev_cookies[i].ck_evname, evname) == 0)
2969 			return (ldi_ev_cookies[i].ck_ctype);
2970 	}
2971 
2972 	/*
2973 	 * This should never happen until non-contract based
2974 	 * LDI events are introduced. If that happens, we will
2975 	 * use a "special" token to indicate that there are no
2976 	 * contracts corresponding to this LDI event.
2977 	 */
2978 	cmn_err(CE_PANIC, "Unknown LDI event: %s", evname);
2979 
2980 	return (0);
2981 }
2982 
2983 char *
2984 ldi_ev_get_type(ldi_ev_cookie_t cookie)
2985 {
2986 	int i;
2987 	struct ldi_ev_cookie *cookie_impl = (struct ldi_ev_cookie *)cookie;
2988 
2989 	for (i = 0; ldi_ev_cookies[i].ck_evname != NULL; i++) {
2990 		if (&ldi_ev_cookies[i] == cookie_impl) {
2991 			LDI_EVTRC((CE_NOTE, "ldi_ev_get_type: LDI: %s",
2992 			    ldi_ev_cookies[i].ck_evname));
2993 			return (ldi_ev_cookies[i].ck_evname);
2994 		}
2995 	}
2996 
2997 	/*
2998 	 * Not an LDI native event. Must be NDI event service.
2999 	 * Just return a generic string
3000 	 */
3001 	LDI_EVTRC((CE_NOTE, "ldi_ev_get_type: is NDI"));
3002 	return (NDI_EVENT_SERVICE);
3003 }
3004 
3005 static int
3006 ldi_native_cookie(ldi_ev_cookie_t cookie)
3007 {
3008 	int i;
3009 	struct ldi_ev_cookie *cookie_impl = (struct ldi_ev_cookie *)cookie;
3010 
3011 	for (i = 0; ldi_ev_cookies[i].ck_evname != NULL; i++) {
3012 		if (&ldi_ev_cookies[i] == cookie_impl) {
3013 			LDI_EVTRC((CE_NOTE, "ldi_native_cookie: native LDI"));
3014 			return (1);
3015 		}
3016 	}
3017 
3018 	LDI_EVTRC((CE_NOTE, "ldi_native_cookie: is NDI"));
3019 	return (0);
3020 }
3021 
3022 static ldi_ev_cookie_t
3023 ldi_get_native_cookie(const char *evname)
3024 {
3025 	int i;
3026 
3027 	for (i = 0; ldi_ev_cookies[i].ck_evname != NULL; i++) {
3028 		if (strcmp(ldi_ev_cookies[i].ck_evname, evname) == 0) {
3029 			LDI_EVTRC((CE_NOTE, "ldi_get_native_cookie: found"));
3030 			return ((ldi_ev_cookie_t)&ldi_ev_cookies[i]);
3031 		}
3032 	}
3033 
3034 	LDI_EVTRC((CE_NOTE, "ldi_get_native_cookie: NOT found"));
3035 	return (NULL);
3036 }
3037 
3038 /*
3039  * ldi_ev_lock() needs to be recursive, since layered drivers may call
3040  * other LDI interfaces (such as ldi_close() from within the context of
3041  * a notify callback. Since the notify callback is called with the
3042  * ldi_ev_lock() held and ldi_close() also grabs ldi_ev_lock, the lock needs
3043  * to be recursive.
3044  */
3045 static void
3046 ldi_ev_lock(void)
3047 {
3048 	LDI_EVTRC((CE_NOTE, "ldi_ev_lock: entered"));
3049 
3050 	mutex_enter(&ldi_ev_callback_list.le_lock);
3051 	if (ldi_ev_callback_list.le_thread == curthread) {
3052 		ASSERT(ldi_ev_callback_list.le_busy >= 1);
3053 		ldi_ev_callback_list.le_busy++;
3054 	} else {
3055 		while (ldi_ev_callback_list.le_busy)
3056 			cv_wait(&ldi_ev_callback_list.le_cv,
3057 			    &ldi_ev_callback_list.le_lock);
3058 		ASSERT(ldi_ev_callback_list.le_thread == NULL);
3059 		ldi_ev_callback_list.le_busy = 1;
3060 		ldi_ev_callback_list.le_thread = curthread;
3061 	}
3062 	mutex_exit(&ldi_ev_callback_list.le_lock);
3063 
3064 	LDI_EVTRC((CE_NOTE, "ldi_ev_lock: exit"));
3065 }
3066 
3067 static void
3068 ldi_ev_unlock(void)
3069 {
3070 	LDI_EVTRC((CE_NOTE, "ldi_ev_unlock: entered"));
3071 	mutex_enter(&ldi_ev_callback_list.le_lock);
3072 	ASSERT(ldi_ev_callback_list.le_thread == curthread);
3073 	ASSERT(ldi_ev_callback_list.le_busy >= 1);
3074 
3075 	ldi_ev_callback_list.le_busy--;
3076 	if (ldi_ev_callback_list.le_busy == 0) {
3077 		ldi_ev_callback_list.le_thread = NULL;
3078 		cv_signal(&ldi_ev_callback_list.le_cv);
3079 	}
3080 	mutex_exit(&ldi_ev_callback_list.le_lock);
3081 	LDI_EVTRC((CE_NOTE, "ldi_ev_unlock: exit"));
3082 }
3083 
3084 int
3085 ldi_ev_get_cookie(ldi_handle_t lh, char *evname, ldi_ev_cookie_t *cookiep)
3086 {
3087 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
3088 	dev_info_t		*dip;
3089 	dev_t			dev;
3090 	int			res;
3091 	struct snode		*csp;
3092 	ddi_eventcookie_t	ddi_cookie;
3093 	ldi_ev_cookie_t		tcookie;
3094 
3095 	LDI_EVDBG((CE_NOTE, "ldi_ev_get_cookie: entered: evname=%s",
3096 	    evname ? evname : "<NULL>"));
3097 
3098 	if (lh == NULL || evname == NULL ||
3099 	    strlen(evname) == 0 || cookiep == NULL) {
3100 		LDI_EVDBG((CE_NOTE, "ldi_ev_get_cookie: invalid args"));
3101 		return (LDI_EV_FAILURE);
3102 	}
3103 
3104 	*cookiep = NULL;
3105 
3106 	/*
3107 	 * First check if it is a LDI native event
3108 	 */
3109 	tcookie = ldi_get_native_cookie(evname);
3110 	if (tcookie) {
3111 		LDI_EVDBG((CE_NOTE, "ldi_ev_get_cookie: got native cookie"));
3112 		*cookiep = tcookie;
3113 		return (LDI_EV_SUCCESS);
3114 	}
3115 
3116 	/*
3117 	 * Not a LDI native event. Try NDI event services
3118 	 */
3119 
3120 	dev = handlep->lh_vp->v_rdev;
3121 
3122 	csp = VTOCS(handlep->lh_vp);
3123 	mutex_enter(&csp->s_lock);
3124 	if ((dip = csp->s_dip) != NULL)
3125 		e_ddi_hold_devi(dip);
3126 	mutex_exit(&csp->s_lock);
3127 	if (dip == NULL)
3128 		dip = e_ddi_hold_devi_by_dev(dev, 0);
3129 
3130 	if (dip == NULL) {
3131 		cmn_err(CE_WARN, "ldi_ev_get_cookie: No devinfo node for LDI "
3132 		    "handle: %p", (void *)handlep);
3133 		return (LDI_EV_FAILURE);
3134 	}
3135 
3136 	LDI_EVDBG((CE_NOTE, "Calling ddi_get_eventcookie: dip=%p, ev=%s",
3137 	    (void *)dip, evname));
3138 
3139 	res = ddi_get_eventcookie(dip, evname, &ddi_cookie);
3140 
3141 	ddi_release_devi(dip);
3142 
3143 	if (res == DDI_SUCCESS) {
3144 		LDI_EVDBG((CE_NOTE, "ldi_ev_get_cookie: NDI cookie found"));
3145 		*cookiep = (ldi_ev_cookie_t)ddi_cookie;
3146 		return (LDI_EV_SUCCESS);
3147 	} else {
3148 		LDI_EVDBG((CE_WARN, "ldi_ev_get_cookie: NDI cookie: failed"));
3149 		return (LDI_EV_FAILURE);
3150 	}
3151 }
3152 
3153 /*ARGSUSED*/
3154 static void
3155 i_ldi_ev_callback(dev_info_t *dip, ddi_eventcookie_t event_cookie,
3156     void *arg, void *ev_data)
3157 {
3158 	ldi_ev_callback_impl_t *lecp = (ldi_ev_callback_impl_t *)arg;
3159 
3160 	ASSERT(lecp != NULL);
3161 	ASSERT(!ldi_native_cookie(lecp->lec_cookie));
3162 	ASSERT(lecp->lec_lhp);
3163 	ASSERT(lecp->lec_notify == NULL);
3164 	ASSERT(lecp->lec_finalize);
3165 
3166 	LDI_EVDBG((CE_NOTE, "i_ldi_ev_callback: ldh=%p, cookie=%p, arg=%p, "
3167 	    "ev_data=%p", (void *)lecp->lec_lhp, (void *)event_cookie,
3168 	    (void *)lecp->lec_arg, (void *)ev_data));
3169 
3170 	lecp->lec_finalize(lecp->lec_lhp, (ldi_ev_cookie_t)event_cookie,
3171 	    lecp->lec_arg, ev_data);
3172 }
3173 
3174 int
3175 ldi_ev_register_callbacks(ldi_handle_t lh, ldi_ev_cookie_t cookie,
3176     ldi_ev_callback_t *callb, void *arg, ldi_callback_id_t *id)
3177 {
3178 	struct ldi_handle	*lhp = (struct ldi_handle *)lh;
3179 	ldi_ev_callback_impl_t	*lecp;
3180 	dev_t			dev;
3181 	struct snode		*csp;
3182 	dev_info_t		*dip;
3183 	int			ddi_event;
3184 
3185 	ASSERT(!servicing_interrupt());
3186 
3187 	if (lh == NULL || cookie == NULL || callb == NULL || id == NULL) {
3188 		LDI_EVDBG((CE_NOTE, "ldi_ev_register_callbacks: Invalid args"));
3189 		return (LDI_EV_FAILURE);
3190 	}
3191 
3192 	if (callb->cb_vers != LDI_EV_CB_VERS) {
3193 		LDI_EVDBG((CE_NOTE, "ldi_ev_register_callbacks: Invalid vers"));
3194 		return (LDI_EV_FAILURE);
3195 	}
3196 
3197 	if (callb->cb_notify == NULL && callb->cb_finalize == NULL) {
3198 		LDI_EVDBG((CE_NOTE, "ldi_ev_register_callbacks: NULL callb"));
3199 		return (LDI_EV_FAILURE);
3200 	}
3201 
3202 	*id = 0;
3203 
3204 	dev = lhp->lh_vp->v_rdev;
3205 	csp = VTOCS(lhp->lh_vp);
3206 	mutex_enter(&csp->s_lock);
3207 	if ((dip = csp->s_dip) != NULL)
3208 		e_ddi_hold_devi(dip);
3209 	mutex_exit(&csp->s_lock);
3210 	if (dip == NULL)
3211 		dip = e_ddi_hold_devi_by_dev(dev, 0);
3212 
3213 	if (dip == NULL) {
3214 		cmn_err(CE_WARN, "ldi_ev_register: No devinfo node for "
3215 		    "LDI handle: %p", (void *)lhp);
3216 		return (LDI_EV_FAILURE);
3217 	}
3218 
3219 	lecp = kmem_zalloc(sizeof (ldi_ev_callback_impl_t), KM_SLEEP);
3220 
3221 	ddi_event = 0;
3222 	if (!ldi_native_cookie(cookie)) {
3223 		if (callb->cb_notify || callb->cb_finalize == NULL) {
3224 			/*
3225 			 * NDI event services only accept finalize
3226 			 */
3227 			cmn_err(CE_WARN, "%s: module: %s: NDI event cookie. "
3228 			    "Only finalize"
3229 			    " callback supported with this cookie",
3230 			    "ldi_ev_register_callbacks",
3231 			    lhp->lh_ident->li_modname);
3232 			kmem_free(lecp, sizeof (ldi_ev_callback_impl_t));
3233 			ddi_release_devi(dip);
3234 			return (LDI_EV_FAILURE);
3235 		}
3236 
3237 		if (ddi_add_event_handler(dip, (ddi_eventcookie_t)cookie,
3238 		    i_ldi_ev_callback, (void *)lecp,
3239 		    (ddi_callback_id_t *)&lecp->lec_id)
3240 		    != DDI_SUCCESS) {
3241 			kmem_free(lecp, sizeof (ldi_ev_callback_impl_t));
3242 			ddi_release_devi(dip);
3243 			LDI_EVDBG((CE_NOTE, "ldi_ev_register_callbacks(): "
3244 			    "ddi_add_event_handler failed"));
3245 			return (LDI_EV_FAILURE);
3246 		}
3247 		ddi_event = 1;
3248 		LDI_EVDBG((CE_NOTE, "ldi_ev_register_callbacks(): "
3249 		    "ddi_add_event_handler success"));
3250 	}
3251 
3252 
3253 
3254 	ldi_ev_lock();
3255 
3256 	/*
3257 	 * Add the notify/finalize callback to the LDI's list of callbacks.
3258 	 */
3259 	lecp->lec_lhp = lhp;
3260 	lecp->lec_dev = lhp->lh_vp->v_rdev;
3261 	lecp->lec_spec = VTYP_TO_STYP(lhp->lh_vp->v_type);
3262 	lecp->lec_notify = callb->cb_notify;
3263 	lecp->lec_finalize = callb->cb_finalize;
3264 	lecp->lec_arg = arg;
3265 	lecp->lec_cookie = cookie;
3266 	if (!ddi_event)
3267 		lecp->lec_id = (void *)(uintptr_t)(++ldi_ev_id_pool);
3268 	else
3269 		ASSERT(lecp->lec_id);
3270 	lecp->lec_dip = dip;
3271 	list_insert_tail(&ldi_ev_callback_list.le_head, lecp);
3272 
3273 	*id = (ldi_callback_id_t)lecp->lec_id;
3274 
3275 	ldi_ev_unlock();
3276 
3277 	ddi_release_devi(dip);
3278 
3279 	LDI_EVDBG((CE_NOTE, "ldi_ev_register_callbacks: registered "
3280 	    "notify/finalize"));
3281 
3282 	return (LDI_EV_SUCCESS);
3283 }
3284 
3285 static int
3286 ldi_ev_device_match(ldi_ev_callback_impl_t *lecp, dev_info_t *dip,
3287     dev_t dev, int spec_type)
3288 {
3289 	ASSERT(lecp);
3290 	ASSERT(dip);
3291 	ASSERT(dev != DDI_DEV_T_NONE);
3292 	ASSERT(dev != NODEV);
3293 	ASSERT((dev == DDI_DEV_T_ANY && spec_type == 0) ||
3294 	    (spec_type == S_IFCHR || spec_type == S_IFBLK));
3295 	ASSERT(lecp->lec_dip);
3296 	ASSERT(lecp->lec_spec == S_IFCHR || lecp->lec_spec == S_IFBLK);
3297 	ASSERT(lecp->lec_dev != DDI_DEV_T_ANY);
3298 	ASSERT(lecp->lec_dev != DDI_DEV_T_NONE);
3299 	ASSERT(lecp->lec_dev != NODEV);
3300 
3301 	if (dip != lecp->lec_dip)
3302 		return (0);
3303 
3304 	if (dev != DDI_DEV_T_ANY) {
3305 		if (dev != lecp->lec_dev || spec_type != lecp->lec_spec)
3306 			return (0);
3307 	}
3308 
3309 	LDI_EVTRC((CE_NOTE, "ldi_ev_device_match: MATCH dip=%p", (void *)dip));
3310 
3311 	return (1);
3312 }
3313 
3314 /*
3315  * LDI framework function to post a "notify" event to all layered drivers
3316  * that have registered for that event
3317  *
3318  * Returns:
3319  *		LDI_EV_SUCCESS - registered callbacks allow event
3320  *		LDI_EV_FAILURE - registered callbacks block event
3321  *		LDI_EV_NONE    - No matching LDI callbacks
3322  *
3323  * This function is *not* to be called by layered drivers. It is for I/O
3324  * framework code in Solaris, such as the I/O retire code and DR code
3325  * to call while servicing a device event such as offline or degraded.
3326  */
3327 int
3328 ldi_invoke_notify(dev_info_t *dip, dev_t dev, int spec_type, char *event,
3329     void *ev_data)
3330 {
3331 	ldi_ev_callback_impl_t *lecp;
3332 	list_t	*listp;
3333 	int	ret;
3334 	char	*lec_event;
3335 
3336 	ASSERT(dip);
3337 	ASSERT(dev != DDI_DEV_T_NONE);
3338 	ASSERT(dev != NODEV);
3339 	ASSERT((dev == DDI_DEV_T_ANY && spec_type == 0) ||
3340 	    (spec_type == S_IFCHR || spec_type == S_IFBLK));
3341 	ASSERT(event);
3342 	ASSERT(ldi_native_event(event));
3343 	ASSERT(ldi_ev_sync_event(event));
3344 
3345 	LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): entered: dip=%p, ev=%s",
3346 	    (void *)dip, event));
3347 
3348 	ret = LDI_EV_NONE;
3349 	ldi_ev_lock();
3350 
3351 	VERIFY(ldi_ev_callback_list.le_walker_next == NULL);
3352 	listp = &ldi_ev_callback_list.le_head;
3353 	for (lecp = list_head(listp); lecp; lecp =
3354 	    ldi_ev_callback_list.le_walker_next) {
3355 		ldi_ev_callback_list.le_walker_next = list_next(listp, lecp);
3356 
3357 		/* Check if matching device */
3358 		if (!ldi_ev_device_match(lecp, dip, dev, spec_type))
3359 			continue;
3360 
3361 		if (lecp->lec_lhp == NULL) {
3362 			/*
3363 			 * Consumer has unregistered the handle and so
3364 			 * is no longer interested in notify events.
3365 			 */
3366 			LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): No LDI "
3367 			    "handle, skipping"));
3368 			continue;
3369 		}
3370 
3371 		if (lecp->lec_notify == NULL) {
3372 			LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): No notify "
3373 			    "callback. skipping"));
3374 			continue;	/* not interested in notify */
3375 		}
3376 
3377 		/*
3378 		 * Check if matching event
3379 		 */
3380 		lec_event = ldi_ev_get_type(lecp->lec_cookie);
3381 		if (strcmp(event, lec_event) != 0) {
3382 			LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): Not matching"
3383 			    " event {%s,%s}. skipping", event, lec_event));
3384 			continue;
3385 		}
3386 
3387 		lecp->lec_lhp->lh_flags |= LH_FLAGS_NOTIFY;
3388 		if (lecp->lec_notify(lecp->lec_lhp, lecp->lec_cookie,
3389 		    lecp->lec_arg, ev_data) != LDI_EV_SUCCESS) {
3390 			ret = LDI_EV_FAILURE;
3391 			LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): notify"
3392 			    " FAILURE"));
3393 			break;
3394 		}
3395 
3396 		/* We have a matching callback that allows the event to occur */
3397 		ret = LDI_EV_SUCCESS;
3398 
3399 		LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): 1 consumer success"));
3400 	}
3401 
3402 	if (ret != LDI_EV_FAILURE)
3403 		goto out;
3404 
3405 	LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): undoing notify"));
3406 
3407 	/*
3408 	 * Undo notifies already sent
3409 	 */
3410 	lecp = list_prev(listp, lecp);
3411 	VERIFY(ldi_ev_callback_list.le_walker_prev == NULL);
3412 	for (; lecp; lecp = ldi_ev_callback_list.le_walker_prev) {
3413 		ldi_ev_callback_list.le_walker_prev = list_prev(listp, lecp);
3414 
3415 		/*
3416 		 * Check if matching device
3417 		 */
3418 		if (!ldi_ev_device_match(lecp, dip, dev, spec_type))
3419 			continue;
3420 
3421 
3422 		if (lecp->lec_finalize == NULL) {
3423 			LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): no finalize, "
3424 			    "skipping"));
3425 			continue;	/* not interested in finalize */
3426 		}
3427 
3428 		/*
3429 		 * it is possible that in response to a notify event a
3430 		 * layered driver closed its LDI handle so it is ok
3431 		 * to have a NULL LDI handle for finalize. The layered
3432 		 * driver is expected to maintain state in its "arg"
3433 		 * parameter to keep track of the closed device.
3434 		 */
3435 
3436 		/* Check if matching event */
3437 		lec_event = ldi_ev_get_type(lecp->lec_cookie);
3438 		if (strcmp(event, lec_event) != 0) {
3439 			LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): not matching "
3440 			    "event: %s,%s, skipping", event, lec_event));
3441 			continue;
3442 		}
3443 
3444 		LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): calling finalize"));
3445 
3446 		lecp->lec_finalize(lecp->lec_lhp, lecp->lec_cookie,
3447 		    LDI_EV_FAILURE, lecp->lec_arg, ev_data);
3448 
3449 		/*
3450 		 * If LDI native event and LDI handle closed in context
3451 		 * of notify, NULL out the finalize callback as we have
3452 		 * already called the 1 finalize above allowed in this situation
3453 		 */
3454 		if (lecp->lec_lhp == NULL &&
3455 		    ldi_native_cookie(lecp->lec_cookie)) {
3456 			LDI_EVDBG((CE_NOTE,
3457 			    "ldi_invoke_notify(): NULL-ing finalize after "
3458 			    "calling 1 finalize following ldi_close"));
3459 			lecp->lec_finalize = NULL;
3460 		}
3461 	}
3462 
3463 out:
3464 	ldi_ev_callback_list.le_walker_next = NULL;
3465 	ldi_ev_callback_list.le_walker_prev = NULL;
3466 	ldi_ev_unlock();
3467 
3468 	if (ret == LDI_EV_NONE) {
3469 		LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): no matching "
3470 		    "LDI callbacks"));
3471 	}
3472 
3473 	return (ret);
3474 }
3475 
3476 /*
3477  * Framework function to be called from a layered driver to propagate
3478  * LDI "notify" events to exported minors.
3479  *
3480  * This function is a public interface exported by the LDI framework
3481  * for use by layered drivers to propagate device events up the software
3482  * stack.
3483  */
3484 int
3485 ldi_ev_notify(dev_info_t *dip, minor_t minor, int spec_type,
3486     ldi_ev_cookie_t cookie, void *ev_data)
3487 {
3488 	char		*evname = ldi_ev_get_type(cookie);
3489 	uint_t		ct_evtype;
3490 	dev_t		dev;
3491 	major_t		major;
3492 	int		retc;
3493 	int		retl;
3494 
3495 	ASSERT(spec_type == S_IFBLK || spec_type == S_IFCHR);
3496 	ASSERT(dip);
3497 	ASSERT(ldi_native_cookie(cookie));
3498 
3499 	LDI_EVDBG((CE_NOTE, "ldi_ev_notify(): entered: event=%s, dip=%p",
3500 	    evname, (void *)dip));
3501 
3502 	if (!ldi_ev_sync_event(evname)) {
3503 		cmn_err(CE_PANIC, "ldi_ev_notify(): %s not a "
3504 		    "negotiatable event", evname);
3505 	}
3506 
3507 	major = ddi_driver_major(dip);
3508 	if (major == DDI_MAJOR_T_NONE) {
3509 		char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3510 		(void) ddi_pathname(dip, path);
3511 		cmn_err(CE_WARN, "ldi_ev_notify: cannot derive major number "
3512 		    "for device %s", path);
3513 		kmem_free(path, MAXPATHLEN);
3514 		return (LDI_EV_FAILURE);
3515 	}
3516 	dev = makedevice(major, minor);
3517 
3518 	/*
3519 	 * Generate negotiation contract events on contracts (if any) associated
3520 	 * with this minor.
3521 	 */
3522 	LDI_EVDBG((CE_NOTE, "ldi_ev_notify(): calling contract nego."));
3523 	ct_evtype = ldi_contract_event(evname);
3524 	retc = contract_device_negotiate(dip, dev, spec_type, ct_evtype);
3525 	if (retc == CT_NACK) {
3526 		LDI_EVDBG((CE_NOTE, "ldi_ev_notify(): contract neg. NACK"));
3527 		return (LDI_EV_FAILURE);
3528 	}
3529 
3530 	LDI_EVDBG((CE_NOTE, "ldi_ev_notify(): LDI invoke notify"));
3531 	retl = ldi_invoke_notify(dip, dev, spec_type, evname, ev_data);
3532 	if (retl == LDI_EV_FAILURE) {
3533 		LDI_EVDBG((CE_NOTE, "ldi_ev_notify(): ldi_invoke_notify "
3534 		    "returned FAILURE. Calling contract negend"));
3535 		contract_device_negend(dip, dev, spec_type, CT_EV_FAILURE);
3536 		return (LDI_EV_FAILURE);
3537 	}
3538 
3539 	/*
3540 	 * The very fact that we are here indicates that there is a
3541 	 * LDI callback (and hence a constraint) for the retire of the
3542 	 * HW device. So we just return success even if there are no
3543 	 * contracts or LDI callbacks against the minors layered on top
3544 	 * of the HW minors
3545 	 */
3546 	LDI_EVDBG((CE_NOTE, "ldi_ev_notify(): returning SUCCESS"));
3547 	return (LDI_EV_SUCCESS);
3548 }
3549 
3550 /*
3551  * LDI framework function to invoke "finalize" callbacks for all layered
3552  * drivers that have registered callbacks for that event.
3553  *
3554  * This function is *not* to be called by layered drivers. It is for I/O
3555  * framework code in Solaris, such as the I/O retire code and DR code
3556  * to call while servicing a device event such as offline or degraded.
3557  */
3558 void
3559 ldi_invoke_finalize(dev_info_t *dip, dev_t dev, int spec_type, char *event,
3560     int ldi_result, void *ev_data)
3561 {
3562 	ldi_ev_callback_impl_t *lecp;
3563 	list_t	*listp;
3564 	char	*lec_event;
3565 	int	found = 0;
3566 
3567 	ASSERT(dip);
3568 	ASSERT(dev != DDI_DEV_T_NONE);
3569 	ASSERT(dev != NODEV);
3570 	ASSERT((dev == DDI_DEV_T_ANY && spec_type == 0) ||
3571 	    (spec_type == S_IFCHR || spec_type == S_IFBLK));
3572 	ASSERT(event);
3573 	ASSERT(ldi_native_event(event));
3574 	ASSERT(ldi_result == LDI_EV_SUCCESS || ldi_result == LDI_EV_FAILURE);
3575 
3576 	LDI_EVDBG((CE_NOTE, "ldi_invoke_finalize(): entered: dip=%p, result=%d"
3577 	    " event=%s", (void *)dip, ldi_result, event));
3578 
3579 	ldi_ev_lock();
3580 	VERIFY(ldi_ev_callback_list.le_walker_next == NULL);
3581 	listp = &ldi_ev_callback_list.le_head;
3582 	for (lecp = list_head(listp); lecp; lecp =
3583 	    ldi_ev_callback_list.le_walker_next) {
3584 		ldi_ev_callback_list.le_walker_next = list_next(listp, lecp);
3585 
3586 		if (lecp->lec_finalize == NULL) {
3587 			LDI_EVDBG((CE_NOTE, "ldi_invoke_finalize(): No "
3588 			    "finalize. Skipping"));
3589 			continue;	/* Not interested in finalize */
3590 		}
3591 
3592 		/*
3593 		 * Check if matching device
3594 		 */
3595 		if (!ldi_ev_device_match(lecp, dip, dev, spec_type))
3596 			continue;
3597 
3598 		/*
3599 		 * It is valid for the LDI handle to be NULL during finalize.
3600 		 * The layered driver may have done an LDI close in the notify
3601 		 * callback.
3602 		 */
3603 
3604 		/*
3605 		 * Check if matching event
3606 		 */
3607 		lec_event = ldi_ev_get_type(lecp->lec_cookie);
3608 		if (strcmp(event, lec_event) != 0) {
3609 			LDI_EVDBG((CE_NOTE, "ldi_invoke_finalize(): Not "
3610 			    "matching event {%s,%s}. Skipping",
3611 			    event, lec_event));
3612 			continue;
3613 		}
3614 
3615 		LDI_EVDBG((CE_NOTE, "ldi_invoke_finalize(): calling finalize"));
3616 
3617 		found = 1;
3618 
3619 		lecp->lec_finalize(lecp->lec_lhp, lecp->lec_cookie,
3620 		    ldi_result, lecp->lec_arg, ev_data);
3621 
3622 		/*
3623 		 * If LDI native event and LDI handle closed in context
3624 		 * of notify, NULL out the finalize callback as we have
3625 		 * already called the 1 finalize above allowed in this situation
3626 		 */
3627 		if (lecp->lec_lhp == NULL &&
3628 		    ldi_native_cookie(lecp->lec_cookie)) {
3629 			LDI_EVDBG((CE_NOTE,
3630 			    "ldi_invoke_finalize(): NULLing finalize after "
3631 			    "calling 1 finalize following ldi_close"));
3632 			lecp->lec_finalize = NULL;
3633 		}
3634 	}
3635 	ldi_ev_callback_list.le_walker_next = NULL;
3636 	ldi_ev_unlock();
3637 
3638 	if (found)
3639 		return;
3640 
3641 	LDI_EVDBG((CE_NOTE, "ldi_invoke_finalize(): no matching callbacks"));
3642 }
3643 
3644 /*
3645  * Framework function to be called from a layered driver to propagate
3646  * LDI "finalize" events to exported minors.
3647  *
3648  * This function is a public interface exported by the LDI framework
3649  * for use by layered drivers to propagate device events up the software
3650  * stack.
3651  */
3652 void
3653 ldi_ev_finalize(dev_info_t *dip, minor_t minor, int spec_type, int ldi_result,
3654     ldi_ev_cookie_t cookie, void *ev_data)
3655 {
3656 	dev_t dev;
3657 	major_t major;
3658 	char *evname;
3659 	int ct_result = (ldi_result == LDI_EV_SUCCESS) ?
3660 	    CT_EV_SUCCESS : CT_EV_FAILURE;
3661 	uint_t ct_evtype;
3662 
3663 	ASSERT(dip);
3664 	ASSERT(spec_type == S_IFBLK || spec_type == S_IFCHR);
3665 	ASSERT(ldi_result == LDI_EV_SUCCESS || ldi_result == LDI_EV_FAILURE);
3666 	ASSERT(ldi_native_cookie(cookie));
3667 
3668 	LDI_EVDBG((CE_NOTE, "ldi_ev_finalize: entered: dip=%p", (void *)dip));
3669 
3670 	major = ddi_driver_major(dip);
3671 	if (major == DDI_MAJOR_T_NONE) {
3672 		char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3673 		(void) ddi_pathname(dip, path);
3674 		cmn_err(CE_WARN, "ldi_ev_finalize: cannot derive major number "
3675 		    "for device %s", path);
3676 		kmem_free(path, MAXPATHLEN);
3677 		return;
3678 	}
3679 	dev = makedevice(major, minor);
3680 
3681 	evname = ldi_ev_get_type(cookie);
3682 
3683 	LDI_EVDBG((CE_NOTE, "ldi_ev_finalize: calling contracts"));
3684 	ct_evtype = ldi_contract_event(evname);
3685 	contract_device_finalize(dip, dev, spec_type, ct_evtype, ct_result);
3686 
3687 	LDI_EVDBG((CE_NOTE, "ldi_ev_finalize: calling ldi_invoke_finalize"));
3688 	ldi_invoke_finalize(dip, dev, spec_type, evname, ldi_result, ev_data);
3689 }
3690 
3691 int
3692 ldi_ev_remove_callbacks(ldi_callback_id_t id)
3693 {
3694 	ldi_ev_callback_impl_t	*lecp;
3695 	ldi_ev_callback_impl_t	*next;
3696 	ldi_ev_callback_impl_t	*found;
3697 	list_t			*listp;
3698 
3699 	ASSERT(!servicing_interrupt());
3700 
3701 	if (id == 0) {
3702 		cmn_err(CE_WARN, "ldi_ev_remove_callbacks: Invalid ID 0");
3703 		return (LDI_EV_FAILURE);
3704 	}
3705 
3706 	LDI_EVDBG((CE_NOTE, "ldi_ev_remove_callbacks: entered: id=%p",
3707 	    (void *)id));
3708 
3709 	ldi_ev_lock();
3710 
3711 	listp = &ldi_ev_callback_list.le_head;
3712 	next = found = NULL;
3713 	for (lecp = list_head(listp); lecp; lecp = next) {
3714 		next = list_next(listp, lecp);
3715 		if (lecp->lec_id == id) {
3716 			VERIFY(found == NULL);
3717 
3718 			/*
3719 			 * If there is a walk in progress, shift that walk
3720 			 * along to the next element so that we can remove
3721 			 * this one.  This allows us to unregister an arbitrary
3722 			 * number of callbacks from within a callback.
3723 			 *
3724 			 * See the struct definition (in sunldi_impl.h) for
3725 			 * more information.
3726 			 */
3727 			if (ldi_ev_callback_list.le_walker_next == lecp)
3728 				ldi_ev_callback_list.le_walker_next = next;
3729 			if (ldi_ev_callback_list.le_walker_prev == lecp)
3730 				ldi_ev_callback_list.le_walker_prev = list_prev(
3731 				    listp, ldi_ev_callback_list.le_walker_prev);
3732 
3733 			list_remove(listp, lecp);
3734 			found = lecp;
3735 		}
3736 	}
3737 	ldi_ev_unlock();
3738 
3739 	if (found == NULL) {
3740 		cmn_err(CE_WARN, "No LDI event handler for id (%p)",
3741 		    (void *)id);
3742 		return (LDI_EV_SUCCESS);
3743 	}
3744 
3745 	if (!ldi_native_cookie(found->lec_cookie)) {
3746 		ASSERT(found->lec_notify == NULL);
3747 		if (ddi_remove_event_handler((ddi_callback_id_t)id)
3748 		    != DDI_SUCCESS) {
3749 			cmn_err(CE_WARN, "failed to remove NDI event handler "
3750 			    "for id (%p)", (void *)id);
3751 			ldi_ev_lock();
3752 			list_insert_tail(listp, found);
3753 			ldi_ev_unlock();
3754 			return (LDI_EV_FAILURE);
3755 		}
3756 		LDI_EVDBG((CE_NOTE, "ldi_ev_remove_callbacks: NDI event "
3757 		    "service removal succeeded"));
3758 	} else {
3759 		LDI_EVDBG((CE_NOTE, "ldi_ev_remove_callbacks: removed "
3760 		    "LDI native callbacks"));
3761 	}
3762 	kmem_free(found, sizeof (ldi_ev_callback_impl_t));
3763 
3764 	return (LDI_EV_SUCCESS);
3765 }
3766