Skip to content

Commit fe43cf0

Browse files
Swark-AxiadoJassi Brar
authored andcommitted
mailbox: axiado: fix RX frame draining and error handling
Two RX-path bugs found during bring-up: 1. axiado_rx_thread() previously consumed only one frame per invocation. Multiple frames can accumulate in the FIFO while the threaded handler is running. Split the single-frame handling into axiado_mbox_receive_one_frame() and drain all currently queued frames before returning from the handler. A cond_resched() in the drain loop prevents a continuously-fed channel from monopolizing the CPU. 2. readl_poll_timeout() waiting for the remainder of a frame can return success either because the required FIFO level was reached or because a FIFO error appeared -- it does not distinguish the two. The old code did not check for that case and could therefore enter the read loop without enough words actually being present. Explicitly inspect the error status after the poll: if an error occurs while waiting for the remainder of an already-started frame, treat that frame as unusable and flush the FIFO to resynchronize, rather than risk delivering incomplete or garbage data to the client. While here: - Track whether the handler actually serviced a hardware condition, such as clearing a FIFO error or consuming frame data. Return IRQ_HANDLED whenever something was serviced, even if the FIFO is empty afterward, instead of reporting IRQ_NONE after handling a hardware condition. - Use readsl() for the RX FIFO payload. The destination is the driver-owned rx_buffer allocated by devm_kzalloc(), so it has suitable alignment for the stream accessor. readsl() is appropriate for copying a sequence of words from the FIFO into that buffer. The TX path intentionally stays on get_unaligned_le32()/writel(): its buffer is supplied by an external mailbox client, the mailbox API gives no guarantee that buffer is naturally aligned, and the TX path also explicitly handles a partial final word. mbox_free_channel() invokes ->shutdown() before chan->cl is cleared, and axiado_mbox_shutdown() calls disable_irq(), which waits for the in-flight threaded handler to complete, so no functional change is needed for that path. Verified: builds clean with the aarch64 cross-compiler, passes checkpatch.pl, and was regression-tested with live mailbox traffic on AX3005. Fixes: 14af7a9 ("mailbox: add Axiado AX3005 mailbox driver") Signed-off-by: Swark Yang <syang@axiado.com> Signed-off-by: Jassi Brar <jassisinghbrar@gmail.com>
1 parent a90ee43 commit fe43cf0

1 file changed

Lines changed: 89 additions & 25 deletions

File tree

‎drivers/mailbox/axiado-mailbox.c‎

Lines changed: 89 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/of.h>
1616
#include <linux/platform_device.h>
1717
#include <linux/property.h>
18+
#include <linux/sched.h>
1819
#include <linux/slab.h>
1920
#include <linux/unaligned.h>
2021

@@ -36,6 +37,12 @@
3637
#define AXIADO_MBOX_CSR_ERRORS (AXIADO_MBOX_CSR_OVERFLOW | \
3738
AXIADO_MBOX_CSR_UNDERFLOW)
3839

40+
enum axiado_mbox_frame_result {
41+
AXIADO_MBOX_FRAME_EMPTY,
42+
AXIADO_MBOX_FRAME_OK,
43+
AXIADO_MBOX_FRAME_BAD, /* malformed/incomplete; FIFO flushed */
44+
};
45+
3946
struct axiado_mbox_data {
4047
u8 num_chans;
4148
u16 msg_size;
@@ -116,6 +123,13 @@ static int axiado_mbox_send_data(struct mbox_chan *chan, void *data)
116123
return -EBUSY;
117124
}
118125

126+
/*
127+
* Per-word get_unaligned_le32()/writel(), not writesl(): @data is
128+
* supplied by whatever client calls mbox_send_message(), and
129+
* mbox_chan_ops.send_data(void *data) makes no alignment guarantee
130+
* for that buffer. writesl() dereferences it as a native u32 *,
131+
* which is unsafe unless alignment is known.
132+
*/
119133
for (offset = 0; offset + sizeof(u32) <= msg_len;
120134
offset += sizeof(u32))
121135
writel(get_unaligned_le32(buf + offset), priv->mbox_reg);
@@ -131,31 +145,42 @@ static int axiado_mbox_send_data(struct mbox_chan *chan, void *data)
131145
return 0;
132146
}
133147

