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