2 * Copyright (c) 2005-2008 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * Copyright (c) 2010 Konstantin Belousov <kib@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/libkern.h>
37 #include <sys/module.h>
38 #include <sys/malloc.h>
39 #include <sys/rwlock.h>
42 #include <crypto/aesni/aesni.h>
43 #include <cryptodev_if.h>
48 TAILQ_HEAD(aesni_sessions_head, aesni_session) sessions;
52 static int aesni_newsession(device_t, uint32_t *sidp, struct cryptoini *cri);
53 static int aesni_freesession(device_t, uint64_t tid);
54 static void aesni_freesession_locked(struct aesni_softc *sc,
55 struct aesni_session *ses);
57 MALLOC_DEFINE(M_AESNI, "aesni_data", "AESNI Data");
60 aesni_identify(driver_t *drv, device_t parent)
63 /* NB: order 10 is so we get attached after h/w devices */
64 if (device_find_child(parent, "aesni", -1) == NULL &&
65 BUS_ADD_CHILD(parent, 10, "aesni", -1) == 0)
66 panic("aesni: could not attach");
70 aesni_probe(device_t dev)
73 if ((cpu_feature2 & CPUID2_AESNI) == 0) {
74 device_printf(dev, "No AESNI support.\n");
78 if ((cpu_feature & CPUID_SSE2) == 0) {
79 device_printf(dev, "No SSE2 support but AESNI!?!\n");
83 device_set_desc_copy(dev, "AES-CBC,AES-XTS");
88 aesni_attach(device_t dev)
90 struct aesni_softc *sc;
92 sc = device_get_softc(dev);
93 TAILQ_INIT(&sc->sessions);
95 sc->cid = crypto_get_driverid(dev, CRYPTOCAP_F_HARDWARE);
97 device_printf(dev, "Could not get crypto driver id.\n");
101 rw_init(&sc->lock, "aesni_lock");
102 crypto_register(sc->cid, CRYPTO_AES_CBC, 0, 0);
103 crypto_register(sc->cid, CRYPTO_AES_XTS, 0, 0);
108 aesni_detach(device_t dev)
110 struct aesni_softc *sc;
111 struct aesni_session *ses;
113 sc = device_get_softc(dev);
115 TAILQ_FOREACH(ses, &sc->sessions, next) {
117 rw_wunlock(&sc->lock);
119 "Cannot detach, sessions still active.\n");
123 while ((ses = TAILQ_FIRST(&sc->sessions)) != NULL) {
124 TAILQ_REMOVE(&sc->sessions, ses, next);
125 fpu_kern_free_ctx(ses->fpu_ctx);
128 rw_wunlock(&sc->lock);
129 rw_destroy(&sc->lock);
130 crypto_unregister_all(sc->cid);
135 aesni_newsession(device_t dev, uint32_t *sidp, struct cryptoini *cri)
137 struct aesni_softc *sc;
138 struct aesni_session *ses;
139 struct cryptoini *encini;
142 if (sidp == NULL || cri == NULL)
145 sc = device_get_softc(dev);
148 for (; cri != NULL; cri = cri->cri_next) {
149 switch (cri->cri_alg) {
165 * Free sessions goes first, so if first session is used, we need to
168 ses = TAILQ_FIRST(&sc->sessions);
169 if (ses == NULL || ses->used) {
170 ses = malloc(sizeof(*ses), M_AESNI, M_NOWAIT | M_ZERO);
172 rw_wunlock(&sc->lock);
175 ses->fpu_ctx = fpu_kern_alloc_ctx(FPU_KERN_NORMAL |
177 if (ses->fpu_ctx == NULL) {
179 rw_wunlock(&sc->lock);
184 TAILQ_REMOVE(&sc->sessions, ses, next);
187 TAILQ_INSERT_TAIL(&sc->sessions, ses, next);
188 rw_wunlock(&sc->lock);
189 ses->algo = encini->cri_alg;
191 error = aesni_cipher_setup(ses, encini);
194 aesni_freesession_locked(sc, ses);
195 rw_wunlock(&sc->lock);
204 aesni_freesession_locked(struct aesni_softc *sc, struct aesni_session *ses)
206 struct fpu_kern_ctx *ctx;
210 TAILQ_REMOVE(&sc->sessions, ses, next);
212 bzero(ses, sizeof(*ses));
215 TAILQ_INSERT_HEAD(&sc->sessions, ses, next);
219 aesni_freesession(device_t dev, uint64_t tid)
221 struct aesni_softc *sc;
222 struct aesni_session *ses;
225 sc = device_get_softc(dev);
226 sid = ((uint32_t)tid) & 0xffffffff;
228 TAILQ_FOREACH_REVERSE(ses, &sc->sessions, aesni_sessions_head, next) {
233 rw_wunlock(&sc->lock);
236 aesni_freesession_locked(sc, ses);
237 rw_wunlock(&sc->lock);
242 aesni_process(device_t dev, struct cryptop *crp, int hint __unused)
244 struct aesni_softc *sc = device_get_softc(dev);
245 struct aesni_session *ses = NULL;
246 struct cryptodesc *crd, *enccrd;
256 if (crp->crp_callback == NULL || crp->crp_desc == NULL) {
261 for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) {
262 switch (crd->crd_alg) {
265 if (enccrd != NULL) {
275 if (enccrd == NULL || (enccrd->crd_len % AES_BLOCK_LEN) != 0) {
281 TAILQ_FOREACH_REVERSE(ses, &sc->sessions, aesni_sessions_head, next) {
282 if (ses->id == (crp->crp_sid & 0xffffffff))
285 rw_runlock(&sc->lock);
291 error = aesni_cipher_process(ses, enccrd, crp);
296 crp->crp_etype = error;
302 aesni_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp,
309 if (crp->crp_flags & CRYPTO_F_IMBUF)
311 else if (crp->crp_flags & CRYPTO_F_IOV) {
312 uio = (struct uio *)crp->crp_buf;
313 if (uio->uio_iovcnt != 1)
316 addr = (u_char *)iov->iov_base + enccrd->crd_skip;
318 addr = (u_char *)crp->crp_buf;
323 addr = malloc(enccrd->crd_len, M_AESNI, M_NOWAIT);
326 crypto_copydata(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
327 enccrd->crd_len, addr);
333 static device_method_t aesni_methods[] = {
334 DEVMETHOD(device_identify, aesni_identify),
335 DEVMETHOD(device_probe, aesni_probe),
336 DEVMETHOD(device_attach, aesni_attach),
337 DEVMETHOD(device_detach, aesni_detach),
339 DEVMETHOD(cryptodev_newsession, aesni_newsession),
340 DEVMETHOD(cryptodev_freesession, aesni_freesession),
341 DEVMETHOD(cryptodev_process, aesni_process),
346 static driver_t aesni_driver = {
349 sizeof(struct aesni_softc),
351 static devclass_t aesni_devclass;
353 DRIVER_MODULE(aesni, nexus, aesni_driver, aesni_devclass, 0, 0);
354 MODULE_VERSION(aesni, 1);
355 MODULE_DEPEND(aesni, crypto, 1, 1, 1);