2 The implementation of iSCSI protocol based on RFC3720.
4 Copyright (c) 2004 - 2010, Intel Corporation.<BR>
5 All rights reserved. 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 // Create a connection for the session.
308 Conn
= IScsiCreateConnection (Private
, Session
);
310 return EFI_OUT_OF_RESOURCES
;
313 IScsiAttatchConnection (Session
, Conn
);
316 // Login througth the newly created connection.
318 Status
= IScsiConnLogin (Conn
);
319 if (EFI_ERROR (Status
)) {
320 IScsiConnReset (Conn
);
321 IScsiDetatchConnection (Conn
);
322 IScsiDestroyConnection (Conn
);
324 Session
->State
= SESSION_STATE_LOGGED_IN
;
328 &gEfiTcp4ProtocolGuid
,
331 Private
->ExtScsiPassThruHandle
,
332 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
340 Build and send the iSCSI login request to the iSCSI target according to
341 the current login stage.
343 @param[in] Conn The connection in the iSCSI login phase.
345 @retval EFI_SUCCESS The iSCSI login request PDU is built and sent on this
347 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
348 @retval EFI_DEVICE_ERROR Some kind of device error happened.
352 IN ISCSI_CONNECTION
*Conn
359 // build the Login Request PDU
361 Pdu
= IScsiPrepareLoginReq (Conn
);
363 return EFI_DEVICE_ERROR
;
366 // Send it to the iSCSI target.
368 Status
= Tcp4IoTransmit (&Conn
->Tcp4Io
, Pdu
);
376 Receive and process the iSCSI login response.
378 @param[in] Conn The connection in the iSCSI login phase.
380 @retval EFI_SUCCESS The iSCSI login response PDU is received and processed.
381 @retval Others Other errors as indicated.
384 IScsiReceiveLoginRsp (
385 IN ISCSI_CONNECTION
*Conn
392 // Receive the iSCSI login response.
394 Status
= IScsiReceivePdu (Conn
, &Pdu
, NULL
, FALSE
, FALSE
, NULL
);
395 if (EFI_ERROR (Status
)) {
399 // A Login Response is received, process it.
401 Status
= IScsiProcessLoginRsp (Conn
, Pdu
);
409 Add an iSCSI key-value pair as a string into the data segment of the Login Request PDU.
410 The DataSegmentLength and the actual size of the net buffer containing this PDU will be
413 @param[in, out] Pdu The iSCSI PDU whose data segment the key-value pair will
415 @param[in] Key The key name string.
416 @param[in] Value The value string.
418 @retval EFI_SUCCESS The key-valu pair is added to the PDU's datasegment and
419 the correspondence length fields are updated.
420 @retval EFI_OUT_OF_RESOURCES There is not enough space in the PDU to add the key-value
424 IScsiAddKeyValuePair (
434 ISCSI_LOGIN_REQUEST
*LoginReq
;
437 LoginReq
= (ISCSI_LOGIN_REQUEST
*) NetbufGetByte (Pdu
, 0, NULL
);
438 DataSegLen
= NTOH24 (LoginReq
->DataSegmentLength
);
440 KeyLen
= (UINT32
) AsciiStrLen (Key
);
441 ValueLen
= (UINT32
) AsciiStrLen (Value
);
444 // 1 byte for the key value separator '=' and 1 byte for the null
445 // delimiter after the value.
447 TotalLen
= KeyLen
+ 1 + ValueLen
+ 1;
450 // Allocate the space for the key-value pair.
452 Data
= (CHAR8
*)NetbufAllocSpace (Pdu
, TotalLen
, NET_BUF_TAIL
);
454 return EFI_OUT_OF_RESOURCES
;
459 CopyMem (Data
, Key
, KeyLen
);
468 CopyMem (Data
, Value
, ValueLen
);
474 // update the DataSegmentLength
476 ISCSI_SET_DATASEG_LEN (LoginReq
, DataSegLen
+ TotalLen
);
482 Prepare the iSCSI login request to be sent according to the current login status.
484 @param[in, out] Conn The connection in the iSCSI login phase.
486 @return The pointer to the net buffer containing the iSCSI login request built.
487 @retval Others Other errors as indicated.
490 IScsiPrepareLoginReq (
491 IN OUT ISCSI_CONNECTION
*Conn
494 ISCSI_SESSION
*Session
;
496 ISCSI_LOGIN_REQUEST
*LoginReq
;
499 Session
= Conn
->Session
;
501 Nbuf
= NetbufAlloc (sizeof (ISCSI_LOGIN_REQUEST
) + DEFAULT_MAX_RECV_DATA_SEG_LEN
);
506 LoginReq
= (ISCSI_LOGIN_REQUEST
*) NetbufAllocSpace (Nbuf
, sizeof (ISCSI_LOGIN_REQUEST
), NET_BUF_TAIL
);
507 ASSERT (LoginReq
!= NULL
);
508 ZeroMem (LoginReq
, sizeof (ISCSI_LOGIN_REQUEST
));
511 // Init the login request pdu
513 ISCSI_SET_OPCODE (LoginReq
, ISCSI_OPCODE_LOGIN_REQ
, ISCSI_REQ_IMMEDIATE
);
514 ISCSI_SET_STAGES (LoginReq
, Conn
->CurrentStage
, Conn
->NextStage
);
515 LoginReq
->VersionMax
= ISCSI_VERSION_MAX
;
516 LoginReq
->VersionMin
= ISCSI_VERSION_MIN
;
517 LoginReq
->Tsih
= HTONS (Session
->Tsih
);
518 LoginReq
->InitiatorTaskTag
= HTONL (Session
->InitiatorTaskTag
);
519 LoginReq
->Cid
= HTONS (Conn
->Cid
);
520 LoginReq
->CmdSN
= HTONL (Session
->CmdSN
);
523 // For the first Login Request on a coonection this is ExpStatSN for the
524 // old connection and this field is only valid if the Login Request restarts
526 // For subsequent Login Requests it is used to acknowledge the Login Responses
527 // with their increasing StatSN values.
529 LoginReq
->ExpStatSN
= HTONL (Conn
->ExpStatSN
);
530 CopyMem (LoginReq
->Isid
, Session
->Isid
, sizeof (LoginReq
->Isid
));
532 if (Conn
->PartialRspRcvd
) {
534 // A partial response, initiator must send an empty Login Request.
539 switch (Conn
->CurrentStage
) {
540 case ISCSI_SECURITY_NEGOTIATION
:
541 Status
= IScsiCHAPToSendReq (Conn
, Nbuf
);
544 case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
:
545 Status
= IScsiFillOpParams (Conn
, Nbuf
);
546 ISCSI_SET_FLAG (LoginReq
, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT
);
551 // something error happens...
553 Status
= EFI_DEVICE_ERROR
;
557 if (EFI_ERROR (Status
)) {
562 // Pad the data segment if needed.
564 IScsiPadSegment (Nbuf
, ISCSI_GET_DATASEG_LEN (LoginReq
));
566 // Check whether we will issue the stage transition signal?
568 Conn
->TransitInitiated
= (BOOLEAN
) ISCSI_FLAG_ON (LoginReq
, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT
);
575 Process the iSCSI Login Response.
577 @param[in, out] Conn The connection on which the iSCSI login response is received.
578 @param[in, out] Pdu The iSCSI login response PDU.
580 @retval EFI_SUCCESS The iSCSI login response PDU is processed and all check are passed.
581 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
582 @retval EFI_MEDIA_CHANGED Target is redirected.
583 @retval Others Other errors as indicated.
586 IScsiProcessLoginRsp (
587 IN OUT ISCSI_CONNECTION
*Conn
,
592 ISCSI_SESSION
*Session
;
593 ISCSI_LOGIN_RESPONSE
*LoginRsp
;
601 Session
= Conn
->Session
;
603 LoginRsp
= (ISCSI_LOGIN_RESPONSE
*) NetbufGetByte (Pdu
, 0, NULL
);
604 if (!ISCSI_CHECK_OPCODE (LoginRsp
, ISCSI_OPCODE_LOGIN_RSP
)) {
606 // It's not a Login Response
608 return EFI_PROTOCOL_ERROR
;
611 // Get the data segment if any.
613 DataSegLen
= ISCSI_GET_DATASEG_LEN (LoginRsp
);
614 if (DataSegLen
!= 0) {
615 DataSeg
= NetbufGetByte (Pdu
, sizeof (ISCSI_LOGIN_RESPONSE
), NULL
);
620 // Check the status class in the login response PDU.
622 switch (LoginRsp
->StatusClass
) {
623 case ISCSI_LOGIN_STATUS_SUCCESS
:
625 // Just break here, the response and the data segment will be processed later.
629 case ISCSI_LOGIN_STATUS_REDIRECTION
:
631 // The target may be moved to a different address
633 if (DataSeg
== NULL
) {
634 return EFI_PROTOCOL_ERROR
;
637 // Process the TargetAddress key-value strings in the data segment to update the
638 // target address info.
640 Status
= IScsiUpdateTargetAddress (Session
, (CHAR8
*)DataSeg
, DataSegLen
);
641 if (EFI_ERROR (Status
)) {
645 // Session will be restarted on this error status because the Target is
646 // redirected by this Login Response.
648 return EFI_MEDIA_CHANGED
;
652 // Initiator Error, Target Error, or any other undefined error code.
654 return EFI_PROTOCOL_ERROR
;
657 // The status is sucess, extract the wanted fields from the header segment.
659 Transit
= (BOOLEAN
) ISCSI_FLAG_ON (LoginRsp
, ISCSI_LOGIN_RSP_PDU_FLAG_TRANSIT
);
660 Continue
= (BOOLEAN
) ISCSI_FLAG_ON (LoginRsp
, ISCSI_LOGIN_RSP_PDU_FLAG_CONTINUE
);
662 CurrentStage
= (UINT8
) ISCSI_GET_CURRENT_STAGE (LoginRsp
);
663 NextStage
= (UINT8
) ISCSI_GET_NEXT_STAGE (LoginRsp
);
665 LoginRsp
->InitiatorTaskTag
= NTOHL (LoginRsp
->InitiatorTaskTag
);
667 if ((Transit
&& Continue
) ||
668 (CurrentStage
!= Conn
->CurrentStage
) ||
669 (!Conn
->TransitInitiated
&& Transit
) ||
670 (Transit
&& (NextStage
!= Conn
->NextStage
)) ||
671 (CompareMem (Session
->Isid
, LoginRsp
->Isid
, sizeof (LoginRsp
->Isid
)) != 0) ||
672 (LoginRsp
->InitiatorTaskTag
!= Session
->InitiatorTaskTag
)
675 // A Login Response with the C bit set to 1 MUST have the T bit set to 0;
676 // The CSG in the Login Response MUST be the same with the I-end of this connection;
677 // The T bit can't be 1 if the last Login Response sent by the initiator doesn't
678 // initiate the transistion;
679 // The NSG MUST be the same with the I-end of this connection if Transit is required.
680 // The ISID in the Login Response MUST be the same with this session.
682 return EFI_PROTOCOL_ERROR
;
685 LoginRsp
->StatSN
= NTOHL (LoginRsp
->StatSN
);
686 LoginRsp
->ExpCmdSN
= NTOHL (LoginRsp
->ExpCmdSN
);
687 LoginRsp
->MaxCmdSN
= NTOHL (LoginRsp
->MaxCmdSN
);
689 if ((Conn
->CurrentStage
== ISCSI_SECURITY_NEGOTIATION
) && (Conn
->CHAPStep
== ISCSI_CHAP_INITIAL
)) {
691 // It's the initial Login Response, initialize the local ExpStatSN, MaxCmdSN
694 Conn
->ExpStatSN
= LoginRsp
->StatSN
+ 1;
695 Session
->MaxCmdSN
= LoginRsp
->MaxCmdSN
;
696 Session
->ExpCmdSN
= LoginRsp
->ExpCmdSN
;
699 // Check the StatSN of this PDU
701 Status
= IScsiCheckSN (&Conn
->ExpStatSN
, LoginRsp
->StatSN
);
702 if (!EFI_ERROR (Status
)) {
704 // Update the MaxCmdSN and ExpCmdSN
706 IScsiUpdateCmdSN (Session
, LoginRsp
->MaxCmdSN
, LoginRsp
->ExpCmdSN
);
712 // Trim off the header segment.
714 NetbufTrim (Pdu
, sizeof (ISCSI_LOGIN_RESPONSE
), NET_BUF_HEAD
);
717 // Queue this login response first in case it's a partial response so that
718 // later when the full response list is received we can combine these scattered
719 // responses' data segment and then process it.
722 NetbufQueAppend (&Conn
->RspQue
, Pdu
);
724 Conn
->PartialRspRcvd
= Continue
;
727 // It's a partial response, have to wait for another or more Request/Response
728 // conversations to get the full response.
733 switch (CurrentStage
) {
734 case ISCSI_SECURITY_NEGOTIATION
:
736 // In security negotiation stage, let CHAP module handle it.
738 Status
= IScsiCHAPOnRspReceived (Conn
);
741 case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
:
743 // Response received with negotiation resonse on iSCSI parameters, check them.
745 Status
= IScsiCheckOpParams (Conn
);
750 // Should never get here.
752 Status
= EFI_PROTOCOL_ERROR
;
756 if (Transit
&& (Status
== EFI_SUCCESS
)) {
758 // Do the state transition.
760 Conn
->CurrentStage
= Conn
->NextStage
;
762 if (Conn
->CurrentStage
== ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
) {
763 Conn
->NextStage
= ISCSI_FULL_FEATURE_PHASE
;
766 // CurrentStage is iSCSI Full Feature, it's the Login-Final Response,
767 // get the TSIH from the Login Response.
769 Session
->Tsih
= NTOHS (LoginRsp
->Tsih
);
773 // Flush the response(s) received.
775 NetbufQueFlush (&Conn
->RspQue
);
781 Updated the target information according the data received in the iSCSI
782 login response with an target redirection status.
784 @param[in, out] Session The iSCSI session.
785 @param[in] Data The data segment which should contain the
786 TargetAddress key-value list.
787 @param[in] Len Length of the data.
789 @retval EFI_SUCCESS The target address is updated.
790 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
791 @retval EFI_NOT_FOUND The TargetAddress key is not found.
792 @retval Others Other errors as indicated.
795 IScsiUpdateTargetAddress (
796 IN OUT ISCSI_SESSION
*Session
,
801 LIST_ENTRY
*KeyValueList
;
802 CHAR8
*TargetAddress
;
807 KeyValueList
= IScsiBuildKeyValueList (Data
, Len
);
808 if (KeyValueList
== NULL
) {
809 return EFI_OUT_OF_RESOURCES
;
812 Status
= EFI_NOT_FOUND
;
815 TargetAddress
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_ADDRESS
);
816 if (TargetAddress
== NULL
) {
820 if (!NET_IS_DIGIT (TargetAddress
[0])) {
822 // The domainname of the target may be presented in three formats: a DNS host name,
823 // a dotted-decimal IPv4 address, or a bracketed IPv6 address. Only accept dotted
829 IpStr
= TargetAddress
;
831 while ((*TargetAddress
!= 0) && (*TargetAddress
!= ':') && (*TargetAddress
!= ',')) {
833 // NULL, ':' or ',' ends the IPv4 string.
838 if (*TargetAddress
== ',') {
840 // Comma and the portal group tag MUST be ommitted if the TargetAddress is sent
841 // as the result of a redirection.
844 } else if (*TargetAddress
== ':') {
845 *TargetAddress
= '\0';
849 Number
= AsciiStrDecimalToUintn (TargetAddress
);
850 if (Number
> 0xFFFF) {
853 Session
->ConfigData
.NvData
.TargetPort
= (UINT16
) Number
;
857 // The string only contains the IPv4 address. Use the well known port.
859 Session
->ConfigData
.NvData
.TargetPort
= ISCSI_WELL_KNOWN_PORT
;
862 // Update the target IP address.
864 Status
= IScsiAsciiStrToIp (IpStr
, &Session
->ConfigData
.NvData
.TargetIp
);
865 if (EFI_ERROR (Status
)) {
872 IScsiFreeKeyValueList (KeyValueList
);
878 The callback function to free the net buffer list.
880 @param[in] Arg The opaque parameter.
887 ASSERT (Arg
!= NULL
);
889 NetbufFreeList ((LIST_ENTRY
*) Arg
);
894 The callback function called in NetBufFree, it does nothing.
896 @param[in] Arg The opaque parameter.
906 Receive an iSCSI response PDU. An iSCSI response PDU contains an iSCSI PDU header and
907 an optional data segment. The two parts will be put into two blocks of buffers in the
908 net buffer. The digest check will be conducted in this function if needed and the digests
909 will be trimmed from the PDU buffer.
911 @param[in] Conn The iSCSI connection to receive data from.
912 @param[out] Pdu The received iSCSI pdu.
913 @param[in] Context The context used to describe information on the caller provided
914 buffer to receive data segment of the iSCSI pdu, it's optional.
915 @param[in] HeaderDigest Whether there will be header digest received.
916 @param[in] DataDigest Whether there will be data digest.
917 @param[in] TimeoutEvent The timeout event, it's optional.
919 @retval EFI_SUCCESS An iSCSI pdu is received.
920 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
921 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
922 @retval Others Other errors as indicated.
926 IN ISCSI_CONNECTION
*Conn
,
928 IN ISCSI_IN_BUFFER_CONTEXT
*Context
, OPTIONAL
929 IN BOOLEAN HeaderDigest
,
930 IN BOOLEAN DataDigest
,
931 IN EFI_EVENT TimeoutEvent OPTIONAL
934 LIST_ENTRY
*NbufList
;
941 NET_FRAGMENT Fragment
[2];
942 UINT32 FragmentCount
;
944 UINT32 PadAndCRC32
[2];
946 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
947 if (NbufList
== NULL
) {
948 return EFI_OUT_OF_RESOURCES
;
951 InitializeListHead (NbufList
);
954 // The header digest will be received together with the PDU header if exists.
956 Len
= sizeof (ISCSI_BASIC_HEADER
) + (HeaderDigest
? sizeof (UINT32
) : 0);
957 PduHdr
= NetbufAlloc (Len
);
958 if (PduHdr
== NULL
) {
960 return EFI_OUT_OF_RESOURCES
;
963 Header
= NetbufAllocSpace (PduHdr
, Len
, NET_BUF_TAIL
);
964 ASSERT (Header
!= NULL
);
965 InsertTailList (NbufList
, &PduHdr
->List
);
968 // First step, receive the BHS of the PDU.
970 Status
= Tcp4IoReceive (&Conn
->Tcp4Io
, PduHdr
, FALSE
, TimeoutEvent
);
971 if (EFI_ERROR (Status
)) {
977 // TODO: check the header-digest.
980 // Trim off the digest.
982 NetbufTrim (PduHdr
, sizeof (UINT32
), NET_BUF_TAIL
);
985 Len
= ISCSI_GET_DATASEG_LEN (Header
);
988 // No data segment.Form the pdu from a list of pdu segments.
990 *Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
992 Status
= EFI_OUT_OF_RESOURCES
;
998 // Get the length of the padding bytes of the data segment.
1000 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1002 switch (ISCSI_GET_OPCODE (Header
)) {
1003 case ISCSI_OPCODE_SCSI_DATA_IN
:
1005 // Try to use the buffer described by Context if the PDU is an
1006 // iSCSI SCSI data in pdu so as to reduce memory copy overhead.
1008 InDataOffset
= ISCSI_GET_BUFFER_OFFSET (Header
);
1009 if ((Context
== NULL
) || ((InDataOffset
+ Len
) > Context
->InDataLen
)) {
1010 Status
= EFI_PROTOCOL_ERROR
;
1014 Fragment
[0].Len
= Len
;
1015 Fragment
[0].Bulk
= Context
->InData
+ InDataOffset
;
1017 if (DataDigest
|| (PadLen
!= 0)) {
1019 // The data segment is padded, use two fragments to receive it.
1020 // The first to receive the useful data. The second to receive the padding.
1022 Fragment
[1].Len
= PadLen
+ (DataDigest
? sizeof (UINT32
) : 0);
1023 Fragment
[1].Bulk
= (UINT8
*)PadAndCRC32
+ (4 - PadLen
);
1029 DataSeg
= NetbufFromExt (&Fragment
[0], FragmentCount
, 0, 0, IScsiNbufExtFree
, NULL
);
1030 if (DataSeg
== NULL
) {
1031 Status
= EFI_OUT_OF_RESOURCES
;
1037 case ISCSI_OPCODE_SCSI_RSP
:
1038 case ISCSI_OPCODE_NOP_IN
:
1039 case ISCSI_OPCODE_LOGIN_RSP
:
1040 case ISCSI_OPCODE_TEXT_RSP
:
1041 case ISCSI_OPCODE_ASYNC_MSG
:
1042 case ISCSI_OPCODE_REJECT
:
1043 case ISCSI_OPCODE_VENDOR_T0
:
1044 case ISCSI_OPCODE_VENDOR_T1
:
1045 case ISCSI_OPCODE_VENDOR_T2
:
1047 // Allocate buffer to receive the data segment.
1049 Len
+= PadLen
+ (DataDigest
? sizeof (UINT32
) : 0);
1050 DataSeg
= NetbufAlloc (Len
);
1051 if (DataSeg
== NULL
) {
1052 Status
= EFI_OUT_OF_RESOURCES
;
1056 NetbufAllocSpace (DataSeg
, Len
, NET_BUF_TAIL
);
1060 Status
= EFI_PROTOCOL_ERROR
;
1064 InsertTailList (NbufList
, &DataSeg
->List
);
1067 // Receive the data segment with the data digest if any.
1069 Status
= Tcp4IoReceive (&Conn
->Tcp4Io
, DataSeg
, FALSE
, TimeoutEvent
);
1070 if (EFI_ERROR (Status
)) {
1076 // TODO: Check the data digest.
1078 NetbufTrim (DataSeg
, sizeof (UINT32
), NET_BUF_TAIL
);
1083 // Trim off the padding bytes in the data segment.
1085 NetbufTrim (DataSeg
, PadLen
, NET_BUF_TAIL
);
1089 // Form the pdu from a list of pdu segments.
1091 *Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1093 Status
= EFI_OUT_OF_RESOURCES
;
1098 if (EFI_ERROR (Status
)) {
1100 // Free the Nbufs in this NbufList and the NbufList itself.
1102 IScsiFreeNbufList (NbufList
);
1109 Check and get the result of the prameter negotiation.
1111 @param[in, out] Conn The connection in iSCSI login.
1113 @retval EFI_SUCCESS The parmeter check is passed and negotiation is finished.
1114 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
1115 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1118 IScsiCheckOpParams (
1119 IN OUT ISCSI_CONNECTION
*Conn
1123 LIST_ENTRY
*KeyValueList
;
1126 ISCSI_SESSION
*Session
;
1130 ASSERT (Conn
->RspQue
.BufNum
!= 0);
1132 Session
= Conn
->Session
;
1134 Len
= Conn
->RspQue
.BufSize
;
1135 Data
= AllocatePool (Len
);
1137 return EFI_OUT_OF_RESOURCES
;
1140 NetbufQueCopy (&Conn
->RspQue
, 0, Len
, (UINT8
*) Data
);
1142 Status
= EFI_PROTOCOL_ERROR
;
1145 // Extract the Key-Value pairs into a list.
1147 KeyValueList
= IScsiBuildKeyValueList (Data
, Len
);
1148 if (KeyValueList
== NULL
) {
1155 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_HEADER_DIGEST
);
1156 if (Value
== NULL
) {
1160 if (AsciiStrCmp (Value
, "CRC32") == 0) {
1161 if (Conn
->HeaderDigest
!= IScsiDigestCRC32
) {
1164 } else if (AsciiStrCmp (Value
, ISCSI_KEY_VALUE_NONE
) == 0) {
1165 Conn
->HeaderDigest
= IScsiDigestNone
;
1172 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_DIGEST
);
1173 if (Value
== NULL
) {
1177 if (AsciiStrCmp (Value
, "CRC32") == 0) {
1178 if (Conn
->DataDigest
!= IScsiDigestCRC32
) {
1181 } else if (AsciiStrCmp (Value
, ISCSI_KEY_VALUE_NONE
) == 0) {
1182 Conn
->DataDigest
= IScsiDigestNone
;
1187 // ErrorRecoveryLevel, result fuction is Minimum.
1189 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_ERROR_RECOVERY_LEVEL
);
1190 if (Value
== NULL
) {
1194 NumericValue
= AsciiStrDecimalToUintn (Value
);
1195 if (NumericValue
> 2) {
1199 Session
->ErrorRecoveryLevel
= (UINT8
) MIN (Session
->ErrorRecoveryLevel
, NumericValue
);
1202 // InitialR2T, result function is OR.
1204 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_INITIAL_R2T
);
1205 if (Value
== NULL
) {
1209 Session
->InitialR2T
= (BOOLEAN
) (Session
->InitialR2T
|| (AsciiStrCmp (Value
, "Yes") == 0));
1212 // ImmediateData, result function is AND.
1214 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_IMMEDIATE_DATA
);
1215 if (Value
== NULL
) {
1219 Session
->ImmediateData
= (BOOLEAN
) (Session
->ImmediateData
&& (AsciiStrCmp (Value
, "Yes") == 0));
1222 // MaxRecvDataSegmentLength is declarative.
1224 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
);
1225 if (Value
!= NULL
) {
1226 Conn
->MaxRecvDataSegmentLength
= (UINT32
) AsciiStrDecimalToUintn (Value
);
1229 // MaxBurstLength, result funtion is Mininum.
1231 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_BURST_LENGTH
);
1232 if (Value
== NULL
) {
1236 NumericValue
= AsciiStrDecimalToUintn (Value
);
1237 Session
->MaxBurstLength
= (UINT32
) MIN (Session
->MaxBurstLength
, NumericValue
);
1240 // FirstBurstLength, result function is Minimum. Irrelevant when InitialR2T=Yes and
1241 // ImmediateData=No.
1242 // This Key/Value is negotiation type.
1244 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_FIRST_BURST_LENGTH
);
1245 if (Value
== NULL
) {
1249 NumericValue
= AsciiStrDecimalToUintn (Value
);
1250 Session
->FirstBurstLength
= (UINT32
) MIN (Session
->FirstBurstLength
, NumericValue
);
1253 // MaxConnections, result function is Minimum.
1255 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_CONNECTIONS
);
1256 if (Value
== NULL
) {
1260 NumericValue
= AsciiStrDecimalToUintn (Value
);
1261 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1265 Session
->MaxConnections
= (UINT32
) MIN (Session
->MaxConnections
, NumericValue
);
1268 // DataPDUInOrder, result function is OR.
1270 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_PDU_IN_ORDER
);
1271 if (Value
== NULL
) {
1275 Session
->DataPDUInOrder
= (BOOLEAN
) (Session
->DataPDUInOrder
|| (AsciiStrCmp (Value
, "Yes") == 0));
1278 // DataSequenceInorder, result function is OR.
1280 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
);
1281 if (Value
== NULL
) {
1285 Session
->DataSequenceInOrder
= (BOOLEAN
) (Session
->DataSequenceInOrder
|| (AsciiStrCmp (Value
, "Yes") == 0));
1288 // DefaultTime2Wait, result function is Maximum.
1290 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2WAIT
);
1291 if (Value
== NULL
) {
1295 NumericValue
= AsciiStrDecimalToUintn (Value
);
1296 if (NumericValue
== 0) {
1297 Session
->DefaultTime2Wait
= 0;
1298 } else if (NumericValue
> 3600) {
1301 Session
->DefaultTime2Wait
= (UINT32
) MAX (Session
->DefaultTime2Wait
, NumericValue
);
1304 // DefaultTime2Retain, result function is Minimum.
1306 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2RETAIN
);
1307 if (Value
== NULL
) {
1311 NumericValue
= AsciiStrDecimalToUintn (Value
);
1312 if (NumericValue
== 0) {
1313 Session
->DefaultTime2Retain
= 0;
1314 } else if (NumericValue
> 3600) {
1317 Session
->DefaultTime2Retain
= (UINT32
) MIN (Session
->DefaultTime2Retain
, NumericValue
);
1320 // MaxOutstandingR2T, result function is Minimum.
1322 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_OUTSTANDING_R2T
);
1323 if (Value
== NULL
) {
1327 NumericValue
= AsciiStrDecimalToUintn (Value
);
1328 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1332 Session
->MaxOutstandingR2T
= (UINT16
) MIN (Session
->MaxOutstandingR2T
, NumericValue
);
1335 // Remove declarative key-value paris if any.
1337 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_SESSION_TYPE
);
1338 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_ALIAS
);
1339 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_PORTAL_GROUP_TAG
);
1341 if (IsListEmpty (KeyValueList
)) {
1343 // Succeed if no more keys in the list.
1345 Status
= EFI_SUCCESS
;
1350 IScsiFreeKeyValueList (KeyValueList
);
1358 Fill the oprational prameters.
1360 @param[in] Conn The connection in iSCSI login.
1361 @param[in, out] Pdu The iSCSI login request PDU to fill the parameters.
1363 @retval EFI_SUCCESS The parmeters are filled into the iSCSI login request PDU.
1367 IN ISCSI_CONNECTION
*Conn
,
1371 ISCSI_SESSION
*Session
;
1374 Session
= Conn
->Session
;
1376 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->HeaderDigest
== IScsiDigestCRC32
) ? "None,CRC32" : "None");
1377 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_HEADER_DIGEST
, Value
);
1379 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->DataDigest
== IScsiDigestCRC32
) ? "None,CRC32" : "None");
1380 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_DIGEST
, Value
);
1382 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->ErrorRecoveryLevel
);
1383 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_ERROR_RECOVERY_LEVEL
, Value
);
1385 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->InitialR2T
? "Yes" : "No");
1386 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_INITIAL_R2T
, Value
);
1388 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->ImmediateData
? "Yes" : "No");
1389 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_IMMEDIATE_DATA
, Value
);
1391 AsciiSPrint (Value
, sizeof (Value
), "%d", MAX_RECV_DATA_SEG_LEN_IN_FFP
);
1392 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
, Value
);
1394 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxBurstLength
);
1395 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_BURST_LENGTH
, Value
);
1397 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->FirstBurstLength
);
1398 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_FIRST_BURST_LENGTH
, Value
);
1400 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxConnections
);
1401 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_CONNECTIONS
, Value
);
1403 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataPDUInOrder
? "Yes" : "No");
1404 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_PDU_IN_ORDER
, Value
);
1406 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataSequenceInOrder
? "Yes" : "No");
1407 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
, Value
);
1409 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Wait
);
1410 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2WAIT
, Value
);
1412 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Retain
);
1413 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2RETAIN
, Value
);
1415 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxOutstandingR2T
);
1416 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_OUTSTANDING_R2T
, Value
);
1422 Pad the iSCSI AHS or data segment to an integer number of 4 byte words.
1424 @param[in, out] Pdu The iSCSI pdu which contains segments to pad.
1425 @param[in] Len The length of the last semgnet in the PDU.
1427 @retval EFI_SUCCESS The segment is padded or no need to pad it.
1428 @retval EFI_OUT_OF_RESOURCES There is not enough remaining free space to add the
1433 IN OUT NET_BUF
*Pdu
,
1440 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1443 Data
= NetbufAllocSpace (Pdu
, PadLen
, NET_BUF_TAIL
);
1445 return EFI_OUT_OF_RESOURCES
;
1448 ZeroMem (Data
, PadLen
);
1455 Build a key-value list from the data segment.
1457 @param[in] Data The data segment containing the key-value pairs.
1458 @param[in] Len Length of the data segment.
1460 @return The key-value list.
1461 @retval NULL Other errors as indicated.
1464 IScsiBuildKeyValueList (
1469 LIST_ENTRY
*ListHead
;
1470 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1472 ListHead
= AllocatePool (sizeof (LIST_ENTRY
));
1473 if (ListHead
== NULL
) {
1477 InitializeListHead (ListHead
);
1480 KeyValuePair
= AllocatePool (sizeof (ISCSI_KEY_VALUE_PAIR
));
1481 if (KeyValuePair
== NULL
) {
1485 InitializeListHead (&KeyValuePair
->List
);
1487 KeyValuePair
->Key
= Data
;
1489 while ((Len
> 0) && (*Data
!= '=')) {
1500 FreePool (KeyValuePair
);
1504 KeyValuePair
->Value
= Data
;
1506 InsertTailList (ListHead
, &KeyValuePair
->List
);;
1508 Data
+= AsciiStrLen (KeyValuePair
->Value
) + 1;
1509 Len
-= (UINT32
) AsciiStrLen (KeyValuePair
->Value
) + 1;
1516 IScsiFreeKeyValueList (ListHead
);
1522 Get the value string by the key name from the key-value list. If found,
1523 the key-value entry will be removed from the list.
1525 @param[in, out] KeyValueList The key-value list.
1526 @param[in] Key The key name to find.
1528 @return The value string.
1531 IScsiGetValueByKeyFromList (
1532 IN OUT LIST_ENTRY
*KeyValueList
,
1537 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1542 NET_LIST_FOR_EACH (Entry
, KeyValueList
) {
1543 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1545 if (AsciiStrCmp (KeyValuePair
->Key
, Key
) == 0) {
1546 Value
= KeyValuePair
->Value
;
1548 RemoveEntryList (&KeyValuePair
->List
);
1549 FreePool (KeyValuePair
);
1558 Free the key-value list.
1560 @param[in] KeyValueList The key-value list.
1563 IScsiFreeKeyValueList (
1564 IN LIST_ENTRY
*KeyValueList
1568 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1570 while (!IsListEmpty (KeyValueList
)) {
1571 Entry
= NetListRemoveHead (KeyValueList
);
1572 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1574 FreePool (KeyValuePair
);
1577 FreePool (KeyValueList
);
1581 Normalize the iSCSI name according to RFC.
1583 @param[in, out] Name The iSCSI name.
1584 @param[in] Len length of the iSCSI name.
1586 @retval EFI_SUCCESS The iSCSI name is valid and normalized.
1587 @retval EFI_PROTOCOL_ERROR The iSCSI name is mal-formatted or not in the IQN format.
1590 IScsiNormalizeName (
1597 for (Index
= 0; Index
< Len
; Index
++) {
1598 if (NET_IS_UPPER_CASE_CHAR (Name
[Index
])) {
1600 // Convert the upper-case characters to lower-case ones
1602 Name
[Index
] = (CHAR8
) (Name
[Index
] - 'A' + 'a');
1605 if (!NET_IS_LOWER_CASE_CHAR (Name
[Index
]) &&
1606 !NET_IS_DIGIT (Name
[Index
]) &&
1607 (Name
[Index
] != '-') &&
1608 (Name
[Index
] != '.') &&
1609 (Name
[Index
] != ':')
1612 // ASCII dash, dot, colon lower-case characters and digit characters
1615 return EFI_PROTOCOL_ERROR
;
1619 if ((Len
< 4) || (CompareMem (Name
, "iqn.", 4) != 0)) {
1621 // Only IQN format is accepted now.
1623 return EFI_PROTOCOL_ERROR
;
1630 Create an iSCSI task control block.
1632 @param[in] Conn The connection on which the task control block will be created.
1633 @param[out] Tcb The newly created task control block.
1635 @retval EFI_SUCCESS The task control block is created.
1636 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1637 @retval EFI_NOT_READY The target can not accept new commands.
1641 IN ISCSI_CONNECTION
*Conn
,
1645 ISCSI_SESSION
*Session
;
1648 ASSERT (Tcb
!= NULL
);
1650 Session
= Conn
->Session
;
1652 if (ISCSI_SEQ_GT (Session
->CmdSN
, Session
->MaxCmdSN
)) {
1653 return EFI_NOT_READY
;
1656 NewTcb
= AllocateZeroPool (sizeof (ISCSI_TCB
));
1657 if (NewTcb
== NULL
) {
1658 return EFI_OUT_OF_RESOURCES
;
1661 InitializeListHead (&NewTcb
->Link
);
1663 NewTcb
->SoFarInOrder
= TRUE
;
1664 NewTcb
->InitiatorTaskTag
= Session
->InitiatorTaskTag
;
1665 NewTcb
->CmdSN
= Session
->CmdSN
;
1666 NewTcb
->Conn
= Conn
;
1668 InsertTailList (&Session
->TcbList
, &NewTcb
->Link
);
1671 // Advance the initiator task tag.
1673 Session
->InitiatorTaskTag
++;
1682 Delete the tcb from the connection and destroy it.
1684 @param[in] Tcb The tcb to delete.
1691 RemoveEntryList (&Tcb
->Link
);
1697 Find the task control block by the initator task tag.
1699 @param[in] TcbList The tcb list.
1700 @param[in] InitiatorTaskTag The initiator task tag.
1702 @return The task control block found.
1706 IN LIST_ENTRY
*TcbList
,
1707 IN UINT32 InitiatorTaskTag
1715 NET_LIST_FOR_EACH (Entry
, TcbList
) {
1716 Tcb
= NET_LIST_USER_STRUCT (Entry
, ISCSI_TCB
, Link
);
1718 if (Tcb
->InitiatorTaskTag
== InitiatorTaskTag
) {
1729 Create a data segment, pad it and calculate the CRC if needed.
1731 @param[in] Data The data to fill into the data segment.
1732 @param[in] Len Length of the data.
1733 @param[in] DataDigest Whether to calculate CRC for this data segment.
1735 @return The net buffer wrapping the data segment.
1738 IScsiNewDataSegment (
1741 IN BOOLEAN DataDigest
1744 NET_FRAGMENT Fragment
[2];
1745 UINT32 FragmentCount
;
1749 Fragment
[0].Len
= Len
;
1750 Fragment
[0].Bulk
= Data
;
1752 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1754 Fragment
[1].Len
= PadLen
;
1755 Fragment
[1].Bulk
= (UINT8
*) &mDataSegPad
;
1762 DataSeg
= NetbufFromExt (&Fragment
[0], FragmentCount
, 0, 0, IScsiNbufExtFree
, NULL
);
1768 Create a iSCSI SCSI command PDU to encapsulate the command issued
1769 by SCSI through the EXT SCSI PASS THRU Protocol.
1771 @param[in] Packet The EXT SCSI PASS THRU request packet containing the SCSI command.
1772 @param[in] Lun The LUN.
1773 @param[in] Tcb The tcb assocated with this SCSI command.
1775 @return The created iSCSI SCSI command PDU.
1776 @retval NULL Other errors as indicated.
1779 IScsiNewScsiCmdPdu (
1780 IN EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
1785 LIST_ENTRY
*NbufList
;
1789 SCSI_COMMAND
*ScsiCmd
;
1792 ISCSI_ADDITIONAL_HEADER
*Header
;
1793 ISCSI_BI_EXP_READ_DATA_LEN_AHS
*BiExpReadDataLenAHS
;
1794 ISCSI_SESSION
*Session
;
1795 UINT32 ImmediateDataLen
;
1799 if (Packet
->DataDirection
== DataBi
) {
1801 // Bi directional Read/Write command, the bidirectional expected
1802 // read data length AHS is required.
1804 AHSLength
+= sizeof (ISCSI_BI_EXP_READ_DATA_LEN_AHS
);
1807 if (Packet
->CdbLength
> 16) {
1809 // The CDB exceeds 16 bytes, an extended CDB AHS is required.
1811 AHSLength
= (UINT8
) (AHSLength
+ (ISCSI_ROUNDUP (Packet
->CdbLength
- 16) + sizeof (ISCSI_ADDITIONAL_HEADER
)));
1814 Length
= sizeof (SCSI_COMMAND
) + AHSLength
;
1815 PduHeader
= NetbufAlloc (Length
);
1816 if (PduHeader
== NULL
) {
1820 ScsiCmd
= (SCSI_COMMAND
*) NetbufAllocSpace (PduHeader
, Length
, NET_BUF_TAIL
);
1821 if (ScsiCmd
== NULL
) {
1822 NetbufFree (PduHeader
);
1825 Header
= (ISCSI_ADDITIONAL_HEADER
*) (ScsiCmd
+ 1);
1827 ZeroMem (ScsiCmd
, Length
);
1829 ISCSI_SET_OPCODE (ScsiCmd
, ISCSI_OPCODE_SCSI_CMD
, 0);
1830 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_TASK_ATTR_SIMPLE
);
1833 // Set the READ/WRITE flags according to the IO type of this request.
1835 switch (Packet
->DataDirection
) {
1837 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
);
1838 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->InTransferLength
);
1842 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_WRITE
);
1843 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
1847 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
| SCSI_CMD_PDU_FLAG_WRITE
);
1848 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
1851 // Fill the bidirectional expected read data length AHS.
1853 BiExpReadDataLenAHS
= (ISCSI_BI_EXP_READ_DATA_LEN_AHS
*) Header
;
1854 Header
= (ISCSI_ADDITIONAL_HEADER
*) (BiExpReadDataLenAHS
+ 1);
1856 BiExpReadDataLenAHS
->Length
= NTOHS (5);
1857 BiExpReadDataLenAHS
->Type
= ISCSI_AHS_TYPE_BI_EXP_READ_DATA_LEN
;
1858 BiExpReadDataLenAHS
->ExpReadDataLength
= NTOHL (Packet
->InTransferLength
);
1863 ScsiCmd
->TotalAHSLength
= AHSLength
;
1864 CopyMem (ScsiCmd
->Lun
, &Lun
, sizeof (ScsiCmd
->Lun
));
1865 ScsiCmd
->InitiatorTaskTag
= NTOHL (Tcb
->InitiatorTaskTag
);
1866 ScsiCmd
->CmdSN
= NTOHL (Tcb
->CmdSN
);
1867 ScsiCmd
->ExpStatSN
= NTOHL (Tcb
->Conn
->ExpStatSN
);
1869 CopyMem (ScsiCmd
->Cdb
, Packet
->Cdb
, sizeof (ScsiCmd
->Cdb
));
1871 if (Packet
->CdbLength
> 16) {
1872 Header
->Length
= NTOHS ((UINT16
) (Packet
->CdbLength
- 15));
1873 Header
->Type
= ISCSI_AHS_TYPE_EXT_CDB
;
1875 CopyMem (Header
+ 1, (UINT8
*) Packet
->Cdb
+ 16, Packet
->CdbLength
- 16);
1879 Session
= Tcb
->Conn
->Session
;
1880 ImmediateDataLen
= 0;
1882 if (Session
->ImmediateData
&& (Packet
->OutTransferLength
!= 0)) {
1884 // Send immediate data in this SCSI Command PDU. The length of the immeidate
1885 // data is the minimum of FirstBurstLength, the data length to be xfered and
1886 // the MaxRecvdataSegmentLength on this connection.
1888 ImmediateDataLen
= MIN (Session
->FirstBurstLength
, Packet
->OutTransferLength
);
1889 ImmediateDataLen
= MIN (ImmediateDataLen
, Tcb
->Conn
->MaxRecvDataSegmentLength
);
1892 // Update the data segment length in the PDU header.
1894 ISCSI_SET_DATASEG_LEN (ScsiCmd
, ImmediateDataLen
);
1897 // Create the data segment.
1899 DataSeg
= IScsiNewDataSegment ((UINT8
*) Packet
->OutDataBuffer
, ImmediateDataLen
, FALSE
);
1900 if (DataSeg
== NULL
) {
1901 NetbufFree (PduHeader
);
1906 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
1907 if (NbufList
== NULL
) {
1908 NetbufFree (PduHeader
);
1909 NetbufFree (DataSeg
);
1915 InitializeListHead (NbufList
);
1916 InsertTailList (NbufList
, &PduHeader
->List
);
1917 InsertTailList (NbufList
, &DataSeg
->List
);
1919 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1921 IScsiFreeNbufList (NbufList
);
1925 if (Session
->InitialR2T
||
1926 (ImmediateDataLen
== Session
->FirstBurstLength
) ||
1927 (ImmediateDataLen
== Packet
->OutTransferLength
)
1930 // Unsolicited data out sequence is not allowed,
1931 // or FirstBurstLength data is already sent out by immediate data
1932 // or all the OUT data accompany this SCSI packet is sent as
1933 // immediate data, the final flag should be set on this SCSI Command
1936 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_BHS_FLAG_FINAL
);
1945 Create a new iSCSI SCSI Data Out PDU.
1947 @param[in] Data The data to put into the Data Out PDU.
1948 @param[in] Len Length of the data.
1949 @param[in] DataSN The DataSN of the Data Out PDU.
1950 @param[in] Tcb The task control block of this Data Out PDU.
1951 @param[in] Lun The LUN.
1953 @return The net buffer wrapping the Data Out PDU.
1954 @retval NULL Other errors as indicated.
1957 IScsiNewDataOutPdu (
1965 LIST_ENTRY
*NbufList
;
1969 ISCSI_SCSI_DATA_OUT
*DataOutHdr
;
1970 ISCSI_XFER_CONTEXT
*XferContext
;
1972 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
1973 if (NbufList
== NULL
) {
1977 InitializeListHead (NbufList
);
1980 // Allocate memory for the BHS.
1982 PduHdr
= NetbufAlloc (sizeof (ISCSI_SCSI_DATA_OUT
));
1983 if (PduHdr
== NULL
) {
1984 FreePool (NbufList
);
1988 // Insert the BHS into the buffer list.
1990 InsertTailList (NbufList
, &PduHdr
->List
);
1992 DataOutHdr
= (ISCSI_SCSI_DATA_OUT
*) NetbufAllocSpace (PduHdr
, sizeof (ISCSI_SCSI_DATA_OUT
), NET_BUF_TAIL
);
1993 ASSERT (DataOutHdr
!= NULL
);
1994 XferContext
= &Tcb
->XferContext
;
1996 ZeroMem (DataOutHdr
, sizeof (ISCSI_SCSI_DATA_OUT
));
1999 // Set the flags and fields of the Data Out PDU BHS.
2001 ISCSI_SET_OPCODE (DataOutHdr
, ISCSI_OPCODE_SCSI_DATA_OUT
, 0);
2002 ISCSI_SET_DATASEG_LEN (DataOutHdr
, Len
);
2004 DataOutHdr
->InitiatorTaskTag
= HTONL (Tcb
->InitiatorTaskTag
);
2005 DataOutHdr
->TargetTransferTag
= HTONL (XferContext
->TargetTransferTag
);
2006 DataOutHdr
->ExpStatSN
= HTONL (Tcb
->Conn
->ExpStatSN
);
2007 DataOutHdr
->DataSN
= HTONL (DataSN
);
2008 DataOutHdr
->BufferOffset
= HTONL (XferContext
->Offset
);
2010 if (XferContext
->TargetTransferTag
!= ISCSI_RESERVED_TAG
) {
2011 CopyMem (&DataOutHdr
->Lun
, &Lun
, sizeof (DataOutHdr
->Lun
));
2014 // Build the data segment for this Data Out PDU.
2016 DataSeg
= IScsiNewDataSegment (Data
, Len
, FALSE
);
2017 if (DataSeg
== NULL
) {
2018 IScsiFreeNbufList (NbufList
);
2022 // Put the data segment into the buffer list and combine it with the BHS
2023 // into a full Data Out PDU.
2025 InsertTailList (NbufList
, &DataSeg
->List
);
2026 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
2028 IScsiFreeNbufList (NbufList
);
2035 Generate a consecutive sequence of iSCSI SCSI Data Out PDUs.
2037 @param[in] Data The data which will be carried by the sequence of iSCSI SCSI Data Out PDUs.
2038 @param[in] Tcb The task control block of the data to send out.
2039 @param[in] Lun The LUN the data will be sent to.
2041 @return A list of net buffers with each of them wraps an iSCSI SCSI Data Out PDU.
2042 @retval NULL Other errors as indicated.
2045 IScsiGenerateDataOutPduSequence (
2051 LIST_ENTRY
*PduList
;
2054 NET_BUF
*DataOutPdu
;
2055 ISCSI_CONNECTION
*Conn
;
2056 ISCSI_XFER_CONTEXT
*XferContext
;
2058 PduList
= AllocatePool (sizeof (LIST_ENTRY
));
2059 if (PduList
== NULL
) {
2063 InitializeListHead (PduList
);
2068 XferContext
= &Tcb
->XferContext
;
2070 while (XferContext
->DesiredLength
> 0) {
2072 // Determine the length of data this Data Out PDU can carry.
2074 DataLen
= MIN (XferContext
->DesiredLength
, Conn
->MaxRecvDataSegmentLength
);
2077 // Create a Data Out PDU.
2079 DataOutPdu
= IScsiNewDataOutPdu (Data
, DataLen
, DataSN
, Tcb
, Lun
);
2080 if (DataOutPdu
== NULL
) {
2081 IScsiFreeNbufList (PduList
);
2087 InsertTailList (PduList
, &DataOutPdu
->List
);
2090 // Update the context and DataSN.
2092 XferContext
->Offset
+= DataLen
;
2093 XferContext
->DesiredLength
-= DataLen
;
2098 // Set the F bit for the last data out PDU in this sequence.
2100 ISCSI_SET_FLAG (NetbufGetByte (DataOutPdu
, 0, NULL
), ISCSI_BHS_FLAG_FINAL
);
2108 Send the Data in a sequence of Data Out PDUs one by one.
2110 @param[in] Data The data to carry by Data Out PDUs.
2111 @param[in] Lun The LUN the data will be sent to.
2112 @param[in] Tcb The task control block.
2114 @retval EFI_SUCCES The data is sent out to the LUN.
2115 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2116 @retval Others Other errors as indicated.
2119 IScsiSendDataOutPduSequence (
2125 LIST_ENTRY
*DataOutPduList
;
2131 // Generate the Data Out PDU sequence.
2133 DataOutPduList
= IScsiGenerateDataOutPduSequence (Data
, Tcb
, Lun
);
2134 if (DataOutPduList
== NULL
) {
2135 return EFI_OUT_OF_RESOURCES
;
2138 Status
= EFI_SUCCESS
;
2141 // Send the Data Out PDU's one by one.
2143 NET_LIST_FOR_EACH (Entry
, DataOutPduList
) {
2144 Pdu
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
2146 Status
= Tcp4IoTransmit (&Tcb
->Conn
->Tcp4Io
, Pdu
);
2147 if (EFI_ERROR (Status
)) {
2152 IScsiFreeNbufList (DataOutPduList
);
2158 Process the received iSCSI SCSI Data In PDU.
2160 @param[in] Pdu The Data In PDU received.
2161 @param[in] Tcb The task control block.
2162 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2164 @retval EFI_SUCCES The check on the Data IN PDU is passed and some update
2166 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2167 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2168 @retval Others Other errors as indicated.
2174 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2177 ISCSI_SCSI_DATA_IN
*DataInHdr
;
2180 DataInHdr
= (ISCSI_SCSI_DATA_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2182 DataInHdr
->InitiatorTaskTag
= NTOHL (DataInHdr
->InitiatorTaskTag
);
2183 DataInHdr
->ExpCmdSN
= NTOHL (DataInHdr
->ExpCmdSN
);
2184 DataInHdr
->MaxCmdSN
= NTOHL (DataInHdr
->MaxCmdSN
);
2185 DataInHdr
->DataSN
= NTOHL (DataInHdr
->DataSN
);
2188 // Check the DataSN.
2190 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, DataInHdr
->DataSN
);
2191 if (EFI_ERROR (Status
)) {
2195 if (DataInHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2196 return EFI_PROTOCOL_ERROR
;
2199 // Update the command related sequence numbers.
2201 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, DataInHdr
->MaxCmdSN
, DataInHdr
->ExpCmdSN
);
2203 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_STATUS_VALID
)) {
2204 if (!ISCSI_FLAG_ON (DataInHdr
, ISCSI_BHS_FLAG_FINAL
)) {
2206 // The S bit is on but the F bit is off.
2208 return EFI_PROTOCOL_ERROR
;
2211 Tcb
->StatusXferd
= TRUE
;
2213 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_OVERFLOW
| SCSI_DATA_IN_PDU_FLAG_UNDERFLOW
)) {
2215 // Underflow and Overflow are mutual flags.
2217 return EFI_PROTOCOL_ERROR
;
2220 // S bit is on, the StatSN is valid.
2222 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NTOHL (DataInHdr
->StatSN
));
2223 if (EFI_ERROR (Status
)) {
2227 Packet
->HostAdapterStatus
= 0;
2228 Packet
->TargetStatus
= DataInHdr
->Status
;
2230 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2231 Packet
->InTransferLength
+= NTOHL (DataInHdr
->ResidualCount
);
2232 Status
= EFI_BAD_BUFFER_SIZE
;
2235 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2236 Packet
->InTransferLength
-= NTOHL (DataInHdr
->ResidualCount
);
2244 Process the received iSCSI R2T PDU.
2246 @param[in] Pdu The R2T PDU received.
2247 @param[in] Tcb The task control block.
2248 @param[in] Lun The Lun.
2249 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2251 @retval EFI_SUCCES The R2T PDU is valid and the solicited data is sent out.
2252 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2253 @retval Others Other errors as indicated.
2260 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2263 ISCSI_READY_TO_TRANSFER
*R2THdr
;
2265 ISCSI_XFER_CONTEXT
*XferContext
;
2268 R2THdr
= (ISCSI_READY_TO_TRANSFER
*) NetbufGetByte (Pdu
, 0, NULL
);
2270 R2THdr
->InitiatorTaskTag
= NTOHL (R2THdr
->InitiatorTaskTag
);
2271 R2THdr
->TargetTransferTag
= NTOHL (R2THdr
->TargetTransferTag
);
2272 R2THdr
->StatSN
= NTOHL (R2THdr
->StatSN
);
2273 R2THdr
->R2TSeqNum
= NTOHL (R2THdr
->R2TSeqNum
);
2274 R2THdr
->BufferOffset
= NTOHL (R2THdr
->BufferOffset
);
2275 R2THdr
->DesiredDataTransferLength
= NTOHL (R2THdr
->DesiredDataTransferLength
);
2277 if ((R2THdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) || !ISCSI_SEQ_EQ (R2THdr
->StatSN
, Tcb
->Conn
->ExpStatSN
)) {
2278 return EFI_PROTOCOL_ERROR
;;
2281 // Check the sequence number.
2283 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, R2THdr
->R2TSeqNum
);
2284 if (EFI_ERROR (Status
)) {
2288 XferContext
= &Tcb
->XferContext
;
2289 XferContext
->TargetTransferTag
= R2THdr
->TargetTransferTag
;
2290 XferContext
->Offset
= R2THdr
->BufferOffset
;
2291 XferContext
->DesiredLength
= R2THdr
->DesiredDataTransferLength
;
2293 if (((XferContext
->Offset
+ XferContext
->DesiredLength
) > Packet
->OutTransferLength
) ||
2294 (XferContext
->DesiredLength
> Tcb
->Conn
->Session
->MaxBurstLength
)
2296 return EFI_PROTOCOL_ERROR
;
2299 // Send the data solicited by this R2T.
2301 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2302 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2308 Process the received iSCSI SCSI Response PDU.
2310 @param[in] Pdu The Response PDU received.
2311 @param[in] Tcb The task control block.
2312 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2314 @retval EFI_SUCCES The Response PDU is processed.
2315 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2316 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2317 @retval Others Other errors as indicated.
2320 IScsiOnScsiRspRcvd (
2323 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2326 SCSI_RESPONSE
*ScsiRspHdr
;
2327 ISCSI_SENSE_DATA
*SenseData
;
2331 ScsiRspHdr
= (SCSI_RESPONSE
*) NetbufGetByte (Pdu
, 0, NULL
);
2333 ScsiRspHdr
->InitiatorTaskTag
= NTOHL (ScsiRspHdr
->InitiatorTaskTag
);
2334 if (ScsiRspHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2335 return EFI_PROTOCOL_ERROR
;
2338 ScsiRspHdr
->StatSN
= NTOHL (ScsiRspHdr
->StatSN
);
2340 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, ScsiRspHdr
->StatSN
);
2341 if (EFI_ERROR (Status
)) {
2345 ScsiRspHdr
->MaxCmdSN
= NTOHL (ScsiRspHdr
->MaxCmdSN
);
2346 ScsiRspHdr
->ExpCmdSN
= NTOHL (ScsiRspHdr
->ExpCmdSN
);
2347 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, ScsiRspHdr
->MaxCmdSN
, ScsiRspHdr
->ExpCmdSN
);
2349 Tcb
->StatusXferd
= TRUE
;
2351 Packet
->HostAdapterStatus
= ScsiRspHdr
->Response
;
2352 if (Packet
->HostAdapterStatus
!= ISCSI_SERVICE_RSP_COMMAND_COMPLETE_AT_TARGET
) {
2356 Packet
->TargetStatus
= ScsiRspHdr
->Status
;
2358 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
| SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
) ||
2359 ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
| SCSI_RSP_PDU_FLAG_UNDERFLOW
)
2361 return EFI_PROTOCOL_ERROR
;
2364 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
)) {
2365 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2366 Status
= EFI_BAD_BUFFER_SIZE
;
2369 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
)) {
2370 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2373 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2374 if (Packet
->DataDirection
== DataIn
) {
2375 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2377 Packet
->OutTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2380 Status
= EFI_BAD_BUFFER_SIZE
;
2383 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2384 if (Packet
->DataDirection
== DataIn
) {
2385 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2387 Packet
->OutTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2391 DataSegLen
= ISCSI_GET_DATASEG_LEN (ScsiRspHdr
);
2392 if (DataSegLen
!= 0) {
2393 SenseData
= (ISCSI_SENSE_DATA
*) NetbufGetByte (Pdu
, sizeof (SCSI_RESPONSE
), NULL
);
2395 SenseData
->Length
= NTOHS (SenseData
->Length
);
2397 Packet
->SenseDataLength
= (UINT8
) MIN (SenseData
->Length
, Packet
->SenseDataLength
);
2398 if (Packet
->SenseDataLength
!= 0) {
2399 CopyMem (Packet
->SenseData
, &SenseData
->Data
[0], Packet
->SenseDataLength
);
2402 Packet
->SenseDataLength
= 0;
2409 Process the received NOP In PDU.
2411 @param[in] Pdu The NOP In PDU received.
2412 @param[in] Tcb The task control block.
2414 @retval EFI_SUCCES The NOP In PDU is processed and the related sequence
2415 numbers are updated.
2416 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2424 ISCSI_NOP_IN
*NopInHdr
;
2427 NopInHdr
= (ISCSI_NOP_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2429 NopInHdr
->StatSN
= NTOHL (NopInHdr
->StatSN
);
2430 NopInHdr
->ExpCmdSN
= NTOHL (NopInHdr
->ExpCmdSN
);
2431 NopInHdr
->MaxCmdSN
= NTOHL (NopInHdr
->MaxCmdSN
);
2433 if (NopInHdr
->InitiatorTaskTag
== ISCSI_RESERVED_TAG
) {
2434 if (NopInHdr
->StatSN
!= Tcb
->Conn
->ExpStatSN
) {
2435 return EFI_PROTOCOL_ERROR
;
2438 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NopInHdr
->StatSN
);
2439 if (EFI_ERROR (Status
)) {
2444 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, NopInHdr
->MaxCmdSN
, NopInHdr
->ExpCmdSN
);
2450 Execute the SCSI command issued through the EXT SCSI PASS THRU protocol.
2452 @param[in] PassThru The EXT SCSI PASS THRU protocol.
2453 @param[in] Target The target ID.
2454 @param[in] Lun The LUN.
2455 @param[in, out] Packet The request packet containing IO request, SCSI command
2456 buffer and buffers to read/write.
2458 @retval EFI_SUCCES The SCSI command is executed and the result is updated to
2460 @retval EFI_DEVICE_ERROR Session state was not as required.
2461 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2462 @retval Others Other errors as indicated.
2465 IScsiExecuteScsiCommand (
2466 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*PassThru
,
2469 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2473 ISCSI_DRIVER_DATA
*Private
;
2474 ISCSI_SESSION
*Session
;
2475 EFI_EVENT TimeoutEvent
;
2476 ISCSI_CONNECTION
*Conn
;
2479 ISCSI_XFER_CONTEXT
*XferContext
;
2481 ISCSI_IN_BUFFER_CONTEXT InBufferContext
;
2485 Private
= ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru
);
2486 Session
= &Private
->Session
;
2487 Status
= EFI_SUCCESS
;
2489 TimeoutEvent
= NULL
;
2492 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
2493 return EFI_DEVICE_ERROR
;
2496 Conn
= NET_LIST_USER_STRUCT_S (
2497 Session
->Conns
.ForwardLink
,
2500 ISCSI_CONNECTION_SIGNATURE
2503 if (Packet
->Timeout
!= 0) {
2504 Timeout
= MultU64x32 (Packet
->Timeout
, 2);
2507 Status
= IScsiNewTcb (Conn
, &Tcb
);
2508 if (EFI_ERROR (Status
)) {
2512 // Encapsulate the SCSI request packet into an iSCSI SCSI Command PDU.
2514 Pdu
= IScsiNewScsiCmdPdu (Packet
, Lun
, Tcb
);
2516 Status
= EFI_OUT_OF_RESOURCES
;
2520 XferContext
= &Tcb
->XferContext
;
2521 Buffer
= NetbufGetByte (Pdu
, 0, NULL
);
2522 XferContext
->Offset
= ISCSI_GET_DATASEG_LEN (Buffer
);
2525 // Transmit the SCSI Command PDU.
2527 Status
= Tcp4IoTransmit (&Conn
->Tcp4Io
, Pdu
);
2531 if (EFI_ERROR (Status
)) {
2535 if (!Session
->InitialR2T
&&
2536 (XferContext
->Offset
< Session
->FirstBurstLength
) &&
2537 (XferContext
->Offset
< Packet
->OutTransferLength
)
2540 // Unsolicited Data-Out sequence is allowed, there is remaining SCSI
2541 // OUT data and the limit of FirstBurstLength is not reached.
2543 XferContext
->TargetTransferTag
= ISCSI_RESERVED_TAG
;
2544 XferContext
->DesiredLength
= MIN (
2545 Session
->FirstBurstLength
,
2546 Packet
->OutTransferLength
- XferContext
->Offset
2549 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2550 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2551 if (EFI_ERROR (Status
)) {
2556 InBufferContext
.InData
= (UINT8
*) Packet
->InDataBuffer
;
2557 InBufferContext
.InDataLen
= Packet
->InTransferLength
;
2559 while (!Tcb
->StatusXferd
) {
2561 // Start the timeout timer.
2564 Status
= gBS
->SetTimer (Conn
->TimeoutEvent
, TimerRelative
, Timeout
);
2565 if (EFI_ERROR (Status
)) {
2568 TimeoutEvent
= Conn
->TimeoutEvent
;
2571 // try to receive PDU from target.
2573 Status
= IScsiReceivePdu (Conn
, &Pdu
, &InBufferContext
, FALSE
, FALSE
, TimeoutEvent
);
2574 if (EFI_ERROR (Status
)) {
2578 switch (ISCSI_GET_OPCODE (NetbufGetByte (Pdu
, 0, NULL
))) {
2579 case ISCSI_OPCODE_SCSI_DATA_IN
:
2580 Status
= IScsiOnDataInRcvd (Pdu
, Tcb
, Packet
);
2583 case ISCSI_OPCODE_R2T
:
2584 Status
= IScsiOnR2TRcvd (Pdu
, Tcb
, Lun
, Packet
);
2587 case ISCSI_OPCODE_SCSI_RSP
:
2588 Status
= IScsiOnScsiRspRcvd (Pdu
, Tcb
, Packet
);
2591 case ISCSI_OPCODE_NOP_IN
:
2592 Status
= IScsiOnNopInRcvd (Pdu
, Tcb
);
2595 case ISCSI_OPCODE_VENDOR_T0
:
2596 case ISCSI_OPCODE_VENDOR_T1
:
2597 case ISCSI_OPCODE_VENDOR_T2
:
2599 // These messages are vendor specific, skip them.
2604 Status
= EFI_PROTOCOL_ERROR
;
2610 if (EFI_ERROR (Status
)) {
2617 if (TimeoutEvent
!= NULL
) {
2618 gBS
->SetTimer (TimeoutEvent
, TimerCancel
, 0);
2625 if ((Status
!= EFI_SUCCESS
) && (Status
!= EFI_NOT_READY
)) {
2627 // Reinstate the session.
2629 if (EFI_ERROR (IScsiSessionReinstatement (Private
))) {
2630 Status
= EFI_DEVICE_ERROR
;
2638 Reinstate the session on some error.
2640 @param[in, out] Private The iSCSI driver data.
2642 @retval EFI_SUCCES The session is reinstated from some error.
2643 @retval Other Reinstatement failed.
2646 IScsiSessionReinstatement (
2647 IN OUT ISCSI_DRIVER_DATA
*Private
2650 ISCSI_SESSION
*Session
;
2653 Session
= &Private
->Session
;
2654 ASSERT (Session
->State
== SESSION_STATE_LOGGED_IN
);
2657 // Abort the session and re-init it.
2659 IScsiSessionAbort (Session
);
2660 IScsiSessionInit (Session
, TRUE
);
2665 Status
= IScsiSessionLogin (Private
);
2671 Initialize some session parameters before login.
2673 @param[in, out] Session The iSCSI session.
2674 @param[in] Recovery Whether the request is from a fresh new start or recovery.
2678 IN OUT ISCSI_SESSION
*Session
,
2685 Session
->Signature
= ISCSI_SESSION_SIGNATURE
;
2686 Session
->State
= SESSION_STATE_FREE
;
2688 Random
= NET_RANDOM (NetRandomInitSeed ());
2690 Session
->Isid
[0] = ISID_BYTE_0
;
2691 Session
->Isid
[1] = ISID_BYTE_1
;
2692 Session
->Isid
[2] = ISID_BYTE_2
;
2693 Session
->Isid
[3] = ISID_BYTE_3
;
2694 Session
->Isid
[4] = (UINT8
) Random
;
2695 Session
->Isid
[5] = (UINT8
) (Random
>> 8);
2697 InitializeListHead (&Session
->Conns
);
2698 InitializeListHead (&Session
->TcbList
);
2704 Session
->InitiatorTaskTag
= 1;
2705 Session
->NextCid
= 1;
2707 Session
->TargetPortalGroupTag
= 0;
2708 Session
->MaxConnections
= ISCSI_MAX_CONNS_PER_SESSION
;
2709 Session
->InitialR2T
= FALSE
;
2710 Session
->ImmediateData
= TRUE
;
2711 Session
->MaxBurstLength
= 262144;
2712 Session
->FirstBurstLength
= MAX_RECV_DATA_SEG_LEN_IN_FFP
;
2713 Session
->DefaultTime2Wait
= 2;
2714 Session
->DefaultTime2Retain
= 20;
2715 Session
->MaxOutstandingR2T
= DEFAULT_MAX_OUTSTANDING_R2T
;
2716 Session
->DataPDUInOrder
= TRUE
;
2717 Session
->DataSequenceInOrder
= TRUE
;
2718 Session
->ErrorRecoveryLevel
= 0;
2722 Abort the iSCSI session, that is, reset all the connection and free the
2725 @param[in, out] Session The iSCSI session.
2727 @retval EFI_SUCCES The session is aborted.
2731 IN OUT ISCSI_SESSION
*Session
2734 ISCSI_DRIVER_DATA
*Private
;
2735 ISCSI_CONNECTION
*Conn
;
2737 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
2741 ASSERT (!IsListEmpty (&Session
->Conns
));
2743 Private
= ISCSI_DRIVER_DATA_FROM_SESSION (Session
);
2745 while (!IsListEmpty (&Session
->Conns
)) {
2746 Conn
= NET_LIST_USER_STRUCT_S (
2747 Session
->Conns
.ForwardLink
,
2750 ISCSI_CONNECTION_SIGNATURE
2753 gBS
->CloseProtocol (
2754 Conn
->Tcp4Io
.Handle
,
2755 &gEfiTcp4ProtocolGuid
,
2757 Private
->ExtScsiPassThruHandle
2760 IScsiConnReset (Conn
);
2762 IScsiDetatchConnection (Conn
);
2763 IScsiDestroyConnection (Conn
);
2766 Session
->State
= SESSION_STATE_FAILED
;