ftl.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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 <linux/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. Scan_header() checks to see if a memory region contains an FTL
  116. partition. build_maps() reads all the erase unit headers, builds
  117. the erase unit map, and then builds the virtual page map.
  118. ======================================================================*/
  119. static int scan_header(partition_t *part)
  120. {
  121. erase_unit_header_t header;
  122. loff_t offset, max_offset;
  123. size_t ret;
  124. int err;
  125. part->header.FormattedSize = 0;
  126. max_offset = (0x100000<part->mbd.mtd->size)?0x100000:part->mbd.mtd->size;
  127. /* Search first megabyte for a valid FTL header */
  128. for (offset = 0;
  129. (offset + sizeof(header)) < max_offset;
  130. offset += part->mbd.mtd->erasesize ? : 0x2000) {
  131. err = mtd_read(part->mbd.mtd, offset, sizeof(header), &ret,
  132. (unsigned char *)&header);
  133. if (err)
  134. return err;
  135. if (strcmp(header.DataOrgTuple+3, "FTL100") == 0) break;
  136. }
  137. if (offset == max_offset) {
  138. printk(KERN_NOTICE "ftl_cs: FTL header not found.\n");
  139. return -ENOENT;
  140. }
  141. if (header.BlockSize != 9 ||
  142. (header.EraseUnitSize < 10) || (header.EraseUnitSize > 31) ||
  143. (header.NumTransferUnits >= le16_to_cpu(header.NumEraseUnits))) {
  144. printk(KERN_NOTICE "ftl_cs: FTL header corrupt!\n");
  145. return -1;
  146. }
  147. if ((1 << header.EraseUnitSize) != part->mbd.mtd->erasesize) {
  148. printk(KERN_NOTICE "ftl: FTL EraseUnitSize %x != MTD erasesize %x\n",
  149. 1 << header.EraseUnitSize,part->mbd.mtd->erasesize);
  150. return -1;
  151. }
  152. part->header = header;
  153. return 0;
  154. }
  155. static int build_maps(partition_t *part)
  156. {
  157. erase_unit_header_t header;
  158. uint16_t xvalid, xtrans, i;
  159. unsigned blocks, j;
  160. int hdr_ok, ret = -1;
  161. ssize_t retval;
  162. loff_t offset;
  163. /* Set up erase unit maps */
  164. part->DataUnits = le16_to_cpu(part->header.NumEraseUnits) -
  165. part->header.NumTransferUnits;
  166. part->EUNInfo = kmalloc_array(part->DataUnits, sizeof(struct eun_info_t),
  167. GFP_KERNEL);
  168. if (!part->EUNInfo)
  169. goto out;
  170. for (i = 0; i < part->DataUnits; i++)
  171. part->EUNInfo[i].Offset = 0xffffffff;
  172. part->XferInfo =
  173. kmalloc_array(part->header.NumTransferUnits,
  174. sizeof(struct xfer_info_t),
  175. GFP_KERNEL);
  176. if (!part->XferInfo)
  177. goto out_EUNInfo;
  178. xvalid = xtrans = 0;
  179. for (i = 0; i < le16_to_cpu(part->header.NumEraseUnits); i++) {
  180. offset = ((i + le16_to_cpu(part->header.FirstPhysicalEUN))
  181. << part->header.EraseUnitSize);
  182. ret = mtd_read(part->mbd.mtd, offset, sizeof(header), &retval,
  183. (unsigned char *)&header);
  184. if (ret)
  185. goto out_XferInfo;
  186. ret = -1;
  187. /* Is this a transfer partition? */
  188. hdr_ok = (strcmp(header.DataOrgTuple+3, "FTL100") == 0);
  189. if (hdr_ok && (le16_to_cpu(header.LogicalEUN) < part->DataUnits) &&
  190. (part->EUNInfo[le16_to_cpu(header.LogicalEUN)].Offset == 0xffffffff)) {
  191. part->EUNInfo[le16_to_cpu(header.LogicalEUN)].Offset = offset;
  192. part->EUNInfo[le16_to_cpu(header.LogicalEUN)].EraseCount =
  193. le32_to_cpu(header.EraseCount);
  194. xvalid++;
  195. } else {
  196. if (xtrans == part->header.NumTransferUnits) {
  197. printk(KERN_NOTICE "ftl_cs: format error: too many "
  198. "transfer units!\n");
  199. goto out_XferInfo;
  200. }
  201. if (hdr_ok && (le16_to_cpu(header.LogicalEUN) == 0xffff)) {
  202. part->XferInfo[xtrans].state = XFER_PREPARED;
  203. part->XferInfo[xtrans].EraseCount = le32_to_cpu(header.EraseCount);
  204. } else {
  205. part->XferInfo[xtrans].state = XFER_UNKNOWN;
  206. /* Pick anything reasonable for the erase count */
  207. part->XferInfo[xtrans].EraseCount =
  208. le32_to_cpu(part->header.EraseCount);
  209. }
  210. part->XferInfo[xtrans].Offset = offset;
  211. xtrans++;
  212. }
  213. }
  214. /* Check for format trouble */
  215. header = part->header;
  216. if ((xtrans != header.NumTransferUnits) ||
  217. (xvalid+xtrans != le16_to_cpu(header.NumEraseUnits))) {
  218. printk(KERN_NOTICE "ftl_cs: format error: erase units "
  219. "don't add up!\n");
  220. goto out_XferInfo;
  221. }
  222. /* Set up virtual page map */
  223. blocks = le32_to_cpu(header.FormattedSize) >> header.BlockSize;
  224. part->VirtualBlockMap = vmalloc(array_size(blocks, sizeof(uint32_t)));
  225. if (!part->VirtualBlockMap)
  226. goto out_XferInfo;
  227. memset(part->VirtualBlockMap, 0xff, blocks * sizeof(uint32_t));
  228. part->BlocksPerUnit = (1 << header.EraseUnitSize) >> header.BlockSize;
  229. part->bam_cache = kmalloc_array(part->BlocksPerUnit, sizeof(uint32_t),
  230. GFP_KERNEL);
  231. if (!part->bam_cache)
  232. goto out_VirtualBlockMap;
  233. part->bam_index = 0xffff;
  234. part->FreeTotal = 0;
  235. for (i = 0; i < part->DataUnits; i++) {
  236. part->EUNInfo[i].Free = 0;
  237. part->EUNInfo[i].Deleted = 0;
  238. offset = part->EUNInfo[i].Offset + le32_to_cpu(header.BAMOffset);
  239. ret = mtd_read(part->mbd.mtd, offset,
  240. part->BlocksPerUnit * sizeof(uint32_t), &retval,
  241. (unsigned char *)part->bam_cache);
  242. if (ret)
  243. goto out_bam_cache;
  244. for (j = 0; j < part->BlocksPerUnit; j++) {
  245. if (BLOCK_FREE(le32_to_cpu(part->bam_cache[j]))) {
  246. part->EUNInfo[i].Free++;
  247. part->FreeTotal++;
  248. } else if ((BLOCK_TYPE(le32_to_cpu(part->bam_cache[j])) == BLOCK_DATA) &&
  249. (BLOCK_NUMBER(le32_to_cpu(part->bam_cache[j])) < blocks))
  250. part->VirtualBlockMap[BLOCK_NUMBER(le32_to_cpu(part->bam_cache[j]))] =
  251. (i << header.EraseUnitSize) + (j << header.BlockSize);
  252. else if (BLOCK_DELETED(le32_to_cpu(part->bam_cache[j])))
  253. part->EUNInfo[i].Deleted++;
  254. }
  255. }
  256. ret = 0;
  257. goto out;
  258. out_bam_cache:
  259. kfree(part->bam_cache);
  260. out_VirtualBlockMap:
  261. vfree(part->VirtualBlockMap);
  262. out_XferInfo:
  263. kfree(part->XferInfo);
  264. out_EUNInfo:
  265. kfree(part->EUNInfo);
  266. out:
  267. return ret;
  268. } /* build_maps */
  269. /*======================================================================
  270. Erase_xfer() schedules an asynchronous erase operation for a
  271. transfer unit.
  272. ======================================================================*/
  273. static int erase_xfer(partition_t *part,
  274. uint16_t xfernum)
  275. {
  276. int ret;
  277. struct xfer_info_t *xfer;
  278. struct erase_info *erase;
  279. xfer = &part->XferInfo[xfernum];
  280. pr_debug("ftl_cs: erasing xfer unit at 0x%x\n", xfer->Offset);
  281. xfer->state = XFER_ERASING;
  282. /* Is there a free erase slot? Always in MTD. */
  283. erase=kmalloc(sizeof(struct erase_info), GFP_KERNEL);
  284. if (!erase)
  285. return -ENOMEM;
  286. erase->addr = xfer->Offset;
  287. erase->len = 1 << part->header.EraseUnitSize;
  288. ret = mtd_erase(part->mbd.mtd, erase);
  289. if (!ret) {
  290. xfer->state = XFER_ERASED;
  291. xfer->EraseCount++;
  292. } else {
  293. xfer->state = XFER_FAILED;
  294. pr_notice("ftl_cs: erase failed: err = %d\n", ret);
  295. }
  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 int prepare_xfer(partition_t *part, int i)
  304. {
  305. erase_unit_header_t header;
  306. struct xfer_info_t *xfer;
  307. int nbam, ret;
  308. uint32_t ctl;
  309. ssize_t retlen;
  310. loff_t offset;
  311. xfer = &part->XferInfo[i];
  312. xfer->state = XFER_FAILED;
  313. pr_debug("ftl_cs: preparing xfer unit at 0x%x\n", xfer->Offset);
  314. /* Write the transfer unit header */
  315. header = part->header;
  316. header.LogicalEUN = cpu_to_le16(0xffff);
  317. header.EraseCount = cpu_to_le32(xfer->EraseCount);
  318. ret = mtd_write(part->mbd.mtd, xfer->Offset, sizeof(header), &retlen,
  319. (u_char *)&header);
  320. if (ret) {
  321. return ret;
  322. }
  323. /* Write the BAM stub */
  324. nbam = DIV_ROUND_UP(part->BlocksPerUnit * sizeof(uint32_t) +
  325. le32_to_cpu(part->header.BAMOffset), SECTOR_SIZE);
  326. offset = xfer->Offset + le32_to_cpu(part->header.BAMOffset);
  327. ctl = cpu_to_le32(BLOCK_CONTROL);
  328. for (i = 0; i < nbam; i++, offset += sizeof(uint32_t)) {
  329. ret = mtd_write(part->mbd.mtd, offset, sizeof(uint32_t), &retlen,
  330. (u_char *)&ctl);
  331. if (ret)
  332. return ret;
  333. }
  334. xfer->state = XFER_PREPARED;
  335. return 0;
  336. } /* prepare_xfer */
  337. /*======================================================================
  338. Copy_erase_unit() takes a full erase block and a transfer unit,
  339. copies everything to the transfer unit, then swaps the block
  340. pointers.
  341. All data blocks are copied to the corresponding blocks in the
  342. target unit, so the virtual block map does not need to be
  343. updated.
  344. ======================================================================*/
  345. static int copy_erase_unit(partition_t *part, uint16_t srcunit,
  346. uint16_t xferunit)
  347. {
  348. u_char buf[SECTOR_SIZE];
  349. struct eun_info_t *eun;
  350. struct xfer_info_t *xfer;
  351. uint32_t src, dest, free, i;
  352. uint16_t unit;
  353. int ret;
  354. ssize_t retlen;
  355. loff_t offset;
  356. uint16_t srcunitswap = cpu_to_le16(srcunit);
  357. eun = &part->EUNInfo[srcunit];
  358. xfer = &part->XferInfo[xferunit];
  359. pr_debug("ftl_cs: copying block 0x%x to 0x%x\n",
  360. eun->Offset, xfer->Offset);
  361. /* Read current BAM */
  362. if (part->bam_index != srcunit) {
  363. offset = eun->Offset + le32_to_cpu(part->header.BAMOffset);
  364. ret = mtd_read(part->mbd.mtd, offset,
  365. part->BlocksPerUnit * sizeof(uint32_t), &retlen,
  366. (u_char *)(part->bam_cache));
  367. /* mark the cache bad, in case we get an error later */
  368. part->bam_index = 0xffff;
  369. if (ret) {
  370. printk( KERN_WARNING "ftl: Failed to read BAM cache in copy_erase_unit()!\n");
  371. return ret;
  372. }
  373. }
  374. /* Write the LogicalEUN for the transfer unit */
  375. xfer->state = XFER_UNKNOWN;
  376. offset = xfer->Offset + 20; /* Bad! */
  377. unit = cpu_to_le16(0x7fff);
  378. ret = mtd_write(part->mbd.mtd, offset, sizeof(uint16_t), &retlen,
  379. (u_char *)&unit);
  380. if (ret) {
  381. printk( KERN_WARNING "ftl: Failed to write back to BAM cache in copy_erase_unit()!\n");
  382. return ret;
  383. }
  384. /* Copy all data blocks from source unit to transfer unit */
  385. src = eun->Offset; dest = xfer->Offset;
  386. free = 0;
  387. ret = 0;
  388. for (i = 0; i < part->BlocksPerUnit; i++) {
  389. switch (BLOCK_TYPE(le32_to_cpu(part->bam_cache[i]))) {
  390. case BLOCK_CONTROL:
  391. /* This gets updated later */
  392. break;
  393. case BLOCK_DATA:
  394. case BLOCK_REPLACEMENT:
  395. ret = mtd_read(part->mbd.mtd, src, SECTOR_SIZE, &retlen,
  396. (u_char *)buf);
  397. if (ret) {
  398. printk(KERN_WARNING "ftl: Error reading old xfer unit in copy_erase_unit\n");
  399. return ret;
  400. }
  401. ret = mtd_write(part->mbd.mtd, dest, SECTOR_SIZE, &retlen,
  402. (u_char *)buf);
  403. if (ret) {
  404. printk(KERN_WARNING "ftl: Error writing new xfer unit in copy_erase_unit\n");
  405. return ret;
  406. }
  407. break;
  408. default:
  409. /* All other blocks must be free */
  410. part->bam_cache[i] = cpu_to_le32(0xffffffff);
  411. free++;
  412. break;
  413. }
  414. src += SECTOR_SIZE;
  415. dest += SECTOR_SIZE;
  416. }
  417. /* Write the BAM to the transfer unit */
  418. ret = mtd_write(part->mbd.mtd,
  419. xfer->Offset + le32_to_cpu(part->header.BAMOffset),
  420. part->BlocksPerUnit * sizeof(int32_t),
  421. &retlen,
  422. (u_char *)part->bam_cache);
  423. if (ret) {
  424. printk( KERN_WARNING "ftl: Error writing BAM in copy_erase_unit\n");
  425. return ret;
  426. }
  427. /* All clear? Then update the LogicalEUN again */
  428. ret = mtd_write(part->mbd.mtd, xfer->Offset + 20, sizeof(uint16_t),
  429. &retlen, (u_char *)&srcunitswap);
  430. if (ret) {
  431. printk(KERN_WARNING "ftl: Error writing new LogicalEUN in copy_erase_unit\n");
  432. return ret;
  433. }
  434. /* Update the maps and usage stats*/
  435. swap(xfer->EraseCount, eun->EraseCount);
  436. swap(xfer->Offset, eun->Offset);
  437. part->FreeTotal -= eun->Free;
  438. part->FreeTotal += free;
  439. eun->Free = free;
  440. eun->Deleted = 0;
  441. /* Now, the cache should be valid for the new block */
  442. part->bam_index = srcunit;
  443. return 0;
  444. } /* copy_erase_unit */
  445. /*======================================================================
  446. reclaim_block() picks a full erase unit and a transfer unit and
  447. then calls copy_erase_unit() to copy one to the other. Then, it
  448. schedules an erase on the expired block.
  449. What's a good way to decide which transfer unit and which erase
  450. unit to use? Beats me. My way is to always pick the transfer
  451. unit with the fewest erases, and usually pick the data unit with
  452. the most deleted blocks. But with a small probability, pick the
  453. oldest data unit instead. This means that we generally postpone
  454. the next reclamation as long as possible, but shuffle static
  455. stuff around a bit for wear leveling.
  456. ======================================================================*/
  457. static int reclaim_block(partition_t *part)
  458. {
  459. uint16_t i, eun, xfer;
  460. uint32_t best;
  461. int queued, ret;
  462. pr_debug("ftl_cs: reclaiming space...\n");
  463. pr_debug("NumTransferUnits == %x\n", part->header.NumTransferUnits);
  464. /* Pick the least erased transfer unit */
  465. best = 0xffffffff; xfer = 0xffff;
  466. do {
  467. queued = 0;
  468. for (i = 0; i < part->header.NumTransferUnits; i++) {
  469. int n=0;
  470. if (part->XferInfo[i].state == XFER_UNKNOWN) {
  471. pr_debug("XferInfo[%d].state == XFER_UNKNOWN\n",i);
  472. n=1;
  473. erase_xfer(part, i);
  474. }
  475. if (part->XferInfo[i].state == XFER_ERASING) {
  476. pr_debug("XferInfo[%d].state == XFER_ERASING\n",i);
  477. n=1;
  478. queued = 1;
  479. }
  480. else if (part->XferInfo[i].state == XFER_ERASED) {
  481. pr_debug("XferInfo[%d].state == XFER_ERASED\n",i);
  482. n=1;
  483. prepare_xfer(part, i);
  484. }
  485. if (part->XferInfo[i].state == XFER_PREPARED) {
  486. pr_debug("XferInfo[%d].state == XFER_PREPARED\n",i);
  487. n=1;
  488. if (part->XferInfo[i].EraseCount <= best) {
  489. best = part->XferInfo[i].EraseCount;
  490. xfer = i;
  491. }
  492. }
  493. if (!n)
  494. pr_debug("XferInfo[%d].state == %x\n",i, part->XferInfo[i].state);
  495. }
  496. if (xfer == 0xffff) {
  497. if (queued) {
  498. pr_debug("ftl_cs: waiting for transfer "
  499. "unit to be prepared...\n");
  500. mtd_sync(part->mbd.mtd);
  501. } else {
  502. static int ne = 0;
  503. if (++ne < 5)
  504. printk(KERN_NOTICE "ftl_cs: reclaim failed: no "
  505. "suitable transfer units!\n");
  506. else
  507. pr_debug("ftl_cs: reclaim failed: no "
  508. "suitable transfer units!\n");
  509. return -EIO;
  510. }
  511. }
  512. } while (xfer == 0xffff);
  513. eun = 0;
  514. if ((jiffies % shuffle_freq) == 0) {
  515. pr_debug("ftl_cs: recycling freshest block...\n");
  516. best = 0xffffffff;
  517. for (i = 0; i < part->DataUnits; i++)
  518. if (part->EUNInfo[i].EraseCount <= best) {
  519. best = part->EUNInfo[i].EraseCount;
  520. eun = i;
  521. }
  522. } else {
  523. best = 0;
  524. for (i = 0; i < part->DataUnits; i++)
  525. if (part->EUNInfo[i].Deleted >= best) {
  526. best = part->EUNInfo[i].Deleted;
  527. eun = i;
  528. }
  529. if (best == 0) {
  530. static int ne = 0;
  531. if (++ne < 5)
  532. printk(KERN_NOTICE "ftl_cs: reclaim failed: "
  533. "no free blocks!\n");
  534. else
  535. pr_debug("ftl_cs: reclaim failed: "
  536. "no free blocks!\n");
  537. return -EIO;
  538. }
  539. }
  540. ret = copy_erase_unit(part, eun, xfer);
  541. if (!ret)
  542. erase_xfer(part, xfer);
  543. else
  544. printk(KERN_NOTICE "ftl_cs: copy_erase_unit failed!\n");
  545. return ret;
  546. } /* reclaim_block */
  547. /*======================================================================
  548. Find_free() searches for a free block. If necessary, it updates
  549. the BAM cache for the erase unit containing the free block. It
  550. returns the block index -- the erase unit is just the currently
  551. cached unit. If there are no free blocks, it returns 0 -- this
  552. is never a valid data block because it contains the header.
  553. ======================================================================*/
  554. #ifdef PSYCHO_DEBUG
  555. static void dump_lists(partition_t *part)
  556. {
  557. int i;
  558. printk(KERN_DEBUG "ftl_cs: Free total = %d\n", part->FreeTotal);
  559. for (i = 0; i < part->DataUnits; i++)
  560. printk(KERN_DEBUG "ftl_cs: unit %d: %d phys, %d free, "
  561. "%d deleted\n", i,
  562. part->EUNInfo[i].Offset >> part->header.EraseUnitSize,
  563. part->EUNInfo[i].Free, part->EUNInfo[i].Deleted);
  564. }
  565. #endif
  566. static uint32_t find_free(partition_t *part)
  567. {
  568. uint16_t stop, eun;
  569. uint32_t blk;
  570. size_t retlen;
  571. int ret;
  572. /* Find an erase unit with some free space */
  573. stop = (part->bam_index == 0xffff) ? 0 : part->bam_index;
  574. eun = stop;
  575. do {
  576. if (part->EUNInfo[eun].Free != 0) break;
  577. /* Wrap around at end of table */
  578. if (++eun == part->DataUnits) eun = 0;
  579. } while (eun != stop);
  580. if (part->EUNInfo[eun].Free == 0)
  581. return 0;
  582. /* Is this unit's BAM cached? */
  583. if (eun != part->bam_index) {
  584. /* Invalidate cache */
  585. part->bam_index = 0xffff;
  586. ret = mtd_read(part->mbd.mtd,
  587. part->EUNInfo[eun].Offset + le32_to_cpu(part->header.BAMOffset),
  588. part->BlocksPerUnit * sizeof(uint32_t),
  589. &retlen,
  590. (u_char *)(part->bam_cache));
  591. if (ret) {
  592. printk(KERN_WARNING"ftl: Error reading BAM in find_free\n");
  593. return 0;
  594. }
  595. part->bam_index = eun;
  596. }
  597. /* Find a free block */
  598. for (blk = 0; blk < part->BlocksPerUnit; blk++)
  599. if (BLOCK_FREE(le32_to_cpu(part->bam_cache[blk]))) break;
  600. if (blk == part->BlocksPerUnit) {
  601. #ifdef PSYCHO_DEBUG
  602. static int ne = 0;
  603. if (++ne == 1)
  604. dump_lists(part);
  605. #endif
  606. printk(KERN_NOTICE "ftl_cs: bad free list!\n");
  607. return 0;
  608. }
  609. pr_debug("ftl_cs: found free block at %d in %d\n", blk, eun);
  610. return blk;
  611. } /* find_free */
  612. /*======================================================================
  613. Read a series of sectors from an FTL partition.
  614. ======================================================================*/
  615. static int ftl_read(partition_t *part, caddr_t buffer,
  616. u_long sector, u_long nblocks)
  617. {
  618. uint32_t log_addr, bsize;
  619. u_long i;
  620. int ret;
  621. size_t offset, retlen;
  622. pr_debug("ftl_cs: ftl_read(0x%p, 0x%lx, %ld)\n",
  623. part, sector, nblocks);
  624. if (!(part->state & FTL_FORMATTED)) {
  625. printk(KERN_NOTICE "ftl_cs: bad partition\n");
  626. return -EIO;
  627. }
  628. bsize = 1 << part->header.EraseUnitSize;
  629. for (i = 0; i < nblocks; i++) {
  630. if (((sector+i) * SECTOR_SIZE) >= le32_to_cpu(part->header.FormattedSize)) {
  631. printk(KERN_NOTICE "ftl_cs: bad read offset\n");
  632. return -EIO;
  633. }
  634. log_addr = part->VirtualBlockMap[sector+i];
  635. if (log_addr == 0xffffffff)
  636. memset(buffer, 0, SECTOR_SIZE);
  637. else {
  638. offset = (part->EUNInfo[log_addr / bsize].Offset
  639. + (log_addr % bsize));
  640. ret = mtd_read(part->mbd.mtd, offset, SECTOR_SIZE, &retlen,
  641. (u_char *)buffer);
  642. if (ret) {
  643. printk(KERN_WARNING "Error reading MTD device in ftl_read()\n");
  644. return ret;
  645. }
  646. }
  647. buffer += SECTOR_SIZE;
  648. }
  649. return 0;
  650. } /* ftl_read */
  651. /*======================================================================
  652. Write a series of sectors to an FTL partition
  653. ======================================================================*/
  654. static int set_bam_entry(partition_t *part, uint32_t log_addr,
  655. uint32_t virt_addr)
  656. {
  657. uint32_t bsize, blk, le_virt_addr;
  658. #ifdef PSYCHO_DEBUG
  659. uint32_t old_addr;
  660. #endif
  661. uint16_t eun;
  662. int ret;
  663. size_t retlen, offset;
  664. pr_debug("ftl_cs: set_bam_entry(0x%p, 0x%x, 0x%x)\n",
  665. part, log_addr, virt_addr);
  666. bsize = 1 << part->header.EraseUnitSize;
  667. eun = log_addr / bsize;
  668. blk = (log_addr % bsize) / SECTOR_SIZE;
  669. offset = (part->EUNInfo[eun].Offset + blk * sizeof(uint32_t) +
  670. le32_to_cpu(part->header.BAMOffset));
  671. #ifdef PSYCHO_DEBUG
  672. ret = mtd_read(part->mbd.mtd, offset, sizeof(uint32_t), &retlen,
  673. (u_char *)&old_addr);
  674. if (ret) {
  675. printk(KERN_WARNING"ftl: Error reading old_addr in set_bam_entry: %d\n",ret);
  676. return ret;
  677. }
  678. old_addr = le32_to_cpu(old_addr);
  679. if (((virt_addr == 0xfffffffe) && !BLOCK_FREE(old_addr)) ||
  680. ((virt_addr == 0) && (BLOCK_TYPE(old_addr) != BLOCK_DATA)) ||
  681. (!BLOCK_DELETED(virt_addr) && (old_addr != 0xfffffffe))) {
  682. static int ne = 0;
  683. if (++ne < 5) {
  684. printk(KERN_NOTICE "ftl_cs: set_bam_entry() inconsistency!\n");
  685. printk(KERN_NOTICE "ftl_cs: log_addr = 0x%x, old = 0x%x"
  686. ", new = 0x%x\n", log_addr, old_addr, virt_addr);
  687. }
  688. return -EIO;
  689. }
  690. #endif
  691. le_virt_addr = cpu_to_le32(virt_addr);
  692. if (part->bam_index == eun) {
  693. #ifdef PSYCHO_DEBUG
  694. if (le32_to_cpu(part->bam_cache[blk]) != old_addr) {
  695. static int ne = 0;
  696. if (++ne < 5) {
  697. printk(KERN_NOTICE "ftl_cs: set_bam_entry() "
  698. "inconsistency!\n");
  699. printk(KERN_NOTICE "ftl_cs: log_addr = 0x%x, cache"
  700. " = 0x%x\n",
  701. le32_to_cpu(part->bam_cache[blk]), old_addr);
  702. }
  703. return -EIO;
  704. }
  705. #endif
  706. part->bam_cache[blk] = le_virt_addr;
  707. }
  708. ret = mtd_write(part->mbd.mtd, offset, sizeof(uint32_t), &retlen,
  709. (u_char *)&le_virt_addr);
  710. if (ret) {
  711. printk(KERN_NOTICE "ftl_cs: set_bam_entry() failed!\n");
  712. printk(KERN_NOTICE "ftl_cs: log_addr = 0x%x, new = 0x%x\n",
  713. log_addr, virt_addr);
  714. }
  715. return ret;
  716. } /* set_bam_entry */
  717. static int ftl_write(partition_t *part, caddr_t buffer,
  718. u_long sector, u_long nblocks)
  719. {
  720. uint32_t bsize, log_addr, virt_addr, old_addr, blk;
  721. u_long i;
  722. int ret;
  723. size_t retlen, offset;
  724. pr_debug("ftl_cs: ftl_write(0x%p, %ld, %ld)\n",
  725. part, sector, nblocks);
  726. if (!(part->state & FTL_FORMATTED)) {
  727. printk(KERN_NOTICE "ftl_cs: bad partition\n");
  728. return -EIO;
  729. }
  730. /* See if we need to reclaim space, before we start */
  731. while (part->FreeTotal < nblocks) {
  732. ret = reclaim_block(part);
  733. if (ret)
  734. return ret;
  735. }
  736. bsize = 1 << part->header.EraseUnitSize;
  737. virt_addr = sector * SECTOR_SIZE | BLOCK_DATA;
  738. for (i = 0; i < nblocks; i++) {
  739. if (virt_addr >= le32_to_cpu(part->header.FormattedSize)) {
  740. printk(KERN_NOTICE "ftl_cs: bad write offset\n");
  741. return -EIO;
  742. }
  743. /* Grab a free block */
  744. blk = find_free(part);
  745. if (blk == 0) {
  746. static int ne = 0;
  747. if (++ne < 5)
  748. printk(KERN_NOTICE "ftl_cs: internal error: "
  749. "no free blocks!\n");
  750. return -ENOSPC;
  751. }
  752. /* Tag the BAM entry, and write the new block */
  753. log_addr = part->bam_index * bsize + blk * SECTOR_SIZE;
  754. part->EUNInfo[part->bam_index].Free--;
  755. part->FreeTotal--;
  756. if (set_bam_entry(part, log_addr, 0xfffffffe))
  757. return -EIO;
  758. part->EUNInfo[part->bam_index].Deleted++;
  759. offset = (part->EUNInfo[part->bam_index].Offset +
  760. blk * SECTOR_SIZE);
  761. ret = mtd_write(part->mbd.mtd, offset, SECTOR_SIZE, &retlen, buffer);
  762. if (ret) {
  763. printk(KERN_NOTICE "ftl_cs: block write failed!\n");
  764. printk(KERN_NOTICE "ftl_cs: log_addr = 0x%x, virt_addr"
  765. " = 0x%x, Offset = 0x%zx\n", log_addr, virt_addr,
  766. offset);
  767. return -EIO;
  768. }
  769. /* Only delete the old entry when the new entry is ready */
  770. old_addr = part->VirtualBlockMap[sector+i];
  771. if (old_addr != 0xffffffff) {
  772. part->VirtualBlockMap[sector+i] = 0xffffffff;
  773. part->EUNInfo[old_addr/bsize].Deleted++;
  774. if (set_bam_entry(part, old_addr, 0))
  775. return -EIO;
  776. }
  777. /* Finally, set up the new pointers */
  778. if (set_bam_entry(part, log_addr, virt_addr))
  779. return -EIO;
  780. part->VirtualBlockMap[sector+i] = log_addr;
  781. part->EUNInfo[part->bam_index].Deleted--;
  782. buffer += SECTOR_SIZE;
  783. virt_addr += SECTOR_SIZE;
  784. }
  785. return 0;
  786. } /* ftl_write */
  787. static int ftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
  788. {
  789. partition_t *part = (void *)dev;
  790. u_long sect;
  791. /* Sort of arbitrary: round size down to 4KiB boundary */
  792. sect = le32_to_cpu(part->header.FormattedSize)/SECTOR_SIZE;
  793. geo->heads = 1;
  794. geo->sectors = 8;
  795. geo->cylinders = sect >> 3;
  796. return 0;
  797. }
  798. static int ftl_readsect(struct mtd_blktrans_dev *dev,
  799. unsigned long block, char *buf)
  800. {
  801. return ftl_read((void *)dev, buf, block, 1);
  802. }
  803. static int ftl_writesect(struct mtd_blktrans_dev *dev,
  804. unsigned long block, char *buf)
  805. {
  806. return ftl_write((void *)dev, buf, block, 1);
  807. }
  808. static int ftl_discardsect(struct mtd_blktrans_dev *dev,
  809. unsigned long sector, unsigned nr_sects)
  810. {
  811. partition_t *part = (void *)dev;
  812. uint32_t bsize = 1 << part->header.EraseUnitSize;
  813. pr_debug("FTL erase sector %ld for %d sectors\n",
  814. sector, nr_sects);
  815. while (nr_sects) {
  816. uint32_t old_addr = part->VirtualBlockMap[sector];
  817. if (old_addr != 0xffffffff) {
  818. part->VirtualBlockMap[sector] = 0xffffffff;
  819. part->EUNInfo[old_addr/bsize].Deleted++;
  820. if (set_bam_entry(part, old_addr, 0))
  821. return -EIO;
  822. }
  823. nr_sects--;
  824. sector++;
  825. }
  826. return 0;
  827. }
  828. /*====================================================================*/
  829. static void ftl_freepart(partition_t *part)
  830. {
  831. vfree(part->VirtualBlockMap);
  832. part->VirtualBlockMap = NULL;
  833. kfree(part->EUNInfo);
  834. part->EUNInfo = NULL;
  835. kfree(part->XferInfo);
  836. part->XferInfo = NULL;
  837. kfree(part->bam_cache);
  838. part->bam_cache = NULL;
  839. } /* ftl_freepart */
  840. static void ftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
  841. {
  842. partition_t *partition;
  843. partition = kzalloc(sizeof(partition_t), GFP_KERNEL);
  844. if (!partition) {
  845. printk(KERN_WARNING "No memory to scan for FTL on %s\n",
  846. mtd->name);
  847. return;
  848. }
  849. partition->mbd.mtd = mtd;
  850. if ((scan_header(partition) == 0) &&
  851. (build_maps(partition) == 0)) {
  852. partition->state = FTL_FORMATTED;
  853. #ifdef PCMCIA_DEBUG
  854. printk(KERN_INFO "ftl_cs: opening %d KiB FTL partition\n",
  855. le32_to_cpu(partition->header.FormattedSize) >> 10);
  856. #endif
  857. partition->mbd.size = le32_to_cpu(partition->header.FormattedSize) >> 9;
  858. partition->mbd.tr = tr;
  859. partition->mbd.devnum = -1;
  860. if (!add_mtd_blktrans_dev((void *)partition))
  861. return;
  862. }
  863. kfree(partition);
  864. }
  865. static void ftl_remove_dev(struct mtd_blktrans_dev *dev)
  866. {
  867. del_mtd_blktrans_dev(dev);
  868. ftl_freepart((partition_t *)dev);
  869. }
  870. static struct mtd_blktrans_ops ftl_tr = {
  871. .name = "ftl",
  872. .major = FTL_MAJOR,
  873. .part_bits = PART_BITS,
  874. .blksize = SECTOR_SIZE,
  875. .readsect = ftl_readsect,
  876. .writesect = ftl_writesect,
  877. .discard = ftl_discardsect,
  878. .getgeo = ftl_getgeo,
  879. .add_mtd = ftl_add_mtd,
  880. .remove_dev = ftl_remove_dev,
  881. .owner = THIS_MODULE,
  882. };
  883. static int __init init_ftl(void)
  884. {
  885. return register_mtd_blktrans(&ftl_tr);
  886. }
  887. static void __exit cleanup_ftl(void)
  888. {
  889. deregister_mtd_blktrans(&ftl_tr);
  890. }
  891. module_init(init_ftl);
  892. module_exit(cleanup_ftl);
  893. MODULE_LICENSE("Dual MPL/GPL");
  894. MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
  895. MODULE_DESCRIPTION("Support code for Flash Translation Layer, used on PCMCIA devices");