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