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 private services.
26 EFI_SERVICE_BINDING_PROTOCOL mMnpServiceBindingProtocol
= {
27 MnpServiceBindingCreateChild
,
28 MnpServiceBindingDestroyChild
31 EFI_MANAGED_NETWORK_PROTOCOL mMnpProtocolTemplate
= {
42 EFI_MANAGED_NETWORK_CONFIG_DATA mMnpDefaultConfigData
= {
58 IN MNP_SERVICE_DATA
*MnpServiceData
,
65 IN EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
71 IN EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
77 IN MNP_SERVICE_DATA
*MnpServiceData
,
78 IN BOOLEAN IsConfigUpdate
,
79 IN BOOLEAN EnableSystemPoll
85 IN MNP_SERVICE_DATA
*MnpServiceData
90 MnpConfigReceiveFilters (
91 IN MNP_SERVICE_DATA
*MnpServiceData
96 MnpGroupOpAddCtrlBlk (
97 IN MNP_INSTANCE_DATA
*Instance
,
98 IN MNP_GROUP_CONTROL_BLOCK
*CtrlBlk
,
99 IN MNP_GROUP_ADDRESS
*GroupAddress OPTIONAL
,
100 IN EFI_MAC_ADDRESS
*MacAddress
,
101 IN UINT32 HwAddressSize
106 MnpGroupOpDelCtrlBlk (
107 IN MNP_INSTANCE_DATA
*Instance
,
108 IN MNP_GROUP_CONTROL_BLOCK
*CtrlBlk
113 Add some NET_BUF into MnpServiceData->FreeNbufQue. The buffer length of
114 the NET_BUF is specified by MnpServiceData->BufferLength.
116 @param MnpServiceData Pointer to the MNP_SERVICE_DATA.
117 @param Count Number of NET_BUFFERs to add.
119 @retval EFI_SUCCESS The specified amount of NET_BUFs are allocated and
120 added into MnpServiceData->FreeNbufQue.
121 @retval EFI_OUT_OF_RESOURCES Failed to allocate a NET_BUF structure.
127 IN MNP_SERVICE_DATA
*MnpServiceData
,
135 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
136 ASSERT ((Count
> 0) && (MnpServiceData
->BufferLength
> 0));
138 Status
= EFI_SUCCESS
;
140 for (Index
= 0; Index
< Count
; Index
++) {
142 Nbuf
= NetbufAlloc (MnpServiceData
->BufferLength
+ MnpServiceData
->PaddingSize
);
145 MNP_DEBUG_ERROR (("MnpAddFreeNbuf: NetBufAlloc failed.\n"));
146 Status
= EFI_OUT_OF_RESOURCES
;
150 if (MnpServiceData
->PaddingSize
> 0) {
152 // Pad padding bytes before the media header
154 NetbufAllocSpace (Nbuf
, MnpServiceData
->PaddingSize
, NET_BUF_TAIL
);
155 NetbufTrim (Nbuf
, MnpServiceData
->PaddingSize
, NET_BUF_HEAD
);
158 NetbufQueAppend (&MnpServiceData
->FreeNbufQue
, Nbuf
);
161 MnpServiceData
->NbufCnt
+= Index
;
168 Allocate a free NET_BUF from MnpServiceData->FreeNbufQue. If there is none
169 in the queue, first try to allocate some and add them into the queue, then
170 fetch the NET_BUF from the updated FreeNbufQue.
172 @param MnpServiceData Pointer to the MNP_SERVICE_DATA.
174 @return Pointer to the allocated free NET_BUF structure, if NULL the operation is failed.
179 IN MNP_SERVICE_DATA
*MnpServiceData
183 NET_BUF_QUEUE
*FreeNbufQue
;
187 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
189 FreeNbufQue
= &MnpServiceData
->FreeNbufQue
;
191 OldTpl
= NET_RAISE_TPL (NET_TPL_RECYCLE
);
194 // Check whether there are available buffers, or else try to add some.
196 if (FreeNbufQue
->BufNum
== 0) {
198 if ((MnpServiceData
->NbufCnt
+ MNP_NET_BUFFER_INCREASEMENT
) > MNP_MAX_NET_BUFFER_NUM
) {
201 ("MnpAllocNbuf: The maximum NET_BUF size is reached for MNP driver instance %p.\n",
209 Status
= MnpAddFreeNbuf (MnpServiceData
, MNP_NET_BUFFER_INCREASEMENT
);
210 if (EFI_ERROR (Status
)) {
213 ("MnpAllocNbuf: Failed to add NET_BUFs into the FreeNbufQue, %r.\n",
217 // Don't return NULL, perhaps MnpAddFreeNbuf does add some NET_BUFs but
218 // the amount is less than MNP_NET_BUFFER_INCREASEMENT.
223 Nbuf
= NetbufQueRemove (FreeNbufQue
);
226 // Increase the RefCnt.
233 NET_RESTORE_TPL (OldTpl
);
240 Try to reclaim the Nbuf into the buffer pool.
242 @param MnpServiceData Pointer to the mnp service context data.
243 @param Nbuf Pointer to the NET_BUF to free.
250 IN MNP_SERVICE_DATA
*MnpServiceData
,
256 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
257 ASSERT (Nbuf
->RefCnt
> 1);
259 OldTpl
= NET_RAISE_TPL (NET_TPL_RECYCLE
);
263 if (Nbuf
->RefCnt
== 1) {
265 // Trim all buffer contained in the Nbuf, then append it to the NbufQue.
267 NetbufTrim (Nbuf
, Nbuf
->TotalSize
, NET_BUF_TAIL
);
268 NetbufQueAppend (&MnpServiceData
->FreeNbufQue
, Nbuf
);
271 NET_RESTORE_TPL (OldTpl
);
276 Initialize the mnp service context data.
278 @param MnpServiceData Pointer to the mnp service context data.
279 @param Snp Pointer to the simple network protocol.
281 @retval EFI_SUCCESS The mnp service context is initialized.
282 @retval Other Some error occurs.
286 MnpInitializeServiceData (
287 IN MNP_SERVICE_DATA
*MnpServiceData
,
288 IN EFI_HANDLE ImageHandle
,
289 IN EFI_HANDLE ControllerHandle
293 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
294 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
296 MnpServiceData
->Signature
= MNP_SERVICE_DATA_SIGNATURE
;
298 MnpServiceData
->ControllerHandle
= ControllerHandle
;
301 // Copy the ServiceBinding structure.
303 MnpServiceData
->ServiceBinding
= mMnpServiceBindingProtocol
;
306 // Open the Simple Network protocol.
308 Status
= gBS
->OpenProtocol (
310 &gEfiSimpleNetworkProtocolGuid
,
314 EFI_OPEN_PROTOCOL_BY_DRIVER
316 if (EFI_ERROR (Status
)) {
317 return EFI_UNSUPPORTED
;
324 MnpServiceData
->Snp
= Snp
;
325 MnpServiceData
->Mtu
= SnpMode
->MaxPacketSize
;
328 // Initialize the lists.
330 NetListInit (&MnpServiceData
->GroupAddressList
);
331 NetListInit (&MnpServiceData
->ChildrenList
);
334 // Get the buffer length used to allocate NET_BUF to hold data received
335 // from SNP. Do this before fill the FreeNetBufQue.
337 MnpServiceData
->BufferLength
= MnpServiceData
->Mtu
+ SnpMode
->MediaHeaderSize
+ NET_ETHER_FCS_SIZE
;
340 // Make sure the protocol headers immediately following the media header
343 MnpServiceData
->PaddingSize
= (4 - SnpMode
->MediaHeaderSize
) & 0x3;
346 // Initialize the FreeNetBufQue and pre-allocate some NET_BUFs.
348 NetbufQueInit (&MnpServiceData
->FreeNbufQue
);
349 Status
= MnpAddFreeNbuf (MnpServiceData
, MNP_INIT_NET_BUFFER_NUM
);
350 if (EFI_ERROR (Status
)) {
352 MNP_DEBUG_ERROR (("MnpInitializeServiceData: MnpAddFreeNbuf failed, %r.\n", Status
));
356 // Get one NET_BUF from the FreeNbufQue for rx cache.
358 MnpServiceData
->RxNbufCache
= MnpAllocNbuf (MnpServiceData
);
360 MnpServiceData
->RxNbufCache
,
361 MnpServiceData
->BufferLength
,
366 // Allocate buffer pool for tx.
368 MnpServiceData
->TxBuf
= NetAllocatePool (MnpServiceData
->Mtu
+ SnpMode
->MediaHeaderSize
);
369 if (MnpServiceData
->TxBuf
== NULL
) {
371 MNP_DEBUG_ERROR (("MnpInitializeServiceData: NetAllocatePool failed.\n"));
372 Status
= EFI_OUT_OF_RESOURCES
;
378 // Create the system poll timer.
380 Status
= gBS
->CreateEvent (
381 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
385 &MnpServiceData
->PollTimer
387 if (EFI_ERROR (Status
)) {
389 MNP_DEBUG_ERROR (("MnpInitializeServiceData: CreateEvent for poll timer failed.\n"));
394 // Create the timer for packet timeout check.
396 Status
= gBS
->CreateEvent (
397 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
399 MnpCheckPacketTimeout
,
401 &MnpServiceData
->TimeoutCheckTimer
403 if (EFI_ERROR (Status
)) {
405 MNP_DEBUG_ERROR (("MnpInitializeServiceData: CreateEvent for packet timeout check failed.\n"));
410 // Create the timer for tx timeout check.
412 Status
= gBS
->CreateEvent (
417 &MnpServiceData
->TxTimeoutEvent
419 if (EFI_ERROR (Status
)) {
421 MNP_DEBUG_ERROR (("MnpInitializeServiceData: CreateEvent for tx timeout event failed.\n"));
426 if (EFI_ERROR (Status
)) {
428 // Free the dynamic allocated resources if necessary.
430 if (MnpServiceData
->TimeoutCheckTimer
!= NULL
) {
432 gBS
->CloseEvent (MnpServiceData
->TimeoutCheckTimer
);
435 if (MnpServiceData
->PollTimer
!= NULL
) {
437 gBS
->CloseEvent (MnpServiceData
->PollTimer
);
440 if (MnpServiceData
->TxBuf
!= NULL
) {
442 NetFreePool (MnpServiceData
->TxBuf
);
445 if (MnpServiceData
->RxNbufCache
!= NULL
) {
447 MnpFreeNbuf (MnpServiceData
, MnpServiceData
->RxNbufCache
);
450 if (MnpServiceData
->FreeNbufQue
.BufNum
!= 0) {
452 NetbufQueFlush (&MnpServiceData
->FreeNbufQue
);
461 Flush the mnp service context data.
463 @param MnpServiceData Pointer to the mnp service context data.
469 MnpFlushServiceData (
470 MNP_SERVICE_DATA
*MnpServiceData
473 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
476 // The GroupAddressList must be empty.
478 ASSERT (NetListIsEmpty (&MnpServiceData
->GroupAddressList
));
483 gBS
->CloseEvent (&MnpServiceData
->TxTimeoutEvent
);
484 gBS
->CloseEvent (&MnpServiceData
->TimeoutCheckTimer
);
485 gBS
->CloseEvent (&MnpServiceData
->PollTimer
);
488 // Free the tx buffer.
490 NetFreePool (MnpServiceData
->TxBuf
);
493 // Free the RxNbufCache.
495 MnpFreeNbuf (MnpServiceData
, MnpServiceData
->RxNbufCache
);
498 // Flush the FreeNbufQue.
500 MnpServiceData
->NbufCnt
-= MnpServiceData
->FreeNbufQue
.BufNum
;
501 NetbufQueFlush (&MnpServiceData
->FreeNbufQue
);
505 if (MnpServiceData
->NbufCnt
!= 0) {
507 MNP_DEBUG_WARN (("MnpFlushServiceData: Memory leak, MnpServiceData->NbufCnt != 0.\n"));
514 Initialize the mnp instance context data.
516 @param MnpServiceData Pointer to the mnp service context data.
517 @param Instance Pointer to the mnp instance context data to
524 MnpInitializeInstanceData (
525 IN MNP_SERVICE_DATA
*MnpServiceData
,
526 IN MNP_INSTANCE_DATA
*Instance
529 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
530 ASSERT (Instance
!= NULL
);
533 // Set the signature.
535 Instance
->Signature
= MNP_INSTANCE_DATA_SIGNATURE
;
538 // Copy the MNP Protocol interfaces from the template.
540 CopyMem (&Instance
->ManagedNetwork
, &mMnpProtocolTemplate
, sizeof (Instance
->ManagedNetwork
));
543 // Copy the default config data.
545 CopyMem (&Instance
->ConfigData
, &mMnpDefaultConfigData
, sizeof (Instance
->ConfigData
));
548 // Initialize the lists.
550 NetListInit (&Instance
->GroupCtrlBlkList
);
551 NetListInit (&Instance
->RcvdPacketQueue
);
552 NetListInit (&Instance
->RxDeliveredPacketQueue
);
555 // Initialize the RxToken Map.
557 NetMapInit (&Instance
->RxTokenMap
);
560 // Save the MnpServiceData info.
562 Instance
->MnpServiceData
= MnpServiceData
;
567 Check whether the token specified by Arg maches the token in Item.
569 @param Map Pointer to the NET_MAP.
570 @param Item Pointer to the NET_MAP_ITEM
571 @param Arg Pointer to the Arg, it's a pointer to the token to
574 @retval EFI_SUCCESS The token specified by Arg is different from the
576 @retval EFI_ACCESS_DENIED The token specified by Arg is the same as that in
583 IN NET_MAP_ITEM
*Item
,
587 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*Token
;
588 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*TokenInItem
;
590 Token
= (EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*) Arg
;
591 TokenInItem
= (EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*) Item
->Key
;
593 if ((Token
== TokenInItem
) || (Token
->Event
== TokenInItem
->Event
)) {
595 // The token is the same either the two tokens equals or the Events in
596 // the two tokens are the same.
598 return EFI_ACCESS_DENIED
;
606 Cancel the token specified by Arg if it matches the token in Item.
608 @param Map Pointer to the NET_MAP.
609 @param Item Pointer to the NET_MAP_ITEM
610 @param Arg Pointer to the Arg, it's a pointer to the token to
613 @retval EFI_SUCCESS The Arg is NULL, and the token in Item is
614 cancelled, or the Arg isn't NULL, and the token in
615 Item is different from the Arg.
616 @retval EFI_ABORTED The Arg isn't NULL, the token in Item mathces the
617 Arg, and the token is cancelled.
623 IN NET_MAP_ITEM
*Item
,
627 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*TokenToCancel
;
629 if ((Arg
!= NULL
) && (Item
->Key
!= Arg
)) {
631 // The token in Item is not the token specified by Arg.
636 TokenToCancel
= (EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*) Item
->Key
;
639 // Cancel this token with status set to EFI_ABORTED.
641 TokenToCancel
->Status
= EFI_ABORTED
;
642 gBS
->SignalEvent (TokenToCancel
->Event
);
645 // Remove the item from the map.
647 NetMapRemoveItem (Map
, Item
, NULL
);
651 // Only abort the token specified by Arg if Arg isn't NULL.
661 Start and initialize the simple network.
663 @param Snp Pointer to the simple network protocol.
665 @retval EFI_SUCCESS The simple network protocol is started.
666 @retval Other Some error occurs.
672 IN EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
677 ASSERT (Snp
!= NULL
);
680 // Start the simple network.
682 Status
= Snp
->Start (Snp
);
684 if (!EFI_ERROR (Status
)) {
686 // Initialize the simple network.
688 Status
= Snp
->Initialize (Snp
, 0, 0);
696 Stop the simple network.
698 @param Snp Pointer to the simple network protocol.
700 @retval EFI_SUCCESS The simple network is stopped.
701 @retval Other Some error occurs.
707 IN EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
712 ASSERT (Snp
!= NULL
);
715 // Shut down the simple network.
717 Status
= Snp
->Shutdown (Snp
);
719 if (!EFI_ERROR (Status
)) {
721 // Stop the simple network.
723 Status
= Snp
->Stop (Snp
);
731 Start the managed network, this function is called when one instance is configured
734 @param MnpServiceData Pointer to the mnp service context data.
735 @param IsConfigUpdate The instance is reconfigured or it's the first time
736 the instanced is configured.
737 @param EnableSystemPoll Enable the system polling or not.
739 @retval EFI_SUCCESS The managed network is started and some
740 configuration is updated.
741 @retval Other Some error occurs.
747 IN MNP_SERVICE_DATA
*MnpServiceData
,
748 IN BOOLEAN IsConfigUpdate
,
749 IN BOOLEAN EnableSystemPoll
753 EFI_TIMER_DELAY TimerOpType
;
755 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
757 Status
= EFI_SUCCESS
;
759 if (!IsConfigUpdate
) {
761 // If it's not a configuration update, increase the configured children number.
763 MnpServiceData
->ConfiguredChildrenNumber
++;
765 if (MnpServiceData
->ConfiguredChildrenNumber
== 1) {
767 // It's the first configured child, start the simple network.
769 Status
= MnpStartSnp (MnpServiceData
->Snp
);
770 if (EFI_ERROR (Status
)) {
772 MNP_DEBUG_ERROR (("MnpStart: MnpStartSnp failed, %r.\n", Status
));
777 // Start the timeout timer.
779 Status
= gBS
->SetTimer (
780 MnpServiceData
->TimeoutCheckTimer
,
782 MNP_TIMEOUT_CHECK_INTERVAL
784 if (EFI_ERROR (Status
)) {
787 ("MnpStart, gBS->SetTimer for TimeoutCheckTimer %r.\n",
795 if (MnpServiceData
->EnableSystemPoll
^ EnableSystemPoll
) {
797 // The EnableSystemPoll differs with the current state, disable or enable
800 TimerOpType
= EnableSystemPoll
? TimerPeriodic
: TimerCancel
;
802 Status
= gBS
->SetTimer (MnpServiceData
->PollTimer
, TimerOpType
, MNP_SYS_POLL_INTERVAL
);
803 if (EFI_ERROR (Status
)) {
805 MNP_DEBUG_ERROR (("MnpStart: gBS->SetTimer for PollTimer failed, %r.\n", Status
));
809 MnpServiceData
->EnableSystemPoll
= EnableSystemPoll
;
813 // Change the receive filters if need.
815 Status
= MnpConfigReceiveFilters (MnpServiceData
);
824 Stop the managed network.
826 @param MnpServiceData Pointer to the mnp service context data.
828 @retval EFI_SUCCESS The managed network is stopped.
829 @retval Other Some error occurs.
835 IN MNP_SERVICE_DATA
*MnpServiceData
840 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
841 ASSERT (MnpServiceData
->ConfiguredChildrenNumber
> 0);
844 // Configure the receive filters.
846 MnpConfigReceiveFilters (MnpServiceData
);
849 // Decrease the children number.
851 MnpServiceData
->ConfiguredChildrenNumber
--;
853 if (MnpServiceData
->ConfiguredChildrenNumber
> 0) {
855 // If there are other configured chilren, return and keep the timers and
856 // simple network unchanged.
862 // No configured children now.
865 if (MnpServiceData
->EnableSystemPoll
) {
867 // The system poll in on, cancel the poll timer.
869 Status
= gBS
->SetTimer (MnpServiceData
->PollTimer
, TimerCancel
, 0);
870 MnpServiceData
->EnableSystemPoll
= FALSE
;
874 // Cancel the timeout timer.
876 Status
= gBS
->SetTimer (MnpServiceData
->TimeoutCheckTimer
, TimerCancel
, 0);
879 // Stop the simple network.
881 Status
= MnpStopSnp (MnpServiceData
->Snp
);
888 Flush the instance's received data.
890 @param Instance Pointer to the mnp instance context data.
896 MnpFlushRcvdDataQueue (
897 IN MNP_INSTANCE_DATA
*Instance
901 MNP_RXDATA_WRAP
*RxDataWrap
;
903 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
905 OldTpl
= NET_RAISE_TPL (NET_TPL_RECYCLE
);
907 while (!NetListIsEmpty (&Instance
->RcvdPacketQueue
)) {
909 // Remove all the Wraps.
911 RxDataWrap
= NET_LIST_HEAD (&Instance
->RcvdPacketQueue
, MNP_RXDATA_WRAP
, WrapEntry
);
914 // Recycle the RxDataWrap.
916 MnpRecycleRxData (NULL
, (VOID
*) RxDataWrap
);
917 Instance
->RcvdPacketQueueSize
--;
920 ASSERT (Instance
->RcvdPacketQueueSize
== 0);
922 NET_RESTORE_TPL (OldTpl
);
927 Configure the Instance using ConfigData.
929 @param Instance Pointer to the mnp instance context data.
930 @param ConfigData Pointer to the configuration data used to configure
933 @retval EFI_SUCCESS The Instance is configured.
934 @retval EFI_UNSUPPORTED EnableReceiveTimestamps is on and the
935 implementation doesn't support it.
936 @retval Other Some error occurs.
940 MnpConfigureInstance (
941 IN MNP_INSTANCE_DATA
*Instance
,
942 IN EFI_MANAGED_NETWORK_CONFIG_DATA
*ConfigData OPTIONAL
946 MNP_SERVICE_DATA
*MnpServiceData
;
947 EFI_MANAGED_NETWORK_CONFIG_DATA
*OldConfigData
;
948 EFI_MANAGED_NETWORK_CONFIG_DATA
*NewConfigData
;
949 BOOLEAN IsConfigUpdate
;
951 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
953 if ((ConfigData
!= NULL
) && ConfigData
->EnableReceiveTimestamps
) {
955 // Don't support timestamp.
957 return EFI_UNSUPPORTED
;
960 Status
= EFI_SUCCESS
;
962 MnpServiceData
= Instance
->MnpServiceData
;
963 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
965 IsConfigUpdate
= (BOOLEAN
) ((Instance
->Configured
) && (ConfigData
!= NULL
));
967 OldConfigData
= &Instance
->ConfigData
;
968 NewConfigData
= ConfigData
;
969 if (NewConfigData
== NULL
) {
971 // Restore back the default config data if a reset of this instance
974 NewConfigData
= &mMnpDefaultConfigData
;
978 // Reset the instance's receive filter.
980 Instance
->ReceiveFilter
= 0;
983 // Clear the receive counters according to the old ConfigData.
985 if (OldConfigData
->EnableUnicastReceive
) {
986 MnpServiceData
->UnicastCount
--;
989 if (OldConfigData
->EnableMulticastReceive
) {
990 MnpServiceData
->MulticastCount
--;
993 if (OldConfigData
->EnableBroadcastReceive
) {
994 MnpServiceData
->BroadcastCount
--;
997 if (OldConfigData
->EnablePromiscuousReceive
) {
998 MnpServiceData
->PromiscuousCount
--;
1002 // Set the receive filter counters and the receive filter of the
1003 // instance according to the new ConfigData.
1005 if (NewConfigData
->EnableUnicastReceive
) {
1006 MnpServiceData
->UnicastCount
++;
1007 Instance
->ReceiveFilter
|= MNP_RECEIVE_UNICAST
;
1010 if (NewConfigData
->EnableMulticastReceive
) {
1011 MnpServiceData
->MulticastCount
++;
1014 if (NewConfigData
->EnableBroadcastReceive
) {
1015 MnpServiceData
->BroadcastCount
++;
1016 Instance
->ReceiveFilter
|= MNP_RECEIVE_BROADCAST
;
1019 if (NewConfigData
->EnablePromiscuousReceive
) {
1020 MnpServiceData
->PromiscuousCount
++;
1023 if (OldConfigData
->FlushQueuesOnReset
) {
1025 MnpFlushRcvdDataQueue (Instance
);
1028 if (ConfigData
== NULL
) {
1030 NetMapIterate (&Instance
->RxTokenMap
, MnpCancelTokens
, NULL
);
1033 if (!NewConfigData
->EnableMulticastReceive
) {
1035 MnpGroupOp (Instance
, FALSE
, NULL
, NULL
);
1039 // Save the new configuration data.
1041 CopyMem (OldConfigData
, NewConfigData
, sizeof (*OldConfigData
));
1043 Instance
->Configured
= (BOOLEAN
) (ConfigData
!= NULL
);
1045 if (Instance
->Configured
) {
1047 // The instance is configured, start the Mnp.
1052 (BOOLEAN
) !NewConfigData
->DisableBackgroundPolling
1056 // The instance is changed to the unconfigured state, stop the Mnp.
1058 Status
= MnpStop (MnpServiceData
);
1066 Configure the Snp receive filters according to the instances' receive filter
1069 @param MnpServiceData Pointer to the mnp service context data.
1071 @retval EFI_SUCCESS The receive filters is configured.
1072 @retval EFI_OUT_OF_RESOURCES The receive filters can't be configured due to lack
1078 MnpConfigReceiveFilters (
1079 IN MNP_SERVICE_DATA
*MnpServiceData
1083 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1084 EFI_MAC_ADDRESS
*MCastFilter
;
1085 UINT32 MCastFilterCnt
;
1086 UINT32 EnableFilterBits
;
1087 UINT32 DisableFilterBits
;
1088 BOOLEAN ResetMCastFilters
;
1089 NET_LIST_ENTRY
*Entry
;
1091 MNP_GROUP_ADDRESS
*GroupAddress
;
1093 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
1095 Snp
= MnpServiceData
->Snp
;
1098 // Initialize the enable filter and disable filter.
1100 EnableFilterBits
= 0;
1101 DisableFilterBits
= Snp
->Mode
->ReceiveFilterMask
;
1103 if (MnpServiceData
->UnicastCount
!= 0) {
1105 // Enable unicast if any instance wants to receive unicast.
1107 EnableFilterBits
|= EFI_SIMPLE_NETWORK_RECEIVE_UNICAST
;
1110 if (MnpServiceData
->BroadcastCount
!= 0) {
1112 // Enable broadcast if any instance wants to receive broadcast.
1114 EnableFilterBits
|= EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST
;
1119 ResetMCastFilters
= TRUE
;
1121 if ((MnpServiceData
->MulticastCount
!= 0) && (MnpServiceData
->GroupAddressCount
!= 0)) {
1123 // There are instances configured to receive multicast and already some group
1124 // addresses are joined.
1127 ResetMCastFilters
= FALSE
;
1129 if (MnpServiceData
->GroupAddressCount
<= Snp
->Mode
->MaxMCastFilterCount
) {
1131 // The joind group address is less than simple network's maximum count.
1132 // Just configure the snp to do the multicast filtering.
1135 EnableFilterBits
|= EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST
;
1138 // Allocate pool for the mulicast addresses.
1140 MCastFilterCnt
= MnpServiceData
->GroupAddressCount
;
1141 MCastFilter
= NetAllocatePool (sizeof (EFI_MAC_ADDRESS
) * MCastFilterCnt
);
1142 if (MCastFilter
== NULL
) {
1144 MNP_DEBUG_ERROR (("MnpConfigReceiveFilters: Failed to allocate memory resource for MCastFilter.\n"));
1145 return EFI_OUT_OF_RESOURCES
;
1149 // Fill the multicast HW address buffer.
1152 NET_LIST_FOR_EACH (Entry
, &MnpServiceData
->GroupAddressList
) {
1154 GroupAddress
= NET_LIST_USER_STRUCT (Entry
, MNP_GROUP_ADDRESS
, AddrEntry
);
1155 CopyMem (MCastFilter
+ Index
, &GroupAddress
->Address
, sizeof (*(MCastFilter
+ Index
)));
1158 ASSERT (Index
<= MCastFilterCnt
);
1162 // The maximum multicast is reached, set the filter to be promiscuous
1166 if (Snp
->Mode
->ReceiveFilterMask
& EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST
) {
1167 EnableFilterBits
|= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST
;
1170 // Either MULTICAST or PROMISCUOUS_MULTICAST is not supported by Snp,
1171 // set the NIC to be promiscuous although this will tremendously degrade
1174 EnableFilterBits
|= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS
;
1179 if (MnpServiceData
->PromiscuousCount
!= 0) {
1181 // Enable promiscuous if any instance wants to receive promiscuous.
1183 EnableFilterBits
|= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS
;
1187 // Set the disable filter.
1189 DisableFilterBits
^= EnableFilterBits
;
1192 // Configure the receive filters of SNP.
1194 Status
= Snp
->ReceiveFilters (
1203 if (EFI_ERROR (Status
)) {
1206 ("MnpConfigReceiveFilters: Snp->ReceiveFilters failed, %r.\n",
1212 if (MCastFilter
!= NULL
) {
1214 // Free the buffer used to hold the group addresses.
1216 NetFreePool (MCastFilter
);
1224 Add a group address control block which controls the MacAddress for
1227 @param Instance Pointer to the mnp instance context data.
1228 @param CtrlBlk Pointer to the group address control block.
1229 @param GroupAddress Pointer to the group adress.
1230 @param MacAddress Pointer to the mac address.
1231 @param HwAddressSize The hardware address size.
1233 @retval EFI_SUCCESS The group address control block is added.
1234 @retval EFI_OUT_OF_RESOURCE Failed due to lack of memory resources.
1239 MnpGroupOpAddCtrlBlk (
1240 IN MNP_INSTANCE_DATA
*Instance
,
1241 IN MNP_GROUP_CONTROL_BLOCK
*CtrlBlk
,
1242 IN MNP_GROUP_ADDRESS
*GroupAddress OPTIONAL
,
1243 IN EFI_MAC_ADDRESS
*MacAddress
,
1244 IN UINT32 HwAddressSize
1247 MNP_SERVICE_DATA
*MnpServiceData
;
1249 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
1251 MnpServiceData
= Instance
->MnpServiceData
;
1252 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
1254 if (GroupAddress
== NULL
) {
1256 ASSERT (MacAddress
!= NULL
);
1259 // Allocate a new GroupAddress to be added into MNP's GroupAddressList.
1261 GroupAddress
= NetAllocatePool (sizeof (MNP_GROUP_ADDRESS
));
1262 if (GroupAddress
== NULL
) {
1264 MNP_DEBUG_ERROR (("MnpGroupOpFormCtrlBlk: Failed to allocate memory resource.\n"));
1266 return EFI_OUT_OF_RESOURCES
;
1269 CopyMem (&GroupAddress
->Address
, MacAddress
, sizeof (GroupAddress
->Address
));
1270 GroupAddress
->RefCnt
= 0;
1272 &MnpServiceData
->GroupAddressList
,
1273 &GroupAddress
->AddrEntry
1275 MnpServiceData
->GroupAddressCount
++;
1279 // Increase the RefCnt.
1281 GroupAddress
->RefCnt
++;
1284 // Add the CtrlBlk into the instance's GroupCtrlBlkList.
1286 CtrlBlk
->GroupAddress
= GroupAddress
;
1287 NetListInsertTail (&Instance
->GroupCtrlBlkList
, &CtrlBlk
->CtrlBlkEntry
);
1294 Delete a group control block from the instance. If the controlled group address's
1295 reference count reaches zero, the group address is removed too.
1297 @param Instance Pointer to the instance context data.
1298 @param CtrlBlk Pointer to the group control block to delete.
1300 @return The group address controlled by the control block is no longer used or not.
1305 MnpGroupOpDelCtrlBlk (
1306 IN MNP_INSTANCE_DATA
*Instance
,
1307 IN MNP_GROUP_CONTROL_BLOCK
*CtrlBlk
1310 MNP_SERVICE_DATA
*MnpServiceData
;
1311 MNP_GROUP_ADDRESS
*GroupAddress
;
1313 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
1315 MnpServiceData
= Instance
->MnpServiceData
;
1316 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
1319 // Remove and free the CtrlBlk.
1321 GroupAddress
= CtrlBlk
->GroupAddress
;
1322 NetListRemoveEntry (&CtrlBlk
->CtrlBlkEntry
);
1323 NetFreePool (CtrlBlk
);
1325 ASSERT (GroupAddress
->RefCnt
> 0);
1328 // Count down the RefCnt.
1330 GroupAddress
->RefCnt
--;
1332 if (GroupAddress
->RefCnt
== 0) {
1334 // Free this GroupAddress entry if no instance uses it.
1336 MnpServiceData
->GroupAddressCount
--;
1337 NetListRemoveEntry (&GroupAddress
->AddrEntry
);
1338 NetFreePool (GroupAddress
);
1348 Do the group operations for this instance.
1350 @param Instance Pointer to the instance context data.
1351 @param JoinFlag Set to TRUE to join a group. Set to TRUE to leave a
1353 @param MacAddress Pointer to the group address to join or leave.
1354 @param CtrlBlk Pointer to the group control block if JoinFlag if
1357 @retval EFI_SUCCESS The group operation finished.
1358 @retval Other Some error occurs.
1363 IN MNP_INSTANCE_DATA
*Instance
,
1364 IN BOOLEAN JoinFlag
,
1365 IN EFI_MAC_ADDRESS
*MacAddress OPTIONAL
,
1366 IN MNP_GROUP_CONTROL_BLOCK
*CtrlBlk OPTIONAL
1369 MNP_SERVICE_DATA
*MnpServiceData
;
1370 NET_LIST_ENTRY
*Entry
;
1371 NET_LIST_ENTRY
*NextEntry
;
1372 MNP_GROUP_ADDRESS
*GroupAddress
;
1373 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
1374 MNP_GROUP_CONTROL_BLOCK
*NewCtrlBlk
;
1376 BOOLEAN AddressExist
;
1379 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
1381 MnpServiceData
= Instance
->MnpServiceData
;
1382 SnpMode
= MnpServiceData
->Snp
->Mode
;
1386 // A new gropu address is to be added.
1389 GroupAddress
= NULL
;
1390 AddressExist
= FALSE
;
1393 // Allocate memory for the control block.
1395 NewCtrlBlk
= NetAllocatePool (sizeof (MNP_GROUP_CONTROL_BLOCK
));
1396 if (NewCtrlBlk
== NULL
) {
1398 MNP_DEBUG_ERROR (("MnpGroupOp: Failed to allocate memory resource.\n"));
1399 return EFI_OUT_OF_RESOURCES
;
1402 NET_LIST_FOR_EACH (Entry
, &MnpServiceData
->GroupAddressList
) {
1404 // Check whether the MacAddress is already joined by other instances.
1406 GroupAddress
= NET_LIST_USER_STRUCT (Entry
, MNP_GROUP_ADDRESS
, AddrEntry
);
1407 if (0 == NetCompareMem (
1409 &GroupAddress
->Address
,
1410 SnpMode
->HwAddressSize
1413 AddressExist
= TRUE
;
1418 if (!AddressExist
) {
1419 GroupAddress
= NULL
;
1423 // Add the GroupAddress for this instance.
1425 Status
= MnpGroupOpAddCtrlBlk (
1430 SnpMode
->HwAddressSize
1432 if (EFI_ERROR (Status
)) {
1440 if (MacAddress
!= NULL
) {
1442 ASSERT (CtrlBlk
!= NULL
);
1445 // Leave the specific multicast mac address.
1447 NeedUpdate
= MnpGroupOpDelCtrlBlk (Instance
, CtrlBlk
);
1450 // Leave all multicast mac addresses.
1454 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Instance
->GroupCtrlBlkList
) {
1456 NewCtrlBlk
= NET_LIST_USER_STRUCT (
1458 MNP_GROUP_CONTROL_BLOCK
,
1462 // Update is required if the group address left is no longer used
1463 // by other instances.
1465 NeedUpdate
= MnpGroupOpDelCtrlBlk (Instance
, NewCtrlBlk
);
1470 Status
= EFI_SUCCESS
;
1474 // Reconfigure the receive filters if necessary.
1476 Status
= MnpConfigReceiveFilters (MnpServiceData
);