xref: /titanic_41/usr/src/uts/common/os/sunndi.c (revision da14cebe459d3275048785f25bd869cb09b5307f)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/buf.h>
32 #include <sys/uio.h>
33 #include <sys/cred.h>
34 #include <sys/poll.h>
35 #include <sys/mman.h>
36 #include <sys/kmem.h>
37 #include <sys/model.h>
38 #include <sys/file.h>
39 #include <sys/proc.h>
40 #include <sys/open.h>
41 #include <sys/user.h>
42 #include <sys/t_lock.h>
43 #include <sys/vm.h>
44 #include <sys/stat.h>
45 #include <vm/hat.h>
46 #include <vm/seg.h>
47 #include <vm/as.h>
48 #include <sys/cmn_err.h>
49 #include <sys/debug.h>
50 #include <sys/avintr.h>
51 #include <sys/autoconf.h>
52 #include <sys/sunddi.h>
53 #include <sys/esunddi.h>
54 #include <sys/sunndi.h>
55 #include <sys/ddi.h>
56 #include <sys/kstat.h>
57 #include <sys/conf.h>
58 #include <sys/ddi_impldefs.h>	/* include implementation structure defs */
59 #include <sys/ndi_impldefs.h>
60 #include <sys/hwconf.h>
61 #include <sys/pathname.h>
62 #include <sys/modctl.h>
63 #include <sys/epm.h>
64 #include <sys/devctl.h>
65 #include <sys/callb.h>
66 #include <sys/bootconf.h>
67 #include <sys/dacf_impl.h>
68 #include <sys/nvpair.h>
69 #include <sys/sunmdi.h>
70 #include <sys/fs/dv_node.h>
71 #include <sys/sunldi_impl.h>
72 
73 #ifdef __sparc
74 #include <sys/archsystm.h>	/* getpil/setpil */
75 #include <sys/membar.h>		/* membar_sync */
76 #endif
77 
78 /*
79  * ndi property handling
80  */
81 int
82 ndi_prop_update_int(dev_t match_dev, dev_info_t *dip,
83     char *name, int data)
84 {
85 	return (ddi_prop_update_common(match_dev, dip,
86 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT | DDI_PROP_DONTSLEEP,
87 	    name, &data, 1, ddi_prop_fm_encode_ints));
88 }
89 
90 int
91 ndi_prop_update_int64(dev_t match_dev, dev_info_t *dip,
92     char *name, int64_t data)
93 {
94 	return (ddi_prop_update_common(match_dev, dip,
95 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT64 | DDI_PROP_DONTSLEEP,
96 	    name, &data, 1, ddi_prop_fm_encode_int64));
97 }
98 
99 int
100 ndi_prop_create_boolean(dev_t match_dev, dev_info_t *dip,
101     char *name)
102 {
103 	return (ddi_prop_update_common(match_dev, dip,
104 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_ANY | DDI_PROP_DONTSLEEP,
105 	    name, NULL, 0, ddi_prop_fm_encode_bytes));
106 }
107 
108 int
109 ndi_prop_update_int_array(dev_t match_dev, dev_info_t *dip,
110     char *name, int *data, uint_t nelements)
111 {
112 	return (ddi_prop_update_common(match_dev, dip,
113 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT | DDI_PROP_DONTSLEEP,
114 	    name, data, nelements, ddi_prop_fm_encode_ints));
115 }
116 
117 int
118 ndi_prop_update_int64_array(dev_t match_dev, dev_info_t *dip,
119     char *name, int64_t *data, uint_t nelements)
120 {
121 	return (ddi_prop_update_common(match_dev, dip,
122 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT64 | DDI_PROP_DONTSLEEP,
123 	    name, data, nelements, ddi_prop_fm_encode_int64));
124 }
125 
126 int
127 ndi_prop_update_string(dev_t match_dev, dev_info_t *dip,
128     char *name, char *data)
129 {
130 	return (ddi_prop_update_common(match_dev, dip,
131 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_STRING | DDI_PROP_DONTSLEEP,
132 	    name, &data, 1, ddi_prop_fm_encode_string));
133 }
134 
135 int
136 ndi_prop_update_string_array(dev_t match_dev, dev_info_t *dip,
137     char *name, char **data, uint_t nelements)
138 {
139 	return (ddi_prop_update_common(match_dev, dip,
140 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_STRING | DDI_PROP_DONTSLEEP,
141 	    name, data, nelements,
142 	    ddi_prop_fm_encode_strings));
143 }
144 
145 int
146 ndi_prop_update_byte_array(dev_t match_dev, dev_info_t *dip,
147     char *name, uchar_t *data, uint_t nelements)
148 {
149 	if (nelements == 0)
150 		return (DDI_PROP_INVAL_ARG);
151 
152 	return (ddi_prop_update_common(match_dev, dip,
153 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_BYTE | DDI_PROP_DONTSLEEP,
154 	    name, data, nelements, ddi_prop_fm_encode_bytes));
155 }
156 
157 int
158 ndi_prop_remove(dev_t dev, dev_info_t *dip, char *name)
159 {
160 	return (ddi_prop_remove_common(dev, dip, name, DDI_PROP_HW_DEF));
161 }
162 
163 void
164 ndi_prop_remove_all(dev_info_t *dip)
165 {
166 	i_ddi_prop_dyn_parent_set(dip, NULL);
167 	ddi_prop_remove_all_common(dip, (int)DDI_PROP_HW_DEF);
168 }
169 
170 /*
171  * Post an event notification to nexus driver responsible for handling
172  * the event.  The responsible nexus is defined in the cookie passed in as
173  * the third parameter.
174  * The dip parameter is an artifact of an older implementation in which all
175  * requests to remove an eventcall would bubble up the tree.  Today, this
176  * parameter is ignored.
177  * Input Parameters:
178  *	dip 	- Ignored.
179  *	rdip 	- device driver posting the event
180  *	cookie	- valid ddi_eventcookie_t, obtained by caller prior to
181  *		  invocation of this routine
182  *	impl_data - used by framework
183  */
184 /*ARGSUSED*/
185 int
186 ndi_post_event(dev_info_t *dip, dev_info_t *rdip,
187 		ddi_eventcookie_t cookie, void *impl_data)
188 {
189 	dev_info_t *ddip;
190 
191 	ASSERT(cookie);
192 	ddip = NDI_EVENT_DDIP(cookie);
193 
194 	/*
195 	 * perform sanity checks.  These conditions should never be true.
196 	 */
197 
198 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops != NULL);
199 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_6);
200 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->bus_post_event != NULL);
201 
202 	/*
203 	 * post the event to the responsible ancestor
204 	 */
205 	return ((*(DEVI(ddip)->devi_ops->devo_bus_ops->bus_post_event))
206 	    (ddip, rdip, cookie, impl_data));
207 }
208 
209 /*
210  * Calls the bus nexus driver's implementation of the
211  * (*bus_remove_eventcall)() interface.
212  */
213 int
214 ndi_busop_remove_eventcall(dev_info_t *ddip, ddi_callback_id_t id)
215 {
216 
217 	ASSERT(id);
218 	/* check for a correct revno before calling up the device tree. */
219 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops != NULL);
220 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_6);
221 
222 	if (DEVI(ddip)->devi_ops->devo_bus_ops->bus_remove_eventcall == NULL)
223 		return (DDI_FAILURE);
224 
225 	/*
226 	 * request responsible nexus to remove the eventcall
227 	 */
228 	return ((*(DEVI(ddip)->devi_ops->devo_bus_ops->bus_remove_eventcall))
229 	    (ddip, id));
230 }
231 
232 /*
233  * Calls the bus nexus driver's implementation of the
234  * (*bus_add_eventcall)() interface.  The dip parameter is an
235  * artifact of an older implementation in which all requests to
236  * add an eventcall would bubble up the tree.  Today, this parameter is
237  * ignored.
238  */
239 /*ARGSUSED*/
240 int
241 ndi_busop_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
242 		ddi_eventcookie_t cookie, void (*callback)(), void *arg,
243 		ddi_callback_id_t *cb_id)
244 {
245 	dev_info_t *ddip = (dev_info_t *)NDI_EVENT_DDIP(cookie);
246 
247 	/*
248 	 * check for a correct revno before calling up the device tree.
249 	 */
250 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops != NULL);
251 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_6);
252 
253 	if (DEVI(ddip)->devi_ops->devo_bus_ops->bus_add_eventcall == NULL)
254 		return (DDI_FAILURE);
255 
256 	/*
257 	 * request responsible ancestor to add the eventcall
258 	 */
259 	return ((*(DEVI(ddip)->devi_ops->devo_bus_ops->bus_add_eventcall))
260 	    (ddip, rdip, cookie, callback, arg, cb_id));
261 }
262 
263 /*
264  * Calls the bus nexus driver's implementation of the
265  * (*bus_get_eventcookie)() interface up the device tree hierarchy.
266  */
267 int
268 ndi_busop_get_eventcookie(dev_info_t *dip, dev_info_t *rdip, char *name,
269 		ddi_eventcookie_t *event_cookiep)
270 {
271 	dev_info_t *pdip = (dev_info_t *)DEVI(dip)->devi_parent;
272 
273 	/* Can not be called from rootnex. */
274 	ASSERT(pdip);
275 
276 	/*
277 	 * check for a correct revno before calling up the device tree.
278 	 */
279 	ASSERT(DEVI(pdip)->devi_ops->devo_bus_ops != NULL);
280 
281 	if ((DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_6) ||
282 	    (DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie == NULL)) {
283 #ifdef DEBUG
284 		if ((DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev >=
285 		    BUSO_REV_3) &&
286 		    (DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie)) {
287 			cmn_err(CE_WARN,
288 			    "Warning: %s%d busops_rev=%d no longer supported"
289 			    " by the NDI event framework.\nBUSO_REV_6 or "
290 			    "greater must be used.",
291 			    DEVI(pdip)->devi_binding_name,
292 			    DEVI(pdip)->devi_instance,
293 			    DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev);
294 		}
295 #endif /* DEBUG */
296 
297 		return (ndi_busop_get_eventcookie(pdip, rdip, name,
298 		    event_cookiep));
299 	}
300 
301 	return ((*(DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie))
302 	    (pdip, rdip, name, event_cookiep));
303 }
304 
305 /*
306  * Copy in the devctl IOCTL data and return a handle to
307  * the data.
308  */
309 int
310 ndi_dc_allochdl(void *iocarg, struct devctl_iocdata **rdcp)
311 {
312 	struct devctl_iocdata *dcp;
313 	char *cpybuf;
314 
315 	ASSERT(rdcp != NULL);
316 
317 	dcp = kmem_zalloc(sizeof (*dcp), KM_SLEEP);
318 
319 	if (get_udatamodel() == DATAMODEL_NATIVE) {
320 		if (copyin(iocarg, dcp, sizeof (*dcp)) != 0) {
321 			kmem_free(dcp, sizeof (*dcp));
322 			return (NDI_FAULT);
323 		}
324 	}
325 #ifdef _SYSCALL32_IMPL
326 	else {
327 		struct devctl_iocdata32 dcp32;
328 
329 		if (copyin(iocarg, &dcp32, sizeof (dcp32)) != 0) {
330 			kmem_free(dcp, sizeof (*dcp));
331 			return (NDI_FAULT);
332 		}
333 		dcp->cmd = (uint_t)dcp32.cmd;
334 		dcp->flags = (uint_t)dcp32.flags;
335 		dcp->cpyout_buf = (uint_t *)(uintptr_t)dcp32.cpyout_buf;
336 		dcp->nvl_user = (nvlist_t *)(uintptr_t)dcp32.nvl_user;
337 		dcp->nvl_usersz = (size_t)dcp32.nvl_usersz;
338 		dcp->c_nodename = (char *)(uintptr_t)dcp32.c_nodename;
339 		dcp->c_unitaddr = (char *)(uintptr_t)dcp32.c_unitaddr;
340 	}
341 #endif
342 	if (dcp->c_nodename != NULL) {
343 		cpybuf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
344 		if (copyinstr(dcp->c_nodename, cpybuf, MAXNAMELEN, 0) != 0) {
345 			kmem_free(cpybuf, MAXNAMELEN);
346 			kmem_free(dcp, sizeof (*dcp));
347 			return (NDI_FAULT);
348 		}
349 		cpybuf[MAXNAMELEN - 1] = '\0';
350 		dcp->c_nodename = cpybuf;
351 	}
352 
353 	if (dcp->c_unitaddr != NULL) {
354 		cpybuf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
355 		if (copyinstr(dcp->c_unitaddr, cpybuf, MAXNAMELEN, 0) != 0) {
356 			kmem_free(cpybuf, MAXNAMELEN);
357 			if (dcp->c_nodename != NULL)
358 				kmem_free(dcp->c_nodename, MAXNAMELEN);
359 			kmem_free(dcp, sizeof (*dcp));
360 			return (NDI_FAULT);
361 		}
362 		cpybuf[MAXNAMELEN - 1] = '\0';
363 		dcp->c_unitaddr = cpybuf;
364 	}
365 
366 	/*
367 	 * copyin and unpack a user defined nvlist if one was passed
368 	 */
369 	if (dcp->nvl_user != NULL) {
370 		if ((dcp->nvl_usersz == 0) ||
371 		    (dcp->nvl_usersz > DEVCTL_MAX_NVL_USERSZ)) {
372 			if (dcp->c_nodename != NULL)
373 				kmem_free(dcp->c_nodename, MAXNAMELEN);
374 			if (dcp->c_unitaddr != NULL)
375 				kmem_free(dcp->c_unitaddr, MAXNAMELEN);
376 			kmem_free(dcp, sizeof (*dcp));
377 			return (NDI_FAILURE);
378 		}
379 		cpybuf = kmem_alloc(dcp->nvl_usersz, KM_SLEEP);
380 		if (copyin(dcp->nvl_user, cpybuf, dcp->nvl_usersz) != 0) {
381 			kmem_free(cpybuf, dcp->nvl_usersz);
382 			if (dcp->c_nodename != NULL)
383 				kmem_free(dcp->c_nodename, MAXNAMELEN);
384 			if (dcp->c_unitaddr != NULL)
385 				kmem_free(dcp->c_unitaddr, MAXNAMELEN);
386 			kmem_free(dcp, sizeof (*dcp));
387 			return (NDI_FAULT);
388 		}
389 
390 		if (nvlist_unpack(cpybuf, dcp->nvl_usersz, &dcp->nvl_user,
391 		    KM_SLEEP)) {
392 			kmem_free(cpybuf, dcp->nvl_usersz);
393 			if (dcp->c_nodename != NULL)
394 				kmem_free(dcp->c_nodename, MAXNAMELEN);
395 			if (dcp->c_unitaddr != NULL)
396 				kmem_free(dcp->c_unitaddr, MAXNAMELEN);
397 			kmem_free(dcp, sizeof (*dcp));
398 			return (NDI_FAULT);
399 		}
400 		/*
401 		 * free the buffer containing the packed nvlist
402 		 */
403 		kmem_free(cpybuf, dcp->nvl_usersz);
404 
405 	}
406 
407 	*rdcp = dcp;
408 	return (NDI_SUCCESS);
409 }
410 
411 /*
412  * free all space allocated to a handle.
413  */
414 void
415 ndi_dc_freehdl(struct devctl_iocdata *dcp)
416 {
417 	ASSERT(dcp != NULL);
418 
419 	if (dcp->c_nodename != NULL)
420 		kmem_free(dcp->c_nodename, MAXNAMELEN);
421 
422 	if (dcp->c_unitaddr != NULL)
423 		kmem_free(dcp->c_unitaddr, MAXNAMELEN);
424 
425 	if (dcp->nvl_user != NULL)
426 		nvlist_free(dcp->nvl_user);
427 
428 	kmem_free(dcp, sizeof (*dcp));
429 }
430 
431 char *
432 ndi_dc_getname(struct devctl_iocdata *dcp)
433 {
434 	ASSERT(dcp != NULL);
435 	return (dcp->c_nodename);
436 
437 }
438 
439 char *
440 ndi_dc_getaddr(struct devctl_iocdata *dcp)
441 {
442 	ASSERT(dcp != NULL);
443 	return (dcp->c_unitaddr);
444 }
445 
446 nvlist_t *
447 ndi_dc_get_ap_data(struct devctl_iocdata *dcp)
448 {
449 	ASSERT(dcp != NULL);
450 
451 	return (dcp->nvl_user);
452 }
453 
454 /*
455  * Transition the child named by "devname@devaddr" to the online state.
456  * For use by a driver's DEVCTL_DEVICE_ONLINE handler.
457  */
458 int
459 ndi_devctl_device_online(dev_info_t *dip, struct devctl_iocdata *dcp,
460 	uint_t flags)
461 {
462 	int	rval;
463 	char	*name;
464 	dev_info_t *rdip;
465 
466 	if (ndi_dc_getname(dcp) == NULL || ndi_dc_getaddr(dcp) == NULL)
467 		return (EINVAL);
468 
469 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
470 	(void) snprintf(name, MAXNAMELEN, "%s@%s",
471 	    ndi_dc_getname(dcp), ndi_dc_getaddr(dcp));
472 
473 	if ((rval = ndi_devi_config_one(dip, name, &rdip,
474 	    flags | NDI_DEVI_ONLINE | NDI_CONFIG)) == NDI_SUCCESS) {
475 		ndi_rele_devi(rdip);
476 
477 		/*
478 		 * Invalidate devfs cached directory contents. For the checks
479 		 * in the "if" condition see the comment in ndi_devi_online().
480 		 */
481 		if (i_ddi_devi_attached(dip) && !DEVI_BUSY_OWNED(dip))
482 			(void) devfs_clean(dip, NULL, 0);
483 
484 	} else if (rval == NDI_BUSY) {
485 		rval = EBUSY;
486 	} else if (rval == NDI_FAILURE) {
487 		rval = EIO;
488 	}
489 
490 	NDI_DEBUG(flags, (CE_CONT, "%s%d: online: %s: %s\n",
491 	    ddi_driver_name(dip), ddi_get_instance(dip), name,
492 	    ((rval == NDI_SUCCESS) ? "ok" : "failed")));
493 
494 	kmem_free(name, MAXNAMELEN);
495 
496 	return (rval);
497 }
498 
499 /*
500  * Transition the child named by "devname@devaddr" to the offline state.
501  * For use by a driver's DEVCTL_DEVICE_OFFLINE handler.
502  */
503 int
504 ndi_devctl_device_offline(dev_info_t *dip, struct devctl_iocdata *dcp,
505 	uint_t flags)
506 {
507 	int	rval;
508 	char	*name;
509 
510 	if (ndi_dc_getname(dcp) == NULL || ndi_dc_getaddr(dcp) == NULL)
511 		return (EINVAL);
512 
513 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
514 	(void) snprintf(name, MAXNAMELEN, "%s@%s",
515 	    ndi_dc_getname(dcp), ndi_dc_getaddr(dcp));
516 
517 	(void) devfs_clean(dip, name, DV_CLEAN_FORCE);
518 	rval = ndi_devi_unconfig_one(dip, name, NULL,
519 	    flags | NDI_DEVI_OFFLINE);
520 
521 	if (rval == NDI_BUSY) {
522 		rval = EBUSY;
523 	} else if (rval == NDI_FAILURE) {
524 		rval = EIO;
525 	}
526 
527 	NDI_DEBUG(flags, (CE_CONT, "%s%d: offline: %s: %s\n",
528 	    ddi_driver_name(dip), ddi_get_instance(dip), name,
529 	    (rval == NDI_SUCCESS) ? "ok" : "failed"));
530 
531 	kmem_free(name, MAXNAMELEN);
532 
533 	return (rval);
534 }
535 
536 /*
537  * Remove the child named by "devname@devaddr".
538  * For use by a driver's DEVCTL_DEVICE_REMOVE handler.
539  */
540 int
541 ndi_devctl_device_remove(dev_info_t *dip, struct devctl_iocdata *dcp,
542 	uint_t flags)
543 {
544 	int	rval;
545 	char	*name;
546 
547 	if (ndi_dc_getname(dcp) == NULL || ndi_dc_getaddr(dcp) == NULL)
548 		return (EINVAL);
549 
550 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
551 	(void) snprintf(name, MAXNAMELEN, "%s@%s",
552 	    ndi_dc_getname(dcp), ndi_dc_getaddr(dcp));
553 
554 	(void) devfs_clean(dip, name, DV_CLEAN_FORCE);
555 
556 	rval = ndi_devi_unconfig_one(dip, name, NULL, flags | NDI_DEVI_REMOVE);
557 
558 	if (rval == NDI_BUSY) {
559 		rval = EBUSY;
560 	} else if (rval == NDI_FAILURE) {
561 		rval = EIO;
562 	}
563 
564 	NDI_DEBUG(flags, (CE_CONT, "%s%d: remove: %s: %s\n",
565 	    ddi_driver_name(dip), ddi_get_instance(dip), name,
566 	    (rval == NDI_SUCCESS) ? "ok" : "failed"));
567 
568 	kmem_free(name, MAXNAMELEN);
569 
570 	return (rval);
571 }
572 
573 /*
574  * Return devctl state of the child named by "name@addr".
575  * For use by a driver's DEVCTL_DEVICE_GETSTATE handler.
576  */
577 int
578 ndi_devctl_device_getstate(dev_info_t *parent, struct devctl_iocdata *dcp,
579 	uint_t *state)
580 {
581 	dev_info_t *dip;
582 	char *name, *addr;
583 	char *devname;
584 	int devnamelen;
585 	int circ;
586 
587 	if (parent == NULL ||
588 	    ((name = ndi_dc_getname(dcp)) == NULL) ||
589 	    ((addr = ndi_dc_getaddr(dcp)) == NULL))
590 		return (NDI_FAILURE);
591 
592 	devnamelen = strlen(name) + strlen(addr) + 2;
593 	devname = kmem_alloc(devnamelen, KM_SLEEP);
594 	if (strlen(addr) > 0) {
595 		(void) snprintf(devname, devnamelen, "%s@%s", name, addr);
596 	} else {
597 		(void) snprintf(devname, devnamelen, "%s", name);
598 	}
599 
600 	ndi_devi_enter(parent, &circ);
601 
602 	dip = ndi_devi_findchild(parent, devname);
603 	kmem_free(devname, devnamelen);
604 
605 	if (dip == NULL) {
606 		ndi_devi_exit(parent, circ);
607 		return (NDI_FAILURE);
608 	}
609 
610 	mutex_enter(&(DEVI(dip)->devi_lock));
611 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
612 		*state = DEVICE_OFFLINE;
613 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
614 		*state = DEVICE_DOWN;
615 	} else {
616 		*state = DEVICE_ONLINE;
617 		if (devi_stillreferenced(dip) == DEVI_REFERENCED)
618 			*state |= DEVICE_BUSY;
619 	}
620 
621 	mutex_exit(&(DEVI(dip)->devi_lock));
622 	ndi_devi_exit(parent, circ);
623 
624 	return (NDI_SUCCESS);
625 }
626 
627 /*
628  * return the current state of the device "dip"
629  *
630  * recommend using ndi_devctl_ioctl() or
631  * ndi_devctl_device_getstate() instead
632  */
633 int
634 ndi_dc_return_dev_state(dev_info_t *dip, struct devctl_iocdata *dcp)
635 {
636 	dev_info_t *pdip;
637 	uint_t devstate = 0;
638 	int circ;
639 
640 	if ((dip == NULL) || (dcp == NULL))
641 		return (NDI_FAILURE);
642 
643 	pdip = ddi_get_parent(dip);
644 
645 	ndi_devi_enter(pdip, &circ);
646 	mutex_enter(&(DEVI(dip)->devi_lock));
647 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
648 		devstate = DEVICE_OFFLINE;
649 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
650 		devstate = DEVICE_DOWN;
651 	} else {
652 		devstate = DEVICE_ONLINE;
653 		if (devi_stillreferenced(dip) == DEVI_REFERENCED)
654 			devstate |= DEVICE_BUSY;
655 	}
656 
657 	mutex_exit(&(DEVI(dip)->devi_lock));
658 	ndi_devi_exit(pdip, circ);
659 
660 	if (copyout(&devstate, dcp->cpyout_buf, sizeof (uint_t)) != 0)
661 		return (NDI_FAULT);
662 
663 	return (NDI_SUCCESS);
664 }
665 
666 /*
667  * Return device's bus state
668  * For use by a driver's DEVCTL_BUS_GETSTATE handler.
669  */
670 int
671 ndi_devctl_bus_getstate(dev_info_t *dip, struct devctl_iocdata *dcp,
672 	uint_t *state)
673 {
674 	if ((dip == NULL) || (dcp == NULL))
675 		return (NDI_FAILURE);
676 
677 	return (ndi_get_bus_state(dip, state));
678 }
679 
680 /*
681  * Generic devctl ioctl handler
682  */
683 int
684 ndi_devctl_ioctl(dev_info_t *dip, int cmd, intptr_t arg, int mode, uint_t flags)
685 {
686 	_NOTE(ARGUNUSED(mode))
687 	struct devctl_iocdata *dcp;
688 	uint_t state;
689 	int rval = ENOTTY;
690 
691 	/*
692 	 * read devctl ioctl data
693 	 */
694 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
695 		return (EFAULT);
696 
697 	switch (cmd) {
698 
699 	case DEVCTL_BUS_GETSTATE:
700 		rval = ndi_devctl_bus_getstate(dip, dcp, &state);
701 		if (rval == NDI_SUCCESS) {
702 			if (copyout(&state, dcp->cpyout_buf,
703 			    sizeof (uint_t)) != 0)
704 				rval = NDI_FAULT;
705 		}
706 		break;
707 
708 	case DEVCTL_DEVICE_ONLINE:
709 		rval = ndi_devctl_device_online(dip, dcp, flags);
710 		break;
711 
712 	case DEVCTL_DEVICE_OFFLINE:
713 		rval = ndi_devctl_device_offline(dip, dcp, flags);
714 		break;
715 
716 	case DEVCTL_DEVICE_GETSTATE:
717 		rval = ndi_devctl_device_getstate(dip, dcp, &state);
718 		if (rval == NDI_SUCCESS) {
719 			if (copyout(&state, dcp->cpyout_buf,
720 			    sizeof (uint_t)) != 0)
721 				rval = NDI_FAULT;
722 		}
723 		break;
724 
725 	case DEVCTL_DEVICE_REMOVE:
726 		rval = ndi_devctl_device_remove(dip, dcp, flags);
727 		break;
728 
729 	case DEVCTL_BUS_DEV_CREATE:
730 		rval = ndi_dc_devi_create(dcp, dip, 0, NULL);
731 		break;
732 
733 	/*
734 	 * ioctls for which a generic implementation makes no sense
735 	 */
736 	case DEVCTL_BUS_RESET:
737 	case DEVCTL_BUS_RESETALL:
738 	case DEVCTL_DEVICE_RESET:
739 	case DEVCTL_AP_CONNECT:
740 	case DEVCTL_AP_DISCONNECT:
741 	case DEVCTL_AP_INSERT:
742 	case DEVCTL_AP_REMOVE:
743 	case DEVCTL_AP_CONFIGURE:
744 	case DEVCTL_AP_UNCONFIGURE:
745 	case DEVCTL_AP_GETSTATE:
746 	case DEVCTL_AP_CONTROL:
747 	case DEVCTL_BUS_QUIESCE:
748 	case DEVCTL_BUS_UNQUIESCE:
749 		rval = ENOTSUP;
750 		break;
751 	}
752 
753 	ndi_dc_freehdl(dcp);
754 	return (rval);
755 }
756 
757 /*
758  * Copyout the state of the Attachment Point "ap" to the requesting
759  * user process.
760  */
761 int
762 ndi_dc_return_ap_state(devctl_ap_state_t *ap, struct devctl_iocdata *dcp)
763 {
764 	if ((ap == NULL) || (dcp == NULL))
765 		return (NDI_FAILURE);
766 
767 
768 	if (get_udatamodel() == DATAMODEL_NATIVE) {
769 		if (copyout(ap, dcp->cpyout_buf,
770 		    sizeof (devctl_ap_state_t)) != 0)
771 			return (NDI_FAULT);
772 	}
773 #ifdef _SYSCALL32_IMPL
774 	else {
775 		struct devctl_ap_state32 ap_state32;
776 
777 		ap_state32.ap_rstate = ap->ap_rstate;
778 		ap_state32.ap_ostate = ap->ap_ostate;
779 		ap_state32.ap_condition = ap->ap_condition;
780 		ap_state32.ap_error_code = ap->ap_error_code;
781 		ap_state32.ap_in_transition = ap->ap_in_transition;
782 		ap_state32.ap_last_change = (time32_t)ap->ap_last_change;
783 		if (copyout(&ap_state32, dcp->cpyout_buf,
784 		    sizeof (devctl_ap_state32_t)) != 0)
785 			return (NDI_FAULT);
786 	}
787 #endif
788 
789 	return (NDI_SUCCESS);
790 }
791 
792 /*
793  * Copyout the bus state of the bus nexus device "dip" to the requesting
794  * user process.
795  */
796 int
797 ndi_dc_return_bus_state(dev_info_t *dip, struct devctl_iocdata *dcp)
798 {
799 	uint_t devstate = 0;
800 
801 	if ((dip == NULL) || (dcp == NULL))
802 		return (NDI_FAILURE);
803 
804 	if (ndi_get_bus_state(dip, &devstate) != NDI_SUCCESS)
805 		return (NDI_FAILURE);
806 
807 	if (copyout(&devstate, dcp->cpyout_buf, sizeof (uint_t)) != 0)
808 		return (NDI_FAULT);
809 
810 	return (NDI_SUCCESS);
811 }
812 
813 static int
814 i_dc_devi_create(struct devctl_iocdata *, dev_info_t *, dev_info_t **);
815 
816 /*
817  * create a child device node given the property definitions
818  * supplied by the userland process
819  */
820 int
821 ndi_dc_devi_create(struct devctl_iocdata *dcp, dev_info_t *pdip, int flags,
822     dev_info_t **rdip)
823 {
824 	dev_info_t *cdip;
825 	int rv, circular = 0;
826 	char devnm[MAXNAMELEN];
827 	int nmlen;
828 
829 	/*
830 	 * The child device may have been pre-constructed by an earlier
831 	 * call to this function with the flag DEVCTL_CONSTRUCT set.
832 	 */
833 
834 	if ((cdip = (rdip != NULL) ? *rdip : NULL) == NULL)
835 		if ((rv = i_dc_devi_create(dcp, pdip, &cdip)) != 0)
836 			return (rv);
837 
838 	ASSERT(cdip != NULL);
839 
840 	/*
841 	 * Return the device node partially constructed if the
842 	 * DEVCTL_CONSTRUCT flag is set.
843 	 */
844 	if (flags & DEVCTL_CONSTRUCT) {
845 		if (rdip == NULL) {
846 			(void) ndi_devi_free(cdip);
847 			return (EINVAL);
848 		}
849 		*rdip = cdip;
850 		return (0);
851 	}
852 
853 	/*
854 	 * Bring the node up to a named but OFFLINE state.  The calling
855 	 * application will need to manage the node from here on.
856 	 */
857 	if (dcp->flags & DEVCTL_OFFLINE) {
858 		/*
859 		 * In the unlikely event that the dip was somehow attached by
860 		 * the userland process (and device contracts or LDI opens
861 		 * were registered against the dip) after it was created by
862 		 * a previous DEVCTL_CONSTRUCT call, we start notify
863 		 * proceedings on this dip. Note that we don't need to
864 		 * return the dip after a failure of the notify since
865 		 * for a contract or LDI handle to be created the dip was
866 		 * already available to the user.
867 		 */
868 		if (e_ddi_offline_notify(cdip) == DDI_FAILURE) {
869 			return (EBUSY);
870 		}
871 
872 		/*
873 		 * hand set the OFFLINE flag to prevent any asynchronous
874 		 * autoconfiguration operations from attaching this node.
875 		 */
876 		mutex_enter(&(DEVI(cdip)->devi_lock));
877 		DEVI_SET_DEVICE_OFFLINE(cdip);
878 		mutex_exit(&(DEVI(cdip)->devi_lock));
879 
880 		e_ddi_offline_finalize(cdip, DDI_SUCCESS);
881 
882 		rv = ndi_devi_bind_driver(cdip, flags);
883 		if (rv != NDI_SUCCESS) {
884 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
885 			return (ENXIO);
886 		}
887 
888 		/*
889 		 * remove the dev_info node if it failed to bind to a
890 		 * driver above.
891 		 */
892 		if (i_ddi_node_state(cdip) < DS_BOUND) {
893 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
894 			return (ENXIO);
895 		}
896 
897 		/*
898 		 * add the node to the per-driver list and INITCHILD it
899 		 * to give it a name.
900 		 */
901 		ndi_devi_enter(pdip, &circular);
902 		if ((rv = ddi_initchild(pdip, cdip)) != DDI_SUCCESS) {
903 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
904 			ndi_devi_exit(pdip, circular);
905 			return (EINVAL);
906 		}
907 		ndi_devi_exit(pdip, circular);
908 
909 	} else {
910 		/*
911 		 * Attempt to bring the device ONLINE. If the request to
912 		 * fails, remove the dev_info node.
913 		 */
914 		if (ndi_devi_online(cdip, NDI_ONLINE_ATTACH) != NDI_SUCCESS) {
915 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
916 			return (ENXIO);
917 		}
918 
919 		/*
920 		 * if the node was successfully added but there was
921 		 * no driver available for the device, remove the node
922 		 */
923 		if (i_ddi_node_state(cdip) < DS_BOUND) {
924 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
925 			return (ENODEV);
926 		}
927 	}
928 
929 	/*
930 	 * return a handle to the child device
931 	 * copy out the name of the newly attached child device if
932 	 * the IOCTL request has provided a copyout buffer.
933 	 */
934 	if (rdip != NULL)
935 		*rdip = cdip;
936 
937 	if (dcp->cpyout_buf == NULL)
938 		return (0);
939 
940 	ASSERT(ddi_node_name(cdip) != NULL);
941 	ASSERT(ddi_get_name_addr(cdip) != NULL);
942 
943 	nmlen = snprintf(devnm, MAXNAMELEN, "%s@%s",
944 	    ddi_node_name(cdip), ddi_get_name_addr(cdip));
945 
946 	if (copyout(&devnm, dcp->cpyout_buf, nmlen) != 0) {
947 		(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
948 		return (EFAULT);
949 	}
950 	return (0);
951 }
952 
953 static int
954 i_dc_devi_create(struct devctl_iocdata *dcp, dev_info_t *pdip,
955     dev_info_t **rdip)
956 {
957 
958 	dev_info_t *cdip;
959 	char *cname = NULL;
960 	nvlist_t *nvlp = dcp->nvl_user;
961 	nvpair_t *npp;
962 	char *np;
963 	int rv = 0;
964 
965 	ASSERT(rdip != NULL && *rdip == NULL);
966 
967 	if ((nvlp == NULL) ||
968 	    (nvlist_lookup_string(nvlp, DC_DEVI_NODENAME, &cname) != 0))
969 		return (EINVAL);
970 
971 	/*
972 	 * construct a new dev_info node with a user-provided nodename
973 	 */
974 	ndi_devi_alloc_sleep(pdip, cname, (pnode_t)DEVI_SID_NODEID, &cdip);
975 
976 	/*
977 	 * create hardware properties for each member in the property
978 	 * list.
979 	 */
980 	for (npp = nvlist_next_nvpair(nvlp, NULL); (npp != NULL && !rv);
981 	    npp = nvlist_next_nvpair(nvlp, npp)) {
982 
983 		np = nvpair_name(npp);
984 
985 		/*
986 		 * skip the nodename property
987 		 */
988 		if (strcmp(np, DC_DEVI_NODENAME) == 0)
989 			continue;
990 
991 		switch (nvpair_type(npp)) {
992 
993 		case DATA_TYPE_INT32: {
994 			int32_t prop_val;
995 
996 			if ((rv = nvpair_value_int32(npp, &prop_val)) != 0)
997 				break;
998 
999 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip, np,
1000 			    (int)prop_val);
1001 			break;
1002 		}
1003 
1004 		case DATA_TYPE_STRING: {
1005 			char *prop_val;
1006 
1007 			if ((rv = nvpair_value_string(npp, &prop_val)) != 0)
1008 				break;
1009 
1010 			(void) ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
1011 			    np, prop_val);
1012 			break;
1013 		}
1014 
1015 		case DATA_TYPE_BYTE_ARRAY: {
1016 			uchar_t *val;
1017 			uint_t nelms;
1018 
1019 			if ((rv = nvpair_value_byte_array(npp, &val,
1020 			    &nelms)) != 0)
1021 				break;
1022 
1023 			(void) ndi_prop_update_byte_array(DDI_DEV_T_NONE,
1024 			    cdip, np, (uchar_t *)val, nelms);
1025 			break;
1026 		}
1027 
1028 		case DATA_TYPE_INT32_ARRAY: {
1029 			int32_t *val;
1030 			uint_t nelms;
1031 
1032 			if ((rv = nvpair_value_int32_array(npp, &val,
1033 			    &nelms)) != 0)
1034 				break;
1035 
1036 			(void) ndi_prop_update_int_array(DDI_DEV_T_NONE,
1037 			    cdip, np, val, nelms);
1038 			break;
1039 		}
1040 
1041 		case DATA_TYPE_STRING_ARRAY: {
1042 			char **val;
1043 			uint_t nelms;
1044 
1045 			if ((rv = nvpair_value_string_array(npp, &val,
1046 			    &nelms)) != 0)
1047 				break;
1048 
1049 			(void) ndi_prop_update_string_array(DDI_DEV_T_NONE,
1050 			    cdip, np, val, nelms);
1051 			break;
1052 		}
1053 
1054 		/*
1055 		 * unsupported property data type
1056 		 */
1057 		default:
1058 			rv = EINVAL;
1059 		}
1060 	}
1061 
1062 	/*
1063 	 * something above failed
1064 	 * destroy the partially child device and abort the request
1065 	 */
1066 	if (rv != 0) {
1067 		(void) ndi_devi_free(cdip);
1068 		return (rv);
1069 	}
1070 
1071 	*rdip = cdip;
1072 	return (0);
1073 }
1074 
1075 /*
1076  * return current soft bus state of bus nexus "dip"
1077  */
1078 int
1079 ndi_get_bus_state(dev_info_t *dip, uint_t *rstate)
1080 {
1081 	if (dip == NULL || rstate == NULL)
1082 		return (NDI_FAILURE);
1083 
1084 	if (DEVI(dip)->devi_ops->devo_bus_ops == NULL)
1085 		return (NDI_FAILURE);
1086 
1087 	mutex_enter(&(DEVI(dip)->devi_lock));
1088 	if (DEVI_IS_BUS_QUIESCED(dip))
1089 		*rstate = BUS_QUIESCED;
1090 	else if (DEVI_IS_BUS_DOWN(dip))
1091 		*rstate = BUS_SHUTDOWN;
1092 	else
1093 		*rstate = BUS_ACTIVE;
1094 	mutex_exit(&(DEVI(dip)->devi_lock));
1095 	return (NDI_SUCCESS);
1096 }
1097 
1098 /*
1099  * Set the soft state of bus nexus "dip"
1100  */
1101 int
1102 ndi_set_bus_state(dev_info_t *dip, uint_t state)
1103 {
1104 	int rv = NDI_SUCCESS;
1105 
1106 	if (dip == NULL)
1107 		return (NDI_FAILURE);
1108 
1109 	mutex_enter(&(DEVI(dip)->devi_lock));
1110 
1111 	switch (state) {
1112 	case BUS_QUIESCED:
1113 		DEVI_SET_BUS_QUIESCE(dip);
1114 		break;
1115 
1116 	case BUS_ACTIVE:
1117 		DEVI_SET_BUS_ACTIVE(dip);
1118 		DEVI_SET_BUS_UP(dip);
1119 		break;
1120 
1121 	case BUS_SHUTDOWN:
1122 		DEVI_SET_BUS_DOWN(dip);
1123 		break;
1124 
1125 	default:
1126 		rv = NDI_FAILURE;
1127 	}
1128 
1129 	mutex_exit(&(DEVI(dip)->devi_lock));
1130 	return (rv);
1131 }
1132 
1133 /*
1134  * These dummy functions are obsolete and may be removed.
1135  * Retained for existing driver compatibility only.
1136  * Drivers should be fixed not to use these functions.
1137  * Don't write new code using these obsolete interfaces.
1138  */
1139 /*ARGSUSED*/
1140 void
1141 i_ndi_block_device_tree_changes(uint_t *lkcnt)	/* obsolete */
1142 {
1143 	/* obsolete dummy function */
1144 }
1145 
1146 /*ARGSUSED*/
1147 void
1148 i_ndi_allow_device_tree_changes(uint_t lkcnt)	/* obsolete */
1149 {
1150 	/* obsolete dummy function */
1151 }
1152 
1153 /*
1154  * Single thread entry into per-driver list
1155  */
1156 /*ARGSUSED*/
1157 void
1158 e_ddi_enter_driver_list(struct devnames *dnp, int *listcnt)	/* obsolete */
1159 {
1160 	/* obsolete dummy function */
1161 }
1162 
1163 /*
1164  * release the per-driver list
1165  */
1166 /*ARGSUSED*/
1167 void
1168 e_ddi_exit_driver_list(struct devnames *dnp, int listcnt)	/* obsolete */
1169 {
1170 	/* obsolete dummy function */
1171 }
1172 
1173 /*
1174  * Attempt to enter driver list
1175  */
1176 /*ARGSUSED*/
1177 int
1178 e_ddi_tryenter_driver_list(struct devnames *dnp, int *listcnt)	/* obsolete */
1179 {
1180 	return (1);	/* obsolete dummy function */
1181 }
1182 
1183 /*
1184  * ndi event handling support functions:
1185  * The NDI event support model is as follows:
1186  *
1187  * The nexus driver defines a set of events using some static structures (so
1188  * these structures can be shared by all instances of the nexus driver).
1189  * The nexus driver allocates an event handle and binds the event set
1190  * to this handle. The nexus driver's event busop functions can just
1191  * call the appropriate NDI event support function using this handle
1192  * as the first argument.
1193  *
1194  * The reasoning for tying events to the device tree is that the entity
1195  * generating the callback will typically be one of the device driver's
1196  * ancestors in the tree.
1197  */
1198 static int ndi_event_debug = 0;
1199 
1200 #ifdef DEBUG
1201 #define	NDI_EVENT_DEBUG	ndi_event_debug
1202 #endif /* DEBUG */
1203 
1204 /*
1205  * allocate a new ndi event handle
1206  */
1207 int
1208 ndi_event_alloc_hdl(dev_info_t *dip, ddi_iblock_cookie_t cookie,
1209 	ndi_event_hdl_t *handle, uint_t flag)
1210 {
1211 	struct ndi_event_hdl *ndi_event_hdl;
1212 
1213 	ndi_event_hdl = kmem_zalloc(sizeof (struct ndi_event_hdl),
1214 	    ((flag & NDI_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP));
1215 
1216 	if (!ndi_event_hdl) {
1217 		return (NDI_FAILURE);
1218 	}
1219 
1220 	ndi_event_hdl->ndi_evthdl_dip = dip;
1221 	ndi_event_hdl->ndi_evthdl_iblock_cookie = cookie;
1222 	mutex_init(&ndi_event_hdl->ndi_evthdl_mutex, NULL,
1223 	    MUTEX_DRIVER, (void *)cookie);
1224 
1225 	mutex_init(&ndi_event_hdl->ndi_evthdl_cb_mutex, NULL,
1226 	    MUTEX_DRIVER, (void *)cookie);
1227 
1228 	*handle = (ndi_event_hdl_t)ndi_event_hdl;
1229 
1230 	return (NDI_SUCCESS);
1231 }
1232 
1233 /*
1234  * free the ndi event handle
1235  */
1236 int
1237 ndi_event_free_hdl(ndi_event_hdl_t handle)
1238 {
1239 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
1240 	ndi_event_cookie_t *cookie;
1241 	ndi_event_cookie_t *free;
1242 
1243 	ASSERT(handle);
1244 
1245 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1246 	mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1247 
1248 	cookie = ndi_event_hdl->ndi_evthdl_cookie_list;
1249 
1250 	/* deallocate all defined cookies */
1251 	while (cookie != NULL) {
1252 		ASSERT(cookie->callback_list == NULL);
1253 		free = cookie;
1254 		cookie = cookie->next_cookie;
1255 
1256 		kmem_free(free, sizeof (ndi_event_cookie_t));
1257 	}
1258 
1259 
1260 	mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1261 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1262 
1263 	/* destroy mutexes */
1264 	mutex_destroy(&ndi_event_hdl->ndi_evthdl_mutex);
1265 	mutex_destroy(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1266 
1267 	/* free event handle */
1268 	kmem_free(ndi_event_hdl, sizeof (struct ndi_event_hdl));
1269 
1270 	return (NDI_SUCCESS);
1271 }
1272 
1273 
1274 /*
1275  * ndi_event_bind_set() adds a set of events to the NDI event
1276  * handle.
1277  *
1278  * Events generated by high level interrupts should not
1279  * be mixed in the same event set with events generated by
1280  * normal interrupts or kernel events.
1281  *
1282  * This function can be called multiple times to bind
1283  * additional sets to the event handle.
1284  * However, events generated by high level interrupts cannot
1285  * be bound to a handle that already has bound events generated
1286  * by normal interrupts or from kernel context and vice versa.
1287  */
1288 int
1289 ndi_event_bind_set(ndi_event_hdl_t handle,
1290 	ndi_event_set_t		*ndi_events,
1291 	uint_t			flag)
1292 {
1293 	struct ndi_event_hdl	*ndi_event_hdl;
1294 	ndi_event_cookie_t	*next, *prev, *new_cookie;
1295 	uint_t			i, len;
1296 	uint_t			dup = 0;
1297 	uint_t			high_plevels, other_plevels;
1298 	ndi_event_definition_t *ndi_event_defs;
1299 
1300 	int km_flag = ((flag & NDI_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP);
1301 
1302 	ASSERT(handle);
1303 	ASSERT(ndi_events);
1304 
1305 	/*
1306 	 * binding must be performed during attach/detach
1307 	 */
1308 	if (!DEVI_IS_ATTACHING(handle->ndi_evthdl_dip) &&
1309 	    !DEVI_IS_DETACHING(handle->ndi_evthdl_dip)) {
1310 		cmn_err(CE_WARN, "ndi_event_bind_set must be called within "
1311 		    "attach or detach");
1312 		return (NDI_FAILURE);
1313 	}
1314 
1315 	/*
1316 	 * if it is not the correct version or the event set is
1317 	 * empty, bail out
1318 	 */
1319 	if (ndi_events->ndi_events_version != NDI_EVENTS_REV1)
1320 		return (NDI_FAILURE);
1321 
1322 	ndi_event_hdl	= (struct ndi_event_hdl *)handle;
1323 	ndi_event_defs = ndi_events->ndi_event_defs;
1324 	high_plevels	= other_plevels = 0;
1325 
1326 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1327 
1328 	/* check for mixing events at high level with the other types */
1329 	for (i = 0; i < ndi_events->ndi_n_events; i++) {
1330 		if (ndi_event_defs[i].ndi_event_plevel == EPL_HIGHLEVEL) {
1331 			high_plevels++;
1332 		} else {
1333 			other_plevels++;
1334 		}
1335 	}
1336 
1337 	/*
1338 	 * bail out if high level events are mixed with other types in this
1339 	 * event set or the set is incompatible with the set in the handle
1340 	 */
1341 	if ((high_plevels && other_plevels) ||
1342 	    (other_plevels && ndi_event_hdl->ndi_evthdl_high_plevels) ||
1343 	    (high_plevels && ndi_event_hdl->ndi_evthdl_other_plevels)) {
1344 		mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1345 
1346 		return (NDI_FAILURE);
1347 	}
1348 
1349 	/*
1350 	 * check for duplicate events in both the existing handle
1351 	 * and the event set, add events if not duplicates
1352 	 */
1353 	next = ndi_event_hdl->ndi_evthdl_cookie_list;
1354 	for (i = 0; i < ndi_events->ndi_n_events; i++) {
1355 		while (next != NULL) {
1356 			len = strlen(NDI_EVENT_NAME(next)) + 1;
1357 			if (strncmp(NDI_EVENT_NAME(next),
1358 			    ndi_event_defs[i].ndi_event_name, len) == 0) {
1359 				dup = 1;
1360 				break;
1361 			}
1362 
1363 			prev = next;
1364 			next = next->next_cookie;
1365 		}
1366 
1367 		if (dup == 0) {
1368 			new_cookie = kmem_zalloc(sizeof (ndi_event_cookie_t),
1369 			    km_flag);
1370 
1371 			if (!new_cookie)
1372 				return (NDI_FAILURE);
1373 
1374 			if (ndi_event_hdl->ndi_evthdl_n_events == 0) {
1375 				ndi_event_hdl->ndi_evthdl_cookie_list =
1376 				    new_cookie;
1377 			} else {
1378 				prev->next_cookie = new_cookie;
1379 			}
1380 
1381 			ndi_event_hdl->ndi_evthdl_n_events++;
1382 
1383 			/*
1384 			 * set up new cookie
1385 			 */
1386 			new_cookie->definition = &ndi_event_defs[i];
1387 			new_cookie->ddip = ndi_event_hdl->ndi_evthdl_dip;
1388 
1389 		} else {
1390 			/*
1391 			 * event not added, must correct plevel numbers
1392 			 */
1393 			if (ndi_event_defs[i].ndi_event_plevel ==
1394 			    EPL_HIGHLEVEL) {
1395 				high_plevels--;
1396 			} else {
1397 				other_plevels--;
1398 			}
1399 		}
1400 
1401 		dup = 0;
1402 		next = ndi_event_hdl->ndi_evthdl_cookie_list;
1403 		prev = NULL;
1404 
1405 	}
1406 
1407 	ndi_event_hdl->ndi_evthdl_high_plevels	+= high_plevels;
1408 	ndi_event_hdl->ndi_evthdl_other_plevels += other_plevels;
1409 
1410 	ASSERT((ndi_event_hdl->ndi_evthdl_high_plevels == 0) ||
1411 	    (ndi_event_hdl->ndi_evthdl_other_plevels == 0));
1412 
1413 #ifdef NDI_EVENT_DEBUG
1414 	if (ndi_event_debug) {
1415 		ndi_event_dump_hdl(ndi_event_hdl, "ndi_event_bind_set");
1416 	}
1417 #endif /* NDI_EVENT_DEBUG */
1418 
1419 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1420 
1421 	return (NDI_SUCCESS);
1422 }
1423 
1424 /*
1425  * ndi_event_unbind_set() unbinds a set of events, previously
1426  * bound using ndi_event_bind_set(), from the NDI event
1427  * handle.
1428  *
1429  * This routine will unbind all events in the event set.  If an event,
1430  * specified in the event set, is not found in the handle, this
1431  * routine will proceed onto the next member of the set as if the event
1432  * was never specified.
1433  *
1434  * The event set may be a subset of the set of events that
1435  * was previously bound to the handle. For example, events
1436  * can be individually unbound.
1437  *
1438  * An event cannot be unbound if callbacks are still
1439  * registered against the event.
1440  */
1441 /*ARGSUSED*/
1442 int
1443 ndi_event_unbind_set(ndi_event_hdl_t   handle, ndi_event_set_t	*ndi_events,
1444     uint_t flag)
1445 {
1446 	ndi_event_definition_t	*ndi_event_defs;
1447 	int			len;
1448 	uint_t			i;
1449 	int			rval;
1450 	ndi_event_cookie_t *cookie_list;
1451 	ndi_event_cookie_t *prev = NULL;
1452 
1453 	ASSERT(ndi_events);
1454 	ASSERT(handle);
1455 
1456 	/*
1457 	 * binding must be performed during attach/detac
1458 	 */
1459 	if (!DEVI_IS_ATTACHING(handle->ndi_evthdl_dip) &&
1460 	    !DEVI_IS_DETACHING(handle->ndi_evthdl_dip)) {
1461 		cmn_err(CE_WARN, "ndi_event_bind_set must be called within "
1462 		    "attach or detach");
1463 		return (NDI_FAILURE);
1464 	}
1465 
1466 	/* bail out if ndi_event_set is outdated */
1467 	if (ndi_events->ndi_events_version != NDI_EVENTS_REV1) {
1468 		return (NDI_FAILURE);
1469 	}
1470 
1471 	ASSERT(ndi_events->ndi_event_defs);
1472 
1473 	ndi_event_defs = ndi_events->ndi_event_defs;
1474 
1475 	mutex_enter(&handle->ndi_evthdl_mutex);
1476 	mutex_enter(&handle->ndi_evthdl_cb_mutex);
1477 
1478 	/*
1479 	 * Verify that all events in the event set are eligible
1480 	 * for unbinding(ie. there are no outstanding callbacks).
1481 	 * If any one of the events are ineligible, fail entire
1482 	 * operation.
1483 	 */
1484 
1485 	for (i = 0; i < ndi_events->ndi_n_events; i++) {
1486 		cookie_list = handle->ndi_evthdl_cookie_list;
1487 		while (cookie_list != NULL) {
1488 			len = strlen(NDI_EVENT_NAME(cookie_list)) + 1;
1489 			if (strncmp(NDI_EVENT_NAME(cookie_list),
1490 			    ndi_event_defs[i].ndi_event_name, len) == 0) {
1491 
1492 				ASSERT(cookie_list->callback_list == NULL);
1493 				if (cookie_list->callback_list) {
1494 					rval = NDI_FAILURE;
1495 					goto done;
1496 				}
1497 				break;
1498 			} else {
1499 				cookie_list = cookie_list->next_cookie;
1500 			}
1501 		}
1502 	}
1503 
1504 	/*
1505 	 * remove all events found within the handle
1506 	 * If an event is not found, this function will proceed as if the event
1507 	 * was never specified.
1508 	 */
1509 
1510 	for (i = 0; i < ndi_events->ndi_n_events; i++) {
1511 		cookie_list = handle->ndi_evthdl_cookie_list;
1512 		prev = NULL;
1513 		while (cookie_list != NULL) {
1514 			len = strlen(NDI_EVENT_NAME(cookie_list)) + 1;
1515 			if (strncmp(NDI_EVENT_NAME(cookie_list),
1516 			    ndi_event_defs[i].ndi_event_name, len) == 0) {
1517 
1518 				/*
1519 				 * can not unbind an event definition with
1520 				 * outstanding callbacks
1521 				 */
1522 				if (cookie_list->callback_list) {
1523 					rval = NDI_FAILURE;
1524 					goto done;
1525 				}
1526 
1527 				/* remove this cookie from the list */
1528 				if (prev != NULL) {
1529 					prev->next_cookie =
1530 					    cookie_list->next_cookie;
1531 				} else {
1532 					handle->ndi_evthdl_cookie_list =
1533 					    cookie_list->next_cookie;
1534 				}
1535 
1536 				/* adjust plevel counts */
1537 				if (NDI_EVENT_PLEVEL(cookie_list) ==
1538 				    EPL_HIGHLEVEL) {
1539 					handle->ndi_evthdl_high_plevels--;
1540 				} else {
1541 					handle->ndi_evthdl_other_plevels--;
1542 				}
1543 
1544 				/* adjust cookie count */
1545 				handle->ndi_evthdl_n_events--;
1546 
1547 				/* free the cookie */
1548 				kmem_free(cookie_list,
1549 				    sizeof (ndi_event_cookie_t));
1550 
1551 				cookie_list = handle->ndi_evthdl_cookie_list;
1552 				break;
1553 
1554 			} else {
1555 				prev = cookie_list;
1556 				cookie_list = cookie_list->next_cookie;
1557 			}
1558 
1559 		}
1560 
1561 	}
1562 
1563 #ifdef NDI_EVENT_DEBUG
1564 	if (ndi_event_debug) {
1565 		ndi_event_dump_hdl(handle, "ndi_event_unbind_set");
1566 	}
1567 #endif /* NDI_EVENT_DEBUG */
1568 
1569 	rval = NDI_SUCCESS;
1570 
1571 done:
1572 	mutex_exit(&handle->ndi_evthdl_cb_mutex);
1573 	mutex_exit(&handle->ndi_evthdl_mutex);
1574 
1575 	return (rval);
1576 }
1577 
1578 /*
1579  * ndi_event_retrieve_cookie():
1580  * Return an event cookie for eventname if this nexus driver
1581  * has defined the named event. The event cookie returned
1582  * by this function is used to register callback handlers
1583  * for the event.
1584  *
1585  * ndi_event_retrieve_cookie() is intended to be used in the
1586  * nexus driver's bus_get_eventcookie busop routine.
1587  *
1588  * If the event is not defined by this bus nexus driver, and flag
1589  * does not include NDI_EVENT_NOPASS, then ndi_event_retrieve_cookie()
1590  * will pass the request up the device tree hierarchy by calling
1591  * ndi_busop_get_eventcookie(9N).
1592  * If the event is not defined by this bus nexus driver, and flag
1593  * does include NDI_EVENT_NOPASS, ndi_event_retrieve_cookie()
1594  * will return NDI_FAILURE.  The caller may then determine what further
1595  * action to take, such as using a different handle, passing the
1596  * request up the device tree using ndi_busop_get_eventcookie(9N),
1597  * or returning the failure to the caller, thus blocking the
1598  * progress of the request up the tree.
1599  */
1600 int
1601 ndi_event_retrieve_cookie(ndi_event_hdl_t handle,
1602 	dev_info_t		*rdip,
1603 	char			*eventname,
1604 	ddi_eventcookie_t	*cookiep,
1605 	uint_t			flag)
1606 {
1607 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
1608 	int		len;
1609 	ndi_event_cookie_t *cookie_list;
1610 
1611 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1612 
1613 	cookie_list = ndi_event_hdl->ndi_evthdl_cookie_list;
1614 	/*
1615 	 * search the cookie list for the event name and return
1616 	 * cookie if found.
1617 	 */
1618 	while (cookie_list != NULL) {
1619 
1620 		len = strlen(NDI_EVENT_NAME(cookie_list)) + 1;
1621 		if (strncmp(NDI_EVENT_NAME(cookie_list), eventname,
1622 		    len) == 0) {
1623 			*cookiep = (ddi_eventcookie_t)cookie_list;
1624 
1625 			mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1626 			return (NDI_SUCCESS);
1627 		}
1628 
1629 		cookie_list = cookie_list->next_cookie;
1630 	}
1631 
1632 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1633 	/*
1634 	 * event was not found, pass up or return failure
1635 	 */
1636 	if ((flag & NDI_EVENT_NOPASS) == 0) {
1637 		return (ndi_busop_get_eventcookie(
1638 		    ndi_event_hdl->ndi_evthdl_dip, rdip, eventname, cookiep));
1639 	} else {
1640 		return (NDI_FAILURE);
1641 	}
1642 }
1643 
1644 /*
1645  * check whether this nexus defined this event and look up attributes
1646  */
1647 static int
1648 ndi_event_is_defined(ndi_event_hdl_t handle,
1649 	ddi_eventcookie_t cookie, int *attributes)
1650 {
1651 
1652 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
1653 	ndi_event_cookie_t *cookie_list;
1654 
1655 	ASSERT(mutex_owned(&handle->ndi_evthdl_mutex));
1656 
1657 	cookie_list = ndi_event_hdl->ndi_evthdl_cookie_list;
1658 	while (cookie_list != NULL) {
1659 		if (cookie_list == NDI_EVENT(cookie)) {
1660 			if (attributes)
1661 				*attributes =
1662 				    NDI_EVENT_ATTRIBUTES(cookie_list);
1663 
1664 			return (NDI_SUCCESS);
1665 		}
1666 
1667 		cookie_list = cookie_list->next_cookie;
1668 	}
1669 
1670 	return (NDI_FAILURE);
1671 }
1672 
1673 /*
1674  * ndi_event_add_callback(): adds an event callback registration
1675  * to the event cookie defining this event.
1676  *
1677  * Refer also to bus_add_eventcall(9n) and ndi_busop_add_eventcall(9n).
1678  *
1679  * ndi_event_add_callback(9n) is intended to be used in
1680  * the nexus driver's bus_add_eventcall(9n) busop function.
1681  *
1682  * If the event is not defined by this bus nexus driver,
1683  * ndi_event_add_callback() will return NDI_FAILURE.
1684  */
1685 int
1686 ndi_event_add_callback(ndi_event_hdl_t handle, dev_info_t *child_dip,
1687 	ddi_eventcookie_t cookie,
1688 	void		(*event_callback)(dev_info_t *,
1689 			ddi_eventcookie_t, void *arg, void *impldata),
1690 	void		*arg,
1691 	uint_t		flag,
1692 	ddi_callback_id_t *cb_id)
1693 {
1694 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
1695 	int km_flag = ((flag & NDI_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP);
1696 	ndi_event_callbacks_t *cb;
1697 
1698 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1699 
1700 	/*
1701 	 * if the event was not bound to this handle, return failure
1702 	 */
1703 	if (ndi_event_is_defined(handle, cookie, NULL) != NDI_SUCCESS) {
1704 
1705 		mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1706 		return (NDI_FAILURE);
1707 
1708 	}
1709 
1710 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1711 
1712 	/*
1713 	 * allocate space for a callback structure
1714 	 */
1715 	cb = kmem_zalloc(sizeof (ndi_event_callbacks_t), km_flag);
1716 	if (cb == NULL) {
1717 		return (NDI_FAILURE);
1718 	}
1719 
1720 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1721 
1722 	/* initialize callback structure */
1723 	cb->ndi_evtcb_dip	= child_dip;
1724 	cb->ndi_evtcb_callback	= event_callback;
1725 	cb->ndi_evtcb_arg	= arg;
1726 	cb->ndi_evtcb_cookie	= cookie;
1727 	cb->devname 		= (char *)ddi_driver_name(child_dip);
1728 
1729 	*cb_id = (ddi_callback_id_t)cb;
1730 	mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1731 
1732 	/* add this callback structure to the list */
1733 	if (NDI_EVENT(cookie)->callback_list) {
1734 		cb->ndi_evtcb_next = NDI_EVENT(cookie)->callback_list;
1735 		NDI_EVENT(cookie)->callback_list->ndi_evtcb_prev = cb;
1736 		NDI_EVENT(cookie)->callback_list = cb;
1737 	} else {
1738 		NDI_EVENT(cookie)->callback_list = cb;
1739 	}
1740 #ifdef NDI_EVENT_DEBUG
1741 	if (ndi_event_debug) {
1742 		ndi_event_dump_hdl(ndi_event_hdl, "ndi_event_add_callback");
1743 	}
1744 #endif /* NDI_EVENT_DEBUG */
1745 
1746 	mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1747 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1748 
1749 	return (NDI_SUCCESS);
1750 }
1751 
1752 /*
1753  * ndi_event_remove_callback():
1754  *
1755  * ndi_event_remove_callback() removes a callback that was
1756  * previously registered using ndi_event_add_callback(9N).
1757  * Refer also to bus_remove_eventcall(9n) and
1758  * ndi_busop_remove_eventcall(9n).
1759  * ndi_event_remove_callback(9n) is intended to be used in
1760  * the nexus driver's bus_remove_eventcall (9n) busop function.
1761  * If the event is not defined by this bus nexus driver,
1762  * ndi_event_remove_callback() will return NDI_FAILURE.
1763  */
1764 static void do_ndi_event_remove_callback(struct ndi_event_hdl *ndi_event_hdl,
1765 	ddi_callback_id_t cb_id);
1766 
1767 int
1768 ndi_event_remove_callback(ndi_event_hdl_t handle, ddi_callback_id_t cb_id)
1769 {
1770 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
1771 
1772 	ASSERT(cb_id);
1773 
1774 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1775 	mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1776 
1777 	do_ndi_event_remove_callback(ndi_event_hdl, cb_id);
1778 
1779 	mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1780 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1781 
1782 	return (NDI_SUCCESS);
1783 }
1784 
1785 /*ARGSUSED*/
1786 static void
1787 do_ndi_event_remove_callback(struct ndi_event_hdl *ndi_event_hdl,
1788     ddi_callback_id_t cb_id)
1789 {
1790 	ndi_event_callbacks_t *cb = (ndi_event_callbacks_t *)cb_id;
1791 	ASSERT(cb);
1792 
1793 	ASSERT(mutex_owned(&ndi_event_hdl->ndi_evthdl_mutex));
1794 	ASSERT(mutex_owned(&ndi_event_hdl->ndi_evthdl_cb_mutex));
1795 
1796 	/* remove from callback linked list */
1797 	if (cb->ndi_evtcb_prev) {
1798 		cb->ndi_evtcb_prev->ndi_evtcb_next = cb->ndi_evtcb_next;
1799 	}
1800 
1801 	if (cb->ndi_evtcb_next) {
1802 		cb->ndi_evtcb_next->ndi_evtcb_prev = cb->ndi_evtcb_prev;
1803 	}
1804 
1805 	if (NDI_EVENT(cb->ndi_evtcb_cookie)->callback_list == cb) {
1806 		NDI_EVENT(cb->ndi_evtcb_cookie)->callback_list =
1807 		    cb->ndi_evtcb_next;
1808 	}
1809 
1810 	kmem_free(cb, sizeof (ndi_event_callbacks_t));
1811 }
1812 
1813 /*
1814  * ndi_event_run_callbacks() performs event callbacks for the event
1815  * specified by cookie, if this is among those bound to the
1816  * supplied handle.
1817  * If the event is among those bound to the handle, none,
1818  * some, or all of the handlers registered for the event
1819  * will be called, according to the delivery attributes of
1820  * the event.
1821  * If the event attributes include NDI_EVENT_POST_TO_ALL
1822  * (the default), all the handlers for the event will be
1823  * called in an unspecified order.
1824  * If the event attributes include NDI_EVENT_POST_TO_TGT, only
1825  * the handlers (if any) registered by the driver identified by
1826  * rdip will be called.
1827  * If the event identified by cookie is not bound to the handle,
1828  * NDI_FAILURE will be returned.
1829  */
1830 int
1831 ndi_event_run_callbacks(ndi_event_hdl_t handle, dev_info_t *child_dip,
1832 	ddi_eventcookie_t cookie, void *bus_impldata)
1833 {
1834 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
1835 	ndi_event_callbacks_t *next, *cb;
1836 	int attributes;
1837 
1838 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1839 
1840 	/* if this is not our event, fail */
1841 	if (ndi_event_is_defined(handle, cookie, &attributes) !=
1842 	    NDI_SUCCESS) {
1843 
1844 		mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1845 		return (NDI_FAILURE);
1846 	}
1847 
1848 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1849 
1850 #ifdef NDI_EVENT_DEBUG
1851 	if (ndi_event_debug) {
1852 		cmn_err(CE_CONT, "ndi_event_run_callbacks:\n\t"
1853 		    "producer dip=%p (%s%d): cookie = %p, name = %s\n",
1854 		    (void *)ndi_event_hdl->ndi_evthdl_dip,
1855 		    ddi_node_name(ndi_event_hdl->ndi_evthdl_dip),
1856 		    ddi_get_instance(ndi_event_hdl->ndi_evthdl_dip),
1857 		    (void *)cookie,
1858 		    ndi_event_cookie_to_name(handle, cookie));
1859 	}
1860 #endif /* #ifdef NDI_EVENT_DEBUG */
1861 
1862 
1863 	/*
1864 	 * The callback handlers may call conversion functions.  The conversion
1865 	 * functions may hold the ndi_evthdl_mutex during execution.  Thus, to
1866 	 * avoid a recursive mutex problem, only the ndi_evthdl_cb_mutex is
1867 	 * held.  The ndi_evthdl_mutex is not held when running the callbacks.
1868 	 */
1869 	mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1870 
1871 	/* perform callbacks */
1872 	next = NDI_EVENT(cookie)->callback_list;
1873 	while (next != NULL) {
1874 
1875 		cb = next;
1876 		next = next->ndi_evtcb_next;
1877 
1878 		ASSERT(cb->ndi_evtcb_cookie == cookie);
1879 
1880 		if (attributes == NDI_EVENT_POST_TO_TGT &&
1881 		    child_dip != cb->ndi_evtcb_dip) {
1882 			continue;
1883 		}
1884 
1885 		cb->ndi_evtcb_callback(cb->ndi_evtcb_dip, cb->ndi_evtcb_cookie,
1886 		    cb->ndi_evtcb_arg, bus_impldata);
1887 
1888 #ifdef NDI_EVENT_DEBUG
1889 		if (ndi_event_debug) {
1890 			cmn_err(CE_CONT,
1891 			    "\t\tconsumer dip=%p (%s%d)\n",
1892 			    (void *)cb->ndi_evtcb_dip,
1893 			    ddi_node_name(cb->ndi_evtcb_dip),
1894 			    ddi_get_instance(cb->ndi_evtcb_dip));
1895 		}
1896 #endif
1897 
1898 	}
1899 
1900 	mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1901 
1902 #ifdef NDI_EVENT_DEBUG
1903 	if (ndi_event_debug) {
1904 		mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1905 		ndi_event_dump_hdl(ndi_event_hdl, "ndi_event_run_callbacks");
1906 		mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1907 	}
1908 #endif /* NDI_EVENT_DEBUG */
1909 
1910 	return (NDI_SUCCESS);
1911 }
1912 
1913 
1914 /*
1915  * perform one callback for a specified cookie and just one target
1916  */
1917 int
1918 ndi_event_do_callback(ndi_event_hdl_t handle, dev_info_t *child_dip,
1919 	ddi_eventcookie_t cookie, void *bus_impldata)
1920 {
1921 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
1922 	ndi_event_callbacks_t *next, *cb;
1923 	int attributes;
1924 
1925 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1926 
1927 	/* if this is not our event, fail */
1928 	if (ndi_event_is_defined(handle, cookie, &attributes) !=
1929 	    NDI_SUCCESS) {
1930 
1931 		mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1932 
1933 		return (NDI_FAILURE);
1934 	}
1935 
1936 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1937 
1938 #ifdef NDI_EVENT_DEBUG
1939 	if (ndi_event_debug) {
1940 		cmn_err(CE_CONT, "ndi_event_run_callbacks:\n\t"
1941 		    "producer dip=%p (%s%d): cookie = %p, name = %s\n",
1942 		    (void *)ndi_event_hdl->ndi_evthdl_dip,
1943 		    ddi_node_name(ndi_event_hdl->ndi_evthdl_dip),
1944 		    ddi_get_instance(ndi_event_hdl->ndi_evthdl_dip),
1945 		    (void *)cookie,
1946 		    ndi_event_cookie_to_name(handle, cookie));
1947 	}
1948 #endif
1949 
1950 
1951 	/*
1952 	 * we only grab the cb mutex because the callback handlers
1953 	 * may call the conversion functions which would cause a recursive
1954 	 * mutex problem
1955 	 */
1956 	mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1957 
1958 	/* perform callbacks */
1959 	for (next = NDI_EVENT(cookie)->callback_list; next != NULL; ) {
1960 		cb = next;
1961 		next = next->ndi_evtcb_next;
1962 
1963 		if (cb->ndi_evtcb_dip == child_dip) {
1964 			cb->ndi_evtcb_callback(cb->ndi_evtcb_dip,
1965 			    cb->ndi_evtcb_cookie, cb->ndi_evtcb_arg,
1966 			    bus_impldata);
1967 
1968 #ifdef NDI_EVENT_DEBUG
1969 			if (ndi_event_debug) {
1970 				cmn_err(CE_CONT,
1971 				    "\t\tconsumer dip=%p (%s%d)\n",
1972 				    (void *)cb->ndi_evtcb_dip,
1973 				    ddi_node_name(cb->ndi_evtcb_dip),
1974 				    ddi_get_instance(cb->ndi_evtcb_dip));
1975 			}
1976 #endif
1977 			break;
1978 		}
1979 	}
1980 
1981 	mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1982 
1983 #ifdef NDI_EVENT_DEBUG
1984 	if (ndi_event_debug) {
1985 		mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1986 		ndi_event_dump_hdl(ndi_event_hdl, "ndi_event_run_callbacks");
1987 		mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1988 	}
1989 #endif /* NDI_EVENT_DEBUG */
1990 
1991 	return (NDI_SUCCESS);
1992 }
1993 
1994 
1995 /*
1996  * ndi_event_tag_to_cookie: utility function to find an event cookie
1997  * given an event tag
1998  */
1999 ddi_eventcookie_t
2000 ndi_event_tag_to_cookie(ndi_event_hdl_t handle, int event_tag)
2001 {
2002 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
2003 	ndi_event_cookie_t *list;
2004 
2005 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
2006 
2007 	list = ndi_event_hdl->ndi_evthdl_cookie_list;
2008 	while (list != NULL) {
2009 		if (NDI_EVENT_TAG(list) == event_tag) {
2010 			mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
2011 			return ((ddi_eventcookie_t)list);
2012 		}
2013 
2014 		list = list->next_cookie;
2015 	}
2016 
2017 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
2018 	return (NULL);
2019 }
2020 
2021 /*
2022  * ndi_event_cookie_to_tag: utility function to find a event tag
2023  * given an event_cookie
2024  */
2025 int
2026 ndi_event_cookie_to_tag(ndi_event_hdl_t handle, ddi_eventcookie_t cookie)
2027 {
2028 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
2029 	ndi_event_cookie_t *list;
2030 
2031 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
2032 
2033 	list = ndi_event_hdl->ndi_evthdl_cookie_list;
2034 
2035 	while (list != NULL) {
2036 		if ((ddi_eventcookie_t)list == cookie) {
2037 			mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
2038 			return (NDI_EVENT_TAG(list));
2039 		}
2040 
2041 		list = list->next_cookie;
2042 	}
2043 
2044 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
2045 	return (NDI_FAILURE);
2046 
2047 }
2048 
2049 /*
2050  * ndi_event_cookie_to_name: utility function to find an event name
2051  * given an event_cookie
2052  */
2053 char *
2054 ndi_event_cookie_to_name(ndi_event_hdl_t handle, ddi_eventcookie_t cookie)
2055 {
2056 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
2057 	ndi_event_cookie_t *list;
2058 
2059 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
2060 
2061 	list = ndi_event_hdl->ndi_evthdl_cookie_list;
2062 
2063 	while (list != NULL) {
2064 		if (list == NDI_EVENT(cookie)) {
2065 			mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
2066 			return (NDI_EVENT_NAME(list));
2067 		}
2068 
2069 		list = list->next_cookie;
2070 	}
2071 
2072 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
2073 	return (NULL);
2074 }
2075 
2076 /*
2077  * ndi_event_tag_to_name: utility function to find an event name
2078  * given an event tag
2079  */
2080 char *
2081 ndi_event_tag_to_name(ndi_event_hdl_t handle, int event_tag)
2082 {
2083 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
2084 	ndi_event_cookie_t *list;
2085 
2086 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
2087 
2088 	list = ndi_event_hdl->ndi_evthdl_cookie_list;
2089 
2090 	while (list) {
2091 		if (NDI_EVENT_TAG(list) == event_tag) {
2092 			mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
2093 			return (NDI_EVENT_NAME(list));
2094 		}
2095 
2096 		list = list->next_cookie;
2097 	}
2098 
2099 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
2100 
2101 	return (NULL);
2102 }
2103 
2104 #ifdef NDI_EVENT_DEBUG
2105 void
2106 ndi_event_dump_hdl(struct ndi_event_hdl *hdl, char *location)
2107 {
2108 
2109 
2110 	ndi_event_callbacks_t *next;
2111 	ndi_event_cookie_t *list;
2112 
2113 	ASSERT(mutex_owned(&hdl->ndi_evthdl_mutex));
2114 	list = hdl->ndi_evthdl_cookie_list;
2115 
2116 	cmn_err(CE_CONT, "%s: event handle (%p): dip = %p (%s%d)\n",
2117 	    location, (void *)hdl, (void *)hdl->ndi_evthdl_dip,
2118 	    ddi_node_name(hdl->ndi_evthdl_dip),
2119 	    ddi_get_instance(hdl->ndi_evthdl_dip));
2120 	cmn_err(CE_CONT, "\thigh=%d other=%d n=%d\n",
2121 	    hdl->ndi_evthdl_high_plevels, hdl->ndi_evthdl_other_plevels,
2122 	    hdl->ndi_evthdl_n_events);
2123 
2124 	cmn_err(CE_CONT, "\tevent cookies:\n");
2125 	while (list) {
2126 		cmn_err(CE_CONT, "\t\ttag=%d name=%s p=%d a=%x dd=%p\n",
2127 		    NDI_EVENT_TAG(list), NDI_EVENT_NAME(list),
2128 		    NDI_EVENT_PLEVEL(list), NDI_EVENT_ATTRIBUTES(list),
2129 		    (void *)NDI_EVENT_DDIP(list));
2130 		cmn_err(CE_CONT, "\t\tcallbacks:\n");
2131 		for (next = list->callback_list; next != NULL;
2132 		    next = next->ndi_evtcb_next) {
2133 			cmn_err(CE_CONT,
2134 			    "\t\t  dip=%p (%s%d) cookie=%p arg=%p\n",
2135 			    (void*)next->ndi_evtcb_dip,
2136 			    ddi_driver_name(next->ndi_evtcb_dip),
2137 			    ddi_get_instance(next->ndi_evtcb_dip),
2138 			    (void *)next->ndi_evtcb_cookie,
2139 			    next->ndi_evtcb_arg);
2140 		}
2141 
2142 		list = list->next_cookie;
2143 	}
2144 
2145 	cmn_err(CE_CONT, "\n");
2146 }
2147 #endif
2148 
2149 int
2150 ndi_dev_is_prom_node(dev_info_t *dip)
2151 {
2152 	return (DEVI(dip)->devi_node_class == DDI_NC_PROM);
2153 }
2154 
2155 int
2156 ndi_dev_is_pseudo_node(dev_info_t *dip)
2157 {
2158 	/*
2159 	 * NOTE: this does NOT mean the pseudo branch of the device tree,
2160 	 * it means the node was created by software (DEVI_SID_NODEID |
2161 	 * DEVI_PSEUDO_NODEID) instead of being generated from a PROM node.
2162 	 */
2163 	return (DEVI(dip)->devi_node_class == DDI_NC_PSEUDO);
2164 }
2165 
2166 int
2167 ndi_dev_is_persistent_node(dev_info_t *dip)
2168 {
2169 	return ((DEVI(dip)->devi_node_attributes & DDI_PERSISTENT) != 0);
2170 }
2171 
2172 int
2173 i_ndi_dev_is_auto_assigned_node(dev_info_t *dip)
2174 {
2175 	return ((DEVI(dip)->devi_node_attributes &
2176 	    DDI_AUTO_ASSIGNED_NODEID) != 0);
2177 }
2178 
2179 void
2180 i_ndi_set_node_class(dev_info_t *dip, ddi_node_class_t c)
2181 {
2182 	DEVI(dip)->devi_node_class = c;
2183 }
2184 
2185 ddi_node_class_t
2186 i_ndi_get_node_class(dev_info_t *dip)
2187 {
2188 	return (DEVI(dip)->devi_node_class);
2189 }
2190 
2191 void
2192 i_ndi_set_node_attributes(dev_info_t *dip, int p)
2193 {
2194 	DEVI(dip)->devi_node_attributes = p;
2195 }
2196 
2197 int
2198 i_ndi_get_node_attributes(dev_info_t *dip)
2199 {
2200 	return (DEVI(dip)->devi_node_attributes);
2201 }
2202 
2203 void
2204 i_ndi_set_nodeid(dev_info_t *dip, int n)
2205 {
2206 	DEVI(dip)->devi_nodeid = n;
2207 }
2208 
2209 void
2210 ndi_set_acc_fault(ddi_acc_handle_t ah)
2211 {
2212 	i_ddi_acc_set_fault(ah);
2213 }
2214 
2215 void
2216 ndi_clr_acc_fault(ddi_acc_handle_t ah)
2217 {
2218 	i_ddi_acc_clr_fault(ah);
2219 }
2220 
2221 void
2222 ndi_set_dma_fault(ddi_dma_handle_t dh)
2223 {
2224 	i_ddi_dma_set_fault(dh);
2225 }
2226 
2227 void
2228 ndi_clr_dma_fault(ddi_dma_handle_t dh)
2229 {
2230 	i_ddi_dma_clr_fault(dh);
2231 }
2232 
2233 /*
2234  *  The default fault-handler, called when the event posted by
2235  *  ddi_dev_report_fault() reaches rootnex.
2236  */
2237 static void
2238 i_ddi_fault_handler(dev_info_t *dip, struct ddi_fault_event_data *fedp)
2239 {
2240 	ASSERT(fedp);
2241 
2242 	mutex_enter(&(DEVI(dip)->devi_lock));
2243 	if (!DEVI_IS_DEVICE_OFFLINE(dip)) {
2244 		switch (fedp->f_impact) {
2245 		case DDI_SERVICE_LOST:
2246 			DEVI_SET_DEVICE_DOWN(dip);
2247 			break;
2248 
2249 		case DDI_SERVICE_DEGRADED:
2250 			DEVI_SET_DEVICE_DEGRADED(dip);
2251 			break;
2252 
2253 		case DDI_SERVICE_UNAFFECTED:
2254 		default:
2255 			break;
2256 
2257 		case DDI_SERVICE_RESTORED:
2258 			DEVI_SET_DEVICE_UP(dip);
2259 			break;
2260 		}
2261 	}
2262 	mutex_exit(&(DEVI(dip)->devi_lock));
2263 }
2264 
2265 /*
2266  * The default fault-logger, called when the event posted by
2267  * ddi_dev_report_fault() reaches rootnex.
2268  */
2269 /*ARGSUSED*/
2270 static void
2271 i_ddi_fault_logger(dev_info_t *rdip, struct ddi_fault_event_data *fedp)
2272 {
2273 	ddi_devstate_t newstate;
2274 	const char *action;
2275 	const char *servstate;
2276 	const char *location;
2277 	int bad;
2278 	int changed;
2279 	int level;
2280 	int still;
2281 
2282 	ASSERT(fedp);
2283 
2284 	bad = 0;
2285 	switch (fedp->f_location) {
2286 	case DDI_DATAPATH_FAULT:
2287 		location = "in datapath to";
2288 		break;
2289 	case DDI_DEVICE_FAULT:
2290 		location = "in";
2291 		break;
2292 	case DDI_EXTERNAL_FAULT:
2293 		location = "external to";
2294 		break;
2295 	default:
2296 		location = "somewhere near";
2297 		bad = 1;
2298 		break;
2299 	}
2300 
2301 	newstate = ddi_get_devstate(fedp->f_dip);
2302 	switch (newstate) {
2303 	case DDI_DEVSTATE_OFFLINE:
2304 		servstate = "unavailable";
2305 		break;
2306 	case DDI_DEVSTATE_DOWN:
2307 		servstate = "unavailable";
2308 		break;
2309 	case DDI_DEVSTATE_QUIESCED:
2310 		servstate = "suspended";
2311 		break;
2312 	case DDI_DEVSTATE_DEGRADED:
2313 		servstate = "degraded";
2314 		break;
2315 	default:
2316 		servstate = "available";
2317 		break;
2318 	}
2319 
2320 	changed = (newstate != fedp->f_oldstate);
2321 	level = (newstate < fedp->f_oldstate) ? CE_WARN : CE_NOTE;
2322 	switch (fedp->f_impact) {
2323 	case DDI_SERVICE_LOST:
2324 	case DDI_SERVICE_DEGRADED:
2325 	case DDI_SERVICE_UNAFFECTED:
2326 		/* fault detected; service [still] <servstate> */
2327 		action = "fault detected";
2328 		still = !changed;
2329 		break;
2330 
2331 	case DDI_SERVICE_RESTORED:
2332 		if (newstate != DDI_DEVSTATE_UP) {
2333 			/* fault cleared; service still <servstate> */
2334 			action = "fault cleared";
2335 			still = 1;
2336 		} else if (changed) {
2337 			/* fault cleared; service <servstate> */
2338 			action = "fault cleared";
2339 			still = 0;
2340 		} else {
2341 			/* no fault; service <servstate> */
2342 			action = "no fault";
2343 			still = 0;
2344 		}
2345 		break;
2346 
2347 	default:
2348 		bad = 1;
2349 		break;
2350 	}
2351 
2352 	cmn_err(level, "!%s%d: %s %s device; service %s%s"+(bad|changed),
2353 	    ddi_driver_name(fedp->f_dip), ddi_get_instance(fedp->f_dip),
2354 	    bad ? "invalid report of fault" : action,
2355 	    location, still ? "still " : "", servstate);
2356 
2357 	cmn_err(level, "!%s%d: %s"+(bad|changed),
2358 	    ddi_driver_name(fedp->f_dip), ddi_get_instance(fedp->f_dip),
2359 	    fedp->f_message);
2360 }
2361 
2362 /*
2363  * Platform-settable pointers to fault handler and logger functions.
2364  * These are called by the default rootnex event-posting code when
2365  * a fault event reaches rootnex.
2366  */
2367 void (*plat_fault_handler)(dev_info_t *, struct ddi_fault_event_data *) =
2368 	i_ddi_fault_handler;
2369 void (*plat_fault_logger)(dev_info_t *, struct ddi_fault_event_data *) =
2370 	i_ddi_fault_logger;
2371 
2372 /*
2373  * Rootnex event definitions ...
2374  */
2375 enum rootnex_event_tags {
2376 	ROOTNEX_FAULT_EVENT
2377 };
2378 static ndi_event_hdl_t rootnex_event_hdl;
2379 static ndi_event_definition_t rootnex_event_set[] = {
2380 	{
2381 		ROOTNEX_FAULT_EVENT,
2382 		DDI_DEVI_FAULT_EVENT,
2383 		EPL_INTERRUPT,
2384 		NDI_EVENT_POST_TO_ALL
2385 	}
2386 };
2387 static ndi_event_set_t rootnex_events = {
2388 	NDI_EVENTS_REV1,
2389 	sizeof (rootnex_event_set) / sizeof (rootnex_event_set[0]),
2390 	rootnex_event_set
2391 };
2392 
2393 /*
2394  * Initialize rootnex event handle
2395  */
2396 void
2397 i_ddi_rootnex_init_events(dev_info_t *dip)
2398 {
2399 	if (ndi_event_alloc_hdl(dip, (ddi_iblock_cookie_t)(LOCK_LEVEL-1),
2400 	    &rootnex_event_hdl, NDI_SLEEP) == NDI_SUCCESS) {
2401 		if (ndi_event_bind_set(rootnex_event_hdl,
2402 		    &rootnex_events, NDI_SLEEP) != NDI_SUCCESS) {
2403 			(void) ndi_event_free_hdl(rootnex_event_hdl);
2404 			rootnex_event_hdl = NULL;
2405 		}
2406 	}
2407 }
2408 
2409 /*
2410  *      Event-handling functions for rootnex
2411  *      These provide the standard implementation of fault handling
2412  */
2413 /*ARGSUSED*/
2414 int
2415 i_ddi_rootnex_get_eventcookie(dev_info_t *dip, dev_info_t *rdip,
2416 	char *eventname, ddi_eventcookie_t *cookiep)
2417 {
2418 	if (rootnex_event_hdl == NULL)
2419 		return (NDI_FAILURE);
2420 	return (ndi_event_retrieve_cookie(rootnex_event_hdl, rdip, eventname,
2421 	    cookiep, NDI_EVENT_NOPASS));
2422 }
2423 
2424 /*ARGSUSED*/
2425 int
2426 i_ddi_rootnex_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
2427 	ddi_eventcookie_t eventid, void (*handler)(dev_info_t *dip,
2428 	ddi_eventcookie_t event, void *arg, void *impl_data), void *arg,
2429 	ddi_callback_id_t *cb_id)
2430 {
2431 	if (rootnex_event_hdl == NULL)
2432 		return (NDI_FAILURE);
2433 	return (ndi_event_add_callback(rootnex_event_hdl, rdip,
2434 	    eventid, handler, arg, NDI_SLEEP, cb_id));
2435 }
2436 
2437 /*ARGSUSED*/
2438 int
2439 i_ddi_rootnex_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
2440 {
2441 	if (rootnex_event_hdl == NULL)
2442 		return (NDI_FAILURE);
2443 
2444 	return (ndi_event_remove_callback(rootnex_event_hdl, cb_id));
2445 }
2446 
2447 /*ARGSUSED*/
2448 int
2449 i_ddi_rootnex_post_event(dev_info_t *dip, dev_info_t *rdip,
2450 	ddi_eventcookie_t eventid, void *impl_data)
2451 {
2452 	int tag;
2453 
2454 	if (rootnex_event_hdl == NULL)
2455 		return (NDI_FAILURE);
2456 
2457 	tag = ndi_event_cookie_to_tag(rootnex_event_hdl, eventid);
2458 	if (tag == ROOTNEX_FAULT_EVENT) {
2459 		(*plat_fault_handler)(rdip, impl_data);
2460 		(*plat_fault_logger)(rdip, impl_data);
2461 	}
2462 	return (ndi_event_run_callbacks(rootnex_event_hdl, rdip,
2463 	    eventid, impl_data));
2464 }
2465 
2466 /*
2467  * ndi_set_bus_private/ndi_get_bus_private:
2468  * Get/set device bus private data in devinfo.
2469  */
2470 void
2471 ndi_set_bus_private(dev_info_t *dip, boolean_t up, uint32_t port_type,
2472     void *data)
2473 {
2474 	if (up) {
2475 		DEVI(dip)->devi_bus.port_up.info.port.type = port_type;
2476 		DEVI(dip)->devi_bus.port_up.priv_p = data;
2477 	} else {
2478 		DEVI(dip)->devi_bus.port_down.info.port.type = port_type;
2479 		DEVI(dip)->devi_bus.port_down.priv_p = data;
2480 	}
2481 }
2482 
2483 void *
2484 ndi_get_bus_private(dev_info_t *dip, boolean_t up)
2485 {
2486 	if (up)
2487 		return (DEVI(dip)->devi_bus.port_up.priv_p);
2488 	else
2489 		return (DEVI(dip)->devi_bus.port_down.priv_p);
2490 }
2491 
2492 boolean_t
2493 ndi_port_type(dev_info_t *dip, boolean_t up, uint32_t port_type)
2494 {
2495 	if (up) {
2496 		return ((DEVI(dip)->devi_bus.port_up.info.port.type) ==
2497 		    port_type);
2498 	} else {
2499 		return ((DEVI(dip)->devi_bus.port_down.info.port.type) ==
2500 		    port_type);
2501 	}
2502 }
2503