Skip to contentSkip to conversation

MTD: Add nand_ecc test module

PATCH In review Akinobu Mita 3 participants 9 messages Oct 9, 2009 – Oct 15, 2009

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

PATCH 1/1 MTD: Add nand_ecc test module

This module tests NAND ECC functions.

The test is simple.

1. Create a 256 or 512 bytes block of data filled with random bytes (data)
2. Duplicate the data block and inject single bit error (error_data)
3. Try to correct error_data
4. Compare data and error_data

If the test fails, this creates create these files in debugfs:

/sys/kernel/debug/nand-ecc-test-crashers/

<testname>
<testname>-error

<testname> contains data
<testname>-error contains error_data

---
 drivers/mtd/nand/nand_ecc.c       |   25 ++++-
 drivers/mtd/tests/Makefile        |    1 +
 drivers/mtd/tests/nand_ecc-test.c |  200 +++++++++++++++++++++++++++++++++++++
 include/linux/mtd/nand_ecc.h      |    6 +
 4 files changed, 227 insertions(+), 5 deletions(-)
 create mode 100644 drivers/mtd/tests/nand_ecc-test.c
drivers/mtd/nand/nand_ecc.c +20 -5
index db7ae9d..809fb53 100644
@@ -150,20 +150,19 @@ static const char addressbits[256] = {
 };
 
 /**
- * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256/512-byte
+ * __nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256/512-byte
  *			 block
- * @mtd:	MTD block structure
  * @buf:	input buffer with raw data
+ * @eccsize:	data bytes per ecc step (256 or 512)
  * @code:	output buffer with ECC
  */
-int nand_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf,
+void __nand_calculate_ecc(const unsigned char *buf, unsigned int eccsize,
 		       unsigned char *code)
 {
 	int i;
 	const uint32_t *bp = (uint32_t *)buf;
 	/* 256 or 512 bytes/ecc  */
-	const uint32_t eccsize_mult =
-			(((struct nand_chip *)mtd->priv)->ecc.size) >> 8;
+	const uint32_t eccsize_mult = eccsize >> 8;
 	uint32_t cur;		/* current value in buffer */
 	/* rp0..rp15..rp17 are the various accumulated parities (per byte) */
 	uint32_t rp0, rp1, rp2, rp3, rp4, rp5, rp6, rp7;
@@ -412,6 +411,22 @@ int nand_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf,
 		    (invparity[par & 0x55] << 2) |
 		    (invparity[rp17] << 1) |
 		    (invparity[rp16] << 0);
+}
+EXPORT_SYMBOL(__nand_calculate_ecc);
+
+/**
+ * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256/512-byte
+ *			 block
+ * @mtd:	MTD block structure
+ * @buf:	input buffer with raw data
+ * @code:	output buffer with ECC
+ */
+int nand_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf,
+		       unsigned char *code)
+{
+	__nand_calculate_ecc(buf,
+			((struct nand_chip *)mtd->priv)->ecc.size, code);
+
 	return 0;
 }
 EXPORT_SYMBOL(nand_calculate_ecc);
drivers/mtd/tests/Makefile +1 -0
index c1d5013..0c23b29 100644
@@ -5,3 +5,4 @@ obj-$(CONFIG_MTD_TESTS) += mtd_speedtest.o
 obj-$(CONFIG_MTD_TESTS) += mtd_stresstest.o
 obj-$(CONFIG_MTD_TESTS) += mtd_subpagetest.o
 obj-$(CONFIG_MTD_TESTS) += mtd_torturetest.o
+obj-$(CONFIG_MTD_TESTS) += nand_ecc-test.o
drivers/mtd/tests/nand_ecc-test.c new file +200 -0

200 lines not on this page · raw message

