On Tue, Nov 06, 2007 at 02:33:53AM -0800, akpm@linux-foundation.org wrote:
[mmotm.git] / drivers / mtd / mtdpart.c
blob9f4d5f80495058d58e1ba7b723750559676a48a4
1 /*
2 * Simple MTD partitioning layer
4 * (C) 2000 Nicolas Pitre <nico@fluxnic.net>
6 * This code is GPL
8 * 02-21-2002 Thomas Gleixner <gleixner@autronix.de>
9 * added support for read_oob, write_oob
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/list.h>
17 #include <linux/kmod.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/partitions.h>
20 #include <linux/mtd/compatmac.h>
22 /* Our partition linked list */
23 static LIST_HEAD(mtd_partitions);
25 /* Our partition node structure */
26 struct mtd_part {
27 struct mtd_info mtd;
28 struct mtd_info *master;
29 struct memory_accessor macc;
30 uint64_t offset;
31 struct list_head list;
35 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
36 * the pointer to that structure with this macro.
38 #define PART(x) ((struct mtd_part *)(x))
42 * MTD methods which simply translate the effective address and pass through
43 * to the _real_ device.
46 static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
47 size_t *retlen, u_char *buf)
49 struct mtd_part *part = PART(mtd);
50 struct mtd_ecc_stats stats;
51 int res;
53 stats = part->master->ecc_stats;
55 if (from >= mtd->size)
56 len = 0;
57 else if (from + len > mtd->size)
58 len = mtd->size - from;
59 res = part->master->read(part->master, from + part->offset,
60 len, retlen, buf);
61 if (unlikely(res)) {
62 if (res == -EUCLEAN)
63 mtd->ecc_stats.corrected += part->master->ecc_stats.corrected - stats.corrected;
64 if (res == -EBADMSG)
65 mtd->ecc_stats.failed += part->master->ecc_stats.failed - stats.failed;
67 return res;
70 static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
71 size_t *retlen, void **virt, resource_size_t *phys)
73 struct mtd_part *part = PART(mtd);
74 if (from >= mtd->size)
75 len = 0;
76 else if (from + len > mtd->size)
77 len = mtd->size - from;
78 return part->master->point (part->master, from + part->offset,
79 len, retlen, virt, phys);
82 static void part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
84 struct mtd_part *part = PART(mtd);
86 part->master->unpoint(part->master, from + part->offset, len);
89 static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
90 unsigned long len,
91 unsigned long offset,
92 unsigned long flags)
94 struct mtd_part *part = PART(mtd);
96 offset += part->offset;
97 return part->master->get_unmapped_area(part->master, len, offset,
98 flags);
101 static int part_read_oob(struct mtd_info *mtd, loff_t from,
102 struct mtd_oob_ops *ops)
104 struct mtd_part *part = PART(mtd);
105 int res;
107 if (from >= mtd->size)
108 return -EINVAL;
109 if (ops->datbuf && from + ops->len > mtd->size)
110 return -EINVAL;
111 res = part->master->read_oob(part->master, from + part->offset, ops);
113 if (unlikely(res)) {
114 if (res == -EUCLEAN)
115 mtd->ecc_stats.corrected++;
116 if (res == -EBADMSG)
117 mtd->ecc_stats.failed++;
119 return res;
122 static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
123 size_t len, size_t *retlen, u_char *buf)
125 struct mtd_part *part = PART(mtd);
126 return part->master->read_user_prot_reg(part->master, from,
127 len, retlen, buf);
130 static int part_get_user_prot_info(struct mtd_info *mtd,
131 struct otp_info *buf, size_t len)
133 struct mtd_part *part = PART(mtd);
134 return part->master->get_user_prot_info(part->master, buf, len);
137 static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
138 size_t len, size_t *retlen, u_char *buf)
140 struct mtd_part *part = PART(mtd);
141 return part->master->read_fact_prot_reg(part->master, from,
142 len, retlen, buf);
145 static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
146 size_t len)
148 struct mtd_part *part = PART(mtd);
149 return part->master->get_fact_prot_info(part->master, buf, len);
152 static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
153 size_t *retlen, const u_char *buf)
155 struct mtd_part *part = PART(mtd);
156 if (!(mtd->flags & MTD_WRITEABLE))
157 return -EROFS;
158 if (to >= mtd->size)
159 len = 0;
160 else if (to + len > mtd->size)
161 len = mtd->size - to;
162 return part->master->write(part->master, to + part->offset,
163 len, retlen, buf);
166 static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
167 size_t *retlen, const u_char *buf)
169 struct mtd_part *part = PART(mtd);
170 if (!(mtd->flags & MTD_WRITEABLE))
171 return -EROFS;
172 if (to >= mtd->size)
173 len = 0;
174 else if (to + len > mtd->size)
175 len = mtd->size - to;
176 return part->master->panic_write(part->master, to + part->offset,
177 len, retlen, buf);
180 static int part_write_oob(struct mtd_info *mtd, loff_t to,
181 struct mtd_oob_ops *ops)
183 struct mtd_part *part = PART(mtd);
185 if (!(mtd->flags & MTD_WRITEABLE))
186 return -EROFS;
188 if (to >= mtd->size)
189 return -EINVAL;
190 if (ops->datbuf && to + ops->len > mtd->size)
191 return -EINVAL;
192 return part->master->write_oob(part->master, to + part->offset, ops);
195 static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
196 size_t len, size_t *retlen, u_char *buf)
198 struct mtd_part *part = PART(mtd);
199 return part->master->write_user_prot_reg(part->master, from,
200 len, retlen, buf);
203 static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
204 size_t len)
206 struct mtd_part *part = PART(mtd);
207 return part->master->lock_user_prot_reg(part->master, from, len);
210 static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
211 unsigned long count, loff_t to, size_t *retlen)
213 struct mtd_part *part = PART(mtd);
214 if (!(mtd->flags & MTD_WRITEABLE))
215 return -EROFS;
216 return part->master->writev(part->master, vecs, count,
217 to + part->offset, retlen);
220 static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
222 struct mtd_part *part = PART(mtd);
223 int ret;
224 if (!(mtd->flags & MTD_WRITEABLE))
225 return -EROFS;
226 if (instr->addr >= mtd->size)
227 return -EINVAL;
228 instr->addr += part->offset;
229 ret = part->master->erase(part->master, instr);
230 if (ret) {
231 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
232 instr->fail_addr -= part->offset;
233 instr->addr -= part->offset;
235 return ret;
238 void mtd_erase_callback(struct erase_info *instr)
240 if (instr->mtd->erase == part_erase) {
241 struct mtd_part *part = PART(instr->mtd);
243 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
244 instr->fail_addr -= part->offset;
245 instr->addr -= part->offset;
247 if (instr->callback)
248 instr->callback(instr);
250 EXPORT_SYMBOL_GPL(mtd_erase_callback);
252 static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
254 struct mtd_part *part = PART(mtd);
255 if ((len + ofs) > mtd->size)
256 return -EINVAL;
257 return part->master->lock(part->master, ofs + part->offset, len);
260 static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
262 struct mtd_part *part = PART(mtd);
263 if ((len + ofs) > mtd->size)
264 return -EINVAL;
265 return part->master->unlock(part->master, ofs + part->offset, len);
268 static void part_sync(struct mtd_info *mtd)
270 struct mtd_part *part = PART(mtd);
271 part->master->sync(part->master);
274 static int part_suspend(struct mtd_info *mtd)
276 struct mtd_part *part = PART(mtd);
277 return part->master->suspend(part->master);
280 static void part_resume(struct mtd_info *mtd)
282 struct mtd_part *part = PART(mtd);
283 part->master->resume(part->master);
286 static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
288 struct mtd_part *part = PART(mtd);
289 if (ofs >= mtd->size)
290 return -EINVAL;
291 ofs += part->offset;
292 return part->master->block_isbad(part->master, ofs);
295 static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
297 struct mtd_part *part = PART(mtd);
298 int res;
300 if (!(mtd->flags & MTD_WRITEABLE))
301 return -EROFS;
302 if (ofs >= mtd->size)
303 return -EINVAL;
304 ofs += part->offset;
305 res = part->master->block_markbad(part->master, ofs);
306 if (!res)
307 mtd->ecc_stats.badblocks++;
308 return res;
312 * This function unregisters and destroy all slave MTD objects which are
313 * attached to the given master MTD object.
316 int del_mtd_partitions(struct mtd_info *master)
318 struct mtd_part *slave, *next;
320 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
321 if (slave->master == master) {
322 list_del(&slave->list);
323 del_mtd_device(&slave->mtd);
324 kfree(slave);
327 return 0;
329 EXPORT_SYMBOL(del_mtd_partitions);
332 * This lets other kernel code access the flash data. For example, it
333 * might hold a board's Ethernet address, or board-specific calibration
334 * data generated on the manufacturing floor.
336 static ssize_t mtd_macc_read(struct memory_accessor *macc, char *buf,
337 off_t offset, size_t count)
339 struct mtd_part *part = container_of(macc, struct mtd_part, macc);
340 ssize_t ret = -EIO;
341 size_t retlen;
343 if (part_read((struct mtd_info *)part, offset, count,
344 &retlen, buf) == 0)
345 ret = retlen;
347 return ret;
350 static ssize_t mtd_macc_write(struct memory_accessor *macc, const char *buf,
351 off_t offset, size_t count)
353 struct mtd_part *part = container_of(macc, struct mtd_part, macc);
354 ssize_t ret = -EIO;
355 size_t retlen;
357 if (part_write((struct mtd_info *)part, offset, count,
358 &retlen, buf) == 0)
359 ret = retlen;
361 return ret;
364 static struct mtd_part *add_one_partition(struct mtd_info *master,
365 const struct mtd_partition *part, int partno,
366 uint64_t cur_offset)
368 struct mtd_part *slave;
370 /* allocate the partition structure */
371 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
372 if (!slave) {
373 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
374 master->name);
375 del_mtd_partitions(master);
376 return NULL;
378 list_add(&slave->list, &mtd_partitions);
380 /* set up the MTD object for this partition */
381 slave->mtd.type = master->type;
382 slave->mtd.flags = master->flags & ~part->mask_flags;
383 slave->mtd.size = part->size;
384 slave->mtd.writesize = master->writesize;
385 slave->mtd.oobsize = master->oobsize;
386 slave->mtd.oobavail = master->oobavail;
387 slave->mtd.subpage_sft = master->subpage_sft;
389 slave->mtd.name = part->name;
390 slave->mtd.owner = master->owner;
391 slave->mtd.backing_dev_info = master->backing_dev_info;
393 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
394 * to have the same data be in two different partitions.
396 slave->mtd.dev.parent = master->dev.parent;
398 slave->mtd.read = part_read;
399 slave->mtd.write = part_write;
401 slave->macc.read = mtd_macc_read;
402 slave->macc.write = mtd_macc_write;
404 if (master->panic_write)
405 slave->mtd.panic_write = part_panic_write;
407 if (master->point && master->unpoint) {
408 slave->mtd.point = part_point;
409 slave->mtd.unpoint = part_unpoint;
412 if (master->get_unmapped_area)
413 slave->mtd.get_unmapped_area = part_get_unmapped_area;
414 if (master->read_oob)
415 slave->mtd.read_oob = part_read_oob;
416 if (master->write_oob)
417 slave->mtd.write_oob = part_write_oob;
418 if (master->read_user_prot_reg)
419 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
420 if (master->read_fact_prot_reg)
421 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
422 if (master->write_user_prot_reg)
423 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
424 if (master->lock_user_prot_reg)
425 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
426 if (master->get_user_prot_info)
427 slave->mtd.get_user_prot_info = part_get_user_prot_info;
428 if (master->get_fact_prot_info)
429 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
430 if (master->sync)
431 slave->mtd.sync = part_sync;
432 if (!partno && !master->dev.class && master->suspend && master->resume) {
433 slave->mtd.suspend = part_suspend;
434 slave->mtd.resume = part_resume;
436 if (master->writev)
437 slave->mtd.writev = part_writev;
438 if (master->lock)
439 slave->mtd.lock = part_lock;
440 if (master->unlock)
441 slave->mtd.unlock = part_unlock;
442 if (master->block_isbad)
443 slave->mtd.block_isbad = part_block_isbad;
444 if (master->block_markbad)
445 slave->mtd.block_markbad = part_block_markbad;
446 slave->mtd.erase = part_erase;
447 slave->master = master;
448 slave->offset = part->offset;
450 if (slave->offset == MTDPART_OFS_APPEND)
451 slave->offset = cur_offset;
452 if (slave->offset == MTDPART_OFS_NXTBLK) {
453 slave->offset = cur_offset;
454 if (mtd_mod_by_eb(cur_offset, master) != 0) {
455 /* Round up to next erasesize */
456 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
457 printk(KERN_NOTICE "Moving partition %d: "
458 "0x%012llx -> 0x%012llx\n", partno,
459 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
462 if (slave->mtd.size == MTDPART_SIZ_FULL)
463 slave->mtd.size = master->size - slave->offset;
465 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
466 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
468 if (part->setup)
469 part->setup(&slave->macc, (void *)part->context);
471 /* let's do some sanity checks */
472 if (slave->offset >= master->size) {
473 /* let's register it anyway to preserve ordering */
474 slave->offset = 0;
475 slave->mtd.size = 0;
476 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
477 part->name);
478 goto out_register;
480 if (slave->offset + slave->mtd.size > master->size) {
481 slave->mtd.size = master->size - slave->offset;
482 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
483 part->name, master->name, (unsigned long long)slave->mtd.size);
485 if (master->numeraseregions > 1) {
486 /* Deal with variable erase size stuff */
487 int i, max = master->numeraseregions;
488 u64 end = slave->offset + slave->mtd.size;
489 struct mtd_erase_region_info *regions = master->eraseregions;
491 /* Find the first erase regions which is part of this
492 * partition. */
493 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
495 /* The loop searched for the region _behind_ the first one */
496 if (i > 0)
497 i--;
499 /* Pick biggest erasesize */
500 for (; i < max && regions[i].offset < end; i++) {
501 if (slave->mtd.erasesize < regions[i].erasesize) {
502 slave->mtd.erasesize = regions[i].erasesize;
505 BUG_ON(slave->mtd.erasesize == 0);
506 } else {
507 /* Single erase size */
508 slave->mtd.erasesize = master->erasesize;
511 if ((slave->mtd.flags & MTD_WRITEABLE) &&
512 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
513 /* Doesn't start on a boundary of major erase size */
514 /* FIXME: Let it be writable if it is on a boundary of
515 * _minor_ erase size though */
516 slave->mtd.flags &= ~MTD_WRITEABLE;
517 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
518 part->name);
520 if ((slave->mtd.flags & MTD_WRITEABLE) &&
521 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
522 slave->mtd.flags &= ~MTD_WRITEABLE;
523 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
524 part->name);
527 slave->mtd.ecclayout = master->ecclayout;
528 if (master->block_isbad) {
529 uint64_t offs = 0;
531 while (offs < slave->mtd.size) {
532 if (master->block_isbad(master,
533 offs + slave->offset))
534 slave->mtd.ecc_stats.badblocks++;
535 offs += slave->mtd.erasesize;
539 out_register:
540 /* register our partition */
541 add_mtd_device(&slave->mtd);
543 return slave;
547 * This function, given a master MTD object and a partition table, creates
548 * and registers slave MTD objects which are bound to the master according to
549 * the partition definitions.
551 * We don't register the master, or expect the caller to have done so,
552 * for reasons of data integrity.
555 int add_mtd_partitions(struct mtd_info *master,
556 const struct mtd_partition *parts,
557 int nbparts)
559 struct mtd_part *slave;
560 uint64_t cur_offset = 0;
561 int i;
563 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
565 for (i = 0; i < nbparts; i++) {
566 slave = add_one_partition(master, parts + i, i, cur_offset);
567 if (!slave)
568 return -ENOMEM;
569 cur_offset = slave->offset + slave->mtd.size;
572 return 0;
574 EXPORT_SYMBOL(add_mtd_partitions);
576 static DEFINE_SPINLOCK(part_parser_lock);
577 static LIST_HEAD(part_parsers);
579 static struct mtd_part_parser *get_partition_parser(const char *name)
581 struct mtd_part_parser *p, *ret = NULL;
583 spin_lock(&part_parser_lock);
585 list_for_each_entry(p, &part_parsers, list)
586 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
587 ret = p;
588 break;
591 spin_unlock(&part_parser_lock);
593 return ret;
596 int register_mtd_parser(struct mtd_part_parser *p)
598 spin_lock(&part_parser_lock);
599 list_add(&p->list, &part_parsers);
600 spin_unlock(&part_parser_lock);
602 return 0;
604 EXPORT_SYMBOL_GPL(register_mtd_parser);
606 int deregister_mtd_parser(struct mtd_part_parser *p)
608 spin_lock(&part_parser_lock);
609 list_del(&p->list);
610 spin_unlock(&part_parser_lock);
611 return 0;
613 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
615 int parse_mtd_partitions(struct mtd_info *master, const char **types,
616 struct mtd_partition **pparts, unsigned long origin)
618 struct mtd_part_parser *parser;
619 int ret = 0;
621 for ( ; ret <= 0 && *types; types++) {
622 parser = get_partition_parser(*types);
623 if (!parser && !request_module("%s", *types))
624 parser = get_partition_parser(*types);
625 if (!parser) {
626 printk(KERN_NOTICE "%s partition parsing not available\n",
627 *types);
628 continue;
630 ret = (*parser->parse_fn)(master, pparts, origin);
631 if (ret > 0) {
632 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
633 ret, parser->name, master->name);
635 put_partition_parser(parser);
637 return ret;
639 EXPORT_SYMBOL_GPL(parse_mtd_partitions);