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