include/linux/mtd/nand_ecc.h +6 -0
index 052ea8c..9cb10ff 100644
@@ -16,6 +16,12 @@
 struct mtd_info;
 
 /*
+ * Calculate 3 byte ECC code for eccsize byte block
+ */
+void __nand_calculate_ecc(const u_char *dat, unsigned int eccsize,
+				u_char *ecc_code);
+
+/*
  * Calculate 3 byte ECC code for 256 byte block
  */
 int nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code);
Signature · 1 line
1.5.4.3
#1 lore raw
Quoted 8 lines · Akinobu Mita

This module tests NAND ECC functions.

The test is simple.

1. Create a 256 or 512 bytes block of data filled with random bytes (data)
2. Duplicate the data block and inject single bit error (error_data)
3. Try to correct error_data
4. Compare data and error_data

So you want to test the built-in 256/512 software ECC calculation and correction functions?

Quoted 9 lines

If the test fails, this creates create these files in debugfs:

/sys/kernel/debug/nand-ecc-test-crashers/

<testname>
<testname>-error

<testname> contains data
<testname>-error contains error_data

Why you need these files? This looks like overkill. Please, remove them.

Quoted 10 lines
---
 drivers/mtd/nand/nand_ecc.c       |   25 ++++-
 drivers/mtd/tests/Makefile        |    1 +
 drivers/mtd/tests/nand_ecc-test.c |  200 +++++++++++++++++++++++++++++++++++++
 include/linux/mtd/nand_ecc.h      |    6 +
 4 files changed, 227 insertions(+), 5 deletions(-)
 create mode 100644 drivers/mtd/tests/nand_ecc-test.c

Could you please split this on 2 patches - one is changing the generic code, one is the test itself.

Quoted 181 lines
drivers/mtd/nand/nand_ecc.c +20 -5
index db7ae9d..809fb53 100644
@@ -150,20 +150,19 @@ static const char addressbits[256] = {
 };
 
 /**
- * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256/512-byte
+ * __nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256/512-byte
  *			 block
- * @mtd:	MTD block structure
  * @buf:	input buffer with raw data
+ * @eccsize:	data bytes per ecc step (256 or 512)
  * @code:	output buffer with ECC
  */
-int nand_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf,
+void __nand_calculate_ecc(const unsigned char *buf, unsigned int eccsize,
 		       unsigned char *code)
 {
 	int i;
 	const uint32_t *bp = (uint32_t *)buf;
 	/* 256 or 512 bytes/ecc  */
-	const uint32_t eccsize_mult =
-			(((struct nand_chip *)mtd->priv)->ecc.size) >> 8;
+	const uint32_t eccsize_mult = eccsize >> 8;
 	uint32_t cur;		/* current value in buffer */
 	/* rp0..rp15..rp17 are the various accumulated parities (per byte) */
 	uint32_t rp0, rp1, rp2, rp3, rp4, rp5, rp6, rp7;
@@ -412,6 +411,22 @@ int nand_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf,
 		    (invparity[par & 0x55] << 2) |
 		    (invparity[rp17] << 1) |
 		    (invparity[rp16] << 0);
+}
+EXPORT_SYMBOL(__nand_calculate_ecc);
+
+/**
+ * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256/512-byte
+ *			 block
+ * @mtd:	MTD block structure
+ * @buf:	input buffer with raw data
+ * @code:	output buffer with ECC
+ */
+int nand_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf,
+		       unsigned char *code)
+{
+	__nand_calculate_ecc(buf,
+			((struct nand_chip *)mtd->priv)->ecc.size, code);
+
 	return 0;
 }
 EXPORT_SYMBOL(nand_calculate_ecc);
drivers/mtd/tests/Makefile +1 -0
index c1d5013..0c23b29 100644
@@ -5,3 +5,4 @@ obj-$(CONFIG_MTD_TESTS) += mtd_speedtest.o
 obj-$(CONFIG_MTD_TESTS) += mtd_stresstest.o
 obj-$(CONFIG_MTD_TESTS) += mtd_subpagetest.o
 obj-$(CONFIG_MTD_TESTS) += mtd_torturetest.o
