New upstream version 17.11.1
[deb_dpdk.git] / drivers / net / mlx4 / mlx4.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2012 6WIND S.A.
5  *   Copyright 2012 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 /**
35  * @file
36  * mlx4 driver initialization.
37  */
38
39 #include <assert.h>
40 #include <errno.h>
41 #include <inttypes.h>
42 #include <stddef.h>
43 #include <stdint.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47
48 /* Verbs headers do not support -pedantic. */
49 #ifdef PEDANTIC
50 #pragma GCC diagnostic ignored "-Wpedantic"
51 #endif
52 #include <infiniband/verbs.h>
53 #ifdef PEDANTIC
54 #pragma GCC diagnostic error "-Wpedantic"
55 #endif
56
57 #include <rte_common.h>
58 #include <rte_dev.h>
59 #include <rte_errno.h>
60 #include <rte_ethdev.h>
61 #include <rte_ethdev_pci.h>
62 #include <rte_ether.h>
63 #include <rte_flow.h>
64 #include <rte_interrupts.h>
65 #include <rte_kvargs.h>
66 #include <rte_malloc.h>
67 #include <rte_mbuf.h>
68
69 #include "mlx4.h"
70 #include "mlx4_flow.h"
71 #include "mlx4_rxtx.h"
72 #include "mlx4_utils.h"
73
74 /** Configuration structure for device arguments. */
75 struct mlx4_conf {
76         struct {
77                 uint32_t present; /**< Bit-field for existing ports. */
78                 uint32_t enabled; /**< Bit-field for user-enabled ports. */
79         } ports;
80 };
81
82 /* Available parameters list. */
83 const char *pmd_mlx4_init_params[] = {
84         MLX4_PMD_PORT_KVARG,
85         NULL,
86 };
87
88 /**
89  * DPDK callback for Ethernet device configuration.
90  *
91  * @param dev
92  *   Pointer to Ethernet device structure.
93  *
94  * @return
95  *   0 on success, negative errno value otherwise and rte_errno is set.
96  */
97 static int
98 mlx4_dev_configure(struct rte_eth_dev *dev)
99 {
100         struct priv *priv = dev->data->dev_private;
101         struct rte_flow_error error;
102         int ret;
103
104         /* Prepare internal flow rules. */
105         ret = mlx4_flow_sync(priv, &error);
106         if (ret) {
107                 ERROR("cannot set up internal flow rules (code %d, \"%s\"),"
108                       " flow error type %d, cause %p, message: %s",
109                       -ret, strerror(-ret), error.type, error.cause,
110                       error.message ? error.message : "(unspecified)");
111         }
112         return ret;
113 }
114
115 /**
116  * DPDK callback to start the device.
117  *
118  * Simulate device start by initializing common RSS resources and attaching
119  * all configured flows.
120  *
121  * @param dev
122  *   Pointer to Ethernet device structure.
123  *
124  * @return
125  *   0 on success, negative errno value otherwise and rte_errno is set.
126  */
127 static int
128 mlx4_dev_start(struct rte_eth_dev *dev)
129 {
130         struct priv *priv = dev->data->dev_private;
131         struct rte_flow_error error;
132         int ret;
133
134         if (priv->started)
135                 return 0;
136         DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
137         priv->started = 1;
138         ret = mlx4_rss_init(priv);
139         if (ret) {
140                 ERROR("%p: cannot initialize RSS resources: %s",
141                       (void *)dev, strerror(-ret));
142                 goto err;
143         }
144         ret = mlx4_intr_install(priv);
145         if (ret) {
146                 ERROR("%p: interrupt handler installation failed",
147                      (void *)dev);
148                 goto err;
149         }
150         ret = mlx4_flow_sync(priv, &error);
151         if (ret) {
152                 ERROR("%p: cannot attach flow rules (code %d, \"%s\"),"
153                       " flow error type %d, cause %p, message: %s",
154                       (void *)dev,
155                       -ret, strerror(-ret), error.type, error.cause,
156                       error.message ? error.message : "(unspecified)");
157                 goto err;
158         }
159         rte_wmb();
160         dev->tx_pkt_burst = mlx4_tx_burst;
161         dev->rx_pkt_burst = mlx4_rx_burst;
162         return 0;
163 err:
164         /* Rollback. */
165         priv->started = 0;
166         return ret;
167 }
168
169 /**
170  * DPDK callback to stop the device.
171  *
172  * Simulate device stop by detaching all configured flows.
173  *
174  * @param dev
175  *   Pointer to Ethernet device structure.
176  */
177 static void
178 mlx4_dev_stop(struct rte_eth_dev *dev)
179 {
180         struct priv *priv = dev->data->dev_private;
181
182         if (!priv->started)
183                 return;
184         DEBUG("%p: detaching flows from all RX queues", (void *)dev);
185         priv->started = 0;
186         dev->tx_pkt_burst = mlx4_tx_burst_removed;
187         dev->rx_pkt_burst = mlx4_rx_burst_removed;
188         rte_wmb();
189         mlx4_flow_sync(priv, NULL);
190         mlx4_intr_uninstall(priv);
191         mlx4_rss_deinit(priv);
192 }
193
194 /**
195  * DPDK callback to close the device.
196  *
197  * Destroy all queues and objects, free memory.
198  *
199  * @param dev
200  *   Pointer to Ethernet device structure.
201  */
202 static void
203 mlx4_dev_close(struct rte_eth_dev *dev)
204 {
205         struct priv *priv = dev->data->dev_private;
206         unsigned int i;
207
208         DEBUG("%p: closing device \"%s\"",
209               (void *)dev,
210               ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
211         dev->rx_pkt_burst = mlx4_rx_burst_removed;
212         dev->tx_pkt_burst = mlx4_tx_burst_removed;
213         rte_wmb();
214         mlx4_flow_clean(priv);
215         for (i = 0; i != dev->data->nb_rx_queues; ++i)
216                 mlx4_rx_queue_release(dev->data->rx_queues[i]);
217         for (i = 0; i != dev->data->nb_tx_queues; ++i)
218                 mlx4_tx_queue_release(dev->data->tx_queues[i]);
219         if (priv->pd != NULL) {
220                 assert(priv->ctx != NULL);
221                 claim_zero(ibv_dealloc_pd(priv->pd));
222                 claim_zero(ibv_close_device(priv->ctx));
223         } else
224                 assert(priv->ctx == NULL);
225         mlx4_intr_uninstall(priv);
226         memset(priv, 0, sizeof(*priv));
227 }
228
229 static const struct eth_dev_ops mlx4_dev_ops = {
230         .dev_configure = mlx4_dev_configure,
231         .dev_start = mlx4_dev_start,
232         .dev_stop = mlx4_dev_stop,
233         .dev_set_link_down = mlx4_dev_set_link_down,
234         .dev_set_link_up = mlx4_dev_set_link_up,
235         .dev_close = mlx4_dev_close,
236         .link_update = mlx4_link_update,
237         .promiscuous_enable = mlx4_promiscuous_enable,
238         .promiscuous_disable = mlx4_promiscuous_disable,
239         .allmulticast_enable = mlx4_allmulticast_enable,
240         .allmulticast_disable = mlx4_allmulticast_disable,
241         .mac_addr_remove = mlx4_mac_addr_remove,
242         .mac_addr_add = mlx4_mac_addr_add,
243         .mac_addr_set = mlx4_mac_addr_set,
244         .stats_get = mlx4_stats_get,
245         .stats_reset = mlx4_stats_reset,
246         .dev_infos_get = mlx4_dev_infos_get,
247         .dev_supported_ptypes_get = mlx4_dev_supported_ptypes_get,
248         .vlan_filter_set = mlx4_vlan_filter_set,
249         .rx_queue_setup = mlx4_rx_queue_setup,
250         .tx_queue_setup = mlx4_tx_queue_setup,
251         .rx_queue_release = mlx4_rx_queue_release,
252         .tx_queue_release = mlx4_tx_queue_release,
253         .flow_ctrl_get = mlx4_flow_ctrl_get,
254         .flow_ctrl_set = mlx4_flow_ctrl_set,
255         .mtu_set = mlx4_mtu_set,
256         .filter_ctrl = mlx4_filter_ctrl,
257         .rx_queue_intr_enable = mlx4_rx_intr_enable,
258         .rx_queue_intr_disable = mlx4_rx_intr_disable,
259 };
260
261 /**
262  * Get PCI information from struct ibv_device.
263  *
264  * @param device
265  *   Pointer to Ethernet device structure.
266  * @param[out] pci_addr
267  *   PCI bus address output buffer.
268  *
269  * @return
270  *   0 on success, negative errno value otherwise and rte_errno is set.
271  */
272 static int
273 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
274                             struct rte_pci_addr *pci_addr)
275 {
276         FILE *file;
277         char line[32];
278         MKSTR(path, "%s/device/uevent", device->ibdev_path);
279
280         file = fopen(path, "rb");
281         if (file == NULL) {
282                 rte_errno = errno;
283                 return -rte_errno;
284         }
285         while (fgets(line, sizeof(line), file) == line) {
286                 size_t len = strlen(line);
287                 int ret;
288
289                 /* Truncate long lines. */
290                 if (len == (sizeof(line) - 1))
291                         while (line[(len - 1)] != '\n') {
292                                 ret = fgetc(file);
293                                 if (ret == EOF)
294                                         break;
295                                 line[(len - 1)] = ret;
296                         }
297                 /* Extract information. */
298                 if (sscanf(line,
299                            "PCI_SLOT_NAME="
300                            "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
301                            &pci_addr->domain,
302                            &pci_addr->bus,
303                            &pci_addr->devid,
304                            &pci_addr->function) == 4) {
305                         ret = 0;
306                         break;
307                 }
308         }
309         fclose(file);
310         return 0;
311 }
312
313 /**
314  * Verify and store value for device argument.
315  *
316  * @param[in] key
317  *   Key argument to verify.
318  * @param[in] val
319  *   Value associated with key.
320  * @param[in, out] conf
321  *   Shared configuration data.
322  *
323  * @return
324  *   0 on success, negative errno value otherwise and rte_errno is set.
325  */
326 static int
327 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
328 {
329         unsigned long tmp;
330
331         errno = 0;
332         tmp = strtoul(val, NULL, 0);
333         if (errno) {
334                 rte_errno = errno;
335                 WARN("%s: \"%s\" is not a valid integer", key, val);
336                 return -rte_errno;
337         }
338         if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
339                 uint32_t ports = rte_log2_u32(conf->ports.present);
340
341                 if (tmp >= ports) {
342                         ERROR("port index %lu outside range [0,%" PRIu32 ")",
343                               tmp, ports);
344                         return -EINVAL;
345                 }
346                 if (!(conf->ports.present & (1 << tmp))) {
347                         rte_errno = EINVAL;
348                         ERROR("invalid port index %lu", tmp);
349                         return -rte_errno;
350                 }
351                 conf->ports.enabled |= 1 << tmp;
352         } else {
353                 rte_errno = EINVAL;
354                 WARN("%s: unknown parameter", key);
355                 return -rte_errno;
356         }
357         return 0;
358 }
359
360 /**
361  * Parse device parameters.
362  *
363  * @param devargs
364  *   Device arguments structure.
365  *
366  * @return
367  *   0 on success, negative errno value otherwise and rte_errno is set.
368  */
369 static int
370 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
371 {
372         struct rte_kvargs *kvlist;
373         unsigned int arg_count;
374         int ret = 0;
375         int i;
376
377         if (devargs == NULL)
378                 return 0;
379         kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
380         if (kvlist == NULL) {
381                 rte_errno = EINVAL;
382                 ERROR("failed to parse kvargs");
383                 return -rte_errno;
384         }
385         /* Process parameters. */
386         for (i = 0; pmd_mlx4_init_params[i]; ++i) {
387                 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
388                 while (arg_count-- > 0) {
389                         ret = rte_kvargs_process(kvlist,
390                                                  MLX4_PMD_PORT_KVARG,
391                                                  (int (*)(const char *,
392                                                           const char *,
393                                                           void *))
394                                                  mlx4_arg_parse,
395                                                  conf);
396                         if (ret != 0)
397                                 goto free_kvlist;
398                 }
399         }
400 free_kvlist:
401         rte_kvargs_free(kvlist);
402         return ret;
403 }
404
405 static struct rte_pci_driver mlx4_driver;
406
407 /**
408  * DPDK callback to register a PCI device.
409  *
410  * This function creates an Ethernet device for each port of a given
411  * PCI device.
412  *
413  * @param[in] pci_drv
414  *   PCI driver structure (mlx4_driver).
415  * @param[in] pci_dev
416  *   PCI device information.
417  *
418  * @return
419  *   0 on success, negative errno value otherwise and rte_errno is set.
420  */
421 static int
422 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
423 {
424         struct ibv_device **list;
425         struct ibv_device *ibv_dev;
426         int err = 0;
427         struct ibv_context *attr_ctx = NULL;
428         struct ibv_device_attr device_attr;
429         struct mlx4_conf conf = {
430                 .ports.present = 0,
431         };
432         unsigned int vf;
433         int i;
434
435         (void)pci_drv;
436         assert(pci_drv == &mlx4_driver);
437         list = ibv_get_device_list(&i);
438         if (list == NULL) {
439                 rte_errno = errno;
440                 assert(rte_errno);
441                 if (rte_errno == ENOSYS)
442                         ERROR("cannot list devices, is ib_uverbs loaded?");
443                 return -rte_errno;
444         }
445         assert(i >= 0);
446         /*
447          * For each listed device, check related sysfs entry against
448          * the provided PCI ID.
449          */
450         while (i != 0) {
451                 struct rte_pci_addr pci_addr;
452
453                 --i;
454                 DEBUG("checking device \"%s\"", list[i]->name);
455                 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
456                         continue;
457                 if ((pci_dev->addr.domain != pci_addr.domain) ||
458                     (pci_dev->addr.bus != pci_addr.bus) ||
459                     (pci_dev->addr.devid != pci_addr.devid) ||
460                     (pci_dev->addr.function != pci_addr.function))
461                         continue;
462                 vf = (pci_dev->id.device_id ==
463                       PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
464                 INFO("PCI information matches, using device \"%s\" (VF: %s)",
465                      list[i]->name, (vf ? "true" : "false"));
466                 attr_ctx = ibv_open_device(list[i]);
467                 err = errno;
468                 break;
469         }
470         if (attr_ctx == NULL) {
471                 ibv_free_device_list(list);
472                 switch (err) {
473                 case 0:
474                         rte_errno = ENODEV;
475                         ERROR("cannot access device, is mlx4_ib loaded?");
476                         return -rte_errno;
477                 case EINVAL:
478                         rte_errno = EINVAL;
479                         ERROR("cannot use device, are drivers up to date?");
480                         return -rte_errno;
481                 }
482                 assert(err > 0);
483                 rte_errno = err;
484                 return -rte_errno;
485         }
486         ibv_dev = list[i];
487         DEBUG("device opened");
488         if (ibv_query_device(attr_ctx, &device_attr)) {
489                 rte_errno = ENODEV;
490                 goto error;
491         }
492         INFO("%u port(s) detected", device_attr.phys_port_cnt);
493         conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
494         if (mlx4_args(pci_dev->device.devargs, &conf)) {
495                 ERROR("failed to process device arguments");
496                 rte_errno = EINVAL;
497                 goto error;
498         }
499         /* Use all ports when none are defined */
500         if (!conf.ports.enabled)
501                 conf.ports.enabled = conf.ports.present;
502         for (i = 0; i < device_attr.phys_port_cnt; i++) {
503                 uint32_t port = i + 1; /* ports are indexed from one */
504                 struct ibv_context *ctx = NULL;
505                 struct ibv_port_attr port_attr;
506                 struct ibv_pd *pd = NULL;
507                 struct priv *priv = NULL;
508                 struct rte_eth_dev *eth_dev = NULL;
509                 struct ether_addr mac;
510
511                 /* If port is not enabled, skip. */
512                 if (!(conf.ports.enabled & (1 << i)))
513                         continue;
514                 DEBUG("using port %u", port);
515                 ctx = ibv_open_device(ibv_dev);
516                 if (ctx == NULL) {
517                         rte_errno = ENODEV;
518                         goto port_error;
519                 }
520                 /* Check port status. */
521                 err = ibv_query_port(ctx, port, &port_attr);
522                 if (err) {
523                         rte_errno = err;
524                         ERROR("port query failed: %s", strerror(rte_errno));
525                         goto port_error;
526                 }
527                 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
528                         rte_errno = ENOTSUP;
529                         ERROR("port %d is not configured in Ethernet mode",
530                               port);
531                         goto port_error;
532                 }
533                 if (port_attr.state != IBV_PORT_ACTIVE)
534                         DEBUG("port %d is not active: \"%s\" (%d)",
535                               port, ibv_port_state_str(port_attr.state),
536                               port_attr.state);
537                 /* Make asynchronous FD non-blocking to handle interrupts. */
538                 if (mlx4_fd_set_non_blocking(ctx->async_fd) < 0) {
539                         ERROR("cannot make asynchronous FD non-blocking: %s",
540                               strerror(rte_errno));
541                         goto port_error;
542                 }
543                 /* Allocate protection domain. */
544                 pd = ibv_alloc_pd(ctx);
545                 if (pd == NULL) {
546                         rte_errno = ENOMEM;
547                         ERROR("PD allocation failure");
548                         goto port_error;
549                 }
550                 /* from rte_ethdev.c */
551                 priv = rte_zmalloc("ethdev private structure",
552                                    sizeof(*priv),
553                                    RTE_CACHE_LINE_SIZE);
554                 if (priv == NULL) {
555                         rte_errno = ENOMEM;
556                         ERROR("priv allocation failure");
557                         goto port_error;
558                 }
559                 priv->ctx = ctx;
560                 priv->device_attr = device_attr;
561                 priv->port = port;
562                 priv->pd = pd;
563                 priv->mtu = ETHER_MTU;
564                 priv->vf = vf;
565                 priv->hw_csum = !!(device_attr.device_cap_flags &
566                                    IBV_DEVICE_RAW_IP_CSUM);
567                 DEBUG("checksum offloading is %ssupported",
568                       (priv->hw_csum ? "" : "not "));
569                 /* Only ConnectX-3 Pro supports tunneling. */
570                 priv->hw_csum_l2tun =
571                         priv->hw_csum &&
572                         (device_attr.vendor_part_id ==
573                          PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO);
574                 DEBUG("L2 tunnel checksum offloads are %ssupported",
575                       (priv->hw_csum_l2tun ? "" : "not "));
576                 /* Configure the first MAC address by default. */
577                 if (mlx4_get_mac(priv, &mac.addr_bytes)) {
578                         ERROR("cannot get MAC address, is mlx4_en loaded?"
579                               " (rte_errno: %s)", strerror(rte_errno));
580                         goto port_error;
581                 }
582                 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
583                      priv->port,
584                      mac.addr_bytes[0], mac.addr_bytes[1],
585                      mac.addr_bytes[2], mac.addr_bytes[3],
586                      mac.addr_bytes[4], mac.addr_bytes[5]);
587                 /* Register MAC address. */
588                 priv->mac[0] = mac;
589 #ifndef NDEBUG
590                 {
591                         char ifname[IF_NAMESIZE];
592
593                         if (mlx4_get_ifname(priv, &ifname) == 0)
594                                 DEBUG("port %u ifname is \"%s\"",
595                                       priv->port, ifname);
596                         else
597                                 DEBUG("port %u ifname is unknown", priv->port);
598                 }
599 #endif
600                 /* Get actual MTU if possible. */
601                 mlx4_mtu_get(priv, &priv->mtu);
602                 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
603                 /* from rte_ethdev.c */
604                 {
605                         char name[RTE_ETH_NAME_MAX_LEN];
606
607                         snprintf(name, sizeof(name), "%s port %u",
608                                  ibv_get_device_name(ibv_dev), port);
609                         eth_dev = rte_eth_dev_allocate(name);
610                 }
611                 if (eth_dev == NULL) {
612                         ERROR("can not allocate rte ethdev");
613                         rte_errno = ENOMEM;
614                         goto port_error;
615                 }
616                 eth_dev->data->dev_private = priv;
617                 eth_dev->data->mac_addrs = priv->mac;
618                 eth_dev->device = &pci_dev->device;
619                 rte_eth_copy_pci_info(eth_dev, pci_dev);
620                 eth_dev->device->driver = &mlx4_driver.driver;
621                 /* Initialize local interrupt handle for current port. */
622                 priv->intr_handle = (struct rte_intr_handle){
623                         .fd = -1,
624                         .type = RTE_INTR_HANDLE_EXT,
625                 };
626                 /*
627                  * Override ethdev interrupt handle pointer with private
628                  * handle instead of that of the parent PCI device used by
629                  * default. This prevents it from being shared between all
630                  * ports of the same PCI device since each of them is
631                  * associated its own Verbs context.
632                  *
633                  * Rx interrupts in particular require this as the PMD has
634                  * no control over the registration of queue interrupts
635                  * besides setting up eth_dev->intr_handle, the rest is
636                  * handled by rte_intr_rx_ctl().
637                  */
638                 eth_dev->intr_handle = &priv->intr_handle;
639                 priv->dev = eth_dev;
640                 eth_dev->dev_ops = &mlx4_dev_ops;
641                 /* Bring Ethernet device up. */
642                 DEBUG("forcing Ethernet interface up");
643                 mlx4_dev_set_link_up(priv->dev);
644                 /* Update link status once if waiting for LSC. */
645                 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
646                         mlx4_link_update(eth_dev, 0);
647                 continue;
648 port_error:
649                 rte_free(priv);
650                 if (pd)
651                         claim_zero(ibv_dealloc_pd(pd));
652                 if (ctx)
653                         claim_zero(ibv_close_device(ctx));
654                 if (eth_dev)
655                         rte_eth_dev_release_port(eth_dev);
656                 break;
657         }
658         if (i == device_attr.phys_port_cnt)
659                 return 0;
660         /*
661          * XXX if something went wrong in the loop above, there is a resource
662          * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
663          * long as the dpdk does not provide a way to deallocate a ethdev and a
664          * way to enumerate the registered ethdevs to free the previous ones.
665          */
666 error:
667         if (attr_ctx)
668                 claim_zero(ibv_close_device(attr_ctx));
669         if (list)
670                 ibv_free_device_list(list);
671         assert(rte_errno >= 0);
672         return -rte_errno;
673 }
674
675 static const struct rte_pci_id mlx4_pci_id_map[] = {
676         {
677                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
678                                PCI_DEVICE_ID_MELLANOX_CONNECTX3)
679         },
680         {
681                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
682                                PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
683         },
684         {
685                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
686                                PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
687         },
688         {
689                 .vendor_id = 0
690         }
691 };
692
693 static struct rte_pci_driver mlx4_driver = {
694         .driver = {
695                 .name = MLX4_DRIVER_NAME
696         },
697         .id_table = mlx4_pci_id_map,
698         .probe = mlx4_pci_probe,
699         .drv_flags = RTE_PCI_DRV_INTR_LSC |
700                      RTE_PCI_DRV_INTR_RMV,
701 };
702
703 /**
704  * Driver initialization routine.
705  */
706 RTE_INIT(rte_mlx4_pmd_init);
707 static void
708 rte_mlx4_pmd_init(void)
709 {
710         /*
711          * MLX4_DEVICE_FATAL_CLEANUP tells ibv_destroy functions we
712          * want to get success errno value in case of calling them
713          * when the device was removed.
714          */
715         setenv("MLX4_DEVICE_FATAL_CLEANUP", "1", 1);
716         /*
717          * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
718          * huge pages. Calling ibv_fork_init() during init allows
719          * applications to use fork() safely for purposes other than
720          * using this PMD, which is not supported in forked processes.
721          */
722         setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
723         ibv_fork_init();
724         rte_pci_register(&mlx4_driver);
725 }
726
727 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
728 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
729 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
730         "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");