summaryrefslogtreecommitdiffstats
path: root/MdeModulePkg/Universal/Network/MnpDxe
diff options
context:
space:
mode:
authorvanjeff <vanjeff@6f19259b-4bc3-4df7-8a09-765794883524>2007-07-24 08:06:37 +0000
committervanjeff <vanjeff@6f19259b-4bc3-4df7-8a09-765794883524>2007-07-24 08:06:37 +0000
commit8a67d61da4d5a8f08a656cbeea2d902d0ad9042a (patch)
tree6618049196a9f4a206b8d6e42fb8b67a71558503 /MdeModulePkg/Universal/Network/MnpDxe
parentf9bef4b3ac2bf3bd5f79313f772519800761f104 (diff)
downloadedk2-8a67d61da4d5a8f08a656cbeea2d902d0ad9042a.tar.gz
edk2-8a67d61da4d5a8f08a656cbeea2d902d0ad9042a.tar.bz2
edk2-8a67d61da4d5a8f08a656cbeea2d902d0ad9042a.zip
Import SnpDxe, Tcp4Dxe, Udp4Dxe and MnpDxe.
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@3416 6f19259b-4bc3-4df7-8a09-765794883524
Diffstat (limited to 'MdeModulePkg/Universal/Network/MnpDxe')
-rw-r--r--MdeModulePkg/Universal/Network/MnpDxe/ComponentName.c162
-rw-r--r--MdeModulePkg/Universal/Network/MnpDxe/MnpConfig.c1466
-rw-r--r--MdeModulePkg/Universal/Network/MnpDxe/MnpDebug.h28
-rw-r--r--MdeModulePkg/Universal/Network/MnpDxe/MnpDriver.c560
-rw-r--r--MdeModulePkg/Universal/Network/MnpDxe/MnpDriver.h136
-rw-r--r--MdeModulePkg/Universal/Network/MnpDxe/MnpDxe.inf63
-rw-r--r--MdeModulePkg/Universal/Network/MnpDxe/MnpDxe.msa70
-rw-r--r--MdeModulePkg/Universal/Network/MnpDxe/MnpImpl.h274
-rw-r--r--MdeModulePkg/Universal/Network/MnpDxe/MnpIo.c1087
-rw-r--r--MdeModulePkg/Universal/Network/MnpDxe/MnpMain.c655
10 files changed, 4501 insertions, 0 deletions
diff --git a/MdeModulePkg/Universal/Network/MnpDxe/ComponentName.c b/MdeModulePkg/Universal/Network/MnpDxe/ComponentName.c
new file mode 100644
index 0000000000..209861c775
--- /dev/null
+++ b/MdeModulePkg/Universal/Network/MnpDxe/ComponentName.c
@@ -0,0 +1,162 @@
+/** @file
+
+Copyright (c) 2005 - 2007, Intel Corporation
+All rights reserved. This program and the accompanying materials
+are licensed and made available under the terms and conditions of the BSD License
+which accompanies this distribution. The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+Module Name:
+
+ ComponentName.c
+
+Abstract:
+
+
+**/
+
+
+#include "MnpDriver.h"
+
+//
+// EFI Component Name Functions
+//
+EFI_STATUS
+EFIAPI
+MnpComponentNameGetDriverName (
+ IN EFI_COMPONENT_NAME_PROTOCOL *This,
+ IN CHAR8 *Language,
+ OUT CHAR16 **DriverName
+ );
+
+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 Component Name Protocol
+//
+EFI_COMPONENT_NAME_PROTOCOL gMnpComponentName = {
+ MnpComponentNameGetDriverName,
+ MnpComponentNameGetControllerName,
+ "eng"
+};
+
+STATIC EFI_UNICODE_STRING_TABLE mMnpDriverNameTable[] = {
+ {
+ "eng",
+ L"MNP Network Service Driver"
+ },
+ {
+ NULL,
+ NULL
+ }
+};
+
+EFI_STATUS
+EFIAPI
+MnpComponentNameGetDriverName (
+ IN EFI_COMPONENT_NAME_PROTOCOL *This,
+ IN CHAR8 *Language,
+ OUT CHAR16 **DriverName
+ )
+/*++
+
+ Routine Description:
+ Retrieves a Unicode string that is the user readable name of the EFI Driver.
+
+ Arguments:
+ This - A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance.
+ Language - A pointer to a three character ISO 639-2 language identifier.
+ This is the language of the driver name that 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.
+ 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.
+
+ Returns:
+ EFI_SUCCES - The Unicode string for the Driver specified by This
+ and the language specified by Language was returned
+ in DriverName.
+ EFI_INVALID_PARAMETER - Language is NULL.
+ EFI_INVALID_PARAMETER - DriverName is NULL.
+ EFI_UNSUPPORTED - The driver specified by This does not support the
+ language specified by Language.
+
+--*/
+{
+ return LookupUnicodeString (
+ Language,
+ gMnpComponentName.SupportedLanguages,
+ mMnpDriverNameTable,
+ DriverName
+ );
+}
+
+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
+ )
+/*++
+
+ Routine Description:
+ Retrieves a Unicode string that is the user readable name of the controller
+ that is being managed by an EFI Driver.
+
+ Arguments:
+ This - A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance.
+ 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.
+ 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.
+ Language - A pointer to a three character ISO 639-2 language
+ identifier. This is the language of the controller name
+ that 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.
+ 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.
+
+ Returns:
+ EFI_SUCCESS - The Unicode string for the user readable name in the
+ language specified by Language for the driver
+ specified by This was returned in DriverName.
+ EFI_INVALID_PARAMETER - ControllerHandle is not a valid EFI_HANDLE.
+ EFI_INVALID_PARAMETER - ChildHandle is not NULL and it is not a valid EFI_HANDLE.
+ EFI_INVALID_PARAMETER - Language is NULL.
+ EFI_INVALID_PARAMETER - ControllerName is NULL.
+ EFI_UNSUPPORTED - The driver specified by This is not currently managing
+ the controller specified by ControllerHandle and
+ ChildHandle.
+ EFI_UNSUPPORTED - The driver specified by This does not support the
+ language specified by Language.
+
+--*/
+{
+ return EFI_UNSUPPORTED;
+}
diff --git a/MdeModulePkg/Universal/Network/MnpDxe/MnpConfig.c b/MdeModulePkg/Universal/Network/MnpDxe/MnpConfig.c
new file mode 100644
index 0000000000..c842991f30
--- /dev/null
+++ b/MdeModulePkg/Universal/Network/MnpDxe/MnpConfig.c
@@ -0,0 +1,1466 @@
+/** @file
+
+Copyright (c) 2005 - 2007, Intel Corporation
+All rights reserved. This program and the accompanying materials
+are licensed and made available under the terms and conditions of the BSD License
+which accompanies this distribution. The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+Module Name:
+
+ MnpConfig.c
+
+Abstract:
+
+ Implementation of Managed Network Protocol private services.
+
+
+**/
+
+
+#include "MnpImpl.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 = {
+ 10000,
+ 10000,
+ 0,
+ FALSE,
+ FALSE,
+ FALSE,
+ FALSE,
+ FALSE,
+ FALSE,
+ FALSE
+};
+
+STATIC
+EFI_STATUS
+MnpAddFreeNbuf (
+ IN MNP_SERVICE_DATA *MnpServiceData,
+ IN UINTN Count
+ );
+
+STATIC
+EFI_STATUS
+MnpStartSnp (
+ IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp
+ );
+
+STATIC
+EFI_STATUS
+MnpStopSnp (
+ IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp
+ );
+
+STATIC
+EFI_STATUS
+MnpStart (
+ IN MNP_SERVICE_DATA *MnpServiceData,
+ IN BOOLEAN IsConfigUpdate,
+ IN BOOLEAN EnableSystemPoll
+ );
+
+STATIC
+EFI_STATUS
+MnpStop (
+ IN MNP_SERVICE_DATA *MnpServiceData
+ );
+
+STATIC
+EFI_STATUS
+MnpConfigReceiveFilters (
+ IN MNP_SERVICE_DATA *MnpServiceData
+ );
+
+STATIC
+EFI_STATUS
+MnpGroupOpAddCtrlBlk (
+ IN MNP_INSTANCE_DATA *Instance,
+ IN MNP_GROUP_CONTROL_BLOCK *CtrlBlk,
+ IN MNP_GROUP_ADDRESS *GroupAddress OPTIONAL,
+ IN EFI_MAC_ADDRESS *MacAddress,
+ IN UINT32 HwAddressSize
+ );
+
+STATIC
+BOOLEAN
+MnpGroupOpDelCtrlBlk (
+ IN MNP_INSTANCE_DATA *Instance,
+ IN MNP_GROUP_CONTROL_BLOCK *CtrlBlk
+ );
+
+
+/**
+ Add some NET_BUF into MnpServiceData->FreeNbufQue. The buffer length of
+ the NET_BUF is specified by MnpServiceData->BufferLength.
+
+ @param MnpServiceData Pointer to the MNP_SERVICE_DATA.
+ @param Count Number of NET_BUFFERs to add.
+
+ @retval EFI_SUCCESS The specified amount of NET_BUFs are allocated and
+ added into MnpServiceData->FreeNbufQue.
+ @retval EFI_OUT_OF_RESOURCES Failed to allocate a NET_BUF structure.
+
+**/
+STATIC
+EFI_STATUS
+MnpAddFreeNbuf (
+ IN MNP_SERVICE_DATA *MnpServiceData,
+ IN UINTN Count
+ )
+{
+ EFI_STATUS Status;
+ UINTN Index;
+ NET_BUF *Nbuf;
+
+ NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
+ ASSERT ((Count > 0) && (MnpServiceData->BufferLength > 0));
+
+ Status = EFI_SUCCESS;
+
+ for (Index = 0; Index < Count; Index++) {
+
+ Nbuf = NetbufAlloc (MnpServiceData->BufferLength);
+ if (Nbuf == NULL) {
+
+ MNP_DEBUG_ERROR (("MnpAddFreeNbuf: NetBufAlloc failed.\n"));
+ Status = EFI_OUT_OF_RESOURCES;
+ break;
+ }
+
+ NetbufQueAppend (&MnpServiceData->FreeNbufQue, Nbuf);
+ }
+
+ MnpServiceData->NbufCnt += Index;
+
+ return Status;
+}
+
+
+/**
+ Allocate a free NET_BUF from MnpServiceData->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 MnpServiceData Pointer to the MNP_SERVICE_DATA.
+
+ @return Pointer to the allocated free NET_BUF structure, if NULL the operation is failed.
+
+**/
+NET_BUF *
+MnpAllocNbuf (
+ IN MNP_SERVICE_DATA *MnpServiceData
+ )
+{
+ EFI_STATUS Status;
+ NET_BUF_QUEUE *FreeNbufQue;
+ NET_BUF *Nbuf;
+ EFI_TPL OldTpl;
+
+ NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
+
+ FreeNbufQue = &MnpServiceData->FreeNbufQue;
+
+ OldTpl = NET_RAISE_TPL (NET_TPL_RECYCLE);
+
+ //
+ // Check whether there are available buffers, or else try to add some.
+ //
+ if (FreeNbufQue->BufNum == 0) {
+
+ if ((MnpServiceData->NbufCnt + MNP_NET_BUFFER_INCREASEMENT) > MNP_MAX_NET_BUFFER_NUM) {
+
+ MNP_DEBUG_ERROR (
+ ("MnpAllocNbuf: The maximum NET_BUF size is reached for MNP driver instance %p.\n",
+ MnpServiceData)
+ );
+
+ Nbuf = NULL;
+ goto ON_EXIT;
+ }
+
+ Status = MnpAddFreeNbuf (MnpServiceData, MNP_NET_BUFFER_INCREASEMENT);
+ if (EFI_ERROR (Status)) {
+
+ MNP_DEBUG_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:
+ NET_RESTORE_TPL (OldTpl);
+
+ return Nbuf;
+}
+
+
+/**
+ Try to reclaim the Nbuf into the buffer pool.
+
+ @param MnpServiceData Pointer to the mnp service context data.
+ @param Nbuf Pointer to the NET_BUF to free.
+
+ @return None.
+
+**/
+VOID
+MnpFreeNbuf (
+ IN MNP_SERVICE_DATA *MnpServiceData,
+ IN NET_BUF *Nbuf
+ )
+{
+ EFI_TPL OldTpl;
+
+ NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
+ ASSERT (Nbuf->RefCnt > 1);
+
+ OldTpl = NET_RAISE_TPL (NET_TPL_RECYCLE);
+
+ 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);
+ NetbufQueAppend (&MnpServiceData->FreeNbufQue, Nbuf);
+ }
+
+ NET_RESTORE_TPL (OldTpl);
+}
+
+
+/**
+ Initialize the mnp service context data.
+
+ @param MnpServiceData Pointer to the mnp service context data.
+ @param Snp Pointer to the simple network protocol.
+
+ @retval EFI_SUCCESS The mnp service context is initialized.
+ @retval Other Some error occurs.
+
+**/
+EFI_STATUS
+MnpInitializeServiceData (
+ IN MNP_SERVICE_DATA *MnpServiceData,
+ IN EFI_HANDLE ImageHandle,
+ IN EFI_HANDLE ControllerHandle
+ )
+{
+ EFI_STATUS Status;
+ EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
+ EFI_SIMPLE_NETWORK_MODE *SnpMode;
+
+ MnpServiceData->Signature = MNP_SERVICE_DATA_SIGNATURE;
+
+ MnpServiceData->ControllerHandle = ControllerHandle;
+
+ //
+ // Copy the ServiceBinding structure.
+ //
+ MnpServiceData->ServiceBinding = mMnpServiceBindingProtocol;
+
+ //
+ // 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;
+ MnpServiceData->Snp = Snp;
+ MnpServiceData->Mtu = SnpMode->MaxPacketSize;
+
+ //
+ // Initialize the lists.
+ //
+ NetListInit (&MnpServiceData->GroupAddressList);
+ NetListInit (&MnpServiceData->ChildrenList);
+
+ //
+ // Get the buffer length used to allocate NET_BUF to hold data received
+ // from SNP. Do this before fill the FreeNetBufQue.
+ //
+ MnpServiceData->BufferLength = MnpServiceData->Mtu + SnpMode->MediaHeaderSize + NET_ETHER_FCS_SIZE;
+
+ //
+ // Initialize the FreeNetBufQue and pre-allocate some NET_BUFs.
+ //
+ NetbufQueInit (&MnpServiceData->FreeNbufQue);
+ Status = MnpAddFreeNbuf (MnpServiceData, MNP_INIT_NET_BUFFER_NUM);
+ if (EFI_ERROR (Status)) {
+
+ MNP_DEBUG_ERROR (("MnpInitializeServiceData: MnpAddFreeNbuf failed, %r.\n", Status));
+ goto ERROR;
+ }
+ //
+ // Get one NET_BUF from the FreeNbufQue for rx cache.
+ //
+ MnpServiceData->RxNbufCache = MnpAllocNbuf (MnpServiceData);
+ NetbufAllocSpace (
+ MnpServiceData->RxNbufCache,
+ MnpServiceData->BufferLength,
+ NET_BUF_TAIL
+ );
+
+ //
+ // Allocate buffer pool for tx.
+ //
+ MnpServiceData->TxBuf = NetAllocatePool (MnpServiceData->Mtu + SnpMode->MediaHeaderSize);
+ if (MnpServiceData->TxBuf == NULL) {
+
+ MNP_DEBUG_ERROR (("MnpInitializeServiceData: NetAllocatePool failed.\n"));
+ Status = EFI_OUT_OF_RESOURCES;
+
+ goto ERROR;
+ }
+
+ //
+ // Create the system poll timer.
+ //
+ Status = gBS->CreateEvent (
+ EVT_NOTIFY_SIGNAL | EVT_TIMER,
+ NET_TPL_LOCK,
+ MnpSystemPoll,
+ MnpServiceData,
+ &MnpServiceData->PollTimer
+ );
+ if (EFI_ERROR (Status)) {
+
+ MNP_DEBUG_ERROR (("MnpInitializeServiceData: CreateEvent for poll timer failed.\n"));
+ goto ERROR;
+ }
+
+ //
+ // Create the timer for packet timeout check.
+ //
+ Status = gBS->CreateEvent (
+ EVT_NOTIFY_SIGNAL | EVT_TIMER,
+ NET_TPL_EVENT,
+ MnpCheckPacketTimeout,
+ MnpServiceData,
+ &MnpServiceData->TimeoutCheckTimer
+ );
+ if (EFI_ERROR (Status)) {
+
+ MNP_DEBUG_ERROR (("MnpInitializeServiceData: CreateEvent for packet timeout check failed.\n"));
+ goto ERROR;
+ }
+
+ //
+ // Create the timer for tx timeout check.
+ //
+ Status = gBS->CreateEvent (
+ EVT_TIMER,
+ NET_TPL_SLOW_TIMER,
+ NULL,
+ NULL,
+ &MnpServiceData->TxTimeoutEvent
+ );
+ if (EFI_ERROR (Status)) {
+
+ MNP_DEBUG_ERROR (("MnpInitializeServiceData: CreateEvent for tx timeout event failed.\n"));
+ }
+
+ERROR:
+
+ if (EFI_ERROR (Status)) {
+ //
+ // Free the dynamic allocated resources if necessary.
+ //
+ if (MnpServiceData->TimeoutCheckTimer != NULL) {
+
+ gBS->CloseEvent (MnpServiceData->TimeoutCheckTimer);
+ }
+
+ if (MnpServiceData->PollTimer != NULL) {
+
+ gBS->CloseEvent (MnpServiceData->PollTimer);
+ }
+
+ if (MnpServiceData->TxBuf != NULL) {
+
+ NetFreePool (MnpServiceData->TxBuf);
+ }
+
+ if (MnpServiceData->RxNbufCache != NULL) {
+
+ MnpFreeNbuf (MnpServiceData, MnpServiceData->RxNbufCache);
+ }
+
+ if (MnpServiceData->FreeNbufQue.BufNum != 0) {
+
+ NetbufQueFlush (&MnpServiceData->FreeNbufQue);
+ }
+ }
+
+ return Status;
+}
+
+
+/**
+ Flush the mnp service context data.
+
+ @param MnpServiceData Pointer to the mnp service context data.
+
+ @return None.
+
+**/
+VOID
+MnpFlushServiceData (
+ MNP_SERVICE_DATA *MnpServiceData
+ )
+{
+ NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
+
+ //
+ // The GroupAddressList must be empty.
+ //
+ ASSERT (NetListIsEmpty (&MnpServiceData->GroupAddressList));
+
+ //
+ // Close the event.
+ //
+ gBS->CloseEvent (&MnpServiceData->TxTimeoutEvent);
+ gBS->CloseEvent (&MnpServiceData->TimeoutCheckTimer);
+ gBS->CloseEvent (&MnpServiceData->PollTimer);
+
+ //
+ // Free the tx buffer.
+ //
+ NetFreePool (MnpServiceData->TxBuf);
+
+ //
+ // Free the RxNbufCache.
+ //
+ MnpFreeNbuf (MnpServiceData, MnpServiceData->RxNbufCache);
+
+ //
+ // Flush the FreeNbufQue.
+ //
+ MnpServiceData->NbufCnt -= MnpServiceData->FreeNbufQue.BufNum;
+ NetbufQueFlush (&MnpServiceData->FreeNbufQue);
+
+ DEBUG_CODE (
+
+ if (MnpServiceData->NbufCnt != 0) {
+
+ MNP_DEBUG_WARN (("MnpFlushServiceData: Memory leak, MnpServiceData->NbufCnt != 0.\n"));
+ }
+ );
+}
+
+
+/**
+ Initialize the mnp instance context data.
+
+ @param MnpServiceData Pointer to the mnp service context data.
+ @param Instance Pointer to the mnp instance context data to
+ initialize.
+
+ @return None.
+
+**/
+VOID
+MnpInitializeInstanceData (
+ IN MNP_SERVICE_DATA *MnpServiceData,
+ IN 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.
+ //
+ Instance->ManagedNetwork = mMnpProtocolTemplate;
+
+ //
+ // Copy the default config data.
+ //
+ Instance->ConfigData = mMnpDefaultConfigData;
+
+ //
+ // Initialize the lists.
+ //
+ NetListInit (&Instance->GroupCtrlBlkList);
+ NetListInit (&Instance->RcvdPacketQueue);
+ NetListInit (&Instance->RxDeliveredPacketQueue);
+
+ //
+ // Initialize the RxToken Map.
+ //
+ NetMapInit (&Instance->RxTokenMap);
+
+ //
+ // Save the MnpServiceData info.
+ //
+ Instance->MnpServiceData = MnpServiceData;
+}
+
+
+/**
+ Check whether the token specified by Arg maches the token in Item.
+
+ @param Map Pointer to the NET_MAP.
+ @param Item Pointer to the NET_MAP_ITEM
+ @param 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
+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 Map Pointer to the NET_MAP.
+ @param Item Pointer to the NET_MAP_ITEM
+ @param 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
+MnpCancelTokens (
+ IN NET_MAP *Map,
+ IN 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;
+
+ //
+ // Cancel this token with status set to EFI_ABORTED.
+ //
+ TokenToCancel->Status = EFI_ABORTED;
+ gBS->SignalEvent (TokenToCancel->Event);
+
+ //
+ // Remove the item from the map.
+ //
+ NetMapRemoveItem (Map, Item, NULL);
+
+ 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 Snp Pointer to the simple network protocol.
+
+ @retval EFI_SUCCESS The simple network protocol is started.
+ @retval Other Some error occurs.
+
+**/
+STATIC
+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 Snp Pointer to the simple network protocol.
+
+ @retval EFI_SUCCESS The simple network is stopped.
+ @retval Other Some error occurs.
+
+**/
+STATIC
+EFI_STATUS
+MnpStopSnp (
+ IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp
+ )
+{
+ EFI_STATUS Status;
+
+ ASSERT (Snp != NULL);
+
+ //
+ // 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 MnpServiceData Pointer to the mnp service context data.
+ @param IsConfigUpdate The instance is reconfigured or it's the first time
+ the instanced is configured.
+ @param EnableSystemPoll Enable the system polling or not.
+
+ @retval EFI_SUCCESS The managed network is started and some
+ configuration is updated.
+ @retval Other Some error occurs.
+
+**/
+STATIC
+EFI_STATUS
+MnpStart (
+ IN MNP_SERVICE_DATA *MnpServiceData,
+ IN BOOLEAN IsConfigUpdate,
+ IN BOOLEAN EnableSystemPoll
+ )
+{
+ EFI_STATUS Status;
+ EFI_TIMER_DELAY TimerOpType;
+
+ NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
+
+ Status = EFI_SUCCESS;
+
+ if (!IsConfigUpdate) {
+ //
+ // If it's not a configuration update, increase the configured children number.
+ //
+ MnpServiceData->ConfiguredChildrenNumber++;
+
+ if (MnpServiceData->ConfiguredChildrenNumber == 1) {
+ //
+ // It's the first configured child, start the simple network.
+ //
+ Status = MnpStartSnp (MnpServiceData->Snp);
+ if (EFI_ERROR (Status)) {
+
+ MNP_DEBUG_ERROR (("MnpStart: MnpStartSnp failed, %r.\n", Status));
+ goto ErrorExit;
+ }
+
+ //
+ // Start the timeout timer.
+ //
+ Status = gBS->SetTimer (
+ MnpServiceData->TimeoutCheckTimer,
+ TimerPeriodic,
+ MNP_TIMEOUT_CHECK_INTERVAL
+ );
+ if (EFI_ERROR (Status)) {
+
+ MNP_DEBUG_ERROR (
+ ("MnpStart, gBS->SetTimer for TimeoutCheckTimer %r.\n",
+ Status)
+ );
+ goto ErrorExit;
+ }
+ }
+ }
+
+ if (MnpServiceData->EnableSystemPoll ^ EnableSystemPoll) {
+ //
+ // The EnableSystemPoll differs with the current state, disable or enable
+ // the system poll.
+ //
+ TimerOpType = EnableSystemPoll ? TimerPeriodic : TimerCancel;
+
+ Status = gBS->SetTimer (MnpServiceData->PollTimer, TimerOpType, MNP_SYS_POLL_INTERVAL);
+ if (EFI_ERROR (Status)) {
+
+ MNP_DEBUG_ERROR (("MnpStart: gBS->SetTimer for PollTimer failed, %r.\n", Status));
+ goto ErrorExit;
+ }
+
+ MnpServiceData->EnableSystemPoll = EnableSystemPoll;
+ }
+
+ //
+ // Change the receive filters if need.
+ //
+ Status = MnpConfigReceiveFilters (MnpServiceData);
+
+ErrorExit:
+
+ return Status;
+}
+
+
+/**
+ Stop the managed network.
+
+ @param MnpServiceData Pointer to the mnp service context data.
+
+ @retval EFI_SUCCESS The managed network is stopped.
+ @retval Other Some error occurs.
+
+**/
+STATIC
+EFI_STATUS
+MnpStop (
+ IN MNP_SERVICE_DATA *MnpServiceData
+ )
+{
+ EFI_STATUS Status;
+
+ NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
+ ASSERT (MnpServiceData->ConfiguredChildrenNumber > 0);
+
+ //
+ // Configure the receive filters.
+ //
+ MnpConfigReceiveFilters (MnpServiceData);
+
+ //
+ // Decrease the children number.
+ //
+ MnpServiceData->ConfiguredChildrenNumber--;
+
+ if (MnpServiceData->ConfiguredChildrenNumber > 0) {
+ //
+ // If there are other configured chilren, return and keep the timers and
+ // simple network unchanged.
+ //
+ return EFI_SUCCESS;
+ }
+
+ //
+ // No configured children now.
+ //
+
+ if (MnpServiceData->EnableSystemPoll) {
+ //
+ // The system poll in on, cancel the poll timer.
+ //
+ Status = gBS->SetTimer (MnpServiceData->PollTimer, TimerCancel, 0);
+ MnpServiceData->EnableSystemPoll = FALSE;
+ }
+
+ //
+ // Cancel the timeout timer.
+ //
+ Status = gBS->SetTimer (MnpServiceData->TimeoutCheckTimer, TimerCancel, 0);
+
+ //
+ // Stop the simple network.
+ //
+ Status = MnpStopSnp (MnpServiceData->Snp);
+
+ return Status;
+}
+
+
+/**
+ Flush the instance's received data.
+
+ @param Instance Pointer to the mnp instance context data.
+
+ @return None.
+
+**/
+VOID
+MnpFlushRcvdDataQueue (
+ IN MNP_INSTANCE_DATA *Instance
+ )
+{
+ EFI_TPL OldTpl;
+ MNP_RXDATA_WRAP *RxDataWrap;
+
+ NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
+
+ OldTpl = NET_RAISE_TPL (NET_TPL_RECYCLE);
+
+ while (!NetListIsEmpty (&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);
+
+ NET_RESTORE_TPL (OldTpl);
+}
+
+
+/**
+ Configure the Instance using ConfigData.
+
+ @param Instance Pointer to the mnp instance context data.
+ @param ConfigData Pointer to the configuration data used to configure
+ the isntance.
+
+ @retval EFI_SUCCESS The Instance is configured.
+ @retval EFI_UNSUPPORTED EnableReceiveTimestamps is on and the
+ implementation doesn't support it.
+ @retval Other Some error occurs.
+
+**/
+EFI_STATUS
+MnpConfigureInstance (
+ IN MNP_INSTANCE_DATA *Instance,
+ IN EFI_MANAGED_NETWORK_CONFIG_DATA *ConfigData OPTIONAL
+ )
+{
+ EFI_STATUS Status;
+ MNP_SERVICE_DATA *MnpServiceData;
+ 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;
+ NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_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) {
+ MnpServiceData->UnicastCount--;
+ }
+
+ if (OldConfigData->EnableMulticastReceive) {
+ MnpServiceData->MulticastCount--;
+ }
+
+ if (OldConfigData->EnableBroadcastReceive) {
+ MnpServiceData->BroadcastCount--;
+ }
+
+ if (OldConfigData->EnablePromiscuousReceive) {
+ MnpServiceData->PromiscuousCount--;
+ }
+
+ //
+ // Set the receive filter counters and the receive filter of the
+ // instance according to the new ConfigData.
+ //
+ if (NewConfigData->EnableUnicastReceive) {
+ MnpServiceData->UnicastCount++;
+ Instance->ReceiveFilter |= MNP_RECEIVE_UNICAST;
+ }
+
+ if (NewConfigData->EnableMulticastReceive) {
+ MnpServiceData->MulticastCount++;
+ }
+
+ if (NewConfigData->EnableBroadcastReceive) {
+ MnpServiceData->BroadcastCount++;
+ Instance->ReceiveFilter |= MNP_RECEIVE_BROADCAST;
+ }
+
+ if (NewConfigData->EnablePromiscuousReceive) {
+ MnpServiceData->PromiscuousCount++;
+ }
+
+ if (OldConfigData->FlushQueuesOnReset) {
+
+ MnpFlushRcvdDataQueue (Instance);
+ }
+
+ if (ConfigData == NULL) {
+
+ NetMapIterate (&Instance->RxTokenMap, MnpCancelTokens, NULL);
+ }
+
+ if (!NewConfigData->EnableMulticastReceive) {
+
+ MnpGroupOp (Instance, FALSE, NULL, NULL);
+ }
+
+ //
+ // Save the new configuration data.
+ //
+ *OldConfigData = *NewConfigData;
+
+ Instance->Configured = (BOOLEAN) (ConfigData != NULL);
+
+ if (Instance->Configured) {
+ //
+ // The instance is configured, start the Mnp.
+ //
+ Status = MnpStart (
+ MnpServiceData,
+ IsConfigUpdate,
+ !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 MnpServiceData Pointer to the mnp service 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.
+
+**/
+STATIC
+EFI_STATUS
+MnpConfigReceiveFilters (
+ IN MNP_SERVICE_DATA *MnpServiceData
+ )
+{
+ EFI_STATUS Status;
+ EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
+ EFI_MAC_ADDRESS *MCastFilter;
+ UINT32 MCastFilterCnt;
+ UINT32 EnableFilterBits;
+ UINT32 DisableFilterBits;
+ BOOLEAN ResetMCastFilters;
+ NET_LIST_ENTRY *Entry;
+ UINT32 Index;
+ MNP_GROUP_ADDRESS *GroupAddress;
+
+ NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
+
+ Snp = MnpServiceData->Snp;
+
+ //
+ // Initialize the enable filter and disable filter.
+ //
+ EnableFilterBits = 0;
+ DisableFilterBits = Snp->Mode->ReceiveFilterMask;
+
+ if (MnpServiceData->UnicastCount != 0) {
+ //
+ // Enable unicast if any instance wants to receive unicast.
+ //
+ EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_UNICAST;
+ }
+
+ if (MnpServiceData->BroadcastCount != 0) {
+ //
+ // Enable broadcast if any instance wants to receive broadcast.
+ //
+ EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
+ }
+
+ MCastFilter = NULL;
+ MCastFilterCnt = 0;
+ ResetMCastFilters = TRUE;
+
+ if ((MnpServiceData->MulticastCount != 0) && (MnpServiceData->GroupAddressCount != 0)) {
+ //
+ // There are instances configured to receive multicast and already some group
+ // addresses are joined.
+ //
+
+ ResetMCastFilters = FALSE;
+
+ if (MnpServiceData->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 mulicast addresses.
+ //
+ MCastFilterCnt = MnpServiceData->GroupAddressCount;
+ MCastFilter = NetAllocatePool (sizeof (EFI_MAC_ADDRESS) * MCastFilterCnt);
+ if (MCastFilter == NULL) {
+
+ MNP_DEBUG_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, &MnpServiceData->GroupAddressList) {
+
+ GroupAddress = NET_LIST_USER_STRUCT (Entry, MNP_GROUP_ADDRESS, AddrEntry);
+ *(MCastFilter + Index) = GroupAddress->Address;
+ 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) {
+ 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 (MnpServiceData->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)) {
+
+ MNP_DEBUG_ERROR (
+ ("MnpConfigReceiveFilters: Snp->ReceiveFilters failed, %r.\n",
+ Status)
+ );
+ }
+ );
+
+ if (MCastFilter != NULL) {
+ //
+ // Free the buffer used to hold the group addresses.
+ //
+ NetFreePool (MCastFilter);
+ }
+
+ return Status;
+}
+
+
+/**
+ Add a group address control block which controls the MacAddress for
+ this instance.
+
+ @param Instance Pointer to the mnp instance context data.
+ @param CtrlBlk Pointer to the group address control block.
+ @param GroupAddress Pointer to the group adress.
+ @param MacAddress Pointer to the mac address.
+ @param HwAddressSize The hardware address size.
+
+ @retval EFI_SUCCESS The group address control block is added.
+ @retval EFI_OUT_OF_RESOURCE Failed due to lack of memory resources.
+
+**/
+STATIC
+EFI_STATUS
+MnpGroupOpAddCtrlBlk (
+ IN MNP_INSTANCE_DATA *Instance,
+ IN MNP_GROUP_CONTROL_BLOCK *CtrlBlk,
+ IN MNP_GROUP_ADDRESS *GroupAddress OPTIONAL,
+ IN EFI_MAC_ADDRESS *MacAddress,
+ IN UINT32 HwAddressSize
+ )
+{
+ MNP_SERVICE_DATA *MnpServiceData;
+
+ NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
+
+ MnpServiceData = Instance->MnpServiceData;
+ NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
+
+ if (GroupAddress == NULL) {
+
+ ASSERT (MacAddress != NULL);
+
+ //
+ // Allocate a new GroupAddress to be added into MNP's GroupAddressList.
+ //
+ GroupAddress = NetAllocatePool (sizeof (MNP_GROUP_ADDRESS));
+ if (GroupAddress == NULL) {
+
+ MNP_DEBUG_ERROR (("MnpGroupOpFormCtrlBlk: Failed to allocate memory resource.\n"));
+
+ return EFI_OUT_OF_RESOURCES;
+ }
+
+ GroupAddress->Address = *MacAddress;
+ GroupAddress->RefCnt = 0;
+ NetListInsertTail (
+ &MnpServiceData->GroupAddressList,
+ &GroupAddress->AddrEntry
+ );
+ MnpServiceData->GroupAddressCount++;
+ }
+
+ //
+ // Increase the RefCnt.
+ //
+ GroupAddress->RefCnt++;
+
+ //
+ // Add the CtrlBlk into the instance's GroupCtrlBlkList.
+ //
+ CtrlBlk->GroupAddress = GroupAddress;
+ NetListInsertTail (&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 Instance Pointer to the instance context data.
+ @param CtrlBlk Pointer to the group control block to delete.
+
+ @return The group address controlled by the control block is no longer used or not.
+
+**/
+STATIC
+BOOLEAN
+MnpGroupOpDelCtrlBlk (
+ IN MNP_INSTANCE_DATA *Instance,
+ IN MNP_GROUP_CONTROL_BLOCK *CtrlBlk
+ )
+{
+ MNP_SERVICE_DATA *MnpServiceData;
+ MNP_GROUP_ADDRESS *GroupAddress;
+
+ NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
+
+ MnpServiceData = Instance->MnpServiceData;
+ NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
+
+ //
+ // Remove and free the CtrlBlk.
+ //
+ GroupAddress = CtrlBlk->GroupAddress;
+ NetListRemoveEntry (&CtrlBlk->CtrlBlkEntry);
+ NetFreePool (CtrlBlk);
+
+ ASSERT (GroupAddress->RefCnt > 0);
+
+ //
+ // Count down the RefCnt.
+ //
+ GroupAddress->RefCnt--;
+
+ if (GroupAddress->RefCnt == 0) {
+ //
+ // Free this GroupAddress entry if no instance uses it.
+ //
+ MnpServiceData->GroupAddressCount--;
+ NetListRemoveEntry (&GroupAddress->AddrEntry);
+ NetFreePool (GroupAddress);
+
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+
+/**
+ Do the group operations for this instance.
+
+ @param Instance Pointer to the instance context data.
+ @param JoinFlag Set to TRUE to join a group. Set to TRUE to leave a
+ group/groups.
+ @param MacAddress Pointer to the group address to join or leave.
+ @param CtrlBlk Pointer to the group control block if JoinFlag if
+ FALSE.
+
+ @retval EFI_SUCCESS The group operation finished.
+ @retval Other Some error occurs.
+
+**/
+EFI_STATUS
+MnpGroupOp (
+ IN MNP_INSTANCE_DATA *Instance,
+ IN BOOLEAN JoinFlag,
+ IN EFI_MAC_ADDRESS *MacAddress OPTIONAL,
+ IN MNP_GROUP_CONTROL_BLOCK *CtrlBlk OPTIONAL
+ )
+{
+ MNP_SERVICE_DATA *MnpServiceData;
+ NET_LIST_ENTRY *Entry;
+ NET_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);
+
+ MnpServiceData = Instance->MnpServiceData;
+ SnpMode = MnpServiceData->Snp->Mode;
+
+ if (JoinFlag) {
+ //
+ // A new gropu address is to be added.
+ //
+
+ GroupAddress = NULL;
+ AddressExist = FALSE;
+
+ //
+ // Allocate memory for the control block.
+ //
+ NewCtrlBlk = NetAllocatePool (sizeof (MNP_GROUP_CONTROL_BLOCK));
+ if (NewCtrlBlk == NULL) {
+
+ MNP_DEBUG_ERROR (("MnpGroupOp: Failed to allocate memory resource.\n"));
+ return EFI_OUT_OF_RESOURCES;
+ }
+
+ NET_LIST_FOR_EACH (Entry, &MnpServiceData->GroupAddressList) {
+ //
+ // Check whether the MacAddress is already joined by other instances.
+ //
+ GroupAddress = NET_LIST_USER_STRUCT (Entry, MNP_GROUP_ADDRESS, AddrEntry);
+ if (0 == NetCompareMem (
+ MacAddress,
+ &GroupAddress->Address,
+ SnpMode->HwAddressSize
+ )) {
+
+ 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 (MnpServiceData);
+ }
+
+ return Status;
+}
diff --git a/MdeModulePkg/Universal/Network/MnpDxe/MnpDebug.h b/MdeModulePkg/Universal/Network/MnpDxe/MnpDebug.h
new file mode 100644
index 0000000000..72eb59b9b4
--- /dev/null
+++ b/MdeModulePkg/Universal/Network/MnpDxe/MnpDebug.h
@@ -0,0 +1,28 @@
+/** @file
+
+Copyright (c) 2005 - 2006, Intel Corporation
+All rights reserved. This program and the accompanying materials
+are licensed and made available under the terms and conditions of the BSD License
+which accompanies this distribution. The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+Module Name:
+
+ MnpDebug.h
+
+Abstract:
+
+
+**/
+
+#ifndef _MNP_DEBUG_H_
+#define _MNP_DEBUG_H_
+
+#define MNP_DEBUG_TRACE(PrintArg) NET_DEBUG_TRACE ("Mnp", PrintArg)
+#define MNP_DEBUG_WARN(PrintArg) NET_DEBUG_WARNING ("Mnp", PrintArg)
+#define MNP_DEBUG_ERROR(PrintArg) NET_DEBUG_ERROR ("Mnp", PrintArg)
+
+#endif
diff --git a/MdeModulePkg/Universal/Network/MnpDxe/MnpDriver.c b/MdeModulePkg/Universal/Network/MnpDxe/MnpDriver.c
new file mode 100644
index 0000000000..07be21dced
--- /dev/null
+++ b/MdeModulePkg/Universal/Network/MnpDxe/MnpDriver.c
@@ -0,0 +1,560 @@
+/** @file
+
+Copyright (c) 2005 - 2007, Intel Corporation
+All rights reserved. This program and the accompanying materials
+are licensed and made available under the terms and conditions of the BSD License
+which accompanies this distribution. The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+Module Name:
+
+ MnpDriver.c
+
+Abstract:
+
+
+**/
+
+#include "MnpDriver.h"
+#include "MnpDebug.h"
+#include "MnpImpl.h"
+
+
+EFI_DRIVER_BINDING_PROTOCOL gMnpDriverBinding = {
+ MnpDriverBindingSupported,
+ MnpDriverBindingStart,
+ MnpDriverBindingStop,
+ 0xa,
+ NULL,
+ NULL
+};
+
+
+/**
+ Test to see if this driver supports ControllerHandle.
+
+ @param This Protocol instance pointer.
+ @param ControllerHandle Handle of device to test.
+ @param RemainingDevicePath Optional parameter use to pick a specific child
+ device to start.
+
+ @retval EFI_SUCCES This driver supports this device.
+ @retval EFI_ALREADY_STARTED This driver is already running on this device.
+ @retval other 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;
+
+ //
+ // Test to see if MNP is already installed.
+ //
+ Status = gBS->OpenProtocol (
+ ControllerHandle,
+ &gEfiManagedNetworkServiceBindingProtocolGuid,
+ NULL,
+ This->DriverBindingHandle,
+ ControllerHandle,
+ EFI_OPEN_PROTOCOL_TEST_PROTOCOL
+ );
+ if (!EFI_ERROR (Status)) {
+
+ return EFI_ALREADY_STARTED;
+ }
+
+ //
+ // Test to see if SNP is installed.
+ //
+ Status = gBS->OpenProtocol (
+ ControllerHandle,
+ &gEfiSimpleNetworkProtocolGuid,
+ NULL,
+ This->DriverBindingHandle,
+ ControllerHandle,
+ EFI_OPEN_PROTOCOL_TEST_PROTOCOL
+ );
+
+ return Status;
+}
+
+
+/**
+ Start this driver on ControllerHandle.
+
+ @param This Protocol instance pointer.
+ @param ControllerHandle Handle of device to bind driver to.
+ @param RemainingDevicePath Optional parameter use to pick a specific child
+ device to start.
+
+ @retval EFI_SUCCES This driver is added to ControllerHandle.
+ @retval EFI_ALREADY_STARTED This driver is already running on
+ ControllerHandle.
+ @retval other 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;
+ BOOLEAN MnpInitialized;
+
+ MnpInitialized = FALSE;
+
+ MnpServiceData = NetAllocateZeroPool (sizeof (MNP_SERVICE_DATA));
+ if (MnpServiceData == NULL) {
+ MNP_DEBUG_ERROR (("MnpDriverBindingStart(): Failed to allocate the "
+ L"Mnp Service Data.\n"));
+
+ return EFI_OUT_OF_RESOURCES;
+ }
+
+ //
+ // Initialize the Mnp Service Data.
+ //
+ Status = MnpInitializeServiceData (MnpServiceData, This->DriverBindingHandle, ControllerHandle);
+ if (EFI_ERROR (Status)) {
+
+ MNP_DEBUG_ERROR (("MnpDriverBindingStart: MnpInitializeServiceData "
+ L"failed, %r.\n",Status));
+ goto ErrorExit;
+ }
+
+ MnpInitialized = TRUE;
+
+ //
+ // Install the MNP Service Binding Protocol.
+ //
+ Status = gBS->InstallMultipleProtocolInterfaces (
+ &ControllerHandle,
+ &gEfiManagedNetworkServiceBindingProtocolGuid,
+ &MnpServiceData->ServiceBinding,
+ NULL
+ );
+
+ErrorExit:
+
+ if (EFI_ERROR (Status)) {
+
+ if (MnpInitialized) {
+ //
+ // Flush the Mnp Service Data.
+ //
+ MnpFlushServiceData (MnpServiceData);
+ }
+
+ //
+ // Close the Simple Network Protocol.
+ //
+ gBS->CloseProtocol (
+ ControllerHandle,
+ &gEfiSimpleNetworkProtocolGuid,
+ This->DriverBindingHandle,
+ ControllerHandle
+ );
+
+ NetFreePool (MnpServiceData);
+ }
+
+ return Status;
+}
+
+
+/**
+ Stop this driver on ControllerHandle.
+
+ @param This Protocol instance pointer.
+ @param ControllerHandle Handle of device to stop driver on.
+ @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number
+ of children is zero stop the entire bus driver.
+ @param ChildHandleBuffer List of Child Handles to Stop.
+
+ @retval EFI_SUCCES This driver is removed ControllerHandle.
+ @retval other This driver was not removed from this device.
+
+**/
+EFI_STATUS
+EFIAPI
+MnpDriverBindingStop (
+ IN EFI_DRIVER_BINDING_PROTOCOL *This,
+ IN EFI_HANDLE ControllerHandle,
+ IN UINTN NumberOfChildren,
+ IN EFI_HANDLE *ChildHandleBuffer
+ )
+{
+ EFI_STATUS Status;
+ EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
+ MNP_SERVICE_DATA *MnpServiceData;
+ MNP_INSTANCE_DATA *Instance;
+
+ //
+ // Retrieve the 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)) {
+
+ MNP_DEBUG_ERROR (
+ ("MnpDriverBindingStop: Locate MNP Service Binding Protocol failed, %r.\n",
+ Status)
+ );
+ goto EXIT;
+ }
+
+ MnpServiceData = MNP_SERVICE_DATA_FROM_THIS (ServiceBinding);
+
+ while (!NetListIsEmpty (&MnpServiceData->ChildrenList)) {
+ //
+ // Don't use NetListRemoveHead here, the remove opreration will be done
+ // in ServiceBindingDestroyChild.
+ //
+ Instance = NET_LIST_HEAD (
+ &MnpServiceData->ChildrenList,
+ MNP_INSTANCE_DATA,
+ InstEntry
+ );
+
+ ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle);
+ }
+
+ //
+ // Uninstall the MNP Service Binding Protocol.
+ //
+ Status = gBS->UninstallMultipleProtocolInterfaces (
+ ControllerHandle,
+ &gEfiManagedNetworkServiceBindingProtocolGuid,
+ ServiceBinding,
+ NULL
+ );
+ if (EFI_ERROR (Status)) {
+
+ MNP_DEBUG_ERROR (("MnpDriverBindingStop: Uninstall MNP Service Binding Protocol failed, %r.\n"));
+ goto EXIT;
+ }
+
+ //
+ // Close the openned Snp protocol.
+ //
+ Status = gBS->CloseProtocol (
+ ControllerHandle,
+ &gEfiSimpleNetworkProtocolGuid,
+ This->DriverBindingHandle,
+ ControllerHandle
+ );
+ if (EFI_ERROR (Status)) {
+
+ MNP_DEBUG_ERROR (("MnpDriverBindingStop: Close SNP Protocol failed, %r.\n", Status));
+ goto EXIT;
+ }
+
+ //
+ // Flush the Mnp service data.
+ //
+ MnpFlushServiceData (MnpServiceData);
+
+ NetFreePool (MnpServiceData);
+
+EXIT:
+
+ return Status;
+}
+
+
+/**
+ Creates a child handle with a set of I/O services.
+
+ @param This Protocol instance pointer.
+ @param 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_SUCCES The child handle was created with the I/O
+ services.
+ @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
+ the child.
+ @retval other The child handle was not created.
+
+**/
+EFI_STATUS
+EFIAPI
+MnpServiceBindingCreateChild (
+ IN EFI_SERVICE_BINDING_PROTOCOL *This,
+ IN EFI_HANDLE *ChildHandle
+ )
+{
+ EFI_STATUS Status;
+ MNP_SERVICE_DATA *MnpServiceData;
+ MNP_INSTANCE_DATA *Instance;
+ VOID *Snp;
+ 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 = NetAllocateZeroPool (sizeof (MNP_INSTANCE_DATA));
+ if (Instance == NULL) {
+
+ MNP_DEBUG_ERROR (("MnpServiceBindingCreateChild: Faild 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)) {
+
+ MNP_DEBUG_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->ControllerHandle,
+ &gEfiSimpleNetworkProtocolGuid,
+ (VOID **) &Snp,
+ gMnpDriverBinding.DriverBindingHandle,
+ Instance->Handle,
+ EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
+ );
+ if (EFI_ERROR (Status)) {
+ goto ErrorExit;
+ }
+
+ //
+ // Add the child instance into ChildrenList.
+ //
+ OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);
+
+ NetListInsertTail (&MnpServiceData->ChildrenList, &Instance->InstEntry);
+ MnpServiceData->ChildrenNumber++;
+
+ NET_RESTORE_TPL (OldTpl);
+
+ErrorExit:
+
+ if (EFI_ERROR (Status)) {
+
+ if (Instance->Handle != NULL) {
+
+ gBS->UninstallMultipleProtocolInterfaces (
+ &gEfiManagedNetworkProtocolGuid,
+ &Instance->ManagedNetwork,
+ NULL
+ );
+ }
+
+ NetFreePool (Instance);
+ }
+
+ return Status;
+}
+
+
+/**
+ Destroys a child handle with a set of I/O services.
+
+ @param This Protocol instance pointer.
+ @param ChildHandle Handle of the child to destroy.
+
+ @retval EFI_SUCCES The I/O services were removed from the child
+ handle.
+ @retval EFI_UNSUPPORTED The child handle does not support the I/O services
+ that are being removed.
+ @retval EFI_INVALID_PARAMETER Child handle is not a valid EFI Handle.
+ @retval EFI_ACCESS_DENIED The child handle could not be destroyed because
+ its I/O services are being used.
+ @retval other 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->ControllerHandle,
+ &gEfiSimpleNetworkProtocolGuid,
+ gMnpDriverBinding.DriverBindingHandle,
+ ChildHandle
+ );
+
+ //
+ // Uninstall the ManagedNetwork protocol.
+ //
+ Status = gBS->UninstallMultipleProtocolInterfaces (
+ ChildHandle,
+ &gEfiManagedNetworkProtocolGuid,
+ &Instance->ManagedNetwork,
+ NULL
+ );
+ if (EFI_ERROR (Status)) {
+
+ MNP_DEBUG_ERROR (
+ ("MnpServiceBindingDestroyChild: Failed to uninstall the ManagedNetwork protocol, %r.\n",
+ Status)
+ );
+
+ Instance->Destroyed = FALSE;
+ return Status;
+ }
+
+ OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);
+
+ //
+ // 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.
+ //
+ NetListRemoveEntry (&Instance->InstEntry);
+ MnpServiceData->ChildrenNumber--;
+
+ NET_RESTORE_TPL (OldTpl);
+
+ NetFreePool (Instance);
+
+ return Status;
+}
+
+//@MT: EFI_DRIVER_ENTRY_POINT (MnpDriverEntryPoint)
+
+EFI_STATUS
+EFIAPI
+MnpDriverEntryPoint (
+ IN EFI_HANDLE ImageHandle,
+ IN EFI_SYSTEM_TABLE *SystemTable
+ )
+/*++
+
+Routine Description:
+
+ The entry point for Mnp driver which installs the driver binding and component name
+ protocol on its ImageHandle.
+
+Arguments:
+
+ ImageHandle - The image handle of the driver.
+ SystemTable - The system table.
+
+Returns:
+
+ EFI_SUCCESS - If the driver binding and component name protocols are successfully
+ installed, otherwise if failed.
+
+--*/
+{
+ return NetLibInstallAllDriverProtocols (
+ ImageHandle,
+ SystemTable,
+ &gMnpDriverBinding,
+ ImageHandle,
+ &gMnpComponentName,
+ NULL,
+ NULL
+ );
+}
diff --git a/MdeModulePkg/Universal/Network/MnpDxe/MnpDriver.h b/MdeModulePkg/Universal/Network/MnpDxe/MnpDriver.h
new file mode 100644
index 0000000000..48ad0960cd
--- /dev/null
+++ b/MdeModulePkg/Universal/Network/MnpDxe/MnpDriver.h
@@ -0,0 +1,136 @@
+/** @file
+
+Copyright (c) 2005 - 2007, Intel Corporation
+All rights reserved. This program and the accompanying materials
+are licensed and made available under the terms and conditions of the BSD License
+which accompanies this distribution. The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+Module Name:
+
+ MnpDriver.h
+
+Abstract:
+
+
+**/
+
+#ifndef _MNP_DRIVER_H_
+#define _MNP_DRIVER_H_
+#include <PiDxe.h>
+
+#include <Protocol/ManagedNetwork.h>
+#include <Protocol/SimpleNetwork.h>
+#include <Protocol/ServiceBinding.h>
+
+#include <Library/DebugLib.h>
+#include <Library/UefiDriverEntryPoint.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/UefiLib.h>
+#include <Library/NetLib.h>
+#include <Library/BaseLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/BaseMemoryLib.h>
+
+#include "MnpDebug.h"
+
+//
+// Required Global Variables
+//
+extern EFI_DRIVER_BINDING_PROTOCOL gMnpDriverBinding;
+extern EFI_COMPONENT_NAME_PROTOCOL gMnpComponentName;
+
+#define MNP_SERVICE_DATA_SIGNATURE EFI_SIGNATURE_32 ('M', 'n', 'p', 'S')
+
+typedef struct _MNP_SERVICE_DATA {
+ UINT32 Signature;
+
+ EFI_HANDLE ControllerHandle;
+
+ EFI_SERVICE_BINDING_PROTOCOL ServiceBinding;
+ EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
+
+ UINT32 Mtu;
+
+ NET_LIST_ENTRY ChildrenList;
+ UINTN ChildrenNumber;
+ UINTN ConfiguredChildrenNumber;
+
+ NET_LIST_ENTRY GroupAddressList;
+ UINT32 GroupAddressCount;
+
+ EFI_EVENT TxTimeoutEvent;
+
+ NET_BUF_QUEUE FreeNbufQue;
+ INTN NbufCnt;
+
+ EFI_EVENT PollTimer;
+ BOOLEAN EnableSystemPoll;
+
+ EFI_EVENT TimeoutCheckTimer;
+
+ 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;
+ NET_BUF *RxNbufCache;
+ UINT8 *TxBuf;
+} MNP_SERVICE_DATA;
+
+#define MNP_SERVICE_DATA_FROM_THIS(a) \
+ CR ( \
+ (a), \
+ MNP_SERVICE_DATA, \
+ ServiceBinding, \
+ MNP_SERVICE_DATA_SIGNATURE \
+ )
+
+EFI_STATUS
+EFIAPI
+MnpDriverBindingSupported (
+ IN EFI_DRIVER_BINDING_PROTOCOL * This,
+ IN EFI_HANDLE ControllerHandle,
+ IN EFI_DEVICE_PATH_PROTOCOL * RemainingDevicePath OPTIONAL
+ );
+
+EFI_STATUS
+EFIAPI
+MnpDriverBindingStart (
+ IN EFI_DRIVER_BINDING_PROTOCOL * This,
+ IN EFI_HANDLE ControllerHandle,
+ IN EFI_DEVICE_PATH_PROTOCOL * RemainingDevicePath OPTIONAL
+ );
+
+EFI_STATUS
+EFIAPI
+MnpDriverBindingStop (
+ IN EFI_DRIVER_BINDING_PROTOCOL *This,
+ IN EFI_HANDLE ControllerHandle,
+ IN UINTN NumberOfChildren,
+ IN EFI_HANDLE *ChildHandleBuffer
+ );
+
+EFI_STATUS
+EFIAPI
+MnpServiceBindingCreateChild (
+ IN EFI_SERVICE_BINDING_PROTOCOL *This,
+ IN EFI_HANDLE *ChildHandle
+ );
+
+EFI_STATUS
+EFIAPI
+MnpServiceBindingDestroyChild (
+ IN EFI_SERVICE_BINDING_PROTOCOL *This,
+ IN EFI_HANDLE ChildHandle
+ );
+
+#endif
diff --git a/MdeModulePkg/Universal/Network/MnpDxe/MnpDxe.inf b/MdeModulePkg/Universal/Network/MnpDxe/MnpDxe.inf
new file mode 100644
index 0000000000..26512cd1da
--- /dev/null
+++ b/MdeModulePkg/Universal/Network/MnpDxe/MnpDxe.inf
@@ -0,0 +1,63 @@
+#/** @file
+# Component name for module Mnp
+#
+# FIX ME!
+# Copyright (c) 2006, Intel Corporation. All right reserved.
+#
+# All rights reserved. This program and the accompanying materials
+# are licensed and made available under the terms and conditions of the BSD License
+# which accompanies this distribution. The full text of the license may be found at
+# http://opensource.org/licenses/bsd-license.php
+#
+# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+#
+#**/
+
+[Defines]
+ INF_VERSION = 0x00010005
+ BASE_NAME = MnpDxe
+ FILE_GUID = 025BBFC7-E6A9-4b8b-82AD-6815A1AEAF4A
+ MODULE_TYPE = DXE_DRIVER
+ VERSION_STRING = 1.0
+ EDK_RELEASE_VERSION = 0x00020000
+ EFI_SPECIFICATION_VERSION = 0x00020000
+
+ ENTRY_POINT = MnpDriverEntryPoint
+
+#
+# The following information is for reference only and not required by the build tools.
+#
+# VALID_ARCHITECTURES = IA32 X64 IPF EBC
+#
+
+[Sources.common]
+ MnpDebug.h
+ MnpMain.c
+ MnpIo.c
+ MnpDriver.h
+ ComponentName.c
+ MnpDriver.c
+ MnpConfig.c
+ MnpImpl.h
+
+
+[Packages]
+ MdePkg/MdePkg.dec
+ MdeModulePkg/MdeModulePkg.dec
+
+
+[LibraryClasses]
+ UefiLib
+ UefiBootServicesTableLib
+ UefiDriverEntryPoint
+ DebugLib
+ NetLib
+
+
+[Protocols]
+ gEfiManagedNetworkServiceBindingProtocolGuid # PROTOCOL ALWAYS_CONSUMED
+ gEfiSimpleNetworkProtocolGuid # PROTOCOL ALWAYS_CONSUMED
+ gEfiManagedNetworkProtocolGuid # PROTOCOL ALWAYS_CONSUMED
+
diff --git a/MdeModulePkg/Universal/Network/MnpDxe/MnpDxe.msa b/MdeModulePkg/Universal/Network/MnpDxe/MnpDxe.msa
new file mode 100644
index 0000000000..3ef22ddc03
--- /dev/null
+++ b/MdeModulePkg/Universal/Network/MnpDxe/MnpDxe.msa
@@ -0,0 +1,70 @@
+<ModuleSurfaceArea xmlns="http://www.TianoCore.org/2006/Edk2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+ <MsaHeader>
+ <ModuleName>MnpDxe</ModuleName>
+ <ModuleType>DXE_DRIVER</ModuleType>
+ <GuidValue>025BBFC7-E6A9-4b8b-82AD-6815A1AEAF4A</GuidValue>
+ <Version>1.0</Version>
+ <Abstract>Component name for module Mnp</Abstract>
+ <Description>FIX ME!</Description>
+ <Copyright>Copyright (c) 2006, Intel Corporation. All right reserved.</Copyright>
+ <License>All rights reserved. This program and the accompanying materials
+ are licensed and made available under the terms and conditions of the BSD License
+ which accompanies this distribution. The full text of the license may be found at
+ http://opensource.org/licenses/bsd-license.php
+
+ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.</License>
+ <Specification>FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052</Specification>
+ </MsaHeader>
+ <ModuleDefinitions>
+ <SupportedArchitectures>IA32 X64 IPF EBC</SupportedArchitectures>
+ <BinaryModule>false</BinaryModule>
+ <OutputFileBasename>MnpDxe</OutputFileBasename>
+ </ModuleDefinitions>
+ <LibraryClassDefinitions>
+ <LibraryClass Usage="ALWAYS_CONSUMED">
+ <Keyword>DebugLib</Keyword>
+ </LibraryClass>
+ <LibraryClass Usage="ALWAYS_CONSUMED">
+ <Keyword>UefiDriverEntryPoint</Keyword>
+ </LibraryClass>
+ <LibraryClass Usage="ALWAYS_CONSUMED">
+ <Keyword>UefiBootServicesTableLib</Keyword>
+ </LibraryClass>
+ <LibraryClass Usage="ALWAYS_CONSUMED">
+ <Keyword>UefiLib</Keyword>
+ </LibraryClass>
+ </LibraryClassDefinitions>
+ <SourceFiles>
+ <Filename>MnpImpl.h</Filename>
+ <Filename>MnpConfig.c</Filename>
+ <Filename>MnpDriver.c</Filename>
+ <Filename>ComponentName.c</Filename>
+ <Filename>MnpDriver.h</Filename>
+ <Filename>MnpIo.c</Filename>
+ <Filename>MnpMain.c</Filename>
+ <Filename>MnpDebug.h</Filename>
+ </SourceFiles>
+ <PackageDependencies>
+ <Package PackageGuid="5e0e9358-46b6-4ae2-8218-4ab8b9bbdcec"/>
+ <Package PackageGuid="68169ab0-d41b-4009-9060-292c253ac43d"/>
+ </PackageDependencies>
+ <Protocols>
+ <Protocol Usage="ALWAYS_CONSUMED">
+ <ProtocolCName>gEfiManagedNetworkProtocolGuid</ProtocolCName>
+ </Protocol>
+ <Protocol Usage="ALWAYS_CONSUMED">
+ <ProtocolCName>gEfiSimpleNetworkProtocolGuid</ProtocolCName>
+ </Protocol>
+ <Protocol Usage="ALWAYS_CONSUMED">
+ <ProtocolCName>gEfiManagedNetworkServiceBindingProtocolGuid</ProtocolCName>
+ </Protocol>
+ </Protocols>
+ <Externs>
+ <Specification>EFI_SPECIFICATION_VERSION 0x00020000</Specification>
+ <Specification>EDK_RELEASE_VERSION 0x00020000</Specification>
+ <Extern>
+ <ModuleEntryPoint>MnpDriverEntryPoint</ModuleEntryPoint>
+ </Extern>
+ </Externs>
+</ModuleSurfaceArea> \ No newline at end of file
diff --git a/MdeModulePkg/Universal/Network/MnpDxe/MnpImpl.h b/MdeModulePkg/Universal/Network/MnpDxe/MnpImpl.h
new file mode 100644
index 0000000000..cdb081d2b4
--- /dev/null
+++ b/MdeModulePkg/Universal/Network/MnpDxe/MnpImpl.h
@@ -0,0 +1,274 @@
+/** @file
+
+Copyright (c) 2005 - 2007, Intel Corporation
+All rights reserved. This program and the accompanying materials
+are licensed and made available under the terms and conditions of the BSD License
+which accompanies this distribution. The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+Module Name:
+
+ MnpImpl.h
+
+Abstract:
+
+
+**/
+
+#ifndef _MNP_IMPL_H_
+#define _MNP_IMPL_H_
+
+#include "MnpDriver.h"
+#include "MnpDebug.h"
+
+#define NET_ETHER_FCS_SIZE 4
+
+#define MNP_SYS_POLL_INTERVAL (2 * TICKS_PER_MS) // 2 milliseconds
+#define MNP_TIMEOUT_CHECK_INTERVAL (50 * TICKS_PER_MS) // 50 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_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 EFI_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 _MNP_INSTANCE_DATA {
+ UINT32 Signature;
+
+ MNP_SERVICE_DATA *MnpServiceData;
+
+ EFI_HANDLE Handle;
+
+ NET_LIST_ENTRY InstEntry;
+
+ EFI_MANAGED_NETWORK_PROTOCOL ManagedNetwork;
+
+ BOOLEAN Configured;
+ BOOLEAN Destroyed;
+
+ NET_LIST_ENTRY GroupCtrlBlkList;
+
+ NET_MAP RxTokenMap;
+
+ NET_LIST_ENTRY RxDeliveredPacketQueue;
+ NET_LIST_ENTRY RcvdPacketQueue;
+ UINTN RcvdPacketQueueSize;
+
+ EFI_MANAGED_NETWORK_CONFIG_DATA ConfigData;
+
+ UINT8 ReceiveFilter;
+} MNP_INSTANCE_DATA;
+
+typedef struct _MNP_GROUP_ADDRESS {
+ NET_LIST_ENTRY AddrEntry;
+ EFI_MAC_ADDRESS Address;
+ INTN RefCnt;
+} MNP_GROUP_ADDRESS;
+
+typedef struct _MNP_GROUP_CONTROL_BLOCK {
+ NET_LIST_ENTRY CtrlBlkEntry;
+ MNP_GROUP_ADDRESS *GroupAddress;
+} MNP_GROUP_CONTROL_BLOCK;
+
+typedef struct _MNP_RXDATA_WRAP {
+ NET_LIST_ENTRY WrapEntry;
+ MNP_INSTANCE_DATA *Instance;
+ EFI_MANAGED_NETWORK_RECEIVE_DATA RxData;
+ NET_BUF *Nbuf;
+ UINT64 TimeoutTick;
+} MNP_RXDATA_WRAP;
+
+EFI_STATUS
+MnpInitializeServiceData (
+ IN MNP_SERVICE_DATA *MnpServiceData,
+ IN EFI_HANDLE ImageHandle,
+ IN EFI_HANDLE ControllerHandle
+ );
+
+VOID
+MnpFlushServiceData (
+ MNP_SERVICE_DATA *MnpServiceData
+ );
+
+VOID
+MnpInitializeInstanceData (
+ IN MNP_SERVICE_DATA *MnpServiceData,
+ IN MNP_INSTANCE_DATA *Instance
+ );
+
+EFI_STATUS
+MnpTokenExist (
+ IN NET_MAP *Map,
+ IN NET_MAP_ITEM *Item,
+ IN VOID *Arg
+ );
+
+EFI_STATUS
+MnpCancelTokens (
+ IN NET_MAP *Map,
+ IN NET_MAP_ITEM *Item,
+ IN VOID *Arg
+ );
+
+VOID
+MnpFlushRcvdDataQueue (
+ IN MNP_INSTANCE_DATA *Instance
+ );
+
+EFI_STATUS
+MnpConfigureInstance (
+ IN MNP_INSTANCE_DATA *Instance,
+ IN EFI_MANAGED_NETWORK_CONFIG_DATA *ConfigData OPTIONAL
+ );
+
+EFI_STATUS
+MnpGroupOp (
+ IN MNP_INSTANCE_DATA *Instance,
+ IN BOOLEAN JoinFlag,
+ IN EFI_MAC_ADDRESS *MacAddr OPTIONAL,
+ IN MNP_GROUP_CONTROL_BLOCK *CtrlBlk OPTIONAL
+ );
+
+BOOLEAN
+MnpIsValidTxToken (
+ IN MNP_INSTANCE_DATA *Instance,
+ IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token
+ );
+
+VOID
+MnpBuildTxPacket (
+ IN MNP_SERVICE_DATA *MnpServiceData,
+ IN EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData,
+ OUT UINT8 **PktBuf,
+ OUT UINT32 *PktLen
+ );
+
+EFI_STATUS
+MnpSyncSendPacket (
+ IN MNP_SERVICE_DATA *MnpServiceData,
+ IN UINT8 *Packet,
+ IN UINT32 Length,
+ IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token
+ );
+
+EFI_STATUS
+MnpInstanceDeliverPacket (
+ IN MNP_INSTANCE_DATA *Instance
+ );
+
+VOID
+EFIAPI
+MnpRecycleRxData (
+ IN EFI_EVENT Event,
+ IN VOID *Context
+ );
+
+EFI_STATUS
+MnpReceivePacket (
+ IN MNP_SERVICE_DATA *MnpServiceData
+ );
+
+NET_BUF *
+MnpAllocNbuf (
+ IN MNP_SERVICE_DATA *MnpServiceData
+ );
+
+VOID
+MnpFreeNbuf (
+ IN MNP_SERVICE_DATA *MnpServiceData,
+ IN NET_BUF *Nbuf
+ );
+
+VOID
+EFIAPI
+MnpCheckPacketTimeout (
+ IN EFI_EVENT Event,
+ IN VOID *Context
+ );
+
+VOID
+EFIAPI
+MnpSystemPoll (
+ IN EFI_EVENT Event,
+ IN VOID *Context
+ );
+
+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
+ );
+
+EFI_STATUS
+EFIAPI
+MnpConfigure (
+ IN EFI_MANAGED_NETWORK_PROTOCOL *This,
+ IN EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL
+ );
+
+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
+EFIAPI
+MnpGroups (
+ IN EFI_MANAGED_NETWORK_PROTOCOL *This,
+ IN BOOLEAN JoinFlag,
+ IN EFI_MAC_ADDRESS *MacAddress OPTIONAL
+ );
+
+EFI_STATUS
+EFIAPI
+MnpTransmit (
+ IN EFI_MANAGED_NETWORK_PROTOCOL *This,
+ IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token
+ );
+
+EFI_STATUS
+EFIAPI
+MnpCancel (
+ IN EFI_MANAGED_NETWORK_PROTOCOL *This,
+ IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token OPTIONAL
+ );
+
+EFI_STATUS
+EFIAPI
+MnpReceive (
+ IN EFI_MANAGED_NETWORK_PROTOCOL *This,
+ IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token
+ );
+
+EFI_STATUS
+EFIAPI
+MnpPoll (
+ IN EFI_MANAGED_NETWORK_PROTOCOL *This
+ );
+
+#endif
diff --git a/MdeModulePkg/Universal/Network/MnpDxe/MnpIo.c b/MdeModulePkg/Universal/Network/MnpDxe/MnpIo.c
new file mode 100644
index 0000000000..cde235912f
--- /dev/null
+++ b/MdeModulePkg/Universal/Network/MnpDxe/MnpIo.c
@@ -0,0 +1,1087 @@
+/** @file
+
+Copyright (c) 2005 - 2007, Intel Corporation
+All rights reserved. This program and the accompanying materials
+are licensed and made available under the terms and conditions of the BSD License
+which accompanies this distribution. The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+Module Name:
+
+ MnpIo.c
+
+Abstract:
+
+ Implementation of Managed Network Protocol I/O functions.
+
+
+**/
+
+#include <Library/NetLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/BaseLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include "MnpImpl.h"
+
+
+/**
+ Validates the Mnp transmit token.
+
+ @param Instance Pointer to the Mnp instance context data.
+ @param 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_SIMPLE_NETWORK_MODE *SnpMode;
+ 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);
+
+ SnpMode = MnpServiceData->Snp->Mode;
+ 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.
+ //
+ MNP_DEBUG_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).
+ //
+ MNP_DEBUG_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.
+ //
+ MNP_DEBUG_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.
+ //
+ MNP_DEBUG_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.
+ //
+ MNP_DEBUG_WARN (("MnpIsValidTxData: TxData->DataLength exceeds Mtu.\n"));
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+
+/**
+ Build the 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 PktBuf Pointer to record the address of the packet.
+ @param PktLen Pointer to a UINT32 variable used to record the
+ packet's length.
+
+ @return None.
+
+**/
+VOID
+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;
+
+ if ((TxData->DestinationAddress == NULL) && (TxData->FragmentCount == 1)) {
+ //
+ // Media header is in FragmentTable and there is only one fragment,
+ // use fragment buffer directly.
+ //
+ *PktBuf = TxData->FragmentTable[0].FragmentBuffer;
+ *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 = MnpServiceData->Snp->Mode;
+ DstPos = MnpServiceData->TxBuf;
+
+ *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.
+ //
+ NetCopyMem (
+ DstPos,
+ TxData->FragmentTable[Index].FragmentBuffer,
+ TxData->FragmentTable[Index].FragmentLength
+ );
+ DstPos += TxData->FragmentTable[Index].FragmentLength;
+ }
+
+ //
+ // Set the buffer pointer and the buffer length.
+ //
+ *PktBuf = MnpServiceData->TxBuf;
+ *PktLen += TxData->DataLength + TxData->HeaderLength;
+ }
+}
+
+
+/**
+ Synchronously send out the packet.
+
+ @param MnpServiceData Pointer to the mnp service context data.
+ @param Packet Pointer to the pakcet buffer.
+ @param Length The length of the packet.
+ @param 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 EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token
+ )
+{
+ EFI_STATUS Status;
+ EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
+ EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData;
+ UINT32 HeaderSize;
+ UINT8 *TxBuf;
+
+ Snp = MnpServiceData->Snp;
+ TxData = Token->Packet.TxData;
+
+ HeaderSize = Snp->Mode->MediaHeaderSize - TxData->HeaderLength;
+
+ //
+ // Start the timeout event.
+ //
+ Status = gBS->SetTimer (
+ MnpServiceData->TxTimeoutEvent,
+ TimerRelative,
+ MNP_TX_TIMEOUT_TIME
+ );
+ if (EFI_ERROR (Status)) {
+
+ goto SIGNAL_TOKEN;
+ }
+
+ for (;;) {
+ //
+ // Transmit the packet through SNP.
+ //
+ Status = Snp->Transmit (
+ Snp,
+ HeaderSize,
+ Length,
+ Packet,
+ TxData->SourceAddress,
+ TxData->DestinationAddress,
+ &TxData->ProtocolType
+ );
+ if ((Status != EFI_SUCCESS) && (Status != EFI_NOT_READY)) {
+
+ Status = EFI_DEVICE_ERROR;
+ break;
+ }
+
+ //
+ // If Status is EFI_SUCCESS, the packet is put in the transmit queue.
+ // if Status is EFI_NOT_READY, the transmit engine of the network interface is busy.
+ // Both need to sync SNP.
+ //
+ TxBuf = NULL;
+ do {
+ //
+ // Get the recycled transmit buffer status.
+ //
+ Snp->GetStatus (Snp, NULL, &TxBuf);
+
+ if (!EFI_ERROR (gBS->CheckEvent (MnpServiceData->TxTimeoutEvent))) {
+
+ Status = EFI_TIMEOUT;
+ break;
+ }
+ } while (TxBuf == NULL);
+
+ if ((Status == EFI_SUCCESS) || (Status == EFI_TIMEOUT)) {
+
+ break;
+ } else {
+ //
+ // Status is EFI_NOT_READY. Restart the timer event and call Snp->Transmit again.
+ //
+ gBS->SetTimer (
+ MnpServiceData->TxTimeoutEvent,
+ TimerRelative,
+ MNP_TX_TIMEOUT_TIME
+ );
+ }
+ }
+
+ //
+ // Cancel the timer event.
+ //
+ gBS->SetTimer (MnpServiceData->TxTimeoutEvent, TimerCancel, 0);
+
+SIGNAL_TOKEN:
+
+ Token->Status = Status;
+ gBS->SignalEvent (Token->Event);
+
+ return EFI_SUCCESS;
+}
+
+
+/**
+ Try to deliver the received packet to the instance.
+
+ @param 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 MNP_INSTANCE_DATA *Instance
+ )
+{
+ MNP_SERVICE_DATA *MnpServiceData;
+ MNP_RXDATA_WRAP *RxDataWrap;
+ NET_BUF *DupNbuf;
+ EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData;
+ EFI_SIMPLE_NETWORK_MODE *SnpMode;
+ EFI_MANAGED_NETWORK_COMPLETION_TOKEN *RxToken;
+
+ MnpServiceData = Instance->MnpServiceData;
+ NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
+
+ if (NetMapIsEmpty (&Instance->RxTokenMap) || NetListIsEmpty (&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 (MnpServiceData);
+ if (DupNbuf == NULL) {
+ MNP_DEBUG_WARN (("MnpDeliverPacket: Failed to allocate a free Nbuf.\n"));
+
+ return EFI_OUT_OF_RESOURCES;
+ }
+
+ //
+ // Duplicate the net buffer.
+ //
+ NetbufDuplicate (RxDataWrap->Nbuf, DupNbuf, 0);
+ MnpFreeNbuf (MnpServiceData, RxDataWrap->Nbuf);
+ RxDataWrap->Nbuf = DupNbuf;
+ }
+
+ //
+ // All resources are OK, remove the packet from the queue.
+ //
+ NetListRemoveHead (&Instance->RcvdPacketQueue);
+ Instance->RcvdPacketQueueSize--;
+
+ RxData = &RxDataWrap->RxData;
+ SnpMode = MnpServiceData->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.
+ //
+ NetListInsertTail (&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 MnpServiceData Pointer to the mnp service context data.
+
+ @return None.
+
+**/
+STATIC
+VOID
+MnpDeliverPacket (
+ IN MNP_SERVICE_DATA *MnpServiceData
+ )
+{
+ NET_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 Event The event this notify function registered to.
+ @param Context Pointer to the context data registerd to the Event.
+
+ @return None.
+
+**/
+VOID
+EFIAPI
+MnpRecycleRxData (
+ IN EFI_EVENT Event,
+ IN VOID *Context
+ )
+{
+ MNP_RXDATA_WRAP *RxDataWrap;
+ MNP_SERVICE_DATA *MnpServiceData;
+
+ ASSERT (Context != NULL);
+
+ RxDataWrap = (MNP_RXDATA_WRAP *) Context;
+ NET_CHECK_SIGNATURE (RxDataWrap->Instance, MNP_INSTANCE_DATA_SIGNATURE);
+
+ ASSERT (RxDataWrap->Nbuf != NULL);
+
+ MnpServiceData = RxDataWrap->Instance->MnpServiceData;
+ NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
+
+ //
+ // Free this Nbuf.
+ //
+ MnpFreeNbuf (MnpServiceData, RxDataWrap->Nbuf);
+ RxDataWrap->Nbuf = NULL;
+
+ //
+ // Close the recycle event.
+ //
+ gBS->CloseEvent (RxDataWrap->RxData.RecycleEvent);
+
+ //
+ // Remove this Wrap entry from the list.
+ //
+ NetListRemoveEntry (&RxDataWrap->WrapEntry);
+
+ NetFreePool (RxDataWrap);
+}
+
+
+/**
+ Queue the received packet into instance's receive queue.
+
+ @param Instance Pointer to the mnp instance context data.
+ @param RxDataWrap Pointer to the Wrap structure containing the
+ received data and other information.
+
+ @return None.
+
+**/
+STATIC
+VOID
+MnpQueueRcvdPacket (
+ IN MNP_INSTANCE_DATA *Instance,
+ IN 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) {
+
+ MNP_DEBUG_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.
+ //
+ NetListInsertTail (&Instance->RcvdPacketQueue, &RxDataWrap->WrapEntry);
+ Instance->RcvdPacketQueueSize++;
+}
+
+
+/**
+ Match the received packet with the instance receive filters.
+
+ @param Instance Pointer to the mnp instance context data.
+ @param RxData Pointer to the EFI_MANAGED_NETWORK_RECEIVE_DATA.
+ @param 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 PktAttr The received packets attribute.
+
+ @return The received packet matches the instance's receive filters or not.
+
+**/
+STATIC
+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;
+ NET_LIST_ENTRY *Entry;
+ MNP_GROUP_CONTROL_BLOCK *GroupCtrlBlk;
+
+ NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
+
+ ConfigData = &Instance->ConfigData;
+
+ if (ConfigData->EnablePromiscuousReceive) {
+ //
+ // Always match if this instance is configured to be promiscuous.
+ //
+ return TRUE;
+ }
+ //
+ // Check the protocol type.
+ //
+ if ((ConfigData->ProtocolTypeFilter != 0) && (ConfigData->ProtocolTypeFilter != RxData->ProtocolType)) {
+ return FALSE;
+ }
+
+ //
+ // 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 MnpServiceData Pointer to the mnp service context data.
+ @param Nbuf Pointer to the net buffer holding the received
+ packet.
+ @param RxData Pointer to the buffer used to save the analysed
+ result in EFI_MANAGED_NETWORK_RECEIVE_DATA.
+ @param 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 PktAttr Pointer to the buffer used to save the analysed
+ packet attribute.
+
+ @return None.
+
+**/
+STATIC
+VOID
+MnpAnalysePacket (
+ IN MNP_SERVICE_DATA *MnpServiceData,
+ IN NET_BUF *Nbuf,
+ IN EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData,
+ OUT MNP_GROUP_ADDRESS **GroupAddress,
+ OUT UINT8 *PktAttr
+ )
+{
+ EFI_SIMPLE_NETWORK_MODE *SnpMode;
+ UINT8 *BufPtr;
+ NET_LIST_ENTRY *Entry;
+
+ SnpMode = MnpServiceData->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, &MnpServiceData->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 (MnpServiceData->PromiscuousCount == 0) {
+ //
+ // Skip the below code, there is no receiver of this packet.
+ //
+ return ;
+ }
+ }
+ } else {
+ RxData->PromiscuousFlag = TRUE;
+ }
+ }
+
+ NetZeroMem (&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 Instance Pointer to the mnp instance context data.
+ @param RxData Pointer to the receive data to wrap.
+
+ @return Pointer to a MNP_RXDATA_WRAP which wraps the RxData.
+
+**/
+STATIC
+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 = NetAllocatePool (sizeof (MNP_RXDATA_WRAP));
+ if (RxDataWrap == NULL) {
+ MNP_DEBUG_ERROR (("MnpDispatchPacket: Failed to allocate a MNP_RXDATA_WRAP.\n"));
+ return NULL;
+ }
+
+ RxDataWrap->Instance = Instance;
+
+ //
+ // Fill the RxData in RxDataWrap,
+ //
+ RxDataWrap->RxData = *RxData;
+
+ //
+ // Create the recycle event.
+ //
+ Status = gBS->CreateEvent (
+ EVT_NOTIFY_SIGNAL,
+ NET_TPL_RECYCLE,
+ MnpRecycleRxData,
+ RxDataWrap,
+ &RxDataWrap->RxData.RecycleEvent
+ );
+ if (EFI_ERROR (Status)) {
+
+ MNP_DEBUG_ERROR (("MnpDispatchPacket: gBS->CreateEvent failed, %r.\n", Status));
+ NetFreePool (RxDataWrap);
+ return NULL;
+ }
+
+ return RxDataWrap;
+}
+
+
+/**
+ Enqueue the received the packets to the instances belonging to the
+ MnpServiceData.
+
+ @param MnpServiceData Pointer to the mnp service context data.
+ @param Nbuf Pointer to the net buffer representing the received
+ packet.
+
+ @return None.
+
+**/
+STATIC
+VOID
+MnpEnqueuePacket (
+ IN MNP_SERVICE_DATA *MnpServiceData,
+ IN NET_BUF *Nbuf
+ )
+{
+ NET_LIST_ENTRY *Entry;
+ MNP_INSTANCE_DATA *Instance;
+ EFI_MANAGED_NETWORK_RECEIVE_DATA RxData;
+ UINT8 PktAttr;
+ MNP_GROUP_ADDRESS *GroupAddress;
+ MNP_RXDATA_WRAP *RxDataWrap;
+
+ //
+ // First, analyse the packet header.
+ //
+ MnpAnalysePacket (MnpServiceData, Nbuf, &RxData, &GroupAddress, &PktAttr);
+
+ if (RxData.PromiscuousFlag && (MnpServiceData->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 MnpServiceData Pointer to the mnp service 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 MNP_SERVICE_DATA *MnpServiceData
+ )
+{
+ EFI_STATUS Status;
+ EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
+ NET_BUF *Nbuf;
+ UINT8 *BufPtr;
+ UINTN BufLen;
+ UINTN HeaderSize;
+ UINT32 Trimmed;
+
+ NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
+
+ Snp = MnpServiceData->Snp;
+ if (Snp->Mode->State != EfiSimpleNetworkInitialized) {
+ //
+ // The simple network protocol is not started.
+ //
+ return EFI_NOT_STARTED;
+ }
+
+ if (NetListIsEmpty (&MnpServiceData->ChildrenList)) {
+ //
+ // There is no child, no need to receive packets.
+ //
+ return EFI_SUCCESS;
+ }
+
+ if (MnpServiceData->RxNbufCache == NULL) {
+ //
+ // Try to get a new buffer as there may be buffers recycled.
+ //
+ MnpServiceData->RxNbufCache = MnpAllocNbuf (MnpServiceData);
+
+ if (MnpServiceData->RxNbufCache == NULL) {
+ //
+ // No availabe buffer in the buffer pool.
+ //
+ return EFI_DEVICE_ERROR;
+ }
+
+ NetbufAllocSpace (
+ MnpServiceData->RxNbufCache,
+ MnpServiceData->BufferLength,
+ NET_BUF_TAIL
+ );
+ }
+
+ Nbuf = MnpServiceData->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) {
+ MNP_DEBUG_ERROR (("MnpReceivePacket: Snp->Receive() = %r.\n", Status));
+ }
+ );
+
+ return Status;
+ }
+
+ //
+ // Sanity check.
+ //
+ if ((HeaderSize != Snp->Mode->MediaHeaderSize) || (BufLen < HeaderSize)) {
+
+ MNP_DEBUG_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);
+ }
+
+ //
+ // 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 (MnpServiceData, Nbuf);
+
+ Nbuf = MnpAllocNbuf (MnpServiceData);
+ MnpServiceData->RxNbufCache = Nbuf;
+ if (Nbuf == NULL) {
+ MNP_DEBUG_ERROR (("MnpReceivePacket: Alloc packet for receiving cache failed.\n"));
+ return EFI_DEVICE_ERROR;
+ }
+
+ NetbufAllocSpace (Nbuf, MnpServiceData->BufferLength, NET_BUF_TAIL);
+ } else {
+ //
+ // No receiver for this packet.
+ //
+ NetbufAllocSpace (Nbuf, Trimmed, NET_BUF_TAIL);
+ goto EXIT;
+ }
+ //
+ // Deliver the queued packets.
+ //
+ MnpDeliverPacket (MnpServiceData);
+
+EXIT:
+
+ ASSERT (Nbuf->TotalSize == MnpServiceData->BufferLength);
+
+ return Status;
+}
+
+
+/**
+ Remove the received packets if timeout occurs.
+
+ @param Event The event this notify function registered to.
+ @param Context Pointer to the context data registered to the
+ event.
+
+ @return None.
+
+**/
+VOID
+EFIAPI
+MnpCheckPacketTimeout (
+ IN EFI_EVENT Event,
+ IN VOID *Context
+ )
+{
+ MNP_SERVICE_DATA *MnpServiceData;
+ NET_LIST_ENTRY *Entry;
+ NET_LIST_ENTRY *RxEntry;
+ NET_LIST_ENTRY *NextEntry;
+ MNP_INSTANCE_DATA *Instance;
+ MNP_RXDATA_WRAP *RxDataWrap;
+ EFI_TPL OldTpl;
+
+ MnpServiceData = (MNP_SERVICE_DATA *) Context;
+ 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);
+
+ 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 = NET_RAISE_TPL (NET_TPL_RECYCLE);
+
+ NET_LIST_FOR_EACH_SAFE (RxEntry, NextEntry, &Instance->RcvdPacketQueue) {
+
+ RxDataWrap = NET_LIST_USER_STRUCT (RxEntry, MNP_RXDATA_WRAP, WrapEntry);
+
+ if (RxDataWrap->TimeoutTick >= MNP_TIMEOUT_CHECK_INTERVAL) {
+
+ RxDataWrap->TimeoutTick -= MNP_TIMEOUT_CHECK_INTERVAL;
+ } else {
+ //
+ // Drop the timeout packet.
+ //
+ MNP_DEBUG_WARN (("MnpCheckPacketTimeout: Received packet timeout.\n"));
+ MnpRecycleRxData (NULL, RxDataWrap);
+ Instance->RcvdPacketQueueSize--;
+ }
+ }
+
+ NET_RESTORE_TPL (OldTpl);
+ }
+}
+
+
+/**
+ Poll to receive the packets from Snp. This function is either called by upperlayer
+ protocols/applications or the system poll timer notify mechanism.
+
+ @param Event The event this notify function registered to.
+ @param Context Pointer to the context data registered to the
+ event.
+
+ @return None.
+
+**/
+VOID
+EFIAPI
+MnpSystemPoll (
+ IN EFI_EVENT Event,
+ IN VOID *Context
+ )
+{
+ MNP_SERVICE_DATA *MnpServiceData;
+
+ MnpServiceData = (MNP_SERVICE_DATA *) Context;
+ NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
+
+ //
+ // Try to receive packets from Snp.
+ //
+ MnpReceivePacket (MnpServiceData);
+}
diff --git a/MdeModulePkg/Universal/Network/MnpDxe/MnpMain.c b/MdeModulePkg/Universal/Network/MnpDxe/MnpMain.c
new file mode 100644
index 0000000000..02c0065c34
--- /dev/null
+++ b/MdeModulePkg/Universal/Network/MnpDxe/MnpMain.c
@@ -0,0 +1,655 @@
+/** @file
+
+Copyright (c) 2005 - 2007, Intel Corporation
+All rights reserved. This program and the accompanying materials
+are licensed and made available under the terms and conditions of the BSD License
+which accompanies this distribution. The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+Module Name:
+
+ MnpMain.c
+
+Abstract:
+
+ Implementation of Managed Network Protocol public services.
+
+
+**/
+
+#include <Library/BaseMemoryLib.h>
+#include <Library/BaseLib.h>
+
+#include "MnpImpl.h"
+
+
+/**
+ Get configuration data of this instance.
+
+ @param This Pointer to the Managed Network Protocol.
+ @param MnpConfigData Pointer to strorage for MNP operational
+ parameters.
+ @param SnpModeData Pointer to strorage for SNP operational
+ parameters.
+
+ @retval EFI_SUCCESS The operation completed successfully.
+ @retval EFI_INVALID_PARAMETER This is NULL.
+ @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.
+
+**/
+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;
+
+ if (This == NULL) {
+
+ return EFI_INVALID_PARAMETER;
+ }
+
+ Instance = MNP_INSTANCE_DATA_FROM_THIS (This);
+
+ OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);
+
+ if (MnpConfigData != NULL) {
+ //
+ // Copy the instance configuration data.
+ //
+ *MnpConfigData = Instance->ConfigData;
+ }
+
+ if (SnpModeData != NULL) {
+ //
+ // Copy the underlayer Snp mode data.
+ //
+ Snp = Instance->MnpServiceData->Snp;
+ *SnpModeData = *(Snp->Mode);
+ }
+
+ if (!Instance->Configured) {
+ Status = EFI_NOT_STARTED;
+ } else {
+ Status = EFI_SUCCESS;
+ }
+
+ NET_RESTORE_TPL (OldTpl);
+
+ return Status;
+}
+
+
+/**
+ Set or clear the operational parameters for the MNP child driver.
+
+ @param This Pointer to the Managed Network Protocol.
+ @param MnpConfigData Pointer to the 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.
+
+ @retval EFI_SUCCESS The operation completed successfully.
+ @retval EFI_INVALID_PARAMETER One or more parameter is invalid.
+ @retval EFI_OUT_OF_RESOURCES Required system resources (usually memory) could
+ not be allocated.
+ @retval EFI_UNSUPPORTED EnableReceiveTimestamps is TRUE, this
+ implementation doesn't support it.
+ @retval EFI_DEVICE_ERROR An unexpected network or system error occurred.
+ @retval Other 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 = NET_RAISE_TPL (NET_TPL_LOCK);
+
+ 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:
+ NET_RESTORE_TPL (OldTpl);
+
+ return Status;
+}
+
+
+/**
+ Translate a multicast IP address to a multicast hardware (MAC) address.
+
+ @param This Pointer to the Managed Network Protocol.
+ @param Ipv6Flag Set to TRUE if IpAddress is an IPv6 multicast
+ address. Set to FALSE if IpAddress is an IPv4
+ multicast address.
+ @param IpAddress Pointer to the multicast IP address to convert.
+ @param MacAddress Pointer to the resulting multicast MAC address.
+
+ @retval EFI_SUCCESS The operation completed successfully.
+ @retval EFI_INVALID_PARAMETER One or more parameter is invalid.
+ @retval EFI_NOT_STARTED This MNP child driver instance has not been
+ configured.
+ @retval EFI_UNSUPPORTED Ipv6Flag is TRUE, this implementation doesn't
+ supported it.
+ @retval EFI_DEVICE_ERROR An unexpected network or system error occurred.
+ @retval Other 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;
+
+ if ((This == NULL) || (IpAddress == NULL) || (MacAddress == NULL)) {
+
+ return EFI_INVALID_PARAMETER;
+ }
+
+ if (Ipv6Flag) {
+ //
+ // Currently IPv6 isn't supported.
+ //
+ return EFI_UNSUPPORTED;
+ }
+
+ if (!IP4_IS_MULTICAST (EFI_NTOHL (*IpAddress))) {
+ //
+ // The IPv4 address passed in is not a multicast address.
+ //
+ return EFI_INVALID_PARAMETER;
+ }
+
+ Instance = MNP_INSTANCE_DATA_FROM_THIS (This);
+
+ OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);
+
+ if (!Instance->Configured) {
+
+ Status = EFI_NOT_STARTED;
+ goto ON_EXIT;
+ }
+
+ Snp = Instance->MnpServiceData->Snp;
+ ASSERT (Snp != NULL);
+
+ if (Snp->Mode->IfType == NET_IFTYPE_ETHERNET) {
+ //
+ // Translate the IPv4 address into a multicast MAC address if the NIC is an
+ // ethernet NIC.
+ //
+ MacAddress->Addr[0] = 0x01;
+ MacAddress->Addr[1] = 0x00;
+ MacAddress->Addr[2] = 0x5E;
+ MacAddress->Addr[3] = IpAddress->v4.Addr[1] & 0x7F;
+ MacAddress->Addr[4] = IpAddress->v4.Addr[2];
+ MacAddress->Addr[5] = IpAddress->v4.Addr[3];
+
+ Status = EFI_SUCCESS;
+ } else {
+ //
+ // Invoke Snp to translate the multicast IP address.
+ //
+ Status = Snp->MCastIpToMac (
+ Snp,
+ Ipv6Flag,
+ IpAddress,
+ MacAddress
+ );
+ }
+
+ON_EXIT:
+ NET_RESTORE_TPL (OldTpl);
+
+ return Status;
+}
+
+
+/**
+ Enable or disable receie filters for multicast address.
+
+ @param This Pointer to the Managed Network Protocol.
+ @param JoinFlag Set to TRUE to join this multicast group. Set to
+ FALSE to leave this multicast group.
+ @param MacAddress Pointer to the multicast MAC group (address) to
+ join or leave.
+
+ @retval EFI_SUCCESS The operation completed successfully.
+ @retval EFI_INVALID_PARAMETER One or more parameter is invalid
+ @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.
+ @retval Other 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;
+ NET_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->Snp->Mode;
+
+ OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);
+
+ 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 mulitcast receive. OR
+ // the passed in MacAddress is not a mutlticast 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 == NetCompareMem (
+ 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 (NetListIsEmpty (&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:
+ NET_RESTORE_TPL (OldTpl);
+
+ return Status;
+}
+
+
+/**
+ Place an outgoing packet into the transmit queue.
+
+ @param This Pointer to the Managed Network Protocol.
+ @param Token Pointer to a token associated with the transmit
+ data descriptor.
+
+ @retval EFI_SUCCESS The operation completed successfully.
+ @retval EFI_INVALID_PARAMETER One or more parameter is invalid
+ @retval EFI_NOT_STARTED This MNP child driver instance has not been
+ configured.
+ @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 = NET_RAISE_TPL (NET_TPL_LOCK);
+
+ 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
+ //
+ MnpBuildTxPacket (MnpServiceData, Token->Packet.TxData, &PktBuf, &PktLen);
+
+ //
+ // OK, send the packet synchronously.
+ //
+ Status = MnpSyncSendPacket (MnpServiceData, PktBuf, PktLen, Token);
+
+ON_EXIT:
+ NET_RESTORE_TPL (OldTpl);
+
+ return Status;
+}
+
+
+/**
+ Place an asynchronous receiving request into the receiving queue.
+
+ @param This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL
+ instance.
+ @param Token Pointer to a token associated with the receive
+ data descriptor.
+
+ @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 parameter is invalid.
+ @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 = NET_RAISE_TPL (NET_TPL_LOCK);
+
+ 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);
+ }
+
+ON_EXIT:
+ NET_RESTORE_TPL (OldTpl);
+
+ return Status;
+}
+
+
+/**
+ Abort a pending transmit or receive request.
+
+ @param This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL
+ instance.
+ @param 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.
+ @retval EFI_NOT_STARTED This MNP child driver instance has not been
+ configured.
+ @retval EFI_INVALID_PARAMETER This is NULL.
+ @retval EFI_NOT_FOUND 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 = NET_RAISE_TPL (NET_TPL_LOCK);
+
+ 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;
+ }
+
+ON_EXIT:
+ NET_RESTORE_TPL (OldTpl);
+
+ return Status;
+}
+
+
+/**
+ Poll the network interface to do transmit/receive work.
+
+ @param 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.
+ @retval EFI_TIMEOUT Data was dropped out of the transmit and/or
+ receive queue.
+
+**/
+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 = NET_RAISE_TPL (NET_TPL_LOCK);
+
+ if (!Instance->Configured) {
+ Status = EFI_NOT_STARTED;
+ goto ON_EXIT;
+ }
+
+ //
+ // Try to receive packets.
+ //
+ Status = MnpReceivePacket (Instance->MnpServiceData);
+
+ON_EXIT:
+ NET_RESTORE_TPL (OldTpl);
+
+ return Status;
+}