nftlcore.c 23 KB

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