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