mk: build with _GNU_SOURCE defined by default
[dpdk.git] / drivers / net / mlx5 / mlx5_ethdev.c
index b8f9136..d178ed6 100644 (file)
@@ -3,8 +3,6 @@
  * Copyright 2015 Mellanox Technologies, Ltd
  */
 
-#define _GNU_SOURCE
-
 #include <stddef.h>
 #include <assert.h>
 #include <inttypes.h>
 #include "mlx5_rxtx.h"
 #include "mlx5_utils.h"
 
+/* Supported speed values found in /usr/include/linux/ethtool.h */
+#ifndef HAVE_SUPPORTED_40000baseKR4_Full
+#define SUPPORTED_40000baseKR4_Full (1 << 23)
+#endif
+#ifndef HAVE_SUPPORTED_40000baseCR4_Full
+#define SUPPORTED_40000baseCR4_Full (1 << 24)
+#endif
+#ifndef HAVE_SUPPORTED_40000baseSR4_Full
+#define SUPPORTED_40000baseSR4_Full (1 << 25)
+#endif
+#ifndef HAVE_SUPPORTED_40000baseLR4_Full
+#define SUPPORTED_40000baseLR4_Full (1 << 26)
+#endif
+#ifndef HAVE_SUPPORTED_56000baseKR4_Full
+#define SUPPORTED_56000baseKR4_Full (1 << 27)
+#endif
+#ifndef HAVE_SUPPORTED_56000baseCR4_Full
+#define SUPPORTED_56000baseCR4_Full (1 << 28)
+#endif
+#ifndef HAVE_SUPPORTED_56000baseSR4_Full
+#define SUPPORTED_56000baseSR4_Full (1 << 29)
+#endif
+#ifndef HAVE_SUPPORTED_56000baseLR4_Full
+#define SUPPORTED_56000baseLR4_Full (1 << 30)
+#endif
+
 /* Add defines in case the running kernel is not the same as user headers. */
 #ifndef ETHTOOL_GLINKSETTINGS
 struct ethtool_link_settings {
@@ -103,7 +127,7 @@ struct ethtool_link_settings {
  * @return
  *   0 on success, a negative errno value otherwise and rte_errno is set.
  */
-int
+static int
 mlx5_get_master_ifname(const struct rte_eth_dev *dev,
                       char (*ifname)[IF_NAMESIZE])
 {
@@ -219,24 +243,20 @@ mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE])
  *   Pointer to Ethernet device.
  *
  * @return
- *   Interface index on success, a negative errno value otherwise and
- *   rte_errno is set.
+ *   Nonzero interface index on success, zero otherwise and rte_errno is set.
  */
-int
+unsigned int
 mlx5_ifindex(const struct rte_eth_dev *dev)
 {
        char ifname[IF_NAMESIZE];
-       unsigned int ret;
+       unsigned int ifindex;
 
-       ret = mlx5_get_ifname(dev, &ifname);
-       if (ret)
-               return ret;
-       ret = if_nametoindex(ifname);
-       if (ret == 0) {
+       if (mlx5_get_ifname(dev, &ifname))
+               return 0;
+       ifindex = if_nametoindex(ifname);
+       if (!ifindex)
                rte_errno = errno;
-               return -rte_errno;
-       }
-       return ret;
+       return ifindex;
 }
 
 /**
@@ -248,16 +268,12 @@ mlx5_ifindex(const struct rte_eth_dev *dev)
  *   Request number to pass to ioctl().
  * @param[out] ifr
  *   Interface request structure output buffer.
- * @param master
- *   When device is a port representor, perform request on master device
- *   instead.
  *
  * @return
  *   0 on success, a negative errno value otherwise and rte_errno is set.
  */
 int
-mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr,
-          int master)
+mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
 {
        int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
        int ret = 0;
@@ -266,10 +282,7 @@ mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr,
                rte_errno = errno;
                return -rte_errno;
        }
-       if (master)
-               ret = mlx5_get_master_ifname(dev, &ifr->ifr_name);
-       else
-               ret = mlx5_get_ifname(dev, &ifr->ifr_name);
+       ret = mlx5_get_ifname(dev, &ifr->ifr_name);
        if (ret)
                goto error;
        ret = ioctl(sock, req, ifr);
@@ -299,7 +312,7 @@ int
 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
 {
        struct ifreq request;
-       int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request, 0);
+       int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
 
        if (ret)
                return ret;
