source: mainline/uspace/srv/bd/ata_bd/ata_bd.c@ 5bd1ffb

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

Move signature definition to ata_hw.h.

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