kernel: linux/version.h was removed in kernel 2.6.19 and is now replaced by generated...
[15.05/openwrt.git] / target / linux / generic / files / crypto / ocf / criov.c
1 /*      $OpenBSD: criov.c,v 1.9 2002/01/29 15:48:29 jason Exp $ */
2
3 /*
4  * Linux port done by David McCullough <david_mccullough@mcafee.com>
5  * Copyright (C) 2006-2010 David McCullough
6  * Copyright (C) 2004-2005 Intel Corporation.
7  * The license and original author are listed below.
8  *
9  * Copyright (c) 1999 Theo de Raadt
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  *   notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *   notice, this list of conditions and the following disclaimer in the
19  *   documentation and/or other materials provided with the distribution.
20  * 3. The name of the author may not be used to endorse or promote products
21  *   derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34 __FBSDID("$FreeBSD: src/sys/opencrypto/criov.c,v 1.5 2006/06/04 22:15:13 pjd Exp $");
35  */
36
37 #include <linux/version.h>
38 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33))
39 #include <generated/autoconf.h>
40 #else
41 #include <linux/autoconf.h>
42 #endif
43 #include <linux/module.h>
44 #include <linux/init.h>
45 #include <linux/slab.h>
46 #include <linux/uio.h>
47 #include <linux/skbuff.h>
48 #include <linux/kernel.h>
49 #include <linux/mm.h>
50 #include <asm/io.h>
51
52 #include <uio.h>
53 #include <cryptodev.h>
54
55 /*
56  * This macro is only for avoiding code duplication, as we need to skip
57  * given number of bytes in the same way in three functions below.
58  */
59 #define CUIO_SKIP()     do {                                            \
60         KASSERT(off >= 0, ("%s: off %d < 0", __func__, off));           \
61         KASSERT(len >= 0, ("%s: len %d < 0", __func__, len));           \
62         while (off > 0) {                                               \
63                 KASSERT(iol >= 0, ("%s: empty in skip", __func__));     \
64                 if (off < iov->iov_len)                                 \
65                         break;                                          \
66                 off -= iov->iov_len;                                    \
67                 iol--;                                                  \
68                 iov++;                                                  \
69         }                                                               \
70 } while (0)
71
72 void
73 cuio_copydata(struct uio* uio, int off, int len, caddr_t cp)
74 {
75         struct iovec *iov = uio->uio_iov;
76         int iol = uio->uio_iovcnt;
77         unsigned count;
78
79         CUIO_SKIP();
80         while (len > 0) {
81                 KASSERT(iol >= 0, ("%s: empty", __func__));
82                 count = min((int)(iov->iov_len - off), len);
83                 memcpy(cp, ((caddr_t)iov->iov_base) + off, count);
84                 len -= count;
85                 cp += count;
86                 off = 0;
87                 iol--;
88                 iov++;
89         }
90 }
91
92 void
93 cuio_copyback(struct uio* uio, int off, int len, caddr_t cp)
94 {
95         struct iovec *iov = uio->uio_iov;
96         int iol = uio->uio_iovcnt;
97         unsigned count;
98
99         CUIO_SKIP();
100         while (len > 0) {
101                 KASSERT(iol >= 0, ("%s: empty", __func__));
102                 count = min((int)(iov->iov_len - off), len);
103                 memcpy(((caddr_t)iov->iov_base) + off, cp, count);
104                 len -= count;
105                 cp += count;
106                 off = 0;
107                 iol--;
108                 iov++;
109         }
110 }
111
112 /*
113  * Return a pointer to iov/offset of location in iovec list.
114  */
115 struct iovec *
116 cuio_getptr(struct uio *uio, int loc, int *off)
117 {
118         struct iovec *iov = uio->uio_iov;
119         int iol = uio->uio_iovcnt;
120
121         while (loc >= 0) {
122                 /* Normal end of search */
123                 if (loc < iov->iov_len) {
124                         *off = loc;
125                         return (iov);
126                 }
127
128                 loc -= iov->iov_len;
129                 if (iol == 0) {
130                         if (loc == 0) {
131                                 /* Point at the end of valid data */
132                                 *off = iov->iov_len;
133                                 return (iov);
134                         } else
135                                 return (NULL);
136                 } else {
137                         iov++, iol--;
138                 }
139         }
140
141         return (NULL);
142 }
143
144 EXPORT_SYMBOL(cuio_copyback);
145 EXPORT_SYMBOL(cuio_copydata);
146 EXPORT_SYMBOL(cuio_getptr);
147
148
149 static void
150 skb_copy_bits_back(struct sk_buff *skb, int offset, caddr_t cp, int len)
151 {
152         int i;
153         if (offset < skb_headlen(skb)) {
154                 memcpy(skb->data + offset, cp, min_t(int, skb_headlen(skb), len));
155                 len -= skb_headlen(skb);
156                 cp += skb_headlen(skb);
157         }
158         offset -= skb_headlen(skb);
159         for (i = 0; len > 0 && i < skb_shinfo(skb)->nr_frags; i++) {
160                 if (offset < skb_shinfo(skb)->frags[i].size) {
161                         memcpy(page_address(skb_shinfo(skb)->frags[i].page) +
162                                         skb_shinfo(skb)->frags[i].page_offset,
163                                         cp, min_t(int, skb_shinfo(skb)->frags[i].size, len));
164                         len -= skb_shinfo(skb)->frags[i].size;
165                         cp += skb_shinfo(skb)->frags[i].size;
166                 }
167                 offset -= skb_shinfo(skb)->frags[i].size;
168         }
169 }
170
171 void
172 crypto_copyback(int flags, caddr_t buf, int off, int size, caddr_t in)
173 {
174
175         if ((flags & CRYPTO_F_SKBUF) != 0)
176                 skb_copy_bits_back((struct sk_buff *)buf, off, in, size);
177         else if ((flags & CRYPTO_F_IOV) != 0)
178                 cuio_copyback((struct uio *)buf, off, size, in);
179         else
180                 bcopy(in, buf + off, size);
181 }
182
183 void
184 crypto_copydata(int flags, caddr_t buf, int off, int size, caddr_t out)
185 {
186
187         if ((flags & CRYPTO_F_SKBUF) != 0)
188                 skb_copy_bits((struct sk_buff *)buf, off, out, size);
189         else if ((flags & CRYPTO_F_IOV) != 0)
190                 cuio_copydata((struct uio *)buf, off, size, out);
191         else
192                 bcopy(buf + off, out, size);
193 }
194
195 int
196 crypto_apply(int flags, caddr_t buf, int off, int len,
197     int (*f)(void *, void *, u_int), void *arg)
198 {
199 #if 0
200         int error;
201
202         if ((flags & CRYPTO_F_SKBUF) != 0)
203                 error = XXXXXX((struct mbuf *)buf, off, len, f, arg);
204         else if ((flags & CRYPTO_F_IOV) != 0)
205                 error = cuio_apply((struct uio *)buf, off, len, f, arg);
206         else
207                 error = (*f)(arg, buf + off, len);
208         return (error);
209 #else
210         KASSERT(0, ("crypto_apply not implemented!\n"));
211 #endif
212         return 0;
213 }
214
215 EXPORT_SYMBOL(crypto_copyback);
216 EXPORT_SYMBOL(crypto_copydata);
217 EXPORT_SYMBOL(crypto_apply);
218