nftlcore.c 23 KB

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