SataController.h 23 KB

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