inftlcore.c 25 KB

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