inftlcore.c 24 KB

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