@@ -323,7 +336,7 @@ mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
 {
        struct ifreq request = { .ifr_mtu = mtu, };
 
-       return mlx5_ifreq(dev, SIOCSIFMTU, &request, 0);
+       return mlx5_ifreq(dev, SIOCSIFMTU, &request);
 }
 
 /**
@@ -343,13 +356,13 @@ int
 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
 {
        struct ifreq request;
-       int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request, 0);
+       int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
 
        if (ret)
                return ret;
        request.ifr_flags &= keep;
        request.ifr_flags |= flags & ~keep;
-       return mlx5_ifreq(dev, SIOCSIFFLAGS, &request, 0);
+       return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
 }
 
 /**
@@ -605,17 +618,20 @@ mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
        int link_speed = 0;
        int ret;
 
-       ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr, 1);
+       ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
        if (ret) {
                DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
                        dev->data->port_id, strerror(rte_errno));
                return ret;
        }
-       memset(&dev_link, 0, sizeof(dev_link));
-       dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
-                               (ifr.ifr_flags & IFF_RUNNING));
-       ifr.ifr_data = (void *)&edata;
-       ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr, 1);
+       dev_link = (struct rte_eth_link) {
+               .link_status = ((ifr.ifr_flags & IFF_UP) &&
+                               (ifr.ifr_flags & IFF_RUNNING)),
+       };
+       ifr = (struct ifreq) {
+               .ifr_data = (void *)&edata,
+       };
+       ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
        if (ret) {
                DRV_LOG(WARNING,
                        "port %u ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
@@ -644,8 +660,8 @@ mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
                                ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
        dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
                        ETH_LINK_SPEED_FIXED);
-       if ((dev_link.link_speed && !dev_link.link_status) ||
-           (!dev_link.link_speed && dev_link.link_status)) {
+       if (((dev_link.link_speed && !dev_link.link_status) ||
+            (!dev_link.link_speed && dev_link.link_status))) {
                rte_errno = EAGAIN;
                return -rte_errno;
        }
@@ -676,17 +692,20 @@ mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
        uint64_t sc;
        int ret;
 
-       ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr, 1);
+       ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
        if (ret) {
                DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
                        dev->data->port_id, strerror(rte_errno));
                return ret;
        }
-       memset(&dev_link, 0, sizeof(dev_link));
-       dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
-                               (ifr.ifr_flags & IFF_RUNNING));
-       ifr.ifr_data = (void *)&gcmd;
-       ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr, 1);
+       dev_link = (struct rte_eth_link) {
+               .link_status = ((ifr.ifr_flags & IFF_UP) &&
+                               (ifr.ifr_flags & IFF_RUNNING)),
+       };
+       ifr = (struct ifreq) {
+               .ifr_data = (void *)&gcmd,
+       };
+       ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
        if (ret) {
                DRV_LOG(DEBUG,
                        "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
@@ -703,7 +722,7 @@ mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
 
        *ecmd = gcmd;
        ifr.ifr_data = (void *)ecmd;
-       ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr, 1);
+       ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
        if (ret) {
                DRV_LOG(DEBUG,
                        "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
@@ -753,8 +772,8 @@ mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
                                ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
        dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
                                  ETH_LINK_SPEED_FIXED);
-       if ((dev_link.link_speed && !dev_link.link_status) ||
-           (!dev_link.link_speed && dev_link.link_status)) {
+       if (((dev_link.link_speed && !dev_link.link_status) ||
+            (!dev_link.link_speed && dev_link.link_status))) {
                rte_errno = EAGAIN;
                return -rte_errno;
        }
@@ -866,7 +885,7 @@ mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
        int ret;
 
        ifr.ifr_data = (void *)&ethpause;
-       ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr, 1);
+       ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
        if (ret) {
                DRV_LOG(WARNING,
                        "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
@@ -919,7 +938,7 @@ mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
                ethpause.tx_pause = 1;
        else
                ethpause.tx_pause = 0;
-       ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr, 0);
+       ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
        if (ret) {
                DRV_LOG(WARNING,
                        "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
@@ -1284,10 +1303,7 @@ mlx5_dev_to_port_id(const struct rte_device *dev, uint16_t *port_list,
        RTE_ETH_FOREACH_DEV(id) {
                struct rte_eth_dev *ldev = &rte_eth_devices[id];
 
-               if (!ldev->device ||
-                   !ldev->device->driver ||
-                   strcmp(ldev->device->driver->name, MLX5_DRIVER_NAME) ||
-                   ldev->device != dev)
+               if (ldev->device != dev)
                        continue;
                if (n < port_list_n)
                        port_list[n] = id;
@@ -1295,3 +1311,56 @@ mlx5_dev_to_port_id(const struct rte_device *dev, uint16_t *port_list,
        }
        return n;
 }
+
+/**
+ * Get switch information associated with network interface.
+ *
+ * @param ifindex
+ *   Network interface index.
+ * @param[out] info
+ *   Switch information object, populated in case of success.
+ *
+ * @return
+ *   0 on success, a negative errno value otherwise and rte_errno is set.
+ */
+int
+mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
+{
+       char ifname[IF_NAMESIZE];
+       FILE *file;
+       struct mlx5_switch_info data = { .master = 0, };
+       bool port_name_set = false;
+       bool port_switch_id_set = false;
+       char c;
+
+       if (!if_indextoname(ifindex, ifname)) {
+               rte_errno = errno;
+               return -rte_errno;
+       }
+
+       MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
+             ifname);
+       MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
+             ifname);
+
+       file = fopen(phys_port_name, "rb");
+       if (file != NULL) {
+               port_name_set =
+                       fscanf(file, "%d%c", &data.port_name, &c) == 2 &&
+                       c == '\n';
+               fclose(file);
+       }
+       file = fopen(phys_switch_id, "rb");
+       if (file == NULL) {
+               rte_errno = errno;
+               return -rte_errno;
+       }
+       port_switch_id_set =
+               fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
+               c == '\n';
+       fclose(file);
+       data.master = port_switch_id_set && !port_name_set;
+       data.representor = port_switch_id_set && port_name_set;
+       *info = data;
+       return 0;
+}