Rev 4529 | Rev 4531 | 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 | |
4530 | svoboda | 55 | #include "ata_bd.h" |
56 | |||
4507 | svoboda | 57 | #define NAME "ata_bd" |
58 | |||
59 | static const size_t block_size = 512; |
||
60 | static size_t comm_size; |
||
61 | |||
62 | static uintptr_t cmd_physical = 0x1f0; |
||
63 | static uintptr_t ctl_physical = 0x170; |
||
64 | static ata_cmd_t *cmd; |
||
65 | static ata_ctl_t *ctl; |
||
66 | |||
67 | static dev_handle_t dev_handle[MAX_DISKS]; |
||
68 | |||
69 | static atomic_t dev_futex = FUTEX_INITIALIZER; |
||
70 | |||
4529 | svoboda | 71 | static disk_t disk[2]; |
4507 | svoboda | 72 | |
73 | static int ata_bd_init(void); |
||
74 | static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall); |
||
75 | static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, off_t size, |
||
76 | void *buf); |
||
77 | static int ata_bd_read_block(int disk_id, uint64_t blk_idx, size_t blk_cnt, |
||
78 | void *buf); |
||
4529 | svoboda | 79 | static int drive_identify(int drive_id, disk_t *d); |
4507 | svoboda | 80 | |
81 | int main(int argc, char **argv) |
||
82 | { |
||
83 | uint8_t status; |
||
4529 | svoboda | 84 | char name[16]; |
85 | int i, rc; |
||
86 | int n_disks; |
||
4507 | svoboda | 87 | |
88 | printf(NAME ": ATA disk driver\n"); |
||
89 | |||
90 | printf("cmd_physical = 0x%x\n", cmd_physical); |
||
91 | printf("ctl_physical = 0x%x\n", ctl_physical); |
||
92 | |||
93 | if (ata_bd_init() != EOK) |
||
94 | return -1; |
||
95 | |||
96 | /* Put drives to reset, disable interrupts. */ |
||
97 | printf("Reset drives...\n"); |
||
98 | pio_write_8(&ctl->device_control, DCR_SRST); |
||
99 | /* printf("wait for busy\n"); |
||
100 | do { |
||
101 | status = pio_read_8(&cmd->status); |
||
102 | } while ((status & SR_BSY) == 0); |
||
103 | */ |
||
104 | async_usleep(100); |
||
105 | pio_write_8(&ctl->device_control, 0); |
||
106 | |||
107 | do { |
||
108 | status = pio_read_8(&cmd->status); |
||
109 | } while ((status & SR_BSY) != 0); |
||
110 | printf("Done\n"); |
||
111 | |||
112 | printf("Status = 0x%x\n", pio_read_8(&cmd->status)); |
||
113 | |||
4529 | svoboda | 114 | (void) drive_identify(0, &disk[0]); |
115 | (void) drive_identify(1, &disk[1]); |
||
116 | |||
117 | n_disks = 0; |
||
118 | |||
119 | for (i = 0; i < MAX_DISKS; i++) { |
||
120 | /* Skip unattached drives. */ |
||
121 | if (disk[i].present == false) |
||
122 | continue; |
||
123 | |||
124 | snprintf(name, 16, "disk%d", i); |
||
125 | rc = devmap_device_register(name, &dev_handle[i]); |
||
126 | if (rc != EOK) { |
||
127 | devmap_hangup_phone(DEVMAP_DRIVER); |
||
128 | printf(NAME ": Unable to register device %s.\n", |
||
129 | name); |
||
130 | return rc; |
||
131 | } |
||
132 | ++n_disks; |
||
133 | } |
||
134 | |||
135 | if (n_disks == 0) { |
||
136 | printf("No disks detected.\n"); |
||
137 | return -1; |
||
138 | } |
||
139 | |||
140 | printf(NAME ": Accepting connections\n"); |
||
141 | async_manager(); |
||
142 | |||
143 | /* Not reached */ |
||
144 | return 0; |
||
145 | } |
||
146 | |||
147 | static int drive_identify(int disk_id, disk_t *d) |
||
148 | { |
||
149 | uint16_t data; |
||
150 | uint8_t status; |
||
151 | int i; |
||
152 | |||
153 | printf("Identify drive %d\n", disk_id); |
||
154 | pio_write_8(&cmd->drive_head, ((disk_id != 0) ? DHR_DRV : 0)); |
||
4507 | svoboda | 155 | async_usleep(100); |
156 | pio_write_8(&cmd->command, 0xEC); |
||
157 | |||
4529 | svoboda | 158 | status = pio_read_8(&cmd->status); |
159 | printf("Status = 0x%x\n", status); |
||
4507 | svoboda | 160 | |
4529 | svoboda | 161 | d->present = false; |
162 | |||
163 | /* |
||
164 | * Detect if drive is present. This is Qemu only! Need to |
||
165 | * do the right thing to work with real drives. |
||
166 | */ |
||
167 | if ((status & SR_DRDY) == 0) { |
||
168 | printf("None attached.\n"); |
||
169 | return ENOENT; |
||
170 | } |
||
171 | |||
4507 | svoboda | 172 | for (i = 0; i < 256; i++) { |
173 | do { |
||
174 | status = pio_read_8(&cmd->status); |
||
175 | } while ((status & SR_DRDY) == 0); |
||
176 | |||
177 | data = pio_read_16(&cmd->data_port); |
||
178 | |||
179 | switch (i) { |
||
4529 | svoboda | 180 | case 1: d->cylinders = data; break; |
181 | case 3: d->heads = data; break; |
||
182 | case 6: d->sectors = data; break; |
||
4507 | svoboda | 183 | } |
184 | } |
||
185 | |||
186 | printf("\n\nStatus = 0x%x\n", pio_read_8(&cmd->status)); |
||
187 | |||
4529 | svoboda | 188 | d->blocks = d->cylinders * d->heads * d->sectors; |
189 | |||
4507 | svoboda | 190 | printf("Geometry: %u cylinders, %u heads, %u sectors\n", |
4529 | svoboda | 191 | d->cylinders, d->heads, d->sectors); |
4507 | svoboda | 192 | |
4529 | svoboda | 193 | d->present = true; |
4507 | svoboda | 194 | |
4529 | svoboda | 195 | return EOK; |
4507 | svoboda | 196 | } |
197 | |||
198 | static int ata_bd_init(void) |
||
199 | { |
||
200 | void *vaddr; |
||
4529 | svoboda | 201 | int rc; |
4507 | svoboda | 202 | |
203 | rc = devmap_driver_register(NAME, ata_bd_connection); |
||
204 | if (rc < 0) { |
||
205 | printf(NAME ": Unable to register driver.\n"); |
||
206 | return rc; |
||
207 | } |
||
208 | |||
209 | rc = pio_enable((void *) cmd_physical, sizeof(ata_cmd_t), &vaddr); |
||
210 | if (rc != EOK) { |
||
211 | printf(NAME ": Could not initialize device I/O space.\n"); |
||
212 | return rc; |
||
213 | } |
||
214 | |||
215 | cmd = vaddr; |
||
216 | |||
217 | rc = pio_enable((void *) ctl_physical, sizeof(ata_ctl_t), &vaddr); |
||
218 | if (rc != EOK) { |
||
219 | printf(NAME ": Could not initialize device I/O space.\n"); |
||
220 | return rc; |
||
221 | } |
||
222 | |||
223 | ctl = vaddr; |
||
224 | |||
225 | |||
226 | return EOK; |
||
227 | } |
||
228 | |||
229 | static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall) |
||
230 | { |
||
231 | void *fs_va = NULL; |
||
232 | ipc_callid_t callid; |
||
233 | ipc_call_t call; |
||
234 | ipcarg_t method; |
||
235 | dev_handle_t dh; |
||
236 | int flags; |
||
237 | int retval; |
||
238 | off_t idx; |
||
239 | off_t size; |
||
240 | int disk_id, i; |
||
241 | |||
242 | /* Get the device handle. */ |
||
243 | dh = IPC_GET_ARG1(*icall); |
||
244 | |||
245 | /* Determine which disk device is the client connecting to. */ |
||
246 | disk_id = -1; |
||
247 | for (i = 0; i < MAX_DISKS; i++) |
||
248 | if (dev_handle[i] == dh) |
||
249 | disk_id = i; |
||
250 | |||
4529 | svoboda | 251 | if (disk_id < 0 || disk[disk_id].present == false) { |
4507 | svoboda | 252 | ipc_answer_0(iid, EINVAL); |
253 | return; |
||
254 | } |
||
255 | |||
256 | /* Answer the IPC_M_CONNECT_ME_TO call. */ |
||
257 | ipc_answer_0(iid, EOK); |
||
258 | |||
259 | if (!ipc_share_out_receive(&callid, &comm_size, &flags)) { |
||
260 | ipc_answer_0(callid, EHANGUP); |
||
261 | return; |
||
262 | } |
||
263 | |||
264 | fs_va = as_get_mappable_page(comm_size); |
||
265 | if (fs_va == NULL) { |
||
266 | ipc_answer_0(callid, EHANGUP); |
||
267 | return; |
||
268 | } |
||
269 | |||
270 | (void) ipc_share_out_finalize(callid, fs_va); |
||
271 | |||
272 | while (1) { |
||
273 | callid = async_get_call(&call); |
||
274 | method = IPC_GET_METHOD(call); |
||
275 | switch (method) { |
||
276 | case IPC_M_PHONE_HUNGUP: |
||
277 | /* The other side has hung up. */ |
||
278 | ipc_answer_0(callid, EOK); |
||
279 | return; |
||
280 | case BD_READ_BLOCK: |
||
281 | case BD_WRITE_BLOCK: |
||
282 | idx = IPC_GET_ARG1(call); |
||
283 | size = IPC_GET_ARG2(call); |
||
284 | if (size > comm_size) { |
||
285 | retval = EINVAL; |
||
286 | break; |
||
287 | } |
||
288 | retval = ata_bd_rdwr(disk_id, method, idx, |
||
289 | size, fs_va); |
||
290 | break; |
||
291 | default: |
||
292 | retval = EINVAL; |
||
293 | break; |
||
294 | } |
||
295 | ipc_answer_0(callid, retval); |
||
296 | } |
||
297 | } |
||
298 | |||
299 | static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t blk_idx, off_t size, |
||
300 | void *buf) |
||
301 | { |
||
302 | int rc; |
||
303 | off_t now; |
||
304 | |||
305 | while (size > 0) { |
||
306 | now = size < block_size ? size : (off_t) block_size; |
||
307 | if (now != block_size) |
||
308 | return EINVAL; |
||
309 | |||
310 | if (method == BD_READ_BLOCK) |
||
311 | rc = ata_bd_read_block(disk_id, blk_idx, 1, buf); |
||
312 | else |
||
313 | rc = ENOTSUP; |
||
314 | |||
315 | if (rc != EOK) |
||
316 | return rc; |
||
317 | |||
318 | buf += block_size; |
||
319 | blk_idx++; |
||
320 | |||
321 | if (size > block_size) |
||
322 | size -= block_size; |
||
323 | else |
||
324 | size = 0; |
||
325 | } |
||
326 | |||
327 | return EOK; |
||
328 | } |
||
329 | |||
330 | |||
331 | static int ata_bd_read_block(int disk_id, uint64_t blk_idx, size_t blk_cnt, |
||
332 | void *buf) |
||
333 | { |
||
334 | size_t i; |
||
335 | uint16_t data; |
||
336 | uint8_t status; |
||
337 | uint64_t c, h, s; |
||
338 | uint64_t idx; |
||
339 | uint8_t drv_head; |
||
4529 | svoboda | 340 | disk_t *d; |
4507 | svoboda | 341 | |
4529 | svoboda | 342 | d = &disk[disk_id]; |
343 | |||
4507 | svoboda | 344 | /* Check device bounds. */ |
4529 | svoboda | 345 | if (blk_idx >= d->blocks) |
4507 | svoboda | 346 | return EINVAL; |
347 | |||
348 | /* Compute CHS. */ |
||
4529 | svoboda | 349 | c = blk_idx / (d->heads * d->sectors); |
350 | idx = blk_idx % (d->heads * d->sectors); |
||
4507 | svoboda | 351 | |
4529 | svoboda | 352 | h = idx / d->sectors; |
353 | s = 1 + (idx % d->sectors); |
||
4507 | svoboda | 354 | |
355 | /* New value for Drive/Head register */ |
||
356 | drv_head = |
||
357 | ((disk_id != 0) ? DHR_DRV : 0) | |
||
358 | (h & 0x0f); |
||
359 | |||
360 | futex_down(&dev_futex); |
||
361 | |||
362 | /* Program a Read Sectors operation. */ |
||
363 | |||
364 | pio_write_8(&cmd->drive_head, drv_head); |
||
365 | pio_write_8(&cmd->sector_count, 1); |
||
366 | pio_write_8(&cmd->sector_number, s); |
||
367 | pio_write_8(&cmd->cylinder_low, c & 0xff); |
||
368 | pio_write_8(&cmd->cylinder_high, c >> 16); |
||
369 | pio_write_8(&cmd->command, 0x20); |
||
370 | |||
371 | /* Read data from the disk buffer. */ |
||
372 | |||
373 | for (i = 0; i < 256; i++) { |
||
374 | do { |
||
375 | status = pio_read_8(&cmd->status); |
||
376 | } while ((status & SR_DRDY) == 0); |
||
377 | |||
378 | data = pio_read_16(&cmd->data_port); |
||
379 | ((uint16_t *) buf)[i] = data; |
||
380 | } |
||
381 | |||
382 | futex_up(&dev_futex); |
||
383 | return EOK; |
||
384 | } |
||
385 | |||
386 | |||
387 | /** |
||
388 | * @} |
||
389 | */ |