1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <sys/conf.h>
30*0Sstevel@tonic-gate #include <sys/sysmacros.h>
31*0Sstevel@tonic-gate #include <sys/async.h>
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate static bus_func_desc_t *bfd_list = NULL; /* list of busfunc descriptors */
34*0Sstevel@tonic-gate kmutex_t bfd_lock; /* lock protecting bfd_list */
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate /*
37*0Sstevel@tonic-gate * Register a new bus function. We expect this function to be called from a
38*0Sstevel@tonic-gate * driver attach or detach routine, so we can safely perform a sleeping
39*0Sstevel@tonic-gate * allocation here. We just insert the new element at the head of the list.
40*0Sstevel@tonic-gate * For more information on bus_func semantics, refer to sun4u/sys/async.h.
41*0Sstevel@tonic-gate */
42*0Sstevel@tonic-gate void
bus_func_register(int type,busfunc_t func,void * arg)43*0Sstevel@tonic-gate bus_func_register(int type, busfunc_t func, void *arg)
44*0Sstevel@tonic-gate {
45*0Sstevel@tonic-gate bus_func_desc_t *bfd = kmem_alloc(sizeof (bus_func_desc_t), KM_SLEEP);
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate bfd->bf_type = type;
48*0Sstevel@tonic-gate bfd->bf_func = func;
49*0Sstevel@tonic-gate bfd->bf_arg = arg;
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gate mutex_enter(&bfd_lock);
52*0Sstevel@tonic-gate bfd->bf_next = bfd_list;
53*0Sstevel@tonic-gate bfd_list = bfd;
54*0Sstevel@tonic-gate mutex_exit(&bfd_lock);
55*0Sstevel@tonic-gate }
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate /*
58*0Sstevel@tonic-gate * Unregister the specified bus function. We only delete an element that
59*0Sstevel@tonic-gate * exactly matches the specified (type, func, arg) tuple. We expect this
60*0Sstevel@tonic-gate * function to only be called from driver detach context.
61*0Sstevel@tonic-gate */
62*0Sstevel@tonic-gate void
bus_func_unregister(int type,busfunc_t func,void * arg)63*0Sstevel@tonic-gate bus_func_unregister(int type, busfunc_t func, void *arg)
64*0Sstevel@tonic-gate {
65*0Sstevel@tonic-gate bus_func_desc_t *bfd, **pp;
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gate mutex_enter(&bfd_lock);
68*0Sstevel@tonic-gate pp = &bfd_list;
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gate for (bfd = bfd_list; bfd != NULL; bfd = bfd->bf_next) {
71*0Sstevel@tonic-gate if (bfd->bf_type == type && bfd->bf_func == func &&
72*0Sstevel@tonic-gate bfd->bf_arg == arg) {
73*0Sstevel@tonic-gate *pp = bfd->bf_next;
74*0Sstevel@tonic-gate break;
75*0Sstevel@tonic-gate }
76*0Sstevel@tonic-gate pp = &bfd->bf_next;
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate mutex_exit(&bfd_lock);
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate if (bfd != NULL)
82*0Sstevel@tonic-gate kmem_free(bfd, sizeof (bus_func_desc_t));
83*0Sstevel@tonic-gate }
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gate /*
86*0Sstevel@tonic-gate * Invoke the registered bus functions of the specified type. We return the
87*0Sstevel@tonic-gate * maximum of the result values (e.g. BF_FATAL if any call returned BF_FATAL).
88*0Sstevel@tonic-gate * This function assumes that (1) the BF_* constants are defined so that the
89*0Sstevel@tonic-gate * most fatal error has the highest numerical value, and (2) that the bf_func
90*0Sstevel@tonic-gate * callbacks obey the rules described in async.h.
91*0Sstevel@tonic-gate */
92*0Sstevel@tonic-gate uint_t
bus_func_invoke(int type)93*0Sstevel@tonic-gate bus_func_invoke(int type)
94*0Sstevel@tonic-gate {
95*0Sstevel@tonic-gate bus_func_desc_t *bfd;
96*0Sstevel@tonic-gate uint_t err = BF_NONE;
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gate mutex_enter(&bfd_lock);
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate for (bfd = bfd_list; bfd != NULL; bfd = bfd->bf_next) {
101*0Sstevel@tonic-gate if (bfd->bf_type == type) {
102*0Sstevel@tonic-gate uint_t status = bfd->bf_func(bfd->bf_arg);
103*0Sstevel@tonic-gate err = MAX(err, status);
104*0Sstevel@tonic-gate }
105*0Sstevel@tonic-gate }
106*0Sstevel@tonic-gate
107*0Sstevel@tonic-gate mutex_exit(&bfd_lock);
108*0Sstevel@tonic-gate return (err);
109*0Sstevel@tonic-gate }
110