net/mlx4: move rdma-core calls to separate file
[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_driver.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_glue.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         int ret;
104
105         /* Prepare internal flow rules. */
106         ret = mlx4_flow_sync(priv, &error);
107         if (ret) {
108                 ERROR("cannot set up internal flow rules (code %d, \"%s\"),"
109                       " flow error type %d, cause %p, message: %s",
110                       -ret, strerror(-ret), error.type, error.cause,
111                       error.message ? error.message : "(unspecified)");
112                 goto exit;
113         }
114         ret = mlx4_intr_install(priv);
115         if (ret)
116                 ERROR("%p: interrupt handler installation failed",
117                       (void *)dev);
118 exit:
119         return ret;
120 }
121
122 /**
123  * DPDK callback to start the device.
124  *
125  * Simulate device start by initializing common RSS resources and attaching
126  * all configured flows.
127  *
128  * @param dev
129  *   Pointer to Ethernet device structure.
130  *
131  * @return
132  *   0 on success, negative errno value otherwise and rte_errno is set.
133  */
134 static int
135 mlx4_dev_start(struct rte_eth_dev *dev)
136 {
137         struct priv *priv = dev->data->dev_private;
138         struct rte_flow_error error;
139         int ret;
140
141         if (priv->started)
142                 return 0;
143         DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
144         priv->started = 1;
145         ret = mlx4_rss_init(priv);
146         if (ret) {
147                 ERROR("%p: cannot initialize RSS resources: %s",
148                       (void *)dev, strerror(-ret));
149                 goto err;
150         }
151         ret = mlx4_rxq_intr_enable(priv);
152         if (ret) {
153                 ERROR("%p: interrupt handler installation failed",
154                      (void *)dev);
155                 goto err;
156         }
157         ret = mlx4_flow_sync(priv, &error);
158         if (ret) {
159                 ERROR("%p: cannot attach flow rules (code %d, \"%s\"),"
160                       " flow error type %d, cause %p, message: %s",
161                       (void *)dev,
162                       -ret, strerror(-ret), error.type, error.cause,
163                       error.message ? error.message : "(unspecified)");
164                 goto err;
165         }
166         rte_wmb();
167         dev->tx_pkt_burst = mlx4_tx_burst;
168         dev->rx_pkt_burst = mlx4_rx_burst;
169         return 0;
170 err:
171         /* Rollback. */
172         priv->started = 0;
173         return ret;
174 }
175
176 /**
177  * DPDK callback to stop the device.
178  *
179  * Simulate device stop by detaching all configured flows.
180  *
181  * @param dev
182  *   Pointer to Ethernet device structure.
183  */
184 static void
185 mlx4_dev_stop(struct rte_eth_dev *dev)
186 {
187         struct priv *priv = dev->data->dev_private;
188
189         if (!priv->started)
190                 return;
191         DEBUG("%p: detaching flows from all RX queues", (void *)dev);
192         priv->started = 0;
193         dev->tx_pkt_burst = mlx4_tx_burst_removed;
194         dev->rx_pkt_burst = mlx4_rx_burst_removed;
195         rte_wmb();
196         mlx4_flow_sync(priv, NULL);
197         mlx4_rxq_intr_disable(priv);
198         mlx4_rss_deinit(priv);
199 }
200
201 /**
202  * DPDK callback to close the device.
203  *
204  * Destroy all queues and objects, free memory.
205  *
206  * @param dev
207  *   Pointer to Ethernet device structure.
208  */
209 static void
210 mlx4_dev_close(struct rte_eth_dev *dev)
211 {
212         struct priv *priv = dev->data->dev_private;
213         unsigned int i;
214
215         DEBUG("%p: closing device \"%s\"",
216               (void *)dev,
217               ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
218         dev->rx_pkt_burst = mlx4_rx_burst_removed;
219         dev->tx_pkt_burst = mlx4_tx_burst_removed;
220         rte_wmb();
221         mlx4_flow_clean(priv);
222         for (i = 0; i != dev->data->nb_rx_queues; ++i)
223                 mlx4_rx_queue_release(dev->data->rx_queues[i]);
224         for (i = 0; i != dev->data->nb_tx_queues; ++i)
225                 mlx4_tx_queue_release(dev->data->tx_queues[i]);
226         if (priv->pd != NULL) {
227                 assert(priv->ctx != NULL);
228                 claim_zero(mlx4_glue->dealloc_pd(priv->pd));
229                 claim_zero(mlx4_glue->close_device(priv->ctx));
230         } else
231                 assert(priv->ctx == NULL);
232         mlx4_intr_uninstall(priv);
233         memset(priv, 0, sizeof(*priv));
234 }
235
236 static const struct eth_dev_ops mlx4_dev_ops = {
237         .dev_configure = mlx4_dev_configure,
238         .dev_start = mlx4_dev_start,
239         .dev_stop = mlx4_dev_stop,
240         .dev_set_link_down = mlx4_dev_set_link_down,
241         .dev_set_link_up = mlx4_dev_set_link_up,
242         .dev_close = mlx4_dev_close,
243         .link_update = mlx4_link_update,
244         .promiscuous_enable = mlx4_promiscuous_enable,
245         .promiscuous_disable = mlx4_promiscuous_disable,
246         .allmulticast_enable = mlx4_allmulticast_enable,
247         .allmulticast_disable = mlx4_allmulticast_disable,
248         .mac_addr_remove = mlx4_mac_addr_remove,
249         .mac_addr_add = mlx4_mac_addr_add,
250         .mac_addr_set = mlx4_mac_addr_set,
251         .stats_get = mlx4_stats_get,
252         .stats_reset = mlx4_stats_reset,
253         .dev_infos_get = mlx4_dev_infos_get,
254         .dev_supported_ptypes_get = mlx4_dev_supported_ptypes_get,
255         .vlan_filter_set = mlx4_vlan_filter_set,
256         .rx_queue_setup = mlx4_rx_queue_setup,
257         .tx_queue_setup = mlx4_tx_queue_setup,
258         .rx_queue_release = mlx4_rx_queue_release,
259         .tx_queue_release = mlx4_tx_queue_release,
260         .flow_ctrl_get = mlx4_flow_ctrl_get,
261         .flow_ctrl_set = mlx4_flow_ctrl_set,
262         .mtu_set = mlx4_mtu_set,
263         .filter_ctrl = mlx4_filter_ctrl,
264         .rx_queue_intr_enable = mlx4_rx_intr_enable,
265         .rx_queue_intr_disable = mlx4_rx_intr_disable,
266         .is_removed = mlx4_is_removed,
267 };
268
269 /**
270  * Get PCI information from struct ibv_device.
271  *
272  * @param device
273  *   Pointer to Ethernet device structure.
274  * @param[out] pci_addr
275  *   PCI bus address output buffer.
276  *
277  * @return
278  *   0 on success, negative errno value otherwise and rte_errno is set.
279  */
280 static int
281 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
282                             struct rte_pci_addr *pci_addr)
283 {
284         FILE *file;
285         char line[32];
286         MKSTR(path, "%s/device/uevent", device->ibdev_path);
287
288         file = fopen(path, "rb");
289         if (file == NULL) {
290                 rte_errno = errno;
291                 return -rte_errno;
292         }
293         while (fgets(line, sizeof(line), file) == line) {
294                 size_t len = strlen(line);
295                 int ret;
296
297                 /* Truncate long lines. */
298                 if (len == (sizeof(line) - 1))
299                         while (line[(len - 1)] != '\n') {
300                                 ret = fgetc(file);
301                                 if (ret == EOF)
302                                         break;
303                                 line[(len - 1)] = ret;
304                         }
305                 /* Extract information. */
306                 if (sscanf(line,
307                            "PCI_SLOT_NAME="
308                            "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
309                            &pci_addr->domain,
310                            &pci_addr->bus,
311                            &pci_addr->devid,
312                            &pci_addr->function) == 4) {
313                         ret = 0;
314                         break;
315                 }
316         }
317         fclose(file);
318         return 0;
319 }
320
321 /**
322  * Verify and store value for device argument.
323  *
324  * @param[in] key
325  *   Key argument to verify.
326  * @param[in] val
327  *   Value associated with key.
328  * @param[in, out] conf
329  *   Shared configuration data.
330  *
331  * @return
332  *   0 on success, negative errno value otherwise and rte_errno is set.
333  */
334 static int
335 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
336 {
337         unsigned long tmp;
338
339         errno = 0;
340         tmp = strtoul(val, NULL, 0);
341         if (errno) {
342                 rte_errno = errno;
343                 WARN("%s: \"%s\" is not a valid integer", key, val);
344                 return -rte_errno;
345         }
346         if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
347                 uint32_t ports = rte_log2_u32(conf->ports.present + 1);
348
349                 if (tmp >= ports) {
350                         ERROR("port index %lu outside range [0,%" PRIu32 ")",
351                               tmp, ports);
352                         return -EINVAL;
353                 }
354                 if (!(conf->ports.present & (1 << tmp))) {
355                         rte_errno = EINVAL;
356                         ERROR("invalid port index %lu", tmp);
357                         return -rte_errno;
358                 }
359                 conf->ports.enabled |= 1 << tmp;
360         } else {
361                 rte_errno = EINVAL;
362                 WARN("%s: unknown parameter", key);
363                 return -rte_errno;
364         }
365         return 0;
366 }
367
368 /**
369  * Parse device parameters.
370  *
371  * @param devargs
372  *   Device arguments structure.
373  *
374  * @return
375  *   0 on success, negative errno value otherwise and rte_errno is set.
376  */
377 static int
378 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
379 {
380         struct rte_kvargs *kvlist;
381         unsigned int arg_count;
382         int ret = 0;
383         int i;
384
385         if (devargs == NULL)
386                 return 0;
387         kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
388         if (kvlist == NULL) {
389                 rte_errno = EINVAL;
390                 ERROR("failed to parse kvargs");
391                 return -rte_errno;
392         }
393         /* Process parameters. */
394         for (i = 0; pmd_mlx4_init_params[i]; ++i) {
395                 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
396                 while (arg_count-- > 0) {
397                         ret = rte_kvargs_process(kvlist,
398                                                  MLX4_PMD_PORT_KVARG,
399                                                  (int (*)(const char *,
400                                                           const char *,
401                                                           void *))
402                                                  mlx4_arg_parse,
403                                                  conf);
404                         if (ret != 0)
405                                 goto free_kvlist;
406                 }
407         }
408 free_kvlist:
409         rte_kvargs_free(kvlist);
410         return ret;
411 }
412
413 static struct rte_pci_driver mlx4_driver;
414
415 /**
416  * DPDK callback to register a PCI device.
417  *
418  * This function creates an Ethernet device for each port of a given
419  * PCI device.
420  *
421  * @param[in] pci_drv
422  *   PCI driver structure (mlx4_driver).
423  * @param[in] pci_dev
424  *   PCI device information.
425  *
426  * @return
427  *   0 on success, negative errno value otherwise and rte_errno is set.
428  */
429 static int
430 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
431 {
432         struct ibv_device **list;
433         struct ibv_device *ibv_dev;
434         int err = 0;
435         struct ibv_context *attr_ctx = NULL;
436         struct ibv_device_attr device_attr;
437         struct ibv_device_attr_ex device_attr_ex;
438         struct mlx4_conf conf = {
439                 .ports.present = 0,
440         };
441         unsigned int vf;
442         int i;
443
444         (void)pci_drv;
445         assert(pci_drv == &mlx4_driver);
446         list = mlx4_glue->get_device_list(&i);
447         if (list == NULL) {
448                 rte_errno = errno;
449                 assert(rte_errno);
450                 if (rte_errno == ENOSYS)
451                         ERROR("cannot list devices, is ib_uverbs loaded?");
452                 return -rte_errno;
453         }
454         assert(i >= 0);
455         /*
456          * For each listed device, check related sysfs entry against
457          * the provided PCI ID.
458          */
459         while (i != 0) {
460                 struct rte_pci_addr pci_addr;
461
462                 --i;
463                 DEBUG("checking device \"%s\"", list[i]->name);
464                 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
465                         continue;
466                 if ((pci_dev->addr.domain != pci_addr.domain) ||
467                     (pci_dev->addr.bus != pci_addr.bus) ||
468                     (pci_dev->addr.devid != pci_addr.devid) ||
469                     (pci_dev->addr.function != pci_addr.function))
470                         continue;
471                 vf = (pci_dev->id.device_id ==
472                       PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
473                 INFO("PCI information matches, using device \"%s\" (VF: %s)",
474                      list[i]->name, (vf ? "true" : "false"));
475                 attr_ctx = mlx4_glue->open_device(list[i]);
476                 err = errno;
477                 break;
478         }
479         if (attr_ctx == NULL) {
480                 mlx4_glue->free_device_list(list);
481                 switch (err) {
482                 case 0:
483                         rte_errno = ENODEV;
484                         ERROR("cannot access device, is mlx4_ib loaded?");
485                         return -rte_errno;
486                 case EINVAL:
487                         rte_errno = EINVAL;
488                         ERROR("cannot use device, are drivers up to date?");
489                         return -rte_errno;
490                 }
491                 assert(err > 0);
492                 rte_errno = err;
493                 return -rte_errno;
494         }
495         ibv_dev = list[i];
496         DEBUG("device opened");
497         if (mlx4_glue->query_device(attr_ctx, &device_attr)) {
498                 rte_errno = ENODEV;
499                 goto error;
500         }
501         INFO("%u port(s) detected", device_attr.phys_port_cnt);
502         conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
503         if (mlx4_args(pci_dev->device.devargs, &conf)) {
504                 ERROR("failed to process device arguments");
505                 rte_errno = EINVAL;
506                 goto error;
507         }
508         /* Use all ports when none are defined */
509         if (!conf.ports.enabled)
510                 conf.ports.enabled = conf.ports.present;
511         /* Retrieve extended device attributes. */
512         if (mlx4_glue->query_device_ex(attr_ctx, NULL, &device_attr_ex)) {
513                 rte_errno = ENODEV;
514                 goto error;
515         }
516         assert(device_attr.max_sge >= MLX4_MAX_SGE);
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 = mlx4_glue->open_device(ibv_dev);
531                 if (ctx == NULL) {
532                         rte_errno = ENODEV;
533                         goto port_error;
534                 }
535                 /* Check port status. */
536                 err = mlx4_glue->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, mlx4_glue->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 = mlx4_glue->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                 priv->hw_csum = !!(device_attr.device_cap_flags &
581                                    IBV_DEVICE_RAW_IP_CSUM);
582                 DEBUG("checksum offloading is %ssupported",
583                       (priv->hw_csum ? "" : "not "));
584                 /* Only ConnectX-3 Pro supports tunneling. */
585                 priv->hw_csum_l2tun =
586                         priv->hw_csum &&
587                         (device_attr.vendor_part_id ==
588                          PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO);
589                 DEBUG("L2 tunnel checksum offloads are %ssupported",
590                       (priv->hw_csum_l2tun ? "" : "not "));
591                 priv->hw_rss_sup = device_attr_ex.rss_caps.rx_hash_fields_mask;
592                 if (!priv->hw_rss_sup) {
593                         WARN("no RSS capabilities reported; disabling support"
594                              " for UDP RSS and inner VXLAN RSS");
595                         /* Fake support for all possible RSS hash fields. */
596                         priv->hw_rss_sup = ~UINT64_C(0);
597                         priv->hw_rss_sup = mlx4_conv_rss_hf(priv, -1);
598                         /* Filter out known unsupported fields. */
599                         priv->hw_rss_sup &=
600                                 ~(uint64_t)(IBV_RX_HASH_SRC_PORT_UDP |
601                                             IBV_RX_HASH_DST_PORT_UDP |
602                                             IBV_RX_HASH_INNER);
603                 }
604                 DEBUG("supported RSS hash fields mask: %016" PRIx64,
605                       priv->hw_rss_sup);
606                 /* Configure the first MAC address by default. */
607                 if (mlx4_get_mac(priv, &mac.addr_bytes)) {
608                         ERROR("cannot get MAC address, is mlx4_en loaded?"
609                               " (rte_errno: %s)", strerror(rte_errno));
610                         goto port_error;
611                 }
612                 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
613                      priv->port,
614                      mac.addr_bytes[0], mac.addr_bytes[1],
615                      mac.addr_bytes[2], mac.addr_bytes[3],
616                      mac.addr_bytes[4], mac.addr_bytes[5]);
617                 /* Register MAC address. */
618                 priv->mac[0] = mac;
619 #ifndef NDEBUG
620                 {
621                         char ifname[IF_NAMESIZE];
622
623                         if (mlx4_get_ifname(priv, &ifname) == 0)
624                                 DEBUG("port %u ifname is \"%s\"",
625                                       priv->port, ifname);
626                         else
627                                 DEBUG("port %u ifname is unknown", priv->port);
628                 }
629 #endif
630                 /* Get actual MTU if possible. */
631                 mlx4_mtu_get(priv, &priv->mtu);
632                 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
633                 /* from rte_ethdev.c */
634                 {
635                         char name[RTE_ETH_NAME_MAX_LEN];
636
637                         snprintf(name, sizeof(name), "%s port %u",
638                                  mlx4_glue->get_device_name(ibv_dev), port);
639                         eth_dev = rte_eth_dev_allocate(name);
640                 }
641                 if (eth_dev == NULL) {
642                         ERROR("can not allocate rte ethdev");
643                         rte_errno = ENOMEM;
644                         goto port_error;
645                 }
646                 eth_dev->data->dev_private = priv;
647                 eth_dev->data->mac_addrs = priv->mac;
648                 eth_dev->device = &pci_dev->device;
649                 rte_eth_copy_pci_info(eth_dev, pci_dev);
650                 eth_dev->device->driver = &mlx4_driver.driver;
651                 /* Initialize local interrupt handle for current port. */
652                 priv->intr_handle = (struct rte_intr_handle){
653                         .fd = -1,
654                         .type = RTE_INTR_HANDLE_EXT,
655                 };
656                 /*
657                  * Override ethdev interrupt handle pointer with private
658                  * handle instead of that of the parent PCI device used by
659                  * default. This prevents it from being shared between all
660                  * ports of the same PCI device since each of them is
661                  * associated its own Verbs context.
662                  *
663                  * Rx interrupts in particular require this as the PMD has
664                  * no control over the registration of queue interrupts
665                  * besides setting up eth_dev->intr_handle, the rest is
666                  * handled by rte_intr_rx_ctl().
667                  */
668                 eth_dev->intr_handle = &priv->intr_handle;
669                 priv->dev = eth_dev;
670                 eth_dev->dev_ops = &mlx4_dev_ops;
671                 /* Bring Ethernet device up. */
672                 DEBUG("forcing Ethernet interface up");
673                 mlx4_dev_set_link_up(priv->dev);
674                 /* Update link status once if waiting for LSC. */
675                 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
676                         mlx4_link_update(eth_dev, 0);
677                 continue;
678 port_error:
679                 rte_free(priv);
680                 if (pd)
681                         claim_zero(mlx4_glue->dealloc_pd(pd));
682                 if (ctx)
683                         claim_zero(mlx4_glue->close_device(ctx));
684                 if (eth_dev)
685                         rte_eth_dev_release_port(eth_dev);
686                 break;
687         }
688         if (i == device_attr.phys_port_cnt)
689                 return 0;
690         /*
691          * XXX if something went wrong in the loop above, there is a resource
692          * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
693          * long as the dpdk does not provide a way to deallocate a ethdev and a
694          * way to enumerate the registered ethdevs to free the previous ones.
695          */
696 error:
697         if (attr_ctx)
698                 claim_zero(mlx4_glue->close_device(attr_ctx));
699         if (list)
700                 mlx4_glue->free_device_list(list);
701         assert(rte_errno >= 0);
702         return -rte_errno;
703 }
704
705 static const struct rte_pci_id mlx4_pci_id_map[] = {
706         {
707                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
708                                PCI_DEVICE_ID_MELLANOX_CONNECTX3)
709         },
710         {
711                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
712                                PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
713         },
714         {
715                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
716                                PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
717         },
718         {
719                 .vendor_id = 0
720         }
721 };
722
723 static struct rte_pci_driver mlx4_driver = {
724         .driver = {
725                 .name = MLX4_DRIVER_NAME
726         },
727         .id_table = mlx4_pci_id_map,
728         .probe = mlx4_pci_probe,
729         .drv_flags = RTE_PCI_DRV_INTR_LSC |
730                      RTE_PCI_DRV_INTR_RMV,
731 };
732
733 /**
734  * Driver initialization routine.
735  */
736 RTE_INIT(rte_mlx4_pmd_init);
737 static void
738 rte_mlx4_pmd_init(void)
739 {
740         /*
741          * MLX4_DEVICE_FATAL_CLEANUP tells ibv_destroy functions we
742          * want to get success errno value in case of calling them
743          * when the device was removed.
744          */
745         setenv("MLX4_DEVICE_FATAL_CLEANUP", "1", 1);
746         /*
747          * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
748          * huge pages. Calling ibv_fork_init() during init allows
749          * applications to use fork() safely for purposes other than
750          * using this PMD, which is not supported in forked processes.
751          */
752         setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
753         mlx4_glue->fork_init();
754         rte_pci_register(&mlx4_driver);
755 }
756
757 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
758 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
759 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
760         "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");