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[] = {
88 static void mlx4_dev_stop(struct rte_eth_dev *dev);
91 * DPDK callback for Ethernet device configuration.
94 * Pointer to Ethernet device structure.
97 * 0 on success, negative errno value otherwise and rte_errno is set.
100 mlx4_dev_configure(struct rte_eth_dev *dev)
102 struct priv *priv = dev->data->dev_private;
103 struct rte_flow_error error;
106 /* Prepare internal flow rules. */
107 ret = mlx4_flow_sync(priv, &error);
109 ERROR("cannot set up internal flow rules (code %d, \"%s\"),"
110 " flow error type %d, cause %p, message: %s",
111 -ret, strerror(-ret), error.type, error.cause,
112 error.message ? error.message : "(unspecified)");
115 ret = mlx4_intr_install(priv);
117 ERROR("%p: interrupt handler installation failed",
124 * DPDK callback to start the device.
126 * Simulate device start by initializing common RSS resources and attaching
127 * all configured flows.
130 * Pointer to Ethernet device structure.
133 * 0 on success, negative errno value otherwise and rte_errno is set.
136 mlx4_dev_start(struct rte_eth_dev *dev)
138 struct priv *priv = dev->data->dev_private;
139 struct rte_flow_error error;
144 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
146 ret = mlx4_rss_init(priv);
148 ERROR("%p: cannot initialize RSS resources: %s",
149 (void *)dev, strerror(-ret));
152 ret = mlx4_rxq_intr_enable(priv);
154 ERROR("%p: interrupt handler installation failed",
158 ret = mlx4_flow_sync(priv, &error);
160 ERROR("%p: cannot attach flow rules (code %d, \"%s\"),"
161 " flow error type %d, cause %p, message: %s",
163 -ret, strerror(-ret), error.type, error.cause,
164 error.message ? error.message : "(unspecified)");
168 dev->tx_pkt_burst = mlx4_tx_burst;
169 dev->rx_pkt_burst = mlx4_rx_burst;
177 * DPDK callback to stop the device.
179 * Simulate device stop by detaching all configured flows.
182 * Pointer to Ethernet device structure.
185 mlx4_dev_stop(struct rte_eth_dev *dev)
187 struct priv *priv = dev->data->dev_private;
191 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
193 dev->tx_pkt_burst = mlx4_tx_burst_removed;
194 dev->rx_pkt_burst = mlx4_rx_burst_removed;
196 mlx4_flow_sync(priv, NULL);
197 mlx4_rxq_intr_disable(priv);
198 mlx4_rss_deinit(priv);
202 * DPDK callback to close the device.
204 * Destroy all queues and objects, free memory.
207 * Pointer to Ethernet device structure.
210 mlx4_dev_close(struct rte_eth_dev *dev)
212 struct priv *priv = dev->data->dev_private;
215 DEBUG("%p: closing device \"%s\"",
217 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
218 dev->rx_pkt_burst = mlx4_rx_burst_removed;
219 dev->tx_pkt_burst = mlx4_tx_burst_removed;
221 mlx4_flow_clean(priv);
222 mlx4_rss_deinit(priv);
223 for (i = 0; i != dev->data->nb_rx_queues; ++i)
224 mlx4_rx_queue_release(dev->data->rx_queues[i]);
225 for (i = 0; i != dev->data->nb_tx_queues; ++i)
226 mlx4_tx_queue_release(dev->data->tx_queues[i]);
227 if (priv->pd != NULL) {
228 assert(priv->ctx != NULL);
229 claim_zero(ibv_dealloc_pd(priv->pd));
230 claim_zero(ibv_close_device(priv->ctx));
232 assert(priv->ctx == NULL);
233 mlx4_intr_uninstall(priv);
234 memset(priv, 0, sizeof(*priv));
237 static const struct eth_dev_ops mlx4_dev_ops = {
238 .dev_configure = mlx4_dev_configure,
239 .dev_start = mlx4_dev_start,
240 .dev_stop = mlx4_dev_stop,
241 .dev_set_link_down = mlx4_dev_set_link_down,
242 .dev_set_link_up = mlx4_dev_set_link_up,
243 .dev_close = mlx4_dev_close,
244 .link_update = mlx4_link_update,
245 .promiscuous_enable = mlx4_promiscuous_enable,
246 .promiscuous_disable = mlx4_promiscuous_disable,
247 .allmulticast_enable = mlx4_allmulticast_enable,
248 .allmulticast_disable = mlx4_allmulticast_disable,
249 .mac_addr_remove = mlx4_mac_addr_remove,
250 .mac_addr_add = mlx4_mac_addr_add,
251 .mac_addr_set = mlx4_mac_addr_set,
252 .stats_get = mlx4_stats_get,
253 .stats_reset = mlx4_stats_reset,
254 .dev_infos_get = mlx4_dev_infos_get,
255 .dev_supported_ptypes_get = mlx4_dev_supported_ptypes_get,
256 .vlan_filter_set = mlx4_vlan_filter_set,
257 .rx_queue_setup = mlx4_rx_queue_setup,
258 .tx_queue_setup = mlx4_tx_queue_setup,
259 .rx_queue_release = mlx4_rx_queue_release,
260 .tx_queue_release = mlx4_tx_queue_release,
261 .flow_ctrl_get = mlx4_flow_ctrl_get,
262 .flow_ctrl_set = mlx4_flow_ctrl_set,
263 .mtu_set = mlx4_mtu_set,
264 .filter_ctrl = mlx4_filter_ctrl,
265 .rx_queue_intr_enable = mlx4_rx_intr_enable,
266 .rx_queue_intr_disable = mlx4_rx_intr_disable,
270 * Get PCI information from struct ibv_device.
273 * Pointer to Ethernet device structure.
274 * @param[out] pci_addr
275 * PCI bus address output buffer.
278 * 0 on success, negative errno value otherwise and rte_errno is set.
281 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
282 struct rte_pci_addr *pci_addr)
286 MKSTR(path, "%s/device/uevent", device->ibdev_path);
288 file = fopen(path, "rb");
293 while (fgets(line, sizeof(line), file) == line) {
294 size_t len = strlen(line);
297 /* Truncate long lines. */
298 if (len == (sizeof(line) - 1))
299 while (line[(len - 1)] != '\n') {
303 line[(len - 1)] = ret;
305 /* Extract information. */
308 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
312 &pci_addr->function) == 4) {
322 * Verify and store value for device argument.
325 * Key argument to verify.
327 * Value associated with key.
328 * @param[in, out] conf
329 * Shared configuration data.
332 * 0 on success, negative errno value otherwise and rte_errno is set.
335 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
340 tmp = strtoul(val, NULL, 0);
343 WARN("%s: \"%s\" is not a valid integer", key, val);
346 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
347 uint32_t ports = rte_log2_u32(conf->ports.present + 1);
350 ERROR("port index %lu outside range [0,%" PRIu32 ")",
354 if (!(conf->ports.present & (1 << tmp))) {
356 ERROR("invalid port index %lu", tmp);
359 conf->ports.enabled |= 1 << tmp;
362 WARN("%s: unknown parameter", key);
369 * Parse device parameters.
372 * Device arguments structure.
375 * 0 on success, negative errno value otherwise and rte_errno is set.
378 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
380 struct rte_kvargs *kvlist;
381 unsigned int arg_count;
387 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
388 if (kvlist == NULL) {
390 ERROR("failed to parse kvargs");
393 /* Process parameters. */
394 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
395 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
396 while (arg_count-- > 0) {
397 ret = rte_kvargs_process(kvlist,
399 (int (*)(const char *,
409 rte_kvargs_free(kvlist);
413 static struct rte_pci_driver mlx4_driver;
416 * DPDK callback to register a PCI device.
418 * This function creates an Ethernet device for each port of a given
422 * PCI driver structure (mlx4_driver).
424 * PCI device information.
427 * 0 on success, negative errno value otherwise and rte_errno is set.
430 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
432 struct ibv_device **list;
433 struct ibv_device *ibv_dev;
435 struct ibv_context *attr_ctx = NULL;
436 struct ibv_device_attr device_attr;
437 struct ibv_device_attr_ex device_attr_ex;
438 struct mlx4_conf conf = {
445 assert(pci_drv == &mlx4_driver);
446 list = ibv_get_device_list(&i);
450 if (rte_errno == ENOSYS)
451 ERROR("cannot list devices, is ib_uverbs loaded?");
456 * For each listed device, check related sysfs entry against
457 * the provided PCI ID.
460 struct rte_pci_addr pci_addr;
463 DEBUG("checking device \"%s\"", list[i]->name);
464 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
466 if ((pci_dev->addr.domain != pci_addr.domain) ||
467 (pci_dev->addr.bus != pci_addr.bus) ||
468 (pci_dev->addr.devid != pci_addr.devid) ||
469 (pci_dev->addr.function != pci_addr.function))
471 vf = (pci_dev->id.device_id ==
472 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
473 INFO("PCI information matches, using device \"%s\" (VF: %s)",
474 list[i]->name, (vf ? "true" : "false"));
475 attr_ctx = ibv_open_device(list[i]);
479 if (attr_ctx == NULL) {
480 ibv_free_device_list(list);
484 ERROR("cannot access device, is mlx4_ib loaded?");
488 ERROR("cannot use device, are drivers up to date?");
496 DEBUG("device opened");
497 if (ibv_query_device(attr_ctx, &device_attr)) {
501 INFO("%u port(s) detected", device_attr.phys_port_cnt);
502 conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
503 if (mlx4_args(pci_dev->device.devargs, &conf)) {
504 ERROR("failed to process device arguments");
508 /* Use all ports when none are defined */
509 if (!conf.ports.enabled)
510 conf.ports.enabled = conf.ports.present;
511 /* Retrieve extended device attributes. */
512 if (ibv_query_device_ex(attr_ctx, NULL, &device_attr_ex)) {
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;
525 /* If port is not enabled, skip. */
526 if (!(conf.ports.enabled & (1 << i)))
528 DEBUG("using port %u", port);
529 ctx = ibv_open_device(ibv_dev);
534 /* Check port status. */
535 err = ibv_query_port(ctx, port, &port_attr);
538 ERROR("port query failed: %s", strerror(err));
541 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
543 ERROR("port %d is not configured in Ethernet mode",
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),
551 /* Make asynchronous FD non-blocking to handle interrupts. */
552 err = mlx4_fd_set_non_blocking(ctx->async_fd);
554 ERROR("cannot make asynchronous FD non-blocking: %s",
558 /* Allocate protection domain. */
559 pd = ibv_alloc_pd(ctx);
562 ERROR("PD allocation failure");
565 /* from rte_ethdev.c */
566 priv = rte_zmalloc("ethdev private structure",
568 RTE_CACHE_LINE_SIZE);
571 ERROR("priv allocation failure");
575 priv->device_attr = device_attr;
578 priv->mtu = ETHER_MTU;
580 priv->hw_csum = !!(device_attr.device_cap_flags &
581 IBV_DEVICE_RAW_IP_CSUM);
582 DEBUG("checksum offloading is %ssupported",
583 (priv->hw_csum ? "" : "not "));
584 /* Only ConnectX-3 Pro supports tunneling. */
585 priv->hw_csum_l2tun =
587 (device_attr.vendor_part_id ==
588 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO);
589 DEBUG("L2 tunnel checksum offloads are %ssupported",
590 (priv->hw_csum_l2tun ? "" : "not "));
591 priv->hw_rss_max_qps =
592 device_attr_ex.rss_caps.max_rwq_indirection_table_size;
593 DEBUG("MAX RSS queues %d", priv->hw_rss_max_qps);
594 /* Configure the first MAC address by default. */
595 err = mlx4_get_mac(priv, &mac.addr_bytes);
597 ERROR("cannot get MAC address, is mlx4_en loaded?"
598 " (error: %s)", strerror(err));
601 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
603 mac.addr_bytes[0], mac.addr_bytes[1],
604 mac.addr_bytes[2], mac.addr_bytes[3],
605 mac.addr_bytes[4], mac.addr_bytes[5]);
606 /* Register MAC address. */
610 char ifname[IF_NAMESIZE];
612 if (mlx4_get_ifname(priv, &ifname) == 0)
613 DEBUG("port %u ifname is \"%s\"",
616 DEBUG("port %u ifname is unknown", priv->port);
619 /* Get actual MTU if possible. */
620 mlx4_mtu_get(priv, &priv->mtu);
621 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
622 /* from rte_ethdev.c */
624 char name[RTE_ETH_NAME_MAX_LEN];
626 snprintf(name, sizeof(name), "%s port %u",
627 ibv_get_device_name(ibv_dev), port);
628 eth_dev = rte_eth_dev_allocate(name);
630 if (eth_dev == NULL) {
632 ERROR("can not allocate rte ethdev");
635 eth_dev->data->dev_private = priv;
636 eth_dev->data->mac_addrs = priv->mac;
637 eth_dev->device = &pci_dev->device;
638 rte_eth_copy_pci_info(eth_dev, pci_dev);
639 eth_dev->device->driver = &mlx4_driver.driver;
640 /* Initialize local interrupt handle for current port. */
641 priv->intr_handle = (struct rte_intr_handle){
643 .type = RTE_INTR_HANDLE_EXT,
646 * Override ethdev interrupt handle pointer with private
647 * handle instead of that of the parent PCI device used by
648 * default. This prevents it from being shared between all
649 * ports of the same PCI device since each of them is
650 * associated its own Verbs context.
652 * Rx interrupts in particular require this as the PMD has
653 * no control over the registration of queue interrupts
654 * besides setting up eth_dev->intr_handle, the rest is
655 * handled by rte_intr_rx_ctl().
657 eth_dev->intr_handle = &priv->intr_handle;
659 eth_dev->dev_ops = &mlx4_dev_ops;
660 /* Bring Ethernet device up. */
661 DEBUG("forcing Ethernet interface up");
662 mlx4_dev_set_link_up(priv->dev);
663 /* Update link status once if waiting for LSC. */
664 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
665 mlx4_link_update(eth_dev, 0);
670 claim_zero(ibv_dealloc_pd(pd));
672 claim_zero(ibv_close_device(ctx));
674 rte_eth_dev_release_port(eth_dev);
678 * XXX if something went wrong in the loop above, there is a resource
679 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
680 * long as the dpdk does not provide a way to deallocate a ethdev and a
681 * way to enumerate the registered ethdevs to free the previous ones.
685 claim_zero(ibv_close_device(attr_ctx));
687 ibv_free_device_list(list);
693 static const struct rte_pci_id mlx4_pci_id_map[] = {
695 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
696 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
699 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
700 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
703 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
704 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
711 static struct rte_pci_driver mlx4_driver = {
713 .name = MLX4_DRIVER_NAME
715 .id_table = mlx4_pci_id_map,
716 .probe = mlx4_pci_probe,
717 .drv_flags = RTE_PCI_DRV_INTR_LSC |
718 RTE_PCI_DRV_INTR_RMV,
722 * Driver initialization routine.
724 RTE_INIT(rte_mlx4_pmd_init);
726 rte_mlx4_pmd_init(void)
729 * MLX4_DEVICE_FATAL_CLEANUP tells ibv_destroy functions we
730 * want to get success errno value in case of calling them
731 * when the device was removed.
733 setenv("MLX4_DEVICE_FATAL_CLEANUP", "1", 1);
735 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
736 * huge pages. Calling ibv_fork_init() during init allows
737 * applications to use fork() safely for purposes other than
738 * using this PMD, which is not supported in forked processes.
740 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
742 rte_pci_register(&mlx4_driver);
745 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
746 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
747 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
748 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");