Merge git://git.infradead.org/mtd-2.6
[pandora-kernel.git] / drivers / mtd / sm_ftl.c
1 /*
2  * Copyright © 2009 - Maxim Levitsky
3  * SmartMedia/xD translation layer
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/random.h>
13 #include <linux/hdreg.h>
14 #include <linux/kthread.h>
15 #include <linux/freezer.h>
16 #include <linux/sysfs.h>
17 #include <linux/bitops.h>
18 #include <linux/slab.h>
19 #include <linux/mtd/nand_ecc.h>
20 #include "nand/sm_common.h"
21 #include "sm_ftl.h"
22
23
24
25 struct workqueue_struct *cache_flush_workqueue;
26
27 static int cache_timeout = 1000;
28 module_param(cache_timeout, bool, S_IRUGO);
29 MODULE_PARM_DESC(cache_timeout,
30         "Timeout (in ms) for cache flush (1000 ms default");
31
32 static int debug;
33 module_param(debug, int, S_IRUGO | S_IWUSR);
34 MODULE_PARM_DESC(debug, "Debug level (0-2)");
35
36
37 /* ------------------- sysfs attributtes ---------------------------------- */
38 struct sm_sysfs_attribute {
39         struct device_attribute dev_attr;
40         char *data;
41         int len;
42 };
43
44 ssize_t sm_attr_show(struct device *dev, struct device_attribute *attr,
45                      char *buf)
46 {
47         struct sm_sysfs_attribute *sm_attr =
48                 container_of(attr, struct sm_sysfs_attribute, dev_attr);
49
50         strncpy(buf, sm_attr->data, sm_attr->len);
51         return sm_attr->len;
52 }
53
54
55 #define NUM_ATTRIBUTES 1
56 #define SM_CIS_VENDOR_OFFSET 0x59
57 struct attribute_group *sm_create_sysfs_attributes(struct sm_ftl *ftl)
58 {
59         struct attribute_group *attr_group;
60         struct attribute **attributes;
61         struct sm_sysfs_attribute *vendor_attribute;
62
63         int vendor_len = strnlen(ftl->cis_buffer + SM_CIS_VENDOR_OFFSET,
64                                         SM_SMALL_PAGE - SM_CIS_VENDOR_OFFSET);
65
66         char *vendor = kmalloc(vendor_len, GFP_KERNEL);
67         memcpy(vendor, ftl->cis_buffer + SM_CIS_VENDOR_OFFSET, vendor_len);
68         vendor[vendor_len] = 0;
69
70         /* Initialize sysfs attributes */
71         vendor_attribute =
72                 kzalloc(sizeof(struct sm_sysfs_attribute), GFP_KERNEL);
73
74         sysfs_attr_init(&vendor_attribute->dev_attr.attr);
75
76         vendor_attribute->data = vendor;
77         vendor_attribute->len = vendor_len;
78         vendor_attribute->dev_attr.attr.name = "vendor";
79         vendor_attribute->dev_attr.attr.mode = S_IRUGO;
80         vendor_attribute->dev_attr.show = sm_attr_show;
81
82
83         /* Create array of pointers to the attributes */
84         attributes = kzalloc(sizeof(struct attribute *) * (NUM_ATTRIBUTES + 1),
85                                                                 GFP_KERNEL);
86         attributes[0] = &vendor_attribute->dev_attr.attr;
87
88         /* Finally create the attribute group */
89         attr_group = kzalloc(sizeof(struct attribute_group), GFP_KERNEL);
90         attr_group->attrs = attributes;
91         return attr_group;
92 }
93
94 void sm_delete_sysfs_attributes(struct sm_ftl *ftl)
95 {
96         struct attribute **attributes = ftl->disk_attributes->attrs;
97         int i;
98
99         for (i = 0; attributes[i] ; i++) {
100
101                 struct device_attribute *dev_attr = container_of(attributes[i],
102                         struct device_attribute, attr);
103
104                 struct sm_sysfs_attribute *sm_attr =
105                         container_of(dev_attr,
106                                 struct sm_sysfs_attribute, dev_attr);
107
108                 kfree(sm_attr->data);
109                 kfree(sm_attr);
110         }
111
112         kfree(ftl->disk_attributes->attrs);
113         kfree(ftl->disk_attributes);
114 }
115
116
117 /* ----------------------- oob helpers -------------------------------------- */
118
119 static int sm_get_lba(uint8_t *lba)
120 {
121         /* check fixed bits */
122         if ((lba[0] & 0xF8) != 0x10)
123                 return -2;
124
125         /* check parity - endianess doesn't matter */
126         if (hweight16(*(uint16_t *)lba) & 1)
127                 return -2;
128
129         return (lba[1] >> 1) | ((lba[0] & 0x07) << 7);
130 }
131
132
133 /*
134  * Read LBA asscociated with block
135  * returns -1, if block is erased
136  * returns -2 if error happens
137  */
138 static int sm_read_lba(struct sm_oob *oob)
139 {
140         static const uint32_t erased_pattern[4] = {
141                 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
142
143         uint16_t lba_test;
144         int lba;
145
146         /* First test for erased block */
147         if (!memcmp(oob, erased_pattern, SM_OOB_SIZE))
148                 return -1;
149
150         /* Now check is both copies of the LBA differ too much */
151         lba_test = *(uint16_t *)oob->lba_copy1 ^ *(uint16_t*)oob->lba_copy2;
152         if (lba_test && !is_power_of_2(lba_test))
153                 return -2;
154
155         /* And read it */
156         lba = sm_get_lba(oob->lba_copy1);
157
158         if (lba == -2)
159                 lba = sm_get_lba(oob->lba_copy2);
160
161         return lba;
162 }
163
164 static void sm_write_lba(struct sm_oob *oob, uint16_t lba)
165 {
166         uint8_t tmp[2];
167
168         WARN_ON(lba >= 1000);
169
170         tmp[0] = 0x10 | ((lba >> 7) & 0x07);
171         tmp[1] = (lba << 1) & 0xFF;
172
173         if (hweight16(*(uint16_t *)tmp) & 0x01)
174                 tmp[1] |= 1;
175
176         oob->lba_copy1[0] = oob->lba_copy2[0] = tmp[0];
177         oob->lba_copy1[1] = oob->lba_copy2[1] = tmp[1];
178 }
179
180
181 /* Make offset from parts */
182 static loff_t sm_mkoffset(struct sm_ftl *ftl, int zone, int block, int boffset)
183 {
184         WARN_ON(boffset & (SM_SECTOR_SIZE - 1));
185         WARN_ON(zone < 0 || zone >= ftl->zone_count);
186         WARN_ON(block >= ftl->zone_size);
187         WARN_ON(boffset >= ftl->block_size);
188
189         if (block == -1)
190                 return -1;
191
192         return (zone * SM_MAX_ZONE_SIZE + block) * ftl->block_size + boffset;
193 }
194
195 /* Breaks offset into parts */
196 static void sm_break_offset(struct sm_ftl *ftl, loff_t offset,
197                             int *zone, int *block, int *boffset)
198 {
199         *boffset = do_div(offset, ftl->block_size);
200         *block = do_div(offset, ftl->max_lba);
201         *zone = offset >= ftl->zone_count ? -1 : offset;
202 }
203
204 /* ---------------------- low level IO ------------------------------------- */
205
206 static int sm_correct_sector(uint8_t *buffer, struct sm_oob *oob)
207 {
208         uint8_t ecc[3];
209
210         __nand_calculate_ecc(buffer, SM_SMALL_PAGE, ecc);
211         if (__nand_correct_data(buffer, ecc, oob->ecc1, SM_SMALL_PAGE) < 0)
212                 return -EIO;
213
214         buffer += SM_SMALL_PAGE;
215
216         __nand_calculate_ecc(buffer, SM_SMALL_PAGE, ecc);
217         if (__nand_correct_data(buffer, ecc, oob->ecc2, SM_SMALL_PAGE) < 0)
218                 return -EIO;
219         return 0;
220 }
221
222 /* Reads a sector + oob*/
223 static int sm_read_sector(struct sm_ftl *ftl,
224                           int zone, int block, int boffset,
225                           uint8_t *buffer, struct sm_oob *oob)
226 {
227         struct mtd_info *mtd = ftl->trans->mtd;
228         struct mtd_oob_ops ops;
229         struct sm_oob tmp_oob;
230         int ret = -EIO;
231         int try = 0;
232
233         /* FTL can contain -1 entries that are by default filled with bits */
234         if (block == -1) {
235                 memset(buffer, 0xFF, SM_SECTOR_SIZE);
236                 return 0;
237         }
238
239         /* User might not need the oob, but we do for data vertification */
240         if (!oob)
241                 oob = &tmp_oob;
242
243         ops.mode = ftl->smallpagenand ? MTD_OOB_RAW : MTD_OOB_PLACE;
244         ops.ooboffs = 0;
245         ops.ooblen = SM_OOB_SIZE;
246         ops.oobbuf = (void *)oob;
247         ops.len = SM_SECTOR_SIZE;
248         ops.datbuf = buffer;
249
250 again:
251         if (try++) {
252                 /* Avoid infinite recursion on CIS reads, sm_recheck_media
253                         won't help anyway */
254                 if (zone == 0 && block == ftl->cis_block && boffset ==
255                         ftl->cis_boffset)
256                         return ret;
257
258                 /* Test if media is stable */
259                 if (try == 3 || sm_recheck_media(ftl))
260                         return ret;
261         }
262
263         /* Unfortunelly, oob read will _always_ succeed,
264                 despite card removal..... */
265         ret = mtd->read_oob(mtd, sm_mkoffset(ftl, zone, block, boffset), &ops);
266
267         /* Test for unknown errors */
268         if (ret != 0 && ret != -EUCLEAN && ret != -EBADMSG) {
269                 dbg("read of block %d at zone %d, failed due to error (%d)",
270                         block, zone, ret);
271                 goto again;
272         }
273
274         /* Do a basic test on the oob, to guard against returned garbage */
275         if (oob->reserved != 0xFFFFFFFF && !is_power_of_2(~oob->reserved))
276                 goto again;
277
278         /* This should never happen, unless there is a bug in the mtd driver */
279         WARN_ON(ops.oobretlen != SM_OOB_SIZE);
280         WARN_ON(buffer && ops.retlen != SM_SECTOR_SIZE);
281
282         if (!buffer)
283                 return 0;
284
285         /* Test if sector marked as bad */
286         if (!sm_sector_valid(oob)) {
287                 dbg("read of block %d at zone %d, failed because it is marked"
288                         " as bad" , block, zone);
289                 goto again;
290         }
291
292         /* Test ECC*/
293         if (ret == -EBADMSG ||
294                 (ftl->smallpagenand && sm_correct_sector(buffer, oob))) {
295
296                 dbg("read of block %d at zone %d, failed due to ECC error",
297                         block, zone);
298                 goto again;
299         }
300
301         return 0;
302 }
303
304 /* Writes a sector to media */
305 static int sm_write_sector(struct sm_ftl *ftl,
306                            int zone, int block, int boffset,
307                            uint8_t *buffer, struct sm_oob *oob)
308 {
309         struct mtd_oob_ops ops;
310         struct mtd_info *mtd = ftl->trans->mtd;
311         int ret;
312
313         BUG_ON(ftl->readonly);
314
315         if (zone == 0 && (block == ftl->cis_block || block == 0)) {
316                 dbg("attempted to write the CIS!");
317                 return -EIO;
318         }
319
320         if (ftl->unstable)
321                 return -EIO;
322
323         ops.mode = ftl->smallpagenand ? MTD_OOB_RAW : MTD_OOB_PLACE;
324         ops.len = SM_SECTOR_SIZE;
325         ops.datbuf = buffer;
326         ops.ooboffs = 0;
327         ops.ooblen = SM_OOB_SIZE;
328         ops.oobbuf = (void *)oob;
329
330         ret = mtd->write_oob(mtd, sm_mkoffset(ftl, zone, block, boffset), &ops);
331
332         /* Now we assume that hardware will catch write bitflip errors */
333         /* If you are paranoid, use CONFIG_MTD_NAND_VERIFY_WRITE */
334
335         if (ret) {
336                 dbg("write to block %d at zone %d, failed with error %d",
337                         block, zone, ret);
338
339                 sm_recheck_media(ftl);
340                 return ret;
341         }
342
343         /* This should never happen, unless there is a bug in the driver */
344         WARN_ON(ops.oobretlen != SM_OOB_SIZE);
345         WARN_ON(buffer && ops.retlen != SM_SECTOR_SIZE);
346
347         return 0;
348 }
349
350 /* ------------------------ block IO ------------------------------------- */
351
352 /* Write a block using data and lba, and invalid sector bitmap */
353 static int sm_write_block(struct sm_ftl *ftl, uint8_t *buf,
354                           int zone, int block, int lba,
355                           unsigned long invalid_bitmap)
356 {
357         struct sm_oob oob;
358         int boffset;
359         int retry = 0;
360
361         /* Initialize the oob with requested values */
362         memset(&oob, 0xFF, SM_OOB_SIZE);
363         sm_write_lba(&oob, lba);
364 restart:
365         if (ftl->unstable)
366                 return -EIO;
367
368         for (boffset = 0; boffset < ftl->block_size;
369                                 boffset += SM_SECTOR_SIZE) {
370
371                 oob.data_status = 0xFF;
372
373                 if (test_bit(boffset / SM_SECTOR_SIZE, &invalid_bitmap)) {
374
375                         sm_printk("sector %d of block at LBA %d of zone %d"
376                                 " coudn't be read, marking it as invalid",
377                                 boffset / SM_SECTOR_SIZE, lba, zone);
378
379                         oob.data_status = 0;
380                 }
381
382                 if (ftl->smallpagenand) {
383                         __nand_calculate_ecc(buf + boffset,
384                                                 SM_SMALL_PAGE, oob.ecc1);
385
386                         __nand_calculate_ecc(buf + boffset + SM_SMALL_PAGE,
387                                                 SM_SMALL_PAGE, oob.ecc2);
388                 }
389                 if (!sm_write_sector(ftl, zone, block, boffset,
390                                                         buf + boffset, &oob))
391                         continue;
392
393                 if (!retry) {
394
395                         /* If write fails. try to erase the block */
396                         /* This is safe, because we never write in blocks
397                                 that contain valuable data.
398                         This is intended to repair block that are marked
399                         as erased, but that isn't fully erased*/
400
401                         if (sm_erase_block(ftl, zone, block, 0))
402                                 return -EIO;
403
404                         retry = 1;
405                         goto restart;
406                 } else {
407                         sm_mark_block_bad(ftl, zone, block);
408                         return -EIO;
409                 }
410         }
411         return 0;
412 }
413
414
415 /* Mark whole block at offset 'offs' as bad. */
416 static void sm_mark_block_bad(struct sm_ftl *ftl, int zone, int block)
417 {
418         struct sm_oob oob;
419         int boffset;
420
421         memset(&oob, 0xFF, SM_OOB_SIZE);
422         oob.block_status = 0xF0;
423
424         if (ftl->unstable)
425                 return;
426
427         if (sm_recheck_media(ftl))
428                 return;
429
430         sm_printk("marking block %d of zone %d as bad", block, zone);
431
432         /* We aren't checking the return value, because we don't care */
433         /* This also fails on fake xD cards, but I guess these won't expose
434                 any bad blocks till fail completly */
435         for (boffset = 0; boffset < ftl->block_size; boffset += SM_SECTOR_SIZE)
436                 sm_write_sector(ftl, zone, block, boffset, NULL, &oob);
437 }
438
439 /*
440  * Erase a block within a zone
441  * If erase succedes, it updates free block fifo, otherwise marks block as bad
442  */
443 static int sm_erase_block(struct sm_ftl *ftl, int zone_num, uint16_t block,
444                           int put_free)
445 {
446         struct ftl_zone *zone = &ftl->zones[zone_num];
447         struct mtd_info *mtd = ftl->trans->mtd;
448         struct erase_info erase;
449
450         erase.mtd = mtd;
451         erase.callback = sm_erase_callback;
452         erase.addr = sm_mkoffset(ftl, zone_num, block, 0);
453         erase.len = ftl->block_size;
454         erase.priv = (u_long)ftl;
455
456         if (ftl->unstable)
457                 return -EIO;
458
459         BUG_ON(ftl->readonly);
460
461         if (zone_num == 0 && (block == ftl->cis_block || block == 0)) {
462                 sm_printk("attempted to erase the CIS!");
463                 return -EIO;
464         }
465
466         if (mtd->erase(mtd, &erase)) {
467                 sm_printk("erase of block %d in zone %d failed",
468                                                         block, zone_num);
469                 goto error;
470         }
471
472         if (erase.state == MTD_ERASE_PENDING)
473                 wait_for_completion(&ftl->erase_completion);
474
475         if (erase.state != MTD_ERASE_DONE) {
476                 sm_printk("erase of block %d in zone %d failed after wait",
477                         block, zone_num);
478                 goto error;
479         }
480
481         if (put_free)
482                 kfifo_in(&zone->free_sectors,
483                         (const unsigned char *)&block, sizeof(block));
484
485         return 0;
486 error:
487         sm_mark_block_bad(ftl, zone_num, block);
488         return -EIO;
489 }
490
491 static void sm_erase_callback(struct erase_info *self)
492 {
493         struct sm_ftl *ftl = (struct sm_ftl *)self->priv;
494         complete(&ftl->erase_completion);
495 }
496
497 /* Throughtly test that block is valid. */
498 static int sm_check_block(struct sm_ftl *ftl, int zone, int block)
499 {
500         int boffset;
501         struct sm_oob oob;
502         int lbas[] = { -3, 0, 0, 0 };
503         int i = 0;
504         int test_lba;
505
506
507         /* First just check that block doesn't look fishy */
508         /* Only blocks that are valid or are sliced in two parts, are
509                 accepted */
510         for (boffset = 0; boffset < ftl->block_size;
511                                         boffset += SM_SECTOR_SIZE) {
512
513                 /* This shoudn't happen anyway */
514                 if (sm_read_sector(ftl, zone, block, boffset, NULL, &oob))
515                         return -2;
516
517                 test_lba = sm_read_lba(&oob);
518
519                 if (lbas[i] != test_lba)
520                         lbas[++i] = test_lba;
521
522                 /* If we found three different LBAs, something is fishy */
523                 if (i == 3)
524                         return -EIO;
525         }
526
527         /* If the block is sliced (partialy erased usually) erase it */
528         if (i == 2) {
529                 sm_erase_block(ftl, zone, block, 1);
530                 return 1;
531         }
532
533         return 0;
534 }
535
536 /* ----------------- media scanning --------------------------------- */
537 static const struct chs_entry chs_table[] = {
538         { 1,    125,  4,  4  },
539         { 2,    125,  4,  8  },
540         { 4,    250,  4,  8  },
541         { 8,    250,  4,  16 },
542         { 16,   500,  4,  16 },
543         { 32,   500,  8,  16 },
544         { 64,   500,  8,  32 },
545         { 128,  500,  16, 32 },
546         { 256,  1000, 16, 32 },
547         { 512,  1015, 32, 63 },
548         { 1024, 985,  33, 63 },
549         { 2048, 985,  33, 63 },
550         { 0 },
551 };
552
553
554 static const uint8_t cis_signature[] = {
555         0x01, 0x03, 0xD9, 0x01, 0xFF, 0x18, 0x02, 0xDF, 0x01, 0x20
556 };
557 /* Find out media parameters.
558  * This ideally has to be based on nand id, but for now device size is enough */
559 int sm_get_media_info(struct sm_ftl *ftl, struct mtd_info *mtd)
560 {
561         int i;
562         int size_in_megs = mtd->size / (1024 * 1024);
563
564         ftl->readonly = mtd->type == MTD_ROM;
565
566         /* Manual settings for very old devices */
567         ftl->zone_count = 1;
568         ftl->smallpagenand = 0;
569
570         switch (size_in_megs) {
571         case 1:
572                 /* 1 MiB flash/rom SmartMedia card (256 byte pages)*/
573                 ftl->zone_size = 256;
574                 ftl->max_lba = 250;
575                 ftl->block_size = 8 * SM_SECTOR_SIZE;
576                 ftl->smallpagenand = 1;
577
578                 break;
579         case 2:
580                 /* 2 MiB flash SmartMedia (256 byte pages)*/
581                 if (mtd->writesize == SM_SMALL_PAGE) {
582                         ftl->zone_size = 512;
583                         ftl->max_lba = 500;
584                         ftl->block_size = 8 * SM_SECTOR_SIZE;
585                         ftl->smallpagenand = 1;
586                 /* 2 MiB rom SmartMedia */
587                 } else {
588
589                         if (!ftl->readonly)
590                                 return -ENODEV;
591
592                         ftl->zone_size = 256;
593                         ftl->max_lba = 250;
594                         ftl->block_size = 16 * SM_SECTOR_SIZE;
595                 }
596                 break;
597         case 4:
598                 /* 4 MiB flash/rom SmartMedia device */
599                 ftl->zone_size = 512;
600                 ftl->max_lba = 500;
601                 ftl->block_size = 16 * SM_SECTOR_SIZE;
602                 break;
603         case 8:
604                 /* 8 MiB flash/rom SmartMedia device */
605                 ftl->zone_size = 1024;
606                 ftl->max_lba = 1000;
607                 ftl->block_size = 16 * SM_SECTOR_SIZE;
608         }
609
610         /* Minimum xD size is 16MiB. Also, all xD cards have standard zone
611            sizes. SmartMedia cards exist up to 128 MiB and have same layout*/
612         if (size_in_megs >= 16) {
613                 ftl->zone_count = size_in_megs / 16;
614                 ftl->zone_size = 1024;
615                 ftl->max_lba = 1000;
616                 ftl->block_size = 32 * SM_SECTOR_SIZE;
617         }
618
619         /* Test for proper write,erase and oob sizes */
620         if (mtd->erasesize > ftl->block_size)
621                 return -ENODEV;
622
623         if (mtd->writesize > SM_SECTOR_SIZE)
624                 return -ENODEV;
625
626         if (ftl->smallpagenand && mtd->oobsize < SM_SMALL_OOB_SIZE)
627                 return -ENODEV;
628
629         if (!ftl->smallpagenand && mtd->oobsize < SM_OOB_SIZE)
630                 return -ENODEV;
631
632         /* We use these functions for IO */
633         if (!mtd->read_oob || !mtd->write_oob)
634                 return -ENODEV;
635
636         /* Find geometry information */
637         for (i = 0 ; i < ARRAY_SIZE(chs_table) ; i++) {
638                 if (chs_table[i].size == size_in_megs) {
639                         ftl->cylinders = chs_table[i].cyl;
640                         ftl->heads = chs_table[i].head;
641                         ftl->sectors = chs_table[i].sec;
642                         return 0;
643                 }
644         }
645
646         sm_printk("media has unknown size : %dMiB", size_in_megs);
647         ftl->cylinders = 985;
648         ftl->heads =  33;
649         ftl->sectors = 63;
650         return 0;
651 }
652
653 /* Validate the CIS */
654 static int sm_read_cis(struct sm_ftl *ftl)
655 {
656         struct sm_oob oob;
657
658         if (sm_read_sector(ftl,
659                 0, ftl->cis_block, ftl->cis_boffset, ftl->cis_buffer, &oob))
660                         return -EIO;
661
662         if (!sm_sector_valid(&oob) || !sm_block_valid(&oob))
663                 return -EIO;
664
665         if (!memcmp(ftl->cis_buffer + ftl->cis_page_offset,
666                         cis_signature, sizeof(cis_signature))) {
667                 return 0;
668         }
669
670         return -EIO;
671 }
672
673 /* Scan the media for the CIS */
674 static int sm_find_cis(struct sm_ftl *ftl)
675 {
676         struct sm_oob oob;
677         int block, boffset;
678         int block_found = 0;
679         int cis_found = 0;
680
681         /* Search for first valid block */
682         for (block = 0 ; block < ftl->zone_size - ftl->max_lba ; block++) {
683
684                 if (sm_read_sector(ftl, 0, block, 0, NULL, &oob))
685                         continue;
686
687                 if (!sm_block_valid(&oob))
688                         continue;
689                 block_found = 1;
690                 break;
691         }
692
693         if (!block_found)
694                 return -EIO;
695
696         /* Search for first valid sector in this block */
697         for (boffset = 0 ; boffset < ftl->block_size;
698                                                 boffset += SM_SECTOR_SIZE) {
699
700                 if (sm_read_sector(ftl, 0, block, boffset, NULL, &oob))
701                         continue;
702
703                 if (!sm_sector_valid(&oob))
704                         continue;
705                 break;
706         }
707
708         if (boffset == ftl->block_size)
709                 return -EIO;
710
711         ftl->cis_block = block;
712         ftl->cis_boffset = boffset;
713         ftl->cis_page_offset = 0;
714
715         cis_found = !sm_read_cis(ftl);
716
717         if (!cis_found) {
718                 ftl->cis_page_offset = SM_SMALL_PAGE;
719                 cis_found = !sm_read_cis(ftl);
720         }
721
722         if (cis_found) {
723                 dbg("CIS block found at offset %x",
724                         block * ftl->block_size +
725                                 boffset + ftl->cis_page_offset);
726                 return 0;
727         }
728         return -EIO;
729 }
730
731 /* Basic test to determine if underlying mtd device if functional */
732 static int sm_recheck_media(struct sm_ftl *ftl)
733 {
734         if (sm_read_cis(ftl)) {
735
736                 if (!ftl->unstable) {
737                         sm_printk("media unstable, not allowing writes");
738                         ftl->unstable = 1;
739                 }
740                 return -EIO;
741         }
742         return 0;
743 }
744
745 /* Initialize a FTL zone */
746 static int sm_init_zone(struct sm_ftl *ftl, int zone_num)
747 {
748         struct ftl_zone *zone = &ftl->zones[zone_num];
749         struct sm_oob oob;
750         uint16_t block;
751         int lba;
752         int i = 0;
753         int len;
754
755         dbg("initializing zone %d", zone_num);
756
757         /* Allocate memory for FTL table */
758         zone->lba_to_phys_table = kmalloc(ftl->max_lba * 2, GFP_KERNEL);
759
760         if (!zone->lba_to_phys_table)
761                 return -ENOMEM;
762         memset(zone->lba_to_phys_table, -1, ftl->max_lba * 2);
763
764
765         /* Allocate memory for free sectors FIFO */
766         if (kfifo_alloc(&zone->free_sectors, ftl->zone_size * 2, GFP_KERNEL)) {
767                 kfree(zone->lba_to_phys_table);
768                 return -ENOMEM;
769         }
770
771         /* Now scan the zone */
772         for (block = 0 ; block < ftl->zone_size ; block++) {
773
774                 /* Skip blocks till the CIS (including) */
775                 if (zone_num == 0 && block <= ftl->cis_block)
776                         continue;
777
778                 /* Read the oob of first sector */
779                 if (sm_read_sector(ftl, zone_num, block, 0, NULL, &oob))
780                         return -EIO;
781
782                 /* Test to see if block is erased. It is enough to test
783                         first sector, because erase happens in one shot */
784                 if (sm_block_erased(&oob)) {
785                         kfifo_in(&zone->free_sectors,
786                                 (unsigned char *)&block, 2);
787                         continue;
788                 }
789
790                 /* If block is marked as bad, skip it */
791                 /* This assumes we can trust first sector*/
792                 /* However the way the block valid status is defined, ensures
793                         very low probability of failure here */
794                 if (!sm_block_valid(&oob)) {
795                         dbg("PH %04d <-> <marked bad>", block);
796                         continue;
797                 }
798
799
800                 lba = sm_read_lba(&oob);
801
802                 /* Invalid LBA means that block is damaged. */
803                 /* We can try to erase it, or mark it as bad, but
804                         lets leave that to recovery application */
805                 if (lba == -2 || lba >= ftl->max_lba) {
806                         dbg("PH %04d <-> LBA %04d(bad)", block, lba);
807                         continue;
808                 }
809
810
811                 /* If there is no collision,
812                         just put the sector in the FTL table */
813                 if (zone->lba_to_phys_table[lba] < 0) {
814                         dbg_verbose("PH %04d <-> LBA %04d", block, lba);
815                         zone->lba_to_phys_table[lba] = block;
816                         continue;
817                 }
818
819                 sm_printk("collision"
820                         " of LBA %d between blocks %d and %d in zone %d",
821                         lba, zone->lba_to_phys_table[lba], block, zone_num);
822
823                 /* Test that this block is valid*/
824                 if (sm_check_block(ftl, zone_num, block))
825                         continue;
826
827                 /* Test now the old block */
828                 if (sm_check_block(ftl, zone_num,
829                                         zone->lba_to_phys_table[lba])) {
830                         zone->lba_to_phys_table[lba] = block;
831                         continue;
832                 }
833
834                 /* If both blocks are valid and share same LBA, it means that
835                         they hold different versions of same data. It not
836                         known which is more recent, thus just erase one of them
837                 */
838                 sm_printk("both blocks are valid, erasing the later");
839                 sm_erase_block(ftl, zone_num, block, 1);
840         }
841
842         dbg("zone initialized");
843         zone->initialized = 1;
844
845         /* No free sectors, means that the zone is heavily damaged, write won't
846                 work, but it can still can be (partially) read */
847         if (!kfifo_len(&zone->free_sectors)) {
848                 sm_printk("no free blocks in zone %d", zone_num);
849                 return 0;
850         }
851
852         /* Randomize first block we write to */
853         get_random_bytes(&i, 2);
854         i %= (kfifo_len(&zone->free_sectors) / 2);
855
856         while (i--) {
857                 len = kfifo_out(&zone->free_sectors,
858                                         (unsigned char *)&block, 2);
859                 WARN_ON(len != 2);
860                 kfifo_in(&zone->free_sectors, (const unsigned char *)&block, 2);
861         }
862         return 0;
863 }
864
865 /* Get and automaticly initialize an FTL mapping for one zone */
866 struct ftl_zone *sm_get_zone(struct sm_ftl *ftl, int zone_num)
867 {
868         struct ftl_zone *zone;
869         int error;
870
871         BUG_ON(zone_num >= ftl->zone_count);
872         zone = &ftl->zones[zone_num];
873
874         if (!zone->initialized) {
875                 error = sm_init_zone(ftl, zone_num);
876
877                 if (error)
878                         return ERR_PTR(error);
879         }
880         return zone;
881 }
882
883
884 /* ----------------- cache handling ------------------------------------------*/
885
886 /* Initialize the one block cache */
887 void sm_cache_init(struct sm_ftl *ftl)
888 {
889         ftl->cache_data_invalid_bitmap = 0xFFFFFFFF;
890         ftl->cache_clean = 1;
891         ftl->cache_zone = -1;
892         ftl->cache_block = -1;
893         /*memset(ftl->cache_data, 0xAA, ftl->block_size);*/
894 }
895
896 /* Put sector in one block cache */
897 void sm_cache_put(struct sm_ftl *ftl, char *buffer, int boffset)
898 {
899         memcpy(ftl->cache_data + boffset, buffer, SM_SECTOR_SIZE);
900         clear_bit(boffset / SM_SECTOR_SIZE, &ftl->cache_data_invalid_bitmap);
901         ftl->cache_clean = 0;
902 }
903
904 /* Read a sector from the cache */
905 int sm_cache_get(struct sm_ftl *ftl, char *buffer, int boffset)
906 {
907         if (test_bit(boffset / SM_SECTOR_SIZE,
908                 &ftl->cache_data_invalid_bitmap))
909                         return -1;
910
911         memcpy(buffer, ftl->cache_data + boffset, SM_SECTOR_SIZE);
912         return 0;
913 }
914
915 /* Write the cache to hardware */
916 int sm_cache_flush(struct sm_ftl *ftl)
917 {
918         struct ftl_zone *zone;
919
920         int sector_num;
921         uint16_t write_sector;
922         int zone_num = ftl->cache_zone;
923         int block_num;
924
925         if (ftl->cache_clean)
926                 return 0;
927
928         if (ftl->unstable)
929                 return -EIO;
930
931         BUG_ON(zone_num < 0);
932         zone = &ftl->zones[zone_num];
933         block_num = zone->lba_to_phys_table[ftl->cache_block];
934
935
936         /* Try to read all unread areas of the cache block*/
937         for_each_set_bit(sector_num, &ftl->cache_data_invalid_bitmap,
938                 ftl->block_size / SM_SECTOR_SIZE) {
939
940                 if (!sm_read_sector(ftl,
941                         zone_num, block_num, sector_num * SM_SECTOR_SIZE,
942                         ftl->cache_data + sector_num * SM_SECTOR_SIZE, NULL))
943                                 clear_bit(sector_num,
944                                         &ftl->cache_data_invalid_bitmap);
945         }
946 restart:
947
948         if (ftl->unstable)
949                 return -EIO;
950
951         /* If there are no spare blocks, */
952         /* we could still continue by erasing/writing the current block,
953                 but for such worn out media it doesn't worth the trouble,
954                         and the dangers */
955         if (kfifo_out(&zone->free_sectors,
956                                 (unsigned char *)&write_sector, 2) != 2) {
957                 dbg("no free sectors for write!");
958                 return -EIO;
959         }
960
961
962         if (sm_write_block(ftl, ftl->cache_data, zone_num, write_sector,
963                 ftl->cache_block, ftl->cache_data_invalid_bitmap))
964                         goto restart;
965
966         /* Update the FTL table */
967         zone->lba_to_phys_table[ftl->cache_block] = write_sector;
968
969         /* Write succesfull, so erase and free the old block */
970         if (block_num > 0)
971                 sm_erase_block(ftl, zone_num, block_num, 1);
972
973         sm_cache_init(ftl);
974         return 0;
975 }
976
977
978 /* flush timer, runs a second after last write */
979 static void sm_cache_flush_timer(unsigned long data)
980 {
981         struct sm_ftl *ftl = (struct sm_ftl *)data;
982         queue_work(cache_flush_workqueue, &ftl->flush_work);
983 }
984
985 /* cache flush work, kicked by timer */
986 static void sm_cache_flush_work(struct work_struct *work)
987 {
988         struct sm_ftl *ftl = container_of(work, struct sm_ftl, flush_work);
989         mutex_lock(&ftl->mutex);
990         sm_cache_flush(ftl);
991         mutex_unlock(&ftl->mutex);
992         return;
993 }
994
995 /* ---------------- outside interface -------------------------------------- */
996
997 /* outside interface: read a sector */
998 static int sm_read(struct mtd_blktrans_dev *dev,
999                    unsigned long sect_no, char *buf)
1000 {
1001         struct sm_ftl *ftl = dev->priv;
1002         struct ftl_zone *zone;
1003         int error = 0, in_cache = 0;
1004         int zone_num, block, boffset;
1005
1006         sm_break_offset(ftl, sect_no << 9, &zone_num, &block, &boffset);
1007         mutex_lock(&ftl->mutex);
1008
1009
1010         zone = sm_get_zone(ftl, zone_num);
1011         if (IS_ERR(zone)) {
1012                 error = PTR_ERR(zone);
1013                 goto unlock;
1014         }
1015
1016         /* Have to look at cache first */
1017         if (ftl->cache_zone == zone_num && ftl->cache_block == block) {
1018                 in_cache = 1;
1019                 if (!sm_cache_get(ftl, buf, boffset))
1020                         goto unlock;
1021         }
1022
1023         /* Translate the block and return if doesn't exist in the table */
1024         block = zone->lba_to_phys_table[block];
1025
1026         if (block == -1) {
1027                 memset(buf, 0xFF, SM_SECTOR_SIZE);
1028                 goto unlock;
1029         }
1030
1031         if (sm_read_sector(ftl, zone_num, block, boffset, buf, NULL)) {
1032                 error = -EIO;
1033                 goto unlock;
1034         }
1035
1036         if (in_cache)
1037                 sm_cache_put(ftl, buf, boffset);
1038 unlock:
1039         mutex_unlock(&ftl->mutex);
1040         return error;
1041 }
1042
1043 /* outside interface: write a sector */
1044 static int sm_write(struct mtd_blktrans_dev *dev,
1045                                 unsigned long sec_no, char *buf)
1046 {
1047         struct sm_ftl *ftl = dev->priv;
1048         struct ftl_zone *zone;
1049         int error, zone_num, block, boffset;
1050
1051         BUG_ON(ftl->readonly);
1052         sm_break_offset(ftl, sec_no << 9, &zone_num, &block, &boffset);
1053
1054         /* No need in flush thread running now */
1055         del_timer(&ftl->timer);
1056         mutex_lock(&ftl->mutex);
1057
1058         zone = sm_get_zone(ftl, zone_num);
1059         if (IS_ERR(zone)) {
1060                 error = PTR_ERR(zone);
1061                 goto unlock;
1062         }
1063
1064         /* If entry is not in cache, flush it */
1065         if (ftl->cache_block != block || ftl->cache_zone != zone_num) {
1066
1067                 error = sm_cache_flush(ftl);
1068                 if (error)
1069                         goto unlock;
1070
1071                 ftl->cache_block = block;
1072                 ftl->cache_zone = zone_num;
1073         }
1074
1075         sm_cache_put(ftl, buf, boffset);
1076 unlock:
1077         mod_timer(&ftl->timer, jiffies + msecs_to_jiffies(cache_timeout));
1078         mutex_unlock(&ftl->mutex);
1079         return error;
1080 }
1081
1082 /* outside interface: flush everything */
1083 static int sm_flush(struct mtd_blktrans_dev *dev)
1084 {
1085         struct sm_ftl *ftl = dev->priv;
1086         int retval;
1087
1088         mutex_lock(&ftl->mutex);
1089         retval =  sm_cache_flush(ftl);
1090         mutex_unlock(&ftl->mutex);
1091         return retval;
1092 }
1093
1094 /* outside interface: device is released */
1095 static int sm_release(struct mtd_blktrans_dev *dev)
1096 {
1097         struct sm_ftl *ftl = dev->priv;
1098
1099         mutex_lock(&ftl->mutex);
1100         del_timer_sync(&ftl->timer);
1101         cancel_work_sync(&ftl->flush_work);
1102         sm_cache_flush(ftl);
1103         mutex_unlock(&ftl->mutex);
1104         return 0;
1105 }
1106
1107 /* outside interface: get geometry */
1108 static int sm_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
1109 {
1110         struct sm_ftl *ftl = dev->priv;
1111         geo->heads = ftl->heads;
1112         geo->sectors = ftl->sectors;
1113         geo->cylinders = ftl->cylinders;
1114         return 0;
1115 }
1116
1117 /* external interface: main initialization function */
1118 static void sm_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
1119 {
1120         struct mtd_blktrans_dev *trans;
1121         struct sm_ftl *ftl;
1122
1123         /* Allocate & initialize our private structure */
1124         ftl = kzalloc(sizeof(struct sm_ftl), GFP_KERNEL);
1125         if (!ftl)
1126                 goto error1;
1127
1128
1129         mutex_init(&ftl->mutex);
1130         setup_timer(&ftl->timer, sm_cache_flush_timer, (unsigned long)ftl);
1131         INIT_WORK(&ftl->flush_work, sm_cache_flush_work);
1132         init_completion(&ftl->erase_completion);
1133
1134         /* Read media information */
1135         if (sm_get_media_info(ftl, mtd)) {
1136                 dbg("found unsupported mtd device, aborting");
1137                 goto error2;
1138         }
1139
1140
1141         /* Allocate temporary CIS buffer for read retry support */
1142         ftl->cis_buffer = kzalloc(SM_SECTOR_SIZE, GFP_KERNEL);
1143         if (!ftl->cis_buffer)
1144                 goto error2;
1145
1146         /* Allocate zone array, it will be initialized on demand */
1147         ftl->zones = kzalloc(sizeof(struct ftl_zone) * ftl->zone_count,
1148                                                                 GFP_KERNEL);
1149         if (!ftl->zones)
1150                 goto error3;
1151
1152         /* Allocate the cache*/
1153         ftl->cache_data = kzalloc(ftl->block_size, GFP_KERNEL);
1154
1155         if (!ftl->cache_data)
1156                 goto error4;
1157
1158         sm_cache_init(ftl);
1159
1160
1161         /* Allocate upper layer structure and initialize it */
1162         trans = kzalloc(sizeof(struct mtd_blktrans_dev), GFP_KERNEL);
1163         if (!trans)
1164                 goto error5;
1165
1166         ftl->trans = trans;
1167         trans->priv = ftl;
1168
1169         trans->tr = tr;
1170         trans->mtd = mtd;
1171         trans->devnum = -1;
1172         trans->size = (ftl->block_size * ftl->max_lba * ftl->zone_count) >> 9;
1173         trans->readonly = ftl->readonly;
1174
1175         if (sm_find_cis(ftl)) {
1176                 dbg("CIS not found on mtd device, aborting");
1177                 goto error6;
1178         }
1179
1180         ftl->disk_attributes = sm_create_sysfs_attributes(ftl);
1181         trans->disk_attributes = ftl->disk_attributes;
1182
1183         sm_printk("Found %d MiB xD/SmartMedia FTL on mtd%d",
1184                 (int)(mtd->size / (1024 * 1024)), mtd->index);
1185
1186         dbg("FTL layout:");
1187         dbg("%d zone(s), each consists of %d blocks (+%d spares)",
1188                 ftl->zone_count, ftl->max_lba,
1189                 ftl->zone_size - ftl->max_lba);
1190         dbg("each block consists of %d bytes",
1191                 ftl->block_size);
1192
1193
1194         /* Register device*/
1195         if (add_mtd_blktrans_dev(trans)) {
1196                 dbg("error in mtdblktrans layer");
1197                 goto error6;
1198         }
1199         return;
1200 error6:
1201         kfree(trans);
1202 error5:
1203         kfree(ftl->cache_data);
1204 error4:
1205         kfree(ftl->zones);
1206 error3:
1207         kfree(ftl->cis_buffer);
1208 error2:
1209         kfree(ftl);
1210 error1:
1211         return;
1212 }
1213
1214 /* main interface: device {surprise,} removal */
1215 static void sm_remove_dev(struct mtd_blktrans_dev *dev)
1216 {
1217         struct sm_ftl *ftl = dev->priv;
1218         int i;
1219
1220         del_mtd_blktrans_dev(dev);
1221         ftl->trans = NULL;
1222
1223         for (i = 0 ; i < ftl->zone_count; i++) {
1224
1225                 if (!ftl->zones[i].initialized)
1226                         continue;
1227
1228                 kfree(ftl->zones[i].lba_to_phys_table);
1229                 kfifo_free(&ftl->zones[i].free_sectors);
1230         }
1231
1232         sm_delete_sysfs_attributes(ftl);
1233         kfree(ftl->cis_buffer);
1234         kfree(ftl->zones);
1235         kfree(ftl->cache_data);
1236         kfree(ftl);
1237 }
1238
1239 static struct mtd_blktrans_ops sm_ftl_ops = {
1240         .name           = "smblk",
1241         .major          = -1,
1242         .part_bits      = SM_FTL_PARTN_BITS,
1243         .blksize        = SM_SECTOR_SIZE,
1244         .getgeo         = sm_getgeo,
1245
1246         .add_mtd        = sm_add_mtd,
1247         .remove_dev     = sm_remove_dev,
1248
1249         .readsect       = sm_read,
1250         .writesect      = sm_write,
1251
1252         .flush          = sm_flush,
1253         .release        = sm_release,
1254
1255         .owner          = THIS_MODULE,
1256 };
1257
1258 static __init int sm_module_init(void)
1259 {
1260         int error = 0;
1261         cache_flush_workqueue = create_freezeable_workqueue("smflush");
1262
1263         if (IS_ERR(cache_flush_workqueue))
1264                 return PTR_ERR(cache_flush_workqueue);
1265
1266         error = register_mtd_blktrans(&sm_ftl_ops);
1267         if (error)
1268                 destroy_workqueue(cache_flush_workqueue);
1269         return error;
1270
1271 }
1272
1273 static void __exit sm_module_exit(void)
1274 {
1275         destroy_workqueue(cache_flush_workqueue);
1276         deregister_mtd_blktrans(&sm_ftl_ops);
1277 }
1278
1279 module_init(sm_module_init);
1280 module_exit(sm_module_exit);
1281
1282 MODULE_LICENSE("GPL");
1283 MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
1284 MODULE_DESCRIPTION("Smartmedia/xD mtd translation layer");