inftlcore.c 25 KB

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