1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2018 Intel Corporation
5 #include <rte_common.h>
7 #include <rte_malloc.h>
8 #include <rte_memzone.h>
10 #include <rte_bus_pci.h>
11 #include <rte_atomic.h>
12 #include <rte_prefetch.h>
16 #include "qat_device.h"
17 #include "adf_transport_access_macros.h"
20 #define ADF_MAX_DESC 4096
21 #define ADF_MIN_DESC 128
23 #define ADF_ARB_REG_SLOT 0x1000
24 #define ADF_ARB_RINGSRVARBEN_OFFSET 0x19C
26 #define WRITE_CSR_ARB_RINGSRVARBEN(csr_addr, index, value) \
27 ADF_CSR_WR(csr_addr, ADF_ARB_RINGSRVARBEN_OFFSET + \
28 (ADF_ARB_REG_SLOT * index), value)
31 const struct qat_qp_hw_data qat_gen1_qps[QAT_MAX_SERVICES]
32 [ADF_MAX_QPS_PER_BUNDLE] = {
33 /* queue pairs which provide an asymmetric crypto service */
34 [QAT_SERVICE_ASYMMETRIC] = {
36 .service_type = QAT_SERVICE_ASYMMETRIC,
44 .service_type = QAT_SERVICE_ASYMMETRIC,
50 .service_type = QAT_SERVICE_INVALID,
52 .service_type = QAT_SERVICE_INVALID,
55 /* queue pairs which provide a symmetric crypto service */
56 [QAT_SERVICE_SYMMETRIC] = {
58 .service_type = QAT_SERVICE_SYMMETRIC,
66 .service_type = QAT_SERVICE_SYMMETRIC,
73 .service_type = QAT_SERVICE_INVALID,
75 .service_type = QAT_SERVICE_INVALID,
78 /* queue pairs which provide a compression service */
79 [QAT_SERVICE_COMPRESSION] = {
81 .service_type = QAT_SERVICE_COMPRESSION,
88 .service_type = QAT_SERVICE_COMPRESSION,
95 .service_type = QAT_SERVICE_INVALID,
97 .service_type = QAT_SERVICE_INVALID,
102 static int qat_qp_check_queue_alignment(uint64_t phys_addr,
103 uint32_t queue_size_bytes);
104 static void qat_queue_delete(struct qat_queue *queue);
105 static int qat_queue_create(struct qat_pci_device *qat_dev,
106 struct qat_queue *queue, struct qat_qp_config *, uint8_t dir);
107 static int adf_verify_queue_size(uint32_t msg_size, uint32_t msg_num,
108 uint32_t *queue_size_for_csr);
109 static void adf_configure_queues(struct qat_qp *queue);
110 static void adf_queue_arb_enable(struct qat_queue *txq, void *base_addr);
111 static void adf_queue_arb_disable(struct qat_queue *txq, void *base_addr);
114 int qat_qps_per_service(const struct qat_qp_hw_data *qp_hw_data,
115 enum qat_service_type service)
119 for (i = 0, count = 0; i < ADF_MAX_QPS_PER_BUNDLE; i++)
120 if (qp_hw_data[i].service_type == service)
122 return count * ADF_NUM_BUNDLES_PER_DEV;
125 static const struct rte_memzone *
126 queue_dma_zone_reserve(const char *queue_name, uint32_t queue_size,
129 const struct rte_memzone *mz;
131 PMD_INIT_FUNC_TRACE();
132 mz = rte_memzone_lookup(queue_name);
134 if (((size_t)queue_size <= mz->len) &&
135 ((socket_id == SOCKET_ID_ANY) ||
136 (socket_id == mz->socket_id))) {
137 PMD_DRV_LOG(DEBUG, "re-use memzone already "
138 "allocated for %s", queue_name);
142 PMD_DRV_LOG(ERR, "Incompatible memzone already "
143 "allocated %s, size %u, socket %d. "
144 "Requested size %u, socket %u",
145 queue_name, (uint32_t)mz->len,
146 mz->socket_id, queue_size, socket_id);
150 PMD_DRV_LOG(DEBUG, "Allocate memzone for %s, size %u on socket %u",
151 queue_name, queue_size, socket_id);
152 return rte_memzone_reserve_aligned(queue_name, queue_size,
153 socket_id, RTE_MEMZONE_IOVA_CONTIG, queue_size);
156 int qat_qp_setup(struct qat_pci_device *qat_dev,
157 struct qat_qp **qp_addr,
158 uint16_t queue_pair_id,
159 struct qat_qp_config *qat_qp_conf)
163 struct rte_pci_device *pci_dev = qat_dev->pci_dev;
164 char op_cookie_pool_name[RTE_RING_NAMESIZE];
167 PMD_DRV_LOG(DEBUG, "Setup qp %u on qat pci device %d gen %d",
168 queue_pair_id, qat_dev->qat_dev_id, qat_dev->qat_dev_gen);
170 if ((qat_qp_conf->nb_descriptors > ADF_MAX_DESC) ||
171 (qat_qp_conf->nb_descriptors < ADF_MIN_DESC)) {
172 PMD_DRV_LOG(ERR, "Can't create qp for %u descriptors",
173 qat_qp_conf->nb_descriptors);
177 if (pci_dev->mem_resource[0].addr == NULL) {
178 PMD_DRV_LOG(ERR, "Could not find VF config space "
179 "(UIO driver attached?).");
183 /* Allocate the queue pair data structure. */
184 qp = rte_zmalloc("qat PMD qp metadata",
185 sizeof(*qp), RTE_CACHE_LINE_SIZE);
187 PMD_DRV_LOG(ERR, "Failed to alloc mem for qp struct");
190 qp->nb_descriptors = qat_qp_conf->nb_descriptors;
191 qp->op_cookies = rte_zmalloc("qat PMD op cookie pointer",
192 qat_qp_conf->nb_descriptors * sizeof(*qp->op_cookies),
193 RTE_CACHE_LINE_SIZE);
194 if (qp->op_cookies == NULL) {
195 PMD_DRV_LOG(ERR, "Failed to alloc mem for cookie");
200 qp->mmap_bar_addr = pci_dev->mem_resource[0].addr;
203 if (qat_queue_create(qat_dev, &(qp->tx_q), qat_qp_conf,
204 ADF_RING_DIR_TX) != 0) {
205 PMD_INIT_LOG(ERR, "Tx queue create failed "
206 "queue_pair_id=%u", queue_pair_id);
210 if (qat_queue_create(qat_dev, &(qp->rx_q), qat_qp_conf,
211 ADF_RING_DIR_RX) != 0) {
212 PMD_DRV_LOG(ERR, "Rx queue create failed "
213 "queue_pair_id=%hu", queue_pair_id);
214 qat_queue_delete(&(qp->tx_q));
218 adf_configure_queues(qp);
219 adf_queue_arb_enable(&qp->tx_q, qp->mmap_bar_addr);
221 snprintf(op_cookie_pool_name, RTE_RING_NAMESIZE,
222 "%s%d_cookies_%s_qp%hu",
223 pci_dev->driver->driver.name, qat_dev->qat_dev_id,
224 qat_qp_conf->service_str, queue_pair_id);
226 PMD_DRV_LOG(DEBUG, "cookiepool: %s", op_cookie_pool_name);
227 qp->op_cookie_pool = rte_mempool_lookup(op_cookie_pool_name);
228 if (qp->op_cookie_pool == NULL)
229 qp->op_cookie_pool = rte_mempool_create(op_cookie_pool_name,
231 qat_qp_conf->cookie_size, 64, 0,
232 NULL, NULL, NULL, NULL, qat_qp_conf->socket_id,
234 if (!qp->op_cookie_pool) {
235 PMD_DRV_LOG(ERR, "QAT PMD Cannot create"
240 for (i = 0; i < qp->nb_descriptors; i++) {
241 if (rte_mempool_get(qp->op_cookie_pool, &qp->op_cookies[i])) {
242 PMD_DRV_LOG(ERR, "QAT PMD Cannot get op_cookie");
247 qp->qat_dev_gen = qat_dev->qat_dev_gen;
248 qp->build_request = qat_qp_conf->build_request;
249 qp->process_response = qat_qp_conf->process_response;
250 qp->qat_dev = qat_dev;
252 PMD_DRV_LOG(DEBUG, "QP setup complete: id: %d, cookiepool: %s",
253 queue_pair_id, op_cookie_pool_name);
263 int qat_qp_release(struct qat_qp **qp_addr)
265 struct qat_qp *qp = *qp_addr;
268 PMD_INIT_FUNC_TRACE();
270 PMD_DRV_LOG(DEBUG, "qp already freed");
274 PMD_DRV_LOG(DEBUG, "Free qp on qat_pci device %d",
275 qp->qat_dev->qat_dev_id);
277 /* Don't free memory if there are still responses to be processed */
278 if (qp->inflights16 == 0) {
279 qat_queue_delete(&(qp->tx_q));
280 qat_queue_delete(&(qp->rx_q));
285 adf_queue_arb_disable(&(qp->tx_q), qp->mmap_bar_addr);
287 for (i = 0; i < qp->nb_descriptors; i++)
288 rte_mempool_put(qp->op_cookie_pool, qp->op_cookies[i]);
290 if (qp->op_cookie_pool)
291 rte_mempool_free(qp->op_cookie_pool);
293 rte_free(qp->op_cookies);
300 static void qat_queue_delete(struct qat_queue *queue)
302 const struct rte_memzone *mz;
306 PMD_DRV_LOG(DEBUG, "Invalid queue");
309 PMD_DRV_LOG(DEBUG, "Free ring %d, memzone: %s",
310 queue->hw_queue_number, queue->memz_name);
312 mz = rte_memzone_lookup(queue->memz_name);
314 /* Write an unused pattern to the queue memory. */
315 memset(queue->base_addr, 0x7F, queue->queue_size);
316 status = rte_memzone_free(mz);
318 PMD_DRV_LOG(ERR, "Error %d on freeing queue %s",
319 status, queue->memz_name);
321 PMD_DRV_LOG(DEBUG, "queue %s doesn't exist",
327 qat_queue_create(struct qat_pci_device *qat_dev, struct qat_queue *queue,
328 struct qat_qp_config *qp_conf, uint8_t dir)
332 const struct rte_memzone *qp_mz;
333 struct rte_pci_device *pci_dev = qat_dev->pci_dev;
335 uint16_t desc_size = (dir == ADF_RING_DIR_TX ?
336 qp_conf->hw->tx_msg_size : qp_conf->hw->rx_msg_size);
337 uint32_t queue_size_bytes = (qp_conf->nb_descriptors)*(desc_size);
339 queue->hw_bundle_number = qp_conf->hw->hw_bundle_num;
340 queue->hw_queue_number = (dir == ADF_RING_DIR_TX ?
341 qp_conf->hw->tx_ring_num : qp_conf->hw->rx_ring_num);
343 if (desc_size > ADF_MSG_SIZE_TO_BYTES(ADF_MAX_MSG_SIZE)) {
344 PMD_DRV_LOG(ERR, "Invalid descriptor size %d", desc_size);
349 * Allocate a memzone for the queue - create a unique name.
351 snprintf(queue->memz_name, sizeof(queue->memz_name),
353 pci_dev->driver->driver.name, qat_dev->qat_dev_id,
354 qp_conf->service_str, "qp_mem",
355 queue->hw_bundle_number, queue->hw_queue_number);
356 qp_mz = queue_dma_zone_reserve(queue->memz_name, queue_size_bytes,
359 PMD_DRV_LOG(ERR, "Failed to allocate ring memzone");
363 queue->base_addr = (char *)qp_mz->addr;
364 queue->base_phys_addr = qp_mz->iova;
365 if (qat_qp_check_queue_alignment(queue->base_phys_addr,
367 PMD_DRV_LOG(ERR, "Invalid alignment on queue create "
369 queue->base_phys_addr);
371 goto queue_create_err;
374 if (adf_verify_queue_size(desc_size, qp_conf->nb_descriptors,
375 &(queue->queue_size)) != 0) {
376 PMD_DRV_LOG(ERR, "Invalid num inflights");
378 goto queue_create_err;
381 queue->max_inflights = ADF_MAX_INFLIGHTS(queue->queue_size,
382 ADF_BYTES_TO_MSG_SIZE(desc_size));
383 queue->modulo = ADF_RING_SIZE_MODULO(queue->queue_size);
385 if (queue->max_inflights < 2) {
386 PMD_DRV_LOG(ERR, "Invalid num inflights");
388 goto queue_create_err;
392 queue->msg_size = desc_size;
395 * Write an unused pattern to the queue memory.
397 memset(queue->base_addr, 0x7F, queue_size_bytes);
399 queue_base = BUILD_RING_BASE_ADDR(queue->base_phys_addr,
402 io_addr = pci_dev->mem_resource[0].addr;
404 WRITE_CSR_RING_BASE(io_addr, queue->hw_bundle_number,
405 queue->hw_queue_number, queue_base);
407 PMD_DRV_LOG(DEBUG, "RING: Name:%s, size in CSR: %u, in bytes %u,"
408 " nb msgs %u, msg_size %u, max_inflights %u modulo %u",
410 queue->queue_size, queue_size_bytes,
411 qp_conf->nb_descriptors, desc_size,
412 queue->max_inflights, queue->modulo);
417 rte_memzone_free(qp_mz);
421 static int qat_qp_check_queue_alignment(uint64_t phys_addr,
422 uint32_t queue_size_bytes)
424 PMD_INIT_FUNC_TRACE();
425 if (((queue_size_bytes - 1) & phys_addr) != 0)
430 static int adf_verify_queue_size(uint32_t msg_size, uint32_t msg_num,
431 uint32_t *p_queue_size_for_csr)
433 uint8_t i = ADF_MIN_RING_SIZE;
435 PMD_INIT_FUNC_TRACE();
436 for (; i <= ADF_MAX_RING_SIZE; i++)
437 if ((msg_size * msg_num) ==
438 (uint32_t)ADF_SIZE_TO_RING_SIZE_IN_BYTES(i)) {
439 *p_queue_size_for_csr = i;
442 PMD_DRV_LOG(ERR, "Invalid ring size %d", msg_size * msg_num);
446 static void adf_queue_arb_enable(struct qat_queue *txq, void *base_addr)
448 uint32_t arb_csr_offset = ADF_ARB_RINGSRVARBEN_OFFSET +
450 txq->hw_bundle_number);
453 PMD_INIT_FUNC_TRACE();
454 value = ADF_CSR_RD(base_addr, arb_csr_offset);
455 value |= (0x01 << txq->hw_queue_number);
456 ADF_CSR_WR(base_addr, arb_csr_offset, value);
459 static void adf_queue_arb_disable(struct qat_queue *txq, void *base_addr)
461 uint32_t arb_csr_offset = ADF_ARB_RINGSRVARBEN_OFFSET +
463 txq->hw_bundle_number);
466 PMD_INIT_FUNC_TRACE();
467 value = ADF_CSR_RD(base_addr, arb_csr_offset);
468 value ^= (0x01 << txq->hw_queue_number);
469 ADF_CSR_WR(base_addr, arb_csr_offset, value);
472 static void adf_configure_queues(struct qat_qp *qp)
474 uint32_t queue_config;
475 struct qat_queue *queue = &qp->tx_q;
477 PMD_INIT_FUNC_TRACE();
478 queue_config = BUILD_RING_CONFIG(queue->queue_size);
480 WRITE_CSR_RING_CONFIG(qp->mmap_bar_addr, queue->hw_bundle_number,
481 queue->hw_queue_number, queue_config);
485 BUILD_RESP_RING_CONFIG(queue->queue_size,
486 ADF_RING_NEAR_WATERMARK_512,
487 ADF_RING_NEAR_WATERMARK_0);
489 WRITE_CSR_RING_CONFIG(qp->mmap_bar_addr, queue->hw_bundle_number,
490 queue->hw_queue_number, queue_config);
494 static inline uint32_t adf_modulo(uint32_t data, uint32_t shift)
496 uint32_t div = data >> shift;
497 uint32_t mult = div << shift;
503 txq_write_tail(struct qat_qp *qp, struct qat_queue *q) {
504 WRITE_CSR_RING_TAIL(qp->mmap_bar_addr, q->hw_bundle_number,
505 q->hw_queue_number, q->tail);
506 q->nb_pending_requests = 0;
507 q->csr_tail = q->tail;
511 void rxq_free_desc(struct qat_qp *qp, struct qat_queue *q)
513 uint32_t old_head, new_head;
516 old_head = q->csr_head;
518 max_head = qp->nb_descriptors * q->msg_size;
520 /* write out free descriptors */
521 void *cur_desc = (uint8_t *)q->base_addr + old_head;
523 if (new_head < old_head) {
524 memset(cur_desc, ADF_RING_EMPTY_SIG_BYTE, max_head - old_head);
525 memset(q->base_addr, ADF_RING_EMPTY_SIG_BYTE, new_head);
527 memset(cur_desc, ADF_RING_EMPTY_SIG_BYTE, new_head - old_head);
529 q->nb_processed_responses = 0;
530 q->csr_head = new_head;
532 /* write current head to CSR */
533 WRITE_CSR_RING_HEAD(qp->mmap_bar_addr, q->hw_bundle_number,
534 q->hw_queue_number, new_head);
538 qat_enqueue_op_burst(void *qp, void **ops, uint16_t nb_ops)
540 register struct qat_queue *queue;
541 struct qat_qp *tmp_qp = (struct qat_qp *)qp;
542 register uint32_t nb_ops_sent = 0;
544 uint16_t nb_ops_possible = nb_ops;
545 register uint8_t *base_addr;
546 register uint32_t tail;
549 if (unlikely(nb_ops == 0))
552 /* read params used a lot in main loop into registers */
553 queue = &(tmp_qp->tx_q);
554 base_addr = (uint8_t *)queue->base_addr;
557 /* Find how many can actually fit on the ring */
558 tmp_qp->inflights16 += nb_ops;
559 overflow = tmp_qp->inflights16 - queue->max_inflights;
561 tmp_qp->inflights16 -= overflow;
562 nb_ops_possible = nb_ops - overflow;
563 if (nb_ops_possible == 0)
567 while (nb_ops_sent != nb_ops_possible) {
568 ret = tmp_qp->build_request(*ops, base_addr + tail,
569 tmp_qp->op_cookies[tail / queue->msg_size],
570 tmp_qp->qat_dev_gen);
572 tmp_qp->stats.enqueue_err_count++;
574 * This message cannot be enqueued,
575 * decrease number of ops that wasn't sent
577 tmp_qp->inflights16 -= nb_ops_possible - nb_ops_sent;
578 if (nb_ops_sent == 0)
583 tail = adf_modulo(tail + queue->msg_size, queue->modulo);
589 tmp_qp->stats.enqueued_count += nb_ops_sent;
590 queue->nb_pending_requests += nb_ops_sent;
591 if (tmp_qp->inflights16 < QAT_CSR_TAIL_FORCE_WRITE_THRESH ||
592 queue->nb_pending_requests > QAT_CSR_TAIL_WRITE_THRESH) {
593 txq_write_tail(tmp_qp, queue);
599 qat_dequeue_op_burst(void *qp, void **ops, uint16_t nb_ops)
601 struct qat_queue *rx_queue, *tx_queue;
602 struct qat_qp *tmp_qp = (struct qat_qp *)qp;
604 uint32_t resp_counter = 0;
607 rx_queue = &(tmp_qp->rx_q);
608 tx_queue = &(tmp_qp->tx_q);
609 head = rx_queue->head;
610 resp_msg = (uint8_t *)rx_queue->base_addr + rx_queue->head;
612 while (*(uint32_t *)resp_msg != ADF_RING_EMPTY_SIG &&
613 resp_counter != nb_ops) {
615 tmp_qp->process_response(ops, resp_msg,
616 tmp_qp->op_cookies[head / rx_queue->msg_size],
617 tmp_qp->qat_dev_gen);
619 head = adf_modulo(head + rx_queue->msg_size, rx_queue->modulo);
621 resp_msg = (uint8_t *)rx_queue->base_addr + head;
625 if (resp_counter > 0) {
626 rx_queue->head = head;
627 tmp_qp->stats.dequeued_count += resp_counter;
628 rx_queue->nb_processed_responses += resp_counter;
629 tmp_qp->inflights16 -= resp_counter;
631 if (rx_queue->nb_processed_responses >
632 QAT_CSR_HEAD_WRITE_THRESH)
633 rxq_free_desc(tmp_qp, rx_queue);
635 /* also check if tail needs to be advanced */
636 if (tmp_qp->inflights16 <= QAT_CSR_TAIL_FORCE_WRITE_THRESH &&
637 tx_queue->tail != tx_queue->csr_tail) {
638 txq_write_tail(tmp_qp, tx_queue);