+obj-$(CONFIG_MTD_TESTS) += nand_ecc-test.o
drivers/mtd/tests/nand_ecc-test.c new file +113 -0
new file mode 100644
index 0000000..143630c
@@ -0,0 +1,200 @@
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/debugfs.h>
+#include <linux/list.h>
+#include <linux/slab.h>
+#include <linux/random.h>
+#include <linux/string.h>
+#include <linux/bitops.h>
+#include <linux/jiffies.h>
+
+void inject_single_bit_error(void *data, size_t size)
+{
+	unsigned long offset = random32() % (size * BITS_PER_BYTE);
+
+	__change_bit(offset, data);
+}
+
+static struct dentry *ecc_test_dir;
+
+struct ecc_test_blob {
+	struct dentry *dir;
+	struct debugfs_blob_wrapper blob;
+};
+
+static struct ecc_test_blob *create_ecc_test_blob(const char *name, void *data,
+						size_t size)
+{
+	struct ecc_test_blob *blob;
+
+	blob = kmalloc(sizeof(*blob), GFP_KERNEL);
+	if (!blob)
+		return NULL;
+
+	blob->blob.size = size;
+	blob->blob.data = kmemdup(data, size, GFP_KERNEL);
+	if (!blob->blob.data)
+		goto err1;
+
+	blob->dir = debugfs_create_blob(name, S_IRUSR, ecc_test_dir,
+						&blob->blob);
+	if (!blob->dir)
+		goto err2;
+
+	return blob;
+err2:
+	kfree(blob->blob.data);
+err1:
+	kfree(blob);
+
+	return NULL;
+}
+
+static void remove_ecc_test_blob(struct ecc_test_blob *blob)
+{
+	debugfs_remove(blob->dir);
+	kfree(blob->blob.data);
+	kfree(blob);
+}
+
+static LIST_HEAD(ecc_test_crashers);
+
+struct ecc_test_crasher {
+	struct list_head list;
+
+	struct ecc_test_blob *data;
+	struct ecc_test_blob *error_data;
+};
+
+/*
+ * Create these files in debugfs:
+ *
+ * /sys/kernel/debug/nand-ecc-test-crashers/
+ *	<name>		binary blob specified with data
+ *	<name>-error	binary blob specified with error_data
+ */
+static int register_ecc_test_crasher(const char *name, void *data,
+				void *error_data, size_t size)
+{
+	struct ecc_test_crasher *crasher;
+	char *error_name;
+
+	crasher = kmalloc(sizeof(*crasher), GFP_KERNEL);
+	if (!crasher)
+		return -ENOMEM;
+
+	crasher->data = create_ecc_test_blob(name, data, size);
+	if (!crasher->data)
+		goto err1;
+
+	error_name = kasprintf(GFP_KERNEL, "%s-error", name);
+	if (!error_name)
+		goto err2;
+
+	crasher->error_data = create_ecc_test_blob(error_name, error_data,
+							size);
+	kfree(error_name);
+
+	if (!crasher->error_data)
+		goto err3;
+
+	list_add(&crasher->list, &ecc_test_crashers);
+
+	return 0;
+err3:
+	remove_ecc_test_blob(crasher->error_data);
+err2:
+	remove_ecc_test_blob(crasher->data);
+err1:
+	kfree(crasher);
+
+	return -ENOMEM;
+}
+

drivers/mtd/tests/nand_ecc-test.c line 123

Quoted 10 lines
drivers/mtd/tests/nand_ecc-test.c new file +10 -0
+static void unregister_ecc_test_crasher(struct ecc_test_crasher *crasher)
+{
+	list_del(&crasher->list);
+
+	remove_ecc_test_blob(crasher->error_data);
+	remove_ecc_test_blob(crasher->data);
+	kfree(crasher);
+}
+
+#if defined(CONFIG_MTD_NAND) || defined(CONFIG_MTD_NAND_MODULE)

