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
27 * FreeBSD: src/sys/crypto/aesni/aesni.c,v 1.1 2010/07/23 11:00:46 kib Exp
30 #include <sys/cdefs.h>
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/spinlock.h>
40 #include <sys/spinlock2.h>
43 #include <crypto/aesni/aesni.h>
44 #include "cryptodev_if.h"
49 TAILQ_HEAD(aesni_sessions_head, aesni_session) sessions;
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);
58 MALLOC_DEFINE(M_AESNI, "aesni_data", "AESNI Data");
61 aesni_identify(driver_t *drv, device_t parent)
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");
71 aesni_probe(device_t dev)
75 if ((cpu_feature2 & CPUID2_AESNI) == 0) {
76 device_printf(dev, "No AESNI support.\n");
79 strlcpy(capp, "AES-CBC", sizeof(capp));
80 device_set_desc_copy(dev, capp);
85 aesni_attach(device_t dev)
87 struct aesni_softc *sc;
89 sc = device_get_softc(dev);
90 TAILQ_INIT(&sc->sessions);
92 sc->cid = crypto_get_driverid(dev, CRYPTOCAP_F_HARDWARE);
94 device_printf(dev, "Could not get crypto driver id.\n");
99 crypto_register(sc->cid, CRYPTO_AES_CBC, 0, 0);
104 aesni_detach(device_t dev)
106 struct aesni_softc *sc;
107 struct aesni_session *ses;
109 sc = device_get_softc(dev);
110 spin_lock_wr(&sc->lock);
111 TAILQ_FOREACH(ses, &sc->sessions, next) {
113 spin_unlock_wr(&sc->lock);
115 "Cannot detach, sessions still active.\n");
119 while ((ses = TAILQ_FIRST(&sc->sessions)) != NULL) {
120 TAILQ_REMOVE(&sc->sessions, ses, next);
123 spin_unlock_wr(&sc->lock);
124 spin_uninit(&sc->lock);
125 crypto_unregister_all(sc->cid);
130 aesni_newsession(device_t dev, uint32_t *sidp, struct cryptoini *cri)
132 struct aesni_softc *sc;
133 struct aesni_session *ses;
134 struct cryptoini *encini;
137 if (sidp == NULL || cri == NULL)
140 sc = device_get_softc(dev);
143 for (; cri != NULL; cri = cri->cri_next) {
144 switch (cri->cri_alg) {
157 spin_lock_wr(&sc->lock);
159 * Free sessions goes first, so if first session is used, we need to
162 ses = TAILQ_FIRST(&sc->sessions);
163 if (ses == NULL || ses->used) {
164 ses = kmalloc(sizeof(*ses), M_AESNI, M_NOWAIT | M_ZERO);
166 spin_unlock_wr(&sc->lock);
169 KASSERT(((uintptr_t)ses) % 0x10 == 0,
170 ("kmalloc returned unaligned pointer"));
173 TAILQ_REMOVE(&sc->sessions, ses, next);
176 TAILQ_INSERT_TAIL(&sc->sessions, ses, next);
177 spin_unlock_wr(&sc->lock);
179 error = aesni_cipher_setup(ses, encini);
181 spin_lock_wr(&sc->lock);
182 aesni_freesession_locked(sc, ses);
183 spin_unlock_wr(&sc->lock);
192 aesni_freesession_locked(struct aesni_softc *sc, struct aesni_session *ses)
197 TAILQ_REMOVE(&sc->sessions, ses, next);
198 bzero(ses, sizeof(*ses));
200 TAILQ_INSERT_HEAD(&sc->sessions, ses, next);
204 aesni_freesession(device_t dev, uint64_t tid)
206 struct aesni_softc *sc;
207 struct aesni_session *ses;
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) {
218 spin_unlock_wr(&sc->lock);
221 aesni_freesession_locked(sc, ses);
222 spin_unlock_wr(&sc->lock);
227 aesni_process(device_t dev, struct cryptop *crp, int hint __unused)
229 struct aesni_softc *sc = device_get_softc(dev);
230 struct aesni_session *ses = NULL;
231 struct cryptodesc *crd, *enccrd;
241 if (crp->crp_callback == NULL || crp->crp_desc == NULL) {
246 for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) {
247 switch (crd->crd_alg) {
249 if (enccrd != NULL) {
259 if (enccrd == NULL || (enccrd->crd_len % AES_BLOCK_LEN) != 0) {
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))
269 spin_unlock_wr(&sc->lock); /* XXX: was rd lock */
275 error = aesni_cipher_process(ses, enccrd, crp);
280 crp->crp_etype = error;
286 aesni_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp,
293 if (crp->crp_flags & CRYPTO_F_IMBUF)
295 else if (crp->crp_flags & CRYPTO_F_IOV) {
296 uio = (struct uio *)crp->crp_buf;
297 if (uio->uio_iovcnt != 1)
300 addr = (u_char *)iov->iov_base + enccrd->crd_skip;
302 addr = (u_char *)crp->crp_buf;
307 addr = kmalloc(enccrd->crd_len, M_AESNI, M_NOWAIT);
310 crypto_copydata(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
311 enccrd->crd_len, addr);
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),
323 DEVMETHOD(cryptodev_newsession, aesni_newsession),
324 DEVMETHOD(cryptodev_freesession, aesni_freesession),
325 DEVMETHOD(cryptodev_process, aesni_process),
330 static driver_t aesni_driver = {
333 sizeof(struct aesni_softc),
335 static devclass_t aesni_devclass;
337 DRIVER_MODULE(aesni, nexus, aesni_driver, aesni_devclass, 0, 0);
338 MODULE_VERSION(aesni, 1);
339 MODULE_DEPEND(aesni, crypto, 1, 1, 1);