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