This should not be needed. MTD tests are always compiled as modules, so you should not have copilation errors. And if MTD NAND support is not present, modprobe will just fail.

Signature · 2 lines
Best Regards,
Artem Bityutskiy (Артём Битюцкий)
#2 lore raw
Quoted 5 lines · Artem Bityutskiy
Quoted 4 lines · Akinobu Mita

This module tests NAND ECC functions.

The test is simple.

Quoted 7 lines
Quoted 4 lines

1. Create a 256 or 512 bytes block of data filled with random bytes (data)
2. Duplicate the data block and inject single bit error (error_data)
3. Try to correct error_data
4. Compare data and error_data

So you want to test the built-in 256/512 software ECC calculation and correction functions?

Yes.

Quoted 4 lines
Quoted 4 lines

If the test fails, this creates create these files in debugfs:

/sys/kernel/debug/nand-ecc-test-crashers/

Quoted 8 lines
Quoted 5 lines

<testname>
<testname>-error

<testname> contains data
<testname>-error contains error_data

Why you need these files? This looks like overkill. Please, remove them.

OK. I'll remove them and just do hexdump() if the test fails.

Quoted 11 lines
Quoted 10 lines
---
 drivers/mtd/nand/nand_ecc.c       |   25 ++++-
 drivers/mtd/tests/Makefile        |    1 +
 drivers/mtd/tests/nand_ecc-test.c |  200 +++++++++++++++++++++++++++++++++++++
 include/linux/mtd/nand_ecc.h      |    6 +
 4 files changed, 227 insertions(+), 5 deletions(-)
 create mode 100644 drivers/mtd/tests/nand_ecc-test.c
Quoted 2 lines

Could you please split this on 2 patches - one is changing the generic code, one is the test itself.

OK.

...

Quoted 6 lines
Quoted 6 lines
drivers/mtd/tests/nand_ecc-test.c new file +6 -0
+static void unregister_ecc_test_crasher(struct ecc_test_crasher *crasher)
+{
+	list_del(&crasher->list);
+
+	remove_ecc_test_blob(crasher->error_data);
+	remove_ecc_test_blob(crasher->data);
Quoted 8 lines

drivers/mtd/tests/nand_ecc-test.c line 123

Quoted 4 lines
drivers/mtd/tests/nand_ecc-test.c new file +4 -0
+	kfree(crasher);
+}
+
+#if defined(CONFIG_MTD_NAND) || defined(CONFIG_MTD_NAND_MODULE)

This should not be needed. MTD tests are always compiled as modules, so you should not have copilation errors. And if MTD NAND support is not present, modprobe will just fail.

nand_calculate_ecc and nand_correct_data needs CONFIG_MTD_NAND=y|m, otherwise it will get link error:

ERROR: "__nand_correct_data" [drivers/mtd/tests/nand_ecc-test.ko] undefined!
ERROR: "__nand_calculate_ecc" [drivers/mtd/tests/nand_ecc-test.ko] undefined!

#3 lore raw
Quoted 9 lines · Akinobu Mita
Quoted 3 lines

This should not be needed. MTD tests are always compiled as modules, so you should not have copilation errors. And if MTD NAND support is not present, modprobe will just fail.

nand_calculate_ecc and nand_correct_data needs CONFIG_MTD_NAND=y|m, otherwise it will get link error:

ERROR: "__nand_correct_data" [drivers/mtd/tests/nand_ecc-test.ko] undefined!
ERROR: "__nand_calculate_ecc" [drivers/mtd/tests/nand_ecc-test.ko] undefined!

How about splitting tests on 2 parts - one part which does not require NAND, and one which does, and make the second part compile only if NAND is enabled ("depends" operator in Kconfig) ?

