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