]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/SataControllerDxe/SataController.c
OvmfPkg/AmdSevDxe: refresh #includes and LibraryClasses
[mirror_edk2.git] / OvmfPkg / SataControllerDxe / SataController.c
CommitLineData
12e92a23
RJ
1/** @file\r
2 This driver module produces IDE_CONTROLLER_INIT protocol for Sata Controllers.\r
3\r
4 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\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 "SataController.h"\r
16\r
17///\r
18/// EFI_DRIVER_BINDING_PROTOCOL instance\r
19///\r
20EFI_DRIVER_BINDING_PROTOCOL gSataControllerDriverBinding = {\r
21 SataControllerSupported,\r
22 SataControllerStart,\r
23 SataControllerStop,\r
24 0xa,\r
25 NULL,\r
26 NULL\r
27};\r
28\r
29/**\r
30 Read AHCI Operation register.\r
31\r
32 @param PciIo The PCI IO protocol instance.\r
33 @param Offset The operation register offset.\r
34\r
35 @return The register content read.\r
36\r
37**/\r
38UINT32\r
39EFIAPI\r
40AhciReadReg (\r
41 IN EFI_PCI_IO_PROTOCOL *PciIo,\r
42 IN UINT32 Offset\r
43 )\r
44{\r
45 UINT32 Data;\r
46\r
47 ASSERT (PciIo != NULL);\r
48 \r
49 Data = 0;\r
50\r
51 PciIo->Mem.Read (\r
52 PciIo,\r
53 EfiPciIoWidthUint32,\r
54 AHCI_BAR_INDEX,\r
55 (UINT64) Offset,\r
56 1,\r
57 &Data\r
58 );\r
59\r
60 return Data;\r
61}\r
62\r
63/**\r
64 Write AHCI Operation register.\r
65\r
66 @param PciIo The PCI IO protocol instance.\r
67 @param Offset The operation register offset.\r
68 @param Data The data used to write down.\r
69\r
70**/\r
71VOID\r
72EFIAPI\r
73AhciWriteReg (\r
74 IN EFI_PCI_IO_PROTOCOL *PciIo,\r
75 IN UINT32 Offset,\r
76 IN UINT32 Data\r
77 )\r
78{\r
79 ASSERT (PciIo != NULL);\r
80\r
81 PciIo->Mem.Write (\r
82 PciIo,\r
83 EfiPciIoWidthUint32,\r
84 AHCI_BAR_INDEX,\r
85 (UINT64) Offset,\r
86 1,\r
87 &Data\r
88 );\r
89\r
90 return;\r
91}\r
92\r
93/**\r
94 This function is used to calculate the best PIO mode supported by specific IDE device\r
95\r
96 @param IdentifyData The identify data of specific IDE device.\r
97 @param DisPioMode Disqualified PIO modes collection.\r
98 @param SelectedMode Available PIO modes collection.\r
99\r
100 @retval EFI_SUCCESS Best PIO modes are returned.\r
101 @retval EFI_UNSUPPORTED The device doesn't support PIO mode,\r
102 or all supported modes have been disqualified.\r
103**/\r
104EFI_STATUS\r
105CalculateBestPioMode (\r
106 IN EFI_IDENTIFY_DATA *IdentifyData,\r
107 IN UINT16 *DisPioMode OPTIONAL,\r
108 OUT UINT16 *SelectedMode\r
109 )\r
110{\r
111 UINT16 PioMode;\r
112 UINT16 AdvancedPioMode;\r
113 UINT16 Temp;\r
114 UINT16 Index;\r
115 UINT16 MinimumPioCycleTime;\r
116\r
117 Temp = 0xff;\r
118\r
119 PioMode = (UINT8) (((ATA5_IDENTIFY_DATA *) (&(IdentifyData->AtaData)))->pio_cycle_timing >> 8);\r
120\r
121 //\r
122 // See whether Identify Data word 64 - 70 are valid\r
123 //\r
124 if ((IdentifyData->AtaData.field_validity & 0x02) == 0x02) {\r
125\r
126 AdvancedPioMode = IdentifyData->AtaData.advanced_pio_modes;\r
127 DEBUG ((EFI_D_INFO, "CalculateBestPioMode: AdvancedPioMode = %x\n", AdvancedPioMode));\r
128\r
129 for (Index = 0; Index < 8; Index++) {\r
130 if ((AdvancedPioMode & 0x01) != 0) {\r
131 Temp = Index;\r
132 }\r
133\r
134 AdvancedPioMode >>= 1;\r
135 }\r
136\r
137 //\r
138 // If Temp is modified, mean the advanced_pio_modes is not zero;\r
139 // if Temp is not modified, mean there is no advanced PIO mode supported,\r
140 // the best PIO Mode is the value in pio_cycle_timing.\r
141 //\r
142 if (Temp != 0xff) {\r
143 AdvancedPioMode = (UINT16) (Temp + 3);\r
144 } else {\r
145 AdvancedPioMode = PioMode;\r
146 }\r
147\r
148 //\r
149 // Limit the PIO mode to at most PIO4.\r
150 //\r
151 PioMode = (UINT16) MIN (AdvancedPioMode, 4);\r
152\r
153 MinimumPioCycleTime = IdentifyData->AtaData.min_pio_cycle_time_with_flow_control;\r
154\r
155 if (MinimumPioCycleTime <= 120) {\r
156 PioMode = (UINT16) MIN (4, PioMode);\r
157 } else if (MinimumPioCycleTime <= 180) {\r
158 PioMode = (UINT16) MIN (3, PioMode);\r
159 } else if (MinimumPioCycleTime <= 240) {\r
160 PioMode = (UINT16) MIN (2, PioMode);\r
161 } else {\r
162 PioMode = 0;\r
163 }\r
164\r
165 //\r
166 // Degrade the PIO mode if the mode has been disqualified\r
167 //\r
168 if (DisPioMode != NULL) {\r
169 if (*DisPioMode < 2) {\r
170 return EFI_UNSUPPORTED; // no mode below ATA_PIO_MODE_BELOW_2\r
171 }\r
172\r
173 if (PioMode >= *DisPioMode) {\r
174 PioMode = (UINT16) (*DisPioMode - 1);\r
175 }\r
176 }\r
177\r
178 if (PioMode < 2) {\r
179 *SelectedMode = 1; // ATA_PIO_MODE_BELOW_2;\r
180 } else {\r
181 *SelectedMode = PioMode; // ATA_PIO_MODE_2 to ATA_PIO_MODE_4;\r
182 }\r
183\r
184 } else {\r
185 //\r
186 // Identify Data word 64 - 70 are not valid\r
187 // Degrade the PIO mode if the mode has been disqualified\r
188 //\r
189 if (DisPioMode != NULL) {\r
190 if (*DisPioMode < 2) {\r
191 return EFI_UNSUPPORTED; // no mode below ATA_PIO_MODE_BELOW_2\r
192 }\r
193\r
194 if (PioMode == *DisPioMode) {\r
195 PioMode--;\r
196 }\r
197 }\r
198\r
199 if (PioMode < 2) {\r
200 *SelectedMode = 1; // ATA_PIO_MODE_BELOW_2;\r
201 } else {\r
202 *SelectedMode = 2; // ATA_PIO_MODE_2;\r
203 }\r
204\r
205 }\r
206\r
207 return EFI_SUCCESS;\r
208}\r
209\r
210/**\r
211 This function is used to calculate the best UDMA mode supported by specific IDE device\r
212\r
213 @param IdentifyData The identify data of specific IDE device.\r
214 @param DisUDmaMode Disqualified UDMA modes collection.\r
215 @param SelectedMode Available UDMA modes collection.\r
216\r
217 @retval EFI_SUCCESS Best UDMA modes are returned.\r
218 @retval EFI_UNSUPPORTED The device doesn't support UDMA mode,\r
219 or all supported modes have been disqualified.\r
220**/\r
221EFI_STATUS\r
222CalculateBestUdmaMode (\r
223 IN EFI_IDENTIFY_DATA *IdentifyData,\r
224 IN UINT16 *DisUDmaMode OPTIONAL,\r
225 OUT UINT16 *SelectedMode\r
226 )\r
227{\r
228 UINT16 TempMode;\r
229 UINT16 DeviceUDmaMode;\r
230\r
231 DeviceUDmaMode = 0;\r
232\r
233 //\r
234 // Check whether the WORD 88 (supported UltraDMA by drive) is valid\r
235 //\r
236 if ((IdentifyData->AtaData.field_validity & 0x04) == 0x00) {\r
237 return EFI_UNSUPPORTED;\r
238 }\r
239\r
240 DeviceUDmaMode = IdentifyData->AtaData.ultra_dma_mode;\r
241 DEBUG ((EFI_D_INFO, "CalculateBestUdmaMode: DeviceUDmaMode = %x\n", DeviceUDmaMode));\r
242 DeviceUDmaMode &= 0x3f;\r
243 TempMode = 0; // initialize it to UDMA-0\r
244\r
245 while ((DeviceUDmaMode >>= 1) != 0) {\r
246 TempMode++;\r
247 }\r
248\r
249 //\r
250 // Degrade the UDMA mode if the mode has been disqualified\r
251 //\r
252 if (DisUDmaMode != NULL) {\r
253 if (*DisUDmaMode == 0) {\r
254 *SelectedMode = 0;\r
255 return EFI_UNSUPPORTED; // no mode below ATA_UDMA_MODE_0\r
256 }\r
257\r
258 if (TempMode >= *DisUDmaMode) {\r
259 TempMode = (UINT16) (*DisUDmaMode - 1);\r
260 }\r
261 }\r
262\r
263 //\r
264 // Possible returned mode is between ATA_UDMA_MODE_0 and ATA_UDMA_MODE_5\r
265 //\r
266 *SelectedMode = TempMode;\r
267\r
268 return EFI_SUCCESS;\r
269}\r
270\r
271/**\r
272 The Entry Point of module. It follows the standard UEFI driver model.\r
273\r
274 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
275 @param[in] SystemTable A pointer to the EFI System Table.\r
276 \r
277 @retval EFI_SUCCESS The entry point is executed successfully.\r
278 @retval other Some error occurs when executing this entry point.\r
279\r
280**/\r
281EFI_STATUS\r
282EFIAPI\r
283InitializeSataControllerDriver (\r
284 IN EFI_HANDLE ImageHandle,\r
285 IN EFI_SYSTEM_TABLE *SystemTable\r
286 )\r
287{\r
288 EFI_STATUS Status;\r
289\r
290 //\r
291 // Install driver model protocol(s).\r
292 //\r
293 Status = EfiLibInstallDriverBindingComponentName2 (\r
294 ImageHandle,\r
295 SystemTable,\r
296 &gSataControllerDriverBinding,\r
297 ImageHandle,\r
298 &gSataControllerComponentName,\r
299 &gSataControllerComponentName2\r
300 );\r
301 ASSERT_EFI_ERROR (Status);\r
302\r
303 return Status;\r
304}\r
305\r
306/**\r
307 Supported function of Driver Binding protocol for this driver.\r
308 Test to see if this driver supports ControllerHandle.\r
309\r
310 @param This Protocol instance pointer.\r
311 @param Controller Handle of device to test.\r
312 @param RemainingDevicePath A pointer to the device path.\r
313 it should be ignored by device driver.\r
314\r
315 @retval EFI_SUCCESS This driver supports this device.\r
316 @retval EFI_ALREADY_STARTED This driver is already running on this device.\r
317 @retval other This driver does not support this device.\r
318\r
319**/\r
320EFI_STATUS\r
321EFIAPI\r
322SataControllerSupported (\r
323 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
324 IN EFI_HANDLE Controller,\r
325 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
326 )\r
327{\r
328 EFI_STATUS Status;\r
329 EFI_PCI_IO_PROTOCOL *PciIo;\r
330 PCI_TYPE00 PciData;\r
331\r
332 //\r
333 // Attempt to open PCI I/O Protocol\r
334 //\r
335 Status = gBS->OpenProtocol (\r
336 Controller,\r
337 &gEfiPciIoProtocolGuid,\r
338 (VOID **) &PciIo,\r
339 This->DriverBindingHandle,\r
340 Controller,\r
341 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
342 );\r
343 if (EFI_ERROR (Status)) {\r
344 return Status;\r
345 }\r
346\r
347 //\r
348 // Now further check the PCI header: Base Class (offset 0x0B) and\r
349 // Sub Class (offset 0x0A). This controller should be an SATA controller\r
350 //\r
351 Status = PciIo->Pci.Read (\r
352 PciIo,\r
353 EfiPciIoWidthUint8,\r
354 PCI_CLASSCODE_OFFSET,\r
355 sizeof (PciData.Hdr.ClassCode),\r
356 PciData.Hdr.ClassCode\r
357 );\r
358 if (EFI_ERROR (Status)) {\r
359 return EFI_UNSUPPORTED;\r
360 }\r
361\r
362 if (IS_PCI_IDE (&PciData) || IS_PCI_SATADPA (&PciData)) {\r
363 return EFI_SUCCESS;\r
364 }\r
365\r
366 return EFI_UNSUPPORTED;\r
367}\r
368\r
369/**\r
370 This routine is called right after the .Supported() called and \r
371 Start this driver on ControllerHandle.\r
372\r
373 @param This Protocol instance pointer.\r
374 @param Controller Handle of device to bind driver to.\r
375 @param RemainingDevicePath A pointer to the device path.\r
376 it should be ignored by device driver.\r
377\r
378 @retval EFI_SUCCESS This driver is added to this device.\r
379 @retval EFI_ALREADY_STARTED This driver is already running on this device.\r
380 @retval other Some error occurs when binding this driver to this device.\r
381\r
382**/\r
383EFI_STATUS\r
384EFIAPI\r
385SataControllerStart (\r
386 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
387 IN EFI_HANDLE Controller,\r
388 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
389 )\r
390{\r
5dfba97c 391 UINTN BailLogMask;\r
12e92a23
RJ
392 EFI_STATUS Status;\r
393 EFI_PCI_IO_PROTOCOL *PciIo;\r
bcab7141 394 UINT64 OriginalPciAttributes;\r
12e92a23
RJ
395 PCI_TYPE00 PciData;\r
396 EFI_SATA_CONTROLLER_PRIVATE_DATA *SataPrivateData;\r
397 UINT32 Data32;\r
398 UINTN ChannelDeviceCount;\r
399\r
400 DEBUG ((EFI_D_INFO, "SataControllerStart START\n"));\r
401\r
5dfba97c 402 BailLogMask = DEBUG_ERROR;\r
12e92a23
RJ
403 SataPrivateData = NULL;\r
404\r
405 //\r
406 // Now test and open PCI I/O Protocol\r
407 //\r
408 Status = gBS->OpenProtocol (\r
409 Controller,\r
410 &gEfiPciIoProtocolGuid,\r
411 (VOID **) &PciIo,\r
412 This->DriverBindingHandle,\r
413 Controller,\r
414 EFI_OPEN_PROTOCOL_BY_DRIVER\r
415 );\r
416 if (EFI_ERROR (Status)) {\r
5dfba97c
LE
417 if (Status == EFI_ALREADY_STARTED) {\r
418 //\r
419 // This is an expected condition for OpenProtocol() / BY_DRIVER, in a\r
420 // DriverBindingStart() member function; degrade the log mask to\r
421 // DEBUG_INFO.\r
422 //\r
423 BailLogMask = DEBUG_INFO;\r
424 }\r
379b1796 425 goto Bail;\r
12e92a23
RJ
426 }\r
427\r
bcab7141
LE
428 //\r
429 // Save original PCI attributes, and enable IO space access, memory space\r
430 // access, and Bus Master (DMA).\r
431 //\r
432 Status = PciIo->Attributes (PciIo, EfiPciIoAttributeOperationGet, 0,\r
433 &OriginalPciAttributes);\r
434 if (EFI_ERROR (Status)) {\r
435 goto ClosePciIo;\r
436 }\r
437 Status = PciIo->Attributes (PciIo, EfiPciIoAttributeOperationEnable,\r
438 EFI_PCI_DEVICE_ENABLE, NULL);\r
439 if (EFI_ERROR (Status)) {\r
440 goto ClosePciIo;\r
441 }\r
442\r
12e92a23
RJ
443 //\r
444 // Allocate Sata Private Data structure\r
445 //\r
446 SataPrivateData = AllocateZeroPool (sizeof (EFI_SATA_CONTROLLER_PRIVATE_DATA));\r
447 if (SataPrivateData == NULL) {\r
448 Status = EFI_OUT_OF_RESOURCES;\r
bcab7141 449 goto RestorePciAttributes;\r
12e92a23
RJ
450 }\r
451\r
452 //\r
453 // Initialize Sata Private Data\r
454 //\r
455 SataPrivateData->Signature = SATA_CONTROLLER_SIGNATURE;\r
456 SataPrivateData->PciIo = PciIo;\r
bcab7141 457 SataPrivateData->OriginalPciAttributes = OriginalPciAttributes;\r
12e92a23
RJ
458 SataPrivateData->IdeInit.GetChannelInfo = IdeInitGetChannelInfo;\r
459 SataPrivateData->IdeInit.NotifyPhase = IdeInitNotifyPhase;\r
460 SataPrivateData->IdeInit.SubmitData = IdeInitSubmitData;\r
461 SataPrivateData->IdeInit.DisqualifyMode = IdeInitDisqualifyMode;\r
462 SataPrivateData->IdeInit.CalculateMode = IdeInitCalculateMode;\r
463 SataPrivateData->IdeInit.SetTiming = IdeInitSetTiming;\r
464 SataPrivateData->IdeInit.EnumAll = SATA_ENUMER_ALL;\r
465\r
466 Status = PciIo->Pci.Read (\r
467 PciIo,\r
468 EfiPciIoWidthUint8,\r
469 PCI_CLASSCODE_OFFSET,\r
470 sizeof (PciData.Hdr.ClassCode),\r
471 PciData.Hdr.ClassCode\r
472 );\r
379b1796
LE
473 if (EFI_ERROR (Status)) {\r
474 goto FreeSataPrivateData;\r
475 }\r
12e92a23
RJ
476\r
477 if (IS_PCI_IDE (&PciData)) {\r
478 SataPrivateData->IdeInit.ChannelCount = IDE_MAX_CHANNEL;\r
479 SataPrivateData->DeviceCount = IDE_MAX_DEVICES;\r
480 } else if (IS_PCI_SATADPA (&PciData)) {\r
481 //\r
482 // Read Host Capability Register(CAP) to get Number of Ports(NPS) and Supports Port Multiplier(SPM)\r
483 // NPS is 0's based value indicating the maximum number of ports supported by the HBA silicon.\r
484 // A maximum of 32 ports can be supported. A value of '0h', indicating one port, is the minimum requirement.\r
485 //\r
486 Data32 = AhciReadReg (PciIo, R_AHCI_CAP);\r
487 SataPrivateData->IdeInit.ChannelCount = (UINT8) ((Data32 & B_AHCI_CAP_NPS) + 1);\r
488 SataPrivateData->DeviceCount = AHCI_MAX_DEVICES;\r
489 if ((Data32 & B_AHCI_CAP_SPM) == B_AHCI_CAP_SPM) {\r
490 SataPrivateData->DeviceCount = AHCI_MULTI_MAX_DEVICES;\r
491 }\r
492 }\r
493\r
494 ChannelDeviceCount = (UINTN) (SataPrivateData->IdeInit.ChannelCount) * (UINTN) (SataPrivateData->DeviceCount);\r
495 SataPrivateData->DisqualifiedModes = AllocateZeroPool ((sizeof (EFI_ATA_COLLECTIVE_MODE)) * ChannelDeviceCount);\r
496 if (SataPrivateData->DisqualifiedModes == NULL) {\r
497 Status = EFI_OUT_OF_RESOURCES;\r
379b1796 498 goto FreeSataPrivateData;\r
12e92a23
RJ
499 }\r
500\r
501 SataPrivateData->IdentifyData = AllocateZeroPool ((sizeof (EFI_IDENTIFY_DATA)) * ChannelDeviceCount);\r
502 if (SataPrivateData->IdentifyData == NULL) {\r
503 Status = EFI_OUT_OF_RESOURCES;\r
379b1796 504 goto FreeDisqualifiedModes;\r
12e92a23
RJ
505 }\r
506\r
507 SataPrivateData->IdentifyValid = AllocateZeroPool ((sizeof (BOOLEAN)) * ChannelDeviceCount);\r
508 if (SataPrivateData->IdentifyValid == NULL) {\r
509 Status = EFI_OUT_OF_RESOURCES;\r
379b1796 510 goto FreeIdentifyData;\r
12e92a23
RJ
511 }\r
512\r
513 //\r
514 // Install IDE Controller Init Protocol to this instance\r
515 //\r
516 Status = gBS->InstallMultipleProtocolInterfaces (\r
517 &Controller,\r
518 &gEfiIdeControllerInitProtocolGuid,\r
519 &(SataPrivateData->IdeInit),\r
520 NULL\r
521 );\r
522\r
12e92a23 523 if (EFI_ERROR (Status)) {\r
379b1796 524 goto FreeIdentifyValid;\r
12e92a23
RJ
525 }\r
526\r
527 DEBUG ((EFI_D_INFO, "SataControllerStart END status = %r\n", Status));\r
379b1796
LE
528 return Status;\r
529\r
530FreeIdentifyValid:\r
531 FreePool (SataPrivateData->IdentifyValid);\r
532\r
533FreeIdentifyData:\r
534 FreePool (SataPrivateData->IdentifyData);\r
535\r
536FreeDisqualifiedModes:\r
537 FreePool (SataPrivateData->DisqualifiedModes);\r
538\r
539FreeSataPrivateData:\r
540 FreePool (SataPrivateData);\r
541\r
bcab7141
LE
542RestorePciAttributes:\r
543 PciIo->Attributes (PciIo, EfiPciIoAttributeOperationSet,\r
544 OriginalPciAttributes, NULL);\r
545\r
379b1796
LE
546ClosePciIo:\r
547 gBS->CloseProtocol (\r
548 Controller,\r
549 &gEfiPciIoProtocolGuid,\r
550 This->DriverBindingHandle,\r
551 Controller\r
552 );\r
12e92a23 553\r
379b1796 554Bail:\r
5dfba97c
LE
555 DEBUG ((BailLogMask, "SataControllerStart error return status = %r\n",\r
556 Status));\r
12e92a23
RJ
557 return Status;\r
558}\r
559\r
560/**\r
561 Stop this driver on ControllerHandle.\r
562\r
563 @param This Protocol instance pointer.\r
564 @param Controller Handle of device to stop driver on.\r
565 @param NumberOfChildren Not used.\r
566 @param ChildHandleBuffer Not used.\r
567\r
568 @retval EFI_SUCCESS This driver is removed from this device.\r
569 @retval other Some error occurs when removing this driver from this device.\r
570\r
571**/\r
572EFI_STATUS\r
573EFIAPI\r
574SataControllerStop (\r
575 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
576 IN EFI_HANDLE Controller,\r
577 IN UINTN NumberOfChildren,\r
578 IN EFI_HANDLE *ChildHandleBuffer\r
579 )\r
580{\r
581 EFI_STATUS Status;\r
582 EFI_IDE_CONTROLLER_INIT_PROTOCOL *IdeInit;\r
583 EFI_SATA_CONTROLLER_PRIVATE_DATA *SataPrivateData;\r
0b448dd8
LE
584 EFI_PCI_IO_PROTOCOL *PciIo;\r
585 UINT64 OriginalPciAttributes;\r
12e92a23
RJ
586\r
587 //\r
588 // Open the produced protocol\r
589 //\r
590 Status = gBS->OpenProtocol (\r
591 Controller,\r
592 &gEfiIdeControllerInitProtocolGuid,\r
593 (VOID **) &IdeInit,\r
594 This->DriverBindingHandle,\r
595 Controller,\r
596 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
597 );\r
598 if (EFI_ERROR (Status)) {\r
599 return EFI_UNSUPPORTED;\r
600 }\r
601\r
602 SataPrivateData = SATA_CONTROLLER_PRIVATE_DATA_FROM_THIS (IdeInit);\r
603 ASSERT (SataPrivateData != NULL);\r
604\r
0b448dd8
LE
605 PciIo = SataPrivateData->PciIo;\r
606 OriginalPciAttributes = SataPrivateData->OriginalPciAttributes;\r
607\r
12e92a23
RJ
608 //\r
609 // Uninstall the IDE Controller Init Protocol from this instance\r
610 //\r
611 Status = gBS->UninstallMultipleProtocolInterfaces (\r
612 Controller,\r
613 &gEfiIdeControllerInitProtocolGuid,\r
614 &(SataPrivateData->IdeInit),\r
615 NULL\r
616 );\r
617 if (EFI_ERROR (Status)) {\r
618 return Status;\r
619 }\r
620\r
81310a62
LE
621 if (SataPrivateData->DisqualifiedModes != NULL) {\r
622 FreePool (SataPrivateData->DisqualifiedModes);\r
12e92a23 623 }\r
81310a62
LE
624 if (SataPrivateData->IdentifyData != NULL) {\r
625 FreePool (SataPrivateData->IdentifyData);\r
626 }\r
627 if (SataPrivateData->IdentifyValid != NULL) {\r
628 FreePool (SataPrivateData->IdentifyValid);\r
629 }\r
630 FreePool (SataPrivateData);\r
12e92a23 631\r
bcab7141
LE
632 //\r
633 // Restore original PCI attributes\r
634 //\r
0b448dd8
LE
635 PciIo->Attributes (\r
636 PciIo,\r
637 EfiPciIoAttributeOperationSet,\r
638 OriginalPciAttributes,\r
639 NULL\r
640 );\r
bcab7141 641\r
12e92a23
RJ
642 //\r
643 // Close protocols opened by Sata Controller driver\r
644 //\r
645 return gBS->CloseProtocol (\r
646 Controller,\r
647 &gEfiPciIoProtocolGuid,\r
648 This->DriverBindingHandle,\r
649 Controller\r
650 );\r
651}\r
652\r
653/**\r
654 Calculate the flat array subscript of a (Channel, Device) pair.\r
655\r
656 @param[in] SataPrivateData The private data structure corresponding to the\r
657 SATA controller that attaches the device for\r
658 which the flat array subscript is being\r
659 calculated.\r
660\r
661 @param[in] Channel The channel (ie. port) number on the SATA\r
662 controller that the device is attached to.\r
663\r
664 @param[in] Device The device number on the channel.\r
665\r
666 @return The flat array subscript suitable for indexing DisqualifiedModes,\r
667 IdentifyData, and IdentifyValid.\r
668**/\r
669STATIC\r
670UINTN\r
671FlatDeviceIndex (\r
672 IN CONST EFI_SATA_CONTROLLER_PRIVATE_DATA *SataPrivateData,\r
673 IN UINTN Channel,\r
674 IN UINTN Device\r
675 )\r
676{\r
677 ASSERT (SataPrivateData != NULL);\r
678 ASSERT (Channel < SataPrivateData->IdeInit.ChannelCount);\r
679 ASSERT (Device < SataPrivateData->DeviceCount);\r
680\r
681 return Channel * SataPrivateData->DeviceCount + Device;\r
682}\r
683\r
684//\r
685// Interface functions of IDE_CONTROLLER_INIT protocol\r
686//\r
687/**\r
688 Returns the information about the specified IDE channel.\r
689 \r
690 This function can be used to obtain information about a particular IDE channel.\r
691 The driver entity uses this information during the enumeration process. \r
692 \r
693 If Enabled is set to FALSE, the driver entity will not scan the channel. Note \r
694 that it will not prevent an operating system driver from scanning the channel.\r
695 \r
696 For most of today's controllers, MaxDevices will either be 1 or 2. For SATA \r
697 controllers, this value will always be 1. SATA configurations can contain SATA \r
698 port multipliers. SATA port multipliers behave like SATA bridges and can support\r
699 up to 16 devices on the other side. If a SATA port out of the IDE controller \r
700 is connected to a port multiplier, MaxDevices will be set to the number of SATA \r
701 devices that the port multiplier supports. Because today's port multipliers \r
702 support up to fifteen SATA devices, this number can be as large as fifteen. The IDE \r
703 bus driver is required to scan for the presence of port multipliers behind an SATA \r
704 controller and enumerate up to MaxDevices number of devices behind the port \r
705 multiplier. \r
706 \r
707 In this context, the devices behind a port multiplier constitute a channel. \r
708 \r
709 @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.\r
710 @param[in] Channel Zero-based channel number.\r
711 @param[out] Enabled TRUE if this channel is enabled. Disabled channels \r
712 are not scanned to see if any devices are present.\r
713 @param[out] MaxDevices The maximum number of IDE devices that the bus driver\r
714 can expect on this channel. For the ATA/ATAPI \r
715 specification, version 6, this number will either be \r
716 one or two. For Serial ATA (SATA) configurations with a \r
717 port multiplier, this number can be as large as fifteen.\r
718\r
719 @retval EFI_SUCCESS Information was returned without any errors.\r
720 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).\r
721\r
722**/\r
723EFI_STATUS\r
724EFIAPI\r
725IdeInitGetChannelInfo (\r
726 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL *This,\r
727 IN UINT8 Channel,\r
728 OUT BOOLEAN *Enabled,\r
729 OUT UINT8 *MaxDevices\r
730 )\r
731{\r
732 EFI_SATA_CONTROLLER_PRIVATE_DATA *SataPrivateData;\r
733 SataPrivateData = SATA_CONTROLLER_PRIVATE_DATA_FROM_THIS (This);\r
734 ASSERT (SataPrivateData != NULL);\r
735\r
736 if (Channel < This->ChannelCount) {\r
737 *Enabled = TRUE;\r
738 *MaxDevices = SataPrivateData->DeviceCount;\r
739 return EFI_SUCCESS;\r
740 }\r
741\r
742 *Enabled = FALSE;\r
743 return EFI_INVALID_PARAMETER;\r
744}\r
745\r
746/**\r
747 The notifications from the driver entity that it is about to enter a certain\r
748 phase of the IDE channel enumeration process.\r
749 \r
750 This function can be used to notify the IDE controller driver to perform \r
751 specific actions, including any chipset-specific initialization, so that the \r
752 chipset is ready to enter the next phase. Seven notification points are defined \r
753 at this time. \r
754 \r
755 More synchronization points may be added as required in the future. \r
756\r
757 @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.\r
758 @param[in] Phase The phase during enumeration.\r
759 @param[in] Channel Zero-based channel number.\r
760\r
761 @retval EFI_SUCCESS The notification was accepted without any errors.\r
762 @retval EFI_UNSUPPORTED Phase is not supported.\r
763 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).\r
764 @retval EFI_NOT_READY This phase cannot be entered at this time; for \r
765 example, an attempt was made to enter a Phase \r
766 without having entered one or more previous \r
767 Phase.\r
768\r
769**/\r
770EFI_STATUS\r
771EFIAPI\r
772IdeInitNotifyPhase (\r
773 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL *This,\r
774 IN EFI_IDE_CONTROLLER_ENUM_PHASE Phase,\r
775 IN UINT8 Channel\r
776 )\r
777{\r
778 return EFI_SUCCESS;\r
779}\r
780\r
781/**\r
782 Submits the device information to the IDE controller driver.\r
783\r
784 This function is used by the driver entity to pass detailed information about \r
785 a particular device to the IDE controller driver. The driver entity obtains \r
786 this information by issuing an ATA or ATAPI IDENTIFY_DEVICE command. IdentifyData\r
787 is the pointer to the response data buffer. The IdentifyData buffer is owned \r
788 by the driver entity, and the IDE controller driver must make a local copy \r
789 of the entire buffer or parts of the buffer as needed. The original IdentifyData \r
790 buffer pointer may not be valid when\r
791 \r
792 - EFI_IDE_CONTROLLER_INIT_PROTOCOL.CalculateMode() or\r
793 - EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode() is called at a later point.\r
794 \r
795 The IDE controller driver may consult various fields of EFI_IDENTIFY_DATA to \r
796 compute the optimum mode for the device. These fields are not limited to the \r
797 timing information. For example, an implementation of the IDE controller driver \r
798 may examine the vendor and type/mode field to match known bad drives. \r
799 \r
800 The driver entity may submit drive information in any order, as long as it \r
801 submits information for all the devices belonging to the enumeration group \r
802 before EFI_IDE_CONTROLLER_INIT_PROTOCOL.CalculateMode() is called for any device\r
803 in that enumeration group. If a device is absent, EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()\r
804 should be called with IdentifyData set to NULL. The IDE controller driver may \r
805 not have any other mechanism to know whether a device is present or not. Therefore, \r
806 setting IdentifyData to NULL does not constitute an error condition. \r
807 EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData() can be called only once for a \r
808 given (Channel, Device) pair. \r
809 \r
810 @param[in] This A pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.\r
811 @param[in] Channel Zero-based channel number.\r
812 @param[in] Device Zero-based device number on the Channel.\r
813 @param[in] IdentifyData The device's response to the ATA IDENTIFY_DEVICE command.\r
814\r
815 @retval EFI_SUCCESS The information was accepted without any errors.\r
816 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).\r
817 @retval EFI_INVALID_PARAMETER Device is invalid.\r
818\r
819**/\r
820EFI_STATUS\r
821EFIAPI\r
822IdeInitSubmitData (\r
823 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL *This,\r
824 IN UINT8 Channel,\r
825 IN UINT8 Device,\r
826 IN EFI_IDENTIFY_DATA *IdentifyData\r
827 )\r
828{\r
829 EFI_SATA_CONTROLLER_PRIVATE_DATA *SataPrivateData;\r
830 UINTN DeviceIndex;\r
831\r
832 SataPrivateData = SATA_CONTROLLER_PRIVATE_DATA_FROM_THIS (This);\r
833 ASSERT (SataPrivateData != NULL);\r
834\r
835 if ((Channel >= This->ChannelCount) || (Device >= SataPrivateData->DeviceCount)) {\r
836 return EFI_INVALID_PARAMETER;\r
837 }\r
838\r
839 DeviceIndex = FlatDeviceIndex (SataPrivateData, Channel, Device);\r
840\r
841 //\r
842 // Make a local copy of device's IdentifyData and mark the valid flag\r
843 //\r
844 if (IdentifyData != NULL) {\r
845 CopyMem (\r
846 &(SataPrivateData->IdentifyData[DeviceIndex]),\r
847 IdentifyData,\r
848 sizeof (EFI_IDENTIFY_DATA)\r
849 );\r
850\r
851 SataPrivateData->IdentifyValid[DeviceIndex] = TRUE;\r
852 } else {\r
853 SataPrivateData->IdentifyValid[DeviceIndex] = FALSE;\r
854 }\r
855\r
856 return EFI_SUCCESS;\r
857}\r
858\r
859/**\r
860 Disqualifies specific modes for an IDE device.\r
861\r
862 This function allows the driver entity or other drivers (such as platform \r
863 drivers) to reject certain timing modes and request the IDE controller driver\r
864 to recalculate modes. This function allows the driver entity and the IDE \r
865 controller driver to negotiate the timings on a per-device basis. This function \r
866 is useful in the case of drives that lie about their capabilities. An example \r
867 is when the IDE device fails to accept the timing modes that are calculated \r
868 by the IDE controller driver based on the response to the Identify Drive command.\r
869\r
870 If the driver entity does not want to limit the ATA timing modes and leave that \r
871 decision to the IDE controller driver, it can either not call this function for \r
872 the given device or call this function and set the Valid flag to FALSE for all \r
873 modes that are listed in EFI_ATA_COLLECTIVE_MODE.\r
874 \r
875 The driver entity may disqualify modes for a device in any order and any number \r
876 of times.\r
877 \r
878 This function can be called multiple times to invalidate multiple modes of the \r
879 same type (e.g., Programmed Input/Output [PIO] modes 3 and 4). See the ATA/ATAPI \r
880 specification for more information on PIO modes. \r
881 \r
882 For Serial ATA (SATA) controllers, this member function can be used to disqualify\r
883 a higher transfer rate mode on a given channel. For example, a platform driver\r
884 may inform the IDE controller driver to not use second-generation (Gen2) speeds \r
885 for a certain SATA drive.\r
886 \r
887 @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.\r
888 @param[in] Channel The zero-based channel number.\r
889 @param[in] Device The zero-based device number on the Channel.\r
890 @param[in] BadModes The modes that the device does not support and that\r
891 should be disqualified.\r
892\r
893 @retval EFI_SUCCESS The modes were accepted without any errors.\r
894 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).\r
895 @retval EFI_INVALID_PARAMETER Device is invalid.\r
896 @retval EFI_INVALID_PARAMETER IdentifyData is NULL.\r
897 \r
898**/\r
899EFI_STATUS\r
900EFIAPI\r
901IdeInitDisqualifyMode (\r
902 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL *This,\r
903 IN UINT8 Channel,\r
904 IN UINT8 Device,\r
905 IN EFI_ATA_COLLECTIVE_MODE *BadModes\r
906 )\r
907{\r
908 EFI_SATA_CONTROLLER_PRIVATE_DATA *SataPrivateData;\r
909 UINTN DeviceIndex;\r
910\r
911 SataPrivateData = SATA_CONTROLLER_PRIVATE_DATA_FROM_THIS (This);\r
912 ASSERT (SataPrivateData != NULL);\r
913\r
914 if ((Channel >= This->ChannelCount) || (BadModes == NULL) || (Device >= SataPrivateData->DeviceCount)) {\r
915 return EFI_INVALID_PARAMETER;\r
916 }\r
917\r
918 DeviceIndex = FlatDeviceIndex (SataPrivateData, Channel, Device);\r
919\r
920 //\r
921 // Record the disqualified modes per channel per device. From ATA/ATAPI spec,\r
922 // if a mode is not supported, the modes higher than it is also not supported.\r
923 //\r
924 CopyMem (\r
925 &(SataPrivateData->DisqualifiedModes[DeviceIndex]),\r
926 BadModes,\r
927 sizeof (EFI_ATA_COLLECTIVE_MODE)\r
928 );\r
929\r
930 return EFI_SUCCESS;\r
931}\r
932\r
933/**\r
934 Returns the information about the optimum modes for the specified IDE device.\r
935\r
936 This function is used by the driver entity to obtain the optimum ATA modes for\r
937 a specific device. The IDE controller driver takes into account the following \r
938 while calculating the mode:\r
939 - The IdentifyData inputs to EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()\r
940 - The BadModes inputs to EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode()\r
941\r
942 The driver entity is required to call EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData() \r
943 for all the devices that belong to an enumeration group before calling \r
944 EFI_IDE_CONTROLLER_INIT_PROTOCOL.CalculateMode() for any device in the same group. \r
945 \r
946 The IDE controller driver will use controller- and possibly platform-specific \r
947 algorithms to arrive at SupportedModes. The IDE controller may base its \r
948 decision on user preferences and other considerations as well. This function \r
949 may be called multiple times because the driver entity may renegotiate the mode \r
950 with the IDE controller driver using EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode().\r
951 \r
952 The driver entity may collect timing information for various devices in any \r
953 order. The driver entity is responsible for making sure that all the dependencies\r
954 are satisfied. For example, the SupportedModes information for device A that \r
955 was previously returned may become stale after a call to \r
956 EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode() for device B.\r
957 \r
958 The buffer SupportedModes is allocated by the callee because the caller does \r
959 not necessarily know the size of the buffer. The type EFI_ATA_COLLECTIVE_MODE \r
960 is defined in a way that allows for future extensibility and can be of variable \r
961 length. This memory pool should be deallocated by the caller when it is no \r
962 longer necessary. \r
963 \r
964 The IDE controller driver for a Serial ATA (SATA) controller can use this \r
965 member function to force a lower speed (first-generation [Gen1] speeds on a \r
966 second-generation [Gen2]-capable hardware). The IDE controller driver can \r
967 also allow the driver entity to stay with the speed that has been negotiated \r
968 by the physical layer.\r
969 \r
970 @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.\r
971 @param[in] Channel A zero-based channel number.\r
972 @param[in] Device A zero-based device number on the Channel.\r
973 @param[out] SupportedModes The optimum modes for the device.\r
974\r
975 @retval EFI_SUCCESS SupportedModes was returned.\r
976 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).\r
977 @retval EFI_INVALID_PARAMETER Device is invalid. \r
978 @retval EFI_INVALID_PARAMETER SupportedModes is NULL.\r
979 @retval EFI_NOT_READY Modes cannot be calculated due to a lack of \r
980 data. This error may happen if \r
981 EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData() \r
982 and EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyData() \r
983 were not called for at least one drive in the \r
984 same enumeration group.\r
985\r
986**/\r
987EFI_STATUS\r
988EFIAPI\r
989IdeInitCalculateMode (\r
990 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL *This,\r
991 IN UINT8 Channel,\r
992 IN UINT8 Device,\r
993 OUT EFI_ATA_COLLECTIVE_MODE **SupportedModes\r
994 )\r
995{\r
996 EFI_SATA_CONTROLLER_PRIVATE_DATA *SataPrivateData;\r
997 EFI_IDENTIFY_DATA *IdentifyData;\r
998 BOOLEAN IdentifyValid;\r
999 EFI_ATA_COLLECTIVE_MODE *DisqualifiedModes;\r
1000 UINT16 SelectedMode;\r
1001 EFI_STATUS Status;\r
1002 UINTN DeviceIndex;\r
1003\r
1004 SataPrivateData = SATA_CONTROLLER_PRIVATE_DATA_FROM_THIS (This);\r
1005 ASSERT (SataPrivateData != NULL);\r
1006\r
1007 if ((Channel >= This->ChannelCount) || (SupportedModes == NULL) || (Device >= SataPrivateData->DeviceCount)) {\r
1008 return EFI_INVALID_PARAMETER;\r
1009 }\r
1010\r
1011 *SupportedModes = AllocateZeroPool (sizeof (EFI_ATA_COLLECTIVE_MODE));\r
1012 if (*SupportedModes == NULL) {\r
1013 ASSERT (*SupportedModes != NULL);\r
1014 return EFI_OUT_OF_RESOURCES;\r
1015 }\r
1016\r
1017 DeviceIndex = FlatDeviceIndex (SataPrivateData, Channel, Device);\r
1018\r
1019 IdentifyData = &(SataPrivateData->IdentifyData[DeviceIndex]);\r
1020 IdentifyValid = SataPrivateData->IdentifyValid[DeviceIndex];\r
1021 DisqualifiedModes = &(SataPrivateData->DisqualifiedModes[DeviceIndex]);\r
1022\r
1023 //\r
1024 // Make sure we've got the valid identify data of the device from SubmitData()\r
1025 //\r
1026 if (!IdentifyValid) {\r
1027 FreePool (*SupportedModes);\r
1028 return EFI_NOT_READY;\r
1029 }\r
1030\r
1031 Status = CalculateBestPioMode (\r
1032 IdentifyData,\r
1033 (DisqualifiedModes->PioMode.Valid ? ((UINT16 *) &(DisqualifiedModes->PioMode.Mode)) : NULL),\r
1034 &SelectedMode\r
1035 );\r
1036 if (!EFI_ERROR (Status)) {\r
1037 (*SupportedModes)->PioMode.Valid = TRUE;\r
1038 (*SupportedModes)->PioMode.Mode = SelectedMode;\r
1039\r
1040 } else {\r
1041 (*SupportedModes)->PioMode.Valid = FALSE;\r
1042 }\r
1043 DEBUG ((EFI_D_INFO, "IdeInitCalculateMode: PioMode = %x\n", (*SupportedModes)->PioMode.Mode));\r
1044\r
1045 Status = CalculateBestUdmaMode (\r
1046 IdentifyData,\r
1047 (DisqualifiedModes->UdmaMode.Valid ? ((UINT16 *) &(DisqualifiedModes->UdmaMode.Mode)) : NULL),\r
1048 &SelectedMode\r
1049 );\r
1050\r
1051 if (!EFI_ERROR (Status)) {\r
1052 (*SupportedModes)->UdmaMode.Valid = TRUE;\r
1053 (*SupportedModes)->UdmaMode.Mode = SelectedMode;\r
1054\r
1055 } else {\r
1056 (*SupportedModes)->UdmaMode.Valid = FALSE;\r
1057 }\r
1058 DEBUG ((EFI_D_INFO, "IdeInitCalculateMode: UdmaMode = %x\n", (*SupportedModes)->UdmaMode.Mode));\r
1059\r
1060 //\r
1061 // The modes other than PIO and UDMA are not supported\r
1062 //\r
1063 return EFI_SUCCESS;\r
1064}\r
1065\r
1066/**\r
1067 Commands the IDE controller driver to program the IDE controller hardware\r
1068 so that the specified device can operate at the specified mode.\r
1069\r
1070 This function is used by the driver entity to instruct the IDE controller \r
1071 driver to program the IDE controller hardware to the specified modes. This \r
1072 function can be called only once for a particular device. For a Serial ATA \r
1073 (SATA) Advanced Host Controller Interface (AHCI) controller, no controller-\r
1074 specific programming may be required.\r
1075\r
1076 @param[in] This Pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.\r
1077 @param[in] Channel Zero-based channel number.\r
1078 @param[in] Device Zero-based device number on the Channel.\r
1079 @param[in] Modes The modes to set.\r
1080\r
1081 @retval EFI_SUCCESS The command was accepted without any errors.\r
1082 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).\r
1083 @retval EFI_INVALID_PARAMETER Device is invalid.\r
1084 @retval EFI_NOT_READY Modes cannot be set at this time due to lack of data.\r
1085 @retval EFI_DEVICE_ERROR Modes cannot be set due to hardware failure.\r
1086 The driver entity should not use this device.\r
1087\r
1088**/\r
1089EFI_STATUS\r
1090EFIAPI\r
1091IdeInitSetTiming (\r
1092 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL *This,\r
1093 IN UINT8 Channel,\r
1094 IN UINT8 Device,\r
1095 IN EFI_ATA_COLLECTIVE_MODE *Modes\r
1096 )\r
1097{\r
1098 return EFI_SUCCESS;\r
1099}\r