1 /* $NetBSD: dmover_backend.c,v 1.7 2007/12/05 07:06:51 ad Exp $ */
4 * Copyright (c) 2002 Wasabi Systems, Inc.
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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.
39 * dmover_backend.c: Backend management functions for dmover-api.
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: dmover_backend.c,v 1.7 2007/12/05 07:06:51 ad Exp $");
45 #include <sys/param.h>
46 #include <sys/mutex.h>
47 #include <sys/simplelock.h>
48 #include <sys/systm.h>
50 #include <dev/dmover/dmovervar.h>
52 TAILQ_HEAD(, dmover_backend
) dmover_backend_list
;
53 kmutex_t dmover_backend_list_lock
;
54 static int initialized
;
55 static struct simplelock initialized_slock
= SIMPLELOCK_INITIALIZER
;
61 simple_lock(&initialized_slock
);
62 if (__predict_true(initialized
== 0)) {
63 TAILQ_INIT(&dmover_backend_list
);
64 mutex_init(&dmover_backend_list_lock
, MUTEX_DEFAULT
, IPL_VM
);
66 /* Initialize the other bits of dmover. */
67 dmover_session_initialize();
68 dmover_request_initialize();
69 dmover_process_initialize();
73 simple_unlock(&initialized_slock
);
77 * dmover_backend_register: [back-end interface function]
79 * Register a back-end with dmover-api.
82 dmover_backend_register(struct dmover_backend
*dmb
)
85 if (__predict_false(initialized
== 0))
88 LIST_INIT(&dmb
->dmb_sessions
);
89 dmb
->dmb_nsessions
= 0;
91 TAILQ_INIT(&dmb
->dmb_pendreqs
);
92 dmb
->dmb_npendreqs
= 0;
94 mutex_enter(&dmover_backend_list_lock
);
95 TAILQ_INSERT_TAIL(&dmover_backend_list
, dmb
, dmb_list
);
96 mutex_exit(&dmover_backend_list_lock
);
100 * dmover_backend_unregister: [back-end interface function]
102 * Un-register a back-end from dmover-api.
105 dmover_backend_unregister(struct dmover_backend
*dmb
)
109 if (__predict_false(initialized
== 0)) {
112 simple_lock(&initialized_slock
);
113 croak
= (initialized
== 0);
114 simple_unlock(&initialized_slock
);
117 panic("dmover_backend_unregister: not initialized");
122 if (dmb
->dmb_nsessions
)
123 panic("dmover_backend_unregister");
125 mutex_enter(&dmover_backend_list_lock
);
126 TAILQ_REMOVE(&dmover_backend_list
, dmb
, dmb_list
);
127 mutex_exit(&dmover_backend_list_lock
);
131 * dmover_backend_alloc:
133 * Allocate and return a back-end on behalf of a session.
136 dmover_backend_alloc(struct dmover_session
*dses
, const char *type
)
138 struct dmover_backend
*dmb
, *best_dmb
= NULL
;
139 const struct dmover_algdesc
*algdesc
, *best_algdesc
= NULL
;
141 if (__predict_false(initialized
== 0)) {
144 simple_lock(&initialized_slock
);
145 fail
= (initialized
== 0);
146 simple_unlock(&initialized_slock
);
152 mutex_enter(&dmover_backend_list_lock
);
154 /* First, find a back-end that can handle the session parts. */
155 for (dmb
= TAILQ_FIRST(&dmover_backend_list
); dmb
!= NULL
;
156 dmb
= TAILQ_NEXT(dmb
, dmb_list
)) {
158 * First, check to see if the back-end supports the
159 * function we wish to perform.
161 algdesc
= dmover_algdesc_lookup(dmb
->dmb_algdescs
,
162 dmb
->dmb_nalgdescs
, type
);
166 if (best_dmb
== NULL
) {
168 best_algdesc
= algdesc
;
173 * XXX All the stuff from here on should be shot in
174 * XXX the head. Instead, we should build a list
175 * XXX of candidates, and select the best back-end
176 * XXX when a request is scheduled for processing.
179 if (dmb
->dmb_speed
>= best_dmb
->dmb_speed
) {
181 * If the current best match is slower than
182 * this back-end, then this one is the new
185 if (dmb
->dmb_speed
> best_dmb
->dmb_speed
) {
187 best_algdesc
= algdesc
;
192 * If this back-end has fewer sessions allocated
193 * to it than the current best match, then this
194 * one is now the best match.
196 if (best_dmb
->dmb_nsessions
> dmb
->dmb_nsessions
) {
198 best_algdesc
= algdesc
;
203 if (best_dmb
== NULL
) {
204 mutex_exit(&dmover_backend_list_lock
);
208 KASSERT(best_algdesc
!= NULL
);
210 /* Plug the back-end into the static (XXX) assignment. */
211 dses
->__dses_assignment
.das_backend
= best_dmb
;
212 dses
->__dses_assignment
.das_algdesc
= best_algdesc
;
214 dses
->dses_ninputs
= best_algdesc
->dad_ninputs
;
216 LIST_INSERT_HEAD(&best_dmb
->dmb_sessions
, dses
, __dses_list
);
217 best_dmb
->dmb_nsessions
++;
219 mutex_exit(&dmover_backend_list_lock
);
225 * dmover_backend_release:
227 * Release the back-end from the specified session.
230 dmover_backend_release(struct dmover_session
*dses
)
232 struct dmover_backend
*dmb
;
234 mutex_enter(&dmover_backend_list_lock
);
236 /* XXX Clear out the static assignment. */
237 dmb
= dses
->__dses_assignment
.das_backend
;
238 dses
->__dses_assignment
.das_backend
= NULL
;
239 dses
->__dses_assignment
.das_algdesc
= NULL
;
241 LIST_REMOVE(dses
, __dses_list
);
242 dmb
->dmb_nsessions
--;
244 mutex_exit(&dmover_backend_list_lock
);