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 Status
= IScsiProcessLoginRsp (Conn
, Pdu
);
414 Add an iSCSI key-value pair as a string into the data segment of the Login Request PDU.
415 The DataSegmentLength and the actual size of the net buffer containing this PDU will be
418 @param[in, out] Pdu The iSCSI PDU whose data segment the key-value pair will
420 @param[in] Key The key name string.
421 @param[in] Value The value string.
423 @retval EFI_SUCCESS The key-valu pair is added to the PDU's datasegment and
424 the correspondence length fields are updated.
425 @retval EFI_OUT_OF_RESOURCES There is not enough space in the PDU to add the key-value
429 IScsiAddKeyValuePair (
439 ISCSI_LOGIN_REQUEST
*LoginReq
;
442 LoginReq
= (ISCSI_LOGIN_REQUEST
*) NetbufGetByte (Pdu
, 0, NULL
);
443 DataSegLen
= NTOH24 (LoginReq
->DataSegmentLength
);
445 KeyLen
= (UINT32
) AsciiStrLen (Key
);
446 ValueLen
= (UINT32
) AsciiStrLen (Value
);
449 // 1 byte for the key value separator '=' and 1 byte for the null
450 // delimiter after the value.
452 TotalLen
= KeyLen
+ 1 + ValueLen
+ 1;
455 // Allocate the space for the key-value pair.
457 Data
= (CHAR8
*)NetbufAllocSpace (Pdu
, TotalLen
, NET_BUF_TAIL
);
459 return EFI_OUT_OF_RESOURCES
;
464 CopyMem (Data
, Key
, KeyLen
);
473 CopyMem (Data
, Value
, ValueLen
);
479 // update the DataSegmentLength
481 ISCSI_SET_DATASEG_LEN (LoginReq
, DataSegLen
+ TotalLen
);
487 Prepare the iSCSI login request to be sent according to the current login status.
489 @param[in, out] Conn The connection in the iSCSI login phase.
491 @return The pointer to the net buffer containing the iSCSI login request built.
492 @retval Others Other errors as indicated.
495 IScsiPrepareLoginReq (
496 IN OUT ISCSI_CONNECTION
*Conn
499 ISCSI_SESSION
*Session
;
501 ISCSI_LOGIN_REQUEST
*LoginReq
;
504 Session
= Conn
->Session
;
506 Nbuf
= NetbufAlloc (sizeof (ISCSI_LOGIN_REQUEST
) + DEFAULT_MAX_RECV_DATA_SEG_LEN
);
511 LoginReq
= (ISCSI_LOGIN_REQUEST
*) NetbufAllocSpace (Nbuf
, sizeof (ISCSI_LOGIN_REQUEST
), NET_BUF_TAIL
);
512 ASSERT (LoginReq
!= NULL
);
513 ZeroMem (LoginReq
, sizeof (ISCSI_LOGIN_REQUEST
));
516 // Init the login request pdu
518 ISCSI_SET_OPCODE (LoginReq
, ISCSI_OPCODE_LOGIN_REQ
, ISCSI_REQ_IMMEDIATE
);
519 ISCSI_SET_STAGES (LoginReq
, Conn
->CurrentStage
, Conn
->NextStage
);
520 LoginReq
->VersionMax
= ISCSI_VERSION_MAX
;
521 LoginReq
->VersionMin
= ISCSI_VERSION_MIN
;
522 LoginReq
->Tsih
= HTONS (Session
->Tsih
);
523 LoginReq
->InitiatorTaskTag
= HTONL (Session
->InitiatorTaskTag
);
524 LoginReq
->Cid
= HTONS (Conn
->Cid
);
525 LoginReq
->CmdSN
= HTONL (Session
->CmdSN
);
528 // For the first Login Request on a coonection this is ExpStatSN for the
529 // old connection and this field is only valid if the Login Request restarts
531 // For subsequent Login Requests it is used to acknowledge the Login Responses
532 // with their increasing StatSN values.
534 LoginReq
->ExpStatSN
= HTONL (Conn
->ExpStatSN
);
535 CopyMem (LoginReq
->Isid
, Session
->Isid
, sizeof (LoginReq
->Isid
));
537 if (Conn
->PartialRspRcvd
) {
539 // A partial response, initiator must send an empty Login Request.
544 switch (Conn
->CurrentStage
) {
545 case ISCSI_SECURITY_NEGOTIATION
:
546 Status
= IScsiCHAPToSendReq (Conn
, Nbuf
);
549 case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
:
550 Status
= IScsiFillOpParams (Conn
, Nbuf
);
551 ISCSI_SET_FLAG (LoginReq
, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT
);
556 // something error happens...
558 Status
= EFI_DEVICE_ERROR
;
562 if (EFI_ERROR (Status
)) {
567 // Pad the data segment if needed.
569 IScsiPadSegment (Nbuf
, ISCSI_GET_DATASEG_LEN (LoginReq
));
571 // Check whether we will issue the stage transition signal?
573 Conn
->TransitInitiated
= (BOOLEAN
) ISCSI_FLAG_ON (LoginReq
, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT
);
580 Process the iSCSI Login Response.
582 @param[in, out] Conn The connection on which the iSCSI login response is received.
583 @param[in, out] Pdu The iSCSI login response PDU.
585 @retval EFI_SUCCESS The iSCSI login response PDU is processed and all check are passed.
586 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
587 @retval EFI_MEDIA_CHANGED Target is redirected.
588 @retval Others Other errors as indicated.
591 IScsiProcessLoginRsp (
592 IN OUT ISCSI_CONNECTION
*Conn
,
597 ISCSI_SESSION
*Session
;
598 ISCSI_LOGIN_RESPONSE
*LoginRsp
;
606 Session
= Conn
->Session
;
608 LoginRsp
= (ISCSI_LOGIN_RESPONSE
*) NetbufGetByte (Pdu
, 0, NULL
);
609 if (!ISCSI_CHECK_OPCODE (LoginRsp
, ISCSI_OPCODE_LOGIN_RSP
)) {
611 // It's not a Login Response
613 return EFI_PROTOCOL_ERROR
;
616 // Get the data segment if any.
618 DataSegLen
= ISCSI_GET_DATASEG_LEN (LoginRsp
);
619 if (DataSegLen
!= 0) {
620 DataSeg
= NetbufGetByte (Pdu
, sizeof (ISCSI_LOGIN_RESPONSE
), NULL
);
625 // Check the status class in the login response PDU.
627 switch (LoginRsp
->StatusClass
) {
628 case ISCSI_LOGIN_STATUS_SUCCESS
:
630 // Just break here, the response and the data segment will be processed later.
634 case ISCSI_LOGIN_STATUS_REDIRECTION
:
636 // The target may be moved to a different address
638 if (DataSeg
== NULL
) {
639 return EFI_PROTOCOL_ERROR
;
642 // Process the TargetAddress key-value strings in the data segment to update the
643 // target address info.
645 Status
= IScsiUpdateTargetAddress (Session
, (CHAR8
*)DataSeg
, DataSegLen
);
646 if (EFI_ERROR (Status
)) {
650 // Session will be restarted on this error status because the Target is
651 // redirected by this Login Response.
653 return EFI_MEDIA_CHANGED
;
657 // Initiator Error, Target Error, or any other undefined error code.
659 return EFI_PROTOCOL_ERROR
;
662 // The status is sucess, extract the wanted fields from the header segment.
664 Transit
= (BOOLEAN
) ISCSI_FLAG_ON (LoginRsp
, ISCSI_LOGIN_RSP_PDU_FLAG_TRANSIT
);
665 Continue
= (BOOLEAN
) ISCSI_FLAG_ON (LoginRsp
, ISCSI_LOGIN_RSP_PDU_FLAG_CONTINUE
);
667 CurrentStage
= (UINT8
) ISCSI_GET_CURRENT_STAGE (LoginRsp
);
668 NextStage
= (UINT8
) ISCSI_GET_NEXT_STAGE (LoginRsp
);
670 LoginRsp
->InitiatorTaskTag
= NTOHL (LoginRsp
->InitiatorTaskTag
);
672 if ((Transit
&& Continue
) ||
673 (CurrentStage
!= Conn
->CurrentStage
) ||
674 (!Conn
->TransitInitiated
&& Transit
) ||
675 (Transit
&& (NextStage
!= Conn
->NextStage
)) ||
676 (CompareMem (Session
->Isid
, LoginRsp
->Isid
, sizeof (LoginRsp
->Isid
)) != 0) ||
677 (LoginRsp
->InitiatorTaskTag
!= Session
->InitiatorTaskTag
)
680 // A Login Response with the C bit set to 1 MUST have the T bit set to 0;
681 // The CSG in the Login Response MUST be the same with the I-end of this connection;
682 // The T bit can't be 1 if the last Login Response sent by the initiator doesn't
683 // initiate the transistion;
684 // The NSG MUST be the same with the I-end of this connection if Transit is required.
685 // The ISID in the Login Response MUST be the same with this session.
687 return EFI_PROTOCOL_ERROR
;
690 LoginRsp
->StatSN
= NTOHL (LoginRsp
->StatSN
);
691 LoginRsp
->ExpCmdSN
= NTOHL (LoginRsp
->ExpCmdSN
);
692 LoginRsp
->MaxCmdSN
= NTOHL (LoginRsp
->MaxCmdSN
);
694 if ((Conn
->CurrentStage
== ISCSI_SECURITY_NEGOTIATION
) && (Conn
->CHAPStep
== ISCSI_CHAP_INITIAL
)) {
696 // If the Login Request is a leading Login Request, the target MUST use
697 // the value presented in CmdSN as the target value for ExpCmdSN.
699 if ((Session
->State
== SESSION_STATE_FREE
) && (Session
->CmdSN
!= LoginRsp
->ExpCmdSN
)) {
700 return EFI_PROTOCOL_ERROR
;
704 // It's the initial Login Response, initialize the local ExpStatSN, MaxCmdSN
707 Conn
->ExpStatSN
= LoginRsp
->StatSN
+ 1;
708 Session
->MaxCmdSN
= LoginRsp
->MaxCmdSN
;
709 Session
->ExpCmdSN
= LoginRsp
->ExpCmdSN
;
712 // Check the StatSN of this PDU
714 Status
= IScsiCheckSN (&Conn
->ExpStatSN
, LoginRsp
->StatSN
);
715 if (!EFI_ERROR (Status
)) {
717 // Update the MaxCmdSN and ExpCmdSN
719 IScsiUpdateCmdSN (Session
, LoginRsp
->MaxCmdSN
, LoginRsp
->ExpCmdSN
);
725 // Trim off the header segment.
727 NetbufTrim (Pdu
, sizeof (ISCSI_LOGIN_RESPONSE
), NET_BUF_HEAD
);
730 // Queue this login response first in case it's a partial response so that
731 // later when the full response list is received we can combine these scattered
732 // responses' data segment and then process it.
735 NetbufQueAppend (&Conn
->RspQue
, Pdu
);
737 Conn
->PartialRspRcvd
= Continue
;
740 // It's a partial response, have to wait for another or more Request/Response
741 // conversations to get the full response.
746 switch (CurrentStage
) {
747 case ISCSI_SECURITY_NEGOTIATION
:
749 // In security negotiation stage, let CHAP module handle it.
751 Status
= IScsiCHAPOnRspReceived (Conn
);
754 case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
:
756 // Response received with negotiation resonse on iSCSI parameters, check them.
758 Status
= IScsiCheckOpParams (Conn
);
763 // Should never get here.
765 Status
= EFI_PROTOCOL_ERROR
;
769 if (Transit
&& (Status
== EFI_SUCCESS
)) {
771 // Do the state transition.
773 Conn
->CurrentStage
= Conn
->NextStage
;
775 if (Conn
->CurrentStage
== ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
) {
776 Conn
->NextStage
= ISCSI_FULL_FEATURE_PHASE
;
779 // CurrentStage is iSCSI Full Feature, it's the Login-Final Response,
780 // get the TSIH from the Login Response.
782 Session
->Tsih
= NTOHS (LoginRsp
->Tsih
);
786 // Flush the response(s) received.
788 NetbufQueFlush (&Conn
->RspQue
);
794 Updated the target information according the data received in the iSCSI
795 login response with an target redirection status.
797 @param[in, out] Session The iSCSI session.
798 @param[in] Data The data segment which should contain the
799 TargetAddress key-value list.
800 @param[in] Len Length of the data.
802 @retval EFI_SUCCESS The target address is updated.
803 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
804 @retval EFI_NOT_FOUND The TargetAddress key is not found.
805 @retval Others Other errors as indicated.
808 IScsiUpdateTargetAddress (
809 IN OUT ISCSI_SESSION
*Session
,
814 LIST_ENTRY
*KeyValueList
;
815 CHAR8
*TargetAddress
;
820 KeyValueList
= IScsiBuildKeyValueList (Data
, Len
);
821 if (KeyValueList
== NULL
) {
822 return EFI_OUT_OF_RESOURCES
;
825 Status
= EFI_NOT_FOUND
;
828 TargetAddress
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_ADDRESS
);
829 if (TargetAddress
== NULL
) {
833 if (!NET_IS_DIGIT (TargetAddress
[0])) {
835 // The domainname of the target may be presented in three formats: a DNS host name,
836 // a dotted-decimal IPv4 address, or a bracketed IPv6 address. Only accept dotted
842 IpStr
= TargetAddress
;
844 while ((*TargetAddress
!= 0) && (*TargetAddress
!= ':') && (*TargetAddress
!= ',')) {
846 // NULL, ':' or ',' ends the IPv4 string.
851 if (*TargetAddress
== ',') {
853 // Comma and the portal group tag MUST be ommitted if the TargetAddress is sent
854 // as the result of a redirection.
857 } else if (*TargetAddress
== ':') {
858 *TargetAddress
= '\0';
862 Number
= AsciiStrDecimalToUintn (TargetAddress
);
863 if (Number
> 0xFFFF) {
866 Session
->ConfigData
.NvData
.TargetPort
= (UINT16
) Number
;
870 // The string only contains the IPv4 address. Use the well known port.
872 Session
->ConfigData
.NvData
.TargetPort
= ISCSI_WELL_KNOWN_PORT
;
875 // Update the target IP address.
877 Status
= IScsiAsciiStrToIp (IpStr
, &Session
->ConfigData
.NvData
.TargetIp
);
878 if (EFI_ERROR (Status
)) {
885 IScsiFreeKeyValueList (KeyValueList
);
891 The callback function to free the net buffer list.
893 @param[in] Arg The opaque parameter.
901 ASSERT (Arg
!= NULL
);
903 NetbufFreeList ((LIST_ENTRY
*) Arg
);
908 The callback function called in NetBufFree, it does nothing.
910 @param[in] Arg The opaque parameter.
921 Receive an iSCSI response PDU. An iSCSI response PDU contains an iSCSI PDU header and
922 an optional data segment. The two parts will be put into two blocks of buffers in the
923 net buffer. The digest check will be conducted in this function if needed and the digests
924 will be trimmed from the PDU buffer.
926 @param[in] Conn The iSCSI connection to receive data from.
927 @param[out] Pdu The received iSCSI pdu.
928 @param[in] Context The context used to describe information on the caller provided
929 buffer to receive data segment of the iSCSI pdu, it's optional.
930 @param[in] HeaderDigest Whether there will be header digest received.
931 @param[in] DataDigest Whether there will be data digest.
932 @param[in] TimeoutEvent The timeout event, it's optional.
934 @retval EFI_SUCCESS An iSCSI pdu is received.
935 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
936 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
937 @retval Others Other errors as indicated.
941 IN ISCSI_CONNECTION
*Conn
,
943 IN ISCSI_IN_BUFFER_CONTEXT
*Context
, OPTIONAL
944 IN BOOLEAN HeaderDigest
,
945 IN BOOLEAN DataDigest
,
946 IN EFI_EVENT TimeoutEvent OPTIONAL
949 LIST_ENTRY
*NbufList
;
956 NET_FRAGMENT Fragment
[2];
957 UINT32 FragmentCount
;
959 UINT32 PadAndCRC32
[2];
961 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
962 if (NbufList
== NULL
) {
963 return EFI_OUT_OF_RESOURCES
;
966 InitializeListHead (NbufList
);
969 // The header digest will be received together with the PDU header if exists.
971 Len
= sizeof (ISCSI_BASIC_HEADER
) + (HeaderDigest
? sizeof (UINT32
) : 0);
972 PduHdr
= NetbufAlloc (Len
);
973 if (PduHdr
== NULL
) {
975 return EFI_OUT_OF_RESOURCES
;
978 Header
= NetbufAllocSpace (PduHdr
, Len
, NET_BUF_TAIL
);
979 ASSERT (Header
!= NULL
);
980 InsertTailList (NbufList
, &PduHdr
->List
);
983 // First step, receive the BHS of the PDU.
985 Status
= Tcp4IoReceive (&Conn
->Tcp4Io
, PduHdr
, FALSE
, TimeoutEvent
);
986 if (EFI_ERROR (Status
)) {
992 // TODO: check the header-digest.
995 // Trim off the digest.
997 NetbufTrim (PduHdr
, sizeof (UINT32
), NET_BUF_TAIL
);
1000 Len
= ISCSI_GET_DATASEG_LEN (Header
);
1003 // No data segment.Form the pdu from a list of pdu segments.
1005 *Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1007 Status
= EFI_OUT_OF_RESOURCES
;
1013 // Get the length of the padding bytes of the data segment.
1015 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1017 switch (ISCSI_GET_OPCODE (Header
)) {
1018 case ISCSI_OPCODE_SCSI_DATA_IN
:
1020 // Try to use the buffer described by Context if the PDU is an
1021 // iSCSI SCSI data in pdu so as to reduce memory copy overhead.
1023 InDataOffset
= ISCSI_GET_BUFFER_OFFSET (Header
);
1024 if ((Context
== NULL
) || ((InDataOffset
+ Len
) > Context
->InDataLen
)) {
1025 Status
= EFI_PROTOCOL_ERROR
;
1029 Fragment
[0].Len
= Len
;
1030 Fragment
[0].Bulk
= Context
->InData
+ InDataOffset
;
1032 if (DataDigest
|| (PadLen
!= 0)) {
1034 // The data segment is padded, use two fragments to receive it.
1035 // The first to receive the useful data. The second to receive the padding.
1037 Fragment
[1].Len
= PadLen
+ (DataDigest
? sizeof (UINT32
) : 0);
1038 Fragment
[1].Bulk
= (UINT8
*)PadAndCRC32
+ (4 - PadLen
);
1044 DataSeg
= NetbufFromExt (&Fragment
[0], FragmentCount
, 0, 0, IScsiNbufExtFree
, NULL
);
1045 if (DataSeg
== NULL
) {
1046 Status
= EFI_OUT_OF_RESOURCES
;
1052 case ISCSI_OPCODE_SCSI_RSP
:
1053 case ISCSI_OPCODE_NOP_IN
:
1054 case ISCSI_OPCODE_LOGIN_RSP
:
1055 case ISCSI_OPCODE_TEXT_RSP
:
1056 case ISCSI_OPCODE_ASYNC_MSG
:
1057 case ISCSI_OPCODE_REJECT
:
1058 case ISCSI_OPCODE_VENDOR_T0
:
1059 case ISCSI_OPCODE_VENDOR_T1
:
1060 case ISCSI_OPCODE_VENDOR_T2
:
1062 // Allocate buffer to receive the data segment.
1064 Len
+= PadLen
+ (DataDigest
? sizeof (UINT32
) : 0);
1065 DataSeg
= NetbufAlloc (Len
);
1066 if (DataSeg
== NULL
) {
1067 Status
= EFI_OUT_OF_RESOURCES
;
1071 NetbufAllocSpace (DataSeg
, Len
, NET_BUF_TAIL
);
1075 Status
= EFI_PROTOCOL_ERROR
;
1079 InsertTailList (NbufList
, &DataSeg
->List
);
1082 // Receive the data segment with the data digest if any.
1084 Status
= Tcp4IoReceive (&Conn
->Tcp4Io
, DataSeg
, FALSE
, TimeoutEvent
);
1085 if (EFI_ERROR (Status
)) {
1091 // TODO: Check the data digest.
1093 NetbufTrim (DataSeg
, sizeof (UINT32
), NET_BUF_TAIL
);
1098 // Trim off the padding bytes in the data segment.
1100 NetbufTrim (DataSeg
, PadLen
, NET_BUF_TAIL
);
1104 // Form the pdu from a list of pdu segments.
1106 *Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1108 Status
= EFI_OUT_OF_RESOURCES
;
1113 if (EFI_ERROR (Status
)) {
1115 // Free the Nbufs in this NbufList and the NbufList itself.
1117 IScsiFreeNbufList (NbufList
);
1124 Check and get the result of the prameter negotiation.
1126 @param[in, out] Conn The connection in iSCSI login.
1128 @retval EFI_SUCCESS The parmeter check is passed and negotiation is finished.
1129 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
1130 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1133 IScsiCheckOpParams (
1134 IN OUT ISCSI_CONNECTION
*Conn
1138 LIST_ENTRY
*KeyValueList
;
1141 ISCSI_SESSION
*Session
;
1145 ASSERT (Conn
->RspQue
.BufNum
!= 0);
1147 Session
= Conn
->Session
;
1149 Len
= Conn
->RspQue
.BufSize
;
1150 Data
= AllocatePool (Len
);
1152 return EFI_OUT_OF_RESOURCES
;
1155 NetbufQueCopy (&Conn
->RspQue
, 0, Len
, (UINT8
*) Data
);
1157 Status
= EFI_PROTOCOL_ERROR
;
1160 // Extract the Key-Value pairs into a list.
1162 KeyValueList
= IScsiBuildKeyValueList (Data
, Len
);
1163 if (KeyValueList
== NULL
) {
1170 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_HEADER_DIGEST
);
1171 if (Value
== NULL
) {
1175 if (AsciiStrCmp (Value
, "CRC32") == 0) {
1176 if (Conn
->HeaderDigest
!= IScsiDigestCRC32
) {
1179 } else if (AsciiStrCmp (Value
, ISCSI_KEY_VALUE_NONE
) == 0) {
1180 Conn
->HeaderDigest
= IScsiDigestNone
;
1187 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_DIGEST
);
1188 if (Value
== NULL
) {
1192 if (AsciiStrCmp (Value
, "CRC32") == 0) {
1193 if (Conn
->DataDigest
!= IScsiDigestCRC32
) {
1196 } else if (AsciiStrCmp (Value
, ISCSI_KEY_VALUE_NONE
) == 0) {
1197 Conn
->DataDigest
= IScsiDigestNone
;
1202 // ErrorRecoveryLevel, result fuction is Minimum.
1204 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_ERROR_RECOVERY_LEVEL
);
1205 if (Value
== NULL
) {
1209 NumericValue
= AsciiStrDecimalToUintn (Value
);
1210 if (NumericValue
> 2) {
1214 Session
->ErrorRecoveryLevel
= (UINT8
) MIN (Session
->ErrorRecoveryLevel
, NumericValue
);
1217 // InitialR2T, result function is OR.
1219 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_INITIAL_R2T
);
1220 if (Value
== NULL
) {
1224 Session
->InitialR2T
= (BOOLEAN
) (Session
->InitialR2T
|| (AsciiStrCmp (Value
, "Yes") == 0));
1227 // ImmediateData, result function is AND.
1229 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_IMMEDIATE_DATA
);
1230 if (Value
== NULL
) {
1234 Session
->ImmediateData
= (BOOLEAN
) (Session
->ImmediateData
&& (AsciiStrCmp (Value
, "Yes") == 0));
1237 // MaxRecvDataSegmentLength is declarative.
1239 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
);
1240 if (Value
!= NULL
) {
1241 Conn
->MaxRecvDataSegmentLength
= (UINT32
) AsciiStrDecimalToUintn (Value
);
1244 // MaxBurstLength, result funtion is Mininum.
1246 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_BURST_LENGTH
);
1247 if (Value
== NULL
) {
1251 NumericValue
= AsciiStrDecimalToUintn (Value
);
1252 Session
->MaxBurstLength
= (UINT32
) MIN (Session
->MaxBurstLength
, NumericValue
);
1255 // FirstBurstLength, result function is Minimum. Irrelevant when InitialR2T=Yes and
1256 // ImmediateData=No.
1257 // This Key/Value is negotiation type.
1259 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_FIRST_BURST_LENGTH
);
1260 if (Value
== NULL
) {
1264 NumericValue
= AsciiStrDecimalToUintn (Value
);
1265 Session
->FirstBurstLength
= (UINT32
) MIN (Session
->FirstBurstLength
, NumericValue
);
1268 // MaxConnections, result function is Minimum.
1270 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_CONNECTIONS
);
1271 if (Value
== NULL
) {
1275 NumericValue
= AsciiStrDecimalToUintn (Value
);
1276 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1280 Session
->MaxConnections
= (UINT32
) MIN (Session
->MaxConnections
, NumericValue
);
1283 // DataPDUInOrder, result function is OR.
1285 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_PDU_IN_ORDER
);
1286 if (Value
== NULL
) {
1290 Session
->DataPDUInOrder
= (BOOLEAN
) (Session
->DataPDUInOrder
|| (AsciiStrCmp (Value
, "Yes") == 0));
1293 // DataSequenceInorder, result function is OR.
1295 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
);
1296 if (Value
== NULL
) {
1300 Session
->DataSequenceInOrder
= (BOOLEAN
) (Session
->DataSequenceInOrder
|| (AsciiStrCmp (Value
, "Yes") == 0));
1303 // DefaultTime2Wait, result function is Maximum.
1305 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2WAIT
);
1306 if (Value
== NULL
) {
1310 NumericValue
= AsciiStrDecimalToUintn (Value
);
1311 if (NumericValue
== 0) {
1312 Session
->DefaultTime2Wait
= 0;
1313 } else if (NumericValue
> 3600) {
1316 Session
->DefaultTime2Wait
= (UINT32
) MAX (Session
->DefaultTime2Wait
, NumericValue
);
1319 // DefaultTime2Retain, result function is Minimum.
1321 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2RETAIN
);
1322 if (Value
== NULL
) {
1326 NumericValue
= AsciiStrDecimalToUintn (Value
);
1327 if (NumericValue
== 0) {
1328 Session
->DefaultTime2Retain
= 0;
1329 } else if (NumericValue
> 3600) {
1332 Session
->DefaultTime2Retain
= (UINT32
) MIN (Session
->DefaultTime2Retain
, NumericValue
);
1335 // MaxOutstandingR2T, result function is Minimum.
1337 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_OUTSTANDING_R2T
);
1338 if (Value
== NULL
) {
1342 NumericValue
= AsciiStrDecimalToUintn (Value
);
1343 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1347 Session
->MaxOutstandingR2T
= (UINT16
) MIN (Session
->MaxOutstandingR2T
, NumericValue
);
1350 // Remove declarative key-value paris if any.
1352 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_SESSION_TYPE
);
1353 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_ALIAS
);
1354 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_PORTAL_GROUP_TAG
);
1356 if (IsListEmpty (KeyValueList
)) {
1358 // Succeed if no more keys in the list.
1360 Status
= EFI_SUCCESS
;
1365 IScsiFreeKeyValueList (KeyValueList
);
1373 Fill the oprational prameters.
1375 @param[in] Conn The connection in iSCSI login.
1376 @param[in, out] Pdu The iSCSI login request PDU to fill the parameters.
1378 @retval EFI_SUCCESS The parmeters are filled into the iSCSI login request PDU.
1382 IN ISCSI_CONNECTION
*Conn
,
1386 ISCSI_SESSION
*Session
;
1389 Session
= Conn
->Session
;
1391 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->HeaderDigest
== IScsiDigestCRC32
) ? "None,CRC32" : "None");
1392 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_HEADER_DIGEST
, Value
);
1394 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->DataDigest
== IScsiDigestCRC32
) ? "None,CRC32" : "None");
1395 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_DIGEST
, Value
);
1397 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->ErrorRecoveryLevel
);
1398 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_ERROR_RECOVERY_LEVEL
, Value
);
1400 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->InitialR2T
? "Yes" : "No");
1401 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_INITIAL_R2T
, Value
);
1403 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->ImmediateData
? "Yes" : "No");
1404 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_IMMEDIATE_DATA
, Value
);
1406 AsciiSPrint (Value
, sizeof (Value
), "%d", MAX_RECV_DATA_SEG_LEN_IN_FFP
);
1407 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
, Value
);
1409 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxBurstLength
);
1410 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_BURST_LENGTH
, Value
);
1412 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->FirstBurstLength
);
1413 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_FIRST_BURST_LENGTH
, Value
);
1415 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxConnections
);
1416 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_CONNECTIONS
, Value
);
1418 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataPDUInOrder
? "Yes" : "No");
1419 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_PDU_IN_ORDER
, Value
);
1421 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataSequenceInOrder
? "Yes" : "No");
1422 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
, Value
);
1424 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Wait
);
1425 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2WAIT
, Value
);
1427 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Retain
);
1428 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2RETAIN
, Value
);
1430 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxOutstandingR2T
);
1431 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_OUTSTANDING_R2T
, Value
);
1437 Pad the iSCSI AHS or data segment to an integer number of 4 byte words.
1439 @param[in, out] Pdu The iSCSI pdu which contains segments to pad.
1440 @param[in] Len The length of the last semgnet in the PDU.
1442 @retval EFI_SUCCESS The segment is padded or no need to pad it.
1443 @retval EFI_OUT_OF_RESOURCES There is not enough remaining free space to add the
1448 IN OUT NET_BUF
*Pdu
,
1455 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1458 Data
= NetbufAllocSpace (Pdu
, PadLen
, NET_BUF_TAIL
);
1460 return EFI_OUT_OF_RESOURCES
;
1463 ZeroMem (Data
, PadLen
);
1470 Build a key-value list from the data segment.
1472 @param[in] Data The data segment containing the key-value pairs.
1473 @param[in] Len Length of the data segment.
1475 @return The key-value list.
1476 @retval NULL Other errors as indicated.
1479 IScsiBuildKeyValueList (
1484 LIST_ENTRY
*ListHead
;
1485 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1487 ListHead
= AllocatePool (sizeof (LIST_ENTRY
));
1488 if (ListHead
== NULL
) {
1492 InitializeListHead (ListHead
);
1495 KeyValuePair
= AllocatePool (sizeof (ISCSI_KEY_VALUE_PAIR
));
1496 if (KeyValuePair
== NULL
) {
1500 InitializeListHead (&KeyValuePair
->List
);
1502 KeyValuePair
->Key
= Data
;
1504 while ((Len
> 0) && (*Data
!= '=')) {
1515 FreePool (KeyValuePair
);
1519 KeyValuePair
->Value
= Data
;
1521 InsertTailList (ListHead
, &KeyValuePair
->List
);;
1523 Data
+= AsciiStrLen (KeyValuePair
->Value
) + 1;
1524 Len
-= (UINT32
) AsciiStrLen (KeyValuePair
->Value
) + 1;
1531 IScsiFreeKeyValueList (ListHead
);
1537 Get the value string by the key name from the key-value list. If found,
1538 the key-value entry will be removed from the list.
1540 @param[in, out] KeyValueList The key-value list.
1541 @param[in] Key The key name to find.
1543 @return The value string.
1546 IScsiGetValueByKeyFromList (
1547 IN OUT LIST_ENTRY
*KeyValueList
,
1552 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1557 NET_LIST_FOR_EACH (Entry
, KeyValueList
) {
1558 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1560 if (AsciiStrCmp (KeyValuePair
->Key
, Key
) == 0) {
1561 Value
= KeyValuePair
->Value
;
1563 RemoveEntryList (&KeyValuePair
->List
);
1564 FreePool (KeyValuePair
);
1573 Free the key-value list.
1575 @param[in] KeyValueList The key-value list.
1578 IScsiFreeKeyValueList (
1579 IN LIST_ENTRY
*KeyValueList
1583 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1585 while (!IsListEmpty (KeyValueList
)) {
1586 Entry
= NetListRemoveHead (KeyValueList
);
1587 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1589 FreePool (KeyValuePair
);
1592 FreePool (KeyValueList
);
1596 Normalize the iSCSI name according to RFC.
1598 @param[in, out] Name The iSCSI name.
1599 @param[in] Len length of the iSCSI name.
1601 @retval EFI_SUCCESS The iSCSI name is valid and normalized.
1602 @retval EFI_PROTOCOL_ERROR The iSCSI name is mal-formatted or not in the IQN format.
1605 IScsiNormalizeName (
1612 for (Index
= 0; Index
< Len
; Index
++) {
1613 if (NET_IS_UPPER_CASE_CHAR (Name
[Index
])) {
1615 // Convert the upper-case characters to lower-case ones
1617 Name
[Index
] = (CHAR8
) (Name
[Index
] - 'A' + 'a');
1620 if (!NET_IS_LOWER_CASE_CHAR (Name
[Index
]) &&
1621 !NET_IS_DIGIT (Name
[Index
]) &&
1622 (Name
[Index
] != '-') &&
1623 (Name
[Index
] != '.') &&
1624 (Name
[Index
] != ':')
1627 // ASCII dash, dot, colon lower-case characters and digit characters
1630 return EFI_PROTOCOL_ERROR
;
1634 if ((Len
< 4) || (CompareMem (Name
, "iqn.", 4) != 0)) {
1636 // Only IQN format is accepted now.
1638 return EFI_PROTOCOL_ERROR
;
1645 Create an iSCSI task control block.
1647 @param[in] Conn The connection on which the task control block will be created.
1648 @param[out] Tcb The newly created task control block.
1650 @retval EFI_SUCCESS The task control block is created.
1651 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1652 @retval EFI_NOT_READY The target can not accept new commands.
1656 IN ISCSI_CONNECTION
*Conn
,
1660 ISCSI_SESSION
*Session
;
1663 ASSERT (Tcb
!= NULL
);
1665 Session
= Conn
->Session
;
1667 if (ISCSI_SEQ_GT (Session
->CmdSN
, Session
->MaxCmdSN
)) {
1668 return EFI_NOT_READY
;
1671 NewTcb
= AllocateZeroPool (sizeof (ISCSI_TCB
));
1672 if (NewTcb
== NULL
) {
1673 return EFI_OUT_OF_RESOURCES
;
1676 InitializeListHead (&NewTcb
->Link
);
1678 NewTcb
->SoFarInOrder
= TRUE
;
1679 NewTcb
->InitiatorTaskTag
= Session
->InitiatorTaskTag
;
1680 NewTcb
->CmdSN
= Session
->CmdSN
;
1681 NewTcb
->Conn
= Conn
;
1683 InsertTailList (&Session
->TcbList
, &NewTcb
->Link
);
1686 // Advance the initiator task tag.
1688 Session
->InitiatorTaskTag
++;
1697 Delete the tcb from the connection and destroy it.
1699 @param[in] Tcb The tcb to delete.
1706 RemoveEntryList (&Tcb
->Link
);
1712 Find the task control block by the initator task tag.
1714 @param[in] TcbList The tcb list.
1715 @param[in] InitiatorTaskTag The initiator task tag.
1717 @return The task control block found.
1721 IN LIST_ENTRY
*TcbList
,
1722 IN UINT32 InitiatorTaskTag
1730 NET_LIST_FOR_EACH (Entry
, TcbList
) {
1731 Tcb
= NET_LIST_USER_STRUCT (Entry
, ISCSI_TCB
, Link
);
1733 if (Tcb
->InitiatorTaskTag
== InitiatorTaskTag
) {
1744 Create a data segment, pad it and calculate the CRC if needed.
1746 @param[in] Data The data to fill into the data segment.
1747 @param[in] Len Length of the data.
1748 @param[in] DataDigest Whether to calculate CRC for this data segment.
1750 @return The net buffer wrapping the data segment.
1753 IScsiNewDataSegment (
1756 IN BOOLEAN DataDigest
1759 NET_FRAGMENT Fragment
[2];
1760 UINT32 FragmentCount
;
1764 Fragment
[0].Len
= Len
;
1765 Fragment
[0].Bulk
= Data
;
1767 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1769 Fragment
[1].Len
= PadLen
;
1770 Fragment
[1].Bulk
= (UINT8
*) &mDataSegPad
;
1777 DataSeg
= NetbufFromExt (&Fragment
[0], FragmentCount
, 0, 0, IScsiNbufExtFree
, NULL
);
1783 Create a iSCSI SCSI command PDU to encapsulate the command issued
1784 by SCSI through the EXT SCSI PASS THRU Protocol.
1786 @param[in] Packet The EXT SCSI PASS THRU request packet containing the SCSI command.
1787 @param[in] Lun The LUN.
1788 @param[in] Tcb The tcb assocated with this SCSI command.
1790 @return The created iSCSI SCSI command PDU.
1791 @retval NULL Other errors as indicated.
1794 IScsiNewScsiCmdPdu (
1795 IN EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
1800 LIST_ENTRY
*NbufList
;
1804 SCSI_COMMAND
*ScsiCmd
;
1807 ISCSI_ADDITIONAL_HEADER
*Header
;
1808 ISCSI_BI_EXP_READ_DATA_LEN_AHS
*BiExpReadDataLenAHS
;
1809 ISCSI_SESSION
*Session
;
1810 UINT32 ImmediateDataLen
;
1814 if (Packet
->DataDirection
== DataBi
) {
1816 // Bi directional Read/Write command, the bidirectional expected
1817 // read data length AHS is required.
1819 AHSLength
+= sizeof (ISCSI_BI_EXP_READ_DATA_LEN_AHS
);
1822 if (Packet
->CdbLength
> 16) {
1824 // The CDB exceeds 16 bytes, an extended CDB AHS is required.
1826 AHSLength
= (UINT8
) (AHSLength
+ (ISCSI_ROUNDUP (Packet
->CdbLength
- 16) + sizeof (ISCSI_ADDITIONAL_HEADER
)));
1829 Length
= sizeof (SCSI_COMMAND
) + AHSLength
;
1830 PduHeader
= NetbufAlloc (Length
);
1831 if (PduHeader
== NULL
) {
1835 ScsiCmd
= (SCSI_COMMAND
*) NetbufAllocSpace (PduHeader
, Length
, NET_BUF_TAIL
);
1836 if (ScsiCmd
== NULL
) {
1837 NetbufFree (PduHeader
);
1840 Header
= (ISCSI_ADDITIONAL_HEADER
*) (ScsiCmd
+ 1);
1842 ZeroMem (ScsiCmd
, Length
);
1844 ISCSI_SET_OPCODE (ScsiCmd
, ISCSI_OPCODE_SCSI_CMD
, 0);
1845 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_TASK_ATTR_SIMPLE
);
1848 // Set the READ/WRITE flags according to the IO type of this request.
1850 switch (Packet
->DataDirection
) {
1852 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
);
1853 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->InTransferLength
);
1857 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_WRITE
);
1858 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
1862 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
| SCSI_CMD_PDU_FLAG_WRITE
);
1863 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
1866 // Fill the bidirectional expected read data length AHS.
1868 BiExpReadDataLenAHS
= (ISCSI_BI_EXP_READ_DATA_LEN_AHS
*) Header
;
1869 Header
= (ISCSI_ADDITIONAL_HEADER
*) (BiExpReadDataLenAHS
+ 1);
1871 BiExpReadDataLenAHS
->Length
= NTOHS (5);
1872 BiExpReadDataLenAHS
->Type
= ISCSI_AHS_TYPE_BI_EXP_READ_DATA_LEN
;
1873 BiExpReadDataLenAHS
->ExpReadDataLength
= NTOHL (Packet
->InTransferLength
);
1878 ScsiCmd
->TotalAHSLength
= AHSLength
;
1879 CopyMem (ScsiCmd
->Lun
, &Lun
, sizeof (ScsiCmd
->Lun
));
1880 ScsiCmd
->InitiatorTaskTag
= NTOHL (Tcb
->InitiatorTaskTag
);
1881 ScsiCmd
->CmdSN
= NTOHL (Tcb
->CmdSN
);
1882 ScsiCmd
->ExpStatSN
= NTOHL (Tcb
->Conn
->ExpStatSN
);
1884 CopyMem (ScsiCmd
->Cdb
, Packet
->Cdb
, sizeof (ScsiCmd
->Cdb
));
1886 if (Packet
->CdbLength
> 16) {
1887 Header
->Length
= NTOHS ((UINT16
) (Packet
->CdbLength
- 15));
1888 Header
->Type
= ISCSI_AHS_TYPE_EXT_CDB
;
1890 CopyMem (Header
+ 1, (UINT8
*) Packet
->Cdb
+ 16, Packet
->CdbLength
- 16);
1894 Session
= Tcb
->Conn
->Session
;
1895 ImmediateDataLen
= 0;
1897 if (Session
->ImmediateData
&& (Packet
->OutTransferLength
!= 0)) {
1899 // Send immediate data in this SCSI Command PDU. The length of the immeidate
1900 // data is the minimum of FirstBurstLength, the data length to be xfered and
1901 // the MaxRecvdataSegmentLength on this connection.
1903 ImmediateDataLen
= MIN (Session
->FirstBurstLength
, Packet
->OutTransferLength
);
1904 ImmediateDataLen
= MIN (ImmediateDataLen
, Tcb
->Conn
->MaxRecvDataSegmentLength
);
1907 // Update the data segment length in the PDU header.
1909 ISCSI_SET_DATASEG_LEN (ScsiCmd
, ImmediateDataLen
);
1912 // Create the data segment.
1914 DataSeg
= IScsiNewDataSegment ((UINT8
*) Packet
->OutDataBuffer
, ImmediateDataLen
, FALSE
);
1915 if (DataSeg
== NULL
) {
1916 NetbufFree (PduHeader
);
1921 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
1922 if (NbufList
== NULL
) {
1923 NetbufFree (PduHeader
);
1924 NetbufFree (DataSeg
);
1930 InitializeListHead (NbufList
);
1931 InsertTailList (NbufList
, &PduHeader
->List
);
1932 InsertTailList (NbufList
, &DataSeg
->List
);
1934 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1936 IScsiFreeNbufList (NbufList
);
1940 if (Session
->InitialR2T
||
1941 (ImmediateDataLen
== Session
->FirstBurstLength
) ||
1942 (ImmediateDataLen
== Packet
->OutTransferLength
)
1945 // Unsolicited data out sequence is not allowed,
1946 // or FirstBurstLength data is already sent out by immediate data
1947 // or all the OUT data accompany this SCSI packet is sent as
1948 // immediate data, the final flag should be set on this SCSI Command
1951 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_BHS_FLAG_FINAL
);
1960 Create a new iSCSI SCSI Data Out PDU.
1962 @param[in] Data The data to put into the Data Out PDU.
1963 @param[in] Len Length of the data.
1964 @param[in] DataSN The DataSN of the Data Out PDU.
1965 @param[in] Tcb The task control block of this Data Out PDU.
1966 @param[in] Lun The LUN.
1968 @return The net buffer wrapping the Data Out PDU.
1969 @retval NULL Other errors as indicated.
1972 IScsiNewDataOutPdu (
1980 LIST_ENTRY
*NbufList
;
1984 ISCSI_SCSI_DATA_OUT
*DataOutHdr
;
1985 ISCSI_XFER_CONTEXT
*XferContext
;
1987 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
1988 if (NbufList
== NULL
) {
1992 InitializeListHead (NbufList
);
1995 // Allocate memory for the BHS.
1997 PduHdr
= NetbufAlloc (sizeof (ISCSI_SCSI_DATA_OUT
));
1998 if (PduHdr
== NULL
) {
1999 FreePool (NbufList
);
2003 // Insert the BHS into the buffer list.
2005 InsertTailList (NbufList
, &PduHdr
->List
);
2007 DataOutHdr
= (ISCSI_SCSI_DATA_OUT
*) NetbufAllocSpace (PduHdr
, sizeof (ISCSI_SCSI_DATA_OUT
), NET_BUF_TAIL
);
2008 ASSERT (DataOutHdr
!= NULL
);
2009 XferContext
= &Tcb
->XferContext
;
2011 ZeroMem (DataOutHdr
, sizeof (ISCSI_SCSI_DATA_OUT
));
2014 // Set the flags and fields of the Data Out PDU BHS.
2016 ISCSI_SET_OPCODE (DataOutHdr
, ISCSI_OPCODE_SCSI_DATA_OUT
, 0);
2017 ISCSI_SET_DATASEG_LEN (DataOutHdr
, Len
);
2019 DataOutHdr
->InitiatorTaskTag
= HTONL (Tcb
->InitiatorTaskTag
);
2020 DataOutHdr
->TargetTransferTag
= HTONL (XferContext
->TargetTransferTag
);
2021 DataOutHdr
->ExpStatSN
= HTONL (Tcb
->Conn
->ExpStatSN
);
2022 DataOutHdr
->DataSN
= HTONL (DataSN
);
2023 DataOutHdr
->BufferOffset
= HTONL (XferContext
->Offset
);
2025 if (XferContext
->TargetTransferTag
!= ISCSI_RESERVED_TAG
) {
2026 CopyMem (&DataOutHdr
->Lun
, &Lun
, sizeof (DataOutHdr
->Lun
));
2029 // Build the data segment for this Data Out PDU.
2031 DataSeg
= IScsiNewDataSegment (Data
, Len
, FALSE
);
2032 if (DataSeg
== NULL
) {
2033 IScsiFreeNbufList (NbufList
);
2037 // Put the data segment into the buffer list and combine it with the BHS
2038 // into a full Data Out PDU.
2040 InsertTailList (NbufList
, &DataSeg
->List
);
2041 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
2043 IScsiFreeNbufList (NbufList
);
2050 Generate a consecutive sequence of iSCSI SCSI Data Out PDUs.
2052 @param[in] Data The data which will be carried by the sequence of iSCSI SCSI Data Out PDUs.
2053 @param[in] Tcb The task control block of the data to send out.
2054 @param[in] Lun The LUN the data will be sent to.
2056 @return A list of net buffers with each of them wraps an iSCSI SCSI Data Out PDU.
2057 @retval NULL Other errors as indicated.
2060 IScsiGenerateDataOutPduSequence (
2066 LIST_ENTRY
*PduList
;
2069 NET_BUF
*DataOutPdu
;
2070 ISCSI_CONNECTION
*Conn
;
2071 ISCSI_XFER_CONTEXT
*XferContext
;
2073 PduList
= AllocatePool (sizeof (LIST_ENTRY
));
2074 if (PduList
== NULL
) {
2078 InitializeListHead (PduList
);
2083 XferContext
= &Tcb
->XferContext
;
2085 while (XferContext
->DesiredLength
> 0) {
2087 // Determine the length of data this Data Out PDU can carry.
2089 DataLen
= MIN (XferContext
->DesiredLength
, Conn
->MaxRecvDataSegmentLength
);
2092 // Create a Data Out PDU.
2094 DataOutPdu
= IScsiNewDataOutPdu (Data
, DataLen
, DataSN
, Tcb
, Lun
);
2095 if (DataOutPdu
== NULL
) {
2096 IScsiFreeNbufList (PduList
);
2102 InsertTailList (PduList
, &DataOutPdu
->List
);
2105 // Update the context and DataSN.
2107 XferContext
->Offset
+= DataLen
;
2108 XferContext
->DesiredLength
-= DataLen
;
2113 // Set the F bit for the last data out PDU in this sequence.
2115 ISCSI_SET_FLAG (NetbufGetByte (DataOutPdu
, 0, NULL
), ISCSI_BHS_FLAG_FINAL
);
2123 Send the Data in a sequence of Data Out PDUs one by one.
2125 @param[in] Data The data to carry by Data Out PDUs.
2126 @param[in] Lun The LUN the data will be sent to.
2127 @param[in] Tcb The task control block.
2129 @retval EFI_SUCCES The data is sent out to the LUN.
2130 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2131 @retval Others Other errors as indicated.
2134 IScsiSendDataOutPduSequence (
2140 LIST_ENTRY
*DataOutPduList
;
2146 // Generate the Data Out PDU sequence.
2148 DataOutPduList
= IScsiGenerateDataOutPduSequence (Data
, Tcb
, Lun
);
2149 if (DataOutPduList
== NULL
) {
2150 return EFI_OUT_OF_RESOURCES
;
2153 Status
= EFI_SUCCESS
;
2156 // Send the Data Out PDU's one by one.
2158 NET_LIST_FOR_EACH (Entry
, DataOutPduList
) {
2159 Pdu
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
2161 Status
= Tcp4IoTransmit (&Tcb
->Conn
->Tcp4Io
, Pdu
);
2162 if (EFI_ERROR (Status
)) {
2167 IScsiFreeNbufList (DataOutPduList
);
2173 Process the received iSCSI SCSI Data In PDU.
2175 @param[in] Pdu The Data In PDU received.
2176 @param[in] Tcb The task control block.
2177 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2179 @retval EFI_SUCCES The check on the Data IN PDU is passed and some update
2181 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2182 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2183 @retval Others Other errors as indicated.
2189 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2192 ISCSI_SCSI_DATA_IN
*DataInHdr
;
2195 DataInHdr
= (ISCSI_SCSI_DATA_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2197 DataInHdr
->InitiatorTaskTag
= NTOHL (DataInHdr
->InitiatorTaskTag
);
2198 DataInHdr
->ExpCmdSN
= NTOHL (DataInHdr
->ExpCmdSN
);
2199 DataInHdr
->MaxCmdSN
= NTOHL (DataInHdr
->MaxCmdSN
);
2200 DataInHdr
->DataSN
= NTOHL (DataInHdr
->DataSN
);
2203 // Check the DataSN.
2205 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, DataInHdr
->DataSN
);
2206 if (EFI_ERROR (Status
)) {
2210 if (DataInHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2211 return EFI_PROTOCOL_ERROR
;
2214 // Update the command related sequence numbers.
2216 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, DataInHdr
->MaxCmdSN
, DataInHdr
->ExpCmdSN
);
2218 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_STATUS_VALID
)) {
2219 if (!ISCSI_FLAG_ON (DataInHdr
, ISCSI_BHS_FLAG_FINAL
)) {
2221 // The S bit is on but the F bit is off.
2223 return EFI_PROTOCOL_ERROR
;
2226 Tcb
->StatusXferd
= TRUE
;
2228 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_OVERFLOW
| SCSI_DATA_IN_PDU_FLAG_UNDERFLOW
)) {
2230 // Underflow and Overflow are mutual flags.
2232 return EFI_PROTOCOL_ERROR
;
2235 // S bit is on, the StatSN is valid.
2237 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NTOHL (DataInHdr
->StatSN
));
2238 if (EFI_ERROR (Status
)) {
2242 Packet
->HostAdapterStatus
= 0;
2243 Packet
->TargetStatus
= DataInHdr
->Status
;
2245 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2246 Packet
->InTransferLength
+= NTOHL (DataInHdr
->ResidualCount
);
2247 Status
= EFI_BAD_BUFFER_SIZE
;
2250 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2251 Packet
->InTransferLength
-= NTOHL (DataInHdr
->ResidualCount
);
2259 Process the received iSCSI R2T PDU.
2261 @param[in] Pdu The R2T PDU received.
2262 @param[in] Tcb The task control block.
2263 @param[in] Lun The Lun.
2264 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2266 @retval EFI_SUCCES The R2T PDU is valid and the solicited data is sent out.
2267 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2268 @retval Others Other errors as indicated.
2275 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2278 ISCSI_READY_TO_TRANSFER
*R2THdr
;
2280 ISCSI_XFER_CONTEXT
*XferContext
;
2283 R2THdr
= (ISCSI_READY_TO_TRANSFER
*) NetbufGetByte (Pdu
, 0, NULL
);
2285 R2THdr
->InitiatorTaskTag
= NTOHL (R2THdr
->InitiatorTaskTag
);
2286 R2THdr
->TargetTransferTag
= NTOHL (R2THdr
->TargetTransferTag
);
2287 R2THdr
->StatSN
= NTOHL (R2THdr
->StatSN
);
2288 R2THdr
->R2TSeqNum
= NTOHL (R2THdr
->R2TSeqNum
);
2289 R2THdr
->BufferOffset
= NTOHL (R2THdr
->BufferOffset
);
2290 R2THdr
->DesiredDataTransferLength
= NTOHL (R2THdr
->DesiredDataTransferLength
);
2292 if ((R2THdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) || !ISCSI_SEQ_EQ (R2THdr
->StatSN
, Tcb
->Conn
->ExpStatSN
)) {
2293 return EFI_PROTOCOL_ERROR
;;
2296 // Check the sequence number.
2298 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, R2THdr
->R2TSeqNum
);
2299 if (EFI_ERROR (Status
)) {
2303 XferContext
= &Tcb
->XferContext
;
2304 XferContext
->TargetTransferTag
= R2THdr
->TargetTransferTag
;
2305 XferContext
->Offset
= R2THdr
->BufferOffset
;
2306 XferContext
->DesiredLength
= R2THdr
->DesiredDataTransferLength
;
2308 if (((XferContext
->Offset
+ XferContext
->DesiredLength
) > Packet
->OutTransferLength
) ||
2309 (XferContext
->DesiredLength
> Tcb
->Conn
->Session
->MaxBurstLength
)
2311 return EFI_PROTOCOL_ERROR
;
2314 // Send the data solicited by this R2T.
2316 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2317 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2323 Process the received iSCSI SCSI Response PDU.
2325 @param[in] Pdu The Response PDU received.
2326 @param[in] Tcb The task control block.
2327 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2329 @retval EFI_SUCCES The Response PDU is processed.
2330 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2331 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2332 @retval Others Other errors as indicated.
2335 IScsiOnScsiRspRcvd (
2338 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2341 SCSI_RESPONSE
*ScsiRspHdr
;
2342 ISCSI_SENSE_DATA
*SenseData
;
2346 ScsiRspHdr
= (SCSI_RESPONSE
*) NetbufGetByte (Pdu
, 0, NULL
);
2348 ScsiRspHdr
->InitiatorTaskTag
= NTOHL (ScsiRspHdr
->InitiatorTaskTag
);
2349 if (ScsiRspHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2350 return EFI_PROTOCOL_ERROR
;
2353 ScsiRspHdr
->StatSN
= NTOHL (ScsiRspHdr
->StatSN
);
2355 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, ScsiRspHdr
->StatSN
);
2356 if (EFI_ERROR (Status
)) {
2360 ScsiRspHdr
->MaxCmdSN
= NTOHL (ScsiRspHdr
->MaxCmdSN
);
2361 ScsiRspHdr
->ExpCmdSN
= NTOHL (ScsiRspHdr
->ExpCmdSN
);
2362 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, ScsiRspHdr
->MaxCmdSN
, ScsiRspHdr
->ExpCmdSN
);
2364 Tcb
->StatusXferd
= TRUE
;
2366 Packet
->HostAdapterStatus
= ScsiRspHdr
->Response
;
2367 if (Packet
->HostAdapterStatus
!= ISCSI_SERVICE_RSP_COMMAND_COMPLETE_AT_TARGET
) {
2371 Packet
->TargetStatus
= ScsiRspHdr
->Status
;
2373 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
| SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
) ||
2374 ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
| SCSI_RSP_PDU_FLAG_UNDERFLOW
)
2376 return EFI_PROTOCOL_ERROR
;
2379 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
)) {
2380 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2381 Status
= EFI_BAD_BUFFER_SIZE
;
2384 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
)) {
2385 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2388 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2389 if (Packet
->DataDirection
== DataIn
) {
2390 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2392 Packet
->OutTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2395 Status
= EFI_BAD_BUFFER_SIZE
;
2398 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2399 if (Packet
->DataDirection
== DataIn
) {
2400 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2402 Packet
->OutTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2406 DataSegLen
= ISCSI_GET_DATASEG_LEN (ScsiRspHdr
);
2407 if (DataSegLen
!= 0) {
2408 SenseData
= (ISCSI_SENSE_DATA
*) NetbufGetByte (Pdu
, sizeof (SCSI_RESPONSE
), NULL
);
2410 SenseData
->Length
= NTOHS (SenseData
->Length
);
2412 Packet
->SenseDataLength
= (UINT8
) MIN (SenseData
->Length
, Packet
->SenseDataLength
);
2413 if (Packet
->SenseDataLength
!= 0) {
2414 CopyMem (Packet
->SenseData
, &SenseData
->Data
[0], Packet
->SenseDataLength
);
2417 Packet
->SenseDataLength
= 0;
2424 Process the received NOP In PDU.
2426 @param[in] Pdu The NOP In PDU received.
2427 @param[in] Tcb The task control block.
2429 @retval EFI_SUCCES The NOP In PDU is processed and the related sequence
2430 numbers are updated.
2431 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2439 ISCSI_NOP_IN
*NopInHdr
;
2442 NopInHdr
= (ISCSI_NOP_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2444 NopInHdr
->StatSN
= NTOHL (NopInHdr
->StatSN
);
2445 NopInHdr
->ExpCmdSN
= NTOHL (NopInHdr
->ExpCmdSN
);
2446 NopInHdr
->MaxCmdSN
= NTOHL (NopInHdr
->MaxCmdSN
);
2448 if (NopInHdr
->InitiatorTaskTag
== ISCSI_RESERVED_TAG
) {
2449 if (NopInHdr
->StatSN
!= Tcb
->Conn
->ExpStatSN
) {
2450 return EFI_PROTOCOL_ERROR
;
2453 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NopInHdr
->StatSN
);
2454 if (EFI_ERROR (Status
)) {
2459 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, NopInHdr
->MaxCmdSN
, NopInHdr
->ExpCmdSN
);
2465 Execute the SCSI command issued through the EXT SCSI PASS THRU protocol.
2467 @param[in] PassThru The EXT SCSI PASS THRU protocol.
2468 @param[in] Target The target ID.
2469 @param[in] Lun The LUN.
2470 @param[in, out] Packet The request packet containing IO request, SCSI command
2471 buffer and buffers to read/write.
2473 @retval EFI_SUCCES The SCSI command is executed and the result is updated to
2475 @retval EFI_DEVICE_ERROR Session state was not as required.
2476 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2477 @retval Others Other errors as indicated.
2480 IScsiExecuteScsiCommand (
2481 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*PassThru
,
2484 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2488 ISCSI_DRIVER_DATA
*Private
;
2489 ISCSI_SESSION
*Session
;
2490 EFI_EVENT TimeoutEvent
;
2491 ISCSI_CONNECTION
*Conn
;
2494 ISCSI_XFER_CONTEXT
*XferContext
;
2496 ISCSI_IN_BUFFER_CONTEXT InBufferContext
;
2500 Private
= ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru
);
2501 Session
= &Private
->Session
;
2502 Status
= EFI_SUCCESS
;
2504 TimeoutEvent
= NULL
;
2507 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
2508 return EFI_DEVICE_ERROR
;
2511 Conn
= NET_LIST_USER_STRUCT_S (
2512 Session
->Conns
.ForwardLink
,
2515 ISCSI_CONNECTION_SIGNATURE
2518 if (Packet
->Timeout
!= 0) {
2519 Timeout
= MultU64x32 (Packet
->Timeout
, 2);
2522 Status
= IScsiNewTcb (Conn
, &Tcb
);
2523 if (EFI_ERROR (Status
)) {
2527 // Encapsulate the SCSI request packet into an iSCSI SCSI Command PDU.
2529 Pdu
= IScsiNewScsiCmdPdu (Packet
, Lun
, Tcb
);
2531 Status
= EFI_OUT_OF_RESOURCES
;
2535 XferContext
= &Tcb
->XferContext
;
2536 Buffer
= NetbufGetByte (Pdu
, 0, NULL
);
2537 XferContext
->Offset
= ISCSI_GET_DATASEG_LEN (Buffer
);
2540 // Transmit the SCSI Command PDU.
2542 Status
= Tcp4IoTransmit (&Conn
->Tcp4Io
, Pdu
);
2546 if (EFI_ERROR (Status
)) {
2550 if (!Session
->InitialR2T
&&
2551 (XferContext
->Offset
< Session
->FirstBurstLength
) &&
2552 (XferContext
->Offset
< Packet
->OutTransferLength
)
2555 // Unsolicited Data-Out sequence is allowed, there is remaining SCSI
2556 // OUT data and the limit of FirstBurstLength is not reached.
2558 XferContext
->TargetTransferTag
= ISCSI_RESERVED_TAG
;
2559 XferContext
->DesiredLength
= MIN (
2560 Session
->FirstBurstLength
,
2561 Packet
->OutTransferLength
- XferContext
->Offset
2564 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2565 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2566 if (EFI_ERROR (Status
)) {
2571 InBufferContext
.InData
= (UINT8
*) Packet
->InDataBuffer
;
2572 InBufferContext
.InDataLen
= Packet
->InTransferLength
;
2574 while (!Tcb
->StatusXferd
) {
2576 // Start the timeout timer.
2579 Status
= gBS
->SetTimer (Conn
->TimeoutEvent
, TimerRelative
, Timeout
);
2580 if (EFI_ERROR (Status
)) {
2583 TimeoutEvent
= Conn
->TimeoutEvent
;
2586 // try to receive PDU from target.
2588 Status
= IScsiReceivePdu (Conn
, &Pdu
, &InBufferContext
, FALSE
, FALSE
, TimeoutEvent
);
2589 if (EFI_ERROR (Status
)) {
2593 switch (ISCSI_GET_OPCODE (NetbufGetByte (Pdu
, 0, NULL
))) {
2594 case ISCSI_OPCODE_SCSI_DATA_IN
:
2595 Status
= IScsiOnDataInRcvd (Pdu
, Tcb
, Packet
);
2598 case ISCSI_OPCODE_R2T
:
2599 Status
= IScsiOnR2TRcvd (Pdu
, Tcb
, Lun
, Packet
);
2602 case ISCSI_OPCODE_SCSI_RSP
:
2603 Status
= IScsiOnScsiRspRcvd (Pdu
, Tcb
, Packet
);
2606 case ISCSI_OPCODE_NOP_IN
:
2607 Status
= IScsiOnNopInRcvd (Pdu
, Tcb
);
2610 case ISCSI_OPCODE_VENDOR_T0
:
2611 case ISCSI_OPCODE_VENDOR_T1
:
2612 case ISCSI_OPCODE_VENDOR_T2
:
2614 // These messages are vendor specific, skip them.
2619 Status
= EFI_PROTOCOL_ERROR
;
2625 if (EFI_ERROR (Status
)) {
2632 if (TimeoutEvent
!= NULL
) {
2633 gBS
->SetTimer (TimeoutEvent
, TimerCancel
, 0);
2640 if ((Status
!= EFI_SUCCESS
) && (Status
!= EFI_NOT_READY
)) {
2642 // Reinstate the session.
2644 if (EFI_ERROR (IScsiSessionReinstatement (Private
))) {
2645 Status
= EFI_DEVICE_ERROR
;
2653 Reinstate the session on some error.
2655 @param[in, out] Private The iSCSI driver data.
2657 @retval EFI_SUCCES The session is reinstated from some error.
2658 @retval Other Reinstatement failed.
2661 IScsiSessionReinstatement (
2662 IN OUT ISCSI_DRIVER_DATA
*Private
2665 ISCSI_SESSION
*Session
;
2668 Session
= &Private
->Session
;
2669 ASSERT (Session
->State
== SESSION_STATE_LOGGED_IN
);
2672 // Abort the session and re-init it.
2674 IScsiSessionAbort (Session
);
2675 IScsiSessionInit (Session
, TRUE
);
2680 Status
= IScsiSessionLogin (Private
);
2686 Initialize some session parameters before login.
2688 @param[in, out] Session The iSCSI session.
2689 @param[in] Recovery Whether the request is from a fresh new start or recovery.
2693 IN OUT ISCSI_SESSION
*Session
,
2698 Session
->Signature
= ISCSI_SESSION_SIGNATURE
;
2699 Session
->State
= SESSION_STATE_FREE
;
2701 InitializeListHead (&Session
->Conns
);
2702 InitializeListHead (&Session
->TcbList
);
2708 Session
->InitiatorTaskTag
= 1;
2709 Session
->NextCid
= 1;
2711 Session
->TargetPortalGroupTag
= 0;
2712 Session
->MaxConnections
= ISCSI_MAX_CONNS_PER_SESSION
;
2713 Session
->InitialR2T
= FALSE
;
2714 Session
->ImmediateData
= TRUE
;
2715 Session
->MaxBurstLength
= 262144;
2716 Session
->FirstBurstLength
= MAX_RECV_DATA_SEG_LEN_IN_FFP
;
2717 Session
->DefaultTime2Wait
= 2;
2718 Session
->DefaultTime2Retain
= 20;
2719 Session
->MaxOutstandingR2T
= DEFAULT_MAX_OUTSTANDING_R2T
;
2720 Session
->DataPDUInOrder
= TRUE
;
2721 Session
->DataSequenceInOrder
= TRUE
;
2722 Session
->ErrorRecoveryLevel
= 0;
2726 Abort the iSCSI session, that is, reset all the connection and free the
2729 @param[in, out] Session The iSCSI session.
2731 @retval EFI_SUCCES The session is aborted.
2735 IN OUT ISCSI_SESSION
*Session
2738 ISCSI_DRIVER_DATA
*Private
;
2739 ISCSI_CONNECTION
*Conn
;
2741 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
2745 ASSERT (!IsListEmpty (&Session
->Conns
));
2747 Private
= ISCSI_DRIVER_DATA_FROM_SESSION (Session
);
2749 while (!IsListEmpty (&Session
->Conns
)) {
2750 Conn
= NET_LIST_USER_STRUCT_S (
2751 Session
->Conns
.ForwardLink
,
2754 ISCSI_CONNECTION_SIGNATURE
2757 gBS
->CloseProtocol (
2758 Conn
->Tcp4Io
.Handle
,
2759 &gEfiTcp4ProtocolGuid
,
2761 Private
->ExtScsiPassThruHandle
2764 IScsiConnReset (Conn
);
2766 IScsiDetatchConnection (Conn
);
2767 IScsiDestroyConnection (Conn
);
2770 Session
->State
= SESSION_STATE_FAILED
;