134-
static irqreturn_t axiado_rx_thread(int irq, void *dev_id)
148+
/*
149+
* Read and, if valid, deliver exactly one length-prefixed frame currently
150+
* at the head of the RX FIFO. Does not loop -- the caller decides whether
151+
* to call this again. May clear a pending FIFO error at several points
152+
* below, wherever a new one could plausibly have appeared since the last
153+
* check; each check is a cheap readl() when nothing is pending.
154+
*/
155+
static enum axiado_mbox_frame_result axiado_mbox_receive_one_frame(struct axiado_channel_data *priv,
156+
bool *serviced)
135157
{
136-
struct axiado_channel_data *priv = dev_id;
137158
struct mbox_chan *chan = priv->chan;
138159
struct axiado_mbox *mb = dev_get_drvdata(chan->mbox->dev);
139160
u8 *buf = priv->rx_buffer;
140161
unsigned int num_words;
141162
unsigned int remaining;
142-
unsigned int i;
143163
u32 msg_len;
144164
u32 word;
145165
u32 csr;
146166
int ret;
147167

148168
/*
149-
* Clear and log/count any pending errors, but don't discard data
150-
* on their account alone: a rejected overflow write doesn't
151-
* corrupt what was already safely queued ahead of it, so let the
152-
* length-based read below decide whether what's here is usable.
169+
* Don't discard data on a cleared error's account alone: a rejected
170+
* overflow write doesn't corrupt what was already safely queued
171+
* ahead of it, so let the length-based read below decide whether
172+
* what's here is usable. Count clearing a real error as @serviced
173+
* so the handler reports IRQ_HANDLED whenever it actually handled
174+
* a hardware condition.
153175
*/
154-
axiado_mbox_clear_fifo_errors(priv);
176+
if (axiado_mbox_clear_fifo_errors(priv))
177+
*serviced = true;
155178

156179
csr = readl(priv->csr_reg);
157180
if (csr & AXIADO_MBOX_CSR_EMPTY)
158-
return IRQ_NONE;
181+
return AXIADO_MBOX_FRAME_EMPTY;
182+
183+
*serviced = true;
159184

160185
/*
161186
* The first word contains the total message length in bytes,
@@ -165,8 +190,12 @@ static irqreturn_t axiado_rx_thread(int irq, void *dev_id)
165190
msg_len = word;
166191
put_unaligned_le32(word, buf);
167192

168-
if (msg_len < sizeof(u32) || msg_len > mb->drv_data->msg_size)
169-
goto invalid_message;
193+
if (msg_len < sizeof(u32) || msg_len > mb->drv_data->msg_size) {
194+
dev_warn_ratelimited(chan->mbox->dev,
195+
"Channel %u invalid frame length %u (max %u)\n",
196+
priv->channel_num, msg_len, mb->drv_data->msg_size);
197+
goto bad_frame;
198+
}
170199

171200
num_words = DIV_ROUND_UP(msg_len, sizeof(u32));
172201
remaining = num_words - 1;
@@ -182,33 +211,68 @@ static irqreturn_t axiado_rx_thread(int irq, void *dev_id)
182211
remaining,
183212
AXIADO_MBOX_RX_POLL_US,
184213
AXIADO_MBOX_RX_TIMEOUT_US);
185-
if (ret)
186-
goto incomplete_message;
214+
/*
215+
* The wait condition above is satisfied by EITHER the
216+
* expected word count landing OR a FIFO error appearing.
217+
* ret == 0 only means "didn't time out" - it does not mean
218+
* the words are actually there. Check @csr for the error
219+
* bit explicitly; otherwise an underflow can present as
220+
* "success" and the read loop below pulls words that were
221+
* never pushed, handing the client garbage.
222+
*/
223+
if (ret || (csr & AXIADO_MBOX_CSR_ERRORS)) {
224+
dev_warn_ratelimited(chan->mbox->dev,
225+
"Channel %u incomplete frame: got %lu/%u words (%s)\n",
226+
priv->channel_num,
227+
FIELD_GET(AXIADO_MBOX_CSR_LEVEL, csr),
228+
num_words,
229+
(csr & AXIADO_MBOX_CSR_ERRORS) ?
230+
"FIFO error" : "timed out");
231+
goto bad_frame;
232+
}
187233

188234
axiado_mbox_clear_fifo_errors(priv);
189235
}
190236

191-
for (i = 1; i < num_words; i++) {
192-
word = readl(priv->mbox_reg);
193-
put_unaligned_le32(word, buf + i * sizeof(u32));
194-
}
237+
/*
238+
* readsl() streams the payload words directly into rx_buffer, which
239+
* is allocated by this driver via devm_kzalloc() and therefore has
240+
* suitable alignment for a stream accessor.
241+
*/
242+
readsl(priv->mbox_reg, buf + sizeof(u32), remaining);
195243

196244
axiado_mbox_clear_fifo_errors(priv);
197245

198246
if (READ_ONCE(priv->active))
199247
mbox_chan_received_data(chan, priv->rx_buffer);
200248

201-
return IRQ_HANDLED;
249+
return AXIADO_MBOX_FRAME_OK;
202250

203-
incomplete_message:
204-
dev_warn_ratelimited(chan->mbox->dev,
205-
"Channel %u received an incomplete message\n",
206-
priv->channel_num);
207-
208-
invalid_message:
251+
bad_frame:
252+
axiado_mbox_clear_fifo_errors(priv);
209253
writel(AXIADO_MBOX_CSR_FLUSH, priv->csr_reg);
210254

211-
return IRQ_HANDLED;
255+
return AXIADO_MBOX_FRAME_BAD;
256+
}
257+
258+
static irqreturn_t axiado_rx_thread(int irq, void *dev_id)
259+
{
260+
struct axiado_channel_data *priv = dev_id;
261+
enum axiado_mbox_frame_result result;
262+
bool handled = false;
263+
264+
/*
265+
* Drain all frames currently queued in the FIFO before returning.
266+
* Multiple frames may accumulate while the threaded handler is
267+
* running. cond_resched() prevents a continuously-fed channel from
268+
* monopolizing the CPU.
269+
*/
270+
do {
271+
result = axiado_mbox_receive_one_frame(priv, &handled);
272+
cond_resched();
273+
} while (result == AXIADO_MBOX_FRAME_OK);
274+
275+
return handled ? IRQ_HANDLED : IRQ_NONE;
212276
}
213277

214278
static int axiado_mbox_startup(struct mbox_chan *chan)

0 commit comments

Comments
 (0)