Changeset ad7a6c9 in mainline for uspace/srv/bd/ata_bd/ata_bd.c
- Timestamp:
- 2011-03-30T13:10:24Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 4ae90f9
- Parents:
- 6e50466 (diff), d6b81941 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/bd/ata_bd/ata_bd.c
r6e50466 rad7a6c9 51 51 #include <libarch/ddi.h> 52 52 #include <ddi.h> 53 #include <ipc/ipc.h>54 53 #include <ipc/bd.h> 55 54 #include <async.h> 56 55 #include <as.h> 57 56 #include <fibril_synch.h> 57 #include <stdint.h> 58 58 #include <str.h> 59 59 #include <devmap.h> … … 62 62 #include <errno.h> 63 63 #include <bool.h> 64 #include <byteorder.h> 64 65 #include <task.h> 65 66 #include <macros.h> 66 67 68 #include "ata_hw.h" 67 69 #include "ata_bd.h" 68 70 … … 70 72 #define NAMESPACE "bd" 71 73 72 /** Physical block size. Should be always 512. */ 73 static const size_t block_size = 512; 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; 74 82 75 83 /** Size of the communication area. */ … … 77 85 78 86 /** I/O base address of the command registers. */ 79 static uintptr_t cmd_physical = 0x1f0;87 static uintptr_t cmd_physical; 80 88 /** I/O base address of the control registers. */ 81 static uintptr_t ctl_physical = 0x170; 89 static uintptr_t ctl_physical; 90 91 /** I/O base addresses for legacy (ISA-compatible) controllers. */ 92 static ata_base_t legacy_base[LEGACY_CTLS] = { 93 { 0x1f0, 0x3f0 }, 94 { 0x170, 0x370 }, 95 { 0x1e8, 0x3e8 }, 96 { 0x168, 0x368 } 97 }; 82 98 83 99 static ata_cmd_t *cmd; … … 87 103 static disk_t disk[MAX_DISKS]; 88 104 105 static void print_syntax(void); 89 106 static int ata_bd_init(void); 90 107 static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall); … … 93 110 static int ata_bd_write_blocks(int disk_id, uint64_t ba, size_t cnt, 94 111 const void *buf); 95 static int ata_ bd_read_block(int disk_id, uint64_t ba, size_t cnt,112 static int ata_rcmd_read(int disk_id, uint64_t ba, size_t cnt, 96 113 void *buf); 97 static int ata_ bd_write_block(int disk_id, uint64_t ba, size_t cnt,114 static int ata_rcmd_write(int disk_id, uint64_t ba, size_t cnt, 98 115 const void *buf); 99 116 static int disk_init(disk_t *d, int disk_id); 100 117 static int drive_identify(int drive_id, void *buf); 118 static int identify_pkt_dev(int dev_idx, void *buf); 119 static int ata_cmd_packet(int dev_idx, const void *cpkt, size_t cpkt_size, 120 void *obuf, size_t obuf_size); 121 static int ata_pcmd_inquiry(int dev_idx, void *obuf, size_t obuf_size); 122 static int ata_pcmd_read_12(int dev_idx, uint64_t ba, size_t cnt, 123 void *obuf, size_t obuf_size); 101 124 static void disk_print_summary(disk_t *d); 102 125 static int coord_calc(disk_t *d, uint64_t ba, block_coord_t *bc); … … 110 133 int i, rc; 111 134 int n_disks; 135 unsigned ctl_num; 136 char *eptr; 112 137 113 138 printf(NAME ": ATA disk driver\n"); 114 139 115 printf("I/O address %p/%p\n", (void *) ctl_physical, 116 (void *) cmd_physical); 140 if (argc > 1) { 141 ctl_num = strtoul(argv[1], &eptr, 0); 142 if (*eptr != '\0' || ctl_num == 0 || ctl_num > 4) { 143 printf("Invalid argument.\n"); 144 print_syntax(); 145 return -1; 146 } 147 } else { 148 ctl_num = 1; 149 } 150 151 cmd_physical = legacy_base[ctl_num - 1].cmd; 152 ctl_physical = legacy_base[ctl_num - 1].ctl; 153 154 printf("I/O address %p/%p\n", (void *) cmd_physical, 155 (void *) ctl_physical); 117 156 118 157 if (ata_bd_init() != EOK) … … 139 178 continue; 140 179 141 snprintf(name, 16, "%s/ disk%d", NAMESPACE, i);180 snprintf(name, 16, "%s/ata%udisk%d", NAMESPACE, ctl_num, i); 142 181 rc = devmap_device_register(name, &disk[i].devmap_handle); 143 182 if (rc != EOK) { 144 devmap_hangup_phone(DEVMAP_DRIVER);145 183 printf(NAME ": Unable to register device %s.\n", name); 146 184 return rc; … … 162 200 } 163 201 202 203 static void print_syntax(void) 204 { 205 printf("Syntax: " NAME " <controller_number>\n"); 206 printf("Controller number = 1..4\n"); 207 } 208 164 209 /** Print one-line device summary. */ 165 210 static void disk_print_summary(disk_t *d) … … 169 214 printf("%s: ", d->model); 170 215 171 switch (d->amode) { 172 case am_chs: 173 printf("CHS %u cylinders, %u heads, %u sectors", 174 disk->geom.cylinders, disk->geom.heads, disk->geom.sectors); 175 break; 176 case am_lba28: 177 printf("LBA-28"); 178 break; 179 case am_lba48: 180 printf("LBA-48"); 181 break; 216 if (d->dev_type == ata_reg_dev) { 217 switch (d->amode) { 218 case am_chs: 219 printf("CHS %u cylinders, %u heads, %u sectors", 220 disk->geom.cylinders, disk->geom.heads, 221 disk->geom.sectors); 222 break; 223 case am_lba28: 224 printf("LBA-28"); 225 break; 226 case am_lba48: 227 printf("LBA-48"); 228 break; 229 } 230 } else { 231 printf("PACKET"); 182 232 } 183 233 … … 231 281 sysarg_t method; 232 282 devmap_handle_t dh; 233 int flags;283 unsigned int flags; 234 284 int retval; 235 285 uint64_t ba; … … 247 297 248 298 if (disk_id < 0 || disk[disk_id].present == false) { 249 ipc_answer_0(iid, EINVAL);299 async_answer_0(iid, EINVAL); 250 300 return; 251 301 } 252 302 253 303 /* Answer the IPC_M_CONNECT_ME_TO call. */ 254 ipc_answer_0(iid, EOK);304 async_answer_0(iid, EOK); 255 305 256 306 if (!async_share_out_receive(&callid, &comm_size, &flags)) { 257 ipc_answer_0(callid, EHANGUP);307 async_answer_0(callid, EHANGUP); 258 308 return; 259 309 } … … 261 311 fs_va = as_get_mappable_page(comm_size); 262 312 if (fs_va == NULL) { 263 ipc_answer_0(callid, EHANGUP);313 async_answer_0(callid, EHANGUP); 264 314 return; 265 315 } … … 273 323 case IPC_M_PHONE_HUNGUP: 274 324 /* The other side has hung up. */ 275 ipc_answer_0(callid, EOK);325 async_answer_0(callid, EOK); 276 326 return; 277 327 case BD_READ_BLOCKS: … … 279 329 IPC_GET_ARG2(call)); 280 330 cnt = IPC_GET_ARG3(call); 281 if (cnt * block_size > comm_size) {331 if (cnt * disk[disk_id].block_size > comm_size) { 282 332 retval = ELIMIT; 283 333 break; … … 289 339 IPC_GET_ARG2(call)); 290 340 cnt = IPC_GET_ARG3(call); 291 if (cnt * block_size > comm_size) {341 if (cnt * disk[disk_id].block_size > comm_size) { 292 342 retval = ELIMIT; 293 343 break; … … 296 346 break; 297 347 case BD_GET_BLOCK_SIZE: 298 ipc_answer_1(callid, EOK,block_size);348 async_answer_1(callid, EOK, disk[disk_id].block_size); 299 349 continue; 300 350 case BD_GET_NUM_BLOCKS: 301 ipc_answer_2(callid, EOK, LOWER32(disk[disk_id].blocks),351 async_answer_2(callid, EOK, LOWER32(disk[disk_id].blocks), 302 352 UPPER32(disk[disk_id].blocks)); 303 353 continue; … … 306 356 break; 307 357 } 308 ipc_answer_0(callid, retval);358 async_answer_0(callid, retval); 309 359 } 310 360 } … … 319 369 identify_data_t idata; 320 370 uint8_t model[40]; 371 ata_inquiry_data_t inq_data; 321 372 uint16_t w; 322 373 uint8_t c; … … 325 376 unsigned i; 326 377 378 d->present = false; 379 fibril_mutex_initialize(&d->lock); 380 381 /* Try identify command. */ 327 382 rc = drive_identify(disk_id, &idata); 328 if (rc != EOK) { 329 d->present = false; 330 return rc; 331 } 332 333 if ((idata.caps & cap_lba) == 0) { 383 if (rc == EOK) { 384 /* Success. It's a register (non-packet) device. */ 385 printf("ATA register-only device found.\n"); 386 d->dev_type = ata_reg_dev; 387 } else if (rc == EIO) { 388 /* 389 * There is something, but not a register device. 390 * It could be a packet device. 391 */ 392 rc = identify_pkt_dev(disk_id, &idata); 393 if (rc == EOK) { 394 /* We have a packet device. */ 395 d->dev_type = ata_pkt_dev; 396 } else { 397 /* Nope. Something's there, but not recognized. */ 398 return EIO; 399 } 400 } else { 401 /* Operation timed out. That means there is no device there. */ 402 return EIO; 403 } 404 405 printf("device caps: 0x%04x\n", idata.caps); 406 if (d->dev_type == ata_pkt_dev) { 407 /* Packet device */ 408 d->amode = 0; 409 410 d->geom.cylinders = 0; 411 d->geom.heads = 0; 412 d->geom.sectors = 0; 413 414 d->blocks = 0; 415 } else if ((idata.caps & rd_cap_lba) == 0) { 334 416 /* Device only supports CHS addressing. */ 335 417 d->amode = am_chs; … … 388 470 d->model[pos] = '\0'; 389 471 472 if (d->dev_type == ata_pkt_dev) { 473 /* Send inquiry. */ 474 rc = ata_pcmd_inquiry(0, &inq_data, sizeof(inq_data)); 475 if (rc != EOK) { 476 printf("Device inquiry failed.\n"); 477 d->present = false; 478 return EIO; 479 } 480 481 /* Check device type. */ 482 if (INQUIRY_PDEV_TYPE(inq_data.pdev_type) != PDEV_TYPE_CDROM) 483 printf("Warning: Peripheral device type is not CD-ROM.\n"); 484 485 /* Assume 2k block size for now. */ 486 d->block_size = 2048; 487 } else { 488 /* Assume register Read always uses 512-byte blocks. */ 489 d->block_size = 512; 490 } 491 390 492 d->present = true; 391 fibril_mutex_initialize(&d->lock);392 393 493 return EOK; 394 494 } … … 401 501 402 502 while (cnt > 0) { 403 rc = ata_bd_read_block(disk_id, ba, 1, buf); 503 if (disk[disk_id].dev_type == ata_reg_dev) 504 rc = ata_rcmd_read(disk_id, ba, 1, buf); 505 else 506 rc = ata_pcmd_read_12(disk_id, ba, 1, buf, 507 disk[disk_id].block_size); 508 404 509 if (rc != EOK) 405 510 return rc; … … 407 512 ++ba; 408 513 --cnt; 409 buf += block_size;514 buf += disk[disk_id].block_size; 410 515 } 411 516 … … 419 524 int rc; 420 525 526 if (disk[disk_id].dev_type != ata_reg_dev) 527 return ENOTSUP; 528 421 529 while (cnt > 0) { 422 rc = ata_ bd_write_block(disk_id, ba, 1, buf);530 rc = ata_rcmd_write(disk_id, ba, 1, buf); 423 531 if (rc != EOK) 424 532 return rc; … … 426 534 ++ba; 427 535 --cnt; 428 buf += block_size;536 buf += disk[disk_id].block_size; 429 537 } 430 538 … … 439 547 * @param disk_id Device ID, 0 or 1. 440 548 * @param buf Pointer to a 512-byte buffer. 549 * 550 * @return ETIMEOUT on timeout (this can mean the device is 551 * not present). EIO if device responds with error. 441 552 */ 442 553 static int drive_identify(int disk_id, void *buf) … … 450 561 451 562 if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK) 452 return E IO;563 return ETIMEOUT; 453 564 454 565 pio_write_8(&cmd->drive_head, drv_head); … … 459 570 */ 460 571 if (wait_status(SR_DRDY, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK) 461 return E IO;572 return ETIMEOUT; 462 573 463 574 pio_write_8(&cmd->command, CMD_IDENTIFY_DRIVE); 464 575 465 576 if (wait_status(0, ~SR_BSY, &status, TIMEOUT_PROBE) != EOK) 466 return E IO;577 return ETIMEOUT; 467 578 468 579 /* Read data from the disk buffer. */ 469 580 470 581 if ((status & SR_DRQ) != 0) { 471 for (i = 0; i < block_size / 2; i++) {582 for (i = 0; i < identify_data_size / 2; i++) { 472 583 data = pio_read_16(&cmd->data_port); 473 584 ((uint16_t *) buf)[i] = data; … … 475 586 } 476 587 588 if ((status & SR_ERR) != 0) { 589 return EIO; 590 } 591 592 return EOK; 593 } 594 595 /** Issue Identify Packet Device command. 596 * 597 * Reads @c identify data into the provided buffer. This is used to detect 598 * whether an ATAPI device is present and if so, to determine its parameters. 599 * 600 * @param dev_idx Device index, 0 or 1. 601 * @param buf Pointer to a 512-byte buffer. 602 */ 603 static int identify_pkt_dev(int dev_idx, void *buf) 604 { 605 uint16_t data; 606 uint8_t status; 607 uint8_t drv_head; 608 size_t i; 609 610 drv_head = ((dev_idx != 0) ? DHR_DRV : 0); 611 612 if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK) 613 return EIO; 614 615 pio_write_8(&cmd->drive_head, drv_head); 616 617 /* For ATAPI commands we do not need to wait for DRDY. */ 618 if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK) 619 return EIO; 620 621 pio_write_8(&cmd->command, CMD_IDENTIFY_PKT_DEV); 622 623 if (wait_status(0, ~SR_BSY, &status, TIMEOUT_BSY) != EOK) 624 return EIO; 625 626 /* Read data from the device buffer. */ 627 628 if ((status & SR_DRQ) != 0) { 629 for (i = 0; i < identify_data_size / 2; i++) { 630 data = pio_read_16(&cmd->data_port); 631 ((uint16_t *) buf)[i] = data; 632 } 633 } 634 477 635 if ((status & SR_ERR) != 0) 478 636 return EIO; 637 638 return EOK; 639 } 640 641 /** Issue packet command (i. e. write a command packet to the device). 642 * 643 * Only data-in commands are supported (e.g. inquiry, read). 644 * 645 * @param dev_idx Device index (0 or 1) 646 * @param obuf Buffer for storing data read from device 647 * @param obuf_size Size of obuf in bytes 648 * 649 * @return EOK on success, EIO on error. 650 */ 651 static int ata_cmd_packet(int dev_idx, const void *cpkt, size_t cpkt_size, 652 void *obuf, size_t obuf_size) 653 { 654 size_t i; 655 uint8_t status; 656 uint8_t drv_head; 657 disk_t *d; 658 size_t data_size; 659 uint16_t val; 660 661 d = &disk[dev_idx]; 662 fibril_mutex_lock(&d->lock); 663 664 /* New value for Drive/Head register */ 665 drv_head = 666 ((dev_idx != 0) ? DHR_DRV : 0); 667 668 if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK) { 669 fibril_mutex_unlock(&d->lock); 670 return EIO; 671 } 672 673 pio_write_8(&cmd->drive_head, drv_head); 674 675 if (wait_status(0, ~(SR_BSY|SR_DRQ), NULL, TIMEOUT_BSY) != EOK) { 676 fibril_mutex_unlock(&d->lock); 677 return EIO; 678 } 679 680 /* Byte count <- max. number of bytes we can read in one transfer. */ 681 pio_write_8(&cmd->cylinder_low, 0xfe); 682 pio_write_8(&cmd->cylinder_high, 0xff); 683 684 pio_write_8(&cmd->command, CMD_PACKET); 685 686 if (wait_status(SR_DRQ, ~SR_BSY, &status, TIMEOUT_BSY) != EOK) { 687 fibril_mutex_unlock(&d->lock); 688 return EIO; 689 } 690 691 /* Write command packet. */ 692 for (i = 0; i < (cpkt_size + 1) / 2; i++) 693 pio_write_16(&cmd->data_port, ((uint16_t *) cpkt)[i]); 694 695 if (wait_status(0, ~SR_BSY, &status, TIMEOUT_BSY) != EOK) { 696 fibril_mutex_unlock(&d->lock); 697 return EIO; 698 } 699 700 if ((status & SR_DRQ) == 0) { 701 fibril_mutex_unlock(&d->lock); 702 return EIO; 703 } 704 705 /* Read byte count. */ 706 data_size = (uint16_t) pio_read_8(&cmd->cylinder_low) + 707 ((uint16_t) pio_read_8(&cmd->cylinder_high) << 8); 708 709 /* Check whether data fits into output buffer. */ 710 if (data_size > obuf_size) { 711 /* Output buffer is too small to store data. */ 712 fibril_mutex_unlock(&d->lock); 713 return EIO; 714 } 715 716 /* Read data from the device buffer. */ 717 for (i = 0; i < (data_size + 1) / 2; i++) { 718 val = pio_read_16(&cmd->data_port); 719 ((uint16_t *) obuf)[i] = val; 720 } 721 722 if (status & SR_ERR) { 723 fibril_mutex_unlock(&d->lock); 724 return EIO; 725 } 726 727 fibril_mutex_unlock(&d->lock); 728 729 return EOK; 730 } 731 732 /** Issue ATAPI Inquiry. 733 * 734 * @param dev_idx Device index (0 or 1) 735 * @param obuf Buffer for storing inquiry data read from device 736 * @param obuf_size Size of obuf in bytes 737 * 738 * @return EOK on success, EIO on error. 739 */ 740 static int ata_pcmd_inquiry(int dev_idx, void *obuf, size_t obuf_size) 741 { 742 ata_pcmd_inquiry_t cp; 743 int rc; 744 745 memset(&cp, 0, sizeof(cp)); 746 747 cp.opcode = PCMD_INQUIRY; 748 cp.alloc_len = min(obuf_size, 0xff); /* Allocation length */ 749 750 rc = ata_cmd_packet(0, &cp, sizeof(cp), obuf, obuf_size); 751 if (rc != EOK) 752 return rc; 753 754 return EOK; 755 } 756 757 /** Issue ATAPI read(12) command. 758 * 759 * Output buffer must be large enough to hold the data, otherwise the 760 * function will fail. 761 * 762 * @param dev_idx Device index (0 or 1) 763 * @param ba Starting block address 764 * @param cnt Number of blocks to read 765 * @param obuf Buffer for storing inquiry data read from device 766 * @param obuf_size Size of obuf in bytes 767 * 768 * @return EOK on success, EIO on error. 769 */ 770 static int ata_pcmd_read_12(int dev_idx, uint64_t ba, size_t cnt, 771 void *obuf, size_t obuf_size) 772 { 773 ata_pcmd_read_12_t cp; 774 int rc; 775 776 if (ba > UINT32_MAX) 777 return EINVAL; 778 779 memset(&cp, 0, sizeof(cp)); 780 781 cp.opcode = PCMD_READ_12; 782 cp.ba = host2uint32_t_be(ba); 783 cp.nblocks = host2uint32_t_be(cnt); 784 785 rc = ata_cmd_packet(0, &cp, sizeof(cp), obuf, obuf_size); 786 if (rc != EOK) 787 return rc; 479 788 480 789 return EOK; … … 490 799 * @return EOK on success, EIO on error. 491 800 */ 492 static int ata_ bd_read_block(int disk_id, uint64_t ba, size_t blk_cnt,801 static int ata_rcmd_read(int disk_id, uint64_t ba, size_t blk_cnt, 493 802 void *buf) 494 803 { … … 545 854 /* Read data from the device buffer. */ 546 855 547 for (i = 0; i < block_size / 2; i++) {856 for (i = 0; i < disk[disk_id].block_size / 2; i++) { 548 857 data = pio_read_16(&cmd->data_port); 549 858 ((uint16_t *) buf)[i] = data; … … 567 876 * @return EOK on success, EIO on error. 568 877 */ 569 static int ata_ bd_write_block(int disk_id, uint64_t ba, size_t cnt,878 static int ata_rcmd_write(int disk_id, uint64_t ba, size_t cnt, 570 879 const void *buf) 571 880 { … … 621 930 /* Write data to the device buffer. */ 622 931 623 for (i = 0; i < block_size / 2; i++) {932 for (i = 0; i < disk[disk_id].block_size / 2; i++) { 624 933 pio_write_16(&cmd->data_port, ((uint16_t *) buf)[i]); 625 934 }
Note:
See TracChangeset
for help on using the changeset viewer.