nftlcore.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /* Linux driver for NAND Flash Translation Layer */
  2. /* (c) 1999 Machine Vision Holdings, Inc. */
  3. /* Author: David Woodhouse <dwmw2@infradead.org> */
  4. /* $Id: nftlcore.c,v 1.98 2005/11/07 11:14:21 gleixner Exp $ */
  5. /*
  6. The contents of this file are distributed under the GNU General
  7. Public License version 2. The author places no additional
  8. restrictions of any kind on it.
  9. */
  10. #define PRERELEASE
  11. #include <linux/config.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <asm/errno.h>
  15. #include <asm/io.h>
  16. #include <asm/uaccess.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/pci.h>
  19. #include <linux/delay.h>
  20. #include <linux/slab.h>
  21. #include <linux/sched.h>
  22. #include <linux/init.h>
  23. #include <linux/hdreg.h>
  24. #include <linux/kmod.h>
  25. #include <linux/mtd/mtd.h>
  26. #include <linux/mtd/nand.h>
  27. #include <linux/mtd/nftl.h>
  28. #include <linux/mtd/blktrans.h>
  29. /* maximum number of loops while examining next block, to have a
  30. chance to detect consistency problems (they should never happen
  31. because of the checks done in the mounting */
  32. #define MAX_LOOPS 10000
  33. static void nftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
  34. {
  35. struct NFTLrecord *nftl;
  36. unsigned long temp;
  37. if (mtd->type != MTD_NANDFLASH)
  38. return;
  39. /* OK, this is moderately ugly. But probably safe. Alternatives? */
  40. if (memcmp(mtd->name, "DiskOnChip", 10))
  41. return;
  42. if (!mtd->block_isbad) {
  43. printk(KERN_ERR
  44. "NFTL no longer supports the old DiskOnChip drivers loaded via docprobe.\n"
  45. "Please use the new diskonchip driver under the NAND subsystem.\n");
  46. return;
  47. }
  48. DEBUG(MTD_DEBUG_LEVEL1, "NFTL: add_mtd for %s\n", mtd->name);
  49. nftl = kmalloc(sizeof(struct NFTLrecord), GFP_KERNEL);
  50. if (!nftl) {
  51. printk(KERN_WARNING "NFTL: out of memory for data structures\n");
  52. return;
  53. }
  54. memset(nftl, 0, sizeof(*nftl));
  55. nftl->mbd.mtd = mtd;
  56. nftl->mbd.devnum = -1;
  57. nftl->mbd.blksize = 512;
  58. nftl->mbd.tr = tr;
  59. memcpy(&nftl->oobinfo, &mtd->oobinfo, sizeof(struct nand_oobinfo));
  60. nftl->oobinfo.useecc = MTD_NANDECC_PLACEONLY;
  61. if (NFTL_mount(nftl) < 0) {
  62. printk(KERN_WARNING "NFTL: could not mount device\n");
  63. kfree(nftl);
  64. return;
  65. }
  66. /* OK, it's a new one. Set up all the data structures. */
  67. /* Calculate geometry */
  68. nftl->cylinders = 1024;
  69. nftl->heads = 16;
  70. temp = nftl->cylinders * nftl->heads;
  71. nftl->sectors = nftl->mbd.size / temp;
  72. if (nftl->mbd.size % temp) {
  73. nftl->sectors++;
  74. temp = nftl->cylinders * nftl->sectors;
  75. nftl->heads = nftl->mbd.size / temp;
  76. if (nftl->mbd.size % temp) {
  77. nftl->heads++;
  78. temp = nftl->heads * nftl->sectors;
  79. nftl->cylinders = nftl->mbd.size / temp;
  80. }
  81. }
  82. if (nftl->mbd.size != nftl->heads * nftl->cylinders * nftl->sectors) {
  83. /*
  84. Oh no we don't have
  85. mbd.size == heads * cylinders * sectors
  86. */
  87. printk(KERN_WARNING "NFTL: cannot calculate a geometry to "
  88. "match size of 0x%lx.\n", nftl->mbd.size);
  89. printk(KERN_WARNING "NFTL: using C:%d H:%d S:%d "
  90. "(== 0x%lx sects)\n",
  91. nftl->cylinders, nftl->heads , nftl->sectors,
  92. (long)nftl->cylinders * (long)nftl->heads *
  93. (long)nftl->sectors );
  94. }
  95. if (add_mtd_blktrans_dev(&nftl->mbd)) {
  96. kfree(nftl->ReplUnitTable);
  97. kfree(nftl->EUNtable);
  98. kfree(nftl);
  99. return;
  100. }
  101. #ifdef PSYCHO_DEBUG
  102. printk(KERN_INFO "NFTL: Found new nftl%c\n", nftl->mbd.devnum + 'a');
  103. #endif
  104. }
  105. static void nftl_remove_dev(struct mtd_blktrans_dev *dev)
  106. {
  107. struct NFTLrecord *nftl = (void *)dev;
  108. DEBUG(MTD_DEBUG_LEVEL1, "NFTL: remove_dev (i=%d)\n", dev->devnum);
  109. del_mtd_blktrans_dev(dev);
  110. kfree(nftl->ReplUnitTable);
  111. kfree(nftl->EUNtable);
  112. kfree(nftl);
  113. }
  114. #ifdef CONFIG_NFTL_RW
  115. /* Actual NFTL access routines */
  116. /* NFTL_findfreeblock: Find a free Erase Unit on the NFTL partition. This function is used
  117. * when the give Virtual Unit Chain
  118. */
  119. static u16 NFTL_findfreeblock(struct NFTLrecord *nftl, int desperate )
  120. {
  121. /* For a given Virtual Unit Chain: find or create a free block and
  122. add it to the chain */
  123. /* We're passed the number of the last EUN in the chain, to save us from
  124. having to look it up again */
  125. u16 pot = nftl->LastFreeEUN;
  126. int silly = nftl->nb_blocks;
  127. /* Normally, we force a fold to happen before we run out of free blocks completely */
  128. if (!desperate && nftl->numfreeEUNs < 2) {
  129. DEBUG(MTD_DEBUG_LEVEL1, "NFTL_findfreeblock: there are too few free EUNs\n");
  130. return 0xffff;
  131. }
  132. /* Scan for a free block */
  133. do {
  134. if (nftl->ReplUnitTable[pot] == BLOCK_FREE) {
  135. nftl->LastFreeEUN = pot;
  136. nftl->numfreeEUNs--;
  137. return pot;
  138. }
  139. /* This will probably point to the MediaHdr unit itself,
  140. right at the beginning of the partition. But that unit
  141. (and the backup unit too) should have the UCI set
  142. up so that it's not selected for overwriting */
  143. if (++pot > nftl->lastEUN)
  144. pot = le16_to_cpu(nftl->MediaHdr.FirstPhysicalEUN);
  145. if (!silly--) {
  146. printk("Argh! No free blocks found! LastFreeEUN = %d, "
  147. "FirstEUN = %d\n", nftl->LastFreeEUN,
  148. le16_to_cpu(nftl->MediaHdr.FirstPhysicalEUN));
  149. return 0xffff;
  150. }
  151. } while (pot != nftl->LastFreeEUN);
  152. return 0xffff;
  153. }
  154. static u16 NFTL_foldchain (struct NFTLrecord *nftl, unsigned thisVUC, unsigned pendingblock )
  155. {
  156. u16 BlockMap[MAX_SECTORS_PER_UNIT];
  157. unsigned char BlockLastState[MAX_SECTORS_PER_UNIT];
  158. unsigned char BlockFreeFound[MAX_SECTORS_PER_UNIT];
  159. unsigned int thisEUN;
  160. int block;
  161. int silly;
  162. unsigned int targetEUN;
  163. struct nftl_oob oob;
  164. int inplace = 1;
  165. size_t retlen;
  166. memset(BlockMap, 0xff, sizeof(BlockMap));
  167. memset(BlockFreeFound, 0, sizeof(BlockFreeFound));
  168. thisEUN = nftl->EUNtable[thisVUC];
  169. if (thisEUN == BLOCK_NIL) {
  170. printk(KERN_WARNING "Trying to fold non-existent "
  171. "Virtual Unit Chain %d!\n", thisVUC);
  172. return BLOCK_NIL;
  173. }
  174. /* Scan to find the Erase Unit which holds the actual data for each
  175. 512-byte block within the Chain.
  176. */
  177. silly = MAX_LOOPS;
  178. targetEUN = BLOCK_NIL;
  179. while (thisEUN <= nftl->lastEUN ) {
  180. unsigned int status, foldmark;
  181. targetEUN = thisEUN;
  182. for (block = 0; block < nftl->EraseSize / 512; block ++) {
  183. MTD_READOOB(nftl->mbd.mtd,
  184. (thisEUN * nftl->EraseSize) + (block * 512),
  185. 16 , &retlen, (char *)&oob);
  186. if (block == 2) {
  187. foldmark = oob.u.c.FoldMark | oob.u.c.FoldMark1;
  188. if (foldmark == FOLD_MARK_IN_PROGRESS) {
  189. DEBUG(MTD_DEBUG_LEVEL1,
  190. "Write Inhibited on EUN %d\n", thisEUN);
  191. inplace = 0;
  192. } else {
  193. /* There's no other reason not to do inplace,
  194. except ones that come later. So we don't need
  195. to preserve inplace */
  196. inplace = 1;
  197. }
  198. }
  199. status = oob.b.Status | oob.b.Status1;
  200. BlockLastState[block] = status;
  201. switch(status) {
  202. case SECTOR_FREE:
  203. BlockFreeFound[block] = 1;
  204. break;
  205. case SECTOR_USED:
  206. if (!BlockFreeFound[block])
  207. BlockMap[block] = thisEUN;
  208. else
  209. printk(KERN_WARNING
  210. "SECTOR_USED found after SECTOR_FREE "
  211. "in Virtual Unit Chain %d for block %d\n",
  212. thisVUC, block);
  213. break;
  214. case SECTOR_DELETED:
  215. if (!BlockFreeFound[block])
  216. BlockMap[block] = BLOCK_NIL;
  217. else
  218. printk(KERN_WARNING
  219. "SECTOR_DELETED found after SECTOR_FREE "
  220. "in Virtual Unit Chain %d for block %d\n",
  221. thisVUC, block);
  222. break;
  223. case SECTOR_IGNORE:
  224. break;
  225. default:
  226. printk("Unknown status for block %d in EUN %d: %x\n",
  227. block, thisEUN, status);
  228. }
  229. }
  230. if (!silly--) {
  231. printk(KERN_WARNING "Infinite loop in Virtual Unit Chain 0x%x\n",
  232. thisVUC);
  233. return BLOCK_NIL;
  234. }
  235. thisEUN = nftl->ReplUnitTable[thisEUN];
  236. }
  237. if (inplace) {
  238. /* We're being asked to be a fold-in-place. Check
  239. that all blocks which actually have data associated
  240. with them (i.e. BlockMap[block] != BLOCK_NIL) are
  241. either already present or SECTOR_FREE in the target
  242. block. If not, we're going to have to fold out-of-place
  243. anyway.
  244. */
  245. for (block = 0; block < nftl->EraseSize / 512 ; block++) {
  246. if (BlockLastState[block] != SECTOR_FREE &&
  247. BlockMap[block] != BLOCK_NIL &&
  248. BlockMap[block] != targetEUN) {
  249. DEBUG(MTD_DEBUG_LEVEL1, "Setting inplace to 0. VUC %d, "
  250. "block %d was %x lastEUN, "
  251. "and is in EUN %d (%s) %d\n",
  252. thisVUC, block, BlockLastState[block],
  253. BlockMap[block],
  254. BlockMap[block]== targetEUN ? "==" : "!=",
  255. targetEUN);
  256. inplace = 0;
  257. break;
  258. }
  259. }
  260. if (pendingblock >= (thisVUC * (nftl->EraseSize / 512)) &&
  261. pendingblock < ((thisVUC + 1)* (nftl->EraseSize / 512)) &&
  262. BlockLastState[pendingblock - (thisVUC * (nftl->EraseSize / 512))] !=
  263. SECTOR_FREE) {
  264. DEBUG(MTD_DEBUG_LEVEL1, "Pending write not free in EUN %d. "
  265. "Folding out of place.\n", targetEUN);
  266. inplace = 0;
  267. }
  268. }
  269. if (!inplace) {
  270. DEBUG(MTD_DEBUG_LEVEL1, "Cannot fold Virtual Unit Chain %d in place. "
  271. "Trying out-of-place\n", thisVUC);
  272. /* We need to find a targetEUN to fold into. */
  273. targetEUN = NFTL_findfreeblock(nftl, 1);
  274. if (targetEUN == BLOCK_NIL) {
  275. /* Ouch. Now we're screwed. We need to do a
  276. fold-in-place of another chain to make room
  277. for this one. We need a better way of selecting
  278. which chain to fold, because makefreeblock will
  279. only ask us to fold the same one again.
  280. */
  281. printk(KERN_WARNING
  282. "NFTL_findfreeblock(desperate) returns 0xffff.\n");
  283. return BLOCK_NIL;
  284. }
  285. } else {
  286. /* We put a fold mark in the chain we are folding only if
  287. we fold in place to help the mount check code. If we do
  288. not fold in place, it is possible to find the valid
  289. chain by selecting the longer one */
  290. oob.u.c.FoldMark = oob.u.c.FoldMark1 = cpu_to_le16(FOLD_MARK_IN_PROGRESS);
  291. oob.u.c.unused = 0xffffffff;
  292. MTD_WRITEOOB(nftl->mbd.mtd, (nftl->EraseSize * targetEUN) + 2 * 512 + 8,
  293. 8, &retlen, (char *)&oob.u);
  294. }
  295. /* OK. We now know the location of every block in the Virtual Unit Chain,
  296. and the Erase Unit into which we are supposed to be copying.
  297. Go for it.
  298. */
  299. DEBUG(MTD_DEBUG_LEVEL1,"Folding chain %d into unit %d\n", thisVUC, targetEUN);
  300. for (block = 0; block < nftl->EraseSize / 512 ; block++) {
  301. unsigned char movebuf[512];
  302. int ret;
  303. /* If it's in the target EUN already, or if it's pending write, do nothing */
  304. if (BlockMap[block] == targetEUN ||
  305. (pendingblock == (thisVUC * (nftl->EraseSize / 512) + block))) {
  306. continue;
  307. }
  308. /* copy only in non free block (free blocks can only
  309. happen in case of media errors or deleted blocks) */
  310. if (BlockMap[block] == BLOCK_NIL)
  311. continue;
  312. ret = MTD_READ(nftl->mbd.mtd, (nftl->EraseSize * BlockMap[block]) + (block * 512),
  313. 512, &retlen, movebuf);
  314. if (ret < 0) {
  315. ret = MTD_READ(nftl->mbd.mtd, (nftl->EraseSize * BlockMap[block])
  316. + (block * 512), 512, &retlen,
  317. movebuf);
  318. if (ret != -EIO)
  319. printk("Error went away on retry.\n");
  320. }
  321. memset(&oob, 0xff, sizeof(struct nftl_oob));
  322. oob.b.Status = oob.b.Status1 = SECTOR_USED;
  323. MTD_WRITEECC(nftl->mbd.mtd, (nftl->EraseSize * targetEUN) + (block * 512),
  324. 512, &retlen, movebuf, (char *)&oob, &nftl->oobinfo);
  325. }
  326. /* add the header so that it is now a valid chain */
  327. oob.u.a.VirtUnitNum = oob.u.a.SpareVirtUnitNum
  328. = cpu_to_le16(thisVUC);
  329. oob.u.a.ReplUnitNum = oob.u.a.SpareReplUnitNum = 0xffff;
  330. MTD_WRITEOOB(nftl->mbd.mtd, (nftl->EraseSize * targetEUN) + 8,
  331. 8, &retlen, (char *)&oob.u);
  332. /* OK. We've moved the whole lot into the new block. Now we have to free the original blocks. */
  333. /* At this point, we have two different chains for this Virtual Unit, and no way to tell
  334. them apart. If we crash now, we get confused. However, both contain the same data, so we
  335. shouldn't actually lose data in this case. It's just that when we load up on a medium which
  336. has duplicate chains, we need to free one of the chains because it's not necessary any more.
  337. */
  338. thisEUN = nftl->EUNtable[thisVUC];
  339. DEBUG(MTD_DEBUG_LEVEL1,"Want to erase\n");
  340. /* For each block in the old chain (except the targetEUN of course),
  341. free it and make it available for future use */
  342. while (thisEUN <= nftl->lastEUN && thisEUN != targetEUN) {
  343. unsigned int EUNtmp;
  344. EUNtmp = nftl->ReplUnitTable[thisEUN];
  345. if (NFTL_formatblock(nftl, thisEUN) < 0) {
  346. /* could not erase : mark block as reserved
  347. */
  348. nftl->ReplUnitTable[thisEUN] = BLOCK_RESERVED;
  349. } else {
  350. /* correctly erased : mark it as free */
  351. nftl->ReplUnitTable[thisEUN] = BLOCK_FREE;
  352. nftl->numfreeEUNs++;
  353. }
  354. thisEUN = EUNtmp;
  355. }
  356. /* Make this the new start of chain for thisVUC */
  357. nftl->ReplUnitTable[targetEUN] = BLOCK_NIL;
  358. nftl->EUNtable[thisVUC] = targetEUN;
  359. return targetEUN;
  360. }
  361. static u16 NFTL_makefreeblock( struct NFTLrecord *nftl , unsigned pendingblock)
  362. {
  363. /* This is the part that needs some cleverness applied.
  364. For now, I'm doing the minimum applicable to actually
  365. get the thing to work.
  366. Wear-levelling and other clever stuff needs to be implemented
  367. and we also need to do some assessment of the results when
  368. the system loses power half-way through the routine.
  369. */
  370. u16 LongestChain = 0;
  371. u16 ChainLength = 0, thislen;
  372. u16 chain, EUN;
  373. for (chain = 0; chain < le32_to_cpu(nftl->MediaHdr.FormattedSize) / nftl->EraseSize; chain++) {
  374. EUN = nftl->EUNtable[chain];
  375. thislen = 0;
  376. while (EUN <= nftl->lastEUN) {
  377. thislen++;
  378. //printk("VUC %d reaches len %d with EUN %d\n", chain, thislen, EUN);
  379. EUN = nftl->ReplUnitTable[EUN] & 0x7fff;
  380. if (thislen > 0xff00) {
  381. printk("Endless loop in Virtual Chain %d: Unit %x\n",
  382. chain, EUN);
  383. }
  384. if (thislen > 0xff10) {
  385. /* Actually, don't return failure. Just ignore this chain and
  386. get on with it. */
  387. thislen = 0;
  388. break;
  389. }
  390. }
  391. if (thislen > ChainLength) {
  392. //printk("New longest chain is %d with length %d\n", chain, thislen);
  393. ChainLength = thislen;
  394. LongestChain = chain;
  395. }
  396. }
  397. if (ChainLength < 2) {
  398. printk(KERN_WARNING "No Virtual Unit Chains available for folding. "
  399. "Failing request\n");
  400. return 0xffff;
  401. }
  402. return NFTL_foldchain (nftl, LongestChain, pendingblock);
  403. }
  404. /* NFTL_findwriteunit: Return the unit number into which we can write
  405. for this block. Make it available if it isn't already
  406. */
  407. static inline u16 NFTL_findwriteunit(struct NFTLrecord *nftl, unsigned block)
  408. {
  409. u16 lastEUN;
  410. u16 thisVUC = block / (nftl->EraseSize / 512);
  411. unsigned int writeEUN;
  412. unsigned long blockofs = (block * 512) & (nftl->EraseSize -1);
  413. size_t retlen;
  414. int silly, silly2 = 3;
  415. struct nftl_oob oob;
  416. do {
  417. /* Scan the media to find a unit in the VUC which has
  418. a free space for the block in question.
  419. */
  420. /* This condition catches the 0x[7f]fff cases, as well as
  421. being a sanity check for past-end-of-media access
  422. */
  423. lastEUN = BLOCK_NIL;
  424. writeEUN = nftl->EUNtable[thisVUC];
  425. silly = MAX_LOOPS;
  426. while (writeEUN <= nftl->lastEUN) {
  427. struct nftl_bci bci;
  428. size_t retlen;
  429. unsigned int status;
  430. lastEUN = writeEUN;
  431. MTD_READOOB(nftl->mbd.mtd, (writeEUN * nftl->EraseSize) + blockofs,
  432. 8, &retlen, (char *)&bci);
  433. DEBUG(MTD_DEBUG_LEVEL2, "Status of block %d in EUN %d is %x\n",
  434. block , writeEUN, le16_to_cpu(bci.Status));
  435. status = bci.Status | bci.Status1;
  436. switch(status) {
  437. case SECTOR_FREE:
  438. return writeEUN;
  439. case SECTOR_DELETED:
  440. case SECTOR_USED:
  441. case SECTOR_IGNORE:
  442. break;
  443. default:
  444. // Invalid block. Don't use it any more. Must implement.
  445. break;
  446. }
  447. if (!silly--) {
  448. printk(KERN_WARNING
  449. "Infinite loop in Virtual Unit Chain 0x%x\n",
  450. thisVUC);
  451. return 0xffff;
  452. }
  453. /* Skip to next block in chain */
  454. writeEUN = nftl->ReplUnitTable[writeEUN];
  455. }
  456. /* OK. We didn't find one in the existing chain, or there
  457. is no existing chain. */
  458. /* Try to find an already-free block */
  459. writeEUN = NFTL_findfreeblock(nftl, 0);
  460. if (writeEUN == BLOCK_NIL) {
  461. /* That didn't work - there were no free blocks just
  462. waiting to be picked up. We're going to have to fold
  463. a chain to make room.
  464. */
  465. /* First remember the start of this chain */
  466. //u16 startEUN = nftl->EUNtable[thisVUC];
  467. //printk("Write to VirtualUnitChain %d, calling makefreeblock()\n", thisVUC);
  468. writeEUN = NFTL_makefreeblock(nftl, 0xffff);
  469. if (writeEUN == BLOCK_NIL) {
  470. /* OK, we accept that the above comment is
  471. lying - there may have been free blocks
  472. last time we called NFTL_findfreeblock(),
  473. but they are reserved for when we're
  474. desperate. Well, now we're desperate.
  475. */
  476. DEBUG(MTD_DEBUG_LEVEL1, "Using desperate==1 to find free EUN to accommodate write to VUC %d\n", thisVUC);
  477. writeEUN = NFTL_findfreeblock(nftl, 1);
  478. }
  479. if (writeEUN == BLOCK_NIL) {
  480. /* Ouch. This should never happen - we should
  481. always be able to make some room somehow.
  482. If we get here, we've allocated more storage
  483. space than actual media, or our makefreeblock
  484. routine is missing something.
  485. */
  486. printk(KERN_WARNING "Cannot make free space.\n");
  487. return BLOCK_NIL;
  488. }
  489. //printk("Restarting scan\n");
  490. lastEUN = BLOCK_NIL;
  491. continue;
  492. }
  493. /* We've found a free block. Insert it into the chain. */
  494. if (lastEUN != BLOCK_NIL) {
  495. thisVUC |= 0x8000; /* It's a replacement block */
  496. } else {
  497. /* The first block in a new chain */
  498. nftl->EUNtable[thisVUC] = writeEUN;
  499. }
  500. /* set up the actual EUN we're writing into */
  501. /* Both in our cache... */
  502. nftl->ReplUnitTable[writeEUN] = BLOCK_NIL;
  503. /* ... and on the flash itself */
  504. MTD_READOOB(nftl->mbd.mtd, writeEUN * nftl->EraseSize + 8, 8,
  505. &retlen, (char *)&oob.u);
  506. oob.u.a.VirtUnitNum = oob.u.a.SpareVirtUnitNum = cpu_to_le16(thisVUC);
  507. MTD_WRITEOOB(nftl->mbd.mtd, writeEUN * nftl->EraseSize + 8, 8,
  508. &retlen, (char *)&oob.u);
  509. /* we link the new block to the chain only after the
  510. block is ready. It avoids the case where the chain
  511. could point to a free block */
  512. if (lastEUN != BLOCK_NIL) {
  513. /* Both in our cache... */
  514. nftl->ReplUnitTable[lastEUN] = writeEUN;
  515. /* ... and on the flash itself */
  516. MTD_READOOB(nftl->mbd.mtd, (lastEUN * nftl->EraseSize) + 8,
  517. 8, &retlen, (char *)&oob.u);
  518. oob.u.a.ReplUnitNum = oob.u.a.SpareReplUnitNum
  519. = cpu_to_le16(writeEUN);
  520. MTD_WRITEOOB(nftl->mbd.mtd, (lastEUN * nftl->EraseSize) + 8,
  521. 8, &retlen, (char *)&oob.u);
  522. }
  523. return writeEUN;
  524. } while (silly2--);
  525. printk(KERN_WARNING "Error folding to make room for Virtual Unit Chain 0x%x\n",
  526. thisVUC);
  527. return 0xffff;
  528. }
  529. static int nftl_writeblock(struct mtd_blktrans_dev *mbd, unsigned long block,
  530. char *buffer)
  531. {
  532. struct NFTLrecord *nftl = (void *)mbd;
  533. u16 writeEUN;
  534. unsigned long blockofs = (block * 512) & (nftl->EraseSize - 1);
  535. size_t retlen;
  536. struct nftl_oob oob;
  537. writeEUN = NFTL_findwriteunit(nftl, block);
  538. if (writeEUN == BLOCK_NIL) {
  539. printk(KERN_WARNING
  540. "NFTL_writeblock(): Cannot find block to write to\n");
  541. /* If we _still_ haven't got a block to use, we're screwed */
  542. return 1;
  543. }
  544. memset(&oob, 0xff, sizeof(struct nftl_oob));
  545. oob.b.Status = oob.b.Status1 = SECTOR_USED;
  546. MTD_WRITEECC(nftl->mbd.mtd, (writeEUN * nftl->EraseSize) + blockofs,
  547. 512, &retlen, (char *)buffer, (char *)&oob, &nftl->oobinfo);
  548. /* need to write SECTOR_USED flags since they are not written in mtd_writeecc */
  549. return 0;
  550. }
  551. #endif /* CONFIG_NFTL_RW */
  552. static int nftl_readblock(struct mtd_blktrans_dev *mbd, unsigned long block,
  553. char *buffer)
  554. {
  555. struct NFTLrecord *nftl = (void *)mbd;
  556. u16 lastgoodEUN;
  557. u16 thisEUN = nftl->EUNtable[block / (nftl->EraseSize / 512)];
  558. unsigned long blockofs = (block * 512) & (nftl->EraseSize - 1);
  559. unsigned int status;
  560. int silly = MAX_LOOPS;
  561. size_t retlen;
  562. struct nftl_bci bci;
  563. lastgoodEUN = BLOCK_NIL;
  564. if (thisEUN != BLOCK_NIL) {
  565. while (thisEUN < nftl->nb_blocks) {
  566. if (MTD_READOOB(nftl->mbd.mtd, (thisEUN * nftl->EraseSize) + blockofs,
  567. 8, &retlen, (char *)&bci) < 0)
  568. status = SECTOR_IGNORE;
  569. else
  570. status = bci.Status | bci.Status1;
  571. switch (status) {
  572. case SECTOR_FREE:
  573. /* no modification of a sector should follow a free sector */
  574. goto the_end;
  575. case SECTOR_DELETED:
  576. lastgoodEUN = BLOCK_NIL;
  577. break;
  578. case SECTOR_USED:
  579. lastgoodEUN = thisEUN;
  580. break;
  581. case SECTOR_IGNORE:
  582. break;
  583. default:
  584. printk("Unknown status for block %ld in EUN %d: %x\n",
  585. block, thisEUN, status);
  586. break;
  587. }
  588. if (!silly--) {
  589. printk(KERN_WARNING "Infinite loop in Virtual Unit Chain 0x%lx\n",
  590. block / (nftl->EraseSize / 512));
  591. return 1;
  592. }
  593. thisEUN = nftl->ReplUnitTable[thisEUN];
  594. }
  595. }
  596. the_end:
  597. if (lastgoodEUN == BLOCK_NIL) {
  598. /* the requested block is not on the media, return all 0x00 */
  599. memset(buffer, 0, 512);
  600. } else {
  601. loff_t ptr = (lastgoodEUN * nftl->EraseSize) + blockofs;
  602. size_t retlen;
  603. if (MTD_READ(nftl->mbd.mtd, ptr, 512, &retlen, buffer))
  604. return -EIO;
  605. }
  606. return 0;
  607. }
  608. static int nftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
  609. {
  610. struct NFTLrecord *nftl = (void *)dev;
  611. geo->heads = nftl->heads;
  612. geo->sectors = nftl->sectors;
  613. geo->cylinders = nftl->cylinders;
  614. return 0;
  615. }
  616. /****************************************************************************
  617. *
  618. * Module stuff
  619. *
  620. ****************************************************************************/
  621. static struct mtd_blktrans_ops nftl_tr = {
  622. .name = "nftl",
  623. .major = NFTL_MAJOR,
  624. .part_bits = NFTL_PARTN_BITS,
  625. .getgeo = nftl_getgeo,
  626. .readsect = nftl_readblock,
  627. #ifdef CONFIG_NFTL_RW
  628. .writesect = nftl_writeblock,
  629. #endif
  630. .add_mtd = nftl_add_mtd,
  631. .remove_dev = nftl_remove_dev,
  632. .owner = THIS_MODULE,
  633. };
  634. extern char nftlmountrev[];
  635. static int __init init_nftl(void)
  636. {
  637. printk(KERN_INFO "NFTL driver: nftlcore.c $Revision: 1.98 $, nftlmount.c %s\n", nftlmountrev);
  638. return register_mtd_blktrans(&nftl_tr);
  639. }
  640. static void __exit cleanup_nftl(void)
  641. {
  642. deregister_mtd_blktrans(&nftl_tr);
  643. }
  644. module_init(init_nftl);
  645. module_exit(cleanup_nftl);
  646. MODULE_LICENSE("GPL");
  647. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>, Fabrice Bellard <fabrice.bellard@netgem.com> et al.");
  648. MODULE_DESCRIPTION("Support code for NAND Flash Translation Layer, used on M-Systems DiskOnChip 2000 and Millennium");