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