SataController.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /** @file
  2. Header file for Sata Controller driver.
  3. Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #ifndef _SATA_CONTROLLER_H_
  7. #define _SATA_CONTROLLER_H_
  8. #include <Uefi.h>
  9. #include <Protocol/ComponentName.h>
  10. #include <Protocol/DriverBinding.h>
  11. #include <Protocol/PciIo.h>
  12. #include <Protocol/IdeControllerInit.h>
  13. #include <Library/UefiDriverEntryPoint.h>
  14. #include <Library/DebugLib.h>
  15. #include <Library/UefiLib.h>
  16. #include <Library/BaseLib.h>
  17. #include <Library/BaseMemoryLib.h>
  18. #include <Library/MemoryAllocationLib.h>
  19. #include <Library/UefiBootServicesTableLib.h>
  20. #include <IndustryStandard/Pci.h>
  21. //
  22. // Global Variables definitions
  23. //
  24. extern EFI_DRIVER_BINDING_PROTOCOL gSataControllerDriverBinding;
  25. extern EFI_COMPONENT_NAME_PROTOCOL gSataControllerComponentName;
  26. extern EFI_COMPONENT_NAME2_PROTOCOL gSataControllerComponentName2;
  27. #define AHCI_BAR_INDEX 0x05
  28. #define R_AHCI_CAP 0x0
  29. #define B_AHCI_CAP_NPS (BIT4 | BIT3 | BIT2 | BIT1 | BIT0) // Number of Ports
  30. #define B_AHCI_CAP_SPM BIT17 // Supports Port Multiplier
  31. ///
  32. /// AHCI each channel can have up to 1 device
  33. ///
  34. #define AHCI_MAX_DEVICES 0x01
  35. ///
  36. /// AHCI each channel can have 15 devices in the presence of a multiplier
  37. ///
  38. #define AHCI_MULTI_MAX_DEVICES 0x0F
  39. ///
  40. /// IDE supports 2 channel max
  41. ///
  42. #define IDE_MAX_CHANNEL 0x02
  43. ///
  44. /// IDE supports 2 devices max
  45. ///
  46. #define IDE_MAX_DEVICES 0x02
  47. #define SATA_ENUMER_ALL FALSE
  48. //
  49. // Sata Controller driver private data structure
  50. //
  51. #define SATA_CONTROLLER_SIGNATURE SIGNATURE_32('S','A','T','A')
  52. typedef struct _EFI_SATA_CONTROLLER_PRIVATE_DATA {
  53. //
  54. // Standard signature used to identify Sata Controller private data
  55. //
  56. UINT32 Signature;
  57. //
  58. // Protocol instance of IDE_CONTROLLER_INIT produced by this driver
  59. //
  60. EFI_IDE_CONTROLLER_INIT_PROTOCOL IdeInit;
  61. //
  62. // Copy of protocol pointers used by this driver
  63. //
  64. EFI_PCI_IO_PROTOCOL *PciIo;
  65. //
  66. // Original PCI attributes
  67. //
  68. UINT64 OriginalPciAttributes;
  69. //
  70. // The number of devices that are supported by this channel
  71. //
  72. UINT8 DeviceCount;
  73. //
  74. // The highest disqulified mode for each attached device,
  75. // From ATA/ATAPI spec, if a mode is not supported,
  76. // the modes higher than it is also not supported
  77. //
  78. EFI_ATA_COLLECTIVE_MODE *DisqualifiedModes;
  79. //
  80. // A copy of EFI_IDENTIFY_DATA data for each attached SATA device and its flag
  81. //
  82. EFI_IDENTIFY_DATA *IdentifyData;
  83. BOOLEAN *IdentifyValid;
  84. } EFI_SATA_CONTROLLER_PRIVATE_DATA;
  85. #define SATA_CONTROLLER_PRIVATE_DATA_FROM_THIS(a) CR(a, EFI_SATA_CONTROLLER_PRIVATE_DATA, IdeInit, SATA_CONTROLLER_SIGNATURE)
  86. //
  87. // Driver binding functions declaration
  88. //
  89. /**
  90. Supported function of Driver Binding protocol for this driver.
  91. Test to see if this driver supports ControllerHandle.
  92. @param This Protocol instance pointer.
  93. @param Controller Handle of device to test.
  94. @param RemainingDevicePath A pointer to the device path. Should be ignored by
  95. device driver.
  96. @retval EFI_SUCCESS This driver supports this device.
  97. @retval EFI_ALREADY_STARTED This driver is already running on this device.
  98. @retval other This driver does not support this device.
  99. **/
  100. EFI_STATUS
  101. EFIAPI
  102. SataControllerSupported (
  103. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  104. IN EFI_HANDLE Controller,
  105. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
  106. )
  107. ;
  108. /**
  109. This routine is called right after the .Supported() called and
  110. Start this driver on ControllerHandle.
  111. @param This Protocol instance pointer.
  112. @param Controller Handle of device to bind driver to.
  113. @param RemainingDevicePath A pointer to the device path. Should be ignored by
  114. device driver.
  115. @retval EFI_SUCCESS This driver is added to this device.
  116. @retval EFI_ALREADY_STARTED This driver is already running on this device.
  117. @retval other Some error occurs when binding this driver to this device.
  118. **/
  119. EFI_STATUS
  120. EFIAPI
  121. SataControllerStart (
  122. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  123. IN EFI_HANDLE Controller,
  124. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
  125. )
  126. ;
  127. /**
  128. Stop this driver on ControllerHandle.
  129. @param This Protocol instance pointer.
  130. @param Controller Handle of device to stop driver on.
  131. @param NumberOfChildren Not used.
  132. @param ChildHandleBuffer Not used.
  133. @retval EFI_SUCCESS This driver is removed from this device.
  134. @retval other Some error occurs when removing this driver from this device.
  135. **/
  136. EFI_STATUS
  137. EFIAPI
  138. SataControllerStop (
  139. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  140. IN EFI_HANDLE Controller,
  141. IN UINTN NumberOfChildren,
  142. IN EFI_HANDLE *ChildHandleBuffer
  143. )
  144. ;
  145. //
  146. // IDE controller init functions declaration
  147. //
  148. /**
  149. Returns the information about the specified IDE channel.
  150. This function can be used to obtain information about a particular IDE channel.
  151. The driver entity uses this information during the enumeration process.
  152. If Enabled is set to FALSE, the driver entity will not scan the channel. Note
  153. that it will not prevent an operating system driver from scanning the channel.
  154. For most of today's controllers, MaxDevices will either be 1 or 2. For SATA
  155. controllers, this value will always be 1. SATA configurations can contain SATA
  156. port multipliers. SATA port multipliers behave like SATA bridges and can support
  157. up to 16 devices on the other side. If a SATA port out of the IDE controller
  158. is connected to a port multiplier, MaxDevices will be set to the number of SATA
  159. devices that the port multiplier supports. Because today's port multipliers
  160. support up to fifteen SATA devices, this number can be as large as fifteen. The IDE
  161. bus driver is required to scan for the presence of port multipliers behind an SATA
  162. controller and enumerate up to MaxDevices number of devices behind the port
  163. multiplier.
  164. In this context, the devices behind a port multiplier constitute a channel.
  165. @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
  166. @param[in] Channel Zero-based channel number.
  167. @param[out] Enabled TRUE if this channel is enabled. Disabled channels
  168. are not scanned to see if any devices are present.
  169. @param[out] MaxDevices The maximum number of IDE devices that the bus driver
  170. can expect on this channel. For the ATA/ATAPI
  171. specification, version 6, this number will either be
  172. one or two. For Serial ATA (SATA) configurations with a
  173. port multiplier, this number can be as large as fifteen.
  174. @retval EFI_SUCCESS Information was returned without any errors.
  175. @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
  176. **/
  177. EFI_STATUS
  178. EFIAPI
  179. IdeInitGetChannelInfo (
  180. IN EFI_IDE_CONTROLLER_INIT_PROTOCOL *This,
  181. IN UINT8 Channel,
  182. OUT BOOLEAN *Enabled,
  183. OUT UINT8 *MaxDevices
  184. )
  185. ;
  186. /**
  187. The notifications from the driver entity that it is about to enter a certain
  188. phase of the IDE channel enumeration process.
  189. This function can be used to notify the IDE controller driver to perform
  190. specific actions, including any chipset-specific initialization, so that the
  191. chipset is ready to enter the next phase. Seven notification points are defined
  192. at this time.
  193. More synchronization points may be added as required in the future.
  194. @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
  195. @param[in] Phase The phase during enumeration.
  196. @param[in] Channel Zero-based channel number.
  197. @retval EFI_SUCCESS The notification was accepted without any errors.
  198. @retval EFI_UNSUPPORTED Phase is not supported.
  199. @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
  200. @retval EFI_NOT_READY This phase cannot be entered at this time; for
  201. example, an attempt was made to enter a Phase
  202. without having entered one or more previous
  203. Phase.
  204. **/
  205. EFI_STATUS
  206. EFIAPI
  207. IdeInitNotifyPhase (
  208. IN EFI_IDE_CONTROLLER_INIT_PROTOCOL *This,
  209. IN EFI_IDE_CONTROLLER_ENUM_PHASE Phase,
  210. IN UINT8 Channel
  211. )
  212. ;
  213. /**
  214. Submits the device information to the IDE controller driver.
  215. This function is used by the driver entity to pass detailed information about
  216. a particular device to the IDE controller driver. The driver entity obtains
  217. this information by issuing an ATA or ATAPI IDENTIFY_DEVICE command. IdentifyData
  218. is the pointer to the response data buffer. The IdentifyData buffer is owned
  219. by the driver entity, and the IDE controller driver must make a local copy
  220. of the entire buffer or parts of the buffer as needed. The original IdentifyData
  221. buffer pointer may not be valid when
  222. - EFI_IDE_CONTROLLER_INIT_PROTOCOL.CalculateMode() or
  223. - EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode() is called at a later point.
  224. The IDE controller driver may consult various fields of EFI_IDENTIFY_DATA to
  225. compute the optimum mode for the device. These fields are not limited to the
  226. timing information. For example, an implementation of the IDE controller driver
  227. may examine the vendor and type/mode field to match known bad drives.
  228. The driver entity may submit drive information in any order, as long as it
  229. submits information for all the devices belonging to the enumeration group
  230. before EFI_IDE_CONTROLLER_INIT_PROTOCOL.CalculateMode() is called for any device
  231. in that enumeration group. If a device is absent, EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()
  232. should be called with IdentifyData set to NULL. The IDE controller driver may
  233. not have any other mechanism to know whether a device is present or not. Therefore,
  234. setting IdentifyData to NULL does not constitute an error condition.
  235. EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData() can be called only once for a
  236. given (Channel, Device) pair.
  237. @param[in] This A pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
  238. @param[in] Channel Zero-based channel number.
  239. @param[in] Device Zero-based device number on the Channel.
  240. @param[in] IdentifyData The device's response to the ATA IDENTIFY_DEVICE command.
  241. @retval EFI_SUCCESS The information was accepted without any errors.
  242. @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
  243. @retval EFI_INVALID_PARAMETER Device is invalid.
  244. **/
  245. EFI_STATUS
  246. EFIAPI
  247. IdeInitSubmitData (
  248. IN EFI_IDE_CONTROLLER_INIT_PROTOCOL *This,
  249. IN UINT8 Channel,
  250. IN UINT8 Device,
  251. IN EFI_IDENTIFY_DATA *IdentifyData
  252. )
  253. ;
  254. /**
  255. Disqualifies specific modes for an IDE device.
  256. This function allows the driver entity or other drivers (such as platform
  257. drivers) to reject certain timing modes and request the IDE controller driver
  258. to recalculate modes. This function allows the driver entity and the IDE
  259. controller driver to negotiate the timings on a per-device basis. This function
  260. is useful in the case of drives that lie about their capabilities. An example
  261. is when the IDE device fails to accept the timing modes that are calculated
  262. by the IDE controller driver based on the response to the Identify Drive command.
  263. If the driver entity does not want to limit the ATA timing modes and leave that
  264. decision to the IDE controller driver, it can either not call this function for
  265. the given device or call this function and set the Valid flag to FALSE for all
  266. modes that are listed in EFI_ATA_COLLECTIVE_MODE.
  267. The driver entity may disqualify modes for a device in any order and any number
  268. of times.
  269. This function can be called multiple times to invalidate multiple modes of the
  270. same type (e.g., Programmed Input/Output [PIO] modes 3 and 4). See the ATA/ATAPI
  271. specification for more information on PIO modes.
  272. For Serial ATA (SATA) controllers, this member function can be used to disqualify
  273. a higher transfer rate mode on a given channel. For example, a platform driver
  274. may inform the IDE controller driver to not use second-generation (Gen2) speeds
  275. for a certain SATA drive.
  276. @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
  277. @param[in] Channel The zero-based channel number.
  278. @param[in] Device The zero-based device number on the Channel.
  279. @param[in] BadModes The modes that the device does not support and that
  280. should be disqualified.
  281. @retval EFI_SUCCESS The modes were accepted without any errors.
  282. @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
  283. @retval EFI_INVALID_PARAMETER Device is invalid.
  284. @retval EFI_INVALID_PARAMETER IdentifyData is NULL.
  285. **/
  286. EFI_STATUS
  287. EFIAPI
  288. IdeInitDisqualifyMode (
  289. IN EFI_IDE_CONTROLLER_INIT_PROTOCOL *This,
  290. IN UINT8 Channel,
  291. IN UINT8 Device,
  292. IN EFI_ATA_COLLECTIVE_MODE *BadModes
  293. )
  294. ;
  295. /**
  296. Returns the information about the optimum modes for the specified IDE device.
  297. This function is used by the driver entity to obtain the optimum ATA modes for
  298. a specific device. The IDE controller driver takes into account the following
  299. while calculating the mode:
  300. - The IdentifyData inputs to EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()
  301. - The BadModes inputs to EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode()
  302. The driver entity is required to call EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()
  303. for all the devices that belong to an enumeration group before calling
  304. EFI_IDE_CONTROLLER_INIT_PROTOCOL.CalculateMode() for any device in the same group.
  305. The IDE controller driver will use controller- and possibly platform-specific
  306. algorithms to arrive at SupportedModes. The IDE controller may base its
  307. decision on user preferences and other considerations as well. This function
  308. may be called multiple times because the driver entity may renegotiate the mode
  309. with the IDE controller driver using EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode().
  310. The driver entity may collect timing information for various devices in any
  311. order. The driver entity is responsible for making sure that all the dependencies
  312. are satisfied. For example, the SupportedModes information for device A that
  313. was previously returned may become stale after a call to
  314. EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode() for device B.
  315. The buffer SupportedModes is allocated by the callee because the caller does
  316. not necessarily know the size of the buffer. The type EFI_ATA_COLLECTIVE_MODE
  317. is defined in a way that allows for future extensibility and can be of variable
  318. length. This memory pool should be deallocated by the caller when it is no
  319. longer necessary.
  320. The IDE controller driver for a Serial ATA (SATA) controller can use this
  321. member function to force a lower speed (first-generation [Gen1] speeds on a
  322. second-generation [Gen2]-capable hardware). The IDE controller driver can
  323. also allow the driver entity to stay with the speed that has been negotiated
  324. by the physical layer.
  325. @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
  326. @param[in] Channel A zero-based channel number.
  327. @param[in] Device A zero-based device number on the Channel.
  328. @param[out] SupportedModes The optimum modes for the device.
  329. @retval EFI_SUCCESS SupportedModes was returned.
  330. @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
  331. @retval EFI_INVALID_PARAMETER Device is invalid.
  332. @retval EFI_INVALID_PARAMETER SupportedModes is NULL.
  333. @retval EFI_NOT_READY Modes cannot be calculated due to a lack of
  334. data. This error may happen if
  335. EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()
  336. and EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyData()
  337. were not called for at least one drive in the
  338. same enumeration group.
  339. **/
  340. EFI_STATUS
  341. EFIAPI
  342. IdeInitCalculateMode (
  343. IN EFI_IDE_CONTROLLER_INIT_PROTOCOL *This,
  344. IN UINT8 Channel,
  345. IN UINT8 Device,
  346. OUT EFI_ATA_COLLECTIVE_MODE **SupportedModes
  347. )
  348. ;
  349. /**
  350. Commands the IDE controller driver to program the IDE controller hardware
  351. so that the specified device can operate at the specified mode.
  352. This function is used by the driver entity to instruct the IDE controller
  353. driver to program the IDE controller hardware to the specified modes. This
  354. function can be called only once for a particular device. For a Serial ATA
  355. (SATA) Advanced Host Controller Interface (AHCI) controller, no controller-
  356. specific programming may be required.
  357. @param[in] This Pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
  358. @param[in] Channel Zero-based channel number.
  359. @param[in] Device Zero-based device number on the Channel.
  360. @param[in] Modes The modes to set.
  361. @retval EFI_SUCCESS The command was accepted without any errors.
  362. @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
  363. @retval EFI_INVALID_PARAMETER Device is invalid.
  364. @retval EFI_NOT_READY Modes cannot be set at this time due to lack of data.
  365. @retval EFI_DEVICE_ERROR Modes cannot be set due to hardware failure.
  366. The driver entity should not use this device.
  367. **/
  368. EFI_STATUS
  369. EFIAPI
  370. IdeInitSetTiming (
  371. IN EFI_IDE_CONTROLLER_INIT_PROTOCOL *This,
  372. IN UINT8 Channel,
  373. IN UINT8 Device,
  374. IN EFI_ATA_COLLECTIVE_MODE *Modes
  375. )
  376. ;
  377. //
  378. // Forward reference declaration
  379. //
  380. /**
  381. Retrieves a Unicode string that is the user readable name of the UEFI Driver.
  382. @param This A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance.
  383. @param Language A pointer to a three character ISO 639-2 language identifier.
  384. This is the language of the driver name that the caller
  385. is requesting, and it must match one of the languages specified
  386. in SupportedLanguages. The number of languages supported by a
  387. driver is up to the driver writer.
  388. @param DriverName A pointer to the Unicode string to return. This Unicode string
  389. is the name of the driver specified by This in the language
  390. specified by Language.
  391. @retval EFI_SUCCESS The Unicode string for the Driver specified by This
  392. and the language specified by Language was returned
  393. in DriverName.
  394. @retval EFI_INVALID_PARAMETER Language is NULL.
  395. @retval EFI_INVALID_PARAMETER DriverName is NULL.
  396. @retval EFI_UNSUPPORTED The driver specified by This does not support the
  397. language specified by Language.
  398. **/
  399. EFI_STATUS
  400. EFIAPI
  401. SataControllerComponentNameGetDriverName (
  402. IN EFI_COMPONENT_NAME_PROTOCOL *This,
  403. IN CHAR8 *Language,
  404. OUT CHAR16 **DriverName
  405. )
  406. ;
  407. /**
  408. Retrieves a Unicode string that is the user readable name of the controller
  409. that is being managed by an UEFI Driver.
  410. @param This A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance.
  411. @param ControllerHandle The handle of a controller that the driver specified by
  412. This is managing. This handle specifies the controller
  413. whose name is to be returned.
  414. @param OPTIONAL ChildHandle The handle of the child controller to retrieve the name
  415. of. This is an optional parameter that may be NULL. It
  416. will be NULL for device drivers. It will also be NULL
  417. for a bus drivers that wish to retrieve the name of the
  418. bus controller. It will not be NULL for a bus driver
  419. that wishes to retrieve the name of a child controller.
  420. @param Language A pointer to a three character ISO 639-2 language
  421. identifier. This is the language of the controller name
  422. that the caller is requesting, and it must match one
  423. of the languages specified in SupportedLanguages. The
  424. number of languages supported by a driver is up to the
  425. driver writer.
  426. @param ControllerName A pointer to the Unicode string to return. This Unicode
  427. string is the name of the controller specified by
  428. ControllerHandle and ChildHandle in the language
  429. specified by Language from the point of view of the
  430. driver specified by This.
  431. @retval EFI_SUCCESS The Unicode string for the user readable name in the
  432. language specified by Language for the driver
  433. specified by This was returned in DriverName.
  434. @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE.
  435. @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
  436. EFI_HANDLE.
  437. @retval EFI_INVALID_PARAMETER Language is NULL.
  438. @retval EFI_INVALID_PARAMETER ControllerName is NULL.
  439. @retval EFI_UNSUPPORTED The driver specified by This is not currently
  440. managing the controller specified by
  441. ControllerHandle and ChildHandle.
  442. @retval EFI_UNSUPPORTED The driver specified by This does not support the
  443. language specified by Language.
  444. **/
  445. EFI_STATUS
  446. EFIAPI
  447. SataControllerComponentNameGetControllerName (
  448. IN EFI_COMPONENT_NAME_PROTOCOL *This,
  449. IN EFI_HANDLE ControllerHandle,
  450. IN EFI_HANDLE ChildHandle OPTIONAL,
  451. IN CHAR8 *Language,
  452. OUT CHAR16 **ControllerName
  453. )
  454. ;
  455. #endif