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 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 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 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
;
63 int vendor_len
= strnlen(ftl
->cis_buffer
+ SM_CIS_VENDOR_OFFSET
,
64 SM_SMALL_PAGE
- SM_CIS_VENDOR_OFFSET
);
66 char *vendor
= kmalloc(vendor_len
, GFP_KERNEL
);
69 memcpy(vendor
, ftl
->cis_buffer
+ SM_CIS_VENDOR_OFFSET
, vendor_len
);
70 vendor
[vendor_len
] = 0;
72 /* Initialize sysfs attributes */
74 kzalloc(sizeof(struct sm_sysfs_attribute
), GFP_KERNEL
);
75 if (!vendor_attribute
)
78 sysfs_attr_init(&vendor_attribute
->dev_attr
.attr
);
80 vendor_attribute
->data
= vendor
;
81 vendor_attribute
->len
= vendor_len
;
82 vendor_attribute
->dev_attr
.attr
.name
= "vendor";
83 vendor_attribute
->dev_attr
.attr
.mode
= S_IRUGO
;
84 vendor_attribute
->dev_attr
.show
= sm_attr_show
;
87 /* Create array of pointers to the attributes */
88 attributes
= kzalloc(sizeof(struct attribute
*) * (NUM_ATTRIBUTES
+ 1),
92 attributes
[0] = &vendor_attribute
->dev_attr
.attr
;
94 /* Finally create the attribute group */
95 attr_group
= kzalloc(sizeof(struct attribute_group
), GFP_KERNEL
);
98 attr_group
->attrs
= attributes
;
103 kfree(vendor_attribute
);
110 void sm_delete_sysfs_attributes(struct sm_ftl
*ftl
)
112 struct attribute
**attributes
= ftl
->disk_attributes
->attrs
;
115 for (i
= 0; attributes
[i
] ; i
++) {
117 struct device_attribute
*dev_attr
= container_of(attributes
[i
],
118 struct device_attribute
, attr
);
120 struct sm_sysfs_attribute
*sm_attr
=
121 container_of(dev_attr
,
122 struct sm_sysfs_attribute
, dev_attr
);
124 kfree(sm_attr
->data
);
128 kfree(ftl
->disk_attributes
->attrs
);
129 kfree(ftl
->disk_attributes
);
133 /* ----------------------- oob helpers -------------------------------------- */
135 static int sm_get_lba(uint8_t *lba
)
137 /* check fixed bits */
138 if ((lba
[0] & 0xF8) != 0x10)
141 /* check parity - endianness doesn't matter */
142 if (hweight16(*(uint16_t *)lba
) & 1)
145 return (lba
[1] >> 1) | ((lba
[0] & 0x07) << 7);
150 * Read LBA associated with block
151 * returns -1, if block is erased
152 * returns -2 if error happens
154 static int sm_read_lba(struct sm_oob
*oob
)
156 static const uint32_t erased_pattern
[4] = {
157 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
162 /* First test for erased block */
163 if (!memcmp(oob
, erased_pattern
, SM_OOB_SIZE
))
166 /* Now check is both copies of the LBA differ too much */
167 lba_test
= *(uint16_t *)oob
->lba_copy1
^ *(uint16_t*)oob
->lba_copy2
;
168 if (lba_test
&& !is_power_of_2(lba_test
))
172 lba
= sm_get_lba(oob
->lba_copy1
);
175 lba
= sm_get_lba(oob
->lba_copy2
);
180 static void sm_write_lba(struct sm_oob
*oob
, uint16_t lba
)
184 WARN_ON(lba
>= 1000);
186 tmp
[0] = 0x10 | ((lba
>> 7) & 0x07);
187 tmp
[1] = (lba
<< 1) & 0xFF;
189 if (hweight16(*(uint16_t *)tmp
) & 0x01)
192 oob
->lba_copy1
[0] = oob
->lba_copy2
[0] = tmp
[0];
193 oob
->lba_copy1
[1] = oob
->lba_copy2
[1] = tmp
[1];
197 /* Make offset from parts */
198 static loff_t
sm_mkoffset(struct sm_ftl
*ftl
, int zone
, int block
, int boffset
)
200 WARN_ON(boffset
& (SM_SECTOR_SIZE
- 1));
201 WARN_ON(zone
< 0 || zone
>= ftl
->zone_count
);
202 WARN_ON(block
>= ftl
->zone_size
);
203 WARN_ON(boffset
>= ftl
->block_size
);
208 return (zone
* SM_MAX_ZONE_SIZE
+ block
) * ftl
->block_size
+ boffset
;
211 /* Breaks offset into parts */
212 static void sm_break_offset(struct sm_ftl
*ftl
, loff_t offset
,
213 int *zone
, int *block
, int *boffset
)
215 *boffset
= do_div(offset
, ftl
->block_size
);
216 *block
= do_div(offset
, ftl
->max_lba
);
217 *zone
= offset
>= ftl
->zone_count
? -1 : offset
;
220 /* ---------------------- low level IO ------------------------------------- */
222 static int sm_correct_sector(uint8_t *buffer
, struct sm_oob
*oob
)
226 __nand_calculate_ecc(buffer
, SM_SMALL_PAGE
, ecc
);
227 if (__nand_correct_data(buffer
, ecc
, oob
->ecc1
, SM_SMALL_PAGE
) < 0)
230 buffer
+= SM_SMALL_PAGE
;
232 __nand_calculate_ecc(buffer
, SM_SMALL_PAGE
, ecc
);
233 if (__nand_correct_data(buffer
, ecc
, oob
->ecc2
, SM_SMALL_PAGE
) < 0)
238 /* Reads a sector + oob*/
239 static int sm_read_sector(struct sm_ftl
*ftl
,
240 int zone
, int block
, int boffset
,
241 uint8_t *buffer
, struct sm_oob
*oob
)
243 struct mtd_info
*mtd
= ftl
->trans
->mtd
;
244 struct mtd_oob_ops ops
;
245 struct sm_oob tmp_oob
;
249 /* FTL can contain -1 entries that are by default filled with bits */
251 memset(buffer
, 0xFF, SM_SECTOR_SIZE
);
255 /* User might not need the oob, but we do for data verification */
259 ops
.mode
= ftl
->smallpagenand
? MTD_OPS_RAW
: MTD_OPS_PLACE_OOB
;
261 ops
.ooblen
= SM_OOB_SIZE
;
262 ops
.oobbuf
= (void *)oob
;
263 ops
.len
= SM_SECTOR_SIZE
;
268 /* Avoid infinite recursion on CIS reads, sm_recheck_media
270 if (zone
== 0 && block
== ftl
->cis_block
&& boffset
==
274 /* Test if media is stable */
275 if (try == 3 || sm_recheck_media(ftl
))
279 /* Unfortunately, oob read will _always_ succeed,
280 despite card removal..... */
281 ret
= mtd_read_oob(mtd
, sm_mkoffset(ftl
, zone
, block
, boffset
), &ops
);
283 /* Test for unknown errors */
284 if (ret
!= 0 && !mtd_is_bitflip_or_eccerr(ret
)) {
285 dbg("read of block %d at zone %d, failed due to error (%d)",
290 /* Do a basic test on the oob, to guard against returned garbage */
291 if (oob
->reserved
!= 0xFFFFFFFF && !is_power_of_2(~oob
->reserved
))
294 /* This should never happen, unless there is a bug in the mtd driver */
295 WARN_ON(ops
.oobretlen
!= SM_OOB_SIZE
);
296 WARN_ON(buffer
&& ops
.retlen
!= SM_SECTOR_SIZE
);
301 /* Test if sector marked as bad */
302 if (!sm_sector_valid(oob
)) {
303 dbg("read of block %d at zone %d, failed because it is marked"
304 " as bad" , block
, zone
);
309 if (mtd_is_eccerr(ret
) ||
310 (ftl
->smallpagenand
&& sm_correct_sector(buffer
, oob
))) {
312 dbg("read of block %d at zone %d, failed due to ECC error",
320 /* Writes a sector to media */
321 static int sm_write_sector(struct sm_ftl
*ftl
,
322 int zone
, int block
, int boffset
,
323 uint8_t *buffer
, struct sm_oob
*oob
)
325 struct mtd_oob_ops ops
;
326 struct mtd_info
*mtd
= ftl
->trans
->mtd
;
329 BUG_ON(ftl
->readonly
);
331 if (zone
== 0 && (block
== ftl
->cis_block
|| block
== 0)) {
332 dbg("attempted to write the CIS!");
339 ops
.mode
= ftl
->smallpagenand
? MTD_OPS_RAW
: MTD_OPS_PLACE_OOB
;
340 ops
.len
= SM_SECTOR_SIZE
;
343 ops
.ooblen
= SM_OOB_SIZE
;
344 ops
.oobbuf
= (void *)oob
;
346 ret
= mtd_write_oob(mtd
, sm_mkoffset(ftl
, zone
, block
, boffset
), &ops
);
348 /* Now we assume that hardware will catch write bitflip errors */
349 /* If you are paranoid, use CONFIG_MTD_NAND_VERIFY_WRITE */
352 dbg("write to block %d at zone %d, failed with error %d",
355 sm_recheck_media(ftl
);
359 /* This should never happen, unless there is a bug in the driver */
360 WARN_ON(ops
.oobretlen
!= SM_OOB_SIZE
);
361 WARN_ON(buffer
&& ops
.retlen
!= SM_SECTOR_SIZE
);
366 /* ------------------------ block IO ------------------------------------- */
368 /* Write a block using data and lba, and invalid sector bitmap */
369 static int sm_write_block(struct sm_ftl
*ftl
, uint8_t *buf
,
370 int zone
, int block
, int lba
,
371 unsigned long invalid_bitmap
)
377 /* Initialize the oob with requested values */
378 memset(&oob
, 0xFF, SM_OOB_SIZE
);
379 sm_write_lba(&oob
, lba
);
384 for (boffset
= 0; boffset
< ftl
->block_size
;
385 boffset
+= SM_SECTOR_SIZE
) {
387 oob
.data_status
= 0xFF;
389 if (test_bit(boffset
/ SM_SECTOR_SIZE
, &invalid_bitmap
)) {
391 sm_printk("sector %d of block at LBA %d of zone %d"
392 " coudn't be read, marking it as invalid",
393 boffset
/ SM_SECTOR_SIZE
, lba
, zone
);
398 if (ftl
->smallpagenand
) {
399 __nand_calculate_ecc(buf
+ boffset
,
400 SM_SMALL_PAGE
, oob
.ecc1
);
402 __nand_calculate_ecc(buf
+ boffset
+ SM_SMALL_PAGE
,
403 SM_SMALL_PAGE
, oob
.ecc2
);
405 if (!sm_write_sector(ftl
, zone
, block
, boffset
,
406 buf
+ boffset
, &oob
))
411 /* If write fails. try to erase the block */
412 /* This is safe, because we never write in blocks
413 that contain valuable data.
414 This is intended to repair block that are marked
415 as erased, but that isn't fully erased*/
417 if (sm_erase_block(ftl
, zone
, block
, 0))
423 sm_mark_block_bad(ftl
, zone
, block
);
431 /* Mark whole block at offset 'offs' as bad. */
432 static void sm_mark_block_bad(struct sm_ftl
*ftl
, int zone
, int block
)
437 memset(&oob
, 0xFF, SM_OOB_SIZE
);
438 oob
.block_status
= 0xF0;
443 if (sm_recheck_media(ftl
))
446 sm_printk("marking block %d of zone %d as bad", block
, zone
);
448 /* We aren't checking the return value, because we don't care */
449 /* This also fails on fake xD cards, but I guess these won't expose
450 any bad blocks till fail completely */
451 for (boffset
= 0; boffset
< ftl
->block_size
; boffset
+= SM_SECTOR_SIZE
)
452 sm_write_sector(ftl
, zone
, block
, boffset
, NULL
, &oob
);
456 * Erase a block within a zone
457 * If erase succeeds, it updates free block fifo, otherwise marks block as bad
459 static int sm_erase_block(struct sm_ftl
*ftl
, int zone_num
, uint16_t block
,
462 struct ftl_zone
*zone
= &ftl
->zones
[zone_num
];
463 struct mtd_info
*mtd
= ftl
->trans
->mtd
;
464 struct erase_info erase
;
467 erase
.callback
= sm_erase_callback
;
468 erase
.addr
= sm_mkoffset(ftl
, zone_num
, block
, 0);
469 erase
.len
= ftl
->block_size
;
470 erase
.priv
= (u_long
)ftl
;
475 BUG_ON(ftl
->readonly
);
477 if (zone_num
== 0 && (block
== ftl
->cis_block
|| block
== 0)) {
478 sm_printk("attempted to erase the CIS!");
482 if (mtd_erase(mtd
, &erase
)) {
483 sm_printk("erase of block %d in zone %d failed",
488 if (erase
.state
== MTD_ERASE_PENDING
)
489 wait_for_completion(&ftl
->erase_completion
);
491 if (erase
.state
!= MTD_ERASE_DONE
) {
492 sm_printk("erase of block %d in zone %d failed after wait",
498 kfifo_in(&zone
->free_sectors
,
499 (const unsigned char *)&block
, sizeof(block
));
503 sm_mark_block_bad(ftl
, zone_num
, block
);
507 static void sm_erase_callback(struct erase_info
*self
)
509 struct sm_ftl
*ftl
= (struct sm_ftl
*)self
->priv
;
510 complete(&ftl
->erase_completion
);
513 /* Thoroughly test that block is valid. */
514 static int sm_check_block(struct sm_ftl
*ftl
, int zone
, int block
)
518 int lbas
[] = { -3, 0, 0, 0 };
523 /* First just check that block doesn't look fishy */
524 /* Only blocks that are valid or are sliced in two parts, are
526 for (boffset
= 0; boffset
< ftl
->block_size
;
527 boffset
+= SM_SECTOR_SIZE
) {
529 /* This shouldn't happen anyway */
530 if (sm_read_sector(ftl
, zone
, block
, boffset
, NULL
, &oob
))
533 test_lba
= sm_read_lba(&oob
);
535 if (lbas
[i
] != test_lba
)
536 lbas
[++i
] = test_lba
;
538 /* If we found three different LBAs, something is fishy */
543 /* If the block is sliced (partially erased usually) erase it */
545 sm_erase_block(ftl
, zone
, block
, 1);
552 /* ----------------- media scanning --------------------------------- */
553 static const struct chs_entry chs_table
[] = {
561 { 128, 500, 16, 32 },
562 { 256, 1000, 16, 32 },
563 { 512, 1015, 32, 63 },
564 { 1024, 985, 33, 63 },
565 { 2048, 985, 33, 63 },
570 static const uint8_t cis_signature
[] = {
571 0x01, 0x03, 0xD9, 0x01, 0xFF, 0x18, 0x02, 0xDF, 0x01, 0x20
573 /* Find out media parameters.
574 * This ideally has to be based on nand id, but for now device size is enough */
575 int sm_get_media_info(struct sm_ftl
*ftl
, struct mtd_info
*mtd
)
578 int size_in_megs
= mtd
->size
/ (1024 * 1024);
580 ftl
->readonly
= mtd
->type
== MTD_ROM
;
582 /* Manual settings for very old devices */
584 ftl
->smallpagenand
= 0;
586 switch (size_in_megs
) {
588 /* 1 MiB flash/rom SmartMedia card (256 byte pages)*/
589 ftl
->zone_size
= 256;
591 ftl
->block_size
= 8 * SM_SECTOR_SIZE
;
592 ftl
->smallpagenand
= 1;
596 /* 2 MiB flash SmartMedia (256 byte pages)*/
597 if (mtd
->writesize
== SM_SMALL_PAGE
) {
598 ftl
->zone_size
= 512;
600 ftl
->block_size
= 8 * SM_SECTOR_SIZE
;
601 ftl
->smallpagenand
= 1;
602 /* 2 MiB rom SmartMedia */
608 ftl
->zone_size
= 256;
610 ftl
->block_size
= 16 * SM_SECTOR_SIZE
;
614 /* 4 MiB flash/rom SmartMedia device */
615 ftl
->zone_size
= 512;
617 ftl
->block_size
= 16 * SM_SECTOR_SIZE
;
620 /* 8 MiB flash/rom SmartMedia device */
621 ftl
->zone_size
= 1024;
623 ftl
->block_size
= 16 * SM_SECTOR_SIZE
;
626 /* Minimum xD size is 16MiB. Also, all xD cards have standard zone
627 sizes. SmartMedia cards exist up to 128 MiB and have same layout*/
628 if (size_in_megs
>= 16) {
629 ftl
->zone_count
= size_in_megs
/ 16;
630 ftl
->zone_size
= 1024;
632 ftl
->block_size
= 32 * SM_SECTOR_SIZE
;
635 /* Test for proper write,erase and oob sizes */
636 if (mtd
->erasesize
> ftl
->block_size
)
639 if (mtd
->writesize
> SM_SECTOR_SIZE
)
642 if (ftl
->smallpagenand
&& mtd
->oobsize
< SM_SMALL_OOB_SIZE
)
645 if (!ftl
->smallpagenand
&& mtd
->oobsize
< SM_OOB_SIZE
)
649 if (!mtd_has_oob(mtd
))
652 /* Find geometry information */
653 for (i
= 0 ; i
< ARRAY_SIZE(chs_table
) ; i
++) {
654 if (chs_table
[i
].size
== size_in_megs
) {
655 ftl
->cylinders
= chs_table
[i
].cyl
;
656 ftl
->heads
= chs_table
[i
].head
;
657 ftl
->sectors
= chs_table
[i
].sec
;
662 sm_printk("media has unknown size : %dMiB", size_in_megs
);
663 ftl
->cylinders
= 985;
669 /* Validate the CIS */
670 static int sm_read_cis(struct sm_ftl
*ftl
)
674 if (sm_read_sector(ftl
,
675 0, ftl
->cis_block
, ftl
->cis_boffset
, ftl
->cis_buffer
, &oob
))
678 if (!sm_sector_valid(&oob
) || !sm_block_valid(&oob
))
681 if (!memcmp(ftl
->cis_buffer
+ ftl
->cis_page_offset
,
682 cis_signature
, sizeof(cis_signature
))) {
689 /* Scan the media for the CIS */
690 static int sm_find_cis(struct sm_ftl
*ftl
)
697 /* Search for first valid block */
698 for (block
= 0 ; block
< ftl
->zone_size
- ftl
->max_lba
; block
++) {
700 if (sm_read_sector(ftl
, 0, block
, 0, NULL
, &oob
))
703 if (!sm_block_valid(&oob
))
712 /* Search for first valid sector in this block */
713 for (boffset
= 0 ; boffset
< ftl
->block_size
;
714 boffset
+= SM_SECTOR_SIZE
) {
716 if (sm_read_sector(ftl
, 0, block
, boffset
, NULL
, &oob
))
719 if (!sm_sector_valid(&oob
))
724 if (boffset
== ftl
->block_size
)
727 ftl
->cis_block
= block
;
728 ftl
->cis_boffset
= boffset
;
729 ftl
->cis_page_offset
= 0;
731 cis_found
= !sm_read_cis(ftl
);
734 ftl
->cis_page_offset
= SM_SMALL_PAGE
;
735 cis_found
= !sm_read_cis(ftl
);
739 dbg("CIS block found at offset %x",
740 block
* ftl
->block_size
+
741 boffset
+ ftl
->cis_page_offset
);
747 /* Basic test to determine if underlying mtd device if functional */
748 static int sm_recheck_media(struct sm_ftl
*ftl
)
750 if (sm_read_cis(ftl
)) {
752 if (!ftl
->unstable
) {
753 sm_printk("media unstable, not allowing writes");
761 /* Initialize a FTL zone */
762 static int sm_init_zone(struct sm_ftl
*ftl
, int zone_num
)
764 struct ftl_zone
*zone
= &ftl
->zones
[zone_num
];
771 dbg("initializing zone %d", zone_num
);
773 /* Allocate memory for FTL table */
774 zone
->lba_to_phys_table
= kmalloc(ftl
->max_lba
* 2, GFP_KERNEL
);
776 if (!zone
->lba_to_phys_table
)
778 memset(zone
->lba_to_phys_table
, -1, ftl
->max_lba
* 2);
781 /* Allocate memory for free sectors FIFO */
782 if (kfifo_alloc(&zone
->free_sectors
, ftl
->zone_size
* 2, GFP_KERNEL
)) {
783 kfree(zone
->lba_to_phys_table
);
787 /* Now scan the zone */
788 for (block
= 0 ; block
< ftl
->zone_size
; block
++) {
790 /* Skip blocks till the CIS (including) */
791 if (zone_num
== 0 && block
<= ftl
->cis_block
)
794 /* Read the oob of first sector */
795 if (sm_read_sector(ftl
, zone_num
, block
, 0, NULL
, &oob
))
798 /* Test to see if block is erased. It is enough to test
799 first sector, because erase happens in one shot */
800 if (sm_block_erased(&oob
)) {
801 kfifo_in(&zone
->free_sectors
,
802 (unsigned char *)&block
, 2);
806 /* If block is marked as bad, skip it */
807 /* This assumes we can trust first sector*/
808 /* However the way the block valid status is defined, ensures
809 very low probability of failure here */
810 if (!sm_block_valid(&oob
)) {
811 dbg("PH %04d <-> <marked bad>", block
);
816 lba
= sm_read_lba(&oob
);
818 /* Invalid LBA means that block is damaged. */
819 /* We can try to erase it, or mark it as bad, but
820 lets leave that to recovery application */
821 if (lba
== -2 || lba
>= ftl
->max_lba
) {
822 dbg("PH %04d <-> LBA %04d(bad)", block
, lba
);
827 /* If there is no collision,
828 just put the sector in the FTL table */
829 if (zone
->lba_to_phys_table
[lba
] < 0) {
830 dbg_verbose("PH %04d <-> LBA %04d", block
, lba
);
831 zone
->lba_to_phys_table
[lba
] = block
;
835 sm_printk("collision"
836 " of LBA %d between blocks %d and %d in zone %d",
837 lba
, zone
->lba_to_phys_table
[lba
], block
, zone_num
);
839 /* Test that this block is valid*/
840 if (sm_check_block(ftl
, zone_num
, block
))
843 /* Test now the old block */
844 if (sm_check_block(ftl
, zone_num
,
845 zone
->lba_to_phys_table
[lba
])) {
846 zone
->lba_to_phys_table
[lba
] = block
;
850 /* If both blocks are valid and share same LBA, it means that
851 they hold different versions of same data. It not
852 known which is more recent, thus just erase one of them
854 sm_printk("both blocks are valid, erasing the later");
855 sm_erase_block(ftl
, zone_num
, block
, 1);
858 dbg("zone initialized");
859 zone
->initialized
= 1;
861 /* No free sectors, means that the zone is heavily damaged, write won't
862 work, but it can still can be (partially) read */
863 if (!kfifo_len(&zone
->free_sectors
)) {
864 sm_printk("no free blocks in zone %d", zone_num
);
868 /* Randomize first block we write to */
869 get_random_bytes(&i
, 2);
870 i
%= (kfifo_len(&zone
->free_sectors
) / 2);
873 len
= kfifo_out(&zone
->free_sectors
,
874 (unsigned char *)&block
, 2);
876 kfifo_in(&zone
->free_sectors
, (const unsigned char *)&block
, 2);
881 /* Get and automatically initialize an FTL mapping for one zone */
882 struct ftl_zone
*sm_get_zone(struct sm_ftl
*ftl
, int zone_num
)
884 struct ftl_zone
*zone
;
887 BUG_ON(zone_num
>= ftl
->zone_count
);
888 zone
= &ftl
->zones
[zone_num
];
890 if (!zone
->initialized
) {
891 error
= sm_init_zone(ftl
, zone_num
);
894 return ERR_PTR(error
);
900 /* ----------------- cache handling ------------------------------------------*/
902 /* Initialize the one block cache */
903 void sm_cache_init(struct sm_ftl
*ftl
)
905 ftl
->cache_data_invalid_bitmap
= 0xFFFFFFFF;
906 ftl
->cache_clean
= 1;
907 ftl
->cache_zone
= -1;
908 ftl
->cache_block
= -1;
909 /*memset(ftl->cache_data, 0xAA, ftl->block_size);*/
912 /* Put sector in one block cache */
913 void sm_cache_put(struct sm_ftl
*ftl
, char *buffer
, int boffset
)
915 memcpy(ftl
->cache_data
+ boffset
, buffer
, SM_SECTOR_SIZE
);
916 clear_bit(boffset
/ SM_SECTOR_SIZE
, &ftl
->cache_data_invalid_bitmap
);
917 ftl
->cache_clean
= 0;
920 /* Read a sector from the cache */
921 int sm_cache_get(struct sm_ftl
*ftl
, char *buffer
, int boffset
)
923 if (test_bit(boffset
/ SM_SECTOR_SIZE
,
924 &ftl
->cache_data_invalid_bitmap
))
927 memcpy(buffer
, ftl
->cache_data
+ boffset
, SM_SECTOR_SIZE
);
931 /* Write the cache to hardware */
932 int sm_cache_flush(struct sm_ftl
*ftl
)
934 struct ftl_zone
*zone
;
937 uint16_t write_sector
;
938 int zone_num
= ftl
->cache_zone
;
941 if (ftl
->cache_clean
)
947 BUG_ON(zone_num
< 0);
948 zone
= &ftl
->zones
[zone_num
];
949 block_num
= zone
->lba_to_phys_table
[ftl
->cache_block
];
952 /* Try to read all unread areas of the cache block*/
953 for_each_set_bit(sector_num
, &ftl
->cache_data_invalid_bitmap
,
954 ftl
->block_size
/ SM_SECTOR_SIZE
) {
956 if (!sm_read_sector(ftl
,
957 zone_num
, block_num
, sector_num
* SM_SECTOR_SIZE
,
958 ftl
->cache_data
+ sector_num
* SM_SECTOR_SIZE
, NULL
))
959 clear_bit(sector_num
,
960 &ftl
->cache_data_invalid_bitmap
);
967 /* If there are no spare blocks, */
968 /* we could still continue by erasing/writing the current block,
969 but for such worn out media it doesn't worth the trouble,
971 if (kfifo_out(&zone
->free_sectors
,
972 (unsigned char *)&write_sector
, 2) != 2) {
973 dbg("no free sectors for write!");
978 if (sm_write_block(ftl
, ftl
->cache_data
, zone_num
, write_sector
,
979 ftl
->cache_block
, ftl
->cache_data_invalid_bitmap
))
982 /* Update the FTL table */
983 zone
->lba_to_phys_table
[ftl
->cache_block
] = write_sector
;
985 /* Write succesfull, so erase and free the old block */
987 sm_erase_block(ftl
, zone_num
, block_num
, 1);
994 /* flush timer, runs a second after last write */
995 static void sm_cache_flush_timer(unsigned long data
)
997 struct sm_ftl
*ftl
= (struct sm_ftl
*)data
;
998 queue_work(cache_flush_workqueue
, &ftl
->flush_work
);
1001 /* cache flush work, kicked by timer */
1002 static void sm_cache_flush_work(struct work_struct
*work
)
1004 struct sm_ftl
*ftl
= container_of(work
, struct sm_ftl
, flush_work
);
1005 mutex_lock(&ftl
->mutex
);
1006 sm_cache_flush(ftl
);
1007 mutex_unlock(&ftl
->mutex
);
1011 /* ---------------- outside interface -------------------------------------- */
1013 /* outside interface: read a sector */
1014 static int sm_read(struct mtd_blktrans_dev
*dev
,
1015 unsigned long sect_no
, char *buf
)
1017 struct sm_ftl
*ftl
= dev
->priv
;
1018 struct ftl_zone
*zone
;
1019 int error
= 0, in_cache
= 0;
1020 int zone_num
, block
, boffset
;
1022 sm_break_offset(ftl
, sect_no
<< 9, &zone_num
, &block
, &boffset
);
1023 mutex_lock(&ftl
->mutex
);
1026 zone
= sm_get_zone(ftl
, zone_num
);
1028 error
= PTR_ERR(zone
);
1032 /* Have to look at cache first */
1033 if (ftl
->cache_zone
== zone_num
&& ftl
->cache_block
== block
) {
1035 if (!sm_cache_get(ftl
, buf
, boffset
))
1039 /* Translate the block and return if doesn't exist in the table */
1040 block
= zone
->lba_to_phys_table
[block
];
1043 memset(buf
, 0xFF, SM_SECTOR_SIZE
);
1047 if (sm_read_sector(ftl
, zone_num
, block
, boffset
, buf
, NULL
)) {
1053 sm_cache_put(ftl
, buf
, boffset
);
1055 mutex_unlock(&ftl
->mutex
);
1059 /* outside interface: write a sector */
1060 static int sm_write(struct mtd_blktrans_dev
*dev
,
1061 unsigned long sec_no
, char *buf
)
1063 struct sm_ftl
*ftl
= dev
->priv
;
1064 struct ftl_zone
*zone
;
1065 int error
, zone_num
, block
, boffset
;
1067 BUG_ON(ftl
->readonly
);
1068 sm_break_offset(ftl
, sec_no
<< 9, &zone_num
, &block
, &boffset
);
1070 /* No need in flush thread running now */
1071 del_timer(&ftl
->timer
);
1072 mutex_lock(&ftl
->mutex
);
1074 zone
= sm_get_zone(ftl
, zone_num
);
1076 error
= PTR_ERR(zone
);
1080 /* If entry is not in cache, flush it */
1081 if (ftl
->cache_block
!= block
|| ftl
->cache_zone
!= zone_num
) {
1083 error
= sm_cache_flush(ftl
);
1087 ftl
->cache_block
= block
;
1088 ftl
->cache_zone
= zone_num
;
1091 sm_cache_put(ftl
, buf
, boffset
);
1093 mod_timer(&ftl
->timer
, jiffies
+ msecs_to_jiffies(cache_timeout
));
1094 mutex_unlock(&ftl
->mutex
);
1098 /* outside interface: flush everything */
1099 static int sm_flush(struct mtd_blktrans_dev
*dev
)
1101 struct sm_ftl
*ftl
= dev
->priv
;
1104 mutex_lock(&ftl
->mutex
);
1105 retval
= sm_cache_flush(ftl
);
1106 mutex_unlock(&ftl
->mutex
);
1110 /* outside interface: device is released */
1111 static int sm_release(struct mtd_blktrans_dev
*dev
)
1113 struct sm_ftl
*ftl
= dev
->priv
;
1115 mutex_lock(&ftl
->mutex
);
1116 del_timer_sync(&ftl
->timer
);
1117 cancel_work_sync(&ftl
->flush_work
);
1118 sm_cache_flush(ftl
);
1119 mutex_unlock(&ftl
->mutex
);
1123 /* outside interface: get geometry */
1124 static int sm_getgeo(struct mtd_blktrans_dev
*dev
, struct hd_geometry
*geo
)
1126 struct sm_ftl
*ftl
= dev
->priv
;
1127 geo
->heads
= ftl
->heads
;
1128 geo
->sectors
= ftl
->sectors
;
1129 geo
->cylinders
= ftl
->cylinders
;
1133 /* external interface: main initialization function */
1134 static void sm_add_mtd(struct mtd_blktrans_ops
*tr
, struct mtd_info
*mtd
)
1136 struct mtd_blktrans_dev
*trans
;
1139 /* Allocate & initialize our private structure */
1140 ftl
= kzalloc(sizeof(struct sm_ftl
), GFP_KERNEL
);
1145 mutex_init(&ftl
->mutex
);
1146 setup_timer(&ftl
->timer
, sm_cache_flush_timer
, (unsigned long)ftl
);
1147 INIT_WORK(&ftl
->flush_work
, sm_cache_flush_work
);
1148 init_completion(&ftl
->erase_completion
);
1150 /* Read media information */
1151 if (sm_get_media_info(ftl
, mtd
)) {
1152 dbg("found unsupported mtd device, aborting");
1157 /* Allocate temporary CIS buffer for read retry support */
1158 ftl
->cis_buffer
= kzalloc(SM_SECTOR_SIZE
, GFP_KERNEL
);
1159 if (!ftl
->cis_buffer
)
1162 /* Allocate zone array, it will be initialized on demand */
1163 ftl
->zones
= kzalloc(sizeof(struct ftl_zone
) * ftl
->zone_count
,
1168 /* Allocate the cache*/
1169 ftl
->cache_data
= kzalloc(ftl
->block_size
, GFP_KERNEL
);
1171 if (!ftl
->cache_data
)
1177 /* Allocate upper layer structure and initialize it */
1178 trans
= kzalloc(sizeof(struct mtd_blktrans_dev
), GFP_KERNEL
);
1188 trans
->size
= (ftl
->block_size
* ftl
->max_lba
* ftl
->zone_count
) >> 9;
1189 trans
->readonly
= ftl
->readonly
;
1191 if (sm_find_cis(ftl
)) {
1192 dbg("CIS not found on mtd device, aborting");
1196 ftl
->disk_attributes
= sm_create_sysfs_attributes(ftl
);
1197 if (!ftl
->disk_attributes
)
1199 trans
->disk_attributes
= ftl
->disk_attributes
;
1201 sm_printk("Found %d MiB xD/SmartMedia FTL on mtd%d",
1202 (int)(mtd
->size
/ (1024 * 1024)), mtd
->index
);
1205 dbg("%d zone(s), each consists of %d blocks (+%d spares)",
1206 ftl
->zone_count
, ftl
->max_lba
,
1207 ftl
->zone_size
- ftl
->max_lba
);
1208 dbg("each block consists of %d bytes",
1212 /* Register device*/
1213 if (add_mtd_blktrans_dev(trans
)) {
1214 dbg("error in mtdblktrans layer");
1221 kfree(ftl
->cache_data
);
1225 kfree(ftl
->cis_buffer
);
1232 /* main interface: device {surprise,} removal */
1233 static void sm_remove_dev(struct mtd_blktrans_dev
*dev
)
1235 struct sm_ftl
*ftl
= dev
->priv
;
1238 del_mtd_blktrans_dev(dev
);
1241 for (i
= 0 ; i
< ftl
->zone_count
; i
++) {
1243 if (!ftl
->zones
[i
].initialized
)
1246 kfree(ftl
->zones
[i
].lba_to_phys_table
);
1247 kfifo_free(&ftl
->zones
[i
].free_sectors
);
1250 sm_delete_sysfs_attributes(ftl
);
1251 kfree(ftl
->cis_buffer
);
1253 kfree(ftl
->cache_data
);
1257 static struct mtd_blktrans_ops sm_ftl_ops
= {
1260 .part_bits
= SM_FTL_PARTN_BITS
,
1261 .blksize
= SM_SECTOR_SIZE
,
1262 .getgeo
= sm_getgeo
,
1264 .add_mtd
= sm_add_mtd
,
1265 .remove_dev
= sm_remove_dev
,
1267 .readsect
= sm_read
,
1268 .writesect
= sm_write
,
1271 .release
= sm_release
,
1273 .owner
= THIS_MODULE
,
1276 static __init
int sm_module_init(void)
1279 cache_flush_workqueue
= create_freezable_workqueue("smflush");
1281 if (IS_ERR(cache_flush_workqueue
))
1282 return PTR_ERR(cache_flush_workqueue
);
1284 error
= register_mtd_blktrans(&sm_ftl_ops
);
1286 destroy_workqueue(cache_flush_workqueue
);
1291 static void __exit
sm_module_exit(void)
1293 destroy_workqueue(cache_flush_workqueue
);
1294 deregister_mtd_blktrans(&sm_ftl_ops
);
1297 module_init(sm_module_init
);
1298 module_exit(sm_module_exit
);
1300 MODULE_LICENSE("GPL");
1301 MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
1302 MODULE_DESCRIPTION("Smartmedia/xD mtd translation layer");