dmover_backend.c revision 1.6.6.1 1 1.6.6.1 jmcneill /* $NetBSD: dmover_backend.c,v 1.6.6.1 2007/12/09 19:37:46 jmcneill 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.6.6.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: dmover_backend.c,v 1.6.6.1 2007/12/09 19:37:46 jmcneill Exp $");
44 1.1 thorpej
45 1.1 thorpej #include <sys/param.h>
46 1.6 ad #include <sys/mutex.h>
47 1.1 thorpej #include <sys/systm.h>
48 1.1 thorpej
49 1.1 thorpej #include <dev/dmover/dmovervar.h>
50 1.1 thorpej
51 1.1 thorpej TAILQ_HEAD(, dmover_backend) dmover_backend_list;
52 1.6 ad kmutex_t dmover_backend_list_lock;
53 1.1 thorpej static int initialized;
54 1.1 thorpej static struct simplelock initialized_slock = SIMPLELOCK_INITIALIZER;
55 1.1 thorpej
56 1.1 thorpej static void
57 1.1 thorpej initialize(void)
58 1.1 thorpej {
59 1.1 thorpej
60 1.1 thorpej simple_lock(&initialized_slock);
61 1.1 thorpej if (__predict_true(initialized == 0)) {
62 1.1 thorpej TAILQ_INIT(&dmover_backend_list);
63 1.6.6.1 jmcneill mutex_init(&dmover_backend_list_lock, MUTEX_DEFAULT, IPL_VM);
64 1.1 thorpej
65 1.1 thorpej /* Initialize the other bits of dmover. */
66 1.1 thorpej dmover_session_initialize();
67 1.1 thorpej dmover_request_initialize();
68 1.1 thorpej dmover_process_initialize();
69 1.1 thorpej
70 1.1 thorpej initialized = 1;
71 1.1 thorpej }
72 1.1 thorpej simple_unlock(&initialized_slock);
73 1.1 thorpej }
74 1.1 thorpej
75 1.1 thorpej /*
76 1.1 thorpej * dmover_backend_register: [back-end interface function]
77 1.1 thorpej *
78 1.1 thorpej * Register a back-end with dmover-api.
79 1.1 thorpej */
80 1.1 thorpej void
81 1.1 thorpej dmover_backend_register(struct dmover_backend *dmb)
82 1.1 thorpej {
83 1.1 thorpej
84 1.1 thorpej if (__predict_false(initialized == 0))
85 1.1 thorpej initialize();
86 1.1 thorpej
87 1.1 thorpej LIST_INIT(&dmb->dmb_sessions);
88 1.1 thorpej dmb->dmb_nsessions = 0;
89 1.1 thorpej
90 1.1 thorpej TAILQ_INIT(&dmb->dmb_pendreqs);
91 1.1 thorpej dmb->dmb_npendreqs = 0;
92 1.1 thorpej
93 1.6 ad mutex_enter(&dmover_backend_list_lock);
94 1.1 thorpej TAILQ_INSERT_TAIL(&dmover_backend_list, dmb, dmb_list);
95 1.6 ad mutex_exit(&dmover_backend_list_lock);
96 1.1 thorpej }
97 1.1 thorpej
98 1.1 thorpej /*
99 1.1 thorpej * dmover_backend_unregister: [back-end interface function]
100 1.1 thorpej *
101 1.1 thorpej * Un-register a back-end from dmover-api.
102 1.1 thorpej */
103 1.1 thorpej void
104 1.1 thorpej dmover_backend_unregister(struct dmover_backend *dmb)
105 1.1 thorpej {
106 1.1 thorpej
107 1.1 thorpej #ifdef DIAGNOSTIC
108 1.1 thorpej if (__predict_false(initialized == 0)) {
109 1.1 thorpej int croak;
110 1.1 thorpej
111 1.1 thorpej simple_lock(&initialized_slock);
112 1.1 thorpej croak = (initialized == 0);
113 1.1 thorpej simple_unlock(&initialized_slock);
114 1.1 thorpej
115 1.1 thorpej if (croak)
116 1.1 thorpej panic("dmover_backend_unregister: not initialized");
117 1.1 thorpej }
118 1.1 thorpej #endif
119 1.1 thorpej
120 1.1 thorpej /* XXX */
121 1.1 thorpej if (dmb->dmb_nsessions)
122 1.1 thorpej panic("dmover_backend_unregister");
123 1.1 thorpej
124 1.6 ad mutex_enter(&dmover_backend_list_lock);
125 1.1 thorpej TAILQ_REMOVE(&dmover_backend_list, dmb, dmb_list);
126 1.6 ad mutex_exit(&dmover_backend_list_lock);
127 1.1 thorpej }
128 1.1 thorpej
129 1.1 thorpej /*
130 1.1 thorpej * dmover_backend_alloc:
131 1.1 thorpej *
132 1.1 thorpej * Allocate and return a back-end on behalf of a session.
133 1.1 thorpej */
134 1.1 thorpej int
135 1.1 thorpej dmover_backend_alloc(struct dmover_session *dses, const char *type)
136 1.1 thorpej {
137 1.1 thorpej struct dmover_backend *dmb, *best_dmb = NULL;
138 1.1 thorpej const struct dmover_algdesc *algdesc, *best_algdesc = NULL;
139 1.1 thorpej
140 1.1 thorpej if (__predict_false(initialized == 0)) {
141 1.1 thorpej int fail;
142 1.1 thorpej
143 1.1 thorpej simple_lock(&initialized_slock);
144 1.1 thorpej fail = (initialized == 0);
145 1.1 thorpej simple_unlock(&initialized_slock);
146 1.1 thorpej
147 1.1 thorpej if (fail)
148 1.1 thorpej return (ESRCH);
149 1.1 thorpej }
150 1.1 thorpej
151 1.6 ad mutex_enter(&dmover_backend_list_lock);
152 1.1 thorpej
153 1.1 thorpej /* First, find a back-end that can handle the session parts. */
154 1.1 thorpej for (dmb = TAILQ_FIRST(&dmover_backend_list); dmb != NULL;
155 1.1 thorpej dmb = TAILQ_NEXT(dmb, dmb_list)) {
156 1.1 thorpej /*
157 1.1 thorpej * First, check to see if the back-end supports the
158 1.1 thorpej * function we wish to perform.
159 1.1 thorpej */
160 1.1 thorpej algdesc = dmover_algdesc_lookup(dmb->dmb_algdescs,
161 1.1 thorpej dmb->dmb_nalgdescs, type);
162 1.1 thorpej if (algdesc == NULL)
163 1.1 thorpej continue;
164 1.1 thorpej
165 1.1 thorpej if (best_dmb == NULL) {
166 1.1 thorpej best_dmb = dmb;
167 1.1 thorpej best_algdesc = algdesc;
168 1.1 thorpej continue;
169 1.1 thorpej }
170 1.1 thorpej
171 1.1 thorpej /*
172 1.1 thorpej * XXX All the stuff from here on should be shot in
173 1.1 thorpej * XXX the head. Instead, we should build a list
174 1.1 thorpej * XXX of candidates, and select the best back-end
175 1.1 thorpej * XXX when a request is scheduled for processing.
176 1.1 thorpej */
177 1.1 thorpej
178 1.1 thorpej if (dmb->dmb_speed >= best_dmb->dmb_speed) {
179 1.1 thorpej /*
180 1.1 thorpej * If the current best match is slower than
181 1.1 thorpej * this back-end, then this one is the new
182 1.1 thorpej * best match.
183 1.1 thorpej */
184 1.1 thorpej if (dmb->dmb_speed > best_dmb->dmb_speed) {
185 1.1 thorpej best_dmb = dmb;
186 1.1 thorpej best_algdesc = algdesc;
187 1.1 thorpej continue;
188 1.1 thorpej }
189 1.1 thorpej
190 1.1 thorpej /*
191 1.1 thorpej * If this back-end has fewer sessions allocated
192 1.1 thorpej * to it than the current best match, then this
193 1.1 thorpej * one is now the best match.
194 1.1 thorpej */
195 1.1 thorpej if (best_dmb->dmb_nsessions > dmb->dmb_nsessions) {
196 1.1 thorpej best_dmb = dmb;
197 1.1 thorpej best_algdesc = algdesc;
198 1.1 thorpej continue;
199 1.1 thorpej }
200 1.1 thorpej }
201 1.1 thorpej }
202 1.1 thorpej if (best_dmb == NULL) {
203 1.6 ad mutex_exit(&dmover_backend_list_lock);
204 1.1 thorpej return (ESRCH);
205 1.1 thorpej }
206 1.1 thorpej
207 1.1 thorpej KASSERT(best_algdesc != NULL);
208 1.1 thorpej
209 1.1 thorpej /* Plug the back-end into the static (XXX) assignment. */
210 1.1 thorpej dses->__dses_assignment.das_backend = best_dmb;
211 1.1 thorpej dses->__dses_assignment.das_algdesc = best_algdesc;
212 1.1 thorpej
213 1.5 briggs dses->dses_ninputs = best_algdesc->dad_ninputs;
214 1.1 thorpej
215 1.1 thorpej LIST_INSERT_HEAD(&best_dmb->dmb_sessions, dses, __dses_list);
216 1.1 thorpej best_dmb->dmb_nsessions++;
217 1.1 thorpej
218 1.6 ad mutex_exit(&dmover_backend_list_lock);
219 1.1 thorpej
220 1.1 thorpej return (0);
221 1.1 thorpej }
222 1.1 thorpej
223 1.1 thorpej /*
224 1.1 thorpej * dmover_backend_release:
225 1.1 thorpej *
226 1.1 thorpej * Release the back-end from the specified session.
227 1.1 thorpej */
228 1.1 thorpej void
229 1.1 thorpej dmover_backend_release(struct dmover_session *dses)
230 1.1 thorpej {
231 1.1 thorpej struct dmover_backend *dmb;
232 1.1 thorpej
233 1.6 ad mutex_enter(&dmover_backend_list_lock);
234 1.1 thorpej
235 1.1 thorpej /* XXX Clear out the static assignment. */
236 1.1 thorpej dmb = dses->__dses_assignment.das_backend;
237 1.1 thorpej dses->__dses_assignment.das_backend = NULL;
238 1.1 thorpej dses->__dses_assignment.das_algdesc = NULL;
239 1.1 thorpej
240 1.1 thorpej LIST_REMOVE(dses, __dses_list);
241 1.1 thorpej dmb->dmb_nsessions--;
242 1.1 thorpej
243 1.6 ad mutex_exit(&dmover_backend_list_lock);
244 1.1 thorpej }
245