inftlcore.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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 ([email protected])
  6. *
  7. * Based heavily on the nftlcore.c code which is:
  8. * Copyright © 1999 Machine Vision Holdings, Inc.
  9. * Copyright © 1999 David Woodhouse <[email protected]>
  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(sizeof(*inftl), GFP_KERNEL);
  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. * it 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. inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) +
  413. blockofs, 8, &retlen, (char *)&bci);
  414. status = bci.Status | bci.Status1;
  415. pr_debug("INFTL: status of block %d in EUN %d is %x\n",
  416. block , writeEUN, status);
  417. switch(status) {
  418. case SECTOR_FREE:
  419. writeEUN = thisEUN;
  420. break;
  421. case SECTOR_DELETED:
  422. case SECTOR_USED:
  423. /* Can't go any further */
  424. goto hitused;
  425. case SECTOR_IGNORE:
  426. break;
  427. default:
  428. /*
  429. * Invalid block. Don't use it any more.
  430. * Must implement.
  431. */
  432. break;
  433. }
  434. if (!silly--) {
  435. printk(KERN_WARNING "INFTL: infinite loop in "
  436. "Virtual Unit Chain 0x%x\n", thisVUC);
  437. return BLOCK_NIL;
  438. }
  439. /* Skip to next block in chain */
  440. thisEUN = inftl->PUtable[thisEUN];
  441. }
  442. hitused:
  443. if (writeEUN != BLOCK_NIL)
  444. return writeEUN;
  445. /*
  446. * OK. We didn't find one in the existing chain, or there
  447. * is no existing chain. Allocate a new one.
  448. */
  449. writeEUN = INFTL_findfreeblock(inftl, 0);
  450. if (writeEUN == BLOCK_NIL) {
  451. /*
  452. * That didn't work - there were no free blocks just
  453. * waiting to be picked up. We're going to have to fold
  454. * a chain to make room.
  455. */
  456. thisEUN = INFTL_makefreeblock(inftl, block);
  457. /*
  458. * Hopefully we free something, lets try again.
  459. * This time we are desperate...
  460. */
  461. pr_debug("INFTL: using desperate==1 to find free EUN "
  462. "to accommodate write to VUC %d\n",
  463. thisVUC);
  464. writeEUN = INFTL_findfreeblock(inftl, 1);
  465. if (writeEUN == BLOCK_NIL) {
  466. /*
  467. * Ouch. This should never happen - we should
  468. * always be able to make some room somehow.
  469. * If we get here, we've allocated more storage
  470. * space than actual media, or our makefreeblock
  471. * routine is missing something.
  472. */
  473. printk(KERN_WARNING "INFTL: cannot make free "
  474. "space.\n");
  475. #ifdef DEBUG
  476. INFTL_dumptables(inftl);
  477. INFTL_dumpVUchains(inftl);
  478. #endif
  479. return BLOCK_NIL;
  480. }
  481. }
  482. /*
  483. * Insert new block into virtual chain. Firstly update the
  484. * block headers in flash...
  485. */
  486. anac = 0;
  487. nacs = 0;
  488. thisEUN = inftl->VUtable[thisVUC];
  489. if (thisEUN != BLOCK_NIL) {
  490. inftl_read_oob(mtd, thisEUN * inftl->EraseSize
  491. + 8, 8, &retlen, (char *)&oob.u);
  492. anac = oob.u.a.ANAC + 1;
  493. nacs = oob.u.a.NACs + 1;
  494. }
  495. prev_block = inftl->VUtable[thisVUC];
  496. if (prev_block < inftl->nb_blocks)
  497. prev_block -= inftl->firstEUN;
  498. parity = (nrbits(thisVUC, 16) & 0x1) ? 0x1 : 0;
  499. parity |= (nrbits(prev_block, 16) & 0x1) ? 0x2 : 0;
  500. parity |= (nrbits(anac, 8) & 0x1) ? 0x4 : 0;
  501. parity |= (nrbits(nacs, 8) & 0x1) ? 0x8 : 0;
  502. oob.u.a.virtualUnitNo = cpu_to_le16(thisVUC);
  503. oob.u.a.prevUnitNo = cpu_to_le16(prev_block);
  504. oob.u.a.ANAC = anac;
  505. oob.u.a.NACs = nacs;
  506. oob.u.a.parityPerField = parity;
  507. oob.u.a.discarded = 0xaa;
  508. inftl_write_oob(mtd, writeEUN * inftl->EraseSize + 8, 8,
  509. &retlen, (char *)&oob.u);
  510. /* Also back up header... */
  511. oob.u.b.virtualUnitNo = cpu_to_le16(thisVUC);
  512. oob.u.b.prevUnitNo = cpu_to_le16(prev_block);
  513. oob.u.b.ANAC = anac;
  514. oob.u.b.NACs = nacs;
  515. oob.u.b.parityPerField = parity;
  516. oob.u.b.discarded = 0xaa;
  517. inftl_write_oob(mtd, writeEUN * inftl->EraseSize +
  518. SECTORSIZE * 4 + 8, 8, &retlen, (char *)&oob.u);
  519. inftl->PUtable[writeEUN] = inftl->VUtable[thisVUC];
  520. inftl->VUtable[thisVUC] = writeEUN;
  521. inftl->numfreeEUNs--;
  522. return writeEUN;
  523. } while (silly2--);
  524. printk(KERN_WARNING "INFTL: error folding to make room for Virtual "
  525. "Unit Chain 0x%x\n", thisVUC);
  526. return BLOCK_NIL;
  527. }
  528. /*
  529. * Given a Virtual Unit Chain, see if it can be deleted, and if so do it.
  530. */
  531. static void INFTL_trydeletechain(struct INFTLrecord *inftl, unsigned thisVUC)
  532. {
  533. struct mtd_info *mtd = inftl->mbd.mtd;
  534. unsigned char BlockUsed[MAX_SECTORS_PER_UNIT];
  535. unsigned char BlockDeleted[MAX_SECTORS_PER_UNIT];
  536. unsigned int thisEUN, status;
  537. int block, silly;
  538. struct inftl_bci bci;
  539. size_t retlen;
  540. pr_debug("INFTL: INFTL_trydeletechain(inftl=%p,"
  541. "thisVUC=%d)\n", inftl, thisVUC);
  542. memset(BlockUsed, 0, sizeof(BlockUsed));
  543. memset(BlockDeleted, 0, sizeof(BlockDeleted));
  544. thisEUN = inftl->VUtable[thisVUC];
  545. if (thisEUN == BLOCK_NIL) {
  546. printk(KERN_WARNING "INFTL: trying to delete non-existent "
  547. "Virtual Unit Chain %d!\n", thisVUC);
  548. return;
  549. }
  550. /*
  551. * Scan through the Erase Units to determine whether any data is in
  552. * each of the 512-byte blocks within the Chain.
  553. */
  554. silly = MAX_LOOPS;
  555. while (thisEUN < inftl->nb_blocks) {
  556. for (block = 0; block < inftl->EraseSize/SECTORSIZE; block++) {
  557. if (BlockUsed[block] || BlockDeleted[block])
  558. continue;
  559. if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize)
  560. + (block * SECTORSIZE), 8 , &retlen,
  561. (char *)&bci) < 0)
  562. status = SECTOR_IGNORE;
  563. else
  564. status = bci.Status | bci.Status1;
  565. switch(status) {
  566. case SECTOR_FREE:
  567. case SECTOR_IGNORE:
  568. break;
  569. case SECTOR_USED:
  570. BlockUsed[block] = 1;
  571. continue;
  572. case SECTOR_DELETED:
  573. BlockDeleted[block] = 1;
  574. continue;
  575. default:
  576. printk(KERN_WARNING "INFTL: unknown status "
  577. "for block %d in EUN %d: 0x%x\n",
  578. block, thisEUN, status);
  579. }
  580. }
  581. if (!silly--) {
  582. printk(KERN_WARNING "INFTL: infinite loop in Virtual "
  583. "Unit Chain 0x%x\n", thisVUC);
  584. return;
  585. }
  586. thisEUN = inftl->PUtable[thisEUN];
  587. }
  588. for (block = 0; block < inftl->EraseSize/SECTORSIZE; block++)
  589. if (BlockUsed[block])
  590. return;
  591. /*
  592. * For each block in the chain free it and make it available
  593. * for future use. Erase from the oldest unit first.
  594. */
  595. pr_debug("INFTL: deleting empty VUC %d\n", thisVUC);
  596. for (;;) {
  597. u16 *prevEUN = &inftl->VUtable[thisVUC];
  598. thisEUN = *prevEUN;
  599. /* If the chain is all gone already, we're done */
  600. if (thisEUN == BLOCK_NIL) {
  601. pr_debug("INFTL: Empty VUC %d for deletion was already absent\n", thisEUN);
  602. return;
  603. }
  604. /* Find oldest unit in chain. */
  605. while (inftl->PUtable[thisEUN] != BLOCK_NIL) {
  606. BUG_ON(thisEUN >= inftl->nb_blocks);
  607. prevEUN = &inftl->PUtable[thisEUN];
  608. thisEUN = *prevEUN;
  609. }
  610. pr_debug("Deleting EUN %d from VUC %d\n",
  611. thisEUN, thisVUC);
  612. if (INFTL_formatblock(inftl, thisEUN) < 0) {
  613. /*
  614. * Could not erase : mark block as reserved.
  615. */
  616. inftl->PUtable[thisEUN] = BLOCK_RESERVED;
  617. } else {
  618. /* Correctly erased : mark it as free */
  619. inftl->PUtable[thisEUN] = BLOCK_FREE;
  620. inftl->numfreeEUNs++;
  621. }
  622. /* Now sort out whatever was pointing to it... */
  623. *prevEUN = BLOCK_NIL;
  624. /* Ideally we'd actually be responsive to new
  625. requests while we're doing this -- if there's
  626. free space why should others be made to wait? */
  627. cond_resched();
  628. }
  629. inftl->VUtable[thisVUC] = BLOCK_NIL;
  630. }
  631. static int INFTL_deleteblock(struct INFTLrecord *inftl, unsigned block)
  632. {
  633. unsigned int thisEUN = inftl->VUtable[block / (inftl->EraseSize / SECTORSIZE)];
  634. unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize - 1);
  635. struct mtd_info *mtd = inftl->mbd.mtd;
  636. unsigned int status;
  637. int silly = MAX_LOOPS;
  638. size_t retlen;
  639. struct inftl_bci bci;
  640. pr_debug("INFTL: INFTL_deleteblock(inftl=%p,"
  641. "block=%d)\n", inftl, block);
  642. while (thisEUN < inftl->nb_blocks) {
  643. if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) +
  644. blockofs, 8, &retlen, (char *)&bci) < 0)
  645. status = SECTOR_IGNORE;
  646. else
  647. status = bci.Status | bci.Status1;
  648. switch (status) {
  649. case SECTOR_FREE:
  650. case SECTOR_IGNORE:
  651. break;
  652. case SECTOR_DELETED:
  653. thisEUN = BLOCK_NIL;
  654. goto foundit;
  655. case SECTOR_USED:
  656. goto foundit;
  657. default:
  658. printk(KERN_WARNING "INFTL: unknown status for "
  659. "block %d in EUN %d: 0x%x\n",
  660. block, thisEUN, status);
  661. break;
  662. }
  663. if (!silly--) {
  664. printk(KERN_WARNING "INFTL: infinite loop in Virtual "
  665. "Unit Chain 0x%x\n",
  666. block / (inftl->EraseSize / SECTORSIZE));
  667. return 1;
  668. }
  669. thisEUN = inftl->PUtable[thisEUN];
  670. }
  671. foundit:
  672. if (thisEUN != BLOCK_NIL) {
  673. loff_t ptr = (thisEUN * inftl->EraseSize) + blockofs;
  674. if (inftl_read_oob(mtd, ptr, 8, &retlen, (char *)&bci) < 0)
  675. return -EIO;
  676. bci.Status = bci.Status1 = SECTOR_DELETED;
  677. if (inftl_write_oob(mtd, ptr, 8, &retlen, (char *)&bci) < 0)
  678. return -EIO;
  679. INFTL_trydeletechain(inftl, block / (inftl->EraseSize / SECTORSIZE));
  680. }
  681. return 0;
  682. }
  683. static int inftl_writeblock(struct mtd_blktrans_dev *mbd, unsigned long block,
  684. char *buffer)
  685. {
  686. struct INFTLrecord *inftl = (void *)mbd;
  687. unsigned int writeEUN;
  688. unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize - 1);
  689. size_t retlen;
  690. struct inftl_oob oob;
  691. char *p, *pend;
  692. pr_debug("INFTL: inftl_writeblock(inftl=%p,block=%ld,"
  693. "buffer=%p)\n", inftl, block, buffer);
  694. /* Is block all zero? */
  695. pend = buffer + SECTORSIZE;
  696. for (p = buffer; p < pend && !*p; p++)
  697. ;
  698. if (p < pend) {
  699. writeEUN = INFTL_findwriteunit(inftl, block);
  700. if (writeEUN == BLOCK_NIL) {
  701. printk(KERN_WARNING "inftl_writeblock(): cannot find "
  702. "block to write to\n");
  703. /*
  704. * If we _still_ haven't got a block to use,
  705. * we're screwed.
  706. */
  707. return 1;
  708. }
  709. memset(&oob, 0xff, sizeof(struct inftl_oob));
  710. oob.b.Status = oob.b.Status1 = SECTOR_USED;
  711. inftl_write(inftl->mbd.mtd, (writeEUN * inftl->EraseSize) +
  712. blockofs, SECTORSIZE, &retlen, (char *)buffer,
  713. (char *)&oob);
  714. /*
  715. * need to write SECTOR_USED flags since they are not written
  716. * in mtd_writeecc
  717. */
  718. } else {
  719. INFTL_deleteblock(inftl, block);
  720. }
  721. return 0;
  722. }
  723. static int inftl_readblock(struct mtd_blktrans_dev *mbd, unsigned long block,
  724. char *buffer)
  725. {
  726. struct INFTLrecord *inftl = (void *)mbd;
  727. unsigned int thisEUN = inftl->VUtable[block / (inftl->EraseSize / SECTORSIZE)];
  728. unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize - 1);
  729. struct mtd_info *mtd = inftl->mbd.mtd;
  730. unsigned int status;
  731. int silly = MAX_LOOPS;
  732. struct inftl_bci bci;
  733. size_t retlen;
  734. pr_debug("INFTL: inftl_readblock(inftl=%p,block=%ld,"
  735. "buffer=%p)\n", inftl, block, buffer);
  736. while (thisEUN < inftl->nb_blocks) {
  737. if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) +
  738. blockofs, 8, &retlen, (char *)&bci) < 0)
  739. status = SECTOR_IGNORE;
  740. else
  741. status = bci.Status | bci.Status1;
  742. switch (status) {
  743. case SECTOR_DELETED:
  744. thisEUN = BLOCK_NIL;
  745. goto foundit;
  746. case SECTOR_USED:
  747. goto foundit;
  748. case SECTOR_FREE:
  749. case SECTOR_IGNORE:
  750. break;
  751. default:
  752. printk(KERN_WARNING "INFTL: unknown status for "
  753. "block %ld in EUN %d: 0x%04x\n",
  754. block, thisEUN, status);
  755. break;
  756. }
  757. if (!silly--) {
  758. printk(KERN_WARNING "INFTL: infinite loop in "
  759. "Virtual Unit Chain 0x%lx\n",
  760. block / (inftl->EraseSize / SECTORSIZE));
  761. return 1;
  762. }
  763. thisEUN = inftl->PUtable[thisEUN];
  764. }
  765. foundit:
  766. if (thisEUN == BLOCK_NIL) {
  767. /* The requested block is not on the media, return all 0x00 */
  768. memset(buffer, 0, SECTORSIZE);
  769. } else {
  770. size_t retlen;
  771. loff_t ptr = (thisEUN * inftl->EraseSize) + blockofs;
  772. int ret = mtd_read(mtd, ptr, SECTORSIZE, &retlen, buffer);
  773. /* Handle corrected bit flips gracefully */
  774. if (ret < 0 && !mtd_is_bitflip(ret))
  775. return -EIO;
  776. }
  777. return 0;
  778. }
  779. static int inftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
  780. {
  781. struct INFTLrecord *inftl = (void *)dev;
  782. geo->heads = inftl->heads;
  783. geo->sectors = inftl->sectors;
  784. geo->cylinders = inftl->cylinders;
  785. return 0;
  786. }
  787. static struct mtd_blktrans_ops inftl_tr = {
  788. .name = "inftl",
  789. .major = INFTL_MAJOR,
  790. .part_bits = INFTL_PARTN_BITS,
  791. .blksize = 512,
  792. .getgeo = inftl_getgeo,
  793. .readsect = inftl_readblock,
  794. .writesect = inftl_writeblock,
  795. .add_mtd = inftl_add_mtd,
  796. .remove_dev = inftl_remove_dev,
  797. .owner = THIS_MODULE,
  798. };
  799. module_mtd_blktrans(inftl_tr);
  800. MODULE_LICENSE("GPL");
  801. MODULE_AUTHOR("Greg Ungerer <[email protected]>, David Woodhouse <[email protected]>, Fabrice Bellard <[email protected]> et al.");
  802. MODULE_DESCRIPTION("Support code for Inverse Flash Translation Layer, used on M-Systems DiskOnChip 2000, Millennium and Millennium Plus");