2 The implementation of iSCSI protocol based on RFC3720.
4 Copyright (c) 2004 - 2014, 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 BOOLEAN MediaPresent
;
294 Session
= &Private
->Session
;
297 // Check media status before session login
300 NetLibDetectMedia (Private
->Controller
, &MediaPresent
);
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 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_INITIAL_R2T
);
1229 if (Value
== NULL
) {
1233 Session
->InitialR2T
= (BOOLEAN
) (Session
->InitialR2T
|| (AsciiStrCmp (Value
, "Yes") == 0));
1236 // ImmediateData, result function is AND.
1238 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_IMMEDIATE_DATA
);
1239 if (Value
== NULL
) {
1243 Session
->ImmediateData
= (BOOLEAN
) (Session
->ImmediateData
&& (AsciiStrCmp (Value
, "Yes") == 0));
1246 // MaxRecvDataSegmentLength is declarative.
1248 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
);
1249 if (Value
!= NULL
) {
1250 Conn
->MaxRecvDataSegmentLength
= (UINT32
) AsciiStrDecimalToUintn (Value
);
1253 // MaxBurstLength, result funtion is Mininum.
1255 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_BURST_LENGTH
);
1256 if (Value
== NULL
) {
1260 NumericValue
= AsciiStrDecimalToUintn (Value
);
1261 Session
->MaxBurstLength
= (UINT32
) MIN (Session
->MaxBurstLength
, NumericValue
);
1264 // FirstBurstLength, result function is Minimum. Irrelevant when InitialR2T=Yes and
1265 // ImmediateData=No.
1266 // This Key/Value is negotiation type.
1268 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_FIRST_BURST_LENGTH
);
1269 if (Value
== NULL
) {
1273 NumericValue
= AsciiStrDecimalToUintn (Value
);
1274 Session
->FirstBurstLength
= (UINT32
) MIN (Session
->FirstBurstLength
, NumericValue
);
1277 // MaxConnections, result function is Minimum.
1279 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_CONNECTIONS
);
1280 if (Value
== NULL
) {
1284 NumericValue
= AsciiStrDecimalToUintn (Value
);
1285 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1289 Session
->MaxConnections
= (UINT32
) MIN (Session
->MaxConnections
, NumericValue
);
1292 // DataPDUInOrder, result function is OR.
1294 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_PDU_IN_ORDER
);
1295 if (Value
== NULL
) {
1299 Session
->DataPDUInOrder
= (BOOLEAN
) (Session
->DataPDUInOrder
|| (AsciiStrCmp (Value
, "Yes") == 0));
1302 // DataSequenceInorder, result function is OR.
1304 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
);
1305 if (Value
== NULL
) {
1309 Session
->DataSequenceInOrder
= (BOOLEAN
) (Session
->DataSequenceInOrder
|| (AsciiStrCmp (Value
, "Yes") == 0));
1312 // DefaultTime2Wait, result function is Maximum.
1314 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2WAIT
);
1315 if (Value
== NULL
) {
1319 NumericValue
= AsciiStrDecimalToUintn (Value
);
1320 if (NumericValue
== 0) {
1321 Session
->DefaultTime2Wait
= 0;
1322 } else if (NumericValue
> 3600) {
1325 Session
->DefaultTime2Wait
= (UINT32
) MAX (Session
->DefaultTime2Wait
, NumericValue
);
1328 // DefaultTime2Retain, result function is Minimum.
1330 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2RETAIN
);
1331 if (Value
== NULL
) {
1335 NumericValue
= AsciiStrDecimalToUintn (Value
);
1336 if (NumericValue
== 0) {
1337 Session
->DefaultTime2Retain
= 0;
1338 } else if (NumericValue
> 3600) {
1341 Session
->DefaultTime2Retain
= (UINT32
) MIN (Session
->DefaultTime2Retain
, NumericValue
);
1344 // MaxOutstandingR2T, result function is Minimum.
1346 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_OUTSTANDING_R2T
);
1347 if (Value
== NULL
) {
1351 NumericValue
= AsciiStrDecimalToUintn (Value
);
1352 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1356 Session
->MaxOutstandingR2T
= (UINT16
) MIN (Session
->MaxOutstandingR2T
, NumericValue
);
1359 // Remove declarative key-value paris if any.
1361 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_SESSION_TYPE
);
1362 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_ALIAS
);
1363 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_PORTAL_GROUP_TAG
);
1365 if (IsListEmpty (KeyValueList
)) {
1367 // Succeed if no more keys in the list.
1369 Status
= EFI_SUCCESS
;
1374 IScsiFreeKeyValueList (KeyValueList
);
1382 Fill the oprational prameters.
1384 @param[in] Conn The connection in iSCSI login.
1385 @param[in, out] Pdu The iSCSI login request PDU to fill the parameters.
1387 @retval EFI_SUCCESS The parmeters are filled into the iSCSI login request PDU.
1391 IN ISCSI_CONNECTION
*Conn
,
1395 ISCSI_SESSION
*Session
;
1398 Session
= Conn
->Session
;
1400 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->HeaderDigest
== IScsiDigestCRC32
) ? "None,CRC32" : "None");
1401 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_HEADER_DIGEST
, Value
);
1403 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->DataDigest
== IScsiDigestCRC32
) ? "None,CRC32" : "None");
1404 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_DIGEST
, Value
);
1406 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->ErrorRecoveryLevel
);
1407 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_ERROR_RECOVERY_LEVEL
, Value
);
1409 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->InitialR2T
? "Yes" : "No");
1410 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_INITIAL_R2T
, Value
);
1412 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->ImmediateData
? "Yes" : "No");
1413 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_IMMEDIATE_DATA
, Value
);
1415 AsciiSPrint (Value
, sizeof (Value
), "%d", MAX_RECV_DATA_SEG_LEN_IN_FFP
);
1416 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
, Value
);
1418 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxBurstLength
);
1419 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_BURST_LENGTH
, Value
);
1421 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->FirstBurstLength
);
1422 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_FIRST_BURST_LENGTH
, Value
);
1424 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxConnections
);
1425 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_CONNECTIONS
, Value
);
1427 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataPDUInOrder
? "Yes" : "No");
1428 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_PDU_IN_ORDER
, Value
);
1430 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataSequenceInOrder
? "Yes" : "No");
1431 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
, Value
);
1433 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Wait
);
1434 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2WAIT
, Value
);
1436 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Retain
);
1437 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2RETAIN
, Value
);
1439 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxOutstandingR2T
);
1440 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_OUTSTANDING_R2T
, Value
);
1446 Pad the iSCSI AHS or data segment to an integer number of 4 byte words.
1448 @param[in, out] Pdu The iSCSI pdu which contains segments to pad.
1449 @param[in] Len The length of the last semgnet in the PDU.
1451 @retval EFI_SUCCESS The segment is padded or no need to pad it.
1452 @retval EFI_OUT_OF_RESOURCES There is not enough remaining free space to add the
1457 IN OUT NET_BUF
*Pdu
,
1464 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1467 Data
= NetbufAllocSpace (Pdu
, PadLen
, NET_BUF_TAIL
);
1469 return EFI_OUT_OF_RESOURCES
;
1472 ZeroMem (Data
, PadLen
);
1479 Build a key-value list from the data segment.
1481 @param[in] Data The data segment containing the key-value pairs.
1482 @param[in] Len Length of the data segment.
1484 @return The key-value list.
1485 @retval NULL Other errors as indicated.
1488 IScsiBuildKeyValueList (
1493 LIST_ENTRY
*ListHead
;
1494 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1496 ListHead
= AllocatePool (sizeof (LIST_ENTRY
));
1497 if (ListHead
== NULL
) {
1501 InitializeListHead (ListHead
);
1504 KeyValuePair
= AllocatePool (sizeof (ISCSI_KEY_VALUE_PAIR
));
1505 if (KeyValuePair
== NULL
) {
1509 InitializeListHead (&KeyValuePair
->List
);
1511 KeyValuePair
->Key
= Data
;
1513 while ((Len
> 0) && (*Data
!= '=')) {
1524 FreePool (KeyValuePair
);
1528 KeyValuePair
->Value
= Data
;
1530 InsertTailList (ListHead
, &KeyValuePair
->List
);;
1532 Data
+= AsciiStrLen (KeyValuePair
->Value
) + 1;
1533 Len
-= (UINT32
) AsciiStrLen (KeyValuePair
->Value
) + 1;
1540 IScsiFreeKeyValueList (ListHead
);
1546 Get the value string by the key name from the key-value list. If found,
1547 the key-value entry will be removed from the list.
1549 @param[in, out] KeyValueList The key-value list.
1550 @param[in] Key The key name to find.
1552 @return The value string.
1555 IScsiGetValueByKeyFromList (
1556 IN OUT LIST_ENTRY
*KeyValueList
,
1561 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1566 NET_LIST_FOR_EACH (Entry
, KeyValueList
) {
1567 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1569 if (AsciiStrCmp (KeyValuePair
->Key
, Key
) == 0) {
1570 Value
= KeyValuePair
->Value
;
1572 RemoveEntryList (&KeyValuePair
->List
);
1573 FreePool (KeyValuePair
);
1582 Free the key-value list.
1584 @param[in] KeyValueList The key-value list.
1587 IScsiFreeKeyValueList (
1588 IN LIST_ENTRY
*KeyValueList
1592 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1594 while (!IsListEmpty (KeyValueList
)) {
1595 Entry
= NetListRemoveHead (KeyValueList
);
1596 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1598 FreePool (KeyValuePair
);
1601 FreePool (KeyValueList
);
1605 Normalize the iSCSI name according to RFC.
1607 @param[in, out] Name The iSCSI name.
1608 @param[in] Len length of the iSCSI name.
1610 @retval EFI_SUCCESS The iSCSI name is valid and normalized.
1611 @retval EFI_PROTOCOL_ERROR The iSCSI name is mal-formatted or not in the IQN format.
1614 IScsiNormalizeName (
1621 for (Index
= 0; Index
< Len
; Index
++) {
1622 if (NET_IS_UPPER_CASE_CHAR (Name
[Index
])) {
1624 // Convert the upper-case characters to lower-case ones
1626 Name
[Index
] = (CHAR8
) (Name
[Index
] - 'A' + 'a');
1629 if (!NET_IS_LOWER_CASE_CHAR (Name
[Index
]) &&
1630 !NET_IS_DIGIT (Name
[Index
]) &&
1631 (Name
[Index
] != '-') &&
1632 (Name
[Index
] != '.') &&
1633 (Name
[Index
] != ':')
1636 // ASCII dash, dot, colon lower-case characters and digit characters
1639 return EFI_PROTOCOL_ERROR
;
1643 if ((Len
< 4) || (CompareMem (Name
, "iqn.", 4) != 0)) {
1645 // Only IQN format is accepted now.
1647 return EFI_PROTOCOL_ERROR
;
1654 Create an iSCSI task control block.
1656 @param[in] Conn The connection on which the task control block will be created.
1657 @param[out] Tcb The newly created task control block.
1659 @retval EFI_SUCCESS The task control block is created.
1660 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1661 @retval EFI_NOT_READY The target can not accept new commands.
1665 IN ISCSI_CONNECTION
*Conn
,
1669 ISCSI_SESSION
*Session
;
1672 ASSERT (Tcb
!= NULL
);
1674 Session
= Conn
->Session
;
1676 if (ISCSI_SEQ_GT (Session
->CmdSN
, Session
->MaxCmdSN
)) {
1677 return EFI_NOT_READY
;
1680 NewTcb
= AllocateZeroPool (sizeof (ISCSI_TCB
));
1681 if (NewTcb
== NULL
) {
1682 return EFI_OUT_OF_RESOURCES
;
1685 InitializeListHead (&NewTcb
->Link
);
1687 NewTcb
->SoFarInOrder
= TRUE
;
1688 NewTcb
->InitiatorTaskTag
= Session
->InitiatorTaskTag
;
1689 NewTcb
->CmdSN
= Session
->CmdSN
;
1690 NewTcb
->Conn
= Conn
;
1692 InsertTailList (&Session
->TcbList
, &NewTcb
->Link
);
1695 // Advance the initiator task tag.
1697 Session
->InitiatorTaskTag
++;
1706 Delete the tcb from the connection and destroy it.
1708 @param[in] Tcb The tcb to delete.
1715 RemoveEntryList (&Tcb
->Link
);
1721 Find the task control block by the initator task tag.
1723 @param[in] TcbList The tcb list.
1724 @param[in] InitiatorTaskTag The initiator task tag.
1726 @return The task control block found.
1730 IN LIST_ENTRY
*TcbList
,
1731 IN UINT32 InitiatorTaskTag
1739 NET_LIST_FOR_EACH (Entry
, TcbList
) {
1740 Tcb
= NET_LIST_USER_STRUCT (Entry
, ISCSI_TCB
, Link
);
1742 if (Tcb
->InitiatorTaskTag
== InitiatorTaskTag
) {
1753 Create a data segment, pad it and calculate the CRC if needed.
1755 @param[in] Data The data to fill into the data segment.
1756 @param[in] Len Length of the data.
1757 @param[in] DataDigest Whether to calculate CRC for this data segment.
1759 @return The net buffer wrapping the data segment.
1762 IScsiNewDataSegment (
1765 IN BOOLEAN DataDigest
1768 NET_FRAGMENT Fragment
[2];
1769 UINT32 FragmentCount
;
1773 Fragment
[0].Len
= Len
;
1774 Fragment
[0].Bulk
= Data
;
1776 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1778 Fragment
[1].Len
= PadLen
;
1779 Fragment
[1].Bulk
= (UINT8
*) &mDataSegPad
;
1786 DataSeg
= NetbufFromExt (&Fragment
[0], FragmentCount
, 0, 0, IScsiNbufExtFree
, NULL
);
1792 Create a iSCSI SCSI command PDU to encapsulate the command issued
1793 by SCSI through the EXT SCSI PASS THRU Protocol.
1795 @param[in] Packet The EXT SCSI PASS THRU request packet containing the SCSI command.
1796 @param[in] Lun The LUN.
1797 @param[in] Tcb The tcb assocated with this SCSI command.
1799 @return The created iSCSI SCSI command PDU.
1800 @retval NULL Other errors as indicated.
1803 IScsiNewScsiCmdPdu (
1804 IN EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
1809 LIST_ENTRY
*NbufList
;
1813 SCSI_COMMAND
*ScsiCmd
;
1816 ISCSI_ADDITIONAL_HEADER
*Header
;
1817 ISCSI_BI_EXP_READ_DATA_LEN_AHS
*BiExpReadDataLenAHS
;
1818 ISCSI_SESSION
*Session
;
1819 UINT32 ImmediateDataLen
;
1823 if (Packet
->DataDirection
== DataBi
) {
1825 // Bi directional Read/Write command, the bidirectional expected
1826 // read data length AHS is required.
1828 AHSLength
+= sizeof (ISCSI_BI_EXP_READ_DATA_LEN_AHS
);
1831 if (Packet
->CdbLength
> 16) {
1833 // The CDB exceeds 16 bytes, an extended CDB AHS is required.
1835 AHSLength
= (UINT8
) (AHSLength
+ (ISCSI_ROUNDUP (Packet
->CdbLength
- 16) + sizeof (ISCSI_ADDITIONAL_HEADER
)));
1838 Length
= sizeof (SCSI_COMMAND
) + AHSLength
;
1839 PduHeader
= NetbufAlloc (Length
);
1840 if (PduHeader
== NULL
) {
1844 ScsiCmd
= (SCSI_COMMAND
*) NetbufAllocSpace (PduHeader
, Length
, NET_BUF_TAIL
);
1845 if (ScsiCmd
== NULL
) {
1846 NetbufFree (PduHeader
);
1849 Header
= (ISCSI_ADDITIONAL_HEADER
*) (ScsiCmd
+ 1);
1851 ZeroMem (ScsiCmd
, Length
);
1853 ISCSI_SET_OPCODE (ScsiCmd
, ISCSI_OPCODE_SCSI_CMD
, 0);
1854 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_TASK_ATTR_SIMPLE
);
1857 // Set the READ/WRITE flags according to the IO type of this request.
1859 switch (Packet
->DataDirection
) {
1861 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
);
1862 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->InTransferLength
);
1866 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_WRITE
);
1867 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
1871 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
| SCSI_CMD_PDU_FLAG_WRITE
);
1872 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
1875 // Fill the bidirectional expected read data length AHS.
1877 BiExpReadDataLenAHS
= (ISCSI_BI_EXP_READ_DATA_LEN_AHS
*) Header
;
1878 Header
= (ISCSI_ADDITIONAL_HEADER
*) (BiExpReadDataLenAHS
+ 1);
1880 BiExpReadDataLenAHS
->Length
= NTOHS (5);
1881 BiExpReadDataLenAHS
->Type
= ISCSI_AHS_TYPE_BI_EXP_READ_DATA_LEN
;
1882 BiExpReadDataLenAHS
->ExpReadDataLength
= NTOHL (Packet
->InTransferLength
);
1887 ScsiCmd
->TotalAHSLength
= AHSLength
;
1888 CopyMem (ScsiCmd
->Lun
, &Lun
, sizeof (ScsiCmd
->Lun
));
1889 ScsiCmd
->InitiatorTaskTag
= NTOHL (Tcb
->InitiatorTaskTag
);
1890 ScsiCmd
->CmdSN
= NTOHL (Tcb
->CmdSN
);
1891 ScsiCmd
->ExpStatSN
= NTOHL (Tcb
->Conn
->ExpStatSN
);
1893 CopyMem (ScsiCmd
->Cdb
, Packet
->Cdb
, sizeof (ScsiCmd
->Cdb
));
1895 if (Packet
->CdbLength
> 16) {
1896 Header
->Length
= NTOHS ((UINT16
) (Packet
->CdbLength
- 15));
1897 Header
->Type
= ISCSI_AHS_TYPE_EXT_CDB
;
1899 CopyMem (Header
+ 1, (UINT8
*) Packet
->Cdb
+ 16, Packet
->CdbLength
- 16);
1903 Session
= Tcb
->Conn
->Session
;
1904 ImmediateDataLen
= 0;
1906 if (Session
->ImmediateData
&& (Packet
->OutTransferLength
!= 0)) {
1908 // Send immediate data in this SCSI Command PDU. The length of the immeidate
1909 // data is the minimum of FirstBurstLength, the data length to be xfered and
1910 // the MaxRecvdataSegmentLength on this connection.
1912 ImmediateDataLen
= MIN (Session
->FirstBurstLength
, Packet
->OutTransferLength
);
1913 ImmediateDataLen
= MIN (ImmediateDataLen
, Tcb
->Conn
->MaxRecvDataSegmentLength
);
1916 // Update the data segment length in the PDU header.
1918 ISCSI_SET_DATASEG_LEN (ScsiCmd
, ImmediateDataLen
);
1921 // Create the data segment.
1923 DataSeg
= IScsiNewDataSegment ((UINT8
*) Packet
->OutDataBuffer
, ImmediateDataLen
, FALSE
);
1924 if (DataSeg
== NULL
) {
1925 NetbufFree (PduHeader
);
1930 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
1931 if (NbufList
== NULL
) {
1932 NetbufFree (PduHeader
);
1933 NetbufFree (DataSeg
);
1939 InitializeListHead (NbufList
);
1940 InsertTailList (NbufList
, &PduHeader
->List
);
1941 InsertTailList (NbufList
, &DataSeg
->List
);
1943 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1945 IScsiFreeNbufList (NbufList
);
1949 if (Session
->InitialR2T
||
1950 (ImmediateDataLen
== Session
->FirstBurstLength
) ||
1951 (ImmediateDataLen
== Packet
->OutTransferLength
)
1954 // Unsolicited data out sequence is not allowed,
1955 // or FirstBurstLength data is already sent out by immediate data
1956 // or all the OUT data accompany this SCSI packet is sent as
1957 // immediate data, the final flag should be set on this SCSI Command
1960 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_BHS_FLAG_FINAL
);
1969 Create a new iSCSI SCSI Data Out PDU.
1971 @param[in] Data The data to put into the Data Out PDU.
1972 @param[in] Len Length of the data.
1973 @param[in] DataSN The DataSN of the Data Out PDU.
1974 @param[in] Tcb The task control block of this Data Out PDU.
1975 @param[in] Lun The LUN.
1977 @return The net buffer wrapping the Data Out PDU.
1978 @retval NULL Other errors as indicated.
1981 IScsiNewDataOutPdu (
1989 LIST_ENTRY
*NbufList
;
1993 ISCSI_SCSI_DATA_OUT
*DataOutHdr
;
1994 ISCSI_XFER_CONTEXT
*XferContext
;
1996 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
1997 if (NbufList
== NULL
) {
2001 InitializeListHead (NbufList
);
2004 // Allocate memory for the BHS.
2006 PduHdr
= NetbufAlloc (sizeof (ISCSI_SCSI_DATA_OUT
));
2007 if (PduHdr
== NULL
) {
2008 FreePool (NbufList
);
2012 // Insert the BHS into the buffer list.
2014 InsertTailList (NbufList
, &PduHdr
->List
);
2016 DataOutHdr
= (ISCSI_SCSI_DATA_OUT
*) NetbufAllocSpace (PduHdr
, sizeof (ISCSI_SCSI_DATA_OUT
), NET_BUF_TAIL
);
2017 ASSERT (DataOutHdr
!= NULL
);
2018 XferContext
= &Tcb
->XferContext
;
2020 ZeroMem (DataOutHdr
, sizeof (ISCSI_SCSI_DATA_OUT
));
2023 // Set the flags and fields of the Data Out PDU BHS.
2025 ISCSI_SET_OPCODE (DataOutHdr
, ISCSI_OPCODE_SCSI_DATA_OUT
, 0);
2026 ISCSI_SET_DATASEG_LEN (DataOutHdr
, Len
);
2028 DataOutHdr
->InitiatorTaskTag
= HTONL (Tcb
->InitiatorTaskTag
);
2029 DataOutHdr
->TargetTransferTag
= HTONL (XferContext
->TargetTransferTag
);
2030 DataOutHdr
->ExpStatSN
= HTONL (Tcb
->Conn
->ExpStatSN
);
2031 DataOutHdr
->DataSN
= HTONL (DataSN
);
2032 DataOutHdr
->BufferOffset
= HTONL (XferContext
->Offset
);
2034 if (XferContext
->TargetTransferTag
!= ISCSI_RESERVED_TAG
) {
2035 CopyMem (&DataOutHdr
->Lun
, &Lun
, sizeof (DataOutHdr
->Lun
));
2038 // Build the data segment for this Data Out PDU.
2040 DataSeg
= IScsiNewDataSegment (Data
, Len
, FALSE
);
2041 if (DataSeg
== NULL
) {
2042 IScsiFreeNbufList (NbufList
);
2046 // Put the data segment into the buffer list and combine it with the BHS
2047 // into a full Data Out PDU.
2049 InsertTailList (NbufList
, &DataSeg
->List
);
2050 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
2052 IScsiFreeNbufList (NbufList
);
2059 Generate a consecutive sequence of iSCSI SCSI Data Out PDUs.
2061 @param[in] Data The data which will be carried by the sequence of iSCSI SCSI Data Out PDUs.
2062 @param[in] Tcb The task control block of the data to send out.
2063 @param[in] Lun The LUN the data will be sent to.
2065 @return A list of net buffers with each of them wraps an iSCSI SCSI Data Out PDU.
2066 @retval NULL Other errors as indicated.
2069 IScsiGenerateDataOutPduSequence (
2075 LIST_ENTRY
*PduList
;
2078 NET_BUF
*DataOutPdu
;
2079 ISCSI_CONNECTION
*Conn
;
2080 ISCSI_XFER_CONTEXT
*XferContext
;
2081 UINT8
*DataOutPacket
;
2083 PduList
= AllocatePool (sizeof (LIST_ENTRY
));
2084 if (PduList
== NULL
) {
2088 InitializeListHead (PduList
);
2093 XferContext
= &Tcb
->XferContext
;
2095 while (XferContext
->DesiredLength
> 0) {
2097 // Determine the length of data this Data Out PDU can carry.
2099 DataLen
= MIN (XferContext
->DesiredLength
, Conn
->MaxRecvDataSegmentLength
);
2102 // Create a Data Out PDU.
2104 DataOutPdu
= IScsiNewDataOutPdu (Data
, DataLen
, DataSN
, Tcb
, Lun
);
2105 if (DataOutPdu
== NULL
) {
2106 IScsiFreeNbufList (PduList
);
2112 InsertTailList (PduList
, &DataOutPdu
->List
);
2115 // Update the context and DataSN.
2117 XferContext
->Offset
+= DataLen
;
2118 XferContext
->DesiredLength
-= DataLen
;
2123 // Set the F bit for the last data out PDU in this sequence.
2125 DataOutPacket
= NetbufGetByte (DataOutPdu
, 0, NULL
);
2126 if (DataOutPacket
== NULL
) {
2127 IScsiFreeNbufList (PduList
);
2132 ISCSI_SET_FLAG (DataOutPacket
, ISCSI_BHS_FLAG_FINAL
);
2140 Send the Data in a sequence of Data Out PDUs one by one.
2142 @param[in] Data The data to carry by Data Out PDUs.
2143 @param[in] Lun The LUN the data will be sent to.
2144 @param[in] Tcb The task control block.
2146 @retval EFI_SUCCES The data is sent out to the LUN.
2147 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2148 @retval Others Other errors as indicated.
2151 IScsiSendDataOutPduSequence (
2157 LIST_ENTRY
*DataOutPduList
;
2163 // Generate the Data Out PDU sequence.
2165 DataOutPduList
= IScsiGenerateDataOutPduSequence (Data
, Tcb
, Lun
);
2166 if (DataOutPduList
== NULL
) {
2167 return EFI_OUT_OF_RESOURCES
;
2170 Status
= EFI_SUCCESS
;
2173 // Send the Data Out PDU's one by one.
2175 NET_LIST_FOR_EACH (Entry
, DataOutPduList
) {
2176 Pdu
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
2178 Status
= Tcp4IoTransmit (&Tcb
->Conn
->Tcp4Io
, Pdu
);
2179 if (EFI_ERROR (Status
)) {
2184 IScsiFreeNbufList (DataOutPduList
);
2190 Process the received iSCSI SCSI Data In PDU.
2192 @param[in] Pdu The Data In PDU received.
2193 @param[in] Tcb The task control block.
2194 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2196 @retval EFI_SUCCES The check on the Data IN PDU is passed and some update
2198 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2199 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2200 @retval Others Other errors as indicated.
2206 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2209 ISCSI_SCSI_DATA_IN
*DataInHdr
;
2212 DataInHdr
= (ISCSI_SCSI_DATA_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2213 if (DataInHdr
== NULL
) {
2214 return EFI_PROTOCOL_ERROR
;
2217 DataInHdr
->InitiatorTaskTag
= NTOHL (DataInHdr
->InitiatorTaskTag
);
2218 DataInHdr
->ExpCmdSN
= NTOHL (DataInHdr
->ExpCmdSN
);
2219 DataInHdr
->MaxCmdSN
= NTOHL (DataInHdr
->MaxCmdSN
);
2220 DataInHdr
->DataSN
= NTOHL (DataInHdr
->DataSN
);
2223 // Check the DataSN.
2225 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, DataInHdr
->DataSN
);
2226 if (EFI_ERROR (Status
)) {
2230 if (DataInHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2231 return EFI_PROTOCOL_ERROR
;
2234 // Update the command related sequence numbers.
2236 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, DataInHdr
->MaxCmdSN
, DataInHdr
->ExpCmdSN
);
2238 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_STATUS_VALID
)) {
2239 if (!ISCSI_FLAG_ON (DataInHdr
, ISCSI_BHS_FLAG_FINAL
)) {
2241 // The S bit is on but the F bit is off.
2243 return EFI_PROTOCOL_ERROR
;
2246 Tcb
->StatusXferd
= TRUE
;
2248 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_OVERFLOW
| SCSI_DATA_IN_PDU_FLAG_UNDERFLOW
)) {
2250 // Underflow and Overflow are mutual flags.
2252 return EFI_PROTOCOL_ERROR
;
2255 // S bit is on, the StatSN is valid.
2257 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NTOHL (DataInHdr
->StatSN
));
2258 if (EFI_ERROR (Status
)) {
2262 Packet
->HostAdapterStatus
= 0;
2263 Packet
->TargetStatus
= DataInHdr
->Status
;
2265 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2266 Packet
->InTransferLength
+= NTOHL (DataInHdr
->ResidualCount
);
2267 Status
= EFI_BAD_BUFFER_SIZE
;
2270 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2271 Packet
->InTransferLength
-= NTOHL (DataInHdr
->ResidualCount
);
2279 Process the received iSCSI R2T PDU.
2281 @param[in] Pdu The R2T PDU received.
2282 @param[in] Tcb The task control block.
2283 @param[in] Lun The Lun.
2284 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2286 @retval EFI_SUCCES The R2T PDU is valid and the solicited data is sent out.
2287 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2288 @retval Others Other errors as indicated.
2295 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2298 ISCSI_READY_TO_TRANSFER
*R2THdr
;
2300 ISCSI_XFER_CONTEXT
*XferContext
;
2303 R2THdr
= (ISCSI_READY_TO_TRANSFER
*) NetbufGetByte (Pdu
, 0, NULL
);
2304 if (R2THdr
== NULL
) {
2305 return EFI_PROTOCOL_ERROR
;
2308 R2THdr
->InitiatorTaskTag
= NTOHL (R2THdr
->InitiatorTaskTag
);
2309 R2THdr
->TargetTransferTag
= NTOHL (R2THdr
->TargetTransferTag
);
2310 R2THdr
->StatSN
= NTOHL (R2THdr
->StatSN
);
2311 R2THdr
->R2TSeqNum
= NTOHL (R2THdr
->R2TSeqNum
);
2312 R2THdr
->BufferOffset
= NTOHL (R2THdr
->BufferOffset
);
2313 R2THdr
->DesiredDataTransferLength
= NTOHL (R2THdr
->DesiredDataTransferLength
);
2315 if ((R2THdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) || !ISCSI_SEQ_EQ (R2THdr
->StatSN
, Tcb
->Conn
->ExpStatSN
)) {
2316 return EFI_PROTOCOL_ERROR
;;
2319 // Check the sequence number.
2321 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, R2THdr
->R2TSeqNum
);
2322 if (EFI_ERROR (Status
)) {
2326 XferContext
= &Tcb
->XferContext
;
2327 XferContext
->TargetTransferTag
= R2THdr
->TargetTransferTag
;
2328 XferContext
->Offset
= R2THdr
->BufferOffset
;
2329 XferContext
->DesiredLength
= R2THdr
->DesiredDataTransferLength
;
2331 if (((XferContext
->Offset
+ XferContext
->DesiredLength
) > Packet
->OutTransferLength
) ||
2332 (XferContext
->DesiredLength
> Tcb
->Conn
->Session
->MaxBurstLength
)
2334 return EFI_PROTOCOL_ERROR
;
2337 // Send the data solicited by this R2T.
2339 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2340 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2346 Process the received iSCSI SCSI Response PDU.
2348 @param[in] Pdu The Response PDU received.
2349 @param[in] Tcb The task control block.
2350 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2352 @retval EFI_SUCCES The Response PDU is processed.
2353 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2354 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2355 @retval Others Other errors as indicated.
2358 IScsiOnScsiRspRcvd (
2361 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2364 SCSI_RESPONSE
*ScsiRspHdr
;
2365 ISCSI_SENSE_DATA
*SenseData
;
2369 ScsiRspHdr
= (SCSI_RESPONSE
*) NetbufGetByte (Pdu
, 0, NULL
);
2370 if (ScsiRspHdr
== NULL
) {
2371 return EFI_PROTOCOL_ERROR
;
2374 ScsiRspHdr
->InitiatorTaskTag
= NTOHL (ScsiRspHdr
->InitiatorTaskTag
);
2375 if (ScsiRspHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2376 return EFI_PROTOCOL_ERROR
;
2379 ScsiRspHdr
->StatSN
= NTOHL (ScsiRspHdr
->StatSN
);
2381 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, ScsiRspHdr
->StatSN
);
2382 if (EFI_ERROR (Status
)) {
2386 ScsiRspHdr
->MaxCmdSN
= NTOHL (ScsiRspHdr
->MaxCmdSN
);
2387 ScsiRspHdr
->ExpCmdSN
= NTOHL (ScsiRspHdr
->ExpCmdSN
);
2388 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, ScsiRspHdr
->MaxCmdSN
, ScsiRspHdr
->ExpCmdSN
);
2390 Tcb
->StatusXferd
= TRUE
;
2392 Packet
->HostAdapterStatus
= ScsiRspHdr
->Response
;
2393 if (Packet
->HostAdapterStatus
!= ISCSI_SERVICE_RSP_COMMAND_COMPLETE_AT_TARGET
) {
2397 Packet
->TargetStatus
= ScsiRspHdr
->Status
;
2399 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
| SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
) ||
2400 ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
| SCSI_RSP_PDU_FLAG_UNDERFLOW
)
2402 return EFI_PROTOCOL_ERROR
;
2405 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
)) {
2406 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2407 Status
= EFI_BAD_BUFFER_SIZE
;
2410 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
)) {
2411 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2414 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2415 if (Packet
->DataDirection
== DataIn
) {
2416 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2418 Packet
->OutTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2421 Status
= EFI_BAD_BUFFER_SIZE
;
2424 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2425 if (Packet
->DataDirection
== DataIn
) {
2426 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2428 Packet
->OutTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2432 DataSegLen
= ISCSI_GET_DATASEG_LEN (ScsiRspHdr
);
2433 if (DataSegLen
!= 0) {
2434 SenseData
= (ISCSI_SENSE_DATA
*) NetbufGetByte (Pdu
, sizeof (SCSI_RESPONSE
), NULL
);
2435 if (SenseData
== NULL
) {
2436 return EFI_PROTOCOL_ERROR
;
2439 SenseData
->Length
= NTOHS (SenseData
->Length
);
2441 Packet
->SenseDataLength
= (UINT8
) MIN (SenseData
->Length
, Packet
->SenseDataLength
);
2442 if (Packet
->SenseDataLength
!= 0) {
2443 CopyMem (Packet
->SenseData
, &SenseData
->Data
[0], Packet
->SenseDataLength
);
2446 Packet
->SenseDataLength
= 0;
2453 Process the received NOP In PDU.
2455 @param[in] Pdu The NOP In PDU received.
2456 @param[in] Tcb The task control block.
2458 @retval EFI_SUCCES The NOP In PDU is processed and the related sequence
2459 numbers are updated.
2460 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2468 ISCSI_NOP_IN
*NopInHdr
;
2471 NopInHdr
= (ISCSI_NOP_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2472 if (NopInHdr
== NULL
) {
2473 return EFI_PROTOCOL_ERROR
;
2476 NopInHdr
->StatSN
= NTOHL (NopInHdr
->StatSN
);
2477 NopInHdr
->ExpCmdSN
= NTOHL (NopInHdr
->ExpCmdSN
);
2478 NopInHdr
->MaxCmdSN
= NTOHL (NopInHdr
->MaxCmdSN
);
2480 if (NopInHdr
->InitiatorTaskTag
== ISCSI_RESERVED_TAG
) {
2481 if (NopInHdr
->StatSN
!= Tcb
->Conn
->ExpStatSN
) {
2482 return EFI_PROTOCOL_ERROR
;
2485 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NopInHdr
->StatSN
);
2486 if (EFI_ERROR (Status
)) {
2491 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, NopInHdr
->MaxCmdSN
, NopInHdr
->ExpCmdSN
);
2497 Execute the SCSI command issued through the EXT SCSI PASS THRU protocol.
2499 @param[in] PassThru The EXT SCSI PASS THRU protocol.
2500 @param[in] Target The target ID.
2501 @param[in] Lun The LUN.
2502 @param[in, out] Packet The request packet containing IO request, SCSI command
2503 buffer and buffers to read/write.
2505 @retval EFI_SUCCES The SCSI command is executed and the result is updated to
2507 @retval EFI_DEVICE_ERROR Session state was not as required.
2508 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2509 @retval EFI_NOT_READY The target can not accept new commands.
2510 @retval Others Other errors as indicated.
2513 IScsiExecuteScsiCommand (
2514 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*PassThru
,
2517 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2521 ISCSI_DRIVER_DATA
*Private
;
2522 ISCSI_SESSION
*Session
;
2523 EFI_EVENT TimeoutEvent
;
2524 ISCSI_CONNECTION
*Conn
;
2527 ISCSI_XFER_CONTEXT
*XferContext
;
2529 ISCSI_IN_BUFFER_CONTEXT InBufferContext
;
2533 Private
= ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru
);
2534 Session
= &Private
->Session
;
2535 Status
= EFI_SUCCESS
;
2537 TimeoutEvent
= NULL
;
2540 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
2541 Status
= EFI_DEVICE_ERROR
;
2545 Conn
= NET_LIST_USER_STRUCT_S (
2546 Session
->Conns
.ForwardLink
,
2549 ISCSI_CONNECTION_SIGNATURE
2552 if (Packet
->Timeout
!= 0) {
2553 Timeout
= MultU64x32 (Packet
->Timeout
, 2);
2556 Status
= IScsiNewTcb (Conn
, &Tcb
);
2557 if (EFI_ERROR (Status
)) {
2561 // Encapsulate the SCSI request packet into an iSCSI SCSI Command PDU.
2563 Pdu
= IScsiNewScsiCmdPdu (Packet
, Lun
, Tcb
);
2565 Status
= EFI_OUT_OF_RESOURCES
;
2569 XferContext
= &Tcb
->XferContext
;
2570 PduHdr
= NetbufGetByte (Pdu
, 0, NULL
);
2571 if (PduHdr
== NULL
) {
2572 Status
= EFI_PROTOCOL_ERROR
;
2576 XferContext
->Offset
= ISCSI_GET_DATASEG_LEN (PduHdr
);
2579 // Transmit the SCSI Command PDU.
2581 Status
= Tcp4IoTransmit (&Conn
->Tcp4Io
, Pdu
);
2585 if (EFI_ERROR (Status
)) {
2589 if (!Session
->InitialR2T
&&
2590 (XferContext
->Offset
< Session
->FirstBurstLength
) &&
2591 (XferContext
->Offset
< Packet
->OutTransferLength
)
2594 // Unsolicited Data-Out sequence is allowed, there is remaining SCSI
2595 // OUT data and the limit of FirstBurstLength is not reached.
2597 XferContext
->TargetTransferTag
= ISCSI_RESERVED_TAG
;
2598 XferContext
->DesiredLength
= MIN (
2599 Session
->FirstBurstLength
,
2600 Packet
->OutTransferLength
- XferContext
->Offset
2603 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2604 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2605 if (EFI_ERROR (Status
)) {
2610 InBufferContext
.InData
= (UINT8
*) Packet
->InDataBuffer
;
2611 InBufferContext
.InDataLen
= Packet
->InTransferLength
;
2613 while (!Tcb
->StatusXferd
) {
2615 // Start the timeout timer.
2618 Status
= gBS
->SetTimer (Conn
->TimeoutEvent
, TimerRelative
, Timeout
);
2619 if (EFI_ERROR (Status
)) {
2622 TimeoutEvent
= Conn
->TimeoutEvent
;
2625 // try to receive PDU from target.
2627 Status
= IScsiReceivePdu (Conn
, &Pdu
, &InBufferContext
, FALSE
, FALSE
, TimeoutEvent
);
2628 if (EFI_ERROR (Status
)) {
2632 PduHdr
= NetbufGetByte (Pdu
, 0, NULL
);
2633 if (PduHdr
== NULL
) {
2634 Status
= EFI_PROTOCOL_ERROR
;
2638 switch (ISCSI_GET_OPCODE (PduHdr
)) {
2639 case ISCSI_OPCODE_SCSI_DATA_IN
:
2640 Status
= IScsiOnDataInRcvd (Pdu
, Tcb
, Packet
);
2643 case ISCSI_OPCODE_R2T
:
2644 Status
= IScsiOnR2TRcvd (Pdu
, Tcb
, Lun
, Packet
);
2647 case ISCSI_OPCODE_SCSI_RSP
:
2648 Status
= IScsiOnScsiRspRcvd (Pdu
, Tcb
, Packet
);
2651 case ISCSI_OPCODE_NOP_IN
:
2652 Status
= IScsiOnNopInRcvd (Pdu
, Tcb
);
2655 case ISCSI_OPCODE_VENDOR_T0
:
2656 case ISCSI_OPCODE_VENDOR_T1
:
2657 case ISCSI_OPCODE_VENDOR_T2
:
2659 // These messages are vendor specific, skip them.
2664 Status
= EFI_PROTOCOL_ERROR
;
2670 if (EFI_ERROR (Status
)) {
2677 if (TimeoutEvent
!= NULL
) {
2678 gBS
->SetTimer (TimeoutEvent
, TimerCancel
, 0);
2689 Reinstate the session on some error.
2691 @param[in, out] Private The iSCSI driver data.
2693 @retval EFI_SUCCES The session is reinstated from some error.
2694 @retval Other Reinstatement failed.
2697 IScsiSessionReinstatement (
2698 IN OUT ISCSI_DRIVER_DATA
*Private
2701 ISCSI_SESSION
*Session
;
2704 Session
= &Private
->Session
;
2705 ASSERT (Session
->State
!= SESSION_STATE_FREE
);
2708 // Abort the session and re-init it.
2710 IScsiSessionAbort (Session
);
2711 IScsiSessionInit (Session
, TRUE
);
2716 Status
= IScsiSessionLogin (Private
);
2722 Initialize some session parameters before login.
2724 @param[in, out] Session The iSCSI session.
2725 @param[in] Recovery Whether the request is from a fresh new start or recovery.
2729 IN OUT ISCSI_SESSION
*Session
,
2734 Session
->Signature
= ISCSI_SESSION_SIGNATURE
;
2735 Session
->State
= SESSION_STATE_FREE
;
2737 InitializeListHead (&Session
->Conns
);
2738 InitializeListHead (&Session
->TcbList
);
2744 Session
->InitiatorTaskTag
= 1;
2745 Session
->NextCid
= 1;
2747 Session
->TargetPortalGroupTag
= 0;
2748 Session
->MaxConnections
= ISCSI_MAX_CONNS_PER_SESSION
;
2749 Session
->InitialR2T
= FALSE
;
2750 Session
->ImmediateData
= TRUE
;
2751 Session
->MaxBurstLength
= 262144;
2752 Session
->FirstBurstLength
= MAX_RECV_DATA_SEG_LEN_IN_FFP
;
2753 Session
->DefaultTime2Wait
= 2;
2754 Session
->DefaultTime2Retain
= 20;
2755 Session
->MaxOutstandingR2T
= DEFAULT_MAX_OUTSTANDING_R2T
;
2756 Session
->DataPDUInOrder
= TRUE
;
2757 Session
->DataSequenceInOrder
= TRUE
;
2758 Session
->ErrorRecoveryLevel
= 0;
2762 Abort the iSCSI session, that is, reset all the connection and free the
2765 @param[in, out] Session The iSCSI session.
2767 @retval EFI_SUCCES The session is aborted.
2771 IN OUT ISCSI_SESSION
*Session
2774 ISCSI_DRIVER_DATA
*Private
;
2775 ISCSI_CONNECTION
*Conn
;
2777 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
2781 ASSERT (!IsListEmpty (&Session
->Conns
));
2783 Private
= ISCSI_DRIVER_DATA_FROM_SESSION (Session
);
2785 while (!IsListEmpty (&Session
->Conns
)) {
2786 Conn
= NET_LIST_USER_STRUCT_S (
2787 Session
->Conns
.ForwardLink
,
2790 ISCSI_CONNECTION_SIGNATURE
2793 gBS
->CloseProtocol (
2794 Conn
->Tcp4Io
.Handle
,
2795 &gEfiTcp4ProtocolGuid
,
2797 Private
->ExtScsiPassThruHandle
2800 IScsiConnReset (Conn
);
2802 IScsiDetatchConnection (Conn
);
2803 IScsiDestroyConnection (Conn
);
2806 Session
->State
= SESSION_STATE_FAILED
;