4 * Copyright 2017 6WIND S.A.
5 * Copyright 2017 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 * Miscellaneous control operations for mlx4 driver.
42 #include <linux/ethtool.h>
43 #include <linux/sockios.h>
45 #include <netinet/ip.h>
51 #include <sys/ioctl.h>
52 #include <sys/socket.h>
55 /* Verbs headers do not support -pedantic. */
57 #pragma GCC diagnostic ignored "-Wpedantic"
59 #include <infiniband/verbs.h>
61 #pragma GCC diagnostic error "-Wpedantic"
64 #include <rte_errno.h>
65 #include <rte_ethdev.h>
66 #include <rte_ether.h>
71 #include "mlx4_flow.h"
72 #include "mlx4_rxtx.h"
73 #include "mlx4_utils.h"
76 * Get interface name from private structure.
79 * Pointer to private structure.
81 * Interface name output buffer.
84 * 0 on success, negative errno value otherwise and rte_errno is set.
87 mlx4_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
91 unsigned int dev_type = 0;
92 unsigned int dev_port_prev = ~0u;
93 char match[IF_NAMESIZE] = "";
96 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
104 while ((dent = readdir(dir)) != NULL) {
105 char *name = dent->d_name;
107 unsigned int dev_port;
110 if ((name[0] == '.') &&
111 ((name[1] == '\0') ||
112 ((name[1] == '.') && (name[2] == '\0'))))
115 MKSTR(path, "%s/device/net/%s/%s",
116 priv->ctx->device->ibdev_path, name,
117 (dev_type ? "dev_id" : "dev_port"));
119 file = fopen(path, "rb");
124 * Switch to dev_id when dev_port does not exist as
125 * is the case with Linux kernel versions < 3.15.
136 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
141 * Switch to dev_id when dev_port returns the same value for
142 * all ports. May happen when using a MOFED release older than
143 * 3.0 with a Linux kernel >= 3.15.
145 if (dev_port == dev_port_prev)
147 dev_port_prev = dev_port;
148 if (dev_port == (priv->port - 1u))
149 snprintf(match, sizeof(match), "%s", name);
152 if (match[0] == '\0') {
156 strncpy(*ifname, match, sizeof(*ifname));
161 * Read from sysfs entry.
164 * Pointer to private structure.
166 * Entry name relative to sysfs path.
168 * Data output buffer.
173 * Number of bytes read on success, negative errno value otherwise and
177 mlx4_sysfs_read(const struct priv *priv, const char *entry,
178 char *buf, size_t size)
180 char ifname[IF_NAMESIZE];
184 ret = mlx4_get_ifname(priv, &ifname);
188 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
191 file = fopen(path, "rb");
196 ret = fread(buf, 1, size, file);
197 if ((size_t)ret < size && ferror(file)) {
208 * Write to sysfs entry.
211 * Pointer to private structure.
213 * Entry name relative to sysfs path.
220 * Number of bytes written on success, negative errno value otherwise and
224 mlx4_sysfs_write(const struct priv *priv, const char *entry,
225 char *buf, size_t size)
227 char ifname[IF_NAMESIZE];
231 ret = mlx4_get_ifname(priv, &ifname);
235 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
238 file = fopen(path, "wb");
243 ret = fwrite(buf, 1, size, file);
244 if ((size_t)ret < size || ferror(file)) {
255 * Get unsigned long sysfs property.
258 * Pointer to private structure.
260 * Entry name relative to sysfs path.
262 * Value output buffer.
265 * 0 on success, negative errno value otherwise and rte_errno is set.
268 mlx4_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
271 unsigned long value_ret;
274 ret = mlx4_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
276 DEBUG("cannot read %s value from sysfs: %s",
277 name, strerror(rte_errno));
280 value_str[ret] = '\0';
282 value_ret = strtoul(value_str, NULL, 0);
285 DEBUG("invalid %s value `%s': %s", name, value_str,
286 strerror(rte_errno));
294 * Set unsigned long sysfs property.
297 * Pointer to private structure.
299 * Entry name relative to sysfs path.
304 * 0 on success, negative errno value otherwise and rte_errno is set.
307 mlx4_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
310 MKSTR(value_str, "%lu", value);
312 ret = mlx4_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
314 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
315 name, value_str, value, strerror(rte_errno));
322 * Perform ifreq ioctl() on associated Ethernet device.
325 * Pointer to private structure.
327 * Request number to pass to ioctl().
329 * Interface request structure output buffer.
332 * 0 on success, negative errno value otherwise and rte_errno is set.
335 mlx4_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
337 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
344 ret = mlx4_get_ifname(priv, &ifr->ifr_name);
345 if (!ret && ioctl(sock, req, ifr) == -1) {
354 * Get MAC address by querying netdevice.
357 * Pointer to private structure.
359 * MAC address output buffer.
362 * 0 on success, negative errno value otherwise and rte_errno is set.
365 mlx4_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
367 struct ifreq request;
368 int ret = mlx4_ifreq(priv, SIOCGIFHWADDR, &request);
372 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
380 * Pointer to private structure.
382 * MTU value output buffer.
385 * 0 on success, negative errno value otherwise and rte_errno is set.
388 mlx4_mtu_get(struct priv *priv, uint16_t *mtu)
390 unsigned long ulong_mtu = 0;
391 int ret = mlx4_get_sysfs_ulong(priv, "mtu", &ulong_mtu);
400 * DPDK callback to change the MTU.
403 * Pointer to Ethernet device structure.
408 * 0 on success, negative errno value otherwise and rte_errno is set.
411 mlx4_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
413 struct priv *priv = dev->data->dev_private;
415 int ret = mlx4_set_sysfs_ulong(priv, "mtu", mtu);
419 ret = mlx4_mtu_get(priv, &new_mtu);
422 if (new_mtu == mtu) {
434 * Pointer to private structure.
436 * Bitmask for flags that must remain untouched.
438 * Bitmask for flags to modify.
441 * 0 on success, negative errno value otherwise and rte_errno is set.
444 mlx4_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
446 unsigned long tmp = 0;
447 int ret = mlx4_get_sysfs_ulong(priv, "flags", &tmp);
452 tmp |= (flags & (~keep));
453 return mlx4_set_sysfs_ulong(priv, "flags", tmp);
457 * Change the link state (UP / DOWN).
460 * Pointer to Ethernet device private data.
462 * Nonzero for link up, otherwise link down.
465 * 0 on success, negative errno value otherwise and rte_errno is set.
468 mlx4_dev_set_link(struct priv *priv, int up)
470 struct rte_eth_dev *dev = priv->dev;
474 err = mlx4_set_flags(priv, ~IFF_UP, IFF_UP);
477 dev->rx_pkt_burst = mlx4_rx_burst;
479 err = mlx4_set_flags(priv, ~IFF_UP, ~IFF_UP);
482 dev->rx_pkt_burst = mlx4_rx_burst_removed;
483 dev->tx_pkt_burst = mlx4_tx_burst_removed;
489 * DPDK callback to bring the link DOWN.
492 * Pointer to Ethernet device structure.
495 * 0 on success, negative errno value otherwise and rte_errno is set.
498 mlx4_dev_set_link_down(struct rte_eth_dev *dev)
500 struct priv *priv = dev->data->dev_private;
502 return mlx4_dev_set_link(priv, 0);
506 * DPDK callback to bring the link UP.
509 * Pointer to Ethernet device structure.
512 * 0 on success, negative errno value otherwise and rte_errno is set.
515 mlx4_dev_set_link_up(struct rte_eth_dev *dev)
517 struct priv *priv = dev->data->dev_private;
519 return mlx4_dev_set_link(priv, 1);
523 * Supported Rx mode toggles.
525 * Even and odd values respectively stand for off and on.
528 RXMODE_TOGGLE_PROMISC_OFF,
529 RXMODE_TOGGLE_PROMISC_ON,
530 RXMODE_TOGGLE_ALLMULTI_OFF,
531 RXMODE_TOGGLE_ALLMULTI_ON,
535 * Helper function to toggle promiscuous and all multicast modes.
538 * Pointer to Ethernet device structure.
543 mlx4_rxmode_toggle(struct rte_eth_dev *dev, enum rxmode_toggle toggle)
545 struct priv *priv = dev->data->dev_private;
547 struct rte_flow_error error;
550 case RXMODE_TOGGLE_PROMISC_OFF:
551 case RXMODE_TOGGLE_PROMISC_ON:
552 mode = "promiscuous";
553 dev->data->promiscuous = toggle & 1;
555 case RXMODE_TOGGLE_ALLMULTI_OFF:
556 case RXMODE_TOGGLE_ALLMULTI_ON:
557 mode = "all multicast";
558 dev->data->all_multicast = toggle & 1;
561 if (!mlx4_flow_sync(priv, &error))
563 ERROR("cannot toggle %s mode (code %d, \"%s\"),"
564 " flow error type %d, cause %p, message: %s",
565 mode, rte_errno, strerror(rte_errno), error.type, error.cause,
566 error.message ? error.message : "(unspecified)");
570 * DPDK callback to enable promiscuous mode.
573 * Pointer to Ethernet device structure.
576 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
578 mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_PROMISC_ON);
582 * DPDK callback to disable promiscuous mode.
585 * Pointer to Ethernet device structure.
588 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
590 mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_PROMISC_OFF);
594 * DPDK callback to enable all multicast mode.
597 * Pointer to Ethernet device structure.
600 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
602 mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_ALLMULTI_ON);
606 * DPDK callback to disable all multicast mode.
609 * Pointer to Ethernet device structure.
612 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
614 mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_ALLMULTI_OFF);
618 * DPDK callback to remove a MAC address.
621 * Pointer to Ethernet device structure.
626 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
628 struct priv *priv = dev->data->dev_private;
629 struct rte_flow_error error;
631 if (index >= RTE_DIM(priv->mac)) {
635 memset(&priv->mac[index], 0, sizeof(priv->mac[index]));
636 if (!mlx4_flow_sync(priv, &error))
638 ERROR("failed to synchronize flow rules after removing MAC address"
639 " at index %d (code %d, \"%s\"),"
640 " flow error type %d, cause %p, message: %s",
641 index, rte_errno, strerror(rte_errno), error.type, error.cause,
642 error.message ? error.message : "(unspecified)");
646 * DPDK callback to add a MAC address.
649 * Pointer to Ethernet device structure.
651 * MAC address to register.
655 * VMDq pool index to associate address with (ignored).
658 * 0 on success, negative errno value otherwise and rte_errno is set.
661 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
662 uint32_t index, uint32_t vmdq)
664 struct priv *priv = dev->data->dev_private;
665 struct rte_flow_error error;
669 if (index >= RTE_DIM(priv->mac)) {
673 memcpy(&priv->mac[index], mac_addr, sizeof(priv->mac[index]));
674 ret = mlx4_flow_sync(priv, &error);
677 ERROR("failed to synchronize flow rules after adding MAC address"
678 " at index %d (code %d, \"%s\"),"
679 " flow error type %d, cause %p, message: %s",
680 index, rte_errno, strerror(rte_errno), error.type, error.cause,
681 error.message ? error.message : "(unspecified)");
686 * DPDK callback to configure a VLAN filter.
689 * Pointer to Ethernet device structure.
696 * 0 on success, negative errno value otherwise and rte_errno is set.
699 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
701 struct priv *priv = dev->data->dev_private;
702 struct rte_flow_error error;
703 unsigned int vidx = vlan_id / 64;
704 unsigned int vbit = vlan_id % 64;
708 if (vidx >= RTE_DIM(dev->data->vlan_filter_conf.ids)) {
712 v = &dev->data->vlan_filter_conf.ids[vidx];
713 *v &= ~(UINT64_C(1) << vbit);
714 *v |= (uint64_t)!!on << vbit;
715 ret = mlx4_flow_sync(priv, &error);
718 ERROR("failed to synchronize flow rules after %s VLAN filter on ID %u"
719 " (code %d, \"%s\"), "
720 " flow error type %d, cause %p, message: %s",
721 on ? "enabling" : "disabling", vlan_id,
722 rte_errno, strerror(rte_errno), error.type, error.cause,
723 error.message ? error.message : "(unspecified)");
728 * DPDK callback to set the primary MAC address.
731 * Pointer to Ethernet device structure.
733 * MAC address to register.
736 mlx4_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
738 mlx4_mac_addr_add(dev, mac_addr, 0, 0);
742 * DPDK callback to get information about the device.
745 * Pointer to Ethernet device structure.
747 * Info structure output buffer.
750 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
752 struct priv *priv = dev->data->dev_private;
754 char ifname[IF_NAMESIZE];
756 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
759 /* FIXME: we should ask the device for these values. */
760 info->min_rx_bufsize = 32;
761 info->max_rx_pktlen = 65536;
763 * Since we need one CQ per QP, the limit is the minimum number
764 * between the two values.
766 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
767 priv->device_attr.max_qp : priv->device_attr.max_cq);
768 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
771 info->max_rx_queues = max;
772 info->max_tx_queues = max;
773 info->max_mac_addrs = RTE_DIM(priv->mac);
774 info->rx_offload_capa = 0;
775 info->tx_offload_capa = 0;
776 if (mlx4_get_ifname(priv, &ifname) == 0)
777 info->if_index = if_nametoindex(ifname);
787 * DPDK callback to get device statistics.
790 * Pointer to Ethernet device structure.
792 * Stats structure output buffer.
795 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
797 struct rte_eth_stats tmp;
801 memset(&tmp, 0, sizeof(tmp));
802 /* Add software counters. */
803 for (i = 0; i != dev->data->nb_rx_queues; ++i) {
804 struct rxq *rxq = dev->data->rx_queues[i];
808 idx = rxq->stats.idx;
809 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
810 tmp.q_ipackets[idx] += rxq->stats.ipackets;
811 tmp.q_ibytes[idx] += rxq->stats.ibytes;
812 tmp.q_errors[idx] += (rxq->stats.idropped +
813 rxq->stats.rx_nombuf);
815 tmp.ipackets += rxq->stats.ipackets;
816 tmp.ibytes += rxq->stats.ibytes;
817 tmp.ierrors += rxq->stats.idropped;
818 tmp.rx_nombuf += rxq->stats.rx_nombuf;
820 for (i = 0; i != dev->data->nb_tx_queues; ++i) {
821 struct txq *txq = dev->data->tx_queues[i];
825 idx = txq->stats.idx;
826 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
827 tmp.q_opackets[idx] += txq->stats.opackets;
828 tmp.q_obytes[idx] += txq->stats.obytes;
829 tmp.q_errors[idx] += txq->stats.odropped;
831 tmp.opackets += txq->stats.opackets;
832 tmp.obytes += txq->stats.obytes;
833 tmp.oerrors += txq->stats.odropped;
840 * DPDK callback to clear device statistics.
843 * Pointer to Ethernet device structure.
846 mlx4_stats_reset(struct rte_eth_dev *dev)
850 for (i = 0; i != dev->data->nb_rx_queues; ++i) {
851 struct rxq *rxq = dev->data->rx_queues[i];
854 rxq->stats = (struct mlx4_rxq_stats){
855 .idx = rxq->stats.idx,
858 for (i = 0; i != dev->data->nb_tx_queues; ++i) {
859 struct txq *txq = dev->data->tx_queues[i];
862 txq->stats = (struct mlx4_txq_stats){
863 .idx = txq->stats.idx,
869 * DPDK callback to retrieve physical link information.
872 * Pointer to Ethernet device structure.
873 * @param wait_to_complete
874 * Wait for request completion (ignored).
877 * 0 on success, negative errno value otherwise and rte_errno is set.
880 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
882 const struct priv *priv = dev->data->dev_private;
883 struct ethtool_cmd edata = {
887 struct rte_eth_link dev_link;
894 (void)wait_to_complete;
895 if (mlx4_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
896 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(rte_errno));
899 memset(&dev_link, 0, sizeof(dev_link));
900 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
901 (ifr.ifr_flags & IFF_RUNNING));
902 ifr.ifr_data = (void *)&edata;
903 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
904 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
905 strerror(rte_errno));
908 link_speed = ethtool_cmd_speed(&edata);
909 if (link_speed == -1)
910 dev_link.link_speed = 0;
912 dev_link.link_speed = link_speed;
913 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
914 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
915 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
916 ETH_LINK_SPEED_FIXED);
917 dev->data->dev_link = dev_link;
922 * DPDK callback to get flow control status.
925 * Pointer to Ethernet device structure.
926 * @param[out] fc_conf
927 * Flow control output buffer.
930 * 0 on success, negative errno value otherwise and rte_errno is set.
933 mlx4_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
935 struct priv *priv = dev->data->dev_private;
937 struct ethtool_pauseparam ethpause = {
938 .cmd = ETHTOOL_GPAUSEPARAM,
942 ifr.ifr_data = (void *)ðpause;
943 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
945 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
947 strerror(rte_errno));
950 fc_conf->autoneg = ethpause.autoneg;
951 if (ethpause.rx_pause && ethpause.tx_pause)
952 fc_conf->mode = RTE_FC_FULL;
953 else if (ethpause.rx_pause)
954 fc_conf->mode = RTE_FC_RX_PAUSE;
955 else if (ethpause.tx_pause)
956 fc_conf->mode = RTE_FC_TX_PAUSE;
958 fc_conf->mode = RTE_FC_NONE;
966 * DPDK callback to modify flow control parameters.
969 * Pointer to Ethernet device structure.
971 * Flow control parameters.
974 * 0 on success, negative errno value otherwise and rte_errno is set.
977 mlx4_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
979 struct priv *priv = dev->data->dev_private;
981 struct ethtool_pauseparam ethpause = {
982 .cmd = ETHTOOL_SPAUSEPARAM,
986 ifr.ifr_data = (void *)ðpause;
987 ethpause.autoneg = fc_conf->autoneg;
988 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
989 (fc_conf->mode & RTE_FC_RX_PAUSE))
990 ethpause.rx_pause = 1;
992 ethpause.rx_pause = 0;
993 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
994 (fc_conf->mode & RTE_FC_TX_PAUSE))
995 ethpause.tx_pause = 1;
997 ethpause.tx_pause = 0;
998 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
1000 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1002 strerror(rte_errno));