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