aesni - Port to DragonFly
[dragonfly.git] / sys / crypto / aesni / aesni.c
1 /*-
2  * Copyright (c) 2005-2008 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * Copyright (c) 2010 Konstantin Belousov <kib@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * FreeBSD: src/sys/crypto/aesni/aesni.c,v 1.1 2010/07/23 11:00:46 kib Exp
28  */
29
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/kobj.h>
35 #include <sys/libkern.h>
36 #include <sys/lock.h>
37 #include <sys/module.h>
38 #include <sys/malloc.h>
39 #include <sys/spinlock.h>
40 #include <sys/spinlock2.h>
41 #include <sys/bus.h>
42 #include <sys/uio.h>
43 #include <crypto/aesni/aesni.h>
44 #include "cryptodev_if.h"
45
46 struct aesni_softc {
47         int32_t cid;
48         uint32_t sid;
49         TAILQ_HEAD(aesni_sessions_head, aesni_session) sessions;
50         struct spinlock lock;
51 };
52
53 static int aesni_newsession(device_t, uint32_t *sidp, struct cryptoini *cri);
54 static int aesni_freesession(device_t, uint64_t tid);
55 static void aesni_freesession_locked(struct aesni_softc *sc,
56     struct aesni_session *ses);
57
58 MALLOC_DEFINE(M_AESNI, "aesni_data", "AESNI Data");
59
60 static void
61 aesni_identify(driver_t *drv, device_t parent)
62 {
63
64         /* NB: order 10 is so we get attached after h/w devices */
65         if (device_find_child(parent, "aesni", -1) == NULL &&
66             BUS_ADD_CHILD(parent, parent, 10, "aesni", -1) == 0)
67                 panic("aesni: could not attach");
68 }
69
70 static int
71 aesni_probe(device_t dev)
72 {
73         char capp[32];
74
75         if ((cpu_feature2 & CPUID2_AESNI) == 0) {
76                 device_printf(dev, "No AESNI support.\n");
77                 return (EINVAL);
78         }
79         strlcpy(capp, "AES-CBC", sizeof(capp));
80         device_set_desc_copy(dev, capp);
81         return (0);
82 }
83
84 static int
85 aesni_attach(device_t dev)
86 {
87         struct aesni_softc *sc;
88
89         sc = device_get_softc(dev);
90         TAILQ_INIT(&sc->sessions);
91         sc->sid = 1;
92         sc->cid = crypto_get_driverid(dev, CRYPTOCAP_F_HARDWARE);
93         if (sc->cid < 0) {
94                 device_printf(dev, "Could not get crypto driver id.\n");
95                 return (ENOMEM);
96         }
97
98         spin_init(&sc->lock);
99         crypto_register(sc->cid, CRYPTO_AES_CBC, 0, 0);
100         return (0);
101 }
102
103 static int
104 aesni_detach(device_t dev)
105 {
106         struct aesni_softc *sc;
107         struct aesni_session *ses;
108
109         sc = device_get_softc(dev);
110         spin_lock_wr(&sc->lock);
111         TAILQ_FOREACH(ses, &sc->sessions, next) {
112                 if (ses->used) {
113                         spin_unlock_wr(&sc->lock);
114                         device_printf(dev,
115                             "Cannot detach, sessions still active.\n");
116                         return (EBUSY);
117                 }
118         }
119         while ((ses = TAILQ_FIRST(&sc->sessions)) != NULL) {
120                 TAILQ_REMOVE(&sc->sessions, ses, next);
121                 kfree(ses, M_AESNI);
122         }
123         spin_unlock_wr(&sc->lock);
124         spin_uninit(&sc->lock);
125         crypto_unregister_all(sc->cid);
126         return (0);
127 }
128
129 static int
130 aesni_newsession(device_t dev, uint32_t *sidp, struct cryptoini *cri)
131 {
132         struct aesni_softc *sc;
133         struct aesni_session *ses;
134         struct cryptoini *encini;
135         int error;
136
137         if (sidp == NULL || cri == NULL)
138                 return (EINVAL);
139
140         sc = device_get_softc(dev);
141         ses = NULL;
142         encini = NULL;
143         for (; cri != NULL; cri = cri->cri_next) {
144                 switch (cri->cri_alg) {
145                 case CRYPTO_AES_CBC:
146                         if (encini != NULL)
147                                 return (EINVAL);
148                         encini = cri;
149                         break;
150                 default:
151                         return (EINVAL);
152                 }
153         }
154         if (encini == NULL)
155                 return (EINVAL);
156
157         spin_lock_wr(&sc->lock);
158         /*
159          * Free sessions goes first, so if first session is used, we need to
160          * allocate one.
161          */
162         ses = TAILQ_FIRST(&sc->sessions);
163         if (ses == NULL || ses->used) {
164                 ses = kmalloc(sizeof(*ses), M_AESNI, M_NOWAIT | M_ZERO);
165                 if (ses == NULL) {
166                         spin_unlock_wr(&sc->lock);
167                         return (ENOMEM);
168                 }
169                 KASSERT(((uintptr_t)ses) % 0x10 == 0,
170                     ("kmalloc returned unaligned pointer"));
171                 ses->id = sc->sid++;
172         } else {
173                 TAILQ_REMOVE(&sc->sessions, ses, next);
174         }
175         ses->used = 1;
176         TAILQ_INSERT_TAIL(&sc->sessions, ses, next);
177         spin_unlock_wr(&sc->lock);
178
179         error = aesni_cipher_setup(ses, encini);
180         if (error != 0) {
181                 spin_lock_wr(&sc->lock);
182                 aesni_freesession_locked(sc, ses);
183                 spin_unlock_wr(&sc->lock);
184                 return (error);
185         }
186
187         *sidp = ses->id;
188         return (0);
189 }
190
191 static void
192 aesni_freesession_locked(struct aesni_softc *sc, struct aesni_session *ses)
193 {
194         uint32_t sid;
195
196         sid = ses->id;
197         TAILQ_REMOVE(&sc->sessions, ses, next);
198         bzero(ses, sizeof(*ses));
199         ses->id = sid;
200         TAILQ_INSERT_HEAD(&sc->sessions, ses, next);
201 }
202
203 static int
204 aesni_freesession(device_t dev, uint64_t tid)
205 {
206         struct aesni_softc *sc;
207         struct aesni_session *ses;
208         uint32_t sid;
209
210         sc = device_get_softc(dev);
211         sid = ((uint32_t)tid) & 0xffffffff;
212         spin_lock_wr(&sc->lock);
213         TAILQ_FOREACH_REVERSE(ses, &sc->sessions, aesni_sessions_head, next) {
214                 if (ses->id == sid)
215                         break;
216         }
217         if (ses == NULL) {
218                 spin_unlock_wr(&sc->lock);
219                 return (EINVAL);
220         }
221         aesni_freesession_locked(sc, ses);
222         spin_unlock_wr(&sc->lock);
223         return (0);
224 }
225
226 static int
227 aesni_process(device_t dev, struct cryptop *crp, int hint __unused)
228 {
229         struct aesni_softc *sc = device_get_softc(dev);
230         struct aesni_session *ses = NULL;
231         struct cryptodesc *crd, *enccrd;
232         int error;
233
234         error = 0;
235         enccrd = NULL;
236
237         /* Sanity check. */
238         if (crp == NULL)
239                 return (EINVAL);
240
241         if (crp->crp_callback == NULL || crp->crp_desc == NULL) {
242                 error = EINVAL;
243                 goto out;
244         }
245
246         for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) {
247                 switch (crd->crd_alg) {
248                 case CRYPTO_AES_CBC:
249                         if (enccrd != NULL) {
250                                 error = EINVAL;
251                                 goto out;
252                         }
253                         enccrd = crd;
254                         break;
255                 default:
256                         return (EINVAL);
257                 }
258         }
259         if (enccrd == NULL || (enccrd->crd_len % AES_BLOCK_LEN) != 0) {
260                 error = EINVAL;
261                 goto out;
262         }
263
264         spin_lock_wr(&sc->lock); /* XXX: was rd lock */
265         TAILQ_FOREACH_REVERSE(ses, &sc->sessions, aesni_sessions_head, next) {
266                 if (ses->id == (crp->crp_sid & 0xffffffff))
267                         break;
268         }
269         spin_unlock_wr(&sc->lock); /* XXX: was rd lock */
270         if (ses == NULL) {
271                 error = EINVAL;
272                 goto out;
273         }
274
275         error = aesni_cipher_process(ses, enccrd, crp);
276         if (error != 0)
277                 goto out;
278
279 out:
280         crp->crp_etype = error;
281         crypto_done(crp);
282         return (error);
283 }
284
285 uint8_t *
286 aesni_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp,
287     int *allocated)
288 {
289         struct uio *uio;
290         struct iovec *iov;
291         uint8_t *addr;
292
293         if (crp->crp_flags & CRYPTO_F_IMBUF)
294                 goto alloc;
295         else if (crp->crp_flags & CRYPTO_F_IOV) {
296                 uio = (struct uio *)crp->crp_buf;
297                 if (uio->uio_iovcnt != 1)
298                         goto alloc;
299                 iov = uio->uio_iov;
300                 addr = (u_char *)iov->iov_base + enccrd->crd_skip;
301         } else
302                 addr = (u_char *)crp->crp_buf;
303         *allocated = 0;
304         return (addr);
305
306 alloc:
307         addr = kmalloc(enccrd->crd_len, M_AESNI, M_NOWAIT);
308         if (addr != NULL) {
309                 *allocated = 1;
310                 crypto_copydata(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
311                     enccrd->crd_len, addr);
312         } else
313                 *allocated = 0;
314         return (addr);
315 }
316
317 static device_method_t aesni_methods[] = {
318         DEVMETHOD(device_identify, aesni_identify),
319         DEVMETHOD(device_probe, aesni_probe),
320         DEVMETHOD(device_attach, aesni_attach),
321         DEVMETHOD(device_detach, aesni_detach),
322
323         DEVMETHOD(cryptodev_newsession, aesni_newsession),
324         DEVMETHOD(cryptodev_freesession, aesni_freesession),
325         DEVMETHOD(cryptodev_process, aesni_process),
326
327         {0, 0},
328 };
329
330 static driver_t aesni_driver = {
331         "aesni",
332         aesni_methods,
333         sizeof(struct aesni_softc),
334 };
335 static devclass_t aesni_devclass;
336
337 DRIVER_MODULE(aesni, nexus, aesni_driver, aesni_devclass, 0, 0);
338 MODULE_VERSION(aesni, 1);
339 MODULE_DEPEND(aesni, crypto, 1, 1, 1);