4 * Copyright 2015 6WIND S.A.
5 * Copyright 2015 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.
44 #include <sys/ioctl.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
48 #include <linux/ethtool.h>
49 #include <linux/sockios.h>
52 /* DPDK headers don't like -pedantic. */
54 #pragma GCC diagnostic ignored "-pedantic"
56 #include <rte_atomic.h>
57 #include <rte_ethdev.h>
59 #include <rte_common.h>
60 #include <rte_interrupts.h>
61 #include <rte_alarm.h>
63 #pragma GCC diagnostic error "-pedantic"
67 #include "mlx5_rxtx.h"
68 #include "mlx5_utils.h"
71 * Get interface name from private structure.
74 * Pointer to private structure.
76 * Interface name output buffer.
79 * 0 on success, -1 on failure and errno is set.
82 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
86 unsigned int dev_type = 0;
87 unsigned int dev_port_prev = ~0u;
88 char match[IF_NAMESIZE] = "";
91 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
97 while ((dent = readdir(dir)) != NULL) {
98 char *name = dent->d_name;
100 unsigned int dev_port;
103 if ((name[0] == '.') &&
104 ((name[1] == '\0') ||
105 ((name[1] == '.') && (name[2] == '\0'))))
108 MKSTR(path, "%s/device/net/%s/%s",
109 priv->ctx->device->ibdev_path, name,
110 (dev_type ? "dev_id" : "dev_port"));
112 file = fopen(path, "rb");
117 * Switch to dev_id when dev_port does not exist as
118 * is the case with Linux kernel versions < 3.15.
129 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
134 * Switch to dev_id when dev_port returns the same value for
135 * all ports. May happen when using a MOFED release older than
136 * 3.0 with a Linux kernel >= 3.15.
138 if (dev_port == dev_port_prev)
140 dev_port_prev = dev_port;
141 if (dev_port == (priv->port - 1u))
142 snprintf(match, sizeof(match), "%s", name);
145 if (match[0] == '\0')
147 strncpy(*ifname, match, sizeof(*ifname));
152 * Read from sysfs entry.
155 * Pointer to private structure.
157 * Entry name relative to sysfs path.
159 * Data output buffer.
164 * 0 on success, -1 on failure and errno is set.
167 priv_sysfs_read(const struct priv *priv, const char *entry,
168 char *buf, size_t size)
170 char ifname[IF_NAMESIZE];
175 if (priv_get_ifname(priv, &ifname))
178 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
181 file = fopen(path, "rb");
184 ret = fread(buf, 1, size, file);
186 if (((size_t)ret < size) && (ferror(file)))
196 * Write to sysfs entry.
199 * Pointer to private structure.
201 * Entry name relative to sysfs path.
208 * 0 on success, -1 on failure and errno is set.
211 priv_sysfs_write(const struct priv *priv, const char *entry,
212 char *buf, size_t size)
214 char ifname[IF_NAMESIZE];
219 if (priv_get_ifname(priv, &ifname))
222 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
225 file = fopen(path, "wb");
228 ret = fwrite(buf, 1, size, file);
230 if (((size_t)ret < size) || (ferror(file)))
240 * Get unsigned long sysfs property.
243 * Pointer to private structure.
245 * Entry name relative to sysfs path.
247 * Value output buffer.
250 * 0 on success, -1 on failure and errno is set.
253 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
256 unsigned long value_ret;
259 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
261 DEBUG("cannot read %s value from sysfs: %s",
262 name, strerror(errno));
265 value_str[ret] = '\0';
267 value_ret = strtoul(value_str, NULL, 0);
269 DEBUG("invalid %s value `%s': %s", name, value_str,
278 * Set unsigned long sysfs property.
281 * Pointer to private structure.
283 * Entry name relative to sysfs path.
288 * 0 on success, -1 on failure and errno is set.
291 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
294 MKSTR(value_str, "%lu", value);
296 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
298 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
299 name, value_str, value, strerror(errno));
306 * Perform ifreq ioctl() on associated Ethernet device.
309 * Pointer to private structure.
311 * Request number to pass to ioctl().
313 * Interface request structure output buffer.
316 * 0 on success, -1 on failure and errno is set.
319 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
321 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
326 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
327 ret = ioctl(sock, req, ifr);
336 * Pointer to private structure.
338 * MTU value output buffer.
341 * 0 on success, -1 on failure and errno is set.
344 priv_get_mtu(struct priv *priv, uint16_t *mtu)
346 unsigned long ulong_mtu;
348 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
358 * Pointer to private structure.
363 * 0 on success, -1 on failure and errno is set.
366 priv_set_mtu(struct priv *priv, uint16_t mtu)
368 return priv_set_sysfs_ulong(priv, "mtu", mtu);
375 * Pointer to private structure.
377 * Bitmask for flags that must remain untouched.
379 * Bitmask for flags to modify.
382 * 0 on success, -1 on failure and errno is set.
385 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
389 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
393 return priv_set_sysfs_ulong(priv, "flags", tmp);
397 * Ethernet device configuration.
399 * Prepare the driver for a given number of TX and RX queues.
402 * Pointer to Ethernet device structure.
405 * 0 on success, errno value on failure.
408 dev_configure(struct rte_eth_dev *dev)
410 struct priv *priv = dev->data->dev_private;
411 unsigned int rxqs_n = dev->data->nb_rx_queues;
412 unsigned int txqs_n = dev->data->nb_tx_queues;
415 unsigned int reta_idx_n;
417 priv->rxqs = (void *)dev->data->rx_queues;
418 priv->txqs = (void *)dev->data->tx_queues;
419 if (txqs_n != priv->txqs_n) {
420 INFO("%p: TX queues number update: %u -> %u",
421 (void *)dev, priv->txqs_n, txqs_n);
422 priv->txqs_n = txqs_n;
424 if (rxqs_n > priv->ind_table_max_size) {
425 ERROR("cannot handle this many RX queues (%u)", rxqs_n);
428 if (rxqs_n == priv->rxqs_n)
430 INFO("%p: RX queues number update: %u -> %u",
431 (void *)dev, priv->rxqs_n, rxqs_n);
432 priv->rxqs_n = rxqs_n;
433 /* If the requested number of RX queues is not a power of two, use the
434 * maximum indirection table size for better balancing.
435 * The result is always rounded to the next power of two. */
436 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
437 priv->ind_table_max_size :
439 if (priv_rss_reta_index_resize(priv, reta_idx_n))
441 /* When the number of RX queues is not a power of two, the remaining
442 * table entries are padded with reused WQs and hashes are not spread
444 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
445 (*priv->reta_idx)[i] = j;
453 * DPDK callback for Ethernet device configuration.
456 * Pointer to Ethernet device structure.
459 * 0 on success, negative errno value on failure.
462 mlx5_dev_configure(struct rte_eth_dev *dev)
464 struct priv *priv = dev->data->dev_private;
468 ret = dev_configure(dev);
475 * DPDK callback to get information about the device.
478 * Pointer to Ethernet device structure.
480 * Info structure output buffer.
483 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
485 struct priv *priv = dev->data->dev_private;
487 char ifname[IF_NAMESIZE];
490 /* FIXME: we should ask the device for these values. */
491 info->min_rx_bufsize = 32;
492 info->max_rx_pktlen = 65536;
494 * Since we need one CQ per QP, the limit is the minimum number
495 * between the two values.
497 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
498 priv->device_attr.max_qp : priv->device_attr.max_cq);
499 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
502 info->max_rx_queues = max;
503 info->max_tx_queues = max;
504 /* Last array entry is reserved for broadcast. */
505 info->max_mac_addrs = (RTE_DIM(priv->mac) - 1);
506 info->rx_offload_capa =
508 (DEV_RX_OFFLOAD_IPV4_CKSUM |
509 DEV_RX_OFFLOAD_UDP_CKSUM |
510 DEV_RX_OFFLOAD_TCP_CKSUM) :
512 info->tx_offload_capa =
514 (DEV_TX_OFFLOAD_IPV4_CKSUM |
515 DEV_TX_OFFLOAD_UDP_CKSUM |
516 DEV_TX_OFFLOAD_TCP_CKSUM) :
518 if (priv_get_ifname(priv, &ifname) == 0)
519 info->if_index = if_nametoindex(ifname);
520 /* FIXME: RETA update/query API expects the callee to know the size of
521 * the indirection table, for this PMD the size varies depending on
522 * the number of RX queues, it becomes impossible to find the correct
523 * size if it is not fixed.
524 * The API should be updated to solve this problem. */
525 info->reta_size = priv->ind_table_max_size;
530 * DPDK callback to retrieve physical link information (unlocked version).
533 * Pointer to Ethernet device structure.
534 * @param wait_to_complete
535 * Wait for request completion (ignored).
538 mlx5_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
540 struct priv *priv = dev->data->dev_private;
541 struct ethtool_cmd edata = {
545 struct rte_eth_link dev_link;
548 (void)wait_to_complete;
549 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
550 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
553 memset(&dev_link, 0, sizeof(dev_link));
554 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
555 (ifr.ifr_flags & IFF_RUNNING));
556 ifr.ifr_data = &edata;
557 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
558 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
562 link_speed = ethtool_cmd_speed(&edata);
563 if (link_speed == -1)
564 dev_link.link_speed = 0;
566 dev_link.link_speed = link_speed;
567 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
568 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
569 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
570 /* Link status changed. */
571 dev->data->dev_link = dev_link;
574 /* Link status is still the same. */
579 * DPDK callback to retrieve physical link information.
582 * Pointer to Ethernet device structure.
583 * @param wait_to_complete
584 * Wait for request completion (ignored).
587 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
589 struct priv *priv = dev->data->dev_private;
593 ret = mlx5_link_update_unlocked(dev, wait_to_complete);
599 * DPDK callback to change the MTU.
601 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
602 * received). Use this as a hint to enable/disable scattered packets support
603 * and improve performance when not needed.
604 * Since failure is not an option, reconfiguring queues on the fly is not
608 * Pointer to Ethernet device structure.
613 * 0 on success, negative errno value on failure.
616 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
618 struct priv *priv = dev->data->dev_private;
621 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
625 /* Set kernel interface MTU first. */
626 if (priv_set_mtu(priv, mtu)) {
628 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
632 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
634 /* Temporarily replace RX handler with a fake one, assuming it has not
635 * been copied elsewhere. */
636 dev->rx_pkt_burst = removed_rx_burst;
637 /* Make sure everyone has left mlx5_rx_burst() and uses
638 * removed_rx_burst() instead. */
641 /* Reconfigure each RX queue. */
642 for (i = 0; (i != priv->rxqs_n); ++i) {
643 struct rxq *rxq = (*priv->rxqs)[i];
644 unsigned int max_frame_len;
649 /* Calculate new maximum frame length according to MTU and
650 * toggle scattered support (sp) if necessary. */
651 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
652 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
653 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
654 /* Provide new values to rxq_setup(). */
655 dev->data->dev_conf.rxmode.jumbo_frame = sp;
656 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
657 ret = rxq_rehash(dev, rxq);
659 /* Force SP RX if that queue requires it and abort. */
661 rx_func = mlx5_rx_burst_sp;
664 /* Scattered burst function takes priority. */
666 rx_func = mlx5_rx_burst_sp;
668 /* Burst functions can now be called again. */
670 dev->rx_pkt_burst = rx_func;
678 * DPDK callback to get flow control status.
681 * Pointer to Ethernet device structure.
682 * @param[out] fc_conf
683 * Flow control output buffer.
686 * 0 on success, negative errno value on failure.
689 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
691 struct priv *priv = dev->data->dev_private;
693 struct ethtool_pauseparam ethpause = {
694 .cmd = ETHTOOL_GPAUSEPARAM
698 ifr.ifr_data = ðpause;
700 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
702 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
708 fc_conf->autoneg = ethpause.autoneg;
709 if (ethpause.rx_pause && ethpause.tx_pause)
710 fc_conf->mode = RTE_FC_FULL;
711 else if (ethpause.rx_pause)
712 fc_conf->mode = RTE_FC_RX_PAUSE;
713 else if (ethpause.tx_pause)
714 fc_conf->mode = RTE_FC_TX_PAUSE;
716 fc_conf->mode = RTE_FC_NONE;
726 * DPDK callback to modify flow control parameters.
729 * Pointer to Ethernet device structure.
731 * Flow control parameters.
734 * 0 on success, negative errno value on failure.
737 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
739 struct priv *priv = dev->data->dev_private;
741 struct ethtool_pauseparam ethpause = {
742 .cmd = ETHTOOL_SPAUSEPARAM
746 ifr.ifr_data = ðpause;
747 ethpause.autoneg = fc_conf->autoneg;
748 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
749 (fc_conf->mode & RTE_FC_RX_PAUSE))
750 ethpause.rx_pause = 1;
752 ethpause.rx_pause = 0;
754 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
755 (fc_conf->mode & RTE_FC_TX_PAUSE))
756 ethpause.tx_pause = 1;
758 ethpause.tx_pause = 0;
761 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
763 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
777 * Get PCI information from struct ibv_device.
780 * Pointer to Ethernet device structure.
781 * @param[out] pci_addr
782 * PCI bus address output buffer.
785 * 0 on success, -1 on failure and errno is set.
788 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
789 struct rte_pci_addr *pci_addr)
793 MKSTR(path, "%s/device/uevent", device->ibdev_path);
795 file = fopen(path, "rb");
798 while (fgets(line, sizeof(line), file) == line) {
799 size_t len = strlen(line);
802 /* Truncate long lines. */
803 if (len == (sizeof(line) - 1))
804 while (line[(len - 1)] != '\n') {
808 line[(len - 1)] = ret;
810 /* Extract information. */
813 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
817 &pci_addr->function) == 4) {
827 * Link status handler.
830 * Pointer to private structure.
832 * Pointer to the rte_eth_dev structure.
835 * Nonzero if the callback process can be called immediately.
838 priv_dev_link_status_handler(struct priv *priv, struct rte_eth_dev *dev)
840 struct ibv_async_event event;
844 /* Read all message and acknowledge them. */
846 if (ibv_get_async_event(priv->ctx, &event))
849 if (event.event_type == IBV_EVENT_PORT_ACTIVE ||
850 event.event_type == IBV_EVENT_PORT_ERR)
853 DEBUG("event type %d on port %d not handled",
854 event.event_type, event.element.port_num);
855 ibv_ack_async_event(&event);
858 if (port_change ^ priv->pending_alarm) {
859 struct rte_eth_link *link = &dev->data->dev_link;
861 priv->pending_alarm = 0;
862 mlx5_link_update_unlocked(dev, 0);
863 if (((link->link_speed == 0) && link->link_status) ||
864 ((link->link_speed != 0) && !link->link_status)) {
865 /* Inconsistent status, check again later. */
866 priv->pending_alarm = 1;
867 rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
868 mlx5_dev_link_status_handler,
877 * Handle delayed link status event.
880 * Registered argument.
883 mlx5_dev_link_status_handler(void *arg)
885 struct rte_eth_dev *dev = arg;
886 struct priv *priv = dev->data->dev_private;
890 assert(priv->pending_alarm == 1);
891 ret = priv_dev_link_status_handler(priv, dev);
894 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC);
898 * Handle interrupts from the NIC.
900 * @param[in] intr_handle
906 mlx5_dev_interrupt_handler(struct rte_intr_handle *intr_handle, void *cb_arg)
908 struct rte_eth_dev *dev = cb_arg;
909 struct priv *priv = dev->data->dev_private;
914 ret = priv_dev_link_status_handler(priv, dev);
917 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC);
921 * Uninstall interrupt handler.
924 * Pointer to private structure.
926 * Pointer to the rte_eth_dev structure.
929 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
931 if (!dev->data->dev_conf.intr_conf.lsc)
933 rte_intr_callback_unregister(&priv->intr_handle,
934 mlx5_dev_interrupt_handler,
936 if (priv->pending_alarm)
937 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
938 priv->pending_alarm = 0;
939 priv->intr_handle.fd = 0;
940 priv->intr_handle.type = 0;
944 * Install interrupt handler.
947 * Pointer to private structure.
949 * Pointer to the rte_eth_dev structure.
952 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
956 if (!dev->data->dev_conf.intr_conf.lsc)
958 assert(priv->ctx->async_fd > 0);
959 flags = fcntl(priv->ctx->async_fd, F_GETFL);
960 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
962 INFO("failed to change file descriptor async event queue");
963 dev->data->dev_conf.intr_conf.lsc = 0;
965 priv->intr_handle.fd = priv->ctx->async_fd;
966 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
967 rte_intr_callback_register(&priv->intr_handle,
968 mlx5_dev_interrupt_handler,