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