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