mtd: nand: ecc-hamming: Stop using raw NAND structures
authorMiquel Raynal <miquel.raynal@bootlin.com>
Tue, 29 Sep 2020 23:01:20 +0000 (01:01 +0200)
committerMiquel Raynal <miquel.raynal@bootlin.com>
Thu, 10 Dec 2020 21:37:29 +0000 (22:37 +0100)
This code is meant to be reused by the SPI-NAND core. Now that the
driver has been cleaned and reorganized, use a generic ECC engine
object to store the driver's data instead of accessing members of the
nand_chip structure. This means adding proper init/cleanup helpers.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/20200929230124.31491-17-miquel.raynal@bootlin.com
drivers/mtd/nand/ecc-sw-hamming.c
drivers/mtd/nand/raw/nand_base.c
include/linux/mtd/nand-ecc-sw-hamming.h
include/linux/mtd/rawnand.h

index 7ee0387..9cd70b4 100644 (file)
@@ -17,8 +17,6 @@
 #include <linux/types.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
-#include <linux/mtd/mtd.h>
-#include <linux/mtd/rawnand.h>
 #include <linux/mtd/nand-ecc-sw-hamming.h>
 #include <asm/byteorder.h>
 
@@ -361,10 +359,11 @@ EXPORT_SYMBOL(ecc_sw_hamming_calculate);
 int nand_ecc_sw_hamming_calculate(struct nand_device *nand,
                                  const unsigned char *buf, unsigned char *code)
 {
-       struct nand_chip *chip = mtd_to_nand(nanddev_to_mtd(nand));
-       bool sm_order = chip->ecc.options & NAND_ECC_SOFT_HAMMING_SM_ORDER;
+       struct nand_ecc_sw_hamming_conf *engine_conf = nand->ecc.ctx.priv;
+       unsigned int step_size = nand->ecc.ctx.conf.step_size;
 
-       return ecc_sw_hamming_calculate(buf, chip->ecc.size, code, sm_order);
+       return ecc_sw_hamming_calculate(buf, step_size, code,
+                                       engine_conf->sm_order);
 }
 EXPORT_SYMBOL(nand_ecc_sw_hamming_calculate);
 
@@ -453,11 +452,11 @@ int nand_ecc_sw_hamming_correct(struct nand_device *nand, unsigned char *buf,
                                unsigned char *read_ecc,
                                unsigned char *calc_ecc)
 {
-       struct nand_chip *chip = mtd_to_nand(nanddev_to_mtd(nand));
-       bool sm_order = chip->ecc.options & NAND_ECC_SOFT_HAMMING_SM_ORDER;
+       struct nand_ecc_sw_hamming_conf *engine_conf = nand->ecc.ctx.priv;
+       unsigned int step_size = nand->ecc.ctx.conf.step_size;
 
-       return ecc_sw_hamming_correct(buf, read_ecc, calc_ecc, chip->ecc.size,
-                                     sm_order);
+       return ecc_sw_hamming_correct(buf, read_ecc, calc_ecc, step_size,
+                                     engine_conf->sm_order);
 }
 EXPORT_SYMBOL(nand_ecc_sw_hamming_correct);
 
index 32e9870..c36d2d4 100644 (file)
@@ -5139,6 +5139,46 @@ static void nand_scan_ident_cleanup(struct nand_chip *chip)
        kfree(chip->parameters.onfi);
 }
 
+int rawnand_sw_hamming_init(struct nand_chip *chip)
+{
+       struct mtd_info *mtd = nand_to_mtd(chip);
+       struct nand_ecc_sw_hamming_conf *engine_conf;
+       struct nand_device *base = &chip->base;
+
+       base->ecc.user_conf.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
+       base->ecc.user_conf.algo = NAND_ECC_ALGO_HAMMING;
+       base->ecc.user_conf.strength = chip->ecc.strength;
+       base->ecc.user_conf.step_size = chip->ecc.size;
+
+       if (base->ecc.user_conf.strength != 1 ||
+           (base->ecc.user_conf.step_size != 256 &&
+            base->ecc.user_conf.step_size != 512)) {
+               pr_err("%s: unsupported strength or step size\n", __func__);
+               return -EINVAL;
+       }
+
+       engine_conf = kzalloc(sizeof(*engine_conf), GFP_KERNEL);
+       if (!engine_conf)
+               return -ENOMEM;
+
+       engine_conf->code_size = 3;
+       engine_conf->nsteps = mtd->writesize / base->ecc.user_conf.step_size;
+
+       if (chip->ecc.options & NAND_ECC_SOFT_HAMMING_SM_ORDER)
+               engine_conf->sm_order = true;
+
+       base->ecc.ctx.priv = engine_conf;
+
+       chip->ecc.size = base->ecc.ctx.conf.step_size;
+       chip->ecc.strength = base->ecc.ctx.conf.strength;
+       chip->ecc.total = base->ecc.ctx.total;
+       chip->ecc.steps = engine_conf->nsteps;
+       chip->ecc.bytes = engine_conf->code_size;
+
+       return 0;
+}
+EXPORT_SYMBOL(rawnand_sw_hamming_init);
+
 int rawnand_sw_hamming_calculate(struct nand_chip *chip,
                                 const unsigned char *buf,
                                 unsigned char *code)
