Skip to contentSkip to conversation

mtd: jz4780_nand: replace if/else blocks with switch/case

PATCH In review Brian Norris 3 participants 6 messages Jan 7, 2016 – Jan 8, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mtd: jz4780_nand: replace if/else blocks with switch/case

Using switch/case helps make this logic more clear and more robust. With this structure:

* it's clear that this driver only support ECC_{HW,SOFT,SOFT_BCH}; and

* we can sanely handle new ECC unsupported modes (right now, this code makes incorrect assumptions about the possible values in the nand_ecc_modes_t enum; e.g., what happens with NAND_ECC_HW_OOB_FIRST?)

Cc Alex Smith <[email protected]>
Cc Harvey Hunt <[email protected]>

---
Compile tested only

 drivers/mtd/nand/jz4780_nand.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
drivers/mtd/nand/jz4780_nand.c +19 -15
index 17eb9f264187..aabee8f5627e 100644
@@ -171,29 +171,33 @@ static int jz4780_nand_init_ecc(struct jz4780_nand_chip *nand, struct device *de
 	chip->ecc.bytes = fls((1 + 8) * chip->ecc.size)	*
 				(chip->ecc.strength / 8);
 
-	if (nfc->bch && chip->ecc.mode == NAND_ECC_HW) {
+	switch (chip->ecc.mode) {
+	case NAND_ECC_HW:
+		if (!nfc->bch) {
+			dev_err(dev, "HW BCH selected, but BCH controller not found\n");
+			return -ENODEV;
+		}
+
 		chip->ecc.hwctl = jz4780_nand_ecc_hwctl;
 		chip->ecc.calculate = jz4780_nand_ecc_calculate;
 		chip->ecc.correct = jz4780_nand_ecc_correct;
-	} else if (!nfc->bch && chip->ecc.mode == NAND_ECC_HW) {
-		dev_err(dev, "HW BCH selected, but BCH controller not found\n");
-		return -ENODEV;
-	}
-
-	if (chip->ecc.mode == NAND_ECC_HW_SYNDROME) {
-		dev_err(dev, "ECC HW syndrome not supported\n");
-		return -EINVAL;
-	}
-
-	if (chip->ecc.mode != NAND_ECC_NONE)
+		/* fall through */
+	case NAND_ECC_SOFT:
+	case NAND_ECC_SOFT_BCH:
 		dev_info(dev, "using %s (strength %d, size %d, bytes %d)\n",
 			(nfc->bch) ? "hardware BCH" : "software ECC",
 			chip->ecc.strength, chip->ecc.size, chip->ecc.bytes);
-	else
+		break;
+	case NAND_ECC_NONE:
 		dev_info(dev, "not using ECC\n");
+		break;
+	default:
+		dev_err(dev, "ECC mode %d not supported\n", chip->ecc.mode);
+		return -EINVAL;
+	}
 
-	/* The NAND core will generate the ECC layout. */
-	if (chip->ecc.mode == NAND_ECC_SOFT || chip->ecc.mode == NAND_ECC_SOFT_BCH)
+	/* The NAND core will generate the ECC layout for SW ECC */
+	if (chip->ecc.mode != NAND_ECC_HW)
 		return 0;
 
 	/* Generate ECC layout. ECC codes are right aligned in the OOB area. */
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · On Thu, 7 Jan 2016 09:53:08 -0800 Brian Norris <[email protected]>…

Using switch/case helps make this logic more clear and more robust. With this structure:

* it's clear that this driver only support ECC_{HW,SOFT,SOFT_BCH}; and

Quoted 7 lines

* we can sanely handle new ECC unsupported modes (right now, this code makes incorrect assumptions about the possible values in the nand_ecc_modes_t enum; e.g., what happens with NAND_ECC_HW_OOB_FIRST?)

Cc Alex Smith <[email protected]>
Cc Harvey Hunt <[email protected]>

LGTM

Quoted 59 lines

---
Compile tested only

 drivers/mtd/nand/jz4780_nand.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
drivers/mtd/nand/jz4780_nand.c +19 -15
index 17eb9f264187..aabee8f5627e 100644
@@ -171,29 +171,33 @@ static int jz4780_nand_init_ecc(struct jz4780_nand_chip *nand, struct device *de
 	chip->ecc.bytes = fls((1 + 8) * chip->ecc.size)	*
 				(chip->ecc.strength / 8);
 
-	if (nfc->bch && chip->ecc.mode == NAND_ECC_HW) {
+	switch (chip->ecc.mode) {
+	case NAND_ECC_HW:
+		if (!nfc->bch) {
+			dev_err(dev, "HW BCH selected, but BCH controller not found\n");
+			return -ENODEV;
+		}
+
 		chip->ecc.hwctl = jz4780_nand_ecc_hwctl;
 		chip->ecc.calculate = jz4780_nand_ecc_calculate;
 		chip->ecc.correct = jz4780_nand_ecc_correct;
-	} else if (!nfc->bch && chip->ecc.mode == NAND_ECC_HW) {
-		dev_err(dev, "HW BCH selected, but BCH controller not found\n");
-		return -ENODEV;
-	}
-
-	if (chip->ecc.mode == NAND_ECC_HW_SYNDROME) {
-		dev_err(dev, "ECC HW syndrome not supported\n");
-		return -EINVAL;
-	}
-
-	if (chip->ecc.mode != NAND_ECC_NONE)
+		/* fall through */
+	case NAND_ECC_SOFT:
+	case NAND_ECC_SOFT_BCH:
 		dev_info(dev, "using %s (strength %d, size %d, bytes %d)\n",
 			(nfc->bch) ? "hardware BCH" : "software ECC",
 			chip->ecc.strength, chip->ecc.size, chip->ecc.bytes);
-	else
+		break;
+	case NAND_ECC_NONE:
 		dev_info(dev, "not using ECC\n");
+		break;
+	default:
+		dev_err(dev, "ECC mode %d not supported\n", chip->ecc.mode);
+		return -EINVAL;
+	}
 
-	/* The NAND core will generate the ECC layout. */
-	if (chip->ecc.mode == NAND_ECC_SOFT || chip->ecc.mode == NAND_ECC_SOFT_BCH)
+	/* The NAND core will generate the ECC layout for SW ECC */
+	if (chip->ecc.mode != NAND_ECC_HW)
 		return 0;
 
 	/* Generate ECC layout. ECC codes are right aligned in the OOB area. */
Signature · 3 lines
Boris Brezillon, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
#2 lore raw

Hi,

Quoted 12 lines · Boris Brezillon
Quoted 9 lines · On Thu, 7 Jan 2016 09:53:08 -0800 Brian Norris <[email protected]>…

Using switch/case helps make this logic more clear and more robust. With this structure:

* it's clear that this driver only support ECC_{HW,SOFT,SOFT_BCH}; and

* we can sanely handle new ECC unsupported modes (right now, this code makes incorrect assumptions about the possible values in the nand_ecc_modes_t enum; e.g., what happens with NAND_ECC_HW_OOB_FIRST?)

Quoted 8 lines
Quoted 3 lines
Cc Alex Smith <[email protected]>
Cc Harvey Hunt <[email protected]>

LGTM

If it helps, I can add my Tested-By later today?

Harvey

#3 lore raw
Quoted 19 lines · On Fri, 8 Jan 2016 12:30:32 +0000 Harvey Hunt <[email protected]> wrote:

Hi,

Quoted 16 lines · Boris Brezillon
Quoted 12 lines · On Thu, 7 Jan 2016 09:53:08 -0800 Brian Norris <[email protected]>…

Using switch/case helps make this logic more clear and more robust. With this structure:

* it's clear that this driver only support ECC_{HW,SOFT,SOFT_BCH}; and

* we can sanely handle new ECC unsupported modes (right now, this code makes incorrect assumptions about the possible values in the nand_ecc_modes_t enum; e.g., what happens with NAND_ECC_HW_OOB_FIRST?)

Cc Alex Smith <[email protected]>
Cc Harvey Hunt <[email protected]>
Quoted 6 lines
Quoted 4 lines

LGTM

If it helps, I can add my Tested-By later today?

Yes, that would be good, I only reviewed the changes, a real test would confirm it does not break anything.

Signature · 3 lines
Boris Brezillon, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
Tested-by Acked-by on v1 1/1
#4 lore raw
In reply to Brian Norris

Hi Brian,

Quoted 62 lines · Brian Norris

Using switch/case helps make this logic more clear and more robust. With this structure:

* it's clear that this driver only support ECC_{HW,SOFT,SOFT_BCH}; and

* we can sanely handle new ECC unsupported modes (right now, this code makes incorrect assumptions about the possible values in the nand_ecc_modes_t enum; e.g., what happens with NAND_ECC_HW_OOB_FIRST?)

Cc Alex Smith <[email protected]>
Cc Harvey Hunt <[email protected]>

---
Compile tested only

  drivers/mtd/nand/jz4780_nand.c | 34 +++++++++++++++++++---------------
  1 file changed, 19 insertions(+), 15 deletions(-)
drivers/mtd/nand/jz4780_nand.c +15 -13
index 17eb9f264187..aabee8f5627e 100644
@@ -171,29 +171,33 @@ static int jz4780_nand_init_ecc(struct jz4780_nand_chip *nand, struct device *de
  	chip->ecc.bytes = fls((1 + 8) * chip->ecc.size)	*
  				(chip->ecc.strength / 8);
 
-	if (nfc->bch && chip->ecc.mode == NAND_ECC_HW) {
+	switch (chip->ecc.mode) {
+	case NAND_ECC_HW:
+		if (!nfc->bch) {
+			dev_err(dev, "HW BCH selected, but BCH controller not found\n");
+			return -ENODEV;
+		}
+
  		chip->ecc.hwctl = jz4780_nand_ecc_hwctl;
  		chip->ecc.calculate = jz4780_nand_ecc_calculate;
  		chip->ecc.correct = jz4780_nand_ecc_correct;
-	} else if (!nfc->bch && chip->ecc.mode == NAND_ECC_HW) {
-		dev_err(dev, "HW BCH selected, but BCH controller not found\n");
-		return -ENODEV;
-	}
-
-	if (chip->ecc.mode == NAND_ECC_HW_SYNDROME) {
-		dev_err(dev, "ECC HW syndrome not supported\n");
-		return -EINVAL;
-	}
-
-	if (chip->ecc.mode != NAND_ECC_NONE)
+		/* fall through */
+	case NAND_ECC_SOFT:
+	case NAND_ECC_SOFT_BCH:
  		dev_info(dev, "using %s (strength %d, size %d, bytes %d)\n",
  			(nfc->bch) ? "hardware BCH" : "software ECC",
  			chip->ecc.strength, chip->ecc.size, chip->ecc.bytes);
-	else
+		break;
+	case NAND_ECC_NONE:
  		dev_info(dev, "not using ECC\n");
+		break;
+	default:
+		dev_err(dev, "ECC mode %d not supported\n", chip->ecc.mode);

drivers/mtd/nand/jz4780_nand.c static int jz4780_nand_init_ecc(struct jz4780_nand_chip *nand, struct device *de · line 203

Quoted 10 lines
drivers/mtd/nand/jz4780_nand.c +4 -2
+		return -EINVAL;
+	}
 
-	/* The NAND core will generate the ECC layout. */
-	if (chip->ecc.mode == NAND_ECC_SOFT || chip->ecc.mode == NAND_ECC_SOFT_BCH)
+	/* The NAND core will generate the ECC layout for SW ECC */
+	if (chip->ecc.mode != NAND_ECC_HW)
  		return 0;
 
  	/* Generate ECC layout. ECC codes are right aligned in the OOB area. */

I've just tested this on a Ci20 and all seems well.

Thanks,

Harvey

#5 lore raw
Quoted 12 lines · Harvey Hunt

Hi Brian,

Quoted 9 lines · Brian Norris

Using switch/case helps make this logic more clear and more robust. With this structure:

* it's clear that this driver only support ECC_{HW,SOFT,SOFT_BCH}; and

* we can sanely handle new ECC unsupported modes (right now, this code makes incorrect assumptions about the possible values in the nand_ecc_modes_t enum; e.g., what happens with NAND_ECC_HW_OOB_FIRST?)

Quoted 8 lines
Quoted 3 lines
Cc Alex Smith <[email protected]>
Cc Harvey Hunt <[email protected]>

I've just tested this on a Ci20 and all seems well.

Great, applied to l2-mtd.git