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