3 Copyright (c) 2005 - 2008, 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
= {
57 IN MNP_SERVICE_DATA
*MnpServiceData
,
63 IN EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
68 IN EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
73 IN MNP_SERVICE_DATA
*MnpServiceData
,
74 IN BOOLEAN IsConfigUpdate
,
75 IN BOOLEAN EnableSystemPoll
80 IN MNP_SERVICE_DATA
*MnpServiceData
84 MnpConfigReceiveFilters (
85 IN MNP_SERVICE_DATA
*MnpServiceData
89 MnpGroupOpAddCtrlBlk (
90 IN MNP_INSTANCE_DATA
*Instance
,
91 IN MNP_GROUP_CONTROL_BLOCK
*CtrlBlk
,
92 IN MNP_GROUP_ADDRESS
*GroupAddress OPTIONAL
,
93 IN EFI_MAC_ADDRESS
*MacAddress
,
94 IN UINT32 HwAddressSize
98 MnpGroupOpDelCtrlBlk (
99 IN MNP_INSTANCE_DATA
*Instance
,
100 IN MNP_GROUP_CONTROL_BLOCK
*CtrlBlk
105 Add some NET_BUF into MnpServiceData->FreeNbufQue. The buffer length of
106 the NET_BUF is specified by MnpServiceData->BufferLength.
108 @param MnpServiceData Pointer to the MNP_SERVICE_DATA.
109 @param Count Number of NET_BUFFERs to add.
111 @retval EFI_SUCCESS The specified amount of NET_BUFs are allocated and
112 added into MnpServiceData->FreeNbufQue.
113 @retval EFI_OUT_OF_RESOURCES Failed to allocate a NET_BUF structure.
118 IN MNP_SERVICE_DATA
*MnpServiceData
,
126 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
127 ASSERT ((Count
> 0) && (MnpServiceData
->BufferLength
> 0));
129 Status
= EFI_SUCCESS
;
131 for (Index
= 0; Index
< Count
; Index
++) {
133 Nbuf
= NetbufAlloc (MnpServiceData
->BufferLength
+ MnpServiceData
->PaddingSize
);
136 DEBUG ((EFI_D_ERROR
, "MnpAddFreeNbuf: NetBufAlloc failed.\n"));
137 Status
= EFI_OUT_OF_RESOURCES
;
141 if (MnpServiceData
->PaddingSize
> 0) {
143 // Pad padding bytes before the media header
145 NetbufAllocSpace (Nbuf
, MnpServiceData
->PaddingSize
, NET_BUF_TAIL
);
146 NetbufTrim (Nbuf
, MnpServiceData
->PaddingSize
, NET_BUF_HEAD
);
149 NetbufQueAppend (&MnpServiceData
->FreeNbufQue
, Nbuf
);
152 MnpServiceData
->NbufCnt
+= Index
;
159 Allocate a free NET_BUF from MnpServiceData->FreeNbufQue. If there is none
160 in the queue, first try to allocate some and add them into the queue, then
161 fetch the NET_BUF from the updated FreeNbufQue.
163 @param MnpServiceData Pointer to the MNP_SERVICE_DATA.
165 @return Pointer to the allocated free NET_BUF structure, if NULL the operation is failed.
170 IN MNP_SERVICE_DATA
*MnpServiceData
174 NET_BUF_QUEUE
*FreeNbufQue
;
178 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
180 FreeNbufQue
= &MnpServiceData
->FreeNbufQue
;
182 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
185 // Check whether there are available buffers, or else try to add some.
187 if (FreeNbufQue
->BufNum
== 0) {
189 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
)) {
206 "MnpAllocNbuf: Failed to add NET_BUFs into the FreeNbufQue, %r.\n",
210 // Don't return NULL, perhaps MnpAddFreeNbuf does add some NET_BUFs but
211 // the amount is less than MNP_NET_BUFFER_INCREASEMENT.
216 Nbuf
= NetbufQueRemove (FreeNbufQue
);
219 // Increase the RefCnt.
226 gBS
->RestoreTPL (OldTpl
);
233 Try to reclaim the Nbuf into the buffer pool.
235 @param MnpServiceData Pointer to the mnp service context data.
236 @param Nbuf Pointer to the NET_BUF to free.
243 IN MNP_SERVICE_DATA
*MnpServiceData
,
249 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
250 ASSERT (Nbuf
->RefCnt
> 1);
252 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
256 if (Nbuf
->RefCnt
== 1) {
258 // Trim all buffer contained in the Nbuf, then append it to the NbufQue.
260 NetbufTrim (Nbuf
, Nbuf
->TotalSize
, NET_BUF_TAIL
);
261 NetbufQueAppend (&MnpServiceData
->FreeNbufQue
, Nbuf
);
264 gBS
->RestoreTPL (OldTpl
);
269 Initialize the mnp service context data.
271 @param MnpServiceData Pointer to the mnp service context data.
272 @param Snp Pointer to the simple network protocol.
274 @retval EFI_SUCCESS The mnp service context is initialized.
275 @retval Other Some error occurs.
279 MnpInitializeServiceData (
280 IN MNP_SERVICE_DATA
*MnpServiceData
,
281 IN EFI_HANDLE ImageHandle
,
282 IN EFI_HANDLE ControllerHandle
286 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
287 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
289 MnpServiceData
->Signature
= MNP_SERVICE_DATA_SIGNATURE
;
291 MnpServiceData
->ControllerHandle
= ControllerHandle
;
294 // Copy the ServiceBinding structure.
296 MnpServiceData
->ServiceBinding
= mMnpServiceBindingProtocol
;
299 // Open the Simple Network protocol.
301 Status
= gBS
->OpenProtocol (
303 &gEfiSimpleNetworkProtocolGuid
,
307 EFI_OPEN_PROTOCOL_BY_DRIVER
309 if (EFI_ERROR (Status
)) {
310 return EFI_UNSUPPORTED
;
317 MnpServiceData
->Snp
= Snp
;
318 MnpServiceData
->Mtu
= SnpMode
->MaxPacketSize
;
321 // Initialize the lists.
323 InitializeListHead (&MnpServiceData
->GroupAddressList
);
324 InitializeListHead (&MnpServiceData
->ChildrenList
);
327 // Get the buffer length used to allocate NET_BUF to hold data received
328 // from SNP. Do this before fill the FreeNetBufQue.
330 MnpServiceData
->BufferLength
= MnpServiceData
->Mtu
+ SnpMode
->MediaHeaderSize
+ NET_ETHER_FCS_SIZE
;
333 // Make sure the protocol headers immediately following the media header
336 MnpServiceData
->PaddingSize
= (4 - SnpMode
->MediaHeaderSize
) & 0x3;
339 // Initialize the FreeNetBufQue and pre-allocate some NET_BUFs.
341 NetbufQueInit (&MnpServiceData
->FreeNbufQue
);
342 Status
= MnpAddFreeNbuf (MnpServiceData
, MNP_INIT_NET_BUFFER_NUM
);
343 if (EFI_ERROR (Status
)) {
345 DEBUG ((EFI_D_ERROR
, "MnpInitializeServiceData: MnpAddFreeNbuf failed, %r.\n", Status
));
349 // Get one NET_BUF from the FreeNbufQue for rx cache.
351 MnpServiceData
->RxNbufCache
= MnpAllocNbuf (MnpServiceData
);
353 MnpServiceData
->RxNbufCache
,
354 MnpServiceData
->BufferLength
,
359 // Allocate buffer pool for tx.
361 MnpServiceData
->TxBuf
= AllocatePool (MnpServiceData
->Mtu
+ SnpMode
->MediaHeaderSize
);
362 if (MnpServiceData
->TxBuf
== NULL
) {
364 DEBUG ((EFI_D_ERROR
, "MnpInitializeServiceData: AllocatePool failed.\n"));
365 Status
= EFI_OUT_OF_RESOURCES
;
371 // Create the system poll timer.
373 Status
= gBS
->CreateEvent (
374 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
378 &MnpServiceData
->PollTimer
380 if (EFI_ERROR (Status
)) {
382 DEBUG ((EFI_D_ERROR
, "MnpInitializeServiceData: CreateEvent for poll timer failed.\n"));
387 // Create the timer for packet timeout check.
389 Status
= gBS
->CreateEvent (
390 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
392 MnpCheckPacketTimeout
,
394 &MnpServiceData
->TimeoutCheckTimer
396 if (EFI_ERROR (Status
)) {
398 DEBUG ((EFI_D_ERROR
, "MnpInitializeServiceData: CreateEvent for packet timeout check failed.\n"));
403 // Create the timer for tx timeout check.
405 Status
= gBS
->CreateEvent (
410 &MnpServiceData
->TxTimeoutEvent
412 if (EFI_ERROR (Status
)) {
414 DEBUG ((EFI_D_ERROR
, "MnpInitializeServiceData: CreateEvent for tx timeout event failed.\n"));
419 if (EFI_ERROR (Status
)) {
421 // Free the dynamic allocated resources if necessary.
423 if (MnpServiceData
->TimeoutCheckTimer
!= NULL
) {
425 gBS
->CloseEvent (MnpServiceData
->TimeoutCheckTimer
);
428 if (MnpServiceData
->PollTimer
!= NULL
) {
430 gBS
->CloseEvent (MnpServiceData
->PollTimer
);
433 if (MnpServiceData
->TxBuf
!= NULL
) {
435 gBS
->FreePool (MnpServiceData
->TxBuf
);
438 if (MnpServiceData
->RxNbufCache
!= NULL
) {
440 MnpFreeNbuf (MnpServiceData
, MnpServiceData
->RxNbufCache
);
443 if (MnpServiceData
->FreeNbufQue
.BufNum
!= 0) {
445 NetbufQueFlush (&MnpServiceData
->FreeNbufQue
);
454 Flush the mnp service context data.
456 @param MnpServiceData Pointer to the mnp service context data.
462 MnpFlushServiceData (
463 IN MNP_SERVICE_DATA
*MnpServiceData
,
464 IN EFI_HANDLE ImageHandle
467 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
470 // The GroupAddressList must be empty.
472 ASSERT (IsListEmpty (&MnpServiceData
->GroupAddressList
));
477 gBS
->CloseEvent (&MnpServiceData
->TxTimeoutEvent
);
478 gBS
->CloseEvent (&MnpServiceData
->TimeoutCheckTimer
);
479 gBS
->CloseEvent (&MnpServiceData
->PollTimer
);
482 // Free the tx buffer.
484 gBS
->FreePool (MnpServiceData
->TxBuf
);
487 // Free the RxNbufCache.
489 MnpFreeNbuf (MnpServiceData
, MnpServiceData
->RxNbufCache
);
492 // Flush the FreeNbufQue.
494 MnpServiceData
->NbufCnt
-= MnpServiceData
->FreeNbufQue
.BufNum
;
495 NetbufQueFlush (&MnpServiceData
->FreeNbufQue
);
499 if (MnpServiceData
->NbufCnt
!= 0) {
501 DEBUG ((EFI_D_WARN
, "MnpFlushServiceData: Memory leak, MnpServiceData->NbufCnt != 0.\n"));
506 // Close the Simple Network Protocol.
509 MnpServiceData
->ControllerHandle
,
510 &gEfiSimpleNetworkProtocolGuid
,
512 MnpServiceData
->ControllerHandle
518 Initialize the mnp instance context data.
520 @param MnpServiceData Pointer to the mnp service context data.
521 @param Instance Pointer to the mnp instance context data to
528 MnpInitializeInstanceData (
529 IN MNP_SERVICE_DATA
*MnpServiceData
,
530 IN MNP_INSTANCE_DATA
*Instance
533 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
534 ASSERT (Instance
!= NULL
);
537 // Set the signature.
539 Instance
->Signature
= MNP_INSTANCE_DATA_SIGNATURE
;
542 // Copy the MNP Protocol interfaces from the template.
544 CopyMem (&Instance
->ManagedNetwork
, &mMnpProtocolTemplate
, sizeof (Instance
->ManagedNetwork
));
547 // Copy the default config data.
549 CopyMem (&Instance
->ConfigData
, &mMnpDefaultConfigData
, sizeof (Instance
->ConfigData
));
552 // Initialize the lists.
554 InitializeListHead (&Instance
->GroupCtrlBlkList
);
555 InitializeListHead (&Instance
->RcvdPacketQueue
);
556 InitializeListHead (&Instance
->RxDeliveredPacketQueue
);
559 // Initialize the RxToken Map.
561 NetMapInit (&Instance
->RxTokenMap
);
564 // Save the MnpServiceData info.
566 Instance
->MnpServiceData
= MnpServiceData
;
571 Check whether the token specified by Arg maches the token in Item.
573 @param Map Pointer to the NET_MAP.
574 @param Item Pointer to the NET_MAP_ITEM
575 @param Arg Pointer to the Arg, it's a pointer to the token to
578 @retval EFI_SUCCESS The token specified by Arg is different from the
580 @retval EFI_ACCESS_DENIED The token specified by Arg is the same as that in
587 IN NET_MAP_ITEM
*Item
,
591 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*Token
;
592 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*TokenInItem
;
594 Token
= (EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*) Arg
;
595 TokenInItem
= (EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*) Item
->Key
;
597 if ((Token
== TokenInItem
) || (Token
->Event
== TokenInItem
->Event
)) {
599 // The token is the same either the two tokens equals or the Events in
600 // the two tokens are the same.
602 return EFI_ACCESS_DENIED
;
610 Cancel the token specified by Arg if it matches the token in Item.
612 @param Map Pointer to the NET_MAP.
613 @param Item Pointer to the NET_MAP_ITEM
614 @param Arg Pointer to the Arg, it's a pointer to the token to
617 @retval EFI_SUCCESS The Arg is NULL, and the token in Item is
618 cancelled, or the Arg isn't NULL, and the token in
619 Item is different from the Arg.
620 @retval EFI_ABORTED The Arg isn't NULL, the token in Item mathces the
621 Arg, and the token is cancelled.
627 IN NET_MAP_ITEM
*Item
,
631 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*TokenToCancel
;
633 if ((Arg
!= NULL
) && (Item
->Key
!= Arg
)) {
635 // The token in Item is not the token specified by Arg.
640 TokenToCancel
= (EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*) Item
->Key
;
643 // Remove the item from the map.
645 NetMapRemoveItem (Map
, Item
, NULL
);
648 // Cancel this token with status set to EFI_ABORTED.
650 TokenToCancel
->Status
= EFI_ABORTED
;
651 gBS
->SignalEvent (TokenToCancel
->Event
);
655 // Only abort the token specified by Arg if Arg isn't NULL.
665 Start and initialize the simple network.
667 @param Snp Pointer to the simple network protocol.
669 @retval EFI_SUCCESS The simple network protocol is started.
670 @retval Other Some error occurs.
675 IN EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
680 ASSERT (Snp
!= NULL
);
683 // Start the simple network.
685 Status
= Snp
->Start (Snp
);
687 if (!EFI_ERROR (Status
)) {
689 // Initialize the simple network.
691 Status
= Snp
->Initialize (Snp
, 0, 0);
699 Stop the simple network.
701 @param Snp Pointer to the simple network protocol.
703 @retval EFI_SUCCESS The simple network is stopped.
704 @retval Other Some error occurs.
709 IN EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
714 ASSERT (Snp
!= NULL
);
717 // Shut down the simple network.
719 Status
= Snp
->Shutdown (Snp
);
721 if (!EFI_ERROR (Status
)) {
723 // Stop the simple network.
725 Status
= Snp
->Stop (Snp
);
733 Start the managed network, this function is called when one instance is configured
736 @param MnpServiceData Pointer to the mnp service context data.
737 @param IsConfigUpdate The instance is reconfigured or it's the first time
738 the instanced is configured.
739 @param EnableSystemPoll Enable the system polling or not.
741 @retval EFI_SUCCESS The managed network is started and some
742 configuration is updated.
743 @retval Other Some error occurs.
748 IN MNP_SERVICE_DATA
*MnpServiceData
,
749 IN BOOLEAN IsConfigUpdate
,
750 IN BOOLEAN EnableSystemPoll
754 EFI_TIMER_DELAY TimerOpType
;
756 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
758 Status
= EFI_SUCCESS
;
760 if (!IsConfigUpdate
) {
762 // If it's not a configuration update, increase the configured children number.
764 MnpServiceData
->ConfiguredChildrenNumber
++;
766 if (MnpServiceData
->ConfiguredChildrenNumber
== 1) {
768 // It's the first configured child, start the simple network.
770 Status
= MnpStartSnp (MnpServiceData
->Snp
);
771 if (EFI_ERROR (Status
)) {
773 DEBUG ((EFI_D_ERROR
, "MnpStart: MnpStartSnp failed, %r.\n", Status
));
778 // Start the timeout timer.
780 Status
= gBS
->SetTimer (
781 MnpServiceData
->TimeoutCheckTimer
,
783 MNP_TIMEOUT_CHECK_INTERVAL
785 if (EFI_ERROR (Status
)) {
789 "MnpStart, gBS->SetTimer for TimeoutCheckTimer %r.\n",
797 if (MnpServiceData
->EnableSystemPoll
^ EnableSystemPoll
) {
799 // The EnableSystemPoll differs with the current state, disable or enable
802 TimerOpType
= EnableSystemPoll
? TimerPeriodic
: TimerCancel
;
804 Status
= gBS
->SetTimer (MnpServiceData
->PollTimer
, TimerOpType
, MNP_SYS_POLL_INTERVAL
);
805 if (EFI_ERROR (Status
)) {
807 DEBUG ((EFI_D_ERROR
, "MnpStart: gBS->SetTimer for PollTimer failed, %r.\n", Status
));
811 MnpServiceData
->EnableSystemPoll
= EnableSystemPoll
;
815 // Change the receive filters if need.
817 Status
= MnpConfigReceiveFilters (MnpServiceData
);
826 Stop the managed network.
828 @param MnpServiceData Pointer to the mnp service context data.
830 @retval EFI_SUCCESS The managed network is stopped.
831 @retval Other Some error occurs.
836 IN MNP_SERVICE_DATA
*MnpServiceData
841 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
842 ASSERT (MnpServiceData
->ConfiguredChildrenNumber
> 0);
845 // Configure the receive filters.
847 MnpConfigReceiveFilters (MnpServiceData
);
850 // Decrease the children number.
852 MnpServiceData
->ConfiguredChildrenNumber
--;
854 if (MnpServiceData
->ConfiguredChildrenNumber
> 0) {
856 // If there are other configured chilren, return and keep the timers and
857 // simple network unchanged.
863 // No configured children now.
866 if (MnpServiceData
->EnableSystemPoll
) {
868 // The system poll in on, cancel the poll timer.
870 Status
= gBS
->SetTimer (MnpServiceData
->PollTimer
, TimerCancel
, 0);
871 MnpServiceData
->EnableSystemPoll
= FALSE
;
875 // Cancel the timeout timer.
877 Status
= gBS
->SetTimer (MnpServiceData
->TimeoutCheckTimer
, TimerCancel
, 0);
880 // Stop the simple network.
882 Status
= MnpStopSnp (MnpServiceData
->Snp
);
889 Flush the instance's received data.
891 @param Instance Pointer to the mnp instance context data.
897 MnpFlushRcvdDataQueue (
898 IN MNP_INSTANCE_DATA
*Instance
902 MNP_RXDATA_WRAP
*RxDataWrap
;
904 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
906 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
908 while (!IsListEmpty (&Instance
->RcvdPacketQueue
)) {
910 // Remove all the Wraps.
912 RxDataWrap
= NET_LIST_HEAD (&Instance
->RcvdPacketQueue
, MNP_RXDATA_WRAP
, WrapEntry
);
915 // Recycle the RxDataWrap.
917 MnpRecycleRxData (NULL
, (VOID
*) RxDataWrap
);
918 Instance
->RcvdPacketQueueSize
--;
921 ASSERT (Instance
->RcvdPacketQueueSize
== 0);
923 gBS
->RestoreTPL (OldTpl
);
928 Configure the Instance using ConfigData.
930 @param Instance Pointer to the mnp instance context data.
931 @param ConfigData Pointer to the configuration data used to configure
934 @retval EFI_SUCCESS The Instance is configured.
935 @retval EFI_UNSUPPORTED EnableReceiveTimestamps is on and the
936 implementation doesn't support it.
937 @retval Other Some error occurs.
941 MnpConfigureInstance (
942 IN MNP_INSTANCE_DATA
*Instance
,
943 IN EFI_MANAGED_NETWORK_CONFIG_DATA
*ConfigData OPTIONAL
947 MNP_SERVICE_DATA
*MnpServiceData
;
948 EFI_MANAGED_NETWORK_CONFIG_DATA
*OldConfigData
;
949 EFI_MANAGED_NETWORK_CONFIG_DATA
*NewConfigData
;
950 BOOLEAN IsConfigUpdate
;
952 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
954 if ((ConfigData
!= NULL
) && ConfigData
->EnableReceiveTimestamps
) {
956 // Don't support timestamp.
958 return EFI_UNSUPPORTED
;
961 Status
= EFI_SUCCESS
;
963 MnpServiceData
= Instance
->MnpServiceData
;
964 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
966 IsConfigUpdate
= (BOOLEAN
) ((Instance
->Configured
) && (ConfigData
!= NULL
));
968 OldConfigData
= &Instance
->ConfigData
;
969 NewConfigData
= ConfigData
;
970 if (NewConfigData
== NULL
) {
972 // Restore back the default config data if a reset of this instance
975 NewConfigData
= &mMnpDefaultConfigData
;
979 // Reset the instance's receive filter.
981 Instance
->ReceiveFilter
= 0;
984 // Clear the receive counters according to the old ConfigData.
986 if (OldConfigData
->EnableUnicastReceive
) {
987 MnpServiceData
->UnicastCount
--;
990 if (OldConfigData
->EnableMulticastReceive
) {
991 MnpServiceData
->MulticastCount
--;
994 if (OldConfigData
->EnableBroadcastReceive
) {
995 MnpServiceData
->BroadcastCount
--;
998 if (OldConfigData
->EnablePromiscuousReceive
) {
999 MnpServiceData
->PromiscuousCount
--;
1003 // Set the receive filter counters and the receive filter of the
1004 // instance according to the new ConfigData.
1006 if (NewConfigData
->EnableUnicastReceive
) {
1007 MnpServiceData
->UnicastCount
++;
1008 Instance
->ReceiveFilter
|= MNP_RECEIVE_UNICAST
;
1011 if (NewConfigData
->EnableMulticastReceive
) {
1012 MnpServiceData
->MulticastCount
++;
1015 if (NewConfigData
->EnableBroadcastReceive
) {
1016 MnpServiceData
->BroadcastCount
++;
1017 Instance
->ReceiveFilter
|= MNP_RECEIVE_BROADCAST
;
1020 if (NewConfigData
->EnablePromiscuousReceive
) {
1021 MnpServiceData
->PromiscuousCount
++;
1024 if (OldConfigData
->FlushQueuesOnReset
) {
1026 MnpFlushRcvdDataQueue (Instance
);
1029 if (ConfigData
== NULL
) {
1031 Instance
->ManagedNetwork
.Cancel (&Instance
->ManagedNetwork
, NULL
);
1034 if (!NewConfigData
->EnableMulticastReceive
) {
1036 MnpGroupOp (Instance
, FALSE
, NULL
, NULL
);
1040 // Save the new configuration data.
1042 CopyMem (OldConfigData
, NewConfigData
, sizeof (*OldConfigData
));
1044 Instance
->Configured
= (BOOLEAN
) (ConfigData
!= NULL
);
1046 if (Instance
->Configured
) {
1048 // The instance is configured, start the Mnp.
1053 (BOOLEAN
) !NewConfigData
->DisableBackgroundPolling
1057 // The instance is changed to the unconfigured state, stop the Mnp.
1059 Status
= MnpStop (MnpServiceData
);
1067 Configure the Snp receive filters according to the instances' receive filter
1070 @param MnpServiceData Pointer to the mnp service context data.
1072 @retval EFI_SUCCESS The receive filters is configured.
1073 @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
;
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
= AllocatePool (sizeof (EFI_MAC_ADDRESS
) * MCastFilterCnt
);
1142 if (MCastFilter
== NULL
) {
1144 DEBUG ((EFI_D_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
)) {
1207 "MnpConfigReceiveFilters: Snp->ReceiveFilters failed, %r.\n",
1213 if (MCastFilter
!= NULL
) {
1215 // Free the buffer used to hold the group addresses.
1217 gBS
->FreePool (MCastFilter
);
1225 Add a group address control block which controls the MacAddress for
1228 @param Instance Pointer to the mnp instance context data.
1229 @param CtrlBlk Pointer to the group address control block.
1230 @param GroupAddress Pointer to the group adress.
1231 @param MacAddress Pointer to the mac address.
1232 @param HwAddressSize The hardware address size.
1234 @retval EFI_SUCCESS The group address control block is added.
1235 @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
= AllocatePool (sizeof (MNP_GROUP_ADDRESS
));
1262 if (GroupAddress
== NULL
) {
1264 DEBUG ((EFI_D_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 InsertTailList (&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.
1304 MnpGroupOpDelCtrlBlk (
1305 IN MNP_INSTANCE_DATA
*Instance
,
1306 IN MNP_GROUP_CONTROL_BLOCK
*CtrlBlk
1309 MNP_SERVICE_DATA
*MnpServiceData
;
1310 MNP_GROUP_ADDRESS
*GroupAddress
;
1312 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
1314 MnpServiceData
= Instance
->MnpServiceData
;
1315 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
1318 // Remove and free the CtrlBlk.
1320 GroupAddress
= CtrlBlk
->GroupAddress
;
1321 RemoveEntryList (&CtrlBlk
->CtrlBlkEntry
);
1322 gBS
->FreePool (CtrlBlk
);
1324 ASSERT (GroupAddress
->RefCnt
> 0);
1327 // Count down the RefCnt.
1329 GroupAddress
->RefCnt
--;
1331 if (GroupAddress
->RefCnt
== 0) {
1333 // Free this GroupAddress entry if no instance uses it.
1335 MnpServiceData
->GroupAddressCount
--;
1336 RemoveEntryList (&GroupAddress
->AddrEntry
);
1337 gBS
->FreePool (GroupAddress
);
1347 Do the group operations for this instance.
1349 @param Instance Pointer to the instance context data.
1350 @param JoinFlag Set to TRUE to join a group. Set to TRUE to leave a
1352 @param MacAddress Pointer to the group address to join or leave.
1353 @param CtrlBlk Pointer to the group control block if JoinFlag if
1356 @retval EFI_SUCCESS The group operation finished.
1357 @retval Other Some error occurs.
1362 IN MNP_INSTANCE_DATA
*Instance
,
1363 IN BOOLEAN JoinFlag
,
1364 IN EFI_MAC_ADDRESS
*MacAddress OPTIONAL
,
1365 IN MNP_GROUP_CONTROL_BLOCK
*CtrlBlk OPTIONAL
1368 MNP_SERVICE_DATA
*MnpServiceData
;
1370 LIST_ENTRY
*NextEntry
;
1371 MNP_GROUP_ADDRESS
*GroupAddress
;
1372 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
1373 MNP_GROUP_CONTROL_BLOCK
*NewCtrlBlk
;
1375 BOOLEAN AddressExist
;
1378 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
1380 MnpServiceData
= Instance
->MnpServiceData
;
1381 SnpMode
= MnpServiceData
->Snp
->Mode
;
1385 // A new gropu address is to be added.
1388 GroupAddress
= NULL
;
1389 AddressExist
= FALSE
;
1392 // Allocate memory for the control block.
1394 NewCtrlBlk
= AllocatePool (sizeof (MNP_GROUP_CONTROL_BLOCK
));
1395 if (NewCtrlBlk
== NULL
) {
1397 DEBUG ((EFI_D_ERROR
, "MnpGroupOp: Failed to allocate memory resource.\n"));
1398 return EFI_OUT_OF_RESOURCES
;
1401 NET_LIST_FOR_EACH (Entry
, &MnpServiceData
->GroupAddressList
) {
1403 // Check whether the MacAddress is already joined by other instances.
1405 GroupAddress
= NET_LIST_USER_STRUCT (Entry
, MNP_GROUP_ADDRESS
, AddrEntry
);
1406 if (0 == CompareMem (
1408 &GroupAddress
->Address
,
1409 SnpMode
->HwAddressSize
1412 AddressExist
= TRUE
;
1417 if (!AddressExist
) {
1418 GroupAddress
= NULL
;
1422 // Add the GroupAddress for this instance.
1424 Status
= MnpGroupOpAddCtrlBlk (
1429 SnpMode
->HwAddressSize
1431 if (EFI_ERROR (Status
)) {
1439 if (MacAddress
!= NULL
) {
1441 ASSERT (CtrlBlk
!= NULL
);
1444 // Leave the specific multicast mac address.
1446 NeedUpdate
= MnpGroupOpDelCtrlBlk (Instance
, CtrlBlk
);
1449 // Leave all multicast mac addresses.
1453 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Instance
->GroupCtrlBlkList
) {
1455 NewCtrlBlk
= NET_LIST_USER_STRUCT (
1457 MNP_GROUP_CONTROL_BLOCK
,
1461 // Update is required if the group address left is no longer used
1462 // by other instances.
1464 NeedUpdate
= MnpGroupOpDelCtrlBlk (Instance
, NewCtrlBlk
);
1469 Status
= EFI_SUCCESS
;
1473 // Reconfigure the receive filters if necessary.
1475 Status
= MnpConfigReceiveFilters (MnpServiceData
);