2 The implementation of iSCSI protocol based on RFC3720.
4 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
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
)) {
222 NetbufQueInit (&Conn
->RspQue
);
225 // set the default connection-only parameters
227 Conn
->MaxRecvDataSegmentLength
= DEFAULT_MAX_RECV_DATA_SEG_LEN
;
228 Conn
->HeaderDigest
= IScsiDigestNone
;
229 Conn
->DataDigest
= IScsiDigestNone
;
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
);
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
);
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 EFI_NO_MEDIA There was a media error.
280 @retval Others Other errors as indicated.
285 IN ISCSI_DRIVER_DATA
*Private
289 ISCSI_SESSION
*Session
;
290 ISCSI_CONNECTION
*Conn
;
291 EFI_TCP4_PROTOCOL
*Tcp4
;
292 EFI_STATUS MediaStatus
;
294 Session
= &Private
->Session
;
297 // Check media status before session login
299 MediaStatus
= EFI_SUCCESS
;
300 NetLibDetectMediaWaitTimeout (Private
->Controller
, ISCSI_CHECK_MEDIA_LOGIN_WAITING_TIME
, &MediaStatus
);
301 if (MediaStatus
!= EFI_SUCCESS
) {
306 // Set session identifier
308 CopyMem (Session
->Isid
, Session
->ConfigData
.NvData
.IsId
, 6);
311 // Create a connection for the session.
313 Conn
= IScsiCreateConnection (Private
, Session
);
315 return EFI_OUT_OF_RESOURCES
;
318 IScsiAttatchConnection (Session
, Conn
);
321 // Login througth the newly created connection.
323 Status
= IScsiConnLogin (Conn
);
324 if (EFI_ERROR (Status
)) {
325 IScsiConnReset (Conn
);
326 IScsiDetatchConnection (Conn
);
327 IScsiDestroyConnection (Conn
);
329 Session
->State
= SESSION_STATE_LOGGED_IN
;
333 &gEfiTcp4ProtocolGuid
,
336 Private
->ExtScsiPassThruHandle
,
337 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
345 Build and send the iSCSI login request to the iSCSI target according to
346 the current login stage.
348 @param[in] Conn The connection in the iSCSI login phase.
350 @retval EFI_SUCCESS The iSCSI login request PDU is built and sent on this
352 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
353 @retval EFI_DEVICE_ERROR Some kind of device error happened.
357 IN ISCSI_CONNECTION
*Conn
364 // build the Login Request PDU
366 Pdu
= IScsiPrepareLoginReq (Conn
);
368 return EFI_DEVICE_ERROR
;
371 // Send it to the iSCSI target.
373 Status
= Tcp4IoTransmit (&Conn
->Tcp4Io
, Pdu
);
381 Receive and process the iSCSI login response.
383 @param[in] Conn The connection in the iSCSI login phase.
385 @retval EFI_SUCCESS The iSCSI login response PDU is received and processed.
386 @retval Others Other errors as indicated.
389 IScsiReceiveLoginRsp (
390 IN ISCSI_CONNECTION
*Conn
399 // Receive the iSCSI login response.
401 Status
= IScsiReceivePdu (Conn
, &Pdu
, NULL
, FALSE
, FALSE
, NULL
);
402 if (EFI_ERROR (Status
)) {
406 // A Login Response is received, process it.
408 ASSERT (Pdu
!= NULL
);
409 Status
= IScsiProcessLoginRsp (Conn
, Pdu
);
417 Add an iSCSI key-value pair as a string into the data segment of the Login Request PDU.
418 The DataSegmentLength and the actual size of the net buffer containing this PDU will be
421 @param[in, out] Pdu The iSCSI PDU whose data segment the key-value pair will
423 @param[in] Key The key name string.
424 @param[in] Value The value string.
426 @retval EFI_SUCCESS The key-valu pair is added to the PDU's datasegment and
427 the correspondence length fields are updated.
428 @retval EFI_OUT_OF_RESOURCES There is not enough space in the PDU to add the key-value
432 IScsiAddKeyValuePair (
442 ISCSI_LOGIN_REQUEST
*LoginReq
;
445 LoginReq
= (ISCSI_LOGIN_REQUEST
*) NetbufGetByte (Pdu
, 0, NULL
);
446 if (LoginReq
== NULL
) {
447 return EFI_PROTOCOL_ERROR
;
449 DataSegLen
= NTOH24 (LoginReq
->DataSegmentLength
);
451 KeyLen
= (UINT32
) AsciiStrLen (Key
);
452 ValueLen
= (UINT32
) AsciiStrLen (Value
);
455 // 1 byte for the key value separator '=' and 1 byte for the null
456 // delimiter after the value.
458 TotalLen
= KeyLen
+ 1 + ValueLen
+ 1;
461 // Allocate the space for the key-value pair.
463 Data
= (CHAR8
*)NetbufAllocSpace (Pdu
, TotalLen
, NET_BUF_TAIL
);
465 return EFI_OUT_OF_RESOURCES
;
470 CopyMem (Data
, Key
, KeyLen
);
479 CopyMem (Data
, Value
, ValueLen
);
485 // update the DataSegmentLength
487 ISCSI_SET_DATASEG_LEN (LoginReq
, DataSegLen
+ TotalLen
);
493 Prepare the iSCSI login request to be sent according to the current login status.
495 @param[in, out] Conn The connection in the iSCSI login phase.
497 @return The pointer to the net buffer containing the iSCSI login request built.
498 @retval Others Other errors as indicated.
501 IScsiPrepareLoginReq (
502 IN OUT ISCSI_CONNECTION
*Conn
505 ISCSI_SESSION
*Session
;
507 ISCSI_LOGIN_REQUEST
*LoginReq
;
510 Session
= Conn
->Session
;
512 Nbuf
= NetbufAlloc (sizeof (ISCSI_LOGIN_REQUEST
) + DEFAULT_MAX_RECV_DATA_SEG_LEN
);
517 LoginReq
= (ISCSI_LOGIN_REQUEST
*) NetbufAllocSpace (Nbuf
, sizeof (ISCSI_LOGIN_REQUEST
), NET_BUF_TAIL
);
518 ASSERT (LoginReq
!= NULL
);
519 ZeroMem (LoginReq
, sizeof (ISCSI_LOGIN_REQUEST
));
522 // Init the login request pdu
524 ISCSI_SET_OPCODE (LoginReq
, ISCSI_OPCODE_LOGIN_REQ
, ISCSI_REQ_IMMEDIATE
);
525 ISCSI_SET_STAGES (LoginReq
, Conn
->CurrentStage
, Conn
->NextStage
);
526 LoginReq
->VersionMax
= ISCSI_VERSION_MAX
;
527 LoginReq
->VersionMin
= ISCSI_VERSION_MIN
;
528 LoginReq
->Tsih
= HTONS (Session
->Tsih
);
529 LoginReq
->InitiatorTaskTag
= HTONL (Session
->InitiatorTaskTag
);
530 LoginReq
->Cid
= HTONS (Conn
->Cid
);
531 LoginReq
->CmdSN
= HTONL (Session
->CmdSN
);
534 // For the first Login Request on a coonection this is ExpStatSN for the
535 // old connection and this field is only valid if the Login Request restarts
537 // For subsequent Login Requests it is used to acknowledge the Login Responses
538 // with their increasing StatSN values.
540 LoginReq
->ExpStatSN
= HTONL (Conn
->ExpStatSN
);
541 CopyMem (LoginReq
->Isid
, Session
->Isid
, sizeof (LoginReq
->Isid
));
543 if (Conn
->PartialRspRcvd
) {
545 // A partial response, initiator must send an empty Login Request.
550 switch (Conn
->CurrentStage
) {
551 case ISCSI_SECURITY_NEGOTIATION
:
552 Status
= IScsiCHAPToSendReq (Conn
, Nbuf
);
555 case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
:
556 Status
= IScsiFillOpParams (Conn
, Nbuf
);
557 ISCSI_SET_FLAG (LoginReq
, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT
);
562 // something error happens...
564 Status
= EFI_DEVICE_ERROR
;
568 if (EFI_ERROR (Status
)) {
573 // Pad the data segment if needed.
575 IScsiPadSegment (Nbuf
, ISCSI_GET_DATASEG_LEN (LoginReq
));
577 // Check whether we will issue the stage transition signal?
579 Conn
->TransitInitiated
= (BOOLEAN
) ISCSI_FLAG_ON (LoginReq
, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT
);
586 Process the iSCSI Login Response.
588 @param[in, out] Conn The connection on which the iSCSI login response is received.
589 @param[in, out] Pdu The iSCSI login response PDU.
591 @retval EFI_SUCCESS The iSCSI login response PDU is processed and all check are passed.
592 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
593 @retval EFI_MEDIA_CHANGED Target is redirected.
594 @retval Others Other errors as indicated.
597 IScsiProcessLoginRsp (
598 IN OUT ISCSI_CONNECTION
*Conn
,
603 ISCSI_SESSION
*Session
;
604 ISCSI_LOGIN_RESPONSE
*LoginRsp
;
612 Session
= Conn
->Session
;
614 LoginRsp
= (ISCSI_LOGIN_RESPONSE
*) NetbufGetByte (Pdu
, 0, NULL
);
615 if (LoginRsp
== NULL
) {
616 return EFI_PROTOCOL_ERROR
;
618 if (!ISCSI_CHECK_OPCODE (LoginRsp
, ISCSI_OPCODE_LOGIN_RSP
)) {
620 // It's not a Login Response
622 return EFI_PROTOCOL_ERROR
;
625 // Get the data segment if any.
627 DataSegLen
= ISCSI_GET_DATASEG_LEN (LoginRsp
);
628 if (DataSegLen
!= 0) {
629 DataSeg
= NetbufGetByte (Pdu
, sizeof (ISCSI_LOGIN_RESPONSE
), NULL
);
634 // Check the status class in the login response PDU.
636 switch (LoginRsp
->StatusClass
) {
637 case ISCSI_LOGIN_STATUS_SUCCESS
:
639 // Just break here, the response and the data segment will be processed later.
643 case ISCSI_LOGIN_STATUS_REDIRECTION
:
645 // The target may be moved to a different address
647 if (DataSeg
== NULL
) {
648 return EFI_PROTOCOL_ERROR
;
651 // Process the TargetAddress key-value strings in the data segment to update the
652 // target address info.
654 Status
= IScsiUpdateTargetAddress (Session
, (CHAR8
*)DataSeg
, DataSegLen
);
655 if (EFI_ERROR (Status
)) {
659 // Session will be restarted on this error status because the Target is
660 // redirected by this Login Response.
662 return EFI_MEDIA_CHANGED
;
666 // Initiator Error, Target Error, or any other undefined error code.
668 return EFI_PROTOCOL_ERROR
;
671 // The status is sucess, extract the wanted fields from the header segment.
673 Transit
= (BOOLEAN
) ISCSI_FLAG_ON (LoginRsp
, ISCSI_LOGIN_RSP_PDU_FLAG_TRANSIT
);
674 Continue
= (BOOLEAN
) ISCSI_FLAG_ON (LoginRsp
, ISCSI_LOGIN_RSP_PDU_FLAG_CONTINUE
);
676 CurrentStage
= (UINT8
) ISCSI_GET_CURRENT_STAGE (LoginRsp
);
677 NextStage
= (UINT8
) ISCSI_GET_NEXT_STAGE (LoginRsp
);
679 LoginRsp
->InitiatorTaskTag
= NTOHL (LoginRsp
->InitiatorTaskTag
);
681 if ((Transit
&& Continue
) ||
682 (CurrentStage
!= Conn
->CurrentStage
) ||
683 (!Conn
->TransitInitiated
&& Transit
) ||
684 (Transit
&& (NextStage
!= Conn
->NextStage
)) ||
685 (CompareMem (Session
->Isid
, LoginRsp
->Isid
, sizeof (LoginRsp
->Isid
)) != 0) ||
686 (LoginRsp
->InitiatorTaskTag
!= Session
->InitiatorTaskTag
)
689 // A Login Response with the C bit set to 1 MUST have the T bit set to 0;
690 // The CSG in the Login Response MUST be the same with the I-end of this connection;
691 // The T bit can't be 1 if the last Login Response sent by the initiator doesn't
692 // initiate the transistion;
693 // The NSG MUST be the same with the I-end of this connection if Transit is required.
694 // The ISID in the Login Response MUST be the same with this session.
696 return EFI_PROTOCOL_ERROR
;
699 LoginRsp
->StatSN
= NTOHL (LoginRsp
->StatSN
);
700 LoginRsp
->ExpCmdSN
= NTOHL (LoginRsp
->ExpCmdSN
);
701 LoginRsp
->MaxCmdSN
= NTOHL (LoginRsp
->MaxCmdSN
);
703 if ((Conn
->CurrentStage
== ISCSI_SECURITY_NEGOTIATION
) && (Conn
->CHAPStep
== ISCSI_CHAP_INITIAL
)) {
705 // If the Login Request is a leading Login Request, the target MUST use
706 // the value presented in CmdSN as the target value for ExpCmdSN.
708 if ((Session
->State
== SESSION_STATE_FREE
) && (Session
->CmdSN
!= LoginRsp
->ExpCmdSN
)) {
709 return EFI_PROTOCOL_ERROR
;
713 // It's the initial Login Response, initialize the local ExpStatSN, MaxCmdSN
716 Conn
->ExpStatSN
= LoginRsp
->StatSN
+ 1;
717 Session
->MaxCmdSN
= LoginRsp
->MaxCmdSN
;
718 Session
->ExpCmdSN
= LoginRsp
->ExpCmdSN
;
721 // Check the StatSN of this PDU
723 Status
= IScsiCheckSN (&Conn
->ExpStatSN
, LoginRsp
->StatSN
);
724 if (!EFI_ERROR (Status
)) {
726 // Update the MaxCmdSN and ExpCmdSN
728 IScsiUpdateCmdSN (Session
, LoginRsp
->MaxCmdSN
, LoginRsp
->ExpCmdSN
);
734 // Trim off the header segment.
736 NetbufTrim (Pdu
, sizeof (ISCSI_LOGIN_RESPONSE
), NET_BUF_HEAD
);
739 // Queue this login response first in case it's a partial response so that
740 // later when the full response list is received we can combine these scattered
741 // responses' data segment and then process it.
744 NetbufQueAppend (&Conn
->RspQue
, Pdu
);
746 Conn
->PartialRspRcvd
= Continue
;
749 // It's a partial response, have to wait for another or more Request/Response
750 // conversations to get the full response.
755 switch (CurrentStage
) {
756 case ISCSI_SECURITY_NEGOTIATION
:
758 // In security negotiation stage, let CHAP module handle it.
760 Status
= IScsiCHAPOnRspReceived (Conn
);
763 case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
:
765 // Response received with negotiation resonse on iSCSI parameters, check them.
767 Status
= IScsiCheckOpParams (Conn
);
772 // Should never get here.
774 Status
= EFI_PROTOCOL_ERROR
;
778 if (Transit
&& (Status
== EFI_SUCCESS
)) {
780 // Do the state transition.
782 Conn
->CurrentStage
= Conn
->NextStage
;
784 if (Conn
->CurrentStage
== ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
) {
785 Conn
->NextStage
= ISCSI_FULL_FEATURE_PHASE
;
788 // CurrentStage is iSCSI Full Feature, it's the Login-Final Response,
789 // get the TSIH from the Login Response.
791 Session
->Tsih
= NTOHS (LoginRsp
->Tsih
);
795 // Flush the response(s) received.
797 NetbufQueFlush (&Conn
->RspQue
);
803 Updated the target information according the data received in the iSCSI
804 login response with an target redirection status.
806 @param[in, out] Session The iSCSI session.
807 @param[in] Data The data segment which should contain the
808 TargetAddress key-value list.
809 @param[in] Len Length of the data.
811 @retval EFI_SUCCESS The target address is updated.
812 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
813 @retval EFI_NOT_FOUND The TargetAddress key is not found.
814 @retval Others Other errors as indicated.
817 IScsiUpdateTargetAddress (
818 IN OUT ISCSI_SESSION
*Session
,
823 LIST_ENTRY
*KeyValueList
;
824 CHAR8
*TargetAddress
;
829 KeyValueList
= IScsiBuildKeyValueList (Data
, Len
);
830 if (KeyValueList
== NULL
) {
831 return EFI_OUT_OF_RESOURCES
;
834 Status
= EFI_NOT_FOUND
;
837 TargetAddress
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_ADDRESS
);
838 if (TargetAddress
== NULL
) {
842 if (!NET_IS_DIGIT (TargetAddress
[0])) {
844 // The domainname of the target may be presented in three formats: a DNS host name,
845 // a dotted-decimal IPv4 address, or a bracketed IPv6 address. Only accept dotted
851 IpStr
= TargetAddress
;
853 while ((*TargetAddress
!= 0) && (*TargetAddress
!= ':') && (*TargetAddress
!= ',')) {
855 // NULL, ':' or ',' ends the IPv4 string.
860 if (*TargetAddress
== ',') {
862 // Comma and the portal group tag MUST be ommitted if the TargetAddress is sent
863 // as the result of a redirection.
866 } else if (*TargetAddress
== ':') {
867 *TargetAddress
= '\0';
871 Number
= AsciiStrDecimalToUintn (TargetAddress
);
872 if (Number
> 0xFFFF) {
875 Session
->ConfigData
.NvData
.TargetPort
= (UINT16
) Number
;
879 // The string only contains the IPv4 address. Use the well known port.
881 Session
->ConfigData
.NvData
.TargetPort
= ISCSI_WELL_KNOWN_PORT
;
884 // Update the target IP address.
886 Status
= IScsiAsciiStrToIp (IpStr
, &Session
->ConfigData
.NvData
.TargetIp
);
887 if (EFI_ERROR (Status
)) {
894 IScsiFreeKeyValueList (KeyValueList
);
900 The callback function to free the net buffer list.
902 @param[in] Arg The opaque parameter.
910 ASSERT (Arg
!= NULL
);
912 NetbufFreeList ((LIST_ENTRY
*) Arg
);
917 The callback function called in NetBufFree, it does nothing.
919 @param[in] Arg The opaque parameter.
930 Receive an iSCSI response PDU. An iSCSI response PDU contains an iSCSI PDU header and
931 an optional data segment. The two parts will be put into two blocks of buffers in the
932 net buffer. The digest check will be conducted in this function if needed and the digests
933 will be trimmed from the PDU buffer.
935 @param[in] Conn The iSCSI connection to receive data from.
936 @param[out] Pdu The received iSCSI pdu.
937 @param[in] Context The context used to describe information on the caller provided
938 buffer to receive data segment of the iSCSI pdu, it's optional.
939 @param[in] HeaderDigest Whether there will be header digest received.
940 @param[in] DataDigest Whether there will be data digest.
941 @param[in] TimeoutEvent The timeout event, it's optional.
943 @retval EFI_SUCCESS An iSCSI pdu is received.
944 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
945 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
946 @retval Others Other errors as indicated.
950 IN ISCSI_CONNECTION
*Conn
,
952 IN ISCSI_IN_BUFFER_CONTEXT
*Context
, OPTIONAL
953 IN BOOLEAN HeaderDigest
,
954 IN BOOLEAN DataDigest
,
955 IN EFI_EVENT TimeoutEvent OPTIONAL
958 LIST_ENTRY
*NbufList
;
965 NET_FRAGMENT Fragment
[2];
966 UINT32 FragmentCount
;
968 UINT32 PadAndCRC32
[2];
970 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
971 if (NbufList
== NULL
) {
972 return EFI_OUT_OF_RESOURCES
;
975 InitializeListHead (NbufList
);
978 // The header digest will be received together with the PDU header if exists.
980 Len
= sizeof (ISCSI_BASIC_HEADER
) + (HeaderDigest
? sizeof (UINT32
) : 0);
981 PduHdr
= NetbufAlloc (Len
);
982 if (PduHdr
== NULL
) {
984 return EFI_OUT_OF_RESOURCES
;
987 Header
= NetbufAllocSpace (PduHdr
, Len
, NET_BUF_TAIL
);
988 ASSERT (Header
!= NULL
);
989 InsertTailList (NbufList
, &PduHdr
->List
);
992 // First step, receive the BHS of the PDU.
994 Status
= Tcp4IoReceive (&Conn
->Tcp4Io
, PduHdr
, FALSE
, TimeoutEvent
);
995 if (EFI_ERROR (Status
)) {
1001 // TODO: check the header-digest.
1004 // Trim off the digest.
1006 NetbufTrim (PduHdr
, sizeof (UINT32
), NET_BUF_TAIL
);
1009 Len
= ISCSI_GET_DATASEG_LEN (Header
);
1012 // No data segment.Form the pdu from a list of pdu segments.
1014 *Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1016 Status
= EFI_OUT_OF_RESOURCES
;
1022 // Get the length of the padding bytes of the data segment.
1024 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1026 switch (ISCSI_GET_OPCODE (Header
)) {
1027 case ISCSI_OPCODE_SCSI_DATA_IN
:
1029 // Try to use the buffer described by Context if the PDU is an
1030 // iSCSI SCSI data in pdu so as to reduce memory copy overhead.
1032 InDataOffset
= ISCSI_GET_BUFFER_OFFSET (Header
);
1033 if ((Context
== NULL
) || ((InDataOffset
+ Len
) > Context
->InDataLen
)) {
1034 Status
= EFI_PROTOCOL_ERROR
;
1038 Fragment
[0].Len
= Len
;
1039 Fragment
[0].Bulk
= Context
->InData
+ InDataOffset
;
1041 if (DataDigest
|| (PadLen
!= 0)) {
1043 // The data segment is padded, use two fragments to receive it.
1044 // The first to receive the useful data. The second to receive the padding.
1046 Fragment
[1].Len
= PadLen
+ (DataDigest
? sizeof (UINT32
) : 0);
1047 Fragment
[1].Bulk
= (UINT8
*)PadAndCRC32
+ (4 - PadLen
);
1053 DataSeg
= NetbufFromExt (&Fragment
[0], FragmentCount
, 0, 0, IScsiNbufExtFree
, NULL
);
1054 if (DataSeg
== NULL
) {
1055 Status
= EFI_OUT_OF_RESOURCES
;
1061 case ISCSI_OPCODE_SCSI_RSP
:
1062 case ISCSI_OPCODE_NOP_IN
:
1063 case ISCSI_OPCODE_LOGIN_RSP
:
1064 case ISCSI_OPCODE_TEXT_RSP
:
1065 case ISCSI_OPCODE_ASYNC_MSG
:
1066 case ISCSI_OPCODE_REJECT
:
1067 case ISCSI_OPCODE_VENDOR_T0
:
1068 case ISCSI_OPCODE_VENDOR_T1
:
1069 case ISCSI_OPCODE_VENDOR_T2
:
1071 // Allocate buffer to receive the data segment.
1073 Len
+= PadLen
+ (DataDigest
? sizeof (UINT32
) : 0);
1074 DataSeg
= NetbufAlloc (Len
);
1075 if (DataSeg
== NULL
) {
1076 Status
= EFI_OUT_OF_RESOURCES
;
1080 NetbufAllocSpace (DataSeg
, Len
, NET_BUF_TAIL
);
1084 Status
= EFI_PROTOCOL_ERROR
;
1088 InsertTailList (NbufList
, &DataSeg
->List
);
1091 // Receive the data segment with the data digest if any.
1093 Status
= Tcp4IoReceive (&Conn
->Tcp4Io
, DataSeg
, FALSE
, TimeoutEvent
);
1094 if (EFI_ERROR (Status
)) {
1100 // TODO: Check the data digest.
1102 NetbufTrim (DataSeg
, sizeof (UINT32
), NET_BUF_TAIL
);
1107 // Trim off the padding bytes in the data segment.
1109 NetbufTrim (DataSeg
, PadLen
, NET_BUF_TAIL
);
1113 // Form the pdu from a list of pdu segments.
1115 *Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1117 Status
= EFI_OUT_OF_RESOURCES
;
1122 if (EFI_ERROR (Status
)) {
1124 // Free the Nbufs in this NbufList and the NbufList itself.
1126 IScsiFreeNbufList (NbufList
);
1133 Check and get the result of the prameter negotiation.
1135 @param[in, out] Conn The connection in iSCSI login.
1137 @retval EFI_SUCCESS The parmeter check is passed and negotiation is finished.
1138 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
1139 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1142 IScsiCheckOpParams (
1143 IN OUT ISCSI_CONNECTION
*Conn
1147 LIST_ENTRY
*KeyValueList
;
1150 ISCSI_SESSION
*Session
;
1154 ASSERT (Conn
->RspQue
.BufNum
!= 0);
1156 Session
= Conn
->Session
;
1158 Len
= Conn
->RspQue
.BufSize
;
1159 Data
= AllocatePool (Len
);
1161 return EFI_OUT_OF_RESOURCES
;
1164 NetbufQueCopy (&Conn
->RspQue
, 0, Len
, (UINT8
*) Data
);
1166 Status
= EFI_PROTOCOL_ERROR
;
1169 // Extract the Key-Value pairs into a list.
1171 KeyValueList
= IScsiBuildKeyValueList (Data
, Len
);
1172 if (KeyValueList
== NULL
) {
1179 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_HEADER_DIGEST
);
1180 if (Value
== NULL
) {
1184 if (AsciiStrCmp (Value
, "CRC32") == 0) {
1185 if (Conn
->HeaderDigest
!= IScsiDigestCRC32
) {
1188 } else if (AsciiStrCmp (Value
, ISCSI_KEY_VALUE_NONE
) == 0) {
1189 Conn
->HeaderDigest
= IScsiDigestNone
;
1196 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_DIGEST
);
1197 if (Value
== NULL
) {
1201 if (AsciiStrCmp (Value
, "CRC32") == 0) {
1202 if (Conn
->DataDigest
!= IScsiDigestCRC32
) {
1205 } else if (AsciiStrCmp (Value
, ISCSI_KEY_VALUE_NONE
) == 0) {
1206 Conn
->DataDigest
= IScsiDigestNone
;
1211 // ErrorRecoveryLevel, result fuction is Minimum.
1213 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_ERROR_RECOVERY_LEVEL
);
1214 if (Value
== NULL
) {
1218 NumericValue
= AsciiStrDecimalToUintn (Value
);
1219 if (NumericValue
> 2) {
1223 Session
->ErrorRecoveryLevel
= (UINT8
) MIN (Session
->ErrorRecoveryLevel
, NumericValue
);
1226 // InitialR2T, result function is OR.
1228 if (!Session
->InitialR2T
) {
1229 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_INITIAL_R2T
);
1230 if (Value
== NULL
) {
1234 Session
->InitialR2T
= (BOOLEAN
) (AsciiStrCmp (Value
, "Yes") == 0);
1238 // ImmediateData, result function is AND.
1240 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_IMMEDIATE_DATA
);
1241 if (Value
== NULL
) {
1245 Session
->ImmediateData
= (BOOLEAN
) (Session
->ImmediateData
&& (BOOLEAN
) (AsciiStrCmp (Value
, "Yes") == 0));
1248 // MaxRecvDataSegmentLength is declarative.
1250 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
);
1251 if (Value
!= NULL
) {
1252 Conn
->MaxRecvDataSegmentLength
= (UINT32
) AsciiStrDecimalToUintn (Value
);
1255 // MaxBurstLength, result funtion is Mininum.
1257 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_BURST_LENGTH
);
1258 if (Value
== NULL
) {
1262 NumericValue
= AsciiStrDecimalToUintn (Value
);
1263 Session
->MaxBurstLength
= (UINT32
) MIN (Session
->MaxBurstLength
, NumericValue
);
1266 // FirstBurstLength, result function is Minimum. Irrelevant when InitialR2T=Yes and
1267 // ImmediateData=No.
1268 // This Key/Value is negotiation type.
1270 if (!(Session
->InitialR2T
&& !Session
->ImmediateData
)) {
1271 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_FIRST_BURST_LENGTH
);
1272 if (Value
== NULL
) {
1276 NumericValue
= AsciiStrDecimalToUintn (Value
);
1277 Session
->FirstBurstLength
= (UINT32
) MIN (Session
->FirstBurstLength
, NumericValue
);
1281 // MaxConnections, result function is Minimum.
1283 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_CONNECTIONS
);
1284 if (Value
== NULL
) {
1288 NumericValue
= AsciiStrDecimalToUintn (Value
);
1289 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1293 Session
->MaxConnections
= (UINT32
) MIN (Session
->MaxConnections
, NumericValue
);
1296 // DataPDUInOrder, result function is OR.
1298 if (!Session
->DataPDUInOrder
) {
1299 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_PDU_IN_ORDER
);
1300 if (Value
== NULL
) {
1304 Session
->DataPDUInOrder
= (BOOLEAN
) (AsciiStrCmp (Value
, "Yes") == 0);
1308 // DataSequenceInorder, result function is OR.
1310 if (!Session
->DataSequenceInOrder
) {
1311 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
);
1312 if (Value
== NULL
) {
1316 Session
->DataSequenceInOrder
= (BOOLEAN
) (AsciiStrCmp (Value
, "Yes") == 0);
1320 // DefaultTime2Wait, result function is Maximum.
1322 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2WAIT
);
1323 if (Value
== NULL
) {
1327 NumericValue
= AsciiStrDecimalToUintn (Value
);
1328 if (NumericValue
== 0) {
1329 Session
->DefaultTime2Wait
= 0;
1330 } else if (NumericValue
> 3600) {
1333 Session
->DefaultTime2Wait
= (UINT32
) MAX (Session
->DefaultTime2Wait
, NumericValue
);
1336 // DefaultTime2Retain, result function is Minimum.
1338 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2RETAIN
);
1339 if (Value
== NULL
) {
1343 NumericValue
= AsciiStrDecimalToUintn (Value
);
1344 if (NumericValue
== 0) {
1345 Session
->DefaultTime2Retain
= 0;
1346 } else if (NumericValue
> 3600) {
1349 Session
->DefaultTime2Retain
= (UINT32
) MIN (Session
->DefaultTime2Retain
, NumericValue
);
1352 // MaxOutstandingR2T, result function is Minimum.
1354 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_OUTSTANDING_R2T
);
1355 if (Value
== NULL
) {
1359 NumericValue
= AsciiStrDecimalToUintn (Value
);
1360 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1364 Session
->MaxOutstandingR2T
= (UINT16
) MIN (Session
->MaxOutstandingR2T
, NumericValue
);
1367 // Remove declarative key-value pairs, if any.
1369 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_SESSION_TYPE
);
1370 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_ALIAS
);
1371 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_PORTAL_GROUP_TAG
);
1373 // Remove the key-value that may not needed for result function is OR.
1375 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_INITIAL_R2T
);
1376 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_PDU_IN_ORDER
);
1377 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
);
1380 // Remove irrelevant parameter, if any.
1382 if (Session
->InitialR2T
&& !Session
->ImmediateData
) {
1383 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_FIRST_BURST_LENGTH
);
1386 if (IsListEmpty (KeyValueList
)) {
1388 // Succeed if no more keys in the list.
1390 Status
= EFI_SUCCESS
;
1395 IScsiFreeKeyValueList (KeyValueList
);
1403 Fill the oprational prameters.
1405 @param[in] Conn The connection in iSCSI login.
1406 @param[in, out] Pdu The iSCSI login request PDU to fill the parameters.
1408 @retval EFI_SUCCESS The parmeters are filled into the iSCSI login request PDU.
1412 IN ISCSI_CONNECTION
*Conn
,
1416 ISCSI_SESSION
*Session
;
1419 Session
= Conn
->Session
;
1421 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->HeaderDigest
== IScsiDigestCRC32
) ? "None,CRC32" : "None");
1422 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_HEADER_DIGEST
, Value
);
1424 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->DataDigest
== IScsiDigestCRC32
) ? "None,CRC32" : "None");
1425 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_DIGEST
, Value
);
1427 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->ErrorRecoveryLevel
);
1428 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_ERROR_RECOVERY_LEVEL
, Value
);
1430 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->InitialR2T
? "Yes" : "No");
1431 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_INITIAL_R2T
, Value
);
1433 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->ImmediateData
? "Yes" : "No");
1434 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_IMMEDIATE_DATA
, Value
);
1436 AsciiSPrint (Value
, sizeof (Value
), "%d", MAX_RECV_DATA_SEG_LEN_IN_FFP
);
1437 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
, Value
);
1439 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxBurstLength
);
1440 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_BURST_LENGTH
, Value
);
1442 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->FirstBurstLength
);
1443 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_FIRST_BURST_LENGTH
, Value
);
1445 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxConnections
);
1446 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_CONNECTIONS
, Value
);
1448 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataPDUInOrder
? "Yes" : "No");
1449 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_PDU_IN_ORDER
, Value
);
1451 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataSequenceInOrder
? "Yes" : "No");
1452 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
, Value
);
1454 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Wait
);
1455 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2WAIT
, Value
);
1457 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Retain
);
1458 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2RETAIN
, Value
);
1460 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxOutstandingR2T
);
1461 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_OUTSTANDING_R2T
, Value
);
1467 Pad the iSCSI AHS or data segment to an integer number of 4 byte words.
1469 @param[in, out] Pdu The iSCSI pdu which contains segments to pad.
1470 @param[in] Len The length of the last semgnet in the PDU.
1472 @retval EFI_SUCCESS The segment is padded or no need to pad it.
1473 @retval EFI_OUT_OF_RESOURCES There is not enough remaining free space to add the
1478 IN OUT NET_BUF
*Pdu
,
1485 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1488 Data
= NetbufAllocSpace (Pdu
, PadLen
, NET_BUF_TAIL
);
1490 return EFI_OUT_OF_RESOURCES
;
1493 ZeroMem (Data
, PadLen
);
1500 Build a key-value list from the data segment.
1502 @param[in] Data The data segment containing the key-value pairs.
1503 @param[in] Len Length of the data segment.
1505 @return The key-value list.
1506 @retval NULL Other errors as indicated.
1509 IScsiBuildKeyValueList (
1514 LIST_ENTRY
*ListHead
;
1515 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1517 ListHead
= AllocatePool (sizeof (LIST_ENTRY
));
1518 if (ListHead
== NULL
) {
1522 InitializeListHead (ListHead
);
1525 KeyValuePair
= AllocatePool (sizeof (ISCSI_KEY_VALUE_PAIR
));
1526 if (KeyValuePair
== NULL
) {
1530 InitializeListHead (&KeyValuePair
->List
);
1532 KeyValuePair
->Key
= Data
;
1534 while ((Len
> 0) && (*Data
!= '=')) {
1545 FreePool (KeyValuePair
);
1549 KeyValuePair
->Value
= Data
;
1551 InsertTailList (ListHead
, &KeyValuePair
->List
);;
1553 Data
+= AsciiStrLen (KeyValuePair
->Value
) + 1;
1554 Len
-= (UINT32
) AsciiStrLen (KeyValuePair
->Value
) + 1;
1561 IScsiFreeKeyValueList (ListHead
);
1567 Get the value string by the key name from the key-value list. If found,
1568 the key-value entry will be removed from the list.
1570 @param[in, out] KeyValueList The key-value list.
1571 @param[in] Key The key name to find.
1573 @return The value string.
1576 IScsiGetValueByKeyFromList (
1577 IN OUT LIST_ENTRY
*KeyValueList
,
1582 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1587 NET_LIST_FOR_EACH (Entry
, KeyValueList
) {
1588 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1590 if (AsciiStrCmp (KeyValuePair
->Key
, Key
) == 0) {
1591 Value
= KeyValuePair
->Value
;
1593 RemoveEntryList (&KeyValuePair
->List
);
1594 FreePool (KeyValuePair
);
1603 Free the key-value list.
1605 @param[in] KeyValueList The key-value list.
1608 IScsiFreeKeyValueList (
1609 IN LIST_ENTRY
*KeyValueList
1613 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1615 while (!IsListEmpty (KeyValueList
)) {
1616 Entry
= NetListRemoveHead (KeyValueList
);
1617 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1619 FreePool (KeyValuePair
);
1622 FreePool (KeyValueList
);
1626 Normalize the iSCSI name according to RFC.
1628 @param[in, out] Name The iSCSI name.
1629 @param[in] Len length of the iSCSI name.
1631 @retval EFI_SUCCESS The iSCSI name is valid and normalized.
1632 @retval EFI_PROTOCOL_ERROR The iSCSI name is mal-formatted or not in the IQN format.
1635 IScsiNormalizeName (
1642 for (Index
= 0; Index
< Len
; Index
++) {
1643 if (NET_IS_UPPER_CASE_CHAR (Name
[Index
])) {
1645 // Convert the upper-case characters to lower-case ones
1647 Name
[Index
] = (CHAR8
) (Name
[Index
] - 'A' + 'a');
1650 if (!NET_IS_LOWER_CASE_CHAR (Name
[Index
]) &&
1651 !NET_IS_DIGIT (Name
[Index
]) &&
1652 (Name
[Index
] != '-') &&
1653 (Name
[Index
] != '.') &&
1654 (Name
[Index
] != ':')
1657 // ASCII dash, dot, colon lower-case characters and digit characters
1660 return EFI_PROTOCOL_ERROR
;
1664 if ((Len
< 4) || (CompareMem (Name
, "iqn.", 4) != 0)) {
1666 // Only IQN format is accepted now.
1668 return EFI_PROTOCOL_ERROR
;
1675 Create an iSCSI task control block.
1677 @param[in] Conn The connection on which the task control block will be created.
1678 @param[out] Tcb The newly created task control block.
1680 @retval EFI_SUCCESS The task control block is created.
1681 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1682 @retval EFI_NOT_READY The target can not accept new commands.
1686 IN ISCSI_CONNECTION
*Conn
,
1690 ISCSI_SESSION
*Session
;
1693 ASSERT (Tcb
!= NULL
);
1695 Session
= Conn
->Session
;
1697 if (ISCSI_SEQ_GT (Session
->CmdSN
, Session
->MaxCmdSN
)) {
1698 return EFI_NOT_READY
;
1701 NewTcb
= AllocateZeroPool (sizeof (ISCSI_TCB
));
1702 if (NewTcb
== NULL
) {
1703 return EFI_OUT_OF_RESOURCES
;
1706 InitializeListHead (&NewTcb
->Link
);
1708 NewTcb
->SoFarInOrder
= TRUE
;
1709 NewTcb
->InitiatorTaskTag
= Session
->InitiatorTaskTag
;
1710 NewTcb
->CmdSN
= Session
->CmdSN
;
1711 NewTcb
->Conn
= Conn
;
1713 InsertTailList (&Session
->TcbList
, &NewTcb
->Link
);
1716 // Advance the initiator task tag.
1718 Session
->InitiatorTaskTag
++;
1727 Delete the tcb from the connection and destroy it.
1729 @param[in] Tcb The tcb to delete.
1736 RemoveEntryList (&Tcb
->Link
);
1742 Find the task control block by the initator task tag.
1744 @param[in] TcbList The tcb list.
1745 @param[in] InitiatorTaskTag The initiator task tag.
1747 @return The task control block found.
1751 IN LIST_ENTRY
*TcbList
,
1752 IN UINT32 InitiatorTaskTag
1760 NET_LIST_FOR_EACH (Entry
, TcbList
) {
1761 Tcb
= NET_LIST_USER_STRUCT (Entry
, ISCSI_TCB
, Link
);
1763 if (Tcb
->InitiatorTaskTag
== InitiatorTaskTag
) {
1774 Create a data segment, pad it and calculate the CRC if needed.
1776 @param[in] Data The data to fill into the data segment.
1777 @param[in] Len Length of the data.
1778 @param[in] DataDigest Whether to calculate CRC for this data segment.
1780 @return The net buffer wrapping the data segment.
1783 IScsiNewDataSegment (
1786 IN BOOLEAN DataDigest
1789 NET_FRAGMENT Fragment
[2];
1790 UINT32 FragmentCount
;
1794 Fragment
[0].Len
= Len
;
1795 Fragment
[0].Bulk
= Data
;
1797 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1799 Fragment
[1].Len
= PadLen
;
1800 Fragment
[1].Bulk
= (UINT8
*) &mDataSegPad
;
1807 DataSeg
= NetbufFromExt (&Fragment
[0], FragmentCount
, 0, 0, IScsiNbufExtFree
, NULL
);
1813 Create a iSCSI SCSI command PDU to encapsulate the command issued
1814 by SCSI through the EXT SCSI PASS THRU Protocol.
1816 @param[in] Packet The EXT SCSI PASS THRU request packet containing the SCSI command.
1817 @param[in] Lun The LUN.
1818 @param[in] Tcb The tcb associated with this SCSI command.
1820 @return The created iSCSI SCSI command PDU.
1821 @retval NULL Other errors as indicated.
1824 IScsiNewScsiCmdPdu (
1825 IN EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
1830 LIST_ENTRY
*NbufList
;
1834 SCSI_COMMAND
*ScsiCmd
;
1837 ISCSI_ADDITIONAL_HEADER
*Header
;
1838 ISCSI_BI_EXP_READ_DATA_LEN_AHS
*BiExpReadDataLenAHS
;
1839 ISCSI_SESSION
*Session
;
1840 UINT32 ImmediateDataLen
;
1844 if (Packet
->DataDirection
== DataBi
) {
1846 // Bi directional Read/Write command, the bidirectional expected
1847 // read data length AHS is required.
1849 AHSLength
+= sizeof (ISCSI_BI_EXP_READ_DATA_LEN_AHS
);
1852 if (Packet
->CdbLength
> 16) {
1854 // The CDB exceeds 16 bytes, an extended CDB AHS is required.
1856 AHSLength
= (UINT8
) (AHSLength
+ (ISCSI_ROUNDUP (Packet
->CdbLength
- 16) + sizeof (ISCSI_ADDITIONAL_HEADER
)));
1859 Length
= sizeof (SCSI_COMMAND
) + AHSLength
;
1860 PduHeader
= NetbufAlloc (Length
);
1861 if (PduHeader
== NULL
) {
1865 ScsiCmd
= (SCSI_COMMAND
*) NetbufAllocSpace (PduHeader
, Length
, NET_BUF_TAIL
);
1866 if (ScsiCmd
== NULL
) {
1867 NetbufFree (PduHeader
);
1870 Header
= (ISCSI_ADDITIONAL_HEADER
*) (ScsiCmd
+ 1);
1872 ZeroMem (ScsiCmd
, Length
);
1874 ISCSI_SET_OPCODE (ScsiCmd
, ISCSI_OPCODE_SCSI_CMD
, 0);
1875 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_TASK_ATTR_SIMPLE
);
1878 // Set the READ/WRITE flags according to the IO type of this request.
1880 switch (Packet
->DataDirection
) {
1882 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
);
1883 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->InTransferLength
);
1887 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_WRITE
);
1888 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
1892 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
| SCSI_CMD_PDU_FLAG_WRITE
);
1893 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
1896 // Fill the bidirectional expected read data length AHS.
1898 BiExpReadDataLenAHS
= (ISCSI_BI_EXP_READ_DATA_LEN_AHS
*) Header
;
1899 Header
= (ISCSI_ADDITIONAL_HEADER
*) (BiExpReadDataLenAHS
+ 1);
1901 BiExpReadDataLenAHS
->Length
= NTOHS (5);
1902 BiExpReadDataLenAHS
->Type
= ISCSI_AHS_TYPE_BI_EXP_READ_DATA_LEN
;
1903 BiExpReadDataLenAHS
->ExpReadDataLength
= NTOHL (Packet
->InTransferLength
);
1908 ScsiCmd
->TotalAHSLength
= AHSLength
;
1909 CopyMem (ScsiCmd
->Lun
, &Lun
, sizeof (ScsiCmd
->Lun
));
1910 ScsiCmd
->InitiatorTaskTag
= NTOHL (Tcb
->InitiatorTaskTag
);
1911 ScsiCmd
->CmdSN
= NTOHL (Tcb
->CmdSN
);
1912 ScsiCmd
->ExpStatSN
= NTOHL (Tcb
->Conn
->ExpStatSN
);
1914 CopyMem (ScsiCmd
->Cdb
, Packet
->Cdb
, sizeof (ScsiCmd
->Cdb
));
1916 if (Packet
->CdbLength
> 16) {
1917 Header
->Length
= NTOHS ((UINT16
) (Packet
->CdbLength
- 15));
1918 Header
->Type
= ISCSI_AHS_TYPE_EXT_CDB
;
1920 CopyMem (Header
+ 1, (UINT8
*) Packet
->Cdb
+ 16, Packet
->CdbLength
- 16);
1924 Session
= Tcb
->Conn
->Session
;
1925 ImmediateDataLen
= 0;
1927 if (Session
->ImmediateData
&& (Packet
->OutTransferLength
!= 0)) {
1929 // Send immediate data in this SCSI Command PDU. The length of the immeidate
1930 // data is the minimum of FirstBurstLength, the data length to be xfered and
1931 // the MaxRecvdataSegmentLength on this connection.
1933 ImmediateDataLen
= MIN (Session
->FirstBurstLength
, Packet
->OutTransferLength
);
1934 ImmediateDataLen
= MIN (ImmediateDataLen
, Tcb
->Conn
->MaxRecvDataSegmentLength
);
1937 // Update the data segment length in the PDU header.
1939 ISCSI_SET_DATASEG_LEN (ScsiCmd
, ImmediateDataLen
);
1942 // Create the data segment.
1944 DataSeg
= IScsiNewDataSegment ((UINT8
*) Packet
->OutDataBuffer
, ImmediateDataLen
, FALSE
);
1945 if (DataSeg
== NULL
) {
1946 NetbufFree (PduHeader
);
1951 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
1952 if (NbufList
== NULL
) {
1953 NetbufFree (PduHeader
);
1954 NetbufFree (DataSeg
);
1960 InitializeListHead (NbufList
);
1961 InsertTailList (NbufList
, &PduHeader
->List
);
1962 InsertTailList (NbufList
, &DataSeg
->List
);
1964 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1966 IScsiFreeNbufList (NbufList
);
1970 if (Session
->InitialR2T
||
1971 (ImmediateDataLen
== Session
->FirstBurstLength
) ||
1972 (ImmediateDataLen
== Packet
->OutTransferLength
)
1975 // Unsolicited data out sequence is not allowed,
1976 // or FirstBurstLength data is already sent out by immediate data
1977 // or all the OUT data accompany this SCSI packet is sent as
1978 // immediate data, the final flag should be set on this SCSI Command
1981 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_BHS_FLAG_FINAL
);
1990 Create a new iSCSI SCSI Data Out PDU.
1992 @param[in] Data The data to put into the Data Out PDU.
1993 @param[in] Len Length of the data.
1994 @param[in] DataSN The DataSN of the Data Out PDU.
1995 @param[in] Tcb The task control block of this Data Out PDU.
1996 @param[in] Lun The LUN.
1998 @return The net buffer wrapping the Data Out PDU.
1999 @retval NULL Other errors as indicated.
2002 IScsiNewDataOutPdu (
2010 LIST_ENTRY
*NbufList
;
2014 ISCSI_SCSI_DATA_OUT
*DataOutHdr
;
2015 ISCSI_XFER_CONTEXT
*XferContext
;
2017 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
2018 if (NbufList
== NULL
) {
2022 InitializeListHead (NbufList
);
2025 // Allocate memory for the BHS.
2027 PduHdr
= NetbufAlloc (sizeof (ISCSI_SCSI_DATA_OUT
));
2028 if (PduHdr
== NULL
) {
2029 FreePool (NbufList
);
2033 // Insert the BHS into the buffer list.
2035 InsertTailList (NbufList
, &PduHdr
->List
);
2037 DataOutHdr
= (ISCSI_SCSI_DATA_OUT
*) NetbufAllocSpace (PduHdr
, sizeof (ISCSI_SCSI_DATA_OUT
), NET_BUF_TAIL
);
2038 ASSERT (DataOutHdr
!= NULL
);
2039 XferContext
= &Tcb
->XferContext
;
2041 ZeroMem (DataOutHdr
, sizeof (ISCSI_SCSI_DATA_OUT
));
2044 // Set the flags and fields of the Data Out PDU BHS.
2046 ISCSI_SET_OPCODE (DataOutHdr
, ISCSI_OPCODE_SCSI_DATA_OUT
, 0);
2047 ISCSI_SET_DATASEG_LEN (DataOutHdr
, Len
);
2049 DataOutHdr
->InitiatorTaskTag
= HTONL (Tcb
->InitiatorTaskTag
);
2050 DataOutHdr
->TargetTransferTag
= HTONL (XferContext
->TargetTransferTag
);
2051 DataOutHdr
->ExpStatSN
= HTONL (Tcb
->Conn
->ExpStatSN
);
2052 DataOutHdr
->DataSN
= HTONL (DataSN
);
2053 DataOutHdr
->BufferOffset
= HTONL (XferContext
->Offset
);
2055 if (XferContext
->TargetTransferTag
!= ISCSI_RESERVED_TAG
) {
2056 CopyMem (&DataOutHdr
->Lun
, &Lun
, sizeof (DataOutHdr
->Lun
));
2059 // Build the data segment for this Data Out PDU.
2061 DataSeg
= IScsiNewDataSegment (Data
, Len
, FALSE
);
2062 if (DataSeg
== NULL
) {
2063 IScsiFreeNbufList (NbufList
);
2067 // Put the data segment into the buffer list and combine it with the BHS
2068 // into a full Data Out PDU.
2070 InsertTailList (NbufList
, &DataSeg
->List
);
2071 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
2073 IScsiFreeNbufList (NbufList
);
2080 Generate a consecutive sequence of iSCSI SCSI Data Out PDUs.
2082 @param[in] Data The data which will be carried by the sequence of iSCSI SCSI Data Out PDUs.
2083 @param[in] Tcb The task control block of the data to send out.
2084 @param[in] Lun The LUN the data will be sent to.
2086 @return A list of net buffers with each of them wraps an iSCSI SCSI Data Out PDU.
2087 @retval NULL Other errors as indicated.
2090 IScsiGenerateDataOutPduSequence (
2096 LIST_ENTRY
*PduList
;
2099 NET_BUF
*DataOutPdu
;
2100 ISCSI_CONNECTION
*Conn
;
2101 ISCSI_XFER_CONTEXT
*XferContext
;
2102 UINT8
*DataOutPacket
;
2104 PduList
= AllocatePool (sizeof (LIST_ENTRY
));
2105 if (PduList
== NULL
) {
2109 InitializeListHead (PduList
);
2114 XferContext
= &Tcb
->XferContext
;
2116 while (XferContext
->DesiredLength
> 0) {
2118 // Determine the length of data this Data Out PDU can carry.
2120 DataLen
= MIN (XferContext
->DesiredLength
, Conn
->MaxRecvDataSegmentLength
);
2123 // Create a Data Out PDU.
2125 DataOutPdu
= IScsiNewDataOutPdu (Data
, DataLen
, DataSN
, Tcb
, Lun
);
2126 if (DataOutPdu
== NULL
) {
2127 IScsiFreeNbufList (PduList
);
2133 InsertTailList (PduList
, &DataOutPdu
->List
);
2136 // Update the context and DataSN.
2138 XferContext
->Offset
+= DataLen
;
2139 XferContext
->DesiredLength
-= DataLen
;
2144 // Set the F bit for the last data out PDU in this sequence.
2146 DataOutPacket
= NetbufGetByte (DataOutPdu
, 0, NULL
);
2147 if (DataOutPacket
== NULL
) {
2148 IScsiFreeNbufList (PduList
);
2153 ISCSI_SET_FLAG (DataOutPacket
, ISCSI_BHS_FLAG_FINAL
);
2161 Send the Data in a sequence of Data Out PDUs one by one.
2163 @param[in] Data The data to carry by Data Out PDUs.
2164 @param[in] Lun The LUN the data will be sent to.
2165 @param[in] Tcb The task control block.
2167 @retval EFI_SUCCES The data is sent out to the LUN.
2168 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2169 @retval Others Other errors as indicated.
2172 IScsiSendDataOutPduSequence (
2178 LIST_ENTRY
*DataOutPduList
;
2184 // Generate the Data Out PDU sequence.
2186 DataOutPduList
= IScsiGenerateDataOutPduSequence (Data
, Tcb
, Lun
);
2187 if (DataOutPduList
== NULL
) {
2188 return EFI_OUT_OF_RESOURCES
;
2191 Status
= EFI_SUCCESS
;
2194 // Send the Data Out PDU's one by one.
2196 NET_LIST_FOR_EACH (Entry
, DataOutPduList
) {
2197 Pdu
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
2199 Status
= Tcp4IoTransmit (&Tcb
->Conn
->Tcp4Io
, Pdu
);
2200 if (EFI_ERROR (Status
)) {
2205 IScsiFreeNbufList (DataOutPduList
);
2211 Process the received iSCSI SCSI Data In PDU.
2213 @param[in] Pdu The Data In PDU received.
2214 @param[in] Tcb The task control block.
2215 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2217 @retval EFI_SUCCES The check on the Data IN PDU is passed and some update
2219 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2220 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2221 @retval Others Other errors as indicated.
2227 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2230 ISCSI_SCSI_DATA_IN
*DataInHdr
;
2233 DataInHdr
= (ISCSI_SCSI_DATA_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2234 if (DataInHdr
== NULL
) {
2235 return EFI_PROTOCOL_ERROR
;
2238 DataInHdr
->InitiatorTaskTag
= NTOHL (DataInHdr
->InitiatorTaskTag
);
2239 DataInHdr
->ExpCmdSN
= NTOHL (DataInHdr
->ExpCmdSN
);
2240 DataInHdr
->MaxCmdSN
= NTOHL (DataInHdr
->MaxCmdSN
);
2241 DataInHdr
->DataSN
= NTOHL (DataInHdr
->DataSN
);
2244 // Check the DataSN.
2246 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, DataInHdr
->DataSN
);
2247 if (EFI_ERROR (Status
)) {
2251 if (DataInHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2252 return EFI_PROTOCOL_ERROR
;
2255 // Update the command related sequence numbers.
2257 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, DataInHdr
->MaxCmdSN
, DataInHdr
->ExpCmdSN
);
2259 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_STATUS_VALID
)) {
2260 if (!ISCSI_FLAG_ON (DataInHdr
, ISCSI_BHS_FLAG_FINAL
)) {
2262 // The S bit is on but the F bit is off.
2264 return EFI_PROTOCOL_ERROR
;
2267 Tcb
->StatusXferd
= TRUE
;
2269 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_OVERFLOW
| SCSI_DATA_IN_PDU_FLAG_UNDERFLOW
)) {
2271 // Underflow and Overflow are mutual flags.
2273 return EFI_PROTOCOL_ERROR
;
2276 // S bit is on, the StatSN is valid.
2278 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NTOHL (DataInHdr
->StatSN
));
2279 if (EFI_ERROR (Status
)) {
2283 Packet
->HostAdapterStatus
= 0;
2284 Packet
->TargetStatus
= DataInHdr
->Status
;
2286 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2287 Packet
->InTransferLength
+= NTOHL (DataInHdr
->ResidualCount
);
2288 Status
= EFI_BAD_BUFFER_SIZE
;
2291 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2292 Packet
->InTransferLength
-= NTOHL (DataInHdr
->ResidualCount
);
2300 Process the received iSCSI R2T PDU.
2302 @param[in] Pdu The R2T PDU received.
2303 @param[in] Tcb The task control block.
2304 @param[in] Lun The Lun.
2305 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2307 @retval EFI_SUCCES The R2T PDU is valid and the solicited data is sent out.
2308 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2309 @retval Others Other errors as indicated.
2316 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2319 ISCSI_READY_TO_TRANSFER
*R2THdr
;
2321 ISCSI_XFER_CONTEXT
*XferContext
;
2324 R2THdr
= (ISCSI_READY_TO_TRANSFER
*) NetbufGetByte (Pdu
, 0, NULL
);
2325 if (R2THdr
== NULL
) {
2326 return EFI_PROTOCOL_ERROR
;
2329 R2THdr
->InitiatorTaskTag
= NTOHL (R2THdr
->InitiatorTaskTag
);
2330 R2THdr
->TargetTransferTag
= NTOHL (R2THdr
->TargetTransferTag
);
2331 R2THdr
->StatSN
= NTOHL (R2THdr
->StatSN
);
2332 R2THdr
->R2TSeqNum
= NTOHL (R2THdr
->R2TSeqNum
);
2333 R2THdr
->BufferOffset
= NTOHL (R2THdr
->BufferOffset
);
2334 R2THdr
->DesiredDataTransferLength
= NTOHL (R2THdr
->DesiredDataTransferLength
);
2336 if ((R2THdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) || !ISCSI_SEQ_EQ (R2THdr
->StatSN
, Tcb
->Conn
->ExpStatSN
)) {
2337 return EFI_PROTOCOL_ERROR
;;
2340 // Check the sequence number.
2342 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, R2THdr
->R2TSeqNum
);
2343 if (EFI_ERROR (Status
)) {
2347 XferContext
= &Tcb
->XferContext
;
2348 XferContext
->TargetTransferTag
= R2THdr
->TargetTransferTag
;
2349 XferContext
->Offset
= R2THdr
->BufferOffset
;
2350 XferContext
->DesiredLength
= R2THdr
->DesiredDataTransferLength
;
2352 if (((XferContext
->Offset
+ XferContext
->DesiredLength
) > Packet
->OutTransferLength
) ||
2353 (XferContext
->DesiredLength
> Tcb
->Conn
->Session
->MaxBurstLength
)
2355 return EFI_PROTOCOL_ERROR
;
2358 // Send the data solicited by this R2T.
2360 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2361 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2367 Process the received iSCSI SCSI Response PDU.
2369 @param[in] Pdu The Response PDU received.
2370 @param[in] Tcb The task control block.
2371 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2373 @retval EFI_SUCCES The Response PDU is processed.
2374 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2375 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2376 @retval Others Other errors as indicated.
2379 IScsiOnScsiRspRcvd (
2382 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2385 SCSI_RESPONSE
*ScsiRspHdr
;
2386 ISCSI_SENSE_DATA
*SenseData
;
2390 ScsiRspHdr
= (SCSI_RESPONSE
*) NetbufGetByte (Pdu
, 0, NULL
);
2391 if (ScsiRspHdr
== NULL
) {
2392 return EFI_PROTOCOL_ERROR
;
2395 ScsiRspHdr
->InitiatorTaskTag
= NTOHL (ScsiRspHdr
->InitiatorTaskTag
);
2396 if (ScsiRspHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2397 return EFI_PROTOCOL_ERROR
;
2400 ScsiRspHdr
->StatSN
= NTOHL (ScsiRspHdr
->StatSN
);
2402 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, ScsiRspHdr
->StatSN
);
2403 if (EFI_ERROR (Status
)) {
2407 ScsiRspHdr
->MaxCmdSN
= NTOHL (ScsiRspHdr
->MaxCmdSN
);
2408 ScsiRspHdr
->ExpCmdSN
= NTOHL (ScsiRspHdr
->ExpCmdSN
);
2409 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, ScsiRspHdr
->MaxCmdSN
, ScsiRspHdr
->ExpCmdSN
);
2411 Tcb
->StatusXferd
= TRUE
;
2413 Packet
->HostAdapterStatus
= ScsiRspHdr
->Response
;
2414 if (Packet
->HostAdapterStatus
!= ISCSI_SERVICE_RSP_COMMAND_COMPLETE_AT_TARGET
) {
2418 Packet
->TargetStatus
= ScsiRspHdr
->Status
;
2420 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
| SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
) ||
2421 ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
| SCSI_RSP_PDU_FLAG_UNDERFLOW
)
2423 return EFI_PROTOCOL_ERROR
;
2426 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
)) {
2427 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2428 Status
= EFI_BAD_BUFFER_SIZE
;
2431 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
)) {
2432 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2435 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2436 if (Packet
->DataDirection
== DataIn
) {
2437 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2439 Packet
->OutTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2442 Status
= EFI_BAD_BUFFER_SIZE
;
2445 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2446 if (Packet
->DataDirection
== DataIn
) {
2447 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2449 Packet
->OutTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2453 DataSegLen
= ISCSI_GET_DATASEG_LEN (ScsiRspHdr
);
2454 if (DataSegLen
!= 0) {
2455 SenseData
= (ISCSI_SENSE_DATA
*) NetbufGetByte (Pdu
, sizeof (SCSI_RESPONSE
), NULL
);
2456 if (SenseData
== NULL
) {
2457 return EFI_PROTOCOL_ERROR
;
2460 SenseData
->Length
= NTOHS (SenseData
->Length
);
2462 Packet
->SenseDataLength
= (UINT8
) MIN (SenseData
->Length
, Packet
->SenseDataLength
);
2463 if (Packet
->SenseDataLength
!= 0) {
2464 CopyMem (Packet
->SenseData
, &SenseData
->Data
[0], Packet
->SenseDataLength
);
2467 Packet
->SenseDataLength
= 0;
2474 Process the received NOP In PDU.
2476 @param[in] Pdu The NOP In PDU received.
2477 @param[in] Tcb The task control block.
2479 @retval EFI_SUCCES The NOP In PDU is processed and the related sequence
2480 numbers are updated.
2481 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2489 ISCSI_NOP_IN
*NopInHdr
;
2492 NopInHdr
= (ISCSI_NOP_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2493 if (NopInHdr
== NULL
) {
2494 return EFI_PROTOCOL_ERROR
;
2497 NopInHdr
->StatSN
= NTOHL (NopInHdr
->StatSN
);
2498 NopInHdr
->ExpCmdSN
= NTOHL (NopInHdr
->ExpCmdSN
);
2499 NopInHdr
->MaxCmdSN
= NTOHL (NopInHdr
->MaxCmdSN
);
2501 if (NopInHdr
->InitiatorTaskTag
== ISCSI_RESERVED_TAG
) {
2502 if (NopInHdr
->StatSN
!= Tcb
->Conn
->ExpStatSN
) {
2503 return EFI_PROTOCOL_ERROR
;
2506 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NopInHdr
->StatSN
);
2507 if (EFI_ERROR (Status
)) {
2512 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, NopInHdr
->MaxCmdSN
, NopInHdr
->ExpCmdSN
);
2518 Execute the SCSI command issued through the EXT SCSI PASS THRU protocol.
2520 @param[in] PassThru The EXT SCSI PASS THRU protocol.
2521 @param[in] Target The target ID.
2522 @param[in] Lun The LUN.
2523 @param[in, out] Packet The request packet containing IO request, SCSI command
2524 buffer and buffers to read/write.
2526 @retval EFI_SUCCES The SCSI command is executed and the result is updated to
2528 @retval EFI_DEVICE_ERROR Session state was not as required.
2529 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2530 @retval EFI_NOT_READY The target can not accept new commands.
2531 @retval Others Other errors as indicated.
2534 IScsiExecuteScsiCommand (
2535 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*PassThru
,
2538 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2542 ISCSI_DRIVER_DATA
*Private
;
2543 ISCSI_SESSION
*Session
;
2544 EFI_EVENT TimeoutEvent
;
2545 ISCSI_CONNECTION
*Conn
;
2548 ISCSI_XFER_CONTEXT
*XferContext
;
2550 ISCSI_IN_BUFFER_CONTEXT InBufferContext
;
2554 Private
= ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru
);
2555 Session
= &Private
->Session
;
2556 Status
= EFI_SUCCESS
;
2558 TimeoutEvent
= NULL
;
2561 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
2562 Status
= EFI_DEVICE_ERROR
;
2566 Conn
= NET_LIST_USER_STRUCT_S (
2567 Session
->Conns
.ForwardLink
,
2570 ISCSI_CONNECTION_SIGNATURE
2573 if (Packet
->Timeout
!= 0) {
2574 Timeout
= MultU64x32 (Packet
->Timeout
, 2);
2577 Status
= IScsiNewTcb (Conn
, &Tcb
);
2578 if (EFI_ERROR (Status
)) {
2582 // Encapsulate the SCSI request packet into an iSCSI SCSI Command PDU.
2584 Pdu
= IScsiNewScsiCmdPdu (Packet
, Lun
, Tcb
);
2586 Status
= EFI_OUT_OF_RESOURCES
;
2590 XferContext
= &Tcb
->XferContext
;
2591 PduHdr
= NetbufGetByte (Pdu
, 0, NULL
);
2592 if (PduHdr
== NULL
) {
2593 Status
= EFI_PROTOCOL_ERROR
;
2597 XferContext
->Offset
= ISCSI_GET_DATASEG_LEN (PduHdr
);
2600 // Transmit the SCSI Command PDU.
2602 Status
= Tcp4IoTransmit (&Conn
->Tcp4Io
, Pdu
);
2606 if (EFI_ERROR (Status
)) {
2610 if (!Session
->InitialR2T
&&
2611 (XferContext
->Offset
< Session
->FirstBurstLength
) &&
2612 (XferContext
->Offset
< Packet
->OutTransferLength
)
2615 // Unsolicited Data-Out sequence is allowed, there is remaining SCSI
2616 // OUT data and the limit of FirstBurstLength is not reached.
2618 XferContext
->TargetTransferTag
= ISCSI_RESERVED_TAG
;
2619 XferContext
->DesiredLength
= MIN (
2620 Session
->FirstBurstLength
,
2621 Packet
->OutTransferLength
- XferContext
->Offset
2624 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2625 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2626 if (EFI_ERROR (Status
)) {
2631 InBufferContext
.InData
= (UINT8
*) Packet
->InDataBuffer
;
2632 InBufferContext
.InDataLen
= Packet
->InTransferLength
;
2634 while (!Tcb
->StatusXferd
) {
2636 // Start the timeout timer.
2639 Status
= gBS
->SetTimer (Conn
->TimeoutEvent
, TimerRelative
, Timeout
);
2640 if (EFI_ERROR (Status
)) {
2643 TimeoutEvent
= Conn
->TimeoutEvent
;
2646 // try to receive PDU from target.
2648 Status
= IScsiReceivePdu (Conn
, &Pdu
, &InBufferContext
, FALSE
, FALSE
, TimeoutEvent
);
2649 if (EFI_ERROR (Status
)) {
2653 PduHdr
= NetbufGetByte (Pdu
, 0, NULL
);
2654 if (PduHdr
== NULL
) {
2655 Status
= EFI_PROTOCOL_ERROR
;
2659 switch (ISCSI_GET_OPCODE (PduHdr
)) {
2660 case ISCSI_OPCODE_SCSI_DATA_IN
:
2661 Status
= IScsiOnDataInRcvd (Pdu
, Tcb
, Packet
);
2664 case ISCSI_OPCODE_R2T
:
2665 Status
= IScsiOnR2TRcvd (Pdu
, Tcb
, Lun
, Packet
);
2668 case ISCSI_OPCODE_SCSI_RSP
:
2669 Status
= IScsiOnScsiRspRcvd (Pdu
, Tcb
, Packet
);
2672 case ISCSI_OPCODE_NOP_IN
:
2673 Status
= IScsiOnNopInRcvd (Pdu
, Tcb
);
2676 case ISCSI_OPCODE_VENDOR_T0
:
2677 case ISCSI_OPCODE_VENDOR_T1
:
2678 case ISCSI_OPCODE_VENDOR_T2
:
2680 // These messages are vendor specific, skip them.
2685 Status
= EFI_PROTOCOL_ERROR
;
2691 if (EFI_ERROR (Status
)) {
2698 if (TimeoutEvent
!= NULL
) {
2699 gBS
->SetTimer (TimeoutEvent
, TimerCancel
, 0);
2710 Reinstate the session on some error.
2712 @param[in, out] Private The iSCSI driver data.
2714 @retval EFI_SUCCES The session is reinstated from some error.
2715 @retval Other Reinstatement failed.
2718 IScsiSessionReinstatement (
2719 IN OUT ISCSI_DRIVER_DATA
*Private
2722 ISCSI_SESSION
*Session
;
2725 Session
= &Private
->Session
;
2726 ASSERT (Session
->State
!= SESSION_STATE_FREE
);
2729 // Abort the session and re-init it.
2731 IScsiSessionAbort (Session
);
2732 IScsiSessionInit (Session
, TRUE
);
2737 Status
= IScsiSessionLogin (Private
);
2743 Initialize some session parameters before login.
2745 @param[in, out] Session The iSCSI session.
2746 @param[in] Recovery Whether the request is from a fresh new start or recovery.
2750 IN OUT ISCSI_SESSION
*Session
,
2755 Session
->Signature
= ISCSI_SESSION_SIGNATURE
;
2756 Session
->State
= SESSION_STATE_FREE
;
2758 InitializeListHead (&Session
->Conns
);
2759 InitializeListHead (&Session
->TcbList
);
2765 Session
->InitiatorTaskTag
= 1;
2766 Session
->NextCid
= 1;
2768 Session
->TargetPortalGroupTag
= 0;
2769 Session
->MaxConnections
= ISCSI_MAX_CONNS_PER_SESSION
;
2770 Session
->InitialR2T
= FALSE
;
2771 Session
->ImmediateData
= TRUE
;
2772 Session
->MaxBurstLength
= 262144;
2773 Session
->FirstBurstLength
= MAX_RECV_DATA_SEG_LEN_IN_FFP
;
2774 Session
->DefaultTime2Wait
= 2;
2775 Session
->DefaultTime2Retain
= 20;
2776 Session
->MaxOutstandingR2T
= DEFAULT_MAX_OUTSTANDING_R2T
;
2777 Session
->DataPDUInOrder
= TRUE
;
2778 Session
->DataSequenceInOrder
= TRUE
;
2779 Session
->ErrorRecoveryLevel
= 0;
2783 Abort the iSCSI session, that is, reset all the connection and free the
2786 @param[in, out] Session The iSCSI session.
2788 @retval EFI_SUCCES The session is aborted.
2792 IN OUT ISCSI_SESSION
*Session
2795 ISCSI_DRIVER_DATA
*Private
;
2796 ISCSI_CONNECTION
*Conn
;
2798 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
2802 ASSERT (!IsListEmpty (&Session
->Conns
));
2804 Private
= ISCSI_DRIVER_DATA_FROM_SESSION (Session
);
2806 while (!IsListEmpty (&Session
->Conns
)) {
2807 Conn
= NET_LIST_USER_STRUCT_S (
2808 Session
->Conns
.ForwardLink
,
2811 ISCSI_CONNECTION_SIGNATURE
2814 gBS
->CloseProtocol (
2815 Conn
->Tcp4Io
.Handle
,
2816 &gEfiTcp4ProtocolGuid
,
2818 Private
->ExtScsiPassThruHandle
2821 IScsiConnReset (Conn
);
2823 IScsiDetatchConnection (Conn
);
2824 IScsiDestroyConnection (Conn
);
2827 Session
->State
= SESSION_STATE_FAILED
;