]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressHci.c
1) Add type cast for better coding style.
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / NvmExpressDxe / NvmExpressHci.c
index 157e10127aa76675ef38b981253ed5d0849f3093..ac09fc2bd82f70ef51ee97b11f7d8e65656471b9 100644 (file)
@@ -2,7 +2,7 @@
   NvmExpressDxe driver is used to manage non-volatile memory subsystem which follows\r
   NVM Express specification.\r
 \r
-  Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -33,21 +33,23 @@ ReadNvmeControllerCapabilities (
 {\r
   EFI_PCI_IO_PROTOCOL   *PciIo;\r
   EFI_STATUS            Status;\r
+  UINT64                Data;\r
 \r
   PciIo  = Private->PciIo;\r
   Status = PciIo->Mem.Read (\r
                         PciIo,\r
-                        EfiPciIoWidthUint64,\r
+                        EfiPciIoWidthUint32,\r
                         NVME_BAR,\r
                         NVME_CAP_OFFSET,\r
-                        1,\r
-                        Cap\r
+                        2,\r
+                        &Data\r
                         );\r
 \r
   if (EFI_ERROR(Status)) {\r
     return Status;\r
   }\r
 \r
+  WriteUnaligned64 ((UINT64*)Cap, Data);\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -69,6 +71,7 @@ ReadNvmeControllerConfiguration (
 {\r
   EFI_PCI_IO_PROTOCOL   *PciIo;\r
   EFI_STATUS            Status;\r
+  UINT32                Data;\r
 \r
   PciIo  = Private->PciIo;\r
   Status = PciIo->Mem.Read (\r
@@ -77,13 +80,14 @@ ReadNvmeControllerConfiguration (
                         NVME_BAR,\r
                         NVME_CC_OFFSET,\r
                         1,\r
-                        Cc\r
+                        &Data\r
                         );\r
 \r
   if (EFI_ERROR(Status)) {\r
     return Status;\r
   }\r
 \r
+  WriteUnaligned32 ((UINT32*)Cc, Data);\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -105,15 +109,17 @@ WriteNvmeControllerConfiguration (
 {\r
   EFI_PCI_IO_PROTOCOL   *PciIo;\r
   EFI_STATUS            Status;\r
+  UINT32                Data;\r
 \r
   PciIo  = Private->PciIo;\r
+  Data   = ReadUnaligned32 ((UINT32*)Cc);\r
   Status = PciIo->Mem.Write (\r
                         PciIo,\r
                         EfiPciIoWidthUint32,\r
                         NVME_BAR,\r
                         NVME_CC_OFFSET,\r
                         1,\r
-                        Cc\r
+                        &Data\r
                         );\r
 \r
   if (EFI_ERROR(Status)) {\r
@@ -149,6 +155,7 @@ ReadNvmeControllerStatus (
 {\r
   EFI_PCI_IO_PROTOCOL   *PciIo;\r
   EFI_STATUS            Status;\r
+  UINT32                Data;\r
 \r
   PciIo  = Private->PciIo;\r
   Status = PciIo->Mem.Read (\r
@@ -157,13 +164,14 @@ ReadNvmeControllerStatus (
                         NVME_BAR,\r
                         NVME_CSTS_OFFSET,\r
                         1,\r
-                        Csts\r
+                        &Data\r
                         );\r
 \r
   if (EFI_ERROR(Status)) {\r
     return Status;\r
   }\r
 \r
+  WriteUnaligned32 ((UINT32*)Csts, Data);\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -185,6 +193,7 @@ ReadNvmeAdminQueueAttributes (
 {\r
   EFI_PCI_IO_PROTOCOL   *PciIo;\r
   EFI_STATUS            Status;\r
+  UINT32                Data;\r
 \r
   PciIo  = Private->PciIo;\r
   Status = PciIo->Mem.Read (\r
@@ -193,13 +202,14 @@ ReadNvmeAdminQueueAttributes (
                         NVME_BAR,\r
                         NVME_AQA_OFFSET,\r
                         1,\r
-                        Aqa\r
+                        &Data\r
                         );\r
 \r
   if (EFI_ERROR(Status)) {\r
     return Status;\r
   }\r
 \r
+  WriteUnaligned32 ((UINT32*)Aqa, Data);\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -221,15 +231,17 @@ WriteNvmeAdminQueueAttributes (
 {\r
   EFI_PCI_IO_PROTOCOL   *PciIo;\r
   EFI_STATUS            Status;\r
+  UINT32                Data;\r
 \r
   PciIo  = Private->PciIo;\r
+  Data   = ReadUnaligned32 ((UINT32*)Aqa);\r
   Status = PciIo->Mem.Write (\r
                         PciIo,\r
                         EfiPciIoWidthUint32,\r
                         NVME_BAR,\r
                         NVME_AQA_OFFSET,\r
                         1,\r
-                        Aqa\r
+                        &Data\r
                         );\r
 \r
   if (EFI_ERROR(Status)) {\r
@@ -260,21 +272,23 @@ ReadNvmeAdminSubmissionQueueBaseAddress (
 {\r
   EFI_PCI_IO_PROTOCOL   *PciIo;\r
   EFI_STATUS            Status;\r
+  UINT64                Data;\r
 \r
   PciIo  = Private->PciIo;\r
   Status = PciIo->Mem.Read (\r
                         PciIo,\r
-                        EfiPciIoWidthUint64,\r
+                        EfiPciIoWidthUint32,\r
                         NVME_BAR,\r
                         NVME_ASQ_OFFSET,\r
-                        1,\r
-                        Asq\r
+                        2,\r
+                        &Data\r
                         );\r
 \r
   if (EFI_ERROR(Status)) {\r
     return Status;\r
   }\r
 \r
+  WriteUnaligned64 ((UINT64*)Asq, Data);\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -296,15 +310,18 @@ WriteNvmeAdminSubmissionQueueBaseAddress (
 {\r
   EFI_PCI_IO_PROTOCOL   *PciIo;\r
   EFI_STATUS            Status;\r
+  UINT64                Data;\r
 \r
   PciIo  = Private->PciIo;\r
+  Data   = ReadUnaligned64 ((UINT64*)Asq);\r
+\r
   Status = PciIo->Mem.Write (\r
                         PciIo,\r
-                        EfiPciIoWidthUint64,\r
+                        EfiPciIoWidthUint32,\r
                         NVME_BAR,\r
                         NVME_ASQ_OFFSET,\r
-                        1,\r
-                        Asq\r
+                        2,\r
+                        &Data\r
                         );\r
 \r
   if (EFI_ERROR(Status)) {\r
@@ -334,21 +351,24 @@ ReadNvmeAdminCompletionQueueBaseAddress (
 {\r
   EFI_PCI_IO_PROTOCOL   *PciIo;\r
   EFI_STATUS            Status;\r
+  UINT64                Data;\r
 \r
   PciIo  = Private->PciIo;\r
+\r
   Status = PciIo->Mem.Read (\r
                         PciIo,\r
-                        EfiPciIoWidthUint64,\r
+                        EfiPciIoWidthUint32,\r
                         NVME_BAR,\r
                         NVME_ACQ_OFFSET,\r
-                        1,\r
-                        Acq\r
+                        2,\r
+                        &Data\r
                         );\r
 \r
   if (EFI_ERROR(Status)) {\r
     return Status;\r
   }\r
 \r
+  WriteUnaligned64 ((UINT64*)Acq, Data);\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -370,15 +390,18 @@ WriteNvmeAdminCompletionQueueBaseAddress (
 {\r
   EFI_PCI_IO_PROTOCOL   *PciIo;\r
   EFI_STATUS            Status;\r
+  UINT64                Data;\r
 \r
   PciIo  = Private->PciIo;\r
+  Data   = ReadUnaligned64 ((UINT64*)Acq);\r
+\r
   Status = PciIo->Mem.Write (\r
                         PciIo,\r
-                        EfiPciIoWidthUint64,\r
+                        EfiPciIoWidthUint32,\r
                         NVME_BAR,\r
                         NVME_ACQ_OFFSET,\r
-                        1,\r
-                        Acq\r
+                        2,\r
+                        &Data\r
                         );\r
 \r
   if (EFI_ERROR(Status)) {\r
@@ -407,6 +430,8 @@ NvmeDisableController (
   NVME_CC                Cc;\r
   NVME_CSTS              Csts;\r
   EFI_STATUS             Status;\r
+  UINT32                 Index;\r
+  UINT8                  Timeout;\r
 \r
   //\r
   // Read Controller Configuration Register.\r
@@ -427,19 +452,35 @@ NvmeDisableController (
     return Status;\r
   }\r
 \r
-  gBS->Stall(10000);\r
-\r
   //\r
-  // Check if the controller is reset\r
+  // Cap.To specifies max delay time in 500ms increments for Csts.Rdy to transition from 1 to 0 after\r
+  // Cc.Enable transition from 1 to 0. Loop produces a 1 millisecond delay per itteration, up to 500 * Cap.To.\r
   //\r
-  Status = ReadNvmeControllerStatus (Private, &Csts);\r
+  if (Private->Cap.To == 0) {\r
+    Timeout = 1;\r
+  } else {\r
+    Timeout = Private->Cap.To;\r
+  }\r
 \r
-  if (EFI_ERROR(Status)) {\r
-    return Status;\r
+  for(Index = (Timeout * 500); Index != 0; --Index) {\r
+    gBS->Stall(1000);\r
+\r
+    //\r
+    // Check if the controller is initialized\r
+    //\r
+    Status = ReadNvmeControllerStatus (Private, &Csts);\r
+\r
+    if (EFI_ERROR(Status)) {\r
+      return Status;\r
+    }\r
+\r
+    if (Csts.Rdy == 0) {\r
+      break;\r
+    }\r
   }\r
 \r
-  if (Csts.Rdy != 0) {\r
-    return EFI_DEVICE_ERROR;\r
+  if (Index == 0) {\r
+    Status = EFI_DEVICE_ERROR;\r
   }\r
 \r
   DEBUG ((EFI_D_INFO, "NVMe controller is disabled with status [%r].\n", Status));\r
@@ -775,7 +816,7 @@ NvmeControllerInit (
                     );\r
 \r
   if (!EFI_ERROR (Status)) {\r
-    Supports &= EFI_PCI_DEVICE_ENABLE;\r
+    Supports &= (UINT64)EFI_PCI_DEVICE_ENABLE;\r
     Status    = PciIo->Attributes (\r
                          PciIo,\r
                          EfiPciIoAttributeOperationEnable,\r
@@ -819,8 +860,10 @@ NvmeControllerInit (
   //\r
   // set number of entries admin submission & completion queues.\r
   //\r
-  Aqa.Asqs = NVME_ASQ_SIZE;\r
-  Aqa.Acqs = NVME_ACQ_SIZE;\r
+  Aqa.Asqs  = NVME_ASQ_SIZE;\r
+  Aqa.Rsvd1 = 0;\r
+  Aqa.Acqs  = NVME_ACQ_SIZE;\r
+  Aqa.Rsvd2 = 0;\r
 \r
   //\r
   // Address of admin submission queue.\r
@@ -921,6 +964,25 @@ NvmeControllerInit (
     Private->ControllerData = NULL;\r
     return EFI_NOT_FOUND;\r
   }\r
+\r
+  //\r
+  // Dump NvmExpress Identify Controller Data\r
+  //\r
+  Private->ControllerData->Sn[19] = 0;\r
+  Private->ControllerData->Mn[39] = 0;\r
+  DEBUG ((EFI_D_INFO, " == NVME IDENTIFY CONTROLLER DATA ==\n"));\r
+  DEBUG ((EFI_D_INFO, "    PCI VID   : 0x%x\n", Private->ControllerData->Vid));\r
+  DEBUG ((EFI_D_INFO, "    PCI SSVID : 0x%x\n", Private->ControllerData->Ssvid));\r
+  DEBUG ((EFI_D_INFO, "    SN        : %a\n",   (CHAR8 *)(Private->ControllerData->Sn)));\r
+  DEBUG ((EFI_D_INFO, "    MN        : %a\n",   (CHAR8 *)(Private->ControllerData->Mn)));\r
+  DEBUG ((EFI_D_INFO, "    FR        : 0x%x\n", *((UINT64*)Private->ControllerData->Fr)));\r
+  DEBUG ((EFI_D_INFO, "    RAB       : 0x%x\n", Private->ControllerData->Rab));\r
+  DEBUG ((EFI_D_INFO, "    IEEE      : 0x%x\n", *(UINT32*)Private->ControllerData->Ieee_oui));\r
+  DEBUG ((EFI_D_INFO, "    AERL      : 0x%x\n", Private->ControllerData->Aerl));\r
+  DEBUG ((EFI_D_INFO, "    SQES      : 0x%x\n", Private->ControllerData->Sqes));\r
+  DEBUG ((EFI_D_INFO, "    CQES      : 0x%x\n", Private->ControllerData->Cqes));\r
+  DEBUG ((EFI_D_INFO, "    NN        : 0x%x\n", Private->ControllerData->Nn));\r
+\r
   return Status;\r
 }\r
 \r