AhciPeiPassThru.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /** @file
  2. The AhciPei driver is used to manage ATA hard disk device working under AHCI
  3. mode at PEI phase.
  4. Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "AhciPei.h"
  8. /**
  9. Traverse the attached ATA devices list to find out the device with given Port
  10. and PortMultiplierPort.
  11. @param[in] Private A pointer to the PEI_AHCI_CONTROLLER_PRIVATE_DATA
  12. instance.
  13. @param[in] Port The port number of the ATA device.
  14. @param[in] PortMultiplierPort The port multiplier port number of the ATA device.
  15. @retval The pointer to the PEI_AHCI_ATA_DEVICE_DATA structure of the device
  16. info to access.
  17. **/
  18. PEI_AHCI_ATA_DEVICE_DATA *
  19. SearchDeviceByPort (
  20. IN PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private,
  21. IN UINT16 Port,
  22. IN UINT16 PortMultiplierPort
  23. )
  24. {
  25. PEI_AHCI_ATA_DEVICE_DATA *DeviceData;
  26. LIST_ENTRY *Node;
  27. Node = GetFirstNode (&Private->DeviceList);
  28. while (!IsNull (&Private->DeviceList, Node)) {
  29. DeviceData = AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node);
  30. if ((DeviceData->Port == Port) &&
  31. (DeviceData->PortMultiplier == PortMultiplierPort)) {
  32. return DeviceData;
  33. }
  34. Node = GetNextNode (&Private->DeviceList, Node);
  35. }
  36. return NULL;
  37. }
  38. /**
  39. Sends an ATA command to an ATA device that is attached to the ATA controller.
  40. @param[in] Private Pointer to the PEI_AHCI_CONTROLLER_PRIVATE_DATA.
  41. @param[in] Port The port number of the ATA device.
  42. @param[in] PortMultiplierPort The port multiplier port number of the ATA
  43. device.
  44. @param[in] FisIndex The index of the FIS.
  45. @param[in,out] Packet A pointer to the ATA command to send to
  46. the ATA device specified by Port and
  47. PortMultiplierPort.
  48. @retval EFI_SUCCESS The ATA command was sent by the host. For
  49. bi-directional commands, InTransferLength bytes
  50. were transferred from InDataBuffer. For write
  51. and bi-directional commands, OutTransferLength
  52. bytes were transferred by OutDataBuffer.
  53. @retval EFI_BAD_BUFFER_SIZE The ATA command was not executed. The number
  54. of bytes that could be transferred is returned
  55. in InTransferLength. For write and bi-directional
  56. commands, OutTransferLength bytes were transferred
  57. by OutDataBuffer.
  58. @retval EFI_NOT_READY The ATA command could not be sent because there
  59. are too many ATA commands already queued. The
  60. caller may retry again later.
  61. @retval EFI_DEVICE_ERROR A device error occurred while attempting to
  62. send the ATA command.
  63. @retval EFI_INVALID_PARAMETER Port, PortMultiplierPort, or the contents of
  64. Acb are invalid. The ATA command was not sent,
  65. so no additional status information is available.
  66. **/
  67. EFI_STATUS
  68. AhciPassThruExecute (
  69. IN PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private,
  70. IN UINT16 Port,
  71. IN UINT16 PortMultiplierPort,
  72. IN UINT8 FisIndex,
  73. IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet
  74. )
  75. {
  76. EFI_STATUS Status;
  77. switch (Packet->Protocol) {
  78. case EFI_ATA_PASS_THRU_PROTOCOL_ATA_NON_DATA:
  79. Status = AhciNonDataTransfer (
  80. Private,
  81. (UINT8) Port,
  82. (UINT8) PortMultiplierPort,
  83. FisIndex,
  84. Packet->Acb,
  85. Packet->Asb,
  86. Packet->Timeout
  87. );
  88. break;
  89. case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN:
  90. Status = AhciPioTransfer (
  91. Private,
  92. (UINT8) Port,
  93. (UINT8) PortMultiplierPort,
  94. FisIndex,
  95. TRUE,
  96. Packet->Acb,
  97. Packet->Asb,
  98. Packet->InDataBuffer,
  99. Packet->InTransferLength,
  100. Packet->Timeout
  101. );
  102. break;
  103. case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT:
  104. Status = AhciPioTransfer (
  105. Private,
  106. (UINT8) Port,
  107. (UINT8) PortMultiplierPort,
  108. FisIndex,
  109. FALSE,
  110. Packet->Acb,
  111. Packet->Asb,
  112. Packet->OutDataBuffer,
  113. Packet->OutTransferLength,
  114. Packet->Timeout
  115. );
  116. break;
  117. default:
  118. return EFI_UNSUPPORTED;
  119. }
  120. return Status;
  121. }
  122. /**
  123. Sends an ATA command to an ATA device that is attached to the ATA controller.
  124. @param[in] This The PPI instance pointer.
  125. @param[in] Port The port number of the ATA device to send
  126. the command.
  127. @param[in] PortMultiplierPort The port multiplier port number of the ATA
  128. device to send the command.
  129. If there is no port multiplier, then specify
  130. 0xFFFF.
  131. @param[in,out] Packet A pointer to the ATA command to send to
  132. the ATA device specified by Port and
  133. PortMultiplierPort.
  134. @retval EFI_SUCCESS The ATA command was sent by the host. For
  135. bi-directional commands, InTransferLength bytes
  136. were transferred from InDataBuffer. For write
  137. and bi-directional commands, OutTransferLength
  138. bytes were transferred by OutDataBuffer.
  139. @retval EFI_NOT_FOUND The specified ATA device is not found.
  140. @retval EFI_INVALID_PARAMETER The contents of Acb are invalid. The ATA command
  141. was not sent, so no additional status information
  142. is available.
  143. @retval EFI_BAD_BUFFER_SIZE The ATA command was not executed. The number
  144. of bytes that could be transferred is returned
  145. in InTransferLength. For write and bi-directional
  146. commands, OutTransferLength bytes were transferred
  147. by OutDataBuffer.
  148. @retval EFI_NOT_READY The ATA command could not be sent because there
  149. are too many ATA commands already queued. The
  150. caller may retry again later.
  151. @retval EFI_DEVICE_ERROR A device error occurred while attempting to
  152. send the ATA command.
  153. **/
  154. EFI_STATUS
  155. EFIAPI
  156. AhciAtaPassThruPassThru (
  157. IN EDKII_PEI_ATA_PASS_THRU_PPI *This,
  158. IN UINT16 Port,
  159. IN UINT16 PortMultiplierPort,
  160. IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet
  161. )
  162. {
  163. UINT32 IoAlign;
  164. PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private;
  165. PEI_AHCI_ATA_DEVICE_DATA *DeviceData;
  166. UINT32 MaxSectorCount;
  167. UINT32 BlockSize;
  168. if (This == NULL || Packet == NULL) {
  169. return EFI_INVALID_PARAMETER;
  170. }
  171. IoAlign = This->Mode->IoAlign;
  172. if ((IoAlign > 1) && !IS_ALIGNED (Packet->InDataBuffer, IoAlign)) {
  173. return EFI_INVALID_PARAMETER;
  174. }
  175. if ((IoAlign > 1) && !IS_ALIGNED (Packet->OutDataBuffer, IoAlign)) {
  176. return EFI_INVALID_PARAMETER;
  177. }
  178. if ((IoAlign > 1) && !IS_ALIGNED (Packet->Asb, IoAlign)) {
  179. return EFI_INVALID_PARAMETER;
  180. }
  181. Private = GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_PASS_THRU (This);
  182. DeviceData = SearchDeviceByPort (Private, Port, PortMultiplierPort);
  183. if (DeviceData == NULL) {
  184. return EFI_NOT_FOUND;
  185. }
  186. MaxSectorCount = mMaxTransferBlockNumber[DeviceData->Lba48Bit];
  187. BlockSize = DeviceData->Media.BlockSize;
  188. //
  189. // Convert the transfer length from sector count to byte.
  190. //
  191. if (((Packet->Length & EFI_ATA_PASS_THRU_LENGTH_BYTES) == 0) &&
  192. (Packet->InTransferLength != 0)) {
  193. Packet->InTransferLength = Packet->InTransferLength * BlockSize;
  194. }
  195. //
  196. // Convert the transfer length from sector count to byte.
  197. //
  198. if (((Packet->Length & EFI_ATA_PASS_THRU_LENGTH_BYTES) == 0) &&
  199. (Packet->OutTransferLength != 0)) {
  200. Packet->OutTransferLength = Packet->OutTransferLength * BlockSize;
  201. }
  202. //
  203. // If the data buffer described by InDataBuffer/OutDataBuffer and
  204. // InTransferLength/OutTransferLength is too big to be transferred in a single
  205. // command, then no data is transferred and EFI_BAD_BUFFER_SIZE is returned.
  206. //
  207. if (((Packet->InTransferLength != 0) && (Packet->InTransferLength > MaxSectorCount * BlockSize)) ||
  208. ((Packet->OutTransferLength != 0) && (Packet->OutTransferLength > MaxSectorCount * BlockSize))) {
  209. return EFI_BAD_BUFFER_SIZE;
  210. }
  211. return AhciPassThruExecute (
  212. Private,
  213. DeviceData->Port,
  214. DeviceData->PortMultiplier,
  215. DeviceData->FisIndex,
  216. Packet
  217. );
  218. }
  219. /**
  220. Used to retrieve the list of legal port numbers for ATA devices on an ATA controller.
  221. These can either be the list of ports where ATA devices are actually present or the
  222. list of legal port numbers for the ATA controller. Regardless, the caller of this
  223. function must probe the port number returned to see if an ATA device is actually
  224. present at that location on the ATA controller.
  225. The GetNextPort() function retrieves the port number on an ATA controller. If on
  226. input Port is 0xFFFF, then the port number of the first port on the ATA controller
  227. is returned in Port and EFI_SUCCESS is returned.
  228. If Port is a port number that was returned on a previous call to GetNextPort(),
  229. then the port number of the next port on the ATA controller is returned in Port,
  230. and EFI_SUCCESS is returned. If Port is not 0xFFFF and Port was not returned on
  231. a previous call to GetNextPort(), then EFI_INVALID_PARAMETER is returned.
  232. If Port is the port number of the last port on the ATA controller, then EFI_NOT_FOUND
  233. is returned.
  234. @param[in] This The PPI instance pointer.
  235. @param[in,out] Port On input, a pointer to the port number on the ATA controller.
  236. On output, a pointer to the next port number on the ATA
  237. controller. An input value of 0xFFFF retrieves the first
  238. port number on the ATA controller.
  239. @retval EFI_SUCCESS The next port number on the ATA controller was
  240. returned in Port.
  241. @retval EFI_NOT_FOUND There are no more ports on this ATA controller.
  242. @retval EFI_INVALID_PARAMETER Port is not 0xFFFF and Port was not returned
  243. on a previous call to GetNextPort().
  244. **/
  245. EFI_STATUS
  246. EFIAPI
  247. AhciAtaPassThruGetNextPort (
  248. IN EDKII_PEI_ATA_PASS_THRU_PPI *This,
  249. IN OUT UINT16 *Port
  250. )
  251. {
  252. PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private;
  253. PEI_AHCI_ATA_DEVICE_DATA *DeviceData;
  254. LIST_ENTRY *Node;
  255. if (This == NULL || Port == NULL) {
  256. return EFI_INVALID_PARAMETER;
  257. }
  258. Private = GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_PASS_THRU (This);
  259. if (*Port == 0xFFFF) {
  260. //
  261. // If the Port is all 0xFF's, start to traverse the device list from the
  262. // beginning.
  263. //
  264. Node = GetFirstNode (&Private->DeviceList);
  265. if (!IsNull (&Private->DeviceList, Node)) {
  266. DeviceData = AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node);
  267. *Port = DeviceData->Port;
  268. goto Exit;
  269. }
  270. return EFI_NOT_FOUND;
  271. } else if (*Port == Private->PreviousPort) {
  272. Node = GetFirstNode (&Private->DeviceList);
  273. while (!IsNull (&Private->DeviceList, Node)) {
  274. DeviceData = AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node);
  275. if (DeviceData->Port > *Port){
  276. *Port = DeviceData->Port;
  277. goto Exit;
  278. }
  279. Node = GetNextNode (&Private->DeviceList, Node);
  280. }
  281. return EFI_NOT_FOUND;
  282. } else {
  283. //
  284. // Port is not equal to all 0xFF's and not equal to previous return value.
  285. //
  286. return EFI_INVALID_PARAMETER;
  287. }
  288. Exit:
  289. //
  290. // Update the PreviousPort.
  291. //
  292. Private->PreviousPort = *Port;
  293. return EFI_SUCCESS;
  294. }
  295. /**
  296. Used to retrieve the list of legal port multiplier port numbers for ATA devices
  297. on a port of an ATA controller. These can either be the list of port multiplier
  298. ports where ATA devices are actually present on port or the list of legal port
  299. multiplier ports on that port. Regardless, the caller of this function must probe
  300. the port number and port multiplier port number returned to see if an ATA device
  301. is actually present.
  302. The GetNextDevice() function retrieves the port multiplier port number of an ATA
  303. device present on a port of an ATA controller.
  304. If PortMultiplierPort points to a port multiplier port number value that was
  305. returned on a previous call to GetNextDevice(), then the port multiplier port
  306. number of the next ATA device on the port of the ATA controller is returned in
  307. PortMultiplierPort, and EFI_SUCCESS is returned.
  308. If PortMultiplierPort points to 0xFFFF, then the port multiplier port number
  309. of the first ATA device on port of the ATA controller is returned in PortMultiplierPort
  310. and EFI_SUCCESS is returned.
  311. If PortMultiplierPort is not 0xFFFF and the value pointed to by PortMultiplierPort
  312. was not returned on a previous call to GetNextDevice(), then EFI_INVALID_PARAMETER
  313. is returned.
  314. If PortMultiplierPort is the port multiplier port number of the last ATA device
  315. on the port of the ATA controller, then EFI_NOT_FOUND is returned.
  316. @param[in] This The PPI instance pointer.
  317. @param[in] Port The port number present on the ATA controller.
  318. @param[in,out] PortMultiplierPort On input, a pointer to the port multiplier
  319. port number of an ATA device present on the
  320. ATA controller. If on input a PortMultiplierPort
  321. of 0xFFFF is specified, then the port multiplier
  322. port number of the first ATA device is returned.
  323. On output, a pointer to the port multiplier port
  324. number of the next ATA device present on an ATA
  325. controller.
  326. @retval EFI_SUCCESS The port multiplier port number of the next ATA
  327. device on the port of the ATA controller was
  328. returned in PortMultiplierPort.
  329. @retval EFI_NOT_FOUND There are no more ATA devices on this port of
  330. the ATA controller.
  331. @retval EFI_INVALID_PARAMETER PortMultiplierPort is not 0xFFFF, and PortMultiplierPort
  332. was not returned on a previous call to GetNextDevice().
  333. **/
  334. EFI_STATUS
  335. EFIAPI
  336. AhciAtaPassThruGetNextDevice (
  337. IN EDKII_PEI_ATA_PASS_THRU_PPI *This,
  338. IN UINT16 Port,
  339. IN OUT UINT16 *PortMultiplierPort
  340. )
  341. {
  342. PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private;
  343. PEI_AHCI_ATA_DEVICE_DATA *DeviceData;
  344. LIST_ENTRY *Node;
  345. if (This == NULL || PortMultiplierPort == NULL) {
  346. return EFI_INVALID_PARAMETER;
  347. }
  348. Private = GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_PASS_THRU (This);
  349. if (Private->PreviousPortMultiplier == 0xFFFF) {
  350. //
  351. // If a device is directly attached on a port, previous call to this
  352. // function will return the value 0xFFFF for PortMultiplierPort. In
  353. // this case, there should be no more device on the port multiplier.
  354. //
  355. Private->PreviousPortMultiplier = 0;
  356. return EFI_NOT_FOUND;
  357. }
  358. if (*PortMultiplierPort == Private->PreviousPortMultiplier) {
  359. Node = GetFirstNode (&Private->DeviceList);
  360. while (!IsNull (&Private->DeviceList, Node)) {
  361. DeviceData = AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node);
  362. if ((DeviceData->Port == Port) &&
  363. (DeviceData->PortMultiplier > *PortMultiplierPort)){
  364. *PortMultiplierPort = DeviceData->PortMultiplier;
  365. goto Exit;
  366. }
  367. Node = GetNextNode (&Private->DeviceList, Node);
  368. }
  369. return EFI_NOT_FOUND;
  370. } else if (*PortMultiplierPort == 0xFFFF) {
  371. //
  372. // If the PortMultiplierPort is all 0xFF's, start to traverse the device list
  373. // from the beginning.
  374. //
  375. Node = GetFirstNode (&Private->DeviceList);
  376. while (!IsNull (&Private->DeviceList, Node)) {
  377. DeviceData = AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node);
  378. if (DeviceData->Port == Port){
  379. *PortMultiplierPort = DeviceData->PortMultiplier;
  380. goto Exit;
  381. }
  382. Node = GetNextNode (&Private->DeviceList, Node);
  383. }
  384. return EFI_NOT_FOUND;
  385. } else {
  386. //
  387. // PortMultiplierPort is not equal to all 0xFF's and not equal to previous
  388. // return value.
  389. //
  390. return EFI_INVALID_PARAMETER;
  391. }
  392. Exit:
  393. //
  394. // Update the PreviousPortMultiplier.
  395. //
  396. Private->PreviousPortMultiplier = *PortMultiplierPort;
  397. return EFI_SUCCESS;
  398. }
  399. /**
  400. Gets the device path information of the underlying ATA host controller.
  401. @param[in] This The PPI instance pointer.
  402. @param[out] DevicePathLength The length of the device path in bytes specified
  403. by DevicePath.
  404. @param[out] DevicePath The device path of the underlying ATA host controller.
  405. This field re-uses EFI Device Path Protocol as
  406. defined by Section 10.2 EFI Device Path Protocol
  407. of UEFI 2.7 Specification.
  408. @retval EFI_SUCCESS The device path of the ATA host controller has
  409. been successfully returned.
  410. @retval EFI_INVALID_PARAMETER DevicePathLength or DevicePath is NULL.
  411. @retval EFI_OUT_OF_RESOURCES Not enough resource to return the device path.
  412. **/
  413. EFI_STATUS
  414. EFIAPI
  415. AhciAtaPassThruGetDevicePath (
  416. IN EDKII_PEI_ATA_PASS_THRU_PPI *This,
  417. OUT UINTN *DevicePathLength,
  418. OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath
  419. )
  420. {
  421. PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private;
  422. if (This == NULL || DevicePathLength == NULL || DevicePath == NULL) {
  423. return EFI_INVALID_PARAMETER;
  424. }
  425. Private = GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_PASS_THRU (This);
  426. *DevicePathLength = Private->DevicePathLength;
  427. *DevicePath = AllocateCopyPool (Private->DevicePathLength, Private->DevicePath);
  428. if (*DevicePath == NULL) {
  429. *DevicePathLength = 0;
  430. return EFI_OUT_OF_RESOURCES;
  431. }
  432. return EFI_SUCCESS;
  433. }