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