[PATCH RFC 03/16] spi: spi-mem: Add support for dummy cycles
sashiko-bot at kernel.org
sashiko-bot at kernel.org
Wed Nov 25 10:07:02 PST 2026
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] spi: spi-mem: broken nodirmap fallback in
spi_mem_dirmap_create()
- [High] spi: spi-mem: missing dummy phase on spi_sync() path in
spi_mem_exec_op()
- [Medium] spi: spi-mem: missing dummy conversion in
spi_mem_poll_status()
- [Medium] spi: spi-mem: spi_mem_convert_dummy() bypasses
spi_mem_check_op()
- [Low] spi: spi-mem: trace_spi_mem_start_op() ignores dummy.ncycles
--
commit a302459c2782bc8488f61e01f3aac67fe9d71262
Author: Miquel Raynal <miquel.raynal at bootlin.com>
spi: spi-mem: Add support for dummy cycles
This commit adds a dummy.ncycles field to struct spi_mem_op and a
dummy_cycles controller capability, converting between dummy clock cycles
and dummy bytes depending on what the SPI memory controller expects.
Link: https://lore.kernel.org/all/20220911174551.653599-1-sergiu.moga@microchip.com/
Link: https://lore.kernel.org/all/20250115134158.19908-1-pratyush@kernel.org/
> diff --git a/drivers/spi/spi-mem.c b/drivers/spi/spi-mem.c
> index 9b8b8f2b33877..7f5849bf1a0d7 100644
> --- a/drivers/spi/spi-mem.c
> +++ b/drivers/spi/spi-mem.c
[ ... ]
> @@ -231,9 +359,13 @@ static int spi_mem_check_op(const struct spi_mem_op *op)
>
> if ((op->addr.nbytes && !op->addr.buswidth) ||
> (op->dummy.nbytes && !op->dummy.buswidth) ||
> + (op->dummy.ncycles && !op->dummy.buswidth) ||
> (op->data.nbytes && !op->data.buswidth))
> return -EINVAL;
>
> + if (op->dummy.nbytes && op->dummy.ncycles)
> + return -EINVAL;
> +
> if (!spi_mem_buswidth_is_valid(op->cmd.buswidth) ||
> !spi_mem_buswidth_is_valid(op->addr.buswidth) ||
> !spi_mem_buswidth_is_valid(op->dummy.buswidth) ||
> @@ -290,6 +422,9 @@ bool spi_mem_supports_op(struct spi_mem *mem, const struct spi_mem_op *op)
> */
> spi_mem_adjust_op_freq(mem, &eval_op);
>
> + if (spi_mem_convert_dummy(mem, &eval_op))
> + return false;
> +
> if (spi_mem_check_op(&eval_op))
> return false;
[Severity: Medium]
Should spi_mem_check_op() run before spi_mem_convert_dummy() in
spi_mem_supports_op() and spi_mem_exec_op()?
On controllers without the dummy_cycles capability, an operation with both
dummy.ncycles and dummy.nbytes set enters the if (op->dummy.ncycles) branch
in spi_mem_convert_dummy(), which overwrites op->dummy.nbytes and zeroes
op->dummy.ncycles before spi_mem_check_op() can reject it with -EINVAL.
Running spi_mem_convert_dummy() first also causes spi_mem_exec_op() to
return -EOPNOTSUPP instead of -EINVAL when dummy.buswidth is 0.
[ ... ]
> @@ -390,22 +526,28 @@ static void spi_mem_add_op_stats(struct spi_statistics __percpu *pcpu_stats,
> *
> * Return: 0 in case of success, a negative error code otherwise.
> */
> -int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
> +int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *ro_op)
> {
> unsigned int tmpbufsize, xferpos = 0, totalxferlen = 0;
> struct spi_controller *ctlr = mem->spi->controller;
> + struct spi_mem_op wr_op = *ro_op, *op = &wr_op;
> struct spi_transfer xfers[4] = { };
> struct spi_message msg;
> u8 *tmpbuf;
> int ret;
>
> /* Make sure the operation frequency is correct before going futher */
> - spi_mem_adjust_op_freq(mem, (struct spi_mem_op *)op);
> + spi_mem_adjust_op_freq(mem, op);
> +
> + /* Also make sure the dummy phase can be consumed by the controller */
> + ret = spi_mem_convert_dummy(mem, op);
> + if (ret)
> + return ret;
[Severity: High]
Does calling spi_mem_convert_dummy() here drop the dummy phase when a
dummy_cycles-capable controller falls back to regular SPI transfers via
spi_sync()?
When the controller advertises dummy_cycles, spi_mem_convert_dummy() stores
the dummy phase in op->dummy.ncycles and zeroes op->dummy.nbytes. If
spi_get_csgpiod(mem->spi, 0) is non-NULL or ctlr->mem_ops->exec_op()
returns -ENOTSUPP or -EOPNOTSUPP, spi_mem_exec_op() falls back to building
spi_transfer entries from op->dummy.nbytes:
drivers/spi/spi-mem.c:spi_mem_exec_op() {
...
tmpbufsize = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
...
if (op->dummy.nbytes) {
memset(tmpbuf + op->addr.nbytes + 1, 0xff, op->dummy.nbytes);
xfers[xferpos].tx_buf = tmpbuf + op->addr.nbytes + 1;
xfers[xferpos].len = op->dummy.nbytes;
...
}
Because op->dummy.nbytes is 0, the fallback path omits the dummy transfer
completely instead of converting op->dummy.ncycles back to bytes (or
returning -EOPNOTSUPP if the cycle count is not byte-aligned), corrupting
flash transfers.
>
> dev_vdbg(&mem->spi->dev, "[cmd: 0x%02x][%dB addr: %#8llx][%2dB dummy][%4dB data %s] %d%c-%d%c-%d%c-%d%c @ %uHz\n",
> op->cmd.opcode,
> op->addr.nbytes, (op->addr.nbytes ? op->addr.val : 0),
> - op->dummy.nbytes,
> + spi_mem_get_lossy_dummy_bytes(op),
[Severity: Low]
Does trace_spi_mem_start_op(mem, op) in spi_mem_exec_op() also need to
account for op->dummy.ncycles?
On dummy_cycles-capable controllers, op->dummy.nbytes is 0 after
spi_mem_convert_dummy(), so TRACE_EVENT(spi_mem_start_op) in
include/trace/events/spi-mem.h sizes its dynamic array with
op->dummy.nbytes and omits the dummy bytes from the trace event.
[ ... ]
> @@ -781,6 +931,21 @@ spi_mem_dirmap_create(struct spi_mem *mem,
> desc->mem = mem;
> desc->info = *info;
> desc->info.op_tmpl = &desc->info.primary_op_tmpl;
> +
> + ret = spi_mem_convert_dummy(mem, &desc->info.primary_op_tmpl);
> + if (ret) {
> + kfree(desc);
> + return ERR_PTR(ret);
> + }
> +
> + if (desc->info.secondary_op_tmpl.cmd.opcode) {
> + ret = spi_mem_convert_dummy(mem, &desc->info.secondary_op_tmpl);
> + if (ret) {
> + kfree(desc);
> + return ERR_PTR(ret);
> + }
> + }
[Severity: High]
Does assigning ret = spi_mem_convert_dummy() here break the nodirmap
fallback on controllers that do not implement ctlr->mem_ops->dirmap_create?
spi_mem_dirmap_create() initializes ret to -ENOTSUPP so that when
ctlr->mem_ops->dirmap_create is absent, the if (ret) block below sets
desc->nodirmap = true and validates primary_op_tmpl:
drivers/spi/spi-mem.c:spi_mem_dirmap_create() {
...
if (ctlr->mem_ops && ctlr->mem_ops->dirmap_create) {
...
ret = ctlr->mem_ops->dirmap_create(desc);
spi_mem_access_end(mem);
}
if (ret) {
desc->nodirmap = true;
if (!spi_mem_supports_op(desc->mem, &desc->info.primary_op_tmpl))
ret = -EOPNOTSUPP;
else
ret = 0;
}
...
}
When spi_mem_convert_dummy() succeeds, it sets ret to 0. If the controller
does not provide dirmap_create, ret stays 0, leaving desc->nodirmap false
and causing all subsequent spi_mem_dirmap_read() and spi_mem_dirmap_write()
calls to fail with -ENOTSUPP.
> +
> if (ctlr->mem_ops && ctlr->mem_ops->dirmap_create) {
> ret = spi_mem_access_start(mem);
> if (ret) {
[Severity: Medium]
Should spi_mem_poll_status() also convert the operation with
spi_mem_convert_dummy() before calling ctlr->mem_ops->poll_status()?
drivers/spi/spi-mem.c:spi_mem_poll_status() {
...
if (ctlr->mem_ops && ctlr->mem_ops->poll_status && !spi_get_csgpiod(mem->spi, 0)) {
ret = spi_mem_access_start(mem);
if (ret)
return ret;
ret = ctlr->mem_ops->poll_status(mem, op, mask, match,
initial_delay_us, polling_delay_us,
timeout_ms);
spi_mem_access_end(mem);
}
...
}
Without converting a local copy of op in spi_mem_poll_status(),
ctlr->mem_ops->poll_status() receives the caller's unconverted dummy phase
and programs 0 dummy cycles whenever the caller and controller use
different dummy representations.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125-macronix-upstreaming-spi-mem-cycles-v1-0-fd036402fd37@bootlin.com?part=3
More information about the linux-mtd
mailing list