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