Home | History | Annotate | Line # | Download | only in opencrypto
criov.c revision 1.3
      1  1.3     perry /*	$NetBSD: criov.c,v 1.3 2005/02/26 22:39:52 perry Exp $ */
      2  1.1  jonathan /*      $OpenBSD: criov.c,v 1.11 2002/06/10 19:36:43 espie Exp $	*/
      3  1.1  jonathan 
      4  1.1  jonathan /*
      5  1.1  jonathan  * Copyright (c) 1999 Theo de Raadt
      6  1.1  jonathan  *
      7  1.1  jonathan  * Redistribution and use in source and binary forms, with or without
      8  1.1  jonathan  * modification, are permitted provided that the following conditions
      9  1.1  jonathan  * are met:
     10  1.1  jonathan  *
     11  1.1  jonathan  * 1. Redistributions of source code must retain the above copyright
     12  1.1  jonathan  *   notice, this list of conditions and the following disclaimer.
     13  1.1  jonathan  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  jonathan  *   notice, this list of conditions and the following disclaimer in the
     15  1.1  jonathan  *   documentation and/or other materials provided with the distribution.
     16  1.1  jonathan  * 3. The name of the author may not be used to endorse or promote products
     17  1.1  jonathan  *   derived from this software without specific prior written permission.
     18  1.1  jonathan  *
     19  1.1  jonathan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  1.1  jonathan  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  1.1  jonathan  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  1.1  jonathan  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  1.1  jonathan  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  1.1  jonathan  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  1.1  jonathan  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  1.1  jonathan  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  1.1  jonathan  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  1.1  jonathan  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  1.1  jonathan  */
     30  1.1  jonathan 
     31  1.1  jonathan #include <sys/cdefs.h>
     32  1.3     perry __KERNEL_RCSID(0, "$NetBSD: criov.c,v 1.3 2005/02/26 22:39:52 perry Exp $");
     33  1.1  jonathan 
     34  1.1  jonathan #include <sys/param.h>
     35  1.1  jonathan #include <sys/systm.h>
     36  1.1  jonathan #include <sys/proc.h>
     37  1.1  jonathan #include <sys/errno.h>
     38  1.1  jonathan #include <sys/malloc.h>
     39  1.1  jonathan #include <sys/kernel.h>
     40  1.1  jonathan #include <sys/mbuf.h>
     41  1.1  jonathan 
     42  1.1  jonathan #include <uvm/uvm_extern.h>
     43  1.1  jonathan 
     44  1.1  jonathan #include <opencrypto/cryptodev.h>
     45  1.1  jonathan int cuio_getindx(struct uio *uio, int loc, int *off);
     46  1.1  jonathan 
     47  1.1  jonathan 
     48  1.1  jonathan void
     49  1.1  jonathan cuio_copydata(uio, off, len, cp)
     50  1.1  jonathan 	struct uio *uio;
     51  1.1  jonathan 	int off, len;
     52  1.1  jonathan 	caddr_t cp;
     53  1.1  jonathan {
     54  1.1  jonathan 	struct iovec *iov = uio->uio_iov;
     55  1.1  jonathan 	int iol = uio->uio_iovcnt;
     56  1.1  jonathan 	unsigned count;
     57  1.1  jonathan 
     58  1.1  jonathan 	if (off < 0)
     59  1.1  jonathan 		panic("cuio_copydata: off %d < 0", off);
     60  1.1  jonathan 	if (len < 0)
     61  1.1  jonathan 		panic("cuio_copydata: len %d < 0", len);
     62  1.1  jonathan 	while (off > 0) {
     63  1.1  jonathan 		if (iol == 0)
     64  1.1  jonathan 			panic("iov_copydata: empty in skip");
     65  1.1  jonathan 		if (off < iov->iov_len)
     66  1.1  jonathan 			break;
     67  1.1  jonathan 		off -= iov->iov_len;
     68  1.1  jonathan 		iol--;
     69  1.1  jonathan 		iov++;
     70  1.1  jonathan 	}
     71  1.1  jonathan 	while (len > 0) {
     72  1.1  jonathan 		if (iol == 0)
     73  1.1  jonathan 			panic("cuio_copydata: empty");
     74  1.1  jonathan 		count = min(iov->iov_len - off, len);
     75  1.1  jonathan 		bcopy(((caddr_t)iov->iov_base) + off, cp, count);
     76  1.1  jonathan 		len -= count;
     77  1.1  jonathan 		cp += count;
     78  1.1  jonathan 		off = 0;
     79  1.1  jonathan 		iol--;
     80  1.1  jonathan 		iov++;
     81  1.1  jonathan 	}
     82  1.1  jonathan }
     83  1.1  jonathan 
     84  1.1  jonathan void
     85  1.1  jonathan cuio_copyback(uio, off, len, cp)
     86  1.1  jonathan 	struct uio *uio;
     87  1.1  jonathan 	int off, len;
     88  1.1  jonathan 	caddr_t cp;
     89  1.1  jonathan {
     90  1.1  jonathan 	struct iovec *iov = uio->uio_iov;
     91  1.1  jonathan 	int iol = uio->uio_iovcnt;
     92  1.1  jonathan 	unsigned count;
     93  1.1  jonathan 
     94  1.1  jonathan 	if (off < 0)
     95  1.1  jonathan 		panic("cuio_copyback: off %d < 0", off);
     96  1.1  jonathan 	if (len < 0)
     97  1.1  jonathan 		panic("cuio_copyback: len %d < 0", len);
     98  1.1  jonathan 	while (off > 0) {
     99  1.1  jonathan 		if (iol == 0)
    100  1.1  jonathan 			panic("cuio_copyback: empty in skip");
    101  1.1  jonathan 		if (off < iov->iov_len)
    102  1.1  jonathan 			break;
    103  1.1  jonathan 		off -= iov->iov_len;
    104  1.1  jonathan 		iol--;
    105  1.1  jonathan 		iov++;
    106  1.1  jonathan 	}
    107  1.1  jonathan 	while (len > 0) {
    108  1.1  jonathan 		if (iol == 0)
    109  1.1  jonathan 			panic("uio_copyback: empty");
    110  1.1  jonathan 		count = min(iov->iov_len - off, len);
    111  1.1  jonathan 		bcopy(cp, ((caddr_t)iov->iov_base) + off, count);
    112  1.1  jonathan 		len -= count;
    113  1.1  jonathan 		cp += count;
    114  1.1  jonathan 		off = 0;
    115  1.1  jonathan 		iol--;
    116  1.1  jonathan 		iov++;
    117  1.1  jonathan 	}
    118  1.1  jonathan }
    119  1.1  jonathan 
    120  1.1  jonathan /*
    121  1.1  jonathan  * Return a pointer to iov/offset of location in iovec list.
    122  1.1  jonathan  */
    123  1.1  jonathan #ifdef __FreeBSD__
    124  1.1  jonathan struct iovec *
    125  1.1  jonathan cuio_getptr(struct uio *uio, int loc, int *off)
    126  1.1  jonathan {
    127  1.1  jonathan 	struct iovec *iov = uio->uio_iov;
    128  1.1  jonathan 	int iol = uio->uio_iovcnt;
    129  1.1  jonathan 
    130  1.1  jonathan 	while (loc >= 0) {
    131  1.1  jonathan 		/* Normal end of search */
    132  1.1  jonathan 		if (loc < iov->iov_len) {
    133  1.1  jonathan 	    		*off = loc;
    134  1.1  jonathan 	    		return (iov);
    135  1.1  jonathan 		}
    136  1.1  jonathan 
    137  1.1  jonathan 		loc -= iov->iov_len;
    138  1.1  jonathan 		if (iol == 0) {
    139  1.1  jonathan 			if (loc == 0) {
    140  1.1  jonathan 				/* Point at the end of valid data */
    141  1.1  jonathan 				*off = iov->iov_len;
    142  1.1  jonathan 				return (iov);
    143  1.1  jonathan 			} else
    144  1.1  jonathan 				return (NULL);
    145  1.1  jonathan 		} else {
    146  1.1  jonathan 			iov++, iol--;
    147  1.1  jonathan 		}
    148  1.1  jonathan     	}
    149  1.1  jonathan 
    150  1.1  jonathan 	return (NULL);
    151  1.1  jonathan }
    152  1.1  jonathan 
    153  1.1  jonathan #else
    154  1.1  jonathan 
    155  1.1  jonathan int
    156  1.1  jonathan cuio_getptr(struct uio *uio, int loc, int *off)
    157  1.1  jonathan {
    158  1.1  jonathan 	int ind, len;
    159  1.1  jonathan 
    160  1.1  jonathan 	ind = 0;
    161  1.1  jonathan 	while (loc >= 0 && ind < uio->uio_iovcnt) {
    162  1.1  jonathan 		len = uio->uio_iov[ind].iov_len;
    163  1.1  jonathan 		if (len > loc) {
    164  1.1  jonathan 			*off = loc;
    165  1.1  jonathan 			return (ind);
    166  1.1  jonathan 		}
    167  1.1  jonathan 		loc -= len;
    168  1.1  jonathan 		ind++;
    169  1.1  jonathan 	}
    170  1.1  jonathan 
    171  1.1  jonathan 	if (ind > 0 && loc == 0) {
    172  1.1  jonathan 		ind--;
    173  1.1  jonathan 		*off = uio->uio_iov[ind].iov_len;
    174  1.1  jonathan 		return (ind);
    175  1.1  jonathan 	}
    176  1.1  jonathan 
    177  1.1  jonathan 	return (-1);
    178  1.1  jonathan }
    179  1.1  jonathan #endif
    180  1.1  jonathan 
    181  1.1  jonathan int
    182  1.1  jonathan cuio_apply(struct uio *uio, int off, int len,
    183  1.1  jonathan     int (*f)(caddr_t, caddr_t, unsigned int), caddr_t fstate)
    184  1.1  jonathan {
    185  1.1  jonathan 	int rval, ind, uiolen;
    186  1.1  jonathan 	unsigned int count;
    187  1.1  jonathan 
    188  1.1  jonathan 	if (len < 0)
    189  1.1  jonathan 		panic("%s: len %d < 0", __func__, len);
    190  1.1  jonathan 	if (off < 0)
    191  1.1  jonathan 		panic("%s: off %d < 0", __func__, off);
    192  1.3     perry 
    193  1.1  jonathan 	ind = 0;
    194  1.1  jonathan 	while (off > 0) {
    195  1.1  jonathan 		if (ind >= uio->uio_iovcnt)
    196  1.2       lha 			panic("cuio_apply: out of ivecs before data in uio");
    197  1.1  jonathan 		uiolen = uio->uio_iov[ind].iov_len;
    198  1.1  jonathan 		if (off < uiolen)
    199  1.1  jonathan 			break;
    200  1.1  jonathan 		off -= uiolen;
    201  1.1  jonathan 		ind++;
    202  1.1  jonathan 	}
    203  1.1  jonathan 	while (len > 0) {
    204  1.1  jonathan 		if (ind >= uio->uio_iovcnt)
    205  1.2       lha 			panic("cuio_apply: out of ivecs when processing uio");
    206  1.1  jonathan 		count = min(uio->uio_iov[ind].iov_len - off, len);
    207  1.1  jonathan 
    208  1.3     perry 		rval = f(fstate,
    209  1.1  jonathan 			 ((caddr_t)uio->uio_iov[ind].iov_base + off), count);
    210  1.1  jonathan 		if (rval)
    211  1.1  jonathan 			return (rval);
    212  1.1  jonathan 
    213  1.1  jonathan 		len -= count;
    214  1.1  jonathan 		off = 0;
    215  1.1  jonathan 		ind++;
    216  1.1  jonathan 	}
    217  1.1  jonathan 
    218  1.1  jonathan 	return (0);
    219  1.1  jonathan }
    220