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