Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2015-2022 Intel Corporation
3 : : */
4 : :
5 : : #include <rte_common.h>
6 : : #include <rte_cycles.h>
7 : : #include <dev_driver.h>
8 : : #include <rte_malloc.h>
9 : : #include <rte_memzone.h>
10 : : #include <rte_pci.h>
11 : : #include <bus_pci_driver.h>
12 : : #include <rte_atomic.h>
13 : : #include <rte_prefetch.h>
14 : : #ifdef BUILD_QAT_SYM
15 : : #include <rte_ether.h>
16 : : #endif
17 : :
18 : : #include "qat_logs.h"
19 : : #include "qat_device.h"
20 : : #include "qat_qp.h"
21 : : #include "qat_sym.h"
22 : : #include "qat_asym.h"
23 : : #include "qat_comp.h"
24 : :
25 : : #define QAT_CQ_MAX_DEQ_RETRIES 10
26 : :
27 : : #define ADF_MAX_DESC 4096
28 : : #define ADF_MIN_DESC 128
29 : :
30 : : #ifdef BUILD_QAT_SYM
31 : : /* Cipher-CRC capability check test parameters */
32 : : static const uint8_t cipher_crc_cap_check_iv[] = {
33 : : 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
34 : : 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11
35 : : };
36 : :
37 : : static const uint8_t cipher_crc_cap_check_key[] = {
38 : : 0x00, 0x00, 0x00, 0x00, 0xAA, 0xBB, 0xCC, 0xDD,
39 : : 0xEE, 0xFF, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55
40 : : };
41 : :
42 : : static const uint8_t cipher_crc_cap_check_plaintext[] = {
43 : : /* Outer protocol header */
44 : : 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45 : : /* Ethernet frame */
46 : : 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x06, 0x05,
47 : : 0x04, 0x03, 0x02, 0x01, 0x08, 0x00, 0xAA, 0xAA,
48 : : 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
49 : : /* CRC */
50 : : 0xFF, 0xFF, 0xFF, 0xFF
51 : : };
52 : :
53 : : static const uint8_t cipher_crc_cap_check_ciphertext[] = {
54 : : /* Outer protocol header */
55 : : 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56 : : /* Ethernet frame */
57 : : 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x06, 0x05,
58 : : 0x04, 0x03, 0x02, 0x01, 0xD6, 0xE2, 0x70, 0x5C,
59 : : 0xE6, 0x4D, 0xCC, 0x8C, 0x47, 0xB7, 0x09, 0xD6,
60 : : /* CRC */
61 : : 0x54, 0x85, 0xF8, 0x32
62 : : };
63 : :
64 : : static const uint8_t cipher_crc_cap_check_cipher_offset = 18;
65 : : static const uint8_t cipher_crc_cap_check_crc_offset = 6;
66 : : #endif
67 : :
68 : : struct qat_qp_hw_spec_funcs*
69 : : qat_qp_hw_spec[QAT_N_GENS];
70 : :
71 : : static int qat_qp_check_queue_alignment(uint64_t phys_addr,
72 : : uint32_t queue_size_bytes);
73 : : static void qat_queue_delete(struct qat_queue *queue);
74 : : static int qat_queue_create(struct qat_pci_device *qat_dev,
75 : : struct qat_queue *queue, struct qat_qp_config *, uint8_t dir);
76 : : static int adf_verify_queue_size(uint32_t msg_size, uint32_t msg_num,
77 : : uint32_t *queue_size_for_csr);
78 : : static int adf_configure_queues(struct qat_qp *queue,
79 : : enum qat_device_gen qat_dev_gen);
80 : : static int adf_queue_arb_enable(struct qat_pci_device *qat_dev,
81 : : struct qat_queue *txq, void *base_addr, rte_spinlock_t *lock);
82 : : static int adf_queue_arb_disable(enum qat_device_gen qat_dev_gen,
83 : : struct qat_queue *txq, void *base_addr, rte_spinlock_t *lock);
84 : : static int qat_qp_build_ring_base(struct qat_pci_device *qat_dev,
85 : : void *io_addr, struct qat_queue *queue);
86 : : static const struct rte_memzone *queue_dma_zone_reserve(const char *queue_name,
87 : : uint32_t queue_size, int socket_id);
88 : : static int qat_qp_csr_setup(struct qat_pci_device *qat_dev, void *io_addr,
89 : : struct qat_qp *qp);
90 : :
91 : : int
92 : 0 : qat_qp_setup(struct qat_pci_device *qat_dev,
93 : : struct qat_qp **qp_addr,
94 : : uint16_t queue_pair_id,
95 : : struct qat_qp_config *qat_qp_conf)
96 : : {
97 : : struct qat_qp *qp = NULL;
98 : 0 : struct rte_pci_device *pci_dev =
99 : 0 : qat_pci_devs[qat_dev->qat_dev_id].pci_dev;
100 : : char op_cookie_pool_name[RTE_RING_NAMESIZE];
101 : 0 : struct qat_dev_hw_spec_funcs *ops_hw =
102 : 0 : qat_dev_hw_spec[qat_dev->qat_dev_gen];
103 : : void *io_addr;
104 : : uint32_t i;
105 : :
106 : 0 : QAT_LOG(DEBUG, "Setup qp %u on qat pci device %d gen %d",
107 : : queue_pair_id, qat_dev->qat_dev_id, qat_dev->qat_dev_gen);
108 : :
109 [ # # ]: 0 : if ((qat_qp_conf->nb_descriptors > ADF_MAX_DESC) ||
110 : : (qat_qp_conf->nb_descriptors < ADF_MIN_DESC)) {
111 : 0 : QAT_LOG(ERR, "Can't create qp for %u descriptors",
112 : : qat_qp_conf->nb_descriptors);
113 : 0 : return -EINVAL;
114 : : }
115 : :
116 [ # # ]: 0 : if (ops_hw->qat_dev_get_transport_bar == NULL) {
117 : 0 : QAT_LOG(ERR,
118 : : "QAT Internal Error: qat_dev_get_transport_bar not set for gen %d",
119 : : qat_dev->qat_dev_gen);
120 : 0 : goto create_err;
121 : : }
122 : :
123 : 0 : io_addr = ops_hw->qat_dev_get_transport_bar(pci_dev)->addr;
124 [ # # ]: 0 : if (io_addr == NULL) {
125 : 0 : QAT_LOG(ERR, "Could not find VF config space "
126 : : "(UIO driver attached?).");
127 : 0 : return -EINVAL;
128 : : }
129 : :
130 : : /* Allocate the queue pair data structure. */
131 : 0 : qp = rte_zmalloc_socket("qat PMD qp metadata",
132 : : sizeof(*qp), RTE_CACHE_LINE_SIZE,
133 : : qat_qp_conf->socket_id);
134 [ # # ]: 0 : if (qp == NULL) {
135 : 0 : QAT_LOG(ERR, "Failed to alloc mem for qp struct");
136 : 0 : return -ENOMEM;
137 : : }
138 : 0 : qp->nb_descriptors = qat_qp_conf->nb_descriptors;
139 : 0 : qp->op_cookies = rte_zmalloc_socket("qat PMD op cookie pointer",
140 : 0 : qat_qp_conf->nb_descriptors * sizeof(*qp->op_cookies),
141 : : RTE_CACHE_LINE_SIZE, qat_qp_conf->socket_id);
142 [ # # ]: 0 : if (qp->op_cookies == NULL) {
143 : 0 : QAT_LOG(ERR, "Failed to alloc mem for cookie");
144 : 0 : rte_free(qp);
145 : 0 : return -ENOMEM;
146 : : }
147 : :
148 : 0 : qp->mmap_bar_addr = io_addr;
149 : 0 : qp->enqueued = qp->dequeued = 0;
150 : :
151 [ # # ]: 0 : if (qat_queue_create(qat_dev, &(qp->tx_q), qat_qp_conf,
152 : : ADF_RING_DIR_TX) != 0) {
153 : 0 : QAT_LOG(ERR, "Tx queue create failed "
154 : : "queue_pair_id=%u", queue_pair_id);
155 : 0 : goto create_err;
156 : : }
157 : :
158 : 0 : qp->max_inflights = ADF_MAX_INFLIGHTS(qp->tx_q.queue_size,
159 : : ADF_BYTES_TO_MSG_SIZE(qp->tx_q.msg_size));
160 : :
161 [ # # ]: 0 : if (qp->max_inflights < 2) {
162 : 0 : QAT_LOG(ERR, "Invalid num inflights");
163 : 0 : qat_queue_delete(&(qp->tx_q));
164 : 0 : goto create_err;
165 : : }
166 : :
167 [ # # ]: 0 : if (qat_queue_create(qat_dev, &(qp->rx_q), qat_qp_conf,
168 : : ADF_RING_DIR_RX) != 0) {
169 : 0 : QAT_LOG(ERR, "Rx queue create failed "
170 : : "queue_pair_id=%hu", queue_pair_id);
171 : 0 : qat_queue_delete(&(qp->tx_q));
172 : 0 : goto create_err;
173 : : }
174 : :
175 : 0 : snprintf(op_cookie_pool_name, RTE_RING_NAMESIZE,
176 : : "%s%d_cookies_%s_qp%hu",
177 : 0 : pci_dev->driver->driver.name, qat_dev->qat_dev_id,
178 : : qat_qp_conf->service_str, queue_pair_id);
179 : :
180 : 0 : QAT_LOG(DEBUG, "cookiepool: %s", op_cookie_pool_name);
181 : 0 : qp->op_cookie_pool = rte_mempool_lookup(op_cookie_pool_name);
182 [ # # ]: 0 : if (qp->op_cookie_pool == NULL)
183 : 0 : qp->op_cookie_pool = rte_mempool_create(op_cookie_pool_name,
184 : : qp->nb_descriptors,
185 : : qat_qp_conf->cookie_size, 64, 0,
186 : : NULL, NULL, NULL, NULL,
187 : : pci_dev->device.numa_node,
188 : : 0);
189 [ # # ]: 0 : if (!qp->op_cookie_pool) {
190 : 0 : QAT_LOG(ERR, "QAT PMD Cannot create"
191 : : " op mempool");
192 : 0 : qat_queue_delete(&(qp->tx_q));
193 : 0 : qat_queue_delete(&(qp->rx_q));
194 : 0 : goto create_err;
195 : : }
196 : :
197 [ # # ]: 0 : for (i = 0; i < qp->nb_descriptors; i++) {
198 [ # # # # ]: 0 : if (rte_mempool_get(qp->op_cookie_pool, &qp->op_cookies[i])) {
199 : 0 : QAT_LOG(ERR, "QAT PMD Cannot get op_cookie");
200 : 0 : goto create_err;
201 : : }
202 : 0 : memset(qp->op_cookies[i], 0, qat_qp_conf->cookie_size);
203 : : }
204 : :
205 : 0 : qp->qat_dev_gen = qat_dev->qat_dev_gen;
206 : 0 : qp->service_type = qat_qp_conf->hw->service_type;
207 : 0 : qp->qat_dev = qat_dev;
208 : :
209 : 0 : QAT_LOG(DEBUG, "QP setup complete: id: %d, cookiepool: %s",
210 : : queue_pair_id, op_cookie_pool_name);
211 : :
212 : : qat_qp_csr_setup(qat_dev, io_addr, qp);
213 : :
214 : 0 : *qp_addr = qp;
215 : 0 : return 0;
216 : :
217 : 0 : create_err:
218 : : if (qp) {
219 : 0 : rte_mempool_free(qp->op_cookie_pool);
220 : :
221 : 0 : rte_free(qp->op_cookies);
222 : :
223 : 0 : rte_free(qp);
224 : : }
225 : :
226 : : return -EFAULT;
227 : : }
228 : :
229 : : static int
230 : 0 : qat_queue_create(struct qat_pci_device *qat_dev, struct qat_queue *queue,
231 : : struct qat_qp_config *qp_conf, uint8_t dir)
232 : : {
233 : : const struct rte_memzone *qp_mz;
234 : 0 : struct rte_pci_device *pci_dev =
235 : 0 : qat_pci_devs[qat_dev->qat_dev_id].pci_dev;
236 : : int ret = 0;
237 [ # # ]: 0 : uint16_t desc_size = (dir == ADF_RING_DIR_TX ?
238 : 0 : qp_conf->hw->tx_msg_size : qp_conf->hw->rx_msg_size);
239 : 0 : uint32_t queue_size_bytes = (qp_conf->nb_descriptors)*(desc_size);
240 : :
241 : 0 : queue->hw_bundle_number = qp_conf->hw->hw_bundle_num;
242 [ # # ]: 0 : queue->hw_queue_number = (dir == ADF_RING_DIR_TX ?
243 : : qp_conf->hw->tx_ring_num : qp_conf->hw->rx_ring_num);
244 : :
245 [ # # ]: 0 : if (desc_size > ADF_MSG_SIZE_TO_BYTES(ADF_MAX_MSG_SIZE)) {
246 : 0 : QAT_LOG(ERR, "Invalid descriptor size %d", desc_size);
247 : 0 : return -EINVAL;
248 : : }
249 : :
250 : : /*
251 : : * Allocate a memzone for the queue - create a unique name.
252 : : */
253 : 0 : snprintf(queue->memz_name, sizeof(queue->memz_name),
254 : : "%s_%d_%s_%s_%d_%d",
255 : 0 : pci_dev->driver->driver.name, qat_dev->qat_dev_id,
256 : : qp_conf->service_str, "qp_mem",
257 : : queue->hw_bundle_number, queue->hw_queue_number);
258 : 0 : qp_mz = queue_dma_zone_reserve(queue->memz_name, queue_size_bytes,
259 : : pci_dev->device.numa_node);
260 [ # # ]: 0 : if (qp_mz == NULL) {
261 : 0 : QAT_LOG(ERR, "Failed to allocate ring memzone");
262 : 0 : return -ENOMEM;
263 : : }
264 : :
265 : 0 : queue->base_addr = (char *)qp_mz->addr;
266 : 0 : queue->base_phys_addr = qp_mz->iova;
267 : : if (qat_qp_check_queue_alignment(queue->base_phys_addr,
268 : : queue_size_bytes)) {
269 : 0 : QAT_LOG(ERR, "Invalid alignment on queue create "
270 : : " 0x%"PRIx64,
271 : : queue->base_phys_addr);
272 : : ret = -EFAULT;
273 : 0 : goto queue_create_err;
274 : : }
275 : :
276 [ # # ]: 0 : if (adf_verify_queue_size(desc_size, qp_conf->nb_descriptors,
277 : : &(queue->queue_size)) != 0) {
278 : 0 : QAT_LOG(ERR, "Invalid num inflights");
279 : : ret = -EINVAL;
280 : 0 : goto queue_create_err;
281 : : }
282 : :
283 : 0 : queue->modulo_mask = (1 << ADF_RING_SIZE_MODULO(queue->queue_size)) - 1;
284 : 0 : queue->head = 0;
285 : 0 : queue->tail = 0;
286 : 0 : queue->msg_size = desc_size;
287 : :
288 : : /* For fast calculation of cookie index, relies on msg_size being 2^n */
289 : 0 : queue->trailz = rte_ctz32(desc_size);
290 : :
291 : : /*
292 : : * Write an unused pattern to the queue memory.
293 : : */
294 : 0 : memset(queue->base_addr, 0x7F, queue_size_bytes);
295 : :
296 : 0 : QAT_LOG(DEBUG, "RING: Name:%s, size in CSR: %u, in bytes %u,"
297 : : " nb msgs %u, msg_size %u, modulo mask %u",
298 : : queue->memz_name,
299 : : queue->queue_size, queue_size_bytes,
300 : : qp_conf->nb_descriptors, desc_size,
301 : : queue->modulo_mask);
302 : :
303 : 0 : return 0;
304 : :
305 : 0 : queue_create_err:
306 : 0 : rte_memzone_free(qp_mz);
307 : 0 : return ret;
308 : : }
309 : :
310 : : static const struct rte_memzone *
311 : 0 : queue_dma_zone_reserve(const char *queue_name, uint32_t queue_size,
312 : : int socket_id)
313 : : {
314 : : const struct rte_memzone *mz;
315 : :
316 : 0 : mz = rte_memzone_lookup(queue_name);
317 [ # # ]: 0 : if (mz != 0) {
318 [ # # # # ]: 0 : if (((size_t)queue_size <= mz->len) &&
319 : 0 : ((socket_id == SOCKET_ID_ANY) ||
320 [ # # ]: 0 : (socket_id == mz->socket_id))) {
321 : 0 : QAT_LOG(DEBUG, "re-use memzone already "
322 : : "allocated for %s", queue_name);
323 : 0 : return mz;
324 : : }
325 : :
326 : 0 : QAT_LOG(ERR, "Incompatible memzone already "
327 : : "allocated %s, size %u, socket %d. "
328 : : "Requested size %u, socket %u",
329 : : queue_name, (uint32_t)mz->len,
330 : : mz->socket_id, queue_size, socket_id);
331 : 0 : return NULL;
332 : : }
333 : :
334 : 0 : QAT_LOG(DEBUG, "Allocate memzone for %s, size %u on socket %u",
335 : : queue_name, queue_size, socket_id);
336 : 0 : return rte_memzone_reserve_aligned(queue_name, queue_size,
337 : : socket_id, RTE_MEMZONE_IOVA_CONTIG, queue_size);
338 : : }
339 : :
340 : : int
341 : 0 : qat_qp_release(enum qat_device_gen qat_dev_gen, struct qat_qp **qp_addr)
342 : : {
343 : : int ret;
344 : 0 : struct qat_qp *qp = *qp_addr;
345 : : uint32_t i;
346 : :
347 [ # # ]: 0 : if (qp == NULL) {
348 : 0 : QAT_LOG(DEBUG, "qp already freed");
349 : 0 : return 0;
350 : : }
351 : :
352 : 0 : QAT_LOG(DEBUG, "Free qp on qat_pci device %d",
353 : : qp->qat_dev->qat_dev_id);
354 : :
355 : : /* Don't free memory if there are still responses to be processed */
356 [ # # ]: 0 : if ((qp->enqueued - qp->dequeued) == 0) {
357 : 0 : qat_queue_delete(&(qp->tx_q));
358 : 0 : qat_queue_delete(&(qp->rx_q));
359 : : } else {
360 : : return -EAGAIN;
361 : : }
362 : :
363 : 0 : ret = adf_queue_arb_disable(qat_dev_gen, &(qp->tx_q),
364 : 0 : qp->mmap_bar_addr, &qp->qat_dev->arb_csr_lock);
365 : : if (ret)
366 : : return ret;
367 : :
368 [ # # ]: 0 : for (i = 0; i < qp->nb_descriptors; i++)
369 [ # # ]: 0 : rte_mempool_put(qp->op_cookie_pool, qp->op_cookies[i]);
370 : :
371 : 0 : rte_mempool_free(qp->op_cookie_pool);
372 : :
373 : 0 : rte_free(qp->op_cookies);
374 : 0 : rte_free(qp);
375 : 0 : *qp_addr = NULL;
376 : 0 : return 0;
377 : : }
378 : :
379 : :
380 : : static void
381 : 0 : qat_queue_delete(struct qat_queue *queue)
382 : : {
383 : : const struct rte_memzone *mz;
384 : : int status = 0;
385 : :
386 [ # # ]: 0 : if (queue == NULL) {
387 : 0 : QAT_LOG(DEBUG, "Invalid queue");
388 : 0 : return;
389 : : }
390 : 0 : QAT_LOG(DEBUG, "Free ring %d, memzone: %s",
391 : : queue->hw_queue_number, queue->memz_name);
392 : :
393 : 0 : mz = rte_memzone_lookup(queue->memz_name);
394 [ # # ]: 0 : if (mz != NULL) {
395 : : /* Write an unused pattern to the queue memory. */
396 : 0 : memset(queue->base_addr, 0x7F, queue->queue_size);
397 : 0 : status = rte_memzone_free(mz);
398 [ # # ]: 0 : if (status != 0)
399 : 0 : QAT_LOG(ERR, "Error %d on freeing queue %s",
400 : : status, queue->memz_name);
401 : : } else {
402 : 0 : QAT_LOG(DEBUG, "queue %s doesn't exist",
403 : : queue->memz_name);
404 : : }
405 : : }
406 : :
407 : : static int __rte_unused
408 : : adf_queue_arb_enable(struct qat_pci_device *qat_dev, struct qat_queue *txq,
409 : : void *base_addr, rte_spinlock_t *lock)
410 : : {
411 : : struct qat_qp_hw_spec_funcs *ops =
412 : : qat_qp_hw_spec[qat_dev->qat_dev_gen];
413 : :
414 : : if (ops->qat_qp_adf_arb_enable == NULL)
415 : : return -ENOTSUP;
416 : : ops->qat_qp_adf_arb_enable(txq, base_addr, lock);
417 : : return 0;
418 : : }
419 : :
420 : : static int
421 : : adf_queue_arb_disable(enum qat_device_gen qat_dev_gen, struct qat_queue *txq,
422 : : void *base_addr, rte_spinlock_t *lock)
423 : : {
424 : 0 : struct qat_qp_hw_spec_funcs *ops =
425 : : qat_qp_hw_spec[qat_dev_gen];
426 : :
427 [ # # ]: 0 : if (ops->qat_qp_adf_arb_disable == NULL)
428 : : return -ENOTSUP;
429 : 0 : ops->qat_qp_adf_arb_disable(txq, base_addr, lock);
430 : : return 0;
431 : : }
432 : :
433 : : static int __rte_unused
434 : : qat_qp_build_ring_base(struct qat_pci_device *qat_dev, void *io_addr,
435 : : struct qat_queue *queue)
436 : : {
437 : : struct qat_qp_hw_spec_funcs *ops =
438 : : qat_qp_hw_spec[qat_dev->qat_dev_gen];
439 : :
440 : : if (ops->qat_qp_build_ring_base == NULL)
441 : : return -ENOTSUP;
442 : : ops->qat_qp_build_ring_base(io_addr, queue);
443 : : return 0;
444 : : }
445 : :
446 : : int
447 : 0 : qat_qps_per_service(struct qat_pci_device *qat_dev,
448 : : enum qat_service_type service)
449 : : {
450 : 0 : struct qat_qp_hw_spec_funcs *ops =
451 : 0 : qat_qp_hw_spec[qat_dev->qat_dev_gen];
452 : :
453 [ # # ]: 0 : if (ops->qat_qp_rings_per_service == NULL)
454 : : return -ENOTSUP;
455 : 0 : return ops->qat_qp_rings_per_service(qat_dev, service);
456 : : }
457 : :
458 : : const struct qat_qp_hw_data *
459 : 0 : qat_qp_get_hw_data(struct qat_pci_device *qat_dev,
460 : : enum qat_service_type service, uint16_t qp_id)
461 : : {
462 : 0 : struct qat_qp_hw_spec_funcs *ops =
463 : 0 : qat_qp_hw_spec[qat_dev->qat_dev_gen];
464 : :
465 [ # # ]: 0 : if (ops->qat_qp_get_hw_data == NULL)
466 : : return NULL;
467 : 0 : return ops->qat_qp_get_hw_data(qat_dev, service, qp_id);
468 : : }
469 : :
470 : : int
471 : 0 : qat_read_qp_config(struct qat_pci_device *qat_dev)
472 : : {
473 : 0 : struct qat_dev_hw_spec_funcs *ops_hw =
474 : 0 : qat_dev_hw_spec[qat_dev->qat_dev_gen];
475 : :
476 [ # # ]: 0 : if (ops_hw->qat_dev_read_config == NULL)
477 : : return -ENOTSUP;
478 : 0 : return ops_hw->qat_dev_read_config(qat_dev);
479 : : }
480 : :
481 : : static int __rte_unused
482 : : adf_configure_queues(struct qat_qp *qp, enum qat_device_gen qat_dev_gen)
483 : : {
484 : : struct qat_qp_hw_spec_funcs *ops =
485 : : qat_qp_hw_spec[qat_dev_gen];
486 : :
487 : : if (ops->qat_qp_adf_configure_queues == NULL)
488 : : return -ENOTSUP;
489 : : ops->qat_qp_adf_configure_queues(qp);
490 : : return 0;
491 : : }
492 : :
493 : : static inline void
494 : : qat_qp_csr_write_head(enum qat_device_gen qat_dev_gen, struct qat_qp *qp,
495 : : struct qat_queue *q, uint32_t new_head)
496 : : {
497 : 0 : struct qat_qp_hw_spec_funcs *ops =
498 : : qat_qp_hw_spec[qat_dev_gen];
499 : :
500 : : /*
501 : : * Pointer check should be done during
502 : : * initialization
503 : : */
504 : 0 : ops->qat_qp_csr_write_head(qp, q, new_head);
505 : : }
506 : :
507 : : static int
508 : : qat_qp_csr_setup(struct qat_pci_device *qat_dev,
509 : : void *io_addr, struct qat_qp *qp)
510 : : {
511 : 0 : struct qat_qp_hw_spec_funcs *ops =
512 : 0 : qat_qp_hw_spec[qat_dev->qat_dev_gen];
513 : :
514 [ # # ]: 0 : if (ops->qat_qp_csr_setup == NULL)
515 : : return -ENOTSUP;
516 : 0 : ops->qat_qp_csr_setup(qat_dev, io_addr, qp);
517 : 0 : return 0;
518 : : }
519 : :
520 : :
521 : : static inline
522 : 0 : void rxq_free_desc(enum qat_device_gen qat_dev_gen, struct qat_qp *qp,
523 : : struct qat_queue *q)
524 : : {
525 : : uint32_t old_head, new_head;
526 : : uint32_t max_head;
527 : :
528 : 0 : old_head = q->csr_head;
529 : 0 : new_head = q->head;
530 : 0 : max_head = qp->nb_descriptors * q->msg_size;
531 : :
532 : : /* write out free descriptors */
533 : 0 : void *cur_desc = (uint8_t *)q->base_addr + old_head;
534 : :
535 [ # # ]: 0 : if (new_head < old_head) {
536 : 0 : memset(cur_desc, ADF_RING_EMPTY_SIG_BYTE, max_head - old_head);
537 : 0 : memset(q->base_addr, ADF_RING_EMPTY_SIG_BYTE, new_head);
538 : : } else {
539 : 0 : memset(cur_desc, ADF_RING_EMPTY_SIG_BYTE, new_head - old_head);
540 : : }
541 : 0 : q->nb_processed_responses = 0;
542 : 0 : q->csr_head = new_head;
543 : :
544 : : qat_qp_csr_write_head(qat_dev_gen, qp, q, new_head);
545 : 0 : }
546 : :
547 : : static int
548 : : qat_qp_check_queue_alignment(uint64_t phys_addr, uint32_t queue_size_bytes)
549 : : {
550 [ # # ]: 0 : if (((queue_size_bytes - 1) & phys_addr) != 0)
551 : : return -EINVAL;
552 : : return 0;
553 : : }
554 : :
555 : : static int
556 : 0 : adf_verify_queue_size(uint32_t msg_size, uint32_t msg_num,
557 : : uint32_t *p_queue_size_for_csr)
558 : : {
559 : : uint8_t i = ADF_MIN_RING_SIZE;
560 : :
561 [ # # ]: 0 : for (; i <= ADF_MAX_RING_SIZE; i++)
562 : 0 : if ((msg_size * msg_num) ==
563 [ # # ]: 0 : (uint32_t)ADF_SIZE_TO_RING_SIZE_IN_BYTES(i)) {
564 : 0 : *p_queue_size_for_csr = i;
565 : 0 : return 0;
566 : : }
567 : 0 : QAT_LOG(ERR, "Invalid ring size %d", msg_size * msg_num);
568 : 0 : return -EINVAL;
569 : : }
570 : :
571 : : static inline uint32_t
572 : : adf_modulo(uint32_t data, uint32_t modulo_mask)
573 : : {
574 : 0 : return data & modulo_mask;
575 : : }
576 : :
577 : : uint16_t
578 : 0 : qat_enqueue_op_burst(void *qp, qat_op_build_request_t op_build_request,
579 : : void **ops, uint16_t nb_ops)
580 : : {
581 : : register struct qat_queue *queue;
582 : : struct qat_qp *tmp_qp = (struct qat_qp *)qp;
583 : : register uint32_t nb_ops_sent = 0;
584 : : register int ret = -1;
585 : : uint16_t nb_ops_possible = nb_ops;
586 : : register uint8_t *base_addr;
587 : : register uint32_t tail;
588 : :
589 [ # # ]: 0 : if (unlikely(nb_ops == 0))
590 : : return 0;
591 : :
592 : : /* read params used a lot in main loop into registers */
593 : 0 : queue = &(tmp_qp->tx_q);
594 : 0 : base_addr = (uint8_t *)queue->base_addr;
595 : 0 : tail = queue->tail;
596 : :
597 : : /* Find how many can actually fit on the ring */
598 : : {
599 : : /* dequeued can only be written by one thread, but it may not
600 : : * be this thread. As it's 4-byte aligned it will be read
601 : : * atomically here by any Intel CPU.
602 : : * enqueued can wrap before dequeued, but cannot
603 : : * lap it as var size of enq/deq (uint32_t) > var size of
604 : : * max_inflights (uint16_t). In reality inflights is never
605 : : * even as big as max uint16_t, as it's <= ADF_MAX_DESC.
606 : : * On wrapping, the calculation still returns the correct
607 : : * positive value as all three vars are unsigned.
608 : : */
609 : 0 : uint32_t inflights =
610 : 0 : tmp_qp->enqueued - tmp_qp->dequeued;
611 : :
612 [ # # ]: 0 : if ((inflights + nb_ops) > tmp_qp->max_inflights) {
613 : 0 : nb_ops_possible = tmp_qp->max_inflights - inflights;
614 [ # # ]: 0 : if (nb_ops_possible == 0)
615 : : return 0;
616 : : }
617 : : /* QAT has plenty of work queued already, so don't waste cycles
618 : : * enqueueing, wait til the application has gathered a bigger
619 : : * burst or some completed ops have been dequeued
620 : : */
621 [ # # # # ]: 0 : if (tmp_qp->min_enq_burst_threshold && inflights >
622 [ # # ]: 0 : QAT_QP_MIN_INFL_THRESHOLD && nb_ops_possible <
623 : : tmp_qp->min_enq_burst_threshold) {
624 : 0 : tmp_qp->stats.threshold_hit_count++;
625 : 0 : return 0;
626 : : }
627 : : }
628 : :
629 [ # # ]: 0 : if (tmp_qp->service_type == QAT_SERVICE_SYMMETRIC)
630 : 0 : qat_sym_preprocess_requests(ops, nb_ops_possible);
631 : :
632 : 0 : memset(tmp_qp->opaque, 0xff, sizeof(tmp_qp->opaque));
633 : :
634 [ # # ]: 0 : while (nb_ops_sent != nb_ops_possible) {
635 : 0 : ret = op_build_request(*ops, base_addr + tail,
636 : 0 : tmp_qp->op_cookies[tail >> queue->trailz],
637 : : tmp_qp);
638 : :
639 [ # # ]: 0 : if (ret != 0) {
640 : 0 : tmp_qp->stats.enqueue_err_count++;
641 : : /* This message cannot be enqueued */
642 [ # # ]: 0 : if (nb_ops_sent == 0)
643 : : return 0;
644 : 0 : goto kick_tail;
645 : : }
646 : :
647 : 0 : tail = adf_modulo(tail + queue->msg_size, queue->modulo_mask);
648 : 0 : ops++;
649 : 0 : nb_ops_sent++;
650 : : }
651 : 0 : kick_tail:
652 : 0 : queue->tail = tail;
653 : 0 : tmp_qp->enqueued += nb_ops_sent;
654 : 0 : tmp_qp->stats.enqueued_count += nb_ops_sent;
655 : 0 : txq_write_tail(tmp_qp->qat_dev_gen, tmp_qp, queue);
656 : 0 : return nb_ops_sent;
657 : : }
658 : :
659 : : uint16_t
660 : 0 : qat_dequeue_op_burst(void *qp, void **ops,
661 : : qat_op_dequeue_t qat_dequeue_process_response, uint16_t nb_ops)
662 : : {
663 : : struct qat_queue *rx_queue;
664 : : struct qat_qp *tmp_qp = (struct qat_qp *)qp;
665 : : uint32_t head;
666 : : uint32_t op_resp_counter = 0, fw_resp_counter = 0;
667 : : uint8_t *resp_msg;
668 : : int nb_fw_responses;
669 : :
670 : 0 : rx_queue = &(tmp_qp->rx_q);
671 : 0 : head = rx_queue->head;
672 : 0 : resp_msg = (uint8_t *)rx_queue->base_addr + rx_queue->head;
673 : :
674 [ # # ]: 0 : while (*(uint32_t *)resp_msg != ADF_RING_EMPTY_SIG &&
675 [ # # ]: 0 : op_resp_counter != nb_ops) {
676 : :
677 : : nb_fw_responses = 1;
678 : :
679 : 0 : nb_fw_responses = qat_dequeue_process_response(
680 : : ops, resp_msg,
681 : 0 : tmp_qp->op_cookies[head >> rx_queue->trailz],
682 : : &tmp_qp->stats.dequeue_err_count);
683 : :
684 : 0 : head = adf_modulo(head + rx_queue->msg_size,
685 : : rx_queue->modulo_mask);
686 : :
687 : 0 : resp_msg = (uint8_t *)rx_queue->base_addr + head;
688 : :
689 [ # # ]: 0 : if (nb_fw_responses) {
690 : : /* only move on to next op if one was ready to return
691 : : * to API
692 : : */
693 : 0 : ops++;
694 : 0 : op_resp_counter++;
695 : : }
696 : :
697 : : /* A compression op may be broken up into multiple fw requests.
698 : : * Only count fw responses as complete once ALL the responses
699 : : * associated with an op have been processed, as the cookie
700 : : * data from the first response must be available until
701 : : * finished with all firmware responses.
702 : : */
703 : 0 : fw_resp_counter += nb_fw_responses;
704 : :
705 : 0 : rx_queue->nb_processed_responses++;
706 : : }
707 : :
708 : 0 : tmp_qp->dequeued += fw_resp_counter;
709 : 0 : tmp_qp->stats.dequeued_count += op_resp_counter;
710 : :
711 : 0 : rx_queue->head = head;
712 [ # # ]: 0 : if (rx_queue->nb_processed_responses > QAT_CSR_HEAD_WRITE_THRESH)
713 : 0 : rxq_free_desc(tmp_qp->qat_dev_gen, tmp_qp, rx_queue);
714 : :
715 : 0 : QAT_DP_LOG(DEBUG, "Dequeue burst return: %u, QAT responses: %u",
716 : : op_resp_counter, fw_resp_counter);
717 : :
718 : 0 : return op_resp_counter;
719 : : }
720 : :
721 : : /* This is almost same as dequeue_op_burst, without the atomic, without stats
722 : : * and without the op. Dequeues one response.
723 : : */
724 : : static uint8_t
725 : 0 : qat_cq_dequeue_response(struct qat_qp *qp, void *out_data)
726 : : {
727 : : uint8_t result = 0;
728 : : uint8_t retries = 0;
729 : 0 : struct qat_queue *queue = &(qp->rx_q);
730 : 0 : struct icp_qat_fw_comn_resp *resp_msg = (struct icp_qat_fw_comn_resp *)
731 : 0 : ((uint8_t *)queue->base_addr + queue->head);
732 : :
733 [ # # ]: 0 : while (retries++ < QAT_CQ_MAX_DEQ_RETRIES &&
734 [ # # ]: 0 : *(uint32_t *)resp_msg == ADF_RING_EMPTY_SIG) {
735 : : /* loop waiting for response until we reach the timeout */
736 : : rte_delay_ms(20);
737 : : }
738 : :
739 [ # # ]: 0 : if (*(uint32_t *)resp_msg != ADF_RING_EMPTY_SIG) {
740 : : /* response received */
741 : : result = 1;
742 : :
743 : : /* check status flag */
744 [ # # ]: 0 : if (ICP_QAT_FW_COMN_RESP_CRYPTO_STAT_GET(
745 : : resp_msg->comn_hdr.comn_status) ==
746 : : ICP_QAT_FW_COMN_STATUS_FLAG_OK) {
747 : : /* success */
748 : 0 : memcpy(out_data, resp_msg, queue->msg_size);
749 : : } else {
750 : 0 : memset(out_data, 0, queue->msg_size);
751 : : }
752 : :
753 : 0 : queue->head = adf_modulo(queue->head + queue->msg_size,
754 : : queue->modulo_mask);
755 : 0 : rxq_free_desc(qp->qat_dev_gen, qp, queue);
756 : : }
757 : :
758 : 0 : return result;
759 : : }
760 : :
761 : : /* Sends a NULL message and extracts QAT fw version from the response.
762 : : * Used to determine detailed capabilities based on the fw version number.
763 : : * This assumes that there are no inflight messages, i.e. assumes there's space
764 : : * on the qp, one message is sent and only one response collected.
765 : : * Returns fw version number or 0 for unknown version or a negative error code.
766 : : */
767 : : int
768 : 0 : qat_cq_get_fw_version(struct qat_qp *qp)
769 : : {
770 : 0 : struct qat_queue *queue = &(qp->tx_q);
771 : 0 : uint8_t *base_addr = (uint8_t *)queue->base_addr;
772 : : struct icp_qat_fw_comn_req null_msg;
773 : : struct icp_qat_fw_comn_resp response;
774 : :
775 : : /* prepare the NULL request */
776 : : memset(&null_msg, 0, sizeof(null_msg));
777 : 0 : null_msg.comn_hdr.hdr_flags =
778 : : ICP_QAT_FW_COMN_HDR_FLAGS_BUILD(ICP_QAT_FW_COMN_REQ_FLAG_SET);
779 : : null_msg.comn_hdr.service_type = ICP_QAT_FW_COMN_REQ_NULL;
780 : 0 : null_msg.comn_hdr.service_cmd_id = ICP_QAT_FW_NULL_REQ_SERV_ID;
781 : :
782 : : #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
783 : : QAT_DP_HEXDUMP_LOG(DEBUG, "NULL request", &null_msg, sizeof(null_msg));
784 : : #endif
785 : :
786 : : /* send the NULL request */
787 : 0 : memcpy(base_addr + queue->tail, &null_msg, sizeof(null_msg));
788 : 0 : queue->tail = adf_modulo(queue->tail + queue->msg_size,
789 : : queue->modulo_mask);
790 : 0 : txq_write_tail(qp->qat_dev_gen, qp, queue);
791 : :
792 : : /* receive a response */
793 [ # # ]: 0 : if (qat_cq_dequeue_response(qp, &response)) {
794 : :
795 : : #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
796 : : QAT_DP_HEXDUMP_LOG(DEBUG, "NULL response:", &response,
797 : : sizeof(response));
798 : : #endif
799 : : /* if LW0 bit 24 is set - then the fw version was returned */
800 [ # # ]: 0 : if (QAT_FIELD_GET(response.comn_hdr.hdr_flags,
801 : : ICP_QAT_FW_COMN_NULL_VERSION_FLAG_BITPOS,
802 : : ICP_QAT_FW_COMN_NULL_VERSION_FLAG_MASK))
803 : 0 : return response.resrvd[0]; /* return LW4 */
804 : : else
805 : : return 0; /* not set - we don't know fw version */
806 : : }
807 : :
808 : 0 : QAT_LOG(ERR, "No response received");
809 : 0 : return -EINVAL;
810 : : }
811 : :
812 : : #ifdef BUILD_QAT_SYM
813 : : /* Sends an LA bulk req message to determine if a QAT device supports Cipher-CRC
814 : : * offload. This assumes that there are no inflight messages, i.e. assumes
815 : : * there's space on the qp, one message is sent and only one response
816 : : * collected. The status bit of the response and returned data are checked.
817 : : * Returns:
818 : : * 1 if status bit indicates success and returned data matches expected
819 : : * data (i.e. Cipher-CRC supported)
820 : : * 0 if status bit indicates error or returned data does not match expected
821 : : * data (i.e. Cipher-CRC not supported)
822 : : * Negative error code in case of error
823 : : */
824 : : int
825 : 0 : qat_cq_get_fw_cipher_crc_cap(struct qat_qp *qp)
826 : : {
827 : 0 : struct qat_queue *queue = &(qp->tx_q);
828 : 0 : uint8_t *base_addr = (uint8_t *)queue->base_addr;
829 : 0 : struct icp_qat_fw_la_bulk_req cipher_crc_cap_msg = {{0}};
830 : 0 : struct icp_qat_fw_comn_resp response = {{0}};
831 : : struct icp_qat_fw_la_cipher_req_params *cipher_param;
832 : : struct icp_qat_fw_la_auth_req_params *auth_param;
833 : : struct qat_sym_session *session;
834 : : phys_addr_t phy_src_addr;
835 : : uint64_t *src_data_addr;
836 : : int ret;
837 : :
838 : 0 : session = rte_zmalloc(NULL, sizeof(struct qat_sym_session), 0);
839 [ # # ]: 0 : if (session == NULL)
840 : : return -EINVAL;
841 : :
842 : : /* Verify the session physical address is known */
843 : 0 : rte_iova_t session_paddr = rte_mem_virt2iova(session);
844 [ # # ]: 0 : if (session_paddr == 0 || session_paddr == RTE_BAD_IOVA) {
845 : 0 : QAT_LOG(ERR, "Session physical address unknown.");
846 : 0 : return -EINVAL;
847 : : }
848 : :
849 : : /* Prepare the LA bulk request */
850 : 0 : ret = qat_cipher_crc_cap_msg_sess_prepare(session,
851 : : session_paddr,
852 : : cipher_crc_cap_check_key,
853 : : sizeof(cipher_crc_cap_check_key),
854 : : qp->qat_dev_gen);
855 [ # # ]: 0 : if (ret < 0) {
856 : 0 : rte_free(session);
857 : : /* Returning 0 here to allow qp setup to continue, but
858 : : * indicate that Cipher-CRC offload is not supported on the
859 : : * device
860 : : */
861 : 0 : return 0;
862 : : }
863 : :
864 : 0 : cipher_crc_cap_msg = session->fw_req;
865 : :
866 : 0 : src_data_addr = rte_zmalloc(NULL,
867 : : sizeof(cipher_crc_cap_check_plaintext),
868 : : 0);
869 [ # # ]: 0 : if (src_data_addr == NULL) {
870 : 0 : rte_free(session);
871 : 0 : return -EINVAL;
872 : : }
873 : :
874 : : rte_memcpy(src_data_addr,
875 : : cipher_crc_cap_check_plaintext,
876 : : sizeof(cipher_crc_cap_check_plaintext));
877 : :
878 : 0 : phy_src_addr = rte_mem_virt2iova(src_data_addr);
879 [ # # ]: 0 : if (phy_src_addr == 0 || phy_src_addr == RTE_BAD_IOVA) {
880 : 0 : QAT_LOG(ERR, "Source physical address unknown.");
881 : 0 : return -EINVAL;
882 : : }
883 : :
884 : 0 : cipher_crc_cap_msg.comn_mid.src_data_addr = phy_src_addr;
885 : 0 : cipher_crc_cap_msg.comn_mid.src_length =
886 : : sizeof(cipher_crc_cap_check_plaintext);
887 : 0 : cipher_crc_cap_msg.comn_mid.dest_data_addr = phy_src_addr;
888 [ # # ]: 0 : cipher_crc_cap_msg.comn_mid.dst_length =
889 : : sizeof(cipher_crc_cap_check_plaintext);
890 : :
891 : : cipher_param = (void *)&cipher_crc_cap_msg.serv_specif_rqpars;
892 : : auth_param = (void *)((uint8_t *)cipher_param +
893 : : ICP_QAT_FW_HASH_REQUEST_PARAMETERS_OFFSET);
894 : :
895 : : rte_memcpy(cipher_param->u.cipher_IV_array,
896 : : cipher_crc_cap_check_iv,
897 : : sizeof(cipher_crc_cap_check_iv));
898 : :
899 : 0 : cipher_param->cipher_offset = cipher_crc_cap_check_cipher_offset;
900 : 0 : cipher_param->cipher_length =
901 : : sizeof(cipher_crc_cap_check_plaintext) -
902 : : cipher_crc_cap_check_cipher_offset;
903 : 0 : auth_param->auth_off = cipher_crc_cap_check_crc_offset;
904 : 0 : auth_param->auth_len = sizeof(cipher_crc_cap_check_plaintext) -
905 : : cipher_crc_cap_check_crc_offset -
906 : : RTE_ETHER_CRC_LEN;
907 : :
908 : 0 : ICP_QAT_FW_LA_DIGEST_IN_BUFFER_SET(
909 : : cipher_crc_cap_msg.comn_hdr.serv_specif_flags,
910 : : ICP_QAT_FW_LA_DIGEST_IN_BUFFER);
911 : :
912 : : #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
913 : : QAT_DP_HEXDUMP_LOG(DEBUG, "LA Bulk request", &cipher_crc_cap_msg,
914 : : sizeof(cipher_crc_cap_msg));
915 : : #endif
916 : :
917 : : /* Send the cipher_crc_cap_msg request */
918 : 0 : memcpy(base_addr + queue->tail,
919 : : &cipher_crc_cap_msg,
920 : : sizeof(cipher_crc_cap_msg));
921 : 0 : queue->tail = adf_modulo(queue->tail + queue->msg_size,
922 : : queue->modulo_mask);
923 : 0 : txq_write_tail(qp->qat_dev_gen, qp, queue);
924 : :
925 : : /* Check for response and verify data is same as ciphertext */
926 [ # # ]: 0 : if (qat_cq_dequeue_response(qp, &response)) {
927 : : #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
928 : : QAT_DP_HEXDUMP_LOG(DEBUG, "LA response:", &response,
929 : : sizeof(response));
930 : : #endif
931 : :
932 [ # # ]: 0 : if (memcmp(src_data_addr,
933 : : cipher_crc_cap_check_ciphertext,
934 : : sizeof(cipher_crc_cap_check_ciphertext)) != 0)
935 : : ret = 0; /* Cipher-CRC offload not supported */
936 : : else
937 : : ret = 1;
938 : : } else {
939 : : ret = -EINVAL;
940 : : }
941 : :
942 : 0 : rte_free(src_data_addr);
943 : 0 : rte_free(session);
944 : 0 : return ret;
945 : : }
946 : : #endif
|