1 | /* |
---|
2 | * ata_internal.h |
---|
3 | * |
---|
4 | * ATA RTEMS driver internal header file |
---|
5 | * |
---|
6 | * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia |
---|
7 | * Authors: Eugeny S. Mints <Eugeny.Mints@oktet.ru> |
---|
8 | * Alexandra Kossovsky <sasha@oktet.ru> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | * |
---|
14 | */ |
---|
15 | #ifndef __ATA_INTERNAL_H__ |
---|
16 | #define __ATA_INTERNAL_H__ |
---|
17 | |
---|
18 | #include <sys/param.h> |
---|
19 | #include <rtems.h> |
---|
20 | #include <sys/types.h> |
---|
21 | #include <rtems/libio.h> |
---|
22 | #include <stdlib.h> |
---|
23 | |
---|
24 | #include <rtems/blkdev.h> |
---|
25 | #include <rtems/diskdevs.h> |
---|
26 | |
---|
27 | #ifdef __cplusplus |
---|
28 | extern "C" { |
---|
29 | #endif |
---|
30 | |
---|
31 | /* |
---|
32 | * Conversion from and to little-endian byte order. (no-op on i386/i486) |
---|
33 | * |
---|
34 | * Naming: Ca_b_c, where a: F = from, T = to, b: LE = little-endian, |
---|
35 | * BE = big-endian, c: W = word (16 bits), L = longword (32 bits) |
---|
36 | */ |
---|
37 | #if (CPU_BIG_ENDIAN == TRUE) |
---|
38 | # define CF_LE_W(v) CPU_swap_u16(v) |
---|
39 | # define CF_LE_L(v) CPU_swap_u32(v) |
---|
40 | # define CT_LE_W(v) CPU_swap_u16(v) |
---|
41 | # define CT_LE_L(v) CPU_swap_u32(v) |
---|
42 | #else |
---|
43 | # define CF_LE_W(v) (v) |
---|
44 | # define CF_LE_L(v) (v) |
---|
45 | # define CT_LE_W(v) (v) |
---|
46 | # define CT_LE_L(v) (v) |
---|
47 | #endif |
---|
48 | |
---|
49 | #define ATA_UNDEFINED_VALUE (-1) |
---|
50 | |
---|
51 | /* Sector size for all ATA devices */ |
---|
52 | #define ATA_SECTOR_SIZE 512 |
---|
53 | |
---|
54 | |
---|
55 | #define ATA_MAX_CMD_REG_OFFSET 8 |
---|
56 | |
---|
57 | |
---|
58 | /* ATA Commands */ |
---|
59 | |
---|
60 | /* Types of ATA commands */ |
---|
61 | #define ATA_COMMAND_TYPE_NON_DATA 0 |
---|
62 | #define ATA_COMMAND_TYPE_PIO_IN 1 |
---|
63 | #define ATA_COMMAND_TYPE_PIO_OUT 2 |
---|
64 | #define ATA_COMMAND_TYPE_DMA 3 |
---|
65 | |
---|
66 | /* ATA commands opcodes */ |
---|
67 | /* |
---|
68 | * Commands present in both ATA-2 and ATA-4 specs. |
---|
69 | * Some commands have two values in ATA-2, |
---|
70 | * in such case value from ATA-4 used. |
---|
71 | * Some commands have slightly different names in these specifications, |
---|
72 | * so names from ATA-4 are used. |
---|
73 | */ |
---|
74 | #define ATA_COMMAND_NOP 0x00 |
---|
75 | #define ATA_COMMAND_READ_SECTORS 0x20 |
---|
76 | #define ATA_COMMAND_WRITE_SECTORS 0x30 |
---|
77 | #define ATA_COMMAND_READ_VERIFY_SECTORS 0x40 |
---|
78 | #define ATA_COMMAND_SEEK 0x70 /* or 0x7. */ |
---|
79 | #define ATA_COMMAND_EXECUTE_DEVICE_DIAGNOSTIC 0x90 |
---|
80 | #define ATA_COMMAND_INITIALIZE_DEVICE_PARAMETERS 0x91 |
---|
81 | #define ATA_COMMAND_DOWNLOAD_MICROCODE 0x92 |
---|
82 | #define ATA_COMMAND_READ_MULTIPLE 0xc4 |
---|
83 | #define ATA_COMMAND_WRITE_MULTIPLE 0xc5 |
---|
84 | #define ATA_COMMAND_SET_MULTIPLE_MODE 0xc6 |
---|
85 | #define ATA_COMMAND_READ_DMA 0xc8 |
---|
86 | #define ATA_COMMAND_WRITE_DMA 0xca |
---|
87 | #define ATA_COMMAND_STANDBY_IMMEDIATE 0xe0 /* or 0x94 */ |
---|
88 | #define ATA_COMMAND_IDLE_IMMEDIATE 0xe1 /* or 0x95 */ |
---|
89 | #define ATA_COMMAND_STANDBY 0xe2 /* or 0x96 */ |
---|
90 | #define ATA_COMMAND_IDLE 0xe3 /* or 0x97 */ |
---|
91 | #define ATA_COMMAND_READ_BUFFER 0xe4 |
---|
92 | #define ATA_COMMAND_CHECK_POWER_MODE 0xe5 /* or 0x98 in ATA-2 */ |
---|
93 | #define ATA_COMMAND_SLEEP 0xe6 /* or 0x99 */ |
---|
94 | #define ATA_COMMAND_WRITE_BUFFER 0xe8 |
---|
95 | #define ATA_COMMAND_IDENTIFY_DEVICE 0xec |
---|
96 | #define ATA_COMMAND_SET_FEATURES 0xef |
---|
97 | |
---|
98 | /* Commands present in both ATA-2 and ATA-4 specs: removable media */ |
---|
99 | #define ATA_COMMAND_MEDIA_LOCK 0xde |
---|
100 | #define ATA_COMMAND_MEDIA_UNLOCK 0xdf |
---|
101 | #define ATA_COMMAND_MEDIA_EJECT 0xed |
---|
102 | |
---|
103 | |
---|
104 | /* Commands present in ATA-2, but not in ATA-4 (not used) */ |
---|
105 | #define ATA_COMMAND_RECALIBRATE 0x10 /* or 0x1. */ |
---|
106 | #define ATA_COMMAND_READ_SECTOR_NON_RETRY 0x21 |
---|
107 | #define ATA_COMMAND_READ_LONG_RETRY 0x22 |
---|
108 | #define ATA_COMMAND_READ_LONG_NON_RETRY 0x23 |
---|
109 | #define ATA_COMMAND_WRITE_SECTOR_NON_RETRY 0x31 |
---|
110 | #define ATA_COMMAND_WRITE_LONG_RETRY 0x32 |
---|
111 | #define ATA_COMMAND_WRITE_LONG_NON_RETRY 0x33 |
---|
112 | #define ATA_COMMAND_WRITE_VERIFY 0x3c |
---|
113 | #define ATA_COMMAND_READ_VERIFY_SECTOR_NON_RETRY 0x41 |
---|
114 | #define ATA_COMMAND_FORMAT_TRACK 0x50 |
---|
115 | #define ATA_COMMAND_READ_DMA_NON_RETRY 0xc9 |
---|
116 | #define ATA_COMMAND_WRITE_DMA_NON_RETRY 0xcb |
---|
117 | #define ATA_COMMAND_ACKNOWLEGE_MEDIA_CHANGE 0xdb |
---|
118 | #define ATA_COMMAND_BOOT_POST_BOOT 0xdc |
---|
119 | #define ATA_COMMAND_BOOT_PRE_BOOT 0xdd |
---|
120 | #define ATA_COMMAND_WRITE_SAME 0xe9 |
---|
121 | |
---|
122 | /* Commands from ATA-4 specification: CFA feature set */ |
---|
123 | #define ATA_COMMAND_CFA_REQUEST_EXTENDED_ERROR_CODE 0x03 |
---|
124 | #define ATA_COMMAND_CFA_WRITE_SECTORS_WITHOUT_ERASE 0x38 |
---|
125 | #define ATA_COMMAND_CFA_TRANSLATE_SECTOR 0x87 |
---|
126 | #define ATA_COMMAND_CFA_ERASE_SECTORS 0xc0 |
---|
127 | #define ATA_COMMAND_CFA_WRITE_MULTIPLE_WITHOUT_ERASE 0xcd |
---|
128 | |
---|
129 | /* Commands from ATA-4 specification: commands to use with PACKET command */ |
---|
130 | #define ATA_COMMAND_DEVICE_RESET 0x08 |
---|
131 | #define ATA_COMMAND_PACKET 0xa0 |
---|
132 | #define ATA_COMMAND_IDENTIFY_PACKET_DEVICE 0xa1 |
---|
133 | #define ATA_COMMAND_SERVICE 0xa2 |
---|
134 | |
---|
135 | /* Commands from ATA-4 specification: SECURITY commands */ |
---|
136 | #define ATA_COMMAND_SECURITY_SET_PASSWORD 0xf1 |
---|
137 | #define ATA_COMMAND_SECURITY_UNLOCK 0xf2 |
---|
138 | #define ATA_COMMAND_SECURITY_ERASE_PREPARE 0xf3 |
---|
139 | #define ATA_COMMAND_SECURITY_ERASE_UNIT 0xf4 |
---|
140 | #define ATA_COMMAND_SECURITY_FREEZE_LOCK 0xf5 |
---|
141 | #define ATA_COMMAND_SECURITY_DISABLE_PASSWORD 0xf6 |
---|
142 | |
---|
143 | /* Commands from ATA-4 specification: other commands */ |
---|
144 | #define ATA_COMMAND_SMART 0xb0 |
---|
145 | #define ATA_COMMAND_READ_DMA_QUEUED 0xc7 |
---|
146 | #define ATA_COMMAND_WRITE_DMA_QUEUED 0xcc |
---|
147 | #define ATA_COMMAND_GET_MEDIA_STATUS 0xda |
---|
148 | #define ATA_COMMAND_FLUSH_CACHE 0xe7 |
---|
149 | #define ATA_COMMAND_READ_NATIVE_MAX_ADDRESS 0xf8 |
---|
150 | #define ATA_COMMAND_SET_MAX_ADDRESS 0xf9 |
---|
151 | |
---|
152 | #define ATA_REGISTERS_VALUE(reg) (1 << (reg)) |
---|
153 | |
---|
154 | /* ATA IDENTIFY DEVICE command words and bits */ |
---|
155 | #define ATA_IDENT_WORD_RW_MULT 47 |
---|
156 | #define ATA_IDENT_WORD_CAPABILITIES 49 |
---|
157 | #define ATA_IDENT_WORD_FIELD_VALIDITY 53 |
---|
158 | #define ATA_IDENT_WORD_NUM_OF_CURR_LOG_CLNDS 54 |
---|
159 | #define ATA_IDENT_WORD_NUM_OF_CURR_LOG_HEADS 55 |
---|
160 | #define ATA_IDENT_WORD_NUM_OF_CURR_LOG_SECS 56 |
---|
161 | #define ATA_IDENT_WORD_MULT_SECS 59 |
---|
162 | #define ATA_IDENT_WORD_NUM_OF_USR_SECS0 60 |
---|
163 | #define ATA_IDENT_WORD_NUM_OF_USR_SECS1 61 |
---|
164 | #define ATA_IDENT_WORD_PIO_SPPRTD 64 |
---|
165 | |
---|
166 | #define ATA_IDENT_BIT_VALID 0x02 |
---|
167 | |
---|
168 | /* |
---|
169 | * It is OR for all ATA_REGISTERS_VALUE(reg), where reg is neccessary |
---|
170 | * for setting block position |
---|
171 | */ |
---|
172 | #define ATA_REGISTERS_POSITION 0xfc |
---|
173 | |
---|
174 | #define ATA_MINOR_NUM_RESERVED_PER_ATA_DEVICE 64 |
---|
175 | |
---|
176 | #define ATA_MAX_RTEMS_INT_VEC_NUMBER 255 |
---|
177 | |
---|
178 | #define ATA_MAX_NAME_LENGTH 10 |
---|
179 | |
---|
180 | /* diagnostic codes */ |
---|
181 | #define ATA_DEV0_PASSED_DEV1_PASSED_OR_NOT_PRSNT 0x01 |
---|
182 | #define ATA_DEV0_PASSED_DEV1_FAILED 0x81 |
---|
183 | #define ATA_DEV1_PASSED_DEV0_FAILED 0x80 |
---|
184 | |
---|
185 | /* |
---|
186 | * Obtain ata device parameters by controller minor number and device number |
---|
187 | */ |
---|
188 | #define ATA_DEV_INFO(controller_minor, dev) \ |
---|
189 | ata_ide_ctrls[controller_minor].device[dev] |
---|
190 | |
---|
191 | /* ATA RTEMS driver internal data stuctures */ |
---|
192 | |
---|
193 | /* Command block registers */ |
---|
194 | typedef struct ata_registers_s { |
---|
195 | uint16_t regs[8]; /* command block registers */ |
---|
196 | uint16_t to_read; /* mask: which ata registers should be read */ |
---|
197 | uint16_t to_write; /* mask: which ata registers should be written */ |
---|
198 | } ata_registers_t; |
---|
199 | |
---|
200 | /* ATA request */ |
---|
201 | typedef struct ata_req_s { |
---|
202 | rtems_chain_node link; /* link in requests chain */ |
---|
203 | char type; /* request type */ |
---|
204 | ata_registers_t regs; /* ATA command */ |
---|
205 | uint32_t cnt; /* Number of sectors to be exchanged */ |
---|
206 | uint32_t cbuf; /* number of current buffer from breq in use */ |
---|
207 | uint32_t pos; /* current position in 'cbuf' */ |
---|
208 | rtems_blkdev_request *breq; /* blkdev_request which corresponds to the |
---|
209 | * ata request |
---|
210 | */ |
---|
211 | rtems_id sema; /* semaphore which is used if synchronous |
---|
212 | * processing of the ata request is required |
---|
213 | */ |
---|
214 | rtems_status_code status; /* status of ata request processing */ |
---|
215 | int info; /* device info code */ |
---|
216 | } ata_req_t; |
---|
217 | |
---|
218 | /* call callback provided by block device request if it is defined */ |
---|
219 | #define ATA_EXEC_CALLBACK(areq, status) \ |
---|
220 | do {\ |
---|
221 | if ((areq)->breq != NULL) \ |
---|
222 | rtems_blkdev_request_done((areq)->breq, status); \ |
---|
223 | } while (0) |
---|
224 | |
---|
225 | /* ATA RTEMS driver events types */ |
---|
226 | typedef enum ata_msg_type_s { |
---|
227 | ATA_MSG_GEN_EVT = 1, /* general event */ |
---|
228 | ATA_MSG_SUCCESS_EVT, /* success event */ |
---|
229 | ATA_MSG_ERROR_EVT, /* error event */ |
---|
230 | ATA_MSG_PROCESS_NEXT_EVT /* process next request event */ |
---|
231 | } ata_msg_type_t; |
---|
232 | |
---|
233 | /* ATA RTEMS driver message */ |
---|
234 | typedef struct ata_queue_msg_s { |
---|
235 | ata_msg_type_t type; /* message type */ |
---|
236 | rtems_device_minor_number ctrl_minor; /* IDE controller minor number */ |
---|
237 | int error; /* error code */ |
---|
238 | } ata_queue_msg_t; |
---|
239 | |
---|
240 | /* macros for messages processing */ |
---|
241 | #define ATA_FILL_MSG(msg, evt_type, ctrl, err)\ |
---|
242 | do {\ |
---|
243 | msg.type = evt_type;\ |
---|
244 | msg.ctrl_minor = ctrl;\ |
---|
245 | msg.error = err;\ |
---|
246 | } while (0) |
---|
247 | |
---|
248 | #define ATA_SEND_EVT(msg, type, ctrl, err)\ |
---|
249 | do {\ |
---|
250 | rtems_status_code rc;\ |
---|
251 | ATA_FILL_MSG(msg, type, ctrl, err);\ |
---|
252 | rc = rtems_message_queue_send(ata_queue_id, &msg,\ |
---|
253 | sizeof(ata_queue_msg_t));\ |
---|
254 | if (rc != RTEMS_SUCCESSFUL)\ |
---|
255 | rtems_fatal_error_occurred(RTEMS_INTERNAL_ERROR);\ |
---|
256 | } while (0) |
---|
257 | |
---|
258 | /* |
---|
259 | * Array of such structures is indexed by interrupt vecotrs and used for |
---|
260 | * mapping of IDE controllers and interrupt vectors |
---|
261 | */ |
---|
262 | typedef struct ata_int_st_s { |
---|
263 | rtems_chain_node link; |
---|
264 | rtems_device_minor_number ctrl_minor; |
---|
265 | } ata_int_st_t; |
---|
266 | |
---|
267 | /* |
---|
268 | * Mapping of rtems ATA devices to the following pairs: |
---|
269 | * (IDE controller number served the device, device number on the controller) |
---|
270 | */ |
---|
271 | typedef struct ata_ide_dev_s { |
---|
272 | int ctrl_minor;/* minor number of IDE controller served rtems ATA device */ |
---|
273 | int device; /* device number on IDE controller (0 or 1) */ |
---|
274 | } ata_ide_dev_t; |
---|
275 | |
---|
276 | /* |
---|
277 | * ATA device description |
---|
278 | */ |
---|
279 | typedef struct ata_dev_s { |
---|
280 | int8_t present; /* 1 -- present, 0 -- not present, */ |
---|
281 | /* -1 -- non-initialized */ |
---|
282 | uint16_t cylinders; |
---|
283 | uint16_t heads; |
---|
284 | uint16_t sectors; |
---|
285 | uint32_t lba_sectors; /* for small disk */ |
---|
286 | /* == cylinders * heads * sectors */ |
---|
287 | |
---|
288 | uint8_t lba_avaible; /* 0 - CHS mode, 1 - LBA mode */ |
---|
289 | |
---|
290 | uint16_t modes_available; /* OR of values for this modes */ |
---|
291 | uint16_t mode_active; |
---|
292 | } ata_dev_t; |
---|
293 | |
---|
294 | /* |
---|
295 | * This structure describes controller state, devices configuration on the |
---|
296 | * controller and chain of ATA requests to the controller. Array of such |
---|
297 | * structures is indexed by controller minor number |
---|
298 | */ |
---|
299 | typedef struct ata_ide_ctrl_s { |
---|
300 | bool present; /* controller state */ |
---|
301 | ata_dev_t device[2]; /* ata diveces description */ |
---|
302 | rtems_chain_control reqs; /* requests chain */ |
---|
303 | } ata_ide_ctrl_t; |
---|
304 | |
---|
305 | /* Block device request with a single buffer provided */ |
---|
306 | typedef struct blkdev_request1 { |
---|
307 | rtems_blkdev_request req; |
---|
308 | rtems_blkdev_sg_buffer sg[1]; |
---|
309 | } blkdev_request1; |
---|
310 | |
---|
311 | void ata_breq_init(blkdev_request1 *breq, uint16_t *sector_buffer); |
---|
312 | |
---|
313 | rtems_status_code ata_identify_device( |
---|
314 | rtems_device_minor_number ctrl_minor, |
---|
315 | int dev, |
---|
316 | uint16_t *sector_buffer, |
---|
317 | ata_dev_t *device_entry |
---|
318 | ); |
---|
319 | |
---|
320 | void ata_process_request_on_init_phase( |
---|
321 | rtems_device_minor_number ctrl_minor, |
---|
322 | ata_req_t *areq |
---|
323 | ); |
---|
324 | |
---|
325 | #ifdef __cplusplus |
---|
326 | } |
---|
327 | #endif |
---|
328 | |
---|
329 | #endif /* __ATA_INTERNAL_H__ */ |
---|