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