Signature · 2 lines
Best Regards,
Artem Bityutskiy (Артём Битюцкий)
#4 lore raw
Quoted 8 lines · Artem Bityutskiy
Quoted 7 lines · Akinobu Mita
Quoted 3 lines

This should not be needed. MTD tests are always compiled as modules, so you should not have copilation errors. And if MTD NAND support is not present, modprobe will just fail.

nand_calculate_ecc and nand_correct_data needs CONFIG_MTD_NAND=y|m, otherwise it will get link error:

Quoted 6 lines
Quoted 2 lines

ERROR: "__nand_correct_data" [drivers/mtd/tests/nand_ecc-test.ko] undefined!
ERROR: "__nand_calculate_ecc" [drivers/mtd/tests/nand_ecc-test.ko] undefined!

How about splitting tests on 2 parts - one part which does not require NAND, and one which does, and make the second part compile only if NAND is enabled ("depends" operator in Kconfig) ?

I thought purpose of this test was to test correctness of 'nand_calculate_ecc' and 'nand_correct_data' functions (i.e. algos), which are only compiled and available when NAND is enabled.

-vimal

Quoted 10 lines

--
Best Regards,
Artem Bityutskiy (Артём Битюцкий)

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

#5 lore raw
Quoted 12 lines · vimal singh
Quoted 11 lines · Artem Bityutskiy
Quoted 9 lines · Akinobu Mita
Quoted 3 lines

This should not be needed. MTD tests are always compiled as modules, so you should not have copilation errors. And if MTD NAND support is not present, modprobe will just fail.

nand_calculate_ecc and nand_correct_data needs CONFIG_MTD_NAND=y|m, otherwise it will get link error:

ERROR: "__nand_correct_data" [drivers/mtd/tests/nand_ecc-test.ko] undefined!
ERROR: "__nand_calculate_ecc" [drivers/mtd/tests/nand_ecc-test.ko] undefined!

Quoted 7 lines
Quoted 3 lines

How about splitting tests on 2 parts - one part which does not require NAND, and one which does, and make the second part compile only if NAND is enabled ("depends" operator in Kconfig) ?

I thought purpose of this test was to test correctness of 'nand_calculate_ecc' and 'nand_correct_data' functions (i.e. algos), which are only compiled and available when NAND is enabled.

That is exactly what I meant as well.

Signature · 2 lines
Best Regards,
Artem Bityutskiy (Артём Битюцкий)
#6 lore raw
Quoted 17 lines · Artem Bityutskiy
Quoted 16 lines · vimal singh
Quoted 14 lines · Artem Bityutskiy
Quoted 9 lines · Akinobu Mita
Quoted 3 lines

This should not be needed. MTD tests are always compiled as modules, so you should not have copilation errors. And if MTD NAND support is not present, modprobe will just fail.

nand_calculate_ecc and nand_correct_data needs CONFIG_MTD_NAND=y|m, otherwise it will get link error:

ERROR: "__nand_correct_data" [drivers/mtd/tests/nand_ecc-test.ko] undefined!
ERROR: "__nand_calculate_ecc" [drivers/mtd/tests/nand_ecc-test.ko] undefined!

How about splitting tests on 2 parts - one part which does not require NAND, and one which does, and make the second part compile only if NAND is enabled ("depends" operator in Kconfig) ?

Quoted 5 lines
Quoted 3 lines

I thought purpose of this test was to test correctness of 'nand_calculate_ecc' and 'nand_correct_data' functions (i.e. algos), which are only compiled and available when NAND is enabled.

That is exactly what I meant as well.

Then, what do you meant to test in first part of test. Sorry if I am misinterpreting you statement above.

Signature · 2 lines
Regards,
Vimal Singh
Quoted 6 lines

--
Best Regards,
Artem Bityutskiy (Артём Битюцкий)

