net: add helpers to resolve negotiated flow control
authorRussell King <rmk+kernel@armlinux.org.uk>
Sat, 15 Feb 2020 15:49:27 +0000 (15:49 +0000)
committerDavid S. Miller <davem@davemloft.net>
Mon, 17 Feb 2020 03:39:44 +0000 (19:39 -0800)
Add a couple of helpers to resolve negotiated flow control. Two helpers
are provided:

- linkmode_resolve_pause() which takes the link partner and local
  advertisements, and decodes whether we should enable TX or RX pause
  at the MAC. This is useful outside of phylib, e.g. in phylink.
- phy_get_pause(), which returns the TX/RX enablement status for the
  current negotiation results of the PHY.

This allows us to centralise the flow control resolution, rather than
spreading it around.

Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/phy/Makefile
drivers/net/phy/linkmode.c [new file with mode: 0644]
drivers/net/phy/phy_device.c
include/linux/linkmode.h
include/linux/phy.h

index fe5badf13b651eface589a7d59c9b2f4873351ae..d523fd5670e42d3a77e85eaa35e3a6ddaac2a38d 100644 (file)
@@ -1,7 +1,8 @@
 # SPDX-License-Identifier: GPL-2.0
 # Makefile for Linux PHY drivers and MDIO bus drivers
 
-libphy-y                       := phy.o phy-c45.o phy-core.o phy_device.o
+libphy-y                       := phy.o phy-c45.o phy-core.o phy_device.o \
+                                  linkmode.o
 mdio-bus-y                     += mdio_bus.o mdio_device.o
 
 ifdef CONFIG_MDIO_DEVICE
diff --git a/drivers/net/phy/linkmode.c b/drivers/net/phy/linkmode.c
new file mode 100644 (file)
index 0000000..9699187
--- /dev/null
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: GPL-2.0+
+#include <linux/linkmode.h>
+
+/**
+ * linkmode_resolve_pause - resolve the allowable pause modes
+ * @local_adv: local advertisement in ethtool format
+ * @partner_adv: partner advertisement in ethtool format
+ * @tx_pause: pointer to bool to indicate whether transmit pause should be
+ * enabled.
+ * @rx_pause: pointer to bool to indicate whether receive pause should be
+ * enabled.
+ *
+ * Flow control is resolved according to our and the link partners
+ * advertisements using the following drawn from the 802.3 specs:
+ *  Local device  Link partner
+ *  Pause AsymDir Pause AsymDir Result
+ *    0     X       0     X     Disabled
+ *    0     1       1     0     Disabled
+ *    0     1       1     1     TX
+ *    1     0       0     X     Disabled
+ *    1     X       1     X     TX+RX
+ *    1     1       0     1     RX
+ */
+void linkmode_resolve_pause(const unsigned long *local_adv,
+                           const unsigned long *partner_adv,
+                           bool *tx_pause, bool *rx_pause)
+{
+       __ETHTOOL_DECLARE_LINK_MODE_MASK(m);
+
+       linkmode_and(m, local_adv, partner_adv);
+       if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, m)) {
+               *tx_pause = true;
+               *rx_pause = true;
+       } else if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, m)) {
+               *tx_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
+                                             partner_adv);
+               *rx_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
+                                             local_adv);
+       } else {
+               *tx_pause = false;
+               *rx_pause = false;
+       }
+}
+EXPORT_SYMBOL_GPL(linkmode_resolve_pause);
index 6a5056e0ae77578cc44006eaec2fef72cf646717..f5a7a077ec1fa9b3f76ebeeb1ac8f12222f00040 100644 (file)
@@ -2409,6 +2409,32 @@ bool phy_validate_pause(struct phy_device *phydev,
 }
 EXPORT_SYMBOL(phy_validate_pause);
 
+/**
+ * phy_get_pause - resolve negotiated pause modes
+ * @phydev: phy_device struct
+ * @tx_pause: pointer to bool to indicate whether transmit pause should be
+ * enabled.
+ * @rx_pause: pointer to bool to indicate whether receive pause should be
+ * enabled.
+ *
+ * Resolve and return the flow control modes according to the negotiation
+ * result. This includes checking that we are operating in full duplex mode.
+ * See linkmode_resolve_pause() for further details.
+ */
+void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause)
+{
+       if (phydev->duplex != DUPLEX_FULL) {
+               *tx_pause = false;
+               *rx_pause = false;
+               return;
+       }
+
+       return linkmode_resolve_pause(phydev->advertising,
+                                     phydev->lp_advertising,
+                                     tx_pause, rx_pause);
+}
+EXPORT_SYMBOL(phy_get_pause);
+
 static bool phy_drv_supports_irq(struct phy_driver *phydrv)
 {
        return phydrv->config_intr && phydrv->ack_interrupt;
index 8e5b352e44f2a97efc83d7582e3feec342bb56f4..9ec210f31d06184866c880dd80382b217876c7da 100644 (file)
@@ -88,4 +88,8 @@ static inline int linkmode_subset(const unsigned long *src1,
        return bitmap_subset(src1, src2, __ETHTOOL_LINK_MODE_MASK_NBITS);
 }
 
+void linkmode_resolve_pause(const unsigned long *local_adv,
+                           const unsigned long *partner_adv,
+                           bool *tx_pause, bool *rx_pause);
+
 #endif /* __LINKMODE_H */
index c570e162e05e58745d613325b0d93a6bbabf3ace..80f8b215827115208841544569af1e7ebcfecc20 100644 (file)
@@ -1257,6 +1257,9 @@ void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx);
 bool phy_validate_pause(struct phy_device *phydev,
                        struct ethtool_pauseparam *pp);
+void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause);
+void phy_resolve_pause(unsigned long *local_adv, unsigned long *partner_adv,
+                      bool *tx_pause, bool *rx_pause);
 
 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
                       int (*run)(struct phy_device *));