2 * Copyright © 2009 - Maxim Levitsky
3 * SmartMedia/xD translation layer
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.
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"
25 static struct workqueue_struct
*cache_flush_workqueue
;
27 static int cache_timeout
= 1000;
28 module_param(cache_timeout
, int, S_IRUGO
);
29 MODULE_PARM_DESC(cache_timeout
,
30 "Timeout (in ms) for cache flush (1000 ms default");
33 module_param(debug
, int, S_IRUGO
| S_IWUSR
);
34 MODULE_PARM_DESC(debug
, "Debug level (0-2)");
37 /* ------------------- sysfs attributes ---------------------------------- */
38 struct sm_sysfs_attribute
{
39 struct device_attribute dev_attr
;
44 static ssize_t
sm_attr_show(struct device
*dev
, struct device_attribute
*attr
,
47 struct sm_sysfs_attribute
*sm_attr
=
48 container_of(attr
, struct sm_sysfs_attribute
, dev_attr
);
50 strncpy(buf
, sm_attr
->data
, sm_attr
->len
);
55 #define NUM_ATTRIBUTES 1
56 #define SM_CIS_VENDOR_OFFSET 0x59
57 static struct attribute_group
*sm_create_sysfs_attributes(struct sm_ftl
*ftl
)
59 struct attribute_group
*attr_group
;
60 struct attribute
**attributes
;
61 struct sm_sysfs_attribute
*vendor_attribute
;
64 vendor
= kstrndup(ftl
->cis_buffer
+ SM_CIS_VENDOR_OFFSET
,
65 SM_SMALL_PAGE
- SM_CIS_VENDOR_OFFSET
, GFP_KERNEL
);
69 /* Initialize sysfs attributes */
71 kzalloc(sizeof(struct sm_sysfs_attribute
), GFP_KERNEL
);
72 if (!vendor_attribute
)
75 sysfs_attr_init(&vendor_attribute
->dev_attr
.attr
);
77 vendor_attribute
->data
= vendor
;
78 vendor_attribute
->len
= strlen(vendor
);
79 vendor_attribute
->dev_attr
.attr
.name
= "vendor";
80 vendor_attribute
->dev_attr
.attr
.mode
= S_IRUGO
;
81 vendor_attribute
->dev_attr
.show
= sm_attr_show
;
84 /* Create array of pointers to the attributes */
85 attributes
= kzalloc(sizeof(struct attribute
*) * (NUM_ATTRIBUTES
+ 1),
89 attributes
[0] = &vendor_attribute
->dev_attr
.attr
;
91 /* Finally create the attribute group */
92 attr_group
= kzalloc(sizeof(struct attribute_group
), GFP_KERNEL
);
95 attr_group
->attrs
= attributes
;
100 kfree(vendor_attribute
);
107 static void sm_delete_sysfs_attributes(struct sm_ftl
*ftl
)
109 struct attribute
**attributes
= ftl
->disk_attributes
->attrs
;
112 for (i
= 0; attributes
[i
] ; i
++) {
114 struct device_attribute
*dev_attr
= container_of(attributes
[i
],
115 struct device_attribute
, attr
);
117 struct sm_sysfs_attribute
*sm_attr
=
118 container_of(dev_attr
,
119 struct sm_sysfs_attribute
, dev_attr
);
121 kfree(sm_attr
->data
);
125 kfree(ftl
->disk_attributes
->attrs
);
126 kfree(ftl
->disk_attributes
);
130 /* ----------------------- oob helpers -------------------------------------- */
132 static int sm_get_lba(uint8_t *lba
)
134 /* check fixed bits */
135 if ((lba
[0] & 0xF8) != 0x10)
138 /* check parity - endianness doesn't matter */
139 if (hweight16(*(uint16_t *)lba
) & 1)
142 return (lba
[1] >> 1) | ((lba
[0] & 0x07) << 7);
147 * Read LBA associated with block
148 * returns -1, if block is erased
149 * returns -2 if error happens
151 static int sm_read_lba(struct sm_oob
*oob
)
153 static const uint32_t erased_pattern
[4] = {
154 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
159 /* First test for erased block */
160 if (!memcmp(oob
, erased_pattern
, SM_OOB_SIZE
))
163 /* Now check is both copies of the LBA differ too much */
164 lba_test
= *(uint16_t *)oob
->lba_copy1
^ *(uint16_t*)oob
->lba_copy2
;
165 if (lba_test
&& !is_power_of_2(lba_test
))
169 lba
= sm_get_lba(oob
->lba_copy1
);
172 lba
= sm_get_lba(oob
->lba_copy2
);
177 static void sm_write_lba(struct sm_oob
*oob
, uint16_t lba
)
181 WARN_ON(lba
>= 1000);
183 tmp
[0] = 0x10 | ((lba
>> 7) & 0x07);
184 tmp
[1] = (lba
<< 1) & 0xFF;
186 if (hweight16(*(uint16_t *)tmp
) & 0x01)
189 oob
->lba_copy1
[0] = oob
->lba_copy2
[0] = tmp
[0];
190 oob
->lba_copy1
[1] = oob
->lba_copy2
[1] = tmp
[1];
194 /* Make offset from parts */
195 static loff_t
sm_mkoffset(struct sm_ftl
*ftl
, int zone
, int block
, int boffset
)
197 WARN_ON(boffset
& (SM_SECTOR_SIZE
- 1));
198 WARN_ON(zone
< 0 || zone
>= ftl
->zone_count
);
199 WARN_ON(block
>= ftl
->zone_size
);
200 WARN_ON(boffset
>= ftl
->block_size
);
205 return (zone
* SM_MAX_ZONE_SIZE
+ block
) * ftl
->block_size
+ boffset
;
208 /* Breaks offset into parts */
209 static void sm_break_offset(struct sm_ftl
*ftl
, loff_t loffset
,
210 int *zone
, int *block
, int *boffset
)
212 u64 offset
= loffset
;
213 *boffset
= do_div(offset
, ftl
->block_size
);
214 *block
= do_div(offset
, ftl
->max_lba
);
215 *zone
= offset
>= ftl
->zone_count
? -1 : offset
;
218 /* ---------------------- low level IO ------------------------------------- */
220 static int sm_correct_sector(uint8_t *buffer
, struct sm_oob
*oob
)
224 __nand_calculate_ecc(buffer
, SM_SMALL_PAGE
, ecc
);
225 if (__nand_correct_data(buffer
, ecc
, oob
->ecc1
, SM_SMALL_PAGE
) < 0)
228 buffer
+= SM_SMALL_PAGE
;
230 __nand_calculate_ecc(buffer
, SM_SMALL_PAGE
, ecc
);
231 if (__nand_correct_data(buffer
, ecc
, oob
->ecc2
, SM_SMALL_PAGE
) < 0)
236 /* Reads a sector + oob*/
237 static int sm_read_sector(struct sm_ftl
*ftl
,
238 int zone
, int block
, int boffset
,
239 uint8_t *buffer
, struct sm_oob
*oob
)
241 struct mtd_info
*mtd
= ftl
->trans
->mtd
;
242 struct mtd_oob_ops ops
;
243 struct sm_oob tmp_oob
;
247 /* FTL can contain -1 entries that are by default filled with bits */
249 memset(buffer
, 0xFF, SM_SECTOR_SIZE
);
253 /* User might not need the oob, but we do for data verification */
257 ops
.mode
= ftl
->smallpagenand
? MTD_OPS_RAW
: MTD_OPS_PLACE_OOB
;
259 ops
.ooblen
= SM_OOB_SIZE
;
260 ops
.oobbuf
= (void *)oob
;
261 ops
.len
= SM_SECTOR_SIZE
;
266 /* Avoid infinite recursion on CIS reads, sm_recheck_media
268 if (zone
== 0 && block
== ftl
->cis_block
&& boffset
==
272 /* Test if media is stable */
273 if (try == 3 || sm_recheck_media(ftl
))
277 /* Unfortunately, oob read will _always_ succeed,
278 despite card removal..... */
279 ret
= mtd_read_oob(mtd
, sm_mkoffset(ftl
, zone
, block
, boffset
), &ops
);
281 /* Test for unknown errors */
282 if (ret
!= 0 && !mtd_is_bitflip_or_eccerr(ret
)) {
283 dbg("read of block %d at zone %d, failed due to error (%d)",
288 /* Do a basic test on the oob, to guard against returned garbage */
289 if (oob
->reserved
!= 0xFFFFFFFF && !is_power_of_2(~oob
->reserved
))
292 /* This should never happen, unless there is a bug in the mtd driver */
293 WARN_ON(ops
.oobretlen
!= SM_OOB_SIZE
);
294 WARN_ON(buffer
&& ops
.retlen
!= SM_SECTOR_SIZE
);
299 /* Test if sector marked as bad */
300 if (!sm_sector_valid(oob
)) {
301 dbg("read of block %d at zone %d, failed because it is marked"
302 " as bad" , block
, zone
);
307 if (mtd_is_eccerr(ret
) ||
308 (ftl
->smallpagenand
&& sm_correct_sector(buffer
, oob
))) {
310 dbg("read of block %d at zone %d, failed due to ECC error",
318 /* Writes a sector to media */
319 static int sm_write_sector(struct sm_ftl
*ftl
,
320 int zone
, int block
, int boffset
,
321 uint8_t *buffer
, struct sm_oob
*oob
)
323 struct mtd_oob_ops ops
;
324 struct mtd_info
*mtd
= ftl
->trans
->mtd
;
327 BUG_ON(ftl
->readonly
);
329 if (zone
== 0 && (block
== ftl
->cis_block
|| block
== 0)) {
330 dbg("attempted to write the CIS!");
337 ops
.mode
= ftl
->smallpagenand
? MTD_OPS_RAW
: MTD_OPS_PLACE_OOB
;
338 ops
.len
= SM_SECTOR_SIZE
;
341 ops
.ooblen
= SM_OOB_SIZE
;
342 ops
.oobbuf
= (void *)oob
;
344 ret
= mtd_write_oob(mtd
, sm_mkoffset(ftl
, zone
, block
, boffset
), &ops
);
346 /* Now we assume that hardware will catch write bitflip errors */
349 dbg("write to block %d at zone %d, failed with error %d",
352 sm_recheck_media(ftl
);
356 /* This should never happen, unless there is a bug in the driver */
357 WARN_ON(ops
.oobretlen
!= SM_OOB_SIZE
);
358 WARN_ON(buffer
&& ops
.retlen
!= SM_SECTOR_SIZE
);
363 /* ------------------------ block IO ------------------------------------- */
365 /* Write a block using data and lba, and invalid sector bitmap */
366 static int sm_write_block(struct sm_ftl
*ftl
, uint8_t *buf
,
367 int zone
, int block
, int lba
,
368 unsigned long invalid_bitmap
)
374 /* Initialize the oob with requested values */
375 memset(&oob
, 0xFF, SM_OOB_SIZE
);
376 sm_write_lba(&oob
, lba
);
381 for (boffset
= 0; boffset
< ftl
->block_size
;
382 boffset
+= SM_SECTOR_SIZE
) {
384 oob
.data_status
= 0xFF;
386 if (test_bit(boffset
/ SM_SECTOR_SIZE
, &invalid_bitmap
)) {
388 sm_printk("sector %d of block at LBA %d of zone %d"
389 " couldn't be read, marking it as invalid",
390 boffset
/ SM_SECTOR_SIZE
, lba
, zone
);
395 if (ftl
->smallpagenand
) {
396 __nand_calculate_ecc(buf
+ boffset
,
397 SM_SMALL_PAGE
, oob
.ecc1
);
399 __nand_calculate_ecc(buf
+ boffset
+ SM_SMALL_PAGE
,
400 SM_SMALL_PAGE
, oob
.ecc2
);
402 if (!sm_write_sector(ftl
, zone
, block
, boffset
,
403 buf
+ boffset
, &oob
))
408 /* If write fails. try to erase the block */
409 /* This is safe, because we never write in blocks
410 that contain valuable data.
411 This is intended to repair block that are marked
412 as erased, but that isn't fully erased*/
414 if (sm_erase_block(ftl
, zone
, block
, 0))
420 sm_mark_block_bad(ftl
, zone
, block
);
428 /* Mark whole block at offset 'offs' as bad. */
429 static void sm_mark_block_bad(struct sm_ftl
*ftl
, int zone
, int block
)
434 memset(&oob
, 0xFF, SM_OOB_SIZE
);
435 oob
.block_status
= 0xF0;
440 if (sm_recheck_media(ftl
))
443 sm_printk("marking block %d of zone %d as bad", block
, zone
);
445 /* We aren't checking the return value, because we don't care */
446 /* This also fails on fake xD cards, but I guess these won't expose
447 any bad blocks till fail completely */
448 for (boffset
= 0; boffset
< ftl
->block_size
; boffset
+= SM_SECTOR_SIZE
)
449 sm_write_sector(ftl
, zone
, block
, boffset
, NULL
, &oob
);
453 * Erase a block within a zone
454 * If erase succeeds, it updates free block fifo, otherwise marks block as bad
456 static int sm_erase_block(struct sm_ftl
*ftl
, int zone_num
, uint16_t block
,
459 struct ftl_zone
*zone
= &ftl
->zones
[zone_num
];
460 struct mtd_info
*mtd
= ftl
->trans
->mtd
;
461 struct erase_info erase
;
464 erase
.callback
= sm_erase_callback
;
465 erase
.addr
= sm_mkoffset(ftl
, zone_num
, block
, 0);
466 erase
.len
= ftl
->block_size
;
467 erase
.priv
= (u_long
)ftl
;
472 BUG_ON(ftl
->readonly
);
474 if (zone_num
== 0 && (block
== ftl
->cis_block
|| block
== 0)) {
475 sm_printk("attempted to erase the CIS!");
479 if (mtd_erase(mtd
, &erase
)) {
480 sm_printk("erase of block %d in zone %d failed",
485 if (erase
.state
== MTD_ERASE_PENDING
)
486 wait_for_completion(&ftl
->erase_completion
);
488 if (erase
.state
!= MTD_ERASE_DONE
) {
489 sm_printk("erase of block %d in zone %d failed after wait",
495 kfifo_in(&zone
->free_sectors
,
496 (const unsigned char *)&block
, sizeof(block
));
500 sm_mark_block_bad(ftl
, zone_num
, block
);
504 static void sm_erase_callback(struct erase_info
*self
)
506 struct sm_ftl
*ftl
= (struct sm_ftl
*)self
->priv
;
507 complete(&ftl
->erase_completion
);
510 /* Thoroughly test that block is valid. */
511 static int sm_check_block(struct sm_ftl
*ftl
, int zone
, int block
)
515 int lbas
[] = { -3, 0, 0, 0 };
520 /* First just check that block doesn't look fishy */
521 /* Only blocks that are valid or are sliced in two parts, are
523 for (boffset
= 0; boffset
< ftl
->block_size
;
524 boffset
+= SM_SECTOR_SIZE
) {
526 /* This shouldn't happen anyway */
527 if (sm_read_sector(ftl
, zone
, block
, boffset
, NULL
, &oob
))
530 test_lba
= sm_read_lba(&oob
);
532 if (lbas
[i
] != test_lba
)
533 lbas
[++i
] = test_lba
;
535 /* If we found three different LBAs, something is fishy */
540 /* If the block is sliced (partially erased usually) erase it */
542 sm_erase_block(ftl
, zone
, block
, 1);
549 /* ----------------- media scanning --------------------------------- */
550 static const struct chs_entry chs_table
[] = {
558 { 128, 500, 16, 32 },
559 { 256, 1000, 16, 32 },
560 { 512, 1015, 32, 63 },
561 { 1024, 985, 33, 63 },
562 { 2048, 985, 33, 63 },
567 static const uint8_t cis_signature
[] = {
568 0x01, 0x03, 0xD9, 0x01, 0xFF, 0x18, 0x02, 0xDF, 0x01, 0x20
570 /* Find out media parameters.
571 * This ideally has to be based on nand id, but for now device size is enough */
572 static int sm_get_media_info(struct sm_ftl
*ftl
, struct mtd_info
*mtd
)
575 int size_in_megs
= mtd
->size
/ (1024 * 1024);
577 ftl
->readonly
= mtd
->type
== MTD_ROM
;
579 /* Manual settings for very old devices */
581 ftl
->smallpagenand
= 0;
583 switch (size_in_megs
) {
585 /* 1 MiB flash/rom SmartMedia card (256 byte pages)*/
586 ftl
->zone_size
= 256;
588 ftl
->block_size
= 8 * SM_SECTOR_SIZE
;
589 ftl
->smallpagenand
= 1;
593 /* 2 MiB flash SmartMedia (256 byte pages)*/
594 if (mtd
->writesize
== SM_SMALL_PAGE
) {
595 ftl
->zone_size
= 512;
597 ftl
->block_size
= 8 * SM_SECTOR_SIZE
;
598 ftl
->smallpagenand
= 1;
599 /* 2 MiB rom SmartMedia */
605 ftl
->zone_size
= 256;
607 ftl
->block_size
= 16 * SM_SECTOR_SIZE
;
611 /* 4 MiB flash/rom SmartMedia device */
612 ftl
->zone_size
= 512;
614 ftl
->block_size
= 16 * SM_SECTOR_SIZE
;
617 /* 8 MiB flash/rom SmartMedia device */
618 ftl
->zone_size
= 1024;
620 ftl
->block_size
= 16 * SM_SECTOR_SIZE
;
623 /* Minimum xD size is 16MiB. Also, all xD cards have standard zone
624 sizes. SmartMedia cards exist up to 128 MiB and have same layout*/
625 if (size_in_megs
>= 16) {
626 ftl
->zone_count
= size_in_megs
/ 16;
627 ftl
->zone_size
= 1024;
629 ftl
->block_size
= 32 * SM_SECTOR_SIZE
;
632 /* Test for proper write,erase and oob sizes */
633 if (mtd
->erasesize
> ftl
->block_size
)
636 if (mtd
->writesize
> SM_SECTOR_SIZE
)
639 if (ftl
->smallpagenand
&& mtd
->oobsize
< SM_SMALL_OOB_SIZE
)
642 if (!ftl
->smallpagenand
&& mtd
->oobsize
< SM_OOB_SIZE
)
646 if (!mtd_has_oob(mtd
))
649 /* Find geometry information */
650 for (i
= 0 ; i
< ARRAY_SIZE(chs_table
) ; i
++) {
651 if (chs_table
[i
].size
== size_in_megs
) {
652 ftl
->cylinders
= chs_table
[i
].cyl
;
653 ftl
->heads
= chs_table
[i
].head
;
654 ftl
->sectors
= chs_table
[i
].sec
;
659 sm_printk("media has unknown size : %dMiB", size_in_megs
);
660 ftl
->cylinders
= 985;
666 /* Validate the CIS */
667 static int sm_read_cis(struct sm_ftl
*ftl
)
671 if (sm_read_sector(ftl
,
672 0, ftl
->cis_block
, ftl
->cis_boffset
, ftl
->cis_buffer
, &oob
))
675 if (!sm_sector_valid(&oob
) || !sm_block_valid(&oob
))
678 if (!memcmp(ftl
->cis_buffer
+ ftl
->cis_page_offset
,
679 cis_signature
, sizeof(cis_signature
))) {
686 /* Scan the media for the CIS */
687 static int sm_find_cis(struct sm_ftl
*ftl
)
694 /* Search for first valid block */
695 for (block
= 0 ; block
< ftl
->zone_size
- ftl
->max_lba
; block
++) {
697 if (sm_read_sector(ftl
, 0, block
, 0, NULL
, &oob
))
700 if (!sm_block_valid(&oob
))
709 /* Search for first valid sector in this block */
710 for (boffset
= 0 ; boffset
< ftl
->block_size
;
711 boffset
+= SM_SECTOR_SIZE
) {
713 if (sm_read_sector(ftl
, 0, block
, boffset
, NULL
, &oob
))
716 if (!sm_sector_valid(&oob
))
721 if (boffset
== ftl
->block_size
)
724 ftl
->cis_block
= block
;
725 ftl
->cis_boffset
= boffset
;
726 ftl
->cis_page_offset
= 0;
728 cis_found
= !sm_read_cis(ftl
);
731 ftl
->cis_page_offset
= SM_SMALL_PAGE
;
732 cis_found
= !sm_read_cis(ftl
);
736 dbg("CIS block found at offset %x",
737 block
* ftl
->block_size
+
738 boffset
+ ftl
->cis_page_offset
);
744 /* Basic test to determine if underlying mtd device if functional */
745 static int sm_recheck_media(struct sm_ftl
*ftl
)
747 if (sm_read_cis(ftl
)) {
749 if (!ftl
->unstable
) {
750 sm_printk("media unstable, not allowing writes");
758 /* Initialize a FTL zone */
759 static int sm_init_zone(struct sm_ftl
*ftl
, int zone_num
)
761 struct ftl_zone
*zone
= &ftl
->zones
[zone_num
];
768 dbg("initializing zone %d", zone_num
);
770 /* Allocate memory for FTL table */
771 zone
->lba_to_phys_table
= kmalloc(ftl
->max_lba
* 2, GFP_KERNEL
);
773 if (!zone
->lba_to_phys_table
)
775 memset(zone
->lba_to_phys_table
, -1, ftl
->max_lba
* 2);
778 /* Allocate memory for free sectors FIFO */
779 if (kfifo_alloc(&zone
->free_sectors
, ftl
->zone_size
* 2, GFP_KERNEL
)) {
780 kfree(zone
->lba_to_phys_table
);
784 /* Now scan the zone */
785 for (block
= 0 ; block
< ftl
->zone_size
; block
++) {
787 /* Skip blocks till the CIS (including) */
788 if (zone_num
== 0 && block
<= ftl
->cis_block
)
791 /* Read the oob of first sector */
792 if (sm_read_sector(ftl
, zone_num
, block
, 0, NULL
, &oob
))
795 /* Test to see if block is erased. It is enough to test
796 first sector, because erase happens in one shot */
797 if (sm_block_erased(&oob
)) {
798 kfifo_in(&zone
->free_sectors
,
799 (unsigned char *)&block
, 2);
803 /* If block is marked as bad, skip it */
804 /* This assumes we can trust first sector*/
805 /* However the way the block valid status is defined, ensures
806 very low probability of failure here */
807 if (!sm_block_valid(&oob
)) {
808 dbg("PH %04d <-> <marked bad>", block
);
813 lba
= sm_read_lba(&oob
);
815 /* Invalid LBA means that block is damaged. */
816 /* We can try to erase it, or mark it as bad, but
817 lets leave that to recovery application */
818 if (lba
== -2 || lba
>= ftl
->max_lba
) {
819 dbg("PH %04d <-> LBA %04d(bad)", block
, lba
);
824 /* If there is no collision,
825 just put the sector in the FTL table */
826 if (zone
->lba_to_phys_table
[lba
] < 0) {
827 dbg_verbose("PH %04d <-> LBA %04d", block
, lba
);
828 zone
->lba_to_phys_table
[lba
] = block
;
832 sm_printk("collision"
833 " of LBA %d between blocks %d and %d in zone %d",
834 lba
, zone
->lba_to_phys_table
[lba
], block
, zone_num
);
836 /* Test that this block is valid*/
837 if (sm_check_block(ftl
, zone_num
, block
))
840 /* Test now the old block */
841 if (sm_check_block(ftl
, zone_num
,
842 zone
->lba_to_phys_table
[lba
])) {
843 zone
->lba_to_phys_table
[lba
] = block
;
847 /* If both blocks are valid and share same LBA, it means that
848 they hold different versions of same data. It not
849 known which is more recent, thus just erase one of them
851 sm_printk("both blocks are valid, erasing the later");
852 sm_erase_block(ftl
, zone_num
, block
, 1);
855 dbg("zone initialized");
856 zone
->initialized
= 1;
858 /* No free sectors, means that the zone is heavily damaged, write won't
859 work, but it can still can be (partially) read */
860 if (!kfifo_len(&zone
->free_sectors
)) {
861 sm_printk("no free blocks in zone %d", zone_num
);
865 /* Randomize first block we write to */
866 get_random_bytes(&i
, 2);
867 i
%= (kfifo_len(&zone
->free_sectors
) / 2);
870 len
= kfifo_out(&zone
->free_sectors
,
871 (unsigned char *)&block
, 2);
873 kfifo_in(&zone
->free_sectors
, (const unsigned char *)&block
, 2);
878 /* Get and automatically initialize an FTL mapping for one zone */
879 static struct ftl_zone
*sm_get_zone(struct sm_ftl
*ftl
, int zone_num
)
881 struct ftl_zone
*zone
;
884 BUG_ON(zone_num
>= ftl
->zone_count
);
885 zone
= &ftl
->zones
[zone_num
];
887 if (!zone
->initialized
) {
888 error
= sm_init_zone(ftl
, zone_num
);
891 return ERR_PTR(error
);
897 /* ----------------- cache handling ------------------------------------------*/
899 /* Initialize the one block cache */
900 static void sm_cache_init(struct sm_ftl
*ftl
)
902 ftl
->cache_data_invalid_bitmap
= 0xFFFFFFFF;
903 ftl
->cache_clean
= 1;
904 ftl
->cache_zone
= -1;
905 ftl
->cache_block
= -1;
906 /*memset(ftl->cache_data, 0xAA, ftl->block_size);*/
909 /* Put sector in one block cache */
910 static void sm_cache_put(struct sm_ftl
*ftl
, char *buffer
, int boffset
)
912 memcpy(ftl
->cache_data
+ boffset
, buffer
, SM_SECTOR_SIZE
);
913 clear_bit(boffset
/ SM_SECTOR_SIZE
, &ftl
->cache_data_invalid_bitmap
);
914 ftl
->cache_clean
= 0;
917 /* Read a sector from the cache */
918 static int sm_cache_get(struct sm_ftl
*ftl
, char *buffer
, int boffset
)
920 if (test_bit(boffset
/ SM_SECTOR_SIZE
,
921 &ftl
->cache_data_invalid_bitmap
))
924 memcpy(buffer
, ftl
->cache_data
+ boffset
, SM_SECTOR_SIZE
);
928 /* Write the cache to hardware */
929 static int sm_cache_flush(struct sm_ftl
*ftl
)
931 struct ftl_zone
*zone
;
934 uint16_t write_sector
;
935 int zone_num
= ftl
->cache_zone
;
938 if (ftl
->cache_clean
)
944 BUG_ON(zone_num
< 0);
945 zone
= &ftl
->zones
[zone_num
];
946 block_num
= zone
->lba_to_phys_table
[ftl
->cache_block
];
949 /* Try to read all unread areas of the cache block*/
950 for_each_set_bit(sector_num
, &ftl
->cache_data_invalid_bitmap
,
951 ftl
->block_size
/ SM_SECTOR_SIZE
) {
953 if (!sm_read_sector(ftl
,
954 zone_num
, block_num
, sector_num
* SM_SECTOR_SIZE
,
955 ftl
->cache_data
+ sector_num
* SM_SECTOR_SIZE
, NULL
))
956 clear_bit(sector_num
,
957 &ftl
->cache_data_invalid_bitmap
);
964 /* If there are no spare blocks, */
965 /* we could still continue by erasing/writing the current block,
966 but for such worn out media it doesn't worth the trouble,
968 if (kfifo_out(&zone
->free_sectors
,
969 (unsigned char *)&write_sector
, 2) != 2) {
970 dbg("no free sectors for write!");
975 if (sm_write_block(ftl
, ftl
->cache_data
, zone_num
, write_sector
,
976 ftl
->cache_block
, ftl
->cache_data_invalid_bitmap
))
979 /* Update the FTL table */
980 zone
->lba_to_phys_table
[ftl
->cache_block
] = write_sector
;
982 /* Write succesfull, so erase and free the old block */
984 sm_erase_block(ftl
, zone_num
, block_num
, 1);
991 /* flush timer, runs a second after last write */
992 static void sm_cache_flush_timer(unsigned long data
)
994 struct sm_ftl
*ftl
= (struct sm_ftl
*)data
;
995 queue_work(cache_flush_workqueue
, &ftl
->flush_work
);
998 /* cache flush work, kicked by timer */
999 static void sm_cache_flush_work(struct work_struct
*work
)
1001 struct sm_ftl
*ftl
= container_of(work
, struct sm_ftl
, flush_work
);
1002 mutex_lock(&ftl
->mutex
);
1003 sm_cache_flush(ftl
);
1004 mutex_unlock(&ftl
->mutex
);
1008 /* ---------------- outside interface -------------------------------------- */
1010 /* outside interface: read a sector */
1011 static int sm_read(struct mtd_blktrans_dev
*dev
,
1012 unsigned long sect_no
, char *buf
)
1014 struct sm_ftl
*ftl
= dev
->priv
;
1015 struct ftl_zone
*zone
;
1016 int error
= 0, in_cache
= 0;
1017 int zone_num
, block
, boffset
;
1019 sm_break_offset(ftl
, sect_no
<< 9, &zone_num
, &block
, &boffset
);
1020 mutex_lock(&ftl
->mutex
);
1023 zone
= sm_get_zone(ftl
, zone_num
);
1025 error
= PTR_ERR(zone
);
1029 /* Have to look at cache first */
1030 if (ftl
->cache_zone
== zone_num
&& ftl
->cache_block
== block
) {
1032 if (!sm_cache_get(ftl
, buf
, boffset
))
1036 /* Translate the block and return if doesn't exist in the table */
1037 block
= zone
->lba_to_phys_table
[block
];
1040 memset(buf
, 0xFF, SM_SECTOR_SIZE
);
1044 if (sm_read_sector(ftl
, zone_num
, block
, boffset
, buf
, NULL
)) {
1050 sm_cache_put(ftl
, buf
, boffset
);
1052 mutex_unlock(&ftl
->mutex
);
1056 /* outside interface: write a sector */
1057 static int sm_write(struct mtd_blktrans_dev
*dev
,
1058 unsigned long sec_no
, char *buf
)
1060 struct sm_ftl
*ftl
= dev
->priv
;
1061 struct ftl_zone
*zone
;
1062 int error
= 0, zone_num
, block
, boffset
;
1064 BUG_ON(ftl
->readonly
);
1065 sm_break_offset(ftl
, sec_no
<< 9, &zone_num
, &block
, &boffset
);
1067 /* No need in flush thread running now */
1068 del_timer(&ftl
->timer
);
1069 mutex_lock(&ftl
->mutex
);
1071 zone
= sm_get_zone(ftl
, zone_num
);
1073 error
= PTR_ERR(zone
);
1077 /* If entry is not in cache, flush it */
1078 if (ftl
->cache_block
!= block
|| ftl
->cache_zone
!= zone_num
) {
1080 error
= sm_cache_flush(ftl
);
1084 ftl
->cache_block
= block
;
1085 ftl
->cache_zone
= zone_num
;
1088 sm_cache_put(ftl
, buf
, boffset
);
1090 mod_timer(&ftl
->timer
, jiffies
+ msecs_to_jiffies(cache_timeout
));
1091 mutex_unlock(&ftl
->mutex
);
1095 /* outside interface: flush everything */
1096 static int sm_flush(struct mtd_blktrans_dev
*dev
)
1098 struct sm_ftl
*ftl
= dev
->priv
;
1101 mutex_lock(&ftl
->mutex
);
1102 retval
= sm_cache_flush(ftl
);
1103 mutex_unlock(&ftl
->mutex
);
1107 /* outside interface: device is released */
1108 static void sm_release(struct mtd_blktrans_dev
*dev
)
1110 struct sm_ftl
*ftl
= dev
->priv
;
1112 mutex_lock(&ftl
->mutex
);
1113 del_timer_sync(&ftl
->timer
);
1114 cancel_work_sync(&ftl
->flush_work
);
1115 sm_cache_flush(ftl
);
1116 mutex_unlock(&ftl
->mutex
);
1119 /* outside interface: get geometry */
1120 static int sm_getgeo(struct mtd_blktrans_dev
*dev
, struct hd_geometry
*geo
)
1122 struct sm_ftl
*ftl
= dev
->priv
;
1123 geo
->heads
= ftl
->heads
;
1124 geo
->sectors
= ftl
->sectors
;
1125 geo
->cylinders
= ftl
->cylinders
;
1129 /* external interface: main initialization function */
1130 static void sm_add_mtd(struct mtd_blktrans_ops
*tr
, struct mtd_info
*mtd
)
1132 struct mtd_blktrans_dev
*trans
;
1135 /* Allocate & initialize our private structure */
1136 ftl
= kzalloc(sizeof(struct sm_ftl
), GFP_KERNEL
);
1141 mutex_init(&ftl
->mutex
);
1142 setup_timer(&ftl
->timer
, sm_cache_flush_timer
, (unsigned long)ftl
);
1143 INIT_WORK(&ftl
->flush_work
, sm_cache_flush_work
);
1144 init_completion(&ftl
->erase_completion
);
1146 /* Read media information */
1147 if (sm_get_media_info(ftl
, mtd
)) {
1148 dbg("found unsupported mtd device, aborting");
1153 /* Allocate temporary CIS buffer for read retry support */
1154 ftl
->cis_buffer
= kzalloc(SM_SECTOR_SIZE
, GFP_KERNEL
);
1155 if (!ftl
->cis_buffer
)
1158 /* Allocate zone array, it will be initialized on demand */
1159 ftl
->zones
= kzalloc(sizeof(struct ftl_zone
) * ftl
->zone_count
,
1164 /* Allocate the cache*/
1165 ftl
->cache_data
= kzalloc(ftl
->block_size
, GFP_KERNEL
);
1167 if (!ftl
->cache_data
)
1173 /* Allocate upper layer structure and initialize it */
1174 trans
= kzalloc(sizeof(struct mtd_blktrans_dev
), GFP_KERNEL
);
1184 trans
->size
= (ftl
->block_size
* ftl
->max_lba
* ftl
->zone_count
) >> 9;
1185 trans
->readonly
= ftl
->readonly
;
1187 if (sm_find_cis(ftl
)) {
1188 dbg("CIS not found on mtd device, aborting");
1192 ftl
->disk_attributes
= sm_create_sysfs_attributes(ftl
);
1193 if (!ftl
->disk_attributes
)
1195 trans
->disk_attributes
= ftl
->disk_attributes
;
1197 sm_printk("Found %d MiB xD/SmartMedia FTL on mtd%d",
1198 (int)(mtd
->size
/ (1024 * 1024)), mtd
->index
);
1201 dbg("%d zone(s), each consists of %d blocks (+%d spares)",
1202 ftl
->zone_count
, ftl
->max_lba
,
1203 ftl
->zone_size
- ftl
->max_lba
);
1204 dbg("each block consists of %d bytes",
1208 /* Register device*/
1209 if (add_mtd_blktrans_dev(trans
)) {
1210 dbg("error in mtdblktrans layer");
1217 kfree(ftl
->cache_data
);
1221 kfree(ftl
->cis_buffer
);
1228 /* main interface: device {surprise,} removal */
1229 static void sm_remove_dev(struct mtd_blktrans_dev
*dev
)
1231 struct sm_ftl
*ftl
= dev
->priv
;
1234 del_mtd_blktrans_dev(dev
);
1237 for (i
= 0 ; i
< ftl
->zone_count
; i
++) {
1239 if (!ftl
->zones
[i
].initialized
)
1242 kfree(ftl
->zones
[i
].lba_to_phys_table
);
1243 kfifo_free(&ftl
->zones
[i
].free_sectors
);
1246 sm_delete_sysfs_attributes(ftl
);
1247 kfree(ftl
->cis_buffer
);
1249 kfree(ftl
->cache_data
);
1253 static struct mtd_blktrans_ops sm_ftl_ops
= {
1256 .part_bits
= SM_FTL_PARTN_BITS
,
1257 .blksize
= SM_SECTOR_SIZE
,
1258 .getgeo
= sm_getgeo
,
1260 .add_mtd
= sm_add_mtd
,
1261 .remove_dev
= sm_remove_dev
,
1263 .readsect
= sm_read
,
1264 .writesect
= sm_write
,
1267 .release
= sm_release
,
1269 .owner
= THIS_MODULE
,
1272 static __init
int sm_module_init(void)
1276 cache_flush_workqueue
= create_freezable_workqueue("smflush");
1277 if (!cache_flush_workqueue
)
1280 error
= register_mtd_blktrans(&sm_ftl_ops
);
1282 destroy_workqueue(cache_flush_workqueue
);
1287 static void __exit
sm_module_exit(void)
1289 destroy_workqueue(cache_flush_workqueue
);
1290 deregister_mtd_blktrans(&sm_ftl_ops
);
1293 module_init(sm_module_init
);
1294 module_exit(sm_module_exit
);
1296 MODULE_LICENSE("GPL");
1297 MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
1298 MODULE_DESCRIPTION("Smartmedia/xD mtd translation layer");