Message ID | 20230606101644.3297859-1-AVKrasnov@sberdevices.ru |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp3293766vqr; Tue, 6 Jun 2023 03:38:14 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ71u1LAPM7vaAXWzbIC4MApiafOrqaNftkm5vAxVyYUJkOvZJUiYNUOEZ5f+/UzhSGlQ7tZ X-Received: by 2002:a05:620a:3ac5:b0:75d:4682:125b with SMTP id ss5-20020a05620a3ac500b0075d4682125bmr1154205qkn.33.1686047894597; Tue, 06 Jun 2023 03:38:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686047894; cv=none; d=google.com; s=arc-20160816; b=gQAYsGMb/n0TD7PtAzR/EQ/kqga8ZAYRmPL+mFnl81r8mMf79ihBiWbnAIPxUoOvVk mD80wkKg2yappR0L+FLJ4qP8ISE61TMH/cwFM8rr8pt9XUTyK100x0ISAkI2zkqIfPED Gvs2DtyKfaynGhpmFydgBC6+qt88uNzwzBLu+axQ+u4zYUWsvlol9KjEEJ6SHRAA6qS/ yWN4dBUPclWVci6xXBBEm9qgMkdPBqKEQ202OVysDXWjhzZYGy9oujfbXPZoc2aTjo8t UZuSao2x443iKRZJP2pKT+O/W002ioQeAFOaxhUKoeWQakSVbVPYwFiEyYYfp8VvA4jl 4SPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=DTBB8jOO77vAsxmiB/i7I8AL4Y5HWSjjJbkLXHcxMxY=; b=FZY6+XRYKIIM5Z9Y3Eb9TV5xifhTfr+6UNXhNxhjJzfuP6ue3GQhNiR5Dmvq8m6dzi RJo8AKKEg0OXAPDj6gIOvZkmuA8AFJBiXvAG3F1voXAaLUVgXvjCHrUTdzi85dohlKFc G/LRLknaaNfsZ1pEjww5cqEEXvfanMHgBLZg+NqRMvWRVNDnkhY3Rrh8Pf9e1m1XF1Ll GNEqmMEu5fgF9SVG9e4FLQgCkeYVCsrEtPQwEhMUVuBRMo8fc+zO6X5ydNFVoS1CAya5 sxOJOczxKA8jI3Jc8BUsaTEXdQlFP7vEufzRPJVwgusfbAZeCvKslWNGSaeTc5IYOK1W G2EQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sberdevices.ru header.s=mail header.b="IWy/fUlA"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=sberdevices.ru Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x9-20020ae9e909000000b0075cdd7e2c4bsi5639875qkf.368.2023.06.06.03.38.00; Tue, 06 Jun 2023 03:38:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@sberdevices.ru header.s=mail header.b="IWy/fUlA"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=sberdevices.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235701AbjFFKWo (ORCPT <rfc822;xxoosimple@gmail.com> + 99 others); Tue, 6 Jun 2023 06:22:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236071AbjFFKWV (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 6 Jun 2023 06:22:21 -0400 Received: from mx.sberdevices.ru (mx.sberdevices.ru [45.89.227.171]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48A7310F1 for <linux-kernel@vger.kernel.org>; Tue, 6 Jun 2023 03:22:10 -0700 (PDT) Received: from s-lin-edge02.sberdevices.ru (localhost [127.0.0.1]) by mx.sberdevices.ru (Postfix) with ESMTP id CF6835FD1B; Tue, 6 Jun 2023 13:22:07 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sberdevices.ru; s=mail; t=1686046927; bh=DTBB8jOO77vAsxmiB/i7I8AL4Y5HWSjjJbkLXHcxMxY=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type; b=IWy/fUlAKarZIURZ2S8lVHBjPYVcItMkVqJahSnQb8MZcKSTbqACk4UPLrXx7FLzK J0p9mFLwW2hBU0f6GZtZVCdFtjHg828gCqRRoirrS+t0QYzxSRpci5x7JCfVKjuzHA So1aAl8dxz3LeFi1zf+uG1htVtaH//OLvWj0Rey8YY10jyeuZbVawWeShdRFEmEWMp X8enaFbnsAvtOSwXflXLqPlqEL22+y5reYcffxPIU0JcXYOJQkN/cxviSV5sW97+ou 3ItNT1+TMqwFXNxHvA8050B5bWXpIR5LKaBatPFLF4RRR7kkDHuBUmecooP4/OxRyh s/WEctiWRU/ow== Received: from S-MS-EXCH01.sberdevices.ru (S-MS-EXCH01.sberdevices.ru [172.16.1.4]) by mx.sberdevices.ru (Postfix) with ESMTP; Tue, 6 Jun 2023 13:22:06 +0300 (MSK) From: Arseniy Krasnov <AVKrasnov@sberdevices.ru> To: Liang Yang <liang.yang@amlogic.com>, Miquel Raynal <miquel.raynal@bootlin.com>, Richard Weinberger <richard@nod.at>, Vignesh Raghavendra <vigneshr@ti.com>, Neil Armstrong <neil.armstrong@linaro.org>, Kevin Hilman <khilman@baylibre.com>, Jerome Brunet <jbrunet@baylibre.com>, Martin Blumenstingl <martin.blumenstingl@googlemail.com> CC: <oxffffaa@gmail.com>, <kernel@sberdevices.ru>, Arseniy Krasnov <AVKrasnov@sberdevices.ru>, <linux-mtd@lists.infradead.org>, <linux-arm-kernel@lists.infradead.org>, <linux-amlogic@lists.infradead.org>, <linux-kernel@vger.kernel.org> Subject: [PATCH v2] mtd: rawnand: meson: check buffer length Date: Tue, 6 Jun 2023 13:16:43 +0300 Message-ID: <20230606101644.3297859-1-AVKrasnov@sberdevices.ru> X-Mailer: git-send-email 2.35.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [172.16.1.6] X-ClientProxiedBy: S-MS-EXCH01.sberdevices.ru (172.16.1.4) To S-MS-EXCH01.sberdevices.ru (172.16.1.4) X-KSMG-Rule-ID: 4 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiPhishing: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 1.1.2.30, bases: 2023/06/06 07:40:00 #21442908 X-KSMG-AntiVirus-Status: Clean, skipped X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767949357356164887?= X-GMAIL-MSGID: =?utf-8?q?1767949357356164887?= |
Series |
[v2] mtd: rawnand: meson: check buffer length
|
|
Commit Message
Arseniy Krasnov
June 6, 2023, 10:16 a.m. UTC
Meson NAND controller has limited buffer length, so check it before
command execution to avoid length trim. Also check MTD write size on
chip attach.
Signed-off-by: Arseniy Krasnov <AVKrasnov@sberdevices.ru>
---
drivers/mtd/nand/raw/meson_nand.c | 47 +++++++++++++++++++++++++++----
1 file changed, 42 insertions(+), 5 deletions(-)
Comments
Sorry, here is changelog: v1 -> v2: * Move checks from 'switch/case' which executes commands in 'meson_nfc_exec_op()' to a special separated function 'meson_nfc_check_op()' which is called before commands processing. On 06.06.2023 13:16, Arseniy Krasnov wrote: > Meson NAND controller has limited buffer length, so check it before > command execution to avoid length trim. Also check MTD write size on > chip attach. > > Signed-off-by: Arseniy Krasnov <AVKrasnov@sberdevices.ru> > --- > drivers/mtd/nand/raw/meson_nand.c | 47 +++++++++++++++++++++++++++---- > 1 file changed, 42 insertions(+), 5 deletions(-) > > diff --git a/drivers/mtd/nand/raw/meson_nand.c b/drivers/mtd/nand/raw/meson_nand.c > index 23a73268421b..db6b18753071 100644 > --- a/drivers/mtd/nand/raw/meson_nand.c > +++ b/drivers/mtd/nand/raw/meson_nand.c > @@ -111,6 +111,8 @@ > > #define PER_INFO_BYTE 8 > > +#define NFC_CMD_RAW_LEN GENMASK(13, 0) > + > struct meson_nfc_nand_chip { > struct list_head node; > struct nand_chip nand; > @@ -284,7 +286,7 @@ static void meson_nfc_cmd_access(struct nand_chip *nand, int raw, bool dir, > > if (raw) { > len = mtd->writesize + mtd->oobsize; > - cmd = (len & GENMASK(13, 0)) | scrambler | DMA_DIR(dir); > + cmd = len | scrambler | DMA_DIR(dir); > writel(cmd, nfc->reg_base + NFC_REG_CMD); > return; > } > @@ -573,7 +575,7 @@ static int meson_nfc_read_buf(struct nand_chip *nand, u8 *buf, int len) > if (ret) > goto out; > > - cmd = NFC_CMD_N2M | (len & GENMASK(13, 0)); > + cmd = NFC_CMD_N2M | len; > writel(cmd, nfc->reg_base + NFC_REG_CMD); > > meson_nfc_drain_cmd(nfc); > @@ -597,7 +599,7 @@ static int meson_nfc_write_buf(struct nand_chip *nand, u8 *buf, int len) > if (ret) > return ret; > > - cmd = NFC_CMD_M2N | (len & GENMASK(13, 0)); > + cmd = NFC_CMD_M2N | len; > writel(cmd, nfc->reg_base + NFC_REG_CMD); > > meson_nfc_drain_cmd(nfc); > @@ -1007,6 +1009,31 @@ meson_nand_op_put_dma_safe_output_buf(const struct nand_op_instr *instr, > kfree(buf); > } > > +static int meson_nfc_check_op(struct nand_chip *chip, > + const struct nand_operation *op) > +{ > + int op_id; > + > + for (op_id = 0; op_id < op->ninstrs; op_id++) { > + const struct nand_op_instr *instr; > + > + instr = &op->instrs[op_id]; > + > + switch (instr->type) { > + case NAND_OP_DATA_IN_INSTR: > + case NAND_OP_DATA_OUT_INSTR: > + if (instr->ctx.data.len > NFC_CMD_RAW_LEN) > + return -ENOTSUPP; > + > + break; > + default: > + break; > + } > + } > + > + return 0; > +} > + > static int meson_nfc_exec_op(struct nand_chip *nand, > const struct nand_operation *op, bool check_only) > { > @@ -1015,10 +1042,12 @@ static int meson_nfc_exec_op(struct nand_chip *nand, > const struct nand_op_instr *instr = NULL; > void *buf; > u32 op_id, delay_idle, cmd; > + int err; > int i; > > - if (check_only) > - return 0; > + err = meson_nfc_check_op(nand, op); > + if (err || check_only) > + return err; > > meson_nfc_select_chip(nand, op->cs); > for (op_id = 0; op_id < op->ninstrs; op_id++) { > @@ -1293,6 +1322,7 @@ static int meson_nand_attach_chip(struct nand_chip *nand) > struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); > struct mtd_info *mtd = nand_to_mtd(nand); > int nsectors = mtd->writesize / 1024; > + int raw_writesize; > int ret; > > if (!mtd->name) { > @@ -1304,6 +1334,13 @@ static int meson_nand_attach_chip(struct nand_chip *nand) > return -ENOMEM; > } > > + raw_writesize = mtd->writesize + mtd->oobsize; > + if (raw_writesize > NFC_CMD_RAW_LEN) { > + dev_err(nfc->dev, "too big write size in raw mode: %d > %ld\n", > + raw_writesize, NFC_CMD_RAW_LEN); > + return -EINVAL; > + } > + > if (nand->bbt_options & NAND_BBT_USE_FLASH) > nand->bbt_options |= NAND_BBT_NO_OOB; >
Hi again Miquel, Liang! What do You think about this patch? Thanks, Arseniy On 06.06.2023 19:29, Arseniy Krasnov wrote: > Sorry, here is changelog: > v1 -> v2: > * Move checks from 'switch/case' which executes commands in 'meson_nfc_exec_op()' to a special > separated function 'meson_nfc_check_op()' which is called before commands processing. > > On 06.06.2023 13:16, Arseniy Krasnov wrote: >> Meson NAND controller has limited buffer length, so check it before >> command execution to avoid length trim. Also check MTD write size on >> chip attach. >> >> Signed-off-by: Arseniy Krasnov <AVKrasnov@sberdevices.ru> >> --- >> drivers/mtd/nand/raw/meson_nand.c | 47 +++++++++++++++++++++++++++---- >> 1 file changed, 42 insertions(+), 5 deletions(-) >> >> diff --git a/drivers/mtd/nand/raw/meson_nand.c b/drivers/mtd/nand/raw/meson_nand.c >> index 23a73268421b..db6b18753071 100644 >> --- a/drivers/mtd/nand/raw/meson_nand.c >> +++ b/drivers/mtd/nand/raw/meson_nand.c >> @@ -111,6 +111,8 @@ >> >> #define PER_INFO_BYTE 8 >> >> +#define NFC_CMD_RAW_LEN GENMASK(13, 0) >> + >> struct meson_nfc_nand_chip { >> struct list_head node; >> struct nand_chip nand; >> @@ -284,7 +286,7 @@ static void meson_nfc_cmd_access(struct nand_chip *nand, int raw, bool dir, >> >> if (raw) { >> len = mtd->writesize + mtd->oobsize; >> - cmd = (len & GENMASK(13, 0)) | scrambler | DMA_DIR(dir); >> + cmd = len | scrambler | DMA_DIR(dir); >> writel(cmd, nfc->reg_base + NFC_REG_CMD); >> return; >> } >> @@ -573,7 +575,7 @@ static int meson_nfc_read_buf(struct nand_chip *nand, u8 *buf, int len) >> if (ret) >> goto out; >> >> - cmd = NFC_CMD_N2M | (len & GENMASK(13, 0)); >> + cmd = NFC_CMD_N2M | len; >> writel(cmd, nfc->reg_base + NFC_REG_CMD); >> >> meson_nfc_drain_cmd(nfc); >> @@ -597,7 +599,7 @@ static int meson_nfc_write_buf(struct nand_chip *nand, u8 *buf, int len) >> if (ret) >> return ret; >> >> - cmd = NFC_CMD_M2N | (len & GENMASK(13, 0)); >> + cmd = NFC_CMD_M2N | len; >> writel(cmd, nfc->reg_base + NFC_REG_CMD); >> >> meson_nfc_drain_cmd(nfc); >> @@ -1007,6 +1009,31 @@ meson_nand_op_put_dma_safe_output_buf(const struct nand_op_instr *instr, >> kfree(buf); >> } >> >> +static int meson_nfc_check_op(struct nand_chip *chip, >> + const struct nand_operation *op) >> +{ >> + int op_id; >> + >> + for (op_id = 0; op_id < op->ninstrs; op_id++) { >> + const struct nand_op_instr *instr; >> + >> + instr = &op->instrs[op_id]; >> + >> + switch (instr->type) { >> + case NAND_OP_DATA_IN_INSTR: >> + case NAND_OP_DATA_OUT_INSTR: >> + if (instr->ctx.data.len > NFC_CMD_RAW_LEN) >> + return -ENOTSUPP; >> + >> + break; >> + default: >> + break; >> + } >> + } >> + >> + return 0; >> +} >> + >> static int meson_nfc_exec_op(struct nand_chip *nand, >> const struct nand_operation *op, bool check_only) >> { >> @@ -1015,10 +1042,12 @@ static int meson_nfc_exec_op(struct nand_chip *nand, >> const struct nand_op_instr *instr = NULL; >> void *buf; >> u32 op_id, delay_idle, cmd; >> + int err; >> int i; >> >> - if (check_only) >> - return 0; >> + err = meson_nfc_check_op(nand, op); >> + if (err || check_only) >> + return err; >> >> meson_nfc_select_chip(nand, op->cs); >> for (op_id = 0; op_id < op->ninstrs; op_id++) { >> @@ -1293,6 +1322,7 @@ static int meson_nand_attach_chip(struct nand_chip *nand) >> struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); >> struct mtd_info *mtd = nand_to_mtd(nand); >> int nsectors = mtd->writesize / 1024; >> + int raw_writesize; >> int ret; >> >> if (!mtd->name) { >> @@ -1304,6 +1334,13 @@ static int meson_nand_attach_chip(struct nand_chip *nand) >> return -ENOMEM; >> } >> >> + raw_writesize = mtd->writesize + mtd->oobsize; >> + if (raw_writesize > NFC_CMD_RAW_LEN) { >> + dev_err(nfc->dev, "too big write size in raw mode: %d > %ld\n", >> + raw_writesize, NFC_CMD_RAW_LEN); >> + return -EINVAL; >> + } >> + >> if (nand->bbt_options & NAND_BBT_USE_FLASH) >> nand->bbt_options |= NAND_BBT_NO_OOB; >>
On 08.06.2023 09:10, Miquel Raynal wrote: > Hi Arseniy, > > avkrasnov@sberdevices.ru wrote on Thu, 8 Jun 2023 00:17:35 +0300: > >> Hi again Miquel, Liang! >> >> What do You think about this patch? > > I really appreciate all the effort you are putting into this but > please consider giving me a little bit of air as well, I'm already > spending a lot of time reviewing all your patches. Please mind a ping > is not relevant before a couple of weeks in general. Sorry for that! Sure, no problem! > > In this case I had it in my "to apply" list but actually looking at it > again I have a minor comment below. Got it! Thanks, Arseniy > >> >> Thanks, Arseniy >> >> On 06.06.2023 19:29, Arseniy Krasnov wrote: >>> Sorry, here is changelog: >>> v1 -> v2: >>> * Move checks from 'switch/case' which executes commands in 'meson_nfc_exec_op()' to a special >>> separated function 'meson_nfc_check_op()' which is called before commands processing. >>> >>> On 06.06.2023 13:16, Arseniy Krasnov wrote: >>>> Meson NAND controller has limited buffer length, so check it before >>>> command execution to avoid length trim. Also check MTD write size on >>>> chip attach. >>>> >>>> Signed-off-by: Arseniy Krasnov <AVKrasnov@sberdevices.ru> >>>> --- >>>> drivers/mtd/nand/raw/meson_nand.c | 47 +++++++++++++++++++++++++++---- >>>> 1 file changed, 42 insertions(+), 5 deletions(-) >>>> >>>> diff --git a/drivers/mtd/nand/raw/meson_nand.c b/drivers/mtd/nand/raw/meson_nand.c >>>> index 23a73268421b..db6b18753071 100644 >>>> --- a/drivers/mtd/nand/raw/meson_nand.c >>>> +++ b/drivers/mtd/nand/raw/meson_nand.c >>>> @@ -111,6 +111,8 @@ >>>> >>>> #define PER_INFO_BYTE 8 >>>> >>>> +#define NFC_CMD_RAW_LEN GENMASK(13, 0) >>>> + >>>> struct meson_nfc_nand_chip { >>>> struct list_head node; >>>> struct nand_chip nand; >>>> @@ -284,7 +286,7 @@ static void meson_nfc_cmd_access(struct nand_chip *nand, int raw, bool dir, >>>> >>>> if (raw) { >>>> len = mtd->writesize + mtd->oobsize; >>>> - cmd = (len & GENMASK(13, 0)) | scrambler | DMA_DIR(dir); >>>> + cmd = len | scrambler | DMA_DIR(dir); >>>> writel(cmd, nfc->reg_base + NFC_REG_CMD); >>>> return; >>>> } >>>> @@ -573,7 +575,7 @@ static int meson_nfc_read_buf(struct nand_chip *nand, u8 *buf, int len) >>>> if (ret) >>>> goto out; >>>> >>>> - cmd = NFC_CMD_N2M | (len & GENMASK(13, 0)); >>>> + cmd = NFC_CMD_N2M | len; >>>> writel(cmd, nfc->reg_base + NFC_REG_CMD); >>>> >>>> meson_nfc_drain_cmd(nfc); >>>> @@ -597,7 +599,7 @@ static int meson_nfc_write_buf(struct nand_chip *nand, u8 *buf, int len) >>>> if (ret) >>>> return ret; >>>> >>>> - cmd = NFC_CMD_M2N | (len & GENMASK(13, 0)); >>>> + cmd = NFC_CMD_M2N | len; >>>> writel(cmd, nfc->reg_base + NFC_REG_CMD); >>>> >>>> meson_nfc_drain_cmd(nfc); >>>> @@ -1007,6 +1009,31 @@ meson_nand_op_put_dma_safe_output_buf(const struct nand_op_instr *instr, >>>> kfree(buf); >>>> } >>>> >>>> +static int meson_nfc_check_op(struct nand_chip *chip, >>>> + const struct nand_operation *op) >>>> +{ >>>> + int op_id; >>>> + >>>> + for (op_id = 0; op_id < op->ninstrs; op_id++) { >>>> + const struct nand_op_instr *instr; >>>> + >>>> + instr = &op->instrs[op_id]; >>>> + >>>> + switch (instr->type) { >>>> + case NAND_OP_DATA_IN_INSTR: >>>> + case NAND_OP_DATA_OUT_INSTR: >>>> + if (instr->ctx.data.len > NFC_CMD_RAW_LEN) >>>> + return -ENOTSUPP; >>>> + >>>> + break; >>>> + default: >>>> + break; >>>> + } >>>> + } >>>> + >>>> + return 0; >>>> +} >>>> + >>>> static int meson_nfc_exec_op(struct nand_chip *nand, >>>> const struct nand_operation *op, bool check_only) >>>> { >>>> @@ -1015,10 +1042,12 @@ static int meson_nfc_exec_op(struct nand_chip *nand, >>>> const struct nand_op_instr *instr = NULL; >>>> void *buf; >>>> u32 op_id, delay_idle, cmd; >>>> + int err; >>>> int i; >>>> >>>> - if (check_only) >>>> - return 0; >>>> + err = meson_nfc_check_op(nand, op); >>>> + if (err || check_only) >>>> + return err; > > For the sake of readability it is really important that we keep using > standard constructions, so don't mix orthogonal checks and please use > something like: > > err = check(); > if (err) > return err; > > if (check_only) > return 0; > >>>> >>>> meson_nfc_select_chip(nand, op->cs); >>>> for (op_id = 0; op_id < op->ninstrs; op_id++) { >>>> @@ -1293,6 +1322,7 @@ static int meson_nand_attach_chip(struct nand_chip *nand) >>>> struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); >>>> struct mtd_info *mtd = nand_to_mtd(nand); >>>> int nsectors = mtd->writesize / 1024; >>>> + int raw_writesize; >>>> int ret; >>>> >>>> if (!mtd->name) { >>>> @@ -1304,6 +1334,13 @@ static int meson_nand_attach_chip(struct nand_chip *nand) >>>> return -ENOMEM; >>>> } >>>> >>>> + raw_writesize = mtd->writesize + mtd->oobsize; >>>> + if (raw_writesize > NFC_CMD_RAW_LEN) { >>>> + dev_err(nfc->dev, "too big write size in raw mode: %d > %ld\n", >>>> + raw_writesize, NFC_CMD_RAW_LEN); >>>> + return -EINVAL; >>>> + } >>>> + >>>> if (nand->bbt_options & NAND_BBT_USE_FLASH) >>>> nand->bbt_options |= NAND_BBT_NO_OOB; >>>> > > > Thanks, > Miquèl
Hi Arseniy, avkrasnov@sberdevices.ru wrote on Thu, 8 Jun 2023 00:17:35 +0300: > Hi again Miquel, Liang! > > What do You think about this patch? I really appreciate all the effort you are putting into this but please consider giving me a little bit of air as well, I'm already spending a lot of time reviewing all your patches. Please mind a ping is not relevant before a couple of weeks in general. In this case I had it in my "to apply" list but actually looking at it again I have a minor comment below. > > Thanks, Arseniy > > On 06.06.2023 19:29, Arseniy Krasnov wrote: > > Sorry, here is changelog: > > v1 -> v2: > > * Move checks from 'switch/case' which executes commands in 'meson_nfc_exec_op()' to a special > > separated function 'meson_nfc_check_op()' which is called before commands processing. > > > > On 06.06.2023 13:16, Arseniy Krasnov wrote: > >> Meson NAND controller has limited buffer length, so check it before > >> command execution to avoid length trim. Also check MTD write size on > >> chip attach. > >> > >> Signed-off-by: Arseniy Krasnov <AVKrasnov@sberdevices.ru> > >> --- > >> drivers/mtd/nand/raw/meson_nand.c | 47 +++++++++++++++++++++++++++---- > >> 1 file changed, 42 insertions(+), 5 deletions(-) > >> > >> diff --git a/drivers/mtd/nand/raw/meson_nand.c b/drivers/mtd/nand/raw/meson_nand.c > >> index 23a73268421b..db6b18753071 100644 > >> --- a/drivers/mtd/nand/raw/meson_nand.c > >> +++ b/drivers/mtd/nand/raw/meson_nand.c > >> @@ -111,6 +111,8 @@ > >> > >> #define PER_INFO_BYTE 8 > >> > >> +#define NFC_CMD_RAW_LEN GENMASK(13, 0) > >> + > >> struct meson_nfc_nand_chip { > >> struct list_head node; > >> struct nand_chip nand; > >> @@ -284,7 +286,7 @@ static void meson_nfc_cmd_access(struct nand_chip *nand, int raw, bool dir, > >> > >> if (raw) { > >> len = mtd->writesize + mtd->oobsize; > >> - cmd = (len & GENMASK(13, 0)) | scrambler | DMA_DIR(dir); > >> + cmd = len | scrambler | DMA_DIR(dir); > >> writel(cmd, nfc->reg_base + NFC_REG_CMD); > >> return; > >> } > >> @@ -573,7 +575,7 @@ static int meson_nfc_read_buf(struct nand_chip *nand, u8 *buf, int len) > >> if (ret) > >> goto out; > >> > >> - cmd = NFC_CMD_N2M | (len & GENMASK(13, 0)); > >> + cmd = NFC_CMD_N2M | len; > >> writel(cmd, nfc->reg_base + NFC_REG_CMD); > >> > >> meson_nfc_drain_cmd(nfc); > >> @@ -597,7 +599,7 @@ static int meson_nfc_write_buf(struct nand_chip *nand, u8 *buf, int len) > >> if (ret) > >> return ret; > >> > >> - cmd = NFC_CMD_M2N | (len & GENMASK(13, 0)); > >> + cmd = NFC_CMD_M2N | len; > >> writel(cmd, nfc->reg_base + NFC_REG_CMD); > >> > >> meson_nfc_drain_cmd(nfc); > >> @@ -1007,6 +1009,31 @@ meson_nand_op_put_dma_safe_output_buf(const struct nand_op_instr *instr, > >> kfree(buf); > >> } > >> > >> +static int meson_nfc_check_op(struct nand_chip *chip, > >> + const struct nand_operation *op) > >> +{ > >> + int op_id; > >> + > >> + for (op_id = 0; op_id < op->ninstrs; op_id++) { > >> + const struct nand_op_instr *instr; > >> + > >> + instr = &op->instrs[op_id]; > >> + > >> + switch (instr->type) { > >> + case NAND_OP_DATA_IN_INSTR: > >> + case NAND_OP_DATA_OUT_INSTR: > >> + if (instr->ctx.data.len > NFC_CMD_RAW_LEN) > >> + return -ENOTSUPP; > >> + > >> + break; > >> + default: > >> + break; > >> + } > >> + } > >> + > >> + return 0; > >> +} > >> + > >> static int meson_nfc_exec_op(struct nand_chip *nand, > >> const struct nand_operation *op, bool check_only) > >> { > >> @@ -1015,10 +1042,12 @@ static int meson_nfc_exec_op(struct nand_chip *nand, > >> const struct nand_op_instr *instr = NULL; > >> void *buf; > >> u32 op_id, delay_idle, cmd; > >> + int err; > >> int i; > >> > >> - if (check_only) > >> - return 0; > >> + err = meson_nfc_check_op(nand, op); > >> + if (err || check_only) > >> + return err; For the sake of readability it is really important that we keep using standard constructions, so don't mix orthogonal checks and please use something like: err = check(); if (err) return err; if (check_only) return 0; > >> > >> meson_nfc_select_chip(nand, op->cs); > >> for (op_id = 0; op_id < op->ninstrs; op_id++) { > >> @@ -1293,6 +1322,7 @@ static int meson_nand_attach_chip(struct nand_chip *nand) > >> struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); > >> struct mtd_info *mtd = nand_to_mtd(nand); > >> int nsectors = mtd->writesize / 1024; > >> + int raw_writesize; > >> int ret; > >> > >> if (!mtd->name) { > >> @@ -1304,6 +1334,13 @@ static int meson_nand_attach_chip(struct nand_chip *nand) > >> return -ENOMEM; > >> } > >> > >> + raw_writesize = mtd->writesize + mtd->oobsize; > >> + if (raw_writesize > NFC_CMD_RAW_LEN) { > >> + dev_err(nfc->dev, "too big write size in raw mode: %d > %ld\n", > >> + raw_writesize, NFC_CMD_RAW_LEN); > >> + return -EINVAL; > >> + } > >> + > >> if (nand->bbt_options & NAND_BBT_USE_FLASH) > >> nand->bbt_options |= NAND_BBT_NO_OOB; > >> Thanks, Miquèl
Hi Arseniy, On 2023/6/8 5:17, Arseniy Krasnov wrote: > [ EXTERNAL EMAIL ] > > Hi again Miquel, Liang! > > What do You think about this patch? > > Thanks, Arseniy > > On 06.06.2023 19:29, Arseniy Krasnov wrote: >> Sorry, here is changelog: >> v1 -> v2: >> * Move checks from 'switch/case' which executes commands in 'meson_nfc_exec_op()' to a special >> separated function 'meson_nfc_check_op()' which is called before commands processing. >> >> On 06.06.2023 13:16, Arseniy Krasnov wrote: >>> Meson NAND controller has limited buffer length, so check it before >>> command execution to avoid length trim. Also check MTD write size on >>> chip attach. >>> >>> Signed-off-by: Arseniy Krasnov <AVKrasnov@sberdevices.ru> >>> --- >>> drivers/mtd/nand/raw/meson_nand.c | 47 +++++++++++++++++++++++++++---- >>> 1 file changed, 42 insertions(+), 5 deletions(-) >>> >>> diff --git a/drivers/mtd/nand/raw/meson_nand.c b/drivers/mtd/nand/raw/meson_nand.c >>> index 23a73268421b..db6b18753071 100644 >>> --- a/drivers/mtd/nand/raw/meson_nand.c >>> +++ b/drivers/mtd/nand/raw/meson_nand.c >>> @@ -111,6 +111,8 @@ >>> >>> #define PER_INFO_BYTE 8 >>> >>> +#define NFC_CMD_RAW_LEN GENMASK(13, 0) >>> + >>> struct meson_nfc_nand_chip { >>> struct list_head node; >>> struct nand_chip nand; >>> @@ -284,7 +286,7 @@ static void meson_nfc_cmd_access(struct nand_chip *nand, int raw, bool dir, >>> >>> if (raw) { >>> len = mtd->writesize + mtd->oobsize; >>> - cmd = (len & GENMASK(13, 0)) | scrambler | DMA_DIR(dir); >>> + cmd = len | scrambler | DMA_DIR(dir); >>> writel(cmd, nfc->reg_base + NFC_REG_CMD); I think we could keep "& GENMASK(13, 0)". it is better here to indicate how many bits of length in this command and don't destory the command once we don't check the "len" outside of this function. or the code "if (len > NFC_CMD_RAW_LEN)" is better to put inside this function nearly. Thanks. >>> return; >>> } >>> @@ -573,7 +575,7 @@ static int meson_nfc_read_buf(struct nand_chip *nand, u8 *buf, int len) >>> if (ret) >>> goto out; >>> >>> - cmd = NFC_CMD_N2M | (len & GENMASK(13, 0)); >>> + cmd = NFC_CMD_N2M | len; >>> writel(cmd, nfc->reg_base + NFC_REG_CMD); >>> >>> meson_nfc_drain_cmd(nfc); >>> @@ -597,7 +599,7 @@ static int meson_nfc_write_buf(struct nand_chip *nand, u8 *buf, int len) >>> if (ret) >>> return ret; >>> >>> - cmd = NFC_CMD_M2N | (len & GENMASK(13, 0)); >>> + cmd = NFC_CMD_M2N | len; >>> writel(cmd, nfc->reg_base + NFC_REG_CMD); >>> >>> meson_nfc_drain_cmd(nfc); >>> @@ -1007,6 +1009,31 @@ meson_nand_op_put_dma_safe_output_buf(const struct nand_op_instr *instr, >>> kfree(buf); >>> } >>> >>> +static int meson_nfc_check_op(struct nand_chip *chip, >>> + const struct nand_operation *op) >>> +{ >>> + int op_id; >>> + >>> + for (op_id = 0; op_id < op->ninstrs; op_id++) { >>> + const struct nand_op_instr *instr; >>> + >>> + instr = &op->instrs[op_id]; >>> + >>> + switch (instr->type) { >>> + case NAND_OP_DATA_IN_INSTR: >>> + case NAND_OP_DATA_OUT_INSTR: >>> + if (instr->ctx.data.len > NFC_CMD_RAW_LEN) >>> + return -ENOTSUPP; >>> + >>> + break; >>> + default: >>> + break; >>> + } >>> + } >>> + >>> + return 0; >>> +} >>> + >>> static int meson_nfc_exec_op(struct nand_chip *nand, >>> const struct nand_operation *op, bool check_only) >>> { >>> @@ -1015,10 +1042,12 @@ static int meson_nfc_exec_op(struct nand_chip *nand, >>> const struct nand_op_instr *instr = NULL; >>> void *buf; >>> u32 op_id, delay_idle, cmd; >>> + int err; >>> int i; >>> >>> - if (check_only) >>> - return 0; >>> + err = meson_nfc_check_op(nand, op); >>> + if (err || check_only) >>> + return err; >>> >>> meson_nfc_select_chip(nand, op->cs); >>> for (op_id = 0; op_id < op->ninstrs; op_id++) { >>> @@ -1293,6 +1322,7 @@ static int meson_nand_attach_chip(struct nand_chip *nand) >>> struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); >>> struct mtd_info *mtd = nand_to_mtd(nand); >>> int nsectors = mtd->writesize / 1024; >>> + int raw_writesize; >>> int ret; >>> >>> if (!mtd->name) { >>> @@ -1304,6 +1334,13 @@ static int meson_nand_attach_chip(struct nand_chip *nand) >>> return -ENOMEM; >>> } >>> >>> + raw_writesize = mtd->writesize + mtd->oobsize; >>> + if (raw_writesize > NFC_CMD_RAW_LEN) { >>> + dev_err(nfc->dev, "too big write size in raw mode: %d > %ld\n", >>> + raw_writesize, NFC_CMD_RAW_LEN); >>> + return -EINVAL; >>> + } >>> + >>> if (nand->bbt_options & NAND_BBT_USE_FLASH) >>> nand->bbt_options |= NAND_BBT_NO_OOB; >>>
Hi Liang, liang.yang@amlogic.com wrote on Thu, 8 Jun 2023 14:42:53 +0800: > Hi Arseniy, > > On 2023/6/8 5:17, Arseniy Krasnov wrote: > > [ EXTERNAL EMAIL ] > > > > Hi again Miquel, Liang! > > > > What do You think about this patch? > > > > Thanks, Arseniy > > > > On 06.06.2023 19:29, Arseniy Krasnov wrote: > >> Sorry, here is changelog: > >> v1 -> v2: > >> * Move checks from 'switch/case' which executes commands in 'meson_nfc_exec_op()' to a special > >> separated function 'meson_nfc_check_op()' which is called before commands processing. > >> > >> On 06.06.2023 13:16, Arseniy Krasnov wrote: > >>> Meson NAND controller has limited buffer length, so check it before > >>> command execution to avoid length trim. Also check MTD write size on > >>> chip attach. > >>> > >>> Signed-off-by: Arseniy Krasnov <AVKrasnov@sberdevices.ru> > >>> --- > >>> drivers/mtd/nand/raw/meson_nand.c | 47 +++++++++++++++++++++++++++---- > >>> 1 file changed, 42 insertions(+), 5 deletions(-) > >>> > >>> diff --git a/drivers/mtd/nand/raw/meson_nand.c b/drivers/mtd/nand/raw/meson_nand.c > >>> index 23a73268421b..db6b18753071 100644 > >>> --- a/drivers/mtd/nand/raw/meson_nand.c > >>> +++ b/drivers/mtd/nand/raw/meson_nand.c > >>> @@ -111,6 +111,8 @@ > >>> > >>> #define PER_INFO_BYTE 8 > >>> > >>> +#define NFC_CMD_RAW_LEN GENMASK(13, 0) > >>> + > >>> struct meson_nfc_nand_chip { > >>> struct list_head node; > >>> struct nand_chip nand; > >>> @@ -284,7 +286,7 @@ static void meson_nfc_cmd_access(struct nand_chip *nand, int raw, bool dir, > >>> > >>> if (raw) { > >>> len = mtd->writesize + mtd->oobsize; > >>> - cmd = (len & GENMASK(13, 0)) | scrambler | DMA_DIR(dir); > >>> + cmd = len | scrambler | DMA_DIR(dir); > >>> writel(cmd, nfc->reg_base + NFC_REG_CMD); > > I think we could keep "& GENMASK(13, 0)". it is better here to indicate how many bits of length in this command and don't destory the command once we don't check the "len" outside of this function. or the code "if (len > NFC_CMD_RAW_LEN)" is better to put inside this function nearly. Thanks. It depends who calls this helper. If only used inside exec_op, it's not useful. If used outside, then if you want to clarify things you may want to use: #define NFC_CMD_OP_LEN(cmd) FIELD_PREP(GENMASK(13, 0), (cmd)) This is by far my favorite construction. Could apply to many other fields, like DMA_DIR, scrambler, etc. > >>> return; > >>> } > >>> @@ -573,7 +575,7 @@ static int meson_nfc_read_buf(struct nand_chip *nand, u8 *buf, int len) > >>> if (ret) > >>> goto out; > >>> > >>> - cmd = NFC_CMD_N2M | (len & GENMASK(13, 0)); > >>> + cmd = NFC_CMD_N2M | len; > >>> writel(cmd, nfc->reg_base + NFC_REG_CMD); > >>> > >>> meson_nfc_drain_cmd(nfc); > >>> @@ -597,7 +599,7 @@ static int meson_nfc_write_buf(struct nand_chip *nand, u8 *buf, int len) > >>> if (ret) > >>> return ret; > >>> > >>> - cmd = NFC_CMD_M2N | (len & GENMASK(13, 0)); > >>> + cmd = NFC_CMD_M2N | len; > >>> writel(cmd, nfc->reg_base + NFC_REG_CMD); > >>> > >>> meson_nfc_drain_cmd(nfc); > >>> @@ -1007,6 +1009,31 @@ meson_nand_op_put_dma_safe_output_buf(const struct nand_op_instr *instr, > >>> kfree(buf); > >>> } > >>> > >>> +static int meson_nfc_check_op(struct nand_chip *chip, > >>> + const struct nand_operation *op) > >>> +{ > >>> + int op_id; > >>> + > >>> + for (op_id = 0; op_id < op->ninstrs; op_id++) { > >>> + const struct nand_op_instr *instr; > >>> + > >>> + instr = &op->instrs[op_id]; > >>> + > >>> + switch (instr->type) { > >>> + case NAND_OP_DATA_IN_INSTR: > >>> + case NAND_OP_DATA_OUT_INSTR: > >>> + if (instr->ctx.data.len > NFC_CMD_RAW_LEN) > >>> + return -ENOTSUPP; > >>> + > >>> + break; > >>> + default: > >>> + break; > >>> + } > >>> + } > >>> + > >>> + return 0; > >>> +} > >>> + > >>> static int meson_nfc_exec_op(struct nand_chip *nand, > >>> const struct nand_operation *op, bool check_only) > >>> { > >>> @@ -1015,10 +1042,12 @@ static int meson_nfc_exec_op(struct nand_chip *nand, > >>> const struct nand_op_instr *instr = NULL; > >>> void *buf; > >>> u32 op_id, delay_idle, cmd; > >>> + int err; > >>> int i; > >>> > >>> - if (check_only) > >>> - return 0; > >>> + err = meson_nfc_check_op(nand, op); > >>> + if (err || check_only) > >>> + return err; > >>> > >>> meson_nfc_select_chip(nand, op->cs); > >>> for (op_id = 0; op_id < op->ninstrs; op_id++) { > >>> @@ -1293,6 +1322,7 @@ static int meson_nand_attach_chip(struct nand_chip *nand) > >>> struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); > >>> struct mtd_info *mtd = nand_to_mtd(nand); > >>> int nsectors = mtd->writesize / 1024; > >>> + int raw_writesize; > >>> int ret; > >>> > >>> if (!mtd->name) { > >>> @@ -1304,6 +1334,13 @@ static int meson_nand_attach_chip(struct nand_chip *nand) > >>> return -ENOMEM; > >>> } > >>> > >>> + raw_writesize = mtd->writesize + mtd->oobsize; > >>> + if (raw_writesize > NFC_CMD_RAW_LEN) { > >>> + dev_err(nfc->dev, "too big write size in raw mode: %d > %ld\n", > >>> + raw_writesize, NFC_CMD_RAW_LEN); > >>> + return -EINVAL; > >>> + } > >>> + > >>> if (nand->bbt_options & NAND_BBT_USE_FLASH) > >>> nand->bbt_options |= NAND_BBT_NO_OOB; > >>> Thanks, Miquèl
Hi Miquel, On 2023/6/8 14:54, Miquel Raynal wrote: > [ EXTERNAL EMAIL ] > > Hi Liang, > > liang.yang@amlogic.com wrote on Thu, 8 Jun 2023 14:42:53 +0800: > >> Hi Arseniy, >> >> On 2023/6/8 5:17, Arseniy Krasnov wrote: >>> [ EXTERNAL EMAIL ] >>> >>> Hi again Miquel, Liang! >>> >>> What do You think about this patch? >>> >>> Thanks, Arseniy >>> >>> On 06.06.2023 19:29, Arseniy Krasnov wrote: >>>> Sorry, here is changelog: >>>> v1 -> v2: >>>> * Move checks from 'switch/case' which executes commands in 'meson_nfc_exec_op()' to a special >>>> separated function 'meson_nfc_check_op()' which is called before commands processing. >>>> >>>> On 06.06.2023 13:16, Arseniy Krasnov wrote: >>>>> Meson NAND controller has limited buffer length, so check it before >>>>> command execution to avoid length trim. Also check MTD write size on >>>>> chip attach. >>>>> >>>>> Signed-off-by: Arseniy Krasnov <AVKrasnov@sberdevices.ru> >>>>> --- >>>>> drivers/mtd/nand/raw/meson_nand.c | 47 +++++++++++++++++++++++++++---- >>>>> 1 file changed, 42 insertions(+), 5 deletions(-) >>>>> >>>>> diff --git a/drivers/mtd/nand/raw/meson_nand.c b/drivers/mtd/nand/raw/meson_nand.c >>>>> index 23a73268421b..db6b18753071 100644 >>>>> --- a/drivers/mtd/nand/raw/meson_nand.c >>>>> +++ b/drivers/mtd/nand/raw/meson_nand.c >>>>> @@ -111,6 +111,8 @@ >>>>> >>>>> #define PER_INFO_BYTE 8 >>>>> >>>>> +#define NFC_CMD_RAW_LEN GENMASK(13, 0) >>>>> + >>>>> struct meson_nfc_nand_chip { >>>>> struct list_head node; >>>>> struct nand_chip nand; >>>>> @@ -284,7 +286,7 @@ static void meson_nfc_cmd_access(struct nand_chip *nand, int raw, bool dir, >>>>> >>>>> if (raw) { >>>>> len = mtd->writesize + mtd->oobsize; >>>>> - cmd = (len & GENMASK(13, 0)) | scrambler | DMA_DIR(dir); >>>>> + cmd = len | scrambler | DMA_DIR(dir); >>>>> writel(cmd, nfc->reg_base + NFC_REG_CMD); >> >> I think we could keep "& GENMASK(13, 0)". it is better here to indicate how many bits of length in this command and don't destory the command once we don't check the "len" outside of this function. or the code "if (len > NFC_CMD_RAW_LEN)" is better to put inside this function nearly. Thanks. > > It depends who calls this helper. If only used inside exec_op,write_page_raw() and read_page_raw() also call meson_nfc_cmd_access(), but i don't find where constrain the "len". > it's not useful. If used outside, then if you want to clarify > things you may want to use: > > #define NFC_CMD_OP_LEN(cmd) FIELD_PREP(GENMASK(13, 0), (cmd)) > > This is by far my favorite construction. Could apply to many other > fields, like DMA_DIR, scrambler, etc. > >>>>> return; >>>>> } >>>>> @@ -573,7 +575,7 @@ static int meson_nfc_read_buf(struct nand_chip *nand, u8 *buf, int len) >>>>> if (ret) >>>>> goto out; >>>>> >>>>> - cmd = NFC_CMD_N2M | (len & GENMASK(13, 0)); >>>>> + cmd = NFC_CMD_N2M | len; >>>>> writel(cmd, nfc->reg_base + NFC_REG_CMD); >>>>> >>>>> meson_nfc_drain_cmd(nfc); >>>>> @@ -597,7 +599,7 @@ static int meson_nfc_write_buf(struct nand_chip *nand, u8 *buf, int len) >>>>> if (ret) >>>>> return ret; >>>>> >>>>> - cmd = NFC_CMD_M2N | (len & GENMASK(13, 0)); >>>>> + cmd = NFC_CMD_M2N | len; >>>>> writel(cmd, nfc->reg_base + NFC_REG_CMD); >>>>> >>>>> meson_nfc_drain_cmd(nfc); >>>>> @@ -1007,6 +1009,31 @@ meson_nand_op_put_dma_safe_output_buf(const struct nand_op_instr *instr, >>>>> kfree(buf); >>>>> } >>>>> >>>>> +static int meson_nfc_check_op(struct nand_chip *chip, >>>>> + const struct nand_operation *op) >>>>> +{ >>>>> + int op_id; >>>>> + >>>>> + for (op_id = 0; op_id < op->ninstrs; op_id++) { >>>>> + const struct nand_op_instr *instr; >>>>> + >>>>> + instr = &op->instrs[op_id]; >>>>> + >>>>> + switch (instr->type) { >>>>> + case NAND_OP_DATA_IN_INSTR: >>>>> + case NAND_OP_DATA_OUT_INSTR: >>>>> + if (instr->ctx.data.len > NFC_CMD_RAW_LEN) >>>>> + return -ENOTSUPP; >>>>> + >>>>> + break; >>>>> + default: >>>>> + break; >>>>> + } >>>>> + } >>>>> + >>>>> + return 0; >>>>> +} >>>>> + >>>>> static int meson_nfc_exec_op(struct nand_chip *nand, >>>>> const struct nand_operation *op, bool check_only) >>>>> { >>>>> @@ -1015,10 +1042,12 @@ static int meson_nfc_exec_op(struct nand_chip *nand, >>>>> const struct nand_op_instr *instr = NULL; >>>>> void *buf; >>>>> u32 op_id, delay_idle, cmd; >>>>> + int err; >>>>> int i; >>>>> >>>>> - if (check_only) >>>>> - return 0; >>>>> + err = meson_nfc_check_op(nand, op); >>>>> + if (err || check_only) >>>>> + return err; >>>>> >>>>> meson_nfc_select_chip(nand, op->cs); >>>>> for (op_id = 0; op_id < op->ninstrs; op_id++) { >>>>> @@ -1293,6 +1322,7 @@ static int meson_nand_attach_chip(struct nand_chip *nand) >>>>> struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); >>>>> struct mtd_info *mtd = nand_to_mtd(nand); >>>>> int nsectors = mtd->writesize / 1024; >>>>> + int raw_writesize; >>>>> int ret; >>>>> >>>>> if (!mtd->name) { >>>>> @@ -1304,6 +1334,13 @@ static int meson_nand_attach_chip(struct nand_chip *nand) >>>>> return -ENOMEM; >>>>> } >>>>> >>>>> + raw_writesize = mtd->writesize + mtd->oobsize; >>>>> + if (raw_writesize > NFC_CMD_RAW_LEN) { >>>>> + dev_err(nfc->dev, "too big write size in raw mode: %d > %ld\n", >>>>> + raw_writesize, NFC_CMD_RAW_LEN); >>>>> + return -EINVAL; >>>>> + } >>>>> + >>>>> if (nand->bbt_options & NAND_BBT_USE_FLASH) >>>>> nand->bbt_options |= NAND_BBT_NO_OOB; >>>>> > > > Thanks, > Miquèl
Hi Arseniy and Miquel, On 2023/6/8 15:12, Liang Yang wrote: > Hi Miquel, > > On 2023/6/8 14:54, Miquel Raynal wrote: >> [ EXTERNAL EMAIL ] >> >> Hi Liang, >> >> liang.yang@amlogic.com wrote on Thu, 8 Jun 2023 14:42:53 +0800: >> >>> Hi Arseniy, >>> >>> On 2023/6/8 5:17, Arseniy Krasnov wrote: >>>> [ EXTERNAL EMAIL ] >>>> >>>> Hi again Miquel, Liang! >>>> >>>> What do You think about this patch? >>>> >>>> Thanks, Arseniy >>>> >>>> On 06.06.2023 19:29, Arseniy Krasnov wrote: >>>>> Sorry, here is changelog: >>>>> v1 -> v2: >>>>> * Move checks from 'switch/case' which executes commands in >>>>> 'meson_nfc_exec_op()' to a special >>>>> separated function 'meson_nfc_check_op()' which is called >>>>> before commands processing. >>>>> >>>>> On 06.06.2023 13:16, Arseniy Krasnov wrote: >>>>>> Meson NAND controller has limited buffer length, so check it before >>>>>> command execution to avoid length trim. Also check MTD write size on >>>>>> chip attach. >>>>>> >>>>>> Signed-off-by: Arseniy Krasnov <AVKrasnov@sberdevices.ru> >>>>>> --- >>>>>> drivers/mtd/nand/raw/meson_nand.c | 47 >>>>>> +++++++++++++++++++++++++++---- >>>>>> 1 file changed, 42 insertions(+), 5 deletions(-) >>>>>> >>>>>> diff --git a/drivers/mtd/nand/raw/meson_nand.c >>>>>> b/drivers/mtd/nand/raw/meson_nand.c >>>>>> index 23a73268421b..db6b18753071 100644 >>>>>> --- a/drivers/mtd/nand/raw/meson_nand.c >>>>>> +++ b/drivers/mtd/nand/raw/meson_nand.c >>>>>> @@ -111,6 +111,8 @@ >>>>>> >>>>>> #define PER_INFO_BYTE 8 >>>>>> >>>>>> +#define NFC_CMD_RAW_LEN GENMASK(13, 0) >>>>>> + >>>>>> struct meson_nfc_nand_chip { >>>>>> struct list_head node; >>>>>> struct nand_chip nand; >>>>>> @@ -284,7 +286,7 @@ static void meson_nfc_cmd_access(struct >>>>>> nand_chip *nand, int raw, bool dir, >>>>>> >>>>>> if (raw) { >>>>>> len = mtd->writesize + mtd->oobsize; >>>>>> - cmd = (len & GENMASK(13, 0)) | scrambler | DMA_DIR(dir); >>>>>> + cmd = len | scrambler | DMA_DIR(dir); >>>>>> writel(cmd, nfc->reg_base + NFC_REG_CMD); >>> >>> I think we could keep "& GENMASK(13, 0)". it is better here to >>> indicate how many bits of length in this command and don't destory >>> the command once we don't check the "len" outside of this function. >>> or the code "if (len > NFC_CMD_RAW_LEN)" is better to put inside >>> this function nearly. Thanks. >> >> It depends who calls this helper. If only used inside >> exec_op,write_page_raw() and read_page_raw() also call >> meson_nfc_cmd_access(), > but i don't find where constrain the "len". Is the helper "meson_nfc_check_op()" needed by exec_op() after the constraint in attach_chip()? the length passed in exec_op() seems smaller than "mtd->writesize + mtd->oobsize" now. @Arseniy if it does need, I think the same constraint is needed by "meson_nfc_cmd_access()" > >> it's not useful. If used outside, then if you want to clarify >> things you may want to use: >> >> #define NFC_CMD_OP_LEN(cmd) FIELD_PREP(GENMASK(13, 0), (cmd)) >> >> This is by far my favorite construction. Could apply to many other >> fields, like DMA_DIR, scrambler, etc. @Miquel, FIELD_PREP() is better, but i have no idea whether we should add FIELD_PREP() in this patch, or keep the previous code. >> >>>>>> return; >>>>>> } >>>>>> @@ -573,7 +575,7 @@ static int meson_nfc_read_buf(struct nand_chip >>>>>> *nand, u8 *buf, int len) >>>>>> if (ret) >>>>>> goto out; >>>>>> >>>>>> - cmd = NFC_CMD_N2M | (len & GENMASK(13, 0)); >>>>>> + cmd = NFC_CMD_N2M | len; >>>>>> writel(cmd, nfc->reg_base + NFC_REG_CMD); >>>>>> >>>>>> meson_nfc_drain_cmd(nfc); >>>>>> @@ -597,7 +599,7 @@ static int meson_nfc_write_buf(struct >>>>>> nand_chip *nand, u8 *buf, int len) >>>>>> if (ret) >>>>>> return ret; >>>>>> >>>>>> - cmd = NFC_CMD_M2N | (len & GENMASK(13, 0)); >>>>>> + cmd = NFC_CMD_M2N | len; >>>>>> writel(cmd, nfc->reg_base + NFC_REG_CMD); >>>>>> >>>>>> meson_nfc_drain_cmd(nfc); >>>>>> @@ -1007,6 +1009,31 @@ meson_nand_op_put_dma_safe_output_buf(const >>>>>> struct nand_op_instr *instr, >>>>>> kfree(buf); >>>>>> } >>>>>> >>>>>> +static int meson_nfc_check_op(struct nand_chip *chip, >>>>>> + const struct nand_operation *op) >>>>>> +{ >>>>>> + int op_id; >>>>>> + >>>>>> + for (op_id = 0; op_id < op->ninstrs; op_id++) { >>>>>> + const struct nand_op_instr *instr; >>>>>> + >>>>>> + instr = &op->instrs[op_id]; >>>>>> + >>>>>> + switch (instr->type) { >>>>>> + case NAND_OP_DATA_IN_INSTR: >>>>>> + case NAND_OP_DATA_OUT_INSTR: >>>>>> + if (instr->ctx.data.len > NFC_CMD_RAW_LEN) >>>>>> + return -ENOTSUPP; >>>>>> + >>>>>> + break; >>>>>> + default: >>>>>> + break; >>>>>> + } >>>>>> + } >>>>>> + >>>>>> + return 0; >>>>>> +} >>>>>> + >>>>>> static int meson_nfc_exec_op(struct nand_chip *nand, >>>>>> const struct nand_operation *op, bool >>>>>> check_only) >>>>>> { >>>>>> @@ -1015,10 +1042,12 @@ static int meson_nfc_exec_op(struct >>>>>> nand_chip *nand, >>>>>> const struct nand_op_instr *instr = NULL; >>>>>> void *buf; >>>>>> u32 op_id, delay_idle, cmd; >>>>>> + int err; >>>>>> int i; >>>>>> >>>>>> - if (check_only) >>>>>> - return 0; >>>>>> + err = meson_nfc_check_op(nand, op); >>>>>> + if (err || check_only) >>>>>> + return err; >>>>>> >>>>>> meson_nfc_select_chip(nand, op->cs); >>>>>> for (op_id = 0; op_id < op->ninstrs; op_id++) { >>>>>> @@ -1293,6 +1322,7 @@ static int meson_nand_attach_chip(struct >>>>>> nand_chip *nand) >>>>>> struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); >>>>>> struct mtd_info *mtd = nand_to_mtd(nand); >>>>>> int nsectors = mtd->writesize / 1024; >>>>>> + int raw_writesize; >>>>>> int ret; >>>>>> >>>>>> if (!mtd->name) { >>>>>> @@ -1304,6 +1334,13 @@ static int meson_nand_attach_chip(struct >>>>>> nand_chip *nand) >>>>>> return -ENOMEM; >>>>>> } >>>>>> >>>>>> + raw_writesize = mtd->writesize + mtd->oobsize; >>>>>> + if (raw_writesize > NFC_CMD_RAW_LEN) { >>>>>> + dev_err(nfc->dev, "too big write size in raw mode: %d >>>>>> > %ld\n", >>>>>> + raw_writesize, NFC_CMD_RAW_LEN); >>>>>> + return -EINVAL; >>>>>> + } >>>>>> + >>>>>> if (nand->bbt_options & NAND_BBT_USE_FLASH) >>>>>> nand->bbt_options |= NAND_BBT_NO_OOB; >>>>>> >> >> >> Thanks, >> Miquèl
Hi Liang, liang.yang@amlogic.com wrote on Thu, 8 Jun 2023 20:37:14 +0800: > Hi Arseniy and Miquel, > > On 2023/6/8 15:12, Liang Yang wrote: > > Hi Miquel, > > > > On 2023/6/8 14:54, Miquel Raynal wrote: > >> [ EXTERNAL EMAIL ] > >> > >> Hi Liang, > >> > >> liang.yang@amlogic.com wrote on Thu, 8 Jun 2023 14:42:53 +0800: > >> > >>> Hi Arseniy, > >>> > >>> On 2023/6/8 5:17, Arseniy Krasnov wrote: > >>>> [ EXTERNAL EMAIL ] > >>>> > >>>> Hi again Miquel, Liang! > >>>> > >>>> What do You think about this patch? > >>>> > >>>> Thanks, Arseniy > >>>> > >>>> On 06.06.2023 19:29, Arseniy Krasnov wrote: > >>>>> Sorry, here is changelog: > >>>>> v1 -> v2: > >>>>> * Move checks from 'switch/case' which executes commands in >>>>> 'meson_nfc_exec_op()' to a special > >>>>> separated function 'meson_nfc_check_op()' which is called >>>>> before commands processing. > >>>>> > >>>>> On 06.06.2023 13:16, Arseniy Krasnov wrote: > >>>>>> Meson NAND controller has limited buffer length, so check it before > >>>>>> command execution to avoid length trim. Also check MTD write size on > >>>>>> chip attach. > >>>>>> > >>>>>> Signed-off-by: Arseniy Krasnov <AVKrasnov@sberdevices.ru> > >>>>>> --- > >>>>>> drivers/mtd/nand/raw/meson_nand.c | 47 >>>>>> +++++++++++++++++++++++++++---- > >>>>>> 1 file changed, 42 insertions(+), 5 deletions(-) > >>>>>> > >>>>>> diff --git a/drivers/mtd/nand/raw/meson_nand.c >>>>>> b/drivers/mtd/nand/raw/meson_nand.c > >>>>>> index 23a73268421b..db6b18753071 100644 > >>>>>> --- a/drivers/mtd/nand/raw/meson_nand.c > >>>>>> +++ b/drivers/mtd/nand/raw/meson_nand.c > >>>>>> @@ -111,6 +111,8 @@ > >>>>>> > >>>>>> #define PER_INFO_BYTE 8 > >>>>>> > >>>>>> +#define NFC_CMD_RAW_LEN GENMASK(13, 0) > >>>>>> + > >>>>>> struct meson_nfc_nand_chip { > >>>>>> struct list_head node; > >>>>>> struct nand_chip nand; > >>>>>> @@ -284,7 +286,7 @@ static void meson_nfc_cmd_access(struct >>>>>> nand_chip *nand, int raw, bool dir, > >>>>>> > >>>>>> if (raw) { > >>>>>> len = mtd->writesize + mtd->oobsize; > >>>>>> - cmd = (len & GENMASK(13, 0)) | scrambler | DMA_DIR(dir); > >>>>>> + cmd = len | scrambler | DMA_DIR(dir); > >>>>>> writel(cmd, nfc->reg_base + NFC_REG_CMD); > >>> > >>> I think we could keep "& GENMASK(13, 0)". it is better here to >>> indicate how many bits of length in this command and don't destory >>> the command once we don't check the "len" outside of this function. >>> or the code "if (len > NFC_CMD_RAW_LEN)" is better to put inside >>> this function nearly. Thanks. > >> > >> It depends who calls this helper. If only used inside >> exec_op,write_page_raw() and read_page_raw() also call >> meson_nfc_cmd_access(), > but i don't find where constrain the "len". > > Is the helper "meson_nfc_check_op()" needed by exec_op() after the constraint in attach_chip()? the length passed in exec_op() seems smaller than "mtd->writesize + mtd->oobsize" now. exec_op() is primarily dedicated to performing side commands than page accesses, typically the parameter page is X bytes, you might want to read it 3 times, depending on the capabilities of the controller, you might need to split the operation or not, so the core checks what are the controller capabilities before doing the operation. So yes, the check_op() thing is necessary. > > @Arseniy if it does need, I think the same constraint is needed by > "meson_nfc_cmd_access()" > > > > >> it's not useful. If used outside, then if you want to clarify > >> things you may want to use: > >> > >> #define NFC_CMD_OP_LEN(cmd) FIELD_PREP(GENMASK(13, 0), (cmd)) > >> > >> This is by far my favorite construction. Could apply to many other > >> fields, like DMA_DIR, scrambler, etc. > > @Miquel, FIELD_PREP() is better, but i have no idea whether we should add FIELD_PREP() in this patch, or keep the previous code. > > >> > >>>>>> return; > >>>>>> } > >>>>>> @@ -573,7 +575,7 @@ static int meson_nfc_read_buf(struct nand_chip >>>>>> *nand, u8 *buf, int len) > >>>>>> if (ret) > >>>>>> goto out; > >>>>>> > >>>>>> - cmd = NFC_CMD_N2M | (len & GENMASK(13, 0)); > >>>>>> + cmd = NFC_CMD_N2M | len; > >>>>>> writel(cmd, nfc->reg_base + NFC_REG_CMD); > >>>>>> > >>>>>> meson_nfc_drain_cmd(nfc); > >>>>>> @@ -597,7 +599,7 @@ static int meson_nfc_write_buf(struct >>>>>> nand_chip *nand, u8 *buf, int len) > >>>>>> if (ret) > >>>>>> return ret; > >>>>>> > >>>>>> - cmd = NFC_CMD_M2N | (len & GENMASK(13, 0)); > >>>>>> + cmd = NFC_CMD_M2N | len; > >>>>>> writel(cmd, nfc->reg_base + NFC_REG_CMD); > >>>>>> > >>>>>> meson_nfc_drain_cmd(nfc); > >>>>>> @@ -1007,6 +1009,31 @@ meson_nand_op_put_dma_safe_output_buf(const >>>>>> struct nand_op_instr *instr, > >>>>>> kfree(buf); > >>>>>> } > >>>>>> > >>>>>> +static int meson_nfc_check_op(struct nand_chip *chip, > >>>>>> + const struct nand_operation *op) > >>>>>> +{ > >>>>>> + int op_id; > >>>>>> + > >>>>>> + for (op_id = 0; op_id < op->ninstrs; op_id++) { > >>>>>> + const struct nand_op_instr *instr; > >>>>>> + > >>>>>> + instr = &op->instrs[op_id]; > >>>>>> + > >>>>>> + switch (instr->type) { > >>>>>> + case NAND_OP_DATA_IN_INSTR: > >>>>>> + case NAND_OP_DATA_OUT_INSTR: > >>>>>> + if (instr->ctx.data.len > NFC_CMD_RAW_LEN) > >>>>>> + return -ENOTSUPP; > >>>>>> + > >>>>>> + break; > >>>>>> + default: > >>>>>> + break; > >>>>>> + } > >>>>>> + } > >>>>>> + > >>>>>> + return 0; > >>>>>> +} > >>>>>> + > >>>>>> static int meson_nfc_exec_op(struct nand_chip *nand, > >>>>>> const struct nand_operation *op, bool >>>>>> check_only) > >>>>>> { > >>>>>> @@ -1015,10 +1042,12 @@ static int meson_nfc_exec_op(struct >>>>>> nand_chip *nand, > >>>>>> const struct nand_op_instr *instr = NULL; > >>>>>> void *buf; > >>>>>> u32 op_id, delay_idle, cmd; > >>>>>> + int err; > >>>>>> int i; > >>>>>> > >>>>>> - if (check_only) > >>>>>> - return 0; > >>>>>> + err = meson_nfc_check_op(nand, op); > >>>>>> + if (err || check_only) > >>>>>> + return err; > >>>>>> > >>>>>> meson_nfc_select_chip(nand, op->cs); > >>>>>> for (op_id = 0; op_id < op->ninstrs; op_id++) { > >>>>>> @@ -1293,6 +1322,7 @@ static int meson_nand_attach_chip(struct >>>>>> nand_chip *nand) > >>>>>> struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); > >>>>>> struct mtd_info *mtd = nand_to_mtd(nand); > >>>>>> int nsectors = mtd->writesize / 1024; > >>>>>> + int raw_writesize; > >>>>>> int ret; > >>>>>> > >>>>>> if (!mtd->name) { > >>>>>> @@ -1304,6 +1334,13 @@ static int meson_nand_attach_chip(struct >>>>>> nand_chip *nand) > >>>>>> return -ENOMEM; > >>>>>> } > >>>>>> > >>>>>> + raw_writesize = mtd->writesize + mtd->oobsize; > >>>>>> + if (raw_writesize > NFC_CMD_RAW_LEN) { > >>>>>> + dev_err(nfc->dev, "too big write size in raw mode: %d >>>>>> > %ld\n", > >>>>>> + raw_writesize, NFC_CMD_RAW_LEN); > >>>>>> + return -EINVAL; > >>>>>> + } > >>>>>> + > >>>>>> if (nand->bbt_options & NAND_BBT_USE_FLASH) > >>>>>> nand->bbt_options |= NAND_BBT_NO_OOB; > >>>>>> > >> > >> > >> Thanks, > >> Miquèl Thanks, Miquèl
Hi Miquel, On 2023/6/8 21:21, Miquel Raynal wrote: > [ EXTERNAL EMAIL ] > > Hi Liang, > > liang.yang@amlogic.com wrote on Thu, 8 Jun 2023 20:37:14 +0800: > >> Hi Arseniy and Miquel, >> >> On 2023/6/8 15:12, Liang Yang wrote: >>> Hi Miquel, >>> >>> On 2023/6/8 14:54, Miquel Raynal wrote: >>>> [ EXTERNAL EMAIL ] >>>> >>>> Hi Liang, >>>> >>>> liang.yang@amlogic.com wrote on Thu, 8 Jun 2023 14:42:53 +0800: >>>> >>>>> Hi Arseniy, >>>>> >>>>> On 2023/6/8 5:17, Arseniy Krasnov wrote: >>>>>> [ EXTERNAL EMAIL ] >>>>>> >>>>>> Hi again Miquel, Liang! >>>>>> >>>>>> What do You think about this patch? >>>>>> >>>>>> Thanks, Arseniy >>>>>> >>>>>> On 06.06.2023 19:29, Arseniy Krasnov wrote: >>>>>>> Sorry, here is changelog: >>>>>>> v1 -> v2: >>>>>>> * Move checks from 'switch/case' which executes commands in >>>>> 'meson_nfc_exec_op()' to a special >>>>>>> separated function 'meson_nfc_check_op()' which is called >>>>> before commands processing. >>>>>>> >>>>>>> On 06.06.2023 13:16, Arseniy Krasnov wrote: >>>>>>>> Meson NAND controller has limited buffer length, so check it before >>>>>>>> command execution to avoid length trim. Also check MTD write size on >>>>>>>> chip attach. >>>>>>>> >>>>>>>> Signed-off-by: Arseniy Krasnov <AVKrasnov@sberdevices.ru> >>>>>>>> --- >>>>>>>> drivers/mtd/nand/raw/meson_nand.c | 47 >>>>>> +++++++++++++++++++++++++++---- >>>>>>>> 1 file changed, 42 insertions(+), 5 deletions(-) >>>>>>>> >>>>>>>> diff --git a/drivers/mtd/nand/raw/meson_nand.c >>>>>> b/drivers/mtd/nand/raw/meson_nand.c >>>>>>>> index 23a73268421b..db6b18753071 100644 >>>>>>>> --- a/drivers/mtd/nand/raw/meson_nand.c >>>>>>>> +++ b/drivers/mtd/nand/raw/meson_nand.c >>>>>>>> @@ -111,6 +111,8 @@ >>>>>>>> >>>>>>>> #define PER_INFO_BYTE 8 >>>>>>>> >>>>>>>> +#define NFC_CMD_RAW_LEN GENMASK(13, 0) >>>>>>>> + >>>>>>>> struct meson_nfc_nand_chip { >>>>>>>> struct list_head node; >>>>>>>> struct nand_chip nand; >>>>>>>> @@ -284,7 +286,7 @@ static void meson_nfc_cmd_access(struct >>>>>> nand_chip *nand, int raw, bool dir, >>>>>>>> >>>>>>>> if (raw) { >>>>>>>> len = mtd->writesize + mtd->oobsize; >>>>>>>> - cmd = (len & GENMASK(13, 0)) | scrambler | DMA_DIR(dir); >>>>>>>> + cmd = len | scrambler | DMA_DIR(dir); >>>>>>>> writel(cmd, nfc->reg_base + NFC_REG_CMD); >>>>> >>>>> I think we could keep "& GENMASK(13, 0)". it is better here to >>> indicate how many bits of length in this command and don't destory >>> the command once we don't check the "len" outside of this function. >>> or the code "if (len > NFC_CMD_RAW_LEN)" is better to put inside >>> this function nearly. Thanks. >>>> >>>> It depends who calls this helper. If only used inside >> exec_op,write_page_raw() and read_page_raw() also call >> meson_nfc_cmd_access(), > but i don't find where constrain the "len". >> >> Is the helper "meson_nfc_check_op()" needed by exec_op() after the constraint in attach_chip()? the length passed in exec_op() seems smaller than "mtd->writesize + mtd->oobsize" now. > > exec_op() is primarily dedicated to performing side commands than page > accesses, typically the parameter page is X bytes, you might want to > read it 3 times, depending on the capabilities of the controller, you > might need to split the operation or not, so the core checks what are > the controller capabilities before doing the operation. So yes, the > check_op() thing is necessary. ok, i get it. thanks @Arseniy I have no other questions about this patch. > >> >> @Arseniy if it does need, I think the same constraint is needed by >> "meson_nfc_cmd_access()" >> >>> >>>> it's not useful. If used outside, then if you want to clarify >>>> things you may want to use: >>>> >>>> #define NFC_CMD_OP_LEN(cmd) FIELD_PREP(GENMASK(13, 0), (cmd)) >>>> >>>> This is by far my favorite construction. Could apply to many other >>>> fields, like DMA_DIR, scrambler, etc. >> >> @Miquel, FIELD_PREP() is better, but i have no idea whether we should add FIELD_PREP() in this patch, or keep the previous code. >> >>>> >>>>>>>> return; >>>>>>>> } >>>>>>>> @@ -573,7 +575,7 @@ static int meson_nfc_read_buf(struct nand_chip >>>>>> *nand, u8 *buf, int len) >>>>>>>> if (ret) >>>>>>>> goto out; >>>>>>>> >>>>>>>> - cmd = NFC_CMD_N2M | (len & GENMASK(13, 0)); >>>>>>>> + cmd = NFC_CMD_N2M | len; >>>>>>>> writel(cmd, nfc->reg_base + NFC_REG_CMD); >>>>>>>> >>>>>>>> meson_nfc_drain_cmd(nfc); >>>>>>>> @@ -597,7 +599,7 @@ static int meson_nfc_write_buf(struct >>>>>> nand_chip *nand, u8 *buf, int len) >>>>>>>> if (ret) >>>>>>>> return ret; >>>>>>>> >>>>>>>> - cmd = NFC_CMD_M2N | (len & GENMASK(13, 0)); >>>>>>>> + cmd = NFC_CMD_M2N | len; >>>>>>>> writel(cmd, nfc->reg_base + NFC_REG_CMD); >>>>>>>> >>>>>>>> meson_nfc_drain_cmd(nfc); >>>>>>>> @@ -1007,6 +1009,31 @@ meson_nand_op_put_dma_safe_output_buf(const >>>>>> struct nand_op_instr *instr, >>>>>>>> kfree(buf); >>>>>>>> } >>>>>>>> >>>>>>>> +static int meson_nfc_check_op(struct nand_chip *chip, >>>>>>>> + const struct nand_operation *op) >>>>>>>> +{ >>>>>>>> + int op_id; >>>>>>>> + >>>>>>>> + for (op_id = 0; op_id < op->ninstrs; op_id++) { >>>>>>>> + const struct nand_op_instr *instr; >>>>>>>> + >>>>>>>> + instr = &op->instrs[op_id]; >>>>>>>> + >>>>>>>> + switch (instr->type) { >>>>>>>> + case NAND_OP_DATA_IN_INSTR: >>>>>>>> + case NAND_OP_DATA_OUT_INSTR: >>>>>>>> + if (instr->ctx.data.len > NFC_CMD_RAW_LEN) >>>>>>>> + return -ENOTSUPP; >>>>>>>> + >>>>>>>> + break; >>>>>>>> + default: >>>>>>>> + break; >>>>>>>> + } >>>>>>>> + } >>>>>>>> + >>>>>>>> + return 0; >>>>>>>> +} >>>>>>>> + >>>>>>>> static int meson_nfc_exec_op(struct nand_chip *nand, >>>>>>>> const struct nand_operation *op, bool >>>>>> check_only) >>>>>>>> { >>>>>>>> @@ -1015,10 +1042,12 @@ static int meson_nfc_exec_op(struct >>>>>> nand_chip *nand, >>>>>>>> const struct nand_op_instr *instr = NULL; >>>>>>>> void *buf; >>>>>>>> u32 op_id, delay_idle, cmd; >>>>>>>> + int err; >>>>>>>> int i; >>>>>>>> >>>>>>>> - if (check_only) >>>>>>>> - return 0; >>>>>>>> + err = meson_nfc_check_op(nand, op); >>>>>>>> + if (err || check_only) >>>>>>>> + return err; >>>>>>>> >>>>>>>> meson_nfc_select_chip(nand, op->cs); >>>>>>>> for (op_id = 0; op_id < op->ninstrs; op_id++) { >>>>>>>> @@ -1293,6 +1322,7 @@ static int meson_nand_attach_chip(struct >>>>>> nand_chip *nand) >>>>>>>> struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); >>>>>>>> struct mtd_info *mtd = nand_to_mtd(nand); >>>>>>>> int nsectors = mtd->writesize / 1024; >>>>>>>> + int raw_writesize; >>>>>>>> int ret; >>>>>>>> >>>>>>>> if (!mtd->name) { >>>>>>>> @@ -1304,6 +1334,13 @@ static int meson_nand_attach_chip(struct >>>>>> nand_chip *nand) >>>>>>>> return -ENOMEM; >>>>>>>> } >>>>>>>> >>>>>>>> + raw_writesize = mtd->writesize + mtd->oobsize; >>>>>>>> + if (raw_writesize > NFC_CMD_RAW_LEN) { >>>>>>>> + dev_err(nfc->dev, "too big write size in raw mode: %d >>>>>> > %ld\n", >>>>>>>> + raw_writesize, NFC_CMD_RAW_LEN); >>>>>>>> + return -EINVAL; >>>>>>>> + } >>>>>>>> + >>>>>>>> if (nand->bbt_options & NAND_BBT_USE_FLASH) >>>>>>>> nand->bbt_options |= NAND_BBT_NO_OOB; >>>>>>>> >>>> >>>> >>>> Thanks, >>>> Miquèl > > > Thanks, > Miquèl
On 09.06.2023 10:59, Liang Yang wrote: > Hi Miquel, > > On 2023/6/8 21:21, Miquel Raynal wrote: >> [ EXTERNAL EMAIL ] >> >> Hi Liang, >> >> liang.yang@amlogic.com wrote on Thu, 8 Jun 2023 20:37:14 +0800: >> >>> Hi Arseniy and Miquel, >>> >>> On 2023/6/8 15:12, Liang Yang wrote: >>>> Hi Miquel, >>>> >>>> On 2023/6/8 14:54, Miquel Raynal wrote: >>>>> [ EXTERNAL EMAIL ] >>>>> >>>>> Hi Liang, >>>>> >>>>> liang.yang@amlogic.com wrote on Thu, 8 Jun 2023 14:42:53 +0800: >>>>> >>>>>> Hi Arseniy, >>>>>> >>>>>> On 2023/6/8 5:17, Arseniy Krasnov wrote: >>>>>>> [ EXTERNAL EMAIL ] >>>>>>> >>>>>>> Hi again Miquel, Liang! >>>>>>> >>>>>>> What do You think about this patch? >>>>>>> >>>>>>> Thanks, Arseniy >>>>>>> >>>>>>> On 06.06.2023 19:29, Arseniy Krasnov wrote: >>>>>>>> Sorry, here is changelog: >>>>>>>> v1 -> v2: >>>>>>>> * Move checks from 'switch/case' which executes commands in >>>>> 'meson_nfc_exec_op()' to a special >>>>>>>> separated function 'meson_nfc_check_op()' which is called >>>>> before commands processing. >>>>>>>> >>>>>>>> On 06.06.2023 13:16, Arseniy Krasnov wrote: >>>>>>>>> Meson NAND controller has limited buffer length, so check it before >>>>>>>>> command execution to avoid length trim. Also check MTD write size on >>>>>>>>> chip attach. >>>>>>>>> >>>>>>>>> Signed-off-by: Arseniy Krasnov <AVKrasnov@sberdevices.ru> >>>>>>>>> --- >>>>>>>>> drivers/mtd/nand/raw/meson_nand.c | 47 >>>>>> +++++++++++++++++++++++++++---- >>>>>>>>> 1 file changed, 42 insertions(+), 5 deletions(-) >>>>>>>>> >>>>>>>>> diff --git a/drivers/mtd/nand/raw/meson_nand.c >>>>>> b/drivers/mtd/nand/raw/meson_nand.c >>>>>>>>> index 23a73268421b..db6b18753071 100644 >>>>>>>>> --- a/drivers/mtd/nand/raw/meson_nand.c >>>>>>>>> +++ b/drivers/mtd/nand/raw/meson_nand.c >>>>>>>>> @@ -111,6 +111,8 @@ >>>>>>>>> >>>>>>>>> #define PER_INFO_BYTE 8 >>>>>>>>> >>>>>>>>> +#define NFC_CMD_RAW_LEN GENMASK(13, 0) >>>>>>>>> + >>>>>>>>> struct meson_nfc_nand_chip { >>>>>>>>> struct list_head node; >>>>>>>>> struct nand_chip nand; >>>>>>>>> @@ -284,7 +286,7 @@ static void meson_nfc_cmd_access(struct >>>>>> nand_chip *nand, int raw, bool dir, >>>>>>>>> >>>>>>>>> if (raw) { >>>>>>>>> len = mtd->writesize + mtd->oobsize; >>>>>>>>> - cmd = (len & GENMASK(13, 0)) | scrambler | DMA_DIR(dir); >>>>>>>>> + cmd = len | scrambler | DMA_DIR(dir); >>>>>>>>> writel(cmd, nfc->reg_base + NFC_REG_CMD); >>>>>> >>>>>> I think we could keep "& GENMASK(13, 0)". it is better here to >>> indicate how many bits of length in this command and don't destory >>> the command once we don't check the "len" outside of this function. >>> or the code "if (len > NFC_CMD_RAW_LEN)" is better to put inside >>> this function nearly. Thanks. >>>>> >>>>> It depends who calls this helper. If only used inside >> exec_op,write_page_raw() and read_page_raw() also call >> meson_nfc_cmd_access(), > but i don't find where constrain the "len". >>> >>> Is the helper "meson_nfc_check_op()" needed by exec_op() after the constraint in attach_chip()? the length passed in exec_op() seems smaller than "mtd->writesize + mtd->oobsize" now. >> >> exec_op() is primarily dedicated to performing side commands than page >> accesses, typically the parameter page is X bytes, you might want to >> read it 3 times, depending on the capabilities of the controller, you >> might need to split the operation or not, so the core checks what are >> the controller capabilities before doing the operation. So yes, the >> check_op() thing is necessary. > > ok, i get it. thanks > > @Arseniy I have no other questions about this patch. Got it! Thanks, Arseniy >> >>> >>> @Arseniy if it does need, I think the same constraint is needed by >>> "meson_nfc_cmd_access()" >>> >>>> >>>>> it's not useful. If used outside, then if you want to clarify >>>>> things you may want to use: >>>>> >>>>> #define NFC_CMD_OP_LEN(cmd) FIELD_PREP(GENMASK(13, 0), (cmd)) >>>>> >>>>> This is by far my favorite construction. Could apply to many other >>>>> fields, like DMA_DIR, scrambler, etc. >>> >>> @Miquel, FIELD_PREP() is better, but i have no idea whether we should add FIELD_PREP() in this patch, or keep the previous code. >>> >>>>> >>>>>>>>> return; >>>>>>>>> } >>>>>>>>> @@ -573,7 +575,7 @@ static int meson_nfc_read_buf(struct nand_chip >>>>>> *nand, u8 *buf, int len) >>>>>>>>> if (ret) >>>>>>>>> goto out; >>>>>>>>> >>>>>>>>> - cmd = NFC_CMD_N2M | (len & GENMASK(13, 0)); >>>>>>>>> + cmd = NFC_CMD_N2M | len; >>>>>>>>> writel(cmd, nfc->reg_base + NFC_REG_CMD); >>>>>>>>> >>>>>>>>> meson_nfc_drain_cmd(nfc); >>>>>>>>> @@ -597,7 +599,7 @@ static int meson_nfc_write_buf(struct >>>>>> nand_chip *nand, u8 *buf, int len) >>>>>>>>> if (ret) >>>>>>>>> return ret; >>>>>>>>> >>>>>>>>> - cmd = NFC_CMD_M2N | (len & GENMASK(13, 0)); >>>>>>>>> + cmd = NFC_CMD_M2N | len; >>>>>>>>> writel(cmd, nfc->reg_base + NFC_REG_CMD); >>>>>>>>> >>>>>>>>> meson_nfc_drain_cmd(nfc); >>>>>>>>> @@ -1007,6 +1009,31 @@ meson_nand_op_put_dma_safe_output_buf(const >>>>>> struct nand_op_instr *instr, >>>>>>>>> kfree(buf); >>>>>>>>> } >>>>>>>>> >>>>>>>>> +static int meson_nfc_check_op(struct nand_chip *chip, >>>>>>>>> + const struct nand_operation *op) >>>>>>>>> +{ >>>>>>>>> + int op_id; >>>>>>>>> + >>>>>>>>> + for (op_id = 0; op_id < op->ninstrs; op_id++) { >>>>>>>>> + const struct nand_op_instr *instr; >>>>>>>>> + >>>>>>>>> + instr = &op->instrs[op_id]; >>>>>>>>> + >>>>>>>>> + switch (instr->type) { >>>>>>>>> + case NAND_OP_DATA_IN_INSTR: >>>>>>>>> + case NAND_OP_DATA_OUT_INSTR: >>>>>>>>> + if (instr->ctx.data.len > NFC_CMD_RAW_LEN) >>>>>>>>> + return -ENOTSUPP; >>>>>>>>> + >>>>>>>>> + break; >>>>>>>>> + default: >>>>>>>>> + break; >>>>>>>>> + } >>>>>>>>> + } >>>>>>>>> + >>>>>>>>> + return 0; >>>>>>>>> +} >>>>>>>>> + >>>>>>>>> static int meson_nfc_exec_op(struct nand_chip *nand, >>>>>>>>> const struct nand_operation *op, bool >>>>>> check_only) >>>>>>>>> { >>>>>>>>> @@ -1015,10 +1042,12 @@ static int meson_nfc_exec_op(struct >>>>>> nand_chip *nand, >>>>>>>>> const struct nand_op_instr *instr = NULL; >>>>>>>>> void *buf; >>>>>>>>> u32 op_id, delay_idle, cmd; >>>>>>>>> + int err; >>>>>>>>> int i; >>>>>>>>> >>>>>>>>> - if (check_only) >>>>>>>>> - return 0; >>>>>>>>> + err = meson_nfc_check_op(nand, op); >>>>>>>>> + if (err || check_only) >>>>>>>>> + return err; >>>>>>>>> >>>>>>>>> meson_nfc_select_chip(nand, op->cs); >>>>>>>>> for (op_id = 0; op_id < op->ninstrs; op_id++) { >>>>>>>>> @@ -1293,6 +1322,7 @@ static int meson_nand_attach_chip(struct >>>>>> nand_chip *nand) >>>>>>>>> struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); >>>>>>>>> struct mtd_info *mtd = nand_to_mtd(nand); >>>>>>>>> int nsectors = mtd->writesize / 1024; >>>>>>>>> + int raw_writesize; >>>>>>>>> int ret; >>>>>>>>> >>>>>>>>> if (!mtd->name) { >>>>>>>>> @@ -1304,6 +1334,13 @@ static int meson_nand_attach_chip(struct >>>>>> nand_chip *nand) >>>>>>>>> return -ENOMEM; >>>>>>>>> } >>>>>>>>> >>>>>>>>> + raw_writesize = mtd->writesize + mtd->oobsize; >>>>>>>>> + if (raw_writesize > NFC_CMD_RAW_LEN) { >>>>>>>>> + dev_err(nfc->dev, "too big write size in raw mode: %d >>>>>> > %ld\n", >>>>>>>>> + raw_writesize, NFC_CMD_RAW_LEN); >>>>>>>>> + return -EINVAL; >>>>>>>>> + } >>>>>>>>> + >>>>>>>>> if (nand->bbt_options & NAND_BBT_USE_FLASH) >>>>>>>>> nand->bbt_options |= NAND_BBT_NO_OOB; >>>>>>>>> >>>>> >>>>> >>>>> Thanks, >>>>> Miquèl >> >> >> Thanks, >> Miquèl
diff --git a/drivers/mtd/nand/raw/meson_nand.c b/drivers/mtd/nand/raw/meson_nand.c index 23a73268421b..db6b18753071 100644 --- a/drivers/mtd/nand/raw/meson_nand.c +++ b/drivers/mtd/nand/raw/meson_nand.c @@ -111,6 +111,8 @@ #define PER_INFO_BYTE 8 +#define NFC_CMD_RAW_LEN GENMASK(13, 0) + struct meson_nfc_nand_chip { struct list_head node; struct nand_chip nand; @@ -284,7 +286,7 @@ static void meson_nfc_cmd_access(struct nand_chip *nand, int raw, bool dir, if (raw) { len = mtd->writesize + mtd->oobsize; - cmd = (len & GENMASK(13, 0)) | scrambler | DMA_DIR(dir); + cmd = len | scrambler | DMA_DIR(dir); writel(cmd, nfc->reg_base + NFC_REG_CMD); return; } @@ -573,7 +575,7 @@ static int meson_nfc_read_buf(struct nand_chip *nand, u8 *buf, int len) if (ret) goto out; - cmd = NFC_CMD_N2M | (len & GENMASK(13, 0)); + cmd = NFC_CMD_N2M | len; writel(cmd, nfc->reg_base + NFC_REG_CMD); meson_nfc_drain_cmd(nfc); @@ -597,7 +599,7 @@ static int meson_nfc_write_buf(struct nand_chip *nand, u8 *buf, int len) if (ret) return ret; - cmd = NFC_CMD_M2N | (len & GENMASK(13, 0)); + cmd = NFC_CMD_M2N | len; writel(cmd, nfc->reg_base + NFC_REG_CMD); meson_nfc_drain_cmd(nfc); @@ -1007,6 +1009,31 @@ meson_nand_op_put_dma_safe_output_buf(const struct nand_op_instr *instr, kfree(buf); } +static int meson_nfc_check_op(struct nand_chip *chip, + const struct nand_operation *op) +{ + int op_id; + + for (op_id = 0; op_id < op->ninstrs; op_id++) { + const struct nand_op_instr *instr; + + instr = &op->instrs[op_id]; + + switch (instr->type) { + case NAND_OP_DATA_IN_INSTR: + case NAND_OP_DATA_OUT_INSTR: + if (instr->ctx.data.len > NFC_CMD_RAW_LEN) + return -ENOTSUPP; + + break; + default: + break; + } + } + + return 0; +} + static int meson_nfc_exec_op(struct nand_chip *nand, const struct nand_operation *op, bool check_only) { @@ -1015,10 +1042,12 @@ static int meson_nfc_exec_op(struct nand_chip *nand, const struct nand_op_instr *instr = NULL; void *buf; u32 op_id, delay_idle, cmd; + int err; int i; - if (check_only) - return 0; + err = meson_nfc_check_op(nand, op); + if (err || check_only) + return err; meson_nfc_select_chip(nand, op->cs); for (op_id = 0; op_id < op->ninstrs; op_id++) { @@ -1293,6 +1322,7 @@ static int meson_nand_attach_chip(struct nand_chip *nand) struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); struct mtd_info *mtd = nand_to_mtd(nand); int nsectors = mtd->writesize / 1024; + int raw_writesize; int ret; if (!mtd->name) { @@ -1304,6 +1334,13 @@ static int meson_nand_attach_chip(struct nand_chip *nand) return -ENOMEM; } + raw_writesize = mtd->writesize + mtd->oobsize; + if (raw_writesize > NFC_CMD_RAW_LEN) { + dev_err(nfc->dev, "too big write size in raw mode: %d > %ld\n", + raw_writesize, NFC_CMD_RAW_LEN); + return -EINVAL; + } + if (nand->bbt_options & NAND_BBT_USE_FLASH) nand->bbt_options |= NAND_BBT_NO_OOB;