Skip to content

Commit 3d9db29

Browse files
can: m_can: m_can_handle_state_errors(): fix CAN state transition to Error Active
The CAN Error State is determined by the receive and transmit error counters. The CAN error counters decrease when reception/transmission is successful, so that a status transition back to the Error Active status is possible. This transition is not handled by m_can_handle_state_errors(). Add the missing detection of the Error Active state to m_can_handle_state_errors() and extend the handling of this state in m_can_handle_state_change(). Fixes: e0d1f48 ("can: m_can: add Bosch M_CAN controller support") Fixes: cd0d83e ("can: m_can: m_can_handle_state_change(): fix state change") Reviewed-by: Markus Schneider-Pargmann <[email protected]> Link: https://patch.msgid.link/20250929-m_can-fix-state-handling-v4-2-682b49b49d9a@pengutronix.de Signed-off-by: Marc Kleine-Budde <[email protected]>
1 parent ba569fb commit 3d9db29

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

drivers/net/can/m_can/m_can.c

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,9 @@ static int m_can_handle_state_change(struct net_device *dev,
812812
u32 timestamp = 0;
813813

814814
switch (new_state) {
815+
case CAN_STATE_ERROR_ACTIVE:
816+
cdev->can.state = CAN_STATE_ERROR_ACTIVE;
817+
break;
815818
case CAN_STATE_ERROR_WARNING:
816819
/* error warning state */
817820
cdev->can.can_stats.error_warning++;
@@ -841,6 +844,12 @@ static int m_can_handle_state_change(struct net_device *dev,
841844
__m_can_get_berr_counter(dev, &bec);
842845

843846
switch (new_state) {
847+
case CAN_STATE_ERROR_ACTIVE:
848+
cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
849+
cf->data[1] = CAN_ERR_CRTL_ACTIVE;
850+
cf->data[6] = bec.txerr;
851+
cf->data[7] = bec.rxerr;
852+
break;
844853
case CAN_STATE_ERROR_WARNING:
845854
/* error warning state */
846855
cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
@@ -877,30 +886,33 @@ static int m_can_handle_state_change(struct net_device *dev,
877886
return 1;
878887
}
879888

880-
static int m_can_handle_state_errors(struct net_device *dev, u32 psr)
889+
static enum can_state
890+
m_can_state_get_by_psr(struct m_can_classdev *cdev)
881891
{
882-
struct m_can_classdev *cdev = netdev_priv(dev);
883-
int work_done = 0;
892+
u32 reg_psr;
884893

885-
if (psr & PSR_EW && cdev->can.state != CAN_STATE_ERROR_WARNING) {
886-
netdev_dbg(dev, "entered error warning state\n");
887-
work_done += m_can_handle_state_change(dev,
888-
CAN_STATE_ERROR_WARNING);
889-
}
894+
reg_psr = m_can_read(cdev, M_CAN_PSR);
890895

891-
if (psr & PSR_EP && cdev->can.state != CAN_STATE_ERROR_PASSIVE) {
892-
netdev_dbg(dev, "entered error passive state\n");
893-
work_done += m_can_handle_state_change(dev,
894-
CAN_STATE_ERROR_PASSIVE);
895-
}
896+
if (reg_psr & PSR_BO)
897+
return CAN_STATE_BUS_OFF;
898+
if (reg_psr & PSR_EP)
899+
return CAN_STATE_ERROR_PASSIVE;
900+
if (reg_psr & PSR_EW)
901+
return CAN_STATE_ERROR_WARNING;
896902

897-
if (psr & PSR_BO && cdev->can.state != CAN_STATE_BUS_OFF) {
898-
netdev_dbg(dev, "entered error bus off state\n");
899-
work_done += m_can_handle_state_change(dev,
900-
CAN_STATE_BUS_OFF);
901-
}
903+
return CAN_STATE_ERROR_ACTIVE;
904+
}
902905

903-
return work_done;
906+
static int m_can_handle_state_errors(struct net_device *dev)
907+
{
908+
struct m_can_classdev *cdev = netdev_priv(dev);
909+
enum can_state new_state;
910+
911+
new_state = m_can_state_get_by_psr(cdev);
912+
if (new_state == cdev->can.state)
913+
return 0;
914+
915+
return m_can_handle_state_change(dev, new_state);
904916
}
905917

906918
static void m_can_handle_other_err(struct net_device *dev, u32 irqstatus)
@@ -1031,8 +1043,7 @@ static int m_can_rx_handler(struct net_device *dev, int quota, u32 irqstatus)
10311043
}
10321044

10331045
if (irqstatus & IR_ERR_STATE)
1034-
work_done += m_can_handle_state_errors(dev,
1035-
m_can_read(cdev, M_CAN_PSR));
1046+
work_done += m_can_handle_state_errors(dev);
10361047

10371048
if (irqstatus & IR_ERR_BUS_30X)
10381049
work_done += m_can_handle_bus_errors(dev, irqstatus,

0 commit comments

Comments
 (0)