From patchwork Fri Oct 9 09:54:55 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akinobu Mita X-Patchwork-Id: 35608 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [18.85.46.34]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 324BBB7B76 for ; Fri, 9 Oct 2009 20:56:41 +1100 (EST) Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.69 #1 (Red Hat Linux)) id 1MwCBs-0003pB-7p; Fri, 09 Oct 2009 09:55:12 +0000 Received: from mail-gx0-f210.google.com ([209.85.217.210]) by bombadil.infradead.org with esmtp (Exim 4.69 #1 (Red Hat Linux)) id 1MwCBg-0003Lm-Kc for linux-mtd@lists.infradead.org; Fri, 09 Oct 2009 09:55:08 +0000 Received: by gxk2 with SMTP id 2so1539359gxk.4 for ; Fri, 09 Oct 2009 02:54:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:from:to:cc:subject:date :message-id:x-mailer:in-reply-to:references; bh=Eowpz9AnbkeifIHF0reyP6WvXQ4EVHWh275aqn7MXzs=; b=khrDrYzyTQYGc77+6Yr0rMJrTcXD93E+ZLMNAw0n37fVtTbVw28HAqeJ0Vs/AfWntQ yyJRgpOMhZir92w38uYs/mtqG62GE/X1vmcGFV9aElfJjj1jpoaVROCp2Nx+Myhmj+Rw V3Lob6DqgUMiKrigts/dtRbsVbwmlw/5bp2Kg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; b=U6yfeSxCazcxM4rznz6LmuLUNKnD8Yk+8eP6eAGwuc+0SaSzlv7OBcOAkWn5lGOTf1 E/g4UjOPIIqD1oRfO3WnKvGhkulrXSzZv93RPEQZrgPRooAgbOQ+Exq/vhyfjF6pME1c cLgeR1Uv0dVCepu3bXIzLL5dSHLhP6/Yx8QP0= Received: by 10.101.90.12 with SMTP id s12mr2826780anl.77.1255082099301; Fri, 09 Oct 2009 02:54:59 -0700 (PDT) Received: from localhost ([220.110.185.192]) by mx.google.com with ESMTPS id 20sm510393ywh.14.2009.10.09.02.54.58 (version=TLSv1/SSLv3 cipher=RC4-MD5); Fri, 09 Oct 2009 02:54:58 -0700 (PDT) From: Akinobu Mita To: linux-mtd@lists.infradead.org Subject: [PATCH] MTD: Add nand_ecc test module Date: Fri, 9 Oct 2009 18:54:55 +0900 Message-Id: <1255082095-22158-1-git-send-email-akinobu.mita@gmail.com> X-Mailer: git-send-email 1.5.4.3 In-Reply-To: <> References: <> X-CRM114-Version: 20090807-BlameThorstenAndJenny ( TRE 0.7.6 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20091009_055500_808906_C25E99EB X-CRM114-Status: GOOD ( 16.57 ) X-Spam-Score: 0.0 (/) X-Spam-Report: SpamAssassin version 3.2.5 on bombadil.infradead.org summary: Content analysis details: (0.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- _SUMMARY_ Cc: David Woodhouse , Akinobu Mita X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: linux-mtd-bounces@lists.infradead.org Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org 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/ -error contains data -error contains error_data Cc: David Woodhouse Cc: linux-mtd@lists.infradead.org Signed-off-by: Akinobu Mita --- 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 diff --git a/drivers/mtd/nand/nand_ecc.c b/drivers/mtd/nand/nand_ecc.c index db7ae9d..809fb53 100644 --- a/drivers/mtd/nand/nand_ecc.c +++ b/drivers/mtd/nand/nand_ecc.c @@ -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); diff --git a/drivers/mtd/tests/Makefile b/drivers/mtd/tests/Makefile index c1d5013..0c23b29 100644 --- a/drivers/mtd/tests/Makefile +++ b/drivers/mtd/tests/Makefile @@ -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 diff --git a/drivers/mtd/tests/nand_ecc-test.c b/drivers/mtd/tests/nand_ecc-test.c new file mode 100644 index 0000000..143630c --- /dev/null +++ b/drivers/mtd/tests/nand_ecc-test.c @@ -0,0 +1,200 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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/ + * binary blob specified with data + * -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; +} + +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) + +#include + +static unsigned char data[512]; +static unsigned char error_data[512]; + +static int nand_ecc_test(const size_t size) +{ + unsigned char code[3]; + unsigned char error_code[3]; + char testname[30]; + + BUG_ON(sizeof(data) < size); + + sprintf(testname, "nand-ecc-%zu", size); + + get_random_bytes(data, size); + + memcpy(error_data, data, size); + inject_single_bit_error(error_data, size); + + __nand_calculate_ecc(data, size, code); + __nand_calculate_ecc(error_data, size, error_code); + __nand_correct_data(error_data, code, error_code, size); + + if (!memcmp(data, error_data, size)) { + printk(KERN_INFO "nand_ecc-test: ok - %s\n", testname); + return 0; + } + + printk(KERN_ERR "nand_ecc-test: not ok - %s\n", testname); + register_ecc_test_crasher(testname, data, error_data, size); + + return -1; +} + +#else + +static int nand_ecc_test(const size_t size) +{ + return 0; +} + +#endif + +static int __init ecc_test_init(void) +{ + srandom32(jiffies); + + ecc_test_dir = debugfs_create_dir("nand-ecc-test-crashers", NULL); + if (!ecc_test_dir) + return -ENOMEM; + + nand_ecc_test(256); + nand_ecc_test(512); + + return 0; +} + +static void __exit ecc_test_exit(void) +{ + while (!list_empty(&ecc_test_crashers)) { + struct ecc_test_crasher *crasher; + + crasher = list_first_entry(&ecc_test_crashers, + struct ecc_test_crasher, list); + unregister_ecc_test_crasher(crasher); + } + debugfs_remove_recursive(ecc_test_dir); +} + +module_init(ecc_test_init); +module_exit(ecc_test_exit); + +MODULE_DESCRIPTION("ECC test module"); +MODULE_AUTHOR("Akinobu Mita"); +MODULE_LICENSE("GPL"); diff --git a/include/linux/mtd/nand_ecc.h b/include/linux/mtd/nand_ecc.h index 052ea8c..9cb10ff 100644 --- a/include/linux/mtd/nand_ecc.h +++ b/include/linux/mtd/nand_ecc.h @@ -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);