net/mlx4: fix indirection table error rollback
[dpdk.git] / drivers / net / mlx4 / mlx4_rxq.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 6WIND S.A.
5  *   Copyright 2017 Mellanox
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 /**
35  * @file
36  * Rx queues configuration for mlx4 driver.
37  */
38
39 #include <assert.h>
40 #include <errno.h>
41 #include <stddef.h>
42 #include <stdint.h>
43 #include <string.h>
44
45 /* Verbs headers do not support -pedantic. */
46 #ifdef PEDANTIC
47 #pragma GCC diagnostic ignored "-Wpedantic"
48 #endif
49 #include <infiniband/verbs.h>
50 #ifdef PEDANTIC
51 #pragma GCC diagnostic error "-Wpedantic"
52 #endif
53
54 #include <rte_byteorder.h>
55 #include <rte_common.h>
56 #include <rte_errno.h>
57 #include <rte_ethdev.h>
58 #include <rte_flow.h>
59 #include <rte_malloc.h>
60 #include <rte_mbuf.h>
61 #include <rte_mempool.h>
62
63 #include "mlx4.h"
64 #include "mlx4_flow.h"
65 #include "mlx4_rxtx.h"
66 #include "mlx4_utils.h"
67
68 /**
69  * Historical RSS hash key.
70  *
71  * This used to be the default for mlx4 in Linux before v3.19 switched to
72  * generating random hash keys through netdev_rss_key_fill().
73  *
74  * It is used in this PMD for consistency with past DPDK releases but can
75  * now be overridden through user configuration.
76  *
77  * Note: this is not const to work around API quirks.
78  */
79 uint8_t
80 mlx4_rss_hash_key_default[MLX4_RSS_HASH_KEY_SIZE] = {
81         0x2c, 0xc6, 0x81, 0xd1,
82         0x5b, 0xdb, 0xf4, 0xf7,
83         0xfc, 0xa2, 0x83, 0x19,
84         0xdb, 0x1a, 0x3e, 0x94,
85         0x6b, 0x9e, 0x38, 0xd9,
86         0x2c, 0x9c, 0x03, 0xd1,
87         0xad, 0x99, 0x44, 0xa7,
88         0xd9, 0x56, 0x3d, 0x59,
89         0x06, 0x3c, 0x25, 0xf3,
90         0xfc, 0x1f, 0xdc, 0x2a,
91 };
92
93 /**
94  * Obtain a RSS context with specified properties.
95  *
96  * Used when creating a flow rule targeting one or several Rx queues.
97  *
98  * If a matching RSS context already exists, it is returned with its
99  * reference count incremented.
100  *
101  * @param priv
102  *   Pointer to private structure.
103  * @param fields
104  *   Fields for RSS processing (Verbs format).
105  * @param[in] key
106  *   Hash key to use (whose size is exactly MLX4_RSS_HASH_KEY_SIZE).
107  * @param queues
108  *   Number of target queues.
109  * @param[in] queue_id
110  *   Target queues.
111  *
112  * @return
113  *   Pointer to RSS context on success, NULL otherwise and rte_errno is set.
114  */
115 struct mlx4_rss *
116 mlx4_rss_get(struct priv *priv, uint64_t fields,
117              uint8_t key[MLX4_RSS_HASH_KEY_SIZE],
118              uint16_t queues, const uint16_t queue_id[])
119 {
120         struct mlx4_rss *rss;
121         size_t queue_id_size = sizeof(queue_id[0]) * queues;
122
123         LIST_FOREACH(rss, &priv->rss, next)
124                 if (fields == rss->fields &&
125                     queues == rss->queues &&
126                     !memcmp(key, rss->key, MLX4_RSS_HASH_KEY_SIZE) &&
127                     !memcmp(queue_id, rss->queue_id, queue_id_size)) {
128                         ++rss->refcnt;
129                         return rss;
130                 }
131         rss = rte_malloc(__func__, offsetof(struct mlx4_rss, queue_id) +
132                          queue_id_size, 0);
133         if (!rss)
134                 goto error;
135         *rss = (struct mlx4_rss){
136                 .priv = priv,
137                 .refcnt = 1,
138                 .usecnt = 0,
139                 .qp = NULL,
140                 .ind = NULL,
141                 .fields = fields,
142                 .queues = queues,
143         };
144         memcpy(rss->key, key, MLX4_RSS_HASH_KEY_SIZE);
145         memcpy(rss->queue_id, queue_id, queue_id_size);
146         LIST_INSERT_HEAD(&priv->rss, rss, next);
147         return rss;
148 error:
149         rte_errno = ENOMEM;
150         return NULL;
151 }
152
153 /**
154  * Release a RSS context instance.
155  *
156  * Used when destroying a flow rule targeting one or several Rx queues.
157  *
158  * This function decrements the reference count of the context and destroys
159  * it after reaching 0. The context must have no users at this point; all
160  * prior calls to mlx4_rss_attach() must have been followed by matching
161  * calls to mlx4_rss_detach().
162  *
163  * @param rss
164  *   RSS context to release.
165  */
166 void mlx4_rss_put(struct mlx4_rss *rss)
167 {
168         assert(rss->refcnt);
169         if (--rss->refcnt)
170                 return;
171         assert(!rss->usecnt);
172         assert(!rss->qp);
173         assert(!rss->ind);
174         LIST_REMOVE(rss, next);
175         rte_free(rss);
176 }
177
178 /**
179  * Attach a user to a RSS context instance.
180  *
181  * Used when the RSS QP and indirection table objects must be instantiated,
182  * that is, when a flow rule must be enabled.
183  *
184  * This function increments the usage count of the context.
185  *
186  * @param rss
187  *   RSS context to attach to.
188  *
189  * @return
190  *   0 on success, a negative errno value otherwise and rte_errno is set.
191  */
192 int mlx4_rss_attach(struct mlx4_rss *rss)
193 {
194         assert(rss->refcnt);
195         if (rss->usecnt++) {
196                 assert(rss->qp);
197                 assert(rss->ind);
198                 return 0;
199         }
200
201         struct ibv_wq *ind_tbl[rss->queues];
202         struct priv *priv = rss->priv;
203         const char *msg;
204         unsigned int i;
205         int ret;
206
207         if (!rte_is_power_of_2(RTE_DIM(ind_tbl))) {
208                 ret = EINVAL;
209                 msg = "number of RSS queues must be a power of two";
210                 goto error;
211         }
212         for (i = 0; i != RTE_DIM(ind_tbl); ++i) {
213                 uint16_t id = rss->queue_id[i];
214                 struct rxq *rxq = NULL;
215
216                 if (id < priv->dev->data->nb_rx_queues)
217                         rxq = priv->dev->data->rx_queues[id];
218                 if (!rxq) {
219                         ret = EINVAL;
220                         msg = "RSS target queue is not configured";
221                         goto error;
222                 }
223                 ind_tbl[i] = rxq->wq;
224         }
225         rss->ind = ibv_create_rwq_ind_table
226                 (priv->ctx,
227                  &(struct ibv_rwq_ind_table_init_attr){
228                         .log_ind_tbl_size = rte_log2_u32(RTE_DIM(ind_tbl)),
229                         .ind_tbl = ind_tbl,
230                         .comp_mask = 0,
231                  });
232         if (!rss->ind) {
233                 ret = errno ? errno : EINVAL;
234                 msg = "RSS indirection table creation failure";
235                 goto error;
236         }
237         rss->qp = ibv_create_qp_ex
238                 (priv->ctx,
239                  &(struct ibv_qp_init_attr_ex){
240                         .comp_mask = (IBV_QP_INIT_ATTR_PD |
241                                       IBV_QP_INIT_ATTR_RX_HASH |
242                                       IBV_QP_INIT_ATTR_IND_TABLE),
243                         .qp_type = IBV_QPT_RAW_PACKET,
244                         .pd = priv->pd,
245                         .rwq_ind_tbl = rss->ind,
246                         .rx_hash_conf = {
247                                 .rx_hash_function = IBV_RX_HASH_FUNC_TOEPLITZ,
248                                 .rx_hash_key_len = MLX4_RSS_HASH_KEY_SIZE,
249                                 .rx_hash_key = rss->key,
250                                 .rx_hash_fields_mask = rss->fields,
251                         },
252                  });
253         if (!rss->qp) {
254                 ret = errno ? errno : EINVAL;
255                 msg = "RSS hash QP creation failure";
256                 goto error;
257         }
258         ret = ibv_modify_qp
259                 (rss->qp,
260                  &(struct ibv_qp_attr){
261                         .qp_state = IBV_QPS_INIT,
262                         .port_num = priv->port,
263                  },
264                  IBV_QP_STATE | IBV_QP_PORT);
265         if (ret) {
266                 msg = "failed to switch RSS hash QP to INIT state";
267                 goto error;
268         }
269         ret = ibv_modify_qp
270                 (rss->qp,
271                  &(struct ibv_qp_attr){
272                         .qp_state = IBV_QPS_RTR,
273                  },
274                  IBV_QP_STATE);
275         if (ret) {
276                 msg = "failed to switch RSS hash QP to RTR state";
277                 goto error;
278         }
279         return 0;
280 error:
281         if (rss->qp) {
282                 claim_zero(ibv_destroy_qp(rss->qp));
283                 rss->qp = NULL;
284         }
285         if (rss->ind) {
286                 claim_zero(ibv_destroy_rwq_ind_table(rss->ind));
287                 rss->ind = NULL;
288         }
289         ERROR("mlx4: %s", msg);
290         --rss->usecnt;
291         rte_errno = ret;
292         return -ret;
293 }
294
295 /**
296  * Detach a user from a RSS context instance.
297  *
298  * Used when disabling (not destroying) a flow rule.
299  *
300  * This function decrements the usage count of the context and destroys
301  * usage resources after reaching 0.
302  *
303  * @param rss
304  *   RSS context to detach from.
305  */
306 void mlx4_rss_detach(struct mlx4_rss *rss)
307 {
308         assert(rss->refcnt);
309         assert(rss->qp);
310         assert(rss->ind);
311         if (--rss->usecnt)
312                 return;
313         claim_zero(ibv_destroy_qp(rss->qp));
314         rss->qp = NULL;
315         claim_zero(ibv_destroy_rwq_ind_table(rss->ind));
316         rss->ind = NULL;
317 }
318
319 /**
320  * Allocate Rx queue elements.
321  *
322  * @param rxq
323  *   Pointer to Rx queue structure.
324  *
325  * @return
326  *   0 on success, negative errno value otherwise and rte_errno is set.
327  */
328 static int
329 mlx4_rxq_alloc_elts(struct rxq *rxq)
330 {
331         const uint32_t elts_n = 1 << rxq->elts_n;
332         const uint32_t sges_n = 1 << rxq->sges_n;
333         struct rte_mbuf *(*elts)[elts_n] = rxq->elts;
334         unsigned int i;
335
336         assert(rte_is_power_of_2(elts_n));
337         for (i = 0; i != RTE_DIM(*elts); ++i) {
338                 volatile struct mlx4_wqe_data_seg *scat = &(*rxq->wqes)[i];
339                 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
340
341                 if (buf == NULL) {
342                         while (i--) {
343                                 rte_pktmbuf_free_seg((*elts)[i]);
344                                 (*elts)[i] = NULL;
345                         }
346                         rte_errno = ENOMEM;
347                         return -rte_errno;
348                 }
349                 /* Headroom is reserved by rte_pktmbuf_alloc(). */
350                 assert(buf->data_off == RTE_PKTMBUF_HEADROOM);
351                 /* Buffer is supposed to be empty. */
352                 assert(rte_pktmbuf_data_len(buf) == 0);
353                 assert(rte_pktmbuf_pkt_len(buf) == 0);
354                 /* Only the first segment keeps headroom. */
355                 if (i % sges_n)
356                         buf->data_off = 0;
357                 buf->port = rxq->port_id;
358                 buf->data_len = rte_pktmbuf_tailroom(buf);
359                 buf->pkt_len = rte_pktmbuf_tailroom(buf);
360                 buf->nb_segs = 1;
361                 *scat = (struct mlx4_wqe_data_seg){
362                         .addr = rte_cpu_to_be_64(rte_pktmbuf_mtod(buf,
363                                                                   uintptr_t)),
364                         .byte_count = rte_cpu_to_be_32(buf->data_len),
365                         .lkey = rte_cpu_to_be_32(rxq->mr->lkey),
366                 };
367                 (*elts)[i] = buf;
368         }
369         DEBUG("%p: allocated and configured %u segments (max %u packets)",
370               (void *)rxq, elts_n, elts_n / sges_n);
371         return 0;
372 }
373
374 /**
375  * Free Rx queue elements.
376  *
377  * @param rxq
378  *   Pointer to Rx queue structure.
379  */
380 static void
381 mlx4_rxq_free_elts(struct rxq *rxq)
382 {
383         unsigned int i;
384         struct rte_mbuf *(*elts)[1 << rxq->elts_n] = rxq->elts;
385
386         DEBUG("%p: freeing Rx queue elements", (void *)rxq);
387         for (i = 0; (i != RTE_DIM(*elts)); ++i) {
388                 if (!(*elts)[i])
389                         continue;
390                 rte_pktmbuf_free_seg((*elts)[i]);
391                 (*elts)[i] = NULL;
392         }
393 }
394
395 /**
396  * DPDK callback to configure a Rx queue.
397  *
398  * @param dev
399  *   Pointer to Ethernet device structure.
400  * @param idx
401  *   Rx queue index.
402  * @param desc
403  *   Number of descriptors to configure in queue.
404  * @param socket
405  *   NUMA socket on which memory must be allocated.
406  * @param[in] conf
407  *   Thresholds parameters.
408  * @param mp
409  *   Memory pool for buffer allocations.
410  *
411  * @return
412  *   0 on success, negative errno value otherwise and rte_errno is set.
413  */
414 int
415 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
416                     unsigned int socket, const struct rte_eth_rxconf *conf,
417                     struct rte_mempool *mp)
418 {
419         struct priv *priv = dev->data->dev_private;
420         struct mlx4dv_obj mlxdv;
421         struct mlx4dv_rwq dv_rwq;
422         struct mlx4dv_cq dv_cq;
423         uint32_t mb_len = rte_pktmbuf_data_room_size(mp);
424         struct rte_mbuf *(*elts)[rte_align32pow2(desc)];
425         struct rxq *rxq;
426         struct mlx4_malloc_vec vec[] = {
427                 {
428                         .align = RTE_CACHE_LINE_SIZE,
429                         .size = sizeof(*rxq),
430                         .addr = (void **)&rxq,
431                 },
432                 {
433                         .align = RTE_CACHE_LINE_SIZE,
434                         .size = sizeof(*elts),
435                         .addr = (void **)&elts,
436                 },
437         };
438         int ret;
439
440         (void)conf; /* Thresholds configuration (ignored). */
441         DEBUG("%p: configuring queue %u for %u descriptors",
442               (void *)dev, idx, desc);
443         if (idx >= dev->data->nb_rx_queues) {
444                 rte_errno = EOVERFLOW;
445                 ERROR("%p: queue index out of range (%u >= %u)",
446                       (void *)dev, idx, dev->data->nb_rx_queues);
447                 return -rte_errno;
448         }
449         rxq = dev->data->rx_queues[idx];
450         if (rxq) {
451                 rte_errno = EEXIST;
452                 ERROR("%p: Rx queue %u already configured, release it first",
453                       (void *)dev, idx);
454                 return -rte_errno;
455         }
456         if (!desc) {
457                 rte_errno = EINVAL;
458                 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
459                 return -rte_errno;
460         }
461         if (desc != RTE_DIM(*elts)) {
462                 desc = RTE_DIM(*elts);
463                 WARN("%p: increased number of descriptors in Rx queue %u"
464                      " to the next power of two (%u)",
465                      (void *)dev, idx, desc);
466         }
467         /* Allocate and initialize Rx queue. */
468         mlx4_zmallocv_socket("RXQ", vec, RTE_DIM(vec), socket);
469         if (!rxq) {
470                 ERROR("%p: unable to allocate queue index %u",
471                       (void *)dev, idx);
472                 return -rte_errno;
473         }
474         *rxq = (struct rxq){
475                 .priv = priv,
476                 .mp = mp,
477                 .port_id = dev->data->port_id,
478                 .sges_n = 0,
479                 .elts_n = rte_log2_u32(desc),
480                 .elts = elts,
481                 /* Toggle Rx checksum offload if hardware supports it. */
482                 .csum = (priv->hw_csum &&
483                          dev->data->dev_conf.rxmode.hw_ip_checksum),
484                 .csum_l2tun = (priv->hw_csum_l2tun &&
485                                dev->data->dev_conf.rxmode.hw_ip_checksum),
486                 .stats = {
487                         .idx = idx,
488                 },
489                 .socket = socket,
490         };
491         /* Enable scattered packets support for this queue if necessary. */
492         assert(mb_len >= RTE_PKTMBUF_HEADROOM);
493         if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
494             (mb_len - RTE_PKTMBUF_HEADROOM)) {
495                 ;
496         } else if (dev->data->dev_conf.rxmode.enable_scatter) {
497                 uint32_t size =
498                         RTE_PKTMBUF_HEADROOM +
499                         dev->data->dev_conf.rxmode.max_rx_pkt_len;
500                 uint32_t sges_n;
501
502                 /*
503                  * Determine the number of SGEs needed for a full packet
504                  * and round it to the next power of two.
505                  */
506                 sges_n = rte_log2_u32((size / mb_len) + !!(size % mb_len));
507                 rxq->sges_n = sges_n;
508                 /* Make sure sges_n did not overflow. */
509                 size = mb_len * (1 << rxq->sges_n);
510                 size -= RTE_PKTMBUF_HEADROOM;
511                 if (size < dev->data->dev_conf.rxmode.max_rx_pkt_len) {
512                         rte_errno = EOVERFLOW;
513                         ERROR("%p: too many SGEs (%u) needed to handle"
514                               " requested maximum packet size %u",
515                               (void *)dev,
516                               1 << sges_n,
517                               dev->data->dev_conf.rxmode.max_rx_pkt_len);
518                         goto error;
519                 }
520         } else {
521                 WARN("%p: the requested maximum Rx packet size (%u) is"
522                      " larger than a single mbuf (%u) and scattered"
523                      " mode has not been requested",
524                      (void *)dev,
525                      dev->data->dev_conf.rxmode.max_rx_pkt_len,
526                      mb_len - RTE_PKTMBUF_HEADROOM);
527         }
528         DEBUG("%p: maximum number of segments per packet: %u",
529               (void *)dev, 1 << rxq->sges_n);
530         if (desc % (1 << rxq->sges_n)) {
531                 rte_errno = EINVAL;
532                 ERROR("%p: number of Rx queue descriptors (%u) is not a"
533                       " multiple of maximum segments per packet (%u)",
534                       (void *)dev,
535                       desc,
536                       1 << rxq->sges_n);
537                 goto error;
538         }
539         /* Use the entire Rx mempool as the memory region. */
540         rxq->mr = mlx4_mp2mr(priv->pd, mp);
541         if (!rxq->mr) {
542                 rte_errno = EINVAL;
543                 ERROR("%p: MR creation failure: %s",
544                       (void *)dev, strerror(rte_errno));
545                 goto error;
546         }
547         if (dev->data->dev_conf.intr_conf.rxq) {
548                 rxq->channel = ibv_create_comp_channel(priv->ctx);
549                 if (rxq->channel == NULL) {
550                         rte_errno = ENOMEM;
551                         ERROR("%p: Rx interrupt completion channel creation"
552                               " failure: %s",
553                               (void *)dev, strerror(rte_errno));
554                         goto error;
555                 }
556                 if (mlx4_fd_set_non_blocking(rxq->channel->fd) < 0) {
557                         ERROR("%p: unable to make Rx interrupt completion"
558                               " channel non-blocking: %s",
559                               (void *)dev, strerror(rte_errno));
560                         goto error;
561                 }
562         }
563         rxq->cq = ibv_create_cq(priv->ctx, desc >> rxq->sges_n, NULL,
564                                 rxq->channel, 0);
565         if (!rxq->cq) {
566                 rte_errno = ENOMEM;
567                 ERROR("%p: CQ creation failure: %s",
568                       (void *)dev, strerror(rte_errno));
569                 goto error;
570         }
571         rxq->wq = ibv_create_wq
572                 (priv->ctx,
573                  &(struct ibv_wq_init_attr){
574                         .wq_type = IBV_WQT_RQ,
575                         .max_wr = desc >> rxq->sges_n,
576                         .max_sge = 1 << rxq->sges_n,
577                         .pd = priv->pd,
578                         .cq = rxq->cq,
579                  });
580         if (!rxq->wq) {
581                 rte_errno = errno ? errno : EINVAL;
582                 ERROR("%p: WQ creation failure: %s",
583                       (void *)dev, strerror(rte_errno));
584                 goto error;
585         }
586         ret = ibv_modify_wq
587                 (rxq->wq,
588                  &(struct ibv_wq_attr){
589                         .attr_mask = IBV_WQ_ATTR_STATE,
590                         .wq_state = IBV_WQS_RDY,
591                  });
592         if (ret) {
593                 rte_errno = ret;
594                 ERROR("%p: WQ state to IBV_WPS_RDY failed: %s",
595                       (void *)dev, strerror(rte_errno));
596                 goto error;
597         }
598         /* Retrieve device queue information. */
599         mlxdv.cq.in = rxq->cq;
600         mlxdv.cq.out = &dv_cq;
601         mlxdv.rwq.in = rxq->wq;
602         mlxdv.rwq.out = &dv_rwq;
603         ret = mlx4dv_init_obj(&mlxdv, MLX4DV_OBJ_RWQ | MLX4DV_OBJ_CQ);
604         if (ret) {
605                 rte_errno = EINVAL;
606                 ERROR("%p: failed to obtain device information", (void *)dev);
607                 goto error;
608         }
609         rxq->wqes =
610                 (volatile struct mlx4_wqe_data_seg (*)[])
611                 ((uintptr_t)dv_rwq.buf.buf + dv_rwq.rq.offset);
612         rxq->rq_db = dv_rwq.rdb;
613         rxq->rq_ci = 0;
614         rxq->mcq.buf = dv_cq.buf.buf;
615         rxq->mcq.cqe_cnt = dv_cq.cqe_cnt;
616         rxq->mcq.set_ci_db = dv_cq.set_ci_db;
617         rxq->mcq.cqe_64 = (dv_cq.cqe_size & 64) ? 1 : 0;
618         ret = mlx4_rxq_alloc_elts(rxq);
619         if (ret) {
620                 ERROR("%p: RXQ allocation failed: %s",
621                       (void *)dev, strerror(rte_errno));
622                 goto error;
623         }
624         DEBUG("%p: adding Rx queue %p to list", (void *)dev, (void *)rxq);
625         dev->data->rx_queues[idx] = rxq;
626         /* Update doorbell counter. */
627         rxq->rq_ci = desc >> rxq->sges_n;
628         rte_wmb();
629         *rxq->rq_db = rte_cpu_to_be_32(rxq->rq_ci);
630         return 0;
631 error:
632         dev->data->rx_queues[idx] = NULL;
633         ret = rte_errno;
634         mlx4_rx_queue_release(rxq);
635         rte_errno = ret;
636         assert(rte_errno > 0);
637         return -rte_errno;
638 }
639
640 /**
641  * DPDK callback to release a Rx queue.
642  *
643  * @param dpdk_rxq
644  *   Generic Rx queue pointer.
645  */
646 void
647 mlx4_rx_queue_release(void *dpdk_rxq)
648 {
649         struct rxq *rxq = (struct rxq *)dpdk_rxq;
650         struct priv *priv;
651         unsigned int i;
652
653         if (rxq == NULL)
654                 return;
655         priv = rxq->priv;
656         for (i = 0; i != priv->dev->data->nb_rx_queues; ++i)
657                 if (priv->dev->data->rx_queues[i] == rxq) {
658                         DEBUG("%p: removing Rx queue %p from list",
659                               (void *)priv->dev, (void *)rxq);
660                         priv->dev->data->rx_queues[i] = NULL;
661                         break;
662                 }
663         mlx4_rxq_free_elts(rxq);
664         if (rxq->wq)
665                 claim_zero(ibv_destroy_wq(rxq->wq));
666         if (rxq->cq)
667                 claim_zero(ibv_destroy_cq(rxq->cq));
668         if (rxq->channel)
669                 claim_zero(ibv_destroy_comp_channel(rxq->channel));
670         if (rxq->mr)
671                 claim_zero(ibv_dereg_mr(rxq->mr));
672         rte_free(rxq);
673 }