1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2007 Nokia Corporation
4 *
5 * Test read and write speed of a MTD device.
6 *
7 * Author: Adrian Hunter <adrian.hunter@nokia.com>
8 */
9
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12#include <linux/init.h>
13#include <linux/ktime.h>
14#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/err.h>
17#include <linux/mtd/mtd.h>
18#include <linux/slab.h>
19#include <linux/sched.h>
20#include <linux/random.h>
21
22#include "mtd_test.h"
23
24static int dev = -EINVAL;
25module_param(dev, int, S_IRUGO);
26MODULE_PARM_DESC(dev, "MTD device number to use");
27
28static int count;
29module_param(count, int, S_IRUGO);
30MODULE_PARM_DESC(count, "Maximum number of eraseblocks to use "
31			"(0 means use all)");
32
33static struct mtd_info *mtd;
34static unsigned char *iobuf;
35static unsigned char *bbt;
36
37static int pgsize;
38static int ebcnt;
39static int pgcnt;
40static int goodebcnt;
41static ktime_t start, finish;
42
43static int multiblock_erase(int ebnum, int blocks)
44{
45	int err;
46	struct erase_info ei;
47	loff_t addr = (loff_t)ebnum * mtd->erasesize;
48
49	memset(&ei, 0, sizeof(struct erase_info));
50	ei.addr = addr;
51	ei.len  = mtd->erasesize * blocks;
52
53	err = mtd_erase(mtd, &ei);
54	if (err) {
55		pr_err("error %d while erasing EB %d, blocks %d\n",
56		       err, ebnum, blocks);
57		return err;
58	}
59
60	return 0;
61}
62
63static int write_eraseblock(int ebnum)
64{
65	loff_t addr = (loff_t)ebnum * mtd->erasesize;
66
67	return mtdtest_write(mtd, addr, mtd->erasesize, iobuf);
68}
69
70static int write_eraseblock_by_page(int ebnum)
71{
72	int i, err = 0;
73	loff_t addr = (loff_t)ebnum * mtd->erasesize;
74	void *buf = iobuf;
75
76	for (i = 0; i < pgcnt; i++) {
77		err = mtdtest_write(mtd, addr, pgsize, buf);
78		if (err)
79			break;
80		addr += pgsize;
81		buf += pgsize;
82	}
83
84	return err;
85}
86
87static int write_eraseblock_by_2pages(int ebnum)
88{
89	size_t sz = pgsize * 2;
90	int i, n = pgcnt / 2, err = 0;
91	loff_t addr = (loff_t)ebnum * mtd->erasesize;
92	void *buf = iobuf;
93
94	for (i = 0; i < n; i++) {
95		err = mtdtest_write(mtd, addr, sz, buf);
96		if (err)
97			return err;
98		addr += sz;
99		buf += sz;
100	}
101	if (pgcnt % 2)
102		err = mtdtest_write(mtd, addr, pgsize, buf);
103
104	return err;
105}
106
107static int read_eraseblock(int ebnum)
108{
109	loff_t addr = (loff_t)ebnum * mtd->erasesize;
110
111	return mtdtest_read(mtd, addr, mtd->erasesize, iobuf);
112}
113
114static int read_eraseblock_by_page(int ebnum)
115{
116	int i, err = 0;
117	loff_t addr = (loff_t)ebnum * mtd->erasesize;
118	void *buf = iobuf;
119
120	for (i = 0; i < pgcnt; i++) {
121		err = mtdtest_read(mtd, addr, pgsize, buf);
122		if (err)
123			break;
124		addr += pgsize;
125		buf += pgsize;
126	}
127
128	return err;
129}
130
131static int read_eraseblock_by_2pages(int ebnum)
132{
133	size_t sz = pgsize * 2;
134	int i, n = pgcnt / 2, err = 0;
135	loff_t addr = (loff_t)ebnum * mtd->erasesize;
136	void *buf = iobuf;
137
138	for (i = 0; i < n; i++) {
139		err = mtdtest_read(mtd, addr, sz, buf);
140		if (err)
141			return err;
142		addr += sz;
143		buf += sz;
144	}
145	if (pgcnt % 2)
146		err = mtdtest_read(mtd, addr, pgsize, buf);
147
148	return err;
149}
150
151static inline void start_timing(void)
152{
153	start = ktime_get();
154}
155
156static inline void stop_timing(void)
157{
158	finish = ktime_get();
159}
160
161static long calc_speed(void)
162{
163	uint64_t k;
164	long ms;
165
166	ms = ktime_ms_delta(finish, start);
167	if (ms == 0)
168		return 0;
169	k = (uint64_t)goodebcnt * (mtd->erasesize / 1024) * 1000;
170	do_div(k, ms);
171	return k;
172}
173
174static int __init mtd_speedtest_init(void)
175{
176	int err, i, blocks, j, k;
177	long speed;
178	uint64_t tmp;
179
180	printk(KERN_INFO "\n");
181	printk(KERN_INFO "=================================================\n");
182
183	if (dev < 0) {
184		pr_info("Please specify a valid mtd-device via module parameter\n");
185		pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
186		return -EINVAL;
187	}
188
189	if (count)
190		pr_info("MTD device: %d    count: %d\n", dev, count);
191	else
192		pr_info("MTD device: %d\n", dev);
193
194	mtd = get_mtd_device(NULL, dev);
195	if (IS_ERR(mtd)) {
196		err = PTR_ERR(mtd);
197		pr_err("error: cannot get MTD device\n");
198		return err;
199	}
200
201	if (mtd->writesize == 1) {
202		pr_info("not NAND flash, assume page size is 512 "
203		       "bytes.\n");
204		pgsize = 512;
205	} else
206		pgsize = mtd->writesize;
207
208	tmp = mtd->size;
209	do_div(tmp, mtd->erasesize);
210	ebcnt = tmp;
211	pgcnt = mtd->erasesize / pgsize;
212
213	pr_info("MTD device size %llu, eraseblock size %u, "
214	       "page size %u, count of eraseblocks %u, pages per "
215	       "eraseblock %u, OOB size %u\n",
216	       (unsigned long long)mtd->size, mtd->erasesize,
217	       pgsize, ebcnt, pgcnt, mtd->oobsize);
218
219	if (count > 0 && count < ebcnt)
220		ebcnt = count;
221
222	err = -ENOMEM;
223	iobuf = kmalloc(mtd->erasesize, GFP_KERNEL);
224	if (!iobuf)
225		goto out;
226
227	prandom_bytes(iobuf, mtd->erasesize);
228
229	bbt = kzalloc(ebcnt, GFP_KERNEL);
230	if (!bbt)
231		goto out;
232	err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
233	if (err)
234		goto out;
235	for (i = 0; i < ebcnt; i++) {
236		if (!bbt[i])
237			goodebcnt++;
238	}
239
240	err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
241	if (err)
242		goto out;
243
244	/* Write all eraseblocks, 1 eraseblock at a time */
245	pr_info("testing eraseblock write speed\n");
246	start_timing();
247	for (i = 0; i < ebcnt; ++i) {
248		if (bbt[i])
249			continue;
250		err = write_eraseblock(i);
251		if (err)
252			goto out;
253
254		err = mtdtest_relax();
255		if (err)
256			goto out;
257	}
258	stop_timing();
259	speed = calc_speed();
260	pr_info("eraseblock write speed is %ld KiB/s\n", speed);
261
262	/* Read all eraseblocks, 1 eraseblock at a time */
263	pr_info("testing eraseblock read speed\n");
264	start_timing();
265	for (i = 0; i < ebcnt; ++i) {
266		if (bbt[i])
267			continue;
268		err = read_eraseblock(i);
269		if (err)
270			goto out;
271
272		err = mtdtest_relax();
273		if (err)
274			goto out;
275	}
276	stop_timing();
277	speed = calc_speed();
278	pr_info("eraseblock read speed is %ld KiB/s\n", speed);
279
280	err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
281	if (err)
282		goto out;
283
284	/* Write all eraseblocks, 1 page at a time */
285	pr_info("testing page write speed\n");
286	start_timing();
287	for (i = 0; i < ebcnt; ++i) {
288		if (bbt[i])
289			continue;
290		err = write_eraseblock_by_page(i);
291		if (err)
292			goto out;
293
294		err = mtdtest_relax();
295		if (err)
296			goto out;
297	}
298	stop_timing();
299	speed = calc_speed();
300	pr_info("page write speed is %ld KiB/s\n", speed);
301
302	/* Read all eraseblocks, 1 page at a time */
303	pr_info("testing page read speed\n");
304	start_timing();
305	for (i = 0; i < ebcnt; ++i) {
306		if (bbt[i])
307			continue;
308		err = read_eraseblock_by_page(i);
309		if (err)
310			goto out;
311
312		err = mtdtest_relax();
313		if (err)
314			goto out;
315	}
316	stop_timing();
317	speed = calc_speed();
318	pr_info("page read speed is %ld KiB/s\n", speed);
319
320	err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
321	if (err)
322		goto out;
323
324	/* Write all eraseblocks, 2 pages at a time */
325	pr_info("testing 2 page write speed\n");
326	start_timing();
327	for (i = 0; i < ebcnt; ++i) {
328		if (bbt[i])
329			continue;
330		err = write_eraseblock_by_2pages(i);
331		if (err)
332			goto out;
333
334		err = mtdtest_relax();
335		if (err)
336			goto out;
337	}
338	stop_timing();
339	speed = calc_speed();
340	pr_info("2 page write speed is %ld KiB/s\n", speed);
341
342	/* Read all eraseblocks, 2 pages at a time */
343	pr_info("testing 2 page read speed\n");
344	start_timing();
345	for (i = 0; i < ebcnt; ++i) {
346		if (bbt[i])
347			continue;
348		err = read_eraseblock_by_2pages(i);
349		if (err)
350			goto out;
351
352		err = mtdtest_relax();
353		if (err)
354			goto out;
355	}
356	stop_timing();
357	speed = calc_speed();
358	pr_info("2 page read speed is %ld KiB/s\n", speed);
359
360	/* Erase all eraseblocks */
361	pr_info("Testing erase speed\n");
362	start_timing();
363	err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
364	if (err)
365		goto out;
366	stop_timing();
367	speed = calc_speed();
368	pr_info("erase speed is %ld KiB/s\n", speed);
369
370	/* Multi-block erase all eraseblocks */
371	for (k = 1; k < 7; k++) {
372		blocks = 1 << k;
373		pr_info("Testing %dx multi-block erase speed\n",
374		       blocks);
375		start_timing();
376		for (i = 0; i < ebcnt; ) {
377			for (j = 0; j < blocks && (i + j) < ebcnt; j++)
378				if (bbt[i + j])
379					break;
380			if (j < 1) {
381				i++;
382				continue;
383			}
384			err = multiblock_erase(i, j);
385			if (err)
386				goto out;
387
388			err = mtdtest_relax();
389			if (err)
390				goto out;
391
392			i += j;
393		}
394		stop_timing();
395		speed = calc_speed();
396		pr_info("%dx multi-block erase speed is %ld KiB/s\n",
397		       blocks, speed);
398	}
399	pr_info("finished\n");
400out:
401	kfree(iobuf);
402	kfree(bbt);
403	put_mtd_device(mtd);
404	if (err)
405		pr_info("error %d occurred\n", err);
406	printk(KERN_INFO "=================================================\n");
407	return err;
408}
409module_init(mtd_speedtest_init);
410
411static void __exit mtd_speedtest_exit(void)
412{
413	return;
414}
415module_exit(mtd_speedtest_exit);
416
417MODULE_DESCRIPTION("Speed test module");
418MODULE_AUTHOR("Adrian Hunter");
419MODULE_LICENSE("GPL");
420