1 | /*
|
---|
2 | * Copyright (c) 2009 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup bd
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | * @brief ATA disk driver
|
---|
36 | *
|
---|
37 | * This driver supports CHS, 28-bit and 48-bit LBA addressing. It only uses
|
---|
38 | * PIO transfers. There is no support DMA, the PACKET feature set or any other
|
---|
39 | * fancy features such as S.M.A.R.T, removable devices, etc.
|
---|
40 | *
|
---|
41 | * This driver is based on the ATA-1, ATA-2, ATA-3 and ATA/ATAPI-4 through 7
|
---|
42 | * standards, as published by the ANSI, NCITS and INCITS standards bodies,
|
---|
43 | * which are freely available. This driver contains no vendor-specific
|
---|
44 | * code at this moment.
|
---|
45 | *
|
---|
46 | * The driver services a single controller which can have up to two disks
|
---|
47 | * attached.
|
---|
48 | */
|
---|
49 |
|
---|
50 | #include <stdio.h>
|
---|
51 | #include <libarch/ddi.h>
|
---|
52 | #include <ddi.h>
|
---|
53 | #include <ipc/ipc.h>
|
---|
54 | #include <ipc/bd.h>
|
---|
55 | #include <async.h>
|
---|
56 | #include <as.h>
|
---|
57 | #include <fibril_synch.h>
|
---|
58 | #include <string.h>
|
---|
59 | #include <devmap.h>
|
---|
60 | #include <sys/types.h>
|
---|
61 | #include <inttypes.h>
|
---|
62 | #include <errno.h>
|
---|
63 | #include <bool.h>
|
---|
64 | #include <task.h>
|
---|
65 | #include <macros.h>
|
---|
66 |
|
---|
67 | #include "ata_bd.h"
|
---|
68 |
|
---|
69 | #define NAME "ata_bd"
|
---|
70 | #define NAMESPACE "bd"
|
---|
71 |
|
---|
72 | /** Physical block size. Should be always 512. */
|
---|
73 | static const size_t block_size = 512;
|
---|
74 |
|
---|
75 | /** Size of the communication area. */
|
---|
76 | static size_t comm_size;
|
---|
77 |
|
---|
78 | /** I/O base address of the command registers. */
|
---|
79 | static uintptr_t cmd_physical = 0x1f0;
|
---|
80 | /** I/O base address of the control registers. */
|
---|
81 | static uintptr_t ctl_physical = 0x170;
|
---|
82 |
|
---|
83 | static ata_cmd_t *cmd;
|
---|
84 | static ata_ctl_t *ctl;
|
---|
85 |
|
---|
86 | /** Per-disk state. */
|
---|
87 | static disk_t disk[MAX_DISKS];
|
---|
88 |
|
---|
89 | static int ata_bd_init(void);
|
---|
90 | static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
|
---|
91 | static int ata_bd_read_blocks(int disk_id, uint64_t ba, size_t cnt,
|
---|
92 | void *buf);
|
---|
93 | static int ata_bd_write_blocks(int disk_id, uint64_t ba, size_t cnt,
|
---|
94 | const void *buf);
|
---|
95 | static int ata_bd_read_block(int disk_id, uint64_t ba, size_t cnt,
|
---|
96 | void *buf);
|
---|
97 | static int ata_bd_write_block(int disk_id, uint64_t ba, size_t cnt,
|
---|
98 | const void *buf);
|
---|
99 | static int disk_init(disk_t *d, int disk_id);
|
---|
100 | static int drive_identify(int drive_id, void *buf);
|
---|
101 | static void disk_print_summary(disk_t *d);
|
---|
102 | static int coord_calc(disk_t *d, uint64_t ba, block_coord_t *bc);
|
---|
103 | static void coord_sc_program(const block_coord_t *bc, uint16_t scnt);
|
---|
104 | static int wait_status(unsigned set, unsigned n_reset, uint8_t *pstatus,
|
---|
105 | unsigned timeout);
|
---|
106 |
|
---|
107 | int main(int argc, char **argv)
|
---|
108 | {
|
---|
109 | char name[16];
|
---|
110 | int i, rc;
|
---|
111 | int n_disks;
|
---|
112 |
|
---|
113 | printf(NAME ": ATA disk driver\n");
|
---|
114 |
|
---|
115 | printf("I/O address %p/%p\n", ctl_physical, cmd_physical);
|
---|
116 |
|
---|
117 | if (ata_bd_init() != EOK)
|
---|
118 | return -1;
|
---|
119 |
|
---|
120 | for (i = 0; i < MAX_DISKS; i++) {
|
---|
121 | printf("Identify drive %d... ", i);
|
---|
122 | fflush(stdout);
|
---|
123 |
|
---|
124 | rc = disk_init(&disk[i], i);
|
---|
125 |
|
---|
126 | if (rc == EOK) {
|
---|
127 | disk_print_summary(&disk[i]);
|
---|
128 | } else {
|
---|
129 | printf("Not found.\n");
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | n_disks = 0;
|
---|
134 |
|
---|
135 | for (i = 0; i < MAX_DISKS; i++) {
|
---|
136 | /* Skip unattached drives. */
|
---|
137 | if (disk[i].present == false)
|
---|
138 | continue;
|
---|
139 |
|
---|
140 | snprintf(name, 16, "%s/disk%d", NAMESPACE, i);
|
---|
141 | rc = devmap_device_register(name, &disk[i].dev_handle);
|
---|
142 | if (rc != EOK) {
|
---|
143 | devmap_hangup_phone(DEVMAP_DRIVER);
|
---|
144 | printf(NAME ": Unable to register device %s.\n", name);
|
---|
145 | return rc;
|
---|
146 | }
|
---|
147 | ++n_disks;
|
---|
148 | }
|
---|
149 |
|
---|
150 | if (n_disks == 0) {
|
---|
151 | printf("No disks detected.\n");
|
---|
152 | return -1;
|
---|
153 | }
|
---|
154 |
|
---|
155 | printf(NAME ": Accepting connections\n");
|
---|
156 | task_retval(0);
|
---|
157 | async_manager();
|
---|
158 |
|
---|
159 | /* Not reached */
|
---|
160 | return 0;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /** Print one-line device summary. */
|
---|
164 | static void disk_print_summary(disk_t *d)
|
---|
165 | {
|
---|
166 | uint64_t mbytes;
|
---|
167 |
|
---|
168 | printf("%s: ", d->model);
|
---|
169 |
|
---|
170 | switch (d->amode) {
|
---|
171 | case am_chs:
|
---|
172 | printf("CHS %u cylinders, %u heads, %u sectors",
|
---|
173 | disk->geom.cylinders, disk->geom.heads, disk->geom.sectors);
|
---|
174 | break;
|
---|
175 | case am_lba28:
|
---|
176 | printf("LBA-28");
|
---|
177 | break;
|
---|
178 | case am_lba48:
|
---|
179 | printf("LBA-48");
|
---|
180 | break;
|
---|
181 | }
|
---|
182 |
|
---|
183 | printf(" %" PRIu64 " blocks", d->blocks, d->blocks / (2 * 1024));
|
---|
184 |
|
---|
185 | mbytes = d->blocks / (2 * 1024);
|
---|
186 | if (mbytes > 0)
|
---|
187 | printf(" %" PRIu64 " MB.", mbytes);
|
---|
188 |
|
---|
189 | printf("\n");
|
---|
190 | }
|
---|
191 |
|
---|
192 | /** Register driver and enable device I/O. */
|
---|
193 | static int ata_bd_init(void)
|
---|
194 | {
|
---|
195 | void *vaddr;
|
---|
196 | int rc;
|
---|
197 |
|
---|
198 | rc = devmap_driver_register(NAME, ata_bd_connection);
|
---|
199 | if (rc < 0) {
|
---|
200 | printf(NAME ": Unable to register driver.\n");
|
---|
201 | return rc;
|
---|
202 | }
|
---|
203 |
|
---|
204 | rc = pio_enable((void *) cmd_physical, sizeof(ata_cmd_t), &vaddr);
|
---|
205 | if (rc != EOK) {
|
---|
206 | printf(NAME ": Could not initialize device I/O space.\n");
|
---|
207 | return rc;
|
---|
208 | }
|
---|
209 |
|
---|
210 | cmd = vaddr;
|
---|
211 |
|
---|
212 | rc = pio_enable((void *) ctl_physical, sizeof(ata_ctl_t), &vaddr);
|
---|
213 | if (rc != EOK) {
|
---|
214 | printf(NAME ": Could not initialize device I/O space.\n");
|
---|
215 | return rc;
|
---|
216 | }
|
---|
217 |
|
---|
218 | ctl = vaddr;
|
---|
219 |
|
---|
220 |
|
---|
221 | return EOK;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /** Block device connection handler */
|
---|
225 | static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
226 | {
|
---|
227 | void *fs_va = NULL;
|
---|
228 | ipc_callid_t callid;
|
---|
229 | ipc_call_t call;
|
---|
230 | ipcarg_t method;
|
---|
231 | dev_handle_t dh;
|
---|
232 | int flags;
|
---|
233 | int retval;
|
---|
234 | uint64_t ba;
|
---|
235 | size_t cnt;
|
---|
236 | int disk_id, i;
|
---|
237 |
|
---|
238 | /* Get the device handle. */
|
---|
239 | dh = IPC_GET_ARG1(*icall);
|
---|
240 |
|
---|
241 | /* Determine which disk device is the client connecting to. */
|
---|
242 | disk_id = -1;
|
---|
243 | for (i = 0; i < MAX_DISKS; i++)
|
---|
244 | if (disk[i].dev_handle == dh)
|
---|
245 | disk_id = i;
|
---|
246 |
|
---|
247 | if (disk_id < 0 || disk[disk_id].present == false) {
|
---|
248 | ipc_answer_0(iid, EINVAL);
|
---|
249 | return;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /* Answer the IPC_M_CONNECT_ME_TO call. */
|
---|
253 | ipc_answer_0(iid, EOK);
|
---|
254 |
|
---|
255 | if (!async_share_out_receive(&callid, &comm_size, &flags)) {
|
---|
256 | ipc_answer_0(callid, EHANGUP);
|
---|
257 | return;
|
---|
258 | }
|
---|
259 |
|
---|
260 | fs_va = as_get_mappable_page(comm_size);
|
---|
261 | if (fs_va == NULL) {
|
---|
262 | ipc_answer_0(callid, EHANGUP);
|
---|
263 | return;
|
---|
264 | }
|
---|
265 |
|
---|
266 | (void) async_share_out_finalize(callid, fs_va);
|
---|
267 |
|
---|
268 | while (1) {
|
---|
269 | callid = async_get_call(&call);
|
---|
270 | method = IPC_GET_METHOD(call);
|
---|
271 | switch (method) {
|
---|
272 | case IPC_M_PHONE_HUNGUP:
|
---|
273 | /* The other side has hung up. */
|
---|
274 | ipc_answer_0(callid, EOK);
|
---|
275 | return;
|
---|
276 | case BD_READ_BLOCKS:
|
---|
277 | ba = MERGE_LOUP32(IPC_GET_ARG1(call),
|
---|
278 | IPC_GET_ARG2(call));
|
---|
279 | cnt = IPC_GET_ARG3(call);
|
---|
280 | if (cnt * block_size > comm_size) {
|
---|
281 | retval = ELIMIT;
|
---|
282 | break;
|
---|
283 | }
|
---|
284 | retval = ata_bd_read_blocks(disk_id, ba, cnt, fs_va);
|
---|
285 | break;
|
---|
286 | case BD_WRITE_BLOCKS:
|
---|
287 | ba = MERGE_LOUP32(IPC_GET_ARG1(call),
|
---|
288 | IPC_GET_ARG2(call));
|
---|
289 | cnt = IPC_GET_ARG3(call);
|
---|
290 | if (cnt * block_size > comm_size) {
|
---|
291 | retval = ELIMIT;
|
---|
292 | break;
|
---|
293 | }
|
---|
294 | retval = ata_bd_write_blocks(disk_id, ba, cnt, fs_va);
|
---|
295 | break;
|
---|
296 | case BD_GET_BLOCK_SIZE:
|
---|
297 | ipc_answer_1(callid, EOK, block_size);
|
---|
298 | continue;
|
---|
299 | case BD_GET_NUM_BLOCKS:
|
---|
300 | ipc_answer_2(callid, EOK, LOWER32(disk[disk_id].blocks),
|
---|
301 | UPPER32(disk[disk_id].blocks));
|
---|
302 | continue;
|
---|
303 | default:
|
---|
304 | retval = EINVAL;
|
---|
305 | break;
|
---|
306 | }
|
---|
307 | ipc_answer_0(callid, retval);
|
---|
308 | }
|
---|
309 | }
|
---|
310 |
|
---|
311 | /** Initialize a disk.
|
---|
312 | *
|
---|
313 | * Probes for a disk, determines its parameters and initializes
|
---|
314 | * the disk structure.
|
---|
315 | */
|
---|
316 | static int disk_init(disk_t *d, int disk_id)
|
---|
317 | {
|
---|
318 | identify_data_t idata;
|
---|
319 | uint8_t model[40];
|
---|
320 | uint16_t w;
|
---|
321 | uint8_t c;
|
---|
322 | size_t pos, len;
|
---|
323 | int rc;
|
---|
324 | unsigned i;
|
---|
325 |
|
---|
326 | rc = drive_identify(disk_id, &idata);
|
---|
327 | if (rc != EOK) {
|
---|
328 | d->present = false;
|
---|
329 | return rc;
|
---|
330 | }
|
---|
331 |
|
---|
332 | if ((idata.caps & cap_lba) == 0) {
|
---|
333 | /* Device only supports CHS addressing. */
|
---|
334 | d->amode = am_chs;
|
---|
335 |
|
---|
336 | d->geom.cylinders = idata.cylinders;
|
---|
337 | d->geom.heads = idata.heads;
|
---|
338 | d->geom.sectors = idata.sectors;
|
---|
339 |
|
---|
340 | d->blocks = d->geom.cylinders * d->geom.heads * d->geom.sectors;
|
---|
341 | } else if ((idata.cmd_set1 & cs1_addr48) == 0) {
|
---|
342 | /* Device only supports LBA-28 addressing. */
|
---|
343 | d->amode = am_lba28;
|
---|
344 |
|
---|
345 | d->geom.cylinders = 0;
|
---|
346 | d->geom.heads = 0;
|
---|
347 | d->geom.sectors = 0;
|
---|
348 |
|
---|
349 | d->blocks =
|
---|
350 | (uint32_t) idata.total_lba28_0 |
|
---|
351 | ((uint32_t) idata.total_lba28_1 << 16);
|
---|
352 | } else {
|
---|
353 | /* Device supports LBA-48 addressing. */
|
---|
354 | d->amode = am_lba48;
|
---|
355 |
|
---|
356 | d->geom.cylinders = 0;
|
---|
357 | d->geom.heads = 0;
|
---|
358 | d->geom.sectors = 0;
|
---|
359 |
|
---|
360 | d->blocks =
|
---|
361 | (uint64_t) idata.total_lba48_0 |
|
---|
362 | ((uint64_t) idata.total_lba48_1 << 16) |
|
---|
363 | ((uint64_t) idata.total_lba48_2 << 32) |
|
---|
364 | ((uint64_t) idata.total_lba48_3 << 48);
|
---|
365 | }
|
---|
366 |
|
---|
367 | /*
|
---|
368 | * Convert model name to string representation.
|
---|
369 | */
|
---|
370 | for (i = 0; i < 20; i++) {
|
---|
371 | w = idata.model_name[i];
|
---|
372 | model[2 * i] = w >> 8;
|
---|
373 | model[2 * i + 1] = w & 0x00ff;
|
---|
374 | }
|
---|
375 |
|
---|
376 | len = 40;
|
---|
377 | while (len > 0 && model[len - 1] == 0x20)
|
---|
378 | --len;
|
---|
379 |
|
---|
380 | pos = 0;
|
---|
381 | for (i = 0; i < len; ++i) {
|
---|
382 | c = model[i];
|
---|
383 | if (c >= 0x80) c = '?';
|
---|
384 |
|
---|
385 | chr_encode(c, d->model, &pos, 40);
|
---|
386 | }
|
---|
387 | d->model[pos] = '\0';
|
---|
388 |
|
---|
389 | d->present = true;
|
---|
390 | fibril_mutex_initialize(&d->lock);
|
---|
391 |
|
---|
392 | return EOK;
|
---|
393 | }
|
---|
394 |
|
---|
395 | /** Read multiple blocks from the device. */
|
---|
396 | static int ata_bd_read_blocks(int disk_id, uint64_t ba, size_t cnt,
|
---|
397 | void *buf) {
|
---|
398 |
|
---|
399 | int rc;
|
---|
400 |
|
---|
401 | while (cnt > 0) {
|
---|
402 | rc = ata_bd_read_block(disk_id, ba, 1, buf);
|
---|
403 | if (rc != EOK)
|
---|
404 | return rc;
|
---|
405 |
|
---|
406 | ++ba;
|
---|
407 | --cnt;
|
---|
408 | buf += block_size;
|
---|
409 | }
|
---|
410 |
|
---|
411 | return EOK;
|
---|
412 | }
|
---|
413 |
|
---|
414 | /** Write multiple blocks to the device. */
|
---|
415 | static int ata_bd_write_blocks(int disk_id, uint64_t ba, size_t cnt,
|
---|
416 | const void *buf) {
|
---|
417 |
|
---|
418 | int rc;
|
---|
419 |
|
---|
420 | while (cnt > 0) {
|
---|
421 | rc = ata_bd_write_block(disk_id, ba, 1, buf);
|
---|
422 | if (rc != EOK)
|
---|
423 | return rc;
|
---|
424 |
|
---|
425 | ++ba;
|
---|
426 | --cnt;
|
---|
427 | buf += block_size;
|
---|
428 | }
|
---|
429 |
|
---|
430 | return EOK;
|
---|
431 | }
|
---|
432 |
|
---|
433 | /** Issue IDENTIFY command.
|
---|
434 | *
|
---|
435 | * Reads @c identify data into the provided buffer. This is used to detect
|
---|
436 | * whether an ATA device is present and if so, to determine its parameters.
|
---|
437 | *
|
---|
438 | * @param disk_id Device ID, 0 or 1.
|
---|
439 | * @param buf Pointer to a 512-byte buffer.
|
---|
440 | */
|
---|
441 | static int drive_identify(int disk_id, void *buf)
|
---|
442 | {
|
---|
443 | uint16_t data;
|
---|
444 | uint8_t status;
|
---|
445 | uint8_t drv_head;
|
---|
446 | size_t i;
|
---|
447 |
|
---|
448 | drv_head = ((disk_id != 0) ? DHR_DRV : 0);
|
---|
449 |
|
---|
450 | if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK)
|
---|
451 | return EIO;
|
---|
452 |
|
---|
453 | pio_write_8(&cmd->drive_head, drv_head);
|
---|
454 |
|
---|
455 | /*
|
---|
456 | * This is where we would most likely expect a non-existing device to
|
---|
457 | * show up by not setting SR_DRDY.
|
---|
458 | */
|
---|
459 | if (wait_status(SR_DRDY, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK)
|
---|
460 | return EIO;
|
---|
461 |
|
---|
462 | pio_write_8(&cmd->command, CMD_IDENTIFY_DRIVE);
|
---|
463 |
|
---|
464 | if (wait_status(0, ~SR_BSY, &status, TIMEOUT_PROBE) != EOK)
|
---|
465 | return EIO;
|
---|
466 |
|
---|
467 | /* Read data from the disk buffer. */
|
---|
468 |
|
---|
469 | if ((status & SR_DRQ) != 0) {
|
---|
470 | for (i = 0; i < block_size / 2; i++) {
|
---|
471 | data = pio_read_16(&cmd->data_port);
|
---|
472 | ((uint16_t *) buf)[i] = data;
|
---|
473 | }
|
---|
474 | }
|
---|
475 |
|
---|
476 | if ((status & SR_ERR) != 0)
|
---|
477 | return EIO;
|
---|
478 |
|
---|
479 | return EOK;
|
---|
480 | }
|
---|
481 |
|
---|
482 | /** Read a physical from the device.
|
---|
483 | *
|
---|
484 | * @param disk_id Device index (0 or 1)
|
---|
485 | * @param ba Address the first block.
|
---|
486 | * @param cnt Number of blocks to transfer.
|
---|
487 | * @param buf Buffer for holding the data.
|
---|
488 | *
|
---|
489 | * @return EOK on success, EIO on error.
|
---|
490 | */
|
---|
491 | static int ata_bd_read_block(int disk_id, uint64_t ba, size_t blk_cnt,
|
---|
492 | void *buf)
|
---|
493 | {
|
---|
494 | size_t i;
|
---|
495 | uint16_t data;
|
---|
496 | uint8_t status;
|
---|
497 | uint8_t drv_head;
|
---|
498 | disk_t *d;
|
---|
499 | block_coord_t bc;
|
---|
500 |
|
---|
501 | d = &disk[disk_id];
|
---|
502 |
|
---|
503 | /* Silence warning. */
|
---|
504 | memset(&bc, 0, sizeof(bc));
|
---|
505 |
|
---|
506 | /* Compute block coordinates. */
|
---|
507 | if (coord_calc(d, ba, &bc) != EOK)
|
---|
508 | return EINVAL;
|
---|
509 |
|
---|
510 | /* New value for Drive/Head register */
|
---|
511 | drv_head =
|
---|
512 | ((disk_id != 0) ? DHR_DRV : 0) |
|
---|
513 | ((d->amode != am_chs) ? DHR_LBA : 0) |
|
---|
514 | (bc.h & 0x0f);
|
---|
515 |
|
---|
516 | fibril_mutex_lock(&d->lock);
|
---|
517 |
|
---|
518 | /* Program a Read Sectors operation. */
|
---|
519 |
|
---|
520 | if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_BSY) != EOK) {
|
---|
521 | fibril_mutex_unlock(&d->lock);
|
---|
522 | return EIO;
|
---|
523 | }
|
---|
524 |
|
---|
525 | pio_write_8(&cmd->drive_head, drv_head);
|
---|
526 |
|
---|
527 | if (wait_status(SR_DRDY, ~SR_BSY, NULL, TIMEOUT_DRDY) != EOK) {
|
---|
528 | fibril_mutex_unlock(&d->lock);
|
---|
529 | return EIO;
|
---|
530 | }
|
---|
531 |
|
---|
532 | /* Program block coordinates into the device. */
|
---|
533 | coord_sc_program(&bc, 1);
|
---|
534 |
|
---|
535 | pio_write_8(&cmd->command, d->amode == am_lba48 ?
|
---|
536 | CMD_READ_SECTORS_EXT : CMD_READ_SECTORS);
|
---|
537 |
|
---|
538 | if (wait_status(0, ~SR_BSY, &status, TIMEOUT_BSY) != EOK) {
|
---|
539 | fibril_mutex_unlock(&d->lock);
|
---|
540 | return EIO;
|
---|
541 | }
|
---|
542 |
|
---|
543 | if ((status & SR_DRQ) != 0) {
|
---|
544 | /* Read data from the device buffer. */
|
---|
545 |
|
---|
546 | for (i = 0; i < block_size / 2; i++) {
|
---|
547 | data = pio_read_16(&cmd->data_port);
|
---|
548 | ((uint16_t *) buf)[i] = data;
|
---|
549 | }
|
---|
550 | }
|
---|
551 |
|
---|
552 | if ((status & SR_ERR) != 0)
|
---|
553 | return EIO;
|
---|
554 |
|
---|
555 | fibril_mutex_unlock(&d->lock);
|
---|
556 | return EOK;
|
---|
557 | }
|
---|
558 |
|
---|
559 | /** Write a physical block to the device.
|
---|
560 | *
|
---|
561 | * @param disk_id Device index (0 or 1)
|
---|
562 | * @param ba Address of the first block.
|
---|
563 | * @param cnt Number of blocks to transfer.
|
---|
564 | * @param buf Buffer holding the data to write.
|
---|
565 | *
|
---|
566 | * @return EOK on success, EIO on error.
|
---|
567 | */
|
---|
568 | static int ata_bd_write_block(int disk_id, uint64_t ba, size_t cnt,
|
---|
569 | const void *buf)
|
---|
570 | {
|
---|
571 | size_t i;
|
---|
572 | uint8_t status;
|
---|
573 | uint8_t drv_head;
|
---|
574 | disk_t *d;
|
---|
575 | block_coord_t bc;
|
---|
576 |
|
---|
577 | d = &disk[disk_id];
|
---|
578 |
|
---|
579 | /* Silence warning. */
|
---|
580 | memset(&bc, 0, sizeof(bc));
|
---|
581 |
|
---|
582 | /* Compute block coordinates. */
|
---|
583 | if (coord_calc(d, ba, &bc) != EOK)
|
---|
584 | return EINVAL;
|
---|
585 |
|
---|
586 | /* New value for Drive/Head register */
|
---|
587 | drv_head =
|
---|
588 | ((disk_id != 0) ? DHR_DRV : 0) |
|
---|
589 | ((d->amode != am_chs) ? DHR_LBA : 0) |
|
---|
590 | (bc.h & 0x0f);
|
---|
591 |
|
---|
592 | fibril_mutex_lock(&d->lock);
|
---|
593 |
|
---|
594 | /* Program a Write Sectors operation. */
|
---|
595 |
|
---|
596 | if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_BSY) != EOK) {
|
---|
597 | fibril_mutex_unlock(&d->lock);
|
---|
598 | return EIO;
|
---|
599 | }
|
---|
600 |
|
---|
601 | pio_write_8(&cmd->drive_head, drv_head);
|
---|
602 |
|
---|
603 | if (wait_status(SR_DRDY, ~SR_BSY, NULL, TIMEOUT_DRDY) != EOK) {
|
---|
604 | fibril_mutex_unlock(&d->lock);
|
---|
605 | return EIO;
|
---|
606 | }
|
---|
607 |
|
---|
608 | /* Program block coordinates into the device. */
|
---|
609 | coord_sc_program(&bc, 1);
|
---|
610 |
|
---|
611 | pio_write_8(&cmd->command, d->amode == am_lba48 ?
|
---|
612 | CMD_WRITE_SECTORS_EXT : CMD_WRITE_SECTORS);
|
---|
613 |
|
---|
614 | if (wait_status(0, ~SR_BSY, &status, TIMEOUT_BSY) != EOK) {
|
---|
615 | fibril_mutex_unlock(&d->lock);
|
---|
616 | return EIO;
|
---|
617 | }
|
---|
618 |
|
---|
619 | if ((status & SR_DRQ) != 0) {
|
---|
620 | /* Write data to the device buffer. */
|
---|
621 |
|
---|
622 | for (i = 0; i < block_size / 2; i++) {
|
---|
623 | pio_write_16(&cmd->data_port, ((uint16_t *) buf)[i]);
|
---|
624 | }
|
---|
625 | }
|
---|
626 |
|
---|
627 | fibril_mutex_unlock(&d->lock);
|
---|
628 |
|
---|
629 | if (status & SR_ERR)
|
---|
630 | return EIO;
|
---|
631 |
|
---|
632 | return EOK;
|
---|
633 | }
|
---|
634 |
|
---|
635 | /** Calculate block coordinates.
|
---|
636 | *
|
---|
637 | * Calculates block coordinates in the best coordinate system supported
|
---|
638 | * by the device. These can be later programmed into the device using
|
---|
639 | * @c coord_sc_program().
|
---|
640 | *
|
---|
641 | * @return EOK on success or EINVAL if block index is past end of device.
|
---|
642 | */
|
---|
643 | static int coord_calc(disk_t *d, uint64_t ba, block_coord_t *bc)
|
---|
644 | {
|
---|
645 | uint64_t c;
|
---|
646 | uint64_t idx;
|
---|
647 |
|
---|
648 | /* Check device bounds. */
|
---|
649 | if (ba >= d->blocks)
|
---|
650 | return EINVAL;
|
---|
651 |
|
---|
652 | bc->amode = d->amode;
|
---|
653 |
|
---|
654 | switch (d->amode) {
|
---|
655 | case am_chs:
|
---|
656 | /* Compute CHS coordinates. */
|
---|
657 | c = ba / (d->geom.heads * d->geom.sectors);
|
---|
658 | idx = ba % (d->geom.heads * d->geom.sectors);
|
---|
659 |
|
---|
660 | bc->cyl_lo = c & 0xff;
|
---|
661 | bc->cyl_hi = (c >> 8) & 0xff;
|
---|
662 | bc->h = (idx / d->geom.sectors) & 0x0f;
|
---|
663 | bc->sector = (1 + (idx % d->geom.sectors)) & 0xff;
|
---|
664 | break;
|
---|
665 |
|
---|
666 | case am_lba28:
|
---|
667 | /* Compute LBA-28 coordinates. */
|
---|
668 | bc->c0 = ba & 0xff; /* bits 0-7 */
|
---|
669 | bc->c1 = (ba >> 8) & 0xff; /* bits 8-15 */
|
---|
670 | bc->c2 = (ba >> 16) & 0xff; /* bits 16-23 */
|
---|
671 | bc->h = (ba >> 24) & 0x0f; /* bits 24-27 */
|
---|
672 | break;
|
---|
673 |
|
---|
674 | case am_lba48:
|
---|
675 | /* Compute LBA-48 coordinates. */
|
---|
676 | bc->c0 = ba & 0xff; /* bits 0-7 */
|
---|
677 | bc->c1 = (ba >> 8) & 0xff; /* bits 8-15 */
|
---|
678 | bc->c2 = (ba >> 16) & 0xff; /* bits 16-23 */
|
---|
679 | bc->c3 = (ba >> 24) & 0xff; /* bits 24-31 */
|
---|
680 | bc->c4 = (ba >> 32) & 0xff; /* bits 32-39 */
|
---|
681 | bc->c5 = (ba >> 40) & 0xff; /* bits 40-47 */
|
---|
682 | bc->h = 0;
|
---|
683 | break;
|
---|
684 | }
|
---|
685 |
|
---|
686 | return EOK;
|
---|
687 | }
|
---|
688 |
|
---|
689 | /** Program block coordinates and sector count into ATA registers.
|
---|
690 | *
|
---|
691 | * Note that bc->h must be programmed separately into the device/head register.
|
---|
692 | */
|
---|
693 | static void coord_sc_program(const block_coord_t *bc, uint16_t scnt)
|
---|
694 | {
|
---|
695 | if (bc->amode == am_lba48) {
|
---|
696 | /* Write high-order bits. */
|
---|
697 | pio_write_8(&cmd->sector_count, scnt >> 8);
|
---|
698 | pio_write_8(&cmd->sector_number, bc->c3);
|
---|
699 | pio_write_8(&cmd->cylinder_low, bc->c4);
|
---|
700 | pio_write_8(&cmd->cylinder_high, bc->c5);
|
---|
701 | }
|
---|
702 |
|
---|
703 | /* Write low-order bits. */
|
---|
704 | pio_write_8(&cmd->sector_count, scnt & 0x00ff);
|
---|
705 | pio_write_8(&cmd->sector_number, bc->c0);
|
---|
706 | pio_write_8(&cmd->cylinder_low, bc->c1);
|
---|
707 | pio_write_8(&cmd->cylinder_high, bc->c2);
|
---|
708 | }
|
---|
709 |
|
---|
710 | /** Wait until some status bits are set and some are reset.
|
---|
711 | *
|
---|
712 | * Example: wait_status(SR_DRDY, ~SR_BSY) waits for SR_DRDY to become
|
---|
713 | * set and SR_BSY to become reset.
|
---|
714 | *
|
---|
715 | * @param set Combination if bits which must be all set.
|
---|
716 | * @param n_reset Negated combination of bits which must be all reset.
|
---|
717 | * @param pstatus Pointer where to store last read status or NULL.
|
---|
718 | * @param timeout Timeout in 10ms units.
|
---|
719 | *
|
---|
720 | * @return EOK on success, EIO on timeout.
|
---|
721 | */
|
---|
722 | static int wait_status(unsigned set, unsigned n_reset, uint8_t *pstatus,
|
---|
723 | unsigned timeout)
|
---|
724 | {
|
---|
725 | uint8_t status;
|
---|
726 | int cnt;
|
---|
727 |
|
---|
728 | status = pio_read_8(&cmd->status);
|
---|
729 |
|
---|
730 | /*
|
---|
731 | * This is crude, yet simple. First try with 1us delays
|
---|
732 | * (most likely the device will respond very fast). If not,
|
---|
733 | * start trying every 10 ms.
|
---|
734 | */
|
---|
735 |
|
---|
736 | cnt = 100;
|
---|
737 | while ((status & ~n_reset) != 0 || (status & set) != set) {
|
---|
738 | async_usleep(1);
|
---|
739 | --cnt;
|
---|
740 | if (cnt <= 0) break;
|
---|
741 |
|
---|
742 | status = pio_read_8(&cmd->status);
|
---|
743 | }
|
---|
744 |
|
---|
745 | cnt = timeout;
|
---|
746 | while ((status & ~n_reset) != 0 || (status & set) != set) {
|
---|
747 | async_usleep(10000);
|
---|
748 | --cnt;
|
---|
749 | if (cnt <= 0) break;
|
---|
750 |
|
---|
751 | status = pio_read_8(&cmd->status);
|
---|
752 | }
|
---|
753 |
|
---|
754 | if (pstatus)
|
---|
755 | *pstatus = status;
|
---|
756 |
|
---|
757 | if (cnt == 0)
|
---|
758 | return EIO;
|
---|
759 |
|
---|
760 | return EOK;
|
---|
761 | }
|
---|
762 |
|
---|
763 | /**
|
---|
764 | * @}
|
---|
765 | */
|
---|