Linux-mtd Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Fil Dunsky" <filipp.dunsky@gmail.com>,
	"Miquel Raynal" <miquel.raynal@bootlin.com>,
	"Richard Weinberger" <richard@nod.at>,
	"Vignesh Raghavendra" <vigneshr@ti.com>,
	"Rob Herring" <robh@kernel.org>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>
Cc: linux-mtd@lists.infradead.org, devicetree@vger.kernel.org
Subject: Re: [PATCH 2/3] mtd: rawnand: Add Cortina CS7542 controller support
Date: Mon, 23 Nov 2026 19:05:54 +0000	[thread overview]
Message-ID: <sashiko-outbox-229910@kernel.org> (raw)
In-Reply-To: <20261123-b4-cs7542-nand-v1-2-7ce179c011e2@gmail.com>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [Medium] mtd: rawnand: cs752x: bad OOB layout checks in
  cs752x_nand_attach_chip()
- [Medium] mtd: rawnand: cs752x: 32-bit overflow and missing EXTADDR in
  FLASH_NF_ACCESS
- [Low] mtd: rawnand: cs752x: unchecked 4-bit ECC_STS_ERRBIT in
  hamming_correct()
- [Low] mtd: rawnand: cs752x: DT label overwritten in
  cs752x_nand_probe()
- [Low] mtd: rawnand: cs752x: dead small-page checks and duplicate
  nand_write_oob_std()
- [Low] mtd: rawnand: cs752x: missing final register read on timeout in
  polling loops
--

commit a2112af7453f86da46c6a91f8462d27a8fc6193a
Author: Fil Dunsky <filipp.dunsky@gmail.com>

mtd: rawnand: Add Cortina CS7542 controller support

This commit adds a raw NAND controller driver for the Cortina Systems CS7542
SoC using programmed I/O, the controller's command sequencer, and hardware
Hamming ECC.

> diff --git a/drivers/mtd/nand/raw/cs752x_nand.c b/drivers/mtd/nand/raw/cs752x_nand.c
> new file mode 100644
> index 0000000000000..7c8f73e6f1b3a
> --- /dev/null
> +++ b/drivers/mtd/nand/raw/cs752x_nand.c
> @@ -0,0 +1,826 @@

[ ... ]

> +static int check_flash_ctrl_status(struct cs752x_nand_host *h)
> +{
> +	unsigned long timeo = jiffies + HZ;
> +	u32 val;
> +
> +	do {
> +		val = fl_readl(h, FLASH_STATUS) & FLASH_STATUS_NSTATE_MASK;
> +		if (val == 0)
> +			return 0;
> +		cpu_relax();
> +	} while (time_before(jiffies, timeo));
> +
> +	dev_err(h->dev, "FLASH_STATUS busy: 0x%x\n", val);
> +	return -ETIMEDOUT;
> +}
> +
> +static inline u32 mk_nf_command(u8 cmd0, u8 cmd1, u8 cmd2)
> +{
> +	return ((u32)cmd2 << 16) | ((u32)cmd1 << 8) | cmd0;
> +}
> +
> +static u32 mk_nf_addr(struct nand_chip *chip, u32 page, u32 *addr1,
> +		      u32 *addr2, u32 column)
> +{
> +	*addr1 = ((page & 0xffff) << 16) | column;
> +	*addr2 = page >> 16;
> +	return chip->options & NAND_ROW_ADDR_3 ? NCNT_ADDR_5 : NCNT_ADDR_4;
> +}
> +
> +static int wait_reg(struct cs752x_nand_host *h, u32 reg, u32 bit, const char *what)
> +{
> +	unsigned long timeo = jiffies + 2 * HZ;
> +
> +	while (!(fl_readl(h, reg) & bit)) {
> +		if (time_after(jiffies, timeo)) {
> +			dev_err(h->dev, "%s timeout\n", what);
> +			return -ETIMEDOUT;
> +		}
> +		udelay(1);
> +		cond_resched();
> +	}
> +	return 0;
> +}

[Severity: Low]
Can check_flash_ctrl_status() and wait_reg() return a spurious -ETIMEDOUT if
the task is preempted between fl_readl() and the jiffies comparison?

Neither loop re-reads the status register after timeo expires to check
whether the hardware finished while the task was scheduled out.  Using
readl_poll_timeout(), as cs752x_nand_wait_ready() does, would re-check the
condition on timeout.

[ ... ]

