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