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>
54 #include <rte_kvargs.h>
55 #include <rte_interrupts.h>
56 #include <rte_common.h>
60 #include "mlx4_flow.h"
61 #include "mlx4_rxtx.h"
62 #include "mlx4_utils.h"
64 /** Configuration structure for device arguments. */
67 uint32_t present; /**< Bit-field for existing ports. */
68 uint32_t enabled; /**< Bit-field for user-enabled ports. */
72 /* Available parameters list. */
73 const char *pmd_mlx4_init_params[] = {
78 /* Device configuration. */
81 * DPDK callback for Ethernet device configuration.
83 * Prepare the driver for a given number of TX and RX queues.
86 * Pointer to Ethernet device structure.
89 * 0 on success, negative errno value otherwise and rte_errno is set.
92 mlx4_dev_configure(struct rte_eth_dev *dev)
94 struct priv *priv = dev->data->dev_private;
95 unsigned int rxqs_n = dev->data->nb_rx_queues;
96 unsigned int txqs_n = dev->data->nb_tx_queues;
98 priv->rxqs = (void *)dev->data->rx_queues;
99 priv->txqs = (void *)dev->data->tx_queues;
100 if (txqs_n != priv->txqs_n) {
101 INFO("%p: TX queues number update: %u -> %u",
102 (void *)dev, priv->txqs_n, txqs_n);
103 priv->txqs_n = txqs_n;
105 if (rxqs_n != priv->rxqs_n) {
106 INFO("%p: Rx queues number update: %u -> %u",
107 (void *)dev, priv->rxqs_n, rxqs_n);
108 priv->rxqs_n = rxqs_n;
113 struct mlx4_check_mempool_data {
119 /* Called by mlx4_check_mempool() when iterating the memory chunks. */
120 static void mlx4_check_mempool_cb(struct rte_mempool *mp,
121 void *opaque, struct rte_mempool_memhdr *memhdr,
124 struct mlx4_check_mempool_data *data = opaque;
128 /* It already failed, skip the next chunks. */
131 /* It is the first chunk. */
132 if (data->start == NULL && data->end == NULL) {
133 data->start = memhdr->addr;
134 data->end = data->start + memhdr->len;
137 if (data->end == memhdr->addr) {
138 data->end += memhdr->len;
141 if (data->start == (char *)memhdr->addr + memhdr->len) {
142 data->start -= memhdr->len;
145 /* Error, mempool is not virtually contigous. */
150 * Check if a mempool can be used: it must be virtually contiguous.
153 * Pointer to memory pool.
155 * Pointer to the start address of the mempool virtual memory area
157 * Pointer to the end address of the mempool virtual memory area
160 * 0 on success (mempool is virtually contiguous), -1 on error.
162 static int mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start,
165 struct mlx4_check_mempool_data data;
167 memset(&data, 0, sizeof(data));
168 rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
169 *start = (uintptr_t)data.start;
170 *end = (uintptr_t)data.end;
175 * Register mempool as a memory region.
178 * Pointer to protection domain.
180 * Pointer to memory pool.
183 * Memory region pointer, NULL in case of error and rte_errno is set.
186 mlx4_mp2mr(struct ibv_pd *pd, struct rte_mempool *mp)
188 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
194 if (mlx4_check_mempool(mp, &start, &end) != 0) {
196 ERROR("mempool %p: not virtually contiguous",
200 DEBUG("mempool %p area start=%p end=%p size=%zu",
201 (void *)mp, (void *)start, (void *)end,
202 (size_t)(end - start));
203 /* Round start and end to page boundary if found in memory segments. */
204 for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
205 uintptr_t addr = (uintptr_t)ms[i].addr;
206 size_t len = ms[i].len;
207 unsigned int align = ms[i].hugepage_sz;
209 if ((start > addr) && (start < addr + len))
210 start = RTE_ALIGN_FLOOR(start, align);
211 if ((end > addr) && (end < addr + len))
212 end = RTE_ALIGN_CEIL(end, align);
214 DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
215 (void *)mp, (void *)start, (void *)end,
216 (size_t)(end - start));
220 IBV_ACCESS_LOCAL_WRITE);
222 rte_errno = errno ? errno : EINVAL;
227 * DPDK callback to start the device.
229 * Simulate device start by attaching all configured flows.
232 * Pointer to Ethernet device structure.
235 * 0 on success, negative errno value otherwise and rte_errno is set.
238 mlx4_dev_start(struct rte_eth_dev *dev)
240 struct priv *priv = dev->data->dev_private;
245 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
247 ret = mlx4_mac_addr_add(priv);
250 ret = mlx4_intr_install(priv);
252 ERROR("%p: interrupt handler installation failed",
256 ret = mlx4_flow_start(priv);
258 ERROR("%p: flow start failed: %s",
259 (void *)dev, strerror(ret));
265 mlx4_mac_addr_del(priv);
271 * DPDK callback to stop the device.
273 * Simulate device stop by detaching all configured flows.
276 * Pointer to Ethernet device structure.
279 mlx4_dev_stop(struct rte_eth_dev *dev)
281 struct priv *priv = dev->data->dev_private;
285 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
287 mlx4_flow_stop(priv);
288 mlx4_intr_uninstall(priv);
289 mlx4_mac_addr_del(priv);
293 * DPDK callback to close the device.
295 * Destroy all queues and objects, free memory.
298 * Pointer to Ethernet device structure.
301 mlx4_dev_close(struct rte_eth_dev *dev)
303 struct priv *priv = dev->data->dev_private;
309 DEBUG("%p: closing device \"%s\"",
311 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
312 mlx4_mac_addr_del(priv);
314 * Prevent crashes when queues are still in use. This is unfortunately
315 * still required for DPDK 1.3 because some programs (such as testpmd)
316 * never release them before closing the device.
318 dev->rx_pkt_burst = mlx4_rx_burst_removed;
319 dev->tx_pkt_burst = mlx4_tx_burst_removed;
320 if (priv->rxqs != NULL) {
321 /* XXX race condition if mlx4_rx_burst() is still running. */
323 for (i = 0; (i != priv->rxqs_n); ++i) {
324 tmp = (*priv->rxqs)[i];
327 (*priv->rxqs)[i] = NULL;
328 mlx4_rxq_cleanup(tmp);
334 if (priv->txqs != NULL) {
335 /* XXX race condition if mlx4_tx_burst() is still running. */
337 for (i = 0; (i != priv->txqs_n); ++i) {
338 tmp = (*priv->txqs)[i];
341 (*priv->txqs)[i] = NULL;
342 mlx4_txq_cleanup(tmp);
348 if (priv->pd != NULL) {
349 assert(priv->ctx != NULL);
350 claim_zero(ibv_dealloc_pd(priv->pd));
351 claim_zero(ibv_close_device(priv->ctx));
353 assert(priv->ctx == NULL);
354 mlx4_intr_uninstall(priv);
355 memset(priv, 0, sizeof(*priv));
358 static const struct eth_dev_ops mlx4_dev_ops = {
359 .dev_configure = mlx4_dev_configure,
360 .dev_start = mlx4_dev_start,
361 .dev_stop = mlx4_dev_stop,
362 .dev_set_link_down = mlx4_dev_set_link_down,
363 .dev_set_link_up = mlx4_dev_set_link_up,
364 .dev_close = mlx4_dev_close,
365 .link_update = mlx4_link_update,
366 .stats_get = mlx4_stats_get,
367 .stats_reset = mlx4_stats_reset,
368 .dev_infos_get = mlx4_dev_infos_get,
369 .rx_queue_setup = mlx4_rx_queue_setup,
370 .tx_queue_setup = mlx4_tx_queue_setup,
371 .rx_queue_release = mlx4_rx_queue_release,
372 .tx_queue_release = mlx4_tx_queue_release,
373 .flow_ctrl_get = mlx4_flow_ctrl_get,
374 .flow_ctrl_set = mlx4_flow_ctrl_set,
375 .mtu_set = mlx4_mtu_set,
376 .filter_ctrl = mlx4_filter_ctrl,
377 .rx_queue_intr_enable = mlx4_rx_intr_enable,
378 .rx_queue_intr_disable = mlx4_rx_intr_disable,
382 * Get PCI information from struct ibv_device.
385 * Pointer to Ethernet device structure.
386 * @param[out] pci_addr
387 * PCI bus address output buffer.
390 * 0 on success, negative errno value otherwise and rte_errno is set.
393 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
394 struct rte_pci_addr *pci_addr)
398 MKSTR(path, "%s/device/uevent", device->ibdev_path);
400 file = fopen(path, "rb");
405 while (fgets(line, sizeof(line), file) == line) {
406 size_t len = strlen(line);
409 /* Truncate long lines. */
410 if (len == (sizeof(line) - 1))
411 while (line[(len - 1)] != '\n') {
415 line[(len - 1)] = ret;
417 /* Extract information. */
420 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
424 &pci_addr->function) == 4) {
434 * Verify and store value for device argument.
437 * Key argument to verify.
439 * Value associated with key.
440 * @param[in, out] conf
441 * Shared configuration data.
444 * 0 on success, negative errno value otherwise and rte_errno is set.
447 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
452 tmp = strtoul(val, NULL, 0);
455 WARN("%s: \"%s\" is not a valid integer", key, val);
458 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
459 uint32_t ports = rte_log2_u32(conf->ports.present);
462 ERROR("port index %lu outside range [0,%" PRIu32 ")",
466 if (!(conf->ports.present & (1 << tmp))) {
468 ERROR("invalid port index %lu", tmp);
471 conf->ports.enabled |= 1 << tmp;
474 WARN("%s: unknown parameter", key);
481 * Parse device parameters.
484 * Device arguments structure.
487 * 0 on success, negative errno value otherwise and rte_errno is set.
490 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
492 struct rte_kvargs *kvlist;
493 unsigned int arg_count;
499 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
500 if (kvlist == NULL) {
502 ERROR("failed to parse kvargs");
505 /* Process parameters. */
506 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
507 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
508 while (arg_count-- > 0) {
509 ret = rte_kvargs_process(kvlist,
511 (int (*)(const char *,
521 rte_kvargs_free(kvlist);
525 static struct rte_pci_driver mlx4_driver;
528 * DPDK callback to register a PCI device.
530 * This function creates an Ethernet device for each port of a given
534 * PCI driver structure (mlx4_driver).
536 * PCI device information.
539 * 0 on success, negative errno value otherwise and rte_errno is set.
542 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
544 struct ibv_device **list;
545 struct ibv_device *ibv_dev;
547 struct ibv_context *attr_ctx = NULL;
548 struct ibv_device_attr device_attr;
549 struct mlx4_conf conf = {
556 assert(pci_drv == &mlx4_driver);
557 list = ibv_get_device_list(&i);
561 if (rte_errno == ENOSYS)
562 ERROR("cannot list devices, is ib_uverbs loaded?");
567 * For each listed device, check related sysfs entry against
568 * the provided PCI ID.
571 struct rte_pci_addr pci_addr;
574 DEBUG("checking device \"%s\"", list[i]->name);
575 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
577 if ((pci_dev->addr.domain != pci_addr.domain) ||
578 (pci_dev->addr.bus != pci_addr.bus) ||
579 (pci_dev->addr.devid != pci_addr.devid) ||
580 (pci_dev->addr.function != pci_addr.function))
582 vf = (pci_dev->id.device_id ==
583 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
584 INFO("PCI information matches, using device \"%s\" (VF: %s)",
585 list[i]->name, (vf ? "true" : "false"));
586 attr_ctx = ibv_open_device(list[i]);
590 if (attr_ctx == NULL) {
591 ibv_free_device_list(list);
595 ERROR("cannot access device, is mlx4_ib loaded?");
599 ERROR("cannot use device, are drivers up to date?");
607 DEBUG("device opened");
608 if (ibv_query_device(attr_ctx, &device_attr)) {
612 INFO("%u port(s) detected", device_attr.phys_port_cnt);
613 conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
614 if (mlx4_args(pci_dev->device.devargs, &conf)) {
615 ERROR("failed to process device arguments");
619 /* Use all ports when none are defined */
620 if (!conf.ports.enabled)
621 conf.ports.enabled = conf.ports.present;
622 for (i = 0; i < device_attr.phys_port_cnt; i++) {
623 uint32_t port = i + 1; /* ports are indexed from one */
624 struct ibv_context *ctx = NULL;
625 struct ibv_port_attr port_attr;
626 struct ibv_pd *pd = NULL;
627 struct priv *priv = NULL;
628 struct rte_eth_dev *eth_dev = NULL;
629 struct ether_addr mac;
631 /* If port is not enabled, skip. */
632 if (!(conf.ports.enabled & (1 << i)))
634 DEBUG("using port %u", port);
635 ctx = ibv_open_device(ibv_dev);
640 /* Check port status. */
641 err = ibv_query_port(ctx, port, &port_attr);
644 ERROR("port query failed: %s", strerror(rte_errno));
647 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
649 ERROR("port %d is not configured in Ethernet mode",
653 if (port_attr.state != IBV_PORT_ACTIVE)
654 DEBUG("port %d is not active: \"%s\" (%d)",
655 port, ibv_port_state_str(port_attr.state),
657 /* Make asynchronous FD non-blocking to handle interrupts. */
658 if (mlx4_fd_set_non_blocking(ctx->async_fd) < 0) {
659 ERROR("cannot make asynchronous FD non-blocking: %s",
660 strerror(rte_errno));
663 /* Allocate protection domain. */
664 pd = ibv_alloc_pd(ctx);
667 ERROR("PD allocation failure");
670 /* from rte_ethdev.c */
671 priv = rte_zmalloc("ethdev private structure",
673 RTE_CACHE_LINE_SIZE);
676 ERROR("priv allocation failure");
680 priv->device_attr = device_attr;
683 priv->mtu = ETHER_MTU;
685 /* Configure the first MAC address by default. */
686 if (mlx4_get_mac(priv, &mac.addr_bytes)) {
687 ERROR("cannot get MAC address, is mlx4_en loaded?"
688 " (rte_errno: %s)", strerror(rte_errno));
691 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
693 mac.addr_bytes[0], mac.addr_bytes[1],
694 mac.addr_bytes[2], mac.addr_bytes[3],
695 mac.addr_bytes[4], mac.addr_bytes[5]);
696 /* Register MAC address. */
698 if (mlx4_mac_addr_add(priv))
702 char ifname[IF_NAMESIZE];
704 if (mlx4_get_ifname(priv, &ifname) == 0)
705 DEBUG("port %u ifname is \"%s\"",
708 DEBUG("port %u ifname is unknown", priv->port);
711 /* Get actual MTU if possible. */
712 mlx4_mtu_get(priv, &priv->mtu);
713 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
714 /* from rte_ethdev.c */
716 char name[RTE_ETH_NAME_MAX_LEN];
718 snprintf(name, sizeof(name), "%s port %u",
719 ibv_get_device_name(ibv_dev), port);
720 eth_dev = rte_eth_dev_allocate(name);
722 if (eth_dev == NULL) {
723 ERROR("can not allocate rte ethdev");
727 eth_dev->data->dev_private = priv;
728 eth_dev->data->mac_addrs = &priv->mac;
729 eth_dev->device = &pci_dev->device;
730 rte_eth_copy_pci_info(eth_dev, pci_dev);
731 eth_dev->device->driver = &mlx4_driver.driver;
732 /* Initialize local interrupt handle for current port. */
733 priv->intr_handle = (struct rte_intr_handle){
735 .type = RTE_INTR_HANDLE_EXT,
738 * Override ethdev interrupt handle pointer with private
739 * handle instead of that of the parent PCI device used by
740 * default. This prevents it from being shared between all
741 * ports of the same PCI device since each of them is
742 * associated its own Verbs context.
744 * Rx interrupts in particular require this as the PMD has
745 * no control over the registration of queue interrupts
746 * besides setting up eth_dev->intr_handle, the rest is
747 * handled by rte_intr_rx_ctl().
749 eth_dev->intr_handle = &priv->intr_handle;
751 eth_dev->dev_ops = &mlx4_dev_ops;
752 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
753 /* Bring Ethernet device up. */
754 DEBUG("forcing Ethernet interface up");
755 mlx4_dev_set_link_up(priv->dev);
756 /* Update link status once if waiting for LSC. */
757 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
758 mlx4_link_update(eth_dev, 0);
763 claim_zero(ibv_dealloc_pd(pd));
765 claim_zero(ibv_close_device(ctx));
767 rte_eth_dev_release_port(eth_dev);
770 if (i == device_attr.phys_port_cnt)
773 * XXX if something went wrong in the loop above, there is a resource
774 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
775 * long as the dpdk does not provide a way to deallocate a ethdev and a
776 * way to enumerate the registered ethdevs to free the previous ones.
780 claim_zero(ibv_close_device(attr_ctx));
782 ibv_free_device_list(list);
783 assert(rte_errno >= 0);
787 static const struct rte_pci_id mlx4_pci_id_map[] = {
789 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
790 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
793 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
794 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
797 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
798 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
805 static struct rte_pci_driver mlx4_driver = {
807 .name = MLX4_DRIVER_NAME
809 .id_table = mlx4_pci_id_map,
810 .probe = mlx4_pci_probe,
811 .drv_flags = RTE_PCI_DRV_INTR_LSC |
812 RTE_PCI_DRV_INTR_RMV,
816 * Driver initialization routine.
818 RTE_INIT(rte_mlx4_pmd_init);
820 rte_mlx4_pmd_init(void)
823 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
824 * huge pages. Calling ibv_fork_init() during init allows
825 * applications to use fork() safely for purposes other than
826 * using this PMD, which is not supported in forked processes.
828 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
830 rte_pci_register(&mlx4_driver);
833 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
834 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
835 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
836 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");