]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Pci/SataControllerDxe/SataController.c
MdeModulePkg: Enable SATA Controller PCI mem space
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / SataControllerDxe / SataController.c
CommitLineData
fda951df
FT
1/** @file\r
2 This driver module produces IDE_CONTROLLER_INIT protocol for Sata Controllers.\r
3\r
4 Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>\r
24fee052 5 Copyright (c) 2018, ARM Ltd. All rights reserved.<BR>\r
fda951df
FT
6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "SataController.h"\r
17\r
18///\r
19/// EFI_DRIVER_BINDING_PROTOCOL instance\r
20///\r
21EFI_DRIVER_BINDING_PROTOCOL gSataControllerDriverBinding = {\r
22 SataControllerSupported,\r
23 SataControllerStart,\r
24 SataControllerStop,\r
25 0xa,\r
26 NULL,\r
27 NULL\r
28};\r
29\r
30/**\r
31 Read AHCI Operation register.\r
32\r
33 @param PciIo The PCI IO protocol instance.\r
34 @param Offset The operation register offset.\r
35\r
36 @return The register content read.\r
37\r
38**/\r
39UINT32\r
40EFIAPI\r
41AhciReadReg (\r
42 IN EFI_PCI_IO_PROTOCOL *PciIo,\r
43 IN UINT32 Offset\r
44 )\r
45{\r
46 UINT32 Data;\r
47\r
48 ASSERT (PciIo != NULL);\r
49 \r
50 Data = 0;\r
51\r
52 PciIo->Mem.Read (\r
53 PciIo,\r
54 EfiPciIoWidthUint32,\r
55 AHCI_BAR_INDEX,\r
56 (UINT64) Offset,\r
57 1,\r
58 &Data\r
59 );\r
60\r
61 return Data;\r
62}\r
63\r
64/**\r
65 This function is used to calculate the best PIO mode supported by specific IDE device\r
66\r
67 @param IdentifyData The identify data of specific IDE device.\r
68 @param DisPioMode Disqualified PIO modes collection.\r
69 @param SelectedMode Available PIO modes collection.\r
70\r
71 @retval EFI_SUCCESS Best PIO modes are returned.\r
72 @retval EFI_UNSUPPORTED The device doesn't support PIO mode,\r
73 or all supported modes have been disqualified.\r
74**/\r
75EFI_STATUS\r
76CalculateBestPioMode (\r
77 IN EFI_IDENTIFY_DATA *IdentifyData,\r
78 IN UINT16 *DisPioMode OPTIONAL,\r
79 OUT UINT16 *SelectedMode\r
80 )\r
81{\r
82 UINT16 PioMode;\r
83 UINT16 AdvancedPioMode;\r
84 UINT16 Temp;\r
85 UINT16 Index;\r
86 UINT16 MinimumPioCycleTime;\r
87\r
88 Temp = 0xff;\r
89\r
90 PioMode = (UINT8) (((ATA5_IDENTIFY_DATA *) (&(IdentifyData->AtaData)))->pio_cycle_timing >> 8);\r
91\r
92 //\r
93 // See whether Identify Data word 64 - 70 are valid\r
94 //\r
95 if ((IdentifyData->AtaData.field_validity & 0x02) == 0x02) {\r
96\r
97 AdvancedPioMode = IdentifyData->AtaData.advanced_pio_modes;\r
98 DEBUG ((EFI_D_INFO, "CalculateBestPioMode: AdvancedPioMode = %x\n", AdvancedPioMode));\r
99\r
100 for (Index = 0; Index < 8; Index++) {\r
101 if ((AdvancedPioMode & 0x01) != 0) {\r
102 Temp = Index;\r
103 }\r
104\r
105 AdvancedPioMode >>= 1;\r
106 }\r
107\r
108 //\r
109 // If Temp is modified, mean the advanced_pio_modes is not zero;\r
110 // if Temp is not modified, mean there is no advanced PIO mode supported,\r
111 // the best PIO Mode is the value in pio_cycle_timing.\r
112 //\r
113 if (Temp != 0xff) {\r
114 AdvancedPioMode = (UINT16) (Temp + 3);\r
115 } else {\r
116 AdvancedPioMode = PioMode;\r
117 }\r
118\r
119 //\r
120 // Limit the PIO mode to at most PIO4.\r
121 //\r
122 PioMode = (UINT16) MIN (AdvancedPioMode, 4);\r
123\r
124 MinimumPioCycleTime = IdentifyData->AtaData.min_pio_cycle_time_with_flow_control;\r
125\r
126 if (MinimumPioCycleTime <= 120) {\r
127 PioMode = (UINT16) MIN (4, PioMode);\r
128 } else if (MinimumPioCycleTime <= 180) {\r
129 PioMode = (UINT16) MIN (3, PioMode);\r
130 } else if (MinimumPioCycleTime <= 240) {\r
131 PioMode = (UINT16) MIN (2, PioMode);\r
132 } else {\r
133 PioMode = 0;\r
134 }\r
135\r
136 //\r
137 // Degrade the PIO mode if the mode has been disqualified\r
138 //\r
139 if (DisPioMode != NULL) {\r
140 if (*DisPioMode < 2) {\r
141 return EFI_UNSUPPORTED; // no mode below ATA_PIO_MODE_BELOW_2\r
142 }\r
143\r
144 if (PioMode >= *DisPioMode) {\r
145 PioMode = (UINT16) (*DisPioMode - 1);\r
146 }\r
147 }\r
148\r
149 if (PioMode < 2) {\r
150 *SelectedMode = 1; // ATA_PIO_MODE_BELOW_2;\r
151 } else {\r
152 *SelectedMode = PioMode; // ATA_PIO_MODE_2 to ATA_PIO_MODE_4;\r
153 }\r
154\r
155 } else {\r
156 //\r
157 // Identify Data word 64 - 70 are not valid\r
158 // Degrade the PIO mode if the mode has been disqualified\r
159 //\r
160 if (DisPioMode != NULL) {\r
161 if (*DisPioMode < 2) {\r
162 return EFI_UNSUPPORTED; // no mode below ATA_PIO_MODE_BELOW_2\r
163 }\r
164\r
165 if (PioMode == *DisPioMode) {\r
166 PioMode--;\r
167 }\r
168 }\r
169\r
170 if (PioMode < 2) {\r
171 *SelectedMode = 1; // ATA_PIO_MODE_BELOW_2;\r
172 } else {\r
173 *SelectedMode = 2; // ATA_PIO_MODE_2;\r
174 }\r
175\r
176 }\r
177\r
178 return EFI_SUCCESS;\r
179}\r
180\r
181/**\r
182 This function is used to calculate the best UDMA mode supported by specific IDE device\r
183\r
184 @param IdentifyData The identify data of specific IDE device.\r
185 @param DisUDmaMode Disqualified UDMA modes collection.\r
186 @param SelectedMode Available UDMA modes collection.\r
187\r
188 @retval EFI_SUCCESS Best UDMA modes are returned.\r
189 @retval EFI_UNSUPPORTED The device doesn't support UDMA mode,\r
190 or all supported modes have been disqualified.\r
191**/\r
192EFI_STATUS\r
193CalculateBestUdmaMode (\r
194 IN EFI_IDENTIFY_DATA *IdentifyData,\r
195 IN UINT16 *DisUDmaMode OPTIONAL,\r
196 OUT UINT16 *SelectedMode\r
197 )\r
198{\r
199 UINT16 TempMode;\r
200 UINT16 DeviceUDmaMode;\r
201\r
202 DeviceUDmaMode = 0;\r
203\r
204 //\r
205 // Check whether the WORD 88 (supported UltraDMA by drive) is valid\r
206 //\r
207 if ((IdentifyData->AtaData.field_validity & 0x04) == 0x00) {\r
208 return EFI_UNSUPPORTED;\r
209 }\r
210\r
211 DeviceUDmaMode = IdentifyData->AtaData.ultra_dma_mode;\r
212 DEBUG ((EFI_D_INFO, "CalculateBestUdmaMode: DeviceUDmaMode = %x\n", DeviceUDmaMode));\r
213 DeviceUDmaMode &= 0x3f;\r
214 TempMode = 0; // initialize it to UDMA-0\r
215\r
216 while ((DeviceUDmaMode >>= 1) != 0) {\r
217 TempMode++;\r
218 }\r
219\r
220 //\r
221 // Degrade the UDMA mode if the mode has been disqualified\r
222 //\r
223 if (DisUDmaMode != NULL) {\r
224 if (*DisUDmaMode == 0) {\r
225 *SelectedMode = 0;\r
226 return EFI_UNSUPPORTED; // no mode below ATA_UDMA_MODE_0\r
227 }\r
228\r
229 if (TempMode >= *DisUDmaMode) {\r
230 TempMode = (UINT16) (*DisUDmaMode - 1);\r
231 }\r
232 }\r
233\r
234 //\r
235 // Possible returned mode is between ATA_UDMA_MODE_0 and ATA_UDMA_MODE_5\r
236 //\r
237 *SelectedMode = TempMode;\r
238\r
239 return EFI_SUCCESS;\r
240}\r
241\r
242/**\r
243 The Entry Point of module. It follows the standard UEFI driver model.\r
244\r
245 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
246 @param[in] SystemTable A pointer to the EFI System Table.\r
247 \r
248 @retval EFI_SUCCESS The entry point is executed successfully.\r
249 @retval other Some error occurs when executing this entry point.\r
250\r
251**/\r
252EFI_STATUS\r
253EFIAPI\r
254InitializeSataControllerDriver (\r
255 IN EFI_HANDLE ImageHandle,\r
256 IN EFI_SYSTEM_TABLE *SystemTable\r
257 )\r
258{\r
259 EFI_STATUS Status;\r
260\r
261 //\r
262 // Install driver model protocol(s).\r
263 //\r
264 Status = EfiLibInstallDriverBindingComponentName2 (\r
265 ImageHandle,\r
266 SystemTable,\r
267 &gSataControllerDriverBinding,\r
268 ImageHandle,\r
269 &gSataControllerComponentName,\r
270 &gSataControllerComponentName2\r
271 );\r
272 ASSERT_EFI_ERROR (Status);\r
273\r
274 return Status;\r
275}\r
276\r
277/**\r
278 Supported function of Driver Binding protocol for this driver.\r
279 Test to see if this driver supports ControllerHandle.\r
280\r
281 @param This Protocol instance pointer.\r
282 @param Controller Handle of device to test.\r
283 @param RemainingDevicePath A pointer to the device path.\r
284 it should be ignored by device driver.\r
285\r
286 @retval EFI_SUCCESS This driver supports this device.\r
287 @retval EFI_ALREADY_STARTED This driver is already running on this device.\r
288 @retval other This driver does not support this device.\r
289\r
290**/\r
291EFI_STATUS\r
292EFIAPI\r
293SataControllerSupported (\r
294 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
295 IN EFI_HANDLE Controller,\r
296 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
297 )\r
298{\r
299 EFI_STATUS Status;\r
300 EFI_PCI_IO_PROTOCOL *PciIo;\r
301 PCI_TYPE00 PciData;\r
302\r
303 //\r
304 // Attempt to open PCI I/O Protocol\r
305 //\r
306 Status = gBS->OpenProtocol (\r
307 Controller,\r
308 &gEfiPciIoProtocolGuid,\r
309 (VOID **) &PciIo,\r
310 This->DriverBindingHandle,\r
311 Controller,\r
312 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
313 );\r
314 if (EFI_ERROR (Status)) {\r
315 return Status;\r
316 }\r
317\r
318 //\r
319 // Now further check the PCI header: Base Class (offset 0x0B) and\r
320 // Sub Class (offset 0x0A). This controller should be an SATA controller\r
321 //\r
322 Status = PciIo->Pci.Read (\r
323 PciIo,\r
324 EfiPciIoWidthUint8,\r
325 PCI_CLASSCODE_OFFSET,\r
326 sizeof (PciData.Hdr.ClassCode),\r
327 PciData.Hdr.ClassCode\r
328 );\r
329 if (EFI_ERROR (Status)) {\r
330 return EFI_UNSUPPORTED;\r
331 }\r
332\r
333 if (IS_PCI_IDE (&PciData) || IS_PCI_SATADPA (&PciData)) {\r
334 return EFI_SUCCESS;\r
335 }\r
336\r
337 return EFI_UNSUPPORTED;\r
338}\r
339\r
340/**\r
341 This routine is called right after the .Supported() called and \r
342 Start this driver on ControllerHandle.\r
343\r
344 @param This Protocol instance pointer.\r
345 @param Controller Handle of device to bind driver to.\r
346 @param RemainingDevicePath A pointer to the device path.\r
347 it should be ignored by device driver.\r
348\r
349 @retval EFI_SUCCESS This driver is added to this device.\r
350 @retval EFI_ALREADY_STARTED This driver is already running on this device.\r
351 @retval other Some error occurs when binding this driver to this device.\r
352\r
353**/\r
354EFI_STATUS\r
355EFIAPI\r
356SataControllerStart (\r
357 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
358 IN EFI_HANDLE Controller,\r
359 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
360 )\r
361{\r
362 EFI_STATUS Status;\r
363 EFI_PCI_IO_PROTOCOL *PciIo;\r
364 PCI_TYPE00 PciData;\r
365 EFI_SATA_CONTROLLER_PRIVATE_DATA *Private;\r
366 UINT32 Data32;\r
367 UINTN TotalCount;\r
24fee052 368 UINT64 Supports;\r
fda951df
FT
369\r
370 DEBUG ((EFI_D_INFO, "SataControllerStart start\n"));\r
371\r
372 Private = NULL;\r
373\r
374 //\r
375 // Now test and open PCI I/O Protocol\r
376 //\r
377 Status = gBS->OpenProtocol (\r
378 Controller,\r
379 &gEfiPciIoProtocolGuid,\r
380 (VOID **) &PciIo,\r
381 This->DriverBindingHandle,\r
382 Controller,\r
383 EFI_OPEN_PROTOCOL_BY_DRIVER\r
384 );\r
385 if (EFI_ERROR (Status)) {\r
386 DEBUG ((EFI_D_ERROR, "SataControllerStart error. return status = %r\n", Status));\r
387 return Status;\r
388 }\r
389\r
390 //\r
391 // Allocate Sata Private Data structure\r
392 //\r
393 Private = AllocateZeroPool (sizeof (EFI_SATA_CONTROLLER_PRIVATE_DATA));\r
394 if (Private == NULL) {\r
395 Status = EFI_OUT_OF_RESOURCES;\r
396 goto Done;\r
397 }\r
398\r
399 //\r
400 // Initialize Sata Private Data\r
401 //\r
402 Private->Signature = SATA_CONTROLLER_SIGNATURE;\r
403 Private->PciIo = PciIo;\r
404 Private->IdeInit.GetChannelInfo = IdeInitGetChannelInfo;\r
405 Private->IdeInit.NotifyPhase = IdeInitNotifyPhase;\r
406 Private->IdeInit.SubmitData = IdeInitSubmitData;\r
407 Private->IdeInit.DisqualifyMode = IdeInitDisqualifyMode;\r
408 Private->IdeInit.CalculateMode = IdeInitCalculateMode;\r
409 Private->IdeInit.SetTiming = IdeInitSetTiming;\r
410 Private->IdeInit.EnumAll = SATA_ENUMER_ALL;\r
24fee052
SM
411 Private->PciAttributesChanged = FALSE;\r
412\r
413 //\r
414 // Save original PCI attributes\r
415 //\r
416 Status = PciIo->Attributes (\r
417 PciIo,\r
418 EfiPciIoAttributeOperationGet,\r
419 0,\r
420 &Private->OriginalPciAttributes\r
421 );\r
422 if (EFI_ERROR (Status)) {\r
423 goto Done;\r
424 }\r
425\r
426 DEBUG ((\r
427 EFI_D_INFO,\r
428 "Original PCI Attributes = 0x%llx\n",\r
429 Private->OriginalPciAttributes\r
430 ));\r
431\r
432 Status = PciIo->Attributes (\r
433 PciIo,\r
434 EfiPciIoAttributeOperationSupported,\r
435 0,\r
436 &Supports\r
437 );\r
438 if (EFI_ERROR (Status)) {\r
439 goto Done;\r
440 }\r
441\r
442 DEBUG ((EFI_D_INFO, "Supported PCI Attributes = 0x%llx\n", Supports));\r
443\r
444 Supports &= (UINT64)EFI_PCI_DEVICE_ENABLE;\r
445 Status = PciIo->Attributes (\r
446 PciIo,\r
447 EfiPciIoAttributeOperationEnable,\r
448 Supports,\r
449 NULL\r
450 );\r
451 if (EFI_ERROR (Status)) {\r
452 goto Done;\r
453 }\r
454\r
455 DEBUG ((EFI_D_INFO, "Enabled PCI Attributes = 0x%llx\n", Supports));\r
456 Private->PciAttributesChanged = TRUE;\r
fda951df
FT
457\r
458 Status = PciIo->Pci.Read (\r
459 PciIo,\r
460 EfiPciIoWidthUint8,\r
461 PCI_CLASSCODE_OFFSET,\r
462 sizeof (PciData.Hdr.ClassCode),\r
463 PciData.Hdr.ClassCode\r
464 );\r
24fee052
SM
465 if (EFI_ERROR (Status)) {\r
466 ASSERT (FALSE);\r
467 goto Done;\r
468 }\r
fda951df
FT
469\r
470 if (IS_PCI_IDE (&PciData)) {\r
471 Private->IdeInit.ChannelCount = IDE_MAX_CHANNEL;\r
472 Private->DeviceCount = IDE_MAX_DEVICES;\r
473 } else if (IS_PCI_SATADPA (&PciData)) {\r
474 //\r
475 // Read Host Capability Register(CAP) to get Number of Ports(NPS) and Supports Port Multiplier(SPM)\r
476 // NPS is 0's based value indicating the maximum number of ports supported by the HBA silicon.\r
477 // A maximum of 32 ports can be supported. A value of '0h', indicating one port, is the minimum requirement.\r
478 //\r
479 Data32 = AhciReadReg (PciIo, R_AHCI_CAP);\r
480 Private->IdeInit.ChannelCount = (UINT8) ((Data32 & B_AHCI_CAP_NPS) + 1);\r
481 Private->DeviceCount = AHCI_MAX_DEVICES;\r
482 if ((Data32 & B_AHCI_CAP_SPM) == B_AHCI_CAP_SPM) {\r
483 Private->DeviceCount = AHCI_MULTI_MAX_DEVICES;\r
484 }\r
485 }\r
486\r
487 TotalCount = (UINTN) (Private->IdeInit.ChannelCount) * (UINTN) (Private->DeviceCount);\r
488 Private->DisqualifiedModes = AllocateZeroPool ((sizeof (EFI_ATA_COLLECTIVE_MODE)) * TotalCount);\r
489 if (Private->DisqualifiedModes == NULL) {\r
490 Status = EFI_OUT_OF_RESOURCES;\r
491 goto Done;\r
492 }\r
493\r
494 Private->IdentifyData = AllocateZeroPool ((sizeof (EFI_IDENTIFY_DATA)) * TotalCount);\r
495 if (Private->IdentifyData == NULL) {\r
496 Status = EFI_OUT_OF_RESOURCES;\r
497 goto Done;\r
498 }\r
499\r
500 Private->IdentifyValid = AllocateZeroPool ((sizeof (BOOLEAN)) * TotalCount);\r
501 if (Private->IdentifyValid == NULL) {\r
502 Status = EFI_OUT_OF_RESOURCES;\r
503 goto Done;\r
504 }\r
505\r
506 //\r
507 // Install IDE Controller Init Protocol to this instance\r
508 //\r
509 Status = gBS->InstallMultipleProtocolInterfaces (\r
510 &Controller,\r
511 &gEfiIdeControllerInitProtocolGuid,\r
512 &(Private->IdeInit),\r
513 NULL\r
514 );\r
515\r
516Done:\r
517 if (EFI_ERROR (Status)) {\r
518\r
519 gBS->CloseProtocol (\r
520 Controller,\r
521 &gEfiPciIoProtocolGuid,\r
522 This->DriverBindingHandle,\r
523 Controller\r
524 );\r
525 if (Private != NULL) {\r
526 if (Private->DisqualifiedModes != NULL) {\r
527 FreePool (Private->DisqualifiedModes);\r
528 }\r
529 if (Private->IdentifyData != NULL) {\r
530 FreePool (Private->IdentifyData);\r
531 }\r
532 if (Private->IdentifyValid != NULL) {\r
533 FreePool (Private->IdentifyValid);\r
534 }\r
24fee052
SM
535 if (Private->PciAttributesChanged) {\r
536 //\r
537 // Restore original PCI attributes\r
538 //\r
539 PciIo->Attributes (\r
540 PciIo,\r
541 EfiPciIoAttributeOperationSet,\r
542 Private->OriginalPciAttributes,\r
543 NULL\r
544 );\r
545 }\r
fda951df
FT
546 FreePool (Private);\r
547 }\r
548 }\r
549\r
550 DEBUG ((EFI_D_INFO, "SataControllerStart end with %r\n", Status));\r
551\r
552 return Status;\r
553}\r
554\r
555/**\r
556 Stop this driver on ControllerHandle.\r
557\r
558 @param This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
559 @param Controller A handle to the device being stopped.\r
560 @param NumberOfChildren The number of child device handles in ChildHandleBuffer.\r
561 @param ChildHandleBuffer An array of child handles to be freed. \r
562\r
563 @retval EFI_SUCCESS This driver is removed from this device.\r
564 @retval other Some error occurs when removing this driver from this device.\r
565\r
566**/\r
567EFI_STATUS\r
568EFIAPI\r
569SataControllerStop (\r
570 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
571 IN EFI_HANDLE Controller,\r
572 IN UINTN NumberOfChildren,\r
573 IN EFI_HANDLE *ChildHandleBuffer\r
574 )\r
575{\r
576 EFI_STATUS Status;\r
577 EFI_IDE_CONTROLLER_INIT_PROTOCOL *IdeInit;\r
578 EFI_SATA_CONTROLLER_PRIVATE_DATA *Private;\r
579\r
580 //\r
581 // Open the produced protocol\r
582 //\r
583 Status = gBS->OpenProtocol (\r
584 Controller,\r
585 &gEfiIdeControllerInitProtocolGuid,\r
586 (VOID **) &IdeInit,\r
587 This->DriverBindingHandle,\r
588 Controller,\r
589 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
590 );\r
591 if (EFI_ERROR (Status)) {\r
592 return EFI_UNSUPPORTED;\r
593 }\r
594\r
595 Private = SATA_CONTROLLER_PRIVATE_DATA_FROM_THIS (IdeInit);\r
596 ASSERT (Private != NULL);\r
597\r
598 //\r
599 // Uninstall the IDE Controller Init Protocol from this instance\r
600 //\r
601 Status = gBS->UninstallMultipleProtocolInterfaces (\r
602 Controller,\r
603 &gEfiIdeControllerInitProtocolGuid,\r
604 &(Private->IdeInit),\r
605 NULL\r
606 );\r
607 if (EFI_ERROR (Status)) {\r
608 return Status;\r
609 }\r
610\r
611 if (Private != NULL) {\r
612 if (Private->DisqualifiedModes != NULL) {\r
613 FreePool (Private->DisqualifiedModes);\r
614 }\r
615 if (Private->IdentifyData != NULL) {\r
616 FreePool (Private->IdentifyData);\r
617 }\r
618 if (Private->IdentifyValid != NULL) {\r
619 FreePool (Private->IdentifyValid);\r
620 }\r
24fee052
SM
621 if (Private->PciAttributesChanged) {\r
622 //\r
623 // Restore original PCI attributes\r
624 //\r
625 Private->PciIo->Attributes (\r
626 Private->PciIo,\r
627 EfiPciIoAttributeOperationSet,\r
628 Private->OriginalPciAttributes,\r
629 NULL\r
630 );\r
631 }\r
fda951df
FT
632 FreePool (Private);\r
633 }\r
634\r
635 //\r
636 // Close protocols opened by Sata Controller driver\r
637 //\r
638 return gBS->CloseProtocol (\r
639 Controller,\r
640 &gEfiPciIoProtocolGuid,\r
641 This->DriverBindingHandle,\r
642 Controller\r
643 );\r
644}\r
645\r
646/**\r
647 Calculate the flat array subscript of a (Channel, Device) pair.\r
648\r
649 @param[in] Private The private data structure corresponding to the\r
650 SATA controller that attaches the device for\r
651 which the flat array subscript is being\r
652 calculated.\r
653\r
654 @param[in] Channel The channel (ie. port) number on the SATA\r
655 controller that the device is attached to.\r
656\r
657 @param[in] Device The device number on the channel.\r
658\r
659 @return The flat array subscript suitable for indexing DisqualifiedModes,\r
660 IdentifyData, and IdentifyValid.\r
661**/\r
662STATIC\r
663UINTN\r
664FlatDeviceIndex (\r
665 IN CONST EFI_SATA_CONTROLLER_PRIVATE_DATA *Private,\r
666 IN UINTN Channel,\r
667 IN UINTN Device\r
668 )\r
669{\r
670 ASSERT (Private != NULL);\r
671 ASSERT (Channel < Private->IdeInit.ChannelCount);\r
672 ASSERT (Device < Private->DeviceCount);\r
673\r
674 return Channel * Private->DeviceCount + Device;\r
675}\r
676\r
677//\r
678// Interface functions of IDE_CONTROLLER_INIT protocol\r
679//\r
680/**\r
681 Returns the information about the specified IDE channel.\r
682 \r
683 This function can be used to obtain information about a particular IDE channel.\r
684 The driver entity uses this information during the enumeration process. \r
685 \r
686 If Enabled is set to FALSE, the driver entity will not scan the channel. Note \r
687 that it will not prevent an operating system driver from scanning the channel.\r
688 \r
689 For most of today's controllers, MaxDevices will either be 1 or 2. For SATA \r
690 controllers, this value will always be 1. SATA configurations can contain SATA \r
691 port multipliers. SATA port multipliers behave like SATA bridges and can support\r
692 up to 16 devices on the other side. If a SATA port out of the IDE controller \r
693 is connected to a port multiplier, MaxDevices will be set to the number of SATA \r
694 devices that the port multiplier supports. Because today's port multipliers \r
695 support up to fifteen SATA devices, this number can be as large as fifteen. The IDE \r
696 bus driver is required to scan for the presence of port multipliers behind an SATA \r
697 controller and enumerate up to MaxDevices number of devices behind the port \r
698 multiplier. \r
699 \r
700 In this context, the devices behind a port multiplier constitute a channel. \r
701 \r
702 @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.\r
703 @param[in] Channel Zero-based channel number.\r
704 @param[out] Enabled TRUE if this channel is enabled. Disabled channels \r
705 are not scanned to see if any devices are present.\r
706 @param[out] MaxDevices The maximum number of IDE devices that the bus driver\r
707 can expect on this channel. For the ATA/ATAPI \r
708 specification, version 6, this number will either be \r
709 one or two. For Serial ATA (SATA) configurations with a \r
710 port multiplier, this number can be as large as fifteen.\r
711\r
712 @retval EFI_SUCCESS Information was returned without any errors.\r
713 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).\r
714\r
715**/\r
716EFI_STATUS\r
717EFIAPI\r
718IdeInitGetChannelInfo (\r
719 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL *This,\r
720 IN UINT8 Channel,\r
721 OUT BOOLEAN *Enabled,\r
722 OUT UINT8 *MaxDevices\r
723 )\r
724{\r
725 EFI_SATA_CONTROLLER_PRIVATE_DATA *Private;\r
726 Private = SATA_CONTROLLER_PRIVATE_DATA_FROM_THIS (This);\r
727 ASSERT (Private != NULL);\r
728\r
729 if (Channel < This->ChannelCount) {\r
730 *Enabled = TRUE;\r
731 *MaxDevices = Private->DeviceCount;\r
732 return EFI_SUCCESS;\r
733 }\r
734\r
735 *Enabled = FALSE;\r
736 return EFI_INVALID_PARAMETER;\r
737}\r
738\r
739/**\r
740 The notifications from the driver entity that it is about to enter a certain\r
741 phase of the IDE channel enumeration process.\r
742 \r
743 This function can be used to notify the IDE controller driver to perform \r
744 specific actions, including any chipset-specific initialization, so that the \r
745 chipset is ready to enter the next phase. Seven notification points are defined \r
746 at this time. \r
747 \r
748 More synchronization points may be added as required in the future. \r
749\r
750 @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL\r
751 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 *Private;\r
824 UINTN DeviceIndex;\r
825\r
826 Private = SATA_CONTROLLER_PRIVATE_DATA_FROM_THIS (This);\r
827 ASSERT (Private != NULL);\r
828\r
829 if ((Channel >= This->ChannelCount) || (Device >= Private->DeviceCount)) {\r
830 return EFI_INVALID_PARAMETER;\r
831 }\r
832\r
833 DeviceIndex = FlatDeviceIndex (Private, 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 &(Private->IdentifyData[DeviceIndex]),\r
841 IdentifyData,\r
842 sizeof (EFI_IDENTIFY_DATA)\r
843 );\r
844\r
845 Private->IdentifyValid[DeviceIndex] = TRUE;\r
846 } else {\r
847 Private->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 *Private;\r
903 UINTN DeviceIndex;\r
904\r
905 Private = SATA_CONTROLLER_PRIVATE_DATA_FROM_THIS (This);\r
906 ASSERT (Private != NULL);\r
907\r
908 if ((Channel >= This->ChannelCount) || (BadModes == NULL) || (Device >= Private->DeviceCount)) {\r
909 return EFI_INVALID_PARAMETER;\r
910 }\r
911\r
912 DeviceIndex = FlatDeviceIndex (Private, 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 &(Private->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 *Private;\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 Private = SATA_CONTROLLER_PRIVATE_DATA_FROM_THIS (This);\r
999 ASSERT (Private != NULL);\r
1000\r
1001 if ((Channel >= This->ChannelCount) || (SupportedModes == NULL) || (Device >= Private->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 (Private, Channel, Device);\r
1012\r
1013 IdentifyData = &(Private->IdentifyData[DeviceIndex]);\r
1014 IdentifyValid = Private->IdentifyValid[DeviceIndex];\r
1015 DisqualifiedModes = &(Private->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
1094\r