3 Copyright (c) 2005 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Implementation of Managed Network Protocol I/O functions.
23 #include <Library/NetLib.h>
24 #include <Library/BaseMemoryLib.h>
25 #include <Library/BaseLib.h>
26 #include <Library/MemoryAllocationLib.h>
31 Validates the Mnp transmit token.
33 @param Instance Pointer to the Mnp instance context data.
34 @param Token Pointer to the transmit token to check.
36 @return The Token is valid or not.
41 IN MNP_INSTANCE_DATA
*Instance
,
42 IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*Token
45 MNP_SERVICE_DATA
*MnpServiceData
;
46 EFI_MANAGED_NETWORK_TRANSMIT_DATA
*TxData
;
49 EFI_MANAGED_NETWORK_FRAGMENT_DATA
*FragmentTable
;
51 MnpServiceData
= Instance
->MnpServiceData
;
52 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
54 TxData
= Token
->Packet
.TxData
;
56 if ((Token
->Event
== NULL
) || (TxData
== NULL
) || (TxData
->FragmentCount
== 0)) {
58 // The token is invalid if the Event is NULL, or the TxData is NULL, or
59 // the fragment count is zero.
61 DEBUG ((EFI_D_WARN
, "MnpIsValidTxToken: Invalid Token.\n"));
65 if ((TxData
->DestinationAddress
!= NULL
) && (TxData
->HeaderLength
!= 0)) {
67 // The token is invalid if the HeaderLength isn't zero while the DestinationAddress
68 // is NULL (The destination address is already put into the packet).
70 DEBUG ((EFI_D_WARN
, "MnpIsValidTxToken: DestinationAddress isn't NULL, HeaderLength must be 0.\n"));
75 FragmentTable
= TxData
->FragmentTable
;
76 for (Index
= 0; Index
< TxData
->FragmentCount
; Index
++) {
78 if ((FragmentTable
[Index
].FragmentLength
== 0) || (FragmentTable
[Index
].FragmentBuffer
== NULL
)) {
80 // The token is invalid if any FragmentLength is zero or any FragmentBuffer is NULL.
82 DEBUG ((EFI_D_WARN
, "MnpIsValidTxToken: Invalid FragmentLength or FragmentBuffer.\n"));
86 TotalLength
+= FragmentTable
[Index
].FragmentLength
;
89 if ((TxData
->DestinationAddress
== NULL
) && (FragmentTable
[0].FragmentLength
< TxData
->HeaderLength
)) {
91 // Media header is split between fragments.
96 if (TotalLength
!= (TxData
->DataLength
+ TxData
->HeaderLength
)) {
98 // The length calculated from the fragment information doesn't equal to the
99 // sum of the DataLength and the HeaderLength.
101 DEBUG ((EFI_D_WARN
, "MnpIsValidTxData: Invalid Datalength compared with the sum of fragment length.\n"));
105 if (TxData
->DataLength
> MnpServiceData
->Mtu
) {
107 // The total length is larger than the MTU.
109 DEBUG ((EFI_D_WARN
, "MnpIsValidTxData: TxData->DataLength exceeds Mtu.\n"));
118 Build the packet to transmit from the TxData passed in.
120 @param MnpServiceData Pointer to the mnp service context data.
121 @param TxData Pointer to the transmit data containing the
122 information to build the packet.
123 @param PktBuf Pointer to record the address of the packet.
124 @param PktLen Pointer to a UINT32 variable used to record the
132 IN MNP_SERVICE_DATA
*MnpServiceData
,
133 IN EFI_MANAGED_NETWORK_TRANSMIT_DATA
*TxData
,
138 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
142 if ((TxData
->DestinationAddress
== NULL
) && (TxData
->FragmentCount
== 1)) {
144 // Media header is in FragmentTable and there is only one fragment,
145 // use fragment buffer directly.
147 *PktBuf
= TxData
->FragmentTable
[0].FragmentBuffer
;
148 *PktLen
= TxData
->FragmentTable
[0].FragmentLength
;
151 // Either media header isn't in FragmentTable or there is more than
152 // one fragment, copy the data into the packet buffer. Reserve the
153 // media header space if necessary.
155 SnpMode
= MnpServiceData
->Snp
->Mode
;
156 DstPos
= MnpServiceData
->TxBuf
;
159 if (TxData
->DestinationAddress
!= NULL
) {
161 // If dest address is not NULL, move DstPos to reserve space for the
162 // media header. Add the media header length to buflen.
164 DstPos
+= SnpMode
->MediaHeaderSize
;
165 *PktLen
+= SnpMode
->MediaHeaderSize
;
168 for (Index
= 0; Index
< TxData
->FragmentCount
; Index
++) {
174 TxData
->FragmentTable
[Index
].FragmentBuffer
,
175 TxData
->FragmentTable
[Index
].FragmentLength
177 DstPos
+= TxData
->FragmentTable
[Index
].FragmentLength
;
181 // Set the buffer pointer and the buffer length.
183 *PktBuf
= MnpServiceData
->TxBuf
;
184 *PktLen
+= TxData
->DataLength
+ TxData
->HeaderLength
;
190 Synchronously send out the packet.
192 @param MnpServiceData Pointer to the mnp service context data.
193 @param Packet Pointer to the pakcet buffer.
194 @param Length The length of the packet.
195 @param Token Pointer to the token the packet generated from.
197 @retval EFI_SUCCESS The packet is sent out.
198 @retval EFI_TIMEOUT Time out occurs, the packet isn't sent.
199 @retval EFI_DEVICE_ERROR An unexpected network error occurs.
204 IN MNP_SERVICE_DATA
*MnpServiceData
,
207 IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*Token
211 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
212 EFI_MANAGED_NETWORK_TRANSMIT_DATA
*TxData
;
216 Snp
= MnpServiceData
->Snp
;
217 TxData
= Token
->Packet
.TxData
;
219 HeaderSize
= Snp
->Mode
->MediaHeaderSize
- TxData
->HeaderLength
;
222 // Start the timeout event.
224 Status
= gBS
->SetTimer (
225 MnpServiceData
->TxTimeoutEvent
,
229 if (EFI_ERROR (Status
)) {
236 // Transmit the packet through SNP.
238 Status
= Snp
->Transmit (
243 TxData
->SourceAddress
,
244 TxData
->DestinationAddress
,
245 &TxData
->ProtocolType
247 if ((Status
!= EFI_SUCCESS
) && (Status
!= EFI_NOT_READY
)) {
249 Status
= EFI_DEVICE_ERROR
;
254 // If Status is EFI_SUCCESS, the packet is put in the transmit queue.
255 // if Status is EFI_NOT_READY, the transmit engine of the network interface is busy.
256 // Both need to sync SNP.
261 // Get the recycled transmit buffer status.
263 Snp
->GetStatus (Snp
, NULL
, (VOID
**) &TxBuf
);
265 if (!EFI_ERROR (gBS
->CheckEvent (MnpServiceData
->TxTimeoutEvent
))) {
267 Status
= EFI_TIMEOUT
;
270 } while (TxBuf
== NULL
);
272 if ((Status
== EFI_SUCCESS
) || (Status
== EFI_TIMEOUT
)) {
277 // Status is EFI_NOT_READY. Restart the timer event and call Snp->Transmit again.
280 MnpServiceData
->TxTimeoutEvent
,
288 // Cancel the timer event.
290 gBS
->SetTimer (MnpServiceData
->TxTimeoutEvent
, TimerCancel
, 0);
294 Token
->Status
= Status
;
295 gBS
->SignalEvent (Token
->Event
);
298 // Dispatch the DPC queued by the NotifyFunction of Token->Event.
300 NetLibDispatchDpc ();
307 Try to deliver the received packet to the instance.
309 @param Instance Pointer to the mnp instance context data.
311 @retval EFI_SUCCESS The received packet is delivered, or there is no
312 packet to deliver, or there is no available receive
314 @retval EFI_OUT_OF_RESOURCES The deliver fails due to lack of memory resource.
318 MnpInstanceDeliverPacket (
319 IN MNP_INSTANCE_DATA
*Instance
322 MNP_SERVICE_DATA
*MnpServiceData
;
323 MNP_RXDATA_WRAP
*RxDataWrap
;
325 EFI_MANAGED_NETWORK_RECEIVE_DATA
*RxData
;
326 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
327 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*RxToken
;
329 MnpServiceData
= Instance
->MnpServiceData
;
330 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
332 if (NetMapIsEmpty (&Instance
->RxTokenMap
) || IsListEmpty (&Instance
->RcvdPacketQueue
)) {
334 // No pending received data or no available receive token, return.
339 ASSERT (Instance
->RcvdPacketQueueSize
!= 0);
341 RxDataWrap
= NET_LIST_HEAD (&Instance
->RcvdPacketQueue
, MNP_RXDATA_WRAP
, WrapEntry
);
342 if (RxDataWrap
->Nbuf
->RefCnt
> 2) {
344 // There are other instances share this Nbuf, duplicate to get a
345 // copy to allow the instance to do R/W operations.
347 DupNbuf
= MnpAllocNbuf (MnpServiceData
);
348 if (DupNbuf
== NULL
) {
349 DEBUG ((EFI_D_WARN
, "MnpDeliverPacket: Failed to allocate a free Nbuf.\n"));
351 return EFI_OUT_OF_RESOURCES
;
355 // Duplicate the net buffer.
357 NetbufDuplicate (RxDataWrap
->Nbuf
, DupNbuf
, 0);
358 MnpFreeNbuf (MnpServiceData
, RxDataWrap
->Nbuf
);
359 RxDataWrap
->Nbuf
= DupNbuf
;
363 // All resources are OK, remove the packet from the queue.
365 NetListRemoveHead (&Instance
->RcvdPacketQueue
);
366 Instance
->RcvdPacketQueueSize
--;
368 RxData
= &RxDataWrap
->RxData
;
369 SnpMode
= MnpServiceData
->Snp
->Mode
;
372 // Set all the buffer pointers.
374 RxData
->MediaHeader
= NetbufGetByte (RxDataWrap
->Nbuf
, 0, NULL
);
375 RxData
->DestinationAddress
= RxData
->MediaHeader
;
376 RxData
->SourceAddress
= (UINT8
*) RxData
->MediaHeader
+ SnpMode
->HwAddressSize
;
377 RxData
->PacketData
= (UINT8
*) RxData
->MediaHeader
+ SnpMode
->MediaHeaderSize
;
380 // Insert this RxDataWrap into the delivered queue.
382 InsertTailList (&Instance
->RxDeliveredPacketQueue
, &RxDataWrap
->WrapEntry
);
385 // Get the receive token from the RxTokenMap.
387 RxToken
= NetMapRemoveHead (&Instance
->RxTokenMap
, NULL
);
390 // Signal this token's event.
392 RxToken
->Packet
.RxData
= &RxDataWrap
->RxData
;
393 RxToken
->Status
= EFI_SUCCESS
;
394 gBS
->SignalEvent (RxToken
->Event
);
401 Deliver the received packet for the instances belonging to the MnpServiceData.
403 @param MnpServiceData Pointer to the mnp service context data.
411 IN MNP_SERVICE_DATA
*MnpServiceData
415 MNP_INSTANCE_DATA
*Instance
;
417 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
419 NET_LIST_FOR_EACH (Entry
, &MnpServiceData
->ChildrenList
) {
420 Instance
= NET_LIST_USER_STRUCT (Entry
, MNP_INSTANCE_DATA
, InstEntry
);
421 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
424 // Try to deliver packet for this instance.
426 MnpInstanceDeliverPacket (Instance
);
432 Recycle the RxData and other resources used to hold and deliver the received
435 @param Event The event this notify function registered to.
436 @param Context Pointer to the context data registerd to the Event.
448 MNP_RXDATA_WRAP
*RxDataWrap
;
449 MNP_SERVICE_DATA
*MnpServiceData
;
451 ASSERT (Context
!= NULL
);
453 RxDataWrap
= (MNP_RXDATA_WRAP
*) Context
;
454 NET_CHECK_SIGNATURE (RxDataWrap
->Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
456 ASSERT (RxDataWrap
->Nbuf
!= NULL
);
458 MnpServiceData
= RxDataWrap
->Instance
->MnpServiceData
;
459 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
464 MnpFreeNbuf (MnpServiceData
, RxDataWrap
->Nbuf
);
465 RxDataWrap
->Nbuf
= NULL
;
468 // Close the recycle event.
470 gBS
->CloseEvent (RxDataWrap
->RxData
.RecycleEvent
);
473 // Remove this Wrap entry from the list.
475 RemoveEntryList (&RxDataWrap
->WrapEntry
);
477 gBS
->FreePool (RxDataWrap
);
482 Queue the received packet into instance's receive queue.
484 @param Instance Pointer to the mnp instance context data.
485 @param RxDataWrap Pointer to the Wrap structure containing the
486 received data and other information.
494 IN MNP_INSTANCE_DATA
*Instance
,
495 IN MNP_RXDATA_WRAP
*RxDataWrap
498 MNP_RXDATA_WRAP
*OldRxDataWrap
;
500 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
503 // Check the queue size. If it exceeds the limit, drop one packet
506 if (Instance
->RcvdPacketQueueSize
== MNP_MAX_RCVD_PACKET_QUE_SIZE
) {
508 DEBUG ((EFI_D_WARN
, "MnpQueueRcvdPacket: Drop one packet bcz queue size limit reached.\n"));
511 // Get the oldest packet.
513 OldRxDataWrap
= NET_LIST_HEAD (
514 &Instance
->RcvdPacketQueue
,
520 // Recycle this OldRxDataWrap, this entry will be removed by the callee.
522 MnpRecycleRxData (NULL
, (VOID
*) OldRxDataWrap
);
523 Instance
->RcvdPacketQueueSize
--;
527 // Update the timeout tick using the configured parameter.
529 RxDataWrap
->TimeoutTick
= Instance
->ConfigData
.ReceivedQueueTimeoutValue
;
532 // Insert this Wrap into the instance queue.
534 InsertTailList (&Instance
->RcvdPacketQueue
, &RxDataWrap
->WrapEntry
);
535 Instance
->RcvdPacketQueueSize
++;
540 Match the received packet with the instance receive filters.
542 @param Instance Pointer to the mnp instance context data.
543 @param RxData Pointer to the EFI_MANAGED_NETWORK_RECEIVE_DATA.
544 @param GroupAddress Pointer to the GroupAddress, the GroupAddress is
545 non-NULL and it contains the destination multicast
546 mac address of the received packet if the packet
547 destinated to a multicast mac address.
548 @param PktAttr The received packets attribute.
550 @return The received packet matches the instance's receive filters or not.
556 IN MNP_INSTANCE_DATA
*Instance
,
557 IN EFI_MANAGED_NETWORK_RECEIVE_DATA
*RxData
,
558 IN MNP_GROUP_ADDRESS
*GroupAddress OPTIONAL
,
562 EFI_MANAGED_NETWORK_CONFIG_DATA
*ConfigData
;
564 MNP_GROUP_CONTROL_BLOCK
*GroupCtrlBlk
;
566 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
568 ConfigData
= &Instance
->ConfigData
;
571 // Check the protocol type.
573 if ((ConfigData
->ProtocolTypeFilter
!= 0) && (ConfigData
->ProtocolTypeFilter
!= RxData
->ProtocolType
)) {
577 if (ConfigData
->EnablePromiscuousReceive
) {
579 // Always match if this instance is configured to be promiscuous.
585 // The protocol type is matched, check receive filter, include unicast and broadcast.
587 if ((Instance
->ReceiveFilter
& PktAttr
) != 0) {
592 // Check multicast addresses.
594 if (ConfigData
->EnableMulticastReceive
&& RxData
->MulticastFlag
) {
596 ASSERT (GroupAddress
!= NULL
);
598 NET_LIST_FOR_EACH (Entry
, &Instance
->GroupCtrlBlkList
) {
600 GroupCtrlBlk
= NET_LIST_USER_STRUCT (Entry
, MNP_GROUP_CONTROL_BLOCK
, CtrlBlkEntry
);
601 if (GroupCtrlBlk
->GroupAddress
== GroupAddress
) {
603 // The instance is configured to receiveing packets destinated to this
604 // multicast address.
619 Analyse the received packets.
621 @param MnpServiceData Pointer to the mnp service context data.
622 @param Nbuf Pointer to the net buffer holding the received
624 @param RxData Pointer to the buffer used to save the analysed
625 result in EFI_MANAGED_NETWORK_RECEIVE_DATA.
626 @param GroupAddress Pointer to pointer to a MNP_GROUP_ADDRESS used to
627 pass out the address of the multicast address the
628 received packet destinated to.
629 @param PktAttr Pointer to the buffer used to save the analysed
638 IN MNP_SERVICE_DATA
*MnpServiceData
,
640 IN EFI_MANAGED_NETWORK_RECEIVE_DATA
*RxData
,
641 OUT MNP_GROUP_ADDRESS
**GroupAddress
,
645 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
649 SnpMode
= MnpServiceData
->Snp
->Mode
;
652 // Get the packet buffer.
654 BufPtr
= NetbufGetByte (Nbuf
, 0, NULL
);
655 ASSERT (BufPtr
!= NULL
);
658 // Set the initial values.
660 RxData
->BroadcastFlag
= FALSE
;
661 RxData
->MulticastFlag
= FALSE
;
662 RxData
->PromiscuousFlag
= FALSE
;
663 *PktAttr
= UNICAST_PACKET
;
665 if (!NET_MAC_EQUAL (&SnpMode
->CurrentAddress
, BufPtr
, SnpMode
->HwAddressSize
)) {
667 // This packet isn't destinated to our current mac address, it't not unicast.
671 if (NET_MAC_EQUAL (&SnpMode
->BroadcastAddress
, BufPtr
, SnpMode
->HwAddressSize
)) {
675 RxData
->BroadcastFlag
= TRUE
;
676 *PktAttr
= BROADCAST_PACKET
;
677 } else if ((*BufPtr
& 0x01) == 0x1) {
679 // It's multicast, try to match the multicast filters.
681 NET_LIST_FOR_EACH (Entry
, &MnpServiceData
->GroupAddressList
) {
683 *GroupAddress
= NET_LIST_USER_STRUCT (Entry
, MNP_GROUP_ADDRESS
, AddrEntry
);
684 if (NET_MAC_EQUAL (BufPtr
, &((*GroupAddress
)->Address
), SnpMode
->HwAddressSize
)) {
685 RxData
->MulticastFlag
= TRUE
;
690 if (!RxData
->MulticastFlag
) {
692 // No match, set GroupAddress to NULL. This multicast packet must
693 // be the result of PROMISUCOUS or PROMISUCOUS_MULTICAST flag is on.
695 *GroupAddress
= NULL
;
696 RxData
->PromiscuousFlag
= TRUE
;
698 if (MnpServiceData
->PromiscuousCount
== 0) {
700 // Skip the below code, there is no receiver of this packet.
706 RxData
->PromiscuousFlag
= TRUE
;
710 ZeroMem (&RxData
->Timestamp
, sizeof (EFI_TIME
));
713 // Fill the common parts of RxData.
715 RxData
->PacketLength
= Nbuf
->TotalSize
;
716 RxData
->HeaderLength
= SnpMode
->MediaHeaderSize
;
717 RxData
->AddressLength
= SnpMode
->HwAddressSize
;
718 RxData
->DataLength
= RxData
->PacketLength
- RxData
->HeaderLength
;
719 RxData
->ProtocolType
= NTOHS (*(UINT16
*) (BufPtr
+ 2 * SnpMode
->HwAddressSize
));
726 @param Instance Pointer to the mnp instance context data.
727 @param RxData Pointer to the receive data to wrap.
729 @return Pointer to a MNP_RXDATA_WRAP which wraps the RxData.
735 IN MNP_INSTANCE_DATA
*Instance
,
736 IN EFI_MANAGED_NETWORK_RECEIVE_DATA
*RxData
740 MNP_RXDATA_WRAP
*RxDataWrap
;
745 RxDataWrap
= AllocatePool (sizeof (MNP_RXDATA_WRAP
));
746 if (RxDataWrap
== NULL
) {
747 DEBUG ((EFI_D_ERROR
, "MnpDispatchPacket: Failed to allocate a MNP_RXDATA_WRAP.\n"));
751 RxDataWrap
->Instance
= Instance
;
754 // Fill the RxData in RxDataWrap,
756 CopyMem (&RxDataWrap
->RxData
, RxData
, sizeof (RxDataWrap
->RxData
));
759 // Create the recycle event.
761 Status
= gBS
->CreateEvent (
766 &RxDataWrap
->RxData
.RecycleEvent
768 if (EFI_ERROR (Status
)) {
770 DEBUG ((EFI_D_ERROR
, "MnpDispatchPacket: gBS->CreateEvent failed, %r.\n", Status
));
771 gBS
->FreePool (RxDataWrap
);
780 Enqueue the received the packets to the instances belonging to the
783 @param MnpServiceData Pointer to the mnp service context data.
784 @param Nbuf Pointer to the net buffer representing the received
793 IN MNP_SERVICE_DATA
*MnpServiceData
,
798 MNP_INSTANCE_DATA
*Instance
;
799 EFI_MANAGED_NETWORK_RECEIVE_DATA RxData
;
801 MNP_GROUP_ADDRESS
*GroupAddress
;
802 MNP_RXDATA_WRAP
*RxDataWrap
;
807 // First, analyse the packet header.
809 MnpAnalysePacket (MnpServiceData
, Nbuf
, &RxData
, &GroupAddress
, &PktAttr
);
811 if (RxData
.PromiscuousFlag
&& (MnpServiceData
->PromiscuousCount
== 0)) {
813 // No receivers, no more action need.
819 // Iterate the children to find match.
821 NET_LIST_FOR_EACH (Entry
, &MnpServiceData
->ChildrenList
) {
823 Instance
= NET_LIST_USER_STRUCT (Entry
, MNP_INSTANCE_DATA
, InstEntry
);
824 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
826 if (!Instance
->Configured
) {
831 // Check the packet against the instance receive filters.
833 if (MnpMatchPacket (Instance
, &RxData
, GroupAddress
, PktAttr
)) {
838 RxDataWrap
= MnpWrapRxData (Instance
, &RxData
);
839 if (RxDataWrap
== NULL
) {
844 // Associate RxDataWrap with Nbuf and increase the RefCnt.
846 RxDataWrap
->Nbuf
= Nbuf
;
847 NET_GET_REF (RxDataWrap
->Nbuf
);
850 // Queue the packet into the instance queue.
852 MnpQueueRcvdPacket (Instance
, RxDataWrap
);
859 Try to receive a packet and deliver it.
861 @param MnpServiceData Pointer to the mnp service context data.
863 @retval EFI_SUCCESS add return value to function comment
864 @retval EFI_NOT_STARTED The simple network protocol is not started.
865 @retval EFI_NOT_READY No packet received.
866 @retval EFI_DEVICE_ERROR An unexpected error occurs.
871 IN MNP_SERVICE_DATA
*MnpServiceData
875 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
882 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
884 Snp
= MnpServiceData
->Snp
;
885 if (Snp
->Mode
->State
!= EfiSimpleNetworkInitialized
) {
887 // The simple network protocol is not started.
889 return EFI_NOT_STARTED
;
892 if (IsListEmpty (&MnpServiceData
->ChildrenList
)) {
894 // There is no child, no need to receive packets.
899 if (MnpServiceData
->RxNbufCache
== NULL
) {
901 // Try to get a new buffer as there may be buffers recycled.
903 MnpServiceData
->RxNbufCache
= MnpAllocNbuf (MnpServiceData
);
905 if (MnpServiceData
->RxNbufCache
== NULL
) {
907 // No availabe buffer in the buffer pool.
909 return EFI_DEVICE_ERROR
;
913 MnpServiceData
->RxNbufCache
,
914 MnpServiceData
->BufferLength
,
919 Nbuf
= MnpServiceData
->RxNbufCache
;
920 BufLen
= Nbuf
->TotalSize
;
921 BufPtr
= NetbufGetByte (Nbuf
, 0, NULL
);
922 ASSERT (BufPtr
!= NULL
);
925 // Receive packet through Snp.
927 Status
= Snp
->Receive (Snp
, &HeaderSize
, &BufLen
, BufPtr
, NULL
, NULL
, NULL
);
928 if (EFI_ERROR (Status
)) {
931 if (Status
!= EFI_NOT_READY
) {
932 DEBUG ((EFI_D_ERROR
, "MnpReceivePacket: Snp->Receive() = %r.\n", Status
));
942 if ((HeaderSize
!= Snp
->Mode
->MediaHeaderSize
) || (BufLen
< HeaderSize
)) {
946 "MnpReceivePacket: Size error, HL:TL = %d:%d.\n",
950 return EFI_DEVICE_ERROR
;
954 if (Nbuf
->TotalSize
!= BufLen
) {
956 // Trim the packet from tail.
958 Trimmed
= NetbufTrim (Nbuf
, Nbuf
->TotalSize
- (UINT32
) BufLen
, NET_BUF_TAIL
);
959 ASSERT (Nbuf
->TotalSize
== BufLen
);
963 // Enqueue the packet to the matched instances.
965 MnpEnqueuePacket (MnpServiceData
, Nbuf
);
967 if (Nbuf
->RefCnt
> 2) {
969 // RefCnt > 2 indicates there is at least one receiver of this packet.
970 // Free the current RxNbufCache and allocate a new one.
972 MnpFreeNbuf (MnpServiceData
, Nbuf
);
974 Nbuf
= MnpAllocNbuf (MnpServiceData
);
975 MnpServiceData
->RxNbufCache
= Nbuf
;
977 DEBUG ((EFI_D_ERROR
, "MnpReceivePacket: Alloc packet for receiving cache failed.\n"));
978 return EFI_DEVICE_ERROR
;
981 NetbufAllocSpace (Nbuf
, MnpServiceData
->BufferLength
, NET_BUF_TAIL
);
984 // No receiver for this packet.
987 NetbufAllocSpace (Nbuf
, Trimmed
, NET_BUF_TAIL
);
993 // Deliver the queued packets.
995 MnpDeliverPacket (MnpServiceData
);
998 // Dispatch the DPC queued by the NotifyFunction of rx token's events.
1000 NetLibDispatchDpc ();
1004 ASSERT (Nbuf
->TotalSize
== MnpServiceData
->BufferLength
);
1011 Remove the received packets if timeout occurs.
1013 @param Event The event this notify function registered to.
1014 @param Context Pointer to the context data registered to the
1022 MnpCheckPacketTimeout (
1027 MNP_SERVICE_DATA
*MnpServiceData
;
1029 LIST_ENTRY
*RxEntry
;
1030 LIST_ENTRY
*NextEntry
;
1031 MNP_INSTANCE_DATA
*Instance
;
1032 MNP_RXDATA_WRAP
*RxDataWrap
;
1035 MnpServiceData
= (MNP_SERVICE_DATA
*) Context
;
1036 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
1038 NET_LIST_FOR_EACH (Entry
, &MnpServiceData
->ChildrenList
) {
1040 Instance
= NET_LIST_USER_STRUCT (Entry
, MNP_INSTANCE_DATA
, InstEntry
);
1041 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
1043 if (!Instance
->Configured
|| (Instance
->ConfigData
.ReceivedQueueTimeoutValue
== 0)) {
1045 // This instance is not configured or there is no receive time out,
1046 // just skip to the next instance.
1051 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1053 NET_LIST_FOR_EACH_SAFE (RxEntry
, NextEntry
, &Instance
->RcvdPacketQueue
) {
1055 RxDataWrap
= NET_LIST_USER_STRUCT (RxEntry
, MNP_RXDATA_WRAP
, WrapEntry
);
1057 if (RxDataWrap
->TimeoutTick
>= MNP_TIMEOUT_CHECK_INTERVAL
) {
1059 RxDataWrap
->TimeoutTick
-= MNP_TIMEOUT_CHECK_INTERVAL
;
1062 // Drop the timeout packet.
1064 DEBUG ((EFI_D_WARN
, "MnpCheckPacketTimeout: Received packet timeout.\n"));
1065 MnpRecycleRxData (NULL
, RxDataWrap
);
1066 Instance
->RcvdPacketQueueSize
--;
1070 gBS
->RestoreTPL (OldTpl
);
1076 Poll to receive the packets from Snp. This function is either called by upperlayer
1077 protocols/applications or the system poll timer notify mechanism.
1079 @param Event The event this notify function registered to.
1080 @param Context Pointer to the context data registered to the
1093 MNP_SERVICE_DATA
*MnpServiceData
;
1095 MnpServiceData
= (MNP_SERVICE_DATA
*) Context
;
1096 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
1099 // Try to receive packets from Snp.
1101 MnpReceivePacket (MnpServiceData
);
1103 NetLibDispatchDpc ();