[PATCH net-next v2 2/2] net: dsa: microchip: ksz8: Add function to configure downstream ports for KSZ8xxx

From: Oleksij Rempel
Date: Wed May 17 2023 - 08:11:12 EST


This patch introduces the function 'ksz8_downstram_link_up' to the
Microchip KSZ8xxx driver. This function configures the flow control settings
for the downstream ports of the switch based on desired settings and the
current duplex mode.

The KSZ8795 switch, unlike the KSZ8873, supports asynchronous pause control.
However, a single bit controls both RX and TX pause, so we can't enforce
asynchronous pause control. The flow control can be set based on the
auto-negotiation process, depending on the capabilities of both link partners.

For the KSZ8873, the PORT_FORCE_FLOW_CTRL bit can be set by the hardware
bootstrap, ignoring the auto-negotiation result. Therefore, even in
auto-negotiation mode, we need to ensure that the PORT_FORCE_FLOW_CTRL bit is
correctly set.

In the absence of auto-negotiation, we will enforce synchronous pause control
for the KSZ8795 switch.

Note: It is currently not possible to force disable flow control on a port if
we still advertise pause support. This configuration is not currently supported
by Linux, and it may not make practical sense. However, it's essential to
understand this limitation when working with the KSZ8873 and similar devices.

Signed-off-by: Oleksij Rempel <o.rempel@xxxxxxxxxxxxxx>
---
drivers/net/dsa/microchip/ksz8795.c | 84 +++++++++++++++++++++++++++++
1 file changed, 84 insertions(+)

diff --git a/drivers/net/dsa/microchip/ksz8795.c b/drivers/net/dsa/microchip/ksz8795.c
index 75b98a5d53af..a6cacf273991 100644
--- a/drivers/net/dsa/microchip/ksz8795.c
+++ b/drivers/net/dsa/microchip/ksz8795.c
@@ -1371,6 +1371,88 @@ void ksz8_config_cpu_port(struct dsa_switch *ds)
}
}

+/**
+ * ksz8_downstram_link_up - Configures the downstream port of the switch.
+ * @dev: The KSZ device instance.
+ * @port: The port number to configure.
+ * @duplex: The desired duplex mode.
+ * @tx_pause: If true, enables transmit pause.
+ * @rx_pause: If true, enables receive pause.
+ *
+ * Description:
+ * The function configures flow control settings for a given port based on the
+ * desired settings and current duplex mode.
+ *
+ * According to the KSZ8873 datasheet, the PORT_FORCE_FLOW_CTRL bit in the
+ * Port Control 2 register (0x1A for Port 1, 0x22 for Port 2, 0x32 for Port 3)
+ * determines how flow control is handled on the port:
+ * "1 = will always enable full-duplex flow control on the port, regardless
+ * of AN result.
+ * 0 = full-duplex flow control is enabled based on AN result."
+ *
+ * This means that the flow control behavior depends on the state of this bit:
+ * - If PORT_FORCE_FLOW_CTRL is set to 1, the switch will ignore AN results and
+ * force flow control on the port.
+ * - If PORT_FORCE_FLOW_CTRL is set to 0, the switch will enable or disable
+ * flow control based on the AN results.
+ *
+ * However, there is a potential limitation in this configuration. It is
+ * currently not possible to force disable flow control on a port if we still
+ * advertise pause support. While such a configuration is not currently
+ * supported by Linux, and may not make practical sense, it's important to be
+ * aware of this limitation when working with the KSZ8873 and similar devices.
+ */
+static void ksz8_downstram_link_up(struct ksz_device *dev, int port,
+ int duplex, bool tx_pause, bool rx_pause)
+{
+ const u16 *regs = dev->info->regs;
+ u8 ctrl = 0;
+ int ret;
+
+ /*
+ * The KSZ8795 switch differs from the KSZ8873 by supporting
+ * asynchronous pause control. However, since a single bit is used to
+ * control both RX and TX pause, we can't enforce asynchronous pause
+ * control - both TX and RX pause will be either enabled or disabled
+ * together.
+ *
+ * If auto-negotiation is enabled, we usually allow the flow control to
+ * be determined by the auto-negotiation process based on the
+ * capabilities of both link partners. However, for KSZ8873, the
+ * PORT_FORCE_FLOW_CTRL bit may be set by the hardware bootstrap,
+ * ignoring the auto-negotiation result. Thus, even in auto-negotiatio
+ * mode, we need to ensure that the PORT_FORCE_FLOW_CTRL bit is
+ * properly cleared.
+ *
+ * In the absence of auto-negotiation, we will enforce synchronous
+ * pause control for the KSZ8795 switch.
+ */
+ if (duplex) {
+ bool aneg_en = false;
+
+ ret = ksz_pread8(dev, port, regs[P_FORCE_CTRL], &ctrl);
+ if (ret)
+ return;
+
+ if (ksz_is_ksz88x3(dev)) {
+ if ((ctrl & PORT_AUTO_NEG_ENABLE))
+ aneg_en = true;
+ } else {
+ if (!(ctrl & PORT_AUTO_NEG_DISABLE))
+ aneg_en = true;
+ }
+
+ if (!aneg_en && (tx_pause || rx_pause))
+ ctrl |= PORT_FORCE_FLOW_CTRL;
+ } else {
+ if (tx_pause || rx_pause)
+ ctrl |= PORT_BACK_PRESSURE;
+ }
+
+ ksz_prmw8(dev, port, regs[P_STP_CTRL], PORT_FORCE_FLOW_CTRL |
+ PORT_BACK_PRESSURE, ctrl);
+}
+
/**
* ksz8_upstram_link_up - Configures the CPU/upstream port of the switch.
* @dev: The KSZ device instance.
@@ -1414,6 +1496,8 @@ void ksz8_phylink_mac_link_up(struct ksz_device *dev, int port,
if (dsa_is_upstream_port(dev->ds, port))
ksz8_upstram_link_up(dev, port, speed, duplex, tx_pause,
rx_pause);
+ else
+ ksz8_downstram_link_up(dev, port, duplex, tx_pause, rx_pause);
}

static int ksz8_handle_global_errata(struct dsa_switch *ds)
--
2.39.2