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.
50 /* Verbs headers do not support -pedantic. */
52 #pragma GCC diagnostic ignored "-Wpedantic"
54 #include <infiniband/verbs.h>
56 #pragma GCC diagnostic error "-Wpedantic"
59 #include <rte_common.h>
60 #include <rte_config.h>
62 #include <rte_errno.h>
63 #include <rte_ethdev_driver.h>
64 #include <rte_ethdev_pci.h>
65 #include <rte_ether.h>
67 #include <rte_interrupts.h>
68 #include <rte_kvargs.h>
69 #include <rte_malloc.h>
73 #include "mlx4_glue.h"
74 #include "mlx4_flow.h"
75 #include "mlx4_rxtx.h"
76 #include "mlx4_utils.h"
78 /** Configuration structure for device arguments. */
81 uint32_t present; /**< Bit-field for existing ports. */
82 uint32_t enabled; /**< Bit-field for user-enabled ports. */
86 /* Available parameters list. */
87 const char *pmd_mlx4_init_params[] = {
93 * DPDK callback for Ethernet device configuration.
96 * Pointer to Ethernet device structure.
99 * 0 on success, negative errno value otherwise and rte_errno is set.
102 mlx4_dev_configure(struct rte_eth_dev *dev)
104 struct priv *priv = dev->data->dev_private;
105 struct rte_flow_error error;
108 /* Prepare internal flow rules. */
109 ret = mlx4_flow_sync(priv, &error);
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)");
117 ret = mlx4_intr_install(priv);
119 ERROR("%p: interrupt handler installation failed",
126 * DPDK callback to start the device.
128 * Simulate device start by initializing common RSS resources and attaching
129 * all configured flows.
132 * Pointer to Ethernet device structure.
135 * 0 on success, negative errno value otherwise and rte_errno is set.
138 mlx4_dev_start(struct rte_eth_dev *dev)
140 struct priv *priv = dev->data->dev_private;
141 struct rte_flow_error error;
146 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
148 ret = mlx4_rss_init(priv);
150 ERROR("%p: cannot initialize RSS resources: %s",
151 (void *)dev, strerror(-ret));
154 ret = mlx4_rxq_intr_enable(priv);
156 ERROR("%p: interrupt handler installation failed",
160 ret = mlx4_flow_sync(priv, &error);
162 ERROR("%p: cannot attach flow rules (code %d, \"%s\"),"
163 " flow error type %d, cause %p, message: %s",
165 -ret, strerror(-ret), error.type, error.cause,
166 error.message ? error.message : "(unspecified)");
170 dev->tx_pkt_burst = mlx4_tx_burst;
171 dev->rx_pkt_burst = mlx4_rx_burst;
180 * DPDK callback to stop the device.
182 * Simulate device stop by detaching all configured flows.
185 * Pointer to Ethernet device structure.
188 mlx4_dev_stop(struct rte_eth_dev *dev)
190 struct priv *priv = dev->data->dev_private;
194 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
196 dev->tx_pkt_burst = mlx4_tx_burst_removed;
197 dev->rx_pkt_burst = mlx4_rx_burst_removed;
199 mlx4_flow_sync(priv, NULL);
200 mlx4_rxq_intr_disable(priv);
201 mlx4_rss_deinit(priv);
205 * DPDK callback to close the device.
207 * Destroy all queues and objects, free memory.
210 * Pointer to Ethernet device structure.
213 mlx4_dev_close(struct rte_eth_dev *dev)
215 struct priv *priv = dev->data->dev_private;
218 DEBUG("%p: closing device \"%s\"",
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;
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));
234 assert(priv->ctx == NULL);
235 mlx4_intr_uninstall(priv);
236 memset(priv, 0, sizeof(*priv));
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,
273 * Get PCI information from struct ibv_device.
276 * Pointer to Ethernet device structure.
277 * @param[out] pci_addr
278 * PCI bus address output buffer.
281 * 0 on success, negative errno value otherwise and rte_errno is set.
284 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
285 struct rte_pci_addr *pci_addr)
289 MKSTR(path, "%s/device/uevent", device->ibdev_path);
291 file = fopen(path, "rb");
296 while (fgets(line, sizeof(line), file) == line) {
297 size_t len = strlen(line);
300 /* Truncate long lines. */
301 if (len == (sizeof(line) - 1))
302 while (line[(len - 1)] != '\n') {
306 line[(len - 1)] = ret;
308 /* Extract information. */
311 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
315 &pci_addr->function) == 4) {
325 * Verify and store value for device argument.
328 * Key argument to verify.
330 * Value associated with key.
331 * @param[in, out] conf
332 * Shared configuration data.
335 * 0 on success, negative errno value otherwise and rte_errno is set.
338 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
343 tmp = strtoul(val, NULL, 0);
346 WARN("%s: \"%s\" is not a valid integer", key, val);
349 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
350 uint32_t ports = rte_log2_u32(conf->ports.present + 1);
353 ERROR("port index %lu outside range [0,%" PRIu32 ")",
357 if (!(conf->ports.present & (1 << tmp))) {
359 ERROR("invalid port index %lu", tmp);
362 conf->ports.enabled |= 1 << tmp;
365 WARN("%s: unknown parameter", key);
372 * Parse device parameters.
375 * Device arguments structure.
378 * 0 on success, negative errno value otherwise and rte_errno is set.
381 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
383 struct rte_kvargs *kvlist;
384 unsigned int arg_count;
390 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
391 if (kvlist == NULL) {
393 ERROR("failed to parse kvargs");
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,
402 (int (*)(const char *,
412 rte_kvargs_free(kvlist);
416 static struct rte_pci_driver mlx4_driver;
419 * DPDK callback to register a PCI device.
421 * This function creates an Ethernet device for each port of a given
425 * PCI driver structure (mlx4_driver).
427 * PCI device information.
430 * 0 on success, negative errno value otherwise and rte_errno is set.
433 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
435 struct ibv_device **list;
436 struct ibv_device *ibv_dev;
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 = {
448 assert(pci_drv == &mlx4_driver);
449 list = mlx4_glue->get_device_list(&i);
453 if (rte_errno == ENOSYS)
454 ERROR("cannot list devices, is ib_uverbs loaded?");
459 * For each listed device, check related sysfs entry against
460 * the provided PCI ID.
463 struct rte_pci_addr pci_addr;
466 DEBUG("checking device \"%s\"", list[i]->name);
467 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
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))
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]);
482 if (attr_ctx == NULL) {
483 mlx4_glue->free_device_list(list);
487 ERROR("cannot access device, is mlx4_ib loaded?");
491 ERROR("cannot use device, are drivers up to date?");
499 DEBUG("device opened");
500 if (mlx4_glue->query_device(attr_ctx, &device_attr)) {
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");
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)) {
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;
529 /* If port is not enabled, skip. */
530 if (!(conf.ports.enabled & (1 << i)))
532 DEBUG("using port %u", port);
533 ctx = mlx4_glue->open_device(ibv_dev);
538 /* Check port status. */
539 err = mlx4_glue->query_port(ctx, port, &port_attr);
542 ERROR("port query failed: %s", strerror(rte_errno));
545 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
547 ERROR("port %d is not configured in Ethernet mode",
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),
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));
561 /* Allocate protection domain. */
562 pd = mlx4_glue->alloc_pd(ctx);
565 ERROR("PD allocation failure");
568 /* from rte_ethdev.c */
569 priv = rte_zmalloc("ethdev private structure",
571 RTE_CACHE_LINE_SIZE);
574 ERROR("priv allocation failure");
578 priv->device_attr = device_attr;
581 priv->mtu = ETHER_MTU;
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 =
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. */
603 ~(uint64_t)(IBV_RX_HASH_SRC_PORT_UDP |
604 IBV_RX_HASH_DST_PORT_UDP |
607 DEBUG("supported RSS hash fields mask: %016" PRIx64,
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));
615 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
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. */
624 char ifname[IF_NAMESIZE];
626 if (mlx4_get_ifname(priv, &ifname) == 0)
627 DEBUG("port %u ifname is \"%s\"",
630 DEBUG("port %u ifname is unknown", priv->port);
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 */
638 char name[RTE_ETH_NAME_MAX_LEN];
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);
644 if (eth_dev == NULL) {
645 ERROR("can not allocate rte ethdev");
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){
657 .type = RTE_INTR_HANDLE_EXT,
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.
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().
671 eth_dev->intr_handle = &priv->intr_handle;
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);
684 claim_zero(mlx4_glue->dealloc_pd(pd));
686 claim_zero(mlx4_glue->close_device(ctx));
688 rte_eth_dev_release_port(eth_dev);
691 if (i == device_attr.phys_port_cnt)
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.
701 claim_zero(mlx4_glue->close_device(attr_ctx));
703 mlx4_glue->free_device_list(list);
704 assert(rte_errno >= 0);
708 static const struct rte_pci_id mlx4_pci_id_map[] = {
710 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
711 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
714 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
715 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
718 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
719 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
726 static struct rte_pci_driver mlx4_driver = {
728 .name = MLX4_DRIVER_NAME
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,
736 #ifdef RTE_LIBRTE_MLX4_DLOPEN_DEPS
739 * Initialization routine for run-time dependency on rdma-core.
748 handle = dlopen(MLX4_GLUE, RTLD_LAZY);
753 WARN("cannot load glue library: %s", dlmsg);
756 sym = dlsym(handle, "mlx4_glue");
761 ERROR("cannot resolve glue symbol: %s", dlmsg);
769 WARN("cannot initialize PMD due to missing run-time"
770 " dependency on rdma-core libraries (libibverbs,"
778 * Driver initialization routine.
780 RTE_INIT(rte_mlx4_pmd_init);
782 rte_mlx4_pmd_init(void)
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.
789 setenv("MLX4_DEVICE_FATAL_CLEANUP", "1", 1);
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.
796 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
797 #ifdef RTE_LIBRTE_MLX4_DLOPEN_DEPS
798 if (mlx4_glue_init())
802 mlx4_glue->fork_init();
803 rte_pci_register(&mlx4_driver);
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");