@@ -5160,6 +5200,14 @@ int rawnand_sw_hamming_correct(struct nand_chip *chip,
 }
 EXPORT_SYMBOL(rawnand_sw_hamming_correct);
 
+void rawnand_sw_hamming_cleanup(struct nand_chip *chip)
+{
+       struct nand_device *base = &chip->base;
+
+       kfree(base->ecc.ctx.priv);
+}
+EXPORT_SYMBOL(rawnand_sw_hamming_cleanup);
+
 int rawnand_sw_bch_init(struct nand_chip *chip)
 {
        struct nand_device *base = &chip->base;
@@ -5303,6 +5351,12 @@ static int nand_set_ecc_soft_ops(struct nand_chip *chip)
                if (IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_HAMMING_SMC))
                        ecc->options |= NAND_ECC_SOFT_HAMMING_SM_ORDER;
 
+               ret = rawnand_sw_hamming_init(chip);
+               if (ret) {
+                       WARN(1, "Hamming ECC initialization failed!\n");
+                       return ret;
+               }
+
                return 0;
        case NAND_ECC_ALGO_BCH:
                if (!IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_BCH)) {
@@ -5996,9 +6050,12 @@ EXPORT_SYMBOL(nand_scan_with_ids);
  */
 void nand_cleanup(struct nand_chip *chip)
 {
-       if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT &&
-           chip->ecc.algo == NAND_ECC_ALGO_BCH)
-               rawnand_sw_bch_cleanup(chip);
+       if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT) {
+               if (chip->ecc.algo == NAND_ECC_ALGO_HAMMING)
+                       rawnand_sw_hamming_cleanup(chip);
+               else if (chip->ecc.algo == NAND_ECC_ALGO_BCH)
+                       rawnand_sw_bch_cleanup(chip);
+       }
 
        nanddev_cleanup(&chip->base);
 
index 84a123b..9d4b4d6 100644 (file)
 
 #include <linux/mtd/nand.h>
 
+/**
+ * struct nand_ecc_sw_hamming_conf - private software Hamming ECC engine structure
+ * @reqooblen: Save the actual user OOB length requested before overwriting it
+ * @spare_oobbuf: Spare OOB buffer if none is provided
+ * @code_size: Number of bytes needed to store a code (one code per step)
+ * @nsteps: Number of steps
+ * @calc_buf: Buffer to use when calculating ECC bytes
+ * @code_buf: Buffer to use when reading (raw) ECC bytes from the chip
+ * @sm_order: Smart Media special ordering
+ */
+struct nand_ecc_sw_hamming_conf {
+       unsigned int reqooblen;
+       void *spare_oobbuf;
+       unsigned int code_size;
+       unsigned int nsteps;
+       u8 *calc_buf;
+       u8 *code_buf;
+       unsigned int sm_order;
+};
+
 int ecc_sw_hamming_calculate(const unsigned char *buf, unsigned int step_size,
                             unsigned char *code, bool sm_order);
 int nand_ecc_sw_hamming_calculate(struct nand_device *nand,
index 685d245..0a90a87 100644 (file)
@@ -1301,6 +1301,7 @@ static inline int nand_opcode_8bits(unsigned int command)
        return 0;
 }
 
+int rawnand_sw_hamming_init(struct nand_chip *chip);
 int rawnand_sw_hamming_calculate(struct nand_chip *chip,
                                 const unsigned char *buf,
                                 unsigned char *code);
@@ -1308,6 +1309,7 @@ int rawnand_sw_hamming_correct(struct nand_chip *chip,
                               unsigned char *buf,
                               unsigned char *read_ecc,
                               unsigned char *calc_ecc);
+void rawnand_sw_hamming_cleanup(struct nand_chip *chip);
 int rawnand_sw_bch_init(struct nand_chip *chip);
 int rawnand_sw_bch_correct(struct nand_chip *chip, unsigned char *buf,
                           unsigned char *read_ecc, unsigned char *calc_ecc);