nftlcore.c 22 KB

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