summaryrefslogtreecommitdiffstats
path: root/drivers/memstick/core/ms_block.h
blob: 122e1a8a8bd5be775c23f047dd67cdc4a32c4b89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* SPDX-License-Identifier: GPL-2.0-only */
/*
 *  ms_block.h - Sony MemoryStick (legacy) storage support

 *  Copyright (C) 2013 Maxim Levitsky <maximlevitsky@gmail.com>
 *
 * Minor portions of the driver are copied from mspro_block.c which is
 * Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
 *
 * Also ms structures were copied from old broken driver by same author
 * These probably come from MS spec
 */

#ifndef MS_BLOCK_NEW_H
#define MS_BLOCK_NEW_H

#define MS_BLOCK_MAX_SEGS      32
#define MS_BLOCK_MAX_PAGES     ((2 << 16) - 1)

#define MS_BLOCK_MAX_BOOT_ADDR 0x000c
#define MS_BLOCK_BOOT_ID       0x0001
#define MS_BLOCK_INVALID       0xffff
#define MS_MAX_ZONES           16
#define MS_BLOCKS_IN_ZONE      512

#define MS_BLOCK_MAP_LINE_SZ   16
#define MS_BLOCK_PART_SHIFT    3


#define MEMSTICK_UNCORR_ERROR (MEMSTICK_STATUS1_UCFG | \
		MEMSTICK_STATUS1_UCEX | MEMSTICK_STATUS1_UCDT)

#define MEMSTICK_CORR_ERROR (MEMSTICK_STATUS1_FGER | MEMSTICK_STATUS1_EXER | \
	MEMSTICK_STATUS1_DTER)

#define MEMSTICK_INT_ERROR (MEMSTICK_INT_CMDNAK | MEMSTICK_INT_ERR)

#define MEMSTICK_OVERWRITE_FLAG_NORMAL \
	(MEMSTICK_OVERWRITE_PGST1 | \
	MEMSTICK_OVERWRITE_PGST0  | \
	MEMSTICK_OVERWRITE_BKST)

#define MEMSTICK_OV_PG_NORMAL \
	(MEMSTICK_OVERWRITE_PGST1 | MEMSTICK_OVERWRITE_PGST0)

#define MEMSTICK_MANAGEMENT_FLAG_NORMAL \
	(MEMSTICK_MANAGEMENT_SYSFLG |  \
	MEMSTICK_MANAGEMENT_SCMS1   |  \
	MEMSTICK_MANAGEMENT_SCMS0)     \

struct ms_boot_header {
	unsigned short block_id;
	unsigned short format_reserved;
	unsigned char  reserved0[184];
	unsigned char  data_entry;
	unsigned char  reserved1[179];
} __packed;


struct ms_system_item {
	unsigned int  start_addr;
	unsigned int  data_size;
	unsigned char data_type_id;
	unsigned char reserved[3];
} __packed;

struct ms_system_entry {
	struct ms_system_item disabled_block;
	struct ms_system_item cis_idi;
	unsigned char         reserved[24];
} __packed;

struct ms_boot_attr_info {
	unsigned char      memorystick_class;
	unsigned char      format_unique_value1;
	unsigned short     block_size;
	unsigned short     number_of_blocks;
	unsigned short     number_of_effective_blocks;
	unsigned short     page_size;
	unsigned char      extra_data_size;
	unsigned char      format_unique_value2;
	unsigned char      assembly_time[8];
	unsigned char      format_unique_value3;
	unsigned char      serial_number[3];
	unsigned char      assembly_manufacturer_code;
	unsigned char      assembly_model_code[3];
	unsigned short     memory_manufacturer_code;
	unsigned short     memory_device_code;
	unsigned short     implemented_capacity;
	unsigned char      format_unique_value4[2];
	unsigned char      vcc;
	unsigned char      vpp;
	unsigned short     controller_number;
	unsigned short     controller_function;
	unsigned char      reserved0[9];
	unsigned char      transfer_supporting;
	unsigned short     format_unique_value5;
	unsigned char      format_type;
	unsigned char      memorystick_application;
	unsigned char      device_type;
	unsigned char      reserved1[22];
	unsigned char      format_uniqure_value6[2];
	unsigned char      reserved2[15];
} __packed;

struct ms_cis_idi {
	unsigned short general_config;
	unsigned short logical_cylinders;
	unsigned short reserved0;
	unsigned short logical_heads;
	unsigned short track_size;
	unsigned short page_size;
	unsigned short pages_per_track;
	unsigned short msw;
	unsigned short lsw;
	unsigned short reserved1;
	unsigned char  serial_number[20];
	unsigned short buffer_type;
	unsigned short buffer_size_increments;
	unsigned short long_command_ecc;
	unsigned char  firmware_version[28];
	unsigned char  model_name[18];
	unsigned short reserved2[5];
	unsigned short pio_mode_number;
	unsigned short dma_mode_number;
	unsigned short field_validity;
	unsigned short current_logical_cylinders;
	unsigned short current_logical_heads;
	unsigned short current_pages_per_track;
	unsigned int   current_page_capacity;
	unsigned short mutiple_page_setting;
	unsigned int   addressable_pages;
	unsigned short single_word_dma;
	unsigned short multi_word_dma;
	unsigned char  reserved3[128];
} __packed;


