]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/IScsiDxe/IScsiExtScsiPassThru.c
dea7f7cb9585c750450c4932f980f38d7d005d29
[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 - 2007, 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
71 Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (This);
72 ConfigNvData = &Private->Session.ConfigData.NvData;
73
74 if (Target[0] != 0 || (CompareMem (&Lun, ConfigNvData->BootLun, sizeof (UINT64)) != 0)) {
75 return EFI_INVALID_PARAMETER;
76 }
77
78 if ((Packet == NULL) || (Packet->Cdb == NULL)) {
79 return EFI_INVALID_PARAMETER;
80 }
81
82 return IScsiExecuteScsiCommand (This, Target, Lun, Packet);
83 }
84
85 /**
86 Used to retrieve the list of legal Target IDs and LUNs for SCSI devices on a SCSI channel. These
87 can either be the list SCSI devices that are actually present on the SCSI channel, or the list of legal
88 Target Ids and LUNs for the SCSI channel. Regardless, the caller of this function must probe the
89 Target ID and LUN returned to see if a SCSI device is actually present at that location on the SCSI
90 channel.
91
92 @param[in] This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
93 @param[in, out] Target On input, a pointer to the Target ID (an array of size
94 TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
95 On output, a pointer to the Target ID (an array of
96 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
97 channel. An input value of 0xF(all bytes in the array are 0xF) in the
98 Target array retrieves the Target ID of the first SCSI device present on a
99 SCSI channel.
100 @param[in, out] Lun On input, a pointer to the LUN of a SCSI device present on the SCSI
101 channel. On output, a pointer to the LUN of the next SCSI device present
102 on a SCSI channel.
103
104 @retval EFI_SUCCESS The Target ID and LUN of the next SCSI device on the SCSI
105 channel was returned in Target and Lun.
106 @retval EFI_INVALID_PARAMETER Target array is not all 0xF, and Target and Lun were
107 not returned on a previous call to GetNextTargetLun().
108 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.
109 **/
110 EFI_STATUS
111 EFIAPI
112 IScsiExtScsiPassThruGetNextTargetLun (
113 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
114 IN OUT UINT8 **Target,
115 IN OUT UINT64 *Lun
116 )
117 {
118 ISCSI_DRIVER_DATA *Private;
119 ISCSI_SESSION_CONFIG_NVDATA *ConfigNvData;
120 UINT8 TargetId[TARGET_MAX_BYTES];
121
122 Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (This);
123 ConfigNvData = &Private->Session.ConfigData.NvData;
124
125 if ((*Target)[0] == 0 && (CompareMem (Lun, ConfigNvData->BootLun, sizeof (UINT64)) == 0)) {
126 //
127 // Only one <Target, Lun> pair per iSCSI Driver instance.
128 //
129 return EFI_NOT_FOUND;
130 }
131
132 SetMem (TargetId, TARGET_MAX_BYTES, 0xFF);
133 if (CompareMem (*Target, TargetId, TARGET_MAX_BYTES) == 0) {
134 (*Target)[0] = 0;
135 CopyMem (Lun, ConfigNvData->BootLun, sizeof (UINT64));
136
137 return EFI_SUCCESS;
138 }
139
140 return EFI_INVALID_PARAMETER;
141 }
142
143 /**
144 Used to allocate and build a device path node for a SCSI device on a SCSI channel.
145
146 @param[in] This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
147 @param[in] Target The Target is an array of size TARGET_MAX_BYTES and it specifies the
148 Target ID of the SCSI device for which a device path node is to be
149 allocated and built. Transport drivers may chose to utilize a subset of
150 this size to suit the representation of targets. For example, a Fibre
151 Channel driver may use only 8 bytes (WWN) in the array to represent a
152 FC target.
153 @param[in] Lun The LUN of the SCSI device for which a device path node is to be
154 allocated and built.
155 @param[in, out] DevicePath A pointer to a single device path node that describes the SCSI device
156 specified by Target and Lun. This function is responsible for
157 allocating the buffer DevicePath with the boot service
158 AllocatePool(). It is the caller's responsibility to free
159 DevicePath when the caller is finished with DevicePath.
160
161 @retval EFI_SUCCESS The device path node that describes the SCSI device specified by
162 Target and Lun was allocated and returned in
163 DevicePath.
164 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
165 @retval EFI_NOT_FOUND The SCSI devices specified by Target and Lun does not exist
166 on the SCSI channel.
167 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
168
169 **/
170 EFI_STATUS
171 EFIAPI
172 IScsiExtScsiPassThruBuildDevicePath (
173 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
174 IN UINT8 *Target,
175 IN UINT64 Lun,
176 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath
177 )
178 {
179 ISCSI_DRIVER_DATA *Private;
180 ISCSI_SESSION *Session;
181 ISCSI_SESSION_CONFIG_NVDATA *ConfigNvData;
182 ISCSI_CHAP_AUTH_CONFIG_NVDATA *AuthConfig;
183 EFI_DEV_PATH *Node;
184 UINTN DevPathNodeLen;
185
186 if (DevicePath == NULL) {
187 return EFI_INVALID_PARAMETER;
188 }
189
190 if (Target[0] != 0) {
191 return EFI_NOT_FOUND;
192 }
193
194 Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (This);
195 Session = &Private->Session;
196 ConfigNvData = &Session->ConfigData.NvData;
197 AuthConfig = &Session->AuthData.AuthConfig;
198
199 if (CompareMem (&Lun, ConfigNvData->BootLun, sizeof (UINT64)) != 0) {
200 return EFI_NOT_FOUND;
201 }
202
203 DevPathNodeLen = sizeof (ISCSI_DEVICE_PATH) + AsciiStrLen (ConfigNvData->TargetName) + 1;
204 Node = AllocatePool (DevPathNodeLen);
205 if (Node == NULL) {
206 return EFI_OUT_OF_RESOURCES;
207 }
208
209 Node->DevPath.Type = MESSAGING_DEVICE_PATH;
210 Node->DevPath.SubType = MSG_ISCSI_DP;
211 SetDevicePathNodeLength (&Node->DevPath, (UINT16)DevPathNodeLen);
212
213 //
214 // 0 for TCP, others are reserved.
215 //
216 Node->Iscsi.NetworkProtocol = 0;
217
218 Node->Iscsi.LoginOption = 0;
219 switch (AuthConfig->CHAPType) {
220 case ISCSI_CHAP_NONE:
221 Node->Iscsi.LoginOption |= 0x0800;
222 break;
223
224 case ISCSI_CHAP_UNI:
225 Node->Iscsi.LoginOption |= 0x1000;
226 break;
227
228 default:
229 break;
230 }
231
232 CopyMem (&Node->Iscsi.Lun, ConfigNvData->BootLun, sizeof (UINT64));
233 Node->Iscsi.TargetPortalGroupTag = Session->TargetPortalGroupTag;
234 AsciiStrCpy ((CHAR8 *) Node + sizeof (ISCSI_DEVICE_PATH), ConfigNvData->TargetName);
235
236 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) Node;
237
238 return EFI_SUCCESS;
239 }
240
241 /**
242 Used to translate a device path node to a Target ID and LUN.
243
244 @param[in] This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
245 @param[in] DevicePath A pointer to a single device path node that describes the SCSI device
246 on the SCSI channel.
247 @param[out] Target A pointer to the Target Array which represents the ID of a SCSI device
248 on the SCSI channel.
249 @param[out] Lun A pointer to the LUN of a SCSI device on the SCSI channel.
250
251 @retval EFI_SUCCESS DevicePath was successfully translated to a Target ID and
252 LUN, and they were returned in Target and Lun.
253 @retval EFI_INVALID_PARAMETER DevicePath or Target or Lun is NULL.
254 @retval EFI_NOT_FOUND A valid translation from DevicePath to a Target ID and LUN
255 does not exist.Currently not implemented.
256 @retval EFI_UNSUPPORTED This driver does not support the device path node type in
257 DevicePath.
258 **/
259 EFI_STATUS
260 EFIAPI
261 IScsiExtScsiPassThruGetTargetLun (
262 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
263 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
264 OUT UINT8 **Target,
265 OUT UINT64 *Lun
266 )
267 {
268 ISCSI_DRIVER_DATA *Private;
269 ISCSI_SESSION_CONFIG_NVDATA *ConfigNvData;
270
271 if ((DevicePath == NULL) || (Target == NULL) || (Lun == NULL)) {
272 return EFI_INVALID_PARAMETER;
273 }
274
275 if ((DevicePath->Type != MESSAGING_DEVICE_PATH) ||
276 (DevicePath->SubType != MSG_ISCSI_DP) ||
277 (DevicePathNodeLength (DevicePath) <= sizeof (ISCSI_DEVICE_PATH))
278 ) {
279 return EFI_UNSUPPORTED;
280 }
281
282 Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (This);
283 ConfigNvData = &Private->Session.ConfigData.NvData;
284
285 SetMem (*Target, TARGET_MAX_BYTES, 0xFF);
286 (*Target)[0] = 0;
287
288 if (AsciiStrCmp (ConfigNvData->TargetName, (CHAR8 *) DevicePath + sizeof (ISCSI_DEVICE_PATH)) != 0) {
289 return EFI_UNSUPPORTED;
290 }
291
292 CopyMem (Lun, ConfigNvData->BootLun, sizeof (UINT64));
293
294 return EFI_SUCCESS;
295 }
296
297 /**
298 Resets a SCSI channel. This operation resets all the SCSI devices connected to the SCSI channel.
299 Currently not implemented.
300
301 @param[in] This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
302
303 @retval EFI_SUCCESS The SCSI channel was reset.
304 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI channel.
305 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI channel.
306 @retval EFI_UNSUPPORTED The SCSI channel does not support a channel reset operation.
307 **/
308 EFI_STATUS
309 EFIAPI
310 IScsiExtScsiPassThruResetChannel (
311 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This
312 )
313 {
314 return EFI_UNSUPPORTED;
315 }
316
317 /**
318 Resets a SCSI logical unit that is connected to a SCSI channel. Currently not implemented.
319
320 @param[in] This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
321 @param[in] Target The Target is an array of size TARGET_MAX_BYTE and it represents the
322 target port ID of the SCSI device containing the SCSI logical unit to
323 reset. Transport drivers may chose to utilize a subset of this array to suit
324 the representation of their targets.
325 @param[in] Lun The LUN of the SCSI device to reset.
326
327 @retval EFI_SUCCESS The SCSI device specified by Target and Lun was reset.
328 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
329 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI device
330 specified by Target and Lun.
331 @retval EFI_UNSUPPORTED The SCSI channel does not support a target reset operation.
332 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI device
333 specified by Target and Lun.
334
335 **/
336 EFI_STATUS
337 EFIAPI
338 IScsiExtScsiPassThruResetTargetLun (
339 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
340 IN UINT8 *Target,
341 IN UINT64 Lun
342 )
343 {
344 return EFI_UNSUPPORTED;
345 }
346
347 /**
348 Used to retrieve the list of legal Target IDs for SCSI devices on a SCSI channel. These can either
349 be the list SCSI devices that are actually present on the SCSI channel, or the list of legal Target IDs
350 for the SCSI channel. Regardless, the caller of this function must probe the Target ID returned to
351 see if a SCSI device is actually present at that location on the SCSI channel.
352
353 @param[in] This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
354 @param[in, out] Target (TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
355 On output, a pointer to the Target ID (an array of
356 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
357 channel. An input value of 0xF(all bytes in the array are 0xF) in the
358 Target array retrieves the Target ID of the first SCSI device present on a
359 SCSI channel.
360
361 @retval EFI_SUCCESS The Target ID of the next SCSI device on the SCSI
362 channel was returned in Target.
363 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
364 @retval EFI_TIMEOUT Target array is not all 0xF, and Target were not
365 returned on a previous call to GetNextTarget().
366 Currently not implemented.
367 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.
368 **/
369 EFI_STATUS
370 EFIAPI
371 IScsiExtScsiPassThruGetNextTarget (
372 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
373 IN OUT UINT8 **Target
374 )
375 {
376 UINT8 TargetId[TARGET_MAX_BYTES];
377
378 SetMem (TargetId, TARGET_MAX_BYTES, 0xFF);
379
380 if (CompareMem (*Target, TargetId, TARGET_MAX_BYTES) == 0) {
381 (*Target)[0] = 0;
382 return EFI_SUCCESS;
383 } else if ((*Target)[0] == 0) {
384 return EFI_NOT_FOUND;
385 } else {
386 return EFI_INVALID_PARAMETER;
387 }
388 }
389
390 EFI_EXT_SCSI_PASS_THRU_PROTOCOL gIScsiExtScsiPassThruProtocolTemplate = {
391 NULL,
392 IScsiExtScsiPassThruFunction,
393 IScsiExtScsiPassThruGetNextTargetLun,
394 IScsiExtScsiPassThruBuildDevicePath,
395 IScsiExtScsiPassThruGetTargetLun,
396 IScsiExtScsiPassThruResetChannel,
397 IScsiExtScsiPassThruResetTargetLun,
398 IScsiExtScsiPassThruGetNextTarget
399 };