Linux kernel and device drivers for NXP i.MX platforms
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Miquel Raynal" <miquel.raynal@bootlin.com>,
	"Mark Brown" <broonie@kernel.org>,
	"Liang Yang" <liang.yang@amlogic.com>,
	"Feng Chen" <feng.chen@amlogic.com>,
	"Xianwei Zhao" <xianwei.zhao@amlogic.com>,
	"Han Xu" <han.xu@nxp.com>,
	"Conor Dooley" <conor.dooley@microchip.com>,
	"Valentina Fernandez" <valentina.fernandezalanis@microchip.com>,
	"Daire McNamara" <daire.mcnamara@microchip.com>,
	"Haibo Chen" <haibo.chen@nxp.com>,
	"Jyan Chou" <jyanchou@realtek.com>,
	"Yu-Chun Lin" <eleanor.lin@realtek.com>,
	"Maxime Coquelin" <mcoquelin.stm32@gmail.com>,
	"Alexandre Torgue" <alexandre.torgue@foss.st.com>,
	"Pratyush Yadav" <pratyush@kernel.org>,
	"Michael Walle" <mwalle@kernel.org>,
	"Takahiro Kuwano" <takahiro.kuwano@infineon.com>,
	"Richard Weinberger" <richard@nod.at>,
	"Vignesh Raghavendra" <vigneshr@ti.com>
Cc: "Thomas Petazzoni" <thomas.petazzoni@bootlin.com>,
	"Alvin Zhou" <alvinzhou@mxic.com.tw>,
	"Cheng Ming Lin" <chengminglin@mxic.com.tw>,
	linux-amlogic@lists.infradead.org, imx@lists.linux.dev,
	linux-stm32@st-md-mailman.stormreply.com,
	linux-mtd@lists.infradead.org, Frank.Li@kernel.org,
	neil.armstrong@linaro.org
Subject: Re: [PATCH RFC 03/16] spi: spi-mem: Add support for dummy cycles
Date: Wed, 25 Nov 2026 18:07:02 +0000	[thread overview]
Message-ID: <sashiko-outbox-233906@kernel.org> (raw)
In-Reply-To: <20261125-macronix-upstreaming-spi-mem-cycles-v1-3-fd036402fd37@bootlin.com>

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@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

  reply	other threads:[~2026-11-25 18:07 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 17:51 [PATCH RFC 00/16] spi: spi-mem: Express the dummy phase using cycles when relevant Miquel Raynal
2026-11-25 17:51 ` [PATCH RFC 01/16] spi: spi-mem: Handle a corner case while sanitizing ops Miquel Raynal
2026-11-25 17:58   ` sashiko-bot
2026-11-25 17:51 ` [PATCH RFC 02/16] spi: spi-mem: Stop checking the dummy buswidth against board wiring Miquel Raynal
2026-11-25 17:51 ` [PATCH RFC 03/16] spi: spi-mem: Add support for dummy cycles Miquel Raynal
2026-11-25 18:07   ` sashiko-bot [this message]
2026-11-25 17:51 ` [PATCH RFC 04/16] spi: amlogic-spifc-a1: Consume dummy cycles directly Miquel Raynal
2026-11-25 17:51 ` [PATCH RFC 05/16] spi: amlogic-spifc-a1: Fix a typo in a macro Miquel Raynal
2026-11-25 17:51 ` [PATCH RFC 06/16] spi: amlogic: spifc-a4: Consume dummy cycles directly Miquel Raynal
2026-11-25 17:59   ` sashiko-bot
2026-11-25 17:51 ` [PATCH RFC 07/16] spi: cadence-qspi: " Miquel Raynal
2026-11-25 17:51 ` [PATCH RFC 08/16] spi: fsl-qspi: " Miquel Raynal
2026-11-25 17:58   ` sashiko-bot
2026-11-25 17:51 ` [PATCH RFC 09/16] spi: microchip-core-qspi: " Miquel Raynal
2026-11-25 18:06   ` sashiko-bot
2026-11-25 17:51 ` [PATCH RFC 10/16] spi: nxp-xspi: " Miquel Raynal
2026-11-25 17:51 ` [PATCH RFC 11/16] spi: nxp-xspi: Fix a comment Miquel Raynal
2026-11-25 17:51 ` [PATCH RFC 12/16] spi: rtk-nor: Consume dummy cycles directly Miquel Raynal
2026-11-25 18:02   ` sashiko-bot
2026-11-25 17:51 ` [PATCH RFC 13/16] spi: sn-f-ospi: " Miquel Raynal
2026-11-25 18:01   ` sashiko-bot
2026-11-25 17:51 ` [PATCH RFC 14/16] spi: stm32-ospi: " Miquel Raynal
2026-11-25 18:12   ` sashiko-bot
2026-11-25 17:51 ` [PATCH RFC 15/16] spi: stm32-qspi: " Miquel Raynal
2026-11-25 18:14   ` sashiko-bot
2026-11-25 17:51 ` [PATCH RFC 16/16] mtd: spi-nor: Express the dummy phase in cycles Miquel Raynal
2026-11-25 18:08   ` sashiko-bot
2026-11-26 11:09 ` [PATCH RFC 00/16] spi: spi-mem: Express the dummy phase using cycles when relevant Miquel Raynal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-233906@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Frank.Li@kernel.org \
    --cc=alexandre.torgue@foss.st.com \
    --cc=alvinzhou@mxic.com.tw \
    --cc=broonie@kernel.org \
    --cc=chengminglin@mxic.com.tw \
    --cc=conor.dooley@microchip.com \
    --cc=daire.mcnamara@microchip.com \
    --cc=eleanor.lin@realtek.com \
    --cc=feng.chen@amlogic.com \
    --cc=haibo.chen@nxp.com \
    --cc=han.xu@nxp.com \
    --cc=imx@lists.linux.dev \
    --cc=jyanchou@realtek.com \
    --cc=liang.yang@amlogic.com \
    --cc=linux-amlogic@lists.infradead.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=linux-stm32@st-md-mailman.stormreply.com \
    --cc=mcoquelin.stm32@gmail.com \
    --cc=miquel.raynal@bootlin.com \
    --cc=mwalle@kernel.org \
    --cc=neil.armstrong@linaro.org \
    --cc=pratyush@kernel.org \
    --cc=richard@nod.at \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=takahiro.kuwano@infineon.com \
    --cc=thomas.petazzoni@bootlin.com \
    --cc=valentina.fernandezalanis@microchip.com \
    --cc=vigneshr@ti.com \
    --cc=xianwei.zhao@amlogic.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox