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