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