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>
70 #include "mlx4_rxtx.h"
71 #include "mlx4_utils.h"
74 * Get interface name from private structure.
77 * Pointer to private structure.
79 * Interface name output buffer.
82 * 0 on success, negative errno value otherwise and rte_errno is set.
85 mlx4_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
89 unsigned int dev_type = 0;
90 unsigned int dev_port_prev = ~0u;
91 char match[IF_NAMESIZE] = "";
94 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
102 while ((dent = readdir(dir)) != NULL) {
103 char *name = dent->d_name;
105 unsigned int dev_port;
108 if ((name[0] == '.') &&
109 ((name[1] == '\0') ||
110 ((name[1] == '.') && (name[2] == '\0'))))
113 MKSTR(path, "%s/device/net/%s/%s",
114 priv->ctx->device->ibdev_path, name,
115 (dev_type ? "dev_id" : "dev_port"));
117 file = fopen(path, "rb");
122 * Switch to dev_id when dev_port does not exist as
123 * is the case with Linux kernel versions < 3.15.
134 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
139 * Switch to dev_id when dev_port returns the same value for
140 * all ports. May happen when using a MOFED release older than
141 * 3.0 with a Linux kernel >= 3.15.
143 if (dev_port == dev_port_prev)
145 dev_port_prev = dev_port;
146 if (dev_port == (priv->port - 1u))
147 snprintf(match, sizeof(match), "%s", name);
150 if (match[0] == '\0') {
154 strncpy(*ifname, match, sizeof(*ifname));
159 * Read from sysfs entry.
162 * Pointer to private structure.
164 * Entry name relative to sysfs path.
166 * Data output buffer.
171 * Number of bytes read on success, negative errno value otherwise and
175 mlx4_sysfs_read(const struct priv *priv, const char *entry,
176 char *buf, size_t size)
178 char ifname[IF_NAMESIZE];
182 ret = mlx4_get_ifname(priv, &ifname);
186 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
189 file = fopen(path, "rb");
194 ret = fread(buf, 1, size, file);
195 if ((size_t)ret < size && ferror(file)) {
206 * Write to sysfs entry.
209 * Pointer to private structure.
211 * Entry name relative to sysfs path.
218 * Number of bytes written on success, negative errno value otherwise and
222 mlx4_sysfs_write(const struct priv *priv, const char *entry,
223 char *buf, size_t size)
225 char ifname[IF_NAMESIZE];
229 ret = mlx4_get_ifname(priv, &ifname);
233 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
236 file = fopen(path, "wb");
241 ret = fwrite(buf, 1, size, file);
242 if ((size_t)ret < size || ferror(file)) {
253 * Get unsigned long sysfs property.
256 * Pointer to private structure.
258 * Entry name relative to sysfs path.
260 * Value output buffer.
263 * 0 on success, negative errno value otherwise and rte_errno is set.
266 mlx4_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
269 unsigned long value_ret;
272 ret = mlx4_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
274 DEBUG("cannot read %s value from sysfs: %s",
275 name, strerror(rte_errno));
278 value_str[ret] = '\0';
280 value_ret = strtoul(value_str, NULL, 0);
283 DEBUG("invalid %s value `%s': %s", name, value_str,
284 strerror(rte_errno));
292 * Set unsigned long sysfs property.
295 * Pointer to private structure.
297 * Entry name relative to sysfs path.
302 * 0 on success, negative errno value otherwise and rte_errno is set.
305 mlx4_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
308 MKSTR(value_str, "%lu", value);
310 ret = mlx4_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
312 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
313 name, value_str, value, strerror(rte_errno));
320 * Perform ifreq ioctl() on associated Ethernet device.
323 * Pointer to private structure.
325 * Request number to pass to ioctl().
327 * Interface request structure output buffer.
330 * 0 on success, negative errno value otherwise and rte_errno is set.
333 mlx4_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
335 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
342 ret = mlx4_get_ifname(priv, &ifr->ifr_name);
343 if (!ret && ioctl(sock, req, ifr) == -1) {
352 * Get MAC address by querying netdevice.
355 * Pointer to private structure.
357 * MAC address output buffer.
360 * 0 on success, negative errno value otherwise and rte_errno is set.
363 mlx4_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
365 struct ifreq request;
366 int ret = mlx4_ifreq(priv, SIOCGIFHWADDR, &request);
370 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
378 * Pointer to private structure.
380 * MTU value output buffer.
383 * 0 on success, negative errno value otherwise and rte_errno is set.
386 mlx4_mtu_get(struct priv *priv, uint16_t *mtu)
388 unsigned long ulong_mtu = 0;
389 int ret = mlx4_get_sysfs_ulong(priv, "mtu", &ulong_mtu);
398 * DPDK callback to change the MTU.
401 * Pointer to Ethernet device structure.
406 * 0 on success, negative errno value otherwise and rte_errno is set.
409 mlx4_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
411 struct priv *priv = dev->data->dev_private;
413 int ret = mlx4_set_sysfs_ulong(priv, "mtu", mtu);
417 ret = mlx4_mtu_get(priv, &new_mtu);
420 if (new_mtu == mtu) {
432 * Pointer to private structure.
434 * Bitmask for flags that must remain untouched.
436 * Bitmask for flags to modify.
439 * 0 on success, negative errno value otherwise and rte_errno is set.
442 mlx4_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
444 unsigned long tmp = 0;
445 int ret = mlx4_get_sysfs_ulong(priv, "flags", &tmp);
450 tmp |= (flags & (~keep));
451 return mlx4_set_sysfs_ulong(priv, "flags", tmp);
455 * Change the link state (UP / DOWN).
458 * Pointer to Ethernet device private data.
460 * Nonzero for link up, otherwise link down.
463 * 0 on success, negative errno value otherwise and rte_errno is set.
466 mlx4_dev_set_link(struct priv *priv, int up)
468 struct rte_eth_dev *dev = priv->dev;
472 err = mlx4_set_flags(priv, ~IFF_UP, IFF_UP);
475 dev->rx_pkt_burst = mlx4_rx_burst;
477 err = mlx4_set_flags(priv, ~IFF_UP, ~IFF_UP);
480 dev->rx_pkt_burst = mlx4_rx_burst_removed;
481 dev->tx_pkt_burst = mlx4_tx_burst_removed;
487 * DPDK callback to bring the link DOWN.
490 * Pointer to Ethernet device structure.
493 * 0 on success, negative errno value otherwise and rte_errno is set.
496 mlx4_dev_set_link_down(struct rte_eth_dev *dev)
498 struct priv *priv = dev->data->dev_private;
500 return mlx4_dev_set_link(priv, 0);
504 * DPDK callback to bring the link UP.
507 * Pointer to Ethernet device structure.
510 * 0 on success, negative errno value otherwise and rte_errno is set.
513 mlx4_dev_set_link_up(struct rte_eth_dev *dev)
515 struct priv *priv = dev->data->dev_private;
517 return mlx4_dev_set_link(priv, 1);
521 * DPDK callback to get information about the device.
524 * Pointer to Ethernet device structure.
526 * Info structure output buffer.
529 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
531 struct priv *priv = dev->data->dev_private;
533 char ifname[IF_NAMESIZE];
535 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
538 /* FIXME: we should ask the device for these values. */
539 info->min_rx_bufsize = 32;
540 info->max_rx_pktlen = 65536;
542 * Since we need one CQ per QP, the limit is the minimum number
543 * between the two values.
545 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
546 priv->device_attr.max_qp : priv->device_attr.max_cq);
547 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
550 info->max_rx_queues = max;
551 info->max_tx_queues = max;
552 /* Last array entry is reserved for broadcast. */
553 info->max_mac_addrs = 1;
554 info->rx_offload_capa = 0;
555 info->tx_offload_capa = 0;
556 if (mlx4_get_ifname(priv, &ifname) == 0)
557 info->if_index = if_nametoindex(ifname);
567 * DPDK callback to get device statistics.
570 * Pointer to Ethernet device structure.
572 * Stats structure output buffer.
575 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
577 struct priv *priv = dev->data->dev_private;
578 struct rte_eth_stats tmp;
584 memset(&tmp, 0, sizeof(tmp));
585 /* Add software counters. */
586 for (i = 0; (i != priv->rxqs_n); ++i) {
587 struct rxq *rxq = (*priv->rxqs)[i];
591 idx = rxq->stats.idx;
592 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
593 tmp.q_ipackets[idx] += rxq->stats.ipackets;
594 tmp.q_ibytes[idx] += rxq->stats.ibytes;
595 tmp.q_errors[idx] += (rxq->stats.idropped +
596 rxq->stats.rx_nombuf);
598 tmp.ipackets += rxq->stats.ipackets;
599 tmp.ibytes += rxq->stats.ibytes;
600 tmp.ierrors += rxq->stats.idropped;
601 tmp.rx_nombuf += rxq->stats.rx_nombuf;
603 for (i = 0; (i != priv->txqs_n); ++i) {
604 struct txq *txq = (*priv->txqs)[i];
608 idx = txq->stats.idx;
609 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
610 tmp.q_opackets[idx] += txq->stats.opackets;
611 tmp.q_obytes[idx] += txq->stats.obytes;
612 tmp.q_errors[idx] += txq->stats.odropped;
614 tmp.opackets += txq->stats.opackets;
615 tmp.obytes += txq->stats.obytes;
616 tmp.oerrors += txq->stats.odropped;
622 * DPDK callback to clear device statistics.
625 * Pointer to Ethernet device structure.
628 mlx4_stats_reset(struct rte_eth_dev *dev)
630 struct priv *priv = dev->data->dev_private;
636 for (i = 0; (i != priv->rxqs_n); ++i) {
637 if ((*priv->rxqs)[i] == NULL)
639 idx = (*priv->rxqs)[i]->stats.idx;
640 (*priv->rxqs)[i]->stats =
641 (struct mlx4_rxq_stats){ .idx = idx };
643 for (i = 0; (i != priv->txqs_n); ++i) {
644 if ((*priv->txqs)[i] == NULL)
646 idx = (*priv->txqs)[i]->stats.idx;
647 (*priv->txqs)[i]->stats =
648 (struct mlx4_txq_stats){ .idx = idx };
653 * DPDK callback to retrieve physical link information.
656 * Pointer to Ethernet device structure.
657 * @param wait_to_complete
658 * Wait for request completion (ignored).
661 * 0 on success, negative errno value otherwise and rte_errno is set.
664 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
666 const struct priv *priv = dev->data->dev_private;
667 struct ethtool_cmd edata = {
671 struct rte_eth_link dev_link;
678 (void)wait_to_complete;
679 if (mlx4_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
680 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(rte_errno));
683 memset(&dev_link, 0, sizeof(dev_link));
684 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
685 (ifr.ifr_flags & IFF_RUNNING));
686 ifr.ifr_data = (void *)&edata;
687 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
688 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
689 strerror(rte_errno));
692 link_speed = ethtool_cmd_speed(&edata);
693 if (link_speed == -1)
694 dev_link.link_speed = 0;
696 dev_link.link_speed = link_speed;
697 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
698 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
699 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
700 ETH_LINK_SPEED_FIXED);
701 dev->data->dev_link = dev_link;
706 * DPDK callback to get flow control status.
709 * Pointer to Ethernet device structure.
710 * @param[out] fc_conf
711 * Flow control output buffer.
714 * 0 on success, negative errno value otherwise and rte_errno is set.
717 mlx4_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
719 struct priv *priv = dev->data->dev_private;
721 struct ethtool_pauseparam ethpause = {
722 .cmd = ETHTOOL_GPAUSEPARAM,
726 ifr.ifr_data = (void *)ðpause;
727 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
729 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
731 strerror(rte_errno));
734 fc_conf->autoneg = ethpause.autoneg;
735 if (ethpause.rx_pause && ethpause.tx_pause)
736 fc_conf->mode = RTE_FC_FULL;
737 else if (ethpause.rx_pause)
738 fc_conf->mode = RTE_FC_RX_PAUSE;
739 else if (ethpause.tx_pause)
740 fc_conf->mode = RTE_FC_TX_PAUSE;
742 fc_conf->mode = RTE_FC_NONE;
750 * DPDK callback to modify flow control parameters.
753 * Pointer to Ethernet device structure.
755 * Flow control parameters.
758 * 0 on success, negative errno value otherwise and rte_errno is set.
761 mlx4_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
763 struct priv *priv = dev->data->dev_private;
765 struct ethtool_pauseparam ethpause = {
766 .cmd = ETHTOOL_SPAUSEPARAM,
770 ifr.ifr_data = (void *)ðpause;
771 ethpause.autoneg = fc_conf->autoneg;
772 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
773 (fc_conf->mode & RTE_FC_RX_PAUSE))
774 ethpause.rx_pause = 1;
776 ethpause.rx_pause = 0;
777 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
778 (fc_conf->mode & RTE_FC_TX_PAUSE))
779 ethpause.tx_pause = 1;
781 ethpause.tx_pause = 0;
782 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
784 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
786 strerror(rte_errno));