1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/nand.h>
#include <linux/delay.h>
#include <asm/div64.h>
#include <linux/err.h>
#include <linux/random.h>
#include <linux/vmalloc.h>
#undef pr_fmt
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
static int dev = 4;
module_param(dev, int, S_IRUGO);
MODULE_PARM_DESC(dev, "MTD device number to use");
static int ebcnt = 0;
static int pgcnt = 0;
static unsigned char *bbt = NULL;
static struct mtd_info *mtd = NULL;
static struct rnd_state rnd_state = {0};
static int agetest_erase_eraseblock(unsigned int ebnum)
{
int err = 0;
struct erase_info ei = {0};
ei.mtd = mtd;
ei.addr = ebnum * mtd->erasesize;
ei.len = mtd->erasesize;
err = mtd_erase(mtd, &ei);
if (err) {
pr_err("err %d while erasing EB %d\n", err, ebnum);
return err;
}
if (ei.state == MTD_ERASE_FAILED) {
pr_err("some erase error occurred at EB %d\n", ebnum);
return -EIO;
}
return 0;
}
static int agetest_scan_bad_block(void)
{
unsigned int i = 0, bad = 0;
bbt = (unsigned char *)vmalloc(ebcnt);
if (!bbt) {
pr_info("error: cannot allocate memory\n");
return -ENOMEM;
}
pr_info("scanning for bad eraseblocks first\n");
for (i = 0; i < ebcnt; ++i) {
bbt[i] = mtd_block_isbad(mtd, i * mtd->erasesize) ? 1 : 0;
if (bbt[i])
bad += 1;
cond_resched();
}
pr_info("scanned %d eraseblocks, %d are bad\n", ebcnt, bad);
return 0;
}
static int nandflash_agetest(void)
{
int err = 0;
loff_t addr = 0;
unsigned int test_blk = 10;
unsigned int op = 0;
unsigned char *readbuf = NULL;
unsigned char *writebuf = NULL;
unsigned int written = 0, read = 0;
unsigned int continue_err = 0;
unsigned int erase_time = 0;
readbuf = vmalloc(mtd->writesize);
if(!readbuf) {
pr_err("%s, alloc readbuf failed!\n", __func__);
err = -ENOMEM;
goto out;
}
writebuf = vmalloc(mtd->writesize);
if(!writebuf) {
pr_err("%s, alloc writebuf failed!\n", __func__);
err = -ENOMEM;
goto out;
}
while((test_blk < ebcnt) && bbt[test_blk]) {
test_blk ++;
}
if(test_blk >= ebcnt) {
pr_err("The whole mtd_partion are bad!\n");
err = -EINVAL;
goto out;
}
pr_info("Begin to make block %d as bad!\n", test_blk);
addr = test_blk * mtd->erasesize;
prandom_seed_state(&rnd_state, 1);
prandom_bytes_state(&rnd_state, writebuf, mtd->writesize);
err = agetest_erase_eraseblock(test_blk);
if(err) {
pr_err("the first erase failed at addr: 0x%.8x, err: %d\n", (unsigned int)addr, err);
goto out;
}
while(1) {
if(signal_pending(current))
goto out;
err = mtd_write(mtd, addr, mtd->writesize, &written, writebuf);
if(err || written != mtd->writesize) {
pr_err("write failed at addr: 0x%.8x, err: %d, written: %d, time: %d\n", (unsigned int)addr, err, written, op);
goto erase;
}
err = mtd_read(mtd, addr, mtd->writesize, &read, readbuf);
if(err || read != mtd->writesize) {
pr_err("read failed at addr: 0x%.8x, err: %d, read: %d, time: %d\n", (unsigned int)addr, err, read, op);
goto erase;
}
if(memcmp(readbuf, writebuf, mtd->writesize) != 0) {
pr_err("verify failed at addr: 0x%.8x!", (unsigned int)addr);
goto erase;
}
erase:
err = agetest_erase_eraseblock(test_blk);
if(err) {
pr_err("erase failed at addr: 0x%.8x, err: %d, time: %d\n", (unsigned int)addr, err, op);
break;
}
op += 1;
if(op % 1024 == 0)
pr_info("we have erased %d times!\n", op);
}
/*make sure the block has been erased as bad block*/
while(1) {
if(signal_pending(current))
break;
err = mtd_write(mtd, addr, mtd->writesize, &written, writebuf);
err = agetest_erase_eraseblock(test_blk);
if(err) {
continue_err ++;
if(continue_err >= 0x1000) {
pr_info("after %d times continue erase, the block has been erased as bad block!", erase_time);
break;
}
if( continue_err % 100 == 0)
pr_info("the continue_err is %d!", continue_err);
} else {
if(continue_err)
pr_info("the continue_err may be clean, old value is %d\n", continue_err);
continue_err = 0;
}
erase_time ++;
if( erase_time % 1024 == 0)
pr_info("the erase_time is %d!", erase_time);
}
pr_info("mark block %d as bad!\n", test_blk);
mtd->_block_markbad(mtd, addr);
err = 0;
out:
vfree(readbuf);
vfree(writebuf);
pr_info("agetest finished, %d(%d) operations done!\n", op, erase_time);
return err;
}
static int __init nandflash_agetest_init(void)
{
int err = 0;
uint64_t tmp = 0;
pr_info(KERN_INFO "\n");
pr_info(KERN_INFO "=================================================\n");
if (dev < 0) {
pr_info("Please specify a valid mtd-device via module parameter\n");
return -EINVAL;
}
pr_info("MTD device: %d\n", dev);
mtd = get_mtd_device(NULL, dev);
if (IS_ERR(mtd)) {
err = PTR_ERR(mtd);
pr_info("error: cannot get MTD device\n");
return err;
}
if (mtd->type != MTD_NANDFLASH) {
pr_info("this test requires NAND flash\n");
goto out;
}
tmp = mtd->size;
do_div(tmp, mtd->erasesize);
ebcnt = tmp;
pgcnt = mtd->erasesize / mtd->writesize;
pr_info("MTD device size %llu, eraseblock size %u, "
"page size %u, count of eraseblocks %u, pages per "
"eraseblock %u, OOB size %u\n",
(unsigned long long)mtd->size, mtd->erasesize,
mtd->writesize, ebcnt, pgcnt, mtd->oobsize);
err = agetest_scan_bad_block();
if (err)
goto out;
err = nandflash_agetest();
if(err) {
pr_err("stresstest failed!\n");
goto out;
}
out:
pr_info(KERN_INFO "=================================================\n");
if(bbt != NULL)
vfree(bbt);
put_mtd_device(mtd);
return -1;
}
static void __exit nandflash_agetest_exit(void)
{
return;
}
module_init(nandflash_agetest_init);
module_exit(nandflash_agetest_exit);
MODULE_DESCRIPTION("mtd_age_test");
MODULE_AUTHOR("ming.tang@spreadtrum.com");
MODULE_LICENSE("GPL");
|