6be6a0b9a3f5431085f3097d9c16cbb314133617
[dpdk.git] / drivers / net / mlx4 / mlx4_rxq.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017 6WIND S.A.
3  * Copyright 2017 Mellanox Technologies, Ltd
4  */
5
6 /**
7  * @file
8  * Rx queues configuration for mlx4 driver.
9  */
10
11 #include <assert.h>
12 #include <errno.h>
13 #include <stddef.h>
14 #include <stdint.h>
15 #include <string.h>
16
17 /* Verbs headers do not support -pedantic. */
18 #ifdef PEDANTIC
19 #pragma GCC diagnostic ignored "-Wpedantic"
20 #endif
21 #include <infiniband/mlx4dv.h>
22 #include <infiniband/verbs.h>
23 #ifdef PEDANTIC
24 #pragma GCC diagnostic error "-Wpedantic"
25 #endif
26
27 #include <rte_byteorder.h>
28 #include <rte_common.h>
29 #include <rte_errno.h>
30 #include <rte_ethdev_driver.h>
31 #include <rte_flow.h>
32 #include <rte_malloc.h>
33 #include <rte_mbuf.h>
34 #include <rte_mempool.h>
35
36 #include "mlx4.h"
37 #include "mlx4_glue.h"
38 #include "mlx4_flow.h"
39 #include "mlx4_rxtx.h"
40 #include "mlx4_utils.h"
41
42 /**
43  * Historical RSS hash key.
44  *
45  * This used to be the default for mlx4 in Linux before v3.19 switched to
46  * generating random hash keys through netdev_rss_key_fill().
47  *
48  * It is used in this PMD for consistency with past DPDK releases but can
49  * now be overridden through user configuration.
50  *
51  * Note: this is not const to work around API quirks.
52  */
53 uint8_t
54 mlx4_rss_hash_key_default[MLX4_RSS_HASH_KEY_SIZE] = {
55         0x2c, 0xc6, 0x81, 0xd1,
56         0x5b, 0xdb, 0xf4, 0xf7,
57         0xfc, 0xa2, 0x83, 0x19,
58         0xdb, 0x1a, 0x3e, 0x94,
59         0x6b, 0x9e, 0x38, 0xd9,
60         0x2c, 0x9c, 0x03, 0xd1,
61         0xad, 0x99, 0x44, 0xa7,
62         0xd9, 0x56, 0x3d, 0x59,
63         0x06, 0x3c, 0x25, 0xf3,
64         0xfc, 0x1f, 0xdc, 0x2a,
65 };
66
67 /**
68  * Obtain a RSS context with specified properties.
69  *
70  * Used when creating a flow rule targeting one or several Rx queues.
71  *
72  * If a matching RSS context already exists, it is returned with its
73  * reference count incremented.
74  *
75  * @param priv
76  *   Pointer to private structure.
77  * @param fields
78  *   Fields for RSS processing (Verbs format).
79  * @param[in] key
80  *   Hash key to use (whose size is exactly MLX4_RSS_HASH_KEY_SIZE).
81  * @param queues
82  *   Number of target queues.
83  * @param[in] queue_id
84  *   Target queues.
85  *
86  * @return
87  *   Pointer to RSS context on success, NULL otherwise and rte_errno is set.
88  */
89 struct mlx4_rss *
90 mlx4_rss_get(struct priv *priv, uint64_t fields,
91              uint8_t key[MLX4_RSS_HASH_KEY_SIZE],
92              uint16_t queues, const uint16_t queue_id[])
93 {
94         struct mlx4_rss *rss;
95         size_t queue_id_size = sizeof(queue_id[0]) * queues;
96
97         LIST_FOREACH(rss, &priv->rss, next)
98                 if (fields == rss->fields &&
99                     queues == rss->queues &&
100                     !memcmp(key, rss->key, MLX4_RSS_HASH_KEY_SIZE) &&
101                     !memcmp(queue_id, rss->queue_id, queue_id_size)) {
102                         ++rss->refcnt;
103                         return rss;
104                 }
105         rss = rte_malloc(__func__, offsetof(struct mlx4_rss, queue_id) +
106                          queue_id_size, 0);
107         if (!rss)
108                 goto error;
109         *rss = (struct mlx4_rss){
110                 .priv = priv,
111                 .refcnt = 1,
112                 .usecnt = 0,
113                 .qp = NULL,
114                 .ind = NULL,
115                 .fields = fields,
116                 .queues = queues,
117         };
118         memcpy(rss->key, key, MLX4_RSS_HASH_KEY_SIZE);
119         memcpy(rss->queue_id, queue_id, queue_id_size);
120         LIST_INSERT_HEAD(&priv->rss, rss, next);
121         return rss;
122 error:
123         rte_errno = ENOMEM;
124         return NULL;
125 }
126
127 /**
128  * Release a RSS context instance.
129  *
130  * Used when destroying a flow rule targeting one or several Rx queues.
131  *
132  * This function decrements the reference count of the context and destroys
133  * it after reaching 0. The context must have no users at this point; all
134  * prior calls to mlx4_rss_attach() must have been followed by matching
135  * calls to mlx4_rss_detach().
136  *
137  * @param rss
138  *   RSS context to release.
139  */
140 void
141 mlx4_rss_put(struct mlx4_rss *rss)
142 {
143         assert(rss->refcnt);
144         if (--rss->refcnt)
145                 return;
146         assert(!rss->usecnt);
147         assert(!rss->qp);
148         assert(!rss->ind);
149         LIST_REMOVE(rss, next);
150         rte_free(rss);
151 }
152
153 /**
154  * Attach a user to a RSS context instance.
155  *
156  * Used when the RSS QP and indirection table objects must be instantiated,
157  * that is, when a flow rule must be enabled.
158  *
159  * This function increments the usage count of the context.
160  *
161  * @param rss
162  *   RSS context to attach to.
163  *
164  * @return
165  *   0 on success, a negative errno value otherwise and rte_errno is set.
166  */
167 int
168 mlx4_rss_attach(struct mlx4_rss *rss)
169 {
170         assert(rss->refcnt);
171         if (rss->usecnt++) {
172                 assert(rss->qp);
173                 assert(rss->ind);
174                 return 0;
175         }
176
177         struct ibv_wq *ind_tbl[rss->queues];
178         struct priv *priv = rss->priv;
179         const char *msg;
180         unsigned int i = 0;
181         int ret;
182
183         if (!rte_is_power_of_2(RTE_DIM(ind_tbl))) {
184                 ret = EINVAL;
185                 msg = "number of RSS queues must be a power of two";
186                 goto error;
187         }
188         for (i = 0; i != RTE_DIM(ind_tbl); ++i) {
189                 uint16_t id = rss->queue_id[i];
190                 struct rxq *rxq = NULL;
191
192                 if (id < priv->dev->data->nb_rx_queues)
193                         rxq = priv->dev->data->rx_queues[id];
194                 if (!rxq) {
195                         ret = EINVAL;
196                         msg = "RSS target queue is not configured";
197                         goto error;
198                 }
199                 ret = mlx4_rxq_attach(rxq);
200                 if (ret) {
201                         ret = -ret;
202                         msg = "unable to attach RSS target queue";
203                         goto error;
204                 }
205                 ind_tbl[i] = rxq->wq;
206         }
207         rss->ind = mlx4_glue->create_rwq_ind_table
208                 (priv->ctx,
209                  &(struct ibv_rwq_ind_table_init_attr){
210                         .log_ind_tbl_size = rte_log2_u32(RTE_DIM(ind_tbl)),
211                         .ind_tbl = ind_tbl,
212                         .comp_mask = 0,
213                  });
214         if (!rss->ind) {
215                 ret = errno ? errno : EINVAL;
216                 msg = "RSS indirection table creation failure";
217                 goto error;
218         }
219         rss->qp = mlx4_glue->create_qp_ex
220                 (priv->ctx,
221                  &(struct ibv_qp_init_attr_ex){
222                         .comp_mask = (IBV_QP_INIT_ATTR_PD |
223                                       IBV_QP_INIT_ATTR_RX_HASH |
224                                       IBV_QP_INIT_ATTR_IND_TABLE),
225                         .qp_type = IBV_QPT_RAW_PACKET,
226                         .pd = priv->pd,
227                         .rwq_ind_tbl = rss->ind,
228                         .rx_hash_conf = {
229                                 .rx_hash_function = IBV_RX_HASH_FUNC_TOEPLITZ,
230                                 .rx_hash_key_len = MLX4_RSS_HASH_KEY_SIZE,
231                                 .rx_hash_key = rss->key,
232                                 .rx_hash_fields_mask = rss->fields,
233                         },
234                  });
235         if (!rss->qp) {
236                 ret = errno ? errno : EINVAL;
237                 msg = "RSS hash QP creation failure";
238                 goto error;
239         }
240         ret = mlx4_glue->modify_qp
241                 (rss->qp,
242                  &(struct ibv_qp_attr){
243                         .qp_state = IBV_QPS_INIT,
244                         .port_num = priv->port,
245                  },
246                  IBV_QP_STATE | IBV_QP_PORT);
247         if (ret) {
248                 msg = "failed to switch RSS hash QP to INIT state";
249                 goto error;
250         }
251         ret = mlx4_glue->modify_qp
252                 (rss->qp,
253                  &(struct ibv_qp_attr){
254                         .qp_state = IBV_QPS_RTR,
255                  },
256                  IBV_QP_STATE);
257         if (ret) {
258                 msg = "failed to switch RSS hash QP to RTR state";
259                 goto error;
260         }
261         return 0;
262 error:
263         if (rss->qp) {
264                 claim_zero(mlx4_glue->destroy_qp(rss->qp));
265                 rss->qp = NULL;
266         }
267         if (rss->ind) {
268                 claim_zero(mlx4_glue->destroy_rwq_ind_table(rss->ind));
269                 rss->ind = NULL;
270         }
271         while (i--)
272                 mlx4_rxq_detach(priv->dev->data->rx_queues[rss->queue_id[i]]);
273         ERROR("mlx4: %s", msg);
274         --rss->usecnt;
275         rte_errno = ret;
276         return -ret;
277 }
278
279 /**
280  * Detach a user from a RSS context instance.
281  *
282  * Used when disabling (not destroying) a flow rule.
283  *
284  * This function decrements the usage count of the context and destroys
285  * usage resources after reaching 0.
286  *
287  * @param rss
288  *   RSS context to detach from.
289  */
290 void
291 mlx4_rss_detach(struct mlx4_rss *rss)
292 {
293         struct priv *priv = rss->priv;
294         unsigned int i;
295
296         assert(rss->refcnt);
297         assert(rss->qp);
298         assert(rss->ind);
299         if (--rss->usecnt)
300                 return;
301         claim_zero(mlx4_glue->destroy_qp(rss->qp));
302         rss->qp = NULL;
303         claim_zero(mlx4_glue->destroy_rwq_ind_table(rss->ind));
304         rss->ind = NULL;
305         for (i = 0; i != rss->queues; ++i)
306                 mlx4_rxq_detach(priv->dev->data->rx_queues[rss->queue_id[i]]);
307 }
308
309 /**
310  * Initialize common RSS context resources.
311  *
312  * Because ConnectX-3 hardware limitations require a fixed order in the
313  * indirection table, WQs must be allocated sequentially to be part of a
314  * common RSS context.
315  *
316  * Since a newly created WQ cannot be moved to a different context, this
317  * function allocates them all at once, one for each configured Rx queue,
318  * as well as all related resources (CQs and mbufs).
319  *
320  * This must therefore be done before creating any Rx flow rules relying on
321  * indirection tables.
322  *
323  * @param priv
324  *   Pointer to private structure.
325  *
326  * @return
327  *   0 on success, a negative errno value otherwise and rte_errno is set.
328  */
329 int
330 mlx4_rss_init(struct priv *priv)
331 {
332         struct rte_eth_dev *dev = priv->dev;
333         uint8_t log2_range = rte_log2_u32(dev->data->nb_rx_queues);
334         uint32_t wq_num_prev = 0;
335         const char *msg;
336         unsigned int i;
337         int ret;
338
339         /* Prepare range for RSS contexts before creating the first WQ. */
340         ret = mlx4_glue->dv_set_context_attr
341                 (priv->ctx,
342                  MLX4DV_SET_CTX_ATTR_LOG_WQS_RANGE_SZ,
343                  &log2_range);
344         if (ret) {
345                 ERROR("cannot set up range size for RSS context to %u"
346                       " (for %u Rx queues), error: %s",
347                       1 << log2_range, dev->data->nb_rx_queues, strerror(ret));
348                 rte_errno = ret;
349                 return -ret;
350         }
351         for (i = 0; i != priv->dev->data->nb_rx_queues; ++i) {
352                 struct rxq *rxq = priv->dev->data->rx_queues[i];
353                 struct ibv_cq *cq;
354                 struct ibv_wq *wq;
355                 uint32_t wq_num;
356
357                 /* Attach the configured Rx queues. */
358                 if (rxq) {
359                         assert(!rxq->usecnt);
360                         ret = mlx4_rxq_attach(rxq);
361                         if (!ret) {
362                                 wq_num = rxq->wq->wq_num;
363                                 goto wq_num_check;
364                         }
365                         ret = -ret;
366                         msg = "unable to create Rx queue resources";
367                         goto error;
368                 }
369                 /*
370                  * WQs are temporarily allocated for unconfigured Rx queues
371                  * to maintain proper index alignment in indirection table
372                  * by skipping unused WQ numbers.
373                  *
374                  * The reason this works at all even though these WQs are
375                  * immediately destroyed is that WQNs are allocated
376                  * sequentially and are guaranteed to never be reused in the
377                  * same context by the underlying implementation.
378                  */
379                 cq = mlx4_glue->create_cq(priv->ctx, 1, NULL, NULL, 0);
380                 if (!cq) {
381                         ret = ENOMEM;
382                         msg = "placeholder CQ creation failure";
383                         goto error;
384                 }
385                 wq = mlx4_glue->create_wq
386                         (priv->ctx,
387                          &(struct ibv_wq_init_attr){
388                                 .wq_type = IBV_WQT_RQ,
389                                 .max_wr = 1,
390                                 .max_sge = 1,
391                                 .pd = priv->pd,
392                                 .cq = cq,
393                          });
394                 if (wq) {
395                         wq_num = wq->wq_num;
396                         claim_zero(mlx4_glue->destroy_wq(wq));
397                 } else {
398                         wq_num = 0; /* Shut up GCC 4.8 warnings. */
399                 }
400                 claim_zero(mlx4_glue->destroy_cq(cq));
401                 if (!wq) {
402                         ret = ENOMEM;
403                         msg = "placeholder WQ creation failure";
404                         goto error;
405                 }
406 wq_num_check:
407                 /*
408                  * While guaranteed by the implementation, make sure WQ
409                  * numbers are really sequential (as the saying goes,
410                  * trust, but verify).
411                  */
412                 if (i && wq_num - wq_num_prev != 1) {
413                         if (rxq)
414                                 mlx4_rxq_detach(rxq);
415                         ret = ERANGE;
416                         msg = "WQ numbers are not sequential";
417                         goto error;
418                 }
419                 wq_num_prev = wq_num;
420         }
421         return 0;
422 error:
423         ERROR("cannot initialize common RSS resources (queue %u): %s: %s",
424               i, msg, strerror(ret));
425         while (i--) {
426                 struct rxq *rxq = priv->dev->data->rx_queues[i];
427
428                 if (rxq)
429                         mlx4_rxq_detach(rxq);
430         }
431         rte_errno = ret;
432         return -ret;
433 }
434
435 /**
436  * Release common RSS context resources.
437  *
438  * As the reverse of mlx4_rss_init(), this must be done after removing all
439  * flow rules relying on indirection tables.
440  *
441  * @param priv
442  *   Pointer to private structure.
443  */
444 void
445 mlx4_rss_deinit(struct priv *priv)
446 {
447         unsigned int i;
448
449         for (i = 0; i != priv->dev->data->nb_rx_queues; ++i) {
450                 struct rxq *rxq = priv->dev->data->rx_queues[i];
451
452                 if (rxq) {
453                         assert(rxq->usecnt == 1);
454                         mlx4_rxq_detach(rxq);
455                 }
456         }
457 }
458
459 /**
460  * Attach a user to a Rx queue.
461  *
462  * Used when the resources of an Rx queue must be instantiated for it to
463  * become in a usable state.
464  *
465  * This function increments the usage count of the Rx queue.
466  *
467  * @param rxq
468  *   Pointer to Rx queue structure.
469  *
470  * @return
471  *   0 on success, negative errno value otherwise and rte_errno is set.
472  */
473 int
474 mlx4_rxq_attach(struct rxq *rxq)
475 {
476         if (rxq->usecnt++) {
477                 assert(rxq->cq);
478                 assert(rxq->wq);
479                 assert(rxq->wqes);
480                 assert(rxq->rq_db);
481                 return 0;
482         }
483
484         struct priv *priv = rxq->priv;
485         const uint32_t elts_n = 1 << rxq->elts_n;
486         const uint32_t sges_n = 1 << rxq->sges_n;
487         struct rte_mbuf *(*elts)[elts_n] = rxq->elts;
488         struct mlx4dv_obj mlxdv;
489         struct mlx4dv_rwq dv_rwq;
490         struct mlx4dv_cq dv_cq = { .comp_mask = MLX4DV_CQ_MASK_UAR, };
491         const char *msg;
492         struct ibv_cq *cq = NULL;
493         struct ibv_wq *wq = NULL;
494         uint32_t create_flags = 0;
495         uint32_t comp_mask = 0;
496         volatile struct mlx4_wqe_data_seg (*wqes)[];
497         unsigned int i;
498         int ret;
499
500         assert(rte_is_power_of_2(elts_n));
501         cq = mlx4_glue->create_cq(priv->ctx, elts_n / sges_n, NULL,
502                                   rxq->channel, 0);
503         if (!cq) {
504                 ret = ENOMEM;
505                 msg = "CQ creation failure";
506                 goto error;
507         }
508         /* By default, FCS (CRC) is stripped by hardware. */
509         if (rxq->crc_present) {
510                 create_flags |= IBV_WQ_FLAGS_SCATTER_FCS;
511                 comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
512         }
513         wq = mlx4_glue->create_wq
514                 (priv->ctx,
515                  &(struct ibv_wq_init_attr){
516                         .wq_type = IBV_WQT_RQ,
517                         .max_wr = elts_n / sges_n,
518                         .max_sge = sges_n,
519                         .pd = priv->pd,
520                         .cq = cq,
521                         .comp_mask = comp_mask,
522                         .create_flags = create_flags,
523                  });
524         if (!wq) {
525                 ret = errno ? errno : EINVAL;
526                 msg = "WQ creation failure";
527                 goto error;
528         }
529         ret = mlx4_glue->modify_wq
530                 (wq,
531                  &(struct ibv_wq_attr){
532                         .attr_mask = IBV_WQ_ATTR_STATE,
533                         .wq_state = IBV_WQS_RDY,
534                  });
535         if (ret) {
536                 msg = "WQ state change to IBV_WQS_RDY failed";
537                 goto error;
538         }
539         /* Retrieve device queue information. */
540         mlxdv.cq.in = cq;
541         mlxdv.cq.out = &dv_cq;
542         mlxdv.rwq.in = wq;
543         mlxdv.rwq.out = &dv_rwq;
544         ret = mlx4_glue->dv_init_obj(&mlxdv, MLX4DV_OBJ_RWQ | MLX4DV_OBJ_CQ);
545         if (ret) {
546                 msg = "failed to obtain device information from WQ/CQ objects";
547                 goto error;
548         }
549         wqes = (volatile struct mlx4_wqe_data_seg (*)[])
550                 ((uintptr_t)dv_rwq.buf.buf + dv_rwq.rq.offset);
551         for (i = 0; i != RTE_DIM(*elts); ++i) {
552                 volatile struct mlx4_wqe_data_seg *scat = &(*wqes)[i];
553                 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
554
555                 if (buf == NULL) {
556                         while (i--) {
557                                 rte_pktmbuf_free_seg((*elts)[i]);
558                                 (*elts)[i] = NULL;
559                         }
560                         ret = ENOMEM;
561                         msg = "cannot allocate mbuf";
562                         goto error;
563                 }
564                 /* Headroom is reserved by rte_pktmbuf_alloc(). */
565                 assert(buf->data_off == RTE_PKTMBUF_HEADROOM);
566                 /* Buffer is supposed to be empty. */
567                 assert(rte_pktmbuf_data_len(buf) == 0);
568                 assert(rte_pktmbuf_pkt_len(buf) == 0);
569                 /* Only the first segment keeps headroom. */
570                 if (i % sges_n)
571                         buf->data_off = 0;
572                 buf->port = rxq->port_id;
573                 buf->data_len = rte_pktmbuf_tailroom(buf);
574                 buf->pkt_len = rte_pktmbuf_tailroom(buf);
575                 buf->nb_segs = 1;
576                 *scat = (struct mlx4_wqe_data_seg){
577                         .addr = rte_cpu_to_be_64(rte_pktmbuf_mtod(buf,
578                                                                   uintptr_t)),
579                         .byte_count = rte_cpu_to_be_32(buf->data_len),
580                         .lkey = rte_cpu_to_be_32(rxq->mr->lkey),
581                 };
582                 (*elts)[i] = buf;
583         }
584         DEBUG("%p: allocated and configured %u segments (max %u packets)",
585               (void *)rxq, elts_n, elts_n / sges_n);
586         rxq->cq = cq;
587         rxq->wq = wq;
588         rxq->wqes = wqes;
589         rxq->rq_db = dv_rwq.rdb;
590         rxq->mcq.buf = dv_cq.buf.buf;
591         rxq->mcq.cqe_cnt = dv_cq.cqe_cnt;
592         rxq->mcq.set_ci_db = dv_cq.set_ci_db;
593         rxq->mcq.cqe_64 = (dv_cq.cqe_size & 64) ? 1 : 0;
594         rxq->mcq.arm_db = dv_cq.arm_db;
595         rxq->mcq.arm_sn = dv_cq.arm_sn;
596         rxq->mcq.cqn = dv_cq.cqn;
597         rxq->mcq.cq_uar = dv_cq.cq_uar;
598         rxq->mcq.cq_db_reg = (uint8_t *)dv_cq.cq_uar + MLX4_CQ_DOORBELL;
599         /* Update doorbell counter. */
600         rxq->rq_ci = elts_n / sges_n;
601         rte_wmb();
602         *rxq->rq_db = rte_cpu_to_be_32(rxq->rq_ci);
603         return 0;
604 error:
605         if (wq)
606                 claim_zero(mlx4_glue->destroy_wq(wq));
607         if (cq)
608                 claim_zero(mlx4_glue->destroy_cq(cq));
609         rte_errno = ret;
610         ERROR("error while attaching Rx queue %p: %s: %s",
611               (void *)rxq, msg, strerror(ret));
612         return -ret;
613 }
614
615 /**
616  * Detach a user from a Rx queue.
617  *
618  * This function decrements the usage count of the Rx queue and destroys
619  * usage resources after reaching 0.
620  *
621  * @param rxq
622  *   Pointer to Rx queue structure.
623  */
624 void
625 mlx4_rxq_detach(struct rxq *rxq)
626 {
627         unsigned int i;
628         struct rte_mbuf *(*elts)[1 << rxq->elts_n] = rxq->elts;
629
630         if (--rxq->usecnt)
631                 return;
632         rxq->rq_ci = 0;
633         memset(&rxq->mcq, 0, sizeof(rxq->mcq));
634         rxq->rq_db = NULL;
635         rxq->wqes = NULL;
636         claim_zero(mlx4_glue->destroy_wq(rxq->wq));
637         rxq->wq = NULL;
638         claim_zero(mlx4_glue->destroy_cq(rxq->cq));
639         rxq->cq = NULL;
640         DEBUG("%p: freeing Rx queue elements", (void *)rxq);
641         for (i = 0; (i != RTE_DIM(*elts)); ++i) {
642                 if (!(*elts)[i])
643                         continue;
644                 rte_pktmbuf_free_seg((*elts)[i]);
645                 (*elts)[i] = NULL;
646         }
647 }
648
649 /**
650  * Returns the per-queue supported offloads.
651  *
652  * @param priv
653  *   Pointer to private structure.
654  *
655  * @return
656  *   Supported Tx offloads.
657  */
658 uint64_t
659 mlx4_get_rx_queue_offloads(struct priv *priv)
660 {
661         uint64_t offloads = DEV_RX_OFFLOAD_SCATTER;
662
663         if (priv->hw_fcs_strip)
664                 offloads |= DEV_RX_OFFLOAD_CRC_STRIP;
665         if (priv->hw_csum)
666                 offloads |= DEV_RX_OFFLOAD_CHECKSUM;
667         return offloads;
668 }
669
670 /**
671  * Returns the per-port supported offloads.
672  *
673  * @param priv
674  *   Pointer to private structure.
675  *
676  * @return
677  *   Supported Rx offloads.
678  */
679 uint64_t
680 mlx4_get_rx_port_offloads(struct priv *priv)
681 {
682         uint64_t offloads = DEV_RX_OFFLOAD_VLAN_FILTER;
683
684         (void)priv;
685         return offloads;
686 }
687
688 /**
689  * Checks if the per-queue offload configuration is valid.
690  *
691  * @param priv
692  *   Pointer to private structure.
693  * @param requested
694  *   Per-queue offloads configuration.
695  *
696  * @return
697  *   Nonzero when configuration is valid.
698  */
699 static int
700 mlx4_check_rx_queue_offloads(struct priv *priv, uint64_t requested)
701 {
702         uint64_t mandatory = priv->dev->data->dev_conf.rxmode.offloads;
703         uint64_t supported = mlx4_get_rx_port_offloads(priv);
704
705         return !((mandatory ^ requested) & supported);
706 }
707
708 /**
709  * DPDK callback to configure a Rx queue.
710  *
711  * @param dev
712  *   Pointer to Ethernet device structure.
713  * @param idx
714  *   Rx queue index.
715  * @param desc
716  *   Number of descriptors to configure in queue.
717  * @param socket
718  *   NUMA socket on which memory must be allocated.
719  * @param[in] conf
720  *   Thresholds parameters.
721  * @param mp
722  *   Memory pool for buffer allocations.
723  *
724  * @return
725  *   0 on success, negative errno value otherwise and rte_errno is set.
726  */
727 int
728 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
729                     unsigned int socket, const struct rte_eth_rxconf *conf,
730                     struct rte_mempool *mp)
731 {
732         struct priv *priv = dev->data->dev_private;
733         uint32_t mb_len = rte_pktmbuf_data_room_size(mp);
734         struct rte_mbuf *(*elts)[rte_align32pow2(desc)];
735         struct rxq *rxq;
736         struct mlx4_malloc_vec vec[] = {
737                 {
738                         .align = RTE_CACHE_LINE_SIZE,
739                         .size = sizeof(*rxq),
740                         .addr = (void **)&rxq,
741                 },
742                 {
743                         .align = RTE_CACHE_LINE_SIZE,
744                         .size = sizeof(*elts),
745                         .addr = (void **)&elts,
746                 },
747         };
748         int ret;
749         uint32_t crc_present;
750
751         (void)conf; /* Thresholds configuration (ignored). */
752         DEBUG("%p: configuring queue %u for %u descriptors",
753               (void *)dev, idx, desc);
754         if (!mlx4_check_rx_queue_offloads(priv, conf->offloads)) {
755                 rte_errno = ENOTSUP;
756                 ERROR("%p: Rx queue offloads 0x%" PRIx64 " don't match port "
757                       "offloads 0x%" PRIx64 " or supported offloads 0x%" PRIx64,
758                       (void *)dev, conf->offloads,
759                       dev->data->dev_conf.rxmode.offloads,
760                       (mlx4_get_rx_port_offloads(priv) |
761                        mlx4_get_rx_queue_offloads(priv)));
762                 return -rte_errno;
763         }
764         if (idx >= dev->data->nb_rx_queues) {
765                 rte_errno = EOVERFLOW;
766                 ERROR("%p: queue index out of range (%u >= %u)",
767                       (void *)dev, idx, dev->data->nb_rx_queues);
768                 return -rte_errno;
769         }
770         rxq = dev->data->rx_queues[idx];
771         if (rxq) {
772                 rte_errno = EEXIST;
773                 ERROR("%p: Rx queue %u already configured, release it first",
774                       (void *)dev, idx);
775                 return -rte_errno;
776         }
777         if (!desc) {
778                 rte_errno = EINVAL;
779                 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
780                 return -rte_errno;
781         }
782         if (desc != RTE_DIM(*elts)) {
783                 desc = RTE_DIM(*elts);
784                 WARN("%p: increased number of descriptors in Rx queue %u"
785                      " to the next power of two (%u)",
786                      (void *)dev, idx, desc);
787         }
788         /* By default, FCS (CRC) is stripped by hardware. */
789         if (conf->offloads & DEV_RX_OFFLOAD_CRC_STRIP) {
790                 crc_present = 0;
791         } else if (priv->hw_fcs_strip) {
792                 crc_present = 1;
793         } else {
794                 WARN("%p: CRC stripping has been disabled but will still"
795                      " be performed by hardware, make sure MLNX_OFED and"
796                      " firmware are up to date",
797                      (void *)dev);
798                 crc_present = 0;
799         }
800         DEBUG("%p: CRC stripping is %s, %u bytes will be subtracted from"
801               " incoming frames to hide it",
802               (void *)dev,
803               crc_present ? "disabled" : "enabled",
804               crc_present << 2);
805         /* Allocate and initialize Rx queue. */
806         mlx4_zmallocv_socket("RXQ", vec, RTE_DIM(vec), socket);
807         if (!rxq) {
808                 ERROR("%p: unable to allocate queue index %u",
809                       (void *)dev, idx);
810                 return -rte_errno;
811         }
812         *rxq = (struct rxq){
813                 .priv = priv,
814                 .mp = mp,
815                 .port_id = dev->data->port_id,
816                 .sges_n = 0,
817                 .elts_n = rte_log2_u32(desc),
818                 .elts = elts,
819                 /* Toggle Rx checksum offload if hardware supports it. */
820                 .csum = priv->hw_csum &&
821                         (conf->offloads & DEV_RX_OFFLOAD_CHECKSUM),
822                 .csum_l2tun = priv->hw_csum_l2tun &&
823                               (conf->offloads & DEV_RX_OFFLOAD_CHECKSUM),
824                 .crc_present = crc_present,
825                 .l2tun_offload = priv->hw_csum_l2tun,
826                 .stats = {
827                         .idx = idx,
828                 },
829                 .socket = socket,
830         };
831         /* Enable scattered packets support for this queue if necessary. */
832         assert(mb_len >= RTE_PKTMBUF_HEADROOM);
833         if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
834             (mb_len - RTE_PKTMBUF_HEADROOM)) {
835                 ;
836         } else if (conf->offloads & DEV_RX_OFFLOAD_SCATTER) {
837                 uint32_t size =
838                         RTE_PKTMBUF_HEADROOM +
839                         dev->data->dev_conf.rxmode.max_rx_pkt_len;
840                 uint32_t sges_n;
841
842                 /*
843                  * Determine the number of SGEs needed for a full packet
844                  * and round it to the next power of two.
845                  */
846                 sges_n = rte_log2_u32((size / mb_len) + !!(size % mb_len));
847                 rxq->sges_n = sges_n;
848                 /* Make sure sges_n did not overflow. */
849                 size = mb_len * (1 << rxq->sges_n);
850                 size -= RTE_PKTMBUF_HEADROOM;
851                 if (size < dev->data->dev_conf.rxmode.max_rx_pkt_len) {
852                         rte_errno = EOVERFLOW;
853                         ERROR("%p: too many SGEs (%u) needed to handle"
854                               " requested maximum packet size %u",
855                               (void *)dev,
856                               1 << sges_n,
857                               dev->data->dev_conf.rxmode.max_rx_pkt_len);
858                         goto error;
859                 }
860         } else {
861                 WARN("%p: the requested maximum Rx packet size (%u) is"
862                      " larger than a single mbuf (%u) and scattered"
863                      " mode has not been requested",
864                      (void *)dev,
865                      dev->data->dev_conf.rxmode.max_rx_pkt_len,
866                      mb_len - RTE_PKTMBUF_HEADROOM);
867         }
868         DEBUG("%p: maximum number of segments per packet: %u",
869               (void *)dev, 1 << rxq->sges_n);
870         if (desc % (1 << rxq->sges_n)) {
871                 rte_errno = EINVAL;
872                 ERROR("%p: number of Rx queue descriptors (%u) is not a"
873                       " multiple of maximum segments per packet (%u)",
874                       (void *)dev,
875                       desc,
876                       1 << rxq->sges_n);
877                 goto error;
878         }
879         /* Use the entire Rx mempool as the memory region. */
880         rxq->mr = mlx4_mr_get(priv, mp);
881         if (!rxq->mr) {
882                 ERROR("%p: MR creation failure: %s",
883                       (void *)dev, strerror(rte_errno));
884                 goto error;
885         }
886         if (dev->data->dev_conf.intr_conf.rxq) {
887                 rxq->channel = mlx4_glue->create_comp_channel(priv->ctx);
888                 if (rxq->channel == NULL) {
889                         rte_errno = ENOMEM;
890                         ERROR("%p: Rx interrupt completion channel creation"
891                               " failure: %s",
892                               (void *)dev, strerror(rte_errno));
893                         goto error;
894                 }
895                 if (mlx4_fd_set_non_blocking(rxq->channel->fd) < 0) {
896                         ERROR("%p: unable to make Rx interrupt completion"
897                               " channel non-blocking: %s",
898                               (void *)dev, strerror(rte_errno));
899                         goto error;
900                 }
901         }
902         DEBUG("%p: adding Rx queue %p to list", (void *)dev, (void *)rxq);
903         dev->data->rx_queues[idx] = rxq;
904         return 0;
905 error:
906         dev->data->rx_queues[idx] = NULL;
907         ret = rte_errno;
908         mlx4_rx_queue_release(rxq);
909         rte_errno = ret;
910         assert(rte_errno > 0);
911         return -rte_errno;
912 }
913
914 /**
915  * DPDK callback to release a Rx queue.
916  *
917  * @param dpdk_rxq
918  *   Generic Rx queue pointer.
919  */
920 void
921 mlx4_rx_queue_release(void *dpdk_rxq)
922 {
923         struct rxq *rxq = (struct rxq *)dpdk_rxq;
924         struct priv *priv;
925         unsigned int i;
926
927         if (rxq == NULL)
928                 return;
929         priv = rxq->priv;
930         for (i = 0; i != priv->dev->data->nb_rx_queues; ++i)
931                 if (priv->dev->data->rx_queues[i] == rxq) {
932                         DEBUG("%p: removing Rx queue %p from list",
933                               (void *)priv->dev, (void *)rxq);
934                         priv->dev->data->rx_queues[i] = NULL;
935                         break;
936                 }
937         assert(!rxq->cq);
938         assert(!rxq->wq);
939         assert(!rxq->wqes);
940         assert(!rxq->rq_db);
941         if (rxq->channel)
942                 claim_zero(mlx4_glue->destroy_comp_channel(rxq->channel));
943         if (rxq->mr)
944                 mlx4_mr_put(rxq->mr);
945         rte_free(rxq);
946 }