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