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