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>
51 /* DPDK headers don't like -pedantic. */
53 #pragma GCC diagnostic ignored "-pedantic"
55 #include <rte_atomic.h>
56 #include <rte_ethdev.h>
58 #include <rte_common.h>
60 #pragma GCC diagnostic error "-pedantic"
64 #include "mlx5_rxtx.h"
65 #include "mlx5_utils.h"
68 * Get interface name from private structure.
71 * Pointer to private structure.
73 * Interface name output buffer.
76 * 0 on success, -1 on failure and errno is set.
79 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
83 unsigned int dev_type = 0;
84 unsigned int dev_port_prev = ~0u;
85 char match[IF_NAMESIZE] = "";
88 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
94 while ((dent = readdir(dir)) != NULL) {
95 char *name = dent->d_name;
97 unsigned int dev_port;
100 if ((name[0] == '.') &&
101 ((name[1] == '\0') ||
102 ((name[1] == '.') && (name[2] == '\0'))))
105 MKSTR(path, "%s/device/net/%s/%s",
106 priv->ctx->device->ibdev_path, name,
107 (dev_type ? "dev_id" : "dev_port"));
109 file = fopen(path, "rb");
114 * Switch to dev_id when dev_port does not exist as
115 * is the case with Linux kernel versions < 3.15.
126 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
131 * Switch to dev_id when dev_port returns the same value for
132 * all ports. May happen when using a MOFED release older than
133 * 3.0 with a Linux kernel >= 3.15.
135 if (dev_port == dev_port_prev)
137 dev_port_prev = dev_port;
138 if (dev_port == (priv->port - 1u))
139 snprintf(match, sizeof(match), "%s", name);
142 if (match[0] == '\0')
144 strncpy(*ifname, match, sizeof(*ifname));
149 * Read from sysfs entry.
152 * Pointer to private structure.
154 * Entry name relative to sysfs path.
156 * Data output buffer.
161 * 0 on success, -1 on failure and errno is set.
164 priv_sysfs_read(const struct priv *priv, const char *entry,
165 char *buf, size_t size)
167 char ifname[IF_NAMESIZE];
172 if (priv_get_ifname(priv, &ifname))
175 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
178 file = fopen(path, "rb");
181 ret = fread(buf, 1, size, file);
183 if (((size_t)ret < size) && (ferror(file)))
193 * Write to sysfs entry.
196 * Pointer to private structure.
198 * Entry name relative to sysfs path.
205 * 0 on success, -1 on failure and errno is set.
208 priv_sysfs_write(const struct priv *priv, const char *entry,
209 char *buf, size_t size)
211 char ifname[IF_NAMESIZE];
216 if (priv_get_ifname(priv, &ifname))
219 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
222 file = fopen(path, "wb");
225 ret = fwrite(buf, 1, size, file);
227 if (((size_t)ret < size) || (ferror(file)))
237 * Get unsigned long sysfs property.
240 * Pointer to private structure.
242 * Entry name relative to sysfs path.
244 * Value output buffer.
247 * 0 on success, -1 on failure and errno is set.
250 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
253 unsigned long value_ret;
256 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
258 DEBUG("cannot read %s value from sysfs: %s",
259 name, strerror(errno));
262 value_str[ret] = '\0';
264 value_ret = strtoul(value_str, NULL, 0);
266 DEBUG("invalid %s value `%s': %s", name, value_str,
275 * Set unsigned long sysfs property.
278 * Pointer to private structure.
280 * Entry name relative to sysfs path.
285 * 0 on success, -1 on failure and errno is set.
288 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
291 MKSTR(value_str, "%lu", value);
293 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
295 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
296 name, value_str, value, strerror(errno));
303 * Perform ifreq ioctl() on associated Ethernet device.
306 * Pointer to private structure.
308 * Request number to pass to ioctl().
310 * Interface request structure output buffer.
313 * 0 on success, -1 on failure and errno is set.
316 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
318 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
323 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
324 ret = ioctl(sock, req, ifr);
333 * Pointer to private structure.
335 * MTU value output buffer.
338 * 0 on success, -1 on failure and errno is set.
341 priv_get_mtu(struct priv *priv, uint16_t *mtu)
343 unsigned long ulong_mtu;
345 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
355 * Pointer to private structure.
360 * 0 on success, -1 on failure and errno is set.
363 priv_set_mtu(struct priv *priv, uint16_t mtu)
365 return priv_set_sysfs_ulong(priv, "mtu", mtu);
372 * Pointer to private structure.
374 * Bitmask for flags that must remain untouched.
376 * Bitmask for flags to modify.
379 * 0 on success, -1 on failure and errno is set.
382 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
386 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
390 return priv_set_sysfs_ulong(priv, "flags", tmp);
394 * Ethernet device configuration.
396 * Prepare the driver for a given number of TX and RX queues.
399 * Pointer to Ethernet device structure.
402 * 0 on success, errno value on failure.
405 dev_configure(struct rte_eth_dev *dev)
407 struct priv *priv = dev->data->dev_private;
408 unsigned int rxqs_n = dev->data->nb_rx_queues;
409 unsigned int txqs_n = dev->data->nb_tx_queues;
411 priv->rxqs = (void *)dev->data->rx_queues;
412 priv->txqs = (void *)dev->data->tx_queues;
413 if (txqs_n != priv->txqs_n) {
414 INFO("%p: TX queues number update: %u -> %u",
415 (void *)dev, priv->txqs_n, txqs_n);
416 priv->txqs_n = txqs_n;
418 if (rxqs_n == priv->rxqs_n)
420 INFO("%p: RX queues number update: %u -> %u",
421 (void *)dev, priv->rxqs_n, rxqs_n);
422 priv->rxqs_n = rxqs_n;
427 * DPDK callback for Ethernet device configuration.
430 * Pointer to Ethernet device structure.
433 * 0 on success, negative errno value on failure.
436 mlx5_dev_configure(struct rte_eth_dev *dev)
438 struct priv *priv = dev->data->dev_private;
442 ret = dev_configure(dev);
449 * DPDK callback to get information about the device.
452 * Pointer to Ethernet device structure.
454 * Info structure output buffer.
457 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
459 struct priv *priv = dev->data->dev_private;
461 char ifname[IF_NAMESIZE];
464 /* FIXME: we should ask the device for these values. */
465 info->min_rx_bufsize = 32;
466 info->max_rx_pktlen = 65536;
468 * Since we need one CQ per QP, the limit is the minimum number
469 * between the two values.
471 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
472 priv->device_attr.max_qp : priv->device_attr.max_cq);
473 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
476 info->max_rx_queues = max;
477 info->max_tx_queues = max;
478 /* Last array entry is reserved for broadcast. */
479 info->max_mac_addrs = (RTE_DIM(priv->mac) - 1);
480 info->rx_offload_capa =
482 (DEV_RX_OFFLOAD_IPV4_CKSUM |
483 DEV_RX_OFFLOAD_UDP_CKSUM |
484 DEV_RX_OFFLOAD_TCP_CKSUM) :
486 info->tx_offload_capa =
488 (DEV_TX_OFFLOAD_IPV4_CKSUM |
489 DEV_TX_OFFLOAD_UDP_CKSUM |
490 DEV_TX_OFFLOAD_TCP_CKSUM) :
492 if (priv_get_ifname(priv, &ifname) == 0)
493 info->if_index = if_nametoindex(ifname);
498 * DPDK callback to retrieve physical link information (unlocked version).
501 * Pointer to Ethernet device structure.
502 * @param wait_to_complete
503 * Wait for request completion (ignored).
506 mlx5_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
508 struct priv *priv = dev->data->dev_private;
509 struct ethtool_cmd edata = {
513 struct rte_eth_link dev_link;
516 (void)wait_to_complete;
517 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
518 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
521 memset(&dev_link, 0, sizeof(dev_link));
522 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
523 (ifr.ifr_flags & IFF_RUNNING));
524 ifr.ifr_data = &edata;
525 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
526 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
530 link_speed = ethtool_cmd_speed(&edata);
531 if (link_speed == -1)
532 dev_link.link_speed = 0;
534 dev_link.link_speed = link_speed;
535 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
536 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
537 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
538 /* Link status changed. */
539 dev->data->dev_link = dev_link;
542 /* Link status is still the same. */
547 * DPDK callback to retrieve physical link information.
550 * Pointer to Ethernet device structure.
551 * @param wait_to_complete
552 * Wait for request completion (ignored).
555 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
557 struct priv *priv = dev->data->dev_private;
561 ret = mlx5_link_update_unlocked(dev, wait_to_complete);
567 * DPDK callback to change the MTU.
569 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
570 * received). Use this as a hint to enable/disable scattered packets support
571 * and improve performance when not needed.
572 * Since failure is not an option, reconfiguring queues on the fly is not
576 * Pointer to Ethernet device structure.
581 * 0 on success, negative errno value on failure.
584 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
586 struct priv *priv = dev->data->dev_private;
589 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
593 /* Set kernel interface MTU first. */
594 if (priv_set_mtu(priv, mtu)) {
596 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
600 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
602 /* Temporarily replace RX handler with a fake one, assuming it has not
603 * been copied elsewhere. */
604 dev->rx_pkt_burst = removed_rx_burst;
605 /* Make sure everyone has left mlx5_rx_burst() and uses
606 * removed_rx_burst() instead. */
609 /* Reconfigure each RX queue. */
610 for (i = 0; (i != priv->rxqs_n); ++i) {
611 struct rxq *rxq = (*priv->rxqs)[i];
612 unsigned int max_frame_len;
617 /* Calculate new maximum frame length according to MTU and
618 * toggle scattered support (sp) if necessary. */
619 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
620 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
621 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
622 /* Provide new values to rxq_setup(). */
623 dev->data->dev_conf.rxmode.jumbo_frame = sp;
624 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
625 ret = rxq_rehash(dev, rxq);
627 /* Force SP RX if that queue requires it and abort. */
629 rx_func = mlx5_rx_burst_sp;
632 /* Scattered burst function takes priority. */
634 rx_func = mlx5_rx_burst_sp;
636 /* Burst functions can now be called again. */
638 dev->rx_pkt_burst = rx_func;
646 * DPDK callback to get flow control status.
649 * Pointer to Ethernet device structure.
650 * @param[out] fc_conf
651 * Flow control output buffer.
654 * 0 on success, negative errno value on failure.
657 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
659 struct priv *priv = dev->data->dev_private;
661 struct ethtool_pauseparam ethpause = {
662 .cmd = ETHTOOL_GPAUSEPARAM
666 ifr.ifr_data = ðpause;
668 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
670 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
676 fc_conf->autoneg = ethpause.autoneg;
677 if (ethpause.rx_pause && ethpause.tx_pause)
678 fc_conf->mode = RTE_FC_FULL;
679 else if (ethpause.rx_pause)
680 fc_conf->mode = RTE_FC_RX_PAUSE;
681 else if (ethpause.tx_pause)
682 fc_conf->mode = RTE_FC_TX_PAUSE;
684 fc_conf->mode = RTE_FC_NONE;
694 * DPDK callback to modify flow control parameters.
697 * Pointer to Ethernet device structure.
699 * Flow control parameters.
702 * 0 on success, negative errno value on failure.
705 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
707 struct priv *priv = dev->data->dev_private;
709 struct ethtool_pauseparam ethpause = {
710 .cmd = ETHTOOL_SPAUSEPARAM
714 ifr.ifr_data = ðpause;
715 ethpause.autoneg = fc_conf->autoneg;
716 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
717 (fc_conf->mode & RTE_FC_RX_PAUSE))
718 ethpause.rx_pause = 1;
720 ethpause.rx_pause = 0;
722 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
723 (fc_conf->mode & RTE_FC_TX_PAUSE))
724 ethpause.tx_pause = 1;
726 ethpause.tx_pause = 0;
729 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
731 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
745 * Get PCI information from struct ibv_device.
748 * Pointer to Ethernet device structure.
749 * @param[out] pci_addr
750 * PCI bus address output buffer.
753 * 0 on success, -1 on failure and errno is set.
756 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
757 struct rte_pci_addr *pci_addr)
761 MKSTR(path, "%s/device/uevent", device->ibdev_path);
763 file = fopen(path, "rb");
766 while (fgets(line, sizeof(line), file) == line) {
767 size_t len = strlen(line);
770 /* Truncate long lines. */
771 if (len == (sizeof(line) - 1))
772 while (line[(len - 1)] != '\n') {
776 line[(len - 1)] = ret;
778 /* Extract information. */
781 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
785 &pci_addr->function) == 4) {