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