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