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>
9 #include <rte_cryptodev_pmd.h>
11 #include <rte_bus_pci.h>
12 #include <rte_atomic.h>
13 #include <rte_prefetch.h>
19 #include "adf_transport_access_macros.h"
21 #define ADF_MAX_DESC 4096
22 #define ADF_MIN_DESC 128
24 #define ADF_ARB_REG_SLOT 0x1000
25 #define ADF_ARB_RINGSRVARBEN_OFFSET 0x19C
27 #define WRITE_CSR_ARB_RINGSRVARBEN(csr_addr, index, value) \
28 ADF_CSR_WR(csr_addr, ADF_ARB_RINGSRVARBEN_OFFSET + \
29 (ADF_ARB_REG_SLOT * index), value)
31 static int qat_qp_check_queue_alignment(uint64_t phys_addr,
32 uint32_t queue_size_bytes);
33 static void qat_queue_delete(struct qat_queue *queue);
34 static int qat_queue_create(struct rte_cryptodev *dev,
35 struct qat_queue *queue, struct qat_qp_config *, uint8_t dir);
36 static int adf_verify_queue_size(uint32_t msg_size, uint32_t msg_num,
37 uint32_t *queue_size_for_csr);
38 static void adf_configure_queues(struct qat_qp *queue);
39 static void adf_queue_arb_enable(struct qat_queue *txq, void *base_addr);
40 static void adf_queue_arb_disable(struct qat_queue *txq, void *base_addr);
42 static const struct rte_memzone *
43 queue_dma_zone_reserve(const char *queue_name, uint32_t queue_size,
46 const struct rte_memzone *mz;
48 PMD_INIT_FUNC_TRACE();
49 mz = rte_memzone_lookup(queue_name);
51 if (((size_t)queue_size <= mz->len) &&
52 ((socket_id == SOCKET_ID_ANY) ||
53 (socket_id == mz->socket_id))) {
54 PMD_DRV_LOG(DEBUG, "re-use memzone already "
55 "allocated for %s", queue_name);
59 PMD_DRV_LOG(ERR, "Incompatible memzone already "
60 "allocated %s, size %u, socket %d. "
61 "Requested size %u, socket %u",
62 queue_name, (uint32_t)mz->len,
63 mz->socket_id, queue_size, socket_id);
67 PMD_DRV_LOG(DEBUG, "Allocate memzone for %s, size %u on socket %u",
68 queue_name, queue_size, socket_id);
69 return rte_memzone_reserve_aligned(queue_name, queue_size,
70 socket_id, RTE_MEMZONE_IOVA_CONTIG, queue_size);
73 int qat_qp_setup(struct rte_cryptodev *dev, uint16_t queue_pair_id,
74 struct qat_qp_config *qat_qp_conf)
77 struct rte_pci_device *pci_dev;
78 char op_cookie_pool_name[RTE_RING_NAMESIZE];
82 if ((qat_qp_conf->nb_descriptors > ADF_MAX_DESC) ||
83 (qat_qp_conf->nb_descriptors < ADF_MIN_DESC)) {
84 PMD_DRV_LOG(ERR, "Can't create qp for %u descriptors",
85 qat_qp_conf->nb_descriptors);
89 pci_dev = RTE_DEV_TO_PCI(dev->device);
91 if (pci_dev->mem_resource[0].addr == NULL) {
92 PMD_DRV_LOG(ERR, "Could not find VF config space "
93 "(UIO driver attached?).");
97 /* Allocate the queue pair data structure. */
98 qp = rte_zmalloc("qat PMD qp metadata",
99 sizeof(*qp), RTE_CACHE_LINE_SIZE);
101 PMD_DRV_LOG(ERR, "Failed to alloc mem for qp struct");
104 qp->nb_descriptors = qat_qp_conf->nb_descriptors;
105 qp->op_cookies = rte_zmalloc("qat PMD op cookie pointer",
106 qat_qp_conf->nb_descriptors * sizeof(*qp->op_cookies),
107 RTE_CACHE_LINE_SIZE);
108 if (qp->op_cookies == NULL) {
109 PMD_DRV_LOG(ERR, "Failed to alloc mem for cookie");
114 qp->mmap_bar_addr = pci_dev->mem_resource[0].addr;
117 if (qat_queue_create(dev, &(qp->tx_q), qat_qp_conf,
118 ADF_RING_DIR_TX) != 0) {
119 PMD_INIT_LOG(ERR, "Tx queue create failed "
120 "queue_pair_id=%u", queue_pair_id);
124 if (qat_queue_create(dev, &(qp->rx_q), qat_qp_conf,
125 ADF_RING_DIR_RX) != 0) {
126 PMD_DRV_LOG(ERR, "Rx queue create failed "
127 "queue_pair_id=%hu", queue_pair_id);
128 qat_queue_delete(&(qp->tx_q));
132 adf_configure_queues(qp);
133 adf_queue_arb_enable(&qp->tx_q, qp->mmap_bar_addr);
135 snprintf(op_cookie_pool_name, RTE_RING_NAMESIZE, "%s_%s_qp_op_%d_%hu",
136 pci_dev->driver->driver.name, qat_qp_conf->service_str,
137 dev->data->dev_id, queue_pair_id);
139 qp->op_cookie_pool = rte_mempool_lookup(op_cookie_pool_name);
140 if (qp->op_cookie_pool == NULL)
141 qp->op_cookie_pool = rte_mempool_create(op_cookie_pool_name,
143 qat_qp_conf->cookie_size, 64, 0,
144 NULL, NULL, NULL, NULL, qat_qp_conf->socket_id,
146 if (!qp->op_cookie_pool) {
147 PMD_DRV_LOG(ERR, "QAT PMD Cannot create"
152 for (i = 0; i < qp->nb_descriptors; i++) {
153 if (rte_mempool_get(qp->op_cookie_pool, &qp->op_cookies[i])) {
154 PMD_DRV_LOG(ERR, "QAT PMD Cannot get op_cookie");
159 struct qat_pmd_private *internals
160 = dev->data->dev_private;
161 qp->qat_dev_gen = internals->qat_dev_gen;
162 qp->build_request = qat_qp_conf->build_request;
163 qp->process_response = qat_qp_conf->process_response;
165 dev->data->queue_pairs[queue_pair_id] = qp;
173 int qat_qp_release(struct rte_cryptodev *dev, uint16_t queue_pair_id)
176 (struct qat_qp *)dev->data->queue_pairs[queue_pair_id];
179 PMD_INIT_FUNC_TRACE();
181 PMD_DRV_LOG(DEBUG, "qp already freed");
185 /* Don't free memory if there are still responses to be processed */
186 if (qp->inflights16 == 0) {
187 qat_queue_delete(&(qp->tx_q));
188 qat_queue_delete(&(qp->rx_q));
193 adf_queue_arb_disable(&(qp->tx_q), qp->mmap_bar_addr);
195 for (i = 0; i < qp->nb_descriptors; i++)
196 rte_mempool_put(qp->op_cookie_pool, qp->op_cookies[i]);
198 if (qp->op_cookie_pool)
199 rte_mempool_free(qp->op_cookie_pool);
201 rte_free(qp->op_cookies);
203 dev->data->queue_pairs[queue_pair_id] = NULL;
210 static void qat_queue_delete(struct qat_queue *queue)
212 const struct rte_memzone *mz;
216 PMD_DRV_LOG(DEBUG, "Invalid queue");
219 mz = rte_memzone_lookup(queue->memz_name);
221 /* Write an unused pattern to the queue memory. */
222 memset(queue->base_addr, 0x7F, queue->queue_size);
223 status = rte_memzone_free(mz);
225 PMD_DRV_LOG(ERR, "Error %d on freeing queue %s",
226 status, queue->memz_name);
228 PMD_DRV_LOG(DEBUG, "queue %s doesn't exist",
234 qat_queue_create(struct rte_cryptodev *dev, struct qat_queue *queue,
235 struct qat_qp_config *qp_conf, uint8_t dir)
239 const struct rte_memzone *qp_mz;
240 struct rte_pci_device *pci_dev;
242 uint16_t desc_size = (dir == ADF_RING_DIR_TX ?
243 qp_conf->tx_msg_size : qp_conf->rx_msg_size);
244 uint32_t queue_size_bytes = (qp_conf->nb_descriptors)*(desc_size);
246 queue->hw_bundle_number = qp_conf->hw_bundle_num;
247 queue->hw_queue_number = (dir == ADF_RING_DIR_TX ?
248 qp_conf->tx_ring_num : qp_conf->rx_ring_num);
250 if (desc_size > ADF_MSG_SIZE_TO_BYTES(ADF_MAX_MSG_SIZE)) {
251 PMD_DRV_LOG(ERR, "Invalid descriptor size %d", desc_size);
255 pci_dev = RTE_DEV_TO_PCI(dev->device);
258 * Allocate a memzone for the queue - create a unique name.
260 snprintf(queue->memz_name, sizeof(queue->memz_name),
262 pci_dev->driver->driver.name, qp_conf->service_str,
263 "qp_mem", dev->data->dev_id,
264 queue->hw_bundle_number, queue->hw_queue_number);
265 qp_mz = queue_dma_zone_reserve(queue->memz_name, queue_size_bytes,
268 PMD_DRV_LOG(ERR, "Failed to allocate ring memzone");
272 queue->base_addr = (char *)qp_mz->addr;
273 queue->base_phys_addr = qp_mz->iova;
274 if (qat_qp_check_queue_alignment(queue->base_phys_addr,
276 PMD_DRV_LOG(ERR, "Invalid alignment on queue create "
278 queue->base_phys_addr);
280 goto queue_create_err;
283 if (adf_verify_queue_size(desc_size, qp_conf->nb_descriptors,
284 &(queue->queue_size)) != 0) {
285 PMD_DRV_LOG(ERR, "Invalid num inflights");
287 goto queue_create_err;
290 queue->max_inflights = ADF_MAX_INFLIGHTS(queue->queue_size,
291 ADF_BYTES_TO_MSG_SIZE(desc_size));
292 queue->modulo = ADF_RING_SIZE_MODULO(queue->queue_size);
293 PMD_DRV_LOG(DEBUG, "RING: Name:%s, size in CSR: %u, in bytes %u,"
294 " nb msgs %u, msg_size %u, max_inflights %u modulo %u",
296 queue->queue_size, queue_size_bytes,
297 qp_conf->nb_descriptors, desc_size,
298 queue->max_inflights, queue->modulo);
300 if (queue->max_inflights < 2) {
301 PMD_DRV_LOG(ERR, "Invalid num inflights");
303 goto queue_create_err;
307 queue->msg_size = desc_size;
310 * Write an unused pattern to the queue memory.
312 memset(queue->base_addr, 0x7F, queue_size_bytes);
314 queue_base = BUILD_RING_BASE_ADDR(queue->base_phys_addr,
317 io_addr = pci_dev->mem_resource[0].addr;
319 WRITE_CSR_RING_BASE(io_addr, queue->hw_bundle_number,
320 queue->hw_queue_number, queue_base);
324 rte_memzone_free(qp_mz);
328 static int qat_qp_check_queue_alignment(uint64_t phys_addr,
329 uint32_t queue_size_bytes)
331 PMD_INIT_FUNC_TRACE();
332 if (((queue_size_bytes - 1) & phys_addr) != 0)
337 static int adf_verify_queue_size(uint32_t msg_size, uint32_t msg_num,
338 uint32_t *p_queue_size_for_csr)
340 uint8_t i = ADF_MIN_RING_SIZE;
342 PMD_INIT_FUNC_TRACE();
343 for (; i <= ADF_MAX_RING_SIZE; i++)
344 if ((msg_size * msg_num) ==
345 (uint32_t)ADF_SIZE_TO_RING_SIZE_IN_BYTES(i)) {
346 *p_queue_size_for_csr = i;
349 PMD_DRV_LOG(ERR, "Invalid ring size %d", msg_size * msg_num);
353 static void adf_queue_arb_enable(struct qat_queue *txq, void *base_addr)
355 uint32_t arb_csr_offset = ADF_ARB_RINGSRVARBEN_OFFSET +
357 txq->hw_bundle_number);
360 PMD_INIT_FUNC_TRACE();
361 value = ADF_CSR_RD(base_addr, arb_csr_offset);
362 value |= (0x01 << txq->hw_queue_number);
363 ADF_CSR_WR(base_addr, arb_csr_offset, value);
366 static void adf_queue_arb_disable(struct qat_queue *txq, void *base_addr)
368 uint32_t arb_csr_offset = ADF_ARB_RINGSRVARBEN_OFFSET +
370 txq->hw_bundle_number);
373 PMD_INIT_FUNC_TRACE();
374 value = ADF_CSR_RD(base_addr, arb_csr_offset);
375 value ^= (0x01 << txq->hw_queue_number);
376 ADF_CSR_WR(base_addr, arb_csr_offset, value);
379 static void adf_configure_queues(struct qat_qp *qp)
381 uint32_t queue_config;
382 struct qat_queue *queue = &qp->tx_q;
384 PMD_INIT_FUNC_TRACE();
385 queue_config = BUILD_RING_CONFIG(queue->queue_size);
387 WRITE_CSR_RING_CONFIG(qp->mmap_bar_addr, queue->hw_bundle_number,
388 queue->hw_queue_number, queue_config);
392 BUILD_RESP_RING_CONFIG(queue->queue_size,
393 ADF_RING_NEAR_WATERMARK_512,
394 ADF_RING_NEAR_WATERMARK_0);
396 WRITE_CSR_RING_CONFIG(qp->mmap_bar_addr, queue->hw_bundle_number,
397 queue->hw_queue_number, queue_config);
401 static inline uint32_t adf_modulo(uint32_t data, uint32_t shift)
403 uint32_t div = data >> shift;
404 uint32_t mult = div << shift;
410 txq_write_tail(struct qat_qp *qp, struct qat_queue *q) {
411 WRITE_CSR_RING_TAIL(qp->mmap_bar_addr, q->hw_bundle_number,
412 q->hw_queue_number, q->tail);
413 q->nb_pending_requests = 0;
414 q->csr_tail = q->tail;
418 void rxq_free_desc(struct qat_qp *qp, struct qat_queue *q)
420 uint32_t old_head, new_head;
423 old_head = q->csr_head;
425 max_head = qp->nb_descriptors * q->msg_size;
427 /* write out free descriptors */
428 void *cur_desc = (uint8_t *)q->base_addr + old_head;
430 if (new_head < old_head) {
431 memset(cur_desc, ADF_RING_EMPTY_SIG_BYTE, max_head - old_head);
432 memset(q->base_addr, ADF_RING_EMPTY_SIG_BYTE, new_head);
434 memset(cur_desc, ADF_RING_EMPTY_SIG_BYTE, new_head - old_head);
436 q->nb_processed_responses = 0;
437 q->csr_head = new_head;
439 /* write current head to CSR */
440 WRITE_CSR_RING_HEAD(qp->mmap_bar_addr, q->hw_bundle_number,
441 q->hw_queue_number, new_head);
445 qat_enqueue_op_burst(void *qp, void **ops, uint16_t nb_ops)
447 register struct qat_queue *queue;
448 struct qat_qp *tmp_qp = (struct qat_qp *)qp;
449 register uint32_t nb_ops_sent = 0;
451 uint16_t nb_ops_possible = nb_ops;
452 register uint8_t *base_addr;
453 register uint32_t tail;
456 if (unlikely(nb_ops == 0))
459 /* read params used a lot in main loop into registers */
460 queue = &(tmp_qp->tx_q);
461 base_addr = (uint8_t *)queue->base_addr;
464 /* Find how many can actually fit on the ring */
465 tmp_qp->inflights16 += nb_ops;
466 overflow = tmp_qp->inflights16 - queue->max_inflights;
468 tmp_qp->inflights16 -= overflow;
469 nb_ops_possible = nb_ops - overflow;
470 if (nb_ops_possible == 0)
474 while (nb_ops_sent != nb_ops_possible) {
475 ret = tmp_qp->build_request(*ops, base_addr + tail,
476 tmp_qp->op_cookies[tail / queue->msg_size],
477 tmp_qp->qat_dev_gen);
479 tmp_qp->stats.enqueue_err_count++;
481 * This message cannot be enqueued,
482 * decrease number of ops that wasn't sent
484 tmp_qp->inflights16 -= nb_ops_possible - nb_ops_sent;
485 if (nb_ops_sent == 0)
490 tail = adf_modulo(tail + queue->msg_size, queue->modulo);
496 tmp_qp->stats.enqueued_count += nb_ops_sent;
497 queue->nb_pending_requests += nb_ops_sent;
498 if (tmp_qp->inflights16 < QAT_CSR_TAIL_FORCE_WRITE_THRESH ||
499 queue->nb_pending_requests > QAT_CSR_TAIL_WRITE_THRESH) {
500 txq_write_tail(tmp_qp, queue);
506 qat_dequeue_op_burst(void *qp, void **ops, uint16_t nb_ops)
508 struct qat_queue *rx_queue, *tx_queue;
509 struct qat_qp *tmp_qp = (struct qat_qp *)qp;
511 uint32_t resp_counter = 0;
514 rx_queue = &(tmp_qp->rx_q);
515 tx_queue = &(tmp_qp->tx_q);
516 head = rx_queue->head;
517 resp_msg = (uint8_t *)rx_queue->base_addr + rx_queue->head;
519 while (*(uint32_t *)resp_msg != ADF_RING_EMPTY_SIG &&
520 resp_counter != nb_ops) {
522 tmp_qp->process_response(ops, resp_msg,
523 tmp_qp->op_cookies[head / rx_queue->msg_size],
524 tmp_qp->qat_dev_gen);
526 head = adf_modulo(head + rx_queue->msg_size, rx_queue->modulo);
528 resp_msg = (uint8_t *)rx_queue->base_addr + head;
532 if (resp_counter > 0) {
533 rx_queue->head = head;
534 tmp_qp->stats.dequeued_count += resp_counter;
535 rx_queue->nb_processed_responses += resp_counter;
536 tmp_qp->inflights16 -= resp_counter;
538 if (rx_queue->nb_processed_responses >
539 QAT_CSR_HEAD_WRITE_THRESH)
540 rxq_free_desc(tmp_qp, rx_queue);
542 /* also check if tail needs to be advanced */
543 if (tmp_qp->inflights16 <= QAT_CSR_TAIL_FORCE_WRITE_THRESH &&
544 tx_queue->tail != tx_queue->csr_tail) {
545 txq_write_tail(tmp_qp, tx_queue);