ocf-linux: version bump to 20110720
[openwrt/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,38) && !defined(AUTOCONF_INCLUDED)
39 #include <linux/config.h>
40 #endif
41 #include <linux/module.h>
42 #include <linux/init.h>
43 #include <linux/slab.h>
44 #include <linux/uio.h>
45 #include <linux/skbuff.h>
46 #include <linux/kernel.h>
47 #include <linux/mm.h>
48 #include <asm/io.h>
49
50 #include <uio.h>
51 #include <cryptodev.h>
52
53 /*
54 * This macro is only for avoiding code duplication, as we need to skip
55 * given number of bytes in the same way in three functions below.
56 */
57 #define CUIO_SKIP() do { \
58 KASSERT(off >= 0, ("%s: off %d < 0", __func__, off)); \
59 KASSERT(len >= 0, ("%s: len %d < 0", __func__, len)); \
60 while (off > 0) { \
61 KASSERT(iol >= 0, ("%s: empty in skip", __func__)); \
62 if (off < iov->iov_len) \
63 break; \
64 off -= iov->iov_len; \
65 iol--; \
66 iov++; \
67 } \
68 } while (0)
69
70 void
71 cuio_copydata(struct uio* uio, int off, int len, caddr_t cp)
72 {
73 struct iovec *iov = uio->uio_iov;
74 int iol = uio->uio_iovcnt;
75 unsigned count;
76
77 CUIO_SKIP();
78 while (len > 0) {
79 KASSERT(iol >= 0, ("%s: empty", __func__));
80 count = min((int)(iov->iov_len - off), len);
81 memcpy(cp, ((caddr_t)iov->iov_base) + off, count);
82 len -= count;
83 cp += count;
84 off = 0;
85 iol--;
86 iov++;
87 }
88 }
89
90 void
91 cuio_copyback(struct uio* uio, int off, int len, caddr_t cp)
92 {
93 struct iovec *iov = uio->uio_iov;
94 int iol = uio->uio_iovcnt;
95 unsigned count;
96
97 CUIO_SKIP();
98 while (len > 0) {
99 KASSERT(iol >= 0, ("%s: empty", __func__));
100 count = min((int)(iov->iov_len - off), len);
101 memcpy(((caddr_t)iov->iov_base) + off, cp, count);
102 len -= count;
103 cp += count;
104 off = 0;
105 iol--;
106 iov++;
107 }
108 }
109
110 /*
111 * Return a pointer to iov/offset of location in iovec list.
112 */
113 struct iovec *
114 cuio_getptr(struct uio *uio, int loc, int *off)
115 {
116 struct iovec *iov = uio->uio_iov;
117 int iol = uio->uio_iovcnt;
118
119 while (loc >= 0) {
120 /* Normal end of search */
121 if (loc < iov->iov_len) {
122 *off = loc;
123 return (iov);
124 }
125
126 loc -= iov->iov_len;
127 if (iol == 0) {
128 if (loc == 0) {
129 /* Point at the end of valid data */
130 *off = iov->iov_len;
131 return (iov);
132 } else
133 return (NULL);
134 } else {
135 iov++, iol--;
136 }
137 }
138
139 return (NULL);
140 }
141
142 EXPORT_SYMBOL(cuio_copyback);
143 EXPORT_SYMBOL(cuio_copydata);
144 EXPORT_SYMBOL(cuio_getptr);
145
146
147 static void
148 skb_copy_bits_back(struct sk_buff *skb, int offset, caddr_t cp, int len)
149 {
150 int i;
151 if (offset < skb_headlen(skb)) {
152 memcpy(skb->data + offset, cp, min_t(int, skb_headlen(skb), len));
153 len -= skb_headlen(skb);
154 cp += skb_headlen(skb);
155 }
156 offset -= skb_headlen(skb);
157 for (i = 0; len > 0 && i < skb_shinfo(skb)->nr_frags; i++) {
158 if (offset < skb_shinfo(skb)->frags[i].size) {
159 memcpy(page_address(skb_shinfo(skb)->frags[i].page) +
160 skb_shinfo(skb)->frags[i].page_offset,
161 cp, min_t(int, skb_shinfo(skb)->frags[i].size, len));
162 len -= skb_shinfo(skb)->frags[i].size;
163 cp += skb_shinfo(skb)->frags[i].size;
164 }
165 offset -= skb_shinfo(skb)->frags[i].size;
166 }
167 }
168
169 void
170 crypto_copyback(int flags, caddr_t buf, int off, int size, caddr_t in)
171 {
172
173 if ((flags & CRYPTO_F_SKBUF) != 0)
174 skb_copy_bits_back((struct sk_buff *)buf, off, in, size);
175 else if ((flags & CRYPTO_F_IOV) != 0)
176 cuio_copyback((struct uio *)buf, off, size, in);
177 else
178 bcopy(in, buf + off, size);
179 }
180
181 void
182 crypto_copydata(int flags, caddr_t buf, int off, int size, caddr_t out)
183 {
184
185 if ((flags & CRYPTO_F_SKBUF) != 0)
186 skb_copy_bits((struct sk_buff *)buf, off, out, size);
187 else if ((flags & CRYPTO_F_IOV) != 0)
188 cuio_copydata((struct uio *)buf, off, size, out);
189 else
190 bcopy(buf + off, out, size);
191 }
192
193 int
194 crypto_apply(int flags, caddr_t buf, int off, int len,
195 int (*f)(void *, void *, u_int), void *arg)
196 {
197 #if 0
198 int error;
199
200 if ((flags & CRYPTO_F_SKBUF) != 0)
201 error = XXXXXX((struct mbuf *)buf, off, len, f, arg);
202 else if ((flags & CRYPTO_F_IOV) != 0)
203 error = cuio_apply((struct uio *)buf, off, len, f, arg);
204 else
205 error = (*f)(arg, buf + off, len);
206 return (error);
207 #else
208 KASSERT(0, ("crypto_apply not implemented!\n"));
209 #endif
210 return 0;
211 }
212
213 EXPORT_SYMBOL(crypto_copyback);
214 EXPORT_SYMBOL(crypto_copydata);
215 EXPORT_SYMBOL(crypto_apply);
216