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 hd_geometry		geometry;
156 	struct attribute_group		attr_group;
157 	struct request			*req;
158 	int				caps;
159 	int				disk_id;
160 
161 	/* IO */
162 	struct workqueue_struct		*io_queue;
163 	bool				io_queue_stopped;
164 	struct work_struct		io_work;
165 	bool				card_dead;
166 
167 	/* Media properties */
168 	struct ms_boot_page		*boot_page;
169 	u16				boot_block_locations[2];
170 	int				boot_block_count;
171 
172 	bool				read_only;
173 	unsigned short			page_size;
174 	int				block_size;
175 	int				pages_in_block;
176 	int				zone_count;
177 	int				block_count;
178 	int				logical_block_count;
179 
180 	/* FTL tables */
181 	unsigned long			*used_blocks_bitmap;
182 	unsigned long			*erased_blocks_bitmap;
183 	u16				*lba_to_pba_table;
184 	int				free_block_count[MS_MAX_ZONES];
185 	bool				ftl_initialized;
186 
187 	/* Cache */
188 	unsigned char			*cache;
189 	unsigned long			valid_cache_bitmap;
190 	int				cache_block_lba;
191 	bool				need_flush_cache;
192 	struct timer_list		cache_flush_timer;
193 
194 	/* Preallocated buffers */
195 	unsigned char			*block_buffer;
196 	struct scatterlist		prealloc_sg[MS_BLOCK_MAX_SEGS+1];
197 
198 
199 	/* handler's local data */
200 	struct ms_register_addr		reg_addr;
201 	bool				addr_valid;
202 
203 	u8				command_value;
204 	bool				command_need_oob;
205 	struct scatterlist		*current_sg;
206 	int				current_sg_offset;
207 
208 	struct ms_register		regs;
209 	int				current_page;
210 
211 	int				state;
212 	int				exit_error;
213 	bool				int_polling;
214 	unsigned long			int_timeout;
215 
216 };
217 
218 enum msb_readpage_states {
219 	MSB_RP_SEND_BLOCK_ADDRESS = 0,
220 	MSB_RP_SEND_READ_COMMAND,
221 
222 	MSB_RP_SEND_INT_REQ,
223 	MSB_RP_RECEIVE_INT_REQ_RESULT,
224 
225 	MSB_RP_SEND_READ_STATUS_REG,
226 	MSB_RP_RECEIVE_STATUS_REG,
227 
228 	MSB_RP_SEND_OOB_READ,
229 	MSB_RP_RECEIVE_OOB_READ,
230 
231 	MSB_RP_SEND_READ_DATA,
232 	MSB_RP_RECEIVE_READ_DATA,
233 };
234 
235 enum msb_write_block_states {
236 	MSB_WB_SEND_WRITE_PARAMS = 0,
237 	MSB_WB_SEND_WRITE_OOB,
238 	MSB_WB_SEND_WRITE_COMMAND,
239 
240 	MSB_WB_SEND_INT_REQ,
241 	MSB_WB_RECEIVE_INT_REQ,
242 
243 	MSB_WB_SEND_WRITE_DATA,
244 	MSB_WB_RECEIVE_WRITE_CONFIRMATION,
245 };
246 
247 enum msb_send_command_states {
248 	MSB_SC_SEND_WRITE_PARAMS,
249 	MSB_SC_SEND_WRITE_OOB,
250 	MSB_SC_SEND_COMMAND,
251 
252 	MSB_SC_SEND_INT_REQ,
253 	MSB_SC_RECEIVE_INT_REQ,
254 
255 };
256 
257 enum msb_reset_states {
258 	MSB_RS_SEND,
259 	MSB_RS_CONFIRM,
260 };
261 
262 enum msb_par_switch_states {
263 	MSB_PS_SEND_SWITCH_COMMAND,
264 	MSB_PS_SWICH_HOST,
265 	MSB_PS_CONFIRM,
266 };
267 
268 struct chs_entry {
269 	unsigned long size;
270 	unsigned char sec;
271 	unsigned short cyl;
272 	unsigned char head;
273 };
274 
275 static int msb_reset(struct msb_data *msb, bool full);
276 
277 static int h_msb_default_bad(struct memstick_dev *card,
278 						struct memstick_request **mrq);
279 
280 #define __dbg(level, format, ...) \
281 	do { \
282 		if (debug >= level) \
283 			pr_err(format "\n", ## __VA_ARGS__); \
284 	} while (0)
285 
286 
287 #define dbg(format, ...)		__dbg(1, format, ## __VA_ARGS__)
288 #define dbg_verbose(format, ...)	__dbg(2, format, ## __VA_ARGS__)
289 
290 #endif
291