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