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.
397 * Allocate parent RSS queue when several RX queues are requested.
400 * Pointer to Ethernet device structure.
403 * 0 on success, errno value on failure.
406 dev_configure(struct rte_eth_dev *dev)
408 struct priv *priv = dev->data->dev_private;
409 unsigned int rxqs_n = dev->data->nb_rx_queues;
410 unsigned int txqs_n = dev->data->nb_tx_queues;
414 priv->rxqs = (void *)dev->data->rx_queues;
415 priv->txqs = (void *)dev->data->tx_queues;
416 if (txqs_n != priv->txqs_n) {
417 INFO("%p: TX queues number update: %u -> %u",
418 (void *)dev, priv->txqs_n, txqs_n);
419 priv->txqs_n = txqs_n;
421 if (rxqs_n == priv->rxqs_n)
423 INFO("%p: RX queues number update: %u -> %u",
424 (void *)dev, priv->rxqs_n, rxqs_n);
425 /* If RSS is enabled, disable it first. */
429 /* Only if there are no remaining child RX queues. */
430 for (i = 0; (i != priv->rxqs_n); ++i)
431 if ((*priv->rxqs)[i] != NULL)
433 rxq_cleanup(&priv->rxq_parent);
438 /* Nothing else to do. */
439 priv->rxqs_n = rxqs_n;
442 /* Allocate a new RSS parent queue if supported by hardware. */
444 ERROR("%p: only a single RX queue can be configured when"
445 " hardware doesn't support RSS",
449 /* Fail if hardware doesn't support that many RSS queues. */
450 if (rxqs_n >= priv->max_rss_tbl_sz) {
451 ERROR("%p: only %u RX queues can be configured for RSS",
452 (void *)dev, priv->max_rss_tbl_sz);
457 priv->rxqs_n = rxqs_n;
458 ret = rxq_setup(dev, &priv->rxq_parent, 0, 0, NULL, NULL);
461 /* Failure, rollback. */
469 * DPDK callback for Ethernet device configuration.
472 * Pointer to Ethernet device structure.
475 * 0 on success, negative errno value on failure.
478 mlx5_dev_configure(struct rte_eth_dev *dev)
480 struct priv *priv = dev->data->dev_private;
484 ret = dev_configure(dev);
491 * DPDK callback to get information about the device.
494 * Pointer to Ethernet device structure.
496 * Info structure output buffer.
499 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
501 struct priv *priv = dev->data->dev_private;
503 char ifname[IF_NAMESIZE];
506 /* FIXME: we should ask the device for these values. */
507 info->min_rx_bufsize = 32;
508 info->max_rx_pktlen = 65536;
510 * Since we need one CQ per QP, the limit is the minimum number
511 * between the two values.
513 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
514 priv->device_attr.max_qp : priv->device_attr.max_cq);
515 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
518 info->max_rx_queues = max;
519 info->max_tx_queues = max;
520 /* Last array entry is reserved for broadcast. */
521 info->max_mac_addrs = (RTE_DIM(priv->mac) - 1);
522 info->rx_offload_capa =
524 (DEV_RX_OFFLOAD_IPV4_CKSUM |
525 DEV_RX_OFFLOAD_UDP_CKSUM |
526 DEV_RX_OFFLOAD_TCP_CKSUM) :
528 info->tx_offload_capa =
530 (DEV_TX_OFFLOAD_IPV4_CKSUM |
531 DEV_TX_OFFLOAD_UDP_CKSUM |
532 DEV_TX_OFFLOAD_TCP_CKSUM) :
534 if (priv_get_ifname(priv, &ifname) == 0)
535 info->if_index = if_nametoindex(ifname);
540 * DPDK callback to retrieve physical link information (unlocked version).
543 * Pointer to Ethernet device structure.
544 * @param wait_to_complete
545 * Wait for request completion (ignored).
548 mlx5_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
550 struct priv *priv = dev->data->dev_private;
551 struct ethtool_cmd edata = {
555 struct rte_eth_link dev_link;
558 (void)wait_to_complete;
559 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
560 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
563 memset(&dev_link, 0, sizeof(dev_link));
564 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
565 (ifr.ifr_flags & IFF_RUNNING));
566 ifr.ifr_data = &edata;
567 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
568 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
572 link_speed = ethtool_cmd_speed(&edata);
573 if (link_speed == -1)
574 dev_link.link_speed = 0;
576 dev_link.link_speed = link_speed;
577 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
578 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
579 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
580 /* Link status changed. */
581 dev->data->dev_link = dev_link;
584 /* Link status is still the same. */
589 * DPDK callback to retrieve physical link information.
592 * Pointer to Ethernet device structure.
593 * @param wait_to_complete
594 * Wait for request completion (ignored).
597 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
599 struct priv *priv = dev->data->dev_private;
603 ret = mlx5_link_update_unlocked(dev, wait_to_complete);
609 * DPDK callback to change the MTU.
611 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
612 * received). Use this as a hint to enable/disable scattered packets support
613 * and improve performance when not needed.
614 * Since failure is not an option, reconfiguring queues on the fly is not
618 * Pointer to Ethernet device structure.
623 * 0 on success, negative errno value on failure.
626 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
628 struct priv *priv = dev->data->dev_private;
631 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
635 /* Set kernel interface MTU first. */
636 if (priv_set_mtu(priv, mtu)) {
638 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
642 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
644 /* Temporarily replace RX handler with a fake one, assuming it has not
645 * been copied elsewhere. */
646 dev->rx_pkt_burst = removed_rx_burst;
647 /* Make sure everyone has left mlx5_rx_burst() and uses
648 * removed_rx_burst() instead. */
651 /* Reconfigure each RX queue. */
652 for (i = 0; (i != priv->rxqs_n); ++i) {
653 struct rxq *rxq = (*priv->rxqs)[i];
654 unsigned int max_frame_len;
659 /* Calculate new maximum frame length according to MTU and
660 * toggle scattered support (sp) if necessary. */
661 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
662 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
663 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
664 /* Provide new values to rxq_setup(). */
665 dev->data->dev_conf.rxmode.jumbo_frame = sp;
666 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
667 ret = rxq_rehash(dev, rxq);
669 /* Force SP RX if that queue requires it and abort. */
671 rx_func = mlx5_rx_burst_sp;
674 /* Reenable non-RSS queue attributes. No need to check
675 * for errors at this stage. */
678 rxq_mac_addrs_add(rxq);
679 if (priv->started && priv->promisc_req)
680 rxq_promiscuous_enable(rxq);
681 if (priv->started && priv->allmulti_req)
682 rxq_allmulticast_enable(rxq);
684 /* Scattered burst function takes priority. */
686 rx_func = mlx5_rx_burst_sp;
688 /* Burst functions can now be called again. */
690 dev->rx_pkt_burst = rx_func;
698 * Get PCI information from struct ibv_device.
701 * Pointer to Ethernet device structure.
702 * @param[out] pci_addr
703 * PCI bus address output buffer.
706 * 0 on success, -1 on failure and errno is set.
709 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
710 struct rte_pci_addr *pci_addr)
714 MKSTR(path, "%s/device/uevent", device->ibdev_path);
716 file = fopen(path, "rb");
719 while (fgets(line, sizeof(line), file) == line) {
720 size_t len = strlen(line);
723 /* Truncate long lines. */
724 if (len == (sizeof(line) - 1))
725 while (line[(len - 1)] != '\n') {
729 line[(len - 1)] = ret;
731 /* Extract information. */
734 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
738 &pci_addr->function) == 4) {