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