struct ms_boot_page {
	struct ms_boot_header    header;
	struct ms_system_entry   entry;
	struct ms_boot_attr_info attr;
} __packed;

struct msb_data {
	unsigned int			usage_count;
	struct memstick_dev		*card;
	struct gendisk			*disk;
	struct request_queue		*queue;
	spinlock_t			q_lock;
	struct blk_mq_tag_set		tag_set;
	struct hd_geometry		geometry;
	struct attribute_group		attr_group;
	struct request			*req;
	int				caps;
	int				disk_id;

	/* IO */
	struct workqueue_struct		*io_queue;
	bool				io_queue_stopped;
	struct work_struct		io_work;
	bool				card_dead;

	/* Media properties */
	struct ms_boot_page		*boot_page;
	u16				boot_block_locations[2];
	int				boot_block_count;

	bool				read_only;
	unsigned short			page_size;
	int				block_size;
	int				pages_in_block;
	int				zone_count;
	int				block_count;
	int				logical_block_count;

	/* FTL tables */
	unsigned long			*used_blocks_bitmap;
	unsigned long			*erased_blocks_bitmap;
	u16				*lba_to_pba_table;
	int				free_block_count[MS_MAX_ZONES];
	bool				ftl_initialized;

	/* Cache */
	unsigned char			*cache;
	unsigned long			valid_cache_bitmap;
	int				cache_block_lba;
	bool				need_flush_cache;
	struct timer_list		cache_flush_timer;

	/* Preallocated buffers */
	unsigned char			*block_buffer;
	struct scatterlist		prealloc_sg[MS_BLOCK_MAX_SEGS+1];


	/* handler's local data */
	struct ms_register_addr		reg_addr;
	bool				addr_valid;

	u8				command_value;
	bool				command_need_oob;
	struct scatterlist		*current_sg;
	int				current_sg_offset;

	struct ms_register		regs;
	int				current_page;

	int				state;
	int				exit_error;
	bool				int_polling;
	unsigned long			int_timeout;

};

enum msb_readpage_states {
	MSB_RP_SEND_BLOCK_ADDRESS = 0,
	MSB_RP_SEND_READ_COMMAND,

	MSB_RP_SEND_INT_REQ,
	MSB_RP_RECEIVE_INT_REQ_RESULT,

	MSB_RP_SEND_READ_STATUS_REG,
	MSB_RP_RECEIVE_STATUS_REG,

	MSB_RP_SEND_OOB_READ,
	MSB_RP_RECEIVE_OOB_READ,

	MSB_RP_SEND_READ_DATA,
	MSB_RP_RECEIVE_READ_DATA,
};

enum msb_write_block_states {
	MSB_WB_SEND_WRITE_PARAMS = 0,
	MSB_WB_SEND_WRITE_OOB,
	MSB_WB_SEND_WRITE_COMMAND,

	MSB_WB_SEND_INT_REQ,
	MSB_WB_RECEIVE_INT_REQ,

	MSB_WB_SEND_WRITE_DATA,
	MSB_WB_RECEIVE_WRITE_CONFIRMATION,
};

enum msb_send_command_states {
	MSB_SC_SEND_WRITE_PARAMS,
	MSB_SC_SEND_WRITE_OOB,
	MSB_SC_SEND_COMMAND,

	MSB_SC_SEND_INT_REQ,
	MSB_SC_RECEIVE_INT_REQ,

};

enum msb_reset_states {
	MSB_RS_SEND,
	MSB_RS_CONFIRM,
};

enum msb_par_switch_states {
	MSB_PS_SEND_SWITCH_COMMAND,
	MSB_PS_SWICH_HOST,
	MSB_PS_CONFIRM,
};

struct chs_entry {
	unsigned long size;
	unsigned char sec;
	unsigned short cyl;
	unsigned char head;
};

static int msb_reset(struct msb_data *msb, bool full);

static int h_msb_default_bad(struct memstick_dev *card,
						struct memstick_request **mrq);

#define __dbg(level, format, ...) \
	do { \
		if (debug >= level) \
			pr_err(format "\n", ## __VA_ARGS__); \
	} while (0)


#define dbg(format, ...)		__dbg(1, format, ## __VA_ARGS__)
#define dbg_verbose(format, ...)	__dbg(2, format, ## __VA_ARGS__)

#endif