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