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