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 rte_eth_stats tmp;
581 memset(&tmp, 0, sizeof(tmp));
582 /* Add software counters. */
583 for (i = 0; i != dev->data->nb_rx_queues; ++i) {
584 struct rxq *rxq = dev->data->rx_queues[i];
588 idx = rxq->stats.idx;
589 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
590 tmp.q_ipackets[idx] += rxq->stats.ipackets;
591 tmp.q_ibytes[idx] += rxq->stats.ibytes;
592 tmp.q_errors[idx] += (rxq->stats.idropped +
593 rxq->stats.rx_nombuf);
595 tmp.ipackets += rxq->stats.ipackets;
596 tmp.ibytes += rxq->stats.ibytes;
597 tmp.ierrors += rxq->stats.idropped;
598 tmp.rx_nombuf += rxq->stats.rx_nombuf;
600 for (i = 0; i != dev->data->nb_tx_queues; ++i) {
601 struct txq *txq = dev->data->tx_queues[i];
605 idx = txq->stats.idx;
606 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
607 tmp.q_opackets[idx] += txq->stats.opackets;
608 tmp.q_obytes[idx] += txq->stats.obytes;
609 tmp.q_errors[idx] += txq->stats.odropped;
611 tmp.opackets += txq->stats.opackets;
612 tmp.obytes += txq->stats.obytes;
613 tmp.oerrors += txq->stats.odropped;
619 * DPDK callback to clear device statistics.
622 * Pointer to Ethernet device structure.
625 mlx4_stats_reset(struct rte_eth_dev *dev)
629 for (i = 0; i != dev->data->nb_rx_queues; ++i) {
630 struct rxq *rxq = dev->data->rx_queues[i];
633 rxq->stats = (struct mlx4_rxq_stats){
634 .idx = rxq->stats.idx,
637 for (i = 0; i != dev->data->nb_tx_queues; ++i) {
638 struct txq *txq = dev->data->tx_queues[i];
641 txq->stats = (struct mlx4_txq_stats){
642 .idx = txq->stats.idx,
648 * DPDK callback to retrieve physical link information.
651 * Pointer to Ethernet device structure.
652 * @param wait_to_complete
653 * Wait for request completion (ignored).
656 * 0 on success, negative errno value otherwise and rte_errno is set.
659 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
661 const struct priv *priv = dev->data->dev_private;
662 struct ethtool_cmd edata = {
666 struct rte_eth_link dev_link;
673 (void)wait_to_complete;
674 if (mlx4_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
675 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(rte_errno));
678 memset(&dev_link, 0, sizeof(dev_link));
679 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
680 (ifr.ifr_flags & IFF_RUNNING));
681 ifr.ifr_data = (void *)&edata;
682 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
683 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
684 strerror(rte_errno));
687 link_speed = ethtool_cmd_speed(&edata);
688 if (link_speed == -1)
689 dev_link.link_speed = 0;
691 dev_link.link_speed = link_speed;
692 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
693 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
694 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
695 ETH_LINK_SPEED_FIXED);
696 dev->data->dev_link = dev_link;
701 * DPDK callback to get flow control status.
704 * Pointer to Ethernet device structure.
705 * @param[out] fc_conf
706 * Flow control output buffer.
709 * 0 on success, negative errno value otherwise and rte_errno is set.
712 mlx4_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
714 struct priv *priv = dev->data->dev_private;
716 struct ethtool_pauseparam ethpause = {
717 .cmd = ETHTOOL_GPAUSEPARAM,
721 ifr.ifr_data = (void *)ðpause;
722 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
724 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
726 strerror(rte_errno));
729 fc_conf->autoneg = ethpause.autoneg;
730 if (ethpause.rx_pause && ethpause.tx_pause)
731 fc_conf->mode = RTE_FC_FULL;
732 else if (ethpause.rx_pause)
733 fc_conf->mode = RTE_FC_RX_PAUSE;
734 else if (ethpause.tx_pause)
735 fc_conf->mode = RTE_FC_TX_PAUSE;
737 fc_conf->mode = RTE_FC_NONE;
745 * DPDK callback to modify flow control parameters.
748 * Pointer to Ethernet device structure.
750 * Flow control parameters.
753 * 0 on success, negative errno value otherwise and rte_errno is set.
756 mlx4_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
758 struct priv *priv = dev->data->dev_private;
760 struct ethtool_pauseparam ethpause = {
761 .cmd = ETHTOOL_SPAUSEPARAM,
765 ifr.ifr_data = (void *)ðpause;
766 ethpause.autoneg = fc_conf->autoneg;
767 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
768 (fc_conf->mode & RTE_FC_RX_PAUSE))
769 ethpause.rx_pause = 1;
771 ethpause.rx_pause = 0;
772 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
773 (fc_conf->mode & RTE_FC_TX_PAUSE))
774 ethpause.tx_pause = 1;
776 ethpause.tx_pause = 0;
777 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
779 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
781 strerror(rte_errno));