17d17c02aSMaxim Levitsky /* 27d17c02aSMaxim Levitsky * Copyright © 2009 - Maxim Levitsky 37d17c02aSMaxim Levitsky * SmartMedia/xD translation layer 47d17c02aSMaxim Levitsky * 57d17c02aSMaxim Levitsky * This program is free software; you can redistribute it and/or modify 67d17c02aSMaxim Levitsky * it under the terms of the GNU General Public License version 2 as 77d17c02aSMaxim Levitsky * published by the Free Software Foundation. 87d17c02aSMaxim Levitsky */ 97d17c02aSMaxim Levitsky 107d17c02aSMaxim Levitsky #include <linux/kernel.h> 117d17c02aSMaxim Levitsky #include <linux/module.h> 127d17c02aSMaxim Levitsky #include <linux/random.h> 137d17c02aSMaxim Levitsky #include <linux/hdreg.h> 147d17c02aSMaxim Levitsky #include <linux/kthread.h> 157d17c02aSMaxim Levitsky #include <linux/freezer.h> 167d17c02aSMaxim Levitsky #include <linux/sysfs.h> 177d17c02aSMaxim Levitsky #include <linux/bitops.h> 187d17c02aSMaxim Levitsky #include "nand/sm_common.h" 197d17c02aSMaxim Levitsky #include "sm_ftl.h" 207d17c02aSMaxim Levitsky 217d17c02aSMaxim Levitsky #ifdef CONFIG_SM_FTL_MUSEUM 227d17c02aSMaxim Levitsky #include <linux/mtd/nand_ecc.h> 237d17c02aSMaxim Levitsky #endif 247d17c02aSMaxim Levitsky 257d17c02aSMaxim Levitsky 267d17c02aSMaxim Levitsky struct workqueue_struct *cache_flush_workqueue; 277d17c02aSMaxim Levitsky 287d17c02aSMaxim Levitsky static int cache_timeout = 1000; 297d17c02aSMaxim Levitsky module_param(cache_timeout, bool, S_IRUGO); 307d17c02aSMaxim Levitsky MODULE_PARM_DESC(cache_timeout, 317d17c02aSMaxim Levitsky "Timeout (in ms) for cache flush (1000 ms default"); 327d17c02aSMaxim Levitsky 337d17c02aSMaxim Levitsky static int debug; 347d17c02aSMaxim Levitsky module_param(debug, int, S_IRUGO | S_IWUSR); 357d17c02aSMaxim Levitsky MODULE_PARM_DESC(debug, "Debug level (0-2)"); 367d17c02aSMaxim Levitsky 377d17c02aSMaxim Levitsky 387d17c02aSMaxim Levitsky /* ------------------- sysfs attributtes ---------------------------------- */ 397d17c02aSMaxim Levitsky struct sm_sysfs_attribute { 407d17c02aSMaxim Levitsky struct device_attribute dev_attr; 417d17c02aSMaxim Levitsky char *data; 427d17c02aSMaxim Levitsky int len; 437d17c02aSMaxim Levitsky }; 447d17c02aSMaxim Levitsky 457d17c02aSMaxim Levitsky ssize_t sm_attr_show(struct device *dev, struct device_attribute *attr, 467d17c02aSMaxim Levitsky char *buf) 477d17c02aSMaxim Levitsky { 487d17c02aSMaxim Levitsky struct sm_sysfs_attribute *sm_attr = 497d17c02aSMaxim Levitsky container_of(attr, struct sm_sysfs_attribute, dev_attr); 507d17c02aSMaxim Levitsky 517d17c02aSMaxim Levitsky strncpy(buf, sm_attr->data, sm_attr->len); 527d17c02aSMaxim Levitsky return sm_attr->len; 537d17c02aSMaxim Levitsky } 547d17c02aSMaxim Levitsky 557d17c02aSMaxim Levitsky 567d17c02aSMaxim Levitsky #define NUM_ATTRIBUTES 1 577d17c02aSMaxim Levitsky #define SM_CIS_VENDOR_OFFSET 0x59 587d17c02aSMaxim Levitsky struct attribute_group *sm_create_sysfs_attributes(struct sm_ftl *ftl) 597d17c02aSMaxim Levitsky { 607d17c02aSMaxim Levitsky struct attribute_group *attr_group; 617d17c02aSMaxim Levitsky struct attribute **attributes; 627d17c02aSMaxim Levitsky struct sm_sysfs_attribute *vendor_attribute; 637d17c02aSMaxim Levitsky 647d17c02aSMaxim Levitsky int vendor_len = strnlen(ftl->cis_buffer + SM_CIS_VENDOR_OFFSET, 657d17c02aSMaxim Levitsky SM_SMALL_PAGE - SM_CIS_VENDOR_OFFSET); 667d17c02aSMaxim Levitsky 677d17c02aSMaxim Levitsky char *vendor = kmalloc(vendor_len, GFP_KERNEL); 687d17c02aSMaxim Levitsky memcpy(vendor, ftl->cis_buffer + SM_CIS_VENDOR_OFFSET, vendor_len); 697d17c02aSMaxim Levitsky vendor[vendor_len] = 0; 707d17c02aSMaxim Levitsky 717d17c02aSMaxim Levitsky /* Initialize sysfs attributes */ 727d17c02aSMaxim Levitsky vendor_attribute = 737d17c02aSMaxim Levitsky kzalloc(sizeof(struct sm_sysfs_attribute), GFP_KERNEL); 747d17c02aSMaxim Levitsky 757d17c02aSMaxim Levitsky vendor_attribute->data = vendor; 767d17c02aSMaxim Levitsky vendor_attribute->len = vendor_len; 777d17c02aSMaxim Levitsky vendor_attribute->dev_attr.attr.name = "vendor"; 787d17c02aSMaxim Levitsky vendor_attribute->dev_attr.attr.mode = S_IRUGO; 797d17c02aSMaxim Levitsky vendor_attribute->dev_attr.show = sm_attr_show; 807d17c02aSMaxim Levitsky 817d17c02aSMaxim Levitsky 827d17c02aSMaxim Levitsky /* Create array of pointers to the attributes */ 837d17c02aSMaxim Levitsky attributes = kzalloc(sizeof(struct attribute *) * (NUM_ATTRIBUTES + 1), 847d17c02aSMaxim Levitsky GFP_KERNEL); 857d17c02aSMaxim Levitsky attributes[0] = &vendor_attribute->dev_attr.attr; 867d17c02aSMaxim Levitsky 877d17c02aSMaxim Levitsky /* Finally create the attribute group */ 887d17c02aSMaxim Levitsky attr_group = kzalloc(sizeof(struct attribute_group), GFP_KERNEL); 897d17c02aSMaxim Levitsky attr_group->attrs = attributes; 907d17c02aSMaxim Levitsky return attr_group; 917d17c02aSMaxim Levitsky } 927d17c02aSMaxim Levitsky 937d17c02aSMaxim Levitsky void sm_delete_sysfs_attributes(struct sm_ftl *ftl) 947d17c02aSMaxim Levitsky { 957d17c02aSMaxim Levitsky struct attribute **attributes = ftl->disk_attributes->attrs; 967d17c02aSMaxim Levitsky int i; 977d17c02aSMaxim Levitsky 987d17c02aSMaxim Levitsky for (i = 0; attributes[i] ; i++) { 997d17c02aSMaxim Levitsky 1007d17c02aSMaxim Levitsky struct device_attribute *dev_attr = container_of(attributes[i], 1017d17c02aSMaxim Levitsky struct device_attribute, attr); 1027d17c02aSMaxim Levitsky 1037d17c02aSMaxim Levitsky struct sm_sysfs_attribute *sm_attr = 1047d17c02aSMaxim Levitsky container_of(dev_attr, 1057d17c02aSMaxim Levitsky struct sm_sysfs_attribute, dev_attr); 1067d17c02aSMaxim Levitsky 1077d17c02aSMaxim Levitsky kfree(sm_attr->data); 1087d17c02aSMaxim Levitsky kfree(sm_attr); 1097d17c02aSMaxim Levitsky } 1107d17c02aSMaxim Levitsky 1117d17c02aSMaxim Levitsky kfree(ftl->disk_attributes->attrs); 1127d17c02aSMaxim Levitsky kfree(ftl->disk_attributes); 1137d17c02aSMaxim Levitsky } 1147d17c02aSMaxim Levitsky 1157d17c02aSMaxim Levitsky 1167d17c02aSMaxim Levitsky /* ----------------------- oob helpers -------------------------------------- */ 1177d17c02aSMaxim Levitsky 1187d17c02aSMaxim Levitsky static int sm_get_lba(uint8_t *lba) 1197d17c02aSMaxim Levitsky { 1207d17c02aSMaxim Levitsky /* check fixed bits */ 1217d17c02aSMaxim Levitsky if ((lba[0] & 0xF8) != 0x10) 1227d17c02aSMaxim Levitsky return -2; 1237d17c02aSMaxim Levitsky 1247d17c02aSMaxim Levitsky /* check parity - endianess doesn't matter */ 1257d17c02aSMaxim Levitsky if (hweight16(*(uint16_t *)lba) & 1) 1267d17c02aSMaxim Levitsky return -2; 1277d17c02aSMaxim Levitsky 1287d17c02aSMaxim Levitsky return (lba[1] >> 1) | ((lba[0] & 0x07) << 7); 1297d17c02aSMaxim Levitsky } 1307d17c02aSMaxim Levitsky 1317d17c02aSMaxim Levitsky 1327d17c02aSMaxim Levitsky /* 1337d17c02aSMaxim Levitsky * Read LBA asscociated with block 1347d17c02aSMaxim Levitsky * returns -1, if block is erased 1357d17c02aSMaxim Levitsky * returns -2 if error happens 1367d17c02aSMaxim Levitsky */ 1377d17c02aSMaxim Levitsky static int sm_read_lba(struct sm_oob *oob) 1387d17c02aSMaxim Levitsky { 1397d17c02aSMaxim Levitsky static const uint32_t erased_pattern[4] = { 1407d17c02aSMaxim Levitsky 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF }; 1417d17c02aSMaxim Levitsky 1427d17c02aSMaxim Levitsky uint16_t lba_test; 1437d17c02aSMaxim Levitsky int lba; 1447d17c02aSMaxim Levitsky 1457d17c02aSMaxim Levitsky /* First test for erased block */ 1467d17c02aSMaxim Levitsky if (!memcmp(oob, erased_pattern, SM_OOB_SIZE)) 1477d17c02aSMaxim Levitsky return -1; 1487d17c02aSMaxim Levitsky 1497d17c02aSMaxim Levitsky /* Now check is both copies of the LBA differ too much */ 1507d17c02aSMaxim Levitsky lba_test = *(uint16_t *)oob->lba_copy1 ^ *(uint16_t*)oob->lba_copy2; 1517d17c02aSMaxim Levitsky if (lba_test && !is_power_of_2(lba_test)) 1527d17c02aSMaxim Levitsky return -2; 1537d17c02aSMaxim Levitsky 1547d17c02aSMaxim Levitsky /* And read it */ 1557d17c02aSMaxim Levitsky lba = sm_get_lba(oob->lba_copy1); 1567d17c02aSMaxim Levitsky 1577d17c02aSMaxim Levitsky if (lba == -2) 1587d17c02aSMaxim Levitsky lba = sm_get_lba(oob->lba_copy2); 1597d17c02aSMaxim Levitsky 1607d17c02aSMaxim Levitsky return lba; 1617d17c02aSMaxim Levitsky } 1627d17c02aSMaxim Levitsky 1637d17c02aSMaxim Levitsky static void sm_write_lba(struct sm_oob *oob, uint16_t lba) 1647d17c02aSMaxim Levitsky { 1657d17c02aSMaxim Levitsky uint8_t tmp[2]; 1667d17c02aSMaxim Levitsky 1677d17c02aSMaxim Levitsky WARN_ON(lba >= 1000); 1687d17c02aSMaxim Levitsky 1697d17c02aSMaxim Levitsky tmp[0] = 0x10 | ((lba >> 7) & 0x07); 1707d17c02aSMaxim Levitsky tmp[1] = (lba << 1) & 0xFF; 1717d17c02aSMaxim Levitsky 1727d17c02aSMaxim Levitsky if (hweight16(*(uint16_t *)tmp) & 0x01) 1737d17c02aSMaxim Levitsky tmp[1] |= 1; 1747d17c02aSMaxim Levitsky 1757d17c02aSMaxim Levitsky oob->lba_copy1[0] = oob->lba_copy2[0] = tmp[0]; 1767d17c02aSMaxim Levitsky oob->lba_copy1[1] = oob->lba_copy2[1] = tmp[1]; 1777d17c02aSMaxim Levitsky } 1787d17c02aSMaxim Levitsky 1797d17c02aSMaxim Levitsky 1807d17c02aSMaxim Levitsky /* Make offset from parts */ 1817d17c02aSMaxim Levitsky static loff_t sm_mkoffset(struct sm_ftl *ftl, int zone, int block, int boffset) 1827d17c02aSMaxim Levitsky { 1837d17c02aSMaxim Levitsky WARN_ON(boffset & (SM_SECTOR_SIZE - 1)); 1847d17c02aSMaxim Levitsky WARN_ON(zone < 0 || zone >= ftl->zone_count); 1857d17c02aSMaxim Levitsky WARN_ON(block >= ftl->zone_size); 1867d17c02aSMaxim Levitsky WARN_ON(boffset >= ftl->block_size); 1877d17c02aSMaxim Levitsky 1887d17c02aSMaxim Levitsky if (block == -1) 1897d17c02aSMaxim Levitsky return -1; 1907d17c02aSMaxim Levitsky 1917d17c02aSMaxim Levitsky return (zone * SM_MAX_ZONE_SIZE + block) * ftl->block_size + boffset; 1927d17c02aSMaxim Levitsky } 1937d17c02aSMaxim Levitsky 1947d17c02aSMaxim Levitsky /* Breaks offset into parts */ 1957d17c02aSMaxim Levitsky static void sm_break_offset(struct sm_ftl *ftl, loff_t offset, 1967d17c02aSMaxim Levitsky int *zone, int *block, int *boffset) 1977d17c02aSMaxim Levitsky { 1987d17c02aSMaxim Levitsky *boffset = do_div(offset, ftl->block_size); 1997d17c02aSMaxim Levitsky *block = do_div(offset, ftl->max_lba); 2007d17c02aSMaxim Levitsky *zone = offset >= ftl->zone_count ? -1 : offset; 2017d17c02aSMaxim Levitsky } 2027d17c02aSMaxim Levitsky 2037d17c02aSMaxim Levitsky /* ---------------------- low level IO ------------------------------------- */ 2047d17c02aSMaxim Levitsky 2057d17c02aSMaxim Levitsky static int sm_correct_sector(uint8_t *buffer, struct sm_oob *oob) 2067d17c02aSMaxim Levitsky { 2077d17c02aSMaxim Levitsky #ifdef CONFIG_SM_FTL_MUSEUM 2087d17c02aSMaxim Levitsky uint8_t ecc[3]; 2097d17c02aSMaxim Levitsky 2107d17c02aSMaxim Levitsky __nand_calculate_ecc(buffer, SM_SMALL_PAGE, ecc); 2117d17c02aSMaxim Levitsky if (__nand_correct_data(buffer, ecc, oob->ecc1, SM_SMALL_PAGE) < 0) 2127d17c02aSMaxim Levitsky return -EIO; 2137d17c02aSMaxim Levitsky 2147d17c02aSMaxim Levitsky buffer += SM_SMALL_PAGE; 2157d17c02aSMaxim Levitsky 2167d17c02aSMaxim Levitsky __nand_calculate_ecc(buffer, SM_SMALL_PAGE, ecc); 2177d17c02aSMaxim Levitsky if (__nand_correct_data(buffer, ecc, oob->ecc2, SM_SMALL_PAGE) < 0) 2187d17c02aSMaxim Levitsky return -EIO; 2197d17c02aSMaxim Levitsky #endif 2207d17c02aSMaxim Levitsky return 0; 2217d17c02aSMaxim Levitsky } 2227d17c02aSMaxim Levitsky 2237d17c02aSMaxim Levitsky /* Reads a sector + oob*/ 2247d17c02aSMaxim Levitsky static int sm_read_sector(struct sm_ftl *ftl, 2257d17c02aSMaxim Levitsky int zone, int block, int boffset, 2267d17c02aSMaxim Levitsky uint8_t *buffer, struct sm_oob *oob) 2277d17c02aSMaxim Levitsky { 2287d17c02aSMaxim Levitsky struct mtd_info *mtd = ftl->trans->mtd; 2297d17c02aSMaxim Levitsky struct mtd_oob_ops ops; 2307d17c02aSMaxim Levitsky struct sm_oob tmp_oob; 231133fa8c7SMaxim Levitsky int ret = -EIO; 2327d17c02aSMaxim Levitsky int try = 0; 2337d17c02aSMaxim Levitsky 2347d17c02aSMaxim Levitsky /* FTL can contain -1 entries that are by default filled with bits */ 2357d17c02aSMaxim Levitsky if (block == -1) { 2367d17c02aSMaxim Levitsky memset(buffer, 0xFF, SM_SECTOR_SIZE); 2377d17c02aSMaxim Levitsky return 0; 2387d17c02aSMaxim Levitsky } 2397d17c02aSMaxim Levitsky 2407d17c02aSMaxim Levitsky /* User might not need the oob, but we do for data vertification */ 2417d17c02aSMaxim Levitsky if (!oob) 2427d17c02aSMaxim Levitsky oob = &tmp_oob; 2437d17c02aSMaxim Levitsky 2447d17c02aSMaxim Levitsky ops.mode = ftl->smallpagenand ? MTD_OOB_RAW : MTD_OOB_PLACE; 2457d17c02aSMaxim Levitsky ops.ooboffs = 0; 2467d17c02aSMaxim Levitsky ops.ooblen = SM_OOB_SIZE; 2477d17c02aSMaxim Levitsky ops.oobbuf = (void *)oob; 2487d17c02aSMaxim Levitsky ops.len = SM_SECTOR_SIZE; 2497d17c02aSMaxim Levitsky ops.datbuf = buffer; 2507d17c02aSMaxim Levitsky 2517d17c02aSMaxim Levitsky again: 2527d17c02aSMaxim Levitsky if (try++) { 2537d17c02aSMaxim Levitsky /* Avoid infinite recursion on CIS reads, sm_recheck_media 2547d17c02aSMaxim Levitsky won't help anyway */ 2557d17c02aSMaxim Levitsky if (zone == 0 && block == ftl->cis_block && boffset == 2567d17c02aSMaxim Levitsky ftl->cis_boffset) 2577d17c02aSMaxim Levitsky return ret; 2587d17c02aSMaxim Levitsky 2597d17c02aSMaxim Levitsky /* Test if media is stable */ 2607d17c02aSMaxim Levitsky if (try == 3 || sm_recheck_media(ftl)) 2617d17c02aSMaxim Levitsky return ret; 2627d17c02aSMaxim Levitsky } 2637d17c02aSMaxim Levitsky 2647d17c02aSMaxim Levitsky /* Unfortunelly, oob read will _always_ succeed, 2657d17c02aSMaxim Levitsky despite card removal..... */ 2667d17c02aSMaxim Levitsky ret = mtd->read_oob(mtd, sm_mkoffset(ftl, zone, block, boffset), &ops); 2677d17c02aSMaxim Levitsky 2687d17c02aSMaxim Levitsky /* Test for unknown errors */ 2697d17c02aSMaxim Levitsky if (ret != 0 && ret != -EUCLEAN && ret != -EBADMSG) { 2707d17c02aSMaxim Levitsky dbg("read of block %d at zone %d, failed due to error (%d)", 2717d17c02aSMaxim Levitsky block, zone, ret); 2727d17c02aSMaxim Levitsky goto again; 2737d17c02aSMaxim Levitsky } 2747d17c02aSMaxim Levitsky 2757d17c02aSMaxim Levitsky /* Do a basic test on the oob, to guard against returned garbage */ 2767d17c02aSMaxim Levitsky if (oob->reserved != 0xFFFFFFFF && !is_power_of_2(~oob->reserved)) 2777d17c02aSMaxim Levitsky goto again; 2787d17c02aSMaxim Levitsky 2797d17c02aSMaxim Levitsky /* This should never happen, unless there is a bug in the mtd driver */ 2807d17c02aSMaxim Levitsky WARN_ON(ops.oobretlen != SM_OOB_SIZE); 2817d17c02aSMaxim Levitsky WARN_ON(buffer && ops.retlen != SM_SECTOR_SIZE); 2827d17c02aSMaxim Levitsky 2837d17c02aSMaxim Levitsky if (!buffer) 2847d17c02aSMaxim Levitsky return 0; 2857d17c02aSMaxim Levitsky 2867d17c02aSMaxim Levitsky /* Test if sector marked as bad */ 2877d17c02aSMaxim Levitsky if (!sm_sector_valid(oob)) { 2887d17c02aSMaxim Levitsky dbg("read of block %d at zone %d, failed because it is marked" 2897d17c02aSMaxim Levitsky " as bad" , block, zone); 2907d17c02aSMaxim Levitsky goto again; 2917d17c02aSMaxim Levitsky } 2927d17c02aSMaxim Levitsky 2937d17c02aSMaxim Levitsky /* Test ECC*/ 2947d17c02aSMaxim Levitsky if (ret == -EBADMSG || 2957d17c02aSMaxim Levitsky (ftl->smallpagenand && sm_correct_sector(buffer, oob))) { 2967d17c02aSMaxim Levitsky 2977d17c02aSMaxim Levitsky dbg("read of block %d at zone %d, failed due to ECC error", 2987d17c02aSMaxim Levitsky block, zone); 2997d17c02aSMaxim Levitsky goto again; 3007d17c02aSMaxim Levitsky } 3017d17c02aSMaxim Levitsky 3027d17c02aSMaxim Levitsky return 0; 3037d17c02aSMaxim Levitsky } 3047d17c02aSMaxim Levitsky 3057d17c02aSMaxim Levitsky /* Writes a sector to media */ 3067d17c02aSMaxim Levitsky static int sm_write_sector(struct sm_ftl *ftl, 3077d17c02aSMaxim Levitsky int zone, int block, int boffset, 3087d17c02aSMaxim Levitsky uint8_t *buffer, struct sm_oob *oob) 3097d17c02aSMaxim Levitsky { 3107d17c02aSMaxim Levitsky struct mtd_oob_ops ops; 3117d17c02aSMaxim Levitsky struct mtd_info *mtd = ftl->trans->mtd; 3127d17c02aSMaxim Levitsky int ret; 3137d17c02aSMaxim Levitsky 3147d17c02aSMaxim Levitsky BUG_ON(ftl->readonly); 3157d17c02aSMaxim Levitsky 3167d17c02aSMaxim Levitsky if (zone == 0 && (block == ftl->cis_block || block == 0)) { 3177d17c02aSMaxim Levitsky dbg("attempted to write the CIS!"); 3187d17c02aSMaxim Levitsky return -EIO; 3197d17c02aSMaxim Levitsky } 3207d17c02aSMaxim Levitsky 3217d17c02aSMaxim Levitsky if (ftl->unstable) 3227d17c02aSMaxim Levitsky return -EIO; 3237d17c02aSMaxim Levitsky 3247d17c02aSMaxim Levitsky ops.mode = ftl->smallpagenand ? MTD_OOB_RAW : MTD_OOB_PLACE; 3257d17c02aSMaxim Levitsky ops.len = SM_SECTOR_SIZE; 3267d17c02aSMaxim Levitsky ops.datbuf = buffer; 3277d17c02aSMaxim Levitsky ops.ooboffs = 0; 3287d17c02aSMaxim Levitsky ops.ooblen = SM_OOB_SIZE; 3297d17c02aSMaxim Levitsky ops.oobbuf = (void *)oob; 3307d17c02aSMaxim Levitsky 3317d17c02aSMaxim Levitsky ret = mtd->write_oob(mtd, sm_mkoffset(ftl, zone, block, boffset), &ops); 3327d17c02aSMaxim Levitsky 3337d17c02aSMaxim Levitsky /* Now we assume that hardware will catch write bitflip errors */ 3347d17c02aSMaxim Levitsky /* If you are paranoid, use CONFIG_MTD_NAND_VERIFY_WRITE */ 3357d17c02aSMaxim Levitsky 3367d17c02aSMaxim Levitsky if (ret) { 3377d17c02aSMaxim Levitsky dbg("write to block %d at zone %d, failed with error %d", 3387d17c02aSMaxim Levitsky block, zone, ret); 3397d17c02aSMaxim Levitsky 3407d17c02aSMaxim Levitsky sm_recheck_media(ftl); 3417d17c02aSMaxim Levitsky return ret; 3427d17c02aSMaxim Levitsky } 3437d17c02aSMaxim Levitsky 3447d17c02aSMaxim Levitsky /* This should never happen, unless there is a bug in the driver */ 3457d17c02aSMaxim Levitsky WARN_ON(ops.oobretlen != SM_OOB_SIZE); 3467d17c02aSMaxim Levitsky WARN_ON(buffer && ops.retlen != SM_SECTOR_SIZE); 3477d17c02aSMaxim Levitsky 3487d17c02aSMaxim Levitsky return 0; 3497d17c02aSMaxim Levitsky } 3507d17c02aSMaxim Levitsky 3517d17c02aSMaxim Levitsky /* ------------------------ block IO ------------------------------------- */ 3527d17c02aSMaxim Levitsky 3537d17c02aSMaxim Levitsky /* Write a block using data and lba, and invalid sector bitmap */ 3547d17c02aSMaxim Levitsky static int sm_write_block(struct sm_ftl *ftl, uint8_t *buf, 3557d17c02aSMaxim Levitsky int zone, int block, int lba, 3567d17c02aSMaxim Levitsky unsigned long invalid_bitmap) 3577d17c02aSMaxim Levitsky { 3587d17c02aSMaxim Levitsky struct sm_oob oob; 3597d17c02aSMaxim Levitsky int boffset; 3607d17c02aSMaxim Levitsky int retry = 0; 3617d17c02aSMaxim Levitsky 3627d17c02aSMaxim Levitsky /* Initialize the oob with requested values */ 3637d17c02aSMaxim Levitsky memset(&oob, 0xFF, SM_OOB_SIZE); 3647d17c02aSMaxim Levitsky sm_write_lba(&oob, lba); 3657d17c02aSMaxim Levitsky restart: 3667d17c02aSMaxim Levitsky if (ftl->unstable) 3677d17c02aSMaxim Levitsky return -EIO; 3687d17c02aSMaxim Levitsky 3697d17c02aSMaxim Levitsky for (boffset = 0; boffset < ftl->block_size; 3707d17c02aSMaxim Levitsky boffset += SM_SECTOR_SIZE) { 3717d17c02aSMaxim Levitsky 3727d17c02aSMaxim Levitsky oob.data_status = 0xFF; 3737d17c02aSMaxim Levitsky 3747d17c02aSMaxim Levitsky if (test_bit(boffset / SM_SECTOR_SIZE, &invalid_bitmap)) { 3757d17c02aSMaxim Levitsky 3767d17c02aSMaxim Levitsky sm_printk("sector %d of block at LBA %d of zone %d" 3777d17c02aSMaxim Levitsky " coudn't be read, marking it as invalid", 3787d17c02aSMaxim Levitsky boffset / SM_SECTOR_SIZE, lba, zone); 3797d17c02aSMaxim Levitsky 3807d17c02aSMaxim Levitsky oob.data_status = 0; 3817d17c02aSMaxim Levitsky } 3827d17c02aSMaxim Levitsky 3837d17c02aSMaxim Levitsky #ifdef CONFIG_SM_FTL_MUSEUM 3847d17c02aSMaxim Levitsky if (ftl->smallpagenand) { 3857d17c02aSMaxim Levitsky __nand_calculate_ecc(buf + boffset, 3867d17c02aSMaxim Levitsky SM_SMALL_PAGE, oob.ecc1); 3877d17c02aSMaxim Levitsky 3887d17c02aSMaxim Levitsky __nand_calculate_ecc(buf + boffset + SM_SMALL_PAGE, 3897d17c02aSMaxim Levitsky SM_SMALL_PAGE, oob.ecc2); 3907d17c02aSMaxim Levitsky } 3917d17c02aSMaxim Levitsky #endif 3927d17c02aSMaxim Levitsky if (!sm_write_sector(ftl, zone, block, boffset, 3937d17c02aSMaxim Levitsky buf + boffset, &oob)) 3947d17c02aSMaxim Levitsky continue; 3957d17c02aSMaxim Levitsky 3967d17c02aSMaxim Levitsky if (!retry) { 3977d17c02aSMaxim Levitsky 3987d17c02aSMaxim Levitsky /* If write fails. try to erase the block */ 3997d17c02aSMaxim Levitsky /* This is safe, because we never write in blocks 4007d17c02aSMaxim Levitsky that contain valuable data. 4017d17c02aSMaxim Levitsky This is intended to repair block that are marked 4027d17c02aSMaxim Levitsky as erased, but that isn't fully erased*/ 4037d17c02aSMaxim Levitsky 4047d17c02aSMaxim Levitsky if (sm_erase_block(ftl, zone, block, 0)) 4057d17c02aSMaxim Levitsky return -EIO; 4067d17c02aSMaxim Levitsky 4077d17c02aSMaxim Levitsky retry = 1; 4087d17c02aSMaxim Levitsky goto restart; 4097d17c02aSMaxim Levitsky } else { 4107d17c02aSMaxim Levitsky sm_mark_block_bad(ftl, zone, block); 4117d17c02aSMaxim Levitsky return -EIO; 4127d17c02aSMaxim Levitsky } 4137d17c02aSMaxim Levitsky } 4147d17c02aSMaxim Levitsky return 0; 4157d17c02aSMaxim Levitsky } 4167d17c02aSMaxim Levitsky 4177d17c02aSMaxim Levitsky 4187d17c02aSMaxim Levitsky /* Mark whole block at offset 'offs' as bad. */ 4197d17c02aSMaxim Levitsky static void sm_mark_block_bad(struct sm_ftl *ftl, int zone, int block) 4207d17c02aSMaxim Levitsky { 4217d17c02aSMaxim Levitsky struct sm_oob oob; 4227d17c02aSMaxim Levitsky int boffset; 4237d17c02aSMaxim Levitsky 4247d17c02aSMaxim Levitsky memset(&oob, 0xFF, SM_OOB_SIZE); 4257d17c02aSMaxim Levitsky oob.block_status = 0xF0; 4267d17c02aSMaxim Levitsky 4277d17c02aSMaxim Levitsky if (ftl->unstable) 4287d17c02aSMaxim Levitsky return; 4297d17c02aSMaxim Levitsky 4307d17c02aSMaxim Levitsky if (sm_recheck_media(ftl)) 4317d17c02aSMaxim Levitsky return; 4327d17c02aSMaxim Levitsky 4337d17c02aSMaxim Levitsky sm_printk("marking block %d of zone %d as bad", block, zone); 4347d17c02aSMaxim Levitsky 4357d17c02aSMaxim Levitsky /* We aren't checking the return value, because we don't care */ 4367d17c02aSMaxim Levitsky /* This also fails on fake xD cards, but I guess these won't expose 4377d17c02aSMaxim Levitsky any bad blocks till fail completly */ 4387d17c02aSMaxim Levitsky for (boffset = 0; boffset < ftl->block_size; boffset += SM_SECTOR_SIZE) 4397d17c02aSMaxim Levitsky sm_write_sector(ftl, zone, block, boffset, NULL, &oob); 4407d17c02aSMaxim Levitsky } 4417d17c02aSMaxim Levitsky 4427d17c02aSMaxim Levitsky /* 4437d17c02aSMaxim Levitsky * Erase a block within a zone 4447d17c02aSMaxim Levitsky * If erase succedes, it updates free block fifo, otherwise marks block as bad 4457d17c02aSMaxim Levitsky */ 4467d17c02aSMaxim Levitsky static int sm_erase_block(struct sm_ftl *ftl, int zone_num, uint16_t block, 4477d17c02aSMaxim Levitsky int put_free) 4487d17c02aSMaxim Levitsky { 4497d17c02aSMaxim Levitsky struct ftl_zone *zone = &ftl->zones[zone_num]; 4507d17c02aSMaxim Levitsky struct mtd_info *mtd = ftl->trans->mtd; 4517d17c02aSMaxim Levitsky struct erase_info erase; 4527d17c02aSMaxim Levitsky 4537d17c02aSMaxim Levitsky erase.mtd = mtd; 4547d17c02aSMaxim Levitsky erase.callback = sm_erase_callback; 4557d17c02aSMaxim Levitsky erase.addr = sm_mkoffset(ftl, zone_num, block, 0); 4567d17c02aSMaxim Levitsky erase.len = ftl->block_size; 4577d17c02aSMaxim Levitsky erase.priv = (u_long)ftl; 4587d17c02aSMaxim Levitsky 4597d17c02aSMaxim Levitsky if (ftl->unstable) 4607d17c02aSMaxim Levitsky return -EIO; 4617d17c02aSMaxim Levitsky 4627d17c02aSMaxim Levitsky BUG_ON(ftl->readonly); 4637d17c02aSMaxim Levitsky 4647d17c02aSMaxim Levitsky if (zone_num == 0 && (block == ftl->cis_block || block == 0)) { 4657d17c02aSMaxim Levitsky sm_printk("attempted to erase the CIS!"); 4667d17c02aSMaxim Levitsky return -EIO; 4677d17c02aSMaxim Levitsky } 4687d17c02aSMaxim Levitsky 4697d17c02aSMaxim Levitsky if (mtd->erase(mtd, &erase)) { 4707d17c02aSMaxim Levitsky sm_printk("erase of block %d in zone %d failed", 4717d17c02aSMaxim Levitsky block, zone_num); 4727d17c02aSMaxim Levitsky goto error; 4737d17c02aSMaxim Levitsky } 4747d17c02aSMaxim Levitsky 4757d17c02aSMaxim Levitsky if (erase.state == MTD_ERASE_PENDING) 4767d17c02aSMaxim Levitsky wait_for_completion(&ftl->erase_completion); 4777d17c02aSMaxim Levitsky 4787d17c02aSMaxim Levitsky if (erase.state != MTD_ERASE_DONE) { 4797d17c02aSMaxim Levitsky sm_printk("erase of block %d in zone %d failed after wait", 4807d17c02aSMaxim Levitsky block, zone_num); 4817d17c02aSMaxim Levitsky goto error; 4827d17c02aSMaxim Levitsky } 4837d17c02aSMaxim Levitsky 4847d17c02aSMaxim Levitsky if (put_free) 4857d17c02aSMaxim Levitsky kfifo_in(&zone->free_sectors, 4867d17c02aSMaxim Levitsky (const unsigned char *)&block, sizeof(block)); 4877d17c02aSMaxim Levitsky 4887d17c02aSMaxim Levitsky return 0; 4897d17c02aSMaxim Levitsky error: 4907d17c02aSMaxim Levitsky sm_mark_block_bad(ftl, zone_num, block); 4917d17c02aSMaxim Levitsky return -EIO; 4927d17c02aSMaxim Levitsky } 4937d17c02aSMaxim Levitsky 4947d17c02aSMaxim Levitsky static void sm_erase_callback(struct erase_info *self) 4957d17c02aSMaxim Levitsky { 4967d17c02aSMaxim Levitsky struct sm_ftl *ftl = (struct sm_ftl *)self->priv; 4977d17c02aSMaxim Levitsky complete(&ftl->erase_completion); 4987d17c02aSMaxim Levitsky } 4997d17c02aSMaxim Levitsky 5007d17c02aSMaxim Levitsky /* Throughtly test that block is valid. */ 5017d17c02aSMaxim Levitsky static int sm_check_block(struct sm_ftl *ftl, int zone, int block) 5027d17c02aSMaxim Levitsky { 5037d17c02aSMaxim Levitsky int boffset; 5047d17c02aSMaxim Levitsky struct sm_oob oob; 5057d17c02aSMaxim Levitsky int lbas[] = { -3, 0, 0, 0 }; 5067d17c02aSMaxim Levitsky int i = 0; 5077d17c02aSMaxim Levitsky int test_lba; 5087d17c02aSMaxim Levitsky 5097d17c02aSMaxim Levitsky 5107d17c02aSMaxim Levitsky /* First just check that block doesn't look fishy */ 5117d17c02aSMaxim Levitsky /* Only blocks that are valid or are sliced in two parts, are 5127d17c02aSMaxim Levitsky accepted */ 5137d17c02aSMaxim Levitsky for (boffset = 0; boffset < ftl->block_size; 5147d17c02aSMaxim Levitsky boffset += SM_SECTOR_SIZE) { 5157d17c02aSMaxim Levitsky 5167d17c02aSMaxim Levitsky /* This shoudn't happen anyway */ 5177d17c02aSMaxim Levitsky if (sm_read_sector(ftl, zone, block, boffset, NULL, &oob)) 5187d17c02aSMaxim Levitsky return -2; 5197d17c02aSMaxim Levitsky 5207d17c02aSMaxim Levitsky test_lba = sm_read_lba(&oob); 5217d17c02aSMaxim Levitsky 5227d17c02aSMaxim Levitsky if (lbas[i] != test_lba) 5237d17c02aSMaxim Levitsky lbas[++i] = test_lba; 5247d17c02aSMaxim Levitsky 5257d17c02aSMaxim Levitsky /* If we found three different LBAs, something is fishy */ 5267d17c02aSMaxim Levitsky if (i == 3) 5277d17c02aSMaxim Levitsky return -EIO; 5287d17c02aSMaxim Levitsky } 5297d17c02aSMaxim Levitsky 5307d17c02aSMaxim Levitsky /* If the block is sliced (partialy erased usually) erase it */ 5317d17c02aSMaxim Levitsky if (i == 2) { 5327d17c02aSMaxim Levitsky sm_erase_block(ftl, zone, block, 1); 5337d17c02aSMaxim Levitsky return 1; 5347d17c02aSMaxim Levitsky } 5357d17c02aSMaxim Levitsky 5367d17c02aSMaxim Levitsky return 0; 5377d17c02aSMaxim Levitsky } 5387d17c02aSMaxim Levitsky 5397d17c02aSMaxim Levitsky /* ----------------- media scanning --------------------------------- */ 5407d17c02aSMaxim Levitsky static const struct chs_entry chs_table[] = { 5417d17c02aSMaxim Levitsky { 1, 125, 4, 4 }, 5427d17c02aSMaxim Levitsky { 2, 125, 4, 8 }, 5437d17c02aSMaxim Levitsky { 4, 250, 4, 8 }, 5447d17c02aSMaxim Levitsky { 8, 250, 4, 16 }, 5457d17c02aSMaxim Levitsky { 16, 500, 4, 16 }, 5467d17c02aSMaxim Levitsky { 32, 500, 8, 16 }, 5477d17c02aSMaxim Levitsky { 64, 500, 8, 32 }, 5487d17c02aSMaxim Levitsky { 128, 500, 16, 32 }, 5497d17c02aSMaxim Levitsky { 256, 1000, 16, 32 }, 5507d17c02aSMaxim Levitsky { 512, 1015, 32, 63 }, 5517d17c02aSMaxim Levitsky { 1024, 985, 33, 63 }, 5527d17c02aSMaxim Levitsky { 2048, 985, 33, 63 }, 5537d17c02aSMaxim Levitsky { 0 }, 5547d17c02aSMaxim Levitsky }; 5557d17c02aSMaxim Levitsky 5567d17c02aSMaxim Levitsky 5577d17c02aSMaxim Levitsky static const uint8_t cis_signature[] = { 5587d17c02aSMaxim Levitsky 0x01, 0x03, 0xD9, 0x01, 0xFF, 0x18, 0x02, 0xDF, 0x01, 0x20 5597d17c02aSMaxim Levitsky }; 5607d17c02aSMaxim Levitsky /* Find out media parameters. 5617d17c02aSMaxim Levitsky * This ideally has to be based on nand id, but for now device size is enough */ 5627d17c02aSMaxim Levitsky int sm_get_media_info(struct sm_ftl *ftl, struct mtd_info *mtd) 5637d17c02aSMaxim Levitsky { 5647d17c02aSMaxim Levitsky int i; 5657d17c02aSMaxim Levitsky int size_in_megs = mtd->size / (1024 * 1024); 5667d17c02aSMaxim Levitsky 5677d17c02aSMaxim Levitsky ftl->readonly = mtd->type == MTD_ROM; 5687d17c02aSMaxim Levitsky 5697d17c02aSMaxim Levitsky /* Manual settings for very old devices */ 5707d17c02aSMaxim Levitsky ftl->zone_count = 1; 5717d17c02aSMaxim Levitsky ftl->smallpagenand = 0; 5727d17c02aSMaxim Levitsky 5737d17c02aSMaxim Levitsky switch (size_in_megs) { 5747d17c02aSMaxim Levitsky case 1: 5757d17c02aSMaxim Levitsky /* 1 MiB flash/rom SmartMedia card (256 byte pages)*/ 5767d17c02aSMaxim Levitsky ftl->zone_size = 256; 5777d17c02aSMaxim Levitsky ftl->max_lba = 250; 5787d17c02aSMaxim Levitsky ftl->block_size = 8 * SM_SECTOR_SIZE; 5797d17c02aSMaxim Levitsky ftl->smallpagenand = 1; 5807d17c02aSMaxim Levitsky 5817d17c02aSMaxim Levitsky break; 5827d17c02aSMaxim Levitsky case 2: 5837d17c02aSMaxim Levitsky /* 2 MiB flash SmartMedia (256 byte pages)*/ 5847d17c02aSMaxim Levitsky if (mtd->writesize == SM_SMALL_PAGE) { 5857d17c02aSMaxim Levitsky ftl->zone_size = 512; 5867d17c02aSMaxim Levitsky ftl->max_lba = 500; 5877d17c02aSMaxim Levitsky ftl->block_size = 8 * SM_SECTOR_SIZE; 5887d17c02aSMaxim Levitsky ftl->smallpagenand = 1; 5897d17c02aSMaxim Levitsky /* 2 MiB rom SmartMedia */ 5907d17c02aSMaxim Levitsky } else { 5917d17c02aSMaxim Levitsky 5927d17c02aSMaxim Levitsky if (!ftl->readonly) 5937d17c02aSMaxim Levitsky return -ENODEV; 5947d17c02aSMaxim Levitsky 5957d17c02aSMaxim Levitsky ftl->zone_size = 256; 5967d17c02aSMaxim Levitsky ftl->max_lba = 250; 5977d17c02aSMaxim Levitsky ftl->block_size = 16 * SM_SECTOR_SIZE; 5987d17c02aSMaxim Levitsky } 5997d17c02aSMaxim Levitsky break; 6007d17c02aSMaxim Levitsky case 4: 6017d17c02aSMaxim Levitsky /* 4 MiB flash/rom SmartMedia device */ 6027d17c02aSMaxim Levitsky ftl->zone_size = 512; 6037d17c02aSMaxim Levitsky ftl->max_lba = 500; 6047d17c02aSMaxim Levitsky ftl->block_size = 16 * SM_SECTOR_SIZE; 6057d17c02aSMaxim Levitsky break; 6067d17c02aSMaxim Levitsky case 8: 6077d17c02aSMaxim Levitsky /* 8 MiB flash/rom SmartMedia device */ 6087d17c02aSMaxim Levitsky ftl->zone_size = 1024; 6097d17c02aSMaxim Levitsky ftl->max_lba = 1000; 6107d17c02aSMaxim Levitsky ftl->block_size = 16 * SM_SECTOR_SIZE; 6117d17c02aSMaxim Levitsky } 6127d17c02aSMaxim Levitsky 6137d17c02aSMaxim Levitsky /* Minimum xD size is 16MiB. Also, all xD cards have standard zone 6147d17c02aSMaxim Levitsky sizes. SmartMedia cards exist up to 128 MiB and have same layout*/ 6157d17c02aSMaxim Levitsky if (size_in_megs >= 16) { 6167d17c02aSMaxim Levitsky ftl->zone_count = size_in_megs / 16; 6177d17c02aSMaxim Levitsky ftl->zone_size = 1024; 6187d17c02aSMaxim Levitsky ftl->max_lba = 1000; 6197d17c02aSMaxim Levitsky ftl->block_size = 32 * SM_SECTOR_SIZE; 6207d17c02aSMaxim Levitsky } 6217d17c02aSMaxim Levitsky 6227d17c02aSMaxim Levitsky /* Test for proper write,erase and oob sizes */ 6237d17c02aSMaxim Levitsky if (mtd->erasesize > ftl->block_size) 6247d17c02aSMaxim Levitsky return -ENODEV; 6257d17c02aSMaxim Levitsky 6267d17c02aSMaxim Levitsky if (mtd->writesize > SM_SECTOR_SIZE) 6277d17c02aSMaxim Levitsky return -ENODEV; 6287d17c02aSMaxim Levitsky 6297d17c02aSMaxim Levitsky if (ftl->smallpagenand && mtd->oobsize < SM_SMALL_OOB_SIZE) 6307d17c02aSMaxim Levitsky return -ENODEV; 6317d17c02aSMaxim Levitsky 6327d17c02aSMaxim Levitsky if (!ftl->smallpagenand && mtd->oobsize < SM_OOB_SIZE) 6337d17c02aSMaxim Levitsky return -ENODEV; 6347d17c02aSMaxim Levitsky 6357d17c02aSMaxim Levitsky /* We use these functions for IO */ 6367d17c02aSMaxim Levitsky if (!mtd->read_oob || !mtd->write_oob) 6377d17c02aSMaxim Levitsky return -ENODEV; 6387d17c02aSMaxim Levitsky 6397d17c02aSMaxim Levitsky /* Find geometry information */ 6407d17c02aSMaxim Levitsky for (i = 0 ; i < ARRAY_SIZE(chs_table) ; i++) { 6417d17c02aSMaxim Levitsky if (chs_table[i].size == size_in_megs) { 6427d17c02aSMaxim Levitsky ftl->cylinders = chs_table[i].cyl; 6437d17c02aSMaxim Levitsky ftl->heads = chs_table[i].head; 6447d17c02aSMaxim Levitsky ftl->sectors = chs_table[i].sec; 6457d17c02aSMaxim Levitsky return 0; 6467d17c02aSMaxim Levitsky } 6477d17c02aSMaxim Levitsky } 6487d17c02aSMaxim Levitsky 6497d17c02aSMaxim Levitsky sm_printk("media has unknown size : %dMiB", size_in_megs); 6507d17c02aSMaxim Levitsky ftl->cylinders = 985; 6517d17c02aSMaxim Levitsky ftl->heads = 33; 6527d17c02aSMaxim Levitsky ftl->sectors = 63; 6537d17c02aSMaxim Levitsky return 0; 6547d17c02aSMaxim Levitsky } 6557d17c02aSMaxim Levitsky 6567d17c02aSMaxim Levitsky /* Validate the CIS */ 6577d17c02aSMaxim Levitsky static int sm_read_cis(struct sm_ftl *ftl) 6587d17c02aSMaxim Levitsky { 6597d17c02aSMaxim Levitsky struct sm_oob oob; 6607d17c02aSMaxim Levitsky 6617d17c02aSMaxim Levitsky if (sm_read_sector(ftl, 6627d17c02aSMaxim Levitsky 0, ftl->cis_block, ftl->cis_boffset, ftl->cis_buffer, &oob)) 6637d17c02aSMaxim Levitsky return -EIO; 6647d17c02aSMaxim Levitsky 6657d17c02aSMaxim Levitsky if (!sm_sector_valid(&oob) || !sm_block_valid(&oob)) 6667d17c02aSMaxim Levitsky return -EIO; 6677d17c02aSMaxim Levitsky 6687d17c02aSMaxim Levitsky if (!memcmp(ftl->cis_buffer + ftl->cis_page_offset, 6697d17c02aSMaxim Levitsky cis_signature, sizeof(cis_signature))) { 6707d17c02aSMaxim Levitsky return 0; 6717d17c02aSMaxim Levitsky } 6727d17c02aSMaxim Levitsky 6737d17c02aSMaxim Levitsky return -EIO; 6747d17c02aSMaxim Levitsky } 6757d17c02aSMaxim Levitsky 6767d17c02aSMaxim Levitsky /* Scan the media for the CIS */ 6777d17c02aSMaxim Levitsky static int sm_find_cis(struct sm_ftl *ftl) 6787d17c02aSMaxim Levitsky { 6797d17c02aSMaxim Levitsky struct sm_oob oob; 6807d17c02aSMaxim Levitsky int block, boffset; 6817d17c02aSMaxim Levitsky int block_found = 0; 6827d17c02aSMaxim Levitsky int cis_found = 0; 6837d17c02aSMaxim Levitsky 6847d17c02aSMaxim Levitsky /* Search for first valid block */ 6857d17c02aSMaxim Levitsky for (block = 0 ; block < ftl->zone_size - ftl->max_lba ; block++) { 6867d17c02aSMaxim Levitsky 6877d17c02aSMaxim Levitsky if (sm_read_sector(ftl, 0, block, 0, NULL, &oob)) 6887d17c02aSMaxim Levitsky continue; 6897d17c02aSMaxim Levitsky 6907d17c02aSMaxim Levitsky if (!sm_block_valid(&oob)) 6917d17c02aSMaxim Levitsky continue; 6927d17c02aSMaxim Levitsky block_found = 1; 6937d17c02aSMaxim Levitsky break; 6947d17c02aSMaxim Levitsky } 6957d17c02aSMaxim Levitsky 6967d17c02aSMaxim Levitsky if (!block_found) 6977d17c02aSMaxim Levitsky return -EIO; 6987d17c02aSMaxim Levitsky 6997d17c02aSMaxim Levitsky /* Search for first valid sector in this block */ 7007d17c02aSMaxim Levitsky for (boffset = 0 ; boffset < ftl->block_size; 7017d17c02aSMaxim Levitsky boffset += SM_SECTOR_SIZE) { 7027d17c02aSMaxim Levitsky 7037d17c02aSMaxim Levitsky if (sm_read_sector(ftl, 0, block, boffset, NULL, &oob)) 7047d17c02aSMaxim Levitsky continue; 7057d17c02aSMaxim Levitsky 7067d17c02aSMaxim Levitsky if (!sm_sector_valid(&oob)) 7077d17c02aSMaxim Levitsky continue; 7087d17c02aSMaxim Levitsky break; 7097d17c02aSMaxim Levitsky } 7107d17c02aSMaxim Levitsky 7117d17c02aSMaxim Levitsky if (boffset == ftl->block_size) 7127d17c02aSMaxim Levitsky return -EIO; 7137d17c02aSMaxim Levitsky 7147d17c02aSMaxim Levitsky ftl->cis_block = block; 7157d17c02aSMaxim Levitsky ftl->cis_boffset = boffset; 7167d17c02aSMaxim Levitsky ftl->cis_page_offset = 0; 7177d17c02aSMaxim Levitsky 7187d17c02aSMaxim Levitsky cis_found = !sm_read_cis(ftl); 7197d17c02aSMaxim Levitsky 7207d17c02aSMaxim Levitsky if (!cis_found) { 7217d17c02aSMaxim Levitsky ftl->cis_page_offset = SM_SMALL_PAGE; 7227d17c02aSMaxim Levitsky cis_found = !sm_read_cis(ftl); 7237d17c02aSMaxim Levitsky } 7247d17c02aSMaxim Levitsky 7257d17c02aSMaxim Levitsky if (cis_found) { 7267d17c02aSMaxim Levitsky dbg("CIS block found at offset %x", 7277d17c02aSMaxim Levitsky block * ftl->block_size + 7287d17c02aSMaxim Levitsky boffset + ftl->cis_page_offset); 7297d17c02aSMaxim Levitsky return 0; 7307d17c02aSMaxim Levitsky } 7317d17c02aSMaxim Levitsky return -EIO; 7327d17c02aSMaxim Levitsky } 7337d17c02aSMaxim Levitsky 7347d17c02aSMaxim Levitsky /* Basic test to determine if underlying mtd device if functional */ 7357d17c02aSMaxim Levitsky static int sm_recheck_media(struct sm_ftl *ftl) 7367d17c02aSMaxim Levitsky { 7377d17c02aSMaxim Levitsky if (sm_read_cis(ftl)) { 7387d17c02aSMaxim Levitsky 7397d17c02aSMaxim Levitsky if (!ftl->unstable) { 7407d17c02aSMaxim Levitsky sm_printk("media unstable, not allowing writes"); 7417d17c02aSMaxim Levitsky ftl->unstable = 1; 7427d17c02aSMaxim Levitsky } 7437d17c02aSMaxim Levitsky return -EIO; 7447d17c02aSMaxim Levitsky } 7457d17c02aSMaxim Levitsky return 0; 7467d17c02aSMaxim Levitsky } 7477d17c02aSMaxim Levitsky 7487d17c02aSMaxim Levitsky /* Initialize a FTL zone */ 7497d17c02aSMaxim Levitsky static int sm_init_zone(struct sm_ftl *ftl, int zone_num) 7507d17c02aSMaxim Levitsky { 7517d17c02aSMaxim Levitsky struct ftl_zone *zone = &ftl->zones[zone_num]; 7527d17c02aSMaxim Levitsky struct sm_oob oob; 7537d17c02aSMaxim Levitsky uint16_t block; 7547d17c02aSMaxim Levitsky int lba; 7557d17c02aSMaxim Levitsky int i = 0; 756133fa8c7SMaxim Levitsky int len; 7577d17c02aSMaxim Levitsky 7587d17c02aSMaxim Levitsky dbg("initializing zone %d", zone_num); 7597d17c02aSMaxim Levitsky 7607d17c02aSMaxim Levitsky /* Allocate memory for FTL table */ 7617d17c02aSMaxim Levitsky zone->lba_to_phys_table = kmalloc(ftl->max_lba * 2, GFP_KERNEL); 7627d17c02aSMaxim Levitsky 7637d17c02aSMaxim Levitsky if (!zone->lba_to_phys_table) 7647d17c02aSMaxim Levitsky return -ENOMEM; 7657d17c02aSMaxim Levitsky memset(zone->lba_to_phys_table, -1, ftl->max_lba * 2); 7667d17c02aSMaxim Levitsky 7677d17c02aSMaxim Levitsky 7687d17c02aSMaxim Levitsky /* Allocate memory for free sectors FIFO */ 7697d17c02aSMaxim Levitsky if (kfifo_alloc(&zone->free_sectors, ftl->zone_size * 2, GFP_KERNEL)) { 7707d17c02aSMaxim Levitsky kfree(zone->lba_to_phys_table); 7717d17c02aSMaxim Levitsky return -ENOMEM; 7727d17c02aSMaxim Levitsky } 7737d17c02aSMaxim Levitsky 7747d17c02aSMaxim Levitsky /* Now scan the zone */ 7757d17c02aSMaxim Levitsky for (block = 0 ; block < ftl->zone_size ; block++) { 7767d17c02aSMaxim Levitsky 7777d17c02aSMaxim Levitsky /* Skip blocks till the CIS (including) */ 7787d17c02aSMaxim Levitsky if (zone_num == 0 && block <= ftl->cis_block) 7797d17c02aSMaxim Levitsky continue; 7807d17c02aSMaxim Levitsky 7817d17c02aSMaxim Levitsky /* Read the oob of first sector */ 7827d17c02aSMaxim Levitsky if (sm_read_sector(ftl, zone_num, block, 0, NULL, &oob)) 7837d17c02aSMaxim Levitsky return -EIO; 7847d17c02aSMaxim Levitsky 7857d17c02aSMaxim Levitsky /* Test to see if block is erased. It is enough to test 7867d17c02aSMaxim Levitsky first sector, because erase happens in one shot */ 7877d17c02aSMaxim Levitsky if (sm_block_erased(&oob)) { 7887d17c02aSMaxim Levitsky kfifo_in(&zone->free_sectors, 7897d17c02aSMaxim Levitsky (unsigned char *)&block, 2); 7907d17c02aSMaxim Levitsky continue; 7917d17c02aSMaxim Levitsky } 7927d17c02aSMaxim Levitsky 7937d17c02aSMaxim Levitsky /* If block is marked as bad, skip it */ 7947d17c02aSMaxim Levitsky /* This assumes we can trust first sector*/ 7957d17c02aSMaxim Levitsky /* However the way the block valid status is defined, ensures 7967d17c02aSMaxim Levitsky very low probability of failure here */ 7977d17c02aSMaxim Levitsky if (!sm_block_valid(&oob)) { 7987d17c02aSMaxim Levitsky dbg("PH %04d <-> <marked bad>", block); 7997d17c02aSMaxim Levitsky continue; 8007d17c02aSMaxim Levitsky } 8017d17c02aSMaxim Levitsky 8027d17c02aSMaxim Levitsky 8037d17c02aSMaxim Levitsky lba = sm_read_lba(&oob); 8047d17c02aSMaxim Levitsky 8057d17c02aSMaxim Levitsky /* Invalid LBA means that block is damaged. */ 8067d17c02aSMaxim Levitsky /* We can try to erase it, or mark it as bad, but 8077d17c02aSMaxim Levitsky lets leave that to recovery application */ 8087d17c02aSMaxim Levitsky if (lba == -2 || lba >= ftl->max_lba) { 8097d17c02aSMaxim Levitsky dbg("PH %04d <-> LBA %04d(bad)", block, lba); 8107d17c02aSMaxim Levitsky continue; 8117d17c02aSMaxim Levitsky } 8127d17c02aSMaxim Levitsky 8137d17c02aSMaxim Levitsky 8147d17c02aSMaxim Levitsky /* If there is no collision, 8157d17c02aSMaxim Levitsky just put the sector in the FTL table */ 8167d17c02aSMaxim Levitsky if (zone->lba_to_phys_table[lba] < 0) { 8177d17c02aSMaxim Levitsky dbg_verbose("PH %04d <-> LBA %04d", block, lba); 8187d17c02aSMaxim Levitsky zone->lba_to_phys_table[lba] = block; 8197d17c02aSMaxim Levitsky continue; 8207d17c02aSMaxim Levitsky } 8217d17c02aSMaxim Levitsky 8227d17c02aSMaxim Levitsky sm_printk("collision" 8237d17c02aSMaxim Levitsky " of LBA %d between blocks %d and %d in zone %d", 8247d17c02aSMaxim Levitsky lba, zone->lba_to_phys_table[lba], block, zone_num); 8257d17c02aSMaxim Levitsky 8267d17c02aSMaxim Levitsky /* Test that this block is valid*/ 8277d17c02aSMaxim Levitsky if (sm_check_block(ftl, zone_num, block)) 8287d17c02aSMaxim Levitsky continue; 8297d17c02aSMaxim Levitsky 8307d17c02aSMaxim Levitsky /* Test now the old block */ 8317d17c02aSMaxim Levitsky if (sm_check_block(ftl, zone_num, 8327d17c02aSMaxim Levitsky zone->lba_to_phys_table[lba])) { 8337d17c02aSMaxim Levitsky zone->lba_to_phys_table[lba] = block; 8347d17c02aSMaxim Levitsky continue; 8357d17c02aSMaxim Levitsky } 8367d17c02aSMaxim Levitsky 8377d17c02aSMaxim Levitsky /* If both blocks are valid and share same LBA, it means that 8387d17c02aSMaxim Levitsky they hold different versions of same data. It not 8397d17c02aSMaxim Levitsky known which is more recent, thus just erase one of them 8407d17c02aSMaxim Levitsky */ 8417d17c02aSMaxim Levitsky sm_printk("both blocks are valid, erasing the later"); 8427d17c02aSMaxim Levitsky sm_erase_block(ftl, zone_num, block, 1); 8437d17c02aSMaxim Levitsky } 8447d17c02aSMaxim Levitsky 8457d17c02aSMaxim Levitsky dbg("zone initialized"); 8467d17c02aSMaxim Levitsky zone->initialized = 1; 8477d17c02aSMaxim Levitsky 8487d17c02aSMaxim Levitsky /* No free sectors, means that the zone is heavily damaged, write won't 8497d17c02aSMaxim Levitsky work, but it can still can be (partially) read */ 8507d17c02aSMaxim Levitsky if (!kfifo_len(&zone->free_sectors)) { 8517d17c02aSMaxim Levitsky sm_printk("no free blocks in zone %d", zone_num); 8527d17c02aSMaxim Levitsky return 0; 8537d17c02aSMaxim Levitsky } 8547d17c02aSMaxim Levitsky 8557d17c02aSMaxim Levitsky /* Randomize first block we write to */ 8567d17c02aSMaxim Levitsky get_random_bytes(&i, 2); 8577d17c02aSMaxim Levitsky i %= (kfifo_len(&zone->free_sectors) / 2); 8587d17c02aSMaxim Levitsky 8597d17c02aSMaxim Levitsky while (i--) { 860133fa8c7SMaxim Levitsky len = kfifo_out(&zone->free_sectors, 861133fa8c7SMaxim Levitsky (unsigned char *)&block, 2); 862133fa8c7SMaxim Levitsky WARN_ON(len != 2); 8637d17c02aSMaxim Levitsky kfifo_in(&zone->free_sectors, (const unsigned char *)&block, 2); 8647d17c02aSMaxim Levitsky } 8657d17c02aSMaxim Levitsky return 0; 8667d17c02aSMaxim Levitsky } 8677d17c02aSMaxim Levitsky 8687d17c02aSMaxim Levitsky /* Get and automaticly initialize an FTL mapping for one zone */ 8697d17c02aSMaxim Levitsky struct ftl_zone *sm_get_zone(struct sm_ftl *ftl, int zone_num) 8707d17c02aSMaxim Levitsky { 8717d17c02aSMaxim Levitsky struct ftl_zone *zone; 8727d17c02aSMaxim Levitsky int error; 8737d17c02aSMaxim Levitsky 8747d17c02aSMaxim Levitsky BUG_ON(zone_num >= ftl->zone_count); 8757d17c02aSMaxim Levitsky zone = &ftl->zones[zone_num]; 8767d17c02aSMaxim Levitsky 8777d17c02aSMaxim Levitsky if (!zone->initialized) { 8787d17c02aSMaxim Levitsky error = sm_init_zone(ftl, zone_num); 8797d17c02aSMaxim Levitsky 8807d17c02aSMaxim Levitsky if (error) 8817d17c02aSMaxim Levitsky return ERR_PTR(error); 8827d17c02aSMaxim Levitsky } 8837d17c02aSMaxim Levitsky return zone; 8847d17c02aSMaxim Levitsky } 8857d17c02aSMaxim Levitsky 8867d17c02aSMaxim Levitsky 8877d17c02aSMaxim Levitsky /* ----------------- cache handling ------------------------------------------*/ 8887d17c02aSMaxim Levitsky 8897d17c02aSMaxim Levitsky /* Initialize the one block cache */ 8907d17c02aSMaxim Levitsky void sm_cache_init(struct sm_ftl *ftl) 8917d17c02aSMaxim Levitsky { 8927d17c02aSMaxim Levitsky ftl->cache_data_invalid_bitmap = 0xFFFFFFFF; 8937d17c02aSMaxim Levitsky ftl->cache_clean = 1; 8947d17c02aSMaxim Levitsky ftl->cache_zone = -1; 8957d17c02aSMaxim Levitsky ftl->cache_block = -1; 8967d17c02aSMaxim Levitsky /*memset(ftl->cache_data, 0xAA, ftl->block_size);*/ 8977d17c02aSMaxim Levitsky } 8987d17c02aSMaxim Levitsky 8997d17c02aSMaxim Levitsky /* Put sector in one block cache */ 9007d17c02aSMaxim Levitsky void sm_cache_put(struct sm_ftl *ftl, char *buffer, int boffset) 9017d17c02aSMaxim Levitsky { 9027d17c02aSMaxim Levitsky memcpy(ftl->cache_data + boffset, buffer, SM_SECTOR_SIZE); 9037d17c02aSMaxim Levitsky clear_bit(boffset / SM_SECTOR_SIZE, &ftl->cache_data_invalid_bitmap); 9047d17c02aSMaxim Levitsky ftl->cache_clean = 0; 9057d17c02aSMaxim Levitsky } 9067d17c02aSMaxim Levitsky 9077d17c02aSMaxim Levitsky /* Read a sector from the cache */ 9087d17c02aSMaxim Levitsky int sm_cache_get(struct sm_ftl *ftl, char *buffer, int boffset) 9097d17c02aSMaxim Levitsky { 9107d17c02aSMaxim Levitsky if (test_bit(boffset / SM_SECTOR_SIZE, 9117d17c02aSMaxim Levitsky &ftl->cache_data_invalid_bitmap)) 9127d17c02aSMaxim Levitsky return -1; 9137d17c02aSMaxim Levitsky 9147d17c02aSMaxim Levitsky memcpy(buffer, ftl->cache_data + boffset, SM_SECTOR_SIZE); 9157d17c02aSMaxim Levitsky return 0; 9167d17c02aSMaxim Levitsky } 9177d17c02aSMaxim Levitsky 9187d17c02aSMaxim Levitsky /* Write the cache to hardware */ 9197d17c02aSMaxim Levitsky int sm_cache_flush(struct sm_ftl *ftl) 9207d17c02aSMaxim Levitsky { 9217d17c02aSMaxim Levitsky struct ftl_zone *zone; 9227d17c02aSMaxim Levitsky 9237d17c02aSMaxim Levitsky int sector_num; 9247d17c02aSMaxim Levitsky uint16_t write_sector; 9257d17c02aSMaxim Levitsky int zone_num = ftl->cache_zone; 9267d17c02aSMaxim Levitsky int block_num; 9277d17c02aSMaxim Levitsky 9287d17c02aSMaxim Levitsky if (ftl->cache_clean) 9297d17c02aSMaxim Levitsky return 0; 9307d17c02aSMaxim Levitsky 9317d17c02aSMaxim Levitsky if (ftl->unstable) 9327d17c02aSMaxim Levitsky return -EIO; 9337d17c02aSMaxim Levitsky 9347d17c02aSMaxim Levitsky BUG_ON(zone_num < 0); 9357d17c02aSMaxim Levitsky zone = &ftl->zones[zone_num]; 9367d17c02aSMaxim Levitsky block_num = zone->lba_to_phys_table[ftl->cache_block]; 9377d17c02aSMaxim Levitsky 9387d17c02aSMaxim Levitsky 9397d17c02aSMaxim Levitsky /* Try to read all unread areas of the cache block*/ 9407d17c02aSMaxim Levitsky for_each_bit(sector_num, &ftl->cache_data_invalid_bitmap, 9417d17c02aSMaxim Levitsky ftl->block_size / SM_SECTOR_SIZE) { 9427d17c02aSMaxim Levitsky 9437d17c02aSMaxim Levitsky if (!sm_read_sector(ftl, 9447d17c02aSMaxim Levitsky zone_num, block_num, sector_num * SM_SECTOR_SIZE, 9457d17c02aSMaxim Levitsky ftl->cache_data + sector_num * SM_SECTOR_SIZE, NULL)) 9467d17c02aSMaxim Levitsky clear_bit(sector_num, 9477d17c02aSMaxim Levitsky &ftl->cache_data_invalid_bitmap); 9487d17c02aSMaxim Levitsky } 9497d17c02aSMaxim Levitsky restart: 9507d17c02aSMaxim Levitsky 9517d17c02aSMaxim Levitsky if (ftl->unstable) 9527d17c02aSMaxim Levitsky return -EIO; 953133fa8c7SMaxim Levitsky 954133fa8c7SMaxim Levitsky /* If there are no spare blocks, */ 955133fa8c7SMaxim Levitsky /* we could still continue by erasing/writing the current block, 9567d17c02aSMaxim Levitsky but for such worn out media it doesn't worth the trouble, 9577d17c02aSMaxim Levitsky and the dangers */ 958133fa8c7SMaxim Levitsky if (kfifo_out(&zone->free_sectors, 959133fa8c7SMaxim Levitsky (unsigned char *)&write_sector, 2) != 2) { 9607d17c02aSMaxim Levitsky dbg("no free sectors for write!"); 9617d17c02aSMaxim Levitsky return -EIO; 9627d17c02aSMaxim Levitsky } 9637d17c02aSMaxim Levitsky 9647d17c02aSMaxim Levitsky 9657d17c02aSMaxim Levitsky if (sm_write_block(ftl, ftl->cache_data, zone_num, write_sector, 9667d17c02aSMaxim Levitsky ftl->cache_block, ftl->cache_data_invalid_bitmap)) 9677d17c02aSMaxim Levitsky goto restart; 9687d17c02aSMaxim Levitsky 9697d17c02aSMaxim Levitsky /* Update the FTL table */ 9707d17c02aSMaxim Levitsky zone->lba_to_phys_table[ftl->cache_block] = write_sector; 9717d17c02aSMaxim Levitsky 9727d17c02aSMaxim Levitsky /* Write succesfull, so erase and free the old block */ 9737d17c02aSMaxim Levitsky if (block_num > 0) 9747d17c02aSMaxim Levitsky sm_erase_block(ftl, zone_num, block_num, 1); 9757d17c02aSMaxim Levitsky 9767d17c02aSMaxim Levitsky sm_cache_init(ftl); 9777d17c02aSMaxim Levitsky return 0; 9787d17c02aSMaxim Levitsky } 9797d17c02aSMaxim Levitsky 9807d17c02aSMaxim Levitsky 9817d17c02aSMaxim Levitsky /* flush timer, runs a second after last write */ 9827d17c02aSMaxim Levitsky static void sm_cache_flush_timer(unsigned long data) 9837d17c02aSMaxim Levitsky { 9847d17c02aSMaxim Levitsky struct sm_ftl *ftl = (struct sm_ftl *)data; 9857d17c02aSMaxim Levitsky queue_work(cache_flush_workqueue, &ftl->flush_work); 9867d17c02aSMaxim Levitsky } 9877d17c02aSMaxim Levitsky 9887d17c02aSMaxim Levitsky /* cache flush work, kicked by timer */ 9897d17c02aSMaxim Levitsky static void sm_cache_flush_work(struct work_struct *work) 9907d17c02aSMaxim Levitsky { 9917d17c02aSMaxim Levitsky struct sm_ftl *ftl = container_of(work, struct sm_ftl, flush_work); 9927d17c02aSMaxim Levitsky mutex_lock(&ftl->mutex); 9937d17c02aSMaxim Levitsky sm_cache_flush(ftl); 9947d17c02aSMaxim Levitsky mutex_unlock(&ftl->mutex); 9957d17c02aSMaxim Levitsky return; 9967d17c02aSMaxim Levitsky } 9977d17c02aSMaxim Levitsky 9987d17c02aSMaxim Levitsky /* ---------------- outside interface -------------------------------------- */ 9997d17c02aSMaxim Levitsky 10007d17c02aSMaxim Levitsky /* outside interface: read a sector */ 10017d17c02aSMaxim Levitsky static int sm_read(struct mtd_blktrans_dev *dev, 10027d17c02aSMaxim Levitsky unsigned long sect_no, char *buf) 10037d17c02aSMaxim Levitsky { 10047d17c02aSMaxim Levitsky struct sm_ftl *ftl = dev->priv; 10057d17c02aSMaxim Levitsky struct ftl_zone *zone; 10067d17c02aSMaxim Levitsky int error = 0, in_cache = 0; 10077d17c02aSMaxim Levitsky int zone_num, block, boffset; 10087d17c02aSMaxim Levitsky 10097d17c02aSMaxim Levitsky sm_break_offset(ftl, sect_no << 9, &zone_num, &block, &boffset); 10107d17c02aSMaxim Levitsky mutex_lock(&ftl->mutex); 10117d17c02aSMaxim Levitsky 10127d17c02aSMaxim Levitsky 10137d17c02aSMaxim Levitsky zone = sm_get_zone(ftl, zone_num); 10147d17c02aSMaxim Levitsky if (IS_ERR(zone)) { 10157d17c02aSMaxim Levitsky error = PTR_ERR(zone); 10167d17c02aSMaxim Levitsky goto unlock; 10177d17c02aSMaxim Levitsky } 10187d17c02aSMaxim Levitsky 10197d17c02aSMaxim Levitsky /* Have to look at cache first */ 10207d17c02aSMaxim Levitsky if (ftl->cache_zone == zone_num && ftl->cache_block == block) { 10217d17c02aSMaxim Levitsky in_cache = 1; 10227d17c02aSMaxim Levitsky if (!sm_cache_get(ftl, buf, boffset)) 10237d17c02aSMaxim Levitsky goto unlock; 10247d17c02aSMaxim Levitsky } 10257d17c02aSMaxim Levitsky 10267d17c02aSMaxim Levitsky /* Translate the block and return if doesn't exist in the table */ 10277d17c02aSMaxim Levitsky block = zone->lba_to_phys_table[block]; 10287d17c02aSMaxim Levitsky 10297d17c02aSMaxim Levitsky if (block == -1) { 10307d17c02aSMaxim Levitsky memset(buf, 0xFF, SM_SECTOR_SIZE); 10317d17c02aSMaxim Levitsky goto unlock; 10327d17c02aSMaxim Levitsky } 10337d17c02aSMaxim Levitsky 10347d17c02aSMaxim Levitsky if (sm_read_sector(ftl, zone_num, block, boffset, buf, NULL)) { 10357d17c02aSMaxim Levitsky error = -EIO; 10367d17c02aSMaxim Levitsky goto unlock; 10377d17c02aSMaxim Levitsky } 10387d17c02aSMaxim Levitsky 10397d17c02aSMaxim Levitsky if (in_cache) 10407d17c02aSMaxim Levitsky sm_cache_put(ftl, buf, boffset); 10417d17c02aSMaxim Levitsky unlock: 10427d17c02aSMaxim Levitsky mutex_unlock(&ftl->mutex); 10437d17c02aSMaxim Levitsky return error; 10447d17c02aSMaxim Levitsky } 10457d17c02aSMaxim Levitsky 10467d17c02aSMaxim Levitsky /* outside interface: write a sector */ 10477d17c02aSMaxim Levitsky static int sm_write(struct mtd_blktrans_dev *dev, 10487d17c02aSMaxim Levitsky unsigned long sec_no, char *buf) 10497d17c02aSMaxim Levitsky { 10507d17c02aSMaxim Levitsky struct sm_ftl *ftl = dev->priv; 10517d17c02aSMaxim Levitsky struct ftl_zone *zone; 10527d17c02aSMaxim Levitsky int error, zone_num, block, boffset; 10537d17c02aSMaxim Levitsky 10547d17c02aSMaxim Levitsky BUG_ON(ftl->readonly); 10557d17c02aSMaxim Levitsky sm_break_offset(ftl, sec_no << 9, &zone_num, &block, &boffset); 10567d17c02aSMaxim Levitsky 10577d17c02aSMaxim Levitsky /* No need in flush thread running now */ 10587d17c02aSMaxim Levitsky del_timer(&ftl->timer); 10597d17c02aSMaxim Levitsky mutex_lock(&ftl->mutex); 10607d17c02aSMaxim Levitsky 10617d17c02aSMaxim Levitsky zone = sm_get_zone(ftl, zone_num); 10627d17c02aSMaxim Levitsky if (IS_ERR(zone)) { 10637d17c02aSMaxim Levitsky error = PTR_ERR(zone); 10647d17c02aSMaxim Levitsky goto unlock; 10657d17c02aSMaxim Levitsky } 10667d17c02aSMaxim Levitsky 10677d17c02aSMaxim Levitsky /* If entry is not in cache, flush it */ 10687d17c02aSMaxim Levitsky if (ftl->cache_block != block || ftl->cache_zone != zone_num) { 10697d17c02aSMaxim Levitsky 10707d17c02aSMaxim Levitsky error = sm_cache_flush(ftl); 10717d17c02aSMaxim Levitsky if (error) 10727d17c02aSMaxim Levitsky goto unlock; 10737d17c02aSMaxim Levitsky 10747d17c02aSMaxim Levitsky ftl->cache_block = block; 10757d17c02aSMaxim Levitsky ftl->cache_zone = zone_num; 10767d17c02aSMaxim Levitsky } 10777d17c02aSMaxim Levitsky 10787d17c02aSMaxim Levitsky sm_cache_put(ftl, buf, boffset); 10797d17c02aSMaxim Levitsky unlock: 10807d17c02aSMaxim Levitsky mod_timer(&ftl->timer, jiffies + msecs_to_jiffies(cache_timeout)); 10817d17c02aSMaxim Levitsky mutex_unlock(&ftl->mutex); 10827d17c02aSMaxim Levitsky return error; 10837d17c02aSMaxim Levitsky } 10847d17c02aSMaxim Levitsky 10857d17c02aSMaxim Levitsky /* outside interface: flush everything */ 10867d17c02aSMaxim Levitsky static int sm_flush(struct mtd_blktrans_dev *dev) 10877d17c02aSMaxim Levitsky { 10887d17c02aSMaxim Levitsky struct sm_ftl *ftl = dev->priv; 10897d17c02aSMaxim Levitsky int retval; 10907d17c02aSMaxim Levitsky 10917d17c02aSMaxim Levitsky mutex_lock(&ftl->mutex); 10927d17c02aSMaxim Levitsky retval = sm_cache_flush(ftl); 10937d17c02aSMaxim Levitsky mutex_unlock(&ftl->mutex); 10947d17c02aSMaxim Levitsky return retval; 10957d17c02aSMaxim Levitsky } 10967d17c02aSMaxim Levitsky 10977d17c02aSMaxim Levitsky /* outside interface: device is released */ 10987d17c02aSMaxim Levitsky static int sm_release(struct mtd_blktrans_dev *dev) 10997d17c02aSMaxim Levitsky { 11007d17c02aSMaxim Levitsky struct sm_ftl *ftl = dev->priv; 11017d17c02aSMaxim Levitsky 11027d17c02aSMaxim Levitsky mutex_lock(&ftl->mutex); 11037d17c02aSMaxim Levitsky del_timer_sync(&ftl->timer); 11047d17c02aSMaxim Levitsky cancel_work_sync(&ftl->flush_work); 11057d17c02aSMaxim Levitsky sm_cache_flush(ftl); 11067d17c02aSMaxim Levitsky mutex_unlock(&ftl->mutex); 11077d17c02aSMaxim Levitsky return 0; 11087d17c02aSMaxim Levitsky } 11097d17c02aSMaxim Levitsky 11107d17c02aSMaxim Levitsky /* outside interface: get geometry */ 11117d17c02aSMaxim Levitsky static int sm_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo) 11127d17c02aSMaxim Levitsky { 11137d17c02aSMaxim Levitsky struct sm_ftl *ftl = dev->priv; 11147d17c02aSMaxim Levitsky geo->heads = ftl->heads; 11157d17c02aSMaxim Levitsky geo->sectors = ftl->sectors; 11167d17c02aSMaxim Levitsky geo->cylinders = ftl->cylinders; 11177d17c02aSMaxim Levitsky return 0; 11187d17c02aSMaxim Levitsky } 11197d17c02aSMaxim Levitsky 11207d17c02aSMaxim Levitsky /* external interface: main initialization function */ 11217d17c02aSMaxim Levitsky static void sm_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd) 11227d17c02aSMaxim Levitsky { 11237d17c02aSMaxim Levitsky struct mtd_blktrans_dev *trans; 11247d17c02aSMaxim Levitsky struct sm_ftl *ftl; 11257d17c02aSMaxim Levitsky 11267d17c02aSMaxim Levitsky /* Allocate & initialize our private structure */ 11277d17c02aSMaxim Levitsky ftl = kzalloc(sizeof(struct sm_ftl), GFP_KERNEL); 11287d17c02aSMaxim Levitsky if (!ftl) 11297d17c02aSMaxim Levitsky goto error1; 11307d17c02aSMaxim Levitsky 11317d17c02aSMaxim Levitsky 11327d17c02aSMaxim Levitsky mutex_init(&ftl->mutex); 11337d17c02aSMaxim Levitsky setup_timer(&ftl->timer, sm_cache_flush_timer, (unsigned long)ftl); 11347d17c02aSMaxim Levitsky INIT_WORK(&ftl->flush_work, sm_cache_flush_work); 11357d17c02aSMaxim Levitsky init_completion(&ftl->erase_completion); 11367d17c02aSMaxim Levitsky 11377d17c02aSMaxim Levitsky /* Read media information */ 11387d17c02aSMaxim Levitsky if (sm_get_media_info(ftl, mtd)) { 11397d17c02aSMaxim Levitsky dbg("found unsupported mtd device, aborting"); 11407d17c02aSMaxim Levitsky goto error2; 11417d17c02aSMaxim Levitsky } 11427d17c02aSMaxim Levitsky 11437d17c02aSMaxim Levitsky 11447d17c02aSMaxim Levitsky /* Allocate temporary CIS buffer for read retry support */ 11457d17c02aSMaxim Levitsky ftl->cis_buffer = kzalloc(SM_SECTOR_SIZE, GFP_KERNEL); 11467d17c02aSMaxim Levitsky if (!ftl->cis_buffer) 11477d17c02aSMaxim Levitsky goto error2; 11487d17c02aSMaxim Levitsky 11497d17c02aSMaxim Levitsky /* Allocate zone array, it will be initialized on demand */ 11507d17c02aSMaxim Levitsky ftl->zones = kzalloc(sizeof(struct ftl_zone) * ftl->zone_count, 11517d17c02aSMaxim Levitsky GFP_KERNEL); 11527d17c02aSMaxim Levitsky if (!ftl->zones) 11537d17c02aSMaxim Levitsky goto error3; 11547d17c02aSMaxim Levitsky 11557d17c02aSMaxim Levitsky /* Allocate the cache*/ 11567d17c02aSMaxim Levitsky ftl->cache_data = kzalloc(ftl->block_size, GFP_KERNEL); 11577d17c02aSMaxim Levitsky 11587d17c02aSMaxim Levitsky if (!ftl->cache_data) 11597d17c02aSMaxim Levitsky goto error4; 11607d17c02aSMaxim Levitsky 11617d17c02aSMaxim Levitsky sm_cache_init(ftl); 11627d17c02aSMaxim Levitsky 11637d17c02aSMaxim Levitsky 11647d17c02aSMaxim Levitsky /* Allocate upper layer structure and initialize it */ 11657d17c02aSMaxim Levitsky trans = kzalloc(sizeof(struct mtd_blktrans_dev), GFP_KERNEL); 11667d17c02aSMaxim Levitsky if (!trans) 11677d17c02aSMaxim Levitsky goto error5; 11687d17c02aSMaxim Levitsky 11697d17c02aSMaxim Levitsky ftl->trans = trans; 11707d17c02aSMaxim Levitsky trans->priv = ftl; 11717d17c02aSMaxim Levitsky 11727d17c02aSMaxim Levitsky trans->tr = tr; 11737d17c02aSMaxim Levitsky trans->mtd = mtd; 11747d17c02aSMaxim Levitsky trans->devnum = -1; 11757d17c02aSMaxim Levitsky trans->size = (ftl->block_size * ftl->max_lba * ftl->zone_count) >> 9; 11767d17c02aSMaxim Levitsky trans->readonly = ftl->readonly; 11777d17c02aSMaxim Levitsky 11787d17c02aSMaxim Levitsky if (sm_find_cis(ftl)) { 11797d17c02aSMaxim Levitsky dbg("CIS not found on mtd device, aborting"); 11807d17c02aSMaxim Levitsky goto error6; 11817d17c02aSMaxim Levitsky } 11827d17c02aSMaxim Levitsky 11837d17c02aSMaxim Levitsky ftl->disk_attributes = sm_create_sysfs_attributes(ftl); 11847d17c02aSMaxim Levitsky trans->disk_attributes = ftl->disk_attributes; 11857d17c02aSMaxim Levitsky 11867d17c02aSMaxim Levitsky sm_printk("Found %d MiB xD/SmartMedia FTL on mtd%d", 11877d17c02aSMaxim Levitsky (int)(mtd->size / (1024 * 1024)), mtd->index); 11887d17c02aSMaxim Levitsky 11897d17c02aSMaxim Levitsky dbg("FTL layout:"); 11907d17c02aSMaxim Levitsky dbg("%d zone(s), each consists of %d blocks (+%d spares)", 11917d17c02aSMaxim Levitsky ftl->zone_count, ftl->max_lba, 11927d17c02aSMaxim Levitsky ftl->zone_size - ftl->max_lba); 11937d17c02aSMaxim Levitsky dbg("each block consists of %d bytes", 11947d17c02aSMaxim Levitsky ftl->block_size); 11957d17c02aSMaxim Levitsky 11967d17c02aSMaxim Levitsky 11977d17c02aSMaxim Levitsky /* Register device*/ 11987d17c02aSMaxim Levitsky if (add_mtd_blktrans_dev(trans)) { 11997d17c02aSMaxim Levitsky dbg("error in mtdblktrans layer"); 12007d17c02aSMaxim Levitsky goto error6; 12017d17c02aSMaxim Levitsky } 12027d17c02aSMaxim Levitsky return; 12037d17c02aSMaxim Levitsky error6: 12047d17c02aSMaxim Levitsky kfree(trans); 12057d17c02aSMaxim Levitsky error5: 12067d17c02aSMaxim Levitsky kfree(ftl->cache_data); 12077d17c02aSMaxim Levitsky error4: 12087d17c02aSMaxim Levitsky kfree(ftl->zones); 12097d17c02aSMaxim Levitsky error3: 12107d17c02aSMaxim Levitsky kfree(ftl->cis_buffer); 12117d17c02aSMaxim Levitsky error2: 12127d17c02aSMaxim Levitsky kfree(ftl); 12137d17c02aSMaxim Levitsky error1: 12147d17c02aSMaxim Levitsky return; 12157d17c02aSMaxim Levitsky } 12167d17c02aSMaxim Levitsky 12177d17c02aSMaxim Levitsky /* main interface: device {surprise,} removal */ 12187d17c02aSMaxim Levitsky static void sm_remove_dev(struct mtd_blktrans_dev *dev) 12197d17c02aSMaxim Levitsky { 12207d17c02aSMaxim Levitsky struct sm_ftl *ftl = dev->priv; 12217d17c02aSMaxim Levitsky int i; 12227d17c02aSMaxim Levitsky 12237d17c02aSMaxim Levitsky del_mtd_blktrans_dev(dev); 12247d17c02aSMaxim Levitsky ftl->trans = NULL; 12257d17c02aSMaxim Levitsky 12267d17c02aSMaxim Levitsky for (i = 0 ; i < ftl->zone_count; i++) { 12277d17c02aSMaxim Levitsky 12287d17c02aSMaxim Levitsky if (!ftl->zones[i].initialized) 12297d17c02aSMaxim Levitsky continue; 12307d17c02aSMaxim Levitsky 12317d17c02aSMaxim Levitsky kfree(ftl->zones[i].lba_to_phys_table); 12327d17c02aSMaxim Levitsky kfifo_free(&ftl->zones[i].free_sectors); 12337d17c02aSMaxim Levitsky } 12347d17c02aSMaxim Levitsky 12357d17c02aSMaxim Levitsky sm_delete_sysfs_attributes(ftl); 12367d17c02aSMaxim Levitsky kfree(ftl->cis_buffer); 12377d17c02aSMaxim Levitsky kfree(ftl->zones); 12387d17c02aSMaxim Levitsky kfree(ftl->cache_data); 12397d17c02aSMaxim Levitsky kfree(ftl); 12407d17c02aSMaxim Levitsky } 12417d17c02aSMaxim Levitsky 12427d17c02aSMaxim Levitsky static struct mtd_blktrans_ops sm_ftl_ops = { 12437d17c02aSMaxim Levitsky .name = "smblk", 12447d17c02aSMaxim Levitsky .major = -1, 12457d17c02aSMaxim Levitsky .part_bits = SM_FTL_PARTN_BITS, 12467d17c02aSMaxim Levitsky .blksize = SM_SECTOR_SIZE, 12477d17c02aSMaxim Levitsky .getgeo = sm_getgeo, 12487d17c02aSMaxim Levitsky 12497d17c02aSMaxim Levitsky .add_mtd = sm_add_mtd, 12507d17c02aSMaxim Levitsky .remove_dev = sm_remove_dev, 12517d17c02aSMaxim Levitsky 12527d17c02aSMaxim Levitsky .readsect = sm_read, 12537d17c02aSMaxim Levitsky .writesect = sm_write, 12547d17c02aSMaxim Levitsky 12557d17c02aSMaxim Levitsky .flush = sm_flush, 12567d17c02aSMaxim Levitsky .release = sm_release, 12577d17c02aSMaxim Levitsky 12587d17c02aSMaxim Levitsky .owner = THIS_MODULE, 12597d17c02aSMaxim Levitsky }; 12607d17c02aSMaxim Levitsky 12617d17c02aSMaxim Levitsky static __init int sm_module_init(void) 12627d17c02aSMaxim Levitsky { 12637d17c02aSMaxim Levitsky int error = 0; 12647d17c02aSMaxim Levitsky cache_flush_workqueue = create_freezeable_workqueue("smflush"); 12657d17c02aSMaxim Levitsky 12667d17c02aSMaxim Levitsky if (IS_ERR(cache_flush_workqueue)) 12677d17c02aSMaxim Levitsky return PTR_ERR(cache_flush_workqueue); 12687d17c02aSMaxim Levitsky 12697d17c02aSMaxim Levitsky error = register_mtd_blktrans(&sm_ftl_ops); 12707d17c02aSMaxim Levitsky if (error) 12717d17c02aSMaxim Levitsky destroy_workqueue(cache_flush_workqueue); 12727d17c02aSMaxim Levitsky return error; 12737d17c02aSMaxim Levitsky 12747d17c02aSMaxim Levitsky } 12757d17c02aSMaxim Levitsky 12767d17c02aSMaxim Levitsky static void __exit sm_module_exit(void) 12777d17c02aSMaxim Levitsky { 12787d17c02aSMaxim Levitsky destroy_workqueue(cache_flush_workqueue); 12797d17c02aSMaxim Levitsky deregister_mtd_blktrans(&sm_ftl_ops); 12807d17c02aSMaxim Levitsky } 12817d17c02aSMaxim Levitsky 12827d17c02aSMaxim Levitsky module_init(sm_module_init); 12837d17c02aSMaxim Levitsky module_exit(sm_module_exit); 12847d17c02aSMaxim Levitsky 12857d17c02aSMaxim Levitsky MODULE_LICENSE("GPL"); 12867d17c02aSMaxim Levitsky MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>"); 12877d17c02aSMaxim Levitsky MODULE_DESCRIPTION("Smartmedia/xD mtd translation layer"); 1288