net/mlx5: fix tunnel offload detection
[dpdk.git] / drivers / net / mlx5 / mlx5.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 <unistd.h>
36 #include <string.h>
37 #include <assert.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <errno.h>
41 #include <net/if.h>
42
43 /* Verbs header. */
44 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
45 #ifdef PEDANTIC
46 #pragma GCC diagnostic ignored "-Wpedantic"
47 #endif
48 #include <infiniband/verbs.h>
49 #ifdef PEDANTIC
50 #pragma GCC diagnostic error "-Wpedantic"
51 #endif
52
53 #include <rte_malloc.h>
54 #include <rte_ethdev.h>
55 #include <rte_ethdev_pci.h>
56 #include <rte_pci.h>
57 #include <rte_common.h>
58 #include <rte_kvargs.h>
59
60 #include "mlx5.h"
61 #include "mlx5_utils.h"
62 #include "mlx5_rxtx.h"
63 #include "mlx5_autoconf.h"
64 #include "mlx5_defs.h"
65
66 /* Device parameter to enable RX completion queue compression. */
67 #define MLX5_RXQ_CQE_COMP_EN "rxq_cqe_comp_en"
68
69 /* Device parameter to configure inline send. */
70 #define MLX5_TXQ_INLINE "txq_inline"
71
72 /*
73  * Device parameter to configure the number of TX queues threshold for
74  * enabling inline send.
75  */
76 #define MLX5_TXQS_MIN_INLINE "txqs_min_inline"
77
78 /* Device parameter to enable multi-packet send WQEs. */
79 #define MLX5_TXQ_MPW_EN "txq_mpw_en"
80
81 /* Device parameter to include 2 dsegs in the title WQEBB. */
82 #define MLX5_TXQ_MPW_HDR_DSEG_EN "txq_mpw_hdr_dseg_en"
83
84 /* Device parameter to limit the size of inlining packet. */
85 #define MLX5_TXQ_MAX_INLINE_LEN "txq_max_inline_len"
86
87 /* Device parameter to enable hardware TSO offload. */
88 #define MLX5_TSO "tso"
89
90 /* Device parameter to enable hardware Tx vector. */
91 #define MLX5_TX_VEC_EN "tx_vec_en"
92
93 /* Device parameter to enable hardware Rx vector. */
94 #define MLX5_RX_VEC_EN "rx_vec_en"
95
96 /* Default PMD specific parameter value. */
97 #define MLX5_ARG_UNSET (-1)
98
99 struct mlx5_args {
100         int cqe_comp;
101         int txq_inline;
102         int txqs_inline;
103         int mps;
104         int mpw_hdr_dseg;
105         int inline_max_packet_sz;
106         int tso;
107         int tx_vec_en;
108         int rx_vec_en;
109 };
110 /**
111  * Retrieve integer value from environment variable.
112  *
113  * @param[in] name
114  *   Environment variable name.
115  *
116  * @return
117  *   Integer value, 0 if the variable is not set.
118  */
119 int
120 mlx5_getenv_int(const char *name)
121 {
122         const char *val = getenv(name);
123
124         if (val == NULL)
125                 return 0;
126         return atoi(val);
127 }
128
129 /**
130  * DPDK callback to close the device.
131  *
132  * Destroy all queues and objects, free memory.
133  *
134  * @param dev
135  *   Pointer to Ethernet device structure.
136  */
137 static void
138 mlx5_dev_close(struct rte_eth_dev *dev)
139 {
140         struct priv *priv = mlx5_get_priv(dev);
141         unsigned int i;
142
143         priv_lock(priv);
144         DEBUG("%p: closing device \"%s\"",
145               (void *)dev,
146               ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
147         /* In case mlx5_dev_stop() has not been called. */
148         priv_dev_interrupt_handler_uninstall(priv, dev);
149         priv_special_flow_disable_all(priv);
150         priv_mac_addrs_disable(priv);
151         priv_destroy_hash_rxqs(priv);
152
153         /* Remove flow director elements. */
154         priv_fdir_disable(priv);
155         priv_fdir_delete_filters_list(priv);
156
157         /* Prevent crashes when queues are still in use. */
158         dev->rx_pkt_burst = removed_rx_burst;
159         dev->tx_pkt_burst = removed_tx_burst;
160         if (priv->rxqs != NULL) {
161                 /* XXX race condition if mlx5_rx_burst() is still running. */
162                 usleep(1000);
163                 for (i = 0; (i != priv->rxqs_n); ++i) {
164                         struct rxq *rxq = (*priv->rxqs)[i];
165                         struct rxq_ctrl *rxq_ctrl;
166
167                         if (rxq == NULL)
168                                 continue;
169                         rxq_ctrl = container_of(rxq, struct rxq_ctrl, rxq);
170                         (*priv->rxqs)[i] = NULL;
171                         rxq_cleanup(rxq_ctrl);
172                         rte_free(rxq_ctrl);
173                 }
174                 priv->rxqs_n = 0;
175                 priv->rxqs = NULL;
176         }
177         if (priv->txqs != NULL) {
178                 /* XXX race condition if mlx5_tx_burst() is still running. */
179                 usleep(1000);
180                 for (i = 0; (i != priv->txqs_n); ++i) {
181                         struct txq *txq = (*priv->txqs)[i];
182                         struct txq_ctrl *txq_ctrl;
183
184                         if (txq == NULL)
185                                 continue;
186                         txq_ctrl = container_of(txq, struct txq_ctrl, txq);
187                         (*priv->txqs)[i] = NULL;
188                         txq_cleanup(txq_ctrl);
189                         rte_free(txq_ctrl);
190                 }
191                 priv->txqs_n = 0;
192                 priv->txqs = NULL;
193         }
194         if (priv->pd != NULL) {
195                 assert(priv->ctx != NULL);
196                 claim_zero(ibv_dealloc_pd(priv->pd));
197                 claim_zero(ibv_close_device(priv->ctx));
198         } else
199                 assert(priv->ctx == NULL);
200         if (priv->rss_conf != NULL) {
201                 for (i = 0; (i != hash_rxq_init_n); ++i)
202                         rte_free((*priv->rss_conf)[i]);
203                 rte_free(priv->rss_conf);
204         }
205         if (priv->reta_idx != NULL)
206                 rte_free(priv->reta_idx);
207         priv_unlock(priv);
208         memset(priv, 0, sizeof(*priv));
209 }
210
211 static const struct eth_dev_ops mlx5_dev_ops = {
212         .dev_configure = mlx5_dev_configure,
213         .dev_start = mlx5_dev_start,
214         .dev_stop = mlx5_dev_stop,
215         .dev_set_link_down = mlx5_set_link_down,
216         .dev_set_link_up = mlx5_set_link_up,
217         .dev_close = mlx5_dev_close,
218         .promiscuous_enable = mlx5_promiscuous_enable,
219         .promiscuous_disable = mlx5_promiscuous_disable,
220         .allmulticast_enable = mlx5_allmulticast_enable,
221         .allmulticast_disable = mlx5_allmulticast_disable,
222         .link_update = mlx5_link_update,
223         .stats_get = mlx5_stats_get,
224         .stats_reset = mlx5_stats_reset,
225         .xstats_get = mlx5_xstats_get,
226         .xstats_reset = mlx5_xstats_reset,
227         .xstats_get_names = mlx5_xstats_get_names,
228         .dev_infos_get = mlx5_dev_infos_get,
229         .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
230         .vlan_filter_set = mlx5_vlan_filter_set,
231         .rx_queue_setup = mlx5_rx_queue_setup,
232         .tx_queue_setup = mlx5_tx_queue_setup,
233         .rx_queue_release = mlx5_rx_queue_release,
234         .tx_queue_release = mlx5_tx_queue_release,
235         .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
236         .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
237         .mac_addr_remove = mlx5_mac_addr_remove,
238         .mac_addr_add = mlx5_mac_addr_add,
239         .mac_addr_set = mlx5_mac_addr_set,
240         .mtu_set = mlx5_dev_set_mtu,
241         .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
242         .vlan_offload_set = mlx5_vlan_offload_set,
243         .reta_update = mlx5_dev_rss_reta_update,
244         .reta_query = mlx5_dev_rss_reta_query,
245         .rss_hash_update = mlx5_rss_hash_update,
246         .rss_hash_conf_get = mlx5_rss_hash_conf_get,
247         .filter_ctrl = mlx5_dev_filter_ctrl,
248         .rx_descriptor_status = mlx5_rx_descriptor_status,
249         .tx_descriptor_status = mlx5_tx_descriptor_status,
250 #ifdef HAVE_UPDATE_CQ_CI
251         .rx_queue_intr_enable = mlx5_rx_intr_enable,
252         .rx_queue_intr_disable = mlx5_rx_intr_disable,
253 #endif
254 };
255
256 static struct {
257         struct rte_pci_addr pci_addr; /* associated PCI address */
258         uint32_t ports; /* physical ports bitfield. */
259 } mlx5_dev[32];
260
261 /**
262  * Get device index in mlx5_dev[] from PCI bus address.
263  *
264  * @param[in] pci_addr
265  *   PCI bus address to look for.
266  *
267  * @return
268  *   mlx5_dev[] index on success, -1 on failure.
269  */
270 static int
271 mlx5_dev_idx(struct rte_pci_addr *pci_addr)
272 {
273         unsigned int i;
274         int ret = -1;
275
276         assert(pci_addr != NULL);
277         for (i = 0; (i != RTE_DIM(mlx5_dev)); ++i) {
278                 if ((mlx5_dev[i].pci_addr.domain == pci_addr->domain) &&
279                     (mlx5_dev[i].pci_addr.bus == pci_addr->bus) &&
280                     (mlx5_dev[i].pci_addr.devid == pci_addr->devid) &&
281                     (mlx5_dev[i].pci_addr.function == pci_addr->function))
282                         return i;
283                 if ((mlx5_dev[i].ports == 0) && (ret == -1))
284                         ret = i;
285         }
286         return ret;
287 }
288
289 /**
290  * Verify and store value for device argument.
291  *
292  * @param[in] key
293  *   Key argument to verify.
294  * @param[in] val
295  *   Value associated with key.
296  * @param opaque
297  *   User data.
298  *
299  * @return
300  *   0 on success, negative errno value on failure.
301  */
302 static int
303 mlx5_args_check(const char *key, const char *val, void *opaque)
304 {
305         struct mlx5_args *args = opaque;
306         unsigned long tmp;
307
308         errno = 0;
309         tmp = strtoul(val, NULL, 0);
310         if (errno) {
311                 WARN("%s: \"%s\" is not a valid integer", key, val);
312                 return errno;
313         }
314         if (strcmp(MLX5_RXQ_CQE_COMP_EN, key) == 0) {
315                 args->cqe_comp = !!tmp;
316         } else if (strcmp(MLX5_TXQ_INLINE, key) == 0) {
317                 args->txq_inline = tmp;
318         } else if (strcmp(MLX5_TXQS_MIN_INLINE, key) == 0) {
319                 args->txqs_inline = tmp;
320         } else if (strcmp(MLX5_TXQ_MPW_EN, key) == 0) {
321                 args->mps = !!tmp;
322         } else if (strcmp(MLX5_TXQ_MPW_HDR_DSEG_EN, key) == 0) {
323                 args->mpw_hdr_dseg = !!tmp;
324         } else if (strcmp(MLX5_TXQ_MAX_INLINE_LEN, key) == 0) {
325                 args->inline_max_packet_sz = tmp;
326         } else if (strcmp(MLX5_TSO, key) == 0) {
327                 args->tso = !!tmp;
328         } else if (strcmp(MLX5_TX_VEC_EN, key) == 0) {
329                 args->tx_vec_en = !!tmp;
330         } else if (strcmp(MLX5_RX_VEC_EN, key) == 0) {
331                 args->rx_vec_en = !!tmp;
332         } else {
333                 WARN("%s: unknown parameter", key);
334                 return -EINVAL;
335         }
336         return 0;
337 }
338
339 /**
340  * Parse device parameters.
341  *
342  * @param priv
343  *   Pointer to private structure.
344  * @param devargs
345  *   Device arguments structure.
346  *
347  * @return
348  *   0 on success, errno value on failure.
349  */
350 static int
351 mlx5_args(struct mlx5_args *args, struct rte_devargs *devargs)
352 {
353         const char **params = (const char *[]){
354                 MLX5_RXQ_CQE_COMP_EN,
355                 MLX5_TXQ_INLINE,
356                 MLX5_TXQS_MIN_INLINE,
357                 MLX5_TXQ_MPW_EN,
358                 MLX5_TXQ_MPW_HDR_DSEG_EN,
359                 MLX5_TXQ_MAX_INLINE_LEN,
360                 MLX5_TSO,
361                 MLX5_TX_VEC_EN,
362                 MLX5_RX_VEC_EN,
363                 NULL,
364         };
365         struct rte_kvargs *kvlist;
366         int ret = 0;
367         int i;
368
369         if (devargs == NULL)
370                 return 0;
371         /* Following UGLY cast is done to pass checkpatch. */
372         kvlist = rte_kvargs_parse(devargs->args, params);
373         if (kvlist == NULL)
374                 return 0;
375         /* Process parameters. */
376         for (i = 0; (params[i] != NULL); ++i) {
377                 if (rte_kvargs_count(kvlist, params[i])) {
378                         ret = rte_kvargs_process(kvlist, params[i],
379                                                  mlx5_args_check, args);
380                         if (ret != 0) {
381                                 rte_kvargs_free(kvlist);
382                                 return ret;
383                         }
384                 }
385         }
386         rte_kvargs_free(kvlist);
387         return 0;
388 }
389
390 static struct rte_pci_driver mlx5_driver;
391
392 /**
393  * Assign parameters from args into priv, only non default
394  * values are considered.
395  *
396  * @param[out] priv
397  *   Pointer to private structure.
398  * @param[in] args
399  *   Pointer to args values.
400  */
401 static void
402 mlx5_args_assign(struct priv *priv, struct mlx5_args *args)
403 {
404         if (args->cqe_comp != MLX5_ARG_UNSET)
405                 priv->cqe_comp = args->cqe_comp;
406         if (args->txq_inline != MLX5_ARG_UNSET)
407                 priv->txq_inline = args->txq_inline;
408         if (args->txqs_inline != MLX5_ARG_UNSET)
409                 priv->txqs_inline = args->txqs_inline;
410         if (args->mps != MLX5_ARG_UNSET)
411                 priv->mps = args->mps ? priv->mps : 0;
412         if (args->mpw_hdr_dseg != MLX5_ARG_UNSET)
413                 priv->mpw_hdr_dseg = args->mpw_hdr_dseg;
414         if (args->inline_max_packet_sz != MLX5_ARG_UNSET)
415                 priv->inline_max_packet_sz = args->inline_max_packet_sz;
416         if (args->tso != MLX5_ARG_UNSET)
417                 priv->tso = args->tso;
418         if (args->tx_vec_en != MLX5_ARG_UNSET)
419                 priv->tx_vec_en = args->tx_vec_en;
420         if (args->rx_vec_en != MLX5_ARG_UNSET)
421                 priv->rx_vec_en = args->rx_vec_en;
422 }
423
424 /**
425  * DPDK callback to register a PCI device.
426  *
427  * This function creates an Ethernet device for each port of a given
428  * PCI device.
429  *
430  * @param[in] pci_drv
431  *   PCI driver structure (mlx5_driver).
432  * @param[in] pci_dev
433  *   PCI device information.
434  *
435  * @return
436  *   0 on success, negative errno value on failure.
437  */
438 static int
439 mlx5_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
440 {
441         struct ibv_device **list;
442         struct ibv_device *ibv_dev;
443         int err = 0;
444         struct ibv_context *attr_ctx = NULL;
445         struct ibv_device_attr device_attr;
446         unsigned int sriov;
447         unsigned int mps;
448         unsigned int tunnel_en = 0;
449         int idx;
450         int i;
451
452         (void)pci_drv;
453         assert(pci_drv == &mlx5_driver);
454         /* Get mlx5_dev[] index. */
455         idx = mlx5_dev_idx(&pci_dev->addr);
456         if (idx == -1) {
457                 ERROR("this driver cannot support any more adapters");
458                 return -ENOMEM;
459         }
460         DEBUG("using driver device index %d", idx);
461
462         /* Save PCI address. */
463         mlx5_dev[idx].pci_addr = pci_dev->addr;
464         list = ibv_get_device_list(&i);
465         if (list == NULL) {
466                 assert(errno);
467                 if (errno == ENOSYS)
468                         ERROR("cannot list devices, is ib_uverbs loaded?");
469                 return -errno;
470         }
471         assert(i >= 0);
472         /*
473          * For each listed device, check related sysfs entry against
474          * the provided PCI ID.
475          */
476         while (i != 0) {
477                 struct rte_pci_addr pci_addr;
478
479                 --i;
480                 DEBUG("checking device \"%s\"", list[i]->name);
481                 if (mlx5_ibv_device_to_pci_addr(list[i], &pci_addr))
482                         continue;
483                 if ((pci_dev->addr.domain != pci_addr.domain) ||
484                     (pci_dev->addr.bus != pci_addr.bus) ||
485                     (pci_dev->addr.devid != pci_addr.devid) ||
486                     (pci_dev->addr.function != pci_addr.function))
487                         continue;
488                 sriov = ((pci_dev->id.device_id ==
489                        PCI_DEVICE_ID_MELLANOX_CONNECTX4VF) ||
490                       (pci_dev->id.device_id ==
491                        PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF) ||
492                       (pci_dev->id.device_id ==
493                        PCI_DEVICE_ID_MELLANOX_CONNECTX5VF) ||
494                       (pci_dev->id.device_id ==
495                        PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF));
496                 /*
497                  * Multi-packet send is supported by ConnectX-4 Lx PF as well
498                  * as all ConnectX-5 devices.
499                  */
500                 switch (pci_dev->id.device_id) {
501                 case PCI_DEVICE_ID_MELLANOX_CONNECTX4:
502                         tunnel_en = 1;
503                         mps = MLX5_MPW_DISABLED;
504                         break;
505                 case PCI_DEVICE_ID_MELLANOX_CONNECTX4LX:
506                         tunnel_en = 1;
507                         mps = MLX5_MPW;
508                         break;
509                 case PCI_DEVICE_ID_MELLANOX_CONNECTX5:
510                 case PCI_DEVICE_ID_MELLANOX_CONNECTX5VF:
511                 case PCI_DEVICE_ID_MELLANOX_CONNECTX5EX:
512                 case PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF:
513                         tunnel_en = 1;
514                         mps = MLX5_MPW_ENHANCED;
515                         break;
516                 default:
517                         mps = MLX5_MPW_DISABLED;
518                 }
519                 INFO("PCI information matches, using device \"%s\""
520                      " (SR-IOV: %s, %sMPS: %s)",
521                      list[i]->name,
522                      sriov ? "true" : "false",
523                      mps == MLX5_MPW_ENHANCED ? "Enhanced " : "",
524                      mps != MLX5_MPW_DISABLED ? "true" : "false");
525                 attr_ctx = ibv_open_device(list[i]);
526                 err = errno;
527                 break;
528         }
529         if (attr_ctx == NULL) {
530                 ibv_free_device_list(list);
531                 switch (err) {
532                 case 0:
533                         ERROR("cannot access device, is mlx5_ib loaded?");
534                         return -ENODEV;
535                 case EINVAL:
536                         ERROR("cannot use device, are drivers up to date?");
537                         return -EINVAL;
538                 }
539                 assert(err > 0);
540                 return -err;
541         }
542         ibv_dev = list[i];
543
544         DEBUG("device opened");
545         if (ibv_query_device(attr_ctx, &device_attr))
546                 goto error;
547         INFO("%u port(s) detected", device_attr.phys_port_cnt);
548
549         for (i = 0; i < device_attr.phys_port_cnt; i++) {
550                 uint32_t port = i + 1; /* ports are indexed from one */
551                 uint32_t test = (1 << i);
552                 struct ibv_context *ctx = NULL;
553                 struct ibv_port_attr port_attr;
554                 struct ibv_pd *pd = NULL;
555                 struct priv *priv = NULL;
556                 struct rte_eth_dev *eth_dev;
557                 struct ibv_exp_device_attr exp_device_attr;
558                 struct ether_addr mac;
559                 uint16_t num_vfs = 0;
560                 struct mlx5_args args = {
561                         .cqe_comp = MLX5_ARG_UNSET,
562                         .txq_inline = MLX5_ARG_UNSET,
563                         .txqs_inline = MLX5_ARG_UNSET,
564                         .mps = MLX5_ARG_UNSET,
565                         .mpw_hdr_dseg = MLX5_ARG_UNSET,
566                         .inline_max_packet_sz = MLX5_ARG_UNSET,
567                         .tso = MLX5_ARG_UNSET,
568                         .tx_vec_en = MLX5_ARG_UNSET,
569                         .rx_vec_en = MLX5_ARG_UNSET,
570                 };
571
572                 exp_device_attr.comp_mask =
573                         IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS |
574                         IBV_EXP_DEVICE_ATTR_RX_HASH |
575                         IBV_EXP_DEVICE_ATTR_VLAN_OFFLOADS |
576                         IBV_EXP_DEVICE_ATTR_RX_PAD_END_ALIGN |
577                         IBV_EXP_DEVICE_ATTR_TSO_CAPS |
578                         0;
579
580                 DEBUG("using port %u (%08" PRIx32 ")", port, test);
581
582                 ctx = ibv_open_device(ibv_dev);
583                 if (ctx == NULL) {
584                         err = ENODEV;
585                         goto port_error;
586                 }
587
588                 /* Check port status. */
589                 err = ibv_query_port(ctx, port, &port_attr);
590                 if (err) {
591                         ERROR("port query failed: %s", strerror(err));
592                         goto port_error;
593                 }
594
595                 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
596                         ERROR("port %d is not configured in Ethernet mode",
597                               port);
598                         err = EINVAL;
599                         goto port_error;
600                 }
601
602                 if (port_attr.state != IBV_PORT_ACTIVE)
603                         DEBUG("port %d is not active: \"%s\" (%d)",
604                               port, ibv_port_state_str(port_attr.state),
605                               port_attr.state);
606
607                 /* Allocate protection domain. */
608                 pd = ibv_alloc_pd(ctx);
609                 if (pd == NULL) {
610                         ERROR("PD allocation failure");
611                         err = ENOMEM;
612                         goto port_error;
613                 }
614
615                 mlx5_dev[idx].ports |= test;
616
617                 /* from rte_ethdev.c */
618                 priv = rte_zmalloc("ethdev private structure",
619                                    sizeof(*priv),
620                                    RTE_CACHE_LINE_SIZE);
621                 if (priv == NULL) {
622                         ERROR("priv allocation failure");
623                         err = ENOMEM;
624                         goto port_error;
625                 }
626
627                 priv->ctx = ctx;
628                 priv->device_attr = device_attr;
629                 priv->port = port;
630                 priv->pd = pd;
631                 priv->mtu = ETHER_MTU;
632                 priv->mps = mps; /* Enable MPW by default if supported. */
633                 priv->cqe_comp = 1; /* Enable compression by default. */
634                 priv->tunnel_en = tunnel_en;
635                 /* Enable vector by default if supported. */
636                 priv->tx_vec_en = 1;
637                 priv->rx_vec_en = 1;
638                 err = mlx5_args(&args, pci_dev->device.devargs);
639                 if (err) {
640                         ERROR("failed to process device arguments: %s",
641                               strerror(err));
642                         goto port_error;
643                 }
644                 mlx5_args_assign(priv, &args);
645                 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
646                         ERROR("ibv_exp_query_device() failed");
647                         err = ENODEV;
648                         goto port_error;
649                 }
650
651                 priv->hw_csum =
652                         ((exp_device_attr.exp_device_cap_flags &
653                           IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
654                          (exp_device_attr.exp_device_cap_flags &
655                           IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
656                 DEBUG("checksum offloading is %ssupported",
657                       (priv->hw_csum ? "" : "not "));
658
659                 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
660                                          IBV_EXP_DEVICE_VXLAN_SUPPORT);
661                 DEBUG("L2 tunnel checksum offloads are %ssupported",
662                       (priv->hw_csum_l2tun ? "" : "not "));
663
664                 priv->ind_table_max_size = exp_device_attr.rx_hash_caps.max_rwq_indirection_table_size;
665                 /* Remove this check once DPDK supports larger/variable
666                  * indirection tables. */
667                 if (priv->ind_table_max_size >
668                                 (unsigned int)ETH_RSS_RETA_SIZE_512)
669                         priv->ind_table_max_size = ETH_RSS_RETA_SIZE_512;
670                 DEBUG("maximum RX indirection table size is %u",
671                       priv->ind_table_max_size);
672                 priv->hw_vlan_strip = !!(exp_device_attr.wq_vlan_offloads_cap &
673                                          IBV_EXP_RECEIVE_WQ_CVLAN_STRIP);
674                 DEBUG("VLAN stripping is %ssupported",
675                       (priv->hw_vlan_strip ? "" : "not "));
676
677                 priv->hw_fcs_strip = !!(exp_device_attr.exp_device_cap_flags &
678                                         IBV_EXP_DEVICE_SCATTER_FCS);
679                 DEBUG("FCS stripping configuration is %ssupported",
680                       (priv->hw_fcs_strip ? "" : "not "));
681
682                 priv->hw_padding = !!exp_device_attr.rx_pad_end_addr_align;
683                 DEBUG("hardware RX end alignment padding is %ssupported",
684                       (priv->hw_padding ? "" : "not "));
685
686                 priv_get_num_vfs(priv, &num_vfs);
687                 priv->sriov = (num_vfs || sriov);
688                 priv->tso = ((priv->tso) &&
689                             (exp_device_attr.tso_caps.max_tso > 0) &&
690                             (exp_device_attr.tso_caps.supported_qpts &
691                             (1 << IBV_QPT_RAW_ETH)));
692                 if (priv->tso)
693                         priv->max_tso_payload_sz =
694                                 exp_device_attr.tso_caps.max_tso;
695                 if (priv->mps && !mps) {
696                         ERROR("multi-packet send not supported on this device"
697                               " (" MLX5_TXQ_MPW_EN ")");
698                         err = ENOTSUP;
699                         goto port_error;
700                 } else if (priv->mps && priv->tso) {
701                         WARN("multi-packet send not supported in conjunction "
702                               "with TSO. MPS disabled");
703                         priv->mps = 0;
704                 }
705                 INFO("%sMPS is %s",
706                      priv->mps == MLX5_MPW_ENHANCED ? "Enhanced " : "",
707                      priv->mps != MLX5_MPW_DISABLED ? "enabled" : "disabled");
708                 /* Set default values for Enhanced MPW, a.k.a MPWv2. */
709                 if (priv->mps == MLX5_MPW_ENHANCED) {
710                         if (args.txqs_inline == MLX5_ARG_UNSET)
711                                 priv->txqs_inline = MLX5_EMPW_MIN_TXQS;
712                         if (args.inline_max_packet_sz == MLX5_ARG_UNSET)
713                                 priv->inline_max_packet_sz =
714                                         MLX5_EMPW_MAX_INLINE_LEN;
715                         if (args.txq_inline == MLX5_ARG_UNSET)
716                                 priv->txq_inline = MLX5_WQE_SIZE_MAX -
717                                                    MLX5_WQE_SIZE;
718                 }
719                 /* Allocate and register default RSS hash keys. */
720                 priv->rss_conf = rte_calloc(__func__, hash_rxq_init_n,
721                                             sizeof((*priv->rss_conf)[0]), 0);
722                 if (priv->rss_conf == NULL) {
723                         err = ENOMEM;
724                         goto port_error;
725                 }
726                 err = rss_hash_rss_conf_new_key(priv,
727                                                 rss_hash_default_key,
728                                                 rss_hash_default_key_len,
729                                                 ETH_RSS_PROTO_MASK);
730                 if (err)
731                         goto port_error;
732                 /* Configure the first MAC address by default. */
733                 if (priv_get_mac(priv, &mac.addr_bytes)) {
734                         ERROR("cannot get MAC address, is mlx5_en loaded?"
735                               " (errno: %s)", strerror(errno));
736                         err = ENODEV;
737                         goto port_error;
738                 }
739                 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
740                      priv->port,
741                      mac.addr_bytes[0], mac.addr_bytes[1],
742                      mac.addr_bytes[2], mac.addr_bytes[3],
743                      mac.addr_bytes[4], mac.addr_bytes[5]);
744                 /* Register MAC address. */
745                 claim_zero(priv_mac_addr_add(priv, 0,
746                                              (const uint8_t (*)[ETHER_ADDR_LEN])
747                                              mac.addr_bytes));
748                 /* Initialize FD filters list. */
749                 err = fdir_init_filters_list(priv);
750                 if (err)
751                         goto port_error;
752 #ifndef NDEBUG
753                 {
754                         char ifname[IF_NAMESIZE];
755
756                         if (priv_get_ifname(priv, &ifname) == 0)
757                                 DEBUG("port %u ifname is \"%s\"",
758                                       priv->port, ifname);
759                         else
760                                 DEBUG("port %u ifname is unknown", priv->port);
761                 }
762 #endif
763                 /* Get actual MTU if possible. */
764                 priv_get_mtu(priv, &priv->mtu);
765                 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
766
767                 /* from rte_ethdev.c */
768                 {
769                         char name[RTE_ETH_NAME_MAX_LEN];
770
771                         snprintf(name, sizeof(name), "%s port %u",
772                                  ibv_get_device_name(ibv_dev), port);
773                         eth_dev = rte_eth_dev_allocate(name);
774                 }
775                 if (eth_dev == NULL) {
776                         ERROR("can not allocate rte ethdev");
777                         err = ENOMEM;
778                         goto port_error;
779                 }
780                 eth_dev->data->dev_private = priv;
781                 eth_dev->data->mac_addrs = priv->mac;
782                 eth_dev->device = &pci_dev->device;
783                 rte_eth_copy_pci_info(eth_dev, pci_dev);
784                 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
785                 eth_dev->device->driver = &mlx5_driver.driver;
786                 priv->dev = eth_dev;
787                 eth_dev->dev_ops = &mlx5_dev_ops;
788                 TAILQ_INIT(&priv->flows);
789
790                 /* Bring Ethernet device up. */
791                 DEBUG("forcing Ethernet interface up");
792                 priv_set_flags(priv, ~IFF_UP, IFF_UP);
793                 mlx5_link_update(priv->dev, 1);
794                 continue;
795
796 port_error:
797                 if (priv) {
798                         rte_free(priv->rss_conf);
799                         rte_free(priv);
800                 }
801                 if (pd)
802                         claim_zero(ibv_dealloc_pd(pd));
803                 if (ctx)
804                         claim_zero(ibv_close_device(ctx));
805                 break;
806         }
807
808         /*
809          * XXX if something went wrong in the loop above, there is a resource
810          * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
811          * long as the dpdk does not provide a way to deallocate a ethdev and a
812          * way to enumerate the registered ethdevs to free the previous ones.
813          */
814
815         /* no port found, complain */
816         if (!mlx5_dev[idx].ports) {
817                 err = ENODEV;
818                 goto error;
819         }
820
821 error:
822         if (attr_ctx)
823                 claim_zero(ibv_close_device(attr_ctx));
824         if (list)
825                 ibv_free_device_list(list);
826         assert(err >= 0);
827         return -err;
828 }
829
830 static const struct rte_pci_id mlx5_pci_id_map[] = {
831         {
832                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
833                                PCI_DEVICE_ID_MELLANOX_CONNECTX4)
834         },
835         {
836                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
837                                PCI_DEVICE_ID_MELLANOX_CONNECTX4VF)
838         },
839         {
840                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
841                                PCI_DEVICE_ID_MELLANOX_CONNECTX4LX)
842         },
843         {
844                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
845                                PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF)
846         },
847         {
848                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
849                                PCI_DEVICE_ID_MELLANOX_CONNECTX5)
850         },
851         {
852                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
853                                PCI_DEVICE_ID_MELLANOX_CONNECTX5VF)
854         },
855         {
856                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
857                                PCI_DEVICE_ID_MELLANOX_CONNECTX5EX)
858         },
859         {
860                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
861                                PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF)
862         },
863         {
864                 .vendor_id = 0
865         }
866 };
867
868 static struct rte_pci_driver mlx5_driver = {
869         .driver = {
870                 .name = MLX5_DRIVER_NAME
871         },
872         .id_table = mlx5_pci_id_map,
873         .probe = mlx5_pci_probe,
874         .drv_flags = RTE_PCI_DRV_INTR_LSC,
875 };
876
877 /**
878  * Driver initialization routine.
879  */
880 RTE_INIT(rte_mlx5_pmd_init);
881 static void
882 rte_mlx5_pmd_init(void)
883 {
884         /* Build the static table for ptype conversion. */
885         mlx5_set_ptype_table();
886         /*
887          * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
888          * huge pages. Calling ibv_fork_init() during init allows
889          * applications to use fork() safely for purposes other than
890          * using this PMD, which is not supported in forked processes.
891          */
892         setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
893         /* Don't map UAR to WC if BlueFlame is not used.*/
894         setenv("MLX5_SHUT_UP_BF", "1", 1);
895         ibv_fork_init();
896         rte_pci_register(&mlx5_driver);
897 }
898
899 RTE_PMD_EXPORT_NAME(net_mlx5, __COUNTER__);
900 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5, mlx5_pci_id_map);
901 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5, "* ib_uverbs & mlx5_core & mlx5_ib");