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