4 * Copyright 2012 6WIND S.A.
5 * Copyright 2012 Mellanox
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
36 * mlx4 driver initialization.
48 /* Verbs headers do not support -pedantic. */
50 #pragma GCC diagnostic ignored "-Wpedantic"
52 #include <infiniband/verbs.h>
54 #pragma GCC diagnostic error "-Wpedantic"
57 #include <rte_common.h>
59 #include <rte_errno.h>
60 #include <rte_ethdev.h>
61 #include <rte_ethdev_pci.h>
62 #include <rte_ether.h>
64 #include <rte_interrupts.h>
65 #include <rte_kvargs.h>
66 #include <rte_malloc.h>
70 #include "mlx4_flow.h"
71 #include "mlx4_rxtx.h"
72 #include "mlx4_utils.h"
74 /** Configuration structure for device arguments. */
77 uint32_t present; /**< Bit-field for existing ports. */
78 uint32_t enabled; /**< Bit-field for user-enabled ports. */
82 /* Available parameters list. */
83 const char *pmd_mlx4_init_params[] = {
89 * DPDK callback for Ethernet device configuration.
92 * Pointer to Ethernet device structure.
95 * 0 on success, negative errno value otherwise and rte_errno is set.
98 mlx4_dev_configure(struct rte_eth_dev *dev)
100 struct priv *priv = dev->data->dev_private;
101 struct rte_flow_error error;
104 /* Prepare internal flow rules. */
105 ret = mlx4_flow_sync(priv, &error);
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)");
116 * DPDK callback to start the device.
118 * Simulate device start by initializing common RSS resources and attaching
119 * all configured flows.
122 * Pointer to Ethernet device structure.
125 * 0 on success, negative errno value otherwise and rte_errno is set.
128 mlx4_dev_start(struct rte_eth_dev *dev)
130 struct priv *priv = dev->data->dev_private;
131 struct rte_flow_error error;
136 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
138 ret = mlx4_rss_init(priv);
140 ERROR("%p: cannot initialize RSS resources: %s",
141 (void *)dev, strerror(-ret));
144 ret = mlx4_intr_install(priv);
146 ERROR("%p: interrupt handler installation failed",
150 ret = mlx4_flow_sync(priv, &error);
152 ERROR("%p: cannot attach flow rules (code %d, \"%s\"),"
153 " flow error type %d, cause %p, message: %s",
155 -ret, strerror(-ret), error.type, error.cause,
156 error.message ? error.message : "(unspecified)");
160 dev->tx_pkt_burst = mlx4_tx_burst;
161 dev->rx_pkt_burst = mlx4_rx_burst;
170 * DPDK callback to stop the device.
172 * Simulate device stop by detaching all configured flows.
175 * Pointer to Ethernet device structure.
178 mlx4_dev_stop(struct rte_eth_dev *dev)
180 struct priv *priv = dev->data->dev_private;
184 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
186 dev->tx_pkt_burst = mlx4_tx_burst_removed;
187 dev->rx_pkt_burst = mlx4_rx_burst_removed;
189 mlx4_flow_sync(priv, NULL);
190 mlx4_intr_uninstall(priv);
191 mlx4_rss_deinit(priv);
195 * DPDK callback to close the device.
197 * Destroy all queues and objects, free memory.
200 * Pointer to Ethernet device structure.
203 mlx4_dev_close(struct rte_eth_dev *dev)
205 struct priv *priv = dev->data->dev_private;
208 DEBUG("%p: closing device \"%s\"",
210 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
211 dev->rx_pkt_burst = mlx4_rx_burst_removed;
212 dev->tx_pkt_burst = mlx4_tx_burst_removed;
214 mlx4_flow_clean(priv);
215 for (i = 0; i != dev->data->nb_rx_queues; ++i)
216 mlx4_rx_queue_release(dev->data->rx_queues[i]);
217 for (i = 0; i != dev->data->nb_tx_queues; ++i)
218 mlx4_tx_queue_release(dev->data->tx_queues[i]);
219 if (priv->pd != NULL) {
220 assert(priv->ctx != NULL);
221 claim_zero(ibv_dealloc_pd(priv->pd));
222 claim_zero(ibv_close_device(priv->ctx));
224 assert(priv->ctx == NULL);
225 mlx4_intr_uninstall(priv);
226 memset(priv, 0, sizeof(*priv));
229 static const struct eth_dev_ops mlx4_dev_ops = {
230 .dev_configure = mlx4_dev_configure,
231 .dev_start = mlx4_dev_start,
232 .dev_stop = mlx4_dev_stop,
233 .dev_set_link_down = mlx4_dev_set_link_down,
234 .dev_set_link_up = mlx4_dev_set_link_up,
235 .dev_close = mlx4_dev_close,
236 .link_update = mlx4_link_update,
237 .promiscuous_enable = mlx4_promiscuous_enable,
238 .promiscuous_disable = mlx4_promiscuous_disable,
239 .allmulticast_enable = mlx4_allmulticast_enable,
240 .allmulticast_disable = mlx4_allmulticast_disable,
241 .mac_addr_remove = mlx4_mac_addr_remove,
242 .mac_addr_add = mlx4_mac_addr_add,
243 .mac_addr_set = mlx4_mac_addr_set,
244 .stats_get = mlx4_stats_get,
245 .stats_reset = mlx4_stats_reset,
246 .dev_infos_get = mlx4_dev_infos_get,
247 .dev_supported_ptypes_get = mlx4_dev_supported_ptypes_get,
248 .vlan_filter_set = mlx4_vlan_filter_set,
249 .rx_queue_setup = mlx4_rx_queue_setup,
250 .tx_queue_setup = mlx4_tx_queue_setup,
251 .rx_queue_release = mlx4_rx_queue_release,
252 .tx_queue_release = mlx4_tx_queue_release,
253 .flow_ctrl_get = mlx4_flow_ctrl_get,
254 .flow_ctrl_set = mlx4_flow_ctrl_set,
255 .mtu_set = mlx4_mtu_set,
256 .filter_ctrl = mlx4_filter_ctrl,
257 .rx_queue_intr_enable = mlx4_rx_intr_enable,
258 .rx_queue_intr_disable = mlx4_rx_intr_disable,
262 * Get PCI information from struct ibv_device.
265 * Pointer to Ethernet device structure.
266 * @param[out] pci_addr
267 * PCI bus address output buffer.
270 * 0 on success, negative errno value otherwise and rte_errno is set.
273 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
274 struct rte_pci_addr *pci_addr)
278 MKSTR(path, "%s/device/uevent", device->ibdev_path);
280 file = fopen(path, "rb");
285 while (fgets(line, sizeof(line), file) == line) {
286 size_t len = strlen(line);
289 /* Truncate long lines. */
290 if (len == (sizeof(line) - 1))
291 while (line[(len - 1)] != '\n') {
295 line[(len - 1)] = ret;
297 /* Extract information. */
300 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
304 &pci_addr->function) == 4) {
314 * Verify and store value for device argument.
317 * Key argument to verify.
319 * Value associated with key.
320 * @param[in, out] conf
321 * Shared configuration data.
324 * 0 on success, negative errno value otherwise and rte_errno is set.
327 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
332 tmp = strtoul(val, NULL, 0);
335 WARN("%s: \"%s\" is not a valid integer", key, val);
338 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
339 uint32_t ports = rte_log2_u32(conf->ports.present);
342 ERROR("port index %lu outside range [0,%" PRIu32 ")",
346 if (!(conf->ports.present & (1 << tmp))) {
348 ERROR("invalid port index %lu", tmp);
351 conf->ports.enabled |= 1 << tmp;
354 WARN("%s: unknown parameter", key);
361 * Parse device parameters.
364 * Device arguments structure.
367 * 0 on success, negative errno value otherwise and rte_errno is set.
370 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
372 struct rte_kvargs *kvlist;
373 unsigned int arg_count;
379 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
380 if (kvlist == NULL) {
382 ERROR("failed to parse kvargs");
385 /* Process parameters. */
386 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
387 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
388 while (arg_count-- > 0) {
389 ret = rte_kvargs_process(kvlist,
391 (int (*)(const char *,
401 rte_kvargs_free(kvlist);
405 static struct rte_pci_driver mlx4_driver;
408 * DPDK callback to register a PCI device.
410 * This function creates an Ethernet device for each port of a given
414 * PCI driver structure (mlx4_driver).
416 * PCI device information.
419 * 0 on success, negative errno value otherwise and rte_errno is set.
422 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
424 struct ibv_device **list;
425 struct ibv_device *ibv_dev;
427 struct ibv_context *attr_ctx = NULL;
428 struct ibv_device_attr device_attr;
429 struct ibv_device_attr_ex device_attr_ex;
430 struct mlx4_conf conf = {
437 assert(pci_drv == &mlx4_driver);
438 list = ibv_get_device_list(&i);
442 if (rte_errno == ENOSYS)
443 ERROR("cannot list devices, is ib_uverbs loaded?");
448 * For each listed device, check related sysfs entry against
449 * the provided PCI ID.
452 struct rte_pci_addr pci_addr;
455 DEBUG("checking device \"%s\"", list[i]->name);
456 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
458 if ((pci_dev->addr.domain != pci_addr.domain) ||
459 (pci_dev->addr.bus != pci_addr.bus) ||
460 (pci_dev->addr.devid != pci_addr.devid) ||
461 (pci_dev->addr.function != pci_addr.function))
463 vf = (pci_dev->id.device_id ==
464 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
465 INFO("PCI information matches, using device \"%s\" (VF: %s)",
466 list[i]->name, (vf ? "true" : "false"));
467 attr_ctx = ibv_open_device(list[i]);
471 if (attr_ctx == NULL) {
472 ibv_free_device_list(list);
476 ERROR("cannot access device, is mlx4_ib loaded?");
480 ERROR("cannot use device, are drivers up to date?");
488 DEBUG("device opened");
489 if (ibv_query_device(attr_ctx, &device_attr)) {
493 INFO("%u port(s) detected", device_attr.phys_port_cnt);
494 conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
495 if (mlx4_args(pci_dev->device.devargs, &conf)) {
496 ERROR("failed to process device arguments");
500 /* Use all ports when none are defined */
501 if (!conf.ports.enabled)
502 conf.ports.enabled = conf.ports.present;
503 /* Retrieve extended device attributes. */
504 if (ibv_query_device_ex(attr_ctx, NULL, &device_attr_ex)) {
508 for (i = 0; i < device_attr.phys_port_cnt; i++) {
509 uint32_t port = i + 1; /* ports are indexed from one */
510 struct ibv_context *ctx = NULL;
511 struct ibv_port_attr port_attr;
512 struct ibv_pd *pd = NULL;
513 struct priv *priv = NULL;
514 struct rte_eth_dev *eth_dev = NULL;
515 struct ether_addr mac;
517 /* If port is not enabled, skip. */
518 if (!(conf.ports.enabled & (1 << i)))
520 DEBUG("using port %u", port);
521 ctx = ibv_open_device(ibv_dev);
526 /* Check port status. */
527 err = ibv_query_port(ctx, port, &port_attr);
530 ERROR("port query failed: %s", strerror(rte_errno));
533 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
535 ERROR("port %d is not configured in Ethernet mode",
539 if (port_attr.state != IBV_PORT_ACTIVE)
540 DEBUG("port %d is not active: \"%s\" (%d)",
541 port, ibv_port_state_str(port_attr.state),
543 /* Make asynchronous FD non-blocking to handle interrupts. */
544 if (mlx4_fd_set_non_blocking(ctx->async_fd) < 0) {
545 ERROR("cannot make asynchronous FD non-blocking: %s",
546 strerror(rte_errno));
549 /* Allocate protection domain. */
550 pd = ibv_alloc_pd(ctx);
553 ERROR("PD allocation failure");
556 /* from rte_ethdev.c */
557 priv = rte_zmalloc("ethdev private structure",
559 RTE_CACHE_LINE_SIZE);
562 ERROR("priv allocation failure");
566 priv->device_attr = device_attr;
569 priv->mtu = ETHER_MTU;
571 priv->hw_csum = !!(device_attr.device_cap_flags &
572 IBV_DEVICE_RAW_IP_CSUM);
573 DEBUG("checksum offloading is %ssupported",
574 (priv->hw_csum ? "" : "not "));
575 /* Only ConnectX-3 Pro supports tunneling. */
576 priv->hw_csum_l2tun =
578 (device_attr.vendor_part_id ==
579 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO);
580 DEBUG("L2 tunnel checksum offloads are %ssupported",
581 (priv->hw_csum_l2tun ? "" : "not "));
582 priv->hw_rss_sup = device_attr_ex.rss_caps.rx_hash_fields_mask;
583 if (!priv->hw_rss_sup) {
584 WARN("no RSS capabilities reported; disabling support"
585 " for UDP RSS and inner VXLAN RSS");
586 /* Fake support for all possible RSS hash fields. */
587 priv->hw_rss_sup = ~UINT64_C(0);
588 priv->hw_rss_sup = mlx4_conv_rss_hf(priv, -1);
589 /* Filter out known unsupported fields. */
591 ~(uint64_t)(IBV_RX_HASH_SRC_PORT_UDP |
592 IBV_RX_HASH_DST_PORT_UDP |
595 DEBUG("supported RSS hash fields mask: %016" PRIx64,
597 /* Configure the first MAC address by default. */
598 if (mlx4_get_mac(priv, &mac.addr_bytes)) {
599 ERROR("cannot get MAC address, is mlx4_en loaded?"
600 " (rte_errno: %s)", strerror(rte_errno));
603 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
605 mac.addr_bytes[0], mac.addr_bytes[1],
606 mac.addr_bytes[2], mac.addr_bytes[3],
607 mac.addr_bytes[4], mac.addr_bytes[5]);
608 /* Register MAC address. */
612 char ifname[IF_NAMESIZE];
614 if (mlx4_get_ifname(priv, &ifname) == 0)
615 DEBUG("port %u ifname is \"%s\"",
618 DEBUG("port %u ifname is unknown", priv->port);
621 /* Get actual MTU if possible. */
622 mlx4_mtu_get(priv, &priv->mtu);
623 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
624 /* from rte_ethdev.c */
626 char name[RTE_ETH_NAME_MAX_LEN];
628 snprintf(name, sizeof(name), "%s port %u",
629 ibv_get_device_name(ibv_dev), port);
630 eth_dev = rte_eth_dev_allocate(name);
632 if (eth_dev == NULL) {
633 ERROR("can not allocate rte ethdev");
637 eth_dev->data->dev_private = priv;
638 eth_dev->data->mac_addrs = priv->mac;
639 eth_dev->device = &pci_dev->device;
640 rte_eth_copy_pci_info(eth_dev, pci_dev);
641 eth_dev->device->driver = &mlx4_driver.driver;
642 /* Initialize local interrupt handle for current port. */
643 priv->intr_handle = (struct rte_intr_handle){
645 .type = RTE_INTR_HANDLE_EXT,
648 * Override ethdev interrupt handle pointer with private
649 * handle instead of that of the parent PCI device used by
650 * default. This prevents it from being shared between all
651 * ports of the same PCI device since each of them is
652 * associated its own Verbs context.
654 * Rx interrupts in particular require this as the PMD has
655 * no control over the registration of queue interrupts
656 * besides setting up eth_dev->intr_handle, the rest is
657 * handled by rte_intr_rx_ctl().
659 eth_dev->intr_handle = &priv->intr_handle;
661 eth_dev->dev_ops = &mlx4_dev_ops;
662 /* Bring Ethernet device up. */
663 DEBUG("forcing Ethernet interface up");
664 mlx4_dev_set_link_up(priv->dev);
665 /* Update link status once if waiting for LSC. */
666 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
667 mlx4_link_update(eth_dev, 0);
672 claim_zero(ibv_dealloc_pd(pd));
674 claim_zero(ibv_close_device(ctx));
676 rte_eth_dev_release_port(eth_dev);
679 if (i == device_attr.phys_port_cnt)
682 * XXX if something went wrong in the loop above, there is a resource
683 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
684 * long as the dpdk does not provide a way to deallocate a ethdev and a
685 * way to enumerate the registered ethdevs to free the previous ones.
689 claim_zero(ibv_close_device(attr_ctx));
691 ibv_free_device_list(list);
692 assert(rte_errno >= 0);
696 static const struct rte_pci_id mlx4_pci_id_map[] = {
698 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
699 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
702 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
703 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
706 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
707 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
714 static struct rte_pci_driver mlx4_driver = {
716 .name = MLX4_DRIVER_NAME
718 .id_table = mlx4_pci_id_map,
719 .probe = mlx4_pci_probe,
720 .drv_flags = RTE_PCI_DRV_INTR_LSC |
721 RTE_PCI_DRV_INTR_RMV,
725 * Driver initialization routine.
727 RTE_INIT(rte_mlx4_pmd_init);
729 rte_mlx4_pmd_init(void)
732 * MLX4_DEVICE_FATAL_CLEANUP tells ibv_destroy functions we
733 * want to get success errno value in case of calling them
734 * when the device was removed.
736 setenv("MLX4_DEVICE_FATAL_CLEANUP", "1", 1);
738 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
739 * huge pages. Calling ibv_fork_init() during init allows
740 * applications to use fork() safely for purposes other than
741 * using this PMD, which is not supported in forked processes.
743 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
745 rte_pci_register(&mlx4_driver);
748 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
749 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
750 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
751 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");