Home | History | Annotate | Download | only in AtaBusDxe
      1 /** @file
      2   This file implements protocol interfaces for ATA bus driver.
      3 
      4   This file implements protocol interfaces: Driver Binding protocol,
      5   Block IO protocol and DiskInfo protocol.
      6 
      7   Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
      8   This program and the accompanying materials
      9   are licensed and made available under the terms and conditions of the BSD License
     10   which accompanies this distribution.  The full text of the license may be found at
     11   http://opensource.org/licenses/bsd-license.php
     12 
     13   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     14   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     15 
     16 
     17 **/
     18 
     19 #include "AtaBus.h"
     20 
     21 //
     22 // ATA Bus Driver Binding Protocol Instance
     23 //
     24 EFI_DRIVER_BINDING_PROTOCOL gAtaBusDriverBinding = {
     25   AtaBusDriverBindingSupported,
     26   AtaBusDriverBindingStart,
     27   AtaBusDriverBindingStop,
     28   0x10,
     29   NULL,
     30   NULL
     31 };
     32 
     33 //
     34 // Template for ATA Child Device.
     35 //
     36 ATA_DEVICE gAtaDeviceTemplate = {
     37   ATA_DEVICE_SIGNATURE,        // Signature
     38   NULL,                        // Handle
     39   {                            // BlockIo
     40     EFI_BLOCK_IO_PROTOCOL_REVISION,
     41     NULL,
     42     AtaBlockIoReset,
     43     AtaBlockIoReadBlocks,
     44     AtaBlockIoWriteBlocks,
     45     AtaBlockIoFlushBlocks
     46   },
     47   {                            // BlockIo2
     48     NULL,
     49     AtaBlockIoResetEx,
     50     AtaBlockIoReadBlocksEx,
     51     AtaBlockIoWriteBlocksEx,
     52     AtaBlockIoFlushBlocksEx
     53   },
     54   {                            // BlockMedia
     55     0,                         // MediaId
     56     FALSE,                     // RemovableMedia
     57     TRUE,                      // MediaPresent
     58     FALSE,                     // LogicPartition
     59     FALSE,                     // ReadOnly
     60     FALSE,                     // WritingCache
     61     0x200,                     // BlockSize
     62     0,                         // IoAlign
     63     0,                         // LastBlock
     64     0,                         // LowestAlignedLba
     65     1                          // LogicalBlocksPerPhysicalBlock
     66   },
     67   {                            // DiskInfo
     68     EFI_DISK_INFO_IDE_INTERFACE_GUID,
     69     AtaDiskInfoInquiry,
     70     AtaDiskInfoIdentify,
     71     AtaDiskInfoSenseData,
     72     AtaDiskInfoWhichIde
     73   },
     74   NULL,                        // DevicePath
     75   {
     76     AtaStorageSecurityReceiveData,
     77     AtaStorageSecuritySendData
     78   },
     79   NULL,                        // AtaBusDriverData
     80   0,                           // Port
     81   0,                           // PortMultiplierPort
     82   { 0, },                      // Packet
     83   {{ 0}, },                    // Acb
     84   NULL,                        // Asb
     85   FALSE,                       // UdmaValid
     86   FALSE,                       // Lba48Bit
     87   NULL,                        // IdentifyData
     88   NULL,                        // ControllerNameTable
     89   {L'\0', },                   // ModelName
     90   {NULL, NULL},                // AtaTaskList
     91   {NULL, NULL},                // AtaSubTaskList
     92   FALSE                        // Abort
     93 };
     94 
     95 /**
     96   Allocates an aligned buffer for ATA device.
     97 
     98   This function allocates an aligned buffer for the ATA device to perform
     99   ATA pass through operations. The alignment requirement is from ATA pass
    100   through interface.
    101 
    102   @param  AtaDevice         The ATA child device involved for the operation.
    103   @param  BufferSize        The request buffer size.
    104 
    105   @return A pointer to the aligned buffer or NULL if the allocation fails.
    106 
    107 **/
    108 VOID *
    109 AllocateAlignedBuffer (
    110   IN ATA_DEVICE               *AtaDevice,
    111   IN UINTN                    BufferSize
    112   )
    113 {
    114   return AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize), AtaDevice->AtaBusDriverData->AtaPassThru->Mode->IoAlign);
    115 }
    116 
    117 /**
    118   Frees an aligned buffer for ATA device.
    119 
    120   This function frees an aligned buffer for the ATA device to perform
    121   ATA pass through operations.
    122 
    123   @param  Buffer            The aligned buffer to be freed.
    124   @param  BufferSize        The request buffer size.
    125 
    126 **/
    127 VOID
    128 FreeAlignedBuffer (
    129   IN VOID                     *Buffer,
    130   IN UINTN                    BufferSize
    131   )
    132 {
    133   if (Buffer != NULL) {
    134     FreeAlignedPages (Buffer, EFI_SIZE_TO_PAGES (BufferSize));
    135   }
    136 }
    137 
    138 
    139 /**
    140   Release all the resources allocated for the ATA device.
    141 
    142   This function releases all the resources allocated for the ATA device.
    143 
    144   @param  AtaDevice         The ATA child device involved for the operation.
    145 
    146 **/
    147 VOID
    148 ReleaseAtaResources (
    149   IN ATA_DEVICE  *AtaDevice
    150   )
    151 {
    152   ATA_BUS_ASYN_SUB_TASK *SubTask;
    153   ATA_BUS_ASYN_TASK     *AtaTask;
    154   LIST_ENTRY            *Entry;
    155   LIST_ENTRY            *DelEntry;
    156   EFI_TPL               OldTpl;
    157 
    158   FreeUnicodeStringTable (AtaDevice->ControllerNameTable);
    159   FreeAlignedBuffer (AtaDevice->Asb, sizeof (EFI_ATA_STATUS_BLOCK));
    160   FreeAlignedBuffer (AtaDevice->IdentifyData, sizeof (ATA_IDENTIFY_DATA));
    161   if (AtaDevice->DevicePath != NULL) {
    162     FreePool (AtaDevice->DevicePath);
    163   }
    164   OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
    165   if (!IsListEmpty (&AtaDevice->AtaSubTaskList)) {
    166     //
    167     // Free the Subtask list.
    168     //
    169     for(Entry = AtaDevice->AtaSubTaskList.ForwardLink;
    170         Entry != (&AtaDevice->AtaSubTaskList);
    171        ) {
    172       DelEntry = Entry;
    173       Entry    = Entry->ForwardLink;
    174       SubTask  = ATA_ASYN_SUB_TASK_FROM_ENTRY (DelEntry);
    175 
    176       RemoveEntryList (DelEntry);
    177       FreeAtaSubTask (SubTask);
    178     }
    179   }
    180   if (!IsListEmpty (&AtaDevice->AtaTaskList)) {
    181     //
    182     // Free the Subtask list.
    183     //
    184     for(Entry = AtaDevice->AtaTaskList.ForwardLink;
    185         Entry != (&AtaDevice->AtaTaskList);
    186        ) {
    187       DelEntry = Entry;
    188       Entry    = Entry->ForwardLink;
    189       AtaTask  = ATA_ASYN_TASK_FROM_ENTRY (DelEntry);
    190 
    191       RemoveEntryList (DelEntry);
    192       FreePool (AtaTask);
    193     }
    194   }
    195   gBS->RestoreTPL (OldTpl);
    196   FreePool (AtaDevice);
    197 }
    198 
    199 
    200 /**
    201   Registers an ATA device.
    202 
    203   This function allocates an ATA device structure for the ATA device specified by
    204   Port and PortMultiplierPort if the ATA device is identified as a valid one.
    205   Then it will create child handle and install Block IO and Disk Info protocol on
    206   it.
    207 
    208   @param  AtaBusDriverData      The parent ATA bus driver data structure.
    209   @param  Port                  The port number of the ATA device.
    210   @param  PortMultiplierPort    The port multiplier port number of the ATA device.
    211 
    212   @retval EFI_SUCCESS           The ATA device is successfully registered.
    213   @retval EFI_OUT_OF_RESOURCES  There is not enough memory to allocate the ATA device
    214                                 and related data structures.
    215   @return Others                Some error occurs when registering the ATA device.
    216 **/
    217 EFI_STATUS
    218 RegisterAtaDevice (
    219   IN OUT ATA_BUS_DRIVER_DATA        *AtaBusDriverData,
    220   IN     UINT16                     Port,
    221   IN     UINT16                     PortMultiplierPort
    222   )
    223 {
    224   EFI_STATUS                        Status;
    225   ATA_DEVICE                        *AtaDevice;
    226   EFI_ATA_PASS_THRU_PROTOCOL        *AtaPassThru;
    227   EFI_DEVICE_PATH_PROTOCOL          *NewDevicePathNode;
    228   EFI_DEVICE_PATH_PROTOCOL          *DevicePath;
    229   EFI_DEVICE_PATH_PROTOCOL          *RemainingDevicePath;
    230   EFI_HANDLE                        DeviceHandle;
    231 
    232   AtaDevice         = NULL;
    233   NewDevicePathNode = NULL;
    234   DevicePath        = NULL;
    235   RemainingDevicePath = NULL;
    236 
    237   //
    238   // Build device path
    239   //
    240   AtaPassThru = AtaBusDriverData->AtaPassThru;
    241   Status = AtaPassThru->BuildDevicePath (AtaPassThru, Port, PortMultiplierPort, &NewDevicePathNode);
    242   if (EFI_ERROR (Status)) {
    243     goto Done;
    244   }
    245 
    246   DevicePath = AppendDevicePathNode (AtaBusDriverData->ParentDevicePath, NewDevicePathNode);
    247   if (DevicePath == NULL) {
    248     Status = EFI_OUT_OF_RESOURCES;
    249     goto Done;
    250   }
    251 
    252   DeviceHandle = NULL;
    253   RemainingDevicePath = DevicePath;
    254   Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &DeviceHandle);
    255   if (!EFI_ERROR (Status) && (DeviceHandle != NULL) && IsDevicePathEnd(RemainingDevicePath)) {
    256     Status = EFI_ALREADY_STARTED;
    257     FreePool (DevicePath);
    258     goto Done;
    259   }
    260 
    261   //
    262   // Allocate ATA device from the template.
    263   //
    264   AtaDevice = AllocateCopyPool (sizeof (ATA_DEVICE), &gAtaDeviceTemplate);
    265   if (AtaDevice == NULL) {
    266     Status = EFI_OUT_OF_RESOURCES;
    267     goto Done;
    268   }
    269 
    270   //
    271   // Initializes ATA device structures and allocates the required buffer.
    272   //
    273   AtaDevice->BlockIo.Media      = &AtaDevice->BlockMedia;
    274   AtaDevice->BlockIo2.Media     = &AtaDevice->BlockMedia;
    275   AtaDevice->AtaBusDriverData   = AtaBusDriverData;
    276   AtaDevice->DevicePath         = DevicePath;
    277   AtaDevice->Port               = Port;
    278   AtaDevice->PortMultiplierPort = PortMultiplierPort;
    279   AtaDevice->Asb = AllocateAlignedBuffer (AtaDevice, sizeof (EFI_ATA_STATUS_BLOCK));
    280   if (AtaDevice->Asb == NULL) {
    281     Status = EFI_OUT_OF_RESOURCES;
    282     goto Done;
    283   }
    284   AtaDevice->IdentifyData = AllocateAlignedBuffer (AtaDevice, sizeof (ATA_IDENTIFY_DATA));
    285   if (AtaDevice->IdentifyData == NULL) {
    286     Status = EFI_OUT_OF_RESOURCES;
    287     goto Done;
    288   }
    289 
    290   //
    291   // Initial Ata Task List
    292   //
    293   InitializeListHead (&AtaDevice->AtaTaskList);
    294   InitializeListHead (&AtaDevice->AtaSubTaskList);
    295 
    296   //
    297   // Report Status Code to indicate the ATA device will be enabled
    298   //
    299   REPORT_STATUS_CODE_WITH_DEVICE_PATH (
    300     EFI_PROGRESS_CODE,
    301     (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_PC_ENABLE),
    302     AtaBusDriverData->ParentDevicePath
    303     );
    304 
    305   //
    306   // Try to identify the ATA device via the ATA pass through command.
    307   //
    308   Status = DiscoverAtaDevice (AtaDevice);
    309   if (EFI_ERROR (Status)) {
    310     goto Done;
    311   }
    312 
    313   //
    314   // Build controller name for Component Name (2) protocol.
    315   //
    316   Status = AddUnicodeString2 (
    317              "eng",
    318              gAtaBusComponentName.SupportedLanguages,
    319              &AtaDevice->ControllerNameTable,
    320              AtaDevice->ModelName,
    321              TRUE
    322              );
    323   if (EFI_ERROR (Status)) {
    324     goto Done;
    325   }
    326 
    327   Status = AddUnicodeString2 (
    328              "en",
    329              gAtaBusComponentName2.SupportedLanguages,
    330              &AtaDevice->ControllerNameTable,
    331              AtaDevice->ModelName,
    332              FALSE
    333              );
    334   if (EFI_ERROR (Status)) {
    335     goto Done;
    336   }
    337 
    338   //
    339   // Update to AHCI interface GUID based on device path node. The default one
    340   // is IDE interface GUID copied from template.
    341   //
    342   if (NewDevicePathNode->SubType == MSG_SATA_DP) {
    343     CopyGuid (&AtaDevice->DiskInfo.Interface, &gEfiDiskInfoAhciInterfaceGuid);
    344   }
    345 
    346   Status = gBS->InstallMultipleProtocolInterfaces (
    347                   &AtaDevice->Handle,
    348                   &gEfiDevicePathProtocolGuid,
    349                   AtaDevice->DevicePath,
    350                   &gEfiBlockIoProtocolGuid,
    351                   &AtaDevice->BlockIo,
    352                   &gEfiBlockIo2ProtocolGuid,
    353                   &AtaDevice->BlockIo2,
    354                   &gEfiDiskInfoProtocolGuid,
    355                   &AtaDevice->DiskInfo,
    356                   NULL
    357                   );
    358   if (EFI_ERROR (Status)) {
    359     goto Done;
    360   }
    361 
    362   //
    363   // See if the ata device support trust computing feature or not.
    364   // If yes, then install Storage Security Protocol at the ata device handle.
    365   //
    366   if ((AtaDevice->IdentifyData->trusted_computing_support & BIT0) != 0) {
    367     DEBUG ((EFI_D_INFO, "Found TCG support in Port %x PortMultiplierPort %x\n", Port, PortMultiplierPort));
    368     Status = gBS->InstallProtocolInterface (
    369                     &AtaDevice->Handle,
    370                     &gEfiStorageSecurityCommandProtocolGuid,
    371                     EFI_NATIVE_INTERFACE,
    372                     &AtaDevice->StorageSecurity
    373                     );
    374     if (EFI_ERROR (Status)) {
    375       goto Done;
    376     }
    377     DEBUG ((EFI_D_INFO, "Successfully Install Storage Security Protocol on the ATA device\n"));
    378   }
    379 
    380   gBS->OpenProtocol (
    381          AtaBusDriverData->Controller,
    382          &gEfiAtaPassThruProtocolGuid,
    383          (VOID **) &AtaPassThru,
    384          AtaBusDriverData->DriverBindingHandle,
    385          AtaDevice->Handle,
    386          EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
    387          );
    388 
    389 Done:
    390   if (NewDevicePathNode != NULL) {
    391     FreePool (NewDevicePathNode);
    392   }
    393 
    394   if (EFI_ERROR (Status) && (AtaDevice != NULL)) {
    395     ReleaseAtaResources (AtaDevice);
    396     DEBUG ((EFI_D_ERROR | EFI_D_INIT, "Failed to initialize Port %x PortMultiplierPort %x, status = %r\n", Port, PortMultiplierPort, Status));
    397   }
    398   return Status;
    399 }
    400 
    401 
    402 /**
    403   Unregisters an ATA device.
    404 
    405   This function removes the protocols installed on the controller handle and
    406   frees the resources allocated for the ATA device.
    407 
    408   @param  This                  The pointer to EFI_DRIVER_BINDING_PROTOCOL instance.
    409   @param  Controller            The controller handle of the ATA device.
    410   @param  Handle                The child handle.
    411 
    412   @retval EFI_SUCCESS           The ATA device is successfully unregistered.
    413   @return Others                Some error occurs when unregistering the ATA device.
    414 
    415 **/
    416 EFI_STATUS
    417 UnregisterAtaDevice (
    418   IN  EFI_DRIVER_BINDING_PROTOCOL    *This,
    419   IN  EFI_HANDLE                     Controller,
    420   IN  EFI_HANDLE                     Handle
    421   )
    422 {
    423   EFI_STATUS                               Status;
    424   EFI_BLOCK_IO_PROTOCOL                    *BlockIo;
    425   EFI_BLOCK_IO2_PROTOCOL                   *BlockIo2;
    426   ATA_DEVICE                               *AtaDevice;
    427   EFI_ATA_PASS_THRU_PROTOCOL               *AtaPassThru;
    428   EFI_STORAGE_SECURITY_COMMAND_PROTOCOL    *StorageSecurity;
    429 
    430   BlockIo2             =     NULL;
    431   BlockIo              =     NULL;
    432 
    433   Status = gBS->OpenProtocol (
    434                   Handle,
    435                   &gEfiBlockIoProtocolGuid,
    436                   (VOID **) &BlockIo,
    437                   This->DriverBindingHandle,
    438                   Controller,
    439                   EFI_OPEN_PROTOCOL_GET_PROTOCOL
    440                   );
    441   if (EFI_ERROR (Status)) {
    442     //
    443     // Locate BlockIo2 protocol
    444     //
    445     Status = gBS->OpenProtocol (
    446                     Handle,
    447                     &gEfiBlockIo2ProtocolGuid,
    448                     (VOID **) &BlockIo2,
    449                     This->DriverBindingHandle,
    450                     Controller,
    451                     EFI_OPEN_PROTOCOL_GET_PROTOCOL
    452                     );
    453     if (EFI_ERROR (Status)) {
    454       return Status;
    455     }
    456   }
    457 
    458   //
    459   // Get AtaDevice data.
    460   //
    461   if (BlockIo != NULL) {
    462     AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (BlockIo);
    463   } else {
    464     ASSERT (BlockIo2 != NULL);
    465     AtaDevice = ATA_DEVICE_FROM_BLOCK_IO2 (BlockIo2);
    466   }
    467 
    468   //
    469   // Close the child handle
    470   //
    471   gBS->CloseProtocol (
    472          Controller,
    473          &gEfiAtaPassThruProtocolGuid,
    474          This->DriverBindingHandle,
    475          Handle
    476          );
    477 
    478   //
    479   // The Ata Bus driver installs the BlockIo and BlockIo2 in the DriverBindingStart().
    480   // Here should uninstall both of them.
    481   //
    482   Status = gBS->UninstallMultipleProtocolInterfaces (
    483                   Handle,
    484                   &gEfiDevicePathProtocolGuid,
    485                   AtaDevice->DevicePath,
    486                   &gEfiBlockIoProtocolGuid,
    487                   &AtaDevice->BlockIo,
    488                   &gEfiBlockIo2ProtocolGuid,
    489                   &AtaDevice->BlockIo2,
    490                   &gEfiDiskInfoProtocolGuid,
    491                   &AtaDevice->DiskInfo,
    492                   NULL
    493                   );
    494 
    495   if (EFI_ERROR (Status)) {
    496     gBS->OpenProtocol (
    497           Controller,
    498           &gEfiAtaPassThruProtocolGuid,
    499           (VOID **) &AtaPassThru,
    500           This->DriverBindingHandle,
    501           Handle,
    502           EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
    503           );
    504     return Status;
    505   }
    506 
    507   //
    508   // If Storage Security Command Protocol is installed, then uninstall this protocol.
    509   //
    510   Status = gBS->OpenProtocol (
    511                   Handle,
    512                   &gEfiStorageSecurityCommandProtocolGuid,
    513                   (VOID **) &StorageSecurity,
    514                   This->DriverBindingHandle,
    515                   Controller,
    516                   EFI_OPEN_PROTOCOL_GET_PROTOCOL
    517                   );
    518 
    519   if (!EFI_ERROR (Status)) {
    520     Status = gBS->UninstallProtocolInterface (
    521                     Handle,
    522                     &gEfiStorageSecurityCommandProtocolGuid,
    523                     &AtaDevice->StorageSecurity
    524                     );
    525     if (EFI_ERROR (Status)) {
    526       gBS->OpenProtocol (
    527         Controller,
    528         &gEfiAtaPassThruProtocolGuid,
    529         (VOID **) &AtaPassThru,
    530         This->DriverBindingHandle,
    531         Handle,
    532         EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
    533         );
    534       return Status;
    535     }
    536   }
    537 
    538   ReleaseAtaResources (AtaDevice);
    539   return EFI_SUCCESS;
    540 }
    541 
    542 
    543 
    544 /**
    545   Tests to see if this driver supports a given controller. If a child device is provided,
    546   it further tests to see if this driver supports creating a handle for the specified child device.
    547 
    548   This function checks to see if the driver specified by This supports the device specified by
    549   ControllerHandle. Drivers will typically use the device path attached to
    550   ControllerHandle and/or the services from the bus I/O abstraction attached to
    551   ControllerHandle to determine if the driver supports ControllerHandle. This function
    552   may be called many times during platform initialization. In order to reduce boot times, the tests
    553   performed by this function must be very small, and take as little time as possible to execute. This
    554   function must not change the state of any hardware devices, and this function must be aware that the
    555   device specified by ControllerHandle may already be managed by the same driver or a
    556   different driver. This function must match its calls to AllocatePages() with FreePages(),
    557   AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
    558   Since ControllerHandle may have been previously started by the same driver, if a protocol is
    559   already in the opened state, then it must not be closed with CloseProtocol(). This is required
    560   to guarantee the state of ControllerHandle is not modified by this function.
    561 
    562   @param[in]  This                 A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
    563   @param[in]  ControllerHandle     The handle of the controller to test. This handle
    564                                    must support a protocol interface that supplies
    565                                    an I/O abstraction to the driver.
    566   @param[in]  RemainingDevicePath  A pointer to the remaining portion of a device path.  This
    567                                    parameter is ignored by device drivers, and is optional for bus
    568                                    drivers. For bus drivers, if this parameter is not NULL, then
    569                                    the bus driver must determine if the bus controller specified
    570                                    by ControllerHandle and the child controller specified
    571                                    by RemainingDevicePath are both supported by this
    572                                    bus driver.
    573 
    574   @retval EFI_SUCCESS              The device specified by ControllerHandle and
    575                                    RemainingDevicePath is supported by the driver specified by This.
    576   @retval EFI_ALREADY_STARTED      The device specified by ControllerHandle and
    577                                    RemainingDevicePath is already being managed by the driver
    578                                    specified by This.
    579   @retval EFI_ACCESS_DENIED        The device specified by ControllerHandle and
    580                                    RemainingDevicePath is already being managed by a different
    581                                    driver or an application that requires exclusive access.
    582                                    Currently not implemented.
    583   @retval EFI_UNSUPPORTED          The device specified by ControllerHandle and
    584                                    RemainingDevicePath is not supported by the driver specified by This.
    585 **/
    586 EFI_STATUS
    587 EFIAPI
    588 AtaBusDriverBindingSupported (
    589   IN EFI_DRIVER_BINDING_PROTOCOL  *This,
    590   IN EFI_HANDLE                   Controller,
    591   IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath
    592   )
    593 {
    594   EFI_STATUS                        Status;
    595   EFI_DEVICE_PATH_PROTOCOL          *ParentDevicePath;
    596   EFI_ATA_PASS_THRU_PROTOCOL        *AtaPassThru;
    597   UINT16                            Port;
    598   UINT16                            PortMultiplierPort;
    599 
    600   //
    601   // Test EFI_ATA_PASS_THRU_PROTOCOL on controller handle.
    602   //
    603   Status = gBS->OpenProtocol (
    604                   Controller,
    605                   &gEfiAtaPassThruProtocolGuid,
    606                   (VOID **) &AtaPassThru,
    607                   This->DriverBindingHandle,
    608                   Controller,
    609                   EFI_OPEN_PROTOCOL_BY_DRIVER
    610                   );
    611 
    612   if (Status == EFI_ALREADY_STARTED) {
    613     return EFI_SUCCESS;
    614   }
    615 
    616   if (EFI_ERROR (Status)) {
    617     return Status;
    618   }
    619 
    620   //
    621   // Test to see if this ATA Pass Thru Protocol is for a LOGICAL channel
    622   //
    623   if ((AtaPassThru->Mode->Attributes & EFI_ATA_PASS_THRU_ATTRIBUTES_LOGICAL) == 0) {
    624     //
    625     // Close the I/O Abstraction(s) used to perform the supported test
    626     //
    627     gBS->CloseProtocol (
    628           Controller,
    629           &gEfiAtaPassThruProtocolGuid,
    630           This->DriverBindingHandle,
    631           Controller
    632           );
    633     return EFI_UNSUPPORTED;
    634   }
    635 
    636   //
    637   // Test RemainingDevicePath is valid or not.
    638   //
    639   if ((RemainingDevicePath != NULL) && !IsDevicePathEnd (RemainingDevicePath)) {
    640     Status = AtaPassThru->GetDevice (AtaPassThru, RemainingDevicePath, &Port, &PortMultiplierPort);
    641     if (EFI_ERROR (Status)) {
    642       //
    643       // Close the I/O Abstraction(s) used to perform the supported test
    644       //
    645       gBS->CloseProtocol (
    646             Controller,
    647             &gEfiAtaPassThruProtocolGuid,
    648             This->DriverBindingHandle,
    649             Controller
    650             );
    651       return Status;
    652     }
    653   }
    654 
    655   //
    656   // Close the I/O Abstraction(s) used to perform the supported test
    657   //
    658   gBS->CloseProtocol (
    659         Controller,
    660         &gEfiAtaPassThruProtocolGuid,
    661         This->DriverBindingHandle,
    662         Controller
    663         );
    664 
    665   //
    666   // Open the EFI Device Path protocol needed to perform the supported test
    667   //
    668   Status = gBS->OpenProtocol (
    669                   Controller,
    670                   &gEfiDevicePathProtocolGuid,
    671                   (VOID **) &ParentDevicePath,
    672                   This->DriverBindingHandle,
    673                   Controller,
    674                   EFI_OPEN_PROTOCOL_GET_PROTOCOL
    675                   );
    676   return Status;
    677 }
    678 
    679 
    680 /**
    681   Starts a device controller or a bus controller.
    682 
    683   The Start() function is designed to be invoked from the EFI boot service ConnectController().
    684   As a result, much of the error checking on the parameters to Start() has been moved into this
    685   common boot service. It is legal to call Start() from other locations,
    686   but the following calling restrictions must be followed or the system behavior will not be deterministic.
    687   1. ControllerHandle must be a valid EFI_HANDLE.
    688   2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
    689      EFI_DEVICE_PATH_PROTOCOL.
    690   3. Prior to calling Start(), the Supported() function for the driver specified by This must
    691      have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
    692 
    693   @param[in]  This                 A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
    694   @param[in]  ControllerHandle     The handle of the controller to start. This handle
    695                                    must support a protocol interface that supplies
    696                                    an I/O abstraction to the driver.
    697   @param[in]  RemainingDevicePath  A pointer to the remaining portion of a device path.  This
    698                                    parameter is ignored by device drivers, and is optional for bus
    699                                    drivers. For a bus driver, if this parameter is NULL, then handles
    700                                    for all the children of Controller are created by this driver.
    701                                    If this parameter is not NULL and the first Device Path Node is
    702                                    not the End of Device Path Node, then only the handle for the
    703                                    child device specified by the first Device Path Node of
    704                                    RemainingDevicePath is created by this driver.
    705                                    If the first Device Path Node of RemainingDevicePath is
    706                                    the End of Device Path Node, no child handle is created by this
    707                                    driver.
    708 
    709   @retval EFI_SUCCESS              The device was started.
    710   @retval EFI_DEVICE_ERROR         The device could not be started due to a device error.Currently not implemented.
    711   @retval EFI_OUT_OF_RESOURCES     The request could not be completed due to a lack of resources.
    712   @retval Others                   The driver failded to start the device.
    713 
    714 **/
    715 EFI_STATUS
    716 EFIAPI
    717 AtaBusDriverBindingStart (
    718   IN EFI_DRIVER_BINDING_PROTOCOL  *This,
    719   IN EFI_HANDLE                   Controller,
    720   IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath
    721   )
    722 {
    723   EFI_STATUS                        Status;
    724   EFI_ATA_PASS_THRU_PROTOCOL        *AtaPassThru;
    725   EFI_DEVICE_PATH_PROTOCOL          *ParentDevicePath;
    726   ATA_BUS_DRIVER_DATA               *AtaBusDriverData;
    727   UINT16                            Port;
    728   UINT16                            PortMultiplierPort;
    729 
    730   AtaBusDriverData = NULL;
    731 
    732   Status = gBS->OpenProtocol (
    733                   Controller,
    734                   &gEfiDevicePathProtocolGuid,
    735                   (VOID **) &ParentDevicePath,
    736                   This->DriverBindingHandle,
    737                   Controller,
    738                   EFI_OPEN_PROTOCOL_GET_PROTOCOL
    739                   );
    740   if (EFI_ERROR (Status)) {
    741     return Status;
    742   }
    743 
    744   //
    745   // Report Status Code to indicate ATA bus starts
    746   //
    747   REPORT_STATUS_CODE_WITH_DEVICE_PATH (
    748     EFI_PROGRESS_CODE,
    749     (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_PC_INIT),
    750     ParentDevicePath
    751     );
    752 
    753   Status = gBS->OpenProtocol (
    754                   Controller,
    755                   &gEfiAtaPassThruProtocolGuid,
    756                   (VOID **) &AtaPassThru,
    757                   This->DriverBindingHandle,
    758                   Controller,
    759                   EFI_OPEN_PROTOCOL_BY_DRIVER
    760                   );
    761   if ((EFI_ERROR (Status)) && (Status != EFI_ALREADY_STARTED)) {
    762     goto ErrorExit;
    763   }
    764 
    765   //
    766   // Check EFI_ALREADY_STARTED to reuse the original ATA_BUS_DRIVER_DATA.
    767   //
    768   if (Status != EFI_ALREADY_STARTED) {
    769     AtaBusDriverData = AllocateZeroPool (sizeof (ATA_BUS_DRIVER_DATA));
    770     if (AtaBusDriverData == NULL) {
    771       Status = EFI_OUT_OF_RESOURCES;
    772       goto ErrorExit;
    773     }
    774 
    775     AtaBusDriverData->AtaPassThru = AtaPassThru;
    776     AtaBusDriverData->Controller  = Controller;
    777     AtaBusDriverData->ParentDevicePath = ParentDevicePath;
    778     AtaBusDriverData->DriverBindingHandle = This->DriverBindingHandle;
    779 
    780     Status = gBS->InstallMultipleProtocolInterfaces (
    781                     &Controller,
    782                     &gEfiCallerIdGuid,
    783                     AtaBusDriverData,
    784                     NULL
    785                     );
    786     if (EFI_ERROR (Status)) {
    787       goto ErrorExit;
    788     }
    789 
    790   } else {
    791     Status = gBS->OpenProtocol (
    792                     Controller,
    793                     &gEfiCallerIdGuid,
    794                     (VOID **) &AtaBusDriverData,
    795                     This->DriverBindingHandle,
    796                     Controller,
    797                     EFI_OPEN_PROTOCOL_GET_PROTOCOL
    798                     );
    799     if (EFI_ERROR (Status)) {
    800       AtaBusDriverData = NULL;
    801       goto ErrorExit;
    802     }
    803   }
    804 
    805   //
    806   // Report Status Code to indicate detecting devices on bus
    807   //
    808   REPORT_STATUS_CODE_WITH_DEVICE_PATH (
    809     EFI_PROGRESS_CODE,
    810     (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_PC_DETECT),
    811     ParentDevicePath
    812     );
    813 
    814   if (RemainingDevicePath == NULL) {
    815     Port = 0xFFFF;
    816     while (TRUE) {
    817       Status = AtaPassThru->GetNextPort (AtaPassThru, &Port);
    818       if (EFI_ERROR (Status)) {
    819         //
    820         // We cannot find more legal port then we are done.
    821         //
    822         break;
    823       }
    824 
    825       PortMultiplierPort = 0xFFFF;
    826       while (TRUE) {
    827         Status = AtaPassThru->GetNextDevice (AtaPassThru, Port, &PortMultiplierPort);
    828         if (EFI_ERROR (Status)) {
    829           //
    830           // We cannot find more legal port multiplier port number for ATA device
    831           // on the port, then we are done.
    832           //
    833           break;
    834         }
    835         RegisterAtaDevice (AtaBusDriverData, Port, PortMultiplierPort);
    836       }
    837     }
    838     Status = EFI_SUCCESS;
    839   } else if (!IsDevicePathEnd (RemainingDevicePath)) {
    840     Status = AtaPassThru->GetDevice (AtaPassThru, RemainingDevicePath, &Port, &PortMultiplierPort);
    841     if (!EFI_ERROR (Status)) {
    842       Status = RegisterAtaDevice (AtaBusDriverData,Port, PortMultiplierPort);
    843     }
    844   }
    845 
    846   return Status;
    847 
    848 ErrorExit:
    849 
    850   if (AtaBusDriverData != NULL) {
    851     gBS->UninstallMultipleProtocolInterfaces (
    852            Controller,
    853            &gEfiCallerIdGuid,
    854            AtaBusDriverData,
    855            NULL
    856            );
    857     FreePool (AtaBusDriverData);
    858   }
    859 
    860   gBS->CloseProtocol (
    861         Controller,
    862         &gEfiAtaPassThruProtocolGuid,
    863         This->DriverBindingHandle,
    864         Controller
    865         );
    866 
    867   return Status;
    868 
    869 }
    870 
    871 
    872 /**
    873   Stops a device controller or a bus controller.
    874 
    875   The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
    876   As a result, much of the error checking on the parameters to Stop() has been moved
    877   into this common boot service. It is legal to call Stop() from other locations,
    878   but the following calling restrictions must be followed or the system behavior will not be deterministic.
    879   1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
    880      same driver's Start() function.
    881   2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
    882      EFI_HANDLE. In addition, all of these handles must have been created in this driver's
    883      Start() function, and the Start() function must have called OpenProtocol() on
    884      ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
    885 
    886   @param[in]  This              A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
    887   @param[in]  ControllerHandle  A handle to the device being stopped. The handle must
    888                                 support a bus specific I/O protocol for the driver
    889                                 to use to stop the device.
    890   @param[in]  NumberOfChildren  The number of child device handles in ChildHandleBuffer.
    891   @param[in]  ChildHandleBuffer An array of child handles to be freed. May be NULL
    892                                 if NumberOfChildren is 0.
    893 
    894   @retval EFI_SUCCESS           The device was stopped.
    895   @retval EFI_DEVICE_ERROR      The device could not be stopped due to a device error.
    896 
    897 **/
    898 EFI_STATUS
    899 EFIAPI
    900 AtaBusDriverBindingStop (
    901   IN  EFI_DRIVER_BINDING_PROTOCOL     *This,
    902   IN  EFI_HANDLE                      Controller,
    903   IN  UINTN                           NumberOfChildren,
    904   IN  EFI_HANDLE                      *ChildHandleBuffer
    905   )
    906 {
    907   EFI_STATUS                  Status;
    908   BOOLEAN                     AllChildrenStopped;
    909   UINTN                       Index;
    910   ATA_BUS_DRIVER_DATA         *AtaBusDriverData;
    911 
    912   if (NumberOfChildren == 0) {
    913     Status = gBS->OpenProtocol (
    914                     Controller,
    915                     &gEfiCallerIdGuid,
    916                     (VOID **) &AtaBusDriverData,
    917                     This->DriverBindingHandle,
    918                     Controller,
    919                     EFI_OPEN_PROTOCOL_GET_PROTOCOL
    920                     );
    921     if (!EFI_ERROR (Status)) {
    922       gBS->UninstallMultipleProtocolInterfaces (
    923             Controller,
    924             &gEfiCallerIdGuid,
    925             AtaBusDriverData,
    926             NULL
    927             );
    928       FreePool (AtaBusDriverData);
    929     }
    930 
    931     gBS->CloseProtocol (
    932           Controller,
    933           &gEfiAtaPassThruProtocolGuid,
    934           This->DriverBindingHandle,
    935           Controller
    936           );
    937 
    938     return EFI_SUCCESS;
    939   }
    940 
    941   AllChildrenStopped = TRUE;
    942 
    943   for (Index = 0; Index < NumberOfChildren; Index++) {
    944 
    945     Status = UnregisterAtaDevice (This, Controller, ChildHandleBuffer[Index]);
    946     if (EFI_ERROR (Status)) {
    947       AllChildrenStopped = FALSE;
    948     }
    949   }
    950 
    951   if (!AllChildrenStopped) {
    952     return EFI_DEVICE_ERROR;
    953   }
    954 
    955   return EFI_SUCCESS;
    956 }
    957 
    958 
    959 /**
    960   Reset the Block Device.
    961 
    962   @param  This                 Indicates a pointer to the calling context.
    963   @param  ExtendedVerification Driver may perform diagnostics on reset.
    964 
    965   @retval EFI_SUCCESS          The device was reset.
    966   @retval EFI_DEVICE_ERROR     The device is not functioning properly and could
    967                                not be reset.
    968 
    969 **/
    970 EFI_STATUS
    971 EFIAPI
    972 AtaBlockIoReset (
    973   IN  EFI_BLOCK_IO_PROTOCOL   *This,
    974   IN  BOOLEAN                 ExtendedVerification
    975   )
    976 {
    977   EFI_STATUS      Status;
    978   ATA_DEVICE      *AtaDevice;
    979   EFI_TPL         OldTpl;
    980 
    981   OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
    982 
    983   AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (This);
    984 
    985   Status = ResetAtaDevice (AtaDevice);
    986 
    987   if (EFI_ERROR (Status)) {
    988     Status = EFI_DEVICE_ERROR;
    989   }
    990 
    991   gBS->RestoreTPL (OldTpl);
    992   return Status;
    993 }
    994 
    995 
    996 /**
    997   Read/Write BufferSize bytes from Lba from/into Buffer.
    998 
    999   @param[in]       This       Indicates a pointer to the calling context. Either be
   1000                               block I/O or block I/O2.
   1001   @param[in]       MediaId    The media ID that the read/write request is for.
   1002   @param[in]       Lba        The starting logical block address to be read/written.
   1003                               The caller is responsible for reading/writing to only
   1004                               legitimate locations.
   1005   @param[in, out]  Token      A pointer to the token associated with the transaction.
   1006   @param[in]       BufferSize Size of Buffer, must be a multiple of device block size.
   1007   @param[out]      Buffer     A pointer to the destination/source buffer for the data.
   1008   @param[in]       IsBlockIo2 Indicate the calling is from BlockIO or BlockIO2. TRUE is
   1009                               from BlockIO2, FALSE is for BlockIO.
   1010   @param[in]       IsWrite    Indicates whether it is a write operation.
   1011 
   1012   @retval EFI_SUCCESS           The data was read/written correctly to the device.
   1013   @retval EFI_WRITE_PROTECTED   The device can not be read/written to.
   1014   @retval EFI_DEVICE_ERROR      The device reported an error while performing the read/write.
   1015   @retval EFI_NO_MEDIA          There is no media in the device.
   1016   @retval EFI_MEDIA_CHNAGED     The MediaId does not matched the current device.
   1017   @retval EFI_BAD_BUFFER_SIZE   The Buffer was not a multiple of the block size of the device.
   1018   @retval EFI_INVALID_PARAMETER The read/write request contains LBAs that are not valid,
   1019                                 or the buffer is not on proper alignment.
   1020 
   1021 **/
   1022 EFI_STATUS
   1023 BlockIoReadWrite (
   1024   IN     VOID                    *This,
   1025   IN     UINT32                  MediaId,
   1026   IN     EFI_LBA                 Lba,
   1027   IN OUT EFI_BLOCK_IO2_TOKEN     *Token,
   1028   IN     UINTN                   BufferSize,
   1029   OUT    VOID                    *Buffer,
   1030   IN     BOOLEAN                 IsBlockIo2,
   1031   IN     BOOLEAN                 IsWrite
   1032   )
   1033 {
   1034   ATA_DEVICE                        *AtaDevice;
   1035   EFI_STATUS                        Status;
   1036   EFI_TPL                           OldTpl;
   1037   EFI_BLOCK_IO_MEDIA                *Media;
   1038   UINTN                             BlockSize;
   1039   UINTN                             NumberOfBlocks;
   1040   UINTN                             IoAlign;
   1041 
   1042   if (IsBlockIo2) {
   1043    Media     = ((EFI_BLOCK_IO2_PROTOCOL *) This)->Media;
   1044    AtaDevice = ATA_DEVICE_FROM_BLOCK_IO2 (This);
   1045   } else {
   1046    Media     = ((EFI_BLOCK_IO_PROTOCOL *) This)->Media;
   1047    AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (This);
   1048   }
   1049 
   1050   if (MediaId != Media->MediaId) {
   1051     return EFI_MEDIA_CHANGED;
   1052   }
   1053 
   1054   //
   1055   // Check parameters.
   1056   //
   1057   if (Buffer == NULL) {
   1058     return EFI_INVALID_PARAMETER;
   1059   }
   1060 
   1061   if (BufferSize == 0) {
   1062     if ((Token != NULL) && (Token->Event != NULL)) {
   1063       Token->TransactionStatus = EFI_SUCCESS;
   1064       gBS->SignalEvent (Token->Event);
   1065     }
   1066     return EFI_SUCCESS;
   1067   }
   1068 
   1069   BlockSize = Media->BlockSize;
   1070   if ((BufferSize % BlockSize) != 0) {
   1071     return EFI_BAD_BUFFER_SIZE;
   1072   }
   1073 
   1074   NumberOfBlocks  = BufferSize / BlockSize;
   1075   if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {
   1076     return EFI_INVALID_PARAMETER;
   1077   }
   1078 
   1079   IoAlign = Media->IoAlign;
   1080   if (IoAlign > 0 && (((UINTN) Buffer & (IoAlign - 1)) != 0)) {
   1081     return EFI_INVALID_PARAMETER;
   1082   }
   1083 
   1084   OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
   1085 
   1086   //
   1087   // Invoke low level AtaDevice Access Routine.
   1088   //
   1089   Status = AccessAtaDevice (AtaDevice, Buffer, Lba, NumberOfBlocks, IsWrite, Token);
   1090 
   1091   gBS->RestoreTPL (OldTpl);
   1092 
   1093   return Status;
   1094 }
   1095 
   1096 
   1097 /**
   1098   Read BufferSize bytes from Lba into Buffer.
   1099 
   1100   @param  This       Indicates a pointer to the calling context.
   1101   @param  MediaId    Id of the media, changes every time the media is replaced.
   1102   @param  Lba        The starting Logical Block Address to read from
   1103   @param  BufferSize Size of Buffer, must be a multiple of device block size.
   1104   @param  Buffer     A pointer to the destination buffer for the data. The caller is
   1105                      responsible for either having implicit or explicit ownership of the buffer.
   1106 
   1107   @retval EFI_SUCCESS           The data was read correctly from the device.
   1108   @retval EFI_DEVICE_ERROR      The device reported an error while performing the read.
   1109   @retval EFI_NO_MEDIA          There is no media in the device.
   1110   @retval EFI_MEDIA_CHANGED     The MediaId does not matched the current device.
   1111   @retval EFI_BAD_BUFFER_SIZE   The Buffer was not a multiple of the block size of the device.
   1112   @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
   1113                                 or the buffer is not on proper alignment.
   1114 
   1115 **/
   1116 EFI_STATUS
   1117 EFIAPI
   1118 AtaBlockIoReadBlocks (
   1119   IN  EFI_BLOCK_IO_PROTOCOL   *This,
   1120   IN  UINT32                  MediaId,
   1121   IN  EFI_LBA                 Lba,
   1122   IN  UINTN                   BufferSize,
   1123   OUT VOID                    *Buffer
   1124   )
   1125 {
   1126   return BlockIoReadWrite ((VOID *) This, MediaId, Lba, NULL, BufferSize, Buffer, FALSE, FALSE);
   1127 }
   1128 
   1129 
   1130 /**
   1131   Write BufferSize bytes from Lba into Buffer.
   1132 
   1133   @param  This       Indicates a pointer to the calling context.
   1134   @param  MediaId    The media ID that the write request is for.
   1135   @param  Lba        The starting logical block address to be written. The caller is
   1136                      responsible for writing to only legitimate locations.
   1137   @param  BufferSize Size of Buffer, must be a multiple of device block size.
   1138   @param  Buffer     A pointer to the source buffer for the data.
   1139 
   1140   @retval EFI_SUCCESS           The data was written correctly to the device.
   1141   @retval EFI_WRITE_PROTECTED   The device can not be written to.
   1142   @retval EFI_DEVICE_ERROR      The device reported an error while performing the write.
   1143   @retval EFI_NO_MEDIA          There is no media in the device.
   1144   @retval EFI_MEDIA_CHNAGED     The MediaId does not matched the current device.
   1145   @retval EFI_BAD_BUFFER_SIZE   The Buffer was not a multiple of the block size of the device.
   1146   @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
   1147                                 or the buffer is not on proper alignment.
   1148 
   1149 **/
   1150 EFI_STATUS
   1151 EFIAPI
   1152 AtaBlockIoWriteBlocks (
   1153   IN  EFI_BLOCK_IO_PROTOCOL   *This,
   1154   IN  UINT32                  MediaId,
   1155   IN  EFI_LBA                 Lba,
   1156   IN  UINTN                   BufferSize,
   1157   IN  VOID                    *Buffer
   1158   )
   1159 {
   1160   return BlockIoReadWrite ((VOID *) This, MediaId, Lba, NULL, BufferSize, Buffer, FALSE, TRUE);
   1161 }
   1162 
   1163 
   1164 /**
   1165   Flush the Block Device.
   1166 
   1167   @param  This              Indicates a pointer to the calling context.
   1168 
   1169   @retval EFI_SUCCESS       All outstanding data was written to the device
   1170   @retval EFI_DEVICE_ERROR  The device reported an error while writing back the data
   1171   @retval EFI_NO_MEDIA      There is no media in the device.
   1172 
   1173 **/
   1174 EFI_STATUS
   1175 EFIAPI
   1176 AtaBlockIoFlushBlocks (
   1177   IN  EFI_BLOCK_IO_PROTOCOL   *This
   1178   )
   1179 {
   1180   //
   1181   // return directly
   1182   //
   1183   return EFI_SUCCESS;
   1184 }
   1185 
   1186 /**
   1187   Reset the Block Device.
   1188 
   1189   @param[in]  This                 Indicates a pointer to the calling context.
   1190   @param[in]  ExtendedVerification Driver may perform diagnostics on reset.
   1191 
   1192   @retval EFI_SUCCESS          The device was reset.
   1193   @retval EFI_DEVICE_ERROR     The device is not functioning properly and could
   1194                                not be reset.
   1195 
   1196 **/
   1197 EFI_STATUS
   1198 EFIAPI
   1199 AtaBlockIoResetEx (
   1200   IN  EFI_BLOCK_IO2_PROTOCOL  *This,
   1201   IN  BOOLEAN                 ExtendedVerification
   1202   )
   1203 {
   1204   EFI_STATUS      Status;
   1205   ATA_DEVICE      *AtaDevice;
   1206   EFI_TPL         OldTpl;
   1207 
   1208   OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
   1209 
   1210   AtaDevice = ATA_DEVICE_FROM_BLOCK_IO2 (This);
   1211 
   1212   AtaTerminateNonBlockingTask (AtaDevice);
   1213 
   1214   Status = ResetAtaDevice (AtaDevice);
   1215 
   1216   if (EFI_ERROR (Status)) {
   1217     Status = EFI_DEVICE_ERROR;
   1218   }
   1219 
   1220   gBS->RestoreTPL (OldTpl);
   1221   return Status;
   1222 }
   1223 
   1224 /**
   1225   Read BufferSize bytes from Lba into Buffer.
   1226 
   1227   @param[in]       This       Indicates a pointer to the calling context.
   1228   @param[in]       MediaId    Id of the media, changes every time the media is replaced.
   1229   @param[in]       Lba        The starting Logical Block Address to read from.
   1230   @param[in, out]  Token      A pointer to the token associated with the transaction.
   1231   @param[in]       BufferSize Size of Buffer, must be a multiple of device block size.
   1232   @param[out]      Buffer     A pointer to the destination buffer for the data. The caller is
   1233                               responsible for either having implicit or explicit ownership of the buffer.
   1234 
   1235   @retval EFI_SUCCESS           The read request was queued if Event is not NULL.
   1236                                 The data was read correctly from the device if
   1237                                 the Event is NULL.
   1238   @retval EFI_DEVICE_ERROR      The device reported an error while performing
   1239                                 the read.
   1240   @retval EFI_NO_MEDIA          There is no media in the device.
   1241   @retval EFI_MEDIA_CHANGED     The MediaId is not for the current media.
   1242   @retval EFI_BAD_BUFFER_SIZE   The BufferSize parameter is not a multiple of the
   1243                                 intrinsic block size of the device.
   1244   @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
   1245                                 or the buffer is not on proper alignment.
   1246   @retval EFI_OUT_OF_RESOURCES  The request could not be completed due to a lack
   1247                                 of resources.
   1248 
   1249 **/
   1250 EFI_STATUS
   1251 EFIAPI
   1252 AtaBlockIoReadBlocksEx (
   1253   IN  EFI_BLOCK_IO2_PROTOCOL  *This,
   1254   IN  UINT32                  MediaId,
   1255   IN  EFI_LBA                 Lba,
   1256   IN OUT EFI_BLOCK_IO2_TOKEN  *Token,
   1257   IN  UINTN                   BufferSize,
   1258   OUT VOID                    *Buffer
   1259   )
   1260 {
   1261   return BlockIoReadWrite ((VOID *) This, MediaId, Lba, Token, BufferSize, Buffer, TRUE, FALSE);
   1262 }
   1263 
   1264 
   1265 /**
   1266   Write BufferSize bytes from Lba into Buffer.
   1267 
   1268   @param[in]       This       Indicates a pointer to the calling context.
   1269   @param[in]       MediaId    The media ID that the write request is for.
   1270   @param[in]       Lba        The starting logical block address to be written. The
   1271                               caller is responsible for writing to only legitimate
   1272                               locations.
   1273   @param[in, out]  Token      A pointer to the token associated with the transaction.
   1274   @param[in]       BufferSize Size of Buffer, must be a multiple of device block size.
   1275   @param[in]       Buffer     A pointer to the source buffer for the data.
   1276 
   1277   @retval EFI_SUCCESS           The data was written correctly to the device.
   1278   @retval EFI_WRITE_PROTECTED   The device can not be written to.
   1279   @retval EFI_DEVICE_ERROR      The device reported an error while performing the write.
   1280   @retval EFI_NO_MEDIA          There is no media in the device.
   1281   @retval EFI_MEDIA_CHNAGED     The MediaId does not matched the current device.
   1282   @retval EFI_BAD_BUFFER_SIZE   The Buffer was not a multiple of the block size of the device.
   1283   @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
   1284                                 or the buffer is not on proper alignment.
   1285 
   1286 **/
   1287 EFI_STATUS
   1288 EFIAPI
   1289 AtaBlockIoWriteBlocksEx (
   1290   IN  EFI_BLOCK_IO2_PROTOCOL  *This,
   1291   IN  UINT32                  MediaId,
   1292   IN  EFI_LBA                 Lba,
   1293   IN OUT EFI_BLOCK_IO2_TOKEN  *Token,
   1294   IN  UINTN                   BufferSize,
   1295   IN  VOID                    *Buffer
   1296   )
   1297 {
   1298   return BlockIoReadWrite ((VOID *) This, MediaId, Lba, Token, BufferSize, Buffer, TRUE, TRUE);
   1299 }
   1300 
   1301 
   1302 /**
   1303   Flush the Block Device.
   1304 
   1305   @param[in]       This       Indicates a pointer to the calling context.
   1306   @param[in, out]  Token      A pointer to the token associated with the transaction.
   1307 
   1308   @retval EFI_SUCCESS       All outstanding data was written to the device
   1309   @retval EFI_DEVICE_ERROR  The device reported an error while writing back the data
   1310   @retval EFI_NO_MEDIA      There is no media in the device.
   1311 
   1312 **/
   1313 EFI_STATUS
   1314 EFIAPI
   1315 AtaBlockIoFlushBlocksEx (
   1316   IN  EFI_BLOCK_IO2_PROTOCOL  *This,
   1317   IN OUT EFI_BLOCK_IO2_TOKEN  *Token
   1318   )
   1319 {
   1320   //
   1321   // Signal event and return directly.
   1322   //
   1323   if (Token != NULL && Token->Event != NULL) {
   1324     Token->TransactionStatus = EFI_SUCCESS;
   1325     gBS->SignalEvent (Token->Event);
   1326   }
   1327   return EFI_SUCCESS;
   1328 }
   1329 /**
   1330   Provides inquiry information for the controller type.
   1331 
   1332   This function is used by the IDE bus driver to get inquiry data.  Data format
   1333   of Identify data is defined by the Interface GUID.
   1334 
   1335   @param[in]      This             Pointer to the EFI_DISK_INFO_PROTOCOL instance.
   1336   @param[in, out] InquiryData      Pointer to a buffer for the inquiry data.
   1337   @param[in, out] InquiryDataSize  Pointer to the value for the inquiry data size.
   1338 
   1339   @retval EFI_SUCCESS            The command was accepted without any errors.
   1340   @retval EFI_NOT_FOUND          Device does not support this data class
   1341   @retval EFI_DEVICE_ERROR       Error reading InquiryData from device
   1342   @retval EFI_BUFFER_TOO_SMALL   InquiryDataSize not big enough
   1343 
   1344 **/
   1345 EFI_STATUS
   1346 EFIAPI
   1347 AtaDiskInfoInquiry (
   1348   IN     EFI_DISK_INFO_PROTOCOL   *This,
   1349   IN OUT VOID                     *InquiryData,
   1350   IN OUT UINT32                   *InquiryDataSize
   1351   )
   1352 {
   1353   return EFI_NOT_FOUND;
   1354 }
   1355 
   1356 
   1357 /**
   1358   Provides identify information for the controller type.
   1359 
   1360   This function is used by the IDE bus driver to get identify data.  Data format
   1361   of Identify data is defined by the Interface GUID.
   1362 
   1363   @param[in]      This              Pointer to the EFI_DISK_INFO_PROTOCOL
   1364                                     instance.
   1365   @param[in, out] IdentifyData      Pointer to a buffer for the identify data.
   1366   @param[in, out] IdentifyDataSize  Pointer to the value for the identify data
   1367                                     size.
   1368 
   1369   @retval EFI_SUCCESS            The command was accepted without any errors.
   1370   @retval EFI_NOT_FOUND          Device does not support this data class
   1371   @retval EFI_DEVICE_ERROR       Error reading IdentifyData from device
   1372   @retval EFI_BUFFER_TOO_SMALL   IdentifyDataSize not big enough
   1373 
   1374 **/
   1375 EFI_STATUS
   1376 EFIAPI
   1377 AtaDiskInfoIdentify (
   1378   IN     EFI_DISK_INFO_PROTOCOL   *This,
   1379   IN OUT VOID                     *IdentifyData,
   1380   IN OUT UINT32                   *IdentifyDataSize
   1381   )
   1382 {
   1383   EFI_STATUS                      Status;
   1384   ATA_DEVICE                      *AtaDevice;
   1385 
   1386   AtaDevice = ATA_DEVICE_FROM_DISK_INFO (This);
   1387 
   1388   Status = EFI_BUFFER_TOO_SMALL;
   1389   if (*IdentifyDataSize >= sizeof (ATA_IDENTIFY_DATA)) {
   1390     Status = EFI_SUCCESS;
   1391     CopyMem (IdentifyData, AtaDevice->IdentifyData, sizeof (ATA_IDENTIFY_DATA));
   1392   }
   1393   *IdentifyDataSize = sizeof (ATA_IDENTIFY_DATA);
   1394 
   1395   return Status;
   1396 }
   1397 
   1398 
   1399 /**
   1400   Provides sense data information for the controller type.
   1401 
   1402   This function is used by the IDE bus driver to get sense data.
   1403   Data format of Sense data is defined by the Interface GUID.
   1404 
   1405   @param[in]      This             Pointer to the EFI_DISK_INFO_PROTOCOL instance.
   1406   @param[in, out] SenseData        Pointer to the SenseData.
   1407   @param[in, out] SenseDataSize    Size of SenseData in bytes.
   1408   @param[out]     SenseDataNumber  Pointer to the value for the sense data size.
   1409 
   1410   @retval EFI_SUCCESS            The command was accepted without any errors.
   1411   @retval EFI_NOT_FOUND          Device does not support this data class.
   1412   @retval EFI_DEVICE_ERROR       Error reading SenseData from device.
   1413   @retval EFI_BUFFER_TOO_SMALL   SenseDataSize not big enough.
   1414 
   1415 **/
   1416 EFI_STATUS
   1417 EFIAPI
   1418 AtaDiskInfoSenseData (
   1419   IN     EFI_DISK_INFO_PROTOCOL   *This,
   1420   IN OUT VOID                     *SenseData,
   1421   IN OUT UINT32                   *SenseDataSize,
   1422   OUT    UINT8                    *SenseDataNumber
   1423   )
   1424 {
   1425   return EFI_NOT_FOUND;
   1426 }
   1427 
   1428 
   1429 /**
   1430   This function is used by the IDE bus driver to get controller information.
   1431 
   1432   @param[in]  This         Pointer to the EFI_DISK_INFO_PROTOCOL instance.
   1433   @param[out] IdeChannel   Pointer to the Ide Channel number.  Primary or secondary.
   1434   @param[out] IdeDevice    Pointer to the Ide Device number.  Master or slave.
   1435 
   1436   @retval EFI_SUCCESS       IdeChannel and IdeDevice are valid.
   1437   @retval EFI_UNSUPPORTED   This is not an IDE device.
   1438 
   1439 **/
   1440 EFI_STATUS
   1441 EFIAPI
   1442 AtaDiskInfoWhichIde (
   1443   IN  EFI_DISK_INFO_PROTOCOL   *This,
   1444   OUT UINT32                   *IdeChannel,
   1445   OUT UINT32                   *IdeDevice
   1446   )
   1447 {
   1448   ATA_DEVICE                   *AtaDevice;
   1449 
   1450   AtaDevice       = ATA_DEVICE_FROM_DISK_INFO (This);
   1451   *IdeChannel     = AtaDevice->Port;
   1452   *IdeDevice      = AtaDevice->PortMultiplierPort;
   1453 
   1454   return EFI_SUCCESS;
   1455 }
   1456 
   1457 /**
   1458   Send a security protocol command to a device that receives data and/or the result
   1459   of one or more commands sent by SendData.
   1460 
   1461   The ReceiveData function sends a security protocol command to the given MediaId.
   1462   The security protocol command sent is defined by SecurityProtocolId and contains
   1463   the security protocol specific data SecurityProtocolSpecificData. The function
   1464   returns the data from the security protocol command in PayloadBuffer.
   1465 
   1466   For devices supporting the SCSI command set, the security protocol command is sent
   1467   using the SECURITY PROTOCOL IN command defined in SPC-4.
   1468 
   1469   For devices supporting the ATA command set, the security protocol command is sent
   1470   using one of the TRUSTED RECEIVE commands defined in ATA8-ACS if PayloadBufferSize
   1471   is non-zero.
   1472 
   1473   If the PayloadBufferSize is zero, the security protocol command is sent using the
   1474   Trusted Non-Data command defined in ATA8-ACS.
   1475 
   1476   If PayloadBufferSize is too small to store the available data from the security
   1477   protocol command, the function shall copy PayloadBufferSize bytes into the
   1478   PayloadBuffer and return EFI_WARN_BUFFER_TOO_SMALL.
   1479 
   1480   If PayloadBuffer or PayloadTransferSize is NULL and PayloadBufferSize is non-zero,
   1481   the function shall return EFI_INVALID_PARAMETER.
   1482 
   1483   If the given MediaId does not support security protocol commands, the function shall
   1484   return EFI_UNSUPPORTED. If there is no media in the device, the function returns
   1485   EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the device,
   1486   the function returns EFI_MEDIA_CHANGED.
   1487 
   1488   If the security protocol fails to complete within the Timeout period, the function
   1489   shall return EFI_TIMEOUT.
   1490 
   1491   If the security protocol command completes without an error, the function shall
   1492   return EFI_SUCCESS. If the security protocol command completes with an error, the
   1493   function shall return EFI_DEVICE_ERROR.
   1494 
   1495   @param  This                         Indicates a pointer to the calling context.
   1496   @param  MediaId                      ID of the medium to receive data from.
   1497   @param  Timeout                      The timeout, in 100ns units, to use for the execution
   1498                                        of the security protocol command. A Timeout value of 0
   1499                                        means that this function will wait indefinitely for the
   1500                                        security protocol command to execute. If Timeout is greater
   1501                                        than zero, then this function will return EFI_TIMEOUT
   1502                                        if the time required to execute the receive data command
   1503                                        is greater than Timeout.
   1504   @param  SecurityProtocolId           The value of the "Security Protocol" parameter of
   1505                                        the security protocol command to be sent.
   1506   @param  SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
   1507                                        of the security protocol command to be sent.
   1508   @param  PayloadBufferSize            Size in bytes of the payload data buffer.
   1509   @param  PayloadBuffer                A pointer to a destination buffer to store the security
   1510                                        protocol command specific payload data for the security
   1511                                        protocol command. The caller is responsible for having
   1512                                        either implicit or explicit ownership of the buffer.
   1513   @param  PayloadTransferSize          A pointer to a buffer to store the size in bytes of the
   1514                                        data written to the payload data buffer.
   1515 
   1516   @retval EFI_SUCCESS                  The security protocol command completed successfully.
   1517   @retval EFI_WARN_BUFFER_TOO_SMALL    The PayloadBufferSize was too small to store the available
   1518                                        data from the device. The PayloadBuffer contains the truncated data.
   1519   @retval EFI_UNSUPPORTED              The given MediaId does not support security protocol commands.
   1520   @retval EFI_DEVICE_ERROR             The security protocol command completed with an error.
   1521   @retval EFI_NO_MEDIA                 There is no media in the device.
   1522   @retval EFI_MEDIA_CHANGED            The MediaId is not for the current media.
   1523   @retval EFI_INVALID_PARAMETER        The PayloadBuffer or PayloadTransferSize is NULL and
   1524                                        PayloadBufferSize is non-zero.
   1525   @retval EFI_TIMEOUT                  A timeout occurred while waiting for the security
   1526                                        protocol command to execute.
   1527 
   1528 **/
   1529 EFI_STATUS
   1530 EFIAPI
   1531 AtaStorageSecurityReceiveData (
   1532   IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL    *This,
   1533   IN UINT32                                   MediaId,
   1534   IN UINT64                                   Timeout,
   1535   IN UINT8                                    SecurityProtocolId,
   1536   IN UINT16                                   SecurityProtocolSpecificData,
   1537   IN UINTN                                    PayloadBufferSize,
   1538   OUT VOID                                    *PayloadBuffer,
   1539   OUT UINTN                                   *PayloadTransferSize
   1540   )
   1541 {
   1542   EFI_STATUS                       Status;
   1543   ATA_DEVICE                       *Private;
   1544   EFI_TPL                          OldTpl;
   1545 
   1546   DEBUG ((EFI_D_INFO, "EFI Storage Security Protocol - Read\n"));
   1547   if ((PayloadBuffer == NULL || PayloadTransferSize == NULL) && PayloadBufferSize != 0) {
   1548     return EFI_INVALID_PARAMETER;
   1549   }
   1550 
   1551   Status  = EFI_SUCCESS;
   1552   Private = ATA_DEVICE_FROM_STORAGE_SECURITY (This);
   1553 
   1554   if (MediaId != Private->BlockIo.Media->MediaId) {
   1555     return EFI_MEDIA_CHANGED;
   1556   }
   1557 
   1558   if (!Private->BlockIo.Media->MediaPresent) {
   1559     return EFI_NO_MEDIA;
   1560   }
   1561 
   1562   OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
   1563 
   1564   Status = TrustTransferAtaDevice (
   1565              Private,
   1566              PayloadBuffer,
   1567              SecurityProtocolId,
   1568              SecurityProtocolSpecificData,
   1569              PayloadBufferSize,
   1570              FALSE,
   1571              Timeout,
   1572              PayloadTransferSize
   1573              );
   1574 
   1575   gBS->RestoreTPL (OldTpl);
   1576   return Status;
   1577 }
   1578 
   1579 /**
   1580   Send a security protocol command to a device.
   1581 
   1582   The SendData function sends a security protocol command containing the payload
   1583   PayloadBuffer to the given MediaId. The security protocol command sent is
   1584   defined by SecurityProtocolId and contains the security protocol specific data
   1585   SecurityProtocolSpecificData. If the underlying protocol command requires a
   1586   specific padding for the command payload, the SendData function shall add padding
   1587   bytes to the command payload to satisfy the padding requirements.
   1588 
   1589   For devices supporting the SCSI command set, the security protocol command is sent
   1590   using the SECURITY PROTOCOL OUT command defined in SPC-4.
   1591 
   1592   For devices supporting the ATA command set, the security protocol command is sent
   1593   using one of the TRUSTED SEND commands defined in ATA8-ACS if PayloadBufferSize
   1594   is non-zero. If the PayloadBufferSize is zero, the security protocol command is
   1595   sent using the Trusted Non-Data command defined in ATA8-ACS.
   1596 
   1597   If PayloadBuffer is NULL and PayloadBufferSize is non-zero, the function shall
   1598   return EFI_INVALID_PARAMETER.
   1599 
   1600   If the given MediaId does not support security protocol commands, the function
   1601   shall return EFI_UNSUPPORTED. If there is no media in the device, the function
   1602   returns EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the
   1603   device, the function returns EFI_MEDIA_CHANGED.
   1604 
   1605   If the security protocol fails to complete within the Timeout period, the function
   1606   shall return EFI_TIMEOUT.
   1607 
   1608   If the security protocol command completes without an error, the function shall return
   1609   EFI_SUCCESS. If the security protocol command completes with an error, the function
   1610   shall return EFI_DEVICE_ERROR.
   1611 
   1612   @param  This                         Indicates a pointer to the calling context.
   1613   @param  MediaId                      ID of the medium to receive data from.
   1614   @param  Timeout                      The timeout, in 100ns units, to use for the execution
   1615                                        of the security protocol command. A Timeout value of 0
   1616                                        means that this function will wait indefinitely for the
   1617                                        security protocol command to execute. If Timeout is greater
   1618                                        than zero, then this function will return EFI_TIMEOUT
   1619                                        if the time required to execute the receive data command
   1620                                        is greater than Timeout.
   1621   @param  SecurityProtocolId           The value of the "Security Protocol" parameter of
   1622                                        the security protocol command to be sent.
   1623   @param  SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
   1624                                        of the security protocol command to be sent.
   1625   @param  PayloadBufferSize            Size in bytes of the payload data buffer.
   1626   @param  PayloadBuffer                A pointer to a destination buffer to store the security
   1627                                        protocol command specific payload data for the security
   1628                                        protocol command.
   1629 
   1630   @retval EFI_SUCCESS                  The security protocol command completed successfully.
   1631   @retval EFI_UNSUPPORTED              The given MediaId does not support security protocol commands.
   1632   @retval EFI_DEVICE_ERROR             The security protocol command completed with an error.
   1633   @retval EFI_NO_MEDIA                 There is no media in the device.
   1634   @retval EFI_MEDIA_CHANGED            The MediaId is not for the current media.
   1635   @retval EFI_INVALID_PARAMETER        The PayloadBuffer is NULL and PayloadBufferSize is non-zero.
   1636   @retval EFI_TIMEOUT                  A timeout occurred while waiting for the security
   1637                                        protocol command to execute.
   1638 
   1639 **/
   1640 EFI_STATUS
   1641 EFIAPI
   1642 AtaStorageSecuritySendData (
   1643   IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL    *This,
   1644   IN UINT32                                   MediaId,
   1645   IN UINT64                                   Timeout,
   1646   IN UINT8                                    SecurityProtocolId,
   1647   IN UINT16                                   SecurityProtocolSpecificData,
   1648   IN UINTN                                    PayloadBufferSize,
   1649   IN VOID                                     *PayloadBuffer
   1650   )
   1651 {
   1652   EFI_STATUS                       Status;
   1653   ATA_DEVICE                       *Private;
   1654   EFI_TPL                          OldTpl;
   1655 
   1656   DEBUG ((EFI_D_INFO, "EFI Storage Security Protocol - Send\n"));
   1657   if ((PayloadBuffer == NULL) && (PayloadBufferSize != 0)) {
   1658     return EFI_INVALID_PARAMETER;
   1659   }
   1660 
   1661   Status  = EFI_SUCCESS;
   1662   Private = ATA_DEVICE_FROM_STORAGE_SECURITY (This);
   1663 
   1664   if (MediaId != Private->BlockIo.Media->MediaId) {
   1665     return EFI_MEDIA_CHANGED;
   1666   }
   1667 
   1668   OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
   1669   Status = TrustTransferAtaDevice (
   1670              Private,
   1671              PayloadBuffer,
   1672              SecurityProtocolId,
   1673              SecurityProtocolSpecificData,
   1674              PayloadBufferSize,
   1675              TRUE,
   1676              Timeout,
   1677              NULL
   1678              );
   1679 
   1680   gBS->RestoreTPL (OldTpl);
   1681   return Status;
   1682 }
   1683 
   1684 /**
   1685   The user Entry Point for module AtaBus. The user code starts with this function.
   1686 
   1687   @param[in] ImageHandle    The firmware allocated handle for the EFI image.
   1688   @param[in] SystemTable    A pointer to the EFI System Table.
   1689 
   1690   @retval EFI_SUCCESS       The entry point is executed successfully.
   1691   @retval other             Some error occurs when executing this entry point.
   1692 
   1693 **/
   1694 EFI_STATUS
   1695 EFIAPI
   1696 InitializeAtaBus(
   1697   IN EFI_HANDLE           ImageHandle,
   1698   IN EFI_SYSTEM_TABLE     *SystemTable
   1699   )
   1700 {
   1701   EFI_STATUS              Status;
   1702 
   1703   //
   1704   // Install driver model protocol(s).
   1705   //
   1706   Status = EfiLibInstallDriverBindingComponentName2 (
   1707              ImageHandle,
   1708              SystemTable,
   1709              &gAtaBusDriverBinding,
   1710              ImageHandle,
   1711              &gAtaBusComponentName,
   1712              &gAtaBusComponentName2
   1713              );
   1714   ASSERT_EFI_ERROR (Status);
   1715 
   1716   return Status;
   1717 }
   1718 
   1719