SataController.c 37 KB

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