xref: /dpdk/drivers/common/qat/qat_qp.c (revision f665790a5dbad7b645ff46f31d65e977324e7bfc)
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 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 	struct rte_pci_device *pci_dev =
99 			qat_pci_devs[qat_dev->qat_dev_id].pci_dev;
100 	char op_cookie_pool_name[RTE_RING_NAMESIZE];
101 	struct qat_dev_hw_spec_funcs *ops_hw =
102 		qat_dev_hw_spec[qat_dev->qat_dev_gen];
103 	void *io_addr;
104 	uint32_t i;
105 
106 	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 	if ((qat_qp_conf->nb_descriptors > ADF_MAX_DESC) ||
110 		(qat_qp_conf->nb_descriptors < ADF_MIN_DESC)) {
111 		QAT_LOG(ERR, "Can't create qp for %u descriptors",
112 				qat_qp_conf->nb_descriptors);
113 		return -EINVAL;
114 	}
115 
116 	if (ops_hw->qat_dev_get_transport_bar == NULL)	{
117 		QAT_LOG(ERR,
118 			"QAT Internal Error: qat_dev_get_transport_bar not set for gen %d",
119 			qat_dev->qat_dev_gen);
120 		goto create_err;
121 	}
122 
123 	io_addr = ops_hw->qat_dev_get_transport_bar(pci_dev)->addr;
124 	if (io_addr == NULL) {
125 		QAT_LOG(ERR, "Could not find VF config space "
126 				"(UIO driver attached?).");
127 		return -EINVAL;
128 	}
129 
130 	/* Allocate the queue pair data structure. */
131 	qp = rte_zmalloc_socket("qat PMD qp metadata",
132 				sizeof(*qp), RTE_CACHE_LINE_SIZE,
133 				qat_qp_conf->socket_id);
134 	if (qp == NULL) {
135 		QAT_LOG(ERR, "Failed to alloc mem for qp struct");
136 		return -ENOMEM;
137 	}
138 	qp->nb_descriptors = qat_qp_conf->nb_descriptors;
139 	qp->op_cookies = rte_zmalloc_socket("qat PMD op cookie pointer",
140 			qat_qp_conf->nb_descriptors * sizeof(*qp->op_cookies),
141 			RTE_CACHE_LINE_SIZE, qat_qp_conf->socket_id);
142 	if (qp->op_cookies == NULL) {
143 		QAT_LOG(ERR, "Failed to alloc mem for cookie");
144 		rte_free(qp);
145 		return -ENOMEM;
146 	}
147 
148 	qp->mmap_bar_addr = io_addr;
149 	qp->enqueued = qp->dequeued = 0;
150 
151 	if (qat_queue_create(qat_dev, &(qp->tx_q), qat_qp_conf,
152 					ADF_RING_DIR_TX) != 0) {
153 		QAT_LOG(ERR, "Tx queue create failed "
154 				"queue_pair_id=%u", queue_pair_id);
155 		goto create_err;
156 	}
157 
158 	qp->max_inflights = ADF_MAX_INFLIGHTS(qp->tx_q.queue_size,
159 				ADF_BYTES_TO_MSG_SIZE(qp->tx_q.msg_size));
160 
161 	if (qp->max_inflights < 2) {
162 		QAT_LOG(ERR, "Invalid num inflights");
163 		qat_queue_delete(&(qp->tx_q));
164 		goto create_err;
165 	}
166 
167 	if (qat_queue_create(qat_dev, &(qp->rx_q), qat_qp_conf,
168 					ADF_RING_DIR_RX) != 0) {
169 		QAT_LOG(ERR, "Rx queue create failed "
170 				"queue_pair_id=%hu", queue_pair_id);
171 		qat_queue_delete(&(qp->tx_q));
172 		goto create_err;
173 	}
174 
175 	snprintf(op_cookie_pool_name, RTE_RING_NAMESIZE,
176 					"%s%d_cookies_%s_qp%hu",
177 		pci_dev->driver->driver.name, qat_dev->qat_dev_id,
178 		qat_qp_conf->service_str, queue_pair_id);
179 
180 	QAT_LOG(DEBUG, "cookiepool: %s", op_cookie_pool_name);
181 	qp->op_cookie_pool = rte_mempool_lookup(op_cookie_pool_name);
182 	if (qp->op_cookie_pool == NULL)
183 		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 	if (!qp->op_cookie_pool) {
190 		QAT_LOG(ERR, "QAT PMD Cannot create"
191 				" op mempool");
192 		qat_queue_delete(&(qp->tx_q));
193 		qat_queue_delete(&(qp->rx_q));
194 		goto create_err;
195 	}
196 
197 	for (i = 0; i < qp->nb_descriptors; i++) {
198 		if (rte_mempool_get(qp->op_cookie_pool, &qp->op_cookies[i])) {
199 			QAT_LOG(ERR, "QAT PMD Cannot get op_cookie");
200 			goto create_err;
201 		}
202 		memset(qp->op_cookies[i], 0, qat_qp_conf->cookie_size);
203 	}
204 
205 	qp->qat_dev_gen = qat_dev->qat_dev_gen;
206 	qp->service_type = qat_qp_conf->hw->service_type;
207 	qp->qat_dev = qat_dev;
208 
209 	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 	*qp_addr = qp;
215 	return 0;
216 
217 create_err:
218 	if (qp) {
219 		rte_mempool_free(qp->op_cookie_pool);
220 
221 		rte_free(qp->op_cookies);
222 
223 		rte_free(qp);
224 	}
225 
226 	return -EFAULT;
227 }
228 
229 static int
230 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 	struct rte_pci_device *pci_dev =
235 			qat_pci_devs[qat_dev->qat_dev_id].pci_dev;
236 	int ret = 0;
237 	uint16_t desc_size = (dir == ADF_RING_DIR_TX ?
238 			qp_conf->hw->tx_msg_size : qp_conf->hw->rx_msg_size);
239 	uint32_t queue_size_bytes = (qp_conf->nb_descriptors)*(desc_size);
240 
241 	queue->hw_bundle_number = qp_conf->hw->hw_bundle_num;
242 	queue->hw_queue_number = (dir == ADF_RING_DIR_TX ?
243 			qp_conf->hw->tx_ring_num : qp_conf->hw->rx_ring_num);
244 
245 	if (desc_size > ADF_MSG_SIZE_TO_BYTES(ADF_MAX_MSG_SIZE)) {
246 		QAT_LOG(ERR, "Invalid descriptor size %d", desc_size);
247 		return -EINVAL;
248 	}
249 
250 	/*
251 	 * Allocate a memzone for the queue - create a unique name.
252 	 */
253 	snprintf(queue->memz_name, sizeof(queue->memz_name),
254 			"%s_%d_%s_%s_%d_%d",
255 		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 	qp_mz = queue_dma_zone_reserve(queue->memz_name, queue_size_bytes,
259 			pci_dev->device.numa_node);
260 	if (qp_mz == NULL) {
261 		QAT_LOG(ERR, "Failed to allocate ring memzone");
262 		return -ENOMEM;
263 	}
264 
265 	queue->base_addr = (char *)qp_mz->addr;
266 	queue->base_phys_addr = qp_mz->iova;
267 	if (qat_qp_check_queue_alignment(queue->base_phys_addr,
268 			queue_size_bytes)) {
269 		QAT_LOG(ERR, "Invalid alignment on queue create "
270 					" 0x%"PRIx64,
271 					queue->base_phys_addr);
272 		ret = -EFAULT;
273 		goto queue_create_err;
274 	}
275 
276 	if (adf_verify_queue_size(desc_size, qp_conf->nb_descriptors,
277 			&(queue->queue_size)) != 0) {
278 		QAT_LOG(ERR, "Invalid num inflights");
279 		ret = -EINVAL;
280 		goto queue_create_err;
281 	}
282 
283 	queue->modulo_mask = (1 << ADF_RING_SIZE_MODULO(queue->queue_size)) - 1;
284 	queue->head = 0;
285 	queue->tail = 0;
286 	queue->msg_size = desc_size;
287 
288 	/* For fast calculation of cookie index, relies on msg_size being 2^n */
289 	queue->trailz = rte_ctz32(desc_size);
290 
291 	/*
292 	 * Write an unused pattern to the queue memory.
293 	 */
294 	memset(queue->base_addr, 0x7F, queue_size_bytes);
295 
296 	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 	return 0;
304 
305 queue_create_err:
306 	rte_memzone_free(qp_mz);
307 	return ret;
308 }
309 
310 static const struct rte_memzone *
311 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 	mz = rte_memzone_lookup(queue_name);
317 	if (mz != 0) {
318 		if (((size_t)queue_size <= mz->len) &&
319 				((socket_id == SOCKET_ID_ANY) ||
320 					(socket_id == mz->socket_id))) {
321 			QAT_LOG(DEBUG, "re-use memzone already "
322 					"allocated for %s", queue_name);
323 			return mz;
324 		}
325 
326 		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 		return NULL;
332 	}
333 
334 	QAT_LOG(DEBUG, "Allocate memzone for %s, size %u on socket %u",
335 					queue_name, queue_size, socket_id);
336 	return rte_memzone_reserve_aligned(queue_name, queue_size,
337 		socket_id, RTE_MEMZONE_IOVA_CONTIG, queue_size);
338 }
339 
340 int
341 qat_qp_release(enum qat_device_gen qat_dev_gen, struct qat_qp **qp_addr)
342 {
343 	int ret;
344 	struct qat_qp *qp = *qp_addr;
345 	uint32_t i;
346 
347 	if (qp == NULL) {
348 		QAT_LOG(DEBUG, "qp already freed");
349 		return 0;
350 	}
351 
352 	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 	if ((qp->enqueued - qp->dequeued) == 0) {
357 		qat_queue_delete(&(qp->tx_q));
358 		qat_queue_delete(&(qp->rx_q));
359 	} else {
360 		return -EAGAIN;
361 	}
362 
363 	ret = adf_queue_arb_disable(qat_dev_gen, &(qp->tx_q),
364 			qp->mmap_bar_addr, &qp->qat_dev->arb_csr_lock);
365 	if (ret)
366 		return ret;
367 
368 	for (i = 0; i < qp->nb_descriptors; i++)
369 		rte_mempool_put(qp->op_cookie_pool, qp->op_cookies[i]);
370 
371 	rte_mempool_free(qp->op_cookie_pool);
372 
373 	rte_free(qp->op_cookies);
374 	rte_free(qp);
375 	*qp_addr = NULL;
376 	return 0;
377 }
378 
379 
380 static void
381 qat_queue_delete(struct qat_queue *queue)
382 {
383 	const struct rte_memzone *mz;
384 	int status = 0;
385 
386 	if (queue == NULL) {
387 		QAT_LOG(DEBUG, "Invalid queue");
388 		return;
389 	}
390 	QAT_LOG(DEBUG, "Free ring %d, memzone: %s",
391 			queue->hw_queue_number, queue->memz_name);
392 
393 	mz = rte_memzone_lookup(queue->memz_name);
394 	if (mz != NULL)	{
395 		/* Write an unused pattern to the queue memory. */
396 		memset(queue->base_addr, 0x7F, queue->queue_size);
397 		status = rte_memzone_free(mz);
398 		if (status != 0)
399 			QAT_LOG(ERR, "Error %d on freeing queue %s",
400 					status, queue->memz_name);
401 	} else {
402 		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 	struct qat_qp_hw_spec_funcs *ops =
425 		qat_qp_hw_spec[qat_dev_gen];
426 
427 	if (ops->qat_qp_adf_arb_disable == NULL)
428 		return -ENOTSUP;
429 	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 qat_qps_per_service(struct qat_pci_device *qat_dev,
448 		enum qat_service_type service)
449 {
450 	struct qat_qp_hw_spec_funcs *ops =
451 		qat_qp_hw_spec[qat_dev->qat_dev_gen];
452 
453 	if (ops->qat_qp_rings_per_service == NULL)
454 		return -ENOTSUP;
455 	return ops->qat_qp_rings_per_service(qat_dev, service);
456 }
457 
458 const struct qat_qp_hw_data *
459 qat_qp_get_hw_data(struct qat_pci_device *qat_dev,
460 		enum qat_service_type service, uint16_t qp_id)
461 {
462 	struct qat_qp_hw_spec_funcs *ops =
463 		qat_qp_hw_spec[qat_dev->qat_dev_gen];
464 
465 	if (ops->qat_qp_get_hw_data == NULL)
466 		return NULL;
467 	return ops->qat_qp_get_hw_data(qat_dev, service, qp_id);
468 }
469 
470 int
471 qat_read_qp_config(struct qat_pci_device *qat_dev)
472 {
473 	struct qat_dev_hw_spec_funcs *ops_hw =
474 		qat_dev_hw_spec[qat_dev->qat_dev_gen];
475 
476 	if (ops_hw->qat_dev_read_config == NULL)
477 		return -ENOTSUP;
478 	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 	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 	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 	struct qat_qp_hw_spec_funcs *ops =
512 		qat_qp_hw_spec[qat_dev->qat_dev_gen];
513 
514 	if (ops->qat_qp_csr_setup == NULL)
515 		return -ENOTSUP;
516 	ops->qat_qp_csr_setup(qat_dev, io_addr, qp);
517 	return 0;
518 }
519 
520 
521 static inline
522 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 	old_head = q->csr_head;
529 	new_head = q->head;
530 	max_head = qp->nb_descriptors * q->msg_size;
531 
532 	/* write out free descriptors */
533 	void *cur_desc = (uint8_t *)q->base_addr + old_head;
534 
535 	if (new_head < old_head) {
536 		memset(cur_desc, ADF_RING_EMPTY_SIG_BYTE, max_head - old_head);
537 		memset(q->base_addr, ADF_RING_EMPTY_SIG_BYTE, new_head);
538 	} else {
539 		memset(cur_desc, ADF_RING_EMPTY_SIG_BYTE, new_head - old_head);
540 	}
541 	q->nb_processed_responses = 0;
542 	q->csr_head = new_head;
543 
544 	qat_qp_csr_write_head(qat_dev_gen, qp, q, new_head);
545 }
546 
547 static int
548 qat_qp_check_queue_alignment(uint64_t phys_addr, uint32_t queue_size_bytes)
549 {
550 	if (((queue_size_bytes - 1) & phys_addr) != 0)
551 		return -EINVAL;
552 	return 0;
553 }
554 
555 static int
556 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 	for (; i <= ADF_MAX_RING_SIZE; i++)
562 		if ((msg_size * msg_num) ==
563 				(uint32_t)ADF_SIZE_TO_RING_SIZE_IN_BYTES(i)) {
564 			*p_queue_size_for_csr = i;
565 			return 0;
566 		}
567 	QAT_LOG(ERR, "Invalid ring size %d", msg_size * msg_num);
568 	return -EINVAL;
569 }
570 
571 static inline uint32_t
572 adf_modulo(uint32_t data, uint32_t modulo_mask)
573 {
574 	return data & modulo_mask;
575 }
576 
577 uint16_t
578 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 	if (unlikely(nb_ops == 0))
590 		return 0;
591 
592 	/* read params used a lot in main loop into registers */
593 	queue = &(tmp_qp->tx_q);
594 	base_addr = (uint8_t *)queue->base_addr;
595 	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 		uint32_t inflights =
610 			tmp_qp->enqueued - tmp_qp->dequeued;
611 
612 		if ((inflights + nb_ops) > tmp_qp->max_inflights) {
613 			nb_ops_possible = tmp_qp->max_inflights - inflights;
614 			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 		if (tmp_qp->min_enq_burst_threshold && inflights >
622 				QAT_QP_MIN_INFL_THRESHOLD && nb_ops_possible <
623 				tmp_qp->min_enq_burst_threshold) {
624 			tmp_qp->stats.threshold_hit_count++;
625 			return 0;
626 		}
627 	}
628 
629 	if (tmp_qp->service_type == QAT_SERVICE_SYMMETRIC)
630 		qat_sym_preprocess_requests(ops, nb_ops_possible);
631 
632 	memset(tmp_qp->opaque, 0xff, sizeof(tmp_qp->opaque));
633 
634 	while (nb_ops_sent != nb_ops_possible) {
635 		ret = op_build_request(*ops, base_addr + tail,
636 				tmp_qp->op_cookies[tail >> queue->trailz],
637 				tmp_qp);
638 
639 		if (ret != 0) {
640 			tmp_qp->stats.enqueue_err_count++;
641 			/* This message cannot be enqueued */
642 			if (nb_ops_sent == 0)
643 				return 0;
644 			goto kick_tail;
645 		}
646 
647 		tail = adf_modulo(tail + queue->msg_size, queue->modulo_mask);
648 		ops++;
649 		nb_ops_sent++;
650 	}
651 kick_tail:
652 	queue->tail = tail;
653 	tmp_qp->enqueued += nb_ops_sent;
654 	tmp_qp->stats.enqueued_count += nb_ops_sent;
655 	txq_write_tail(tmp_qp->qat_dev_gen, tmp_qp, queue);
656 	return nb_ops_sent;
657 }
658 
659 uint16_t
660 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 	rx_queue = &(tmp_qp->rx_q);
671 	head = rx_queue->head;
672 	resp_msg = (uint8_t *)rx_queue->base_addr + rx_queue->head;
673 
674 	while (*(uint32_t *)resp_msg != ADF_RING_EMPTY_SIG &&
675 			op_resp_counter != nb_ops) {
676 
677 		nb_fw_responses = 1;
678 
679 		nb_fw_responses = qat_dequeue_process_response(
680 				ops, resp_msg,
681 				tmp_qp->op_cookies[head >> rx_queue->trailz],
682 				&tmp_qp->stats.dequeue_err_count);
683 
684 		head = adf_modulo(head + rx_queue->msg_size,
685 				  rx_queue->modulo_mask);
686 
687 		resp_msg = (uint8_t *)rx_queue->base_addr + head;
688 
689 		if (nb_fw_responses) {
690 			/* only move on to next op if one was ready to return
691 			 * to API
692 			 */
693 			ops++;
694 			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 		fw_resp_counter += nb_fw_responses;
704 
705 		rx_queue->nb_processed_responses++;
706 	}
707 
708 	tmp_qp->dequeued += fw_resp_counter;
709 	tmp_qp->stats.dequeued_count += op_resp_counter;
710 
711 	rx_queue->head = head;
712 	if (rx_queue->nb_processed_responses > QAT_CSR_HEAD_WRITE_THRESH)
713 		rxq_free_desc(tmp_qp->qat_dev_gen, tmp_qp, rx_queue);
714 
715 	QAT_DP_LOG(DEBUG, "Dequeue burst return: %u, QAT responses: %u",
716 			op_resp_counter, fw_resp_counter);
717 
718 	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 qat_cq_dequeue_response(struct qat_qp *qp, void *out_data)
726 {
727 	uint8_t result = 0;
728 	uint8_t retries = 0;
729 	struct qat_queue *queue = &(qp->rx_q);
730 	struct icp_qat_fw_comn_resp *resp_msg = (struct icp_qat_fw_comn_resp *)
731 			((uint8_t *)queue->base_addr + queue->head);
732 
733 	while (retries++ < QAT_CQ_MAX_DEQ_RETRIES &&
734 			*(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 	if (*(uint32_t *)resp_msg != ADF_RING_EMPTY_SIG) {
740 		/* response received */
741 		result = 1;
742 
743 		/* check status flag */
744 		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 			memcpy(out_data, resp_msg, queue->msg_size);
749 		} else {
750 			memset(out_data, 0, queue->msg_size);
751 		}
752 
753 		queue->head = adf_modulo(queue->head + queue->msg_size,
754 				queue->modulo_mask);
755 		rxq_free_desc(qp->qat_dev_gen, qp, queue);
756 	}
757 
758 	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 qat_cq_get_fw_version(struct qat_qp *qp)
769 {
770 	struct qat_queue *queue = &(qp->tx_q);
771 	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 	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 	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 	memcpy(base_addr + queue->tail, &null_msg, sizeof(null_msg));
788 	queue->tail = adf_modulo(queue->tail + queue->msg_size,
789 			queue->modulo_mask);
790 	txq_write_tail(qp->qat_dev_gen, qp, queue);
791 
792 	/* receive a response */
793 	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 		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 			return response.resrvd[0]; /* return LW4 */
804 		else
805 			return 0; /* not set - we don't know fw version */
806 	}
807 
808 	QAT_LOG(ERR, "No response received");
809 	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 qat_cq_get_fw_cipher_crc_cap(struct qat_qp *qp)
826 {
827 	struct qat_queue *queue = &(qp->tx_q);
828 	uint8_t *base_addr = (uint8_t *)queue->base_addr;
829 	struct icp_qat_fw_la_bulk_req cipher_crc_cap_msg = {{0}};
830 	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 	session = rte_zmalloc(NULL, sizeof(struct qat_sym_session), 0);
839 	if (session == NULL)
840 		return -EINVAL;
841 
842 	/* Verify the session physical address is known */
843 	rte_iova_t session_paddr = rte_mem_virt2iova(session);
844 	if (session_paddr == 0 || session_paddr == RTE_BAD_IOVA) {
845 		QAT_LOG(ERR, "Session physical address unknown.");
846 		return -EINVAL;
847 	}
848 
849 	/* Prepare the LA bulk request */
850 	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 	if (ret < 0) {
856 		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 		return 0;
862 	}
863 
864 	cipher_crc_cap_msg = session->fw_req;
865 
866 	src_data_addr = rte_zmalloc(NULL,
867 					sizeof(cipher_crc_cap_check_plaintext),
868 					0);
869 	if (src_data_addr == NULL) {
870 		rte_free(session);
871 		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 	phy_src_addr = rte_mem_virt2iova(src_data_addr);
879 	if (phy_src_addr == 0 || phy_src_addr == RTE_BAD_IOVA) {
880 		QAT_LOG(ERR, "Source physical address unknown.");
881 		return -EINVAL;
882 	}
883 
884 	cipher_crc_cap_msg.comn_mid.src_data_addr = phy_src_addr;
885 	cipher_crc_cap_msg.comn_mid.src_length =
886 					sizeof(cipher_crc_cap_check_plaintext);
887 	cipher_crc_cap_msg.comn_mid.dest_data_addr = phy_src_addr;
888 	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 	cipher_param->cipher_offset = cipher_crc_cap_check_cipher_offset;
900 	cipher_param->cipher_length =
901 			sizeof(cipher_crc_cap_check_plaintext) -
902 			cipher_crc_cap_check_cipher_offset;
903 	auth_param->auth_off = cipher_crc_cap_check_crc_offset;
904 	auth_param->auth_len = sizeof(cipher_crc_cap_check_plaintext) -
905 				cipher_crc_cap_check_crc_offset -
906 				RTE_ETHER_CRC_LEN;
907 
908 	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 	memcpy(base_addr + queue->tail,
919 	       &cipher_crc_cap_msg,
920 	       sizeof(cipher_crc_cap_msg));
921 	queue->tail = adf_modulo(queue->tail + queue->msg_size,
922 			queue->modulo_mask);
923 	txq_write_tail(qp->qat_dev_gen, qp, queue);
924 
925 	/* Check for response and verify data is same as ciphertext */
926 	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 		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 	rte_free(src_data_addr);
943 	rte_free(session);
944 	return ret;
945 }
946 #endif
947 
948 __rte_weak int
949 qat_comp_process_response(void **op __rte_unused, uint8_t *resp __rte_unused,
950 			  void *op_cookie __rte_unused,
951 			  uint64_t *dequeue_err_count __rte_unused)
952 {
953 	return  0;
954 }
955