inftlcore.c 24 KB

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