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