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