656645e4cf2dffe51f7d8a6c03032bc7d50ba595
[dpdk.git] / drivers / crypto / qat / qat_qp.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2018 Intel Corporation
3  */
4
5 #include <rte_common.h>
6 #include <rte_dev.h>
7 #include <rte_malloc.h>
8 #include <rte_memzone.h>
9 #include <rte_pci.h>
10 #include <rte_bus_pci.h>
11 #include <rte_atomic.h>
12 #include <rte_prefetch.h>
13
14 #include "qat_logs.h"
15 #include "qat_qp.h"
16 #include "qat_device.h"
17 #include "adf_transport_access_macros.h"
18
19
20 #define ADF_MAX_DESC                            4096
21 #define ADF_MIN_DESC                            128
22
23 #define ADF_ARB_REG_SLOT                        0x1000
24 #define ADF_ARB_RINGSRVARBEN_OFFSET             0x19C
25
26 #define WRITE_CSR_ARB_RINGSRVARBEN(csr_addr, index, value) \
27         ADF_CSR_WR(csr_addr, ADF_ARB_RINGSRVARBEN_OFFSET + \
28         (ADF_ARB_REG_SLOT * index), value)
29
30 __extension__
31 const struct qat_qp_hw_data qat_gen1_qps[QAT_MAX_SERVICES]
32                                          [ADF_MAX_QPS_PER_BUNDLE] = {
33         /* queue pairs which provide an asymmetric crypto service */
34         [QAT_SERVICE_ASYMMETRIC] = {
35                 {
36                         .service_type = QAT_SERVICE_ASYMMETRIC,
37                         .hw_bundle_num = 0,
38                         .tx_ring_num = 0,
39                         .rx_ring_num = 8,
40                         .tx_msg_size = 64,
41                         .rx_msg_size = 32,
42
43                 }, {
44                         .service_type = QAT_SERVICE_ASYMMETRIC,
45                         .tx_ring_num = 1,
46                         .rx_ring_num = 9,
47                         .tx_msg_size = 64,
48                         .rx_msg_size = 32,
49                 }, {
50                         .service_type = QAT_SERVICE_INVALID,
51                 }, {
52                         .service_type = QAT_SERVICE_INVALID,
53                 }
54         },
55         /* queue pairs which provide a symmetric crypto service */
56         [QAT_SERVICE_SYMMETRIC] = {
57                 {
58                         .service_type = QAT_SERVICE_SYMMETRIC,
59                         .hw_bundle_num = 0,
60                         .tx_ring_num = 2,
61                         .rx_ring_num = 10,
62                         .tx_msg_size = 128,
63                         .rx_msg_size = 32,
64                 },
65                 {
66                         .service_type = QAT_SERVICE_SYMMETRIC,
67                         .hw_bundle_num = 0,
68                         .tx_ring_num = 3,
69                         .rx_ring_num = 11,
70                         .tx_msg_size = 128,
71                         .rx_msg_size = 32,
72                 }, {
73                         .service_type = QAT_SERVICE_INVALID,
74                 }, {
75                         .service_type = QAT_SERVICE_INVALID,
76                 }
77         },
78         /* queue pairs which provide a compression service */
79         [QAT_SERVICE_COMPRESSION] = {
80                 {
81                         .service_type = QAT_SERVICE_COMPRESSION,
82                         .hw_bundle_num = 0,
83                         .tx_ring_num = 6,
84                         .rx_ring_num = 14,
85                         .tx_msg_size = 128,
86                         .rx_msg_size = 32,
87                 }, {
88                         .service_type = QAT_SERVICE_COMPRESSION,
89                         .hw_bundle_num = 0,
90                         .tx_ring_num = 7,
91                         .rx_ring_num = 15,
92                         .tx_msg_size = 128,
93                         .rx_msg_size = 32,
94                 }, {
95                         .service_type = QAT_SERVICE_INVALID,
96                 }, {
97                         .service_type = QAT_SERVICE_INVALID,
98                 }
99         }
100 };
101
102 static int qat_qp_check_queue_alignment(uint64_t phys_addr,
103         uint32_t queue_size_bytes);
104 static void qat_queue_delete(struct qat_queue *queue);
105 static int qat_queue_create(struct qat_pmd_private *qat_dev,
106         struct qat_queue *queue, struct qat_qp_config *, uint8_t dir);
107 static int adf_verify_queue_size(uint32_t msg_size, uint32_t msg_num,
108         uint32_t *queue_size_for_csr);
109 static void adf_configure_queues(struct qat_qp *queue);
110 static void adf_queue_arb_enable(struct qat_queue *txq, void *base_addr);
111 static void adf_queue_arb_disable(struct qat_queue *txq, void *base_addr);
112
113
114 int qat_qps_per_service(const struct qat_qp_hw_data *qp_hw_data,
115                 enum qat_service_type service)
116 {
117         int i, count;
118
119         for (i = 0, count = 0; i < ADF_MAX_QPS_PER_BUNDLE; i++)
120                 if (qp_hw_data[i].service_type == service)
121                         count++;
122         return count * ADF_NUM_BUNDLES_PER_DEV;
123 }
124
125 static const struct rte_memzone *
126 queue_dma_zone_reserve(const char *queue_name, uint32_t queue_size,
127                         int socket_id)
128 {
129         const struct rte_memzone *mz;
130
131         PMD_INIT_FUNC_TRACE();
132         mz = rte_memzone_lookup(queue_name);
133         if (mz != 0) {
134                 if (((size_t)queue_size <= mz->len) &&
135                                 ((socket_id == SOCKET_ID_ANY) ||
136                                         (socket_id == mz->socket_id))) {
137                         PMD_DRV_LOG(DEBUG, "re-use memzone already "
138                                         "allocated for %s", queue_name);
139                         return mz;
140                 }
141
142                 PMD_DRV_LOG(ERR, "Incompatible memzone already "
143                                 "allocated %s, size %u, socket %d. "
144                                 "Requested size %u, socket %u",
145                                 queue_name, (uint32_t)mz->len,
146                                 mz->socket_id, queue_size, socket_id);
147                 return NULL;
148         }
149
150         PMD_DRV_LOG(DEBUG, "Allocate memzone for %s, size %u on socket %u",
151                                         queue_name, queue_size, socket_id);
152         return rte_memzone_reserve_aligned(queue_name, queue_size,
153                 socket_id, RTE_MEMZONE_IOVA_CONTIG, queue_size);
154 }
155
156 int qat_qp_setup(struct qat_pmd_private *qat_dev,
157                 struct qat_qp **qp_addr,
158                 uint16_t queue_pair_id,
159                 struct qat_qp_config *qat_qp_conf)
160
161 {
162         struct qat_qp *qp;
163         struct rte_pci_device *pci_dev = qat_dev->pci_dev;
164         char op_cookie_pool_name[RTE_RING_NAMESIZE];
165         uint32_t i;
166
167         PMD_DRV_LOG(DEBUG, "Setup qp %u on device %d gen %d",
168                         queue_pair_id, qat_dev->dev_id, qat_dev->qat_dev_gen);
169
170         if ((qat_qp_conf->nb_descriptors > ADF_MAX_DESC) ||
171                 (qat_qp_conf->nb_descriptors < ADF_MIN_DESC)) {
172                 PMD_DRV_LOG(ERR, "Can't create qp for %u descriptors",
173                                 qat_qp_conf->nb_descriptors);
174                 return -EINVAL;
175         }
176
177         if (pci_dev->mem_resource[0].addr == NULL) {
178                 PMD_DRV_LOG(ERR, "Could not find VF config space "
179                                 "(UIO driver attached?).");
180                 return -EINVAL;
181         }
182
183         /* Allocate the queue pair data structure. */
184         qp = rte_zmalloc("qat PMD qp metadata",
185                         sizeof(*qp), RTE_CACHE_LINE_SIZE);
186         if (qp == NULL) {
187                 PMD_DRV_LOG(ERR, "Failed to alloc mem for qp struct");
188                 return -ENOMEM;
189         }
190         qp->nb_descriptors = qat_qp_conf->nb_descriptors;
191         qp->op_cookies = rte_zmalloc("qat PMD op cookie pointer",
192                         qat_qp_conf->nb_descriptors * sizeof(*qp->op_cookies),
193                         RTE_CACHE_LINE_SIZE);
194         if (qp->op_cookies == NULL) {
195                 PMD_DRV_LOG(ERR, "Failed to alloc mem for cookie");
196                 rte_free(qp);
197                 return -ENOMEM;
198         }
199
200         qp->mmap_bar_addr = pci_dev->mem_resource[0].addr;
201         qp->inflights16 = 0;
202
203         if (qat_queue_create(qat_dev, &(qp->tx_q), qat_qp_conf,
204                                         ADF_RING_DIR_TX) != 0) {
205                 PMD_INIT_LOG(ERR, "Tx queue create failed "
206                                 "queue_pair_id=%u", queue_pair_id);
207                 goto create_err;
208         }
209
210         if (qat_queue_create(qat_dev, &(qp->rx_q), qat_qp_conf,
211                                         ADF_RING_DIR_RX) != 0) {
212                 PMD_DRV_LOG(ERR, "Rx queue create failed "
213                                 "queue_pair_id=%hu", queue_pair_id);
214                 qat_queue_delete(&(qp->tx_q));
215                 goto create_err;
216         }
217
218         adf_configure_queues(qp);
219         adf_queue_arb_enable(&qp->tx_q, qp->mmap_bar_addr);
220
221         snprintf(op_cookie_pool_name, RTE_RING_NAMESIZE, "%s_%s_qp_op_%d_%hu",
222                 pci_dev->driver->driver.name, qat_qp_conf->service_str,
223                 qat_dev->dev_id, queue_pair_id);
224
225         qp->op_cookie_pool = rte_mempool_lookup(op_cookie_pool_name);
226         if (qp->op_cookie_pool == NULL)
227                 qp->op_cookie_pool = rte_mempool_create(op_cookie_pool_name,
228                                 qp->nb_descriptors,
229                                 qat_qp_conf->cookie_size, 64, 0,
230                                 NULL, NULL, NULL, NULL, qat_qp_conf->socket_id,
231                                 0);
232         if (!qp->op_cookie_pool) {
233                 PMD_DRV_LOG(ERR, "QAT PMD Cannot create"
234                                 " op mempool");
235                 goto create_err;
236         }
237
238         for (i = 0; i < qp->nb_descriptors; i++) {
239                 if (rte_mempool_get(qp->op_cookie_pool, &qp->op_cookies[i])) {
240                         PMD_DRV_LOG(ERR, "QAT PMD Cannot get op_cookie");
241                         goto create_err;
242                 }
243         }
244
245         qp->qat_dev_gen = qat_dev->qat_dev_gen;
246         qp->build_request = qat_qp_conf->build_request;
247         qp->process_response = qat_qp_conf->process_response;
248         qp->qat_dev = qat_dev;
249
250         PMD_DRV_LOG(DEBUG, "QP setup complete: id: %d, cookiepool: %s",
251                         queue_pair_id, op_cookie_pool_name);
252
253         *qp_addr = qp;
254         return 0;
255
256 create_err:
257         rte_free(qp);
258         return -EFAULT;
259 }
260
261 int qat_qp_release(struct qat_qp **qp_addr)
262 {
263         struct qat_qp *qp = *qp_addr;
264         uint32_t i;
265
266         PMD_INIT_FUNC_TRACE();
267         if (qp == NULL) {
268                 PMD_DRV_LOG(DEBUG, "qp already freed");
269                 return 0;
270         }
271
272         PMD_DRV_LOG(DEBUG, "Free qp on qat_pci device %d",
273                         qp->qat_dev->dev_id);
274
275         /* Don't free memory if there are still responses to be processed */
276         if (qp->inflights16 == 0) {
277                 qat_queue_delete(&(qp->tx_q));
278                 qat_queue_delete(&(qp->rx_q));
279         } else {
280                 return -EAGAIN;
281         }
282
283         adf_queue_arb_disable(&(qp->tx_q), qp->mmap_bar_addr);
284
285         for (i = 0; i < qp->nb_descriptors; i++)
286                 rte_mempool_put(qp->op_cookie_pool, qp->op_cookies[i]);
287
288         if (qp->op_cookie_pool)
289                 rte_mempool_free(qp->op_cookie_pool);
290
291         rte_free(qp->op_cookies);
292         rte_free(qp);
293         *qp_addr = NULL;
294         return 0;
295 }
296
297
298 static void qat_queue_delete(struct qat_queue *queue)
299 {
300         const struct rte_memzone *mz;
301         int status = 0;
302
303         if (queue == NULL) {
304                 PMD_DRV_LOG(DEBUG, "Invalid queue");
305                 return;
306         }
307         PMD_DRV_LOG(DEBUG, "Free ring %d, memzone: %s",
308                         queue->hw_queue_number, queue->memz_name);
309
310         mz = rte_memzone_lookup(queue->memz_name);
311         if (mz != NULL) {
312                 /* Write an unused pattern to the queue memory. */
313                 memset(queue->base_addr, 0x7F, queue->queue_size);
314                 status = rte_memzone_free(mz);
315                 if (status != 0)
316                         PMD_DRV_LOG(ERR, "Error %d on freeing queue %s",
317                                         status, queue->memz_name);
318         } else {
319                 PMD_DRV_LOG(DEBUG, "queue %s doesn't exist",
320                                 queue->memz_name);
321         }
322 }
323
324 static int
325 qat_queue_create(struct qat_pmd_private *qat_dev, struct qat_queue *queue,
326                 struct qat_qp_config *qp_conf, uint8_t dir)
327 {
328         uint64_t queue_base;
329         void *io_addr;
330         const struct rte_memzone *qp_mz;
331         struct rte_pci_device *pci_dev = qat_dev->pci_dev;
332         int ret = 0;
333         uint16_t desc_size = (dir == ADF_RING_DIR_TX ?
334                         qp_conf->hw->tx_msg_size : qp_conf->hw->rx_msg_size);
335         uint32_t queue_size_bytes = (qp_conf->nb_descriptors)*(desc_size);
336
337         queue->hw_bundle_number = qp_conf->hw->hw_bundle_num;
338         queue->hw_queue_number = (dir == ADF_RING_DIR_TX ?
339                         qp_conf->hw->tx_ring_num : qp_conf->hw->rx_ring_num);
340
341         if (desc_size > ADF_MSG_SIZE_TO_BYTES(ADF_MAX_MSG_SIZE)) {
342                 PMD_DRV_LOG(ERR, "Invalid descriptor size %d", desc_size);
343                 return -EINVAL;
344         }
345
346         /*
347          * Allocate a memzone for the queue - create a unique name.
348          */
349         snprintf(queue->memz_name, sizeof(queue->memz_name),
350                 "%s_%s_%s_%d_%d_%d",
351                 pci_dev->driver->driver.name, qp_conf->service_str,
352                 "qp_mem", qat_dev->dev_id,
353                 queue->hw_bundle_number, queue->hw_queue_number);
354         qp_mz = queue_dma_zone_reserve(queue->memz_name, queue_size_bytes,
355                         qp_conf->socket_id);
356         if (qp_mz == NULL) {
357                 PMD_DRV_LOG(ERR, "Failed to allocate ring memzone");
358                 return -ENOMEM;
359         }
360
361         queue->base_addr = (char *)qp_mz->addr;
362         queue->base_phys_addr = qp_mz->iova;
363         if (qat_qp_check_queue_alignment(queue->base_phys_addr,
364                         queue_size_bytes)) {
365                 PMD_DRV_LOG(ERR, "Invalid alignment on queue create "
366                                         " 0x%"PRIx64"\n",
367                                         queue->base_phys_addr);
368                 ret = -EFAULT;
369                 goto queue_create_err;
370         }
371
372         if (adf_verify_queue_size(desc_size, qp_conf->nb_descriptors,
373                         &(queue->queue_size)) != 0) {
374                 PMD_DRV_LOG(ERR, "Invalid num inflights");
375                 ret = -EINVAL;
376                 goto queue_create_err;
377         }
378
379         queue->max_inflights = ADF_MAX_INFLIGHTS(queue->queue_size,
380                                         ADF_BYTES_TO_MSG_SIZE(desc_size));
381         queue->modulo = ADF_RING_SIZE_MODULO(queue->queue_size);
382
383         if (queue->max_inflights < 2) {
384                 PMD_DRV_LOG(ERR, "Invalid num inflights");
385                 ret = -EINVAL;
386                 goto queue_create_err;
387         }
388         queue->head = 0;
389         queue->tail = 0;
390         queue->msg_size = desc_size;
391
392         /*
393          * Write an unused pattern to the queue memory.
394          */
395         memset(queue->base_addr, 0x7F, queue_size_bytes);
396
397         queue_base = BUILD_RING_BASE_ADDR(queue->base_phys_addr,
398                                         queue->queue_size);
399
400         io_addr = pci_dev->mem_resource[0].addr;
401
402         WRITE_CSR_RING_BASE(io_addr, queue->hw_bundle_number,
403                         queue->hw_queue_number, queue_base);
404
405         PMD_DRV_LOG(DEBUG, "RING: Name:%s, size in CSR: %u, in bytes %u,"
406                         " nb msgs %u, msg_size %u, max_inflights %u modulo %u",
407                         queue->memz_name,
408                         queue->queue_size, queue_size_bytes,
409                         qp_conf->nb_descriptors, desc_size,
410                         queue->max_inflights, queue->modulo);
411
412         return 0;
413
414 queue_create_err:
415         rte_memzone_free(qp_mz);
416         return ret;
417 }
418
419 static int qat_qp_check_queue_alignment(uint64_t phys_addr,
420                                         uint32_t queue_size_bytes)
421 {
422         PMD_INIT_FUNC_TRACE();
423         if (((queue_size_bytes - 1) & phys_addr) != 0)
424                 return -EINVAL;
425         return 0;
426 }
427
428 static int adf_verify_queue_size(uint32_t msg_size, uint32_t msg_num,
429         uint32_t *p_queue_size_for_csr)
430 {
431         uint8_t i = ADF_MIN_RING_SIZE;
432
433         PMD_INIT_FUNC_TRACE();
434         for (; i <= ADF_MAX_RING_SIZE; i++)
435                 if ((msg_size * msg_num) ==
436                                 (uint32_t)ADF_SIZE_TO_RING_SIZE_IN_BYTES(i)) {
437                         *p_queue_size_for_csr = i;
438                         return 0;
439                 }
440         PMD_DRV_LOG(ERR, "Invalid ring size %d", msg_size * msg_num);
441         return -EINVAL;
442 }
443
444 static void adf_queue_arb_enable(struct qat_queue *txq, void *base_addr)
445 {
446         uint32_t arb_csr_offset =  ADF_ARB_RINGSRVARBEN_OFFSET +
447                                         (ADF_ARB_REG_SLOT *
448                                                         txq->hw_bundle_number);
449         uint32_t value;
450
451         PMD_INIT_FUNC_TRACE();
452         value = ADF_CSR_RD(base_addr, arb_csr_offset);
453         value |= (0x01 << txq->hw_queue_number);
454         ADF_CSR_WR(base_addr, arb_csr_offset, value);
455 }
456
457 static void adf_queue_arb_disable(struct qat_queue *txq, void *base_addr)
458 {
459         uint32_t arb_csr_offset =  ADF_ARB_RINGSRVARBEN_OFFSET +
460                                         (ADF_ARB_REG_SLOT *
461                                                         txq->hw_bundle_number);
462         uint32_t value;
463
464         PMD_INIT_FUNC_TRACE();
465         value = ADF_CSR_RD(base_addr, arb_csr_offset);
466         value ^= (0x01 << txq->hw_queue_number);
467         ADF_CSR_WR(base_addr, arb_csr_offset, value);
468 }
469
470 static void adf_configure_queues(struct qat_qp *qp)
471 {
472         uint32_t queue_config;
473         struct qat_queue *queue = &qp->tx_q;
474
475         PMD_INIT_FUNC_TRACE();
476         queue_config = BUILD_RING_CONFIG(queue->queue_size);
477
478         WRITE_CSR_RING_CONFIG(qp->mmap_bar_addr, queue->hw_bundle_number,
479                         queue->hw_queue_number, queue_config);
480
481         queue = &qp->rx_q;
482         queue_config =
483                         BUILD_RESP_RING_CONFIG(queue->queue_size,
484                                         ADF_RING_NEAR_WATERMARK_512,
485                                         ADF_RING_NEAR_WATERMARK_0);
486
487         WRITE_CSR_RING_CONFIG(qp->mmap_bar_addr, queue->hw_bundle_number,
488                         queue->hw_queue_number, queue_config);
489 }
490
491
492 static inline uint32_t adf_modulo(uint32_t data, uint32_t shift)
493 {
494         uint32_t div = data >> shift;
495         uint32_t mult = div << shift;
496
497         return data - mult;
498 }
499
500 static inline void
501 txq_write_tail(struct qat_qp *qp, struct qat_queue *q) {
502         WRITE_CSR_RING_TAIL(qp->mmap_bar_addr, q->hw_bundle_number,
503                         q->hw_queue_number, q->tail);
504         q->nb_pending_requests = 0;
505         q->csr_tail = q->tail;
506 }
507
508 static inline
509 void rxq_free_desc(struct qat_qp *qp, struct qat_queue *q)
510 {
511         uint32_t old_head, new_head;
512         uint32_t max_head;
513
514         old_head = q->csr_head;
515         new_head = q->head;
516         max_head = qp->nb_descriptors * q->msg_size;
517
518         /* write out free descriptors */
519         void *cur_desc = (uint8_t *)q->base_addr + old_head;
520
521         if (new_head < old_head) {
522                 memset(cur_desc, ADF_RING_EMPTY_SIG_BYTE, max_head - old_head);
523                 memset(q->base_addr, ADF_RING_EMPTY_SIG_BYTE, new_head);
524         } else {
525                 memset(cur_desc, ADF_RING_EMPTY_SIG_BYTE, new_head - old_head);
526         }
527         q->nb_processed_responses = 0;
528         q->csr_head = new_head;
529
530         /* write current head to CSR */
531         WRITE_CSR_RING_HEAD(qp->mmap_bar_addr, q->hw_bundle_number,
532                             q->hw_queue_number, new_head);
533 }
534
535 uint16_t
536 qat_enqueue_op_burst(void *qp, void **ops, uint16_t nb_ops)
537 {
538         register struct qat_queue *queue;
539         struct qat_qp *tmp_qp = (struct qat_qp *)qp;
540         register uint32_t nb_ops_sent = 0;
541         register int ret;
542         uint16_t nb_ops_possible = nb_ops;
543         register uint8_t *base_addr;
544         register uint32_t tail;
545         int overflow;
546
547         if (unlikely(nb_ops == 0))
548                 return 0;
549
550         /* read params used a lot in main loop into registers */
551         queue = &(tmp_qp->tx_q);
552         base_addr = (uint8_t *)queue->base_addr;
553         tail = queue->tail;
554
555         /* Find how many can actually fit on the ring */
556         tmp_qp->inflights16 += nb_ops;
557         overflow = tmp_qp->inflights16 - queue->max_inflights;
558         if (overflow > 0) {
559                 tmp_qp->inflights16 -= overflow;
560                 nb_ops_possible = nb_ops - overflow;
561                 if (nb_ops_possible == 0)
562                         return 0;
563         }
564
565         while (nb_ops_sent != nb_ops_possible) {
566                 ret = tmp_qp->build_request(*ops, base_addr + tail,
567                                 tmp_qp->op_cookies[tail / queue->msg_size],
568                                 tmp_qp->qat_dev_gen);
569                 if (ret != 0) {
570                         tmp_qp->stats.enqueue_err_count++;
571                         /*
572                          * This message cannot be enqueued,
573                          * decrease number of ops that wasn't sent
574                          */
575                         tmp_qp->inflights16 -= nb_ops_possible - nb_ops_sent;
576                         if (nb_ops_sent == 0)
577                                 return 0;
578                         goto kick_tail;
579                 }
580
581                 tail = adf_modulo(tail + queue->msg_size, queue->modulo);
582                 ops++;
583                 nb_ops_sent++;
584         }
585 kick_tail:
586         queue->tail = tail;
587         tmp_qp->stats.enqueued_count += nb_ops_sent;
588         queue->nb_pending_requests += nb_ops_sent;
589         if (tmp_qp->inflights16 < QAT_CSR_TAIL_FORCE_WRITE_THRESH ||
590                     queue->nb_pending_requests > QAT_CSR_TAIL_WRITE_THRESH) {
591                 txq_write_tail(tmp_qp, queue);
592         }
593         return nb_ops_sent;
594 }
595
596 uint16_t
597 qat_dequeue_op_burst(void *qp, void **ops, uint16_t nb_ops)
598 {
599         struct qat_queue *rx_queue, *tx_queue;
600         struct qat_qp *tmp_qp = (struct qat_qp *)qp;
601         uint32_t head;
602         uint32_t resp_counter = 0;
603         uint8_t *resp_msg;
604
605         rx_queue = &(tmp_qp->rx_q);
606         tx_queue = &(tmp_qp->tx_q);
607         head = rx_queue->head;
608         resp_msg = (uint8_t *)rx_queue->base_addr + rx_queue->head;
609
610         while (*(uint32_t *)resp_msg != ADF_RING_EMPTY_SIG &&
611                         resp_counter != nb_ops) {
612
613                 tmp_qp->process_response(ops, resp_msg,
614                         tmp_qp->op_cookies[head / rx_queue->msg_size],
615                         tmp_qp->qat_dev_gen);
616
617                 head = adf_modulo(head + rx_queue->msg_size, rx_queue->modulo);
618
619                 resp_msg = (uint8_t *)rx_queue->base_addr + head;
620                 ops++;
621                 resp_counter++;
622         }
623         if (resp_counter > 0) {
624                 rx_queue->head = head;
625                 tmp_qp->stats.dequeued_count += resp_counter;
626                 rx_queue->nb_processed_responses += resp_counter;
627                 tmp_qp->inflights16 -= resp_counter;
628
629                 if (rx_queue->nb_processed_responses >
630                                                 QAT_CSR_HEAD_WRITE_THRESH)
631                         rxq_free_desc(tmp_qp, rx_queue);
632         }
633         /* also check if tail needs to be advanced */
634         if (tmp_qp->inflights16 <= QAT_CSR_TAIL_FORCE_WRITE_THRESH &&
635                 tx_queue->tail != tx_queue->csr_tail) {
636                 txq_write_tail(tmp_qp, tx_queue);
637         }
638         return resp_counter;
639 }