sm_ftl.c 30 KB

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