2 The implementation of iSCSI protocol based on RFC3720.
4 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 UINT32 mDataSegPad
= 0;
14 Attach the iSCSI connection to the iSCSI session.
16 @param[in, out] Session The iSCSI session.
17 @param[in, out] Conn The iSCSI connection.
21 IScsiAttatchConnection (
22 IN OUT ISCSI_SESSION
*Session
,
23 IN OUT ISCSI_CONNECTION
*Conn
26 InsertTailList (&Session
->Conns
, &Conn
->Link
);
27 Conn
->Session
= Session
;
32 Detach the iSCSI connection from the session it belongs to.
34 @param[in, out] Conn The iSCSI connection.
38 IScsiDetatchConnection (
39 IN OUT ISCSI_CONNECTION
*Conn
42 RemoveEntryList (&Conn
->Link
);
43 Conn
->Session
->NumConns
--;
49 Check the sequence number according to RFC3720.
51 @param[in, out] ExpSN The currently expected sequence number.
52 @param[in] NewSN The sequence number to check.
54 @retval EFI_SUCCESS The check passed and the ExpSN is increased.
55 @retval EFI_NOT_READY Response was sent due to a retransmission request.
56 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred.
65 if (!ISCSI_SEQ_EQ (NewSN
, *ExpSN
)) {
66 if (ISCSI_SEQ_LT (NewSN
, *ExpSN
)) {
72 return EFI_PROTOCOL_ERROR
;
85 Update the sequence numbers for the iSCSI command.
87 @param[in, out] Session The iSCSI session.
88 @param[in] MaxCmdSN Maximum CmdSN from the target.
89 @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
;
114 This function does the iSCSI connection login.
116 @param[in, out] Conn The iSCSI connection to login.
117 @param Timeout The timeout value in millisecond.
119 @retval EFI_SUCCESS The iSCSI connection is logged into the iSCSI target.
120 @retval EFI_TIMEOUT Timeout occurred during the login procedure.
121 @retval Others Other errors as indicated.
126 IN OUT ISCSI_CONNECTION
*Conn
,
133 // Start the timer, and wait Timeout seconds to establish the TCP connection.
135 Status
= gBS
->SetTimer (
138 MultU64x32 (Timeout
, TICKS_PER_MS
)
140 if (EFI_ERROR (Status
)) {
145 // Try to establish the tcp connection.
147 Status
= TcpIoConnect (&Conn
->TcpIo
, Conn
->TimeoutEvent
);
148 gBS
->SetTimer (Conn
->TimeoutEvent
, TimerCancel
, 0);
150 if (EFI_ERROR (Status
)) {
154 Conn
->State
= CONN_STATE_IN_LOGIN
;
157 // Connection is established, start the iSCSI Login.
160 Status
= IScsiSendLoginReq (Conn
);
161 if (EFI_ERROR (Status
)) {
165 Status
= IScsiReceiveLoginRsp (Conn
);
166 if (EFI_ERROR (Status
)) {
169 } while (Conn
->CurrentStage
!= ISCSI_FULL_FEATURE_PHASE
);
176 Reset the iSCSI connection.
178 @param[in, out] Conn The iSCSI connection to reset.
183 IN OUT ISCSI_CONNECTION
*Conn
186 TcpIoReset (&Conn
->TcpIo
);
191 Create a TCP connection for the iSCSI session.
193 @param[in] Session Points to the iSCSI session.
195 @return The newly created iSCSI connection.
199 IScsiCreateConnection (
200 IN ISCSI_SESSION
*Session
203 ISCSI_DRIVER_DATA
*Private
;
204 ISCSI_SESSION_CONFIG_NVDATA
*NvData
;
205 ISCSI_CONNECTION
*Conn
;
206 TCP_IO_CONFIG_DATA TcpIoConfig
;
207 TCP4_IO_CONFIG_DATA
*Tcp4IoConfig
;
208 TCP6_IO_CONFIG_DATA
*Tcp6IoConfig
;
211 Private
= Session
->Private
;
212 NvData
= &Session
->ConfigData
->SessionConfigData
;
214 Conn
= AllocateZeroPool (sizeof (ISCSI_CONNECTION
));
219 Conn
->Signature
= ISCSI_CONNECTION_SIGNATURE
;
220 Conn
->State
= CONN_STATE_FREE
;
221 Conn
->CurrentStage
= ISCSI_SECURITY_NEGOTIATION
;
222 Conn
->NextStage
= ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
;
223 Conn
->AuthStep
= ISCSI_AUTH_INITIAL
;
225 Conn
->PartialReqSent
= FALSE
;
226 Conn
->PartialRspRcvd
= FALSE
;
227 Conn
->ParamNegotiated
= FALSE
;
228 Conn
->Cid
= Session
->NextCid
++;
229 Conn
->Ipv6Flag
= NvData
->IpMode
== IP_MODE_IP6
|| Session
->ConfigData
->AutoConfigureMode
== IP_MODE_AUTOCONFIG_IP6
;
231 Status
= gBS
->CreateEvent (
238 if (EFI_ERROR (Status
)) {
243 NetbufQueInit (&Conn
->RspQue
);
246 // Set the default connection-only parameters.
248 Conn
->MaxRecvDataSegmentLength
= DEFAULT_MAX_RECV_DATA_SEG_LEN
;
249 Conn
->HeaderDigest
= IScsiDigestNone
;
250 Conn
->DataDigest
= IScsiDigestNone
;
252 if (NvData
->DnsMode
) {
254 // perform dns process if target address expressed by domain name.
256 if (!Conn
->Ipv6Flag
) {
257 Status
= IScsiDns4 (Private
->Image
, Private
->Controller
, NvData
);
259 Status
= IScsiDns6 (Private
->Image
, Private
->Controller
, NvData
);
262 if (EFI_ERROR(Status
)) {
263 DEBUG ((EFI_D_ERROR
, "The configuration of Target address or DNS server address is invalid!\n"));
269 if (!Conn
->Ipv6Flag
) {
270 Tcp4IoConfig
= &TcpIoConfig
.Tcp4IoConfigData
;
272 CopyMem (&Tcp4IoConfig
->LocalIp
, &NvData
->LocalIp
, sizeof (EFI_IPv4_ADDRESS
));
273 CopyMem (&Tcp4IoConfig
->SubnetMask
, &NvData
->SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
274 CopyMem (&Tcp4IoConfig
->Gateway
, &NvData
->Gateway
, sizeof (EFI_IPv4_ADDRESS
));
275 CopyMem (&Tcp4IoConfig
->RemoteIp
, &NvData
->TargetIp
, sizeof (EFI_IPv4_ADDRESS
));
277 Tcp4IoConfig
->RemotePort
= NvData
->TargetPort
;
278 Tcp4IoConfig
->ActiveFlag
= TRUE
;
279 Tcp4IoConfig
->StationPort
= 0;
281 Tcp6IoConfig
= &TcpIoConfig
.Tcp6IoConfigData
;
283 CopyMem (&Tcp6IoConfig
->RemoteIp
, &NvData
->TargetIp
, sizeof (EFI_IPv6_ADDRESS
));
284 Tcp6IoConfig
->RemotePort
= NvData
->TargetPort
;
285 Tcp6IoConfig
->ActiveFlag
= TRUE
;
286 Tcp6IoConfig
->StationPort
= 0;
290 // Create the TCP IO for this connection.
292 Status
= TcpIoCreateSocket (
295 (UINT8
) (!Conn
->Ipv6Flag
? TCP_VERSION_4
: TCP_VERSION_6
),
299 if (EFI_ERROR (Status
)) {
300 gBS
->CloseEvent (Conn
->TimeoutEvent
);
310 Destroy an iSCSI connection.
312 @param[in] Conn The connection to destroy.
316 IScsiDestroyConnection (
317 IN ISCSI_CONNECTION
*Conn
320 TcpIoDestroySocket (&Conn
->TcpIo
);
322 NetbufQueFlush (&Conn
->RspQue
);
323 gBS
->CloseEvent (Conn
->TimeoutEvent
);
328 Retrieve the IPv6 Address/Prefix/Gateway from the established TCP connection, these informations
329 will be filled in the iSCSI Boot Firmware Table.
331 @param[in] Conn The connection used in the iSCSI login phase.
333 @retval EFI_SUCCESS Get the NIC information successfully.
334 @retval Others Other errors as indicated.
339 IN ISCSI_CONNECTION
*Conn
342 ISCSI_SESSION_CONFIG_NVDATA
*NvData
;
343 EFI_TCP6_PROTOCOL
*Tcp6
;
344 EFI_IP6_MODE_DATA Ip6ModeData
;
346 EFI_IPv6_ADDRESS
*TargetIp
;
348 UINT8 SubnetPrefixLength
;
351 NvData
= &Conn
->Session
->ConfigData
->SessionConfigData
;
352 TargetIp
= &NvData
->TargetIp
.v6
;
353 Tcp6
= Conn
->TcpIo
.Tcp
.Tcp6
;
355 ZeroMem (&Ip6ModeData
, sizeof (EFI_IP6_MODE_DATA
));
356 Status
= Tcp6
->GetModeData (
364 if (EFI_ERROR (Status
)) {
368 if (!Ip6ModeData
.IsConfigured
) {
369 Status
= EFI_ABORTED
;
373 IP6_COPY_ADDRESS (&NvData
->LocalIp
, &Ip6ModeData
.ConfigData
.StationAddress
);
375 NvData
->PrefixLength
= 0;
376 for (Index
= 0; Index
< Ip6ModeData
.AddressCount
; Index
++) {
377 if (EFI_IP6_EQUAL (&NvData
->LocalIp
.v6
, &Ip6ModeData
.AddressList
[Index
].Address
)) {
378 NvData
->PrefixLength
= Ip6ModeData
.AddressList
[Index
].PrefixLength
;
383 SubnetPrefixLength
= 0;
384 RouteEntry
= Ip6ModeData
.RouteCount
;
385 for (Index
= 0; Index
< Ip6ModeData
.RouteCount
; Index
++) {
386 if (NetIp6IsNetEqual (TargetIp
, &Ip6ModeData
.RouteTable
[Index
].Destination
, Ip6ModeData
.RouteTable
[Index
].PrefixLength
)) {
387 if (SubnetPrefixLength
< Ip6ModeData
.RouteTable
[Index
].PrefixLength
) {
388 SubnetPrefixLength
= Ip6ModeData
.RouteTable
[Index
].PrefixLength
;
393 if (RouteEntry
!= Ip6ModeData
.RouteCount
) {
394 IP6_COPY_ADDRESS (&NvData
->Gateway
, &Ip6ModeData
.RouteTable
[RouteEntry
].Gateway
);
398 if (Ip6ModeData
.AddressList
!= NULL
) {
399 FreePool (Ip6ModeData
.AddressList
);
401 if (Ip6ModeData
.GroupTable
!= NULL
) {
402 FreePool (Ip6ModeData
.GroupTable
);
404 if (Ip6ModeData
.RouteTable
!= NULL
) {
405 FreePool (Ip6ModeData
.RouteTable
);
407 if (Ip6ModeData
.NeighborCache
!= NULL
) {
408 FreePool (Ip6ModeData
.NeighborCache
);
410 if (Ip6ModeData
.PrefixTable
!= NULL
) {
411 FreePool (Ip6ModeData
.PrefixTable
);
413 if (Ip6ModeData
.IcmpTypeList
!= NULL
) {
414 FreePool (Ip6ModeData
.IcmpTypeList
);
421 Login the iSCSI session.
423 @param[in] Session The iSCSI session.
425 @retval EFI_SUCCESS The iSCSI session login procedure finished.
426 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
427 @retval EFI_NO_MEDIA There was a media error.
428 @retval Others Other errors as indicated.
433 IN ISCSI_SESSION
*Session
437 ISCSI_CONNECTION
*Conn
;
439 EFI_GUID
*ProtocolGuid
;
441 EFI_STATUS MediaStatus
;
444 // Check media status before session login.
446 MediaStatus
= EFI_SUCCESS
;
447 NetLibDetectMediaWaitTimeout (Session
->Private
->Controller
, ISCSI_CHECK_MEDIA_LOGIN_WAITING_TIME
, &MediaStatus
);
448 if (MediaStatus
!= EFI_SUCCESS
) {
453 // Set session identifier
455 CopyMem (Session
->Isid
, Session
->ConfigData
->SessionConfigData
.IsId
, 6);
461 // Create a connection for the session.
463 Conn
= IScsiCreateConnection (Session
);
465 return EFI_OUT_OF_RESOURCES
;
468 IScsiAttatchConnection (Session
, Conn
);
471 // Login througth the newly created connection.
473 Status
= IScsiConnLogin (Conn
, Session
->ConfigData
->SessionConfigData
.ConnectTimeout
);
474 if (EFI_ERROR (Status
)) {
475 IScsiConnReset (Conn
);
476 IScsiDetatchConnection (Conn
);
477 IScsiDestroyConnection (Conn
);
480 if (Status
!= EFI_TIMEOUT
) {
485 } while (RetryCount
<= Session
->ConfigData
->SessionConfigData
.ConnectRetryCount
);
487 if (!EFI_ERROR (Status
)) {
488 Session
->State
= SESSION_STATE_LOGGED_IN
;
490 if (!Conn
->Ipv6Flag
) {
491 ProtocolGuid
= &gEfiTcp4ProtocolGuid
;
493 ProtocolGuid
= &gEfiTcp6ProtocolGuid
;
496 Status
= gBS
->OpenProtocol (
500 Session
->Private
->Image
,
501 Session
->Private
->ExtScsiPassThruHandle
,
502 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
505 ASSERT_EFI_ERROR (Status
);
507 if (Conn
->Ipv6Flag
) {
508 Status
= IScsiGetIp6NicInfo (Conn
);
517 Wait for IPsec negotiation, then try to login the iSCSI session again.
519 @param[in] Session The iSCSI session.
521 @retval EFI_SUCCESS The iSCSI session login procedure finished.
522 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
523 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred.
527 IScsiSessionReLogin (
528 IN ISCSI_SESSION
*Session
533 EFI_STATUS TimerStatus
;
536 Status
= gBS
->CreateEvent (EVT_TIMER
, TPL_CALLBACK
, NULL
, NULL
, &Timer
);
537 if (EFI_ERROR (Status
)) {
541 Status
= gBS
->SetTimer (
544 ISCSI_WAIT_IPSEC_TIMEOUT
547 if (EFI_ERROR (Status
)) {
548 gBS
->CloseEvent (Timer
);
554 TimerStatus
= gBS
->CheckEvent (Timer
);
556 if (!EFI_ERROR (TimerStatus
)) {
557 Status
= IScsiSessionLogin (Session
);
560 } while (TimerStatus
== EFI_NOT_READY
);
562 gBS
->CloseEvent (Timer
);
568 Build and send the iSCSI login request to the iSCSI target according to
569 the current login stage.
571 @param[in] Conn The connection in the iSCSI login phase.
573 @retval EFI_SUCCESS The iSCSI login request PDU is built and sent on this
575 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
576 @retval EFI_DEVICE_ERROR Some kind of device error occurred.
581 IN ISCSI_CONNECTION
*Conn
588 // Build the Login Request PDU.
590 Pdu
= IScsiPrepareLoginReq (Conn
);
592 return EFI_DEVICE_ERROR
;
595 // Send it to the iSCSI target.
597 Status
= TcpIoTransmit (&Conn
->TcpIo
, Pdu
);
606 Receive and process the iSCSI login response.
608 @param[in] Conn The connection in the iSCSI login phase.
610 @retval EFI_SUCCESS The iSCSI login response PDU is received and processed.
611 @retval Others Other errors as indicated.
615 IScsiReceiveLoginRsp (
616 IN ISCSI_CONNECTION
*Conn
625 // Receive the iSCSI login response.
627 Status
= IScsiReceivePdu (Conn
, &Pdu
, NULL
, FALSE
, FALSE
, NULL
);
628 if (EFI_ERROR (Status
)) {
631 ASSERT (Pdu
!= NULL
);
634 // A Login Response is received; process it.
636 Status
= IScsiProcessLoginRsp (Conn
, Pdu
);
645 Add an iSCSI key-value pair as a string into the data segment of the Login Request PDU.
646 The DataSegmentLength and the actual size of the net buffer containing this PDU will be
649 @param[in, out] Pdu The iSCSI PDU whose data segment the key-value pair will
651 @param[in] Key The key name string.
652 @param[in] Value The value string.
654 @retval EFI_SUCCESS The key-value pair is added to the PDU's data segment and
655 the correspondence length fields are updated.
656 @retval EFI_OUT_OF_RESOURCES There is not enough space in the PDU to add the key-value
658 @retval EFI_PROTOCOL_ERROR There is no such data in the net buffer.
661 IScsiAddKeyValuePair (
671 ISCSI_LOGIN_REQUEST
*LoginReq
;
674 LoginReq
= (ISCSI_LOGIN_REQUEST
*) NetbufGetByte (Pdu
, 0, NULL
);
675 if (LoginReq
== NULL
) {
676 return EFI_PROTOCOL_ERROR
;
678 DataSegLen
= NTOH24 (LoginReq
->DataSegmentLength
);
680 KeyLen
= (UINT32
) AsciiStrLen (Key
);
681 ValueLen
= (UINT32
) AsciiStrLen (Value
);
684 // 1 byte for the key value separator '=' and 1 byte for the null
685 // delimiter after the value.
687 TotalLen
= KeyLen
+ 1 + ValueLen
+ 1;
690 // Allocate the space for the key-value pair.
692 Data
= (CHAR8
*) NetbufAllocSpace (Pdu
, TotalLen
, NET_BUF_TAIL
);
694 return EFI_OUT_OF_RESOURCES
;
699 CopyMem (Data
, Key
, KeyLen
);
708 CopyMem (Data
, Value
, ValueLen
);
714 // Update the DataSegmentLength
716 ISCSI_SET_DATASEG_LEN (LoginReq
, DataSegLen
+ TotalLen
);
723 Prepare the iSCSI login request to be sent according to the current login status.
725 @param[in, out] Conn The connection in the iSCSI login phase.
727 @return The pointer to the net buffer containing the iSCSI login request built.
728 @retval NULL Other errors as indicated.
732 IScsiPrepareLoginReq (
733 IN OUT ISCSI_CONNECTION
*Conn
736 ISCSI_SESSION
*Session
;
738 ISCSI_LOGIN_REQUEST
*LoginReq
;
741 Session
= Conn
->Session
;
743 Nbuf
= NetbufAlloc (sizeof (ISCSI_LOGIN_REQUEST
) + DEFAULT_MAX_RECV_DATA_SEG_LEN
);
748 LoginReq
= (ISCSI_LOGIN_REQUEST
*) NetbufAllocSpace (Nbuf
, sizeof (ISCSI_LOGIN_REQUEST
), NET_BUF_TAIL
);
749 if (LoginReq
== NULL
) {
753 ZeroMem (LoginReq
, sizeof (ISCSI_LOGIN_REQUEST
));
756 // Init the login request pdu
758 ISCSI_SET_OPCODE (LoginReq
, ISCSI_OPCODE_LOGIN_REQ
, ISCSI_REQ_IMMEDIATE
);
759 ISCSI_SET_STAGES (LoginReq
, Conn
->CurrentStage
, Conn
->NextStage
);
760 LoginReq
->VersionMax
= ISCSI_VERSION_MAX
;
761 LoginReq
->VersionMin
= ISCSI_VERSION_MIN
;
762 LoginReq
->Tsih
= HTONS (Session
->Tsih
);
763 LoginReq
->InitiatorTaskTag
= HTONL (Session
->InitiatorTaskTag
);
764 LoginReq
->Cid
= HTONS (Conn
->Cid
);
765 LoginReq
->CmdSN
= HTONL (Session
->CmdSN
);
768 // For the first Login Request on a coonection this is ExpStatSN for the
769 // old connection, and this field is only valid if the Login Request restarts
771 // For subsequent Login Requests it is used to acknowledge the Login Responses
772 // with their increasing StatSN values.
774 LoginReq
->ExpStatSN
= HTONL (Conn
->ExpStatSN
);
775 CopyMem (LoginReq
->Isid
, Session
->Isid
, sizeof (LoginReq
->Isid
));
777 if (Conn
->PartialRspRcvd
) {
779 // A partial response. The initiator must send an empty Login Request.
784 Status
= EFI_SUCCESS
;
786 switch (Conn
->CurrentStage
) {
787 case ISCSI_SECURITY_NEGOTIATION
:
789 // Both none authentication and CHAP authentication share the CHAP path.
792 if (Session
->AuthType
!= ISCSI_AUTH_TYPE_KRB
) {
793 Status
= IScsiCHAPToSendReq (Conn
, Nbuf
);
798 case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
:
800 // Only negotiate the parameter once.
802 if (!Conn
->ParamNegotiated
) {
803 IScsiFillOpParams (Conn
, Nbuf
);
806 ISCSI_SET_FLAG (LoginReq
, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT
);
811 // An error occurs...
813 Status
= EFI_DEVICE_ERROR
;
817 if (EFI_ERROR (Status
)) {
822 // Pad the data segment if needed.
824 IScsiPadSegment (Nbuf
, ISCSI_GET_DATASEG_LEN (LoginReq
));
826 // Check whether we will issue the stage transition signal?
828 Conn
->TransitInitiated
= ISCSI_FLAG_ON (LoginReq
, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT
);
836 Process the iSCSI Login Response.
838 @param[in, out] Conn The connection on which the iSCSI login response is received.
839 @param[in, out] Pdu The iSCSI login response PDU.
841 @retval EFI_SUCCESS The iSCSI login response PDU is processed, and all checks are passed.
842 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred.
843 @retval EFI_MEDIA_CHANGED Target is redirected.
844 @retval Others Other errors as indicated.
848 IScsiProcessLoginRsp (
849 IN OUT ISCSI_CONNECTION
*Conn
,
854 ISCSI_SESSION
*Session
;
855 ISCSI_LOGIN_RESPONSE
*LoginRsp
;
863 Status
= EFI_SUCCESS
;
864 Session
= Conn
->Session
;
866 LoginRsp
= (ISCSI_LOGIN_RESPONSE
*) NetbufGetByte (Pdu
, 0, NULL
);
867 if (LoginRsp
== NULL
) {
868 return EFI_PROTOCOL_ERROR
;
870 if (!ISCSI_CHECK_OPCODE (LoginRsp
, ISCSI_OPCODE_LOGIN_RSP
)) {
872 // It is not a Login Response.
874 return EFI_PROTOCOL_ERROR
;
877 // Get the data segment, if any.
879 DataSegLen
= ISCSI_GET_DATASEG_LEN (LoginRsp
);
880 if (DataSegLen
!= 0) {
881 DataSeg
= NetbufGetByte (Pdu
, sizeof (ISCSI_LOGIN_RESPONSE
), NULL
);
886 // Check the status class in the login response PDU.
888 switch (LoginRsp
->StatusClass
) {
889 case ISCSI_LOGIN_STATUS_SUCCESS
:
891 // Just break here; the response and the data segment will be processed later.
895 case ISCSI_LOGIN_STATUS_REDIRECTION
:
897 // The target may be moved to a different address.
899 if (DataSeg
== NULL
) {
900 return EFI_PROTOCOL_ERROR
;
903 // Process the TargetAddress key-value strings in the data segment to update the
904 // target address info.
906 Status
= IScsiUpdateTargetAddress (Session
, (CHAR8
*) DataSeg
, DataSegLen
);
907 if (EFI_ERROR (Status
)) {
911 // Session will be restarted on this error status because the Target is
912 // redirected by this Login Response.
914 return EFI_MEDIA_CHANGED
;
918 // Initiator Error, Target Error, or any other undefined error code.
920 return EFI_PROTOCOL_ERROR
;
923 // The status is success; extract the wanted fields from the header segment.
925 Transit
= ISCSI_FLAG_ON (LoginRsp
, ISCSI_LOGIN_RSP_PDU_FLAG_TRANSIT
);
926 Continue
= ISCSI_FLAG_ON (LoginRsp
, ISCSI_LOGIN_RSP_PDU_FLAG_CONTINUE
);
928 CurrentStage
= ISCSI_GET_CURRENT_STAGE (LoginRsp
);
929 NextStage
= ISCSI_GET_NEXT_STAGE (LoginRsp
);
931 LoginRsp
->InitiatorTaskTag
= NTOHL (LoginRsp
->InitiatorTaskTag
);
933 if ((Transit
&& Continue
) ||
934 (CurrentStage
!= Conn
->CurrentStage
) ||
935 (!Conn
->TransitInitiated
&& Transit
) ||
936 (Transit
&& (NextStage
!= Conn
->NextStage
)) ||
937 (CompareMem (Session
->Isid
, LoginRsp
->Isid
, sizeof (LoginRsp
->Isid
)) != 0) ||
938 (LoginRsp
->InitiatorTaskTag
!= Session
->InitiatorTaskTag
)
941 // A Login Response with the C bit set to 1 MUST have the T bit set to 0.
942 // The CSG in the Login Response MUST be the same with the I-end of this connection.
943 // The T bit can't be 1 if the last Login Response sent by the initiator doesn't
944 // initiate the transistion.
945 // The NSG MUST be the same with the I-end of this connection if Transit is required.
946 // The ISID in the Login Response MUST be the same with this session.
948 return EFI_PROTOCOL_ERROR
;
951 LoginRsp
->StatSN
= NTOHL (LoginRsp
->StatSN
);
952 LoginRsp
->ExpCmdSN
= NTOHL (LoginRsp
->ExpCmdSN
);
953 LoginRsp
->MaxCmdSN
= NTOHL (LoginRsp
->MaxCmdSN
);
955 if ((Conn
->CurrentStage
== ISCSI_SECURITY_NEGOTIATION
) && (Conn
->AuthStep
== ISCSI_AUTH_INITIAL
)) {
957 // If the Login Request is a leading Login Request, the target MUST use
958 // the value presented in CmdSN as the target value for ExpCmdSN.
960 if ((Session
->State
== SESSION_STATE_FREE
) && (Session
->CmdSN
!= LoginRsp
->ExpCmdSN
)) {
961 return EFI_PROTOCOL_ERROR
;
965 // It's the initial Login Response, initialize the local ExpStatSN, MaxCmdSN
968 Conn
->ExpStatSN
= LoginRsp
->StatSN
+ 1;
969 Session
->MaxCmdSN
= LoginRsp
->MaxCmdSN
;
970 Session
->ExpCmdSN
= LoginRsp
->ExpCmdSN
;
973 // Check the StatSN of this PDU.
975 Status
= IScsiCheckSN (&Conn
->ExpStatSN
, LoginRsp
->StatSN
);
976 if (!EFI_ERROR (Status
)) {
978 // Update the MaxCmdSN and ExpCmdSN.
980 IScsiUpdateCmdSN (Session
, LoginRsp
->MaxCmdSN
, LoginRsp
->ExpCmdSN
);
986 // Trim off the header segment.
988 NetbufTrim (Pdu
, sizeof (ISCSI_LOGIN_RESPONSE
), NET_BUF_HEAD
);
991 // Queue this login response first in case it's a partial response so that
992 // later when the full response list is received we can combine these scattered
993 // responses' data segment and then process it.
996 NetbufQueAppend (&Conn
->RspQue
, Pdu
);
998 Conn
->PartialRspRcvd
= Continue
;
1001 // It is a partial response; must wait for another or more Request/Response
1002 // conversations to get the full response.
1007 switch (CurrentStage
) {
1008 case ISCSI_SECURITY_NEGOTIATION
:
1010 // In security negotiation stage, let CHAP module handle it.
1012 if (Session
->AuthType
!= ISCSI_AUTH_TYPE_KRB
) {
1013 Status
= IScsiCHAPOnRspReceived (Conn
);
1017 case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
:
1019 // Response received with negotiation response on iSCSI parameters: check them.
1021 Status
= IScsiCheckOpParams (Conn
);
1022 if (!EFI_ERROR (Status
)) {
1023 Conn
->ParamNegotiated
= TRUE
;
1030 // Should never get here.
1032 Status
= EFI_PROTOCOL_ERROR
;
1036 if (Transit
&& (Status
== EFI_SUCCESS
)) {
1038 // Do the state transition.
1040 Conn
->CurrentStage
= Conn
->NextStage
;
1042 if (Conn
->CurrentStage
== ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
) {
1043 Conn
->NextStage
= ISCSI_FULL_FEATURE_PHASE
;
1046 // CurrentStage is iSCSI Full Feature. It is the Login-Final Response;
1047 // get the TSIH from the Login Response.
1049 Session
->Tsih
= NTOHS (LoginRsp
->Tsih
);
1053 // Flush the response(s) received.
1055 NetbufQueFlush (&Conn
->RspQue
);
1062 Updated the target information according the data received in the iSCSI
1063 login response with an target redirection status.
1065 @param[in, out] Session The iSCSI session.
1066 @param[in] Data The data segment that should contain the
1067 TargetAddress key-value list.
1068 @param[in] Len Length of the data.
1070 @retval EFI_SUCCESS The target address is updated.
1071 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1072 @retval EFI_NOT_FOUND The TargetAddress key is not found.
1073 @retval Others Other errors as indicated.
1077 IScsiUpdateTargetAddress (
1078 IN OUT ISCSI_SESSION
*Session
,
1083 LIST_ENTRY
*KeyValueList
;
1084 CHAR8
*TargetAddress
;
1089 ISCSI_SESSION_CONFIG_NVDATA
*NvData
;
1091 KeyValueList
= IScsiBuildKeyValueList (Data
, Len
);
1092 if (KeyValueList
== NULL
) {
1093 return EFI_OUT_OF_RESOURCES
;
1096 Status
= EFI_NOT_FOUND
;
1097 NvData
= &Session
->ConfigData
->SessionConfigData
;
1100 TargetAddress
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_ADDRESS
);
1101 if (TargetAddress
== NULL
) {
1106 // RFC 3720 defines format of the TargetAddress=domainname[:port][,portal-group-tag]
1107 // The domainname can be specified as either a DNS host name, adotted-decimal IPv4 address,
1108 // or a bracketed IPv6 address as specified in [RFC2732].
1110 if (NET_IS_DIGIT (TargetAddress
[0])) {
1112 // The domainname of the target is presented in a dotted-decimal IPv4 address format.
1114 IpStr
= TargetAddress
;
1116 while ((*TargetAddress
!= '\0') && (*TargetAddress
!= ':') && (*TargetAddress
!= ',')) {
1118 // NULL, ':', or ',' ends the IPv4 string.
1122 } else if (*TargetAddress
== ISCSI_REDIRECT_ADDR_START_DELIMITER
){
1124 // The domainname of the target is presented in a bracketed IPv6 address format.
1127 IpStr
= TargetAddress
;
1128 while ((*TargetAddress
!= '\0') && (*TargetAddress
!= ISCSI_REDIRECT_ADDR_END_DELIMITER
)) {
1130 // ']' ends the IPv6 string.
1135 if (*TargetAddress
!= ISCSI_REDIRECT_ADDR_END_DELIMITER
) {
1139 *TargetAddress
= '\0';
1144 // The domainname of the target is presented in the format of a DNS host name.
1146 IpStr
= TargetAddress
;
1148 while ((*TargetAddress
!= '\0') && (*TargetAddress
!= ':') && (*TargetAddress
!= ',')) {
1151 NvData
->DnsMode
= TRUE
;
1155 // Save the origial user setting which specifies the proxy/virtual iSCSI target.
1157 NvData
->OriginalTargetPort
= NvData
->TargetPort
;
1159 if (*TargetAddress
== ',') {
1161 // Comma and the portal group tag MUST be ommitted if the TargetAddress is sent
1162 // as the result of a redirection.
1165 } else if (*TargetAddress
== ':') {
1166 *TargetAddress
= '\0';
1170 Number
= AsciiStrDecimalToUintn (TargetAddress
);
1171 if (Number
> 0xFFFF) {
1174 NvData
->TargetPort
= (UINT16
) Number
;
1178 // The string only contains the Target address. Use the well-known port.
1180 NvData
->TargetPort
= ISCSI_WELL_KNOWN_PORT
;
1184 // Save the origial user setting which specifies the proxy/virtual iSCSI target.
1186 CopyMem (&NvData
->OriginalTargetIp
, &NvData
->TargetIp
, sizeof (EFI_IP_ADDRESS
));
1189 // Update the target IP address.
1191 if (NvData
->IpMode
< IP_MODE_AUTOCONFIG
) {
1192 IpMode
= NvData
->IpMode
;
1194 IpMode
= Session
->ConfigData
->AutoConfigureMode
;
1197 if (NvData
->DnsMode
) {
1199 // Target address is expressed as URL format, just save it and
1200 // do DNS resolution when creating a TCP connection.
1202 if (AsciiStrSize (IpStr
) > sizeof (Session
->ConfigData
->SessionConfigData
.TargetUrl
)){
1203 return EFI_INVALID_PARAMETER
;
1205 CopyMem (&Session
->ConfigData
->SessionConfigData
.TargetUrl
, IpStr
, AsciiStrSize (IpStr
));
1207 Status
= IScsiAsciiStrToIp (
1210 &Session
->ConfigData
->SessionConfigData
.TargetIp
1213 if (EFI_ERROR (Status
)) {
1216 NvData
->RedirectFlag
= TRUE
;
1222 IScsiFreeKeyValueList (KeyValueList
);
1229 The callback function to free the net buffer list.
1231 @param[in] Arg The opaque parameter.
1240 ASSERT (Arg
!= NULL
);
1242 NetbufFreeList ((LIST_ENTRY
*) Arg
);
1248 The callback function called in NetBufFree; it does nothing.
1250 @param[in] Arg The opaque parameter.
1263 Receive an iSCSI response PDU. An iSCSI response PDU contains an iSCSI PDU header and
1264 an optional data segment. The two parts will be put into two blocks of buffers in the
1265 net buffer. The digest check will be conducted in this function if needed and the digests
1266 will be trimmed from the PDU buffer.
1268 @param[in] Conn The iSCSI connection to receive data from.
1269 @param[out] Pdu The received iSCSI pdu.
1270 @param[in] Context The context used to describe information on the caller provided
1271 buffer to receive data segment of the iSCSI pdu. It is optional.
1272 @param[in] HeaderDigest Whether there will be header digest received.
1273 @param[in] DataDigest Whether there will be data digest.
1274 @param[in] TimeoutEvent The timeout event. It is optional.
1276 @retval EFI_SUCCESS An iSCSI pdu is received.
1277 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1278 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred.
1279 @retval Others Other errors as indicated.
1284 IN ISCSI_CONNECTION
*Conn
,
1286 IN ISCSI_IN_BUFFER_CONTEXT
*Context
, OPTIONAL
1287 IN BOOLEAN HeaderDigest
,
1288 IN BOOLEAN DataDigest
,
1289 IN EFI_EVENT TimeoutEvent OPTIONAL
1292 LIST_ENTRY
*NbufList
;
1298 UINT32 InDataOffset
;
1299 NET_FRAGMENT Fragment
[2];
1300 UINT32 FragmentCount
;
1302 UINT32 PadAndCRC32
[2];
1304 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
1305 if (NbufList
== NULL
) {
1306 return EFI_OUT_OF_RESOURCES
;
1309 InitializeListHead (NbufList
);
1312 // The header digest will be received together with the PDU header, if exists.
1314 Len
= sizeof (ISCSI_BASIC_HEADER
) + (HeaderDigest
? sizeof (UINT32
) : 0);
1315 PduHdr
= NetbufAlloc (Len
);
1316 if (PduHdr
== NULL
) {
1317 Status
= EFI_OUT_OF_RESOURCES
;
1321 Header
= NetbufAllocSpace (PduHdr
, Len
, NET_BUF_TAIL
);
1322 if (Header
== NULL
) {
1323 Status
= EFI_OUT_OF_RESOURCES
;
1326 InsertTailList (NbufList
, &PduHdr
->List
);
1329 // First step, receive the BHS of the PDU.
1331 Status
= TcpIoReceive (&Conn
->TcpIo
, PduHdr
, FALSE
, TimeoutEvent
);
1333 if (EFI_ERROR (Status
)) {
1339 // TODO: check the header-digest.
1342 // Trim off the digest.
1344 NetbufTrim (PduHdr
, sizeof (UINT32
), NET_BUF_TAIL
);
1347 Len
= ISCSI_GET_DATASEG_LEN (Header
);
1355 // Get the length of the padding bytes of the data segment.
1357 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1359 switch (ISCSI_GET_OPCODE (Header
)) {
1360 case ISCSI_OPCODE_SCSI_DATA_IN
:
1362 // To reduce memory copy overhead, try to use the buffer described by Context
1363 // if the PDU is an iSCSI SCSI data.
1365 InDataOffset
= ISCSI_GET_BUFFER_OFFSET (Header
);
1366 if ((Context
== NULL
) || ((InDataOffset
+ Len
) > Context
->InDataLen
)) {
1367 Status
= EFI_PROTOCOL_ERROR
;
1371 Fragment
[0].Len
= Len
;
1372 Fragment
[0].Bulk
= Context
->InData
+ InDataOffset
;
1374 if (DataDigest
|| (PadLen
!= 0)) {
1376 // The data segment is padded. Use two fragments to receive it:
1377 // the first to receive the useful data; the second to receive the padding.
1379 Fragment
[1].Len
= PadLen
+ (DataDigest
? sizeof (UINT32
) : 0);
1380 Fragment
[1].Bulk
= (UINT8
*)PadAndCRC32
+ (4 - PadLen
);
1387 DataSeg
= NetbufFromExt (&Fragment
[0], FragmentCount
, 0, 0, IScsiNbufExtFree
, NULL
);
1388 if (DataSeg
== NULL
) {
1389 Status
= EFI_OUT_OF_RESOURCES
;
1395 case ISCSI_OPCODE_SCSI_RSP
:
1396 case ISCSI_OPCODE_NOP_IN
:
1397 case ISCSI_OPCODE_LOGIN_RSP
:
1398 case ISCSI_OPCODE_TEXT_RSP
:
1399 case ISCSI_OPCODE_ASYNC_MSG
:
1400 case ISCSI_OPCODE_REJECT
:
1401 case ISCSI_OPCODE_VENDOR_T0
:
1402 case ISCSI_OPCODE_VENDOR_T1
:
1403 case ISCSI_OPCODE_VENDOR_T2
:
1405 // Allocate buffer to receive the data segment.
1407 Len
+= PadLen
+ (DataDigest
? sizeof (UINT32
) : 0);
1408 DataSeg
= NetbufAlloc (Len
);
1409 if (DataSeg
== NULL
) {
1410 Status
= EFI_OUT_OF_RESOURCES
;
1414 NetbufAllocSpace (DataSeg
, Len
, NET_BUF_TAIL
);
1418 Status
= EFI_PROTOCOL_ERROR
;
1422 InsertTailList (NbufList
, &DataSeg
->List
);
1425 // Receive the data segment with the data digest, if any.
1427 Status
= TcpIoReceive (&Conn
->TcpIo
, DataSeg
, FALSE
, TimeoutEvent
);
1429 if (EFI_ERROR (Status
)) {
1435 // TODO: Check the data digest.
1437 NetbufTrim (DataSeg
, sizeof (UINT32
), NET_BUF_TAIL
);
1442 // Trim off the padding bytes in the data segment.
1444 NetbufTrim (DataSeg
, PadLen
, NET_BUF_TAIL
);
1449 // Form the pdu from a list of pdu segments.
1451 *Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1453 Status
= EFI_OUT_OF_RESOURCES
;
1458 if (EFI_ERROR (Status
)) {
1460 // Free the Nbufs in this NbufList and the NbufList itself.
1462 IScsiFreeNbufList (NbufList
);
1470 Check and get the result of the parameter negotiation.
1472 @param[in, out] Conn The connection in iSCSI login.
1474 @retval EFI_SUCCESS The parmeter check is passed and negotiation is finished.
1475 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred.
1476 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1480 IScsiCheckOpParams (
1481 IN OUT ISCSI_CONNECTION
*Conn
1485 LIST_ENTRY
*KeyValueList
;
1488 ISCSI_SESSION
*Session
;
1492 ASSERT (Conn
->RspQue
.BufNum
!= 0);
1494 Session
= Conn
->Session
;
1496 Len
= Conn
->RspQue
.BufSize
;
1497 Data
= AllocatePool (Len
);
1499 return EFI_OUT_OF_RESOURCES
;
1502 NetbufQueCopy (&Conn
->RspQue
, 0, Len
, (UINT8
*) Data
);
1504 Status
= EFI_PROTOCOL_ERROR
;
1507 // Extract the Key-Value pairs into a list.
1509 KeyValueList
= IScsiBuildKeyValueList (Data
, Len
);
1510 if (KeyValueList
== NULL
) {
1517 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_HEADER_DIGEST
);
1518 if (Value
== NULL
) {
1522 if (AsciiStrCmp (Value
, "CRC32") == 0) {
1523 if (Conn
->HeaderDigest
!= IScsiDigestCRC32
) {
1526 } else if (AsciiStrCmp (Value
, ISCSI_KEY_VALUE_NONE
) == 0) {
1527 Conn
->HeaderDigest
= IScsiDigestNone
;
1534 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_DIGEST
);
1535 if (Value
== NULL
) {
1539 if (AsciiStrCmp (Value
, "CRC32") == 0) {
1540 if (Conn
->DataDigest
!= IScsiDigestCRC32
) {
1543 } else if (AsciiStrCmp (Value
, ISCSI_KEY_VALUE_NONE
) == 0) {
1544 Conn
->DataDigest
= IScsiDigestNone
;
1549 // ErrorRecoveryLevel: result fuction is Minimum.
1551 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_ERROR_RECOVERY_LEVEL
);
1552 if (Value
== NULL
) {
1556 NumericValue
= IScsiNetNtoi (Value
);
1557 if (NumericValue
> 2) {
1561 Session
->ErrorRecoveryLevel
= (UINT8
) MIN (Session
->ErrorRecoveryLevel
, NumericValue
);
1564 // InitialR2T: result function is OR.
1566 if (!Session
->InitialR2T
) {
1567 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_INITIAL_R2T
);
1568 if (Value
== NULL
) {
1572 Session
->InitialR2T
= (BOOLEAN
) (AsciiStrCmp (Value
, "Yes") == 0);
1576 // ImmediateData: result function is AND.
1578 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_IMMEDIATE_DATA
);
1579 if (Value
== NULL
) {
1583 Session
->ImmediateData
= (BOOLEAN
) (Session
->ImmediateData
&& (BOOLEAN
) (AsciiStrCmp (Value
, "Yes") == 0));
1586 // MaxRecvDataSegmentLength is declarative.
1588 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
);
1589 if (Value
!= NULL
) {
1590 Conn
->MaxRecvDataSegmentLength
= (UINT32
) IScsiNetNtoi (Value
);
1593 // MaxBurstLength: result funtion is Mininum.
1595 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_BURST_LENGTH
);
1596 if (Value
== NULL
) {
1600 NumericValue
= IScsiNetNtoi (Value
);
1601 Session
->MaxBurstLength
= (UINT32
) MIN (Session
->MaxBurstLength
, NumericValue
);
1604 // FirstBurstLength: result function is Minimum. Irrelevant when InitialR2T=Yes and
1605 // ImmediateData=No.
1607 if (!(Session
->InitialR2T
&& !Session
->ImmediateData
)) {
1608 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_FIRST_BURST_LENGTH
);
1609 if (Value
== NULL
) {
1613 NumericValue
= IScsiNetNtoi (Value
);
1614 Session
->FirstBurstLength
= (UINT32
) MIN (Session
->FirstBurstLength
, NumericValue
);
1618 // MaxConnections: result function is Minimum.
1620 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_CONNECTIONS
);
1621 if (Value
== NULL
) {
1625 NumericValue
= IScsiNetNtoi (Value
);
1626 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1630 Session
->MaxConnections
= (UINT32
) MIN (Session
->MaxConnections
, NumericValue
);
1633 // DataPDUInOrder: result function is OR.
1635 if (!Session
->DataPDUInOrder
) {
1636 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_PDU_IN_ORDER
);
1637 if (Value
== NULL
) {
1641 Session
->DataPDUInOrder
= (BOOLEAN
) (AsciiStrCmp (Value
, "Yes") == 0);
1645 // DataSequenceInorder: result function is OR.
1647 if (!Session
->DataSequenceInOrder
) {
1648 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
);
1649 if (Value
== NULL
) {
1653 Session
->DataSequenceInOrder
= (BOOLEAN
) (AsciiStrCmp (Value
, "Yes") == 0);
1657 // DefaultTime2Wait: result function is Maximum.
1659 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2WAIT
);
1660 if (Value
== NULL
) {
1664 NumericValue
= IScsiNetNtoi (Value
);
1665 if (NumericValue
== 0) {
1666 Session
->DefaultTime2Wait
= 0;
1667 } else if (NumericValue
> 3600) {
1670 Session
->DefaultTime2Wait
= (UINT32
) MAX (Session
->DefaultTime2Wait
, NumericValue
);
1673 // DefaultTime2Retain: result function is Minimum.
1675 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2RETAIN
);
1676 if (Value
== NULL
) {
1680 NumericValue
= IScsiNetNtoi (Value
);
1681 if (NumericValue
== 0) {
1682 Session
->DefaultTime2Retain
= 0;
1683 } else if (NumericValue
> 3600) {
1686 Session
->DefaultTime2Retain
= (UINT32
) MIN (Session
->DefaultTime2Retain
, NumericValue
);
1689 // MaxOutstandingR2T: result function is Minimum.
1691 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_OUTSTANDING_R2T
);
1692 if (Value
== NULL
) {
1696 NumericValue
= IScsiNetNtoi (Value
);
1697 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1701 Session
->MaxOutstandingR2T
= (UINT16
) MIN (Session
->MaxOutstandingR2T
, NumericValue
);
1704 // Remove declarative key-value pairs, if any.
1706 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_SESSION_TYPE
);
1707 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_ALIAS
);
1708 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_PORTAL_GROUP_TAG
);
1712 // Remove the key-value that may not needed for result function is OR.
1714 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_INITIAL_R2T
);
1715 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_PDU_IN_ORDER
);
1716 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
);
1719 // Remove irrelevant parameter, if any.
1721 if (Session
->InitialR2T
&& !Session
->ImmediateData
) {
1722 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_FIRST_BURST_LENGTH
);
1725 if (IsListEmpty (KeyValueList
)) {
1727 // Succeed if no more keys in the list.
1729 Status
= EFI_SUCCESS
;
1734 IScsiFreeKeyValueList (KeyValueList
);
1743 Fill the operational parameters.
1745 @param[in] Conn The connection in iSCSI login.
1746 @param[in, out] Pdu The iSCSI login request PDU to fill the parameters.
1751 IN ISCSI_CONNECTION
*Conn
,
1755 ISCSI_SESSION
*Session
;
1758 Session
= Conn
->Session
;
1760 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->HeaderDigest
== IScsiDigestCRC32
) ? "None,CRC32" : "None");
1761 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_HEADER_DIGEST
, Value
);
1763 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->DataDigest
== IScsiDigestCRC32
) ? "None,CRC32" : "None");
1764 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_DIGEST
, Value
);
1766 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->ErrorRecoveryLevel
);
1767 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_ERROR_RECOVERY_LEVEL
, Value
);
1769 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->InitialR2T
? "Yes" : "No");
1770 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_INITIAL_R2T
, Value
);
1772 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->ImmediateData
? "Yes" : "No");
1773 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_IMMEDIATE_DATA
, Value
);
1775 AsciiSPrint (Value
, sizeof (Value
), "%d", MAX_RECV_DATA_SEG_LEN_IN_FFP
);
1776 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
, Value
);
1778 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxBurstLength
);
1779 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_BURST_LENGTH
, Value
);
1781 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->FirstBurstLength
);
1782 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_FIRST_BURST_LENGTH
, Value
);
1784 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxConnections
);
1785 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_CONNECTIONS
, Value
);
1787 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataPDUInOrder
? "Yes" : "No");
1788 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_PDU_IN_ORDER
, Value
);
1790 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataSequenceInOrder
? "Yes" : "No");
1791 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
, Value
);
1793 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Wait
);
1794 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2WAIT
, Value
);
1796 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Retain
);
1797 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2RETAIN
, Value
);
1799 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxOutstandingR2T
);
1800 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_OUTSTANDING_R2T
, Value
);
1805 Pad the iSCSI AHS or data segment to an integer number of 4 byte words.
1807 @param[in, out] Pdu The iSCSI pdu which contains segments to pad.
1808 @param[in] Len The length of the last segment in the PDU.
1810 @retval EFI_SUCCESS The segment is padded or there is no need to pad it.
1811 @retval EFI_OUT_OF_RESOURCES There is not enough remaining free space to add the
1816 IN OUT NET_BUF
*Pdu
,
1823 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1826 Data
= NetbufAllocSpace (Pdu
, PadLen
, NET_BUF_TAIL
);
1828 return EFI_OUT_OF_RESOURCES
;
1831 ZeroMem (Data
, PadLen
);
1839 Build a key-value list from the data segment.
1841 @param[in] Data The data segment containing the key-value pairs.
1842 @param[in] Len Length of the data segment.
1844 @return The key-value list.
1845 @retval NULL Other errors as indicated.
1849 IScsiBuildKeyValueList (
1854 LIST_ENTRY
*ListHead
;
1855 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1857 ListHead
= AllocatePool (sizeof (LIST_ENTRY
));
1858 if (ListHead
== NULL
) {
1862 InitializeListHead (ListHead
);
1865 KeyValuePair
= AllocatePool (sizeof (ISCSI_KEY_VALUE_PAIR
));
1866 if (KeyValuePair
== NULL
) {
1870 InitializeListHead (&KeyValuePair
->List
);
1872 KeyValuePair
->Key
= Data
;
1874 while ((Len
> 0) && (*Data
!= '=')) {
1885 FreePool (KeyValuePair
);
1889 KeyValuePair
->Value
= Data
;
1891 InsertTailList (ListHead
, &KeyValuePair
->List
);;
1893 Data
+= AsciiStrLen (KeyValuePair
->Value
) + 1;
1894 Len
-= (UINT32
) AsciiStrLen (KeyValuePair
->Value
) + 1;
1901 IScsiFreeKeyValueList (ListHead
);
1908 Get the value string by the key name from the key-value list. If found,
1909 the key-value entry will be removed from the list.
1911 @param[in, out] KeyValueList The key-value list.
1912 @param[in] Key The key name to find.
1914 @return The value string.
1915 @retval NULL The key value pair cannot be found.
1919 IScsiGetValueByKeyFromList (
1920 IN OUT LIST_ENTRY
*KeyValueList
,
1925 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1930 NET_LIST_FOR_EACH (Entry
, KeyValueList
) {
1931 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1933 if (AsciiStrCmp (KeyValuePair
->Key
, Key
) == 0) {
1934 Value
= KeyValuePair
->Value
;
1936 RemoveEntryList (&KeyValuePair
->List
);
1937 FreePool (KeyValuePair
);
1947 Free the key-value list.
1949 @param[in] KeyValueList The key-value list.
1953 IScsiFreeKeyValueList (
1954 IN LIST_ENTRY
*KeyValueList
1958 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1960 while (!IsListEmpty (KeyValueList
)) {
1961 Entry
= NetListRemoveHead (KeyValueList
);
1962 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1964 FreePool (KeyValuePair
);
1967 FreePool (KeyValueList
);
1972 Normalize the iSCSI name according to RFC.
1974 @param[in, out] Name The iSCSI name.
1975 @param[in] Len Length of the iSCSI name.
1977 @retval EFI_SUCCESS The iSCSI name is valid and normalized.
1978 @retval EFI_PROTOCOL_ERROR The iSCSI name is malformatted or not in the IQN format.
1982 IScsiNormalizeName (
1989 for (Index
= 0; Index
< Len
; Index
++) {
1990 if (NET_IS_UPPER_CASE_CHAR (Name
[Index
])) {
1992 // Convert the upper-case characters to lower-case ones.
1994 Name
[Index
] = (CHAR8
) (Name
[Index
] - 'A' + 'a');
1997 if (!NET_IS_LOWER_CASE_CHAR (Name
[Index
]) &&
1998 !NET_IS_DIGIT (Name
[Index
]) &&
1999 (Name
[Index
] != '-') &&
2000 (Name
[Index
] != '.') &&
2001 (Name
[Index
] != ':')
2004 // ASCII dash, dot, colon lower-case characters and digit characters
2007 return EFI_PROTOCOL_ERROR
;
2011 if ((Len
< 4) || (CompareMem (Name
, "iqn.", 4) != 0)) {
2013 // Only IQN format is accepted now.
2015 return EFI_PROTOCOL_ERROR
;
2023 Create an iSCSI task control block.
2025 @param[in] Conn The connection on which the task control block will be created.
2026 @param[out] Tcb The newly created task control block.
2028 @retval EFI_SUCCESS The task control block is created.
2029 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2030 @retval EFI_NOT_READY The target cannot accept new commands.
2035 IN ISCSI_CONNECTION
*Conn
,
2039 ISCSI_SESSION
*Session
;
2042 ASSERT (Tcb
!= NULL
);
2044 Session
= Conn
->Session
;
2046 if (ISCSI_SEQ_GT (Session
->CmdSN
, Session
->MaxCmdSN
)) {
2047 return EFI_NOT_READY
;
2050 NewTcb
= AllocateZeroPool (sizeof (ISCSI_TCB
));
2051 if (NewTcb
== NULL
) {
2052 return EFI_OUT_OF_RESOURCES
;
2055 InitializeListHead (&NewTcb
->Link
);
2057 NewTcb
->SoFarInOrder
= TRUE
;
2058 NewTcb
->InitiatorTaskTag
= Session
->InitiatorTaskTag
;
2059 NewTcb
->CmdSN
= Session
->CmdSN
;
2060 NewTcb
->Conn
= Conn
;
2062 InsertTailList (&Session
->TcbList
, &NewTcb
->Link
);
2065 // Advance the initiator task tag.
2067 Session
->InitiatorTaskTag
++;
2077 Delete the tcb from the connection and destroy it.
2079 @param[in] Tcb The tcb to delete.
2087 RemoveEntryList (&Tcb
->Link
);
2094 Create a data segment, pad it, and calculate the CRC if needed.
2096 @param[in] Data The data to fill into the data segment.
2097 @param[in] Len Length of the data.
2098 @param[in] DataDigest Whether to calculate CRC for this data segment.
2100 @return The net buffer wrapping the data segment.
2104 IScsiNewDataSegment (
2107 IN BOOLEAN DataDigest
2110 NET_FRAGMENT Fragment
[2];
2111 UINT32 FragmentCount
;
2115 Fragment
[0].Len
= Len
;
2116 Fragment
[0].Bulk
= Data
;
2118 PadLen
= ISCSI_GET_PAD_LEN (Len
);
2120 Fragment
[1].Len
= PadLen
;
2121 Fragment
[1].Bulk
= (UINT8
*) &mDataSegPad
;
2128 DataSeg
= NetbufFromExt (&Fragment
[0], FragmentCount
, 0, 0, IScsiNbufExtFree
, NULL
);
2135 Create a iSCSI SCSI command PDU to encapsulate the command issued
2136 by SCSI through the EXT SCSI PASS THRU Protocol.
2138 @param[in] Packet The EXT SCSI PASS THRU request packet containing the SCSI command.
2139 @param[in] Lun The LUN.
2140 @param[in] Tcb The tcb associated with this SCSI command.
2142 @return The created iSCSI SCSI command PDU.
2143 @retval NULL Other errors as indicated.
2147 IScsiNewScsiCmdPdu (
2148 IN EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
2153 LIST_ENTRY
*NbufList
;
2157 SCSI_COMMAND
*ScsiCmd
;
2160 ISCSI_ADDITIONAL_HEADER
*Header
;
2161 ISCSI_BI_EXP_READ_DATA_LEN_AHS
*BiExpReadDataLenAHS
;
2162 ISCSI_SESSION
*Session
;
2163 UINT32 ImmediateDataLen
;
2167 if (Packet
->DataDirection
== DataBi
) {
2169 // Bidirectional Read/Write command, the bidirectional expected
2170 // read data length AHS is required.
2172 AHSLength
+= sizeof (ISCSI_BI_EXP_READ_DATA_LEN_AHS
);
2175 if (Packet
->CdbLength
> 16) {
2177 // The CDB exceeds 16 bytes. An extended CDB AHS is required.
2179 AHSLength
= (UINT8
) (AHSLength
+ ISCSI_ROUNDUP (Packet
->CdbLength
- 16) + sizeof (ISCSI_ADDITIONAL_HEADER
));
2182 Length
= sizeof (SCSI_COMMAND
) + AHSLength
;
2183 PduHeader
= NetbufAlloc (Length
);
2184 if (PduHeader
== NULL
) {
2188 ScsiCmd
= (SCSI_COMMAND
*) NetbufAllocSpace (PduHeader
, Length
, NET_BUF_TAIL
);
2189 if (ScsiCmd
== NULL
) {
2190 NetbufFree (PduHeader
);
2193 Header
= (ISCSI_ADDITIONAL_HEADER
*) (ScsiCmd
+ 1);
2195 ZeroMem (ScsiCmd
, Length
);
2197 ISCSI_SET_OPCODE (ScsiCmd
, ISCSI_OPCODE_SCSI_CMD
, 0);
2198 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_TASK_ATTR_SIMPLE
);
2201 // Set the READ/WRITE flags according to the IO type of this request.
2203 switch (Packet
->DataDirection
) {
2205 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
);
2206 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->InTransferLength
);
2210 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_WRITE
);
2211 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
2215 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
| SCSI_CMD_PDU_FLAG_WRITE
);
2216 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
2219 // Fill the bidirectional expected read data length AHS.
2221 BiExpReadDataLenAHS
= (ISCSI_BI_EXP_READ_DATA_LEN_AHS
*) Header
;
2222 Header
= (ISCSI_ADDITIONAL_HEADER
*) (BiExpReadDataLenAHS
+ 1);
2224 BiExpReadDataLenAHS
->Length
= NTOHS (5);
2225 BiExpReadDataLenAHS
->Type
= ISCSI_AHS_TYPE_BI_EXP_READ_DATA_LEN
;
2226 BiExpReadDataLenAHS
->ExpReadDataLength
= NTOHL (Packet
->InTransferLength
);
2231 ScsiCmd
->TotalAHSLength
= AHSLength
;
2232 CopyMem (ScsiCmd
->Lun
, &Lun
, sizeof (ScsiCmd
->Lun
));
2233 ScsiCmd
->InitiatorTaskTag
= NTOHL (Tcb
->InitiatorTaskTag
);
2234 ScsiCmd
->CmdSN
= NTOHL (Tcb
->CmdSN
);
2235 ScsiCmd
->ExpStatSN
= NTOHL (Tcb
->Conn
->ExpStatSN
);
2237 CopyMem (ScsiCmd
->Cdb
, Packet
->Cdb
, sizeof (ScsiCmd
->Cdb
));
2239 if (Packet
->CdbLength
> 16) {
2240 Header
->Length
= NTOHS ((UINT16
) (Packet
->CdbLength
- 15));
2241 Header
->Type
= ISCSI_AHS_TYPE_EXT_CDB
;
2243 CopyMem (Header
+ 1, (UINT8
*) Packet
->Cdb
+ 16, Packet
->CdbLength
- 16);
2247 Session
= Tcb
->Conn
->Session
;
2248 ImmediateDataLen
= 0;
2250 if (Session
->ImmediateData
&& (Packet
->OutTransferLength
!= 0)) {
2252 // Send immediate data in this SCSI Command PDU. The length of the immeidate
2253 // data is the minimum of FirstBurstLength, the data length to be xfered, and
2254 // the MaxRecvdataSegmentLength on this connection.
2256 ImmediateDataLen
= MIN (Session
->FirstBurstLength
, Packet
->OutTransferLength
);
2257 ImmediateDataLen
= MIN (ImmediateDataLen
, Tcb
->Conn
->MaxRecvDataSegmentLength
);
2260 // Update the data segment length in the PDU header.
2262 ISCSI_SET_DATASEG_LEN (ScsiCmd
, ImmediateDataLen
);
2265 // Create the data segment.
2267 DataSeg
= IScsiNewDataSegment ((UINT8
*) Packet
->OutDataBuffer
, ImmediateDataLen
, FALSE
);
2268 if (DataSeg
== NULL
) {
2269 NetbufFree (PduHeader
);
2274 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
2275 if (NbufList
== NULL
) {
2276 NetbufFree (PduHeader
);
2277 NetbufFree (DataSeg
);
2283 InitializeListHead (NbufList
);
2284 InsertTailList (NbufList
, &PduHeader
->List
);
2285 InsertTailList (NbufList
, &DataSeg
->List
);
2287 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
2289 IScsiFreeNbufList (NbufList
);
2293 if (Session
->InitialR2T
||
2294 (ImmediateDataLen
== Session
->FirstBurstLength
) ||
2295 (ImmediateDataLen
== Packet
->OutTransferLength
)
2298 // Unsolicited data out sequence is not allowed,
2299 // or FirstBustLength data is already sent out by immediate data,
2300 // or all the OUT data accompany this SCSI packet are sent as
2301 // immediate data. The final flag should be set on this SCSI Command
2304 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_BHS_FLAG_FINAL
);
2314 Create a new iSCSI SCSI Data Out PDU.
2316 @param[in] Data The data to put into the Data Out PDU.
2317 @param[in] Len Length of the data.
2318 @param[in] DataSN The DataSN of the Data Out PDU.
2319 @param[in] Tcb The task control block of this Data Out PDU.
2320 @param[in] Lun The LUN.
2322 @return The net buffer wrapping the Data Out PDU.
2323 @retval NULL Other errors as indicated.
2327 IScsiNewDataOutPdu (
2335 LIST_ENTRY
*NbufList
;
2339 ISCSI_SCSI_DATA_OUT
*DataOutHdr
;
2340 ISCSI_XFER_CONTEXT
*XferContext
;
2342 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
2343 if (NbufList
== NULL
) {
2347 InitializeListHead (NbufList
);
2350 // Allocate memory for the BHS.
2352 PduHdr
= NetbufAlloc (sizeof (ISCSI_SCSI_DATA_OUT
));
2353 if (PduHdr
== NULL
) {
2354 FreePool (NbufList
);
2358 // Insert the BHS into the buffer list.
2360 InsertTailList (NbufList
, &PduHdr
->List
);
2362 DataOutHdr
= (ISCSI_SCSI_DATA_OUT
*) NetbufAllocSpace (PduHdr
, sizeof (ISCSI_SCSI_DATA_OUT
), NET_BUF_TAIL
);
2363 if (DataOutHdr
== NULL
) {
2364 IScsiFreeNbufList (NbufList
);
2367 XferContext
= &Tcb
->XferContext
;
2369 ZeroMem (DataOutHdr
, sizeof (ISCSI_SCSI_DATA_OUT
));
2372 // Set the flags and fields of the Data Out PDU BHS.
2374 ISCSI_SET_OPCODE (DataOutHdr
, ISCSI_OPCODE_SCSI_DATA_OUT
, 0);
2375 ISCSI_SET_DATASEG_LEN (DataOutHdr
, Len
);
2377 DataOutHdr
->InitiatorTaskTag
= HTONL (Tcb
->InitiatorTaskTag
);
2378 DataOutHdr
->TargetTransferTag
= HTONL (XferContext
->TargetTransferTag
);
2379 DataOutHdr
->ExpStatSN
= HTONL (Tcb
->Conn
->ExpStatSN
);
2380 DataOutHdr
->DataSN
= HTONL (DataSN
);
2381 DataOutHdr
->BufferOffset
= HTONL (XferContext
->Offset
);
2383 if (XferContext
->TargetTransferTag
!= ISCSI_RESERVED_TAG
) {
2384 CopyMem (&DataOutHdr
->Lun
, &Lun
, sizeof (DataOutHdr
->Lun
));
2387 // Build the data segment for this Data Out PDU.
2389 DataSeg
= IScsiNewDataSegment (Data
, Len
, FALSE
);
2390 if (DataSeg
== NULL
) {
2391 IScsiFreeNbufList (NbufList
);
2395 // Put the data segment into the buffer list and combine it with the BHS
2396 // into a full Data Out PDU.
2398 InsertTailList (NbufList
, &DataSeg
->List
);
2399 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
2401 IScsiFreeNbufList (NbufList
);
2409 Generate a consecutive sequence of iSCSI SCSI Data Out PDUs.
2411 @param[in] Data The data which will be carried by the sequence of iSCSI SCSI Data Out PDUs.
2412 @param[in] Tcb The task control block of the data to send out.
2413 @param[in] Lun The LUN the data will be sent to.
2415 @return A list of net buffers with each of them wrapping an iSCSI SCSI Data Out PDU.
2416 @retval NULL Other errors as indicated.
2420 IScsiGenerateDataOutPduSequence (
2426 LIST_ENTRY
*PduList
;
2429 NET_BUF
*DataOutPdu
;
2430 ISCSI_CONNECTION
*Conn
;
2431 ISCSI_XFER_CONTEXT
*XferContext
;
2432 UINT8
*DataOutPacket
;
2434 PduList
= AllocatePool (sizeof (LIST_ENTRY
));
2435 if (PduList
== NULL
) {
2439 InitializeListHead (PduList
);
2444 XferContext
= &Tcb
->XferContext
;
2446 while (XferContext
->DesiredLength
> 0) {
2448 // Determine the length of data this Data Out PDU can carry.
2450 DataLen
= MIN (XferContext
->DesiredLength
, Conn
->MaxRecvDataSegmentLength
);
2453 // Create a Data Out PDU.
2455 DataOutPdu
= IScsiNewDataOutPdu (Data
, DataLen
, DataSN
, Tcb
, Lun
);
2456 if (DataOutPdu
== NULL
) {
2457 IScsiFreeNbufList (PduList
);
2463 InsertTailList (PduList
, &DataOutPdu
->List
);
2466 // Update the context and DataSN.
2469 XferContext
->Offset
+= DataLen
;
2470 XferContext
->DesiredLength
-= DataLen
;
2474 // Set the F bit for the last data out PDU in this sequence.
2476 DataOutPacket
= NetbufGetByte (DataOutPdu
, 0, NULL
);
2477 if (DataOutPacket
== NULL
) {
2478 IScsiFreeNbufList (PduList
);
2483 ISCSI_SET_FLAG (DataOutPacket
, ISCSI_BHS_FLAG_FINAL
);
2491 Send the Data in a sequence of Data Out PDUs one by one.
2493 @param[in] Data The data to carry by Data Out PDUs.
2494 @param[in] Lun The LUN the data will be sent to.
2495 @param[in] Tcb The task control block.
2497 @retval EFI_SUCCES The data is sent out to the LUN.
2498 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2499 @retval Others Other errors as indicated.
2503 IScsiSendDataOutPduSequence (
2509 LIST_ENTRY
*DataOutPduList
;
2515 // Generate the Data Out PDU sequence.
2517 DataOutPduList
= IScsiGenerateDataOutPduSequence (Data
, Tcb
, Lun
);
2518 if (DataOutPduList
== NULL
) {
2519 return EFI_OUT_OF_RESOURCES
;
2522 Status
= EFI_SUCCESS
;
2525 // Send the Data Out PDU's one by one.
2527 NET_LIST_FOR_EACH (Entry
, DataOutPduList
) {
2528 Pdu
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
2530 Status
= TcpIoTransmit (&Tcb
->Conn
->TcpIo
, Pdu
);
2532 if (EFI_ERROR (Status
)) {
2537 IScsiFreeNbufList (DataOutPduList
);
2544 Process the received iSCSI SCSI Data In PDU.
2546 @param[in] Pdu The Data In PDU received.
2547 @param[in] Tcb The task control block.
2548 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2550 @retval EFI_SUCCES The check on the Data IN PDU is passed and some update
2552 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror occurred.
2553 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2554 @retval Others Other errors as indicated.
2561 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2564 ISCSI_SCSI_DATA_IN
*DataInHdr
;
2567 DataInHdr
= (ISCSI_SCSI_DATA_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2568 if (DataInHdr
== NULL
) {
2569 return EFI_PROTOCOL_ERROR
;
2572 DataInHdr
->InitiatorTaskTag
= NTOHL (DataInHdr
->InitiatorTaskTag
);
2573 DataInHdr
->ExpCmdSN
= NTOHL (DataInHdr
->ExpCmdSN
);
2574 DataInHdr
->MaxCmdSN
= NTOHL (DataInHdr
->MaxCmdSN
);
2575 DataInHdr
->DataSN
= NTOHL (DataInHdr
->DataSN
);
2578 // Check the DataSN.
2580 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, DataInHdr
->DataSN
);
2581 if (EFI_ERROR (Status
)) {
2585 if (DataInHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2586 return EFI_PROTOCOL_ERROR
;
2589 // Update the command related sequence numbers.
2591 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, DataInHdr
->MaxCmdSN
, DataInHdr
->ExpCmdSN
);
2593 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_STATUS_VALID
)) {
2594 if (!ISCSI_FLAG_ON (DataInHdr
, ISCSI_BHS_FLAG_FINAL
)) {
2596 // The S bit is on but the F bit is off.
2598 return EFI_PROTOCOL_ERROR
;
2601 Tcb
->StatusXferd
= TRUE
;
2603 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_OVERFLOW
| SCSI_DATA_IN_PDU_FLAG_UNDERFLOW
)) {
2605 // Underflow and Overflow are mutual flags.
2607 return EFI_PROTOCOL_ERROR
;
2610 // S bit is on, the StatSN is valid.
2612 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NTOHL (DataInHdr
->StatSN
));
2613 if (EFI_ERROR (Status
)) {
2617 Packet
->HostAdapterStatus
= 0;
2618 Packet
->TargetStatus
= DataInHdr
->Status
;
2620 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2621 Packet
->InTransferLength
+= NTOHL (DataInHdr
->ResidualCount
);
2622 Status
= EFI_BAD_BUFFER_SIZE
;
2625 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2626 Packet
->InTransferLength
-= NTOHL (DataInHdr
->ResidualCount
);
2635 Process the received iSCSI R2T PDU.
2637 @param[in] Pdu The R2T PDU received.
2638 @param[in] Tcb The task control block.
2639 @param[in] Lun The Lun.
2640 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2642 @retval EFI_SUCCES The R2T PDU is valid and the solicited data is sent out.
2643 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror occurred.
2644 @retval Others Other errors as indicated.
2652 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2655 ISCSI_READY_TO_TRANSFER
*R2THdr
;
2657 ISCSI_XFER_CONTEXT
*XferContext
;
2660 R2THdr
= (ISCSI_READY_TO_TRANSFER
*) NetbufGetByte (Pdu
, 0, NULL
);
2661 if (R2THdr
== NULL
) {
2662 return EFI_PROTOCOL_ERROR
;
2665 R2THdr
->InitiatorTaskTag
= NTOHL (R2THdr
->InitiatorTaskTag
);
2666 R2THdr
->TargetTransferTag
= NTOHL (R2THdr
->TargetTransferTag
);
2667 R2THdr
->StatSN
= NTOHL (R2THdr
->StatSN
);
2668 R2THdr
->R2TSeqNum
= NTOHL (R2THdr
->R2TSeqNum
);
2669 R2THdr
->BufferOffset
= NTOHL (R2THdr
->BufferOffset
);
2670 R2THdr
->DesiredDataTransferLength
= NTOHL (R2THdr
->DesiredDataTransferLength
);
2672 if ((R2THdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) || !ISCSI_SEQ_EQ (R2THdr
->StatSN
, Tcb
->Conn
->ExpStatSN
)) {
2673 return EFI_PROTOCOL_ERROR
;;
2676 // Check the sequence number.
2678 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, R2THdr
->R2TSeqNum
);
2679 if (EFI_ERROR (Status
)) {
2683 XferContext
= &Tcb
->XferContext
;
2684 XferContext
->TargetTransferTag
= R2THdr
->TargetTransferTag
;
2685 XferContext
->Offset
= R2THdr
->BufferOffset
;
2686 XferContext
->DesiredLength
= R2THdr
->DesiredDataTransferLength
;
2688 if (((XferContext
->Offset
+ XferContext
->DesiredLength
) > Packet
->OutTransferLength
) ||
2689 (XferContext
->DesiredLength
> Tcb
->Conn
->Session
->MaxBurstLength
)
2691 return EFI_PROTOCOL_ERROR
;
2694 // Send the data solicited by this R2T.
2696 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2697 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2704 Process the received iSCSI SCSI Response PDU.
2706 @param[in] Pdu The Response PDU received.
2707 @param[in] Tcb The task control block.
2708 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2710 @retval EFI_SUCCES The Response PDU is processed.
2711 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror occurred.
2712 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2713 @retval Others Other errors as indicated.
2717 IScsiOnScsiRspRcvd (
2720 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2723 SCSI_RESPONSE
*ScsiRspHdr
;
2724 ISCSI_SENSE_DATA
*SenseData
;
2728 ScsiRspHdr
= (SCSI_RESPONSE
*) NetbufGetByte (Pdu
, 0, NULL
);
2729 if (ScsiRspHdr
== NULL
) {
2730 return EFI_PROTOCOL_ERROR
;
2733 ScsiRspHdr
->InitiatorTaskTag
= NTOHL (ScsiRspHdr
->InitiatorTaskTag
);
2734 if (ScsiRspHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2735 return EFI_PROTOCOL_ERROR
;
2738 ScsiRspHdr
->StatSN
= NTOHL (ScsiRspHdr
->StatSN
);
2740 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, ScsiRspHdr
->StatSN
);
2741 if (EFI_ERROR (Status
)) {
2745 ScsiRspHdr
->MaxCmdSN
= NTOHL (ScsiRspHdr
->MaxCmdSN
);
2746 ScsiRspHdr
->ExpCmdSN
= NTOHL (ScsiRspHdr
->ExpCmdSN
);
2747 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, ScsiRspHdr
->MaxCmdSN
, ScsiRspHdr
->ExpCmdSN
);
2749 Tcb
->StatusXferd
= TRUE
;
2751 Packet
->HostAdapterStatus
= ScsiRspHdr
->Response
;
2752 if (Packet
->HostAdapterStatus
!= ISCSI_SERVICE_RSP_COMMAND_COMPLETE_AT_TARGET
) {
2756 Packet
->TargetStatus
= ScsiRspHdr
->Status
;
2758 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
| SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
) ||
2759 ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
| SCSI_RSP_PDU_FLAG_UNDERFLOW
)
2761 return EFI_PROTOCOL_ERROR
;
2764 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
)) {
2765 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2766 Status
= EFI_BAD_BUFFER_SIZE
;
2769 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
)) {
2770 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2773 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2774 if (Packet
->DataDirection
== DataIn
) {
2775 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2777 Packet
->OutTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2780 Status
= EFI_BAD_BUFFER_SIZE
;
2783 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2784 if (Packet
->DataDirection
== DataIn
) {
2785 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2787 Packet
->OutTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2791 DataSegLen
= ISCSI_GET_DATASEG_LEN (ScsiRspHdr
);
2792 if (DataSegLen
!= 0) {
2793 SenseData
= (ISCSI_SENSE_DATA
*) NetbufGetByte (Pdu
, sizeof (SCSI_RESPONSE
), NULL
);
2794 if (SenseData
== NULL
) {
2795 return EFI_PROTOCOL_ERROR
;
2798 SenseData
->Length
= NTOHS (SenseData
->Length
);
2800 Packet
->SenseDataLength
= (UINT8
) MIN (SenseData
->Length
, Packet
->SenseDataLength
);
2801 if (Packet
->SenseDataLength
!= 0) {
2802 CopyMem (Packet
->SenseData
, &SenseData
->Data
[0], Packet
->SenseDataLength
);
2805 Packet
->SenseDataLength
= 0;
2813 Process the received NOP In PDU.
2815 @param[in] Pdu The NOP In PDU received.
2816 @param[in] Tcb The task control block.
2818 @retval EFI_SUCCES The NOP In PDU is processed and the related sequence
2819 numbers are updated.
2820 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror occurred.
2829 ISCSI_NOP_IN
*NopInHdr
;
2832 NopInHdr
= (ISCSI_NOP_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2833 if (NopInHdr
== NULL
) {
2834 return EFI_PROTOCOL_ERROR
;
2837 NopInHdr
->StatSN
= NTOHL (NopInHdr
->StatSN
);
2838 NopInHdr
->ExpCmdSN
= NTOHL (NopInHdr
->ExpCmdSN
);
2839 NopInHdr
->MaxCmdSN
= NTOHL (NopInHdr
->MaxCmdSN
);
2841 if (NopInHdr
->InitiatorTaskTag
== ISCSI_RESERVED_TAG
) {
2842 if (NopInHdr
->StatSN
!= Tcb
->Conn
->ExpStatSN
) {
2843 return EFI_PROTOCOL_ERROR
;
2846 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NopInHdr
->StatSN
);
2847 if (EFI_ERROR (Status
)) {
2852 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, NopInHdr
->MaxCmdSN
, NopInHdr
->ExpCmdSN
);
2859 Execute the SCSI command issued through the EXT SCSI PASS THRU protocol.
2861 @param[in] PassThru The EXT SCSI PASS THRU protocol.
2862 @param[in] Target The target ID.
2863 @param[in] Lun The LUN.
2864 @param[in, out] Packet The request packet containing IO request, SCSI command
2865 buffer and buffers to read/write.
2867 @retval EFI_SUCCES The SCSI command is executed and the result is updated to
2869 @retval EFI_DEVICE_ERROR Session state was not as required.
2870 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2871 @retval EFI_PROTOCOL_ERROR There is no such data in the net buffer.
2872 @retval EFI_NOT_READY The target can not accept new commands.
2873 @retval Others Other errors as indicated.
2877 IScsiExecuteScsiCommand (
2878 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*PassThru
,
2881 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2885 ISCSI_DRIVER_DATA
*Private
;
2886 ISCSI_SESSION
*Session
;
2887 EFI_EVENT TimeoutEvent
;
2888 ISCSI_CONNECTION
*Conn
;
2891 ISCSI_XFER_CONTEXT
*XferContext
;
2893 ISCSI_IN_BUFFER_CONTEXT InBufferContext
;
2897 Private
= ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru
);
2898 Session
= Private
->Session
;
2899 Status
= EFI_SUCCESS
;
2901 TimeoutEvent
= NULL
;
2904 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
2905 Status
= EFI_DEVICE_ERROR
;
2909 Conn
= NET_LIST_USER_STRUCT_S (
2910 Session
->Conns
.ForwardLink
,
2913 ISCSI_CONNECTION_SIGNATURE
2916 if (Packet
->Timeout
!= 0) {
2917 Timeout
= MultU64x32 (Packet
->Timeout
, 4);
2920 Status
= IScsiNewTcb (Conn
, &Tcb
);
2921 if (EFI_ERROR (Status
)) {
2925 // Encapsulate the SCSI request packet into an iSCSI SCSI Command PDU.
2927 Pdu
= IScsiNewScsiCmdPdu (Packet
, Lun
, Tcb
);
2929 Status
= EFI_OUT_OF_RESOURCES
;
2933 XferContext
= &Tcb
->XferContext
;
2934 PduHdr
= NetbufGetByte (Pdu
, 0, NULL
);
2935 if (PduHdr
== NULL
) {
2936 Status
= EFI_PROTOCOL_ERROR
;
2940 XferContext
->Offset
= ISCSI_GET_DATASEG_LEN (PduHdr
);
2943 // Transmit the SCSI Command PDU.
2945 Status
= TcpIoTransmit (&Conn
->TcpIo
, Pdu
);
2949 if (EFI_ERROR (Status
)) {
2953 if (!Session
->InitialR2T
&&
2954 (XferContext
->Offset
< Session
->FirstBurstLength
) &&
2955 (XferContext
->Offset
< Packet
->OutTransferLength
)
2958 // Unsolicited Data-Out sequence is allowed. There is remaining SCSI
2959 // OUT data, and the limit of FirstBurstLength is not reached.
2961 XferContext
->TargetTransferTag
= ISCSI_RESERVED_TAG
;
2962 XferContext
->DesiredLength
= MIN (
2963 Session
->FirstBurstLength
,
2964 Packet
->OutTransferLength
- XferContext
->Offset
2967 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2968 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2969 if (EFI_ERROR (Status
)) {
2974 InBufferContext
.InData
= (UINT8
*) Packet
->InDataBuffer
;
2975 InBufferContext
.InDataLen
= Packet
->InTransferLength
;
2977 while (!Tcb
->StatusXferd
) {
2979 // Start the timeout timer.
2982 Status
= gBS
->SetTimer (Conn
->TimeoutEvent
, TimerRelative
, Timeout
);
2983 if (EFI_ERROR (Status
)) {
2987 TimeoutEvent
= Conn
->TimeoutEvent
;
2991 // Try to receive PDU from target.
2993 Status
= IScsiReceivePdu (Conn
, &Pdu
, &InBufferContext
, FALSE
, FALSE
, TimeoutEvent
);
2994 if (EFI_ERROR (Status
)) {
2998 PduHdr
= NetbufGetByte (Pdu
, 0, NULL
);
2999 if (PduHdr
== NULL
) {
3000 Status
= EFI_PROTOCOL_ERROR
;
3004 switch (ISCSI_GET_OPCODE (PduHdr
)) {
3005 case ISCSI_OPCODE_SCSI_DATA_IN
:
3006 Status
= IScsiOnDataInRcvd (Pdu
, Tcb
, Packet
);
3009 case ISCSI_OPCODE_R2T
:
3010 Status
= IScsiOnR2TRcvd (Pdu
, Tcb
, Lun
, Packet
);
3013 case ISCSI_OPCODE_SCSI_RSP
:
3014 Status
= IScsiOnScsiRspRcvd (Pdu
, Tcb
, Packet
);
3017 case ISCSI_OPCODE_NOP_IN
:
3018 Status
= IScsiOnNopInRcvd (Pdu
, Tcb
);
3021 case ISCSI_OPCODE_VENDOR_T0
:
3022 case ISCSI_OPCODE_VENDOR_T1
:
3023 case ISCSI_OPCODE_VENDOR_T2
:
3025 // These messages are vendor specific. Skip them.
3030 Status
= EFI_PROTOCOL_ERROR
;
3036 if (EFI_ERROR (Status
)) {
3043 if (TimeoutEvent
!= NULL
) {
3044 gBS
->SetTimer (TimeoutEvent
, TimerCancel
, 0);
3056 Reinstate the session on some error.
3058 @param[in] Session The iSCSI session
3060 @retval EFI_SUCCESS The session is reinstated from some error.
3061 @retval Other Reinstatement failed.
3065 IScsiSessionReinstatement (
3066 IN ISCSI_SESSION
*Session
3071 ASSERT (Session
->State
!= SESSION_STATE_FREE
);
3074 // Abort the session and re-init it.
3076 IScsiSessionAbort (Session
);
3077 IScsiSessionInit (Session
, TRUE
);
3082 Status
= IScsiSessionLogin (Session
);
3089 Initialize some session parameters before login.
3091 @param[in, out] Session The iSCSI session.
3092 @param[in] Recovery Whether the request is from a fresh new start or recovery.
3097 IN OUT ISCSI_SESSION
*Session
,
3102 Session
->Signature
= ISCSI_SESSION_SIGNATURE
;
3103 Session
->State
= SESSION_STATE_FREE
;
3105 InitializeListHead (&Session
->Conns
);
3106 InitializeListHead (&Session
->TcbList
);
3112 Session
->InitiatorTaskTag
= 1;
3113 Session
->NextCid
= 1;
3115 Session
->TargetPortalGroupTag
= 0;
3116 Session
->MaxConnections
= ISCSI_MAX_CONNS_PER_SESSION
;
3117 Session
->InitialR2T
= FALSE
;
3118 Session
->ImmediateData
= TRUE
;
3119 Session
->MaxBurstLength
= 262144;
3120 Session
->FirstBurstLength
= MAX_RECV_DATA_SEG_LEN_IN_FFP
;
3121 Session
->DefaultTime2Wait
= 2;
3122 Session
->DefaultTime2Retain
= 20;
3123 Session
->MaxOutstandingR2T
= DEFAULT_MAX_OUTSTANDING_R2T
;
3124 Session
->DataPDUInOrder
= TRUE
;
3125 Session
->DataSequenceInOrder
= TRUE
;
3126 Session
->ErrorRecoveryLevel
= 0;
3131 Abort the iSCSI session. That is, reset all the connection(s), and free the
3134 @param[in, out] Session The iSCSI session.
3139 IN OUT ISCSI_SESSION
*Session
3142 ISCSI_CONNECTION
*Conn
;
3143 EFI_GUID
*ProtocolGuid
;
3145 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
3149 ASSERT (!IsListEmpty (&Session
->Conns
));
3151 while (!IsListEmpty (&Session
->Conns
)) {
3152 Conn
= NET_LIST_USER_STRUCT_S (
3153 Session
->Conns
.ForwardLink
,
3156 ISCSI_CONNECTION_SIGNATURE
3158 if (!Conn
->Ipv6Flag
) {
3159 ProtocolGuid
= &gEfiTcp4ProtocolGuid
;
3161 ProtocolGuid
= &gEfiTcp6ProtocolGuid
;
3164 gBS
->CloseProtocol (
3167 Session
->Private
->Image
,
3168 Session
->Private
->ExtScsiPassThruHandle
3171 IScsiConnReset (Conn
);
3173 IScsiDetatchConnection (Conn
);
3174 IScsiDestroyConnection (Conn
);
3177 Session
->State
= SESSION_STATE_FAILED
;