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