X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdeModulePkg%2FUniversal%2FNetwork%2FIScsiDxe%2FIScsiProto.c;h=cb1f79b16aa3e6dc11a33d8c88822da62cbdab99;hb=4e1005eca7186cbe61aaae09108f6fdf29959f22;hp=860755839791b833b3b5a0188fbb9fea3e27d025;hpb=e48e37fce2611df7a52aff271835ff72ee396d9b;p=mirror_edk2.git diff --git a/MdeModulePkg/Universal/Network/IScsiDxe/IScsiProto.c b/MdeModulePkg/Universal/Network/IScsiDxe/IScsiProto.c index 8607558397..cb1f79b16a 100644 --- a/MdeModulePkg/Universal/Network/IScsiDxe/IScsiProto.c +++ b/MdeModulePkg/Universal/Network/IScsiDxe/IScsiProto.c @@ -1,7 +1,8 @@ -/*++ +/** @file + The implementation of iSCSI protocol based on RFC3720. -Copyright (c) 2004 - 2007, Intel Corporation -All rights reserved. This program and the accompanying materials +Copyright (c) 2004 - 2014, Intel Corporation. All rights reserved.
+This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at http://opensource.org/licenses/bsd-license.php @@ -9,91 +10,59 @@ http://opensource.org/licenses/bsd-license.php THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -Module Name: - - IScsiProto.c - -Abstract: - ---*/ +**/ #include "IScsiImpl.h" -static UINT32 mDataSegPad = 0; +UINT32 mDataSegPad = 0; + +/** + Attach the iSCSI connection to the iSCSI session. + @param[in, out] Session The iSCSI session. + @param[in, out] Conn The iSCSI connection. +**/ VOID IScsiAttatchConnection ( - IN ISCSI_SESSION *Session, - IN ISCSI_CONNECTION *Conn + IN OUT ISCSI_SESSION *Session, + IN OUT ISCSI_CONNECTION *Conn ) -/*++ - -Routine Description: - - Attach the iSCSI connection to the iSCSI session. - -Arguments: - - Session - The iSCSI session. - Conn - The iSCSI connection. - -Returns: - - None. - ---*/ { InsertTailList (&Session->Conns, &Conn->Link); Conn->Session = Session; Session->NumConns++; } +/** + Detach the iSCSI connection from the session it belongs to. + + @param[in, out] Conn The iSCSI connection. +**/ VOID IScsiDetatchConnection ( - IN ISCSI_CONNECTION *Conn + IN OUT ISCSI_CONNECTION *Conn ) -/*++ - -Routine Description: - - Detach the iSCSI connection from the session it belongs to. - -Arguments: - - Conn - The iSCSI connection. - -Returns: - - None. - ---*/ { RemoveEntryList (&Conn->Link); Conn->Session->NumConns--; Conn->Session = NULL; } -EFI_STATUS -IScsiCheckSN ( - IN UINT32 *ExpSN, - IN UINT32 NewSN - ) -/*++ - -Routine Description: - +/** Check the sequence number according to RFC3720. -Arguments: - - ExpSN - The currently expected sequence number. - NewSN - The sequence number to check. - -Returns: + @param[in, out] ExpSN The currently expected sequence number. + @param[in] NewSN The sequence number to check. - EFI_SUCCESS - The check passed and the ExpSN is increased. - ---*/ + @retval EFI_SUCCESS The check passed and the ExpSN is increased. + @retval EFI_NOT_READY Response was sent due to a retransmission request. + @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened. +**/ +EFI_STATUS +IScsiCheckSN ( + IN OUT UINT32 *ExpSN, + IN UINT32 NewSN + ) { if (!ISCSI_SEQ_EQ (NewSN, *ExpSN)) { if (ISCSI_SEQ_LT (NewSN, *ExpSN)) { @@ -113,29 +82,19 @@ Returns: } } +/** + Update the sequence numbers for the iSCSI command. + + @param[in, out] Session The iSCSI session. + @param[in] MaxCmdSN Maximum CmdSN from the target. + @param[in] ExpCmdSN Next expected CmdSN from the target. +**/ VOID IScsiUpdateCmdSN ( - IN ISCSI_SESSION *Session, - IN UINT32 MaxCmdSN, - IN UINT32 ExpCmdSN + IN OUT ISCSI_SESSION *Session, + IN UINT32 MaxCmdSN, + IN UINT32 ExpCmdSN ) -/*++ - -Routine Description: - - Update the sequence numbers for the iSCSI command. - -Arguments: - - Session - The iSCSI session. - MaxCmdSN - Maximum CmdSN from the target. - ExpCmdSN - Next expected CmdSN from the target. - -Returns: - - None. - ---*/ { if (ISCSI_SEQ_LT (MaxCmdSN, ExpCmdSN - 1)) { return ; @@ -150,27 +109,19 @@ Returns: } } -EFI_STATUS -IScsiConnLogin ( - IN ISCSI_CONNECTION *Conn - ) -/*++ - -Routine Description: - +/** This function does the iSCSI connection login. -Arguments: + @param[in, out] Conn The iSCSI connection to login. - Conn - The iSCSI connection to login. - -Returns: - - EFI_SUCCESS - The iSCSI connection is logged into the iSCSI target. - EFI_TIMEOUT - Timeout happened during the login procedure. - EFI_PROTOCOL_ERROR - Some kind of iSCSI protocol error happened. - ---*/ + @retval EFI_SUCCESS The iSCSI connection is logged into the iSCSI target. + @retval EFI_TIMEOUT Timeout happened during the login procedure. + @retval Others Other errors as indicated. +**/ +EFI_STATUS +IScsiConnLogin ( + IN OUT ISCSI_CONNECTION *Conn + ) { EFI_STATUS Status; @@ -210,50 +161,32 @@ Returns: return Status; } +/** + Reset the iSCSI connection. + + @param[in, out] Conn The iSCSI connection to reset. +**/ VOID IScsiConnReset ( - IN ISCSI_CONNECTION *Conn + IN OUT ISCSI_CONNECTION *Conn ) -/*++ - -Routine Description: - - Reset the iSCSI connection. - -Arguments: - - Conn - The iSCSI connection to reset. - -Returns: - - None. - ---*/ { Tcp4IoReset (&Conn->Tcp4Io); } +/** + Create a TCP connection for the iSCSI session. + + @param[in] Private The iSCSI driver data. + @param[in] Session Maximum CmdSN from the target. + + @return The newly created iSCSI connection. +**/ ISCSI_CONNECTION * IScsiCreateConnection ( IN ISCSI_DRIVER_DATA *Private, IN ISCSI_SESSION *Session ) -/*++ - -Routine Description: - - Create a TCP connection for the iSCSI session. - -Arguments: - - Private - The iSCSI driver data. - Session - Maximum CmdSN from the target. - -Returns: - - The newly created iSCSI connection. - ---*/ { ISCSI_CONNECTION *Conn; TCP4_IO_CONFIG_DATA Tcp4IoConfig; @@ -272,17 +205,17 @@ Returns: Conn->ExpStatSN = 0; Conn->PartialReqSent = FALSE; Conn->PartialRspRcvd = FALSE; - Conn->CID = Session->NextCID++; + Conn->Cid = Session->NextCid++; Status = gBS->CreateEvent ( - EFI_EVENT_TIMER, + EVT_TIMER, TPL_CALLBACK, NULL, NULL, &Conn->TimeoutEvent ); if (EFI_ERROR (Status)) { - gBS->FreePool (Conn); + FreePool (Conn); return NULL; } @@ -291,9 +224,9 @@ Returns: // // set the default connection-only parameters // - Conn->MaxRecvDataSegmentLength = MAX_RECV_DATA_SEG_LEN_IN_FFP; - Conn->HeaderDigest = ISCSI_DIGEST_NONE; - Conn->DataDigest = ISCSI_DIGEST_NONE; + Conn->MaxRecvDataSegmentLength = DEFAULT_MAX_RECV_DATA_SEG_LEN; + Conn->HeaderDigest = IScsiDigestNone; + Conn->DataDigest = IScsiDigestNone; CopyMem (&Tcp4IoConfig.LocalIp, &Session->ConfigData.NvData.LocalIp, sizeof (EFI_IPv4_ADDRESS)); CopyMem (&Tcp4IoConfig.SubnetMask, &Session->ConfigData.NvData.SubnetMask, sizeof (EFI_IPv4_ADDRESS)); @@ -313,68 +246,67 @@ Returns: ); if (EFI_ERROR (Status)) { gBS->CloseEvent (Conn->TimeoutEvent); - gBS->FreePool (Conn); + FreePool (Conn); Conn = NULL; } return Conn; } +/** + Destroy an iSCSI connection. + + @param[in] Conn The connection to destroy. +**/ VOID IScsiDestroyConnection ( IN ISCSI_CONNECTION *Conn ) -/*++ - -Routine Description: - - Destroy an iSCSI connection. - -Arguments: - - Conn - The connection to destroy. - -Returns: - - None. - ---*/ { Tcp4IoDestroySocket (&Conn->Tcp4Io); NetbufQueFlush (&Conn->RspQue); gBS->CloseEvent (Conn->TimeoutEvent); - gBS->FreePool (Conn); + FreePool (Conn); } -EFI_STATUS -IScsiSessionLogin ( - IN ISCSI_DRIVER_DATA *Private - ) -/*++ - -Routine Description: - +/** Login the iSCSI session. -Arguments: + @param[in] Private The iSCSI driver data. - Private - The iSCSI driver data. + @retval EFI_SUCCESS The iSCSI session login procedure finished. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_NO_MEDIA There was a media error. + @retval Others Other errors as indicated. -Returns: - - EFI_SUCCESS - The iSCSI session login procedure finished. - EFI_OUT_OF_RESOURCES - Failed to allocate memory. - EFI_PROTOCOL_ERROR - Some kind of iSCSI protocol error happened. - ---*/ +**/ +EFI_STATUS +IScsiSessionLogin ( + IN ISCSI_DRIVER_DATA *Private + ) { EFI_STATUS Status; ISCSI_SESSION *Session; ISCSI_CONNECTION *Conn; EFI_TCP4_PROTOCOL *Tcp4; + BOOLEAN MediaPresent; Session = &Private->Session; + // + // Check media status before session login + // + MediaPresent = TRUE; + NetLibDetectMedia (Private->Controller, &MediaPresent); + if (!MediaPresent) { + return EFI_NO_MEDIA; + } + + // + // Set session identifier + // + CopyMem (Session->Isid, Session->ConfigData.NvData.IsId, 6); + // // Create a connection for the session. // @@ -409,29 +341,21 @@ Returns: return Status; } -EFI_STATUS -IScsiSendLoginReq ( - IN ISCSI_CONNECTION *Conn - ) -/*++ - -Routine Description: - +/** Build and send the iSCSI login request to the iSCSI target according to the current login stage. -Arguments: - - Conn - The connection in the iSCSI login phase. + @param[in] Conn The connection in the iSCSI login phase. -Returns: - - EFI_SUCCESS - The iSCSI login request PDU is built and sent on this - connection. - EFI_OUT_OF_RESOURCES - Failed to allocate memory. - EFI_PROTOCOL_ERROR - Some kind of iSCSI protocol error happened. - ---*/ + @retval EFI_SUCCESS The iSCSI login request PDU is built and sent on this + connection. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_DEVICE_ERROR Some kind of device error happened. +**/ +EFI_STATUS +IScsiSendLoginReq ( + IN ISCSI_CONNECTION *Conn + ) { NET_BUF *Pdu; EFI_STATUS Status; @@ -453,31 +377,24 @@ Returns: return Status; } +/** + Receive and process the iSCSI login response. + + @param[in] Conn The connection in the iSCSI login phase. + + @retval EFI_SUCCESS The iSCSI login response PDU is received and processed. + @retval Others Other errors as indicated. +**/ EFI_STATUS IScsiReceiveLoginRsp ( IN ISCSI_CONNECTION *Conn ) -/*++ - -Routine Description: - - Receive and process the iSCSI login response. - -Arguments: - - Conn - The connection in the iSCSI login phase. - -Returns: - - EFI_SUCCESS - The iSCSI login response PDU is received and processed. - EFI_OUT_OF_RESOURCES - Failed to allocate memory. - EFI_PROTOCOL_ERROR - Some kind of iSCSI protocol error happened. - ---*/ { EFI_STATUS Status; NET_BUF *Pdu; + Pdu = NULL; + // // Receive the iSCSI login response. // @@ -488,6 +405,7 @@ Returns: // // A Login Response is received, process it. // + ASSERT (Pdu != NULL); Status = IScsiProcessLoginRsp (Conn, Pdu); NetbufFree (Pdu); @@ -495,34 +413,27 @@ Returns: return Status; } -EFI_STATUS -IScsiAddKeyValuePair ( - IN NET_BUF *Pdu, - IN CHAR8 *Key, - IN CHAR8 *Value - ) -/*++ - -Routine Description: - +/** Add an iSCSI key-value pair as a string into the data segment of the Login Request PDU. The DataSegmentLength and the actual size of the net buffer containing this PDU will be updated. -Arguments: + @param[in, out] Pdu The iSCSI PDU whose data segment the key-value pair will + be added to. + @param[in] Key The key name string. + @param[in] Value The value string. - Pdu - The iSCSI PDU whose data segment the key-value pair will be added to. - Key - The key name string. - Value - The value string. - -Returns: - - EFI_SUCCESS - The key-valu pair is added to the PDU's datasegment and - the correspondence length fields are updated. - EFI_OUT_OF_RESOURCES - There is not enough space in the PDU to add the key-value - pair. - ---*/ + @retval EFI_SUCCESS The key-valu pair is added to the PDU's datasegment and + the correspondence length fields are updated. + @retval EFI_OUT_OF_RESOURCES There is not enough space in the PDU to add the key-value + pair. +**/ +EFI_STATUS +IScsiAddKeyValuePair ( + IN OUT NET_BUF *Pdu, + IN CHAR8 *Key, + IN CHAR8 *Value + ) { UINT32 DataSegLen; UINT32 KeyLen; @@ -532,6 +443,9 @@ Returns: CHAR8 *Data; LoginReq = (ISCSI_LOGIN_REQUEST *) NetbufGetByte (Pdu, 0, NULL); + if (LoginReq == NULL) { + return EFI_PROTOCOL_ERROR; + } DataSegLen = NTOH24 (LoginReq->DataSegmentLength); KeyLen = (UINT32) AsciiStrLen (Key); @@ -575,25 +489,18 @@ Returns: return EFI_SUCCESS; } -NET_BUF * -IScsiPrepareLoginReq ( - IN ISCSI_CONNECTION *Conn - ) -/*++ - -Routine Description: - +/** Prepare the iSCSI login request to be sent according to the current login status. -Arguments: + @param[in, out] Conn The connection in the iSCSI login phase. - Conn - The connection in the iSCSI login phase. - -Returns: - - The pointer to the net buffer containing the iSCSI login request built. - ---*/ + @return The pointer to the net buffer containing the iSCSI login request built. + @retval Others Other errors as indicated. +**/ +NET_BUF * +IScsiPrepareLoginReq ( + IN OUT ISCSI_CONNECTION *Conn + ) { ISCSI_SESSION *Session; NET_BUF *Nbuf; @@ -608,6 +515,7 @@ Returns: } LoginReq = (ISCSI_LOGIN_REQUEST *) NetbufAllocSpace (Nbuf, sizeof (ISCSI_LOGIN_REQUEST), NET_BUF_TAIL); + ASSERT (LoginReq != NULL); ZeroMem (LoginReq, sizeof (ISCSI_LOGIN_REQUEST)); // @@ -617,9 +525,9 @@ Returns: ISCSI_SET_STAGES (LoginReq, Conn->CurrentStage, Conn->NextStage); LoginReq->VersionMax = ISCSI_VERSION_MAX; LoginReq->VersionMin = ISCSI_VERSION_MIN; - LoginReq->TSIH = HTONS (Session->TSIH); + LoginReq->Tsih = HTONS (Session->Tsih); LoginReq->InitiatorTaskTag = HTONL (Session->InitiatorTaskTag); - LoginReq->CID = HTONS (Conn->CID); + LoginReq->Cid = HTONS (Conn->Cid); LoginReq->CmdSN = HTONL (Session->CmdSN); // @@ -630,7 +538,7 @@ Returns: // with their increasing StatSN values. // LoginReq->ExpStatSN = HTONL (Conn->ExpStatSN); - CopyMem (LoginReq->ISID, Session->ISID, sizeof (LoginReq->ISID)); + CopyMem (LoginReq->Isid, Session->Isid, sizeof (LoginReq->Isid)); if (Conn->PartialRspRcvd) { // @@ -674,28 +582,22 @@ Returns: return Nbuf; } -EFI_STATUS -IScsiProcessLoginRsp ( - IN ISCSI_CONNECTION *Conn, - IN NET_BUF *Pdu - ) -/*++ - -Routine Description: - +/** Process the iSCSI Login Response. -Arguments: + @param[in, out] Conn The connection on which the iSCSI login response is received. + @param[in, out] Pdu The iSCSI login response PDU. - Conn - The connection on which the iSCSI login response is received. - Pdu - The iSCSI login response PDU. - -Returns: - - EFI_SUCCESS - The iSCSI login response PDU is processed and all check are passed. - EFI_PROTOCOL_ERROR - Some kind of iSCSI protocol error happened. - ---*/ + @retval EFI_SUCCESS The iSCSI login response PDU is processed and all check are passed. + @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened. + @retval EFI_MEDIA_CHANGED Target is redirected. + @retval Others Other errors as indicated. +**/ +EFI_STATUS +IScsiProcessLoginRsp ( + IN OUT ISCSI_CONNECTION *Conn, + IN OUT NET_BUF *Pdu + ) { EFI_STATUS Status; ISCSI_SESSION *Session; @@ -710,6 +612,9 @@ Returns: Session = Conn->Session; LoginRsp = (ISCSI_LOGIN_RESPONSE *) NetbufGetByte (Pdu, 0, NULL); + if (LoginRsp == NULL) { + return EFI_PROTOCOL_ERROR; + } if (!ISCSI_CHECK_OPCODE (LoginRsp, ISCSI_OPCODE_LOGIN_RSP)) { // // It's not a Login Response @@ -777,7 +682,7 @@ Returns: (CurrentStage != Conn->CurrentStage) || (!Conn->TransitInitiated && Transit) || (Transit && (NextStage != Conn->NextStage)) || - (CompareMem (Session->ISID, LoginRsp->ISID, sizeof (LoginRsp->ISID)) != 0) || + (CompareMem (Session->Isid, LoginRsp->Isid, sizeof (LoginRsp->Isid)) != 0) || (LoginRsp->InitiatorTaskTag != Session->InitiatorTaskTag) ) { // @@ -796,6 +701,14 @@ Returns: LoginRsp->MaxCmdSN = NTOHL (LoginRsp->MaxCmdSN); if ((Conn->CurrentStage == ISCSI_SECURITY_NEGOTIATION) && (Conn->CHAPStep == ISCSI_CHAP_INITIAL)) { + // + // If the Login Request is a leading Login Request, the target MUST use + // the value presented in CmdSN as the target value for ExpCmdSN. + // + if ((Session->State == SESSION_STATE_FREE) && (Session->CmdSN != LoginRsp->ExpCmdSN)) { + return EFI_PROTOCOL_ERROR; + } + // // It's the initial Login Response, initialize the local ExpStatSN, MaxCmdSN // and ExpCmdSN. @@ -844,14 +757,14 @@ Returns: // // In security negotiation stage, let CHAP module handle it. // - Status = IScsiCHAPOnRspReceived (Conn, Transit); + Status = IScsiCHAPOnRspReceived (Conn); break; case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION: // // Response received with negotiation resonse on iSCSI parameters, check them. // - Status = IScsiCheckOpParams (Conn, Transit); + Status = IScsiCheckOpParams (Conn); break; default: @@ -875,7 +788,7 @@ Returns: // CurrentStage is iSCSI Full Feature, it's the Login-Final Response, // get the TSIH from the Login Response. // - Session->TSIH = NTOHS (LoginRsp->TSIH); + Session->Tsih = NTOHS (LoginRsp->Tsih); } } // @@ -886,32 +799,26 @@ Returns: return Status; } -EFI_STATUS -IScsiUpdateTargetAddress ( - IN ISCSI_SESSION *Session, - IN CHAR8 *Data, - IN UINT32 Len - ) -/*++ - -Routine Description: - +/** Updated the target information according the data received in the iSCSI login response with an target redirection status. -Arguments: - - Session - The iSCSI session. - Data - The data segment which should contain the TargetAddress key-value list. - Len - Length of the data. - -Returns: - - EFI_SUCCESS - The target address is updated. - EFI_OUT_OF_RESOURCES - Failed to allocate memory. - EFI_NOT_FOUND - The TargetAddress key is not found. - ---*/ + @param[in, out] Session The iSCSI session. + @param[in] Data The data segment which should contain the + TargetAddress key-value list. + @param[in] Len Length of the data. + + @retval EFI_SUCCESS The target address is updated. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_NOT_FOUND The TargetAddress key is not found. + @retval Others Other errors as indicated. +**/ +EFI_STATUS +IScsiUpdateTargetAddress ( + IN OUT ISCSI_SESSION *Session, + IN CHAR8 *Data, + IN UINT32 Len + ) { LIST_ENTRY *KeyValueList; CHAR8 *TargetAddress; @@ -943,7 +850,7 @@ Returns: IpStr = TargetAddress; - while (*TargetAddress && (*TargetAddress != ':') && (*TargetAddress != ',')) { + while ((*TargetAddress != 0) && (*TargetAddress != ':') && (*TargetAddress != ',')) { // // NULL, ':' or ',' ends the IPv4 string. // @@ -989,54 +896,55 @@ Returns: return Status; } +/** + The callback function to free the net buffer list. + + @param[in] Arg The opaque parameter. +**/ VOID +EFIAPI IScsiFreeNbufList ( VOID *Arg ) -/*++ - -Routine Description: - - The callback function to free the net buffer list. - -Arguments: - - Arg - The opaque parameter. - -Returns: - - None. - ---*/ { ASSERT (Arg != NULL); NetbufFreeList ((LIST_ENTRY *) Arg); - gBS->FreePool (Arg); + FreePool (Arg); } +/** + The callback function called in NetBufFree, it does nothing. + + @param[in] Arg The opaque parameter. +**/ VOID +EFIAPI IScsiNbufExtFree ( VOID *Arg ) -/*++ - -Routine Description: - - The callback function called in NetBufFree, it does nothing. - -Arguments: - - Arg - The opaque parameter. - -Returns: - - None. - ---*/ { } +/** + Receive an iSCSI response PDU. An iSCSI response PDU contains an iSCSI PDU header and + an optional data segment. The two parts will be put into two blocks of buffers in the + net buffer. The digest check will be conducted in this function if needed and the digests + will be trimmed from the PDU buffer. + + @param[in] Conn The iSCSI connection to receive data from. + @param[out] Pdu The received iSCSI pdu. + @param[in] Context The context used to describe information on the caller provided + buffer to receive data segment of the iSCSI pdu, it's optional. + @param[in] HeaderDigest Whether there will be header digest received. + @param[in] DataDigest Whether there will be data digest. + @param[in] TimeoutEvent The timeout event, it's optional. + + @retval EFI_SUCCESS An iSCSI pdu is received. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened. + @retval Others Other errors as indicated. +**/ EFI_STATUS IScsiReceivePdu ( IN ISCSI_CONNECTION *Conn, @@ -1046,31 +954,6 @@ IScsiReceivePdu ( IN BOOLEAN DataDigest, IN EFI_EVENT TimeoutEvent OPTIONAL ) -/*++ - -Routine Description: - - Receive an iSCSI response PDU. An iSCSI response PDU contains an iSCSI PDU header and - an optional data segment. The two parts will be put into two blocks of buffers in the - net buffer. The digest check will be conducted in this function if needed and the digests - will be trimmed from the PDU buffer. - -Arguments: - - Conn - The iSCSI connection to receive data from. - Pdu - The received iSCSI pdu. - Context - The context used to describe information on the caller provided - buffer to receive data segment of the iSCSI pdu, it's optional. - HeaderDigest - Whether there will be header digest received. - DataDigest - Whether there will be data digest. - TimeoutEvent - The timeout event, it's optional. - -Returns: - - EFI_SUCCESS - An iSCSI pdu is received. - EFI_TIMEOUT - Timeout happenend. - ---*/ { LIST_ENTRY *NbufList; UINT32 Len; @@ -1084,7 +967,7 @@ Returns: NET_BUF *DataSeg; UINT32 PadAndCRC32[2]; - NbufList = AllocatePool (sizeof (LIST_ENTRY )); + NbufList = AllocatePool (sizeof (LIST_ENTRY)); if (NbufList == NULL) { return EFI_OUT_OF_RESOURCES; } @@ -1097,11 +980,12 @@ Returns: Len = sizeof (ISCSI_BASIC_HEADER) + (HeaderDigest ? sizeof (UINT32) : 0); PduHdr = NetbufAlloc (Len); if (PduHdr == NULL) { - Status = EFI_OUT_OF_RESOURCES; - goto ON_EXIT; + FreePool (NbufList); + return EFI_OUT_OF_RESOURCES; } Header = NetbufAllocSpace (PduHdr, Len, NET_BUF_TAIL); + ASSERT (Header != NULL); InsertTailList (NbufList, &PduHdr->List); // @@ -1125,9 +1009,14 @@ Returns: Len = ISCSI_GET_DATASEG_LEN (Header); if (Len == 0) { // - // No data segment. + // No data segment.Form the pdu from a list of pdu segments. // - goto FORM_PDU; + *Pdu = NetbufFromBufList (NbufList, 0, 0, IScsiFreeNbufList, NbufList); + if (*Pdu == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + return Status; } // // Get the length of the padding bytes of the data segment. @@ -1155,8 +1044,7 @@ Returns: // The first to receive the useful data. The second to receive the padding. // Fragment[1].Len = PadLen + (DataDigest ? sizeof (UINT32) : 0); - Fragment[1].Bulk = (UINT8 *) ((UINTN) &PadAndCRC32[1] - PadLen); - + Fragment[1].Bulk = (UINT8 *)PadAndCRC32 + (4 - PadLen); FragmentCount = 2; } else { FragmentCount = 1; @@ -1221,7 +1109,6 @@ Returns: NetbufTrim (DataSeg, PadLen, NET_BUF_TAIL); } -FORM_PDU: // // Form the pdu from a list of pdu segments. // @@ -1242,28 +1129,19 @@ ON_EXIT: return Status; } -EFI_STATUS -IScsiCheckOpParams ( - IN ISCSI_CONNECTION *Conn, - IN BOOLEAN Transit - ) -/*++ - -Routine Description: - +/** Check and get the result of the prameter negotiation. -Arguments: - - Conn - The connection in iSCSI login. - Pdu - The iSCSI response PDU containing the parameter list. + @param[in, out] Conn The connection in iSCSI login. -Returns: - - EFI_SUCCESS - The parmeter check is passed and negotiation is finished. - EFI_PROTOCOL_ERROR - Some kind of iSCSI protocol error happened. - ---*/ + @retval EFI_SUCCESS The parmeter check is passed and negotiation is finished. + @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. +**/ +EFI_STATUS +IScsiCheckOpParams ( + IN OUT ISCSI_CONNECTION *Conn + ) { EFI_STATUS Status; LIST_ENTRY *KeyValueList; @@ -1292,7 +1170,7 @@ Returns: // KeyValueList = IScsiBuildKeyValueList (Data, Len); if (KeyValueList == NULL) { - gBS->FreePool (Data); + FreePool (Data); return Status; } // @@ -1304,11 +1182,11 @@ Returns: } if (AsciiStrCmp (Value, "CRC32") == 0) { - if (Conn->HeaderDigest != ISCSI_DIGEST_CRC32) { + if (Conn->HeaderDigest != IScsiDigestCRC32) { goto ON_ERROR; } } else if (AsciiStrCmp (Value, ISCSI_KEY_VALUE_NONE) == 0) { - Conn->HeaderDigest = ISCSI_DIGEST_NONE; + Conn->HeaderDigest = IScsiDigestNone; } else { goto ON_ERROR; } @@ -1321,11 +1199,11 @@ Returns: } if (AsciiStrCmp (Value, "CRC32") == 0) { - if (Conn->DataDigest != ISCSI_DIGEST_CRC32) { + if (Conn->DataDigest != IScsiDigestCRC32) { goto ON_ERROR; } } else if (AsciiStrCmp (Value, ISCSI_KEY_VALUE_NONE) == 0) { - Conn->DataDigest = ISCSI_DIGEST_NONE; + Conn->DataDigest = IScsiDigestNone; } else { goto ON_ERROR; } @@ -1365,16 +1243,11 @@ Returns: Session->ImmediateData = (BOOLEAN) (Session->ImmediateData && (AsciiStrCmp (Value, "Yes") == 0)); // - // MaxRecvDataSegmentLength, result function is Mininum. + // MaxRecvDataSegmentLength is declarative. // Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH); if (Value != NULL) { - // - // MaxRecvDataSegmentLength is declarative. - // - NumericValue = AsciiStrDecimalToUintn (Value); - - Conn->MaxRecvDataSegmentLength = (UINT32) MIN (Conn->MaxRecvDataSegmentLength, NumericValue); + Conn->MaxRecvDataSegmentLength = (UINT32) AsciiStrDecimalToUintn (Value); } // // MaxBurstLength, result funtion is Mininum. @@ -1390,9 +1263,10 @@ Returns: // // FirstBurstLength, result function is Minimum. Irrelevant when InitialR2T=Yes and // ImmediateData=No. + // This Key/Value is negotiation type. // Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_FIRST_BURST_LENGTH); - if ((Value == NULL) && !(Session->InitialR2T && !Session->ImmediateData)) { + if (Value == NULL) { goto ON_ERROR; } @@ -1499,43 +1373,34 @@ ON_ERROR: IScsiFreeKeyValueList (KeyValueList); - gBS->FreePool (Data); + FreePool (Data); return Status; } +/** + Fill the oprational prameters. + + @param[in] Conn The connection in iSCSI login. + @param[in, out] Pdu The iSCSI login request PDU to fill the parameters. + + @retval EFI_SUCCESS The parmeters are filled into the iSCSI login request PDU. +**/ EFI_STATUS IScsiFillOpParams ( IN ISCSI_CONNECTION *Conn, - IN NET_BUF *Pdu + IN OUT NET_BUF *Pdu ) -/*++ - -Routine Description: - - Fill the oprational prameters. - -Arguments: - - Conn - The connection in iSCSI login. - Pdu - The iSCSI login request PDU to fill the parameters. - -Returns: - - EFI_SUCCESS - The parmeters are filled into the iSCSI login request PDU. - EFI_OUT_OF_RESOURCES - There is not enough space in the PDU to hold the parameters. - ---*/ { ISCSI_SESSION *Session; CHAR8 Value[256]; Session = Conn->Session; - AsciiSPrint (Value, sizeof (Value), "%a", (Conn->HeaderDigest == ISCSI_DIGEST_CRC32) ? "None,CRC32" : "None"); + AsciiSPrint (Value, sizeof (Value), "%a", (Conn->HeaderDigest == IScsiDigestCRC32) ? "None,CRC32" : "None"); IScsiAddKeyValuePair (Pdu, ISCSI_KEY_HEADER_DIGEST, Value); - AsciiSPrint (Value, sizeof (Value), "%a", (Conn->DataDigest == ISCSI_DIGEST_CRC32) ? "None,CRC32" : "None"); + AsciiSPrint (Value, sizeof (Value), "%a", (Conn->DataDigest == IScsiDigestCRC32) ? "None,CRC32" : "None"); IScsiAddKeyValuePair (Pdu, ISCSI_KEY_DATA_DIGEST, Value); AsciiSPrint (Value, sizeof (Value), "%d", Session->ErrorRecoveryLevel); @@ -1547,7 +1412,7 @@ Returns: AsciiSPrint (Value, sizeof (Value), "%a", Session->ImmediateData ? "Yes" : "No"); IScsiAddKeyValuePair (Pdu, ISCSI_KEY_IMMEDIATE_DATA, Value); - AsciiSPrint (Value, sizeof (Value), "%d", Conn->MaxRecvDataSegmentLength); + AsciiSPrint (Value, sizeof (Value), "%d", MAX_RECV_DATA_SEG_LEN_IN_FFP); IScsiAddKeyValuePair (Pdu, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH, Value); AsciiSPrint (Value, sizeof (Value), "%d", Session->MaxBurstLength); @@ -1577,29 +1442,21 @@ Returns: return EFI_SUCCESS; } -EFI_STATUS -IScsiPadSegment ( - IN NET_BUF *Pdu, - IN UINT32 Len - ) -/*++ - -Routine Description: - +/** Pad the iSCSI AHS or data segment to an integer number of 4 byte words. -Arguments: - - Pdu - The iSCSI pdu which contains segments to pad. - Len - The length of the last semgnet in the PDU. + @param[in, out] Pdu The iSCSI pdu which contains segments to pad. + @param[in] Len The length of the last semgnet in the PDU. -Returns: - - EFI_SUCCESS - The segment is padded or no need to pad it. - EFI_OUT_OF_RESOURCES - There is not enough remaining free space to add the - padding bytes. - ---*/ + @retval EFI_SUCCESS The segment is padded or no need to pad it. + @retval EFI_OUT_OF_RESOURCES There is not enough remaining free space to add the + padding bytes. +**/ +EFI_STATUS +IScsiPadSegment ( + IN OUT NET_BUF *Pdu, + IN UINT32 Len + ) { UINT32 PadLen; UINT8 *Data; @@ -1618,32 +1475,25 @@ Returns: return EFI_SUCCESS; } -LIST_ENTRY * +/** + Build a key-value list from the data segment. + + @param[in] Data The data segment containing the key-value pairs. + @param[in] Len Length of the data segment. + + @return The key-value list. + @retval NULL Other errors as indicated. +**/ +LIST_ENTRY * IScsiBuildKeyValueList ( IN CHAR8 *Data, IN UINT32 Len ) -/*++ - -Routine Description: - - Build a key-value list from the data segment. - -Arguments: - - Data - The data segment containing the key-value pairs. - Len - Length of the data segment. - -Returns: - - The key-value list. - ---*/ { LIST_ENTRY *ListHead; ISCSI_KEY_VALUE_PAIR *KeyValuePair; - ListHead = AllocatePool (sizeof (LIST_ENTRY )); + ListHead = AllocatePool (sizeof (LIST_ENTRY)); if (ListHead == NULL) { return NULL; } @@ -1671,7 +1521,7 @@ Returns: Data++; Len--; } else { - gBS->FreePool (KeyValuePair); + FreePool (KeyValuePair); goto ON_ERROR; } @@ -1692,28 +1542,20 @@ ON_ERROR: return NULL; } -CHAR8 * -IScsiGetValueByKeyFromList ( - IN LIST_ENTRY *KeyValueList, - IN CHAR8 *Key - ) -/*++ - -Routine Description: - +/** Get the value string by the key name from the key-value list. If found, the key-value entry will be removed from the list. -Arguments: + @param[in, out] KeyValueList The key-value list. + @param[in] Key The key name to find. - KeyValueList - The key-value list. - Key - The key name to find. - -Returns: - - The value string. - ---*/ + @return The value string. +**/ +CHAR8 * +IScsiGetValueByKeyFromList ( + IN OUT LIST_ENTRY *KeyValueList, + IN CHAR8 *Key + ) { LIST_ENTRY *Entry; ISCSI_KEY_VALUE_PAIR *KeyValuePair; @@ -1728,7 +1570,7 @@ Returns: Value = KeyValuePair->Value; RemoveEntryList (&KeyValuePair->List); - gBS->FreePool (KeyValuePair); + FreePool (KeyValuePair); break; } } @@ -1736,25 +1578,15 @@ Returns: return Value; } +/** + Free the key-value list. + + @param[in] KeyValueList The key-value list. +**/ VOID IScsiFreeKeyValueList ( IN LIST_ENTRY *KeyValueList ) -/*++ - -Routine Description: - - Free the key-value list. - -Arguments: - - KeyValueList - The key-value list. - -Returns: - - None. - ---*/ { LIST_ENTRY *Entry; ISCSI_KEY_VALUE_PAIR *KeyValuePair; @@ -1763,34 +1595,26 @@ Returns: Entry = NetListRemoveHead (KeyValueList); KeyValuePair = NET_LIST_USER_STRUCT (Entry, ISCSI_KEY_VALUE_PAIR, List); - gBS->FreePool (KeyValuePair); + FreePool (KeyValuePair); } - gBS->FreePool (KeyValueList); + FreePool (KeyValueList); } -EFI_STATUS -IScsiNormalizeName ( - IN CHAR8 *Name, - IN UINTN Len - ) -/*++ - -Routine Description: - +/** Normalize the iSCSI name according to RFC. -Arguments: - - Name - The iSCSI name. - Len - length of the iSCSI name. + @param[in, out] Name The iSCSI name. + @param[in] Len length of the iSCSI name. -Returns: - - EFI_SUCCESS - The iSCSI name is valid and normalized. - EFI_PROTOCOL_ERROR - The iSCSI name is mal-formatted or not in the IQN format. - ---*/ + @retval EFI_SUCCESS The iSCSI name is valid and normalized. + @retval EFI_PROTOCOL_ERROR The iSCSI name is mal-formatted or not in the IQN format. +**/ +EFI_STATUS +IScsiNormalizeName ( + IN OUT CHAR8 *Name, + IN UINTN Len + ) { UINTN Index; @@ -1826,28 +1650,21 @@ Returns: return EFI_SUCCESS; } +/** + Create an iSCSI task control block. + + @param[in] Conn The connection on which the task control block will be created. + @param[out] Tcb The newly created task control block. + + @retval EFI_SUCCESS The task control block is created. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_NOT_READY The target can not accept new commands. +**/ EFI_STATUS IScsiNewTcb ( IN ISCSI_CONNECTION *Conn, OUT ISCSI_TCB **Tcb ) -/*++ - -Routine Description: - - Create an iSCSI task control block. - -Arguments: - - Conn - The connection on which the task control block will be created. - Tcb - The newly created task control block. - -Returns: - - EFI_SUCCESS - The task control block is created. - EFI_OUT_OF_RESOURCES - Failed to allocate memory. - ---*/ { ISCSI_SESSION *Session; ISCSI_TCB *NewTcb; @@ -1885,52 +1702,34 @@ Returns: return EFI_SUCCESS; } +/** + Delete the tcb from the connection and destroy it. + + @param[in] Tcb The tcb to delete. +**/ VOID IScsiDelTcb ( IN ISCSI_TCB *Tcb ) -/*++ - -Routine Description: - - Delete the tcb from the connection and destroy it. - -Arguments: - - Tcb - The tcb to delete. - -Returns: - - None. - ---*/ { RemoveEntryList (&Tcb->Link); - gBS->FreePool (Tcb); + FreePool (Tcb); } +/** + Find the task control block by the initator task tag. + + @param[in] TcbList The tcb list. + @param[in] InitiatorTaskTag The initiator task tag. + + @return The task control block found. +**/ ISCSI_TCB * IScsiFindTcbByITT ( IN LIST_ENTRY *TcbList, IN UINT32 InitiatorTaskTag ) -/*++ - -Routine Description: - - Find the task control block by the initator task tag. - -Arguments: - - TcbList - The tcb list. - InitiatorTaskTag - The initiator task tag. - -Returns: - - The task control block found. - ---*/ { ISCSI_TCB *Tcb; LIST_ENTRY *Entry; @@ -1950,29 +1749,21 @@ Returns: return Tcb; } +/** + Create a data segment, pad it and calculate the CRC if needed. + + @param[in] Data The data to fill into the data segment. + @param[in] Len Length of the data. + @param[in] DataDigest Whether to calculate CRC for this data segment. + + @return The net buffer wrapping the data segment. +**/ NET_BUF * IScsiNewDataSegment ( IN UINT8 *Data, IN UINT32 Len, IN BOOLEAN DataDigest ) -/*++ - -Routine Description: - - Create a data segment, pad it and calculate the CRC if needed. - -Arguments: - - Data - The data to fill into the data segment. - Len - Length of the data. - DataDigest - Whether to calculate CRC for this data segment. - -Returns: - - The net buffer wrapping the data segment. - ---*/ { NET_FRAGMENT Fragment[2]; UINT32 FragmentCount; @@ -1997,30 +1788,23 @@ Returns: return DataSeg; } +/** + Create a iSCSI SCSI command PDU to encapsulate the command issued + by SCSI through the EXT SCSI PASS THRU Protocol. + + @param[in] Packet The EXT SCSI PASS THRU request packet containing the SCSI command. + @param[in] Lun The LUN. + @param[in] Tcb The tcb assocated with this SCSI command. + + @return The created iSCSI SCSI command PDU. + @retval NULL Other errors as indicated. +**/ NET_BUF * IScsiNewScsiCmdPdu ( IN EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet, IN UINT64 Lun, IN ISCSI_TCB *Tcb ) -/*++ - -Routine Description: - - Create a iSCSI SCSI command PDU to encapsulate the command issued - by SCSI through the EXT SCSI PASS THRU Protocol. - -Arguments: - - Packet - The EXT SCSI PASS THRU request packet containing the SCSI command. - Lun - The LUN. - Tcb - The tcb assocated with this SCSI command. - -Returns: - - The created iSCSI SCSI command PDU. - ---*/ { LIST_ENTRY *NbufList; NET_BUF *Pdu; @@ -2058,6 +1842,10 @@ Returns: } ScsiCmd = (SCSI_COMMAND *) NetbufAllocSpace (PduHeader, Length, NET_BUF_TAIL); + if (ScsiCmd == NULL) { + NetbufFree (PduHeader); + return NULL; + } Header = (ISCSI_ADDITIONAL_HEADER *) (ScsiCmd + 1); ZeroMem (ScsiCmd, Length); @@ -2102,10 +1890,10 @@ Returns: ScsiCmd->CmdSN = NTOHL (Tcb->CmdSN); ScsiCmd->ExpStatSN = NTOHL (Tcb->Conn->ExpStatSN); - CopyMem (ScsiCmd->CDB, Packet->Cdb, sizeof (ScsiCmd->CDB)); + CopyMem (ScsiCmd->Cdb, Packet->Cdb, sizeof (ScsiCmd->Cdb)); if (Packet->CdbLength > 16) { - Header->Length = NTOHS (Packet->CdbLength - 15); + Header->Length = NTOHS ((UINT16) (Packet->CdbLength - 15)); Header->Type = ISCSI_AHS_TYPE_EXT_CDB; CopyMem (Header + 1, (UINT8 *) Packet->Cdb + 16, Packet->CdbLength - 16); @@ -2139,7 +1927,7 @@ Returns: goto ON_EXIT; } - NbufList = AllocatePool (sizeof (LIST_ENTRY )); + NbufList = AllocatePool (sizeof (LIST_ENTRY)); if (NbufList == NULL) { NetbufFree (PduHeader); NetbufFree (DataSeg); @@ -2164,7 +1952,7 @@ Returns: ) { // // Unsolicited data out sequence is not allowed, - // or FirstBustLength data is already sent out by immediate data + // or FirstBurstLength data is already sent out by immediate data // or all the OUT data accompany this SCSI packet is sent as // immediate data, the final flag should be set on this SCSI Command // PDU. @@ -2177,6 +1965,18 @@ ON_EXIT: return Pdu; } +/** + Create a new iSCSI SCSI Data Out PDU. + + @param[in] Data The data to put into the Data Out PDU. + @param[in] Len Length of the data. + @param[in] DataSN The DataSN of the Data Out PDU. + @param[in] Tcb The task control block of this Data Out PDU. + @param[in] Lun The LUN. + + @return The net buffer wrapping the Data Out PDU. + @retval NULL Other errors as indicated. +**/ NET_BUF * IScsiNewDataOutPdu ( IN UINT8 *Data, @@ -2185,25 +1985,6 @@ IScsiNewDataOutPdu ( IN ISCSI_TCB *Tcb, IN UINT64 Lun ) -/*++ - -Routine Description: - - Create a new iSCSI SCSI Data Out PDU. - -Arguments: - - Data - The data to put into the Data Out PDU. - Len - Length of the data. - DataSN - The DataSN of the Data Out PDU. - Tcb - The task control block of this Data Out PDU. - Lun - The LUN. - -Returns: - - The net buffer wrapping the Data Out PDU. - ---*/ { LIST_ENTRY *NbufList; NET_BUF *PduHdr; @@ -2212,7 +1993,7 @@ Returns: ISCSI_SCSI_DATA_OUT *DataOutHdr; ISCSI_XFER_CONTEXT *XferContext; - NbufList = AllocatePool (sizeof (LIST_ENTRY )); + NbufList = AllocatePool (sizeof (LIST_ENTRY)); if (NbufList == NULL) { return NULL; } @@ -2224,7 +2005,7 @@ Returns: // PduHdr = NetbufAlloc (sizeof (ISCSI_SCSI_DATA_OUT)); if (PduHdr == NULL) { - gBS->FreePool (NbufList); + FreePool (NbufList); return NULL; } // @@ -2233,6 +2014,7 @@ Returns: InsertTailList (NbufList, &PduHdr->List); DataOutHdr = (ISCSI_SCSI_DATA_OUT *) NetbufAllocSpace (PduHdr, sizeof (ISCSI_SCSI_DATA_OUT), NET_BUF_TAIL); + ASSERT (DataOutHdr != NULL); XferContext = &Tcb->XferContext; ZeroMem (DataOutHdr, sizeof (ISCSI_SCSI_DATA_OUT)); @@ -2273,29 +2055,22 @@ Returns: return Pdu; } -LIST_ENTRY * +/** + Generate a consecutive sequence of iSCSI SCSI Data Out PDUs. + + @param[in] Data The data which will be carried by the sequence of iSCSI SCSI Data Out PDUs. + @param[in] Tcb The task control block of the data to send out. + @param[in] Lun The LUN the data will be sent to. + + @return A list of net buffers with each of them wraps an iSCSI SCSI Data Out PDU. + @retval NULL Other errors as indicated. +**/ +LIST_ENTRY * IScsiGenerateDataOutPduSequence ( IN UINT8 *Data, IN ISCSI_TCB *Tcb, IN UINT64 Lun ) -/*++ - -Routine Description: - - Generate a consecutive sequence of iSCSI SCSI Data Out PDUs. - -Arguments: - - Data - The data which will be carried by the sequence of iSCSI SCSI Data Out PDUs. - Tcb - The task control block of the data to send out. - Lun - The LUN the data will be sent to. - -Returns: - - A list of net buffers with each of them wraps an iSCSI SCSI Data Out PDU. - ---*/ { LIST_ENTRY *PduList; UINT32 DataSN; @@ -2303,8 +2078,9 @@ Returns: NET_BUF *DataOutPdu; ISCSI_CONNECTION *Conn; ISCSI_XFER_CONTEXT *XferContext; + UINT8 *DataOutPacket; - PduList = AllocatePool (sizeof (LIST_ENTRY )); + PduList = AllocatePool (sizeof (LIST_ENTRY)); if (PduList == NULL) { return NULL; } @@ -2321,7 +2097,6 @@ Returns: // Determine the length of data this Data Out PDU can carry. // DataLen = MIN (XferContext->DesiredLength, Conn->MaxRecvDataSegmentLength); - Data += DataLen; // // Create a Data Out PDU. @@ -2342,41 +2117,42 @@ Returns: XferContext->Offset += DataLen; XferContext->DesiredLength -= DataLen; DataSN++; + Data += DataLen; } // // Set the F bit for the last data out PDU in this sequence. // - ISCSI_SET_FLAG (NetbufGetByte (DataOutPdu, 0, NULL), ISCSI_BHS_FLAG_FINAL); + DataOutPacket = NetbufGetByte (DataOutPdu, 0, NULL); + if (DataOutPacket == NULL) { + IScsiFreeNbufList (PduList); + PduList = NULL; + goto ON_EXIT; + } + + ISCSI_SET_FLAG (DataOutPacket, ISCSI_BHS_FLAG_FINAL); ON_EXIT: return PduList; } +/** + Send the Data in a sequence of Data Out PDUs one by one. + + @param[in] Data The data to carry by Data Out PDUs. + @param[in] Lun The LUN the data will be sent to. + @param[in] Tcb The task control block. + + @retval EFI_SUCCES The data is sent out to the LUN. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval Others Other errors as indicated. +**/ EFI_STATUS IScsiSendDataOutPduSequence ( IN UINT8 *Data, IN UINT64 Lun, IN ISCSI_TCB *Tcb ) -/*++ - -Routine Description: - - Send the Data in a sequence of Data Out PDUs one by one. - -Arguments: - - Data - The data to carry by Data Out PDUs. - Lun - The LUN the data will be sent to. - Tcb - The task control block. - -Returns: - - EFI_SUCCES - The data is sent out to the LUN. - EFI_OUT_OF_RESOURCES - Failed to allocate memory. - ---*/ { LIST_ENTRY *DataOutPduList; LIST_ENTRY *Entry; @@ -2410,36 +2186,33 @@ Returns: return Status; } +/** + Process the received iSCSI SCSI Data In PDU. + + @param[in] Pdu The Data In PDU received. + @param[in] Tcb The task control block. + @param[in, out] Packet The EXT SCSI PASS THRU request packet. + + @retval EFI_SUCCES The check on the Data IN PDU is passed and some update + actions are taken. + @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened. + @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request. + @retval Others Other errors as indicated. +**/ EFI_STATUS IScsiOnDataInRcvd ( IN NET_BUF *Pdu, IN ISCSI_TCB *Tcb, IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet ) -/*++ - -Routine Description: - - Process the received iSCSI SCSI Data In PDU. - -Arguments: - - Pdu - The Data In PDU received. - Tcb - The task control block. - Packet - The EXT SCSI PASS THRU request packet. - -Returns: - - EFI_SUCCES - The check on the Data IN PDU is passed and some update - actions are taken. - EFI_PROTOCOL_ERROR - Some kind of iSCSI protocol errror happened. - ---*/ { ISCSI_SCSI_DATA_IN *DataInHdr; EFI_STATUS Status; DataInHdr = (ISCSI_SCSI_DATA_IN *) NetbufGetByte (Pdu, 0, NULL); + if (DataInHdr == NULL) { + return EFI_PROTOCOL_ERROR; + } DataInHdr->InitiatorTaskTag = NTOHL (DataInHdr->InitiatorTaskTag); DataInHdr->ExpCmdSN = NTOHL (DataInHdr->ExpCmdSN); @@ -2502,6 +2275,18 @@ Returns: return Status; } +/** + Process the received iSCSI R2T PDU. + + @param[in] Pdu The R2T PDU received. + @param[in] Tcb The task control block. + @param[in] Lun The Lun. + @param[in, out] Packet The EXT SCSI PASS THRU request packet. + + @retval EFI_SUCCES The R2T PDU is valid and the solicited data is sent out. + @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened. + @retval Others Other errors as indicated. +**/ EFI_STATUS IScsiOnR2TRcvd ( IN NET_BUF *Pdu, @@ -2509,25 +2294,6 @@ IScsiOnR2TRcvd ( IN UINT64 Lun, IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet ) -/*++ - -Routine Description: - - Process the received iSCSI R2T PDU. - -Arguments: - - Pdu - The R2T PDU received. - Tcb - The task control block. - Lun - The Lun. - Packet - The EXT SCSI PASS THRU request packet. - -Returns: - - EFI_SUCCES - The R2T PDU is valid and the solicited data is sent out. - EFI_PROTOCOL_ERROR - Some kind of iSCSI protocol errror happened. - ---*/ { ISCSI_READY_TO_TRANSFER *R2THdr; EFI_STATUS Status; @@ -2535,11 +2301,14 @@ Returns: UINT8 *Data; R2THdr = (ISCSI_READY_TO_TRANSFER *) NetbufGetByte (Pdu, 0, NULL); + if (R2THdr == NULL) { + return EFI_PROTOCOL_ERROR; + } R2THdr->InitiatorTaskTag = NTOHL (R2THdr->InitiatorTaskTag); R2THdr->TargetTransferTag = NTOHL (R2THdr->TargetTransferTag); R2THdr->StatSN = NTOHL (R2THdr->StatSN); - R2THdr->R2TSN = NTOHL (R2THdr->R2TSN); + R2THdr->R2TSeqNum = NTOHL (R2THdr->R2TSeqNum); R2THdr->BufferOffset = NTOHL (R2THdr->BufferOffset); R2THdr->DesiredDataTransferLength = NTOHL (R2THdr->DesiredDataTransferLength); @@ -2549,7 +2318,7 @@ Returns: // // Check the sequence number. // - Status = IScsiCheckSN (&Tcb->ExpDataSN, R2THdr->R2TSN); + Status = IScsiCheckSN (&Tcb->ExpDataSN, R2THdr->R2TSeqNum); if (EFI_ERROR (Status)) { return Status; } @@ -2573,30 +2342,24 @@ Returns: return Status; } +/** + Process the received iSCSI SCSI Response PDU. + + @param[in] Pdu The Response PDU received. + @param[in] Tcb The task control block. + @param[in, out] Packet The EXT SCSI PASS THRU request packet. + + @retval EFI_SUCCES The Response PDU is processed. + @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened. + @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request. + @retval Others Other errors as indicated. +**/ EFI_STATUS IScsiOnScsiRspRcvd ( IN NET_BUF *Pdu, IN ISCSI_TCB *Tcb, IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet ) -/*++ - -Routine Description: - - Process the received iSCSI SCSI Response PDU. - -Arguments: - - Pdu - The Response PDU received. - Tcb - The task control block. - Packet - The EXT SCSI PASS THRU request packet. - -Returns: - - EFI_SUCCES - The Response PDU is processed. - EFI_PROTOCOL_ERROR - Some kind of iSCSI protocol errror happened. - ---*/ { SCSI_RESPONSE *ScsiRspHdr; ISCSI_SENSE_DATA *SenseData; @@ -2604,6 +2367,9 @@ Returns: UINT32 DataSegLen; ScsiRspHdr = (SCSI_RESPONSE *) NetbufGetByte (Pdu, 0, NULL); + if (ScsiRspHdr == NULL) { + return EFI_PROTOCOL_ERROR; + } ScsiRspHdr->InitiatorTaskTag = NTOHL (ScsiRspHdr->InitiatorTaskTag); if (ScsiRspHdr->InitiatorTaskTag != Tcb->InitiatorTaskTag) { @@ -2666,6 +2432,9 @@ Returns: DataSegLen = ISCSI_GET_DATASEG_LEN (ScsiRspHdr); if (DataSegLen != 0) { SenseData = (ISCSI_SENSE_DATA *) NetbufGetByte (Pdu, sizeof (SCSI_RESPONSE), NULL); + if (SenseData == NULL) { + return EFI_PROTOCOL_ERROR; + } SenseData->Length = NTOHS (SenseData->Length); @@ -2680,34 +2449,29 @@ Returns: return Status; } +/** + Process the received NOP In PDU. + + @param[in] Pdu The NOP In PDU received. + @param[in] Tcb The task control block. + + @retval EFI_SUCCES The NOP In PDU is processed and the related sequence + numbers are updated. + @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror happened. +**/ EFI_STATUS IScsiOnNopInRcvd ( IN NET_BUF *Pdu, IN ISCSI_TCB *Tcb ) -/*++ - -Routine Description: - - Process the received NOP In PDU. - -Arguments: - - Pdu - The NOP In PDU received. - Tcb - The task control block. - -Returns: - - EFI_SUCCES - The NOP In PDU is processed and the related sequence - numbers are updated. - EFI_PROTOCOL_ERROR - Some kind of iSCSI protocol errror happened. - ---*/ { ISCSI_NOP_IN *NopInHdr; EFI_STATUS Status; NopInHdr = (ISCSI_NOP_IN *) NetbufGetByte (Pdu, 0, NULL); + if (NopInHdr == NULL) { + return EFI_PROTOCOL_ERROR; + } NopInHdr->StatSN = NTOHL (NopInHdr->StatSN); NopInHdr->ExpCmdSN = NTOHL (NopInHdr->ExpCmdSN); @@ -2729,6 +2493,22 @@ Returns: return EFI_SUCCESS; } +/** + Execute the SCSI command issued through the EXT SCSI PASS THRU protocol. + + @param[in] PassThru The EXT SCSI PASS THRU protocol. + @param[in] Target The target ID. + @param[in] Lun The LUN. + @param[in, out] Packet The request packet containing IO request, SCSI command + buffer and buffers to read/write. + + @retval EFI_SUCCES The SCSI command is executed and the result is updated to + the Packet. + @retval EFI_DEVICE_ERROR Session state was not as required. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_NOT_READY The target can not accept new commands. + @retval Others Other errors as indicated. +**/ EFI_STATUS IScsiExecuteScsiCommand ( IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *PassThru, @@ -2736,27 +2516,6 @@ IScsiExecuteScsiCommand ( IN UINT64 Lun, IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet ) -/*++ - -Routine Description: - - Execute the SCSI command issued through the EXT SCSI PASS THRU protocol. - -Arguments: - - PassThru - The EXT SCSI PASS THRU protocol. - Target - The target ID. - Lun - The LUN. - Packet - The request packet containing IO request, SCSI command buffer and - buffers to read/write. - -Returns: - - EFI_SUCCES - The SCSI command is executed and the result is updated to - the Packet. - EFI_DEVICE_ERROR - Some unexpected error happened. - ---*/ { EFI_STATUS Status; ISCSI_DRIVER_DATA *Private; @@ -2769,16 +2528,18 @@ Returns: UINT8 *Data; ISCSI_IN_BUFFER_CONTEXT InBufferContext; UINT64 Timeout; - UINT8 *Buffer; + UINT8 *PduHdr; Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru); Session = &Private->Session; Status = EFI_SUCCESS; Tcb = NULL; TimeoutEvent = NULL; + Timeout = 0; if (Session->State != SESSION_STATE_LOGGED_IN) { - return EFI_DEVICE_ERROR; + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; } Conn = NET_LIST_USER_STRUCT_S ( @@ -2790,15 +2551,6 @@ Returns: if (Packet->Timeout != 0) { Timeout = MultU64x32 (Packet->Timeout, 2); - // - // Start the timeout timer. - // - Status = gBS->SetTimer (Conn->TimeoutEvent, TimerRelative, Timeout); - if (EFI_ERROR (Status)) { - goto ON_EXIT; - } - - TimeoutEvent = Conn->TimeoutEvent; } Status = IScsiNewTcb (Conn, &Tcb); @@ -2815,8 +2567,13 @@ Returns: } XferContext = &Tcb->XferContext; - Buffer = NetbufGetByte (Pdu, 0, NULL); - XferContext->Offset = ISCSI_GET_DATASEG_LEN (Buffer); + PduHdr = NetbufGetByte (Pdu, 0, NULL); + if (PduHdr == NULL) { + Status = EFI_PROTOCOL_ERROR; + NetbufFree (Pdu); + goto ON_EXIT; + } + XferContext->Offset = ISCSI_GET_DATASEG_LEN (PduHdr); // // Transmit the SCSI Command PDU. @@ -2854,6 +2611,16 @@ Returns: InBufferContext.InDataLen = Packet->InTransferLength; while (!Tcb->StatusXferd) { + // + // Start the timeout timer. + // + if (Timeout != 0) { + Status = gBS->SetTimer (Conn->TimeoutEvent, TimerRelative, Timeout); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + TimeoutEvent = Conn->TimeoutEvent; + } // // try to receive PDU from target. // @@ -2862,7 +2629,13 @@ Returns: goto ON_EXIT; } - switch (ISCSI_GET_OPCODE (NetbufGetByte (Pdu, 0, NULL))) { + PduHdr = NetbufGetByte (Pdu, 0, NULL); + if (PduHdr == NULL) { + Status = EFI_PROTOCOL_ERROR; + NetbufFree (Pdu); + goto ON_EXIT; + } + switch (ISCSI_GET_OPCODE (PduHdr)) { case ISCSI_OPCODE_SCSI_DATA_IN: Status = IScsiOnDataInRcvd (Pdu, Tcb, Packet); break; @@ -2909,44 +2682,27 @@ ON_EXIT: IScsiDelTcb (Tcb); } - if ((Status != EFI_SUCCESS) && (Status != EFI_NOT_READY)) { - // - // Reinstate the session. - // - if (EFI_ERROR (IScsiSessionReinstatement (Private))) { - Status = EFI_DEVICE_ERROR; - } - } - return Status; } -EFI_STATUS -IScsiSessionReinstatement ( - IN ISCSI_DRIVER_DATA *Private - ) -/*++ - -Routine Description: - +/** Reinstate the session on some error. -Arguments: + @param[in, out] Private The iSCSI driver data. - Private - The iSCSI driver data. - -Returns: - - EFI_SUCCES - The session is reinstated from some error. - other - Reinstatement failed. - ---*/ + @retval EFI_SUCCES The session is reinstated from some error. + @retval Other Reinstatement failed. +**/ +EFI_STATUS +IScsiSessionReinstatement ( + IN OUT ISCSI_DRIVER_DATA *Private + ) { ISCSI_SESSION *Session; EFI_STATUS Status; Session = &Private->Session; - ASSERT (Session->State == SESSION_STATE_LOGGED_IN); + ASSERT (Session->State != SESSION_STATE_FREE); // // Abort the session and re-init it. @@ -2962,52 +2718,31 @@ Returns: return Status; } +/** + Initialize some session parameters before login. + + @param[in, out] Session The iSCSI session. + @param[in] Recovery Whether the request is from a fresh new start or recovery. +**/ VOID IScsiSessionInit ( - IN ISCSI_SESSION *Session, - IN BOOLEAN Recovery + IN OUT ISCSI_SESSION *Session, + IN BOOLEAN Recovery ) -/*++ - -Routine Description: - - Initialize some session parameters before login. - -Arguments: - - Session - The iSCSI session. - Recovery - Whether the request is from a fresh new start or recovery. - -Returns: - - None. - ---*/ { - UINT32 Random; - if (!Recovery) { Session->Signature = ISCSI_SESSION_SIGNATURE; Session->State = SESSION_STATE_FREE; - Random = NET_RANDOM (NetRandomInitSeed ()); - - Session->ISID[0] = ISID_BYTE_0; - Session->ISID[1] = ISID_BYTE_1; - Session->ISID[2] = ISID_BYTE_2; - Session->ISID[3] = ISID_BYTE_3; - Session->ISID[4] = (UINT8) Random; - Session->ISID[5] = (UINT8) (Random >> 8); - InitializeListHead (&Session->Conns); InitializeListHead (&Session->TcbList); } - Session->TSIH = 0; + Session->Tsih = 0; Session->CmdSN = 1; Session->InitiatorTaskTag = 1; - Session->NextCID = 1; + Session->NextCid = 1; Session->TargetPortalGroupTag = 0; Session->MaxConnections = ISCSI_MAX_CONNS_PER_SESSION; @@ -3023,26 +2758,18 @@ Returns: Session->ErrorRecoveryLevel = 0; } -EFI_STATUS -IScsiSessionAbort ( - IN ISCSI_SESSION *Session - ) -/*++ - -Routine Description: - +/** Abort the iSCSI session, that is, reset all the connection and free the resources. -Arguments: - - Session - The iSCSI session. - -Returns: + @param[in, out] Session The iSCSI session. - EFI_SUCCES - The session is aborted. - ---*/ + @retval EFI_SUCCES The session is aborted. +**/ +EFI_STATUS +IScsiSessionAbort ( + IN OUT ISCSI_SESSION *Session + ) { ISCSI_DRIVER_DATA *Private; ISCSI_CONNECTION *Conn;