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