diff options
Diffstat (limited to 'src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe')
13 files changed, 7232 insertions, 0 deletions
diff --git a/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/ComponentName.c b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/ComponentName.c new file mode 100644 index 00000000..031a2b5a --- /dev/null +++ b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/ComponentName.c @@ -0,0 +1,341 @@ +/** @file + UEFI Component Name(2) protocol implementation for MnpDxe driver. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR> +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "MnpImpl.h" + +// +// EFI Component Name Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gMnpComponentName = { + MnpComponentNameGetDriverName, + MnpComponentNameGetControllerName, + "eng" +}; + +// +// EFI Component Name 2 Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gMnpComponentName2 = { + (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) MnpComponentNameGetDriverName, + (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) MnpComponentNameGetControllerName, + "en" +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mMnpDriverNameTable[] = { + { + "eng;en", + L"MNP Network Service Driver" + }, + { + NULL, + NULL + } +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE *gMnpControllerNameTable = NULL; + +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +MnpComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mMnpDriverNameTable, + DriverName, + (BOOLEAN) (This == &gMnpComponentName) + ); +} + +/** + Update the component name for the MNP child handle. + + @param Mnp[in] A pointer to the EFI_MANAGED_NETWORK_PROTOCOL. + + + @retval EFI_SUCCESS Update the ControllerNameTable of this instance successfully. + @retval EFI_INVALID_PARAMETER The input parameter is invalid. + +**/ +EFI_STATUS +UpdateName ( + IN EFI_MANAGED_NETWORK_PROTOCOL *Mnp + ) +{ + EFI_STATUS Status; + MNP_INSTANCE_DATA *Instance; + CHAR16 HandleName[80]; + EFI_MANAGED_NETWORK_CONFIG_DATA MnpConfigData; + EFI_SIMPLE_NETWORK_MODE SnpModeData; + UINTN OffSet; + UINTN Index; + + if (Mnp == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = MNP_INSTANCE_DATA_FROM_THIS (Mnp); + // + // Format the child name into the string buffer as: + // MNP (MAC=FF-FF-FF-FF-FF-FF, ProtocolType=0x0800, VlanId=0) + // + Status = Mnp->GetModeData (Mnp, &MnpConfigData, &SnpModeData); + if (!EFI_ERROR (Status)) { + OffSet = 0; + // + // Print the MAC address. + // + OffSet += UnicodeSPrint ( + HandleName, + sizeof (HandleName), + L"MNP (MAC=" + ); + for (Index = 0; Index < SnpModeData.HwAddressSize; Index++) { + OffSet += UnicodeSPrint ( + HandleName + OffSet, + sizeof (HandleName) - OffSet * sizeof (CHAR16), + L"%02X-", + SnpModeData.CurrentAddress.Addr[Index] + ); + } + ASSERT (OffSet > 0); + // + // Remove the last '-' + // + OffSet--; + // + // Print the ProtocolType and VLAN ID for this instance. + // + OffSet += UnicodeSPrint ( + HandleName + OffSet, + sizeof (HandleName) - OffSet * sizeof (CHAR16), + L", ProtocolType=0x%X, VlanId=%d)", + MnpConfigData.ProtocolTypeFilter, + Instance->MnpServiceData->VlanId + ); + } else if (Status == EFI_NOT_STARTED) { + UnicodeSPrint ( + HandleName, + sizeof (HandleName), + L"MNP (Not started)" + ); + } else { + return Status; + } + + if (gMnpControllerNameTable != NULL) { + FreeUnicodeStringTable (gMnpControllerNameTable); + gMnpControllerNameTable = NULL; + } + + Status = AddUnicodeString2 ( + "eng", + gMnpComponentName.SupportedLanguages, + &gMnpControllerNameTable, + HandleName, + TRUE + ); + if (EFI_ERROR (Status)) { + return Status; + } + + return AddUnicodeString2 ( + "en", + gMnpComponentName2.SupportedLanguages, + &gMnpControllerNameTable, + HandleName, + FALSE + ); +} + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + Currently not implemented. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name + specified by This, ControllerHandle, ChildHandle, + and Language was returned in ControllerName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +MnpComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + EFI_MANAGED_NETWORK_PROTOCOL *Mnp; + + // + // Only provide names for MNP child handles. + // + if (ChildHandle == NULL) { + return EFI_UNSUPPORTED; + } + + // + // Make sure this driver is currently managing ControllerHandle + // + Status = EfiTestManagedDevice ( + ControllerHandle, + gMnpDriverBinding.DriverBindingHandle, + &gEfiSimpleNetworkProtocolGuid + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Make sure this driver produced ChildHandle + // + Status = EfiTestChildHandle ( + ControllerHandle, + ChildHandle, + &gEfiManagedNetworkServiceBindingProtocolGuid + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Retrieve an instance of a produced protocol from ChildHandle + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiManagedNetworkProtocolGuid, + (VOID **)&Mnp, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Update the component name for this child handle. + // + Status = UpdateName (Mnp); + if (EFI_ERROR (Status)) { + return Status; + } + + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + gMnpControllerNameTable, + ControllerName, + (BOOLEAN)(This == &gMnpComponentName) + ); +} diff --git a/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/ComponentName.h b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/ComponentName.h new file mode 100644 index 00000000..45357b13 --- /dev/null +++ b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/ComponentName.h @@ -0,0 +1,144 @@ +/** @file + The header file of UEFI Component Name(2) protocol. + +Copyright (c) 2004 - 2012, Intel Corporation. All rights reserved.<BR> +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _COMPONENT_NAME_H_ +#define _COMPONENT_NAME_H_ + +#include <Protocol/ComponentName.h> +#include <Protocol/ComponentName2.h> + +extern EFI_COMPONENT_NAME2_PROTOCOL gMnpComponentName2; +extern EFI_COMPONENT_NAME_PROTOCOL gMnpComponentName; +extern EFI_UNICODE_STRING_TABLE *gMnpControllerNameTable; + +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +MnpComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + Currently not implemented. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name + specified by This, ControllerHandle, ChildHandle, + and Language was returned in ControllerName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +MnpComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + +#endif diff --git a/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpConfig.c b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpConfig.c new file mode 100644 index 00000000..4b1c0fde --- /dev/null +++ b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpConfig.c @@ -0,0 +1,1939 @@ +/** @file + Implementation of Managed Network Protocol private services. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR> +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "MnpImpl.h" +#include "MnpVlan.h" + +EFI_SERVICE_BINDING_PROTOCOL mMnpServiceBindingProtocol = { + MnpServiceBindingCreateChild, + MnpServiceBindingDestroyChild +}; + +EFI_MANAGED_NETWORK_PROTOCOL mMnpProtocolTemplate = { + MnpGetModeData, + MnpConfigure, + MnpMcastIpToMac, + MnpGroups, + MnpTransmit, + MnpReceive, + MnpCancel, + MnpPoll +}; + +EFI_MANAGED_NETWORK_CONFIG_DATA mMnpDefaultConfigData = { + 10000000, + 10000000, + 0, + FALSE, + FALSE, + FALSE, + FALSE, + FALSE, + FALSE, + FALSE +}; + +/** + Add Count of net buffers to MnpDeviceData->FreeNbufQue. The length of the net + buffer is specified by MnpDeviceData->BufferLength. + + @param[in, out] MnpDeviceData Pointer to the MNP_DEVICE_DATA. + @param[in] Count Number of NET_BUFFERs to add. + + @retval EFI_SUCCESS The specified amount of NET_BUFs are allocated + and added to MnpDeviceData->FreeNbufQue. + @retval EFI_OUT_OF_RESOURCES Failed to allocate a NET_BUF structure. + +**/ +EFI_STATUS +MnpAddFreeNbuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN UINTN Count + ) +{ + EFI_STATUS Status; + UINTN Index; + NET_BUF *Nbuf; + + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + ASSERT ((Count > 0) && (MnpDeviceData->BufferLength > 0)); + + Status = EFI_SUCCESS; + for (Index = 0; Index < Count; Index++) { + Nbuf = NetbufAlloc (MnpDeviceData->BufferLength + MnpDeviceData->PaddingSize); + if (Nbuf == NULL) { + DEBUG ((EFI_D_ERROR, "MnpAddFreeNbuf: NetBufAlloc failed.\n")); + + Status = EFI_OUT_OF_RESOURCES; + break; + } + + if (MnpDeviceData->PaddingSize > 0) { + // + // Pad padding bytes before the media header + // + NetbufAllocSpace (Nbuf, MnpDeviceData->PaddingSize, NET_BUF_TAIL); + NetbufTrim (Nbuf, MnpDeviceData->PaddingSize, NET_BUF_HEAD); + } + + NetbufQueAppend (&MnpDeviceData->FreeNbufQue, Nbuf); + } + + MnpDeviceData->NbufCnt += Index; + return Status; +} + + +/** + Allocate a free NET_BUF from MnpDeviceData->FreeNbufQue. If there is none + in the queue, first try to allocate some and add them into the queue, then + fetch the NET_BUF from the updated FreeNbufQue. + + @param[in, out] MnpDeviceData Pointer to the MNP_DEVICE_DATA. + + @return Pointer to the allocated free NET_BUF structure, if NULL the + operation is failed. + +**/ +NET_BUF * +MnpAllocNbuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData + ) +{ + EFI_STATUS Status; + NET_BUF_QUEUE *FreeNbufQue; + NET_BUF *Nbuf; + EFI_TPL OldTpl; + + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + FreeNbufQue = &MnpDeviceData->FreeNbufQue; + OldTpl = gBS->RaiseTPL (TPL_NOTIFY); + + // + // Check whether there are available buffers, or else try to add some. + // + if (FreeNbufQue->BufNum == 0) { + if ((MnpDeviceData->NbufCnt + MNP_NET_BUFFER_INCREASEMENT) > MNP_MAX_NET_BUFFER_NUM) { + DEBUG ( + (EFI_D_ERROR, + "MnpAllocNbuf: The maximum NET_BUF size is reached for MNP driver instance %p.\n", + MnpDeviceData) + ); + + Nbuf = NULL; + goto ON_EXIT; + } + + Status = MnpAddFreeNbuf (MnpDeviceData, MNP_NET_BUFFER_INCREASEMENT); + if (EFI_ERROR (Status)) { + DEBUG ( + (EFI_D_ERROR, + "MnpAllocNbuf: Failed to add NET_BUFs into the FreeNbufQue, %r.\n", + Status) + ); + + // + // Don't return NULL, perhaps MnpAddFreeNbuf does add some NET_BUFs but + // the amount is less than MNP_NET_BUFFER_INCREASEMENT. + // + } + } + + Nbuf = NetbufQueRemove (FreeNbufQue); + + // + // Increase the RefCnt. + // + if (Nbuf != NULL) { + NET_GET_REF (Nbuf); + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Nbuf; +} + + +/** + Try to reclaim the Nbuf into the buffer pool. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + @param[in, out] Nbuf Pointer to the NET_BUF to free. + +**/ +VOID +MnpFreeNbuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN OUT NET_BUF *Nbuf + ) +{ + EFI_TPL OldTpl; + + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + ASSERT (Nbuf->RefCnt > 1); + + OldTpl = gBS->RaiseTPL (TPL_NOTIFY); + + NET_PUT_REF (Nbuf); + + if (Nbuf->RefCnt == 1) { + // + // Trim all buffer contained in the Nbuf, then append it to the NbufQue. + // + NetbufTrim (Nbuf, Nbuf->TotalSize, NET_BUF_TAIL); + + if (NetbufAllocSpace (Nbuf, NET_VLAN_TAG_LEN, NET_BUF_HEAD) != NULL) { + // + // There is space reserved for vlan tag in the head, reclaim it + // + NetbufTrim (Nbuf, NET_VLAN_TAG_LEN, NET_BUF_TAIL); + } + + NetbufQueAppend (&MnpDeviceData->FreeNbufQue, Nbuf); + } + + gBS->RestoreTPL (OldTpl); +} + +/** + Add Count of TX buffers to MnpDeviceData->AllTxBufList and MnpDeviceData->FreeTxBufList. + The length of the buffer is specified by MnpDeviceData->BufferLength. + + @param[in, out] MnpDeviceData Pointer to the MNP_DEVICE_DATA. + @param[in] Count Number of TX buffers to add. + + @retval EFI_SUCCESS The specified amount of TX buffers are allocated. + @retval EFI_OUT_OF_RESOURCES Failed to allocate a TX buffer. + +**/ +EFI_STATUS +MnpAddFreeTxBuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN UINTN Count + ) +{ + EFI_STATUS Status; + UINT32 Index; + MNP_TX_BUF_WRAP *TxBufWrap; + + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + ASSERT ((Count > 0) && (MnpDeviceData->BufferLength > 0)); + + Status = EFI_SUCCESS; + for (Index = 0; Index < Count; Index++) { + TxBufWrap = (MNP_TX_BUF_WRAP*) AllocatePool (OFFSET_OF (MNP_TX_BUF_WRAP, TxBuf) + MnpDeviceData->BufferLength ); + if (TxBufWrap == NULL) { + DEBUG ((EFI_D_ERROR, "MnpAddFreeTxBuf: TxBuf Alloc failed.\n")); + + Status = EFI_OUT_OF_RESOURCES; + break; + } + DEBUG ((EFI_D_INFO, "MnpAddFreeTxBuf: Add TxBufWrap %p, TxBuf %p\n", TxBufWrap, TxBufWrap->TxBuf)); + TxBufWrap->Signature = MNP_TX_BUF_WRAP_SIGNATURE; + TxBufWrap->InUse = FALSE; + InsertTailList (&MnpDeviceData->FreeTxBufList, &TxBufWrap->WrapEntry); + InsertTailList (&MnpDeviceData->AllTxBufList, &TxBufWrap->AllEntry); + } + + MnpDeviceData->TxBufCount += Index; + return Status; +} + +/** + Allocate a free TX buffer from MnpDeviceData->FreeTxBufList. If there is none + in the queue, first try to recycle some from SNP, then try to allocate some and add + them into the queue, then fetch the NET_BUF from the updated FreeTxBufList. + + @param[in, out] MnpDeviceData Pointer to the MNP_DEVICE_DATA. + + @return Pointer to the allocated free NET_BUF structure, if NULL the + operation is failed. + +**/ +UINT8 * +MnpAllocTxBuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData + ) +{ + EFI_TPL OldTpl; + UINT8 *TxBuf; + EFI_STATUS Status; + LIST_ENTRY *Entry; + MNP_TX_BUF_WRAP *TxBufWrap; + + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (IsListEmpty (&MnpDeviceData->FreeTxBufList)) { + // + // First try to recycle some TX buffer from SNP + // + Status = MnpRecycleTxBuf (MnpDeviceData); + if (EFI_ERROR (Status)) { + TxBuf = NULL; + goto ON_EXIT; + } + + // + // If still no free TX buffer, allocate more. + // + if (IsListEmpty (&MnpDeviceData->FreeTxBufList)) { + if ((MnpDeviceData->TxBufCount + MNP_TX_BUFFER_INCREASEMENT) > MNP_MAX_TX_BUFFER_NUM) { + DEBUG ( + (EFI_D_ERROR, + "MnpAllocTxBuf: The maximum TxBuf size is reached for MNP driver instance %p.\n", + MnpDeviceData) + ); + + TxBuf = NULL; + goto ON_EXIT; + } + + Status = MnpAddFreeTxBuf (MnpDeviceData, MNP_TX_BUFFER_INCREASEMENT); + if (IsListEmpty (&MnpDeviceData->FreeTxBufList)) { + DEBUG ( + (EFI_D_ERROR, + "MnpAllocNbuf: Failed to add TxBuf into the FreeTxBufList, %r.\n", + Status) + ); + + TxBuf = NULL; + goto ON_EXIT; + } + } + } + + ASSERT (!IsListEmpty (&MnpDeviceData->FreeTxBufList)); + Entry = MnpDeviceData->FreeTxBufList.ForwardLink; + RemoveEntryList (MnpDeviceData->FreeTxBufList.ForwardLink); + TxBufWrap = NET_LIST_USER_STRUCT_S (Entry, MNP_TX_BUF_WRAP, WrapEntry, MNP_TX_BUF_WRAP_SIGNATURE); + TxBufWrap->InUse = TRUE; + TxBuf = TxBufWrap->TxBuf; + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return TxBuf; +} + +/** + Try to reclaim the TX buffer into the buffer pool. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + @param[in, out] TxBuf Pointer to the TX buffer to free. + +**/ +VOID +MnpFreeTxBuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN OUT UINT8 *TxBuf + ) +{ + MNP_TX_BUF_WRAP *TxBufWrap; + EFI_TPL OldTpl; + + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + if (TxBuf == NULL) { + return; + } + + TxBufWrap = NET_LIST_USER_STRUCT (TxBuf, MNP_TX_BUF_WRAP, TxBuf); + if (TxBufWrap->Signature != MNP_TX_BUF_WRAP_SIGNATURE) { + DEBUG ( + (EFI_D_ERROR, + "MnpFreeTxBuf: Signature check failed in MnpFreeTxBuf.\n") + ); + return; + } + + if (!TxBufWrap->InUse) { + DEBUG ( + (EFI_D_WARN, + "MnpFreeTxBuf: Duplicated recycle report from SNP.\n") + ); + return; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + InsertTailList (&MnpDeviceData->FreeTxBufList, &TxBufWrap->WrapEntry); + TxBufWrap->InUse = FALSE; + gBS->RestoreTPL (OldTpl); +} + +/** + Try to recycle all the transmitted buffer address from SNP. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + + @retval EFI_SUCCESS Successed to recyclethe transmitted buffer address. + @retval Others Failed to recyclethe transmitted buffer address. + +**/ +EFI_STATUS +MnpRecycleTxBuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData + ) +{ + UINT8 *TxBuf; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + EFI_STATUS Status; + + Snp = MnpDeviceData->Snp; + ASSERT (Snp != NULL); + + do { + TxBuf = NULL; + Status = Snp->GetStatus (Snp, NULL, (VOID **) &TxBuf); + if (EFI_ERROR (Status)) { + return Status; + } + + if (TxBuf != NULL) { + MnpFreeTxBuf (MnpDeviceData, TxBuf); + } + } while (TxBuf != NULL); + + return EFI_SUCCESS; +} + +/** + Initialize the mnp device context data. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + @param[in] ImageHandle The driver image handle. + @param[in] ControllerHandle Handle of device to bind driver to. + + @retval EFI_SUCCESS The mnp service context is initialized. + @retval EFI_UNSUPPORTED ControllerHandle does not support Simple Network Protocol. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpInitializeDeviceData ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN EFI_HANDLE ImageHandle, + IN EFI_HANDLE ControllerHandle + ) +{ + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + EFI_SIMPLE_NETWORK_MODE *SnpMode; + + MnpDeviceData->Signature = MNP_DEVICE_DATA_SIGNATURE; + MnpDeviceData->ImageHandle = ImageHandle; + MnpDeviceData->ControllerHandle = ControllerHandle; + + // + // Copy the MNP Protocol interfaces from the template. + // + CopyMem (&MnpDeviceData->VlanConfig, &mVlanConfigProtocolTemplate, sizeof (EFI_VLAN_CONFIG_PROTOCOL)); + + // + // Open the Simple Network protocol. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiSimpleNetworkProtocolGuid, + (VOID **) &Snp, + ImageHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + // + // Get MTU from Snp. + // + SnpMode = Snp->Mode; + MnpDeviceData->Snp = Snp; + + // + // Initialize the lists. + // + InitializeListHead (&MnpDeviceData->ServiceList); + InitializeListHead (&MnpDeviceData->GroupAddressList); + + // + // Get the buffer length used to allocate NET_BUF to hold data received + // from SNP. Do this before fill the FreeNetBufQue. + // + // + MnpDeviceData->BufferLength = SnpMode->MediaHeaderSize + NET_VLAN_TAG_LEN + SnpMode->MaxPacketSize + NET_ETHER_FCS_SIZE; + + // + // Make sure the protocol headers immediately following the media header + // 4-byte aligned, and also preserve additional space for VLAN tag + // + MnpDeviceData->PaddingSize = ((4 - SnpMode->MediaHeaderSize) & 0x3) + NET_VLAN_TAG_LEN; + + // + // Initialize MAC string which will be used as VLAN configuration variable name + // + Status = NetLibGetMacString (ControllerHandle, ImageHandle, &MnpDeviceData->MacString); + if (EFI_ERROR (Status)) { + goto ERROR; + } + + // + // Initialize the FreeNetBufQue and pre-allocate some NET_BUFs. + // + NetbufQueInit (&MnpDeviceData->FreeNbufQue); + Status = MnpAddFreeNbuf (MnpDeviceData, MNP_INIT_NET_BUFFER_NUM); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MnpInitializeDeviceData: MnpAddFreeNbuf failed, %r.\n", Status)); + + goto ERROR; + } + + // + // Get one NET_BUF from the FreeNbufQue for rx cache. + // + MnpDeviceData->RxNbufCache = MnpAllocNbuf (MnpDeviceData); + NetbufAllocSpace ( + MnpDeviceData->RxNbufCache, + MnpDeviceData->BufferLength, + NET_BUF_TAIL + ); + + // + // Allocate buffer pool for tx. + // + InitializeListHead (&MnpDeviceData->FreeTxBufList); + InitializeListHead (&MnpDeviceData->AllTxBufList); + MnpDeviceData->TxBufCount = 0; + + // + // Create the system poll timer. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL | EVT_TIMER, + TPL_CALLBACK, + MnpSystemPoll, + MnpDeviceData, + &MnpDeviceData->PollTimer + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MnpInitializeDeviceData: CreateEvent for poll timer failed.\n")); + + goto ERROR; + } + + // + // Create the timer for packet timeout check. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL | EVT_TIMER, + TPL_CALLBACK, + MnpCheckPacketTimeout, + MnpDeviceData, + &MnpDeviceData->TimeoutCheckTimer + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MnpInitializeDeviceData: CreateEvent for packet timeout check failed.\n")); + + goto ERROR; + } + + // + // Create the timer for media detection. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL | EVT_TIMER, + TPL_CALLBACK, + MnpCheckMediaStatus, + MnpDeviceData, + &MnpDeviceData->MediaDetectTimer + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MnpInitializeDeviceData: CreateEvent for media detection failed.\n")); + + goto ERROR; + } + +ERROR: + if (EFI_ERROR (Status)) { + // + // Free the dynamic allocated resources if necessary. + // + if (MnpDeviceData->MacString != NULL) { + FreePool (MnpDeviceData->MacString); + } + + if (MnpDeviceData->TimeoutCheckTimer != NULL) { + gBS->CloseEvent (MnpDeviceData->TimeoutCheckTimer); + } + + if (MnpDeviceData->MediaDetectTimer != NULL) { + gBS->CloseEvent (MnpDeviceData->MediaDetectTimer); + } + + if (MnpDeviceData->PollTimer != NULL) { + gBS->CloseEvent (MnpDeviceData->PollTimer); + } + + if (MnpDeviceData->RxNbufCache != NULL) { + MnpFreeNbuf (MnpDeviceData, MnpDeviceData->RxNbufCache); + } + + if (MnpDeviceData->FreeNbufQue.BufNum != 0) { + NetbufQueFlush (&MnpDeviceData->FreeNbufQue); + } + + // + // Close the Simple Network Protocol. + // + gBS->CloseProtocol ( + ControllerHandle, + &gEfiSimpleNetworkProtocolGuid, + ImageHandle, + ControllerHandle + ); + } + + return Status; +} + + +/** + Destroy the MNP device context data. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + @param[in] ImageHandle The driver image handle. + +**/ +VOID +MnpDestroyDeviceData ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN EFI_HANDLE ImageHandle + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *NextEntry; + MNP_TX_BUF_WRAP *TxBufWrap; + + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + // + // Free Vlan Config variable name string + // + if (MnpDeviceData->MacString != NULL) { + FreePool (MnpDeviceData->MacString); + } + + // + // The GroupAddressList must be empty. + // + ASSERT (IsListEmpty (&MnpDeviceData->GroupAddressList)); + + // + // Close the event. + // + gBS->CloseEvent (MnpDeviceData->TimeoutCheckTimer); + gBS->CloseEvent (MnpDeviceData->MediaDetectTimer); + gBS->CloseEvent (MnpDeviceData->PollTimer); + + // + // Free the Tx buffer pool. + // + NET_LIST_FOR_EACH_SAFE(Entry, NextEntry, &MnpDeviceData->AllTxBufList) { + TxBufWrap = NET_LIST_USER_STRUCT (Entry, MNP_TX_BUF_WRAP, AllEntry); + RemoveEntryList (Entry); + FreePool (TxBufWrap); + MnpDeviceData->TxBufCount--; + } + ASSERT (IsListEmpty (&MnpDeviceData->AllTxBufList)); + ASSERT (MnpDeviceData->TxBufCount == 0); + + // + // Free the RxNbufCache. + // + MnpFreeNbuf (MnpDeviceData, MnpDeviceData->RxNbufCache); + + // + // Flush the FreeNbufQue. + // + MnpDeviceData->NbufCnt -= MnpDeviceData->FreeNbufQue.BufNum; + NetbufQueFlush (&MnpDeviceData->FreeNbufQue); + + // + // Close the Simple Network Protocol. + // + gBS->CloseProtocol ( + MnpDeviceData->ControllerHandle, + &gEfiSimpleNetworkProtocolGuid, + ImageHandle, + MnpDeviceData->ControllerHandle + ); +} + + +/** + Create mnp service context data. + + @param[in] MnpDeviceData Pointer to the mnp device context data. + @param[in] VlanId The VLAN ID. + @param[in] Priority The VLAN priority. If VlanId is 0, + Priority is ignored. + + @return A pointer to MNP_SERVICE_DATA or NULL if failed to create MNP service context. + +**/ +MNP_SERVICE_DATA * +MnpCreateServiceData ( + IN MNP_DEVICE_DATA *MnpDeviceData, + IN UINT16 VlanId, + IN UINT8 Priority OPTIONAL + ) +{ + EFI_HANDLE MnpServiceHandle; + MNP_SERVICE_DATA *MnpServiceData; + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_MODE *SnpMode; + EFI_VLAN_CONFIG_PROTOCOL *VlanConfig; + + // + // Initialize the Mnp Service Data. + // + MnpServiceData = AllocateZeroPool (sizeof (MNP_SERVICE_DATA)); + if (MnpServiceData == NULL) { + DEBUG ((DEBUG_ERROR, "MnpCreateServiceData: Failed to allocate memory for the new Mnp Service Data.\n")); + + return NULL; + } + + // + // Add to MNP service list + // + InsertTailList (&MnpDeviceData->ServiceList, &MnpServiceData->Link); + + MnpServiceData->Signature = MNP_SERVICE_DATA_SIGNATURE; + MnpServiceData->MnpDeviceData = MnpDeviceData; + + // + // Copy the ServiceBinding structure. + // + CopyMem (&MnpServiceData->ServiceBinding, &mMnpServiceBindingProtocol, sizeof (EFI_SERVICE_BINDING_PROTOCOL)); + + // + // Initialize the lists. + // + InitializeListHead (&MnpServiceData->ChildrenList); + + SnpMode = MnpDeviceData->Snp->Mode; + if (VlanId != 0) { + // + // Create VLAN child handle + // + MnpServiceHandle = MnpCreateVlanChild ( + MnpDeviceData->ImageHandle, + MnpDeviceData->ControllerHandle, + VlanId, + &MnpServiceData->DevicePath + ); + if (MnpServiceHandle == NULL) { + DEBUG ((DEBUG_ERROR, "MnpCreateServiceData: Failed to create child handle.\n")); + + return NULL; + } + + // + // Open VLAN Config Protocol by child + // + Status = gBS->OpenProtocol ( + MnpDeviceData->ControllerHandle, + &gEfiVlanConfigProtocolGuid, + (VOID **) &VlanConfig, + MnpDeviceData->ImageHandle, + MnpServiceHandle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Reduce MTU for VLAN device + // + MnpServiceData->Mtu = SnpMode->MaxPacketSize - NET_VLAN_TAG_LEN; + } else { + // + // VlanId set to 0 means rx/tx untagged frame + // + MnpServiceHandle = MnpDeviceData->ControllerHandle; + MnpServiceData->Mtu = SnpMode->MaxPacketSize; + } + + MnpServiceData->ServiceHandle = MnpServiceHandle; + MnpServiceData->VlanId = VlanId; + MnpServiceData->Priority = Priority; + + // + // Install the MNP Service Binding Protocol + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &MnpServiceHandle, + &gEfiManagedNetworkServiceBindingProtocolGuid, + &MnpServiceData->ServiceBinding, + NULL + ); + +Exit: + if (EFI_ERROR (Status)) { + MnpDestroyServiceData (MnpServiceData); + MnpServiceData = NULL; + } + + return MnpServiceData; +} + +/** + Destroy the MNP service context data. + + @param[in, out] MnpServiceData Pointer to the mnp service context data. + + @retval EFI_SUCCESS The mnp service context is destroyed. + @retval Others Errors as indicated. + +**/ +EFI_STATUS +MnpDestroyServiceData ( + IN OUT MNP_SERVICE_DATA *MnpServiceData + ) +{ + EFI_STATUS Status; + + // + // Uninstall the MNP Service Binding Protocol + // + Status = gBS->UninstallMultipleProtocolInterfaces ( + MnpServiceData->ServiceHandle, + &gEfiManagedNetworkServiceBindingProtocolGuid, + &MnpServiceData->ServiceBinding, + NULL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (MnpServiceData->VlanId != 0) { + // + // Close VlanConfig Protocol opened by VLAN child handle + // + Status = gBS->CloseProtocol ( + MnpServiceData->MnpDeviceData->ControllerHandle, + &gEfiVlanConfigProtocolGuid, + MnpServiceData->MnpDeviceData->ImageHandle, + MnpServiceData->ServiceHandle + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Uninstall Device Path Protocol to destroy the VLAN child handle + // + Status = gBS->UninstallMultipleProtocolInterfaces ( + MnpServiceData->ServiceHandle, + &gEfiDevicePathProtocolGuid, + MnpServiceData->DevicePath, + NULL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (MnpServiceData->DevicePath != NULL) { + FreePool (MnpServiceData->DevicePath); + } + } + + // + // Remove from MnpDeviceData service list + // + RemoveEntryList (&MnpServiceData->Link); + + FreePool (MnpServiceData); + + return Status; +} + +/** + Callback function which provided by user to remove one node in NetDestroyLinkList process. + + @param[in] Entry The entry to be removed. + @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList. + + @retval EFI_SUCCESS The entry has been removed successfully. + @retval Others Fail to remove the entry. + +**/ +EFI_STATUS +EFIAPI +MnpDestoryChildEntry ( + IN LIST_ENTRY *Entry, + IN VOID *Context + ) +{ + MNP_INSTANCE_DATA *Instance; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + + ServiceBinding = (EFI_SERVICE_BINDING_PROTOCOL *) Context; + Instance = CR (Entry, MNP_INSTANCE_DATA, InstEntry, MNP_INSTANCE_DATA_SIGNATURE); + return ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle); +} + +/** + Destroy all child of the MNP service data. + + @param[in, out] MnpServiceData Pointer to the mnp service context data. + + @retval EFI_SUCCESS All child are destroyed. + @retval Others Failed to destroy all child. + +**/ +EFI_STATUS +MnpDestroyServiceChild ( + IN OUT MNP_SERVICE_DATA *MnpServiceData + ) +{ + LIST_ENTRY *List; + EFI_STATUS Status; + UINTN ListLength; + + List = &MnpServiceData->ChildrenList; + + Status = NetDestroyLinkList ( + List, + MnpDestoryChildEntry, + &MnpServiceData->ServiceBinding, + &ListLength + ); + if (EFI_ERROR (Status) || ListLength != 0) { + return EFI_DEVICE_ERROR; + } + + return EFI_SUCCESS; +} + +/** + Find the MNP Service Data for given VLAN ID. + + @param[in] MnpDeviceData Pointer to the mnp device context data. + @param[in] VlanId The VLAN ID. + + @return A pointer to MNP_SERVICE_DATA or NULL if not found. + +**/ +MNP_SERVICE_DATA * +MnpFindServiceData ( + IN MNP_DEVICE_DATA *MnpDeviceData, + IN UINT16 VlanId + ) +{ + LIST_ENTRY *Entry; + MNP_SERVICE_DATA *MnpServiceData; + + NET_LIST_FOR_EACH (Entry, &MnpDeviceData->ServiceList) { + // + // Check VLAN ID of each Mnp Service Data + // + MnpServiceData = MNP_SERVICE_DATA_FROM_LINK (Entry); + if (MnpServiceData->VlanId == VlanId) { + return MnpServiceData; + } + } + + return NULL; +} + +/** + Initialize the mnp instance context data. + + @param[in] MnpServiceData Pointer to the mnp service context data. + @param[in, out] Instance Pointer to the mnp instance context data + to initialize. + +**/ +VOID +MnpInitializeInstanceData ( + IN MNP_SERVICE_DATA *MnpServiceData, + IN OUT MNP_INSTANCE_DATA *Instance + ) +{ + NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE); + ASSERT (Instance != NULL); + + // + // Set the signature. + // + Instance->Signature = MNP_INSTANCE_DATA_SIGNATURE; + + // + // Copy the MNP Protocol interfaces from the template. + // + CopyMem (&Instance->ManagedNetwork, &mMnpProtocolTemplate, sizeof (Instance->ManagedNetwork)); + + // + // Copy the default config data. + // + CopyMem (&Instance->ConfigData, &mMnpDefaultConfigData, sizeof (Instance->ConfigData)); + + // + // Initialize the lists. + // + InitializeListHead (&Instance->GroupCtrlBlkList); + InitializeListHead (&Instance->RcvdPacketQueue); + InitializeListHead (&Instance->RxDeliveredPacketQueue); + + // + // Initialize the RxToken Map. + // + NetMapInit (&Instance->RxTokenMap); + + // + // Save the MnpServiceData info. + // + Instance->MnpServiceData = MnpServiceData; +} + + +/** + Check whether the token specified by Arg matches the token in Item. + + @param[in] Map Pointer to the NET_MAP. + @param[in] Item Pointer to the NET_MAP_ITEM. + @param[in] Arg Pointer to the Arg, it's a pointer to the token to + check. + + @retval EFI_SUCCESS The token specified by Arg is different from the + token in Item. + @retval EFI_ACCESS_DENIED The token specified by Arg is the same as that in + Item. + +**/ +EFI_STATUS +EFIAPI +MnpTokenExist ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Arg + ) +{ + EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token; + EFI_MANAGED_NETWORK_COMPLETION_TOKEN *TokenInItem; + + Token = (EFI_MANAGED_NETWORK_COMPLETION_TOKEN *) Arg; + TokenInItem = (EFI_MANAGED_NETWORK_COMPLETION_TOKEN *) Item->Key; + + if ((Token == TokenInItem) || (Token->Event == TokenInItem->Event)) { + // + // The token is the same either the two tokens equals or the Events in + // the two tokens are the same. + // + return EFI_ACCESS_DENIED; + } + + return EFI_SUCCESS; +} + +/** + Cancel the token specified by Arg if it matches the token in Item. + + @param[in, out] Map Pointer to the NET_MAP. + @param[in, out] Item Pointer to the NET_MAP_ITEM. + @param[in] Arg Pointer to the Arg, it's a pointer to the + token to cancel. + + @retval EFI_SUCCESS The Arg is NULL, and the token in Item is cancelled, + or the Arg isn't NULL, and the token in Item is + different from the Arg. + @retval EFI_ABORTED The Arg isn't NULL, the token in Item mathces the + Arg, and the token is cancelled. + +**/ +EFI_STATUS +EFIAPI +MnpCancelTokens ( + IN OUT NET_MAP *Map, + IN OUT NET_MAP_ITEM *Item, + IN VOID *Arg + ) +{ + EFI_MANAGED_NETWORK_COMPLETION_TOKEN *TokenToCancel; + + if ((Arg != NULL) && (Item->Key != Arg)) { + // + // The token in Item is not the token specified by Arg. + // + return EFI_SUCCESS; + } + + TokenToCancel = (EFI_MANAGED_NETWORK_COMPLETION_TOKEN *) Item->Key; + + // + // Remove the item from the map. + // + NetMapRemoveItem (Map, Item, NULL); + + // + // Cancel this token with status set to EFI_ABORTED. + // + TokenToCancel->Status = EFI_ABORTED; + gBS->SignalEvent (TokenToCancel->Event); + + if (Arg != NULL) { + // + // Only abort the token specified by Arg if Arg isn't NULL. + // + return EFI_ABORTED; + } + + return EFI_SUCCESS; +} + + +/** + Start and initialize the simple network. + + @param[in] Snp Pointer to the simple network protocol. + + @retval EFI_SUCCESS The simple network protocol is started. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpStartSnp ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp + ) +{ + EFI_STATUS Status; + + ASSERT (Snp != NULL); + + // + // Start the simple network. + // + Status = Snp->Start (Snp); + + if (!EFI_ERROR (Status)) { + // + // Initialize the simple network. + // + Status = Snp->Initialize (Snp, 0, 0); + } + + return Status; +} + + +/** + Stop the simple network. + + @param[in] MnpDeviceData Pointer to the MNP_DEVICE_DATA. + + @retval EFI_SUCCESS The simple network is stopped. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpStopSnp ( + IN MNP_DEVICE_DATA *MnpDeviceData + ) +{ + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + + Snp = MnpDeviceData->Snp; + ASSERT (Snp != NULL); + + // + // Recycle all the transmit buffer from SNP. + // + Status = MnpRecycleTxBuf (MnpDeviceData); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Shut down the simple network. + // + Status = Snp->Shutdown (Snp); + if (!EFI_ERROR (Status)) { + // + // Stop the simple network. + // + Status = Snp->Stop (Snp); + } + + return Status; +} + + +/** + Start the managed network, this function is called when one instance is configured + or reconfigured. + + @param[in, out] MnpServiceData Pointer to the mnp service context data. + @param[in] IsConfigUpdate The instance is reconfigured or it's the first + time the instanced is configured. + @param[in] EnableSystemPoll Enable the system polling or not. + + @retval EFI_SUCCESS The managed network is started and some + configuration is updated. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpStart ( + IN OUT MNP_SERVICE_DATA *MnpServiceData, + IN BOOLEAN IsConfigUpdate, + IN BOOLEAN EnableSystemPoll + ) +{ + EFI_STATUS Status; + EFI_TIMER_DELAY TimerOpType; + MNP_DEVICE_DATA *MnpDeviceData; + + NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE); + + Status = EFI_SUCCESS; + MnpDeviceData = MnpServiceData->MnpDeviceData; + + if (!IsConfigUpdate) { + // + // If it's not a configuration update, increase the configured children number. + // + MnpDeviceData->ConfiguredChildrenNumber++; + + if (MnpDeviceData->ConfiguredChildrenNumber == 1) { + // + // It's the first configured child, start the simple network. + // + Status = MnpStartSnp (MnpDeviceData->Snp); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MnpStart: MnpStartSnp failed, %r.\n", Status)); + + goto ErrorExit; + } + + // + // Start the timeout timer. + // + Status = gBS->SetTimer ( + MnpDeviceData->TimeoutCheckTimer, + TimerPeriodic, + MNP_TIMEOUT_CHECK_INTERVAL + ); + if (EFI_ERROR (Status)) { + DEBUG ( + (EFI_D_ERROR, + "MnpStart, gBS->SetTimer for TimeoutCheckTimer %r.\n", + Status) + ); + + goto ErrorExit; + } + + // + // Start the media detection timer. + // + Status = gBS->SetTimer ( + MnpDeviceData->MediaDetectTimer, + TimerPeriodic, + MNP_MEDIA_DETECT_INTERVAL + ); + if (EFI_ERROR (Status)) { + DEBUG ( + (EFI_D_ERROR, + "MnpStart, gBS->SetTimer for MediaDetectTimer %r.\n", + Status) + ); + + goto ErrorExit; + } + } + } + + if (MnpDeviceData->EnableSystemPoll ^ EnableSystemPoll) { + // + // The EnableSystemPoll differs with the current state, disable or enable + // the system poll. + // + TimerOpType = EnableSystemPoll ? TimerPeriodic : TimerCancel; + + Status = gBS->SetTimer (MnpDeviceData->PollTimer, TimerOpType, MNP_SYS_POLL_INTERVAL); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MnpStart: gBS->SetTimer for PollTimer failed, %r.\n", Status)); + + goto ErrorExit; + } + + MnpDeviceData->EnableSystemPoll = EnableSystemPoll; + } + + // + // Change the receive filters if need. + // + Status = MnpConfigReceiveFilters (MnpDeviceData); + +ErrorExit: + return Status; +} + + +/** + Stop the managed network. + + @param[in, out] MnpServiceData Pointer to the mnp service context data. + + @retval EFI_SUCCESS The managed network is stopped. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpStop ( + IN OUT MNP_SERVICE_DATA *MnpServiceData + ) +{ + EFI_STATUS Status; + MNP_DEVICE_DATA *MnpDeviceData; + + NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE); + MnpDeviceData = MnpServiceData->MnpDeviceData; + ASSERT (MnpDeviceData->ConfiguredChildrenNumber > 0); + + // + // Configure the receive filters. + // + MnpConfigReceiveFilters (MnpDeviceData); + + // + // Decrease the children number. + // + MnpDeviceData->ConfiguredChildrenNumber--; + + if (MnpDeviceData->ConfiguredChildrenNumber > 0) { + // + // If there are other configured children, return and keep the timers and + // simple network unchanged. + // + return EFI_SUCCESS; + } + + // + // No configured children now. + // + if (MnpDeviceData->EnableSystemPoll) { + // + // The system poll in on, cancel the poll timer. + // + Status = gBS->SetTimer (MnpDeviceData->PollTimer, TimerCancel, 0); + MnpDeviceData->EnableSystemPoll = FALSE; + } + + // + // Cancel the timeout timer. + // + Status = gBS->SetTimer (MnpDeviceData->TimeoutCheckTimer, TimerCancel, 0); + + // + // Cancel the media detect timer. + // + Status = gBS->SetTimer (MnpDeviceData->MediaDetectTimer, TimerCancel, 0); + + // + // Stop the simple network. + // + Status = MnpStopSnp (MnpDeviceData); + return Status; +} + + +/** + Flush the instance's received data. + + @param[in, out] Instance Pointer to the mnp instance context data. + +**/ +VOID +MnpFlushRcvdDataQueue ( + IN OUT MNP_INSTANCE_DATA *Instance + ) +{ + EFI_TPL OldTpl; + MNP_RXDATA_WRAP *RxDataWrap; + + NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE); + + OldTpl = gBS->RaiseTPL (TPL_NOTIFY); + + while (!IsListEmpty (&Instance->RcvdPacketQueue)) { + // + // Remove all the Wraps. + // + RxDataWrap = NET_LIST_HEAD (&Instance->RcvdPacketQueue, MNP_RXDATA_WRAP, WrapEntry); + + // + // Recycle the RxDataWrap. + // + MnpRecycleRxData (NULL, (VOID *) RxDataWrap); + Instance->RcvdPacketQueueSize--; + } + + ASSERT (Instance->RcvdPacketQueueSize == 0); + + gBS->RestoreTPL (OldTpl); +} + + +/** + Configure the Instance using ConfigData. + + @param[in, out] Instance Pointer to the mnp instance context data. + @param[in] ConfigData Pointer to the configuration data used to configure + the instance. + + @retval EFI_SUCCESS The Instance is configured. + @retval EFI_UNSUPPORTED EnableReceiveTimestamps is on and the + implementation doesn't support it. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpConfigureInstance ( + IN OUT MNP_INSTANCE_DATA *Instance, + IN EFI_MANAGED_NETWORK_CONFIG_DATA *ConfigData OPTIONAL + ) +{ + EFI_STATUS Status; + MNP_SERVICE_DATA *MnpServiceData; + MNP_DEVICE_DATA *MnpDeviceData; + EFI_MANAGED_NETWORK_CONFIG_DATA *OldConfigData; + EFI_MANAGED_NETWORK_CONFIG_DATA *NewConfigData; + BOOLEAN IsConfigUpdate; + + NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE); + + if ((ConfigData != NULL) && ConfigData->EnableReceiveTimestamps) { + // + // Don't support timestamp. + // + return EFI_UNSUPPORTED; + } + + Status = EFI_SUCCESS; + + MnpServiceData = Instance->MnpServiceData; + MnpDeviceData = MnpServiceData->MnpDeviceData; + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + IsConfigUpdate = (BOOLEAN) ((Instance->Configured) && (ConfigData != NULL)); + + OldConfigData = &Instance->ConfigData; + NewConfigData = ConfigData; + if (NewConfigData == NULL) { + // + // Restore back the default config data if a reset of this instance + // is required. + // + NewConfigData = &mMnpDefaultConfigData; + } + + // + // Reset the instance's receive filter. + // + Instance->ReceiveFilter = 0; + + // + // Clear the receive counters according to the old ConfigData. + // + if (OldConfigData->EnableUnicastReceive) { + MnpDeviceData->UnicastCount--; + } + + if (OldConfigData->EnableMulticastReceive) { + MnpDeviceData->MulticastCount--; + } + + if (OldConfigData->EnableBroadcastReceive) { + MnpDeviceData->BroadcastCount--; + } + + if (OldConfigData->EnablePromiscuousReceive) { + MnpDeviceData->PromiscuousCount--; + } + + // + // Set the receive filter counters and the receive filter of the + // instance according to the new ConfigData. + // + if (NewConfigData->EnableUnicastReceive) { + MnpDeviceData->UnicastCount++; + Instance->ReceiveFilter |= MNP_RECEIVE_UNICAST; + } + + if (NewConfigData->EnableMulticastReceive) { + MnpDeviceData->MulticastCount++; + } + + if (NewConfigData->EnableBroadcastReceive) { + MnpDeviceData->BroadcastCount++; + Instance->ReceiveFilter |= MNP_RECEIVE_BROADCAST; + } + + if (NewConfigData->EnablePromiscuousReceive) { + MnpDeviceData->PromiscuousCount++; + } + + if (OldConfigData->FlushQueuesOnReset) { + MnpFlushRcvdDataQueue (Instance); + } + + if (ConfigData == NULL) { + Instance->ManagedNetwork.Cancel (&Instance->ManagedNetwork, NULL); + } + + if (!NewConfigData->EnableMulticastReceive) { + MnpGroupOp (Instance, FALSE, NULL, NULL); + } + + // + // Save the new configuration data. + // + CopyMem (OldConfigData, NewConfigData, sizeof (*OldConfigData)); + + Instance->Configured = (BOOLEAN) (ConfigData != NULL); + if (Instance->Configured) { + // + // The instance is configured, start the Mnp. + // + Status = MnpStart ( + MnpServiceData, + IsConfigUpdate, + (BOOLEAN) !NewConfigData->DisableBackgroundPolling + ); + } else { + // + // The instance is changed to the unconfigured state, stop the Mnp. + // + Status = MnpStop (MnpServiceData); + } + + return Status; +} + +/** + Configure the Snp receive filters according to the instances' receive filter + settings. + + @param[in] MnpDeviceData Pointer to the mnp device context data. + + @retval EFI_SUCCESS The receive filters is configured. + @retval EFI_OUT_OF_RESOURCES The receive filters can't be configured due + to lack of memory resource. + +**/ +EFI_STATUS +MnpConfigReceiveFilters ( + IN MNP_DEVICE_DATA *MnpDeviceData + ) +{ + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + EFI_MAC_ADDRESS *MCastFilter; + UINT32 MCastFilterCnt; + UINT32 EnableFilterBits; + UINT32 DisableFilterBits; + BOOLEAN ResetMCastFilters; + LIST_ENTRY *Entry; + UINT32 Index; + MNP_GROUP_ADDRESS *GroupAddress; + + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + Snp = MnpDeviceData->Snp; + + // + // Initialize the enable filter and disable filter. + // + EnableFilterBits = 0; + DisableFilterBits = Snp->Mode->ReceiveFilterMask; + + if (MnpDeviceData->UnicastCount != 0) { + // + // Enable unicast if any instance wants to receive unicast. + // + EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_UNICAST; + } + + if (MnpDeviceData->BroadcastCount != 0) { + // + // Enable broadcast if any instance wants to receive broadcast. + // + EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST; + } + + MCastFilter = NULL; + MCastFilterCnt = 0; + ResetMCastFilters = TRUE; + + if ((MnpDeviceData->MulticastCount != 0) && (MnpDeviceData->GroupAddressCount != 0)) { + // + // There are instances configured to receive multicast and already some group + // addresses are joined. + // + + ResetMCastFilters = FALSE; + + if (MnpDeviceData->GroupAddressCount <= Snp->Mode->MaxMCastFilterCount) { + // + // The joind group address is less than simple network's maximum count. + // Just configure the snp to do the multicast filtering. + // + + EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST; + + // + // Allocate pool for the multicast addresses. + // + MCastFilterCnt = MnpDeviceData->GroupAddressCount; + MCastFilter = AllocatePool (sizeof (EFI_MAC_ADDRESS) * MCastFilterCnt); + if (MCastFilter == NULL) { + DEBUG ((EFI_D_ERROR, "MnpConfigReceiveFilters: Failed to allocate memory resource for MCastFilter.\n")); + + return EFI_OUT_OF_RESOURCES; + } + + // + // Fill the multicast HW address buffer. + // + Index = 0; + NET_LIST_FOR_EACH (Entry, &MnpDeviceData->GroupAddressList) { + + GroupAddress = NET_LIST_USER_STRUCT (Entry, MNP_GROUP_ADDRESS, AddrEntry); + CopyMem (MCastFilter + Index, &GroupAddress->Address, sizeof (*(MCastFilter + Index))); + Index++; + + ASSERT (Index <= MCastFilterCnt); + } + } else { + // + // The maximum multicast is reached, set the filter to be promiscuous + // multicast. + // + + if ((Snp->Mode->ReceiveFilterMask & EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST) != 0) { + EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST; + } else { + // + // Either MULTICAST or PROMISCUOUS_MULTICAST is not supported by Snp, + // set the NIC to be promiscuous although this will tremendously degrade + // the performance. + // + EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS; + } + } + } + + if (MnpDeviceData->PromiscuousCount != 0) { + // + // Enable promiscuous if any instance wants to receive promiscuous. + // + EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS; + } + + // + // Set the disable filter. + // + DisableFilterBits ^= EnableFilterBits; + + // + // Configure the receive filters of SNP. + // + Status = Snp->ReceiveFilters ( + Snp, + EnableFilterBits, + DisableFilterBits, + ResetMCastFilters, + MCastFilterCnt, + MCastFilter + ); + DEBUG_CODE ( + if (EFI_ERROR (Status)) { + DEBUG ( + (EFI_D_ERROR, + "MnpConfigReceiveFilters: Snp->ReceiveFilters failed, %r.\n", + Status) + ); + } + ); + + if (MCastFilter != NULL) { + // + // Free the buffer used to hold the group addresses. + // + FreePool (MCastFilter); + } + + return Status; +} + + +/** + Add a group address control block which controls the MacAddress for + this instance. + + @param[in, out] Instance Pointer to the mnp instance context data. + @param[in, out] CtrlBlk Pointer to the group address control block. + @param[in, out] GroupAddress Pointer to the group address. + @param[in] MacAddress Pointer to the mac address. + @param[in] HwAddressSize The hardware address size. + + @retval EFI_SUCCESS The group address control block is added. + @retval EFI_OUT_OF_RESOURCES Failed due to lack of memory resources. + +**/ +EFI_STATUS +MnpGroupOpAddCtrlBlk ( + IN OUT MNP_INSTANCE_DATA *Instance, + IN OUT MNP_GROUP_CONTROL_BLOCK *CtrlBlk, + IN OUT MNP_GROUP_ADDRESS *GroupAddress OPTIONAL, + IN EFI_MAC_ADDRESS *MacAddress, + IN UINT32 HwAddressSize + ) +{ + MNP_DEVICE_DATA *MnpDeviceData; + + NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE); + + MnpDeviceData = Instance->MnpServiceData->MnpDeviceData; + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + if (GroupAddress == NULL) { + ASSERT (MacAddress != NULL); + + // + // Allocate a new GroupAddress to be added into MNP's GroupAddressList. + // + GroupAddress = AllocatePool (sizeof (MNP_GROUP_ADDRESS)); + if (GroupAddress == NULL) { + + DEBUG ((EFI_D_ERROR, "MnpGroupOpFormCtrlBlk: Failed to allocate memory resource.\n")); + + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (&GroupAddress->Address, MacAddress, sizeof (GroupAddress->Address)); + GroupAddress->RefCnt = 0; + InsertTailList ( + &MnpDeviceData->GroupAddressList, + &GroupAddress->AddrEntry + ); + MnpDeviceData->GroupAddressCount++; + } + + // + // Increase the RefCnt. + // + GroupAddress->RefCnt++; + + // + // Add the CtrlBlk into the instance's GroupCtrlBlkList. + // + CtrlBlk->GroupAddress = GroupAddress; + InsertTailList (&Instance->GroupCtrlBlkList, &CtrlBlk->CtrlBlkEntry); + + return EFI_SUCCESS; +} + + +/** + Delete a group control block from the instance. If the controlled group address's + reference count reaches zero, the group address is removed too. + + @param[in] Instance Pointer to the instance context data. + @param[in] CtrlBlk Pointer to the group control block to delete. + + @return The group address controlled by the control block is no longer used or not. + +**/ +BOOLEAN +MnpGroupOpDelCtrlBlk ( + IN MNP_INSTANCE_DATA *Instance, + IN MNP_GROUP_CONTROL_BLOCK *CtrlBlk + ) +{ + MNP_DEVICE_DATA *MnpDeviceData; + MNP_GROUP_ADDRESS *GroupAddress; + + NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE); + + MnpDeviceData = Instance->MnpServiceData->MnpDeviceData; + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + // + // Remove and free the CtrlBlk. + // + GroupAddress = CtrlBlk->GroupAddress; + RemoveEntryList (&CtrlBlk->CtrlBlkEntry); + FreePool (CtrlBlk); + + ASSERT (GroupAddress->RefCnt > 0); + + // + // Count down the RefCnt. + // + GroupAddress->RefCnt--; + + if (GroupAddress->RefCnt == 0) { + // + // Free this GroupAddress entry if no instance uses it. + // + MnpDeviceData->GroupAddressCount--; + RemoveEntryList (&GroupAddress->AddrEntry); + FreePool (GroupAddress); + + return TRUE; + } + + return FALSE; +} + + +/** + Do the group operations for this instance. + + @param[in, out] Instance Pointer to the instance context data. + @param[in] JoinFlag Set to TRUE to join a group. Set to TRUE to + leave a group/groups. + @param[in] MacAddress Pointer to the group address to join or leave. + @param[in] CtrlBlk Pointer to the group control block if JoinFlag + is FALSE. + + @retval EFI_SUCCESS The group operation finished. + @retval EFI_OUT_OF_RESOURCES Failed due to lack of memory resources. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpGroupOp ( + IN OUT MNP_INSTANCE_DATA *Instance, + IN BOOLEAN JoinFlag, + IN EFI_MAC_ADDRESS *MacAddress OPTIONAL, + IN MNP_GROUP_CONTROL_BLOCK *CtrlBlk OPTIONAL + ) +{ + MNP_DEVICE_DATA *MnpDeviceData; + LIST_ENTRY *Entry; + LIST_ENTRY *NextEntry; + MNP_GROUP_ADDRESS *GroupAddress; + EFI_SIMPLE_NETWORK_MODE *SnpMode; + MNP_GROUP_CONTROL_BLOCK *NewCtrlBlk; + EFI_STATUS Status; + BOOLEAN AddressExist; + BOOLEAN NeedUpdate; + + NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE); + + MnpDeviceData = Instance->MnpServiceData->MnpDeviceData; + SnpMode = MnpDeviceData->Snp->Mode; + + if (JoinFlag) { + // + // A new group address is to be added. + // + GroupAddress = NULL; + AddressExist = FALSE; + + // + // Allocate memory for the control block. + // + NewCtrlBlk = AllocatePool (sizeof (MNP_GROUP_CONTROL_BLOCK)); + if (NewCtrlBlk == NULL) { + DEBUG ((EFI_D_ERROR, "MnpGroupOp: Failed to allocate memory resource.\n")); + + return EFI_OUT_OF_RESOURCES; + } + + NET_LIST_FOR_EACH (Entry, &MnpDeviceData->GroupAddressList) { + // + // Check whether the MacAddress is already joined by other instances. + // + GroupAddress = NET_LIST_USER_STRUCT (Entry, MNP_GROUP_ADDRESS, AddrEntry); + if (CompareMem (MacAddress, &GroupAddress->Address, SnpMode->HwAddressSize) == 0) { + AddressExist = TRUE; + break; + } + } + + if (!AddressExist) { + GroupAddress = NULL; + } + + // + // Add the GroupAddress for this instance. + // + Status = MnpGroupOpAddCtrlBlk ( + Instance, + NewCtrlBlk, + GroupAddress, + MacAddress, + SnpMode->HwAddressSize + ); + if (EFI_ERROR (Status)) { + return Status; + } + + NeedUpdate = TRUE; + } else { + if (MacAddress != NULL) { + ASSERT (CtrlBlk != NULL); + + // + // Leave the specific multicast mac address. + // + NeedUpdate = MnpGroupOpDelCtrlBlk (Instance, CtrlBlk); + } else { + // + // Leave all multicast mac addresses. + // + NeedUpdate = FALSE; + + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Instance->GroupCtrlBlkList) { + + NewCtrlBlk = NET_LIST_USER_STRUCT ( + Entry, + MNP_GROUP_CONTROL_BLOCK, + CtrlBlkEntry + ); + // + // Update is required if the group address left is no longer used + // by other instances. + // + NeedUpdate = MnpGroupOpDelCtrlBlk (Instance, NewCtrlBlk); + } + } + } + + Status = EFI_SUCCESS; + + if (NeedUpdate) { + // + // Reconfigure the receive filters if necessary. + // + Status = MnpConfigReceiveFilters (MnpDeviceData); + } + + return Status; +} diff --git a/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpDriver.c b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpDriver.c new file mode 100644 index 00000000..20b75cb7 --- /dev/null +++ b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpDriver.c @@ -0,0 +1,683 @@ +/** @file + Implementation of driver entry point and driver binding protocol. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR> +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "MnpDriver.h" +#include "MnpImpl.h" +#include "MnpVlan.h" + +EFI_DRIVER_BINDING_PROTOCOL gMnpDriverBinding = { + MnpDriverBindingSupported, + MnpDriverBindingStart, + MnpDriverBindingStop, + 0xa, + NULL, + NULL +}; + +/** + Callback function which provided by user to remove one node in NetDestroyLinkList process. + + @param[in] Entry The entry to be removed. + @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList. + + @retval EFI_SUCCESS The entry has been removed successfully. + @retval Others Fail to remove the entry. + +**/ +EFI_STATUS +EFIAPI +MnpDestroyServiceDataEntry ( + IN LIST_ENTRY *Entry, + IN VOID *Context + ) +{ + MNP_SERVICE_DATA *MnpServiceData; + + MnpServiceData = MNP_SERVICE_DATA_FROM_LINK (Entry); + return MnpDestroyServiceData (MnpServiceData); +} + +/** + Callback function which provided by user to remove one node in NetDestroyLinkList process. + + @param[in] Entry The entry to be removed. + @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList. + + @retval EFI_SUCCESS The entry has been removed successfully. + @retval Others Fail to remove the entry. + +**/ +EFI_STATUS +EFIAPI +MnpDestroyServiceChildEntry ( + IN LIST_ENTRY *Entry, + IN VOID *Context + ) +{ + MNP_SERVICE_DATA *MnpServiceData; + + MnpServiceData = MNP_SERVICE_DATA_FROM_LINK (Entry); + return MnpDestroyServiceChild (MnpServiceData); +} + +/** + Test to see if this driver supports ControllerHandle. This service + is called by the EFI boot service ConnectController(). In + order to make drivers as small as possible, there are a few calling + restrictions for this service. ConnectController() must + follow these calling restrictions. If any other agent wishes to call + Supported() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to test. + @param[in] RemainingDevicePath Optional parameter use to pick a specific + child device to start. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_ALREADY_STARTED This driver is already running on this device. + @retval Others This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +MnpDriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + + // + // Test to open the Simple Network protocol BY_DRIVER. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiSimpleNetworkProtocolGuid, + (VOID **) &Snp, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Close the opened SNP protocol. + // + gBS->CloseProtocol ( + ControllerHandle, + &gEfiSimpleNetworkProtocolGuid, + This->DriverBindingHandle, + ControllerHandle + ); + + return EFI_SUCCESS; +} + + +/** + Start this driver on ControllerHandle. This service is called by the + EFI boot service ConnectController(). In order to make drivers as small + as possible, there are a few calling restrictions for this service. + ConnectController() must follow these calling restrictions. If any other + agent wishes to call Start() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind driver to. + @param[in] RemainingDevicePath Optional parameter use to pick a specific + child device to start. + + @retval EFI_SUCCESS This driver is added to ControllerHandle. + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for Mnp Service Data. + @retval Others This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +MnpDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + MNP_SERVICE_DATA *MnpServiceData; + MNP_DEVICE_DATA *MnpDeviceData; + LIST_ENTRY *Entry; + VLAN_TCI *VlanVariable; + UINTN NumberOfVlan; + UINTN Index; + + VlanVariable = NULL; + + // + // Initialize the Mnp Device Data + // + MnpDeviceData = AllocateZeroPool (sizeof (MNP_DEVICE_DATA)); + if (MnpDeviceData == NULL) { + DEBUG ((EFI_D_ERROR, "MnpDriverBindingStart(): Failed to allocate the Mnp Device Data.\n")); + + return EFI_OUT_OF_RESOURCES; + } + + Status = MnpInitializeDeviceData (MnpDeviceData, This->DriverBindingHandle, ControllerHandle); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MnpDriverBindingStart: MnpInitializeDeviceData failed, %r.\n", Status)); + + FreePool (MnpDeviceData); + return Status; + } + + // + // Check whether NIC driver has already produced VlanConfig protocol + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiVlanConfigProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (!EFI_ERROR (Status)) { + // + // NIC hardware already implement VLAN, + // no need to provide software VLAN implementation in MNP driver + // + MnpDeviceData->NumberOfVlan = 0; + ZeroMem (&MnpDeviceData->VlanConfig, sizeof (EFI_VLAN_CONFIG_PROTOCOL)); + MnpServiceData = MnpCreateServiceData (MnpDeviceData, 0, 0); + Status = (MnpServiceData != NULL) ? EFI_SUCCESS : EFI_OUT_OF_RESOURCES; + goto Exit; + } + + // + // Install VLAN Config Protocol + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &ControllerHandle, + &gEfiVlanConfigProtocolGuid, + &MnpDeviceData->VlanConfig, + NULL + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Get current VLAN configuration from EFI Variable + // + NumberOfVlan = 0; + Status = MnpGetVlanVariable (MnpDeviceData, &NumberOfVlan, &VlanVariable); + if (EFI_ERROR (Status)) { + // + // No VLAN is set, create a default MNP service data for untagged frame + // + MnpDeviceData->NumberOfVlan = 0; + MnpServiceData = MnpCreateServiceData (MnpDeviceData, 0, 0); + Status = (MnpServiceData != NULL) ? EFI_SUCCESS : EFI_OUT_OF_RESOURCES; + goto Exit; + } + + // + // Create MNP service data for each VLAN + // + MnpDeviceData->NumberOfVlan = NumberOfVlan; + for (Index = 0; Index < NumberOfVlan; Index++) { + MnpServiceData = MnpCreateServiceData ( + MnpDeviceData, + VlanVariable[Index].Bits.Vid, + (UINT8) VlanVariable[Index].Bits.Priority + ); + + if (MnpServiceData == NULL) { + Status = EFI_OUT_OF_RESOURCES; + + goto Exit; + } + } + +Exit: + if (VlanVariable != NULL) { + FreePool (VlanVariable); + } + + if (EFI_ERROR (Status)) { + // + // Destroy all MNP service data + // + while (!IsListEmpty (&MnpDeviceData->ServiceList)) { + Entry = GetFirstNode (&MnpDeviceData->ServiceList); + MnpServiceData = MNP_SERVICE_DATA_FROM_LINK (Entry); + MnpDestroyServiceData (MnpServiceData); + } + + // + // Uninstall the VLAN Config Protocol if any + // + if (MnpDeviceData->VlanConfig.Set != NULL) { + gBS->UninstallMultipleProtocolInterfaces ( + MnpDeviceData->ControllerHandle, + &gEfiVlanConfigProtocolGuid, + &MnpDeviceData->VlanConfig, + NULL + ); + } + + // + // Destroy Mnp Device Data + // + MnpDestroyDeviceData (MnpDeviceData, This->DriverBindingHandle); + FreePool (MnpDeviceData); + } + + return Status; +} + +/** + Stop this driver on ControllerHandle. This service is called by the + EFI boot service DisconnectController(). In order to make drivers as + small as possible, there are a few calling restrictions for this service. + DisconnectController() must follow these calling restrictions. If any other + agent wishes to call Stop() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on. + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If + number of children is zero stop the entire + bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed ControllerHandle. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +MnpDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + EFI_VLAN_CONFIG_PROTOCOL *VlanConfig; + MNP_DEVICE_DATA *MnpDeviceData; + MNP_SERVICE_DATA *MnpServiceData; + LIST_ENTRY *List; + UINTN ListLength; + + // + // Try to retrieve MNP service binding protocol from the ControllerHandle + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiManagedNetworkServiceBindingProtocolGuid, + (VOID **) &ServiceBinding, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + // + // Retrieve VLAN Config Protocol from the ControllerHandle + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiVlanConfigProtocolGuid, + (VOID **) &VlanConfig, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MnpDriverBindingStop: try to stop unknown Controller.\n")); + return EFI_DEVICE_ERROR; + } + + MnpDeviceData = MNP_DEVICE_DATA_FROM_THIS (VlanConfig); + } else { + MnpServiceData = MNP_SERVICE_DATA_FROM_THIS (ServiceBinding); + MnpDeviceData = MnpServiceData->MnpDeviceData; + } + + if (NumberOfChildren == 0) { + // + // Destroy all MNP service data + // + List = &MnpDeviceData->ServiceList; + Status = NetDestroyLinkList ( + List, + MnpDestroyServiceDataEntry, + NULL, + &ListLength + ); + if (EFI_ERROR (Status) || ListLength !=0) { + return EFI_DEVICE_ERROR; + } + + // + // Uninstall the VLAN Config Protocol if any + // + if (MnpDeviceData->VlanConfig.Set != NULL) { + gBS->UninstallMultipleProtocolInterfaces ( + MnpDeviceData->ControllerHandle, + &gEfiVlanConfigProtocolGuid, + &MnpDeviceData->VlanConfig, + NULL + ); + } + + // + // Destroy Mnp Device Data + // + MnpDestroyDeviceData (MnpDeviceData, This->DriverBindingHandle); + FreePool (MnpDeviceData); + + if (gMnpControllerNameTable != NULL) { + FreeUnicodeStringTable (gMnpControllerNameTable); + gMnpControllerNameTable = NULL; + } + return EFI_SUCCESS; + } + + // + // Stop all MNP child + // + List = &MnpDeviceData->ServiceList; + Status = NetDestroyLinkList ( + List, + MnpDestroyServiceChildEntry, + NULL, + &ListLength + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + return EFI_SUCCESS; +} + + +/** + Creates a child handle with a set of I/O services. + + @param[in] This Protocol instance pointer. + @param[in, out] ChildHandle Pointer to the handle of the child to create. If + it is NULL, then a new handle is created. If + it is not NULL, then the I/O services are added + to the existing child handle. + + @retval EFI_SUCCESS The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to + create the child. + @retval Others The child handle was not created. + +**/ +EFI_STATUS +EFIAPI +MnpServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN OUT EFI_HANDLE *ChildHandle + ) +{ + EFI_STATUS Status; + MNP_SERVICE_DATA *MnpServiceData; + MNP_INSTANCE_DATA *Instance; + VOID *MnpSb; + EFI_TPL OldTpl; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + MnpServiceData = MNP_SERVICE_DATA_FROM_THIS (This); + + // + // Allocate buffer for the new instance. + // + Instance = AllocateZeroPool (sizeof (MNP_INSTANCE_DATA)); + if (Instance == NULL) { + DEBUG ((DEBUG_ERROR, "MnpServiceBindingCreateChild: Failed to allocate memory for the new instance.\n")); + + return EFI_OUT_OF_RESOURCES; + } + + // + // Init the instance data. + // + MnpInitializeInstanceData (MnpServiceData, Instance); + + Status = gBS->InstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiManagedNetworkProtocolGuid, + &Instance->ManagedNetwork, + NULL + ); + if (EFI_ERROR (Status)) { + DEBUG ( + (EFI_D_ERROR, + "MnpServiceBindingCreateChild: Failed to install the MNP protocol, %r.\n", + Status) + ); + + goto ErrorExit; + } + + // + // Save the instance's childhandle. + // + Instance->Handle = *ChildHandle; + + Status = gBS->OpenProtocol ( + MnpServiceData->ServiceHandle, + &gEfiManagedNetworkServiceBindingProtocolGuid, + (VOID **) &MnpSb, + gMnpDriverBinding.DriverBindingHandle, + Instance->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + goto ErrorExit; + } + + // + // Add the child instance into ChildrenList. + // + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + InsertTailList (&MnpServiceData->ChildrenList, &Instance->InstEntry); + MnpServiceData->ChildrenNumber++; + + gBS->RestoreTPL (OldTpl); + +ErrorExit: + + if (EFI_ERROR (Status)) { + + if (Instance->Handle != NULL) { + + gBS->UninstallMultipleProtocolInterfaces ( + Instance->Handle, + &gEfiManagedNetworkProtocolGuid, + &Instance->ManagedNetwork, + NULL + ); + } + + FreePool (Instance); + } + + return Status; +} + + +/** + Destroys a child handle with a set of I/O services. + + The DestroyChild() function does the opposite of CreateChild(). It removes a + protocol that was installed by CreateChild() from ChildHandle. If the removed + protocol is the last protocol on ChildHandle, then ChildHandle is destroyed. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL + instance. + @param[in] ChildHandle Handle of the child to destroy. + + @retval EFI_SUCCESS The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that + is being removed. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the + ChildHandle because its services are being + used. + @retval Others The child handle was not destroyed. + +**/ +EFI_STATUS +EFIAPI +MnpServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ) +{ + EFI_STATUS Status; + MNP_SERVICE_DATA *MnpServiceData; + EFI_MANAGED_NETWORK_PROTOCOL *ManagedNetwork; + MNP_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + MnpServiceData = MNP_SERVICE_DATA_FROM_THIS (This); + + // + // Try to retrieve ManagedNetwork Protocol from ChildHandle. + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiManagedNetworkProtocolGuid, + (VOID **) &ManagedNetwork, + gMnpDriverBinding.DriverBindingHandle, + ChildHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + Instance = MNP_INSTANCE_DATA_FROM_THIS (ManagedNetwork); + + // + // MnpServiceBindingDestroyChild may be called twice: first called by + // MnpServiceBindingStop, second called by uninstalling the MNP protocol + // in this ChildHandle. Use destroyed to make sure the resource clean code + // will only excecute once. + // + if (Instance->Destroyed) { + return EFI_SUCCESS; + } + + Instance->Destroyed = TRUE; + + // + // Close the Simple Network protocol. + // + gBS->CloseProtocol ( + MnpServiceData->ServiceHandle, + &gEfiManagedNetworkServiceBindingProtocolGuid, + MnpServiceData->MnpDeviceData->ImageHandle, + ChildHandle + ); + + // + // Uninstall the ManagedNetwork protocol. + // + Status = gBS->UninstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiManagedNetworkProtocolGuid, + &Instance->ManagedNetwork, + NULL + ); + if (EFI_ERROR (Status)) { + DEBUG ( + (EFI_D_ERROR, + "MnpServiceBindingDestroyChild: Failed to uninstall the ManagedNetwork protocol, %r.\n", + Status) + ); + + Instance->Destroyed = FALSE; + return Status; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Reset the configuration. + // + ManagedNetwork->Configure (ManagedNetwork, NULL); + + // + // Try to flush the RcvdPacketQueue. + // + MnpFlushRcvdDataQueue (Instance); + + // + // Clean the RxTokenMap. + // + NetMapClean (&Instance->RxTokenMap); + + // + // Remove this instance from the ChildrenList. + // + RemoveEntryList (&Instance->InstEntry); + MnpServiceData->ChildrenNumber--; + + gBS->RestoreTPL (OldTpl); + + FreePool (Instance); + + return Status; +} + +/** + The entry point for Mnp driver which installs the driver binding and component + name protocol on its ImageHandle. + + @param[in] ImageHandle The image handle of the driver. + @param[in] SystemTable The system table. + + @retval EFI_SUCCESS The driver binding and component name protocols are + successfully installed. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +EFIAPI +MnpDriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + return EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gMnpDriverBinding, + ImageHandle, + &gMnpComponentName, + &gMnpComponentName2 + ); +} diff --git a/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpDriver.h b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpDriver.h new file mode 100644 index 00000000..70541f03 --- /dev/null +++ b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpDriver.h @@ -0,0 +1,268 @@ +/** @file + Declaration of structures and functions for MnpDxe driver. + +Copyright (c) 2005 - 2016, Intel Corporation. All rights reserved.<BR> +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _MNP_DRIVER_H_ +#define _MNP_DRIVER_H_ + +#include <Uefi.h> + +#include <Protocol/ManagedNetwork.h> +#include <Protocol/SimpleNetwork.h> +#include <Protocol/ServiceBinding.h> +#include <Protocol/VlanConfig.h> + +#include <Library/BaseLib.h> +#include <Library/BaseMemoryLib.h> +#include <Library/DebugLib.h> +#include <Library/MemoryAllocationLib.h> +#include <Library/UefiBootServicesTableLib.h> +#include <Library/UefiLib.h> +#include <Library/NetLib.h> +#include <Library/DpcLib.h> +#include <Library/UefiRuntimeServicesTableLib.h> +#include <Library/DevicePathLib.h> +#include <Library/PrintLib.h> + +#include "ComponentName.h" + +#define MNP_DEVICE_DATA_SIGNATURE SIGNATURE_32 ('M', 'n', 'p', 'D') + +// +// Global Variables +// +extern EFI_DRIVER_BINDING_PROTOCOL gMnpDriverBinding; + +typedef struct { + UINT32 Signature; + + EFI_HANDLE ControllerHandle; + EFI_HANDLE ImageHandle; + + EFI_VLAN_CONFIG_PROTOCOL VlanConfig; + UINTN NumberOfVlan; + CHAR16 *MacString; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + + // + // List of MNP_SERVICE_DATA + // + LIST_ENTRY ServiceList; + // + // Number of configured MNP Service Binding child + // + UINTN ConfiguredChildrenNumber; + + LIST_ENTRY GroupAddressList; + UINT32 GroupAddressCount; + + LIST_ENTRY FreeTxBufList; + LIST_ENTRY AllTxBufList; + UINT32 TxBufCount; + + NET_BUF_QUEUE FreeNbufQue; + INTN NbufCnt; + + EFI_EVENT PollTimer; + BOOLEAN EnableSystemPoll; + + EFI_EVENT TimeoutCheckTimer; + EFI_EVENT MediaDetectTimer; + + UINT32 UnicastCount; + UINT32 BroadcastCount; + UINT32 MulticastCount; + UINT32 PromiscuousCount; + + // + // The size of the data buffer in the MNP_PACKET_BUFFER used to + // store a packet. + // + UINT32 BufferLength; + UINT32 PaddingSize; + NET_BUF *RxNbufCache; +} MNP_DEVICE_DATA; + +#define MNP_DEVICE_DATA_FROM_THIS(a) \ + CR ( \ + (a), \ + MNP_DEVICE_DATA, \ + VlanConfig, \ + MNP_DEVICE_DATA_SIGNATURE \ + ) + +#define MNP_SERVICE_DATA_SIGNATURE SIGNATURE_32 ('M', 'n', 'p', 'S') + +typedef struct { + UINT32 Signature; + + LIST_ENTRY Link; + + MNP_DEVICE_DATA *MnpDeviceData; + EFI_HANDLE ServiceHandle; + EFI_SERVICE_BINDING_PROTOCOL ServiceBinding; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + + LIST_ENTRY ChildrenList; + UINTN ChildrenNumber; + + UINT32 Mtu; + + UINT16 VlanId; + UINT8 Priority; +} MNP_SERVICE_DATA; + + +#define MNP_SERVICE_DATA_FROM_THIS(a) \ + CR ( \ + (a), \ + MNP_SERVICE_DATA, \ + ServiceBinding, \ + MNP_SERVICE_DATA_SIGNATURE \ + ) + +#define MNP_SERVICE_DATA_FROM_LINK(a) \ + CR ( \ + (a), \ + MNP_SERVICE_DATA, \ + Link, \ + MNP_SERVICE_DATA_SIGNATURE \ + ) + + +/** + Test to see if this driver supports ControllerHandle. This service + is called by the EFI boot service ConnectController(). In + order to make drivers as small as possible, there are a few calling + restrictions for this service. ConnectController() must + follow these calling restrictions. If any other agent wishes to call + Supported() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to test. + @param[in] RemainingDevicePath Optional parameter use to pick a specific + child device to start. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_ALREADY_STARTED This driver is already running on this device. + @retval Others This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +MnpDriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Start this driver on ControllerHandle. This service is called by the + EFI boot service ConnectController(). In order to make drivers as small + as possible, there are a few calling restrictions for this service. + ConnectController() must follow these calling restrictions. If any other + agent wishes to call Start() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind driver to. + @param[in] RemainingDevicePath Optional parameter use to pick a specific + child device to start. + + @retval EFI_SUCCESS This driver is added to ControllerHandle. + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for Mnp Service Data. + @retval Others This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +MnpDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + + +/** + Stop this driver on ControllerHandle. This service is called by the + EFI boot service DisconnectController(). In order to make drivers as + small as possible, there are a few calling restrictions for this service. + DisconnectController() must follow these calling restrictions. If any other + agent wishes to call Stop() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on. + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If + number of children is zero stop the entire + bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed ControllerHandle. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +MnpDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ); + +/** + Creates a child handle with a set of I/O services. + + @param[in] This Protocol instance pointer. + @param[in, out] ChildHandle Pointer to the handle of the child to create. If + it is NULL, then a new handle is created. If + it is not NULL, then the I/O services are added + to the existing child handle. + + @retval EFI_SUCCESS The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to + create the child. + @retval Others The child handle was not created. + +**/ +EFI_STATUS +EFIAPI +MnpServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN OUT EFI_HANDLE *ChildHandle + ); + +/** + Destroys a child handle with a set of I/O services. + + The DestroyChild() function does the opposite of CreateChild(). It removes a + protocol that was installed by CreateChild() from ChildHandle. If the removed + protocol is the last protocol on ChildHandle, then ChildHandle is destroyed. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL + instance. + @param[in] ChildHandle Handle of the child to destroy. + + @retval EFI_SUCCESS The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that + is being removed. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the + ChildHandle because its services are being + used. + @retval Others The child handle was not destroyed. + +**/ +EFI_STATUS +EFIAPI +MnpServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ); + +#endif diff --git a/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpDxe.inf b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpDxe.inf new file mode 100644 index 00000000..7dd7d4e2 --- /dev/null +++ b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpDxe.inf @@ -0,0 +1,68 @@ +## @file +# This module produces EFI MNP Protocol, EFI MNP Service Binding Protocol and EFI VLAN Protocol. +# +# This module produces EFI Managed Network Protocol upon EFI Simple Network Protocol, +# to provide raw asynchronous network I/O services. It also produces EFI VLAN Protocol +# to provide manageability interface for VLAN configuration. +# +# Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = MnpDxe + MODULE_UNI_FILE = MnpDxe.uni + FILE_GUID = 025BBFC7-E6A9-4b8b-82AD-6815A1AEAF4A + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = MnpDriverEntryPoint + UNLOAD_IMAGE = NetLibDefaultUnload +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# +# DRIVER_BINDING = gMnpDriverBinding +# COMPONENT_NAME = gMnpComponentName +# COMPONENT_NAME2 = gMnpComponentName2 +# + +[Sources] + MnpMain.c + MnpIo.c + ComponentName.h + MnpDriver.h + ComponentName.c + MnpDriver.c + MnpConfig.c + MnpImpl.h + MnpVlan.h + MnpVlan.c + +[Packages] + MdePkg/MdePkg.dec + NetworkPkg/NetworkPkg.dec + +[LibraryClasses] + BaseLib + BaseMemoryLib + MemoryAllocationLib + UefiLib + UefiBootServicesTableLib + UefiDriverEntryPoint + DebugLib + NetLib + DpcLib + +[Protocols] + gEfiManagedNetworkServiceBindingProtocolGuid ## BY_START + gEfiSimpleNetworkProtocolGuid ## TO_START + gEfiManagedNetworkProtocolGuid ## BY_START + ## BY_START + ## UNDEFINED # variable + gEfiVlanConfigProtocolGuid + +[UserExtensions.TianoCore."ExtraFiles"] + MnpDxeExtra.uni diff --git a/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpDxe.uni b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpDxe.uni new file mode 100644 index 00000000..481e7914 --- /dev/null +++ b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpDxe.uni @@ -0,0 +1,18 @@ +// /** @file
+// This module produces EFI MNP Protocol, EFI MNP Service Binding Protocol and EFI VLAN Protocol.
+//
+// This module produces EFI Managed Network Protocol upon EFI Simple Network Protocol,
+// to provide raw asynchronous network I/O services. It also produces EFI VLAN Protocol
+// to provide manageability interface for VLAN configuration.
+//
+// Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+//
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+// **/
+
+
+#string STR_MODULE_ABSTRACT #language en-US "Produces EFI MNP Protocol, EFI MNP Service Binding Protocol and EFI VLAN Protocol"
+
+#string STR_MODULE_DESCRIPTION #language en-US "This module produces EFI Managed Network Protocol upon EFI Simple Network Protocol to provide raw asynchronous network I/O services. It also produces EFI VLAN Protocol to provide manageability interface for VLAN configuration."
+
diff --git a/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpDxeExtra.uni b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpDxeExtra.uni new file mode 100644 index 00000000..504ad2d8 --- /dev/null +++ b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpDxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file
+// MnpDxe Localized Strings and Content
+//
+// Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
+//
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+// **/
+
+#string STR_PROPERTIES_MODULE_NAME
+#language en-US
+"MNP DXE Driver"
+
+
diff --git a/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpImpl.h b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpImpl.h new file mode 100644 index 00000000..a15fed75 --- /dev/null +++ b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpImpl.h @@ -0,0 +1,898 @@ +/** @file + Declaration of structures and functions of MnpDxe driver. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR> +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _MNP_IMPL_H_ +#define _MNP_IMPL_H_ + +#include "MnpDriver.h" + +#define NET_ETHER_FCS_SIZE 4 + +#define MNP_SYS_POLL_INTERVAL (10 * TICKS_PER_MS) // 10 milliseconds +#define MNP_TIMEOUT_CHECK_INTERVAL (50 * TICKS_PER_MS) // 50 milliseconds +#define MNP_MEDIA_DETECT_INTERVAL (500 * TICKS_PER_MS) // 500 milliseconds +#define MNP_TX_TIMEOUT_TIME (500 * TICKS_PER_MS) // 500 milliseconds +#define MNP_INIT_NET_BUFFER_NUM 512 +#define MNP_NET_BUFFER_INCREASEMENT 64 +#define MNP_MAX_NET_BUFFER_NUM 65536 +#define MNP_TX_BUFFER_INCREASEMENT 32 // Same as the recycling Q length for xmit_done in UNDI command. +#define MNP_MAX_TX_BUFFER_NUM 65536 + +#define MNP_MAX_RCVD_PACKET_QUE_SIZE 256 + +#define MNP_RECEIVE_UNICAST 0x01 +#define MNP_RECEIVE_BROADCAST 0x02 + +#define UNICAST_PACKET MNP_RECEIVE_UNICAST +#define BROADCAST_PACKET MNP_RECEIVE_BROADCAST + +#define MNP_INSTANCE_DATA_SIGNATURE SIGNATURE_32 ('M', 'n', 'p', 'I') + +#define MNP_INSTANCE_DATA_FROM_THIS(a) \ + CR ( \ + (a), \ + MNP_INSTANCE_DATA, \ + ManagedNetwork, \ + MNP_INSTANCE_DATA_SIGNATURE \ + ) + +typedef struct { + UINT32 Signature; + + MNP_SERVICE_DATA *MnpServiceData; + + EFI_HANDLE Handle; + + LIST_ENTRY InstEntry; + + EFI_MANAGED_NETWORK_PROTOCOL ManagedNetwork; + + BOOLEAN Configured; + BOOLEAN Destroyed; + + LIST_ENTRY GroupCtrlBlkList; + + NET_MAP RxTokenMap; + + LIST_ENTRY RxDeliveredPacketQueue; + LIST_ENTRY RcvdPacketQueue; + UINTN RcvdPacketQueueSize; + + EFI_MANAGED_NETWORK_CONFIG_DATA ConfigData; + + UINT8 ReceiveFilter; +} MNP_INSTANCE_DATA; + +typedef struct { + LIST_ENTRY AddrEntry; + EFI_MAC_ADDRESS Address; + INTN RefCnt; +} MNP_GROUP_ADDRESS; + +typedef struct { + LIST_ENTRY CtrlBlkEntry; + MNP_GROUP_ADDRESS *GroupAddress; +} MNP_GROUP_CONTROL_BLOCK; + +typedef struct { + LIST_ENTRY WrapEntry; + MNP_INSTANCE_DATA *Instance; + EFI_MANAGED_NETWORK_RECEIVE_DATA RxData; + NET_BUF *Nbuf; + UINT64 TimeoutTick; +} MNP_RXDATA_WRAP; + +#define MNP_TX_BUF_WRAP_SIGNATURE SIGNATURE_32 ('M', 'T', 'B', 'W') + +typedef struct { + UINT32 Signature; + LIST_ENTRY WrapEntry; // Link to FreeTxBufList + LIST_ENTRY AllEntry; // Link to AllTxBufList + BOOLEAN InUse; + UINT8 TxBuf[1]; +} MNP_TX_BUF_WRAP; + +/** + Initialize the mnp device context data. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + @param[in] ImageHandle The driver image handle. + @param[in] ControllerHandle Handle of device to bind driver to. + + @retval EFI_SUCCESS The mnp service context is initialized. + @retval EFI_UNSUPPORTED ControllerHandle does not support Simple Network Protocol. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpInitializeDeviceData ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN EFI_HANDLE ImageHandle, + IN EFI_HANDLE ControllerHandle + ); + +/** + Destroy the MNP device context data. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + @param[in] ImageHandle The driver image handle. + +**/ +VOID +MnpDestroyDeviceData ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN EFI_HANDLE ImageHandle + ); + +/** + Create mnp service context data. + + @param[in] MnpDeviceData Pointer to the mnp device context data. + @param[in] VlanId The VLAN ID. + @param[in] Priority The VLAN priority. If VlanId is 0, + Priority is ignored. + + @return A pointer to MNP_SERVICE_DATA or NULL if failed to create MNP service context. + +**/ +MNP_SERVICE_DATA * +MnpCreateServiceData ( + IN MNP_DEVICE_DATA *MnpDeviceData, + IN UINT16 VlanId, + IN UINT8 Priority OPTIONAL + ); + +/** + Initialize the mnp service context data. + + @param[in, out] MnpServiceData Pointer to the mnp service context data. + @param[in] ImageHandle The driver image handle. + @param[in] ControllerHandle Handle of device to bind driver to. + + @retval EFI_SUCCESS The mnp service context is initialized. + @retval EFI_UNSUPPORTED ControllerHandle does not support Simple Network Protocol. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpInitializeServiceData ( + IN OUT MNP_SERVICE_DATA *MnpServiceData, + IN EFI_HANDLE ImageHandle, + IN EFI_HANDLE ControllerHandle + ); + +/** + Destroy the MNP service context data. + + @param[in, out] MnpServiceData Pointer to the mnp service context data. + + @retval EFI_SUCCESS The mnp service context is destroyed. + @retval Others Errors as indicated. + +**/ +EFI_STATUS +MnpDestroyServiceData ( + IN OUT MNP_SERVICE_DATA *MnpServiceData + ); + +/** + Destroy all child of the MNP service data. + + @param[in, out] MnpServiceData Pointer to the mnp service context data. + + @retval EFI_SUCCESS All child are destroyed. + @retval Others Failed to destroy all child. + +**/ +EFI_STATUS +MnpDestroyServiceChild ( + IN OUT MNP_SERVICE_DATA *MnpServiceData + ); + +/** + Find the MNP Service Data for given VLAN ID. + + @param[in] MnpDeviceData Pointer to the mnp device context data. + @param[in] VlanId The VLAN ID. + + @return A pointer to MNP_SERVICE_DATA or NULL if not found. + +**/ +MNP_SERVICE_DATA * +MnpFindServiceData ( + IN MNP_DEVICE_DATA *MnpDeviceData, + IN UINT16 VlanId + ); + +/** + Initialize the mnp instance context data. + + @param[in] MnpServiceData Pointer to the mnp service context data. + @param[in, out] Instance Pointer to the mnp instance context data + to initialize. + +**/ +VOID +MnpInitializeInstanceData ( + IN MNP_SERVICE_DATA *MnpServiceData, + IN OUT MNP_INSTANCE_DATA *Instance + ); + +/** + Check whether the token specified by Arg matches the token in Item. + + @param[in] Map Pointer to the NET_MAP. + @param[in] Item Pointer to the NET_MAP_ITEM. + @param[in] Arg Pointer to the Arg, it's a pointer to the token to + check. + + @retval EFI_SUCCESS The token specified by Arg is different from the + token in Item. + @retval EFI_ACCESS_DENIED The token specified by Arg is the same as that in + Item. + +**/ +EFI_STATUS +EFIAPI +MnpTokenExist ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Arg + ); + +/** + Cancel the token specified by Arg if it matches the token in Item. + + @param[in, out] Map Pointer to the NET_MAP. + @param[in, out] Item Pointer to the NET_MAP_ITEM. + @param[in] Arg Pointer to the Arg, it's a pointer to the + token to cancel. + + @retval EFI_SUCCESS The Arg is NULL, and the token in Item is cancelled, + or the Arg isn't NULL, and the token in Item is + different from the Arg. + @retval EFI_ABORTED The Arg isn't NULL, the token in Item mathces the + Arg, and the token is cancelled. + +**/ +EFI_STATUS +EFIAPI +MnpCancelTokens ( + IN OUT NET_MAP *Map, + IN OUT NET_MAP_ITEM *Item, + IN VOID *Arg + ); + +/** + Flush the instance's received data. + + @param[in, out] Instance Pointer to the mnp instance context data. + +**/ +VOID +MnpFlushRcvdDataQueue ( + IN OUT MNP_INSTANCE_DATA *Instance + ); + +/** + Configure the Instance using ConfigData. + + @param[in, out] Instance Pointer to the mnp instance context data. + @param[in] ConfigData Pointer to the configuration data used to configure + the instance. + + @retval EFI_SUCCESS The Instance is configured. + @retval EFI_UNSUPPORTED EnableReceiveTimestamps is on and the + implementation doesn't support it. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpConfigureInstance ( + IN OUT MNP_INSTANCE_DATA *Instance, + IN EFI_MANAGED_NETWORK_CONFIG_DATA *ConfigData OPTIONAL + ); + +/** + Do the group operations for this instance. + + @param[in, out] Instance Pointer to the instance context data. + @param[in] JoinFlag Set to TRUE to join a group. Set to TRUE to + leave a group/groups. + @param[in] MacAddress Pointer to the group address to join or leave. + @param[in] CtrlBlk Pointer to the group control block if JoinFlag + is FALSE. + + @retval EFI_SUCCESS The group operation finished. + @retval EFI_OUT_OF_RESOURCES Failed due to lack of memory resources. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpGroupOp ( + IN OUT MNP_INSTANCE_DATA *Instance, + IN BOOLEAN JoinFlag, + IN EFI_MAC_ADDRESS *MacAddress OPTIONAL, + IN MNP_GROUP_CONTROL_BLOCK *CtrlBlk OPTIONAL + ); + +/** + Validates the Mnp transmit token. + + @param[in] Instance Pointer to the Mnp instance context data. + @param[in] Token Pointer to the transmit token to check. + + @return The Token is valid or not. + +**/ +BOOLEAN +MnpIsValidTxToken ( + IN MNP_INSTANCE_DATA *Instance, + IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token + ); + +/** + Build the packet to transmit from the TxData passed in. + + @param[in] MnpServiceData Pointer to the mnp service context data. + @param[in] TxData Pointer to the transmit data containing the information + to build the packet. + @param[out] PktBuf Pointer to record the address of the packet. + @param[out] PktLen Pointer to a UINT32 variable used to record the packet's + length. + + @retval EFI_SUCCESS TxPackage is built. + @retval EFI_OUT_OF_RESOURCES The deliver fails due to lack of memory resource. + +**/ +EFI_STATUS +MnpBuildTxPacket ( + IN MNP_SERVICE_DATA *MnpServiceData, + IN EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData, + OUT UINT8 **PktBuf, + OUT UINT32 *PktLen + ); + +/** + Synchronously send out the packet. + + This function places the packet buffer to SNP driver's tansmit queue. The packet + can be considered successfully sent out once SNP accept the packet, while the + packet buffer recycle is deferred for better performance. + + @param[in] MnpServiceData Pointer to the mnp service context data. + @param[in] Packet Pointer to the packet buffer. + @param[in] Length The length of the packet. + @param[in, out] Token Pointer to the token the packet generated from. + + @retval EFI_SUCCESS The packet is sent out. + @retval EFI_TIMEOUT Time out occurs, the packet isn't sent. + @retval EFI_DEVICE_ERROR An unexpected network error occurs. + +**/ +EFI_STATUS +MnpSyncSendPacket ( + IN MNP_SERVICE_DATA *MnpServiceData, + IN UINT8 *Packet, + IN UINT32 Length, + IN OUT EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token + ); + +/** + Try to deliver the received packet to the instance. + + @param[in, out] Instance Pointer to the mnp instance context data. + + @retval EFI_SUCCESS The received packet is delivered, or there is no + packet to deliver, or there is no available receive + token. + @retval EFI_OUT_OF_RESOURCES The deliver fails due to lack of memory resource. + +**/ +EFI_STATUS +MnpInstanceDeliverPacket ( + IN OUT MNP_INSTANCE_DATA *Instance + ); + +/** + Recycle the RxData and other resources used to hold and deliver the received + packet. + + @param[in] Event The event this notify function registered to. + @param[in] Context Pointer to the context data registered to the Event. + +**/ +VOID +EFIAPI +MnpRecycleRxData ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Try to receive a packet and deliver it. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + + @retval EFI_SUCCESS add return value to function comment + @retval EFI_NOT_STARTED The simple network protocol is not started. + @retval EFI_NOT_READY No packet received. + @retval EFI_DEVICE_ERROR An unexpected error occurs. + +**/ +EFI_STATUS +MnpReceivePacket ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData + ); + +/** + Allocate a free NET_BUF from MnpDeviceData->FreeNbufQue. If there is none + in the queue, first try to allocate some and add them into the queue, then + fetch the NET_BUF from the updated FreeNbufQue. + + @param[in, out] MnpDeviceData Pointer to the MNP_DEVICE_DATA. + + @return Pointer to the allocated free NET_BUF structure, if NULL the + operation is failed. + +**/ +NET_BUF * +MnpAllocNbuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData + ); + +/** + Try to reclaim the Nbuf into the buffer pool. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + @param[in, out] Nbuf Pointer to the NET_BUF to free. + +**/ +VOID +MnpFreeNbuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN OUT NET_BUF *Nbuf + ); + +/** + Allocate a free TX buffer from MnpDeviceData->FreeTxBufList. If there is none + in the queue, first try to recycle some from SNP, then try to allocate some and add + them into the queue, then fetch the NET_BUF from the updated FreeTxBufList. + + @param[in, out] MnpDeviceData Pointer to the MNP_DEVICE_DATA. + + @return Pointer to the allocated free NET_BUF structure, if NULL the + operation is failed. + +**/ +UINT8 * +MnpAllocTxBuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData + ); + +/** + Try to recycle all the transmitted buffer address from SNP. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + + @retval EFI_SUCCESS Successed to recycle the transmitted buffer address. + @retval Others Failed to recycle the transmitted buffer address. + +**/ +EFI_STATUS +MnpRecycleTxBuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData + ); + +/** + Remove the received packets if timeout occurs. + + @param[in] Event The event this notify function registered to. + @param[in] Context Pointer to the context data registered to the event. + +**/ +VOID +EFIAPI +MnpCheckPacketTimeout ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Poll to update MediaPresent field in SNP ModeData by Snp.GetStatus(). + + @param[in] Event The event this notify function registered to. + @param[in] Context Pointer to the context data registered to the event. + +**/ +VOID +EFIAPI +MnpCheckMediaStatus ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Poll to receive the packets from Snp. This function is either called by upperlayer + protocols/applications or the system poll timer notify mechanism. + + @param[in] Event The event this notify function registered to. + @param[in] Context Pointer to the context data registered to the event. + +**/ +VOID +EFIAPI +MnpSystemPoll ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Returns the operational parameters for the current MNP child driver. May also + support returning the underlying SNP driver mode data. + + The GetModeData() function is used to read the current mode data (operational + parameters) from the MNP or the underlying SNP. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[out] MnpConfigData Pointer to storage for MNP operational parameters. Type + EFI_MANAGED_NETWORK_CONFIG_DATA is defined in "Related + Definitions" below. + @param[out] SnpModeData Pointer to storage for SNP operational parameters. This + feature may be unsupported. Type EFI_SIMPLE_NETWORK_MODE + is defined in the EFI_SIMPLE_NETWORK_PROTOCOL. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_UNSUPPORTED The requested feature is unsupported in this + MNP implementation. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. The default values are returned in + MnpConfigData if it is not NULL. + @retval Others The mode data could not be read. + +**/ +EFI_STATUS +EFIAPI +MnpGetModeData ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL, + OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL + ); + +/** + Sets or clears the operational parameters for the MNP child driver. + + The Configure() function is used to set, change, or reset the operational + parameters for the MNP child driver instance. Until the operational parameters + have been set, no network traffic can be sent or received by this MNP child + driver instance. Once the operational parameters have been reset, no more + traffic can be sent or received until the operational parameters have been set + again. + Each MNP child driver instance can be started and stopped independently of + each other by setting or resetting their receive filter settings with the + Configure() function. + After any successful call to Configure(), the MNP child driver instance is + started. The internal periodic timer (if supported) is enabled. Data can be + transmitted and may be received if the receive filters have also been enabled. + Note: If multiple MNP child driver instances will receive the same packet + because of overlapping receive filter settings, then the first MNP child + driver instance will receive the original packet and additional instances will + receive copies of the original packet. + Note: Warning: Receive filter settings that overlap will consume extra + processor and/or DMA resources and degrade system and network performance. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] MnpConfigData Pointer to configuration data that will be assigned + to the MNP child driver instance. If NULL, the MNP + child driver instance is reset to startup defaults + and all pending transmit and receive requests are + flushed. Type EFI_MANAGED_NETWORK_CONFIG_DATA is + defined in EFI_MANAGED_NETWORK_PROTOCOL.GetModeData(). + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is + TRUE: + * This is NULL. + * MnpConfigData.ProtocolTypeFilter is not + valid. + The operational data for the MNP child driver + instance is unchanged. + @retval EFI_OUT_OF_RESOURCES Required system resources (usually memory) + could not be allocated. + The MNP child driver instance has been reset to + startup defaults. + @retval EFI_UNSUPPORTED The requested feature is unsupported in + this [MNP] implementation. The operational data + for the MNP child driver instance is unchanged. + @retval EFI_DEVICE_ERROR An unexpected network or system error + occurred. The MNP child driver instance has + been reset to startup defaults. + @retval Others The MNP child driver instance has been reset to + startup defaults. + +**/ +EFI_STATUS +EFIAPI +MnpConfigure ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL + ); + +/** + Translates an IP multicast address to a hardware (MAC) multicast address. This + function may be unsupported in some MNP implementations. + + The McastIpToMac() function translates an IP multicast address to a hardware + (MAC) multicast address. This function may be implemented by calling the + underlying EFI_SIMPLE_NETWORK. MCastIpToMac() function, which may also be + unsupported in some MNP implementations. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] Ipv6Flag Set to TRUE to if IpAddress is an IPv6 multicast address. + Set to FALSE if IpAddress is an IPv4 multicast address. + @param[in] IpAddress Pointer to the multicast IP address (in network byte + order) to convert. + @param[out] MacAddress Pointer to the resulting multicast MAC address. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One of the following conditions is TRUE: + * This is NULL. + * IpAddress is NULL. + * IpAddress is not a valid multicast IP + address. + * MacAddress is NULL. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_UNSUPPORTED The requested feature is unsupported in this + MNP implementation. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred. + @retval Others The address could not be converted. +**/ +EFI_STATUS +EFIAPI +MnpMcastIpToMac ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN BOOLEAN Ipv6Flag, + IN EFI_IP_ADDRESS *IpAddress, + OUT EFI_MAC_ADDRESS *MacAddress + ); + +/** + Enables and disables receive filters for multicast address. This function may + be unsupported in some MNP implementations. + + The Groups() function only adds and removes multicast MAC addresses from the + filter list. The MNP driver does not transmit or process Internet Group + Management Protocol (IGMP) packets. If JoinFlag is FALSE and MacAddress is + NULL, then all joined groups are left. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] JoinFlag Set to TRUE to join this multicast group. + Set to FALSE to leave this multicast group. + @param[in] MacAddress Pointer to the multicast MAC group (address) to join or + leave. + + @retval EFI_SUCCESS The requested operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + * This is NULL. + * JoinFlag is TRUE and MacAddress is NULL. + * MacAddress is not a valid multicast MAC + address. + * The MNP multicast group settings are + unchanged. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_ALREADY_STARTED The supplied multicast group is already joined. + @retval EFI_NOT_FOUND The supplied multicast group is not joined. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred. + The MNP child driver instance has been reset to + startup defaults. + @retval EFI_UNSUPPORTED The requested feature is unsupported in this MNP + implementation. + @retval Others The requested operation could not be completed. + The MNP multicast group settings are unchanged. + +**/ +EFI_STATUS +EFIAPI +MnpGroups ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN BOOLEAN JoinFlag, + IN EFI_MAC_ADDRESS *MacAddress OPTIONAL + ); + +/** + Places asynchronous outgoing data packets into the transmit queue. + + The Transmit() function places a completion token into the transmit packet + queue. This function is always asynchronous. + The caller must fill in the Token.Event and Token.TxData fields in the + completion token, and these fields cannot be NULL. When the transmit operation + completes, the MNP updates the Token.Status field and the Token.Event is + signaled. + Note: There may be a performance penalty if the packet needs to be + defragmented before it can be transmitted by the network device. Systems in + which performance is critical should review the requirements and features of + the underlying communications device and drivers. + + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] Token Pointer to a token associated with the transmit data + descriptor. Type EFI_MANAGED_NETWORK_COMPLETION_TOKEN + is defined in "Related Definitions" below. + + @retval EFI_SUCCESS The transmit completion token was cached. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is + TRUE: + * This is NULL. + * Token is NULL. + * Token.Event is NULL. + * Token.TxData is NULL. + * Token.TxData.DestinationAddress is not + NULL and Token.TxData.HeaderLength is zero. + * Token.TxData.FragmentCount is zero. + * (Token.TxData.HeaderLength + + Token.TxData.DataLength) is not equal to the + sum of the + Token.TxData.FragmentTable[].FragmentLength + fields. + * One or more of the + Token.TxData.FragmentTable[].FragmentLength + fields is zero. + * One or more of the + Token.TxData.FragmentTable[].FragmentBufferfields + is NULL. + * Token.TxData.DataLength is greater than MTU. + @retval EFI_ACCESS_DENIED The transmit completion token is already in the + transmit queue. + @retval EFI_OUT_OF_RESOURCES The transmit data could not be queued due to a + lack of system resources (usually memory). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + The MNP child driver instance has been reset to + startup defaults. + @retval EFI_NOT_READY The transmit request could not be queued because + the transmit queue is full. + +**/ +EFI_STATUS +EFIAPI +MnpTransmit ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token + ); + +/** + Aborts an asynchronous transmit or receive request. + + The Cancel() function is used to abort a pending transmit or receive request. + If the token is in the transmit or receive request queues, after calling this + function, Token.Status will be set to EFI_ABORTED and then Token.Event will be + signaled. If the token is not in one of the queues, which usually means that + the asynchronous operation has completed, this function will not signal the + token and EFI_NOT_FOUND is returned. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] Token Pointer to a token that has been issued by + EFI_MANAGED_NETWORK_PROTOCOL.Transmit() or + EFI_MANAGED_NETWORK_PROTOCOL.Receive(). If NULL, all + pending tokens are aborted. + + @retval EFI_SUCCESS The asynchronous I/O request was aborted and + Token.Event was signaled. When Token is NULL, + all pending requests were aborted and their + events were signaled. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O + request was not found in the transmit or + receive queue. It has either completed or was + not issued by Transmit() and Receive(). + +**/ +EFI_STATUS +EFIAPI +MnpCancel ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token OPTIONAL + ); + +/** + Places an asynchronous receiving request into the receiving queue. + + The Receive() function places a completion token into the receive packet + queue. This function is always asynchronous. + The caller must fill in the Token.Event field in the completion token, and + this field cannot be NULL. When the receive operation completes, the MNP + updates the Token.Status and Token.RxData fields and the Token.Event is + signaled. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] Token Pointer to a token associated with the receive + data descriptor. Type + EFI_MANAGED_NETWORK_COMPLETION_TOKEN is defined in + EFI_MANAGED_NETWORK_PROTOCOL.Transmit(). + + @retval EFI_SUCCESS The receive completion token was cached. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is + TRUE: + * This is NULL. + * Token is NULL. + * Token.Event is NULL + @retval EFI_OUT_OF_RESOURCES The transmit data could not be queued due to a + lack of system resources (usually memory). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + The MNP child driver instance has been reset to + startup defaults. + @retval EFI_ACCESS_DENIED The receive completion token was already in the + receive queue. + @retval EFI_NOT_READY The receive request could not be queued because + the receive queue is full. + +**/ +EFI_STATUS +EFIAPI +MnpReceive ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token + ); + +/** + Polls for incoming data packets and processes outgoing data packets. + + The Poll() function can be used by network drivers and applications to + increase the rate that data packets are moved between the communications + device and the transmit and receive queues. + Normally, a periodic timer event internally calls the Poll() function. But, in + some systems, the periodic timer event may not call Poll() fast enough to + transmit and/or receive all data packets without missing packets. Drivers and + applications that are experiencing packet loss should try calling the Poll() + function more often. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The + MNP child driver instance has been reset to startup + defaults. + @retval EFI_NOT_READY No incoming or outgoing data was processed. Consider + increasing the polling rate. + @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive + queue. Consider increasing the polling rate. + +**/ +EFI_STATUS +EFIAPI +MnpPoll ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This + ); + +/** + Configure the Snp receive filters according to the instances' receive filter + settings. + + @param[in] MnpDeviceData Pointer to the mnp device context data. + + @retval EFI_SUCCESS The receive filters is configured. + @retval EFI_OUT_OF_RESOURCES The receive filters can't be configured due + to lack of memory resource. + +**/ +EFI_STATUS +MnpConfigReceiveFilters ( + IN MNP_DEVICE_DATA *MnpDeviceData + ); + +#endif diff --git a/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpIo.c b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpIo.c new file mode 100644 index 00000000..49f4e779 --- /dev/null +++ b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpIo.c @@ -0,0 +1,1133 @@ +/** @file + Implementation of Managed Network Protocol I/O functions. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR> +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "MnpImpl.h" +#include "MnpVlan.h" + +/** + Validates the Mnp transmit token. + + @param[in] Instance Pointer to the Mnp instance context data. + @param[in] Token Pointer to the transmit token to check. + + @return The Token is valid or not. + +**/ +BOOLEAN +MnpIsValidTxToken ( + IN MNP_INSTANCE_DATA *Instance, + IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token + ) +{ + MNP_SERVICE_DATA *MnpServiceData; + EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData; + UINT32 Index; + UINT32 TotalLength; + EFI_MANAGED_NETWORK_FRAGMENT_DATA *FragmentTable; + + MnpServiceData = Instance->MnpServiceData; + NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE); + + TxData = Token->Packet.TxData; + + if ((Token->Event == NULL) || (TxData == NULL) || (TxData->FragmentCount == 0)) { + // + // The token is invalid if the Event is NULL, or the TxData is NULL, or + // the fragment count is zero. + // + DEBUG ((EFI_D_WARN, "MnpIsValidTxToken: Invalid Token.\n")); + return FALSE; + } + + if ((TxData->DestinationAddress != NULL) && (TxData->HeaderLength != 0)) { + // + // The token is invalid if the HeaderLength isn't zero while the DestinationAddress + // is NULL (The destination address is already put into the packet). + // + DEBUG ((EFI_D_WARN, "MnpIsValidTxToken: DestinationAddress isn't NULL, HeaderLength must be 0.\n")); + return FALSE; + } + + TotalLength = 0; + FragmentTable = TxData->FragmentTable; + for (Index = 0; Index < TxData->FragmentCount; Index++) { + + if ((FragmentTable[Index].FragmentLength == 0) || (FragmentTable[Index].FragmentBuffer == NULL)) { + // + // The token is invalid if any FragmentLength is zero or any FragmentBuffer is NULL. + // + DEBUG ((EFI_D_WARN, "MnpIsValidTxToken: Invalid FragmentLength or FragmentBuffer.\n")); + return FALSE; + } + + TotalLength += FragmentTable[Index].FragmentLength; + } + + if ((TxData->DestinationAddress == NULL) && (FragmentTable[0].FragmentLength < TxData->HeaderLength)) { + // + // Media header is split between fragments. + // + return FALSE; + } + + if (TotalLength != (TxData->DataLength + TxData->HeaderLength)) { + // + // The length calculated from the fragment information doesn't equal to the + // sum of the DataLength and the HeaderLength. + // + DEBUG ((EFI_D_WARN, "MnpIsValidTxData: Invalid Datalength compared with the sum of fragment length.\n")); + return FALSE; + } + + if (TxData->DataLength > MnpServiceData->Mtu) { + // + // The total length is larger than the MTU. + // + DEBUG ((EFI_D_WARN, "MnpIsValidTxData: TxData->DataLength exceeds Mtu.\n")); + return FALSE; + } + + return TRUE; +} + +/** + Build the packet to transmit from the TxData passed in. + + @param[in] MnpServiceData Pointer to the mnp service context data. + @param[in] TxData Pointer to the transmit data containing the information + to build the packet. + @param[out] PktBuf Pointer to record the address of the packet. + @param[out] PktLen Pointer to a UINT32 variable used to record the packet's + length. + + @retval EFI_SUCCESS TxPackage is built. + @retval EFI_OUT_OF_RESOURCES The deliver fails due to lack of memory resource. + +**/ +EFI_STATUS +MnpBuildTxPacket ( + IN MNP_SERVICE_DATA *MnpServiceData, + IN EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData, + OUT UINT8 **PktBuf, + OUT UINT32 *PktLen + ) +{ + EFI_SIMPLE_NETWORK_MODE *SnpMode; + UINT8 *DstPos; + UINT16 Index; + MNP_DEVICE_DATA *MnpDeviceData; + UINT8 *TxBuf; + + MnpDeviceData = MnpServiceData->MnpDeviceData; + + TxBuf = MnpAllocTxBuf (MnpDeviceData); + if (TxBuf == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Reserve space for vlan tag if needed. + // + if (MnpServiceData->VlanId != 0) { + *PktBuf = TxBuf + NET_VLAN_TAG_LEN; + } else { + *PktBuf = TxBuf; + } + + if ((TxData->DestinationAddress == NULL) && (TxData->FragmentCount == 1)) { + CopyMem ( + *PktBuf, + TxData->FragmentTable[0].FragmentBuffer, + TxData->FragmentTable[0].FragmentLength + ); + + *PktLen = TxData->FragmentTable[0].FragmentLength; + } else { + // + // Either media header isn't in FragmentTable or there is more than + // one fragment, copy the data into the packet buffer. Reserve the + // media header space if necessary. + // + SnpMode = MnpDeviceData->Snp->Mode; + DstPos = *PktBuf; + *PktLen = 0; + if (TxData->DestinationAddress != NULL) { + // + // If dest address is not NULL, move DstPos to reserve space for the + // media header. Add the media header length to buflen. + // + DstPos += SnpMode->MediaHeaderSize; + *PktLen += SnpMode->MediaHeaderSize; + } + + for (Index = 0; Index < TxData->FragmentCount; Index++) { + // + // Copy the data. + // + CopyMem ( + DstPos, + TxData->FragmentTable[Index].FragmentBuffer, + TxData->FragmentTable[Index].FragmentLength + ); + DstPos += TxData->FragmentTable[Index].FragmentLength; + } + + // + // Set the buffer length. + // + *PktLen += TxData->DataLength + TxData->HeaderLength; + } + + return EFI_SUCCESS; +} + + +/** + Synchronously send out the packet. + + This function places the packet buffer to SNP driver's tansmit queue. The packet + can be considered successfully sent out once SNP accept the packet, while the + packet buffer recycle is deferred for better performance. + + @param[in] MnpServiceData Pointer to the mnp service context data. + @param[in] Packet Pointer to the packet buffer. + @param[in] Length The length of the packet. + @param[in, out] Token Pointer to the token the packet generated from. + + @retval EFI_SUCCESS The packet is sent out. + @retval EFI_TIMEOUT Time out occurs, the packet isn't sent. + @retval EFI_DEVICE_ERROR An unexpected network error occurs. + +**/ +EFI_STATUS +MnpSyncSendPacket ( + IN MNP_SERVICE_DATA *MnpServiceData, + IN UINT8 *Packet, + IN UINT32 Length, + IN OUT EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token + ) +{ + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData; + UINT32 HeaderSize; + MNP_DEVICE_DATA *MnpDeviceData; + UINT16 ProtocolType; + + MnpDeviceData = MnpServiceData->MnpDeviceData; + Snp = MnpDeviceData->Snp; + TxData = Token->Packet.TxData; + Token->Status = EFI_SUCCESS; + HeaderSize = Snp->Mode->MediaHeaderSize - TxData->HeaderLength; + + // + // Check media status before transmit packet. + // Note: media status will be updated by periodic timer MediaDetectTimer. + // + if (Snp->Mode->MediaPresentSupported && !Snp->Mode->MediaPresent) { + // + // Media not present, skip packet transmit and report EFI_NO_MEDIA + // + DEBUG ((EFI_D_WARN, "MnpSyncSendPacket: No network cable detected.\n")); + Token->Status = EFI_NO_MEDIA; + goto SIGNAL_TOKEN; + } + + + if (MnpServiceData->VlanId != 0) { + // + // Insert VLAN tag + // + MnpInsertVlanTag (MnpServiceData, TxData, &ProtocolType, &Packet, &Length); + } else { + ProtocolType = TxData->ProtocolType; + } + + // + // Transmit the packet through SNP. + // + Status = Snp->Transmit ( + Snp, + HeaderSize, + Length, + Packet, + TxData->SourceAddress, + TxData->DestinationAddress, + &ProtocolType + ); + if (Status == EFI_NOT_READY) { + Status = MnpRecycleTxBuf (MnpDeviceData); + if (EFI_ERROR (Status)) { + Token->Status = EFI_DEVICE_ERROR; + goto SIGNAL_TOKEN; + } + + Status = Snp->Transmit ( + Snp, + HeaderSize, + Length, + Packet, + TxData->SourceAddress, + TxData->DestinationAddress, + &ProtocolType + ); + } + + if (EFI_ERROR (Status)) { + Token->Status = EFI_DEVICE_ERROR; + } + +SIGNAL_TOKEN: + + gBS->SignalEvent (Token->Event); + + // + // Dispatch the DPC queued by the NotifyFunction of Token->Event. + // + DispatchDpc (); + + return EFI_SUCCESS; +} + + +/** + Try to deliver the received packet to the instance. + + @param[in, out] Instance Pointer to the mnp instance context data. + + @retval EFI_SUCCESS The received packet is delivered, or there is no + packet to deliver, or there is no available receive + token. + @retval EFI_OUT_OF_RESOURCES The deliver fails due to lack of memory resource. + +**/ +EFI_STATUS +MnpInstanceDeliverPacket ( + IN OUT MNP_INSTANCE_DATA *Instance + ) +{ + MNP_DEVICE_DATA *MnpDeviceData; + MNP_RXDATA_WRAP *RxDataWrap; + NET_BUF *DupNbuf; + EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData; + EFI_SIMPLE_NETWORK_MODE *SnpMode; + EFI_MANAGED_NETWORK_COMPLETION_TOKEN *RxToken; + + MnpDeviceData = Instance->MnpServiceData->MnpDeviceData; + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + if (NetMapIsEmpty (&Instance->RxTokenMap) || IsListEmpty (&Instance->RcvdPacketQueue)) { + // + // No pending received data or no available receive token, return. + // + return EFI_SUCCESS; + } + + ASSERT (Instance->RcvdPacketQueueSize != 0); + + RxDataWrap = NET_LIST_HEAD (&Instance->RcvdPacketQueue, MNP_RXDATA_WRAP, WrapEntry); + if (RxDataWrap->Nbuf->RefCnt > 2) { + // + // There are other instances share this Nbuf, duplicate to get a + // copy to allow the instance to do R/W operations. + // + DupNbuf = MnpAllocNbuf (MnpDeviceData); + if (DupNbuf == NULL) { + DEBUG ((EFI_D_WARN, "MnpDeliverPacket: Failed to allocate a free Nbuf.\n")); + + return EFI_OUT_OF_RESOURCES; + } + + // + // Duplicate the net buffer. + // + NetbufDuplicate (RxDataWrap->Nbuf, DupNbuf, 0); + MnpFreeNbuf (MnpDeviceData, RxDataWrap->Nbuf); + RxDataWrap->Nbuf = DupNbuf; + } + + // + // All resources are OK, remove the packet from the queue. + // + NetListRemoveHead (&Instance->RcvdPacketQueue); + Instance->RcvdPacketQueueSize--; + + RxData = &RxDataWrap->RxData; + SnpMode = MnpDeviceData->Snp->Mode; + + // + // Set all the buffer pointers. + // + RxData->MediaHeader = NetbufGetByte (RxDataWrap->Nbuf, 0, NULL); + RxData->DestinationAddress = RxData->MediaHeader; + RxData->SourceAddress = (UINT8 *) RxData->MediaHeader + SnpMode->HwAddressSize; + RxData->PacketData = (UINT8 *) RxData->MediaHeader + SnpMode->MediaHeaderSize; + + // + // Insert this RxDataWrap into the delivered queue. + // + InsertTailList (&Instance->RxDeliveredPacketQueue, &RxDataWrap->WrapEntry); + + // + // Get the receive token from the RxTokenMap. + // + RxToken = NetMapRemoveHead (&Instance->RxTokenMap, NULL); + + // + // Signal this token's event. + // + RxToken->Packet.RxData = &RxDataWrap->RxData; + RxToken->Status = EFI_SUCCESS; + gBS->SignalEvent (RxToken->Event); + + return EFI_SUCCESS; +} + + +/** + Deliver the received packet for the instances belonging to the MnpServiceData. + + @param[in] MnpServiceData Pointer to the mnp service context data. + +**/ +VOID +MnpDeliverPacket ( + IN MNP_SERVICE_DATA *MnpServiceData + ) +{ + LIST_ENTRY *Entry; + MNP_INSTANCE_DATA *Instance; + + NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE); + + NET_LIST_FOR_EACH (Entry, &MnpServiceData->ChildrenList) { + Instance = NET_LIST_USER_STRUCT (Entry, MNP_INSTANCE_DATA, InstEntry); + NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE); + + // + // Try to deliver packet for this instance. + // + MnpInstanceDeliverPacket (Instance); + } +} + + +/** + Recycle the RxData and other resources used to hold and deliver the received + packet. + + @param[in] Event The event this notify function registered to. + @param[in] Context Pointer to the context data registered to the Event. + +**/ +VOID +EFIAPI +MnpRecycleRxData ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + MNP_RXDATA_WRAP *RxDataWrap; + MNP_DEVICE_DATA *MnpDeviceData; + + ASSERT (Context != NULL); + + RxDataWrap = (MNP_RXDATA_WRAP *) Context; + NET_CHECK_SIGNATURE (RxDataWrap->Instance, MNP_INSTANCE_DATA_SIGNATURE); + + ASSERT (RxDataWrap->Nbuf != NULL); + + MnpDeviceData = RxDataWrap->Instance->MnpServiceData->MnpDeviceData; + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + // + // Free this Nbuf. + // + MnpFreeNbuf (MnpDeviceData, RxDataWrap->Nbuf); + RxDataWrap->Nbuf = NULL; + + // + // Close the recycle event. + // + gBS->CloseEvent (RxDataWrap->RxData.RecycleEvent); + + // + // Remove this Wrap entry from the list. + // + RemoveEntryList (&RxDataWrap->WrapEntry); + + FreePool (RxDataWrap); +} + + +/** + Queue the received packet into instance's receive queue. + + @param[in, out] Instance Pointer to the mnp instance context data. + @param[in, out] RxDataWrap Pointer to the Wrap structure containing the + received data and other information. +**/ +VOID +MnpQueueRcvdPacket ( + IN OUT MNP_INSTANCE_DATA *Instance, + IN OUT MNP_RXDATA_WRAP *RxDataWrap + ) +{ + MNP_RXDATA_WRAP *OldRxDataWrap; + + NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE); + + // + // Check the queue size. If it exceeds the limit, drop one packet + // from the head. + // + if (Instance->RcvdPacketQueueSize == MNP_MAX_RCVD_PACKET_QUE_SIZE) { + + DEBUG ((EFI_D_WARN, "MnpQueueRcvdPacket: Drop one packet bcz queue size limit reached.\n")); + + // + // Get the oldest packet. + // + OldRxDataWrap = NET_LIST_HEAD ( + &Instance->RcvdPacketQueue, + MNP_RXDATA_WRAP, + WrapEntry + ); + + // + // Recycle this OldRxDataWrap, this entry will be removed by the callee. + // + MnpRecycleRxData (NULL, (VOID *) OldRxDataWrap); + Instance->RcvdPacketQueueSize--; + } + + // + // Update the timeout tick using the configured parameter. + // + RxDataWrap->TimeoutTick = Instance->ConfigData.ReceivedQueueTimeoutValue; + + // + // Insert this Wrap into the instance queue. + // + InsertTailList (&Instance->RcvdPacketQueue, &RxDataWrap->WrapEntry); + Instance->RcvdPacketQueueSize++; +} + + +/** + Match the received packet with the instance receive filters. + + @param[in] Instance Pointer to the mnp instance context data. + @param[in] RxData Pointer to the EFI_MANAGED_NETWORK_RECEIVE_DATA. + @param[in] GroupAddress Pointer to the GroupAddress, the GroupAddress is + non-NULL and it contains the destination multicast + mac address of the received packet if the packet + destinated to a multicast mac address. + @param[in] PktAttr The received packets attribute. + + @return The received packet matches the instance's receive filters or not. + +**/ +BOOLEAN +MnpMatchPacket ( + IN MNP_INSTANCE_DATA *Instance, + IN EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData, + IN MNP_GROUP_ADDRESS *GroupAddress OPTIONAL, + IN UINT8 PktAttr + ) +{ + EFI_MANAGED_NETWORK_CONFIG_DATA *ConfigData; + LIST_ENTRY *Entry; + MNP_GROUP_CONTROL_BLOCK *GroupCtrlBlk; + + NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE); + + ConfigData = &Instance->ConfigData; + + // + // Check the protocol type. + // + if ((ConfigData->ProtocolTypeFilter != 0) && (ConfigData->ProtocolTypeFilter != RxData->ProtocolType)) { + return FALSE; + } + + if (ConfigData->EnablePromiscuousReceive) { + // + // Always match if this instance is configured to be promiscuous. + // + return TRUE; + } + + // + // The protocol type is matched, check receive filter, include unicast and broadcast. + // + if ((Instance->ReceiveFilter & PktAttr) != 0) { + return TRUE; + } + + // + // Check multicast addresses. + // + if (ConfigData->EnableMulticastReceive && RxData->MulticastFlag) { + + ASSERT (GroupAddress != NULL); + + NET_LIST_FOR_EACH (Entry, &Instance->GroupCtrlBlkList) { + + GroupCtrlBlk = NET_LIST_USER_STRUCT (Entry, MNP_GROUP_CONTROL_BLOCK, CtrlBlkEntry); + if (GroupCtrlBlk->GroupAddress == GroupAddress) { + // + // The instance is configured to receiveing packets destinated to this + // multicast address. + // + return TRUE; + } + } + } + + // + // No match. + // + return FALSE; +} + + +/** + Analyse the received packets. + + @param[in] MnpServiceData Pointer to the mnp service context data. + @param[in] Nbuf Pointer to the net buffer holding the received + packet. + @param[in, out] RxData Pointer to the buffer used to save the analysed + result in EFI_MANAGED_NETWORK_RECEIVE_DATA. + @param[out] GroupAddress Pointer to pointer to a MNP_GROUP_ADDRESS used to + pass out the address of the multicast address the + received packet destinated to. + @param[out] PktAttr Pointer to the buffer used to save the analysed + packet attribute. + +**/ +VOID +MnpAnalysePacket ( + IN MNP_SERVICE_DATA *MnpServiceData, + IN NET_BUF *Nbuf, + IN OUT EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData, + OUT MNP_GROUP_ADDRESS **GroupAddress, + OUT UINT8 *PktAttr + ) +{ + EFI_SIMPLE_NETWORK_MODE *SnpMode; + MNP_DEVICE_DATA *MnpDeviceData; + UINT8 *BufPtr; + LIST_ENTRY *Entry; + + MnpDeviceData = MnpServiceData->MnpDeviceData; + SnpMode = MnpDeviceData->Snp->Mode; + + // + // Get the packet buffer. + // + BufPtr = NetbufGetByte (Nbuf, 0, NULL); + ASSERT (BufPtr != NULL); + + // + // Set the initial values. + // + RxData->BroadcastFlag = FALSE; + RxData->MulticastFlag = FALSE; + RxData->PromiscuousFlag = FALSE; + *PktAttr = UNICAST_PACKET; + + if (!NET_MAC_EQUAL (&SnpMode->CurrentAddress, BufPtr, SnpMode->HwAddressSize)) { + // + // This packet isn't destinated to our current mac address, it't not unicast. + // + *PktAttr = 0; + + if (NET_MAC_EQUAL (&SnpMode->BroadcastAddress, BufPtr, SnpMode->HwAddressSize)) { + // + // It's broadcast. + // + RxData->BroadcastFlag = TRUE; + *PktAttr = BROADCAST_PACKET; + } else if ((*BufPtr & 0x01) == 0x1) { + // + // It's multicast, try to match the multicast filters. + // + NET_LIST_FOR_EACH (Entry, &MnpDeviceData->GroupAddressList) { + + *GroupAddress = NET_LIST_USER_STRUCT (Entry, MNP_GROUP_ADDRESS, AddrEntry); + if (NET_MAC_EQUAL (BufPtr, &((*GroupAddress)->Address), SnpMode->HwAddressSize)) { + RxData->MulticastFlag = TRUE; + break; + } + } + + if (!RxData->MulticastFlag) { + // + // No match, set GroupAddress to NULL. This multicast packet must + // be the result of PROMISUCOUS or PROMISUCOUS_MULTICAST flag is on. + // + *GroupAddress = NULL; + RxData->PromiscuousFlag = TRUE; + + if (MnpDeviceData->PromiscuousCount == 0) { + // + // Skip the below code, there is no receiver of this packet. + // + return ; + } + } + } else { + RxData->PromiscuousFlag = TRUE; + } + } + + ZeroMem (&RxData->Timestamp, sizeof (EFI_TIME)); + + // + // Fill the common parts of RxData. + // + RxData->PacketLength = Nbuf->TotalSize; + RxData->HeaderLength = SnpMode->MediaHeaderSize; + RxData->AddressLength = SnpMode->HwAddressSize; + RxData->DataLength = RxData->PacketLength - RxData->HeaderLength; + RxData->ProtocolType = NTOHS (*(UINT16 *) (BufPtr + 2 * SnpMode->HwAddressSize)); +} + + +/** + Wrap the RxData. + + @param[in] Instance Pointer to the mnp instance context data. + @param[in] RxData Pointer to the receive data to wrap. + + @return Pointer to a MNP_RXDATA_WRAP which wraps the RxData. + +**/ +MNP_RXDATA_WRAP * +MnpWrapRxData ( + IN MNP_INSTANCE_DATA *Instance, + IN EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData + ) +{ + EFI_STATUS Status; + MNP_RXDATA_WRAP *RxDataWrap; + + // + // Allocate memory. + // + RxDataWrap = AllocatePool (sizeof (MNP_RXDATA_WRAP)); + if (RxDataWrap == NULL) { + DEBUG ((EFI_D_ERROR, "MnpDispatchPacket: Failed to allocate a MNP_RXDATA_WRAP.\n")); + return NULL; + } + + RxDataWrap->Instance = Instance; + + // + // Fill the RxData in RxDataWrap, + // + CopyMem (&RxDataWrap->RxData, RxData, sizeof (RxDataWrap->RxData)); + + // + // Create the recycle event. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + MnpRecycleRxData, + RxDataWrap, + &RxDataWrap->RxData.RecycleEvent + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MnpDispatchPacket: gBS->CreateEvent failed, %r.\n", Status)); + + FreePool (RxDataWrap); + return NULL; + } + + return RxDataWrap; +} + + +/** + Enqueue the received the packets to the instances belonging to the + MnpServiceData. + + @param[in] MnpServiceData Pointer to the mnp service context data. + @param[in] Nbuf Pointer to the net buffer representing the received + packet. + +**/ +VOID +MnpEnqueuePacket ( + IN MNP_SERVICE_DATA *MnpServiceData, + IN NET_BUF *Nbuf + ) +{ + LIST_ENTRY *Entry; + MNP_INSTANCE_DATA *Instance; + EFI_MANAGED_NETWORK_RECEIVE_DATA RxData; + UINT8 PktAttr; + MNP_GROUP_ADDRESS *GroupAddress; + MNP_RXDATA_WRAP *RxDataWrap; + + + GroupAddress = NULL; + // + // First, analyse the packet header. + // + MnpAnalysePacket (MnpServiceData, Nbuf, &RxData, &GroupAddress, &PktAttr); + + if (RxData.PromiscuousFlag && (MnpServiceData->MnpDeviceData->PromiscuousCount == 0)) { + // + // No receivers, no more action need. + // + return ; + } + + // + // Iterate the children to find match. + // + NET_LIST_FOR_EACH (Entry, &MnpServiceData->ChildrenList) { + + Instance = NET_LIST_USER_STRUCT (Entry, MNP_INSTANCE_DATA, InstEntry); + NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE); + + if (!Instance->Configured) { + continue; + } + + // + // Check the packet against the instance receive filters. + // + if (MnpMatchPacket (Instance, &RxData, GroupAddress, PktAttr)) { + // + // Wrap the RxData. + // + RxDataWrap = MnpWrapRxData (Instance, &RxData); + if (RxDataWrap == NULL) { + continue; + } + + // + // Associate RxDataWrap with Nbuf and increase the RefCnt. + // + RxDataWrap->Nbuf = Nbuf; + NET_GET_REF (RxDataWrap->Nbuf); + + // + // Queue the packet into the instance queue. + // + MnpQueueRcvdPacket (Instance, RxDataWrap); + } + } +} + + +/** + Try to receive a packet and deliver it. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + + @retval EFI_SUCCESS add return value to function comment + @retval EFI_NOT_STARTED The simple network protocol is not started. + @retval EFI_NOT_READY No packet received. + @retval EFI_DEVICE_ERROR An unexpected error occurs. + +**/ +EFI_STATUS +MnpReceivePacket ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData + ) +{ + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + NET_BUF *Nbuf; + UINT8 *BufPtr; + UINTN BufLen; + UINTN HeaderSize; + UINT32 Trimmed; + MNP_SERVICE_DATA *MnpServiceData; + UINT16 VlanId; + BOOLEAN IsVlanPacket; + + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + Snp = MnpDeviceData->Snp; + if (Snp->Mode->State != EfiSimpleNetworkInitialized) { + // + // The simple network protocol is not started. + // + return EFI_NOT_STARTED; + } + + if (MnpDeviceData->RxNbufCache == NULL) { + // + // Try to get a new buffer as there may be buffers recycled. + // + MnpDeviceData->RxNbufCache = MnpAllocNbuf (MnpDeviceData); + + if (MnpDeviceData->RxNbufCache == NULL) { + // + // No available buffer in the buffer pool. + // + return EFI_DEVICE_ERROR; + } + + NetbufAllocSpace ( + MnpDeviceData->RxNbufCache, + MnpDeviceData->BufferLength, + NET_BUF_TAIL + ); + } + + Nbuf = MnpDeviceData->RxNbufCache; + BufLen = Nbuf->TotalSize; + BufPtr = NetbufGetByte (Nbuf, 0, NULL); + ASSERT (BufPtr != NULL); + + // + // Receive packet through Snp. + // + Status = Snp->Receive (Snp, &HeaderSize, &BufLen, BufPtr, NULL, NULL, NULL); + if (EFI_ERROR (Status)) { + DEBUG_CODE ( + if (Status != EFI_NOT_READY) { + DEBUG ((EFI_D_WARN, "MnpReceivePacket: Snp->Receive() = %r.\n", Status)); + } + ); + + return Status; + } + + // + // Sanity check. + // + if ((HeaderSize != Snp->Mode->MediaHeaderSize) || (BufLen < HeaderSize)) { + DEBUG ( + (EFI_D_WARN, + "MnpReceivePacket: Size error, HL:TL = %d:%d.\n", + HeaderSize, + BufLen) + ); + return EFI_DEVICE_ERROR; + } + + Trimmed = 0; + if (Nbuf->TotalSize != BufLen) { + // + // Trim the packet from tail. + // + Trimmed = NetbufTrim (Nbuf, Nbuf->TotalSize - (UINT32) BufLen, NET_BUF_TAIL); + ASSERT (Nbuf->TotalSize == BufLen); + } + + VlanId = 0; + if (MnpDeviceData->NumberOfVlan != 0) { + // + // VLAN is configured, remove the VLAN tag if any + // + IsVlanPacket = MnpRemoveVlanTag (MnpDeviceData, Nbuf, &VlanId); + } else { + IsVlanPacket = FALSE; + } + + MnpServiceData = MnpFindServiceData (MnpDeviceData, VlanId); + if (MnpServiceData == NULL) { + // + // VLAN is not set for this tagged frame, ignore this packet + // + if (Trimmed > 0) { + NetbufAllocSpace (Nbuf, Trimmed, NET_BUF_TAIL); + } + + if (IsVlanPacket) { + NetbufAllocSpace (Nbuf, NET_VLAN_TAG_LEN, NET_BUF_HEAD); + } + + goto EXIT; + } + + // + // Enqueue the packet to the matched instances. + // + MnpEnqueuePacket (MnpServiceData, Nbuf); + + if (Nbuf->RefCnt > 2) { + // + // RefCnt > 2 indicates there is at least one receiver of this packet. + // Free the current RxNbufCache and allocate a new one. + // + MnpFreeNbuf (MnpDeviceData, Nbuf); + + Nbuf = MnpAllocNbuf (MnpDeviceData); + MnpDeviceData->RxNbufCache = Nbuf; + if (Nbuf == NULL) { + DEBUG ((EFI_D_ERROR, "MnpReceivePacket: Alloc packet for receiving cache failed.\n")); + return EFI_DEVICE_ERROR; + } + + NetbufAllocSpace (Nbuf, MnpDeviceData->BufferLength, NET_BUF_TAIL); + } else { + // + // No receiver for this packet. + // + if (Trimmed > 0) { + NetbufAllocSpace (Nbuf, Trimmed, NET_BUF_TAIL); + } + if (IsVlanPacket) { + NetbufAllocSpace (Nbuf, NET_VLAN_TAG_LEN, NET_BUF_HEAD); + } + + goto EXIT; + } + // + // Deliver the queued packets. + // + MnpDeliverPacket (MnpServiceData); + +EXIT: + + ASSERT (Nbuf->TotalSize == MnpDeviceData->BufferLength); + + return Status; +} + + +/** + Remove the received packets if timeout occurs. + + @param[in] Event The event this notify function registered to. + @param[in] Context Pointer to the context data registered to the event. + +**/ +VOID +EFIAPI +MnpCheckPacketTimeout ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + MNP_DEVICE_DATA *MnpDeviceData; + MNP_SERVICE_DATA *MnpServiceData; + LIST_ENTRY *Entry; + LIST_ENTRY *ServiceEntry; + LIST_ENTRY *RxEntry; + LIST_ENTRY *NextEntry; + MNP_INSTANCE_DATA *Instance; + MNP_RXDATA_WRAP *RxDataWrap; + EFI_TPL OldTpl; + + MnpDeviceData = (MNP_DEVICE_DATA *) Context; + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + NET_LIST_FOR_EACH (ServiceEntry, &MnpDeviceData->ServiceList) { + MnpServiceData = MNP_SERVICE_DATA_FROM_LINK (ServiceEntry); + + NET_LIST_FOR_EACH (Entry, &MnpServiceData->ChildrenList) { + + Instance = NET_LIST_USER_STRUCT (Entry, MNP_INSTANCE_DATA, InstEntry); + NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE); + + if (!Instance->Configured || (Instance->ConfigData.ReceivedQueueTimeoutValue == 0)) { + // + // This instance is not configured or there is no receive time out, + // just skip to the next instance. + // + continue; + } + + OldTpl = gBS->RaiseTPL (TPL_NOTIFY); + + NET_LIST_FOR_EACH_SAFE (RxEntry, NextEntry, &Instance->RcvdPacketQueue) { + + RxDataWrap = NET_LIST_USER_STRUCT (RxEntry, MNP_RXDATA_WRAP, WrapEntry); + + // + // TimeoutTick unit is microsecond, MNP_TIMEOUT_CHECK_INTERVAL unit is 100ns. + // + if (RxDataWrap->TimeoutTick >= (MNP_TIMEOUT_CHECK_INTERVAL / 10)) { + RxDataWrap->TimeoutTick -= (MNP_TIMEOUT_CHECK_INTERVAL / 10); + } else { + // + // Drop the timeout packet. + // + DEBUG ((EFI_D_WARN, "MnpCheckPacketTimeout: Received packet timeout.\n")); + MnpRecycleRxData (NULL, RxDataWrap); + Instance->RcvdPacketQueueSize--; + } + } + + gBS->RestoreTPL (OldTpl); + } + } +} + +/** + Poll to update MediaPresent field in SNP ModeData by Snp->GetStatus(). + + @param[in] Event The event this notify function registered to. + @param[in] Context Pointer to the context data registered to the event. + +**/ +VOID +EFIAPI +MnpCheckMediaStatus ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + MNP_DEVICE_DATA *MnpDeviceData; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + UINT32 InterruptStatus; + + MnpDeviceData = (MNP_DEVICE_DATA *) Context; + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + Snp = MnpDeviceData->Snp; + if (Snp->Mode->MediaPresentSupported) { + // + // Upon successful return of GetStatus(), the MediaPresent field of + // EFI_SIMPLE_NETWORK_MODE will be updated to reflect any change of media status + // + Snp->GetStatus (Snp, &InterruptStatus, NULL); + } +} + +/** + Poll to receive the packets from Snp. This function is either called by upperlayer + protocols/applications or the system poll timer notify mechanism. + + @param[in] Event The event this notify function registered to. + @param[in] Context Pointer to the context data registered to the event. + +**/ +VOID +EFIAPI +MnpSystemPoll ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + MNP_DEVICE_DATA *MnpDeviceData; + + MnpDeviceData = (MNP_DEVICE_DATA *) Context; + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + // + // Try to receive packets from Snp. + // + MnpReceivePacket (MnpDeviceData); + + // + // Dispatch the DPC queued by the NotifyFunction of rx token's events. + // + DispatchDpc (); +} diff --git a/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpMain.c b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpMain.c new file mode 100644 index 00000000..1dc3ca6b --- /dev/null +++ b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpMain.c @@ -0,0 +1,789 @@ +/** @file + Implementation of Managed Network Protocol public services. + +Copyright (c) 2005 - 2016, Intel Corporation. All rights reserved.<BR> +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "MnpImpl.h" + +/** + Returns the operational parameters for the current MNP child driver. May also + support returning the underlying SNP driver mode data. + + The GetModeData() function is used to read the current mode data (operational + parameters) from the MNP or the underlying SNP. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[out] MnpConfigData Pointer to storage for MNP operational parameters. Type + EFI_MANAGED_NETWORK_CONFIG_DATA is defined in "Related + Definitions" below. + @param[out] SnpModeData Pointer to storage for SNP operational parameters. This + feature may be unsupported. Type EFI_SIMPLE_NETWORK_MODE + is defined in the EFI_SIMPLE_NETWORK_PROTOCOL. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_UNSUPPORTED The requested feature is unsupported in this + MNP implementation. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. The default values are returned in + MnpConfigData if it is not NULL. + @retval Others The mode data could not be read. + +**/ +EFI_STATUS +EFIAPI +MnpGetModeData ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL, + OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL + ) +{ + MNP_INSTANCE_DATA *Instance; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + EFI_TPL OldTpl; + EFI_STATUS Status; + UINT32 InterruptStatus; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = MNP_INSTANCE_DATA_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (MnpConfigData != NULL) { + // + // Copy the instance configuration data. + // + CopyMem (MnpConfigData, &Instance->ConfigData, sizeof (*MnpConfigData)); + } + + if (SnpModeData != NULL) { + // + // Copy the underlayer Snp mode data. + // + Snp = Instance->MnpServiceData->MnpDeviceData->Snp; + + // + // Upon successful return of GetStatus(), the Snp->Mode->MediaPresent + // will be updated to reflect any change of media status + // + Snp->GetStatus (Snp, &InterruptStatus, NULL); + CopyMem (SnpModeData, Snp->Mode, sizeof (*SnpModeData)); + } + + if (!Instance->Configured) { + Status = EFI_NOT_STARTED; + } else { + Status = EFI_SUCCESS; + } + + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + Sets or clears the operational parameters for the MNP child driver. + + The Configure() function is used to set, change, or reset the operational + parameters for the MNP child driver instance. Until the operational parameters + have been set, no network traffic can be sent or received by this MNP child + driver instance. Once the operational parameters have been reset, no more + traffic can be sent or received until the operational parameters have been set + again. + Each MNP child driver instance can be started and stopped independently of + each other by setting or resetting their receive filter settings with the + Configure() function. + After any successful call to Configure(), the MNP child driver instance is + started. The internal periodic timer (if supported) is enabled. Data can be + transmitted and may be received if the receive filters have also been enabled. + Note: If multiple MNP child driver instances will receive the same packet + because of overlapping receive filter settings, then the first MNP child + driver instance will receive the original packet and additional instances will + receive copies of the original packet. + Note: Warning: Receive filter settings that overlap will consume extra + processor and/or DMA resources and degrade system and network performance. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] MnpConfigData Pointer to configuration data that will be assigned + to the MNP child driver instance. If NULL, the MNP + child driver instance is reset to startup defaults + and all pending transmit and receive requests are + flushed. Type EFI_MANAGED_NETWORK_CONFIG_DATA is + defined in EFI_MANAGED_NETWORK_PROTOCOL.GetModeData(). + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is + TRUE: + * This is NULL. + * MnpConfigData.ProtocolTypeFilter is not + valid. + The operational data for the MNP child driver + instance is unchanged. + @retval EFI_OUT_OF_RESOURCES Required system resources (usually memory) + could not be allocated. + The MNP child driver instance has been reset to + startup defaults. + @retval EFI_UNSUPPORTED The requested feature is unsupported in + this [MNP] implementation. The operational data + for the MNP child driver instance is unchanged. + @retval EFI_DEVICE_ERROR An unexpected network or system error + occurred. The MNP child driver instance has + been reset to startup defaults. + @retval Others The MNP child driver instance has been reset to + startup defaults. + +**/ +EFI_STATUS +EFIAPI +MnpConfigure ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL + ) +{ + MNP_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + EFI_STATUS Status; + + if ((This == NULL) || + ((MnpConfigData != NULL) && + (MnpConfigData->ProtocolTypeFilter > 0) && + (MnpConfigData->ProtocolTypeFilter <= 1500)) + ) { + return EFI_INVALID_PARAMETER; + } + + Instance = MNP_INSTANCE_DATA_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if ((MnpConfigData == NULL) && (!Instance->Configured)) { + // + // If the instance is not configured and a reset is requested, just return. + // + Status = EFI_SUCCESS; + goto ON_EXIT; + } + + // + // Configure the instance. + // + Status = MnpConfigureInstance (Instance, MnpConfigData); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + Translates an IP multicast address to a hardware (MAC) multicast address. This + function may be unsupported in some MNP implementations. + + The McastIpToMac() function translates an IP multicast address to a hardware + (MAC) multicast address. This function may be implemented by calling the + underlying EFI_SIMPLE_NETWORK. MCastIpToMac() function, which may also be + unsupported in some MNP implementations. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] Ipv6Flag Set to TRUE to if IpAddress is an IPv6 multicast address. + Set to FALSE if IpAddress is an IPv4 multicast address. + @param[in] IpAddress Pointer to the multicast IP address (in network byte + order) to convert. + @param[out] MacAddress Pointer to the resulting multicast MAC address. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One of the following conditions is TRUE: + * This is NULL. + * IpAddress is NULL. + * IpAddress is not a valid multicast IP + address. + * MacAddress is NULL. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_UNSUPPORTED The requested feature is unsupported in this + MNP implementation. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred. + @retval Others The address could not be converted. +**/ +EFI_STATUS +EFIAPI +MnpMcastIpToMac ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN BOOLEAN Ipv6Flag, + IN EFI_IP_ADDRESS *IpAddress, + OUT EFI_MAC_ADDRESS *MacAddress + ) +{ + EFI_STATUS Status; + MNP_INSTANCE_DATA *Instance; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + EFI_TPL OldTpl; + EFI_IPv6_ADDRESS *Ip6Address; + + if ((This == NULL) || (IpAddress == NULL) || (MacAddress == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Ip6Address = &IpAddress->v6; + + if ((Ipv6Flag && !IP6_IS_MULTICAST (Ip6Address)) || + (!Ipv6Flag && !IP4_IS_MULTICAST (EFI_NTOHL (*IpAddress))) + ) { + // + // The IP address passed in is not a multicast address. + // + return EFI_INVALID_PARAMETER; + } + + Instance = MNP_INSTANCE_DATA_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (!Instance->Configured) { + + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + Snp = Instance->MnpServiceData->MnpDeviceData->Snp; + ASSERT (Snp != NULL); + + ZeroMem (MacAddress, sizeof (EFI_MAC_ADDRESS)); + + if (Snp->Mode->IfType == NET_IFTYPE_ETHERNET) { + if (!Ipv6Flag) { + // + // Translate the IPv4 address into a multicast MAC address if the NIC is an + // ethernet NIC according to RFC1112.. + // + MacAddress->Addr[0] = 0x01; + MacAddress->Addr[1] = 0x00; + MacAddress->Addr[2] = 0x5E; + MacAddress->Addr[3] = (UINT8) (IpAddress->v4.Addr[1] & 0x7F); + MacAddress->Addr[4] = IpAddress->v4.Addr[2]; + MacAddress->Addr[5] = IpAddress->v4.Addr[3]; + } else { + // + // Translate the IPv6 address into a multicast MAC address if the NIC is an + // ethernet NIC according to RFC2464. + // + + MacAddress->Addr[0] = 0x33; + MacAddress->Addr[1] = 0x33; + MacAddress->Addr[2] = Ip6Address->Addr[12]; + MacAddress->Addr[3] = Ip6Address->Addr[13]; + MacAddress->Addr[4] = Ip6Address->Addr[14]; + MacAddress->Addr[5] = Ip6Address->Addr[15]; + } + + Status = EFI_SUCCESS; + } else { + // + // Invoke Snp to translate the multicast IP address. + // + Status = Snp->MCastIpToMac ( + Snp, + Ipv6Flag, + IpAddress, + MacAddress + ); + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} + +/** + Enables and disables receive filters for multicast address. This function may + be unsupported in some MNP implementations. + + The Groups() function only adds and removes multicast MAC addresses from the + filter list. The MNP driver does not transmit or process Internet Group + Management Protocol (IGMP) packets. If JoinFlag is FALSE and MacAddress is + NULL, then all joined groups are left. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] JoinFlag Set to TRUE to join this multicast group. + Set to FALSE to leave this multicast group. + @param[in] MacAddress Pointer to the multicast MAC group (address) to join or + leave. + + @retval EFI_SUCCESS The requested operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + * This is NULL. + * JoinFlag is TRUE and MacAddress is NULL. + * MacAddress is not a valid multicast MAC + address. + * The MNP multicast group settings are + unchanged. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_ALREADY_STARTED The supplied multicast group is already joined. + @retval EFI_NOT_FOUND The supplied multicast group is not joined. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred. + The MNP child driver instance has been reset to + startup defaults. + @retval EFI_UNSUPPORTED The requested feature is unsupported in this MNP + implementation. + @retval Others The requested operation could not be completed. + The MNP multicast group settings are unchanged. + +**/ +EFI_STATUS +EFIAPI +MnpGroups ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN BOOLEAN JoinFlag, + IN EFI_MAC_ADDRESS *MacAddress OPTIONAL + ) +{ + MNP_INSTANCE_DATA *Instance; + EFI_SIMPLE_NETWORK_MODE *SnpMode; + MNP_GROUP_CONTROL_BLOCK *GroupCtrlBlk; + MNP_GROUP_ADDRESS *GroupAddress; + LIST_ENTRY *ListEntry; + BOOLEAN AddressExist; + EFI_TPL OldTpl; + EFI_STATUS Status; + + if (This == NULL || (JoinFlag && (MacAddress == NULL))) { + // + // This is NULL, or it's a join operation but MacAddress is NULL. + // + return EFI_INVALID_PARAMETER; + } + + Instance = MNP_INSTANCE_DATA_FROM_THIS (This); + SnpMode = Instance->MnpServiceData->MnpDeviceData->Snp->Mode; + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (!Instance->Configured) { + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + if ((!Instance->ConfigData.EnableMulticastReceive) || + ((MacAddress != NULL) && !NET_MAC_IS_MULTICAST (MacAddress, &SnpMode->BroadcastAddress, SnpMode->HwAddressSize))) { + // + // The instance isn't configured to do multicast receive. OR + // the passed in MacAddress is not a multicast mac address. + // + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + Status = EFI_SUCCESS; + AddressExist = FALSE; + GroupCtrlBlk = NULL; + + if (MacAddress != NULL) { + // + // Search the instance's GroupCtrlBlkList to find the specific address. + // + NET_LIST_FOR_EACH (ListEntry, &Instance->GroupCtrlBlkList) { + + GroupCtrlBlk = NET_LIST_USER_STRUCT ( + ListEntry, + MNP_GROUP_CONTROL_BLOCK, + CtrlBlkEntry + ); + GroupAddress = GroupCtrlBlk->GroupAddress; + if (0 == CompareMem ( + MacAddress, + &GroupAddress->Address, + SnpMode->HwAddressSize + )) { + // + // There is already the same multicast mac address configured. + // + AddressExist = TRUE; + break; + } + } + + if (JoinFlag && AddressExist) { + // + // The multicast mac address to join already exists. + // + Status = EFI_ALREADY_STARTED; + } + + if (!JoinFlag && !AddressExist) { + // + // The multicast mac address to leave doesn't exist in this instance. + // + Status = EFI_NOT_FOUND; + } + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } else if (IsListEmpty (&Instance->GroupCtrlBlkList)) { + // + // The MacAddress is NULL and there is no configured multicast mac address, + // just return. + // + goto ON_EXIT; + } + + // + // OK, it is time to take action. + // + Status = MnpGroupOp (Instance, JoinFlag, MacAddress, GroupCtrlBlk); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} + +/** + Places asynchronous outgoing data packets into the transmit queue. + + The Transmit() function places a completion token into the transmit packet + queue. This function is always asynchronous. + The caller must fill in the Token.Event and Token.TxData fields in the + completion token, and these fields cannot be NULL. When the transmit operation + completes, the MNP updates the Token.Status field and the Token.Event is + signaled. + Note: There may be a performance penalty if the packet needs to be + defragmented before it can be transmitted by the network device. Systems in + which performance is critical should review the requirements and features of + the underlying communications device and drivers. + + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] Token Pointer to a token associated with the transmit data + descriptor. Type EFI_MANAGED_NETWORK_COMPLETION_TOKEN + is defined in "Related Definitions" below. + + @retval EFI_SUCCESS The transmit completion token was cached. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is + TRUE: + * This is NULL. + * Token is NULL. + * Token.Event is NULL. + * Token.TxData is NULL. + * Token.TxData.DestinationAddress is not + NULL and Token.TxData.HeaderLength is zero. + * Token.TxData.FragmentCount is zero. + * (Token.TxData.HeaderLength + + Token.TxData.DataLength) is not equal to the + sum of the + Token.TxData.FragmentTable[].FragmentLength + fields. + * One or more of the + Token.TxData.FragmentTable[].FragmentLength + fields is zero. + * One or more of the + Token.TxData.FragmentTable[].FragmentBufferfields + is NULL. + * Token.TxData.DataLength is greater than MTU. + @retval EFI_ACCESS_DENIED The transmit completion token is already in the + transmit queue. + @retval EFI_OUT_OF_RESOURCES The transmit data could not be queued due to a + lack of system resources (usually memory). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + The MNP child driver instance has been reset to + startup defaults. + @retval EFI_NOT_READY The transmit request could not be queued because + the transmit queue is full. + +**/ +EFI_STATUS +EFIAPI +MnpTransmit ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token + ) +{ + EFI_STATUS Status; + MNP_INSTANCE_DATA *Instance; + MNP_SERVICE_DATA *MnpServiceData; + UINT8 *PktBuf; + UINT32 PktLen; + EFI_TPL OldTpl; + + if ((This == NULL) || (Token == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Instance = MNP_INSTANCE_DATA_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (!Instance->Configured) { + + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + if (!MnpIsValidTxToken (Instance, Token)) { + // + // The Token is invalid. + // + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + MnpServiceData = Instance->MnpServiceData; + NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE); + + // + // Build the tx packet + // + Status = MnpBuildTxPacket (MnpServiceData, Token->Packet.TxData, &PktBuf, &PktLen); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // OK, send the packet synchronously. + // + Status = MnpSyncSendPacket (MnpServiceData, PktBuf, PktLen, Token); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + Places an asynchronous receiving request into the receiving queue. + + The Receive() function places a completion token into the receive packet + queue. This function is always asynchronous. + The caller must fill in the Token.Event field in the completion token, and + this field cannot be NULL. When the receive operation completes, the MNP + updates the Token.Status and Token.RxData fields and the Token.Event is + signaled. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] Token Pointer to a token associated with the receive + data descriptor. Type + EFI_MANAGED_NETWORK_COMPLETION_TOKEN is defined in + EFI_MANAGED_NETWORK_PROTOCOL.Transmit(). + + @retval EFI_SUCCESS The receive completion token was cached. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is + TRUE: + * This is NULL. + * Token is NULL. + * Token.Event is NULL + @retval EFI_OUT_OF_RESOURCES The transmit data could not be queued due to a + lack of system resources (usually memory). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + The MNP child driver instance has been reset to + startup defaults. + @retval EFI_ACCESS_DENIED The receive completion token was already in the + receive queue. + @retval EFI_NOT_READY The receive request could not be queued because + the receive queue is full. + +**/ +EFI_STATUS +EFIAPI +MnpReceive ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token + ) +{ + EFI_STATUS Status; + MNP_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + + if ((This == NULL) || (Token == NULL) || (Token->Event == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Instance = MNP_INSTANCE_DATA_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (!Instance->Configured) { + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + // + // Check whether this token(event) is already in the rx token queue. + // + Status = NetMapIterate (&Instance->RxTokenMap, MnpTokenExist, (VOID *) Token); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Insert the Token into the RxTokenMap. + // + Status = NetMapInsertTail (&Instance->RxTokenMap, (VOID *) Token, NULL); + if (!EFI_ERROR (Status)) { + // + // Try to deliver any buffered packets. + // + Status = MnpInstanceDeliverPacket (Instance); + + // + // Dispatch the DPC queued by the NotifyFunction of Token->Event. + // + DispatchDpc (); + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} + +/** + Aborts an asynchronous transmit or receive request. + + The Cancel() function is used to abort a pending transmit or receive request. + If the token is in the transmit or receive request queues, after calling this + function, Token.Status will be set to EFI_ABORTED and then Token.Event will be + signaled. If the token is not in one of the queues, which usually means that + the asynchronous operation has completed, this function will not signal the + token and EFI_NOT_FOUND is returned. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] Token Pointer to a token that has been issued by + EFI_MANAGED_NETWORK_PROTOCOL.Transmit() or + EFI_MANAGED_NETWORK_PROTOCOL.Receive(). If NULL, all + pending tokens are aborted. + + @retval EFI_SUCCESS The asynchronous I/O request was aborted and + Token.Event was signaled. When Token is NULL, + all pending requests were aborted and their + events were signaled. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O + request was not found in the transmit or + receive queue. It has either completed or was + not issued by Transmit() and Receive(). + +**/ +EFI_STATUS +EFIAPI +MnpCancel ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token OPTIONAL + ) +{ + EFI_STATUS Status; + MNP_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = MNP_INSTANCE_DATA_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (!Instance->Configured) { + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + // + // Iterate the RxTokenMap to cancel the specified Token. + // + Status = NetMapIterate (&Instance->RxTokenMap, MnpCancelTokens, (VOID *) Token); + if (Token != NULL) { + Status = (Status == EFI_ABORTED) ? EFI_SUCCESS : EFI_NOT_FOUND; + } + + // + // Dispatch the DPC queued by the NotifyFunction of the canceled token's events. + // + DispatchDpc (); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} + +/** + Polls for incoming data packets and processes outgoing data packets. + + The Poll() function can be used by network drivers and applications to + increase the rate that data packets are moved between the communications + device and the transmit and receive queues. + Normally, a periodic timer event internally calls the Poll() function. But, in + some systems, the periodic timer event may not call Poll() fast enough to + transmit and/or receive all data packets without missing packets. Drivers and + applications that are experiencing packet loss should try calling the Poll() + function more often. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The + MNP child driver instance has been reset to startup + defaults. + @retval EFI_NOT_READY No incoming or outgoing data was processed. Consider + increasing the polling rate. + @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive + queue. Consider increasing the polling rate. + +**/ +EFI_STATUS +EFIAPI +MnpPoll ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This + ) +{ + EFI_STATUS Status; + MNP_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = MNP_INSTANCE_DATA_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (!Instance->Configured) { + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + // + // Try to receive packets. + // + Status = MnpReceivePacket (Instance->MnpServiceData->MnpDeviceData); + + // + // Dispatch the DPC queued by the NotifyFunction of rx token's events. + // + DispatchDpc (); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} diff --git a/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpVlan.c b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpVlan.c new file mode 100644 index 00000000..06cc34ca --- /dev/null +++ b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpVlan.c @@ -0,0 +1,732 @@ +/** @file + VLAN Config Protocol implementation and VLAN packet process routine. + +Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR> +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "MnpImpl.h" +#include "MnpVlan.h" + +VLAN_DEVICE_PATH mVlanDevicePathTemplate = { + { + MESSAGING_DEVICE_PATH, + MSG_VLAN_DP, + { + (UINT8) (sizeof (VLAN_DEVICE_PATH)), + (UINT8) ((sizeof (VLAN_DEVICE_PATH)) >> 8) + } + }, + 0 +}; + +EFI_VLAN_CONFIG_PROTOCOL mVlanConfigProtocolTemplate = { + VlanConfigSet, + VlanConfigFind, + VlanConfigRemove +}; + + +/** + Create a child handle for the VLAN ID. + + @param[in] ImageHandle The driver image handle. + @param[in] ControllerHandle Handle of device to bind driver to. + @param[in] VlanId The VLAN ID. + @param[out] Devicepath Pointer to returned device path for child handle. + + @return The handle of VLAN child or NULL if failed to create VLAN child. + +**/ +EFI_HANDLE +MnpCreateVlanChild ( + IN EFI_HANDLE ImageHandle, + IN EFI_HANDLE ControllerHandle, + IN UINT16 VlanId, + OUT EFI_DEVICE_PATH_PROTOCOL **Devicepath OPTIONAL + ) +{ + EFI_HANDLE ChildHandle; + VLAN_DEVICE_PATH VlanNode; + EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath; + EFI_DEVICE_PATH_PROTOCOL *VlanDevicePath; + EFI_STATUS Status; + + // + // Try to get parent device path + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiDevicePathProtocolGuid, + (VOID **) &ParentDevicePath, + ImageHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return NULL; + } + + // + // Construct device path for child handle: MAC + VLAN + // + CopyMem (&VlanNode, &mVlanDevicePathTemplate, sizeof (VLAN_DEVICE_PATH)); + VlanNode.VlanId = VlanId; + VlanDevicePath = AppendDevicePathNode ( + ParentDevicePath, + (EFI_DEVICE_PATH_PROTOCOL *) &VlanNode + ); + if (VlanDevicePath == NULL) { + return NULL; + } + + // + // Create child VLAN handle by installing DevicePath protocol + // + ChildHandle = NULL; + Status = gBS->InstallMultipleProtocolInterfaces ( + &ChildHandle, + &gEfiDevicePathProtocolGuid, + VlanDevicePath, + NULL + ); + if (EFI_ERROR (Status)) { + FreePool (VlanDevicePath); + return NULL; + } + + if (Devicepath != NULL) { + *Devicepath = VlanDevicePath; + } + + return ChildHandle; +} + +/** + Remove VLAN tag from a packet. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + @param[in, out] Nbuf Pointer to the NET_BUF to remove VLAN tag. + @param[out] VlanId Pointer to the returned VLAN ID. + + @retval TRUE VLAN tag is removed from this packet. + @retval FALSE There is no VLAN tag in this packet. + +**/ +BOOLEAN +MnpRemoveVlanTag ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN OUT NET_BUF *Nbuf, + OUT UINT16 *VlanId + ) +{ + UINT8 *Packet; + UINTN ProtocolOffset; + UINT16 ProtocolType; + VLAN_TCI VlanTag; + + ProtocolOffset = MnpDeviceData->Snp->Mode->HwAddressSize * 2; + + // + // Get the packet buffer. + // + Packet = NetbufGetByte (Nbuf, 0, NULL); + ASSERT (Packet != NULL); + + // + // Check whether this is VLAN tagged frame by Ether Type + // + *VlanId = 0; + ProtocolType = NTOHS (*(UINT16 *) (Packet + ProtocolOffset)); + if (ProtocolType != ETHER_TYPE_VLAN) { + // + // Not a VLAN tagged frame + // + return FALSE; + } + + VlanTag.Uint16 = NTOHS (*(UINT16 *) (Packet + ProtocolOffset + sizeof (ProtocolType))); + *VlanId = VlanTag.Bits.Vid; + + // + // Move hardware address (DA + SA) 4 bytes right to override VLAN tag + // + CopyMem (Packet + NET_VLAN_TAG_LEN, Packet, ProtocolOffset); + + // + // Remove VLAN tag from the Nbuf + // + NetbufTrim (Nbuf, NET_VLAN_TAG_LEN, NET_BUF_HEAD); + + return TRUE; +} + + +/** + Build the vlan packet to transmit from the TxData passed in. + + @param MnpServiceData Pointer to the mnp service context data. + @param TxData Pointer to the transmit data containing the + information to build the packet. + @param ProtocolType Pointer to the Ethernet protocol type. + @param Packet Pointer to record the address of the packet. + @param Length Pointer to a UINT32 variable used to record the + packet's length. + +**/ +VOID +MnpInsertVlanTag ( + IN MNP_SERVICE_DATA *MnpServiceData, + IN EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData, + OUT UINT16 *ProtocolType, + IN OUT UINT8 **Packet, + IN OUT UINT32 *Length + ) +{ + VLAN_TCI *VlanTci; + UINT16 *Tpid; + UINT16 *EtherType; + MNP_DEVICE_DATA *MnpDeviceData; + EFI_SIMPLE_NETWORK_MODE *SnpMode; + + MnpDeviceData = MnpServiceData->MnpDeviceData; + SnpMode = MnpDeviceData->Snp->Mode; + + *ProtocolType = ETHER_TYPE_VLAN; + *Length = *Length + NET_VLAN_TAG_LEN; + *Packet = *Packet - NET_VLAN_TAG_LEN; + + Tpid = (UINT16 *) (*Packet + SnpMode->MediaHeaderSize - sizeof (*ProtocolType)); + VlanTci = (VLAN_TCI *) (UINTN) (Tpid + 1); + if (TxData->HeaderLength != 0) { + // + // Media header is in packet, move DA+SA 4 bytes left + // + CopyMem ( + *Packet, + *Packet + NET_VLAN_TAG_LEN, + SnpMode->MediaHeaderSize - sizeof (*ProtocolType) + ); + *Tpid = HTONS (ETHER_TYPE_VLAN); + } else { + // + // Media header not in packet, VLAN TCI and original protocol type becomes payload + // + EtherType = (UINT16 *) (UINTN) (VlanTci + 1); + *EtherType = HTONS (TxData->ProtocolType); + } + + VlanTci->Bits.Vid = MnpServiceData->VlanId; + VlanTci->Bits.Cfi = VLAN_TCI_CFI_CANONICAL_MAC; + VlanTci->Bits.Priority = MnpServiceData->Priority; + VlanTci->Uint16 = HTONS (VlanTci->Uint16); +} + +/** + Check VLAN configuration variable and delete the duplicative content if has identical Vlan ID. + + @param[in] MnpDeviceData Pointer to the MNP device context data. + @param[in] Buffer Pointer to the buffer contains the array of VLAN_TCI. + @param[in] NumberOfVlan Pointer to number of VLAN. + @param[out] NewNumberOfVlan Pointer to number of unique VLAN. + + @retval EFI_SUCCESS The VLAN variable is successfully checked. + @retval EFI_OUT_OF_RESOURCES There is not enough resource to set the configuration. + +**/ +EFI_STATUS +MnpCheckVlanVariable ( + IN MNP_DEVICE_DATA *MnpDeviceData, + IN VLAN_TCI *Buffer, + IN UINTN NumberOfVlan, + OUT UINTN *NewNumberOfVlan + ) +{ + UINTN Index; + UINTN Index2; + UINTN Count; + BOOLEAN FoundDuplicateItem; + EFI_STATUS Status; + + Count = 0; + FoundDuplicateItem = FALSE; + Status = EFI_SUCCESS; + + for (Index = 0; Index < NumberOfVlan; Index++) { + for (Index2 = Index + 1; Index2 < NumberOfVlan; Index2++) { + if (Buffer[Index].Bits.Vid == Buffer[Index2].Bits.Vid) { + FoundDuplicateItem = TRUE; + Count++; + break; + } + } + if (FoundDuplicateItem) { + for (Index2 = Index +1; Index2 < NumberOfVlan; Index++, Index2++) { + CopyMem (Buffer + Index, Buffer + Index2, sizeof (VLAN_TCI)); + } + } + FoundDuplicateItem = FALSE; + } + + *NewNumberOfVlan = NumberOfVlan - Count; + if (Count != 0) { + Status = MnpSetVlanVariable (MnpDeviceData, *NewNumberOfVlan, Buffer); + } + + return Status; +} + +/** + Get VLAN configuration variable. + + @param[in] MnpDeviceData Pointer to the MNP device context data. + @param[out] NumberOfVlan Pointer to number of VLAN to be returned. + @param[out] VlanVariable Pointer to the buffer to return requested + array of VLAN_TCI. + + @retval EFI_SUCCESS The array of VLAN_TCI was returned in VlanVariable + and number of VLAN was returned in NumberOfVlan. + @retval EFI_NOT_FOUND VLAN configuration variable not found. + @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the configuration. + +**/ +EFI_STATUS +MnpGetVlanVariable ( + IN MNP_DEVICE_DATA *MnpDeviceData, + OUT UINTN *NumberOfVlan, + OUT VLAN_TCI **VlanVariable + ) +{ + UINTN BufferSize; + EFI_STATUS Status; + VLAN_TCI *Buffer; + UINTN NewNumberOfVlan; + + // + // Get VLAN configuration from EFI Variable + // + Buffer = NULL; + BufferSize = 0; + Status = gRT->GetVariable ( + MnpDeviceData->MacString, + &gEfiVlanConfigProtocolGuid, + NULL, + &BufferSize, + NULL + ); + if (Status != EFI_BUFFER_TOO_SMALL) { + return EFI_NOT_FOUND; + } + + // + // Allocate buffer to read the variable + // + Buffer = AllocateZeroPool (BufferSize); + if (Buffer == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = gRT->GetVariable ( + MnpDeviceData->MacString, + &gEfiVlanConfigProtocolGuid, + NULL, + &BufferSize, + Buffer + ); + if (EFI_ERROR (Status)) { + FreePool (Buffer); + return Status; + } + + Status = MnpCheckVlanVariable (MnpDeviceData, Buffer, BufferSize / sizeof (VLAN_TCI), &NewNumberOfVlan); + if (!EFI_ERROR (Status)) { + *NumberOfVlan = NewNumberOfVlan; + *VlanVariable = Buffer; + } + + return Status; +} + +/** + Set VLAN configuration variable. + + @param[in] MnpDeviceData Pointer to the MNP device context data. + @param[in] NumberOfVlan Number of VLAN in array VlanVariable. + @param[in] VlanVariable Pointer to array of VLAN_TCI. + + @retval EFI_SUCCESS The VLAN variable is successfully set. + @retval EFI_OUT_OF_RESOURCES There is not enough resource to set the configuration. + +**/ +EFI_STATUS +MnpSetVlanVariable ( + IN MNP_DEVICE_DATA *MnpDeviceData, + IN UINTN NumberOfVlan, + IN VLAN_TCI *VlanVariable + ) +{ + return gRT->SetVariable ( + MnpDeviceData->MacString, + &gEfiVlanConfigProtocolGuid, + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS, + NumberOfVlan * sizeof (VLAN_TCI), + VlanVariable + ); +} + + +/** + Create a VLAN device or modify the configuration parameter of an + already-configured VLAN. + + The Set() function is used to create a new VLAN device or change the VLAN + configuration parameters. If the VlanId hasn't been configured in the + physical Ethernet device, a new VLAN device will be created. If a VLAN with + this VlanId is already configured, then related configuration will be updated + as the input parameters. + + If VlanId is zero, the VLAN device will send and receive untagged frames. + Otherwise, the VLAN device will send and receive VLAN-tagged frames containing the VlanId. + If VlanId is out of scope of (0-4094), EFI_INVALID_PARAMETER is returned. + If Priority is out of the scope of (0-7), then EFI_INVALID_PARAMETER is returned. + If there is not enough system memory to perform the registration, then + EFI_OUT_OF_RESOURCES is returned. + + @param[in] This Points to the EFI_VLAN_CONFIG_PROTOCOL. + @param[in] VlanId A unique identifier (1-4094) of the VLAN which is being created + or modified, or zero (0). + @param[in] Priority 3 bit priority in VLAN header. Priority 0 is default value. If + VlanId is zero (0), Priority is ignored. + + @retval EFI_SUCCESS The VLAN is successfully configured. + @retval EFI_INVALID_PARAMETER One or more of following conditions is TRUE: + - This is NULL. + - VlanId is an invalid VLAN Identifier. + - Priority is invalid. + @retval EFI_OUT_OF_RESOURCES There is not enough system memory to perform the registration. + +**/ +EFI_STATUS +EFIAPI +VlanConfigSet ( + IN EFI_VLAN_CONFIG_PROTOCOL *This, + IN UINT16 VlanId, + IN UINT8 Priority + ) +{ + EFI_STATUS Status; + MNP_DEVICE_DATA *MnpDeviceData; + MNP_SERVICE_DATA *MnpServiceData; + VLAN_TCI *OldVariable; + VLAN_TCI *NewVariable; + UINTN NumberOfVlan; + UINTN Index; + BOOLEAN IsAdd; + LIST_ENTRY *Entry; + + if ((This == NULL) || (VlanId > 4094) || (Priority > 7)) { + return EFI_INVALID_PARAMETER; + } + + IsAdd = FALSE; + MnpDeviceData = MNP_DEVICE_DATA_FROM_THIS (This); + if (MnpDeviceData->NumberOfVlan == 0) { + // + // No existing VLAN, this is the first VLAN to add + // + IsAdd = TRUE; + Entry = GetFirstNode (&MnpDeviceData->ServiceList); + MnpServiceData = MNP_SERVICE_DATA_FROM_LINK (Entry); + + if (VlanId != 0) { + // + // VlanId is not 0, need destroy the default MNP service data + // + Status = MnpDestroyServiceChild (MnpServiceData); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = MnpDestroyServiceData (MnpServiceData); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Create a new MNP service data for this VLAN + // + MnpServiceData = MnpCreateServiceData (MnpDeviceData, VlanId, Priority); + if (MnpServiceData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + } + } else { + // + // Try to find VlanId in existing VLAN list + // + MnpServiceData = MnpFindServiceData (MnpDeviceData, VlanId); + if (MnpServiceData == NULL) { + // + // VlanId not found, create a new MNP service data + // + IsAdd = TRUE; + MnpServiceData = MnpCreateServiceData (MnpDeviceData, VlanId, Priority); + if (MnpServiceData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + } + } + + MnpServiceData->VlanId = VlanId; + MnpServiceData->Priority = Priority; + if (IsAdd) { + MnpDeviceData->NumberOfVlan++; + } + + // + // Update VLAN configuration variable + // + OldVariable = NULL; + NewVariable = NULL; + NumberOfVlan = 0; + MnpGetVlanVariable (MnpDeviceData, &NumberOfVlan, &OldVariable); + + if (IsAdd) { + // + // VLAN not exist - add + // + NewVariable = AllocateZeroPool ((NumberOfVlan + 1) * sizeof (VLAN_TCI)); + if (NewVariable == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + + if (OldVariable != NULL) { + CopyMem (NewVariable, OldVariable, NumberOfVlan * sizeof (VLAN_TCI)); + } + + Index = NumberOfVlan++; + } else { + // + // VLAN already exist - update + // + for (Index = 0; Index < NumberOfVlan; Index++) { + if (OldVariable[Index].Bits.Vid == VlanId) { + break; + } + } + ASSERT (Index < NumberOfVlan); + + NewVariable = OldVariable; + OldVariable = NULL; + } + + NewVariable[Index].Bits.Vid = VlanId; + NewVariable[Index].Bits.Priority = Priority; + + Status = MnpSetVlanVariable (MnpDeviceData, NumberOfVlan, NewVariable); + FreePool (NewVariable); + +Exit: + if (OldVariable != NULL) { + FreePool (OldVariable); + } + + return Status; +} + + +/** + Find configuration information for specified VLAN or all configured VLANs. + + The Find() function is used to find the configuration information for matching + VLAN and allocate a buffer into which those entries are copied. + + @param[in] This Points to the EFI_VLAN_CONFIG_PROTOCOL. + @param[in] VlanId Pointer to VLAN identifier. Set to NULL to find all + configured VLANs. + @param[out] NumberOfVlan The number of VLANs which is found by the specified criteria. + @param[out] Entries The buffer which receive the VLAN configuration. + + @retval EFI_SUCCESS The VLAN is successfully found. + @retval EFI_INVALID_PARAMETER One or more of following conditions is TRUE: + - This is NULL. + - Specified VlanId is invalid. + @retval EFI_NOT_FOUND No matching VLAN is found. + +**/ +EFI_STATUS +EFIAPI +VlanConfigFind ( + IN EFI_VLAN_CONFIG_PROTOCOL *This, + IN UINT16 *VlanId OPTIONAL, + OUT UINT16 *NumberOfVlan, + OUT EFI_VLAN_FIND_DATA **Entries + ) +{ + MNP_DEVICE_DATA *MnpDeviceData; + MNP_SERVICE_DATA *MnpServiceData; + LIST_ENTRY *Entry; + EFI_VLAN_FIND_DATA *VlanData; + + if ((This == NULL) || (VlanId != NULL && *VlanId > 4094) || (NumberOfVlan == NULL) || (Entries == NULL)) { + return EFI_INVALID_PARAMETER; + } + + *NumberOfVlan = 0; + *Entries = NULL; + + MnpDeviceData = MNP_DEVICE_DATA_FROM_THIS (This); + if (MnpDeviceData->NumberOfVlan == 0) { + return EFI_NOT_FOUND; + } + + if (VlanId == NULL) { + // + // Return all current VLAN configuration + // + *NumberOfVlan = (UINT16) MnpDeviceData->NumberOfVlan; + VlanData = AllocateZeroPool (*NumberOfVlan * sizeof (EFI_VLAN_FIND_DATA)); + if (VlanData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + *Entries = VlanData; + NET_LIST_FOR_EACH (Entry, &MnpDeviceData->ServiceList) { + MnpServiceData = MNP_SERVICE_DATA_FROM_LINK (Entry); + + VlanData->VlanId = MnpServiceData->VlanId; + VlanData->Priority = MnpServiceData->Priority; + VlanData++; + } + + return EFI_SUCCESS; + } + + // + // VlanId is specified, try to find it in current VLAN list + // + MnpServiceData = MnpFindServiceData (MnpDeviceData, *VlanId); + if (MnpServiceData == NULL) { + return EFI_NOT_FOUND; + } + + VlanData = AllocateZeroPool (sizeof (EFI_VLAN_FIND_DATA)); + if (VlanData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + VlanData->VlanId = MnpServiceData->VlanId; + VlanData->Priority = MnpServiceData->Priority; + + *NumberOfVlan = 1; + *Entries = VlanData; + + return EFI_SUCCESS; +} + + +/** + Remove the configured VLAN device. + + The Remove() function is used to remove the specified VLAN device. + If the VlanId is out of the scope of (0-4094), EFI_INVALID_PARAMETER is returned. + If specified VLAN hasn't been previously configured, EFI_NOT_FOUND is returned. + + @param[in] This Points to the EFI_VLAN_CONFIG_PROTOCOL. + @param[in] VlanId Identifier (0-4094) of the VLAN to be removed. + + @retval EFI_SUCCESS The VLAN is successfully removed. + @retval EFI_INVALID_PARAMETER One or more of following conditions is TRUE: + - This is NULL. + - VlanId is an invalid parameter. + @retval EFI_NOT_FOUND The to-be-removed VLAN does not exist. + +**/ +EFI_STATUS +EFIAPI +VlanConfigRemove ( + IN EFI_VLAN_CONFIG_PROTOCOL *This, + IN UINT16 VlanId + ) +{ + EFI_STATUS Status; + MNP_DEVICE_DATA *MnpDeviceData; + MNP_SERVICE_DATA *MnpServiceData; + LIST_ENTRY *Entry; + VLAN_TCI *VlanVariable; + VLAN_TCI *VlanData; + + if ((This == NULL) || (VlanId > 4094)) { + return EFI_INVALID_PARAMETER; + } + + MnpDeviceData = MNP_DEVICE_DATA_FROM_THIS (This); + if (MnpDeviceData->NumberOfVlan == 0) { + return EFI_NOT_FOUND; + } + + // + // Try to find the VlanId + // + MnpServiceData = MnpFindServiceData (MnpDeviceData, VlanId); + if (MnpServiceData == NULL) { + return EFI_NOT_FOUND; + } + + MnpDeviceData->NumberOfVlan--; + + if ((VlanId != 0) || (MnpDeviceData->NumberOfVlan != 0)) { + // + // If VlanId is not 0 or VlanId is 0 and it is not the last VLAN to remove, + // destroy its MNP service data + // + Status = MnpDestroyServiceChild (MnpServiceData); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = MnpDestroyServiceData (MnpServiceData); + if (EFI_ERROR (Status)) { + return Status; + } + } + + if ((VlanId != 0) && (MnpDeviceData->NumberOfVlan == 0)) { + // + // This is the last VLAN to be removed, restore the default MNP service data + // + MnpServiceData = MnpCreateServiceData (MnpDeviceData, 0, 0); + if (MnpServiceData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + } + + // + // Update VLAN configuration variable + // + VlanVariable = NULL; + if (MnpDeviceData->NumberOfVlan != 0) { + VlanVariable = AllocatePool (MnpDeviceData->NumberOfVlan * sizeof (VLAN_TCI)); + if (VlanVariable == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + VlanData = VlanVariable; + NET_LIST_FOR_EACH (Entry, &MnpDeviceData->ServiceList) { + MnpServiceData = MNP_SERVICE_DATA_FROM_LINK (Entry); + + VlanData->Bits.Vid = MnpServiceData->VlanId; + VlanData->Bits.Priority = MnpServiceData->Priority; + VlanData++; + } + } + + Status = MnpSetVlanVariable (MnpDeviceData, MnpDeviceData->NumberOfVlan, VlanVariable); + + if (VlanVariable != NULL) { + FreePool (VlanVariable); + } + + return Status; +} diff --git a/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpVlan.h b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpVlan.h new file mode 100644 index 00000000..c5e048e0 --- /dev/null +++ b/src/VBox/Devices/EFI/Firmware/NetworkPkg/MnpDxe/MnpVlan.h @@ -0,0 +1,205 @@ +/** @file + Header file to be included by MnpVlan.c. + +Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR> +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __MNP_VLAN_H__ +#define __MNP_VLAN_H__ + +#include "MnpDriver.h" + +extern EFI_VLAN_CONFIG_PROTOCOL mVlanConfigProtocolTemplate; + + +/** + Create a child handle for the VLAN ID. + + @param[in] ImageHandle The driver image handle. + @param[in] ControllerHandle Handle of device to bind driver to. + @param[in] VlanId The VLAN ID. + @param[out] Devicepath Pointer to returned device path for child handle. + + @return The handle of VLAN child or NULL if failed to create VLAN child. + +**/ +EFI_HANDLE +MnpCreateVlanChild ( + IN EFI_HANDLE ImageHandle, + IN EFI_HANDLE ControllerHandle, + IN UINT16 VlanId, + OUT EFI_DEVICE_PATH_PROTOCOL **Devicepath OPTIONAL + ); + +/** + Remove VLAN tag of a packet. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + @param[in, out] Nbuf Pointer to the NET_BUF to remove VLAN tag. + @param[out] VlanId Pointer to the returned VLAN ID. + + @retval TRUE VLAN tag is removed from this packet. + @retval FALSE There is no VLAN tag in this packet. + +**/ +BOOLEAN +MnpRemoveVlanTag ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN OUT NET_BUF *Nbuf, + OUT UINT16 *VlanId + ); + +/** + Build the vlan packet to transmit from the TxData passed in. + + @param MnpServiceData Pointer to the mnp service context data. + @param TxData Pointer to the transmit data containing the + information to build the packet. + @param ProtocolType Pointer to the Ethernet protocol type. + @param Packet Pointer to record the address of the packet. + @param Length Pointer to a UINT32 variable used to record the + packet's length. + +**/ +VOID +MnpInsertVlanTag ( + IN MNP_SERVICE_DATA *MnpServiceData, + IN EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData, + OUT UINT16 *ProtocolType, + IN OUT UINT8 **Packet, + IN OUT UINT32 *Length + ); + +/** + Get VLAN configuration variable. + + @param[in] MnpDeviceData Pointer to the MNP device context data. + @param[out] NumberOfVlan Pointer to number of VLAN to be returned. + @param[out] VlanVariable Pointer to the buffer to return requested + array of VLAN_TCI. + + @retval EFI_SUCCESS The array of VLAN_TCI was returned in VlanVariable + and number of VLAN was returned in NumberOfVlan. + @retval EFI_NOT_FOUND VLAN configuration variable not found. + @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the configuration. + +**/ +EFI_STATUS +MnpGetVlanVariable ( + IN MNP_DEVICE_DATA *MnpDeviceData, + OUT UINTN *NumberOfVlan, + OUT VLAN_TCI **VlanVariable + ); + +/** + Set VLAN configuration variable. + + @param[in] MnpDeviceData Pointer to the MNP device context data. + @param[in] NumberOfVlan Number of VLAN in array VlanVariable. + @param[in] VlanVariable Pointer to array of VLAN_TCI. + + @retval EFI_SUCCESS The VLAN variable is successfully set. + @retval EFI_OUT_OF_RESOURCES There is not enough resource to set the configuration. + +**/ +EFI_STATUS +MnpSetVlanVariable ( + IN MNP_DEVICE_DATA *MnpDeviceData, + IN UINTN NumberOfVlan, + IN VLAN_TCI *VlanVariable + ); + +/** + Create a VLAN device or modify the configuration parameter of an + already-configured VLAN. + + The Set() function is used to create a new VLAN device or change the VLAN + configuration parameters. If the VlanId hasn't been configured in the + physical Ethernet device, a new VLAN device will be created. If a VLAN with + this VlanId is already configured, then related configuration will be updated + as the input parameters. + + If VlanId is zero, the VLAN device will send and receive untagged frames. + Otherwise, the VLAN device will send and receive VLAN-tagged frames containing the VlanId. + If VlanId is out of scope of (0-4094), EFI_INVALID_PARAMETER is returned. + If Priority is out of the scope of (0-7), then EFI_INVALID_PARAMETER is returned. + If there is not enough system memory to perform the registration, then + EFI_OUT_OF_RESOURCES is returned. + + @param[in] This Points to the EFI_VLAN_CONFIG_PROTOCOL. + @param[in] VlanId A unique identifier (1-4094) of the VLAN which is being created + or modified, or zero (0). + @param[in] Priority 3 bit priority in VLAN header. Priority 0 is default value. If + VlanId is zero (0), Priority is ignored. + + @retval EFI_SUCCESS The VLAN is successfully configured. + @retval EFI_INVALID_PARAMETER One or more of following conditions is TRUE: + - This is NULL. + - VlanId is an invalid VLAN Identifier. + - Priority is invalid. + @retval EFI_OUT_OF_RESOURCES There is not enough system memory to perform the registration. + +**/ +EFI_STATUS +EFIAPI +VlanConfigSet ( + IN EFI_VLAN_CONFIG_PROTOCOL *This, + IN UINT16 VlanId, + IN UINT8 Priority + ); + +/** + Find configuration information for specified VLAN or all configured VLANs. + + The Find() function is used to find the configuration information for matching + VLAN and allocate a buffer into which those entries are copied. + + @param[in] This Points to the EFI_VLAN_CONFIG_PROTOCOL. + @param[in] VlanId Pointer to VLAN identifier. Set to NULL to find all + configured VLANs. + @param[out] NumberOfVlan The number of VLANs which is found by the specified criteria. + @param[out] Entries The buffer which receive the VLAN configuration. + + @retval EFI_SUCCESS The VLAN is successfully found. + @retval EFI_INVALID_PARAMETER One or more of following conditions is TRUE: + - This is NULL. + - Specified VlanId is invalid. + @retval EFI_NOT_FOUND No matching VLAN is found. + +**/ +EFI_STATUS +EFIAPI +VlanConfigFind ( + IN EFI_VLAN_CONFIG_PROTOCOL *This, + IN UINT16 *VlanId OPTIONAL, + OUT UINT16 *NumberOfVlan, + OUT EFI_VLAN_FIND_DATA **Entries + ); + +/** + Remove the configured VLAN device. + + The Remove() function is used to remove the specified VLAN device. + If the VlanId is out of the scope of (0-4094), EFI_INVALID_PARAMETER is returned. + If specified VLAN hasn't been previously configured, EFI_NOT_FOUND is returned. + + @param[in] This Points to the EFI_VLAN_CONFIG_PROTOCOL. + @param[in] VlanId Identifier (0-4094) of the VLAN to be removed. + + @retval EFI_SUCCESS The VLAN is successfully removed. + @retval EFI_INVALID_PARAMETER One or more of following conditions is TRUE: + - This is NULL. + - VlanId is an invalid parameter. + @retval EFI_NOT_FOUND The to-be-removed VLAN does not exist. + +**/ +EFI_STATUS +EFIAPI +VlanConfigRemove ( + IN EFI_VLAN_CONFIG_PROTOCOL *This, + IN UINT16 VlanId + ); + +#endif |