]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/UefiScsiLib/UefiScsiLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdePkg / Library / UefiScsiLib / UefiScsiLib.c
1 /** @file
2 UEFI SCSI Library implementation
3
4 Copyright (c) 2006 - 2020, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9
10 #include <Uefi.h>
11 #include <Library/BaseLib.h>
12 #include <Library/DebugLib.h>
13 #include <Library/UefiScsiLib.h>
14 #include <Library/BaseMemoryLib.h>
15 #include <Library/MemoryAllocationLib.h>
16 #include <Library/UefiBootServicesTableLib.h>
17
18 #include <IndustryStandard/Scsi.h>
19
20
21 //
22 // Scsi Command Length
23 //
24 #define EFI_SCSI_OP_LENGTH_SIX 0x6
25 #define EFI_SCSI_OP_LENGTH_TEN 0xa
26 #define EFI_SCSI_OP_LENGTH_TWELVE 0xc
27 #define EFI_SCSI_OP_LENGTH_SIXTEEN 0x10
28
29 //
30 // The context structure used when non-blocking SCSI read/write operation
31 // completes.
32 //
33 typedef struct {
34 ///
35 /// The SCSI request packet to send to the SCSI controller specified by
36 /// the device handle.
37 ///
38 EFI_SCSI_IO_SCSI_REQUEST_PACKET CommandPacket;
39 ///
40 /// The length of the output sense data.
41 ///
42 UINT8 *SenseDataLength;
43 ///
44 /// The status of the SCSI host adapter.
45 ///
46 UINT8 *HostAdapterStatus;
47 ///
48 /// The status of the target SCSI device.
49 ///
50 UINT8 *TargetStatus;
51 ///
52 /// The length of the data buffer for the SCSI read/write command.
53 ///
54 UINT32 *DataLength;
55 ///
56 /// The caller event to be signaled when the SCSI read/write command
57 /// completes.
58 ///
59 EFI_EVENT CallerEvent;
60 } EFI_SCSI_LIB_ASYNC_CONTEXT;
61
62
63
64 /**
65 Execute Test Unit Ready SCSI command on a specific SCSI target.
66
67 Executes the Test Unit Ready command on the SCSI target specified by ScsiIo.
68 If Timeout is zero, then this function waits indefinitely for the command to complete.
69 If Timeout is greater than zero, then the command is executed and will timeout after Timeout 100 ns units.
70 If ScsiIo is NULL, then ASSERT().
71 If SenseDataLength is NULL, then ASSERT().
72 If HostAdapterStatus is NULL, then ASSERT().
73 If TargetStatus is NULL, then ASSERT().
74
75 If SenseDataLength is non-zero and SenseData is not NULL, SenseData must meet buffer
76 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
77 gets returned.
78
79 @param[in] ScsiIo A pointer to the SCSI I/O Protocol instance
80 for the specific SCSI target.
81 @param[in] Timeout The timeout in 100 ns units to use for the execution
82 of this SCSI Request Packet. A Timeout value of
83 zero means that this function will wait indefinitely
84 for the SCSI Request Packet to execute. If Timeout
85 is greater than zero, then this function will return
86 EFI_TIMEOUT if the time required to execute the SCSI
87 Request Packet is greater than Timeout.
88 @param[in, out] SenseData A pointer to sense data that was generated by
89 the execution of the SCSI Request Packet. This
90 buffer must be allocated by the caller.
91 If SenseDataLength is 0, then this parameter is
92 optional and may be NULL.
93 @param[in, out] SenseDataLength On input, a pointer to the length in bytes of
94 the SenseData buffer. On output, a pointer to
95 the number of bytes written to the SenseData buffer.
96 @param[out] HostAdapterStatus The status of the SCSI Host Controller that produces
97 the SCSI bus containing the SCSI target specified by
98 ScsiIo when the SCSI Request Packet was executed.
99 See the EFI SCSI I/O Protocol in the UEFI Specification
100 for details on the possible return values.
101 @param[out] TargetStatus The status returned by the SCSI target specified
102 by ScsiIo when the SCSI Request Packet was executed
103 on the SCSI Host Controller. See the EFI SCSI I/O
104 Protocol in the UEFI Specification for details on
105 the possible return values.
106
107 @retval EFI_SUCCESS The command was executed successfully.
108 See HostAdapterStatus, TargetStatus, SenseDataLength,
109 and SenseData in that order for additional status
110 information.
111 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because
112 there are too many SCSI Command Packets already
113 queued. The SCSI Request Packet was not sent, so
114 no additional status information is available.
115 The caller may retry again later.
116 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send
117 SCSI Request Packet. See HostAdapterStatus,
118 TargetStatus, SenseDataLength, and SenseData in that
119 order for additional status information.
120 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet
121 is not supported by the SCSI initiator(i.e., SCSI
122 Host Controller). The SCSI Request Packet was not
123 sent, so no additional status information is available.
124 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request
125 Packet to execute. See HostAdapterStatus, TargetStatus,
126 SenseDataLength, and SenseData in that order for
127 additional status information.
128 @retval EFI_INVALID_PARAMETER The contents of the SCSI Request Packet are invalid.
129
130 **/
131 EFI_STATUS
132 EFIAPI
133 ScsiTestUnitReadyCommand (
134 IN EFI_SCSI_IO_PROTOCOL *ScsiIo,
135 IN UINT64 Timeout,
136 IN OUT VOID *SenseData, OPTIONAL
137 IN OUT UINT8 *SenseDataLength,
138 OUT UINT8 *HostAdapterStatus,
139 OUT UINT8 *TargetStatus
140 )
141 {
142 EFI_SCSI_IO_SCSI_REQUEST_PACKET CommandPacket;
143 EFI_STATUS Status;
144 UINT8 Cdb[EFI_SCSI_OP_LENGTH_SIX];
145
146 ASSERT (SenseDataLength != NULL);
147 ASSERT (HostAdapterStatus != NULL);
148 ASSERT (TargetStatus != NULL);
149 ASSERT (ScsiIo != NULL);
150
151 ZeroMem (&CommandPacket, sizeof (EFI_SCSI_IO_SCSI_REQUEST_PACKET));
152 ZeroMem (Cdb, EFI_SCSI_OP_LENGTH_SIX);
153
154 CommandPacket.Timeout = Timeout;
155 CommandPacket.InDataBuffer = NULL;
156 CommandPacket.InTransferLength= 0;
157 CommandPacket.OutDataBuffer = NULL;
158 CommandPacket.OutTransferLength= 0;
159 CommandPacket.SenseData = SenseData;
160 CommandPacket.Cdb = Cdb;
161 //
162 // Fill Cdb for Test Unit Ready Command
163 //
164 Cdb[0] = EFI_SCSI_OP_TEST_UNIT_READY;
165 CommandPacket.CdbLength = (UINT8) EFI_SCSI_OP_LENGTH_SIX;
166 CommandPacket.SenseDataLength = *SenseDataLength;
167
168 Status = ScsiIo->ExecuteScsiCommand (ScsiIo, &CommandPacket, NULL);
169
170 *HostAdapterStatus = CommandPacket.HostAdapterStatus;
171 *TargetStatus = CommandPacket.TargetStatus;
172 *SenseDataLength = CommandPacket.SenseDataLength;
173
174 return Status;
175 }
176
177
178 /**
179 Execute Inquiry SCSI command on a specific SCSI target.
180
181 Executes the Inquiry command on the SCSI target specified by ScsiIo.
182 If Timeout is zero, then this function waits indefinitely for the command to complete.
183 If Timeout is greater than zero, then the command is executed and will timeout after Timeout 100 ns units.
184 If ScsiIo is NULL, then ASSERT().
185 If SenseDataLength is NULL, then ASSERT().
186 If HostAdapterStatus is NULL, then ASSERT().
187 If TargetStatus is NULL, then ASSERT().
188 If InquiryDataLength is NULL, then ASSERT().
189
190 If SenseDataLength is non-zero and SenseData is not NULL, SenseData must meet buffer
191 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
192 gets returned.
193
194 If InquiryDataLength is non-zero and InquiryDataBuffer is not NULL, InquiryDataBuffer
195 must meet buffer alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise
196 EFI_INVALID_PARAMETER gets returned.
197
198 @param[in] ScsiIo A pointer to the SCSI I/O Protocol instance
199 for the specific SCSI target.
200 @param[in] Timeout The timeout in 100 ns units to use for the
201 execution of this SCSI Request Packet. A Timeout
202 value of zero means that this function will wait
203 indefinitely for the SCSI Request Packet to execute.
204 If Timeout is greater than zero, then this function
205 will return EFI_TIMEOUT if the time required to
206 execute the SCSI Request Packet is greater than Timeout.
207 @param[in, out] SenseData A pointer to sense data that was generated
208 by the execution of the SCSI Request Packet.
209 This buffer must be allocated by the caller.
210 If SenseDataLength is 0, then this parameter
211 is optional and may be NULL.
212 @param[in, out] SenseDataLength On input, the length in bytes of the SenseData buffer.
213 On output, the number of bytes written to the SenseData buffer.
214 @param[out] HostAdapterStatus The status of the SCSI Host Controller that
215 produces the SCSI bus containing the SCSI
216 target specified by ScsiIo when the SCSI
217 Request Packet was executed. See the EFI
218 SCSI I/O Protocol in the UEFI Specification
219 for details on the possible return values.
220 @param[out] TargetStatus The status returned by the SCSI target specified
221 by ScsiIo when the SCSI Request Packet was
222 executed on the SCSI Host Controller.
223 See the EFI SCSI I/O Protocol in the UEFI
224 Specification for details on the possible
225 return values.
226 @param[in, out] InquiryDataBuffer A pointer to inquiry data that was generated
227 by the execution of the SCSI Request Packet.
228 This buffer must be allocated by the caller.
229 If InquiryDataLength is 0, then this parameter
230 is optional and may be NULL.
231 @param[in, out] InquiryDataLength On input, a pointer to the length in bytes
232 of the InquiryDataBuffer buffer.
233 On output, a pointer to the number of bytes
234 written to the InquiryDataBuffer buffer.
235 @param[in] EnableVitalProductData If TRUE, then the supported vital product
236 data for the PageCode is returned in InquiryDataBuffer.
237 If FALSE, then the standard inquiry data is
238 returned in InquiryDataBuffer and PageCode is ignored.
239 @param[in] PageCode The page code of the vital product data.
240 It's ignored if EnableVitalProductData is FALSE.
241
242 @retval EFI_SUCCESS The command executed successfully. See HostAdapterStatus,
243 TargetStatus, SenseDataLength, and SenseData in that order
244 for additional status information.
245 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed, but the entire
246 InquiryDataBuffer could not be transferred. The actual
247 number of bytes transferred is returned in InquiryDataLength.
248 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because there
249 are too many SCSI Command Packets already queued.
250 The SCSI Request Packet was not sent, so no additional
251 status information is available. The caller may retry again later.
252 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send SCSI
253 Request Packet. See HostAdapterStatus, TargetStatus,
254 SenseDataLength, and SenseData in that order for additional
255 status information.
256 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet is not
257 supported by the SCSI initiator(i.e., SCSI Host Controller).
258 The SCSI Request Packet was not sent, so no additional
259 status information is available.
260 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request
261 Packet to execute. See HostAdapterStatus, TargetStatus,
262 SenseDataLength, and SenseData in that order for
263 additional status information.
264 @retval EFI_INVALID_PARAMETER The contents of the SCSI Request Packet are invalid.
265
266 **/
267 EFI_STATUS
268 EFIAPI
269 ScsiInquiryCommandEx (
270 IN EFI_SCSI_IO_PROTOCOL *ScsiIo,
271 IN UINT64 Timeout,
272 IN OUT VOID *SenseData, OPTIONAL
273 IN OUT UINT8 *SenseDataLength,
274 OUT UINT8 *HostAdapterStatus,
275 OUT UINT8 *TargetStatus,
276 IN OUT VOID *InquiryDataBuffer, OPTIONAL
277 IN OUT UINT32 *InquiryDataLength,
278 IN BOOLEAN EnableVitalProductData,
279 IN UINT8 PageCode
280 )
281 {
282 EFI_SCSI_IO_SCSI_REQUEST_PACKET CommandPacket;
283 EFI_STATUS Status;
284 UINT8 Cdb[EFI_SCSI_OP_LENGTH_SIX];
285
286 ASSERT (SenseDataLength != NULL);
287 ASSERT (HostAdapterStatus != NULL);
288 ASSERT (TargetStatus != NULL);
289 ASSERT (InquiryDataLength != NULL);
290 ASSERT (ScsiIo != NULL);
291
292 ZeroMem (&CommandPacket, sizeof (EFI_SCSI_IO_SCSI_REQUEST_PACKET));
293 ZeroMem (Cdb, EFI_SCSI_OP_LENGTH_SIX);
294
295 CommandPacket.Timeout = Timeout;
296 CommandPacket.InDataBuffer = InquiryDataBuffer;
297 CommandPacket.InTransferLength= *InquiryDataLength;
298 CommandPacket.SenseData = SenseData;
299 CommandPacket.SenseDataLength = *SenseDataLength;
300 CommandPacket.Cdb = Cdb;
301
302 Cdb[0] = EFI_SCSI_OP_INQUIRY;
303 if (EnableVitalProductData) {
304 Cdb[1] |= 0x01;
305 Cdb[2] = PageCode;
306 }
307
308 if (*InquiryDataLength > 0xff) {
309 *InquiryDataLength = 0xff;
310 }
311
312 Cdb[4] = (UINT8) (*InquiryDataLength);
313 CommandPacket.CdbLength = (UINT8) EFI_SCSI_OP_LENGTH_SIX;
314 CommandPacket.DataDirection = EFI_SCSI_DATA_IN;
315
316 Status = ScsiIo->ExecuteScsiCommand (ScsiIo, &CommandPacket, NULL);
317
318 *HostAdapterStatus = CommandPacket.HostAdapterStatus;
319 *TargetStatus = CommandPacket.TargetStatus;
320 *SenseDataLength = CommandPacket.SenseDataLength;
321 *InquiryDataLength = CommandPacket.InTransferLength;
322
323 return Status;
324 }
325
326
327 /**
328 Execute Inquiry SCSI command on a specific SCSI target.
329
330 Executes the Inquiry command on the SCSI target specified by ScsiIo.
331 If Timeout is zero, then this function waits indefinitely for the command to complete.
332 If Timeout is greater than zero, then the command is executed and will timeout after Timeout 100 ns units.
333 If ScsiIo is NULL, then ASSERT().
334 If SenseDataLength is NULL, then ASSERT().
335 If HostAdapterStatus is NULL, then ASSERT().
336 If TargetStatus is NULL, then ASSERT().
337 If InquiryDataLength is NULL, then ASSERT().
338
339 If SenseDataLength is non-zero and SenseData is not NULL, SenseData must meet buffer
340 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
341 gets returned.
342
343 If InquiryDataLength is non-zero and InquiryDataBuffer is not NULL, InquiryDataBuffer
344 must meet buffer alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise
345 EFI_INVALID_PARAMETER gets returned.
346
347 @param[in] ScsiIo A pointer to the SCSI I/O Protocol instance
348 for the specific SCSI target.
349 @param[in] Timeout The timeout in 100 ns units to use for the
350 execution of this SCSI Request Packet. A Timeout
351 value of zero means that this function will wait
352 indefinitely for the SCSI Request Packet to execute.
353 If Timeout is greater than zero, then this function
354 will return EFI_TIMEOUT if the time required to
355 execute the SCSI Request Packet is greater than Timeout.
356 @param[in, out] SenseData A pointer to sense data that was generated
357 by the execution of the SCSI Request Packet.
358 This buffer must be allocated by the caller.
359 If SenseDataLength is 0, then this parameter
360 is optional and may be NULL.
361 @param[in, out] SenseDataLength On input, the length in bytes of the SenseData buffer.
362 On output, the number of bytes written to the SenseData buffer.
363 @param[out] HostAdapterStatus The status of the SCSI Host Controller that
364 produces the SCSI bus containing the SCSI
365 target specified by ScsiIo when the SCSI
366 Request Packet was executed. See the EFI
367 SCSI I/O Protocol in the UEFI Specification
368 for details on the possible return values.
369 @param[out] TargetStatus The status returned by the SCSI target specified
370 by ScsiIo when the SCSI Request Packet was
371 executed on the SCSI Host Controller.
372 See the EFI SCSI I/O Protocol in the UEFI
373 Specification for details on the possible
374 return values.
375 @param[in, out] InquiryDataBuffer A pointer to inquiry data that was generated
376 by the execution of the SCSI Request Packet.
377 This buffer must be allocated by the caller.
378 If InquiryDataLength is 0, then this parameter
379 is optional and may be NULL.
380 @param[in, out] InquiryDataLength On input, a pointer to the length in bytes
381 of the InquiryDataBuffer buffer.
382 On output, a pointer to the number of bytes
383 written to the InquiryDataBuffer buffer.
384 @param[in] EnableVitalProductData If TRUE, then the supported vital product
385 data is returned in InquiryDataBuffer.
386 If FALSE, then the standard inquiry data is
387 returned in InquiryDataBuffer.
388
389 @retval EFI_SUCCESS The command was executed successfully. See HostAdapterStatus,
390 TargetStatus, SenseDataLength, and SenseData in that order
391 for additional status information.
392 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed, but the entire
393 InquiryDataBuffer could not be transferred. The actual
394 number of bytes transferred is returned in InquiryDataLength.
395 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because there
396 are too many SCSI Command Packets already queued.
397 The SCSI Request Packet was not sent, so no additional
398 status information is available. The caller may retry again later.
399 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send SCSI
400 Request Packet. See HostAdapterStatus, TargetStatus,
401 SenseDataLength, and SenseData in that order for additional
402 status information.
403 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet is not
404 supported by the SCSI initiator(i.e., SCSI Host Controller).
405 The SCSI Request Packet was not sent, so no additional
406 status information is available.
407 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request
408 Packet to execute. See HostAdapterStatus, TargetStatus,
409 SenseDataLength, and SenseData in that order for
410 additional status information.
411 @retval EFI_INVALID_PARAMETER The contents of the SCSI Request Packet are invalid.
412
413 **/
414 EFI_STATUS
415 EFIAPI
416 ScsiInquiryCommand (
417 IN EFI_SCSI_IO_PROTOCOL *ScsiIo,
418 IN UINT64 Timeout,
419 IN OUT VOID *SenseData, OPTIONAL
420 IN OUT UINT8 *SenseDataLength,
421 OUT UINT8 *HostAdapterStatus,
422 OUT UINT8 *TargetStatus,
423 IN OUT VOID *InquiryDataBuffer, OPTIONAL
424 IN OUT UINT32 *InquiryDataLength,
425 IN BOOLEAN EnableVitalProductData
426 )
427 {
428 return ScsiInquiryCommandEx (
429 ScsiIo,
430 Timeout,
431 SenseData,
432 SenseDataLength,
433 HostAdapterStatus,
434 TargetStatus,
435 InquiryDataBuffer,
436 InquiryDataLength,
437 EnableVitalProductData,
438 0
439 );
440 }
441
442 /**
443 Execute Mode Sense(10) SCSI command on a specific SCSI target.
444
445 Executes the SCSI Mode Sense(10) command on the SCSI target specified by ScsiIo.
446 If Timeout is zero, then this function waits indefinitely for the command to complete.
447 If Timeout is greater than zero, then the command is executed and will timeout
448 after Timeout 100 ns units. The DBDField, PageControl, and PageCode parameters
449 are used to construct the CDB for this SCSI command.
450 If ScsiIo is NULL, then ASSERT().
451 If SenseDataLength is NULL, then ASSERT().
452 If HostAdapterStatus is NULL, then ASSERT().
453 If TargetStatus is NULL, then ASSERT().
454 If DataLength is NULL, then ASSERT().
455
456 If SenseDataLength is non-zero and SenseData is not NULL, SenseData must meet buffer
457 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
458 gets returned.
459
460 If DataLength is non-zero and DataBuffer is not NULL, DataBuffer must meet buffer
461 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
462 gets returned.
463
464 @param[in] ScsiIo A pointer to the SCSI I/O Protocol instance
465 for the specific SCSI target.
466 @param[in] Timeout The timeout in 100 ns units to use for the
467 execution of this SCSI Request Packet. A Timeout
468 value of zero means that this function will wait
469 indefinitely for the SCSI Request Packet to execute.
470 If Timeout is greater than zero, then this function
471 will return EFI_TIMEOUT if the time required to
472 execute the SCSI Request Packet is greater than Timeout.
473 @param[in, out] SenseData A pointer to sense data that was generated
474 by the execution of the SCSI Request Packet.
475 This buffer must be allocated by the caller.
476 If SenseDataLength is 0, then this parameter
477 is optional and may be NULL.
478 @param[in, out] SenseDataLength On input, the length in bytes of the SenseData buffer.
479 On output, the number of bytes written to the SenseData buffer.
480 @param[out] HostAdapterStatus The status of the SCSI Host Controller that
481 produces the SCSI bus containing the SCSI target
482 specified by ScsiIo when the SCSI Request Packet
483 was executed. See the EFI SCSI I/O Protocol in the
484 UEFI Specification for details on the possible
485 return values.
486 @param[out] TargetStatus The status returned by the SCSI target specified
487 by ScsiIo when the SCSI Request Packet was executed
488 on the SCSI Host Controller. See the EFI SCSI
489 I/O Protocol in the UEFI Specification for details
490 on the possible return values.
491 @param[in, out] DataBuffer A pointer to data that was generated by the
492 execution of the SCSI Request Packet. This
493 buffer must be allocated by the caller. If
494 DataLength is 0, then this parameter is optional
495 and may be NULL.
496 @param[in, out] DataLength On input, a pointer to the length in bytes of
497 the DataBuffer buffer. On output, a pointer
498 to the number of bytes written to the DataBuffer
499 buffer.
500 @param[in] DBDField Specifies the DBD field of the CDB for this SCSI Command.
501 @param[in] PageControl Specifies the PC field of the CDB for this SCSI Command.
502 @param[in] PageCode Specifies the Page Control field of the CDB for this SCSI Command.
503
504 @retval EFI_SUCCESS The command was executed successfully.
505 See HostAdapterStatus, TargetStatus, SenseDataLength,
506 and SenseData in that order for additional status information.
507 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed, but the
508 entire DataBuffer could not be transferred.
509 The actual number of bytes transferred is returned
510 in DataLength.
511 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because
512 there are too many SCSI Command Packets already queued.
513 The SCSI Request Packet was not sent, so no additional
514 status information is available. The caller may retry
515 again later.
516 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send
517 SCSI Request Packet. See HostAdapterStatus, TargetStatus,
518 SenseDataLength, and SenseData in that order for
519 additional status information.
520 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet
521 is not supported by the SCSI initiator(i.e., SCSI
522 Host Controller). The SCSI Request Packet was not
523 sent, so no additional status information is available.
524 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI
525 Request Packet to execute. See HostAdapterStatus,
526 TargetStatus, SenseDataLength, and SenseData in that
527 order for additional status information.
528 @retval EFI_INVALID_PARAMETER The contents of the SCSI Request Packet are invalid.
529
530 **/
531 EFI_STATUS
532 EFIAPI
533 ScsiModeSense10Command (
534 IN EFI_SCSI_IO_PROTOCOL *ScsiIo,
535 IN UINT64 Timeout,
536 IN OUT VOID *SenseData, OPTIONAL
537 IN OUT UINT8 *SenseDataLength,
538 OUT UINT8 *HostAdapterStatus,
539 OUT UINT8 *TargetStatus,
540 IN OUT VOID *DataBuffer, OPTIONAL
541 IN OUT UINT32 *DataLength,
542 IN UINT8 DBDField, OPTIONAL
543 IN UINT8 PageControl,
544 IN UINT8 PageCode
545 )
546 {
547 EFI_SCSI_IO_SCSI_REQUEST_PACKET CommandPacket;
548 EFI_STATUS Status;
549 UINT8 Cdb[EFI_SCSI_OP_LENGTH_TEN];
550
551 ASSERT (SenseDataLength != NULL);
552 ASSERT (HostAdapterStatus != NULL);
553 ASSERT (TargetStatus != NULL);
554 ASSERT (DataLength != NULL);
555 ASSERT (ScsiIo != NULL);
556
557 ZeroMem (&CommandPacket, sizeof (EFI_SCSI_IO_SCSI_REQUEST_PACKET));
558 ZeroMem (Cdb, EFI_SCSI_OP_LENGTH_TEN);
559
560 CommandPacket.Timeout = Timeout;
561 CommandPacket.InDataBuffer = DataBuffer;
562 CommandPacket.SenseData = SenseData;
563 CommandPacket.InTransferLength= *DataLength;
564 CommandPacket.Cdb = Cdb;
565 //
566 // Fill Cdb for Mode Sense (10) Command
567 //
568 Cdb[0] = EFI_SCSI_OP_MODE_SEN10;
569 //
570 // DBDField is in Cdb[1] bit3 of (bit7..0)
571 //
572 Cdb[1] = (UINT8) ((DBDField << 3) & 0x08);
573 //
574 // PageControl is in Cdb[2] bit7..6, PageCode is in Cdb[2] bit5..0
575 //
576 Cdb[2] = (UINT8) (((PageControl << 6) & 0xc0) | (PageCode & 0x3f));
577 Cdb[7] = (UINT8) (*DataLength >> 8);
578 Cdb[8] = (UINT8) (*DataLength);
579
580 CommandPacket.CdbLength = EFI_SCSI_OP_LENGTH_TEN;
581 CommandPacket.DataDirection = EFI_SCSI_DATA_IN;
582 CommandPacket.SenseDataLength = *SenseDataLength;
583
584 Status = ScsiIo->ExecuteScsiCommand (ScsiIo, &CommandPacket, NULL);
585
586 *HostAdapterStatus = CommandPacket.HostAdapterStatus;
587 *TargetStatus = CommandPacket.TargetStatus;
588 *SenseDataLength = CommandPacket.SenseDataLength;
589 *DataLength = CommandPacket.InTransferLength;
590
591 return Status;
592 }
593
594
595 /**
596 Execute Request Sense SCSI command on a specific SCSI target.
597
598 Executes the Request Sense command on the SCSI target specified by ScsiIo.
599 If Timeout is zero, then this function waits indefinitely for the command to complete.
600 If Timeout is greater than zero, then the command is executed and will timeout after Timeout 100 ns units.
601 If ScsiIo is NULL, then ASSERT().
602 If SenseDataLength is NULL, then ASSERT().
603 If HostAdapterStatus is NULL, then ASSERT().
604 If TargetStatus is NULL, then ASSERT().
605
606 If SenseDataLength is non-zero and SenseData is not NULL, SenseData must meet buffer
607 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
608 gets returned.
609
610 @param[in] ScsiIo A pointer to SCSI IO protocol.
611 @param[in] Timeout The length of timeout period.
612 @param[in, out] SenseData A pointer to output sense data.
613 @param[in, out] SenseDataLength The length of output sense data.
614 @param[out] HostAdapterStatus The status of Host Adapter.
615 @param[out] TargetStatus The status of the target.
616
617 @retval EFI_SUCCESS Command is executed successfully.
618 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because there are
619 too many SCSI Command Packets already queued.
620 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send SCSI Request Packet.
621 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet is not supported by
622 the SCSI initiator(i.e., SCSI Host Controller)
623 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
624 @retval EFI_INVALID_PARAMETER The contents of the SCSI Request Packet are invalid.
625
626 **/
627 EFI_STATUS
628 EFIAPI
629 ScsiRequestSenseCommand (
630 IN EFI_SCSI_IO_PROTOCOL *ScsiIo,
631 IN UINT64 Timeout,
632 IN OUT VOID *SenseData, OPTIONAL
633 IN OUT UINT8 *SenseDataLength,
634 OUT UINT8 *HostAdapterStatus,
635 OUT UINT8 *TargetStatus
636 )
637 {
638 EFI_SCSI_IO_SCSI_REQUEST_PACKET CommandPacket;
639 EFI_STATUS Status;
640 UINT8 Cdb[EFI_SCSI_OP_LENGTH_SIX];
641
642 ASSERT (SenseDataLength != NULL);
643 ASSERT (HostAdapterStatus != NULL);
644 ASSERT (TargetStatus != NULL);
645 ASSERT (ScsiIo != NULL);
646
647 ZeroMem (&CommandPacket, sizeof (EFI_SCSI_IO_SCSI_REQUEST_PACKET));
648 ZeroMem (Cdb, EFI_SCSI_OP_LENGTH_SIX);
649
650 CommandPacket.Timeout = Timeout;
651 CommandPacket.InDataBuffer = SenseData;
652 CommandPacket.SenseData = NULL;
653 CommandPacket.InTransferLength= *SenseDataLength;
654 CommandPacket.Cdb = Cdb;
655 //
656 // Fill Cdb for Request Sense Command
657 //
658 Cdb[0] = EFI_SCSI_OP_REQUEST_SENSE;
659 Cdb[4] = (UINT8) (*SenseDataLength);
660
661 CommandPacket.CdbLength = (UINT8) EFI_SCSI_OP_LENGTH_SIX;
662 CommandPacket.DataDirection = EFI_SCSI_DATA_IN;
663 CommandPacket.SenseDataLength = 0;
664
665 Status = ScsiIo->ExecuteScsiCommand (ScsiIo, &CommandPacket, NULL);
666
667 *HostAdapterStatus = CommandPacket.HostAdapterStatus;
668 *TargetStatus = CommandPacket.TargetStatus;
669 *SenseDataLength = (UINT8) CommandPacket.InTransferLength;
670
671 return Status;
672 }
673
674
675 /**
676 Execute Read Capacity SCSI command on a specific SCSI target.
677
678 Executes the SCSI Read Capacity command on the SCSI target specified by ScsiIo.
679 If Timeout is zero, then this function waits indefinitely for the command to complete.
680 If Timeout is greater than zero, then the command is executed and will timeout after
681 Timeout 100 ns units. The Pmi parameter is used to construct the CDB for this SCSI command.
682 If ScsiIo is NULL, then ASSERT().
683 If SenseDataLength is NULL, then ASSERT().
684 If HostAdapterStatus is NULL, then ASSERT().
685 If TargetStatus is NULL, then ASSERT().
686 If DataLength is NULL, then ASSERT().
687
688 If SenseDataLength is non-zero and SenseData is not NULL, SenseData must meet buffer
689 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
690 gets returned.
691
692 If DataLength is non-zero and DataBuffer is not NULL, DataBuffer must meet buffer
693 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
694 gets returned.
695
696 @param[in] ScsiIo A pointer to SCSI IO protocol.
697 @param[in] Timeout The length of timeout period.
698 @param[in, out] SenseData A pointer to output sense data.
699 @param[in, out] SenseDataLength The length of output sense data.
700 @param[out] HostAdapterStatus The status of Host Adapter.
701 @param[out] TargetStatus The status of the target.
702 @param[in, out] DataBuffer A pointer to a data buffer.
703 @param[in, out] DataLength The length of data buffer.
704 @param[in] Pmi Partial medium indicator.
705
706 @retval EFI_SUCCESS Command is executed successfully.
707 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed, but the entire
708 DataBuffer could not be transferred. The actual
709 number of bytes transferred is returned in DataLength.
710 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because
711 there are too many SCSI Command Packets already queued.
712 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send SCSI Request Packet.
713 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet
714 is not supported by the SCSI initiator(i.e., SCSI Host Controller)
715 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
716 @retval EFI_INVALID_PARAMETER The contents of the SCSI Request Packet are invalid.
717
718 **/
719 EFI_STATUS
720 EFIAPI
721 ScsiReadCapacityCommand (
722 IN EFI_SCSI_IO_PROTOCOL *ScsiIo,
723 IN UINT64 Timeout,
724 IN OUT VOID *SenseData, OPTIONAL
725 IN OUT UINT8 *SenseDataLength,
726 OUT UINT8 *HostAdapterStatus,
727 OUT UINT8 *TargetStatus,
728 IN OUT VOID *DataBuffer, OPTIONAL
729 IN OUT UINT32 *DataLength,
730 IN BOOLEAN Pmi
731 )
732 {
733 EFI_SCSI_IO_SCSI_REQUEST_PACKET CommandPacket;
734 EFI_STATUS Status;
735 UINT8 Cdb[EFI_SCSI_OP_LENGTH_TEN];
736
737 ASSERT (SenseDataLength != NULL);
738 ASSERT (HostAdapterStatus != NULL);
739 ASSERT (TargetStatus != NULL);
740 ASSERT (DataLength != NULL);
741 ASSERT (ScsiIo != NULL);
742
743 ZeroMem (&CommandPacket, sizeof (EFI_SCSI_IO_SCSI_REQUEST_PACKET));
744 ZeroMem (Cdb, EFI_SCSI_OP_LENGTH_TEN);
745
746 CommandPacket.Timeout = Timeout;
747 CommandPacket.InDataBuffer = DataBuffer;
748 CommandPacket.SenseData = SenseData;
749 CommandPacket.InTransferLength= *DataLength;
750 CommandPacket.Cdb = Cdb;
751 //
752 // Fill Cdb for Read Capacity Command
753 //
754 Cdb[0] = EFI_SCSI_OP_READ_CAPACITY;
755 if (!Pmi) {
756 //
757 // Partial medium indicator,if Pmi is FALSE, the Cdb.2 ~ Cdb.5 MUST BE ZERO.
758 //
759 ZeroMem ((Cdb + 2), 4);
760 } else {
761 Cdb[8] |= 0x01;
762 }
763
764 CommandPacket.CdbLength = EFI_SCSI_OP_LENGTH_TEN;
765 CommandPacket.DataDirection = EFI_SCSI_DATA_IN;
766 CommandPacket.SenseDataLength = *SenseDataLength;
767
768 Status = ScsiIo->ExecuteScsiCommand (ScsiIo, &CommandPacket, NULL);
769
770 *HostAdapterStatus = CommandPacket.HostAdapterStatus;
771 *TargetStatus = CommandPacket.TargetStatus;
772 *SenseDataLength = CommandPacket.SenseDataLength;
773 *DataLength = CommandPacket.InTransferLength;
774
775 return Status;
776 }
777
778
779 /**
780 Execute Read Capacity SCSI 16 command on a specific SCSI target.
781
782 Executes the SCSI Read Capacity 16 command on the SCSI target specified by ScsiIo.
783 If Timeout is zero, then this function waits indefinitely for the command to complete.
784 If Timeout is greater than zero, then the command is executed and will timeout after
785 Timeout 100 ns units. The Pmi parameter is used to construct the CDB for this SCSI command.
786 If ScsiIo is NULL, then ASSERT().
787 If SenseDataLength is NULL, then ASSERT().
788 If HostAdapterStatus is NULL, then ASSERT().
789 If TargetStatus is NULL, then ASSERT().
790 If DataLength is NULL, then ASSERT().
791
792 If SenseDataLength is non-zero and SenseData is not NULL, SenseData must meet buffer
793 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
794 gets returned.
795
796 If DataLength is non-zero and DataBuffer is not NULL, DataBuffer must meet buffer
797 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
798 gets returned.
799
800 @param[in] ScsiIo A pointer to SCSI IO protocol.
801 @param[in] Timeout The length of timeout period.
802 @param[in, out] SenseData A pointer to output sense data.
803 @param[in, out] SenseDataLength The length of output sense data.
804 @param[out] HostAdapterStatus The status of Host Adapter.
805 @param[out] TargetStatus The status of the target.
806 @param[in, out] DataBuffer A pointer to a data buffer.
807 @param[in, out] DataLength The length of data buffer.
808 @param[in] Pmi Partial medium indicator.
809
810 @retval EFI_SUCCESS Command is executed successfully.
811 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed, but the entire
812 DataBuffer could not be transferred. The actual
813 number of bytes transferred is returned in DataLength.
814 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because
815 there are too many SCSI Command Packets already queued.
816 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send SCSI Request Packet.
817 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet
818 is not supported by the SCSI initiator(i.e., SCSI Host Controller)
819 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
820 @retval EFI_INVALID_PARAMETER The contents of the SCSI Request Packet are invalid.
821
822 **/
823 EFI_STATUS
824 EFIAPI
825 ScsiReadCapacity16Command (
826 IN EFI_SCSI_IO_PROTOCOL *ScsiIo,
827 IN UINT64 Timeout,
828 IN OUT VOID *SenseData, OPTIONAL
829 IN OUT UINT8 *SenseDataLength,
830 OUT UINT8 *HostAdapterStatus,
831 OUT UINT8 *TargetStatus,
832 IN OUT VOID *DataBuffer, OPTIONAL
833 IN OUT UINT32 *DataLength,
834 IN BOOLEAN Pmi
835 )
836 {
837 EFI_SCSI_IO_SCSI_REQUEST_PACKET CommandPacket;
838 EFI_STATUS Status;
839 UINT8 Cdb[16];
840
841 ASSERT (SenseDataLength != NULL);
842 ASSERT (HostAdapterStatus != NULL);
843 ASSERT (TargetStatus != NULL);
844 ASSERT (DataLength != NULL);
845 ASSERT (ScsiIo != NULL);
846
847 ZeroMem (&CommandPacket, sizeof (EFI_SCSI_IO_SCSI_REQUEST_PACKET));
848 ZeroMem (Cdb, 16);
849
850 CommandPacket.Timeout = Timeout;
851 CommandPacket.InDataBuffer = DataBuffer;
852 CommandPacket.SenseData = SenseData;
853 CommandPacket.InTransferLength= *DataLength;
854 CommandPacket.Cdb = Cdb;
855 //
856 // Fill Cdb for Read Capacity Command
857 //
858 Cdb[0] = EFI_SCSI_OP_READ_CAPACITY16;
859 Cdb[1] = 0x10;
860 if (!Pmi) {
861 //
862 // Partial medium indicator,if Pmi is FALSE, the Cdb.2 ~ Cdb.9 MUST BE ZERO.
863 //
864 ZeroMem ((Cdb + 2), 8);
865 } else {
866 Cdb[14] |= 0x01;
867 }
868
869 Cdb[13] = 0x20;
870 CommandPacket.CdbLength = 16;
871 CommandPacket.DataDirection = EFI_SCSI_DATA_IN;
872 CommandPacket.SenseDataLength = *SenseDataLength;
873
874 Status = ScsiIo->ExecuteScsiCommand (ScsiIo, &CommandPacket, NULL);
875
876 *HostAdapterStatus = CommandPacket.HostAdapterStatus;
877 *TargetStatus = CommandPacket.TargetStatus;
878 *SenseDataLength = CommandPacket.SenseDataLength;
879 *DataLength = CommandPacket.InTransferLength;
880
881 return Status;
882 }
883
884
885 /**
886 Execute Read(10) SCSI command on a specific SCSI target.
887
888 Executes the SCSI Read(10) command on the SCSI target specified by ScsiIo.
889 If Timeout is zero, then this function waits indefinitely for the command to complete.
890 If Timeout is greater than zero, then the command is executed and will timeout
891 after Timeout 100 ns units. The StartLba and SectorSize parameters are used to
892 construct the CDB for this SCSI command.
893 If ScsiIo is NULL, then ASSERT().
894 If SenseDataLength is NULL, then ASSERT().
895 If HostAdapterStatus is NULL, then ASSERT().
896 If TargetStatus is NULL, then ASSERT().
897 If DataLength is NULL, then ASSERT().
898
899 If SenseDataLength is non-zero and SenseData is not NULL, SenseData must meet buffer
900 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
901 gets returned.
902
903 If DataLength is non-zero and DataBuffer is not NULL, DataBuffer must meet buffer
904 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
905 gets returned.
906
907 @param[in] ScsiIo A pointer to SCSI IO protocol.
908 @param[in] Timeout The length of timeout period.
909 @param[in, out] SenseData A pointer to output sense data.
910 @param[in, out] SenseDataLength The length of output sense data.
911 @param[out] HostAdapterStatus The status of Host Adapter.
912 @param[out] TargetStatus The status of the target.
913 @param[in, out] DataBuffer Read 10 command data.
914 @param[in, out] DataLength The length of data buffer.
915 @param[in] StartLba The start address of LBA.
916 @param[in] SectorSize The number of contiguous logical blocks of data that shall be transferred.
917
918 @retval EFI_SUCCESS Command is executed successfully.
919 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed, but the entire DataBuffer could
920 not be transferred. The actual number of bytes transferred is returned in DataLength.
921 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because there are too many
922 SCSI Command Packets already queued.
923 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send SCSI Request Packet.
924 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet is not supported by
925 the SCSI initiator(i.e., SCSI Host Controller)
926 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
927 @retval EFI_INVALID_PARAMETER The contents of the SCSI Request Packet are invalid.
928
929 **/
930 EFI_STATUS
931 EFIAPI
932 ScsiRead10Command (
933 IN EFI_SCSI_IO_PROTOCOL *ScsiIo,
934 IN UINT64 Timeout,
935 IN OUT VOID *SenseData, OPTIONAL
936 IN OUT UINT8 *SenseDataLength,
937 OUT UINT8 *HostAdapterStatus,
938 OUT UINT8 *TargetStatus,
939 IN OUT VOID *DataBuffer, OPTIONAL
940 IN OUT UINT32 *DataLength,
941 IN UINT32 StartLba,
942 IN UINT32 SectorSize
943 )
944 {
945 EFI_SCSI_IO_SCSI_REQUEST_PACKET CommandPacket;
946 EFI_STATUS Status;
947 UINT8 Cdb[EFI_SCSI_OP_LENGTH_TEN];
948
949 ASSERT (SenseDataLength != NULL);
950 ASSERT (HostAdapterStatus != NULL);
951 ASSERT (TargetStatus != NULL);
952 ASSERT (DataLength != NULL);
953 ASSERT (ScsiIo != NULL);
954
955 ZeroMem (&CommandPacket, sizeof (EFI_SCSI_IO_SCSI_REQUEST_PACKET));
956 ZeroMem (Cdb, EFI_SCSI_OP_LENGTH_TEN);
957
958 CommandPacket.Timeout = Timeout;
959 CommandPacket.InDataBuffer = DataBuffer;
960 CommandPacket.SenseData = SenseData;
961 CommandPacket.InTransferLength= *DataLength;
962 CommandPacket.Cdb = Cdb;
963 //
964 // Fill Cdb for Read (10) Command
965 //
966 Cdb[0] = EFI_SCSI_OP_READ10;
967 WriteUnaligned32 ((UINT32 *)&Cdb[2], SwapBytes32 (StartLba));
968 WriteUnaligned16 ((UINT16 *)&Cdb[7], SwapBytes16 ((UINT16) SectorSize));
969
970 CommandPacket.CdbLength = EFI_SCSI_OP_LENGTH_TEN;
971 CommandPacket.DataDirection = EFI_SCSI_DATA_IN;
972 CommandPacket.SenseDataLength = *SenseDataLength;
973
974 Status = ScsiIo->ExecuteScsiCommand (ScsiIo, &CommandPacket, NULL);
975
976 *HostAdapterStatus = CommandPacket.HostAdapterStatus;
977 *TargetStatus = CommandPacket.TargetStatus;
978 *SenseDataLength = CommandPacket.SenseDataLength;
979 *DataLength = CommandPacket.InTransferLength;
980
981 return Status;
982 }
983
984
985 /**
986 Execute Write(10) SCSI command on a specific SCSI target.
987
988 Executes the SCSI Write(10) command on the SCSI target specified by ScsiIo.
989 If Timeout is zero, then this function waits indefinitely for the command to complete.
990 If Timeout is greater than zero, then the command is executed and will timeout after
991 Timeout 100 ns units. The StartLba and SectorSize parameters are used to construct
992 the CDB for this SCSI command.
993 If ScsiIo is NULL, then ASSERT().
994 If SenseDataLength is NULL, then ASSERT().
995 If HostAdapterStatus is NULL, then ASSERT().
996 If TargetStatus is NULL, then ASSERT().
997 If DataLength is NULL, then ASSERT().
998
999 If SenseDataLength is non-zero and SenseData is not NULL, SenseData must meet buffer
1000 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
1001 gets returned.
1002
1003 If DataLength is non-zero and DataBuffer is not NULL, DataBuffer must meet buffer
1004 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
1005 gets returned.
1006
1007 @param[in] ScsiIo SCSI IO Protocol to use
1008 @param[in] Timeout The length of timeout period.
1009 @param[in, out] SenseData A pointer to output sense data.
1010 @param[in, out] SenseDataLength The length of output sense data.
1011 @param[out] HostAdapterStatus The status of Host Adapter.
1012 @param[out] TargetStatus The status of the target.
1013 @param[in, out] DataBuffer A pointer to a data buffer.
1014 @param[in, out] DataLength The length of data buffer.
1015 @param[in] StartLba The start address of LBA.
1016 @param[in] SectorSize The number of contiguous logical blocks of data that shall be transferred.
1017
1018 @retval EFI_SUCCESS Command is executed successfully.
1019 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed, but the entire DataBuffer could
1020 not be transferred. The actual number of bytes transferred is returned in DataLength.
1021 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because there are too many
1022 SCSI Command Packets already queued.
1023 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send SCSI Request Packet.
1024 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet is not supported by
1025 the SCSI initiator(i.e., SCSI Host Controller)
1026 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
1027 @retval EFI_INVALID_PARAMETER The contents of the SCSI Request Packet are invalid.
1028
1029 **/
1030 EFI_STATUS
1031 EFIAPI
1032 ScsiWrite10Command (
1033 IN EFI_SCSI_IO_PROTOCOL *ScsiIo,
1034 IN UINT64 Timeout,
1035 IN OUT VOID *SenseData, OPTIONAL
1036 IN OUT UINT8 *SenseDataLength,
1037 OUT UINT8 *HostAdapterStatus,
1038 OUT UINT8 *TargetStatus,
1039 IN OUT VOID *DataBuffer, OPTIONAL
1040 IN OUT UINT32 *DataLength,
1041 IN UINT32 StartLba,
1042 IN UINT32 SectorSize
1043 )
1044 {
1045 EFI_SCSI_IO_SCSI_REQUEST_PACKET CommandPacket;
1046 EFI_STATUS Status;
1047 UINT8 Cdb[EFI_SCSI_OP_LENGTH_TEN];
1048
1049 ASSERT (SenseDataLength != NULL);
1050 ASSERT (HostAdapterStatus != NULL);
1051 ASSERT (TargetStatus != NULL);
1052 ASSERT (DataLength != NULL);
1053 ASSERT (ScsiIo != NULL);
1054
1055 ZeroMem (&CommandPacket, sizeof (EFI_SCSI_IO_SCSI_REQUEST_PACKET));
1056 ZeroMem (Cdb, EFI_SCSI_OP_LENGTH_TEN);
1057
1058 CommandPacket.Timeout = Timeout;
1059 CommandPacket.OutDataBuffer = DataBuffer;
1060 CommandPacket.SenseData = SenseData;
1061 CommandPacket.OutTransferLength = *DataLength;
1062 CommandPacket.Cdb = Cdb;
1063 //
1064 // Fill Cdb for Write (10) Command
1065 //
1066 Cdb[0] = EFI_SCSI_OP_WRITE10;
1067 Cdb[1] = EFI_SCSI_BLOCK_FUA;
1068 WriteUnaligned32 ((UINT32 *)&Cdb[2], SwapBytes32 (StartLba));
1069 WriteUnaligned16 ((UINT16 *)&Cdb[7], SwapBytes16 ((UINT16) SectorSize));
1070
1071 CommandPacket.CdbLength = EFI_SCSI_OP_LENGTH_TEN;
1072 CommandPacket.DataDirection = EFI_SCSI_DATA_OUT;
1073 CommandPacket.SenseDataLength = *SenseDataLength;
1074
1075 Status = ScsiIo->ExecuteScsiCommand (ScsiIo, &CommandPacket, NULL);
1076
1077 *HostAdapterStatus = CommandPacket.HostAdapterStatus;
1078 *TargetStatus = CommandPacket.TargetStatus;
1079 *SenseDataLength = CommandPacket.SenseDataLength;
1080 *DataLength = CommandPacket.OutTransferLength;
1081
1082 return Status;
1083 }
1084
1085 /**
1086 Execute Read(16) SCSI command on a specific SCSI target.
1087
1088 Executes the SCSI Read(16) command on the SCSI target specified by ScsiIo.
1089 If Timeout is zero, then this function waits indefinitely for the command to complete.
1090 If Timeout is greater than zero, then the command is executed and will timeout
1091 after Timeout 100 ns units. The StartLba and SectorSize parameters are used to
1092 construct the CDB for this SCSI command.
1093 If ScsiIo is NULL, then ASSERT().
1094 If SenseDataLength is NULL, then ASSERT().
1095 If HostAdapterStatus is NULL, then ASSERT().
1096 If TargetStatus is NULL, then ASSERT().
1097 If DataLength is NULL, then ASSERT().
1098
1099 If SenseDataLength is non-zero and SenseData is not NULL, SenseData must meet buffer
1100 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
1101 gets returned.
1102
1103 If DataLength is non-zero and DataBuffer is not NULL, DataBuffer must meet buffer
1104 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
1105 gets returned.
1106
1107 @param[in] ScsiIo A pointer to SCSI IO protocol.
1108 @param[in] Timeout The length of timeout period.
1109 @param[in, out] SenseData A pointer to output sense data.
1110 @param[in, out] SenseDataLength The length of output sense data.
1111 @param[out] HostAdapterStatus The status of Host Adapter.
1112 @param[out] TargetStatus The status of the target.
1113 @param[in, out] DataBuffer Read 16 command data.
1114 @param[in, out] DataLength The length of data buffer.
1115 @param[in] StartLba The start address of LBA.
1116 @param[in] SectorSize The number of contiguous logical blocks of data that shall be transferred.
1117
1118 @retval EFI_SUCCESS Command is executed successfully.
1119 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed, but the entire DataBuffer could
1120 not be transferred. The actual number of bytes transferred is returned in DataLength.
1121 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because there are too many
1122 SCSI Command Packets already queued.
1123 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send SCSI Request Packet.
1124 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet is not supported by
1125 the SCSI initiator(i.e., SCSI Host Controller)
1126 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
1127 @retval EFI_INVALID_PARAMETER The contents of the SCSI Request Packet are invalid.
1128
1129 **/
1130 EFI_STATUS
1131 EFIAPI
1132 ScsiRead16Command (
1133 IN EFI_SCSI_IO_PROTOCOL *ScsiIo,
1134 IN UINT64 Timeout,
1135 IN OUT VOID *SenseData, OPTIONAL
1136 IN OUT UINT8 *SenseDataLength,
1137 OUT UINT8 *HostAdapterStatus,
1138 OUT UINT8 *TargetStatus,
1139 IN OUT VOID *DataBuffer, OPTIONAL
1140 IN OUT UINT32 *DataLength,
1141 IN UINT64 StartLba,
1142 IN UINT32 SectorSize
1143 )
1144 {
1145 EFI_SCSI_IO_SCSI_REQUEST_PACKET CommandPacket;
1146 EFI_STATUS Status;
1147 UINT8 Cdb[EFI_SCSI_OP_LENGTH_SIXTEEN];
1148
1149 ASSERT (SenseDataLength != NULL);
1150 ASSERT (HostAdapterStatus != NULL);
1151 ASSERT (TargetStatus != NULL);
1152 ASSERT (DataLength != NULL);
1153 ASSERT (ScsiIo != NULL);
1154
1155 ZeroMem (&CommandPacket, sizeof (EFI_SCSI_IO_SCSI_REQUEST_PACKET));
1156 ZeroMem (Cdb, EFI_SCSI_OP_LENGTH_SIXTEEN);
1157
1158 CommandPacket.Timeout = Timeout;
1159 CommandPacket.InDataBuffer = DataBuffer;
1160 CommandPacket.SenseData = SenseData;
1161 CommandPacket.InTransferLength = *DataLength;
1162 CommandPacket.Cdb = Cdb;
1163 //
1164 // Fill Cdb for Read (16) Command
1165 //
1166 Cdb[0] = EFI_SCSI_OP_READ16;
1167 WriteUnaligned64 ((UINT64 *)&Cdb[2], SwapBytes64 (StartLba));
1168 WriteUnaligned32 ((UINT32 *)&Cdb[10], SwapBytes32 (SectorSize));
1169
1170 CommandPacket.CdbLength = EFI_SCSI_OP_LENGTH_SIXTEEN;
1171 CommandPacket.DataDirection = EFI_SCSI_DATA_IN;
1172 CommandPacket.SenseDataLength = *SenseDataLength;
1173
1174 Status = ScsiIo->ExecuteScsiCommand (ScsiIo, &CommandPacket, NULL);
1175
1176 *HostAdapterStatus = CommandPacket.HostAdapterStatus;
1177 *TargetStatus = CommandPacket.TargetStatus;
1178 *SenseDataLength = CommandPacket.SenseDataLength;
1179 *DataLength = CommandPacket.InTransferLength;
1180
1181 return Status;
1182 }
1183
1184
1185 /**
1186 Execute Write(16) SCSI command on a specific SCSI target.
1187
1188 Executes the SCSI Write(16) command on the SCSI target specified by ScsiIo.
1189 If Timeout is zero, then this function waits indefinitely for the command to complete.
1190 If Timeout is greater than zero, then the command is executed and will timeout after
1191 Timeout 100 ns units. The StartLba and SectorSize parameters are used to construct
1192 the CDB for this SCSI command.
1193 If ScsiIo is NULL, then ASSERT().
1194 If SenseDataLength is NULL, then ASSERT().
1195 If HostAdapterStatus is NULL, then ASSERT().
1196 If TargetStatus is NULL, then ASSERT().
1197 If DataLength is NULL, then ASSERT().
1198
1199 If SenseDataLength is non-zero and SenseData is not NULL, SenseData must meet buffer
1200 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
1201 gets returned.
1202
1203 If DataLength is non-zero and DataBuffer is not NULL, DataBuffer must meet buffer
1204 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
1205 gets returned.
1206
1207 @param[in] ScsiIo SCSI IO Protocol to use
1208 @param[in] Timeout The length of timeout period.
1209 @param[in, out] SenseData A pointer to output sense data.
1210 @param[in, out] SenseDataLength The length of output sense data.
1211 @param[out] HostAdapterStatus The status of Host Adapter.
1212 @param[out] TargetStatus The status of the target.
1213 @param[in, out] DataBuffer A pointer to a data buffer.
1214 @param[in, out] DataLength The length of data buffer.
1215 @param[in] StartLba The start address of LBA.
1216 @param[in] SectorSize The number of contiguous logical blocks of data that shall be transferred.
1217
1218 @retval EFI_SUCCESS Command is executed successfully.
1219 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed, but the entire DataBuffer could
1220 not be transferred. The actual number of bytes transferred is returned in DataLength.
1221 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because there are too many
1222 SCSI Command Packets already queued.
1223 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send SCSI Request Packet.
1224 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet is not supported by
1225 the SCSI initiator(i.e., SCSI Host Controller)
1226 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
1227 @retval EFI_INVALID_PARAMETER The contents of the SCSI Request Packet are invalid.
1228
1229 **/
1230 EFI_STATUS
1231 EFIAPI
1232 ScsiWrite16Command (
1233 IN EFI_SCSI_IO_PROTOCOL *ScsiIo,
1234 IN UINT64 Timeout,
1235 IN OUT VOID *SenseData, OPTIONAL
1236 IN OUT UINT8 *SenseDataLength,
1237 OUT UINT8 *HostAdapterStatus,
1238 OUT UINT8 *TargetStatus,
1239 IN OUT VOID *DataBuffer, OPTIONAL
1240 IN OUT UINT32 *DataLength,
1241 IN UINT64 StartLba,
1242 IN UINT32 SectorSize
1243 )
1244 {
1245 EFI_SCSI_IO_SCSI_REQUEST_PACKET CommandPacket;
1246 EFI_STATUS Status;
1247 UINT8 Cdb[EFI_SCSI_OP_LENGTH_SIXTEEN];
1248
1249 ASSERT (SenseDataLength != NULL);
1250 ASSERT (HostAdapterStatus != NULL);
1251 ASSERT (TargetStatus != NULL);
1252 ASSERT (DataLength != NULL);
1253 ASSERT (ScsiIo != NULL);
1254
1255 ZeroMem (&CommandPacket, sizeof (EFI_SCSI_IO_SCSI_REQUEST_PACKET));
1256 ZeroMem (Cdb, EFI_SCSI_OP_LENGTH_SIXTEEN);
1257
1258 CommandPacket.Timeout = Timeout;
1259 CommandPacket.OutDataBuffer = DataBuffer;
1260 CommandPacket.SenseData = SenseData;
1261 CommandPacket.OutTransferLength = *DataLength;
1262 CommandPacket.Cdb = Cdb;
1263 //
1264 // Fill Cdb for Write (16) Command
1265 //
1266 Cdb[0] = EFI_SCSI_OP_WRITE16;
1267 Cdb[1] = EFI_SCSI_BLOCK_FUA;
1268 WriteUnaligned64 ((UINT64 *)&Cdb[2], SwapBytes64 (StartLba));
1269 WriteUnaligned32 ((UINT32 *)&Cdb[10], SwapBytes32 (SectorSize));
1270
1271 CommandPacket.CdbLength = EFI_SCSI_OP_LENGTH_SIXTEEN;
1272 CommandPacket.DataDirection = EFI_SCSI_DATA_OUT;
1273 CommandPacket.SenseDataLength = *SenseDataLength;
1274
1275 Status = ScsiIo->ExecuteScsiCommand (ScsiIo, &CommandPacket, NULL);
1276
1277 *HostAdapterStatus = CommandPacket.HostAdapterStatus;
1278 *TargetStatus = CommandPacket.TargetStatus;
1279 *SenseDataLength = CommandPacket.SenseDataLength;
1280 *DataLength = CommandPacket.OutTransferLength;
1281
1282 return Status;
1283 }
1284
1285
1286 /**
1287 Execute Security Protocol In SCSI command on a specific SCSI target.
1288
1289 Executes the SCSI Security Protocol In command on the SCSI target specified by ScsiIo.
1290 If Timeout is zero, then this function waits indefinitely for the command to complete.
1291 If Timeout is greater than zero, then the command is executed and will timeout after
1292 Timeout 100 ns units.
1293 If ScsiIo is NULL, then ASSERT().
1294 If SenseDataLength is NULL, then ASSERT().
1295 If HostAdapterStatus is NULL, then ASSERT().
1296 If TargetStatus is NULL, then ASSERT().
1297 If TransferLength is NULL, then ASSERT().
1298
1299 If SenseDataLength is non-zero and SenseData is not NULL, SenseData must meet buffer
1300 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
1301 gets returned.
1302
1303 If DataLength is non-zero and DataBuffer is not NULL, DataBuffer must meet buffer
1304 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
1305 gets returned.
1306
1307 @param[in] ScsiIo SCSI IO Protocol to use.
1308 @param[in] Timeout The length of timeout period.
1309 @param[in, out] SenseData A pointer to output sense data.
1310 @param[in, out] SenseDataLength The length of output sense data.
1311 @param[out] HostAdapterStatus The status of Host Adapter.
1312 @param[out] TargetStatus The status of the target.
1313 @param[in] SecurityProtocol The Security Protocol to use.
1314 @param[in] SecurityProtocolSpecific The Security Protocol Specific data.
1315 @param[in] Inc512 If TRUE, 512 increment (INC_512) bit will be set for the
1316 SECURITY PROTOCOL IN command.
1317 @param[in] DataLength The size in bytes of the data buffer.
1318 @param[in, out] DataBuffer A pointer to a data buffer.
1319 @param[out] TransferLength A pointer to a buffer to store the size in
1320 bytes of the data written to the data buffer.
1321
1322 @retval EFI_SUCCESS Command is executed successfully.
1323 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed, but the entire DataBuffer could
1324 not be transferred. The actual number of bytes transferred is returned in TransferLength.
1325 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because there are too many
1326 SCSI Command Packets already queued.
1327 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send SCSI Request Packet.
1328 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet is not supported by
1329 the SCSI initiator(i.e., SCSI Host Controller)
1330 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
1331 @retval EFI_INVALID_PARAMETER The contents of the SCSI Request Packet are invalid.
1332
1333 **/
1334 EFI_STATUS
1335 EFIAPI
1336 ScsiSecurityProtocolInCommand (
1337 IN EFI_SCSI_IO_PROTOCOL *ScsiIo,
1338 IN UINT64 Timeout,
1339 IN OUT VOID *SenseData, OPTIONAL
1340 IN OUT UINT8 *SenseDataLength,
1341 OUT UINT8 *HostAdapterStatus,
1342 OUT UINT8 *TargetStatus,
1343 IN UINT8 SecurityProtocol,
1344 IN UINT16 SecurityProtocolSpecific,
1345 IN BOOLEAN Inc512,
1346 IN UINTN DataLength,
1347 IN OUT VOID *DataBuffer, OPTIONAL
1348 OUT UINTN *TransferLength
1349 )
1350 {
1351 EFI_SCSI_IO_SCSI_REQUEST_PACKET CommandPacket;
1352 EFI_STATUS Status;
1353 UINT8 Cdb[EFI_SCSI_OP_LENGTH_TWELVE];
1354
1355 ASSERT (SenseDataLength != NULL);
1356 ASSERT (HostAdapterStatus != NULL);
1357 ASSERT (TargetStatus != NULL);
1358 ASSERT (ScsiIo != NULL);
1359 ASSERT (TransferLength != NULL);
1360 ASSERT (DataLength <= MAX_UINT32);
1361
1362 ZeroMem (&CommandPacket, sizeof (EFI_SCSI_IO_SCSI_REQUEST_PACKET));
1363 ZeroMem (Cdb, EFI_SCSI_OP_LENGTH_TWELVE);
1364
1365 CommandPacket.Timeout = Timeout;
1366 CommandPacket.InDataBuffer = DataBuffer;
1367 CommandPacket.SenseData = SenseData;
1368 CommandPacket.InTransferLength = (UINT32) DataLength;
1369 CommandPacket.Cdb = Cdb;
1370 //
1371 // Fill Cdb for Security Protocol In Command
1372 //
1373 Cdb[0] = EFI_SCSI_OP_SECURITY_PROTOCOL_IN;
1374 Cdb[1] = SecurityProtocol;
1375 WriteUnaligned16 ((UINT16 *)&Cdb[2], SwapBytes16 (SecurityProtocolSpecific));
1376
1377 if (Inc512) {
1378 if (DataLength % 512 != 0) {
1379 return EFI_INVALID_PARAMETER;
1380 }
1381 Cdb[4] = BIT7;
1382 WriteUnaligned32 ((UINT32 *)&Cdb[6], SwapBytes32 ((UINT32) DataLength / 512));
1383 } else {
1384 WriteUnaligned32 ((UINT32 *)&Cdb[6], SwapBytes32 ((UINT32) DataLength));
1385 }
1386
1387 CommandPacket.CdbLength = EFI_SCSI_OP_LENGTH_TWELVE;
1388 CommandPacket.DataDirection = EFI_SCSI_DATA_IN;
1389 CommandPacket.SenseDataLength = *SenseDataLength;
1390
1391 Status = ScsiIo->ExecuteScsiCommand (ScsiIo, &CommandPacket, NULL);
1392
1393 *HostAdapterStatus = CommandPacket.HostAdapterStatus;
1394 *TargetStatus = CommandPacket.TargetStatus;
1395 *SenseDataLength = CommandPacket.SenseDataLength;
1396 *TransferLength = (UINTN) CommandPacket.InTransferLength;
1397
1398 return Status;
1399 }
1400
1401
1402 /**
1403 Execute Security Protocol Out SCSI command on a specific SCSI target.
1404
1405 Executes the SCSI Security Protocol Out command on the SCSI target specified by ScsiIo.
1406 If Timeout is zero, then this function waits indefinitely for the command to complete.
1407 If Timeout is greater than zero, then the command is executed and will timeout after
1408 Timeout 100 ns units.
1409 If ScsiIo is NULL, then ASSERT().
1410 If SenseDataLength is NULL, then ASSERT().
1411 If HostAdapterStatus is NULL, then ASSERT().
1412 If TargetStatus is NULL, then ASSERT().
1413
1414 If SenseDataLength is non-zero and SenseData is not NULL, SenseData must meet buffer
1415 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
1416 gets returned.
1417
1418 If DataLength is non-zero and DataBuffer is not NULL, DataBuffer must meet buffer
1419 alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise EFI_INVALID_PARAMETER
1420 gets returned.
1421
1422 @param[in] ScsiIo SCSI IO Protocol to use.
1423 @param[in] Timeout The length of timeout period.
1424 @param[in, out] SenseData A pointer to output sense data.
1425 @param[in, out] SenseDataLength The length of output sense data.
1426 @param[out] HostAdapterStatus The status of Host Adapter.
1427 @param[out] TargetStatus The status of the target.
1428 @param[in] SecurityProtocol The Security Protocol to use.
1429 @param[in] SecurityProtocolSpecific The Security Protocol Specific data.
1430 @param[in] Inc512 If TRUE, 512 increment (INC_512) bit will be set for the
1431 SECURITY PROTOCOL OUT command.
1432 @param[in] DataLength The size in bytes of the transfer data.
1433 @param[in, out] DataBuffer A pointer to a data buffer.
1434
1435 @retval EFI_SUCCESS Command is executed successfully.
1436 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed, but the entire DataBuffer could
1437 not be transferred. The actual number of bytes transferred is returned in DataLength.
1438 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because there are too many
1439 SCSI Command Packets already queued.
1440 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send SCSI Request Packet.
1441 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet is not supported by
1442 the SCSI initiator(i.e., SCSI Host Controller)
1443 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
1444 @retval EFI_INVALID_PARAMETER The contents of the SCSI Request Packet are invalid.
1445
1446 **/
1447 EFI_STATUS
1448 EFIAPI
1449 ScsiSecurityProtocolOutCommand (
1450 IN EFI_SCSI_IO_PROTOCOL *ScsiIo,
1451 IN UINT64 Timeout,
1452 IN OUT VOID *SenseData, OPTIONAL
1453 IN OUT UINT8 *SenseDataLength,
1454 OUT UINT8 *HostAdapterStatus,
1455 OUT UINT8 *TargetStatus,
1456 IN UINT8 SecurityProtocol,
1457 IN UINT16 SecurityProtocolSpecific,
1458 IN BOOLEAN Inc512,
1459 IN UINTN DataLength,
1460 IN OUT VOID *DataBuffer OPTIONAL
1461 )
1462 {
1463 EFI_SCSI_IO_SCSI_REQUEST_PACKET CommandPacket;
1464 EFI_STATUS Status;
1465 UINT8 Cdb[EFI_SCSI_OP_LENGTH_TWELVE];
1466
1467 ASSERT (SenseDataLength != NULL);
1468 ASSERT (HostAdapterStatus != NULL);
1469 ASSERT (TargetStatus != NULL);
1470 ASSERT (ScsiIo != NULL);
1471 ASSERT (DataLength <= MAX_UINT32);
1472
1473 ZeroMem (&CommandPacket, sizeof (EFI_SCSI_IO_SCSI_REQUEST_PACKET));
1474 ZeroMem (Cdb, EFI_SCSI_OP_LENGTH_TWELVE);
1475
1476 CommandPacket.Timeout = Timeout;
1477 CommandPacket.OutDataBuffer = DataBuffer;
1478 CommandPacket.SenseData = SenseData;
1479 CommandPacket.OutTransferLength = (UINT32) DataLength;
1480 CommandPacket.Cdb = Cdb;
1481 //
1482 // Fill Cdb for Security Protocol Out Command
1483 //
1484 Cdb[0] = EFI_SCSI_OP_SECURITY_PROTOCOL_OUT;
1485 Cdb[1] = SecurityProtocol;
1486 WriteUnaligned16 ((UINT16 *)&Cdb[2], SwapBytes16 (SecurityProtocolSpecific));
1487
1488 if (Inc512) {
1489 if (DataLength % 512 != 0) {
1490 return EFI_INVALID_PARAMETER;
1491 }
1492 Cdb[4] = BIT7;
1493 WriteUnaligned32 ((UINT32 *)&Cdb[6], SwapBytes32 ((UINT32) DataLength / 512));
1494 } else {
1495 WriteUnaligned32 ((UINT32 *)&Cdb[6], SwapBytes32 ((UINT32) DataLength));
1496 }
1497
1498 CommandPacket.CdbLength = EFI_SCSI_OP_LENGTH_TWELVE;
1499 CommandPacket.DataDirection = EFI_SCSI_DATA_OUT;
1500 CommandPacket.SenseDataLength = *SenseDataLength;
1501
1502 Status = ScsiIo->ExecuteScsiCommand (ScsiIo, &CommandPacket, NULL);
1503
1504 *HostAdapterStatus = CommandPacket.HostAdapterStatus;
1505 *TargetStatus = CommandPacket.TargetStatus;
1506 *SenseDataLength = CommandPacket.SenseDataLength;
1507
1508 return Status;
1509 }
1510
1511
1512 /**
1513 Internal helper notify function in which update the result of the
1514 non-blocking SCSI Read/Write commands and signal caller event.
1515
1516 @param Event The instance of EFI_EVENT.
1517 @param Context The parameter passed in.
1518
1519 **/
1520 VOID
1521 EFIAPI
1522 ScsiLibNotify (
1523 IN EFI_EVENT Event,
1524 IN VOID *Context
1525 )
1526 {
1527 EFI_SCSI_LIB_ASYNC_CONTEXT *LibContext;
1528 EFI_SCSI_IO_SCSI_REQUEST_PACKET *CommandPacket;
1529 EFI_EVENT CallerEvent;
1530
1531 LibContext = (EFI_SCSI_LIB_ASYNC_CONTEXT *) Context;
1532 CommandPacket = &LibContext->CommandPacket;
1533 CallerEvent = LibContext->CallerEvent;
1534
1535 //
1536 // Update SCSI Read/Write operation results
1537 //
1538 *LibContext->SenseDataLength = CommandPacket->SenseDataLength;
1539 *LibContext->HostAdapterStatus = CommandPacket->HostAdapterStatus;
1540 *LibContext->TargetStatus = CommandPacket->TargetStatus;
1541 if (CommandPacket->InDataBuffer != NULL) {
1542 *LibContext->DataLength = CommandPacket->InTransferLength;
1543 } else {
1544 *LibContext->DataLength = CommandPacket->OutTransferLength;
1545 }
1546
1547 if (CommandPacket->Cdb != NULL) {
1548 FreePool (CommandPacket->Cdb);
1549 }
1550 FreePool (Context);
1551
1552 gBS->CloseEvent (Event);
1553 gBS->SignalEvent (CallerEvent);
1554 }
1555
1556
1557 /**
1558 Execute blocking/non-blocking Read(10) SCSI command on a specific SCSI
1559 target.
1560
1561 Executes the SCSI Read(10) command on the SCSI target specified by ScsiIo.
1562 When Event is NULL, blocking command will be executed. Otherwise non-blocking
1563 command will be executed.
1564 For blocking I/O, if Timeout is zero, this function will wait indefinitely
1565 for the command to complete. If Timeout is greater than zero, then the
1566 command is executed and will timeout after Timeout 100 ns units.
1567 For non-blocking I/O, if Timeout is zero, Event will be signaled only after
1568 the command to completes. If Timeout is greater than zero, Event will also be
1569 signaled after Timeout 100 ns units.
1570 The StartLba and SectorSize parameters are used to construct the CDB for this
1571 SCSI command.
1572
1573 If ScsiIo is NULL, then ASSERT().
1574 If SenseDataLength is NULL, then ASSERT().
1575 If HostAdapterStatus is NULL, then ASSERT().
1576 If TargetStatus is NULL, then ASSERT().
1577 If DataLength is NULL, then ASSERT().
1578
1579 If SenseDataLength is non-zero and SenseData is not NULL, SenseData must meet
1580 buffer alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise
1581 EFI_INVALID_PARAMETER gets returned.
1582
1583 If DataLength is non-zero and DataBuffer is not NULL, DataBuffer must meet
1584 buffer alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise
1585 EFI_INVALID_PARAMETER gets returned.
1586
1587 @param[in] ScsiIo A pointer to SCSI IO protocol.
1588 @param[in] Timeout The length of timeout period.
1589 @param[in, out] SenseData A pointer to output sense data.
1590 @param[in, out] SenseDataLength The length of output sense data.
1591 @param[out] HostAdapterStatus The status of Host Adapter.
1592 @param[out] TargetStatus The status of the target.
1593 @param[in, out] DataBuffer Read 16 command data.
1594 @param[in, out] DataLength The length of data buffer.
1595 @param[in] StartLba The start address of LBA.
1596 @param[in] SectorSize The number of contiguous logical blocks
1597 of data that shall be transferred.
1598 @param[in] Event If the SCSI target does not support
1599 non-blocking I/O, then Event is ignored,
1600 and blocking I/O is performed. If Event
1601 is NULL, then blocking I/O is performed.
1602 If Event is not NULL and non-blocking
1603 I/O is supported, then non-blocking I/O
1604 is performed, and Event will be signaled
1605 when the SCSI Read(10) command
1606 completes.
1607
1608 @retval EFI_SUCCESS Command is executed successfully.
1609 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed,
1610 but the entire DataBuffer could not be
1611 transferred. The actual number of bytes
1612 transferred is returned in DataLength.
1613 @retval EFI_NOT_READY The SCSI Request Packet could not be
1614 sent because there are too many SCSI
1615 Command Packets already queued.
1616 @retval EFI_DEVICE_ERROR A device error occurred while attempting
1617 to send SCSI Request Packet.
1618 @retval EFI_UNSUPPORTED The command described by the SCSI
1619 Request Packet is not supported by the
1620 SCSI initiator(i.e., SCSI Host
1621 Controller)
1622 @retval EFI_TIMEOUT A timeout occurred while waiting for the
1623 SCSI Request Packet to execute.
1624 @retval EFI_INVALID_PARAMETER The contents of the SCSI Request Packet
1625 are invalid.
1626 @retval EFI_OUT_OF_RESOURCES The request could not be completed due
1627 to a lack of resources.
1628
1629 **/
1630 EFI_STATUS
1631 EFIAPI
1632 ScsiRead10CommandEx (
1633 IN EFI_SCSI_IO_PROTOCOL *ScsiIo,
1634 IN UINT64 Timeout,
1635 IN OUT VOID *SenseData, OPTIONAL
1636 IN OUT UINT8 *SenseDataLength,
1637 OUT UINT8 *HostAdapterStatus,
1638 OUT UINT8 *TargetStatus,
1639 IN OUT VOID *DataBuffer, OPTIONAL
1640 IN OUT UINT32 *DataLength,
1641 IN UINT32 StartLba,
1642 IN UINT32 SectorSize,
1643 IN EFI_EVENT Event OPTIONAL
1644 )
1645 {
1646 EFI_SCSI_LIB_ASYNC_CONTEXT *Context;
1647 EFI_SCSI_IO_SCSI_REQUEST_PACKET *CommandPacket;
1648 EFI_STATUS Status;
1649 UINT8 *Cdb;
1650 EFI_EVENT SelfEvent;
1651
1652 if (Event == NULL) {
1653 return ScsiRead10Command (
1654 ScsiIo,
1655 Timeout,
1656 SenseData,
1657 SenseDataLength,
1658 HostAdapterStatus,
1659 TargetStatus,
1660 DataBuffer,
1661 DataLength,
1662 StartLba,
1663 SectorSize
1664 );
1665 }
1666
1667 ASSERT (SenseDataLength != NULL);
1668 ASSERT (HostAdapterStatus != NULL);
1669 ASSERT (TargetStatus != NULL);
1670 ASSERT (DataLength != NULL);
1671 ASSERT (ScsiIo != NULL);
1672
1673 Context = AllocateZeroPool (sizeof (EFI_SCSI_LIB_ASYNC_CONTEXT));
1674 if (Context == NULL) {
1675 return EFI_OUT_OF_RESOURCES;
1676 }
1677
1678 Cdb = AllocateZeroPool (EFI_SCSI_OP_LENGTH_TEN);
1679 if (Cdb == NULL) {
1680 Status = EFI_OUT_OF_RESOURCES;
1681 goto ErrorExit;
1682 }
1683
1684 Context->SenseDataLength = SenseDataLength;
1685 Context->HostAdapterStatus = HostAdapterStatus;
1686 Context->TargetStatus = TargetStatus;
1687 Context->CallerEvent = Event;
1688
1689 CommandPacket = &Context->CommandPacket;
1690 CommandPacket->Timeout = Timeout;
1691 CommandPacket->InDataBuffer = DataBuffer;
1692 CommandPacket->SenseData = SenseData;
1693 CommandPacket->InTransferLength = *DataLength;
1694 CommandPacket->Cdb = Cdb;
1695 //
1696 // Fill Cdb for Read (10) Command
1697 //
1698 Cdb[0] = EFI_SCSI_OP_READ10;
1699 WriteUnaligned32 ((UINT32 *)&Cdb[2], SwapBytes32 (StartLba));
1700 WriteUnaligned16 ((UINT16 *)&Cdb[7], SwapBytes16 ((UINT16) SectorSize));
1701
1702 CommandPacket->CdbLength = EFI_SCSI_OP_LENGTH_TEN;
1703 CommandPacket->DataDirection = EFI_SCSI_DATA_IN;
1704 CommandPacket->SenseDataLength = *SenseDataLength;
1705
1706 //
1707 // Create Event
1708 //
1709 Status = gBS->CreateEvent (
1710 EVT_NOTIFY_SIGNAL,
1711 TPL_NOTIFY,
1712 ScsiLibNotify,
1713 Context,
1714 &SelfEvent
1715 );
1716 if (EFI_ERROR(Status)) {
1717 goto ErrorExit;
1718 }
1719
1720 Status = ScsiIo->ExecuteScsiCommand (ScsiIo, CommandPacket, SelfEvent);
1721 if (EFI_ERROR(Status)) {
1722 //
1723 // Since ScsiLibNotify() will not be signaled if ExecuteScsiCommand()
1724 // returns with error, close the event here.
1725 //
1726 gBS->CloseEvent (SelfEvent);
1727 goto ErrorExit;
1728 } else {
1729 return EFI_SUCCESS;
1730 }
1731
1732 ErrorExit:
1733 if (Context != NULL) {
1734 FreePool (Context);
1735 }
1736
1737 return Status;
1738 }
1739
1740
1741 /**
1742 Execute blocking/non-blocking Write(10) SCSI command on a specific SCSI
1743 target.
1744
1745 Executes the SCSI Write(10) command on the SCSI target specified by ScsiIo.
1746 When Event is NULL, blocking command will be executed. Otherwise non-blocking
1747 command will be executed.
1748 For blocking I/O, if Timeout is zero, this function will wait indefinitely
1749 for the command to complete. If Timeout is greater than zero, then the
1750 command is executed and will timeout after Timeout 100 ns units.
1751 For non-blocking I/O, if Timeout is zero, Event will be signaled only after
1752 the command to completes. If Timeout is greater than zero, Event will also be
1753 signaled after Timeout 100 ns units.
1754 The StartLba and SectorSize parameters are used to construct the CDB for this
1755 SCSI command.
1756
1757 If ScsiIo is NULL, then ASSERT().
1758 If SenseDataLength is NULL, then ASSERT().
1759 If HostAdapterStatus is NULL, then ASSERT().
1760 If TargetStatus is NULL, then ASSERT().
1761 If DataLength is NULL, then ASSERT().
1762
1763 If SenseDataLength is non-zero and SenseData is not NULL, SenseData must meet
1764 buffer alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise
1765 EFI_INVALID_PARAMETER gets returned.
1766
1767 If DataLength is non-zero and DataBuffer is not NULL, DataBuffer must meet
1768 buffer alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise
1769 EFI_INVALID_PARAMETER gets returned.
1770
1771 @param[in] ScsiIo SCSI IO Protocol to use
1772 @param[in] Timeout The length of timeout period.
1773 @param[in, out] SenseData A pointer to output sense data.
1774 @param[in, out] SenseDataLength The length of output sense data.
1775 @param[out] HostAdapterStatus The status of Host Adapter.
1776 @param[out] TargetStatus The status of the target.
1777 @param[in, out] DataBuffer A pointer to a data buffer.
1778 @param[in, out] DataLength The length of data buffer.
1779 @param[in] StartLba The start address of LBA.
1780 @param[in] SectorSize The number of contiguous logical blocks
1781 of data that shall be transferred.
1782 @param[in] Event If the SCSI target does not support
1783 non-blocking I/O, then Event is ignored,
1784 and blocking I/O is performed. If Event
1785 is NULL, then blocking I/O is performed.
1786 If Event is not NULL and non-blocking
1787 I/O is supported, then non-blocking I/O
1788 is performed, and Event will be signaled
1789 when the SCSI Write(10) command
1790 completes.
1791
1792 @retval EFI_SUCCESS Command is executed successfully.
1793 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed,
1794 but the entire DataBuffer could not be
1795 transferred. The actual number of bytes
1796 transferred is returned in DataLength.
1797 @retval EFI_NOT_READY The SCSI Request Packet could not be
1798 sent because there are too many SCSI
1799 Command Packets already queued.
1800 @retval EFI_DEVICE_ERROR A device error occurred while attempting
1801 to send SCSI Request Packet.
1802 @retval EFI_UNSUPPORTED The command described by the SCSI
1803 Request Packet is not supported by the
1804 SCSI initiator(i.e., SCSI Host
1805 Controller)
1806 @retval EFI_TIMEOUT A timeout occurred while waiting for the
1807 SCSI Request Packet to execute.
1808 @retval EFI_INVALID_PARAMETER The contents of the SCSI Request Packet
1809 are invalid.
1810 @retval EFI_OUT_OF_RESOURCES The request could not be completed due
1811 to a lack of resources.
1812
1813 **/
1814 EFI_STATUS
1815 EFIAPI
1816 ScsiWrite10CommandEx (
1817 IN EFI_SCSI_IO_PROTOCOL *ScsiIo,
1818 IN UINT64 Timeout,
1819 IN OUT VOID *SenseData, OPTIONAL
1820 IN OUT UINT8 *SenseDataLength,
1821 OUT UINT8 *HostAdapterStatus,
1822 OUT UINT8 *TargetStatus,
1823 IN OUT VOID *DataBuffer, OPTIONAL
1824 IN OUT UINT32 *DataLength,
1825 IN UINT32 StartLba,
1826 IN UINT32 SectorSize,
1827 IN EFI_EVENT Event OPTIONAL
1828 )
1829 {
1830 EFI_SCSI_LIB_ASYNC_CONTEXT *Context;
1831 EFI_SCSI_IO_SCSI_REQUEST_PACKET *CommandPacket;
1832 EFI_STATUS Status;
1833 UINT8 *Cdb;
1834 EFI_EVENT SelfEvent;
1835
1836 if (Event == NULL) {
1837 return ScsiWrite10Command (
1838 ScsiIo,
1839 Timeout,
1840 SenseData,
1841 SenseDataLength,
1842 HostAdapterStatus,
1843 TargetStatus,
1844 DataBuffer,
1845 DataLength,
1846 StartLba,
1847 SectorSize
1848 );
1849 }
1850
1851 ASSERT (SenseDataLength != NULL);
1852 ASSERT (HostAdapterStatus != NULL);
1853 ASSERT (TargetStatus != NULL);
1854 ASSERT (DataLength != NULL);
1855 ASSERT (ScsiIo != NULL);
1856
1857 Context = AllocateZeroPool (sizeof (EFI_SCSI_LIB_ASYNC_CONTEXT));
1858 if (Context == NULL) {
1859 return EFI_OUT_OF_RESOURCES;
1860 }
1861
1862 Cdb = AllocateZeroPool (EFI_SCSI_OP_LENGTH_TEN);
1863 if (Cdb == NULL) {
1864 Status = EFI_OUT_OF_RESOURCES;
1865 goto ErrorExit;
1866 }
1867
1868 Context->SenseDataLength = SenseDataLength;
1869 Context->HostAdapterStatus = HostAdapterStatus;
1870 Context->TargetStatus = TargetStatus;
1871 Context->CallerEvent = Event;
1872
1873 CommandPacket = &Context->CommandPacket;
1874 CommandPacket->Timeout = Timeout;
1875 CommandPacket->OutDataBuffer = DataBuffer;
1876 CommandPacket->SenseData = SenseData;
1877 CommandPacket->OutTransferLength = *DataLength;
1878 CommandPacket->Cdb = Cdb;
1879 //
1880 // Fill Cdb for Write (10) Command
1881 //
1882 Cdb[0] = EFI_SCSI_OP_WRITE10;
1883 WriteUnaligned32 ((UINT32 *)&Cdb[2], SwapBytes32 (StartLba));
1884 WriteUnaligned16 ((UINT16 *)&Cdb[7], SwapBytes16 ((UINT16) SectorSize));
1885
1886 CommandPacket->CdbLength = EFI_SCSI_OP_LENGTH_TEN;
1887 CommandPacket->DataDirection = EFI_SCSI_DATA_OUT;
1888 CommandPacket->SenseDataLength = *SenseDataLength;
1889
1890 //
1891 // Create Event
1892 //
1893 Status = gBS->CreateEvent (
1894 EVT_NOTIFY_SIGNAL,
1895 TPL_NOTIFY,
1896 ScsiLibNotify,
1897 Context,
1898 &SelfEvent
1899 );
1900 if (EFI_ERROR(Status)) {
1901 goto ErrorExit;
1902 }
1903
1904 Status = ScsiIo->ExecuteScsiCommand (ScsiIo, CommandPacket, SelfEvent);
1905 if (EFI_ERROR(Status)) {
1906 //
1907 // Since ScsiLibNotify() will not be signaled if ExecuteScsiCommand()
1908 // returns with error, close the event here.
1909 //
1910 gBS->CloseEvent (SelfEvent);
1911 goto ErrorExit;
1912 } else {
1913 return EFI_SUCCESS;
1914 }
1915
1916 ErrorExit:
1917 if (Context != NULL) {
1918 FreePool (Context);
1919 }
1920
1921 return Status;
1922 }
1923
1924
1925 /**
1926 Execute blocking/non-blocking Read(16) SCSI command on a specific SCSI
1927 target.
1928
1929 Executes the SCSI Read(16) command on the SCSI target specified by ScsiIo.
1930 When Event is NULL, blocking command will be executed. Otherwise non-blocking
1931 command will be executed.
1932 For blocking I/O, if Timeout is zero, this function will wait indefinitely
1933 for the command to complete. If Timeout is greater than zero, then the
1934 command is executed and will timeout after Timeout 100 ns units.
1935 For non-blocking I/O, if Timeout is zero, Event will be signaled only after
1936 the command to completes. If Timeout is greater than zero, Event will also be
1937 signaled after Timeout 100 ns units.
1938 The StartLba and SectorSize parameters are used to construct the CDB for this
1939 SCSI command.
1940
1941 If ScsiIo is NULL, then ASSERT().
1942 If SenseDataLength is NULL, then ASSERT().
1943 If HostAdapterStatus is NULL, then ASSERT().
1944 If TargetStatus is NULL, then ASSERT().
1945 If DataLength is NULL, then ASSERT().
1946
1947 If SenseDataLength is non-zero and SenseData is not NULL, SenseData must meet
1948 buffer alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise
1949 EFI_INVALID_PARAMETER gets returned.
1950
1951 If DataLength is non-zero and DataBuffer is not NULL, DataBuffer must meet
1952 buffer alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise
1953 EFI_INVALID_PARAMETER gets returned.
1954
1955 @param[in] ScsiIo A pointer to SCSI IO protocol.
1956 @param[in] Timeout The length of timeout period.
1957 @param[in, out] SenseData A pointer to output sense data.
1958 @param[in, out] SenseDataLength The length of output sense data.
1959 @param[out] HostAdapterStatus The status of Host Adapter.
1960 @param[out] TargetStatus The status of the target.
1961 @param[in, out] DataBuffer Read 16 command data.
1962 @param[in, out] DataLength The length of data buffer.
1963 @param[in] StartLba The start address of LBA.
1964 @param[in] SectorSize The number of contiguous logical blocks
1965 of data that shall be transferred.
1966 @param[in] Event If the SCSI target does not support
1967 non-blocking I/O, then Event is ignored,
1968 and blocking I/O is performed. If Event
1969 is NULL, then blocking I/O is performed.
1970 If Event is not NULL and non-blocking
1971 I/O is supported, then non-blocking I/O
1972 is performed, and Event will be signaled
1973 when the SCSI Read(16) command
1974 completes.
1975
1976 @retval EFI_SUCCESS Command is executed successfully.
1977 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed,
1978 but the entire DataBuffer could not be
1979 transferred. The actual number of bytes
1980 transferred is returned in DataLength.
1981 @retval EFI_NOT_READY The SCSI Request Packet could not be
1982 sent because there are too many SCSI
1983 Command Packets already queued.
1984 @retval EFI_DEVICE_ERROR A device error occurred while attempting
1985 to send SCSI Request Packet.
1986 @retval EFI_UNSUPPORTED The command described by the SCSI
1987 Request Packet is not supported by the
1988 SCSI initiator(i.e., SCSI Host
1989 Controller)
1990 @retval EFI_TIMEOUT A timeout occurred while waiting for the
1991 SCSI Request Packet to execute.
1992 @retval EFI_INVALID_PARAMETER The contents of the SCSI Request Packet
1993 are invalid.
1994 @retval EFI_OUT_OF_RESOURCES The request could not be completed due
1995 to a lack of resources.
1996
1997 **/
1998 EFI_STATUS
1999 EFIAPI
2000 ScsiRead16CommandEx (
2001 IN EFI_SCSI_IO_PROTOCOL *ScsiIo,
2002 IN UINT64 Timeout,
2003 IN OUT VOID *SenseData, OPTIONAL
2004 IN OUT UINT8 *SenseDataLength,
2005 OUT UINT8 *HostAdapterStatus,
2006 OUT UINT8 *TargetStatus,
2007 IN OUT VOID *DataBuffer, OPTIONAL
2008 IN OUT UINT32 *DataLength,
2009 IN UINT64 StartLba,
2010 IN UINT32 SectorSize,
2011 IN EFI_EVENT Event OPTIONAL
2012 )
2013 {
2014 EFI_SCSI_LIB_ASYNC_CONTEXT *Context;
2015 EFI_SCSI_IO_SCSI_REQUEST_PACKET *CommandPacket;
2016 EFI_STATUS Status;
2017 UINT8 *Cdb;
2018 EFI_EVENT SelfEvent;
2019
2020 if (Event == NULL) {
2021 return ScsiRead16Command (
2022 ScsiIo,
2023 Timeout,
2024 SenseData,
2025 SenseDataLength,
2026 HostAdapterStatus,
2027 TargetStatus,
2028 DataBuffer,
2029 DataLength,
2030 StartLba,
2031 SectorSize
2032 );
2033 }
2034
2035 ASSERT (SenseDataLength != NULL);
2036 ASSERT (HostAdapterStatus != NULL);
2037 ASSERT (TargetStatus != NULL);
2038 ASSERT (DataLength != NULL);
2039 ASSERT (ScsiIo != NULL);
2040
2041 Context = AllocateZeroPool (sizeof (EFI_SCSI_LIB_ASYNC_CONTEXT));
2042 if (Context == NULL) {
2043 return EFI_OUT_OF_RESOURCES;
2044 }
2045
2046 Cdb = AllocateZeroPool (EFI_SCSI_OP_LENGTH_SIXTEEN);
2047 if (Cdb == NULL) {
2048 Status = EFI_OUT_OF_RESOURCES;
2049 goto ErrorExit;
2050 }
2051
2052 Context->SenseDataLength = SenseDataLength;
2053 Context->HostAdapterStatus = HostAdapterStatus;
2054 Context->TargetStatus = TargetStatus;
2055 Context->CallerEvent = Event;
2056
2057 CommandPacket = &Context->CommandPacket;
2058 CommandPacket->Timeout = Timeout;
2059 CommandPacket->InDataBuffer = DataBuffer;
2060 CommandPacket->SenseData = SenseData;
2061 CommandPacket->InTransferLength = *DataLength;
2062 CommandPacket->Cdb = Cdb;
2063 //
2064 // Fill Cdb for Read (16) Command
2065 //
2066 Cdb[0] = EFI_SCSI_OP_READ16;
2067 WriteUnaligned64 ((UINT64 *)&Cdb[2], SwapBytes64 (StartLba));
2068 WriteUnaligned32 ((UINT32 *)&Cdb[10], SwapBytes32 (SectorSize));
2069
2070 CommandPacket->CdbLength = EFI_SCSI_OP_LENGTH_SIXTEEN;
2071 CommandPacket->DataDirection = EFI_SCSI_DATA_IN;
2072 CommandPacket->SenseDataLength = *SenseDataLength;
2073
2074 //
2075 // Create Event
2076 //
2077 Status = gBS->CreateEvent (
2078 EVT_NOTIFY_SIGNAL,
2079 TPL_NOTIFY,
2080 ScsiLibNotify,
2081 Context,
2082 &SelfEvent
2083 );
2084 if (EFI_ERROR(Status)) {
2085 goto ErrorExit;
2086 }
2087
2088 Status = ScsiIo->ExecuteScsiCommand (ScsiIo, CommandPacket, SelfEvent);
2089 if (EFI_ERROR(Status)) {
2090 //
2091 // Since ScsiLibNotify() will not be signaled if ExecuteScsiCommand()
2092 // returns with error, close the event here.
2093 //
2094 gBS->CloseEvent (SelfEvent);
2095 goto ErrorExit;
2096 } else {
2097 return EFI_SUCCESS;
2098 }
2099
2100 ErrorExit:
2101 if (Context != NULL) {
2102 FreePool (Context);
2103 }
2104
2105 return Status;
2106 }
2107
2108
2109 /**
2110 Execute blocking/non-blocking Write(16) SCSI command on a specific SCSI
2111 target.
2112
2113 Executes the SCSI Write(16) command on the SCSI target specified by ScsiIo.
2114 When Event is NULL, blocking command will be executed. Otherwise non-blocking
2115 command will be executed.
2116 For blocking I/O, if Timeout is zero, this function will wait indefinitely
2117 for the command to complete. If Timeout is greater than zero, then the
2118 command is executed and will timeout after Timeout 100 ns units.
2119 For non-blocking I/O, if Timeout is zero, Event will be signaled only after
2120 the command to completes. If Timeout is greater than zero, Event will also be
2121 signaled after Timeout 100 ns units.
2122 The StartLba and SectorSize parameters are used to construct the CDB for this
2123 SCSI command.
2124
2125 If ScsiIo is NULL, then ASSERT().
2126 If SenseDataLength is NULL, then ASSERT().
2127 If HostAdapterStatus is NULL, then ASSERT().
2128 If TargetStatus is NULL, then ASSERT().
2129 If DataLength is NULL, then ASSERT().
2130
2131 If SenseDataLength is non-zero and SenseData is not NULL, SenseData must meet
2132 buffer alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise
2133 EFI_INVALID_PARAMETER gets returned.
2134
2135 If DataLength is non-zero and DataBuffer is not NULL, DataBuffer must meet
2136 buffer alignment requirement defined in EFI_SCSI_IO_PROTOCOL. Otherwise
2137 EFI_INVALID_PARAMETER gets returned.
2138
2139 @param[in] ScsiIo SCSI IO Protocol to use
2140 @param[in] Timeout The length of timeout period.
2141 @param[in, out] SenseData A pointer to output sense data.
2142 @param[in, out] SenseDataLength The length of output sense data.
2143 @param[out] HostAdapterStatus The status of Host Adapter.
2144 @param[out] TargetStatus The status of the target.
2145 @param[in, out] DataBuffer A pointer to a data buffer.
2146 @param[in, out] DataLength The length of data buffer.
2147 @param[in] StartLba The start address of LBA.
2148 @param[in] SectorSize The number of contiguous logical blocks
2149 of data that shall be transferred.
2150 @param[in] Event If the SCSI target does not support
2151 non-blocking I/O, then Event is ignored,
2152 and blocking I/O is performed. If Event
2153 is NULL, then blocking I/O is performed.
2154 If Event is not NULL and non-blocking
2155 I/O is supported, then non-blocking I/O
2156 is performed, and Event will be signaled
2157 when the SCSI Write(16) command
2158 completes.
2159
2160 @retval EFI_SUCCESS Command is executed successfully.
2161 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed,
2162 but the entire DataBuffer could not be
2163 transferred. The actual number of bytes
2164 transferred is returned in DataLength.
2165 @retval EFI_NOT_READY The SCSI Request Packet could not be
2166 sent because there are too many SCSI
2167 Command Packets already queued.
2168 @retval EFI_DEVICE_ERROR A device error occurred while attempting
2169 to send SCSI Request Packet.
2170 @retval EFI_UNSUPPORTED The command described by the SCSI
2171 Request Packet is not supported by the
2172 SCSI initiator(i.e., SCSI Host
2173 Controller)
2174 @retval EFI_TIMEOUT A timeout occurred while waiting for the
2175 SCSI Request Packet to execute.
2176 @retval EFI_INVALID_PARAMETER The contents of the SCSI Request Packet
2177 are invalid.
2178 @retval EFI_OUT_OF_RESOURCES The request could not be completed due
2179 to a lack of resources.
2180
2181 **/
2182 EFI_STATUS
2183 EFIAPI
2184 ScsiWrite16CommandEx (
2185 IN EFI_SCSI_IO_PROTOCOL *ScsiIo,
2186 IN UINT64 Timeout,
2187 IN OUT VOID *SenseData, OPTIONAL
2188 IN OUT UINT8 *SenseDataLength,
2189 OUT UINT8 *HostAdapterStatus,
2190 OUT UINT8 *TargetStatus,
2191 IN OUT VOID *DataBuffer, OPTIONAL
2192 IN OUT UINT32 *DataLength,
2193 IN UINT64 StartLba,
2194 IN UINT32 SectorSize,
2195 IN EFI_EVENT Event OPTIONAL
2196 )
2197 {
2198 EFI_SCSI_LIB_ASYNC_CONTEXT *Context;
2199 EFI_SCSI_IO_SCSI_REQUEST_PACKET *CommandPacket;
2200 EFI_STATUS Status;
2201 UINT8 *Cdb;
2202 EFI_EVENT SelfEvent;
2203
2204 if (Event == NULL) {
2205 return ScsiWrite16Command (
2206 ScsiIo,
2207 Timeout,
2208 SenseData,
2209 SenseDataLength,
2210 HostAdapterStatus,
2211 TargetStatus,
2212 DataBuffer,
2213 DataLength,
2214 StartLba,
2215 SectorSize
2216 );
2217 }
2218
2219 ASSERT (SenseDataLength != NULL);
2220 ASSERT (HostAdapterStatus != NULL);
2221 ASSERT (TargetStatus != NULL);
2222 ASSERT (DataLength != NULL);
2223 ASSERT (ScsiIo != NULL);
2224
2225 Context = AllocateZeroPool (sizeof (EFI_SCSI_LIB_ASYNC_CONTEXT));
2226 if (Context == NULL) {
2227 return EFI_OUT_OF_RESOURCES;
2228 }
2229
2230 Cdb = AllocateZeroPool (EFI_SCSI_OP_LENGTH_SIXTEEN);
2231 if (Cdb == NULL) {
2232 Status = EFI_OUT_OF_RESOURCES;
2233 goto ErrorExit;
2234 }
2235
2236 Context->SenseDataLength = SenseDataLength;
2237 Context->HostAdapterStatus = HostAdapterStatus;
2238 Context->TargetStatus = TargetStatus;
2239 Context->CallerEvent = Event;
2240
2241 CommandPacket = &Context->CommandPacket;
2242 CommandPacket->Timeout = Timeout;
2243 CommandPacket->OutDataBuffer = DataBuffer;
2244 CommandPacket->SenseData = SenseData;
2245 CommandPacket->OutTransferLength = *DataLength;
2246 CommandPacket->Cdb = Cdb;
2247 //
2248 // Fill Cdb for Write (16) Command
2249 //
2250 Cdb[0] = EFI_SCSI_OP_WRITE16;
2251 WriteUnaligned64 ((UINT64 *)&Cdb[2], SwapBytes64 (StartLba));
2252 WriteUnaligned32 ((UINT32 *)&Cdb[10], SwapBytes32 (SectorSize));
2253
2254 CommandPacket->CdbLength = EFI_SCSI_OP_LENGTH_SIXTEEN;
2255 CommandPacket->DataDirection = EFI_SCSI_DATA_OUT;
2256 CommandPacket->SenseDataLength = *SenseDataLength;
2257
2258 //
2259 // Create Event
2260 //
2261 Status = gBS->CreateEvent (
2262 EVT_NOTIFY_SIGNAL,
2263 TPL_NOTIFY,
2264 ScsiLibNotify,
2265 Context,
2266 &SelfEvent
2267 );
2268 if (EFI_ERROR(Status)) {
2269 goto ErrorExit;
2270 }
2271
2272 Status = ScsiIo->ExecuteScsiCommand (ScsiIo, CommandPacket, SelfEvent);
2273 if (EFI_ERROR(Status)) {
2274 //
2275 // Since ScsiLibNotify() will not be signaled if ExecuteScsiCommand()
2276 // returns with error, close the event here.
2277 //
2278 gBS->CloseEvent (SelfEvent);
2279 goto ErrorExit;
2280 } else {
2281 return EFI_SUCCESS;
2282 }
2283
2284 ErrorExit:
2285 if (Context != NULL) {
2286 FreePool (Context);
2287 }
2288
2289 return Status;
2290 }