2 The implementation of IScsi protocol based on RFC3720.
4 Copyright (c) 2004 - 2008, 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
)) {
218 gBS
->FreePool (Conn
);
222 NetbufQueInit (&Conn
->RspQue
);
225 // set the default connection-only parameters
227 Conn
->MaxRecvDataSegmentLength
= MAX_RECV_DATA_SEG_LEN_IN_FFP
;
228 Conn
->HeaderDigest
= ISCSI_DIGEST_NONE
;
229 Conn
->DataDigest
= ISCSI_DIGEST_NONE
;
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
);
249 gBS
->FreePool (Conn
);
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
);
269 gBS
->FreePool (Conn
);
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 Others Other errors as indicated.
283 IN ISCSI_DRIVER_DATA
*Private
287 ISCSI_SESSION
*Session
;
288 ISCSI_CONNECTION
*Conn
;
289 EFI_TCP4_PROTOCOL
*Tcp4
;
291 Session
= &Private
->Session
;
294 // Create a connection for the session.
296 Conn
= IScsiCreateConnection (Private
, Session
);
298 return EFI_OUT_OF_RESOURCES
;
301 IScsiAttatchConnection (Session
, Conn
);
304 // Login througth the newly created connection.
306 Status
= IScsiConnLogin (Conn
);
307 if (EFI_ERROR (Status
)) {
308 IScsiConnReset (Conn
);
309 IScsiDetatchConnection (Conn
);
310 IScsiDestroyConnection (Conn
);
312 Session
->State
= SESSION_STATE_LOGGED_IN
;
316 &gEfiTcp4ProtocolGuid
,
319 Private
->ExtScsiPassThruHandle
,
320 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
328 Build and send the iSCSI login request to the iSCSI target according to
329 the current login stage.
331 @param[in] Conn The connection in the iSCSI login phase.
333 @retval EFI_SUCCESS The iSCSI login request PDU is built and sent on this
335 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
336 @retval EFI_DEVICE_ERROR Some kind of device error happened.
340 IN ISCSI_CONNECTION
*Conn
347 // build the Login Request PDU
349 Pdu
= IScsiPrepareLoginReq (Conn
);
351 return EFI_DEVICE_ERROR
;
354 // Send it to the iSCSI target.
356 Status
= Tcp4IoTransmit (&Conn
->Tcp4Io
, Pdu
);
364 Receive and process the iSCSI login response.
366 @param[in] Conn The connection in the iSCSI login phase.
368 @retval EFI_SUCCESS The iSCSI login response PDU is received and processed.
369 @retval Others Other errors as indicated.
372 IScsiReceiveLoginRsp (
373 IN ISCSI_CONNECTION
*Conn
380 // Receive the iSCSI login response.
382 Status
= IScsiReceivePdu (Conn
, &Pdu
, NULL
, FALSE
, FALSE
, NULL
);
383 if (EFI_ERROR (Status
)) {
387 // A Login Response is received, process it.
389 Status
= IScsiProcessLoginRsp (Conn
, Pdu
);
397 Add an iSCSI key-value pair as a string into the data segment of the Login Request PDU.
398 The DataSegmentLength and the actual size of the net buffer containing this PDU will be
401 @param[in, out] Pdu The iSCSI PDU whose data segment the key-value pair will
403 @param[in] Key The key name string.
404 @param[in] Value The value string.
406 @retval EFI_SUCCESS The key-valu pair is added to the PDU's datasegment and
407 the correspondence length fields are updated.
408 @retval EFI_OUT_OF_RESOURCES There is not enough space in the PDU to add the key-value
412 IScsiAddKeyValuePair (
422 ISCSI_LOGIN_REQUEST
*LoginReq
;
425 LoginReq
= (ISCSI_LOGIN_REQUEST
*) NetbufGetByte (Pdu
, 0, NULL
);
426 DataSegLen
= NTOH24 (LoginReq
->DataSegmentLength
);
428 KeyLen
= (UINT32
) AsciiStrLen (Key
);
429 ValueLen
= (UINT32
) AsciiStrLen (Value
);
432 // 1 byte for the key value separator '=' and 1 byte for the null
433 // delimiter after the value.
435 TotalLen
= KeyLen
+ 1 + ValueLen
+ 1;
438 // Allocate the space for the key-value pair.
440 Data
= (CHAR8
*)NetbufAllocSpace (Pdu
, TotalLen
, NET_BUF_TAIL
);
442 return EFI_OUT_OF_RESOURCES
;
447 CopyMem (Data
, Key
, KeyLen
);
456 CopyMem (Data
, Value
, ValueLen
);
462 // update the DataSegmentLength
464 ISCSI_SET_DATASEG_LEN (LoginReq
, DataSegLen
+ TotalLen
);
470 Prepare the iSCSI login request to be sent according to the current login status.
472 @param[in, out] Conn The connection in the iSCSI login phase.
474 @return The pointer to the net buffer containing the iSCSI login request built.
475 @retval Others Other errors as indicated.
478 IScsiPrepareLoginReq (
479 IN OUT ISCSI_CONNECTION
*Conn
482 ISCSI_SESSION
*Session
;
484 ISCSI_LOGIN_REQUEST
*LoginReq
;
487 Session
= Conn
->Session
;
489 Nbuf
= NetbufAlloc (sizeof (ISCSI_LOGIN_REQUEST
) + DEFAULT_MAX_RECV_DATA_SEG_LEN
);
494 LoginReq
= (ISCSI_LOGIN_REQUEST
*) NetbufAllocSpace (Nbuf
, sizeof (ISCSI_LOGIN_REQUEST
), NET_BUF_TAIL
);
495 ZeroMem (LoginReq
, sizeof (ISCSI_LOGIN_REQUEST
));
498 // Init the login request pdu
500 ISCSI_SET_OPCODE (LoginReq
, ISCSI_OPCODE_LOGIN_REQ
, ISCSI_REQ_IMMEDIATE
);
501 ISCSI_SET_STAGES (LoginReq
, Conn
->CurrentStage
, Conn
->NextStage
);
502 LoginReq
->VersionMax
= ISCSI_VERSION_MAX
;
503 LoginReq
->VersionMin
= ISCSI_VERSION_MIN
;
504 LoginReq
->TSIH
= HTONS (Session
->TSIH
);
505 LoginReq
->InitiatorTaskTag
= HTONL (Session
->InitiatorTaskTag
);
506 LoginReq
->CID
= HTONS (Conn
->CID
);
507 LoginReq
->CmdSN
= HTONL (Session
->CmdSN
);
510 // For the first Login Request on a coonection this is ExpStatSN for the
511 // old connection and this field is only valid if the Login Request restarts
513 // For subsequent Login Requests it is used to acknowledge the Login Responses
514 // with their increasing StatSN values.
516 LoginReq
->ExpStatSN
= HTONL (Conn
->ExpStatSN
);
517 CopyMem (LoginReq
->ISID
, Session
->ISID
, sizeof (LoginReq
->ISID
));
519 if (Conn
->PartialRspRcvd
) {
521 // A partial response, initiator must send an empty Login Request.
526 switch (Conn
->CurrentStage
) {
527 case ISCSI_SECURITY_NEGOTIATION
:
528 Status
= IScsiCHAPToSendReq (Conn
, Nbuf
);
531 case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
:
532 Status
= IScsiFillOpParams (Conn
, Nbuf
);
533 ISCSI_SET_FLAG (LoginReq
, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT
);
538 // something error happens...
540 Status
= EFI_DEVICE_ERROR
;
544 if (EFI_ERROR (Status
)) {
549 // Pad the data segment if needed.
551 IScsiPadSegment (Nbuf
, ISCSI_GET_DATASEG_LEN (LoginReq
));
553 // Check whether we will issue the stage transition signal?
555 Conn
->TransitInitiated
= (BOOLEAN
) ISCSI_FLAG_ON (LoginReq
, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT
);
562 Process the iSCSI Login Response.
564 @param[in, out] Conn The connection on which the iSCSI login response is received.
565 @param[in, out] Pdu The iSCSI login response PDU.
567 @retval EFI_SUCCESS The iSCSI login response PDU is processed and all check are passed.
568 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
569 @retval EFI_MEDIA_CHANGED Target is redirected.
570 @retval Others Other errors as indicated.
573 IScsiProcessLoginRsp (
574 IN OUT ISCSI_CONNECTION
*Conn
,
579 ISCSI_SESSION
*Session
;
580 ISCSI_LOGIN_RESPONSE
*LoginRsp
;
588 Session
= Conn
->Session
;
590 LoginRsp
= (ISCSI_LOGIN_RESPONSE
*) NetbufGetByte (Pdu
, 0, NULL
);
591 if (!ISCSI_CHECK_OPCODE (LoginRsp
, ISCSI_OPCODE_LOGIN_RSP
)) {
593 // It's not a Login Response
595 return EFI_PROTOCOL_ERROR
;
598 // Get the data segment if any.
600 DataSegLen
= ISCSI_GET_DATASEG_LEN (LoginRsp
);
601 if (DataSegLen
!= 0) {
602 DataSeg
= NetbufGetByte (Pdu
, sizeof (ISCSI_LOGIN_RESPONSE
), NULL
);
607 // Check the status class in the login response PDU.
609 switch (LoginRsp
->StatusClass
) {
610 case ISCSI_LOGIN_STATUS_SUCCESS
:
612 // Just break here, the response and the data segment will be processed later.
616 case ISCSI_LOGIN_STATUS_REDIRECTION
:
618 // The target may be moved to a different address
620 if (DataSeg
== NULL
) {
621 return EFI_PROTOCOL_ERROR
;
624 // Process the TargetAddress key-value strings in the data segment to update the
625 // target address info.
627 Status
= IScsiUpdateTargetAddress (Session
, (CHAR8
*)DataSeg
, DataSegLen
);
628 if (EFI_ERROR (Status
)) {
632 // Session will be restarted on this error status because the Target is
633 // redirected by this Login Response.
635 return EFI_MEDIA_CHANGED
;
639 // Initiator Error, Target Error, or any other undefined error code.
641 return EFI_PROTOCOL_ERROR
;
644 // The status is sucess, extract the wanted fields from the header segment.
646 Transit
= (BOOLEAN
) ISCSI_FLAG_ON (LoginRsp
, ISCSI_LOGIN_RSP_PDU_FLAG_TRANSIT
);
647 Continue
= (BOOLEAN
) ISCSI_FLAG_ON (LoginRsp
, ISCSI_LOGIN_RSP_PDU_FLAG_CONTINUE
);
649 CurrentStage
= (UINT8
) ISCSI_GET_CURRENT_STAGE (LoginRsp
);
650 NextStage
= (UINT8
) ISCSI_GET_NEXT_STAGE (LoginRsp
);
652 LoginRsp
->InitiatorTaskTag
= NTOHL (LoginRsp
->InitiatorTaskTag
);
654 if ((Transit
&& Continue
) ||
655 (CurrentStage
!= Conn
->CurrentStage
) ||
656 (!Conn
->TransitInitiated
&& Transit
) ||
657 (Transit
&& (NextStage
!= Conn
->NextStage
)) ||
658 (CompareMem (Session
->ISID
, LoginRsp
->ISID
, sizeof (LoginRsp
->ISID
)) != 0) ||
659 (LoginRsp
->InitiatorTaskTag
!= Session
->InitiatorTaskTag
)
662 // A Login Response with the C bit set to 1 MUST have the T bit set to 0;
663 // The CSG in the Login Response MUST be the same with the I-end of this connection;
664 // The T bit can't be 1 if the last Login Response sent by the initiator doesn't
665 // initiate the transistion;
666 // The NSG MUST be the same with the I-end of this connection if Transit is required.
667 // The ISID in the Login Response MUST be the same with this session.
669 return EFI_PROTOCOL_ERROR
;
672 LoginRsp
->StatSN
= NTOHL (LoginRsp
->StatSN
);
673 LoginRsp
->ExpCmdSN
= NTOHL (LoginRsp
->ExpCmdSN
);
674 LoginRsp
->MaxCmdSN
= NTOHL (LoginRsp
->MaxCmdSN
);
676 if ((Conn
->CurrentStage
== ISCSI_SECURITY_NEGOTIATION
) && (Conn
->CHAPStep
== ISCSI_CHAP_INITIAL
)) {
678 // It's the initial Login Response, initialize the local ExpStatSN, MaxCmdSN
681 Conn
->ExpStatSN
= LoginRsp
->StatSN
+ 1;
682 Session
->MaxCmdSN
= LoginRsp
->MaxCmdSN
;
683 Session
->ExpCmdSN
= LoginRsp
->ExpCmdSN
;
686 // Check the StatSN of this PDU
688 Status
= IScsiCheckSN (&Conn
->ExpStatSN
, LoginRsp
->StatSN
);
689 if (!EFI_ERROR (Status
)) {
691 // Update the MaxCmdSN and ExpCmdSN
693 IScsiUpdateCmdSN (Session
, LoginRsp
->MaxCmdSN
, LoginRsp
->ExpCmdSN
);
699 // Trim off the header segment.
701 NetbufTrim (Pdu
, sizeof (ISCSI_LOGIN_RESPONSE
), NET_BUF_HEAD
);
704 // Queue this login response first in case it's a partial response so that
705 // later when the full response list is received we can combine these scattered
706 // responses' data segment and then process it.
709 NetbufQueAppend (&Conn
->RspQue
, Pdu
);
711 Conn
->PartialRspRcvd
= Continue
;
714 // It's a partial response, have to wait for another or more Request/Response
715 // conversations to get the full response.
720 switch (CurrentStage
) {
721 case ISCSI_SECURITY_NEGOTIATION
:
723 // In security negotiation stage, let CHAP module handle it.
725 Status
= IScsiCHAPOnRspReceived (Conn
);
728 case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
:
730 // Response received with negotiation resonse on iSCSI parameters, check them.
732 Status
= IScsiCheckOpParams (Conn
);
737 // Should never get here.
739 Status
= EFI_PROTOCOL_ERROR
;
743 if (Transit
&& (Status
== EFI_SUCCESS
)) {
745 // Do the state transition.
747 Conn
->CurrentStage
= Conn
->NextStage
;
749 if (Conn
->CurrentStage
== ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
) {
750 Conn
->NextStage
= ISCSI_FULL_FEATURE_PHASE
;
753 // CurrentStage is iSCSI Full Feature, it's the Login-Final Response,
754 // get the TSIH from the Login Response.
756 Session
->TSIH
= NTOHS (LoginRsp
->TSIH
);
760 // Flush the response(s) received.
762 NetbufQueFlush (&Conn
->RspQue
);
768 Updated the target information according the data received in the iSCSI
769 login response with an target redirection status.
771 @param[in, out] Session The iSCSI session.
772 @param[in] Data The data segment which should contain the
773 TargetAddress key-value list.
774 @param[in] Len Length of the data.
776 @retval EFI_SUCCESS The target address is updated.
777 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
778 @retval EFI_NOT_FOUND The TargetAddress key is not found.
779 @retval Others Other errors as indicated.
782 IScsiUpdateTargetAddress (
783 IN OUT ISCSI_SESSION
*Session
,
788 LIST_ENTRY
*KeyValueList
;
789 CHAR8
*TargetAddress
;
794 KeyValueList
= IScsiBuildKeyValueList (Data
, Len
);
795 if (KeyValueList
== NULL
) {
796 return EFI_OUT_OF_RESOURCES
;
799 Status
= EFI_NOT_FOUND
;
802 TargetAddress
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_ADDRESS
);
803 if (TargetAddress
== NULL
) {
807 if (!NET_IS_DIGIT (TargetAddress
[0])) {
809 // The domainname of the target may be presented in three formats: a DNS host name,
810 // a dotted-decimal IPv4 address, or a bracketed IPv6 address. Only accept dotted
816 IpStr
= TargetAddress
;
818 while ((*TargetAddress
!= 0) && (*TargetAddress
!= ':') && (*TargetAddress
!= ',')) {
820 // NULL, ':' or ',' ends the IPv4 string.
825 if (*TargetAddress
== ',') {
827 // Comma and the portal group tag MUST be ommitted if the TargetAddress is sent
828 // as the result of a redirection.
831 } else if (*TargetAddress
== ':') {
832 *TargetAddress
= '\0';
836 Number
= AsciiStrDecimalToUintn (TargetAddress
);
837 if (Number
> 0xFFFF) {
840 Session
->ConfigData
.NvData
.TargetPort
= (UINT16
) Number
;
844 // The string only contains the IPv4 address. Use the well known port.
846 Session
->ConfigData
.NvData
.TargetPort
= ISCSI_WELL_KNOWN_PORT
;
849 // Update the target IP address.
851 Status
= IScsiAsciiStrToIp (IpStr
, &Session
->ConfigData
.NvData
.TargetIp
);
852 if (EFI_ERROR (Status
)) {
859 IScsiFreeKeyValueList (KeyValueList
);
865 The callback function to free the net buffer list.
867 @param[in] Arg The opaque parameter.
874 ASSERT (Arg
!= NULL
);
876 NetbufFreeList ((LIST_ENTRY
*) Arg
);
881 The callback function called in NetBufFree, it does nothing.
883 @param[in] Arg The opaque parameter.
893 Receive an iSCSI response PDU. An iSCSI response PDU contains an iSCSI PDU header and
894 an optional data segment. The two parts will be put into two blocks of buffers in the
895 net buffer. The digest check will be conducted in this function if needed and the digests
896 will be trimmed from the PDU buffer.
898 @param[in] Conn The iSCSI connection to receive data from.
899 @param[out] Pdu The received iSCSI pdu.
900 @param[in] Context The context used to describe information on the caller provided
901 buffer to receive data segment of the iSCSI pdu, it's optional.
902 @param[in] HeaderDigest Whether there will be header digest received.
903 @param[in] DataDigest Whether there will be data digest.
904 @param[in] TimeoutEvent The timeout event, it's optional.
906 @retval EFI_SUCCESS An iSCSI pdu is received.
907 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
908 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
909 @retval Others Other errors as indicated.
913 IN ISCSI_CONNECTION
*Conn
,
915 IN ISCSI_IN_BUFFER_CONTEXT
*Context
, OPTIONAL
916 IN BOOLEAN HeaderDigest
,
917 IN BOOLEAN DataDigest
,
918 IN EFI_EVENT TimeoutEvent OPTIONAL
921 LIST_ENTRY
*NbufList
;
928 NET_FRAGMENT Fragment
[2];
929 UINT32 FragmentCount
;
931 UINT32 PadAndCRC32
[2];
933 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
934 if (NbufList
== NULL
) {
935 return EFI_OUT_OF_RESOURCES
;
938 InitializeListHead (NbufList
);
941 // The header digest will be received together with the PDU header if exists.
943 Len
= sizeof (ISCSI_BASIC_HEADER
) + (HeaderDigest
? sizeof (UINT32
) : 0);
944 PduHdr
= NetbufAlloc (Len
);
945 if (PduHdr
== NULL
) {
946 Status
= EFI_OUT_OF_RESOURCES
;
950 Header
= NetbufAllocSpace (PduHdr
, Len
, NET_BUF_TAIL
);
951 InsertTailList (NbufList
, &PduHdr
->List
);
954 // First step, receive the BHS of the PDU.
956 Status
= Tcp4IoReceive (&Conn
->Tcp4Io
, PduHdr
, FALSE
, TimeoutEvent
);
957 if (EFI_ERROR (Status
)) {
963 // TODO: check the header-digest.
966 // Trim off the digest.
968 NetbufTrim (PduHdr
, sizeof (UINT32
), NET_BUF_TAIL
);
971 Len
= ISCSI_GET_DATASEG_LEN (Header
);
979 // Get the length of the padding bytes of the data segment.
981 PadLen
= ISCSI_GET_PAD_LEN (Len
);
983 switch (ISCSI_GET_OPCODE (Header
)) {
984 case ISCSI_OPCODE_SCSI_DATA_IN
:
986 // Try to use the buffer described by Context if the PDU is an
987 // iSCSI SCSI data in pdu so as to reduce memory copy overhead.
989 InDataOffset
= ISCSI_GET_BUFFER_OFFSET (Header
);
990 if ((Context
== NULL
) || ((InDataOffset
+ Len
) > Context
->InDataLen
)) {
991 Status
= EFI_PROTOCOL_ERROR
;
995 Fragment
[0].Len
= Len
;
996 Fragment
[0].Bulk
= Context
->InData
+ InDataOffset
;
998 if (DataDigest
|| (PadLen
!= 0)) {
1000 // The data segment is padded, use two fragments to receive it.
1001 // The first to receive the useful data. The second to receive the padding.
1003 Fragment
[1].Len
= PadLen
+ (DataDigest
? sizeof (UINT32
) : 0);
1004 Fragment
[1].Bulk
= (UINT8
*) ((UINTN
) &PadAndCRC32
[1] - PadLen
);
1011 DataSeg
= NetbufFromExt (&Fragment
[0], FragmentCount
, 0, 0, IScsiNbufExtFree
, NULL
);
1012 if (DataSeg
== NULL
) {
1013 Status
= EFI_OUT_OF_RESOURCES
;
1019 case ISCSI_OPCODE_SCSI_RSP
:
1020 case ISCSI_OPCODE_NOP_IN
:
1021 case ISCSI_OPCODE_LOGIN_RSP
:
1022 case ISCSI_OPCODE_TEXT_RSP
:
1023 case ISCSI_OPCODE_ASYNC_MSG
:
1024 case ISCSI_OPCODE_REJECT
:
1025 case ISCSI_OPCODE_VENDOR_T0
:
1026 case ISCSI_OPCODE_VENDOR_T1
:
1027 case ISCSI_OPCODE_VENDOR_T2
:
1029 // Allocate buffer to receive the data segment.
1031 Len
+= PadLen
+ (DataDigest
? sizeof (UINT32
) : 0);
1032 DataSeg
= NetbufAlloc (Len
);
1033 if (DataSeg
== NULL
) {
1034 Status
= EFI_OUT_OF_RESOURCES
;
1038 NetbufAllocSpace (DataSeg
, Len
, NET_BUF_TAIL
);
1042 Status
= EFI_PROTOCOL_ERROR
;
1046 InsertTailList (NbufList
, &DataSeg
->List
);
1049 // Receive the data segment with the data digest if any.
1051 Status
= Tcp4IoReceive (&Conn
->Tcp4Io
, DataSeg
, FALSE
, TimeoutEvent
);
1052 if (EFI_ERROR (Status
)) {
1058 // TODO: Check the data digest.
1060 NetbufTrim (DataSeg
, sizeof (UINT32
), NET_BUF_TAIL
);
1065 // Trim off the padding bytes in the data segment.
1067 NetbufTrim (DataSeg
, PadLen
, NET_BUF_TAIL
);
1072 // Form the pdu from a list of pdu segments.
1074 *Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1076 Status
= EFI_OUT_OF_RESOURCES
;
1081 if (EFI_ERROR (Status
)) {
1083 // Free the Nbufs in this NbufList and the NbufList itself.
1085 IScsiFreeNbufList (NbufList
);
1092 Check and get the result of the prameter negotiation.
1094 @param[in, out] Conn The connection in iSCSI login.
1096 @retval EFI_SUCCESS The parmeter check is passed and negotiation is finished.
1097 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
1098 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1101 IScsiCheckOpParams (
1102 IN OUT ISCSI_CONNECTION
*Conn
1106 LIST_ENTRY
*KeyValueList
;
1109 ISCSI_SESSION
*Session
;
1113 ASSERT (Conn
->RspQue
.BufNum
!= 0);
1115 Session
= Conn
->Session
;
1117 Len
= Conn
->RspQue
.BufSize
;
1118 Data
= AllocatePool (Len
);
1120 return EFI_OUT_OF_RESOURCES
;
1123 NetbufQueCopy (&Conn
->RspQue
, 0, Len
, (UINT8
*) Data
);
1125 Status
= EFI_PROTOCOL_ERROR
;
1128 // Extract the Key-Value pairs into a list.
1130 KeyValueList
= IScsiBuildKeyValueList (Data
, Len
);
1131 if (KeyValueList
== NULL
) {
1132 gBS
->FreePool (Data
);
1138 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_HEADER_DIGEST
);
1139 if (Value
== NULL
) {
1143 if (AsciiStrCmp (Value
, "CRC32") == 0) {
1144 if (Conn
->HeaderDigest
!= ISCSI_DIGEST_CRC32
) {
1147 } else if (AsciiStrCmp (Value
, ISCSI_KEY_VALUE_NONE
) == 0) {
1148 Conn
->HeaderDigest
= ISCSI_DIGEST_NONE
;
1155 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_DIGEST
);
1156 if (Value
== NULL
) {
1160 if (AsciiStrCmp (Value
, "CRC32") == 0) {
1161 if (Conn
->DataDigest
!= ISCSI_DIGEST_CRC32
) {
1164 } else if (AsciiStrCmp (Value
, ISCSI_KEY_VALUE_NONE
) == 0) {
1165 Conn
->DataDigest
= ISCSI_DIGEST_NONE
;
1170 // ErrorRecoveryLevel, result fuction is Minimum.
1172 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_ERROR_RECOVERY_LEVEL
);
1173 if (Value
== NULL
) {
1177 NumericValue
= AsciiStrDecimalToUintn (Value
);
1178 if (NumericValue
> 2) {
1182 Session
->ErrorRecoveryLevel
= (UINT8
) MIN (Session
->ErrorRecoveryLevel
, NumericValue
);
1185 // InitialR2T, result function is OR.
1187 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_INITIAL_R2T
);
1188 if (Value
== NULL
) {
1192 Session
->InitialR2T
= (BOOLEAN
) (Session
->InitialR2T
|| (AsciiStrCmp (Value
, "Yes") == 0));
1195 // ImmediateData, result function is AND.
1197 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_IMMEDIATE_DATA
);
1198 if (Value
== NULL
) {
1202 Session
->ImmediateData
= (BOOLEAN
) (Session
->ImmediateData
&& (AsciiStrCmp (Value
, "Yes") == 0));
1205 // MaxRecvDataSegmentLength, result function is Mininum.
1207 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
);
1208 if (Value
!= NULL
) {
1210 // MaxRecvDataSegmentLength is declarative.
1212 NumericValue
= AsciiStrDecimalToUintn (Value
);
1214 Conn
->MaxRecvDataSegmentLength
= (UINT32
) MIN (Conn
->MaxRecvDataSegmentLength
, NumericValue
);
1217 // MaxBurstLength, result funtion is Mininum.
1219 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_BURST_LENGTH
);
1220 if (Value
== NULL
) {
1224 NumericValue
= AsciiStrDecimalToUintn (Value
);
1225 Session
->MaxBurstLength
= (UINT32
) MIN (Session
->MaxBurstLength
, NumericValue
);
1228 // FirstBurstLength, result function is Minimum. Irrelevant when InitialR2T=Yes and
1229 // ImmediateData=No.
1231 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_FIRST_BURST_LENGTH
);
1232 if ((Value
== NULL
) && !(Session
->InitialR2T
&& !Session
->ImmediateData
)) {
1236 NumericValue
= AsciiStrDecimalToUintn (Value
);
1237 Session
->FirstBurstLength
= (UINT32
) MIN (Session
->FirstBurstLength
, NumericValue
);
1240 // MaxConnections, result function is Minimum.
1242 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_CONNECTIONS
);
1243 if (Value
== NULL
) {
1247 NumericValue
= AsciiStrDecimalToUintn (Value
);
1248 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1252 Session
->MaxConnections
= (UINT32
) MIN (Session
->MaxConnections
, NumericValue
);
1255 // DataPDUInOrder, result function is OR.
1257 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_PDU_IN_ORDER
);
1258 if (Value
== NULL
) {
1262 Session
->DataPDUInOrder
= (BOOLEAN
) (Session
->DataPDUInOrder
|| (AsciiStrCmp (Value
, "Yes") == 0));
1265 // DataSequenceInorder, result function is OR.
1267 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
);
1268 if (Value
== NULL
) {
1272 Session
->DataSequenceInOrder
= (BOOLEAN
) (Session
->DataSequenceInOrder
|| (AsciiStrCmp (Value
, "Yes") == 0));
1275 // DefaultTime2Wait, result function is Maximum.
1277 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2WAIT
);
1278 if (Value
== NULL
) {
1282 NumericValue
= AsciiStrDecimalToUintn (Value
);
1283 if (NumericValue
== 0) {
1284 Session
->DefaultTime2Wait
= 0;
1285 } else if (NumericValue
> 3600) {
1288 Session
->DefaultTime2Wait
= (UINT32
) MAX (Session
->DefaultTime2Wait
, NumericValue
);
1291 // DefaultTime2Retain, result function is Minimum.
1293 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2RETAIN
);
1294 if (Value
== NULL
) {
1298 NumericValue
= AsciiStrDecimalToUintn (Value
);
1299 if (NumericValue
== 0) {
1300 Session
->DefaultTime2Retain
= 0;
1301 } else if (NumericValue
> 3600) {
1304 Session
->DefaultTime2Retain
= (UINT32
) MIN (Session
->DefaultTime2Retain
, NumericValue
);
1307 // MaxOutstandingR2T, result function is Minimum.
1309 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_OUTSTANDING_R2T
);
1310 if (Value
== NULL
) {
1314 NumericValue
= AsciiStrDecimalToUintn (Value
);
1315 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1319 Session
->MaxOutstandingR2T
= (UINT16
) MIN (Session
->MaxOutstandingR2T
, NumericValue
);
1322 // Remove declarative key-value paris if any.
1324 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_SESSION_TYPE
);
1325 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_ALIAS
);
1326 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_PORTAL_GROUP_TAG
);
1328 if (IsListEmpty (KeyValueList
)) {
1330 // Succeed if no more keys in the list.
1332 Status
= EFI_SUCCESS
;
1337 IScsiFreeKeyValueList (KeyValueList
);
1339 gBS
->FreePool (Data
);
1345 Fill the oprational prameters.
1347 @param[in] Conn The connection in iSCSI login.
1348 @param[in, out] Pdu The iSCSI login request PDU to fill the parameters.
1350 @retval EFI_SUCCESS The parmeters are filled into the iSCSI login request PDU.
1354 IN ISCSI_CONNECTION
*Conn
,
1358 ISCSI_SESSION
*Session
;
1361 Session
= Conn
->Session
;
1363 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->HeaderDigest
== ISCSI_DIGEST_CRC32
) ? "None,CRC32" : "None");
1364 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_HEADER_DIGEST
, Value
);
1366 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->DataDigest
== ISCSI_DIGEST_CRC32
) ? "None,CRC32" : "None");
1367 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_DIGEST
, Value
);
1369 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->ErrorRecoveryLevel
);
1370 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_ERROR_RECOVERY_LEVEL
, Value
);
1372 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->InitialR2T
? "Yes" : "No");
1373 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_INITIAL_R2T
, Value
);
1375 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->ImmediateData
? "Yes" : "No");
1376 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_IMMEDIATE_DATA
, Value
);
1378 AsciiSPrint (Value
, sizeof (Value
), "%d", Conn
->MaxRecvDataSegmentLength
);
1379 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
, Value
);
1381 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxBurstLength
);
1382 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_BURST_LENGTH
, Value
);
1384 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->FirstBurstLength
);
1385 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_FIRST_BURST_LENGTH
, Value
);
1387 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxConnections
);
1388 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_CONNECTIONS
, Value
);
1390 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataPDUInOrder
? "Yes" : "No");
1391 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_PDU_IN_ORDER
, Value
);
1393 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataSequenceInOrder
? "Yes" : "No");
1394 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
, Value
);
1396 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Wait
);
1397 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2WAIT
, Value
);
1399 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Retain
);
1400 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2RETAIN
, Value
);
1402 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxOutstandingR2T
);
1403 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_OUTSTANDING_R2T
, Value
);
1409 Pad the iSCSI AHS or data segment to an integer number of 4 byte words.
1411 @param[in, out] Pdu The iSCSI pdu which contains segments to pad.
1412 @param[in] Len The length of the last semgnet in the PDU.
1414 @retval EFI_SUCCESS The segment is padded or no need to pad it.
1415 @retval EFI_OUT_OF_RESOURCES There is not enough remaining free space to add the
1420 IN OUT NET_BUF
*Pdu
,
1427 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1430 Data
= NetbufAllocSpace (Pdu
, PadLen
, NET_BUF_TAIL
);
1432 return EFI_OUT_OF_RESOURCES
;
1435 ZeroMem (Data
, PadLen
);
1442 Build a key-value list from the data segment.
1444 @param[in] Data The data segment containing the key-value pairs.
1445 @param[in] Len Length of the data segment.
1447 @return The key-value list.
1448 @retval NULL Other errors as indicated.
1451 IScsiBuildKeyValueList (
1456 LIST_ENTRY
*ListHead
;
1457 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1459 ListHead
= AllocatePool (sizeof (LIST_ENTRY
));
1460 if (ListHead
== NULL
) {
1464 InitializeListHead (ListHead
);
1467 KeyValuePair
= AllocatePool (sizeof (ISCSI_KEY_VALUE_PAIR
));
1468 if (KeyValuePair
== NULL
) {
1472 InitializeListHead (&KeyValuePair
->List
);
1474 KeyValuePair
->Key
= Data
;
1476 while ((Len
> 0) && (*Data
!= '=')) {
1487 gBS
->FreePool (KeyValuePair
);
1491 KeyValuePair
->Value
= Data
;
1493 InsertTailList (ListHead
, &KeyValuePair
->List
);;
1495 Data
+= AsciiStrLen (KeyValuePair
->Value
) + 1;
1496 Len
-= (UINT32
) AsciiStrLen (KeyValuePair
->Value
) + 1;
1503 IScsiFreeKeyValueList (ListHead
);
1509 Get the value string by the key name from the key-value list. If found,
1510 the key-value entry will be removed from the list.
1512 @param[in, out] KeyValueList The key-value list.
1513 @param[in] Key The key name to find.
1515 @return The value string.
1518 IScsiGetValueByKeyFromList (
1519 IN OUT LIST_ENTRY
*KeyValueList
,
1524 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1529 NET_LIST_FOR_EACH (Entry
, KeyValueList
) {
1530 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1532 if (AsciiStrCmp (KeyValuePair
->Key
, Key
) == 0) {
1533 Value
= KeyValuePair
->Value
;
1535 RemoveEntryList (&KeyValuePair
->List
);
1536 gBS
->FreePool (KeyValuePair
);
1545 Free the key-value list.
1547 @param[in] KeyValueList The key-value list.
1550 IScsiFreeKeyValueList (
1551 IN LIST_ENTRY
*KeyValueList
1555 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1557 while (!IsListEmpty (KeyValueList
)) {
1558 Entry
= NetListRemoveHead (KeyValueList
);
1559 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1561 gBS
->FreePool (KeyValuePair
);
1564 gBS
->FreePool (KeyValueList
);
1568 Normalize the iSCSI name according to RFC.
1570 @param[in, out] Name The iSCSI name.
1571 @param[in] Len length of the iSCSI name.
1573 @retval EFI_SUCCESS The iSCSI name is valid and normalized.
1574 @retval EFI_PROTOCOL_ERROR The iSCSI name is mal-formatted or not in the IQN format.
1577 IScsiNormalizeName (
1584 for (Index
= 0; Index
< Len
; Index
++) {
1585 if (NET_IS_UPPER_CASE_CHAR (Name
[Index
])) {
1587 // Convert the upper-case characters to lower-case ones
1589 Name
[Index
] = (CHAR8
) (Name
[Index
] - 'A' + 'a');
1592 if (!NET_IS_LOWER_CASE_CHAR (Name
[Index
]) &&
1593 !NET_IS_DIGIT (Name
[Index
]) &&
1594 (Name
[Index
] != '-') &&
1595 (Name
[Index
] != '.') &&
1596 (Name
[Index
] != ':')
1599 // ASCII dash, dot, colon lower-case characters and digit characters
1602 return EFI_PROTOCOL_ERROR
;
1606 if ((Len
< 4) || (CompareMem (Name
, "iqn.", 4) != 0)) {
1608 // Only IQN format is accepted now.
1610 return EFI_PROTOCOL_ERROR
;
1617 Create an iSCSI task control block.
1619 @param[in] Conn The connection on which the task control block will be created.
1620 @param[out] Tcb The newly created task control block.
1622 @retval EFI_SUCCESS The task control block is created.
1623 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1624 @retval EFI_NOT_READY The target can not accept new commands.
1628 IN ISCSI_CONNECTION
*Conn
,
1632 ISCSI_SESSION
*Session
;
1635 ASSERT (Tcb
!= NULL
);
1637 Session
= Conn
->Session
;
1639 if (ISCSI_SEQ_GT (Session
->CmdSN
, Session
->MaxCmdSN
)) {
1640 return EFI_NOT_READY
;
1643 NewTcb
= AllocateZeroPool (sizeof (ISCSI_TCB
));
1644 if (NewTcb
== NULL
) {
1645 return EFI_OUT_OF_RESOURCES
;
1648 InitializeListHead (&NewTcb
->Link
);
1650 NewTcb
->SoFarInOrder
= TRUE
;
1651 NewTcb
->InitiatorTaskTag
= Session
->InitiatorTaskTag
;
1652 NewTcb
->CmdSN
= Session
->CmdSN
;
1653 NewTcb
->Conn
= Conn
;
1655 InsertTailList (&Session
->TcbList
, &NewTcb
->Link
);
1658 // Advance the initiator task tag.
1660 Session
->InitiatorTaskTag
++;
1669 Delete the tcb from the connection and destroy it.
1671 @param[in] Tcb The tcb to delete.
1678 RemoveEntryList (&Tcb
->Link
);
1680 gBS
->FreePool (Tcb
);
1684 Find the task control block by the initator task tag.
1686 @param[in] TcbList The tcb list.
1687 @param[in] InitiatorTaskTag The initiator task tag.
1689 @return The task control block found.
1693 IN LIST_ENTRY
*TcbList
,
1694 IN UINT32 InitiatorTaskTag
1702 NET_LIST_FOR_EACH (Entry
, TcbList
) {
1703 Tcb
= NET_LIST_USER_STRUCT (Entry
, ISCSI_TCB
, Link
);
1705 if (Tcb
->InitiatorTaskTag
== InitiatorTaskTag
) {
1716 Create a data segment, pad it and calculate the CRC if needed.
1718 @param[in] Data The data to fill into the data segment.
1719 @param[in] Len Length of the data.
1720 @param[in] DataDigest Whether to calculate CRC for this data segment.
1722 @return The net buffer wrapping the data segment.
1725 IScsiNewDataSegment (
1728 IN BOOLEAN DataDigest
1731 NET_FRAGMENT Fragment
[2];
1732 UINT32 FragmentCount
;
1736 Fragment
[0].Len
= Len
;
1737 Fragment
[0].Bulk
= Data
;
1739 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1741 Fragment
[1].Len
= PadLen
;
1742 Fragment
[1].Bulk
= (UINT8
*) &mDataSegPad
;
1749 DataSeg
= NetbufFromExt (&Fragment
[0], FragmentCount
, 0, 0, IScsiNbufExtFree
, NULL
);
1755 Create a iSCSI SCSI command PDU to encapsulate the command issued
1756 by SCSI through the EXT SCSI PASS THRU Protocol.
1758 @param[in] Packet The EXT SCSI PASS THRU request packet containing the SCSI command.
1759 @param[in] Lun The LUN.
1760 @param[in] Tcb The tcb assocated with this SCSI command.
1762 @return The created iSCSI SCSI command PDU.
1763 @retval NULL Other errors as indicated.
1766 IScsiNewScsiCmdPdu (
1767 IN EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
1772 LIST_ENTRY
*NbufList
;
1776 SCSI_COMMAND
*ScsiCmd
;
1779 ISCSI_ADDITIONAL_HEADER
*Header
;
1780 ISCSI_BI_EXP_READ_DATA_LEN_AHS
*BiExpReadDataLenAHS
;
1781 ISCSI_SESSION
*Session
;
1782 UINT32 ImmediateDataLen
;
1786 if (Packet
->DataDirection
== DataBi
) {
1788 // Bi directional Read/Write command, the bidirectional expected
1789 // read data length AHS is required.
1791 AHSLength
+= sizeof (ISCSI_BI_EXP_READ_DATA_LEN_AHS
);
1794 if (Packet
->CdbLength
> 16) {
1796 // The CDB exceeds 16 bytes, an extended CDB AHS is required.
1798 AHSLength
= (UINT8
) (AHSLength
+ (ISCSI_ROUNDUP (Packet
->CdbLength
- 16) + sizeof (ISCSI_ADDITIONAL_HEADER
)));
1801 Length
= sizeof (SCSI_COMMAND
) + AHSLength
;
1802 PduHeader
= NetbufAlloc (Length
);
1803 if (PduHeader
== NULL
) {
1807 ScsiCmd
= (SCSI_COMMAND
*) NetbufAllocSpace (PduHeader
, Length
, NET_BUF_TAIL
);
1808 Header
= (ISCSI_ADDITIONAL_HEADER
*) (ScsiCmd
+ 1);
1810 ZeroMem (ScsiCmd
, Length
);
1812 ISCSI_SET_OPCODE (ScsiCmd
, ISCSI_OPCODE_SCSI_CMD
, 0);
1813 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_TASK_ATTR_SIMPLE
);
1816 // Set the READ/WRITE flags according to the IO type of this request.
1818 switch (Packet
->DataDirection
) {
1820 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
);
1821 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->InTransferLength
);
1825 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_WRITE
);
1826 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
1830 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
| SCSI_CMD_PDU_FLAG_WRITE
);
1831 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
1834 // Fill the bidirectional expected read data length AHS.
1836 BiExpReadDataLenAHS
= (ISCSI_BI_EXP_READ_DATA_LEN_AHS
*) Header
;
1837 Header
= (ISCSI_ADDITIONAL_HEADER
*) (BiExpReadDataLenAHS
+ 1);
1839 BiExpReadDataLenAHS
->Length
= NTOHS (5);
1840 BiExpReadDataLenAHS
->Type
= ISCSI_AHS_TYPE_BI_EXP_READ_DATA_LEN
;
1841 BiExpReadDataLenAHS
->ExpReadDataLength
= NTOHL (Packet
->InTransferLength
);
1846 ScsiCmd
->TotalAHSLength
= AHSLength
;
1847 CopyMem (ScsiCmd
->Lun
, &Lun
, sizeof (ScsiCmd
->Lun
));
1848 ScsiCmd
->InitiatorTaskTag
= NTOHL (Tcb
->InitiatorTaskTag
);
1849 ScsiCmd
->CmdSN
= NTOHL (Tcb
->CmdSN
);
1850 ScsiCmd
->ExpStatSN
= NTOHL (Tcb
->Conn
->ExpStatSN
);
1852 CopyMem (ScsiCmd
->CDB
, Packet
->Cdb
, sizeof (ScsiCmd
->CDB
));
1854 if (Packet
->CdbLength
> 16) {
1855 Header
->Length
= NTOHS (Packet
->CdbLength
- 15);
1856 Header
->Type
= ISCSI_AHS_TYPE_EXT_CDB
;
1858 CopyMem (Header
+ 1, (UINT8
*) Packet
->Cdb
+ 16, Packet
->CdbLength
- 16);
1862 Session
= Tcb
->Conn
->Session
;
1863 ImmediateDataLen
= 0;
1865 if (Session
->ImmediateData
&& (Packet
->OutTransferLength
!= 0)) {
1867 // Send immediate data in this SCSI Command PDU. The length of the immeidate
1868 // data is the minimum of FirstBurstLength, the data length to be xfered and
1869 // the MaxRecvdataSegmentLength on this connection.
1871 ImmediateDataLen
= MIN (Session
->FirstBurstLength
, Packet
->OutTransferLength
);
1872 ImmediateDataLen
= MIN (ImmediateDataLen
, Tcb
->Conn
->MaxRecvDataSegmentLength
);
1875 // Update the data segment length in the PDU header.
1877 ISCSI_SET_DATASEG_LEN (ScsiCmd
, ImmediateDataLen
);
1880 // Create the data segment.
1882 DataSeg
= IScsiNewDataSegment ((UINT8
*) Packet
->OutDataBuffer
, ImmediateDataLen
, FALSE
);
1883 if (DataSeg
== NULL
) {
1884 NetbufFree (PduHeader
);
1889 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
1890 if (NbufList
== NULL
) {
1891 NetbufFree (PduHeader
);
1892 NetbufFree (DataSeg
);
1898 InitializeListHead (NbufList
);
1899 InsertTailList (NbufList
, &PduHeader
->List
);
1900 InsertTailList (NbufList
, &DataSeg
->List
);
1902 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1904 IScsiFreeNbufList (NbufList
);
1908 if (Session
->InitialR2T
||
1909 (ImmediateDataLen
== Session
->FirstBurstLength
) ||
1910 (ImmediateDataLen
== Packet
->OutTransferLength
)
1913 // Unsolicited data out sequence is not allowed,
1914 // or FirstBustLength data is already sent out by immediate data
1915 // or all the OUT data accompany this SCSI packet is sent as
1916 // immediate data, the final flag should be set on this SCSI Command
1919 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_BHS_FLAG_FINAL
);
1928 Create a new iSCSI SCSI Data Out PDU.
1930 @param[in] Data The data to put into the Data Out PDU.
1931 @param[in] Len Length of the data.
1932 @param[in] DataSN The DataSN of the Data Out PDU.
1933 @param[in] Tcb The task control block of this Data Out PDU.
1934 @param[in] Lun The LUN.
1936 @return The net buffer wrapping the Data Out PDU.
1937 @retval NULL Other errors as indicated.
1940 IScsiNewDataOutPdu (
1948 LIST_ENTRY
*NbufList
;
1952 ISCSI_SCSI_DATA_OUT
*DataOutHdr
;
1953 ISCSI_XFER_CONTEXT
*XferContext
;
1955 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
1956 if (NbufList
== NULL
) {
1960 InitializeListHead (NbufList
);
1963 // Allocate memory for the BHS.
1965 PduHdr
= NetbufAlloc (sizeof (ISCSI_SCSI_DATA_OUT
));
1966 if (PduHdr
== NULL
) {
1967 gBS
->FreePool (NbufList
);
1971 // Insert the BHS into the buffer list.
1973 InsertTailList (NbufList
, &PduHdr
->List
);
1975 DataOutHdr
= (ISCSI_SCSI_DATA_OUT
*) NetbufAllocSpace (PduHdr
, sizeof (ISCSI_SCSI_DATA_OUT
), NET_BUF_TAIL
);
1976 XferContext
= &Tcb
->XferContext
;
1978 ZeroMem (DataOutHdr
, sizeof (ISCSI_SCSI_DATA_OUT
));
1981 // Set the flags and fields of the Data Out PDU BHS.
1983 ISCSI_SET_OPCODE (DataOutHdr
, ISCSI_OPCODE_SCSI_DATA_OUT
, 0);
1984 ISCSI_SET_DATASEG_LEN (DataOutHdr
, Len
);
1986 DataOutHdr
->InitiatorTaskTag
= HTONL (Tcb
->InitiatorTaskTag
);
1987 DataOutHdr
->TargetTransferTag
= HTONL (XferContext
->TargetTransferTag
);
1988 DataOutHdr
->ExpStatSN
= HTONL (Tcb
->Conn
->ExpStatSN
);
1989 DataOutHdr
->DataSN
= HTONL (DataSN
);
1990 DataOutHdr
->BufferOffset
= HTONL (XferContext
->Offset
);
1992 if (XferContext
->TargetTransferTag
!= ISCSI_RESERVED_TAG
) {
1993 CopyMem (&DataOutHdr
->Lun
, &Lun
, sizeof (DataOutHdr
->Lun
));
1996 // Build the data segment for this Data Out PDU.
1998 DataSeg
= IScsiNewDataSegment (Data
, Len
, FALSE
);
1999 if (DataSeg
== NULL
) {
2000 IScsiFreeNbufList (NbufList
);
2004 // Put the data segment into the buffer list and combine it with the BHS
2005 // into a full Data Out PDU.
2007 InsertTailList (NbufList
, &DataSeg
->List
);
2008 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
2010 IScsiFreeNbufList (NbufList
);
2017 Generate a consecutive sequence of iSCSI SCSI Data Out PDUs.
2019 @param[in] Data The data which will be carried by the sequence of iSCSI SCSI Data Out PDUs.
2020 @param[in] Tcb The task control block of the data to send out.
2021 @param[in] Lun The LUN the data will be sent to.
2023 @return A list of net buffers with each of them wraps an iSCSI SCSI Data Out PDU.
2024 @retval NULL Other errors as indicated.
2027 IScsiGenerateDataOutPduSequence (
2033 LIST_ENTRY
*PduList
;
2036 NET_BUF
*DataOutPdu
;
2037 ISCSI_CONNECTION
*Conn
;
2038 ISCSI_XFER_CONTEXT
*XferContext
;
2040 PduList
= AllocatePool (sizeof (LIST_ENTRY
));
2041 if (PduList
== NULL
) {
2045 InitializeListHead (PduList
);
2050 XferContext
= &Tcb
->XferContext
;
2052 while (XferContext
->DesiredLength
> 0) {
2054 // Determine the length of data this Data Out PDU can carry.
2056 DataLen
= MIN (XferContext
->DesiredLength
, Conn
->MaxRecvDataSegmentLength
);
2059 // Create a Data Out PDU.
2061 DataOutPdu
= IScsiNewDataOutPdu (Data
, DataLen
, DataSN
, Tcb
, Lun
);
2062 if (DataOutPdu
== NULL
) {
2063 IScsiFreeNbufList (PduList
);
2069 InsertTailList (PduList
, &DataOutPdu
->List
);
2072 // Update the context and DataSN.
2074 XferContext
->Offset
+= DataLen
;
2075 XferContext
->DesiredLength
-= DataLen
;
2080 // Set the F bit for the last data out PDU in this sequence.
2082 ISCSI_SET_FLAG (NetbufGetByte (DataOutPdu
, 0, NULL
), ISCSI_BHS_FLAG_FINAL
);
2090 Send the Data in a sequence of Data Out PDUs one by one.
2092 @param[in] Data The data to carry by Data Out PDUs.
2093 @param[in] Lun The LUN the data will be sent to.
2094 @param[in] Tcb The task control block.
2096 @retval EFI_SUCCES The data is sent out to the LUN.
2097 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2098 @retval Others Other errors as indicated.
2101 IScsiSendDataOutPduSequence (
2107 LIST_ENTRY
*DataOutPduList
;
2113 // Generate the Data Out PDU sequence.
2115 DataOutPduList
= IScsiGenerateDataOutPduSequence (Data
, Tcb
, Lun
);
2116 if (DataOutPduList
== NULL
) {
2117 return EFI_OUT_OF_RESOURCES
;
2120 Status
= EFI_SUCCESS
;
2123 // Send the Data Out PDU's one by one.
2125 NET_LIST_FOR_EACH (Entry
, DataOutPduList
) {
2126 Pdu
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
2128 Status
= Tcp4IoTransmit (&Tcb
->Conn
->Tcp4Io
, Pdu
);
2129 if (EFI_ERROR (Status
)) {
2134 IScsiFreeNbufList (DataOutPduList
);
2140 Process the received iSCSI SCSI Data In PDU.
2142 @param[in] Pdu The Data In PDU received.
2143 @param[in] Tcb The task control block.
2144 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2146 @retval EFI_SUCCES The check on the Data IN PDU is passed and some update
2148 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2149 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2150 @retval Others Other errors as indicated.
2156 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2159 ISCSI_SCSI_DATA_IN
*DataInHdr
;
2162 DataInHdr
= (ISCSI_SCSI_DATA_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2164 DataInHdr
->InitiatorTaskTag
= NTOHL (DataInHdr
->InitiatorTaskTag
);
2165 DataInHdr
->ExpCmdSN
= NTOHL (DataInHdr
->ExpCmdSN
);
2166 DataInHdr
->MaxCmdSN
= NTOHL (DataInHdr
->MaxCmdSN
);
2167 DataInHdr
->DataSN
= NTOHL (DataInHdr
->DataSN
);
2170 // Check the DataSN.
2172 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, DataInHdr
->DataSN
);
2173 if (EFI_ERROR (Status
)) {
2177 if (DataInHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2178 return EFI_PROTOCOL_ERROR
;
2181 // Update the command related sequence numbers.
2183 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, DataInHdr
->MaxCmdSN
, DataInHdr
->ExpCmdSN
);
2185 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_STATUS_VALID
)) {
2186 if (!ISCSI_FLAG_ON (DataInHdr
, ISCSI_BHS_FLAG_FINAL
)) {
2188 // The S bit is on but the F bit is off.
2190 return EFI_PROTOCOL_ERROR
;
2193 Tcb
->StatusXferd
= TRUE
;
2195 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_OVERFLOW
| SCSI_DATA_IN_PDU_FLAG_UNDERFLOW
)) {
2197 // Underflow and Overflow are mutual flags.
2199 return EFI_PROTOCOL_ERROR
;
2202 // S bit is on, the StatSN is valid.
2204 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NTOHL (DataInHdr
->StatSN
));
2205 if (EFI_ERROR (Status
)) {
2209 Packet
->HostAdapterStatus
= 0;
2210 Packet
->TargetStatus
= DataInHdr
->Status
;
2212 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2213 Packet
->InTransferLength
+= NTOHL (DataInHdr
->ResidualCount
);
2214 Status
= EFI_BAD_BUFFER_SIZE
;
2217 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2218 Packet
->InTransferLength
-= NTOHL (DataInHdr
->ResidualCount
);
2226 Process the received iSCSI R2T PDU.
2228 @param[in] Pdu The R2T PDU received.
2229 @param[in] Tcb The task control block.
2230 @param[in] Lun The Lun.
2231 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2233 @retval EFI_SUCCES The R2T PDU is valid and the solicited data is sent out.
2234 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2235 @retval Others Other errors as indicated.
2242 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2245 ISCSI_READY_TO_TRANSFER
*R2THdr
;
2247 ISCSI_XFER_CONTEXT
*XferContext
;
2250 R2THdr
= (ISCSI_READY_TO_TRANSFER
*) NetbufGetByte (Pdu
, 0, NULL
);
2252 R2THdr
->InitiatorTaskTag
= NTOHL (R2THdr
->InitiatorTaskTag
);
2253 R2THdr
->TargetTransferTag
= NTOHL (R2THdr
->TargetTransferTag
);
2254 R2THdr
->StatSN
= NTOHL (R2THdr
->StatSN
);
2255 R2THdr
->R2TSN
= NTOHL (R2THdr
->R2TSN
);
2256 R2THdr
->BufferOffset
= NTOHL (R2THdr
->BufferOffset
);
2257 R2THdr
->DesiredDataTransferLength
= NTOHL (R2THdr
->DesiredDataTransferLength
);
2259 if ((R2THdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) || !ISCSI_SEQ_EQ (R2THdr
->StatSN
, Tcb
->Conn
->ExpStatSN
)) {
2260 return EFI_PROTOCOL_ERROR
;;
2263 // Check the sequence number.
2265 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, R2THdr
->R2TSN
);
2266 if (EFI_ERROR (Status
)) {
2270 XferContext
= &Tcb
->XferContext
;
2271 XferContext
->TargetTransferTag
= R2THdr
->TargetTransferTag
;
2272 XferContext
->Offset
= R2THdr
->BufferOffset
;
2273 XferContext
->DesiredLength
= R2THdr
->DesiredDataTransferLength
;
2275 if (((XferContext
->Offset
+ XferContext
->DesiredLength
) > Packet
->OutTransferLength
) ||
2276 (XferContext
->DesiredLength
> Tcb
->Conn
->Session
->MaxBurstLength
)
2278 return EFI_PROTOCOL_ERROR
;
2281 // Send the data solicited by this R2T.
2283 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2284 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2290 Process the received iSCSI SCSI Response PDU.
2292 @param[in] Pdu The Response PDU received.
2293 @param[in] Tcb The task control block.
2294 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2296 @retval EFI_SUCCES The Response PDU is processed.
2297 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2298 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2299 @retval Others Other errors as indicated.
2302 IScsiOnScsiRspRcvd (
2305 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2308 SCSI_RESPONSE
*ScsiRspHdr
;
2309 ISCSI_SENSE_DATA
*SenseData
;
2313 ScsiRspHdr
= (SCSI_RESPONSE
*) NetbufGetByte (Pdu
, 0, NULL
);
2315 ScsiRspHdr
->InitiatorTaskTag
= NTOHL (ScsiRspHdr
->InitiatorTaskTag
);
2316 if (ScsiRspHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2317 return EFI_PROTOCOL_ERROR
;
2320 ScsiRspHdr
->StatSN
= NTOHL (ScsiRspHdr
->StatSN
);
2322 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, ScsiRspHdr
->StatSN
);
2323 if (EFI_ERROR (Status
)) {
2327 ScsiRspHdr
->MaxCmdSN
= NTOHL (ScsiRspHdr
->MaxCmdSN
);
2328 ScsiRspHdr
->ExpCmdSN
= NTOHL (ScsiRspHdr
->ExpCmdSN
);
2329 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, ScsiRspHdr
->MaxCmdSN
, ScsiRspHdr
->ExpCmdSN
);
2331 Tcb
->StatusXferd
= TRUE
;
2333 Packet
->HostAdapterStatus
= ScsiRspHdr
->Response
;
2334 if (Packet
->HostAdapterStatus
!= ISCSI_SERVICE_RSP_COMMAND_COMPLETE_AT_TARGET
) {
2338 Packet
->TargetStatus
= ScsiRspHdr
->Status
;
2340 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
| SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
) ||
2341 ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
| SCSI_RSP_PDU_FLAG_UNDERFLOW
)
2343 return EFI_PROTOCOL_ERROR
;
2346 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
)) {
2347 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2348 Status
= EFI_BAD_BUFFER_SIZE
;
2351 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
)) {
2352 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2355 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2356 if (Packet
->DataDirection
== DataIn
) {
2357 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2359 Packet
->OutTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2362 Status
= EFI_BAD_BUFFER_SIZE
;
2365 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2366 if (Packet
->DataDirection
== DataIn
) {
2367 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2369 Packet
->OutTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2373 DataSegLen
= ISCSI_GET_DATASEG_LEN (ScsiRspHdr
);
2374 if (DataSegLen
!= 0) {
2375 SenseData
= (ISCSI_SENSE_DATA
*) NetbufGetByte (Pdu
, sizeof (SCSI_RESPONSE
), NULL
);
2377 SenseData
->Length
= NTOHS (SenseData
->Length
);
2379 Packet
->SenseDataLength
= (UINT8
) MIN (SenseData
->Length
, Packet
->SenseDataLength
);
2380 if (Packet
->SenseDataLength
!= 0) {
2381 CopyMem (Packet
->SenseData
, &SenseData
->Data
[0], Packet
->SenseDataLength
);
2384 Packet
->SenseDataLength
= 0;
2391 Process the received NOP In PDU.
2393 @param[in] Pdu The NOP In PDU received.
2394 @param[in] Tcb The task control block.
2396 @retval EFI_SUCCES The NOP In PDU is processed and the related sequence
2397 numbers are updated.
2398 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2406 ISCSI_NOP_IN
*NopInHdr
;
2409 NopInHdr
= (ISCSI_NOP_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2411 NopInHdr
->StatSN
= NTOHL (NopInHdr
->StatSN
);
2412 NopInHdr
->ExpCmdSN
= NTOHL (NopInHdr
->ExpCmdSN
);
2413 NopInHdr
->MaxCmdSN
= NTOHL (NopInHdr
->MaxCmdSN
);
2415 if (NopInHdr
->InitiatorTaskTag
== ISCSI_RESERVED_TAG
) {
2416 if (NopInHdr
->StatSN
!= Tcb
->Conn
->ExpStatSN
) {
2417 return EFI_PROTOCOL_ERROR
;
2420 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NopInHdr
->StatSN
);
2421 if (EFI_ERROR (Status
)) {
2426 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, NopInHdr
->MaxCmdSN
, NopInHdr
->ExpCmdSN
);
2432 Execute the SCSI command issued through the EXT SCSI PASS THRU protocol.
2434 @param[in] PassThru The EXT SCSI PASS THRU protocol.
2435 @param[in] Target The target ID.
2436 @param[in] Lun The LUN.
2437 @param[in, out] Packet The request packet containing IO request, SCSI command
2438 buffer and buffers to read/write.
2440 @retval EFI_SUCCES The SCSI command is executed and the result is updated to
2442 @retval EFI_DEVICE_ERROR Session state was not as required.
2443 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2444 @retval Others Other errors as indicated.
2447 IScsiExecuteScsiCommand (
2448 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*PassThru
,
2451 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2455 ISCSI_DRIVER_DATA
*Private
;
2456 ISCSI_SESSION
*Session
;
2457 EFI_EVENT TimeoutEvent
;
2458 ISCSI_CONNECTION
*Conn
;
2461 ISCSI_XFER_CONTEXT
*XferContext
;
2463 ISCSI_IN_BUFFER_CONTEXT InBufferContext
;
2467 Private
= ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru
);
2468 Session
= &Private
->Session
;
2469 Status
= EFI_SUCCESS
;
2471 TimeoutEvent
= NULL
;
2474 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
2475 return EFI_DEVICE_ERROR
;
2478 Conn
= NET_LIST_USER_STRUCT_S (
2479 Session
->Conns
.ForwardLink
,
2482 ISCSI_CONNECTION_SIGNATURE
2485 if (Packet
->Timeout
!= 0) {
2486 Timeout
= MultU64x32 (Packet
->Timeout
, 2);
2489 Status
= IScsiNewTcb (Conn
, &Tcb
);
2490 if (EFI_ERROR (Status
)) {
2494 // Encapsulate the SCSI request packet into an iSCSI SCSI Command PDU.
2496 Pdu
= IScsiNewScsiCmdPdu (Packet
, Lun
, Tcb
);
2498 Status
= EFI_OUT_OF_RESOURCES
;
2502 XferContext
= &Tcb
->XferContext
;
2503 Buffer
= NetbufGetByte (Pdu
, 0, NULL
);
2504 XferContext
->Offset
= ISCSI_GET_DATASEG_LEN (Buffer
);
2507 // Transmit the SCSI Command PDU.
2509 Status
= Tcp4IoTransmit (&Conn
->Tcp4Io
, Pdu
);
2513 if (EFI_ERROR (Status
)) {
2517 if (!Session
->InitialR2T
&&
2518 (XferContext
->Offset
< Session
->FirstBurstLength
) &&
2519 (XferContext
->Offset
< Packet
->OutTransferLength
)
2522 // Unsolicited Data-Out sequence is allowed, there is remaining SCSI
2523 // OUT data and the limit of FirstBurstLength is not reached.
2525 XferContext
->TargetTransferTag
= ISCSI_RESERVED_TAG
;
2526 XferContext
->DesiredLength
= MIN (
2527 Session
->FirstBurstLength
,
2528 Packet
->OutTransferLength
- XferContext
->Offset
2531 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2532 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2533 if (EFI_ERROR (Status
)) {
2538 InBufferContext
.InData
= (UINT8
*) Packet
->InDataBuffer
;
2539 InBufferContext
.InDataLen
= Packet
->InTransferLength
;
2541 while (!Tcb
->StatusXferd
) {
2543 // Start the timeout timer.
2546 Status
= gBS
->SetTimer (Conn
->TimeoutEvent
, TimerRelative
, Timeout
);
2547 if (EFI_ERROR (Status
)) {
2550 TimeoutEvent
= Conn
->TimeoutEvent
;
2553 // try to receive PDU from target.
2555 Status
= IScsiReceivePdu (Conn
, &Pdu
, &InBufferContext
, FALSE
, FALSE
, TimeoutEvent
);
2556 if (EFI_ERROR (Status
)) {
2560 switch (ISCSI_GET_OPCODE (NetbufGetByte (Pdu
, 0, NULL
))) {
2561 case ISCSI_OPCODE_SCSI_DATA_IN
:
2562 Status
= IScsiOnDataInRcvd (Pdu
, Tcb
, Packet
);
2565 case ISCSI_OPCODE_R2T
:
2566 Status
= IScsiOnR2TRcvd (Pdu
, Tcb
, Lun
, Packet
);
2569 case ISCSI_OPCODE_SCSI_RSP
:
2570 Status
= IScsiOnScsiRspRcvd (Pdu
, Tcb
, Packet
);
2573 case ISCSI_OPCODE_NOP_IN
:
2574 Status
= IScsiOnNopInRcvd (Pdu
, Tcb
);
2577 case ISCSI_OPCODE_VENDOR_T0
:
2578 case ISCSI_OPCODE_VENDOR_T1
:
2579 case ISCSI_OPCODE_VENDOR_T2
:
2581 // These messages are vendor specific, skip them.
2586 Status
= EFI_PROTOCOL_ERROR
;
2592 if (EFI_ERROR (Status
)) {
2599 if (TimeoutEvent
!= NULL
) {
2600 gBS
->SetTimer (TimeoutEvent
, TimerCancel
, 0);
2607 if ((Status
!= EFI_SUCCESS
) && (Status
!= EFI_NOT_READY
)) {
2609 // Reinstate the session.
2611 if (EFI_ERROR (IScsiSessionReinstatement (Private
))) {
2612 Status
= EFI_DEVICE_ERROR
;
2620 Reinstate the session on some error.
2622 @param[in, out] Private The iSCSI driver data.
2624 @retval EFI_SUCCES The session is reinstated from some error.
2625 @retval Other Reinstatement failed.
2628 IScsiSessionReinstatement (
2629 IN OUT ISCSI_DRIVER_DATA
*Private
2632 ISCSI_SESSION
*Session
;
2635 Session
= &Private
->Session
;
2636 ASSERT (Session
->State
== SESSION_STATE_LOGGED_IN
);
2639 // Abort the session and re-init it.
2641 IScsiSessionAbort (Session
);
2642 IScsiSessionInit (Session
, TRUE
);
2647 Status
= IScsiSessionLogin (Private
);
2653 Initialize some session parameters before login.
2655 @param[in, out] Session The iSCSI session.
2656 @param[in] Recovery Whether the request is from a fresh new start or recovery.
2660 IN OUT ISCSI_SESSION
*Session
,
2667 Session
->Signature
= ISCSI_SESSION_SIGNATURE
;
2668 Session
->State
= SESSION_STATE_FREE
;
2670 Random
= NET_RANDOM (NetRandomInitSeed ());
2672 Session
->ISID
[0] = ISID_BYTE_0
;
2673 Session
->ISID
[1] = ISID_BYTE_1
;
2674 Session
->ISID
[2] = ISID_BYTE_2
;
2675 Session
->ISID
[3] = ISID_BYTE_3
;
2676 Session
->ISID
[4] = (UINT8
) Random
;
2677 Session
->ISID
[5] = (UINT8
) (Random
>> 8);
2679 InitializeListHead (&Session
->Conns
);
2680 InitializeListHead (&Session
->TcbList
);
2686 Session
->InitiatorTaskTag
= 1;
2687 Session
->NextCID
= 1;
2689 Session
->TargetPortalGroupTag
= 0;
2690 Session
->MaxConnections
= ISCSI_MAX_CONNS_PER_SESSION
;
2691 Session
->InitialR2T
= FALSE
;
2692 Session
->ImmediateData
= TRUE
;
2693 Session
->MaxBurstLength
= 262144;
2694 Session
->FirstBurstLength
= MAX_RECV_DATA_SEG_LEN_IN_FFP
;
2695 Session
->DefaultTime2Wait
= 2;
2696 Session
->DefaultTime2Retain
= 20;
2697 Session
->MaxOutstandingR2T
= DEFAULT_MAX_OUTSTANDING_R2T
;
2698 Session
->DataPDUInOrder
= TRUE
;
2699 Session
->DataSequenceInOrder
= TRUE
;
2700 Session
->ErrorRecoveryLevel
= 0;
2704 Abort the iSCSI session, that is, reset all the connection and free the
2707 @param[in, out] Session The iSCSI session.
2709 @retval EFI_SUCCES The session is aborted.
2713 IN OUT ISCSI_SESSION
*Session
2716 ISCSI_DRIVER_DATA
*Private
;
2717 ISCSI_CONNECTION
*Conn
;
2719 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
2723 ASSERT (!IsListEmpty (&Session
->Conns
));
2725 Private
= ISCSI_DRIVER_DATA_FROM_SESSION (Session
);
2727 while (!IsListEmpty (&Session
->Conns
)) {
2728 Conn
= NET_LIST_USER_STRUCT_S (
2729 Session
->Conns
.ForwardLink
,
2732 ISCSI_CONNECTION_SIGNATURE
2735 gBS
->CloseProtocol (
2736 Conn
->Tcp4Io
.Handle
,
2737 &gEfiTcp4ProtocolGuid
,
2739 Private
->ExtScsiPassThruHandle
2742 IScsiConnReset (Conn
);
2744 IScsiDetatchConnection (Conn
);
2745 IScsiDestroyConnection (Conn
);
2748 Session
->State
= SESSION_STATE_FAILED
;