can: rockchip_canfd: implement workaround for erratum 6
authorMarc Kleine-Budde <mkl@pengutronix.de>
Fri, 24 Nov 2023 08:27:00 +0000 (09:27 +0100)
committerMarc Kleine-Budde <mkl@pengutronix.de>
Wed, 4 Sep 2024 12:41:52 +0000 (14:41 +0200)
The rk3568 CAN-FD errata sheet as of Tue 07 Nov 2023 11:25:31 +08:00
says:

| The CAN controller's transmission of extended frames may
| intermittently change into standard frames.
|
| When using the CAN controller to send extended frames, if the
| 'tx_req' is configured as 1 and coincides with the internal
| transmission point, the extended frame will be transmitted onto the
| bus in the format of a standard frame.

To work around Erratum 6, the driver is in self-receiving mode (RXSTX)
and all received CAN frames are passed through rkcanfd_rxstx_filter().

Add a check in rkcanfd_rxstx_filter() whether the received frame
corresponds to the current outgoing frame, but the extended CAN ID has
been mangled to a standard ID. In this case re-send the original CAN
frame.

Tested-by: Alibek Omarov <a1ba.omarov@gmail.com>
Acked-by: Heiko Stuebner <heiko@sntech.de>
Link: https://patch.msgid.link/20240904-rockchip-canfd-v5-12-8ae22bcb27cc@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
drivers/net/can/rockchip/rockchip_canfd-rx.c
drivers/net/can/rockchip/rockchip_canfd-tx.c
drivers/net/can/rockchip/rockchip_canfd.h

index 650dfd41e0a057dc301d2723e8a40433d5a99c89..31cee3362f1e445e42da3c728096ceca9f14cf43 100644 (file)
@@ -95,6 +95,7 @@ static int rkcanfd_rxstx_filter(struct rkcanfd_priv *priv,
                                const struct canfd_frame *cfd_rx, const u32 ts,
                                bool *tx_done)
 {
+       struct net_device_stats *stats = &priv->ndev->stats;
        const struct canfd_frame *cfd_nominal;
        const struct sk_buff *skb;
        unsigned int tx_tail;
@@ -130,6 +131,49 @@ static int rkcanfd_rxstx_filter(struct rkcanfd_priv *priv,
                return 0;
        }
 
+       if (!(priv->devtype_data.quirks & RKCANFD_QUIRK_RK3568_ERRATUM_6))
+               return 0;
+
+       /* Erratum 6: Extended frames may be send as standard frames.
+        *
+        * Not affected if:
+        * - TX'ed a standard frame -or-
+        * - RX'ed an extended frame
+        */
+       if (!(cfd_nominal->can_id & CAN_EFF_FLAG) ||
+           (cfd_rx->can_id & CAN_EFF_FLAG))
+               return 0;
+
+       /* Not affected if:
+        * - standard part and RTR flag of the TX'ed frame
+        *   is not equal the CAN-ID and RTR flag of the RX'ed frame.
+        */
+       if ((cfd_nominal->can_id & (CAN_RTR_FLAG | CAN_SFF_MASK)) !=
+           (cfd_rx->can_id & (CAN_RTR_FLAG | CAN_SFF_MASK)))
+               return 0;
+
+       /* Not affected if:
+        * - length is not the same
+        */
+       if (cfd_nominal->len != cfd_rx->len)
+               return 0;
+
+       /* Not affected if:
+        * - the data of non RTR frames is different
+        */
+       if (!(cfd_nominal->can_id & CAN_RTR_FLAG) &&
+           memcmp(cfd_nominal->data, cfd_rx->data, cfd_nominal->len))
+               return 0;
+
+       /* Affected by Erratum 6 */
+
+       *tx_done = true;
+
+       stats->tx_packets++;
+       stats->tx_errors++;
+
+       rkcanfd_xmit_retry(priv);
+
        return 0;
 }
 
index 668a902f4c2a4e0fa1b93af66f740ce92025a463..e98e7a836b838041ac5a91e68e36835d732c5a79 100644 (file)
@@ -14,6 +14,14 @@ static void rkcanfd_start_xmit_write_cmd(const struct rkcanfd_priv *priv,
        rkcanfd_write(priv, RKCANFD_REG_CMD, reg_cmd);
 }
 
+void rkcanfd_xmit_retry(struct rkcanfd_priv *priv)
+{
+       const unsigned int tx_head = rkcanfd_get_tx_head(priv);
+       const u32 reg_cmd = RKCANFD_REG_CMD_TX_REQ(tx_head);
+
+       rkcanfd_start_xmit_write_cmd(priv, reg_cmd);
+}
+
 int rkcanfd_start_xmit(struct sk_buff *skb, struct net_device *ndev)
 {
        struct rkcanfd_priv *priv = netdev_priv(ndev);
index a4688411e586ac36a4d333da1840482a101e8872..3fe6ddcdd8acd9597379a99bd4dc710a9c72a346 100644 (file)
 
 /* Erratum 6: The CAN controller's transmission of extended frames may
  * intermittently change into standard frames
+ *
+ * Work around this issue by activating self reception (RXSTX). If we
+ * have pending TX CAN frames, check all RX'ed CAN frames in
+ * rkcanfd_rxstx_filter().
+ *
+ * If it's a frame we've send and it's OK, call the TX complete
+ * handler: rkcanfd_handle_tx_done_one(). Mask the TX complete IRQ.
+ *
+ * If it's a frame we've send, but the CAN-ID is mangled, resend the
+ * original extended frame.
+ *
+ * To reproduce:
+ * host:
+ *   canfdtest -evx -g can0
+ *   candump any,0:80000000 -cexdtA
+ * dut:
+ *   canfdtest -evx can0
+ *   ethtool -S can0
  */
 #define RKCANFD_QUIRK_RK3568_ERRATUM_6 BIT(5)
 
@@ -499,6 +517,7 @@ int rkcanfd_handle_rx_int(struct rkcanfd_priv *priv);
 
 void rkcanfd_timestamp_init(struct rkcanfd_priv *priv);
 
+void rkcanfd_xmit_retry(struct rkcanfd_priv *priv);
 int rkcanfd_start_xmit(struct sk_buff *skb, struct net_device *ndev);
 void rkcanfd_handle_tx_done_one(struct rkcanfd_priv *priv, const u32 ts,
                                unsigned int *frame_len_p);