xref: /freebsd/sys/crypto/ccp/ccp.c (revision dba7640e44c5ec148a84b0d58c6c9a3c9e5147f3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2017 Chelsio Communications, Inc.
5  * Copyright (c) 2017 Conrad Meyer <cem@FreeBSD.org>
6  * All rights reserved.
7  * Largely borrowed from ccr(4), Written by: John Baldwin <jhb@FreeBSD.org>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include "opt_ddb.h"
35 
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #include <sys/lock.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/module.h>
43 #include <sys/random.h>
44 #include <sys/sglist.h>
45 #include <sys/sysctl.h>
46 
47 #ifdef DDB
48 #include <ddb/ddb.h>
49 #endif
50 
51 #include <dev/pci/pcivar.h>
52 
53 #include <dev/random/randomdev.h>
54 
55 #include <opencrypto/cryptodev.h>
56 #include <opencrypto/xform.h>
57 
58 #include "cryptodev_if.h"
59 
60 #include "ccp.h"
61 #include "ccp_hardware.h"
62 
63 MALLOC_DEFINE(M_CCP, "ccp", "AMD CCP crypto");
64 
65 /*
66  * Need a global softc available for garbage random_source API, which lacks any
67  * context pointer.  It's also handy for debugging.
68  */
69 struct ccp_softc *g_ccp_softc;
70 
71 bool g_debug_print = false;
72 SYSCTL_BOOL(_hw_ccp, OID_AUTO, debug, CTLFLAG_RWTUN, &g_debug_print, 0,
73     "Set to enable debugging log messages");
74 
75 static struct pciid {
76 	uint32_t devid;
77 	const char *desc;
78 } ccp_ids[] = {
79 	{ 0x14561022, "AMD CCP-5a" },
80 	{ 0x14681022, "AMD CCP-5b" },
81 	{ 0x14861022, "AMD CCP-5a" },
82 	{ 0x15df1022, "AMD CCP-5a" },
83 };
84 
85 static struct random_source random_ccp = {
86 	.rs_ident = "AMD CCP TRNG",
87 	.rs_source = RANDOM_PURE_CCP,
88 	.rs_read = random_ccp_read,
89 };
90 
91 /*
92  * ccp_populate_sglist() generates a scatter/gather list that covers the entire
93  * crypto operation buffer.
94  */
95 static int
96 ccp_populate_sglist(struct sglist *sg, struct crypto_buffer *cb)
97 {
98 	int error;
99 
100 	sglist_reset(sg);
101 	switch (cb->cb_type) {
102 	case CRYPTO_BUF_MBUF:
103 		error = sglist_append_mbuf(sg, cb->cb_mbuf);
104 		break;
105 	case CRYPTO_BUF_UIO:
106 		error = sglist_append_uio(sg, cb->cb_uio);
107 		break;
108 	case CRYPTO_BUF_CONTIG:
109 		error = sglist_append(sg, cb->cb_buf, cb->cb_buf_len);
110 		break;
111 	case CRYPTO_BUF_VMPAGE:
112 		error = sglist_append_vmpages(sg, cb->cb_vm_page,
113 		    cb->cb_vm_page_len, cb->cb_vm_page_offset);
114 		break;
115 	default:
116 		error = EINVAL;
117 	}
118 	return (error);
119 }
120 
121 static int
122 ccp_probe(device_t dev)
123 {
124 	struct pciid *ip;
125 	uint32_t id;
126 
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);
131 			return (0);
132 		}
133 	}
134 	return (ENXIO);
135 }
136 
137 static void
138 ccp_initialize_queues(struct ccp_softc *sc)
139 {
140 	struct ccp_queue *qp;
141 	size_t i;
142 
143 	for (i = 0; i < nitems(sc->queues); i++) {
144 		qp = &sc->queues[i];
145 
146 		qp->cq_softc = sc;
147 		qp->cq_qindex = 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);
154 	}
155 }
156 
157 static void
158 ccp_free_queues(struct ccp_softc *sc)
159 {
160 	struct ccp_queue *qp;
161 	size_t i;
162 
163 	for (i = 0; i < nitems(sc->queues); i++) {
164 		qp = &sc->queues[i];
165 
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);
170 	}
171 }
172 
173 static int
174 ccp_attach(device_t dev)
175 {
176 	struct ccp_softc *sc;
177 	int error;
178 
179 	sc = device_get_softc(dev);
180 	sc->dev = dev;
181 
182 	sc->cid = crypto_get_driverid(dev, sizeof(struct ccp_session),
183 	    CRYPTOCAP_F_HARDWARE);
184 	if (sc->cid < 0) {
185 		device_printf(dev, "could not get crypto driver id\n");
186 		return (ENXIO);
187 	}
188 
189 	error = ccp_hw_attach(dev);
190 	if (error != 0)
191 		return (error);
192 
193 	mtx_init(&sc->lock, "ccp", NULL, MTX_DEF);
194 
195 	ccp_initialize_queues(sc);
196 
197 	if (g_ccp_softc == NULL) {
198 		g_ccp_softc = sc;
199 		if ((sc->hw_features & VERSION_CAP_TRNG) != 0)
200 			random_source_register(&random_ccp);
201 	}
202 
203 	return (0);
204 }
205 
206 static int
207 ccp_detach(device_t dev)
208 {
209 	struct ccp_softc *sc;
210 
211 	sc = device_get_softc(dev);
212 
213 	mtx_lock(&sc->lock);
214 	sc->detaching = true;
215 	mtx_unlock(&sc->lock);
216 
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);
220 
221 	ccp_hw_detach(dev);
222 	ccp_free_queues(sc);
223 
224 	if (g_ccp_softc == sc)
225 		g_ccp_softc = NULL;
226 
227 	mtx_destroy(&sc->lock);
228 	return (0);
229 }
230 
231 static void
232 ccp_init_hmac_digest(struct ccp_session *s, const char *key, int klen)
233 {
234 	union authctx auth_ctx;
235 	struct auth_hash *axf;
236 	u_int i;
237 
238 	/*
239 	 * If the key is larger than the block size, use the digest of
240 	 * the key as the key instead.
241 	 */
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;
249 	} else
250 		memcpy(s->hmac.ipad, key, klen);
251 
252 	memset(s->hmac.ipad + klen, 0, axf->blocksize - klen);
253 	memcpy(s->hmac.opad, s->hmac.ipad, axf->blocksize);
254 
255 	for (i = 0; i < axf->blocksize; i++) {
256 		s->hmac.ipad[i] ^= HMAC_IPAD_VAL;
257 		s->hmac.opad[i] ^= HMAC_OPAD_VAL;
258 	}
259 }
260 
261 static bool
262 ccp_aes_check_keylen(int alg, int klen)
263 {
264 
265 	switch (klen * 8) {
266 	case 128:
267 	case 192:
268 		if (alg == CRYPTO_AES_XTS)
269 			return (false);
270 		break;
271 	case 256:
272 		break;
273 	case 512:
274 		if (alg != CRYPTO_AES_XTS)
275 			return (false);
276 		break;
277 	default:
278 		return (false);
279 	}
280 	return (true);
281 }
282 
283 static void
284 ccp_aes_setkey(struct ccp_session *s, int alg, const void *key, int klen)
285 {
286 	unsigned kbits;
287 
288 	if (alg == CRYPTO_AES_XTS)
289 		kbits = (klen / 2) * 8;
290 	else
291 		kbits = klen * 8;
292 
293 	switch (kbits) {
294 	case 128:
295 		s->blkcipher.cipher_type = CCP_AES_TYPE_128;
296 		break;
297 	case 192:
298 		s->blkcipher.cipher_type = CCP_AES_TYPE_192;
299 		break;
300 	case 256:
301 		s->blkcipher.cipher_type = CCP_AES_TYPE_256;
302 		break;
303 	default:
304 		panic("should not get here");
305 	}
306 
307 	s->blkcipher.key_len = klen;
308 	memcpy(s->blkcipher.enckey, key, s->blkcipher.key_len);
309 }
310 
311 static bool
312 ccp_auth_supported(struct ccp_softc *sc,
313     const struct crypto_session_params *csp)
314 {
315 
316 	if ((sc->hw_features & VERSION_CAP_SHA) == 0)
317 		return (false);
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)
324 			return (false);
325 		break;
326 	default:
327 		return (false);
328 	}
329 	return (true);
330 }
331 
332 static bool
333 ccp_cipher_supported(struct ccp_softc *sc,
334     const struct crypto_session_params *csp)
335 {
336 
337 	if ((sc->hw_features & VERSION_CAP_AES) == 0)
338 		return (false);
339 	switch (csp->csp_cipher_alg) {
340 	case CRYPTO_AES_CBC:
341 		if (csp->csp_ivlen != AES_BLOCK_LEN)
342 			return (false);
343 		break;
344 	case CRYPTO_AES_ICM:
345 		if (csp->csp_ivlen != AES_BLOCK_LEN)
346 			return (false);
347 		break;
348 	case CRYPTO_AES_XTS:
349 		if (csp->csp_ivlen != AES_XTS_IV_LEN)
350 			return (false);
351 		break;
352 	default:
353 		return (false);
354 	}
355 	return (ccp_aes_check_keylen(csp->csp_cipher_alg,
356 	    csp->csp_cipher_klen));
357 }
358 
359 static int
360 ccp_probesession(device_t dev, const struct crypto_session_params *csp)
361 {
362 	struct ccp_softc *sc;
363 
364 	if (csp->csp_flags != 0)
365 		return (EINVAL);
366 	sc = device_get_softc(dev);
367 	switch (csp->csp_mode) {
368 	case CSP_MODE_DIGEST:
369 		if (!ccp_auth_supported(sc, csp))
370 			return (EINVAL);
371 		break;
372 	case CSP_MODE_CIPHER:
373 		if (!ccp_cipher_supported(sc, csp))
374 			return (EINVAL);
375 		break;
376 	case CSP_MODE_AEAD:
377 		switch (csp->csp_cipher_alg) {
378 		case CRYPTO_AES_NIST_GCM_16:
379 			if (csp->csp_ivlen != AES_GCM_IV_LEN)
380 				return (EINVAL);
381 			if (csp->csp_auth_mlen < 0 ||
382 			    csp->csp_auth_mlen > AES_GMAC_HASH_LEN)
383 				return (EINVAL);
384 			if ((sc->hw_features & VERSION_CAP_AES) == 0)
385 				return (EINVAL);
386 			break;
387 		default:
388 			return (EINVAL);
389 		}
390 		break;
391 	case CSP_MODE_ETA:
392 		if (!ccp_auth_supported(sc, csp) ||
393 		    !ccp_cipher_supported(sc, csp))
394 			return (EINVAL);
395 		break;
396 	default:
397 		return (EINVAL);
398 	}
399 
400 	return (CRYPTODEV_PROBE_HARDWARE);
401 }
402 
403 static int
404 ccp_newsession(device_t dev, crypto_session_t cses,
405     const struct crypto_session_params *csp)
406 {
407 	struct ccp_softc *sc;
408 	struct ccp_session *s;
409 	struct auth_hash *auth_hash;
410 	enum ccp_aes_mode cipher_mode;
411 	unsigned auth_mode;
412 	unsigned q;
413 
414 	/* XXX reconcile auth_mode with use by ccp_sha */
415 	switch (csp->csp_auth_alg) {
416 	case CRYPTO_SHA1_HMAC:
417 		auth_hash = &auth_hash_hmac_sha1;
418 		auth_mode = SHA1;
419 		break;
420 	case CRYPTO_SHA2_256_HMAC:
421 		auth_hash = &auth_hash_hmac_sha2_256;
422 		auth_mode = SHA2_256;
423 		break;
424 	case CRYPTO_SHA2_384_HMAC:
425 		auth_hash = &auth_hash_hmac_sha2_384;
426 		auth_mode = SHA2_384;
427 		break;
428 	case CRYPTO_SHA2_512_HMAC:
429 		auth_hash = &auth_hash_hmac_sha2_512;
430 		auth_mode = SHA2_512;
431 		break;
432 	default:
433 		auth_hash = NULL;
434 		auth_mode = 0;
435 		break;
436 	}
437 
438 	switch (csp->csp_cipher_alg) {
439 	case CRYPTO_AES_CBC:
440 		cipher_mode = CCP_AES_MODE_CBC;
441 		break;
442 	case CRYPTO_AES_ICM:
443 		cipher_mode = CCP_AES_MODE_CTR;
444 		break;
445 	case CRYPTO_AES_NIST_GCM_16:
446 		cipher_mode = CCP_AES_MODE_GCTR;
447 		break;
448 	case CRYPTO_AES_XTS:
449 		cipher_mode = CCP_AES_MODE_XTS;
450 		break;
451 	default:
452 		cipher_mode = CCP_AES_MODE_ECB;
453 		break;
454 	}
455 
456 	sc = device_get_softc(dev);
457 	mtx_lock(&sc->lock);
458 	if (sc->detaching) {
459 		mtx_unlock(&sc->lock);
460 		return (ENXIO);
461 	}
462 
463 	s = crypto_get_driver_session(cses);
464 
465 	/* Just grab the first usable queue for now. */
466 	for (q = 0; q < nitems(sc->queues); q++)
467 		if ((sc->valid_queues & (1 << q)) != 0)
468 			break;
469 	if (q == nitems(sc->queues)) {
470 		mtx_unlock(&sc->lock);
471 		return (ENXIO);
472 	}
473 	s->queue = q;
474 
475 	switch (csp->csp_mode) {
476 	case CSP_MODE_AEAD:
477 		s->mode = GCM;
478 		break;
479 	case CSP_MODE_ETA:
480 		s->mode = AUTHENC;
481 		break;
482 	case CSP_MODE_DIGEST:
483 		s->mode = HMAC;
484 		break;
485 	case CSP_MODE_CIPHER:
486 		s->mode = BLKCIPHER;
487 		break;
488 	}
489 
490 	if (s->mode == GCM) {
491 		if (csp->csp_auth_mlen == 0)
492 			s->gmac.hash_len = AES_GMAC_HASH_LEN;
493 		else
494 			s->gmac.hash_len = csp->csp_auth_mlen;
495 	} else if (auth_hash != NULL) {
496 		s->hmac.auth_hash = auth_hash;
497 		s->hmac.auth_mode = auth_mode;
498 		if (csp->csp_auth_mlen == 0)
499 			s->hmac.hash_len = auth_hash->hashsize;
500 		else
501 			s->hmac.hash_len = csp->csp_auth_mlen;
502 		ccp_init_hmac_digest(s, csp->csp_auth_key, csp->csp_auth_klen);
503 	}
504 	if (cipher_mode != CCP_AES_MODE_ECB) {
505 		s->blkcipher.cipher_mode = cipher_mode;
506 		if (csp->csp_cipher_key != NULL)
507 			ccp_aes_setkey(s, csp->csp_cipher_alg,
508 			    csp->csp_cipher_key, csp->csp_cipher_klen);
509 	}
510 
511 	s->active = true;
512 	mtx_unlock(&sc->lock);
513 
514 	return (0);
515 }
516 
517 static void
518 ccp_freesession(device_t dev, crypto_session_t cses)
519 {
520 	struct ccp_session *s;
521 
522 	s = crypto_get_driver_session(cses);
523 
524 	if (s->pending != 0)
525 		device_printf(dev,
526 		    "session %p freed with %d pending requests\n", s,
527 		    s->pending);
528 	s->active = false;
529 }
530 
531 static int
532 ccp_process(device_t dev, struct cryptop *crp, int hint)
533 {
534 	const struct crypto_session_params *csp;
535 	struct ccp_softc *sc;
536 	struct ccp_queue *qp;
537 	struct ccp_session *s;
538 	int error;
539 	bool qpheld;
540 
541 	qpheld = false;
542 	qp = NULL;
543 
544 	csp = crypto_get_params(crp->crp_session);
545 	s = crypto_get_driver_session(crp->crp_session);
546 	sc = device_get_softc(dev);
547 	mtx_lock(&sc->lock);
548 	qp = &sc->queues[s->queue];
549 	mtx_unlock(&sc->lock);
550 	error = ccp_queue_acquire_reserve(qp, 1 /* placeholder */, M_NOWAIT);
551 	if (error != 0)
552 		goto out;
553 	qpheld = true;
554 
555 	error = ccp_populate_sglist(qp->cq_sg_crp, &crp->crp_buf);
556 	if (error != 0)
557 		goto out;
558 
559 	if (crp->crp_auth_key != NULL) {
560 		KASSERT(s->hmac.auth_hash != NULL, ("auth key without HMAC"));
561 		ccp_init_hmac_digest(s, crp->crp_auth_key, csp->csp_auth_klen);
562 	}
563 	if (crp->crp_cipher_key != NULL)
564 		ccp_aes_setkey(s, csp->csp_cipher_alg, crp->crp_cipher_key,
565 		    csp->csp_cipher_klen);
566 
567 	switch (s->mode) {
568 	case HMAC:
569 		if (s->pending != 0) {
570 			error = EAGAIN;
571 			break;
572 		}
573 		error = ccp_hmac(qp, s, crp);
574 		break;
575 	case BLKCIPHER:
576 		if (s->pending != 0) {
577 			error = EAGAIN;
578 			break;
579 		}
580 		error = ccp_blkcipher(qp, s, crp);
581 		break;
582 	case AUTHENC:
583 		if (s->pending != 0) {
584 			error = EAGAIN;
585 			break;
586 		}
587 		error = ccp_authenc(qp, s, crp);
588 		break;
589 	case GCM:
590 		if (s->pending != 0) {
591 			error = EAGAIN;
592 			break;
593 		}
594 		error = ccp_gcm(qp, s, crp);
595 		break;
596 	}
597 
598 	if (error == 0)
599 		s->pending++;
600 
601 out:
602 	if (qpheld) {
603 		if (error != 0) {
604 			/*
605 			 * Squash EAGAIN so callers don't uselessly and
606 			 * expensively retry if the ring was full.
607 			 */
608 			if (error == EAGAIN)
609 				error = ENOMEM;
610 			ccp_queue_abort(qp);
611 		} else
612 			ccp_queue_release(qp);
613 	}
614 
615 	if (error != 0) {
616 		DPRINTF(dev, "%s: early error:%d\n", __func__, error);
617 		crp->crp_etype = error;
618 		crypto_done(crp);
619 	}
620 	return (0);
621 }
622 
623 static device_method_t ccp_methods[] = {
624 	DEVMETHOD(device_probe,		ccp_probe),
625 	DEVMETHOD(device_attach,	ccp_attach),
626 	DEVMETHOD(device_detach,	ccp_detach),
627 
628 	DEVMETHOD(cryptodev_probesession, ccp_probesession),
629 	DEVMETHOD(cryptodev_newsession,	ccp_newsession),
630 	DEVMETHOD(cryptodev_freesession, ccp_freesession),
631 	DEVMETHOD(cryptodev_process,	ccp_process),
632 
633 	DEVMETHOD_END
634 };
635 
636 static driver_t ccp_driver = {
637 	"ccp",
638 	ccp_methods,
639 	sizeof(struct ccp_softc)
640 };
641 
642 static devclass_t ccp_devclass;
643 DRIVER_MODULE(ccp, pci, ccp_driver, ccp_devclass, NULL, NULL);
644 MODULE_VERSION(ccp, 1);
645 MODULE_DEPEND(ccp, crypto, 1, 1, 1);
646 MODULE_DEPEND(ccp, random_device, 1, 1, 1);
647 #if 0	/* There are enough known issues that we shouldn't load automatically */
648 MODULE_PNP_INFO("W32:vendor/device", pci, ccp, ccp_ids,
649     nitems(ccp_ids));
650 #endif
651 
652 static int
653 ccp_queue_reserve_space(struct ccp_queue *qp, unsigned n, int mflags)
654 {
655 	struct ccp_softc *sc;
656 
657 	mtx_assert(&qp->cq_lock, MA_OWNED);
658 	sc = qp->cq_softc;
659 
660 	if (n < 1 || n >= (1 << sc->ring_size_order))
661 		return (EINVAL);
662 
663 	while (true) {
664 		if (ccp_queue_get_ring_space(qp) >= n)
665 			return (0);
666 		if ((mflags & M_WAITOK) == 0)
667 			return (EAGAIN);
668 		qp->cq_waiting = true;
669 		msleep(&qp->cq_tail, &qp->cq_lock, 0, "ccpqfull", 0);
670 	}
671 }
672 
673 int
674 ccp_queue_acquire_reserve(struct ccp_queue *qp, unsigned n, int mflags)
675 {
676 	int error;
677 
678 	mtx_lock(&qp->cq_lock);
679 	qp->cq_acq_tail = qp->cq_tail;
680 	error = ccp_queue_reserve_space(qp, n, mflags);
681 	if (error != 0)
682 		mtx_unlock(&qp->cq_lock);
683 	return (error);
684 }
685 
686 void
687 ccp_queue_release(struct ccp_queue *qp)
688 {
689 
690 	mtx_assert(&qp->cq_lock, MA_OWNED);
691 	if (qp->cq_tail != qp->cq_acq_tail) {
692 		wmb();
693 		ccp_queue_write_tail(qp);
694 	}
695 	mtx_unlock(&qp->cq_lock);
696 }
697 
698 void
699 ccp_queue_abort(struct ccp_queue *qp)
700 {
701 	unsigned i;
702 
703 	mtx_assert(&qp->cq_lock, MA_OWNED);
704 
705 	/* Wipe out any descriptors associated with this aborted txn. */
706 	for (i = qp->cq_acq_tail; i != qp->cq_tail;
707 	    i = (i + 1) % (1 << qp->cq_softc->ring_size_order)) {
708 		memset(&qp->desc_ring[i], 0, sizeof(qp->desc_ring[i]));
709 	}
710 	qp->cq_tail = qp->cq_acq_tail;
711 
712 	mtx_unlock(&qp->cq_lock);
713 }
714 
715 #ifdef DDB
716 #define	_db_show_lock(lo)	LOCK_CLASS(lo)->lc_ddb_show(lo)
717 #define	db_show_lock(lk)	_db_show_lock(&(lk)->lock_object)
718 static void
719 db_show_ccp_sc(struct ccp_softc *sc)
720 {
721 
722 	db_printf("ccp softc at %p\n", sc);
723 	db_printf(" cid: %d\n", (int)sc->cid);
724 
725 	db_printf(" lock: ");
726 	db_show_lock(&sc->lock);
727 
728 	db_printf(" detaching: %d\n", (int)sc->detaching);
729 	db_printf(" ring_size_order: %u\n", sc->ring_size_order);
730 
731 	db_printf(" hw_version: %d\n", (int)sc->hw_version);
732 	db_printf(" hw_features: %b\n", (int)sc->hw_features,
733 	    "\20\24ELFC\23TRNG\22Zip_Compress\16Zip_Decompress\13ECC\12RSA"
734 	    "\11SHA\0103DES\07AES");
735 
736 	db_printf(" hw status:\n");
737 	db_ccp_show_hw(sc);
738 }
739 
740 static void
741 db_show_ccp_qp(struct ccp_queue *qp)
742 {
743 
744 	db_printf(" lock: ");
745 	db_show_lock(&qp->cq_lock);
746 
747 	db_printf(" cq_qindex: %u\n", qp->cq_qindex);
748 	db_printf(" cq_softc: %p\n", qp->cq_softc);
749 
750 	db_printf(" head: %u\n", qp->cq_head);
751 	db_printf(" tail: %u\n", qp->cq_tail);
752 	db_printf(" acq_tail: %u\n", qp->cq_acq_tail);
753 	db_printf(" desc_ring: %p\n", qp->desc_ring);
754 	db_printf(" completions_ring: %p\n", qp->completions_ring);
755 	db_printf(" descriptors (phys): 0x%jx\n",
756 	    (uintmax_t)qp->desc_ring_bus_addr);
757 
758 	db_printf(" hw status:\n");
759 	db_ccp_show_queue_hw(qp);
760 }
761 
762 DB_SHOW_COMMAND(ccp, db_show_ccp)
763 {
764 	struct ccp_softc *sc;
765 	unsigned unit, qindex;
766 
767 	if (!have_addr)
768 		goto usage;
769 
770 	unit = (unsigned)addr;
771 
772 	sc = devclass_get_softc(ccp_devclass, unit);
773 	if (sc == NULL) {
774 		db_printf("No such device ccp%u\n", unit);
775 		goto usage;
776 	}
777 
778 	if (count == -1) {
779 		db_show_ccp_sc(sc);
780 		return;
781 	}
782 
783 	qindex = (unsigned)count;
784 	if (qindex >= nitems(sc->queues)) {
785 		db_printf("No such queue %u\n", qindex);
786 		goto usage;
787 	}
788 	db_show_ccp_qp(&sc->queues[qindex]);
789 	return;
790 
791 usage:
792 	db_printf("usage: show ccp <unit>[,<qindex>]\n");
793 	return;
794 }
795 #endif /* DDB */
796