Lines Matching refs:ftl
54 static struct attribute_group *sm_create_sysfs_attributes(struct sm_ftl *ftl) in sm_create_sysfs_attributes() argument
61 vendor = kstrndup(ftl->cis_buffer + SM_CIS_VENDOR_OFFSET, in sm_create_sysfs_attributes()
104 static void sm_delete_sysfs_attributes(struct sm_ftl *ftl) in sm_delete_sysfs_attributes() argument
106 struct attribute **attributes = ftl->disk_attributes->attrs; in sm_delete_sysfs_attributes()
122 kfree(ftl->disk_attributes->attrs); in sm_delete_sysfs_attributes()
123 kfree(ftl->disk_attributes); in sm_delete_sysfs_attributes()
192 static loff_t sm_mkoffset(struct sm_ftl *ftl, int zone, int block, int boffset) in sm_mkoffset() argument
195 WARN_ON(zone < 0 || zone >= ftl->zone_count); in sm_mkoffset()
196 WARN_ON(block >= ftl->zone_size); in sm_mkoffset()
197 WARN_ON(boffset >= ftl->block_size); in sm_mkoffset()
202 return (zone * SM_MAX_ZONE_SIZE + block) * ftl->block_size + boffset; in sm_mkoffset()
206 static void sm_break_offset(struct sm_ftl *ftl, loff_t loffset, in sm_break_offset() argument
210 *boffset = do_div(offset, ftl->block_size); in sm_break_offset()
211 *block = do_div(offset, ftl->max_lba); in sm_break_offset()
212 *zone = offset >= ftl->zone_count ? -1 : offset; in sm_break_offset()
238 static int sm_read_sector(struct sm_ftl *ftl, in sm_read_sector() argument
242 struct mtd_info *mtd = ftl->trans->mtd; in sm_read_sector()
258 ops.mode = ftl->smallpagenand ? MTD_OPS_RAW : MTD_OPS_PLACE_OOB; in sm_read_sector()
269 if (zone == 0 && block == ftl->cis_block && boffset == in sm_read_sector()
270 ftl->cis_boffset) in sm_read_sector()
274 if (try == 3 || sm_recheck_media(ftl)) in sm_read_sector()
280 ret = mtd_read_oob(mtd, sm_mkoffset(ftl, zone, block, boffset), &ops); in sm_read_sector()
309 (ftl->smallpagenand && sm_correct_sector(buffer, oob))) { in sm_read_sector()
320 static int sm_write_sector(struct sm_ftl *ftl, in sm_write_sector() argument
325 struct mtd_info *mtd = ftl->trans->mtd; in sm_write_sector()
328 BUG_ON(ftl->readonly); in sm_write_sector()
330 if (zone == 0 && (block == ftl->cis_block || block == 0)) { in sm_write_sector()
335 if (ftl->unstable) in sm_write_sector()
338 ops.mode = ftl->smallpagenand ? MTD_OPS_RAW : MTD_OPS_PLACE_OOB; in sm_write_sector()
345 ret = mtd_write_oob(mtd, sm_mkoffset(ftl, zone, block, boffset), &ops); in sm_write_sector()
353 sm_recheck_media(ftl); in sm_write_sector()
367 static int sm_write_block(struct sm_ftl *ftl, uint8_t *buf, in sm_write_block() argument
379 if (ftl->unstable) in sm_write_block()
382 for (boffset = 0; boffset < ftl->block_size; in sm_write_block()
396 if (ftl->smallpagenand) { in sm_write_block()
405 if (!sm_write_sector(ftl, zone, block, boffset, in sm_write_block()
417 if (sm_erase_block(ftl, zone, block, 0)) in sm_write_block()
423 sm_mark_block_bad(ftl, zone, block); in sm_write_block()
432 static void sm_mark_block_bad(struct sm_ftl *ftl, int zone, int block) in sm_mark_block_bad() argument
440 if (ftl->unstable) in sm_mark_block_bad()
443 if (sm_recheck_media(ftl)) in sm_mark_block_bad()
451 for (boffset = 0; boffset < ftl->block_size; boffset += SM_SECTOR_SIZE) in sm_mark_block_bad()
452 sm_write_sector(ftl, zone, block, boffset, NULL, &oob); in sm_mark_block_bad()
459 static int sm_erase_block(struct sm_ftl *ftl, int zone_num, uint16_t block, in sm_erase_block() argument
462 struct ftl_zone *zone = &ftl->zones[zone_num]; in sm_erase_block()
463 struct mtd_info *mtd = ftl->trans->mtd; in sm_erase_block()
466 erase.addr = sm_mkoffset(ftl, zone_num, block, 0); in sm_erase_block()
467 erase.len = ftl->block_size; in sm_erase_block()
469 if (ftl->unstable) in sm_erase_block()
472 BUG_ON(ftl->readonly); in sm_erase_block()
474 if (zone_num == 0 && (block == ftl->cis_block || block == 0)) { in sm_erase_block()
491 sm_mark_block_bad(ftl, zone_num, block); in sm_erase_block()
496 static int sm_check_block(struct sm_ftl *ftl, int zone, int block) in sm_check_block() argument
508 for (boffset = 0; boffset < ftl->block_size; in sm_check_block()
512 if (sm_read_sector(ftl, zone, block, boffset, NULL, &oob)) in sm_check_block()
527 sm_erase_block(ftl, zone, block, 1); in sm_check_block()
557 static int sm_get_media_info(struct sm_ftl *ftl, struct mtd_info *mtd) in sm_get_media_info() argument
562 ftl->readonly = mtd->type == MTD_ROM; in sm_get_media_info()
565 ftl->zone_count = 1; in sm_get_media_info()
566 ftl->smallpagenand = 0; in sm_get_media_info()
571 ftl->zone_size = 256; in sm_get_media_info()
572 ftl->max_lba = 250; in sm_get_media_info()
573 ftl->block_size = 8 * SM_SECTOR_SIZE; in sm_get_media_info()
574 ftl->smallpagenand = 1; in sm_get_media_info()
580 ftl->zone_size = 512; in sm_get_media_info()
581 ftl->max_lba = 500; in sm_get_media_info()
582 ftl->block_size = 8 * SM_SECTOR_SIZE; in sm_get_media_info()
583 ftl->smallpagenand = 1; in sm_get_media_info()
587 if (!ftl->readonly) in sm_get_media_info()
590 ftl->zone_size = 256; in sm_get_media_info()
591 ftl->max_lba = 250; in sm_get_media_info()
592 ftl->block_size = 16 * SM_SECTOR_SIZE; in sm_get_media_info()
597 ftl->zone_size = 512; in sm_get_media_info()
598 ftl->max_lba = 500; in sm_get_media_info()
599 ftl->block_size = 16 * SM_SECTOR_SIZE; in sm_get_media_info()
603 ftl->zone_size = 1024; in sm_get_media_info()
604 ftl->max_lba = 1000; in sm_get_media_info()
605 ftl->block_size = 16 * SM_SECTOR_SIZE; in sm_get_media_info()
611 ftl->zone_count = size_in_megs / 16; in sm_get_media_info()
612 ftl->zone_size = 1024; in sm_get_media_info()
613 ftl->max_lba = 1000; in sm_get_media_info()
614 ftl->block_size = 32 * SM_SECTOR_SIZE; in sm_get_media_info()
618 if (mtd->erasesize > ftl->block_size) in sm_get_media_info()
624 if (ftl->smallpagenand && mtd->oobsize < SM_SMALL_OOB_SIZE) in sm_get_media_info()
627 if (!ftl->smallpagenand && mtd->oobsize < SM_OOB_SIZE) in sm_get_media_info()
637 ftl->cylinders = chs_table[i].cyl; in sm_get_media_info()
638 ftl->heads = chs_table[i].head; in sm_get_media_info()
639 ftl->sectors = chs_table[i].sec; in sm_get_media_info()
645 ftl->cylinders = 985; in sm_get_media_info()
646 ftl->heads = 33; in sm_get_media_info()
647 ftl->sectors = 63; in sm_get_media_info()
652 static int sm_read_cis(struct sm_ftl *ftl) in sm_read_cis() argument
656 if (sm_read_sector(ftl, in sm_read_cis()
657 0, ftl->cis_block, ftl->cis_boffset, ftl->cis_buffer, &oob)) in sm_read_cis()
663 if (!memcmp(ftl->cis_buffer + ftl->cis_page_offset, in sm_read_cis()
672 static int sm_find_cis(struct sm_ftl *ftl) in sm_find_cis() argument
680 for (block = 0 ; block < ftl->zone_size - ftl->max_lba ; block++) { in sm_find_cis()
682 if (sm_read_sector(ftl, 0, block, 0, NULL, &oob)) in sm_find_cis()
695 for (boffset = 0 ; boffset < ftl->block_size; in sm_find_cis()
698 if (sm_read_sector(ftl, 0, block, boffset, NULL, &oob)) in sm_find_cis()
706 if (boffset == ftl->block_size) in sm_find_cis()
709 ftl->cis_block = block; in sm_find_cis()
710 ftl->cis_boffset = boffset; in sm_find_cis()
711 ftl->cis_page_offset = 0; in sm_find_cis()
713 cis_found = !sm_read_cis(ftl); in sm_find_cis()
716 ftl->cis_page_offset = SM_SMALL_PAGE; in sm_find_cis()
717 cis_found = !sm_read_cis(ftl); in sm_find_cis()
722 block * ftl->block_size + in sm_find_cis()
723 boffset + ftl->cis_page_offset); in sm_find_cis()
730 static int sm_recheck_media(struct sm_ftl *ftl) in sm_recheck_media() argument
732 if (sm_read_cis(ftl)) { in sm_recheck_media()
734 if (!ftl->unstable) { in sm_recheck_media()
736 ftl->unstable = 1; in sm_recheck_media()
744 static int sm_init_zone(struct sm_ftl *ftl, int zone_num) in sm_init_zone() argument
746 struct ftl_zone *zone = &ftl->zones[zone_num]; in sm_init_zone()
756 zone->lba_to_phys_table = kmalloc_array(ftl->max_lba, 2, GFP_KERNEL); in sm_init_zone()
760 memset(zone->lba_to_phys_table, -1, ftl->max_lba * 2); in sm_init_zone()
764 if (kfifo_alloc(&zone->free_sectors, ftl->zone_size * 2, GFP_KERNEL)) { in sm_init_zone()
770 for (block = 0 ; block < ftl->zone_size ; block++) { in sm_init_zone()
773 if (zone_num == 0 && block <= ftl->cis_block) in sm_init_zone()
777 if (sm_read_sector(ftl, zone_num, block, 0, NULL, &oob)) { in sm_init_zone()
806 if (lba == -2 || lba >= ftl->max_lba) { in sm_init_zone()
825 if (sm_check_block(ftl, zone_num, block)) in sm_init_zone()
829 if (sm_check_block(ftl, zone_num, in sm_init_zone()
840 sm_erase_block(ftl, zone_num, block, 1); in sm_init_zone()
867 static struct ftl_zone *sm_get_zone(struct sm_ftl *ftl, int zone_num) in sm_get_zone() argument
872 BUG_ON(zone_num >= ftl->zone_count); in sm_get_zone()
873 zone = &ftl->zones[zone_num]; in sm_get_zone()
876 error = sm_init_zone(ftl, zone_num); in sm_get_zone()
888 static void sm_cache_init(struct sm_ftl *ftl) in sm_cache_init() argument
890 ftl->cache_data_invalid_bitmap = 0xFFFFFFFF; in sm_cache_init()
891 ftl->cache_clean = 1; in sm_cache_init()
892 ftl->cache_zone = -1; in sm_cache_init()
893 ftl->cache_block = -1; in sm_cache_init()
898 static void sm_cache_put(struct sm_ftl *ftl, char *buffer, int boffset) in sm_cache_put() argument
900 memcpy(ftl->cache_data + boffset, buffer, SM_SECTOR_SIZE); in sm_cache_put()
901 clear_bit(boffset / SM_SECTOR_SIZE, &ftl->cache_data_invalid_bitmap); in sm_cache_put()
902 ftl->cache_clean = 0; in sm_cache_put()
906 static int sm_cache_get(struct sm_ftl *ftl, char *buffer, int boffset) in sm_cache_get() argument
909 &ftl->cache_data_invalid_bitmap)) in sm_cache_get()
912 memcpy(buffer, ftl->cache_data + boffset, SM_SECTOR_SIZE); in sm_cache_get()
917 static int sm_cache_flush(struct sm_ftl *ftl) in sm_cache_flush() argument
923 int zone_num = ftl->cache_zone; in sm_cache_flush()
926 if (ftl->cache_clean) in sm_cache_flush()
929 if (ftl->unstable) in sm_cache_flush()
933 zone = &ftl->zones[zone_num]; in sm_cache_flush()
934 block_num = zone->lba_to_phys_table[ftl->cache_block]; in sm_cache_flush()
938 for_each_set_bit(sector_num, &ftl->cache_data_invalid_bitmap, in sm_cache_flush()
939 ftl->block_size / SM_SECTOR_SIZE) { in sm_cache_flush()
941 if (!sm_read_sector(ftl, in sm_cache_flush()
943 ftl->cache_data + sector_num * SM_SECTOR_SIZE, NULL)) in sm_cache_flush()
945 &ftl->cache_data_invalid_bitmap); in sm_cache_flush()
949 if (ftl->unstable) in sm_cache_flush()
963 if (sm_write_block(ftl, ftl->cache_data, zone_num, write_sector, in sm_cache_flush()
964 ftl->cache_block, ftl->cache_data_invalid_bitmap)) in sm_cache_flush()
968 zone->lba_to_phys_table[ftl->cache_block] = write_sector; in sm_cache_flush()
972 sm_erase_block(ftl, zone_num, block_num, 1); in sm_cache_flush()
974 sm_cache_init(ftl); in sm_cache_flush()
982 struct sm_ftl *ftl = from_timer(ftl, t, timer); in sm_cache_flush_timer() local
983 queue_work(cache_flush_workqueue, &ftl->flush_work); in sm_cache_flush_timer()
989 struct sm_ftl *ftl = container_of(work, struct sm_ftl, flush_work); in sm_cache_flush_work() local
990 mutex_lock(&ftl->mutex); in sm_cache_flush_work()
991 sm_cache_flush(ftl); in sm_cache_flush_work()
992 mutex_unlock(&ftl->mutex); in sm_cache_flush_work()
1002 struct sm_ftl *ftl = dev->priv; in sm_read() local
1007 sm_break_offset(ftl, sect_no << 9, &zone_num, &block, &boffset); in sm_read()
1008 mutex_lock(&ftl->mutex); in sm_read()
1011 zone = sm_get_zone(ftl, zone_num); in sm_read()
1018 if (ftl->cache_zone == zone_num && ftl->cache_block == block) { in sm_read()
1020 if (!sm_cache_get(ftl, buf, boffset)) in sm_read()
1032 if (sm_read_sector(ftl, zone_num, block, boffset, buf, NULL)) { in sm_read()
1038 sm_cache_put(ftl, buf, boffset); in sm_read()
1040 mutex_unlock(&ftl->mutex); in sm_read()
1048 struct sm_ftl *ftl = dev->priv; in sm_write() local
1052 BUG_ON(ftl->readonly); in sm_write()
1053 sm_break_offset(ftl, sec_no << 9, &zone_num, &block, &boffset); in sm_write()
1056 del_timer(&ftl->timer); in sm_write()
1057 mutex_lock(&ftl->mutex); in sm_write()
1059 zone = sm_get_zone(ftl, zone_num); in sm_write()
1066 if (ftl->cache_block != block || ftl->cache_zone != zone_num) { in sm_write()
1068 error = sm_cache_flush(ftl); in sm_write()
1072 ftl->cache_block = block; in sm_write()
1073 ftl->cache_zone = zone_num; in sm_write()
1076 sm_cache_put(ftl, buf, boffset); in sm_write()
1078 mod_timer(&ftl->timer, jiffies + msecs_to_jiffies(cache_timeout)); in sm_write()
1079 mutex_unlock(&ftl->mutex); in sm_write()
1086 struct sm_ftl *ftl = dev->priv; in sm_flush() local
1089 mutex_lock(&ftl->mutex); in sm_flush()
1090 retval = sm_cache_flush(ftl); in sm_flush()
1091 mutex_unlock(&ftl->mutex); in sm_flush()
1098 struct sm_ftl *ftl = dev->priv; in sm_release() local
1100 mutex_lock(&ftl->mutex); in sm_release()
1101 del_timer_sync(&ftl->timer); in sm_release()
1102 cancel_work_sync(&ftl->flush_work); in sm_release()
1103 sm_cache_flush(ftl); in sm_release()
1104 mutex_unlock(&ftl->mutex); in sm_release()
1110 struct sm_ftl *ftl = dev->priv; in sm_getgeo() local
1111 geo->heads = ftl->heads; in sm_getgeo()
1112 geo->sectors = ftl->sectors; in sm_getgeo()
1113 geo->cylinders = ftl->cylinders; in sm_getgeo()
1121 struct sm_ftl *ftl; in sm_add_mtd() local
1124 ftl = kzalloc(sizeof(struct sm_ftl), GFP_KERNEL); in sm_add_mtd()
1125 if (!ftl) in sm_add_mtd()
1129 mutex_init(&ftl->mutex); in sm_add_mtd()
1130 timer_setup(&ftl->timer, sm_cache_flush_timer, 0); in sm_add_mtd()
1131 INIT_WORK(&ftl->flush_work, sm_cache_flush_work); in sm_add_mtd()
1134 if (sm_get_media_info(ftl, mtd)) { in sm_add_mtd()
1141 ftl->cis_buffer = kzalloc(SM_SECTOR_SIZE, GFP_KERNEL); in sm_add_mtd()
1142 if (!ftl->cis_buffer) in sm_add_mtd()
1146 ftl->zones = kcalloc(ftl->zone_count, sizeof(struct ftl_zone), in sm_add_mtd()
1148 if (!ftl->zones) in sm_add_mtd()
1152 ftl->cache_data = kzalloc(ftl->block_size, GFP_KERNEL); in sm_add_mtd()
1154 if (!ftl->cache_data) in sm_add_mtd()
1157 sm_cache_init(ftl); in sm_add_mtd()
1165 ftl->trans = trans; in sm_add_mtd()
1166 trans->priv = ftl; in sm_add_mtd()
1171 trans->size = (ftl->block_size * ftl->max_lba * ftl->zone_count) >> 9; in sm_add_mtd()
1172 trans->readonly = ftl->readonly; in sm_add_mtd()
1174 if (sm_find_cis(ftl)) { in sm_add_mtd()
1179 ftl->disk_attributes = sm_create_sysfs_attributes(ftl); in sm_add_mtd()
1180 if (!ftl->disk_attributes) in sm_add_mtd()
1182 trans->disk_attributes = ftl->disk_attributes; in sm_add_mtd()
1189 ftl->zone_count, ftl->max_lba, in sm_add_mtd()
1190 ftl->zone_size - ftl->max_lba); in sm_add_mtd()
1192 ftl->block_size); in sm_add_mtd()
1204 kfree(ftl->cache_data); in sm_add_mtd()
1206 kfree(ftl->zones); in sm_add_mtd()
1208 kfree(ftl->cis_buffer); in sm_add_mtd()
1210 kfree(ftl); in sm_add_mtd()
1218 struct sm_ftl *ftl = dev->priv; in sm_remove_dev() local
1222 ftl->trans = NULL; in sm_remove_dev()
1224 for (i = 0 ; i < ftl->zone_count; i++) { in sm_remove_dev()
1226 if (!ftl->zones[i].initialized) in sm_remove_dev()
1229 kfree(ftl->zones[i].lba_to_phys_table); in sm_remove_dev()
1230 kfifo_free(&ftl->zones[i].free_sectors); in sm_remove_dev()
1233 sm_delete_sysfs_attributes(ftl); in sm_remove_dev()
1234 kfree(ftl->cis_buffer); in sm_remove_dev()
1235 kfree(ftl->zones); in sm_remove_dev()
1236 kfree(ftl->cache_data); in sm_remove_dev()
1237 kfree(ftl); in sm_remove_dev()