source: mainline/uspace/srv/bd/ata_bd/ata_bd.c@ 7a56e33e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7a56e33e was 7a56e33e, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Detection of ATA packet devices.

  • Property mode set to 100644
File size: 20.5 KB
Line 
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 <str.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_hw.h"
68#include "ata_bd.h"
69
70#define NAME "ata_bd"
71#define NAMESPACE "bd"
72
73/** Number of defined legacy controller base addresses. */
74#define LEGACY_CTLS 4
75
76/** Physical block size. Should be always 512. */
77static const size_t block_size = 512;
78
79/** Size of the communication area. */
80static size_t comm_size;
81
82/** I/O base address of the command registers. */
83static uintptr_t cmd_physical;
84/** I/O base address of the control registers. */
85static uintptr_t ctl_physical;
86
87/** I/O base addresses for legacy (ISA-compatible) controllers. */
88static ata_base_t legacy_base[LEGACY_CTLS] = {
89 { 0x1f0, 0x3f0 },
90 { 0x170, 0x370 },
91 { 0x1e8, 0x3e8 },
92 { 0x168, 0x368 }
93};
94
95static ata_cmd_t *cmd;
96static ata_ctl_t *ctl;
97
98/** Per-disk state. */
99static disk_t disk[MAX_DISKS];
100
101static void print_syntax(void);
102static int ata_bd_init(void);
103static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
104static int ata_bd_read_blocks(int disk_id, uint64_t ba, size_t cnt,
105 void *buf);
106static int ata_bd_write_blocks(int disk_id, uint64_t ba, size_t cnt,
107 const void *buf);
108static int ata_bd_read_block(int disk_id, uint64_t ba, size_t cnt,
109 void *buf);
110static int ata_bd_write_block(int disk_id, uint64_t ba, size_t cnt,
111 const void *buf);
112static int disk_init(disk_t *d, int disk_id);
113static int drive_identify(int drive_id, void *buf);
114static int identify_pkt_dev(int dev_idx, void *buf);
115static void disk_print_summary(disk_t *d);
116static int coord_calc(disk_t *d, uint64_t ba, block_coord_t *bc);
117static void coord_sc_program(const block_coord_t *bc, uint16_t scnt);
118static int wait_status(unsigned set, unsigned n_reset, uint8_t *pstatus,
119 unsigned timeout);
120
121int main(int argc, char **argv)
122{
123 char name[16];
124 int i, rc;
125 int n_disks;
126 unsigned ctl_num;
127 char *eptr;
128
129 printf(NAME ": ATA disk driver\n");
130
131 if (argc > 1) {
132 ctl_num = strtoul(argv[1], &eptr, 0);
133 if (*eptr != '\0' || ctl_num == 0 || ctl_num > 4) {
134 printf("Invalid argument.\n");
135 print_syntax();
136 return -1;
137 }
138 } else {
139 ctl_num = 1;
140 }
141
142 cmd_physical = legacy_base[ctl_num - 1].cmd;
143 ctl_physical = legacy_base[ctl_num - 1].ctl;
144
145 printf("I/O address %p/%p\n", (void *) cmd_physical,
146 (void *) ctl_physical);
147
148 if (ata_bd_init() != EOK)
149 return -1;
150
151 for (i = 0; i < MAX_DISKS; i++) {
152 printf("Identify drive %d... ", i);
153 fflush(stdout);
154
155 rc = disk_init(&disk[i], i);
156
157 if (rc == EOK) {
158 disk_print_summary(&disk[i]);
159 } else {
160 printf("Not found.\n");
161 }
162 }
163
164 n_disks = 0;
165
166 for (i = 0; i < MAX_DISKS; i++) {
167 /* Skip unattached drives. */
168 if (disk[i].present == false)
169 continue;
170
171 snprintf(name, 16, "%s/ata%udisk%d", NAMESPACE, ctl_num, i);
172 rc = devmap_device_register(name, &disk[i].devmap_handle);
173 if (rc != EOK) {
174 printf(NAME ": Unable to register device %s.\n", name);
175 return rc;
176 }
177 ++n_disks;
178 }
179
180 if (n_disks == 0) {
181 printf("No disks detected.\n");
182 return -1;
183 }
184
185 printf(NAME ": Accepting connections\n");
186 task_retval(0);
187 async_manager();
188
189 /* Not reached */
190 return 0;
191}
192
193
194static void print_syntax(void)
195{
196 printf("Syntax: " NAME " <controller_number>\n");
197 printf("Controller number = 1..4\n");
198}
199
200/** Print one-line device summary. */
201static void disk_print_summary(disk_t *d)
202{
203 uint64_t mbytes;
204
205 printf("%s: ", d->model);
206
207 if (d->dev_type == ata_reg_dev) {
208 switch (d->amode) {
209 case am_chs:
210 printf("CHS %u cylinders, %u heads, %u sectors",
211 disk->geom.cylinders, disk->geom.heads,
212 disk->geom.sectors);
213 break;
214 case am_lba28:
215 printf("LBA-28");
216 break;
217 case am_lba48:
218 printf("LBA-48");
219 break;
220 }
221 } else {
222 printf("PACKET");
223 }
224
225 printf(" %" PRIu64 " blocks", d->blocks);
226
227 mbytes = d->blocks / (2 * 1024);
228 if (mbytes > 0)
229 printf(" %" PRIu64 " MB.", mbytes);
230
231 printf("\n");
232}
233
234/** Register driver and enable device I/O. */
235static int ata_bd_init(void)
236{
237 void *vaddr;
238 int rc;
239
240 rc = devmap_driver_register(NAME, ata_bd_connection);
241 if (rc < 0) {
242 printf(NAME ": Unable to register driver.\n");
243 return rc;
244 }
245
246 rc = pio_enable((void *) cmd_physical, sizeof(ata_cmd_t), &vaddr);
247 if (rc != EOK) {
248 printf(NAME ": Could not initialize device I/O space.\n");
249 return rc;
250 }
251
252 cmd = vaddr;
253
254 rc = pio_enable((void *) ctl_physical, sizeof(ata_ctl_t), &vaddr);
255 if (rc != EOK) {
256 printf(NAME ": Could not initialize device I/O space.\n");
257 return rc;
258 }
259
260 ctl = vaddr;
261
262
263 return EOK;
264}
265
266/** Block device connection handler */
267static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
268{
269 void *fs_va = NULL;
270 ipc_callid_t callid;
271 ipc_call_t call;
272 sysarg_t method;
273 devmap_handle_t dh;
274 int flags;
275 int retval;
276 uint64_t ba;
277 size_t cnt;
278 int disk_id, i;
279
280 /* Get the device handle. */
281 dh = IPC_GET_ARG1(*icall);
282
283 /* Determine which disk device is the client connecting to. */
284 disk_id = -1;
285 for (i = 0; i < MAX_DISKS; i++)
286 if (disk[i].devmap_handle == dh)
287 disk_id = i;
288
289 if (disk_id < 0 || disk[disk_id].present == false) {
290 ipc_answer_0(iid, EINVAL);
291 return;
292 }
293
294 /* Answer the IPC_M_CONNECT_ME_TO call. */
295 ipc_answer_0(iid, EOK);
296
297 if (!async_share_out_receive(&callid, &comm_size, &flags)) {
298 ipc_answer_0(callid, EHANGUP);
299 return;
300 }
301
302 fs_va = as_get_mappable_page(comm_size);
303 if (fs_va == NULL) {
304 ipc_answer_0(callid, EHANGUP);
305 return;
306 }
307
308 (void) async_share_out_finalize(callid, fs_va);
309
310 while (1) {
311 callid = async_get_call(&call);
312 method = IPC_GET_IMETHOD(call);
313 switch (method) {
314 case IPC_M_PHONE_HUNGUP:
315 /* The other side has hung up. */
316 ipc_answer_0(callid, EOK);
317 return;
318 case BD_READ_BLOCKS:
319 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
320 IPC_GET_ARG2(call));
321 cnt = IPC_GET_ARG3(call);
322 if (cnt * block_size > comm_size) {
323 retval = ELIMIT;
324 break;
325 }
326 retval = ata_bd_read_blocks(disk_id, ba, cnt, fs_va);
327 break;
328 case BD_WRITE_BLOCKS:
329 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
330 IPC_GET_ARG2(call));
331 cnt = IPC_GET_ARG3(call);
332 if (cnt * block_size > comm_size) {
333 retval = ELIMIT;
334 break;
335 }
336 retval = ata_bd_write_blocks(disk_id, ba, cnt, fs_va);
337 break;
338 case BD_GET_BLOCK_SIZE:
339 ipc_answer_1(callid, EOK, block_size);
340 continue;
341 case BD_GET_NUM_BLOCKS:
342 ipc_answer_2(callid, EOK, LOWER32(disk[disk_id].blocks),
343 UPPER32(disk[disk_id].blocks));
344 continue;
345 default:
346 retval = EINVAL;
347 break;
348 }
349 ipc_answer_0(callid, retval);
350 }
351}
352
353/** Initialize a disk.
354 *
355 * Probes for a disk, determines its parameters and initializes
356 * the disk structure.
357 */
358static int disk_init(disk_t *d, int disk_id)
359{
360 identify_data_t idata;
361 uint8_t model[40];
362 uint16_t w;
363 uint8_t c;
364 size_t pos, len;
365 int rc;
366 unsigned i;
367
368 d->present = false;
369
370 /* Try identify command. */
371 rc = drive_identify(disk_id, &idata);
372 if (rc == EOK) {
373 /* Success. It's a register (non-packet) device. */
374 printf("ATA register-only device found.\n");
375 d->present = true;
376 d->dev_type = ata_reg_dev;
377 } else if (rc == EIO) {
378 /*
379 * There is something, but not a register device.
380 * It could be a packet device.
381 */
382 rc = identify_pkt_dev(disk_id, &idata);
383 if (rc == EOK) {
384 /* We have a packet device. */
385 d->present = true;
386 d->dev_type = ata_pkt_dev;
387 } else {
388 /* Nope. Something's there, but not recognized. */
389 }
390 } else {
391 /* Operation timed out. That means there is no device there. */
392 }
393
394 if (d->present == false)
395 return EIO;
396
397 printf("device caps: 0x%04x\n", idata.caps);
398 if (d->dev_type == ata_pkt_dev) {
399 /* Packet device */
400 d->amode = 0;
401
402 d->geom.cylinders = 0;
403 d->geom.heads = 0;
404 d->geom.sectors = 0;
405
406 d->blocks = 0;
407 } else if ((idata.caps & rd_cap_lba) == 0) {
408 /* Device only supports CHS addressing. */
409 d->amode = am_chs;
410
411 d->geom.cylinders = idata.cylinders;
412 d->geom.heads = idata.heads;
413 d->geom.sectors = idata.sectors;
414
415 d->blocks = d->geom.cylinders * d->geom.heads * d->geom.sectors;
416 } else if ((idata.cmd_set1 & cs1_addr48) == 0) {
417 /* Device only supports LBA-28 addressing. */
418 d->amode = am_lba28;
419
420 d->geom.cylinders = 0;
421 d->geom.heads = 0;
422 d->geom.sectors = 0;
423
424 d->blocks =
425 (uint32_t) idata.total_lba28_0 |
426 ((uint32_t) idata.total_lba28_1 << 16);
427 } else {
428 /* Device supports LBA-48 addressing. */
429 d->amode = am_lba48;
430
431 d->geom.cylinders = 0;
432 d->geom.heads = 0;
433 d->geom.sectors = 0;
434
435 d->blocks =
436 (uint64_t) idata.total_lba48_0 |
437 ((uint64_t) idata.total_lba48_1 << 16) |
438 ((uint64_t) idata.total_lba48_2 << 32) |
439 ((uint64_t) idata.total_lba48_3 << 48);
440 }
441
442 /*
443 * Convert model name to string representation.
444 */
445 for (i = 0; i < 20; i++) {
446 w = idata.model_name[i];
447 model[2 * i] = w >> 8;
448 model[2 * i + 1] = w & 0x00ff;
449 }
450
451 len = 40;
452 while (len > 0 && model[len - 1] == 0x20)
453 --len;
454
455 pos = 0;
456 for (i = 0; i < len; ++i) {
457 c = model[i];
458 if (c >= 0x80) c = '?';
459
460 chr_encode(c, d->model, &pos, 40);
461 }
462 d->model[pos] = '\0';
463
464 d->present = true;
465 fibril_mutex_initialize(&d->lock);
466
467 return EOK;
468}
469
470/** Read multiple blocks from the device. */
471static int ata_bd_read_blocks(int disk_id, uint64_t ba, size_t cnt,
472 void *buf) {
473
474 int rc;
475
476 while (cnt > 0) {
477 rc = ata_bd_read_block(disk_id, ba, 1, buf);
478 if (rc != EOK)
479 return rc;
480
481 ++ba;
482 --cnt;
483 buf += block_size;
484 }
485
486 return EOK;
487}
488
489/** Write multiple blocks to the device. */
490static int ata_bd_write_blocks(int disk_id, uint64_t ba, size_t cnt,
491 const void *buf) {
492
493 int rc;
494
495 while (cnt > 0) {
496 rc = ata_bd_write_block(disk_id, ba, 1, buf);
497 if (rc != EOK)
498 return rc;
499
500 ++ba;
501 --cnt;
502 buf += block_size;
503 }
504
505 return EOK;
506}
507
508/** Issue IDENTIFY command.
509 *
510 * Reads @c identify data into the provided buffer. This is used to detect
511 * whether an ATA device is present and if so, to determine its parameters.
512 *
513 * @param disk_id Device ID, 0 or 1.
514 * @param buf Pointer to a 512-byte buffer.
515 *
516 * @return ETIMEOUT on timeout (this can mean the device is
517 * not present). EIO if device responds with error.
518 */
519static int drive_identify(int disk_id, void *buf)
520{
521 uint16_t data;
522 uint8_t status;
523 uint8_t drv_head;
524 size_t i;
525
526 drv_head = ((disk_id != 0) ? DHR_DRV : 0);
527
528 if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK)
529 return ETIMEOUT;
530
531 pio_write_8(&cmd->drive_head, drv_head);
532
533 /*
534 * This is where we would most likely expect a non-existing device to
535 * show up by not setting SR_DRDY.
536 */
537 if (wait_status(SR_DRDY, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK)
538 return ETIMEOUT;
539
540 pio_write_8(&cmd->command, CMD_IDENTIFY_DRIVE);
541
542 if (wait_status(0, ~SR_BSY, &status, TIMEOUT_PROBE) != EOK)
543 return ETIMEOUT;
544
545 /* Read data from the disk buffer. */
546
547 if ((status & SR_DRQ) != 0) {
548 for (i = 0; i < block_size / 2; i++) {
549 data = pio_read_16(&cmd->data_port);
550 ((uint16_t *) buf)[i] = data;
551 }
552 }
553
554 if ((status & SR_ERR) != 0) {
555 return EIO;
556 }
557
558 return EOK;
559}
560
561/** Issue Identify Packet Device command.
562 *
563 * Reads @c identify data into the provided buffer. This is used to detect
564 * whether an ATAPI device is present and if so, to determine its parameters.
565 *
566 * @param dev_idx Device index, 0 or 1.
567 * @param buf Pointer to a 512-byte buffer.
568 */
569static int identify_pkt_dev(int dev_idx, void *buf)
570{
571 uint16_t data;
572 uint8_t status;
573 uint8_t drv_head;
574 size_t i;
575
576 drv_head = ((dev_idx != 0) ? DHR_DRV : 0);
577
578 if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK)
579 return EIO;
580
581 pio_write_8(&cmd->drive_head, drv_head);
582
583 /* For ATAPI commands we do not need to wait for DRDY. */
584 if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK)
585 return EIO;
586
587 pio_write_8(&cmd->command, CMD_IDENTIFY_PKT_DEV);
588
589 if (wait_status(0, ~SR_BSY, &status, TIMEOUT_BSY) != EOK)
590 return EIO;
591
592 /* Read data from the device buffer. */
593
594 if ((status & SR_DRQ) != 0) {
595 for (i = 0; i < block_size / 2; i++) {
596 data = pio_read_16(&cmd->data_port);
597 ((uint16_t *) buf)[i] = data;
598 }
599 }
600
601 if ((status & SR_ERR) != 0)
602 return EIO;
603
604 return EOK;
605}
606
607/** Read a physical from the device.
608 *
609 * @param disk_id Device index (0 or 1)
610 * @param ba Address the first block.
611 * @param cnt Number of blocks to transfer.
612 * @param buf Buffer for holding the data.
613 *
614 * @return EOK on success, EIO on error.
615 */
616static int ata_bd_read_block(int disk_id, uint64_t ba, size_t blk_cnt,
617 void *buf)
618{
619 size_t i;
620 uint16_t data;
621 uint8_t status;
622 uint8_t drv_head;
623 disk_t *d;
624 block_coord_t bc;
625
626 d = &disk[disk_id];
627
628 /* Silence warning. */
629 memset(&bc, 0, sizeof(bc));
630
631 /* Compute block coordinates. */
632 if (coord_calc(d, ba, &bc) != EOK)
633 return EINVAL;
634
635 /* New value for Drive/Head register */
636 drv_head =
637 ((disk_id != 0) ? DHR_DRV : 0) |
638 ((d->amode != am_chs) ? DHR_LBA : 0) |
639 (bc.h & 0x0f);
640
641 fibril_mutex_lock(&d->lock);
642
643 /* Program a Read Sectors operation. */
644
645 if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_BSY) != EOK) {
646 fibril_mutex_unlock(&d->lock);
647 return EIO;
648 }
649
650 pio_write_8(&cmd->drive_head, drv_head);
651
652 if (wait_status(SR_DRDY, ~SR_BSY, NULL, TIMEOUT_DRDY) != EOK) {
653 fibril_mutex_unlock(&d->lock);
654 return EIO;
655 }
656
657 /* Program block coordinates into the device. */
658 coord_sc_program(&bc, 1);
659
660 pio_write_8(&cmd->command, d->amode == am_lba48 ?
661 CMD_READ_SECTORS_EXT : CMD_READ_SECTORS);
662
663 if (wait_status(0, ~SR_BSY, &status, TIMEOUT_BSY) != EOK) {
664 fibril_mutex_unlock(&d->lock);
665 return EIO;
666 }
667
668 if ((status & SR_DRQ) != 0) {
669 /* Read data from the device buffer. */
670
671 for (i = 0; i < block_size / 2; i++) {
672 data = pio_read_16(&cmd->data_port);
673 ((uint16_t *) buf)[i] = data;
674 }
675 }
676
677 if ((status & SR_ERR) != 0)
678 return EIO;
679
680 fibril_mutex_unlock(&d->lock);
681 return EOK;
682}
683
684/** Write a physical block to the device.
685 *
686 * @param disk_id Device index (0 or 1)
687 * @param ba Address of the first block.
688 * @param cnt Number of blocks to transfer.
689 * @param buf Buffer holding the data to write.
690 *
691 * @return EOK on success, EIO on error.
692 */
693static int ata_bd_write_block(int disk_id, uint64_t ba, size_t cnt,
694 const void *buf)
695{
696 size_t i;
697 uint8_t status;
698 uint8_t drv_head;
699 disk_t *d;
700 block_coord_t bc;
701
702 d = &disk[disk_id];
703
704 /* Silence warning. */
705 memset(&bc, 0, sizeof(bc));
706
707 /* Compute block coordinates. */
708 if (coord_calc(d, ba, &bc) != EOK)
709 return EINVAL;
710
711 /* New value for Drive/Head register */
712 drv_head =
713 ((disk_id != 0) ? DHR_DRV : 0) |
714 ((d->amode != am_chs) ? DHR_LBA : 0) |
715 (bc.h & 0x0f);
716
717 fibril_mutex_lock(&d->lock);
718
719 /* Program a Write Sectors operation. */
720
721 if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_BSY) != EOK) {
722 fibril_mutex_unlock(&d->lock);
723 return EIO;
724 }
725
726 pio_write_8(&cmd->drive_head, drv_head);
727
728 if (wait_status(SR_DRDY, ~SR_BSY, NULL, TIMEOUT_DRDY) != EOK) {
729 fibril_mutex_unlock(&d->lock);
730 return EIO;
731 }
732
733 /* Program block coordinates into the device. */
734 coord_sc_program(&bc, 1);
735
736 pio_write_8(&cmd->command, d->amode == am_lba48 ?
737 CMD_WRITE_SECTORS_EXT : CMD_WRITE_SECTORS);
738
739 if (wait_status(0, ~SR_BSY, &status, TIMEOUT_BSY) != EOK) {
740 fibril_mutex_unlock(&d->lock);
741 return EIO;
742 }
743
744 if ((status & SR_DRQ) != 0) {
745 /* Write data to the device buffer. */
746
747 for (i = 0; i < block_size / 2; i++) {
748 pio_write_16(&cmd->data_port, ((uint16_t *) buf)[i]);
749 }
750 }
751
752 fibril_mutex_unlock(&d->lock);
753
754 if (status & SR_ERR)
755 return EIO;
756
757 return EOK;
758}
759
760/** Calculate block coordinates.
761 *
762 * Calculates block coordinates in the best coordinate system supported
763 * by the device. These can be later programmed into the device using
764 * @c coord_sc_program().
765 *
766 * @return EOK on success or EINVAL if block index is past end of device.
767 */
768static int coord_calc(disk_t *d, uint64_t ba, block_coord_t *bc)
769{
770 uint64_t c;
771 uint64_t idx;
772
773 /* Check device bounds. */
774 if (ba >= d->blocks)
775 return EINVAL;
776
777 bc->amode = d->amode;
778
779 switch (d->amode) {
780 case am_chs:
781 /* Compute CHS coordinates. */
782 c = ba / (d->geom.heads * d->geom.sectors);
783 idx = ba % (d->geom.heads * d->geom.sectors);
784
785 bc->cyl_lo = c & 0xff;
786 bc->cyl_hi = (c >> 8) & 0xff;
787 bc->h = (idx / d->geom.sectors) & 0x0f;
788 bc->sector = (1 + (idx % d->geom.sectors)) & 0xff;
789 break;
790
791 case am_lba28:
792 /* Compute LBA-28 coordinates. */
793 bc->c0 = ba & 0xff; /* bits 0-7 */
794 bc->c1 = (ba >> 8) & 0xff; /* bits 8-15 */
795 bc->c2 = (ba >> 16) & 0xff; /* bits 16-23 */
796 bc->h = (ba >> 24) & 0x0f; /* bits 24-27 */
797 break;
798
799 case am_lba48:
800 /* Compute LBA-48 coordinates. */
801 bc->c0 = ba & 0xff; /* bits 0-7 */
802 bc->c1 = (ba >> 8) & 0xff; /* bits 8-15 */
803 bc->c2 = (ba >> 16) & 0xff; /* bits 16-23 */
804 bc->c3 = (ba >> 24) & 0xff; /* bits 24-31 */
805 bc->c4 = (ba >> 32) & 0xff; /* bits 32-39 */
806 bc->c5 = (ba >> 40) & 0xff; /* bits 40-47 */
807 bc->h = 0;
808 break;
809 }
810
811 return EOK;
812}
813
814/** Program block coordinates and sector count into ATA registers.
815 *
816 * Note that bc->h must be programmed separately into the device/head register.
817 */
818static void coord_sc_program(const block_coord_t *bc, uint16_t scnt)
819{
820 if (bc->amode == am_lba48) {
821 /* Write high-order bits. */
822 pio_write_8(&cmd->sector_count, scnt >> 8);
823 pio_write_8(&cmd->sector_number, bc->c3);
824 pio_write_8(&cmd->cylinder_low, bc->c4);
825 pio_write_8(&cmd->cylinder_high, bc->c5);
826 }
827
828 /* Write low-order bits. */
829 pio_write_8(&cmd->sector_count, scnt & 0x00ff);
830 pio_write_8(&cmd->sector_number, bc->c0);
831 pio_write_8(&cmd->cylinder_low, bc->c1);
832 pio_write_8(&cmd->cylinder_high, bc->c2);
833}
834
835/** Wait until some status bits are set and some are reset.
836 *
837 * Example: wait_status(SR_DRDY, ~SR_BSY) waits for SR_DRDY to become
838 * set and SR_BSY to become reset.
839 *
840 * @param set Combination if bits which must be all set.
841 * @param n_reset Negated combination of bits which must be all reset.
842 * @param pstatus Pointer where to store last read status or NULL.
843 * @param timeout Timeout in 10ms units.
844 *
845 * @return EOK on success, EIO on timeout.
846 */
847static int wait_status(unsigned set, unsigned n_reset, uint8_t *pstatus,
848 unsigned timeout)
849{
850 uint8_t status;
851 int cnt;
852
853 status = pio_read_8(&cmd->status);
854
855 /*
856 * This is crude, yet simple. First try with 1us delays
857 * (most likely the device will respond very fast). If not,
858 * start trying every 10 ms.
859 */
860
861 cnt = 100;
862 while ((status & ~n_reset) != 0 || (status & set) != set) {
863 async_usleep(1);
864 --cnt;
865 if (cnt <= 0) break;
866
867 status = pio_read_8(&cmd->status);
868 }
869
870 cnt = timeout;
871 while ((status & ~n_reset) != 0 || (status & set) != set) {
872 async_usleep(10000);
873 --cnt;
874 if (cnt <= 0) break;
875
876 status = pio_read_8(&cmd->status);
877 }
878
879 if (pstatus)
880 *pstatus = status;
881
882 if (cnt == 0)
883 return EIO;
884
885 return EOK;
886}
887
888/**
889 * @}
890 */
Note: See TracBrowser for help on using the repository browser.