Rev 4507 | Rev 4530 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4507 | svoboda | 1 | /* |
2 | * Copyright (c) 2009 Jiri Svoboda |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * - The name of the author may not be used to endorse or promote products |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
29 | /** @addtogroup bd |
||
30 | * @{ |
||
31 | */ |
||
32 | |||
33 | /** |
||
34 | * @file |
||
35 | * @brief ATA disk driver |
||
36 | * |
||
37 | * This driver currently works only with CHS addressing and uses PIO. |
||
38 | * Currently based on the (now obsolete) ANSI X3.221-1994 (ATA-1) standard. |
||
39 | * At this point only reading is possible, not writing. |
||
40 | */ |
||
41 | |||
42 | #include <stdio.h> |
||
43 | #include <libarch/ddi.h> |
||
44 | #include <ddi.h> |
||
45 | #include <ipc/ipc.h> |
||
46 | #include <ipc/bd.h> |
||
47 | #include <async.h> |
||
48 | #include <as.h> |
||
49 | #include <futex.h> |
||
50 | #include <devmap.h> |
||
51 | #include <sys/types.h> |
||
52 | #include <errno.h> |
||
4529 | svoboda | 53 | #include <bool.h> |
4507 | svoboda | 54 | |
55 | #define NAME "ata_bd" |
||
56 | |||
57 | enum { |
||
58 | CTL_READ_START = 0, |
||
59 | CTL_WRITE_START = 1, |
||
60 | }; |
||
61 | |||
62 | enum { |
||
63 | STATUS_FAILURE = 0 |
||
64 | }; |
||
65 | |||
66 | enum { |
||
67 | MAX_DISKS = 2 |
||
68 | }; |
||
69 | |||
70 | typedef union { |
||
71 | /* Read */ |
||
72 | struct { |
||
73 | uint8_t data_port; |
||
74 | uint8_t error; |
||
75 | uint8_t sector_count; |
||
76 | uint8_t sector_number; |
||
77 | uint8_t cylinder_low; |
||
78 | uint8_t cylinder_high; |
||
79 | uint8_t drive_head; |
||
80 | uint8_t status; |
||
81 | }; |
||
82 | |||
83 | /* Write */ |
||
84 | struct { |
||
85 | uint8_t pad0[7]; |
||
86 | uint8_t command; |
||
87 | }; |
||
88 | } ata_cmd_t; |
||
89 | |||
90 | typedef union { |
||
91 | /* Read */ |
||
92 | struct { |
||
93 | uint8_t pad0[6]; |
||
94 | uint8_t alt_status; |
||
95 | uint8_t drive_address; |
||
96 | }; |
||
97 | |||
98 | /* Write */ |
||
99 | struct { |
||
100 | uint8_t pad1[6]; |
||
101 | uint8_t device_control; |
||
102 | uint8_t pad2; |
||
103 | }; |
||
104 | } ata_ctl_t; |
||
105 | |||
106 | enum devctl_bits { |
||
107 | DCR_SRST = 0x04, /**< Software Reset */ |
||
108 | DCR_nIEN = 0x02 /**< Interrupt Enable (negated) */ |
||
109 | }; |
||
110 | |||
111 | enum status_bits { |
||
112 | SR_BSY = 0x80, /**< Busy */ |
||
113 | SR_DRDY = 0x40, /**< Drive Ready */ |
||
114 | SR_DWF = 0x20, /**< Drive Write Fault */ |
||
115 | SR_DSC = 0x10, /**< Drive Seek Complete */ |
||
116 | SR_DRQ = 0x08, /**< Data Request */ |
||
117 | SR_CORR = 0x04, /**< Corrected Data */ |
||
118 | SR_IDX = 0x02, /**< Index */ |
||
119 | SR_ERR = 0x01 /**< Error */ |
||
120 | }; |
||
121 | |||
122 | enum drive_head_bits { |
||
123 | DHR_DRV = 0x10 |
||
124 | }; |
||
125 | |||
126 | enum error_bits { |
||
127 | ER_BBK = 0x80, /**< Bad Block Detected */ |
||
128 | ER_UNC = 0x40, /**< Uncorrectable Data Error */ |
||
129 | ER_MC = 0x20, /**< Media Changed */ |
||
130 | ER_IDNF = 0x10, /**< ID Not Found */ |
||
131 | ER_MCR = 0x08, /**< Media Change Request */ |
||
132 | ER_ABRT = 0x04, /**< Aborted Command */ |
||
133 | ER_TK0NF = 0x02, /**< Track 0 Not Found */ |
||
134 | ER_AMNF = 0x01 /**< Address Mark Not Found */ |
||
135 | }; |
||
136 | |||
4529 | svoboda | 137 | typedef struct { |
138 | bool present; |
||
139 | unsigned heads; |
||
140 | unsigned cylinders; |
||
141 | unsigned sectors; |
||
142 | uint64_t blocks; |
||
143 | } disk_t; |
||
144 | |||
4507 | svoboda | 145 | static const size_t block_size = 512; |
146 | static size_t comm_size; |
||
147 | |||
148 | static uintptr_t cmd_physical = 0x1f0; |
||
149 | static uintptr_t ctl_physical = 0x170; |
||
150 | static ata_cmd_t *cmd; |
||
151 | static ata_ctl_t *ctl; |
||
152 | |||
153 | static dev_handle_t dev_handle[MAX_DISKS]; |
||
154 | |||
155 | static atomic_t dev_futex = FUTEX_INITIALIZER; |
||
156 | |||
4529 | svoboda | 157 | static disk_t disk[2]; |
4507 | svoboda | 158 | |
159 | static int ata_bd_init(void); |
||
160 | static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall); |
||
161 | static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, off_t size, |
||
162 | void *buf); |
||
163 | static int ata_bd_read_block(int disk_id, uint64_t blk_idx, size_t blk_cnt, |
||
164 | void *buf); |
||
4529 | svoboda | 165 | static int drive_identify(int drive_id, disk_t *d); |
4507 | svoboda | 166 | |
167 | int main(int argc, char **argv) |
||
168 | { |
||
169 | uint8_t status; |
||
4529 | svoboda | 170 | char name[16]; |
171 | int i, rc; |
||
172 | int n_disks; |
||
4507 | svoboda | 173 | |
174 | printf(NAME ": ATA disk driver\n"); |
||
175 | |||
176 | printf("cmd_physical = 0x%x\n", cmd_physical); |
||
177 | printf("ctl_physical = 0x%x\n", ctl_physical); |
||
178 | |||
179 | if (ata_bd_init() != EOK) |
||
180 | return -1; |
||
181 | |||
182 | /* Put drives to reset, disable interrupts. */ |
||
183 | printf("Reset drives...\n"); |
||
184 | pio_write_8(&ctl->device_control, DCR_SRST); |
||
185 | /* printf("wait for busy\n"); |
||
186 | do { |
||
187 | status = pio_read_8(&cmd->status); |
||
188 | } while ((status & SR_BSY) == 0); |
||
189 | */ |
||
190 | async_usleep(100); |
||
191 | pio_write_8(&ctl->device_control, 0); |
||
192 | |||
193 | do { |
||
194 | status = pio_read_8(&cmd->status); |
||
195 | } while ((status & SR_BSY) != 0); |
||
196 | printf("Done\n"); |
||
197 | |||
198 | printf("Status = 0x%x\n", pio_read_8(&cmd->status)); |
||
199 | |||
4529 | svoboda | 200 | (void) drive_identify(0, &disk[0]); |
201 | (void) drive_identify(1, &disk[1]); |
||
202 | |||
203 | n_disks = 0; |
||
204 | |||
205 | for (i = 0; i < MAX_DISKS; i++) { |
||
206 | /* Skip unattached drives. */ |
||
207 | if (disk[i].present == false) |
||
208 | continue; |
||
209 | |||
210 | snprintf(name, 16, "disk%d", i); |
||
211 | rc = devmap_device_register(name, &dev_handle[i]); |
||
212 | if (rc != EOK) { |
||
213 | devmap_hangup_phone(DEVMAP_DRIVER); |
||
214 | printf(NAME ": Unable to register device %s.\n", |
||
215 | name); |
||
216 | return rc; |
||
217 | } |
||
218 | ++n_disks; |
||
219 | } |
||
220 | |||
221 | if (n_disks == 0) { |
||
222 | printf("No disks detected.\n"); |
||
223 | return -1; |
||
224 | } |
||
225 | |||
226 | printf(NAME ": Accepting connections\n"); |
||
227 | async_manager(); |
||
228 | |||
229 | /* Not reached */ |
||
230 | return 0; |
||
231 | } |
||
232 | |||
233 | static int drive_identify(int disk_id, disk_t *d) |
||
234 | { |
||
235 | uint16_t data; |
||
236 | uint8_t status; |
||
237 | int i; |
||
238 | |||
239 | printf("Identify drive %d\n", disk_id); |
||
240 | pio_write_8(&cmd->drive_head, ((disk_id != 0) ? DHR_DRV : 0)); |
||
4507 | svoboda | 241 | async_usleep(100); |
242 | pio_write_8(&cmd->command, 0xEC); |
||
243 | |||
4529 | svoboda | 244 | status = pio_read_8(&cmd->status); |
245 | printf("Status = 0x%x\n", status); |
||
4507 | svoboda | 246 | |
4529 | svoboda | 247 | d->present = false; |
248 | |||
249 | /* |
||
250 | * Detect if drive is present. This is Qemu only! Need to |
||
251 | * do the right thing to work with real drives. |
||
252 | */ |
||
253 | if ((status & SR_DRDY) == 0) { |
||
254 | printf("None attached.\n"); |
||
255 | return ENOENT; |
||
256 | } |
||
257 | |||
4507 | svoboda | 258 | for (i = 0; i < 256; i++) { |
259 | do { |
||
260 | status = pio_read_8(&cmd->status); |
||
261 | } while ((status & SR_DRDY) == 0); |
||
262 | |||
263 | data = pio_read_16(&cmd->data_port); |
||
264 | |||
265 | switch (i) { |
||
4529 | svoboda | 266 | case 1: d->cylinders = data; break; |
267 | case 3: d->heads = data; break; |
||
268 | case 6: d->sectors = data; break; |
||
4507 | svoboda | 269 | } |
270 | } |
||
271 | |||
272 | printf("\n\nStatus = 0x%x\n", pio_read_8(&cmd->status)); |
||
273 | |||
4529 | svoboda | 274 | d->blocks = d->cylinders * d->heads * d->sectors; |
275 | |||
4507 | svoboda | 276 | printf("Geometry: %u cylinders, %u heads, %u sectors\n", |
4529 | svoboda | 277 | d->cylinders, d->heads, d->sectors); |
4507 | svoboda | 278 | |
4529 | svoboda | 279 | d->present = true; |
4507 | svoboda | 280 | |
4529 | svoboda | 281 | return EOK; |
4507 | svoboda | 282 | } |
283 | |||
284 | static int ata_bd_init(void) |
||
285 | { |
||
286 | void *vaddr; |
||
4529 | svoboda | 287 | int rc; |
4507 | svoboda | 288 | |
289 | rc = devmap_driver_register(NAME, ata_bd_connection); |
||
290 | if (rc < 0) { |
||
291 | printf(NAME ": Unable to register driver.\n"); |
||
292 | return rc; |
||
293 | } |
||
294 | |||
295 | rc = pio_enable((void *) cmd_physical, sizeof(ata_cmd_t), &vaddr); |
||
296 | if (rc != EOK) { |
||
297 | printf(NAME ": Could not initialize device I/O space.\n"); |
||
298 | return rc; |
||
299 | } |
||
300 | |||
301 | cmd = vaddr; |
||
302 | |||
303 | rc = pio_enable((void *) ctl_physical, sizeof(ata_ctl_t), &vaddr); |
||
304 | if (rc != EOK) { |
||
305 | printf(NAME ": Could not initialize device I/O space.\n"); |
||
306 | return rc; |
||
307 | } |
||
308 | |||
309 | ctl = vaddr; |
||
310 | |||
311 | |||
312 | return EOK; |
||
313 | } |
||
314 | |||
315 | static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall) |
||
316 | { |
||
317 | void *fs_va = NULL; |
||
318 | ipc_callid_t callid; |
||
319 | ipc_call_t call; |
||
320 | ipcarg_t method; |
||
321 | dev_handle_t dh; |
||
322 | int flags; |
||
323 | int retval; |
||
324 | off_t idx; |
||
325 | off_t size; |
||
326 | int disk_id, i; |
||
327 | |||
328 | /* Get the device handle. */ |
||
329 | dh = IPC_GET_ARG1(*icall); |
||
330 | |||
331 | /* Determine which disk device is the client connecting to. */ |
||
332 | disk_id = -1; |
||
333 | for (i = 0; i < MAX_DISKS; i++) |
||
334 | if (dev_handle[i] == dh) |
||
335 | disk_id = i; |
||
336 | |||
4529 | svoboda | 337 | if (disk_id < 0 || disk[disk_id].present == false) { |
4507 | svoboda | 338 | ipc_answer_0(iid, EINVAL); |
339 | return; |
||
340 | } |
||
341 | |||
342 | /* Answer the IPC_M_CONNECT_ME_TO call. */ |
||
343 | ipc_answer_0(iid, EOK); |
||
344 | |||
345 | if (!ipc_share_out_receive(&callid, &comm_size, &flags)) { |
||
346 | ipc_answer_0(callid, EHANGUP); |
||
347 | return; |
||
348 | } |
||
349 | |||
350 | fs_va = as_get_mappable_page(comm_size); |
||
351 | if (fs_va == NULL) { |
||
352 | ipc_answer_0(callid, EHANGUP); |
||
353 | return; |
||
354 | } |
||
355 | |||
356 | (void) ipc_share_out_finalize(callid, fs_va); |
||
357 | |||
358 | while (1) { |
||
359 | callid = async_get_call(&call); |
||
360 | method = IPC_GET_METHOD(call); |
||
361 | switch (method) { |
||
362 | case IPC_M_PHONE_HUNGUP: |
||
363 | /* The other side has hung up. */ |
||
364 | ipc_answer_0(callid, EOK); |
||
365 | return; |
||
366 | case BD_READ_BLOCK: |
||
367 | case BD_WRITE_BLOCK: |
||
368 | idx = IPC_GET_ARG1(call); |
||
369 | size = IPC_GET_ARG2(call); |
||
370 | if (size > comm_size) { |
||
371 | retval = EINVAL; |
||
372 | break; |
||
373 | } |
||
374 | retval = ata_bd_rdwr(disk_id, method, idx, |
||
375 | size, fs_va); |
||
376 | break; |
||
377 | default: |
||
378 | retval = EINVAL; |
||
379 | break; |
||
380 | } |
||
381 | ipc_answer_0(callid, retval); |
||
382 | } |
||
383 | } |
||
384 | |||
385 | static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t blk_idx, off_t size, |
||
386 | void *buf) |
||
387 | { |
||
388 | int rc; |
||
389 | off_t now; |
||
390 | |||
391 | while (size > 0) { |
||
392 | now = size < block_size ? size : (off_t) block_size; |
||
393 | if (now != block_size) |
||
394 | return EINVAL; |
||
395 | |||
396 | if (method == BD_READ_BLOCK) |
||
397 | rc = ata_bd_read_block(disk_id, blk_idx, 1, buf); |
||
398 | else |
||
399 | rc = ENOTSUP; |
||
400 | |||
401 | if (rc != EOK) |
||
402 | return rc; |
||
403 | |||
404 | buf += block_size; |
||
405 | blk_idx++; |
||
406 | |||
407 | if (size > block_size) |
||
408 | size -= block_size; |
||
409 | else |
||
410 | size = 0; |
||
411 | } |
||
412 | |||
413 | return EOK; |
||
414 | } |
||
415 | |||
416 | |||
417 | static int ata_bd_read_block(int disk_id, uint64_t blk_idx, size_t blk_cnt, |
||
418 | void *buf) |
||
419 | { |
||
420 | size_t i; |
||
421 | uint16_t data; |
||
422 | uint8_t status; |
||
423 | uint64_t c, h, s; |
||
424 | uint64_t idx; |
||
425 | uint8_t drv_head; |
||
4529 | svoboda | 426 | disk_t *d; |
4507 | svoboda | 427 | |
4529 | svoboda | 428 | d = &disk[disk_id]; |
429 | |||
4507 | svoboda | 430 | /* Check device bounds. */ |
4529 | svoboda | 431 | if (blk_idx >= d->blocks) |
4507 | svoboda | 432 | return EINVAL; |
433 | |||
434 | /* Compute CHS. */ |
||
4529 | svoboda | 435 | c = blk_idx / (d->heads * d->sectors); |
436 | idx = blk_idx % (d->heads * d->sectors); |
||
4507 | svoboda | 437 | |
4529 | svoboda | 438 | h = idx / d->sectors; |
439 | s = 1 + (idx % d->sectors); |
||
4507 | svoboda | 440 | |
441 | /* New value for Drive/Head register */ |
||
442 | drv_head = |
||
443 | ((disk_id != 0) ? DHR_DRV : 0) | |
||
444 | (h & 0x0f); |
||
445 | |||
446 | futex_down(&dev_futex); |
||
447 | |||
448 | /* Program a Read Sectors operation. */ |
||
449 | |||
450 | pio_write_8(&cmd->drive_head, drv_head); |
||
451 | pio_write_8(&cmd->sector_count, 1); |
||
452 | pio_write_8(&cmd->sector_number, s); |
||
453 | pio_write_8(&cmd->cylinder_low, c & 0xff); |
||
454 | pio_write_8(&cmd->cylinder_high, c >> 16); |
||
455 | pio_write_8(&cmd->command, 0x20); |
||
456 | |||
457 | /* Read data from the disk buffer. */ |
||
458 | |||
459 | for (i = 0; i < 256; i++) { |
||
460 | do { |
||
461 | status = pio_read_8(&cmd->status); |
||
462 | } while ((status & SR_DRDY) == 0); |
||
463 | |||
464 | data = pio_read_16(&cmd->data_port); |
||
465 | ((uint16_t *) buf)[i] = data; |
||
466 | } |
||
467 | |||
468 | futex_up(&dev_futex); |
||
469 | return EOK; |
||
470 | } |
||
471 | |||
472 | |||
473 | /** |
||
474 | * @} |
||
475 | */ |