Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright © 2009 - Maxim Levitsky
0004  * SmartMedia/xD translation layer
0005  */
0006 
0007 #include <linux/kernel.h>
0008 #include <linux/module.h>
0009 #include <linux/random.h>
0010 #include <linux/hdreg.h>
0011 #include <linux/kthread.h>
0012 #include <linux/freezer.h>
0013 #include <linux/sysfs.h>
0014 #include <linux/bitops.h>
0015 #include <linux/slab.h>
0016 #include <linux/mtd/nand-ecc-sw-hamming.h>
0017 #include "nand/raw/sm_common.h"
0018 #include "sm_ftl.h"
0019 
0020 
0021 
0022 static struct workqueue_struct *cache_flush_workqueue;
0023 
0024 static int cache_timeout = 1000;
0025 module_param(cache_timeout, int, S_IRUGO);
0026 MODULE_PARM_DESC(cache_timeout,
0027     "Timeout (in ms) for cache flush (1000 ms default");
0028 
0029 static int debug;
0030 module_param(debug, int, S_IRUGO | S_IWUSR);
0031 MODULE_PARM_DESC(debug, "Debug level (0-2)");
0032 
0033 
0034 /* ------------------- sysfs attributes ---------------------------------- */
0035 struct sm_sysfs_attribute {
0036     struct device_attribute dev_attr;
0037     char *data;
0038     int len;
0039 };
0040 
0041 static ssize_t sm_attr_show(struct device *dev, struct device_attribute *attr,
0042              char *buf)
0043 {
0044     struct sm_sysfs_attribute *sm_attr =
0045         container_of(attr, struct sm_sysfs_attribute, dev_attr);
0046 
0047     strncpy(buf, sm_attr->data, sm_attr->len);
0048     return sm_attr->len;
0049 }
0050 
0051 
0052 #define NUM_ATTRIBUTES 1
0053 #define SM_CIS_VENDOR_OFFSET 0x59
0054 static struct attribute_group *sm_create_sysfs_attributes(struct sm_ftl *ftl)
0055 {
0056     struct attribute_group *attr_group;
0057     struct attribute **attributes;
0058     struct sm_sysfs_attribute *vendor_attribute;
0059     char *vendor;
0060 
0061     vendor = kstrndup(ftl->cis_buffer + SM_CIS_VENDOR_OFFSET,
0062               SM_SMALL_PAGE - SM_CIS_VENDOR_OFFSET, GFP_KERNEL);
0063     if (!vendor)
0064         goto error1;
0065 
0066     /* Initialize sysfs attributes */
0067     vendor_attribute =
0068         kzalloc(sizeof(struct sm_sysfs_attribute), GFP_KERNEL);
0069     if (!vendor_attribute)
0070         goto error2;
0071 
0072     sysfs_attr_init(&vendor_attribute->dev_attr.attr);
0073 
0074     vendor_attribute->data = vendor;
0075     vendor_attribute->len = strlen(vendor);
0076     vendor_attribute->dev_attr.attr.name = "vendor";
0077     vendor_attribute->dev_attr.attr.mode = S_IRUGO;
0078     vendor_attribute->dev_attr.show = sm_attr_show;
0079 
0080 
0081     /* Create array of pointers to the attributes */
0082     attributes = kcalloc(NUM_ATTRIBUTES + 1, sizeof(struct attribute *),
0083                                 GFP_KERNEL);
0084     if (!attributes)
0085         goto error3;
0086     attributes[0] = &vendor_attribute->dev_attr.attr;
0087 
0088     /* Finally create the attribute group */
0089     attr_group = kzalloc(sizeof(struct attribute_group), GFP_KERNEL);
0090     if (!attr_group)
0091         goto error4;
0092     attr_group->attrs = attributes;
0093     return attr_group;
0094 error4:
0095     kfree(attributes);
0096 error3:
0097     kfree(vendor_attribute);
0098 error2:
0099     kfree(vendor);
0100 error1:
0101     return NULL;
0102 }
0103 
0104 static void sm_delete_sysfs_attributes(struct sm_ftl *ftl)
0105 {
0106     struct attribute **attributes = ftl->disk_attributes->attrs;
0107     int i;
0108 
0109     for (i = 0; attributes[i] ; i++) {
0110 
0111         struct device_attribute *dev_attr = container_of(attributes[i],
0112             struct device_attribute, attr);
0113 
0114         struct sm_sysfs_attribute *sm_attr =
0115             container_of(dev_attr,
0116                 struct sm_sysfs_attribute, dev_attr);
0117 
0118         kfree(sm_attr->data);
0119         kfree(sm_attr);
0120     }
0121 
0122     kfree(ftl->disk_attributes->attrs);
0123     kfree(ftl->disk_attributes);
0124 }
0125 
0126 
0127 /* ----------------------- oob helpers -------------------------------------- */
0128 
0129 static int sm_get_lba(uint8_t *lba)
0130 {
0131     /* check fixed bits */
0132     if ((lba[0] & 0xF8) != 0x10)
0133         return -2;
0134 
0135     /* check parity - endianness doesn't matter */
0136     if (hweight16(*(uint16_t *)lba) & 1)
0137         return -2;
0138 
0139     return (lba[1] >> 1) | ((lba[0] & 0x07) << 7);
0140 }
0141 
0142 
0143 /*
0144  * Read LBA associated with block
0145  * returns -1, if block is erased
0146  * returns -2 if error happens
0147  */
0148 static int sm_read_lba(struct sm_oob *oob)
0149 {
0150     static const uint32_t erased_pattern[4] = {
0151         0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
0152 
0153     uint16_t lba_test;
0154     int lba;
0155 
0156     /* First test for erased block */
0157     if (!memcmp(oob, erased_pattern, SM_OOB_SIZE))
0158         return -1;
0159 
0160     /* Now check is both copies of the LBA differ too much */
0161     lba_test = *(uint16_t *)oob->lba_copy1 ^ *(uint16_t*)oob->lba_copy2;
0162     if (lba_test && !is_power_of_2(lba_test))
0163         return -2;
0164 
0165     /* And read it */
0166     lba = sm_get_lba(oob->lba_copy1);
0167 
0168     if (lba == -2)
0169         lba = sm_get_lba(oob->lba_copy2);
0170 
0171     return lba;
0172 }
0173 
0174 static void sm_write_lba(struct sm_oob *oob, uint16_t lba)
0175 {
0176     uint8_t tmp[2];
0177 
0178     WARN_ON(lba >= 1000);
0179 
0180     tmp[0] = 0x10 | ((lba >> 7) & 0x07);
0181     tmp[1] = (lba << 1) & 0xFF;
0182 
0183     if (hweight16(*(uint16_t *)tmp) & 0x01)
0184         tmp[1] |= 1;
0185 
0186     oob->lba_copy1[0] = oob->lba_copy2[0] = tmp[0];
0187     oob->lba_copy1[1] = oob->lba_copy2[1] = tmp[1];
0188 }
0189 
0190 
0191 /* Make offset from parts */
0192 static loff_t sm_mkoffset(struct sm_ftl *ftl, int zone, int block, int boffset)
0193 {
0194     WARN_ON(boffset & (SM_SECTOR_SIZE - 1));
0195     WARN_ON(zone < 0 || zone >= ftl->zone_count);
0196     WARN_ON(block >= ftl->zone_size);
0197     WARN_ON(boffset >= ftl->block_size);
0198 
0199     if (block == -1)
0200         return -1;
0201 
0202     return (zone * SM_MAX_ZONE_SIZE + block) * ftl->block_size + boffset;
0203 }
0204 
0205 /* Breaks offset into parts */
0206 static void sm_break_offset(struct sm_ftl *ftl, loff_t loffset,
0207                 int *zone, int *block, int *boffset)
0208 {
0209     u64 offset = loffset;
0210     *boffset = do_div(offset, ftl->block_size);
0211     *block = do_div(offset, ftl->max_lba);
0212     *zone = offset >= ftl->zone_count ? -1 : offset;
0213 }
0214 
0215 /* ---------------------- low level IO ------------------------------------- */
0216 
0217 static int sm_correct_sector(uint8_t *buffer, struct sm_oob *oob)
0218 {
0219     bool sm_order = IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_HAMMING_SMC);
0220     uint8_t ecc[3];
0221 
0222     ecc_sw_hamming_calculate(buffer, SM_SMALL_PAGE, ecc, sm_order);
0223     if (ecc_sw_hamming_correct(buffer, ecc, oob->ecc1, SM_SMALL_PAGE,
0224                    sm_order) < 0)
0225         return -EIO;
0226 
0227     buffer += SM_SMALL_PAGE;
0228 
0229     ecc_sw_hamming_calculate(buffer, SM_SMALL_PAGE, ecc, sm_order);
0230     if (ecc_sw_hamming_correct(buffer, ecc, oob->ecc2, SM_SMALL_PAGE,
0231                    sm_order) < 0)
0232         return -EIO;
0233     return 0;
0234 }
0235 
0236 /* Reads a sector + oob*/
0237 static int sm_read_sector(struct sm_ftl *ftl,
0238               int zone, int block, int boffset,
0239               uint8_t *buffer, struct sm_oob *oob)
0240 {
0241     struct mtd_info *mtd = ftl->trans->mtd;
0242     struct mtd_oob_ops ops;
0243     struct sm_oob tmp_oob;
0244     int ret = -EIO;
0245     int try = 0;
0246 
0247     /* FTL can contain -1 entries that are by default filled with bits */
0248     if (block == -1) {
0249         if (buffer)
0250             memset(buffer, 0xFF, SM_SECTOR_SIZE);
0251         return 0;
0252     }
0253 
0254     /* User might not need the oob, but we do for data verification */
0255     if (!oob)
0256         oob = &tmp_oob;
0257 
0258     ops.mode = ftl->smallpagenand ? MTD_OPS_RAW : MTD_OPS_PLACE_OOB;
0259     ops.ooboffs = 0;
0260     ops.ooblen = SM_OOB_SIZE;
0261     ops.oobbuf = (void *)oob;
0262     ops.len = SM_SECTOR_SIZE;
0263     ops.datbuf = buffer;
0264 
0265 again:
0266     if (try++) {
0267         /* Avoid infinite recursion on CIS reads, sm_recheck_media
0268          * won't help anyway
0269          */
0270         if (zone == 0 && block == ftl->cis_block && boffset ==
0271             ftl->cis_boffset)
0272             return ret;
0273 
0274         /* Test if media is stable */
0275         if (try == 3 || sm_recheck_media(ftl))
0276             return ret;
0277     }
0278 
0279     /* Unfortunately, oob read will _always_ succeed,
0280      * despite card removal.....
0281      */
0282     ret = mtd_read_oob(mtd, sm_mkoffset(ftl, zone, block, boffset), &ops);
0283 
0284     /* Test for unknown errors */
0285     if (ret != 0 && !mtd_is_bitflip_or_eccerr(ret)) {
0286         dbg("read of block %d at zone %d, failed due to error (%d)",
0287             block, zone, ret);
0288         goto again;
0289     }
0290 
0291     /* Do a basic test on the oob, to guard against returned garbage */
0292     if (oob->reserved != 0xFFFFFFFF && !is_power_of_2(~oob->reserved))
0293         goto again;
0294 
0295     /* This should never happen, unless there is a bug in the mtd driver */
0296     WARN_ON(ops.oobretlen != SM_OOB_SIZE);
0297     WARN_ON(buffer && ops.retlen != SM_SECTOR_SIZE);
0298 
0299     if (!buffer)
0300         return 0;
0301 
0302     /* Test if sector marked as bad */
0303     if (!sm_sector_valid(oob)) {
0304         dbg("read of block %d at zone %d, failed because it is marked"
0305             " as bad" , block, zone);
0306         goto again;
0307     }
0308 
0309     /* Test ECC*/
0310     if (mtd_is_eccerr(ret) ||
0311         (ftl->smallpagenand && sm_correct_sector(buffer, oob))) {
0312 
0313         dbg("read of block %d at zone %d, failed due to ECC error",
0314             block, zone);
0315         goto again;
0316     }
0317 
0318     return 0;
0319 }
0320 
0321 /* Writes a sector to media */
0322 static int sm_write_sector(struct sm_ftl *ftl,
0323                int zone, int block, int boffset,
0324                uint8_t *buffer, struct sm_oob *oob)
0325 {
0326     struct mtd_oob_ops ops;
0327     struct mtd_info *mtd = ftl->trans->mtd;
0328     int ret;
0329 
0330     BUG_ON(ftl->readonly);
0331 
0332     if (zone == 0 && (block == ftl->cis_block || block == 0)) {
0333         dbg("attempted to write the CIS!");
0334         return -EIO;
0335     }
0336 
0337     if (ftl->unstable)
0338         return -EIO;
0339 
0340     ops.mode = ftl->smallpagenand ? MTD_OPS_RAW : MTD_OPS_PLACE_OOB;
0341     ops.len = SM_SECTOR_SIZE;
0342     ops.datbuf = buffer;
0343     ops.ooboffs = 0;
0344     ops.ooblen = SM_OOB_SIZE;
0345     ops.oobbuf = (void *)oob;
0346 
0347     ret = mtd_write_oob(mtd, sm_mkoffset(ftl, zone, block, boffset), &ops);
0348 
0349     /* Now we assume that hardware will catch write bitflip errors */
0350 
0351     if (ret) {
0352         dbg("write to block %d at zone %d, failed with error %d",
0353             block, zone, ret);
0354 
0355         sm_recheck_media(ftl);
0356         return ret;
0357     }
0358 
0359     /* This should never happen, unless there is a bug in the driver */
0360     WARN_ON(ops.oobretlen != SM_OOB_SIZE);
0361     WARN_ON(buffer && ops.retlen != SM_SECTOR_SIZE);
0362 
0363     return 0;
0364 }
0365 
0366 /* ------------------------ block IO ------------------------------------- */
0367 
0368 /* Write a block using data and lba, and invalid sector bitmap */
0369 static int sm_write_block(struct sm_ftl *ftl, uint8_t *buf,
0370               int zone, int block, int lba,
0371               unsigned long invalid_bitmap)
0372 {
0373     bool sm_order = IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_HAMMING_SMC);
0374     struct sm_oob oob;
0375     int boffset;
0376     int retry = 0;
0377 
0378     /* Initialize the oob with requested values */
0379     memset(&oob, 0xFF, SM_OOB_SIZE);
0380     sm_write_lba(&oob, lba);
0381 restart:
0382     if (ftl->unstable)
0383         return -EIO;
0384 
0385     for (boffset = 0; boffset < ftl->block_size;
0386                 boffset += SM_SECTOR_SIZE) {
0387 
0388         oob.data_status = 0xFF;
0389 
0390         if (test_bit(boffset / SM_SECTOR_SIZE, &invalid_bitmap)) {
0391 
0392             sm_printk("sector %d of block at LBA %d of zone %d"
0393                 " couldn't be read, marking it as invalid",
0394                 boffset / SM_SECTOR_SIZE, lba, zone);
0395 
0396             oob.data_status = 0;
0397         }
0398 
0399         if (ftl->smallpagenand) {
0400             ecc_sw_hamming_calculate(buf + boffset,
0401                          SM_SMALL_PAGE, oob.ecc1,
0402                          sm_order);
0403 
0404             ecc_sw_hamming_calculate(buf + boffset + SM_SMALL_PAGE,
0405                          SM_SMALL_PAGE, oob.ecc2,
0406                          sm_order);
0407         }
0408         if (!sm_write_sector(ftl, zone, block, boffset,
0409                             buf + boffset, &oob))
0410             continue;
0411 
0412         if (!retry) {
0413 
0414             /* If write fails. try to erase the block */
0415             /* This is safe, because we never write in blocks
0416              * that contain valuable data.
0417              * This is intended to repair block that are marked
0418              * as erased, but that isn't fully erased
0419              */
0420 
0421             if (sm_erase_block(ftl, zone, block, 0))
0422                 return -EIO;
0423 
0424             retry = 1;
0425             goto restart;
0426         } else {
0427             sm_mark_block_bad(ftl, zone, block);
0428             return -EIO;
0429         }
0430     }
0431     return 0;
0432 }
0433 
0434 
0435 /* Mark whole block at offset 'offs' as bad. */
0436 static void sm_mark_block_bad(struct sm_ftl *ftl, int zone, int block)
0437 {
0438     struct sm_oob oob;
0439     int boffset;
0440 
0441     memset(&oob, 0xFF, SM_OOB_SIZE);
0442     oob.block_status = 0xF0;
0443 
0444     if (ftl->unstable)
0445         return;
0446 
0447     if (sm_recheck_media(ftl))
0448         return;
0449 
0450     sm_printk("marking block %d of zone %d as bad", block, zone);
0451 
0452     /* We aren't checking the return value, because we don't care */
0453     /* This also fails on fake xD cards, but I guess these won't expose
0454      * any bad blocks till fail completely
0455      */
0456     for (boffset = 0; boffset < ftl->block_size; boffset += SM_SECTOR_SIZE)
0457         sm_write_sector(ftl, zone, block, boffset, NULL, &oob);
0458 }
0459 
0460 /*
0461  * Erase a block within a zone
0462  * If erase succeeds, it updates free block fifo, otherwise marks block as bad
0463  */
0464 static int sm_erase_block(struct sm_ftl *ftl, int zone_num, uint16_t block,
0465               int put_free)
0466 {
0467     struct ftl_zone *zone = &ftl->zones[zone_num];
0468     struct mtd_info *mtd = ftl->trans->mtd;
0469     struct erase_info erase;
0470 
0471     erase.addr = sm_mkoffset(ftl, zone_num, block, 0);
0472     erase.len = ftl->block_size;
0473 
0474     if (ftl->unstable)
0475         return -EIO;
0476 
0477     BUG_ON(ftl->readonly);
0478 
0479     if (zone_num == 0 && (block == ftl->cis_block || block == 0)) {
0480         sm_printk("attempted to erase the CIS!");
0481         return -EIO;
0482     }
0483 
0484     if (mtd_erase(mtd, &erase)) {
0485         sm_printk("erase of block %d in zone %d failed",
0486                             block, zone_num);
0487         goto error;
0488     }
0489 
0490     if (put_free)
0491         kfifo_in(&zone->free_sectors,
0492             (const unsigned char *)&block, sizeof(block));
0493 
0494     return 0;
0495 error:
0496     sm_mark_block_bad(ftl, zone_num, block);
0497     return -EIO;
0498 }
0499 
0500 /* Thoroughly test that block is valid. */
0501 static int sm_check_block(struct sm_ftl *ftl, int zone, int block)
0502 {
0503     int boffset;
0504     struct sm_oob oob;
0505     int lbas[] = { -3, 0, 0, 0 };
0506     int i = 0;
0507     int test_lba;
0508 
0509 
0510     /* First just check that block doesn't look fishy */
0511     /* Only blocks that are valid or are sliced in two parts, are
0512      * accepted
0513      */
0514     for (boffset = 0; boffset < ftl->block_size;
0515                     boffset += SM_SECTOR_SIZE) {
0516 
0517         /* This shouldn't happen anyway */
0518         if (sm_read_sector(ftl, zone, block, boffset, NULL, &oob))
0519             return -2;
0520 
0521         test_lba = sm_read_lba(&oob);
0522 
0523         if (lbas[i] != test_lba)
0524             lbas[++i] = test_lba;
0525 
0526         /* If we found three different LBAs, something is fishy */
0527         if (i == 3)
0528             return -EIO;
0529     }
0530 
0531     /* If the block is sliced (partially erased usually) erase it */
0532     if (i == 2) {
0533         sm_erase_block(ftl, zone, block, 1);
0534         return 1;
0535     }
0536 
0537     return 0;
0538 }
0539 
0540 /* ----------------- media scanning --------------------------------- */
0541 static const struct chs_entry chs_table[] = {
0542     { 1,    125,  4,  4  },
0543     { 2,    125,  4,  8  },
0544     { 4,    250,  4,  8  },
0545     { 8,    250,  4,  16 },
0546     { 16,   500,  4,  16 },
0547     { 32,   500,  8,  16 },
0548     { 64,   500,  8,  32 },
0549     { 128,  500,  16, 32 },
0550     { 256,  1000, 16, 32 },
0551     { 512,  1015, 32, 63 },
0552     { 1024, 985,  33, 63 },
0553     { 2048, 985,  33, 63 },
0554     { 0 },
0555 };
0556 
0557 
0558 static const uint8_t cis_signature[] = {
0559     0x01, 0x03, 0xD9, 0x01, 0xFF, 0x18, 0x02, 0xDF, 0x01, 0x20
0560 };
0561 /* Find out media parameters.
0562  * This ideally has to be based on nand id, but for now device size is enough
0563  */
0564 static int sm_get_media_info(struct sm_ftl *ftl, struct mtd_info *mtd)
0565 {
0566     int i;
0567     int size_in_megs = mtd->size / (1024 * 1024);
0568 
0569     ftl->readonly = mtd->type == MTD_ROM;
0570 
0571     /* Manual settings for very old devices */
0572     ftl->zone_count = 1;
0573     ftl->smallpagenand = 0;
0574 
0575     switch (size_in_megs) {
0576     case 1:
0577         /* 1 MiB flash/rom SmartMedia card (256 byte pages)*/
0578         ftl->zone_size = 256;
0579         ftl->max_lba = 250;
0580         ftl->block_size = 8 * SM_SECTOR_SIZE;
0581         ftl->smallpagenand = 1;
0582 
0583         break;
0584     case 2:
0585         /* 2 MiB flash SmartMedia (256 byte pages)*/
0586         if (mtd->writesize == SM_SMALL_PAGE) {
0587             ftl->zone_size = 512;
0588             ftl->max_lba = 500;
0589             ftl->block_size = 8 * SM_SECTOR_SIZE;
0590             ftl->smallpagenand = 1;
0591         /* 2 MiB rom SmartMedia */
0592         } else {
0593 
0594             if (!ftl->readonly)
0595                 return -ENODEV;
0596 
0597             ftl->zone_size = 256;
0598             ftl->max_lba = 250;
0599             ftl->block_size = 16 * SM_SECTOR_SIZE;
0600         }
0601         break;
0602     case 4:
0603         /* 4 MiB flash/rom SmartMedia device */
0604         ftl->zone_size = 512;
0605         ftl->max_lba = 500;
0606         ftl->block_size = 16 * SM_SECTOR_SIZE;
0607         break;
0608     case 8:
0609         /* 8 MiB flash/rom SmartMedia device */
0610         ftl->zone_size = 1024;
0611         ftl->max_lba = 1000;
0612         ftl->block_size = 16 * SM_SECTOR_SIZE;
0613     }
0614 
0615     /* Minimum xD size is 16MiB. Also, all xD cards have standard zone
0616      * sizes. SmartMedia cards exist up to 128 MiB and have same layout
0617      */
0618     if (size_in_megs >= 16) {
0619         ftl->zone_count = size_in_megs / 16;
0620         ftl->zone_size = 1024;
0621         ftl->max_lba = 1000;
0622         ftl->block_size = 32 * SM_SECTOR_SIZE;
0623     }
0624 
0625     /* Test for proper write,erase and oob sizes */
0626     if (mtd->erasesize > ftl->block_size)
0627         return -ENODEV;
0628 
0629     if (mtd->writesize > SM_SECTOR_SIZE)
0630         return -ENODEV;
0631 
0632     if (ftl->smallpagenand && mtd->oobsize < SM_SMALL_OOB_SIZE)
0633         return -ENODEV;
0634 
0635     if (!ftl->smallpagenand && mtd->oobsize < SM_OOB_SIZE)
0636         return -ENODEV;
0637 
0638     /* We use OOB */
0639     if (!mtd_has_oob(mtd))
0640         return -ENODEV;
0641 
0642     /* Find geometry information */
0643     for (i = 0 ; i < ARRAY_SIZE(chs_table) ; i++) {
0644         if (chs_table[i].size == size_in_megs) {
0645             ftl->cylinders = chs_table[i].cyl;
0646             ftl->heads = chs_table[i].head;
0647             ftl->sectors = chs_table[i].sec;
0648             return 0;
0649         }
0650     }
0651 
0652     sm_printk("media has unknown size : %dMiB", size_in_megs);
0653     ftl->cylinders = 985;
0654     ftl->heads =  33;
0655     ftl->sectors = 63;
0656     return 0;
0657 }
0658 
0659 /* Validate the CIS */
0660 static int sm_read_cis(struct sm_ftl *ftl)
0661 {
0662     struct sm_oob oob;
0663 
0664     if (sm_read_sector(ftl,
0665         0, ftl->cis_block, ftl->cis_boffset, ftl->cis_buffer, &oob))
0666             return -EIO;
0667 
0668     if (!sm_sector_valid(&oob) || !sm_block_valid(&oob))
0669         return -EIO;
0670 
0671     if (!memcmp(ftl->cis_buffer + ftl->cis_page_offset,
0672             cis_signature, sizeof(cis_signature))) {
0673         return 0;
0674     }
0675 
0676     return -EIO;
0677 }
0678 
0679 /* Scan the media for the CIS */
0680 static int sm_find_cis(struct sm_ftl *ftl)
0681 {
0682     struct sm_oob oob;
0683     int block, boffset;
0684     int block_found = 0;
0685     int cis_found = 0;
0686 
0687     /* Search for first valid block */
0688     for (block = 0 ; block < ftl->zone_size - ftl->max_lba ; block++) {
0689 
0690         if (sm_read_sector(ftl, 0, block, 0, NULL, &oob))
0691             continue;
0692 
0693         if (!sm_block_valid(&oob))
0694             continue;
0695         block_found = 1;
0696         break;
0697     }
0698 
0699     if (!block_found)
0700         return -EIO;
0701 
0702     /* Search for first valid sector in this block */
0703     for (boffset = 0 ; boffset < ftl->block_size;
0704                         boffset += SM_SECTOR_SIZE) {
0705 
0706         if (sm_read_sector(ftl, 0, block, boffset, NULL, &oob))
0707             continue;
0708 
0709         if (!sm_sector_valid(&oob))
0710             continue;
0711         break;
0712     }
0713 
0714     if (boffset == ftl->block_size)
0715         return -EIO;
0716 
0717     ftl->cis_block = block;
0718     ftl->cis_boffset = boffset;
0719     ftl->cis_page_offset = 0;
0720 
0721     cis_found = !sm_read_cis(ftl);
0722 
0723     if (!cis_found) {
0724         ftl->cis_page_offset = SM_SMALL_PAGE;
0725         cis_found = !sm_read_cis(ftl);
0726     }
0727 
0728     if (cis_found) {
0729         dbg("CIS block found at offset %x",
0730             block * ftl->block_size +
0731                 boffset + ftl->cis_page_offset);
0732         return 0;
0733     }
0734     return -EIO;
0735 }
0736 
0737 /* Basic test to determine if underlying mtd device if functional */
0738 static int sm_recheck_media(struct sm_ftl *ftl)
0739 {
0740     if (sm_read_cis(ftl)) {
0741 
0742         if (!ftl->unstable) {
0743             sm_printk("media unstable, not allowing writes");
0744             ftl->unstable = 1;
0745         }
0746         return -EIO;
0747     }
0748     return 0;
0749 }
0750 
0751 /* Initialize a FTL zone */
0752 static int sm_init_zone(struct sm_ftl *ftl, int zone_num)
0753 {
0754     struct ftl_zone *zone = &ftl->zones[zone_num];
0755     struct sm_oob oob;
0756     uint16_t block;
0757     int lba;
0758     int i = 0;
0759     int len;
0760 
0761     dbg("initializing zone %d", zone_num);
0762 
0763     /* Allocate memory for FTL table */
0764     zone->lba_to_phys_table = kmalloc_array(ftl->max_lba, 2, GFP_KERNEL);
0765 
0766     if (!zone->lba_to_phys_table)
0767         return -ENOMEM;
0768     memset(zone->lba_to_phys_table, -1, ftl->max_lba * 2);
0769 
0770 
0771     /* Allocate memory for free sectors FIFO */
0772     if (kfifo_alloc(&zone->free_sectors, ftl->zone_size * 2, GFP_KERNEL)) {
0773         kfree(zone->lba_to_phys_table);
0774         return -ENOMEM;
0775     }
0776 
0777     /* Now scan the zone */
0778     for (block = 0 ; block < ftl->zone_size ; block++) {
0779 
0780         /* Skip blocks till the CIS (including) */
0781         if (zone_num == 0 && block <= ftl->cis_block)
0782             continue;
0783 
0784         /* Read the oob of first sector */
0785         if (sm_read_sector(ftl, zone_num, block, 0, NULL, &oob)) {
0786             kfifo_free(&zone->free_sectors);
0787             kfree(zone->lba_to_phys_table);
0788             return -EIO;
0789         }
0790 
0791         /* Test to see if block is erased. It is enough to test
0792          * first sector, because erase happens in one shot
0793          */
0794         if (sm_block_erased(&oob)) {
0795             kfifo_in(&zone->free_sectors,
0796                 (unsigned char *)&block, 2);
0797             continue;
0798         }
0799 
0800         /* If block is marked as bad, skip it */
0801         /* This assumes we can trust first sector*/
0802         /* However the way the block valid status is defined, ensures
0803          * very low probability of failure here
0804          */
0805         if (!sm_block_valid(&oob)) {
0806             dbg("PH %04d <-> <marked bad>", block);
0807             continue;
0808         }
0809 
0810 
0811         lba = sm_read_lba(&oob);
0812 
0813         /* Invalid LBA means that block is damaged. */
0814         /* We can try to erase it, or mark it as bad, but
0815          * lets leave that to recovery application
0816          */
0817         if (lba == -2 || lba >= ftl->max_lba) {
0818             dbg("PH %04d <-> LBA %04d(bad)", block, lba);
0819             continue;
0820         }
0821 
0822 
0823         /* If there is no collision,
0824          * just put the sector in the FTL table
0825          */
0826         if (zone->lba_to_phys_table[lba] < 0) {
0827             dbg_verbose("PH %04d <-> LBA %04d", block, lba);
0828             zone->lba_to_phys_table[lba] = block;
0829             continue;
0830         }
0831 
0832         sm_printk("collision"
0833             " of LBA %d between blocks %d and %d in zone %d",
0834             lba, zone->lba_to_phys_table[lba], block, zone_num);
0835 
0836         /* Test that this block is valid*/
0837         if (sm_check_block(ftl, zone_num, block))
0838             continue;
0839 
0840         /* Test now the old block */
0841         if (sm_check_block(ftl, zone_num,
0842                     zone->lba_to_phys_table[lba])) {
0843             zone->lba_to_phys_table[lba] = block;
0844             continue;
0845         }
0846 
0847         /* If both blocks are valid and share same LBA, it means that
0848          * they hold different versions of same data. It not
0849          * known which is more recent, thus just erase one of them
0850          */
0851         sm_printk("both blocks are valid, erasing the later");
0852         sm_erase_block(ftl, zone_num, block, 1);
0853     }
0854 
0855     dbg("zone initialized");
0856     zone->initialized = 1;
0857 
0858     /* No free sectors, means that the zone is heavily damaged, write won't
0859      * work, but it can still can be (partially) read
0860      */
0861     if (!kfifo_len(&zone->free_sectors)) {
0862         sm_printk("no free blocks in zone %d", zone_num);
0863         return 0;
0864     }
0865 
0866     /* Randomize first block we write to */
0867     get_random_bytes(&i, 2);
0868     i %= (kfifo_len(&zone->free_sectors) / 2);
0869 
0870     while (i--) {
0871         len = kfifo_out(&zone->free_sectors,
0872                     (unsigned char *)&block, 2);
0873         WARN_ON(len != 2);
0874         kfifo_in(&zone->free_sectors, (const unsigned char *)&block, 2);
0875     }
0876     return 0;
0877 }
0878 
0879 /* Get and automatically initialize an FTL mapping for one zone */
0880 static struct ftl_zone *sm_get_zone(struct sm_ftl *ftl, int zone_num)
0881 {
0882     struct ftl_zone *zone;
0883     int error;
0884 
0885     BUG_ON(zone_num >= ftl->zone_count);
0886     zone = &ftl->zones[zone_num];
0887 
0888     if (!zone->initialized) {
0889         error = sm_init_zone(ftl, zone_num);
0890 
0891         if (error)
0892             return ERR_PTR(error);
0893     }
0894     return zone;
0895 }
0896 
0897 
0898 /* ----------------- cache handling ------------------------------------------*/
0899 
0900 /* Initialize the one block cache */
0901 static void sm_cache_init(struct sm_ftl *ftl)
0902 {
0903     ftl->cache_data_invalid_bitmap = 0xFFFFFFFF;
0904     ftl->cache_clean = 1;
0905     ftl->cache_zone = -1;
0906     ftl->cache_block = -1;
0907     /*memset(ftl->cache_data, 0xAA, ftl->block_size);*/
0908 }
0909 
0910 /* Put sector in one block cache */
0911 static void sm_cache_put(struct sm_ftl *ftl, char *buffer, int boffset)
0912 {
0913     memcpy(ftl->cache_data + boffset, buffer, SM_SECTOR_SIZE);
0914     clear_bit(boffset / SM_SECTOR_SIZE, &ftl->cache_data_invalid_bitmap);
0915     ftl->cache_clean = 0;
0916 }
0917 
0918 /* Read a sector from the cache */
0919 static int sm_cache_get(struct sm_ftl *ftl, char *buffer, int boffset)
0920 {
0921     if (test_bit(boffset / SM_SECTOR_SIZE,
0922         &ftl->cache_data_invalid_bitmap))
0923             return -1;
0924 
0925     memcpy(buffer, ftl->cache_data + boffset, SM_SECTOR_SIZE);
0926     return 0;
0927 }
0928 
0929 /* Write the cache to hardware */
0930 static int sm_cache_flush(struct sm_ftl *ftl)
0931 {
0932     struct ftl_zone *zone;
0933 
0934     int sector_num;
0935     uint16_t write_sector;
0936     int zone_num = ftl->cache_zone;
0937     int block_num;
0938 
0939     if (ftl->cache_clean)
0940         return 0;
0941 
0942     if (ftl->unstable)
0943         return -EIO;
0944 
0945     BUG_ON(zone_num < 0);
0946     zone = &ftl->zones[zone_num];
0947     block_num = zone->lba_to_phys_table[ftl->cache_block];
0948 
0949 
0950     /* Try to read all unread areas of the cache block*/
0951     for_each_set_bit(sector_num, &ftl->cache_data_invalid_bitmap,
0952         ftl->block_size / SM_SECTOR_SIZE) {
0953 
0954         if (!sm_read_sector(ftl,
0955             zone_num, block_num, sector_num * SM_SECTOR_SIZE,
0956             ftl->cache_data + sector_num * SM_SECTOR_SIZE, NULL))
0957                 clear_bit(sector_num,
0958                     &ftl->cache_data_invalid_bitmap);
0959     }
0960 restart:
0961 
0962     if (ftl->unstable)
0963         return -EIO;
0964 
0965     /* If there are no spare blocks, */
0966     /* we could still continue by erasing/writing the current block,
0967      * but for such worn out media it doesn't worth the trouble,
0968      * and the dangers
0969      */
0970     if (kfifo_out(&zone->free_sectors,
0971                 (unsigned char *)&write_sector, 2) != 2) {
0972         dbg("no free sectors for write!");
0973         return -EIO;
0974     }
0975 
0976 
0977     if (sm_write_block(ftl, ftl->cache_data, zone_num, write_sector,
0978         ftl->cache_block, ftl->cache_data_invalid_bitmap))
0979             goto restart;
0980 
0981     /* Update the FTL table */
0982     zone->lba_to_phys_table[ftl->cache_block] = write_sector;
0983 
0984     /* Write succesfull, so erase and free the old block */
0985     if (block_num > 0)
0986         sm_erase_block(ftl, zone_num, block_num, 1);
0987 
0988     sm_cache_init(ftl);
0989     return 0;
0990 }
0991 
0992 
0993 /* flush timer, runs a second after last write */
0994 static void sm_cache_flush_timer(struct timer_list *t)
0995 {
0996     struct sm_ftl *ftl = from_timer(ftl, t, timer);
0997     queue_work(cache_flush_workqueue, &ftl->flush_work);
0998 }
0999 
1000 /* cache flush work, kicked by timer */
1001 static void sm_cache_flush_work(struct work_struct *work)
1002 {
1003     struct sm_ftl *ftl = container_of(work, struct sm_ftl, flush_work);
1004     mutex_lock(&ftl->mutex);
1005     sm_cache_flush(ftl);
1006     mutex_unlock(&ftl->mutex);
1007     return;
1008 }
1009 
1010 /* ---------------- outside interface -------------------------------------- */
1011 
1012 /* outside interface: read a sector */
1013 static int sm_read(struct mtd_blktrans_dev *dev,
1014            unsigned long sect_no, char *buf)
1015 {
1016     struct sm_ftl *ftl = dev->priv;
1017     struct ftl_zone *zone;
1018     int error = 0, in_cache = 0;
1019     int zone_num, block, boffset;
1020 
1021     sm_break_offset(ftl, sect_no << 9, &zone_num, &block, &boffset);
1022     mutex_lock(&ftl->mutex);
1023 
1024 
1025     zone = sm_get_zone(ftl, zone_num);
1026     if (IS_ERR(zone)) {
1027         error = PTR_ERR(zone);
1028         goto unlock;
1029     }
1030 
1031     /* Have to look at cache first */
1032     if (ftl->cache_zone == zone_num && ftl->cache_block == block) {
1033         in_cache = 1;
1034         if (!sm_cache_get(ftl, buf, boffset))
1035             goto unlock;
1036     }
1037 
1038     /* Translate the block and return if doesn't exist in the table */
1039     block = zone->lba_to_phys_table[block];
1040 
1041     if (block == -1) {
1042         memset(buf, 0xFF, SM_SECTOR_SIZE);
1043         goto unlock;
1044     }
1045 
1046     if (sm_read_sector(ftl, zone_num, block, boffset, buf, NULL)) {
1047         error = -EIO;
1048         goto unlock;
1049     }
1050 
1051     if (in_cache)
1052         sm_cache_put(ftl, buf, boffset);
1053 unlock:
1054     mutex_unlock(&ftl->mutex);
1055     return error;
1056 }
1057 
1058 /* outside interface: write a sector */
1059 static int sm_write(struct mtd_blktrans_dev *dev,
1060                 unsigned long sec_no, char *buf)
1061 {
1062     struct sm_ftl *ftl = dev->priv;
1063     struct ftl_zone *zone;
1064     int error = 0, zone_num, block, boffset;
1065 
1066     BUG_ON(ftl->readonly);
1067     sm_break_offset(ftl, sec_no << 9, &zone_num, &block, &boffset);
1068 
1069     /* No need in flush thread running now */
1070     del_timer(&ftl->timer);
1071     mutex_lock(&ftl->mutex);
1072 
1073     zone = sm_get_zone(ftl, zone_num);
1074     if (IS_ERR(zone)) {
1075         error = PTR_ERR(zone);
1076         goto unlock;
1077     }
1078 
1079     /* If entry is not in cache, flush it */
1080     if (ftl->cache_block != block || ftl->cache_zone != zone_num) {
1081 
1082         error = sm_cache_flush(ftl);
1083         if (error)
1084             goto unlock;
1085 
1086         ftl->cache_block = block;
1087         ftl->cache_zone = zone_num;
1088     }
1089 
1090     sm_cache_put(ftl, buf, boffset);
1091 unlock:
1092     mod_timer(&ftl->timer, jiffies + msecs_to_jiffies(cache_timeout));
1093     mutex_unlock(&ftl->mutex);
1094     return error;
1095 }
1096 
1097 /* outside interface: flush everything */
1098 static int sm_flush(struct mtd_blktrans_dev *dev)
1099 {
1100     struct sm_ftl *ftl = dev->priv;
1101     int retval;
1102 
1103     mutex_lock(&ftl->mutex);
1104     retval =  sm_cache_flush(ftl);
1105     mutex_unlock(&ftl->mutex);
1106     return retval;
1107 }
1108 
1109 /* outside interface: device is released */
1110 static void sm_release(struct mtd_blktrans_dev *dev)
1111 {
1112     struct sm_ftl *ftl = dev->priv;
1113 
1114     del_timer_sync(&ftl->timer);
1115     cancel_work_sync(&ftl->flush_work);
1116     mutex_lock(&ftl->mutex);
1117     sm_cache_flush(ftl);
1118     mutex_unlock(&ftl->mutex);
1119 }
1120 
1121 /* outside interface: get geometry */
1122 static int sm_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
1123 {
1124     struct sm_ftl *ftl = dev->priv;
1125     geo->heads = ftl->heads;
1126     geo->sectors = ftl->sectors;
1127     geo->cylinders = ftl->cylinders;
1128     return 0;
1129 }
1130 
1131 /* external interface: main initialization function */
1132 static void sm_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
1133 {
1134     struct mtd_blktrans_dev *trans;
1135     struct sm_ftl *ftl;
1136 
1137     /* Allocate & initialize our private structure */
1138     ftl = kzalloc(sizeof(struct sm_ftl), GFP_KERNEL);
1139     if (!ftl)
1140         goto error1;
1141 
1142 
1143     mutex_init(&ftl->mutex);
1144     timer_setup(&ftl->timer, sm_cache_flush_timer, 0);
1145     INIT_WORK(&ftl->flush_work, sm_cache_flush_work);
1146 
1147     /* Read media information */
1148     if (sm_get_media_info(ftl, mtd)) {
1149         dbg("found unsupported mtd device, aborting");
1150         goto error2;
1151     }
1152 
1153 
1154     /* Allocate temporary CIS buffer for read retry support */
1155     ftl->cis_buffer = kzalloc(SM_SECTOR_SIZE, GFP_KERNEL);
1156     if (!ftl->cis_buffer)
1157         goto error2;
1158 
1159     /* Allocate zone array, it will be initialized on demand */
1160     ftl->zones = kcalloc(ftl->zone_count, sizeof(struct ftl_zone),
1161                                 GFP_KERNEL);
1162     if (!ftl->zones)
1163         goto error3;
1164 
1165     /* Allocate the cache*/
1166     ftl->cache_data = kzalloc(ftl->block_size, GFP_KERNEL);
1167 
1168     if (!ftl->cache_data)
1169         goto error4;
1170 
1171     sm_cache_init(ftl);
1172 
1173 
1174     /* Allocate upper layer structure and initialize it */
1175     trans = kzalloc(sizeof(struct mtd_blktrans_dev), GFP_KERNEL);
1176     if (!trans)
1177         goto error5;
1178 
1179     ftl->trans = trans;
1180     trans->priv = ftl;
1181 
1182     trans->tr = tr;
1183     trans->mtd = mtd;
1184     trans->devnum = -1;
1185     trans->size = (ftl->block_size * ftl->max_lba * ftl->zone_count) >> 9;
1186     trans->readonly = ftl->readonly;
1187 
1188     if (sm_find_cis(ftl)) {
1189         dbg("CIS not found on mtd device, aborting");
1190         goto error6;
1191     }
1192 
1193     ftl->disk_attributes = sm_create_sysfs_attributes(ftl);
1194     if (!ftl->disk_attributes)
1195         goto error6;
1196     trans->disk_attributes = ftl->disk_attributes;
1197 
1198     sm_printk("Found %d MiB xD/SmartMedia FTL on mtd%d",
1199         (int)(mtd->size / (1024 * 1024)), mtd->index);
1200 
1201     dbg("FTL layout:");
1202     dbg("%d zone(s), each consists of %d blocks (+%d spares)",
1203         ftl->zone_count, ftl->max_lba,
1204         ftl->zone_size - ftl->max_lba);
1205     dbg("each block consists of %d bytes",
1206         ftl->block_size);
1207 
1208 
1209     /* Register device*/
1210     if (add_mtd_blktrans_dev(trans)) {
1211         dbg("error in mtdblktrans layer");
1212         goto error6;
1213     }
1214     return;
1215 error6:
1216     kfree(trans);
1217 error5:
1218     kfree(ftl->cache_data);
1219 error4:
1220     kfree(ftl->zones);
1221 error3:
1222     kfree(ftl->cis_buffer);
1223 error2:
1224     kfree(ftl);
1225 error1:
1226     return;
1227 }
1228 
1229 /* main interface: device {surprise,} removal */
1230 static void sm_remove_dev(struct mtd_blktrans_dev *dev)
1231 {
1232     struct sm_ftl *ftl = dev->priv;
1233     int i;
1234 
1235     del_mtd_blktrans_dev(dev);
1236     ftl->trans = NULL;
1237 
1238     for (i = 0 ; i < ftl->zone_count; i++) {
1239 
1240         if (!ftl->zones[i].initialized)
1241             continue;
1242 
1243         kfree(ftl->zones[i].lba_to_phys_table);
1244         kfifo_free(&ftl->zones[i].free_sectors);
1245     }
1246 
1247     sm_delete_sysfs_attributes(ftl);
1248     kfree(ftl->cis_buffer);
1249     kfree(ftl->zones);
1250     kfree(ftl->cache_data);
1251     kfree(ftl);
1252 }
1253 
1254 static struct mtd_blktrans_ops sm_ftl_ops = {
1255     .name       = "smblk",
1256     .major      = 0,
1257     .part_bits  = SM_FTL_PARTN_BITS,
1258     .blksize    = SM_SECTOR_SIZE,
1259     .getgeo     = sm_getgeo,
1260 
1261     .add_mtd    = sm_add_mtd,
1262     .remove_dev = sm_remove_dev,
1263 
1264     .readsect   = sm_read,
1265     .writesect  = sm_write,
1266 
1267     .flush      = sm_flush,
1268     .release    = sm_release,
1269 
1270     .owner      = THIS_MODULE,
1271 };
1272 
1273 static __init int sm_module_init(void)
1274 {
1275     int error = 0;
1276 
1277     cache_flush_workqueue = create_freezable_workqueue("smflush");
1278     if (!cache_flush_workqueue)
1279         return -ENOMEM;
1280 
1281     error = register_mtd_blktrans(&sm_ftl_ops);
1282     if (error)
1283         destroy_workqueue(cache_flush_workqueue);
1284     return error;
1285 
1286 }
1287 
1288 static void __exit sm_module_exit(void)
1289 {
1290     destroy_workqueue(cache_flush_workqueue);
1291     deregister_mtd_blktrans(&sm_ftl_ops);
1292 }
1293 
1294 module_init(sm_module_init);
1295 module_exit(sm_module_exit);
1296 
1297 MODULE_LICENSE("GPL");
1298 MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
1299 MODULE_DESCRIPTION("Smartmedia/xD mtd translation layer");