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.
45 #include <rte_ether.h>
46 #include <rte_ethdev.h>
47 #include <rte_ethdev_pci.h>
50 #include <rte_errno.h>
51 #include <rte_mempool.h>
52 #include <rte_malloc.h>
53 #include <rte_memory.h>
55 #include <rte_kvargs.h>
56 #include <rte_interrupts.h>
57 #include <rte_common.h>
61 #include "mlx4_flow.h"
62 #include "mlx4_rxtx.h"
63 #include "mlx4_utils.h"
65 /** Configuration structure for device arguments. */
68 uint32_t present; /**< Bit-field for existing ports. */
69 uint32_t enabled; /**< Bit-field for user-enabled ports. */
73 /* Available parameters list. */
74 const char *pmd_mlx4_init_params[] = {
79 /* Device configuration. */
82 * DPDK callback for Ethernet device configuration.
84 * Prepare the driver for a given number of TX and RX queues.
87 * Pointer to Ethernet device structure.
90 * 0 on success, negative errno value otherwise and rte_errno is set.
93 mlx4_dev_configure(struct rte_eth_dev *dev)
95 struct priv *priv = dev->data->dev_private;
96 unsigned int rxqs_n = dev->data->nb_rx_queues;
97 unsigned int txqs_n = dev->data->nb_tx_queues;
99 priv->rxqs = (void *)dev->data->rx_queues;
100 priv->txqs = (void *)dev->data->tx_queues;
101 if (txqs_n != priv->txqs_n) {
102 INFO("%p: TX queues number update: %u -> %u",
103 (void *)dev, priv->txqs_n, txqs_n);
104 priv->txqs_n = txqs_n;
106 if (rxqs_n != priv->rxqs_n) {
107 INFO("%p: Rx queues number update: %u -> %u",
108 (void *)dev, priv->rxqs_n, rxqs_n);
109 priv->rxqs_n = rxqs_n;
114 struct mlx4_check_mempool_data {
120 /* Called by mlx4_check_mempool() when iterating the memory chunks. */
121 static void mlx4_check_mempool_cb(struct rte_mempool *mp,
122 void *opaque, struct rte_mempool_memhdr *memhdr,
125 struct mlx4_check_mempool_data *data = opaque;
129 /* It already failed, skip the next chunks. */
132 /* It is the first chunk. */
133 if (data->start == NULL && data->end == NULL) {
134 data->start = memhdr->addr;
135 data->end = data->start + memhdr->len;
138 if (data->end == memhdr->addr) {
139 data->end += memhdr->len;
142 if (data->start == (char *)memhdr->addr + memhdr->len) {
143 data->start -= memhdr->len;
146 /* Error, mempool is not virtually contigous. */
151 * Check if a mempool can be used: it must be virtually contiguous.
154 * Pointer to memory pool.
156 * Pointer to the start address of the mempool virtual memory area
158 * Pointer to the end address of the mempool virtual memory area
161 * 0 on success (mempool is virtually contiguous), -1 on error.
163 static int mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start,
166 struct mlx4_check_mempool_data data;
168 memset(&data, 0, sizeof(data));
169 rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
170 *start = (uintptr_t)data.start;
171 *end = (uintptr_t)data.end;
176 * Register mempool as a memory region.
179 * Pointer to protection domain.
181 * Pointer to memory pool.
184 * Memory region pointer, NULL in case of error and rte_errno is set.
187 mlx4_mp2mr(struct ibv_pd *pd, struct rte_mempool *mp)
189 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
195 if (mlx4_check_mempool(mp, &start, &end) != 0) {
197 ERROR("mempool %p: not virtually contiguous",
201 DEBUG("mempool %p area start=%p end=%p size=%zu",
202 (void *)mp, (void *)start, (void *)end,
203 (size_t)(end - start));
204 /* Round start and end to page boundary if found in memory segments. */
205 for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
206 uintptr_t addr = (uintptr_t)ms[i].addr;
207 size_t len = ms[i].len;
208 unsigned int align = ms[i].hugepage_sz;
210 if ((start > addr) && (start < addr + len))
211 start = RTE_ALIGN_FLOOR(start, align);
212 if ((end > addr) && (end < addr + len))
213 end = RTE_ALIGN_CEIL(end, align);
215 DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
216 (void *)mp, (void *)start, (void *)end,
217 (size_t)(end - start));
221 IBV_ACCESS_LOCAL_WRITE);
223 rte_errno = errno ? errno : EINVAL;
228 * DPDK callback to start the device.
230 * Simulate device start by attaching all configured flows.
233 * Pointer to Ethernet device structure.
236 * 0 on success, negative errno value otherwise and rte_errno is set.
239 mlx4_dev_start(struct rte_eth_dev *dev)
241 struct priv *priv = dev->data->dev_private;
246 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
248 ret = mlx4_mac_addr_add(priv);
251 ret = mlx4_intr_install(priv);
253 ERROR("%p: interrupt handler installation failed",
257 ret = mlx4_priv_flow_start(priv);
259 ERROR("%p: flow start failed: %s",
260 (void *)dev, strerror(ret));
266 mlx4_mac_addr_del(priv);
272 * DPDK callback to stop the device.
274 * Simulate device stop by detaching all configured flows.
277 * Pointer to Ethernet device structure.
280 mlx4_dev_stop(struct rte_eth_dev *dev)
282 struct priv *priv = dev->data->dev_private;
286 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
288 mlx4_priv_flow_stop(priv);
289 mlx4_intr_uninstall(priv);
290 mlx4_mac_addr_del(priv);
294 * DPDK callback to close the device.
296 * Destroy all queues and objects, free memory.
299 * Pointer to Ethernet device structure.
302 mlx4_dev_close(struct rte_eth_dev *dev)
304 struct priv *priv = dev->data->dev_private;
310 DEBUG("%p: closing device \"%s\"",
312 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
313 mlx4_mac_addr_del(priv);
315 * Prevent crashes when queues are still in use. This is unfortunately
316 * still required for DPDK 1.3 because some programs (such as testpmd)
317 * never release them before closing the device.
319 dev->rx_pkt_burst = mlx4_rx_burst_removed;
320 dev->tx_pkt_burst = mlx4_tx_burst_removed;
321 if (priv->rxqs != NULL) {
322 /* XXX race condition if mlx4_rx_burst() is still running. */
324 for (i = 0; (i != priv->rxqs_n); ++i) {
325 tmp = (*priv->rxqs)[i];
328 (*priv->rxqs)[i] = NULL;
329 mlx4_rxq_cleanup(tmp);
335 if (priv->txqs != NULL) {
336 /* XXX race condition if mlx4_tx_burst() is still running. */
338 for (i = 0; (i != priv->txqs_n); ++i) {
339 tmp = (*priv->txqs)[i];
342 (*priv->txqs)[i] = NULL;
343 mlx4_txq_cleanup(tmp);
349 if (priv->pd != NULL) {
350 assert(priv->ctx != NULL);
351 claim_zero(ibv_dealloc_pd(priv->pd));
352 claim_zero(ibv_close_device(priv->ctx));
354 assert(priv->ctx == NULL);
355 mlx4_intr_uninstall(priv);
356 memset(priv, 0, sizeof(*priv));
359 const struct rte_flow_ops mlx4_flow_ops = {
360 .validate = mlx4_flow_validate,
361 .create = mlx4_flow_create,
362 .destroy = mlx4_flow_destroy,
363 .flush = mlx4_flow_flush,
365 .isolate = mlx4_flow_isolate,
369 * Manage filter operations.
372 * Pointer to Ethernet device structure.
376 * Operation to perform.
378 * Pointer to operation-specific structure.
381 * 0 on success, negative errno value otherwise and rte_errno is set.
384 mlx4_dev_filter_ctrl(struct rte_eth_dev *dev,
385 enum rte_filter_type filter_type,
386 enum rte_filter_op filter_op,
389 switch (filter_type) {
390 case RTE_ETH_FILTER_GENERIC:
391 if (filter_op != RTE_ETH_FILTER_GET)
393 *(const void **)arg = &mlx4_flow_ops;
396 ERROR("%p: filter type (%d) not supported",
397 (void *)dev, filter_type);
404 static const struct eth_dev_ops mlx4_dev_ops = {
405 .dev_configure = mlx4_dev_configure,
406 .dev_start = mlx4_dev_start,
407 .dev_stop = mlx4_dev_stop,
408 .dev_set_link_down = mlx4_dev_set_link_down,
409 .dev_set_link_up = mlx4_dev_set_link_up,
410 .dev_close = mlx4_dev_close,
411 .link_update = mlx4_link_update,
412 .stats_get = mlx4_stats_get,
413 .stats_reset = mlx4_stats_reset,
414 .dev_infos_get = mlx4_dev_infos_get,
415 .rx_queue_setup = mlx4_rx_queue_setup,
416 .tx_queue_setup = mlx4_tx_queue_setup,
417 .rx_queue_release = mlx4_rx_queue_release,
418 .tx_queue_release = mlx4_tx_queue_release,
419 .flow_ctrl_get = mlx4_flow_ctrl_get,
420 .flow_ctrl_set = mlx4_flow_ctrl_set,
421 .mtu_set = mlx4_mtu_set,
422 .filter_ctrl = mlx4_dev_filter_ctrl,
423 .rx_queue_intr_enable = mlx4_rx_intr_enable,
424 .rx_queue_intr_disable = mlx4_rx_intr_disable,
428 * Get PCI information from struct ibv_device.
431 * Pointer to Ethernet device structure.
432 * @param[out] pci_addr
433 * PCI bus address output buffer.
436 * 0 on success, negative errno value otherwise and rte_errno is set.
439 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
440 struct rte_pci_addr *pci_addr)
444 MKSTR(path, "%s/device/uevent", device->ibdev_path);
446 file = fopen(path, "rb");
451 while (fgets(line, sizeof(line), file) == line) {
452 size_t len = strlen(line);
455 /* Truncate long lines. */
456 if (len == (sizeof(line) - 1))
457 while (line[(len - 1)] != '\n') {
461 line[(len - 1)] = ret;
463 /* Extract information. */
466 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
470 &pci_addr->function) == 4) {
480 * Verify and store value for device argument.
483 * Key argument to verify.
485 * Value associated with key.
486 * @param[in, out] conf
487 * Shared configuration data.
490 * 0 on success, negative errno value otherwise and rte_errno is set.
493 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
498 tmp = strtoul(val, NULL, 0);
501 WARN("%s: \"%s\" is not a valid integer", key, val);
504 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
505 uint32_t ports = rte_log2_u32(conf->ports.present);
508 ERROR("port index %lu outside range [0,%" PRIu32 ")",
512 if (!(conf->ports.present & (1 << tmp))) {
514 ERROR("invalid port index %lu", tmp);
517 conf->ports.enabled |= 1 << tmp;
520 WARN("%s: unknown parameter", key);
527 * Parse device parameters.
530 * Device arguments structure.
533 * 0 on success, negative errno value otherwise and rte_errno is set.
536 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
538 struct rte_kvargs *kvlist;
539 unsigned int arg_count;
545 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
546 if (kvlist == NULL) {
548 ERROR("failed to parse kvargs");
551 /* Process parameters. */
552 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
553 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
554 while (arg_count-- > 0) {
555 ret = rte_kvargs_process(kvlist,
557 (int (*)(const char *,
567 rte_kvargs_free(kvlist);
571 static struct rte_pci_driver mlx4_driver;
574 * DPDK callback to register a PCI device.
576 * This function creates an Ethernet device for each port of a given
580 * PCI driver structure (mlx4_driver).
582 * PCI device information.
585 * 0 on success, negative errno value otherwise and rte_errno is set.
588 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
590 struct ibv_device **list;
591 struct ibv_device *ibv_dev;
593 struct ibv_context *attr_ctx = NULL;
594 struct ibv_device_attr device_attr;
595 struct mlx4_conf conf = {
602 assert(pci_drv == &mlx4_driver);
603 list = ibv_get_device_list(&i);
607 if (rte_errno == ENOSYS)
608 ERROR("cannot list devices, is ib_uverbs loaded?");
613 * For each listed device, check related sysfs entry against
614 * the provided PCI ID.
617 struct rte_pci_addr pci_addr;
620 DEBUG("checking device \"%s\"", list[i]->name);
621 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
623 if ((pci_dev->addr.domain != pci_addr.domain) ||
624 (pci_dev->addr.bus != pci_addr.bus) ||
625 (pci_dev->addr.devid != pci_addr.devid) ||
626 (pci_dev->addr.function != pci_addr.function))
628 vf = (pci_dev->id.device_id ==
629 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
630 INFO("PCI information matches, using device \"%s\" (VF: %s)",
631 list[i]->name, (vf ? "true" : "false"));
632 attr_ctx = ibv_open_device(list[i]);
636 if (attr_ctx == NULL) {
637 ibv_free_device_list(list);
641 ERROR("cannot access device, is mlx4_ib loaded?");
645 ERROR("cannot use device, are drivers up to date?");
653 DEBUG("device opened");
654 if (ibv_query_device(attr_ctx, &device_attr)) {
658 INFO("%u port(s) detected", device_attr.phys_port_cnt);
659 conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
660 if (mlx4_args(pci_dev->device.devargs, &conf)) {
661 ERROR("failed to process device arguments");
665 /* Use all ports when none are defined */
666 if (!conf.ports.enabled)
667 conf.ports.enabled = conf.ports.present;
668 for (i = 0; i < device_attr.phys_port_cnt; i++) {
669 uint32_t port = i + 1; /* ports are indexed from one */
670 struct ibv_context *ctx = NULL;
671 struct ibv_port_attr port_attr;
672 struct ibv_pd *pd = NULL;
673 struct priv *priv = NULL;
674 struct rte_eth_dev *eth_dev = NULL;
675 struct ether_addr mac;
677 /* If port is not enabled, skip. */
678 if (!(conf.ports.enabled & (1 << i)))
680 DEBUG("using port %u", port);
681 ctx = ibv_open_device(ibv_dev);
686 /* Check port status. */
687 err = ibv_query_port(ctx, port, &port_attr);
690 ERROR("port query failed: %s", strerror(rte_errno));
693 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
695 ERROR("port %d is not configured in Ethernet mode",
699 if (port_attr.state != IBV_PORT_ACTIVE)
700 DEBUG("port %d is not active: \"%s\" (%d)",
701 port, ibv_port_state_str(port_attr.state),
703 /* Make asynchronous FD non-blocking to handle interrupts. */
704 if (mlx4_fd_set_non_blocking(ctx->async_fd) < 0) {
705 ERROR("cannot make asynchronous FD non-blocking: %s",
706 strerror(rte_errno));
709 /* Allocate protection domain. */
710 pd = ibv_alloc_pd(ctx);
713 ERROR("PD allocation failure");
716 /* from rte_ethdev.c */
717 priv = rte_zmalloc("ethdev private structure",
719 RTE_CACHE_LINE_SIZE);
722 ERROR("priv allocation failure");
726 priv->device_attr = device_attr;
729 priv->mtu = ETHER_MTU;
731 /* Configure the first MAC address by default. */
732 if (mlx4_get_mac(priv, &mac.addr_bytes)) {
733 ERROR("cannot get MAC address, is mlx4_en loaded?"
734 " (rte_errno: %s)", strerror(rte_errno));
737 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
739 mac.addr_bytes[0], mac.addr_bytes[1],
740 mac.addr_bytes[2], mac.addr_bytes[3],
741 mac.addr_bytes[4], mac.addr_bytes[5]);
742 /* Register MAC address. */
744 if (mlx4_mac_addr_add(priv))
748 char ifname[IF_NAMESIZE];
750 if (mlx4_get_ifname(priv, &ifname) == 0)
751 DEBUG("port %u ifname is \"%s\"",
754 DEBUG("port %u ifname is unknown", priv->port);
757 /* Get actual MTU if possible. */
758 mlx4_mtu_get(priv, &priv->mtu);
759 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
760 /* from rte_ethdev.c */
762 char name[RTE_ETH_NAME_MAX_LEN];
764 snprintf(name, sizeof(name), "%s port %u",
765 ibv_get_device_name(ibv_dev), port);
766 eth_dev = rte_eth_dev_allocate(name);
768 if (eth_dev == NULL) {
769 ERROR("can not allocate rte ethdev");
773 eth_dev->data->dev_private = priv;
774 eth_dev->data->mac_addrs = &priv->mac;
775 eth_dev->device = &pci_dev->device;
776 rte_eth_copy_pci_info(eth_dev, pci_dev);
777 eth_dev->device->driver = &mlx4_driver.driver;
778 /* Initialize local interrupt handle for current port. */
779 priv->intr_handle = (struct rte_intr_handle){
781 .type = RTE_INTR_HANDLE_EXT,
784 * Override ethdev interrupt handle pointer with private
785 * handle instead of that of the parent PCI device used by
786 * default. This prevents it from being shared between all
787 * ports of the same PCI device since each of them is
788 * associated its own Verbs context.
790 * Rx interrupts in particular require this as the PMD has
791 * no control over the registration of queue interrupts
792 * besides setting up eth_dev->intr_handle, the rest is
793 * handled by rte_intr_rx_ctl().
795 eth_dev->intr_handle = &priv->intr_handle;
797 eth_dev->dev_ops = &mlx4_dev_ops;
798 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
799 /* Bring Ethernet device up. */
800 DEBUG("forcing Ethernet interface up");
801 mlx4_dev_set_link_up(priv->dev);
802 /* Update link status once if waiting for LSC. */
803 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
804 mlx4_link_update(eth_dev, 0);
809 claim_zero(ibv_dealloc_pd(pd));
811 claim_zero(ibv_close_device(ctx));
813 rte_eth_dev_release_port(eth_dev);
816 if (i == device_attr.phys_port_cnt)
819 * XXX if something went wrong in the loop above, there is a resource
820 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
821 * long as the dpdk does not provide a way to deallocate a ethdev and a
822 * way to enumerate the registered ethdevs to free the previous ones.
826 claim_zero(ibv_close_device(attr_ctx));
828 ibv_free_device_list(list);
829 assert(rte_errno >= 0);
833 static const struct rte_pci_id mlx4_pci_id_map[] = {
835 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
836 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
839 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
840 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
843 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
844 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
851 static struct rte_pci_driver mlx4_driver = {
853 .name = MLX4_DRIVER_NAME
855 .id_table = mlx4_pci_id_map,
856 .probe = mlx4_pci_probe,
857 .drv_flags = RTE_PCI_DRV_INTR_LSC |
858 RTE_PCI_DRV_INTR_RMV,
862 * Driver initialization routine.
864 RTE_INIT(rte_mlx4_pmd_init);
866 rte_mlx4_pmd_init(void)
869 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
870 * huge pages. Calling ibv_fork_init() during init allows
871 * applications to use fork() safely for purposes other than
872 * using this PMD, which is not supported in forked processes.
874 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
876 rte_pci_register(&mlx4_driver);
879 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
880 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
881 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
882 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");