Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] net: stmmac: stmmac_init_rx_csum() checks dev->features
instead of wanted
--
commit 71e6607bc9a26db47d719c3ea93eefca52be2961
Author: James Hilliard <james.hilliard1@gmail.com>
net: stmmac: preserve RX checksum capability when offload is disabled
This commit passes the requested RX checksum offload state directly to the
MAC and masks NETIF_F_RXCSUM when enabling the hardware checker fails,
reconciling features on open and resume instead of discarding the detected
checksum capability.
> diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
> index a685ce4638f61..786eb089dc2b8 100644
> --- a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
> +++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
> @@ -3675,25 +3675,51 @@ static bool stmmac_tso_channel_permitted(struct stmmac_priv *priv,
> return !(priv->dma_conf.tx_queue[chan].tbs & STMMAC_TBS_AVAIL);
> }
>
> +static int stmmac_set_rx_csum(struct stmmac_priv *priv, unsigned int rx_csum)
> +{
> + int ret;
> +
> + if (!rx_csum) {
> + WRITE_ONCE(priv->hw->rx_csum, 0);
> + stmmac_rx_ipc(priv, priv->hw, false);
> + return 0;
> + }
> +
> + ret = stmmac_rx_ipc(priv, priv->hw, true);
> + if (ret <= 0) {
> + WRITE_ONCE(priv->hw->rx_csum, 0);
> + priv->rx_csum_failed = true;
> + return ret ?: -EIO;
> + }
> +
> + WRITE_ONCE(priv->hw->rx_csum, rx_csum);
> + return 0;
> +}
> +
> +/* Recheck the wanted features if this reset clears or uncovers a failure. */
> +static bool stmmac_init_rx_csum(struct net_device *dev)
> +{
> + struct stmmac_priv *priv = netdev_priv(dev);
> + bool was_failed = priv->rx_csum_failed;
> + unsigned int rx_csum;
> +
> + priv->rx_csum_failed = false;
> + rx_csum = dev->features & NETIF_F_RXCSUM ? priv->plat->rx_coe : 0;
> + if (stmmac_set_rx_csum(priv, rx_csum))
> + netdev_warn(dev, "RX IPC Checksum Offload disabled\n");
> +
> + return was_failed || priv->rx_csum_failed;
> +}
[Severity: Medium]
Should stmmac_init_rx_csum() test dev->wanted_features instead of
dev->features?
After a previous enable failure sets priv->rx_csum_failed, NETIF_F_RXCSUM
is cleared in dev->features while remaining set in dev->wanted_features.
On the next open or resume, checking dev->features gives rx_csum == 0, so
stmmac_init_rx_csum() clears priv->rx_csum_failed without retesting
stmmac_rx_ipc(priv, priv->hw, true) and returns true (was_failed). The
caller's netdev_update_features() then leaves NETIF_F_RXCSUM unmasked in
stmmac_fix_features() and invokes stmmac_set_features() with
NETIF_F_RXCSUM set, which fails again with -EIO and aborts
__netdev_update_features() before applying other feature updates.
@@ -699,7 +699,7 @@ static void sun8i_dwmac_get_umac_addr(struct mac_device_info *hw,
}
/* caution this function must return non 0 to work */
-static int sun8i_dwmac_rx_ipc_enable(struct mac_device_info *hw)
+static int sun8i_dwmac_rx_ipc_enable(struct mac_device_info *hw, bool enable)
{
void __iomem *ioaddr = hw->pcsr;
u32 v;
@@ -77,12 +77,12 @@ static void dwmac1000_irq_modify(struct mac_device_info *hw, u32 disable,
spin_unlock_irqrestore(&hw->irq_ctrl_lock, flags);
}
-static int dwmac1000_rx_ipc_enable(struct mac_device_info *hw)
+static int dwmac1000_rx_ipc_enable(struct mac_device_info *hw, bool enable)
{
void __iomem *ioaddr = hw->pcsr;
u32 value = readl(ioaddr + GMAC_CONTROL);
- if (hw->rx_csum)
+ if (enable)
value |= GMAC_CONTROL_IPC;
else
value &= ~GMAC_CONTROL_IPC;
@@ -48,7 +48,7 @@ static void dwmac100_dump_mac_regs(struct mac_device_info *hw, u32 *reg_space)
reg_space[MAC_VLAN2 / 4] = readl(ioaddr + MAC_VLAN2);
}
-static int dwmac100_rx_ipc_enable(struct mac_device_info *hw)
+static int dwmac100_rx_ipc_enable(struct mac_device_info *hw, bool enable)
{
return 0;
}
@@ -329,12 +329,12 @@ static void dwmac4_dump_regs(struct mac_device_info *hw, u32 *reg_space)
reg_space[i] = readl(ioaddr + i * 4);
}
-static int dwmac4_rx_ipc_enable(struct mac_device_info *hw)
+static int dwmac4_rx_ipc_enable(struct mac_device_info *hw, bool enable)
{
void __iomem *ioaddr = hw->pcsr;
u32 value = readl(ioaddr + GMAC_CONFIG);
- if (hw->rx_csum)
+ if (enable)
value |= GMAC_CONFIG_IPC;
else
value &= ~GMAC_CONFIG_IPC;
@@ -65,13 +65,13 @@ static void dwxgmac2_set_mac(void __iomem *ioaddr, bool enable)
writel(rx, ioaddr + XGMAC_RX_CONFIG);
}
-static int dwxgmac2_rx_ipc(struct mac_device_info *hw)
+static int dwxgmac2_rx_ipc(struct mac_device_info *hw, bool enable)
{
void __iomem *ioaddr = hw->pcsr;
u32 value;
value = readl(ioaddr + XGMAC_RX_CONFIG);
- if (hw->rx_csum)
+ if (enable)
value |= XGMAC_CONFIG_IPC;
else
value &= ~XGMAC_CONFIG_IPC;
@@ -319,8 +319,8 @@ struct stmmac_ops {
void (*irq_modify)(struct mac_device_info *hw, u32 disable, u32 enable);
/* Enable the MAC RX/TX */
void (*set_mac)(void __iomem *ioaddr, bool enable);
- /* Enable and verify that the IPC module is supported */
- int (*rx_ipc)(struct mac_device_info *hw);
+ /* Configure RX checksum offload and return whether the checker is enabled. */
+ int (*rx_ipc)(struct mac_device_info *hw, bool enable);
/* Enable RX Queues */
void (*rx_queue_enable)(struct mac_device_info *hw, u8 mode, u32 queue);
/* RX Queues Priority */
@@ -269,6 +269,7 @@ struct stmmac_priv {
bool tx_path_in_lpi_mode;
bool sph_active;
bool sph_capable;
+ bool rx_csum_failed;
u32 sarc_type;
u32 rx_riwt[MTL_MAX_RX_QUEUES];
u32 tx_threshold[MTL_MAX_TX_QUEUES];
@@ -3675,25 +3675,51 @@ static bool stmmac_tso_channel_permitted(struct stmmac_priv *priv,
return !(priv->dma_conf.tx_queue[chan].tbs & STMMAC_TBS_AVAIL);
}
+static int stmmac_set_rx_csum(struct stmmac_priv *priv, unsigned int rx_csum)
+{
+ int ret;
+
+ if (!rx_csum) {
+ WRITE_ONCE(priv->hw->rx_csum, 0);
+ stmmac_rx_ipc(priv, priv->hw, false);
+ return 0;
+ }
+
+ ret = stmmac_rx_ipc(priv, priv->hw, true);
+ if (ret <= 0) {
+ WRITE_ONCE(priv->hw->rx_csum, 0);
+ priv->rx_csum_failed = true;
+ return ret ?: -EIO;
+ }
+
+ WRITE_ONCE(priv->hw->rx_csum, rx_csum);
+ return 0;
+}
+
+/* Recheck the wanted features if this reset clears or uncovers a failure. */
+static bool stmmac_init_rx_csum(struct net_device *dev)
+{
+ struct stmmac_priv *priv = netdev_priv(dev);
+ bool was_failed = priv->rx_csum_failed;
+ unsigned int rx_csum;
+
+ priv->rx_csum_failed = false;
+ rx_csum = dev->features & NETIF_F_RXCSUM ? priv->plat->rx_coe : 0;
+ if (stmmac_set_rx_csum(priv, rx_csum))
+ netdev_warn(dev, "RX IPC Checksum Offload disabled\n");
+
+ return was_failed || priv->rx_csum_failed;
+}
+
/**
- * stmmac_hw_setup - setup mac in a usable state.
- * @dev : pointer to the device structure.
- * Description:
- * this is the main function to setup the HW in a usable state because the
- * dma engine is reset, the core registers are configured (e.g. AXI,
- * Checksum features, timers). The DMA is ready to start receiving and
- * transmitting.
- * Return value:
- * 0 on success and an appropriate (-)ve integer as defined in errno.h
- * file on failure.
+ * stmmac_hw_setup - reset and configure the MAC before starting RX/TX
+ * @dev: network device
+ *
+ * Return: 0 on success or a negative error on failure.
*/
static int stmmac_hw_setup(struct net_device *dev)
{
struct stmmac_priv *priv = netdev_priv(dev);
- u8 rx_cnt = priv->plat->rx_queues_to_use;
- u8 tx_cnt = priv->plat->tx_queues_to_use;
- bool sph_en;
- u8 chan;
int ret;
/* Make sure RX clock is enabled */
@@ -3738,12 +3764,16 @@ static int stmmac_hw_setup(struct net_device *dev)
/* Initialize Safety Features */
stmmac_safety_feat_configuration(priv);
- ret = stmmac_rx_ipc(priv, priv->hw);
- if (!ret) {
- netdev_warn(priv->dev, "RX IPC Checksum Offload disabled\n");
- priv->plat->rx_coe = STMMAC_RX_COE_NONE;
- priv->hw->rx_csum = 0;
- }
+ return 0;
+}
+
+static void stmmac_hw_start(struct net_device *dev)
+{
+ struct stmmac_priv *priv = netdev_priv(dev);
+ u8 rx_cnt = priv->plat->rx_queues_to_use;
+ u8 tx_cnt = priv->plat->tx_queues_to_use;
+ bool sph_en;
+ u8 chan;
/* Enable the MAC Rx/Tx */
stmmac_mac_set(priv, priv->ioaddr, true);
@@ -3806,8 +3836,6 @@ static int stmmac_hw_setup(struct net_device *dev)
phylink_rx_clk_stop_block(priv->phylink);
stmmac_set_hw_vlan_mode(priv, priv->hw);
phylink_rx_clk_stop_unblock(priv->phylink);
-
- return 0;
}
static void stmmac_free_irq(struct net_device *dev,
@@ -4216,6 +4244,9 @@ static int __stmmac_open(struct net_device *dev,
netdev_err(priv->dev, "%s: Hw setup failed\n", __func__);
return ret;
}
+ if (stmmac_init_rx_csum(dev))
+ netdev_update_features(dev);
+ stmmac_hw_start(dev);
ret = stmmac_setup_ptp(priv);
if (ret)
@@ -5233,7 +5264,7 @@ static unsigned int stmmac_rx_buf1_len(struct stmmac_priv *priv,
int status, unsigned int len)
{
unsigned int plen = 0, hlen = 0;
- int coe = priv->hw->rx_csum;
+ int coe = READ_ONCE(priv->hw->rx_csum);
/* Not first descriptor, SPH enabled: buffer1 only carries the
* split header of the first descriptor, so it is zero here.
@@ -5264,7 +5295,7 @@ static unsigned int stmmac_rx_buf2_len(struct stmmac_priv *priv,
struct dma_desc *p,
int status, unsigned int len)
{
- int coe = priv->hw->rx_csum;
+ int coe = READ_ONCE(priv->hw->rx_csum);
unsigned int plen = 0;
if (!stmmac_rx_check_buf2_cap(priv))
@@ -5569,7 +5600,7 @@ static void stmmac_dispatch_skb_zc(struct stmmac_priv *priv, u32 queue,
struct stmmac_channel *ch = &priv->channel[queue];
unsigned int len = xdp->data_end - xdp->data;
enum pkt_hash_types hash_type;
- int coe = priv->hw->rx_csum;
+ int coe = READ_ONCE(priv->hw->rx_csum);
struct sk_buff *skb;
u32 hash;
@@ -5872,7 +5903,7 @@ static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue)
unsigned int next_entry = rx_q->cur_rx;
bool in_progress = rx_q->state_saved;
enum dma_data_direction dma_dir;
- int coe = priv->hw->rx_csum;
+ int coe = READ_ONCE(priv->hw->rx_csum);
unsigned int desc_size;
struct sk_buff *skb = NULL;
struct stmmac_xdp_buff ctx;
@@ -6372,7 +6403,7 @@ static netdev_features_t stmmac_fix_features(struct net_device *dev,
{
struct stmmac_priv *priv = netdev_priv(dev);
- if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
+ if (!priv->plat->rx_coe || priv->rx_csum_failed)
features &= ~NETIF_F_RXCSUM;
if (!priv->plat->tx_coe)
@@ -6400,18 +6431,18 @@ static int stmmac_set_features(struct net_device *netdev,
netdev_features_t features)
{
struct stmmac_priv *priv = netdev_priv(netdev);
+ bool running = netif_running(netdev);
+ unsigned int rx_csum;
+ int ret;
- /* Keep the COE Type in case of csum is supporting */
- if (features & NETIF_F_RXCSUM)
- priv->hw->rx_csum = priv->plat->rx_coe;
- else
- priv->hw->rx_csum = 0;
- /* No check needed because rx_coe has been set before and it will be
- * fixed in case of issue.
- */
- stmmac_rx_ipc(priv, priv->hw);
+ rx_csum = features & NETIF_F_RXCSUM ? priv->plat->rx_coe : 0;
+ if (running && rx_csum != priv->hw->rx_csum) {
+ ret = stmmac_set_rx_csum(priv, rx_csum);
+ if (ret)
+ return ret;
+ }
- if (priv->sph_capable) {
+ if (running && priv->sph_capable) {
bool sph_en = (priv->hw->rx_csum > 0) && priv->sph_active;
u8 chan;
@@ -7650,10 +7681,9 @@ static int stmmac_hw_init(struct stmmac_priv *priv)
}
if (priv->plat->rx_coe) {
- priv->hw->rx_csum = priv->plat->rx_coe;
dev_info(priv->device, "RX Checksum Offload Engine supported\n");
if (priv->synopsys_id < DWMAC_CORE_4_00)
- dev_info(priv->device, "COE Type %d\n", priv->hw->rx_csum);
+ dev_info(priv->device, "COE Type %d\n", priv->plat->rx_coe);
}
if (priv->plat->tx_coe)
dev_info(priv->device, "TX Checksum insertion supported\n");
@@ -8575,6 +8605,12 @@ int stmmac_resume(struct device *dev)
netdev_err(priv->dev, "%s: Hw setup failed\n", __func__);
goto error_unlock;
}
+ if (stmmac_init_rx_csum(ndev)) {
+ mutex_unlock(&priv->lock);
+ netdev_update_features(ndev);
+ mutex_lock(&priv->lock);
+ }
+ stmmac_hw_start(ndev);
if (priv->ptp_enabled) {
if (stmmac_init_ptp_clk_freq(priv))