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 // It's the initial Login Response, initialize the local ExpStatSN, MaxCmdSN
699 Conn
->ExpStatSN
= LoginRsp
->StatSN
+ 1;
700 Session
->MaxCmdSN
= LoginRsp
->MaxCmdSN
;
701 Session
->ExpCmdSN
= LoginRsp
->ExpCmdSN
;
704 // Check the StatSN of this PDU
706 Status
= IScsiCheckSN (&Conn
->ExpStatSN
, LoginRsp
->StatSN
);
707 if (!EFI_ERROR (Status
)) {
709 // Update the MaxCmdSN and ExpCmdSN
711 IScsiUpdateCmdSN (Session
, LoginRsp
->MaxCmdSN
, LoginRsp
->ExpCmdSN
);
717 // Trim off the header segment.
719 NetbufTrim (Pdu
, sizeof (ISCSI_LOGIN_RESPONSE
), NET_BUF_HEAD
);
722 // Queue this login response first in case it's a partial response so that
723 // later when the full response list is received we can combine these scattered
724 // responses' data segment and then process it.
727 NetbufQueAppend (&Conn
->RspQue
, Pdu
);
729 Conn
->PartialRspRcvd
= Continue
;
732 // It's a partial response, have to wait for another or more Request/Response
733 // conversations to get the full response.
738 switch (CurrentStage
) {
739 case ISCSI_SECURITY_NEGOTIATION
:
741 // In security negotiation stage, let CHAP module handle it.
743 Status
= IScsiCHAPOnRspReceived (Conn
);
746 case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
:
748 // Response received with negotiation resonse on iSCSI parameters, check them.
750 Status
= IScsiCheckOpParams (Conn
);
755 // Should never get here.
757 Status
= EFI_PROTOCOL_ERROR
;
761 if (Transit
&& (Status
== EFI_SUCCESS
)) {
763 // Do the state transition.
765 Conn
->CurrentStage
= Conn
->NextStage
;
767 if (Conn
->CurrentStage
== ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
) {
768 Conn
->NextStage
= ISCSI_FULL_FEATURE_PHASE
;
771 // CurrentStage is iSCSI Full Feature, it's the Login-Final Response,
772 // get the TSIH from the Login Response.
774 Session
->Tsih
= NTOHS (LoginRsp
->Tsih
);
778 // Flush the response(s) received.
780 NetbufQueFlush (&Conn
->RspQue
);
786 Updated the target information according the data received in the iSCSI
787 login response with an target redirection status.
789 @param[in, out] Session The iSCSI session.
790 @param[in] Data The data segment which should contain the
791 TargetAddress key-value list.
792 @param[in] Len Length of the data.
794 @retval EFI_SUCCESS The target address is updated.
795 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
796 @retval EFI_NOT_FOUND The TargetAddress key is not found.
797 @retval Others Other errors as indicated.
800 IScsiUpdateTargetAddress (
801 IN OUT ISCSI_SESSION
*Session
,
806 LIST_ENTRY
*KeyValueList
;
807 CHAR8
*TargetAddress
;
812 KeyValueList
= IScsiBuildKeyValueList (Data
, Len
);
813 if (KeyValueList
== NULL
) {
814 return EFI_OUT_OF_RESOURCES
;
817 Status
= EFI_NOT_FOUND
;
820 TargetAddress
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_ADDRESS
);
821 if (TargetAddress
== NULL
) {
825 if (!NET_IS_DIGIT (TargetAddress
[0])) {
827 // The domainname of the target may be presented in three formats: a DNS host name,
828 // a dotted-decimal IPv4 address, or a bracketed IPv6 address. Only accept dotted
834 IpStr
= TargetAddress
;
836 while ((*TargetAddress
!= 0) && (*TargetAddress
!= ':') && (*TargetAddress
!= ',')) {
838 // NULL, ':' or ',' ends the IPv4 string.
843 if (*TargetAddress
== ',') {
845 // Comma and the portal group tag MUST be ommitted if the TargetAddress is sent
846 // as the result of a redirection.
849 } else if (*TargetAddress
== ':') {
850 *TargetAddress
= '\0';
854 Number
= AsciiStrDecimalToUintn (TargetAddress
);
855 if (Number
> 0xFFFF) {
858 Session
->ConfigData
.NvData
.TargetPort
= (UINT16
) Number
;
862 // The string only contains the IPv4 address. Use the well known port.
864 Session
->ConfigData
.NvData
.TargetPort
= ISCSI_WELL_KNOWN_PORT
;
867 // Update the target IP address.
869 Status
= IScsiAsciiStrToIp (IpStr
, &Session
->ConfigData
.NvData
.TargetIp
);
870 if (EFI_ERROR (Status
)) {
877 IScsiFreeKeyValueList (KeyValueList
);
883 The callback function to free the net buffer list.
885 @param[in] Arg The opaque parameter.
893 ASSERT (Arg
!= NULL
);
895 NetbufFreeList ((LIST_ENTRY
*) Arg
);
900 The callback function called in NetBufFree, it does nothing.
902 @param[in] Arg The opaque parameter.
913 Receive an iSCSI response PDU. An iSCSI response PDU contains an iSCSI PDU header and
914 an optional data segment. The two parts will be put into two blocks of buffers in the
915 net buffer. The digest check will be conducted in this function if needed and the digests
916 will be trimmed from the PDU buffer.
918 @param[in] Conn The iSCSI connection to receive data from.
919 @param[out] Pdu The received iSCSI pdu.
920 @param[in] Context The context used to describe information on the caller provided
921 buffer to receive data segment of the iSCSI pdu, it's optional.
922 @param[in] HeaderDigest Whether there will be header digest received.
923 @param[in] DataDigest Whether there will be data digest.
924 @param[in] TimeoutEvent The timeout event, it's optional.
926 @retval EFI_SUCCESS An iSCSI pdu is received.
927 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
928 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
929 @retval Others Other errors as indicated.
933 IN ISCSI_CONNECTION
*Conn
,
935 IN ISCSI_IN_BUFFER_CONTEXT
*Context
, OPTIONAL
936 IN BOOLEAN HeaderDigest
,
937 IN BOOLEAN DataDigest
,
938 IN EFI_EVENT TimeoutEvent OPTIONAL
941 LIST_ENTRY
*NbufList
;
948 NET_FRAGMENT Fragment
[2];
949 UINT32 FragmentCount
;
951 UINT32 PadAndCRC32
[2];
953 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
954 if (NbufList
== NULL
) {
955 return EFI_OUT_OF_RESOURCES
;
958 InitializeListHead (NbufList
);
961 // The header digest will be received together with the PDU header if exists.
963 Len
= sizeof (ISCSI_BASIC_HEADER
) + (HeaderDigest
? sizeof (UINT32
) : 0);
964 PduHdr
= NetbufAlloc (Len
);
965 if (PduHdr
== NULL
) {
967 return EFI_OUT_OF_RESOURCES
;
970 Header
= NetbufAllocSpace (PduHdr
, Len
, NET_BUF_TAIL
);
971 ASSERT (Header
!= NULL
);
972 InsertTailList (NbufList
, &PduHdr
->List
);
975 // First step, receive the BHS of the PDU.
977 Status
= Tcp4IoReceive (&Conn
->Tcp4Io
, PduHdr
, FALSE
, TimeoutEvent
);
978 if (EFI_ERROR (Status
)) {
984 // TODO: check the header-digest.
987 // Trim off the digest.
989 NetbufTrim (PduHdr
, sizeof (UINT32
), NET_BUF_TAIL
);
992 Len
= ISCSI_GET_DATASEG_LEN (Header
);
995 // No data segment.Form the pdu from a list of pdu segments.
997 *Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
999 Status
= EFI_OUT_OF_RESOURCES
;
1005 // Get the length of the padding bytes of the data segment.
1007 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1009 switch (ISCSI_GET_OPCODE (Header
)) {
1010 case ISCSI_OPCODE_SCSI_DATA_IN
:
1012 // Try to use the buffer described by Context if the PDU is an
1013 // iSCSI SCSI data in pdu so as to reduce memory copy overhead.
1015 InDataOffset
= ISCSI_GET_BUFFER_OFFSET (Header
);
1016 if ((Context
== NULL
) || ((InDataOffset
+ Len
) > Context
->InDataLen
)) {
1017 Status
= EFI_PROTOCOL_ERROR
;
1021 Fragment
[0].Len
= Len
;
1022 Fragment
[0].Bulk
= Context
->InData
+ InDataOffset
;
1024 if (DataDigest
|| (PadLen
!= 0)) {
1026 // The data segment is padded, use two fragments to receive it.
1027 // The first to receive the useful data. The second to receive the padding.
1029 Fragment
[1].Len
= PadLen
+ (DataDigest
? sizeof (UINT32
) : 0);
1030 Fragment
[1].Bulk
= (UINT8
*)PadAndCRC32
+ (4 - PadLen
);
1036 DataSeg
= NetbufFromExt (&Fragment
[0], FragmentCount
, 0, 0, IScsiNbufExtFree
, NULL
);
1037 if (DataSeg
== NULL
) {
1038 Status
= EFI_OUT_OF_RESOURCES
;
1044 case ISCSI_OPCODE_SCSI_RSP
:
1045 case ISCSI_OPCODE_NOP_IN
:
1046 case ISCSI_OPCODE_LOGIN_RSP
:
1047 case ISCSI_OPCODE_TEXT_RSP
:
1048 case ISCSI_OPCODE_ASYNC_MSG
:
1049 case ISCSI_OPCODE_REJECT
:
1050 case ISCSI_OPCODE_VENDOR_T0
:
1051 case ISCSI_OPCODE_VENDOR_T1
:
1052 case ISCSI_OPCODE_VENDOR_T2
:
1054 // Allocate buffer to receive the data segment.
1056 Len
+= PadLen
+ (DataDigest
? sizeof (UINT32
) : 0);
1057 DataSeg
= NetbufAlloc (Len
);
1058 if (DataSeg
== NULL
) {
1059 Status
= EFI_OUT_OF_RESOURCES
;
1063 NetbufAllocSpace (DataSeg
, Len
, NET_BUF_TAIL
);
1067 Status
= EFI_PROTOCOL_ERROR
;
1071 InsertTailList (NbufList
, &DataSeg
->List
);
1074 // Receive the data segment with the data digest if any.
1076 Status
= Tcp4IoReceive (&Conn
->Tcp4Io
, DataSeg
, FALSE
, TimeoutEvent
);
1077 if (EFI_ERROR (Status
)) {
1083 // TODO: Check the data digest.
1085 NetbufTrim (DataSeg
, sizeof (UINT32
), NET_BUF_TAIL
);
1090 // Trim off the padding bytes in the data segment.
1092 NetbufTrim (DataSeg
, PadLen
, NET_BUF_TAIL
);
1096 // Form the pdu from a list of pdu segments.
1098 *Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1100 Status
= EFI_OUT_OF_RESOURCES
;
1105 if (EFI_ERROR (Status
)) {
1107 // Free the Nbufs in this NbufList and the NbufList itself.
1109 IScsiFreeNbufList (NbufList
);
1116 Check and get the result of the prameter negotiation.
1118 @param[in, out] Conn The connection in iSCSI login.
1120 @retval EFI_SUCCESS The parmeter check is passed and negotiation is finished.
1121 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
1122 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1125 IScsiCheckOpParams (
1126 IN OUT ISCSI_CONNECTION
*Conn
1130 LIST_ENTRY
*KeyValueList
;
1133 ISCSI_SESSION
*Session
;
1137 ASSERT (Conn
->RspQue
.BufNum
!= 0);
1139 Session
= Conn
->Session
;
1141 Len
= Conn
->RspQue
.BufSize
;
1142 Data
= AllocatePool (Len
);
1144 return EFI_OUT_OF_RESOURCES
;
1147 NetbufQueCopy (&Conn
->RspQue
, 0, Len
, (UINT8
*) Data
);
1149 Status
= EFI_PROTOCOL_ERROR
;
1152 // Extract the Key-Value pairs into a list.
1154 KeyValueList
= IScsiBuildKeyValueList (Data
, Len
);
1155 if (KeyValueList
== NULL
) {
1162 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_HEADER_DIGEST
);
1163 if (Value
== NULL
) {
1167 if (AsciiStrCmp (Value
, "CRC32") == 0) {
1168 if (Conn
->HeaderDigest
!= IScsiDigestCRC32
) {
1171 } else if (AsciiStrCmp (Value
, ISCSI_KEY_VALUE_NONE
) == 0) {
1172 Conn
->HeaderDigest
= IScsiDigestNone
;
1179 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_DIGEST
);
1180 if (Value
== NULL
) {
1184 if (AsciiStrCmp (Value
, "CRC32") == 0) {
1185 if (Conn
->DataDigest
!= IScsiDigestCRC32
) {
1188 } else if (AsciiStrCmp (Value
, ISCSI_KEY_VALUE_NONE
) == 0) {
1189 Conn
->DataDigest
= IScsiDigestNone
;
1194 // ErrorRecoveryLevel, result fuction is Minimum.
1196 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_ERROR_RECOVERY_LEVEL
);
1197 if (Value
== NULL
) {
1201 NumericValue
= AsciiStrDecimalToUintn (Value
);
1202 if (NumericValue
> 2) {
1206 Session
->ErrorRecoveryLevel
= (UINT8
) MIN (Session
->ErrorRecoveryLevel
, NumericValue
);
1209 // InitialR2T, result function is OR.
1211 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_INITIAL_R2T
);
1212 if (Value
== NULL
) {
1216 Session
->InitialR2T
= (BOOLEAN
) (Session
->InitialR2T
|| (AsciiStrCmp (Value
, "Yes") == 0));
1219 // ImmediateData, result function is AND.
1221 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_IMMEDIATE_DATA
);
1222 if (Value
== NULL
) {
1226 Session
->ImmediateData
= (BOOLEAN
) (Session
->ImmediateData
&& (AsciiStrCmp (Value
, "Yes") == 0));
1229 // MaxRecvDataSegmentLength is declarative.
1231 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
);
1232 if (Value
!= NULL
) {
1233 Conn
->MaxRecvDataSegmentLength
= (UINT32
) AsciiStrDecimalToUintn (Value
);
1236 // MaxBurstLength, result funtion is Mininum.
1238 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_BURST_LENGTH
);
1239 if (Value
== NULL
) {
1243 NumericValue
= AsciiStrDecimalToUintn (Value
);
1244 Session
->MaxBurstLength
= (UINT32
) MIN (Session
->MaxBurstLength
, NumericValue
);
1247 // FirstBurstLength, result function is Minimum. Irrelevant when InitialR2T=Yes and
1248 // ImmediateData=No.
1249 // This Key/Value is negotiation type.
1251 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_FIRST_BURST_LENGTH
);
1252 if (Value
== NULL
) {
1256 NumericValue
= AsciiStrDecimalToUintn (Value
);
1257 Session
->FirstBurstLength
= (UINT32
) MIN (Session
->FirstBurstLength
, NumericValue
);
1260 // MaxConnections, result function is Minimum.
1262 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_CONNECTIONS
);
1263 if (Value
== NULL
) {
1267 NumericValue
= AsciiStrDecimalToUintn (Value
);
1268 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1272 Session
->MaxConnections
= (UINT32
) MIN (Session
->MaxConnections
, NumericValue
);
1275 // DataPDUInOrder, result function is OR.
1277 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_PDU_IN_ORDER
);
1278 if (Value
== NULL
) {
1282 Session
->DataPDUInOrder
= (BOOLEAN
) (Session
->DataPDUInOrder
|| (AsciiStrCmp (Value
, "Yes") == 0));
1285 // DataSequenceInorder, result function is OR.
1287 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
);
1288 if (Value
== NULL
) {
1292 Session
->DataSequenceInOrder
= (BOOLEAN
) (Session
->DataSequenceInOrder
|| (AsciiStrCmp (Value
, "Yes") == 0));
1295 // DefaultTime2Wait, result function is Maximum.
1297 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2WAIT
);
1298 if (Value
== NULL
) {
1302 NumericValue
= AsciiStrDecimalToUintn (Value
);
1303 if (NumericValue
== 0) {
1304 Session
->DefaultTime2Wait
= 0;
1305 } else if (NumericValue
> 3600) {
1308 Session
->DefaultTime2Wait
= (UINT32
) MAX (Session
->DefaultTime2Wait
, NumericValue
);
1311 // DefaultTime2Retain, result function is Minimum.
1313 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2RETAIN
);
1314 if (Value
== NULL
) {
1318 NumericValue
= AsciiStrDecimalToUintn (Value
);
1319 if (NumericValue
== 0) {
1320 Session
->DefaultTime2Retain
= 0;
1321 } else if (NumericValue
> 3600) {
1324 Session
->DefaultTime2Retain
= (UINT32
) MIN (Session
->DefaultTime2Retain
, NumericValue
);
1327 // MaxOutstandingR2T, result function is Minimum.
1329 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_OUTSTANDING_R2T
);
1330 if (Value
== NULL
) {
1334 NumericValue
= AsciiStrDecimalToUintn (Value
);
1335 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1339 Session
->MaxOutstandingR2T
= (UINT16
) MIN (Session
->MaxOutstandingR2T
, NumericValue
);
1342 // Remove declarative key-value paris if any.
1344 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_SESSION_TYPE
);
1345 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_ALIAS
);
1346 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_PORTAL_GROUP_TAG
);
1348 if (IsListEmpty (KeyValueList
)) {
1350 // Succeed if no more keys in the list.
1352 Status
= EFI_SUCCESS
;
1357 IScsiFreeKeyValueList (KeyValueList
);
1365 Fill the oprational prameters.
1367 @param[in] Conn The connection in iSCSI login.
1368 @param[in, out] Pdu The iSCSI login request PDU to fill the parameters.
1370 @retval EFI_SUCCESS The parmeters are filled into the iSCSI login request PDU.
1374 IN ISCSI_CONNECTION
*Conn
,
1378 ISCSI_SESSION
*Session
;
1381 Session
= Conn
->Session
;
1383 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->HeaderDigest
== IScsiDigestCRC32
) ? "None,CRC32" : "None");
1384 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_HEADER_DIGEST
, Value
);
1386 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->DataDigest
== IScsiDigestCRC32
) ? "None,CRC32" : "None");
1387 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_DIGEST
, Value
);
1389 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->ErrorRecoveryLevel
);
1390 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_ERROR_RECOVERY_LEVEL
, Value
);
1392 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->InitialR2T
? "Yes" : "No");
1393 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_INITIAL_R2T
, Value
);
1395 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->ImmediateData
? "Yes" : "No");
1396 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_IMMEDIATE_DATA
, Value
);
1398 AsciiSPrint (Value
, sizeof (Value
), "%d", MAX_RECV_DATA_SEG_LEN_IN_FFP
);
1399 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
, Value
);
1401 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxBurstLength
);
1402 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_BURST_LENGTH
, Value
);
1404 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->FirstBurstLength
);
1405 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_FIRST_BURST_LENGTH
, Value
);
1407 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxConnections
);
1408 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_CONNECTIONS
, Value
);
1410 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataPDUInOrder
? "Yes" : "No");
1411 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_PDU_IN_ORDER
, Value
);
1413 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataSequenceInOrder
? "Yes" : "No");
1414 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
, Value
);
1416 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Wait
);
1417 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2WAIT
, Value
);
1419 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Retain
);
1420 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2RETAIN
, Value
);
1422 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxOutstandingR2T
);
1423 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_OUTSTANDING_R2T
, Value
);
1429 Pad the iSCSI AHS or data segment to an integer number of 4 byte words.
1431 @param[in, out] Pdu The iSCSI pdu which contains segments to pad.
1432 @param[in] Len The length of the last semgnet in the PDU.
1434 @retval EFI_SUCCESS The segment is padded or no need to pad it.
1435 @retval EFI_OUT_OF_RESOURCES There is not enough remaining free space to add the
1440 IN OUT NET_BUF
*Pdu
,
1447 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1450 Data
= NetbufAllocSpace (Pdu
, PadLen
, NET_BUF_TAIL
);
1452 return EFI_OUT_OF_RESOURCES
;
1455 ZeroMem (Data
, PadLen
);
1462 Build a key-value list from the data segment.
1464 @param[in] Data The data segment containing the key-value pairs.
1465 @param[in] Len Length of the data segment.
1467 @return The key-value list.
1468 @retval NULL Other errors as indicated.
1471 IScsiBuildKeyValueList (
1476 LIST_ENTRY
*ListHead
;
1477 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1479 ListHead
= AllocatePool (sizeof (LIST_ENTRY
));
1480 if (ListHead
== NULL
) {
1484 InitializeListHead (ListHead
);
1487 KeyValuePair
= AllocatePool (sizeof (ISCSI_KEY_VALUE_PAIR
));
1488 if (KeyValuePair
== NULL
) {
1492 InitializeListHead (&KeyValuePair
->List
);
1494 KeyValuePair
->Key
= Data
;
1496 while ((Len
> 0) && (*Data
!= '=')) {
1507 FreePool (KeyValuePair
);
1511 KeyValuePair
->Value
= Data
;
1513 InsertTailList (ListHead
, &KeyValuePair
->List
);;
1515 Data
+= AsciiStrLen (KeyValuePair
->Value
) + 1;
1516 Len
-= (UINT32
) AsciiStrLen (KeyValuePair
->Value
) + 1;
1523 IScsiFreeKeyValueList (ListHead
);
1529 Get the value string by the key name from the key-value list. If found,
1530 the key-value entry will be removed from the list.
1532 @param[in, out] KeyValueList The key-value list.
1533 @param[in] Key The key name to find.
1535 @return The value string.
1538 IScsiGetValueByKeyFromList (
1539 IN OUT LIST_ENTRY
*KeyValueList
,
1544 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1549 NET_LIST_FOR_EACH (Entry
, KeyValueList
) {
1550 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1552 if (AsciiStrCmp (KeyValuePair
->Key
, Key
) == 0) {
1553 Value
= KeyValuePair
->Value
;
1555 RemoveEntryList (&KeyValuePair
->List
);
1556 FreePool (KeyValuePair
);
1565 Free the key-value list.
1567 @param[in] KeyValueList The key-value list.
1570 IScsiFreeKeyValueList (
1571 IN LIST_ENTRY
*KeyValueList
1575 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1577 while (!IsListEmpty (KeyValueList
)) {
1578 Entry
= NetListRemoveHead (KeyValueList
);
1579 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1581 FreePool (KeyValuePair
);
1584 FreePool (KeyValueList
);
1588 Normalize the iSCSI name according to RFC.
1590 @param[in, out] Name The iSCSI name.
1591 @param[in] Len length of the iSCSI name.
1593 @retval EFI_SUCCESS The iSCSI name is valid and normalized.
1594 @retval EFI_PROTOCOL_ERROR The iSCSI name is mal-formatted or not in the IQN format.
1597 IScsiNormalizeName (
1604 for (Index
= 0; Index
< Len
; Index
++) {
1605 if (NET_IS_UPPER_CASE_CHAR (Name
[Index
])) {
1607 // Convert the upper-case characters to lower-case ones
1609 Name
[Index
] = (CHAR8
) (Name
[Index
] - 'A' + 'a');
1612 if (!NET_IS_LOWER_CASE_CHAR (Name
[Index
]) &&
1613 !NET_IS_DIGIT (Name
[Index
]) &&
1614 (Name
[Index
] != '-') &&
1615 (Name
[Index
] != '.') &&
1616 (Name
[Index
] != ':')
1619 // ASCII dash, dot, colon lower-case characters and digit characters
1622 return EFI_PROTOCOL_ERROR
;
1626 if ((Len
< 4) || (CompareMem (Name
, "iqn.", 4) != 0)) {
1628 // Only IQN format is accepted now.
1630 return EFI_PROTOCOL_ERROR
;
1637 Create an iSCSI task control block.
1639 @param[in] Conn The connection on which the task control block will be created.
1640 @param[out] Tcb The newly created task control block.
1642 @retval EFI_SUCCESS The task control block is created.
1643 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1644 @retval EFI_NOT_READY The target can not accept new commands.
1648 IN ISCSI_CONNECTION
*Conn
,
1652 ISCSI_SESSION
*Session
;
1655 ASSERT (Tcb
!= NULL
);
1657 Session
= Conn
->Session
;
1659 if (ISCSI_SEQ_GT (Session
->CmdSN
, Session
->MaxCmdSN
)) {
1660 return EFI_NOT_READY
;
1663 NewTcb
= AllocateZeroPool (sizeof (ISCSI_TCB
));
1664 if (NewTcb
== NULL
) {
1665 return EFI_OUT_OF_RESOURCES
;
1668 InitializeListHead (&NewTcb
->Link
);
1670 NewTcb
->SoFarInOrder
= TRUE
;
1671 NewTcb
->InitiatorTaskTag
= Session
->InitiatorTaskTag
;
1672 NewTcb
->CmdSN
= Session
->CmdSN
;
1673 NewTcb
->Conn
= Conn
;
1675 InsertTailList (&Session
->TcbList
, &NewTcb
->Link
);
1678 // Advance the initiator task tag.
1680 Session
->InitiatorTaskTag
++;
1689 Delete the tcb from the connection and destroy it.
1691 @param[in] Tcb The tcb to delete.
1698 RemoveEntryList (&Tcb
->Link
);
1704 Find the task control block by the initator task tag.
1706 @param[in] TcbList The tcb list.
1707 @param[in] InitiatorTaskTag The initiator task tag.
1709 @return The task control block found.
1713 IN LIST_ENTRY
*TcbList
,
1714 IN UINT32 InitiatorTaskTag
1722 NET_LIST_FOR_EACH (Entry
, TcbList
) {
1723 Tcb
= NET_LIST_USER_STRUCT (Entry
, ISCSI_TCB
, Link
);
1725 if (Tcb
->InitiatorTaskTag
== InitiatorTaskTag
) {
1736 Create a data segment, pad it and calculate the CRC if needed.
1738 @param[in] Data The data to fill into the data segment.
1739 @param[in] Len Length of the data.
1740 @param[in] DataDigest Whether to calculate CRC for this data segment.
1742 @return The net buffer wrapping the data segment.
1745 IScsiNewDataSegment (
1748 IN BOOLEAN DataDigest
1751 NET_FRAGMENT Fragment
[2];
1752 UINT32 FragmentCount
;
1756 Fragment
[0].Len
= Len
;
1757 Fragment
[0].Bulk
= Data
;
1759 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1761 Fragment
[1].Len
= PadLen
;
1762 Fragment
[1].Bulk
= (UINT8
*) &mDataSegPad
;
1769 DataSeg
= NetbufFromExt (&Fragment
[0], FragmentCount
, 0, 0, IScsiNbufExtFree
, NULL
);
1775 Create a iSCSI SCSI command PDU to encapsulate the command issued
1776 by SCSI through the EXT SCSI PASS THRU Protocol.
1778 @param[in] Packet The EXT SCSI PASS THRU request packet containing the SCSI command.
1779 @param[in] Lun The LUN.
1780 @param[in] Tcb The tcb assocated with this SCSI command.
1782 @return The created iSCSI SCSI command PDU.
1783 @retval NULL Other errors as indicated.
1786 IScsiNewScsiCmdPdu (
1787 IN EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
1792 LIST_ENTRY
*NbufList
;
1796 SCSI_COMMAND
*ScsiCmd
;
1799 ISCSI_ADDITIONAL_HEADER
*Header
;
1800 ISCSI_BI_EXP_READ_DATA_LEN_AHS
*BiExpReadDataLenAHS
;
1801 ISCSI_SESSION
*Session
;
1802 UINT32 ImmediateDataLen
;
1806 if (Packet
->DataDirection
== DataBi
) {
1808 // Bi directional Read/Write command, the bidirectional expected
1809 // read data length AHS is required.
1811 AHSLength
+= sizeof (ISCSI_BI_EXP_READ_DATA_LEN_AHS
);
1814 if (Packet
->CdbLength
> 16) {
1816 // The CDB exceeds 16 bytes, an extended CDB AHS is required.
1818 AHSLength
= (UINT8
) (AHSLength
+ (ISCSI_ROUNDUP (Packet
->CdbLength
- 16) + sizeof (ISCSI_ADDITIONAL_HEADER
)));
1821 Length
= sizeof (SCSI_COMMAND
) + AHSLength
;
1822 PduHeader
= NetbufAlloc (Length
);
1823 if (PduHeader
== NULL
) {
1827 ScsiCmd
= (SCSI_COMMAND
*) NetbufAllocSpace (PduHeader
, Length
, NET_BUF_TAIL
);
1828 if (ScsiCmd
== NULL
) {
1829 NetbufFree (PduHeader
);
1832 Header
= (ISCSI_ADDITIONAL_HEADER
*) (ScsiCmd
+ 1);
1834 ZeroMem (ScsiCmd
, Length
);
1836 ISCSI_SET_OPCODE (ScsiCmd
, ISCSI_OPCODE_SCSI_CMD
, 0);
1837 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_TASK_ATTR_SIMPLE
);
1840 // Set the READ/WRITE flags according to the IO type of this request.
1842 switch (Packet
->DataDirection
) {
1844 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
);
1845 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->InTransferLength
);
1849 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_WRITE
);
1850 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
1854 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
| SCSI_CMD_PDU_FLAG_WRITE
);
1855 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
1858 // Fill the bidirectional expected read data length AHS.
1860 BiExpReadDataLenAHS
= (ISCSI_BI_EXP_READ_DATA_LEN_AHS
*) Header
;
1861 Header
= (ISCSI_ADDITIONAL_HEADER
*) (BiExpReadDataLenAHS
+ 1);
1863 BiExpReadDataLenAHS
->Length
= NTOHS (5);
1864 BiExpReadDataLenAHS
->Type
= ISCSI_AHS_TYPE_BI_EXP_READ_DATA_LEN
;
1865 BiExpReadDataLenAHS
->ExpReadDataLength
= NTOHL (Packet
->InTransferLength
);
1870 ScsiCmd
->TotalAHSLength
= AHSLength
;
1871 CopyMem (ScsiCmd
->Lun
, &Lun
, sizeof (ScsiCmd
->Lun
));
1872 ScsiCmd
->InitiatorTaskTag
= NTOHL (Tcb
->InitiatorTaskTag
);
1873 ScsiCmd
->CmdSN
= NTOHL (Tcb
->CmdSN
);
1874 ScsiCmd
->ExpStatSN
= NTOHL (Tcb
->Conn
->ExpStatSN
);
1876 CopyMem (ScsiCmd
->Cdb
, Packet
->Cdb
, sizeof (ScsiCmd
->Cdb
));
1878 if (Packet
->CdbLength
> 16) {
1879 Header
->Length
= NTOHS ((UINT16
) (Packet
->CdbLength
- 15));
1880 Header
->Type
= ISCSI_AHS_TYPE_EXT_CDB
;
1882 CopyMem (Header
+ 1, (UINT8
*) Packet
->Cdb
+ 16, Packet
->CdbLength
- 16);
1886 Session
= Tcb
->Conn
->Session
;
1887 ImmediateDataLen
= 0;
1889 if (Session
->ImmediateData
&& (Packet
->OutTransferLength
!= 0)) {
1891 // Send immediate data in this SCSI Command PDU. The length of the immeidate
1892 // data is the minimum of FirstBurstLength, the data length to be xfered and
1893 // the MaxRecvdataSegmentLength on this connection.
1895 ImmediateDataLen
= MIN (Session
->FirstBurstLength
, Packet
->OutTransferLength
);
1896 ImmediateDataLen
= MIN (ImmediateDataLen
, Tcb
->Conn
->MaxRecvDataSegmentLength
);
1899 // Update the data segment length in the PDU header.
1901 ISCSI_SET_DATASEG_LEN (ScsiCmd
, ImmediateDataLen
);
1904 // Create the data segment.
1906 DataSeg
= IScsiNewDataSegment ((UINT8
*) Packet
->OutDataBuffer
, ImmediateDataLen
, FALSE
);
1907 if (DataSeg
== NULL
) {
1908 NetbufFree (PduHeader
);
1913 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
1914 if (NbufList
== NULL
) {
1915 NetbufFree (PduHeader
);
1916 NetbufFree (DataSeg
);
1922 InitializeListHead (NbufList
);
1923 InsertTailList (NbufList
, &PduHeader
->List
);
1924 InsertTailList (NbufList
, &DataSeg
->List
);
1926 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1928 IScsiFreeNbufList (NbufList
);
1932 if (Session
->InitialR2T
||
1933 (ImmediateDataLen
== Session
->FirstBurstLength
) ||
1934 (ImmediateDataLen
== Packet
->OutTransferLength
)
1937 // Unsolicited data out sequence is not allowed,
1938 // or FirstBurstLength data is already sent out by immediate data
1939 // or all the OUT data accompany this SCSI packet is sent as
1940 // immediate data, the final flag should be set on this SCSI Command
1943 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_BHS_FLAG_FINAL
);
1952 Create a new iSCSI SCSI Data Out PDU.
1954 @param[in] Data The data to put into the Data Out PDU.
1955 @param[in] Len Length of the data.
1956 @param[in] DataSN The DataSN of the Data Out PDU.
1957 @param[in] Tcb The task control block of this Data Out PDU.
1958 @param[in] Lun The LUN.
1960 @return The net buffer wrapping the Data Out PDU.
1961 @retval NULL Other errors as indicated.
1964 IScsiNewDataOutPdu (
1972 LIST_ENTRY
*NbufList
;
1976 ISCSI_SCSI_DATA_OUT
*DataOutHdr
;
1977 ISCSI_XFER_CONTEXT
*XferContext
;
1979 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
1980 if (NbufList
== NULL
) {
1984 InitializeListHead (NbufList
);
1987 // Allocate memory for the BHS.
1989 PduHdr
= NetbufAlloc (sizeof (ISCSI_SCSI_DATA_OUT
));
1990 if (PduHdr
== NULL
) {
1991 FreePool (NbufList
);
1995 // Insert the BHS into the buffer list.
1997 InsertTailList (NbufList
, &PduHdr
->List
);
1999 DataOutHdr
= (ISCSI_SCSI_DATA_OUT
*) NetbufAllocSpace (PduHdr
, sizeof (ISCSI_SCSI_DATA_OUT
), NET_BUF_TAIL
);
2000 ASSERT (DataOutHdr
!= NULL
);
2001 XferContext
= &Tcb
->XferContext
;
2003 ZeroMem (DataOutHdr
, sizeof (ISCSI_SCSI_DATA_OUT
));
2006 // Set the flags and fields of the Data Out PDU BHS.
2008 ISCSI_SET_OPCODE (DataOutHdr
, ISCSI_OPCODE_SCSI_DATA_OUT
, 0);
2009 ISCSI_SET_DATASEG_LEN (DataOutHdr
, Len
);
2011 DataOutHdr
->InitiatorTaskTag
= HTONL (Tcb
->InitiatorTaskTag
);
2012 DataOutHdr
->TargetTransferTag
= HTONL (XferContext
->TargetTransferTag
);
2013 DataOutHdr
->ExpStatSN
= HTONL (Tcb
->Conn
->ExpStatSN
);
2014 DataOutHdr
->DataSN
= HTONL (DataSN
);
2015 DataOutHdr
->BufferOffset
= HTONL (XferContext
->Offset
);
2017 if (XferContext
->TargetTransferTag
!= ISCSI_RESERVED_TAG
) {
2018 CopyMem (&DataOutHdr
->Lun
, &Lun
, sizeof (DataOutHdr
->Lun
));
2021 // Build the data segment for this Data Out PDU.
2023 DataSeg
= IScsiNewDataSegment (Data
, Len
, FALSE
);
2024 if (DataSeg
== NULL
) {
2025 IScsiFreeNbufList (NbufList
);
2029 // Put the data segment into the buffer list and combine it with the BHS
2030 // into a full Data Out PDU.
2032 InsertTailList (NbufList
, &DataSeg
->List
);
2033 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
2035 IScsiFreeNbufList (NbufList
);
2042 Generate a consecutive sequence of iSCSI SCSI Data Out PDUs.
2044 @param[in] Data The data which will be carried by the sequence of iSCSI SCSI Data Out PDUs.
2045 @param[in] Tcb The task control block of the data to send out.
2046 @param[in] Lun The LUN the data will be sent to.
2048 @return A list of net buffers with each of them wraps an iSCSI SCSI Data Out PDU.
2049 @retval NULL Other errors as indicated.
2052 IScsiGenerateDataOutPduSequence (
2058 LIST_ENTRY
*PduList
;
2061 NET_BUF
*DataOutPdu
;
2062 ISCSI_CONNECTION
*Conn
;
2063 ISCSI_XFER_CONTEXT
*XferContext
;
2065 PduList
= AllocatePool (sizeof (LIST_ENTRY
));
2066 if (PduList
== NULL
) {
2070 InitializeListHead (PduList
);
2075 XferContext
= &Tcb
->XferContext
;
2077 while (XferContext
->DesiredLength
> 0) {
2079 // Determine the length of data this Data Out PDU can carry.
2081 DataLen
= MIN (XferContext
->DesiredLength
, Conn
->MaxRecvDataSegmentLength
);
2084 // Create a Data Out PDU.
2086 DataOutPdu
= IScsiNewDataOutPdu (Data
, DataLen
, DataSN
, Tcb
, Lun
);
2087 if (DataOutPdu
== NULL
) {
2088 IScsiFreeNbufList (PduList
);
2094 InsertTailList (PduList
, &DataOutPdu
->List
);
2097 // Update the context and DataSN.
2099 XferContext
->Offset
+= DataLen
;
2100 XferContext
->DesiredLength
-= DataLen
;
2105 // Set the F bit for the last data out PDU in this sequence.
2107 ISCSI_SET_FLAG (NetbufGetByte (DataOutPdu
, 0, NULL
), ISCSI_BHS_FLAG_FINAL
);
2115 Send the Data in a sequence of Data Out PDUs one by one.
2117 @param[in] Data The data to carry by Data Out PDUs.
2118 @param[in] Lun The LUN the data will be sent to.
2119 @param[in] Tcb The task control block.
2121 @retval EFI_SUCCES The data is sent out to the LUN.
2122 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2123 @retval Others Other errors as indicated.
2126 IScsiSendDataOutPduSequence (
2132 LIST_ENTRY
*DataOutPduList
;
2138 // Generate the Data Out PDU sequence.
2140 DataOutPduList
= IScsiGenerateDataOutPduSequence (Data
, Tcb
, Lun
);
2141 if (DataOutPduList
== NULL
) {
2142 return EFI_OUT_OF_RESOURCES
;
2145 Status
= EFI_SUCCESS
;
2148 // Send the Data Out PDU's one by one.
2150 NET_LIST_FOR_EACH (Entry
, DataOutPduList
) {
2151 Pdu
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
2153 Status
= Tcp4IoTransmit (&Tcb
->Conn
->Tcp4Io
, Pdu
);
2154 if (EFI_ERROR (Status
)) {
2159 IScsiFreeNbufList (DataOutPduList
);
2165 Process the received iSCSI SCSI Data In PDU.
2167 @param[in] Pdu The Data In PDU received.
2168 @param[in] Tcb The task control block.
2169 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2171 @retval EFI_SUCCES The check on the Data IN PDU is passed and some update
2173 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2174 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2175 @retval Others Other errors as indicated.
2181 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2184 ISCSI_SCSI_DATA_IN
*DataInHdr
;
2187 DataInHdr
= (ISCSI_SCSI_DATA_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2189 DataInHdr
->InitiatorTaskTag
= NTOHL (DataInHdr
->InitiatorTaskTag
);
2190 DataInHdr
->ExpCmdSN
= NTOHL (DataInHdr
->ExpCmdSN
);
2191 DataInHdr
->MaxCmdSN
= NTOHL (DataInHdr
->MaxCmdSN
);
2192 DataInHdr
->DataSN
= NTOHL (DataInHdr
->DataSN
);
2195 // Check the DataSN.
2197 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, DataInHdr
->DataSN
);
2198 if (EFI_ERROR (Status
)) {
2202 if (DataInHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2203 return EFI_PROTOCOL_ERROR
;
2206 // Update the command related sequence numbers.
2208 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, DataInHdr
->MaxCmdSN
, DataInHdr
->ExpCmdSN
);
2210 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_STATUS_VALID
)) {
2211 if (!ISCSI_FLAG_ON (DataInHdr
, ISCSI_BHS_FLAG_FINAL
)) {
2213 // The S bit is on but the F bit is off.
2215 return EFI_PROTOCOL_ERROR
;
2218 Tcb
->StatusXferd
= TRUE
;
2220 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_OVERFLOW
| SCSI_DATA_IN_PDU_FLAG_UNDERFLOW
)) {
2222 // Underflow and Overflow are mutual flags.
2224 return EFI_PROTOCOL_ERROR
;
2227 // S bit is on, the StatSN is valid.
2229 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NTOHL (DataInHdr
->StatSN
));
2230 if (EFI_ERROR (Status
)) {
2234 Packet
->HostAdapterStatus
= 0;
2235 Packet
->TargetStatus
= DataInHdr
->Status
;
2237 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2238 Packet
->InTransferLength
+= NTOHL (DataInHdr
->ResidualCount
);
2239 Status
= EFI_BAD_BUFFER_SIZE
;
2242 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2243 Packet
->InTransferLength
-= NTOHL (DataInHdr
->ResidualCount
);
2251 Process the received iSCSI R2T PDU.
2253 @param[in] Pdu The R2T PDU received.
2254 @param[in] Tcb The task control block.
2255 @param[in] Lun The Lun.
2256 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2258 @retval EFI_SUCCES The R2T PDU is valid and the solicited data is sent out.
2259 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2260 @retval Others Other errors as indicated.
2267 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2270 ISCSI_READY_TO_TRANSFER
*R2THdr
;
2272 ISCSI_XFER_CONTEXT
*XferContext
;
2275 R2THdr
= (ISCSI_READY_TO_TRANSFER
*) NetbufGetByte (Pdu
, 0, NULL
);
2277 R2THdr
->InitiatorTaskTag
= NTOHL (R2THdr
->InitiatorTaskTag
);
2278 R2THdr
->TargetTransferTag
= NTOHL (R2THdr
->TargetTransferTag
);
2279 R2THdr
->StatSN
= NTOHL (R2THdr
->StatSN
);
2280 R2THdr
->R2TSeqNum
= NTOHL (R2THdr
->R2TSeqNum
);
2281 R2THdr
->BufferOffset
= NTOHL (R2THdr
->BufferOffset
);
2282 R2THdr
->DesiredDataTransferLength
= NTOHL (R2THdr
->DesiredDataTransferLength
);
2284 if ((R2THdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) || !ISCSI_SEQ_EQ (R2THdr
->StatSN
, Tcb
->Conn
->ExpStatSN
)) {
2285 return EFI_PROTOCOL_ERROR
;;
2288 // Check the sequence number.
2290 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, R2THdr
->R2TSeqNum
);
2291 if (EFI_ERROR (Status
)) {
2295 XferContext
= &Tcb
->XferContext
;
2296 XferContext
->TargetTransferTag
= R2THdr
->TargetTransferTag
;
2297 XferContext
->Offset
= R2THdr
->BufferOffset
;
2298 XferContext
->DesiredLength
= R2THdr
->DesiredDataTransferLength
;
2300 if (((XferContext
->Offset
+ XferContext
->DesiredLength
) > Packet
->OutTransferLength
) ||
2301 (XferContext
->DesiredLength
> Tcb
->Conn
->Session
->MaxBurstLength
)
2303 return EFI_PROTOCOL_ERROR
;
2306 // Send the data solicited by this R2T.
2308 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2309 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2315 Process the received iSCSI SCSI Response PDU.
2317 @param[in] Pdu The Response PDU received.
2318 @param[in] Tcb The task control block.
2319 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2321 @retval EFI_SUCCES The Response PDU is processed.
2322 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2323 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2324 @retval Others Other errors as indicated.
2327 IScsiOnScsiRspRcvd (
2330 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2333 SCSI_RESPONSE
*ScsiRspHdr
;
2334 ISCSI_SENSE_DATA
*SenseData
;
2338 ScsiRspHdr
= (SCSI_RESPONSE
*) NetbufGetByte (Pdu
, 0, NULL
);
2340 ScsiRspHdr
->InitiatorTaskTag
= NTOHL (ScsiRspHdr
->InitiatorTaskTag
);
2341 if (ScsiRspHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2342 return EFI_PROTOCOL_ERROR
;
2345 ScsiRspHdr
->StatSN
= NTOHL (ScsiRspHdr
->StatSN
);
2347 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, ScsiRspHdr
->StatSN
);
2348 if (EFI_ERROR (Status
)) {
2352 ScsiRspHdr
->MaxCmdSN
= NTOHL (ScsiRspHdr
->MaxCmdSN
);
2353 ScsiRspHdr
->ExpCmdSN
= NTOHL (ScsiRspHdr
->ExpCmdSN
);
2354 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, ScsiRspHdr
->MaxCmdSN
, ScsiRspHdr
->ExpCmdSN
);
2356 Tcb
->StatusXferd
= TRUE
;
2358 Packet
->HostAdapterStatus
= ScsiRspHdr
->Response
;
2359 if (Packet
->HostAdapterStatus
!= ISCSI_SERVICE_RSP_COMMAND_COMPLETE_AT_TARGET
) {
2363 Packet
->TargetStatus
= ScsiRspHdr
->Status
;
2365 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
| SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
) ||
2366 ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
| SCSI_RSP_PDU_FLAG_UNDERFLOW
)
2368 return EFI_PROTOCOL_ERROR
;
2371 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
)) {
2372 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2373 Status
= EFI_BAD_BUFFER_SIZE
;
2376 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
)) {
2377 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2380 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2381 if (Packet
->DataDirection
== DataIn
) {
2382 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2384 Packet
->OutTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2387 Status
= EFI_BAD_BUFFER_SIZE
;
2390 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2391 if (Packet
->DataDirection
== DataIn
) {
2392 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2394 Packet
->OutTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2398 DataSegLen
= ISCSI_GET_DATASEG_LEN (ScsiRspHdr
);
2399 if (DataSegLen
!= 0) {
2400 SenseData
= (ISCSI_SENSE_DATA
*) NetbufGetByte (Pdu
, sizeof (SCSI_RESPONSE
), NULL
);
2402 SenseData
->Length
= NTOHS (SenseData
->Length
);
2404 Packet
->SenseDataLength
= (UINT8
) MIN (SenseData
->Length
, Packet
->SenseDataLength
);
2405 if (Packet
->SenseDataLength
!= 0) {
2406 CopyMem (Packet
->SenseData
, &SenseData
->Data
[0], Packet
->SenseDataLength
);
2409 Packet
->SenseDataLength
= 0;
2416 Process the received NOP In PDU.
2418 @param[in] Pdu The NOP In PDU received.
2419 @param[in] Tcb The task control block.
2421 @retval EFI_SUCCES The NOP In PDU is processed and the related sequence
2422 numbers are updated.
2423 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2431 ISCSI_NOP_IN
*NopInHdr
;
2434 NopInHdr
= (ISCSI_NOP_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2436 NopInHdr
->StatSN
= NTOHL (NopInHdr
->StatSN
);
2437 NopInHdr
->ExpCmdSN
= NTOHL (NopInHdr
->ExpCmdSN
);
2438 NopInHdr
->MaxCmdSN
= NTOHL (NopInHdr
->MaxCmdSN
);
2440 if (NopInHdr
->InitiatorTaskTag
== ISCSI_RESERVED_TAG
) {
2441 if (NopInHdr
->StatSN
!= Tcb
->Conn
->ExpStatSN
) {
2442 return EFI_PROTOCOL_ERROR
;
2445 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NopInHdr
->StatSN
);
2446 if (EFI_ERROR (Status
)) {
2451 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, NopInHdr
->MaxCmdSN
, NopInHdr
->ExpCmdSN
);
2457 Execute the SCSI command issued through the EXT SCSI PASS THRU protocol.
2459 @param[in] PassThru The EXT SCSI PASS THRU protocol.
2460 @param[in] Target The target ID.
2461 @param[in] Lun The LUN.
2462 @param[in, out] Packet The request packet containing IO request, SCSI command
2463 buffer and buffers to read/write.
2465 @retval EFI_SUCCES The SCSI command is executed and the result is updated to
2467 @retval EFI_DEVICE_ERROR Session state was not as required.
2468 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2469 @retval Others Other errors as indicated.
2472 IScsiExecuteScsiCommand (
2473 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*PassThru
,
2476 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2480 ISCSI_DRIVER_DATA
*Private
;
2481 ISCSI_SESSION
*Session
;
2482 EFI_EVENT TimeoutEvent
;
2483 ISCSI_CONNECTION
*Conn
;
2486 ISCSI_XFER_CONTEXT
*XferContext
;
2488 ISCSI_IN_BUFFER_CONTEXT InBufferContext
;
2492 Private
= ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru
);
2493 Session
= &Private
->Session
;
2494 Status
= EFI_SUCCESS
;
2496 TimeoutEvent
= NULL
;
2499 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
2500 return EFI_DEVICE_ERROR
;
2503 Conn
= NET_LIST_USER_STRUCT_S (
2504 Session
->Conns
.ForwardLink
,
2507 ISCSI_CONNECTION_SIGNATURE
2510 if (Packet
->Timeout
!= 0) {
2511 Timeout
= MultU64x32 (Packet
->Timeout
, 2);
2514 Status
= IScsiNewTcb (Conn
, &Tcb
);
2515 if (EFI_ERROR (Status
)) {
2519 // Encapsulate the SCSI request packet into an iSCSI SCSI Command PDU.
2521 Pdu
= IScsiNewScsiCmdPdu (Packet
, Lun
, Tcb
);
2523 Status
= EFI_OUT_OF_RESOURCES
;
2527 XferContext
= &Tcb
->XferContext
;
2528 Buffer
= NetbufGetByte (Pdu
, 0, NULL
);
2529 XferContext
->Offset
= ISCSI_GET_DATASEG_LEN (Buffer
);
2532 // Transmit the SCSI Command PDU.
2534 Status
= Tcp4IoTransmit (&Conn
->Tcp4Io
, Pdu
);
2538 if (EFI_ERROR (Status
)) {
2542 if (!Session
->InitialR2T
&&
2543 (XferContext
->Offset
< Session
->FirstBurstLength
) &&
2544 (XferContext
->Offset
< Packet
->OutTransferLength
)
2547 // Unsolicited Data-Out sequence is allowed, there is remaining SCSI
2548 // OUT data and the limit of FirstBurstLength is not reached.
2550 XferContext
->TargetTransferTag
= ISCSI_RESERVED_TAG
;
2551 XferContext
->DesiredLength
= MIN (
2552 Session
->FirstBurstLength
,
2553 Packet
->OutTransferLength
- XferContext
->Offset
2556 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2557 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2558 if (EFI_ERROR (Status
)) {
2563 InBufferContext
.InData
= (UINT8
*) Packet
->InDataBuffer
;
2564 InBufferContext
.InDataLen
= Packet
->InTransferLength
;
2566 while (!Tcb
->StatusXferd
) {
2568 // Start the timeout timer.
2571 Status
= gBS
->SetTimer (Conn
->TimeoutEvent
, TimerRelative
, Timeout
);
2572 if (EFI_ERROR (Status
)) {
2575 TimeoutEvent
= Conn
->TimeoutEvent
;
2578 // try to receive PDU from target.
2580 Status
= IScsiReceivePdu (Conn
, &Pdu
, &InBufferContext
, FALSE
, FALSE
, TimeoutEvent
);
2581 if (EFI_ERROR (Status
)) {
2585 switch (ISCSI_GET_OPCODE (NetbufGetByte (Pdu
, 0, NULL
))) {
2586 case ISCSI_OPCODE_SCSI_DATA_IN
:
2587 Status
= IScsiOnDataInRcvd (Pdu
, Tcb
, Packet
);
2590 case ISCSI_OPCODE_R2T
:
2591 Status
= IScsiOnR2TRcvd (Pdu
, Tcb
, Lun
, Packet
);
2594 case ISCSI_OPCODE_SCSI_RSP
:
2595 Status
= IScsiOnScsiRspRcvd (Pdu
, Tcb
, Packet
);
2598 case ISCSI_OPCODE_NOP_IN
:
2599 Status
= IScsiOnNopInRcvd (Pdu
, Tcb
);
2602 case ISCSI_OPCODE_VENDOR_T0
:
2603 case ISCSI_OPCODE_VENDOR_T1
:
2604 case ISCSI_OPCODE_VENDOR_T2
:
2606 // These messages are vendor specific, skip them.
2611 Status
= EFI_PROTOCOL_ERROR
;
2617 if (EFI_ERROR (Status
)) {
2624 if (TimeoutEvent
!= NULL
) {
2625 gBS
->SetTimer (TimeoutEvent
, TimerCancel
, 0);
2632 if ((Status
!= EFI_SUCCESS
) && (Status
!= EFI_NOT_READY
)) {
2634 // Reinstate the session.
2636 if (EFI_ERROR (IScsiSessionReinstatement (Private
))) {
2637 Status
= EFI_DEVICE_ERROR
;
2645 Reinstate the session on some error.
2647 @param[in, out] Private The iSCSI driver data.
2649 @retval EFI_SUCCES The session is reinstated from some error.
2650 @retval Other Reinstatement failed.
2653 IScsiSessionReinstatement (
2654 IN OUT ISCSI_DRIVER_DATA
*Private
2657 ISCSI_SESSION
*Session
;
2660 Session
= &Private
->Session
;
2661 ASSERT (Session
->State
== SESSION_STATE_LOGGED_IN
);
2664 // Abort the session and re-init it.
2666 IScsiSessionAbort (Session
);
2667 IScsiSessionInit (Session
, TRUE
);
2672 Status
= IScsiSessionLogin (Private
);
2678 Initialize some session parameters before login.
2680 @param[in, out] Session The iSCSI session.
2681 @param[in] Recovery Whether the request is from a fresh new start or recovery.
2685 IN OUT ISCSI_SESSION
*Session
,
2690 Session
->Signature
= ISCSI_SESSION_SIGNATURE
;
2691 Session
->State
= SESSION_STATE_FREE
;
2693 InitializeListHead (&Session
->Conns
);
2694 InitializeListHead (&Session
->TcbList
);
2700 Session
->InitiatorTaskTag
= 1;
2701 Session
->NextCid
= 1;
2703 Session
->TargetPortalGroupTag
= 0;
2704 Session
->MaxConnections
= ISCSI_MAX_CONNS_PER_SESSION
;
2705 Session
->InitialR2T
= FALSE
;
2706 Session
->ImmediateData
= TRUE
;
2707 Session
->MaxBurstLength
= 262144;
2708 Session
->FirstBurstLength
= MAX_RECV_DATA_SEG_LEN_IN_FFP
;
2709 Session
->DefaultTime2Wait
= 2;
2710 Session
->DefaultTime2Retain
= 20;
2711 Session
->MaxOutstandingR2T
= DEFAULT_MAX_OUTSTANDING_R2T
;
2712 Session
->DataPDUInOrder
= TRUE
;
2713 Session
->DataSequenceInOrder
= TRUE
;
2714 Session
->ErrorRecoveryLevel
= 0;
2718 Abort the iSCSI session, that is, reset all the connection and free the
2721 @param[in, out] Session The iSCSI session.
2723 @retval EFI_SUCCES The session is aborted.
2727 IN OUT ISCSI_SESSION
*Session
2730 ISCSI_DRIVER_DATA
*Private
;
2731 ISCSI_CONNECTION
*Conn
;
2733 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
2737 ASSERT (!IsListEmpty (&Session
->Conns
));
2739 Private
= ISCSI_DRIVER_DATA_FROM_SESSION (Session
);
2741 while (!IsListEmpty (&Session
->Conns
)) {
2742 Conn
= NET_LIST_USER_STRUCT_S (
2743 Session
->Conns
.ForwardLink
,
2746 ISCSI_CONNECTION_SIGNATURE
2749 gBS
->CloseProtocol (
2750 Conn
->Tcp4Io
.Handle
,
2751 &gEfiTcp4ProtocolGuid
,
2753 Private
->ExtScsiPassThruHandle
2756 IScsiConnReset (Conn
);
2758 IScsiDetatchConnection (Conn
);
2759 IScsiDestroyConnection (Conn
);
2762 Session
->State
= SESSION_STATE_FAILED
;