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