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