2 The implementation of iSCSI protocol based on RFC3720.
4 Copyright (c) 2004 - 2008, Intel Corporation.<BR>
5 All rights reserved. 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
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.
15 #include "IScsiImpl.h"
17 UINT32 mDataSegPad
= 0;
20 Attach the iSCSI connection to the iSCSI session.
22 @param[in, out] Session The iSCSI session.
23 @param[in, out] Conn The iSCSI connection.
26 IScsiAttatchConnection (
27 IN OUT ISCSI_SESSION
*Session
,
28 IN OUT ISCSI_CONNECTION
*Conn
31 InsertTailList (&Session
->Conns
, &Conn
->Link
);
32 Conn
->Session
= Session
;
37 Detach the iSCSI connection from the session it belongs to.
39 @param[in, out] Conn The iSCSI connection.
42 IScsiDetatchConnection (
43 IN OUT ISCSI_CONNECTION
*Conn
46 RemoveEntryList (&Conn
->Link
);
47 Conn
->Session
->NumConns
--;
52 Check the sequence number according to RFC3720.
54 @param[in, out] ExpSN The currently expected sequence number.
55 @param[in] NewSN The sequence number to check.
57 @retval EFI_SUCCESS The check passed and the ExpSN is increased.
58 @retval EFI_NOT_READY Response was sent due to a retransmission request.
59 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
67 if (!ISCSI_SEQ_EQ (NewSN
, *ExpSN
)) {
68 if (ISCSI_SEQ_LT (NewSN
, *ExpSN
)) {
74 return EFI_PROTOCOL_ERROR
;
86 Update the sequence numbers for the iSCSI command.
88 @param[in, out] Session The iSCSI session.
89 @param[in] MaxCmdSN Maximum CmdSN from the target.
90 @param[in] ExpCmdSN Next expected CmdSN from the target.
94 IN OUT ISCSI_SESSION
*Session
,
99 if (ISCSI_SEQ_LT (MaxCmdSN
, ExpCmdSN
- 1)) {
103 if (ISCSI_SEQ_GT (MaxCmdSN
, Session
->MaxCmdSN
)) {
104 Session
->MaxCmdSN
= MaxCmdSN
;
107 if (ISCSI_SEQ_GT (ExpCmdSN
, Session
->ExpCmdSN
)) {
108 Session
->ExpCmdSN
= ExpCmdSN
;
113 This function does the iSCSI connection login.
115 @param[in, out] Conn The iSCSI connection to login.
117 @retval EFI_SUCCESS The iSCSI connection is logged into the iSCSI target.
118 @retval EFI_TIMEOUT Timeout happened during the login procedure.
119 @retval Others Other errors as indicated.
123 IN OUT ISCSI_CONNECTION
*Conn
129 // Start the timer, wait 16 seconds to establish the TCP connection.
131 Status
= gBS
->SetTimer (Conn
->TimeoutEvent
, TimerRelative
, 16 * TICKS_PER_SECOND
);
132 if (EFI_ERROR (Status
)) {
136 // try to establish the tcp connection
138 Status
= Tcp4IoConnect (&Conn
->Tcp4Io
, Conn
->TimeoutEvent
);
139 if (EFI_ERROR (Status
)) {
143 gBS
->SetTimer (Conn
->TimeoutEvent
, TimerCancel
, 0);
144 Conn
->State
= CONN_STATE_IN_LOGIN
;
147 // connection is established, start the iSCSI Login
150 Status
= IScsiSendLoginReq (Conn
);
151 if (EFI_ERROR (Status
)) {
155 Status
= IScsiReceiveLoginRsp (Conn
);
156 if (EFI_ERROR (Status
)) {
159 } while (Conn
->CurrentStage
!= ISCSI_FULL_FEATURE_PHASE
);
165 Reset the iSCSI connection.
167 @param[in, out] Conn The iSCSI connection to reset.
171 IN OUT ISCSI_CONNECTION
*Conn
174 Tcp4IoReset (&Conn
->Tcp4Io
);
178 Create a TCP connection for the iSCSI session.
180 @param[in] Private The iSCSI driver data.
181 @param[in] Session Maximum CmdSN from the target.
183 @return The newly created iSCSI connection.
186 IScsiCreateConnection (
187 IN ISCSI_DRIVER_DATA
*Private
,
188 IN ISCSI_SESSION
*Session
191 ISCSI_CONNECTION
*Conn
;
192 TCP4_IO_CONFIG_DATA Tcp4IoConfig
;
195 Conn
= AllocatePool (sizeof (ISCSI_CONNECTION
));
200 Conn
->Signature
= ISCSI_CONNECTION_SIGNATURE
;
201 Conn
->State
= CONN_STATE_FREE
;
202 Conn
->CurrentStage
= ISCSI_SECURITY_NEGOTIATION
;
203 Conn
->NextStage
= ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
;
204 Conn
->CHAPStep
= ISCSI_CHAP_INITIAL
;
206 Conn
->PartialReqSent
= FALSE
;
207 Conn
->PartialRspRcvd
= FALSE
;
208 Conn
->Cid
= Session
->NextCid
++;
210 Status
= gBS
->CreateEvent (
217 if (EFI_ERROR (Status
)) {
218 gBS
->FreePool (Conn
);
222 NetbufQueInit (&Conn
->RspQue
);
225 // set the default connection-only parameters
227 Conn
->MaxRecvDataSegmentLength
= DEFAULT_MAX_RECV_DATA_SEG_LEN
;
228 Conn
->HeaderDigest
= ISCSI_DIGEST_NONE
;
229 Conn
->DataDigest
= ISCSI_DIGEST_NONE
;
231 CopyMem (&Tcp4IoConfig
.LocalIp
, &Session
->ConfigData
.NvData
.LocalIp
, sizeof (EFI_IPv4_ADDRESS
));
232 CopyMem (&Tcp4IoConfig
.SubnetMask
, &Session
->ConfigData
.NvData
.SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
233 CopyMem (&Tcp4IoConfig
.Gateway
, &Session
->ConfigData
.NvData
.Gateway
, sizeof (EFI_IPv4_ADDRESS
));
234 CopyMem (&Tcp4IoConfig
.RemoteIp
, &Session
->ConfigData
.NvData
.TargetIp
, sizeof (EFI_IPv4_ADDRESS
));
236 Tcp4IoConfig
.RemotePort
= Session
->ConfigData
.NvData
.TargetPort
;
239 // Create the tcp4 IO for this connection
241 Status
= Tcp4IoCreateSocket (
247 if (EFI_ERROR (Status
)) {
248 gBS
->CloseEvent (Conn
->TimeoutEvent
);
249 gBS
->FreePool (Conn
);
257 Destroy an iSCSI connection.
259 @param[in] Conn The connection to destroy.
262 IScsiDestroyConnection (
263 IN ISCSI_CONNECTION
*Conn
266 Tcp4IoDestroySocket (&Conn
->Tcp4Io
);
267 NetbufQueFlush (&Conn
->RspQue
);
268 gBS
->CloseEvent (Conn
->TimeoutEvent
);
269 gBS
->FreePool (Conn
);
273 Login the iSCSI session.
275 @param[in] Private The iSCSI driver data.
277 @retval EFI_SUCCESS The iSCSI session login procedure finished.
278 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
279 @retval Others Other errors as indicated.
283 IN ISCSI_DRIVER_DATA
*Private
287 ISCSI_SESSION
*Session
;
288 ISCSI_CONNECTION
*Conn
;
289 EFI_TCP4_PROTOCOL
*Tcp4
;
291 Session
= &Private
->Session
;
294 // Create a connection for the session.
296 Conn
= IScsiCreateConnection (Private
, Session
);
298 return EFI_OUT_OF_RESOURCES
;
301 IScsiAttatchConnection (Session
, Conn
);
304 // Login througth the newly created connection.
306 Status
= IScsiConnLogin (Conn
);
307 if (EFI_ERROR (Status
)) {
308 IScsiConnReset (Conn
);
309 IScsiDetatchConnection (Conn
);
310 IScsiDestroyConnection (Conn
);
312 Session
->State
= SESSION_STATE_LOGGED_IN
;
316 &gEfiTcp4ProtocolGuid
,
319 Private
->ExtScsiPassThruHandle
,
320 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
328 Build and send the iSCSI login request to the iSCSI target according to
329 the current login stage.
331 @param[in] Conn The connection in the iSCSI login phase.
333 @retval EFI_SUCCESS The iSCSI login request PDU is built and sent on this
335 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
336 @retval EFI_DEVICE_ERROR Some kind of device error happened.
340 IN ISCSI_CONNECTION
*Conn
347 // build the Login Request PDU
349 Pdu
= IScsiPrepareLoginReq (Conn
);
351 return EFI_DEVICE_ERROR
;
354 // Send it to the iSCSI target.
356 Status
= Tcp4IoTransmit (&Conn
->Tcp4Io
, Pdu
);
364 Receive and process the iSCSI login response.
366 @param[in] Conn The connection in the iSCSI login phase.
368 @retval EFI_SUCCESS The iSCSI login response PDU is received and processed.
369 @retval Others Other errors as indicated.
372 IScsiReceiveLoginRsp (
373 IN ISCSI_CONNECTION
*Conn
380 // Receive the iSCSI login response.
382 Status
= IScsiReceivePdu (Conn
, &Pdu
, NULL
, FALSE
, FALSE
, NULL
);
383 if (EFI_ERROR (Status
)) {
387 // A Login Response is received, process it.
389 Status
= IScsiProcessLoginRsp (Conn
, Pdu
);
397 Add an iSCSI key-value pair as a string into the data segment of the Login Request PDU.
398 The DataSegmentLength and the actual size of the net buffer containing this PDU will be
401 @param[in, out] Pdu The iSCSI PDU whose data segment the key-value pair will
403 @param[in] Key The key name string.
404 @param[in] Value The value string.
406 @retval EFI_SUCCESS The key-valu pair is added to the PDU's datasegment and
407 the correspondence length fields are updated.
408 @retval EFI_OUT_OF_RESOURCES There is not enough space in the PDU to add the key-value
412 IScsiAddKeyValuePair (
422 ISCSI_LOGIN_REQUEST
*LoginReq
;
425 LoginReq
= (ISCSI_LOGIN_REQUEST
*) NetbufGetByte (Pdu
, 0, NULL
);
426 DataSegLen
= NTOH24 (LoginReq
->DataSegmentLength
);
428 KeyLen
= (UINT32
) AsciiStrLen (Key
);
429 ValueLen
= (UINT32
) AsciiStrLen (Value
);
432 // 1 byte for the key value separator '=' and 1 byte for the null
433 // delimiter after the value.
435 TotalLen
= KeyLen
+ 1 + ValueLen
+ 1;
438 // Allocate the space for the key-value pair.
440 Data
= (CHAR8
*)NetbufAllocSpace (Pdu
, TotalLen
, NET_BUF_TAIL
);
442 return EFI_OUT_OF_RESOURCES
;
447 CopyMem (Data
, Key
, KeyLen
);
456 CopyMem (Data
, Value
, ValueLen
);
462 // update the DataSegmentLength
464 ISCSI_SET_DATASEG_LEN (LoginReq
, DataSegLen
+ TotalLen
);
470 Prepare the iSCSI login request to be sent according to the current login status.
472 @param[in, out] Conn The connection in the iSCSI login phase.
474 @return The pointer to the net buffer containing the iSCSI login request built.
475 @retval Others Other errors as indicated.
478 IScsiPrepareLoginReq (
479 IN OUT ISCSI_CONNECTION
*Conn
482 ISCSI_SESSION
*Session
;
484 ISCSI_LOGIN_REQUEST
*LoginReq
;
487 Session
= Conn
->Session
;
489 Nbuf
= NetbufAlloc (sizeof (ISCSI_LOGIN_REQUEST
) + DEFAULT_MAX_RECV_DATA_SEG_LEN
);
494 LoginReq
= (ISCSI_LOGIN_REQUEST
*) NetbufAllocSpace (Nbuf
, sizeof (ISCSI_LOGIN_REQUEST
), NET_BUF_TAIL
);
495 ZeroMem (LoginReq
, sizeof (ISCSI_LOGIN_REQUEST
));
498 // Init the login request pdu
500 ISCSI_SET_OPCODE (LoginReq
, ISCSI_OPCODE_LOGIN_REQ
, ISCSI_REQ_IMMEDIATE
);
501 ISCSI_SET_STAGES (LoginReq
, Conn
->CurrentStage
, Conn
->NextStage
);
502 LoginReq
->VersionMax
= ISCSI_VERSION_MAX
;
503 LoginReq
->VersionMin
= ISCSI_VERSION_MIN
;
504 LoginReq
->Tsih
= HTONS (Session
->Tsih
);
505 LoginReq
->InitiatorTaskTag
= HTONL (Session
->InitiatorTaskTag
);
506 LoginReq
->Cid
= HTONS (Conn
->Cid
);
507 LoginReq
->CmdSN
= HTONL (Session
->CmdSN
);
510 // For the first Login Request on a coonection this is ExpStatSN for the
511 // old connection and this field is only valid if the Login Request restarts
513 // For subsequent Login Requests it is used to acknowledge the Login Responses
514 // with their increasing StatSN values.
516 LoginReq
->ExpStatSN
= HTONL (Conn
->ExpStatSN
);
517 CopyMem (LoginReq
->Isid
, Session
->Isid
, sizeof (LoginReq
->Isid
));
519 if (Conn
->PartialRspRcvd
) {
521 // A partial response, initiator must send an empty Login Request.
526 switch (Conn
->CurrentStage
) {
527 case ISCSI_SECURITY_NEGOTIATION
:
528 Status
= IScsiCHAPToSendReq (Conn
, Nbuf
);
531 case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
:
532 Status
= IScsiFillOpParams (Conn
, Nbuf
);
533 ISCSI_SET_FLAG (LoginReq
, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT
);
538 // something error happens...
540 Status
= EFI_DEVICE_ERROR
;
544 if (EFI_ERROR (Status
)) {
549 // Pad the data segment if needed.
551 IScsiPadSegment (Nbuf
, ISCSI_GET_DATASEG_LEN (LoginReq
));
553 // Check whether we will issue the stage transition signal?
555 Conn
->TransitInitiated
= (BOOLEAN
) ISCSI_FLAG_ON (LoginReq
, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT
);
562 Process the iSCSI Login Response.
564 @param[in, out] Conn The connection on which the iSCSI login response is received.
565 @param[in, out] Pdu The iSCSI login response PDU.
567 @retval EFI_SUCCESS The iSCSI login response PDU is processed and all check are passed.
568 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
569 @retval EFI_MEDIA_CHANGED Target is redirected.
570 @retval Others Other errors as indicated.
573 IScsiProcessLoginRsp (
574 IN OUT ISCSI_CONNECTION
*Conn
,
579 ISCSI_SESSION
*Session
;
580 ISCSI_LOGIN_RESPONSE
*LoginRsp
;
588 Session
= Conn
->Session
;
590 LoginRsp
= (ISCSI_LOGIN_RESPONSE
*) NetbufGetByte (Pdu
, 0, NULL
);
591 if (!ISCSI_CHECK_OPCODE (LoginRsp
, ISCSI_OPCODE_LOGIN_RSP
)) {
593 // It's not a Login Response
595 return EFI_PROTOCOL_ERROR
;
598 // Get the data segment if any.
600 DataSegLen
= ISCSI_GET_DATASEG_LEN (LoginRsp
);
601 if (DataSegLen
!= 0) {
602 DataSeg
= NetbufGetByte (Pdu
, sizeof (ISCSI_LOGIN_RESPONSE
), NULL
);
607 // Check the status class in the login response PDU.
609 switch (LoginRsp
->StatusClass
) {
610 case ISCSI_LOGIN_STATUS_SUCCESS
:
612 // Just break here, the response and the data segment will be processed later.
616 case ISCSI_LOGIN_STATUS_REDIRECTION
:
618 // The target may be moved to a different address
620 if (DataSeg
== NULL
) {
621 return EFI_PROTOCOL_ERROR
;
624 // Process the TargetAddress key-value strings in the data segment to update the
625 // target address info.
627 Status
= IScsiUpdateTargetAddress (Session
, (CHAR8
*)DataSeg
, DataSegLen
);
628 if (EFI_ERROR (Status
)) {
632 // Session will be restarted on this error status because the Target is
633 // redirected by this Login Response.
635 return EFI_MEDIA_CHANGED
;
639 // Initiator Error, Target Error, or any other undefined error code.
641 return EFI_PROTOCOL_ERROR
;
644 // The status is sucess, extract the wanted fields from the header segment.
646 Transit
= (BOOLEAN
) ISCSI_FLAG_ON (LoginRsp
, ISCSI_LOGIN_RSP_PDU_FLAG_TRANSIT
);
647 Continue
= (BOOLEAN
) ISCSI_FLAG_ON (LoginRsp
, ISCSI_LOGIN_RSP_PDU_FLAG_CONTINUE
);
649 CurrentStage
= (UINT8
) ISCSI_GET_CURRENT_STAGE (LoginRsp
);
650 NextStage
= (UINT8
) ISCSI_GET_NEXT_STAGE (LoginRsp
);
652 LoginRsp
->InitiatorTaskTag
= NTOHL (LoginRsp
->InitiatorTaskTag
);
654 if ((Transit
&& Continue
) ||
655 (CurrentStage
!= Conn
->CurrentStage
) ||
656 (!Conn
->TransitInitiated
&& Transit
) ||
657 (Transit
&& (NextStage
!= Conn
->NextStage
)) ||
658 (CompareMem (Session
->Isid
, LoginRsp
->Isid
, sizeof (LoginRsp
->Isid
)) != 0) ||
659 (LoginRsp
->InitiatorTaskTag
!= Session
->InitiatorTaskTag
)
662 // A Login Response with the C bit set to 1 MUST have the T bit set to 0;
663 // The CSG in the Login Response MUST be the same with the I-end of this connection;
664 // The T bit can't be 1 if the last Login Response sent by the initiator doesn't
665 // initiate the transistion;
666 // The NSG MUST be the same with the I-end of this connection if Transit is required.
667 // The ISID in the Login Response MUST be the same with this session.
669 return EFI_PROTOCOL_ERROR
;
672 LoginRsp
->StatSN
= NTOHL (LoginRsp
->StatSN
);
673 LoginRsp
->ExpCmdSN
= NTOHL (LoginRsp
->ExpCmdSN
);
674 LoginRsp
->MaxCmdSN
= NTOHL (LoginRsp
->MaxCmdSN
);
676 if ((Conn
->CurrentStage
== ISCSI_SECURITY_NEGOTIATION
) && (Conn
->CHAPStep
== ISCSI_CHAP_INITIAL
)) {
678 // It's the initial Login Response, initialize the local ExpStatSN, MaxCmdSN
681 Conn
->ExpStatSN
= LoginRsp
->StatSN
+ 1;
682 Session
->MaxCmdSN
= LoginRsp
->MaxCmdSN
;
683 Session
->ExpCmdSN
= LoginRsp
->ExpCmdSN
;
686 // Check the StatSN of this PDU
688 Status
= IScsiCheckSN (&Conn
->ExpStatSN
, LoginRsp
->StatSN
);
689 if (!EFI_ERROR (Status
)) {
691 // Update the MaxCmdSN and ExpCmdSN
693 IScsiUpdateCmdSN (Session
, LoginRsp
->MaxCmdSN
, LoginRsp
->ExpCmdSN
);
699 // Trim off the header segment.
701 NetbufTrim (Pdu
, sizeof (ISCSI_LOGIN_RESPONSE
), NET_BUF_HEAD
);
704 // Queue this login response first in case it's a partial response so that
705 // later when the full response list is received we can combine these scattered
706 // responses' data segment and then process it.
709 NetbufQueAppend (&Conn
->RspQue
, Pdu
);
711 Conn
->PartialRspRcvd
= Continue
;
714 // It's a partial response, have to wait for another or more Request/Response
715 // conversations to get the full response.
720 switch (CurrentStage
) {
721 case ISCSI_SECURITY_NEGOTIATION
:
723 // In security negotiation stage, let CHAP module handle it.
725 Status
= IScsiCHAPOnRspReceived (Conn
);
728 case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
:
730 // Response received with negotiation resonse on iSCSI parameters, check them.
732 Status
= IScsiCheckOpParams (Conn
);
737 // Should never get here.
739 Status
= EFI_PROTOCOL_ERROR
;
743 if (Transit
&& (Status
== EFI_SUCCESS
)) {
745 // Do the state transition.
747 Conn
->CurrentStage
= Conn
->NextStage
;
749 if (Conn
->CurrentStage
== ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
) {
750 Conn
->NextStage
= ISCSI_FULL_FEATURE_PHASE
;
753 // CurrentStage is iSCSI Full Feature, it's the Login-Final Response,
754 // get the TSIH from the Login Response.
756 Session
->Tsih
= NTOHS (LoginRsp
->Tsih
);
760 // Flush the response(s) received.
762 NetbufQueFlush (&Conn
->RspQue
);
768 Updated the target information according the data received in the iSCSI
769 login response with an target redirection status.
771 @param[in, out] Session The iSCSI session.
772 @param[in] Data The data segment which should contain the
773 TargetAddress key-value list.
774 @param[in] Len Length of the data.
776 @retval EFI_SUCCESS The target address is updated.
777 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
778 @retval EFI_NOT_FOUND The TargetAddress key is not found.
779 @retval Others Other errors as indicated.
782 IScsiUpdateTargetAddress (
783 IN OUT ISCSI_SESSION
*Session
,
788 LIST_ENTRY
*KeyValueList
;
789 CHAR8
*TargetAddress
;
794 KeyValueList
= IScsiBuildKeyValueList (Data
, Len
);
795 if (KeyValueList
== NULL
) {
796 return EFI_OUT_OF_RESOURCES
;
799 Status
= EFI_NOT_FOUND
;
802 TargetAddress
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_ADDRESS
);
803 if (TargetAddress
== NULL
) {
807 if (!NET_IS_DIGIT (TargetAddress
[0])) {
809 // The domainname of the target may be presented in three formats: a DNS host name,
810 // a dotted-decimal IPv4 address, or a bracketed IPv6 address. Only accept dotted
816 IpStr
= TargetAddress
;
818 while ((*TargetAddress
!= 0) && (*TargetAddress
!= ':') && (*TargetAddress
!= ',')) {
820 // NULL, ':' or ',' ends the IPv4 string.
825 if (*TargetAddress
== ',') {
827 // Comma and the portal group tag MUST be ommitted if the TargetAddress is sent
828 // as the result of a redirection.
831 } else if (*TargetAddress
== ':') {
832 *TargetAddress
= '\0';
836 Number
= AsciiStrDecimalToUintn (TargetAddress
);
837 if (Number
> 0xFFFF) {
840 Session
->ConfigData
.NvData
.TargetPort
= (UINT16
) Number
;
844 // The string only contains the IPv4 address. Use the well known port.
846 Session
->ConfigData
.NvData
.TargetPort
= ISCSI_WELL_KNOWN_PORT
;
849 // Update the target IP address.
851 Status
= IScsiAsciiStrToIp (IpStr
, &Session
->ConfigData
.NvData
.TargetIp
);
852 if (EFI_ERROR (Status
)) {
859 IScsiFreeKeyValueList (KeyValueList
);
865 The callback function to free the net buffer list.
867 @param[in] Arg The opaque parameter.
874 ASSERT (Arg
!= NULL
);
876 NetbufFreeList ((LIST_ENTRY
*) Arg
);
881 The callback function called in NetBufFree, it does nothing.
883 @param[in] Arg The opaque parameter.
893 Receive an iSCSI response PDU. An iSCSI response PDU contains an iSCSI PDU header and
894 an optional data segment. The two parts will be put into two blocks of buffers in the
895 net buffer. The digest check will be conducted in this function if needed and the digests
896 will be trimmed from the PDU buffer.
898 @param[in] Conn The iSCSI connection to receive data from.
899 @param[out] Pdu The received iSCSI pdu.
900 @param[in] Context The context used to describe information on the caller provided
901 buffer to receive data segment of the iSCSI pdu, it's optional.
902 @param[in] HeaderDigest Whether there will be header digest received.
903 @param[in] DataDigest Whether there will be data digest.
904 @param[in] TimeoutEvent The timeout event, it's optional.
906 @retval EFI_SUCCESS An iSCSI pdu is received.
907 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
908 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
909 @retval Others Other errors as indicated.
913 IN ISCSI_CONNECTION
*Conn
,
915 IN ISCSI_IN_BUFFER_CONTEXT
*Context
, OPTIONAL
916 IN BOOLEAN HeaderDigest
,
917 IN BOOLEAN DataDigest
,
918 IN EFI_EVENT TimeoutEvent OPTIONAL
921 LIST_ENTRY
*NbufList
;
928 NET_FRAGMENT Fragment
[2];
929 UINT32 FragmentCount
;
931 UINT32 PadAndCRC32
[2];
933 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
934 if (NbufList
== NULL
) {
935 return EFI_OUT_OF_RESOURCES
;
938 InitializeListHead (NbufList
);
941 // The header digest will be received together with the PDU header if exists.
943 Len
= sizeof (ISCSI_BASIC_HEADER
) + (HeaderDigest
? sizeof (UINT32
) : 0);
944 PduHdr
= NetbufAlloc (Len
);
945 if (PduHdr
== NULL
) {
946 Status
= EFI_OUT_OF_RESOURCES
;
950 Header
= NetbufAllocSpace (PduHdr
, Len
, NET_BUF_TAIL
);
951 InsertTailList (NbufList
, &PduHdr
->List
);
954 // First step, receive the BHS of the PDU.
956 Status
= Tcp4IoReceive (&Conn
->Tcp4Io
, PduHdr
, FALSE
, TimeoutEvent
);
957 if (EFI_ERROR (Status
)) {
963 // TODO: check the header-digest.
966 // Trim off the digest.
968 NetbufTrim (PduHdr
, sizeof (UINT32
), NET_BUF_TAIL
);
971 Len
= ISCSI_GET_DATASEG_LEN (Header
);
974 // No data segment.Form the pdu from a list of pdu segments.
976 *Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
978 Status
= EFI_OUT_OF_RESOURCES
;
984 // Get the length of the padding bytes of the data segment.
986 PadLen
= ISCSI_GET_PAD_LEN (Len
);
988 switch (ISCSI_GET_OPCODE (Header
)) {
989 case ISCSI_OPCODE_SCSI_DATA_IN
:
991 // Try to use the buffer described by Context if the PDU is an
992 // iSCSI SCSI data in pdu so as to reduce memory copy overhead.
994 InDataOffset
= ISCSI_GET_BUFFER_OFFSET (Header
);
995 if ((Context
== NULL
) || ((InDataOffset
+ Len
) > Context
->InDataLen
)) {
996 Status
= EFI_PROTOCOL_ERROR
;
1000 Fragment
[0].Len
= Len
;
1001 Fragment
[0].Bulk
= Context
->InData
+ InDataOffset
;
1003 if (DataDigest
|| (PadLen
!= 0)) {
1005 // The data segment is padded, use two fragments to receive it.
1006 // The first to receive the useful data. The second to receive the padding.
1008 Fragment
[1].Len
= PadLen
+ (DataDigest
? sizeof (UINT32
) : 0);
1009 Fragment
[1].Bulk
= (UINT8
*) ((UINTN
) &PadAndCRC32
[1] - PadLen
);
1016 DataSeg
= NetbufFromExt (&Fragment
[0], FragmentCount
, 0, 0, IScsiNbufExtFree
, NULL
);
1017 if (DataSeg
== NULL
) {
1018 Status
= EFI_OUT_OF_RESOURCES
;
1024 case ISCSI_OPCODE_SCSI_RSP
:
1025 case ISCSI_OPCODE_NOP_IN
:
1026 case ISCSI_OPCODE_LOGIN_RSP
:
1027 case ISCSI_OPCODE_TEXT_RSP
:
1028 case ISCSI_OPCODE_ASYNC_MSG
:
1029 case ISCSI_OPCODE_REJECT
:
1030 case ISCSI_OPCODE_VENDOR_T0
:
1031 case ISCSI_OPCODE_VENDOR_T1
:
1032 case ISCSI_OPCODE_VENDOR_T2
:
1034 // Allocate buffer to receive the data segment.
1036 Len
+= PadLen
+ (DataDigest
? sizeof (UINT32
) : 0);
1037 DataSeg
= NetbufAlloc (Len
);
1038 if (DataSeg
== NULL
) {
1039 Status
= EFI_OUT_OF_RESOURCES
;
1043 NetbufAllocSpace (DataSeg
, Len
, NET_BUF_TAIL
);
1047 Status
= EFI_PROTOCOL_ERROR
;
1051 InsertTailList (NbufList
, &DataSeg
->List
);
1054 // Receive the data segment with the data digest if any.
1056 Status
= Tcp4IoReceive (&Conn
->Tcp4Io
, DataSeg
, FALSE
, TimeoutEvent
);
1057 if (EFI_ERROR (Status
)) {
1063 // TODO: Check the data digest.
1065 NetbufTrim (DataSeg
, sizeof (UINT32
), NET_BUF_TAIL
);
1070 // Trim off the padding bytes in the data segment.
1072 NetbufTrim (DataSeg
, PadLen
, NET_BUF_TAIL
);
1076 // Form the pdu from a list of pdu segments.
1078 *Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1080 Status
= EFI_OUT_OF_RESOURCES
;
1085 if (EFI_ERROR (Status
)) {
1087 // Free the Nbufs in this NbufList and the NbufList itself.
1089 IScsiFreeNbufList (NbufList
);
1096 Check and get the result of the prameter negotiation.
1098 @param[in, out] Conn The connection in iSCSI login.
1100 @retval EFI_SUCCESS The parmeter check is passed and negotiation is finished.
1101 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
1102 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1105 IScsiCheckOpParams (
1106 IN OUT ISCSI_CONNECTION
*Conn
1110 LIST_ENTRY
*KeyValueList
;
1113 ISCSI_SESSION
*Session
;
1117 ASSERT (Conn
->RspQue
.BufNum
!= 0);
1119 Session
= Conn
->Session
;
1121 Len
= Conn
->RspQue
.BufSize
;
1122 Data
= AllocatePool (Len
);
1124 return EFI_OUT_OF_RESOURCES
;
1127 NetbufQueCopy (&Conn
->RspQue
, 0, Len
, (UINT8
*) Data
);
1129 Status
= EFI_PROTOCOL_ERROR
;
1132 // Extract the Key-Value pairs into a list.
1134 KeyValueList
= IScsiBuildKeyValueList (Data
, Len
);
1135 if (KeyValueList
== NULL
) {
1136 gBS
->FreePool (Data
);
1142 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_HEADER_DIGEST
);
1143 if (Value
== NULL
) {
1147 if (AsciiStrCmp (Value
, "CRC32") == 0) {
1148 if (Conn
->HeaderDigest
!= ISCSI_DIGEST_CRC32
) {
1151 } else if (AsciiStrCmp (Value
, ISCSI_KEY_VALUE_NONE
) == 0) {
1152 Conn
->HeaderDigest
= ISCSI_DIGEST_NONE
;
1159 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_DIGEST
);
1160 if (Value
== NULL
) {
1164 if (AsciiStrCmp (Value
, "CRC32") == 0) {
1165 if (Conn
->DataDigest
!= ISCSI_DIGEST_CRC32
) {
1168 } else if (AsciiStrCmp (Value
, ISCSI_KEY_VALUE_NONE
) == 0) {
1169 Conn
->DataDigest
= ISCSI_DIGEST_NONE
;
1174 // ErrorRecoveryLevel, result fuction is Minimum.
1176 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_ERROR_RECOVERY_LEVEL
);
1177 if (Value
== NULL
) {
1181 NumericValue
= AsciiStrDecimalToUintn (Value
);
1182 if (NumericValue
> 2) {
1186 Session
->ErrorRecoveryLevel
= (UINT8
) MIN (Session
->ErrorRecoveryLevel
, NumericValue
);
1189 // InitialR2T, result function is OR.
1191 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_INITIAL_R2T
);
1192 if (Value
== NULL
) {
1196 Session
->InitialR2T
= (BOOLEAN
) (Session
->InitialR2T
|| (AsciiStrCmp (Value
, "Yes") == 0));
1199 // ImmediateData, result function is AND.
1201 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_IMMEDIATE_DATA
);
1202 if (Value
== NULL
) {
1206 Session
->ImmediateData
= (BOOLEAN
) (Session
->ImmediateData
&& (AsciiStrCmp (Value
, "Yes") == 0));
1209 // MaxRecvDataSegmentLength is declarative.
1211 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
);
1212 if (Value
!= NULL
) {
1213 Conn
->MaxRecvDataSegmentLength
= (UINT32
) AsciiStrDecimalToUintn (Value
);
1216 // MaxBurstLength, result funtion is Mininum.
1218 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_BURST_LENGTH
);
1219 if (Value
== NULL
) {
1223 NumericValue
= AsciiStrDecimalToUintn (Value
);
1224 Session
->MaxBurstLength
= (UINT32
) MIN (Session
->MaxBurstLength
, NumericValue
);
1227 // FirstBurstLength, result function is Minimum. Irrelevant when InitialR2T=Yes and
1228 // ImmediateData=No.
1230 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_FIRST_BURST_LENGTH
);
1231 if ((Value
== NULL
) && !(Session
->InitialR2T
&& !Session
->ImmediateData
)) {
1235 NumericValue
= AsciiStrDecimalToUintn (Value
);
1236 Session
->FirstBurstLength
= (UINT32
) MIN (Session
->FirstBurstLength
, NumericValue
);
1239 // MaxConnections, result function is Minimum.
1241 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_CONNECTIONS
);
1242 if (Value
== NULL
) {
1246 NumericValue
= AsciiStrDecimalToUintn (Value
);
1247 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1251 Session
->MaxConnections
= (UINT32
) MIN (Session
->MaxConnections
, NumericValue
);
1254 // DataPDUInOrder, result function is OR.
1256 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_PDU_IN_ORDER
);
1257 if (Value
== NULL
) {
1261 Session
->DataPDUInOrder
= (BOOLEAN
) (Session
->DataPDUInOrder
|| (AsciiStrCmp (Value
, "Yes") == 0));
1264 // DataSequenceInorder, result function is OR.
1266 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
);
1267 if (Value
== NULL
) {
1271 Session
->DataSequenceInOrder
= (BOOLEAN
) (Session
->DataSequenceInOrder
|| (AsciiStrCmp (Value
, "Yes") == 0));
1274 // DefaultTime2Wait, result function is Maximum.
1276 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2WAIT
);
1277 if (Value
== NULL
) {
1281 NumericValue
= AsciiStrDecimalToUintn (Value
);
1282 if (NumericValue
== 0) {
1283 Session
->DefaultTime2Wait
= 0;
1284 } else if (NumericValue
> 3600) {
1287 Session
->DefaultTime2Wait
= (UINT32
) MAX (Session
->DefaultTime2Wait
, NumericValue
);
1290 // DefaultTime2Retain, result function is Minimum.
1292 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2RETAIN
);
1293 if (Value
== NULL
) {
1297 NumericValue
= AsciiStrDecimalToUintn (Value
);
1298 if (NumericValue
== 0) {
1299 Session
->DefaultTime2Retain
= 0;
1300 } else if (NumericValue
> 3600) {
1303 Session
->DefaultTime2Retain
= (UINT32
) MIN (Session
->DefaultTime2Retain
, NumericValue
);
1306 // MaxOutstandingR2T, result function is Minimum.
1308 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_OUTSTANDING_R2T
);
1309 if (Value
== NULL
) {
1313 NumericValue
= AsciiStrDecimalToUintn (Value
);
1314 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1318 Session
->MaxOutstandingR2T
= (UINT16
) MIN (Session
->MaxOutstandingR2T
, NumericValue
);
1321 // Remove declarative key-value paris if any.
1323 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_SESSION_TYPE
);
1324 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_ALIAS
);
1325 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_PORTAL_GROUP_TAG
);
1327 if (IsListEmpty (KeyValueList
)) {
1329 // Succeed if no more keys in the list.
1331 Status
= EFI_SUCCESS
;
1336 IScsiFreeKeyValueList (KeyValueList
);
1338 gBS
->FreePool (Data
);
1344 Fill the oprational prameters.
1346 @param[in] Conn The connection in iSCSI login.
1347 @param[in, out] Pdu The iSCSI login request PDU to fill the parameters.
1349 @retval EFI_SUCCESS The parmeters are filled into the iSCSI login request PDU.
1353 IN ISCSI_CONNECTION
*Conn
,
1357 ISCSI_SESSION
*Session
;
1360 Session
= Conn
->Session
;
1362 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->HeaderDigest
== ISCSI_DIGEST_CRC32
) ? "None,CRC32" : "None");
1363 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_HEADER_DIGEST
, Value
);
1365 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->DataDigest
== ISCSI_DIGEST_CRC32
) ? "None,CRC32" : "None");
1366 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_DIGEST
, Value
);
1368 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->ErrorRecoveryLevel
);
1369 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_ERROR_RECOVERY_LEVEL
, Value
);
1371 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->InitialR2T
? "Yes" : "No");
1372 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_INITIAL_R2T
, Value
);
1374 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->ImmediateData
? "Yes" : "No");
1375 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_IMMEDIATE_DATA
, Value
);
1377 AsciiSPrint (Value
, sizeof (Value
), "%d", MAX_RECV_DATA_SEG_LEN_IN_FFP
);
1378 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
, Value
);
1380 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxBurstLength
);
1381 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_BURST_LENGTH
, Value
);
1383 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->FirstBurstLength
);
1384 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_FIRST_BURST_LENGTH
, Value
);
1386 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxConnections
);
1387 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_CONNECTIONS
, Value
);
1389 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataPDUInOrder
? "Yes" : "No");
1390 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_PDU_IN_ORDER
, Value
);
1392 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataSequenceInOrder
? "Yes" : "No");
1393 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
, Value
);
1395 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Wait
);
1396 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2WAIT
, Value
);
1398 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Retain
);
1399 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2RETAIN
, Value
);
1401 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxOutstandingR2T
);
1402 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_OUTSTANDING_R2T
, Value
);
1408 Pad the iSCSI AHS or data segment to an integer number of 4 byte words.
1410 @param[in, out] Pdu The iSCSI pdu which contains segments to pad.
1411 @param[in] Len The length of the last semgnet in the PDU.
1413 @retval EFI_SUCCESS The segment is padded or no need to pad it.
1414 @retval EFI_OUT_OF_RESOURCES There is not enough remaining free space to add the
1419 IN OUT NET_BUF
*Pdu
,
1426 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1429 Data
= NetbufAllocSpace (Pdu
, PadLen
, NET_BUF_TAIL
);
1431 return EFI_OUT_OF_RESOURCES
;
1434 ZeroMem (Data
, PadLen
);
1441 Build a key-value list from the data segment.
1443 @param[in] Data The data segment containing the key-value pairs.
1444 @param[in] Len Length of the data segment.
1446 @return The key-value list.
1447 @retval NULL Other errors as indicated.
1450 IScsiBuildKeyValueList (
1455 LIST_ENTRY
*ListHead
;
1456 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1458 ListHead
= AllocatePool (sizeof (LIST_ENTRY
));
1459 if (ListHead
== NULL
) {
1463 InitializeListHead (ListHead
);
1466 KeyValuePair
= AllocatePool (sizeof (ISCSI_KEY_VALUE_PAIR
));
1467 if (KeyValuePair
== NULL
) {
1471 InitializeListHead (&KeyValuePair
->List
);
1473 KeyValuePair
->Key
= Data
;
1475 while ((Len
> 0) && (*Data
!= '=')) {
1486 gBS
->FreePool (KeyValuePair
);
1490 KeyValuePair
->Value
= Data
;
1492 InsertTailList (ListHead
, &KeyValuePair
->List
);;
1494 Data
+= AsciiStrLen (KeyValuePair
->Value
) + 1;
1495 Len
-= (UINT32
) AsciiStrLen (KeyValuePair
->Value
) + 1;
1502 IScsiFreeKeyValueList (ListHead
);
1508 Get the value string by the key name from the key-value list. If found,
1509 the key-value entry will be removed from the list.
1511 @param[in, out] KeyValueList The key-value list.
1512 @param[in] Key The key name to find.
1514 @return The value string.
1517 IScsiGetValueByKeyFromList (
1518 IN OUT LIST_ENTRY
*KeyValueList
,
1523 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1528 NET_LIST_FOR_EACH (Entry
, KeyValueList
) {
1529 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1531 if (AsciiStrCmp (KeyValuePair
->Key
, Key
) == 0) {
1532 Value
= KeyValuePair
->Value
;
1534 RemoveEntryList (&KeyValuePair
->List
);
1535 gBS
->FreePool (KeyValuePair
);
1544 Free the key-value list.
1546 @param[in] KeyValueList The key-value list.
1549 IScsiFreeKeyValueList (
1550 IN LIST_ENTRY
*KeyValueList
1554 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1556 while (!IsListEmpty (KeyValueList
)) {
1557 Entry
= NetListRemoveHead (KeyValueList
);
1558 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1560 gBS
->FreePool (KeyValuePair
);
1563 gBS
->FreePool (KeyValueList
);
1567 Normalize the iSCSI name according to RFC.
1569 @param[in, out] Name The iSCSI name.
1570 @param[in] Len length of the iSCSI name.
1572 @retval EFI_SUCCESS The iSCSI name is valid and normalized.
1573 @retval EFI_PROTOCOL_ERROR The iSCSI name is mal-formatted or not in the IQN format.
1576 IScsiNormalizeName (
1583 for (Index
= 0; Index
< Len
; Index
++) {
1584 if (NET_IS_UPPER_CASE_CHAR (Name
[Index
])) {
1586 // Convert the upper-case characters to lower-case ones
1588 Name
[Index
] = (CHAR8
) (Name
[Index
] - 'A' + 'a');
1591 if (!NET_IS_LOWER_CASE_CHAR (Name
[Index
]) &&
1592 !NET_IS_DIGIT (Name
[Index
]) &&
1593 (Name
[Index
] != '-') &&
1594 (Name
[Index
] != '.') &&
1595 (Name
[Index
] != ':')
1598 // ASCII dash, dot, colon lower-case characters and digit characters
1601 return EFI_PROTOCOL_ERROR
;
1605 if ((Len
< 4) || (CompareMem (Name
, "iqn.", 4) != 0)) {
1607 // Only IQN format is accepted now.
1609 return EFI_PROTOCOL_ERROR
;
1616 Create an iSCSI task control block.
1618 @param[in] Conn The connection on which the task control block will be created.
1619 @param[out] Tcb The newly created task control block.
1621 @retval EFI_SUCCESS The task control block is created.
1622 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1623 @retval EFI_NOT_READY The target can not accept new commands.
1627 IN ISCSI_CONNECTION
*Conn
,
1631 ISCSI_SESSION
*Session
;
1634 ASSERT (Tcb
!= NULL
);
1636 Session
= Conn
->Session
;
1638 if (ISCSI_SEQ_GT (Session
->CmdSN
, Session
->MaxCmdSN
)) {
1639 return EFI_NOT_READY
;
1642 NewTcb
= AllocateZeroPool (sizeof (ISCSI_TCB
));
1643 if (NewTcb
== NULL
) {
1644 return EFI_OUT_OF_RESOURCES
;
1647 InitializeListHead (&NewTcb
->Link
);
1649 NewTcb
->SoFarInOrder
= TRUE
;
1650 NewTcb
->InitiatorTaskTag
= Session
->InitiatorTaskTag
;
1651 NewTcb
->CmdSN
= Session
->CmdSN
;
1652 NewTcb
->Conn
= Conn
;
1654 InsertTailList (&Session
->TcbList
, &NewTcb
->Link
);
1657 // Advance the initiator task tag.
1659 Session
->InitiatorTaskTag
++;
1668 Delete the tcb from the connection and destroy it.
1670 @param[in] Tcb The tcb to delete.
1677 RemoveEntryList (&Tcb
->Link
);
1679 gBS
->FreePool (Tcb
);
1683 Find the task control block by the initator task tag.
1685 @param[in] TcbList The tcb list.
1686 @param[in] InitiatorTaskTag The initiator task tag.
1688 @return The task control block found.
1692 IN LIST_ENTRY
*TcbList
,
1693 IN UINT32 InitiatorTaskTag
1701 NET_LIST_FOR_EACH (Entry
, TcbList
) {
1702 Tcb
= NET_LIST_USER_STRUCT (Entry
, ISCSI_TCB
, Link
);
1704 if (Tcb
->InitiatorTaskTag
== InitiatorTaskTag
) {
1715 Create a data segment, pad it and calculate the CRC if needed.
1717 @param[in] Data The data to fill into the data segment.
1718 @param[in] Len Length of the data.
1719 @param[in] DataDigest Whether to calculate CRC for this data segment.
1721 @return The net buffer wrapping the data segment.
1724 IScsiNewDataSegment (
1727 IN BOOLEAN DataDigest
1730 NET_FRAGMENT Fragment
[2];
1731 UINT32 FragmentCount
;
1735 Fragment
[0].Len
= Len
;
1736 Fragment
[0].Bulk
= Data
;
1738 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1740 Fragment
[1].Len
= PadLen
;
1741 Fragment
[1].Bulk
= (UINT8
*) &mDataSegPad
;
1748 DataSeg
= NetbufFromExt (&Fragment
[0], FragmentCount
, 0, 0, IScsiNbufExtFree
, NULL
);
1754 Create a iSCSI SCSI command PDU to encapsulate the command issued
1755 by SCSI through the EXT SCSI PASS THRU Protocol.
1757 @param[in] Packet The EXT SCSI PASS THRU request packet containing the SCSI command.
1758 @param[in] Lun The LUN.
1759 @param[in] Tcb The tcb assocated with this SCSI command.
1761 @return The created iSCSI SCSI command PDU.
1762 @retval NULL Other errors as indicated.
1765 IScsiNewScsiCmdPdu (
1766 IN EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
1771 LIST_ENTRY
*NbufList
;
1775 SCSI_COMMAND
*ScsiCmd
;
1778 ISCSI_ADDITIONAL_HEADER
*Header
;
1779 ISCSI_BI_EXP_READ_DATA_LEN_AHS
*BiExpReadDataLenAHS
;
1780 ISCSI_SESSION
*Session
;
1781 UINT32 ImmediateDataLen
;
1785 if (Packet
->DataDirection
== DataBi
) {
1787 // Bi directional Read/Write command, the bidirectional expected
1788 // read data length AHS is required.
1790 AHSLength
+= sizeof (ISCSI_BI_EXP_READ_DATA_LEN_AHS
);
1793 if (Packet
->CdbLength
> 16) {
1795 // The CDB exceeds 16 bytes, an extended CDB AHS is required.
1797 AHSLength
= (UINT8
) (AHSLength
+ (ISCSI_ROUNDUP (Packet
->CdbLength
- 16) + sizeof (ISCSI_ADDITIONAL_HEADER
)));
1800 Length
= sizeof (SCSI_COMMAND
) + AHSLength
;
1801 PduHeader
= NetbufAlloc (Length
);
1802 if (PduHeader
== NULL
) {
1806 ScsiCmd
= (SCSI_COMMAND
*) NetbufAllocSpace (PduHeader
, Length
, NET_BUF_TAIL
);
1807 Header
= (ISCSI_ADDITIONAL_HEADER
*) (ScsiCmd
+ 1);
1809 ZeroMem (ScsiCmd
, Length
);
1811 ISCSI_SET_OPCODE (ScsiCmd
, ISCSI_OPCODE_SCSI_CMD
, 0);
1812 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_TASK_ATTR_SIMPLE
);
1815 // Set the READ/WRITE flags according to the IO type of this request.
1817 switch (Packet
->DataDirection
) {
1819 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
);
1820 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->InTransferLength
);
1824 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_WRITE
);
1825 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
1829 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
| SCSI_CMD_PDU_FLAG_WRITE
);
1830 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
1833 // Fill the bidirectional expected read data length AHS.
1835 BiExpReadDataLenAHS
= (ISCSI_BI_EXP_READ_DATA_LEN_AHS
*) Header
;
1836 Header
= (ISCSI_ADDITIONAL_HEADER
*) (BiExpReadDataLenAHS
+ 1);
1838 BiExpReadDataLenAHS
->Length
= NTOHS (5);
1839 BiExpReadDataLenAHS
->Type
= ISCSI_AHS_TYPE_BI_EXP_READ_DATA_LEN
;
1840 BiExpReadDataLenAHS
->ExpReadDataLength
= NTOHL (Packet
->InTransferLength
);
1845 ScsiCmd
->TotalAHSLength
= AHSLength
;
1846 CopyMem (ScsiCmd
->Lun
, &Lun
, sizeof (ScsiCmd
->Lun
));
1847 ScsiCmd
->InitiatorTaskTag
= NTOHL (Tcb
->InitiatorTaskTag
);
1848 ScsiCmd
->CmdSN
= NTOHL (Tcb
->CmdSN
);
1849 ScsiCmd
->ExpStatSN
= NTOHL (Tcb
->Conn
->ExpStatSN
);
1851 CopyMem (ScsiCmd
->CDB
, Packet
->Cdb
, sizeof (ScsiCmd
->CDB
));
1853 if (Packet
->CdbLength
> 16) {
1854 Header
->Length
= NTOHS (Packet
->CdbLength
- 15);
1855 Header
->Type
= ISCSI_AHS_TYPE_EXT_CDB
;
1857 CopyMem (Header
+ 1, (UINT8
*) Packet
->Cdb
+ 16, Packet
->CdbLength
- 16);
1861 Session
= Tcb
->Conn
->Session
;
1862 ImmediateDataLen
= 0;
1864 if (Session
->ImmediateData
&& (Packet
->OutTransferLength
!= 0)) {
1866 // Send immediate data in this SCSI Command PDU. The length of the immeidate
1867 // data is the minimum of FirstBurstLength, the data length to be xfered and
1868 // the MaxRecvdataSegmentLength on this connection.
1870 ImmediateDataLen
= MIN (Session
->FirstBurstLength
, Packet
->OutTransferLength
);
1871 ImmediateDataLen
= MIN (ImmediateDataLen
, Tcb
->Conn
->MaxRecvDataSegmentLength
);
1874 // Update the data segment length in the PDU header.
1876 ISCSI_SET_DATASEG_LEN (ScsiCmd
, ImmediateDataLen
);
1879 // Create the data segment.
1881 DataSeg
= IScsiNewDataSegment ((UINT8
*) Packet
->OutDataBuffer
, ImmediateDataLen
, FALSE
);
1882 if (DataSeg
== NULL
) {
1883 NetbufFree (PduHeader
);
1888 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
1889 if (NbufList
== NULL
) {
1890 NetbufFree (PduHeader
);
1891 NetbufFree (DataSeg
);
1897 InitializeListHead (NbufList
);
1898 InsertTailList (NbufList
, &PduHeader
->List
);
1899 InsertTailList (NbufList
, &DataSeg
->List
);
1901 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1903 IScsiFreeNbufList (NbufList
);
1907 if (Session
->InitialR2T
||
1908 (ImmediateDataLen
== Session
->FirstBurstLength
) ||
1909 (ImmediateDataLen
== Packet
->OutTransferLength
)
1912 // Unsolicited data out sequence is not allowed,
1913 // or FirstBustLength data is already sent out by immediate data
1914 // or all the OUT data accompany this SCSI packet is sent as
1915 // immediate data, the final flag should be set on this SCSI Command
1918 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_BHS_FLAG_FINAL
);
1927 Create a new iSCSI SCSI Data Out PDU.
1929 @param[in] Data The data to put into the Data Out PDU.
1930 @param[in] Len Length of the data.
1931 @param[in] DataSN The DataSN of the Data Out PDU.
1932 @param[in] Tcb The task control block of this Data Out PDU.
1933 @param[in] Lun The LUN.
1935 @return The net buffer wrapping the Data Out PDU.
1936 @retval NULL Other errors as indicated.
1939 IScsiNewDataOutPdu (
1947 LIST_ENTRY
*NbufList
;
1951 ISCSI_SCSI_DATA_OUT
*DataOutHdr
;
1952 ISCSI_XFER_CONTEXT
*XferContext
;
1954 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
1955 if (NbufList
== NULL
) {
1959 InitializeListHead (NbufList
);
1962 // Allocate memory for the BHS.
1964 PduHdr
= NetbufAlloc (sizeof (ISCSI_SCSI_DATA_OUT
));
1965 if (PduHdr
== NULL
) {
1966 gBS
->FreePool (NbufList
);
1970 // Insert the BHS into the buffer list.
1972 InsertTailList (NbufList
, &PduHdr
->List
);
1974 DataOutHdr
= (ISCSI_SCSI_DATA_OUT
*) NetbufAllocSpace (PduHdr
, sizeof (ISCSI_SCSI_DATA_OUT
), NET_BUF_TAIL
);
1975 XferContext
= &Tcb
->XferContext
;
1977 ZeroMem (DataOutHdr
, sizeof (ISCSI_SCSI_DATA_OUT
));
1980 // Set the flags and fields of the Data Out PDU BHS.
1982 ISCSI_SET_OPCODE (DataOutHdr
, ISCSI_OPCODE_SCSI_DATA_OUT
, 0);
1983 ISCSI_SET_DATASEG_LEN (DataOutHdr
, Len
);
1985 DataOutHdr
->InitiatorTaskTag
= HTONL (Tcb
->InitiatorTaskTag
);
1986 DataOutHdr
->TargetTransferTag
= HTONL (XferContext
->TargetTransferTag
);
1987 DataOutHdr
->ExpStatSN
= HTONL (Tcb
->Conn
->ExpStatSN
);
1988 DataOutHdr
->DataSN
= HTONL (DataSN
);
1989 DataOutHdr
->BufferOffset
= HTONL (XferContext
->Offset
);
1991 if (XferContext
->TargetTransferTag
!= ISCSI_RESERVED_TAG
) {
1992 CopyMem (&DataOutHdr
->Lun
, &Lun
, sizeof (DataOutHdr
->Lun
));
1995 // Build the data segment for this Data Out PDU.
1997 DataSeg
= IScsiNewDataSegment (Data
, Len
, FALSE
);
1998 if (DataSeg
== NULL
) {
1999 IScsiFreeNbufList (NbufList
);
2003 // Put the data segment into the buffer list and combine it with the BHS
2004 // into a full Data Out PDU.
2006 InsertTailList (NbufList
, &DataSeg
->List
);
2007 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
2009 IScsiFreeNbufList (NbufList
);
2016 Generate a consecutive sequence of iSCSI SCSI Data Out PDUs.
2018 @param[in] Data The data which will be carried by the sequence of iSCSI SCSI Data Out PDUs.
2019 @param[in] Tcb The task control block of the data to send out.
2020 @param[in] Lun The LUN the data will be sent to.
2022 @return A list of net buffers with each of them wraps an iSCSI SCSI Data Out PDU.
2023 @retval NULL Other errors as indicated.
2026 IScsiGenerateDataOutPduSequence (
2032 LIST_ENTRY
*PduList
;
2035 NET_BUF
*DataOutPdu
;
2036 ISCSI_CONNECTION
*Conn
;
2037 ISCSI_XFER_CONTEXT
*XferContext
;
2039 PduList
= AllocatePool (sizeof (LIST_ENTRY
));
2040 if (PduList
== NULL
) {
2044 InitializeListHead (PduList
);
2049 XferContext
= &Tcb
->XferContext
;
2051 while (XferContext
->DesiredLength
> 0) {
2053 // Determine the length of data this Data Out PDU can carry.
2055 DataLen
= MIN (XferContext
->DesiredLength
, Conn
->MaxRecvDataSegmentLength
);
2058 // Create a Data Out PDU.
2060 DataOutPdu
= IScsiNewDataOutPdu (Data
, DataLen
, DataSN
, Tcb
, Lun
);
2061 if (DataOutPdu
== NULL
) {
2062 IScsiFreeNbufList (PduList
);
2068 InsertTailList (PduList
, &DataOutPdu
->List
);
2071 // Update the context and DataSN.
2073 XferContext
->Offset
+= DataLen
;
2074 XferContext
->DesiredLength
-= DataLen
;
2079 // Set the F bit for the last data out PDU in this sequence.
2081 ISCSI_SET_FLAG (NetbufGetByte (DataOutPdu
, 0, NULL
), ISCSI_BHS_FLAG_FINAL
);
2089 Send the Data in a sequence of Data Out PDUs one by one.
2091 @param[in] Data The data to carry by Data Out PDUs.
2092 @param[in] Lun The LUN the data will be sent to.
2093 @param[in] Tcb The task control block.
2095 @retval EFI_SUCCES The data is sent out to the LUN.
2096 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2097 @retval Others Other errors as indicated.
2100 IScsiSendDataOutPduSequence (
2106 LIST_ENTRY
*DataOutPduList
;
2112 // Generate the Data Out PDU sequence.
2114 DataOutPduList
= IScsiGenerateDataOutPduSequence (Data
, Tcb
, Lun
);
2115 if (DataOutPduList
== NULL
) {
2116 return EFI_OUT_OF_RESOURCES
;
2119 Status
= EFI_SUCCESS
;
2122 // Send the Data Out PDU's one by one.
2124 NET_LIST_FOR_EACH (Entry
, DataOutPduList
) {
2125 Pdu
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
2127 Status
= Tcp4IoTransmit (&Tcb
->Conn
->Tcp4Io
, Pdu
);
2128 if (EFI_ERROR (Status
)) {
2133 IScsiFreeNbufList (DataOutPduList
);
2139 Process the received iSCSI SCSI Data In PDU.
2141 @param[in] Pdu The Data In PDU received.
2142 @param[in] Tcb The task control block.
2143 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2145 @retval EFI_SUCCES The check on the Data IN PDU is passed and some update
2147 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2148 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2149 @retval Others Other errors as indicated.
2155 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2158 ISCSI_SCSI_DATA_IN
*DataInHdr
;
2161 DataInHdr
= (ISCSI_SCSI_DATA_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2163 DataInHdr
->InitiatorTaskTag
= NTOHL (DataInHdr
->InitiatorTaskTag
);
2164 DataInHdr
->ExpCmdSN
= NTOHL (DataInHdr
->ExpCmdSN
);
2165 DataInHdr
->MaxCmdSN
= NTOHL (DataInHdr
->MaxCmdSN
);
2166 DataInHdr
->DataSN
= NTOHL (DataInHdr
->DataSN
);
2169 // Check the DataSN.
2171 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, DataInHdr
->DataSN
);
2172 if (EFI_ERROR (Status
)) {
2176 if (DataInHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2177 return EFI_PROTOCOL_ERROR
;
2180 // Update the command related sequence numbers.
2182 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, DataInHdr
->MaxCmdSN
, DataInHdr
->ExpCmdSN
);
2184 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_STATUS_VALID
)) {
2185 if (!ISCSI_FLAG_ON (DataInHdr
, ISCSI_BHS_FLAG_FINAL
)) {
2187 // The S bit is on but the F bit is off.
2189 return EFI_PROTOCOL_ERROR
;
2192 Tcb
->StatusXferd
= TRUE
;
2194 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_OVERFLOW
| SCSI_DATA_IN_PDU_FLAG_UNDERFLOW
)) {
2196 // Underflow and Overflow are mutual flags.
2198 return EFI_PROTOCOL_ERROR
;
2201 // S bit is on, the StatSN is valid.
2203 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NTOHL (DataInHdr
->StatSN
));
2204 if (EFI_ERROR (Status
)) {
2208 Packet
->HostAdapterStatus
= 0;
2209 Packet
->TargetStatus
= DataInHdr
->Status
;
2211 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2212 Packet
->InTransferLength
+= NTOHL (DataInHdr
->ResidualCount
);
2213 Status
= EFI_BAD_BUFFER_SIZE
;
2216 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2217 Packet
->InTransferLength
-= NTOHL (DataInHdr
->ResidualCount
);
2225 Process the received iSCSI R2T PDU.
2227 @param[in] Pdu The R2T PDU received.
2228 @param[in] Tcb The task control block.
2229 @param[in] Lun The Lun.
2230 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2232 @retval EFI_SUCCES The R2T PDU is valid and the solicited data is sent out.
2233 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2234 @retval Others Other errors as indicated.
2241 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2244 ISCSI_READY_TO_TRANSFER
*R2THdr
;
2246 ISCSI_XFER_CONTEXT
*XferContext
;
2249 R2THdr
= (ISCSI_READY_TO_TRANSFER
*) NetbufGetByte (Pdu
, 0, NULL
);
2251 R2THdr
->InitiatorTaskTag
= NTOHL (R2THdr
->InitiatorTaskTag
);
2252 R2THdr
->TargetTransferTag
= NTOHL (R2THdr
->TargetTransferTag
);
2253 R2THdr
->StatSN
= NTOHL (R2THdr
->StatSN
);
2254 R2THdr
->R2TSN
= NTOHL (R2THdr
->R2TSN
);
2255 R2THdr
->BufferOffset
= NTOHL (R2THdr
->BufferOffset
);
2256 R2THdr
->DesiredDataTransferLength
= NTOHL (R2THdr
->DesiredDataTransferLength
);
2258 if ((R2THdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) || !ISCSI_SEQ_EQ (R2THdr
->StatSN
, Tcb
->Conn
->ExpStatSN
)) {
2259 return EFI_PROTOCOL_ERROR
;;
2262 // Check the sequence number.
2264 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, R2THdr
->R2TSN
);
2265 if (EFI_ERROR (Status
)) {
2269 XferContext
= &Tcb
->XferContext
;
2270 XferContext
->TargetTransferTag
= R2THdr
->TargetTransferTag
;
2271 XferContext
->Offset
= R2THdr
->BufferOffset
;
2272 XferContext
->DesiredLength
= R2THdr
->DesiredDataTransferLength
;
2274 if (((XferContext
->Offset
+ XferContext
->DesiredLength
) > Packet
->OutTransferLength
) ||
2275 (XferContext
->DesiredLength
> Tcb
->Conn
->Session
->MaxBurstLength
)
2277 return EFI_PROTOCOL_ERROR
;
2280 // Send the data solicited by this R2T.
2282 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2283 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2289 Process the received iSCSI SCSI Response PDU.
2291 @param[in] Pdu The Response PDU received.
2292 @param[in] Tcb The task control block.
2293 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2295 @retval EFI_SUCCES The Response PDU is processed.
2296 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2297 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2298 @retval Others Other errors as indicated.
2301 IScsiOnScsiRspRcvd (
2304 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2307 SCSI_RESPONSE
*ScsiRspHdr
;
2308 ISCSI_SENSE_DATA
*SenseData
;
2312 ScsiRspHdr
= (SCSI_RESPONSE
*) NetbufGetByte (Pdu
, 0, NULL
);
2314 ScsiRspHdr
->InitiatorTaskTag
= NTOHL (ScsiRspHdr
->InitiatorTaskTag
);
2315 if (ScsiRspHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2316 return EFI_PROTOCOL_ERROR
;
2319 ScsiRspHdr
->StatSN
= NTOHL (ScsiRspHdr
->StatSN
);
2321 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, ScsiRspHdr
->StatSN
);
2322 if (EFI_ERROR (Status
)) {
2326 ScsiRspHdr
->MaxCmdSN
= NTOHL (ScsiRspHdr
->MaxCmdSN
);
2327 ScsiRspHdr
->ExpCmdSN
= NTOHL (ScsiRspHdr
->ExpCmdSN
);
2328 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, ScsiRspHdr
->MaxCmdSN
, ScsiRspHdr
->ExpCmdSN
);
2330 Tcb
->StatusXferd
= TRUE
;
2332 Packet
->HostAdapterStatus
= ScsiRspHdr
->Response
;
2333 if (Packet
->HostAdapterStatus
!= ISCSI_SERVICE_RSP_COMMAND_COMPLETE_AT_TARGET
) {
2337 Packet
->TargetStatus
= ScsiRspHdr
->Status
;
2339 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
| SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
) ||
2340 ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
| SCSI_RSP_PDU_FLAG_UNDERFLOW
)
2342 return EFI_PROTOCOL_ERROR
;
2345 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
)) {
2346 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2347 Status
= EFI_BAD_BUFFER_SIZE
;
2350 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
)) {
2351 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2354 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2355 if (Packet
->DataDirection
== DataIn
) {
2356 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2358 Packet
->OutTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2361 Status
= EFI_BAD_BUFFER_SIZE
;
2364 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2365 if (Packet
->DataDirection
== DataIn
) {
2366 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2368 Packet
->OutTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2372 DataSegLen
= ISCSI_GET_DATASEG_LEN (ScsiRspHdr
);
2373 if (DataSegLen
!= 0) {
2374 SenseData
= (ISCSI_SENSE_DATA
*) NetbufGetByte (Pdu
, sizeof (SCSI_RESPONSE
), NULL
);
2376 SenseData
->Length
= NTOHS (SenseData
->Length
);
2378 Packet
->SenseDataLength
= (UINT8
) MIN (SenseData
->Length
, Packet
->SenseDataLength
);
2379 if (Packet
->SenseDataLength
!= 0) {
2380 CopyMem (Packet
->SenseData
, &SenseData
->Data
[0], Packet
->SenseDataLength
);
2383 Packet
->SenseDataLength
= 0;
2390 Process the received NOP In PDU.
2392 @param[in] Pdu The NOP In PDU received.
2393 @param[in] Tcb The task control block.
2395 @retval EFI_SUCCES The NOP In PDU is processed and the related sequence
2396 numbers are updated.
2397 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2405 ISCSI_NOP_IN
*NopInHdr
;
2408 NopInHdr
= (ISCSI_NOP_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2410 NopInHdr
->StatSN
= NTOHL (NopInHdr
->StatSN
);
2411 NopInHdr
->ExpCmdSN
= NTOHL (NopInHdr
->ExpCmdSN
);
2412 NopInHdr
->MaxCmdSN
= NTOHL (NopInHdr
->MaxCmdSN
);
2414 if (NopInHdr
->InitiatorTaskTag
== ISCSI_RESERVED_TAG
) {
2415 if (NopInHdr
->StatSN
!= Tcb
->Conn
->ExpStatSN
) {
2416 return EFI_PROTOCOL_ERROR
;
2419 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NopInHdr
->StatSN
);
2420 if (EFI_ERROR (Status
)) {
2425 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, NopInHdr
->MaxCmdSN
, NopInHdr
->ExpCmdSN
);
2431 Execute the SCSI command issued through the EXT SCSI PASS THRU protocol.
2433 @param[in] PassThru The EXT SCSI PASS THRU protocol.
2434 @param[in] Target The target ID.
2435 @param[in] Lun The LUN.
2436 @param[in, out] Packet The request packet containing IO request, SCSI command
2437 buffer and buffers to read/write.
2439 @retval EFI_SUCCES The SCSI command is executed and the result is updated to
2441 @retval EFI_DEVICE_ERROR Session state was not as required.
2442 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2443 @retval Others Other errors as indicated.
2446 IScsiExecuteScsiCommand (
2447 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*PassThru
,
2450 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2454 ISCSI_DRIVER_DATA
*Private
;
2455 ISCSI_SESSION
*Session
;
2456 EFI_EVENT TimeoutEvent
;
2457 ISCSI_CONNECTION
*Conn
;
2460 ISCSI_XFER_CONTEXT
*XferContext
;
2462 ISCSI_IN_BUFFER_CONTEXT InBufferContext
;
2466 Private
= ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru
);
2467 Session
= &Private
->Session
;
2468 Status
= EFI_SUCCESS
;
2470 TimeoutEvent
= NULL
;
2473 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
2474 return EFI_DEVICE_ERROR
;
2477 Conn
= NET_LIST_USER_STRUCT_S (
2478 Session
->Conns
.ForwardLink
,
2481 ISCSI_CONNECTION_SIGNATURE
2484 if (Packet
->Timeout
!= 0) {
2485 Timeout
= MultU64x32 (Packet
->Timeout
, 2);
2488 Status
= IScsiNewTcb (Conn
, &Tcb
);
2489 if (EFI_ERROR (Status
)) {
2493 // Encapsulate the SCSI request packet into an iSCSI SCSI Command PDU.
2495 Pdu
= IScsiNewScsiCmdPdu (Packet
, Lun
, Tcb
);
2497 Status
= EFI_OUT_OF_RESOURCES
;
2501 XferContext
= &Tcb
->XferContext
;
2502 Buffer
= NetbufGetByte (Pdu
, 0, NULL
);
2503 XferContext
->Offset
= ISCSI_GET_DATASEG_LEN (Buffer
);
2506 // Transmit the SCSI Command PDU.
2508 Status
= Tcp4IoTransmit (&Conn
->Tcp4Io
, Pdu
);
2512 if (EFI_ERROR (Status
)) {
2516 if (!Session
->InitialR2T
&&
2517 (XferContext
->Offset
< Session
->FirstBurstLength
) &&
2518 (XferContext
->Offset
< Packet
->OutTransferLength
)
2521 // Unsolicited Data-Out sequence is allowed, there is remaining SCSI
2522 // OUT data and the limit of FirstBurstLength is not reached.
2524 XferContext
->TargetTransferTag
= ISCSI_RESERVED_TAG
;
2525 XferContext
->DesiredLength
= MIN (
2526 Session
->FirstBurstLength
,
2527 Packet
->OutTransferLength
- XferContext
->Offset
2530 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2531 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2532 if (EFI_ERROR (Status
)) {
2537 InBufferContext
.InData
= (UINT8
*) Packet
->InDataBuffer
;
2538 InBufferContext
.InDataLen
= Packet
->InTransferLength
;
2540 while (!Tcb
->StatusXferd
) {
2542 // Start the timeout timer.
2545 Status
= gBS
->SetTimer (Conn
->TimeoutEvent
, TimerRelative
, Timeout
);
2546 if (EFI_ERROR (Status
)) {
2549 TimeoutEvent
= Conn
->TimeoutEvent
;
2552 // try to receive PDU from target.
2554 Status
= IScsiReceivePdu (Conn
, &Pdu
, &InBufferContext
, FALSE
, FALSE
, TimeoutEvent
);
2555 if (EFI_ERROR (Status
)) {
2559 switch (ISCSI_GET_OPCODE (NetbufGetByte (Pdu
, 0, NULL
))) {
2560 case ISCSI_OPCODE_SCSI_DATA_IN
:
2561 Status
= IScsiOnDataInRcvd (Pdu
, Tcb
, Packet
);
2564 case ISCSI_OPCODE_R2T
:
2565 Status
= IScsiOnR2TRcvd (Pdu
, Tcb
, Lun
, Packet
);
2568 case ISCSI_OPCODE_SCSI_RSP
:
2569 Status
= IScsiOnScsiRspRcvd (Pdu
, Tcb
, Packet
);
2572 case ISCSI_OPCODE_NOP_IN
:
2573 Status
= IScsiOnNopInRcvd (Pdu
, Tcb
);
2576 case ISCSI_OPCODE_VENDOR_T0
:
2577 case ISCSI_OPCODE_VENDOR_T1
:
2578 case ISCSI_OPCODE_VENDOR_T2
:
2580 // These messages are vendor specific, skip them.
2585 Status
= EFI_PROTOCOL_ERROR
;
2591 if (EFI_ERROR (Status
)) {
2598 if (TimeoutEvent
!= NULL
) {
2599 gBS
->SetTimer (TimeoutEvent
, TimerCancel
, 0);
2606 if ((Status
!= EFI_SUCCESS
) && (Status
!= EFI_NOT_READY
)) {
2608 // Reinstate the session.
2610 if (EFI_ERROR (IScsiSessionReinstatement (Private
))) {
2611 Status
= EFI_DEVICE_ERROR
;
2619 Reinstate the session on some error.
2621 @param[in, out] Private The iSCSI driver data.
2623 @retval EFI_SUCCES The session is reinstated from some error.
2624 @retval Other Reinstatement failed.
2627 IScsiSessionReinstatement (
2628 IN OUT ISCSI_DRIVER_DATA
*Private
2631 ISCSI_SESSION
*Session
;
2634 Session
= &Private
->Session
;
2635 ASSERT (Session
->State
== SESSION_STATE_LOGGED_IN
);
2638 // Abort the session and re-init it.
2640 IScsiSessionAbort (Session
);
2641 IScsiSessionInit (Session
, TRUE
);
2646 Status
= IScsiSessionLogin (Private
);
2652 Initialize some session parameters before login.
2654 @param[in, out] Session The iSCSI session.
2655 @param[in] Recovery Whether the request is from a fresh new start or recovery.
2659 IN OUT ISCSI_SESSION
*Session
,
2666 Session
->Signature
= ISCSI_SESSION_SIGNATURE
;
2667 Session
->State
= SESSION_STATE_FREE
;
2669 Random
= NET_RANDOM (NetRandomInitSeed ());
2671 Session
->Isid
[0] = ISID_BYTE_0
;
2672 Session
->Isid
[1] = ISID_BYTE_1
;
2673 Session
->Isid
[2] = ISID_BYTE_2
;
2674 Session
->Isid
[3] = ISID_BYTE_3
;
2675 Session
->Isid
[4] = (UINT8
) Random
;
2676 Session
->Isid
[5] = (UINT8
) (Random
>> 8);
2678 InitializeListHead (&Session
->Conns
);
2679 InitializeListHead (&Session
->TcbList
);
2685 Session
->InitiatorTaskTag
= 1;
2686 Session
->NextCid
= 1;
2688 Session
->TargetPortalGroupTag
= 0;
2689 Session
->MaxConnections
= ISCSI_MAX_CONNS_PER_SESSION
;
2690 Session
->InitialR2T
= FALSE
;
2691 Session
->ImmediateData
= TRUE
;
2692 Session
->MaxBurstLength
= 262144;
2693 Session
->FirstBurstLength
= MAX_RECV_DATA_SEG_LEN_IN_FFP
;
2694 Session
->DefaultTime2Wait
= 2;
2695 Session
->DefaultTime2Retain
= 20;
2696 Session
->MaxOutstandingR2T
= DEFAULT_MAX_OUTSTANDING_R2T
;
2697 Session
->DataPDUInOrder
= TRUE
;
2698 Session
->DataSequenceInOrder
= TRUE
;
2699 Session
->ErrorRecoveryLevel
= 0;
2703 Abort the iSCSI session, that is, reset all the connection and free the
2706 @param[in, out] Session The iSCSI session.
2708 @retval EFI_SUCCES The session is aborted.
2712 IN OUT ISCSI_SESSION
*Session
2715 ISCSI_DRIVER_DATA
*Private
;
2716 ISCSI_CONNECTION
*Conn
;
2718 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
2722 ASSERT (!IsListEmpty (&Session
->Conns
));
2724 Private
= ISCSI_DRIVER_DATA_FROM_SESSION (Session
);
2726 while (!IsListEmpty (&Session
->Conns
)) {
2727 Conn
= NET_LIST_USER_STRUCT_S (
2728 Session
->Conns
.ForwardLink
,
2731 ISCSI_CONNECTION_SIGNATURE
2734 gBS
->CloseProtocol (
2735 Conn
->Tcp4Io
.Handle
,
2736 &gEfiTcp4ProtocolGuid
,
2738 Private
->ExtScsiPassThruHandle
2741 IScsiConnReset (Conn
);
2743 IScsiDetatchConnection (Conn
);
2744 IScsiDestroyConnection (Conn
);
2747 Session
->State
= SESSION_STATE_FAILED
;