ftl.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. /* This version ported to the Linux-MTD system by dwmw2@infradead.org
  2. *
  3. * Fixes: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  4. * - fixes some leaks on failure in build_maps and ftl_notify_add, cleanups
  5. *
  6. * Based on:
  7. */
  8. /*======================================================================
  9. A Flash Translation Layer memory card driver
  10. This driver implements a disk-like block device driver with an
  11. apparent block size of 512 bytes for flash memory cards.
  12. ftl_cs.c 1.62 2000/02/01 00:59:04
  13. The contents of this file are subject to the Mozilla Public
  14. License Version 1.1 (the "License"); you may not use this file
  15. except in compliance with the License. You may obtain a copy of
  16. the License at http://www.mozilla.org/MPL/
  17. Software distributed under the License is distributed on an "AS
  18. IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  19. implied. See the License for the specific language governing
  20. rights and limitations under the License.
  21. The initial developer of the original code is David A. Hinds
  22. <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
  23. are Copyright © 1999 David A. Hinds. All Rights Reserved.
  24. Alternatively, the contents of this file may be used under the
  25. terms of the GNU General Public License version 2 (the "GPL"), in
  26. which case the provisions of the GPL are applicable instead of the
  27. above. If you wish to allow the use of your version of this file
  28. only under the terms of the GPL and not to allow others to use
  29. your version of this file under the MPL, indicate your decision
  30. by deleting the provisions above and replace them with the notice
  31. and other provisions required by the GPL. If you do not delete
  32. the provisions above, a recipient may use your version of this
  33. file under either the MPL or the GPL.
  34. LEGAL NOTE: The FTL format is patented by M-Systems. They have
  35. granted a license for its use with PCMCIA devices:
  36. "M-Systems grants a royalty-free, non-exclusive license under
  37. any presently existing M-Systems intellectual property rights
  38. necessary for the design and development of FTL-compatible
  39. drivers, file systems and utilities using the data formats with
  40. PCMCIA PC Cards as described in the PCMCIA Flash Translation
  41. Layer (FTL) Specification."
  42. Use of the FTL format for non-PCMCIA applications may be an
  43. infringement of these patents. For additional information,
  44. contact M-Systems directly. M-Systems since acquired by Sandisk.
  45. ======================================================================*/
  46. #include <linux/mtd/blktrans.h>
  47. #include <linux/module.h>
  48. #include <linux/mtd/mtd.h>
  49. /*#define PSYCHO_DEBUG */
  50. #include <linux/kernel.h>
  51. #include <linux/ptrace.h>
  52. #include <linux/slab.h>
  53. #include <linux/string.h>
  54. #include <linux/timer.h>
  55. #include <linux/major.h>
  56. #include <linux/fs.h>
  57. #include <linux/init.h>
  58. #include <linux/hdreg.h>
  59. #include <linux/vmalloc.h>
  60. #include <linux/blkpg.h>
  61. #include <asm/uaccess.h>
  62. #include <linux/mtd/ftl.h>
  63. /*====================================================================*/
  64. /* Parameters that can be set with 'insmod' */
  65. static int shuffle_freq = 50;
  66. module_param(shuffle_freq, int, 0);
  67. /*====================================================================*/
  68. /* Major device # for FTL device */
  69. #ifndef FTL_MAJOR
  70. #define FTL_MAJOR 44
  71. #endif
  72. /*====================================================================*/
  73. /* Maximum number of separate memory devices we'll allow */
  74. #define MAX_DEV 4
  75. /* Maximum number of regions per device */
  76. #define MAX_REGION 4
  77. /* Maximum number of partitions in an FTL region */
  78. #define PART_BITS 4
  79. /* Maximum number of outstanding erase requests per socket */
  80. #define MAX_ERASE 8
  81. /* Sector size -- shouldn't need to change */
  82. #define SECTOR_SIZE 512
  83. /* Each memory region corresponds to a minor device */
  84. typedef struct partition_t {
  85. struct mtd_blktrans_dev mbd;
  86. uint32_t state;
  87. uint32_t *VirtualBlockMap;
  88. uint32_t FreeTotal;
  89. struct eun_info_t {
  90. uint32_t Offset;
  91. uint32_t EraseCount;
  92. uint32_t Free;
  93. uint32_t Deleted;
  94. } *EUNInfo;
  95. struct xfer_info_t {
  96. uint32_t Offset;
  97. uint32_t EraseCount;
  98. uint16_t state;
  99. } *XferInfo;
  100. uint16_t bam_index;
  101. uint32_t *bam_cache;
  102. uint16_t DataUnits;
  103. uint32_t BlocksPerUnit;
  104. erase_unit_header_t header;
  105. } partition_t;
  106. /* Partition state flags */
  107. #define FTL_FORMATTED 0x01
  108. /* Transfer unit states */
  109. #define XFER_UNKNOWN 0x00
  110. #define XFER_ERASING 0x01
  111. #define XFER_ERASED 0x02
  112. #define XFER_PREPARED 0x03
  113. #define XFER_FAILED 0x04
  114. /*====================================================================*/
  115. static void ftl_erase_callback(struct erase_info *done);
  116. /*======================================================================
  117. Scan_header() checks to see if a memory region contains an FTL
  118. partition. build_maps() reads all the erase unit headers, builds
  119. the erase unit map, and then builds the virtual page map.
  120. ======================================================================*/
  121. static int scan_header(partition_t *part)
  122. {
  123. erase_unit_header_t header;
  124. loff_t offset, max_offset;
  125. size_t ret;
  126. int err;
  127. part->header.FormattedSize = 0;
  128. max_offset = (0x100000<part->mbd.mtd->size)?0x100000:part->mbd.mtd->size;
  129. /* Search first megabyte for a valid FTL header */
  130. for (offset = 0;
  131. (offset + sizeof(header)) < max_offset;
  132. offset += part->mbd.mtd->erasesize ? : 0x2000) {
  133. err = mtd_read(part->mbd.mtd, offset, sizeof(header), &ret,
  134. (unsigned char *)&header);
  135. if (err)
  136. return err;
  137. if (strcmp(header.DataOrgTuple+3, "FTL100") == 0) break;
  138. }
  139. if (offset == max_offset) {
  140. printk(KERN_NOTICE "ftl_cs: FTL header not found.\n");
  141. return -ENOENT;
  142. }
  143. if (header.BlockSize != 9 ||
  144. (header.EraseUnitSize < 10) || (header.EraseUnitSize > 31) ||
  145. (header.NumTransferUnits >= le16_to_cpu(header.NumEraseUnits))) {
  146. printk(KERN_NOTICE "ftl_cs: FTL header corrupt!\n");
  147. return -1;
  148. }
  149. if ((1 << header.EraseUnitSize) != part->mbd.mtd->erasesize) {
  150. printk(KERN_NOTICE "ftl: FTL EraseUnitSize %x != MTD erasesize %x\n",
  151. 1 << header.EraseUnitSize,part->mbd.mtd->erasesize);
  152. return -1;
  153. }
  154. part->header = header;
  155. return 0;
  156. }
  157. static int build_maps(partition_t *part)
  158. {
  159. erase_unit_header_t header;
  160. uint16_t xvalid, xtrans, i;
  161. unsigned blocks, j;
  162. int hdr_ok, ret = -1;
  163. ssize_t retval;
  164. loff_t offset;
  165. /* Set up erase unit maps */
  166. part->DataUnits = le16_to_cpu(part->header.NumEraseUnits) -
  167. part->header.NumTransferUnits;
  168. part->EUNInfo = kmalloc(part->DataUnits * sizeof(struct eun_info_t),
  169. GFP_KERNEL);
  170. if (!part->EUNInfo)
  171. goto out;
  172. for (i = 0; i < part->DataUnits; i++)
  173. part->EUNInfo[i].Offset = 0xffffffff;
  174. part->XferInfo =
  175. kmalloc(part->header.NumTransferUnits * sizeof(struct xfer_info_t),
  176. GFP_KERNEL);
  177. if (!part->XferInfo)
  178. goto out_EUNInfo;
  179. xvalid = xtrans = 0;
  180. for (i = 0; i < le16_to_cpu(part->header.NumEraseUnits); i++) {
  181. offset = ((i + le16_to_cpu(part->header.FirstPhysicalEUN))
  182. << part->header.EraseUnitSize);
  183. ret = mtd_read(part->mbd.mtd, offset, sizeof(header), &retval,
  184. (unsigned char *)&header);
  185. if (ret)
  186. goto out_XferInfo;
  187. ret = -1;
  188. /* Is this a transfer partition? */
  189. hdr_ok = (strcmp(header.DataOrgTuple+3, "FTL100") == 0);
  190. if (hdr_ok && (le16_to_cpu(header.LogicalEUN) < part->DataUnits) &&
  191. (part->EUNInfo[le16_to_cpu(header.LogicalEUN)].Offset == 0xffffffff)) {
  192. part->EUNInfo[le16_to_cpu(header.LogicalEUN)].Offset = offset;
  193. part->EUNInfo[le16_to_cpu(header.LogicalEUN)].EraseCount =
  194. le32_to_cpu(header.EraseCount);
  195. xvalid++;
  196. } else {
  197. if (xtrans == part->header.NumTransferUnits) {
  198. printk(KERN_NOTICE "ftl_cs: format error: too many "
  199. "transfer units!\n");
  200. goto out_XferInfo;
  201. }
  202. if (hdr_ok && (le16_to_cpu(header.LogicalEUN) == 0xffff)) {
  203. part->XferInfo[xtrans].state = XFER_PREPARED;
  204. part->XferInfo[xtrans].EraseCount = le32_to_cpu(header.EraseCount);
  205. } else {
  206. part->XferInfo[xtrans].state = XFER_UNKNOWN;
  207. /* Pick anything reasonable for the erase count */
  208. part->XferInfo[xtrans].EraseCount =
  209. le32_to_cpu(part->header.EraseCount);
  210. }
  211. part->XferInfo[xtrans].Offset = offset;
  212. xtrans++;
  213. }
  214. }
  215. /* Check for format trouble */
  216. header = part->header;
  217. if ((xtrans != header.NumTransferUnits) ||
  218. (xvalid+xtrans != le16_to_cpu(header.NumEraseUnits))) {
  219. printk(KERN_NOTICE "ftl_cs: format error: erase units "
  220. "don't add up!\n");
  221. goto out_XferInfo;
  222. }
  223. /* Set up virtual page map */
  224. blocks = le32_to_cpu(header.FormattedSize) >> header.BlockSize;
  225. part->VirtualBlockMap = vmalloc(blocks * sizeof(uint32_t));
  226. if (!part->VirtualBlockMap)
  227. goto out_XferInfo;
  228. memset(part->VirtualBlockMap, 0xff, blocks * sizeof(uint32_t));
  229. part->BlocksPerUnit = (1 << header.EraseUnitSize) >> header.BlockSize;
  230. part->bam_cache = kmalloc(part->BlocksPerUnit * sizeof(uint32_t),
  231. GFP_KERNEL);
  232. if (!part->bam_cache)
  233. goto out_VirtualBlockMap;
  234. part->bam_index = 0xffff;
  235. part->FreeTotal = 0;
  236. for (i = 0; i < part->DataUnits; i++) {
  237. part->EUNInfo[i].Free = 0;
  238. part->EUNInfo[i].Deleted = 0;
  239. offset = part->EUNInfo[i].Offset + le32_to_cpu(header.BAMOffset);
  240. ret = mtd_read(part->mbd.mtd, offset,
  241. part->BlocksPerUnit * sizeof(uint32_t), &retval,
  242. (unsigned char *)part->bam_cache);
  243. if (ret)
  244. goto out_bam_cache;
  245. for (j = 0; j < part->BlocksPerUnit; j++) {
  246. if (BLOCK_FREE(le32_to_cpu(part->bam_cache[j]))) {
  247. part->EUNInfo[i].Free++;
  248. part->FreeTotal++;
  249. } else if ((BLOCK_TYPE(le32_to_cpu(part->bam_cache[j])) == BLOCK_DATA) &&
  250. (BLOCK_NUMBER(le32_to_cpu(part->bam_cache[j])) < blocks))
  251. part->VirtualBlockMap[BLOCK_NUMBER(le32_to_cpu(part->bam_cache[j]))] =
  252. (i << header.EraseUnitSize) + (j << header.BlockSize);
  253. else if (BLOCK_DELETED(le32_to_cpu(part->bam_cache[j])))
  254. part->EUNInfo[i].Deleted++;
  255. }
  256. }
  257. ret = 0;
  258. goto out;
  259. out_bam_cache:
  260. kfree(part->bam_cache);
  261. out_VirtualBlockMap:
  262. vfree(part->VirtualBlockMap);
  263. out_XferInfo:
  264. kfree(part->XferInfo);
  265. out_EUNInfo:
  266. kfree(part->EUNInfo);
  267. out:
  268. return ret;
  269. } /* build_maps */
  270. /*======================================================================
  271. Erase_xfer() schedules an asynchronous erase operation for a
  272. transfer unit.
  273. ======================================================================*/
  274. static int erase_xfer(partition_t *part,
  275. uint16_t xfernum)
  276. {
  277. int ret;
  278. struct xfer_info_t *xfer;
  279. struct erase_info *erase;
  280. xfer = &part->XferInfo[xfernum];
  281. pr_debug("ftl_cs: erasing xfer unit at 0x%x\n", xfer->Offset);
  282. xfer->state = XFER_ERASING;
  283. /* Is there a free erase slot? Always in MTD. */
  284. erase=kmalloc(sizeof(struct erase_info), GFP_KERNEL);
  285. if (!erase)
  286. return -ENOMEM;
  287. erase->mtd = part->mbd.mtd;
  288. erase->callback = ftl_erase_callback;
  289. erase->addr = xfer->Offset;
  290. erase->len = 1 << part->header.EraseUnitSize;
  291. erase->priv = (u_long)part;
  292. ret = mtd_erase(part->mbd.mtd, erase);
  293. if (!ret)
  294. xfer->EraseCount++;
  295. else
  296. kfree(erase);
  297. return ret;
  298. } /* erase_xfer */
  299. /*======================================================================
  300. Prepare_xfer() takes a freshly erased transfer unit and gives
  301. it an appropriate header.
  302. ======================================================================*/
  303. static void ftl_erase_callback(struct erase_info *erase)
  304. {
  305. partition_t *part;
  306. struct xfer_info_t *xfer;
  307. int i;
  308. /* Look up the transfer unit */
  309. part = (partition_t *)(erase->priv);
  310. for (i = 0; i < part->header.NumTransferUnits; i++)
  311. if (part->XferInfo[i].Offset == erase->addr) break;
  312. if (i == part->header.NumTransferUnits) {
  313. printk(KERN_NOTICE "ftl_cs: internal error: "
  314. "erase lookup failed!\n");
  315. return;
  316. }
  317. xfer = &part->XferInfo[i];
  318. if (erase->state == MTD_ERASE_DONE)
  319. xfer->state = XFER_ERASED;
  320. else {
  321. xfer->state = XFER_FAILED;
  322. printk(KERN_NOTICE "ftl_cs: erase failed: state = %d\n",
  323. erase->state);
  324. }
  325. kfree(erase);
  326. } /* ftl_erase_callback */
  327. static int prepare_xfer(partition_t *part, int i)
  328. {
  329. erase_unit_header_t header;
  330. struct xfer_info_t *xfer;
  331. int nbam, ret;
  332. uint32_t ctl;
  333. ssize_t retlen;
  334. loff_t offset;
  335. xfer = &part->XferInfo[i];
  336. xfer->state = XFER_FAILED;
  337. pr_debug("ftl_cs: preparing xfer unit at 0x%x\n", xfer->Offset);
  338. /* Write the transfer unit header */
  339. header = part->header;
  340. header.LogicalEUN = cpu_to_le16(0xffff);
  341. header.EraseCount = cpu_to_le32(xfer->EraseCount);
  342. ret = mtd_write(part->mbd.mtd, xfer->Offset, sizeof(header), &retlen,
  343. (u_char *)&header);
  344. if (ret) {
  345. return ret;
  346. }
  347. /* Write the BAM stub */
  348. nbam = (part->BlocksPerUnit * sizeof(uint32_t) +
  349. le32_to_cpu(part->header.BAMOffset) + SECTOR_SIZE - 1) / SECTOR_SIZE;
  350. offset = xfer->Offset + le32_to_cpu(part->header.BAMOffset);
  351. ctl = cpu_to_le32(BLOCK_CONTROL);
  352. for (i = 0; i < nbam; i++, offset += sizeof(uint32_t)) {
  353. ret = mtd_write(part->mbd.mtd, offset, sizeof(uint32_t), &retlen,
  354. (u_char *)&ctl);
  355. if (ret)
  356. return ret;
  357. }
  358. xfer->state = XFER_PREPARED;
  359. return 0;
  360. } /* prepare_xfer */
  361. /*======================================================================
  362. Copy_erase_unit() takes a full erase block and a transfer unit,
  363. copies everything to the transfer unit, then swaps the block
  364. pointers.
  365. All data blocks are copied to the corresponding blocks in the
  366. target unit, so the virtual block map does not need to be
  367. updated.
  368. ======================================================================*/
  369. static int copy_erase_unit(partition_t *part, uint16_t srcunit,
  370. uint16_t xferunit)
  371. {
  372. u_char buf[SECTOR_SIZE];
  373. struct eun_info_t *eun;
  374. struct xfer_info_t *xfer;
  375. uint32_t src, dest, free, i;
  376. uint16_t unit;
  377. int ret;
  378. ssize_t retlen;
  379. loff_t offset;
  380. uint16_t srcunitswap = cpu_to_le16(srcunit);
  381. eun = &part->EUNInfo[srcunit];
  382. xfer = &part->XferInfo[xferunit];
  383. pr_debug("ftl_cs: copying block 0x%x to 0x%x\n",
  384. eun->Offset, xfer->Offset);
  385. /* Read current BAM */
  386. if (part->bam_index != srcunit) {
  387. offset = eun->Offset + le32_to_cpu(part->header.BAMOffset);
  388. ret = mtd_read(part->mbd.mtd, offset,
  389. part->BlocksPerUnit * sizeof(uint32_t), &retlen,
  390. (u_char *)(part->bam_cache));
  391. /* mark the cache bad, in case we get an error later */
  392. part->bam_index = 0xffff;
  393. if (ret) {
  394. printk( KERN_WARNING "ftl: Failed to read BAM cache in copy_erase_unit()!\n");
  395. return ret;
  396. }
  397. }
  398. /* Write the LogicalEUN for the transfer unit */
  399. xfer->state = XFER_UNKNOWN;
  400. offset = xfer->Offset + 20; /* Bad! */
  401. unit = cpu_to_le16(0x7fff);
  402. ret = mtd_write(part->mbd.mtd, offset, sizeof(uint16_t), &retlen,
  403. (u_char *)&unit);
  404. if (ret) {
  405. printk( KERN_WARNING "ftl: Failed to write back to BAM cache in copy_erase_unit()!\n");
  406. return ret;
  407. }
  408. /* Copy all data blocks from source unit to transfer unit */
  409. src = eun->Offset; dest = xfer->Offset;
  410. free = 0;
  411. ret = 0;
  412. for (i = 0; i < part->BlocksPerUnit; i++) {
  413. switch (BLOCK_TYPE(le32_to_cpu(part->bam_cache[i]))) {
  414. case BLOCK_CONTROL:
  415. /* This gets updated later */
  416. break;
  417. case BLOCK_DATA:
  418. case BLOCK_REPLACEMENT:
  419. ret = mtd_read(part->mbd.mtd, src, SECTOR_SIZE, &retlen,
  420. (u_char *)buf);
  421. if (ret) {
  422. printk(KERN_WARNING "ftl: Error reading old xfer unit in copy_erase_unit\n");
  423. return ret;
  424. }
  425. ret = mtd_write(part->mbd.mtd, dest, SECTOR_SIZE, &retlen,
  426. (u_char *)buf);
  427. if (ret) {
  428. printk(KERN_WARNING "ftl: Error writing new xfer unit in copy_erase_unit\n");
  429. return ret;
  430. }
  431. break;
  432. default:
  433. /* All other blocks must be free */
  434. part->bam_cache[i] = cpu_to_le32(0xffffffff);
  435. free++;
  436. break;
  437. }
  438. src += SECTOR_SIZE;
  439. dest += SECTOR_SIZE;
  440. }
  441. /* Write the BAM to the transfer unit */
  442. ret = mtd_write(part->mbd.mtd,
  443. xfer->Offset + le32_to_cpu(part->header.BAMOffset),
  444. part->BlocksPerUnit * sizeof(int32_t),
  445. &retlen,
  446. (u_char *)part->bam_cache);
  447. if (ret) {
  448. printk( KERN_WARNING "ftl: Error writing BAM in copy_erase_unit\n");
  449. return ret;
  450. }
  451. /* All clear? Then update the LogicalEUN again */
  452. ret = mtd_write(part->mbd.mtd, xfer->Offset + 20, sizeof(uint16_t),
  453. &retlen, (u_char *)&srcunitswap);
  454. if (ret) {
  455. printk(KERN_WARNING "ftl: Error writing new LogicalEUN in copy_erase_unit\n");
  456. return ret;
  457. }
  458. /* Update the maps and usage stats*/
  459. i = xfer->EraseCount;
  460. xfer->EraseCount = eun->EraseCount;
  461. eun->EraseCount = i;
  462. i = xfer->Offset;
  463. xfer->Offset = eun->Offset;
  464. eun->Offset = i;
  465. part->FreeTotal -= eun->Free;
  466. part->FreeTotal += free;
  467. eun->Free = free;
  468. eun->Deleted = 0;
  469. /* Now, the cache should be valid for the new block */
  470. part->bam_index = srcunit;
  471. return 0;
  472. } /* copy_erase_unit */
  473. /*======================================================================
  474. reclaim_block() picks a full erase unit and a transfer unit and
  475. then calls copy_erase_unit() to copy one to the other. Then, it
  476. schedules an erase on the expired block.
  477. What's a good way to decide which transfer unit and which erase
  478. unit to use? Beats me. My way is to always pick the transfer
  479. unit with the fewest erases, and usually pick the data unit with
  480. the most deleted blocks. But with a small probability, pick the
  481. oldest data unit instead. This means that we generally postpone
  482. the next reclamation as long as possible, but shuffle static
  483. stuff around a bit for wear leveling.
  484. ======================================================================*/
  485. static int reclaim_block(partition_t *part)
  486. {
  487. uint16_t i, eun, xfer;
  488. uint32_t best;
  489. int queued, ret;
  490. pr_debug("ftl_cs: reclaiming space...\n");
  491. pr_debug("NumTransferUnits == %x\n", part->header.NumTransferUnits);
  492. /* Pick the least erased transfer unit */
  493. best = 0xffffffff; xfer = 0xffff;
  494. do {
  495. queued = 0;
  496. for (i = 0; i < part->header.NumTransferUnits; i++) {
  497. int n=0;
  498. if (part->XferInfo[i].state == XFER_UNKNOWN) {
  499. pr_debug("XferInfo[%d].state == XFER_UNKNOWN\n",i);
  500. n=1;
  501. erase_xfer(part, i);
  502. }
  503. if (part->XferInfo[i].state == XFER_ERASING) {
  504. pr_debug("XferInfo[%d].state == XFER_ERASING\n",i);
  505. n=1;
  506. queued = 1;
  507. }
  508. else if (part->XferInfo[i].state == XFER_ERASED) {
  509. pr_debug("XferInfo[%d].state == XFER_ERASED\n",i);
  510. n=1;
  511. prepare_xfer(part, i);
  512. }
  513. if (part->XferInfo[i].state == XFER_PREPARED) {
  514. pr_debug("XferInfo[%d].state == XFER_PREPARED\n",i);
  515. n=1;
  516. if (part->XferInfo[i].EraseCount <= best) {
  517. best = part->XferInfo[i].EraseCount;
  518. xfer = i;
  519. }
  520. }
  521. if (!n)
  522. pr_debug("XferInfo[%d].state == %x\n",i, part->XferInfo[i].state);
  523. }
  524. if (xfer == 0xffff) {
  525. if (queued) {
  526. pr_debug("ftl_cs: waiting for transfer "
  527. "unit to be prepared...\n");
  528. mtd_sync(part->mbd.mtd);
  529. } else {
  530. static int ne = 0;
  531. if (++ne < 5)
  532. printk(KERN_NOTICE "ftl_cs: reclaim failed: no "
  533. "suitable transfer units!\n");
  534. else
  535. pr_debug("ftl_cs: reclaim failed: no "
  536. "suitable transfer units!\n");
  537. return -EIO;
  538. }
  539. }
  540. } while (xfer == 0xffff);
  541. eun = 0;
  542. if ((jiffies % shuffle_freq) == 0) {
  543. pr_debug("ftl_cs: recycling freshest block...\n");
  544. best = 0xffffffff;
  545. for (i = 0; i < part->DataUnits; i++)
  546. if (part->EUNInfo[i].EraseCount <= best) {
  547. best = part->EUNInfo[i].EraseCount;
  548. eun = i;
  549. }
  550. } else {
  551. best = 0;
  552. for (i = 0; i < part->DataUnits; i++)
  553. if (part->EUNInfo[i].Deleted >= best) {
  554. best = part->EUNInfo[i].Deleted;
  555. eun = i;
  556. }
  557. if (best == 0) {
  558. static int ne = 0;
  559. if (++ne < 5)
  560. printk(KERN_NOTICE "ftl_cs: reclaim failed: "
  561. "no free blocks!\n");
  562. else
  563. pr_debug("ftl_cs: reclaim failed: "
  564. "no free blocks!\n");
  565. return -EIO;
  566. }
  567. }
  568. ret = copy_erase_unit(part, eun, xfer);
  569. if (!ret)
  570. erase_xfer(part, xfer);
  571. else
  572. printk(KERN_NOTICE "ftl_cs: copy_erase_unit failed!\n");
  573. return ret;
  574. } /* reclaim_block */
  575. /*======================================================================
  576. Find_free() searches for a free block. If necessary, it updates
  577. the BAM cache for the erase unit containing the free block. It
  578. returns the block index -- the erase unit is just the currently
  579. cached unit. If there are no free blocks, it returns 0 -- this
  580. is never a valid data block because it contains the header.
  581. ======================================================================*/
  582. #ifdef PSYCHO_DEBUG
  583. static void dump_lists(partition_t *part)
  584. {
  585. int i;
  586. printk(KERN_DEBUG "ftl_cs: Free total = %d\n", part->FreeTotal);
  587. for (i = 0; i < part->DataUnits; i++)
  588. printk(KERN_DEBUG "ftl_cs: unit %d: %d phys, %d free, "
  589. "%d deleted\n", i,
  590. part->EUNInfo[i].Offset >> part->header.EraseUnitSize,
  591. part->EUNInfo[i].Free, part->EUNInfo[i].Deleted);
  592. }
  593. #endif
  594. static uint32_t find_free(partition_t *part)
  595. {
  596. uint16_t stop, eun;
  597. uint32_t blk;
  598. size_t retlen;
  599. int ret;
  600. /* Find an erase unit with some free space */
  601. stop = (part->bam_index == 0xffff) ? 0 : part->bam_index;
  602. eun = stop;
  603. do {
  604. if (part->EUNInfo[eun].Free != 0) break;
  605. /* Wrap around at end of table */
  606. if (++eun == part->DataUnits) eun = 0;
  607. } while (eun != stop);
  608. if (part->EUNInfo[eun].Free == 0)
  609. return 0;
  610. /* Is this unit's BAM cached? */
  611. if (eun != part->bam_index) {
  612. /* Invalidate cache */
  613. part->bam_index = 0xffff;
  614. ret = mtd_read(part->mbd.mtd,
  615. part->EUNInfo[eun].Offset + le32_to_cpu(part->header.BAMOffset),
  616. part->BlocksPerUnit * sizeof(uint32_t),
  617. &retlen,
  618. (u_char *)(part->bam_cache));
  619. if (ret) {
  620. printk(KERN_WARNING"ftl: Error reading BAM in find_free\n");
  621. return 0;
  622. }
  623. part->bam_index = eun;
  624. }
  625. /* Find a free block */
  626. for (blk = 0; blk < part->BlocksPerUnit; blk++)
  627. if (BLOCK_FREE(le32_to_cpu(part->bam_cache[blk]))) break;
  628. if (blk == part->BlocksPerUnit) {
  629. #ifdef PSYCHO_DEBUG
  630. static int ne = 0;
  631. if (++ne == 1)
  632. dump_lists(part);
  633. #endif
  634. printk(KERN_NOTICE "ftl_cs: bad free list!\n");
  635. return 0;
  636. }
  637. pr_debug("ftl_cs: found free block at %d in %d\n", blk, eun);
  638. return blk;
  639. } /* find_free */
  640. /*======================================================================
  641. Read a series of sectors from an FTL partition.
  642. ======================================================================*/
  643. static int ftl_read(partition_t *part, caddr_t buffer,
  644. u_long sector, u_long nblocks)
  645. {
  646. uint32_t log_addr, bsize;
  647. u_long i;
  648. int ret;
  649. size_t offset, retlen;
  650. pr_debug("ftl_cs: ftl_read(0x%p, 0x%lx, %ld)\n",
  651. part, sector, nblocks);
  652. if (!(part->state & FTL_FORMATTED)) {
  653. printk(KERN_NOTICE "ftl_cs: bad partition\n");
  654. return -EIO;
  655. }
  656. bsize = 1 << part->header.EraseUnitSize;
  657. for (i = 0; i < nblocks; i++) {
  658. if (((sector+i) * SECTOR_SIZE) >= le32_to_cpu(part->header.FormattedSize)) {
  659. printk(KERN_NOTICE "ftl_cs: bad read offset\n");
  660. return -EIO;
  661. }
  662. log_addr = part->VirtualBlockMap[sector+i];
  663. if (log_addr == 0xffffffff)
  664. memset(buffer, 0, SECTOR_SIZE);
  665. else {
  666. offset = (part->EUNInfo[log_addr / bsize].Offset
  667. + (log_addr % bsize));
  668. ret = mtd_read(part->mbd.mtd, offset, SECTOR_SIZE, &retlen,
  669. (u_char *)buffer);
  670. if (ret) {
  671. printk(KERN_WARNING "Error reading MTD device in ftl_read()\n");
  672. return ret;
  673. }
  674. }
  675. buffer += SECTOR_SIZE;
  676. }
  677. return 0;
  678. } /* ftl_read */
  679. /*======================================================================
  680. Write a series of sectors to an FTL partition
  681. ======================================================================*/
  682. static int set_bam_entry(partition_t *part, uint32_t log_addr,
  683. uint32_t virt_addr)
  684. {
  685. uint32_t bsize, blk, le_virt_addr;
  686. #ifdef PSYCHO_DEBUG
  687. uint32_t old_addr;
  688. #endif
  689. uint16_t eun;
  690. int ret;
  691. size_t retlen, offset;
  692. pr_debug("ftl_cs: set_bam_entry(0x%p, 0x%x, 0x%x)\n",
  693. part, log_addr, virt_addr);
  694. bsize = 1 << part->header.EraseUnitSize;
  695. eun = log_addr / bsize;
  696. blk = (log_addr % bsize) / SECTOR_SIZE;
  697. offset = (part->EUNInfo[eun].Offset + blk * sizeof(uint32_t) +
  698. le32_to_cpu(part->header.BAMOffset));
  699. #ifdef PSYCHO_DEBUG
  700. ret = mtd_read(part->mbd.mtd, offset, sizeof(uint32_t), &retlen,
  701. (u_char *)&old_addr);
  702. if (ret) {
  703. printk(KERN_WARNING"ftl: Error reading old_addr in set_bam_entry: %d\n",ret);
  704. return ret;
  705. }
  706. old_addr = le32_to_cpu(old_addr);
  707. if (((virt_addr == 0xfffffffe) && !BLOCK_FREE(old_addr)) ||
  708. ((virt_addr == 0) && (BLOCK_TYPE(old_addr) != BLOCK_DATA)) ||
  709. (!BLOCK_DELETED(virt_addr) && (old_addr != 0xfffffffe))) {
  710. static int ne = 0;
  711. if (++ne < 5) {
  712. printk(KERN_NOTICE "ftl_cs: set_bam_entry() inconsistency!\n");
  713. printk(KERN_NOTICE "ftl_cs: log_addr = 0x%x, old = 0x%x"
  714. ", new = 0x%x\n", log_addr, old_addr, virt_addr);
  715. }
  716. return -EIO;
  717. }
  718. #endif
  719. le_virt_addr = cpu_to_le32(virt_addr);
  720. if (part->bam_index == eun) {
  721. #ifdef PSYCHO_DEBUG
  722. if (le32_to_cpu(part->bam_cache[blk]) != old_addr) {
  723. static int ne = 0;
  724. if (++ne < 5) {
  725. printk(KERN_NOTICE "ftl_cs: set_bam_entry() "
  726. "inconsistency!\n");
  727. printk(KERN_NOTICE "ftl_cs: log_addr = 0x%x, cache"
  728. " = 0x%x\n",
  729. le32_to_cpu(part->bam_cache[blk]), old_addr);
  730. }
  731. return -EIO;
  732. }
  733. #endif
  734. part->bam_cache[blk] = le_virt_addr;
  735. }
  736. ret = mtd_write(part->mbd.mtd, offset, sizeof(uint32_t), &retlen,
  737. (u_char *)&le_virt_addr);
  738. if (ret) {
  739. printk(KERN_NOTICE "ftl_cs: set_bam_entry() failed!\n");
  740. printk(KERN_NOTICE "ftl_cs: log_addr = 0x%x, new = 0x%x\n",
  741. log_addr, virt_addr);
  742. }
  743. return ret;
  744. } /* set_bam_entry */
  745. static int ftl_write(partition_t *part, caddr_t buffer,
  746. u_long sector, u_long nblocks)
  747. {
  748. uint32_t bsize, log_addr, virt_addr, old_addr, blk;
  749. u_long i;
  750. int ret;
  751. size_t retlen, offset;
  752. pr_debug("ftl_cs: ftl_write(0x%p, %ld, %ld)\n",
  753. part, sector, nblocks);
  754. if (!(part->state & FTL_FORMATTED)) {
  755. printk(KERN_NOTICE "ftl_cs: bad partition\n");
  756. return -EIO;
  757. }
  758. /* See if we need to reclaim space, before we start */
  759. while (part->FreeTotal < nblocks) {
  760. ret = reclaim_block(part);
  761. if (ret)
  762. return ret;
  763. }
  764. bsize = 1 << part->header.EraseUnitSize;
  765. virt_addr = sector * SECTOR_SIZE | BLOCK_DATA;
  766. for (i = 0; i < nblocks; i++) {
  767. if (virt_addr >= le32_to_cpu(part->header.FormattedSize)) {
  768. printk(KERN_NOTICE "ftl_cs: bad write offset\n");
  769. return -EIO;
  770. }
  771. /* Grab a free block */
  772. blk = find_free(part);
  773. if (blk == 0) {
  774. static int ne = 0;
  775. if (++ne < 5)
  776. printk(KERN_NOTICE "ftl_cs: internal error: "
  777. "no free blocks!\n");
  778. return -ENOSPC;
  779. }
  780. /* Tag the BAM entry, and write the new block */
  781. log_addr = part->bam_index * bsize + blk * SECTOR_SIZE;
  782. part->EUNInfo[part->bam_index].Free--;
  783. part->FreeTotal--;
  784. if (set_bam_entry(part, log_addr, 0xfffffffe))
  785. return -EIO;
  786. part->EUNInfo[part->bam_index].Deleted++;
  787. offset = (part->EUNInfo[part->bam_index].Offset +
  788. blk * SECTOR_SIZE);
  789. ret = mtd_write(part->mbd.mtd, offset, SECTOR_SIZE, &retlen, buffer);
  790. if (ret) {
  791. printk(KERN_NOTICE "ftl_cs: block write failed!\n");
  792. printk(KERN_NOTICE "ftl_cs: log_addr = 0x%x, virt_addr"
  793. " = 0x%x, Offset = 0x%zx\n", log_addr, virt_addr,
  794. offset);
  795. return -EIO;
  796. }
  797. /* Only delete the old entry when the new entry is ready */
  798. old_addr = part->VirtualBlockMap[sector+i];
  799. if (old_addr != 0xffffffff) {
  800. part->VirtualBlockMap[sector+i] = 0xffffffff;
  801. part->EUNInfo[old_addr/bsize].Deleted++;
  802. if (set_bam_entry(part, old_addr, 0))
  803. return -EIO;
  804. }
  805. /* Finally, set up the new pointers */
  806. if (set_bam_entry(part, log_addr, virt_addr))
  807. return -EIO;
  808. part->VirtualBlockMap[sector+i] = log_addr;
  809. part->EUNInfo[part->bam_index].Deleted--;
  810. buffer += SECTOR_SIZE;
  811. virt_addr += SECTOR_SIZE;
  812. }
  813. return 0;
  814. } /* ftl_write */
  815. static int ftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
  816. {
  817. partition_t *part = (void *)dev;
  818. u_long sect;
  819. /* Sort of arbitrary: round size down to 4KiB boundary */
  820. sect = le32_to_cpu(part->header.FormattedSize)/SECTOR_SIZE;
  821. geo->heads = 1;
  822. geo->sectors = 8;
  823. geo->cylinders = sect >> 3;
  824. return 0;
  825. }
  826. static int ftl_readsect(struct mtd_blktrans_dev *dev,
  827. unsigned long block, char *buf)
  828. {
  829. return ftl_read((void *)dev, buf, block, 1);
  830. }
  831. static int ftl_writesect(struct mtd_blktrans_dev *dev,
  832. unsigned long block, char *buf)
  833. {
  834. return ftl_write((void *)dev, buf, block, 1);
  835. }
  836. static int ftl_discardsect(struct mtd_blktrans_dev *dev,
  837. unsigned long sector, unsigned nr_sects)
  838. {
  839. partition_t *part = (void *)dev;
  840. uint32_t bsize = 1 << part->header.EraseUnitSize;
  841. pr_debug("FTL erase sector %ld for %d sectors\n",
  842. sector, nr_sects);
  843. while (nr_sects) {
  844. uint32_t old_addr = part->VirtualBlockMap[sector];
  845. if (old_addr != 0xffffffff) {
  846. part->VirtualBlockMap[sector] = 0xffffffff;
  847. part->EUNInfo[old_addr/bsize].Deleted++;
  848. if (set_bam_entry(part, old_addr, 0))
  849. return -EIO;
  850. }
  851. nr_sects--;
  852. sector++;
  853. }
  854. return 0;
  855. }
  856. /*====================================================================*/
  857. static void ftl_freepart(partition_t *part)
  858. {
  859. vfree(part->VirtualBlockMap);
  860. part->VirtualBlockMap = NULL;
  861. kfree(part->EUNInfo);
  862. part->EUNInfo = NULL;
  863. kfree(part->XferInfo);
  864. part->XferInfo = NULL;
  865. kfree(part->bam_cache);
  866. part->bam_cache = NULL;
  867. } /* ftl_freepart */
  868. static void ftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
  869. {
  870. partition_t *partition;
  871. partition = kzalloc(sizeof(partition_t), GFP_KERNEL);
  872. if (!partition) {
  873. printk(KERN_WARNING "No memory to scan for FTL on %s\n",
  874. mtd->name);
  875. return;
  876. }
  877. partition->mbd.mtd = mtd;
  878. if ((scan_header(partition) == 0) &&
  879. (build_maps(partition) == 0)) {
  880. partition->state = FTL_FORMATTED;
  881. #ifdef PCMCIA_DEBUG
  882. printk(KERN_INFO "ftl_cs: opening %d KiB FTL partition\n",
  883. le32_to_cpu(partition->header.FormattedSize) >> 10);
  884. #endif
  885. partition->mbd.size = le32_to_cpu(partition->header.FormattedSize) >> 9;
  886. partition->mbd.tr = tr;
  887. partition->mbd.devnum = -1;
  888. if (!add_mtd_blktrans_dev((void *)partition))
  889. return;
  890. }
  891. kfree(partition);
  892. }
  893. static void ftl_remove_dev(struct mtd_blktrans_dev *dev)
  894. {
  895. del_mtd_blktrans_dev(dev);
  896. ftl_freepart((partition_t *)dev);
  897. }
  898. static struct mtd_blktrans_ops ftl_tr = {
  899. .name = "ftl",
  900. .major = FTL_MAJOR,
  901. .part_bits = PART_BITS,
  902. .blksize = SECTOR_SIZE,
  903. .readsect = ftl_readsect,
  904. .writesect = ftl_writesect,
  905. .discard = ftl_discardsect,
  906. .getgeo = ftl_getgeo,
  907. .add_mtd = ftl_add_mtd,
  908. .remove_dev = ftl_remove_dev,
  909. .owner = THIS_MODULE,
  910. };
  911. static int __init init_ftl(void)
  912. {
  913. return register_mtd_blktrans(&ftl_tr);
  914. }
  915. static void __exit cleanup_ftl(void)
  916. {
  917. deregister_mtd_blktrans(&ftl_tr);
  918. }
  919. module_init(init_ftl);
  920. module_exit(cleanup_ftl);
  921. MODULE_LICENSE("Dual MPL/GPL");
  922. MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
  923. MODULE_DESCRIPTION("Support code for Flash Translation Layer, used on PCMCIA devices");