Home | History | Annotate | Line # | Download | only in dmover
swdmover.c revision 1.1
      1  1.1  thorpej /*	$NetBSD: swdmover.c,v 1.1 2002/08/02 00:30:40 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.1  thorpej __KERNEL_RCSID(0, "$NetBSD");
     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.1  thorpej 	}
    184  1.1  thorpej 
    185  1.1  thorpej 	dmover_done(dreq);
    186  1.1  thorpej }
    187  1.1  thorpej 
    188  1.1  thorpej /*
    189  1.1  thorpej  * swdmover_func_fill8_process:
    190  1.1  thorpej  *
    191  1.1  thorpej  *	Processing routine for the "fill8" function.
    192  1.1  thorpej  */
    193  1.1  thorpej static void
    194  1.1  thorpej swdmover_func_fill8_process(struct dmover_request *dreq)
    195  1.1  thorpej {
    196  1.1  thorpej 
    197  1.1  thorpej 	switch (dreq->dreq_outbuf_type) {
    198  1.1  thorpej 	case DMOVER_BUF_LINEAR:
    199  1.1  thorpej 		memset(dreq->dreq_outbuf.dmbuf_linear.l_addr,
    200  1.1  thorpej 		    dreq->dreq_immediate[0],
    201  1.1  thorpej 		    dreq->dreq_outbuf.dmbuf_linear.l_len);
    202  1.1  thorpej 		break;
    203  1.1  thorpej 
    204  1.1  thorpej 	case DMOVER_BUF_UIO:
    205  1.1  thorpej 	    {
    206  1.1  thorpej 		struct uio *uio = dreq->dreq_outbuf.dmbuf_uio;
    207  1.1  thorpej 		char *cp;
    208  1.1  thorpej 		size_t count, buflen;
    209  1.1  thorpej 		int error;
    210  1.1  thorpej 
    211  1.1  thorpej 		if (uio->uio_rw != UIO_READ) {
    212  1.1  thorpej 			/* XXXLOCK */
    213  1.1  thorpej 			dreq->dreq_error = EINVAL;
    214  1.1  thorpej 			dreq->dreq_flags |= DMOVER_REQ_ERROR;
    215  1.1  thorpej 			/* XXXUNLOCK */
    216  1.1  thorpej 			break;
    217  1.1  thorpej 		}
    218  1.1  thorpej 
    219  1.1  thorpej 		buflen = uio->uio_resid;
    220  1.1  thorpej 		if (buflen > 1024)
    221  1.1  thorpej 			buflen = 1024;
    222  1.1  thorpej 		cp = alloca(buflen);
    223  1.1  thorpej 		memset(cp, dreq->dreq_immediate[0], buflen);
    224  1.1  thorpej 
    225  1.1  thorpej 		while ((count = uio->uio_resid) != 0) {
    226  1.1  thorpej 			if (count > buflen)
    227  1.1  thorpej 				count = buflen;
    228  1.1  thorpej 			error = uiomove(cp, count, uio);
    229  1.1  thorpej 			if (error) {
    230  1.1  thorpej 				/* XXXLOCK */
    231  1.1  thorpej 				dreq->dreq_error = error;
    232  1.1  thorpej 				dreq->dreq_flags |= DMOVER_REQ_ERROR;
    233  1.1  thorpej 				/* XXXUNLOCK */
    234  1.1  thorpej 				break;
    235  1.1  thorpej 			}
    236  1.1  thorpej 		}
    237  1.1  thorpej 		break;
    238  1.1  thorpej 	    }
    239  1.1  thorpej 	}
    240  1.1  thorpej 
    241  1.1  thorpej 	dmover_done(dreq);
    242  1.1  thorpej }
    243  1.1  thorpej 
    244  1.1  thorpej /*
    245  1.1  thorpej  * swdmover_func_copy_process:
    246  1.1  thorpej  *
    247  1.1  thorpej  *	Processing routine for the "copy" function.
    248  1.1  thorpej  */
    249  1.1  thorpej static void
    250  1.1  thorpej swdmover_func_copy_process(struct dmover_request *dreq)
    251  1.1  thorpej {
    252  1.1  thorpej 
    253  1.1  thorpej 	/*
    254  1.1  thorpej 	 * Middle-end makes sure input and output buffers are of
    255  1.1  thorpej 	 * the same type.
    256  1.1  thorpej 	 */
    257  1.1  thorpej 	switch (dreq->dreq_outbuf_type) {
    258  1.1  thorpej 	case DMOVER_BUF_LINEAR:
    259  1.1  thorpej 		if (dreq->dreq_outbuf.dmbuf_linear.l_len !=
    260  1.1  thorpej 		    dreq->dreq_inbuf[0].dmbuf_linear.l_len) {
    261  1.1  thorpej 			/* XXXLOCK */
    262  1.1  thorpej 			dreq->dreq_error = EINVAL;
    263  1.1  thorpej 			dreq->dreq_flags |= DMOVER_REQ_ERROR;
    264  1.1  thorpej 			/* XXXUNLOCK */
    265  1.1  thorpej 			break;
    266  1.1  thorpej 		}
    267  1.1  thorpej 		memcpy(dreq->dreq_outbuf.dmbuf_linear.l_addr,
    268  1.1  thorpej 		    dreq->dreq_inbuf[0].dmbuf_linear.l_addr,
    269  1.1  thorpej 		    dreq->dreq_outbuf.dmbuf_linear.l_len);
    270  1.1  thorpej 		break;
    271  1.1  thorpej 
    272  1.1  thorpej 	case DMOVER_BUF_UIO:
    273  1.1  thorpej 	    {
    274  1.1  thorpej 		struct uio *uio_out = dreq->dreq_outbuf.dmbuf_uio;
    275  1.1  thorpej 		struct uio *uio_in = dreq->dreq_inbuf[0].dmbuf_uio;
    276  1.1  thorpej 		char *cp;
    277  1.1  thorpej 		size_t count, buflen;
    278  1.1  thorpej 		int error;
    279  1.1  thorpej 
    280  1.1  thorpej 		if (uio_in->uio_rw != UIO_WRITE ||
    281  1.1  thorpej 		    uio_out->uio_rw != UIO_READ ||
    282  1.1  thorpej 		    uio_in->uio_resid != uio_out->uio_resid) {
    283  1.1  thorpej 			/* XXXLOCK */
    284  1.1  thorpej 			dreq->dreq_error = EINVAL;
    285  1.1  thorpej 			dreq->dreq_flags |= DMOVER_REQ_ERROR;
    286  1.1  thorpej 			/* XXXUNLOCK */
    287  1.1  thorpej 			break;
    288  1.1  thorpej 		}
    289  1.1  thorpej 
    290  1.1  thorpej 		buflen = uio_in->uio_resid;
    291  1.1  thorpej 		if (buflen > 1024)
    292  1.1  thorpej 			buflen = 1024;
    293  1.1  thorpej 		cp = alloca(buflen);
    294  1.1  thorpej 
    295  1.1  thorpej 		while ((count = uio_in->uio_resid) != 0) {
    296  1.1  thorpej 			if (count > buflen)
    297  1.1  thorpej 				count = buflen;
    298  1.1  thorpej 			error = uiomove(cp, count, uio_in);
    299  1.1  thorpej 			if (error == 0)
    300  1.1  thorpej 				error = uiomove(cp, count, uio_out);
    301  1.1  thorpej 			if (error) {
    302  1.1  thorpej 				/* XXXLOCK */
    303  1.1  thorpej 				dreq->dreq_error = error;
    304  1.1  thorpej 				dreq->dreq_flags |= DMOVER_REQ_ERROR;
    305  1.1  thorpej 				/* XXXUNLOCK */
    306  1.1  thorpej 				break;
    307  1.1  thorpej 			}
    308  1.1  thorpej 		}
    309  1.1  thorpej 		break;
    310  1.1  thorpej 	    }
    311  1.1  thorpej 	}
    312  1.1  thorpej 
    313  1.1  thorpej 	dmover_done(dreq);
    314  1.1  thorpej }
    315  1.1  thorpej 
    316  1.1  thorpej static struct swdmover_function swdmover_func_zero = {
    317  1.1  thorpej 	swdmover_func_zero_process
    318  1.1  thorpej };
    319  1.1  thorpej 
    320  1.1  thorpej static struct swdmover_function swdmover_func_fill8 = {
    321  1.1  thorpej 	swdmover_func_fill8_process
    322  1.1  thorpej };
    323  1.1  thorpej 
    324  1.1  thorpej struct swdmover_function swdmover_func_copy = {
    325  1.1  thorpej 	swdmover_func_copy_process
    326  1.1  thorpej };
    327  1.1  thorpej 
    328  1.1  thorpej const struct dmover_algdesc swdmover_algdescs[] = {
    329  1.1  thorpej 	{
    330  1.1  thorpej 	  DMOVER_FUNC_ZERO,
    331  1.1  thorpej 	  &swdmover_func_zero,
    332  1.1  thorpej 	  0
    333  1.1  thorpej 	},
    334  1.1  thorpej 	{
    335  1.1  thorpej 	  DMOVER_FUNC_FILL8,
    336  1.1  thorpej 	  &swdmover_func_fill8,
    337  1.1  thorpej 	  0
    338  1.1  thorpej 	},
    339  1.1  thorpej 	{
    340  1.1  thorpej 	  DMOVER_FUNC_COPY,
    341  1.1  thorpej 	  &swdmover_func_copy,
    342  1.1  thorpej 	  1
    343  1.1  thorpej 	},
    344  1.1  thorpej };
    345  1.1  thorpej #define	SWDMOVER_ALGDESC_COUNT \
    346  1.1  thorpej 	(sizeof(swdmover_algdescs) / sizeof(swdmover_algdescs[0]))
    347  1.1  thorpej 
    348  1.1  thorpej /*
    349  1.1  thorpej  * swdmover_create_thread:
    350  1.1  thorpej  *
    351  1.1  thorpej  *	Actually create the swdmover processing thread.
    352  1.1  thorpej  */
    353  1.1  thorpej static void
    354  1.1  thorpej swdmover_create_thread(void *arg)
    355  1.1  thorpej {
    356  1.1  thorpej 	int error;
    357  1.1  thorpej 
    358  1.1  thorpej 	error = kthread_create1(swdmover_thread, arg, &swdmover_proc,
    359  1.1  thorpej 	    "swdmover");
    360  1.1  thorpej 	if (error)
    361  1.1  thorpej 		printf("WARNING: unable to create swdmover thread, "
    362  1.1  thorpej 		    "error = %d\n", error);
    363  1.1  thorpej }
    364  1.1  thorpej 
    365  1.1  thorpej /*
    366  1.1  thorpej  * swdmoverattach:
    367  1.1  thorpej  *
    368  1.1  thorpej  *	Pesudo-device attach routine.
    369  1.1  thorpej  */
    370  1.1  thorpej void
    371  1.1  thorpej swdmoverattach(int count)
    372  1.1  thorpej {
    373  1.1  thorpej 
    374  1.1  thorpej 	swdmover_backend.dmb_name = "swdmover";
    375  1.1  thorpej 	swdmover_backend.dmb_speed = 1;		/* XXX */
    376  1.1  thorpej 	swdmover_backend.dmb_cookie = NULL;
    377  1.1  thorpej 	swdmover_backend.dmb_algdescs = swdmover_algdescs;
    378  1.1  thorpej 	swdmover_backend.dmb_nalgdescs = SWDMOVER_ALGDESC_COUNT;
    379  1.1  thorpej 	swdmover_backend.dmb_process = swdmover_process;
    380  1.1  thorpej 
    381  1.1  thorpej 	kthread_create(swdmover_create_thread, &swdmover_backend);
    382  1.1  thorpej 
    383  1.1  thorpej 	/* XXX Should only register this when kthread creation succeeds. */
    384  1.1  thorpej 	dmover_backend_register(&swdmover_backend);
    385  1.1  thorpej }
    386