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