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