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
);
145 MNP_DEBUG_ERROR (("MnpAddFreeNbuf: NetBufAlloc failed.\n"));
146 Status
= EFI_OUT_OF_RESOURCES
;
150 NetbufQueAppend (&MnpServiceData
->FreeNbufQue
, Nbuf
);
153 MnpServiceData
->NbufCnt
+= Index
;
160 Allocate a free NET_BUF from MnpServiceData->FreeNbufQue. If there is none
161 in the queue, first try to allocate some and add them into the queue, then
162 fetch the NET_BUF from the updated FreeNbufQue.
164 @param MnpServiceData Pointer to the MNP_SERVICE_DATA.
166 @return Pointer to the allocated free NET_BUF structure, if NULL the operation is failed.
171 IN MNP_SERVICE_DATA
*MnpServiceData
175 NET_BUF_QUEUE
*FreeNbufQue
;
179 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
181 FreeNbufQue
= &MnpServiceData
->FreeNbufQue
;
183 OldTpl
= NET_RAISE_TPL (NET_TPL_RECYCLE
);
186 // Check whether there are available buffers, or else try to add some.
188 if (FreeNbufQue
->BufNum
== 0) {
190 if ((MnpServiceData
->NbufCnt
+ MNP_NET_BUFFER_INCREASEMENT
) > MNP_MAX_NET_BUFFER_NUM
) {
193 ("MnpAllocNbuf: The maximum NET_BUF size is reached for MNP driver instance %p.\n",
201 Status
= MnpAddFreeNbuf (MnpServiceData
, MNP_NET_BUFFER_INCREASEMENT
);
202 if (EFI_ERROR (Status
)) {
205 ("MnpAllocNbuf: Failed to add NET_BUFs into the FreeNbufQue, %r.\n",
209 // Don't return NULL, perhaps MnpAddFreeNbuf does add some NET_BUFs but
210 // the amount is less than MNP_NET_BUFFER_INCREASEMENT.
215 Nbuf
= NetbufQueRemove (FreeNbufQue
);
218 // Increase the RefCnt.
225 NET_RESTORE_TPL (OldTpl
);
232 Try to reclaim the Nbuf into the buffer pool.
234 @param MnpServiceData Pointer to the mnp service context data.
235 @param Nbuf Pointer to the NET_BUF to free.
242 IN MNP_SERVICE_DATA
*MnpServiceData
,
248 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
249 ASSERT (Nbuf
->RefCnt
> 1);
251 OldTpl
= NET_RAISE_TPL (NET_TPL_RECYCLE
);
255 if (Nbuf
->RefCnt
== 1) {
257 // Trim all buffer contained in the Nbuf, then append it to the NbufQue.
259 NetbufTrim (Nbuf
, Nbuf
->TotalSize
, NET_BUF_TAIL
);
260 NetbufQueAppend (&MnpServiceData
->FreeNbufQue
, Nbuf
);
263 NET_RESTORE_TPL (OldTpl
);
268 Initialize the mnp service context data.
270 @param MnpServiceData Pointer to the mnp service context data.
271 @param Snp Pointer to the simple network protocol.
273 @retval EFI_SUCCESS The mnp service context is initialized.
274 @retval Other Some error occurs.
278 MnpInitializeServiceData (
279 IN MNP_SERVICE_DATA
*MnpServiceData
,
280 IN EFI_HANDLE ImageHandle
,
281 IN EFI_HANDLE ControllerHandle
285 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
286 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
288 MnpServiceData
->Signature
= MNP_SERVICE_DATA_SIGNATURE
;
290 MnpServiceData
->ControllerHandle
= ControllerHandle
;
293 // Copy the ServiceBinding structure.
295 MnpServiceData
->ServiceBinding
= mMnpServiceBindingProtocol
;
298 // Open the Simple Network protocol.
300 Status
= gBS
->OpenProtocol (
302 &gEfiSimpleNetworkProtocolGuid
,
306 EFI_OPEN_PROTOCOL_BY_DRIVER
308 if (EFI_ERROR (Status
)) {
309 return EFI_UNSUPPORTED
;
316 MnpServiceData
->Snp
= Snp
;
317 MnpServiceData
->Mtu
= SnpMode
->MaxPacketSize
;
320 // Initialize the lists.
322 NetListInit (&MnpServiceData
->GroupAddressList
);
323 NetListInit (&MnpServiceData
->ChildrenList
);
326 // Get the buffer length used to allocate NET_BUF to hold data received
327 // from SNP. Do this before fill the FreeNetBufQue.
329 MnpServiceData
->BufferLength
= MnpServiceData
->Mtu
+ SnpMode
->MediaHeaderSize
+ NET_ETHER_FCS_SIZE
;
332 // Initialize the FreeNetBufQue and pre-allocate some NET_BUFs.
334 NetbufQueInit (&MnpServiceData
->FreeNbufQue
);
335 Status
= MnpAddFreeNbuf (MnpServiceData
, MNP_INIT_NET_BUFFER_NUM
);
336 if (EFI_ERROR (Status
)) {
338 MNP_DEBUG_ERROR (("MnpInitializeServiceData: MnpAddFreeNbuf failed, %r.\n", Status
));
342 // Get one NET_BUF from the FreeNbufQue for rx cache.
344 MnpServiceData
->RxNbufCache
= MnpAllocNbuf (MnpServiceData
);
346 MnpServiceData
->RxNbufCache
,
347 MnpServiceData
->BufferLength
,
352 // Allocate buffer pool for tx.
354 MnpServiceData
->TxBuf
= NetAllocatePool (MnpServiceData
->Mtu
+ SnpMode
->MediaHeaderSize
);
355 if (MnpServiceData
->TxBuf
== NULL
) {
357 MNP_DEBUG_ERROR (("MnpInitializeServiceData: NetAllocatePool failed.\n"));
358 Status
= EFI_OUT_OF_RESOURCES
;
364 // Create the system poll timer.
366 Status
= gBS
->CreateEvent (
367 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
371 &MnpServiceData
->PollTimer
373 if (EFI_ERROR (Status
)) {
375 MNP_DEBUG_ERROR (("MnpInitializeServiceData: CreateEvent for poll timer failed.\n"));
380 // Create the timer for packet timeout check.
382 Status
= gBS
->CreateEvent (
383 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
385 MnpCheckPacketTimeout
,
387 &MnpServiceData
->TimeoutCheckTimer
389 if (EFI_ERROR (Status
)) {
391 MNP_DEBUG_ERROR (("MnpInitializeServiceData: CreateEvent for packet timeout check failed.\n"));
396 // Create the timer for tx timeout check.
398 Status
= gBS
->CreateEvent (
403 &MnpServiceData
->TxTimeoutEvent
405 if (EFI_ERROR (Status
)) {
407 MNP_DEBUG_ERROR (("MnpInitializeServiceData: CreateEvent for tx timeout event failed.\n"));
412 if (EFI_ERROR (Status
)) {
414 // Free the dynamic allocated resources if necessary.
416 if (MnpServiceData
->TimeoutCheckTimer
!= NULL
) {
418 gBS
->CloseEvent (MnpServiceData
->TimeoutCheckTimer
);
421 if (MnpServiceData
->PollTimer
!= NULL
) {
423 gBS
->CloseEvent (MnpServiceData
->PollTimer
);
426 if (MnpServiceData
->TxBuf
!= NULL
) {
428 NetFreePool (MnpServiceData
->TxBuf
);
431 if (MnpServiceData
->RxNbufCache
!= NULL
) {
433 MnpFreeNbuf (MnpServiceData
, MnpServiceData
->RxNbufCache
);
436 if (MnpServiceData
->FreeNbufQue
.BufNum
!= 0) {
438 NetbufQueFlush (&MnpServiceData
->FreeNbufQue
);
447 Flush the mnp service context data.
449 @param MnpServiceData Pointer to the mnp service context data.
455 MnpFlushServiceData (
456 MNP_SERVICE_DATA
*MnpServiceData
459 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
462 // The GroupAddressList must be empty.
464 ASSERT (NetListIsEmpty (&MnpServiceData
->GroupAddressList
));
469 gBS
->CloseEvent (&MnpServiceData
->TxTimeoutEvent
);
470 gBS
->CloseEvent (&MnpServiceData
->TimeoutCheckTimer
);
471 gBS
->CloseEvent (&MnpServiceData
->PollTimer
);
474 // Free the tx buffer.
476 NetFreePool (MnpServiceData
->TxBuf
);
479 // Free the RxNbufCache.
481 MnpFreeNbuf (MnpServiceData
, MnpServiceData
->RxNbufCache
);
484 // Flush the FreeNbufQue.
486 MnpServiceData
->NbufCnt
-= MnpServiceData
->FreeNbufQue
.BufNum
;
487 NetbufQueFlush (&MnpServiceData
->FreeNbufQue
);
491 if (MnpServiceData
->NbufCnt
!= 0) {
493 MNP_DEBUG_WARN (("MnpFlushServiceData: Memory leak, MnpServiceData->NbufCnt != 0.\n"));
500 Initialize the mnp instance context data.
502 @param MnpServiceData Pointer to the mnp service context data.
503 @param Instance Pointer to the mnp instance context data to
510 MnpInitializeInstanceData (
511 IN MNP_SERVICE_DATA
*MnpServiceData
,
512 IN MNP_INSTANCE_DATA
*Instance
515 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
516 ASSERT (Instance
!= NULL
);
519 // Set the signature.
521 Instance
->Signature
= MNP_INSTANCE_DATA_SIGNATURE
;
524 // Copy the MNP Protocol interfaces from the template.
526 Instance
->ManagedNetwork
= mMnpProtocolTemplate
;
529 // Copy the default config data.
531 Instance
->ConfigData
= mMnpDefaultConfigData
;
534 // Initialize the lists.
536 NetListInit (&Instance
->GroupCtrlBlkList
);
537 NetListInit (&Instance
->RcvdPacketQueue
);
538 NetListInit (&Instance
->RxDeliveredPacketQueue
);
541 // Initialize the RxToken Map.
543 NetMapInit (&Instance
->RxTokenMap
);
546 // Save the MnpServiceData info.
548 Instance
->MnpServiceData
= MnpServiceData
;
553 Check whether the token specified by Arg maches the token in Item.
555 @param Map Pointer to the NET_MAP.
556 @param Item Pointer to the NET_MAP_ITEM
557 @param Arg Pointer to the Arg, it's a pointer to the token to
560 @retval EFI_SUCCESS The token specified by Arg is different from the
562 @retval EFI_ACCESS_DENIED The token specified by Arg is the same as that in
569 IN NET_MAP_ITEM
*Item
,
573 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*Token
;
574 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*TokenInItem
;
576 Token
= (EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*) Arg
;
577 TokenInItem
= (EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*) Item
->Key
;
579 if ((Token
== TokenInItem
) || (Token
->Event
== TokenInItem
->Event
)) {
581 // The token is the same either the two tokens equals or the Events in
582 // the two tokens are the same.
584 return EFI_ACCESS_DENIED
;
592 Cancel the token specified by Arg if it matches the token in Item.
594 @param Map Pointer to the NET_MAP.
595 @param Item Pointer to the NET_MAP_ITEM
596 @param Arg Pointer to the Arg, it's a pointer to the token to
599 @retval EFI_SUCCESS The Arg is NULL, and the token in Item is
600 cancelled, or the Arg isn't NULL, and the token in
601 Item is different from the Arg.
602 @retval EFI_ABORTED The Arg isn't NULL, the token in Item mathces the
603 Arg, and the token is cancelled.
609 IN NET_MAP_ITEM
*Item
,
613 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*TokenToCancel
;
615 if ((Arg
!= NULL
) && (Item
->Key
!= Arg
)) {
617 // The token in Item is not the token specified by Arg.
622 TokenToCancel
= (EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*) Item
->Key
;
625 // Cancel this token with status set to EFI_ABORTED.
627 TokenToCancel
->Status
= EFI_ABORTED
;
628 gBS
->SignalEvent (TokenToCancel
->Event
);
631 // Remove the item from the map.
633 NetMapRemoveItem (Map
, Item
, NULL
);
637 // Only abort the token specified by Arg if Arg isn't NULL.
647 Start and initialize the simple network.
649 @param Snp Pointer to the simple network protocol.
651 @retval EFI_SUCCESS The simple network protocol is started.
652 @retval Other Some error occurs.
658 IN EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
663 ASSERT (Snp
!= NULL
);
666 // Start the simple network.
668 Status
= Snp
->Start (Snp
);
670 if (!EFI_ERROR (Status
)) {
672 // Initialize the simple network.
674 Status
= Snp
->Initialize (Snp
, 0, 0);
682 Stop the simple network.
684 @param Snp Pointer to the simple network protocol.
686 @retval EFI_SUCCESS The simple network is stopped.
687 @retval Other Some error occurs.
693 IN EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
698 ASSERT (Snp
!= NULL
);
701 // Shut down the simple network.
703 Status
= Snp
->Shutdown (Snp
);
705 if (!EFI_ERROR (Status
)) {
707 // Stop the simple network.
709 Status
= Snp
->Stop (Snp
);
717 Start the managed network, this function is called when one instance is configured
720 @param MnpServiceData Pointer to the mnp service context data.
721 @param IsConfigUpdate The instance is reconfigured or it's the first time
722 the instanced is configured.
723 @param EnableSystemPoll Enable the system polling or not.
725 @retval EFI_SUCCESS The managed network is started and some
726 configuration is updated.
727 @retval Other Some error occurs.
733 IN MNP_SERVICE_DATA
*MnpServiceData
,
734 IN BOOLEAN IsConfigUpdate
,
735 IN BOOLEAN EnableSystemPoll
739 EFI_TIMER_DELAY TimerOpType
;
741 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
743 Status
= EFI_SUCCESS
;
745 if (!IsConfigUpdate
) {
747 // If it's not a configuration update, increase the configured children number.
749 MnpServiceData
->ConfiguredChildrenNumber
++;
751 if (MnpServiceData
->ConfiguredChildrenNumber
== 1) {
753 // It's the first configured child, start the simple network.
755 Status
= MnpStartSnp (MnpServiceData
->Snp
);
756 if (EFI_ERROR (Status
)) {
758 MNP_DEBUG_ERROR (("MnpStart: MnpStartSnp failed, %r.\n", Status
));
763 // Start the timeout timer.
765 Status
= gBS
->SetTimer (
766 MnpServiceData
->TimeoutCheckTimer
,
768 MNP_TIMEOUT_CHECK_INTERVAL
770 if (EFI_ERROR (Status
)) {
773 ("MnpStart, gBS->SetTimer for TimeoutCheckTimer %r.\n",
781 if (MnpServiceData
->EnableSystemPoll
^ EnableSystemPoll
) {
783 // The EnableSystemPoll differs with the current state, disable or enable
786 TimerOpType
= EnableSystemPoll
? TimerPeriodic
: TimerCancel
;
788 Status
= gBS
->SetTimer (MnpServiceData
->PollTimer
, TimerOpType
, MNP_SYS_POLL_INTERVAL
);
789 if (EFI_ERROR (Status
)) {
791 MNP_DEBUG_ERROR (("MnpStart: gBS->SetTimer for PollTimer failed, %r.\n", Status
));
795 MnpServiceData
->EnableSystemPoll
= EnableSystemPoll
;
799 // Change the receive filters if need.
801 Status
= MnpConfigReceiveFilters (MnpServiceData
);
810 Stop the managed network.
812 @param MnpServiceData Pointer to the mnp service context data.
814 @retval EFI_SUCCESS The managed network is stopped.
815 @retval Other Some error occurs.
821 IN MNP_SERVICE_DATA
*MnpServiceData
826 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
827 ASSERT (MnpServiceData
->ConfiguredChildrenNumber
> 0);
830 // Configure the receive filters.
832 MnpConfigReceiveFilters (MnpServiceData
);
835 // Decrease the children number.
837 MnpServiceData
->ConfiguredChildrenNumber
--;
839 if (MnpServiceData
->ConfiguredChildrenNumber
> 0) {
841 // If there are other configured chilren, return and keep the timers and
842 // simple network unchanged.
848 // No configured children now.
851 if (MnpServiceData
->EnableSystemPoll
) {
853 // The system poll in on, cancel the poll timer.
855 Status
= gBS
->SetTimer (MnpServiceData
->PollTimer
, TimerCancel
, 0);
856 MnpServiceData
->EnableSystemPoll
= FALSE
;
860 // Cancel the timeout timer.
862 Status
= gBS
->SetTimer (MnpServiceData
->TimeoutCheckTimer
, TimerCancel
, 0);
865 // Stop the simple network.
867 Status
= MnpStopSnp (MnpServiceData
->Snp
);
874 Flush the instance's received data.
876 @param Instance Pointer to the mnp instance context data.
882 MnpFlushRcvdDataQueue (
883 IN MNP_INSTANCE_DATA
*Instance
887 MNP_RXDATA_WRAP
*RxDataWrap
;
889 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
891 OldTpl
= NET_RAISE_TPL (NET_TPL_RECYCLE
);
893 while (!NetListIsEmpty (&Instance
->RcvdPacketQueue
)) {
895 // Remove all the Wraps.
897 RxDataWrap
= NET_LIST_HEAD (&Instance
->RcvdPacketQueue
, MNP_RXDATA_WRAP
, WrapEntry
);
900 // Recycle the RxDataWrap.
902 MnpRecycleRxData (NULL
, (VOID
*) RxDataWrap
);
903 Instance
->RcvdPacketQueueSize
--;
906 ASSERT (Instance
->RcvdPacketQueueSize
== 0);
908 NET_RESTORE_TPL (OldTpl
);
913 Configure the Instance using ConfigData.
915 @param Instance Pointer to the mnp instance context data.
916 @param ConfigData Pointer to the configuration data used to configure
919 @retval EFI_SUCCESS The Instance is configured.
920 @retval EFI_UNSUPPORTED EnableReceiveTimestamps is on and the
921 implementation doesn't support it.
922 @retval Other Some error occurs.
926 MnpConfigureInstance (
927 IN MNP_INSTANCE_DATA
*Instance
,
928 IN EFI_MANAGED_NETWORK_CONFIG_DATA
*ConfigData OPTIONAL
932 MNP_SERVICE_DATA
*MnpServiceData
;
933 EFI_MANAGED_NETWORK_CONFIG_DATA
*OldConfigData
;
934 EFI_MANAGED_NETWORK_CONFIG_DATA
*NewConfigData
;
935 BOOLEAN IsConfigUpdate
;
937 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
939 if ((ConfigData
!= NULL
) && ConfigData
->EnableReceiveTimestamps
) {
941 // Don't support timestamp.
943 return EFI_UNSUPPORTED
;
946 Status
= EFI_SUCCESS
;
948 MnpServiceData
= Instance
->MnpServiceData
;
949 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
951 IsConfigUpdate
= (BOOLEAN
) ((Instance
->Configured
) && (ConfigData
!= NULL
));
953 OldConfigData
= &Instance
->ConfigData
;
954 NewConfigData
= ConfigData
;
955 if (NewConfigData
== NULL
) {
957 // Restore back the default config data if a reset of this instance
960 NewConfigData
= &mMnpDefaultConfigData
;
964 // Reset the instance's receive filter.
966 Instance
->ReceiveFilter
= 0;
969 // Clear the receive counters according to the old ConfigData.
971 if (OldConfigData
->EnableUnicastReceive
) {
972 MnpServiceData
->UnicastCount
--;
975 if (OldConfigData
->EnableMulticastReceive
) {
976 MnpServiceData
->MulticastCount
--;
979 if (OldConfigData
->EnableBroadcastReceive
) {
980 MnpServiceData
->BroadcastCount
--;
983 if (OldConfigData
->EnablePromiscuousReceive
) {
984 MnpServiceData
->PromiscuousCount
--;
988 // Set the receive filter counters and the receive filter of the
989 // instance according to the new ConfigData.
991 if (NewConfigData
->EnableUnicastReceive
) {
992 MnpServiceData
->UnicastCount
++;
993 Instance
->ReceiveFilter
|= MNP_RECEIVE_UNICAST
;
996 if (NewConfigData
->EnableMulticastReceive
) {
997 MnpServiceData
->MulticastCount
++;
1000 if (NewConfigData
->EnableBroadcastReceive
) {
1001 MnpServiceData
->BroadcastCount
++;
1002 Instance
->ReceiveFilter
|= MNP_RECEIVE_BROADCAST
;
1005 if (NewConfigData
->EnablePromiscuousReceive
) {
1006 MnpServiceData
->PromiscuousCount
++;
1009 if (OldConfigData
->FlushQueuesOnReset
) {
1011 MnpFlushRcvdDataQueue (Instance
);
1014 if (ConfigData
== NULL
) {
1016 NetMapIterate (&Instance
->RxTokenMap
, MnpCancelTokens
, NULL
);
1019 if (!NewConfigData
->EnableMulticastReceive
) {
1021 MnpGroupOp (Instance
, FALSE
, NULL
, NULL
);
1025 // Save the new configuration data.
1027 *OldConfigData
= *NewConfigData
;
1029 Instance
->Configured
= (BOOLEAN
) (ConfigData
!= NULL
);
1031 if (Instance
->Configured
) {
1033 // The instance is configured, start the Mnp.
1038 !NewConfigData
->DisableBackgroundPolling
1042 // The instance is changed to the unconfigured state, stop the Mnp.
1044 Status
= MnpStop (MnpServiceData
);
1052 Configure the Snp receive filters according to the instances' receive filter
1055 @param MnpServiceData Pointer to the mnp service context data.
1057 @retval EFI_SUCCESS The receive filters is configured.
1058 @retval EFI_OUT_OF_RESOURCES The receive filters can't be configured due to lack
1064 MnpConfigReceiveFilters (
1065 IN MNP_SERVICE_DATA
*MnpServiceData
1069 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1070 EFI_MAC_ADDRESS
*MCastFilter
;
1071 UINT32 MCastFilterCnt
;
1072 UINT32 EnableFilterBits
;
1073 UINT32 DisableFilterBits
;
1074 BOOLEAN ResetMCastFilters
;
1075 NET_LIST_ENTRY
*Entry
;
1077 MNP_GROUP_ADDRESS
*GroupAddress
;
1079 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
1081 Snp
= MnpServiceData
->Snp
;
1084 // Initialize the enable filter and disable filter.
1086 EnableFilterBits
= 0;
1087 DisableFilterBits
= Snp
->Mode
->ReceiveFilterMask
;
1089 if (MnpServiceData
->UnicastCount
!= 0) {
1091 // Enable unicast if any instance wants to receive unicast.
1093 EnableFilterBits
|= EFI_SIMPLE_NETWORK_RECEIVE_UNICAST
;
1096 if (MnpServiceData
->BroadcastCount
!= 0) {
1098 // Enable broadcast if any instance wants to receive broadcast.
1100 EnableFilterBits
|= EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST
;
1105 ResetMCastFilters
= TRUE
;
1107 if ((MnpServiceData
->MulticastCount
!= 0) && (MnpServiceData
->GroupAddressCount
!= 0)) {
1109 // There are instances configured to receive multicast and already some group
1110 // addresses are joined.
1113 ResetMCastFilters
= FALSE
;
1115 if (MnpServiceData
->GroupAddressCount
<= Snp
->Mode
->MaxMCastFilterCount
) {
1117 // The joind group address is less than simple network's maximum count.
1118 // Just configure the snp to do the multicast filtering.
1121 EnableFilterBits
|= EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST
;
1124 // Allocate pool for the mulicast addresses.
1126 MCastFilterCnt
= MnpServiceData
->GroupAddressCount
;
1127 MCastFilter
= NetAllocatePool (sizeof (EFI_MAC_ADDRESS
) * MCastFilterCnt
);
1128 if (MCastFilter
== NULL
) {
1130 MNP_DEBUG_ERROR (("MnpConfigReceiveFilters: Failed to allocate memory resource for MCastFilter.\n"));
1131 return EFI_OUT_OF_RESOURCES
;
1135 // Fill the multicast HW address buffer.
1138 NET_LIST_FOR_EACH (Entry
, &MnpServiceData
->GroupAddressList
) {
1140 GroupAddress
= NET_LIST_USER_STRUCT (Entry
, MNP_GROUP_ADDRESS
, AddrEntry
);
1141 *(MCastFilter
+ Index
) = GroupAddress
->Address
;
1144 ASSERT (Index
<= MCastFilterCnt
);
1148 // The maximum multicast is reached, set the filter to be promiscuous
1152 if (Snp
->Mode
->ReceiveFilterMask
& EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST
) {
1153 EnableFilterBits
|= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST
;
1156 // Either MULTICAST or PROMISCUOUS_MULTICAST is not supported by Snp,
1157 // set the NIC to be promiscuous although this will tremendously degrade
1160 EnableFilterBits
|= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS
;
1165 if (MnpServiceData
->PromiscuousCount
!= 0) {
1167 // Enable promiscuous if any instance wants to receive promiscuous.
1169 EnableFilterBits
|= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS
;
1173 // Set the disable filter.
1175 DisableFilterBits
^= EnableFilterBits
;
1178 // Configure the receive filters of SNP.
1180 Status
= Snp
->ReceiveFilters (
1189 if (EFI_ERROR (Status
)) {
1192 ("MnpConfigReceiveFilters: Snp->ReceiveFilters failed, %r.\n",
1198 if (MCastFilter
!= NULL
) {
1200 // Free the buffer used to hold the group addresses.
1202 NetFreePool (MCastFilter
);
1210 Add a group address control block which controls the MacAddress for
1213 @param Instance Pointer to the mnp instance context data.
1214 @param CtrlBlk Pointer to the group address control block.
1215 @param GroupAddress Pointer to the group adress.
1216 @param MacAddress Pointer to the mac address.
1217 @param HwAddressSize The hardware address size.
1219 @retval EFI_SUCCESS The group address control block is added.
1220 @retval EFI_OUT_OF_RESOURCE Failed due to lack of memory resources.
1225 MnpGroupOpAddCtrlBlk (
1226 IN MNP_INSTANCE_DATA
*Instance
,
1227 IN MNP_GROUP_CONTROL_BLOCK
*CtrlBlk
,
1228 IN MNP_GROUP_ADDRESS
*GroupAddress OPTIONAL
,
1229 IN EFI_MAC_ADDRESS
*MacAddress
,
1230 IN UINT32 HwAddressSize
1233 MNP_SERVICE_DATA
*MnpServiceData
;
1235 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
1237 MnpServiceData
= Instance
->MnpServiceData
;
1238 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
1240 if (GroupAddress
== NULL
) {
1242 ASSERT (MacAddress
!= NULL
);
1245 // Allocate a new GroupAddress to be added into MNP's GroupAddressList.
1247 GroupAddress
= NetAllocatePool (sizeof (MNP_GROUP_ADDRESS
));
1248 if (GroupAddress
== NULL
) {
1250 MNP_DEBUG_ERROR (("MnpGroupOpFormCtrlBlk: Failed to allocate memory resource.\n"));
1252 return EFI_OUT_OF_RESOURCES
;
1255 GroupAddress
->Address
= *MacAddress
;
1256 GroupAddress
->RefCnt
= 0;
1258 &MnpServiceData
->GroupAddressList
,
1259 &GroupAddress
->AddrEntry
1261 MnpServiceData
->GroupAddressCount
++;
1265 // Increase the RefCnt.
1267 GroupAddress
->RefCnt
++;
1270 // Add the CtrlBlk into the instance's GroupCtrlBlkList.
1272 CtrlBlk
->GroupAddress
= GroupAddress
;
1273 NetListInsertTail (&Instance
->GroupCtrlBlkList
, &CtrlBlk
->CtrlBlkEntry
);
1280 Delete a group control block from the instance. If the controlled group address's
1281 reference count reaches zero, the group address is removed too.
1283 @param Instance Pointer to the instance context data.
1284 @param CtrlBlk Pointer to the group control block to delete.
1286 @return The group address controlled by the control block is no longer used or not.
1291 MnpGroupOpDelCtrlBlk (
1292 IN MNP_INSTANCE_DATA
*Instance
,
1293 IN MNP_GROUP_CONTROL_BLOCK
*CtrlBlk
1296 MNP_SERVICE_DATA
*MnpServiceData
;
1297 MNP_GROUP_ADDRESS
*GroupAddress
;
1299 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
1301 MnpServiceData
= Instance
->MnpServiceData
;
1302 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
1305 // Remove and free the CtrlBlk.
1307 GroupAddress
= CtrlBlk
->GroupAddress
;
1308 NetListRemoveEntry (&CtrlBlk
->CtrlBlkEntry
);
1309 NetFreePool (CtrlBlk
);
1311 ASSERT (GroupAddress
->RefCnt
> 0);
1314 // Count down the RefCnt.
1316 GroupAddress
->RefCnt
--;
1318 if (GroupAddress
->RefCnt
== 0) {
1320 // Free this GroupAddress entry if no instance uses it.
1322 MnpServiceData
->GroupAddressCount
--;
1323 NetListRemoveEntry (&GroupAddress
->AddrEntry
);
1324 NetFreePool (GroupAddress
);
1334 Do the group operations for this instance.
1336 @param Instance Pointer to the instance context data.
1337 @param JoinFlag Set to TRUE to join a group. Set to TRUE to leave a
1339 @param MacAddress Pointer to the group address to join or leave.
1340 @param CtrlBlk Pointer to the group control block if JoinFlag if
1343 @retval EFI_SUCCESS The group operation finished.
1344 @retval Other Some error occurs.
1349 IN MNP_INSTANCE_DATA
*Instance
,
1350 IN BOOLEAN JoinFlag
,
1351 IN EFI_MAC_ADDRESS
*MacAddress OPTIONAL
,
1352 IN MNP_GROUP_CONTROL_BLOCK
*CtrlBlk OPTIONAL
1355 MNP_SERVICE_DATA
*MnpServiceData
;
1356 NET_LIST_ENTRY
*Entry
;
1357 NET_LIST_ENTRY
*NextEntry
;
1358 MNP_GROUP_ADDRESS
*GroupAddress
;
1359 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
1360 MNP_GROUP_CONTROL_BLOCK
*NewCtrlBlk
;
1362 BOOLEAN AddressExist
;
1365 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
1367 MnpServiceData
= Instance
->MnpServiceData
;
1368 SnpMode
= MnpServiceData
->Snp
->Mode
;
1372 // A new gropu address is to be added.
1375 GroupAddress
= NULL
;
1376 AddressExist
= FALSE
;
1379 // Allocate memory for the control block.
1381 NewCtrlBlk
= NetAllocatePool (sizeof (MNP_GROUP_CONTROL_BLOCK
));
1382 if (NewCtrlBlk
== NULL
) {
1384 MNP_DEBUG_ERROR (("MnpGroupOp: Failed to allocate memory resource.\n"));
1385 return EFI_OUT_OF_RESOURCES
;
1388 NET_LIST_FOR_EACH (Entry
, &MnpServiceData
->GroupAddressList
) {
1390 // Check whether the MacAddress is already joined by other instances.
1392 GroupAddress
= NET_LIST_USER_STRUCT (Entry
, MNP_GROUP_ADDRESS
, AddrEntry
);
1393 if (0 == NetCompareMem (
1395 &GroupAddress
->Address
,
1396 SnpMode
->HwAddressSize
1399 AddressExist
= TRUE
;
1404 if (!AddressExist
) {
1405 GroupAddress
= NULL
;
1409 // Add the GroupAddress for this instance.
1411 Status
= MnpGroupOpAddCtrlBlk (
1416 SnpMode
->HwAddressSize
1418 if (EFI_ERROR (Status
)) {
1426 if (MacAddress
!= NULL
) {
1428 ASSERT (CtrlBlk
!= NULL
);
1431 // Leave the specific multicast mac address.
1433 NeedUpdate
= MnpGroupOpDelCtrlBlk (Instance
, CtrlBlk
);
1436 // Leave all multicast mac addresses.
1440 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Instance
->GroupCtrlBlkList
) {
1442 NewCtrlBlk
= NET_LIST_USER_STRUCT (
1444 MNP_GROUP_CONTROL_BLOCK
,
1448 // Update is required if the group address left is no longer used
1449 // by other instances.
1451 NeedUpdate
= MnpGroupOpDelCtrlBlk (Instance
, NewCtrlBlk
);
1456 Status
= EFI_SUCCESS
;
1460 // Reconfigure the receive filters if necessary.
1462 Status
= MnpConfigReceiveFilters (MnpServiceData
);