> +static void cs752x_setup_page_regs(struct cs752x_nand_host *h, int page,
> +				   bool write, bool oob_required)
> +{
> +	struct nand_chip *chip = &h->chip;
> +	struct mtd_info *mtd = nand_to_mtd(chip);
> +	u32 cmd, addr1, addr2, cnt;
> +
> +	cnt = NF_CNT_DATA(mtd->writesize - 1);
> +	cnt |= NF_CNT_OOB(oob_required ? mtd->oobsize - 1 : NCNT_EMPTY_OOB);
> +
> +	cnt |= NF_CNT_CMD(NCNT_CMD_2);
> +	cmd = write ? mk_nf_command(NAND_CMD_SEQIN, NAND_CMD_PAGEPROG, 0) :
> +		      mk_nf_command(NAND_CMD_READ0, NAND_CMD_READSTART, 0);
> +	cnt |= NF_CNT_ADDR(mk_nf_addr(chip, page, &addr1, &addr2, 0));
> +
> +	fl_writel(h, FLASH_NF_COUNT, cnt);
> +	fl_writel(h, FLASH_NF_COMMAND, cmd);
> +	fl_writel(h, FLASH_NF_ADDRESS_1, addr1);
> +	fl_writel(h, FLASH_NF_ADDRESS_2, addr2);
> +	fl_writel(h, FLASH_NF_ACCESS,
> +		  NF_ACCESS_WIDTH32 |
> +		  NF_ACCESS_EXTADDR(((u32)page << chip->page_shift) / SZ_128M));
> +}

[Severity: Medium]
Does ((u32)page << chip->page_shift) overflow 32-bit arithmetic for byte
offsets at or above 4 GiB?

Because page is cast to u32 before shifting left by chip->page_shift, the
upper bits are lost before dividing by SZ_128M, wrapping the 8-bit
NF_ACCESS_EXTADDR value modulo 32.  In addition, cs752x_nand_pio() writes
FLASH_NF_ACCESS without setting NF_ACCESS_EXTADDR at all, leaving it as 0
for operations executed through cs752x_nand_exec_op() such as OOB reads, OOB
writes, and block erases.

[ ... ]

