Skip to contentSkip to conversation

net: lan743x: automatically recover from TX hang

PATCH In review Chris Babroski 3 participants 13 messages Nov 24 – Nov 25

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover net: lan743x: automatically recover from TX hang

Background
----------
On a custom HW platform that uses the LAN7430 NIC we have been able to reproduce a scenario where the SoC stops receiving TX completion interrupts, appearing as a TX hang. After dumping and decoding the LAN7430 registers we see that the TX channel reports that it is in started state with no error, but TX_HEAD stops advancing. TX MSI-X stops incrementing while RX interrupts continue to increment.

This scenario has most often been reproduced in large server cluster environments, but it's also been seen locally during repeated SCP and iperf testing.

The root cause of this issue is still unknown and we are continuing to debug. It was found that bringing the interface down and up recovers the network.

What this patch series does
---------------------------
This patch series attempts to implement automatic recovery of the network interface after a TX hang is detected. It does not fix the underlying root cause of the TX hang.

At a high level, the patches implement the following:
1. ndo_tx_timeout support to reset and recover the interface when the netdev TX watchdog triggers.
2. BQL support to decrease the amount of time it takes for the watchdog to trigger.
3. Stability improvements and error handling around the open/close paths that now get executed more frequently as part of the TX hang recovery flow.

Potential issues and concerns
-----------------------------
* I do not have fixed-link hardware to test patch 4 on. The change was requested by Sashiko AI review of the v1 patch series.
* LLM review tools flagged several possible bugs during development and v1 review that resulted in the inclusion of several "Fixes:" tagged patches in this series. They are included in this series with the ndo_tx_timeout support because lan743x_netdev_open and lan743x_netdev_close are now called more frequently as a result of TX hang recovery.

Testing
-------
Testing was performed on a 7.0 based kernel using the same driver code. Please let me know if you would like me to run any other tests.

Tests:
* Verified the TX timeout watchdog triggers and recovers the network interface when TX hang is reproduced. This was done as part of repeated SCP loop testing and is also being tested in a server cluster environment. dmesg was inspected for "NETDEV WATCHDOG" to verify that the watchdog is being triggered. No errors were seen in dmesg after reset.
* Performance tests using iperf3 to measure RX and TX throughput show no noticeable difference with the patches applied.
* Repeated module load and unload cycles produce no errors in dmesg. Network appears to function as expected when module is loaded.
* Repeated ifconfig down and up cycles produce no errors in dmesg. Network appears to function as expected when interface is back up.

Changes in v2:
- Patch 1 (was "net: lan743x: reset DMA channels that fail to stop"):
- Retitled to "check DMA channel stop and reset results".
- Reset a TX channel that fails to stop only after its FIFO is disabled, before the ring is freed (Woojung).
- Fail lan743x_tx_open()/lan743x_rx_open() if the channel or TX FIFO reset times out, undoing the earlier steps (Nicolai).
- Patches 2 and 3: swapped, so TX is quiesced before phylink is moved and the series stays bisectable (Sashiko).
- Patch 4: new, keep the fixed link across reopen on boards that use the fixed-link fallback (Sashiko).
- Patch 5: new, handle a failed reopen on resume, keep the PCI resources when lan743x_hardware_init() fails on resume, and program the RX filter again after the device is attached (Nicolai, Sashiko).
- Patch 6 (reset the device on TX timeout):
- Move the ndo_stop wrapper to patch 5.
- Leave the device detached if the DMA controller reset fails, so it cannot be opened on a controller that is still in reset (Sashiko).
- Program the RX filter again after the device is attached (Sashiko).
- Commit message: state that ptp4l has to be restarted after a reset (Nicolai). PHC registration stays in open/close, as discussed with Microchip (Woojung).
- v1: https://lore.kernel.org/r/[email protected]

Chris Babroski (7):
net: lan743x: check DMA channel stop and reset results
net: lan743x: quiesce TX before freeing the rings
net: lan743x: start phylink last and stop it first
net: lan743x: keep the fixed link across reopen
net: lan743x: handle a failed reopen on resume
net: lan743x: reset the device on TX timeout
net: lan743x: add byte queue limits support

 drivers/net/ethernet/microchip/lan743x_main.c | 282 +++++++++++++++---
 drivers/net/ethernet/microchip/lan743x_main.h |   2 +
 2 files changed, 241 insertions(+), 43 deletions(-)

base-commit: d6816d9a93b66a12bf01e10037e377f3333ad6cc

Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/7 net: lan743x: check DMA channel stop and reset results

lan743x_tx_close() and lan743x_rx_close() ask the DMA controller to stop the channel and wait for it, but ignore the result. If the channel is still stop-pending when the wait gives up, the descriptor ring, the head write-back buffer and the packet buffers are unmapped and freed while the channel may still be fetching from or writing to them.

A TX channel has been seen to stop making progress, with its head index no longer advancing while DMAC_CMD still reports it as started. A channel in that state may not honour the stop request either, and closing the interface would then free memory the engine can still access.

If a channel is still stop-pending after the wait, or still reads as started, issue a channel soft reset and wait for it before anything is freed, and log when that happens. For RX the FIFO is already disabled at that point. For TX the reset is issued after the TX FIFO has been disabled, since the TX channel should only be reset once its FIFO is stopped.

Use the same helpers for the channel resets that lan743x_tx_open() and lan743x_rx_open() already open-code. If the channel or TX FIFO reset times out there, log it and fail the open instead of starting a channel that never came out of reset.

Fixes 23f0703c125b ("lan743x: Add main source files for new lan743x driver")
---
 drivers/net/ethernet/microchip/lan743x_main.c | 134 ++++++++++++++----
 1 file changed, 107 insertions(+), 27 deletions(-)
drivers/net/ethernet/microchip/lan743x_main.c +107 -27
index 82d3ec20ba25..92cdc13495fe 100644
@@ -1584,6 +1584,21 @@ static int lan743x_dmac_tx_wait_till_stopped(struct lan743x_adapter *adapter,
 	return result;
 }
 
+static int lan743x_dmac_tx_reset(struct lan743x_adapter *adapter,
+				 int tx_channel)
+{
+	int ret;
+
+	lan743x_csr_write(adapter, DMAC_CMD, DMAC_CMD_TX_SWR_(tx_channel));
+	ret = lan743x_csr_wait_for_bit(adapter, DMAC_CMD,
+				       DMAC_CMD_TX_SWR_(tx_channel),
+				       0, 1000, 20000, 100);
+	if (ret)
+		netdev_err(adapter->netdev,
+			   "TX channel %d reset timed out\n", tx_channel);
+	return ret;
+}
+
 static int lan743x_dmac_rx_get_state(struct lan743x_adapter *adapter,
 				     int rx_channel)
 {
@@ -1613,6 +1628,21 @@ static int lan743x_dmac_rx_wait_till_stopped(struct lan743x_adapter *adapter,
 	return result;
 }
 
+static int lan743x_dmac_rx_reset(struct lan743x_adapter *adapter,
+				 int rx_channel)
+{
+	int ret;
+
+	lan743x_csr_write(adapter, DMAC_CMD, DMAC_CMD_RX_SWR_(rx_channel));
+	ret = lan743x_csr_wait_for_bit(adapter, DMAC_CMD,
+				       DMAC_CMD_RX_SWR_(rx_channel),
+				       0, 1000, 20000, 100);
+	if (ret)
+		netdev_err(adapter->netdev,
+			   "RX channel %d reset timed out\n", rx_channel);
+	return ret;
+}
+
 static void lan743x_tx_release_desc(struct lan743x_tx *tx,
 				    int descriptor_index, bool cleanup)
 {
@@ -2218,14 +2248,63 @@ static int lan743x_tx_ring_init(struct lan743x_tx *tx)
 	return ret;
 }
 
+static int lan743x_tx_fifo_enable(struct lan743x_tx *tx)
+{
+	struct lan743x_adapter *adapter = tx->adapter;
+	int ret;
+
+	/* initialize fifo */
+	lan743x_csr_write(adapter, FCT_TX_CTL,
+			  FCT_TX_CTL_RESET_(tx->channel_number));
+	ret = lan743x_csr_wait_for_bit(adapter, FCT_TX_CTL,
+				       FCT_TX_CTL_RESET_(tx->channel_number),
+				       0, 1000, 20000, 100);
+	if (ret) {
+		netdev_err(adapter->netdev,
+			   "TX channel %d FIFO reset timed out\n",
+			   tx->channel_number);
+		return ret;
+	}
+
+	/* enable fifo */
+	lan743x_csr_write(adapter, FCT_TX_CTL,
+			  FCT_TX_CTL_EN_(tx->channel_number));
+	return 0;
+}
+
+static int lan743x_tx_fifo_disable(struct lan743x_tx *tx)
+{
+	struct lan743x_adapter *adapter = tx->adapter;
+	int ret;
+
+	lan743x_csr_write(adapter, FCT_TX_CTL,
+			  FCT_TX_CTL_DIS_(tx->channel_number));
+	ret = lan743x_csr_wait_for_bit(adapter, FCT_TX_CTL,
+				       FCT_TX_CTL_EN_(tx->channel_number),
+				       0, 1000, 20000, 100);
+	if (ret)
+		netdev_err(adapter->netdev,
+			   "TX channel %d FIFO disable timed out\n",
+			   tx->channel_number);
+	return ret;
+}
+
 static void lan743x_tx_close(struct lan743x_tx *tx)
 {
 	struct lan743x_adapter *adapter = tx->adapter;
+	bool stop_failed = false;
+	int ret;
 
 	lan743x_csr_write(adapter,
 			  DMAC_CMD,
 			  DMAC_CMD_STOP_T_(tx->channel_number));
-	lan743x_dmac_tx_wait_till_stopped(adapter, tx->channel_number);
+	ret = lan743x_dmac_tx_wait_till_stopped(adapter, tx->channel_number);
+	if (ret < 0 || ret == DMAC_CHANNEL_STATE_STARTED) {
+		stop_failed = true;
+		netdev_warn(adapter->netdev,
+			    "TX channel %d failed to stop, resetting it\n",
+			    tx->channel_number);
+	}
 
 	lan743x_csr_write(adapter,
 			  DMAC_INT_EN_CLR,
@@ -2235,11 +2314,9 @@ static void lan743x_tx_close(struct lan743x_tx *tx)
 	napi_disable(&tx->napi);
 	netif_napi_del(&tx->napi);
 
-	lan743x_csr_write(adapter, FCT_TX_CTL,
-			  FCT_TX_CTL_DIS_(tx->channel_number));
-	lan743x_csr_wait_for_bit(adapter, FCT_TX_CTL,
-				 FCT_TX_CTL_EN_(tx->channel_number),
-				 0, 1000, 20000, 100);
+	lan743x_tx_fifo_disable(tx);
+	if (stop_failed)
+		lan743x_dmac_tx_reset(adapter, tx->channel_number);
 
 	lan743x_tx_release_all_descriptors(tx);
 
@@ -2259,23 +2336,14 @@ static int lan743x_tx_open(struct lan743x_tx *tx)
 	if (ret)
 		return ret;
 
-	/* initialize fifo */
-	lan743x_csr_write(adapter, FCT_TX_CTL,
-			  FCT_TX_CTL_RESET_(tx->channel_number));
-	lan743x_csr_wait_for_bit(adapter, FCT_TX_CTL,
-				 FCT_TX_CTL_RESET_(tx->channel_number),
-				 0, 1000, 20000, 100);
-
-	/* enable fifo */
-	lan743x_csr_write(adapter, FCT_TX_CTL,
-			  FCT_TX_CTL_EN_(tx->channel_number));
+	ret = lan743x_tx_fifo_enable(tx);
+	if (ret)
+		goto ring_cleanup;
 
 	/* reset tx channel */
-	lan743x_csr_write(adapter, DMAC_CMD,
-			  DMAC_CMD_TX_SWR_(tx->channel_number));
-	lan743x_csr_wait_for_bit(adapter, DMAC_CMD,
-				 DMAC_CMD_TX_SWR_(tx->channel_number),
-				 0, 1000, 20000, 100);
+	ret = lan743x_dmac_tx_reset(adapter, tx->channel_number);
+	if (ret)
+		goto fifo_disable;
 
 	/* Write TX_BASE_ADDR */
 	lan743x_csr_write(adapter,
@@ -2347,6 +2415,13 @@ static int lan743x_tx_open(struct lan743x_tx *tx)
 	lan743x_csr_write(adapter, DMAC_CMD,
 			  DMAC_CMD_START_T_(tx->channel_number));
 	return 0;
+
+fifo_disable:
+	lan743x_tx_fifo_disable(tx);
+
+ring_cleanup:
+	lan743x_tx_ring_cleanup(tx);
+	return ret;
 }
 
 static int lan743x_rx_next_index(struct lan743x_rx *rx, int index)
@@ -2775,6 +2850,7 @@ static int lan743x_rx_ring_init(struct lan743x_rx *rx)
 static void lan743x_rx_close(struct lan743x_rx *rx)
 {
 	struct lan743x_adapter *adapter = rx->adapter;
+	int ret;
 
 	lan743x_csr_write(adapter, FCT_RX_CTL,
 			  FCT_RX_CTL_DIS_(rx->channel_number));
@@ -2784,7 +2860,13 @@ static void lan743x_rx_close(struct lan743x_rx *rx)
 
 	lan743x_csr_write(adapter, DMAC_CMD,
 			  DMAC_CMD_STOP_R_(rx->channel_number));
-	lan743x_dmac_rx_wait_till_stopped(adapter, rx->channel_number);
+	ret = lan743x_dmac_rx_wait_till_stopped(adapter, rx->channel_number);
+	if (ret < 0 || ret == DMAC_CHANNEL_STATE_STARTED) {
+		netdev_warn(adapter->netdev,
+			    "RX channel %d failed to stop, resetting it\n",
+			    rx->channel_number);
+		lan743x_dmac_rx_reset(adapter, rx->channel_number);
+	}
 
 	lan743x_csr_write(adapter, DMAC_INT_EN_CLR,
 			  DMAC_INT_BIT_RXFRM_(rx->channel_number));
@@ -2810,11 +2892,9 @@ static int lan743x_rx_open(struct lan743x_rx *rx)
 
 	netif_napi_add(adapter->netdev, &rx->napi, lan743x_rx_napi_poll);
 
-	lan743x_csr_write(adapter, DMAC_CMD,
-			  DMAC_CMD_RX_SWR_(rx->channel_number));
-	lan743x_csr_wait_for_bit(adapter, DMAC_CMD,
-				 DMAC_CMD_RX_SWR_(rx->channel_number),
-				 0, 1000, 20000, 100);
+	ret = lan743x_dmac_rx_reset(adapter, rx->channel_number);
+	if (ret)
+		goto napi_delete;
 
 	/* set ring base address */
 	lan743x_csr_write(adapter,
Signature · 1 line
2.34.1
#3 lore raw

PATCH v2 2/7 net: lan743x: quiesce TX before freeing the rings

In reply to Chris Babroski

lan743x_pcidev_shutdown(), which suspend also uses, calls netif_device_detach() and then lan743x_netdev_close() with the qdisc still attached. Detaching only sets the queue stop bits. It does not wait for an ndo_start_xmit() that is already running, and the TX NAPI poll still wakes a stopped queue as soon as it has reclaimed descriptors. The TX NAPI of a channel that has not been closed yet can therefore restart its queue, and the stack then transmits on a ring that lan743x_tx_close() is freeing.

Only wake a queue from TX NAPI while the device is present. In lan743x_netdev_close(), wait for any NAPI poll that may have missed the detach with synchronize_net(), then stop all queues with netif_tx_disable(), which also waits for transmits in progress, before closing the channels. For ndo_stop the qdisc is already deactivated, so this only adds a grace period there.

Fixes 23f0703c125b ("lan743x: Add main source files for new lan743x driver")
---
 drivers/net/ethernet/microchip/lan743x_main.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/net/ethernet/microchip/lan743x_main.c +5 -1
index 92cdc13495fe..57d52930f89b 100644
@@ -2137,7 +2137,8 @@ static int lan743x_tx_napi_poll(struct napi_struct *napi, int weight)
 	/* clean up tx ring */
 	lan743x_tx_release_completed_descriptors(tx);
 	txq = netdev_get_tx_queue(adapter->netdev, tx->channel_number);
-	if (netif_tx_queue_stopped(txq)) {
+	if (netif_tx_queue_stopped(txq) &&
+	    netif_device_present(adapter->netdev)) {
 		if (tx->rqd_descriptors) {
 			if (tx->rqd_descriptors <=
 			    lan743x_tx_get_avail_desc(tx)) {
@@ -3374,6 +3375,9 @@ static int lan743x_netdev_close(struct net_device *netdev)
 	struct lan743x_adapter *adapter = netdev_priv(netdev);
 	int index;
 
+	synchronize_net();
+	netif_tx_disable(netdev);
+
 	for (index = 0; index < adapter->used_tx_channels; index++)
 		lan743x_tx_close(&adapter->tx[index]);
 
Signature · 1 line
2.34.1
#8 lore raw

PATCH v2 5/7 net: lan743x: handle a failed reopen on resume

In reply to Chris Babroski

lan743x_pm_resume() ignores the return value of lan743x_netdev_open(). If the reopen fails, its error path has already closed the channels and freed the rings, but netif_device_attach() still wakes the TX queues. A later ndo_stop then runs lan743x_netdev_close() a second time, and napi_disable() on a NAPI that is already disabled never returns, with rtnl held.

If the reopen fails, take the interface down with dev_close() before attaching the device again, so it can be brought back up later. The hardware is already closed at that point, so route ndo_stop through a wrapper that skips the hardware close while the device is detached.

If lan743x_hardware_init() fails on resume, the device is left detached with the hardware already closed by suspend. Skip the close in lan743x_pcidev_shutdown() in that case as well, so a later suspend or shutdown does not close it a second time. Also stop releasing the PCI resources on that error path. They are taken in probe and released in remove, which still runs later and would release them a second time.

While the device is detached, the core does not call ndo_set_rx_mode, so address list changes made in that window, such as IPv6 multicast joins, which do not take rtnl, never reach the RX filter. Program the filter again after the device is attached.

Fixes 4d94282afd95 ("lan743x: Add power management support")
Fixes d6423d2ec39c ("net: ethernet: microchip: lan743x: Fix driver crash when lan743x_pm_resume fails")
---
 drivers/net/ethernet/microchip/lan743x_main.c | 35 ++++++++++++++++---
 1 file changed, 30 insertions(+), 5 deletions(-)
drivers/net/ethernet/microchip/lan743x_main.c +30 -5
index b226078bf3a9..228b6ab539a1 100644
@@ -3477,6 +3477,14 @@ static int lan743x_netdev_open(struct net_device *netdev)
 	return ret;
 }
 
+static int lan743x_netdev_stop(struct net_device *netdev)
+{
+	if (!netif_device_present(netdev))
+		return 0;
+
+	return lan743x_netdev_close(netdev);
+}
+
 static netdev_tx_t lan743x_netdev_xmit_frame(struct sk_buff *skb,
 					     struct net_device *netdev)
 {
@@ -3583,7 +3591,7 @@ static int lan743x_netdev_set_mac_address(struct net_device *netdev,
 
 static const struct net_device_ops lan743x_netdev_ops = {
 	.ndo_open		= lan743x_netdev_open,
-	.ndo_stop		= lan743x_netdev_close,
+	.ndo_stop		= lan743x_netdev_stop,
 	.ndo_start_xmit		= lan743x_netdev_xmit_frame,
 	.ndo_eth_ioctl		= lan743x_netdev_ioctl,
 	.ndo_set_rx_mode	= lan743x_netdev_set_multicast,
@@ -3885,15 +3893,20 @@ static void lan743x_pcidev_shutdown(struct pci_dev *pdev)
 {
 	struct net_device *netdev = pci_get_drvdata(pdev);
 	struct lan743x_adapter *adapter = netdev_priv(netdev);
+	bool present;
 
 	rtnl_lock();
+	present = netif_device_present(netdev);
 	netif_device_detach(netdev);
 
 	/* close netdev when netdev is at running state.
 	 * For instance, it is true when system goes to sleep by pm-suspend
 	 * However, it is false when system goes to sleep by suspend GUI menu
+	 *
+	 * If the device is not present then netdev has already been closed.
+	 * This can be true if a previous resume failed.
 	 */
-	if (netif_running(netdev))
+	if (present && netif_running(netdev))
 		lan743x_netdev_close(netdev);
 	rtnl_unlock();
 
@@ -4076,7 +4089,6 @@ static int lan743x_pm_resume(struct device *dev)
 	if (ret) {
 		netif_err(adapter, probe, adapter->netdev,
 			  "lan743x_hardware_init returned %d\n", ret);
-		lan743x_pci_cleanup(adapter);
 		return ret;
 	}
 
@@ -4109,10 +4121,23 @@ static int lan743x_pm_resume(struct device *dev)
 	 * For instance, it is true when system wakesup after pm-suspend
 	 * However, it is false when system wakes up after suspend GUI menu
 	 */
-	if (netif_running(netdev))
-		lan743x_netdev_open(netdev);
+	if (netif_running(netdev)) {
+		ret = lan743x_netdev_open(netdev);
+		if (ret) {
+			netdev_err(netdev, "failed to reopen after resume: %d\n",
+				   ret);
+			dev_close(netdev);
+		}
+	}
 
 	netif_device_attach(netdev);
+
+	/* ndo_set_rx_mode is skipped while the device is detached */
+	if (netif_running(netdev)) {
+		netif_addr_lock_bh(netdev);
+		lan743x_rfe_set_multicast(adapter);
+		netif_addr_unlock_bh(netdev);
+	}
 	rtnl_unlock();
 
 	return 0;
Signature · 1 line
2.34.1
#10 lore raw

PATCH v2 6/7 net: lan743x: reset the device on TX timeout

In reply to Chris Babroski

The driver does not implement ndo_tx_timeout, so the core TX watchdog is never started. If a TX DMA channel stops completing descriptors, its queue stays stopped and the interface cannot transmit again until it is brought down and back up.

Add an ndo_tx_timeout handler that schedules a work item to reset the device. Under rtnl_lock(), the work detaches the netdev, closes it, soft-resets the DMA controller, opens it again and reattaches it. This is the sequence suspend and resume already use, with the DMA controller reset taking the place of the full hardware init.

Because everything is reopened, the RX path, PTP and the PHY link are restarted along with TX. For PTP, the reset has the same effect as bringing the interface down and up. As on suspend and resume, the PTP clock is unregistered and registered again, and its time is reset to the system time. Applications that have the old clock open get -ENODEV from it, and the new clock can come up as a different /dev/ptpN, so PTP applications such as ptp4l have to be restarted. The hardware timestamping state is also reset, so the RX filter returns to none and one-step sync insertion is disabled, while the driver keeps its TX timestamping flags.

lan743x_dmac_init() did not check that the DMA controller soft reset completed, so the work could reopen a controller whose reset timed out. Return the error from the poll so the work can tell. Probe already fails when lan743x_dmac_init() returns an error, so a controller that does not come out of reset now also fails probe. On resume, it now leaves the device detached.

If lan743x_dmac_init() fails, the interface is taken down with dev_close() and the device is left detached, so it cannot be opened again on a DMA controller that is still in reset. A later resume resets the controller again and attaches the device. If the reopen fails, the interface is taken down the same way, but the device is attached again so it can be brought back up later. In both cases the device is still detached when dev_close() runs, so lan743x_netdev_stop() skips the hardware close, which has already been done.

As on resume, the RX filter is programmed again after the device is attached, since ndo_set_rx_mode is not called while it is detached.

ndo_stop is called with rtnl held and cannot wait for the work, so it only cancels a pending one. A work that is already waiting for rtnl returns if the device is still down when it gets the lock. If the device was opened again in the meantime, it is reset once more, which only costs an extra link flap. On remove, the work is cancelled synchronously after the netdev is unregistered.

---
 drivers/net/ethernet/microchip/lan743x_main.c | 68 ++++++++++++++++++-
 drivers/net/ethernet/microchip/lan743x_main.h |  2 +
 2 files changed, 68 insertions(+), 2 deletions(-)
drivers/net/ethernet/microchip/lan743x_main.c +66 -2
index 228b6ab539a1..3f129a22921e 100644
@@ -1516,10 +1516,14 @@ static void lan743x_rfe_set_multicast(struct lan743x_adapter *adapter)
 static int lan743x_dmac_init(struct lan743x_adapter *adapter)
 {
 	u32 data = 0;
+	int ret;
 
 	lan743x_csr_write(adapter, DMAC_CMD, DMAC_CMD_SWR_);
-	lan743x_csr_wait_for_bit(adapter, DMAC_CMD, DMAC_CMD_SWR_,
-				 0, 1000, 20000, 100);
+	ret = lan743x_csr_wait_for_bit(adapter, DMAC_CMD, DMAC_CMD_SWR_,
+				       0, 1000, 20000, 100);
+	if (ret)
+		return ret;
+
 	switch (DEFAULT_DMA_DESCRIPTOR_SPACING) {
 	case DMA_DESCRIPTOR_SPACING_16:
 		data = DMAC_CFG_MAX_DSPACE_16_;
@@ -3381,6 +3385,12 @@ static int lan743x_netdev_close(struct net_device *netdev)
 	struct lan743x_adapter *adapter = netdev_priv(netdev);
 	int index;
 
+	/* lan743x_reset_work() takes rtnl, so it cannot be waited for here.
+	 * A running one returns if the device is still down when it gets
+	 * rtnl, otherwise it resets the reopened device once more.
+	 */
+	cancel_work(&adapter->reset_work);
+
 	lan743x_phylink_disconnect(adapter);
 
 	synchronize_net();
@@ -3485,6 +3495,57 @@ static int lan743x_netdev_stop(struct net_device *netdev)
 	return lan743x_netdev_close(netdev);
 }
 
+static void lan743x_reset_work(struct work_struct *work)
+{
+	struct lan743x_adapter *adapter =
+		container_of(work, struct lan743x_adapter, reset_work);
+	struct net_device *netdev = adapter->netdev;
+	int ret;
+
+	rtnl_lock();
+
+	if (!netif_running(netdev) || !netif_device_present(netdev))
+		goto unlock;
+
+	netdev_warn(netdev, "resetting after TX timeout\n");
+
+	netif_device_detach(netdev);
+	lan743x_netdev_close(netdev);
+
+	ret = lan743x_dmac_init(adapter);
+	if (ret) {
+		/* Stay detached to prevent open on a DMAC that failed reset */
+		netdev_err(netdev, "DMA controller reset failed: %d\n", ret);
+		dev_close(netdev);
+		goto unlock;
+	}
+
+	ret = lan743x_netdev_open(netdev);
+	if (ret) {
+		netdev_err(netdev, "failed to reopen after reset: %d\n", ret);
+		dev_close(netdev);
+	}
+
+	netif_device_attach(netdev);
+
+	/* ndo_set_rx_mode is skipped while the device is detached */
+	if (netif_running(netdev)) {
+		netif_addr_lock_bh(netdev);
+		lan743x_rfe_set_multicast(adapter);
+		netif_addr_unlock_bh(netdev);
+	}
+unlock:
+	rtnl_unlock();
+}
+
+static void lan743x_netdev_tx_timeout(struct net_device *netdev,
+				      unsigned int txqueue)
+{
+	struct lan743x_adapter *adapter = netdev_priv(netdev);
+
+	schedule_work(&adapter->reset_work);
+}
+
 static netdev_tx_t lan743x_netdev_xmit_frame(struct sk_buff *skb,
 					     struct net_device *netdev)
 {
@@ -3593,6 +3654,7 @@ static const struct net_device_ops lan743x_netdev_ops = {
 	.ndo_open		= lan743x_netdev_open,
 	.ndo_stop		= lan743x_netdev_stop,
 	.ndo_start_xmit		= lan743x_netdev_xmit_frame,
+	.ndo_tx_timeout		= lan743x_netdev_tx_timeout,
 	.ndo_eth_ioctl		= lan743x_netdev_ioctl,
 	.ndo_set_rx_mode	= lan743x_netdev_set_multicast,
 	.ndo_change_mtu		= lan743x_netdev_change_mtu,
@@ -3621,6 +3683,7 @@ static void lan743x_destroy_phylink(struct lan743x_adapter *adapter)
 static void lan743x_full_cleanup(struct lan743x_adapter *adapter)
 {
 	unregister_netdev(adapter->netdev);
+	cancel_work_sync(&adapter->reset_work);
 
 	lan743x_destroy_phylink(adapter);
 	lan743x_mdiobus_cleanup(adapter);
@@ -3815,6 +3878,7 @@ static int lan743x_pcidev_probe(struct pci_dev *pdev,
 			      NETIF_MSG_LINK | NETIF_MSG_IFUP |
 			      NETIF_MSG_IFDOWN | NETIF_MSG_TX_QUEUED;
 	netdev->max_mtu = LAN743X_MAX_FRAME_SIZE;
+	INIT_WORK(&adapter->reset_work, lan743x_reset_work);
 
 	of_get_mac_address(pdev->dev.of_node, adapter->mac_address);
 
drivers/net/ethernet/microchip/lan743x_main.h +2 -0
index d9495cf96b41..2697f0a2a83b 100644
@@ -6,6 +6,7 @@
 
 #include <linux/phy.h>
 #include <linux/phylink.h>
+#include <linux/workqueue.h>
 #include "lan743x_ptp.h"
 
 #define DRIVER_AUTHOR   "Bryan Whitehead <[email protected]>"
@@ -1075,6 +1076,7 @@ struct lan743x_adapter {
 	struct lan743x_phy      phy;
 	struct lan743x_tx       tx[PCI11X1X_USED_TX_CHANNELS];
 	struct lan743x_rx       rx[LAN743X_USED_RX_CHANNELS];
+	struct work_struct	reset_work;
 	bool			is_pci11x1x;
 	bool			is_sgmii_en;
 	bool			is_rmii_en;
Signature · 1 line
2.34.1
#6 lore raw

PATCH v2 4/7 net: lan743x: keep the fixed link across reopen

In reply to Chris Babroski

On LAN7431 and PCI11x1x boards with no DT node and no PHY on the MDIO bus, lan743x_phylink_connect() falls back to a fixed link with phylink_set_fixed_link(). That switches phylink to fixed-link mode, which neither phylink_stop() nor phylink_disconnect_phy() undoes. phylink_set_fixed_link() only accepts a phylink that is still in PHY mode, so it returns -EINVAL on every later open and the open fails. The interface cannot be brought up again once it has been taken down, and the reopen on resume fails the same way.

If phylink no longer expects a PHY, the fixed link from the earlier open is still configured, so only start phylink.

Fixes a5f199a8d8a0 ("net: lan743x: Migrate phylib to phylink")
---
 drivers/net/ethernet/microchip/lan743x_main.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/net/ethernet/microchip/lan743x_main.c +6 -0
index 8eb0645493e6..b226078bf3a9 100644
@@ -3318,6 +3318,12 @@ static int lan743x_phylink_connect(struct lan743x_adapter *adapter)
 	struct phy_device *phydev;
 	int ret;
 
+	/* A fixed link set up by an earlier open is still in place */
+	if (!phylink_expects_phy(adapter->phylink)) {
+		phylink_start(adapter->phylink);
+		return 0;
+	}
+
 	if (dn)
 		ret = phylink_of_phy_connect(adapter->phylink, dn, 0);
 
Signature · 1 line
2.34.1
#4 lore raw

PATCH v2 3/7 net: lan743x: start phylink last and stop it first

In reply to Chris Babroski

lan743x_netdev_open() starts phylink before the RX and TX rings exist, and lan743x_netdev_close() only stops it after the rings are freed. The mac_link_up() callback wakes all TX queues, so it can run while there is no TX ring to transmit on.

For ndo_open and ndo_stop the qdisc is not active at that point, but the suspend and resume paths call lan743x_netdev_close() and lan743x_netdev_open() directly with the qdisc still attached. A link up event in either window lets the stack call ndo_start_xmit() on a channel whose ring is not allocated yet or has just been freed.

The error path of lan743x_netdev_open() is also wrong: a lan743x_ptp_open() failure jumps past lan743x_phylink_disconnect(), leaving phylink started and the PHY attached after open has failed. Every later open then fails because the PHY is already attached.

Connect and start phylink as the last step of open and stop it as the first step of close, as other phylink users do. If phylink fails to connect, nothing is attached, so the error path only needs to unwind the rings, PTP, MAC and interrupts.

Fixes a5f199a8d8a0 ("net: lan743x: Migrate phylib to phylink")
---
 drivers/net/ethernet/microchip/lan743x_main.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/net/ethernet/microchip/lan743x_main.c +6 -8
index 57d52930f89b..8eb0645493e6 100644
@@ -3375,6 +3375,8 @@ static int lan743x_netdev_close(struct net_device *netdev)
 	struct lan743x_adapter *adapter = netdev_priv(netdev);
 	int index;
 
+	lan743x_phylink_disconnect(adapter);
+
 	synchronize_net();
 	netif_tx_disable(netdev);
 
@@ -3386,8 +3388,6 @@ static int lan743x_netdev_close(struct net_device *netdev)
 
 	lan743x_ptp_close(adapter);
 
-	lan743x_phylink_disconnect(adapter);
-
 	lan743x_mac_close(adapter);
 
 	lan743x_intr_close(adapter);
@@ -3409,10 +3409,6 @@ static int lan743x_netdev_open(struct net_device *netdev)
 	if (ret)
 		goto close_intr;
 
-	ret = lan743x_phylink_connect(adapter);
-	if (ret)
-		goto close_mac;
-
 	ret = lan743x_ptp_open(adapter);
 	if (ret)
 		goto close_mac;
@@ -3431,6 +3427,10 @@ static int lan743x_netdev_open(struct net_device *netdev)
 			goto close_tx;
 	}
 
+	ret = lan743x_phylink_connect(adapter);
+	if (ret)
+		goto close_tx;
+
 	if (netdev->phydev)
 		phy_support_eee(netdev->phydev);
 
@@ -3458,8 +3458,6 @@ static int lan743x_netdev_open(struct net_device *netdev)
 			lan743x_rx_close(&adapter->rx[index]);
 	}
 	lan743x_ptp_close(adapter);
-	if (adapter->phylink)
-		lan743x_phylink_disconnect(adapter);
 
 close_mac:
 	lan743x_mac_close(adapter);
Signature · 1 line
2.34.1
#12 lore raw

PATCH v2 7/7 net: lan743x: add byte queue limits support

In reply to Chris Babroski

Report the bytes queued to and completed by each TX ring to BQL.

Besides limiting how much data sits in the TX ring, this lets the stack stop a queue once the bytes in flight exceed the BQL limit. Without it, a queue is only stopped when its 128 entry ring is full, and the TX watchdog only looks at stopped queues. With light traffic it can take tens of seconds after a TX hang to fill the ring, before the watchdog timeout even starts counting.

lan743x_tx_close() frees pending descriptors without completing them, so reset the BQL state there.

---
 drivers/net/ethernet/microchip/lan743x_main.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
drivers/net/ethernet/microchip/lan743x_main.c +19 -0
index 3f129a22921e..29c868263d2f 100644
@@ -1720,10 +1720,23 @@ static int lan743x_tx_next_index(struct lan743x_tx *tx, int index)
 
 static void lan743x_tx_release_completed_descriptors(struct lan743x_tx *tx)
 {
+	struct lan743x_tx_buffer_info *buffer_info;
+	unsigned int bytes = 0;
+	unsigned int pkts = 0;
+
 	while (le32_to_cpu(*tx->head_cpu_ptr) != (tx->last_head)) {
+		buffer_info = &tx->buffer_info[tx->last_head];
+		if (buffer_info->skb) {
+			bytes += buffer_info->skb->len;
+			pkts++;
+		}
 		lan743x_tx_release_desc(tx, tx->last_head, false);
 		tx->last_head = lan743x_tx_next_index(tx, tx->last_head);
 	}
+
+	netdev_tx_completed_queue(netdev_get_tx_queue(tx->adapter->netdev,
+						      tx->channel_number),
+				  pkts, bytes);
 }
 
 static void lan743x_tx_release_all_descriptors(struct lan743x_tx *tx)
@@ -2015,6 +2028,10 @@ static void lan743x_tx_frame_end(struct lan743x_tx *tx,
 	if (ignore_sync)
 		buffer_info->flags |= TX_BUFFER_INFO_FLAG_IGNORE_SYNC;
 
+	netdev_tx_sent_queue(netdev_get_tx_queue(adapter->netdev,
+						 tx->channel_number),
+			     skb->len);
+
 	tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail];
 	tx_descriptor->data0 = cpu_to_le32(tx->frame_data0);
 	tx->frame_tail = lan743x_tx_next_index(tx, tx->frame_tail);
@@ -2324,6 +2341,8 @@ static void lan743x_tx_close(struct lan743x_tx *tx)
 		lan743x_dmac_tx_reset(adapter, tx->channel_number);
 
 	lan743x_tx_release_all_descriptors(tx);
+	netdev_tx_reset_queue(netdev_get_tx_queue(adapter->netdev,
+						  tx->channel_number));
 
 	tx->rqd_descriptors = 0;
 
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH net-next v2 4/7] net: lan743x: keep the fixed link across reopen

In reply to Chris Babroski
Quoted 4 lines · Chris Babroski

On LAN7431 and PCI11x1x boards with no DT node and no PHY on the MDIO bus, lan743x_phylink_connect() falls back to a fixed link with phylink_set_fixed_link(). That switches phylink to fixed-link mode, which neither phylink_stop() nor phylink_disconnect_phy() undoes.

I can understand your fix, but architecturally, it is wrong.

Anything which open() does, close() should undo.

Anything which lan743x_phylink_connect() does,
lan743x_phylink_disconnect() should undo.

So this might need you to add a phylink_unset_fixed_link() do undo what phylink_set_fixed_link() did.

Andrew

---
pw-bot: cr

#2 lore raw

Re: [PATCH net-next v2 1/7] net: lan743x: check DMA channel stop and reset results

In reply to Chris Babroski
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] The fix does not finish its own error path.
- [Low] In lan743x_tx_close(), the return value of the new lan743x_tx_fifo_disable() is discarded, and `if (stop_failed)…

--

net: lan743x: check DMA channel stop and reset results

This patch checks the DMA channel stop result in lan743x_tx_close() and lan743x_rx_close(). If the channel is still stop-pending or started, it issues a channel soft reset before the rings are freed. It also moves the channel and TX FIFO reset sequences into helpers, and lan743x_tx_open() and lan743x_rx_open() now fail when those resets time out.

Quoted 4 lines
drivers/net/ethernet/microchip/lan743x_main.c +0 -0
index 82d3ec20ba259..92cdc13495fe9 100644

[ ... ]

Quoted 1 line
drivers/net/ethernet/microchip/lan743x_main.c +0 -0
@@ -2218,14 +2248,63 @@ static int lan743x_tx_ring_init(struct lan743x_tx *tx)

[ ... ]

Quoted 22 lines

static void lan743x_tx_close(struct lan743x_tx *tx) {

drivers/net/ethernet/microchip/lan743x_main.c +9 -1
 	struct lan743x_adapter *adapter = tx->adapter;
+	bool stop_failed = false;
+	int ret;
 
 	lan743x_csr_write(adapter,
 			  DMAC_CMD,
 			  DMAC_CMD_STOP_T_(tx->channel_number));
-	lan743x_dmac_tx_wait_till_stopped(adapter, tx->channel_number);
+	ret = lan743x_dmac_tx_wait_till_stopped(adapter, tx->channel_number);
+	if (ret < 0 || ret == DMAC_CHANNEL_STATE_STARTED) {
+		stop_failed = true;
+		netdev_warn(adapter->netdev,
+			    "TX channel %d failed to stop, resetting it\n",
+			    tx->channel_number);
+	}
 
 	lan743x_csr_write(adapter,
 			  DMAC_INT_EN_CLR,
@@ -2235,11 +2314,9 @@ static void lan743x_tx_close(struct lan743x_tx *tx)
 	napi_disable(&tx->napi);

drivers/net/ethernet/microchip/lan743x_main.c static void lan743x_tx_close(struct lan743x_tx *tx) · line 2319

Quoted 10 lines
drivers/net/ethernet/microchip/lan743x_main.c +3 -5
 	netif_napi_del(&tx->napi);
 
-	lan743x_csr_write(adapter, FCT_TX_CTL,
-			  FCT_TX_CTL_DIS_(tx->channel_number));
-	lan743x_csr_wait_for_bit(adapter, FCT_TX_CTL,
-				 FCT_TX_CTL_EN_(tx->channel_number),
-				 0, 1000, 20000, 100);
+	lan743x_tx_fifo_disable(tx);
+	if (stop_failed)
+		lan743x_dmac_tx_reset(adapter, tx->channel_number);

[Severity: Low]
The return value of lan743x_tx_fifo_disable() is dropped here. The channel SWR is issued based on stop_failed alone, even if FCT_TX_CTL_EN_ never cleared.

The commit message says:

the TX channel should only be reset once its FIFO is stopped.

This patch targets a wedged channel. On that path, the FIFO is probably the part most likely to also miss the 100 ms disable poll. Should the FIFO disable result be checked before DMAC_CMD_TX_SWR_ is written? Another option is a FIFO reset with FCT_TX_CTL_RESET_ at that point.

drivers/net/ethernet/microchip/lan743x_main.c static void lan743x_tx_close(struct lan743x_tx *tx) · line 2322

Quoted 3 lines
drivers/net/ethernet/microchip/lan743x_main.c +0 -0
 	lan743x_tx_release_all_descriptors(tx);
 

[Severity: High]
What happens here if lan743x_dmac_tx_reset() also times out?

The helper returns -ETIMEDOUT and logs "TX channel %d reset timed out". That return value is dropped, and lan743x_tx_close() continues into two calls:

- lan743x_tx_release_all_descriptors(), which unmaps and frees the TX skbs.
- lan743x_tx_ring_cleanup(), which calls dma_free_coherent() on the descriptor ring and the head write-back buffer.

The commit message says the reset is issued "before anything is freed", so that memory the channel "may still be fetching from or writing to" is not released. If a wedged channel can ignore STOP_T, can it also ignore SWR? The open path in this patch already treats an SWR timeout as a real failure.

lan743x_rx_close() follows the same pattern:

	ret = lan743x_dmac_rx_wait_till_stopped(adapter, rx->channel_number);
	if (ret < 0 || ret == DMAC_CHANNEL_STATE_STARTED) {
		...
		lan743x_dmac_rx_reset(adapter, rx->channel_number);
	}
	...
	lan743x_rx_ring_cleanup(rx);

On the RX side, an engine that is still running could write incoming frame data, descriptor status and the head index into memory that has already gone back to the allocator.

Older code makes this worse on the next open. The call chain lan743x_rx_ring_init()->lan743x_rx_init_ring_element()-> lan743x_rx_update_tail() writes RX_TAIL before lan743x_rx_open() resets the channel. At that point RX_BASE_ADDR still points at the ring that was freed.

This also looks unchanged after the later patch in the series, "net:
lan743x: reset the device on TX timeout". That patch runs lan743x_dmac_init() only after lan743x_netdev_close() has already freed the rings.

Could the close paths check the reset result and, if it failed, leak the DMA memory instead of freeing it? Escalating to a full DMAC or PCI reset would be another option.

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795027003.git.cbabroski%40nvidia.com
#5 lore raw

Re: [PATCH net-next v2 3/7] net: lan743x: start phylink last and stop it first

In reply to Chris Babroski
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] In lan743x_netdev_close() (drivers/net/ethernet/microchip/lan743x_main.c:3378), the patch now…
- [Medium] lan743x_netdev_open() now calls lan743x_phylink_connect() (drivers/net/ethernet/microchip/lan743x_main.c:3430) after all RX and…

--

net: lan743x: start phylink last and stop it first

This patch moves lan743x_phylink_connect() to the end of lan743x_netdev_open() and lan743x_phylink_disconnect() to the start of lan743x_netdev_close(). The goal is that mac_link_up() can no longer wake the TX queues while the rings are missing. The phylink disconnect is also dropped from the open error path, because phylink is now connected only after everything else has succeeded.

drivers/net/ethernet/microchip/lan743x_main.c static int lan743x_netdev_close(struct net_device *netdev) · line 3382

Quoted 13 lines
drivers/net/ethernet/microchip/lan743x_main.c +2 -0
index 57d52930f89be..8eb0645493e63 100644
@@ -3375,6 +3375,8 @@ static int lan743x_netdev_close(struct net_device *netdev)
 	struct lan743x_adapter *adapter = netdev_priv(netdev);
 	int index;
 
+	lan743x_phylink_disconnect(adapter);
+
 	synchronize_net();
 	netif_tx_disable(netdev);
 

[Severity: Medium]
Does the whole of lan743x_phylink_disconnect() need to move ahead of the ring teardown, or only phylink_stop()?

lan743x_phylink_disconnect() does both:

    phylink_stop(adapter->phylink);
    phylink_disconnect_phy(adapter->phylink);

phylink_disconnect_phy() ends up in phy_detach_internal(), which calls phy_suspend(phydev) and later phy_device_reset(phydev, 1).

As a result, the PHY is now powered down before lan743x_tx_close() and lan743x_rx_close() run their DMAC_CMD_STOP_T_ / DMAC_CMD_STOP_R_ and FCT_TX_CTL / FCT_RX_CTL disable handshakes. If a reset GPIO exists, the PHY is also held in reset at that point. This happens on ndo_stop, on the suspend path through lan743x_pcidev_shutdown(), and on the TX timeout reset later in the series.

Suppose the FCT or DMA teardown depends on a PHY-sourced clock, such as RGMII RXC, MII TX_CLK/RX_CLK, or a PHY-supplied RMII REF_CLK. Could the RX FIFO then still be half-disabled when the ring is freed? The result of this poll in lan743x_rx_close() is ignored:

    lan743x_csr_write(adapter, FCT_RX_CTL,
                      FCT_RX_CTL_DIS_(rx->channel_number));
    lan743x_csr_wait_for_bit(adapter, FCT_RX_CTL,
                             FCT_RX_CTL_EN_(rx->channel_number),
                             0, 1000, 20000, 100);

lan743x_tx_close() could also take the "failed to stop, resetting it" path, and lan743x_tx_fifo_disable() could time out.

I couldn't confirm from the driver code or a datasheet that the FCT and DMA teardown needs the PHY clock. Also, lan743x_mac_close() already ran after the PHY detach before this patch.

Keeping mac_link_up() from waking the queues seems to need only phylink_stop() first. Could phylink_disconnect_phy() stay after the ring and FIFO teardown? __stmmac_release() uses that order: it calls phylink_stop(), stops DMA and frees the descriptors, and only then calls phylink_disconnect_phy().

The ordering here is still the same at the end of the series.

[ ... ]

Quoted 12 lines
drivers/net/ethernet/microchip/lan743x_main.c +0 -4
@@ -3409,10 +3409,6 @@ static int lan743x_netdev_open(struct net_device *netdev)
 	if (ret)
 		goto close_intr;
 
-	ret = lan743x_phylink_connect(adapter);
-	if (ret)
-		goto close_mac;
-
 	ret = lan743x_ptp_open(adapter);
 	if (ret)
 		goto close_mac;

drivers/net/ethernet/microchip/lan743x_main.c static int lan743x_netdev_open(struct net_device *netdev) · line 3433

Quoted 7 lines
drivers/net/ethernet/microchip/lan743x_main.c +4 -0
@@ -3431,6 +3427,10 @@ static int lan743x_netdev_open(struct net_device *netdev)
 			goto close_tx;
 	}
 
+	ret = lan743x_phylink_connect(adapter);
+	if (ret)
+		goto close_tx;
+

[Severity: Medium]
On a reopen, the PHY is still suspended from phy_detach_internal() in the previous close. It is only resumed through phy_attach_direct() and phy_resume() inside lan743x_phylink_connect(). Before this patch, the PHY was attached and resumed right after lan743x_mac_open(), before any FIFO or DMA reset.

With connect moved here, the FCT resets and the DMA channel resets (lan743x_dmac_tx_reset() and lan743x_dmac_rx_reset()) in lan743x_rx_open() and lan743x_tx_open() now run while the PHY is powered down.

If the FCT resets need the PHY-sourced MAC clock, can this make the open fail? lan743x_tx_fifo_enable() returns an error on timeout:

    lan743x_csr_write(adapter, FCT_TX_CTL,
                      FCT_TX_CTL_RESET_(tx->channel_number));
    ret = lan743x_csr_wait_for_bit(adapter, FCT_TX_CTL,
                                   FCT_TX_CTL_RESET_(tx->channel_number),
                                   0, 1000, 20000, 100);
    if (ret) {
        netdev_err(adapter->netdev,
                   "TX channel %d FIFO reset timed out\n",
                   tx->channel_number);
        return ret;
    }

If that times out, the interface would not come back up after its first ifdown or suspend. The FCT_RX_CTL_RESET_ wait in lan743x_rx_open() ignores its result. In that case FCT_FLOW would be programmed and the RX FIFO enabled even though the reset never finished.

A second effect is that phylink_start() now calls mac_config() after the FIFOs and DMA are already running. For SGMII, 1000BASE-X and 2500BASE-X, lan743x_phylink_mac_config() calls lan743x_serdes_clock_and_aneg_update(), which switches the SerDes MPLL, and then lan743x_pcs_power_reset(). Is it safe to do that on every open while DMA is active? phylink_resolve() can already call mac_config() at runtime when the PHY changes interface, so this part is less certain.

As on the close side, I couldn't confirm the clock dependency from the driver code. The DMAC global reset in lan743x_dmac_init() also already runs on resume with the PHY detached.

Would splitting the helper avoid this? The PHY could be attached early as before, with only phylink_start() called last. mac_link_up() would still not run before the rings exist.

This ordering is also unchanged at the end of the series.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795027003.git.cbabroski%40nvidia.com
#9 lore raw

Re: [PATCH net-next v2 5/7] net: lan743x: handle a failed reopen on resume

In reply to Chris Babroski
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 1 (1)

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The RX-filter resync added to lan743x_pm_resume() after netif_device_attach()…
- [Low] This is a patch-structure concern: one commit bundles three changes under the subject "handle a failed reopen on resume".

Pre-existing issues:
- [Low] lan743x_rfe_set_multicast() writes and validates only perfect- filter slots 1..min(mc_count,32).

--

net: lan743x: handle a failed reopen on resume

This patch makes lan743x_pm_resume() check the return value of lan743x_netdev_open(). On failure it calls dev_close(), and a new ndo_stop wrapper skips the hardware close while the device is detached. It also skips the close in lan743x_pcidev_shutdown() when the device is already detached, drops lan743x_pci_cleanup() from the lan743x_hardware_init() failure path, and reprograms the RX filter after netif_device_attach().

Quoted 4 lines
drivers/net/ethernet/microchip/lan743x_main.c +0 -0
index b226078bf3a96..228b6ab539a17 100644

[ ... ]

drivers/net/ethernet/microchip/lan743x_main.c static int lan743x_pm_resume(struct device *dev) · line 4093

Quoted 7 lines
drivers/net/ethernet/microchip/lan743x_main.c +0 -1
@@ -4076,7 +4089,6 @@ static int lan743x_pm_resume(struct device *dev)
 	if (ret) {
 		netif_err(adapter, probe, adapter->netdev,
 			  "lan743x_hardware_init returned %d\n", ret);
-		lan743x_pci_cleanup(adapter);
 		return ret;
 	}

[Severity: Low]

This isn't a bug, but the subject only covers part of what this commit does. It contains three separate changes:

- the reopen failure handling (dev_close() and lan743x_netdev_stop())
- the lan743x_hardware_init() failure path (the present check in lan743x_pcidev_shutdown(), and dropping the lan743x_pci_cleanup() call that d6423d2ec39c added)
- the RX filter reprogram in lan743x_pm_resume()

The third change runs on every successful resume, not only after a failed one. RFE_CTL is written only in lan743x_rfe_set_multicast(). lan743x_rfe_open() writes only RFE_RSS_CFG. So the reprogram also restores RFE_CTL and the multicast filter on every normal resume.

Would it be easier to review and backport these as separate patches?

[ ... ]

Quoted 13 lines
drivers/net/ethernet/microchip/lan743x_main.c +7 -2
@@ -4109,10 +4121,23 @@ static int lan743x_pm_resume(struct device *dev)
 	 * For instance, it is true when system wakesup after pm-suspend
 	 * However, it is false when system wakes up after suspend GUI menu
 	 */
-	if (netif_running(netdev))
-		lan743x_netdev_open(netdev);
+	if (netif_running(netdev)) {
+		ret = lan743x_netdev_open(netdev);
+		if (ret) {
+			netdev_err(netdev, "failed to reopen after resume: %d\n",
+				   ret);
+			dev_close(netdev);
+		}

drivers/net/ethernet/microchip/lan743x_main.c static int lan743x_pm_resume(struct device *dev) · line 4140

Quoted 10 lines
drivers/net/ethernet/microchip/lan743x_main.c +8 -0
+	}
 
 	netif_device_attach(netdev);
+
+	/* ndo_set_rx_mode is skipped while the device is detached */
+	if (netif_running(netdev)) {
+		netif_addr_lock_bh(netdev);
+		lan743x_rfe_set_multicast(adapter);
+		netif_addr_unlock_bh(netdev);
+	}

[Severity: Medium]

Does this resync cover unicast address changes made while the device was detached?

__dev_set_rx_mode() returns early when the device is not present. That return comes before the unicast promisc accounting, not only before ndo_set_rx_mode():

net/core/dev_addr_lists.c:__dev_set_rx_mode() {
    ...
	if (!netif_device_present(dev))
		return;
    ...
	promisc_inc = netif_uc_promisc_update(dev);
	if (promisc_inc)
		__dev_set_promiscuity(dev, promisc_inc, false);

	if (ops->ndo_set_rx_mode)
		ops->ndo_set_rx_mode(dev);
}

lan743x does not set IFF_UNICAST_FLT, so it depends on the core setting IFF_PROMISC when dev->uc is non-empty. lan743x_rfe_set_multicast() only enables unicast acceptance through IFF_PROMISC:

	if (netdev->flags & IFF_PROMISC) {
		rfctl |= RFE_CTL_AM_ | RFE_CTL_AU_;

Consider this sequence:

- lan743x_hardware_init() fails on resume, so the device stays detached with IFF_UP set. This patch now handles that case.
- A macvlan, or a VLAN with its own MAC, is brought up on top.
- dev_uc_add() runs on the lower device, but __dev_set_rx_mode() returns early, so uc_promisc and IFF_PROMISC are never set.
- On the next successful resume, this resync calls lan743x_rfe_set_multicast() with IFF_PROMISC still clear.

Wouldn't frames for the new unicast address be dropped until a later rx-mode event runs the core path? __dev_set_rx_mode() and dev_set_rx_mode() are not exported, so the driver cannot call the core path directly.

The commit message says:

While the device is detached, the core does not call ndo_set_rx_mode, so address list changes made in that window, such as IPv6 multicast joins, which do not take rtnl, never reach the RX filter. Program the filter again after the device is attached.

The IPv6 multicast case is handled. For unicast list changes, though, the filter still seems to miss them after this patch.

At this commit lan743x_hardware_init() does not appear able to fail. The window opens with the later commit "net: lan743x: reset the device on TX timeout", which makes lan743x_dmac_init() return the SWR poll error. That commit's reset work copies the same multicast-only resync, so it does not close this gap.

[Severity: Low]

This is a pre-existing issue, but lan743x_rfe_set_multicast() writes perfect filter slots 1 through min(mc_count, 32) only. It never clears RFE_ADDR_FILT_HI_VALID_ in slots past the current list length:

			if (i < 33) {
				lan743x_csr_write(adapter,
						  RFE_ADDR_FILT_HI(i), 0);
				...
				data |= RFE_ADDR_FILT_HI_VALID_;
				lan743x_csr_write(adapter,
						  RFE_ADDR_FILT_HI(i), data);

If the multicast list shrinks from N to M > 0 entries, can slots M+1..N stay valid while RFE_CTL_DA_PERFECT_ is still set? The hardware would then keep accepting groups the stack has already left.

Nothing in lan743x_hardware_init(), lan743x_rfe_open() or lan743x_mac_init() invalidates slots 1..32 either. The new resume resync relies on this helper, so it has the same behaviour.

Quoted 3 lines
drivers/net/ethernet/microchip/lan743x_main.c +0 -0
 	rtnl_unlock();
 
 	return 0;
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795027003.git.cbabroski%40nvidia.com
#11 lore raw

Re: [PATCH net-next v2 6/7] net: lan743x: reset the device on TX timeout

In reply to Chris Babroski
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] A single TX timeout can trigger two back-to-back device resets, because lan743x_reset_work() does not wait for a dev_watchdog() call…

--

net: lan743x: reset the device on TX timeout

This adds an ndo_tx_timeout handler to the lan743x driver. The handler schedules a work item that, under rtnl, detaches the netdev, closes it, soft-resets the DMA controller, reopens it and reattaches it. The patch also makes lan743x_dmac_init() return an error when the DMAC soft reset times out.

Quoted 4 lines
drivers/net/ethernet/microchip/lan743x_main.c +0 -0
index 228b6ab539a17..3f129a22921e1 100644

[ ... ]

Quoted 13 lines
drivers/net/ethernet/microchip/lan743x_main.c +6 -0
@@ -3381,6 +3385,12 @@ static int lan743x_netdev_close(struct net_device *netdev)
 	struct lan743x_adapter *adapter = netdev_priv(netdev);
 	int index;
 
+	/* lan743x_reset_work() takes rtnl, so it cannot be waited for here.
+	 * A running one returns if the device is still down when it gets
+	 * rtnl, otherwise it resets the reopened device once more.
+	 */
+	cancel_work(&adapter->reset_work);
+
 	lan743x_phylink_disconnect(adapter);
 
 	synchronize_net();

[ ... ]

Quoted 10 lines
drivers/net/ethernet/microchip/lan743x_main.c +6 -0
@@ -3485,6 +3495,57 @@ static int lan743x_netdev_stop(struct net_device *netdev)
 	return lan743x_netdev_close(netdev);
 }
 
+static void lan743x_reset_work(struct work_struct *work)
+{
+	struct lan743x_adapter *adapter =
+		container_of(work, struct lan743x_adapter, reset_work);
+	struct net_device *netdev = adapter->netdev;
+	int ret;

drivers/net/ethernet/microchip/lan743x_main.c static int lan743x_netdev_stop(struct net_device *netdev) · line 3513

Quoted 10 lines
drivers/net/ethernet/microchip/lan743x_main.c +10 -0
+
+	rtnl_lock();
+
+	if (!netif_running(netdev) || !netif_device_present(netdev))
+		goto unlock;
+
+	netdev_warn(netdev, "resetting after TX timeout\n");
+
+	netif_device_detach(netdev);
+	lan743x_netdev_close(netdev);

[Severity: Low]
Can one TX timeout cause two resets in a row here?

netif_device_detach() only clears __LINK_STATE_PRESENT. It does not take dev->tx_global_lock. dev_watchdog() holds that lock while it checks presence, scans the queues, prints a message and calls ndo_tx_timeout():

net/sched/sch_generic.c:dev_watchdog() {
	spin_lock(&dev->tx_global_lock);
	if (!qdisc_tx_is_noop(dev)) {
		if (netif_device_present(dev) &&
		    netif_running(dev) &&
		    netif_carrier_ok(dev)) {
		...
				netdev_crit(dev, "NETDEV WATCHDOG: CPU: %d: transmit queue %u timed out %u ms\n",
				...
				netif_freeze_queues(dev);
				dev->netdev_ops->ndo_tx_timeout(dev, i);
	...
}

This interleaving looks possible:

CPU0 (dev_watchdog) CPU1 (lan743x_reset_work)
spin_lock(tx_global_lock)
netif_device_present() is true
netif_device_detach() lan743x_netdev_close()
cancel_work() /* work is running,
not pending */
netdev_crit(...)
lan743x_netdev_tx_timeout()
schedule_work() /* requeues */
spin_unlock(tx_global_lock)
netif_tx_disable() /* waits on
tx_global_lock */

The netif_tx_disable() call in lan743x_netdev_close() is the first point that serializes against dev_watchdog() through tx_global_lock. It runs after cancel_work(), so nothing cancels the requeued work.

When the first reset calls netif_device_attach(), the requeued work sees netif_running() and netif_device_present() both true. It then runs the full detach/close/dmac_init/open/attach sequence a second time. That means a second link flap and a second PTP clock unregister and register, which can give the clock a new /dev/ptpN again.

The ndo_stop path does not have this window. There, __dev_close_many()->dev_deactivate_many()->netdev_watchdog_down() stops the watchdog before ndo_stop runs cancel_work(). The commit message only accepts an extra reset for the ndo_stop/reopen race.

Would moving cancel_work() after netif_tx_disable() in lan743x_netdev_close() close this window?

Quoted 3 lines
drivers/net/ethernet/microchip/lan743x_main.c +3 -0
+
+	ret = lan743x_dmac_init(adapter);
+	if (ret) {

[ ... ]

Quoted 7 lines
drivers/net/ethernet/microchip/lan743x_main.c +7 -0
+static void lan743x_netdev_tx_timeout(struct net_device *netdev,
+				      unsigned int txqueue)
+{
+	struct lan743x_adapter *adapter = netdev_priv(netdev);
+
+	schedule_work(&adapter->reset_work);
+}

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795027003.git.cbabroski%40nvidia.com