]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/MnpDxe/MnpIo.c
NetworkPkg: Move Network library and drivers from MdeModulePkg to NetworkPkg
[mirror_edk2.git] / NetworkPkg / MnpDxe / MnpIo.c
diff --git a/NetworkPkg/MnpDxe/MnpIo.c b/NetworkPkg/MnpDxe/MnpIo.c
new file mode 100644 (file)
index 0000000..56405d6
--- /dev/null
@@ -0,0 +1,1133 @@
+/** @file\r
+  Implementation of Managed Network Protocol I/O functions.\r
+\r
+Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include "MnpImpl.h"\r
+#include "MnpVlan.h"\r
+\r
+/**\r
+  Validates the Mnp transmit token.\r
+\r
+  @param[in]  Instance            Pointer to the Mnp instance context data.\r
+  @param[in]  Token               Pointer to the transmit token to check.\r
+\r
+  @return The Token is valid or not.\r
+\r
+**/\r
+BOOLEAN\r
+MnpIsValidTxToken (\r
+  IN MNP_INSTANCE_DATA                       *Instance,\r
+  IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN    *Token\r
+  )\r
+{\r
+  MNP_SERVICE_DATA                  *MnpServiceData;\r
+  EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData;\r
+  UINT32                            Index;\r
+  UINT32                            TotalLength;\r
+  EFI_MANAGED_NETWORK_FRAGMENT_DATA *FragmentTable;\r
+\r
+  MnpServiceData = Instance->MnpServiceData;\r
+  NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);\r
+\r
+  TxData = Token->Packet.TxData;\r
+\r
+  if ((Token->Event == NULL) || (TxData == NULL) || (TxData->FragmentCount == 0)) {\r
+    //\r
+    // The token is invalid if the Event is NULL, or the TxData is NULL, or\r
+    // the fragment count is zero.\r
+    //\r
+    DEBUG ((EFI_D_WARN, "MnpIsValidTxToken: Invalid Token.\n"));\r
+    return FALSE;\r
+  }\r
+\r
+  if ((TxData->DestinationAddress != NULL) && (TxData->HeaderLength != 0)) {\r
+    //\r
+    // The token is invalid if the HeaderLength isn't zero while the DestinationAddress\r
+    // is NULL (The destination address is already put into the packet).\r
+    //\r
+    DEBUG ((EFI_D_WARN, "MnpIsValidTxToken: DestinationAddress isn't NULL, HeaderLength must be 0.\n"));\r
+    return FALSE;\r
+  }\r
+\r
+  TotalLength   = 0;\r
+  FragmentTable = TxData->FragmentTable;\r
+  for (Index = 0; Index < TxData->FragmentCount; Index++) {\r
+\r
+    if ((FragmentTable[Index].FragmentLength == 0) || (FragmentTable[Index].FragmentBuffer == NULL)) {\r
+      //\r
+      // The token is invalid if any FragmentLength is zero or any FragmentBuffer is NULL.\r
+      //\r
+      DEBUG ((EFI_D_WARN, "MnpIsValidTxToken: Invalid FragmentLength or FragmentBuffer.\n"));\r
+      return FALSE;\r
+    }\r
+\r
+    TotalLength += FragmentTable[Index].FragmentLength;\r
+  }\r
+\r
+  if ((TxData->DestinationAddress == NULL) && (FragmentTable[0].FragmentLength < TxData->HeaderLength)) {\r
+    //\r
+    // Media header is split between fragments.\r
+    //\r
+    return FALSE;\r
+  }\r
+\r
+  if (TotalLength != (TxData->DataLength + TxData->HeaderLength)) {\r
+    //\r
+    // The length calculated from the fragment information doesn't equal to the\r
+    // sum of the DataLength and the HeaderLength.\r
+    //\r
+    DEBUG ((EFI_D_WARN, "MnpIsValidTxData: Invalid Datalength compared with the sum of fragment length.\n"));\r
+    return FALSE;\r
+  }\r
+\r
+  if (TxData->DataLength > MnpServiceData->Mtu) {\r
+    //\r
+    // The total length is larger than the MTU.\r
+    //\r
+    DEBUG ((EFI_D_WARN, "MnpIsValidTxData: TxData->DataLength exceeds Mtu.\n"));\r
+    return FALSE;\r
+  }\r
+\r
+  return TRUE;\r
+}\r
+\r
+/**\r
+  Build the packet to transmit from the TxData passed in.\r
+\r
+  @param[in]   MnpServiceData      Pointer to the mnp service context data.\r
+  @param[in]   TxData              Pointer to the transmit data containing the information\r
+                                   to build the packet.\r
+  @param[out]  PktBuf              Pointer to record the address of the packet.\r
+  @param[out]  PktLen              Pointer to a UINT32 variable used to record the packet's\r
+                                   length.\r
+\r
+  @retval EFI_SUCCESS           TxPackage is built.\r
+  @retval EFI_OUT_OF_RESOURCES  The deliver fails due to lack of memory resource.\r
+\r
+**/\r
+EFI_STATUS\r
+MnpBuildTxPacket (\r
+  IN     MNP_SERVICE_DATA                    *MnpServiceData,\r
+  IN     EFI_MANAGED_NETWORK_TRANSMIT_DATA   *TxData,\r
+     OUT UINT8                               **PktBuf,\r
+     OUT UINT32                              *PktLen\r
+  )\r
+{\r
+  EFI_SIMPLE_NETWORK_MODE *SnpMode;\r
+  UINT8                   *DstPos;\r
+  UINT16                  Index;\r
+  MNP_DEVICE_DATA         *MnpDeviceData;\r
+  UINT8                   *TxBuf;\r
+\r
+  MnpDeviceData = MnpServiceData->MnpDeviceData;\r
+\r
+  TxBuf = MnpAllocTxBuf (MnpDeviceData);\r
+  if (TxBuf == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  //\r
+  // Reserve space for vlan tag if needed.\r
+  //\r
+  if (MnpServiceData->VlanId != 0) {\r
+    *PktBuf = TxBuf + NET_VLAN_TAG_LEN;\r
+  } else {\r
+    *PktBuf = TxBuf;\r
+  }\r
+\r
+  if ((TxData->DestinationAddress == NULL) && (TxData->FragmentCount == 1)) {\r
+    CopyMem (\r
+        *PktBuf,\r
+        TxData->FragmentTable[0].FragmentBuffer,\r
+        TxData->FragmentTable[0].FragmentLength\r
+        );\r
+\r
+    *PktLen = TxData->FragmentTable[0].FragmentLength;\r
+  } else {\r
+    //\r
+    // Either media header isn't in FragmentTable or there is more than\r
+    // one fragment, copy the data into the packet buffer. Reserve the\r
+    // media header space if necessary.\r
+    //\r
+    SnpMode = MnpDeviceData->Snp->Mode;\r
+    DstPos  = *PktBuf;\r
+    *PktLen = 0;\r
+    if (TxData->DestinationAddress != NULL) {\r
+      //\r
+      // If dest address is not NULL, move DstPos to reserve space for the\r
+      // media header. Add the media header length to buflen.\r
+      //\r
+      DstPos += SnpMode->MediaHeaderSize;\r
+      *PktLen += SnpMode->MediaHeaderSize;\r
+    }\r
+\r
+    for (Index = 0; Index < TxData->FragmentCount; Index++) {\r
+      //\r
+      // Copy the data.\r
+      //\r
+      CopyMem (\r
+        DstPos,\r
+        TxData->FragmentTable[Index].FragmentBuffer,\r
+        TxData->FragmentTable[Index].FragmentLength\r
+        );\r
+      DstPos += TxData->FragmentTable[Index].FragmentLength;\r
+    }\r
+\r
+    //\r
+    // Set the buffer length.\r
+    //\r
+    *PktLen += TxData->DataLength + TxData->HeaderLength;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  Synchronously send out the packet.\r
+\r
+  This functon places the packet buffer to SNP driver's tansmit queue. The packet\r
+  can be considered successfully sent out once SNP acccetp the packet, while the\r
+  packet buffer recycle is deferred for better performance.\r
+\r
+  @param[in]       MnpServiceData      Pointer to the mnp service context data.\r
+  @param[in]       Packet              Pointer to the pakcet buffer.\r
+  @param[in]       Length              The length of the packet.\r
+  @param[in, out]  Token               Pointer to the token the packet generated from.\r
+\r
+  @retval EFI_SUCCESS                  The packet is sent out.\r
+  @retval EFI_TIMEOUT                  Time out occurs, the packet isn't sent.\r
+  @retval EFI_DEVICE_ERROR             An unexpected network error occurs.\r
+\r
+**/\r
+EFI_STATUS\r
+MnpSyncSendPacket (\r
+  IN     MNP_SERVICE_DATA                        *MnpServiceData,\r
+  IN     UINT8                                   *Packet,\r
+  IN     UINT32                                  Length,\r
+  IN OUT EFI_MANAGED_NETWORK_COMPLETION_TOKEN    *Token\r
+  )\r
+{\r
+  EFI_STATUS                        Status;\r
+  EFI_SIMPLE_NETWORK_PROTOCOL       *Snp;\r
+  EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData;\r
+  UINT32                            HeaderSize;\r
+  MNP_DEVICE_DATA                   *MnpDeviceData;\r
+  UINT16                            ProtocolType;\r
+\r
+  MnpDeviceData = MnpServiceData->MnpDeviceData;\r
+  Snp           = MnpDeviceData->Snp;\r
+  TxData        = Token->Packet.TxData;\r
+  Token->Status = EFI_SUCCESS;\r
+  HeaderSize    = Snp->Mode->MediaHeaderSize - TxData->HeaderLength;\r
+\r
+  //\r
+  // Check media status before transmit packet.\r
+  // Note: media status will be updated by periodic timer MediaDetectTimer.\r
+  //\r
+  if (Snp->Mode->MediaPresentSupported && !Snp->Mode->MediaPresent) {\r
+    //\r
+    // Media not present, skip packet transmit and report EFI_NO_MEDIA\r
+    //\r
+    DEBUG ((EFI_D_WARN, "MnpSyncSendPacket: No network cable detected.\n"));\r
+    Token->Status = EFI_NO_MEDIA;\r
+    goto SIGNAL_TOKEN;\r
+  }\r
+\r
+\r
+  if (MnpServiceData->VlanId != 0) {\r
+    //\r
+    // Insert VLAN tag\r
+    //\r
+    MnpInsertVlanTag (MnpServiceData, TxData, &ProtocolType, &Packet, &Length);\r
+  } else {\r
+    ProtocolType = TxData->ProtocolType;\r
+  }\r
+\r
+  //\r
+  // Transmit the packet through SNP.\r
+  //\r
+  Status = Snp->Transmit (\r
+                  Snp,\r
+                  HeaderSize,\r
+                  Length,\r
+                  Packet,\r
+                  TxData->SourceAddress,\r
+                  TxData->DestinationAddress,\r
+                  &ProtocolType\r
+                  );\r
+  if (Status == EFI_NOT_READY) {\r
+    Status = MnpRecycleTxBuf (MnpDeviceData);\r
+    if (EFI_ERROR (Status)) {\r
+      Token->Status = EFI_DEVICE_ERROR;\r
+      goto SIGNAL_TOKEN;\r
+    }\r
+\r
+    Status = Snp->Transmit (\r
+                    Snp,\r
+                    HeaderSize,\r
+                    Length,\r
+                    Packet,\r
+                    TxData->SourceAddress,\r
+                    TxData->DestinationAddress,\r
+                    &ProtocolType\r
+                    );\r
+  }\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    Token->Status = EFI_DEVICE_ERROR;\r
+  }\r
+\r
+SIGNAL_TOKEN:\r
+\r
+  gBS->SignalEvent (Token->Event);\r
+\r
+  //\r
+  // Dispatch the DPC queued by the NotifyFunction of Token->Event.\r
+  //\r
+  DispatchDpc ();\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  Try to deliver the received packet to the instance.\r
+\r
+  @param[in, out]  Instance     Pointer to the mnp instance context data.\r
+\r
+  @retval EFI_SUCCESS           The received packet is delivered, or there is no\r
+                                packet to deliver, or there is no available receive\r
+                                token.\r
+  @retval EFI_OUT_OF_RESOURCES  The deliver fails due to lack of memory resource.\r
+\r
+**/\r
+EFI_STATUS\r
+MnpInstanceDeliverPacket (\r
+  IN OUT MNP_INSTANCE_DATA   *Instance\r
+  )\r
+{\r
+  MNP_DEVICE_DATA                       *MnpDeviceData;\r
+  MNP_RXDATA_WRAP                       *RxDataWrap;\r
+  NET_BUF                               *DupNbuf;\r
+  EFI_MANAGED_NETWORK_RECEIVE_DATA      *RxData;\r
+  EFI_SIMPLE_NETWORK_MODE               *SnpMode;\r
+  EFI_MANAGED_NETWORK_COMPLETION_TOKEN  *RxToken;\r
+\r
+  MnpDeviceData = Instance->MnpServiceData->MnpDeviceData;\r
+  NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE);\r
+\r
+  if (NetMapIsEmpty (&Instance->RxTokenMap) || IsListEmpty (&Instance->RcvdPacketQueue)) {\r
+    //\r
+    // No pending received data or no available receive token, return.\r
+    //\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  ASSERT (Instance->RcvdPacketQueueSize != 0);\r
+\r
+  RxDataWrap = NET_LIST_HEAD (&Instance->RcvdPacketQueue, MNP_RXDATA_WRAP, WrapEntry);\r
+  if (RxDataWrap->Nbuf->RefCnt > 2) {\r
+    //\r
+    // There are other instances share this Nbuf, duplicate to get a\r
+    // copy to allow the instance to do R/W operations.\r
+    //\r
+    DupNbuf = MnpAllocNbuf (MnpDeviceData);\r
+    if (DupNbuf == NULL) {\r
+      DEBUG ((EFI_D_WARN, "MnpDeliverPacket: Failed to allocate a free Nbuf.\n"));\r
+\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+\r
+    //\r
+    // Duplicate the net buffer.\r
+    //\r
+    NetbufDuplicate (RxDataWrap->Nbuf, DupNbuf, 0);\r
+    MnpFreeNbuf (MnpDeviceData, RxDataWrap->Nbuf);\r
+    RxDataWrap->Nbuf = DupNbuf;\r
+  }\r
+\r
+  //\r
+  // All resources are OK, remove the packet from the queue.\r
+  //\r
+  NetListRemoveHead (&Instance->RcvdPacketQueue);\r
+  Instance->RcvdPacketQueueSize--;\r
+\r
+  RxData  = &RxDataWrap->RxData;\r
+  SnpMode = MnpDeviceData->Snp->Mode;\r
+\r
+  //\r
+  // Set all the buffer pointers.\r
+  //\r
+  RxData->MediaHeader         = NetbufGetByte (RxDataWrap->Nbuf, 0, NULL);\r
+  RxData->DestinationAddress  = RxData->MediaHeader;\r
+  RxData->SourceAddress       = (UINT8 *) RxData->MediaHeader + SnpMode->HwAddressSize;\r
+  RxData->PacketData          = (UINT8 *) RxData->MediaHeader + SnpMode->MediaHeaderSize;\r
+\r
+  //\r
+  // Insert this RxDataWrap into the delivered queue.\r
+  //\r
+  InsertTailList (&Instance->RxDeliveredPacketQueue, &RxDataWrap->WrapEntry);\r
+\r
+  //\r
+  // Get the receive token from the RxTokenMap.\r
+  //\r
+  RxToken = NetMapRemoveHead (&Instance->RxTokenMap, NULL);\r
+\r
+  //\r
+  // Signal this token's event.\r
+  //\r
+  RxToken->Packet.RxData  = &RxDataWrap->RxData;\r
+  RxToken->Status         = EFI_SUCCESS;\r
+  gBS->SignalEvent (RxToken->Event);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  Deliver the received packet for the instances belonging to the MnpServiceData.\r
+\r
+  @param[in]  MnpServiceData        Pointer to the mnp service context data.\r
+\r
+**/\r
+VOID\r
+MnpDeliverPacket (\r
+  IN MNP_SERVICE_DATA    *MnpServiceData\r
+  )\r
+{\r
+  LIST_ENTRY        *Entry;\r
+  MNP_INSTANCE_DATA *Instance;\r
+\r
+  NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);\r
+\r
+  NET_LIST_FOR_EACH (Entry, &MnpServiceData->ChildrenList) {\r
+    Instance = NET_LIST_USER_STRUCT (Entry, MNP_INSTANCE_DATA, InstEntry);\r
+    NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);\r
+\r
+    //\r
+    // Try to deliver packet for this instance.\r
+    //\r
+    MnpInstanceDeliverPacket (Instance);\r
+  }\r
+}\r
+\r
+\r
+/**\r
+  Recycle the RxData and other resources used to hold and deliver the received\r
+  packet.\r
+\r
+  @param[in]  Event               The event this notify function registered to.\r
+  @param[in]  Context             Pointer to the context data registerd to the Event.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+MnpRecycleRxData (\r
+  IN EFI_EVENT     Event,\r
+  IN VOID          *Context\r
+  )\r
+{\r
+  MNP_RXDATA_WRAP *RxDataWrap;\r
+  MNP_DEVICE_DATA *MnpDeviceData;\r
+\r
+  ASSERT (Context != NULL);\r
+\r
+  RxDataWrap = (MNP_RXDATA_WRAP *) Context;\r
+  NET_CHECK_SIGNATURE (RxDataWrap->Instance, MNP_INSTANCE_DATA_SIGNATURE);\r
+\r
+  ASSERT (RxDataWrap->Nbuf != NULL);\r
+\r
+  MnpDeviceData = RxDataWrap->Instance->MnpServiceData->MnpDeviceData;\r
+  NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE);\r
+\r
+  //\r
+  // Free this Nbuf.\r
+  //\r
+  MnpFreeNbuf (MnpDeviceData, RxDataWrap->Nbuf);\r
+  RxDataWrap->Nbuf = NULL;\r
+\r
+  //\r
+  // Close the recycle event.\r
+  //\r
+  gBS->CloseEvent (RxDataWrap->RxData.RecycleEvent);\r
+\r
+  //\r
+  // Remove this Wrap entry from the list.\r
+  //\r
+  RemoveEntryList (&RxDataWrap->WrapEntry);\r
+\r
+  FreePool (RxDataWrap);\r
+}\r
+\r
+\r
+/**\r
+  Queue the received packet into instance's receive queue.\r
+\r
+  @param[in, out]  Instance        Pointer to the mnp instance context data.\r
+  @param[in, out]  RxDataWrap      Pointer to the Wrap structure containing the\r
+                                   received data and other information.\r
+**/\r
+VOID\r
+MnpQueueRcvdPacket (\r
+  IN OUT MNP_INSTANCE_DATA   *Instance,\r
+  IN OUT MNP_RXDATA_WRAP     *RxDataWrap\r
+  )\r
+{\r
+  MNP_RXDATA_WRAP *OldRxDataWrap;\r
+\r
+  NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);\r
+\r
+  //\r
+  // Check the queue size. If it exceeds the limit, drop one packet\r
+  // from the head.\r
+  //\r
+  if (Instance->RcvdPacketQueueSize == MNP_MAX_RCVD_PACKET_QUE_SIZE) {\r
+\r
+    DEBUG ((EFI_D_WARN, "MnpQueueRcvdPacket: Drop one packet bcz queue size limit reached.\n"));\r
+\r
+    //\r
+    // Get the oldest packet.\r
+    //\r
+    OldRxDataWrap = NET_LIST_HEAD (\r
+                      &Instance->RcvdPacketQueue,\r
+                      MNP_RXDATA_WRAP,\r
+                      WrapEntry\r
+                      );\r
+\r
+    //\r
+    // Recycle this OldRxDataWrap, this entry will be removed by the callee.\r
+    //\r
+    MnpRecycleRxData (NULL, (VOID *) OldRxDataWrap);\r
+    Instance->RcvdPacketQueueSize--;\r
+  }\r
+\r
+  //\r
+  // Update the timeout tick using the configured parameter.\r
+  //\r
+  RxDataWrap->TimeoutTick = Instance->ConfigData.ReceivedQueueTimeoutValue;\r
+\r
+  //\r
+  // Insert this Wrap into the instance queue.\r
+  //\r
+  InsertTailList (&Instance->RcvdPacketQueue, &RxDataWrap->WrapEntry);\r
+  Instance->RcvdPacketQueueSize++;\r
+}\r
+\r
+\r
+/**\r
+  Match the received packet with the instance receive filters.\r
+\r
+  @param[in]  Instance          Pointer to the mnp instance context data.\r
+  @param[in]  RxData            Pointer to the EFI_MANAGED_NETWORK_RECEIVE_DATA.\r
+  @param[in]  GroupAddress      Pointer to the GroupAddress, the GroupAddress is\r
+                                non-NULL and it contains the destination multicast\r
+                                mac address of the received packet if the packet\r
+                                destinated to a multicast mac address.\r
+  @param[in]  PktAttr           The received packets attribute.\r
+\r
+  @return The received packet matches the instance's receive filters or not.\r
+\r
+**/\r
+BOOLEAN\r
+MnpMatchPacket (\r
+  IN MNP_INSTANCE_DATA                   *Instance,\r
+  IN EFI_MANAGED_NETWORK_RECEIVE_DATA    *RxData,\r
+  IN MNP_GROUP_ADDRESS                   *GroupAddress OPTIONAL,\r
+  IN UINT8                               PktAttr\r
+  )\r
+{\r
+  EFI_MANAGED_NETWORK_CONFIG_DATA *ConfigData;\r
+  LIST_ENTRY                      *Entry;\r
+  MNP_GROUP_CONTROL_BLOCK         *GroupCtrlBlk;\r
+\r
+  NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);\r
+\r
+  ConfigData = &Instance->ConfigData;\r
+\r
+  //\r
+  // Check the protocol type.\r
+  //\r
+  if ((ConfigData->ProtocolTypeFilter != 0) && (ConfigData->ProtocolTypeFilter != RxData->ProtocolType)) {\r
+    return FALSE;\r
+  }\r
+\r
+  if (ConfigData->EnablePromiscuousReceive) {\r
+    //\r
+    // Always match if this instance is configured to be promiscuous.\r
+    //\r
+    return TRUE;\r
+  }\r
+\r
+  //\r
+  // The protocol type is matched, check receive filter, include unicast and broadcast.\r
+  //\r
+  if ((Instance->ReceiveFilter & PktAttr) != 0) {\r
+    return TRUE;\r
+  }\r
+\r
+  //\r
+  // Check multicast addresses.\r
+  //\r
+  if (ConfigData->EnableMulticastReceive && RxData->MulticastFlag) {\r
+\r
+    ASSERT (GroupAddress != NULL);\r
+\r
+    NET_LIST_FOR_EACH (Entry, &Instance->GroupCtrlBlkList) {\r
+\r
+      GroupCtrlBlk = NET_LIST_USER_STRUCT (Entry, MNP_GROUP_CONTROL_BLOCK, CtrlBlkEntry);\r
+      if (GroupCtrlBlk->GroupAddress == GroupAddress) {\r
+        //\r
+        // The instance is configured to receiveing packets destinated to this\r
+        // multicast address.\r
+        //\r
+        return TRUE;\r
+      }\r
+    }\r
+  }\r
+\r
+  //\r
+  // No match.\r
+  //\r
+  return FALSE;\r
+}\r
+\r
+\r
+/**\r
+  Analyse the received packets.\r
+\r
+  @param[in]       MnpServiceData    Pointer to the mnp service context data.\r
+  @param[in]       Nbuf              Pointer to the net buffer holding the received\r
+                                     packet.\r
+  @param[in, out]  RxData            Pointer to the buffer used to save the analysed\r
+                                     result in EFI_MANAGED_NETWORK_RECEIVE_DATA.\r
+  @param[out]      GroupAddress      Pointer to pointer to a MNP_GROUP_ADDRESS used to\r
+                                     pass out the address of the multicast address the\r
+                                     received packet destinated to.\r
+  @param[out]      PktAttr           Pointer to the buffer used to save the analysed\r
+                                     packet attribute.\r
+\r
+**/\r
+VOID\r
+MnpAnalysePacket (\r
+  IN     MNP_SERVICE_DATA                    *MnpServiceData,\r
+  IN     NET_BUF                             *Nbuf,\r
+  IN OUT EFI_MANAGED_NETWORK_RECEIVE_DATA    *RxData,\r
+     OUT MNP_GROUP_ADDRESS                   **GroupAddress,\r
+     OUT UINT8                               *PktAttr\r
+  )\r
+{\r
+  EFI_SIMPLE_NETWORK_MODE *SnpMode;\r
+  MNP_DEVICE_DATA         *MnpDeviceData;\r
+  UINT8                   *BufPtr;\r
+  LIST_ENTRY              *Entry;\r
+\r
+  MnpDeviceData = MnpServiceData->MnpDeviceData;\r
+  SnpMode       = MnpDeviceData->Snp->Mode;\r
+\r
+  //\r
+  // Get the packet buffer.\r
+  //\r
+  BufPtr = NetbufGetByte (Nbuf, 0, NULL);\r
+  ASSERT (BufPtr != NULL);\r
+\r
+  //\r
+  // Set the initial values.\r
+  //\r
+  RxData->BroadcastFlag   = FALSE;\r
+  RxData->MulticastFlag   = FALSE;\r
+  RxData->PromiscuousFlag = FALSE;\r
+  *PktAttr                = UNICAST_PACKET;\r
+\r
+  if (!NET_MAC_EQUAL (&SnpMode->CurrentAddress, BufPtr, SnpMode->HwAddressSize)) {\r
+    //\r
+    // This packet isn't destinated to our current mac address, it't not unicast.\r
+    //\r
+    *PktAttr = 0;\r
+\r
+    if (NET_MAC_EQUAL (&SnpMode->BroadcastAddress, BufPtr, SnpMode->HwAddressSize)) {\r
+      //\r
+      // It's broadcast.\r
+      //\r
+      RxData->BroadcastFlag = TRUE;\r
+      *PktAttr              = BROADCAST_PACKET;\r
+    } else if ((*BufPtr & 0x01) == 0x1) {\r
+      //\r
+      // It's multicast, try to match the multicast filters.\r
+      //\r
+      NET_LIST_FOR_EACH (Entry, &MnpDeviceData->GroupAddressList) {\r
+\r
+        *GroupAddress = NET_LIST_USER_STRUCT (Entry, MNP_GROUP_ADDRESS, AddrEntry);\r
+        if (NET_MAC_EQUAL (BufPtr, &((*GroupAddress)->Address), SnpMode->HwAddressSize)) {\r
+          RxData->MulticastFlag = TRUE;\r
+          break;\r
+        }\r
+      }\r
+\r
+      if (!RxData->MulticastFlag) {\r
+        //\r
+        // No match, set GroupAddress to NULL. This multicast packet must\r
+        // be the result of PROMISUCOUS or PROMISUCOUS_MULTICAST flag is on.\r
+        //\r
+        *GroupAddress           = NULL;\r
+        RxData->PromiscuousFlag = TRUE;\r
+\r
+        if (MnpDeviceData->PromiscuousCount == 0) {\r
+          //\r
+          // Skip the below code, there is no receiver of this packet.\r
+          //\r
+          return ;\r
+        }\r
+      }\r
+    } else {\r
+      RxData->PromiscuousFlag = TRUE;\r
+    }\r
+  }\r
+\r
+  ZeroMem (&RxData->Timestamp, sizeof (EFI_TIME));\r
+\r
+  //\r
+  // Fill the common parts of RxData.\r
+  //\r
+  RxData->PacketLength  = Nbuf->TotalSize;\r
+  RxData->HeaderLength  = SnpMode->MediaHeaderSize;\r
+  RxData->AddressLength = SnpMode->HwAddressSize;\r
+  RxData->DataLength    = RxData->PacketLength - RxData->HeaderLength;\r
+  RxData->ProtocolType  = NTOHS (*(UINT16 *) (BufPtr + 2 * SnpMode->HwAddressSize));\r
+}\r
+\r
+\r
+/**\r
+  Wrap the RxData.\r
+\r
+  @param[in]  Instance           Pointer to the mnp instance context data.\r
+  @param[in]  RxData             Pointer to the receive data to wrap.\r
+\r
+  @return Pointer to a MNP_RXDATA_WRAP which wraps the RxData.\r
+\r
+**/\r
+MNP_RXDATA_WRAP *\r
+MnpWrapRxData (\r
+  IN MNP_INSTANCE_DATA                   *Instance,\r
+  IN EFI_MANAGED_NETWORK_RECEIVE_DATA    *RxData\r
+  )\r
+{\r
+  EFI_STATUS      Status;\r
+  MNP_RXDATA_WRAP *RxDataWrap;\r
+\r
+  //\r
+  // Allocate memory.\r
+  //\r
+  RxDataWrap = AllocatePool (sizeof (MNP_RXDATA_WRAP));\r
+  if (RxDataWrap == NULL) {\r
+    DEBUG ((EFI_D_ERROR, "MnpDispatchPacket: Failed to allocate a MNP_RXDATA_WRAP.\n"));\r
+    return NULL;\r
+  }\r
+\r
+  RxDataWrap->Instance = Instance;\r
+\r
+  //\r
+  // Fill the RxData in RxDataWrap,\r
+  //\r
+  CopyMem (&RxDataWrap->RxData, RxData, sizeof (RxDataWrap->RxData));\r
+\r
+  //\r
+  // Create the recycle event.\r
+  //\r
+  Status = gBS->CreateEvent (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_NOTIFY,\r
+                  MnpRecycleRxData,\r
+                  RxDataWrap,\r
+                  &RxDataWrap->RxData.RecycleEvent\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "MnpDispatchPacket: gBS->CreateEvent failed, %r.\n", Status));\r
+\r
+    FreePool (RxDataWrap);\r
+    return NULL;\r
+  }\r
+\r
+  return RxDataWrap;\r
+}\r
+\r
+\r
+/**\r
+  Enqueue the received the packets to the instances belonging to the\r
+  MnpServiceData.\r
+\r
+  @param[in]  MnpServiceData    Pointer to the mnp service context data.\r
+  @param[in]  Nbuf              Pointer to the net buffer representing the received\r
+                                packet.\r
+\r
+**/\r
+VOID\r
+MnpEnqueuePacket (\r
+  IN MNP_SERVICE_DATA    *MnpServiceData,\r
+  IN NET_BUF             *Nbuf\r
+  )\r
+{\r
+  LIST_ENTRY                        *Entry;\r
+  MNP_INSTANCE_DATA                 *Instance;\r
+  EFI_MANAGED_NETWORK_RECEIVE_DATA  RxData;\r
+  UINT8                             PktAttr;\r
+  MNP_GROUP_ADDRESS                 *GroupAddress;\r
+  MNP_RXDATA_WRAP                   *RxDataWrap;\r
+\r
+\r
+  GroupAddress = NULL;\r
+  //\r
+  // First, analyse the packet header.\r
+  //\r
+  MnpAnalysePacket (MnpServiceData, Nbuf, &RxData, &GroupAddress, &PktAttr);\r
+\r
+  if (RxData.PromiscuousFlag && (MnpServiceData->MnpDeviceData->PromiscuousCount == 0)) {\r
+    //\r
+    // No receivers, no more action need.\r
+    //\r
+    return ;\r
+  }\r
+\r
+  //\r
+  // Iterate the children to find match.\r
+  //\r
+  NET_LIST_FOR_EACH (Entry, &MnpServiceData->ChildrenList) {\r
+\r
+    Instance = NET_LIST_USER_STRUCT (Entry, MNP_INSTANCE_DATA, InstEntry);\r
+    NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);\r
+\r
+    if (!Instance->Configured) {\r
+      continue;\r
+    }\r
+\r
+    //\r
+    // Check the packet against the instance receive filters.\r
+    //\r
+    if (MnpMatchPacket (Instance, &RxData, GroupAddress, PktAttr)) {\r
+      //\r
+      // Wrap the RxData.\r
+      //\r
+      RxDataWrap = MnpWrapRxData (Instance, &RxData);\r
+      if (RxDataWrap == NULL) {\r
+        continue;\r
+      }\r
+\r
+      //\r
+      // Associate RxDataWrap with Nbuf and increase the RefCnt.\r
+      //\r
+      RxDataWrap->Nbuf = Nbuf;\r
+      NET_GET_REF (RxDataWrap->Nbuf);\r
+\r
+      //\r
+      // Queue the packet into the instance queue.\r
+      //\r
+      MnpQueueRcvdPacket (Instance, RxDataWrap);\r
+    }\r
+  }\r
+}\r
+\r
+\r
+/**\r
+  Try to receive a packet and deliver it.\r
+\r
+  @param[in, out]  MnpDeviceData        Pointer to the mnp device context data.\r
+\r
+  @retval EFI_SUCCESS           add return value to function comment\r
+  @retval EFI_NOT_STARTED       The simple network protocol is not started.\r
+  @retval EFI_NOT_READY         No packet received.\r
+  @retval EFI_DEVICE_ERROR      An unexpected error occurs.\r
+\r
+**/\r
+EFI_STATUS\r
+MnpReceivePacket (\r
+  IN OUT MNP_DEVICE_DATA   *MnpDeviceData\r
+  )\r
+{\r
+  EFI_STATUS                  Status;\r
+  EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r
+  NET_BUF                     *Nbuf;\r
+  UINT8                       *BufPtr;\r
+  UINTN                       BufLen;\r
+  UINTN                       HeaderSize;\r
+  UINT32                      Trimmed;\r
+  MNP_SERVICE_DATA            *MnpServiceData;\r
+  UINT16                      VlanId;\r
+  BOOLEAN                     IsVlanPacket;\r
+\r
+  NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE);\r
+\r
+  Snp = MnpDeviceData->Snp;\r
+  if (Snp->Mode->State != EfiSimpleNetworkInitialized) {\r
+    //\r
+    // The simple network protocol is not started.\r
+    //\r
+    return EFI_NOT_STARTED;\r
+  }\r
+\r
+  if (MnpDeviceData->RxNbufCache == NULL) {\r
+    //\r
+    // Try to get a new buffer as there may be buffers recycled.\r
+    //\r
+    MnpDeviceData->RxNbufCache = MnpAllocNbuf (MnpDeviceData);\r
+\r
+    if (MnpDeviceData->RxNbufCache == NULL) {\r
+      //\r
+      // No available buffer in the buffer pool.\r
+      //\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
+\r
+    NetbufAllocSpace (\r
+      MnpDeviceData->RxNbufCache,\r
+      MnpDeviceData->BufferLength,\r
+      NET_BUF_TAIL\r
+      );\r
+  }\r
+\r
+  Nbuf    = MnpDeviceData->RxNbufCache;\r
+  BufLen  = Nbuf->TotalSize;\r
+  BufPtr  = NetbufGetByte (Nbuf, 0, NULL);\r
+  ASSERT (BufPtr != NULL);\r
+\r
+  //\r
+  // Receive packet through Snp.\r
+  //\r
+  Status = Snp->Receive (Snp, &HeaderSize, &BufLen, BufPtr, NULL, NULL, NULL);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG_CODE (\r
+      if (Status != EFI_NOT_READY) {\r
+        DEBUG ((EFI_D_WARN, "MnpReceivePacket: Snp->Receive() = %r.\n", Status));\r
+      }\r
+    );\r
+\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Sanity check.\r
+  //\r
+  if ((HeaderSize != Snp->Mode->MediaHeaderSize) || (BufLen < HeaderSize)) {\r
+    DEBUG (\r
+      (EFI_D_WARN,\r
+      "MnpReceivePacket: Size error, HL:TL = %d:%d.\n",\r
+      HeaderSize,\r
+      BufLen)\r
+      );\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  Trimmed = 0;\r
+  if (Nbuf->TotalSize != BufLen) {\r
+    //\r
+    // Trim the packet from tail.\r
+    //\r
+    Trimmed = NetbufTrim (Nbuf, Nbuf->TotalSize - (UINT32) BufLen, NET_BUF_TAIL);\r
+    ASSERT (Nbuf->TotalSize == BufLen);\r
+  }\r
+\r
+  VlanId = 0;\r
+  if (MnpDeviceData->NumberOfVlan != 0) {\r
+    //\r
+    // VLAN is configured, remove the VLAN tag if any\r
+    //\r
+    IsVlanPacket = MnpRemoveVlanTag (MnpDeviceData, Nbuf, &VlanId);\r
+  } else {\r
+    IsVlanPacket = FALSE;\r
+  }\r
+\r
+  MnpServiceData = MnpFindServiceData (MnpDeviceData, VlanId);\r
+  if (MnpServiceData == NULL) {\r
+    //\r
+    // VLAN is not set for this tagged frame, ignore this packet\r
+    //\r
+    if (Trimmed > 0) {\r
+      NetbufAllocSpace (Nbuf, Trimmed, NET_BUF_TAIL);\r
+    }\r
+\r
+    if (IsVlanPacket) {\r
+      NetbufAllocSpace (Nbuf, NET_VLAN_TAG_LEN, NET_BUF_HEAD);\r
+    }\r
+\r
+    goto EXIT;\r
+  }\r
+\r
+  //\r
+  // Enqueue the packet to the matched instances.\r
+  //\r
+  MnpEnqueuePacket (MnpServiceData, Nbuf);\r
+\r
+  if (Nbuf->RefCnt > 2) {\r
+    //\r
+    // RefCnt > 2 indicates there is at least one receiver of this packet.\r
+    // Free the current RxNbufCache and allocate a new one.\r
+    //\r
+    MnpFreeNbuf (MnpDeviceData, Nbuf);\r
+\r
+    Nbuf                       = MnpAllocNbuf (MnpDeviceData);\r
+    MnpDeviceData->RxNbufCache = Nbuf;\r
+    if (Nbuf == NULL) {\r
+      DEBUG ((EFI_D_ERROR, "MnpReceivePacket: Alloc packet for receiving cache failed.\n"));\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
+\r
+    NetbufAllocSpace (Nbuf, MnpDeviceData->BufferLength, NET_BUF_TAIL);\r
+  } else {\r
+    //\r
+    // No receiver for this packet.\r
+    //\r
+    if (Trimmed > 0) {\r
+      NetbufAllocSpace (Nbuf, Trimmed, NET_BUF_TAIL);\r
+    }\r
+    if (IsVlanPacket) {\r
+      NetbufAllocSpace (Nbuf, NET_VLAN_TAG_LEN, NET_BUF_HEAD);\r
+    }\r
+\r
+    goto EXIT;\r
+  }\r
+  //\r
+  // Deliver the queued packets.\r
+  //\r
+  MnpDeliverPacket (MnpServiceData);\r
+\r
+EXIT:\r
+\r
+  ASSERT (Nbuf->TotalSize == MnpDeviceData->BufferLength);\r
+\r
+  return Status;\r
+}\r
+\r
+\r
+/**\r
+  Remove the received packets if timeout occurs.\r
+\r
+  @param[in]  Event        The event this notify function registered to.\r
+  @param[in]  Context      Pointer to the context data registered to the event.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+MnpCheckPacketTimeout (\r
+  IN EFI_EVENT     Event,\r
+  IN VOID          *Context\r
+  )\r
+{\r
+  MNP_DEVICE_DATA   *MnpDeviceData;\r
+  MNP_SERVICE_DATA  *MnpServiceData;\r
+  LIST_ENTRY        *Entry;\r
+  LIST_ENTRY        *ServiceEntry;\r
+  LIST_ENTRY        *RxEntry;\r
+  LIST_ENTRY        *NextEntry;\r
+  MNP_INSTANCE_DATA *Instance;\r
+  MNP_RXDATA_WRAP   *RxDataWrap;\r
+  EFI_TPL           OldTpl;\r
+\r
+  MnpDeviceData = (MNP_DEVICE_DATA *) Context;\r
+  NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE);\r
+\r
+  NET_LIST_FOR_EACH (ServiceEntry, &MnpDeviceData->ServiceList) {\r
+    MnpServiceData = MNP_SERVICE_DATA_FROM_LINK (ServiceEntry);\r
+\r
+    NET_LIST_FOR_EACH (Entry, &MnpServiceData->ChildrenList) {\r
+\r
+      Instance = NET_LIST_USER_STRUCT (Entry, MNP_INSTANCE_DATA, InstEntry);\r
+      NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);\r
+\r
+      if (!Instance->Configured || (Instance->ConfigData.ReceivedQueueTimeoutValue == 0)) {\r
+        //\r
+        // This instance is not configured or there is no receive time out,\r
+        // just skip to the next instance.\r
+        //\r
+        continue;\r
+      }\r
+\r
+      OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
+\r
+      NET_LIST_FOR_EACH_SAFE (RxEntry, NextEntry, &Instance->RcvdPacketQueue) {\r
+\r
+        RxDataWrap = NET_LIST_USER_STRUCT (RxEntry, MNP_RXDATA_WRAP, WrapEntry);\r
+\r
+        //\r
+        // TimeoutTick unit is microsecond, MNP_TIMEOUT_CHECK_INTERVAL unit is 100ns.\r
+        //\r
+        if (RxDataWrap->TimeoutTick >= (MNP_TIMEOUT_CHECK_INTERVAL / 10)) {\r
+          RxDataWrap->TimeoutTick -= (MNP_TIMEOUT_CHECK_INTERVAL / 10);\r
+        } else {\r
+          //\r
+          // Drop the timeout packet.\r
+          //\r
+          DEBUG ((EFI_D_WARN, "MnpCheckPacketTimeout: Received packet timeout.\n"));\r
+          MnpRecycleRxData (NULL, RxDataWrap);\r
+          Instance->RcvdPacketQueueSize--;\r
+        }\r
+      }\r
+\r
+      gBS->RestoreTPL (OldTpl);\r
+    }\r
+  }\r
+}\r
+\r
+/**\r
+  Poll to update MediaPresent field in SNP ModeData by Snp->GetStatus().\r
+\r
+  @param[in]  Event        The event this notify function registered to.\r
+  @param[in]  Context      Pointer to the context data registered to the event.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+MnpCheckMediaStatus (\r
+  IN EFI_EVENT     Event,\r
+  IN VOID          *Context\r
+  )\r
+{\r
+  MNP_DEVICE_DATA             *MnpDeviceData;\r
+  EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r
+  UINT32                      InterruptStatus;\r
+\r
+  MnpDeviceData = (MNP_DEVICE_DATA *) Context;\r
+  NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE);\r
+\r
+  Snp = MnpDeviceData->Snp;\r
+  if (Snp->Mode->MediaPresentSupported) {\r
+    //\r
+    // Upon successful return of GetStatus(), the MediaPresent field of\r
+    // EFI_SIMPLE_NETWORK_MODE will be updated to reflect any change of media status\r
+    //\r
+    Snp->GetStatus (Snp, &InterruptStatus, NULL);\r
+  }\r
+}\r
+\r
+/**\r
+  Poll to receive the packets from Snp. This function is either called by upperlayer\r
+  protocols/applications or the system poll timer notify mechanism.\r
+\r
+  @param[in]  Event        The event this notify function registered to.\r
+  @param[in]  Context      Pointer to the context data registered to the event.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+MnpSystemPoll (\r
+  IN EFI_EVENT     Event,\r
+  IN VOID          *Context\r
+  )\r
+{\r
+  MNP_DEVICE_DATA  *MnpDeviceData;\r
+\r
+  MnpDeviceData = (MNP_DEVICE_DATA *) Context;\r
+  NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE);\r
+\r
+  //\r
+  // Try to receive packets from Snp.\r
+  //\r
+  MnpReceivePacket (MnpDeviceData);\r
+\r
+  //\r
+  // Dispatch the DPC queued by the NotifyFunction of rx token's events.\r
+  //\r
+  DispatchDpc ();\r
+}\r