2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2017 Chelsio Communications, Inc.
5 * Copyright (c) 2017 Conrad Meyer <cem@FreeBSD.org>
7 * Largely borrowed from ccr(4), Written by: John Baldwin <jhb@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
34 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/module.h>
41 #include <sys/random.h>
42 #include <sys/sglist.h>
43 #include <sys/sysctl.h>
49 #include <dev/pci/pcivar.h>
51 #include <dev/random/randomdev.h>
53 #include <opencrypto/cryptodev.h>
54 #include <opencrypto/xform.h>
56 #include "cryptodev_if.h"
59 #include "ccp_hardware.h"
61 MALLOC_DEFINE(M_CCP, "ccp", "AMD CCP crypto");
64 * Need a global softc available for garbage random_source API, which lacks any
65 * context pointer. It's also handy for debugging.
67 struct ccp_softc *g_ccp_softc;
69 bool g_debug_print = false;
70 SYSCTL_BOOL(_hw_ccp, OID_AUTO, debug, CTLFLAG_RWTUN, &g_debug_print, 0,
71 "Set to enable debugging log messages");
77 { 0x14561022, "AMD CCP-5a" },
78 { 0x14681022, "AMD CCP-5b" },
79 { 0x15df1022, "AMD CCP-5a" },
82 static struct random_source random_ccp = {
83 .rs_ident = "AMD CCP TRNG",
84 .rs_source = RANDOM_PURE_CCP,
85 .rs_read = random_ccp_read,
89 * ccp_populate_sglist() generates a scatter/gather list that covers the entire
90 * crypto operation buffer.
93 ccp_populate_sglist(struct sglist *sg, struct crypto_buffer *cb)
98 switch (cb->cb_type) {
100 error = sglist_append_mbuf(sg, cb->cb_mbuf);
102 case CRYPTO_BUF_SINGLE_MBUF:
103 error = sglist_append_single_mbuf(sg, cb->cb_mbuf);
106 error = sglist_append_uio(sg, cb->cb_uio);
108 case CRYPTO_BUF_CONTIG:
109 error = sglist_append(sg, cb->cb_buf, cb->cb_buf_len);
111 case CRYPTO_BUF_VMPAGE:
112 error = sglist_append_vmpages(sg, cb->cb_vm_page,
113 cb->cb_vm_page_offset, cb->cb_vm_page_len);
122 ccp_probe(device_t dev)
127 id = pci_get_devid(dev);
128 for (ip = ccp_ids; ip < &ccp_ids[nitems(ccp_ids)]; ip++) {
129 if (id == ip->devid) {
130 device_set_desc(dev, ip->desc);
138 ccp_initialize_queues(struct ccp_softc *sc)
140 struct ccp_queue *qp;
143 for (i = 0; i < nitems(sc->queues); i++) {
148 mtx_init(&qp->cq_lock, "ccp queue", NULL, MTX_DEF);
149 /* XXX - arbitrarily chosen sizes */
150 qp->cq_sg_crp = sglist_alloc(32, M_WAITOK);
151 /* Two more SGEs than sg_crp to accommodate ipad. */
152 qp->cq_sg_ulptx = sglist_alloc(34, M_WAITOK);
153 qp->cq_sg_dst = sglist_alloc(2, M_WAITOK);
158 ccp_free_queues(struct ccp_softc *sc)
160 struct ccp_queue *qp;
163 for (i = 0; i < nitems(sc->queues); i++) {
166 mtx_destroy(&qp->cq_lock);
167 sglist_free(qp->cq_sg_crp);
168 sglist_free(qp->cq_sg_ulptx);
169 sglist_free(qp->cq_sg_dst);
174 ccp_attach(device_t dev)
176 struct ccp_softc *sc;
179 sc = device_get_softc(dev);
182 sc->cid = crypto_get_driverid(dev, sizeof(struct ccp_session),
183 CRYPTOCAP_F_HARDWARE);
185 device_printf(dev, "could not get crypto driver id\n");
189 error = ccp_hw_attach(dev);
193 mtx_init(&sc->lock, "ccp", NULL, MTX_DEF);
195 ccp_initialize_queues(sc);
197 if (g_ccp_softc == NULL) {
199 if ((sc->hw_features & VERSION_CAP_TRNG) != 0)
200 random_source_register(&random_ccp);
207 ccp_detach(device_t dev)
209 struct ccp_softc *sc;
211 sc = device_get_softc(dev);
214 sc->detaching = true;
215 mtx_unlock(&sc->lock);
217 crypto_unregister_all(sc->cid);
218 if (g_ccp_softc == sc && (sc->hw_features & VERSION_CAP_TRNG) != 0)
219 random_source_deregister(&random_ccp);
224 if (g_ccp_softc == sc)
227 mtx_destroy(&sc->lock);
232 ccp_init_hmac_digest(struct ccp_session *s, const char *key, int klen)
234 union authctx auth_ctx;
235 const struct auth_hash *axf;
239 * If the key is larger than the block size, use the digest of
240 * the key as the key instead.
242 axf = s->hmac.auth_hash;
243 if (klen > axf->blocksize) {
244 axf->Init(&auth_ctx);
245 axf->Update(&auth_ctx, key, klen);
246 axf->Final(s->hmac.ipad, &auth_ctx);
247 explicit_bzero(&auth_ctx, sizeof(auth_ctx));
248 klen = axf->hashsize;
250 memcpy(s->hmac.ipad, key, klen);
252 memset(s->hmac.ipad + klen, 0, axf->blocksize - klen);
253 memcpy(s->hmac.opad, s->hmac.ipad, axf->blocksize);
255 for (i = 0; i < axf->blocksize; i++) {
256 s->hmac.ipad[i] ^= HMAC_IPAD_VAL;
257 s->hmac.opad[i] ^= HMAC_OPAD_VAL;
262 ccp_aes_check_keylen(int alg, int klen)
268 if (alg == CRYPTO_AES_XTS)
274 if (alg != CRYPTO_AES_XTS)
284 ccp_aes_setkey(struct ccp_session *s, int alg, const void *key, int klen)
288 if (alg == CRYPTO_AES_XTS)
289 kbits = (klen / 2) * 8;
295 s->blkcipher.cipher_type = CCP_AES_TYPE_128;
298 s->blkcipher.cipher_type = CCP_AES_TYPE_192;
301 s->blkcipher.cipher_type = CCP_AES_TYPE_256;
304 panic("should not get here");
307 s->blkcipher.key_len = klen;
308 memcpy(s->blkcipher.enckey, key, s->blkcipher.key_len);
312 ccp_auth_supported(struct ccp_softc *sc,
313 const struct crypto_session_params *csp)
316 if ((sc->hw_features & VERSION_CAP_SHA) == 0)
318 switch (csp->csp_auth_alg) {
319 case CRYPTO_SHA1_HMAC:
320 case CRYPTO_SHA2_256_HMAC:
321 case CRYPTO_SHA2_384_HMAC:
322 case CRYPTO_SHA2_512_HMAC:
323 if (csp->csp_auth_key == NULL)
333 ccp_cipher_supported(struct ccp_softc *sc,
334 const struct crypto_session_params *csp)
337 if ((sc->hw_features & VERSION_CAP_AES) == 0)
339 switch (csp->csp_cipher_alg) {
341 if (csp->csp_ivlen != AES_BLOCK_LEN)
345 if (csp->csp_ivlen != AES_BLOCK_LEN)
349 if (csp->csp_ivlen != AES_XTS_IV_LEN)
355 return (ccp_aes_check_keylen(csp->csp_cipher_alg,
356 csp->csp_cipher_klen));
360 ccp_probesession(device_t dev, const struct crypto_session_params *csp)
362 struct ccp_softc *sc;
364 if (csp->csp_flags != 0)
366 sc = device_get_softc(dev);
367 switch (csp->csp_mode) {
368 case CSP_MODE_DIGEST:
369 if (!ccp_auth_supported(sc, csp))
372 case CSP_MODE_CIPHER:
373 if (!ccp_cipher_supported(sc, csp))
377 switch (csp->csp_cipher_alg) {
378 case CRYPTO_AES_NIST_GCM_16:
379 if ((sc->hw_features & VERSION_CAP_AES) == 0)
387 if (!ccp_auth_supported(sc, csp) ||
388 !ccp_cipher_supported(sc, csp))
395 return (CRYPTODEV_PROBE_HARDWARE);
399 ccp_newsession(device_t dev, crypto_session_t cses,
400 const struct crypto_session_params *csp)
402 struct ccp_softc *sc;
403 struct ccp_session *s;
404 const struct auth_hash *auth_hash;
405 enum ccp_aes_mode cipher_mode;
409 /* XXX reconcile auth_mode with use by ccp_sha */
410 switch (csp->csp_auth_alg) {
411 case CRYPTO_SHA1_HMAC:
412 auth_hash = &auth_hash_hmac_sha1;
415 case CRYPTO_SHA2_256_HMAC:
416 auth_hash = &auth_hash_hmac_sha2_256;
417 auth_mode = SHA2_256;
419 case CRYPTO_SHA2_384_HMAC:
420 auth_hash = &auth_hash_hmac_sha2_384;
421 auth_mode = SHA2_384;
423 case CRYPTO_SHA2_512_HMAC:
424 auth_hash = &auth_hash_hmac_sha2_512;
425 auth_mode = SHA2_512;
433 switch (csp->csp_cipher_alg) {
435 cipher_mode = CCP_AES_MODE_CBC;
438 cipher_mode = CCP_AES_MODE_CTR;
440 case CRYPTO_AES_NIST_GCM_16:
441 cipher_mode = CCP_AES_MODE_GCTR;
444 cipher_mode = CCP_AES_MODE_XTS;
447 cipher_mode = CCP_AES_MODE_ECB;
451 sc = device_get_softc(dev);
454 mtx_unlock(&sc->lock);
458 s = crypto_get_driver_session(cses);
460 /* Just grab the first usable queue for now. */
461 for (q = 0; q < nitems(sc->queues); q++)
462 if ((sc->valid_queues & (1 << q)) != 0)
464 if (q == nitems(sc->queues)) {
465 mtx_unlock(&sc->lock);
470 switch (csp->csp_mode) {
477 case CSP_MODE_DIGEST:
480 case CSP_MODE_CIPHER:
485 if (s->mode == GCM) {
486 if (csp->csp_auth_mlen == 0)
487 s->gmac.hash_len = AES_GMAC_HASH_LEN;
489 s->gmac.hash_len = csp->csp_auth_mlen;
490 } else if (auth_hash != NULL) {
491 s->hmac.auth_hash = auth_hash;
492 s->hmac.auth_mode = auth_mode;
493 if (csp->csp_auth_mlen == 0)
494 s->hmac.hash_len = auth_hash->hashsize;
496 s->hmac.hash_len = csp->csp_auth_mlen;
497 ccp_init_hmac_digest(s, csp->csp_auth_key, csp->csp_auth_klen);
499 if (cipher_mode != CCP_AES_MODE_ECB) {
500 s->blkcipher.cipher_mode = cipher_mode;
501 if (csp->csp_cipher_key != NULL)
502 ccp_aes_setkey(s, csp->csp_cipher_alg,
503 csp->csp_cipher_key, csp->csp_cipher_klen);
507 mtx_unlock(&sc->lock);
513 ccp_freesession(device_t dev, crypto_session_t cses)
515 struct ccp_session *s;
517 s = crypto_get_driver_session(cses);
521 "session %p freed with %d pending requests\n", s,
527 ccp_process(device_t dev, struct cryptop *crp, int hint)
529 const struct crypto_session_params *csp;
530 struct ccp_softc *sc;
531 struct ccp_queue *qp;
532 struct ccp_session *s;
539 csp = crypto_get_params(crp->crp_session);
540 s = crypto_get_driver_session(crp->crp_session);
541 sc = device_get_softc(dev);
543 qp = &sc->queues[s->queue];
544 mtx_unlock(&sc->lock);
545 error = ccp_queue_acquire_reserve(qp, 1 /* placeholder */, M_NOWAIT);
550 error = ccp_populate_sglist(qp->cq_sg_crp, &crp->crp_buf);
554 if (crp->crp_auth_key != NULL) {
555 KASSERT(s->hmac.auth_hash != NULL, ("auth key without HMAC"));
556 ccp_init_hmac_digest(s, crp->crp_auth_key, csp->csp_auth_klen);
558 if (crp->crp_cipher_key != NULL)
559 ccp_aes_setkey(s, csp->csp_cipher_alg, crp->crp_cipher_key,
560 csp->csp_cipher_klen);
564 if (s->pending != 0) {
568 error = ccp_hmac(qp, s, crp);
571 if (s->pending != 0) {
575 error = ccp_blkcipher(qp, s, crp);
578 if (s->pending != 0) {
582 error = ccp_authenc(qp, s, crp);
585 if (s->pending != 0) {
589 error = ccp_gcm(qp, s, crp);
600 * Squash EAGAIN so callers don't uselessly and
601 * expensively retry if the ring was full.
607 ccp_queue_release(qp);
611 DPRINTF(dev, "%s: early error:%d\n", __func__, error);
612 crp->crp_etype = error;
618 static device_method_t ccp_methods[] = {
619 DEVMETHOD(device_probe, ccp_probe),
620 DEVMETHOD(device_attach, ccp_attach),
621 DEVMETHOD(device_detach, ccp_detach),
623 DEVMETHOD(cryptodev_probesession, ccp_probesession),
624 DEVMETHOD(cryptodev_newsession, ccp_newsession),
625 DEVMETHOD(cryptodev_freesession, ccp_freesession),
626 DEVMETHOD(cryptodev_process, ccp_process),
631 static driver_t ccp_driver = {
634 sizeof(struct ccp_softc)
637 DRIVER_MODULE(ccp, pci, ccp_driver, NULL, NULL);
638 MODULE_VERSION(ccp, 1);
639 MODULE_DEPEND(ccp, crypto, 1, 1, 1);
640 MODULE_DEPEND(ccp, random_device, 1, 1, 1);
641 #if 0 /* There are enough known issues that we shouldn't load automatically */
642 MODULE_PNP_INFO("W32:vendor/device", pci, ccp, ccp_ids,
647 ccp_queue_reserve_space(struct ccp_queue *qp, unsigned n, int mflags)
649 struct ccp_softc *sc;
651 mtx_assert(&qp->cq_lock, MA_OWNED);
654 if (n < 1 || n >= (1 << sc->ring_size_order))
658 if (ccp_queue_get_ring_space(qp) >= n)
660 if ((mflags & M_WAITOK) == 0)
662 qp->cq_waiting = true;
663 msleep(&qp->cq_tail, &qp->cq_lock, 0, "ccpqfull", 0);
668 ccp_queue_acquire_reserve(struct ccp_queue *qp, unsigned n, int mflags)
672 mtx_lock(&qp->cq_lock);
673 qp->cq_acq_tail = qp->cq_tail;
674 error = ccp_queue_reserve_space(qp, n, mflags);
676 mtx_unlock(&qp->cq_lock);
681 ccp_queue_release(struct ccp_queue *qp)
684 mtx_assert(&qp->cq_lock, MA_OWNED);
685 if (qp->cq_tail != qp->cq_acq_tail) {
687 ccp_queue_write_tail(qp);
689 mtx_unlock(&qp->cq_lock);
693 ccp_queue_abort(struct ccp_queue *qp)
697 mtx_assert(&qp->cq_lock, MA_OWNED);
699 /* Wipe out any descriptors associated with this aborted txn. */
700 for (i = qp->cq_acq_tail; i != qp->cq_tail;
701 i = (i + 1) % (1 << qp->cq_softc->ring_size_order)) {
702 memset(&qp->desc_ring[i], 0, sizeof(qp->desc_ring[i]));
704 qp->cq_tail = qp->cq_acq_tail;
706 mtx_unlock(&qp->cq_lock);
710 #define _db_show_lock(lo) LOCK_CLASS(lo)->lc_ddb_show(lo)
711 #define db_show_lock(lk) _db_show_lock(&(lk)->lock_object)
713 db_show_ccp_sc(struct ccp_softc *sc)
716 db_printf("ccp softc at %p\n", sc);
717 db_printf(" cid: %d\n", (int)sc->cid);
719 db_printf(" lock: ");
720 db_show_lock(&sc->lock);
722 db_printf(" detaching: %d\n", (int)sc->detaching);
723 db_printf(" ring_size_order: %u\n", sc->ring_size_order);
725 db_printf(" hw_version: %d\n", (int)sc->hw_version);
726 db_printf(" hw_features: %b\n", (int)sc->hw_features,
727 "\20\24ELFC\23TRNG\22Zip_Compress\16Zip_Decompress\13ECC\12RSA"
728 "\11SHA\0103DES\07AES");
730 db_printf(" hw status:\n");
735 db_show_ccp_qp(struct ccp_queue *qp)
738 db_printf(" lock: ");
739 db_show_lock(&qp->cq_lock);
741 db_printf(" cq_qindex: %u\n", qp->cq_qindex);
742 db_printf(" cq_softc: %p\n", qp->cq_softc);
744 db_printf(" head: %u\n", qp->cq_head);
745 db_printf(" tail: %u\n", qp->cq_tail);
746 db_printf(" acq_tail: %u\n", qp->cq_acq_tail);
747 db_printf(" desc_ring: %p\n", qp->desc_ring);
748 db_printf(" completions_ring: %p\n", qp->completions_ring);
749 db_printf(" descriptors (phys): 0x%jx\n",
750 (uintmax_t)qp->desc_ring_bus_addr);
752 db_printf(" hw status:\n");
753 db_ccp_show_queue_hw(qp);
756 DB_SHOW_COMMAND(ccp, db_show_ccp)
758 struct ccp_softc *sc;
759 unsigned unit, qindex;
764 unit = (unsigned)addr;
766 sc = devclass_get_softc(devclass_find("ccp"), unit);
768 db_printf("No such device ccp%u\n", unit);
777 qindex = (unsigned)count;
778 if (qindex >= nitems(sc->queues)) {
779 db_printf("No such queue %u\n", qindex);
782 db_show_ccp_qp(&sc->queues[qindex]);
786 db_printf("usage: show ccp <unit>[,<qindex>]\n");