1 /* 2 * ms_block.h - Sony MemoryStick (legacy) storage support 3 4 * Copyright (C) 2013 Maxim Levitsky <maximlevitsky@gmail.com> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 * 10 * Minor portions of the driver are copied from mspro_block.c which is 11 * Copyright (C) 2007 Alex Dubov <oakad@yahoo.com> 12 * 13 * Also ms structures were copied from old broken driver by same author 14 * These probably come from MS spec 15 * 16 */ 17 18 #ifndef MS_BLOCK_NEW_H 19 #define MS_BLOCK_NEW_H 20 21 #define MS_BLOCK_MAX_SEGS 32 22 #define MS_BLOCK_MAX_PAGES ((2 << 16) - 1) 23 24 #define MS_BLOCK_MAX_BOOT_ADDR 0x000c 25 #define MS_BLOCK_BOOT_ID 0x0001 26 #define MS_BLOCK_INVALID 0xffff 27 #define MS_MAX_ZONES 16 28 #define MS_BLOCKS_IN_ZONE 512 29 30 #define MS_BLOCK_MAP_LINE_SZ 16 31 #define MS_BLOCK_PART_SHIFT 3 32 33 34 #define MEMSTICK_UNCORR_ERROR (MEMSTICK_STATUS1_UCFG | \ 35 MEMSTICK_STATUS1_UCEX | MEMSTICK_STATUS1_UCDT) 36 37 #define MEMSTICK_CORR_ERROR (MEMSTICK_STATUS1_FGER | MEMSTICK_STATUS1_EXER | \ 38 MEMSTICK_STATUS1_DTER) 39 40 #define MEMSTICK_INT_ERROR (MEMSTICK_INT_CMDNAK | MEMSTICK_INT_ERR) 41 42 #define MEMSTICK_OVERWRITE_FLAG_NORMAL \ 43 (MEMSTICK_OVERWRITE_PGST1 | \ 44 MEMSTICK_OVERWRITE_PGST0 | \ 45 MEMSTICK_OVERWRITE_BKST) 46 47 #define MEMSTICK_OV_PG_NORMAL \ 48 (MEMSTICK_OVERWRITE_PGST1 | MEMSTICK_OVERWRITE_PGST0) 49 50 #define MEMSTICK_MANAGEMENT_FLAG_NORMAL \ 51 (MEMSTICK_MANAGEMENT_SYSFLG | \ 52 MEMSTICK_MANAGEMENT_SCMS1 | \ 53 MEMSTICK_MANAGEMENT_SCMS0) \ 54 55 struct ms_boot_header { 56 unsigned short block_id; 57 unsigned short format_reserved; 58 unsigned char reserved0[184]; 59 unsigned char data_entry; 60 unsigned char reserved1[179]; 61 } __packed; 62 63 64 struct ms_system_item { 65 unsigned int start_addr; 66 unsigned int data_size; 67 unsigned char data_type_id; 68 unsigned char reserved[3]; 69 } __packed; 70 71 struct ms_system_entry { 72 struct ms_system_item disabled_block; 73 struct ms_system_item cis_idi; 74 unsigned char reserved[24]; 75 } __packed; 76 77 struct ms_boot_attr_info { 78 unsigned char memorystick_class; 79 unsigned char format_unique_value1; 80 unsigned short block_size; 81 unsigned short number_of_blocks; 82 unsigned short number_of_effective_blocks; 83 unsigned short page_size; 84 unsigned char extra_data_size; 85 unsigned char format_unique_value2; 86 unsigned char assembly_time[8]; 87 unsigned char format_unique_value3; 88 unsigned char serial_number[3]; 89 unsigned char assembly_manufacturer_code; 90 unsigned char assembly_model_code[3]; 91 unsigned short memory_manufacturer_code; 92 unsigned short memory_device_code; 93 unsigned short implemented_capacity; 94 unsigned char format_unique_value4[2]; 95 unsigned char vcc; 96 unsigned char vpp; 97 unsigned short controller_number; 98 unsigned short controller_function; 99 unsigned char reserved0[9]; 100 unsigned char transfer_supporting; 101 unsigned short format_unique_value5; 102 unsigned char format_type; 103 unsigned char memorystick_application; 104 unsigned char device_type; 105 unsigned char reserved1[22]; 106 unsigned char format_uniqure_value6[2]; 107 unsigned char reserved2[15]; 108 } __packed; 109 110 struct ms_cis_idi { 111 unsigned short general_config; 112 unsigned short logical_cylinders; 113 unsigned short reserved0; 114 unsigned short logical_heads; 115 unsigned short track_size; 116 unsigned short page_size; 117 unsigned short pages_per_track; 118 unsigned short msw; 119 unsigned short lsw; 120 unsigned short reserved1; 121 unsigned char serial_number[20]; 122 unsigned short buffer_type; 123 unsigned short buffer_size_increments; 124 unsigned short long_command_ecc; 125 unsigned char firmware_version[28]; 126 unsigned char model_name[18]; 127 unsigned short reserved2[5]; 128 unsigned short pio_mode_number; 129 unsigned short dma_mode_number; 130 unsigned short field_validity; 131 unsigned short current_logical_cylinders; 132 unsigned short current_logical_heads; 133 unsigned short current_pages_per_track; 134 unsigned int current_page_capacity; 135 unsigned short mutiple_page_setting; 136 unsigned int addressable_pages; 137 unsigned short single_word_dma; 138 unsigned short multi_word_dma; 139 unsigned char reserved3[128]; 140 } __packed; 141 142 143 struct ms_boot_page { 144 struct ms_boot_header header; 145 struct ms_system_entry entry; 146 struct ms_boot_attr_info attr; 147 } __packed; 148 149 struct msb_data { 150 unsigned int usage_count; 151 struct memstick_dev *card; 152 struct gendisk *disk; 153 struct request_queue *queue; 154 spinlock_t q_lock; 155 struct blk_mq_tag_set tag_set; 156 struct hd_geometry geometry; 157 struct attribute_group attr_group; 158 struct request *req; 159 int caps; 160 int disk_id; 161 162 /* IO */ 163 struct workqueue_struct *io_queue; 164 bool io_queue_stopped; 165 struct work_struct io_work; 166 bool card_dead; 167 168 /* Media properties */ 169 struct ms_boot_page *boot_page; 170 u16 boot_block_locations[2]; 171 int boot_block_count; 172 173 bool read_only; 174 unsigned short page_size; 175 int block_size; 176 int pages_in_block; 177 int zone_count; 178 int block_count; 179 int logical_block_count; 180 181 /* FTL tables */ 182 unsigned long *used_blocks_bitmap; 183 unsigned long *erased_blocks_bitmap; 184 u16 *lba_to_pba_table; 185 int free_block_count[MS_MAX_ZONES]; 186 bool ftl_initialized; 187 188 /* Cache */ 189 unsigned char *cache; 190 unsigned long valid_cache_bitmap; 191 int cache_block_lba; 192 bool need_flush_cache; 193 struct timer_list cache_flush_timer; 194 195 /* Preallocated buffers */ 196 unsigned char *block_buffer; 197 struct scatterlist prealloc_sg[MS_BLOCK_MAX_SEGS+1]; 198 199 200 /* handler's local data */ 201 struct ms_register_addr reg_addr; 202 bool addr_valid; 203 204 u8 command_value; 205 bool command_need_oob; 206 struct scatterlist *current_sg; 207 int current_sg_offset; 208 209 struct ms_register regs; 210 int current_page; 211 212 int state; 213 int exit_error; 214 bool int_polling; 215 unsigned long int_timeout; 216 217 }; 218 219 enum msb_readpage_states { 220 MSB_RP_SEND_BLOCK_ADDRESS = 0, 221 MSB_RP_SEND_READ_COMMAND, 222 223 MSB_RP_SEND_INT_REQ, 224 MSB_RP_RECEIVE_INT_REQ_RESULT, 225 226 MSB_RP_SEND_READ_STATUS_REG, 227 MSB_RP_RECEIVE_STATUS_REG, 228 229 MSB_RP_SEND_OOB_READ, 230 MSB_RP_RECEIVE_OOB_READ, 231 232 MSB_RP_SEND_READ_DATA, 233 MSB_RP_RECEIVE_READ_DATA, 234 }; 235 236 enum msb_write_block_states { 237 MSB_WB_SEND_WRITE_PARAMS = 0, 238 MSB_WB_SEND_WRITE_OOB, 239 MSB_WB_SEND_WRITE_COMMAND, 240 241 MSB_WB_SEND_INT_REQ, 242 MSB_WB_RECEIVE_INT_REQ, 243 244 MSB_WB_SEND_WRITE_DATA, 245 MSB_WB_RECEIVE_WRITE_CONFIRMATION, 246 }; 247 248 enum msb_send_command_states { 249 MSB_SC_SEND_WRITE_PARAMS, 250 MSB_SC_SEND_WRITE_OOB, 251 MSB_SC_SEND_COMMAND, 252 253 MSB_SC_SEND_INT_REQ, 254 MSB_SC_RECEIVE_INT_REQ, 255 256 }; 257 258 enum msb_reset_states { 259 MSB_RS_SEND, 260 MSB_RS_CONFIRM, 261 }; 262 263 enum msb_par_switch_states { 264 MSB_PS_SEND_SWITCH_COMMAND, 265 MSB_PS_SWICH_HOST, 266 MSB_PS_CONFIRM, 267 }; 268 269 struct chs_entry { 270 unsigned long size; 271 unsigned char sec; 272 unsigned short cyl; 273 unsigned char head; 274 }; 275 276 static int msb_reset(struct msb_data *msb, bool full); 277 278 static int h_msb_default_bad(struct memstick_dev *card, 279 struct memstick_request **mrq); 280 281 #define __dbg(level, format, ...) \ 282 do { \ 283 if (debug >= level) \ 284 pr_err(format "\n", ## __VA_ARGS__); \ 285 } while (0) 286 287 288 #define dbg(format, ...) __dbg(1, format, ## __VA_ARGS__) 289 #define dbg_verbose(format, ...) __dbg(2, format, ## __VA_ARGS__) 290 291 #endif 292