]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Ata/AtaBusDxe/AtaBus.c
Add BlockIO2 Protocol.
[mirror_edk2.git] / MdeModulePkg / Bus / Ata / AtaBusDxe / AtaBus.c
index a3c0670c98c744df6332279cb358095e913c43ef..437eca725c596db0a2b632b8ca6758f8d64777be 100644 (file)
@@ -44,6 +44,13 @@ ATA_DEVICE gAtaDeviceTemplate = {
     AtaBlockIoWriteBlocks,\r
     AtaBlockIoFlushBlocks\r
   },\r
+  {                            // BlockIo2\r
+    NULL,\r
+    AtaBlockIoResetEx,\r
+    AtaBlockIoReadBlocksEx,\r
+    AtaBlockIoWriteBlocksEx,\r
+    AtaBlockIoFlushBlocksEx\r
+  },\r
   {                            // BlockMedia\r
     0,                         // MediaId\r
     FALSE,                     // RemovableMedia\r
@@ -75,7 +82,8 @@ ATA_DEVICE gAtaDeviceTemplate = {
   FALSE,                       // Lba48Bit\r
   NULL,                        // IdentifyData\r
   NULL,                        // ControllerNameTable\r
-  {L'\0', }                    // ModelName\r
+  {L'\0', },                   // ModelName\r
+  {NULL, NULL}                 // AtaTaskList\r
 };\r
 \r
 /**\r
@@ -135,12 +143,34 @@ ReleaseAtaResources (
   IN ATA_DEVICE  *AtaDevice\r
   )\r
 {\r
+  ATA_BUS_ASYN_TASK *Task;\r
+  LIST_ENTRY        *Entry;\r
+  LIST_ENTRY        *DelEntry;\r
+  EFI_TPL           OldTpl;\r
+\r
   FreeUnicodeStringTable (AtaDevice->ControllerNameTable);\r
   FreeAlignedBuffer (AtaDevice->Asb, sizeof (*AtaDevice->Asb));\r
   FreeAlignedBuffer (AtaDevice->IdentifyData, sizeof (*AtaDevice->IdentifyData));\r
   if (AtaDevice->DevicePath != NULL) {\r
     FreePool (AtaDevice->DevicePath);\r
   }\r
+  OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
+  if (!IsListEmpty (&AtaDevice->AtaTaskList)) {\r
+    //\r
+    // Free the Subtask list.\r
+    //\r
+    for(Entry = (&AtaDevice->AtaTaskList)->ForwardLink; \r
+        Entry != (&AtaDevice->AtaTaskList);\r
+       ) {\r
+      DelEntry = Entry;\r
+      Entry    = Entry->ForwardLink;\r
+      Task     = ATA_AYNS_TASK_FROM_ENTRY (DelEntry);\r
+      \r
+      RemoveEntryList (DelEntry);\r
+      FreeAtaSubTask (Task);\r
+    }\r
+  }\r
+  gBS->RestoreTPL (OldTpl);\r
   FreePool (AtaDevice);\r
 }\r
 \r
@@ -218,10 +248,11 @@ RegisterAtaDevice (
   //\r
   // Initializes ATA device structures and allocates the required buffer.\r
   //\r
-  AtaDevice->BlockIo.Media = &AtaDevice->BlockMedia;\r
-  AtaDevice->AtaBusDriverData = AtaBusDriverData;\r
-  AtaDevice->DevicePath = DevicePath;\r
-  AtaDevice->Port = Port;\r
+  AtaDevice->BlockIo.Media      = &AtaDevice->BlockMedia;\r
+  AtaDevice->BlockIo2.Media     = &AtaDevice->BlockMedia;\r
+  AtaDevice->AtaBusDriverData   = AtaBusDriverData;\r
+  AtaDevice->DevicePath         = DevicePath;\r
+  AtaDevice->Port               = Port;\r
   AtaDevice->PortMultiplierPort = PortMultiplierPort;\r
   AtaDevice->Asb = AllocateAlignedBuffer (AtaDevice, sizeof (*AtaDevice->Asb));\r
   if (AtaDevice->Asb == NULL) {\r
@@ -234,6 +265,11 @@ RegisterAtaDevice (
     goto Done;\r
   }\r
 \r
+  //\r
+  // Initial Ata Task List\r
+  //\r
+  InitializeListHead (&AtaDevice->AtaTaskList);\r
+\r
   //\r
   // Try to identify the ATA device via the ATA pass through command. \r
   //\r
@@ -241,7 +277,7 @@ RegisterAtaDevice (
   if (EFI_ERROR (Status)) {\r
     goto Done;\r
   }\r
-  \r
+\r
   //\r
   // Build controller name for Component Name (2) protocol.\r
   //\r
@@ -281,6 +317,8 @@ RegisterAtaDevice (
                   AtaDevice->DevicePath,\r
                   &gEfiBlockIoProtocolGuid,\r
                   &AtaDevice->BlockIo,\r
+                  &gEfiBlockIo2ProtocolGuid,\r
+                  &AtaDevice->BlockIo2,\r
                   &gEfiDiskInfoProtocolGuid,\r
                   &AtaDevice->DiskInfo,\r
                   NULL\r
@@ -334,8 +372,11 @@ UnregisterAtaDevice (
 {\r
   EFI_STATUS                  Status;\r
   EFI_BLOCK_IO_PROTOCOL       *BlockIo;\r
+  EFI_BLOCK_IO2_PROTOCOL      *BlockIo2;\r
   ATA_DEVICE                  *AtaDevice;\r
   EFI_ATA_PASS_THRU_PROTOCOL  *AtaPassThru;\r
+  BlockIo2 = NULL;\r
+  BlockIo  = NULL;\r
 \r
   Status = gBS->OpenProtocol (\r
                   Handle,\r
@@ -346,10 +387,30 @@ UnregisterAtaDevice (
                   EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
                   );\r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    //\r
+    // Locate BlockIo2 protocol\r
+    //\r
+    Status = gBS->OpenProtocol (\r
+                    Handle,\r
+                    &gEfiBlockIo2ProtocolGuid,\r
+                    (VOID **) &BlockIo2,\r
+                    This->DriverBindingHandle,\r
+                    Controller,\r
+                    EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
   }\r
 \r
-  AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (BlockIo);\r
+  //\r
+  // Get AtaDevice data.\r
+  //\r
+  if (BlockIo != NULL) {\r
+    AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (BlockIo);\r
+  } else {\r
+    AtaDevice = ATA_DEVICE_FROM_BLOCK_IO2 (BlockIo2);\r
+  } \r
 \r
   //\r
   // Close the child handle\r
@@ -361,12 +422,18 @@ UnregisterAtaDevice (
          Handle\r
          );\r
 \r
+  //\r
+  // The Ata Bus driver installs the BlockIo and BlockIo2 in the DriverBindingStart().\r
+  // Here should uninstall both of them.\r
+  //\r
   Status = gBS->UninstallMultipleProtocolInterfaces (\r
                   Handle,\r
                   &gEfiDevicePathProtocolGuid,\r
                   AtaDevice->DevicePath,\r
                   &gEfiBlockIoProtocolGuid,\r
                   &AtaDevice->BlockIo,\r
+                  &gEfiBlockIo2ProtocolGuid,\r
+                  &AtaDevice->BlockIo2,\r
                   &gEfiDiskInfoProtocolGuid,\r
                   &AtaDevice->DiskInfo,\r
                   NULL\r
@@ -385,7 +452,6 @@ UnregisterAtaDevice (
   }\r
 \r
   ReleaseAtaResources (AtaDevice);\r
-\r
   return Status;\r
 }\r
 \r
@@ -446,7 +512,7 @@ AtaBusDriverBindingSupported (
   EFI_ATA_PASS_THRU_PROTOCOL        *AtaPassThru;\r
   UINT16                            Port;\r
   UINT16                            PortMultiplierPort;\r
\r
+\r
   //\r
   // Test EFI_ATA_PASS_THRU_PROTOCOL on controller handle.\r
   //\r
@@ -522,7 +588,7 @@ AtaBusDriverBindingSupported (
   @param[in]  RemainingDevicePath  A pointer to the remaining portion of a device path.  This \r
                                    parameter is ignored by device drivers, and is optional for bus \r
                                    drivers. For a bus driver, if this parameter is NULL, then handles \r
-                                   for all the children of Controller are created by this driver.  \r
+                                   for all the children of Controller are created by this driver.\r
                                    If this parameter is not NULL and the first Device Path Node is \r
                                    not the End of Device Path Node, then only the handle for the \r
                                    child device specified by the first Device Path Node of \r
@@ -589,7 +655,7 @@ AtaBusDriverBindingStart (
     }\r
 \r
     AtaBusDriverData->AtaPassThru = AtaPassThru;\r
-    AtaBusDriverData->Controller = Controller;\r
+    AtaBusDriverData->Controller  = Controller;\r
     AtaBusDriverData->ParentDevicePath = ParentDevicePath;\r
     AtaBusDriverData->DriverBindingHandle = This->DriverBindingHandle;\r
 \r
@@ -628,7 +694,7 @@ AtaBusDriverBindingStart (
         //\r
         break;\r
       }\r
-      \r
+\r
       PortMultiplierPort = 0xFFFF;\r
       while (TRUE) {\r
         Status = AtaPassThru->GetNextDevice (AtaPassThru, Port, &PortMultiplierPort);\r
@@ -649,7 +715,7 @@ AtaBusDriverBindingStart (
       Status = RegisterAtaDevice (AtaBusDriverData,Port, PortMultiplierPort);\r
     }\r
   }\r
-  \r
+\r
   return Status;\r
 \r
 ErrorExit:\r
@@ -789,7 +855,7 @@ AtaBlockIoReset (
 \r
   AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (This);\r
 \r
-  Status = ResetAtaDevice (AtaDevice);  \r
+  Status = ResetAtaDevice (AtaDevice);\r
 \r
   if (EFI_ERROR (Status)) {\r
     Status = EFI_DEVICE_ERROR;\r
@@ -803,13 +869,18 @@ AtaBlockIoReset (
 /**\r
   Read/Write BufferSize bytes from Lba from/into Buffer.\r
 \r
-  @param  This       Indicates a pointer to the calling context.\r
-  @param  MediaId    The media ID that the read/write request is for.\r
-  @param  Lba        The starting logical block address to be read/written. The caller is\r
-                     responsible for reading/writing to only legitimate locations.\r
-  @param  BufferSize Size of Buffer, must be a multiple of device block size.\r
-  @param  Buffer     A pointer to the destination/source buffer for the data.\r
-  @param  IsWrite    Indicates whether it is a write operation.\r
+  @param[in]       This       Indicates a pointer to the calling context. Either be\r
+                              block I/O or block I/O2. \r
+  @param[in]       MediaId    The media ID that the read/write request is for.\r
+  @param[in]       Lba        The starting logical block address to be read/written.\r
+                              The caller is responsible for reading/writing to only\r
+                              legitimate locations.\r
+  @param[in, out]  Token      A pointer to the token associated with the transaction.\r
+  @param[in]       BufferSize Size of Buffer, must be a multiple of device block size.\r
+  @param[out]      Buffer     A pointer to the destination/source buffer for the data.\r
+  @param[in]       IsBlockIo2 Indicate the calling is from BlockIO or BlockIO2. TURE is\r
+                              from BlockIO2, FALSE is for BlockIO.\r
+  @param[in]       IsWrite    Indicates whether it is a write operation.\r
 \r
   @retval EFI_SUCCESS           The data was read/written correctly to the device.\r
   @retval EFI_WRITE_PROTECTED   The device can not be read/written to.\r
@@ -823,12 +894,14 @@ AtaBlockIoReset (
 **/\r
 EFI_STATUS\r
 BlockIoReadWrite (\r
-  IN  EFI_BLOCK_IO_PROTOCOL   *This,\r
-  IN  UINT32                  MediaId,\r
-  IN  EFI_LBA                 Lba,\r
-  IN  UINTN                   BufferSize,\r
-  OUT VOID                    *Buffer,\r
-  IN  BOOLEAN                 IsWrite\r
+  IN     VOID                    *This,\r
+  IN     UINT32                  MediaId,\r
+  IN     EFI_LBA                 Lba,\r
+  IN OUT EFI_BLOCK_IO2_TOKEN     *Token,\r
+  IN     UINTN                   BufferSize,\r
+  OUT    VOID                    *Buffer,\r
+  IN     BOOLEAN                 IsBlockIo2,\r
+  IN     BOOLEAN                 IsWrite\r
   )\r
 {\r
   ATA_DEVICE                        *AtaDevice;\r
@@ -839,21 +912,28 @@ BlockIoReadWrite (
   UINTN                             NumberOfBlocks;\r
   UINTN                             IoAlign;\r
 \r
-  //\r
-  // Check parameters.\r
-  //\r
-  Media = This->Media;\r
+  if (IsBlockIo2) {\r
+   Media     = ((EFI_BLOCK_IO2_PROTOCOL *) This)->Media;\r
+   AtaDevice = ATA_DEVICE_FROM_BLOCK_IO2 (This);\r
+  } else {\r
+   Media     = ((EFI_BLOCK_IO_PROTOCOL *) This)->Media;\r
+   AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (This);\r
+  }\r
+\r
   if (MediaId != Media->MediaId) {\r
     return EFI_MEDIA_CHANGED;\r
   }\r
 \r
+  //\r
+  // Check parameters.\r
+  //\r
   if (Buffer == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
   if (BufferSize == 0) {\r
     return EFI_SUCCESS;\r
-  }\r
+  }  \r
 \r
   BlockSize = Media->BlockSize;\r
   if ((BufferSize % BlockSize) != 0) {\r
@@ -871,13 +951,11 @@ BlockIoReadWrite (
   }\r
 \r
   OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
-\r
-  AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (This);\r
   \r
   //\r
   // Invoke low level AtaDevice Access Routine.\r
   //\r
-  Status = AccessAtaDevice (AtaDevice, Buffer, Lba, NumberOfBlocks, IsWrite);\r
+  Status = AccessAtaDevice (AtaDevice, Buffer, Lba, NumberOfBlocks, IsWrite, Token);\r
  \r
   gBS->RestoreTPL (OldTpl);\r
 \r
@@ -914,7 +992,7 @@ AtaBlockIoReadBlocks (
   OUT VOID                    *Buffer\r
   )\r
 {\r
-  return BlockIoReadWrite (This, MediaId, Lba, BufferSize, Buffer, FALSE);\r
+  return BlockIoReadWrite ((VOID *) This, MediaId, Lba, NULL, BufferSize, Buffer, FALSE, FALSE);\r
 }\r
 \r
 \r
@@ -948,7 +1026,7 @@ AtaBlockIoWriteBlocks (
   IN  VOID                    *Buffer\r
   )\r
 {\r
-  return BlockIoReadWrite (This, MediaId, Lba, BufferSize, Buffer, TRUE);\r
+  return BlockIoReadWrite ((VOID *) This, MediaId, Lba, NULL, BufferSize, Buffer, FALSE, TRUE);\r
 }\r
 \r
 \r
@@ -974,7 +1052,147 @@ AtaBlockIoFlushBlocks (
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Reset the Block Device.\r
+\r
+  @param[in]  This                 Indicates a pointer to the calling context.\r
+  @param[in]  ExtendedVerification Driver may perform diagnostics on reset.\r
+\r
+  @retval EFI_SUCCESS          The device was reset.\r
+  @retval EFI_DEVICE_ERROR     The device is not functioning properly and could\r
+                               not be reset.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+AtaBlockIoResetEx (\r
+  IN  EFI_BLOCK_IO2_PROTOCOL  *This,\r
+  IN  BOOLEAN                 ExtendedVerification\r
+  )\r
+{\r
+  EFI_STATUS      Status;\r
+  ATA_DEVICE      *AtaDevice;\r
+  EFI_TPL         OldTpl;\r
+\r
+  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
+\r
+  AtaDevice = ATA_DEVICE_FROM_BLOCK_IO2 (This);\r
+\r
+  Status = ResetAtaDevice (AtaDevice);\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    Status = EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  gBS->RestoreTPL (OldTpl);\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Read BufferSize bytes from Lba into Buffer.\r
+\r
+  @param[in]       This       Indicates a pointer to the calling context.\r
+  @param[in]       MediaId    Id of the media, changes every time the media is replaced.\r
+  @param[in]       Lba        The starting Logical Block Address to read from.\r
+  @param[in, out]  Token      A pointer to the token associated with the transaction.\r
+  @param[in]       BufferSize Size of Buffer, must be a multiple of device block size.\r
+  @param[out]      Buffer     A pointer to the destination buffer for the data. The caller is\r
+                              responsible for either having implicit or explicit ownership of the buffer.\r
+\r
+  @retval EFI_SUCCESS           The read request was queued if Event is not NULL.\r
+                                The data was read correctly from the device if\r
+                                the Event is NULL.\r
+  @retval EFI_DEVICE_ERROR      The device reported an error while performing\r
+                                the read.\r
+  @retval EFI_NO_MEDIA          There is no media in the device.\r
+  @retval EFI_MEDIA_CHANGED     The MediaId is not for the current media.\r
+  @retval EFI_BAD_BUFFER_SIZE   The BufferSize parameter is not a multiple of the\r
+                                intrinsic block size of the device.\r
+  @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid, \r
+                                or the buffer is not on proper alignment.\r
+  @retval EFI_OUT_OF_RESOURCES  The request could not be completed due to a lack\r
+                                of resources.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+AtaBlockIoReadBlocksEx (\r
+  IN  EFI_BLOCK_IO2_PROTOCOL  *This,\r
+  IN  UINT32                  MediaId,\r
+  IN  EFI_LBA                 Lba,\r
+  IN OUT EFI_BLOCK_IO2_TOKEN  *Token,\r
+  IN  UINTN                   BufferSize,\r
+  OUT VOID                    *Buffer\r
+  )\r
+{\r
+  return BlockIoReadWrite ((VOID *) This, MediaId, Lba, Token, BufferSize, Buffer, TRUE, FALSE);\r
+}\r
+\r
+\r
+/**\r
+  Write BufferSize bytes from Lba into Buffer.\r
+\r
+  @param[in]       This       Indicates a pointer to the calling context.\r
+  @param[in]       MediaId    The media ID that the write request is for.\r
+  @param[in]       Lba        The starting logical block address to be written. The\r
+                              caller is responsible for writing to only legitimate\r
+                              locations.\r
+  @param[in, out]  Token      A pointer to the token associated with the transaction.\r
+  @param[in]       BufferSize Size of Buffer, must be a multiple of device block size.\r
+  @param[in]       Buffer     A pointer to the source buffer for the data.\r
 \r
+  @retval EFI_SUCCESS           The data was written correctly to the device.\r
+  @retval EFI_WRITE_PROTECTED   The device can not be written to.\r
+  @retval EFI_DEVICE_ERROR      The device reported an error while performing the write.\r
+  @retval EFI_NO_MEDIA          There is no media in the device.\r
+  @retval EFI_MEDIA_CHNAGED     The MediaId does not matched the current device.\r
+  @retval EFI_BAD_BUFFER_SIZE   The Buffer was not a multiple of the block size of the device.\r
+  @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid, \r
+                                or the buffer is not on proper alignment.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+AtaBlockIoWriteBlocksEx (\r
+  IN  EFI_BLOCK_IO2_PROTOCOL  *This,\r
+  IN  UINT32                  MediaId,\r
+  IN  EFI_LBA                 Lba,\r
+  IN OUT EFI_BLOCK_IO2_TOKEN  *Token,\r
+  IN  UINTN                   BufferSize,\r
+  IN  VOID                    *Buffer\r
+  )\r
+{\r
+  return BlockIoReadWrite ((VOID *) This, MediaId, Lba, Token, BufferSize, Buffer, TRUE, TRUE);\r
+}\r
+\r
+\r
+/**\r
+  Flush the Block Device.\r
+\r
+  @param[in]       This       Indicates a pointer to the calling context.\r
+  @param[in, out]  Token      A pointer to the token associated with the transaction.\r
+\r
+  @retval EFI_SUCCESS       All outstanding data was written to the device\r
+  @retval EFI_DEVICE_ERROR  The device reported an error while writing back the data\r
+  @retval EFI_NO_MEDIA      There is no media in the device.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+AtaBlockIoFlushBlocksEx (\r
+  IN  EFI_BLOCK_IO2_PROTOCOL  *This,\r
+  IN OUT EFI_BLOCK_IO2_TOKEN  *Token\r
+  )\r
+{\r
+  //\r
+  // Signla event and return directly.\r
+  //\r
+  if (Token != NULL && Token->Event != NULL) {\r
+    Token->TransactionStatus = EFI_SUCCESS;\r
+    gBS->SignalEvent (Token->Event);\r
+  }\r
+  return EFI_SUCCESS;\r
+}\r
 /**\r
   Provides inquiry information for the controller type.\r
   \r