1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2018 Intel Corporation
5 #include <rte_common.h>
6 #include <rte_cycles.h>
8 #include <rte_malloc.h>
9 #include <rte_memzone.h>
11 #include <rte_bus_pci.h>
12 #include <rte_atomic.h>
13 #include <rte_prefetch.h>
16 #include "qat_device.h"
21 #include "adf_transport_access_macros.h"
22 #include "adf_transport_access_macros_gen4vf.h"
23 #include "dev/qat_dev_gens.h"
25 #define QAT_CQ_MAX_DEQ_RETRIES 10
27 #define ADF_MAX_DESC 4096
28 #define ADF_MIN_DESC 128
30 #define ADF_ARB_REG_SLOT 0x1000
31 #define ADF_ARB_RINGSRVARBEN_OFFSET 0x19C
33 #define WRITE_CSR_ARB_RINGSRVARBEN(csr_addr, index, value) \
34 ADF_CSR_WR(csr_addr, ADF_ARB_RINGSRVARBEN_OFFSET + \
35 (ADF_ARB_REG_SLOT * index), value)
38 const struct qat_qp_hw_data qat_gen1_qps[QAT_MAX_SERVICES]
39 [ADF_MAX_QPS_ON_ANY_SERVICE] = {
40 /* queue pairs which provide an asymmetric crypto service */
41 [QAT_SERVICE_ASYMMETRIC] = {
43 .service_type = QAT_SERVICE_ASYMMETRIC,
51 .service_type = QAT_SERVICE_ASYMMETRIC,
59 /* queue pairs which provide a symmetric crypto service */
60 [QAT_SERVICE_SYMMETRIC] = {
62 .service_type = QAT_SERVICE_SYMMETRIC,
70 .service_type = QAT_SERVICE_SYMMETRIC,
78 /* queue pairs which provide a compression service */
79 [QAT_SERVICE_COMPRESSION] = {
81 .service_type = QAT_SERVICE_COMPRESSION,
88 .service_type = QAT_SERVICE_COMPRESSION,
99 const struct qat_qp_hw_data qat_gen3_qps[QAT_MAX_SERVICES]
100 [ADF_MAX_QPS_ON_ANY_SERVICE] = {
101 /* queue pairs which provide an asymmetric crypto service */
102 [QAT_SERVICE_ASYMMETRIC] = {
104 .service_type = QAT_SERVICE_ASYMMETRIC,
112 /* queue pairs which provide a symmetric crypto service */
113 [QAT_SERVICE_SYMMETRIC] = {
115 .service_type = QAT_SERVICE_SYMMETRIC,
123 /* queue pairs which provide a compression service */
124 [QAT_SERVICE_COMPRESSION] = {
126 .service_type = QAT_SERVICE_COMPRESSION,
136 static int qat_qp_check_queue_alignment(uint64_t phys_addr,
137 uint32_t queue_size_bytes);
138 static void qat_queue_delete(struct qat_queue *queue);
139 static int qat_queue_create(struct qat_pci_device *qat_dev,
140 struct qat_queue *queue, struct qat_qp_config *, uint8_t dir);
141 static int adf_verify_queue_size(uint32_t msg_size, uint32_t msg_num,
142 uint32_t *queue_size_for_csr);
143 static void adf_configure_queues(struct qat_qp *queue,
144 enum qat_device_gen qat_dev_gen);
145 static void adf_queue_arb_enable(enum qat_device_gen qat_dev_gen,
146 struct qat_queue *txq, void *base_addr, rte_spinlock_t *lock);
147 static void adf_queue_arb_disable(enum qat_device_gen qat_dev_gen,
148 struct qat_queue *txq, void *base_addr, rte_spinlock_t *lock);
150 int qat_qps_per_service(struct qat_pci_device *qat_dev,
151 enum qat_service_type service)
153 int i = 0, count = 0, max_ops_per_srv = 0;
155 if (qat_dev->qat_dev_gen == QAT_GEN4) {
156 max_ops_per_srv = QAT_GEN4_BUNDLE_NUM;
157 for (i = 0, count = 0; i < max_ops_per_srv; i++)
158 if (qat_dev->qp_gen4_data[i][0].service_type == service)
161 const struct qat_qp_hw_data *sym_hw_qps =
162 qat_gen_config[qat_dev->qat_dev_gen]
163 .qp_hw_data[service];
165 max_ops_per_srv = ADF_MAX_QPS_ON_ANY_SERVICE;
166 for (i = 0, count = 0; i < max_ops_per_srv; i++)
167 if (sym_hw_qps[i].service_type == service)
174 static const struct rte_memzone *
175 queue_dma_zone_reserve(const char *queue_name, uint32_t queue_size,
178 const struct rte_memzone *mz;
180 mz = rte_memzone_lookup(queue_name);
182 if (((size_t)queue_size <= mz->len) &&
183 ((socket_id == SOCKET_ID_ANY) ||
184 (socket_id == mz->socket_id))) {
185 QAT_LOG(DEBUG, "re-use memzone already "
186 "allocated for %s", queue_name);
190 QAT_LOG(ERR, "Incompatible memzone already "
191 "allocated %s, size %u, socket %d. "
192 "Requested size %u, socket %u",
193 queue_name, (uint32_t)mz->len,
194 mz->socket_id, queue_size, socket_id);
198 QAT_LOG(DEBUG, "Allocate memzone for %s, size %u on socket %u",
199 queue_name, queue_size, socket_id);
200 return rte_memzone_reserve_aligned(queue_name, queue_size,
201 socket_id, RTE_MEMZONE_IOVA_CONTIG, queue_size);
204 int qat_qp_setup(struct qat_pci_device *qat_dev,
205 struct qat_qp **qp_addr,
206 uint16_t queue_pair_id,
207 struct qat_qp_config *qat_qp_conf)
210 struct rte_pci_device *pci_dev =
211 qat_pci_devs[qat_dev->qat_dev_id].pci_dev;
212 char op_cookie_pool_name[RTE_RING_NAMESIZE];
213 enum qat_device_gen qat_dev_gen = qat_dev->qat_dev_gen;
216 QAT_LOG(DEBUG, "Setup qp %u on qat pci device %d gen %d",
217 queue_pair_id, qat_dev->qat_dev_id, qat_dev->qat_dev_gen);
219 if ((qat_qp_conf->nb_descriptors > ADF_MAX_DESC) ||
220 (qat_qp_conf->nb_descriptors < ADF_MIN_DESC)) {
221 QAT_LOG(ERR, "Can't create qp for %u descriptors",
222 qat_qp_conf->nb_descriptors);
226 if (pci_dev->mem_resource[0].addr == NULL) {
227 QAT_LOG(ERR, "Could not find VF config space "
228 "(UIO driver attached?).");
232 /* Allocate the queue pair data structure. */
233 qp = rte_zmalloc_socket("qat PMD qp metadata",
234 sizeof(*qp), RTE_CACHE_LINE_SIZE,
235 qat_qp_conf->socket_id);
237 QAT_LOG(ERR, "Failed to alloc mem for qp struct");
240 qp->nb_descriptors = qat_qp_conf->nb_descriptors;
241 qp->op_cookies = rte_zmalloc_socket("qat PMD op cookie pointer",
242 qat_qp_conf->nb_descriptors * sizeof(*qp->op_cookies),
243 RTE_CACHE_LINE_SIZE, qat_qp_conf->socket_id);
244 if (qp->op_cookies == NULL) {
245 QAT_LOG(ERR, "Failed to alloc mem for cookie");
250 qp->mmap_bar_addr = pci_dev->mem_resource[0].addr;
251 qp->enqueued = qp->dequeued = 0;
253 if (qat_queue_create(qat_dev, &(qp->tx_q), qat_qp_conf,
254 ADF_RING_DIR_TX) != 0) {
255 QAT_LOG(ERR, "Tx queue create failed "
256 "queue_pair_id=%u", queue_pair_id);
260 qp->max_inflights = ADF_MAX_INFLIGHTS(qp->tx_q.queue_size,
261 ADF_BYTES_TO_MSG_SIZE(qp->tx_q.msg_size));
263 if (qp->max_inflights < 2) {
264 QAT_LOG(ERR, "Invalid num inflights");
265 qat_queue_delete(&(qp->tx_q));
269 if (qat_queue_create(qat_dev, &(qp->rx_q), qat_qp_conf,
270 ADF_RING_DIR_RX) != 0) {
271 QAT_LOG(ERR, "Rx queue create failed "
272 "queue_pair_id=%hu", queue_pair_id);
273 qat_queue_delete(&(qp->tx_q));
277 adf_configure_queues(qp, qat_dev_gen);
278 adf_queue_arb_enable(qat_dev_gen, &qp->tx_q, qp->mmap_bar_addr,
279 &qat_dev->arb_csr_lock);
281 snprintf(op_cookie_pool_name, RTE_RING_NAMESIZE,
282 "%s%d_cookies_%s_qp%hu",
283 pci_dev->driver->driver.name, qat_dev->qat_dev_id,
284 qat_qp_conf->service_str, queue_pair_id);
286 QAT_LOG(DEBUG, "cookiepool: %s", op_cookie_pool_name);
287 qp->op_cookie_pool = rte_mempool_lookup(op_cookie_pool_name);
288 if (qp->op_cookie_pool == NULL)
289 qp->op_cookie_pool = rte_mempool_create(op_cookie_pool_name,
291 qat_qp_conf->cookie_size, 64, 0,
292 NULL, NULL, NULL, NULL,
293 pci_dev->device.numa_node,
295 if (!qp->op_cookie_pool) {
296 QAT_LOG(ERR, "QAT PMD Cannot create"
301 for (i = 0; i < qp->nb_descriptors; i++) {
302 if (rte_mempool_get(qp->op_cookie_pool, &qp->op_cookies[i])) {
303 QAT_LOG(ERR, "QAT PMD Cannot get op_cookie");
306 memset(qp->op_cookies[i], 0, qat_qp_conf->cookie_size);
309 qp->qat_dev_gen = qat_dev->qat_dev_gen;
310 qp->service_type = qat_qp_conf->hw->service_type;
311 qp->qat_dev = qat_dev;
313 QAT_LOG(DEBUG, "QP setup complete: id: %d, cookiepool: %s",
314 queue_pair_id, op_cookie_pool_name);
320 if (qp->op_cookie_pool)
321 rte_mempool_free(qp->op_cookie_pool);
322 rte_free(qp->op_cookies);
328 int qat_qp_release(enum qat_device_gen qat_dev_gen, struct qat_qp **qp_addr)
330 struct qat_qp *qp = *qp_addr;
334 QAT_LOG(DEBUG, "qp already freed");
338 QAT_LOG(DEBUG, "Free qp on qat_pci device %d",
339 qp->qat_dev->qat_dev_id);
341 /* Don't free memory if there are still responses to be processed */
342 if ((qp->enqueued - qp->dequeued) == 0) {
343 qat_queue_delete(&(qp->tx_q));
344 qat_queue_delete(&(qp->rx_q));
349 adf_queue_arb_disable(qat_dev_gen, &(qp->tx_q), qp->mmap_bar_addr,
350 &qp->qat_dev->arb_csr_lock);
352 for (i = 0; i < qp->nb_descriptors; i++)
353 rte_mempool_put(qp->op_cookie_pool, qp->op_cookies[i]);
355 if (qp->op_cookie_pool)
356 rte_mempool_free(qp->op_cookie_pool);
358 rte_free(qp->op_cookies);
365 static void qat_queue_delete(struct qat_queue *queue)
367 const struct rte_memzone *mz;
371 QAT_LOG(DEBUG, "Invalid queue");
374 QAT_LOG(DEBUG, "Free ring %d, memzone: %s",
375 queue->hw_queue_number, queue->memz_name);
377 mz = rte_memzone_lookup(queue->memz_name);
379 /* Write an unused pattern to the queue memory. */
380 memset(queue->base_addr, 0x7F, queue->queue_size);
381 status = rte_memzone_free(mz);
383 QAT_LOG(ERR, "Error %d on freeing queue %s",
384 status, queue->memz_name);
386 QAT_LOG(DEBUG, "queue %s doesn't exist",
392 qat_queue_create(struct qat_pci_device *qat_dev, struct qat_queue *queue,
393 struct qat_qp_config *qp_conf, uint8_t dir)
397 const struct rte_memzone *qp_mz;
398 struct rte_pci_device *pci_dev =
399 qat_pci_devs[qat_dev->qat_dev_id].pci_dev;
400 enum qat_device_gen qat_dev_gen = qat_dev->qat_dev_gen;
402 uint16_t desc_size = (dir == ADF_RING_DIR_TX ?
403 qp_conf->hw->tx_msg_size : qp_conf->hw->rx_msg_size);
404 uint32_t queue_size_bytes = (qp_conf->nb_descriptors)*(desc_size);
406 queue->hw_bundle_number = qp_conf->hw->hw_bundle_num;
407 queue->hw_queue_number = (dir == ADF_RING_DIR_TX ?
408 qp_conf->hw->tx_ring_num : qp_conf->hw->rx_ring_num);
410 if (desc_size > ADF_MSG_SIZE_TO_BYTES(ADF_MAX_MSG_SIZE)) {
411 QAT_LOG(ERR, "Invalid descriptor size %d", desc_size);
416 * Allocate a memzone for the queue - create a unique name.
418 snprintf(queue->memz_name, sizeof(queue->memz_name),
420 pci_dev->driver->driver.name, qat_dev->qat_dev_id,
421 qp_conf->service_str, "qp_mem",
422 queue->hw_bundle_number, queue->hw_queue_number);
423 qp_mz = queue_dma_zone_reserve(queue->memz_name, queue_size_bytes,
424 pci_dev->device.numa_node);
426 QAT_LOG(ERR, "Failed to allocate ring memzone");
430 queue->base_addr = (char *)qp_mz->addr;
431 queue->base_phys_addr = qp_mz->iova;
432 if (qat_qp_check_queue_alignment(queue->base_phys_addr,
434 QAT_LOG(ERR, "Invalid alignment on queue create "
436 queue->base_phys_addr);
438 goto queue_create_err;
441 if (adf_verify_queue_size(desc_size, qp_conf->nb_descriptors,
442 &(queue->queue_size)) != 0) {
443 QAT_LOG(ERR, "Invalid num inflights");
445 goto queue_create_err;
448 queue->modulo_mask = (1 << ADF_RING_SIZE_MODULO(queue->queue_size)) - 1;
451 queue->msg_size = desc_size;
453 /* For fast calculation of cookie index, relies on msg_size being 2^n */
454 queue->trailz = __builtin_ctz(desc_size);
457 * Write an unused pattern to the queue memory.
459 memset(queue->base_addr, 0x7F, queue_size_bytes);
460 io_addr = pci_dev->mem_resource[0].addr;
462 if (qat_dev_gen == QAT_GEN4) {
463 queue_base = BUILD_RING_BASE_ADDR_GEN4(queue->base_phys_addr,
465 WRITE_CSR_RING_BASE_GEN4VF(io_addr, queue->hw_bundle_number,
466 queue->hw_queue_number, queue_base);
468 queue_base = BUILD_RING_BASE_ADDR(queue->base_phys_addr,
470 WRITE_CSR_RING_BASE(io_addr, queue->hw_bundle_number,
471 queue->hw_queue_number, queue_base);
474 QAT_LOG(DEBUG, "RING: Name:%s, size in CSR: %u, in bytes %u,"
475 " nb msgs %u, msg_size %u, modulo mask %u",
477 queue->queue_size, queue_size_bytes,
478 qp_conf->nb_descriptors, desc_size,
484 rte_memzone_free(qp_mz);
489 qat_select_valid_queue(struct qat_pci_device *qat_dev, int qp_id,
490 enum qat_service_type service_type)
492 if (qat_dev->qat_dev_gen == QAT_GEN4) {
493 int i = 0, valid_qps = 0;
495 for (; i < QAT_GEN4_BUNDLE_NUM; i++) {
496 if (qat_dev->qp_gen4_data[i][0].service_type ==
498 if (valid_qps == qp_id)
508 qat_read_qp_config(struct qat_pci_device *qat_dev)
511 enum qat_device_gen qat_dev_gen = qat_dev->qat_dev_gen;
513 if (qat_dev_gen == QAT_GEN4) {
516 if (qat_query_svc_gen4(qat_dev, (uint8_t *)&svc))
518 for (; i < QAT_GEN4_BUNDLE_NUM; i++) {
519 struct qat_qp_hw_data *hw_data =
520 &qat_dev->qp_gen4_data[i][0];
521 uint8_t svc1 = (svc >> (3 * i)) & 0x7;
522 enum qat_service_type service_type = QAT_SERVICE_INVALID;
524 if (svc1 == QAT_SVC_SYM) {
525 service_type = QAT_SERVICE_SYMMETRIC;
527 "Discovered SYMMETRIC service on bundle %d",
529 } else if (svc1 == QAT_SVC_COMPRESSION) {
530 service_type = QAT_SERVICE_COMPRESSION;
532 "Discovered COPRESSION service on bundle %d",
534 } else if (svc1 == QAT_SVC_ASYM) {
535 service_type = QAT_SERVICE_ASYMMETRIC;
537 "Discovered ASYMMETRIC service on bundle %d",
541 "Unrecognized service on bundle %d",
546 memset(hw_data, 0, sizeof(*hw_data));
547 hw_data->service_type = service_type;
548 if (service_type == QAT_SERVICE_ASYMMETRIC) {
549 hw_data->tx_msg_size = 64;
550 hw_data->rx_msg_size = 32;
551 } else if (service_type == QAT_SERVICE_SYMMETRIC ||
553 QAT_SERVICE_COMPRESSION) {
554 hw_data->tx_msg_size = 128;
555 hw_data->rx_msg_size = 32;
557 hw_data->tx_ring_num = 0;
558 hw_data->rx_ring_num = 1;
559 hw_data->hw_bundle_num = i;
566 static int qat_qp_check_queue_alignment(uint64_t phys_addr,
567 uint32_t queue_size_bytes)
569 if (((queue_size_bytes - 1) & phys_addr) != 0)
574 static int adf_verify_queue_size(uint32_t msg_size, uint32_t msg_num,
575 uint32_t *p_queue_size_for_csr)
577 uint8_t i = ADF_MIN_RING_SIZE;
579 for (; i <= ADF_MAX_RING_SIZE; i++)
580 if ((msg_size * msg_num) ==
581 (uint32_t)ADF_SIZE_TO_RING_SIZE_IN_BYTES(i)) {
582 *p_queue_size_for_csr = i;
585 QAT_LOG(ERR, "Invalid ring size %d", msg_size * msg_num);
590 adf_queue_arb_enable(enum qat_device_gen qat_dev_gen, struct qat_queue *txq,
591 void *base_addr, rte_spinlock_t *lock)
593 uint32_t arb_csr_offset = 0, value;
595 rte_spinlock_lock(lock);
596 if (qat_dev_gen == QAT_GEN4) {
597 arb_csr_offset = ADF_ARB_RINGSRVARBEN_OFFSET +
598 (ADF_RING_BUNDLE_SIZE_GEN4 *
599 txq->hw_bundle_number);
600 value = ADF_CSR_RD(base_addr + ADF_RING_CSR_ADDR_OFFSET_GEN4VF,
603 arb_csr_offset = ADF_ARB_RINGSRVARBEN_OFFSET +
605 txq->hw_bundle_number);
606 value = ADF_CSR_RD(base_addr,
609 value |= (0x01 << txq->hw_queue_number);
610 ADF_CSR_WR(base_addr, arb_csr_offset, value);
611 rte_spinlock_unlock(lock);
614 static void adf_queue_arb_disable(enum qat_device_gen qat_dev_gen,
615 struct qat_queue *txq, void *base_addr, rte_spinlock_t *lock)
617 uint32_t arb_csr_offset = 0, value;
619 rte_spinlock_lock(lock);
620 if (qat_dev_gen == QAT_GEN4) {
621 arb_csr_offset = ADF_ARB_RINGSRVARBEN_OFFSET +
622 (ADF_RING_BUNDLE_SIZE_GEN4 *
623 txq->hw_bundle_number);
624 value = ADF_CSR_RD(base_addr + ADF_RING_CSR_ADDR_OFFSET_GEN4VF,
627 arb_csr_offset = ADF_ARB_RINGSRVARBEN_OFFSET +
629 txq->hw_bundle_number);
630 value = ADF_CSR_RD(base_addr,
633 value &= ~(0x01 << txq->hw_queue_number);
634 ADF_CSR_WR(base_addr, arb_csr_offset, value);
635 rte_spinlock_unlock(lock);
638 static void adf_configure_queues(struct qat_qp *qp,
639 enum qat_device_gen qat_dev_gen)
641 uint32_t q_tx_config, q_resp_config;
642 struct qat_queue *q_tx = &qp->tx_q, *q_rx = &qp->rx_q;
644 q_tx_config = BUILD_RING_CONFIG(q_tx->queue_size);
645 q_resp_config = BUILD_RESP_RING_CONFIG(q_rx->queue_size,
646 ADF_RING_NEAR_WATERMARK_512,
647 ADF_RING_NEAR_WATERMARK_0);
649 if (qat_dev_gen == QAT_GEN4) {
650 WRITE_CSR_RING_CONFIG_GEN4VF(qp->mmap_bar_addr,
651 q_tx->hw_bundle_number, q_tx->hw_queue_number,
653 WRITE_CSR_RING_CONFIG_GEN4VF(qp->mmap_bar_addr,
654 q_rx->hw_bundle_number, q_rx->hw_queue_number,
657 WRITE_CSR_RING_CONFIG(qp->mmap_bar_addr,
658 q_tx->hw_bundle_number, q_tx->hw_queue_number,
660 WRITE_CSR_RING_CONFIG(qp->mmap_bar_addr,
661 q_rx->hw_bundle_number, q_rx->hw_queue_number,
666 static inline uint32_t adf_modulo(uint32_t data, uint32_t modulo_mask)
668 return data & modulo_mask;
672 txq_write_tail(enum qat_device_gen qat_dev_gen,
673 struct qat_qp *qp, struct qat_queue *q) {
675 if (qat_dev_gen == QAT_GEN4) {
676 WRITE_CSR_RING_TAIL_GEN4VF(qp->mmap_bar_addr,
677 q->hw_bundle_number, q->hw_queue_number, q->tail);
679 WRITE_CSR_RING_TAIL(qp->mmap_bar_addr, q->hw_bundle_number,
680 q->hw_queue_number, q->tail);
685 void rxq_free_desc(enum qat_device_gen qat_dev_gen, struct qat_qp *qp,
688 uint32_t old_head, new_head;
691 old_head = q->csr_head;
693 max_head = qp->nb_descriptors * q->msg_size;
695 /* write out free descriptors */
696 void *cur_desc = (uint8_t *)q->base_addr + old_head;
698 if (new_head < old_head) {
699 memset(cur_desc, ADF_RING_EMPTY_SIG_BYTE, max_head - old_head);
700 memset(q->base_addr, ADF_RING_EMPTY_SIG_BYTE, new_head);
702 memset(cur_desc, ADF_RING_EMPTY_SIG_BYTE, new_head - old_head);
704 q->nb_processed_responses = 0;
705 q->csr_head = new_head;
707 /* write current head to CSR */
708 if (qat_dev_gen == QAT_GEN4) {
709 WRITE_CSR_RING_HEAD_GEN4VF(qp->mmap_bar_addr,
710 q->hw_bundle_number, q->hw_queue_number, new_head);
712 WRITE_CSR_RING_HEAD(qp->mmap_bar_addr, q->hw_bundle_number,
713 q->hw_queue_number, new_head);
719 qat_enqueue_op_burst(void *qp, void **ops, uint16_t nb_ops)
721 register struct qat_queue *queue;
722 struct qat_qp *tmp_qp = (struct qat_qp *)qp;
723 register uint32_t nb_ops_sent = 0;
724 register int ret = -1;
725 uint16_t nb_ops_possible = nb_ops;
726 register uint8_t *base_addr;
727 register uint32_t tail;
729 if (unlikely(nb_ops == 0))
732 /* read params used a lot in main loop into registers */
733 queue = &(tmp_qp->tx_q);
734 base_addr = (uint8_t *)queue->base_addr;
737 /* Find how many can actually fit on the ring */
739 /* dequeued can only be written by one thread, but it may not
740 * be this thread. As it's 4-byte aligned it will be read
741 * atomically here by any Intel CPU.
742 * enqueued can wrap before dequeued, but cannot
743 * lap it as var size of enq/deq (uint32_t) > var size of
744 * max_inflights (uint16_t). In reality inflights is never
745 * even as big as max uint16_t, as it's <= ADF_MAX_DESC.
746 * On wrapping, the calculation still returns the correct
747 * positive value as all three vars are unsigned.
750 tmp_qp->enqueued - tmp_qp->dequeued;
752 if ((inflights + nb_ops) > tmp_qp->max_inflights) {
753 nb_ops_possible = tmp_qp->max_inflights - inflights;
754 if (nb_ops_possible == 0)
757 /* QAT has plenty of work queued already, so don't waste cycles
758 * enqueueing, wait til the application has gathered a bigger
759 * burst or some completed ops have been dequeued
761 if (tmp_qp->min_enq_burst_threshold && inflights >
762 QAT_QP_MIN_INFL_THRESHOLD && nb_ops_possible <
763 tmp_qp->min_enq_burst_threshold) {
764 tmp_qp->stats.threshold_hit_count++;
770 if (tmp_qp->service_type == QAT_SERVICE_SYMMETRIC)
771 qat_sym_preprocess_requests(ops, nb_ops_possible);
774 while (nb_ops_sent != nb_ops_possible) {
775 if (tmp_qp->service_type == QAT_SERVICE_SYMMETRIC) {
777 ret = qat_sym_build_request(*ops, base_addr + tail,
778 tmp_qp->op_cookies[tail >> queue->trailz],
779 tmp_qp->qat_dev_gen);
781 } else if (tmp_qp->service_type == QAT_SERVICE_COMPRESSION) {
782 ret = qat_comp_build_request(*ops, base_addr + tail,
783 tmp_qp->op_cookies[tail >> queue->trailz],
784 tmp_qp->qat_dev_gen);
785 } else if (tmp_qp->service_type == QAT_SERVICE_ASYMMETRIC) {
786 #ifdef BUILD_QAT_ASYM
787 ret = qat_asym_build_request(*ops, base_addr + tail,
788 tmp_qp->op_cookies[tail >> queue->trailz],
789 tmp_qp->qat_dev_gen);
793 tmp_qp->stats.enqueue_err_count++;
794 /* This message cannot be enqueued */
795 if (nb_ops_sent == 0)
800 tail = adf_modulo(tail + queue->msg_size, queue->modulo_mask);
806 tmp_qp->enqueued += nb_ops_sent;
807 tmp_qp->stats.enqueued_count += nb_ops_sent;
808 txq_write_tail(tmp_qp->qat_dev_gen, tmp_qp, queue);
812 /* Use this for compression only - but keep consistent with above common
813 * function as much as possible.
816 qat_enqueue_comp_op_burst(void *qp, void **ops, uint16_t nb_ops)
818 register struct qat_queue *queue;
819 struct qat_qp *tmp_qp = (struct qat_qp *)qp;
820 register uint32_t nb_ops_sent = 0;
821 register int nb_desc_to_build;
822 uint16_t nb_ops_possible = nb_ops;
823 register uint8_t *base_addr;
824 register uint32_t tail;
826 int descriptors_built, total_descriptors_built = 0;
827 int nb_remaining_descriptors;
830 if (unlikely(nb_ops == 0))
833 /* read params used a lot in main loop into registers */
834 queue = &(tmp_qp->tx_q);
835 base_addr = (uint8_t *)queue->base_addr;
838 /* Find how many can actually fit on the ring */
840 /* dequeued can only be written by one thread, but it may not
841 * be this thread. As it's 4-byte aligned it will be read
842 * atomically here by any Intel CPU.
843 * enqueued can wrap before dequeued, but cannot
844 * lap it as var size of enq/deq (uint32_t) > var size of
845 * max_inflights (uint16_t). In reality inflights is never
846 * even as big as max uint16_t, as it's <= ADF_MAX_DESC.
847 * On wrapping, the calculation still returns the correct
848 * positive value as all three vars are unsigned.
851 tmp_qp->enqueued - tmp_qp->dequeued;
853 /* Find how many can actually fit on the ring */
854 overflow = (inflights + nb_ops) - tmp_qp->max_inflights;
856 nb_ops_possible = nb_ops - overflow;
857 if (nb_ops_possible == 0)
861 /* QAT has plenty of work queued already, so don't waste cycles
862 * enqueueing, wait til the application has gathered a bigger
863 * burst or some completed ops have been dequeued
865 if (tmp_qp->min_enq_burst_threshold && inflights >
866 QAT_QP_MIN_INFL_THRESHOLD && nb_ops_possible <
867 tmp_qp->min_enq_burst_threshold) {
868 tmp_qp->stats.threshold_hit_count++;
873 /* At this point nb_ops_possible is assuming a 1:1 mapping
874 * between ops and descriptors.
875 * Fewer may be sent if some ops have to be split.
876 * nb_ops_possible is <= burst size.
877 * Find out how many spaces are actually available on the qp in case
880 nb_remaining_descriptors = nb_ops_possible
881 + ((overflow >= 0) ? 0 : overflow * (-1));
882 QAT_DP_LOG(DEBUG, "Nb ops requested %d, nb descriptors remaining %d",
883 nb_ops, nb_remaining_descriptors);
885 while (nb_ops_sent != nb_ops_possible &&
886 nb_remaining_descriptors > 0) {
887 struct qat_comp_op_cookie *cookie =
888 tmp_qp->op_cookies[tail >> queue->trailz];
890 descriptors_built = 0;
892 QAT_DP_LOG(DEBUG, "--- data length: %u",
893 ((struct rte_comp_op *)*ops)->src.length);
895 nb_desc_to_build = qat_comp_build_request(*ops,
896 base_addr + tail, cookie, tmp_qp->qat_dev_gen);
897 QAT_DP_LOG(DEBUG, "%d descriptors built, %d remaining, "
898 "%d ops sent, %d descriptors needed",
899 total_descriptors_built, nb_remaining_descriptors,
900 nb_ops_sent, nb_desc_to_build);
902 if (unlikely(nb_desc_to_build < 0)) {
903 /* this message cannot be enqueued */
904 tmp_qp->stats.enqueue_err_count++;
905 if (nb_ops_sent == 0)
908 } else if (unlikely(nb_desc_to_build > 1)) {
909 /* this op is too big and must be split - get more
910 * descriptors and retry
913 QAT_DP_LOG(DEBUG, "Build %d descriptors for this op",
916 nb_remaining_descriptors -= nb_desc_to_build;
917 if (nb_remaining_descriptors >= 0) {
918 /* There are enough remaining descriptors
921 int ret2 = qat_comp_build_multiple_requests(
925 if (unlikely(ret2 < 1)) {
927 "Failed to build (%d) descriptors, status %d",
928 nb_desc_to_build, ret2);
930 qat_comp_free_split_op_memzones(cookie,
931 nb_desc_to_build - 1);
933 tmp_qp->stats.enqueue_err_count++;
935 /* This message cannot be enqueued */
936 if (nb_ops_sent == 0)
940 descriptors_built = ret2;
941 total_descriptors_built +=
943 nb_remaining_descriptors -=
946 "Multiple descriptors (%d) built ok",
950 QAT_DP_LOG(ERR, "For the current op, number of requested descriptors (%d) "
951 "exceeds number of available descriptors (%d)",
953 nb_remaining_descriptors +
956 qat_comp_free_split_op_memzones(cookie,
957 nb_desc_to_build - 1);
959 /* Not enough extra descriptors */
960 if (nb_ops_sent == 0)
965 descriptors_built = 1;
966 total_descriptors_built++;
967 nb_remaining_descriptors--;
968 QAT_DP_LOG(DEBUG, "Single descriptor built ok");
971 tail = adf_modulo(tail + (queue->msg_size * descriptors_built),
979 tmp_qp->enqueued += total_descriptors_built;
980 tmp_qp->stats.enqueued_count += nb_ops_sent;
981 txq_write_tail(tmp_qp->qat_dev_gen, tmp_qp, queue);
986 qat_dequeue_op_burst(void *qp, void **ops, uint16_t nb_ops)
988 struct qat_queue *rx_queue;
989 struct qat_qp *tmp_qp = (struct qat_qp *)qp;
991 uint32_t op_resp_counter = 0, fw_resp_counter = 0;
995 rx_queue = &(tmp_qp->rx_q);
996 head = rx_queue->head;
997 resp_msg = (uint8_t *)rx_queue->base_addr + rx_queue->head;
999 while (*(uint32_t *)resp_msg != ADF_RING_EMPTY_SIG &&
1000 op_resp_counter != nb_ops) {
1002 nb_fw_responses = 1;
1004 if (tmp_qp->service_type == QAT_SERVICE_SYMMETRIC)
1005 qat_sym_process_response(ops, resp_msg,
1006 tmp_qp->op_cookies[head >> rx_queue->trailz]);
1007 else if (tmp_qp->service_type == QAT_SERVICE_COMPRESSION)
1008 nb_fw_responses = qat_comp_process_response(
1010 tmp_qp->op_cookies[head >> rx_queue->trailz],
1011 &tmp_qp->stats.dequeue_err_count);
1012 #ifdef BUILD_QAT_ASYM
1013 else if (tmp_qp->service_type == QAT_SERVICE_ASYMMETRIC)
1014 qat_asym_process_response(ops, resp_msg,
1015 tmp_qp->op_cookies[head >> rx_queue->trailz]);
1018 head = adf_modulo(head + rx_queue->msg_size,
1019 rx_queue->modulo_mask);
1021 resp_msg = (uint8_t *)rx_queue->base_addr + head;
1023 if (nb_fw_responses) {
1024 /* only move on to next op if one was ready to return
1031 /* A compression op may be broken up into multiple fw requests.
1032 * Only count fw responses as complete once ALL the responses
1033 * associated with an op have been processed, as the cookie
1034 * data from the first response must be available until
1035 * finished with all firmware responses.
1037 fw_resp_counter += nb_fw_responses;
1039 rx_queue->nb_processed_responses++;
1042 tmp_qp->dequeued += fw_resp_counter;
1043 tmp_qp->stats.dequeued_count += op_resp_counter;
1045 rx_queue->head = head;
1046 if (rx_queue->nb_processed_responses > QAT_CSR_HEAD_WRITE_THRESH)
1047 rxq_free_desc(tmp_qp->qat_dev_gen, tmp_qp, rx_queue);
1049 QAT_DP_LOG(DEBUG, "Dequeue burst return: %u, QAT responses: %u",
1050 op_resp_counter, fw_resp_counter);
1052 return op_resp_counter;
1055 /* This is almost same as dequeue_op_burst, without the atomic, without stats
1056 * and without the op. Dequeues one response.
1059 qat_cq_dequeue_response(struct qat_qp *qp, void *out_data)
1062 uint8_t retries = 0;
1063 struct qat_queue *queue = &(qp->rx_q);
1064 struct icp_qat_fw_comn_resp *resp_msg = (struct icp_qat_fw_comn_resp *)
1065 ((uint8_t *)queue->base_addr + queue->head);
1067 while (retries++ < QAT_CQ_MAX_DEQ_RETRIES &&
1068 *(uint32_t *)resp_msg == ADF_RING_EMPTY_SIG) {
1069 /* loop waiting for response until we reach the timeout */
1073 if (*(uint32_t *)resp_msg != ADF_RING_EMPTY_SIG) {
1074 /* response received */
1077 /* check status flag */
1078 if (ICP_QAT_FW_COMN_RESP_CRYPTO_STAT_GET(
1079 resp_msg->comn_hdr.comn_status) ==
1080 ICP_QAT_FW_COMN_STATUS_FLAG_OK) {
1082 memcpy(out_data, resp_msg, queue->msg_size);
1084 memset(out_data, 0, queue->msg_size);
1087 queue->head = adf_modulo(queue->head + queue->msg_size,
1088 queue->modulo_mask);
1089 rxq_free_desc(qp->qat_dev_gen, qp, queue);
1095 /* Sends a NULL message and extracts QAT fw version from the response.
1096 * Used to determine detailed capabilities based on the fw version number.
1097 * This assumes that there are no inflight messages, i.e. assumes there's space
1098 * on the qp, one message is sent and only one response collected.
1099 * Returns fw version number or 0 for unknown version or a negative error code.
1102 qat_cq_get_fw_version(struct qat_qp *qp)
1104 struct qat_queue *queue = &(qp->tx_q);
1105 uint8_t *base_addr = (uint8_t *)queue->base_addr;
1106 struct icp_qat_fw_comn_req null_msg;
1107 struct icp_qat_fw_comn_resp response;
1109 /* prepare the NULL request */
1110 memset(&null_msg, 0, sizeof(null_msg));
1111 null_msg.comn_hdr.hdr_flags =
1112 ICP_QAT_FW_COMN_HDR_FLAGS_BUILD(ICP_QAT_FW_COMN_REQ_FLAG_SET);
1113 null_msg.comn_hdr.service_type = ICP_QAT_FW_COMN_REQ_NULL;
1114 null_msg.comn_hdr.service_cmd_id = ICP_QAT_FW_NULL_REQ_SERV_ID;
1116 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
1117 QAT_DP_HEXDUMP_LOG(DEBUG, "NULL request", &null_msg, sizeof(null_msg));
1120 /* send the NULL request */
1121 memcpy(base_addr + queue->tail, &null_msg, sizeof(null_msg));
1122 queue->tail = adf_modulo(queue->tail + queue->msg_size,
1123 queue->modulo_mask);
1124 txq_write_tail(qp->qat_dev_gen, qp, queue);
1126 /* receive a response */
1127 if (qat_cq_dequeue_response(qp, &response)) {
1129 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
1130 QAT_DP_HEXDUMP_LOG(DEBUG, "NULL response:", &response,
1133 /* if LW0 bit 24 is set - then the fw version was returned */
1134 if (QAT_FIELD_GET(response.comn_hdr.hdr_flags,
1135 ICP_QAT_FW_COMN_NULL_VERSION_FLAG_BITPOS,
1136 ICP_QAT_FW_COMN_NULL_VERSION_FLAG_MASK))
1137 return response.resrvd[0]; /* return LW4 */
1139 return 0; /* not set - we don't know fw version */
1142 QAT_LOG(ERR, "No response received");
1147 qat_comp_process_response(void **op __rte_unused, uint8_t *resp __rte_unused,
1148 void *op_cookie __rte_unused,
1149 uint64_t *dequeue_err_count __rte_unused)