2 The implementation of iSCSI protocol based on RFC3720.
4 Copyright (c) 2004 - 2011, 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
397 // Receive the iSCSI login response.
399 Status
= IScsiReceivePdu (Conn
, &Pdu
, NULL
, FALSE
, FALSE
, NULL
);
400 if (EFI_ERROR (Status
)) {
404 // A Login Response is received, process it.
406 ASSERT (Pdu
!= NULL
);
407 Status
= IScsiProcessLoginRsp (Conn
, Pdu
);
415 Add an iSCSI key-value pair as a string into the data segment of the Login Request PDU.
416 The DataSegmentLength and the actual size of the net buffer containing this PDU will be
419 @param[in, out] Pdu The iSCSI PDU whose data segment the key-value pair will
421 @param[in] Key The key name string.
422 @param[in] Value The value string.
424 @retval EFI_SUCCESS The key-valu pair is added to the PDU's datasegment and
425 the correspondence length fields are updated.
426 @retval EFI_OUT_OF_RESOURCES There is not enough space in the PDU to add the key-value
430 IScsiAddKeyValuePair (
440 ISCSI_LOGIN_REQUEST
*LoginReq
;
443 LoginReq
= (ISCSI_LOGIN_REQUEST
*) NetbufGetByte (Pdu
, 0, NULL
);
444 DataSegLen
= NTOH24 (LoginReq
->DataSegmentLength
);
446 KeyLen
= (UINT32
) AsciiStrLen (Key
);
447 ValueLen
= (UINT32
) AsciiStrLen (Value
);
450 // 1 byte for the key value separator '=' and 1 byte for the null
451 // delimiter after the value.
453 TotalLen
= KeyLen
+ 1 + ValueLen
+ 1;
456 // Allocate the space for the key-value pair.
458 Data
= (CHAR8
*)NetbufAllocSpace (Pdu
, TotalLen
, NET_BUF_TAIL
);
460 return EFI_OUT_OF_RESOURCES
;
465 CopyMem (Data
, Key
, KeyLen
);
474 CopyMem (Data
, Value
, ValueLen
);
480 // update the DataSegmentLength
482 ISCSI_SET_DATASEG_LEN (LoginReq
, DataSegLen
+ TotalLen
);
488 Prepare the iSCSI login request to be sent according to the current login status.
490 @param[in, out] Conn The connection in the iSCSI login phase.
492 @return The pointer to the net buffer containing the iSCSI login request built.
493 @retval Others Other errors as indicated.
496 IScsiPrepareLoginReq (
497 IN OUT ISCSI_CONNECTION
*Conn
500 ISCSI_SESSION
*Session
;
502 ISCSI_LOGIN_REQUEST
*LoginReq
;
505 Session
= Conn
->Session
;
507 Nbuf
= NetbufAlloc (sizeof (ISCSI_LOGIN_REQUEST
) + DEFAULT_MAX_RECV_DATA_SEG_LEN
);
512 LoginReq
= (ISCSI_LOGIN_REQUEST
*) NetbufAllocSpace (Nbuf
, sizeof (ISCSI_LOGIN_REQUEST
), NET_BUF_TAIL
);
513 ASSERT (LoginReq
!= NULL
);
514 ZeroMem (LoginReq
, sizeof (ISCSI_LOGIN_REQUEST
));
517 // Init the login request pdu
519 ISCSI_SET_OPCODE (LoginReq
, ISCSI_OPCODE_LOGIN_REQ
, ISCSI_REQ_IMMEDIATE
);
520 ISCSI_SET_STAGES (LoginReq
, Conn
->CurrentStage
, Conn
->NextStage
);
521 LoginReq
->VersionMax
= ISCSI_VERSION_MAX
;
522 LoginReq
->VersionMin
= ISCSI_VERSION_MIN
;
523 LoginReq
->Tsih
= HTONS (Session
->Tsih
);
524 LoginReq
->InitiatorTaskTag
= HTONL (Session
->InitiatorTaskTag
);
525 LoginReq
->Cid
= HTONS (Conn
->Cid
);
526 LoginReq
->CmdSN
= HTONL (Session
->CmdSN
);
529 // For the first Login Request on a coonection this is ExpStatSN for the
530 // old connection and this field is only valid if the Login Request restarts
532 // For subsequent Login Requests it is used to acknowledge the Login Responses
533 // with their increasing StatSN values.
535 LoginReq
->ExpStatSN
= HTONL (Conn
->ExpStatSN
);
536 CopyMem (LoginReq
->Isid
, Session
->Isid
, sizeof (LoginReq
->Isid
));
538 if (Conn
->PartialRspRcvd
) {
540 // A partial response, initiator must send an empty Login Request.
545 switch (Conn
->CurrentStage
) {
546 case ISCSI_SECURITY_NEGOTIATION
:
547 Status
= IScsiCHAPToSendReq (Conn
, Nbuf
);
550 case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
:
551 Status
= IScsiFillOpParams (Conn
, Nbuf
);
552 ISCSI_SET_FLAG (LoginReq
, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT
);
557 // something error happens...
559 Status
= EFI_DEVICE_ERROR
;
563 if (EFI_ERROR (Status
)) {
568 // Pad the data segment if needed.
570 IScsiPadSegment (Nbuf
, ISCSI_GET_DATASEG_LEN (LoginReq
));
572 // Check whether we will issue the stage transition signal?
574 Conn
->TransitInitiated
= (BOOLEAN
) ISCSI_FLAG_ON (LoginReq
, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT
);
581 Process the iSCSI Login Response.
583 @param[in, out] Conn The connection on which the iSCSI login response is received.
584 @param[in, out] Pdu The iSCSI login response PDU.
586 @retval EFI_SUCCESS The iSCSI login response PDU is processed and all check are passed.
587 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
588 @retval EFI_MEDIA_CHANGED Target is redirected.
589 @retval Others Other errors as indicated.
592 IScsiProcessLoginRsp (
593 IN OUT ISCSI_CONNECTION
*Conn
,
598 ISCSI_SESSION
*Session
;
599 ISCSI_LOGIN_RESPONSE
*LoginRsp
;
607 Session
= Conn
->Session
;
609 LoginRsp
= (ISCSI_LOGIN_RESPONSE
*) NetbufGetByte (Pdu
, 0, NULL
);
610 if (!ISCSI_CHECK_OPCODE (LoginRsp
, ISCSI_OPCODE_LOGIN_RSP
)) {
612 // It's not a Login Response
614 return EFI_PROTOCOL_ERROR
;
617 // Get the data segment if any.
619 DataSegLen
= ISCSI_GET_DATASEG_LEN (LoginRsp
);
620 if (DataSegLen
!= 0) {
621 DataSeg
= NetbufGetByte (Pdu
, sizeof (ISCSI_LOGIN_RESPONSE
), NULL
);
626 // Check the status class in the login response PDU.
628 switch (LoginRsp
->StatusClass
) {
629 case ISCSI_LOGIN_STATUS_SUCCESS
:
631 // Just break here, the response and the data segment will be processed later.
635 case ISCSI_LOGIN_STATUS_REDIRECTION
:
637 // The target may be moved to a different address
639 if (DataSeg
== NULL
) {
640 return EFI_PROTOCOL_ERROR
;
643 // Process the TargetAddress key-value strings in the data segment to update the
644 // target address info.
646 Status
= IScsiUpdateTargetAddress (Session
, (CHAR8
*)DataSeg
, DataSegLen
);
647 if (EFI_ERROR (Status
)) {
651 // Session will be restarted on this error status because the Target is
652 // redirected by this Login Response.
654 return EFI_MEDIA_CHANGED
;
658 // Initiator Error, Target Error, or any other undefined error code.
660 return EFI_PROTOCOL_ERROR
;
663 // The status is sucess, extract the wanted fields from the header segment.
665 Transit
= (BOOLEAN
) ISCSI_FLAG_ON (LoginRsp
, ISCSI_LOGIN_RSP_PDU_FLAG_TRANSIT
);
666 Continue
= (BOOLEAN
) ISCSI_FLAG_ON (LoginRsp
, ISCSI_LOGIN_RSP_PDU_FLAG_CONTINUE
);
668 CurrentStage
= (UINT8
) ISCSI_GET_CURRENT_STAGE (LoginRsp
);
669 NextStage
= (UINT8
) ISCSI_GET_NEXT_STAGE (LoginRsp
);
671 LoginRsp
->InitiatorTaskTag
= NTOHL (LoginRsp
->InitiatorTaskTag
);
673 if ((Transit
&& Continue
) ||
674 (CurrentStage
!= Conn
->CurrentStage
) ||
675 (!Conn
->TransitInitiated
&& Transit
) ||
676 (Transit
&& (NextStage
!= Conn
->NextStage
)) ||
677 (CompareMem (Session
->Isid
, LoginRsp
->Isid
, sizeof (LoginRsp
->Isid
)) != 0) ||
678 (LoginRsp
->InitiatorTaskTag
!= Session
->InitiatorTaskTag
)
681 // A Login Response with the C bit set to 1 MUST have the T bit set to 0;
682 // The CSG in the Login Response MUST be the same with the I-end of this connection;
683 // The T bit can't be 1 if the last Login Response sent by the initiator doesn't
684 // initiate the transistion;
685 // The NSG MUST be the same with the I-end of this connection if Transit is required.
686 // The ISID in the Login Response MUST be the same with this session.
688 return EFI_PROTOCOL_ERROR
;
691 LoginRsp
->StatSN
= NTOHL (LoginRsp
->StatSN
);
692 LoginRsp
->ExpCmdSN
= NTOHL (LoginRsp
->ExpCmdSN
);
693 LoginRsp
->MaxCmdSN
= NTOHL (LoginRsp
->MaxCmdSN
);
695 if ((Conn
->CurrentStage
== ISCSI_SECURITY_NEGOTIATION
) && (Conn
->CHAPStep
== ISCSI_CHAP_INITIAL
)) {
697 // If the Login Request is a leading Login Request, the target MUST use
698 // the value presented in CmdSN as the target value for ExpCmdSN.
700 if ((Session
->State
== SESSION_STATE_FREE
) && (Session
->CmdSN
!= LoginRsp
->ExpCmdSN
)) {
701 return EFI_PROTOCOL_ERROR
;
705 // It's the initial Login Response, initialize the local ExpStatSN, MaxCmdSN
708 Conn
->ExpStatSN
= LoginRsp
->StatSN
+ 1;
709 Session
->MaxCmdSN
= LoginRsp
->MaxCmdSN
;
710 Session
->ExpCmdSN
= LoginRsp
->ExpCmdSN
;
713 // Check the StatSN of this PDU
715 Status
= IScsiCheckSN (&Conn
->ExpStatSN
, LoginRsp
->StatSN
);
716 if (!EFI_ERROR (Status
)) {
718 // Update the MaxCmdSN and ExpCmdSN
720 IScsiUpdateCmdSN (Session
, LoginRsp
->MaxCmdSN
, LoginRsp
->ExpCmdSN
);
726 // Trim off the header segment.
728 NetbufTrim (Pdu
, sizeof (ISCSI_LOGIN_RESPONSE
), NET_BUF_HEAD
);
731 // Queue this login response first in case it's a partial response so that
732 // later when the full response list is received we can combine these scattered
733 // responses' data segment and then process it.
736 NetbufQueAppend (&Conn
->RspQue
, Pdu
);
738 Conn
->PartialRspRcvd
= Continue
;
741 // It's a partial response, have to wait for another or more Request/Response
742 // conversations to get the full response.
747 switch (CurrentStage
) {
748 case ISCSI_SECURITY_NEGOTIATION
:
750 // In security negotiation stage, let CHAP module handle it.
752 Status
= IScsiCHAPOnRspReceived (Conn
);
755 case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
:
757 // Response received with negotiation resonse on iSCSI parameters, check them.
759 Status
= IScsiCheckOpParams (Conn
);
764 // Should never get here.
766 Status
= EFI_PROTOCOL_ERROR
;
770 if (Transit
&& (Status
== EFI_SUCCESS
)) {
772 // Do the state transition.
774 Conn
->CurrentStage
= Conn
->NextStage
;
776 if (Conn
->CurrentStage
== ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
) {
777 Conn
->NextStage
= ISCSI_FULL_FEATURE_PHASE
;
780 // CurrentStage is iSCSI Full Feature, it's the Login-Final Response,
781 // get the TSIH from the Login Response.
783 Session
->Tsih
= NTOHS (LoginRsp
->Tsih
);
787 // Flush the response(s) received.
789 NetbufQueFlush (&Conn
->RspQue
);
795 Updated the target information according the data received in the iSCSI
796 login response with an target redirection status.
798 @param[in, out] Session The iSCSI session.
799 @param[in] Data The data segment which should contain the
800 TargetAddress key-value list.
801 @param[in] Len Length of the data.
803 @retval EFI_SUCCESS The target address is updated.
804 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
805 @retval EFI_NOT_FOUND The TargetAddress key is not found.
806 @retval Others Other errors as indicated.
809 IScsiUpdateTargetAddress (
810 IN OUT ISCSI_SESSION
*Session
,
815 LIST_ENTRY
*KeyValueList
;
816 CHAR8
*TargetAddress
;
821 KeyValueList
= IScsiBuildKeyValueList (Data
, Len
);
822 if (KeyValueList
== NULL
) {
823 return EFI_OUT_OF_RESOURCES
;
826 Status
= EFI_NOT_FOUND
;
829 TargetAddress
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_ADDRESS
);
830 if (TargetAddress
== NULL
) {
834 if (!NET_IS_DIGIT (TargetAddress
[0])) {
836 // The domainname of the target may be presented in three formats: a DNS host name,
837 // a dotted-decimal IPv4 address, or a bracketed IPv6 address. Only accept dotted
843 IpStr
= TargetAddress
;
845 while ((*TargetAddress
!= 0) && (*TargetAddress
!= ':') && (*TargetAddress
!= ',')) {
847 // NULL, ':' or ',' ends the IPv4 string.
852 if (*TargetAddress
== ',') {
854 // Comma and the portal group tag MUST be ommitted if the TargetAddress is sent
855 // as the result of a redirection.
858 } else if (*TargetAddress
== ':') {
859 *TargetAddress
= '\0';
863 Number
= AsciiStrDecimalToUintn (TargetAddress
);
864 if (Number
> 0xFFFF) {
867 Session
->ConfigData
.NvData
.TargetPort
= (UINT16
) Number
;
871 // The string only contains the IPv4 address. Use the well known port.
873 Session
->ConfigData
.NvData
.TargetPort
= ISCSI_WELL_KNOWN_PORT
;
876 // Update the target IP address.
878 Status
= IScsiAsciiStrToIp (IpStr
, &Session
->ConfigData
.NvData
.TargetIp
);
879 if (EFI_ERROR (Status
)) {
886 IScsiFreeKeyValueList (KeyValueList
);
892 The callback function to free the net buffer list.
894 @param[in] Arg The opaque parameter.
902 ASSERT (Arg
!= NULL
);
904 NetbufFreeList ((LIST_ENTRY
*) Arg
);
909 The callback function called in NetBufFree, it does nothing.
911 @param[in] Arg The opaque parameter.
922 Receive an iSCSI response PDU. An iSCSI response PDU contains an iSCSI PDU header and
923 an optional data segment. The two parts will be put into two blocks of buffers in the
924 net buffer. The digest check will be conducted in this function if needed and the digests
925 will be trimmed from the PDU buffer.
927 @param[in] Conn The iSCSI connection to receive data from.
928 @param[out] Pdu The received iSCSI pdu.
929 @param[in] Context The context used to describe information on the caller provided
930 buffer to receive data segment of the iSCSI pdu, it's optional.
931 @param[in] HeaderDigest Whether there will be header digest received.
932 @param[in] DataDigest Whether there will be data digest.
933 @param[in] TimeoutEvent The timeout event, it's optional.
935 @retval EFI_SUCCESS An iSCSI pdu is received.
936 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
937 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
938 @retval Others Other errors as indicated.
942 IN ISCSI_CONNECTION
*Conn
,
944 IN ISCSI_IN_BUFFER_CONTEXT
*Context
, OPTIONAL
945 IN BOOLEAN HeaderDigest
,
946 IN BOOLEAN DataDigest
,
947 IN EFI_EVENT TimeoutEvent OPTIONAL
950 LIST_ENTRY
*NbufList
;
957 NET_FRAGMENT Fragment
[2];
958 UINT32 FragmentCount
;
960 UINT32 PadAndCRC32
[2];
962 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
963 if (NbufList
== NULL
) {
964 return EFI_OUT_OF_RESOURCES
;
967 InitializeListHead (NbufList
);
970 // The header digest will be received together with the PDU header if exists.
972 Len
= sizeof (ISCSI_BASIC_HEADER
) + (HeaderDigest
? sizeof (UINT32
) : 0);
973 PduHdr
= NetbufAlloc (Len
);
974 if (PduHdr
== NULL
) {
976 return EFI_OUT_OF_RESOURCES
;
979 Header
= NetbufAllocSpace (PduHdr
, Len
, NET_BUF_TAIL
);
980 ASSERT (Header
!= NULL
);
981 InsertTailList (NbufList
, &PduHdr
->List
);
984 // First step, receive the BHS of the PDU.
986 Status
= Tcp4IoReceive (&Conn
->Tcp4Io
, PduHdr
, FALSE
, TimeoutEvent
);
987 if (EFI_ERROR (Status
)) {
993 // TODO: check the header-digest.
996 // Trim off the digest.
998 NetbufTrim (PduHdr
, sizeof (UINT32
), NET_BUF_TAIL
);
1001 Len
= ISCSI_GET_DATASEG_LEN (Header
);
1004 // No data segment.Form the pdu from a list of pdu segments.
1006 *Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1008 Status
= EFI_OUT_OF_RESOURCES
;
1014 // Get the length of the padding bytes of the data segment.
1016 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1018 switch (ISCSI_GET_OPCODE (Header
)) {
1019 case ISCSI_OPCODE_SCSI_DATA_IN
:
1021 // Try to use the buffer described by Context if the PDU is an
1022 // iSCSI SCSI data in pdu so as to reduce memory copy overhead.
1024 InDataOffset
= ISCSI_GET_BUFFER_OFFSET (Header
);
1025 if ((Context
== NULL
) || ((InDataOffset
+ Len
) > Context
->InDataLen
)) {
1026 Status
= EFI_PROTOCOL_ERROR
;
1030 Fragment
[0].Len
= Len
;
1031 Fragment
[0].Bulk
= Context
->InData
+ InDataOffset
;
1033 if (DataDigest
|| (PadLen
!= 0)) {
1035 // The data segment is padded, use two fragments to receive it.
1036 // The first to receive the useful data. The second to receive the padding.
1038 Fragment
[1].Len
= PadLen
+ (DataDigest
? sizeof (UINT32
) : 0);
1039 Fragment
[1].Bulk
= (UINT8
*)PadAndCRC32
+ (4 - PadLen
);
1045 DataSeg
= NetbufFromExt (&Fragment
[0], FragmentCount
, 0, 0, IScsiNbufExtFree
, NULL
);
1046 if (DataSeg
== NULL
) {
1047 Status
= EFI_OUT_OF_RESOURCES
;
1053 case ISCSI_OPCODE_SCSI_RSP
:
1054 case ISCSI_OPCODE_NOP_IN
:
1055 case ISCSI_OPCODE_LOGIN_RSP
:
1056 case ISCSI_OPCODE_TEXT_RSP
:
1057 case ISCSI_OPCODE_ASYNC_MSG
:
1058 case ISCSI_OPCODE_REJECT
:
1059 case ISCSI_OPCODE_VENDOR_T0
:
1060 case ISCSI_OPCODE_VENDOR_T1
:
1061 case ISCSI_OPCODE_VENDOR_T2
:
1063 // Allocate buffer to receive the data segment.
1065 Len
+= PadLen
+ (DataDigest
? sizeof (UINT32
) : 0);
1066 DataSeg
= NetbufAlloc (Len
);
1067 if (DataSeg
== NULL
) {
1068 Status
= EFI_OUT_OF_RESOURCES
;
1072 NetbufAllocSpace (DataSeg
, Len
, NET_BUF_TAIL
);
1076 Status
= EFI_PROTOCOL_ERROR
;
1080 InsertTailList (NbufList
, &DataSeg
->List
);
1083 // Receive the data segment with the data digest if any.
1085 Status
= Tcp4IoReceive (&Conn
->Tcp4Io
, DataSeg
, FALSE
, TimeoutEvent
);
1086 if (EFI_ERROR (Status
)) {
1092 // TODO: Check the data digest.
1094 NetbufTrim (DataSeg
, sizeof (UINT32
), NET_BUF_TAIL
);
1099 // Trim off the padding bytes in the data segment.
1101 NetbufTrim (DataSeg
, PadLen
, NET_BUF_TAIL
);
1105 // Form the pdu from a list of pdu segments.
1107 *Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1109 Status
= EFI_OUT_OF_RESOURCES
;
1114 if (EFI_ERROR (Status
)) {
1116 // Free the Nbufs in this NbufList and the NbufList itself.
1118 IScsiFreeNbufList (NbufList
);
1125 Check and get the result of the prameter negotiation.
1127 @param[in, out] Conn The connection in iSCSI login.
1129 @retval EFI_SUCCESS The parmeter check is passed and negotiation is finished.
1130 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
1131 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1134 IScsiCheckOpParams (
1135 IN OUT ISCSI_CONNECTION
*Conn
1139 LIST_ENTRY
*KeyValueList
;
1142 ISCSI_SESSION
*Session
;
1146 ASSERT (Conn
->RspQue
.BufNum
!= 0);
1148 Session
= Conn
->Session
;
1150 Len
= Conn
->RspQue
.BufSize
;
1151 Data
= AllocatePool (Len
);
1153 return EFI_OUT_OF_RESOURCES
;
1156 NetbufQueCopy (&Conn
->RspQue
, 0, Len
, (UINT8
*) Data
);
1158 Status
= EFI_PROTOCOL_ERROR
;
1161 // Extract the Key-Value pairs into a list.
1163 KeyValueList
= IScsiBuildKeyValueList (Data
, Len
);
1164 if (KeyValueList
== NULL
) {
1171 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_HEADER_DIGEST
);
1172 if (Value
== NULL
) {
1176 if (AsciiStrCmp (Value
, "CRC32") == 0) {
1177 if (Conn
->HeaderDigest
!= IScsiDigestCRC32
) {
1180 } else if (AsciiStrCmp (Value
, ISCSI_KEY_VALUE_NONE
) == 0) {
1181 Conn
->HeaderDigest
= IScsiDigestNone
;
1188 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_DIGEST
);
1189 if (Value
== NULL
) {
1193 if (AsciiStrCmp (Value
, "CRC32") == 0) {
1194 if (Conn
->DataDigest
!= IScsiDigestCRC32
) {
1197 } else if (AsciiStrCmp (Value
, ISCSI_KEY_VALUE_NONE
) == 0) {
1198 Conn
->DataDigest
= IScsiDigestNone
;
1203 // ErrorRecoveryLevel, result fuction is Minimum.
1205 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_ERROR_RECOVERY_LEVEL
);
1206 if (Value
== NULL
) {
1210 NumericValue
= AsciiStrDecimalToUintn (Value
);
1211 if (NumericValue
> 2) {
1215 Session
->ErrorRecoveryLevel
= (UINT8
) MIN (Session
->ErrorRecoveryLevel
, NumericValue
);
1218 // InitialR2T, result function is OR.
1220 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_INITIAL_R2T
);
1221 if (Value
== NULL
) {
1225 Session
->InitialR2T
= (BOOLEAN
) (Session
->InitialR2T
|| (AsciiStrCmp (Value
, "Yes") == 0));
1228 // ImmediateData, result function is AND.
1230 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_IMMEDIATE_DATA
);
1231 if (Value
== NULL
) {
1235 Session
->ImmediateData
= (BOOLEAN
) (Session
->ImmediateData
&& (AsciiStrCmp (Value
, "Yes") == 0));
1238 // MaxRecvDataSegmentLength is declarative.
1240 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
);
1241 if (Value
!= NULL
) {
1242 Conn
->MaxRecvDataSegmentLength
= (UINT32
) AsciiStrDecimalToUintn (Value
);
1245 // MaxBurstLength, result funtion is Mininum.
1247 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_BURST_LENGTH
);
1248 if (Value
== NULL
) {
1252 NumericValue
= AsciiStrDecimalToUintn (Value
);
1253 Session
->MaxBurstLength
= (UINT32
) MIN (Session
->MaxBurstLength
, NumericValue
);
1256 // FirstBurstLength, result function is Minimum. Irrelevant when InitialR2T=Yes and
1257 // ImmediateData=No.
1258 // This Key/Value is negotiation type.
1260 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_FIRST_BURST_LENGTH
);
1261 if (Value
== NULL
) {
1265 NumericValue
= AsciiStrDecimalToUintn (Value
);
1266 Session
->FirstBurstLength
= (UINT32
) MIN (Session
->FirstBurstLength
, NumericValue
);
1269 // MaxConnections, result function is Minimum.
1271 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_CONNECTIONS
);
1272 if (Value
== NULL
) {
1276 NumericValue
= AsciiStrDecimalToUintn (Value
);
1277 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1281 Session
->MaxConnections
= (UINT32
) MIN (Session
->MaxConnections
, NumericValue
);
1284 // DataPDUInOrder, result function is OR.
1286 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_PDU_IN_ORDER
);
1287 if (Value
== NULL
) {
1291 Session
->DataPDUInOrder
= (BOOLEAN
) (Session
->DataPDUInOrder
|| (AsciiStrCmp (Value
, "Yes") == 0));
1294 // DataSequenceInorder, result function is OR.
1296 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
);
1297 if (Value
== NULL
) {
1301 Session
->DataSequenceInOrder
= (BOOLEAN
) (Session
->DataSequenceInOrder
|| (AsciiStrCmp (Value
, "Yes") == 0));
1304 // DefaultTime2Wait, result function is Maximum.
1306 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2WAIT
);
1307 if (Value
== NULL
) {
1311 NumericValue
= AsciiStrDecimalToUintn (Value
);
1312 if (NumericValue
== 0) {
1313 Session
->DefaultTime2Wait
= 0;
1314 } else if (NumericValue
> 3600) {
1317 Session
->DefaultTime2Wait
= (UINT32
) MAX (Session
->DefaultTime2Wait
, NumericValue
);
1320 // DefaultTime2Retain, result function is Minimum.
1322 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2RETAIN
);
1323 if (Value
== NULL
) {
1327 NumericValue
= AsciiStrDecimalToUintn (Value
);
1328 if (NumericValue
== 0) {
1329 Session
->DefaultTime2Retain
= 0;
1330 } else if (NumericValue
> 3600) {
1333 Session
->DefaultTime2Retain
= (UINT32
) MIN (Session
->DefaultTime2Retain
, NumericValue
);
1336 // MaxOutstandingR2T, result function is Minimum.
1338 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_OUTSTANDING_R2T
);
1339 if (Value
== NULL
) {
1343 NumericValue
= AsciiStrDecimalToUintn (Value
);
1344 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1348 Session
->MaxOutstandingR2T
= (UINT16
) MIN (Session
->MaxOutstandingR2T
, NumericValue
);
1351 // Remove declarative key-value paris if any.
1353 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_SESSION_TYPE
);
1354 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_ALIAS
);
1355 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_PORTAL_GROUP_TAG
);
1357 if (IsListEmpty (KeyValueList
)) {
1359 // Succeed if no more keys in the list.
1361 Status
= EFI_SUCCESS
;
1366 IScsiFreeKeyValueList (KeyValueList
);
1374 Fill the oprational prameters.
1376 @param[in] Conn The connection in iSCSI login.
1377 @param[in, out] Pdu The iSCSI login request PDU to fill the parameters.
1379 @retval EFI_SUCCESS The parmeters are filled into the iSCSI login request PDU.
1383 IN ISCSI_CONNECTION
*Conn
,
1387 ISCSI_SESSION
*Session
;
1390 Session
= Conn
->Session
;
1392 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->HeaderDigest
== IScsiDigestCRC32
) ? "None,CRC32" : "None");
1393 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_HEADER_DIGEST
, Value
);
1395 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->DataDigest
== IScsiDigestCRC32
) ? "None,CRC32" : "None");
1396 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_DIGEST
, Value
);
1398 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->ErrorRecoveryLevel
);
1399 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_ERROR_RECOVERY_LEVEL
, Value
);
1401 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->InitialR2T
? "Yes" : "No");
1402 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_INITIAL_R2T
, Value
);
1404 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->ImmediateData
? "Yes" : "No");
1405 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_IMMEDIATE_DATA
, Value
);
1407 AsciiSPrint (Value
, sizeof (Value
), "%d", MAX_RECV_DATA_SEG_LEN_IN_FFP
);
1408 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
, Value
);
1410 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxBurstLength
);
1411 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_BURST_LENGTH
, Value
);
1413 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->FirstBurstLength
);
1414 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_FIRST_BURST_LENGTH
, Value
);
1416 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxConnections
);
1417 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_CONNECTIONS
, Value
);
1419 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataPDUInOrder
? "Yes" : "No");
1420 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_PDU_IN_ORDER
, Value
);
1422 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataSequenceInOrder
? "Yes" : "No");
1423 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
, Value
);
1425 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Wait
);
1426 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2WAIT
, Value
);
1428 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Retain
);
1429 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2RETAIN
, Value
);
1431 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxOutstandingR2T
);
1432 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_OUTSTANDING_R2T
, Value
);
1438 Pad the iSCSI AHS or data segment to an integer number of 4 byte words.
1440 @param[in, out] Pdu The iSCSI pdu which contains segments to pad.
1441 @param[in] Len The length of the last semgnet in the PDU.
1443 @retval EFI_SUCCESS The segment is padded or no need to pad it.
1444 @retval EFI_OUT_OF_RESOURCES There is not enough remaining free space to add the
1449 IN OUT NET_BUF
*Pdu
,
1456 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1459 Data
= NetbufAllocSpace (Pdu
, PadLen
, NET_BUF_TAIL
);
1461 return EFI_OUT_OF_RESOURCES
;
1464 ZeroMem (Data
, PadLen
);
1471 Build a key-value list from the data segment.
1473 @param[in] Data The data segment containing the key-value pairs.
1474 @param[in] Len Length of the data segment.
1476 @return The key-value list.
1477 @retval NULL Other errors as indicated.
1480 IScsiBuildKeyValueList (
1485 LIST_ENTRY
*ListHead
;
1486 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1488 ListHead
= AllocatePool (sizeof (LIST_ENTRY
));
1489 if (ListHead
== NULL
) {
1493 InitializeListHead (ListHead
);
1496 KeyValuePair
= AllocatePool (sizeof (ISCSI_KEY_VALUE_PAIR
));
1497 if (KeyValuePair
== NULL
) {
1501 InitializeListHead (&KeyValuePair
->List
);
1503 KeyValuePair
->Key
= Data
;
1505 while ((Len
> 0) && (*Data
!= '=')) {
1516 FreePool (KeyValuePair
);
1520 KeyValuePair
->Value
= Data
;
1522 InsertTailList (ListHead
, &KeyValuePair
->List
);;
1524 Data
+= AsciiStrLen (KeyValuePair
->Value
) + 1;
1525 Len
-= (UINT32
) AsciiStrLen (KeyValuePair
->Value
) + 1;
1532 IScsiFreeKeyValueList (ListHead
);
1538 Get the value string by the key name from the key-value list. If found,
1539 the key-value entry will be removed from the list.
1541 @param[in, out] KeyValueList The key-value list.
1542 @param[in] Key The key name to find.
1544 @return The value string.
1547 IScsiGetValueByKeyFromList (
1548 IN OUT LIST_ENTRY
*KeyValueList
,
1553 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1558 NET_LIST_FOR_EACH (Entry
, KeyValueList
) {
1559 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1561 if (AsciiStrCmp (KeyValuePair
->Key
, Key
) == 0) {
1562 Value
= KeyValuePair
->Value
;
1564 RemoveEntryList (&KeyValuePair
->List
);
1565 FreePool (KeyValuePair
);
1574 Free the key-value list.
1576 @param[in] KeyValueList The key-value list.
1579 IScsiFreeKeyValueList (
1580 IN LIST_ENTRY
*KeyValueList
1584 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1586 while (!IsListEmpty (KeyValueList
)) {
1587 Entry
= NetListRemoveHead (KeyValueList
);
1588 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1590 FreePool (KeyValuePair
);
1593 FreePool (KeyValueList
);
1597 Normalize the iSCSI name according to RFC.
1599 @param[in, out] Name The iSCSI name.
1600 @param[in] Len length of the iSCSI name.
1602 @retval EFI_SUCCESS The iSCSI name is valid and normalized.
1603 @retval EFI_PROTOCOL_ERROR The iSCSI name is mal-formatted or not in the IQN format.
1606 IScsiNormalizeName (
1613 for (Index
= 0; Index
< Len
; Index
++) {
1614 if (NET_IS_UPPER_CASE_CHAR (Name
[Index
])) {
1616 // Convert the upper-case characters to lower-case ones
1618 Name
[Index
] = (CHAR8
) (Name
[Index
] - 'A' + 'a');
1621 if (!NET_IS_LOWER_CASE_CHAR (Name
[Index
]) &&
1622 !NET_IS_DIGIT (Name
[Index
]) &&
1623 (Name
[Index
] != '-') &&
1624 (Name
[Index
] != '.') &&
1625 (Name
[Index
] != ':')
1628 // ASCII dash, dot, colon lower-case characters and digit characters
1631 return EFI_PROTOCOL_ERROR
;
1635 if ((Len
< 4) || (CompareMem (Name
, "iqn.", 4) != 0)) {
1637 // Only IQN format is accepted now.
1639 return EFI_PROTOCOL_ERROR
;
1646 Create an iSCSI task control block.
1648 @param[in] Conn The connection on which the task control block will be created.
1649 @param[out] Tcb The newly created task control block.
1651 @retval EFI_SUCCESS The task control block is created.
1652 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1653 @retval EFI_NOT_READY The target can not accept new commands.
1657 IN ISCSI_CONNECTION
*Conn
,
1661 ISCSI_SESSION
*Session
;
1664 ASSERT (Tcb
!= NULL
);
1666 Session
= Conn
->Session
;
1668 if (ISCSI_SEQ_GT (Session
->CmdSN
, Session
->MaxCmdSN
)) {
1669 return EFI_NOT_READY
;
1672 NewTcb
= AllocateZeroPool (sizeof (ISCSI_TCB
));
1673 if (NewTcb
== NULL
) {
1674 return EFI_OUT_OF_RESOURCES
;
1677 InitializeListHead (&NewTcb
->Link
);
1679 NewTcb
->SoFarInOrder
= TRUE
;
1680 NewTcb
->InitiatorTaskTag
= Session
->InitiatorTaskTag
;
1681 NewTcb
->CmdSN
= Session
->CmdSN
;
1682 NewTcb
->Conn
= Conn
;
1684 InsertTailList (&Session
->TcbList
, &NewTcb
->Link
);
1687 // Advance the initiator task tag.
1689 Session
->InitiatorTaskTag
++;
1698 Delete the tcb from the connection and destroy it.
1700 @param[in] Tcb The tcb to delete.
1707 RemoveEntryList (&Tcb
->Link
);
1713 Find the task control block by the initator task tag.
1715 @param[in] TcbList The tcb list.
1716 @param[in] InitiatorTaskTag The initiator task tag.
1718 @return The task control block found.
1722 IN LIST_ENTRY
*TcbList
,
1723 IN UINT32 InitiatorTaskTag
1731 NET_LIST_FOR_EACH (Entry
, TcbList
) {
1732 Tcb
= NET_LIST_USER_STRUCT (Entry
, ISCSI_TCB
, Link
);
1734 if (Tcb
->InitiatorTaskTag
== InitiatorTaskTag
) {
1745 Create a data segment, pad it and calculate the CRC if needed.
1747 @param[in] Data The data to fill into the data segment.
1748 @param[in] Len Length of the data.
1749 @param[in] DataDigest Whether to calculate CRC for this data segment.
1751 @return The net buffer wrapping the data segment.
1754 IScsiNewDataSegment (
1757 IN BOOLEAN DataDigest
1760 NET_FRAGMENT Fragment
[2];
1761 UINT32 FragmentCount
;
1765 Fragment
[0].Len
= Len
;
1766 Fragment
[0].Bulk
= Data
;
1768 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1770 Fragment
[1].Len
= PadLen
;
1771 Fragment
[1].Bulk
= (UINT8
*) &mDataSegPad
;
1778 DataSeg
= NetbufFromExt (&Fragment
[0], FragmentCount
, 0, 0, IScsiNbufExtFree
, NULL
);
1784 Create a iSCSI SCSI command PDU to encapsulate the command issued
1785 by SCSI through the EXT SCSI PASS THRU Protocol.
1787 @param[in] Packet The EXT SCSI PASS THRU request packet containing the SCSI command.
1788 @param[in] Lun The LUN.
1789 @param[in] Tcb The tcb assocated with this SCSI command.
1791 @return The created iSCSI SCSI command PDU.
1792 @retval NULL Other errors as indicated.
1795 IScsiNewScsiCmdPdu (
1796 IN EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
1801 LIST_ENTRY
*NbufList
;
1805 SCSI_COMMAND
*ScsiCmd
;
1808 ISCSI_ADDITIONAL_HEADER
*Header
;
1809 ISCSI_BI_EXP_READ_DATA_LEN_AHS
*BiExpReadDataLenAHS
;
1810 ISCSI_SESSION
*Session
;
1811 UINT32 ImmediateDataLen
;
1815 if (Packet
->DataDirection
== DataBi
) {
1817 // Bi directional Read/Write command, the bidirectional expected
1818 // read data length AHS is required.
1820 AHSLength
+= sizeof (ISCSI_BI_EXP_READ_DATA_LEN_AHS
);
1823 if (Packet
->CdbLength
> 16) {
1825 // The CDB exceeds 16 bytes, an extended CDB AHS is required.
1827 AHSLength
= (UINT8
) (AHSLength
+ (ISCSI_ROUNDUP (Packet
->CdbLength
- 16) + sizeof (ISCSI_ADDITIONAL_HEADER
)));
1830 Length
= sizeof (SCSI_COMMAND
) + AHSLength
;
1831 PduHeader
= NetbufAlloc (Length
);
1832 if (PduHeader
== NULL
) {
1836 ScsiCmd
= (SCSI_COMMAND
*) NetbufAllocSpace (PduHeader
, Length
, NET_BUF_TAIL
);
1837 if (ScsiCmd
== NULL
) {
1838 NetbufFree (PduHeader
);
1841 Header
= (ISCSI_ADDITIONAL_HEADER
*) (ScsiCmd
+ 1);
1843 ZeroMem (ScsiCmd
, Length
);
1845 ISCSI_SET_OPCODE (ScsiCmd
, ISCSI_OPCODE_SCSI_CMD
, 0);
1846 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_TASK_ATTR_SIMPLE
);
1849 // Set the READ/WRITE flags according to the IO type of this request.
1851 switch (Packet
->DataDirection
) {
1853 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
);
1854 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->InTransferLength
);
1858 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_WRITE
);
1859 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
1863 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
| SCSI_CMD_PDU_FLAG_WRITE
);
1864 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
1867 // Fill the bidirectional expected read data length AHS.
1869 BiExpReadDataLenAHS
= (ISCSI_BI_EXP_READ_DATA_LEN_AHS
*) Header
;
1870 Header
= (ISCSI_ADDITIONAL_HEADER
*) (BiExpReadDataLenAHS
+ 1);
1872 BiExpReadDataLenAHS
->Length
= NTOHS (5);
1873 BiExpReadDataLenAHS
->Type
= ISCSI_AHS_TYPE_BI_EXP_READ_DATA_LEN
;
1874 BiExpReadDataLenAHS
->ExpReadDataLength
= NTOHL (Packet
->InTransferLength
);
1879 ScsiCmd
->TotalAHSLength
= AHSLength
;
1880 CopyMem (ScsiCmd
->Lun
, &Lun
, sizeof (ScsiCmd
->Lun
));
1881 ScsiCmd
->InitiatorTaskTag
= NTOHL (Tcb
->InitiatorTaskTag
);
1882 ScsiCmd
->CmdSN
= NTOHL (Tcb
->CmdSN
);
1883 ScsiCmd
->ExpStatSN
= NTOHL (Tcb
->Conn
->ExpStatSN
);
1885 CopyMem (ScsiCmd
->Cdb
, Packet
->Cdb
, sizeof (ScsiCmd
->Cdb
));
1887 if (Packet
->CdbLength
> 16) {
1888 Header
->Length
= NTOHS ((UINT16
) (Packet
->CdbLength
- 15));
1889 Header
->Type
= ISCSI_AHS_TYPE_EXT_CDB
;
1891 CopyMem (Header
+ 1, (UINT8
*) Packet
->Cdb
+ 16, Packet
->CdbLength
- 16);
1895 Session
= Tcb
->Conn
->Session
;
1896 ImmediateDataLen
= 0;
1898 if (Session
->ImmediateData
&& (Packet
->OutTransferLength
!= 0)) {
1900 // Send immediate data in this SCSI Command PDU. The length of the immeidate
1901 // data is the minimum of FirstBurstLength, the data length to be xfered and
1902 // the MaxRecvdataSegmentLength on this connection.
1904 ImmediateDataLen
= MIN (Session
->FirstBurstLength
, Packet
->OutTransferLength
);
1905 ImmediateDataLen
= MIN (ImmediateDataLen
, Tcb
->Conn
->MaxRecvDataSegmentLength
);
1908 // Update the data segment length in the PDU header.
1910 ISCSI_SET_DATASEG_LEN (ScsiCmd
, ImmediateDataLen
);
1913 // Create the data segment.
1915 DataSeg
= IScsiNewDataSegment ((UINT8
*) Packet
->OutDataBuffer
, ImmediateDataLen
, FALSE
);
1916 if (DataSeg
== NULL
) {
1917 NetbufFree (PduHeader
);
1922 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
1923 if (NbufList
== NULL
) {
1924 NetbufFree (PduHeader
);
1925 NetbufFree (DataSeg
);
1931 InitializeListHead (NbufList
);
1932 InsertTailList (NbufList
, &PduHeader
->List
);
1933 InsertTailList (NbufList
, &DataSeg
->List
);
1935 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1937 IScsiFreeNbufList (NbufList
);
1941 if (Session
->InitialR2T
||
1942 (ImmediateDataLen
== Session
->FirstBurstLength
) ||
1943 (ImmediateDataLen
== Packet
->OutTransferLength
)
1946 // Unsolicited data out sequence is not allowed,
1947 // or FirstBurstLength data is already sent out by immediate data
1948 // or all the OUT data accompany this SCSI packet is sent as
1949 // immediate data, the final flag should be set on this SCSI Command
1952 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_BHS_FLAG_FINAL
);
1961 Create a new iSCSI SCSI Data Out PDU.
1963 @param[in] Data The data to put into the Data Out PDU.
1964 @param[in] Len Length of the data.
1965 @param[in] DataSN The DataSN of the Data Out PDU.
1966 @param[in] Tcb The task control block of this Data Out PDU.
1967 @param[in] Lun The LUN.
1969 @return The net buffer wrapping the Data Out PDU.
1970 @retval NULL Other errors as indicated.
1973 IScsiNewDataOutPdu (
1981 LIST_ENTRY
*NbufList
;
1985 ISCSI_SCSI_DATA_OUT
*DataOutHdr
;
1986 ISCSI_XFER_CONTEXT
*XferContext
;
1988 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
1989 if (NbufList
== NULL
) {
1993 InitializeListHead (NbufList
);
1996 // Allocate memory for the BHS.
1998 PduHdr
= NetbufAlloc (sizeof (ISCSI_SCSI_DATA_OUT
));
1999 if (PduHdr
== NULL
) {
2000 FreePool (NbufList
);
2004 // Insert the BHS into the buffer list.
2006 InsertTailList (NbufList
, &PduHdr
->List
);
2008 DataOutHdr
= (ISCSI_SCSI_DATA_OUT
*) NetbufAllocSpace (PduHdr
, sizeof (ISCSI_SCSI_DATA_OUT
), NET_BUF_TAIL
);
2009 ASSERT (DataOutHdr
!= NULL
);
2010 XferContext
= &Tcb
->XferContext
;
2012 ZeroMem (DataOutHdr
, sizeof (ISCSI_SCSI_DATA_OUT
));
2015 // Set the flags and fields of the Data Out PDU BHS.
2017 ISCSI_SET_OPCODE (DataOutHdr
, ISCSI_OPCODE_SCSI_DATA_OUT
, 0);
2018 ISCSI_SET_DATASEG_LEN (DataOutHdr
, Len
);
2020 DataOutHdr
->InitiatorTaskTag
= HTONL (Tcb
->InitiatorTaskTag
);
2021 DataOutHdr
->TargetTransferTag
= HTONL (XferContext
->TargetTransferTag
);
2022 DataOutHdr
->ExpStatSN
= HTONL (Tcb
->Conn
->ExpStatSN
);
2023 DataOutHdr
->DataSN
= HTONL (DataSN
);
2024 DataOutHdr
->BufferOffset
= HTONL (XferContext
->Offset
);
2026 if (XferContext
->TargetTransferTag
!= ISCSI_RESERVED_TAG
) {
2027 CopyMem (&DataOutHdr
->Lun
, &Lun
, sizeof (DataOutHdr
->Lun
));
2030 // Build the data segment for this Data Out PDU.
2032 DataSeg
= IScsiNewDataSegment (Data
, Len
, FALSE
);
2033 if (DataSeg
== NULL
) {
2034 IScsiFreeNbufList (NbufList
);
2038 // Put the data segment into the buffer list and combine it with the BHS
2039 // into a full Data Out PDU.
2041 InsertTailList (NbufList
, &DataSeg
->List
);
2042 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
2044 IScsiFreeNbufList (NbufList
);
2051 Generate a consecutive sequence of iSCSI SCSI Data Out PDUs.
2053 @param[in] Data The data which will be carried by the sequence of iSCSI SCSI Data Out PDUs.
2054 @param[in] Tcb The task control block of the data to send out.
2055 @param[in] Lun The LUN the data will be sent to.
2057 @return A list of net buffers with each of them wraps an iSCSI SCSI Data Out PDU.
2058 @retval NULL Other errors as indicated.
2061 IScsiGenerateDataOutPduSequence (
2067 LIST_ENTRY
*PduList
;
2070 NET_BUF
*DataOutPdu
;
2071 ISCSI_CONNECTION
*Conn
;
2072 ISCSI_XFER_CONTEXT
*XferContext
;
2074 PduList
= AllocatePool (sizeof (LIST_ENTRY
));
2075 if (PduList
== NULL
) {
2079 InitializeListHead (PduList
);
2084 XferContext
= &Tcb
->XferContext
;
2086 while (XferContext
->DesiredLength
> 0) {
2088 // Determine the length of data this Data Out PDU can carry.
2090 DataLen
= MIN (XferContext
->DesiredLength
, Conn
->MaxRecvDataSegmentLength
);
2093 // Create a Data Out PDU.
2095 DataOutPdu
= IScsiNewDataOutPdu (Data
, DataLen
, DataSN
, Tcb
, Lun
);
2096 if (DataOutPdu
== NULL
) {
2097 IScsiFreeNbufList (PduList
);
2103 InsertTailList (PduList
, &DataOutPdu
->List
);
2106 // Update the context and DataSN.
2108 XferContext
->Offset
+= DataLen
;
2109 XferContext
->DesiredLength
-= DataLen
;
2114 // Set the F bit for the last data out PDU in this sequence.
2116 ISCSI_SET_FLAG (NetbufGetByte (DataOutPdu
, 0, NULL
), ISCSI_BHS_FLAG_FINAL
);
2124 Send the Data in a sequence of Data Out PDUs one by one.
2126 @param[in] Data The data to carry by Data Out PDUs.
2127 @param[in] Lun The LUN the data will be sent to.
2128 @param[in] Tcb The task control block.
2130 @retval EFI_SUCCES The data is sent out to the LUN.
2131 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2132 @retval Others Other errors as indicated.
2135 IScsiSendDataOutPduSequence (
2141 LIST_ENTRY
*DataOutPduList
;
2147 // Generate the Data Out PDU sequence.
2149 DataOutPduList
= IScsiGenerateDataOutPduSequence (Data
, Tcb
, Lun
);
2150 if (DataOutPduList
== NULL
) {
2151 return EFI_OUT_OF_RESOURCES
;
2154 Status
= EFI_SUCCESS
;
2157 // Send the Data Out PDU's one by one.
2159 NET_LIST_FOR_EACH (Entry
, DataOutPduList
) {
2160 Pdu
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
2162 Status
= Tcp4IoTransmit (&Tcb
->Conn
->Tcp4Io
, Pdu
);
2163 if (EFI_ERROR (Status
)) {
2168 IScsiFreeNbufList (DataOutPduList
);
2174 Process the received iSCSI SCSI Data In PDU.
2176 @param[in] Pdu The Data In PDU received.
2177 @param[in] Tcb The task control block.
2178 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2180 @retval EFI_SUCCES The check on the Data IN PDU is passed and some update
2182 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2183 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2184 @retval Others Other errors as indicated.
2190 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2193 ISCSI_SCSI_DATA_IN
*DataInHdr
;
2196 DataInHdr
= (ISCSI_SCSI_DATA_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2198 DataInHdr
->InitiatorTaskTag
= NTOHL (DataInHdr
->InitiatorTaskTag
);
2199 DataInHdr
->ExpCmdSN
= NTOHL (DataInHdr
->ExpCmdSN
);
2200 DataInHdr
->MaxCmdSN
= NTOHL (DataInHdr
->MaxCmdSN
);
2201 DataInHdr
->DataSN
= NTOHL (DataInHdr
->DataSN
);
2204 // Check the DataSN.
2206 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, DataInHdr
->DataSN
);
2207 if (EFI_ERROR (Status
)) {
2211 if (DataInHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2212 return EFI_PROTOCOL_ERROR
;
2215 // Update the command related sequence numbers.
2217 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, DataInHdr
->MaxCmdSN
, DataInHdr
->ExpCmdSN
);
2219 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_STATUS_VALID
)) {
2220 if (!ISCSI_FLAG_ON (DataInHdr
, ISCSI_BHS_FLAG_FINAL
)) {
2222 // The S bit is on but the F bit is off.
2224 return EFI_PROTOCOL_ERROR
;
2227 Tcb
->StatusXferd
= TRUE
;
2229 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_OVERFLOW
| SCSI_DATA_IN_PDU_FLAG_UNDERFLOW
)) {
2231 // Underflow and Overflow are mutual flags.
2233 return EFI_PROTOCOL_ERROR
;
2236 // S bit is on, the StatSN is valid.
2238 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NTOHL (DataInHdr
->StatSN
));
2239 if (EFI_ERROR (Status
)) {
2243 Packet
->HostAdapterStatus
= 0;
2244 Packet
->TargetStatus
= DataInHdr
->Status
;
2246 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2247 Packet
->InTransferLength
+= NTOHL (DataInHdr
->ResidualCount
);
2248 Status
= EFI_BAD_BUFFER_SIZE
;
2251 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2252 Packet
->InTransferLength
-= NTOHL (DataInHdr
->ResidualCount
);
2260 Process the received iSCSI R2T PDU.
2262 @param[in] Pdu The R2T PDU received.
2263 @param[in] Tcb The task control block.
2264 @param[in] Lun The Lun.
2265 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2267 @retval EFI_SUCCES The R2T PDU is valid and the solicited data is sent out.
2268 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2269 @retval Others Other errors as indicated.
2276 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2279 ISCSI_READY_TO_TRANSFER
*R2THdr
;
2281 ISCSI_XFER_CONTEXT
*XferContext
;
2284 R2THdr
= (ISCSI_READY_TO_TRANSFER
*) NetbufGetByte (Pdu
, 0, NULL
);
2286 R2THdr
->InitiatorTaskTag
= NTOHL (R2THdr
->InitiatorTaskTag
);
2287 R2THdr
->TargetTransferTag
= NTOHL (R2THdr
->TargetTransferTag
);
2288 R2THdr
->StatSN
= NTOHL (R2THdr
->StatSN
);
2289 R2THdr
->R2TSeqNum
= NTOHL (R2THdr
->R2TSeqNum
);
2290 R2THdr
->BufferOffset
= NTOHL (R2THdr
->BufferOffset
);
2291 R2THdr
->DesiredDataTransferLength
= NTOHL (R2THdr
->DesiredDataTransferLength
);
2293 if ((R2THdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) || !ISCSI_SEQ_EQ (R2THdr
->StatSN
, Tcb
->Conn
->ExpStatSN
)) {
2294 return EFI_PROTOCOL_ERROR
;;
2297 // Check the sequence number.
2299 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, R2THdr
->R2TSeqNum
);
2300 if (EFI_ERROR (Status
)) {
2304 XferContext
= &Tcb
->XferContext
;
2305 XferContext
->TargetTransferTag
= R2THdr
->TargetTransferTag
;
2306 XferContext
->Offset
= R2THdr
->BufferOffset
;
2307 XferContext
->DesiredLength
= R2THdr
->DesiredDataTransferLength
;
2309 if (((XferContext
->Offset
+ XferContext
->DesiredLength
) > Packet
->OutTransferLength
) ||
2310 (XferContext
->DesiredLength
> Tcb
->Conn
->Session
->MaxBurstLength
)
2312 return EFI_PROTOCOL_ERROR
;
2315 // Send the data solicited by this R2T.
2317 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2318 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2324 Process the received iSCSI SCSI Response PDU.
2326 @param[in] Pdu The Response PDU received.
2327 @param[in] Tcb The task control block.
2328 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2330 @retval EFI_SUCCES The Response PDU is processed.
2331 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2332 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2333 @retval Others Other errors as indicated.
2336 IScsiOnScsiRspRcvd (
2339 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2342 SCSI_RESPONSE
*ScsiRspHdr
;
2343 ISCSI_SENSE_DATA
*SenseData
;
2347 ScsiRspHdr
= (SCSI_RESPONSE
*) NetbufGetByte (Pdu
, 0, NULL
);
2349 ScsiRspHdr
->InitiatorTaskTag
= NTOHL (ScsiRspHdr
->InitiatorTaskTag
);
2350 if (ScsiRspHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2351 return EFI_PROTOCOL_ERROR
;
2354 ScsiRspHdr
->StatSN
= NTOHL (ScsiRspHdr
->StatSN
);
2356 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, ScsiRspHdr
->StatSN
);
2357 if (EFI_ERROR (Status
)) {
2361 ScsiRspHdr
->MaxCmdSN
= NTOHL (ScsiRspHdr
->MaxCmdSN
);
2362 ScsiRspHdr
->ExpCmdSN
= NTOHL (ScsiRspHdr
->ExpCmdSN
);
2363 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, ScsiRspHdr
->MaxCmdSN
, ScsiRspHdr
->ExpCmdSN
);
2365 Tcb
->StatusXferd
= TRUE
;
2367 Packet
->HostAdapterStatus
= ScsiRspHdr
->Response
;
2368 if (Packet
->HostAdapterStatus
!= ISCSI_SERVICE_RSP_COMMAND_COMPLETE_AT_TARGET
) {
2372 Packet
->TargetStatus
= ScsiRspHdr
->Status
;
2374 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
| SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
) ||
2375 ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
| SCSI_RSP_PDU_FLAG_UNDERFLOW
)
2377 return EFI_PROTOCOL_ERROR
;
2380 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
)) {
2381 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2382 Status
= EFI_BAD_BUFFER_SIZE
;
2385 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
)) {
2386 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2389 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2390 if (Packet
->DataDirection
== DataIn
) {
2391 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2393 Packet
->OutTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2396 Status
= EFI_BAD_BUFFER_SIZE
;
2399 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2400 if (Packet
->DataDirection
== DataIn
) {
2401 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2403 Packet
->OutTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2407 DataSegLen
= ISCSI_GET_DATASEG_LEN (ScsiRspHdr
);
2408 if (DataSegLen
!= 0) {
2409 SenseData
= (ISCSI_SENSE_DATA
*) NetbufGetByte (Pdu
, sizeof (SCSI_RESPONSE
), NULL
);
2411 SenseData
->Length
= NTOHS (SenseData
->Length
);
2413 Packet
->SenseDataLength
= (UINT8
) MIN (SenseData
->Length
, Packet
->SenseDataLength
);
2414 if (Packet
->SenseDataLength
!= 0) {
2415 CopyMem (Packet
->SenseData
, &SenseData
->Data
[0], Packet
->SenseDataLength
);
2418 Packet
->SenseDataLength
= 0;
2425 Process the received NOP In PDU.
2427 @param[in] Pdu The NOP In PDU received.
2428 @param[in] Tcb The task control block.
2430 @retval EFI_SUCCES The NOP In PDU is processed and the related sequence
2431 numbers are updated.
2432 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2440 ISCSI_NOP_IN
*NopInHdr
;
2443 NopInHdr
= (ISCSI_NOP_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2445 NopInHdr
->StatSN
= NTOHL (NopInHdr
->StatSN
);
2446 NopInHdr
->ExpCmdSN
= NTOHL (NopInHdr
->ExpCmdSN
);
2447 NopInHdr
->MaxCmdSN
= NTOHL (NopInHdr
->MaxCmdSN
);
2449 if (NopInHdr
->InitiatorTaskTag
== ISCSI_RESERVED_TAG
) {
2450 if (NopInHdr
->StatSN
!= Tcb
->Conn
->ExpStatSN
) {
2451 return EFI_PROTOCOL_ERROR
;
2454 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NopInHdr
->StatSN
);
2455 if (EFI_ERROR (Status
)) {
2460 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, NopInHdr
->MaxCmdSN
, NopInHdr
->ExpCmdSN
);
2466 Execute the SCSI command issued through the EXT SCSI PASS THRU protocol.
2468 @param[in] PassThru The EXT SCSI PASS THRU protocol.
2469 @param[in] Target The target ID.
2470 @param[in] Lun The LUN.
2471 @param[in, out] Packet The request packet containing IO request, SCSI command
2472 buffer and buffers to read/write.
2474 @retval EFI_SUCCES The SCSI command is executed and the result is updated to
2476 @retval EFI_DEVICE_ERROR Session state was not as required.
2477 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2478 @retval Others Other errors as indicated.
2481 IScsiExecuteScsiCommand (
2482 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*PassThru
,
2485 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2489 ISCSI_DRIVER_DATA
*Private
;
2490 ISCSI_SESSION
*Session
;
2491 EFI_EVENT TimeoutEvent
;
2492 ISCSI_CONNECTION
*Conn
;
2495 ISCSI_XFER_CONTEXT
*XferContext
;
2497 ISCSI_IN_BUFFER_CONTEXT InBufferContext
;
2501 Private
= ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru
);
2502 Session
= &Private
->Session
;
2503 Status
= EFI_SUCCESS
;
2505 TimeoutEvent
= NULL
;
2508 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
2509 return EFI_DEVICE_ERROR
;
2512 Conn
= NET_LIST_USER_STRUCT_S (
2513 Session
->Conns
.ForwardLink
,
2516 ISCSI_CONNECTION_SIGNATURE
2519 if (Packet
->Timeout
!= 0) {
2520 Timeout
= MultU64x32 (Packet
->Timeout
, 2);
2523 Status
= IScsiNewTcb (Conn
, &Tcb
);
2524 if (EFI_ERROR (Status
)) {
2528 // Encapsulate the SCSI request packet into an iSCSI SCSI Command PDU.
2530 Pdu
= IScsiNewScsiCmdPdu (Packet
, Lun
, Tcb
);
2532 Status
= EFI_OUT_OF_RESOURCES
;
2536 XferContext
= &Tcb
->XferContext
;
2537 Buffer
= NetbufGetByte (Pdu
, 0, NULL
);
2538 XferContext
->Offset
= ISCSI_GET_DATASEG_LEN (Buffer
);
2541 // Transmit the SCSI Command PDU.
2543 Status
= Tcp4IoTransmit (&Conn
->Tcp4Io
, Pdu
);
2547 if (EFI_ERROR (Status
)) {
2551 if (!Session
->InitialR2T
&&
2552 (XferContext
->Offset
< Session
->FirstBurstLength
) &&
2553 (XferContext
->Offset
< Packet
->OutTransferLength
)
2556 // Unsolicited Data-Out sequence is allowed, there is remaining SCSI
2557 // OUT data and the limit of FirstBurstLength is not reached.
2559 XferContext
->TargetTransferTag
= ISCSI_RESERVED_TAG
;
2560 XferContext
->DesiredLength
= MIN (
2561 Session
->FirstBurstLength
,
2562 Packet
->OutTransferLength
- XferContext
->Offset
2565 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2566 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2567 if (EFI_ERROR (Status
)) {
2572 InBufferContext
.InData
= (UINT8
*) Packet
->InDataBuffer
;
2573 InBufferContext
.InDataLen
= Packet
->InTransferLength
;
2575 while (!Tcb
->StatusXferd
) {
2577 // Start the timeout timer.
2580 Status
= gBS
->SetTimer (Conn
->TimeoutEvent
, TimerRelative
, Timeout
);
2581 if (EFI_ERROR (Status
)) {
2584 TimeoutEvent
= Conn
->TimeoutEvent
;
2587 // try to receive PDU from target.
2589 Status
= IScsiReceivePdu (Conn
, &Pdu
, &InBufferContext
, FALSE
, FALSE
, TimeoutEvent
);
2590 if (EFI_ERROR (Status
)) {
2594 switch (ISCSI_GET_OPCODE (NetbufGetByte (Pdu
, 0, NULL
))) {
2595 case ISCSI_OPCODE_SCSI_DATA_IN
:
2596 Status
= IScsiOnDataInRcvd (Pdu
, Tcb
, Packet
);
2599 case ISCSI_OPCODE_R2T
:
2600 Status
= IScsiOnR2TRcvd (Pdu
, Tcb
, Lun
, Packet
);
2603 case ISCSI_OPCODE_SCSI_RSP
:
2604 Status
= IScsiOnScsiRspRcvd (Pdu
, Tcb
, Packet
);
2607 case ISCSI_OPCODE_NOP_IN
:
2608 Status
= IScsiOnNopInRcvd (Pdu
, Tcb
);
2611 case ISCSI_OPCODE_VENDOR_T0
:
2612 case ISCSI_OPCODE_VENDOR_T1
:
2613 case ISCSI_OPCODE_VENDOR_T2
:
2615 // These messages are vendor specific, skip them.
2620 Status
= EFI_PROTOCOL_ERROR
;
2626 if (EFI_ERROR (Status
)) {
2633 if (TimeoutEvent
!= NULL
) {
2634 gBS
->SetTimer (TimeoutEvent
, TimerCancel
, 0);
2641 if ((Status
!= EFI_SUCCESS
) && (Status
!= EFI_NOT_READY
)) {
2643 // Reinstate the session.
2645 if (EFI_ERROR (IScsiSessionReinstatement (Private
))) {
2646 Status
= EFI_DEVICE_ERROR
;
2654 Reinstate the session on some error.
2656 @param[in, out] Private The iSCSI driver data.
2658 @retval EFI_SUCCES The session is reinstated from some error.
2659 @retval Other Reinstatement failed.
2662 IScsiSessionReinstatement (
2663 IN OUT ISCSI_DRIVER_DATA
*Private
2666 ISCSI_SESSION
*Session
;
2669 Session
= &Private
->Session
;
2670 ASSERT (Session
->State
== SESSION_STATE_LOGGED_IN
);
2673 // Abort the session and re-init it.
2675 IScsiSessionAbort (Session
);
2676 IScsiSessionInit (Session
, TRUE
);
2681 Status
= IScsiSessionLogin (Private
);
2687 Initialize some session parameters before login.
2689 @param[in, out] Session The iSCSI session.
2690 @param[in] Recovery Whether the request is from a fresh new start or recovery.
2694 IN OUT ISCSI_SESSION
*Session
,
2699 Session
->Signature
= ISCSI_SESSION_SIGNATURE
;
2700 Session
->State
= SESSION_STATE_FREE
;
2702 InitializeListHead (&Session
->Conns
);
2703 InitializeListHead (&Session
->TcbList
);
2709 Session
->InitiatorTaskTag
= 1;
2710 Session
->NextCid
= 1;
2712 Session
->TargetPortalGroupTag
= 0;
2713 Session
->MaxConnections
= ISCSI_MAX_CONNS_PER_SESSION
;
2714 Session
->InitialR2T
= FALSE
;
2715 Session
->ImmediateData
= TRUE
;
2716 Session
->MaxBurstLength
= 262144;
2717 Session
->FirstBurstLength
= MAX_RECV_DATA_SEG_LEN_IN_FFP
;
2718 Session
->DefaultTime2Wait
= 2;
2719 Session
->DefaultTime2Retain
= 20;
2720 Session
->MaxOutstandingR2T
= DEFAULT_MAX_OUTSTANDING_R2T
;
2721 Session
->DataPDUInOrder
= TRUE
;
2722 Session
->DataSequenceInOrder
= TRUE
;
2723 Session
->ErrorRecoveryLevel
= 0;
2727 Abort the iSCSI session, that is, reset all the connection and free the
2730 @param[in, out] Session The iSCSI session.
2732 @retval EFI_SUCCES The session is aborted.
2736 IN OUT ISCSI_SESSION
*Session
2739 ISCSI_DRIVER_DATA
*Private
;
2740 ISCSI_CONNECTION
*Conn
;
2742 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
2746 ASSERT (!IsListEmpty (&Session
->Conns
));
2748 Private
= ISCSI_DRIVER_DATA_FROM_SESSION (Session
);
2750 while (!IsListEmpty (&Session
->Conns
)) {
2751 Conn
= NET_LIST_USER_STRUCT_S (
2752 Session
->Conns
.ForwardLink
,
2755 ISCSI_CONNECTION_SIGNATURE
2758 gBS
->CloseProtocol (
2759 Conn
->Tcp4Io
.Handle
,
2760 &gEfiTcp4ProtocolGuid
,
2762 Private
->ExtScsiPassThruHandle
2765 IScsiConnReset (Conn
);
2767 IScsiDetatchConnection (Conn
);
2768 IScsiDestroyConnection (Conn
);
2771 Session
->State
= SESSION_STATE_FAILED
;