2 The implementation of iSCSI protocol based on RFC3720.
4 Copyright (c) 2004 - 2009, 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 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 ASSERT (LoginReq
!= NULL
);
496 ZeroMem (LoginReq
, sizeof (ISCSI_LOGIN_REQUEST
));
499 // Init the login request pdu
501 ISCSI_SET_OPCODE (LoginReq
, ISCSI_OPCODE_LOGIN_REQ
, ISCSI_REQ_IMMEDIATE
);
502 ISCSI_SET_STAGES (LoginReq
, Conn
->CurrentStage
, Conn
->NextStage
);
503 LoginReq
->VersionMax
= ISCSI_VERSION_MAX
;
504 LoginReq
->VersionMin
= ISCSI_VERSION_MIN
;
505 LoginReq
->Tsih
= HTONS (Session
->Tsih
);
506 LoginReq
->InitiatorTaskTag
= HTONL (Session
->InitiatorTaskTag
);
507 LoginReq
->Cid
= HTONS (Conn
->Cid
);
508 LoginReq
->CmdSN
= HTONL (Session
->CmdSN
);
511 // For the first Login Request on a coonection this is ExpStatSN for the
512 // old connection and this field is only valid if the Login Request restarts
514 // For subsequent Login Requests it is used to acknowledge the Login Responses
515 // with their increasing StatSN values.
517 LoginReq
->ExpStatSN
= HTONL (Conn
->ExpStatSN
);
518 CopyMem (LoginReq
->Isid
, Session
->Isid
, sizeof (LoginReq
->Isid
));
520 if (Conn
->PartialRspRcvd
) {
522 // A partial response, initiator must send an empty Login Request.
527 switch (Conn
->CurrentStage
) {
528 case ISCSI_SECURITY_NEGOTIATION
:
529 Status
= IScsiCHAPToSendReq (Conn
, Nbuf
);
532 case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
:
533 Status
= IScsiFillOpParams (Conn
, Nbuf
);
534 ISCSI_SET_FLAG (LoginReq
, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT
);
539 // something error happens...
541 Status
= EFI_DEVICE_ERROR
;
545 if (EFI_ERROR (Status
)) {
550 // Pad the data segment if needed.
552 IScsiPadSegment (Nbuf
, ISCSI_GET_DATASEG_LEN (LoginReq
));
554 // Check whether we will issue the stage transition signal?
556 Conn
->TransitInitiated
= (BOOLEAN
) ISCSI_FLAG_ON (LoginReq
, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT
);
563 Process the iSCSI Login Response.
565 @param[in, out] Conn The connection on which the iSCSI login response is received.
566 @param[in, out] Pdu The iSCSI login response PDU.
568 @retval EFI_SUCCESS The iSCSI login response PDU is processed and all check are passed.
569 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
570 @retval EFI_MEDIA_CHANGED Target is redirected.
571 @retval Others Other errors as indicated.
574 IScsiProcessLoginRsp (
575 IN OUT ISCSI_CONNECTION
*Conn
,
580 ISCSI_SESSION
*Session
;
581 ISCSI_LOGIN_RESPONSE
*LoginRsp
;
589 Session
= Conn
->Session
;
591 LoginRsp
= (ISCSI_LOGIN_RESPONSE
*) NetbufGetByte (Pdu
, 0, NULL
);
592 if (!ISCSI_CHECK_OPCODE (LoginRsp
, ISCSI_OPCODE_LOGIN_RSP
)) {
594 // It's not a Login Response
596 return EFI_PROTOCOL_ERROR
;
599 // Get the data segment if any.
601 DataSegLen
= ISCSI_GET_DATASEG_LEN (LoginRsp
);
602 if (DataSegLen
!= 0) {
603 DataSeg
= NetbufGetByte (Pdu
, sizeof (ISCSI_LOGIN_RESPONSE
), NULL
);
608 // Check the status class in the login response PDU.
610 switch (LoginRsp
->StatusClass
) {
611 case ISCSI_LOGIN_STATUS_SUCCESS
:
613 // Just break here, the response and the data segment will be processed later.
617 case ISCSI_LOGIN_STATUS_REDIRECTION
:
619 // The target may be moved to a different address
621 if (DataSeg
== NULL
) {
622 return EFI_PROTOCOL_ERROR
;
625 // Process the TargetAddress key-value strings in the data segment to update the
626 // target address info.
628 Status
= IScsiUpdateTargetAddress (Session
, (CHAR8
*)DataSeg
, DataSegLen
);
629 if (EFI_ERROR (Status
)) {
633 // Session will be restarted on this error status because the Target is
634 // redirected by this Login Response.
636 return EFI_MEDIA_CHANGED
;
640 // Initiator Error, Target Error, or any other undefined error code.
642 return EFI_PROTOCOL_ERROR
;
645 // The status is sucess, extract the wanted fields from the header segment.
647 Transit
= (BOOLEAN
) ISCSI_FLAG_ON (LoginRsp
, ISCSI_LOGIN_RSP_PDU_FLAG_TRANSIT
);
648 Continue
= (BOOLEAN
) ISCSI_FLAG_ON (LoginRsp
, ISCSI_LOGIN_RSP_PDU_FLAG_CONTINUE
);
650 CurrentStage
= (UINT8
) ISCSI_GET_CURRENT_STAGE (LoginRsp
);
651 NextStage
= (UINT8
) ISCSI_GET_NEXT_STAGE (LoginRsp
);
653 LoginRsp
->InitiatorTaskTag
= NTOHL (LoginRsp
->InitiatorTaskTag
);
655 if ((Transit
&& Continue
) ||
656 (CurrentStage
!= Conn
->CurrentStage
) ||
657 (!Conn
->TransitInitiated
&& Transit
) ||
658 (Transit
&& (NextStage
!= Conn
->NextStage
)) ||
659 (CompareMem (Session
->Isid
, LoginRsp
->Isid
, sizeof (LoginRsp
->Isid
)) != 0) ||
660 (LoginRsp
->InitiatorTaskTag
!= Session
->InitiatorTaskTag
)
663 // A Login Response with the C bit set to 1 MUST have the T bit set to 0;
664 // The CSG in the Login Response MUST be the same with the I-end of this connection;
665 // The T bit can't be 1 if the last Login Response sent by the initiator doesn't
666 // initiate the transistion;
667 // The NSG MUST be the same with the I-end of this connection if Transit is required.
668 // The ISID in the Login Response MUST be the same with this session.
670 return EFI_PROTOCOL_ERROR
;
673 LoginRsp
->StatSN
= NTOHL (LoginRsp
->StatSN
);
674 LoginRsp
->ExpCmdSN
= NTOHL (LoginRsp
->ExpCmdSN
);
675 LoginRsp
->MaxCmdSN
= NTOHL (LoginRsp
->MaxCmdSN
);
677 if ((Conn
->CurrentStage
== ISCSI_SECURITY_NEGOTIATION
) && (Conn
->CHAPStep
== ISCSI_CHAP_INITIAL
)) {
679 // It's the initial Login Response, initialize the local ExpStatSN, MaxCmdSN
682 Conn
->ExpStatSN
= LoginRsp
->StatSN
+ 1;
683 Session
->MaxCmdSN
= LoginRsp
->MaxCmdSN
;
684 Session
->ExpCmdSN
= LoginRsp
->ExpCmdSN
;
687 // Check the StatSN of this PDU
689 Status
= IScsiCheckSN (&Conn
->ExpStatSN
, LoginRsp
->StatSN
);
690 if (!EFI_ERROR (Status
)) {
692 // Update the MaxCmdSN and ExpCmdSN
694 IScsiUpdateCmdSN (Session
, LoginRsp
->MaxCmdSN
, LoginRsp
->ExpCmdSN
);
700 // Trim off the header segment.
702 NetbufTrim (Pdu
, sizeof (ISCSI_LOGIN_RESPONSE
), NET_BUF_HEAD
);
705 // Queue this login response first in case it's a partial response so that
706 // later when the full response list is received we can combine these scattered
707 // responses' data segment and then process it.
710 NetbufQueAppend (&Conn
->RspQue
, Pdu
);
712 Conn
->PartialRspRcvd
= Continue
;
715 // It's a partial response, have to wait for another or more Request/Response
716 // conversations to get the full response.
721 switch (CurrentStage
) {
722 case ISCSI_SECURITY_NEGOTIATION
:
724 // In security negotiation stage, let CHAP module handle it.
726 Status
= IScsiCHAPOnRspReceived (Conn
);
729 case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
:
731 // Response received with negotiation resonse on iSCSI parameters, check them.
733 Status
= IScsiCheckOpParams (Conn
);
738 // Should never get here.
740 Status
= EFI_PROTOCOL_ERROR
;
744 if (Transit
&& (Status
== EFI_SUCCESS
)) {
746 // Do the state transition.
748 Conn
->CurrentStage
= Conn
->NextStage
;
750 if (Conn
->CurrentStage
== ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
) {
751 Conn
->NextStage
= ISCSI_FULL_FEATURE_PHASE
;
754 // CurrentStage is iSCSI Full Feature, it's the Login-Final Response,
755 // get the TSIH from the Login Response.
757 Session
->Tsih
= NTOHS (LoginRsp
->Tsih
);
761 // Flush the response(s) received.
763 NetbufQueFlush (&Conn
->RspQue
);
769 Updated the target information according the data received in the iSCSI
770 login response with an target redirection status.
772 @param[in, out] Session The iSCSI session.
773 @param[in] Data The data segment which should contain the
774 TargetAddress key-value list.
775 @param[in] Len Length of the data.
777 @retval EFI_SUCCESS The target address is updated.
778 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
779 @retval EFI_NOT_FOUND The TargetAddress key is not found.
780 @retval Others Other errors as indicated.
783 IScsiUpdateTargetAddress (
784 IN OUT ISCSI_SESSION
*Session
,
789 LIST_ENTRY
*KeyValueList
;
790 CHAR8
*TargetAddress
;
795 KeyValueList
= IScsiBuildKeyValueList (Data
, Len
);
796 if (KeyValueList
== NULL
) {
797 return EFI_OUT_OF_RESOURCES
;
800 Status
= EFI_NOT_FOUND
;
803 TargetAddress
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_ADDRESS
);
804 if (TargetAddress
== NULL
) {
808 if (!NET_IS_DIGIT (TargetAddress
[0])) {
810 // The domainname of the target may be presented in three formats: a DNS host name,
811 // a dotted-decimal IPv4 address, or a bracketed IPv6 address. Only accept dotted
817 IpStr
= TargetAddress
;
819 while ((*TargetAddress
!= 0) && (*TargetAddress
!= ':') && (*TargetAddress
!= ',')) {
821 // NULL, ':' or ',' ends the IPv4 string.
826 if (*TargetAddress
== ',') {
828 // Comma and the portal group tag MUST be ommitted if the TargetAddress is sent
829 // as the result of a redirection.
832 } else if (*TargetAddress
== ':') {
833 *TargetAddress
= '\0';
837 Number
= AsciiStrDecimalToUintn (TargetAddress
);
838 if (Number
> 0xFFFF) {
841 Session
->ConfigData
.NvData
.TargetPort
= (UINT16
) Number
;
845 // The string only contains the IPv4 address. Use the well known port.
847 Session
->ConfigData
.NvData
.TargetPort
= ISCSI_WELL_KNOWN_PORT
;
850 // Update the target IP address.
852 Status
= IScsiAsciiStrToIp (IpStr
, &Session
->ConfigData
.NvData
.TargetIp
);
853 if (EFI_ERROR (Status
)) {
860 IScsiFreeKeyValueList (KeyValueList
);
866 The callback function to free the net buffer list.
868 @param[in] Arg The opaque parameter.
875 ASSERT (Arg
!= NULL
);
877 NetbufFreeList ((LIST_ENTRY
*) Arg
);
882 The callback function called in NetBufFree, it does nothing.
884 @param[in] Arg The opaque parameter.
894 Receive an iSCSI response PDU. An iSCSI response PDU contains an iSCSI PDU header and
895 an optional data segment. The two parts will be put into two blocks of buffers in the
896 net buffer. The digest check will be conducted in this function if needed and the digests
897 will be trimmed from the PDU buffer.
899 @param[in] Conn The iSCSI connection to receive data from.
900 @param[out] Pdu The received iSCSI pdu.
901 @param[in] Context The context used to describe information on the caller provided
902 buffer to receive data segment of the iSCSI pdu, it's optional.
903 @param[in] HeaderDigest Whether there will be header digest received.
904 @param[in] DataDigest Whether there will be data digest.
905 @param[in] TimeoutEvent The timeout event, it's optional.
907 @retval EFI_SUCCESS An iSCSI pdu is received.
908 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
909 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
910 @retval Others Other errors as indicated.
914 IN ISCSI_CONNECTION
*Conn
,
916 IN ISCSI_IN_BUFFER_CONTEXT
*Context
, OPTIONAL
917 IN BOOLEAN HeaderDigest
,
918 IN BOOLEAN DataDigest
,
919 IN EFI_EVENT TimeoutEvent OPTIONAL
922 LIST_ENTRY
*NbufList
;
929 NET_FRAGMENT Fragment
[2];
930 UINT32 FragmentCount
;
932 UINT32 PadAndCRC32
[2];
934 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
935 if (NbufList
== NULL
) {
936 return EFI_OUT_OF_RESOURCES
;
939 InitializeListHead (NbufList
);
942 // The header digest will be received together with the PDU header if exists.
944 Len
= sizeof (ISCSI_BASIC_HEADER
) + (HeaderDigest
? sizeof (UINT32
) : 0);
945 PduHdr
= NetbufAlloc (Len
);
946 if (PduHdr
== NULL
) {
948 return EFI_OUT_OF_RESOURCES
;
951 Header
= NetbufAllocSpace (PduHdr
, Len
, NET_BUF_TAIL
);
952 ASSERT (Header
!= NULL
);
953 InsertTailList (NbufList
, &PduHdr
->List
);
956 // First step, receive the BHS of the PDU.
958 Status
= Tcp4IoReceive (&Conn
->Tcp4Io
, PduHdr
, FALSE
, TimeoutEvent
);
959 if (EFI_ERROR (Status
)) {
965 // TODO: check the header-digest.
968 // Trim off the digest.
970 NetbufTrim (PduHdr
, sizeof (UINT32
), NET_BUF_TAIL
);
973 Len
= ISCSI_GET_DATASEG_LEN (Header
);
976 // No data segment.Form the pdu from a list of pdu segments.
978 *Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
980 Status
= EFI_OUT_OF_RESOURCES
;
986 // Get the length of the padding bytes of the data segment.
988 PadLen
= ISCSI_GET_PAD_LEN (Len
);
990 switch (ISCSI_GET_OPCODE (Header
)) {
991 case ISCSI_OPCODE_SCSI_DATA_IN
:
993 // Try to use the buffer described by Context if the PDU is an
994 // iSCSI SCSI data in pdu so as to reduce memory copy overhead.
996 InDataOffset
= ISCSI_GET_BUFFER_OFFSET (Header
);
997 if ((Context
== NULL
) || ((InDataOffset
+ Len
) > Context
->InDataLen
)) {
998 Status
= EFI_PROTOCOL_ERROR
;
1002 Fragment
[0].Len
= Len
;
1003 Fragment
[0].Bulk
= Context
->InData
+ InDataOffset
;
1005 if (DataDigest
|| (PadLen
!= 0)) {
1007 // The data segment is padded, use two fragments to receive it.
1008 // The first to receive the useful data. The second to receive the padding.
1010 Fragment
[1].Len
= PadLen
+ (DataDigest
? sizeof (UINT32
) : 0);
1011 Fragment
[1].Bulk
= (UINT8
*)PadAndCRC32
+ (4 - PadLen
);
1017 DataSeg
= NetbufFromExt (&Fragment
[0], FragmentCount
, 0, 0, IScsiNbufExtFree
, NULL
);
1018 if (DataSeg
== NULL
) {
1019 Status
= EFI_OUT_OF_RESOURCES
;
1025 case ISCSI_OPCODE_SCSI_RSP
:
1026 case ISCSI_OPCODE_NOP_IN
:
1027 case ISCSI_OPCODE_LOGIN_RSP
:
1028 case ISCSI_OPCODE_TEXT_RSP
:
1029 case ISCSI_OPCODE_ASYNC_MSG
:
1030 case ISCSI_OPCODE_REJECT
:
1031 case ISCSI_OPCODE_VENDOR_T0
:
1032 case ISCSI_OPCODE_VENDOR_T1
:
1033 case ISCSI_OPCODE_VENDOR_T2
:
1035 // Allocate buffer to receive the data segment.
1037 Len
+= PadLen
+ (DataDigest
? sizeof (UINT32
) : 0);
1038 DataSeg
= NetbufAlloc (Len
);
1039 if (DataSeg
== NULL
) {
1040 Status
= EFI_OUT_OF_RESOURCES
;
1044 NetbufAllocSpace (DataSeg
, Len
, NET_BUF_TAIL
);
1048 Status
= EFI_PROTOCOL_ERROR
;
1052 InsertTailList (NbufList
, &DataSeg
->List
);
1055 // Receive the data segment with the data digest if any.
1057 Status
= Tcp4IoReceive (&Conn
->Tcp4Io
, DataSeg
, FALSE
, TimeoutEvent
);
1058 if (EFI_ERROR (Status
)) {
1064 // TODO: Check the data digest.
1066 NetbufTrim (DataSeg
, sizeof (UINT32
), NET_BUF_TAIL
);
1071 // Trim off the padding bytes in the data segment.
1073 NetbufTrim (DataSeg
, PadLen
, NET_BUF_TAIL
);
1077 // Form the pdu from a list of pdu segments.
1079 *Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1081 Status
= EFI_OUT_OF_RESOURCES
;
1086 if (EFI_ERROR (Status
)) {
1088 // Free the Nbufs in this NbufList and the NbufList itself.
1090 IScsiFreeNbufList (NbufList
);
1097 Check and get the result of the prameter negotiation.
1099 @param[in, out] Conn The connection in iSCSI login.
1101 @retval EFI_SUCCESS The parmeter check is passed and negotiation is finished.
1102 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened.
1103 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1106 IScsiCheckOpParams (
1107 IN OUT ISCSI_CONNECTION
*Conn
1111 LIST_ENTRY
*KeyValueList
;
1114 ISCSI_SESSION
*Session
;
1118 ASSERT (Conn
->RspQue
.BufNum
!= 0);
1120 Session
= Conn
->Session
;
1122 Len
= Conn
->RspQue
.BufSize
;
1123 Data
= AllocatePool (Len
);
1125 return EFI_OUT_OF_RESOURCES
;
1128 NetbufQueCopy (&Conn
->RspQue
, 0, Len
, (UINT8
*) Data
);
1130 Status
= EFI_PROTOCOL_ERROR
;
1133 // Extract the Key-Value pairs into a list.
1135 KeyValueList
= IScsiBuildKeyValueList (Data
, Len
);
1136 if (KeyValueList
== NULL
) {
1143 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_HEADER_DIGEST
);
1144 if (Value
== NULL
) {
1148 if (AsciiStrCmp (Value
, "CRC32") == 0) {
1149 if (Conn
->HeaderDigest
!= IScsiDigestCRC32
) {
1152 } else if (AsciiStrCmp (Value
, ISCSI_KEY_VALUE_NONE
) == 0) {
1153 Conn
->HeaderDigest
= IScsiDigestNone
;
1160 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_DIGEST
);
1161 if (Value
== NULL
) {
1165 if (AsciiStrCmp (Value
, "CRC32") == 0) {
1166 if (Conn
->DataDigest
!= IScsiDigestCRC32
) {
1169 } else if (AsciiStrCmp (Value
, ISCSI_KEY_VALUE_NONE
) == 0) {
1170 Conn
->DataDigest
= IScsiDigestNone
;
1175 // ErrorRecoveryLevel, result fuction is Minimum.
1177 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_ERROR_RECOVERY_LEVEL
);
1178 if (Value
== NULL
) {
1182 NumericValue
= AsciiStrDecimalToUintn (Value
);
1183 if (NumericValue
> 2) {
1187 Session
->ErrorRecoveryLevel
= (UINT8
) MIN (Session
->ErrorRecoveryLevel
, NumericValue
);
1190 // InitialR2T, result function is OR.
1192 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_INITIAL_R2T
);
1193 if (Value
== NULL
) {
1197 Session
->InitialR2T
= (BOOLEAN
) (Session
->InitialR2T
|| (AsciiStrCmp (Value
, "Yes") == 0));
1200 // ImmediateData, result function is AND.
1202 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_IMMEDIATE_DATA
);
1203 if (Value
== NULL
) {
1207 Session
->ImmediateData
= (BOOLEAN
) (Session
->ImmediateData
&& (AsciiStrCmp (Value
, "Yes") == 0));
1210 // MaxRecvDataSegmentLength is declarative.
1212 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
);
1213 if (Value
!= NULL
) {
1214 Conn
->MaxRecvDataSegmentLength
= (UINT32
) AsciiStrDecimalToUintn (Value
);
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.
1230 // This Key/Value is negotiation type.
1232 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_FIRST_BURST_LENGTH
);
1233 if (Value
== NULL
) {
1237 NumericValue
= AsciiStrDecimalToUintn (Value
);
1238 Session
->FirstBurstLength
= (UINT32
) MIN (Session
->FirstBurstLength
, NumericValue
);
1241 // MaxConnections, result function is Minimum.
1243 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_CONNECTIONS
);
1244 if (Value
== NULL
) {
1248 NumericValue
= AsciiStrDecimalToUintn (Value
);
1249 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1253 Session
->MaxConnections
= (UINT32
) MIN (Session
->MaxConnections
, NumericValue
);
1256 // DataPDUInOrder, result function is OR.
1258 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_PDU_IN_ORDER
);
1259 if (Value
== NULL
) {
1263 Session
->DataPDUInOrder
= (BOOLEAN
) (Session
->DataPDUInOrder
|| (AsciiStrCmp (Value
, "Yes") == 0));
1266 // DataSequenceInorder, result function is OR.
1268 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
);
1269 if (Value
== NULL
) {
1273 Session
->DataSequenceInOrder
= (BOOLEAN
) (Session
->DataSequenceInOrder
|| (AsciiStrCmp (Value
, "Yes") == 0));
1276 // DefaultTime2Wait, result function is Maximum.
1278 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2WAIT
);
1279 if (Value
== NULL
) {
1283 NumericValue
= AsciiStrDecimalToUintn (Value
);
1284 if (NumericValue
== 0) {
1285 Session
->DefaultTime2Wait
= 0;
1286 } else if (NumericValue
> 3600) {
1289 Session
->DefaultTime2Wait
= (UINT32
) MAX (Session
->DefaultTime2Wait
, NumericValue
);
1292 // DefaultTime2Retain, result function is Minimum.
1294 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2RETAIN
);
1295 if (Value
== NULL
) {
1299 NumericValue
= AsciiStrDecimalToUintn (Value
);
1300 if (NumericValue
== 0) {
1301 Session
->DefaultTime2Retain
= 0;
1302 } else if (NumericValue
> 3600) {
1305 Session
->DefaultTime2Retain
= (UINT32
) MIN (Session
->DefaultTime2Retain
, NumericValue
);
1308 // MaxOutstandingR2T, result function is Minimum.
1310 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_OUTSTANDING_R2T
);
1311 if (Value
== NULL
) {
1315 NumericValue
= AsciiStrDecimalToUintn (Value
);
1316 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1320 Session
->MaxOutstandingR2T
= (UINT16
) MIN (Session
->MaxOutstandingR2T
, NumericValue
);
1323 // Remove declarative key-value paris if any.
1325 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_SESSION_TYPE
);
1326 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_ALIAS
);
1327 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_PORTAL_GROUP_TAG
);
1329 if (IsListEmpty (KeyValueList
)) {
1331 // Succeed if no more keys in the list.
1333 Status
= EFI_SUCCESS
;
1338 IScsiFreeKeyValueList (KeyValueList
);
1346 Fill the oprational prameters.
1348 @param[in] Conn The connection in iSCSI login.
1349 @param[in, out] Pdu The iSCSI login request PDU to fill the parameters.
1351 @retval EFI_SUCCESS The parmeters are filled into the iSCSI login request PDU.
1355 IN ISCSI_CONNECTION
*Conn
,
1359 ISCSI_SESSION
*Session
;
1362 Session
= Conn
->Session
;
1364 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->HeaderDigest
== IScsiDigestCRC32
) ? "None,CRC32" : "None");
1365 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_HEADER_DIGEST
, Value
);
1367 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->DataDigest
== IScsiDigestCRC32
) ? "None,CRC32" : "None");
1368 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_DIGEST
, Value
);
1370 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->ErrorRecoveryLevel
);
1371 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_ERROR_RECOVERY_LEVEL
, Value
);
1373 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->InitialR2T
? "Yes" : "No");
1374 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_INITIAL_R2T
, Value
);
1376 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->ImmediateData
? "Yes" : "No");
1377 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_IMMEDIATE_DATA
, Value
);
1379 AsciiSPrint (Value
, sizeof (Value
), "%d", MAX_RECV_DATA_SEG_LEN_IN_FFP
);
1380 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
, Value
);
1382 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxBurstLength
);
1383 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_BURST_LENGTH
, Value
);
1385 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->FirstBurstLength
);
1386 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_FIRST_BURST_LENGTH
, Value
);
1388 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxConnections
);
1389 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_CONNECTIONS
, Value
);
1391 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataPDUInOrder
? "Yes" : "No");
1392 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_PDU_IN_ORDER
, Value
);
1394 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataSequenceInOrder
? "Yes" : "No");
1395 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
, Value
);
1397 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Wait
);
1398 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2WAIT
, Value
);
1400 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Retain
);
1401 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2RETAIN
, Value
);
1403 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxOutstandingR2T
);
1404 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_OUTSTANDING_R2T
, Value
);
1410 Pad the iSCSI AHS or data segment to an integer number of 4 byte words.
1412 @param[in, out] Pdu The iSCSI pdu which contains segments to pad.
1413 @param[in] Len The length of the last semgnet in the PDU.
1415 @retval EFI_SUCCESS The segment is padded or no need to pad it.
1416 @retval EFI_OUT_OF_RESOURCES There is not enough remaining free space to add the
1421 IN OUT NET_BUF
*Pdu
,
1428 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1431 Data
= NetbufAllocSpace (Pdu
, PadLen
, NET_BUF_TAIL
);
1433 return EFI_OUT_OF_RESOURCES
;
1436 ZeroMem (Data
, PadLen
);
1443 Build a key-value list from the data segment.
1445 @param[in] Data The data segment containing the key-value pairs.
1446 @param[in] Len Length of the data segment.
1448 @return The key-value list.
1449 @retval NULL Other errors as indicated.
1452 IScsiBuildKeyValueList (
1457 LIST_ENTRY
*ListHead
;
1458 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1460 ListHead
= AllocatePool (sizeof (LIST_ENTRY
));
1461 if (ListHead
== NULL
) {
1465 InitializeListHead (ListHead
);
1468 KeyValuePair
= AllocatePool (sizeof (ISCSI_KEY_VALUE_PAIR
));
1469 if (KeyValuePair
== NULL
) {
1473 InitializeListHead (&KeyValuePair
->List
);
1475 KeyValuePair
->Key
= Data
;
1477 while ((Len
> 0) && (*Data
!= '=')) {
1488 FreePool (KeyValuePair
);
1492 KeyValuePair
->Value
= Data
;
1494 InsertTailList (ListHead
, &KeyValuePair
->List
);;
1496 Data
+= AsciiStrLen (KeyValuePair
->Value
) + 1;
1497 Len
-= (UINT32
) AsciiStrLen (KeyValuePair
->Value
) + 1;
1504 IScsiFreeKeyValueList (ListHead
);
1510 Get the value string by the key name from the key-value list. If found,
1511 the key-value entry will be removed from the list.
1513 @param[in, out] KeyValueList The key-value list.
1514 @param[in] Key The key name to find.
1516 @return The value string.
1519 IScsiGetValueByKeyFromList (
1520 IN OUT LIST_ENTRY
*KeyValueList
,
1525 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1530 NET_LIST_FOR_EACH (Entry
, KeyValueList
) {
1531 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1533 if (AsciiStrCmp (KeyValuePair
->Key
, Key
) == 0) {
1534 Value
= KeyValuePair
->Value
;
1536 RemoveEntryList (&KeyValuePair
->List
);
1537 FreePool (KeyValuePair
);
1546 Free the key-value list.
1548 @param[in] KeyValueList The key-value list.
1551 IScsiFreeKeyValueList (
1552 IN LIST_ENTRY
*KeyValueList
1556 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1558 while (!IsListEmpty (KeyValueList
)) {
1559 Entry
= NetListRemoveHead (KeyValueList
);
1560 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1562 FreePool (KeyValuePair
);
1565 FreePool (KeyValueList
);
1569 Normalize the iSCSI name according to RFC.
1571 @param[in, out] Name The iSCSI name.
1572 @param[in] Len length of the iSCSI name.
1574 @retval EFI_SUCCESS The iSCSI name is valid and normalized.
1575 @retval EFI_PROTOCOL_ERROR The iSCSI name is mal-formatted or not in the IQN format.
1578 IScsiNormalizeName (
1585 for (Index
= 0; Index
< Len
; Index
++) {
1586 if (NET_IS_UPPER_CASE_CHAR (Name
[Index
])) {
1588 // Convert the upper-case characters to lower-case ones
1590 Name
[Index
] = (CHAR8
) (Name
[Index
] - 'A' + 'a');
1593 if (!NET_IS_LOWER_CASE_CHAR (Name
[Index
]) &&
1594 !NET_IS_DIGIT (Name
[Index
]) &&
1595 (Name
[Index
] != '-') &&
1596 (Name
[Index
] != '.') &&
1597 (Name
[Index
] != ':')
1600 // ASCII dash, dot, colon lower-case characters and digit characters
1603 return EFI_PROTOCOL_ERROR
;
1607 if ((Len
< 4) || (CompareMem (Name
, "iqn.", 4) != 0)) {
1609 // Only IQN format is accepted now.
1611 return EFI_PROTOCOL_ERROR
;
1618 Create an iSCSI task control block.
1620 @param[in] Conn The connection on which the task control block will be created.
1621 @param[out] Tcb The newly created task control block.
1623 @retval EFI_SUCCESS The task control block is created.
1624 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1625 @retval EFI_NOT_READY The target can not accept new commands.
1629 IN ISCSI_CONNECTION
*Conn
,
1633 ISCSI_SESSION
*Session
;
1636 ASSERT (Tcb
!= NULL
);
1638 Session
= Conn
->Session
;
1640 if (ISCSI_SEQ_GT (Session
->CmdSN
, Session
->MaxCmdSN
)) {
1641 return EFI_NOT_READY
;
1644 NewTcb
= AllocateZeroPool (sizeof (ISCSI_TCB
));
1645 if (NewTcb
== NULL
) {
1646 return EFI_OUT_OF_RESOURCES
;
1649 InitializeListHead (&NewTcb
->Link
);
1651 NewTcb
->SoFarInOrder
= TRUE
;
1652 NewTcb
->InitiatorTaskTag
= Session
->InitiatorTaskTag
;
1653 NewTcb
->CmdSN
= Session
->CmdSN
;
1654 NewTcb
->Conn
= Conn
;
1656 InsertTailList (&Session
->TcbList
, &NewTcb
->Link
);
1659 // Advance the initiator task tag.
1661 Session
->InitiatorTaskTag
++;
1670 Delete the tcb from the connection and destroy it.
1672 @param[in] Tcb The tcb to delete.
1679 RemoveEntryList (&Tcb
->Link
);
1685 Find the task control block by the initator task tag.
1687 @param[in] TcbList The tcb list.
1688 @param[in] InitiatorTaskTag The initiator task tag.
1690 @return The task control block found.
1694 IN LIST_ENTRY
*TcbList
,
1695 IN UINT32 InitiatorTaskTag
1703 NET_LIST_FOR_EACH (Entry
, TcbList
) {
1704 Tcb
= NET_LIST_USER_STRUCT (Entry
, ISCSI_TCB
, Link
);
1706 if (Tcb
->InitiatorTaskTag
== InitiatorTaskTag
) {
1717 Create a data segment, pad it and calculate the CRC if needed.
1719 @param[in] Data The data to fill into the data segment.
1720 @param[in] Len Length of the data.
1721 @param[in] DataDigest Whether to calculate CRC for this data segment.
1723 @return The net buffer wrapping the data segment.
1726 IScsiNewDataSegment (
1729 IN BOOLEAN DataDigest
1732 NET_FRAGMENT Fragment
[2];
1733 UINT32 FragmentCount
;
1737 Fragment
[0].Len
= Len
;
1738 Fragment
[0].Bulk
= Data
;
1740 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1742 Fragment
[1].Len
= PadLen
;
1743 Fragment
[1].Bulk
= (UINT8
*) &mDataSegPad
;
1750 DataSeg
= NetbufFromExt (&Fragment
[0], FragmentCount
, 0, 0, IScsiNbufExtFree
, NULL
);
1756 Create a iSCSI SCSI command PDU to encapsulate the command issued
1757 by SCSI through the EXT SCSI PASS THRU Protocol.
1759 @param[in] Packet The EXT SCSI PASS THRU request packet containing the SCSI command.
1760 @param[in] Lun The LUN.
1761 @param[in] Tcb The tcb assocated with this SCSI command.
1763 @return The created iSCSI SCSI command PDU.
1764 @retval NULL Other errors as indicated.
1767 IScsiNewScsiCmdPdu (
1768 IN EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
1773 LIST_ENTRY
*NbufList
;
1777 SCSI_COMMAND
*ScsiCmd
;
1780 ISCSI_ADDITIONAL_HEADER
*Header
;
1781 ISCSI_BI_EXP_READ_DATA_LEN_AHS
*BiExpReadDataLenAHS
;
1782 ISCSI_SESSION
*Session
;
1783 UINT32 ImmediateDataLen
;
1787 if (Packet
->DataDirection
== DataBi
) {
1789 // Bi directional Read/Write command, the bidirectional expected
1790 // read data length AHS is required.
1792 AHSLength
+= sizeof (ISCSI_BI_EXP_READ_DATA_LEN_AHS
);
1795 if (Packet
->CdbLength
> 16) {
1797 // The CDB exceeds 16 bytes, an extended CDB AHS is required.
1799 AHSLength
= (UINT8
) (AHSLength
+ (ISCSI_ROUNDUP (Packet
->CdbLength
- 16) + sizeof (ISCSI_ADDITIONAL_HEADER
)));
1802 Length
= sizeof (SCSI_COMMAND
) + AHSLength
;
1803 PduHeader
= NetbufAlloc (Length
);
1804 if (PduHeader
== NULL
) {
1808 ScsiCmd
= (SCSI_COMMAND
*) NetbufAllocSpace (PduHeader
, Length
, NET_BUF_TAIL
);
1809 if (ScsiCmd
== NULL
) {
1810 NetbufFree (PduHeader
);
1813 Header
= (ISCSI_ADDITIONAL_HEADER
*) (ScsiCmd
+ 1);
1815 ZeroMem (ScsiCmd
, Length
);
1817 ISCSI_SET_OPCODE (ScsiCmd
, ISCSI_OPCODE_SCSI_CMD
, 0);
1818 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_TASK_ATTR_SIMPLE
);
1821 // Set the READ/WRITE flags according to the IO type of this request.
1823 switch (Packet
->DataDirection
) {
1825 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
);
1826 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->InTransferLength
);
1830 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_WRITE
);
1831 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
1835 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
| SCSI_CMD_PDU_FLAG_WRITE
);
1836 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
1839 // Fill the bidirectional expected read data length AHS.
1841 BiExpReadDataLenAHS
= (ISCSI_BI_EXP_READ_DATA_LEN_AHS
*) Header
;
1842 Header
= (ISCSI_ADDITIONAL_HEADER
*) (BiExpReadDataLenAHS
+ 1);
1844 BiExpReadDataLenAHS
->Length
= NTOHS (5);
1845 BiExpReadDataLenAHS
->Type
= ISCSI_AHS_TYPE_BI_EXP_READ_DATA_LEN
;
1846 BiExpReadDataLenAHS
->ExpReadDataLength
= NTOHL (Packet
->InTransferLength
);
1851 ScsiCmd
->TotalAHSLength
= AHSLength
;
1852 CopyMem (ScsiCmd
->Lun
, &Lun
, sizeof (ScsiCmd
->Lun
));
1853 ScsiCmd
->InitiatorTaskTag
= NTOHL (Tcb
->InitiatorTaskTag
);
1854 ScsiCmd
->CmdSN
= NTOHL (Tcb
->CmdSN
);
1855 ScsiCmd
->ExpStatSN
= NTOHL (Tcb
->Conn
->ExpStatSN
);
1857 CopyMem (ScsiCmd
->Cdb
, Packet
->Cdb
, sizeof (ScsiCmd
->Cdb
));
1859 if (Packet
->CdbLength
> 16) {
1860 Header
->Length
= NTOHS (Packet
->CdbLength
- 15);
1861 Header
->Type
= ISCSI_AHS_TYPE_EXT_CDB
;
1863 CopyMem (Header
+ 1, (UINT8
*) Packet
->Cdb
+ 16, Packet
->CdbLength
- 16);
1867 Session
= Tcb
->Conn
->Session
;
1868 ImmediateDataLen
= 0;
1870 if (Session
->ImmediateData
&& (Packet
->OutTransferLength
!= 0)) {
1872 // Send immediate data in this SCSI Command PDU. The length of the immeidate
1873 // data is the minimum of FirstBurstLength, the data length to be xfered and
1874 // the MaxRecvdataSegmentLength on this connection.
1876 ImmediateDataLen
= MIN (Session
->FirstBurstLength
, Packet
->OutTransferLength
);
1877 ImmediateDataLen
= MIN (ImmediateDataLen
, Tcb
->Conn
->MaxRecvDataSegmentLength
);
1880 // Update the data segment length in the PDU header.
1882 ISCSI_SET_DATASEG_LEN (ScsiCmd
, ImmediateDataLen
);
1885 // Create the data segment.
1887 DataSeg
= IScsiNewDataSegment ((UINT8
*) Packet
->OutDataBuffer
, ImmediateDataLen
, FALSE
);
1888 if (DataSeg
== NULL
) {
1889 NetbufFree (PduHeader
);
1894 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
1895 if (NbufList
== NULL
) {
1896 NetbufFree (PduHeader
);
1897 NetbufFree (DataSeg
);
1903 InitializeListHead (NbufList
);
1904 InsertTailList (NbufList
, &PduHeader
->List
);
1905 InsertTailList (NbufList
, &DataSeg
->List
);
1907 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1909 IScsiFreeNbufList (NbufList
);
1913 if (Session
->InitialR2T
||
1914 (ImmediateDataLen
== Session
->FirstBurstLength
) ||
1915 (ImmediateDataLen
== Packet
->OutTransferLength
)
1918 // Unsolicited data out sequence is not allowed,
1919 // or FirstBurstLength data is already sent out by immediate data
1920 // or all the OUT data accompany this SCSI packet is sent as
1921 // immediate data, the final flag should be set on this SCSI Command
1924 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_BHS_FLAG_FINAL
);
1933 Create a new iSCSI SCSI Data Out PDU.
1935 @param[in] Data The data to put into the Data Out PDU.
1936 @param[in] Len Length of the data.
1937 @param[in] DataSN The DataSN of the Data Out PDU.
1938 @param[in] Tcb The task control block of this Data Out PDU.
1939 @param[in] Lun The LUN.
1941 @return The net buffer wrapping the Data Out PDU.
1942 @retval NULL Other errors as indicated.
1945 IScsiNewDataOutPdu (
1953 LIST_ENTRY
*NbufList
;
1957 ISCSI_SCSI_DATA_OUT
*DataOutHdr
;
1958 ISCSI_XFER_CONTEXT
*XferContext
;
1960 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
1961 if (NbufList
== NULL
) {
1965 InitializeListHead (NbufList
);
1968 // Allocate memory for the BHS.
1970 PduHdr
= NetbufAlloc (sizeof (ISCSI_SCSI_DATA_OUT
));
1971 if (PduHdr
== NULL
) {
1972 FreePool (NbufList
);
1976 // Insert the BHS into the buffer list.
1978 InsertTailList (NbufList
, &PduHdr
->List
);
1980 DataOutHdr
= (ISCSI_SCSI_DATA_OUT
*) NetbufAllocSpace (PduHdr
, sizeof (ISCSI_SCSI_DATA_OUT
), NET_BUF_TAIL
);
1981 ASSERT (DataOutHdr
!= NULL
);
1982 XferContext
= &Tcb
->XferContext
;
1984 ZeroMem (DataOutHdr
, sizeof (ISCSI_SCSI_DATA_OUT
));
1987 // Set the flags and fields of the Data Out PDU BHS.
1989 ISCSI_SET_OPCODE (DataOutHdr
, ISCSI_OPCODE_SCSI_DATA_OUT
, 0);
1990 ISCSI_SET_DATASEG_LEN (DataOutHdr
, Len
);
1992 DataOutHdr
->InitiatorTaskTag
= HTONL (Tcb
->InitiatorTaskTag
);
1993 DataOutHdr
->TargetTransferTag
= HTONL (XferContext
->TargetTransferTag
);
1994 DataOutHdr
->ExpStatSN
= HTONL (Tcb
->Conn
->ExpStatSN
);
1995 DataOutHdr
->DataSN
= HTONL (DataSN
);
1996 DataOutHdr
->BufferOffset
= HTONL (XferContext
->Offset
);
1998 if (XferContext
->TargetTransferTag
!= ISCSI_RESERVED_TAG
) {
1999 CopyMem (&DataOutHdr
->Lun
, &Lun
, sizeof (DataOutHdr
->Lun
));
2002 // Build the data segment for this Data Out PDU.
2004 DataSeg
= IScsiNewDataSegment (Data
, Len
, FALSE
);
2005 if (DataSeg
== NULL
) {
2006 IScsiFreeNbufList (NbufList
);
2010 // Put the data segment into the buffer list and combine it with the BHS
2011 // into a full Data Out PDU.
2013 InsertTailList (NbufList
, &DataSeg
->List
);
2014 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
2016 IScsiFreeNbufList (NbufList
);
2023 Generate a consecutive sequence of iSCSI SCSI Data Out PDUs.
2025 @param[in] Data The data which will be carried by the sequence of iSCSI SCSI Data Out PDUs.
2026 @param[in] Tcb The task control block of the data to send out.
2027 @param[in] Lun The LUN the data will be sent to.
2029 @return A list of net buffers with each of them wraps an iSCSI SCSI Data Out PDU.
2030 @retval NULL Other errors as indicated.
2033 IScsiGenerateDataOutPduSequence (
2039 LIST_ENTRY
*PduList
;
2042 NET_BUF
*DataOutPdu
;
2043 ISCSI_CONNECTION
*Conn
;
2044 ISCSI_XFER_CONTEXT
*XferContext
;
2046 PduList
= AllocatePool (sizeof (LIST_ENTRY
));
2047 if (PduList
== NULL
) {
2051 InitializeListHead (PduList
);
2056 XferContext
= &Tcb
->XferContext
;
2058 while (XferContext
->DesiredLength
> 0) {
2060 // Determine the length of data this Data Out PDU can carry.
2062 DataLen
= MIN (XferContext
->DesiredLength
, Conn
->MaxRecvDataSegmentLength
);
2065 // Create a Data Out PDU.
2067 DataOutPdu
= IScsiNewDataOutPdu (Data
, DataLen
, DataSN
, Tcb
, Lun
);
2068 if (DataOutPdu
== NULL
) {
2069 IScsiFreeNbufList (PduList
);
2075 InsertTailList (PduList
, &DataOutPdu
->List
);
2078 // Update the context and DataSN.
2080 XferContext
->Offset
+= DataLen
;
2081 XferContext
->DesiredLength
-= DataLen
;
2086 // Set the F bit for the last data out PDU in this sequence.
2088 ISCSI_SET_FLAG (NetbufGetByte (DataOutPdu
, 0, NULL
), ISCSI_BHS_FLAG_FINAL
);
2096 Send the Data in a sequence of Data Out PDUs one by one.
2098 @param[in] Data The data to carry by Data Out PDUs.
2099 @param[in] Lun The LUN the data will be sent to.
2100 @param[in] Tcb The task control block.
2102 @retval EFI_SUCCES The data is sent out to the LUN.
2103 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2104 @retval Others Other errors as indicated.
2107 IScsiSendDataOutPduSequence (
2113 LIST_ENTRY
*DataOutPduList
;
2119 // Generate the Data Out PDU sequence.
2121 DataOutPduList
= IScsiGenerateDataOutPduSequence (Data
, Tcb
, Lun
);
2122 if (DataOutPduList
== NULL
) {
2123 return EFI_OUT_OF_RESOURCES
;
2126 Status
= EFI_SUCCESS
;
2129 // Send the Data Out PDU's one by one.
2131 NET_LIST_FOR_EACH (Entry
, DataOutPduList
) {
2132 Pdu
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
2134 Status
= Tcp4IoTransmit (&Tcb
->Conn
->Tcp4Io
, Pdu
);
2135 if (EFI_ERROR (Status
)) {
2140 IScsiFreeNbufList (DataOutPduList
);
2146 Process the received iSCSI SCSI Data In PDU.
2148 @param[in] Pdu The Data In PDU received.
2149 @param[in] Tcb The task control block.
2150 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2152 @retval EFI_SUCCES The check on the Data IN PDU is passed and some update
2154 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2155 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2156 @retval Others Other errors as indicated.
2162 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2165 ISCSI_SCSI_DATA_IN
*DataInHdr
;
2168 DataInHdr
= (ISCSI_SCSI_DATA_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2170 DataInHdr
->InitiatorTaskTag
= NTOHL (DataInHdr
->InitiatorTaskTag
);
2171 DataInHdr
->ExpCmdSN
= NTOHL (DataInHdr
->ExpCmdSN
);
2172 DataInHdr
->MaxCmdSN
= NTOHL (DataInHdr
->MaxCmdSN
);
2173 DataInHdr
->DataSN
= NTOHL (DataInHdr
->DataSN
);
2176 // Check the DataSN.
2178 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, DataInHdr
->DataSN
);
2179 if (EFI_ERROR (Status
)) {
2183 if (DataInHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2184 return EFI_PROTOCOL_ERROR
;
2187 // Update the command related sequence numbers.
2189 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, DataInHdr
->MaxCmdSN
, DataInHdr
->ExpCmdSN
);
2191 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_STATUS_VALID
)) {
2192 if (!ISCSI_FLAG_ON (DataInHdr
, ISCSI_BHS_FLAG_FINAL
)) {
2194 // The S bit is on but the F bit is off.
2196 return EFI_PROTOCOL_ERROR
;
2199 Tcb
->StatusXferd
= TRUE
;
2201 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_OVERFLOW
| SCSI_DATA_IN_PDU_FLAG_UNDERFLOW
)) {
2203 // Underflow and Overflow are mutual flags.
2205 return EFI_PROTOCOL_ERROR
;
2208 // S bit is on, the StatSN is valid.
2210 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NTOHL (DataInHdr
->StatSN
));
2211 if (EFI_ERROR (Status
)) {
2215 Packet
->HostAdapterStatus
= 0;
2216 Packet
->TargetStatus
= DataInHdr
->Status
;
2218 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2219 Packet
->InTransferLength
+= NTOHL (DataInHdr
->ResidualCount
);
2220 Status
= EFI_BAD_BUFFER_SIZE
;
2223 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2224 Packet
->InTransferLength
-= NTOHL (DataInHdr
->ResidualCount
);
2232 Process the received iSCSI R2T PDU.
2234 @param[in] Pdu The R2T PDU received.
2235 @param[in] Tcb The task control block.
2236 @param[in] Lun The Lun.
2237 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2239 @retval EFI_SUCCES The R2T PDU is valid and the solicited data is sent out.
2240 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2241 @retval Others Other errors as indicated.
2248 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2251 ISCSI_READY_TO_TRANSFER
*R2THdr
;
2253 ISCSI_XFER_CONTEXT
*XferContext
;
2256 R2THdr
= (ISCSI_READY_TO_TRANSFER
*) NetbufGetByte (Pdu
, 0, NULL
);
2258 R2THdr
->InitiatorTaskTag
= NTOHL (R2THdr
->InitiatorTaskTag
);
2259 R2THdr
->TargetTransferTag
= NTOHL (R2THdr
->TargetTransferTag
);
2260 R2THdr
->StatSN
= NTOHL (R2THdr
->StatSN
);
2261 R2THdr
->R2TSeqNum
= NTOHL (R2THdr
->R2TSeqNum
);
2262 R2THdr
->BufferOffset
= NTOHL (R2THdr
->BufferOffset
);
2263 R2THdr
->DesiredDataTransferLength
= NTOHL (R2THdr
->DesiredDataTransferLength
);
2265 if ((R2THdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) || !ISCSI_SEQ_EQ (R2THdr
->StatSN
, Tcb
->Conn
->ExpStatSN
)) {
2266 return EFI_PROTOCOL_ERROR
;;
2269 // Check the sequence number.
2271 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, R2THdr
->R2TSeqNum
);
2272 if (EFI_ERROR (Status
)) {
2276 XferContext
= &Tcb
->XferContext
;
2277 XferContext
->TargetTransferTag
= R2THdr
->TargetTransferTag
;
2278 XferContext
->Offset
= R2THdr
->BufferOffset
;
2279 XferContext
->DesiredLength
= R2THdr
->DesiredDataTransferLength
;
2281 if (((XferContext
->Offset
+ XferContext
->DesiredLength
) > Packet
->OutTransferLength
) ||
2282 (XferContext
->DesiredLength
> Tcb
->Conn
->Session
->MaxBurstLength
)
2284 return EFI_PROTOCOL_ERROR
;
2287 // Send the data solicited by this R2T.
2289 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2290 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2296 Process the received iSCSI SCSI Response PDU.
2298 @param[in] Pdu The Response PDU received.
2299 @param[in] Tcb The task control block.
2300 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2302 @retval EFI_SUCCES The Response PDU is processed.
2303 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2304 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2305 @retval Others Other errors as indicated.
2308 IScsiOnScsiRspRcvd (
2311 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2314 SCSI_RESPONSE
*ScsiRspHdr
;
2315 ISCSI_SENSE_DATA
*SenseData
;
2319 ScsiRspHdr
= (SCSI_RESPONSE
*) NetbufGetByte (Pdu
, 0, NULL
);
2321 ScsiRspHdr
->InitiatorTaskTag
= NTOHL (ScsiRspHdr
->InitiatorTaskTag
);
2322 if (ScsiRspHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2323 return EFI_PROTOCOL_ERROR
;
2326 ScsiRspHdr
->StatSN
= NTOHL (ScsiRspHdr
->StatSN
);
2328 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, ScsiRspHdr
->StatSN
);
2329 if (EFI_ERROR (Status
)) {
2333 ScsiRspHdr
->MaxCmdSN
= NTOHL (ScsiRspHdr
->MaxCmdSN
);
2334 ScsiRspHdr
->ExpCmdSN
= NTOHL (ScsiRspHdr
->ExpCmdSN
);
2335 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, ScsiRspHdr
->MaxCmdSN
, ScsiRspHdr
->ExpCmdSN
);
2337 Tcb
->StatusXferd
= TRUE
;
2339 Packet
->HostAdapterStatus
= ScsiRspHdr
->Response
;
2340 if (Packet
->HostAdapterStatus
!= ISCSI_SERVICE_RSP_COMMAND_COMPLETE_AT_TARGET
) {
2344 Packet
->TargetStatus
= ScsiRspHdr
->Status
;
2346 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
| SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
) ||
2347 ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
| SCSI_RSP_PDU_FLAG_UNDERFLOW
)
2349 return EFI_PROTOCOL_ERROR
;
2352 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
)) {
2353 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2354 Status
= EFI_BAD_BUFFER_SIZE
;
2357 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
)) {
2358 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2361 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2362 if (Packet
->DataDirection
== DataIn
) {
2363 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2365 Packet
->OutTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2368 Status
= EFI_BAD_BUFFER_SIZE
;
2371 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2372 if (Packet
->DataDirection
== DataIn
) {
2373 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2375 Packet
->OutTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2379 DataSegLen
= ISCSI_GET_DATASEG_LEN (ScsiRspHdr
);
2380 if (DataSegLen
!= 0) {
2381 SenseData
= (ISCSI_SENSE_DATA
*) NetbufGetByte (Pdu
, sizeof (SCSI_RESPONSE
), NULL
);
2383 SenseData
->Length
= NTOHS (SenseData
->Length
);
2385 Packet
->SenseDataLength
= (UINT8
) MIN (SenseData
->Length
, Packet
->SenseDataLength
);
2386 if (Packet
->SenseDataLength
!= 0) {
2387 CopyMem (Packet
->SenseData
, &SenseData
->Data
[0], Packet
->SenseDataLength
);
2390 Packet
->SenseDataLength
= 0;
2397 Process the received NOP In PDU.
2399 @param[in] Pdu The NOP In PDU received.
2400 @param[in] Tcb The task control block.
2402 @retval EFI_SUCCES The NOP In PDU is processed and the related sequence
2403 numbers are updated.
2404 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened.
2412 ISCSI_NOP_IN
*NopInHdr
;
2415 NopInHdr
= (ISCSI_NOP_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2417 NopInHdr
->StatSN
= NTOHL (NopInHdr
->StatSN
);
2418 NopInHdr
->ExpCmdSN
= NTOHL (NopInHdr
->ExpCmdSN
);
2419 NopInHdr
->MaxCmdSN
= NTOHL (NopInHdr
->MaxCmdSN
);
2421 if (NopInHdr
->InitiatorTaskTag
== ISCSI_RESERVED_TAG
) {
2422 if (NopInHdr
->StatSN
!= Tcb
->Conn
->ExpStatSN
) {
2423 return EFI_PROTOCOL_ERROR
;
2426 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NopInHdr
->StatSN
);
2427 if (EFI_ERROR (Status
)) {
2432 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, NopInHdr
->MaxCmdSN
, NopInHdr
->ExpCmdSN
);
2438 Execute the SCSI command issued through the EXT SCSI PASS THRU protocol.
2440 @param[in] PassThru The EXT SCSI PASS THRU protocol.
2441 @param[in] Target The target ID.
2442 @param[in] Lun The LUN.
2443 @param[in, out] Packet The request packet containing IO request, SCSI command
2444 buffer and buffers to read/write.
2446 @retval EFI_SUCCES The SCSI command is executed and the result is updated to
2448 @retval EFI_DEVICE_ERROR Session state was not as required.
2449 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2450 @retval Others Other errors as indicated.
2453 IScsiExecuteScsiCommand (
2454 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*PassThru
,
2457 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2461 ISCSI_DRIVER_DATA
*Private
;
2462 ISCSI_SESSION
*Session
;
2463 EFI_EVENT TimeoutEvent
;
2464 ISCSI_CONNECTION
*Conn
;
2467 ISCSI_XFER_CONTEXT
*XferContext
;
2469 ISCSI_IN_BUFFER_CONTEXT InBufferContext
;
2473 Private
= ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru
);
2474 Session
= &Private
->Session
;
2475 Status
= EFI_SUCCESS
;
2477 TimeoutEvent
= NULL
;
2480 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
2481 return EFI_DEVICE_ERROR
;
2484 Conn
= NET_LIST_USER_STRUCT_S (
2485 Session
->Conns
.ForwardLink
,
2488 ISCSI_CONNECTION_SIGNATURE
2491 if (Packet
->Timeout
!= 0) {
2492 Timeout
= MultU64x32 (Packet
->Timeout
, 2);
2495 Status
= IScsiNewTcb (Conn
, &Tcb
);
2496 if (EFI_ERROR (Status
)) {
2500 // Encapsulate the SCSI request packet into an iSCSI SCSI Command PDU.
2502 Pdu
= IScsiNewScsiCmdPdu (Packet
, Lun
, Tcb
);
2504 Status
= EFI_OUT_OF_RESOURCES
;
2508 XferContext
= &Tcb
->XferContext
;
2509 Buffer
= NetbufGetByte (Pdu
, 0, NULL
);
2510 XferContext
->Offset
= ISCSI_GET_DATASEG_LEN (Buffer
);
2513 // Transmit the SCSI Command PDU.
2515 Status
= Tcp4IoTransmit (&Conn
->Tcp4Io
, Pdu
);
2519 if (EFI_ERROR (Status
)) {
2523 if (!Session
->InitialR2T
&&
2524 (XferContext
->Offset
< Session
->FirstBurstLength
) &&
2525 (XferContext
->Offset
< Packet
->OutTransferLength
)
2528 // Unsolicited Data-Out sequence is allowed, there is remaining SCSI
2529 // OUT data and the limit of FirstBurstLength is not reached.
2531 XferContext
->TargetTransferTag
= ISCSI_RESERVED_TAG
;
2532 XferContext
->DesiredLength
= MIN (
2533 Session
->FirstBurstLength
,
2534 Packet
->OutTransferLength
- XferContext
->Offset
2537 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2538 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2539 if (EFI_ERROR (Status
)) {
2544 InBufferContext
.InData
= (UINT8
*) Packet
->InDataBuffer
;
2545 InBufferContext
.InDataLen
= Packet
->InTransferLength
;
2547 while (!Tcb
->StatusXferd
) {
2549 // Start the timeout timer.
2552 Status
= gBS
->SetTimer (Conn
->TimeoutEvent
, TimerRelative
, Timeout
);
2553 if (EFI_ERROR (Status
)) {
2556 TimeoutEvent
= Conn
->TimeoutEvent
;
2559 // try to receive PDU from target.
2561 Status
= IScsiReceivePdu (Conn
, &Pdu
, &InBufferContext
, FALSE
, FALSE
, TimeoutEvent
);
2562 if (EFI_ERROR (Status
)) {
2566 switch (ISCSI_GET_OPCODE (NetbufGetByte (Pdu
, 0, NULL
))) {
2567 case ISCSI_OPCODE_SCSI_DATA_IN
:
2568 Status
= IScsiOnDataInRcvd (Pdu
, Tcb
, Packet
);
2571 case ISCSI_OPCODE_R2T
:
2572 Status
= IScsiOnR2TRcvd (Pdu
, Tcb
, Lun
, Packet
);
2575 case ISCSI_OPCODE_SCSI_RSP
:
2576 Status
= IScsiOnScsiRspRcvd (Pdu
, Tcb
, Packet
);
2579 case ISCSI_OPCODE_NOP_IN
:
2580 Status
= IScsiOnNopInRcvd (Pdu
, Tcb
);
2583 case ISCSI_OPCODE_VENDOR_T0
:
2584 case ISCSI_OPCODE_VENDOR_T1
:
2585 case ISCSI_OPCODE_VENDOR_T2
:
2587 // These messages are vendor specific, skip them.
2592 Status
= EFI_PROTOCOL_ERROR
;
2598 if (EFI_ERROR (Status
)) {
2605 if (TimeoutEvent
!= NULL
) {
2606 gBS
->SetTimer (TimeoutEvent
, TimerCancel
, 0);
2613 if ((Status
!= EFI_SUCCESS
) && (Status
!= EFI_NOT_READY
)) {
2615 // Reinstate the session.
2617 if (EFI_ERROR (IScsiSessionReinstatement (Private
))) {
2618 Status
= EFI_DEVICE_ERROR
;
2626 Reinstate the session on some error.
2628 @param[in, out] Private The iSCSI driver data.
2630 @retval EFI_SUCCES The session is reinstated from some error.
2631 @retval Other Reinstatement failed.
2634 IScsiSessionReinstatement (
2635 IN OUT ISCSI_DRIVER_DATA
*Private
2638 ISCSI_SESSION
*Session
;
2641 Session
= &Private
->Session
;
2642 ASSERT (Session
->State
== SESSION_STATE_LOGGED_IN
);
2645 // Abort the session and re-init it.
2647 IScsiSessionAbort (Session
);
2648 IScsiSessionInit (Session
, TRUE
);
2653 Status
= IScsiSessionLogin (Private
);
2659 Initialize some session parameters before login.
2661 @param[in, out] Session The iSCSI session.
2662 @param[in] Recovery Whether the request is from a fresh new start or recovery.
2666 IN OUT ISCSI_SESSION
*Session
,
2673 Session
->Signature
= ISCSI_SESSION_SIGNATURE
;
2674 Session
->State
= SESSION_STATE_FREE
;
2676 Random
= NET_RANDOM (NetRandomInitSeed ());
2678 Session
->Isid
[0] = ISID_BYTE_0
;
2679 Session
->Isid
[1] = ISID_BYTE_1
;
2680 Session
->Isid
[2] = ISID_BYTE_2
;
2681 Session
->Isid
[3] = ISID_BYTE_3
;
2682 Session
->Isid
[4] = (UINT8
) Random
;
2683 Session
->Isid
[5] = (UINT8
) (Random
>> 8);
2685 InitializeListHead (&Session
->Conns
);
2686 InitializeListHead (&Session
->TcbList
);
2692 Session
->InitiatorTaskTag
= 1;
2693 Session
->NextCid
= 1;
2695 Session
->TargetPortalGroupTag
= 0;
2696 Session
->MaxConnections
= ISCSI_MAX_CONNS_PER_SESSION
;
2697 Session
->InitialR2T
= FALSE
;
2698 Session
->ImmediateData
= TRUE
;
2699 Session
->MaxBurstLength
= 262144;
2700 Session
->FirstBurstLength
= MAX_RECV_DATA_SEG_LEN_IN_FFP
;
2701 Session
->DefaultTime2Wait
= 2;
2702 Session
->DefaultTime2Retain
= 20;
2703 Session
->MaxOutstandingR2T
= DEFAULT_MAX_OUTSTANDING_R2T
;
2704 Session
->DataPDUInOrder
= TRUE
;
2705 Session
->DataSequenceInOrder
= TRUE
;
2706 Session
->ErrorRecoveryLevel
= 0;
2710 Abort the iSCSI session, that is, reset all the connection and free the
2713 @param[in, out] Session The iSCSI session.
2715 @retval EFI_SUCCES The session is aborted.
2719 IN OUT ISCSI_SESSION
*Session
2722 ISCSI_DRIVER_DATA
*Private
;
2723 ISCSI_CONNECTION
*Conn
;
2725 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
2729 ASSERT (!IsListEmpty (&Session
->Conns
));
2731 Private
= ISCSI_DRIVER_DATA_FROM_SESSION (Session
);
2733 while (!IsListEmpty (&Session
->Conns
)) {
2734 Conn
= NET_LIST_USER_STRUCT_S (
2735 Session
->Conns
.ForwardLink
,
2738 ISCSI_CONNECTION_SIGNATURE
2741 gBS
->CloseProtocol (
2742 Conn
->Tcp4Io
.Handle
,
2743 &gEfiTcp4ProtocolGuid
,
2745 Private
->ExtScsiPassThruHandle
2748 IScsiConnReset (Conn
);
2750 IScsiDetatchConnection (Conn
);
2751 IScsiDestroyConnection (Conn
);
2754 Session
->State
= SESSION_STATE_FAILED
;