inftlcore.c 24 KB

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