sm_ftl.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright © 2009 - Maxim Levitsky
  4. * SmartMedia/xD translation layer
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/random.h>
  9. #include <linux/hdreg.h>
  10. #include <linux/kthread.h>
  11. #include <linux/freezer.h>
  12. #include <linux/sysfs.h>
  13. #include <linux/bitops.h>
  14. #include <linux/slab.h>
  15. #include <linux/mtd/nand-ecc-sw-hamming.h>
  16. #include "nand/raw/sm_common.h"
  17. #include "sm_ftl.h"
  18. static struct workqueue_struct *cache_flush_workqueue;
  19. static int cache_timeout = 1000;
  20. module_param(cache_timeout, int, S_IRUGO);
  21. MODULE_PARM_DESC(cache_timeout,
  22. "Timeout (in ms) for cache flush (1000 ms default");
  23. static int debug;
  24. module_param(debug, int, S_IRUGO | S_IWUSR);
  25. MODULE_PARM_DESC(debug, "Debug level (0-2)");
  26. /* ------------------- sysfs attributes ---------------------------------- */
  27. struct sm_sysfs_attribute {
  28. struct device_attribute dev_attr;
  29. char *data;
  30. int len;
  31. };
  32. static ssize_t sm_attr_show(struct device *dev, struct device_attribute *attr,
  33. char *buf)
  34. {
  35. struct sm_sysfs_attribute *sm_attr =
  36. container_of(attr, struct sm_sysfs_attribute, dev_attr);
  37. strncpy(buf, sm_attr->data, sm_attr->len);
  38. return sm_attr->len;
  39. }
  40. #define NUM_ATTRIBUTES 1
  41. #define SM_CIS_VENDOR_OFFSET 0x59
  42. static struct attribute_group *sm_create_sysfs_attributes(struct sm_ftl *ftl)
  43. {
  44. struct attribute_group *attr_group;
  45. struct attribute **attributes;
  46. struct sm_sysfs_attribute *vendor_attribute;
  47. char *vendor;
  48. vendor = kstrndup(ftl->cis_buffer + SM_CIS_VENDOR_OFFSET,
  49. SM_SMALL_PAGE - SM_CIS_VENDOR_OFFSET, GFP_KERNEL);
  50. if (!vendor)
  51. goto error1;
  52. /* Initialize sysfs attributes */
  53. vendor_attribute =
  54. kzalloc(sizeof(struct sm_sysfs_attribute), GFP_KERNEL);
  55. if (!vendor_attribute)
  56. goto error2;
  57. sysfs_attr_init(&vendor_attribute->dev_attr.attr);
  58. vendor_attribute->data = vendor;
  59. vendor_attribute->len = strlen(vendor);
  60. vendor_attribute->dev_attr.attr.name = "vendor";
  61. vendor_attribute->dev_attr.attr.mode = S_IRUGO;
  62. vendor_attribute->dev_attr.show = sm_attr_show;
  63. /* Create array of pointers to the attributes */
  64. attributes = kcalloc(NUM_ATTRIBUTES + 1, sizeof(struct attribute *),
  65. GFP_KERNEL);
  66. if (!attributes)
  67. goto error3;
  68. attributes[0] = &vendor_attribute->dev_attr.attr;
  69. /* Finally create the attribute group */
  70. attr_group = kzalloc(sizeof(struct attribute_group), GFP_KERNEL);
  71. if (!attr_group)
  72. goto error4;
  73. attr_group->attrs = attributes;
  74. return attr_group;
  75. error4:
  76. kfree(attributes);
  77. error3:
  78. kfree(vendor_attribute);
  79. error2:
  80. kfree(vendor);
  81. error1:
  82. return NULL;
  83. }
  84. static void sm_delete_sysfs_attributes(struct sm_ftl *ftl)
  85. {
  86. struct attribute **attributes = ftl->disk_attributes->attrs;
  87. int i;
  88. for (i = 0; attributes[i] ; i++) {
  89. struct device_attribute *dev_attr = container_of(attributes[i],
  90. struct device_attribute, attr);
  91. struct sm_sysfs_attribute *sm_attr =
  92. container_of(dev_attr,
  93. struct sm_sysfs_attribute, dev_attr);
  94. kfree(sm_attr->data);
  95. kfree(sm_attr);
  96. }
  97. kfree(ftl->disk_attributes->attrs);
  98. kfree(ftl->disk_attributes);
  99. }
  100. /* ----------------------- oob helpers -------------------------------------- */
  101. static int sm_get_lba(uint8_t *lba)
  102. {
  103. /* check fixed bits */
  104. if ((lba[0] & 0xF8) != 0x10)
  105. return -2;
  106. /* check parity - endianness doesn't matter */
  107. if (hweight16(*(uint16_t *)lba) & 1)
  108. return -2;
  109. return (lba[1] >> 1) | ((lba[0] & 0x07) << 7);
  110. }
  111. /*
  112. * Read LBA associated with block
  113. * returns -1, if block is erased
  114. * returns -2 if error happens
  115. */
  116. static int sm_read_lba(struct sm_oob *oob)
  117. {
  118. static const uint32_t erased_pattern[4] = {
  119. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
  120. uint16_t lba_test;
  121. int lba;
  122. /* First test for erased block */
  123. if (!memcmp(oob, erased_pattern, SM_OOB_SIZE))
  124. return -1;
  125. /* Now check is both copies of the LBA differ too much */
  126. lba_test = *(uint16_t *)oob->lba_copy1 ^ *(uint16_t*)oob->lba_copy2;
  127. if (lba_test && !is_power_of_2(lba_test))
  128. return -2;
  129. /* And read it */
  130. lba = sm_get_lba(oob->lba_copy1);
  131. if (lba == -2)
  132. lba = sm_get_lba(oob->lba_copy2);
  133. return lba;
  134. }
  135. static void sm_write_lba(struct sm_oob *oob, uint16_t lba)
  136. {
  137. uint8_t tmp[2];
  138. WARN_ON(lba >= 1000);
  139. tmp[0] = 0x10 | ((lba >> 7) & 0x07);
  140. tmp[1] = (lba << 1) & 0xFF;
  141. if (hweight16(*(uint16_t *)tmp) & 0x01)
  142. tmp[1] |= 1;
  143. oob->lba_copy1[0] = oob->lba_copy2[0] = tmp[0];
  144. oob->lba_copy1[1] = oob->lba_copy2[1] = tmp[1];
  145. }
  146. /* Make offset from parts */
  147. static loff_t sm_mkoffset(struct sm_ftl *ftl, int zone, int block, int boffset)
  148. {
  149. WARN_ON(boffset & (SM_SECTOR_SIZE - 1));
  150. WARN_ON(zone < 0 || zone >= ftl->zone_count);
  151. WARN_ON(block >= ftl->zone_size);
  152. WARN_ON(boffset >= ftl->block_size);
  153. if (block == -1)
  154. return -1;
  155. return (zone * SM_MAX_ZONE_SIZE + block) * ftl->block_size + boffset;
  156. }
  157. /* Breaks offset into parts */
  158. static void sm_break_offset(struct sm_ftl *ftl, loff_t loffset,
  159. int *zone, int *block, int *boffset)
  160. {
  161. u64 offset = loffset;
  162. *boffset = do_div(offset, ftl->block_size);
  163. *block = do_div(offset, ftl->max_lba);
  164. *zone = offset >= ftl->zone_count ? -1 : offset;
  165. }
  166. /* ---------------------- low level IO ------------------------------------- */
  167. static int sm_correct_sector(uint8_t *buffer, struct sm_oob *oob)
  168. {
  169. bool sm_order = IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_HAMMING_SMC);
  170. uint8_t ecc[3];
  171. ecc_sw_hamming_calculate(buffer, SM_SMALL_PAGE, ecc, sm_order);
  172. if (ecc_sw_hamming_correct(buffer, ecc, oob->ecc1, SM_SMALL_PAGE,
  173. sm_order) < 0)
  174. return -EIO;
  175. buffer += SM_SMALL_PAGE;
  176. ecc_sw_hamming_calculate(buffer, SM_SMALL_PAGE, ecc, sm_order);
  177. if (ecc_sw_hamming_correct(buffer, ecc, oob->ecc2, SM_SMALL_PAGE,
  178. sm_order) < 0)
  179. return -EIO;
  180. return 0;
  181. }
  182. /* Reads a sector + oob*/
  183. static int sm_read_sector(struct sm_ftl *ftl,
  184. int zone, int block, int boffset,
  185. uint8_t *buffer, struct sm_oob *oob)
  186. {
  187. struct mtd_info *mtd = ftl->trans->mtd;
  188. struct mtd_oob_ops ops = { };
  189. struct sm_oob tmp_oob;
  190. int ret = -EIO;
  191. int try = 0;
  192. /* FTL can contain -1 entries that are by default filled with bits */
  193. if (block == -1) {
  194. if (buffer)
  195. memset(buffer, 0xFF, SM_SECTOR_SIZE);
  196. return 0;
  197. }
  198. /* User might not need the oob, but we do for data verification */
  199. if (!oob)
  200. oob = &tmp_oob;
  201. ops.mode = ftl->smallpagenand ? MTD_OPS_RAW : MTD_OPS_PLACE_OOB;
  202. ops.ooboffs = 0;
  203. ops.ooblen = SM_OOB_SIZE;
  204. ops.oobbuf = (void *)oob;
  205. ops.len = SM_SECTOR_SIZE;
  206. ops.datbuf = buffer;
  207. again:
  208. if (try++) {
  209. /* Avoid infinite recursion on CIS reads, sm_recheck_media
  210. * won't help anyway
  211. */
  212. if (zone == 0 && block == ftl->cis_block && boffset ==
  213. ftl->cis_boffset)
  214. return ret;
  215. /* Test if media is stable */
  216. if (try == 3 || sm_recheck_media(ftl))
  217. return ret;
  218. }
  219. /* Unfortunately, oob read will _always_ succeed,
  220. * despite card removal.....
  221. */
  222. ret = mtd_read_oob(mtd, sm_mkoffset(ftl, zone, block, boffset), &ops);
  223. /* Test for unknown errors */
  224. if (ret != 0 && !mtd_is_bitflip_or_eccerr(ret)) {
  225. dbg("read of block %d at zone %d, failed due to error (%d)",
  226. block, zone, ret);
  227. goto again;
  228. }
  229. /* Do a basic test on the oob, to guard against returned garbage */
  230. if (oob->reserved != 0xFFFFFFFF && !is_power_of_2(~oob->reserved))
  231. goto again;
  232. /* This should never happen, unless there is a bug in the mtd driver */
  233. WARN_ON(ops.oobretlen != SM_OOB_SIZE);
  234. WARN_ON(buffer && ops.retlen != SM_SECTOR_SIZE);
  235. if (!buffer)
  236. return 0;
  237. /* Test if sector marked as bad */
  238. if (!sm_sector_valid(oob)) {
  239. dbg("read of block %d at zone %d, failed because it is marked"
  240. " as bad" , block, zone);
  241. goto again;
  242. }
  243. /* Test ECC*/
  244. if (mtd_is_eccerr(ret) ||
  245. (ftl->smallpagenand && sm_correct_sector(buffer, oob))) {
  246. dbg("read of block %d at zone %d, failed due to ECC error",
  247. block, zone);
  248. goto again;
  249. }
  250. return 0;
  251. }
  252. /* Writes a sector to media */
  253. static int sm_write_sector(struct sm_ftl *ftl,
  254. int zone, int block, int boffset,
  255. uint8_t *buffer, struct sm_oob *oob)
  256. {
  257. struct mtd_oob_ops ops = { };
  258. struct mtd_info *mtd = ftl->trans->mtd;
  259. int ret;
  260. BUG_ON(ftl->readonly);
  261. if (zone == 0 && (block == ftl->cis_block || block == 0)) {
  262. dbg("attempted to write the CIS!");
  263. return -EIO;
  264. }
  265. if (ftl->unstable)
  266. return -EIO;
  267. ops.mode = ftl->smallpagenand ? MTD_OPS_RAW : MTD_OPS_PLACE_OOB;
  268. ops.len = SM_SECTOR_SIZE;
  269. ops.datbuf = buffer;
  270. ops.ooboffs = 0;
  271. ops.ooblen = SM_OOB_SIZE;
  272. ops.oobbuf = (void *)oob;
  273. ret = mtd_write_oob(mtd, sm_mkoffset(ftl, zone, block, boffset), &ops);
  274. /* Now we assume that hardware will catch write bitflip errors */
  275. if (ret) {
  276. dbg("write to block %d at zone %d, failed with error %d",
  277. block, zone, ret);
  278. sm_recheck_media(ftl);
  279. return ret;
  280. }
  281. /* This should never happen, unless there is a bug in the driver */
  282. WARN_ON(ops.oobretlen != SM_OOB_SIZE);
  283. WARN_ON(buffer && ops.retlen != SM_SECTOR_SIZE);
  284. return 0;
  285. }
  286. /* ------------------------ block IO ------------------------------------- */
  287. /* Write a block using data and lba, and invalid sector bitmap */
  288. static int sm_write_block(struct sm_ftl *ftl, uint8_t *buf,
  289. int zone, int block, int lba,
  290. unsigned long invalid_bitmap)
  291. {
  292. bool sm_order = IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_HAMMING_SMC);
  293. struct sm_oob oob;
  294. int boffset;
  295. int retry = 0;
  296. /* Initialize the oob with requested values */
  297. memset(&oob, 0xFF, SM_OOB_SIZE);
  298. sm_write_lba(&oob, lba);
  299. restart:
  300. if (ftl->unstable)
  301. return -EIO;
  302. for (boffset = 0; boffset < ftl->block_size;
  303. boffset += SM_SECTOR_SIZE) {
  304. oob.data_status = 0xFF;
  305. if (test_bit(boffset / SM_SECTOR_SIZE, &invalid_bitmap)) {
  306. sm_printk("sector %d of block at LBA %d of zone %d"
  307. " couldn't be read, marking it as invalid",
  308. boffset / SM_SECTOR_SIZE, lba, zone);
  309. oob.data_status = 0;
  310. }
  311. if (ftl->smallpagenand) {
  312. ecc_sw_hamming_calculate(buf + boffset,
  313. SM_SMALL_PAGE, oob.ecc1,
  314. sm_order);
  315. ecc_sw_hamming_calculate(buf + boffset + SM_SMALL_PAGE,
  316. SM_SMALL_PAGE, oob.ecc2,
  317. sm_order);
  318. }
  319. if (!sm_write_sector(ftl, zone, block, boffset,
  320. buf + boffset, &oob))
  321. continue;
  322. if (!retry) {
  323. /* If write fails. try to erase the block */
  324. /* This is safe, because we never write in blocks
  325. * that contain valuable data.
  326. * This is intended to repair block that are marked
  327. * as erased, but that isn't fully erased
  328. */
  329. if (sm_erase_block(ftl, zone, block, 0))
  330. return -EIO;
  331. retry = 1;
  332. goto restart;
  333. } else {
  334. sm_mark_block_bad(ftl, zone, block);
  335. return -EIO;
  336. }
  337. }
  338. return 0;
  339. }
  340. /* Mark whole block at offset 'offs' as bad. */
  341. static void sm_mark_block_bad(struct sm_ftl *ftl, int zone, int block)
  342. {
  343. struct sm_oob oob;
  344. int boffset;
  345. memset(&oob, 0xFF, SM_OOB_SIZE);
  346. oob.block_status = 0xF0;
  347. if (ftl->unstable)
  348. return;
  349. if (sm_recheck_media(ftl))
  350. return;
  351. sm_printk("marking block %d of zone %d as bad", block, zone);
  352. /* We aren't checking the return value, because we don't care */
  353. /* This also fails on fake xD cards, but I guess these won't expose
  354. * any bad blocks till fail completely
  355. */
  356. for (boffset = 0; boffset < ftl->block_size; boffset += SM_SECTOR_SIZE)
  357. sm_write_sector(ftl, zone, block, boffset, NULL, &oob);
  358. }
  359. /*
  360. * Erase a block within a zone
  361. * If erase succeeds, it updates free block fifo, otherwise marks block as bad
  362. */
  363. static int sm_erase_block(struct sm_ftl *ftl, int zone_num, uint16_t block,
  364. int put_free)
  365. {
  366. struct ftl_zone *zone = &ftl->zones[zone_num];
  367. struct mtd_info *mtd = ftl->trans->mtd;
  368. struct erase_info erase;
  369. erase.addr = sm_mkoffset(ftl, zone_num, block, 0);
  370. erase.len = ftl->block_size;
  371. if (ftl->unstable)
  372. return -EIO;
  373. BUG_ON(ftl->readonly);
  374. if (zone_num == 0 && (block == ftl->cis_block || block == 0)) {
  375. sm_printk("attempted to erase the CIS!");
  376. return -EIO;
  377. }
  378. if (mtd_erase(mtd, &erase)) {
  379. sm_printk("erase of block %d in zone %d failed",
  380. block, zone_num);
  381. goto error;
  382. }
  383. if (put_free)
  384. kfifo_in(&zone->free_sectors,
  385. (const unsigned char *)&block, sizeof(block));
  386. return 0;
  387. error:
  388. sm_mark_block_bad(ftl, zone_num, block);
  389. return -EIO;
  390. }
  391. /* Thoroughly test that block is valid. */
  392. static int sm_check_block(struct sm_ftl *ftl, int zone, int block)
  393. {
  394. int boffset;
  395. struct sm_oob oob;
  396. int lbas[] = { -3, 0, 0, 0 };
  397. int i = 0;
  398. int test_lba;
  399. /* First just check that block doesn't look fishy */
  400. /* Only blocks that are valid or are sliced in two parts, are
  401. * accepted
  402. */
  403. for (boffset = 0; boffset < ftl->block_size;
  404. boffset += SM_SECTOR_SIZE) {
  405. /* This shouldn't happen anyway */
  406. if (sm_read_sector(ftl, zone, block, boffset, NULL, &oob))
  407. return -2;
  408. test_lba = sm_read_lba(&oob);
  409. if (lbas[i] != test_lba)
  410. lbas[++i] = test_lba;
  411. /* If we found three different LBAs, something is fishy */
  412. if (i == 3)
  413. return -EIO;
  414. }
  415. /* If the block is sliced (partially erased usually) erase it */
  416. if (i == 2) {
  417. sm_erase_block(ftl, zone, block, 1);
  418. return 1;
  419. }
  420. return 0;
  421. }
  422. /* ----------------- media scanning --------------------------------- */
  423. static const struct chs_entry chs_table[] = {
  424. { 1, 125, 4, 4 },
  425. { 2, 125, 4, 8 },
  426. { 4, 250, 4, 8 },
  427. { 8, 250, 4, 16 },
  428. { 16, 500, 4, 16 },
  429. { 32, 500, 8, 16 },
  430. { 64, 500, 8, 32 },
  431. { 128, 500, 16, 32 },
  432. { 256, 1000, 16, 32 },
  433. { 512, 1015, 32, 63 },
  434. { 1024, 985, 33, 63 },
  435. { 2048, 985, 33, 63 },
  436. { 0 },
  437. };
  438. static const uint8_t cis_signature[] = {
  439. 0x01, 0x03, 0xD9, 0x01, 0xFF, 0x18, 0x02, 0xDF, 0x01, 0x20
  440. };
  441. /* Find out media parameters.
  442. * This ideally has to be based on nand id, but for now device size is enough
  443. */
  444. static int sm_get_media_info(struct sm_ftl *ftl, struct mtd_info *mtd)
  445. {
  446. int i;
  447. int size_in_megs = mtd->size / (1024 * 1024);
  448. ftl->readonly = mtd->type == MTD_ROM;
  449. /* Manual settings for very old devices */
  450. ftl->zone_count = 1;
  451. ftl->smallpagenand = 0;
  452. switch (size_in_megs) {
  453. case 1:
  454. /* 1 MiB flash/rom SmartMedia card (256 byte pages)*/
  455. ftl->zone_size = 256;
  456. ftl->max_lba = 250;
  457. ftl->block_size = 8 * SM_SECTOR_SIZE;
  458. ftl->smallpagenand = 1;
  459. break;
  460. case 2:
  461. /* 2 MiB flash SmartMedia (256 byte pages)*/
  462. if (mtd->writesize == SM_SMALL_PAGE) {
  463. ftl->zone_size = 512;
  464. ftl->max_lba = 500;
  465. ftl->block_size = 8 * SM_SECTOR_SIZE;
  466. ftl->smallpagenand = 1;
  467. /* 2 MiB rom SmartMedia */
  468. } else {
  469. if (!ftl->readonly)
  470. return -ENODEV;
  471. ftl->zone_size = 256;
  472. ftl->max_lba = 250;
  473. ftl->block_size = 16 * SM_SECTOR_SIZE;
  474. }
  475. break;
  476. case 4:
  477. /* 4 MiB flash/rom SmartMedia device */
  478. ftl->zone_size = 512;
  479. ftl->max_lba = 500;
  480. ftl->block_size = 16 * SM_SECTOR_SIZE;
  481. break;
  482. case 8:
  483. /* 8 MiB flash/rom SmartMedia device */
  484. ftl->zone_size = 1024;
  485. ftl->max_lba = 1000;
  486. ftl->block_size = 16 * SM_SECTOR_SIZE;
  487. }
  488. /* Minimum xD size is 16MiB. Also, all xD cards have standard zone
  489. * sizes. SmartMedia cards exist up to 128 MiB and have same layout
  490. */
  491. if (size_in_megs >= 16) {
  492. ftl->zone_count = size_in_megs / 16;
  493. ftl->zone_size = 1024;
  494. ftl->max_lba = 1000;
  495. ftl->block_size = 32 * SM_SECTOR_SIZE;
  496. }
  497. /* Test for proper write,erase and oob sizes */
  498. if (mtd->erasesize > ftl->block_size)
  499. return -ENODEV;
  500. if (mtd->writesize > SM_SECTOR_SIZE)
  501. return -ENODEV;
  502. if (ftl->smallpagenand && mtd->oobsize < SM_SMALL_OOB_SIZE)
  503. return -ENODEV;
  504. if (!ftl->smallpagenand && mtd->oobsize < SM_OOB_SIZE)
  505. return -ENODEV;
  506. /* We use OOB */
  507. if (!mtd_has_oob(mtd))
  508. return -ENODEV;
  509. /* Find geometry information */
  510. for (i = 0 ; i < ARRAY_SIZE(chs_table) ; i++) {
  511. if (chs_table[i].size == size_in_megs) {
  512. ftl->cylinders = chs_table[i].cyl;
  513. ftl->heads = chs_table[i].head;
  514. ftl->sectors = chs_table[i].sec;
  515. return 0;
  516. }
  517. }
  518. sm_printk("media has unknown size : %dMiB", size_in_megs);
  519. ftl->cylinders = 985;
  520. ftl->heads = 33;
  521. ftl->sectors = 63;
  522. return 0;
  523. }
  524. /* Validate the CIS */
  525. static int sm_read_cis(struct sm_ftl *ftl)
  526. {
  527. struct sm_oob oob;
  528. if (sm_read_sector(ftl,
  529. 0, ftl->cis_block, ftl->cis_boffset, ftl->cis_buffer, &oob))
  530. return -EIO;
  531. if (!sm_sector_valid(&oob) || !sm_block_valid(&oob))
  532. return -EIO;
  533. if (!memcmp(ftl->cis_buffer + ftl->cis_page_offset,
  534. cis_signature, sizeof(cis_signature))) {
  535. return 0;
  536. }
  537. return -EIO;
  538. }
  539. /* Scan the media for the CIS */
  540. static int sm_find_cis(struct sm_ftl *ftl)
  541. {
  542. struct sm_oob oob;
  543. int block, boffset;
  544. int block_found = 0;
  545. int cis_found = 0;
  546. /* Search for first valid block */
  547. for (block = 0 ; block < ftl->zone_size - ftl->max_lba ; block++) {
  548. if (sm_read_sector(ftl, 0, block, 0, NULL, &oob))
  549. continue;
  550. if (!sm_block_valid(&oob))
  551. continue;
  552. block_found = 1;
  553. break;
  554. }
  555. if (!block_found)
  556. return -EIO;
  557. /* Search for first valid sector in this block */
  558. for (boffset = 0 ; boffset < ftl->block_size;
  559. boffset += SM_SECTOR_SIZE) {
  560. if (sm_read_sector(ftl, 0, block, boffset, NULL, &oob))
  561. continue;
  562. if (!sm_sector_valid(&oob))
  563. continue;
  564. break;
  565. }
  566. if (boffset == ftl->block_size)
  567. return -EIO;
  568. ftl->cis_block = block;
  569. ftl->cis_boffset = boffset;
  570. ftl->cis_page_offset = 0;
  571. cis_found = !sm_read_cis(ftl);
  572. if (!cis_found) {
  573. ftl->cis_page_offset = SM_SMALL_PAGE;
  574. cis_found = !sm_read_cis(ftl);
  575. }
  576. if (cis_found) {
  577. dbg("CIS block found at offset %x",
  578. block * ftl->block_size +
  579. boffset + ftl->cis_page_offset);
  580. return 0;
  581. }
  582. return -EIO;
  583. }
  584. /* Basic test to determine if underlying mtd device if functional */
  585. static int sm_recheck_media(struct sm_ftl *ftl)
  586. {
  587. if (sm_read_cis(ftl)) {
  588. if (!ftl->unstable) {
  589. sm_printk("media unstable, not allowing writes");
  590. ftl->unstable = 1;
  591. }
  592. return -EIO;
  593. }
  594. return 0;
  595. }
  596. /* Initialize a FTL zone */
  597. static int sm_init_zone(struct sm_ftl *ftl, int zone_num)
  598. {
  599. struct ftl_zone *zone = &ftl->zones[zone_num];
  600. struct sm_oob oob;
  601. uint16_t block;
  602. int lba;
  603. int i = 0;
  604. int len;
  605. dbg("initializing zone %d", zone_num);
  606. /* Allocate memory for FTL table */
  607. zone->lba_to_phys_table = kmalloc_array(ftl->max_lba, 2, GFP_KERNEL);
  608. if (!zone->lba_to_phys_table)
  609. return -ENOMEM;
  610. memset(zone->lba_to_phys_table, -1, ftl->max_lba * 2);
  611. /* Allocate memory for free sectors FIFO */
  612. if (kfifo_alloc(&zone->free_sectors, ftl->zone_size * 2, GFP_KERNEL)) {
  613. kfree(zone->lba_to_phys_table);
  614. return -ENOMEM;
  615. }
  616. /* Now scan the zone */
  617. for (block = 0 ; block < ftl->zone_size ; block++) {
  618. /* Skip blocks till the CIS (including) */
  619. if (zone_num == 0 && block <= ftl->cis_block)
  620. continue;
  621. /* Read the oob of first sector */
  622. if (sm_read_sector(ftl, zone_num, block, 0, NULL, &oob)) {
  623. kfifo_free(&zone->free_sectors);
  624. kfree(zone->lba_to_phys_table);
  625. return -EIO;
  626. }
  627. /* Test to see if block is erased. It is enough to test
  628. * first sector, because erase happens in one shot
  629. */
  630. if (sm_block_erased(&oob)) {
  631. kfifo_in(&zone->free_sectors,
  632. (unsigned char *)&block, 2);
  633. continue;
  634. }
  635. /* If block is marked as bad, skip it */
  636. /* This assumes we can trust first sector*/
  637. /* However the way the block valid status is defined, ensures
  638. * very low probability of failure here
  639. */
  640. if (!sm_block_valid(&oob)) {
  641. dbg("PH %04d <-> <marked bad>", block);
  642. continue;
  643. }
  644. lba = sm_read_lba(&oob);
  645. /* Invalid LBA means that block is damaged. */
  646. /* We can try to erase it, or mark it as bad, but
  647. * lets leave that to recovery application
  648. */
  649. if (lba == -2 || lba >= ftl->max_lba) {
  650. dbg("PH %04d <-> LBA %04d(bad)", block, lba);
  651. continue;
  652. }
  653. /* If there is no collision,
  654. * just put the sector in the FTL table
  655. */
  656. if (zone->lba_to_phys_table[lba] < 0) {
  657. dbg_verbose("PH %04d <-> LBA %04d", block, lba);
  658. zone->lba_to_phys_table[lba] = block;
  659. continue;
  660. }
  661. sm_printk("collision"
  662. " of LBA %d between blocks %d and %d in zone %d",
  663. lba, zone->lba_to_phys_table[lba], block, zone_num);
  664. /* Test that this block is valid*/
  665. if (sm_check_block(ftl, zone_num, block))
  666. continue;
  667. /* Test now the old block */
  668. if (sm_check_block(ftl, zone_num,
  669. zone->lba_to_phys_table[lba])) {
  670. zone->lba_to_phys_table[lba] = block;
  671. continue;
  672. }
  673. /* If both blocks are valid and share same LBA, it means that
  674. * they hold different versions of same data. It not
  675. * known which is more recent, thus just erase one of them
  676. */
  677. sm_printk("both blocks are valid, erasing the later");
  678. sm_erase_block(ftl, zone_num, block, 1);
  679. }
  680. dbg("zone initialized");
  681. zone->initialized = 1;
  682. /* No free sectors, means that the zone is heavily damaged, write won't
  683. * work, but it can still can be (partially) read
  684. */
  685. if (!kfifo_len(&zone->free_sectors)) {
  686. sm_printk("no free blocks in zone %d", zone_num);
  687. return 0;
  688. }
  689. /* Randomize first block we write to */
  690. get_random_bytes(&i, 2);
  691. i %= (kfifo_len(&zone->free_sectors) / 2);
  692. while (i--) {
  693. len = kfifo_out(&zone->free_sectors,
  694. (unsigned char *)&block, 2);
  695. WARN_ON(len != 2);
  696. kfifo_in(&zone->free_sectors, (const unsigned char *)&block, 2);
  697. }
  698. return 0;
  699. }
  700. /* Get and automatically initialize an FTL mapping for one zone */
  701. static struct ftl_zone *sm_get_zone(struct sm_ftl *ftl, int zone_num)
  702. {
  703. struct ftl_zone *zone;
  704. int error;
  705. BUG_ON(zone_num >= ftl->zone_count);
  706. zone = &ftl->zones[zone_num];
  707. if (!zone->initialized) {
  708. error = sm_init_zone(ftl, zone_num);
  709. if (error)
  710. return ERR_PTR(error);
  711. }
  712. return zone;
  713. }
  714. /* ----------------- cache handling ------------------------------------------*/
  715. /* Initialize the one block cache */
  716. static void sm_cache_init(struct sm_ftl *ftl)
  717. {
  718. ftl->cache_data_invalid_bitmap = 0xFFFFFFFF;
  719. ftl->cache_clean = 1;
  720. ftl->cache_zone = -1;
  721. ftl->cache_block = -1;
  722. /*memset(ftl->cache_data, 0xAA, ftl->block_size);*/
  723. }
  724. /* Put sector in one block cache */
  725. static void sm_cache_put(struct sm_ftl *ftl, char *buffer, int boffset)
  726. {
  727. memcpy(ftl->cache_data + boffset, buffer, SM_SECTOR_SIZE);
  728. clear_bit(boffset / SM_SECTOR_SIZE, &ftl->cache_data_invalid_bitmap);
  729. ftl->cache_clean = 0;
  730. }
  731. /* Read a sector from the cache */
  732. static int sm_cache_get(struct sm_ftl *ftl, char *buffer, int boffset)
  733. {
  734. if (test_bit(boffset / SM_SECTOR_SIZE,
  735. &ftl->cache_data_invalid_bitmap))
  736. return -1;
  737. memcpy(buffer, ftl->cache_data + boffset, SM_SECTOR_SIZE);
  738. return 0;
  739. }
  740. /* Write the cache to hardware */
  741. static int sm_cache_flush(struct sm_ftl *ftl)
  742. {
  743. struct ftl_zone *zone;
  744. int sector_num;
  745. uint16_t write_sector;
  746. int zone_num = ftl->cache_zone;
  747. int block_num;
  748. if (ftl->cache_clean)
  749. return 0;
  750. if (ftl->unstable)
  751. return -EIO;
  752. BUG_ON(zone_num < 0);
  753. zone = &ftl->zones[zone_num];
  754. block_num = zone->lba_to_phys_table[ftl->cache_block];
  755. /* Try to read all unread areas of the cache block*/
  756. for_each_set_bit(sector_num, &ftl->cache_data_invalid_bitmap,
  757. ftl->block_size / SM_SECTOR_SIZE) {
  758. if (!sm_read_sector(ftl,
  759. zone_num, block_num, sector_num * SM_SECTOR_SIZE,
  760. ftl->cache_data + sector_num * SM_SECTOR_SIZE, NULL))
  761. clear_bit(sector_num,
  762. &ftl->cache_data_invalid_bitmap);
  763. }
  764. restart:
  765. if (ftl->unstable)
  766. return -EIO;
  767. /* If there are no spare blocks, */
  768. /* we could still continue by erasing/writing the current block,
  769. * but for such worn out media it doesn't worth the trouble,
  770. * and the dangers
  771. */
  772. if (kfifo_out(&zone->free_sectors,
  773. (unsigned char *)&write_sector, 2) != 2) {
  774. dbg("no free sectors for write!");
  775. return -EIO;
  776. }
  777. if (sm_write_block(ftl, ftl->cache_data, zone_num, write_sector,
  778. ftl->cache_block, ftl->cache_data_invalid_bitmap))
  779. goto restart;
  780. /* Update the FTL table */
  781. zone->lba_to_phys_table[ftl->cache_block] = write_sector;
  782. /* Write succesfull, so erase and free the old block */
  783. if (block_num > 0)
  784. sm_erase_block(ftl, zone_num, block_num, 1);
  785. sm_cache_init(ftl);
  786. return 0;
  787. }
  788. /* flush timer, runs a second after last write */
  789. static void sm_cache_flush_timer(struct timer_list *t)
  790. {
  791. struct sm_ftl *ftl = from_timer(ftl, t, timer);
  792. queue_work(cache_flush_workqueue, &ftl->flush_work);
  793. }
  794. /* cache flush work, kicked by timer */
  795. static void sm_cache_flush_work(struct work_struct *work)
  796. {
  797. struct sm_ftl *ftl = container_of(work, struct sm_ftl, flush_work);
  798. mutex_lock(&ftl->mutex);
  799. sm_cache_flush(ftl);
  800. mutex_unlock(&ftl->mutex);
  801. return;
  802. }
  803. /* ---------------- outside interface -------------------------------------- */
  804. /* outside interface: read a sector */
  805. static int sm_read(struct mtd_blktrans_dev *dev,
  806. unsigned long sect_no, char *buf)
  807. {
  808. struct sm_ftl *ftl = dev->priv;
  809. struct ftl_zone *zone;
  810. int error = 0, in_cache = 0;
  811. int zone_num, block, boffset;
  812. sm_break_offset(ftl, sect_no << 9, &zone_num, &block, &boffset);
  813. mutex_lock(&ftl->mutex);
  814. zone = sm_get_zone(ftl, zone_num);
  815. if (IS_ERR(zone)) {
  816. error = PTR_ERR(zone);
  817. goto unlock;
  818. }
  819. /* Have to look at cache first */
  820. if (ftl->cache_zone == zone_num && ftl->cache_block == block) {
  821. in_cache = 1;
  822. if (!sm_cache_get(ftl, buf, boffset))
  823. goto unlock;
  824. }
  825. /* Translate the block and return if doesn't exist in the table */
  826. block = zone->lba_to_phys_table[block];
  827. if (block == -1) {
  828. memset(buf, 0xFF, SM_SECTOR_SIZE);
  829. goto unlock;
  830. }
  831. if (sm_read_sector(ftl, zone_num, block, boffset, buf, NULL)) {
  832. error = -EIO;
  833. goto unlock;
  834. }
  835. if (in_cache)
  836. sm_cache_put(ftl, buf, boffset);
  837. unlock:
  838. mutex_unlock(&ftl->mutex);
  839. return error;
  840. }
  841. /* outside interface: write a sector */
  842. static int sm_write(struct mtd_blktrans_dev *dev,
  843. unsigned long sec_no, char *buf)
  844. {
  845. struct sm_ftl *ftl = dev->priv;
  846. struct ftl_zone *zone;
  847. int error = 0, zone_num, block, boffset;
  848. BUG_ON(ftl->readonly);
  849. sm_break_offset(ftl, sec_no << 9, &zone_num, &block, &boffset);
  850. /* No need in flush thread running now */
  851. del_timer(&ftl->timer);
  852. mutex_lock(&ftl->mutex);
  853. zone = sm_get_zone(ftl, zone_num);
  854. if (IS_ERR(zone)) {
  855. error = PTR_ERR(zone);
  856. goto unlock;
  857. }
  858. /* If entry is not in cache, flush it */
  859. if (ftl->cache_block != block || ftl->cache_zone != zone_num) {
  860. error = sm_cache_flush(ftl);
  861. if (error)
  862. goto unlock;
  863. ftl->cache_block = block;
  864. ftl->cache_zone = zone_num;
  865. }
  866. sm_cache_put(ftl, buf, boffset);
  867. unlock:
  868. mod_timer(&ftl->timer, jiffies + msecs_to_jiffies(cache_timeout));
  869. mutex_unlock(&ftl->mutex);
  870. return error;
  871. }
  872. /* outside interface: flush everything */
  873. static int sm_flush(struct mtd_blktrans_dev *dev)
  874. {
  875. struct sm_ftl *ftl = dev->priv;
  876. int retval;
  877. mutex_lock(&ftl->mutex);
  878. retval = sm_cache_flush(ftl);
  879. mutex_unlock(&ftl->mutex);
  880. return retval;
  881. }
  882. /* outside interface: device is released */
  883. static void sm_release(struct mtd_blktrans_dev *dev)
  884. {
  885. struct sm_ftl *ftl = dev->priv;
  886. del_timer_sync(&ftl->timer);
  887. cancel_work_sync(&ftl->flush_work);
  888. mutex_lock(&ftl->mutex);
  889. sm_cache_flush(ftl);
  890. mutex_unlock(&ftl->mutex);
  891. }
  892. /* outside interface: get geometry */
  893. static int sm_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
  894. {
  895. struct sm_ftl *ftl = dev->priv;
  896. geo->heads = ftl->heads;
  897. geo->sectors = ftl->sectors;
  898. geo->cylinders = ftl->cylinders;
  899. return 0;
  900. }
  901. /* external interface: main initialization function */
  902. static void sm_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
  903. {
  904. struct mtd_blktrans_dev *trans;
  905. struct sm_ftl *ftl;
  906. /* Allocate & initialize our private structure */
  907. ftl = kzalloc(sizeof(struct sm_ftl), GFP_KERNEL);
  908. if (!ftl)
  909. goto error1;
  910. mutex_init(&ftl->mutex);
  911. timer_setup(&ftl->timer, sm_cache_flush_timer, 0);
  912. INIT_WORK(&ftl->flush_work, sm_cache_flush_work);
  913. /* Read media information */
  914. if (sm_get_media_info(ftl, mtd)) {
  915. dbg("found unsupported mtd device, aborting");
  916. goto error2;
  917. }
  918. /* Allocate temporary CIS buffer for read retry support */
  919. ftl->cis_buffer = kzalloc(SM_SECTOR_SIZE, GFP_KERNEL);
  920. if (!ftl->cis_buffer)
  921. goto error2;
  922. /* Allocate zone array, it will be initialized on demand */
  923. ftl->zones = kcalloc(ftl->zone_count, sizeof(struct ftl_zone),
  924. GFP_KERNEL);
  925. if (!ftl->zones)
  926. goto error3;
  927. /* Allocate the cache*/
  928. ftl->cache_data = kzalloc(ftl->block_size, GFP_KERNEL);
  929. if (!ftl->cache_data)
  930. goto error4;
  931. sm_cache_init(ftl);
  932. /* Allocate upper layer structure and initialize it */
  933. trans = kzalloc(sizeof(struct mtd_blktrans_dev), GFP_KERNEL);
  934. if (!trans)
  935. goto error5;
  936. ftl->trans = trans;
  937. trans->priv = ftl;
  938. trans->tr = tr;
  939. trans->mtd = mtd;
  940. trans->devnum = -1;
  941. trans->size = (ftl->block_size * ftl->max_lba * ftl->zone_count) >> 9;
  942. trans->readonly = ftl->readonly;
  943. if (sm_find_cis(ftl)) {
  944. dbg("CIS not found on mtd device, aborting");
  945. goto error6;
  946. }
  947. ftl->disk_attributes = sm_create_sysfs_attributes(ftl);
  948. if (!ftl->disk_attributes)
  949. goto error6;
  950. trans->disk_attributes = ftl->disk_attributes;
  951. sm_printk("Found %d MiB xD/SmartMedia FTL on mtd%d",
  952. (int)(mtd->size / (1024 * 1024)), mtd->index);
  953. dbg("FTL layout:");
  954. dbg("%d zone(s), each consists of %d blocks (+%d spares)",
  955. ftl->zone_count, ftl->max_lba,
  956. ftl->zone_size - ftl->max_lba);
  957. dbg("each block consists of %d bytes",
  958. ftl->block_size);
  959. /* Register device*/
  960. if (add_mtd_blktrans_dev(trans)) {
  961. dbg("error in mtdblktrans layer");
  962. goto error6;
  963. }
  964. return;
  965. error6:
  966. kfree(trans);
  967. error5:
  968. kfree(ftl->cache_data);
  969. error4:
  970. kfree(ftl->zones);
  971. error3:
  972. kfree(ftl->cis_buffer);
  973. error2:
  974. kfree(ftl);
  975. error1:
  976. return;
  977. }
  978. /* main interface: device {surprise,} removal */
  979. static void sm_remove_dev(struct mtd_blktrans_dev *dev)
  980. {
  981. struct sm_ftl *ftl = dev->priv;
  982. int i;
  983. del_mtd_blktrans_dev(dev);
  984. ftl->trans = NULL;
  985. for (i = 0 ; i < ftl->zone_count; i++) {
  986. if (!ftl->zones[i].initialized)
  987. continue;
  988. kfree(ftl->zones[i].lba_to_phys_table);
  989. kfifo_free(&ftl->zones[i].free_sectors);
  990. }
  991. sm_delete_sysfs_attributes(ftl);
  992. kfree(ftl->cis_buffer);
  993. kfree(ftl->zones);
  994. kfree(ftl->cache_data);
  995. kfree(ftl);
  996. }
  997. static struct mtd_blktrans_ops sm_ftl_ops = {
  998. .name = "smblk",
  999. .major = 0,
  1000. .part_bits = SM_FTL_PARTN_BITS,
  1001. .blksize = SM_SECTOR_SIZE,
  1002. .getgeo = sm_getgeo,
  1003. .add_mtd = sm_add_mtd,
  1004. .remove_dev = sm_remove_dev,
  1005. .readsect = sm_read,
  1006. .writesect = sm_write,
  1007. .flush = sm_flush,
  1008. .release = sm_release,
  1009. .owner = THIS_MODULE,
  1010. };
  1011. static __init int sm_module_init(void)
  1012. {
  1013. int error = 0;
  1014. cache_flush_workqueue = create_freezable_workqueue("smflush");
  1015. if (!cache_flush_workqueue)
  1016. return -ENOMEM;
  1017. error = register_mtd_blktrans(&sm_ftl_ops);
  1018. if (error)
  1019. destroy_workqueue(cache_flush_workqueue);
  1020. return error;
  1021. }
  1022. static void __exit sm_module_exit(void)
  1023. {
  1024. destroy_workqueue(cache_flush_workqueue);
  1025. deregister_mtd_blktrans(&sm_ftl_ops);
  1026. }
  1027. module_init(sm_module_init);
  1028. module_exit(sm_module_exit);
  1029. MODULE_LICENSE("GPL");
  1030. MODULE_AUTHOR("Maxim Levitsky <[email protected]>");
  1031. MODULE_DESCRIPTION("Smartmedia/xD mtd translation layer");