Skip to content

can: mpfs_can: read received frames in arrival order - #9

Open
kohei-otsuka-smc wants to merge 2 commits into
linux4microchip:linux-6.18-mchpfrom
Space-Machines-Company:fix/mpfs-can-rx-order
Open

kohei-otsuka-smc wants to merge 2 commits into
linux4microchip:linux-6.18-mchpfrom
Space-Machines-Company:fix/mpfs-can-rx-order

Conversation

@kohei-otsuka-smc

@kohei-otsuka-smc kohei-otsuka-smc commented Nov 25, 2026 •

Copy link
Copy Markdown

Problem

The MSS CAN controller stores each received frame in the lowest-numbered empty receive buffer. mpfs_can read 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

  1. can: mpfs_can: read received frames in arrival order

    • Hold each receive buffer after reading it. Release the held buffers only when the next buffer is empty. While held buffers stay full, new frames land in ascending buffer order, which matches their arrival order.
    • If an skb cannot be allocated, the frame is now dropped and its buffer released with the others, instead of being retried on every poll.
    • Trade-off: under a burst longer than the 32 buffers while the driver only just keeps up, more frames can be lost than before, but the frames that are kept stay in order.
  2. can: mpfs_can: count frames lost for want of a receive buffer

    • The driver enabled the receive message loss interrupt but ignored it. A lost frame is now counted as an RX overrun and reported with a CAN_ERR_CRTL_RX_OVERFLOW error frame.

Both changes are in drivers/net/can/mpfs_can.c.

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>
@ConchuOD ConchuOD self-assigned this Nov 25, 2026
@ConchuOD

Copy link
Copy Markdown
Member

Cool, thanks for these fixes. I will take a look at these and get back to you.

@kohei-otsuka-smc

Copy link
Copy Markdown
Author

@ConchuOD , Thanks! We have tested the fix internally.

@ConchuOD

Copy link
Copy Markdown
Member

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
receive message0"" Please?

mpfs_can_err_interrupt(ndev, isr);

if (isr & MPFS_CAN_ISR_RXMSG_LOS_MASK)
mpfs_can_rx_lost(ndev);

@ConchuOD ConchuOD Nov 26, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ConchuOD

Copy link
Copy Markdown
Member

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.

static int mpfs_can_rx_poll(struct napi_struct *napi, int quota)
{
	struct net_device *ndev = napi->dev;
	struct mpfs_can_priv *priv = netdev_priv(ndev);
	unsigned int next, buf, n;
	u32 int_enable;
	int processed = 0;

	while (processed < quota) {
		if (priv->rx_raced) {
			buf = priv->rx_raced_start;
			for (n = 0; n < MPFS_CAN_RX_BUFFERS && !(priv->rx_raced & BIT(buf)); n++)
				buf = (buf + 1) % MPFS_CAN_RX_BUFFERS;
			priv->rx_raced &= ~BIT(buf);
			priv->rx_raced_start = buf;
			if (mpfs_can_rx_buf_full(priv, buf)) {
				mpfs_can_rx_hold(ndev, buf);
				processed++;
			} else {
				/* A restart reset the controller after the release. */
				priv->rx_raced = 0;
			}
			continue;
		}

		next = priv->rx_held == GENMASK(MPFS_CAN_RX_BUFFERS - 1, 0) ?
		       MPFS_CAN_RX_BUFFERS : ffz(priv->rx_held);
		if (next < MPFS_CAN_RX_BUFFERS && mpfs_can_rx_buf_full(priv, next)) {
			mpfs_can_rx_hold(ndev, next);
			processed++;
			continue;
		}

		{
			unsigned long flags;
			unsigned int buf;

			/* Nothing happens in the isr rx wise, should this instead be a lock?
			 * This only affects the current cpu anyway?
			 */
			local_irq_save(flags);

			/* Impossible because 15 lines above checks this condition and continues.
			 * Has "next" caused confusion because "next" actually represents
			 * the buffer from this iteration? This needs to check "next" + 1, to
			 * avoid freeing buffers when there's data above the quota.
			 */
			if (next < MPFS_CAN_RX_BUFFERS && mpfs_can_rx_buf_full(priv, next)) {
				local_irq_restore(flags);
				priv->rx_raced_start = next;
				return BIT(next);
			}

			/* Release all held buffers. Reachable only if all buffers are held or
			 * if there are fewer than quota filled buffers, because of the continue
			 * after the hold 25 lines above and the one 33 lines above for "raced"
			 * data.
			 */
			for (buf = 0; buf < MPFS_CAN_RX_BUFFERS; buf++)
				if (priv->rx_held & BIT(buf))
					mpfs_can_rx_release(priv, buf);
			priv->rx_held = 0;

			/* This should be able to just check the mask register instead of doing
			 * a loop over the command ones?
			 * Why does the check start from 0 anyway? Shouldn't special handling
			 * only be needed if there is data above the last held buffer?
			 */
			for (buf = 0; buf < MPFS_CAN_RX_BUFFERS; buf++)
				if (mpfs_can_rx_buf_full(priv, buf))
					priv->rx_raced |= BIT(buf);

			local_irq_restore(flags);

			priv->rx_raced_start = next < MPFS_CAN_RX_BUFFERS ? next : 0;
		}

		if (!priv->rx_raced)
			break;
	}

	if (processed)
		mpfs_can_update_error_state(ndev);

	if (processed < quota) {
		if (napi_complete_done(napi, processed)) {
			int_enable = mpfs_can_read(priv, MPFS_CAN_IER_OFFSET);
			int_enable |= (MPFS_CAN_IER_RXMSG_SNT_MASK);
			mpfs_can_write(priv, MPFS_CAN_IER_OFFSET, int_enable);
		}
	}

	return processed;
}
struct clk_bulk_data *clks;
int num_clocks;
const struct can_bittiming_const *bittiming_const;
u32 rx_held;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants