Lines Matching refs:ftl
57 static struct attribute_group *sm_create_sysfs_attributes(struct sm_ftl *ftl) in sm_create_sysfs_attributes() argument
64 vendor = kstrndup(ftl->cis_buffer + SM_CIS_VENDOR_OFFSET, in sm_create_sysfs_attributes()
107 static void sm_delete_sysfs_attributes(struct sm_ftl *ftl) in sm_delete_sysfs_attributes() argument
109 struct attribute **attributes = ftl->disk_attributes->attrs; in sm_delete_sysfs_attributes()
125 kfree(ftl->disk_attributes->attrs); in sm_delete_sysfs_attributes()
126 kfree(ftl->disk_attributes); in sm_delete_sysfs_attributes()
195 static loff_t sm_mkoffset(struct sm_ftl *ftl, int zone, int block, int boffset) in sm_mkoffset() argument
198 WARN_ON(zone < 0 || zone >= ftl->zone_count); in sm_mkoffset()
199 WARN_ON(block >= ftl->zone_size); in sm_mkoffset()
200 WARN_ON(boffset >= ftl->block_size); in sm_mkoffset()
205 return (zone * SM_MAX_ZONE_SIZE + block) * ftl->block_size + boffset; in sm_mkoffset()
209 static void sm_break_offset(struct sm_ftl *ftl, loff_t offset, in sm_break_offset() argument
212 *boffset = do_div(offset, ftl->block_size); in sm_break_offset()
213 *block = do_div(offset, ftl->max_lba); in sm_break_offset()
214 *zone = offset >= ftl->zone_count ? -1 : offset; in sm_break_offset()
236 static int sm_read_sector(struct sm_ftl *ftl, in sm_read_sector() argument
240 struct mtd_info *mtd = ftl->trans->mtd; in sm_read_sector()
256 ops.mode = ftl->smallpagenand ? MTD_OPS_RAW : MTD_OPS_PLACE_OOB; in sm_read_sector()
267 if (zone == 0 && block == ftl->cis_block && boffset == in sm_read_sector()
268 ftl->cis_boffset) in sm_read_sector()
272 if (try == 3 || sm_recheck_media(ftl)) in sm_read_sector()
278 ret = mtd_read_oob(mtd, sm_mkoffset(ftl, zone, block, boffset), &ops); in sm_read_sector()
307 (ftl->smallpagenand && sm_correct_sector(buffer, oob))) { in sm_read_sector()
318 static int sm_write_sector(struct sm_ftl *ftl, in sm_write_sector() argument
323 struct mtd_info *mtd = ftl->trans->mtd; in sm_write_sector()
326 BUG_ON(ftl->readonly); in sm_write_sector()
328 if (zone == 0 && (block == ftl->cis_block || block == 0)) { in sm_write_sector()
333 if (ftl->unstable) in sm_write_sector()
336 ops.mode = ftl->smallpagenand ? MTD_OPS_RAW : MTD_OPS_PLACE_OOB; in sm_write_sector()
343 ret = mtd_write_oob(mtd, sm_mkoffset(ftl, zone, block, boffset), &ops); in sm_write_sector()
351 sm_recheck_media(ftl); in sm_write_sector()
365 static int sm_write_block(struct sm_ftl *ftl, uint8_t *buf, in sm_write_block() argument
377 if (ftl->unstable) in sm_write_block()
380 for (boffset = 0; boffset < ftl->block_size; in sm_write_block()
394 if (ftl->smallpagenand) { in sm_write_block()
401 if (!sm_write_sector(ftl, zone, block, boffset, in sm_write_block()
413 if (sm_erase_block(ftl, zone, block, 0)) in sm_write_block()
419 sm_mark_block_bad(ftl, zone, block); in sm_write_block()
428 static void sm_mark_block_bad(struct sm_ftl *ftl, int zone, int block) in sm_mark_block_bad() argument
436 if (ftl->unstable) in sm_mark_block_bad()
439 if (sm_recheck_media(ftl)) in sm_mark_block_bad()
447 for (boffset = 0; boffset < ftl->block_size; boffset += SM_SECTOR_SIZE) in sm_mark_block_bad()
448 sm_write_sector(ftl, zone, block, boffset, NULL, &oob); in sm_mark_block_bad()
455 static int sm_erase_block(struct sm_ftl *ftl, int zone_num, uint16_t block, in sm_erase_block() argument
458 struct ftl_zone *zone = &ftl->zones[zone_num]; in sm_erase_block()
459 struct mtd_info *mtd = ftl->trans->mtd; in sm_erase_block()
464 erase.addr = sm_mkoffset(ftl, zone_num, block, 0); in sm_erase_block()
465 erase.len = ftl->block_size; in sm_erase_block()
466 erase.priv = (u_long)ftl; in sm_erase_block()
468 if (ftl->unstable) in sm_erase_block()
471 BUG_ON(ftl->readonly); in sm_erase_block()
473 if (zone_num == 0 && (block == ftl->cis_block || block == 0)) { in sm_erase_block()
485 wait_for_completion(&ftl->erase_completion); in sm_erase_block()
499 sm_mark_block_bad(ftl, zone_num, block); in sm_erase_block()
505 struct sm_ftl *ftl = (struct sm_ftl *)self->priv; in sm_erase_callback() local
506 complete(&ftl->erase_completion); in sm_erase_callback()
510 static int sm_check_block(struct sm_ftl *ftl, int zone, int block) in sm_check_block() argument
522 for (boffset = 0; boffset < ftl->block_size; in sm_check_block()
526 if (sm_read_sector(ftl, zone, block, boffset, NULL, &oob)) in sm_check_block()
541 sm_erase_block(ftl, zone, block, 1); in sm_check_block()
571 static int sm_get_media_info(struct sm_ftl *ftl, struct mtd_info *mtd) in sm_get_media_info() argument
576 ftl->readonly = mtd->type == MTD_ROM; in sm_get_media_info()
579 ftl->zone_count = 1; in sm_get_media_info()
580 ftl->smallpagenand = 0; in sm_get_media_info()
585 ftl->zone_size = 256; in sm_get_media_info()
586 ftl->max_lba = 250; in sm_get_media_info()
587 ftl->block_size = 8 * SM_SECTOR_SIZE; in sm_get_media_info()
588 ftl->smallpagenand = 1; in sm_get_media_info()
594 ftl->zone_size = 512; in sm_get_media_info()
595 ftl->max_lba = 500; in sm_get_media_info()
596 ftl->block_size = 8 * SM_SECTOR_SIZE; in sm_get_media_info()
597 ftl->smallpagenand = 1; in sm_get_media_info()
601 if (!ftl->readonly) in sm_get_media_info()
604 ftl->zone_size = 256; in sm_get_media_info()
605 ftl->max_lba = 250; in sm_get_media_info()
606 ftl->block_size = 16 * SM_SECTOR_SIZE; in sm_get_media_info()
611 ftl->zone_size = 512; in sm_get_media_info()
612 ftl->max_lba = 500; in sm_get_media_info()
613 ftl->block_size = 16 * SM_SECTOR_SIZE; in sm_get_media_info()
617 ftl->zone_size = 1024; in sm_get_media_info()
618 ftl->max_lba = 1000; in sm_get_media_info()
619 ftl->block_size = 16 * SM_SECTOR_SIZE; in sm_get_media_info()
625 ftl->zone_count = size_in_megs / 16; in sm_get_media_info()
626 ftl->zone_size = 1024; in sm_get_media_info()
627 ftl->max_lba = 1000; in sm_get_media_info()
628 ftl->block_size = 32 * SM_SECTOR_SIZE; in sm_get_media_info()
632 if (mtd->erasesize > ftl->block_size) in sm_get_media_info()
638 if (ftl->smallpagenand && mtd->oobsize < SM_SMALL_OOB_SIZE) in sm_get_media_info()
641 if (!ftl->smallpagenand && mtd->oobsize < SM_OOB_SIZE) in sm_get_media_info()
651 ftl->cylinders = chs_table[i].cyl; in sm_get_media_info()
652 ftl->heads = chs_table[i].head; in sm_get_media_info()
653 ftl->sectors = chs_table[i].sec; in sm_get_media_info()
659 ftl->cylinders = 985; in sm_get_media_info()
660 ftl->heads = 33; in sm_get_media_info()
661 ftl->sectors = 63; in sm_get_media_info()
666 static int sm_read_cis(struct sm_ftl *ftl) in sm_read_cis() argument
670 if (sm_read_sector(ftl, in sm_read_cis()
671 0, ftl->cis_block, ftl->cis_boffset, ftl->cis_buffer, &oob)) in sm_read_cis()
677 if (!memcmp(ftl->cis_buffer + ftl->cis_page_offset, in sm_read_cis()
686 static int sm_find_cis(struct sm_ftl *ftl) in sm_find_cis() argument
694 for (block = 0 ; block < ftl->zone_size - ftl->max_lba ; block++) { in sm_find_cis()
696 if (sm_read_sector(ftl, 0, block, 0, NULL, &oob)) in sm_find_cis()
709 for (boffset = 0 ; boffset < ftl->block_size; in sm_find_cis()
712 if (sm_read_sector(ftl, 0, block, boffset, NULL, &oob)) in sm_find_cis()
720 if (boffset == ftl->block_size) in sm_find_cis()
723 ftl->cis_block = block; in sm_find_cis()
724 ftl->cis_boffset = boffset; in sm_find_cis()
725 ftl->cis_page_offset = 0; in sm_find_cis()
727 cis_found = !sm_read_cis(ftl); in sm_find_cis()
730 ftl->cis_page_offset = SM_SMALL_PAGE; in sm_find_cis()
731 cis_found = !sm_read_cis(ftl); in sm_find_cis()
736 block * ftl->block_size + in sm_find_cis()
737 boffset + ftl->cis_page_offset); in sm_find_cis()
744 static int sm_recheck_media(struct sm_ftl *ftl) in sm_recheck_media() argument
746 if (sm_read_cis(ftl)) { in sm_recheck_media()
748 if (!ftl->unstable) { in sm_recheck_media()
750 ftl->unstable = 1; in sm_recheck_media()
758 static int sm_init_zone(struct sm_ftl *ftl, int zone_num) in sm_init_zone() argument
760 struct ftl_zone *zone = &ftl->zones[zone_num]; in sm_init_zone()
770 zone->lba_to_phys_table = kmalloc(ftl->max_lba * 2, GFP_KERNEL); in sm_init_zone()
774 memset(zone->lba_to_phys_table, -1, ftl->max_lba * 2); in sm_init_zone()
778 if (kfifo_alloc(&zone->free_sectors, ftl->zone_size * 2, GFP_KERNEL)) { in sm_init_zone()
784 for (block = 0 ; block < ftl->zone_size ; block++) { in sm_init_zone()
787 if (zone_num == 0 && block <= ftl->cis_block) in sm_init_zone()
791 if (sm_read_sector(ftl, zone_num, block, 0, NULL, &oob)) in sm_init_zone()
817 if (lba == -2 || lba >= ftl->max_lba) { in sm_init_zone()
836 if (sm_check_block(ftl, zone_num, block)) in sm_init_zone()
840 if (sm_check_block(ftl, zone_num, in sm_init_zone()
851 sm_erase_block(ftl, zone_num, block, 1); in sm_init_zone()
878 static struct ftl_zone *sm_get_zone(struct sm_ftl *ftl, int zone_num) in sm_get_zone() argument
883 BUG_ON(zone_num >= ftl->zone_count); in sm_get_zone()
884 zone = &ftl->zones[zone_num]; in sm_get_zone()
887 error = sm_init_zone(ftl, zone_num); in sm_get_zone()
899 static void sm_cache_init(struct sm_ftl *ftl) in sm_cache_init() argument
901 ftl->cache_data_invalid_bitmap = 0xFFFFFFFF; in sm_cache_init()
902 ftl->cache_clean = 1; in sm_cache_init()
903 ftl->cache_zone = -1; in sm_cache_init()
904 ftl->cache_block = -1; in sm_cache_init()
909 static void sm_cache_put(struct sm_ftl *ftl, char *buffer, int boffset) in sm_cache_put() argument
911 memcpy(ftl->cache_data + boffset, buffer, SM_SECTOR_SIZE); in sm_cache_put()
912 clear_bit(boffset / SM_SECTOR_SIZE, &ftl->cache_data_invalid_bitmap); in sm_cache_put()
913 ftl->cache_clean = 0; in sm_cache_put()
917 static int sm_cache_get(struct sm_ftl *ftl, char *buffer, int boffset) in sm_cache_get() argument
920 &ftl->cache_data_invalid_bitmap)) in sm_cache_get()
923 memcpy(buffer, ftl->cache_data + boffset, SM_SECTOR_SIZE); in sm_cache_get()
928 static int sm_cache_flush(struct sm_ftl *ftl) in sm_cache_flush() argument
934 int zone_num = ftl->cache_zone; in sm_cache_flush()
937 if (ftl->cache_clean) in sm_cache_flush()
940 if (ftl->unstable) in sm_cache_flush()
944 zone = &ftl->zones[zone_num]; in sm_cache_flush()
945 block_num = zone->lba_to_phys_table[ftl->cache_block]; in sm_cache_flush()
949 for_each_set_bit(sector_num, &ftl->cache_data_invalid_bitmap, in sm_cache_flush()
950 ftl->block_size / SM_SECTOR_SIZE) { in sm_cache_flush()
952 if (!sm_read_sector(ftl, in sm_cache_flush()
954 ftl->cache_data + sector_num * SM_SECTOR_SIZE, NULL)) in sm_cache_flush()
956 &ftl->cache_data_invalid_bitmap); in sm_cache_flush()
960 if (ftl->unstable) in sm_cache_flush()
974 if (sm_write_block(ftl, ftl->cache_data, zone_num, write_sector, in sm_cache_flush()
975 ftl->cache_block, ftl->cache_data_invalid_bitmap)) in sm_cache_flush()
979 zone->lba_to_phys_table[ftl->cache_block] = write_sector; in sm_cache_flush()
983 sm_erase_block(ftl, zone_num, block_num, 1); in sm_cache_flush()
985 sm_cache_init(ftl); in sm_cache_flush()
993 struct sm_ftl *ftl = (struct sm_ftl *)data; in sm_cache_flush_timer() local
994 queue_work(cache_flush_workqueue, &ftl->flush_work); in sm_cache_flush_timer()
1000 struct sm_ftl *ftl = container_of(work, struct sm_ftl, flush_work); in sm_cache_flush_work() local
1001 mutex_lock(&ftl->mutex); in sm_cache_flush_work()
1002 sm_cache_flush(ftl); in sm_cache_flush_work()
1003 mutex_unlock(&ftl->mutex); in sm_cache_flush_work()
1013 struct sm_ftl *ftl = dev->priv; in sm_read() local
1018 sm_break_offset(ftl, sect_no << 9, &zone_num, &block, &boffset); in sm_read()
1019 mutex_lock(&ftl->mutex); in sm_read()
1022 zone = sm_get_zone(ftl, zone_num); in sm_read()
1029 if (ftl->cache_zone == zone_num && ftl->cache_block == block) { in sm_read()
1031 if (!sm_cache_get(ftl, buf, boffset)) in sm_read()
1043 if (sm_read_sector(ftl, zone_num, block, boffset, buf, NULL)) { in sm_read()
1049 sm_cache_put(ftl, buf, boffset); in sm_read()
1051 mutex_unlock(&ftl->mutex); in sm_read()
1059 struct sm_ftl *ftl = dev->priv; in sm_write() local
1063 BUG_ON(ftl->readonly); in sm_write()
1064 sm_break_offset(ftl, sec_no << 9, &zone_num, &block, &boffset); in sm_write()
1067 del_timer(&ftl->timer); in sm_write()
1068 mutex_lock(&ftl->mutex); in sm_write()
1070 zone = sm_get_zone(ftl, zone_num); in sm_write()
1077 if (ftl->cache_block != block || ftl->cache_zone != zone_num) { in sm_write()
1079 error = sm_cache_flush(ftl); in sm_write()
1083 ftl->cache_block = block; in sm_write()
1084 ftl->cache_zone = zone_num; in sm_write()
1087 sm_cache_put(ftl, buf, boffset); in sm_write()
1089 mod_timer(&ftl->timer, jiffies + msecs_to_jiffies(cache_timeout)); in sm_write()
1090 mutex_unlock(&ftl->mutex); in sm_write()
1097 struct sm_ftl *ftl = dev->priv; in sm_flush() local
1100 mutex_lock(&ftl->mutex); in sm_flush()
1101 retval = sm_cache_flush(ftl); in sm_flush()
1102 mutex_unlock(&ftl->mutex); in sm_flush()
1109 struct sm_ftl *ftl = dev->priv; in sm_release() local
1111 mutex_lock(&ftl->mutex); in sm_release()
1112 del_timer_sync(&ftl->timer); in sm_release()
1113 cancel_work_sync(&ftl->flush_work); in sm_release()
1114 sm_cache_flush(ftl); in sm_release()
1115 mutex_unlock(&ftl->mutex); in sm_release()
1121 struct sm_ftl *ftl = dev->priv; in sm_getgeo() local
1122 geo->heads = ftl->heads; in sm_getgeo()
1123 geo->sectors = ftl->sectors; in sm_getgeo()
1124 geo->cylinders = ftl->cylinders; in sm_getgeo()
1132 struct sm_ftl *ftl; in sm_add_mtd() local
1135 ftl = kzalloc(sizeof(struct sm_ftl), GFP_KERNEL); in sm_add_mtd()
1136 if (!ftl) in sm_add_mtd()
1140 mutex_init(&ftl->mutex); in sm_add_mtd()
1141 setup_timer(&ftl->timer, sm_cache_flush_timer, (unsigned long)ftl); in sm_add_mtd()
1142 INIT_WORK(&ftl->flush_work, sm_cache_flush_work); in sm_add_mtd()
1143 init_completion(&ftl->erase_completion); in sm_add_mtd()
1146 if (sm_get_media_info(ftl, mtd)) { in sm_add_mtd()
1153 ftl->cis_buffer = kzalloc(SM_SECTOR_SIZE, GFP_KERNEL); in sm_add_mtd()
1154 if (!ftl->cis_buffer) in sm_add_mtd()
1158 ftl->zones = kzalloc(sizeof(struct ftl_zone) * ftl->zone_count, in sm_add_mtd()
1160 if (!ftl->zones) in sm_add_mtd()
1164 ftl->cache_data = kzalloc(ftl->block_size, GFP_KERNEL); in sm_add_mtd()
1166 if (!ftl->cache_data) in sm_add_mtd()
1169 sm_cache_init(ftl); in sm_add_mtd()
1177 ftl->trans = trans; in sm_add_mtd()
1178 trans->priv = ftl; in sm_add_mtd()
1183 trans->size = (ftl->block_size * ftl->max_lba * ftl->zone_count) >> 9; in sm_add_mtd()
1184 trans->readonly = ftl->readonly; in sm_add_mtd()
1186 if (sm_find_cis(ftl)) { in sm_add_mtd()
1191 ftl->disk_attributes = sm_create_sysfs_attributes(ftl); in sm_add_mtd()
1192 if (!ftl->disk_attributes) in sm_add_mtd()
1194 trans->disk_attributes = ftl->disk_attributes; in sm_add_mtd()
1201 ftl->zone_count, ftl->max_lba, in sm_add_mtd()
1202 ftl->zone_size - ftl->max_lba); in sm_add_mtd()
1204 ftl->block_size); in sm_add_mtd()
1216 kfree(ftl->cache_data); in sm_add_mtd()
1218 kfree(ftl->zones); in sm_add_mtd()
1220 kfree(ftl->cis_buffer); in sm_add_mtd()
1222 kfree(ftl); in sm_add_mtd()
1230 struct sm_ftl *ftl = dev->priv; in sm_remove_dev() local
1234 ftl->trans = NULL; in sm_remove_dev()
1236 for (i = 0 ; i < ftl->zone_count; i++) { in sm_remove_dev()
1238 if (!ftl->zones[i].initialized) in sm_remove_dev()
1241 kfree(ftl->zones[i].lba_to_phys_table); in sm_remove_dev()
1242 kfifo_free(&ftl->zones[i].free_sectors); in sm_remove_dev()
1245 sm_delete_sysfs_attributes(ftl); in sm_remove_dev()
1246 kfree(ftl->cis_buffer); in sm_remove_dev()
1247 kfree(ftl->zones); in sm_remove_dev()
1248 kfree(ftl->cache_data); in sm_remove_dev()
1249 kfree(ftl); in sm_remove_dev()