blob: 539835fabe6162ab666f7b01345a9ad5ca96092b [file] [log] [blame]
Maxim Levitsky7d17c022010-02-22 20:39:41 +02001/*
2 * Copyright © 2009 - Maxim Levitsky
3 * SmartMedia/xD translation layer
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/random.h>
13#include <linux/hdreg.h>
14#include <linux/kthread.h>
15#include <linux/freezer.h>
16#include <linux/sysfs.h>
17#include <linux/bitops.h>
Stephen Rothwell8da552f2010-03-29 16:53:13 +110018#include <linux/slab.h>
Maxim Levitsky01de69c2010-03-19 14:05:51 +020019#include <linux/mtd/nand_ecc.h>
Maxim Levitsky7d17c022010-02-22 20:39:41 +020020#include "nand/sm_common.h"
21#include "sm_ftl.h"
22
Maxim Levitsky7d17c022010-02-22 20:39:41 +020023
24
25struct workqueue_struct *cache_flush_workqueue;
26
27static int cache_timeout = 1000;
Rusty Russellf9fbcdc2011-12-15 13:35:49 +103028module_param(cache_timeout, int, S_IRUGO);
Maxim Levitsky7d17c022010-02-22 20:39:41 +020029MODULE_PARM_DESC(cache_timeout,
30 "Timeout (in ms) for cache flush (1000 ms default");
31
32static int debug;
33module_param(debug, int, S_IRUGO | S_IWUSR);
34MODULE_PARM_DESC(debug, "Debug level (0-2)");
35
36
Brian Norris92394b52011-07-20 09:53:42 -070037/* ------------------- sysfs attributes ---------------------------------- */
Maxim Levitsky7d17c022010-02-22 20:39:41 +020038struct sm_sysfs_attribute {
39 struct device_attribute dev_attr;
40 char *data;
41 int len;
42};
43
44ssize_t sm_attr_show(struct device *dev, struct device_attribute *attr,
45 char *buf)
46{
47 struct sm_sysfs_attribute *sm_attr =
48 container_of(attr, struct sm_sysfs_attribute, dev_attr);
49
50 strncpy(buf, sm_attr->data, sm_attr->len);
51 return sm_attr->len;
52}
53
54
55#define NUM_ATTRIBUTES 1
56#define SM_CIS_VENDOR_OFFSET 0x59
57struct attribute_group *sm_create_sysfs_attributes(struct sm_ftl *ftl)
58{
59 struct attribute_group *attr_group;
60 struct attribute **attributes;
61 struct sm_sysfs_attribute *vendor_attribute;
Dan Carpenter16df0da2013-12-05 17:53:50 +030062 char *vendor;
Maxim Levitsky7d17c022010-02-22 20:39:41 +020063
Dan Carpenter16df0da2013-12-05 17:53:50 +030064 vendor = kstrndup(ftl->cis_buffer + SM_CIS_VENDOR_OFFSET,
65 SM_SMALL_PAGE - SM_CIS_VENDOR_OFFSET, GFP_KERNEL);
Xiaochen Wang629286b92011-03-10 22:31:39 +080066 if (!vendor)
67 goto error1;
Maxim Levitsky7d17c022010-02-22 20:39:41 +020068
69 /* Initialize sysfs attributes */
70 vendor_attribute =
71 kzalloc(sizeof(struct sm_sysfs_attribute), GFP_KERNEL);
Xiaochen Wang629286b92011-03-10 22:31:39 +080072 if (!vendor_attribute)
73 goto error2;
Maxim Levitsky7d17c022010-02-22 20:39:41 +020074
Maxim Levitskyca7081d2010-03-19 17:22:53 +020075 sysfs_attr_init(&vendor_attribute->dev_attr.attr);
76
Maxim Levitsky7d17c022010-02-22 20:39:41 +020077 vendor_attribute->data = vendor;
Dan Carpenter16df0da2013-12-05 17:53:50 +030078 vendor_attribute->len = strlen(vendor);
Maxim Levitsky7d17c022010-02-22 20:39:41 +020079 vendor_attribute->dev_attr.attr.name = "vendor";
80 vendor_attribute->dev_attr.attr.mode = S_IRUGO;
81 vendor_attribute->dev_attr.show = sm_attr_show;
82
83
84 /* Create array of pointers to the attributes */
85 attributes = kzalloc(sizeof(struct attribute *) * (NUM_ATTRIBUTES + 1),
86 GFP_KERNEL);
Xiaochen Wang629286b92011-03-10 22:31:39 +080087 if (!attributes)
88 goto error3;
Maxim Levitsky7d17c022010-02-22 20:39:41 +020089 attributes[0] = &vendor_attribute->dev_attr.attr;
90
91 /* Finally create the attribute group */
92 attr_group = kzalloc(sizeof(struct attribute_group), GFP_KERNEL);
Xiaochen Wang629286b92011-03-10 22:31:39 +080093 if (!attr_group)
94 goto error4;
Maxim Levitsky7d17c022010-02-22 20:39:41 +020095 attr_group->attrs = attributes;
96 return attr_group;
Xiaochen Wang629286b92011-03-10 22:31:39 +080097error4:
98 kfree(attributes);
99error3:
100 kfree(vendor_attribute);
101error2:
102 kfree(vendor);
103error1:
104 return NULL;
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200105}
106
107void sm_delete_sysfs_attributes(struct sm_ftl *ftl)
108{
109 struct attribute **attributes = ftl->disk_attributes->attrs;
110 int i;
111
112 for (i = 0; attributes[i] ; i++) {
113
114 struct device_attribute *dev_attr = container_of(attributes[i],
115 struct device_attribute, attr);
116
117 struct sm_sysfs_attribute *sm_attr =
118 container_of(dev_attr,
119 struct sm_sysfs_attribute, dev_attr);
120
121 kfree(sm_attr->data);
122 kfree(sm_attr);
123 }
124
125 kfree(ftl->disk_attributes->attrs);
126 kfree(ftl->disk_attributes);
127}
128
129
130/* ----------------------- oob helpers -------------------------------------- */
131
132static int sm_get_lba(uint8_t *lba)
133{
134 /* check fixed bits */
135 if ((lba[0] & 0xF8) != 0x10)
136 return -2;
137
Brian Norris7854d3f2011-06-23 14:12:08 -0700138 /* check parity - endianness doesn't matter */
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200139 if (hweight16(*(uint16_t *)lba) & 1)
140 return -2;
141
142 return (lba[1] >> 1) | ((lba[0] & 0x07) << 7);
143}
144
145
146/*
Brian Norris92394b52011-07-20 09:53:42 -0700147 * Read LBA associated with block
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200148 * returns -1, if block is erased
149 * returns -2 if error happens
150 */
151static int sm_read_lba(struct sm_oob *oob)
152{
153 static const uint32_t erased_pattern[4] = {
154 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
155
156 uint16_t lba_test;
157 int lba;
158
159 /* First test for erased block */
160 if (!memcmp(oob, erased_pattern, SM_OOB_SIZE))
161 return -1;
162
163 /* Now check is both copies of the LBA differ too much */
164 lba_test = *(uint16_t *)oob->lba_copy1 ^ *(uint16_t*)oob->lba_copy2;
165 if (lba_test && !is_power_of_2(lba_test))
166 return -2;
167
168 /* And read it */
169 lba = sm_get_lba(oob->lba_copy1);
170
171 if (lba == -2)
172 lba = sm_get_lba(oob->lba_copy2);
173
174 return lba;
175}
176
177static void sm_write_lba(struct sm_oob *oob, uint16_t lba)
178{
179 uint8_t tmp[2];
180
181 WARN_ON(lba >= 1000);
182
183 tmp[0] = 0x10 | ((lba >> 7) & 0x07);
184 tmp[1] = (lba << 1) & 0xFF;
185
186 if (hweight16(*(uint16_t *)tmp) & 0x01)
187 tmp[1] |= 1;
188
189 oob->lba_copy1[0] = oob->lba_copy2[0] = tmp[0];
190 oob->lba_copy1[1] = oob->lba_copy2[1] = tmp[1];
191}
192
193
194/* Make offset from parts */
195static loff_t sm_mkoffset(struct sm_ftl *ftl, int zone, int block, int boffset)
196{
197 WARN_ON(boffset & (SM_SECTOR_SIZE - 1));
198 WARN_ON(zone < 0 || zone >= ftl->zone_count);
199 WARN_ON(block >= ftl->zone_size);
200 WARN_ON(boffset >= ftl->block_size);
201
202 if (block == -1)
203 return -1;
204
205 return (zone * SM_MAX_ZONE_SIZE + block) * ftl->block_size + boffset;
206}
207
208/* Breaks offset into parts */
209static void sm_break_offset(struct sm_ftl *ftl, loff_t offset,
210 int *zone, int *block, int *boffset)
211{
212 *boffset = do_div(offset, ftl->block_size);
213 *block = do_div(offset, ftl->max_lba);
214 *zone = offset >= ftl->zone_count ? -1 : offset;
215}
216
217/* ---------------------- low level IO ------------------------------------- */
218
219static int sm_correct_sector(uint8_t *buffer, struct sm_oob *oob)
220{
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200221 uint8_t ecc[3];
222
223 __nand_calculate_ecc(buffer, SM_SMALL_PAGE, ecc);
224 if (__nand_correct_data(buffer, ecc, oob->ecc1, SM_SMALL_PAGE) < 0)
225 return -EIO;
226
227 buffer += SM_SMALL_PAGE;
228
229 __nand_calculate_ecc(buffer, SM_SMALL_PAGE, ecc);
230 if (__nand_correct_data(buffer, ecc, oob->ecc2, SM_SMALL_PAGE) < 0)
231 return -EIO;
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200232 return 0;
233}
234
235/* Reads a sector + oob*/
236static int sm_read_sector(struct sm_ftl *ftl,
237 int zone, int block, int boffset,
238 uint8_t *buffer, struct sm_oob *oob)
239{
240 struct mtd_info *mtd = ftl->trans->mtd;
241 struct mtd_oob_ops ops;
242 struct sm_oob tmp_oob;
Maxim Levitsky133fa8c2010-02-26 22:08:40 +0200243 int ret = -EIO;
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200244 int try = 0;
245
246 /* FTL can contain -1 entries that are by default filled with bits */
247 if (block == -1) {
248 memset(buffer, 0xFF, SM_SECTOR_SIZE);
249 return 0;
250 }
251
Brian Norris92394b52011-07-20 09:53:42 -0700252 /* User might not need the oob, but we do for data verification */
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200253 if (!oob)
254 oob = &tmp_oob;
255
Brian Norris0612b9d2011-08-30 18:45:40 -0700256 ops.mode = ftl->smallpagenand ? MTD_OPS_RAW : MTD_OPS_PLACE_OOB;
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200257 ops.ooboffs = 0;
258 ops.ooblen = SM_OOB_SIZE;
259 ops.oobbuf = (void *)oob;
260 ops.len = SM_SECTOR_SIZE;
261 ops.datbuf = buffer;
262
263again:
264 if (try++) {
265 /* Avoid infinite recursion on CIS reads, sm_recheck_media
266 won't help anyway */
267 if (zone == 0 && block == ftl->cis_block && boffset ==
268 ftl->cis_boffset)
269 return ret;
270
271 /* Test if media is stable */
272 if (try == 3 || sm_recheck_media(ftl))
273 return ret;
274 }
275
Brian Norris92394b52011-07-20 09:53:42 -0700276 /* Unfortunately, oob read will _always_ succeed,
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200277 despite card removal..... */
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200278 ret = mtd_read_oob(mtd, sm_mkoffset(ftl, zone, block, boffset), &ops);
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200279
280 /* Test for unknown errors */
Brian Norrisd57f40542011-09-20 18:34:25 -0700281 if (ret != 0 && !mtd_is_bitflip_or_eccerr(ret)) {
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200282 dbg("read of block %d at zone %d, failed due to error (%d)",
283 block, zone, ret);
284 goto again;
285 }
286
287 /* Do a basic test on the oob, to guard against returned garbage */
288 if (oob->reserved != 0xFFFFFFFF && !is_power_of_2(~oob->reserved))
289 goto again;
290
291 /* This should never happen, unless there is a bug in the mtd driver */
292 WARN_ON(ops.oobretlen != SM_OOB_SIZE);
293 WARN_ON(buffer && ops.retlen != SM_SECTOR_SIZE);
294
295 if (!buffer)
296 return 0;
297
298 /* Test if sector marked as bad */
299 if (!sm_sector_valid(oob)) {
300 dbg("read of block %d at zone %d, failed because it is marked"
301 " as bad" , block, zone);
302 goto again;
303 }
304
305 /* Test ECC*/
Brian Norrisd57f40542011-09-20 18:34:25 -0700306 if (mtd_is_eccerr(ret) ||
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200307 (ftl->smallpagenand && sm_correct_sector(buffer, oob))) {
308
309 dbg("read of block %d at zone %d, failed due to ECC error",
310 block, zone);
311 goto again;
312 }
313
314 return 0;
315}
316
317/* Writes a sector to media */
318static int sm_write_sector(struct sm_ftl *ftl,
319 int zone, int block, int boffset,
320 uint8_t *buffer, struct sm_oob *oob)
321{
322 struct mtd_oob_ops ops;
323 struct mtd_info *mtd = ftl->trans->mtd;
324 int ret;
325
326 BUG_ON(ftl->readonly);
327
328 if (zone == 0 && (block == ftl->cis_block || block == 0)) {
329 dbg("attempted to write the CIS!");
330 return -EIO;
331 }
332
333 if (ftl->unstable)
334 return -EIO;
335
Brian Norris0612b9d2011-08-30 18:45:40 -0700336 ops.mode = ftl->smallpagenand ? MTD_OPS_RAW : MTD_OPS_PLACE_OOB;
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200337 ops.len = SM_SECTOR_SIZE;
338 ops.datbuf = buffer;
339 ops.ooboffs = 0;
340 ops.ooblen = SM_OOB_SIZE;
341 ops.oobbuf = (void *)oob;
342
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200343 ret = mtd_write_oob(mtd, sm_mkoffset(ftl, zone, block, boffset), &ops);
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200344
345 /* Now we assume that hardware will catch write bitflip errors */
346 /* If you are paranoid, use CONFIG_MTD_NAND_VERIFY_WRITE */
347
348 if (ret) {
349 dbg("write to block %d at zone %d, failed with error %d",
350 block, zone, ret);
351
352 sm_recheck_media(ftl);
353 return ret;
354 }
355
356 /* This should never happen, unless there is a bug in the driver */
357 WARN_ON(ops.oobretlen != SM_OOB_SIZE);
358 WARN_ON(buffer && ops.retlen != SM_SECTOR_SIZE);
359
360 return 0;
361}
362
363/* ------------------------ block IO ------------------------------------- */
364
365/* Write a block using data and lba, and invalid sector bitmap */
366static int sm_write_block(struct sm_ftl *ftl, uint8_t *buf,
367 int zone, int block, int lba,
368 unsigned long invalid_bitmap)
369{
370 struct sm_oob oob;
371 int boffset;
372 int retry = 0;
373
374 /* Initialize the oob with requested values */
375 memset(&oob, 0xFF, SM_OOB_SIZE);
376 sm_write_lba(&oob, lba);
377restart:
378 if (ftl->unstable)
379 return -EIO;
380
381 for (boffset = 0; boffset < ftl->block_size;
382 boffset += SM_SECTOR_SIZE) {
383
384 oob.data_status = 0xFF;
385
386 if (test_bit(boffset / SM_SECTOR_SIZE, &invalid_bitmap)) {
387
388 sm_printk("sector %d of block at LBA %d of zone %d"
389 " coudn't be read, marking it as invalid",
390 boffset / SM_SECTOR_SIZE, lba, zone);
391
392 oob.data_status = 0;
393 }
394
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200395 if (ftl->smallpagenand) {
396 __nand_calculate_ecc(buf + boffset,
397 SM_SMALL_PAGE, oob.ecc1);
398
399 __nand_calculate_ecc(buf + boffset + SM_SMALL_PAGE,
400 SM_SMALL_PAGE, oob.ecc2);
401 }
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200402 if (!sm_write_sector(ftl, zone, block, boffset,
403 buf + boffset, &oob))
404 continue;
405
406 if (!retry) {
407
408 /* If write fails. try to erase the block */
409 /* This is safe, because we never write in blocks
410 that contain valuable data.
411 This is intended to repair block that are marked
412 as erased, but that isn't fully erased*/
413
414 if (sm_erase_block(ftl, zone, block, 0))
415 return -EIO;
416
417 retry = 1;
418 goto restart;
419 } else {
420 sm_mark_block_bad(ftl, zone, block);
421 return -EIO;
422 }
423 }
424 return 0;
425}
426
427
428/* Mark whole block at offset 'offs' as bad. */
429static void sm_mark_block_bad(struct sm_ftl *ftl, int zone, int block)
430{
431 struct sm_oob oob;
432 int boffset;
433
434 memset(&oob, 0xFF, SM_OOB_SIZE);
435 oob.block_status = 0xF0;
436
437 if (ftl->unstable)
438 return;
439
440 if (sm_recheck_media(ftl))
441 return;
442
443 sm_printk("marking block %d of zone %d as bad", block, zone);
444
445 /* We aren't checking the return value, because we don't care */
446 /* This also fails on fake xD cards, but I guess these won't expose
Brian Norris92394b52011-07-20 09:53:42 -0700447 any bad blocks till fail completely */
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200448 for (boffset = 0; boffset < ftl->block_size; boffset += SM_SECTOR_SIZE)
449 sm_write_sector(ftl, zone, block, boffset, NULL, &oob);
450}
451
452/*
453 * Erase a block within a zone
Brian Norris92394b52011-07-20 09:53:42 -0700454 * If erase succeeds, it updates free block fifo, otherwise marks block as bad
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200455 */
456static int sm_erase_block(struct sm_ftl *ftl, int zone_num, uint16_t block,
457 int put_free)
458{
459 struct ftl_zone *zone = &ftl->zones[zone_num];
460 struct mtd_info *mtd = ftl->trans->mtd;
461 struct erase_info erase;
462
463 erase.mtd = mtd;
464 erase.callback = sm_erase_callback;
465 erase.addr = sm_mkoffset(ftl, zone_num, block, 0);
466 erase.len = ftl->block_size;
467 erase.priv = (u_long)ftl;
468
469 if (ftl->unstable)
470 return -EIO;
471
472 BUG_ON(ftl->readonly);
473
474 if (zone_num == 0 && (block == ftl->cis_block || block == 0)) {
475 sm_printk("attempted to erase the CIS!");
476 return -EIO;
477 }
478
Artem Bityutskiy7e1f0dc2011-12-23 15:25:39 +0200479 if (mtd_erase(mtd, &erase)) {
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200480 sm_printk("erase of block %d in zone %d failed",
481 block, zone_num);
482 goto error;
483 }
484
485 if (erase.state == MTD_ERASE_PENDING)
486 wait_for_completion(&ftl->erase_completion);
487
488 if (erase.state != MTD_ERASE_DONE) {
489 sm_printk("erase of block %d in zone %d failed after wait",
490 block, zone_num);
491 goto error;
492 }
493
494 if (put_free)
495 kfifo_in(&zone->free_sectors,
496 (const unsigned char *)&block, sizeof(block));
497
498 return 0;
499error:
500 sm_mark_block_bad(ftl, zone_num, block);
501 return -EIO;
502}
503
504static void sm_erase_callback(struct erase_info *self)
505{
506 struct sm_ftl *ftl = (struct sm_ftl *)self->priv;
507 complete(&ftl->erase_completion);
508}
509
Brian Norris92394b52011-07-20 09:53:42 -0700510/* Thoroughly test that block is valid. */
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200511static int sm_check_block(struct sm_ftl *ftl, int zone, int block)
512{
513 int boffset;
514 struct sm_oob oob;
515 int lbas[] = { -3, 0, 0, 0 };
516 int i = 0;
517 int test_lba;
518
519
520 /* First just check that block doesn't look fishy */
521 /* Only blocks that are valid or are sliced in two parts, are
522 accepted */
523 for (boffset = 0; boffset < ftl->block_size;
524 boffset += SM_SECTOR_SIZE) {
525
Brian Norris92394b52011-07-20 09:53:42 -0700526 /* This shouldn't happen anyway */
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200527 if (sm_read_sector(ftl, zone, block, boffset, NULL, &oob))
528 return -2;
529
530 test_lba = sm_read_lba(&oob);
531
532 if (lbas[i] != test_lba)
533 lbas[++i] = test_lba;
534
535 /* If we found three different LBAs, something is fishy */
536 if (i == 3)
537 return -EIO;
538 }
539
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300540 /* If the block is sliced (partially erased usually) erase it */
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200541 if (i == 2) {
542 sm_erase_block(ftl, zone, block, 1);
543 return 1;
544 }
545
546 return 0;
547}
548
549/* ----------------- media scanning --------------------------------- */
550static const struct chs_entry chs_table[] = {
551 { 1, 125, 4, 4 },
552 { 2, 125, 4, 8 },
553 { 4, 250, 4, 8 },
554 { 8, 250, 4, 16 },
555 { 16, 500, 4, 16 },
556 { 32, 500, 8, 16 },
557 { 64, 500, 8, 32 },
558 { 128, 500, 16, 32 },
559 { 256, 1000, 16, 32 },
560 { 512, 1015, 32, 63 },
561 { 1024, 985, 33, 63 },
562 { 2048, 985, 33, 63 },
563 { 0 },
564};
565
566
567static const uint8_t cis_signature[] = {
568 0x01, 0x03, 0xD9, 0x01, 0xFF, 0x18, 0x02, 0xDF, 0x01, 0x20
569};
570/* Find out media parameters.
571 * This ideally has to be based on nand id, but for now device size is enough */
572int sm_get_media_info(struct sm_ftl *ftl, struct mtd_info *mtd)
573{
574 int i;
575 int size_in_megs = mtd->size / (1024 * 1024);
576
577 ftl->readonly = mtd->type == MTD_ROM;
578
579 /* Manual settings for very old devices */
580 ftl->zone_count = 1;
581 ftl->smallpagenand = 0;
582
583 switch (size_in_megs) {
584 case 1:
585 /* 1 MiB flash/rom SmartMedia card (256 byte pages)*/
586 ftl->zone_size = 256;
587 ftl->max_lba = 250;
588 ftl->block_size = 8 * SM_SECTOR_SIZE;
589 ftl->smallpagenand = 1;
590
591 break;
592 case 2:
593 /* 2 MiB flash SmartMedia (256 byte pages)*/
594 if (mtd->writesize == SM_SMALL_PAGE) {
595 ftl->zone_size = 512;
596 ftl->max_lba = 500;
597 ftl->block_size = 8 * SM_SECTOR_SIZE;
598 ftl->smallpagenand = 1;
599 /* 2 MiB rom SmartMedia */
600 } else {
601
602 if (!ftl->readonly)
603 return -ENODEV;
604
605 ftl->zone_size = 256;
606 ftl->max_lba = 250;
607 ftl->block_size = 16 * SM_SECTOR_SIZE;
608 }
609 break;
610 case 4:
611 /* 4 MiB flash/rom SmartMedia device */
612 ftl->zone_size = 512;
613 ftl->max_lba = 500;
614 ftl->block_size = 16 * SM_SECTOR_SIZE;
615 break;
616 case 8:
617 /* 8 MiB flash/rom SmartMedia device */
618 ftl->zone_size = 1024;
619 ftl->max_lba = 1000;
620 ftl->block_size = 16 * SM_SECTOR_SIZE;
621 }
622
623 /* Minimum xD size is 16MiB. Also, all xD cards have standard zone
624 sizes. SmartMedia cards exist up to 128 MiB and have same layout*/
625 if (size_in_megs >= 16) {
626 ftl->zone_count = size_in_megs / 16;
627 ftl->zone_size = 1024;
628 ftl->max_lba = 1000;
629 ftl->block_size = 32 * SM_SECTOR_SIZE;
630 }
631
632 /* Test for proper write,erase and oob sizes */
633 if (mtd->erasesize > ftl->block_size)
634 return -ENODEV;
635
636 if (mtd->writesize > SM_SECTOR_SIZE)
637 return -ENODEV;
638
639 if (ftl->smallpagenand && mtd->oobsize < SM_SMALL_OOB_SIZE)
640 return -ENODEV;
641
642 if (!ftl->smallpagenand && mtd->oobsize < SM_OOB_SIZE)
643 return -ENODEV;
644
Artem Bityutskiyfc002e32011-12-28 18:35:07 +0200645 /* We use OOB */
646 if (!mtd_has_oob(mtd))
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200647 return -ENODEV;
648
649 /* Find geometry information */
650 for (i = 0 ; i < ARRAY_SIZE(chs_table) ; i++) {
651 if (chs_table[i].size == size_in_megs) {
652 ftl->cylinders = chs_table[i].cyl;
653 ftl->heads = chs_table[i].head;
654 ftl->sectors = chs_table[i].sec;
655 return 0;
656 }
657 }
658
659 sm_printk("media has unknown size : %dMiB", size_in_megs);
660 ftl->cylinders = 985;
661 ftl->heads = 33;
662 ftl->sectors = 63;
663 return 0;
664}
665
666/* Validate the CIS */
667static int sm_read_cis(struct sm_ftl *ftl)
668{
669 struct sm_oob oob;
670
671 if (sm_read_sector(ftl,
672 0, ftl->cis_block, ftl->cis_boffset, ftl->cis_buffer, &oob))
673 return -EIO;
674
675 if (!sm_sector_valid(&oob) || !sm_block_valid(&oob))
676 return -EIO;
677
678 if (!memcmp(ftl->cis_buffer + ftl->cis_page_offset,
679 cis_signature, sizeof(cis_signature))) {
680 return 0;
681 }
682
683 return -EIO;
684}
685
686/* Scan the media for the CIS */
687static int sm_find_cis(struct sm_ftl *ftl)
688{
689 struct sm_oob oob;
690 int block, boffset;
691 int block_found = 0;
692 int cis_found = 0;
693
694 /* Search for first valid block */
695 for (block = 0 ; block < ftl->zone_size - ftl->max_lba ; block++) {
696
697 if (sm_read_sector(ftl, 0, block, 0, NULL, &oob))
698 continue;
699
700 if (!sm_block_valid(&oob))
701 continue;
702 block_found = 1;
703 break;
704 }
705
706 if (!block_found)
707 return -EIO;
708
709 /* Search for first valid sector in this block */
710 for (boffset = 0 ; boffset < ftl->block_size;
711 boffset += SM_SECTOR_SIZE) {
712
713 if (sm_read_sector(ftl, 0, block, boffset, NULL, &oob))
714 continue;
715
716 if (!sm_sector_valid(&oob))
717 continue;
718 break;
719 }
720
721 if (boffset == ftl->block_size)
722 return -EIO;
723
724 ftl->cis_block = block;
725 ftl->cis_boffset = boffset;
726 ftl->cis_page_offset = 0;
727
728 cis_found = !sm_read_cis(ftl);
729
730 if (!cis_found) {
731 ftl->cis_page_offset = SM_SMALL_PAGE;
732 cis_found = !sm_read_cis(ftl);
733 }
734
735 if (cis_found) {
736 dbg("CIS block found at offset %x",
737 block * ftl->block_size +
738 boffset + ftl->cis_page_offset);
739 return 0;
740 }
741 return -EIO;
742}
743
744/* Basic test to determine if underlying mtd device if functional */
745static int sm_recheck_media(struct sm_ftl *ftl)
746{
747 if (sm_read_cis(ftl)) {
748
749 if (!ftl->unstable) {
750 sm_printk("media unstable, not allowing writes");
751 ftl->unstable = 1;
752 }
753 return -EIO;
754 }
755 return 0;
756}
757
758/* Initialize a FTL zone */
759static int sm_init_zone(struct sm_ftl *ftl, int zone_num)
760{
761 struct ftl_zone *zone = &ftl->zones[zone_num];
762 struct sm_oob oob;
763 uint16_t block;
764 int lba;
765 int i = 0;
Maxim Levitsky133fa8c2010-02-26 22:08:40 +0200766 int len;
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200767
768 dbg("initializing zone %d", zone_num);
769
770 /* Allocate memory for FTL table */
771 zone->lba_to_phys_table = kmalloc(ftl->max_lba * 2, GFP_KERNEL);
772
773 if (!zone->lba_to_phys_table)
774 return -ENOMEM;
775 memset(zone->lba_to_phys_table, -1, ftl->max_lba * 2);
776
777
778 /* Allocate memory for free sectors FIFO */
779 if (kfifo_alloc(&zone->free_sectors, ftl->zone_size * 2, GFP_KERNEL)) {
780 kfree(zone->lba_to_phys_table);
781 return -ENOMEM;
782 }
783
784 /* Now scan the zone */
785 for (block = 0 ; block < ftl->zone_size ; block++) {
786
787 /* Skip blocks till the CIS (including) */
788 if (zone_num == 0 && block <= ftl->cis_block)
789 continue;
790
791 /* Read the oob of first sector */
792 if (sm_read_sector(ftl, zone_num, block, 0, NULL, &oob))
793 return -EIO;
794
795 /* Test to see if block is erased. It is enough to test
796 first sector, because erase happens in one shot */
797 if (sm_block_erased(&oob)) {
798 kfifo_in(&zone->free_sectors,
799 (unsigned char *)&block, 2);
800 continue;
801 }
802
803 /* If block is marked as bad, skip it */
804 /* This assumes we can trust first sector*/
805 /* However the way the block valid status is defined, ensures
806 very low probability of failure here */
807 if (!sm_block_valid(&oob)) {
808 dbg("PH %04d <-> <marked bad>", block);
809 continue;
810 }
811
812
813 lba = sm_read_lba(&oob);
814
815 /* Invalid LBA means that block is damaged. */
816 /* We can try to erase it, or mark it as bad, but
817 lets leave that to recovery application */
818 if (lba == -2 || lba >= ftl->max_lba) {
819 dbg("PH %04d <-> LBA %04d(bad)", block, lba);
820 continue;
821 }
822
823
824 /* If there is no collision,
825 just put the sector in the FTL table */
826 if (zone->lba_to_phys_table[lba] < 0) {
827 dbg_verbose("PH %04d <-> LBA %04d", block, lba);
828 zone->lba_to_phys_table[lba] = block;
829 continue;
830 }
831
832 sm_printk("collision"
833 " of LBA %d between blocks %d and %d in zone %d",
834 lba, zone->lba_to_phys_table[lba], block, zone_num);
835
836 /* Test that this block is valid*/
837 if (sm_check_block(ftl, zone_num, block))
838 continue;
839
840 /* Test now the old block */
841 if (sm_check_block(ftl, zone_num,
842 zone->lba_to_phys_table[lba])) {
843 zone->lba_to_phys_table[lba] = block;
844 continue;
845 }
846
847 /* If both blocks are valid and share same LBA, it means that
848 they hold different versions of same data. It not
849 known which is more recent, thus just erase one of them
850 */
851 sm_printk("both blocks are valid, erasing the later");
852 sm_erase_block(ftl, zone_num, block, 1);
853 }
854
855 dbg("zone initialized");
856 zone->initialized = 1;
857
858 /* No free sectors, means that the zone is heavily damaged, write won't
859 work, but it can still can be (partially) read */
860 if (!kfifo_len(&zone->free_sectors)) {
861 sm_printk("no free blocks in zone %d", zone_num);
862 return 0;
863 }
864
865 /* Randomize first block we write to */
866 get_random_bytes(&i, 2);
867 i %= (kfifo_len(&zone->free_sectors) / 2);
868
869 while (i--) {
Maxim Levitsky133fa8c2010-02-26 22:08:40 +0200870 len = kfifo_out(&zone->free_sectors,
871 (unsigned char *)&block, 2);
872 WARN_ON(len != 2);
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200873 kfifo_in(&zone->free_sectors, (const unsigned char *)&block, 2);
874 }
875 return 0;
876}
877
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300878/* Get and automatically initialize an FTL mapping for one zone */
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200879struct ftl_zone *sm_get_zone(struct sm_ftl *ftl, int zone_num)
880{
881 struct ftl_zone *zone;
882 int error;
883
884 BUG_ON(zone_num >= ftl->zone_count);
885 zone = &ftl->zones[zone_num];
886
887 if (!zone->initialized) {
888 error = sm_init_zone(ftl, zone_num);
889
890 if (error)
891 return ERR_PTR(error);
892 }
893 return zone;
894}
895
896
897/* ----------------- cache handling ------------------------------------------*/
898
899/* Initialize the one block cache */
900void sm_cache_init(struct sm_ftl *ftl)
901{
902 ftl->cache_data_invalid_bitmap = 0xFFFFFFFF;
903 ftl->cache_clean = 1;
904 ftl->cache_zone = -1;
905 ftl->cache_block = -1;
906 /*memset(ftl->cache_data, 0xAA, ftl->block_size);*/
907}
908
909/* Put sector in one block cache */
910void sm_cache_put(struct sm_ftl *ftl, char *buffer, int boffset)
911{
912 memcpy(ftl->cache_data + boffset, buffer, SM_SECTOR_SIZE);
913 clear_bit(boffset / SM_SECTOR_SIZE, &ftl->cache_data_invalid_bitmap);
914 ftl->cache_clean = 0;
915}
916
917/* Read a sector from the cache */
918int sm_cache_get(struct sm_ftl *ftl, char *buffer, int boffset)
919{
920 if (test_bit(boffset / SM_SECTOR_SIZE,
921 &ftl->cache_data_invalid_bitmap))
922 return -1;
923
924 memcpy(buffer, ftl->cache_data + boffset, SM_SECTOR_SIZE);
925 return 0;
926}
927
928/* Write the cache to hardware */
929int sm_cache_flush(struct sm_ftl *ftl)
930{
931 struct ftl_zone *zone;
932
933 int sector_num;
934 uint16_t write_sector;
935 int zone_num = ftl->cache_zone;
936 int block_num;
937
938 if (ftl->cache_clean)
939 return 0;
940
941 if (ftl->unstable)
942 return -EIO;
943
944 BUG_ON(zone_num < 0);
945 zone = &ftl->zones[zone_num];
946 block_num = zone->lba_to_phys_table[ftl->cache_block];
947
948
949 /* Try to read all unread areas of the cache block*/
Akinobu Mitafed457a2010-04-05 13:53:35 -0700950 for_each_set_bit(sector_num, &ftl->cache_data_invalid_bitmap,
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200951 ftl->block_size / SM_SECTOR_SIZE) {
952
953 if (!sm_read_sector(ftl,
954 zone_num, block_num, sector_num * SM_SECTOR_SIZE,
955 ftl->cache_data + sector_num * SM_SECTOR_SIZE, NULL))
956 clear_bit(sector_num,
957 &ftl->cache_data_invalid_bitmap);
958 }
959restart:
960
961 if (ftl->unstable)
962 return -EIO;
Maxim Levitsky133fa8c2010-02-26 22:08:40 +0200963
964 /* If there are no spare blocks, */
965 /* we could still continue by erasing/writing the current block,
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200966 but for such worn out media it doesn't worth the trouble,
967 and the dangers */
Maxim Levitsky133fa8c2010-02-26 22:08:40 +0200968 if (kfifo_out(&zone->free_sectors,
969 (unsigned char *)&write_sector, 2) != 2) {
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200970 dbg("no free sectors for write!");
971 return -EIO;
972 }
973
Maxim Levitsky7d17c022010-02-22 20:39:41 +0200974
975 if (sm_write_block(ftl, ftl->cache_data, zone_num, write_sector,
976 ftl->cache_block, ftl->cache_data_invalid_bitmap))
977 goto restart;
978
979 /* Update the FTL table */
980 zone->lba_to_phys_table[ftl->cache_block] = write_sector;
981
982 /* Write succesfull, so erase and free the old block */
983 if (block_num > 0)
984 sm_erase_block(ftl, zone_num, block_num, 1);
985
986 sm_cache_init(ftl);
987 return 0;
988}
989
990
991/* flush timer, runs a second after last write */
992static void sm_cache_flush_timer(unsigned long data)
993{
994 struct sm_ftl *ftl = (struct sm_ftl *)data;
995 queue_work(cache_flush_workqueue, &ftl->flush_work);
996}
997
998/* cache flush work, kicked by timer */
999static void sm_cache_flush_work(struct work_struct *work)
1000{
1001 struct sm_ftl *ftl = container_of(work, struct sm_ftl, flush_work);
1002 mutex_lock(&ftl->mutex);
1003 sm_cache_flush(ftl);
1004 mutex_unlock(&ftl->mutex);
1005 return;
1006}
1007
1008/* ---------------- outside interface -------------------------------------- */
1009
1010/* outside interface: read a sector */
1011static int sm_read(struct mtd_blktrans_dev *dev,
1012 unsigned long sect_no, char *buf)
1013{
1014 struct sm_ftl *ftl = dev->priv;
1015 struct ftl_zone *zone;
1016 int error = 0, in_cache = 0;
1017 int zone_num, block, boffset;
1018
1019 sm_break_offset(ftl, sect_no << 9, &zone_num, &block, &boffset);
1020 mutex_lock(&ftl->mutex);
1021
1022
1023 zone = sm_get_zone(ftl, zone_num);
1024 if (IS_ERR(zone)) {
1025 error = PTR_ERR(zone);
1026 goto unlock;
1027 }
1028
1029 /* Have to look at cache first */
1030 if (ftl->cache_zone == zone_num && ftl->cache_block == block) {
1031 in_cache = 1;
1032 if (!sm_cache_get(ftl, buf, boffset))
1033 goto unlock;
1034 }
1035
1036 /* Translate the block and return if doesn't exist in the table */
1037 block = zone->lba_to_phys_table[block];
1038
1039 if (block == -1) {
1040 memset(buf, 0xFF, SM_SECTOR_SIZE);
1041 goto unlock;
1042 }
1043
1044 if (sm_read_sector(ftl, zone_num, block, boffset, buf, NULL)) {
1045 error = -EIO;
1046 goto unlock;
1047 }
1048
1049 if (in_cache)
1050 sm_cache_put(ftl, buf, boffset);
1051unlock:
1052 mutex_unlock(&ftl->mutex);
1053 return error;
1054}
1055
1056/* outside interface: write a sector */
1057static int sm_write(struct mtd_blktrans_dev *dev,
1058 unsigned long sec_no, char *buf)
1059{
1060 struct sm_ftl *ftl = dev->priv;
1061 struct ftl_zone *zone;
1062 int error, zone_num, block, boffset;
1063
1064 BUG_ON(ftl->readonly);
1065 sm_break_offset(ftl, sec_no << 9, &zone_num, &block, &boffset);
1066
1067 /* No need in flush thread running now */
1068 del_timer(&ftl->timer);
1069 mutex_lock(&ftl->mutex);
1070
1071 zone = sm_get_zone(ftl, zone_num);
1072 if (IS_ERR(zone)) {
1073 error = PTR_ERR(zone);
1074 goto unlock;
1075 }
1076
1077 /* If entry is not in cache, flush it */
1078 if (ftl->cache_block != block || ftl->cache_zone != zone_num) {
1079
1080 error = sm_cache_flush(ftl);
1081 if (error)
1082 goto unlock;
1083
1084 ftl->cache_block = block;
1085 ftl->cache_zone = zone_num;
1086 }
1087
1088 sm_cache_put(ftl, buf, boffset);
1089unlock:
1090 mod_timer(&ftl->timer, jiffies + msecs_to_jiffies(cache_timeout));
1091 mutex_unlock(&ftl->mutex);
1092 return error;
1093}
1094
1095/* outside interface: flush everything */
1096static int sm_flush(struct mtd_blktrans_dev *dev)
1097{
1098 struct sm_ftl *ftl = dev->priv;
1099 int retval;
1100
1101 mutex_lock(&ftl->mutex);
1102 retval = sm_cache_flush(ftl);
1103 mutex_unlock(&ftl->mutex);
1104 return retval;
1105}
1106
1107/* outside interface: device is released */
1108static int sm_release(struct mtd_blktrans_dev *dev)
1109{
1110 struct sm_ftl *ftl = dev->priv;
1111
1112 mutex_lock(&ftl->mutex);
1113 del_timer_sync(&ftl->timer);
1114 cancel_work_sync(&ftl->flush_work);
1115 sm_cache_flush(ftl);
1116 mutex_unlock(&ftl->mutex);
1117 return 0;
1118}
1119
1120/* outside interface: get geometry */
1121static int sm_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
1122{
1123 struct sm_ftl *ftl = dev->priv;
1124 geo->heads = ftl->heads;
1125 geo->sectors = ftl->sectors;
1126 geo->cylinders = ftl->cylinders;
1127 return 0;
1128}
1129
1130/* external interface: main initialization function */
1131static void sm_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
1132{
1133 struct mtd_blktrans_dev *trans;
1134 struct sm_ftl *ftl;
1135
1136 /* Allocate & initialize our private structure */
1137 ftl = kzalloc(sizeof(struct sm_ftl), GFP_KERNEL);
1138 if (!ftl)
1139 goto error1;
1140
1141
1142 mutex_init(&ftl->mutex);
1143 setup_timer(&ftl->timer, sm_cache_flush_timer, (unsigned long)ftl);
1144 INIT_WORK(&ftl->flush_work, sm_cache_flush_work);
1145 init_completion(&ftl->erase_completion);
1146
1147 /* Read media information */
1148 if (sm_get_media_info(ftl, mtd)) {
1149 dbg("found unsupported mtd device, aborting");
1150 goto error2;
1151 }
1152
1153
1154 /* Allocate temporary CIS buffer for read retry support */
1155 ftl->cis_buffer = kzalloc(SM_SECTOR_SIZE, GFP_KERNEL);
1156 if (!ftl->cis_buffer)
1157 goto error2;
1158
1159 /* Allocate zone array, it will be initialized on demand */
1160 ftl->zones = kzalloc(sizeof(struct ftl_zone) * ftl->zone_count,
1161 GFP_KERNEL);
1162 if (!ftl->zones)
1163 goto error3;
1164
1165 /* Allocate the cache*/
1166 ftl->cache_data = kzalloc(ftl->block_size, GFP_KERNEL);
1167
1168 if (!ftl->cache_data)
1169 goto error4;
1170
1171 sm_cache_init(ftl);
1172
1173
1174 /* Allocate upper layer structure and initialize it */
1175 trans = kzalloc(sizeof(struct mtd_blktrans_dev), GFP_KERNEL);
1176 if (!trans)
1177 goto error5;
1178
1179 ftl->trans = trans;
1180 trans->priv = ftl;
1181
1182 trans->tr = tr;
1183 trans->mtd = mtd;
1184 trans->devnum = -1;
1185 trans->size = (ftl->block_size * ftl->max_lba * ftl->zone_count) >> 9;
1186 trans->readonly = ftl->readonly;
1187
1188 if (sm_find_cis(ftl)) {
1189 dbg("CIS not found on mtd device, aborting");
1190 goto error6;
1191 }
1192
1193 ftl->disk_attributes = sm_create_sysfs_attributes(ftl);
Xiaochen Wang629286b92011-03-10 22:31:39 +08001194 if (!ftl->disk_attributes)
1195 goto error6;
Maxim Levitsky7d17c022010-02-22 20:39:41 +02001196 trans->disk_attributes = ftl->disk_attributes;
1197
1198 sm_printk("Found %d MiB xD/SmartMedia FTL on mtd%d",
1199 (int)(mtd->size / (1024 * 1024)), mtd->index);
1200
1201 dbg("FTL layout:");
1202 dbg("%d zone(s), each consists of %d blocks (+%d spares)",
1203 ftl->zone_count, ftl->max_lba,
1204 ftl->zone_size - ftl->max_lba);
1205 dbg("each block consists of %d bytes",
1206 ftl->block_size);
1207
1208
1209 /* Register device*/
1210 if (add_mtd_blktrans_dev(trans)) {
1211 dbg("error in mtdblktrans layer");
1212 goto error6;
1213 }
1214 return;
1215error6:
1216 kfree(trans);
1217error5:
1218 kfree(ftl->cache_data);
1219error4:
1220 kfree(ftl->zones);
1221error3:
1222 kfree(ftl->cis_buffer);
1223error2:
1224 kfree(ftl);
1225error1:
1226 return;
1227}
1228
1229/* main interface: device {surprise,} removal */
1230static void sm_remove_dev(struct mtd_blktrans_dev *dev)
1231{
1232 struct sm_ftl *ftl = dev->priv;
1233 int i;
1234
1235 del_mtd_blktrans_dev(dev);
1236 ftl->trans = NULL;
1237
1238 for (i = 0 ; i < ftl->zone_count; i++) {
1239
1240 if (!ftl->zones[i].initialized)
1241 continue;
1242
1243 kfree(ftl->zones[i].lba_to_phys_table);
1244 kfifo_free(&ftl->zones[i].free_sectors);
1245 }
1246
1247 sm_delete_sysfs_attributes(ftl);
1248 kfree(ftl->cis_buffer);
1249 kfree(ftl->zones);
1250 kfree(ftl->cache_data);
1251 kfree(ftl);
1252}
1253
1254static struct mtd_blktrans_ops sm_ftl_ops = {
1255 .name = "smblk",
Maxim Levitsky452380e2012-03-17 20:16:53 +02001256 .major = 0,
Maxim Levitsky7d17c022010-02-22 20:39:41 +02001257 .part_bits = SM_FTL_PARTN_BITS,
1258 .blksize = SM_SECTOR_SIZE,
1259 .getgeo = sm_getgeo,
1260
1261 .add_mtd = sm_add_mtd,
1262 .remove_dev = sm_remove_dev,
1263
1264 .readsect = sm_read,
1265 .writesect = sm_write,
1266
1267 .flush = sm_flush,
1268 .release = sm_release,
1269
1270 .owner = THIS_MODULE,
1271};
1272
1273static __init int sm_module_init(void)
1274{
1275 int error = 0;
Tejun Heo58a69cb2011-02-16 09:25:31 +01001276 cache_flush_workqueue = create_freezable_workqueue("smflush");
Maxim Levitsky7d17c022010-02-22 20:39:41 +02001277
1278 if (IS_ERR(cache_flush_workqueue))
1279 return PTR_ERR(cache_flush_workqueue);
1280
1281 error = register_mtd_blktrans(&sm_ftl_ops);
1282 if (error)
1283 destroy_workqueue(cache_flush_workqueue);
1284 return error;
1285
1286}
1287
1288static void __exit sm_module_exit(void)
1289{
1290 destroy_workqueue(cache_flush_workqueue);
1291 deregister_mtd_blktrans(&sm_ftl_ops);
1292}
1293
1294module_init(sm_module_init);
1295module_exit(sm_module_exit);
1296
1297MODULE_LICENSE("GPL");
1298MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
1299MODULE_DESCRIPTION("Smartmedia/xD mtd translation layer");