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