dmover_backend.c revision 1.2.2.2 1 1.2.2.2 thorpej /* $NetBSD: dmover_backend.c,v 1.2.2.2 2002/12/29 20:49:15 thorpej Exp $ */
2 1.2.2.2 thorpej
3 1.2.2.2 thorpej /*
4 1.2.2.2 thorpej * Copyright (c) 2002 Wasabi Systems, Inc.
5 1.2.2.2 thorpej * All rights reserved.
6 1.2.2.2 thorpej *
7 1.2.2.2 thorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.2.2.2 thorpej *
9 1.2.2.2 thorpej * Redistribution and use in source and binary forms, with or without
10 1.2.2.2 thorpej * modification, are permitted provided that the following conditions
11 1.2.2.2 thorpej * are met:
12 1.2.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.2.2.2 thorpej * notice, this list of conditions and the following disclaimer.
14 1.2.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.2.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.2.2.2 thorpej * documentation and/or other materials provided with the distribution.
17 1.2.2.2 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.2.2.2 thorpej * must display the following acknowledgement:
19 1.2.2.2 thorpej * This product includes software developed for the NetBSD Project by
20 1.2.2.2 thorpej * Wasabi Systems, Inc.
21 1.2.2.2 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.2.2.2 thorpej * or promote products derived from this software without specific prior
23 1.2.2.2 thorpej * written permission.
24 1.2.2.2 thorpej *
25 1.2.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.2.2.2 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.2.2.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.2.2.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.2.2.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.2.2.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.2.2.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.2.2.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.2.2.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.2.2.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.2.2.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
36 1.2.2.2 thorpej */
37 1.2.2.2 thorpej
38 1.2.2.2 thorpej /*
39 1.2.2.2 thorpej * dmover_backend.c: Backend management functions for dmover-api.
40 1.2.2.2 thorpej */
41 1.2.2.2 thorpej
42 1.2.2.2 thorpej #include <sys/cdefs.h>
43 1.2.2.2 thorpej __KERNEL_RCSID(0, "$NetBSD: dmover_backend.c,v 1.2.2.2 2002/12/29 20:49:15 thorpej Exp $");
44 1.2.2.2 thorpej
45 1.2.2.2 thorpej #include <sys/param.h>
46 1.2.2.2 thorpej #include <sys/lock.h>
47 1.2.2.2 thorpej #include <sys/systm.h>
48 1.2.2.2 thorpej
49 1.2.2.2 thorpej #include <dev/dmover/dmovervar.h>
50 1.2.2.2 thorpej
51 1.2.2.2 thorpej TAILQ_HEAD(, dmover_backend) dmover_backend_list;
52 1.2.2.2 thorpej struct lock dmover_backend_list_lock;
53 1.2.2.2 thorpej
54 1.2.2.2 thorpej #define BACKEND_LIST_LOCK_READ() \
55 1.2.2.2 thorpej do { \
56 1.2.2.2 thorpej (void) spinlockmgr(&dmover_backend_list_lock, LK_SHARED, NULL); \
57 1.2.2.2 thorpej } while (/*CONSTCOND*/0)
58 1.2.2.2 thorpej
59 1.2.2.2 thorpej #define BACKEND_LIST_UNLOCK_READ() \
60 1.2.2.2 thorpej do { \
61 1.2.2.2 thorpej (void) spinlockmgr(&dmover_backend_list_lock, LK_RELEASE, NULL);\
62 1.2.2.2 thorpej } while (/*CONSTCOND*/0)
63 1.2.2.2 thorpej
64 1.2.2.2 thorpej #define BACKEND_LIST_LOCK_WRITE(s) \
65 1.2.2.2 thorpej do { \
66 1.2.2.2 thorpej (s) = splbio(); \
67 1.2.2.2 thorpej (void) spinlockmgr(&dmover_backend_list_lock, LK_EXCLUSIVE, NULL); \
68 1.2.2.2 thorpej } while (/*CONSTCOND*/0)
69 1.2.2.2 thorpej
70 1.2.2.2 thorpej #define BACKEND_LIST_UNLOCK_WRITE(s) \
71 1.2.2.2 thorpej do { \
72 1.2.2.2 thorpej (void) spinlockmgr(&dmover_backend_list_lock, LK_RELEASE, NULL);\
73 1.2.2.2 thorpej splx((s)); \
74 1.2.2.2 thorpej } while (/*CONSTCOND*/0)
75 1.2.2.2 thorpej
76 1.2.2.2 thorpej static int initialized;
77 1.2.2.2 thorpej static struct simplelock initialized_slock = SIMPLELOCK_INITIALIZER;
78 1.2.2.2 thorpej
79 1.2.2.2 thorpej static void
80 1.2.2.2 thorpej initialize(void)
81 1.2.2.2 thorpej {
82 1.2.2.2 thorpej
83 1.2.2.2 thorpej simple_lock(&initialized_slock);
84 1.2.2.2 thorpej if (__predict_true(initialized == 0)) {
85 1.2.2.2 thorpej TAILQ_INIT(&dmover_backend_list);
86 1.2.2.2 thorpej spinlockinit(&dmover_backend_list_lock, "dmbelk", 0);
87 1.2.2.2 thorpej
88 1.2.2.2 thorpej /* Initialize the other bits of dmover. */
89 1.2.2.2 thorpej dmover_session_initialize();
90 1.2.2.2 thorpej dmover_request_initialize();
91 1.2.2.2 thorpej dmover_process_initialize();
92 1.2.2.2 thorpej
93 1.2.2.2 thorpej initialized = 1;
94 1.2.2.2 thorpej }
95 1.2.2.2 thorpej simple_unlock(&initialized_slock);
96 1.2.2.2 thorpej }
97 1.2.2.2 thorpej
98 1.2.2.2 thorpej /*
99 1.2.2.2 thorpej * dmover_backend_register: [back-end interface function]
100 1.2.2.2 thorpej *
101 1.2.2.2 thorpej * Register a back-end with dmover-api.
102 1.2.2.2 thorpej */
103 1.2.2.2 thorpej void
104 1.2.2.2 thorpej dmover_backend_register(struct dmover_backend *dmb)
105 1.2.2.2 thorpej {
106 1.2.2.2 thorpej int s;
107 1.2.2.2 thorpej
108 1.2.2.2 thorpej if (__predict_false(initialized == 0))
109 1.2.2.2 thorpej initialize();
110 1.2.2.2 thorpej
111 1.2.2.2 thorpej LIST_INIT(&dmb->dmb_sessions);
112 1.2.2.2 thorpej dmb->dmb_nsessions = 0;
113 1.2.2.2 thorpej
114 1.2.2.2 thorpej TAILQ_INIT(&dmb->dmb_pendreqs);
115 1.2.2.2 thorpej dmb->dmb_npendreqs = 0;
116 1.2.2.2 thorpej
117 1.2.2.2 thorpej BACKEND_LIST_LOCK_WRITE(s);
118 1.2.2.2 thorpej TAILQ_INSERT_TAIL(&dmover_backend_list, dmb, dmb_list);
119 1.2.2.2 thorpej BACKEND_LIST_UNLOCK_WRITE(s);
120 1.2.2.2 thorpej }
121 1.2.2.2 thorpej
122 1.2.2.2 thorpej /*
123 1.2.2.2 thorpej * dmover_backend_unregister: [back-end interface function]
124 1.2.2.2 thorpej *
125 1.2.2.2 thorpej * Un-register a back-end from dmover-api.
126 1.2.2.2 thorpej */
127 1.2.2.2 thorpej void
128 1.2.2.2 thorpej dmover_backend_unregister(struct dmover_backend *dmb)
129 1.2.2.2 thorpej {
130 1.2.2.2 thorpej int s;
131 1.2.2.2 thorpej
132 1.2.2.2 thorpej #ifdef DIAGNOSTIC
133 1.2.2.2 thorpej if (__predict_false(initialized == 0)) {
134 1.2.2.2 thorpej int croak;
135 1.2.2.2 thorpej
136 1.2.2.2 thorpej simple_lock(&initialized_slock);
137 1.2.2.2 thorpej croak = (initialized == 0);
138 1.2.2.2 thorpej simple_unlock(&initialized_slock);
139 1.2.2.2 thorpej
140 1.2.2.2 thorpej if (croak)
141 1.2.2.2 thorpej panic("dmover_backend_unregister: not initialized");
142 1.2.2.2 thorpej }
143 1.2.2.2 thorpej #endif
144 1.2.2.2 thorpej
145 1.2.2.2 thorpej /* XXX */
146 1.2.2.2 thorpej if (dmb->dmb_nsessions)
147 1.2.2.2 thorpej panic("dmover_backend_unregister");
148 1.2.2.2 thorpej
149 1.2.2.2 thorpej BACKEND_LIST_LOCK_WRITE(s);
150 1.2.2.2 thorpej TAILQ_REMOVE(&dmover_backend_list, dmb, dmb_list);
151 1.2.2.2 thorpej BACKEND_LIST_UNLOCK_WRITE(s);
152 1.2.2.2 thorpej }
153 1.2.2.2 thorpej
154 1.2.2.2 thorpej /*
155 1.2.2.2 thorpej * dmover_backend_alloc:
156 1.2.2.2 thorpej *
157 1.2.2.2 thorpej * Allocate and return a back-end on behalf of a session.
158 1.2.2.2 thorpej */
159 1.2.2.2 thorpej int
160 1.2.2.2 thorpej dmover_backend_alloc(struct dmover_session *dses, const char *type)
161 1.2.2.2 thorpej {
162 1.2.2.2 thorpej struct dmover_backend *dmb, *best_dmb = NULL;
163 1.2.2.2 thorpej const struct dmover_algdesc *algdesc, *best_algdesc = NULL;
164 1.2.2.2 thorpej
165 1.2.2.2 thorpej if (__predict_false(initialized == 0)) {
166 1.2.2.2 thorpej int fail;
167 1.2.2.2 thorpej
168 1.2.2.2 thorpej simple_lock(&initialized_slock);
169 1.2.2.2 thorpej fail = (initialized == 0);
170 1.2.2.2 thorpej simple_unlock(&initialized_slock);
171 1.2.2.2 thorpej
172 1.2.2.2 thorpej if (fail)
173 1.2.2.2 thorpej return (ESRCH);
174 1.2.2.2 thorpej }
175 1.2.2.2 thorpej
176 1.2.2.2 thorpej BACKEND_LIST_LOCK_READ();
177 1.2.2.2 thorpej
178 1.2.2.2 thorpej /* First, find a back-end that can handle the session parts. */
179 1.2.2.2 thorpej for (dmb = TAILQ_FIRST(&dmover_backend_list); dmb != NULL;
180 1.2.2.2 thorpej dmb = TAILQ_NEXT(dmb, dmb_list)) {
181 1.2.2.2 thorpej /*
182 1.2.2.2 thorpej * First, check to see if the back-end supports the
183 1.2.2.2 thorpej * function we wish to perform.
184 1.2.2.2 thorpej */
185 1.2.2.2 thorpej algdesc = dmover_algdesc_lookup(dmb->dmb_algdescs,
186 1.2.2.2 thorpej dmb->dmb_nalgdescs, type);
187 1.2.2.2 thorpej if (algdesc == NULL)
188 1.2.2.2 thorpej continue;
189 1.2.2.2 thorpej
190 1.2.2.2 thorpej if (best_dmb == NULL) {
191 1.2.2.2 thorpej best_dmb = dmb;
192 1.2.2.2 thorpej best_algdesc = algdesc;
193 1.2.2.2 thorpej continue;
194 1.2.2.2 thorpej }
195 1.2.2.2 thorpej
196 1.2.2.2 thorpej /*
197 1.2.2.2 thorpej * XXX All the stuff from here on should be shot in
198 1.2.2.2 thorpej * XXX the head. Instead, we should build a list
199 1.2.2.2 thorpej * XXX of candidates, and select the best back-end
200 1.2.2.2 thorpej * XXX when a request is scheduled for processing.
201 1.2.2.2 thorpej */
202 1.2.2.2 thorpej
203 1.2.2.2 thorpej if (dmb->dmb_speed >= best_dmb->dmb_speed) {
204 1.2.2.2 thorpej /*
205 1.2.2.2 thorpej * If the current best match is slower than
206 1.2.2.2 thorpej * this back-end, then this one is the new
207 1.2.2.2 thorpej * best match.
208 1.2.2.2 thorpej */
209 1.2.2.2 thorpej if (dmb->dmb_speed > best_dmb->dmb_speed) {
210 1.2.2.2 thorpej best_dmb = dmb;
211 1.2.2.2 thorpej best_algdesc = algdesc;
212 1.2.2.2 thorpej continue;
213 1.2.2.2 thorpej }
214 1.2.2.2 thorpej
215 1.2.2.2 thorpej /*
216 1.2.2.2 thorpej * If this back-end has fewer sessions allocated
217 1.2.2.2 thorpej * to it than the current best match, then this
218 1.2.2.2 thorpej * one is now the best match.
219 1.2.2.2 thorpej */
220 1.2.2.2 thorpej if (best_dmb->dmb_nsessions > dmb->dmb_nsessions) {
221 1.2.2.2 thorpej best_dmb = dmb;
222 1.2.2.2 thorpej best_algdesc = algdesc;
223 1.2.2.2 thorpej continue;
224 1.2.2.2 thorpej }
225 1.2.2.2 thorpej }
226 1.2.2.2 thorpej }
227 1.2.2.2 thorpej if (best_dmb == NULL) {
228 1.2.2.2 thorpej BACKEND_LIST_UNLOCK_READ();
229 1.2.2.2 thorpej return (ESRCH);
230 1.2.2.2 thorpej }
231 1.2.2.2 thorpej
232 1.2.2.2 thorpej KASSERT(best_algdesc != NULL);
233 1.2.2.2 thorpej
234 1.2.2.2 thorpej /* Plug the back-end into the static (XXX) assignment. */
235 1.2.2.2 thorpej dses->__dses_assignment.das_backend = best_dmb;
236 1.2.2.2 thorpej dses->__dses_assignment.das_algdesc = best_algdesc;
237 1.2.2.2 thorpej
238 1.2.2.2 thorpej dses->dses_ninputs = algdesc->dad_ninputs;
239 1.2.2.2 thorpej
240 1.2.2.2 thorpej LIST_INSERT_HEAD(&best_dmb->dmb_sessions, dses, __dses_list);
241 1.2.2.2 thorpej best_dmb->dmb_nsessions++;
242 1.2.2.2 thorpej
243 1.2.2.2 thorpej BACKEND_LIST_UNLOCK_READ();
244 1.2.2.2 thorpej
245 1.2.2.2 thorpej return (0);
246 1.2.2.2 thorpej }
247 1.2.2.2 thorpej
248 1.2.2.2 thorpej /*
249 1.2.2.2 thorpej * dmover_backend_release:
250 1.2.2.2 thorpej *
251 1.2.2.2 thorpej * Release the back-end from the specified session.
252 1.2.2.2 thorpej */
253 1.2.2.2 thorpej void
254 1.2.2.2 thorpej dmover_backend_release(struct dmover_session *dses)
255 1.2.2.2 thorpej {
256 1.2.2.2 thorpej struct dmover_backend *dmb;
257 1.2.2.2 thorpej
258 1.2.2.2 thorpej BACKEND_LIST_UNLOCK_READ();
259 1.2.2.2 thorpej
260 1.2.2.2 thorpej /* XXX Clear out the static assignment. */
261 1.2.2.2 thorpej dmb = dses->__dses_assignment.das_backend;
262 1.2.2.2 thorpej dses->__dses_assignment.das_backend = NULL;
263 1.2.2.2 thorpej dses->__dses_assignment.das_algdesc = NULL;
264 1.2.2.2 thorpej
265 1.2.2.2 thorpej LIST_REMOVE(dses, __dses_list);
266 1.2.2.2 thorpej dmb->dmb_nsessions--;
267 1.2.2.2 thorpej
268 1.2.2.2 thorpej BACKEND_LIST_UNLOCK_READ();
269 1.2.2.2 thorpej }
270