net/mlx5: handle Rx CQE compression
[dpdk.git] / drivers / net / mlx5 / mlx5_rxq.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2015 6WIND S.A.
5  *   Copyright 2015 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 #include <stddef.h>
35 #include <assert.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <stdint.h>
39
40 /* Verbs header. */
41 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
42 #ifdef PEDANTIC
43 #pragma GCC diagnostic ignored "-pedantic"
44 #endif
45 #include <infiniband/verbs.h>
46 #include <infiniband/arch.h>
47 #include <infiniband/mlx5_hw.h>
48 #ifdef PEDANTIC
49 #pragma GCC diagnostic error "-pedantic"
50 #endif
51
52 /* DPDK headers don't like -pedantic. */
53 #ifdef PEDANTIC
54 #pragma GCC diagnostic ignored "-pedantic"
55 #endif
56 #include <rte_mbuf.h>
57 #include <rte_malloc.h>
58 #include <rte_ethdev.h>
59 #include <rte_common.h>
60 #ifdef PEDANTIC
61 #pragma GCC diagnostic error "-pedantic"
62 #endif
63
64 #include "mlx5.h"
65 #include "mlx5_rxtx.h"
66 #include "mlx5_utils.h"
67 #include "mlx5_autoconf.h"
68 #include "mlx5_defs.h"
69
70 /* Initialization data for hash RX queues. */
71 const struct hash_rxq_init hash_rxq_init[] = {
72         [HASH_RXQ_TCPV4] = {
73                 .hash_fields = (IBV_EXP_RX_HASH_SRC_IPV4 |
74                                 IBV_EXP_RX_HASH_DST_IPV4 |
75                                 IBV_EXP_RX_HASH_SRC_PORT_TCP |
76                                 IBV_EXP_RX_HASH_DST_PORT_TCP),
77                 .dpdk_rss_hf = ETH_RSS_NONFRAG_IPV4_TCP,
78                 .flow_priority = 0,
79                 .flow_spec.tcp_udp = {
80                         .type = IBV_EXP_FLOW_SPEC_TCP,
81                         .size = sizeof(hash_rxq_init[0].flow_spec.tcp_udp),
82                 },
83                 .underlayer = &hash_rxq_init[HASH_RXQ_IPV4],
84         },
85         [HASH_RXQ_UDPV4] = {
86                 .hash_fields = (IBV_EXP_RX_HASH_SRC_IPV4 |
87                                 IBV_EXP_RX_HASH_DST_IPV4 |
88                                 IBV_EXP_RX_HASH_SRC_PORT_UDP |
89                                 IBV_EXP_RX_HASH_DST_PORT_UDP),
90                 .dpdk_rss_hf = ETH_RSS_NONFRAG_IPV4_UDP,
91                 .flow_priority = 0,
92                 .flow_spec.tcp_udp = {
93                         .type = IBV_EXP_FLOW_SPEC_UDP,
94                         .size = sizeof(hash_rxq_init[0].flow_spec.tcp_udp),
95                 },
96                 .underlayer = &hash_rxq_init[HASH_RXQ_IPV4],
97         },
98         [HASH_RXQ_IPV4] = {
99                 .hash_fields = (IBV_EXP_RX_HASH_SRC_IPV4 |
100                                 IBV_EXP_RX_HASH_DST_IPV4),
101                 .dpdk_rss_hf = (ETH_RSS_IPV4 |
102                                 ETH_RSS_FRAG_IPV4),
103                 .flow_priority = 1,
104                 .flow_spec.ipv4 = {
105                         .type = IBV_EXP_FLOW_SPEC_IPV4,
106                         .size = sizeof(hash_rxq_init[0].flow_spec.ipv4),
107                 },
108                 .underlayer = &hash_rxq_init[HASH_RXQ_ETH],
109         },
110         [HASH_RXQ_TCPV6] = {
111                 .hash_fields = (IBV_EXP_RX_HASH_SRC_IPV6 |
112                                 IBV_EXP_RX_HASH_DST_IPV6 |
113                                 IBV_EXP_RX_HASH_SRC_PORT_TCP |
114                                 IBV_EXP_RX_HASH_DST_PORT_TCP),
115                 .dpdk_rss_hf = ETH_RSS_NONFRAG_IPV6_TCP,
116                 .flow_priority = 0,
117                 .flow_spec.tcp_udp = {
118                         .type = IBV_EXP_FLOW_SPEC_TCP,
119                         .size = sizeof(hash_rxq_init[0].flow_spec.tcp_udp),
120                 },
121                 .underlayer = &hash_rxq_init[HASH_RXQ_IPV6],
122         },
123         [HASH_RXQ_UDPV6] = {
124                 .hash_fields = (IBV_EXP_RX_HASH_SRC_IPV6 |
125                                 IBV_EXP_RX_HASH_DST_IPV6 |
126                                 IBV_EXP_RX_HASH_SRC_PORT_UDP |
127                                 IBV_EXP_RX_HASH_DST_PORT_UDP),
128                 .dpdk_rss_hf = ETH_RSS_NONFRAG_IPV6_UDP,
129                 .flow_priority = 0,
130                 .flow_spec.tcp_udp = {
131                         .type = IBV_EXP_FLOW_SPEC_UDP,
132                         .size = sizeof(hash_rxq_init[0].flow_spec.tcp_udp),
133                 },
134                 .underlayer = &hash_rxq_init[HASH_RXQ_IPV6],
135         },
136         [HASH_RXQ_IPV6] = {
137                 .hash_fields = (IBV_EXP_RX_HASH_SRC_IPV6 |
138                                 IBV_EXP_RX_HASH_DST_IPV6),
139                 .dpdk_rss_hf = (ETH_RSS_IPV6 |
140                                 ETH_RSS_FRAG_IPV6),
141                 .flow_priority = 1,
142                 .flow_spec.ipv6 = {
143                         .type = IBV_EXP_FLOW_SPEC_IPV6,
144                         .size = sizeof(hash_rxq_init[0].flow_spec.ipv6),
145                 },
146                 .underlayer = &hash_rxq_init[HASH_RXQ_ETH],
147         },
148         [HASH_RXQ_ETH] = {
149                 .hash_fields = 0,
150                 .dpdk_rss_hf = 0,
151                 .flow_priority = 2,
152                 .flow_spec.eth = {
153                         .type = IBV_EXP_FLOW_SPEC_ETH,
154                         .size = sizeof(hash_rxq_init[0].flow_spec.eth),
155                 },
156                 .underlayer = NULL,
157         },
158 };
159
160 /* Number of entries in hash_rxq_init[]. */
161 const unsigned int hash_rxq_init_n = RTE_DIM(hash_rxq_init);
162
163 /* Initialization data for hash RX queue indirection tables. */
164 static const struct ind_table_init ind_table_init[] = {
165         {
166                 .max_size = -1u, /* Superseded by HW limitations. */
167                 .hash_types =
168                         1 << HASH_RXQ_TCPV4 |
169                         1 << HASH_RXQ_UDPV4 |
170                         1 << HASH_RXQ_IPV4 |
171                         1 << HASH_RXQ_TCPV6 |
172                         1 << HASH_RXQ_UDPV6 |
173                         1 << HASH_RXQ_IPV6 |
174                         0,
175                 .hash_types_n = 6,
176         },
177         {
178                 .max_size = 1,
179                 .hash_types = 1 << HASH_RXQ_ETH,
180                 .hash_types_n = 1,
181         },
182 };
183
184 #define IND_TABLE_INIT_N RTE_DIM(ind_table_init)
185
186 /* Default RSS hash key also used for ConnectX-3. */
187 uint8_t rss_hash_default_key[] = {
188         0x2c, 0xc6, 0x81, 0xd1,
189         0x5b, 0xdb, 0xf4, 0xf7,
190         0xfc, 0xa2, 0x83, 0x19,
191         0xdb, 0x1a, 0x3e, 0x94,
192         0x6b, 0x9e, 0x38, 0xd9,
193         0x2c, 0x9c, 0x03, 0xd1,
194         0xad, 0x99, 0x44, 0xa7,
195         0xd9, 0x56, 0x3d, 0x59,
196         0x06, 0x3c, 0x25, 0xf3,
197         0xfc, 0x1f, 0xdc, 0x2a,
198 };
199
200 /* Length of the default RSS hash key. */
201 const size_t rss_hash_default_key_len = sizeof(rss_hash_default_key);
202
203 /**
204  * Populate flow steering rule for a given hash RX queue type using
205  * information from hash_rxq_init[]. Nothing is written to flow_attr when
206  * flow_attr_size is not large enough, but the required size is still returned.
207  *
208  * @param priv
209  *   Pointer to private structure.
210  * @param[out] flow_attr
211  *   Pointer to flow attribute structure to fill. Note that the allocated
212  *   area must be larger and large enough to hold all flow specifications.
213  * @param flow_attr_size
214  *   Entire size of flow_attr and trailing room for flow specifications.
215  * @param type
216  *   Hash RX queue type to use for flow steering rule.
217  *
218  * @return
219  *   Total size of the flow attribute buffer. No errors are defined.
220  */
221 size_t
222 priv_flow_attr(struct priv *priv, struct ibv_exp_flow_attr *flow_attr,
223                size_t flow_attr_size, enum hash_rxq_type type)
224 {
225         size_t offset = sizeof(*flow_attr);
226         const struct hash_rxq_init *init = &hash_rxq_init[type];
227
228         assert(priv != NULL);
229         assert((size_t)type < RTE_DIM(hash_rxq_init));
230         do {
231                 offset += init->flow_spec.hdr.size;
232                 init = init->underlayer;
233         } while (init != NULL);
234         if (offset > flow_attr_size)
235                 return offset;
236         flow_attr_size = offset;
237         init = &hash_rxq_init[type];
238         *flow_attr = (struct ibv_exp_flow_attr){
239                 .type = IBV_EXP_FLOW_ATTR_NORMAL,
240                 /* Priorities < 3 are reserved for flow director. */
241                 .priority = init->flow_priority + 3,
242                 .num_of_specs = 0,
243                 .port = priv->port,
244                 .flags = 0,
245         };
246         do {
247                 offset -= init->flow_spec.hdr.size;
248                 memcpy((void *)((uintptr_t)flow_attr + offset),
249                        &init->flow_spec,
250                        init->flow_spec.hdr.size);
251                 ++flow_attr->num_of_specs;
252                 init = init->underlayer;
253         } while (init != NULL);
254         return flow_attr_size;
255 }
256
257 /**
258  * Convert hash type position in indirection table initializer to
259  * hash RX queue type.
260  *
261  * @param table
262  *   Indirection table initializer.
263  * @param pos
264  *   Hash type position.
265  *
266  * @return
267  *   Hash RX queue type.
268  */
269 static enum hash_rxq_type
270 hash_rxq_type_from_pos(const struct ind_table_init *table, unsigned int pos)
271 {
272         enum hash_rxq_type type = 0;
273
274         assert(pos < table->hash_types_n);
275         do {
276                 if ((table->hash_types & (1 << type)) && (pos-- == 0))
277                         break;
278                 ++type;
279         } while (1);
280         return type;
281 }
282
283 /**
284  * Filter out disabled hash RX queue types from ind_table_init[].
285  *
286  * @param priv
287  *   Pointer to private structure.
288  * @param[out] table
289  *   Output table.
290  *
291  * @return
292  *   Number of table entries.
293  */
294 static unsigned int
295 priv_make_ind_table_init(struct priv *priv,
296                          struct ind_table_init (*table)[IND_TABLE_INIT_N])
297 {
298         uint64_t rss_hf;
299         unsigned int i;
300         unsigned int j;
301         unsigned int table_n = 0;
302         /* Mandatory to receive frames not handled by normal hash RX queues. */
303         unsigned int hash_types_sup = 1 << HASH_RXQ_ETH;
304
305         rss_hf = priv->rss_hf;
306         /* Process other protocols only if more than one queue. */
307         if (priv->rxqs_n > 1)
308                 for (i = 0; (i != hash_rxq_init_n); ++i)
309                         if (rss_hf & hash_rxq_init[i].dpdk_rss_hf)
310                                 hash_types_sup |= (1 << i);
311
312         /* Filter out entries whose protocols are not in the set. */
313         for (i = 0, j = 0; (i != IND_TABLE_INIT_N); ++i) {
314                 unsigned int nb;
315                 unsigned int h;
316
317                 /* j is increased only if the table has valid protocols. */
318                 assert(j <= i);
319                 (*table)[j] = ind_table_init[i];
320                 (*table)[j].hash_types &= hash_types_sup;
321                 for (h = 0, nb = 0; (h != hash_rxq_init_n); ++h)
322                         if (((*table)[j].hash_types >> h) & 0x1)
323                                 ++nb;
324                 (*table)[i].hash_types_n = nb;
325                 if (nb) {
326                         ++table_n;
327                         ++j;
328                 }
329         }
330         return table_n;
331 }
332
333 /**
334  * Initialize hash RX queues and indirection table.
335  *
336  * @param priv
337  *   Pointer to private structure.
338  *
339  * @return
340  *   0 on success, errno value on failure.
341  */
342 int
343 priv_create_hash_rxqs(struct priv *priv)
344 {
345         struct ibv_exp_wq *wqs[priv->reta_idx_n];
346         struct ind_table_init ind_table_init[IND_TABLE_INIT_N];
347         unsigned int ind_tables_n =
348                 priv_make_ind_table_init(priv, &ind_table_init);
349         unsigned int hash_rxqs_n = 0;
350         struct hash_rxq (*hash_rxqs)[] = NULL;
351         struct ibv_exp_rwq_ind_table *(*ind_tables)[] = NULL;
352         unsigned int i;
353         unsigned int j;
354         unsigned int k;
355         int err = 0;
356
357         assert(priv->ind_tables == NULL);
358         assert(priv->ind_tables_n == 0);
359         assert(priv->hash_rxqs == NULL);
360         assert(priv->hash_rxqs_n == 0);
361         assert(priv->pd != NULL);
362         assert(priv->ctx != NULL);
363         if (priv->rxqs_n == 0)
364                 return EINVAL;
365         assert(priv->rxqs != NULL);
366         if (ind_tables_n == 0) {
367                 ERROR("all hash RX queue types have been filtered out,"
368                       " indirection table cannot be created");
369                 return EINVAL;
370         }
371         if (priv->rxqs_n & (priv->rxqs_n - 1)) {
372                 INFO("%u RX queues are configured, consider rounding this"
373                      " number to the next power of two for better balancing",
374                      priv->rxqs_n);
375                 DEBUG("indirection table extended to assume %u WQs",
376                       priv->reta_idx_n);
377         }
378         for (i = 0; (i != priv->reta_idx_n); ++i) {
379                 struct rxq_ctrl *rxq_ctrl;
380
381                 rxq_ctrl = container_of((*priv->rxqs)[(*priv->reta_idx)[i]],
382                                         struct rxq_ctrl, rxq);
383                 wqs[i] = rxq_ctrl->wq;
384         }
385         /* Get number of hash RX queues to configure. */
386         for (i = 0, hash_rxqs_n = 0; (i != ind_tables_n); ++i)
387                 hash_rxqs_n += ind_table_init[i].hash_types_n;
388         DEBUG("allocating %u hash RX queues for %u WQs, %u indirection tables",
389               hash_rxqs_n, priv->rxqs_n, ind_tables_n);
390         /* Create indirection tables. */
391         ind_tables = rte_calloc(__func__, ind_tables_n,
392                                 sizeof((*ind_tables)[0]), 0);
393         if (ind_tables == NULL) {
394                 err = ENOMEM;
395                 ERROR("cannot allocate indirection tables container: %s",
396                       strerror(err));
397                 goto error;
398         }
399         for (i = 0; (i != ind_tables_n); ++i) {
400                 struct ibv_exp_rwq_ind_table_init_attr ind_init_attr = {
401                         .pd = priv->pd,
402                         .log_ind_tbl_size = 0, /* Set below. */
403                         .ind_tbl = wqs,
404                         .comp_mask = 0,
405                 };
406                 unsigned int ind_tbl_size = ind_table_init[i].max_size;
407                 struct ibv_exp_rwq_ind_table *ind_table;
408
409                 if (priv->reta_idx_n < ind_tbl_size)
410                         ind_tbl_size = priv->reta_idx_n;
411                 ind_init_attr.log_ind_tbl_size = log2above(ind_tbl_size);
412                 errno = 0;
413                 ind_table = ibv_exp_create_rwq_ind_table(priv->ctx,
414                                                          &ind_init_attr);
415                 if (ind_table != NULL) {
416                         (*ind_tables)[i] = ind_table;
417                         continue;
418                 }
419                 /* Not clear whether errno is set. */
420                 err = (errno ? errno : EINVAL);
421                 ERROR("RX indirection table creation failed with error %d: %s",
422                       err, strerror(err));
423                 goto error;
424         }
425         /* Allocate array that holds hash RX queues and related data. */
426         hash_rxqs = rte_calloc(__func__, hash_rxqs_n,
427                                sizeof((*hash_rxqs)[0]), 0);
428         if (hash_rxqs == NULL) {
429                 err = ENOMEM;
430                 ERROR("cannot allocate hash RX queues container: %s",
431                       strerror(err));
432                 goto error;
433         }
434         for (i = 0, j = 0, k = 0;
435              ((i != hash_rxqs_n) && (j != ind_tables_n));
436              ++i) {
437                 struct hash_rxq *hash_rxq = &(*hash_rxqs)[i];
438                 enum hash_rxq_type type =
439                         hash_rxq_type_from_pos(&ind_table_init[j], k);
440                 struct rte_eth_rss_conf *priv_rss_conf =
441                         (*priv->rss_conf)[type];
442                 struct ibv_exp_rx_hash_conf hash_conf = {
443                         .rx_hash_function = IBV_EXP_RX_HASH_FUNC_TOEPLITZ,
444                         .rx_hash_key_len = (priv_rss_conf ?
445                                             priv_rss_conf->rss_key_len :
446                                             rss_hash_default_key_len),
447                         .rx_hash_key = (priv_rss_conf ?
448                                         priv_rss_conf->rss_key :
449                                         rss_hash_default_key),
450                         .rx_hash_fields_mask = hash_rxq_init[type].hash_fields,
451                         .rwq_ind_tbl = (*ind_tables)[j],
452                 };
453                 struct ibv_exp_qp_init_attr qp_init_attr = {
454                         .max_inl_recv = 0, /* Currently not supported. */
455                         .qp_type = IBV_QPT_RAW_PACKET,
456                         .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
457                                       IBV_EXP_QP_INIT_ATTR_RX_HASH),
458                         .pd = priv->pd,
459                         .rx_hash_conf = &hash_conf,
460                         .port_num = priv->port,
461                 };
462
463                 DEBUG("using indirection table %u for hash RX queue %u type %d",
464                       j, i, type);
465                 *hash_rxq = (struct hash_rxq){
466                         .priv = priv,
467                         .qp = ibv_exp_create_qp(priv->ctx, &qp_init_attr),
468                         .type = type,
469                 };
470                 if (hash_rxq->qp == NULL) {
471                         err = (errno ? errno : EINVAL);
472                         ERROR("Hash RX QP creation failure: %s",
473                               strerror(err));
474                         goto error;
475                 }
476                 if (++k < ind_table_init[j].hash_types_n)
477                         continue;
478                 /* Switch to the next indirection table and reset hash RX
479                  * queue type array index. */
480                 ++j;
481                 k = 0;
482         }
483         priv->ind_tables = ind_tables;
484         priv->ind_tables_n = ind_tables_n;
485         priv->hash_rxqs = hash_rxqs;
486         priv->hash_rxqs_n = hash_rxqs_n;
487         assert(err == 0);
488         return 0;
489 error:
490         if (hash_rxqs != NULL) {
491                 for (i = 0; (i != hash_rxqs_n); ++i) {
492                         struct ibv_qp *qp = (*hash_rxqs)[i].qp;
493
494                         if (qp == NULL)
495                                 continue;
496                         claim_zero(ibv_destroy_qp(qp));
497                 }
498                 rte_free(hash_rxqs);
499         }
500         if (ind_tables != NULL) {
501                 for (j = 0; (j != ind_tables_n); ++j) {
502                         struct ibv_exp_rwq_ind_table *ind_table =
503                                 (*ind_tables)[j];
504
505                         if (ind_table == NULL)
506                                 continue;
507                         claim_zero(ibv_exp_destroy_rwq_ind_table(ind_table));
508                 }
509                 rte_free(ind_tables);
510         }
511         return err;
512 }
513
514 /**
515  * Clean up hash RX queues and indirection table.
516  *
517  * @param priv
518  *   Pointer to private structure.
519  */
520 void
521 priv_destroy_hash_rxqs(struct priv *priv)
522 {
523         unsigned int i;
524
525         DEBUG("destroying %u hash RX queues", priv->hash_rxqs_n);
526         if (priv->hash_rxqs_n == 0) {
527                 assert(priv->hash_rxqs == NULL);
528                 assert(priv->ind_tables == NULL);
529                 return;
530         }
531         for (i = 0; (i != priv->hash_rxqs_n); ++i) {
532                 struct hash_rxq *hash_rxq = &(*priv->hash_rxqs)[i];
533                 unsigned int j, k;
534
535                 assert(hash_rxq->priv == priv);
536                 assert(hash_rxq->qp != NULL);
537                 /* Also check that there are no remaining flows. */
538                 for (j = 0; (j != RTE_DIM(hash_rxq->special_flow)); ++j)
539                         for (k = 0;
540                              (k != RTE_DIM(hash_rxq->special_flow[j]));
541                              ++k)
542                                 assert(hash_rxq->special_flow[j][k] == NULL);
543                 for (j = 0; (j != RTE_DIM(hash_rxq->mac_flow)); ++j)
544                         for (k = 0; (k != RTE_DIM(hash_rxq->mac_flow[j])); ++k)
545                                 assert(hash_rxq->mac_flow[j][k] == NULL);
546                 claim_zero(ibv_destroy_qp(hash_rxq->qp));
547         }
548         priv->hash_rxqs_n = 0;
549         rte_free(priv->hash_rxqs);
550         priv->hash_rxqs = NULL;
551         for (i = 0; (i != priv->ind_tables_n); ++i) {
552                 struct ibv_exp_rwq_ind_table *ind_table =
553                         (*priv->ind_tables)[i];
554
555                 assert(ind_table != NULL);
556                 claim_zero(ibv_exp_destroy_rwq_ind_table(ind_table));
557         }
558         priv->ind_tables_n = 0;
559         rte_free(priv->ind_tables);
560         priv->ind_tables = NULL;
561 }
562
563 /**
564  * Check whether a given flow type is allowed.
565  *
566  * @param priv
567  *   Pointer to private structure.
568  * @param type
569  *   Flow type to check.
570  *
571  * @return
572  *   Nonzero if the given flow type is allowed.
573  */
574 int
575 priv_allow_flow_type(struct priv *priv, enum hash_rxq_flow_type type)
576 {
577         /* Only FLOW_TYPE_PROMISC is allowed when promiscuous mode
578          * has been requested. */
579         if (priv->promisc_req)
580                 return type == HASH_RXQ_FLOW_TYPE_PROMISC;
581         switch (type) {
582         case HASH_RXQ_FLOW_TYPE_PROMISC:
583                 return !!priv->promisc_req;
584         case HASH_RXQ_FLOW_TYPE_ALLMULTI:
585                 return !!priv->allmulti_req;
586         case HASH_RXQ_FLOW_TYPE_BROADCAST:
587         case HASH_RXQ_FLOW_TYPE_IPV6MULTI:
588                 /* If allmulti is enabled, broadcast and ipv6multi
589                  * are unnecessary. */
590                 return !priv->allmulti_req;
591         case HASH_RXQ_FLOW_TYPE_MAC:
592                 return 1;
593         default:
594                 /* Unsupported flow type is not allowed. */
595                 return 0;
596         }
597         return 0;
598 }
599
600 /**
601  * Automatically enable/disable flows according to configuration.
602  *
603  * @param priv
604  *   Private structure.
605  *
606  * @return
607  *   0 on success, errno value on failure.
608  */
609 int
610 priv_rehash_flows(struct priv *priv)
611 {
612         unsigned int i;
613
614         for (i = 0; (i != RTE_DIM((*priv->hash_rxqs)[0].special_flow)); ++i)
615                 if (!priv_allow_flow_type(priv, i)) {
616                         priv_special_flow_disable(priv, i);
617                 } else {
618                         int ret = priv_special_flow_enable(priv, i);
619
620                         if (ret)
621                                 return ret;
622                 }
623         if (priv_allow_flow_type(priv, HASH_RXQ_FLOW_TYPE_MAC))
624                 return priv_mac_addrs_enable(priv);
625         priv_mac_addrs_disable(priv);
626         return 0;
627 }
628
629 /**
630  * Allocate RX queue elements.
631  *
632  * @param rxq_ctrl
633  *   Pointer to RX queue structure.
634  * @param elts_n
635  *   Number of elements to allocate.
636  * @param[in] pool
637  *   If not NULL, fetch buffers from this array instead of allocating them
638  *   with rte_pktmbuf_alloc().
639  *
640  * @return
641  *   0 on success, errno value on failure.
642  */
643 static int
644 rxq_alloc_elts(struct rxq_ctrl *rxq_ctrl, unsigned int elts_n,
645                struct rte_mbuf **pool)
646 {
647         unsigned int i;
648         int ret = 0;
649
650         /* For each WR (packet). */
651         for (i = 0; (i != elts_n); ++i) {
652                 struct rte_mbuf *buf;
653                 volatile struct mlx5_wqe_data_seg *scat =
654                         &(*rxq_ctrl->rxq.wqes)[i];
655
656                 if (pool != NULL) {
657                         buf = *(pool++);
658                         assert(buf != NULL);
659                         rte_pktmbuf_reset(buf);
660                 } else
661                         buf = rte_pktmbuf_alloc(rxq_ctrl->rxq.mp);
662                 if (buf == NULL) {
663                         assert(pool == NULL);
664                         ERROR("%p: empty mbuf pool", (void *)rxq_ctrl);
665                         ret = ENOMEM;
666                         goto error;
667                 }
668                 /* Headroom is reserved by rte_pktmbuf_alloc(). */
669                 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
670                 /* Buffer is supposed to be empty. */
671                 assert(rte_pktmbuf_data_len(buf) == 0);
672                 assert(rte_pktmbuf_pkt_len(buf) == 0);
673                 assert(!buf->next);
674                 PORT(buf) = rxq_ctrl->rxq.port_id;
675                 DATA_LEN(buf) = rte_pktmbuf_tailroom(buf);
676                 PKT_LEN(buf) = DATA_LEN(buf);
677                 NB_SEGS(buf) = 1;
678                 /* scat->addr must be able to store a pointer. */
679                 assert(sizeof(scat->addr) >= sizeof(uintptr_t));
680                 *scat = (struct mlx5_wqe_data_seg){
681                         .addr = htonll(rte_pktmbuf_mtod(buf, uintptr_t)),
682                         .byte_count = htonl(DATA_LEN(buf)),
683                         .lkey = htonl(rxq_ctrl->mr->lkey),
684                 };
685                 (*rxq_ctrl->rxq.elts)[i] = buf;
686         }
687         DEBUG("%p: allocated and configured %u single-segment WRs",
688               (void *)rxq_ctrl, elts_n);
689         assert(ret == 0);
690         return 0;
691 error:
692         assert(pool == NULL);
693         elts_n = i;
694         for (i = 0; (i != elts_n); ++i) {
695                 if ((*rxq_ctrl->rxq.elts)[i] != NULL)
696                         rte_pktmbuf_free_seg((*rxq_ctrl->rxq.elts)[i]);
697                 (*rxq_ctrl->rxq.elts)[i] = NULL;
698         }
699         DEBUG("%p: failed, freed everything", (void *)rxq_ctrl);
700         assert(ret > 0);
701         return ret;
702 }
703
704 /**
705  * Free RX queue elements.
706  *
707  * @param rxq_ctrl
708  *   Pointer to RX queue structure.
709  */
710 static void
711 rxq_free_elts(struct rxq_ctrl *rxq_ctrl)
712 {
713         unsigned int i;
714
715         DEBUG("%p: freeing WRs", (void *)rxq_ctrl);
716         if (rxq_ctrl->rxq.elts == NULL)
717                 return;
718
719         for (i = 0; (i != rxq_ctrl->rxq.elts_n); ++i) {
720                 if ((*rxq_ctrl->rxq.elts)[i] != NULL)
721                         rte_pktmbuf_free_seg((*rxq_ctrl->rxq.elts)[i]);
722                 (*rxq_ctrl->rxq.elts)[i] = NULL;
723         }
724 }
725
726 /**
727  * Clean up a RX queue.
728  *
729  * Destroy objects, free allocated memory and reset the structure for reuse.
730  *
731  * @param rxq_ctrl
732  *   Pointer to RX queue structure.
733  */
734 void
735 rxq_cleanup(struct rxq_ctrl *rxq_ctrl)
736 {
737         struct ibv_exp_release_intf_params params;
738
739         DEBUG("cleaning up %p", (void *)rxq_ctrl);
740         rxq_free_elts(rxq_ctrl);
741         if (rxq_ctrl->if_wq != NULL) {
742                 assert(rxq_ctrl->priv != NULL);
743                 assert(rxq_ctrl->priv->ctx != NULL);
744                 assert(rxq_ctrl->wq != NULL);
745                 params = (struct ibv_exp_release_intf_params){
746                         .comp_mask = 0,
747                 };
748                 claim_zero(ibv_exp_release_intf(rxq_ctrl->priv->ctx,
749                                                 rxq_ctrl->if_wq,
750                                                 &params));
751         }
752         if (rxq_ctrl->if_cq != NULL) {
753                 assert(rxq_ctrl->priv != NULL);
754                 assert(rxq_ctrl->priv->ctx != NULL);
755                 assert(rxq_ctrl->cq != NULL);
756                 params = (struct ibv_exp_release_intf_params){
757                         .comp_mask = 0,
758                 };
759                 claim_zero(ibv_exp_release_intf(rxq_ctrl->priv->ctx,
760                                                 rxq_ctrl->if_cq,
761                                                 &params));
762         }
763         if (rxq_ctrl->wq != NULL)
764                 claim_zero(ibv_exp_destroy_wq(rxq_ctrl->wq));
765         if (rxq_ctrl->cq != NULL)
766                 claim_zero(ibv_destroy_cq(rxq_ctrl->cq));
767         if (rxq_ctrl->rd != NULL) {
768                 struct ibv_exp_destroy_res_domain_attr attr = {
769                         .comp_mask = 0,
770                 };
771
772                 assert(rxq_ctrl->priv != NULL);
773                 assert(rxq_ctrl->priv->ctx != NULL);
774                 claim_zero(ibv_exp_destroy_res_domain(rxq_ctrl->priv->ctx,
775                                                       rxq_ctrl->rd,
776                                                       &attr));
777         }
778         if (rxq_ctrl->mr != NULL)
779                 claim_zero(ibv_dereg_mr(rxq_ctrl->mr));
780         memset(rxq_ctrl, 0, sizeof(*rxq_ctrl));
781 }
782
783 /**
784  * Reconfigure a RX queue with new parameters.
785  *
786  * rxq_rehash() does not allocate mbufs, which, if not done from the right
787  * thread (such as a control thread), may corrupt the pool.
788  * In case of failure, the queue is left untouched.
789  *
790  * @param dev
791  *   Pointer to Ethernet device structure.
792  * @param rxq_ctrl
793  *   RX queue pointer.
794  *
795  * @return
796  *   0 on success, errno value on failure.
797  */
798 int
799 rxq_rehash(struct rte_eth_dev *dev, struct rxq_ctrl *rxq_ctrl)
800 {
801         struct priv *priv = rxq_ctrl->priv;
802         struct rxq_ctrl tmpl = *rxq_ctrl;
803         unsigned int mbuf_n;
804         unsigned int desc_n;
805         struct rte_mbuf **pool;
806         unsigned int i, k;
807         struct ibv_exp_wq_attr mod;
808         int err;
809
810         DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq_ctrl);
811         /* Number of descriptors and mbufs currently allocated. */
812         desc_n = tmpl.rxq.elts_n;
813         mbuf_n = desc_n;
814         /* Toggle RX checksum offload if hardware supports it. */
815         if (priv->hw_csum) {
816                 tmpl.rxq.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
817                 rxq_ctrl->rxq.csum = tmpl.rxq.csum;
818         }
819         if (priv->hw_csum_l2tun) {
820                 tmpl.rxq.csum_l2tun =
821                         !!dev->data->dev_conf.rxmode.hw_ip_checksum;
822                 rxq_ctrl->rxq.csum_l2tun = tmpl.rxq.csum_l2tun;
823         }
824         /* From now on, any failure will render the queue unusable.
825          * Reinitialize WQ. */
826         mod = (struct ibv_exp_wq_attr){
827                 .attr_mask = IBV_EXP_WQ_ATTR_STATE,
828                 .wq_state = IBV_EXP_WQS_RESET,
829         };
830         err = ibv_exp_modify_wq(tmpl.wq, &mod);
831         if (err) {
832                 ERROR("%p: cannot reset WQ: %s", (void *)dev, strerror(err));
833                 assert(err > 0);
834                 return err;
835         }
836         /* Allocate pool. */
837         pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
838         if (pool == NULL) {
839                 ERROR("%p: cannot allocate memory", (void *)dev);
840                 return ENOBUFS;
841         }
842         /* Snatch mbufs from original queue. */
843         k = 0;
844         for (i = 0; (i != desc_n); ++i)
845                 pool[k++] = (*rxq_ctrl->rxq.elts)[i];
846         assert(k == mbuf_n);
847         rte_free(pool);
848         /* Change queue state to ready. */
849         mod = (struct ibv_exp_wq_attr){
850                 .attr_mask = IBV_EXP_WQ_ATTR_STATE,
851                 .wq_state = IBV_EXP_WQS_RDY,
852         };
853         err = ibv_exp_modify_wq(tmpl.wq, &mod);
854         if (err) {
855                 ERROR("%p: WQ state to IBV_EXP_WQS_RDY failed: %s",
856                       (void *)dev, strerror(err));
857                 goto error;
858         }
859         /* Post SGEs. */
860         err = rxq_alloc_elts(&tmpl, desc_n, pool);
861         if (err) {
862                 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
863                 rte_free(pool);
864                 assert(err > 0);
865                 return err;
866         }
867         /* Update doorbell counter. */
868         rxq_ctrl->rxq.rq_ci = desc_n;
869         rte_wmb();
870         *rxq_ctrl->rxq.rq_db = htonl(rxq_ctrl->rxq.rq_ci);
871 error:
872         *rxq_ctrl = tmpl;
873         assert(err >= 0);
874         return err;
875 }
876
877 /**
878  * Initialize RX queue.
879  *
880  * @param tmpl
881  *   Pointer to RX queue control template.
882  * @param rxq_ctrl
883  *   Pointer to RX queue control.
884  *
885  * @return
886  *   0 on success, errno value on failure.
887  */
888 static inline int
889 rxq_setup(struct rxq_ctrl *tmpl, struct rxq_ctrl *rxq_ctrl)
890 {
891         struct ibv_cq *ibcq = tmpl->cq;
892         struct mlx5_cq *cq = to_mxxx(cq, cq);
893         struct mlx5_rwq *rwq = container_of(tmpl->wq, struct mlx5_rwq, wq);
894
895         if (cq->cqe_sz != RTE_CACHE_LINE_SIZE) {
896                 ERROR("Wrong MLX5_CQE_SIZE environment variable value: "
897                       "it should be set to %u", RTE_CACHE_LINE_SIZE);
898                 return EINVAL;
899         }
900         tmpl->rxq.rq_db = rwq->rq.db;
901         tmpl->rxq.cqe_n = ibcq->cqe + 1;
902         tmpl->rxq.cq_ci = 0;
903         tmpl->rxq.rq_ci = 0;
904         tmpl->rxq.cq_db = cq->dbrec;
905         tmpl->rxq.wqes =
906                 (volatile struct mlx5_wqe_data_seg (*)[])
907                 (uintptr_t)rwq->rq.buff;
908         tmpl->rxq.cqes =
909                 (volatile struct mlx5_cqe (*)[])
910                 (uintptr_t)cq->active_buf->buf;
911         tmpl->rxq.elts =
912                 (struct rte_mbuf *(*)[tmpl->rxq.elts_n])
913                 ((uintptr_t)rxq_ctrl + sizeof(*rxq_ctrl));
914         return 0;
915 }
916
917 /**
918  * Configure a RX queue.
919  *
920  * @param dev
921  *   Pointer to Ethernet device structure.
922  * @param rxq_ctrl
923  *   Pointer to RX queue structure.
924  * @param desc
925  *   Number of descriptors to configure in queue.
926  * @param socket
927  *   NUMA socket on which memory must be allocated.
928  * @param[in] conf
929  *   Thresholds parameters.
930  * @param mp
931  *   Memory pool for buffer allocations.
932  *
933  * @return
934  *   0 on success, errno value on failure.
935  */
936 int
937 rxq_ctrl_setup(struct rte_eth_dev *dev, struct rxq_ctrl *rxq_ctrl,
938                uint16_t desc, unsigned int socket,
939                const struct rte_eth_rxconf *conf, struct rte_mempool *mp)
940 {
941         struct priv *priv = dev->data->dev_private;
942         struct rxq_ctrl tmpl = {
943                 .priv = priv,
944                 .socket = socket,
945                 .rxq = {
946                         .elts_n = desc,
947                         .mp = mp,
948                 },
949         };
950         struct ibv_exp_wq_attr mod;
951         union {
952                 struct ibv_exp_query_intf_params params;
953                 struct ibv_exp_cq_init_attr cq;
954                 struct ibv_exp_res_domain_init_attr rd;
955                 struct ibv_exp_wq_init_attr wq;
956                 struct ibv_exp_cq_attr cq_attr;
957         } attr;
958         enum ibv_exp_query_intf_status status;
959         unsigned int mb_len = rte_pktmbuf_data_room_size(mp);
960         unsigned int cqe_n = desc - 1;
961         int ret = 0;
962
963         (void)conf; /* Thresholds configuration (ignored). */
964         if (desc == 0) {
965                 ERROR("%p: invalid number of RX descriptors (must be a"
966                       " multiple of 2)", (void *)dev);
967                 return EINVAL;
968         }
969         /* Toggle RX checksum offload if hardware supports it. */
970         if (priv->hw_csum)
971                 tmpl.rxq.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
972         if (priv->hw_csum_l2tun)
973                 tmpl.rxq.csum_l2tun =
974                         !!dev->data->dev_conf.rxmode.hw_ip_checksum;
975         (void)mb_len; /* I'll be back! */
976         /* Use the entire RX mempool as the memory region. */
977         tmpl.mr = mlx5_mp2mr(priv->pd, mp);
978         if (tmpl.mr == NULL) {
979                 ret = EINVAL;
980                 ERROR("%p: MR creation failure: %s",
981                       (void *)dev, strerror(ret));
982                 goto error;
983         }
984         attr.rd = (struct ibv_exp_res_domain_init_attr){
985                 .comp_mask = (IBV_EXP_RES_DOMAIN_THREAD_MODEL |
986                               IBV_EXP_RES_DOMAIN_MSG_MODEL),
987                 .thread_model = IBV_EXP_THREAD_SINGLE,
988                 .msg_model = IBV_EXP_MSG_HIGH_BW,
989         };
990         tmpl.rd = ibv_exp_create_res_domain(priv->ctx, &attr.rd);
991         if (tmpl.rd == NULL) {
992                 ret = ENOMEM;
993                 ERROR("%p: RD creation failure: %s",
994                       (void *)dev, strerror(ret));
995                 goto error;
996         }
997         attr.cq = (struct ibv_exp_cq_init_attr){
998                 .comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
999                 .res_domain = tmpl.rd,
1000         };
1001         if (priv->cqe_comp) {
1002                 attr.cq.comp_mask |= IBV_EXP_CQ_INIT_ATTR_FLAGS;
1003                 attr.cq.flags |= IBV_EXP_CQ_COMPRESSED_CQE;
1004                 cqe_n = (desc * 2) - 1; /* Double the number of CQEs. */
1005         }
1006         tmpl.cq = ibv_exp_create_cq(priv->ctx, cqe_n, NULL, NULL, 0,
1007                                     &attr.cq);
1008         if (tmpl.cq == NULL) {
1009                 ret = ENOMEM;
1010                 ERROR("%p: CQ creation failure: %s",
1011                       (void *)dev, strerror(ret));
1012                 goto error;
1013         }
1014         DEBUG("priv->device_attr.max_qp_wr is %d",
1015               priv->device_attr.max_qp_wr);
1016         DEBUG("priv->device_attr.max_sge is %d",
1017               priv->device_attr.max_sge);
1018         /* Configure VLAN stripping. */
1019         tmpl.rxq.vlan_strip = (priv->hw_vlan_strip &&
1020                                !!dev->data->dev_conf.rxmode.hw_vlan_strip);
1021         attr.wq = (struct ibv_exp_wq_init_attr){
1022                 .wq_context = NULL, /* Could be useful in the future. */
1023                 .wq_type = IBV_EXP_WQT_RQ,
1024                 /* Max number of outstanding WRs. */
1025                 .max_recv_wr = ((priv->device_attr.max_qp_wr < (int)desc) ?
1026                                 priv->device_attr.max_qp_wr :
1027                                 (int)desc),
1028                 /* Max number of scatter/gather elements in a WR. */
1029                 .max_recv_sge = 1,
1030                 .pd = priv->pd,
1031                 .cq = tmpl.cq,
1032                 .comp_mask =
1033                         IBV_EXP_CREATE_WQ_RES_DOMAIN |
1034                         IBV_EXP_CREATE_WQ_VLAN_OFFLOADS |
1035                         0,
1036                 .res_domain = tmpl.rd,
1037                 .vlan_offloads = (tmpl.rxq.vlan_strip ?
1038                                   IBV_EXP_RECEIVE_WQ_CVLAN_STRIP :
1039                                   0),
1040         };
1041         /* By default, FCS (CRC) is stripped by hardware. */
1042         if (dev->data->dev_conf.rxmode.hw_strip_crc) {
1043                 tmpl.rxq.crc_present = 0;
1044         } else if (priv->hw_fcs_strip) {
1045                 /* Ask HW/Verbs to leave CRC in place when supported. */
1046                 attr.wq.flags |= IBV_EXP_CREATE_WQ_FLAG_SCATTER_FCS;
1047                 attr.wq.comp_mask |= IBV_EXP_CREATE_WQ_FLAGS;
1048                 tmpl.rxq.crc_present = 1;
1049         } else {
1050                 WARN("%p: CRC stripping has been disabled but will still"
1051                      " be performed by hardware, make sure MLNX_OFED and"
1052                      " firmware are up to date",
1053                      (void *)dev);
1054                 tmpl.rxq.crc_present = 0;
1055         }
1056         DEBUG("%p: CRC stripping is %s, %u bytes will be subtracted from"
1057               " incoming frames to hide it",
1058               (void *)dev,
1059               tmpl.rxq.crc_present ? "disabled" : "enabled",
1060               tmpl.rxq.crc_present << 2);
1061         if (!mlx5_getenv_int("MLX5_PMD_ENABLE_PADDING"))
1062                 ; /* Nothing else to do. */
1063         else if (priv->hw_padding) {
1064                 INFO("%p: enabling packet padding on queue %p",
1065                      (void *)dev, (void *)rxq_ctrl);
1066                 attr.wq.flags |= IBV_EXP_CREATE_WQ_FLAG_RX_END_PADDING;
1067                 attr.wq.comp_mask |= IBV_EXP_CREATE_WQ_FLAGS;
1068         } else
1069                 WARN("%p: packet padding has been requested but is not"
1070                      " supported, make sure MLNX_OFED and firmware are"
1071                      " up to date",
1072                      (void *)dev);
1073
1074         tmpl.wq = ibv_exp_create_wq(priv->ctx, &attr.wq);
1075         if (tmpl.wq == NULL) {
1076                 ret = (errno ? errno : EINVAL);
1077                 ERROR("%p: WQ creation failure: %s",
1078                       (void *)dev, strerror(ret));
1079                 goto error;
1080         }
1081         /* Save port ID. */
1082         tmpl.rxq.port_id = dev->data->port_id;
1083         DEBUG("%p: RTE port ID: %u", (void *)rxq_ctrl, tmpl.rxq.port_id);
1084         attr.params = (struct ibv_exp_query_intf_params){
1085                 .intf_scope = IBV_EXP_INTF_GLOBAL,
1086                 .intf_version = 1,
1087                 .intf = IBV_EXP_INTF_CQ,
1088                 .obj = tmpl.cq,
1089         };
1090         tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1091         if (tmpl.if_cq == NULL) {
1092                 ERROR("%p: CQ interface family query failed with status %d",
1093                       (void *)dev, status);
1094                 goto error;
1095         }
1096         attr.params = (struct ibv_exp_query_intf_params){
1097                 .intf_scope = IBV_EXP_INTF_GLOBAL,
1098                 .intf = IBV_EXP_INTF_WQ,
1099                 .obj = tmpl.wq,
1100         };
1101         tmpl.if_wq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1102         if (tmpl.if_wq == NULL) {
1103                 ERROR("%p: WQ interface family query failed with status %d",
1104                       (void *)dev, status);
1105                 goto error;
1106         }
1107         /* Change queue state to ready. */
1108         mod = (struct ibv_exp_wq_attr){
1109                 .attr_mask = IBV_EXP_WQ_ATTR_STATE,
1110                 .wq_state = IBV_EXP_WQS_RDY,
1111         };
1112         ret = ibv_exp_modify_wq(tmpl.wq, &mod);
1113         if (ret) {
1114                 ERROR("%p: WQ state to IBV_EXP_WQS_RDY failed: %s",
1115                       (void *)dev, strerror(ret));
1116                 goto error;
1117         }
1118         ret = rxq_setup(&tmpl, rxq_ctrl);
1119         if (ret) {
1120                 ERROR("%p: cannot initialize RX queue structure: %s",
1121                       (void *)dev, strerror(ret));
1122                 goto error;
1123         }
1124         ret = rxq_alloc_elts(&tmpl, desc, NULL);
1125         if (ret) {
1126                 ERROR("%p: RXQ allocation failed: %s",
1127                       (void *)dev, strerror(ret));
1128                 goto error;
1129         }
1130         /* Clean up rxq in case we're reinitializing it. */
1131         DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq_ctrl);
1132         rxq_cleanup(rxq_ctrl);
1133         *rxq_ctrl = tmpl;
1134         /* Update doorbell counter. */
1135         rxq_ctrl->rxq.rq_ci = desc;
1136         rte_wmb();
1137         *rxq_ctrl->rxq.rq_db = htonl(rxq_ctrl->rxq.rq_ci);
1138         DEBUG("%p: rxq updated with %p", (void *)rxq_ctrl, (void *)&tmpl);
1139         assert(ret == 0);
1140         return 0;
1141 error:
1142         rxq_cleanup(&tmpl);
1143         assert(ret > 0);
1144         return ret;
1145 }
1146
1147 /**
1148  * DPDK callback to configure a RX queue.
1149  *
1150  * @param dev
1151  *   Pointer to Ethernet device structure.
1152  * @param idx
1153  *   RX queue index.
1154  * @param desc
1155  *   Number of descriptors to configure in queue.
1156  * @param socket
1157  *   NUMA socket on which memory must be allocated.
1158  * @param[in] conf
1159  *   Thresholds parameters.
1160  * @param mp
1161  *   Memory pool for buffer allocations.
1162  *
1163  * @return
1164  *   0 on success, negative errno value on failure.
1165  */
1166 int
1167 mlx5_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1168                     unsigned int socket, const struct rte_eth_rxconf *conf,
1169                     struct rte_mempool *mp)
1170 {
1171         struct priv *priv = dev->data->dev_private;
1172         struct rxq *rxq = (*priv->rxqs)[idx];
1173         struct rxq_ctrl *rxq_ctrl = container_of(rxq, struct rxq_ctrl, rxq);
1174         int ret;
1175
1176         if (mlx5_is_secondary())
1177                 return -E_RTE_SECONDARY;
1178
1179         priv_lock(priv);
1180         if (!rte_is_power_of_2(desc)) {
1181                 desc = 1 << log2above(desc);
1182                 WARN("%p: increased number of descriptors in RX queue %u"
1183                      " to the next power of two (%d)",
1184                      (void *)dev, idx, desc);
1185         }
1186         DEBUG("%p: configuring queue %u for %u descriptors",
1187               (void *)dev, idx, desc);
1188         if (idx >= priv->rxqs_n) {
1189                 ERROR("%p: queue index out of range (%u >= %u)",
1190                       (void *)dev, idx, priv->rxqs_n);
1191                 priv_unlock(priv);
1192                 return -EOVERFLOW;
1193         }
1194         if (rxq != NULL) {
1195                 DEBUG("%p: reusing already allocated queue index %u (%p)",
1196                       (void *)dev, idx, (void *)rxq);
1197                 if (priv->started) {
1198                         priv_unlock(priv);
1199                         return -EEXIST;
1200                 }
1201                 (*priv->rxqs)[idx] = NULL;
1202                 rxq_cleanup(rxq_ctrl);
1203         } else {
1204                 rxq_ctrl = rte_calloc_socket("RXQ", 1, sizeof(*rxq_ctrl) +
1205                                              desc * sizeof(struct rte_mbuf *),
1206                                              0, socket);
1207                 if (rxq_ctrl == NULL) {
1208                         ERROR("%p: unable to allocate queue index %u",
1209                               (void *)dev, idx);
1210                         priv_unlock(priv);
1211                         return -ENOMEM;
1212                 }
1213         }
1214         ret = rxq_ctrl_setup(dev, rxq_ctrl, desc, socket, conf, mp);
1215         if (ret)
1216                 rte_free(rxq_ctrl);
1217         else {
1218                 rxq_ctrl->rxq.stats.idx = idx;
1219                 DEBUG("%p: adding RX queue %p to list",
1220                       (void *)dev, (void *)rxq_ctrl);
1221                 (*priv->rxqs)[idx] = &rxq_ctrl->rxq;
1222                 /* Update receive callback. */
1223                 dev->rx_pkt_burst = mlx5_rx_burst;
1224         }
1225         priv_unlock(priv);
1226         return -ret;
1227 }
1228
1229 /**
1230  * DPDK callback to release a RX queue.
1231  *
1232  * @param dpdk_rxq
1233  *   Generic RX queue pointer.
1234  */
1235 void
1236 mlx5_rx_queue_release(void *dpdk_rxq)
1237 {
1238         struct rxq *rxq = (struct rxq *)dpdk_rxq;
1239         struct rxq_ctrl *rxq_ctrl;
1240         struct priv *priv;
1241         unsigned int i;
1242
1243         if (mlx5_is_secondary())
1244                 return;
1245
1246         if (rxq == NULL)
1247                 return;
1248         rxq_ctrl = container_of(rxq, struct rxq_ctrl, rxq);
1249         priv = rxq_ctrl->priv;
1250         priv_lock(priv);
1251         for (i = 0; (i != priv->rxqs_n); ++i)
1252                 if ((*priv->rxqs)[i] == rxq) {
1253                         DEBUG("%p: removing RX queue %p from list",
1254                               (void *)priv->dev, (void *)rxq_ctrl);
1255                         (*priv->rxqs)[i] = NULL;
1256                         break;
1257                 }
1258         rxq_cleanup(rxq_ctrl);
1259         rte_free(rxq_ctrl);
1260         priv_unlock(priv);
1261 }
1262
1263 /**
1264  * DPDK callback for RX in secondary processes.
1265  *
1266  * This function configures all queues from primary process information
1267  * if necessary before reverting to the normal RX burst callback.
1268  *
1269  * @param dpdk_rxq
1270  *   Generic pointer to RX queue structure.
1271  * @param[out] pkts
1272  *   Array to store received packets.
1273  * @param pkts_n
1274  *   Maximum number of packets in array.
1275  *
1276  * @return
1277  *   Number of packets successfully received (<= pkts_n).
1278  */
1279 uint16_t
1280 mlx5_rx_burst_secondary_setup(void *dpdk_rxq, struct rte_mbuf **pkts,
1281                               uint16_t pkts_n)
1282 {
1283         struct rxq *rxq = dpdk_rxq;
1284         struct rxq_ctrl *rxq_ctrl = container_of(rxq, struct rxq_ctrl, rxq);
1285         struct priv *priv = mlx5_secondary_data_setup(rxq_ctrl->priv);
1286         struct priv *primary_priv;
1287         unsigned int index;
1288
1289         if (priv == NULL)
1290                 return 0;
1291         primary_priv =
1292                 mlx5_secondary_data[priv->dev->data->port_id].primary_priv;
1293         /* Look for queue index in both private structures. */
1294         for (index = 0; index != priv->rxqs_n; ++index)
1295                 if (((*primary_priv->rxqs)[index] == rxq) ||
1296                     ((*priv->rxqs)[index] == rxq))
1297                         break;
1298         if (index == priv->rxqs_n)
1299                 return 0;
1300         rxq = (*priv->rxqs)[index];
1301         return priv->dev->rx_pkt_burst(rxq, pkts, pkts_n);
1302 }