1. update timeout interval time from 10ms to 50ms
[edk2.git] / MdeModulePkg / Universal / Network / MnpDxe / MnpConfig.c
blob838765de7403103218804aef70158dbbbeebe89f
1 /** @file
2 Implementation of Managed Network Protocol private services.
4 Copyright (c) 2005 - 2009, Intel Corporation. <BR>
5 All rights reserved. This program and the accompanying materials are licensed
6 and made available under the terms and conditions of the BSD License which
7 accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 **/
16 #include "MnpImpl.h"
18 EFI_SERVICE_BINDING_PROTOCOL mMnpServiceBindingProtocol = {
19 MnpServiceBindingCreateChild,
20 MnpServiceBindingDestroyChild
23 EFI_MANAGED_NETWORK_PROTOCOL mMnpProtocolTemplate = {
24 MnpGetModeData,
25 MnpConfigure,
26 MnpMcastIpToMac,
27 MnpGroups,
28 MnpTransmit,
29 MnpReceive,
30 MnpCancel,
31 MnpPoll
34 EFI_MANAGED_NETWORK_CONFIG_DATA mMnpDefaultConfigData = {
35 10000,
36 10000,
38 FALSE,
39 FALSE,
40 FALSE,
41 FALSE,
42 FALSE,
43 FALSE,
44 FALSE
48 /**
49 Add Count of net buffers to MnpServiceData->FreeNbufQue. The length of the net
50 buffer is specified by MnpServiceData->BufferLength.
52 @param[in, out] MnpServiceData Pointer to the MNP_SERVICE_DATA.
53 @param[in] Count Number of NET_BUFFERs to add.
55 @retval EFI_SUCCESS The specified amount of NET_BUFs are allocated
56 and added to MnpServiceData->FreeNbufQue.
57 @retval EFI_OUT_OF_RESOURCES Failed to allocate a NET_BUF structure.
59 **/
60 EFI_STATUS
61 MnpAddFreeNbuf (
62 IN OUT MNP_SERVICE_DATA *MnpServiceData,
63 IN UINTN Count
66 EFI_STATUS Status;
67 UINTN Index;
68 NET_BUF *Nbuf;
70 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
71 ASSERT ((Count > 0) && (MnpServiceData->BufferLength > 0));
73 Status = EFI_SUCCESS;
75 for (Index = 0; Index < Count; Index++) {
77 Nbuf = NetbufAlloc (MnpServiceData->BufferLength + MnpServiceData->PaddingSize);
78 if (Nbuf == NULL) {
80 DEBUG ((EFI_D_ERROR, "MnpAddFreeNbuf: NetBufAlloc failed.\n"));
81 Status = EFI_OUT_OF_RESOURCES;
82 break;
85 if (MnpServiceData->PaddingSize > 0) {
87 // Pad padding bytes before the media header
89 NetbufAllocSpace (Nbuf, MnpServiceData->PaddingSize, NET_BUF_TAIL);
90 NetbufTrim (Nbuf, MnpServiceData->PaddingSize, NET_BUF_HEAD);
93 NetbufQueAppend (&MnpServiceData->FreeNbufQue, Nbuf);
96 MnpServiceData->NbufCnt += Index;
98 return Status;
103 Allocate a free NET_BUF from MnpServiceData->FreeNbufQue. If there is none
104 in the queue, first try to allocate some and add them into the queue, then
105 fetch the NET_BUF from the updated FreeNbufQue.
107 @param[in, out] MnpServiceData Pointer to the MNP_SERVICE_DATA.
109 @return Pointer to the allocated free NET_BUF structure, if NULL the
110 operation is failed.
113 NET_BUF *
114 MnpAllocNbuf (
115 IN OUT MNP_SERVICE_DATA *MnpServiceData
118 EFI_STATUS Status;
119 NET_BUF_QUEUE *FreeNbufQue;
120 NET_BUF *Nbuf;
121 EFI_TPL OldTpl;
123 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
125 FreeNbufQue = &MnpServiceData->FreeNbufQue;
127 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
130 // Check whether there are available buffers, or else try to add some.
132 if (FreeNbufQue->BufNum == 0) {
134 if ((MnpServiceData->NbufCnt + MNP_NET_BUFFER_INCREASEMENT) > MNP_MAX_NET_BUFFER_NUM) {
136 DEBUG (
137 (EFI_D_ERROR,
138 "MnpAllocNbuf: The maximum NET_BUF size is reached for MNP driver instance %p.\n",
139 MnpServiceData)
142 Nbuf = NULL;
143 goto ON_EXIT;
146 Status = MnpAddFreeNbuf (MnpServiceData, MNP_NET_BUFFER_INCREASEMENT);
147 if (EFI_ERROR (Status)) {
149 DEBUG (
150 (EFI_D_ERROR,
151 "MnpAllocNbuf: Failed to add NET_BUFs into the FreeNbufQue, %r.\n",
152 Status)
155 // Don't return NULL, perhaps MnpAddFreeNbuf does add some NET_BUFs but
156 // the amount is less than MNP_NET_BUFFER_INCREASEMENT.
161 Nbuf = NetbufQueRemove (FreeNbufQue);
164 // Increase the RefCnt.
166 if (Nbuf != NULL) {
167 NET_GET_REF (Nbuf);
170 ON_EXIT:
171 gBS->RestoreTPL (OldTpl);
173 return Nbuf;
178 Try to reclaim the Nbuf into the buffer pool.
180 @param[in, out] MnpServiceData Pointer to the mnp service context data.
181 @param[in, out] Nbuf Pointer to the NET_BUF to free.
184 VOID
185 MnpFreeNbuf (
186 IN OUT MNP_SERVICE_DATA *MnpServiceData,
187 IN OUT NET_BUF *Nbuf
190 EFI_TPL OldTpl;
192 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
193 ASSERT (Nbuf->RefCnt > 1);
195 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
197 NET_PUT_REF (Nbuf);
199 if (Nbuf->RefCnt == 1) {
201 // Trim all buffer contained in the Nbuf, then append it to the NbufQue.
203 NetbufTrim (Nbuf, Nbuf->TotalSize, NET_BUF_TAIL);
204 NetbufQueAppend (&MnpServiceData->FreeNbufQue, Nbuf);
207 gBS->RestoreTPL (OldTpl);
212 Initialize the mnp service context data.
214 @param[in, out] MnpServiceData Pointer to the mnp service context data.
215 @param[in] ImageHandle The driver image handle.
216 @param[in] ControllerHandle Handle of device to bind driver to.
218 @retval EFI_SUCCESS The mnp service context is initialized.
219 @retval EFI_UNSUPPORTED ControllerHandle does not support Simple Network Protocol.
220 @retval Others Other errors as indicated.
223 EFI_STATUS
224 MnpInitializeServiceData (
225 IN OUT MNP_SERVICE_DATA *MnpServiceData,
226 IN EFI_HANDLE ImageHandle,
227 IN EFI_HANDLE ControllerHandle
230 EFI_STATUS Status;
231 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
232 EFI_SIMPLE_NETWORK_MODE *SnpMode;
234 MnpServiceData->Signature = MNP_SERVICE_DATA_SIGNATURE;
236 MnpServiceData->ControllerHandle = ControllerHandle;
239 // Copy the ServiceBinding structure.
241 MnpServiceData->ServiceBinding = mMnpServiceBindingProtocol;
244 // Open the Simple Network protocol.
246 Status = gBS->OpenProtocol (
247 ControllerHandle,
248 &gEfiSimpleNetworkProtocolGuid,
249 (VOID **) &Snp,
250 ImageHandle,
251 ControllerHandle,
252 EFI_OPEN_PROTOCOL_BY_DRIVER
254 if (EFI_ERROR (Status)) {
255 return EFI_UNSUPPORTED;
259 // Get MTU from Snp.
261 SnpMode = Snp->Mode;
262 MnpServiceData->Snp = Snp;
263 MnpServiceData->Mtu = SnpMode->MaxPacketSize;
266 // Initialize the lists.
268 InitializeListHead (&MnpServiceData->GroupAddressList);
269 InitializeListHead (&MnpServiceData->ChildrenList);
272 // Get the buffer length used to allocate NET_BUF to hold data received
273 // from SNP. Do this before fill the FreeNetBufQue.
275 MnpServiceData->BufferLength = MnpServiceData->Mtu + SnpMode->MediaHeaderSize + NET_ETHER_FCS_SIZE;
278 // Make sure the protocol headers immediately following the media header
279 // 4-byte aligned
281 MnpServiceData->PaddingSize = (4 - SnpMode->MediaHeaderSize) & 0x3;
284 // Initialize the FreeNetBufQue and pre-allocate some NET_BUFs.
286 NetbufQueInit (&MnpServiceData->FreeNbufQue);
287 Status = MnpAddFreeNbuf (MnpServiceData, MNP_INIT_NET_BUFFER_NUM);
288 if (EFI_ERROR (Status)) {
290 DEBUG ((EFI_D_ERROR, "MnpInitializeServiceData: MnpAddFreeNbuf failed, %r.\n", Status));
291 goto ERROR;
294 // Get one NET_BUF from the FreeNbufQue for rx cache.
296 MnpServiceData->RxNbufCache = MnpAllocNbuf (MnpServiceData);
297 NetbufAllocSpace (
298 MnpServiceData->RxNbufCache,
299 MnpServiceData->BufferLength,
300 NET_BUF_TAIL
304 // Allocate buffer pool for tx.
306 MnpServiceData->TxBuf = AllocatePool (MnpServiceData->Mtu + SnpMode->MediaHeaderSize);
307 if (MnpServiceData->TxBuf == NULL) {
309 DEBUG ((EFI_D_ERROR, "MnpInitializeServiceData: AllocatePool failed.\n"));
310 Status = EFI_OUT_OF_RESOURCES;
312 goto ERROR;
316 // Create the system poll timer.
318 Status = gBS->CreateEvent (
319 EVT_NOTIFY_SIGNAL | EVT_TIMER,
320 TPL_CALLBACK,
321 MnpSystemPoll,
322 MnpServiceData,
323 &MnpServiceData->PollTimer
325 if (EFI_ERROR (Status)) {
327 DEBUG ((EFI_D_ERROR, "MnpInitializeServiceData: CreateEvent for poll timer failed.\n"));
328 goto ERROR;
332 // Create the timer for packet timeout check.
334 Status = gBS->CreateEvent (
335 EVT_NOTIFY_SIGNAL | EVT_TIMER,
336 TPL_CALLBACK,
337 MnpCheckPacketTimeout,
338 MnpServiceData,
339 &MnpServiceData->TimeoutCheckTimer
341 if (EFI_ERROR (Status)) {
343 DEBUG ((EFI_D_ERROR, "MnpInitializeServiceData: CreateEvent for packet timeout check failed.\n"));
344 goto ERROR;
348 // Create the timer for tx timeout check.
350 Status = gBS->CreateEvent (
351 EVT_TIMER,
352 TPL_CALLBACK,
353 NULL,
354 NULL,
355 &MnpServiceData->TxTimeoutEvent
357 if (EFI_ERROR (Status)) {
359 DEBUG ((EFI_D_ERROR, "MnpInitializeServiceData: CreateEvent for tx timeout event failed.\n"));
362 ERROR:
364 if (EFI_ERROR (Status)) {
366 // Free the dynamic allocated resources if necessary.
368 if (MnpServiceData->TimeoutCheckTimer != NULL) {
370 gBS->CloseEvent (MnpServiceData->TimeoutCheckTimer);
373 if (MnpServiceData->PollTimer != NULL) {
375 gBS->CloseEvent (MnpServiceData->PollTimer);
378 if (MnpServiceData->TxBuf != NULL) {
380 gBS->FreePool (MnpServiceData->TxBuf);
383 if (MnpServiceData->RxNbufCache != NULL) {
385 MnpFreeNbuf (MnpServiceData, MnpServiceData->RxNbufCache);
388 if (MnpServiceData->FreeNbufQue.BufNum != 0) {
390 NetbufQueFlush (&MnpServiceData->FreeNbufQue);
394 return Status;
399 Flush the mnp service context data.
401 @param[in, out] MnpServiceData Pointer to the mnp service context data.
402 @param[in] ImageHandle The driver image handle.
405 VOID
406 MnpFlushServiceData (
407 IN OUT MNP_SERVICE_DATA *MnpServiceData,
408 IN EFI_HANDLE ImageHandle
411 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
414 // The GroupAddressList must be empty.
416 ASSERT (IsListEmpty (&MnpServiceData->GroupAddressList));
419 // Close the event.
421 gBS->CloseEvent (&MnpServiceData->TxTimeoutEvent);
422 gBS->CloseEvent (&MnpServiceData->TimeoutCheckTimer);
423 gBS->CloseEvent (&MnpServiceData->PollTimer);
426 // Free the tx buffer.
428 gBS->FreePool (MnpServiceData->TxBuf);
431 // Free the RxNbufCache.
433 MnpFreeNbuf (MnpServiceData, MnpServiceData->RxNbufCache);
436 // Flush the FreeNbufQue.
438 MnpServiceData->NbufCnt -= MnpServiceData->FreeNbufQue.BufNum;
439 NetbufQueFlush (&MnpServiceData->FreeNbufQue);
441 DEBUG_CODE (
443 if (MnpServiceData->NbufCnt != 0) {
445 DEBUG ((EFI_D_WARN, "MnpFlushServiceData: Memory leak, MnpServiceData->NbufCnt != 0.\n"));
450 // Close the Simple Network Protocol.
452 gBS->CloseProtocol (
453 MnpServiceData->ControllerHandle,
454 &gEfiSimpleNetworkProtocolGuid,
455 ImageHandle,
456 MnpServiceData->ControllerHandle
462 Initialize the mnp instance context data.
464 @param[in] MnpServiceData Pointer to the mnp service context data.
465 @param[in, out] Instance Pointer to the mnp instance context data
466 to initialize.
469 VOID
470 MnpInitializeInstanceData (
471 IN MNP_SERVICE_DATA *MnpServiceData,
472 IN OUT MNP_INSTANCE_DATA *Instance
475 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
476 ASSERT (Instance != NULL);
479 // Set the signature.
481 Instance->Signature = MNP_INSTANCE_DATA_SIGNATURE;
484 // Copy the MNP Protocol interfaces from the template.
486 CopyMem (&Instance->ManagedNetwork, &mMnpProtocolTemplate, sizeof (Instance->ManagedNetwork));
489 // Copy the default config data.
491 CopyMem (&Instance->ConfigData, &mMnpDefaultConfigData, sizeof (Instance->ConfigData));
494 // Initialize the lists.
496 InitializeListHead (&Instance->GroupCtrlBlkList);
497 InitializeListHead (&Instance->RcvdPacketQueue);
498 InitializeListHead (&Instance->RxDeliveredPacketQueue);
501 // Initialize the RxToken Map.
503 NetMapInit (&Instance->RxTokenMap);
506 // Save the MnpServiceData info.
508 Instance->MnpServiceData = MnpServiceData;
513 Check whether the token specified by Arg matches the token in Item.
515 @param[in] Map Pointer to the NET_MAP.
516 @param[in] Item Pointer to the NET_MAP_ITEM.
517 @param[in] Arg Pointer to the Arg, it's a pointer to the token to
518 check.
520 @retval EFI_SUCCESS The token specified by Arg is different from the
521 token in Item.
522 @retval EFI_ACCESS_DENIED The token specified by Arg is the same as that in
523 Item.
526 EFI_STATUS
527 MnpTokenExist (
528 IN NET_MAP *Map,
529 IN NET_MAP_ITEM *Item,
530 IN VOID *Arg
533 EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token;
534 EFI_MANAGED_NETWORK_COMPLETION_TOKEN *TokenInItem;
536 Token = (EFI_MANAGED_NETWORK_COMPLETION_TOKEN *) Arg;
537 TokenInItem = (EFI_MANAGED_NETWORK_COMPLETION_TOKEN *) Item->Key;
539 if ((Token == TokenInItem) || (Token->Event == TokenInItem->Event)) {
541 // The token is the same either the two tokens equals or the Events in
542 // the two tokens are the same.
544 return EFI_ACCESS_DENIED;
547 return EFI_SUCCESS;
551 Cancel the token specified by Arg if it matches the token in Item.
553 @param[in, out] Map Pointer to the NET_MAP.
554 @param[in, out] Item Pointer to the NET_MAP_ITEM.
555 @param[in] Arg Pointer to the Arg, it's a pointer to the
556 token to cancel.
558 @retval EFI_SUCCESS The Arg is NULL, and the token in Item is cancelled,
559 or the Arg isn't NULL, and the token in Item is
560 different from the Arg.
561 @retval EFI_ABORTED The Arg isn't NULL, the token in Item mathces the
562 Arg, and the token is cancelled.
565 EFI_STATUS
566 MnpCancelTokens (
567 IN OUT NET_MAP *Map,
568 IN OUT NET_MAP_ITEM *Item,
569 IN VOID *Arg
572 EFI_MANAGED_NETWORK_COMPLETION_TOKEN *TokenToCancel;
574 if ((Arg != NULL) && (Item->Key != Arg)) {
576 // The token in Item is not the token specified by Arg.
578 return EFI_SUCCESS;
581 TokenToCancel = (EFI_MANAGED_NETWORK_COMPLETION_TOKEN *) Item->Key;
584 // Remove the item from the map.
586 NetMapRemoveItem (Map, Item, NULL);
589 // Cancel this token with status set to EFI_ABORTED.
591 TokenToCancel->Status = EFI_ABORTED;
592 gBS->SignalEvent (TokenToCancel->Event);
594 if (Arg != NULL) {
596 // Only abort the token specified by Arg if Arg isn't NULL.
598 return EFI_ABORTED;
601 return EFI_SUCCESS;
606 Start and initialize the simple network.
608 @param[in] Snp Pointer to the simple network protocol.
610 @retval EFI_SUCCESS The simple network protocol is started.
611 @retval Others Other errors as indicated.
614 EFI_STATUS
615 MnpStartSnp (
616 IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp
619 EFI_STATUS Status;
621 ASSERT (Snp != NULL);
624 // Start the simple network.
626 Status = Snp->Start (Snp);
628 if (!EFI_ERROR (Status)) {
630 // Initialize the simple network.
632 Status = Snp->Initialize (Snp, 0, 0);
635 return Status;
640 Stop the simple network.
642 @param[in] Snp Pointer to the simple network protocol.
644 @retval EFI_SUCCESS The simple network is stopped.
645 @retval Others Other errors as indicated.
648 EFI_STATUS
649 MnpStopSnp (
650 IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp
653 EFI_STATUS Status;
655 ASSERT (Snp != NULL);
658 // Shut down the simple network.
660 Status = Snp->Shutdown (Snp);
662 if (!EFI_ERROR (Status)) {
664 // Stop the simple network.
666 Status = Snp->Stop (Snp);
669 return Status;
674 Start the managed network, this function is called when one instance is configured
675 or reconfigured.
677 @param[in, out] MnpServiceData Pointer to the mnp service context data.
678 @param[in] IsConfigUpdate The instance is reconfigured or it's the first
679 time the instanced is configured.
680 @param[in] EnableSystemPoll Enable the system polling or not.
682 @retval EFI_SUCCESS The managed network is started and some
683 configuration is updated.
684 @retval Others Other errors as indicated.
687 EFI_STATUS
688 MnpStart (
689 IN OUT MNP_SERVICE_DATA *MnpServiceData,
690 IN BOOLEAN IsConfigUpdate,
691 IN BOOLEAN EnableSystemPoll
694 EFI_STATUS Status;
695 EFI_TIMER_DELAY TimerOpType;
697 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
699 Status = EFI_SUCCESS;
701 if (!IsConfigUpdate) {
703 // If it's not a configuration update, increase the configured children number.
705 MnpServiceData->ConfiguredChildrenNumber++;
707 if (MnpServiceData->ConfiguredChildrenNumber == 1) {
709 // It's the first configured child, start the simple network.
711 Status = MnpStartSnp (MnpServiceData->Snp);
712 if (EFI_ERROR (Status)) {
714 DEBUG ((EFI_D_ERROR, "MnpStart: MnpStartSnp failed, %r.\n", Status));
715 goto ErrorExit;
719 // Start the timeout timer.
721 Status = gBS->SetTimer (
722 MnpServiceData->TimeoutCheckTimer,
723 TimerPeriodic,
724 MNP_TIMEOUT_CHECK_INTERVAL
726 if (EFI_ERROR (Status)) {
728 DEBUG (
729 (EFI_D_ERROR,
730 "MnpStart, gBS->SetTimer for TimeoutCheckTimer %r.\n",
731 Status)
733 goto ErrorExit;
738 if (MnpServiceData->EnableSystemPoll ^ EnableSystemPoll) {
740 // The EnableSystemPoll differs with the current state, disable or enable
741 // the system poll.
743 TimerOpType = EnableSystemPoll ? TimerPeriodic : TimerCancel;
745 Status = gBS->SetTimer (MnpServiceData->PollTimer, TimerOpType, MNP_SYS_POLL_INTERVAL);
746 if (EFI_ERROR (Status)) {
748 DEBUG ((EFI_D_ERROR, "MnpStart: gBS->SetTimer for PollTimer failed, %r.\n", Status));
749 goto ErrorExit;
752 MnpServiceData->EnableSystemPoll = EnableSystemPoll;
756 // Change the receive filters if need.
758 Status = MnpConfigReceiveFilters (MnpServiceData);
760 ErrorExit:
762 return Status;
767 Stop the managed network.
769 @param[in, out] MnpServiceData Pointer to the mnp service context data.
771 @retval EFI_SUCCESS The managed network is stopped.
772 @retval Others Other errors as indicated.
775 EFI_STATUS
776 MnpStop (
777 IN OUT MNP_SERVICE_DATA *MnpServiceData
780 EFI_STATUS Status;
782 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
783 ASSERT (MnpServiceData->ConfiguredChildrenNumber > 0);
786 // Configure the receive filters.
788 MnpConfigReceiveFilters (MnpServiceData);
791 // Decrease the children number.
793 MnpServiceData->ConfiguredChildrenNumber--;
795 if (MnpServiceData->ConfiguredChildrenNumber > 0) {
797 // If there are other configured chilren, return and keep the timers and
798 // simple network unchanged.
800 return EFI_SUCCESS;
804 // No configured children now.
807 if (MnpServiceData->EnableSystemPoll) {
809 // The system poll in on, cancel the poll timer.
811 Status = gBS->SetTimer (MnpServiceData->PollTimer, TimerCancel, 0);
812 MnpServiceData->EnableSystemPoll = FALSE;
816 // Cancel the timeout timer.
818 Status = gBS->SetTimer (MnpServiceData->TimeoutCheckTimer, TimerCancel, 0);
821 // Stop the simple network.
823 Status = MnpStopSnp (MnpServiceData->Snp);
825 return Status;
830 Flush the instance's received data.
832 @param[in, out] Instance Pointer to the mnp instance context data.
835 VOID
836 MnpFlushRcvdDataQueue (
837 IN OUT MNP_INSTANCE_DATA *Instance
840 EFI_TPL OldTpl;
841 MNP_RXDATA_WRAP *RxDataWrap;
843 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
845 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
847 while (!IsListEmpty (&Instance->RcvdPacketQueue)) {
849 // Remove all the Wraps.
851 RxDataWrap = NET_LIST_HEAD (&Instance->RcvdPacketQueue, MNP_RXDATA_WRAP, WrapEntry);
854 // Recycle the RxDataWrap.
856 MnpRecycleRxData (NULL, (VOID *) RxDataWrap);
857 Instance->RcvdPacketQueueSize--;
860 ASSERT (Instance->RcvdPacketQueueSize == 0);
862 gBS->RestoreTPL (OldTpl);
867 Configure the Instance using ConfigData.
869 @param[in, out] Instance Pointer to the mnp instance context data.
870 @param[in] ConfigData Pointer to the configuration data used to configure
871 the isntance.
873 @retval EFI_SUCCESS The Instance is configured.
874 @retval EFI_UNSUPPORTED EnableReceiveTimestamps is on and the
875 implementation doesn't support it.
876 @retval Others Other errors as indicated.
879 EFI_STATUS
880 MnpConfigureInstance (
881 IN OUT MNP_INSTANCE_DATA *Instance,
882 IN EFI_MANAGED_NETWORK_CONFIG_DATA *ConfigData OPTIONAL
885 EFI_STATUS Status;
886 MNP_SERVICE_DATA *MnpServiceData;
887 EFI_MANAGED_NETWORK_CONFIG_DATA *OldConfigData;
888 EFI_MANAGED_NETWORK_CONFIG_DATA *NewConfigData;
889 BOOLEAN IsConfigUpdate;
891 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
893 if ((ConfigData != NULL) && ConfigData->EnableReceiveTimestamps) {
895 // Don't support timestamp.
897 return EFI_UNSUPPORTED;
900 Status = EFI_SUCCESS;
902 MnpServiceData = Instance->MnpServiceData;
903 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
905 IsConfigUpdate = (BOOLEAN) ((Instance->Configured) && (ConfigData != NULL));
907 OldConfigData = &Instance->ConfigData;
908 NewConfigData = ConfigData;
909 if (NewConfigData == NULL) {
911 // Restore back the default config data if a reset of this instance
912 // is required.
914 NewConfigData = &mMnpDefaultConfigData;
918 // Reset the instance's receive filter.
920 Instance->ReceiveFilter = 0;
923 // Clear the receive counters according to the old ConfigData.
925 if (OldConfigData->EnableUnicastReceive) {
926 MnpServiceData->UnicastCount--;
929 if (OldConfigData->EnableMulticastReceive) {
930 MnpServiceData->MulticastCount--;
933 if (OldConfigData->EnableBroadcastReceive) {
934 MnpServiceData->BroadcastCount--;
937 if (OldConfigData->EnablePromiscuousReceive) {
938 MnpServiceData->PromiscuousCount--;
942 // Set the receive filter counters and the receive filter of the
943 // instance according to the new ConfigData.
945 if (NewConfigData->EnableUnicastReceive) {
946 MnpServiceData->UnicastCount++;
947 Instance->ReceiveFilter |= MNP_RECEIVE_UNICAST;
950 if (NewConfigData->EnableMulticastReceive) {
951 MnpServiceData->MulticastCount++;
954 if (NewConfigData->EnableBroadcastReceive) {
955 MnpServiceData->BroadcastCount++;
956 Instance->ReceiveFilter |= MNP_RECEIVE_BROADCAST;
959 if (NewConfigData->EnablePromiscuousReceive) {
960 MnpServiceData->PromiscuousCount++;
963 if (OldConfigData->FlushQueuesOnReset) {
965 MnpFlushRcvdDataQueue (Instance);
968 if (ConfigData == NULL) {
970 Instance->ManagedNetwork.Cancel (&Instance->ManagedNetwork, NULL);
973 if (!NewConfigData->EnableMulticastReceive) {
975 MnpGroupOp (Instance, FALSE, NULL, NULL);
979 // Save the new configuration data.
981 CopyMem (OldConfigData, NewConfigData, sizeof (*OldConfigData));
983 Instance->Configured = (BOOLEAN) (ConfigData != NULL);
985 if (Instance->Configured) {
987 // The instance is configured, start the Mnp.
989 Status = MnpStart (
990 MnpServiceData,
991 IsConfigUpdate,
992 (BOOLEAN) !NewConfigData->DisableBackgroundPolling
994 } else {
996 // The instance is changed to the unconfigured state, stop the Mnp.
998 Status = MnpStop (MnpServiceData);
1001 return Status;
1005 Configure the Snp receive filters according to the instances' receive filter
1006 settings.
1008 @param[in] MnpServiceData Pointer to the mnp service context data.
1010 @retval EFI_SUCCESS The receive filters is configured.
1011 @retval EFI_OUT_OF_RESOURCES The receive filters can't be configured due
1012 to lack of memory resource.
1015 EFI_STATUS
1016 MnpConfigReceiveFilters (
1017 IN MNP_SERVICE_DATA *MnpServiceData
1020 EFI_STATUS Status;
1021 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
1022 EFI_MAC_ADDRESS *MCastFilter;
1023 UINT32 MCastFilterCnt;
1024 UINT32 EnableFilterBits;
1025 UINT32 DisableFilterBits;
1026 BOOLEAN ResetMCastFilters;
1027 LIST_ENTRY *Entry;
1028 UINT32 Index;
1029 MNP_GROUP_ADDRESS *GroupAddress;
1031 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
1033 Snp = MnpServiceData->Snp;
1036 // Initialize the enable filter and disable filter.
1038 EnableFilterBits = 0;
1039 DisableFilterBits = Snp->Mode->ReceiveFilterMask;
1041 if (MnpServiceData->UnicastCount != 0) {
1043 // Enable unicast if any instance wants to receive unicast.
1045 EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_UNICAST;
1048 if (MnpServiceData->BroadcastCount != 0) {
1050 // Enable broadcast if any instance wants to receive broadcast.
1052 EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
1055 MCastFilter = NULL;
1056 MCastFilterCnt = 0;
1057 ResetMCastFilters = TRUE;
1059 if ((MnpServiceData->MulticastCount != 0) && (MnpServiceData->GroupAddressCount != 0)) {
1061 // There are instances configured to receive multicast and already some group
1062 // addresses are joined.
1065 ResetMCastFilters = FALSE;
1067 if (MnpServiceData->GroupAddressCount <= Snp->Mode->MaxMCastFilterCount) {
1069 // The joind group address is less than simple network's maximum count.
1070 // Just configure the snp to do the multicast filtering.
1073 EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST;
1076 // Allocate pool for the mulicast addresses.
1078 MCastFilterCnt = MnpServiceData->GroupAddressCount;
1079 MCastFilter = AllocatePool (sizeof (EFI_MAC_ADDRESS) * MCastFilterCnt);
1080 if (MCastFilter == NULL) {
1082 DEBUG ((EFI_D_ERROR, "MnpConfigReceiveFilters: Failed to allocate memory resource for MCastFilter.\n"));
1083 return EFI_OUT_OF_RESOURCES;
1087 // Fill the multicast HW address buffer.
1089 Index = 0;
1090 NET_LIST_FOR_EACH (Entry, &MnpServiceData->GroupAddressList) {
1092 GroupAddress = NET_LIST_USER_STRUCT (Entry, MNP_GROUP_ADDRESS, AddrEntry);
1093 CopyMem (MCastFilter + Index, &GroupAddress->Address, sizeof (*(MCastFilter + Index)));
1094 Index++;
1096 ASSERT (Index <= MCastFilterCnt);
1098 } else {
1100 // The maximum multicast is reached, set the filter to be promiscuous
1101 // multicast.
1104 if ((Snp->Mode->ReceiveFilterMask & EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST) != 0) {
1105 EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST;
1106 } else {
1108 // Either MULTICAST or PROMISCUOUS_MULTICAST is not supported by Snp,
1109 // set the NIC to be promiscuous although this will tremendously degrade
1110 // the performance.
1112 EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS;
1117 if (MnpServiceData->PromiscuousCount != 0) {
1119 // Enable promiscuous if any instance wants to receive promiscuous.
1121 EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS;
1125 // Set the disable filter.
1127 DisableFilterBits ^= EnableFilterBits;
1130 // Configure the receive filters of SNP.
1132 Status = Snp->ReceiveFilters (
1133 Snp,
1134 EnableFilterBits,
1135 DisableFilterBits,
1136 ResetMCastFilters,
1137 MCastFilterCnt,
1138 MCastFilter
1140 DEBUG_CODE (
1141 if (EFI_ERROR (Status)) {
1143 DEBUG (
1144 (EFI_D_ERROR,
1145 "MnpConfigReceiveFilters: Snp->ReceiveFilters failed, %r.\n",
1146 Status)
1151 if (MCastFilter != NULL) {
1153 // Free the buffer used to hold the group addresses.
1155 gBS->FreePool (MCastFilter);
1158 return Status;
1163 Add a group address control block which controls the MacAddress for
1164 this instance.
1166 @param[in, out] Instance Pointer to the mnp instance context data.
1167 @param[in, out] CtrlBlk Pointer to the group address control block.
1168 @param[in, out] GroupAddress Pointer to the group adress.
1169 @param[in] MacAddress Pointer to the mac address.
1170 @param[in] HwAddressSize The hardware address size.
1172 @retval EFI_SUCCESS The group address control block is added.
1173 @retval EFI_OUT_OF_RESOURCES Failed due to lack of memory resources.
1176 EFI_STATUS
1177 MnpGroupOpAddCtrlBlk (
1178 IN OUT MNP_INSTANCE_DATA *Instance,
1179 IN OUT MNP_GROUP_CONTROL_BLOCK *CtrlBlk,
1180 IN OUT MNP_GROUP_ADDRESS *GroupAddress OPTIONAL,
1181 IN EFI_MAC_ADDRESS *MacAddress,
1182 IN UINT32 HwAddressSize
1185 MNP_SERVICE_DATA *MnpServiceData;
1187 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
1189 MnpServiceData = Instance->MnpServiceData;
1190 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
1192 if (GroupAddress == NULL) {
1194 ASSERT (MacAddress != NULL);
1197 // Allocate a new GroupAddress to be added into MNP's GroupAddressList.
1199 GroupAddress = AllocatePool (sizeof (MNP_GROUP_ADDRESS));
1200 if (GroupAddress == NULL) {
1202 DEBUG ((EFI_D_ERROR, "MnpGroupOpFormCtrlBlk: Failed to allocate memory resource.\n"));
1204 return EFI_OUT_OF_RESOURCES;
1207 CopyMem (&GroupAddress->Address, MacAddress, sizeof (GroupAddress->Address));
1208 GroupAddress->RefCnt = 0;
1209 InsertTailList (
1210 &MnpServiceData->GroupAddressList,
1211 &GroupAddress->AddrEntry
1213 MnpServiceData->GroupAddressCount++;
1217 // Increase the RefCnt.
1219 GroupAddress->RefCnt++;
1222 // Add the CtrlBlk into the instance's GroupCtrlBlkList.
1224 CtrlBlk->GroupAddress = GroupAddress;
1225 InsertTailList (&Instance->GroupCtrlBlkList, &CtrlBlk->CtrlBlkEntry);
1227 return EFI_SUCCESS;
1232 Delete a group control block from the instance. If the controlled group address's
1233 reference count reaches zero, the group address is removed too.
1235 @param[in] Instance Pointer to the instance context data.
1236 @param[in] CtrlBlk Pointer to the group control block to delete.
1238 @return The group address controlled by the control block is no longer used or not.
1241 BOOLEAN
1242 MnpGroupOpDelCtrlBlk (
1243 IN MNP_INSTANCE_DATA *Instance,
1244 IN MNP_GROUP_CONTROL_BLOCK *CtrlBlk
1247 MNP_SERVICE_DATA *MnpServiceData;
1248 MNP_GROUP_ADDRESS *GroupAddress;
1250 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
1252 MnpServiceData = Instance->MnpServiceData;
1253 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
1256 // Remove and free the CtrlBlk.
1258 GroupAddress = CtrlBlk->GroupAddress;
1259 RemoveEntryList (&CtrlBlk->CtrlBlkEntry);
1260 gBS->FreePool (CtrlBlk);
1262 ASSERT (GroupAddress->RefCnt > 0);
1265 // Count down the RefCnt.
1267 GroupAddress->RefCnt--;
1269 if (GroupAddress->RefCnt == 0) {
1271 // Free this GroupAddress entry if no instance uses it.
1273 MnpServiceData->GroupAddressCount--;
1274 RemoveEntryList (&GroupAddress->AddrEntry);
1275 gBS->FreePool (GroupAddress);
1277 return TRUE;
1280 return FALSE;
1285 Do the group operations for this instance.
1287 @param[in, out] Instance Pointer to the instance context data.
1288 @param[in] JoinFlag Set to TRUE to join a group. Set to TRUE to
1289 leave a group/groups.
1290 @param[in] MacAddress Pointer to the group address to join or leave.
1291 @param[in] CtrlBlk Pointer to the group control block if JoinFlag
1292 is FALSE.
1294 @retval EFI_SUCCESS The group operation finished.
1295 @retval EFI_OUT_OF_RESOURCES Failed due to lack of memory resources.
1296 @retval Others Other errors as indicated.
1299 EFI_STATUS
1300 MnpGroupOp (
1301 IN OUT MNP_INSTANCE_DATA *Instance,
1302 IN BOOLEAN JoinFlag,
1303 IN EFI_MAC_ADDRESS *MacAddress OPTIONAL,
1304 IN MNP_GROUP_CONTROL_BLOCK *CtrlBlk OPTIONAL
1307 MNP_SERVICE_DATA *MnpServiceData;
1308 LIST_ENTRY *Entry;
1309 LIST_ENTRY *NextEntry;
1310 MNP_GROUP_ADDRESS *GroupAddress;
1311 EFI_SIMPLE_NETWORK_MODE *SnpMode;
1312 MNP_GROUP_CONTROL_BLOCK *NewCtrlBlk;
1313 EFI_STATUS Status;
1314 BOOLEAN AddressExist;
1315 BOOLEAN NeedUpdate;
1317 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
1319 MnpServiceData = Instance->MnpServiceData;
1320 SnpMode = MnpServiceData->Snp->Mode;
1322 if (JoinFlag) {
1324 // A new gropu address is to be added.
1327 GroupAddress = NULL;
1328 AddressExist = FALSE;
1331 // Allocate memory for the control block.
1333 NewCtrlBlk = AllocatePool (sizeof (MNP_GROUP_CONTROL_BLOCK));
1334 if (NewCtrlBlk == NULL) {
1336 DEBUG ((EFI_D_ERROR, "MnpGroupOp: Failed to allocate memory resource.\n"));
1337 return EFI_OUT_OF_RESOURCES;
1340 NET_LIST_FOR_EACH (Entry, &MnpServiceData->GroupAddressList) {
1342 // Check whether the MacAddress is already joined by other instances.
1344 GroupAddress = NET_LIST_USER_STRUCT (Entry, MNP_GROUP_ADDRESS, AddrEntry);
1345 if (0 == CompareMem (
1346 MacAddress,
1347 &GroupAddress->Address,
1348 SnpMode->HwAddressSize
1349 )) {
1351 AddressExist = TRUE;
1352 break;
1356 if (!AddressExist) {
1357 GroupAddress = NULL;
1361 // Add the GroupAddress for this instance.
1363 Status = MnpGroupOpAddCtrlBlk (
1364 Instance,
1365 NewCtrlBlk,
1366 GroupAddress,
1367 MacAddress,
1368 SnpMode->HwAddressSize
1370 if (EFI_ERROR (Status)) {
1372 return Status;
1375 NeedUpdate = TRUE;
1376 } else {
1378 if (MacAddress != NULL) {
1380 ASSERT (CtrlBlk != NULL);
1383 // Leave the specific multicast mac address.
1385 NeedUpdate = MnpGroupOpDelCtrlBlk (Instance, CtrlBlk);
1386 } else {
1388 // Leave all multicast mac addresses.
1390 NeedUpdate = FALSE;
1392 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Instance->GroupCtrlBlkList) {
1394 NewCtrlBlk = NET_LIST_USER_STRUCT (
1395 Entry,
1396 MNP_GROUP_CONTROL_BLOCK,
1397 CtrlBlkEntry
1400 // Update is required if the group address left is no longer used
1401 // by other instances.
1403 NeedUpdate = MnpGroupOpDelCtrlBlk (Instance, NewCtrlBlk);
1408 Status = EFI_SUCCESS;
1410 if (NeedUpdate) {
1412 // Reconfigure the receive filters if necessary.
1414 Status = MnpConfigReceiveFilters (MnpServiceData);
1417 return Status;