swdmover.c revision 1.4 1 1.4 thorpej /* $NetBSD: swdmover.c,v 1.4 2003/03/06 21:10:45 thorpej 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 * swdmover.c: Software back-end providing the dmover functions
40 1.1 thorpej * mentioned in dmover(9).
41 1.1 thorpej *
42 1.1 thorpej * This module provides a fallback for cases where no hardware
43 1.1 thorpej * data movers are present in a system, and also serves an an
44 1.1 thorpej * example of how to write a dmover back-end.
45 1.1 thorpej *
46 1.1 thorpej * Note that even through the software dmover doesn't require
47 1.1 thorpej * interrupts to be blocked, we block them anyway to demonstrate
48 1.1 thorpej * the locking protocol.
49 1.1 thorpej */
50 1.1 thorpej
51 1.1 thorpej #include <sys/cdefs.h>
52 1.4 thorpej __KERNEL_RCSID(0, "$NetBSD: swdmover.c,v 1.4 2003/03/06 21:10:45 thorpej Exp $");
53 1.1 thorpej
54 1.1 thorpej #include <sys/param.h>
55 1.1 thorpej #include <sys/lock.h>
56 1.1 thorpej #include <sys/kthread.h>
57 1.1 thorpej #include <sys/systm.h>
58 1.1 thorpej #include <sys/uio.h>
59 1.1 thorpej
60 1.1 thorpej #include <dev/dmover/dmovervar.h>
61 1.1 thorpej
62 1.1 thorpej struct swdmover_function {
63 1.1 thorpej void (*sdf_process)(struct dmover_request *);
64 1.1 thorpej };
65 1.1 thorpej
66 1.1 thorpej static struct dmover_backend swdmover_backend;
67 1.1 thorpej static struct proc *swdmover_proc;
68 1.1 thorpej static int swdmover_cv;
69 1.1 thorpej
70 1.1 thorpej void swdmoverattach(int);
71 1.1 thorpej
72 1.1 thorpej /*
73 1.1 thorpej * swdmover_process:
74 1.1 thorpej *
75 1.1 thorpej * Dmover back-end entry point.
76 1.1 thorpej */
77 1.1 thorpej static void
78 1.1 thorpej swdmover_process(struct dmover_backend *dmb)
79 1.1 thorpej {
80 1.1 thorpej int s;
81 1.1 thorpej
82 1.1 thorpej /*
83 1.1 thorpej * Just wake up the processing thread. This will allow
84 1.1 thorpej * requests to linger on the middle-end's queue so that
85 1.1 thorpej * they can be cancelled, if need-be.
86 1.1 thorpej */
87 1.1 thorpej s = splbio();
88 1.1 thorpej /* XXXLOCK */
89 1.1 thorpej if (TAILQ_EMPTY(&dmb->dmb_pendreqs) == 0)
90 1.1 thorpej wakeup(&swdmover_cv);
91 1.1 thorpej /* XXXUNLOCK */
92 1.1 thorpej splx(s);
93 1.1 thorpej }
94 1.1 thorpej
95 1.1 thorpej /*
96 1.1 thorpej * swdmover_thread:
97 1.1 thorpej *
98 1.1 thorpej * Request processing thread.
99 1.1 thorpej */
100 1.1 thorpej static void
101 1.1 thorpej swdmover_thread(void *arg)
102 1.1 thorpej {
103 1.1 thorpej struct dmover_backend *dmb = arg;
104 1.1 thorpej struct dmover_request *dreq;
105 1.1 thorpej struct swdmover_function *sdf;
106 1.1 thorpej int s;
107 1.1 thorpej
108 1.1 thorpej s = splbio();
109 1.1 thorpej /* XXXLOCK */
110 1.1 thorpej
111 1.1 thorpej for (;;) {
112 1.1 thorpej dreq = TAILQ_FIRST(&dmb->dmb_pendreqs);
113 1.1 thorpej if (dreq == NULL) {
114 1.1 thorpej /* XXXUNLOCK */
115 1.1 thorpej (void) tsleep(&swdmover_cv, PRIBIO, "swdmvr", 0);
116 1.1 thorpej continue;
117 1.1 thorpej }
118 1.1 thorpej
119 1.1 thorpej dmover_backend_remque(dmb, dreq);
120 1.1 thorpej dreq->dreq_flags |= DMOVER_REQ_RUNNING;
121 1.1 thorpej
122 1.1 thorpej /* XXXUNLOCK */
123 1.1 thorpej splx(s);
124 1.1 thorpej
125 1.1 thorpej sdf = dreq->dreq_assignment->das_algdesc->dad_data;
126 1.1 thorpej (*sdf->sdf_process)(dreq);
127 1.1 thorpej
128 1.1 thorpej s = splbio();
129 1.1 thorpej /* XXXLOCK */
130 1.1 thorpej }
131 1.1 thorpej }
132 1.1 thorpej
133 1.1 thorpej /*
134 1.1 thorpej * swdmover_func_zero_process:
135 1.1 thorpej *
136 1.1 thorpej * Processing routine for the "zero" function.
137 1.1 thorpej */
138 1.1 thorpej static void
139 1.1 thorpej swdmover_func_zero_process(struct dmover_request *dreq)
140 1.1 thorpej {
141 1.1 thorpej
142 1.1 thorpej switch (dreq->dreq_outbuf_type) {
143 1.1 thorpej case DMOVER_BUF_LINEAR:
144 1.1 thorpej memset(dreq->dreq_outbuf.dmbuf_linear.l_addr, 0,
145 1.1 thorpej dreq->dreq_outbuf.dmbuf_linear.l_len);
146 1.1 thorpej break;
147 1.1 thorpej
148 1.1 thorpej case DMOVER_BUF_UIO:
149 1.1 thorpej {
150 1.1 thorpej struct uio *uio = dreq->dreq_outbuf.dmbuf_uio;
151 1.1 thorpej char *cp;
152 1.1 thorpej size_t count, buflen;
153 1.1 thorpej int error;
154 1.1 thorpej
155 1.1 thorpej if (uio->uio_rw != UIO_READ) {
156 1.1 thorpej /* XXXLOCK */
157 1.1 thorpej dreq->dreq_error = EINVAL;
158 1.1 thorpej dreq->dreq_flags |= DMOVER_REQ_ERROR;
159 1.1 thorpej /* XXXUNLOCK */
160 1.1 thorpej break;
161 1.1 thorpej }
162 1.1 thorpej
163 1.1 thorpej buflen = uio->uio_resid;
164 1.1 thorpej if (buflen > 1024)
165 1.1 thorpej buflen = 1024;
166 1.1 thorpej cp = alloca(buflen);
167 1.1 thorpej memset(cp, 0, buflen);
168 1.1 thorpej
169 1.1 thorpej while ((count = uio->uio_resid) != 0) {
170 1.1 thorpej if (count > buflen)
171 1.1 thorpej count = buflen;
172 1.1 thorpej error = uiomove(cp, count, uio);
173 1.1 thorpej if (error) {
174 1.1 thorpej /* XXXLOCK */
175 1.1 thorpej dreq->dreq_error = error;
176 1.1 thorpej dreq->dreq_flags |= DMOVER_REQ_ERROR;
177 1.1 thorpej /* XXXUNLOCK */
178 1.1 thorpej break;
179 1.1 thorpej }
180 1.1 thorpej }
181 1.1 thorpej break;
182 1.1 thorpej }
183 1.3 thorpej
184 1.3 thorpej default:
185 1.3 thorpej /* XXXLOCK */
186 1.3 thorpej dreq->dreq_error = EINVAL;
187 1.3 thorpej dreq->dreq_flags |= DMOVER_REQ_ERROR;
188 1.3 thorpej /* XXXUNLOCK */
189 1.1 thorpej }
190 1.1 thorpej
191 1.1 thorpej dmover_done(dreq);
192 1.1 thorpej }
193 1.1 thorpej
194 1.1 thorpej /*
195 1.1 thorpej * swdmover_func_fill8_process:
196 1.1 thorpej *
197 1.1 thorpej * Processing routine for the "fill8" function.
198 1.1 thorpej */
199 1.1 thorpej static void
200 1.1 thorpej swdmover_func_fill8_process(struct dmover_request *dreq)
201 1.1 thorpej {
202 1.1 thorpej
203 1.1 thorpej switch (dreq->dreq_outbuf_type) {
204 1.1 thorpej case DMOVER_BUF_LINEAR:
205 1.1 thorpej memset(dreq->dreq_outbuf.dmbuf_linear.l_addr,
206 1.1 thorpej dreq->dreq_immediate[0],
207 1.1 thorpej dreq->dreq_outbuf.dmbuf_linear.l_len);
208 1.1 thorpej break;
209 1.1 thorpej
210 1.1 thorpej case DMOVER_BUF_UIO:
211 1.1 thorpej {
212 1.1 thorpej struct uio *uio = dreq->dreq_outbuf.dmbuf_uio;
213 1.1 thorpej char *cp;
214 1.1 thorpej size_t count, buflen;
215 1.1 thorpej int error;
216 1.1 thorpej
217 1.1 thorpej if (uio->uio_rw != UIO_READ) {
218 1.1 thorpej /* XXXLOCK */
219 1.1 thorpej dreq->dreq_error = EINVAL;
220 1.1 thorpej dreq->dreq_flags |= DMOVER_REQ_ERROR;
221 1.1 thorpej /* XXXUNLOCK */
222 1.1 thorpej break;
223 1.1 thorpej }
224 1.1 thorpej
225 1.1 thorpej buflen = uio->uio_resid;
226 1.1 thorpej if (buflen > 1024)
227 1.1 thorpej buflen = 1024;
228 1.1 thorpej cp = alloca(buflen);
229 1.1 thorpej memset(cp, dreq->dreq_immediate[0], buflen);
230 1.1 thorpej
231 1.1 thorpej while ((count = uio->uio_resid) != 0) {
232 1.1 thorpej if (count > buflen)
233 1.1 thorpej count = buflen;
234 1.1 thorpej error = uiomove(cp, count, uio);
235 1.1 thorpej if (error) {
236 1.1 thorpej /* XXXLOCK */
237 1.1 thorpej dreq->dreq_error = error;
238 1.1 thorpej dreq->dreq_flags |= DMOVER_REQ_ERROR;
239 1.1 thorpej /* XXXUNLOCK */
240 1.1 thorpej break;
241 1.1 thorpej }
242 1.1 thorpej }
243 1.1 thorpej break;
244 1.1 thorpej }
245 1.3 thorpej
246 1.3 thorpej default:
247 1.3 thorpej /* XXXLOCK */
248 1.3 thorpej dreq->dreq_error = EINVAL;
249 1.3 thorpej dreq->dreq_flags |= DMOVER_REQ_ERROR;
250 1.3 thorpej /* XXXUNLOCK */
251 1.1 thorpej }
252 1.1 thorpej
253 1.1 thorpej dmover_done(dreq);
254 1.1 thorpej }
255 1.1 thorpej
256 1.1 thorpej /*
257 1.1 thorpej * swdmover_func_copy_process:
258 1.1 thorpej *
259 1.1 thorpej * Processing routine for the "copy" function.
260 1.1 thorpej */
261 1.1 thorpej static void
262 1.1 thorpej swdmover_func_copy_process(struct dmover_request *dreq)
263 1.1 thorpej {
264 1.1 thorpej
265 1.4 thorpej /* XXX Currently, both buffers must be of same type. */
266 1.4 thorpej if (dreq->dreq_inbuf_type != dreq->dreq_outbuf_type) {
267 1.4 thorpej /* XXXLOCK */
268 1.4 thorpej dreq->dreq_error = EINVAL;
269 1.4 thorpej dreq->dreq_flags |= DMOVER_REQ_ERROR;
270 1.4 thorpej /* XXXUNLOCK */
271 1.4 thorpej goto done;
272 1.4 thorpej }
273 1.4 thorpej
274 1.1 thorpej switch (dreq->dreq_outbuf_type) {
275 1.1 thorpej case DMOVER_BUF_LINEAR:
276 1.1 thorpej if (dreq->dreq_outbuf.dmbuf_linear.l_len !=
277 1.1 thorpej dreq->dreq_inbuf[0].dmbuf_linear.l_len) {
278 1.1 thorpej /* XXXLOCK */
279 1.1 thorpej dreq->dreq_error = EINVAL;
280 1.1 thorpej dreq->dreq_flags |= DMOVER_REQ_ERROR;
281 1.1 thorpej /* XXXUNLOCK */
282 1.1 thorpej break;
283 1.1 thorpej }
284 1.1 thorpej memcpy(dreq->dreq_outbuf.dmbuf_linear.l_addr,
285 1.1 thorpej dreq->dreq_inbuf[0].dmbuf_linear.l_addr,
286 1.1 thorpej dreq->dreq_outbuf.dmbuf_linear.l_len);
287 1.1 thorpej break;
288 1.1 thorpej
289 1.1 thorpej case DMOVER_BUF_UIO:
290 1.1 thorpej {
291 1.1 thorpej struct uio *uio_out = dreq->dreq_outbuf.dmbuf_uio;
292 1.1 thorpej struct uio *uio_in = dreq->dreq_inbuf[0].dmbuf_uio;
293 1.1 thorpej char *cp;
294 1.1 thorpej size_t count, buflen;
295 1.1 thorpej int error;
296 1.1 thorpej
297 1.1 thorpej if (uio_in->uio_rw != UIO_WRITE ||
298 1.1 thorpej uio_out->uio_rw != UIO_READ ||
299 1.1 thorpej uio_in->uio_resid != uio_out->uio_resid) {
300 1.1 thorpej /* XXXLOCK */
301 1.1 thorpej dreq->dreq_error = EINVAL;
302 1.1 thorpej dreq->dreq_flags |= DMOVER_REQ_ERROR;
303 1.1 thorpej /* XXXUNLOCK */
304 1.1 thorpej break;
305 1.1 thorpej }
306 1.1 thorpej
307 1.1 thorpej buflen = uio_in->uio_resid;
308 1.1 thorpej if (buflen > 1024)
309 1.1 thorpej buflen = 1024;
310 1.1 thorpej cp = alloca(buflen);
311 1.1 thorpej
312 1.1 thorpej while ((count = uio_in->uio_resid) != 0) {
313 1.1 thorpej if (count > buflen)
314 1.1 thorpej count = buflen;
315 1.1 thorpej error = uiomove(cp, count, uio_in);
316 1.1 thorpej if (error == 0)
317 1.1 thorpej error = uiomove(cp, count, uio_out);
318 1.1 thorpej if (error) {
319 1.1 thorpej /* XXXLOCK */
320 1.1 thorpej dreq->dreq_error = error;
321 1.1 thorpej dreq->dreq_flags |= DMOVER_REQ_ERROR;
322 1.1 thorpej /* XXXUNLOCK */
323 1.1 thorpej break;
324 1.1 thorpej }
325 1.1 thorpej }
326 1.1 thorpej break;
327 1.1 thorpej }
328 1.3 thorpej
329 1.3 thorpej default:
330 1.3 thorpej /* XXXLOCK */
331 1.3 thorpej dreq->dreq_error = EINVAL;
332 1.3 thorpej dreq->dreq_flags |= DMOVER_REQ_ERROR;
333 1.3 thorpej /* XXXUNLOCK */
334 1.1 thorpej }
335 1.1 thorpej
336 1.4 thorpej done:
337 1.1 thorpej dmover_done(dreq);
338 1.1 thorpej }
339 1.1 thorpej
340 1.1 thorpej static struct swdmover_function swdmover_func_zero = {
341 1.1 thorpej swdmover_func_zero_process
342 1.1 thorpej };
343 1.1 thorpej
344 1.1 thorpej static struct swdmover_function swdmover_func_fill8 = {
345 1.1 thorpej swdmover_func_fill8_process
346 1.1 thorpej };
347 1.1 thorpej
348 1.1 thorpej struct swdmover_function swdmover_func_copy = {
349 1.1 thorpej swdmover_func_copy_process
350 1.1 thorpej };
351 1.1 thorpej
352 1.1 thorpej const struct dmover_algdesc swdmover_algdescs[] = {
353 1.1 thorpej {
354 1.1 thorpej DMOVER_FUNC_ZERO,
355 1.1 thorpej &swdmover_func_zero,
356 1.1 thorpej 0
357 1.1 thorpej },
358 1.1 thorpej {
359 1.1 thorpej DMOVER_FUNC_FILL8,
360 1.1 thorpej &swdmover_func_fill8,
361 1.1 thorpej 0
362 1.1 thorpej },
363 1.1 thorpej {
364 1.1 thorpej DMOVER_FUNC_COPY,
365 1.1 thorpej &swdmover_func_copy,
366 1.1 thorpej 1
367 1.1 thorpej },
368 1.1 thorpej };
369 1.1 thorpej #define SWDMOVER_ALGDESC_COUNT \
370 1.1 thorpej (sizeof(swdmover_algdescs) / sizeof(swdmover_algdescs[0]))
371 1.1 thorpej
372 1.1 thorpej /*
373 1.1 thorpej * swdmover_create_thread:
374 1.1 thorpej *
375 1.1 thorpej * Actually create the swdmover processing thread.
376 1.1 thorpej */
377 1.1 thorpej static void
378 1.1 thorpej swdmover_create_thread(void *arg)
379 1.1 thorpej {
380 1.1 thorpej int error;
381 1.1 thorpej
382 1.1 thorpej error = kthread_create1(swdmover_thread, arg, &swdmover_proc,
383 1.1 thorpej "swdmover");
384 1.1 thorpej if (error)
385 1.1 thorpej printf("WARNING: unable to create swdmover thread, "
386 1.1 thorpej "error = %d\n", error);
387 1.1 thorpej }
388 1.1 thorpej
389 1.1 thorpej /*
390 1.1 thorpej * swdmoverattach:
391 1.1 thorpej *
392 1.1 thorpej * Pesudo-device attach routine.
393 1.1 thorpej */
394 1.1 thorpej void
395 1.1 thorpej swdmoverattach(int count)
396 1.1 thorpej {
397 1.1 thorpej
398 1.1 thorpej swdmover_backend.dmb_name = "swdmover";
399 1.1 thorpej swdmover_backend.dmb_speed = 1; /* XXX */
400 1.1 thorpej swdmover_backend.dmb_cookie = NULL;
401 1.1 thorpej swdmover_backend.dmb_algdescs = swdmover_algdescs;
402 1.1 thorpej swdmover_backend.dmb_nalgdescs = SWDMOVER_ALGDESC_COUNT;
403 1.1 thorpej swdmover_backend.dmb_process = swdmover_process;
404 1.1 thorpej
405 1.1 thorpej kthread_create(swdmover_create_thread, &swdmover_backend);
406 1.1 thorpej
407 1.1 thorpej /* XXX Should only register this when kthread creation succeeds. */
408 1.1 thorpej dmover_backend_register(&swdmover_backend);
409 1.1 thorpej }
410