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