#7 lore raw
Quoted 18 lines · vimal singh
Quoted 17 lines · Artem Bityutskiy
Quoted 16 lines · vimal singh
Quoted 14 lines · Artem Bityutskiy
Quoted 9 lines · Akinobu Mita
Quoted 3 lines

This should not be needed. MTD tests are always compiled as modules, so you should not have copilation errors. And if MTD NAND support is not present, modprobe will just fail.

nand_calculate_ecc and nand_correct_data needs CONFIG_MTD_NAND=y|m, otherwise it will get link error:

ERROR: "__nand_correct_data" [drivers/mtd/tests/nand_ecc-test.ko] undefined!
ERROR: "__nand_calculate_ecc" [drivers/mtd/tests/nand_ecc-test.ko] undefined!

How about splitting tests on 2 parts - one part which does not require NAND, and one which does, and make the second part compile only if NAND is enabled ("depends" operator in Kconfig) ?

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

I thought purpose of this test was to test correctness of 'nand_calculate_ecc' and 'nand_correct_data' functions (i.e. algos), which are only compiled and available when NAND is enabled.

That is exactly what I meant as well.

Then, what do you meant to test in first part of test. Sorry if I am misinterpreting you statement above.

May be this is me who is misunderstanding. We have tests which work fine with both NOR and NAND. But some tests work only on NOR. E.g., the subpage test. We did not have _compile_ dependencies, so it was not a problem. NAND-only tests just returned on !NANDs.

This new test requeres NAND support to be compiled.

So what I suggested, is to split the tests on 2 groups on the Kconfig level, and make the NAND-only group depend on NAND support. This will make them be compiled only if NAND support is there. Then this new test will be part of the NAND-only group.

I'm not 100% sure this is nice approach, though. But #ifdef NAND things in the test are ugly, IMO.

Signature · 2 lines
Best Regards,
Artem Bityutskiy (Артём Битюцкий)
#8 lore raw
Quoted 35 lines · Artem Bityutskiy
Quoted 18 lines · vimal singh
Quoted 17 lines · Artem Bityutskiy
Quoted 16 lines · vimal singh
Quoted 14 lines · Artem Bityutskiy
Quoted 9 lines · Akinobu Mita
Quoted 3 lines

This should not be needed. MTD tests are always compiled as modules, so you should not have copilation errors. And if MTD NAND support is not present, modprobe will just fail.

nand_calculate_ecc and nand_correct_data needs CONFIG_MTD_NAND=y|m, otherwise it will get link error:

ERROR: "__nand_correct_data" [drivers/mtd/tests/nand_ecc-test.ko] undefined!
ERROR: "__nand_calculate_ecc" [drivers/mtd/tests/nand_ecc-test.ko] undefined!

How about splitting tests on 2 parts - one part which does not require NAND, and one which does, and make the second part compile only if NAND is enabled ("depends" operator in Kconfig) ?

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

I thought purpose of this test was to test correctness of 'nand_calculate_ecc' and 'nand_correct_data' functions (i.e. algos), which are only compiled and available when NAND is enabled.

That is exactly what I meant as well.

Then, what do you meant to test in first part of test. Sorry if I am misinterpreting you statement above.

May be this is me who is misunderstanding. We have tests which work fine with both NOR and NAND. But some tests work only on NOR. E.g., the subpage test. We did not have _compile_ dependencies, so it was not a problem. NAND-only tests just returned on !NANDs.

This new test requeres NAND support to be compiled.

Quoted 7 lines

So what I suggested, is to split the tests on 2 groups on the Kconfig level, and make the NAND-only group depend on NAND support. This will make them be compiled only if NAND support is there. Then this new test will be part of the NAND-only group.

I'm not 100% sure this is nice approach, though. But #ifdef NAND things in the test are ugly, IMO.

To me this approach looks fine. I do have seen that oobtest test (scenario 4) does not holds good for NAND parts. Since NAND driver allows to read past the page.

Signature · 2 lines
Regards,
Vimal Singh