Repository navigation
can: mpfs_can: read received frames in arrival order - #9
kohei-otsuka-smc wants to merge 2 commits into
Conversation
The MSS CAN controller stores each received frame in the lowest-numbered empty receive buffer (Microchip's MSS CAN user guide, "Received Message Processing": the handler "searches all receive buffers, starting from the receive message0"). The driver read the lowest-numbered full buffer and released it at once. When two frames waited, the next frame landed in the freed lower buffer and was read before the older frame above it. On an Icicle Kit with a CubeComputer on CAN1, CSP replies split over CAN frames arrived as 5, 3, 4, 2, 1, 0 while a second listener on the bus saw 5, 4, 3, 2, 1, 0. libcsp then dropped the reply. Hold each buffer after reading it, and release the held buffers only when the next buffer is empty. While held buffers stay full, new frames land in ascending buffer order, which is their arrival order. Frames that land while the held buffers are released are read first, starting at the buffer that was empty. This relies on at most one frame arriving during the release, which takes far less than one CAN frame time. Holding buffers costs receive capacity: under a burst longer than the 32 buffers while the driver only just keeps up, more frames are lost than before, but the frames kept stay in order. The next patch counts the losses. A frame whose skb cannot be allocated is now dropped and its buffer released with the others, instead of being retried on every poll. Upstream-Status: Pending Signed-off-by: Binh Nguyen <binh@spacemachines.com>
The driver enables the controller's receive message loss interrupt but ignored it, so a frame dropped because every receive buffer was full left no trace in the interface statistics. Count it as an RX overrun and report it with a CAN_ERR_CRTL_RX_OVERFLOW error frame. The interrupt status is one latched bit, so each count means at least one lost frame. Upstream-Status: Pending Signed-off-by: Binh Nguyen <binh@spacemachines.com>
|
Cool, thanks for these fixes. I will take a look at these and get back to you. |
|
@ConchuOD , Thanks! We have tested the fix internally. |
|
Can you provide a link to the document that says "Microchip's MSS CAN user guide, "Received Message Processing": the handler "searches all receive buffers, starting from the |
| mpfs_can_err_interrupt(ndev, isr); | ||
|
|
||
| if (isr & MPFS_CAN_ISR_RXMSG_LOS_MASK) | ||
| mpfs_can_rx_lost(ndev); |
There was a problem hiding this comment.
Instead of creating a new function for this, could the error be added to MPFS_CAN_ERR_MASK and the handling done in mpfs_can_err_interrupt() alongside the other errors? Or does something about this particular error prevent doing so?
There was a problem hiding this comment.
I thought about it but the reason currently it is put outside of mpfs_can_err_interrupt() is that it uniformly increments bus error internally while rx overflow is more like a controller error.
But I am open for a change though.
if (int_status & MPFS_CAN_ERR_MASK) { priv->can.can_stats.bus_error++; cf.can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
There was a problem hiding this comment.
Perhaps you know better than I how errors should be reported, but as things are the driver reports one bus error no matter how many bits are set in int_status. Is that correct?
If it is correct, we should be able to do some sort of
if (int_status & ERR_MASK)
if (int_status & ~RXMSG_LOS_MASK) {
priv->can.can_stats.bus_error++;
cf.can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
}
if (int_status & RXMSG_LOS_MASK) {
stats->rx_over_errors++;
stats->rx_errors++;
cf->can_id |= CAN_ERR_CRTL;
cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
}
I note that the original code's
cf.can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
is a bit suspect too, because the individual if statements for errors mostly
(but not all) set CAN_ERR_PROT, which makes me wonder if that should just be
cf.can_id |= CAN_ERR_BUSERROR;
?
FWIW, I am not expecting an immediate update here - I've been looking at the other patch and I have some concerns about the functionality of it. I will comment later on this when I have the time to arrange my thoughts.
|
So, I have done some re-jiggery of the code in order to comment on it with a bit more context than a UI like github's permits, pulling the contents of mpfs_can_rx_free() into its caller. I've added my thoughts on this as comments. |
| struct clk_bulk_data *clks; | ||
| int num_clocks; | ||
| const struct can_bittiming_const *bittiming_const; | ||
| u32 rx_held; |
There was a problem hiding this comment.
Does this actually need to be a bitmap?
If my understanding of the intention of the free code is correct (see my comments on that) it's not supposed to be possible to release buffers if there is still data in a higher numbered one,
so it should be possible to track the index alone?
If that's possible, releasing can loop from 0 to rx_held, rather than over all buffers.
| const struct can_bittiming_const *bittiming_const; | ||
| u32 rx_held; | ||
| u32 rx_raced; | ||
| unsigned int rx_raced_start; |
There was a problem hiding this comment.
With the holding scheme, I think the important "race" case is that some data appears in the buffer above us, as we are performing the release for the held set?
If data backfills as we are releasing, that's just normal data at that point - the problem would be if it came in faster than we were releasing buffers but I don't think the current code handles that anyway. We should be able to either just use a bitmap and start at the first set bit or track the start and end of the data above the release point.
I think anyway..
Also I don't love "raced" as a name here, because I think this code is also used if the number of populated buffers exceeds the quota?
Problem
The MSS CAN controller stores each received frame in the lowest-numbered empty receive buffer.
mpfs_canread the lowest-numbered full buffer and released it at once. When two or more frames were waiting, the next frame landed in the freed lower buffer and was read before older frames above it, so frames reached userspace out of order.Changes
can: mpfs_can: read received frames in arrival order
can: mpfs_can: count frames lost for want of a receive buffer
CAN_ERR_CRTL_RX_OVERFLOWerror frame.Both changes are in
drivers/net/can/mpfs_can.c.