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