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