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