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