1 /** @file
2 
3   WARNING:
4   This driver fails to follow the UEFI driver model without a good
5   reason, and only remains in the tree because it is still used by
6   a small number of platforms. It will be removed when no longer used.
7   New platforms should not use it, and no one should use this as
8   reference code for developing new drivers.
9 
10   Copyright (c) 2011-2020, ARM Limited. All rights reserved.
11 
12   SPDX-License-Identifier: BSD-2-Clause-Patent
13 
14 **/
15 
16 #include "SataSiI3132.h"
17 
18 #include <IndustryStandard/Atapi.h>
19 #include <Library/DevicePathLib.h>
20 
21 SATA_SI3132_DEVICE*
GetSataDevice(IN SATA_SI3132_INSTANCE * SataInstance,IN UINT16 Port,IN UINT16 PortMultiplierPort)22 GetSataDevice (
23   IN  SATA_SI3132_INSTANCE* SataInstance,
24   IN  UINT16 Port,
25   IN  UINT16 PortMultiplierPort
26 ) {
27   LIST_ENTRY              *List;
28   SATA_SI3132_PORT        *SataPort;
29   SATA_SI3132_DEVICE      *SataDevice;
30 
31   if (Port >= SATA_SII3132_MAXPORT) {
32     return NULL;
33   }
34 
35   SataPort = &(SataInstance->Ports[Port]);
36   List = SataPort->Devices.ForwardLink;
37 
38   while (List != &SataPort->Devices) {
39     SataDevice = (SATA_SI3132_DEVICE*)List;
40     if (SataDevice->Index == PortMultiplierPort) {
41       return SataDevice;
42     }
43     List = List->ForwardLink;
44   }
45   return NULL;
46 }
47 
48 EFI_STATUS
49 EFIAPI
SiI3132AtaPassThruCommand(IN SATA_SI3132_INSTANCE * SataSiI3132Instance,IN SATA_SI3132_PORT * SataPort,IN UINT16 PortMultiplierPort,IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET * Packet,IN EFI_EVENT Event OPTIONAL)50 SiI3132AtaPassThruCommand (
51   IN     SATA_SI3132_INSTANCE             *SataSiI3132Instance,
52   IN     SATA_SI3132_PORT                 *SataPort,
53   IN     UINT16                           PortMultiplierPort,
54   IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet,
55   IN     EFI_EVENT                        Event OPTIONAL
56   )
57 {
58   SATA_SI3132_DEVICE      *SataDevice;
59   EFI_PHYSICAL_ADDRESS    PhysInDataBuffer;
60   UINTN                   InDataBufferLength = 0;
61   EFI_PHYSICAL_ADDRESS    PhysOutDataBuffer;
62   UINTN                   OutDataBufferLength;
63   CONST UINTN             EmptySlot = 0;
64   UINTN                   Control = PRB_CTRL_ATA;
65   UINTN                   Protocol = 0;
66   UINT32                  Value32, Error, Timeout = 0;
67   CONST UINT32            IrqMask = (SII3132_PORT_INT_CMDCOMPL | SII3132_PORT_INT_CMDERR) << 16;
68   EFI_STATUS              Status;
69   VOID*                   PciAllocMapping = NULL;
70   EFI_PCI_IO_PROTOCOL     *PciIo;
71 
72   PciIo = SataSiI3132Instance->PciIo;
73   ZeroMem (SataPort->HostPRB, sizeof (SATA_SI3132_PRB));
74 
75   // Construct Si3132 PRB
76   switch (Packet->Protocol) {
77   case EFI_ATA_PASS_THRU_PROTOCOL_ATA_HARDWARE_RESET:
78     ASSERT (0); //TODO: Implement me!
79     break;
80   case EFI_ATA_PASS_THRU_PROTOCOL_ATA_SOFTWARE_RESET:
81     SATA_TRACE ("SiI3132AtaPassThru() EFI_ATA_PASS_THRU_PROTOCOL_ATA_SOFTWARE_RESET");
82     Control = PRB_CTRL_SRST;
83 
84     if (FeaturePcdGet (PcdSataSiI3132FeaturePMPSupport)) {
85         SataPort->HostPRB->Fis.Control = 0x0F;
86     }
87     break;
88   case EFI_ATA_PASS_THRU_PROTOCOL_ATA_NON_DATA:
89     ASSERT (0); //TODO: Implement me!
90     break;
91 
92   // There is no difference for SiI3132 between PIO and DMA invokation
93   case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_IN:
94   case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN:
95     // Fixup the size for block transfer. Following UEFI Specification, 'InTransferLength' should
96     // be in number of bytes. But for most data transfer commands, the value is in number of blocks
97     if (Packet->Acb->AtaCommand == ATA_CMD_IDENTIFY_DRIVE) {
98       InDataBufferLength = Packet->InTransferLength;
99     } else {
100       SataDevice = GetSataDevice (SataSiI3132Instance, SataPort->Index, PortMultiplierPort);
101       if (!SataDevice || (SataDevice->BlockSize == 0)) {
102         return EFI_INVALID_PARAMETER;
103       }
104 
105       InDataBufferLength = Packet->InTransferLength * SataDevice->BlockSize;
106     }
107 
108     Status = PciIo->Map (
109                PciIo, EfiPciIoOperationBusMasterWrite,
110                Packet->InDataBuffer, &InDataBufferLength, &PhysInDataBuffer, &PciAllocMapping
111                );
112     if (EFI_ERROR (Status)) {
113       return Status;
114     }
115 
116     // Construct SGEs (32-bit system)
117     SataPort->HostPRB->Sge[0].DataAddressLow = (UINT32)PhysInDataBuffer;
118     SataPort->HostPRB->Sge[0].DataAddressHigh = (UINT32)(PhysInDataBuffer >> 32);
119     SataPort->HostPRB->Sge[0].Attributes = SGE_TRM; // Only one SGE
120     SataPort->HostPRB->Sge[0].DataCount = InDataBufferLength;
121 
122     // Copy the Ata Command Block
123     CopyMem (&SataPort->HostPRB->Fis, Packet->Acb, sizeof (EFI_ATA_COMMAND_BLOCK));
124 
125     // Fixup the FIS
126     SataPort->HostPRB->Fis.FisType = 0x27; // Register - Host to Device FIS
127     SataPort->HostPRB->Fis.Control = 1 << 7; // Is a command
128     if (FeaturePcdGet (PcdSataSiI3132FeaturePMPSupport)) {
129       SataPort->HostPRB->Fis.Control |= PortMultiplierPort & 0xFF;
130     }
131     break;
132   case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_OUT:
133   case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT:
134     SataDevice = GetSataDevice (SataSiI3132Instance, SataPort->Index, PortMultiplierPort);
135     if (!SataDevice || (SataDevice->BlockSize == 0)) {
136       return EFI_INVALID_PARAMETER;
137     }
138 
139     // Fixup the size for block transfer. Following UEFI Specification, 'InTransferLength' should
140     // be in number of bytes. But for most data transfer commands, the value is in number of blocks
141     OutDataBufferLength = Packet->OutTransferLength * SataDevice->BlockSize;
142 
143     Status = PciIo->Map (
144                PciIo, EfiPciIoOperationBusMasterRead,
145                Packet->OutDataBuffer, &OutDataBufferLength, &PhysOutDataBuffer, &PciAllocMapping
146                );
147     if (EFI_ERROR (Status)) {
148       return Status;
149     }
150 
151     // Construct SGEs (32-bit system)
152     SataPort->HostPRB->Sge[0].DataAddressLow  = (UINT32)PhysOutDataBuffer;
153     SataPort->HostPRB->Sge[0].DataAddressHigh = (UINT32)(PhysOutDataBuffer >> 32);
154     SataPort->HostPRB->Sge[0].Attributes      = SGE_TRM; // Only one SGE
155     SataPort->HostPRB->Sge[0].DataCount       = OutDataBufferLength;
156 
157     // Copy the Ata Command Block
158     CopyMem (&SataPort->HostPRB->Fis, Packet->Acb, sizeof (EFI_ATA_COMMAND_BLOCK));
159 
160     // Fixup the FIS
161     SataPort->HostPRB->Fis.FisType = 0x27; // Register - Host to Device FIS
162     SataPort->HostPRB->Fis.Control = 1 << 7; // Is a command
163     if (FeaturePcdGet (PcdSataSiI3132FeaturePMPSupport)) {
164       SataPort->HostPRB->Fis.Control |= PortMultiplierPort & 0xFF;
165     }
166     break;
167   case EFI_ATA_PASS_THRU_PROTOCOL_DMA:
168     ASSERT (0); //TODO: Implement me!
169     break;
170   case EFI_ATA_PASS_THRU_PROTOCOL_DMA_QUEUED:
171     ASSERT (0); //TODO: Implement me!
172     break;
173   case EFI_ATA_PASS_THRU_PROTOCOL_DEVICE_DIAGNOSTIC:
174     ASSERT (0); //TODO: Implement me!
175     break;
176   case EFI_ATA_PASS_THRU_PROTOCOL_DEVICE_RESET:
177     ASSERT (0); //TODO: Implement me!
178     break;
179   case EFI_ATA_PASS_THRU_PROTOCOL_FPDMA:
180     ASSERT (0); //TODO: Implement me!
181     break;
182   case EFI_ATA_PASS_THRU_PROTOCOL_RETURN_RESPONSE:
183     ASSERT (0); //TODO: Implement me!
184     break;
185   default:
186     ASSERT (0);
187     break;
188   }
189 
190   SataPort->HostPRB->Control = Control;
191   SataPort->HostPRB->ProtocolOverride = Protocol;
192 
193   // Clear IRQ
194   SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_INTSTATUS_REG, IrqMask);
195 
196   if (!FeaturePcdGet (PcdSataSiI3132FeatureDirectCommandIssuing)) {
197     // Indirect Command Issuance
198 
199     //TODO: Find which slot is free (maybe use the Cmd FIFO)
200     //SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_CMDEXECFIFO_REG, &EmptySlot);
201 
202     SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_CMDACTIV_REG + (EmptySlot * 8),
203                      (UINT32)(SataPort->PhysAddrHostPRB & 0xFFFFFFFF));
204     SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_CMDACTIV_REG + (EmptySlot * 8) + 4,
205                      (UINT32)((SataPort->PhysAddrHostPRB >> 32) & 0xFFFFFFFF));
206   } else {
207     // Direct Command Issuance
208     Status = PciIo->Mem.Write (PciIo, EfiPciIoWidthUint32, 1, // Bar 1
209         SataPort->RegBase + (EmptySlot * 0x80),
210         sizeof (SATA_SI3132_PRB) / 4,
211         SataPort->HostPRB);
212     ASSERT_EFI_ERROR (Status);
213 
214     SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_CMDEXECFIFO_REG, EmptySlot);
215   }
216 
217 #if 0
218   // Could need to be implemented if we run multiple command in parallel to know which slot has been completed
219   SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_SLOTSTATUS_REG, &Value32);
220   Timeout = Packet->Timeout;
221   while (!Timeout && !Value32) {
222     gBS->Stall (1);
223     SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_SLOTSTATUS_REG, &Value32);
224     Timeout--;
225   }
226 #else
227   SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_INTSTATUS_REG, &Value32);
228   if (!Packet->Timeout) {
229     while (!(Value32 & IrqMask)) {
230       gBS->Stall (1);
231       SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_INTSTATUS_REG, &Value32);
232     }
233   } else {
234     Timeout = Packet->Timeout;
235     while (Timeout && !(Value32 & IrqMask)) {
236       gBS->Stall (1);
237       SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_INTSTATUS_REG, &Value32);
238       Timeout--;
239     }
240   }
241 #endif
242   // Fill Packet Ata Status Block
243   Status = PciIo->Mem.Read (PciIo, EfiPciIoWidthUint32, 1, // Bar 1
244       SataPort->RegBase + 0x08,
245       sizeof (EFI_ATA_STATUS_BLOCK) / 4,
246       Packet->Asb);
247   ASSERT_EFI_ERROR (Status);
248 
249 
250   if ((Packet->Timeout != 0) && (Timeout == 0)) {
251     DEBUG ((EFI_D_ERROR, "SiI3132AtaPassThru() Err:Timeout\n"));
252     //ASSERT (0);
253     return EFI_TIMEOUT;
254   } else if (Value32 & (SII3132_PORT_INT_CMDERR << 16)) {
255     SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_CMDERROR_REG, &Error);
256     DEBUG ((EFI_D_ERROR, "SiI3132AtaPassThru() CmdErr:0x%X (SiI3132 Err:0x%X)\n", Value32, Error));
257     ASSERT (0);
258     return EFI_DEVICE_ERROR;
259   } else if (Value32 & (SII3132_PORT_INT_CMDCOMPL << 16)) {
260     // Clear Command Complete
261     SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_INTSTATUS_REG, SII3132_PORT_INT_CMDCOMPL << 16);
262 
263     if (PciAllocMapping) {
264       Status = PciIo->Unmap (PciIo, PciAllocMapping);
265       ASSERT (!EFI_ERROR (Status));
266     }
267 
268     // If the command was ATA_CMD_IDENTIFY_DRIVE then we need to update the BlockSize
269     if (Packet->Acb->AtaCommand == ATA_CMD_IDENTIFY_DRIVE) {
270       ATA_IDENTIFY_DATA *IdentifyData = (ATA_IDENTIFY_DATA*)Packet->InDataBuffer;
271 
272       // Get the corresponding Block Device
273       SataDevice = GetSataDevice (SataSiI3132Instance, SataPort->Index, PortMultiplierPort);
274 
275       // Check logical block size
276       if ((IdentifyData->phy_logic_sector_support & BIT12) != 0) {
277         ASSERT (SataDevice != NULL);
278         SataDevice->BlockSize = (UINT32) (((IdentifyData->logic_sector_size_hi << 16) |
279                                             IdentifyData->logic_sector_size_lo) * sizeof (UINT16));
280       } else {
281         SataDevice->BlockSize = 0x200;
282       }
283     }
284     return EFI_SUCCESS;
285   } else {
286     ASSERT (0);
287     return EFI_DEVICE_ERROR;
288   }
289 }
290 
291 /**
292   Sends an ATA command to an ATA device that is attached to the ATA controller. This function
293   supports both blocking I/O and non-blocking I/O. The blocking I/O functionality is required,
294   and the non-blocking I/O functionality is optional.
295 
296   @param[in]     This                A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
297   @param[in]     Port                The port number of the ATA device to send the command.
298   @param[in]     PortMultiplierPort  The port multiplier port number of the ATA device to send the command.
299                                      If there is no port multiplier, then specify 0.
300   @param[in,out] Packet              A pointer to the ATA command to send to the ATA device specified by Port
301                                      and PortMultiplierPort.
302   @param[in]     Event               If non-blocking I/O is not supported then Event is ignored, and blocking
303                                      I/O is performed. If Event is NULL, then blocking I/O is performed. If
304                                      Event is not NULL and non blocking I/O is supported, then non-blocking
305                                      I/O is performed, and Event will be signaled when the ATA command completes.
306 
307   @retval EFI_SUCCESS                The ATA command was sent by the host. For bi-directional commands,
308                                      InTransferLength bytes were transferred from InDataBuffer. For write and
309                                      bi-directional commands, OutTransferLength bytes were transferred by OutDataBuffer.
310   @retval EFI_BAD_BUFFER_SIZE        The ATA command was not executed. The number of bytes that could be transferred
311                                      is returned in InTransferLength. For write and bi-directional commands,
312                                      OutTransferLength bytes were transferred by OutDataBuffer.
313   @retval EFI_NOT_READY              The ATA command could not be sent because there are too many ATA commands
314                                      already queued. The caller may retry again later.
315   @retval EFI_DEVICE_ERROR           A device error occurred while attempting to send the ATA command.
316   @retval EFI_INVALID_PARAMETER      Port, PortMultiplierPort, or the contents of Acb are invalid. The ATA
317                                      command was not sent, so no additional status information is available.
318 
319 **/
320 EFI_STATUS
321 EFIAPI
SiI3132AtaPassThru(IN EFI_ATA_PASS_THRU_PROTOCOL * This,IN UINT16 Port,IN UINT16 PortMultiplierPort,IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET * Packet,IN EFI_EVENT Event OPTIONAL)322 SiI3132AtaPassThru (
323   IN     EFI_ATA_PASS_THRU_PROTOCOL       *This,
324   IN     UINT16                           Port,
325   IN     UINT16                           PortMultiplierPort,
326   IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet,
327   IN     EFI_EVENT                        Event OPTIONAL
328   )
329 {
330   SATA_SI3132_INSTANCE    *SataSiI3132Instance;
331   SATA_SI3132_DEVICE      *SataDevice;
332   SATA_SI3132_PORT        *SataPort;
333 
334   SataSiI3132Instance = INSTANCE_FROM_ATAPASSTHRU_THIS (This);
335   if (!SataSiI3132Instance) {
336     return EFI_INVALID_PARAMETER;
337   }
338 
339   SataDevice = GetSataDevice (SataSiI3132Instance, Port, PortMultiplierPort);
340   if (!SataDevice) {
341     return EFI_INVALID_PARAMETER;
342   }
343   SataPort = SataDevice->Port;
344 
345   DEBUG ((EFI_D_INFO, "SiI3132AtaPassThru(%d,%d) : AtaCmd:0x%X Prot:%d\n", Port, PortMultiplierPort,
346          Packet->Acb->AtaCommand, Packet->Protocol));
347 
348   return SiI3132AtaPassThruCommand (SataSiI3132Instance, SataPort, PortMultiplierPort, Packet, Event);
349 }
350 
351 /**
352   Used to retrieve the list of legal port numbers for ATA devices on an ATA controller.
353   These can either be the list of ports where ATA devices are actually present or the
354   list of legal port numbers for the ATA controller. Regardless, the caller of this
355   function must probe the port number returned to see if an ATA device is actually
356   present at that location on the ATA controller.
357 
358   The GetNextPort() function retrieves the port number on an ATA controller. If on input
359   Port is 0xFFFF, then the port number of the first port on the ATA controller is returned
360   in Port and EFI_SUCCESS is returned.
361 
362   If Port is a port number that was returned on a previous call to GetNextPort(), then the
363   port number of the next port on the ATA controller is returned in Port, and EFI_SUCCESS
364   is returned. If Port is not 0xFFFF and Port was not returned on a previous call to
365   GetNextPort(), then EFI_INVALID_PARAMETER is returned.
366 
367   If Port is the port number of the last port on the ATA controller, then EFI_NOT_FOUND is
368   returned.
369 
370   @param[in]     This           A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
371   @param[in,out] Port           On input, a pointer to the port number on the ATA controller.
372                                 On output, a pointer to the next port number on the ATA
373                                 controller. An input value of 0xFFFF retrieves the first port
374                                 number on the ATA controller.
375 
376   @retval EFI_SUCCESS           The next port number on the ATA controller was returned in Port.
377   @retval EFI_NOT_FOUND         There are no more ports on this ATA controller.
378   @retval EFI_INVALID_PARAMETER Port is not 0xFFFF and Port was not returned on a previous call
379                                 to GetNextPort().
380 
381 **/
382 EFI_STATUS
383 EFIAPI
SiI3132GetNextPort(IN EFI_ATA_PASS_THRU_PROTOCOL * This,IN OUT UINT16 * Port)384 SiI3132GetNextPort (
385   IN EFI_ATA_PASS_THRU_PROTOCOL *This,
386   IN OUT UINT16                 *Port
387   )
388 {
389   SATA_SI3132_INSTANCE    *SataSiI3132Instance;
390   UINTN                   PrevPort;
391   EFI_STATUS              Status = EFI_SUCCESS;
392 
393   SataSiI3132Instance = INSTANCE_FROM_ATAPASSTHRU_THIS (This);
394   if (!SataSiI3132Instance) {
395     return EFI_INVALID_PARAMETER;
396   }
397 
398   PrevPort = *Port;
399 
400   if (PrevPort == 0xFFFF) {
401     *Port = 0;
402   } else {
403     if (PrevPort < SATA_SII3132_MAXPORT) {
404         *Port = PrevPort + 1;
405     } else {
406         Status = EFI_NOT_FOUND;
407     }
408   }
409   return Status;
410 }
411 
412 /**
413   Used to retrieve the list of legal port multiplier port numbers for ATA devices on a port of an ATA
414   controller. These can either be the list of port multiplier ports where ATA devices are actually
415   present on port or the list of legal port multiplier ports on that port. Regardless, the caller of this
416   function must probe the port number and port multiplier port number returned to see if an ATA
417   device is actually present.
418 
419   The GetNextDevice() function retrieves the port multiplier port number of an ATA device
420   present on a port of an ATA controller.
421 
422   If PortMultiplierPort points to a port multiplier port number value that was returned on a
423   previous call to GetNextDevice(), then the port multiplier port number of the next ATA device
424   on the port of the ATA controller is returned in PortMultiplierPort, and EFI_SUCCESS is
425   returned.
426 
427   If PortMultiplierPort points to 0xFFFF, then the port multiplier port number of the first
428   ATA device on port of the ATA controller is returned in PortMultiplierPort and
429   EFI_SUCCESS is returned.
430 
431   If PortMultiplierPort is not 0xFFFF and the value pointed to by PortMultiplierPort
432   was not returned on a previous call to GetNextDevice(), then EFI_INVALID_PARAMETER
433   is returned.
434 
435   If PortMultiplierPort is the port multiplier port number of the last ATA device on the port of
436   the ATA controller, then EFI_NOT_FOUND is returned.
437 
438   @param[in]     This                A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
439   @param[in]     Port                The port number present on the ATA controller.
440   @param[in,out] PortMultiplierPort  On input, a pointer to the port multiplier port number of an
441                                      ATA device present on the ATA controller.
442                                      If on input a PortMultiplierPort of 0xFFFF is specified,
443                                      then the port multiplier port number of the first ATA device
444                                      is returned. On output, a pointer to the port multiplier port
445                                      number of the next ATA device present on an ATA controller.
446 
447   @retval EFI_SUCCESS                The port multiplier port number of the next ATA device on the port
448                                      of the ATA controller was returned in PortMultiplierPort.
449   @retval EFI_NOT_FOUND              There are no more ATA devices on this port of the ATA controller.
450   @retval EFI_INVALID_PARAMETER      PortMultiplierPort is not 0xFFFF, and PortMultiplierPort was not
451                                      returned on a previous call to GetNextDevice().
452 
453 **/
454 EFI_STATUS
455 EFIAPI
SiI3132GetNextDevice(IN EFI_ATA_PASS_THRU_PROTOCOL * This,IN UINT16 Port,IN OUT UINT16 * PortMultiplierPort)456 SiI3132GetNextDevice (
457   IN EFI_ATA_PASS_THRU_PROTOCOL *This,
458   IN UINT16                     Port,
459   IN OUT UINT16                 *PortMultiplierPort
460   )
461 {
462   SATA_SI3132_INSTANCE    *SataSiI3132Instance;
463   SATA_SI3132_PORT        *SataPort;
464   SATA_SI3132_DEVICE      *SataDevice;
465   LIST_ENTRY              *List;
466   EFI_STATUS              Status = EFI_SUCCESS;
467 
468   SataSiI3132Instance = INSTANCE_FROM_ATAPASSTHRU_THIS (This);
469   if (!SataSiI3132Instance) {
470     return EFI_INVALID_PARAMETER;
471   }
472 
473   if (Port >= SATA_SII3132_MAXPORT) {
474     return EFI_INVALID_PARAMETER;
475   }
476 
477   SataPort = &(SataSiI3132Instance->Ports[Port]);
478 
479   if (*PortMultiplierPort == 0xFFFF) {
480     List = SataPort->Devices.ForwardLink;
481     if (List != &SataPort->Devices) {
482       // The list is not empty, return the first device
483       *PortMultiplierPort = ((SATA_SI3132_DEVICE*)List)->Index;
484     } else {
485       Status = EFI_NOT_FOUND;
486     }
487   } else {
488     SataDevice = GetSataDevice (SataSiI3132Instance, Port, *PortMultiplierPort);
489     if (SataDevice != NULL) {
490       // We have found the previous port multiplier, return the next one
491       List = SataDevice->Link.ForwardLink;
492       if (List != &SataPort->Devices) {
493         *PortMultiplierPort = ((SATA_SI3132_DEVICE*)List)->Index;
494       } else {
495         Status = EFI_NOT_FOUND;
496       }
497     } else {
498       Status = EFI_NOT_FOUND;
499     }
500   }
501   return Status;
502 }
503 
504 /**
505   Used to allocate and build a device path node for an ATA device on an ATA controller.
506 
507   The BuildDevicePath() function allocates and builds a single device node for the ATA
508   device specified by Port and PortMultiplierPort. If the ATA device specified by Port and
509   PortMultiplierPort is not present on the ATA controller, then EFI_NOT_FOUND is returned.
510   If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned. If there are not enough
511   resources to allocate the device path node, then EFI_OUT_OF_RESOURCES is returned.
512 
513   Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of
514   DevicePath are initialized to describe the ATA device specified by Port and PortMultiplierPort,
515   and EFI_SUCCESS is returned.
516 
517   @param[in]     This                A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
518   @param[in]     Port                Port specifies the port number of the ATA device for which a
519                                      device path node is to be allocated and built.
520   @param[in]     PortMultiplierPort  The port multiplier port number of the ATA device for which a
521                                      device path node is to be allocated and built. If there is no
522                                      port multiplier, then specify 0.
523   @param[in,out] DevicePath          A pointer to a single device path node that describes the ATA
524                                      device specified by Port and PortMultiplierPort. This function
525                                      is responsible for allocating the buffer DevicePath with the
526                                      boot service AllocatePool(). It is the caller's responsibility
527                                      to free DevicePath when the caller is finished with DevicePath.
528   @retval EFI_SUCCESS                The device path node that describes the ATA device specified by
529                                      Port and PortMultiplierPort was allocated and returned in DevicePath.
530   @retval EFI_NOT_FOUND              The ATA device specified by Port and PortMultiplierPort does not
531                                      exist on the ATA controller.
532   @retval EFI_INVALID_PARAMETER      DevicePath is NULL.
533   @retval EFI_OUT_OF_RESOURCES       There are not enough resources to allocate DevicePath.
534 
535 **/
536 EFI_STATUS
537 EFIAPI
SiI3132BuildDevicePath(IN EFI_ATA_PASS_THRU_PROTOCOL * This,IN UINT16 Port,IN UINT16 PortMultiplierPort,IN OUT EFI_DEVICE_PATH_PROTOCOL ** DevicePath)538 SiI3132BuildDevicePath (
539   IN     EFI_ATA_PASS_THRU_PROTOCOL *This,
540   IN     UINT16                     Port,
541   IN     UINT16                     PortMultiplierPort,
542   IN OUT EFI_DEVICE_PATH_PROTOCOL   **DevicePath
543   )
544 {
545   SATA_SI3132_INSTANCE        *SataSiI3132Instance;
546   SATA_SI3132_DEVICE          *SataDevice;
547   EFI_DEVICE_PATH_PROTOCOL    *SiI3132DevicePath;
548 
549   SATA_TRACE ("SiI3132BuildDevicePath()");
550 
551   SataSiI3132Instance = INSTANCE_FROM_ATAPASSTHRU_THIS (This);
552   if (!SataSiI3132Instance) {
553     return EFI_INVALID_PARAMETER;
554   }
555 
556   SataDevice = GetSataDevice (SataSiI3132Instance, Port, PortMultiplierPort);
557   if (SataDevice == NULL) {
558     return EFI_NOT_FOUND;
559   }
560 
561   SiI3132DevicePath = CreateDeviceNode (MESSAGING_DEVICE_PATH, MSG_SATA_DP, sizeof (SATA_DEVICE_PATH));
562   if (SiI3132DevicePath == NULL) {
563     return EFI_OUT_OF_RESOURCES;
564   }
565 
566   ((SATA_DEVICE_PATH*)SiI3132DevicePath)->HBAPortNumber = Port;
567   if (FeaturePcdGet (PcdSataSiI3132FeaturePMPSupport)) {
568     ((SATA_DEVICE_PATH*)SiI3132DevicePath)->PortMultiplierPortNumber = PortMultiplierPort;
569   } else {
570     //Temp:((SATA_DEVICE_PATH*)SiI3132DevicePath)->PortMultiplierPortNumber = SATA_HBA_DIRECT_CONNECT_FLAG;
571     ((SATA_DEVICE_PATH*)SiI3132DevicePath)->PortMultiplierPortNumber = 0;
572   }
573   ((SATA_DEVICE_PATH*)SiI3132DevicePath)->Lun = Port; //TODO: Search information how to define properly LUN (Logical Unit Number)
574 
575   *DevicePath = SiI3132DevicePath;
576   return EFI_SUCCESS;
577 }
578 
579 /**
580   Used to translate a device path node to a port number and port multiplier port number.
581 
582   The GetDevice() function determines the port and port multiplier port number associated with
583   the ATA device described by DevicePath. If DevicePath is a device path node type that the
584   ATA Pass Thru driver supports, then the ATA Pass Thru driver will attempt to translate the contents
585   DevicePath into a port number and port multiplier port number.
586 
587   If this translation is successful, then that port number and port multiplier port number are returned
588   in Port and PortMultiplierPort, and EFI_SUCCESS is returned.
589 
590   If DevicePath, Port, or PortMultiplierPort are NULL, then EFI_INVALID_PARAMETER is returned.
591 
592   If DevicePath is not a device path node type that the ATA Pass Thru driver supports, then
593   EFI_UNSUPPORTED is returned.
594 
595   If DevicePath is a device path node type that the ATA Pass Thru driver supports, but there is not
596   a valid translation from DevicePath to a port number and port multiplier port number, then
597   EFI_NOT_FOUND is returned.
598 
599   @param[in]  This                A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
600   @param[in]  DevicePath          A pointer to the device path node that describes an ATA device on the
601                                   ATA controller.
602   @param[out] Port                On return, points to the port number of an ATA device on the ATA controller.
603   @param[out] PortMultiplierPort  On return, points to the port multiplier port number of an ATA device
604                                   on the ATA controller.
605 
606   @retval EFI_SUCCESS             DevicePath was successfully translated to a port number and port multiplier
607                                   port number, and they were returned in Port and PortMultiplierPort.
608   @retval EFI_INVALID_PARAMETER   DevicePath is NULL.
609   @retval EFI_INVALID_PARAMETER   Port is NULL.
610   @retval EFI_INVALID_PARAMETER   PortMultiplierPort is NULL.
611   @retval EFI_UNSUPPORTED         This driver does not support the device path node type in DevicePath.
612   @retval EFI_NOT_FOUND           A valid translation from DevicePath to a port number and port multiplier
613                                   port number does not exist.
614 **/
615 EFI_STATUS
616 EFIAPI
SiI3132GetDevice(IN EFI_ATA_PASS_THRU_PROTOCOL * This,IN EFI_DEVICE_PATH_PROTOCOL * DevicePath,OUT UINT16 * Port,OUT UINT16 * PortMultiplierPort)617 SiI3132GetDevice (
618   IN  EFI_ATA_PASS_THRU_PROTOCOL *This,
619   IN  EFI_DEVICE_PATH_PROTOCOL   *DevicePath,
620   OUT UINT16                     *Port,
621   OUT UINT16                     *PortMultiplierPort
622   )
623 {
624   SATA_SI3132_INSTANCE        *SataSiI3132Instance;
625 
626   SATA_TRACE ("SiI3132GetDevice()");
627 
628   if (!DevicePath || !Port || !PortMultiplierPort) {
629     return EFI_INVALID_PARAMETER;
630   }
631 
632   if ((DevicePath->Type != MESSAGING_DEVICE_PATH) || (DevicePath->SubType != MSG_SATA_DP)) {
633     return EFI_UNSUPPORTED;
634   }
635 
636   SataSiI3132Instance = INSTANCE_FROM_ATAPASSTHRU_THIS (This);
637   if (!SataSiI3132Instance) {
638     return EFI_INVALID_PARAMETER;
639   }
640 
641   if (((SATA_DEVICE_PATH*)DevicePath)->Lun >= SATA_SII3132_MAXPORT) {
642     return EFI_NOT_FOUND;
643   }
644 
645   if (FeaturePcdGet (PcdSataSiI3132FeaturePMPSupport)) {
646     ASSERT (0); //TODO: Implement me!
647     return EFI_UNSUPPORTED;
648   } else {
649     *Port = ((SATA_DEVICE_PATH*)DevicePath)->Lun;
650     // Return the first Sata Device as there should be only one directly connected
651     *PortMultiplierPort = ((SATA_SI3132_DEVICE*)SataSiI3132Instance->Ports[*Port].Devices.ForwardLink)->Index;
652     return EFI_SUCCESS;
653   }
654 }
655 
656 EFI_STATUS
SiI3132HwResetPort(IN SATA_SI3132_PORT * SataPort)657 SiI3132HwResetPort (
658   IN SATA_SI3132_PORT *SataPort
659   )
660 {
661   EFI_PCI_IO_PROTOCOL *PciIo;
662   UINT32              Value32;
663   UINTN               Timeout;
664 
665   SATA_TRACE ("SiI3132HwResetPort()");
666 
667   PciIo = SataPort->Instance->PciIo;
668 
669   // Clear Port Reset
670   SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_CONTROLCLEAR_REG, SII3132_PORT_CONTROL_RESET);
671 
672   SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_STATUS_REG, &Value32);
673   ASSERT (!(Value32 & SII3132_PORT_CONTROL_RESET));
674 
675   // Initialize error counters
676   SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_ERRCOUNTDECODE, 0);
677   SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_ERRCOUNTCRC, 0);
678   SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_ERRCOUNTHANDSHAKE, 0);
679 
680   // Enable interrupts for command completion and command errors
681   SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_ENABLEINT_REG, SII3132_PORT_INT_CMDCOMPL | SII3132_PORT_INT_CMDERR);
682 
683   // Clear IRQ
684   SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_ENABLEINT_REG, SII3132_PORT_INT_CMDCOMPL | SII3132_PORT_INT_CMDERR | SII3132_PORT_INT_PORTRDY | (1 << 3));
685 
686   // Wait until Port Ready
687   SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_INTSTATUS_REG, &Value32);
688   Timeout = 1000;
689   while ((Timeout > 0) && ((Value32 & SII3132_PORT_INT_PORTRDY) == 0)) {
690     gBS->Stall (1);
691     Timeout--;
692     SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_INTSTATUS_REG, &Value32);
693   }
694   // Clear IRQ
695   SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_INTSTATUS_REG, SII3132_PORT_INT_PORTRDY);
696 
697   if (Timeout == 0) {
698     SATA_TRACE ("SiI3132HwResetPort(): Timeout");
699     return EFI_TIMEOUT;
700   } else if ((Value32 & SII3132_PORT_INT_PORTRDY) == 0) {
701     SATA_TRACE ("SiI3132HwResetPort(): Port Not Ready");
702     return EFI_DEVICE_ERROR;
703   } else {
704     return EFI_SUCCESS;
705   }
706 }
707 
708 /**
709   Resets a specific port on the ATA controller. This operation also resets all the ATA devices
710   connected to the port.
711 
712   The ResetChannel() function resets an a specific port on an ATA controller. This operation
713   resets all the ATA devices connected to that port. If this ATA controller does not support
714   a reset port operation, then EFI_UNSUPPORTED is returned.
715 
716   If a device error occurs while executing that port reset operation, then EFI_DEVICE_ERROR is
717   returned.
718 
719   If a timeout occurs during the execution of the port reset operation, then EFI_TIMEOUT is returned.
720 
721   If the port reset operation is completed, then EFI_SUCCESS is returned.
722 
723   @param[in]  This          A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
724   @param[in]  Port          The port number on the ATA controller.
725 
726   @retval EFI_SUCCESS       The ATA controller port was reset.
727   @retval EFI_UNSUPPORTED   The ATA controller does not support a port reset operation.
728   @retval EFI_DEVICE_ERROR  A device error occurred while attempting to reset the ATA port.
729   @retval EFI_TIMEOUT       A timeout occurred while attempting to reset the ATA port.
730 
731 **/
732 EFI_STATUS
733 EFIAPI
SiI3132ResetPort(IN EFI_ATA_PASS_THRU_PROTOCOL * This,IN UINT16 Port)734 SiI3132ResetPort (
735   IN EFI_ATA_PASS_THRU_PROTOCOL *This,
736   IN UINT16                     Port
737   )
738 {
739   SATA_SI3132_INSTANCE    *SataSiI3132Instance;
740   SATA_SI3132_PORT        *SataPort;
741 
742   SATA_TRACE ("SiI3132ResetPort()");
743 
744   SataSiI3132Instance = INSTANCE_FROM_ATAPASSTHRU_THIS (This);
745   if (!SataSiI3132Instance) {
746     return EFI_INVALID_PARAMETER;
747   }
748 
749   if (Port >= SATA_SII3132_MAXPORT) {
750     return EFI_UNSUPPORTED;
751   }
752 
753   SataPort = &(SataSiI3132Instance->Ports[Port]);
754   return SiI3132HwResetPort (SataPort);
755 }
756 
757 /**
758   Resets an ATA device that is connected to an ATA controller.
759 
760   The ResetDevice() function resets the ATA device specified by Port and PortMultiplierPort.
761   If this ATA controller does not support a device reset operation, then EFI_UNSUPPORTED is
762   returned.
763 
764   If Port or PortMultiplierPort are not in a valid range for this ATA controller, then
765   EFI_INVALID_PARAMETER is returned.
766 
767   If a device error occurs while executing that device reset operation, then EFI_DEVICE_ERROR
768   is returned.
769 
770   If a timeout occurs during the execution of the device reset operation, then EFI_TIMEOUT is
771   returned.
772 
773   If the device reset operation is completed, then EFI_SUCCESS is returned.
774 
775   @param[in] This                A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
776   @param[in] Port                Port represents the port number of the ATA device to be reset.
777   @param[in] PortMultiplierPort  The port multiplier port number of the ATA device to reset.
778                                  If there is no port multiplier, then specify 0.
779   @retval EFI_SUCCESS            The ATA device specified by Port and PortMultiplierPort was reset.
780   @retval EFI_UNSUPPORTED        The ATA controller does not support a device reset operation.
781   @retval EFI_INVALID_PARAMETER  Port or PortMultiplierPort are invalid.
782   @retval EFI_DEVICE_ERROR       A device error occurred while attempting to reset the ATA device
783                                  specified by Port and PortMultiplierPort.
784   @retval EFI_TIMEOUT            A timeout occurred while attempting to reset the ATA device
785                                  specified by Port and PortMultiplierPort.
786 
787 **/
788 EFI_STATUS
789 EFIAPI
SiI3132ResetDevice(IN EFI_ATA_PASS_THRU_PROTOCOL * This,IN UINT16 Port,IN UINT16 PortMultiplierPort)790 SiI3132ResetDevice (
791   IN EFI_ATA_PASS_THRU_PROTOCOL *This,
792   IN UINT16                     Port,
793   IN UINT16                     PortMultiplierPort
794   )
795 {
796   EFI_PCI_IO_PROTOCOL     *PciIo;
797   SATA_SI3132_INSTANCE    *SataSiI3132Instance;
798   SATA_SI3132_PORT        *SataPort;
799   SATA_SI3132_DEVICE      *SataDevice;
800   UINTN                   Timeout;
801   UINT32                  Value32;
802 
803   SATA_TRACE ("SiI3132ResetDevice()");
804 
805   SataSiI3132Instance = INSTANCE_FROM_ATAPASSTHRU_THIS (This);
806   if (!SataSiI3132Instance) {
807     return EFI_INVALID_PARAMETER;
808   }
809 
810   PciIo = SataSiI3132Instance->PciIo;
811 
812   SataDevice = GetSataDevice (SataSiI3132Instance, Port, PortMultiplierPort);
813   if (!SataDevice) {
814     return EFI_INVALID_PARAMETER;
815   }
816   SataPort = SataDevice->Port;
817 
818   SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_CONTROLSET_REG, SII3132_PORT_DEVICE_RESET);
819 
820   Timeout = 100;
821   SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_STATUS_REG, &Value32);
822   while ((Timeout > 0) && ((Value32 & SII3132_PORT_DEVICE_RESET) != 0)) {
823     gBS->Stall (1);
824     SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_STATUS_REG, &Value32);
825     Timeout--;
826   }
827 
828   if (Timeout == 0) {
829     SATA_TRACE ("SiI3132ResetDevice(): Timeout");
830     return EFI_TIMEOUT;
831   } else {
832     return EFI_SUCCESS;
833   }
834 }
835