IScsiExtScsiPassThru.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /** @file
  2. The implementation of EFI_EXT_SCSI_PASS_THRU_PROTOCOL.
  3. Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "IScsiImpl.h"
  7. EFI_EXT_SCSI_PASS_THRU_PROTOCOL gIScsiExtScsiPassThruProtocolTemplate = {
  8. NULL,
  9. IScsiExtScsiPassThruFunction,
  10. IScsiExtScsiPassThruGetNextTargetLun,
  11. IScsiExtScsiPassThruBuildDevicePath,
  12. IScsiExtScsiPassThruGetTargetLun,
  13. IScsiExtScsiPassThruResetChannel,
  14. IScsiExtScsiPassThruResetTargetLun,
  15. IScsiExtScsiPassThruGetNextTarget
  16. };
  17. /**
  18. Sends a SCSI Request Packet to a SCSI device that is attached to the SCSI channel.
  19. This function supports both blocking I/O and nonblocking I/O. The blocking I/O
  20. functionality is required, and the nonblocking I/O functionality is optional.
  21. @param[in] This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
  22. @param[in] Target The Target is an array of size TARGET_MAX_BYTES and it
  23. represents the id of the SCSI device to send the SCSI
  24. Request Packet. Each transport driver may choose to
  25. utilize a subset of this size to suit the needs
  26. of transport target representation. For example, a
  27. Fibre Channel driver may use only 8 bytes (WWN)
  28. to represent an FC target.
  29. @param[in] Lun The LUN of the SCSI device to send the SCSI Request Packet.
  30. @param[in, out] Packet A pointer to the SCSI Request Packet to send to the
  31. SCSI device specified by Target and Lun.
  32. @param[in] Event If nonblocking I/O is not supported then Event is ignored,
  33. and blocking I/O is performed. If Event is NULL, then
  34. blocking I/O is performed. If Event is not NULL and non
  35. blocking I/O is supported, then nonblocking I/O is performed,
  36. and Event will be signaled when the SCSI Request Packet
  37. completes.
  38. @retval EFI_SUCCESS The SCSI Request Packet was sent by the host. For
  39. bi-directional commands, InTransferLength bytes
  40. were transferred from InDataBuffer.
  41. For write and bi-directional commands, OutTransferLength
  42. bytes were transferred by OutDataBuffer.
  43. @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was not executed.
  44. The number of bytes that could be transferred is
  45. returned in InTransferLength. For write and
  46. bi-directional commands, OutTransferLength bytes
  47. were transferred by OutDataBuffer.
  48. @retval EFI_NOT_READY The SCSI Request Packet could not be sent because
  49. there are too many SCSI Request Packets already
  50. queued. The caller may retry later.
  51. @retval EFI_DEVICE_ERROR A device error occurred while attempting to send
  52. the SCSI Request Packet.
  53. @retval EFI_INVALID_PARAMETER Target, Lun, or the contents of ScsiRequestPacket,
  54. are invalid.
  55. @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet
  56. is not supported by the host adapter.
  57. This includes the case of Bi-directional SCSI
  58. commands not supported by the implementation.
  59. The SCSI Request Packet was not sent,
  60. so no additional status information is available.
  61. @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI
  62. Request Packet to execute.
  63. **/
  64. EFI_STATUS
  65. EFIAPI
  66. IScsiExtScsiPassThruFunction (
  67. IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
  68. IN UINT8 *Target,
  69. IN UINT64 Lun,
  70. IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet,
  71. IN EFI_EVENT Event OPTIONAL
  72. )
  73. {
  74. EFI_STATUS Status;
  75. ISCSI_DRIVER_DATA *Private;
  76. if (Target[0] != 0) {
  77. return EFI_INVALID_PARAMETER;
  78. }
  79. if ((Packet == NULL) || (Packet->Cdb == NULL)) {
  80. return EFI_INVALID_PARAMETER;
  81. }
  82. Status = IScsiExecuteScsiCommand (This, Target, Lun, Packet);
  83. if ((Status != EFI_SUCCESS) && (Status != EFI_NOT_READY)) {
  84. //
  85. // Try to reinstate the session and re-execute the Scsi command.
  86. //
  87. Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (This);
  88. if (EFI_ERROR (IScsiSessionReinstatement (Private->Session))) {
  89. return EFI_DEVICE_ERROR;
  90. }
  91. Status = IScsiExecuteScsiCommand (This, Target, Lun, Packet);
  92. }
  93. return Status;
  94. }
  95. /**
  96. Used to retrieve the list of legal Target IDs and LUNs for SCSI devices on
  97. a SCSI channel. These can either be the list SCSI devices that are actually
  98. present on the SCSI channel, or the list of legal Target Ids and LUNs for the
  99. SCSI channel. Regardless, the caller of this function must probe the Target ID
  100. and LUN returned to see if a SCSI device is actually present at that location
  101. on the SCSI channel.
  102. @param[in] This The EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
  103. @param[in, out] Target On input, a pointer to the Target ID of a SCSI
  104. device present on the SCSI channel. On output, a
  105. pointer to the Target ID of the next SCSI device
  106. present on a SCSI channel. An input value of
  107. 0xFFFFFFFF retrieves the Target ID of the first
  108. SCSI device present on a SCSI channel.
  109. @param[in, out] Lun On input, a pointer to the LUN of a SCSI device
  110. present on the SCSI channel. On output, a pointer
  111. to the LUN of the next SCSI device present on a
  112. SCSI channel.
  113. @retval EFI_SUCCESS The Target ID and Lun of the next SCSI device on
  114. the SCSI channel was returned in Target and Lun.
  115. @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI
  116. channel.
  117. @retval EFI_INVALID_PARAMETER Target is not 0xFFFFFFFF,and Target and Lun were
  118. not returned on a previous call to
  119. GetNextDevice().
  120. **/
  121. EFI_STATUS
  122. EFIAPI
  123. IScsiExtScsiPassThruGetNextTargetLun (
  124. IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
  125. IN OUT UINT8 **Target,
  126. IN OUT UINT64 *Lun
  127. )
  128. {
  129. ISCSI_DRIVER_DATA *Private;
  130. ISCSI_SESSION_CONFIG_NVDATA *ConfigNvData;
  131. UINT8 TargetId[TARGET_MAX_BYTES];
  132. Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (This);
  133. ConfigNvData = &Private->Session->ConfigData->SessionConfigData;
  134. if (((*Target)[0] == 0) && (CompareMem (Lun, ConfigNvData->BootLun, sizeof (UINT64)) == 0)) {
  135. //
  136. // Only one <Target, Lun> pair per iSCSI Driver instance.
  137. //
  138. return EFI_NOT_FOUND;
  139. }
  140. SetMem (TargetId, TARGET_MAX_BYTES, 0xFF);
  141. if (CompareMem (*Target, TargetId, TARGET_MAX_BYTES) == 0) {
  142. (*Target)[0] = 0;
  143. CopyMem (Lun, ConfigNvData->BootLun, sizeof (UINT64));
  144. return EFI_SUCCESS;
  145. }
  146. return EFI_INVALID_PARAMETER;
  147. }
  148. /**
  149. Allocate and build a device path node for a SCSI device on a SCSI channel.
  150. @param[in] This Protocol instance pointer.
  151. @param[in] Target The Target ID of the SCSI device for which a
  152. device path node is to be allocated and built.
  153. @param[in] Lun The LUN of the SCSI device for which a device
  154. path node is to be allocated and built.
  155. @param[in, out] DevicePath A pointer to a single device path node that
  156. describes the SCSI device specified by Target and
  157. Lun. This function is responsible for allocating
  158. the buffer DevicePath with the boot service
  159. AllocatePool(). It is the caller's
  160. responsibility to free DevicePath when the caller
  161. is finished with DevicePath.
  162. @retval EFI_SUCCESS The device path node that describes the SCSI
  163. device specified by Target and Lun was allocated
  164. and returned in DevicePath.
  165. @retval EFI_NOT_FOUND The SCSI devices specified by Target and Lun does
  166. not exist on the SCSI channel.
  167. @retval EFI_INVALID_PARAMETER DevicePath is NULL.
  168. @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate
  169. DevicePath.
  170. **/
  171. EFI_STATUS
  172. EFIAPI
  173. IScsiExtScsiPassThruBuildDevicePath (
  174. IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
  175. IN UINT8 *Target,
  176. IN UINT64 Lun,
  177. IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath
  178. )
  179. {
  180. ISCSI_DRIVER_DATA *Private;
  181. ISCSI_SESSION *Session;
  182. ISCSI_SESSION_CONFIG_NVDATA *ConfigNvData;
  183. ISCSI_CHAP_AUTH_CONFIG_NVDATA *AuthConfig;
  184. EFI_DEV_PATH *Node;
  185. UINTN DevPathNodeLen;
  186. if (DevicePath == NULL) {
  187. return EFI_INVALID_PARAMETER;
  188. }
  189. if (Target[0] != 0) {
  190. return EFI_NOT_FOUND;
  191. }
  192. Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (This);
  193. Session = Private->Session;
  194. ConfigNvData = &Session->ConfigData->SessionConfigData;
  195. AuthConfig = Session->AuthData.CHAP.AuthConfig;
  196. if (CompareMem (&Lun, ConfigNvData->BootLun, sizeof (UINT64)) != 0) {
  197. return EFI_NOT_FOUND;
  198. }
  199. DevPathNodeLen = sizeof (ISCSI_DEVICE_PATH) + AsciiStrLen (ConfigNvData->TargetName) + 1;
  200. Node = AllocateZeroPool (DevPathNodeLen);
  201. if (Node == NULL) {
  202. return EFI_OUT_OF_RESOURCES;
  203. }
  204. Node->DevPath.Type = MESSAGING_DEVICE_PATH;
  205. Node->DevPath.SubType = MSG_ISCSI_DP;
  206. SetDevicePathNodeLength (&Node->DevPath, DevPathNodeLen);
  207. //
  208. // 0 for TCP, others are reserved.
  209. //
  210. Node->Iscsi.NetworkProtocol = 0;
  211. Node->Iscsi.LoginOption = 0;
  212. switch (Session->AuthType) {
  213. case ISCSI_AUTH_TYPE_NONE:
  214. Node->Iscsi.LoginOption |= 0x0800;
  215. break;
  216. case ISCSI_AUTH_TYPE_CHAP:
  217. //
  218. // Bit12: 0=CHAP_BI, 1=CHAP_UNI
  219. //
  220. if (AuthConfig->CHAPType == ISCSI_CHAP_UNI) {
  221. Node->Iscsi.LoginOption |= 0x1000;
  222. }
  223. break;
  224. default:
  225. break;
  226. }
  227. CopyMem (&Node->Iscsi.Lun, ConfigNvData->BootLun, sizeof (UINT64));
  228. Node->Iscsi.TargetPortalGroupTag = Session->TargetPortalGroupTag;
  229. AsciiStrCpyS ((CHAR8 *)Node + sizeof (ISCSI_DEVICE_PATH), AsciiStrLen (ConfigNvData->TargetName) + 1, ConfigNvData->TargetName);
  230. *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *)Node;
  231. return EFI_SUCCESS;
  232. }
  233. /**
  234. Translate a device path node to a Target ID and LUN.
  235. @param[in] This Protocol instance pointer.
  236. @param[in] DevicePath A pointer to the device path node that describes
  237. a SCSI device on the SCSI channel.
  238. @param[out] Target A pointer to the Target ID of a SCSI device on
  239. the SCSI channel.
  240. @param[out] Lun A pointer to the LUN of a SCSI device on the SCSI
  241. channel.
  242. @retval EFI_SUCCESS DevicePath was successfully translated to a
  243. Target ID and LUN, and they were returned in
  244. Target and Lun.
  245. @retval EFI_INVALID_PARAMETER DevicePath/Target/Lun is NULL.
  246. @retval EFI_UNSUPPORTED This driver does not support the device path node
  247. type in DevicePath.
  248. @retval EFI_NOT_FOUND A valid translation does not exist from DevicePath
  249. to a TargetID and LUN.
  250. **/
  251. EFI_STATUS
  252. EFIAPI
  253. IScsiExtScsiPassThruGetTargetLun (
  254. IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
  255. IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
  256. OUT UINT8 **Target,
  257. OUT UINT64 *Lun
  258. )
  259. {
  260. ISCSI_DRIVER_DATA *Private;
  261. ISCSI_SESSION_CONFIG_NVDATA *ConfigNvData;
  262. if ((DevicePath == NULL) || (Target == NULL) || (Lun == NULL)) {
  263. return EFI_INVALID_PARAMETER;
  264. }
  265. if ((DevicePath->Type != MESSAGING_DEVICE_PATH) ||
  266. (DevicePath->SubType != MSG_ISCSI_DP) ||
  267. (DevicePathNodeLength (DevicePath) <= sizeof (ISCSI_DEVICE_PATH))
  268. )
  269. {
  270. return EFI_UNSUPPORTED;
  271. }
  272. Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (This);
  273. ConfigNvData = &Private->Session->ConfigData->SessionConfigData;
  274. SetMem (*Target, TARGET_MAX_BYTES, 0xFF);
  275. (*Target)[0] = 0;
  276. if (AsciiStrCmp (ConfigNvData->TargetName, (CHAR8 *)DevicePath + sizeof (ISCSI_DEVICE_PATH)) != 0) {
  277. return EFI_UNSUPPORTED;
  278. }
  279. CopyMem (Lun, ConfigNvData->BootLun, sizeof (UINT64));
  280. return EFI_SUCCESS;
  281. }
  282. /**
  283. Resets a SCSI channel. This operation resets all the SCSI devices connected to
  284. the SCSI channel.
  285. @param[in] This Protocol instance pointer.
  286. @retval EFI_UNSUPPORTED It is not supported.
  287. **/
  288. EFI_STATUS
  289. EFIAPI
  290. IScsiExtScsiPassThruResetChannel (
  291. IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This
  292. )
  293. {
  294. return EFI_UNSUPPORTED;
  295. }
  296. /**
  297. Resets a SCSI device that is connected to a SCSI channel.
  298. @param[in] This Protocol instance pointer.
  299. @param[in] Target The Target ID of the SCSI device to reset.
  300. @param[in] Lun The LUN of the SCSI device to reset.
  301. @retval EFI_UNSUPPORTED It is not supported.
  302. **/
  303. EFI_STATUS
  304. EFIAPI
  305. IScsiExtScsiPassThruResetTargetLun (
  306. IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
  307. IN UINT8 *Target,
  308. IN UINT64 Lun
  309. )
  310. {
  311. return EFI_UNSUPPORTED;
  312. }
  313. /**
  314. Retrieve the list of legal Target IDs for SCSI devices on a SCSI channel.
  315. @param[in] This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL
  316. instance.
  317. @param[in, out] Target (TARGET_MAX_BYTES) of a SCSI device present on
  318. the SCSI channel. On output, a pointer to the
  319. Target ID (an array of TARGET_MAX_BYTES) of the
  320. next SCSI device present on a SCSI channel.
  321. An input value of 0xF(all bytes in the array are 0xF)
  322. in the Target array retrieves the Target ID of the
  323. first SCSI device present on a SCSI channel.
  324. @retval EFI_SUCCESS The Target ID of the next SCSI device on the SCSI
  325. channel was returned in Target.
  326. @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
  327. @retval EFI_TIMEOUT Target array is not all 0xF, and Target was not
  328. returned on a previous call to GetNextTarget().
  329. @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.
  330. **/
  331. EFI_STATUS
  332. EFIAPI
  333. IScsiExtScsiPassThruGetNextTarget (
  334. IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
  335. IN OUT UINT8 **Target
  336. )
  337. {
  338. UINT8 TargetId[TARGET_MAX_BYTES];
  339. SetMem (TargetId, TARGET_MAX_BYTES, 0xFF);
  340. if (CompareMem (*Target, TargetId, TARGET_MAX_BYTES) == 0) {
  341. (*Target)[0] = 0;
  342. return EFI_SUCCESS;
  343. } else if ((*Target)[0] == 0) {
  344. return EFI_NOT_FOUND;
  345. } else {
  346. return EFI_INVALID_PARAMETER;
  347. }
  348. }