inftlcore.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * inftlcore.c -- Linux driver for Inverse Flash Translation Layer (INFTL)
  4. *
  5. * Copyright © 2002, Greg Ungerer (gerg@snapgear.com)
  6. *
  7. * Based heavily on the nftlcore.c code which is:
  8. * Copyright © 1999 Machine Vision Holdings, Inc.
  9. * Copyright © 1999 David Woodhouse <dwmw2@infradead.org>
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/delay.h>
  14. #include <linux/slab.h>
  15. #include <linux/sched.h>
  16. #include <linux/init.h>
  17. #include <linux/kmod.h>
  18. #include <linux/hdreg.h>
  19. #include <linux/mtd/mtd.h>
  20. #include <linux/mtd/nftl.h>
  21. #include <linux/mtd/inftl.h>
  22. #include <linux/mtd/rawnand.h>
  23. #include <linux/uaccess.h>
  24. #include <asm/errno.h>
  25. #include <asm/io.h>
  26. /*
  27. * Maximum number of loops while examining next block, to have a
  28. * chance to detect consistency problems (they should never happen
  29. * because of the checks done in the mounting.
  30. */
  31. #define MAX_LOOPS 10000
  32. static void inftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
  33. {
  34. struct INFTLrecord *inftl;
  35. unsigned long temp;
  36. if (!mtd_type_is_nand(mtd) || mtd->size > UINT_MAX)
  37. return;
  38. /* OK, this is moderately ugly. But probably safe. Alternatives? */
  39. if (memcmp(mtd->name, "DiskOnChip", 10))
  40. return;
  41. if (!mtd->_block_isbad) {
  42. printk(KERN_ERR
  43. "INFTL no longer supports the old DiskOnChip drivers loaded via docprobe.\n"
  44. "Please use the new diskonchip driver under the NAND subsystem.\n");
  45. return;
  46. }
  47. pr_debug("INFTL: add_mtd for %s\n", mtd->name);
  48. inftl = kzalloc_obj(*inftl);
  49. if (!inftl)
  50. return;
  51. inftl->mbd.mtd = mtd;
  52. inftl->mbd.devnum = -1;
  53. inftl->mbd.tr = tr;
  54. if (INFTL_mount(inftl) < 0) {
  55. printk(KERN_WARNING "INFTL: could not mount device\n");
  56. kfree(inftl);
  57. return;
  58. }
  59. /* OK, it's a new one. Set up all the data structures. */
  60. /* Calculate geometry */
  61. inftl->cylinders = 1024;
  62. inftl->heads = 16;
  63. temp = inftl->cylinders * inftl->heads;
  64. inftl->sectors = inftl->mbd.size / temp;
  65. if (inftl->mbd.size % temp) {
  66. inftl->sectors++;
  67. temp = inftl->cylinders * inftl->sectors;
  68. inftl->heads = inftl->mbd.size / temp;
  69. if (inftl->mbd.size % temp) {
  70. inftl->heads++;
  71. temp = inftl->heads * inftl->sectors;
  72. inftl->cylinders = inftl->mbd.size / temp;
  73. }
  74. }
  75. if (inftl->mbd.size != inftl->heads * inftl->cylinders * inftl->sectors) {
  76. /*
  77. Oh no we don't have
  78. mbd.size == heads * cylinders * sectors
  79. */
  80. printk(KERN_WARNING "INFTL: cannot calculate a geometry to "
  81. "match size of 0x%lx.\n", inftl->mbd.size);
  82. printk(KERN_WARNING "INFTL: using C:%d H:%d S:%d "
  83. "(== 0x%lx sects)\n",
  84. inftl->cylinders, inftl->heads , inftl->sectors,
  85. (long)inftl->cylinders * (long)inftl->heads *
  86. (long)inftl->sectors );
  87. }
  88. if (add_mtd_blktrans_dev(&inftl->mbd)) {
  89. kfree(inftl->PUtable);
  90. kfree(inftl->VUtable);
  91. kfree(inftl);
  92. return;
  93. }
  94. #ifdef PSYCHO_DEBUG
  95. printk(KERN_INFO "INFTL: Found new inftl%c\n", inftl->mbd.devnum + 'a');
  96. #endif
  97. return;
  98. }
  99. static void inftl_remove_dev(struct mtd_blktrans_dev *dev)
  100. {
  101. struct INFTLrecord *inftl = (void *)dev;
  102. pr_debug("INFTL: remove_dev (i=%d)\n", dev->devnum);
  103. del_mtd_blktrans_dev(dev);
  104. kfree(inftl->PUtable);
  105. kfree(inftl->VUtable);
  106. }
  107. /*
  108. * Actual INFTL access routines.
  109. */
  110. /*
  111. * Read oob data from flash
  112. */
  113. int inftl_read_oob(struct mtd_info *mtd, loff_t offs, size_t len,
  114. size_t *retlen, uint8_t *buf)
  115. {
  116. struct mtd_oob_ops ops = { };
  117. int res;
  118. ops.mode = MTD_OPS_PLACE_OOB;
  119. ops.ooboffs = offs & (mtd->writesize - 1);
  120. ops.ooblen = len;
  121. ops.oobbuf = buf;
  122. ops.datbuf = NULL;
  123. res = mtd_read_oob(mtd, offs & ~(mtd->writesize - 1), &ops);
  124. *retlen = ops.oobretlen;
  125. return res;
  126. }
  127. /*
  128. * Write oob data to flash
  129. */
  130. int inftl_write_oob(struct mtd_info *mtd, loff_t offs, size_t len,
  131. size_t *retlen, uint8_t *buf)
  132. {
  133. struct mtd_oob_ops ops = { };
  134. int res;
  135. ops.mode = MTD_OPS_PLACE_OOB;
  136. ops.ooboffs = offs & (mtd->writesize - 1);
  137. ops.ooblen = len;
  138. ops.oobbuf = buf;
  139. ops.datbuf = NULL;
  140. res = mtd_write_oob(mtd, offs & ~(mtd->writesize - 1), &ops);
  141. *retlen = ops.oobretlen;
  142. return res;
  143. }
  144. /*
  145. * Write data and oob to flash
  146. */
  147. static int inftl_write(struct mtd_info *mtd, loff_t offs, size_t len,
  148. size_t *retlen, uint8_t *buf, uint8_t *oob)
  149. {
  150. struct mtd_oob_ops ops = { };
  151. int res;
  152. ops.mode = MTD_OPS_PLACE_OOB;
  153. ops.ooboffs = offs;
  154. ops.ooblen = mtd->oobsize;
  155. ops.oobbuf = oob;
  156. ops.datbuf = buf;
  157. ops.len = len;
  158. res = mtd_write_oob(mtd, offs & ~(mtd->writesize - 1), &ops);
  159. *retlen = ops.retlen;
  160. return res;
  161. }
  162. /*
  163. * INFTL_findfreeblock: Find a free Erase Unit on the INFTL partition.
  164. * This function is used when the give Virtual Unit Chain.
  165. */
  166. static u16 INFTL_findfreeblock(struct INFTLrecord *inftl, int desperate)
  167. {
  168. u16 pot = inftl->LastFreeEUN;
  169. int silly = inftl->nb_blocks;
  170. pr_debug("INFTL: INFTL_findfreeblock(inftl=%p,desperate=%d)\n",
  171. inftl, desperate);
  172. /*
  173. * Normally, we force a fold to happen before we run out of free
  174. * blocks completely.
  175. */
  176. if (!desperate && inftl->numfreeEUNs < 2) {
  177. pr_debug("INFTL: there are too few free EUNs (%d)\n",
  178. inftl->numfreeEUNs);
  179. return BLOCK_NIL;
  180. }
  181. /* Scan for a free block */
  182. do {
  183. if (inftl->PUtable[pot] == BLOCK_FREE) {
  184. inftl->LastFreeEUN = pot;
  185. return pot;
  186. }
  187. if (++pot > inftl->lastEUN)
  188. pot = 0;
  189. if (!silly--) {
  190. printk(KERN_WARNING "INFTL: no free blocks found! "
  191. "EUN range = %d - %d\n", 0, inftl->LastFreeEUN);
  192. return BLOCK_NIL;
  193. }
  194. } while (pot != inftl->LastFreeEUN);
  195. return BLOCK_NIL;
  196. }
  197. static u16 INFTL_foldchain(struct INFTLrecord *inftl, unsigned thisVUC, unsigned pendingblock)
  198. {
  199. u16 BlockMap[MAX_SECTORS_PER_UNIT];
  200. unsigned char BlockDeleted[MAX_SECTORS_PER_UNIT];
  201. unsigned int thisEUN, prevEUN, status;
  202. struct mtd_info *mtd = inftl->mbd.mtd;
  203. int block, silly;
  204. unsigned int targetEUN;
  205. struct inftl_oob oob;
  206. size_t retlen;
  207. pr_debug("INFTL: INFTL_foldchain(inftl=%p,thisVUC=%d,pending=%d)\n",
  208. inftl, thisVUC, pendingblock);
  209. memset(BlockMap, 0xff, sizeof(BlockMap));
  210. memset(BlockDeleted, 0, sizeof(BlockDeleted));
  211. thisEUN = targetEUN = inftl->VUtable[thisVUC];
  212. if (thisEUN == BLOCK_NIL) {
  213. printk(KERN_WARNING "INFTL: trying to fold non-existent "
  214. "Virtual Unit Chain %d!\n", thisVUC);
  215. return BLOCK_NIL;
  216. }
  217. /*
  218. * Scan to find the Erase Unit which holds the actual data for each
  219. * 512-byte block within the Chain.
  220. */
  221. silly = MAX_LOOPS;
  222. while (thisEUN < inftl->nb_blocks) {
  223. for (block = 0; block < inftl->EraseSize/SECTORSIZE; block ++) {
  224. if ((BlockMap[block] != BLOCK_NIL) ||
  225. BlockDeleted[block])
  226. continue;
  227. if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize)
  228. + (block * SECTORSIZE), 16, &retlen,
  229. (char *)&oob) < 0)
  230. status = SECTOR_IGNORE;
  231. else
  232. status = oob.b.Status | oob.b.Status1;
  233. switch(status) {
  234. case SECTOR_FREE:
  235. case SECTOR_IGNORE:
  236. break;
  237. case SECTOR_USED:
  238. BlockMap[block] = thisEUN;
  239. continue;
  240. case SECTOR_DELETED:
  241. BlockDeleted[block] = 1;
  242. continue;
  243. default:
  244. printk(KERN_WARNING "INFTL: unknown status "
  245. "for block %d in EUN %d: %x\n",
  246. block, thisEUN, status);
  247. break;
  248. }
  249. }
  250. if (!silly--) {
  251. printk(KERN_WARNING "INFTL: infinite loop in Virtual "
  252. "Unit Chain 0x%x\n", thisVUC);
  253. return BLOCK_NIL;
  254. }
  255. thisEUN = inftl->PUtable[thisEUN];
  256. }
  257. /*
  258. * OK. We now know the location of every block in the Virtual Unit
  259. * Chain, and the Erase Unit into which we are supposed to be copying.
  260. * Go for it.
  261. */
  262. pr_debug("INFTL: folding chain %d into unit %d\n", thisVUC, targetEUN);
  263. for (block = 0; block < inftl->EraseSize/SECTORSIZE ; block++) {
  264. unsigned char movebuf[SECTORSIZE];
  265. int ret;
  266. /*
  267. * If it's in the target EUN already, or if it's pending write,
  268. * do nothing.
  269. */
  270. if (BlockMap[block] == targetEUN || (pendingblock ==
  271. (thisVUC * (inftl->EraseSize / SECTORSIZE) + block))) {
  272. continue;
  273. }
  274. /*
  275. * Copy only in non free block (free blocks can only
  276. * happen in case of media errors or deleted blocks).
  277. */
  278. if (BlockMap[block] == BLOCK_NIL)
  279. continue;
  280. ret = mtd_read(mtd,
  281. (inftl->EraseSize * BlockMap[block]) + (block * SECTORSIZE),
  282. SECTORSIZE,
  283. &retlen,
  284. movebuf);
  285. if (ret < 0 && !mtd_is_bitflip(ret)) {
  286. ret = mtd_read(mtd,
  287. (inftl->EraseSize * BlockMap[block]) + (block * SECTORSIZE),
  288. SECTORSIZE,
  289. &retlen,
  290. movebuf);
  291. if (ret != -EIO)
  292. pr_debug("INFTL: error went away on retry?\n");
  293. }
  294. memset(&oob, 0xff, sizeof(struct inftl_oob));
  295. oob.b.Status = oob.b.Status1 = SECTOR_USED;
  296. inftl_write(inftl->mbd.mtd, (inftl->EraseSize * targetEUN) +
  297. (block * SECTORSIZE), SECTORSIZE, &retlen,
  298. movebuf, (char *)&oob);
  299. }
  300. /*
  301. * Newest unit in chain now contains data from _all_ older units.
  302. * So go through and erase each unit in chain, oldest first. (This
  303. * is important, by doing oldest first if we crash/reboot then it
  304. * is relatively simple to clean up the mess).
  305. */
  306. pr_debug("INFTL: want to erase virtual chain %d\n", thisVUC);
  307. for (;;) {
  308. /* Find oldest unit in chain. */
  309. thisEUN = inftl->VUtable[thisVUC];
  310. prevEUN = BLOCK_NIL;
  311. while (inftl->PUtable[thisEUN] != BLOCK_NIL) {
  312. prevEUN = thisEUN;
  313. thisEUN = inftl->PUtable[thisEUN];
  314. }
  315. /* Check if we are all done */
  316. if (thisEUN == targetEUN)
  317. break;
  318. /* Unlink the last block from the chain. */
  319. inftl->PUtable[prevEUN] = BLOCK_NIL;
  320. /* Now try to erase it. */
  321. if (INFTL_formatblock(inftl, thisEUN) < 0) {
  322. /*
  323. * Could not erase : mark block as reserved.
  324. */
  325. inftl->PUtable[thisEUN] = BLOCK_RESERVED;
  326. } else {
  327. /* Correctly erased : mark it as free */
  328. inftl->PUtable[thisEUN] = BLOCK_FREE;
  329. inftl->numfreeEUNs++;
  330. }
  331. }
  332. return targetEUN;
  333. }
  334. static u16 INFTL_makefreeblock(struct INFTLrecord *inftl, unsigned pendingblock)
  335. {
  336. /*
  337. * This is the part that needs some cleverness applied.
  338. * For now, I'm doing the minimum applicable to actually
  339. * get the thing to work.
  340. * Wear-levelling and other clever stuff needs to be implemented
  341. * and we also need to do some assessment of the results when
  342. * the system loses power half-way through the routine.
  343. */
  344. u16 LongestChain = 0;
  345. u16 ChainLength = 0, thislen;
  346. u16 chain, EUN;
  347. pr_debug("INFTL: INFTL_makefreeblock(inftl=%p,"
  348. "pending=%d)\n", inftl, pendingblock);
  349. for (chain = 0; chain < inftl->nb_blocks; chain++) {
  350. EUN = inftl->VUtable[chain];
  351. thislen = 0;
  352. while (EUN <= inftl->lastEUN) {
  353. thislen++;
  354. EUN = inftl->PUtable[EUN];
  355. if (thislen > 0xff00) {
  356. printk(KERN_WARNING "INFTL: endless loop in "
  357. "Virtual Chain %d: Unit %x\n",
  358. chain, EUN);
  359. /*
  360. * Actually, don't return failure.
  361. * Just ignore this chain and get on with it.
  362. */
  363. thislen = 0;
  364. break;
  365. }
  366. }
  367. if (thislen > ChainLength) {
  368. ChainLength = thislen;
  369. LongestChain = chain;
  370. }
  371. }
  372. if (ChainLength < 2) {
  373. printk(KERN_WARNING "INFTL: no Virtual Unit Chains available "
  374. "for folding. Failing request\n");
  375. return BLOCK_NIL;
  376. }
  377. return INFTL_foldchain(inftl, LongestChain, pendingblock);
  378. }
  379. static int nrbits(unsigned int val, int bitcount)
  380. {
  381. int i, total = 0;
  382. for (i = 0; (i < bitcount); i++)
  383. total += (((0x1 << i) & val) ? 1 : 0);
  384. return total;
  385. }
  386. /*
  387. * INFTL_findwriteunit: Return the unit number into which we can write
  388. * for this block. Make it available if it isn't already.
  389. */
  390. static inline u16 INFTL_findwriteunit(struct INFTLrecord *inftl, unsigned block)
  391. {
  392. unsigned int thisVUC = block / (inftl->EraseSize / SECTORSIZE);
  393. unsigned int thisEUN, writeEUN, prev_block, status;
  394. unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize -1);
  395. struct mtd_info *mtd = inftl->mbd.mtd;
  396. struct inftl_oob oob;
  397. struct inftl_bci bci;
  398. unsigned char anac, nacs, parity;
  399. size_t retlen;
  400. int silly, silly2 = 3;
  401. pr_debug("INFTL: INFTL_findwriteunit(inftl=%p,block=%d)\n",
  402. inftl, block);
  403. do {
  404. /*
  405. * Scan the media to find a unit in the VUC which has
  406. * a free space for the block in question.
  407. */
  408. writeEUN = BLOCK_NIL;
  409. thisEUN = inftl->VUtable[thisVUC];
  410. silly = MAX_LOOPS;
  411. while (thisEUN <= inftl->lastEUN) {
  412. if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) +
  413. blockofs, 8, &retlen, (char *)&bci) < 0)
  414. status = SECTOR_IGNORE;
  415. else
  416. status = bci.Status | bci.Status1;
  417. pr_debug("INFTL: status of block %d in EUN %d is %x\n",
  418. block , writeEUN, status);
  419. switch(status) {
  420. case SECTOR_FREE:
  421. writeEUN = thisEUN;
  422. break;
  423. case SECTOR_DELETED:
  424. case SECTOR_USED:
  425. /* Can't go any further */
  426. goto hitused;
  427. case SECTOR_IGNORE:
  428. break;
  429. default:
  430. /*
  431. * Invalid block. Don't use it any more.
  432. * Must implement.
  433. */
  434. break;
  435. }
  436. if (!silly--) {
  437. printk(KERN_WARNING "INFTL: infinite loop in "
  438. "Virtual Unit Chain 0x%x\n", thisVUC);
  439. return BLOCK_NIL;
  440. }
  441. /* Skip to next block in chain */
  442. thisEUN = inftl->PUtable[thisEUN];
  443. }
  444. hitused:
  445. if (writeEUN != BLOCK_NIL)
  446. return writeEUN;
  447. /*
  448. * OK. We didn't find one in the existing chain, or there
  449. * is no existing chain. Allocate a new one.
  450. */
  451. writeEUN = INFTL_findfreeblock(inftl, 0);
  452. if (writeEUN == BLOCK_NIL) {
  453. /*
  454. * That didn't work - there were no free blocks just
  455. * waiting to be picked up. We're going to have to fold
  456. * a chain to make room.
  457. */
  458. thisEUN = INFTL_makefreeblock(inftl, block);
  459. /*
  460. * Hopefully we free something, lets try again.
  461. * This time we are desperate...
  462. */
  463. pr_debug("INFTL: using desperate==1 to find free EUN "
  464. "to accommodate write to VUC %d\n",
  465. thisVUC);
  466. writeEUN = INFTL_findfreeblock(inftl, 1);
  467. if (writeEUN == BLOCK_NIL) {
  468. /*
  469. * Ouch. This should never happen - we should
  470. * always be able to make some room somehow.
  471. * If we get here, we've allocated more storage
  472. * space than actual media, or our makefreeblock
  473. * routine is missing something.
  474. */
  475. printk(KERN_WARNING "INFTL: cannot make free "
  476. "space.\n");
  477. #ifdef DEBUG
  478. INFTL_dumptables(inftl);
  479. INFTL_dumpVUchains(inftl);
  480. #endif
  481. return BLOCK_NIL;
  482. }
  483. }
  484. /*
  485. * Insert new block into virtual chain. Firstly update the
  486. * block headers in flash...
  487. */
  488. anac = 0;
  489. nacs = 0;
  490. thisEUN = inftl->VUtable[thisVUC];
  491. if (thisEUN != BLOCK_NIL) {
  492. inftl_read_oob(mtd, thisEUN * inftl->EraseSize
  493. + 8, 8, &retlen, (char *)&oob.u);
  494. anac = oob.u.a.ANAC + 1;
  495. nacs = oob.u.a.NACs + 1;
  496. }
  497. prev_block = inftl->VUtable[thisVUC];
  498. if (prev_block < inftl->nb_blocks)
  499. prev_block -= inftl->firstEUN;
  500. parity = (nrbits(thisVUC, 16) & 0x1) ? 0x1 : 0;
  501. parity |= (nrbits(prev_block, 16) & 0x1) ? 0x2 : 0;
  502. parity |= (nrbits(anac, 8) & 0x1) ? 0x4 : 0;
  503. parity |= (nrbits(nacs, 8) & 0x1) ? 0x8 : 0;
  504. oob.u.a.virtualUnitNo = cpu_to_le16(thisVUC);
  505. oob.u.a.prevUnitNo = cpu_to_le16(prev_block);
  506. oob.u.a.ANAC = anac;
  507. oob.u.a.NACs = nacs;
  508. oob.u.a.parityPerField = parity;
  509. oob.u.a.discarded = 0xaa;
  510. inftl_write_oob(mtd, writeEUN * inftl->EraseSize + 8, 8,
  511. &retlen, (char *)&oob.u);
  512. /* Also back up header... */
  513. oob.u.b.virtualUnitNo = cpu_to_le16(thisVUC);
  514. oob.u.b.prevUnitNo = cpu_to_le16(prev_block);
  515. oob.u.b.ANAC = anac;
  516. oob.u.b.NACs = nacs;
  517. oob.u.b.parityPerField = parity;
  518. oob.u.b.discarded = 0xaa;
  519. inftl_write_oob(mtd, writeEUN * inftl->EraseSize +
  520. SECTORSIZE * 4 + 8, 8, &retlen, (char *)&oob.u);
  521. inftl->PUtable[writeEUN] = inftl->VUtable[thisVUC];
  522. inftl->VUtable[thisVUC] = writeEUN;
  523. inftl->numfreeEUNs--;
  524. return writeEUN;
  525. } while (silly2--);
  526. printk(KERN_WARNING "INFTL: error folding to make room for Virtual "
  527. "Unit Chain 0x%x\n", thisVUC);
  528. return BLOCK_NIL;
  529. }
  530. /*
  531. * Given a Virtual Unit Chain, see if it can be deleted, and if so do it.
  532. */
  533. static void INFTL_trydeletechain(struct INFTLrecord *inftl, unsigned thisVUC)
  534. {
  535. struct mtd_info *mtd = inftl->mbd.mtd;
  536. unsigned char BlockUsed[MAX_SECTORS_PER_UNIT];
  537. unsigned char BlockDeleted[MAX_SECTORS_PER_UNIT];
  538. unsigned int thisEUN, status;
  539. int block, silly;
  540. struct inftl_bci bci;
  541. size_t retlen;
  542. pr_debug("INFTL: INFTL_trydeletechain(inftl=%p,"
  543. "thisVUC=%d)\n", inftl, thisVUC);
  544. memset(BlockUsed, 0, sizeof(BlockUsed));
  545. memset(BlockDeleted, 0, sizeof(BlockDeleted));
  546. thisEUN = inftl->VUtable[thisVUC];
  547. if (thisEUN == BLOCK_NIL) {
  548. printk(KERN_WARNING "INFTL: trying to delete non-existent "
  549. "Virtual Unit Chain %d!\n", thisVUC);
  550. return;
  551. }
  552. /*
  553. * Scan through the Erase Units to determine whether any data is in
  554. * each of the 512-byte blocks within the Chain.
  555. */
  556. silly = MAX_LOOPS;
  557. while (thisEUN < inftl->nb_blocks) {
  558. for (block = 0; block < inftl->EraseSize/SECTORSIZE; block++) {
  559. if (BlockUsed[block] || BlockDeleted[block])
  560. continue;
  561. if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize)
  562. + (block * SECTORSIZE), 8 , &retlen,
  563. (char *)&bci) < 0)
  564. status = SECTOR_IGNORE;
  565. else
  566. status = bci.Status | bci.Status1;
  567. switch(status) {
  568. case SECTOR_FREE:
  569. case SECTOR_IGNORE:
  570. break;
  571. case SECTOR_USED:
  572. BlockUsed[block] = 1;
  573. continue;
  574. case SECTOR_DELETED:
  575. BlockDeleted[block] = 1;
  576. continue;
  577. default:
  578. printk(KERN_WARNING "INFTL: unknown status "
  579. "for block %d in EUN %d: 0x%x\n",
  580. block, thisEUN, status);
  581. }
  582. }
  583. if (!silly--) {
  584. printk(KERN_WARNING "INFTL: infinite loop in Virtual "
  585. "Unit Chain 0x%x\n", thisVUC);
  586. return;
  587. }
  588. thisEUN = inftl->PUtable[thisEUN];
  589. }
  590. for (block = 0; block < inftl->EraseSize/SECTORSIZE; block++)
  591. if (BlockUsed[block])
  592. return;
  593. /*
  594. * For each block in the chain free it and make it available
  595. * for future use. Erase from the oldest unit first.
  596. */
  597. pr_debug("INFTL: deleting empty VUC %d\n", thisVUC);
  598. for (;;) {
  599. u16 *prevEUN = &inftl->VUtable[thisVUC];
  600. thisEUN = *prevEUN;
  601. /* If the chain is all gone already, we're done */
  602. if (thisEUN == BLOCK_NIL) {
  603. pr_debug("INFTL: Empty VUC %d for deletion was already absent\n", thisEUN);
  604. return;
  605. }
  606. /* Find oldest unit in chain. */
  607. while (inftl->PUtable[thisEUN] != BLOCK_NIL) {
  608. BUG_ON(thisEUN >= inftl->nb_blocks);
  609. prevEUN = &inftl->PUtable[thisEUN];
  610. thisEUN = *prevEUN;
  611. }
  612. pr_debug("Deleting EUN %d from VUC %d\n",
  613. thisEUN, thisVUC);
  614. if (INFTL_formatblock(inftl, thisEUN) < 0) {
  615. /*
  616. * Could not erase : mark block as reserved.
  617. */
  618. inftl->PUtable[thisEUN] = BLOCK_RESERVED;
  619. } else {
  620. /* Correctly erased : mark it as free */
  621. inftl->PUtable[thisEUN] = BLOCK_FREE;
  622. inftl->numfreeEUNs++;
  623. }
  624. /* Now sort out whatever was pointing to it... */
  625. *prevEUN = BLOCK_NIL;
  626. /* Ideally we'd actually be responsive to new
  627. requests while we're doing this -- if there's
  628. free space why should others be made to wait? */
  629. cond_resched();
  630. }
  631. inftl->VUtable[thisVUC] = BLOCK_NIL;
  632. }
  633. static int INFTL_deleteblock(struct INFTLrecord *inftl, unsigned block)
  634. {
  635. unsigned int thisEUN = inftl->VUtable[block / (inftl->EraseSize / SECTORSIZE)];
  636. unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize - 1);
  637. struct mtd_info *mtd = inftl->mbd.mtd;
  638. unsigned int status;
  639. int silly = MAX_LOOPS;
  640. size_t retlen;
  641. struct inftl_bci bci;
  642. pr_debug("INFTL: INFTL_deleteblock(inftl=%p,"
  643. "block=%d)\n", inftl, block);
  644. while (thisEUN < inftl->nb_blocks) {
  645. if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) +
  646. blockofs, 8, &retlen, (char *)&bci) < 0)
  647. status = SECTOR_IGNORE;
  648. else
  649. status = bci.Status | bci.Status1;
  650. switch (status) {
  651. case SECTOR_FREE:
  652. case SECTOR_IGNORE:
  653. break;
  654. case SECTOR_DELETED:
  655. thisEUN = BLOCK_NIL;
  656. goto foundit;
  657. case SECTOR_USED:
  658. goto foundit;
  659. default:
  660. printk(KERN_WARNING "INFTL: unknown status for "
  661. "block %d in EUN %d: 0x%x\n",
  662. block, thisEUN, status);
  663. break;
  664. }
  665. if (!silly--) {
  666. printk(KERN_WARNING "INFTL: infinite loop in Virtual "
  667. "Unit Chain 0x%x\n",
  668. block / (inftl->EraseSize / SECTORSIZE));
  669. return 1;
  670. }
  671. thisEUN = inftl->PUtable[thisEUN];
  672. }
  673. foundit:
  674. if (thisEUN != BLOCK_NIL) {
  675. loff_t ptr = (thisEUN * inftl->EraseSize) + blockofs;
  676. if (inftl_read_oob(mtd, ptr, 8, &retlen, (char *)&bci) < 0)
  677. return -EIO;
  678. bci.Status = bci.Status1 = SECTOR_DELETED;
  679. if (inftl_write_oob(mtd, ptr, 8, &retlen, (char *)&bci) < 0)
  680. return -EIO;
  681. INFTL_trydeletechain(inftl, block / (inftl->EraseSize / SECTORSIZE));
  682. }
  683. return 0;
  684. }
  685. static int inftl_writeblock(struct mtd_blktrans_dev *mbd, unsigned long block,
  686. char *buffer)
  687. {
  688. struct INFTLrecord *inftl = (void *)mbd;
  689. unsigned int writeEUN;
  690. unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize - 1);
  691. size_t retlen;
  692. struct inftl_oob oob;
  693. char *p, *pend;
  694. pr_debug("INFTL: inftl_writeblock(inftl=%p,block=%ld,"
  695. "buffer=%p)\n", inftl, block, buffer);
  696. /* Is block all zero? */
  697. pend = buffer + SECTORSIZE;
  698. for (p = buffer; p < pend && !*p; p++)
  699. ;
  700. if (p < pend) {
  701. writeEUN = INFTL_findwriteunit(inftl, block);
  702. if (writeEUN == BLOCK_NIL) {
  703. printk(KERN_WARNING "inftl_writeblock(): cannot find "
  704. "block to write to\n");
  705. /*
  706. * If we _still_ haven't got a block to use,
  707. * we're screwed.
  708. */
  709. return 1;
  710. }
  711. memset(&oob, 0xff, sizeof(struct inftl_oob));
  712. oob.b.Status = oob.b.Status1 = SECTOR_USED;
  713. inftl_write(inftl->mbd.mtd, (writeEUN * inftl->EraseSize) +
  714. blockofs, SECTORSIZE, &retlen, (char *)buffer,
  715. (char *)&oob);
  716. /*
  717. * need to write SECTOR_USED flags since they are not written
  718. * in mtd_writeecc
  719. */
  720. } else {
  721. INFTL_deleteblock(inftl, block);
  722. }
  723. return 0;
  724. }
  725. static int inftl_readblock(struct mtd_blktrans_dev *mbd, unsigned long block,
  726. char *buffer)
  727. {
  728. struct INFTLrecord *inftl = (void *)mbd;
  729. unsigned int thisEUN = inftl->VUtable[block / (inftl->EraseSize / SECTORSIZE)];
  730. unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize - 1);
  731. struct mtd_info *mtd = inftl->mbd.mtd;
  732. unsigned int status;
  733. int silly = MAX_LOOPS;
  734. struct inftl_bci bci;
  735. size_t retlen;
  736. pr_debug("INFTL: inftl_readblock(inftl=%p,block=%ld,"
  737. "buffer=%p)\n", inftl, block, buffer);
  738. while (thisEUN < inftl->nb_blocks) {
  739. if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) +
  740. blockofs, 8, &retlen, (char *)&bci) < 0)
  741. status = SECTOR_IGNORE;
  742. else
  743. status = bci.Status | bci.Status1;
  744. switch (status) {
  745. case SECTOR_DELETED:
  746. thisEUN = BLOCK_NIL;
  747. goto foundit;
  748. case SECTOR_USED:
  749. goto foundit;
  750. case SECTOR_FREE:
  751. case SECTOR_IGNORE:
  752. break;
  753. default:
  754. printk(KERN_WARNING "INFTL: unknown status for "
  755. "block %ld in EUN %d: 0x%04x\n",
  756. block, thisEUN, status);
  757. break;
  758. }
  759. if (!silly--) {
  760. printk(KERN_WARNING "INFTL: infinite loop in "
  761. "Virtual Unit Chain 0x%lx\n",
  762. block / (inftl->EraseSize / SECTORSIZE));
  763. return 1;
  764. }
  765. thisEUN = inftl->PUtable[thisEUN];
  766. }
  767. foundit:
  768. if (thisEUN == BLOCK_NIL) {
  769. /* The requested block is not on the media, return all 0x00 */
  770. memset(buffer, 0, SECTORSIZE);
  771. } else {
  772. size_t retlen;
  773. loff_t ptr = (thisEUN * inftl->EraseSize) + blockofs;
  774. int ret = mtd_read(mtd, ptr, SECTORSIZE, &retlen, buffer);
  775. /* Handle corrected bit flips gracefully */
  776. if (ret < 0 && !mtd_is_bitflip(ret))
  777. return -EIO;
  778. }
  779. return 0;
  780. }
  781. static int inftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
  782. {
  783. struct INFTLrecord *inftl = (void *)dev;
  784. geo->heads = inftl->heads;
  785. geo->sectors = inftl->sectors;
  786. geo->cylinders = inftl->cylinders;
  787. return 0;
  788. }
  789. static struct mtd_blktrans_ops inftl_tr = {
  790. .name = "inftl",
  791. .major = INFTL_MAJOR,
  792. .part_bits = INFTL_PARTN_BITS,
  793. .blksize = 512,
  794. .getgeo = inftl_getgeo,
  795. .readsect = inftl_readblock,
  796. .writesect = inftl_writeblock,
  797. .add_mtd = inftl_add_mtd,
  798. .remove_dev = inftl_remove_dev,
  799. .owner = THIS_MODULE,
  800. };
  801. module_mtd_blktrans(inftl_tr);
  802. MODULE_LICENSE("GPL");
  803. MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com>, David Woodhouse <dwmw2@infradead.org>, Fabrice Bellard <fabrice.bellard@netgem.com> et al.");
  804. MODULE_DESCRIPTION("Support code for Inverse Flash Translation Layer, used on M-Systems DiskOnChip 2000, Millennium and Millennium Plus");