> +static int hamming_correct(struct cs752x_nand_host *h, u8 *buf, int page)
> +{

[ ... ]

> +		sts = fl_readl(h, FLASH_NF_ECC_STATUS);
> +		switch (ECC_STS_STATUS(sts)) {
> +		case ECC_NO_ERR:
> +			break;
> +		case ECC_1BIT_DATA_ERR:
> +			if (ECC_STS_ERRBYTE(sts) >= eccsize) {
> +				mtd->ecc_stats.failed++;
> +				break;
> +			}
> +			p[ECC_STS_ERRBYTE(sts)] ^= BIT(ECC_STS_ERRBIT(sts));
> +			mtd->ecc_stats.corrected++;
> +			max_bitflips = max(max_bitflips, 1);

[Severity: Low]
Can BIT(ECC_STS_ERRBIT(sts)) overflow the u8 target in p[ECC_STS_ERRBYTE(sts)]?

ECC_STS_ERRBIT(v) masks a 4-bit field (((v) >> 3) & 0xf).  If the register
reports a value in 8..15, BIT(ECC_STS_ERRBIT(sts)) truncates to 0 when XORed
into u8 p[...], leaving the data byte uncorrected while incrementing
mtd->ecc_stats.corrected.

[ ... ]

> +static int cs752x_nand_write_oob_std(struct nand_chip *chip, int page)
> +{
> +	return nand_prog_page_op(chip, page, nand_to_mtd(chip)->writesize,
> +				 chip->oob_poi, nand_to_mtd(chip)->oobsize);
> +}

[Severity: Low]
This isn't a bug, but cs752x_nand_write_oob_std() duplicates the core
nand_write_oob_std() helper, which nand_set_ecc_on_host_ops() assigns
automatically when chip->ecc.write_oob is left NULL.

[ ... ]

> +static int cs752x_nand_attach_chip(struct nand_chip *chip)
> +{
> +	struct mtd_info *mtd = nand_to_mtd(chip);
> +	struct cs752x_nand_host *h = to_host(chip);
> +	int ecc_total;
> +
> +	if (chip->options & NAND_BUSWIDTH_16 ||
> +	    mtd->writesize <= 512 || mtd->oobsize % sizeof(u32) ||
> +	    mtd->writesize + mtd->oobsize > sizeof(h->own_buf))
> +		return -EOPNOTSUPP;
> +	chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
> +	chip->ecc.placement = NAND_ECC_PLACEMENT_OOB;
> +
> +	chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
> +	chip->ecc.size = 256;
> +	chip->ecc.bytes = 3;
> +	chip->ecc.strength = 1;
> +
> +	if (mtd->writesize < chip->ecc.size) {
> +		dev_err(h->dev, "%d byte HW ECC not possible on %d byte page\n",
> +			chip->ecc.size, mtd->writesize);
> +		return -EINVAL;
> +	}
> +
> +	ecc_total = mtd->writesize / chip->ecc.size * chip->ecc.bytes;
> +	if (ecc_total > mtd->oobsize - 2) {
> +		dev_err(h->dev, "%d ECC bytes do not fit in %d byte OOB\n",
> +			ecc_total, mtd->oobsize);
> +		return -EINVAL;
> +	}
> +
> +	h->ecc_calc = devm_kzalloc(h->dev, mtd->oobsize, GFP_KERNEL);
> +	h->ecc_code = devm_kzalloc(h->dev, mtd->oobsize, GFP_KERNEL);
> +	if (!h->ecc_calc || !h->ecc_code)
> +		return -ENOMEM;
> +
> +	if (mtd->oobsize == 8 || mtd->oobsize == 16)
> +		mtd_set_ooblayout(mtd, nand_get_small_page_ooblayout());
> +	else
> +		mtd_set_ooblayout(mtd, nand_get_large_page_hamming_ooblayout());

[Severity: Medium]
Does cs752x_nand_attach_chip() allow OOB geometries that
nand_get_large_page_hamming_ooblayout() cannot handle?

nand_ooblayout_ecc_lp_hamming() only supports mtd->oobsize of 64 (placing
ECC at offset 40, so at most 24 ECC bytes fit) and 128 (placing ECC at
offset 80, so at most 48 ECC bytes fit):

drivers/mtd/nand/ecc.c:nand_ooblayout_ecc_lp_hamming() {
    ...
	switch (mtd->oobsize) {
	case 64:
		oobregion->offset = 40;
		break;
	case 128:
		oobregion->offset = 80;
		break;
	default:
		return -EINVAL;
	}

	oobregion->length = total_ecc_bytes;
	if (oobregion->offset + oobregion->length > mtd->oobsize)
		return -ERANGE;
    ...
}

Because cs752x_nand_attach_chip() only checks ecc_total > mtd->oobsize - 2,
a 4 KiB page chip with 64-byte OOB (ecc_total = 48) or any chip with an OOB
size other than 64 or 128 (such as 224 or 256) passes attach, underflows
oobregion->length in nand_ooblayout_free_lp_hamming() on 4 KiB + 64B OOB,
and fails every ECC page read and write at runtime with -ERANGE or -EINVAL.

Also, cs752x_nand_attach_chip() unconditionally overwrites the chip->ecc
fields parsed from Device Tree by rawnand_dt_init() instead of rejecting
unsupported DT ECC settings, and the mtd->writesize < chip->ecc.size and
mtd->oobsize == 8 || mtd->oobsize == 16 checks are unreachable after
rejecting mtd->writesize <= 512 above.

[ ... ]

> +	nand_set_flash_node(chip, flash);
> +	of_node_put(flash);
> +	nand_set_controller_data(chip, h);
> +	mtd->dev.parent = dev;
> +	mtd->name = "cs752x_nand_flash";

[Severity: Low]
Does assigning mtd->name after nand_set_flash_node() overwrite a Device Tree
label?

nand_set_flash_node() calls mtd_set_of_node(), which sets mtd->name from the
flash node's label property if mtd->name is NULL.  Setting mtd->name
unconditionally afterwards discards any DT-provided label.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123-b4-cs7542-nand-v1-0-7ce179c011e2@gmail.com?part=2

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

  reply	other threads:[~2026-11-23 19:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 18:41 [PATCH 0/3] Add Cortina Systems CS7542 NAND controller support Fil Dunsky via B4 Relay
2026-11-23 18:41 ` [PATCH 1/3] dt-bindings: mtd: Add Cortina CS7542 NAND controller Fil Dunsky via B4 Relay
2026-11-23 18:41 ` [PATCH 2/3] mtd: rawnand: Add Cortina CS7542 controller support Fil Dunsky via B4 Relay
2026-11-23 19:05   ` sashiko-bot [this message]
2026-11-23 18:41 ` [PATCH 3/3] MAINTAINERS: Add Cortina CS7542 NAND controller entry Fil Dunsky via B4 Relay

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-229910@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=filipp.dunsky@gmail.com \
    --cc=krzk+dt@kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=miquel.raynal@bootlin.com \
    --cc=richard@nod.at \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=vigneshr@ti.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