xref: /titanic_44/usr/src/uts/common/os/sunndi.c (revision de860bd9529e1034e1666f74f7bc0ec8cd5ca701)
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 #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 	ddi_prop_remove_all_common(dip, (int)DDI_PROP_HW_DEF);
167 }
168 
169 /*
170  * Post an event notification to nexus driver responsible for handling
171  * the event.  The responsible nexus is defined in the cookie passed in as
172  * the third parameter.
173  * The dip parameter is an artifact of an older implementation in which all
174  * requests to remove an eventcall would bubble up the tree.  Today, this
175  * parameter is ignored.
176  * Input Parameters:
177  *	dip 	- Ignored.
178  *	rdip 	- device driver posting the event
179  *	cookie	- valid ddi_eventcookie_t, obtained by caller prior to
180  *		  invocation of this routine
181  *	impl_data - used by framework
182  */
183 /*ARGSUSED*/
184 int
185 ndi_post_event(dev_info_t *dip, dev_info_t *rdip,
186 		ddi_eventcookie_t cookie, void *impl_data)
187 {
188 	dev_info_t *ddip;
189 
190 	ASSERT(cookie);
191 	ddip = NDI_EVENT_DDIP(cookie);
192 
193 	/*
194 	 * perform sanity checks.  These conditions should never be true.
195 	 */
196 
197 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops != NULL);
198 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_6);
199 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->bus_post_event != NULL);
200 
201 	/*
202 	 * post the event to the responsible ancestor
203 	 */
204 	return ((*(DEVI(ddip)->devi_ops->devo_bus_ops->bus_post_event))
205 		(ddip, rdip, cookie, impl_data));
206 }
207 
208 /*
209  * Calls the bus nexus driver's implementation of the
210  * (*bus_remove_eventcall)() interface.
211  */
212 int
213 ndi_busop_remove_eventcall(dev_info_t *ddip, ddi_callback_id_t id)
214 {
215 
216 	ASSERT(id);
217 	/* check for a correct revno before calling up the device tree. */
218 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops != NULL);
219 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_6);
220 
221 	if (DEVI(ddip)->devi_ops->devo_bus_ops->bus_remove_eventcall == NULL)
222 		return (DDI_FAILURE);
223 
224 	/*
225 	 * request responsible nexus to remove the eventcall
226 	 */
227 	return ((*(DEVI(ddip)->devi_ops->devo_bus_ops->bus_remove_eventcall))
228 		(ddip, id));
229 }
230 
231 /*
232  * Calls the bus nexus driver's implementation of the
233  * (*bus_add_eventcall)() interface.  The dip parameter is an
234  * artifact of an older implementation in which all requests to
235  * add an eventcall would bubble up the tree.  Today, this parameter is
236  * ignored.
237  */
238 /*ARGSUSED*/
239 int
240 ndi_busop_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
241 		ddi_eventcookie_t cookie, void (*callback)(), void *arg,
242 		ddi_callback_id_t *cb_id)
243 {
244 	dev_info_t *ddip = (dev_info_t *)NDI_EVENT_DDIP(cookie);
245 
246 	/*
247 	 * check for a correct revno before calling up the device tree.
248 	 */
249 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops != NULL);
250 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_6);
251 
252 	if (DEVI(ddip)->devi_ops->devo_bus_ops->bus_add_eventcall == NULL)
253 		return (DDI_FAILURE);
254 
255 	/*
256 	 * request responsible ancestor to add the eventcall
257 	 */
258 	return ((*(DEVI(ddip)->devi_ops->devo_bus_ops->bus_add_eventcall))
259 		(ddip, rdip, cookie, callback, arg, cb_id));
260 }
261 
262 /*
263  * Calls the bus nexus driver's implementation of the
264  * (*bus_get_eventcookie)() interface up the device tree hierarchy.
265  */
266 int
267 ndi_busop_get_eventcookie(dev_info_t *dip, dev_info_t *rdip, char *name,
268 		ddi_eventcookie_t *event_cookiep)
269 {
270 	dev_info_t *pdip = (dev_info_t *)DEVI(dip)->devi_parent;
271 
272 	/* Can not be called from rootnex. */
273 	ASSERT(pdip);
274 
275 	/*
276 	 * check for a correct revno before calling up the device tree.
277 	 */
278 	ASSERT(DEVI(pdip)->devi_ops->devo_bus_ops != NULL);
279 
280 	if ((DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_6) ||
281 	    (DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie == NULL)) {
282 #ifdef DEBUG
283 		if ((DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev >=
284 		    BUSO_REV_3) &&
285 		    (DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie)) {
286 			cmn_err(CE_WARN,
287 			    "Warning: %s%d busops_rev=%d no longer supported"
288 			    " by the NDI event framework.\nBUSO_REV_6 or "
289 			    "greater must be used.",
290 			    DEVI(pdip)->devi_binding_name,
291 			    DEVI(pdip)->devi_instance,
292 			    DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev);
293 		}
294 #endif /* DEBUG */
295 
296 		return (ndi_busop_get_eventcookie(pdip, rdip, name,
297 			    event_cookiep));
298 	}
299 
300 	return ((*(DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie))
301 		(pdip, rdip, name, event_cookiep));
302 }
303 
304 /*
305  * Copy in the devctl IOCTL data and return a handle to
306  * the data.
307  */
308 int
309 ndi_dc_allochdl(void *iocarg, struct devctl_iocdata **rdcp)
310 {
311 	struct devctl_iocdata *dcp;
312 	char *cpybuf;
313 
314 	ASSERT(rdcp != NULL);
315 
316 	dcp = kmem_zalloc(sizeof (*dcp), KM_SLEEP);
317 
318 	if (get_udatamodel() == DATAMODEL_NATIVE) {
319 		if (copyin(iocarg, dcp, sizeof (*dcp)) != 0) {
320 			kmem_free(dcp, sizeof (*dcp));
321 			return (NDI_FAULT);
322 		}
323 	}
324 #ifdef _SYSCALL32_IMPL
325 	else {
326 		struct devctl_iocdata32 dcp32;
327 
328 		if (copyin(iocarg, &dcp32, sizeof (dcp32)) != 0) {
329 			kmem_free(dcp, sizeof (*dcp));
330 			return (NDI_FAULT);
331 		}
332 		dcp->cmd = (uint_t)dcp32.cmd;
333 		dcp->flags = (uint_t)dcp32.flags;
334 		dcp->cpyout_buf = (uint_t *)(uintptr_t)dcp32.cpyout_buf;
335 		dcp->nvl_user = (nvlist_t *)(uintptr_t)dcp32.nvl_user;
336 		dcp->nvl_usersz = (size_t)dcp32.nvl_usersz;
337 		dcp->c_nodename = (char *)(uintptr_t)dcp32.c_nodename;
338 		dcp->c_unitaddr = (char *)(uintptr_t)dcp32.c_unitaddr;
339 	}
340 #endif
341 	if (dcp->c_nodename != NULL) {
342 		cpybuf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
343 		if (copyinstr(dcp->c_nodename, cpybuf, MAXNAMELEN, 0) != 0) {
344 			kmem_free(cpybuf, MAXNAMELEN);
345 			kmem_free(dcp, sizeof (*dcp));
346 			return (NDI_FAULT);
347 		}
348 		cpybuf[MAXNAMELEN - 1] = '\0';
349 		dcp->c_nodename = cpybuf;
350 	}
351 
352 	if (dcp->c_unitaddr != NULL) {
353 		cpybuf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
354 		if (copyinstr(dcp->c_unitaddr, cpybuf, MAXNAMELEN, 0) != 0) {
355 			kmem_free(cpybuf, MAXNAMELEN);
356 			if (dcp->c_nodename != NULL)
357 				kmem_free(dcp->c_nodename, MAXNAMELEN);
358 			kmem_free(dcp, sizeof (*dcp));
359 			return (NDI_FAULT);
360 		}
361 		cpybuf[MAXNAMELEN - 1] = '\0';
362 		dcp->c_unitaddr = cpybuf;
363 	}
364 
365 	/*
366 	 * copyin and unpack a user defined nvlist if one was passed
367 	 */
368 	if (dcp->nvl_user != NULL) {
369 		if (dcp->nvl_usersz == 0) {
370 			if (dcp->c_nodename != NULL)
371 				kmem_free(dcp->c_nodename, MAXNAMELEN);
372 			if (dcp->c_unitaddr != NULL)
373 				kmem_free(dcp->c_unitaddr, MAXNAMELEN);
374 			kmem_free(dcp, sizeof (*dcp));
375 			return (NDI_FAILURE);
376 		}
377 		cpybuf = kmem_alloc(dcp->nvl_usersz, KM_SLEEP);
378 		if (copyin(dcp->nvl_user, cpybuf, dcp->nvl_usersz) != 0) {
379 			kmem_free(cpybuf, dcp->nvl_usersz);
380 			if (dcp->c_nodename != NULL)
381 				kmem_free(dcp->c_nodename, MAXNAMELEN);
382 			if (dcp->c_unitaddr != NULL)
383 				kmem_free(dcp->c_unitaddr, MAXNAMELEN);
384 			kmem_free(dcp, sizeof (*dcp));
385 			return (NDI_FAULT);
386 		}
387 
388 		if (nvlist_unpack(cpybuf, dcp->nvl_usersz, &dcp->nvl_user,
389 		    KM_SLEEP)) {
390 			kmem_free(cpybuf, dcp->nvl_usersz);
391 			if (dcp->c_nodename != NULL)
392 				kmem_free(dcp->c_nodename, MAXNAMELEN);
393 			if (dcp->c_unitaddr != NULL)
394 				kmem_free(dcp->c_unitaddr, MAXNAMELEN);
395 			kmem_free(dcp, sizeof (*dcp));
396 			return (NDI_FAULT);
397 		}
398 		/*
399 		 * free the buffer containing the packed nvlist
400 		 */
401 		kmem_free(cpybuf, dcp->nvl_usersz);
402 
403 	}
404 
405 	*rdcp = dcp;
406 	return (NDI_SUCCESS);
407 }
408 
409 /*
410  * free all space allocated to a handle.
411  */
412 void
413 ndi_dc_freehdl(struct devctl_iocdata *dcp)
414 {
415 	ASSERT(dcp != NULL);
416 
417 	if (dcp->c_nodename != NULL)
418 		kmem_free(dcp->c_nodename, MAXNAMELEN);
419 
420 	if (dcp->c_unitaddr != NULL)
421 		kmem_free(dcp->c_unitaddr, MAXNAMELEN);
422 
423 	if (dcp->nvl_user != NULL)
424 		nvlist_free(dcp->nvl_user);
425 
426 	kmem_free(dcp, sizeof (*dcp));
427 }
428 
429 char *
430 ndi_dc_getname(struct devctl_iocdata *dcp)
431 {
432 	ASSERT(dcp != NULL);
433 	return (dcp->c_nodename);
434 
435 }
436 
437 char *
438 ndi_dc_getaddr(struct devctl_iocdata *dcp)
439 {
440 	ASSERT(dcp != NULL);
441 	return (dcp->c_unitaddr);
442 }
443 
444 nvlist_t *
445 ndi_dc_get_ap_data(struct devctl_iocdata *dcp)
446 {
447 	ASSERT(dcp != NULL);
448 
449 	return (dcp->nvl_user);
450 }
451 
452 /*
453  * Transition the child named by "devname@devaddr" to the online state.
454  * For use by a driver's DEVCTL_DEVICE_ONLINE handler.
455  */
456 int
457 ndi_devctl_device_online(dev_info_t *dip, struct devctl_iocdata *dcp,
458 	uint_t flags)
459 {
460 	int	rval;
461 	char	*name;
462 	dev_info_t *rdip;
463 
464 	if (ndi_dc_getname(dcp) == NULL || ndi_dc_getaddr(dcp) == NULL)
465 		return (EINVAL);
466 
467 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
468 	(void) snprintf(name, MAXNAMELEN, "%s@%s",
469 		ndi_dc_getname(dcp), ndi_dc_getaddr(dcp));
470 
471 	if ((rval = ndi_devi_config_one(dip, name, &rdip,
472 	    flags | NDI_DEVI_ONLINE | NDI_CONFIG)) == NDI_SUCCESS) {
473 		ndi_rele_devi(rdip);
474 
475 		/*
476 		 * Invalidate devfs cached directory contents. For the checks
477 		 * in the "if" condition see the comment in ndi_devi_online().
478 		 */
479 		if (i_ddi_devi_attached(dip) && !DEVI_BUSY_OWNED(dip))
480 			(void) devfs_clean(dip, NULL, 0);
481 
482 	} else if (rval == NDI_BUSY) {
483 		rval = EBUSY;
484 	} else if (rval == NDI_FAILURE) {
485 		rval = EIO;
486 	}
487 
488 	NDI_DEBUG(flags, (CE_CONT, "%s%d: online: %s: %s\n",
489 		ddi_driver_name(dip), ddi_get_instance(dip), name,
490 		((rval == NDI_SUCCESS) ? "ok" : "failed")));
491 
492 	kmem_free(name, MAXNAMELEN);
493 
494 	return (rval);
495 }
496 
497 /*
498  * Transition the child named by "devname@devaddr" to the offline state.
499  * For use by a driver's DEVCTL_DEVICE_OFFLINE handler.
500  */
501 int
502 ndi_devctl_device_offline(dev_info_t *dip, struct devctl_iocdata *dcp,
503 	uint_t flags)
504 {
505 	int	rval;
506 	char	*name;
507 
508 	if (ndi_dc_getname(dcp) == NULL || ndi_dc_getaddr(dcp) == NULL)
509 		return (EINVAL);
510 
511 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
512 	(void) snprintf(name, MAXNAMELEN, "%s@%s",
513 		ndi_dc_getname(dcp), ndi_dc_getaddr(dcp));
514 
515 	(void) devfs_clean(dip, name, DV_CLEAN_FORCE);
516 	rval = ndi_devi_unconfig_one(dip, name, NULL,
517 	    flags | NDI_DEVI_OFFLINE);
518 
519 	if (rval == NDI_BUSY) {
520 		rval = EBUSY;
521 	} else if (rval == NDI_FAILURE) {
522 		rval = EIO;
523 	}
524 
525 	NDI_DEBUG(flags, (CE_CONT, "%s%d: offline: %s: %s\n",
526 		ddi_driver_name(dip), ddi_get_instance(dip), name,
527 		(rval == NDI_SUCCESS) ? "ok" : "failed"));
528 
529 	kmem_free(name, MAXNAMELEN);
530 
531 	return (rval);
532 }
533 
534 /*
535  * Remove the child named by "devname@devaddr".
536  * For use by a driver's DEVCTL_DEVICE_REMOVE handler.
537  */
538 int
539 ndi_devctl_device_remove(dev_info_t *dip, struct devctl_iocdata *dcp,
540 	uint_t flags)
541 {
542 	int	rval;
543 	char	*name;
544 
545 	if (ndi_dc_getname(dcp) == NULL || ndi_dc_getaddr(dcp) == NULL)
546 		return (EINVAL);
547 
548 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
549 	(void) snprintf(name, MAXNAMELEN, "%s@%s",
550 		ndi_dc_getname(dcp), ndi_dc_getaddr(dcp));
551 
552 	(void) devfs_clean(dip, name, DV_CLEAN_FORCE);
553 
554 	rval = ndi_devi_unconfig_one(dip, name, NULL, flags | NDI_DEVI_REMOVE);
555 
556 	if (rval == NDI_BUSY) {
557 		rval = EBUSY;
558 	} else if (rval == NDI_FAILURE) {
559 		rval = EIO;
560 	}
561 
562 	NDI_DEBUG(flags, (CE_CONT, "%s%d: remove: %s: %s\n",
563 		ddi_driver_name(dip), ddi_get_instance(dip), name,
564 		(rval == NDI_SUCCESS) ? "ok" : "failed"));
565 
566 	kmem_free(name, MAXNAMELEN);
567 
568 	return (rval);
569 }
570 
571 /*
572  * Return devctl state of the child named by "name@addr".
573  * For use by a driver's DEVCTL_DEVICE_GETSTATE handler.
574  */
575 int
576 ndi_devctl_device_getstate(dev_info_t *parent, struct devctl_iocdata *dcp,
577 	uint_t *state)
578 {
579 	dev_info_t *dip;
580 	char *name, *addr;
581 	char *devname;
582 	int devnamelen;
583 	int circ;
584 
585 	if (parent == NULL ||
586 	    ((name = ndi_dc_getname(dcp)) == NULL) ||
587 	    ((addr = ndi_dc_getaddr(dcp)) == NULL))
588 		return (NDI_FAILURE);
589 
590 	devnamelen = strlen(name) + strlen(addr) + 2;
591 	devname = kmem_alloc(devnamelen, KM_SLEEP);
592 	if (strlen(addr) > 0) {
593 		(void) snprintf(devname, devnamelen, "%s@%s", name, addr);
594 	} else {
595 		(void) snprintf(devname, devnamelen, "%s", name);
596 	}
597 
598 	ndi_devi_enter(parent, &circ);
599 
600 	dip = ndi_devi_findchild(parent, devname);
601 	kmem_free(devname, devnamelen);
602 
603 	if (dip == NULL) {
604 		ndi_devi_exit(parent, circ);
605 		return (NDI_FAILURE);
606 	}
607 
608 	mutex_enter(&(DEVI(dip)->devi_lock));
609 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
610 		*state = DEVICE_OFFLINE;
611 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
612 		*state = DEVICE_DOWN;
613 	} else {
614 		*state = DEVICE_ONLINE;
615 		if (devi_stillreferenced(dip) == DEVI_REFERENCED)
616 			*state |= DEVICE_BUSY;
617 	}
618 
619 	mutex_exit(&(DEVI(dip)->devi_lock));
620 	ndi_devi_exit(parent, circ);
621 
622 	return (NDI_SUCCESS);
623 }
624 
625 /*
626  * return the current state of the device "dip"
627  *
628  * recommend using ndi_devctl_ioctl() or
629  * ndi_devctl_device_getstate() instead
630  */
631 int
632 ndi_dc_return_dev_state(dev_info_t *dip, struct devctl_iocdata *dcp)
633 {
634 	dev_info_t *pdip;
635 	uint_t devstate = 0;
636 	int circ;
637 
638 	if ((dip == NULL) || (dcp == NULL))
639 		return (NDI_FAILURE);
640 
641 	pdip = ddi_get_parent(dip);
642 
643 	ndi_devi_enter(pdip, &circ);
644 	mutex_enter(&(DEVI(dip)->devi_lock));
645 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
646 		devstate = DEVICE_OFFLINE;
647 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
648 		devstate = DEVICE_DOWN;
649 	} else {
650 		devstate = DEVICE_ONLINE;
651 		if (devi_stillreferenced(dip) == DEVI_REFERENCED)
652 			devstate |= DEVICE_BUSY;
653 	}
654 
655 	mutex_exit(&(DEVI(dip)->devi_lock));
656 	ndi_devi_exit(pdip, circ);
657 
658 	if (copyout(&devstate, dcp->cpyout_buf, sizeof (uint_t)) != 0)
659 		return (NDI_FAULT);
660 
661 	return (NDI_SUCCESS);
662 }
663 
664 /*
665  * Return device's bus state
666  * For use by a driver's DEVCTL_BUS_GETSTATE handler.
667  */
668 int
669 ndi_devctl_bus_getstate(dev_info_t *dip, struct devctl_iocdata *dcp,
670 	uint_t *state)
671 {
672 	if ((dip == NULL) || (dcp == NULL))
673 		return (NDI_FAILURE);
674 
675 	return (ndi_get_bus_state(dip, state));
676 }
677 
678 /*
679  * Generic devctl ioctl handler
680  */
681 int
682 ndi_devctl_ioctl(dev_info_t *dip, int cmd, intptr_t arg, int mode, uint_t flags)
683 {
684 	_NOTE(ARGUNUSED(mode))
685 	struct devctl_iocdata *dcp;
686 	uint_t state;
687 	int rval = ENOTTY;
688 
689 	/*
690 	 * read devctl ioctl data
691 	 */
692 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
693 		return (EFAULT);
694 
695 	switch (cmd) {
696 
697 	case DEVCTL_BUS_GETSTATE:
698 		rval = ndi_devctl_bus_getstate(dip, dcp, &state);
699 		if (rval == NDI_SUCCESS) {
700 			if (copyout(&state, dcp->cpyout_buf,
701 			    sizeof (uint_t)) != 0)
702 				rval = NDI_FAULT;
703 		}
704 		break;
705 
706 	case DEVCTL_DEVICE_ONLINE:
707 		rval = ndi_devctl_device_online(dip, dcp, flags);
708 		break;
709 
710 	case DEVCTL_DEVICE_OFFLINE:
711 		rval = ndi_devctl_device_offline(dip, dcp, flags);
712 		break;
713 
714 	case DEVCTL_DEVICE_GETSTATE:
715 		rval = ndi_devctl_device_getstate(dip, dcp, &state);
716 		if (rval == NDI_SUCCESS) {
717 			if (copyout(&state, dcp->cpyout_buf,
718 			    sizeof (uint_t)) != 0)
719 				rval = NDI_FAULT;
720 		}
721 		break;
722 
723 	case DEVCTL_DEVICE_REMOVE:
724 		rval = ndi_devctl_device_remove(dip, dcp, flags);
725 		break;
726 
727 	case DEVCTL_BUS_DEV_CREATE:
728 		rval = ndi_dc_devi_create(dcp, dip, 0, NULL);
729 		break;
730 
731 	/*
732 	 * ioctls for which a generic implementation makes no sense
733 	 */
734 	case DEVCTL_BUS_RESET:
735 	case DEVCTL_BUS_RESETALL:
736 	case DEVCTL_DEVICE_RESET:
737 	case DEVCTL_AP_CONNECT:
738 	case DEVCTL_AP_DISCONNECT:
739 	case DEVCTL_AP_INSERT:
740 	case DEVCTL_AP_REMOVE:
741 	case DEVCTL_AP_CONFIGURE:
742 	case DEVCTL_AP_UNCONFIGURE:
743 	case DEVCTL_AP_GETSTATE:
744 	case DEVCTL_AP_CONTROL:
745 	case DEVCTL_BUS_QUIESCE:
746 	case DEVCTL_BUS_UNQUIESCE:
747 		rval = ENOTSUP;
748 		break;
749 	}
750 
751 	ndi_dc_freehdl(dcp);
752 	return (rval);
753 }
754 
755 /*
756  * Copyout the state of the Attachment Point "ap" to the requesting
757  * user process.
758  */
759 int
760 ndi_dc_return_ap_state(devctl_ap_state_t *ap, struct devctl_iocdata *dcp)
761 {
762 	if ((ap == NULL) || (dcp == NULL))
763 		return (NDI_FAILURE);
764 
765 
766 	if (get_udatamodel() == DATAMODEL_NATIVE) {
767 		if (copyout(ap, dcp->cpyout_buf,
768 			sizeof (devctl_ap_state_t)) != 0)
769 		    return (NDI_FAULT);
770 	}
771 #ifdef _SYSCALL32_IMPL
772 	else {
773 		struct devctl_ap_state32 ap_state32;
774 
775 		ap_state32.ap_rstate = ap->ap_rstate;
776 		ap_state32.ap_ostate = ap->ap_ostate;
777 		ap_state32.ap_condition = ap->ap_condition;
778 		ap_state32.ap_error_code = ap->ap_error_code;
779 		ap_state32.ap_in_transition = ap->ap_in_transition;
780 		ap_state32.ap_last_change = (time32_t)ap->ap_last_change;
781 		if (copyout(&ap_state32, dcp->cpyout_buf,
782 			sizeof (devctl_ap_state32_t)) != 0)
783 		    return (NDI_FAULT);
784 	}
785 #endif
786 
787 	return (NDI_SUCCESS);
788 }
789 
790 /*
791  * Copyout the bus state of the bus nexus device "dip" to the requesting
792  * user process.
793  */
794 int
795 ndi_dc_return_bus_state(dev_info_t *dip, struct devctl_iocdata *dcp)
796 {
797 	uint_t devstate = 0;
798 
799 	if ((dip == NULL) || (dcp == NULL))
800 		return (NDI_FAILURE);
801 
802 	if (ndi_get_bus_state(dip, &devstate) != NDI_SUCCESS)
803 		return (NDI_FAILURE);
804 
805 	if (copyout(&devstate, dcp->cpyout_buf, sizeof (uint_t)) != 0)
806 		return (NDI_FAULT);
807 
808 	return (NDI_SUCCESS);
809 }
810 
811 static int
812 i_dc_devi_create(struct devctl_iocdata *, dev_info_t *, dev_info_t **);
813 
814 /*
815  * create a child device node given the property definitions
816  * supplied by the userland process
817  */
818 int
819 ndi_dc_devi_create(struct devctl_iocdata *dcp, dev_info_t *pdip, int flags,
820     dev_info_t **rdip)
821 {
822 	dev_info_t *cdip;
823 	int rv, circular = 0;
824 	char devnm[MAXNAMELEN];
825 	int nmlen;
826 
827 	/*
828 	 * The child device may have been pre-constructed by an earlier
829 	 * call to this function with the flag DEVCTL_CONSTRUCT set.
830 	 */
831 
832 	if ((cdip = (rdip != NULL) ? *rdip : NULL) == NULL)
833 		if ((rv = i_dc_devi_create(dcp, pdip, &cdip)) != 0)
834 			return (rv);
835 
836 	ASSERT(cdip != NULL);
837 
838 	/*
839 	 * Return the device node partially constructed if the
840 	 * DEVCTL_CONSTRUCT flag is set.
841 	 */
842 	if (flags & DEVCTL_CONSTRUCT) {
843 		if (rdip == NULL) {
844 			(void) ndi_devi_free(cdip);
845 			return (EINVAL);
846 		}
847 		*rdip = cdip;
848 		return (0);
849 	}
850 
851 	/*
852 	 * Bring the node up to a named but OFFLINE state.  The calling
853 	 * application will need to manage the node from here on.
854 	 */
855 	if (dcp->flags & DEVCTL_OFFLINE) {
856 		/*
857 		 * In the unlikely event that the dip was somehow attached by
858 		 * the userland process (and device contracts or LDI opens
859 		 * were registered against the dip) after it was created by
860 		 * a previous DEVCTL_CONSTRUCT call, we start notify
861 		 * proceedings on this dip. Note that we don't need to
862 		 * return the dip after a failure of the notify since
863 		 * for a contract or LDI handle to be created the dip was
864 		 * already available to the user.
865 		 */
866 		if (e_ddi_offline_notify(cdip) == DDI_FAILURE) {
867 			return (EBUSY);
868 		}
869 
870 		/*
871 		 * hand set the OFFLINE flag to prevent any asynchronous
872 		 * autoconfiguration operations from attaching this node.
873 		 */
874 		mutex_enter(&(DEVI(cdip)->devi_lock));
875 		DEVI_SET_DEVICE_OFFLINE(cdip);
876 		mutex_exit(&(DEVI(cdip)->devi_lock));
877 
878 		e_ddi_offline_finalize(cdip, DDI_SUCCESS);
879 
880 		rv = ndi_devi_bind_driver(cdip, flags);
881 		if (rv != NDI_SUCCESS) {
882 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
883 			return (ENXIO);
884 		}
885 
886 		/*
887 		 * remove the dev_info node if it failed to bind to a
888 		 * driver above.
889 		 */
890 		if (i_ddi_node_state(cdip) < DS_BOUND) {
891 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
892 			return (ENXIO);
893 		}
894 
895 		/*
896 		 * add the node to the per-driver list and INITCHILD it
897 		 * to give it a name.
898 		 */
899 		ndi_devi_enter(pdip, &circular);
900 		if ((rv = ddi_initchild(pdip, cdip)) != DDI_SUCCESS) {
901 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
902 			ndi_devi_exit(pdip, circular);
903 			return (EINVAL);
904 		}
905 		ndi_devi_exit(pdip, circular);
906 
907 	} else {
908 		/*
909 		 * Attempt to bring the device ONLINE. If the request to
910 		 * fails, remove the dev_info node.
911 		 */
912 		if (ndi_devi_online(cdip, NDI_ONLINE_ATTACH) != NDI_SUCCESS) {
913 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
914 			return (ENXIO);
915 		}
916 
917 		/*
918 		 * if the node was successfully added but there was
919 		 * no driver available for the device, remove the node
920 		 */
921 		if (i_ddi_node_state(cdip) < DS_BOUND) {
922 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
923 			return (ENODEV);
924 		}
925 	}
926 
927 	/*
928 	 * return a handle to the child device
929 	 * copy out the name of the newly attached child device if
930 	 * the IOCTL request has provided a copyout buffer.
931 	 */
932 	if (rdip != NULL)
933 		*rdip = cdip;
934 
935 	if (dcp->cpyout_buf == NULL)
936 		return (0);
937 
938 	ASSERT(ddi_node_name(cdip) != NULL);
939 	ASSERT(ddi_get_name_addr(cdip) != NULL);
940 
941 	nmlen = snprintf(devnm, MAXNAMELEN, "%s@%s",
942 	    ddi_node_name(cdip), ddi_get_name_addr(cdip));
943 
944 	if (copyout(&devnm, dcp->cpyout_buf, nmlen) != 0) {
945 		(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
946 		return (EFAULT);
947 	}
948 	return (0);
949 }
950 
951 static int
952 i_dc_devi_create(struct devctl_iocdata *dcp, dev_info_t *pdip,
953     dev_info_t **rdip)
954 {
955 
956 	dev_info_t *cdip;
957 	char *cname = NULL;
958 	nvlist_t *nvlp = dcp->nvl_user;
959 	nvpair_t *npp;
960 	char *np;
961 	int rv = 0;
962 
963 	ASSERT(rdip != NULL && *rdip == NULL);
964 
965 	if ((nvlp == NULL) ||
966 	    (nvlist_lookup_string(nvlp, DC_DEVI_NODENAME, &cname) != 0))
967 		return (EINVAL);
968 
969 	/*
970 	 * construct a new dev_info node with a user-provided nodename
971 	 */
972 	ndi_devi_alloc_sleep(pdip, cname, (pnode_t)DEVI_SID_NODEID, &cdip);
973 
974 	/*
975 	 * create hardware properties for each member in the property
976 	 * list.
977 	 */
978 	for (npp = nvlist_next_nvpair(nvlp, NULL); (npp != NULL && !rv);
979 	    npp = nvlist_next_nvpair(nvlp, npp)) {
980 
981 		np = nvpair_name(npp);
982 
983 		/*
984 		 * skip the nodename property
985 		 */
986 		if (strcmp(np, DC_DEVI_NODENAME) == 0)
987 			continue;
988 
989 		switch (nvpair_type(npp)) {
990 
991 		case DATA_TYPE_INT32: {
992 			int32_t prop_val;
993 
994 			if ((rv = nvpair_value_int32(npp, &prop_val)) != 0)
995 				break;
996 
997 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip, np,
998 			    (int)prop_val);
999 			break;
1000 		}
1001 
1002 		case DATA_TYPE_STRING: {
1003 			char *prop_val;
1004 
1005 			if ((rv = nvpair_value_string(npp, &prop_val)) != 0)
1006 				break;
1007 
1008 			(void) ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
1009 			    np, prop_val);
1010 			break;
1011 		}
1012 
1013 		case DATA_TYPE_BYTE_ARRAY: {
1014 			uchar_t *val;
1015 			uint_t nelms;
1016 
1017 			if ((rv = nvpair_value_byte_array(npp, &val,
1018 			    &nelms)) != 0)
1019 				break;
1020 
1021 			(void) ndi_prop_update_byte_array(DDI_DEV_T_NONE,
1022 			    cdip, np, (uchar_t *)val, nelms);
1023 			break;
1024 		}
1025 
1026 		case DATA_TYPE_INT32_ARRAY: {
1027 			int32_t *val;
1028 			uint_t nelms;
1029 
1030 			if ((rv = nvpair_value_int32_array(npp, &val,
1031 			    &nelms)) != 0)
1032 				break;
1033 
1034 			(void) ndi_prop_update_int_array(DDI_DEV_T_NONE,
1035 			    cdip, np, val, nelms);
1036 			break;
1037 		}
1038 
1039 		case DATA_TYPE_STRING_ARRAY: {
1040 			char **val;
1041 			uint_t nelms;
1042 
1043 			if ((rv = nvpair_value_string_array(npp, &val,
1044 			    &nelms)) != 0)
1045 				break;
1046 
1047 			(void) ndi_prop_update_string_array(DDI_DEV_T_NONE,
1048 			    cdip, np, val, nelms);
1049 			break;
1050 		}
1051 
1052 		/*
1053 		 * unsupported property data type
1054 		 */
1055 		default:
1056 			rv = EINVAL;
1057 		}
1058 	}
1059 
1060 	/*
1061 	 * something above failed
1062 	 * destroy the partially child device and abort the request
1063 	 */
1064 	if (rv != 0) {
1065 		(void) ndi_devi_free(cdip);
1066 		return (rv);
1067 	}
1068 
1069 	*rdip = cdip;
1070 	return (0);
1071 }
1072 
1073 /*
1074  * return current soft bus state of bus nexus "dip"
1075  */
1076 int
1077 ndi_get_bus_state(dev_info_t *dip, uint_t *rstate)
1078 {
1079 	if (dip == NULL || rstate == NULL)
1080 		return (NDI_FAILURE);
1081 
1082 	if (DEVI(dip)->devi_ops->devo_bus_ops == NULL)
1083 		return (NDI_FAILURE);
1084 
1085 	mutex_enter(&(DEVI(dip)->devi_lock));
1086 	if (DEVI_IS_BUS_QUIESCED(dip))
1087 		*rstate = BUS_QUIESCED;
1088 	else if (DEVI_IS_BUS_DOWN(dip))
1089 		*rstate = BUS_SHUTDOWN;
1090 	else
1091 		*rstate = BUS_ACTIVE;
1092 	mutex_exit(&(DEVI(dip)->devi_lock));
1093 	return (NDI_SUCCESS);
1094 }
1095 
1096 /*
1097  * Set the soft state of bus nexus "dip"
1098  */
1099 int
1100 ndi_set_bus_state(dev_info_t *dip, uint_t state)
1101 {
1102 	int rv = NDI_SUCCESS;
1103 
1104 	if (dip == NULL)
1105 		return (NDI_FAILURE);
1106 
1107 	mutex_enter(&(DEVI(dip)->devi_lock));
1108 
1109 	switch (state) {
1110 	case BUS_QUIESCED:
1111 		DEVI_SET_BUS_QUIESCE(dip);
1112 		break;
1113 
1114 	case BUS_ACTIVE:
1115 		DEVI_SET_BUS_ACTIVE(dip);
1116 		DEVI_SET_BUS_UP(dip);
1117 		break;
1118 
1119 	case BUS_SHUTDOWN:
1120 		DEVI_SET_BUS_DOWN(dip);
1121 		break;
1122 
1123 	default:
1124 		rv = NDI_FAILURE;
1125 	}
1126 
1127 	mutex_exit(&(DEVI(dip)->devi_lock));
1128 	return (rv);
1129 }
1130 
1131 /*
1132  * These dummy functions are obsolete and may be removed.
1133  * Retained for existing driver compatibility only.
1134  * Drivers should be fixed not to use these functions.
1135  * Don't write new code using these obsolete interfaces.
1136  */
1137 /*ARGSUSED*/
1138 void
1139 i_ndi_block_device_tree_changes(uint_t *lkcnt)	/* obsolete */
1140 {
1141 	/* obsolete dummy function */
1142 }
1143 
1144 /*ARGSUSED*/
1145 void
1146 i_ndi_allow_device_tree_changes(uint_t lkcnt)	/* obsolete */
1147 {
1148 	/* obsolete dummy function */
1149 }
1150 
1151 /*
1152  * Single thread entry into per-driver list
1153  */
1154 /*ARGSUSED*/
1155 void
1156 e_ddi_enter_driver_list(struct devnames *dnp, int *listcnt)	/* obsolete */
1157 {
1158 	/* obsolete dummy function */
1159 }
1160 
1161 /*
1162  * release the per-driver list
1163  */
1164 /*ARGSUSED*/
1165 void
1166 e_ddi_exit_driver_list(struct devnames *dnp, int listcnt)	/* obsolete */
1167 {
1168 	/* obsolete dummy function */
1169 }
1170 
1171 /*
1172  * Attempt to enter driver list
1173  */
1174 /*ARGSUSED*/
1175 int
1176 e_ddi_tryenter_driver_list(struct devnames *dnp, int *listcnt)	/* obsolete */
1177 {
1178 	return (1);	/* obsolete dummy function */
1179 }
1180 
1181 /*
1182  * ndi event handling support functions:
1183  * The NDI event support model is as follows:
1184  *
1185  * The nexus driver defines a set of events using some static structures (so
1186  * these structures can be shared by all instances of the nexus driver).
1187  * The nexus driver allocates an event handle and binds the event set
1188  * to this handle. The nexus driver's event busop functions can just
1189  * call the appropriate NDI event support function using this handle
1190  * as the first argument.
1191  *
1192  * The reasoning for tying events to the device tree is that the entity
1193  * generating the callback will typically be one of the device driver's
1194  * ancestors in the tree.
1195  */
1196 static int ndi_event_debug = 0;
1197 
1198 #ifdef DEBUG
1199 #define	NDI_EVENT_DEBUG	ndi_event_debug
1200 #endif /* DEBUG */
1201 
1202 /*
1203  * allocate a new ndi event handle
1204  */
1205 int
1206 ndi_event_alloc_hdl(dev_info_t *dip, ddi_iblock_cookie_t cookie,
1207 	ndi_event_hdl_t *handle, uint_t flag)
1208 {
1209 	struct ndi_event_hdl *ndi_event_hdl;
1210 
1211 	ndi_event_hdl = kmem_zalloc(sizeof (struct ndi_event_hdl),
1212 		((flag & NDI_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP));
1213 
1214 	if (!ndi_event_hdl) {
1215 		return (NDI_FAILURE);
1216 	}
1217 
1218 	ndi_event_hdl->ndi_evthdl_dip = dip;
1219 	ndi_event_hdl->ndi_evthdl_iblock_cookie = cookie;
1220 	mutex_init(&ndi_event_hdl->ndi_evthdl_mutex, NULL,
1221 	    MUTEX_DRIVER, (void *)cookie);
1222 
1223 	mutex_init(&ndi_event_hdl->ndi_evthdl_cb_mutex, NULL,
1224 	    MUTEX_DRIVER, (void *)cookie);
1225 
1226 	*handle = (ndi_event_hdl_t)ndi_event_hdl;
1227 
1228 	return (NDI_SUCCESS);
1229 }
1230 
1231 /*
1232  * free the ndi event handle
1233  */
1234 int
1235 ndi_event_free_hdl(ndi_event_hdl_t handle)
1236 {
1237 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
1238 	ndi_event_cookie_t *cookie;
1239 	ndi_event_cookie_t *free;
1240 
1241 	ASSERT(handle);
1242 
1243 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1244 	mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1245 
1246 	cookie = ndi_event_hdl->ndi_evthdl_cookie_list;
1247 
1248 	/* deallocate all defined cookies */
1249 	while (cookie != NULL) {
1250 		ASSERT(cookie->callback_list == NULL);
1251 		free = cookie;
1252 		cookie = cookie->next_cookie;
1253 
1254 		kmem_free(free, sizeof (ndi_event_cookie_t));
1255 	}
1256 
1257 
1258 	mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1259 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1260 
1261 	/* destroy mutexes */
1262 	mutex_destroy(&ndi_event_hdl->ndi_evthdl_mutex);
1263 	mutex_destroy(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1264 
1265 	/* free event handle */
1266 	kmem_free(ndi_event_hdl, sizeof (struct ndi_event_hdl));
1267 
1268 	return (NDI_SUCCESS);
1269 }
1270 
1271 
1272 /*
1273  * ndi_event_bind_set() adds a set of events to the NDI event
1274  * handle.
1275  *
1276  * Events generated by high level interrupts should not
1277  * be mixed in the same event set with events generated by
1278  * normal interrupts or kernel events.
1279  *
1280  * This function can be called multiple times to bind
1281  * additional sets to the event handle.
1282  * However, events generated by high level interrupts cannot
1283  * be bound to a handle that already has bound events generated
1284  * by normal interrupts or from kernel context and vice versa.
1285  */
1286 int
1287 ndi_event_bind_set(ndi_event_hdl_t handle,
1288 	ndi_event_set_t		*ndi_events,
1289 	uint_t			flag)
1290 {
1291 	struct ndi_event_hdl	*ndi_event_hdl;
1292 	ndi_event_cookie_t	*next, *prev, *new_cookie;
1293 	uint_t			i, len;
1294 	uint_t			dup = 0;
1295 	uint_t			high_plevels, other_plevels;
1296 	ndi_event_definition_t *ndi_event_defs;
1297 
1298 	int km_flag = ((flag & NDI_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP);
1299 
1300 	ASSERT(handle);
1301 	ASSERT(ndi_events);
1302 
1303 	/*
1304 	 * binding must be performed during attach/detach
1305 	 */
1306 	if (!DEVI_IS_ATTACHING(handle->ndi_evthdl_dip) &&
1307 	    !DEVI_IS_DETACHING(handle->ndi_evthdl_dip)) {
1308 		cmn_err(CE_WARN, "ndi_event_bind_set must be called within "
1309 		    "attach or detach");
1310 		return (NDI_FAILURE);
1311 	}
1312 
1313 	/*
1314 	 * if it is not the correct version or the event set is
1315 	 * empty, bail out
1316 	 */
1317 	if (ndi_events->ndi_events_version != NDI_EVENTS_REV1)
1318 		return (NDI_FAILURE);
1319 
1320 	ndi_event_hdl	= (struct ndi_event_hdl *)handle;
1321 	ndi_event_defs = ndi_events->ndi_event_defs;
1322 	high_plevels	= other_plevels = 0;
1323 
1324 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1325 
1326 	/* check for mixing events at high level with the other types */
1327 	for (i = 0; i < ndi_events->ndi_n_events; i++) {
1328 		if (ndi_event_defs[i].ndi_event_plevel == EPL_HIGHLEVEL) {
1329 			high_plevels++;
1330 		} else {
1331 			other_plevels++;
1332 		}
1333 	}
1334 
1335 	/*
1336 	 * bail out if high level events are mixed with other types in this
1337 	 * event set or the set is incompatible with the set in the handle
1338 	 */
1339 	if ((high_plevels && other_plevels) ||
1340 	    (other_plevels && ndi_event_hdl->ndi_evthdl_high_plevels) ||
1341 	    (high_plevels && ndi_event_hdl->ndi_evthdl_other_plevels)) {
1342 		mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1343 
1344 		return (NDI_FAILURE);
1345 	}
1346 
1347 	/*
1348 	 * check for duplicate events in both the existing handle
1349 	 * and the event set, add events if not duplicates
1350 	 */
1351 	next = ndi_event_hdl->ndi_evthdl_cookie_list;
1352 	for (i = 0; i < ndi_events->ndi_n_events; i++) {
1353 		while (next != NULL) {
1354 			len = strlen(NDI_EVENT_NAME(next)) + 1;
1355 			if (strncmp(NDI_EVENT_NAME(next),
1356 			    ndi_event_defs[i].ndi_event_name, len) == 0) {
1357 				dup = 1;
1358 				break;
1359 			}
1360 
1361 			prev = next;
1362 			next = next->next_cookie;
1363 		}
1364 
1365 		if (dup == 0) {
1366 			new_cookie = kmem_zalloc(sizeof (ndi_event_cookie_t),
1367 			    km_flag);
1368 
1369 			if (!new_cookie)
1370 				return (NDI_FAILURE);
1371 
1372 			if (ndi_event_hdl->ndi_evthdl_n_events == 0) {
1373 				ndi_event_hdl->ndi_evthdl_cookie_list =
1374 				    new_cookie;
1375 			} else {
1376 				prev->next_cookie = new_cookie;
1377 			}
1378 
1379 			ndi_event_hdl->ndi_evthdl_n_events++;
1380 
1381 			/*
1382 			 * set up new cookie
1383 			 */
1384 			new_cookie->definition = &ndi_event_defs[i];
1385 			new_cookie->ddip = ndi_event_hdl->ndi_evthdl_dip;
1386 
1387 		} else {
1388 			/*
1389 			 * event not added, must correct plevel numbers
1390 			 */
1391 			if (ndi_event_defs[i].ndi_event_plevel ==
1392 			    EPL_HIGHLEVEL) {
1393 				high_plevels--;
1394 			} else {
1395 				other_plevels--;
1396 			}
1397 		}
1398 
1399 		dup = 0;
1400 		next = ndi_event_hdl->ndi_evthdl_cookie_list;
1401 		prev = NULL;
1402 
1403 	}
1404 
1405 	ndi_event_hdl->ndi_evthdl_high_plevels	+= high_plevels;
1406 	ndi_event_hdl->ndi_evthdl_other_plevels += other_plevels;
1407 
1408 	ASSERT((ndi_event_hdl->ndi_evthdl_high_plevels == 0) ||
1409 	    (ndi_event_hdl->ndi_evthdl_other_plevels == 0));
1410 
1411 #ifdef NDI_EVENT_DEBUG
1412 	if (ndi_event_debug) {
1413 		ndi_event_dump_hdl(ndi_event_hdl, "ndi_event_bind_set");
1414 	}
1415 #endif /* NDI_EVENT_DEBUG */
1416 
1417 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1418 
1419 	return (NDI_SUCCESS);
1420 }
1421 
1422 /*
1423  * ndi_event_unbind_set() unbinds a set of events, previously
1424  * bound using ndi_event_bind_set(), from the NDI event
1425  * handle.
1426  *
1427  * This routine will unbind all events in the event set.  If an event,
1428  * specified in the event set, is not found in the handle, this
1429  * routine will proceed onto the next member of the set as if the event
1430  * was never specified.
1431  *
1432  * The event set may be a subset of the set of events that
1433  * was previously bound to the handle. For example, events
1434  * can be individually unbound.
1435  *
1436  * An event cannot be unbound if callbacks are still
1437  * registered against the event.
1438  */
1439 /*ARGSUSED*/
1440 int
1441 ndi_event_unbind_set(ndi_event_hdl_t   handle, ndi_event_set_t	*ndi_events,
1442     uint_t flag)
1443 {
1444 	ndi_event_definition_t	*ndi_event_defs;
1445 	int			len;
1446 	uint_t			i;
1447 	int			rval;
1448 	ndi_event_cookie_t *cookie_list;
1449 	ndi_event_cookie_t *prev = NULL;
1450 
1451 	ASSERT(ndi_events);
1452 	ASSERT(handle);
1453 
1454 	/*
1455 	 * binding must be performed during attach/detac
1456 	 */
1457 	if (!DEVI_IS_ATTACHING(handle->ndi_evthdl_dip) &&
1458 	    !DEVI_IS_DETACHING(handle->ndi_evthdl_dip)) {
1459 		cmn_err(CE_WARN, "ndi_event_bind_set must be called within "
1460 		    "attach or detach");
1461 		return (NDI_FAILURE);
1462 	}
1463 
1464 	/* bail out if ndi_event_set is outdated */
1465 	if (ndi_events->ndi_events_version != NDI_EVENTS_REV1) {
1466 		return (NDI_FAILURE);
1467 	}
1468 
1469 	ASSERT(ndi_events->ndi_event_defs);
1470 
1471 	ndi_event_defs = ndi_events->ndi_event_defs;
1472 
1473 	mutex_enter(&handle->ndi_evthdl_mutex);
1474 	mutex_enter(&handle->ndi_evthdl_cb_mutex);
1475 
1476 	/*
1477 	 * Verify that all events in the event set are eligible
1478 	 * for unbinding(ie. there are no outstanding callbacks).
1479 	 * If any one of the events are ineligible, fail entire
1480 	 * operation.
1481 	 */
1482 
1483 	for (i = 0; i < ndi_events->ndi_n_events; i++) {
1484 		cookie_list = handle->ndi_evthdl_cookie_list;
1485 		while (cookie_list != NULL) {
1486 			len = strlen(NDI_EVENT_NAME(cookie_list)) + 1;
1487 			if (strncmp(NDI_EVENT_NAME(cookie_list),
1488 			    ndi_event_defs[i].ndi_event_name, len) == 0) {
1489 
1490 			    ASSERT(cookie_list->callback_list == NULL);
1491 			    if (cookie_list->callback_list) {
1492 				    rval = NDI_FAILURE;
1493 				    goto done;
1494 			    }
1495 			    break;
1496 			} else {
1497 				cookie_list = cookie_list->next_cookie;
1498 			}
1499 		}
1500 	}
1501 
1502 	/*
1503 	 * remove all events found within the handle
1504 	 * If an event is not found, this function will proceed as if the event
1505 	 * was never specified.
1506 	 */
1507 
1508 	for (i = 0; i < ndi_events->ndi_n_events; i++) {
1509 		cookie_list = handle->ndi_evthdl_cookie_list;
1510 		prev = NULL;
1511 		while (cookie_list != NULL) {
1512 			len = strlen(NDI_EVENT_NAME(cookie_list)) + 1;
1513 			if (strncmp(NDI_EVENT_NAME(cookie_list),
1514 			    ndi_event_defs[i].ndi_event_name, len) == 0) {
1515 
1516 				/*
1517 				 * can not unbind an event definition with
1518 				 * outstanding callbacks
1519 				 */
1520 				if (cookie_list->callback_list) {
1521 					rval = NDI_FAILURE;
1522 					goto done;
1523 				}
1524 
1525 				/* remove this cookie from the list */
1526 				if (prev != NULL) {
1527 					prev->next_cookie =
1528 					    cookie_list->next_cookie;
1529 				} else {
1530 					handle->ndi_evthdl_cookie_list =
1531 					    cookie_list->next_cookie;
1532 				}
1533 
1534 				/* adjust plevel counts */
1535 				if (NDI_EVENT_PLEVEL(cookie_list) ==
1536 				    EPL_HIGHLEVEL) {
1537 					handle->ndi_evthdl_high_plevels--;
1538 				} else {
1539 					handle->ndi_evthdl_other_plevels--;
1540 				}
1541 
1542 				/* adjust cookie count */
1543 				handle->ndi_evthdl_n_events--;
1544 
1545 				/* free the cookie */
1546 				kmem_free(cookie_list,
1547 				    sizeof (ndi_event_cookie_t));
1548 
1549 				cookie_list = handle->ndi_evthdl_cookie_list;
1550 				break;
1551 
1552 			} else {
1553 				prev = cookie_list;
1554 				cookie_list = cookie_list->next_cookie;
1555 			}
1556 
1557 		}
1558 
1559 	}
1560 
1561 #ifdef NDI_EVENT_DEBUG
1562 	if (ndi_event_debug) {
1563 		ndi_event_dump_hdl(handle, "ndi_event_unbind_set");
1564 	}
1565 #endif /* NDI_EVENT_DEBUG */
1566 
1567 	rval = NDI_SUCCESS;
1568 
1569 done:
1570 	mutex_exit(&handle->ndi_evthdl_cb_mutex);
1571 	mutex_exit(&handle->ndi_evthdl_mutex);
1572 
1573 	return (rval);
1574 }
1575 
1576 /*
1577  * ndi_event_retrieve_cookie():
1578  * Return an event cookie for eventname if this nexus driver
1579  * has defined the named event. The event cookie returned
1580  * by this function is used to register callback handlers
1581  * for the event.
1582  *
1583  * ndi_event_retrieve_cookie() is intended to be used in the
1584  * nexus driver's bus_get_eventcookie busop routine.
1585  *
1586  * If the event is not defined by this bus nexus driver, and flag
1587  * does not include NDI_EVENT_NOPASS, then ndi_event_retrieve_cookie()
1588  * will pass the request up the device tree hierarchy by calling
1589  * ndi_busop_get_eventcookie(9N).
1590  * If the event is not defined by this bus nexus driver, and flag
1591  * does include NDI_EVENT_NOPASS, ndi_event_retrieve_cookie()
1592  * will return NDI_FAILURE.  The caller may then determine what further
1593  * action to take, such as using a different handle, passing the
1594  * request up the device tree using ndi_busop_get_eventcookie(9N),
1595  * or returning the failure to the caller, thus blocking the
1596  * progress of the request up the tree.
1597  */
1598 int
1599 ndi_event_retrieve_cookie(ndi_event_hdl_t handle,
1600 	dev_info_t		*rdip,
1601 	char			*eventname,
1602 	ddi_eventcookie_t	*cookiep,
1603 	uint_t			flag)
1604 {
1605 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
1606 	int		len;
1607 	ndi_event_cookie_t *cookie_list;
1608 
1609 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1610 
1611 	cookie_list = ndi_event_hdl->ndi_evthdl_cookie_list;
1612 	/*
1613 	 * search the cookie list for the event name and return
1614 	 * cookie if found.
1615 	 */
1616 	while (cookie_list != NULL) {
1617 
1618 		len = strlen(NDI_EVENT_NAME(cookie_list)) + 1;
1619 		if (strncmp(NDI_EVENT_NAME(cookie_list), eventname,
1620 		    len) == 0) {
1621 			*cookiep = (ddi_eventcookie_t)cookie_list;
1622 
1623 			mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1624 			return (NDI_SUCCESS);
1625 		}
1626 
1627 		cookie_list = cookie_list->next_cookie;
1628 	}
1629 
1630 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1631 	/*
1632 	 * event was not found, pass up or return failure
1633 	 */
1634 	if ((flag & NDI_EVENT_NOPASS) == 0) {
1635 		return (ndi_busop_get_eventcookie(
1636 			ndi_event_hdl->ndi_evthdl_dip, rdip,
1637 			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,
2117 		(void *)hdl->ndi_evthdl_dip,
2118 		ddi_node_name(hdl->ndi_evthdl_dip),
2119 		ddi_get_instance(hdl->ndi_evthdl_dip));
2120 	cmn_err(CE_CONT, "\thigh=%d other=%d n=%d\n",
2121 		hdl->ndi_evthdl_high_plevels,
2122 		hdl->ndi_evthdl_other_plevels,
2123 		hdl->ndi_evthdl_n_events);
2124 
2125 
2126 	cmn_err(CE_CONT, "\tevent cookies:\n");
2127 	while (list) {
2128 		cmn_err(CE_CONT,
2129 			"\t\ttag=%d name=%s p=%d a=%x dd=%p\n",
2130 			NDI_EVENT_TAG(list),
2131 			NDI_EVENT_NAME(list),
2132 			NDI_EVENT_PLEVEL(list),
2133 			NDI_EVENT_ATTRIBUTES(list),
2134 			(void *)NDI_EVENT_DDIP(list));
2135 		cmn_err(CE_CONT, "\t\tcallbacks:\n");
2136 		for (next = list->callback_list; next != NULL;
2137 		    next = next->ndi_evtcb_next) {
2138 			cmn_err(CE_CONT,
2139 			    "\t\t  dip=%p (%s%d) cookie=%p arg=%p\n",
2140 			    (void*)next->ndi_evtcb_dip,
2141 			    ddi_driver_name(next->ndi_evtcb_dip),
2142 			    ddi_get_instance(next->ndi_evtcb_dip),
2143 			    (void *)next->ndi_evtcb_cookie,
2144 			    next->ndi_evtcb_arg);
2145 		}
2146 
2147 		list = list->next_cookie;
2148 	}
2149 
2150 	cmn_err(CE_CONT, "\n");
2151 }
2152 #endif
2153 
2154 int
2155 ndi_dev_is_prom_node(dev_info_t *dip)
2156 {
2157 	return (DEVI(dip)->devi_node_class == DDI_NC_PROM);
2158 }
2159 
2160 int
2161 ndi_dev_is_pseudo_node(dev_info_t *dip)
2162 {
2163 	/*
2164 	 * NOTE: this does NOT mean the pseudo branch of the device tree,
2165 	 * it means the node was created by software (DEVI_SID_NODEID |
2166 	 * DEVI_PSEUDO_NODEID) instead of being generated from a PROM node.
2167 	 */
2168 	return (DEVI(dip)->devi_node_class == DDI_NC_PSEUDO);
2169 }
2170 
2171 int
2172 ndi_dev_is_persistent_node(dev_info_t *dip)
2173 {
2174 	return ((DEVI(dip)->devi_node_attributes & DDI_PERSISTENT) != 0);
2175 }
2176 
2177 int
2178 i_ndi_dev_is_auto_assigned_node(dev_info_t *dip)
2179 {
2180 	return ((DEVI(dip)->devi_node_attributes &
2181 	    DDI_AUTO_ASSIGNED_NODEID) != 0);
2182 }
2183 
2184 void
2185 i_ndi_set_node_class(dev_info_t *dip, ddi_node_class_t c)
2186 {
2187 	DEVI(dip)->devi_node_class = c;
2188 }
2189 
2190 ddi_node_class_t
2191 i_ndi_get_node_class(dev_info_t *dip)
2192 {
2193 	return (DEVI(dip)->devi_node_class);
2194 }
2195 
2196 void
2197 i_ndi_set_node_attributes(dev_info_t *dip, int p)
2198 {
2199 	DEVI(dip)->devi_node_attributes = p;
2200 }
2201 
2202 int
2203 i_ndi_get_node_attributes(dev_info_t *dip)
2204 {
2205 	return (DEVI(dip)->devi_node_attributes);
2206 }
2207 
2208 void
2209 i_ndi_set_nodeid(dev_info_t *dip, int n)
2210 {
2211 	DEVI(dip)->devi_nodeid = n;
2212 }
2213 
2214 void
2215 ndi_set_acc_fault(ddi_acc_handle_t ah)
2216 {
2217 	i_ddi_acc_set_fault(ah);
2218 }
2219 
2220 void
2221 ndi_clr_acc_fault(ddi_acc_handle_t ah)
2222 {
2223 	i_ddi_acc_clr_fault(ah);
2224 }
2225 
2226 void
2227 ndi_set_dma_fault(ddi_dma_handle_t dh)
2228 {
2229 	i_ddi_dma_set_fault(dh);
2230 }
2231 
2232 void
2233 ndi_clr_dma_fault(ddi_dma_handle_t dh)
2234 {
2235 	i_ddi_dma_clr_fault(dh);
2236 }
2237 
2238 /*
2239  *  The default fault-handler, called when the event posted by
2240  *  ddi_dev_report_fault() reaches rootnex.
2241  */
2242 static void
2243 i_ddi_fault_handler(dev_info_t *dip, struct ddi_fault_event_data *fedp)
2244 {
2245 	ASSERT(fedp);
2246 
2247 	mutex_enter(&(DEVI(dip)->devi_lock));
2248 	if (!DEVI_IS_DEVICE_OFFLINE(dip)) {
2249 		switch (fedp->f_impact) {
2250 		case DDI_SERVICE_LOST:
2251 			DEVI_SET_DEVICE_DOWN(dip);
2252 			break;
2253 
2254 		case DDI_SERVICE_DEGRADED:
2255 			DEVI_SET_DEVICE_DEGRADED(dip);
2256 			break;
2257 
2258 		case DDI_SERVICE_UNAFFECTED:
2259 		default:
2260 			break;
2261 
2262 		case DDI_SERVICE_RESTORED:
2263 			DEVI_SET_DEVICE_UP(dip);
2264 			break;
2265 		}
2266 	}
2267 	mutex_exit(&(DEVI(dip)->devi_lock));
2268 }
2269 
2270 /*
2271  * The default fault-logger, called when the event posted by
2272  * ddi_dev_report_fault() reaches rootnex.
2273  */
2274 /*ARGSUSED*/
2275 static void
2276 i_ddi_fault_logger(dev_info_t *rdip, struct ddi_fault_event_data *fedp)
2277 {
2278 	ddi_devstate_t newstate;
2279 	const char *action;
2280 	const char *servstate;
2281 	const char *location;
2282 	int bad;
2283 	int changed;
2284 	int level;
2285 	int still;
2286 
2287 	ASSERT(fedp);
2288 
2289 	bad = 0;
2290 	switch (fedp->f_location) {
2291 	case DDI_DATAPATH_FAULT:
2292 		location = "in datapath to";
2293 		break;
2294 	case DDI_DEVICE_FAULT:
2295 		location = "in";
2296 		break;
2297 	case DDI_EXTERNAL_FAULT:
2298 		location = "external to";
2299 		break;
2300 	default:
2301 		location = "somewhere near";
2302 		bad = 1;
2303 		break;
2304 	}
2305 
2306 	newstate = ddi_get_devstate(fedp->f_dip);
2307 	switch (newstate) {
2308 	case DDI_DEVSTATE_OFFLINE:
2309 		servstate = "unavailable";
2310 		break;
2311 	case DDI_DEVSTATE_DOWN:
2312 		servstate = "unavailable";
2313 		break;
2314 	case DDI_DEVSTATE_QUIESCED:
2315 		servstate = "suspended";
2316 		break;
2317 	case DDI_DEVSTATE_DEGRADED:
2318 		servstate = "degraded";
2319 		break;
2320 	default:
2321 		servstate = "available";
2322 		break;
2323 	}
2324 
2325 	changed = (newstate != fedp->f_oldstate);
2326 	level = (newstate < fedp->f_oldstate) ? CE_WARN : CE_NOTE;
2327 	switch (fedp->f_impact) {
2328 	case DDI_SERVICE_LOST:
2329 	case DDI_SERVICE_DEGRADED:
2330 	case DDI_SERVICE_UNAFFECTED:
2331 		/* fault detected; service [still] <servstate> */
2332 		action = "fault detected";
2333 		still = !changed;
2334 		break;
2335 
2336 	case DDI_SERVICE_RESTORED:
2337 		if (newstate != DDI_DEVSTATE_UP) {
2338 			/* fault cleared; service still <servstate> */
2339 			action = "fault cleared";
2340 			still = 1;
2341 		} else if (changed) {
2342 			/* fault cleared; service <servstate> */
2343 			action = "fault cleared";
2344 			still = 0;
2345 		} else {
2346 			/* no fault; service <servstate> */
2347 			action = "no fault";
2348 			still = 0;
2349 		}
2350 		break;
2351 
2352 	default:
2353 		bad = 1;
2354 		break;
2355 	}
2356 
2357 	cmn_err(level, "!%s%d: %s %s device; service %s%s"+(bad|changed),
2358 		ddi_driver_name(fedp->f_dip),
2359 		ddi_get_instance(fedp->f_dip),
2360 		bad ? "invalid report of fault" : action,
2361 		location, still ? "still " : "", servstate);
2362 
2363 	cmn_err(level, "!%s%d: %s"+(bad|changed),
2364 		ddi_driver_name(fedp->f_dip),
2365 		ddi_get_instance(fedp->f_dip),
2366 		fedp->f_message);
2367 }
2368 
2369 /*
2370  * Platform-settable pointers to fault handler and logger functions.
2371  * These are called by the default rootnex event-posting code when
2372  * a fault event reaches rootnex.
2373  */
2374 void (*plat_fault_handler)(dev_info_t *, struct ddi_fault_event_data *) =
2375 	i_ddi_fault_handler;
2376 void (*plat_fault_logger)(dev_info_t *, struct ddi_fault_event_data *) =
2377 	i_ddi_fault_logger;
2378 
2379 /*
2380  * Rootnex event definitions ...
2381  */
2382 enum rootnex_event_tags {
2383 	ROOTNEX_FAULT_EVENT
2384 };
2385 static ndi_event_hdl_t rootnex_event_hdl;
2386 static ndi_event_definition_t rootnex_event_set[] = {
2387 	{
2388 		ROOTNEX_FAULT_EVENT,
2389 		DDI_DEVI_FAULT_EVENT,
2390 		EPL_INTERRUPT,
2391 		NDI_EVENT_POST_TO_ALL
2392 	}
2393 };
2394 static ndi_event_set_t rootnex_events = {
2395 	NDI_EVENTS_REV1,
2396 	sizeof (rootnex_event_set) / sizeof (rootnex_event_set[0]),
2397 	rootnex_event_set
2398 };
2399 
2400 /*
2401  * Initialize rootnex event handle
2402  */
2403 void
2404 i_ddi_rootnex_init_events(dev_info_t *dip)
2405 {
2406 	if (ndi_event_alloc_hdl(dip, (ddi_iblock_cookie_t)(LOCK_LEVEL-1),
2407 	    &rootnex_event_hdl, NDI_SLEEP) == NDI_SUCCESS) {
2408 		if (ndi_event_bind_set(rootnex_event_hdl,
2409 		    &rootnex_events, NDI_SLEEP) != NDI_SUCCESS) {
2410 			(void) ndi_event_free_hdl(rootnex_event_hdl);
2411 			rootnex_event_hdl = NULL;
2412 		}
2413 	}
2414 }
2415 
2416 /*
2417  *      Event-handling functions for rootnex
2418  *      These provide the standard implementation of fault handling
2419  */
2420 /*ARGSUSED*/
2421 int
2422 i_ddi_rootnex_get_eventcookie(dev_info_t *dip, dev_info_t *rdip,
2423 	char *eventname, ddi_eventcookie_t *cookiep)
2424 {
2425 	if (rootnex_event_hdl == NULL)
2426 		return (NDI_FAILURE);
2427 	return (ndi_event_retrieve_cookie(rootnex_event_hdl, rdip, eventname,
2428 		cookiep, NDI_EVENT_NOPASS));
2429 }
2430 
2431 /*ARGSUSED*/
2432 int
2433 i_ddi_rootnex_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
2434 	ddi_eventcookie_t eventid, void (*handler)(dev_info_t *dip,
2435 	ddi_eventcookie_t event, void *arg, void *impl_data), void *arg,
2436 	ddi_callback_id_t *cb_id)
2437 {
2438 	if (rootnex_event_hdl == NULL)
2439 		return (NDI_FAILURE);
2440 	return (ndi_event_add_callback(rootnex_event_hdl, rdip,
2441 		eventid, handler, arg, NDI_SLEEP, cb_id));
2442 }
2443 
2444 /*ARGSUSED*/
2445 int
2446 i_ddi_rootnex_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
2447 {
2448 	if (rootnex_event_hdl == NULL)
2449 		return (NDI_FAILURE);
2450 
2451 	return (ndi_event_remove_callback(rootnex_event_hdl, cb_id));
2452 }
2453 
2454 /*ARGSUSED*/
2455 int
2456 i_ddi_rootnex_post_event(dev_info_t *dip, dev_info_t *rdip,
2457 	ddi_eventcookie_t eventid, void *impl_data)
2458 {
2459 	int tag;
2460 
2461 	if (rootnex_event_hdl == NULL)
2462 		return (NDI_FAILURE);
2463 
2464 	tag = ndi_event_cookie_to_tag(rootnex_event_hdl, eventid);
2465 	if (tag == ROOTNEX_FAULT_EVENT) {
2466 		(*plat_fault_handler)(rdip, impl_data);
2467 		(*plat_fault_logger)(rdip, impl_data);
2468 	}
2469 	return (ndi_event_run_callbacks(rootnex_event_hdl, rdip,
2470 		eventid, impl_data));
2471 }
2472