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