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