Ata.c revision 32faa72a48ac68fbab0acc5bfcd1a8ad0a35a78a
1/** @file 2 Copyright (c) 2006 - 2007 Intel Corporation. <BR> 3 All rights reserved. This program and the accompanying materials 4 are licensed and made available under the terms and conditions of the BSD License 5 which accompanies this distribution. The full text of the license may be found at 6 http://opensource.org/licenses/bsd-license.php 7 8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 10 11 @par Revision Reference: 12 2002-6: Add Atapi6 enhancement, support >120GB hard disk, including 13 update - ATAIdentity() func 14 update - AtaBlockIoReadBlocks() func 15 update - AtaBlockIoWriteBlocks() func 16 add - AtaAtapi6Identify() func 17 add - AtaReadSectorsExt() func 18 add - AtaWriteSectorsExt() func 19 add - AtaPioDataInExt() func 20 add - AtaPioDataOutExt() func 21 22**/ 23 24#include "idebus.h" 25 26/** 27 Sends out an ATA Identify Command to the specified device. 28 29 This function is called by DiscoverIdeDevice() during its device 30 identification. It sends out the ATA Identify Command to the 31 specified device. Only ATA device responses to this command. If 32 the command succeeds, it returns the Identify data structure which 33 contains information about the device. This function extracts the 34 information it needs to fill the IDE_BLK_IO_DEV data structure, 35 including device type, media block size, media capacity, and etc. 36 37 @param[in] *IdeDev 38 pointer pointing to IDE_BLK_IO_DEV data structure,used 39 to record all the information of the IDE device. 40 41 @retval EFI_SUCCESS Identify ATA device successfully. 42 43 @retval EFI_DEVICE_ERROR ATA Identify Device Command failed or 44 device is not ATA device. 45 46 @note 47 parameter IdeDev will be updated in this function. 48 49**/ 50EFI_STATUS 51ATAIdentify ( 52 IN IDE_BLK_IO_DEV *IdeDev 53 ) 54{ 55 EFI_STATUS Status; 56 EFI_IDENTIFY_DATA *AtaIdentifyPointer; 57 UINT32 Capacity; 58 UINT8 DeviceSelect; 59 UINTN Retry; 60 61 // 62 // AtaIdentifyPointer is used for accommodating returned IDENTIFY data of 63 // the ATA Identify command 64 // 65 AtaIdentifyPointer = (EFI_IDENTIFY_DATA *) AllocateZeroPool (sizeof (EFI_IDENTIFY_DATA)); 66 67 // 68 // use ATA PIO Data In protocol to send ATA Identify command 69 // and receive data from device 70 // 71 DeviceSelect = (UINT8) ((IdeDev->Device) << 4); 72 73 74 Retry = 3; 75 while (Retry > 0) { 76 Status = AtaPioDataIn ( 77 IdeDev, 78 (VOID *) AtaIdentifyPointer, 79 sizeof (EFI_IDENTIFY_DATA), 80 ATA_CMD_IDENTIFY_DRIVE, 81 DeviceSelect, 82 0, 83 0, 84 0, 85 0 86 ); 87 // 88 // If ATA Identify command succeeds, then according to the received 89 // IDENTIFY data, 90 // identify the device type ( ATA or not ). 91 // If ATA device, fill the information in IdeDev. 92 // If not ATA device, return IDE_DEVICE_ERROR 93 // 94 if (!EFI_ERROR (Status)) { 95 96 IdeDev->pIdData = AtaIdentifyPointer; 97 98 // 99 // Print ATA Module Name 100 // 101 PrintAtaModuleName (IdeDev); 102 103 // 104 // bit 15 of pAtaIdentify->config is used to identify whether device is 105 // ATA device or ATAPI device. 106 // if 0, means ATA device; if 1, means ATAPI device. 107 // 108 if ((AtaIdentifyPointer->AtaData.config & 0x8000) == 0x00) { 109 // 110 // Detect if support S.M.A.R.T. If yes, enable it as default 111 // 112 AtaSMARTSupport (IdeDev); 113 114 // 115 // Check whether this device needs 48-bit addressing (ATAPI-6 ata device) 116 // 117 Status = AtaAtapi6Identify (IdeDev); 118 if (!EFI_ERROR (Status)) { 119 // 120 // It's a disk with >120GB capacity, initialized in AtaAtapi6Identify() 121 // 122 return EFI_SUCCESS; 123 } else if (Status == EFI_DEVICE_ERROR) { 124 // 125 // Some disk with big capacity (>200GB) is slow when being identified 126 // and will return all zero for word83. 127 // We try twice at first. If it fails, we do a SoftRest and try again. 128 // 129 Retry--; 130 if (Retry == 1) { 131 // 132 // Do a SoftRest before the third attempt. 133 // 134 AtaSoftReset (IdeDev); 135 } 136 continue; 137 } 138 // 139 // This is a hard disk <= 120GB capacity, treat it as normal hard disk 140 // 141 IdeDev->Type = IdeHardDisk; 142 143 // 144 // Block Media Information: 145 // Media->LogicalPartition , Media->WriteCaching will be filled 146 // in the DiscoverIdeDevcie() function. 147 // 148 IdeDev->BlkIo.Media->IoAlign = 4; 149 IdeDev->BlkIo.Media->MediaId = 1; 150 IdeDev->BlkIo.Media->RemovableMedia = FALSE; 151 IdeDev->BlkIo.Media->MediaPresent = TRUE; 152 IdeDev->BlkIo.Media->ReadOnly = FALSE; 153 IdeDev->BlkIo.Media->BlockSize = 0x200; 154 155 // 156 // Calculate device capacity 157 // 158 Capacity = ((UINT32)AtaIdentifyPointer->AtaData.user_addressable_sectors_hi << 16) | 159 AtaIdentifyPointer->AtaData.user_addressable_sectors_lo ; 160 IdeDev->BlkIo.Media->LastBlock = Capacity - 1; 161 162 return EFI_SUCCESS; 163 } 164 165 } 166 break; 167 } 168 169 gBS->FreePool (AtaIdentifyPointer); 170 // 171 // Make sure the pIdData will not be freed again. 172 // 173 IdeDev->pIdData = NULL; 174 175 return EFI_DEVICE_ERROR; 176} 177 178 179/** 180 This function is called by ATAIdentify() to identity whether this disk 181 supports ATA/ATAPI6 48bit addressing, ie support >120G capacity 182 183 @param[in] *IdeDev 184 pointer pointing to IDE_BLK_IO_DEV data structure, used 185 to record all the information of the IDE device. 186 187 @retval EFI_SUCCESS The disk specified by IdeDev is a Atapi6 supported one 188 and 48-bit addressing must be used 189 190 @retval EFI_UNSUPPORTED The disk dosn't not support Atapi6 or it supports but 191 the capacity is below 120G, 48bit addressing is not needed 192 193 @retval EFI_DEVICE_ERROR The identify data in IdeDev is incorrect 194 195 @retval EFI_INVALID_PARAMETER The identify data in IdeDev is NULL. 196 197 @note 198 This function must be called after DEVICE_IDENTITY command has been 199 successfully returned 200 201**/ 202EFI_STATUS 203AtaAtapi6Identify ( 204 IN IDE_BLK_IO_DEV *IdeDev 205 ) 206{ 207 UINT8 Index; 208 EFI_LBA TmpLba; 209 EFI_LBA Capacity; 210 EFI_IDENTIFY_DATA *Atapi6IdentifyStruct; 211 212 if (IdeDev->pIdData == NULL) { 213 return EFI_INVALID_PARAMETER; 214 } 215 216 Atapi6IdentifyStruct = IdeDev->pIdData; 217 218 if ((Atapi6IdentifyStruct->AtapiData.cmd_set_support_83 & (BIT15 | BIT14)) != 0x4000) { 219 // 220 // Per ATA-6 spec, word83: bit15 is zero and bit14 is one 221 // 222 return EFI_DEVICE_ERROR; 223 } 224 225 if ((Atapi6IdentifyStruct->AtapiData.cmd_set_support_83 & BIT10) == 0) { 226 // 227 // The device dosn't support 48 bit addressing 228 // 229 return EFI_UNSUPPORTED; 230 } 231 232 // 233 // 48 bit address feature set is supported, get maximum capacity 234 // 235 Capacity = Atapi6IdentifyStruct->AtapiData.max_user_lba_for_48bit_addr[0]; 236 for (Index = 1; Index < 4; Index++) { 237 // 238 // Lower byte goes first: word[100] is the lowest word, word[103] is highest 239 // 240 TmpLba = Atapi6IdentifyStruct->AtapiData.max_user_lba_for_48bit_addr[Index]; 241 Capacity |= LShiftU64 (TmpLba, 16 * Index); 242 } 243 244 if (Capacity > MAX_28BIT_ADDRESSING_CAPACITY) { 245 // 246 // Capacity exceeds 120GB. 48-bit addressing is really needed 247 // 248 IdeDev->Type = Ide48bitAddressingHardDisk; 249 250 // 251 // Fill block media information:Media->LogicalPartition , 252 // Media->WriteCaching will be filledin the DiscoverIdeDevcie() function. 253 // 254 IdeDev->BlkIo.Media->IoAlign = 4; 255 IdeDev->BlkIo.Media->MediaId = 1; 256 IdeDev->BlkIo.Media->RemovableMedia = FALSE; 257 IdeDev->BlkIo.Media->MediaPresent = TRUE; 258 IdeDev->BlkIo.Media->ReadOnly = FALSE; 259 IdeDev->BlkIo.Media->BlockSize = 0x200; 260 IdeDev->BlkIo.Media->LastBlock = Capacity - 1; 261 262 return EFI_SUCCESS; 263 } 264 265 return EFI_UNSUPPORTED; 266} 267 268/** 269 This function is called by ATAIdentify() or ATAPIIdentify() 270 to print device's module name. 271 272 @param[in] *IdeDev 273 pointer pointing to IDE_BLK_IO_DEV data structure, used 274 to record all the information of the IDE device. 275 276**/ 277VOID 278PrintAtaModuleName ( 279 IN IDE_BLK_IO_DEV *IdeDev 280 ) 281{ 282 if (IdeDev->pIdData == NULL) { 283 return ; 284 } 285 286 SwapStringChars (IdeDev->ModelName, IdeDev->pIdData->AtaData.ModelName, 40); 287 IdeDev->ModelName[40] = 0x00; 288} 289 290/** 291 This function is used to send out ATA commands conforms to the 292 PIO Data In Protocol. 293 294 @param[in] *IdeDev 295 pointer pointing to IDE_BLK_IO_DEV data structure, used 296 to record all the information of the IDE device. 297 298 @param[in] *Buffer 299 buffer contained data transferred from device to host. 300 301 @param[in] ByteCount 302 data size in byte unit of the buffer. 303 304 @param[in] AtaCommand 305 value of the Command Register 306 307 @param[in] Head 308 value of the Head/Device Register 309 310 @param[in] SectorCount 311 value of the Sector Count Register 312 313 @param[in] SectorNumber 314 value of the Sector Number Register 315 316 @param[in] CylinderLsb 317 value of the low byte of the Cylinder Register 318 319 @param[in] CylinderMsb 320 value of the high byte of the Cylinder Register 321 322 @retval EFI_SUCCESS send out the ATA command and device send required 323 data successfully. 324 325 @retval EFI_DEVICE_ERROR command sent failed. 326 327**/ 328EFI_STATUS 329AtaPioDataIn ( 330 IN IDE_BLK_IO_DEV *IdeDev, 331 IN VOID *Buffer, 332 IN UINT32 ByteCount, 333 IN UINT8 AtaCommand, 334 IN UINT8 Head, 335 IN UINT8 SectorCount, 336 IN UINT8 SectorNumber, 337 IN UINT8 CylinderLsb, 338 IN UINT8 CylinderMsb 339 ) 340{ 341 UINTN WordCount; 342 UINTN Increment; 343 UINT16 *Buffer16; 344 EFI_STATUS Status; 345 346 Status = WaitForBSYClear (IdeDev, ATATIMEOUT); 347 if (EFI_ERROR (Status)) { 348 return EFI_DEVICE_ERROR; 349 } 350 351 // 352 // e0:1110,0000-- bit7 and bit5 are reserved bits. 353 // bit6 set means LBA mode 354 // 355 IDEWritePortB ( 356 IdeDev->PciIo, 357 IdeDev->IoPort->Head, 358 (UINT8) ((IdeDev->Device << 4) | 0xe0 | Head) 359 ); 360 361 // 362 // All ATAPI device's ATA commands can be issued regardless of the 363 // state of the DRDY 364 // 365 if (IdeDev->Type == IdeHardDisk) { 366 367 Status = DRDYReady (IdeDev, ATATIMEOUT); 368 if (EFI_ERROR (Status)) { 369 return EFI_DEVICE_ERROR; 370 } 371 } 372 // 373 // set all the command parameters 374 // Before write to all the following registers, BSY and DRQ must be 0. 375 // 376 Status = DRQClear2 (IdeDev, ATATIMEOUT); 377 if (EFI_ERROR (Status)) { 378 return EFI_DEVICE_ERROR; 379 } 380 381 if (AtaCommand == ATA_CMD_SET_FEATURES) { 382 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, 0x03); 383 } 384 385 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount); 386 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, SectorNumber); 387 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, CylinderLsb); 388 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, CylinderMsb); 389 390 // 391 // send command via Command Register 392 // 393 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand); 394 395 Buffer16 = (UINT16 *) Buffer; 396 397 // 398 // According to PIO data in protocol, host can perform a series of reads to 399 // the data register after each time device set DRQ ready; 400 // The data size of "a series of read" is command specific. 401 // For most ATA command, data size received from device will not exceed 402 // 1 sector, hence the data size for "a series of read" can be the whole data 403 // size of one command request. 404 // For ATA command such as Read Sector command, the data size of one ATA 405 // command request is often larger than 1 sector, according to the 406 // Read Sector command, the data size of "a series of read" is exactly 1 407 // sector. 408 // Here for simplification reason, we specify the data size for 409 // "a series of read" to 1 sector (256 words) if data size of one ATA command 410 // request is larger than 256 words. 411 // 412 Increment = 256; 413 414 // 415 // used to record bytes of currently transfered data 416 // 417 WordCount = 0; 418 419 while (WordCount < ByteCount / 2) { 420 // 421 // Poll DRQ bit set, data transfer can be performed only when DRQ is ready. 422 // 423 Status = DRQReady2 (IdeDev, ATATIMEOUT); 424 if (EFI_ERROR (Status)) { 425 return EFI_DEVICE_ERROR; 426 } 427 428 Status = CheckErrorStatus (IdeDev); 429 if (EFI_ERROR (Status)) { 430 return EFI_DEVICE_ERROR; 431 } 432 433 // 434 // Get the byte count for one series of read 435 // 436 if ((WordCount + Increment) > ByteCount / 2) { 437 Increment = ByteCount / 2 - WordCount; 438 } 439 440 IDEReadPortWMultiple ( 441 IdeDev->PciIo, 442 IdeDev->IoPort->Data, 443 Increment, 444 Buffer16 445 ); 446 447 WordCount += Increment; 448 Buffer16 += Increment; 449 450 } 451 452 DRQClear (IdeDev, ATATIMEOUT); 453 454 return CheckErrorStatus (IdeDev); 455} 456 457/** 458 This function is used to send out ATA commands conforms to the 459 PIO Data Out Protocol. 460 461 @param *IdeDev 462 pointer pointing to IDE_BLK_IO_DEV data structure, used 463 to record all the information of the IDE device. 464 465 @param *Buffer buffer contained data transferred from host to device. 466 @param ByteCount data size in byte unit of the buffer. 467 @param AtaCommand value of the Command Register 468 @param Head value of the Head/Device Register 469 @param SectorCount value of the Sector Count Register 470 @param SectorNumber value of the Sector Number Register 471 @param CylinderLsb value of the low byte of the Cylinder Register 472 @param CylinderMsb value of the high byte of the Cylinder Register 473 474 @retval EFI_SUCCESS send out the ATA command and device received required 475 data successfully. 476 477 @retval EFI_DEVICE_ERROR command sent failed. 478 479**/ 480EFI_STATUS 481AtaPioDataOut ( 482 IN IDE_BLK_IO_DEV *IdeDev, 483 IN VOID *Buffer, 484 IN UINT32 ByteCount, 485 IN UINT8 AtaCommand, 486 IN UINT8 Head, 487 IN UINT8 SectorCount, 488 IN UINT8 SectorNumber, 489 IN UINT8 CylinderLsb, 490 IN UINT8 CylinderMsb 491 ) 492{ 493 UINTN WordCount; 494 UINTN Increment; 495 UINT16 *Buffer16; 496 EFI_STATUS Status; 497 498 Status = WaitForBSYClear (IdeDev, ATATIMEOUT); 499 if (EFI_ERROR (Status)) { 500 return EFI_DEVICE_ERROR; 501 } 502 503 // 504 // select device via Head/Device register. 505 // Before write Head/Device register, BSY and DRQ must be 0. 506 // 507 Status = DRQClear2 (IdeDev, ATATIMEOUT); 508 if (EFI_ERROR (Status)) { 509 return EFI_DEVICE_ERROR; 510 } 511 512 // 513 // e0:1110,0000-- bit7 and bit5 are reserved bits. 514 // bit6 set means LBA mode 515 // 516 IDEWritePortB ( 517 IdeDev->PciIo, 518 IdeDev->IoPort->Head, 519 (UINT8) ((IdeDev->Device << 4) | 0xe0 | Head) 520 ); 521 522 Status = DRDYReady (IdeDev, ATATIMEOUT); 523 if (EFI_ERROR (Status)) { 524 return EFI_DEVICE_ERROR; 525 } 526 527 // 528 // set all the command parameters 529 // Before write to all the following registers, BSY and DRQ must be 0. 530 // 531 Status = DRQClear2 (IdeDev, ATATIMEOUT); 532 if (EFI_ERROR (Status)) { 533 return EFI_DEVICE_ERROR; 534 } 535 536 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount); 537 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, SectorNumber); 538 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, CylinderLsb); 539 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, CylinderMsb); 540 541 // 542 // send command via Command Register 543 // 544 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand); 545 546 Buffer16 = (UINT16 *) Buffer; 547 548 // 549 // According to PIO data out protocol, host can perform a series of 550 // writes to the data register after each time device set DRQ ready; 551 // The data size of "a series of read" is command specific. 552 // For most ATA command, data size written to device will not exceed 1 sector, 553 // hence the data size for "a series of write" can be the data size of one 554 // command request. 555 // For ATA command such as Write Sector command, the data size of one 556 // ATA command request is often larger than 1 sector, according to the 557 // Write Sector command, the data size of "a series of read" is exactly 558 // 1 sector. 559 // Here for simplification reason, we specify the data size for 560 // "a series of write" to 1 sector (256 words) if data size of one ATA command 561 // request is larger than 256 words. 562 // 563 Increment = 256; 564 WordCount = 0; 565 566 while (WordCount < ByteCount / 2) { 567 568 // 569 // DRQReady2-- read Alternate Status Register to determine the DRQ bit 570 // data transfer can be performed only when DRQ is ready. 571 // 572 Status = DRQReady2 (IdeDev, ATATIMEOUT); 573 if (EFI_ERROR (Status)) { 574 return EFI_DEVICE_ERROR; 575 } 576 577 Status = CheckErrorStatus (IdeDev); 578 if (EFI_ERROR (Status)) { 579 return EFI_DEVICE_ERROR; 580 } 581 582 // 583 // Check the remaining byte count is less than 512 bytes 584 // 585 if ((WordCount + Increment) > ByteCount / 2) { 586 Increment = ByteCount / 2 - WordCount; 587 } 588 // 589 // perform a series of write without check DRQ ready 590 // 591 592 IDEWritePortWMultiple ( 593 IdeDev->PciIo, 594 IdeDev->IoPort->Data, 595 Increment, 596 Buffer16 597 ); 598 WordCount += Increment; 599 Buffer16 += Increment; 600 601 } 602 603 DRQClear (IdeDev, ATATIMEOUT); 604 605 return CheckErrorStatus (IdeDev); 606} 607 608/** 609 This function is used to analyze the Status Register and print out 610 some debug information and if there is ERR bit set in the Status 611 Register, the Error Register's value is also be parsed and print out. 612 613 @param[in] *IdeDev 614 pointer pointing to IDE_BLK_IO_DEV data structure, used 615 to record all the information of the IDE device. 616 617 @retval EFI_SUCCESS No err information in the Status Register. 618 @retval EFI_DEVICE_ERROR Any err information in the Status Register. 619 620**/ 621EFI_STATUS 622CheckErrorStatus ( 623 IN IDE_BLK_IO_DEV *IdeDev 624 ) 625{ 626 UINT8 StatusRegister; 627 UINT8 ErrorRegister; 628 629 StatusRegister = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Status); 630 631 DEBUG_CODE_BEGIN (); 632 633 if (StatusRegister & ATA_STSREG_DWF) { 634 DEBUG ( 635 (EFI_D_BLKIO, 636 "CheckErrorStatus()-- %02x : Error : Write Fault\n", 637 StatusRegister) 638 ); 639 } 640 641 if (StatusRegister & ATA_STSREG_CORR) { 642 DEBUG ( 643 (EFI_D_BLKIO, 644 "CheckErrorStatus()-- %02x : Error : Corrected Data\n", 645 StatusRegister) 646 ); 647 } 648 649 if (StatusRegister & ATA_STSREG_ERR) { 650 ErrorRegister = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Error); 651 652 if (ErrorRegister & ATA_ERRREG_BBK) { 653 DEBUG ( 654 (EFI_D_BLKIO, 655 "CheckErrorStatus()-- %02x : Error : Bad Block Detected\n", 656 ErrorRegister) 657 ); 658 } 659 660 if (ErrorRegister & ATA_ERRREG_UNC) { 661 DEBUG ( 662 (EFI_D_BLKIO, 663 "CheckErrorStatus()-- %02x : Error : Uncorrectable Data\n", 664 ErrorRegister) 665 ); 666 } 667 668 if (ErrorRegister & ATA_ERRREG_MC) { 669 DEBUG ( 670 (EFI_D_BLKIO, 671 "CheckErrorStatus()-- %02x : Error : Media Change\n", 672 ErrorRegister) 673 ); 674 } 675 676 if (ErrorRegister & ATA_ERRREG_ABRT) { 677 DEBUG ( 678 (EFI_D_BLKIO, 679 "CheckErrorStatus()-- %02x : Error : Abort\n", 680 ErrorRegister) 681 ); 682 } 683 684 if (ErrorRegister & ATA_ERRREG_TK0NF) { 685 DEBUG ( 686 (EFI_D_BLKIO, 687 "CheckErrorStatus()-- %02x : Error : Track 0 Not Found\n", 688 ErrorRegister) 689 ); 690 } 691 692 if (ErrorRegister & ATA_ERRREG_AMNF) { 693 DEBUG ( 694 (EFI_D_BLKIO, 695 "CheckErrorStatus()-- %02x : Error : Address Mark Not Found\n", 696 ErrorRegister) 697 ); 698 } 699 } 700 701 DEBUG_CODE_END (); 702 703 if ((StatusRegister & (ATA_STSREG_ERR | ATA_STSREG_DWF | ATA_STSREG_CORR)) == 0) { 704 return EFI_SUCCESS; 705 } 706 707 return EFI_DEVICE_ERROR; 708 709} 710 711/** 712 This function is called by the AtaBlkIoReadBlocks() to perform 713 reading from media in block unit. 714 715 @param[in] *IdeDev 716 pointer pointing to IDE_BLK_IO_DEV data structure, used 717 to record all the information of the IDE device. 718 719 @param[in] *DataBuffer 720 A pointer to the destination buffer for the data. 721 722 @param[in] Lba 723 The starting logical block address to read from 724 on the device media. 725 726 @param[in] NumberOfBlocks 727 The number of transfer data blocks. 728 729 @return return status is fully dependent on the return status 730 of AtaPioDataIn() function. 731 732**/ 733EFI_STATUS 734AtaReadSectors ( 735 IN IDE_BLK_IO_DEV *IdeDev, 736 IN VOID *DataBuffer, 737 IN EFI_LBA Lba, 738 IN UINTN NumberOfBlocks 739 ) 740{ 741 EFI_STATUS Status; 742 UINTN BlocksRemaining; 743 UINT32 Lba32; 744 UINT8 Lba0; 745 UINT8 Lba1; 746 UINT8 Lba2; 747 UINT8 Lba3; 748 UINT8 AtaCommand; 749 UINT8 SectorCount8; 750 UINT16 SectorCount; 751 UINTN ByteCount; 752 VOID *Buffer; 753 754 Buffer = DataBuffer; 755 756 // 757 // Using ATA Read Sector(s) command (opcode=0x20) with PIO DATA IN protocol 758 // 759 AtaCommand = ATA_CMD_READ_SECTORS; 760 761 762 BlocksRemaining = NumberOfBlocks; 763 764 Lba32 = (UINT32) Lba; 765 766 Status = EFI_SUCCESS; 767 768 while (BlocksRemaining > 0) { 769 770 // 771 // in ATA-3 spec, LBA is in 28 bit width 772 // 773 Lba0 = (UINT8) Lba32; 774 Lba1 = (UINT8) (Lba32 >> 8); 775 Lba2 = (UINT8) (Lba32 >> 16); 776 // 777 // low 4 bit of Lba3 stands for LBA bit24~bit27. 778 // 779 Lba3 = (UINT8) ((Lba32 >> 24) & 0x0f); 780 781 if (BlocksRemaining >= 0x100) { 782 783 // 784 // SectorCount8 is sent to Sector Count register, 0x00 means 256 785 // sectors to be read 786 // 787 SectorCount8 = 0x00; 788 // 789 // SectorCount is used to record the number of sectors to be read 790 // 791 SectorCount = 256; 792 } else { 793 794 SectorCount8 = (UINT8) BlocksRemaining; 795 SectorCount = (UINT16) BlocksRemaining; 796 } 797 798 // 799 // ByteCount is the number of bytes that will be read 800 // 801 ByteCount = SectorCount * (IdeDev->BlkIo.Media->BlockSize); 802 803 // 804 // call AtaPioDataIn() to send Read Sector Command and receive data read 805 // 806 Status = AtaPioDataIn ( 807 IdeDev, 808 Buffer, 809 (UINT32) ByteCount, 810 AtaCommand, 811 Lba3, 812 SectorCount8, 813 Lba0, 814 Lba1, 815 Lba2 816 ); 817 if (EFI_ERROR (Status)) { 818 return Status; 819 } 820 821 Lba32 += SectorCount; 822 Buffer = ((UINT8 *) Buffer + ByteCount); 823 BlocksRemaining -= SectorCount; 824 } 825 826 return Status; 827} 828 829/** 830 This function is called by the AtaBlkIoWriteBlocks() to perform 831 writing onto media in block unit. 832 833 @param[in] *IdeDev 834 pointer pointing to IDE_BLK_IO_DEV data structure,used 835 to record all the information of the IDE device. 836 837 @param[in] *BufferData 838 A pointer to the source buffer for the data. 839 840 @param[in] Lba 841 The starting logical block address to write onto 842 the device media. 843 844 @param[in] NumberOfBlocks 845 The number of transfer data blocks. 846 847 @return return status is fully dependent on the return status 848 of AtaPioDataOut() function. 849 850**/ 851EFI_STATUS 852AtaWriteSectors ( 853 IN IDE_BLK_IO_DEV *IdeDev, 854 IN VOID *BufferData, 855 IN EFI_LBA Lba, 856 IN UINTN NumberOfBlocks 857 ) 858{ 859 EFI_STATUS Status; 860 UINTN BlocksRemaining; 861 UINT32 Lba32; 862 UINT8 Lba0; 863 UINT8 Lba1; 864 UINT8 Lba2; 865 UINT8 Lba3; 866 UINT8 AtaCommand; 867 UINT8 SectorCount8; 868 UINT16 SectorCount; 869 UINTN ByteCount; 870 VOID *Buffer; 871 872 Buffer = BufferData; 873 874 // 875 // Using Write Sector(s) command (opcode=0x30) with PIO DATA OUT protocol 876 // 877 AtaCommand = ATA_CMD_WRITE_SECTORS; 878 879 BlocksRemaining = NumberOfBlocks; 880 881 Lba32 = (UINT32) Lba; 882 883 Status = EFI_SUCCESS; 884 885 while (BlocksRemaining > 0) { 886 887 Lba0 = (UINT8) Lba32; 888 Lba1 = (UINT8) (Lba32 >> 8); 889 Lba2 = (UINT8) (Lba32 >> 16); 890 Lba3 = (UINT8) ((Lba32 >> 24) & 0x0f); 891 892 if (BlocksRemaining >= 0x100) { 893 894 // 895 // SectorCount8 is sent to Sector Count register, 0x00 means 256 sectors 896 // to be written 897 // 898 SectorCount8 = 0x00; 899 // 900 // SectorCount is used to record the number of sectors to be written 901 // 902 SectorCount = 256; 903 } else { 904 905 SectorCount8 = (UINT8) BlocksRemaining; 906 SectorCount = (UINT16) BlocksRemaining; 907 } 908 909 ByteCount = SectorCount * (IdeDev->BlkIo.Media->BlockSize); 910 911 Status = AtaPioDataOut ( 912 IdeDev, 913 Buffer, 914 (UINT32) ByteCount, 915 AtaCommand, 916 Lba3, 917 SectorCount8, 918 Lba0, 919 Lba1, 920 Lba2 921 ); 922 if (EFI_ERROR (Status)) { 923 return Status; 924 } 925 926 Lba32 += SectorCount; 927 Buffer = ((UINT8 *) Buffer + ByteCount); 928 BlocksRemaining -= SectorCount; 929 } 930 931 return Status; 932} 933 934/** 935 This function is used to implement the Soft Reset on the specified 936 device. But, the ATA Soft Reset mechanism is so strong a reset method 937 that it will force resetting on both devices connected to the 938 same cable. 939 940 It is called by IdeBlkIoReset(), a interface function of Block 941 I/O protocol. 942 943 This function can also be used by the ATAPI device to perform reset when 944 ATAPI Reset command is failed. 945 946 @param[in] *IdeDev 947 pointer pointing to IDE_BLK_IO_DEV data structure, used 948 to record all the information of the IDE device. 949 950 @retval EFI_SUCCESS Soft reset completes successfully. 951 @retval EFI_DEVICE_ERROR Any step during the reset process is failed. 952 953 @note 954 The registers initial values after ATA soft reset are different 955 to the ATA device and ATAPI device. 956 957**/ 958EFI_STATUS 959AtaSoftReset ( 960 IN IDE_BLK_IO_DEV *IdeDev 961 ) 962{ 963 964 UINT8 DeviceControl; 965 966 DeviceControl = 0; 967 // 968 // set SRST bit to initiate soft reset 969 // 970 DeviceControl |= ATA_CTLREG_SRST; 971 972 // 973 // disable Interrupt 974 // 975 DeviceControl |= BIT1; 976 977 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl); 978 979 // 980 // SRST should assert for at least 5 us, we use 10 us for 981 // better compatibility 982 // 983 gBS->Stall (10); 984 985 // 986 // Enable interrupt to support UDMA, and clear SRST bit 987 // 988 DeviceControl = 0; 989 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl); 990 991 // 992 // Wait for at least 2 ms to check BSY status, we use 10 ms 993 // for better compatibility 994 // 995 gBS->Stall(10000); 996 // 997 // slave device needs at most 31s to clear BSY 998 // 999 if (WaitForBSYClear (IdeDev, 31000) == EFI_TIMEOUT) { 1000 return EFI_DEVICE_ERROR; 1001 } 1002 1003 return EFI_SUCCESS; 1004} 1005 1006/** 1007 This function is the ATA implementation for ReadBlocks in the 1008 Block I/O Protocol interface. 1009 1010 @param[in] *IdeBlkIoDevice 1011 Indicates the calling context. 1012 1013 @param[in] MediaId 1014 The media id that the read request is for. 1015 1016 @param[in] LBA 1017 The starting logical block address to read from 1018 on the device. 1019 1020 @param[in] BufferSize 1021 The size of the Buffer in bytes. This must be a 1022 multiple of the intrinsic block size of the device. 1023 1024 @param[out] *Buffer 1025 A pointer to the destination buffer for the data. 1026 The caller is responsible for either having implicit 1027 or explicit ownership of the memory that data is read into. 1028 1029 @retval EFI_SUCCESS Read Blocks successfully. 1030 @retval EFI_DEVICE_ERROR Read Blocks failed. 1031 @retval EFI_NO_MEDIA There is no media in the device. 1032 @retval EFI_MEDIA_CHANGE The MediaId is not for the current media. 1033 1034 @retval EFI_BAD_BUFFER_SIZE 1035 The BufferSize parameter is not a multiple of the 1036 intrinsic block size of the device. 1037 1038 @retval EFI_INVALID_PARAMETER 1039 The read request contains LBAs that are not valid, 1040 or the data buffer is not valid. 1041 1042 @note 1043 If Read Block error because of device error, this function will call 1044 AtaSoftReset() function to reset device. 1045 1046**/ 1047EFI_STATUS 1048AtaBlkIoReadBlocks ( 1049 IN IDE_BLK_IO_DEV *IdeBlkIoDevice, 1050 IN UINT32 MediaId, 1051 IN EFI_LBA LBA, 1052 IN UINTN BufferSize, 1053 OUT VOID *Buffer 1054 ) 1055{ 1056 EFI_BLOCK_IO_MEDIA *Media; 1057 UINTN BlockSize; 1058 UINTN NumberOfBlocks; 1059 EFI_STATUS Status; 1060 1061 if (Buffer == NULL) { 1062 return EFI_INVALID_PARAMETER; 1063 } 1064 1065 if (BufferSize == 0) { 1066 return EFI_SUCCESS; 1067 } 1068 1069 Status = EFI_SUCCESS; 1070 1071 // 1072 // Get the intrinsic block size 1073 // 1074 Media = IdeBlkIoDevice->BlkIo.Media; 1075 BlockSize = Media->BlockSize; 1076 1077 NumberOfBlocks = BufferSize / BlockSize; 1078 1079 if (MediaId != Media->MediaId) { 1080 return EFI_MEDIA_CHANGED; 1081 } 1082 1083 if (BufferSize % BlockSize != 0) { 1084 return EFI_BAD_BUFFER_SIZE; 1085 } 1086 1087 if (!(Media->MediaPresent)) { 1088 return EFI_NO_MEDIA; 1089 } 1090 1091 if (LBA > Media->LastBlock) { 1092 return EFI_INVALID_PARAMETER; 1093 } 1094 1095 if ((LBA + NumberOfBlocks - 1) > Media->LastBlock) { 1096 return EFI_INVALID_PARAMETER; 1097 } 1098 1099 if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) { 1100 return EFI_INVALID_PARAMETER; 1101 } 1102 1103 Status = EFI_SUCCESS; 1104 if (IdeBlkIoDevice->Type == Ide48bitAddressingHardDisk) { 1105 // 1106 // For ATA/ATAPI-6 device(capcity > 120GB), use ATA-6 read block mechanism 1107 // 1108 if (IdeBlkIoDevice->UdmaMode.Valid) { 1109 Status = AtaUdmaReadExt (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks); 1110 } else { 1111 Status = AtaReadSectorsExt (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks); 1112 } 1113 } else { 1114 // 1115 // For ATA-3 compatible device, use ATA-3 read block mechanism 1116 // 1117 if (IdeBlkIoDevice->UdmaMode.Valid) { 1118 Status = AtaUdmaRead (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks); 1119 } else { 1120 Status = AtaReadSectors (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks); 1121 } 1122 } 1123 1124 if (EFI_ERROR (Status)) { 1125 AtaSoftReset (IdeBlkIoDevice); 1126 return EFI_DEVICE_ERROR; 1127 } 1128 1129 return EFI_SUCCESS; 1130 1131} 1132 1133/** 1134 This function is the ATA implementation for WriteBlocks in the 1135 Block I/O Protocol interface. 1136 1137 @param[in] *IdeBlkIoDevice 1138 Indicates the calling context. 1139 1140 @param[in] MediaId 1141 The media id that the write request is for. 1142 1143 @param[in] LBA 1144 The starting logical block address to write onto 1145 the device. 1146 1147 @param[in] BufferSize 1148 The size of the Buffer in bytes. This must be a 1149 multiple of the intrinsic block size of the device. 1150 1151 @param[out] *Buffer 1152 A pointer to the source buffer for the data. 1153 The caller is responsible for either having implicit 1154 or explicit ownership of the memory that data is 1155 written from. 1156 1157 @retval EFI_SUCCESS Write Blocks successfully. 1158 @retval EFI_DEVICE_ERROR Write Blocks failed. 1159 @retval EFI_NO_MEDIA There is no media in the device. 1160 @retval EFI_MEDIA_CHANGE The MediaId is not for the current media. 1161 1162 @retval EFI_BAD_BUFFER_SIZE 1163 The BufferSize parameter is not a multiple of the 1164 intrinsic block size of the device. 1165 1166 @retval EFI_INVALID_PARAMETER 1167 The write request contains LBAs that are not valid, 1168 or the data buffer is not valid. 1169 1170 @note 1171 If Write Block error because of device error, this function will call 1172 AtaSoftReset() function to reset device. 1173 1174**/ 1175EFI_STATUS 1176AtaBlkIoWriteBlocks ( 1177 IN IDE_BLK_IO_DEV *IdeBlkIoDevice, 1178 IN UINT32 MediaId, 1179 IN EFI_LBA LBA, 1180 IN UINTN BufferSize, 1181 OUT VOID *Buffer 1182 ) 1183{ 1184 1185 EFI_BLOCK_IO_MEDIA *Media; 1186 UINTN BlockSize; 1187 UINTN NumberOfBlocks; 1188 EFI_STATUS Status; 1189 1190 if (Buffer == NULL) { 1191 return EFI_INVALID_PARAMETER; 1192 } 1193 1194 if (BufferSize == 0) { 1195 return EFI_SUCCESS; 1196 } 1197 1198 Status = EFI_SUCCESS; 1199 1200 // 1201 // Get the intrinsic block size 1202 // 1203 Media = IdeBlkIoDevice->BlkIo.Media; 1204 BlockSize = Media->BlockSize; 1205 NumberOfBlocks = BufferSize / BlockSize; 1206 1207 if (MediaId != Media->MediaId) { 1208 return EFI_MEDIA_CHANGED; 1209 } 1210 1211 if (BufferSize % BlockSize != 0) { 1212 return EFI_BAD_BUFFER_SIZE; 1213 } 1214 1215 if (LBA > Media->LastBlock) { 1216 return EFI_INVALID_PARAMETER; 1217 } 1218 1219 if ((LBA + NumberOfBlocks - 1) > Media->LastBlock) { 1220 return EFI_INVALID_PARAMETER; 1221 } 1222 1223 if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) { 1224 return EFI_INVALID_PARAMETER; 1225 } 1226 1227 Status = EFI_SUCCESS; 1228 if (IdeBlkIoDevice->Type == Ide48bitAddressingHardDisk) { 1229 // 1230 // For ATA/ATAPI-6 device(capcity > 120GB), use ATA-6 write block mechanism 1231 // 1232 if (IdeBlkIoDevice->UdmaMode.Valid) { 1233 Status = AtaUdmaWriteExt (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks); 1234 } else { 1235 Status = AtaWriteSectorsExt (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks); 1236 } 1237 } else { 1238 // 1239 // For ATA-3 compatible device, use ATA-3 write block mechanism 1240 // 1241 if (IdeBlkIoDevice->UdmaMode.Valid) { 1242 Status = AtaUdmaWrite (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks); 1243 } else { 1244 Status = AtaWriteSectors (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks); 1245 } 1246 } 1247 1248 if (EFI_ERROR (Status)) { 1249 AtaSoftReset (IdeBlkIoDevice); 1250 return EFI_DEVICE_ERROR; 1251 } 1252 1253 return EFI_SUCCESS; 1254} 1255 1256/** 1257 This function is called by the AtaBlkIoReadBlocks() to perform 1258 reading from media in block unit. The function has been enhanced to 1259 support >120GB access and transfer at most 65536 blocks per command 1260 1261 @param[in] *IdeDev 1262 pointer pointing to IDE_BLK_IO_DEV data structure, used 1263 to record all the information of the IDE device. 1264 1265 @param[in] *DataBuffer A pointer to the destination buffer for the data. 1266 @param[in] StartLba The starting logical block address to read from 1267 on the device media. 1268 @param[in] NumberOfBlocks The number of transfer data blocks. 1269 1270 @return return status is fully dependent on the return status 1271 of AtaPioDataInExt() function. 1272 1273**/ 1274EFI_STATUS 1275AtaReadSectorsExt ( 1276 IN IDE_BLK_IO_DEV *IdeDev, 1277 IN VOID *DataBuffer, 1278 IN EFI_LBA StartLba, 1279 IN UINTN NumberOfBlocks 1280 ) 1281{ 1282 EFI_STATUS Status; 1283 UINTN BlocksRemaining; 1284 EFI_LBA Lba64; 1285 UINT8 AtaCommand; 1286 UINT16 SectorCount; 1287 UINT32 ByteCount; 1288 VOID *Buffer; 1289 1290 // 1291 // Using ATA "Read Sectors Ext" command(opcode=0x24) with PIO DATA IN protocol 1292 // 1293 AtaCommand = ATA_CMD_READ_SECTORS_EXT; 1294 Buffer = DataBuffer; 1295 BlocksRemaining = NumberOfBlocks; 1296 Lba64 = StartLba; 1297 Status = EFI_SUCCESS; 1298 1299 while (BlocksRemaining > 0) { 1300 1301 if (BlocksRemaining >= 0x10000) { 1302 // 1303 // SectorCount is used to record the number of sectors to be read 1304 // Max 65536 sectors can be transfered at a time. 1305 // 1306 SectorCount = 0xffff; 1307 } else { 1308 SectorCount = (UINT16) BlocksRemaining; 1309 } 1310 1311 // 1312 // ByteCount is the number of bytes that will be read 1313 // 1314 ByteCount = SectorCount * (IdeDev->BlkIo.Media->BlockSize); 1315 1316 // 1317 // call AtaPioDataInExt() to send Read Sector Command and receive data read 1318 // 1319 Status = AtaPioDataInExt ( 1320 IdeDev, 1321 Buffer, 1322 ByteCount, 1323 AtaCommand, 1324 Lba64, 1325 SectorCount 1326 ); 1327 if (EFI_ERROR (Status)) { 1328 return Status; 1329 } 1330 1331 Lba64 += SectorCount; 1332 Buffer = ((UINT8 *) Buffer + ByteCount); 1333 BlocksRemaining -= SectorCount; 1334 } 1335 1336 return Status; 1337} 1338 1339/** 1340 This function is called by the AtaBlkIoWriteBlocks() to perform 1341 writing onto media in block unit. The function has been enhanced to 1342 support >120GB access and transfer at most 65536 blocks per command 1343 1344 @param[in] *IdeDev 1345 pointer pointing to IDE_BLK_IO_DEV data structure,used 1346 to record all the information of the IDE device. 1347 1348 @param[in] *DataBuffer 1349 A pointer to the source buffer for the data. 1350 1351 @param[in] Lba 1352 The starting logical block address to write onto 1353 the device media. 1354 1355 @param[in] NumberOfBlocks 1356 The number of transfer data blocks. 1357 1358 @return status is fully dependent on the return status 1359 of AtaPioDataOutExt() function. 1360 1361**/ 1362EFI_STATUS 1363AtaWriteSectorsExt ( 1364 IN IDE_BLK_IO_DEV *IdeDev, 1365 IN VOID *DataBuffer, 1366 IN EFI_LBA StartLba, 1367 IN UINTN NumberOfBlocks 1368 ) 1369{ 1370 EFI_STATUS Status; 1371 EFI_LBA Lba64; 1372 UINTN BlocksRemaining; 1373 UINT8 AtaCommand; 1374 UINT16 SectorCount; 1375 UINT32 ByteCount; 1376 VOID *Buffer; 1377 1378 // 1379 // Using ATA "Write Sectors Ext" cmd(opcode=0x24) with PIO DATA OUT protocol 1380 // 1381 AtaCommand = ATA_CMD_WRITE_SECTORS_EXT; 1382 Lba64 = StartLba; 1383 Buffer = DataBuffer; 1384 BlocksRemaining = NumberOfBlocks; 1385 1386 Status = EFI_SUCCESS; 1387 1388 while (BlocksRemaining > 0) { 1389 1390 if (BlocksRemaining >= 0x10000) { 1391 // 1392 // SectorCount is used to record the number of sectors to be written. 1393 // Max 65536 sectors can be transfered at a time. 1394 // 1395 SectorCount = 0xffff; 1396 } else { 1397 SectorCount = (UINT16) BlocksRemaining; 1398 } 1399 1400 // 1401 // ByteCount is the number of bytes that will be written 1402 // 1403 ByteCount = SectorCount * (IdeDev->BlkIo.Media->BlockSize); 1404 1405 // 1406 // Call AtaPioDataOutExt() to send "Write Sectors Ext" Command 1407 // 1408 Status = AtaPioDataOutExt ( 1409 IdeDev, 1410 Buffer, 1411 ByteCount, 1412 AtaCommand, 1413 Lba64, 1414 SectorCount 1415 ); 1416 if (EFI_ERROR (Status)) { 1417 return Status; 1418 } 1419 1420 Lba64 += SectorCount; 1421 Buffer = ((UINT8 *) Buffer + ByteCount); 1422 BlocksRemaining -= SectorCount; 1423 } 1424 1425 return Status; 1426} 1427 1428/** 1429 This function is used to send out ATA commands conforms to the 1430 PIO Data In Protocol, supporting ATA/ATAPI-6 standard 1431 1432 Comparing with ATA-3 data in protocol, we have two differents here:<BR> 1433 1. Do NOT wait for DRQ clear before sending command into IDE device.(the 1434 wait will frequently fail... cause writing function return error) 1435 1436 2. Do NOT wait for DRQ clear after all data readed.(the wait greatly 1437 slow down writing performance by 100 times!) 1438 1439 @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used 1440 to record all the information of the IDE device. 1441 1442 @param[in,out] *Buffer buffer contained data transferred from device to host. 1443 @param[in] ByteCount data size in byte unit of the buffer. 1444 @param[in] AtaCommand value of the Command Register 1445 @param[in] StartLba the start LBA of this transaction 1446 @param[in] SectorCount the count of sectors to be transfered 1447 1448 @retval EFI_SUCCESS send out the ATA command and device send required 1449 data successfully. 1450 1451 @retval EFI_DEVICE_ERROR command sent failed. 1452 1453**/ 1454EFI_STATUS 1455AtaPioDataInExt ( 1456 IN IDE_BLK_IO_DEV *IdeDev, 1457 IN OUT VOID *Buffer, 1458 IN UINT32 ByteCount, 1459 IN UINT8 AtaCommand, 1460 IN EFI_LBA StartLba, 1461 IN UINT16 SectorCount 1462 ) 1463{ 1464 UINT8 DevSel; 1465 UINT8 SectorCount8; 1466 UINT8 LbaLow; 1467 UINT8 LbaMid; 1468 UINT8 LbaHigh; 1469 UINTN WordCount; 1470 UINTN Increment; 1471 UINT16 *Buffer16; 1472 EFI_STATUS Status; 1473 1474 Status = WaitForBSYClear (IdeDev, ATATIMEOUT); 1475 if (EFI_ERROR (Status)) { 1476 return EFI_DEVICE_ERROR; 1477 } 1478 1479 // 1480 // Select device, set bit6 as 1 to indicate LBA mode is used 1481 // 1482 DevSel = (UINT8) (IdeDev->Device << 4); 1483 DevSel |= 0x40; 1484 IDEWritePortB ( 1485 IdeDev->PciIo, 1486 IdeDev->IoPort->Head, 1487 DevSel 1488 ); 1489 1490 // 1491 // Wait for DRDY singnal asserting. ATAPI device needn't wait 1492 // 1493 if ( (IdeDev->Type == IdeHardDisk) || 1494 (IdeDev->Type == Ide48bitAddressingHardDisk)) { 1495 1496 Status = DRDYReady (IdeDev, ATATIMEOUT); 1497 if (EFI_ERROR (Status)) { 1498 return EFI_DEVICE_ERROR; 1499 } 1500 } 1501 1502 // 1503 // Fill feature register if needed 1504 // 1505 if (AtaCommand == ATA_CMD_SET_FEATURES) { 1506 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, 0x03); 1507 } 1508 1509 // 1510 // Fill the sector count register, which is a two-byte FIFO. Need write twice. 1511 // 1512 SectorCount8 = (UINT8) (SectorCount >> 8); 1513 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8); 1514 1515 SectorCount8 = (UINT8) SectorCount; 1516 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8); 1517 1518 // 1519 // Fill the start LBA registers, which are also two-byte FIFO 1520 // 1521 LbaLow = (UINT8) RShiftU64 (StartLba, 24); 1522 LbaMid = (UINT8) RShiftU64 (StartLba, 32); 1523 LbaHigh = (UINT8) RShiftU64 (StartLba, 40); 1524 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow); 1525 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid); 1526 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh); 1527 1528 LbaLow = (UINT8) StartLba; 1529 LbaMid = (UINT8) RShiftU64 (StartLba, 8); 1530 LbaHigh = (UINT8) RShiftU64 (StartLba, 16); 1531 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow); 1532 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid); 1533 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh); 1534 1535 // 1536 // Send command via Command Register, invoking the processing of this command 1537 // 1538 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand); 1539 1540 Buffer16 = (UINT16 *) Buffer; 1541 1542 // 1543 // According to PIO data in protocol, host can perform a series of reads to 1544 // the data register after each time device set DRQ ready; 1545 // 1546 1547 // 1548 // 256 words 1549 // 1550 Increment = 256; 1551 1552 // 1553 // used to record bytes of currently transfered data 1554 // 1555 WordCount = 0; 1556 1557 while (WordCount < ByteCount / 2) { 1558 // 1559 // Poll DRQ bit set, data transfer can be performed only when DRQ is ready. 1560 // 1561 Status = DRQReady2 (IdeDev, ATATIMEOUT); 1562 if (EFI_ERROR (Status)) { 1563 return EFI_DEVICE_ERROR; 1564 } 1565 1566 Status = CheckErrorStatus (IdeDev); 1567 if (EFI_ERROR (Status)) { 1568 return EFI_DEVICE_ERROR; 1569 } 1570 1571 // 1572 // Get the byte count for one series of read 1573 // 1574 if ((WordCount + Increment) > ByteCount / 2) { 1575 Increment = ByteCount / 2 - WordCount; 1576 } 1577 1578 IDEReadPortWMultiple ( 1579 IdeDev->PciIo, 1580 IdeDev->IoPort->Data, 1581 Increment, 1582 Buffer16 1583 ); 1584 1585 WordCount += Increment; 1586 Buffer16 += Increment; 1587 1588 } 1589 1590 return CheckErrorStatus (IdeDev); 1591} 1592 1593/** 1594 This function is used to send out ATA commands conforms to the 1595 PIO Data Out Protocol, supporting ATA/ATAPI-6 standard 1596 1597 Comparing with ATA-3 data out protocol, we have two differents here:<BR> 1598 1. Do NOT wait for DRQ clear before sending command into IDE device.(the 1599 wait will frequently fail... cause writing function return error) 1600 1601 2. Do NOT wait for DRQ clear after all data readed.(the wait greatly 1602 slow down writing performance by 100 times!) 1603 1604 @param[in] *IdeDev 1605 pointer pointing to IDE_BLK_IO_DEV data structure, used 1606 to record all the information of the IDE device. 1607 1608 @param[in] *Buffer buffer contained data transferred from host to device. 1609 @param[in] ByteCount data size in byte unit of the buffer. 1610 @param[in] AtaCommand value of the Command Register 1611 @param[in] StartLba the start LBA of this transaction 1612 @param[in] SectorCount the count of sectors to be transfered 1613 1614 @retval EFI_SUCCESS send out the ATA command and device receive required 1615 data successfully. 1616 1617 @retval EFI_DEVICE_ERROR command sent failed. 1618 1619**/ 1620EFI_STATUS 1621AtaPioDataOutExt ( 1622 IN IDE_BLK_IO_DEV *IdeDev, 1623 IN VOID *Buffer, 1624 IN UINT32 ByteCount, 1625 IN UINT8 AtaCommand, 1626 IN EFI_LBA StartLba, 1627 IN UINT16 SectorCount 1628 ) 1629{ 1630 UINT8 DevSel; 1631 UINT8 SectorCount8; 1632 UINT8 LbaLow; 1633 UINT8 LbaMid; 1634 UINT8 LbaHigh; 1635 UINTN WordCount; 1636 UINTN Increment; 1637 UINT16 *Buffer16; 1638 EFI_STATUS Status; 1639 1640 Status = WaitForBSYClear (IdeDev, ATATIMEOUT); 1641 if (EFI_ERROR (Status)) { 1642 return EFI_DEVICE_ERROR; 1643 } 1644 1645 // 1646 // Select device. Set bit6 as 1 to indicate LBA mode is used 1647 // 1648 DevSel = (UINT8) (IdeDev->Device << 4); 1649 DevSel |= 0x40; 1650 IDEWritePortB ( 1651 IdeDev->PciIo, 1652 IdeDev->IoPort->Head, 1653 DevSel 1654 ); 1655 1656 // 1657 // Wait for DRDY singnal asserting. 1658 // 1659 Status = DRDYReady (IdeDev, ATATIMEOUT); 1660 if (EFI_ERROR (Status)) { 1661 return EFI_DEVICE_ERROR; 1662 } 1663 1664 // 1665 // Fill feature register if needed 1666 // 1667 if (AtaCommand == ATA_CMD_SET_FEATURES) { 1668 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, 0x03); 1669 } 1670 1671 // 1672 // Fill the sector count register, which is a two-byte FIFO. Need write twice. 1673 // 1674 SectorCount8 = (UINT8) (SectorCount >> 8); 1675 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8); 1676 1677 SectorCount8 = (UINT8) SectorCount; 1678 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8); 1679 1680 // 1681 // Fill the start LBA registers, which are also two-byte FIFO 1682 // 1683 LbaLow = (UINT8) RShiftU64 (StartLba, 24); 1684 LbaMid = (UINT8) RShiftU64 (StartLba, 32); 1685 LbaHigh = (UINT8) RShiftU64 (StartLba, 40); 1686 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow); 1687 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid); 1688 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh); 1689 1690 LbaLow = (UINT8) StartLba; 1691 LbaMid = (UINT8) RShiftU64 (StartLba, 8); 1692 LbaHigh = (UINT8) RShiftU64 (StartLba, 16); 1693 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow); 1694 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid); 1695 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh); 1696 1697 // 1698 // Send command via Command Register, invoking the processing of this command 1699 // 1700 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand); 1701 1702 Buffer16 = (UINT16 *) Buffer; 1703 1704 // 1705 // According to PIO Data Out protocol, host can perform a series of writes to 1706 // the data register after each time device set DRQ ready; 1707 // 1708 Increment = 256; 1709 1710 // 1711 // used to record bytes of currently transfered data 1712 // 1713 WordCount = 0; 1714 1715 while (WordCount < ByteCount / 2) { 1716 // 1717 // Poll DRQ bit set, data transfer can be performed only when DRQ is ready. 1718 // 1719 Status = DRQReady2 (IdeDev, ATATIMEOUT); 1720 if (EFI_ERROR (Status)) { 1721 return EFI_DEVICE_ERROR; 1722 } 1723 1724 Status = CheckErrorStatus (IdeDev); 1725 if (EFI_ERROR (Status)) { 1726 return EFI_DEVICE_ERROR; 1727 } 1728 1729 // 1730 // Write data into device by one series of writing to data register 1731 // 1732 if ((WordCount + Increment) > ByteCount / 2) { 1733 Increment = ByteCount / 2 - WordCount; 1734 } 1735 1736 IDEWritePortWMultiple ( 1737 IdeDev->PciIo, 1738 IdeDev->IoPort->Data, 1739 Increment, 1740 Buffer16 1741 ); 1742 1743 WordCount += Increment; 1744 Buffer16 += Increment; 1745 1746 } 1747 // 1748 // while 1749 // 1750 1751 return CheckErrorStatus (IdeDev); 1752} 1753 1754 1755/** 1756 Enable SMART of the disk if supported 1757 1758 @param[in] *IdeDev 1759 pointer pointing to IDE_BLK_IO_DEV data structure,used 1760 to record all the information of the IDE device. 1761 1762**/ 1763VOID 1764AtaSMARTSupport ( 1765 IN IDE_BLK_IO_DEV *IdeDev 1766 ) 1767{ 1768 EFI_STATUS Status; 1769 BOOLEAN SMARTSupported; 1770 UINT8 Device; 1771 EFI_IDENTIFY_DATA *TmpAtaIdentifyPointer; 1772 UINT8 DeviceSelect; 1773 UINT8 LBAMid; 1774 UINT8 LBAHigh; 1775 1776 // 1777 // Detect if the device supports S.M.A.R.T. 1778 // 1779 if ((IdeDev->pIdData->AtaData.command_set_supported_83 & 0xc000) != 0x4000) { 1780 // 1781 // Data in word 82 is not valid (bit15 shall be zero and bit14 shall be to one) 1782 // 1783 return ; 1784 } else { 1785 if ((IdeDev->pIdData->AtaData.command_set_supported_82 & 0x0001) != 0x0001) { 1786 // 1787 // S.M.A.R.T is not supported by the device 1788 // 1789 SMARTSupported = FALSE; 1790 } else { 1791 SMARTSupported = TRUE; 1792 } 1793 } 1794 1795 if (!SMARTSupported) { 1796 // 1797 // Report nonsupport status code 1798 // 1799 REPORT_STATUS_CODE ( 1800 EFI_ERROR_CODE | EFI_ERROR_MINOR, 1801 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_NOTSUPPORTED) 1802 ); 1803 } else { 1804 // 1805 // Enable this feature 1806 // 1807 REPORT_STATUS_CODE ( 1808 EFI_PROGRESS_CODE, 1809 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_ENABLE) 1810 ); 1811 1812 Device = (UINT8) ((IdeDev->Device << 4) | 0xe0); 1813 Status = AtaNonDataCommandIn ( 1814 IdeDev, 1815 ATA_CMD_SMART, 1816 Device, 1817 ATA_SMART_ENABLE_OPERATION, 1818 0, 1819 0, 1820 ATA_CONSTANT_4F, 1821 ATA_CONSTANT_C2 1822 ); 1823 // 1824 // Detect if this feature is enabled 1825 // 1826 TmpAtaIdentifyPointer = (EFI_IDENTIFY_DATA *) AllocateZeroPool (sizeof (EFI_IDENTIFY_DATA)); 1827 1828 DeviceSelect = (UINT8) ((IdeDev->Device) << 4); 1829 Status = AtaPioDataIn ( 1830 IdeDev, 1831 (VOID *) TmpAtaIdentifyPointer, 1832 sizeof (EFI_IDENTIFY_DATA), 1833 ATA_CMD_IDENTIFY_DRIVE, 1834 DeviceSelect, 1835 0, 1836 0, 1837 0, 1838 0 1839 ); 1840 if (EFI_ERROR (Status)) { 1841 gBS->FreePool (TmpAtaIdentifyPointer); 1842 return ; 1843 } 1844 1845 // 1846 // Check if the feature is enabled 1847 // 1848 if ((TmpAtaIdentifyPointer->AtaData.command_set_feature_enb_85 & 0x0001) == 0x0001) { 1849 // 1850 // Read status data 1851 // 1852 AtaNonDataCommandIn ( 1853 IdeDev, 1854 ATA_CMD_SMART, 1855 Device, 1856 ATA_SMART_RETURN_STATUS, 1857 0, 1858 0, 1859 ATA_CONSTANT_4F, 1860 ATA_CONSTANT_C2 1861 ); 1862 LBAMid = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb); 1863 LBAHigh = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb); 1864 1865 if ((LBAMid == 0x4f) && (LBAHigh == 0xc2)) { 1866 // 1867 // The threshold exceeded condition is not detected by the device 1868 // 1869 REPORT_STATUS_CODE ( 1870 EFI_PROGRESS_CODE, 1871 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_UNDERTHRESHOLD) 1872 ); 1873 1874 } else if ((LBAMid == 0xf4) && (LBAHigh == 0x2c)) { 1875 // 1876 // The threshold exceeded condition is detected by the device 1877 // 1878 REPORT_STATUS_CODE ( 1879 EFI_PROGRESS_CODE, 1880 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_OVERTHRESHOLD) 1881 ); 1882 } 1883 1884 } else { 1885 // 1886 // Report disabled status code 1887 // 1888 REPORT_STATUS_CODE ( 1889 EFI_ERROR_CODE | EFI_ERROR_MINOR, 1890 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_DISABLED) 1891 ); 1892 } 1893 1894 gBS->FreePool (TmpAtaIdentifyPointer); 1895 } 1896 1897 return ; 1898} 1899 1900/** 1901 Send ATA Ext command into device with NON_DATA protocol 1902 1903 @param IdeDev Standard IDE device private data structure 1904 @param AtaCommand The ATA command to be sent 1905 @param Device The value in Device register 1906 @param Feature The value in Feature register 1907 @param SectorCount The value in SectorCount register 1908 @param LbaAddress The LBA address in 48-bit mode 1909 1910 @retval EFI_SUCCESS Reading succeed 1911 @retval EFI_DEVICE_ERROR Error executing commands on this device 1912 1913**/ 1914EFI_STATUS 1915AtaCommandIssueExt ( 1916 IN IDE_BLK_IO_DEV *IdeDev, 1917 IN UINT8 AtaCommand, 1918 IN UINT8 Device, 1919 IN UINT16 Feature, 1920 IN UINT16 SectorCount, 1921 IN EFI_LBA LbaAddress 1922 ) 1923{ 1924 EFI_STATUS Status; 1925 UINT8 SectorCount8; 1926 UINT8 Feature8; 1927 UINT8 LbaLow; 1928 UINT8 LbaMid; 1929 UINT8 LbaHigh; 1930 1931 Status = WaitForBSYClear (IdeDev, ATATIMEOUT); 1932 if (EFI_ERROR (Status)) { 1933 return EFI_DEVICE_ERROR; 1934 } 1935 1936 // 1937 // Select device (bit4), set LBA mode(bit6) (use 0xe0 for compatibility) 1938 // 1939 IDEWritePortB ( 1940 IdeDev->PciIo, 1941 IdeDev->IoPort->Head, 1942 (UINT8) ((IdeDev->Device << 4) | 0xe0) 1943 ); 1944 1945 // 1946 // ATA commands for ATA device must be issued when DRDY is set 1947 // 1948 Status = DRDYReady (IdeDev, ATATIMEOUT); 1949 if (EFI_ERROR (Status)) { 1950 return EFI_DEVICE_ERROR; 1951 } 1952 1953 // 1954 // Pass parameter into device register block 1955 // 1956 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, Device); 1957 1958 // 1959 // Fill the feature register, which is a two-byte FIFO. Need write twice. 1960 // 1961 Feature8 = (UINT8) (Feature >> 8); 1962 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, Feature8); 1963 1964 Feature8 = (UINT8) Feature; 1965 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, Feature8); 1966 1967 // 1968 // Fill the sector count register, which is a two-byte FIFO. Need write twice. 1969 // 1970 SectorCount8 = (UINT8) (SectorCount >> 8); 1971 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8); 1972 1973 SectorCount8 = (UINT8) SectorCount; 1974 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8); 1975 1976 // 1977 // Fill the start LBA registers, which are also two-byte FIFO 1978 // 1979 LbaLow = (UINT8) RShiftU64 (LbaAddress, 24); 1980 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow); 1981 LbaLow = (UINT8) LbaAddress; 1982 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow); 1983 1984 LbaMid = (UINT8) RShiftU64 (LbaAddress, 32); 1985 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid); 1986 LbaMid = (UINT8) RShiftU64 (LbaAddress, 8); 1987 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid); 1988 1989 LbaHigh = (UINT8) RShiftU64 (LbaAddress, 40); 1990 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh); 1991 LbaHigh = (UINT8) RShiftU64 (LbaAddress, 16); 1992 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh); 1993 1994 // 1995 // Work around for Segate 160G disk writing 1996 // 1997 gBS->Stall (1800); 1998 1999 // 2000 // Send command via Command Register 2001 // 2002 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand); 2003 2004 // 2005 // Stall at least 400ns 2006 // 2007 gBS->Stall (100); 2008 2009 return EFI_SUCCESS; 2010} 2011 2012/** 2013 Send ATA Ext command into device with NON_DATA protocol 2014 2015 @param IdeDev Standard IDE device private data structure 2016 @param AtaCommand The ATA command to be sent 2017 @param Device The value in Device register 2018 @param Feature The value in Feature register 2019 @param SectorCount The value in SectorCount register 2020 @param LbaAddress The LBA address in 48-bit mode 2021 2022 @retval EFI_SUCCESS Reading succeed 2023 @retval EFI_DEVICE_ERROR Error executing commands on this device 2024 2025**/ 2026EFI_STATUS 2027AtaCommandIssue ( 2028 IN IDE_BLK_IO_DEV *IdeDev, 2029 IN UINT8 AtaCommand, 2030 IN UINT8 Device, 2031 IN UINT16 Feature, 2032 IN UINT16 SectorCount, 2033 IN EFI_LBA LbaAddress 2034 ) 2035{ 2036 EFI_STATUS Status; 2037 UINT8 SectorCount8; 2038 UINT8 Feature8; 2039 UINT8 Lba0; 2040 UINT8 Lba1; 2041 UINT8 Lba2; 2042 UINT8 Lba3; 2043 2044 Status = WaitForBSYClear (IdeDev, ATATIMEOUT); 2045 if (EFI_ERROR (Status)) { 2046 return EFI_DEVICE_ERROR; 2047 } 2048 2049 // 2050 // Select device (bit4), set LBA mode(bit6) (use 0xe0 for compatibility) 2051 // 2052 IDEWritePortB ( 2053 IdeDev->PciIo, 2054 IdeDev->IoPort->Head, 2055 (UINT8) ((IdeDev->Device << 4) | 0xe0) 2056 ); 2057 2058 // 2059 // ATA commands for ATA device must be issued when DRDY is set 2060 // 2061 Status = DRDYReady (IdeDev, ATATIMEOUT); 2062 if (EFI_ERROR (Status)) { 2063 return EFI_DEVICE_ERROR; 2064 } 2065 2066 Lba0 = (UINT8) LbaAddress; 2067 Lba1 = (UINT8) RShiftU64 (LbaAddress, 8); 2068 Lba2 = (UINT8) RShiftU64 (LbaAddress, 16); 2069 Lba3 = (UINT8) RShiftU64 (LbaAddress, 24); 2070 Device = (UINT8) (Device | Lba3); 2071 2072 // 2073 // Pass parameter into device register block 2074 // 2075 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, Device); 2076 2077 // 2078 // Fill the feature register, which is a two-byte FIFO. Need write twice. 2079 // 2080 Feature8 = (UINT8) Feature; 2081 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, Feature8); 2082 2083 // 2084 // Fill the sector count register, which is a two-byte FIFO. Need write twice. 2085 // 2086 SectorCount8 = (UINT8) SectorCount; 2087 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8); 2088 2089 // 2090 // Fill the start LBA registers, which are also two-byte FIFO 2091 // 2092 2093 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, Lba0); 2094 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, Lba1); 2095 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, Lba2); 2096 2097 // 2098 // Send command via Command Register 2099 // 2100 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand); 2101 2102 // 2103 // Stall at least 400ns 2104 // 2105 gBS->Stall (100); 2106 2107 return EFI_SUCCESS; 2108} 2109 2110/** 2111 This function is called by the AtaBlkIoReadBlocks() to perform 2112 reading from media in block unit. The function has been enhanced to 2113 support >120GB access and transfer at most 65536 blocks per command 2114 2115 @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used 2116 to record all the information of the IDE device. 2117 2118 @param[in] *DataBuffer A pointer to the destination buffer for the data. 2119 2120 @param[in] StartLba The starting logical block address to read from 2121 on the device media. 2122 2123 @param[in] NumberOfBlocks The number of transfer data blocks. 2124 2125 @return The device status of UDMA operation. If the operation is 2126 successful, return EFI_SUCCESS. 2127 2128 TODO: EFI_UNSUPPORTED - add return value to function comment 2129 TODO: EFI_DEVICE_ERROR - add return value to function comment 2130 TODO: EFI_DEVICE_ERROR - add return value to function comment 2131 TODO: EFI_DEVICE_ERROR - add return value to function comment 2132**/ 2133EFI_STATUS 2134AtaUdmaReadExt ( 2135 IN IDE_BLK_IO_DEV *IdeDev, 2136 IN VOID *DataBuffer, 2137 IN EFI_LBA StartLba, 2138 IN UINTN NumberOfBlocks 2139 ) 2140{ 2141 return DoAtaUdma (IdeDev, DataBuffer, StartLba, NumberOfBlocks, AtaUdmaReadExtOp); 2142} 2143 2144/** 2145 This function is called by the AtaBlkIoReadBlocks() to perform 2146 reading from media in block unit. The function has been enhanced to 2147 support >120GB access and transfer at most 65536 blocks per command 2148 2149 @param[in] *IdeDev 2150 pointer pointing to IDE_BLK_IO_DEV data structure, used 2151 to record all the information of the IDE device. 2152 2153 @param[in] *DataBuffer A pointer to the destination buffer for the data. 2154 @param[in] StartLba The starting logical block address to read from 2155 on the device media. 2156 @param[in] NumberOfBlocks The number of transfer data blocks. 2157 2158 @return The device status of UDMA operation. If the operation is 2159 successful, return EFI_SUCCESS. 2160 2161 TODO: EFI_UNSUPPORTED - add return value to function comment 2162 TODO: EFI_DEVICE_ERROR - add return value to function comment 2163 TODO: EFI_DEVICE_ERROR - add return value to function comment 2164 TODO: EFI_DEVICE_ERROR - add return value to function comment 2165**/ 2166EFI_STATUS 2167AtaUdmaRead ( 2168 IN IDE_BLK_IO_DEV *IdeDev, 2169 IN VOID *DataBuffer, 2170 IN EFI_LBA StartLba, 2171 IN UINTN NumberOfBlocks 2172 ) 2173{ 2174 return DoAtaUdma (IdeDev, DataBuffer, StartLba, NumberOfBlocks, AtaUdmaReadOp); 2175} 2176 2177/** 2178 This function is called by the AtaBlkIoWriteBlocks() to perform 2179 writing to media in block unit. The function has been enhanced to 2180 support >120GB access and transfer at most 65536 blocks per command 2181 2182 @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used 2183 to record all the information of the IDE device. 2184 2185 @param[in] *DataBuffer A pointer to the source buffer for the data. 2186 2187 @param[in] StartLba The starting logical block address to write to 2188 on the device media. 2189 2190 @param[in] NumberOfBlocks The number of transfer data blocks. 2191 2192 @return The device status of UDMA operation. If the operation is 2193 successful, return EFI_SUCCESS. 2194 2195 TODO: EFI_UNSUPPORTED - add return value to function comment 2196 TODO: EFI_DEVICE_ERROR - add return value to function comment 2197 TODO: EFI_DEVICE_ERROR - add return value to function comment 2198**/ 2199EFI_STATUS 2200AtaUdmaWriteExt ( 2201 IN IDE_BLK_IO_DEV *IdeDev, 2202 IN VOID *DataBuffer, 2203 IN EFI_LBA StartLba, 2204 IN UINTN NumberOfBlocks 2205 ) 2206{ 2207 return DoAtaUdma (IdeDev, DataBuffer, StartLba, NumberOfBlocks, AtaUdmaWriteExtOp); 2208} 2209 2210/** 2211 This function is called by the AtaBlkIoWriteBlocks() to perform 2212 writing to media in block unit. The function has been enhanced to 2213 support >120GB access and transfer at most 65536 blocks per command 2214 2215 @param[in] *IdeDev 2216 pointer pointing to IDE_BLK_IO_DEV data structure, used 2217 to record all the information of the IDE device. 2218 2219 @param[in] *DataBuffer 2220 A pointer to the source buffer for the data. 2221 2222 @param[in] StartLba 2223 The starting logical block address to write to 2224 on the device media. 2225 2226 @param[in] NumberOfBlocks 2227 The number of transfer data blocks. 2228 2229 @return The device status of UDMA operation. If the operation is 2230 successful, return EFI_SUCCESS. 2231 2232 TODO: EFI_UNSUPPORTED - add return value to function comment 2233 TODO: EFI_DEVICE_ERROR - add return value to function comment 2234 TODO: EFI_DEVICE_ERROR - add return value to function comment 2235**/ 2236EFI_STATUS 2237AtaUdmaWrite ( 2238 IN IDE_BLK_IO_DEV *IdeDev, 2239 IN VOID *DataBuffer, 2240 IN EFI_LBA StartLba, 2241 IN UINTN NumberOfBlocks 2242 ) 2243{ 2244 return DoAtaUdma (IdeDev, DataBuffer, StartLba, NumberOfBlocks, AtaUdmaWriteOp); 2245} 2246 2247/** 2248 Perform an ATA Udma operation (Read, ReadExt, Write, WriteExt). 2249 2250 @param[in] *IdeDev 2251 pointer pointing to IDE_BLK_IO_DEV data structure, used 2252 to record all the information of the IDE device. 2253 2254 @param[in] *DataBuffer 2255 A pointer to the source buffer for the data. 2256 2257 @param[in] StartLba 2258 The starting logical block address to write to 2259 on the device media. 2260 2261 @param[in] NumberOfBlocks 2262 The number of transfer data blocks. 2263 2264 @param[in] UdmaOp 2265 The perform operations could be AtaUdmaReadOp, AtaUdmaReadExOp, 2266 AtaUdmaWriteOp, AtaUdmaWriteExOp 2267 2268 @return The device status of UDMA operation. If the operation is 2269 successful, return EFI_SUCCESS. 2270 2271**/ 2272EFI_STATUS 2273DoAtaUdma ( 2274 IN IDE_BLK_IO_DEV *IdeDev, 2275 IN VOID *DataBuffer, 2276 IN EFI_LBA StartLba, 2277 IN UINTN NumberOfBlocks, 2278 IN ATA_UDMA_OPERATION UdmaOp 2279 ) 2280{ 2281 IDE_DMA_PRD *PrdAddr; 2282 IDE_DMA_PRD *UsedPrdAddr; 2283 IDE_DMA_PRD *TempPrdAddr; 2284 UINT8 RegisterValue; 2285 UINT8 Device; 2286 UINT64 IoPortForBmic; 2287 UINT64 IoPortForBmis; 2288 UINT64 IoPortForBmid; 2289 EFI_STATUS Status; 2290 UINTN PrdTableNum; 2291 UINTN ByteCount; 2292 UINTN ByteAvailable; 2293 UINT8 *PrdBuffer; 2294 UINTN RemainBlockNum; 2295 UINT8 DeviceControl; 2296 UINT32 Count; 2297 UINTN PageCount; 2298 VOID *Map; 2299 VOID *MemPage; 2300 EFI_PHYSICAL_ADDRESS DeviceAddress; 2301 UINTN MaxDmaCommandSectors; 2302 EFI_PCI_IO_PROTOCOL_OPERATION PciIoProtocolOp; 2303 UINT8 AtaCommand; 2304 2305 switch (UdmaOp) { 2306 case AtaUdmaReadOp: 2307 MaxDmaCommandSectors = ATAPI_MAX_DMA_CMD_SECTORS; 2308 PciIoProtocolOp = EfiPciIoOperationBusMasterWrite; 2309 AtaCommand = ATA_CMD_READ_DMA; 2310 break; 2311 case AtaUdmaReadExtOp: 2312 MaxDmaCommandSectors = ATAPI_MAX_DMA_EXT_CMD_SECTORS; 2313 PciIoProtocolOp = EfiPciIoOperationBusMasterWrite; 2314 AtaCommand = ATA_CMD_READ_DMA_EXT; 2315 break; 2316 case AtaUdmaWriteOp: 2317 MaxDmaCommandSectors = ATAPI_MAX_DMA_CMD_SECTORS; 2318 PciIoProtocolOp = EfiPciIoOperationBusMasterRead; 2319 AtaCommand = ATA_CMD_WRITE_DMA; 2320 break; 2321 case AtaUdmaWriteExtOp: 2322 MaxDmaCommandSectors = ATAPI_MAX_DMA_EXT_CMD_SECTORS; 2323 PciIoProtocolOp = EfiPciIoOperationBusMasterRead; 2324 AtaCommand = ATA_CMD_WRITE_DMA_EXT; 2325 break; 2326 default: 2327 return EFI_UNSUPPORTED; 2328 break; 2329 } 2330 2331 // 2332 // Select device 2333 // 2334 Device = (UINT8) ((IdeDev->Device << 4) | 0xe0); 2335 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, Device); 2336 2337 // 2338 // Enable interrupt to support UDMA 2339 // 2340 DeviceControl = 0; 2341 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl); 2342 2343 if (IdePrimary == IdeDev->Channel) { 2344 IoPortForBmic = IdeDev->IoPort->BusMasterBaseAddr + BMICP_OFFSET; 2345 IoPortForBmis = IdeDev->IoPort->BusMasterBaseAddr + BMISP_OFFSET; 2346 IoPortForBmid = IdeDev->IoPort->BusMasterBaseAddr + BMIDP_OFFSET; 2347 } else { 2348 if (IdeSecondary == IdeDev->Channel) { 2349 IoPortForBmic = IdeDev->IoPort->BusMasterBaseAddr + BMICS_OFFSET; 2350 IoPortForBmis = IdeDev->IoPort->BusMasterBaseAddr + BMISS_OFFSET; 2351 IoPortForBmid = IdeDev->IoPort->BusMasterBaseAddr + BMIDS_OFFSET; 2352 } else { 2353 return EFI_UNSUPPORTED; 2354 } 2355 } 2356 2357 // 2358 // Read BMIS register and clear ERROR and INTR bit 2359 // 2360 IdeDev->PciIo->Io.Read ( 2361 IdeDev->PciIo, 2362 EfiPciIoWidthUint8, 2363 EFI_PCI_IO_PASS_THROUGH_BAR, 2364 IoPortForBmis, 2365 1, 2366 &RegisterValue 2367 ); 2368 2369 RegisterValue |= (BMIS_INTERRUPT | BMIS_ERROR); 2370 2371 IdeDev->PciIo->Io.Write ( 2372 IdeDev->PciIo, 2373 EfiPciIoWidthUint8, 2374 EFI_PCI_IO_PASS_THROUGH_BAR, 2375 IoPortForBmis, 2376 1, 2377 &RegisterValue 2378 ); 2379 2380 Status = EFI_SUCCESS; 2381 2382 RemainBlockNum = NumberOfBlocks; 2383 while (RemainBlockNum > 0) { 2384 2385 if (RemainBlockNum >= MaxDmaCommandSectors) { 2386 // 2387 // SectorCount is used to record the number of sectors to be read 2388 // Max 65536 sectors can be transfered at a time. 2389 // 2390 NumberOfBlocks = MaxDmaCommandSectors; 2391 RemainBlockNum -= MaxDmaCommandSectors; 2392 } else { 2393 NumberOfBlocks = (UINT16) RemainBlockNum; 2394 RemainBlockNum = 0; 2395 } 2396 2397 // 2398 // Calculate the number of PRD table to make sure the memory region 2399 // not cross 64K boundary 2400 // 2401 ByteCount = NumberOfBlocks * IdeDev->BlkIo.Media->BlockSize; 2402 PrdTableNum = ((ByteCount >> 16) + 1) + 1; 2403 2404 // 2405 // Build PRD table 2406 // 2407 PageCount = EFI_SIZE_TO_PAGES (2 * PrdTableNum * sizeof (IDE_DMA_PRD)); 2408 Status = IdeDev->PciIo->AllocateBuffer ( 2409 IdeDev->PciIo, 2410 AllocateAnyPages, 2411 EfiBootServicesData, 2412 PageCount, 2413 &MemPage, 2414 0 2415 ); 2416 if (EFI_ERROR (Status)) { 2417 return EFI_OUT_OF_RESOURCES; 2418 } 2419 ZeroMem ((VOID *) ((UINTN) MemPage), EFI_PAGES_TO_SIZE (PageCount)); 2420 2421 PrdAddr = (IDE_DMA_PRD *) ((UINTN) MemPage); 2422 // 2423 // To make sure PRD is allocated in one 64K page 2424 // 2425 if (((UINTN) PrdAddr & 0x0FFFF) > (((UINTN) PrdAddr + PrdTableNum * sizeof (IDE_DMA_PRD) - 1) & 0x0FFFF)) { 2426 UsedPrdAddr = (IDE_DMA_PRD *) ((UINTN) ((UINT8 *) PrdAddr + 0x10000) & 0xFFFF0000); 2427 } else { 2428 if ((UINTN) PrdAddr & 0x03) { 2429 UsedPrdAddr = (IDE_DMA_PRD *) ((UINTN) ((UINT8 *) PrdAddr + 0x04) & 0xFFFFFFFC); 2430 } else { 2431 UsedPrdAddr = PrdAddr; 2432 } 2433 } 2434 2435 // 2436 // Build the PRD table 2437 // 2438 Status = IdeDev->PciIo->Map ( 2439 IdeDev->PciIo, 2440 PciIoProtocolOp, 2441 DataBuffer, 2442 &ByteCount, 2443 &DeviceAddress, 2444 &Map 2445 ); 2446 if (EFI_ERROR (Status)) { 2447 IdeDev->PciIo->FreeBuffer (IdeDev->PciIo, PageCount, MemPage); 2448 return EFI_OUT_OF_RESOURCES; 2449 } 2450 PrdBuffer = (VOID *) ((UINTN) DeviceAddress); 2451 TempPrdAddr = UsedPrdAddr; 2452 while (TRUE) { 2453 2454 ByteAvailable = 0x10000 - ((UINTN) PrdBuffer & 0xFFFF); 2455 2456 if (ByteCount <= ByteAvailable) { 2457 TempPrdAddr->RegionBaseAddr = (UINT32) ((UINTN) PrdBuffer); 2458 TempPrdAddr->ByteCount = (UINT16) ByteCount; 2459 TempPrdAddr->EndOfTable = 0x8000; 2460 break; 2461 } 2462 2463 TempPrdAddr->RegionBaseAddr = (UINT32) ((UINTN) PrdBuffer); 2464 TempPrdAddr->ByteCount = (UINT16) ByteAvailable; 2465 2466 ByteCount -= ByteAvailable; 2467 PrdBuffer += ByteAvailable; 2468 TempPrdAddr++; 2469 } 2470 2471 // 2472 // Set the base address to BMID register 2473 // 2474 IdeDev->PciIo->Io.Write ( 2475 IdeDev->PciIo, 2476 EfiPciIoWidthUint32, 2477 EFI_PCI_IO_PASS_THROUGH_BAR, 2478 IoPortForBmid, 2479 1, 2480 &UsedPrdAddr 2481 ); 2482 2483 // 2484 // Set BMIC register to identify the operation direction 2485 // 2486 IdeDev->PciIo->Io.Read ( 2487 IdeDev->PciIo, 2488 EfiPciIoWidthUint8, 2489 EFI_PCI_IO_PASS_THROUGH_BAR, 2490 IoPortForBmic, 2491 1, 2492 &RegisterValue 2493 ); 2494 2495 if (UdmaOp == AtaUdmaReadExtOp || UdmaOp == AtaUdmaReadOp) { 2496 RegisterValue |= BMIC_nREAD; 2497 } else { 2498 RegisterValue &= ~((UINT8) BMIC_nREAD); 2499 } 2500 2501 IdeDev->PciIo->Io.Write ( 2502 IdeDev->PciIo, 2503 EfiPciIoWidthUint8, 2504 EFI_PCI_IO_PASS_THROUGH_BAR, 2505 IoPortForBmic, 2506 1, 2507 &RegisterValue 2508 ); 2509 2510 if (UdmaOp == AtaUdmaWriteExtOp || UdmaOp == AtaUdmaReadExtOp) { 2511 Status = AtaCommandIssueExt ( 2512 IdeDev, 2513 AtaCommand, 2514 Device, 2515 0, 2516 (UINT16) NumberOfBlocks, 2517 StartLba 2518 ); 2519 } else { 2520 Status = AtaCommandIssue ( 2521 IdeDev, 2522 AtaCommand, 2523 Device, 2524 0, 2525 (UINT16) NumberOfBlocks, 2526 StartLba 2527 ); 2528 } 2529 2530 if (EFI_ERROR (Status)) { 2531 IdeDev->PciIo->FreeBuffer (IdeDev->PciIo, PageCount, MemPage); 2532 IdeDev->PciIo->Unmap (IdeDev->PciIo, Map); 2533 return EFI_DEVICE_ERROR; 2534 } 2535 2536 // 2537 // Set START bit of BMIC register 2538 // 2539 IdeDev->PciIo->Io.Read ( 2540 IdeDev->PciIo, 2541 EfiPciIoWidthUint8, 2542 EFI_PCI_IO_PASS_THROUGH_BAR, 2543 IoPortForBmic, 2544 1, 2545 &RegisterValue 2546 ); 2547 2548 RegisterValue |= BMIC_START; 2549 2550 IdeDev->PciIo->Io.Write ( 2551 IdeDev->PciIo, 2552 EfiPciIoWidthUint8, 2553 EFI_PCI_IO_PASS_THROUGH_BAR, 2554 IoPortForBmic, 2555 1, 2556 &RegisterValue 2557 ); 2558 2559 // 2560 // Check the INTERRUPT and ERROR bit of BMIS 2561 // Max transfer number of sectors for one command is 65536(32Mbyte), 2562 // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps). 2563 // So set the variable Count to 2000, for about 2 second timeout time. 2564 // 2565 Status = EFI_SUCCESS; 2566 Count = 2000; 2567 while (TRUE) { 2568 2569 IdeDev->PciIo->Io.Read ( 2570 IdeDev->PciIo, 2571 EfiPciIoWidthUint8, 2572 EFI_PCI_IO_PASS_THROUGH_BAR, 2573 IoPortForBmis, 2574 1, 2575 &RegisterValue 2576 ); 2577 if ((RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) || (Count == 0)) { 2578 if ((RegisterValue & BMIS_ERROR) || (Count == 0)) { 2579 Status = EFI_DEVICE_ERROR; 2580 break; 2581 } 2582 break; 2583 } 2584 2585 gBS->Stall (1000); 2586 Count --; 2587 } 2588 2589 IdeDev->PciIo->FreeBuffer (IdeDev->PciIo, PageCount, MemPage); 2590 IdeDev->PciIo->Unmap (IdeDev->PciIo, Map); 2591 // 2592 // Read BMIS register and clear ERROR and INTR bit 2593 // 2594 IdeDev->PciIo->Io.Read ( 2595 IdeDev->PciIo, 2596 EfiPciIoWidthUint8, 2597 EFI_PCI_IO_PASS_THROUGH_BAR, 2598 IoPortForBmis, 2599 1, 2600 &RegisterValue 2601 ); 2602 2603 RegisterValue |= (BMIS_INTERRUPT | BMIS_ERROR); 2604 2605 IdeDev->PciIo->Io.Write ( 2606 IdeDev->PciIo, 2607 EfiPciIoWidthUint8, 2608 EFI_PCI_IO_PASS_THROUGH_BAR, 2609 IoPortForBmis, 2610 1, 2611 &RegisterValue 2612 ); 2613 // 2614 // Read Status Register of IDE device to clear interrupt 2615 // 2616 RegisterValue = IDEReadPortB(IdeDev->PciIo,IdeDev->IoPort->Reg.Status); 2617 // 2618 // Clear START bit of BMIC register 2619 // 2620 IdeDev->PciIo->Io.Read ( 2621 IdeDev->PciIo, 2622 EfiPciIoWidthUint8, 2623 EFI_PCI_IO_PASS_THROUGH_BAR, 2624 IoPortForBmic, 2625 1, 2626 &RegisterValue 2627 ); 2628 2629 RegisterValue &= ~((UINT8) BMIC_START); 2630 2631 IdeDev->PciIo->Io.Write ( 2632 IdeDev->PciIo, 2633 EfiPciIoWidthUint8, 2634 EFI_PCI_IO_PASS_THROUGH_BAR, 2635 IoPortForBmic, 2636 1, 2637 &RegisterValue 2638 ); 2639 2640 if (RegisterValue & BMIS_ERROR) { 2641 return EFI_DEVICE_ERROR; 2642 } 2643 2644 if (EFI_ERROR (Status)) { 2645 break; 2646 } 2647 DataBuffer = (UINT8 *) DataBuffer + NumberOfBlocks * IdeDev->BlkIo.Media->BlockSize; 2648 StartLba += NumberOfBlocks; 2649 } 2650 2651 // 2652 // Disable interrupt of Select device 2653 // 2654 IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl); 2655 DeviceControl |= ATA_CTLREG_IEN_L; 2656 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl); 2657 2658 return Status; 2659} 2660 2661