inftlcore.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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. if (INFTL_formatblock(inftl, thisEUN) < 0) {
  334. /*
  335. * Could not erase : mark block as reserved.
  336. */
  337. inftl->PUtable[thisEUN] = BLOCK_RESERVED;
  338. } else {
  339. /* Correctly erased : mark it as free */
  340. inftl->PUtable[thisEUN] = BLOCK_FREE;
  341. inftl->PUtable[prevEUN] = BLOCK_NIL;
  342. inftl->numfreeEUNs++;
  343. }
  344. }
  345. return targetEUN;
  346. }
  347. static u16 INFTL_makefreeblock(struct INFTLrecord *inftl, unsigned pendingblock)
  348. {
  349. /*
  350. * This is the part that needs some cleverness applied.
  351. * For now, I'm doing the minimum applicable to actually
  352. * get the thing to work.
  353. * Wear-levelling and other clever stuff needs to be implemented
  354. * and we also need to do some assessment of the results when
  355. * the system loses power half-way through the routine.
  356. */
  357. u16 LongestChain = 0;
  358. u16 ChainLength = 0, thislen;
  359. u16 chain, EUN;
  360. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_makefreeblock(inftl=%p,"
  361. "pending=%d)\n", inftl, pendingblock);
  362. for (chain = 0; chain < inftl->nb_blocks; chain++) {
  363. EUN = inftl->VUtable[chain];
  364. thislen = 0;
  365. while (EUN <= inftl->lastEUN) {
  366. thislen++;
  367. EUN = inftl->PUtable[EUN];
  368. if (thislen > 0xff00) {
  369. printk(KERN_WARNING "INFTL: endless loop in "
  370. "Virtual Chain %d: Unit %x\n",
  371. chain, EUN);
  372. /*
  373. * Actually, don't return failure.
  374. * Just ignore this chain and get on with it.
  375. */
  376. thislen = 0;
  377. break;
  378. }
  379. }
  380. if (thislen > ChainLength) {
  381. ChainLength = thislen;
  382. LongestChain = chain;
  383. }
  384. }
  385. if (ChainLength < 2) {
  386. printk(KERN_WARNING "INFTL: no Virtual Unit Chains available "
  387. "for folding. Failing request\n");
  388. return BLOCK_NIL;
  389. }
  390. return INFTL_foldchain(inftl, LongestChain, pendingblock);
  391. }
  392. static int nrbits(unsigned int val, int bitcount)
  393. {
  394. int i, total = 0;
  395. for (i = 0; (i < bitcount); i++)
  396. total += (((0x1 << i) & val) ? 1 : 0);
  397. return total;
  398. }
  399. /*
  400. * INFTL_findwriteunit: Return the unit number into which we can write
  401. * for this block. Make it available if it isn't already.
  402. */
  403. static inline u16 INFTL_findwriteunit(struct INFTLrecord *inftl, unsigned block)
  404. {
  405. unsigned int thisVUC = block / (inftl->EraseSize / SECTORSIZE);
  406. unsigned int thisEUN, writeEUN, prev_block, status;
  407. unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize -1);
  408. struct mtd_info *mtd = inftl->mbd.mtd;
  409. struct inftl_oob oob;
  410. struct inftl_bci bci;
  411. unsigned char anac, nacs, parity;
  412. size_t retlen;
  413. int silly, silly2 = 3;
  414. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_findwriteunit(inftl=%p,"
  415. "block=%d)\n", inftl, block);
  416. do {
  417. /*
  418. * Scan the media to find a unit in the VUC which has
  419. * a free space for the block in question.
  420. */
  421. writeEUN = BLOCK_NIL;
  422. thisEUN = inftl->VUtable[thisVUC];
  423. silly = MAX_LOOPS;
  424. while (thisEUN <= inftl->lastEUN) {
  425. inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) +
  426. blockofs, 8, &retlen, (char *)&bci);
  427. status = bci.Status | bci.Status1;
  428. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: status of block %d in "
  429. "EUN %d is %x\n", block , writeEUN, status);
  430. switch(status) {
  431. case SECTOR_FREE:
  432. writeEUN = thisEUN;
  433. break;
  434. case SECTOR_DELETED:
  435. case SECTOR_USED:
  436. /* Can't go any further */
  437. goto hitused;
  438. case SECTOR_IGNORE:
  439. break;
  440. default:
  441. /*
  442. * Invalid block. Don't use it any more.
  443. * Must implement.
  444. */
  445. break;
  446. }
  447. if (!silly--) {
  448. printk(KERN_WARNING "INFTL: infinite loop in "
  449. "Virtual Unit Chain 0x%x\n", thisVUC);
  450. return 0xffff;
  451. }
  452. /* Skip to next block in chain */
  453. thisEUN = inftl->PUtable[thisEUN];
  454. }
  455. hitused:
  456. if (writeEUN != BLOCK_NIL)
  457. return writeEUN;
  458. /*
  459. * OK. We didn't find one in the existing chain, or there
  460. * is no existing chain. Allocate a new one.
  461. */
  462. writeEUN = INFTL_findfreeblock(inftl, 0);
  463. if (writeEUN == BLOCK_NIL) {
  464. /*
  465. * That didn't work - there were no free blocks just
  466. * waiting to be picked up. We're going to have to fold
  467. * a chain to make room.
  468. */
  469. thisEUN = INFTL_makefreeblock(inftl, 0xffff);
  470. /*
  471. * Hopefully we free something, lets try again.
  472. * This time we are desperate...
  473. */
  474. DEBUG(MTD_DEBUG_LEVEL1, "INFTL: using desperate==1 "
  475. "to find free EUN to accommodate write to "
  476. "VUC %d\n", thisVUC);
  477. writeEUN = INFTL_findfreeblock(inftl, 1);
  478. if (writeEUN == BLOCK_NIL) {
  479. /*
  480. * Ouch. This should never happen - we should
  481. * always be able to make some room somehow.
  482. * If we get here, we've allocated more storage
  483. * space than actual media, or our makefreeblock
  484. * routine is missing something.
  485. */
  486. printk(KERN_WARNING "INFTL: cannot make free "
  487. "space.\n");
  488. #ifdef DEBUG
  489. INFTL_dumptables(inftl);
  490. INFTL_dumpVUchains(inftl);
  491. #endif
  492. return BLOCK_NIL;
  493. }
  494. }
  495. /*
  496. * Insert new block into virtual chain. Firstly update the
  497. * block headers in flash...
  498. */
  499. anac = 0;
  500. nacs = 0;
  501. thisEUN = inftl->VUtable[thisVUC];
  502. if (thisEUN != BLOCK_NIL) {
  503. inftl_read_oob(mtd, thisEUN * inftl->EraseSize
  504. + 8, 8, &retlen, (char *)&oob.u);
  505. anac = oob.u.a.ANAC + 1;
  506. nacs = oob.u.a.NACs + 1;
  507. }
  508. prev_block = inftl->VUtable[thisVUC];
  509. if (prev_block < inftl->nb_blocks)
  510. prev_block -= inftl->firstEUN;
  511. parity = (nrbits(thisVUC, 16) & 0x1) ? 0x1 : 0;
  512. parity |= (nrbits(prev_block, 16) & 0x1) ? 0x2 : 0;
  513. parity |= (nrbits(anac, 8) & 0x1) ? 0x4 : 0;
  514. parity |= (nrbits(nacs, 8) & 0x1) ? 0x8 : 0;
  515. oob.u.a.virtualUnitNo = cpu_to_le16(thisVUC);
  516. oob.u.a.prevUnitNo = cpu_to_le16(prev_block);
  517. oob.u.a.ANAC = anac;
  518. oob.u.a.NACs = nacs;
  519. oob.u.a.parityPerField = parity;
  520. oob.u.a.discarded = 0xaa;
  521. inftl_write_oob(mtd, writeEUN * inftl->EraseSize + 8, 8,
  522. &retlen, (char *)&oob.u);
  523. /* Also back up header... */
  524. oob.u.b.virtualUnitNo = cpu_to_le16(thisVUC);
  525. oob.u.b.prevUnitNo = cpu_to_le16(prev_block);
  526. oob.u.b.ANAC = anac;
  527. oob.u.b.NACs = nacs;
  528. oob.u.b.parityPerField = parity;
  529. oob.u.b.discarded = 0xaa;
  530. inftl_write_oob(mtd, writeEUN * inftl->EraseSize +
  531. SECTORSIZE * 4 + 8, 8, &retlen, (char *)&oob.u);
  532. inftl->PUtable[writeEUN] = inftl->VUtable[thisVUC];
  533. inftl->VUtable[thisVUC] = writeEUN;
  534. inftl->numfreeEUNs--;
  535. return writeEUN;
  536. } while (silly2--);
  537. printk(KERN_WARNING "INFTL: error folding to make room for Virtual "
  538. "Unit Chain 0x%x\n", thisVUC);
  539. return 0xffff;
  540. }
  541. /*
  542. * Given a Virtual Unit Chain, see if it can be deleted, and if so do it.
  543. */
  544. static void INFTL_trydeletechain(struct INFTLrecord *inftl, unsigned thisVUC)
  545. {
  546. struct mtd_info *mtd = inftl->mbd.mtd;
  547. unsigned char BlockUsed[MAX_SECTORS_PER_UNIT];
  548. unsigned char BlockDeleted[MAX_SECTORS_PER_UNIT];
  549. unsigned int thisEUN, status;
  550. int block, silly;
  551. struct inftl_bci bci;
  552. size_t retlen;
  553. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_trydeletechain(inftl=%p,"
  554. "thisVUC=%d)\n", inftl, thisVUC);
  555. memset(BlockUsed, 0, sizeof(BlockUsed));
  556. memset(BlockDeleted, 0, sizeof(BlockDeleted));
  557. thisEUN = inftl->VUtable[thisVUC];
  558. if (thisEUN == BLOCK_NIL) {
  559. printk(KERN_WARNING "INFTL: trying to delete non-existent "
  560. "Virtual Unit Chain %d!\n", thisVUC);
  561. return;
  562. }
  563. /*
  564. * Scan through the Erase Units to determine whether any data is in
  565. * each of the 512-byte blocks within the Chain.
  566. */
  567. silly = MAX_LOOPS;
  568. while (thisEUN < inftl->nb_blocks) {
  569. for (block = 0; block < inftl->EraseSize/SECTORSIZE; block++) {
  570. if (BlockUsed[block] || BlockDeleted[block])
  571. continue;
  572. if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize)
  573. + (block * SECTORSIZE), 8 , &retlen,
  574. (char *)&bci) < 0)
  575. status = SECTOR_IGNORE;
  576. else
  577. status = bci.Status | bci.Status1;
  578. switch(status) {
  579. case SECTOR_FREE:
  580. case SECTOR_IGNORE:
  581. break;
  582. case SECTOR_USED:
  583. BlockUsed[block] = 1;
  584. continue;
  585. case SECTOR_DELETED:
  586. BlockDeleted[block] = 1;
  587. continue;
  588. default:
  589. printk(KERN_WARNING "INFTL: unknown status "
  590. "for block %d in EUN %d: 0x%x\n",
  591. block, thisEUN, status);
  592. }
  593. }
  594. if (!silly--) {
  595. printk(KERN_WARNING "INFTL: infinite loop in Virtual "
  596. "Unit Chain 0x%x\n", thisVUC);
  597. return;
  598. }
  599. thisEUN = inftl->PUtable[thisEUN];
  600. }
  601. for (block = 0; block < inftl->EraseSize/SECTORSIZE; block++)
  602. if (BlockUsed[block])
  603. return;
  604. /*
  605. * For each block in the chain free it and make it available
  606. * for future use. Erase from the oldest unit first.
  607. */
  608. DEBUG(MTD_DEBUG_LEVEL1, "INFTL: deleting empty VUC %d\n", thisVUC);
  609. for (;;) {
  610. u16 *prevEUN = &inftl->VUtable[thisVUC];
  611. thisEUN = *prevEUN;
  612. /* If the chain is all gone already, we're done */
  613. if (thisEUN == BLOCK_NIL) {
  614. DEBUG(MTD_DEBUG_LEVEL2, "INFTL: Empty VUC %d for deletion was already absent\n", thisEUN);
  615. return;
  616. }
  617. /* Find oldest unit in chain. */
  618. while (inftl->PUtable[thisEUN] != BLOCK_NIL) {
  619. BUG_ON(thisEUN >= inftl->nb_blocks);
  620. prevEUN = &inftl->PUtable[thisEUN];
  621. thisEUN = *prevEUN;
  622. }
  623. DEBUG(MTD_DEBUG_LEVEL3, "Deleting EUN %d from VUC %d\n",
  624. thisEUN, thisVUC);
  625. if (INFTL_formatblock(inftl, thisEUN) < 0) {
  626. /*
  627. * Could not erase : mark block as reserved.
  628. */
  629. inftl->PUtable[thisEUN] = BLOCK_RESERVED;
  630. } else {
  631. /* Correctly erased : mark it as free */
  632. inftl->PUtable[thisEUN] = BLOCK_FREE;
  633. inftl->numfreeEUNs++;
  634. }
  635. /* Now sort out whatever was pointing to it... */
  636. *prevEUN = BLOCK_NIL;
  637. /* Ideally we'd actually be responsive to new
  638. requests while we're doing this -- if there's
  639. free space why should others be made to wait? */
  640. cond_resched();
  641. }
  642. inftl->VUtable[thisVUC] = BLOCK_NIL;
  643. }
  644. static int INFTL_deleteblock(struct INFTLrecord *inftl, unsigned block)
  645. {
  646. unsigned int thisEUN = inftl->VUtable[block / (inftl->EraseSize / SECTORSIZE)];
  647. unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize - 1);
  648. struct mtd_info *mtd = inftl->mbd.mtd;
  649. unsigned int status;
  650. int silly = MAX_LOOPS;
  651. size_t retlen;
  652. struct inftl_bci bci;
  653. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_deleteblock(inftl=%p,"
  654. "block=%d)\n", inftl, block);
  655. while (thisEUN < inftl->nb_blocks) {
  656. if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) +
  657. blockofs, 8, &retlen, (char *)&bci) < 0)
  658. status = SECTOR_IGNORE;
  659. else
  660. status = bci.Status | bci.Status1;
  661. switch (status) {
  662. case SECTOR_FREE:
  663. case SECTOR_IGNORE:
  664. break;
  665. case SECTOR_DELETED:
  666. thisEUN = BLOCK_NIL;
  667. goto foundit;
  668. case SECTOR_USED:
  669. goto foundit;
  670. default:
  671. printk(KERN_WARNING "INFTL: unknown status for "
  672. "block %d in EUN %d: 0x%x\n",
  673. block, thisEUN, status);
  674. break;
  675. }
  676. if (!silly--) {
  677. printk(KERN_WARNING "INFTL: infinite loop in Virtual "
  678. "Unit Chain 0x%x\n",
  679. block / (inftl->EraseSize / SECTORSIZE));
  680. return 1;
  681. }
  682. thisEUN = inftl->PUtable[thisEUN];
  683. }
  684. foundit:
  685. if (thisEUN != BLOCK_NIL) {
  686. loff_t ptr = (thisEUN * inftl->EraseSize) + blockofs;
  687. if (inftl_read_oob(mtd, ptr, 8, &retlen, (char *)&bci) < 0)
  688. return -EIO;
  689. bci.Status = bci.Status1 = SECTOR_DELETED;
  690. if (inftl_write_oob(mtd, ptr, 8, &retlen, (char *)&bci) < 0)
  691. return -EIO;
  692. INFTL_trydeletechain(inftl, block / (inftl->EraseSize / SECTORSIZE));
  693. }
  694. return 0;
  695. }
  696. static int inftl_writeblock(struct mtd_blktrans_dev *mbd, unsigned long block,
  697. char *buffer)
  698. {
  699. struct INFTLrecord *inftl = (void *)mbd;
  700. unsigned int writeEUN;
  701. unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize - 1);
  702. size_t retlen;
  703. struct inftl_oob oob;
  704. char *p, *pend;
  705. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: inftl_writeblock(inftl=%p,block=%ld,"
  706. "buffer=%p)\n", inftl, block, buffer);
  707. /* Is block all zero? */
  708. pend = buffer + SECTORSIZE;
  709. for (p = buffer; p < pend && !*p; p++)
  710. ;
  711. if (p < pend) {
  712. writeEUN = INFTL_findwriteunit(inftl, block);
  713. if (writeEUN == BLOCK_NIL) {
  714. printk(KERN_WARNING "inftl_writeblock(): cannot find "
  715. "block to write to\n");
  716. /*
  717. * If we _still_ haven't got a block to use,
  718. * we're screwed.
  719. */
  720. return 1;
  721. }
  722. memset(&oob, 0xff, sizeof(struct inftl_oob));
  723. oob.b.Status = oob.b.Status1 = SECTOR_USED;
  724. inftl_write(inftl->mbd.mtd, (writeEUN * inftl->EraseSize) +
  725. blockofs, SECTORSIZE, &retlen, (char *)buffer,
  726. (char *)&oob);
  727. /*
  728. * need to write SECTOR_USED flags since they are not written
  729. * in mtd_writeecc
  730. */
  731. } else {
  732. INFTL_deleteblock(inftl, block);
  733. }
  734. return 0;
  735. }
  736. static int inftl_readblock(struct mtd_blktrans_dev *mbd, unsigned long block,
  737. char *buffer)
  738. {
  739. struct INFTLrecord *inftl = (void *)mbd;
  740. unsigned int thisEUN = inftl->VUtable[block / (inftl->EraseSize / SECTORSIZE)];
  741. unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize - 1);
  742. struct mtd_info *mtd = inftl->mbd.mtd;
  743. unsigned int status;
  744. int silly = MAX_LOOPS;
  745. struct inftl_bci bci;
  746. size_t retlen;
  747. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: inftl_readblock(inftl=%p,block=%ld,"
  748. "buffer=%p)\n", inftl, block, buffer);
  749. while (thisEUN < inftl->nb_blocks) {
  750. if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) +
  751. blockofs, 8, &retlen, (char *)&bci) < 0)
  752. status = SECTOR_IGNORE;
  753. else
  754. status = bci.Status | bci.Status1;
  755. switch (status) {
  756. case SECTOR_DELETED:
  757. thisEUN = BLOCK_NIL;
  758. goto foundit;
  759. case SECTOR_USED:
  760. goto foundit;
  761. case SECTOR_FREE:
  762. case SECTOR_IGNORE:
  763. break;
  764. default:
  765. printk(KERN_WARNING "INFTL: unknown status for "
  766. "block %ld in EUN %d: 0x%04x\n",
  767. block, thisEUN, status);
  768. break;
  769. }
  770. if (!silly--) {
  771. printk(KERN_WARNING "INFTL: infinite loop in "
  772. "Virtual Unit Chain 0x%lx\n",
  773. block / (inftl->EraseSize / SECTORSIZE));
  774. return 1;
  775. }
  776. thisEUN = inftl->PUtable[thisEUN];
  777. }
  778. foundit:
  779. if (thisEUN == BLOCK_NIL) {
  780. /* The requested block is not on the media, return all 0x00 */
  781. memset(buffer, 0, SECTORSIZE);
  782. } else {
  783. size_t retlen;
  784. loff_t ptr = (thisEUN * inftl->EraseSize) + blockofs;
  785. int ret = mtd->read(mtd, ptr, SECTORSIZE, &retlen, buffer);
  786. /* Handle corrected bit flips gracefully */
  787. if (ret < 0 && ret != -EUCLEAN)
  788. return -EIO;
  789. }
  790. return 0;
  791. }
  792. static int inftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
  793. {
  794. struct INFTLrecord *inftl = (void *)dev;
  795. geo->heads = inftl->heads;
  796. geo->sectors = inftl->sectors;
  797. geo->cylinders = inftl->cylinders;
  798. return 0;
  799. }
  800. static struct mtd_blktrans_ops inftl_tr = {
  801. .name = "inftl",
  802. .major = INFTL_MAJOR,
  803. .part_bits = INFTL_PARTN_BITS,
  804. .blksize = 512,
  805. .getgeo = inftl_getgeo,
  806. .readsect = inftl_readblock,
  807. .writesect = inftl_writeblock,
  808. .add_mtd = inftl_add_mtd,
  809. .remove_dev = inftl_remove_dev,
  810. .owner = THIS_MODULE,
  811. };
  812. static int __init init_inftl(void)
  813. {
  814. return register_mtd_blktrans(&inftl_tr);
  815. }
  816. static void __exit cleanup_inftl(void)
  817. {
  818. deregister_mtd_blktrans(&inftl_tr);
  819. }
  820. module_init(init_inftl);
  821. module_exit(cleanup_inftl);
  822. MODULE_LICENSE("GPL");
  823. MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com>, David Woodhouse <dwmw2@infradead.org>, Fabrice Bellard <fabrice.bellard@netgem.com> et al.");
  824. MODULE_DESCRIPTION("Support code for Inverse Flash Translation Layer, used on M-Systems DiskOnChip 2000, Millennium and Millennium Plus");