can: kvaser_usb_leaf: Ignore stale bus-off after start
authorAnssi Hannula <anssi.hannula@bitwise.fi>
Mon, 10 Oct 2022 18:52:34 +0000 (20:52 +0200)
committerMarc Kleine-Budde <mkl@pengutronix.de>
Wed, 26 Oct 2022 08:19:43 +0000 (10:19 +0200)
With 0bfd:0124 Kvaser Mini PCI Express 2xHS FW 4.18.778 it was observed
that if the device was bus-off when stopped, at next start (either via
interface down/up or manual bus-off restart) the initial
CMD_CHIP_STATE_EVENT received just after CMD_START_CHIP_REPLY will have
the M16C_STATE_BUS_OFF bit still set, causing the interface to
immediately go bus-off again.

The bit seems to internally clear quickly afterwards but we do not get
another CMD_CHIP_STATE_EVENT.

Fix the issue by ignoring any initial bus-off state until we see at
least one bus-on state. Also, poll the state periodically until that
occurs.

It is possible we lose one actual immediately occurring bus-off event
here in which case the HW will auto-recover and we see the recovery
event. We will then catch the next bus-off event, if any.

This issue did not reproduce with 0bfd:0017 Kvaser Memorator
Professional HS/HS FW 2.0.50.

Fixes: 71873a9b38d1 ("can: kvaser_usb: Add support for more Kvaser Leaf v2 devices")
Tested-by: Jimmy Assarsson <extja@kvaser.com>
Signed-off-by: Anssi Hannula <anssi.hannula@bitwise.fi>
Signed-off-by: Jimmy Assarsson <extja@kvaser.com>
Link: https://lore.kernel.org/all/20221010185237.319219-9-extja@kvaser.com
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c

index d6ca2ac..6df7b2e 100644 (file)
@@ -427,6 +427,9 @@ struct kvaser_usb_net_leaf_priv {
        struct kvaser_usb_net_priv *net;
 
        struct delayed_work chip_state_req_work;
+
+       /* started but not reported as bus-on yet */
+       bool joining_bus;
 };
 
 static const struct can_bittiming_const kvaser_usb_leaf_m16c_bittiming_const = {
@@ -966,6 +969,7 @@ kvaser_usb_leaf_rx_error_update_can_state(struct kvaser_usb_net_priv *priv,
                                        const struct kvaser_usb_err_summary *es,
                                        struct can_frame *cf)
 {
+       struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv;
        struct kvaser_usb *dev = priv->dev;
        struct net_device_stats *stats = &priv->netdev->stats;
        enum can_state cur_state, new_state, tx_state, rx_state;
@@ -990,6 +994,22 @@ kvaser_usb_leaf_rx_error_update_can_state(struct kvaser_usb_net_priv *priv,
                new_state = CAN_STATE_ERROR_ACTIVE;
        }
 
+       /* 0bfd:0124 FW 4.18.778 was observed to send the initial
+        * CMD_CHIP_STATE_EVENT after CMD_START_CHIP with M16C_STATE_BUS_OFF
+        * bit set if the channel was bus-off when it was last stopped (even
+        * across chip resets). This bit will clear shortly afterwards, without
+        * triggering a second unsolicited chip state event.
+        * Ignore this initial bus-off.
+        */
+       if (leaf->joining_bus) {
+               if (new_state == CAN_STATE_BUS_OFF) {
+                       netdev_dbg(priv->netdev, "ignoring bus-off during startup");
+                       new_state = cur_state;
+               } else {
+                       leaf->joining_bus = false;
+               }
+       }
+
        if (new_state != cur_state) {
                tx_state = (es->txerr >= es->rxerr) ? new_state : 0;
                rx_state = (es->txerr <= es->rxerr) ? new_state : 0;
@@ -1065,9 +1085,12 @@ static void kvaser_usb_leaf_rx_error(const struct kvaser_usb *dev,
 
        /* If there are errors, request status updates periodically as we do
         * not get automatic notifications of improved state.
+        * Also request updates if we saw a stale BUS_OFF during startup
+        * (joining_bus).
         */
        if (new_state < CAN_STATE_BUS_OFF &&
-           (es->rxerr || es->txerr || new_state == CAN_STATE_ERROR_PASSIVE))
+           (es->rxerr || es->txerr || new_state == CAN_STATE_ERROR_PASSIVE ||
+            leaf->joining_bus))
                schedule_delayed_work(&leaf->chip_state_req_work,
                                      msecs_to_jiffies(500));
 
@@ -1587,8 +1610,11 @@ static int kvaser_usb_leaf_set_opt_mode(const struct kvaser_usb_net_priv *priv)
 
 static int kvaser_usb_leaf_start_chip(struct kvaser_usb_net_priv *priv)
 {
+       struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv;
        int err;
 
+       leaf->joining_bus = true;
+
        init_completion(&priv->start_comp);
 
        err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_START_CHIP,
@@ -1719,12 +1745,15 @@ static int kvaser_usb_leaf_set_mode(struct net_device *netdev,
                                    enum can_mode mode)
 {
        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
+       struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv;
        int err;
 
        switch (mode) {
        case CAN_MODE_START:
                kvaser_usb_unlink_tx_urbs(priv);
 
+               leaf->joining_bus = true;
+
                err = kvaser_usb_leaf_simple_cmd_async(priv, CMD_START_CHIP);
                if (err)
                        return err;