blob: 078e0f67377daf77e5ae2fc02ea5402a82ea8ef9 [file] [log] [blame]
Thomas Gleixnerfd534e92019-05-23 11:14:39 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Thomas Gleixner97894cd2005-11-07 11:15:26 +00002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Direct MTD block device access
4 *
David Woodhousea1452a32010-08-08 20:58:20 +01005 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
6 * Copyright © 2000-2003 Nicolas Pitre <nico@fluxnic.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/fs.h>
10#include <linux/init.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010011#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/slab.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010015#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/vmalloc.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/mtd/mtd.h>
19#include <linux/mtd/blktrans.h>
Ingo Molnar48b19262006-03-31 02:29:41 -080020#include <linux/mutex.h>
Ezequiel Garciaf83c3832013-10-13 18:05:23 -030021#include <linux/major.h>
Ingo Molnar48b19262006-03-31 02:29:41 -080022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Ben Hutchingscbfe93e2010-01-29 20:58:37 +000024struct mtdblk_dev {
25 struct mtd_blktrans_dev mbd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 int count;
Ingo Molnar48b19262006-03-31 02:29:41 -080027 struct mutex cache_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 unsigned char *cache_data;
29 unsigned long cache_offset;
30 unsigned int cache_size;
31 enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state;
Ben Hutchingscbfe93e2010-01-29 20:58:37 +000032};
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/*
35 * Cache stuff...
Thomas Gleixner97894cd2005-11-07 11:15:26 +000036 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * Since typical flash erasable sectors are much larger than what Linux's
38 * buffer cache can handle, we must implement read-modify-write on flash
39 * sectors for each block write requests. To avoid over-erasing flash sectors
40 * and to speed things up, we locally cache a whole flash sector while it is
41 * being written to until a different sector is required.
42 */
43
Thomas Gleixner97894cd2005-11-07 11:15:26 +000044static int erase_write (struct mtd_info *mtd, unsigned long pos,
huijin.parkbafae532018-11-28 23:19:51 -050045 unsigned int len, const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 struct erase_info erase;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 size_t retlen;
49 int ret;
50
51 /*
52 * First, let's erase the flash block.
53 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 erase.addr = pos;
55 erase.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Artem Bityutskiy7e1f0dc2011-12-23 15:25:39 +020057 ret = mtd_erase(mtd, &erase);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 printk (KERN_WARNING "mtdblock: erase of region [0x%lx, 0x%x] "
60 "on \"%s\" failed\n",
61 pos, len, mtd->name);
62 return ret;
63 }
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 /*
Matthias Kaehlckedff15502009-07-06 12:02:08 +020066 * Next, write the data to flash.
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 */
68
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +020069 ret = mtd_write(mtd, pos, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 if (ret)
71 return ret;
72 if (retlen != len)
73 return -EIO;
74 return 0;
75}
76
77
78static int write_cached_data (struct mtdblk_dev *mtdblk)
79{
Ben Hutchingscbfe93e2010-01-29 20:58:37 +000080 struct mtd_info *mtd = mtdblk->mbd.mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 int ret;
82
83 if (mtdblk->cache_state != STATE_DIRTY)
84 return 0;
85
Brian Norris289c0522011-07-19 10:06:09 -070086 pr_debug("mtdblock: writing cached data for \"%s\" "
Thomas Gleixner97894cd2005-11-07 11:15:26 +000087 "at 0x%lx, size 0x%x\n", mtd->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 mtdblk->cache_offset, mtdblk->cache_size);
Thomas Gleixner97894cd2005-11-07 11:15:26 +000089
90 ret = erase_write (mtd, mtdblk->cache_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 mtdblk->cache_size, mtdblk->cache_data);
92 if (ret)
93 return ret;
94
95 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -030096 * Here we could arguably set the cache state to STATE_CLEAN.
Thomas Gleixner97894cd2005-11-07 11:15:26 +000097 * However this could lead to inconsistency since we will not
98 * be notified if this content is altered on the flash by other
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * means. Let's declare it empty and leave buffering tasks to
100 * the buffer cache instead.
101 */
102 mtdblk->cache_state = STATE_EMPTY;
103 return 0;
104}
105
106
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000107static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 int len, const char *buf)
109{
Ben Hutchingscbfe93e2010-01-29 20:58:37 +0000110 struct mtd_info *mtd = mtdblk->mbd.mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 unsigned int sect_size = mtdblk->cache_size;
112 size_t retlen;
113 int ret;
114
Brian Norris289c0522011-07-19 10:06:09 -0700115 pr_debug("mtdblock: write on \"%s\" at 0x%lx, size 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 mtd->name, pos, len);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (!sect_size)
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +0200119 return mtd_write(mtd, pos, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 while (len > 0) {
122 unsigned long sect_start = (pos/sect_size)*sect_size;
123 unsigned int offset = pos - sect_start;
124 unsigned int size = sect_size - offset;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000125 if( size > len )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 size = len;
127
128 if (size == sect_size) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000129 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * We are covering a whole sector. Thus there is no
131 * need to bother with the cache while it may still be
132 * useful for other partial writes.
133 */
134 ret = erase_write (mtd, pos, size, buf);
135 if (ret)
136 return ret;
137 } else {
138 /* Partial sector: need to use the cache */
139
140 if (mtdblk->cache_state == STATE_DIRTY &&
141 mtdblk->cache_offset != sect_start) {
142 ret = write_cached_data(mtdblk);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000143 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return ret;
145 }
146
147 if (mtdblk->cache_state == STATE_EMPTY ||
148 mtdblk->cache_offset != sect_start) {
149 /* fill the cache with the current sector */
150 mtdblk->cache_state = STATE_EMPTY;
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200151 ret = mtd_read(mtd, sect_start, sect_size,
152 &retlen, mtdblk->cache_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (ret)
154 return ret;
155 if (retlen != sect_size)
156 return -EIO;
157
158 mtdblk->cache_offset = sect_start;
159 mtdblk->cache_size = sect_size;
160 mtdblk->cache_state = STATE_CLEAN;
161 }
162
163 /* write data to our local cache */
164 memcpy (mtdblk->cache_data + offset, buf, size);
165 mtdblk->cache_state = STATE_DIRTY;
166 }
167
168 buf += size;
169 pos += size;
170 len -= size;
171 }
172
173 return 0;
174}
175
176
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000177static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 int len, char *buf)
179{
Ben Hutchingscbfe93e2010-01-29 20:58:37 +0000180 struct mtd_info *mtd = mtdblk->mbd.mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 unsigned int sect_size = mtdblk->cache_size;
182 size_t retlen;
183 int ret;
184
Brian Norris289c0522011-07-19 10:06:09 -0700185 pr_debug("mtdblock: read on \"%s\" at 0x%lx, size 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 mtd->name, pos, len);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (!sect_size)
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200189 return mtd_read(mtd, pos, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 while (len > 0) {
192 unsigned long sect_start = (pos/sect_size)*sect_size;
193 unsigned int offset = pos - sect_start;
194 unsigned int size = sect_size - offset;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000195 if (size > len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 size = len;
197
198 /*
199 * Check if the requested data is already cached
200 * Read the requested amount of data from our internal cache if it
201 * contains what we want, otherwise we read the data directly
202 * from flash.
203 */
204 if (mtdblk->cache_state != STATE_EMPTY &&
205 mtdblk->cache_offset == sect_start) {
206 memcpy (buf, mtdblk->cache_data + offset, size);
207 } else {
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200208 ret = mtd_read(mtd, pos, size, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (ret)
210 return ret;
211 if (retlen != size)
212 return -EIO;
213 }
214
215 buf += size;
216 pos += size;
217 len -= size;
218 }
219
220 return 0;
221}
222
223static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
224 unsigned long block, char *buf)
225{
Ben Hutchingscbfe93e2010-01-29 20:58:37 +0000226 struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return do_cached_read(mtdblk, block<<9, 512, buf);
228}
229
230static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
231 unsigned long block, char *buf)
232{
Ben Hutchingscbfe93e2010-01-29 20:58:37 +0000233 struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (unlikely(!mtdblk->cache_data && mtdblk->cache_size)) {
Ben Hutchingscbfe93e2010-01-29 20:58:37 +0000235 mtdblk->cache_data = vmalloc(mtdblk->mbd.mtd->erasesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (!mtdblk->cache_data)
237 return -EINTR;
238 /* -EINTR is not really correct, but it is the best match
239 * documented in man 2 write for all cases. We could also
240 * return -EAGAIN sometimes, but why bother?
241 */
242 }
243 return do_cached_write(mtdblk, block<<9, 512, buf);
244}
245
246static int mtdblock_open(struct mtd_blktrans_dev *mbd)
247{
Ben Hutchingscbfe93e2010-01-29 20:58:37 +0000248 struct mtdblk_dev *mtdblk = container_of(mbd, struct mtdblk_dev, mbd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Brian Norris289c0522011-07-19 10:06:09 -0700250 pr_debug("mtdblock_open\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000251
Ben Hutchingscbfe93e2010-01-29 20:58:37 +0000252 if (mtdblk->count) {
253 mtdblk->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return 0;
255 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 /* OK, it's not open. Create cache info for it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 mtdblk->count = 1;
Ingo Molnar48b19262006-03-31 02:29:41 -0800259 mutex_init(&mtdblk->cache_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 mtdblk->cache_state = STATE_EMPTY;
Ben Hutchingscbfe93e2010-01-29 20:58:37 +0000261 if (!(mbd->mtd->flags & MTD_NO_ERASE) && mbd->mtd->erasesize) {
262 mtdblk->cache_size = mbd->mtd->erasesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 mtdblk->cache_data = NULL;
264 }
265
Brian Norris289c0522011-07-19 10:06:09 -0700266 pr_debug("ok\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 return 0;
269}
270
Al Viroa8ca8892013-05-05 21:31:22 -0400271static void mtdblock_release(struct mtd_blktrans_dev *mbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Ben Hutchingscbfe93e2010-01-29 20:58:37 +0000273 struct mtdblk_dev *mtdblk = container_of(mbd, struct mtdblk_dev, mbd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Brian Norris289c0522011-07-19 10:06:09 -0700275 pr_debug("mtdblock_release\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Ingo Molnar48b19262006-03-31 02:29:41 -0800277 mutex_lock(&mtdblk->cache_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 write_cached_data(mtdblk);
Ingo Molnar48b19262006-03-31 02:29:41 -0800279 mutex_unlock(&mtdblk->cache_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 if (!--mtdblk->count) {
Alexander Stein70d50982012-01-10 13:26:58 +0100282 /*
283 * It was the last usage. Free the cache, but only sync if
284 * opened for writing.
285 */
286 if (mbd->file_mode & FMODE_WRITE)
287 mtd_sync(mbd->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 vfree(mtdblk->cache_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
Matthias Kaehlcked676c112009-07-14 22:04:29 +0200290
Brian Norris289c0522011-07-19 10:06:09 -0700291 pr_debug("ok\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000292}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294static int mtdblock_flush(struct mtd_blktrans_dev *dev)
295{
Ben Hutchingscbfe93e2010-01-29 20:58:37 +0000296 struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
Xiaoming Ni4e4a9a82020-03-20 11:15:11 +0800297 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Ingo Molnar48b19262006-03-31 02:29:41 -0800299 mutex_lock(&mtdblk->cache_mutex);
Xiaoming Ni4e4a9a82020-03-20 11:15:11 +0800300 ret = write_cached_data(mtdblk);
Ingo Molnar48b19262006-03-31 02:29:41 -0800301 mutex_unlock(&mtdblk->cache_mutex);
Artem Bityutskiy327cf292011-12-30 16:35:35 +0200302 mtd_sync(dev->mtd);
Xiaoming Ni4e4a9a82020-03-20 11:15:11 +0800303 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
307{
Ben Hutchingscbfe93e2010-01-29 20:58:37 +0000308 struct mtdblk_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 if (!dev)
311 return;
312
Ben Hutchingscbfe93e2010-01-29 20:58:37 +0000313 dev->mbd.mtd = mtd;
314 dev->mbd.devnum = mtd->index;
Richard Purdie19187672006-10-27 09:09:33 +0100315
Ben Hutchingscbfe93e2010-01-29 20:58:37 +0000316 dev->mbd.size = mtd->size >> 9;
317 dev->mbd.tr = tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 if (!(mtd->flags & MTD_WRITEABLE))
Ben Hutchingscbfe93e2010-01-29 20:58:37 +0000320 dev->mbd.readonly = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Maxim Levitsky298304f2010-02-22 20:39:31 +0200322 if (add_mtd_blktrans_dev(&dev->mbd))
323 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
327{
328 del_mtd_blktrans_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331static struct mtd_blktrans_ops mtdblock_tr = {
332 .name = "mtdblock",
Ezequiel Garcia2aabeb22013-10-08 20:59:08 -0300333 .major = MTD_BLOCK_MAJOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 .part_bits = 0,
Richard Purdie19187672006-10-27 09:09:33 +0100335 .blksize = 512,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 .open = mtdblock_open,
337 .flush = mtdblock_flush,
338 .release = mtdblock_release,
339 .readsect = mtdblock_readsect,
340 .writesect = mtdblock_writesect,
341 .add_mtd = mtdblock_add_mtd,
342 .remove_dev = mtdblock_remove_dev,
343 .owner = THIS_MODULE,
344};
345
346static int __init init_mtdblock(void)
347{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return register_mtd_blktrans(&mtdblock_tr);
349}
350
351static void __exit cleanup_mtdblock(void)
352{
353 deregister_mtd_blktrans(&mtdblock_tr);
354}
355
356module_init(init_mtdblock);
357module_exit(cleanup_mtdblock);
358
359
360MODULE_LICENSE("GPL");
Nicolas Pitre2f82af02009-09-14 03:25:28 -0400361MODULE_AUTHOR("Nicolas Pitre <nico@fluxnic.net> et al.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362MODULE_DESCRIPTION("Caching read/erase/writeback block device emulation access to MTD devices");