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