2 The implementation of iSCSI protocol based on RFC3720.
4 Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "IScsiImpl.h"
17 UINT32 mDataSegPad
= 0;
20 Attach the iSCSI connection to the iSCSI session.
22 @param[in, out] Session The iSCSI session.
23 @param[in, out] Conn The iSCSI connection.
27 IScsiAttatchConnection (
28 IN OUT ISCSI_SESSION
*Session
,
29 IN OUT ISCSI_CONNECTION
*Conn
32 InsertTailList (&Session
->Conns
, &Conn
->Link
);
33 Conn
->Session
= Session
;
38 Detach the iSCSI connection from the session it belongs to.
40 @param[in, out] Conn The iSCSI connection.
44 IScsiDetatchConnection (
45 IN OUT ISCSI_CONNECTION
*Conn
48 RemoveEntryList (&Conn
->Link
);
49 Conn
->Session
->NumConns
--;
55 Check the sequence number according to RFC3720.
57 @param[in, out] ExpSN The currently expected sequence number.
58 @param[in] NewSN The sequence number to check.
60 @retval EFI_SUCCESS The check passed and the ExpSN is increased.
61 @retval EFI_NOT_READY Response was sent due to a retransmission request.
62 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred.
71 if (!ISCSI_SEQ_EQ (NewSN
, *ExpSN
)) {
72 if (ISCSI_SEQ_LT (NewSN
, *ExpSN
)) {
78 return EFI_PROTOCOL_ERROR
;
91 Update the sequence numbers for the iSCSI command.
93 @param[in, out] Session The iSCSI session.
94 @param[in] MaxCmdSN Maximum CmdSN from the target.
95 @param[in] ExpCmdSN Next expected CmdSN from the target.
100 IN OUT ISCSI_SESSION
*Session
,
105 if (ISCSI_SEQ_LT (MaxCmdSN
, ExpCmdSN
- 1)) {
109 if (ISCSI_SEQ_GT (MaxCmdSN
, Session
->MaxCmdSN
)) {
110 Session
->MaxCmdSN
= MaxCmdSN
;
113 if (ISCSI_SEQ_GT (ExpCmdSN
, Session
->ExpCmdSN
)) {
114 Session
->ExpCmdSN
= ExpCmdSN
;
120 This function does the iSCSI connection login.
122 @param[in, out] Conn The iSCSI connection to login.
123 @param Timeout The timeout value in millisecond.
125 @retval EFI_SUCCESS The iSCSI connection is logged into the iSCSI target.
126 @retval EFI_TIMEOUT Timeout occurred during the login procedure.
127 @retval Others Other errors as indicated.
132 IN OUT ISCSI_CONNECTION
*Conn
,
139 // Start the timer, and wait Timeout seconds to establish the TCP connection.
141 Status
= gBS
->SetTimer (
144 MultU64x32 (Timeout
, TICKS_PER_MS
)
146 if (EFI_ERROR (Status
)) {
151 // Try to establish the tcp connection.
153 Status
= TcpIoConnect (&Conn
->TcpIo
, Conn
->TimeoutEvent
);
154 gBS
->SetTimer (Conn
->TimeoutEvent
, TimerCancel
, 0);
156 if (EFI_ERROR (Status
)) {
160 Conn
->State
= CONN_STATE_IN_LOGIN
;
163 // Connection is established, start the iSCSI Login.
166 Status
= IScsiSendLoginReq (Conn
);
167 if (EFI_ERROR (Status
)) {
171 Status
= IScsiReceiveLoginRsp (Conn
);
172 if (EFI_ERROR (Status
)) {
175 } while (Conn
->CurrentStage
!= ISCSI_FULL_FEATURE_PHASE
);
182 Reset the iSCSI connection.
184 @param[in, out] Conn The iSCSI connection to reset.
189 IN OUT ISCSI_CONNECTION
*Conn
192 TcpIoReset (&Conn
->TcpIo
);
197 Create a TCP connection for the iSCSI session.
199 @param[in] Session Points to the iSCSI session.
201 @return The newly created iSCSI connection.
205 IScsiCreateConnection (
206 IN ISCSI_SESSION
*Session
209 ISCSI_DRIVER_DATA
*Private
;
210 ISCSI_SESSION_CONFIG_NVDATA
*NvData
;
211 ISCSI_CONNECTION
*Conn
;
212 TCP_IO_CONFIG_DATA TcpIoConfig
;
213 TCP4_IO_CONFIG_DATA
*Tcp4IoConfig
;
214 TCP6_IO_CONFIG_DATA
*Tcp6IoConfig
;
217 Private
= Session
->Private
;
218 NvData
= &Session
->ConfigData
->SessionConfigData
;
220 Conn
= AllocateZeroPool (sizeof (ISCSI_CONNECTION
));
225 Conn
->Signature
= ISCSI_CONNECTION_SIGNATURE
;
226 Conn
->State
= CONN_STATE_FREE
;
227 Conn
->CurrentStage
= ISCSI_SECURITY_NEGOTIATION
;
228 Conn
->NextStage
= ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
;
229 Conn
->AuthStep
= ISCSI_AUTH_INITIAL
;
231 Conn
->PartialReqSent
= FALSE
;
232 Conn
->PartialRspRcvd
= FALSE
;
233 Conn
->ParamNegotiated
= FALSE
;
234 Conn
->Cid
= Session
->NextCid
++;
235 Conn
->Ipv6Flag
= NvData
->IpMode
== IP_MODE_IP6
|| Session
->ConfigData
->AutoConfigureMode
== IP_MODE_AUTOCONFIG_IP6
;
237 Status
= gBS
->CreateEvent (
244 if (EFI_ERROR (Status
)) {
249 NetbufQueInit (&Conn
->RspQue
);
252 // Set the default connection-only parameters.
254 Conn
->MaxRecvDataSegmentLength
= DEFAULT_MAX_RECV_DATA_SEG_LEN
;
255 Conn
->HeaderDigest
= IScsiDigestNone
;
256 Conn
->DataDigest
= IScsiDigestNone
;
258 if (!Conn
->Ipv6Flag
) {
259 Tcp4IoConfig
= &TcpIoConfig
.Tcp4IoConfigData
;
261 CopyMem (&Tcp4IoConfig
->LocalIp
, &NvData
->LocalIp
, sizeof (EFI_IPv4_ADDRESS
));
262 CopyMem (&Tcp4IoConfig
->SubnetMask
, &NvData
->SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
263 CopyMem (&Tcp4IoConfig
->Gateway
, &NvData
->Gateway
, sizeof (EFI_IPv4_ADDRESS
));
264 CopyMem (&Tcp4IoConfig
->RemoteIp
, &NvData
->TargetIp
, sizeof (EFI_IPv4_ADDRESS
));
266 Tcp4IoConfig
->RemotePort
= NvData
->TargetPort
;
267 Tcp4IoConfig
->ActiveFlag
= TRUE
;
268 Tcp4IoConfig
->StationPort
= 0;
270 Tcp6IoConfig
= &TcpIoConfig
.Tcp6IoConfigData
;
272 CopyMem (&Tcp6IoConfig
->RemoteIp
, &NvData
->TargetIp
, sizeof (EFI_IPv6_ADDRESS
));
273 Tcp6IoConfig
->RemotePort
= NvData
->TargetPort
;
274 Tcp6IoConfig
->ActiveFlag
= TRUE
;
275 Tcp6IoConfig
->StationPort
= 0;
279 // Create the TCP IO for this connection.
281 Status
= TcpIoCreateSocket (
284 (UINT8
) (!Conn
->Ipv6Flag
? TCP_VERSION_4
: TCP_VERSION_6
),
288 if (EFI_ERROR (Status
)) {
289 gBS
->CloseEvent (Conn
->TimeoutEvent
);
299 Destroy an iSCSI connection.
301 @param[in] Conn The connection to destroy.
305 IScsiDestroyConnection (
306 IN ISCSI_CONNECTION
*Conn
309 TcpIoDestroySocket (&Conn
->TcpIo
);
311 NetbufQueFlush (&Conn
->RspQue
);
312 gBS
->CloseEvent (Conn
->TimeoutEvent
);
317 Retrieve the IPv6 Address/Prefix/Gateway from the established TCP connection, these informations
318 will be filled in the iSCSI Boot Firmware Table.
320 @param[in] Conn The connection used in the iSCSI login phase.
322 @retval EFI_SUCCESS Get the NIC information successfully.
323 @retval Others Other errors as indicated.
328 IN ISCSI_CONNECTION
*Conn
331 ISCSI_SESSION_CONFIG_NVDATA
*NvData
;
332 EFI_TCP6_PROTOCOL
*Tcp6
;
333 EFI_IP6_MODE_DATA Ip6ModeData
;
335 EFI_IPv6_ADDRESS
*TargetIp
;
337 UINT8 SubnetPrefixLength
;
340 NvData
= &Conn
->Session
->ConfigData
->SessionConfigData
;
341 TargetIp
= &NvData
->TargetIp
.v6
;
342 Tcp6
= Conn
->TcpIo
.Tcp
.Tcp6
;
344 ZeroMem (&Ip6ModeData
, sizeof (EFI_IP6_MODE_DATA
));
345 Status
= Tcp6
->GetModeData (
353 if (EFI_ERROR (Status
)) {
357 if (!Ip6ModeData
.IsConfigured
) {
358 Status
= EFI_ABORTED
;
362 IP6_COPY_ADDRESS (&NvData
->LocalIp
, &Ip6ModeData
.ConfigData
.StationAddress
);
364 NvData
->PrefixLength
= 0;
365 for (Index
= 0; Index
< Ip6ModeData
.AddressCount
; Index
++) {
366 if (EFI_IP6_EQUAL (&NvData
->LocalIp
.v6
, &Ip6ModeData
.AddressList
[Index
].Address
)) {
367 NvData
->PrefixLength
= Ip6ModeData
.AddressList
[Index
].PrefixLength
;
372 SubnetPrefixLength
= 0;
373 RouteEntry
= Ip6ModeData
.RouteCount
;
374 for (Index
= 0; Index
< Ip6ModeData
.RouteCount
; Index
++) {
375 if (NetIp6IsNetEqual (TargetIp
, &Ip6ModeData
.RouteTable
[Index
].Destination
, Ip6ModeData
.RouteTable
[Index
].PrefixLength
)) {
376 if (SubnetPrefixLength
< Ip6ModeData
.RouteTable
[Index
].PrefixLength
) {
377 SubnetPrefixLength
= Ip6ModeData
.RouteTable
[Index
].PrefixLength
;
382 if (RouteEntry
!= Ip6ModeData
.RouteCount
) {
383 IP6_COPY_ADDRESS (&NvData
->Gateway
, &Ip6ModeData
.RouteTable
[RouteEntry
].Gateway
);
387 if (Ip6ModeData
.AddressList
!= NULL
) {
388 FreePool (Ip6ModeData
.AddressList
);
390 if (Ip6ModeData
.GroupTable
!= NULL
) {
391 FreePool (Ip6ModeData
.GroupTable
);
393 if (Ip6ModeData
.RouteTable
!= NULL
) {
394 FreePool (Ip6ModeData
.RouteTable
);
396 if (Ip6ModeData
.NeighborCache
!= NULL
) {
397 FreePool (Ip6ModeData
.NeighborCache
);
399 if (Ip6ModeData
.PrefixTable
!= NULL
) {
400 FreePool (Ip6ModeData
.PrefixTable
);
402 if (Ip6ModeData
.IcmpTypeList
!= NULL
) {
403 FreePool (Ip6ModeData
.IcmpTypeList
);
410 Login the iSCSI session.
412 @param[in] Session The iSCSI session.
414 @retval EFI_SUCCESS The iSCSI session login procedure finished.
415 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
416 @retval EFI_NO_MEDIA There was a media error.
417 @retval Others Other errors as indicated.
422 IN ISCSI_SESSION
*Session
426 ISCSI_CONNECTION
*Conn
;
428 EFI_GUID
*ProtocolGuid
;
430 BOOLEAN MediaPresent
;
433 // Check media status before session login.
436 NetLibDetectMedia (Session
->Private
->Controller
, &MediaPresent
);
442 // Set session identifier
444 CopyMem (Session
->Isid
, Session
->ConfigData
->SessionConfigData
.IsId
, 6);
450 // Create a connection for the session.
452 Conn
= IScsiCreateConnection (Session
);
454 return EFI_OUT_OF_RESOURCES
;
457 IScsiAttatchConnection (Session
, Conn
);
460 // Login througth the newly created connection.
462 Status
= IScsiConnLogin (Conn
, Session
->ConfigData
->SessionConfigData
.ConnectTimeout
);
463 if (EFI_ERROR (Status
)) {
464 IScsiConnReset (Conn
);
465 IScsiDetatchConnection (Conn
);
466 IScsiDestroyConnection (Conn
);
469 if (Status
!= EFI_TIMEOUT
) {
474 } while (RetryCount
<= Session
->ConfigData
->SessionConfigData
.ConnectRetryCount
);
476 if (!EFI_ERROR (Status
)) {
477 Session
->State
= SESSION_STATE_LOGGED_IN
;
479 if (!Conn
->Ipv6Flag
) {
480 ProtocolGuid
= &gEfiTcp4ProtocolGuid
;
482 ProtocolGuid
= &gEfiTcp6ProtocolGuid
;
485 Status
= gBS
->OpenProtocol (
489 Session
->Private
->Image
,
490 Session
->Private
->ExtScsiPassThruHandle
,
491 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
494 ASSERT_EFI_ERROR (Status
);
496 if (Conn
->Ipv6Flag
) {
497 Status
= IScsiGetIp6NicInfo (Conn
);
506 Wait for IPsec negotiation, then try to login the iSCSI session again.
508 @param[in] Session The iSCSI session.
510 @retval EFI_SUCCESS The iSCSI session login procedure finished.
511 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
512 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred.
516 IScsiSessionReLogin (
517 IN ISCSI_SESSION
*Session
522 EFI_STATUS TimerStatus
;
525 Status
= gBS
->CreateEvent (EVT_TIMER
, TPL_CALLBACK
, NULL
, NULL
, &Timer
);
526 if (EFI_ERROR (Status
)) {
530 Status
= gBS
->SetTimer (
533 ISCSI_WAIT_IPSEC_TIMEOUT
536 if (EFI_ERROR (Status
)) {
537 gBS
->CloseEvent (Timer
);
543 TimerStatus
= gBS
->CheckEvent (Timer
);
545 if (!EFI_ERROR (TimerStatus
)) {
546 Status
= IScsiSessionLogin (Session
);
549 } while (TimerStatus
== EFI_NOT_READY
);
551 gBS
->CloseEvent (Timer
);
557 Build and send the iSCSI login request to the iSCSI target according to
558 the current login stage.
560 @param[in] Conn The connection in the iSCSI login phase.
562 @retval EFI_SUCCESS The iSCSI login request PDU is built and sent on this
564 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
565 @retval EFI_DEVICE_ERROR Some kind of device error occurred.
570 IN ISCSI_CONNECTION
*Conn
577 // Build the Login Request PDU.
579 Pdu
= IScsiPrepareLoginReq (Conn
);
581 return EFI_DEVICE_ERROR
;
584 // Send it to the iSCSI target.
586 Status
= TcpIoTransmit (&Conn
->TcpIo
, Pdu
);
595 Receive and process the iSCSI login response.
597 @param[in] Conn The connection in the iSCSI login phase.
599 @retval EFI_SUCCESS The iSCSI login response PDU is received and processed.
600 @retval Others Other errors as indicated.
604 IScsiReceiveLoginRsp (
605 IN ISCSI_CONNECTION
*Conn
614 // Receive the iSCSI login response.
616 Status
= IScsiReceivePdu (Conn
, &Pdu
, NULL
, FALSE
, FALSE
, NULL
);
617 if (EFI_ERROR (Status
)) {
620 ASSERT (Pdu
!= NULL
);
623 // A Login Response is received; process it.
625 Status
= IScsiProcessLoginRsp (Conn
, Pdu
);
634 Add an iSCSI key-value pair as a string into the data segment of the Login Request PDU.
635 The DataSegmentLength and the actual size of the net buffer containing this PDU will be
638 @param[in, out] Pdu The iSCSI PDU whose data segment the key-value pair will
640 @param[in] Key The key name string.
641 @param[in] Value The value string.
643 @retval EFI_SUCCESS The key-value pair is added to the PDU's data segment and
644 the correspondence length fields are updated.
645 @retval EFI_OUT_OF_RESOURCES There is not enough space in the PDU to add the key-value
647 @retval EFI_PROTOCOL_ERROR There is no such data in the net buffer.
650 IScsiAddKeyValuePair (
660 ISCSI_LOGIN_REQUEST
*LoginReq
;
663 LoginReq
= (ISCSI_LOGIN_REQUEST
*) NetbufGetByte (Pdu
, 0, NULL
);
664 if (LoginReq
== NULL
) {
665 return EFI_PROTOCOL_ERROR
;
667 DataSegLen
= NTOH24 (LoginReq
->DataSegmentLength
);
669 KeyLen
= (UINT32
) AsciiStrLen (Key
);
670 ValueLen
= (UINT32
) AsciiStrLen (Value
);
673 // 1 byte for the key value separator '=' and 1 byte for the null
674 // delimiter after the value.
676 TotalLen
= KeyLen
+ 1 + ValueLen
+ 1;
679 // Allocate the space for the key-value pair.
681 Data
= (CHAR8
*) NetbufAllocSpace (Pdu
, TotalLen
, NET_BUF_TAIL
);
683 return EFI_OUT_OF_RESOURCES
;
688 CopyMem (Data
, Key
, KeyLen
);
697 CopyMem (Data
, Value
, ValueLen
);
703 // Update the DataSegmentLength
705 ISCSI_SET_DATASEG_LEN (LoginReq
, DataSegLen
+ TotalLen
);
712 Prepare the iSCSI login request to be sent according to the current login status.
714 @param[in, out] Conn The connection in the iSCSI login phase.
716 @return The pointer to the net buffer containing the iSCSI login request built.
717 @retval NULL Other errors as indicated.
721 IScsiPrepareLoginReq (
722 IN OUT ISCSI_CONNECTION
*Conn
725 ISCSI_SESSION
*Session
;
727 ISCSI_LOGIN_REQUEST
*LoginReq
;
730 Session
= Conn
->Session
;
732 Nbuf
= NetbufAlloc (sizeof (ISCSI_LOGIN_REQUEST
) + DEFAULT_MAX_RECV_DATA_SEG_LEN
);
737 LoginReq
= (ISCSI_LOGIN_REQUEST
*) NetbufAllocSpace (Nbuf
, sizeof (ISCSI_LOGIN_REQUEST
), NET_BUF_TAIL
);
738 if (LoginReq
== NULL
) {
742 ZeroMem (LoginReq
, sizeof (ISCSI_LOGIN_REQUEST
));
745 // Init the login request pdu
747 ISCSI_SET_OPCODE (LoginReq
, ISCSI_OPCODE_LOGIN_REQ
, ISCSI_REQ_IMMEDIATE
);
748 ISCSI_SET_STAGES (LoginReq
, Conn
->CurrentStage
, Conn
->NextStage
);
749 LoginReq
->VersionMax
= ISCSI_VERSION_MAX
;
750 LoginReq
->VersionMin
= ISCSI_VERSION_MIN
;
751 LoginReq
->Tsih
= HTONS (Session
->Tsih
);
752 LoginReq
->InitiatorTaskTag
= HTONL (Session
->InitiatorTaskTag
);
753 LoginReq
->Cid
= HTONS (Conn
->Cid
);
754 LoginReq
->CmdSN
= HTONL (Session
->CmdSN
);
757 // For the first Login Request on a coonection this is ExpStatSN for the
758 // old connection, and this field is only valid if the Login Request restarts
760 // For subsequent Login Requests it is used to acknowledge the Login Responses
761 // with their increasing StatSN values.
763 LoginReq
->ExpStatSN
= HTONL (Conn
->ExpStatSN
);
764 CopyMem (LoginReq
->Isid
, Session
->Isid
, sizeof (LoginReq
->Isid
));
766 if (Conn
->PartialRspRcvd
) {
768 // A partial response. The initiator must send an empty Login Request.
773 Status
= EFI_SUCCESS
;
775 switch (Conn
->CurrentStage
) {
776 case ISCSI_SECURITY_NEGOTIATION
:
778 // Both none authentication and CHAP authentication share the CHAP path.
781 if (Session
->AuthType
!= ISCSI_AUTH_TYPE_KRB
) {
782 Status
= IScsiCHAPToSendReq (Conn
, Nbuf
);
787 case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
:
789 // Only negotiate the paramter once.
791 if (!Conn
->ParamNegotiated
) {
792 IScsiFillOpParams (Conn
, Nbuf
);
795 ISCSI_SET_FLAG (LoginReq
, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT
);
800 // An error occurs...
802 Status
= EFI_DEVICE_ERROR
;
806 if (EFI_ERROR (Status
)) {
811 // Pad the data segment if needed.
813 IScsiPadSegment (Nbuf
, ISCSI_GET_DATASEG_LEN (LoginReq
));
815 // Check whether we will issue the stage transition signal?
817 Conn
->TransitInitiated
= ISCSI_FLAG_ON (LoginReq
, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT
);
825 Process the iSCSI Login Response.
827 @param[in, out] Conn The connection on which the iSCSI login response is received.
828 @param[in, out] Pdu The iSCSI login response PDU.
830 @retval EFI_SUCCESS The iSCSI login response PDU is processed, and all checks are passed.
831 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred.
832 @retval EFI_MEDIA_CHANGED Target is redirected.
833 @retval Others Other errors as indicated.
837 IScsiProcessLoginRsp (
838 IN OUT ISCSI_CONNECTION
*Conn
,
843 ISCSI_SESSION
*Session
;
844 ISCSI_LOGIN_RESPONSE
*LoginRsp
;
852 Status
= EFI_SUCCESS
;
853 Session
= Conn
->Session
;
855 LoginRsp
= (ISCSI_LOGIN_RESPONSE
*) NetbufGetByte (Pdu
, 0, NULL
);
856 if (LoginRsp
== NULL
) {
857 return EFI_PROTOCOL_ERROR
;
859 if (!ISCSI_CHECK_OPCODE (LoginRsp
, ISCSI_OPCODE_LOGIN_RSP
)) {
861 // It is not a Login Response.
863 return EFI_PROTOCOL_ERROR
;
866 // Get the data segment, if any.
868 DataSegLen
= ISCSI_GET_DATASEG_LEN (LoginRsp
);
869 if (DataSegLen
!= 0) {
870 DataSeg
= NetbufGetByte (Pdu
, sizeof (ISCSI_LOGIN_RESPONSE
), NULL
);
875 // Check the status class in the login response PDU.
877 switch (LoginRsp
->StatusClass
) {
878 case ISCSI_LOGIN_STATUS_SUCCESS
:
880 // Just break here; the response and the data segment will be processed later.
884 case ISCSI_LOGIN_STATUS_REDIRECTION
:
886 // The target may be moved to a different address.
888 if (DataSeg
== NULL
) {
889 return EFI_PROTOCOL_ERROR
;
892 // Process the TargetAddress key-value strings in the data segment to update the
893 // target address info.
895 Status
= IScsiUpdateTargetAddress (Session
, (CHAR8
*) DataSeg
, DataSegLen
);
896 if (EFI_ERROR (Status
)) {
900 // Session will be restarted on this error status because the Target is
901 // redirected by this Login Response.
903 return EFI_MEDIA_CHANGED
;
907 // Initiator Error, Target Error, or any other undefined error code.
909 return EFI_PROTOCOL_ERROR
;
912 // The status is success; extract the wanted fields from the header segment.
914 Transit
= ISCSI_FLAG_ON (LoginRsp
, ISCSI_LOGIN_RSP_PDU_FLAG_TRANSIT
);
915 Continue
= ISCSI_FLAG_ON (LoginRsp
, ISCSI_LOGIN_RSP_PDU_FLAG_CONTINUE
);
917 CurrentStage
= ISCSI_GET_CURRENT_STAGE (LoginRsp
);
918 NextStage
= ISCSI_GET_NEXT_STAGE (LoginRsp
);
920 LoginRsp
->InitiatorTaskTag
= NTOHL (LoginRsp
->InitiatorTaskTag
);
922 if ((Transit
&& Continue
) ||
923 (CurrentStage
!= Conn
->CurrentStage
) ||
924 (!Conn
->TransitInitiated
&& Transit
) ||
925 (Transit
&& (NextStage
!= Conn
->NextStage
)) ||
926 (CompareMem (Session
->Isid
, LoginRsp
->Isid
, sizeof (LoginRsp
->Isid
)) != 0) ||
927 (LoginRsp
->InitiatorTaskTag
!= Session
->InitiatorTaskTag
)
930 // A Login Response with the C bit set to 1 MUST have the T bit set to 0.
931 // The CSG in the Login Response MUST be the same with the I-end of this connection.
932 // The T bit can't be 1 if the last Login Response sent by the initiator doesn't
933 // initiate the transistion.
934 // The NSG MUST be the same with the I-end of this connection if Transit is required.
935 // The ISID in the Login Response MUST be the same with this session.
937 return EFI_PROTOCOL_ERROR
;
940 LoginRsp
->StatSN
= NTOHL (LoginRsp
->StatSN
);
941 LoginRsp
->ExpCmdSN
= NTOHL (LoginRsp
->ExpCmdSN
);
942 LoginRsp
->MaxCmdSN
= NTOHL (LoginRsp
->MaxCmdSN
);
944 if ((Conn
->CurrentStage
== ISCSI_SECURITY_NEGOTIATION
) && (Conn
->AuthStep
== ISCSI_AUTH_INITIAL
)) {
946 // If the Login Request is a leading Login Request, the target MUST use
947 // the value presented in CmdSN as the target value for ExpCmdSN.
949 if ((Session
->State
== SESSION_STATE_FREE
) && (Session
->CmdSN
!= LoginRsp
->ExpCmdSN
)) {
950 return EFI_PROTOCOL_ERROR
;
954 // It's the initial Login Response, initialize the local ExpStatSN, MaxCmdSN
957 Conn
->ExpStatSN
= LoginRsp
->StatSN
+ 1;
958 Session
->MaxCmdSN
= LoginRsp
->MaxCmdSN
;
959 Session
->ExpCmdSN
= LoginRsp
->ExpCmdSN
;
962 // Check the StatSN of this PDU.
964 Status
= IScsiCheckSN (&Conn
->ExpStatSN
, LoginRsp
->StatSN
);
965 if (!EFI_ERROR (Status
)) {
967 // Update the MaxCmdSN and ExpCmdSN.
969 IScsiUpdateCmdSN (Session
, LoginRsp
->MaxCmdSN
, LoginRsp
->ExpCmdSN
);
975 // Trim off the header segment.
977 NetbufTrim (Pdu
, sizeof (ISCSI_LOGIN_RESPONSE
), NET_BUF_HEAD
);
980 // Queue this login response first in case it's a partial response so that
981 // later when the full response list is received we can combine these scattered
982 // responses' data segment and then process it.
985 NetbufQueAppend (&Conn
->RspQue
, Pdu
);
987 Conn
->PartialRspRcvd
= Continue
;
990 // It is a partial response; must wait for another or more Request/Response
991 // conversations to get the full response.
996 switch (CurrentStage
) {
997 case ISCSI_SECURITY_NEGOTIATION
:
999 // In security negotiation stage, let CHAP module handle it.
1001 if (Session
->AuthType
!= ISCSI_AUTH_TYPE_KRB
) {
1002 Status
= IScsiCHAPOnRspReceived (Conn
);
1006 case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
:
1008 // Response received with negotiation response on iSCSI parameters: check them.
1010 Status
= IScsiCheckOpParams (Conn
);
1011 if (!EFI_ERROR (Status
)) {
1012 Conn
->ParamNegotiated
= TRUE
;
1019 // Should never get here.
1021 Status
= EFI_PROTOCOL_ERROR
;
1025 if (Transit
&& (Status
== EFI_SUCCESS
)) {
1027 // Do the state transition.
1029 Conn
->CurrentStage
= Conn
->NextStage
;
1031 if (Conn
->CurrentStage
== ISCSI_LOGIN_OPERATIONAL_NEGOTIATION
) {
1032 Conn
->NextStage
= ISCSI_FULL_FEATURE_PHASE
;
1035 // CurrentStage is iSCSI Full Feature. It is the Login-Final Response;
1036 // get the TSIH from the Login Response.
1038 Session
->Tsih
= NTOHS (LoginRsp
->Tsih
);
1042 // Flush the response(s) received.
1044 NetbufQueFlush (&Conn
->RspQue
);
1051 Updated the target information according the data received in the iSCSI
1052 login response with an target redirection status.
1054 @param[in, out] Session The iSCSI session.
1055 @param[in] Data The data segment that should contain the
1056 TargetAddress key-value list.
1057 @param[in] Len Length of the data.
1059 @retval EFI_SUCCESS The target address is updated.
1060 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1061 @retval EFI_NOT_FOUND The TargetAddress key is not found.
1062 @retval Others Other errors as indicated.
1066 IScsiUpdateTargetAddress (
1067 IN OUT ISCSI_SESSION
*Session
,
1072 LIST_ENTRY
*KeyValueList
;
1073 CHAR8
*TargetAddress
;
1078 ISCSI_SESSION_CONFIG_NVDATA
*NvData
;
1080 KeyValueList
= IScsiBuildKeyValueList (Data
, Len
);
1081 if (KeyValueList
== NULL
) {
1082 return EFI_OUT_OF_RESOURCES
;
1085 Status
= EFI_NOT_FOUND
;
1086 NvData
= &Session
->ConfigData
->SessionConfigData
;
1089 TargetAddress
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_ADDRESS
);
1090 if (TargetAddress
== NULL
) {
1094 if (!NET_IS_DIGIT (TargetAddress
[0])) {
1096 // The domainname of the target may be presented in three formats: a DNS host name,
1097 // a dotted-decimal IPv4 address, or a bracketed IPv6 address. Only accept dotted
1104 // Save the origial user setting which specifies the proxy/virtual iSCSI target.
1106 NvData
->OriginalTargetPort
= NvData
->TargetPort
;
1108 IpStr
= TargetAddress
;
1110 while ((*TargetAddress
!= 0) && (*TargetAddress
!= ':') && (*TargetAddress
!= ',')) {
1112 // NULL, ':', or ',' ends the IPv4 string.
1117 if (*TargetAddress
== ',') {
1119 // Comma and the portal group tag MUST be ommitted if the TargetAddress is sent
1120 // as the result of a redirection.
1123 } else if (*TargetAddress
== ':') {
1124 *TargetAddress
= '\0';
1128 Number
= AsciiStrDecimalToUintn (TargetAddress
);
1129 if (Number
> 0xFFFF) {
1132 NvData
->TargetPort
= (UINT16
) Number
;
1136 // The string only contains the IPv4 address. Use the well-known port.
1138 NvData
->TargetPort
= ISCSI_WELL_KNOWN_PORT
;
1142 // Save the origial user setting which specifies the proxy/virtual iSCSI target.
1144 CopyMem (&NvData
->OriginalTargetIp
, &NvData
->TargetIp
, sizeof (EFI_IP_ADDRESS
));
1147 // Update the target IP address.
1149 if (NvData
->IpMode
< IP_MODE_AUTOCONFIG
) {
1150 IpMode
= NvData
->IpMode
;
1152 IpMode
= Session
->ConfigData
->AutoConfigureMode
;
1155 Status
= IScsiAsciiStrToIp (
1158 &Session
->ConfigData
->SessionConfigData
.TargetIp
1161 if (EFI_ERROR (Status
)) {
1164 NvData
->RedirectFlag
= TRUE
;
1169 IScsiFreeKeyValueList (KeyValueList
);
1176 The callback function to free the net buffer list.
1178 @param[in] Arg The opaque parameter.
1187 ASSERT (Arg
!= NULL
);
1189 NetbufFreeList ((LIST_ENTRY
*) Arg
);
1195 The callback function called in NetBufFree; it does nothing.
1197 @param[in] Arg The opaque parameter.
1210 Receive an iSCSI response PDU. An iSCSI response PDU contains an iSCSI PDU header and
1211 an optional data segment. The two parts will be put into two blocks of buffers in the
1212 net buffer. The digest check will be conducted in this function if needed and the digests
1213 will be trimmed from the PDU buffer.
1215 @param[in] Conn The iSCSI connection to receive data from.
1216 @param[out] Pdu The received iSCSI pdu.
1217 @param[in] Context The context used to describe information on the caller provided
1218 buffer to receive data segment of the iSCSI pdu. It is optional.
1219 @param[in] HeaderDigest Whether there will be header digest received.
1220 @param[in] DataDigest Whether there will be data digest.
1221 @param[in] TimeoutEvent The timeout event. It is optional.
1223 @retval EFI_SUCCESS An iSCSI pdu is received.
1224 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1225 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred.
1226 @retval Others Other errors as indicated.
1231 IN ISCSI_CONNECTION
*Conn
,
1233 IN ISCSI_IN_BUFFER_CONTEXT
*Context
, OPTIONAL
1234 IN BOOLEAN HeaderDigest
,
1235 IN BOOLEAN DataDigest
,
1236 IN EFI_EVENT TimeoutEvent OPTIONAL
1239 LIST_ENTRY
*NbufList
;
1245 UINT32 InDataOffset
;
1246 NET_FRAGMENT Fragment
[2];
1247 UINT32 FragmentCount
;
1249 UINT32 PadAndCRC32
[2];
1251 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
1252 if (NbufList
== NULL
) {
1253 return EFI_OUT_OF_RESOURCES
;
1256 InitializeListHead (NbufList
);
1259 // The header digest will be received together with the PDU header, if exists.
1261 Len
= sizeof (ISCSI_BASIC_HEADER
) + (HeaderDigest
? sizeof (UINT32
) : 0);
1262 PduHdr
= NetbufAlloc (Len
);
1263 if (PduHdr
== NULL
) {
1264 Status
= EFI_OUT_OF_RESOURCES
;
1268 Header
= NetbufAllocSpace (PduHdr
, Len
, NET_BUF_TAIL
);
1269 if (Header
== NULL
) {
1270 Status
= EFI_OUT_OF_RESOURCES
;
1273 InsertTailList (NbufList
, &PduHdr
->List
);
1276 // First step, receive the BHS of the PDU.
1278 Status
= TcpIoReceive (&Conn
->TcpIo
, PduHdr
, FALSE
, TimeoutEvent
);
1280 if (EFI_ERROR (Status
)) {
1286 // TODO: check the header-digest.
1289 // Trim off the digest.
1291 NetbufTrim (PduHdr
, sizeof (UINT32
), NET_BUF_TAIL
);
1294 Len
= ISCSI_GET_DATASEG_LEN (Header
);
1302 // Get the length of the padding bytes of the data segment.
1304 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1306 switch (ISCSI_GET_OPCODE (Header
)) {
1307 case ISCSI_OPCODE_SCSI_DATA_IN
:
1309 // To reduce memory copy overhead, try to use the buffer described by Context
1310 // if the PDU is an iSCSI SCSI data.
1312 InDataOffset
= ISCSI_GET_BUFFER_OFFSET (Header
);
1313 if ((Context
== NULL
) || ((InDataOffset
+ Len
) > Context
->InDataLen
)) {
1314 Status
= EFI_PROTOCOL_ERROR
;
1318 Fragment
[0].Len
= Len
;
1319 Fragment
[0].Bulk
= Context
->InData
+ InDataOffset
;
1321 if (DataDigest
|| (PadLen
!= 0)) {
1323 // The data segment is padded. Use two fragments to receive it:
1324 // the first to receive the useful data; the second to receive the padding.
1326 Fragment
[1].Len
= PadLen
+ (DataDigest
? sizeof (UINT32
) : 0);
1327 Fragment
[1].Bulk
= (UINT8
*)PadAndCRC32
+ (4 - PadLen
);
1334 DataSeg
= NetbufFromExt (&Fragment
[0], FragmentCount
, 0, 0, IScsiNbufExtFree
, NULL
);
1335 if (DataSeg
== NULL
) {
1336 Status
= EFI_OUT_OF_RESOURCES
;
1342 case ISCSI_OPCODE_SCSI_RSP
:
1343 case ISCSI_OPCODE_NOP_IN
:
1344 case ISCSI_OPCODE_LOGIN_RSP
:
1345 case ISCSI_OPCODE_TEXT_RSP
:
1346 case ISCSI_OPCODE_ASYNC_MSG
:
1347 case ISCSI_OPCODE_REJECT
:
1348 case ISCSI_OPCODE_VENDOR_T0
:
1349 case ISCSI_OPCODE_VENDOR_T1
:
1350 case ISCSI_OPCODE_VENDOR_T2
:
1352 // Allocate buffer to receive the data segment.
1354 Len
+= PadLen
+ (DataDigest
? sizeof (UINT32
) : 0);
1355 DataSeg
= NetbufAlloc (Len
);
1356 if (DataSeg
== NULL
) {
1357 Status
= EFI_OUT_OF_RESOURCES
;
1361 NetbufAllocSpace (DataSeg
, Len
, NET_BUF_TAIL
);
1365 Status
= EFI_PROTOCOL_ERROR
;
1369 InsertTailList (NbufList
, &DataSeg
->List
);
1372 // Receive the data segment with the data digest, if any.
1374 Status
= TcpIoReceive (&Conn
->TcpIo
, DataSeg
, FALSE
, TimeoutEvent
);
1376 if (EFI_ERROR (Status
)) {
1382 // TODO: Check the data digest.
1384 NetbufTrim (DataSeg
, sizeof (UINT32
), NET_BUF_TAIL
);
1389 // Trim off the padding bytes in the data segment.
1391 NetbufTrim (DataSeg
, PadLen
, NET_BUF_TAIL
);
1396 // Form the pdu from a list of pdu segments.
1398 *Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
1400 Status
= EFI_OUT_OF_RESOURCES
;
1405 if (EFI_ERROR (Status
)) {
1407 // Free the Nbufs in this NbufList and the NbufList itself.
1409 IScsiFreeNbufList (NbufList
);
1417 Check and get the result of the parameter negotiation.
1419 @param[in, out] Conn The connection in iSCSI login.
1421 @retval EFI_SUCCESS The parmeter check is passed and negotiation is finished.
1422 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred.
1423 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1427 IScsiCheckOpParams (
1428 IN OUT ISCSI_CONNECTION
*Conn
1432 LIST_ENTRY
*KeyValueList
;
1435 ISCSI_SESSION
*Session
;
1439 ASSERT (Conn
->RspQue
.BufNum
!= 0);
1441 Session
= Conn
->Session
;
1443 Len
= Conn
->RspQue
.BufSize
;
1444 Data
= AllocatePool (Len
);
1446 return EFI_OUT_OF_RESOURCES
;
1449 NetbufQueCopy (&Conn
->RspQue
, 0, Len
, (UINT8
*) Data
);
1451 Status
= EFI_PROTOCOL_ERROR
;
1454 // Extract the Key-Value pairs into a list.
1456 KeyValueList
= IScsiBuildKeyValueList (Data
, Len
);
1457 if (KeyValueList
== NULL
) {
1464 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_HEADER_DIGEST
);
1465 if (Value
== NULL
) {
1469 if (AsciiStrCmp (Value
, "CRC32") == 0) {
1470 if (Conn
->HeaderDigest
!= IScsiDigestCRC32
) {
1473 } else if (AsciiStrCmp (Value
, ISCSI_KEY_VALUE_NONE
) == 0) {
1474 Conn
->HeaderDigest
= IScsiDigestNone
;
1481 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_DIGEST
);
1482 if (Value
== NULL
) {
1486 if (AsciiStrCmp (Value
, "CRC32") == 0) {
1487 if (Conn
->DataDigest
!= IScsiDigestCRC32
) {
1490 } else if (AsciiStrCmp (Value
, ISCSI_KEY_VALUE_NONE
) == 0) {
1491 Conn
->DataDigest
= IScsiDigestNone
;
1496 // ErrorRecoveryLevel: result fuction is Minimum.
1498 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_ERROR_RECOVERY_LEVEL
);
1499 if (Value
== NULL
) {
1503 NumericValue
= IScsiNetNtoi (Value
);
1504 if (NumericValue
> 2) {
1508 Session
->ErrorRecoveryLevel
= (UINT8
) MIN (Session
->ErrorRecoveryLevel
, NumericValue
);
1511 // InitialR2T: result function is OR.
1513 if (!Session
->InitialR2T
) {
1514 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_INITIAL_R2T
);
1515 if (Value
== NULL
) {
1519 Session
->InitialR2T
= (BOOLEAN
) (AsciiStrCmp (Value
, "Yes") == 0);
1523 // ImmediateData: result function is AND.
1525 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_IMMEDIATE_DATA
);
1526 if (Value
== NULL
) {
1530 Session
->ImmediateData
= (BOOLEAN
) (Session
->ImmediateData
&& (BOOLEAN
) (AsciiStrCmp (Value
, "Yes") == 0));
1533 // MaxRecvDataSegmentLength is declarative.
1535 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
);
1536 if (Value
!= NULL
) {
1537 Conn
->MaxRecvDataSegmentLength
= (UINT32
) IScsiNetNtoi (Value
);
1540 // MaxBurstLength: result funtion is Mininum.
1542 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_BURST_LENGTH
);
1543 if (Value
== NULL
) {
1547 NumericValue
= IScsiNetNtoi (Value
);
1548 Session
->MaxBurstLength
= (UINT32
) MIN (Session
->MaxBurstLength
, NumericValue
);
1551 // FirstBurstLength: result function is Minimum. Irrelevant when InitialR2T=Yes and
1552 // ImmediateData=No.
1554 if (!(Session
->InitialR2T
&& !Session
->ImmediateData
)) {
1555 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_FIRST_BURST_LENGTH
);
1556 if (Value
== NULL
) {
1560 NumericValue
= IScsiNetNtoi (Value
);
1561 Session
->FirstBurstLength
= (UINT32
) MIN (Session
->FirstBurstLength
, NumericValue
);
1565 // MaxConnections: result function is Minimum.
1567 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_CONNECTIONS
);
1568 if (Value
== NULL
) {
1572 NumericValue
= IScsiNetNtoi (Value
);
1573 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1577 Session
->MaxConnections
= (UINT32
) MIN (Session
->MaxConnections
, NumericValue
);
1580 // DataPDUInOrder: result function is OR.
1582 if (!Session
->DataPDUInOrder
) {
1583 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_PDU_IN_ORDER
);
1584 if (Value
== NULL
) {
1588 Session
->DataPDUInOrder
= (BOOLEAN
) (AsciiStrCmp (Value
, "Yes") == 0);
1592 // DataSequenceInorder: result function is OR.
1594 if (!Session
->DataSequenceInOrder
) {
1595 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
);
1596 if (Value
== NULL
) {
1600 Session
->DataSequenceInOrder
= (BOOLEAN
) (AsciiStrCmp (Value
, "Yes") == 0);
1604 // DefaultTime2Wait: result function is Maximum.
1606 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2WAIT
);
1607 if (Value
== NULL
) {
1611 NumericValue
= IScsiNetNtoi (Value
);
1612 if (NumericValue
== 0) {
1613 Session
->DefaultTime2Wait
= 0;
1614 } else if (NumericValue
> 3600) {
1617 Session
->DefaultTime2Wait
= (UINT32
) MAX (Session
->DefaultTime2Wait
, NumericValue
);
1620 // DefaultTime2Retain: result function is Minimum.
1622 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DEFAULT_TIME2RETAIN
);
1623 if (Value
== NULL
) {
1627 NumericValue
= IScsiNetNtoi (Value
);
1628 if (NumericValue
== 0) {
1629 Session
->DefaultTime2Retain
= 0;
1630 } else if (NumericValue
> 3600) {
1633 Session
->DefaultTime2Retain
= (UINT32
) MIN (Session
->DefaultTime2Retain
, NumericValue
);
1636 // MaxOutstandingR2T: result function is Minimum.
1638 Value
= IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_MAX_OUTSTANDING_R2T
);
1639 if (Value
== NULL
) {
1643 NumericValue
= IScsiNetNtoi (Value
);
1644 if ((NumericValue
== 0) || (NumericValue
> 65535)) {
1648 Session
->MaxOutstandingR2T
= (UINT16
) MIN (Session
->MaxOutstandingR2T
, NumericValue
);
1651 // Remove declarative key-value pairs, if any.
1653 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_SESSION_TYPE
);
1654 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_ALIAS
);
1655 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_TARGET_PORTAL_GROUP_TAG
);
1659 // Remove the key-value that may not needed for result function is OR.
1661 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_INITIAL_R2T
);
1662 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_PDU_IN_ORDER
);
1663 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
);
1666 // Remove irrelevant parameter, if any.
1668 if (Session
->InitialR2T
&& !Session
->ImmediateData
) {
1669 IScsiGetValueByKeyFromList (KeyValueList
, ISCSI_KEY_FIRST_BURST_LENGTH
);
1672 if (IsListEmpty (KeyValueList
)) {
1674 // Succeed if no more keys in the list.
1676 Status
= EFI_SUCCESS
;
1681 IScsiFreeKeyValueList (KeyValueList
);
1690 Fill the operational parameters.
1692 @param[in] Conn The connection in iSCSI login.
1693 @param[in, out] Pdu The iSCSI login request PDU to fill the parameters.
1698 IN ISCSI_CONNECTION
*Conn
,
1702 ISCSI_SESSION
*Session
;
1705 Session
= Conn
->Session
;
1707 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->HeaderDigest
== IScsiDigestCRC32
) ? "None,CRC32" : "None");
1708 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_HEADER_DIGEST
, Value
);
1710 AsciiSPrint (Value
, sizeof (Value
), "%a", (Conn
->DataDigest
== IScsiDigestCRC32
) ? "None,CRC32" : "None");
1711 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_DIGEST
, Value
);
1713 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->ErrorRecoveryLevel
);
1714 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_ERROR_RECOVERY_LEVEL
, Value
);
1716 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->InitialR2T
? "Yes" : "No");
1717 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_INITIAL_R2T
, Value
);
1719 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->ImmediateData
? "Yes" : "No");
1720 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_IMMEDIATE_DATA
, Value
);
1722 AsciiSPrint (Value
, sizeof (Value
), "%d", MAX_RECV_DATA_SEG_LEN_IN_FFP
);
1723 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH
, Value
);
1725 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxBurstLength
);
1726 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_BURST_LENGTH
, Value
);
1728 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->FirstBurstLength
);
1729 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_FIRST_BURST_LENGTH
, Value
);
1731 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxConnections
);
1732 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_CONNECTIONS
, Value
);
1734 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataPDUInOrder
? "Yes" : "No");
1735 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_PDU_IN_ORDER
, Value
);
1737 AsciiSPrint (Value
, sizeof (Value
), "%a", Session
->DataSequenceInOrder
? "Yes" : "No");
1738 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER
, Value
);
1740 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Wait
);
1741 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2WAIT
, Value
);
1743 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->DefaultTime2Retain
);
1744 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_DEFAULT_TIME2RETAIN
, Value
);
1746 AsciiSPrint (Value
, sizeof (Value
), "%d", Session
->MaxOutstandingR2T
);
1747 IScsiAddKeyValuePair (Pdu
, ISCSI_KEY_MAX_OUTSTANDING_R2T
, Value
);
1752 Pad the iSCSI AHS or data segment to an integer number of 4 byte words.
1754 @param[in, out] Pdu The iSCSI pdu which contains segments to pad.
1755 @param[in] Len The length of the last segment in the PDU.
1757 @retval EFI_SUCCESS The segment is padded or there is no need to pad it.
1758 @retval EFI_OUT_OF_RESOURCES There is not enough remaining free space to add the
1763 IN OUT NET_BUF
*Pdu
,
1770 PadLen
= ISCSI_GET_PAD_LEN (Len
);
1773 Data
= NetbufAllocSpace (Pdu
, PadLen
, NET_BUF_TAIL
);
1775 return EFI_OUT_OF_RESOURCES
;
1778 ZeroMem (Data
, PadLen
);
1786 Build a key-value list from the data segment.
1788 @param[in] Data The data segment containing the key-value pairs.
1789 @param[in] Len Length of the data segment.
1791 @return The key-value list.
1792 @retval NULL Other errors as indicated.
1796 IScsiBuildKeyValueList (
1801 LIST_ENTRY
*ListHead
;
1802 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1804 ListHead
= AllocatePool (sizeof (LIST_ENTRY
));
1805 if (ListHead
== NULL
) {
1809 InitializeListHead (ListHead
);
1812 KeyValuePair
= AllocatePool (sizeof (ISCSI_KEY_VALUE_PAIR
));
1813 if (KeyValuePair
== NULL
) {
1817 InitializeListHead (&KeyValuePair
->List
);
1819 KeyValuePair
->Key
= Data
;
1821 while ((Len
> 0) && (*Data
!= '=')) {
1832 FreePool (KeyValuePair
);
1836 KeyValuePair
->Value
= Data
;
1838 InsertTailList (ListHead
, &KeyValuePair
->List
);;
1840 Data
+= AsciiStrLen (KeyValuePair
->Value
) + 1;
1841 Len
-= (UINT32
) AsciiStrLen (KeyValuePair
->Value
) + 1;
1848 IScsiFreeKeyValueList (ListHead
);
1855 Get the value string by the key name from the key-value list. If found,
1856 the key-value entry will be removed from the list.
1858 @param[in, out] KeyValueList The key-value list.
1859 @param[in] Key The key name to find.
1861 @return The value string.
1862 @retval NULL The key value pair cannot be found.
1866 IScsiGetValueByKeyFromList (
1867 IN OUT LIST_ENTRY
*KeyValueList
,
1872 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1877 NET_LIST_FOR_EACH (Entry
, KeyValueList
) {
1878 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1880 if (AsciiStrCmp (KeyValuePair
->Key
, Key
) == 0) {
1881 Value
= KeyValuePair
->Value
;
1883 RemoveEntryList (&KeyValuePair
->List
);
1884 FreePool (KeyValuePair
);
1894 Free the key-value list.
1896 @param[in] KeyValueList The key-value list.
1900 IScsiFreeKeyValueList (
1901 IN LIST_ENTRY
*KeyValueList
1905 ISCSI_KEY_VALUE_PAIR
*KeyValuePair
;
1907 while (!IsListEmpty (KeyValueList
)) {
1908 Entry
= NetListRemoveHead (KeyValueList
);
1909 KeyValuePair
= NET_LIST_USER_STRUCT (Entry
, ISCSI_KEY_VALUE_PAIR
, List
);
1911 FreePool (KeyValuePair
);
1914 FreePool (KeyValueList
);
1919 Normalize the iSCSI name according to RFC.
1921 @param[in, out] Name The iSCSI name.
1922 @param[in] Len Length of the iSCSI name.
1924 @retval EFI_SUCCESS The iSCSI name is valid and normalized.
1925 @retval EFI_PROTOCOL_ERROR The iSCSI name is malformatted or not in the IQN format.
1929 IScsiNormalizeName (
1936 for (Index
= 0; Index
< Len
; Index
++) {
1937 if (NET_IS_UPPER_CASE_CHAR (Name
[Index
])) {
1939 // Convert the upper-case characters to lower-case ones.
1941 Name
[Index
] = (CHAR8
) (Name
[Index
] - 'A' + 'a');
1944 if (!NET_IS_LOWER_CASE_CHAR (Name
[Index
]) &&
1945 !NET_IS_DIGIT (Name
[Index
]) &&
1946 (Name
[Index
] != '-') &&
1947 (Name
[Index
] != '.') &&
1948 (Name
[Index
] != ':')
1951 // ASCII dash, dot, colon lower-case characters and digit characters
1954 return EFI_PROTOCOL_ERROR
;
1958 if ((Len
< 4) || (CompareMem (Name
, "iqn.", 4) != 0)) {
1960 // Only IQN format is accepted now.
1962 return EFI_PROTOCOL_ERROR
;
1970 Create an iSCSI task control block.
1972 @param[in] Conn The connection on which the task control block will be created.
1973 @param[out] Tcb The newly created task control block.
1975 @retval EFI_SUCCESS The task control block is created.
1976 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1977 @retval EFI_NOT_READY The target cannot accept new commands.
1982 IN ISCSI_CONNECTION
*Conn
,
1986 ISCSI_SESSION
*Session
;
1989 ASSERT (Tcb
!= NULL
);
1991 Session
= Conn
->Session
;
1993 if (ISCSI_SEQ_GT (Session
->CmdSN
, Session
->MaxCmdSN
)) {
1994 return EFI_NOT_READY
;
1997 NewTcb
= AllocateZeroPool (sizeof (ISCSI_TCB
));
1998 if (NewTcb
== NULL
) {
1999 return EFI_OUT_OF_RESOURCES
;
2002 InitializeListHead (&NewTcb
->Link
);
2004 NewTcb
->SoFarInOrder
= TRUE
;
2005 NewTcb
->InitiatorTaskTag
= Session
->InitiatorTaskTag
;
2006 NewTcb
->CmdSN
= Session
->CmdSN
;
2007 NewTcb
->Conn
= Conn
;
2009 InsertTailList (&Session
->TcbList
, &NewTcb
->Link
);
2012 // Advance the initiator task tag.
2014 Session
->InitiatorTaskTag
++;
2024 Delete the tcb from the connection and destroy it.
2026 @param[in] Tcb The tcb to delete.
2034 RemoveEntryList (&Tcb
->Link
);
2041 Find the task control block by the initator task tag.
2043 @param[in] TcbList The tcb list.
2044 @param[in] InitiatorTaskTag The initiator task tag.
2046 @return The task control block found.
2047 @retval NULL The task control block cannot be found.
2052 IN LIST_ENTRY
*TcbList
,
2053 IN UINT32 InitiatorTaskTag
2061 NET_LIST_FOR_EACH (Entry
, TcbList
) {
2062 Tcb
= NET_LIST_USER_STRUCT (Entry
, ISCSI_TCB
, Link
);
2064 if (Tcb
->InitiatorTaskTag
== InitiatorTaskTag
) {
2074 Create a data segment, pad it, and calculate the CRC if needed.
2076 @param[in] Data The data to fill into the data segment.
2077 @param[in] Len Length of the data.
2078 @param[in] DataDigest Whether to calculate CRC for this data segment.
2080 @return The net buffer wrapping the data segment.
2084 IScsiNewDataSegment (
2087 IN BOOLEAN DataDigest
2090 NET_FRAGMENT Fragment
[2];
2091 UINT32 FragmentCount
;
2095 Fragment
[0].Len
= Len
;
2096 Fragment
[0].Bulk
= Data
;
2098 PadLen
= ISCSI_GET_PAD_LEN (Len
);
2100 Fragment
[1].Len
= PadLen
;
2101 Fragment
[1].Bulk
= (UINT8
*) &mDataSegPad
;
2108 DataSeg
= NetbufFromExt (&Fragment
[0], FragmentCount
, 0, 0, IScsiNbufExtFree
, NULL
);
2115 Create a iSCSI SCSI command PDU to encapsulate the command issued
2116 by SCSI through the EXT SCSI PASS THRU Protocol.
2118 @param[in] Packet The EXT SCSI PASS THRU request packet containing the SCSI command.
2119 @param[in] Lun The LUN.
2120 @param[in] Tcb The tcb assocated with this SCSI command.
2122 @return The created iSCSI SCSI command PDU.
2123 @retval NULL Other errors as indicated.
2127 IScsiNewScsiCmdPdu (
2128 IN EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
2133 LIST_ENTRY
*NbufList
;
2137 SCSI_COMMAND
*ScsiCmd
;
2140 ISCSI_ADDITIONAL_HEADER
*Header
;
2141 ISCSI_BI_EXP_READ_DATA_LEN_AHS
*BiExpReadDataLenAHS
;
2142 ISCSI_SESSION
*Session
;
2143 UINT32 ImmediateDataLen
;
2147 if (Packet
->DataDirection
== DataBi
) {
2149 // Bidirectional Read/Write command, the bidirectional expected
2150 // read data length AHS is required.
2152 AHSLength
+= sizeof (ISCSI_BI_EXP_READ_DATA_LEN_AHS
);
2155 if (Packet
->CdbLength
> 16) {
2157 // The CDB exceeds 16 bytes. An extended CDB AHS is required.
2159 AHSLength
= (UINT8
) (AHSLength
+ ISCSI_ROUNDUP (Packet
->CdbLength
- 16) + sizeof (ISCSI_ADDITIONAL_HEADER
));
2162 Length
= sizeof (SCSI_COMMAND
) + AHSLength
;
2163 PduHeader
= NetbufAlloc (Length
);
2164 if (PduHeader
== NULL
) {
2168 ScsiCmd
= (SCSI_COMMAND
*) NetbufAllocSpace (PduHeader
, Length
, NET_BUF_TAIL
);
2169 if (ScsiCmd
== NULL
) {
2170 NetbufFree (PduHeader
);
2173 Header
= (ISCSI_ADDITIONAL_HEADER
*) (ScsiCmd
+ 1);
2175 ZeroMem (ScsiCmd
, Length
);
2177 ISCSI_SET_OPCODE (ScsiCmd
, ISCSI_OPCODE_SCSI_CMD
, 0);
2178 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_TASK_ATTR_SIMPLE
);
2181 // Set the READ/WRITE flags according to the IO type of this request.
2183 switch (Packet
->DataDirection
) {
2185 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
);
2186 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->InTransferLength
);
2190 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_WRITE
);
2191 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
2195 ISCSI_SET_FLAG (ScsiCmd
, SCSI_CMD_PDU_FLAG_READ
| SCSI_CMD_PDU_FLAG_WRITE
);
2196 ScsiCmd
->ExpDataXferLength
= NTOHL (Packet
->OutTransferLength
);
2199 // Fill the bidirectional expected read data length AHS.
2201 BiExpReadDataLenAHS
= (ISCSI_BI_EXP_READ_DATA_LEN_AHS
*) Header
;
2202 Header
= (ISCSI_ADDITIONAL_HEADER
*) (BiExpReadDataLenAHS
+ 1);
2204 BiExpReadDataLenAHS
->Length
= NTOHS (5);
2205 BiExpReadDataLenAHS
->Type
= ISCSI_AHS_TYPE_BI_EXP_READ_DATA_LEN
;
2206 BiExpReadDataLenAHS
->ExpReadDataLength
= NTOHL (Packet
->InTransferLength
);
2211 ScsiCmd
->TotalAHSLength
= AHSLength
;
2212 CopyMem (ScsiCmd
->Lun
, &Lun
, sizeof (ScsiCmd
->Lun
));
2213 ScsiCmd
->InitiatorTaskTag
= NTOHL (Tcb
->InitiatorTaskTag
);
2214 ScsiCmd
->CmdSN
= NTOHL (Tcb
->CmdSN
);
2215 ScsiCmd
->ExpStatSN
= NTOHL (Tcb
->Conn
->ExpStatSN
);
2217 CopyMem (ScsiCmd
->Cdb
, Packet
->Cdb
, sizeof (ScsiCmd
->Cdb
));
2219 if (Packet
->CdbLength
> 16) {
2220 Header
->Length
= NTOHS ((UINT16
) (Packet
->CdbLength
- 15));
2221 Header
->Type
= ISCSI_AHS_TYPE_EXT_CDB
;
2223 CopyMem (Header
+ 1, (UINT8
*) Packet
->Cdb
+ 16, Packet
->CdbLength
- 16);
2227 Session
= Tcb
->Conn
->Session
;
2228 ImmediateDataLen
= 0;
2230 if (Session
->ImmediateData
&& (Packet
->OutTransferLength
!= 0)) {
2232 // Send immediate data in this SCSI Command PDU. The length of the immeidate
2233 // data is the minimum of FirstBurstLength, the data length to be xfered, and
2234 // the MaxRecvdataSegmentLength on this connection.
2236 ImmediateDataLen
= MIN (Session
->FirstBurstLength
, Packet
->OutTransferLength
);
2237 ImmediateDataLen
= MIN (ImmediateDataLen
, Tcb
->Conn
->MaxRecvDataSegmentLength
);
2240 // Update the data segment length in the PDU header.
2242 ISCSI_SET_DATASEG_LEN (ScsiCmd
, ImmediateDataLen
);
2245 // Create the data segment.
2247 DataSeg
= IScsiNewDataSegment ((UINT8
*) Packet
->OutDataBuffer
, ImmediateDataLen
, FALSE
);
2248 if (DataSeg
== NULL
) {
2249 NetbufFree (PduHeader
);
2254 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
2255 if (NbufList
== NULL
) {
2256 NetbufFree (PduHeader
);
2257 NetbufFree (DataSeg
);
2263 InitializeListHead (NbufList
);
2264 InsertTailList (NbufList
, &PduHeader
->List
);
2265 InsertTailList (NbufList
, &DataSeg
->List
);
2267 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
2269 IScsiFreeNbufList (NbufList
);
2273 if (Session
->InitialR2T
||
2274 (ImmediateDataLen
== Session
->FirstBurstLength
) ||
2275 (ImmediateDataLen
== Packet
->OutTransferLength
)
2278 // Unsolicited data out sequence is not allowed,
2279 // or FirstBustLength data is already sent out by immediate data,
2280 // or all the OUT data accompany this SCSI packet are sent as
2281 // immediate data. The final flag should be set on this SCSI Command
2284 ISCSI_SET_FLAG (ScsiCmd
, ISCSI_BHS_FLAG_FINAL
);
2294 Create a new iSCSI SCSI Data Out PDU.
2296 @param[in] Data The data to put into the Data Out PDU.
2297 @param[in] Len Length of the data.
2298 @param[in] DataSN The DataSN of the Data Out PDU.
2299 @param[in] Tcb The task control block of this Data Out PDU.
2300 @param[in] Lun The LUN.
2302 @return The net buffer wrapping the Data Out PDU.
2303 @retval NULL Other errors as indicated.
2307 IScsiNewDataOutPdu (
2315 LIST_ENTRY
*NbufList
;
2319 ISCSI_SCSI_DATA_OUT
*DataOutHdr
;
2320 ISCSI_XFER_CONTEXT
*XferContext
;
2322 NbufList
= AllocatePool (sizeof (LIST_ENTRY
));
2323 if (NbufList
== NULL
) {
2327 InitializeListHead (NbufList
);
2330 // Allocate memory for the BHS.
2332 PduHdr
= NetbufAlloc (sizeof (ISCSI_SCSI_DATA_OUT
));
2333 if (PduHdr
== NULL
) {
2334 FreePool (NbufList
);
2338 // Insert the BHS into the buffer list.
2340 InsertTailList (NbufList
, &PduHdr
->List
);
2342 DataOutHdr
= (ISCSI_SCSI_DATA_OUT
*) NetbufAllocSpace (PduHdr
, sizeof (ISCSI_SCSI_DATA_OUT
), NET_BUF_TAIL
);
2343 if (DataOutHdr
== NULL
) {
2344 IScsiFreeNbufList (NbufList
);
2347 XferContext
= &Tcb
->XferContext
;
2349 ZeroMem (DataOutHdr
, sizeof (ISCSI_SCSI_DATA_OUT
));
2352 // Set the flags and fields of the Data Out PDU BHS.
2354 ISCSI_SET_OPCODE (DataOutHdr
, ISCSI_OPCODE_SCSI_DATA_OUT
, 0);
2355 ISCSI_SET_DATASEG_LEN (DataOutHdr
, Len
);
2357 DataOutHdr
->InitiatorTaskTag
= HTONL (Tcb
->InitiatorTaskTag
);
2358 DataOutHdr
->TargetTransferTag
= HTONL (XferContext
->TargetTransferTag
);
2359 DataOutHdr
->ExpStatSN
= HTONL (Tcb
->Conn
->ExpStatSN
);
2360 DataOutHdr
->DataSN
= HTONL (DataSN
);
2361 DataOutHdr
->BufferOffset
= HTONL (XferContext
->Offset
);
2363 if (XferContext
->TargetTransferTag
!= ISCSI_RESERVED_TAG
) {
2364 CopyMem (&DataOutHdr
->Lun
, &Lun
, sizeof (DataOutHdr
->Lun
));
2367 // Build the data segment for this Data Out PDU.
2369 DataSeg
= IScsiNewDataSegment (Data
, Len
, FALSE
);
2370 if (DataSeg
== NULL
) {
2371 IScsiFreeNbufList (NbufList
);
2375 // Put the data segment into the buffer list and combine it with the BHS
2376 // into a full Data Out PDU.
2378 InsertTailList (NbufList
, &DataSeg
->List
);
2379 Pdu
= NetbufFromBufList (NbufList
, 0, 0, IScsiFreeNbufList
, NbufList
);
2381 IScsiFreeNbufList (NbufList
);
2389 Generate a consecutive sequence of iSCSI SCSI Data Out PDUs.
2391 @param[in] Data The data which will be carried by the sequence of iSCSI SCSI Data Out PDUs.
2392 @param[in] Tcb The task control block of the data to send out.
2393 @param[in] Lun The LUN the data will be sent to.
2395 @return A list of net buffers with each of them wrapping an iSCSI SCSI Data Out PDU.
2396 @retval NULL Other errors as indicated.
2400 IScsiGenerateDataOutPduSequence (
2406 LIST_ENTRY
*PduList
;
2409 NET_BUF
*DataOutPdu
;
2410 ISCSI_CONNECTION
*Conn
;
2411 ISCSI_XFER_CONTEXT
*XferContext
;
2412 UINT8
*DataOutPacket
;
2414 PduList
= AllocatePool (sizeof (LIST_ENTRY
));
2415 if (PduList
== NULL
) {
2419 InitializeListHead (PduList
);
2424 XferContext
= &Tcb
->XferContext
;
2426 while (XferContext
->DesiredLength
> 0) {
2428 // Determine the length of data this Data Out PDU can carry.
2430 DataLen
= MIN (XferContext
->DesiredLength
, Conn
->MaxRecvDataSegmentLength
);
2433 // Create a Data Out PDU.
2435 DataOutPdu
= IScsiNewDataOutPdu (Data
, DataLen
, DataSN
, Tcb
, Lun
);
2436 if (DataOutPdu
== NULL
) {
2437 IScsiFreeNbufList (PduList
);
2443 InsertTailList (PduList
, &DataOutPdu
->List
);
2446 // Update the context and DataSN.
2449 XferContext
->Offset
+= DataLen
;
2450 XferContext
->DesiredLength
-= DataLen
;
2454 // Set the F bit for the last data out PDU in this sequence.
2456 DataOutPacket
= NetbufGetByte (DataOutPdu
, 0, NULL
);
2457 if (DataOutPacket
== NULL
) {
2458 IScsiFreeNbufList (PduList
);
2463 ISCSI_SET_FLAG (DataOutPacket
, ISCSI_BHS_FLAG_FINAL
);
2471 Send the Data in a sequence of Data Out PDUs one by one.
2473 @param[in] Data The data to carry by Data Out PDUs.
2474 @param[in] Lun The LUN the data will be sent to.
2475 @param[in] Tcb The task control block.
2477 @retval EFI_SUCCES The data is sent out to the LUN.
2478 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2479 @retval Others Other errors as indicated.
2483 IScsiSendDataOutPduSequence (
2489 LIST_ENTRY
*DataOutPduList
;
2495 // Generate the Data Out PDU sequence.
2497 DataOutPduList
= IScsiGenerateDataOutPduSequence (Data
, Tcb
, Lun
);
2498 if (DataOutPduList
== NULL
) {
2499 return EFI_OUT_OF_RESOURCES
;
2502 Status
= EFI_SUCCESS
;
2505 // Send the Data Out PDU's one by one.
2507 NET_LIST_FOR_EACH (Entry
, DataOutPduList
) {
2508 Pdu
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
2510 Status
= TcpIoTransmit (&Tcb
->Conn
->TcpIo
, Pdu
);
2512 if (EFI_ERROR (Status
)) {
2517 IScsiFreeNbufList (DataOutPduList
);
2524 Process the received iSCSI SCSI Data In PDU.
2526 @param[in] Pdu The Data In PDU received.
2527 @param[in] Tcb The task control block.
2528 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2530 @retval EFI_SUCCES The check on the Data IN PDU is passed and some update
2532 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror occurred.
2533 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2534 @retval Others Other errors as indicated.
2541 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2544 ISCSI_SCSI_DATA_IN
*DataInHdr
;
2547 DataInHdr
= (ISCSI_SCSI_DATA_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2548 if (DataInHdr
== NULL
) {
2549 return EFI_PROTOCOL_ERROR
;
2552 DataInHdr
->InitiatorTaskTag
= NTOHL (DataInHdr
->InitiatorTaskTag
);
2553 DataInHdr
->ExpCmdSN
= NTOHL (DataInHdr
->ExpCmdSN
);
2554 DataInHdr
->MaxCmdSN
= NTOHL (DataInHdr
->MaxCmdSN
);
2555 DataInHdr
->DataSN
= NTOHL (DataInHdr
->DataSN
);
2558 // Check the DataSN.
2560 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, DataInHdr
->DataSN
);
2561 if (EFI_ERROR (Status
)) {
2565 if (DataInHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2566 return EFI_PROTOCOL_ERROR
;
2569 // Update the command related sequence numbers.
2571 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, DataInHdr
->MaxCmdSN
, DataInHdr
->ExpCmdSN
);
2573 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_STATUS_VALID
)) {
2574 if (!ISCSI_FLAG_ON (DataInHdr
, ISCSI_BHS_FLAG_FINAL
)) {
2576 // The S bit is on but the F bit is off.
2578 return EFI_PROTOCOL_ERROR
;
2581 Tcb
->StatusXferd
= TRUE
;
2583 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_DATA_IN_PDU_FLAG_OVERFLOW
| SCSI_DATA_IN_PDU_FLAG_UNDERFLOW
)) {
2585 // Underflow and Overflow are mutual flags.
2587 return EFI_PROTOCOL_ERROR
;
2590 // S bit is on, the StatSN is valid.
2592 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NTOHL (DataInHdr
->StatSN
));
2593 if (EFI_ERROR (Status
)) {
2597 Packet
->HostAdapterStatus
= 0;
2598 Packet
->TargetStatus
= DataInHdr
->Status
;
2600 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2601 Packet
->InTransferLength
+= NTOHL (DataInHdr
->ResidualCount
);
2602 Status
= EFI_BAD_BUFFER_SIZE
;
2605 if (ISCSI_FLAG_ON (DataInHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2606 Packet
->InTransferLength
-= NTOHL (DataInHdr
->ResidualCount
);
2615 Process the received iSCSI R2T PDU.
2617 @param[in] Pdu The R2T PDU received.
2618 @param[in] Tcb The task control block.
2619 @param[in] Lun The Lun.
2620 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2622 @retval EFI_SUCCES The R2T PDU is valid and the solicited data is sent out.
2623 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror occurred.
2624 @retval Others Other errors as indicated.
2632 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2635 ISCSI_READY_TO_TRANSFER
*R2THdr
;
2637 ISCSI_XFER_CONTEXT
*XferContext
;
2640 R2THdr
= (ISCSI_READY_TO_TRANSFER
*) NetbufGetByte (Pdu
, 0, NULL
);
2641 if (R2THdr
== NULL
) {
2642 return EFI_PROTOCOL_ERROR
;
2645 R2THdr
->InitiatorTaskTag
= NTOHL (R2THdr
->InitiatorTaskTag
);
2646 R2THdr
->TargetTransferTag
= NTOHL (R2THdr
->TargetTransferTag
);
2647 R2THdr
->StatSN
= NTOHL (R2THdr
->StatSN
);
2648 R2THdr
->R2TSeqNum
= NTOHL (R2THdr
->R2TSeqNum
);
2649 R2THdr
->BufferOffset
= NTOHL (R2THdr
->BufferOffset
);
2650 R2THdr
->DesiredDataTransferLength
= NTOHL (R2THdr
->DesiredDataTransferLength
);
2652 if ((R2THdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) || !ISCSI_SEQ_EQ (R2THdr
->StatSN
, Tcb
->Conn
->ExpStatSN
)) {
2653 return EFI_PROTOCOL_ERROR
;;
2656 // Check the sequence number.
2658 Status
= IScsiCheckSN (&Tcb
->ExpDataSN
, R2THdr
->R2TSeqNum
);
2659 if (EFI_ERROR (Status
)) {
2663 XferContext
= &Tcb
->XferContext
;
2664 XferContext
->TargetTransferTag
= R2THdr
->TargetTransferTag
;
2665 XferContext
->Offset
= R2THdr
->BufferOffset
;
2666 XferContext
->DesiredLength
= R2THdr
->DesiredDataTransferLength
;
2668 if (((XferContext
->Offset
+ XferContext
->DesiredLength
) > Packet
->OutTransferLength
) ||
2669 (XferContext
->DesiredLength
> Tcb
->Conn
->Session
->MaxBurstLength
)
2671 return EFI_PROTOCOL_ERROR
;
2674 // Send the data solicited by this R2T.
2676 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2677 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2684 Process the received iSCSI SCSI Response PDU.
2686 @param[in] Pdu The Response PDU received.
2687 @param[in] Tcb The task control block.
2688 @param[in, out] Packet The EXT SCSI PASS THRU request packet.
2690 @retval EFI_SUCCES The Response PDU is processed.
2691 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror occurred.
2692 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.
2693 @retval Others Other errors as indicated.
2697 IScsiOnScsiRspRcvd (
2700 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2703 SCSI_RESPONSE
*ScsiRspHdr
;
2704 ISCSI_SENSE_DATA
*SenseData
;
2708 ScsiRspHdr
= (SCSI_RESPONSE
*) NetbufGetByte (Pdu
, 0, NULL
);
2709 if (ScsiRspHdr
== NULL
) {
2710 return EFI_PROTOCOL_ERROR
;
2713 ScsiRspHdr
->InitiatorTaskTag
= NTOHL (ScsiRspHdr
->InitiatorTaskTag
);
2714 if (ScsiRspHdr
->InitiatorTaskTag
!= Tcb
->InitiatorTaskTag
) {
2715 return EFI_PROTOCOL_ERROR
;
2718 ScsiRspHdr
->StatSN
= NTOHL (ScsiRspHdr
->StatSN
);
2720 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, ScsiRspHdr
->StatSN
);
2721 if (EFI_ERROR (Status
)) {
2725 ScsiRspHdr
->MaxCmdSN
= NTOHL (ScsiRspHdr
->MaxCmdSN
);
2726 ScsiRspHdr
->ExpCmdSN
= NTOHL (ScsiRspHdr
->ExpCmdSN
);
2727 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, ScsiRspHdr
->MaxCmdSN
, ScsiRspHdr
->ExpCmdSN
);
2729 Tcb
->StatusXferd
= TRUE
;
2731 Packet
->HostAdapterStatus
= ScsiRspHdr
->Response
;
2732 if (Packet
->HostAdapterStatus
!= ISCSI_SERVICE_RSP_COMMAND_COMPLETE_AT_TARGET
) {
2736 Packet
->TargetStatus
= ScsiRspHdr
->Status
;
2738 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
| SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
) ||
2739 ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
| SCSI_RSP_PDU_FLAG_UNDERFLOW
)
2741 return EFI_PROTOCOL_ERROR
;
2744 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW
)) {
2745 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2746 Status
= EFI_BAD_BUFFER_SIZE
;
2749 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW
)) {
2750 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->BiReadResidualCount
);
2753 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_OVERFLOW
)) {
2754 if (Packet
->DataDirection
== DataIn
) {
2755 Packet
->InTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2757 Packet
->OutTransferLength
+= NTOHL (ScsiRspHdr
->ResidualCount
);
2760 Status
= EFI_BAD_BUFFER_SIZE
;
2763 if (ISCSI_FLAG_ON (ScsiRspHdr
, SCSI_RSP_PDU_FLAG_UNDERFLOW
)) {
2764 if (Packet
->DataDirection
== DataIn
) {
2765 Packet
->InTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2767 Packet
->OutTransferLength
-= NTOHL (ScsiRspHdr
->ResidualCount
);
2771 DataSegLen
= ISCSI_GET_DATASEG_LEN (ScsiRspHdr
);
2772 if (DataSegLen
!= 0) {
2773 SenseData
= (ISCSI_SENSE_DATA
*) NetbufGetByte (Pdu
, sizeof (SCSI_RESPONSE
), NULL
);
2774 if (SenseData
== NULL
) {
2775 return EFI_PROTOCOL_ERROR
;
2778 SenseData
->Length
= NTOHS (SenseData
->Length
);
2780 Packet
->SenseDataLength
= (UINT8
) MIN (SenseData
->Length
, Packet
->SenseDataLength
);
2781 if (Packet
->SenseDataLength
!= 0) {
2782 CopyMem (Packet
->SenseData
, &SenseData
->Data
[0], Packet
->SenseDataLength
);
2785 Packet
->SenseDataLength
= 0;
2793 Process the received NOP In PDU.
2795 @param[in] Pdu The NOP In PDU received.
2796 @param[in] Tcb The task control block.
2798 @retval EFI_SUCCES The NOP In PDU is processed and the related sequence
2799 numbers are updated.
2800 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror occurred.
2809 ISCSI_NOP_IN
*NopInHdr
;
2812 NopInHdr
= (ISCSI_NOP_IN
*) NetbufGetByte (Pdu
, 0, NULL
);
2813 if (NopInHdr
== NULL
) {
2814 return EFI_PROTOCOL_ERROR
;
2817 NopInHdr
->StatSN
= NTOHL (NopInHdr
->StatSN
);
2818 NopInHdr
->ExpCmdSN
= NTOHL (NopInHdr
->ExpCmdSN
);
2819 NopInHdr
->MaxCmdSN
= NTOHL (NopInHdr
->MaxCmdSN
);
2821 if (NopInHdr
->InitiatorTaskTag
== ISCSI_RESERVED_TAG
) {
2822 if (NopInHdr
->StatSN
!= Tcb
->Conn
->ExpStatSN
) {
2823 return EFI_PROTOCOL_ERROR
;
2826 Status
= IScsiCheckSN (&Tcb
->Conn
->ExpStatSN
, NopInHdr
->StatSN
);
2827 if (EFI_ERROR (Status
)) {
2832 IScsiUpdateCmdSN (Tcb
->Conn
->Session
, NopInHdr
->MaxCmdSN
, NopInHdr
->ExpCmdSN
);
2839 Execute the SCSI command issued through the EXT SCSI PASS THRU protocol.
2841 @param[in] PassThru The EXT SCSI PASS THRU protocol.
2842 @param[in] Target The target ID.
2843 @param[in] Lun The LUN.
2844 @param[in, out] Packet The request packet containing IO request, SCSI command
2845 buffer and buffers to read/write.
2847 @retval EFI_SUCCES The SCSI command is executed and the result is updated to
2849 @retval EFI_DEVICE_ERROR Session state was not as required.
2850 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
2851 @retval EFI_PROTOCOL_ERROR There is no such data in the net buffer.
2852 @retval EFI_NOT_READY The target can not accept new commands.
2853 @retval Others Other errors as indicated.
2857 IScsiExecuteScsiCommand (
2858 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*PassThru
,
2861 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
2865 ISCSI_DRIVER_DATA
*Private
;
2866 ISCSI_SESSION
*Session
;
2867 EFI_EVENT TimeoutEvent
;
2868 ISCSI_CONNECTION
*Conn
;
2871 ISCSI_XFER_CONTEXT
*XferContext
;
2873 ISCSI_IN_BUFFER_CONTEXT InBufferContext
;
2877 Private
= ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru
);
2878 Session
= Private
->Session
;
2879 Status
= EFI_SUCCESS
;
2881 TimeoutEvent
= NULL
;
2884 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
2885 Status
= EFI_DEVICE_ERROR
;
2889 Conn
= NET_LIST_USER_STRUCT_S (
2890 Session
->Conns
.ForwardLink
,
2893 ISCSI_CONNECTION_SIGNATURE
2896 if (Packet
->Timeout
!= 0) {
2897 Timeout
= MultU64x32 (Packet
->Timeout
, 4);
2900 Status
= IScsiNewTcb (Conn
, &Tcb
);
2901 if (EFI_ERROR (Status
)) {
2905 // Encapsulate the SCSI request packet into an iSCSI SCSI Command PDU.
2907 Pdu
= IScsiNewScsiCmdPdu (Packet
, Lun
, Tcb
);
2909 Status
= EFI_OUT_OF_RESOURCES
;
2913 XferContext
= &Tcb
->XferContext
;
2914 PduHdr
= NetbufGetByte (Pdu
, 0, NULL
);
2915 if (PduHdr
== NULL
) {
2916 Status
= EFI_PROTOCOL_ERROR
;
2920 XferContext
->Offset
= ISCSI_GET_DATASEG_LEN (PduHdr
);
2923 // Transmit the SCSI Command PDU.
2925 Status
= TcpIoTransmit (&Conn
->TcpIo
, Pdu
);
2929 if (EFI_ERROR (Status
)) {
2933 if (!Session
->InitialR2T
&&
2934 (XferContext
->Offset
< Session
->FirstBurstLength
) &&
2935 (XferContext
->Offset
< Packet
->OutTransferLength
)
2938 // Unsolicited Data-Out sequence is allowed. There is remaining SCSI
2939 // OUT data, and the limit of FirstBurstLength is not reached.
2941 XferContext
->TargetTransferTag
= ISCSI_RESERVED_TAG
;
2942 XferContext
->DesiredLength
= MIN (
2943 Session
->FirstBurstLength
,
2944 Packet
->OutTransferLength
- XferContext
->Offset
2947 Data
= (UINT8
*) Packet
->OutDataBuffer
+ XferContext
->Offset
;
2948 Status
= IScsiSendDataOutPduSequence (Data
, Lun
, Tcb
);
2949 if (EFI_ERROR (Status
)) {
2954 InBufferContext
.InData
= (UINT8
*) Packet
->InDataBuffer
;
2955 InBufferContext
.InDataLen
= Packet
->InTransferLength
;
2957 while (!Tcb
->StatusXferd
) {
2959 // Start the timeout timer.
2962 Status
= gBS
->SetTimer (Conn
->TimeoutEvent
, TimerRelative
, Timeout
);
2963 if (EFI_ERROR (Status
)) {
2967 TimeoutEvent
= Conn
->TimeoutEvent
;
2971 // Try to receive PDU from target.
2973 Status
= IScsiReceivePdu (Conn
, &Pdu
, &InBufferContext
, FALSE
, FALSE
, TimeoutEvent
);
2974 if (EFI_ERROR (Status
)) {
2978 PduHdr
= NetbufGetByte (Pdu
, 0, NULL
);
2979 if (PduHdr
== NULL
) {
2980 Status
= EFI_PROTOCOL_ERROR
;
2984 switch (ISCSI_GET_OPCODE (PduHdr
)) {
2985 case ISCSI_OPCODE_SCSI_DATA_IN
:
2986 Status
= IScsiOnDataInRcvd (Pdu
, Tcb
, Packet
);
2989 case ISCSI_OPCODE_R2T
:
2990 Status
= IScsiOnR2TRcvd (Pdu
, Tcb
, Lun
, Packet
);
2993 case ISCSI_OPCODE_SCSI_RSP
:
2994 Status
= IScsiOnScsiRspRcvd (Pdu
, Tcb
, Packet
);
2997 case ISCSI_OPCODE_NOP_IN
:
2998 Status
= IScsiOnNopInRcvd (Pdu
, Tcb
);
3001 case ISCSI_OPCODE_VENDOR_T0
:
3002 case ISCSI_OPCODE_VENDOR_T1
:
3003 case ISCSI_OPCODE_VENDOR_T2
:
3005 // These messages are vendor specific. Skip them.
3010 Status
= EFI_PROTOCOL_ERROR
;
3016 if (EFI_ERROR (Status
)) {
3023 if (TimeoutEvent
!= NULL
) {
3024 gBS
->SetTimer (TimeoutEvent
, TimerCancel
, 0);
3036 Reinstate the session on some error.
3038 @param[in] Session The iSCSI session
3040 @retval EFI_SUCCESS The session is reinstated from some error.
3041 @retval Other Reinstatement failed.
3045 IScsiSessionReinstatement (
3046 IN ISCSI_SESSION
*Session
3051 ASSERT (Session
->State
!= SESSION_STATE_FREE
);
3054 // Abort the session and re-init it.
3056 IScsiSessionAbort (Session
);
3057 IScsiSessionInit (Session
, TRUE
);
3062 Status
= IScsiSessionLogin (Session
);
3069 Initialize some session parameters before login.
3071 @param[in, out] Session The iSCSI session.
3072 @param[in] Recovery Whether the request is from a fresh new start or recovery.
3077 IN OUT ISCSI_SESSION
*Session
,
3082 Session
->Signature
= ISCSI_SESSION_SIGNATURE
;
3083 Session
->State
= SESSION_STATE_FREE
;
3085 InitializeListHead (&Session
->Conns
);
3086 InitializeListHead (&Session
->TcbList
);
3092 Session
->InitiatorTaskTag
= 1;
3093 Session
->NextCid
= 1;
3095 Session
->TargetPortalGroupTag
= 0;
3096 Session
->MaxConnections
= ISCSI_MAX_CONNS_PER_SESSION
;
3097 Session
->InitialR2T
= FALSE
;
3098 Session
->ImmediateData
= TRUE
;
3099 Session
->MaxBurstLength
= 262144;
3100 Session
->FirstBurstLength
= MAX_RECV_DATA_SEG_LEN_IN_FFP
;
3101 Session
->DefaultTime2Wait
= 2;
3102 Session
->DefaultTime2Retain
= 20;
3103 Session
->MaxOutstandingR2T
= DEFAULT_MAX_OUTSTANDING_R2T
;
3104 Session
->DataPDUInOrder
= TRUE
;
3105 Session
->DataSequenceInOrder
= TRUE
;
3106 Session
->ErrorRecoveryLevel
= 0;
3111 Abort the iSCSI session. That is, reset all the connection(s), and free the
3114 @param[in, out] Session The iSCSI session.
3119 IN OUT ISCSI_SESSION
*Session
3122 ISCSI_CONNECTION
*Conn
;
3123 EFI_GUID
*ProtocolGuid
;
3125 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
3129 ASSERT (!IsListEmpty (&Session
->Conns
));
3131 while (!IsListEmpty (&Session
->Conns
)) {
3132 Conn
= NET_LIST_USER_STRUCT_S (
3133 Session
->Conns
.ForwardLink
,
3136 ISCSI_CONNECTION_SIGNATURE
3138 if (!Conn
->Ipv6Flag
) {
3139 ProtocolGuid
= &gEfiTcp4ProtocolGuid
;
3141 ProtocolGuid
= &gEfiTcp6ProtocolGuid
;
3144 gBS
->CloseProtocol (
3147 Session
->Private
->Image
,
3148 Session
->Private
->ExtScsiPassThruHandle
3151 IScsiConnReset (Conn
);
3153 IScsiDetatchConnection (Conn
);
3154 IScsiDestroyConnection (Conn
);
3157 Session
->State
= SESSION_STATE_FAILED
;