mtd: denali.c: fixed all open brace { check-patch errors
[linux-2.6.git] / drivers / mtd / mtdpart.c
1 /*
2  * Simple MTD partitioning layer
3  *
4  * (C) 2000 Nicolas Pitre <nico@fluxnic.net>
5  *
6  * This code is GPL
7  *
8  *      02-21-2002      Thomas Gleixner <gleixner@autronix.de>
9  *                      added support for read_oob, write_oob
10  */
11
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>
21
22 /* Our partition linked list */
23 static LIST_HEAD(mtd_partitions);
24
25 /* Our partition node structure */
26 struct mtd_part {
27         struct mtd_info mtd;
28         struct mtd_info *master;
29         uint64_t offset;
30         struct list_head list;
31 };
32
33 /*
34  * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
35  * the pointer to that structure with this macro.
36  */
37 #define PART(x)  ((struct mtd_part *)(x))
38
39
40 /*
41  * MTD methods which simply translate the effective address and pass through
42  * to the _real_ device.
43  */
44
45 static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
46                 size_t *retlen, u_char *buf)
47 {
48         struct mtd_part *part = PART(mtd);
49         struct mtd_ecc_stats stats;
50         int res;
51
52         stats = part->master->ecc_stats;
53
54         if (from >= mtd->size)
55                 len = 0;
56         else if (from + len > mtd->size)
57                 len = mtd->size - from;
58         res = part->master->read(part->master, from + part->offset,
59                                    len, retlen, buf);
60         if (unlikely(res)) {
61                 if (res == -EUCLEAN)
62                         mtd->ecc_stats.corrected += part->master->ecc_stats.corrected - stats.corrected;
63                 if (res == -EBADMSG)
64                         mtd->ecc_stats.failed += part->master->ecc_stats.failed - stats.failed;
65         }
66         return res;
67 }
68
69 static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
70                 size_t *retlen, void **virt, resource_size_t *phys)
71 {
72         struct mtd_part *part = PART(mtd);
73         if (from >= mtd->size)
74                 len = 0;
75         else if (from + len > mtd->size)
76                 len = mtd->size - from;
77         return part->master->point (part->master, from + part->offset,
78                                     len, retlen, virt, phys);
79 }
80
81 static void part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
82 {
83         struct mtd_part *part = PART(mtd);
84
85         part->master->unpoint(part->master, from + part->offset, len);
86 }
87
88 static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
89                                             unsigned long len,
90                                             unsigned long offset,
91                                             unsigned long flags)
92 {
93         struct mtd_part *part = PART(mtd);
94
95         offset += part->offset;
96         return part->master->get_unmapped_area(part->master, len, offset,
97                                                flags);
98 }
99
100 static int part_read_oob(struct mtd_info *mtd, loff_t from,
101                 struct mtd_oob_ops *ops)
102 {
103         struct mtd_part *part = PART(mtd);
104         int res;
105
106         if (from >= mtd->size)
107                 return -EINVAL;
108         if (ops->datbuf && from + ops->len > mtd->size)
109                 return -EINVAL;
110         res = part->master->read_oob(part->master, from + part->offset, ops);
111
112         if (unlikely(res)) {
113                 if (res == -EUCLEAN)
114                         mtd->ecc_stats.corrected++;
115                 if (res == -EBADMSG)
116                         mtd->ecc_stats.failed++;
117         }
118         return res;
119 }
120
121 static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
122                 size_t len, size_t *retlen, u_char *buf)
123 {
124         struct mtd_part *part = PART(mtd);
125         return part->master->read_user_prot_reg(part->master, from,
126                                         len, retlen, buf);
127 }
128
129 static int part_get_user_prot_info(struct mtd_info *mtd,
130                 struct otp_info *buf, size_t len)
131 {
132         struct mtd_part *part = PART(mtd);
133         return part->master->get_user_prot_info(part->master, buf, len);
134 }
135
136 static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
137                 size_t len, size_t *retlen, u_char *buf)
138 {
139         struct mtd_part *part = PART(mtd);
140         return part->master->read_fact_prot_reg(part->master, from,
141                                         len, retlen, buf);
142 }
143
144 static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
145                 size_t len)
146 {
147         struct mtd_part *part = PART(mtd);
148         return part->master->get_fact_prot_info(part->master, buf, len);
149 }
150
151 static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
152                 size_t *retlen, const u_char *buf)
153 {
154         struct mtd_part *part = PART(mtd);
155         if (!(mtd->flags & MTD_WRITEABLE))
156                 return -EROFS;
157         if (to >= mtd->size)
158                 len = 0;
159         else if (to + len > mtd->size)
160                 len = mtd->size - to;
161         return part->master->write(part->master, to + part->offset,
162                                     len, retlen, buf);
163 }
164
165 static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
166                 size_t *retlen, const u_char *buf)
167 {
168         struct mtd_part *part = PART(mtd);
169         if (!(mtd->flags & MTD_WRITEABLE))
170                 return -EROFS;
171         if (to >= mtd->size)
172                 len = 0;
173         else if (to + len > mtd->size)
174                 len = mtd->size - to;
175         return part->master->panic_write(part->master, to + part->offset,
176                                     len, retlen, buf);
177 }
178
179 static int part_write_oob(struct mtd_info *mtd, loff_t to,
180                 struct mtd_oob_ops *ops)
181 {
182         struct mtd_part *part = PART(mtd);
183
184         if (!(mtd->flags & MTD_WRITEABLE))
185                 return -EROFS;
186
187         if (to >= mtd->size)
188                 return -EINVAL;
189         if (ops->datbuf && to + ops->len > mtd->size)
190                 return -EINVAL;
191         return part->master->write_oob(part->master, to + part->offset, ops);
192 }
193
194 static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
195                 size_t len, size_t *retlen, u_char *buf)
196 {
197         struct mtd_part *part = PART(mtd);
198         return part->master->write_user_prot_reg(part->master, from,
199                                         len, retlen, buf);
200 }
201
202 static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
203                 size_t len)
204 {
205         struct mtd_part *part = PART(mtd);
206         return part->master->lock_user_prot_reg(part->master, from, len);
207 }
208
209 static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
210                 unsigned long count, loff_t to, size_t *retlen)
211 {
212         struct mtd_part *part = PART(mtd);
213         if (!(mtd->flags & MTD_WRITEABLE))
214                 return -EROFS;
215         return part->master->writev(part->master, vecs, count,
216                                         to + part->offset, retlen);
217 }
218
219 static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
220 {
221         struct mtd_part *part = PART(mtd);
222         int ret;
223         if (!(mtd->flags & MTD_WRITEABLE))
224                 return -EROFS;
225         if (instr->addr >= mtd->size)
226                 return -EINVAL;
227         instr->addr += part->offset;
228         ret = part->master->erase(part->master, instr);
229         if (ret) {
230                 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
231                         instr->fail_addr -= part->offset;
232                 instr->addr -= part->offset;
233         }
234         return ret;
235 }
236
237 void mtd_erase_callback(struct erase_info *instr)
238 {
239         if (instr->mtd->erase == part_erase) {
240                 struct mtd_part *part = PART(instr->mtd);
241
242                 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
243                         instr->fail_addr -= part->offset;
244                 instr->addr -= part->offset;
245         }
246         if (instr->callback)
247                 instr->callback(instr);
248 }
249 EXPORT_SYMBOL_GPL(mtd_erase_callback);
250
251 static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
252 {
253         struct mtd_part *part = PART(mtd);
254         if ((len + ofs) > mtd->size)
255                 return -EINVAL;
256         return part->master->lock(part->master, ofs + part->offset, len);
257 }
258
259 static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
260 {
261         struct mtd_part *part = PART(mtd);
262         if ((len + ofs) > mtd->size)
263                 return -EINVAL;
264         return part->master->unlock(part->master, ofs + part->offset, len);
265 }
266
267 static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
268 {
269         struct mtd_part *part = PART(mtd);
270         if ((len + ofs) > mtd->size)
271                 return -EINVAL;
272         return part->master->is_locked(part->master, ofs + part->offset, len);
273 }
274
275 static void part_sync(struct mtd_info *mtd)
276 {
277         struct mtd_part *part = PART(mtd);
278         part->master->sync(part->master);
279 }
280
281 static int part_suspend(struct mtd_info *mtd)
282 {
283         struct mtd_part *part = PART(mtd);
284         return part->master->suspend(part->master);
285 }
286
287 static void part_resume(struct mtd_info *mtd)
288 {
289         struct mtd_part *part = PART(mtd);
290         part->master->resume(part->master);
291 }
292
293 static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
294 {
295         struct mtd_part *part = PART(mtd);
296         if (ofs >= mtd->size)
297                 return -EINVAL;
298         ofs += part->offset;
299         return part->master->block_isbad(part->master, ofs);
300 }
301
302 static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
303 {
304         struct mtd_part *part = PART(mtd);
305         int res;
306
307         if (!(mtd->flags & MTD_WRITEABLE))
308                 return -EROFS;
309         if (ofs >= mtd->size)
310                 return -EINVAL;
311         ofs += part->offset;
312         res = part->master->block_markbad(part->master, ofs);
313         if (!res)
314                 mtd->ecc_stats.badblocks++;
315         return res;
316 }
317
318 /*
319  * This function unregisters and destroy all slave MTD objects which are
320  * attached to the given master MTD object.
321  */
322
323 int del_mtd_partitions(struct mtd_info *master)
324 {
325         struct mtd_part *slave, *next;
326
327         list_for_each_entry_safe(slave, next, &mtd_partitions, list)
328                 if (slave->master == master) {
329                         list_del(&slave->list);
330                         del_mtd_device(&slave->mtd);
331                         kfree(slave);
332                 }
333
334         return 0;
335 }
336 EXPORT_SYMBOL(del_mtd_partitions);
337
338 static struct mtd_part *add_one_partition(struct mtd_info *master,
339                 const struct mtd_partition *part, int partno,
340                 uint64_t cur_offset)
341 {
342         struct mtd_part *slave;
343
344         /* allocate the partition structure */
345         slave = kzalloc(sizeof(*slave), GFP_KERNEL);
346         if (!slave) {
347                 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
348                         master->name);
349                 del_mtd_partitions(master);
350                 return NULL;
351         }
352         list_add(&slave->list, &mtd_partitions);
353
354         /* set up the MTD object for this partition */
355         slave->mtd.type = master->type;
356         slave->mtd.flags = master->flags & ~part->mask_flags;
357         slave->mtd.size = part->size;
358         slave->mtd.writesize = master->writesize;
359         slave->mtd.oobsize = master->oobsize;
360         slave->mtd.oobavail = master->oobavail;
361         slave->mtd.subpage_sft = master->subpage_sft;
362
363         slave->mtd.name = part->name;
364         slave->mtd.owner = master->owner;
365         slave->mtd.backing_dev_info = master->backing_dev_info;
366
367         /* NOTE:  we don't arrange MTDs as a tree; it'd be error-prone
368          * to have the same data be in two different partitions.
369          */
370         slave->mtd.dev.parent = master->dev.parent;
371
372         slave->mtd.read = part_read;
373         slave->mtd.write = part_write;
374
375         if (master->panic_write)
376                 slave->mtd.panic_write = part_panic_write;
377
378         if (master->point && master->unpoint) {
379                 slave->mtd.point = part_point;
380                 slave->mtd.unpoint = part_unpoint;
381         }
382
383         if (master->get_unmapped_area)
384                 slave->mtd.get_unmapped_area = part_get_unmapped_area;
385         if (master->read_oob)
386                 slave->mtd.read_oob = part_read_oob;
387         if (master->write_oob)
388                 slave->mtd.write_oob = part_write_oob;
389         if (master->read_user_prot_reg)
390                 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
391         if (master->read_fact_prot_reg)
392                 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
393         if (master->write_user_prot_reg)
394                 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
395         if (master->lock_user_prot_reg)
396                 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
397         if (master->get_user_prot_info)
398                 slave->mtd.get_user_prot_info = part_get_user_prot_info;
399         if (master->get_fact_prot_info)
400                 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
401         if (master->sync)
402                 slave->mtd.sync = part_sync;
403         if (!partno && !master->dev.class && master->suspend && master->resume) {
404                         slave->mtd.suspend = part_suspend;
405                         slave->mtd.resume = part_resume;
406         }
407         if (master->writev)
408                 slave->mtd.writev = part_writev;
409         if (master->lock)
410                 slave->mtd.lock = part_lock;
411         if (master->unlock)
412                 slave->mtd.unlock = part_unlock;
413         if (master->is_locked)
414                 slave->mtd.is_locked = part_is_locked;
415         if (master->block_isbad)
416                 slave->mtd.block_isbad = part_block_isbad;
417         if (master->block_markbad)
418                 slave->mtd.block_markbad = part_block_markbad;
419         slave->mtd.erase = part_erase;
420         slave->master = master;
421         slave->offset = part->offset;
422
423         if (slave->offset == MTDPART_OFS_APPEND)
424                 slave->offset = cur_offset;
425         if (slave->offset == MTDPART_OFS_NXTBLK) {
426                 slave->offset = cur_offset;
427                 if (mtd_mod_by_eb(cur_offset, master) != 0) {
428                         /* Round up to next erasesize */
429                         slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
430                         printk(KERN_NOTICE "Moving partition %d: "
431                                "0x%012llx -> 0x%012llx\n", partno,
432                                (unsigned long long)cur_offset, (unsigned long long)slave->offset);
433                 }
434         }
435         if (slave->mtd.size == MTDPART_SIZ_FULL)
436                 slave->mtd.size = master->size - slave->offset;
437
438         printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
439                 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
440
441         /* let's do some sanity checks */
442         if (slave->offset >= master->size) {
443                 /* let's register it anyway to preserve ordering */
444                 slave->offset = 0;
445                 slave->mtd.size = 0;
446                 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
447                         part->name);
448                 goto out_register;
449         }
450         if (slave->offset + slave->mtd.size > master->size) {
451                 slave->mtd.size = master->size - slave->offset;
452                 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
453                         part->name, master->name, (unsigned long long)slave->mtd.size);
454         }
455         if (master->numeraseregions > 1) {
456                 /* Deal with variable erase size stuff */
457                 int i, max = master->numeraseregions;
458                 u64 end = slave->offset + slave->mtd.size;
459                 struct mtd_erase_region_info *regions = master->eraseregions;
460
461                 /* Find the first erase regions which is part of this
462                  * partition. */
463                 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
464                         ;
465                 /* The loop searched for the region _behind_ the first one */
466                 if (i > 0)
467                         i--;
468
469                 /* Pick biggest erasesize */
470                 for (; i < max && regions[i].offset < end; i++) {
471                         if (slave->mtd.erasesize < regions[i].erasesize) {
472                                 slave->mtd.erasesize = regions[i].erasesize;
473                         }
474                 }
475                 BUG_ON(slave->mtd.erasesize == 0);
476         } else {
477                 /* Single erase size */
478                 slave->mtd.erasesize = master->erasesize;
479         }
480
481         if ((slave->mtd.flags & MTD_WRITEABLE) &&
482             mtd_mod_by_eb(slave->offset, &slave->mtd)) {
483                 /* Doesn't start on a boundary of major erase size */
484                 /* FIXME: Let it be writable if it is on a boundary of
485                  * _minor_ erase size though */
486                 slave->mtd.flags &= ~MTD_WRITEABLE;
487                 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
488                         part->name);
489         }
490         if ((slave->mtd.flags & MTD_WRITEABLE) &&
491             mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
492                 slave->mtd.flags &= ~MTD_WRITEABLE;
493                 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
494                         part->name);
495         }
496
497         slave->mtd.ecclayout = master->ecclayout;
498         if (master->block_isbad) {
499                 uint64_t offs = 0;
500
501                 while (offs < slave->mtd.size) {
502                         if (master->block_isbad(master,
503                                                 offs + slave->offset))
504                                 slave->mtd.ecc_stats.badblocks++;
505                         offs += slave->mtd.erasesize;
506                 }
507         }
508
509 out_register:
510         /* register our partition */
511         add_mtd_device(&slave->mtd);
512
513         return slave;
514 }
515
516 /*
517  * This function, given a master MTD object and a partition table, creates
518  * and registers slave MTD objects which are bound to the master according to
519  * the partition definitions.
520  *
521  * We don't register the master, or expect the caller to have done so,
522  * for reasons of data integrity.
523  */
524
525 int add_mtd_partitions(struct mtd_info *master,
526                        const struct mtd_partition *parts,
527                        int nbparts)
528 {
529         struct mtd_part *slave;
530         uint64_t cur_offset = 0;
531         int i;
532
533         printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
534
535         for (i = 0; i < nbparts; i++) {
536                 slave = add_one_partition(master, parts + i, i, cur_offset);
537                 if (!slave)
538                         return -ENOMEM;
539                 cur_offset = slave->offset + slave->mtd.size;
540         }
541
542         return 0;
543 }
544 EXPORT_SYMBOL(add_mtd_partitions);
545
546 static DEFINE_SPINLOCK(part_parser_lock);
547 static LIST_HEAD(part_parsers);
548
549 static struct mtd_part_parser *get_partition_parser(const char *name)
550 {
551         struct mtd_part_parser *p, *ret = NULL;
552
553         spin_lock(&part_parser_lock);
554
555         list_for_each_entry(p, &part_parsers, list)
556                 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
557                         ret = p;
558                         break;
559                 }
560
561         spin_unlock(&part_parser_lock);
562
563         return ret;
564 }
565
566 int register_mtd_parser(struct mtd_part_parser *p)
567 {
568         spin_lock(&part_parser_lock);
569         list_add(&p->list, &part_parsers);
570         spin_unlock(&part_parser_lock);
571
572         return 0;
573 }
574 EXPORT_SYMBOL_GPL(register_mtd_parser);
575
576 int deregister_mtd_parser(struct mtd_part_parser *p)
577 {
578         spin_lock(&part_parser_lock);
579         list_del(&p->list);
580         spin_unlock(&part_parser_lock);
581         return 0;
582 }
583 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
584
585 int parse_mtd_partitions(struct mtd_info *master, const char **types,
586                          struct mtd_partition **pparts, unsigned long origin)
587 {
588         struct mtd_part_parser *parser;
589         int ret = 0;
590
591         for ( ; ret <= 0 && *types; types++) {
592                 parser = get_partition_parser(*types);
593                 if (!parser && !request_module("%s", *types))
594                                 parser = get_partition_parser(*types);
595                 if (!parser) {
596                         printk(KERN_NOTICE "%s partition parsing not available\n",
597                                *types);
598                         continue;
599                 }
600                 ret = (*parser->parse_fn)(master, pparts, origin);
601                 if (ret > 0) {
602                         printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
603                                ret, parser->name, master->name);
604                 }
605                 put_partition_parser(parser);
606         }
607         return ret;
608 }
609 EXPORT_SYMBOL_GPL(parse_mtd_partitions);