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
6 are licensed and made available under the terms and conditions
7 of the BSD License which accompanies this distribution. The full
8 text of the license may be found at<BR>
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 EFI_SERVICE_BINDING_PROTOCOL mMnpServiceBindingProtocol
= {
20 MnpServiceBindingCreateChild
,
21 MnpServiceBindingDestroyChild
24 EFI_MANAGED_NETWORK_PROTOCOL mMnpProtocolTemplate
= {
35 EFI_MANAGED_NETWORK_CONFIG_DATA mMnpDefaultConfigData
= {
50 Add Count of net buffers to MnpDeviceData->FreeNbufQue. The length of the net
51 buffer is specified by MnpDeviceData->BufferLength.
53 @param[in, out] MnpDeviceData Pointer to the MNP_DEVICE_DATA.
54 @param[in] Count Number of NET_BUFFERs to add.
56 @retval EFI_SUCCESS The specified amount of NET_BUFs are allocated
57 and added to MnpDeviceData->FreeNbufQue.
58 @retval EFI_OUT_OF_RESOURCES Failed to allocate a NET_BUF structure.
63 IN OUT MNP_DEVICE_DATA
*MnpDeviceData
,
71 NET_CHECK_SIGNATURE (MnpDeviceData
, MNP_DEVICE_DATA_SIGNATURE
);
72 ASSERT ((Count
> 0) && (MnpDeviceData
->BufferLength
> 0));
75 for (Index
= 0; Index
< Count
; Index
++) {
76 Nbuf
= NetbufAlloc (MnpDeviceData
->BufferLength
+ MnpDeviceData
->PaddingSize
);
78 DEBUG ((EFI_D_ERROR
, "MnpAddFreeNbuf: NetBufAlloc failed.\n"));
80 Status
= EFI_OUT_OF_RESOURCES
;
84 if (MnpDeviceData
->PaddingSize
> 0) {
86 // Pad padding bytes before the media header
88 NetbufAllocSpace (Nbuf
, MnpDeviceData
->PaddingSize
, NET_BUF_TAIL
);
89 NetbufTrim (Nbuf
, MnpDeviceData
->PaddingSize
, NET_BUF_HEAD
);
92 NetbufQueAppend (&MnpDeviceData
->FreeNbufQue
, Nbuf
);
95 MnpDeviceData
->NbufCnt
+= Index
;
101 Allocate a free NET_BUF from MnpDeviceData->FreeNbufQue. If there is none
102 in the queue, first try to allocate some and add them into the queue, then
103 fetch the NET_BUF from the updated FreeNbufQue.
105 @param[in, out] MnpDeviceData Pointer to the MNP_DEVICE_DATA.
107 @return Pointer to the allocated free NET_BUF structure, if NULL the
113 IN OUT MNP_DEVICE_DATA
*MnpDeviceData
117 NET_BUF_QUEUE
*FreeNbufQue
;
121 NET_CHECK_SIGNATURE (MnpDeviceData
, MNP_DEVICE_DATA_SIGNATURE
);
123 FreeNbufQue
= &MnpDeviceData
->FreeNbufQue
;
124 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
127 // Check whether there are available buffers, or else try to add some.
129 if (FreeNbufQue
->BufNum
== 0) {
130 if ((MnpDeviceData
->NbufCnt
+ MNP_NET_BUFFER_INCREASEMENT
) > MNP_MAX_NET_BUFFER_NUM
) {
133 "MnpAllocNbuf: The maximum NET_BUF size is reached for MNP driver instance %p.\n",
141 Status
= MnpAddFreeNbuf (MnpDeviceData
, MNP_NET_BUFFER_INCREASEMENT
);
142 if (EFI_ERROR (Status
)) {
145 "MnpAllocNbuf: Failed to add NET_BUFs into the FreeNbufQue, %r.\n",
150 // Don't return NULL, perhaps MnpAddFreeNbuf does add some NET_BUFs but
151 // the amount is less than MNP_NET_BUFFER_INCREASEMENT.
156 Nbuf
= NetbufQueRemove (FreeNbufQue
);
159 // Increase the RefCnt.
166 gBS
->RestoreTPL (OldTpl
);
173 Try to reclaim the Nbuf into the buffer pool.
175 @param[in, out] MnpDeviceData Pointer to the mnp device context data.
176 @param[in, out] Nbuf Pointer to the NET_BUF to free.
181 IN OUT MNP_DEVICE_DATA
*MnpDeviceData
,
187 NET_CHECK_SIGNATURE (MnpDeviceData
, MNP_DEVICE_DATA_SIGNATURE
);
188 ASSERT (Nbuf
->RefCnt
> 1);
190 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
194 if (Nbuf
->RefCnt
== 1) {
196 // Trim all buffer contained in the Nbuf, then append it to the NbufQue.
198 NetbufTrim (Nbuf
, Nbuf
->TotalSize
, NET_BUF_TAIL
);
200 if (NetbufAllocSpace (Nbuf
, NET_VLAN_TAG_LEN
, NET_BUF_HEAD
) != NULL
) {
202 // There is space reserved for vlan tag in the head, reclaim it
204 NetbufTrim (Nbuf
, NET_VLAN_TAG_LEN
, NET_BUF_TAIL
);
207 NetbufQueAppend (&MnpDeviceData
->FreeNbufQue
, Nbuf
);
210 gBS
->RestoreTPL (OldTpl
);
215 Initialize the mnp device context data.
217 @param[in, out] MnpDeviceData Pointer to the mnp device context data.
218 @param[in] ImageHandle The driver image handle.
219 @param[in] ControllerHandle Handle of device to bind driver to.
221 @retval EFI_SUCCESS The mnp service context is initialized.
222 @retval EFI_UNSUPPORTED ControllerHandle does not support Simple Network Protocol.
223 @retval Others Other errors as indicated.
227 MnpInitializeDeviceData (
228 IN OUT MNP_DEVICE_DATA
*MnpDeviceData
,
229 IN EFI_HANDLE ImageHandle
,
230 IN EFI_HANDLE ControllerHandle
234 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
235 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
237 MnpDeviceData
->Signature
= MNP_DEVICE_DATA_SIGNATURE
;
238 MnpDeviceData
->ImageHandle
= ImageHandle
;
239 MnpDeviceData
->ControllerHandle
= ControllerHandle
;
242 // Copy the MNP Protocol interfaces from the template.
244 CopyMem (&MnpDeviceData
->VlanConfig
, &mVlanConfigProtocolTemplate
, sizeof (EFI_VLAN_CONFIG_PROTOCOL
));
247 // Open the Simple Network protocol.
249 Status
= gBS
->OpenProtocol (
251 &gEfiSimpleNetworkProtocolGuid
,
255 EFI_OPEN_PROTOCOL_BY_DRIVER
257 if (EFI_ERROR (Status
)) {
258 return EFI_UNSUPPORTED
;
265 MnpDeviceData
->Snp
= Snp
;
268 // Initialize the lists.
270 InitializeListHead (&MnpDeviceData
->ServiceList
);
271 InitializeListHead (&MnpDeviceData
->GroupAddressList
);
274 // Get the buffer length used to allocate NET_BUF to hold data received
275 // from SNP. Do this before fill the FreeNetBufQue.
278 MnpDeviceData
->BufferLength
= SnpMode
->MediaHeaderSize
+ NET_VLAN_TAG_LEN
+ SnpMode
->MaxPacketSize
+ NET_ETHER_FCS_SIZE
;
281 // Make sure the protocol headers immediately following the media header
282 // 4-byte aligned, and also preserve additional space for VLAN tag
284 MnpDeviceData
->PaddingSize
= ((4 - SnpMode
->MediaHeaderSize
) & 0x3) + NET_VLAN_TAG_LEN
;
287 // Initialize MAC string which will be used as VLAN configuration variable name
289 Status
= NetLibGetMacString (ControllerHandle
, ImageHandle
, &MnpDeviceData
->MacString
);
290 if (EFI_ERROR (Status
)) {
295 // Initialize the FreeNetBufQue and pre-allocate some NET_BUFs.
297 NetbufQueInit (&MnpDeviceData
->FreeNbufQue
);
298 Status
= MnpAddFreeNbuf (MnpDeviceData
, MNP_INIT_NET_BUFFER_NUM
);
299 if (EFI_ERROR (Status
)) {
300 DEBUG ((EFI_D_ERROR
, "MnpInitializeDeviceData: MnpAddFreeNbuf failed, %r.\n", Status
));
306 // Get one NET_BUF from the FreeNbufQue for rx cache.
308 MnpDeviceData
->RxNbufCache
= MnpAllocNbuf (MnpDeviceData
);
310 MnpDeviceData
->RxNbufCache
,
311 MnpDeviceData
->BufferLength
,
316 // Allocate buffer pool for tx.
318 MnpDeviceData
->TxBuf
= AllocatePool (MnpDeviceData
->BufferLength
);
319 if (MnpDeviceData
->TxBuf
== NULL
) {
320 DEBUG ((EFI_D_ERROR
, "MnpInitializeDeviceData: AllocatePool failed.\n"));
322 Status
= EFI_OUT_OF_RESOURCES
;
327 // Create the system poll timer.
329 Status
= gBS
->CreateEvent (
330 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
334 &MnpDeviceData
->PollTimer
336 if (EFI_ERROR (Status
)) {
337 DEBUG ((EFI_D_ERROR
, "MnpInitializeDeviceData: CreateEvent for poll timer failed.\n"));
343 // Create the timer for packet timeout check.
345 Status
= gBS
->CreateEvent (
346 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
348 MnpCheckPacketTimeout
,
350 &MnpDeviceData
->TimeoutCheckTimer
352 if (EFI_ERROR (Status
)) {
353 DEBUG ((EFI_D_ERROR
, "MnpInitializeDeviceData: CreateEvent for packet timeout check failed.\n"));
359 // Create the timer for tx timeout check.
361 Status
= gBS
->CreateEvent (
366 &MnpDeviceData
->TxTimeoutEvent
368 if (EFI_ERROR (Status
)) {
369 DEBUG ((EFI_D_ERROR
, "MnpInitializeDeviceData: CreateEvent for tx timeout event failed.\n"));
373 if (EFI_ERROR (Status
)) {
375 // Free the dynamic allocated resources if necessary.
377 if (MnpDeviceData
->MacString
!= NULL
) {
378 FreePool (MnpDeviceData
->MacString
);
381 if (MnpDeviceData
->TimeoutCheckTimer
!= NULL
) {
382 gBS
->CloseEvent (MnpDeviceData
->TimeoutCheckTimer
);
385 if (MnpDeviceData
->PollTimer
!= NULL
) {
386 gBS
->CloseEvent (MnpDeviceData
->PollTimer
);
389 if (MnpDeviceData
->TxBuf
!= NULL
) {
390 FreePool (MnpDeviceData
->TxBuf
);
393 if (MnpDeviceData
->RxNbufCache
!= NULL
) {
394 MnpFreeNbuf (MnpDeviceData
, MnpDeviceData
->RxNbufCache
);
397 if (MnpDeviceData
->FreeNbufQue
.BufNum
!= 0) {
398 NetbufQueFlush (&MnpDeviceData
->FreeNbufQue
);
402 // Close the Simple Network Protocol.
406 &gEfiSimpleNetworkProtocolGuid
,
417 Destroy the MNP device context data.
419 @param[in, out] MnpDeviceData Pointer to the mnp device context data.
420 @param[in] ImageHandle The driver image handle.
424 MnpDestroyDeviceData (
425 IN OUT MNP_DEVICE_DATA
*MnpDeviceData
,
426 IN EFI_HANDLE ImageHandle
429 NET_CHECK_SIGNATURE (MnpDeviceData
, MNP_DEVICE_DATA_SIGNATURE
);
432 // Free Vlan Config variable name string
434 if (MnpDeviceData
->MacString
!= NULL
) {
435 FreePool (MnpDeviceData
->MacString
);
439 // The GroupAddressList must be empty.
441 ASSERT (IsListEmpty (&MnpDeviceData
->GroupAddressList
));
446 gBS
->CloseEvent (&MnpDeviceData
->TxTimeoutEvent
);
447 gBS
->CloseEvent (&MnpDeviceData
->TimeoutCheckTimer
);
448 gBS
->CloseEvent (&MnpDeviceData
->PollTimer
);
451 // Free the tx buffer.
453 FreePool (MnpDeviceData
->TxBuf
);
456 // Free the RxNbufCache.
458 MnpFreeNbuf (MnpDeviceData
, MnpDeviceData
->RxNbufCache
);
461 // Flush the FreeNbufQue.
463 MnpDeviceData
->NbufCnt
-= MnpDeviceData
->FreeNbufQue
.BufNum
;
464 NetbufQueFlush (&MnpDeviceData
->FreeNbufQue
);
467 // Close the Simple Network Protocol.
470 MnpDeviceData
->ControllerHandle
,
471 &gEfiSimpleNetworkProtocolGuid
,
473 MnpDeviceData
->ControllerHandle
479 Create mnp service context data.
481 @param[in] MnpDeviceData Pointer to the mnp device context data.
482 @param[in] VlanId The VLAN ID.
483 @param[in] Priority The VLAN priority. If VlanId is 0,
486 @return A pointer to MNP_SERVICE_DATA or NULL if failed to create MNP service context.
490 MnpCreateServiceData (
491 IN MNP_DEVICE_DATA
*MnpDeviceData
,
493 IN UINT8 Priority OPTIONAL
496 EFI_HANDLE MnpServiceHandle
;
497 MNP_SERVICE_DATA
*MnpServiceData
;
499 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
500 EFI_VLAN_CONFIG_PROTOCOL
*VlanConfig
;
503 // Initialize the Mnp Service Data.
505 MnpServiceData
= AllocateZeroPool (sizeof (MNP_SERVICE_DATA
));
506 if (MnpServiceData
== NULL
) {
507 DEBUG ((EFI_D_ERROR
, "MnpCreateServiceData: Faild to allocate memory for the new Mnp Service Data.\n"));
513 // Add to MNP service list
515 InsertTailList (&MnpDeviceData
->ServiceList
, &MnpServiceData
->Link
);
517 MnpServiceData
->Signature
= MNP_SERVICE_DATA_SIGNATURE
;
518 MnpServiceData
->MnpDeviceData
= MnpDeviceData
;
521 // Copy the ServiceBinding structure.
523 CopyMem (&MnpServiceData
->ServiceBinding
, &mMnpServiceBindingProtocol
, sizeof (EFI_SERVICE_BINDING_PROTOCOL
));
526 // Initialize the lists.
528 InitializeListHead (&MnpServiceData
->ChildrenList
);
530 SnpMode
= MnpDeviceData
->Snp
->Mode
;
533 // Create VLAN child handle
535 MnpServiceHandle
= MnpCreateVlanChild (
536 MnpDeviceData
->ImageHandle
,
537 MnpDeviceData
->ControllerHandle
,
539 &MnpServiceData
->DevicePath
541 if (MnpServiceHandle
== NULL
) {
542 DEBUG ((EFI_D_ERROR
, "MnpCreateServiceData: Faild to create child handle.\n"));
548 // Open VLAN Config Protocol by child
550 Status
= gBS
->OpenProtocol (
551 MnpDeviceData
->ControllerHandle
,
552 &gEfiVlanConfigProtocolGuid
,
553 (VOID
**) &VlanConfig
,
554 MnpDeviceData
->ImageHandle
,
556 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
558 if (EFI_ERROR (Status
)) {
563 // Reduce MTU for VLAN device
565 MnpServiceData
->Mtu
= SnpMode
->MaxPacketSize
- NET_VLAN_TAG_LEN
;
568 // VlanId set to 0 means rx/tx untagged frame
570 MnpServiceHandle
= MnpDeviceData
->ControllerHandle
;
571 MnpServiceData
->Mtu
= SnpMode
->MaxPacketSize
;
574 MnpServiceData
->ServiceHandle
= MnpServiceHandle
;
575 MnpServiceData
->VlanId
= VlanId
;
576 MnpServiceData
->Priority
= Priority
;
579 // Install the MNP Service Binding Protocol
581 Status
= gBS
->InstallMultipleProtocolInterfaces (
583 &gEfiManagedNetworkServiceBindingProtocolGuid
,
584 &MnpServiceData
->ServiceBinding
,
589 if (EFI_ERROR (Status
)) {
590 MnpDestroyServiceData (MnpServiceData
);
591 MnpServiceData
= NULL
;
594 return MnpServiceData
;
598 Destroy the MNP service context data.
600 @param[in, out] MnpServiceData Pointer to the mnp service context data.
602 @retval EFI_SUCCESS The mnp service context is destroyed.
603 @retval Others Errors as indicated.
607 MnpDestroyServiceData (
608 IN OUT MNP_SERVICE_DATA
*MnpServiceData
614 // Uninstall the MNP Service Binding Protocol
616 Status
= gBS
->UninstallMultipleProtocolInterfaces (
617 MnpServiceData
->ServiceHandle
,
618 &gEfiManagedNetworkServiceBindingProtocolGuid
,
619 &MnpServiceData
->ServiceBinding
,
622 if (EFI_ERROR (Status
)) {
626 if (MnpServiceData
->VlanId
!= 0) {
628 // Close VlanConfig Protocol opened by VLAN child handle
630 Status
= gBS
->CloseProtocol (
631 MnpServiceData
->MnpDeviceData
->ControllerHandle
,
632 &gEfiVlanConfigProtocolGuid
,
633 MnpServiceData
->MnpDeviceData
->ImageHandle
,
634 MnpServiceData
->ServiceHandle
636 if (EFI_ERROR (Status
)) {
641 // Uninstall Device Path Protocol to destroy the VLAN child handle
643 Status
= gBS
->UninstallMultipleProtocolInterfaces (
644 MnpServiceData
->ServiceHandle
,
645 &gEfiDevicePathProtocolGuid
,
646 MnpServiceData
->DevicePath
,
649 if (EFI_ERROR (Status
)) {
653 if (MnpServiceData
->DevicePath
!= NULL
) {
654 FreePool (MnpServiceData
->DevicePath
);
659 // Remove from MnpDeviceData service list
661 RemoveEntryList (&MnpServiceData
->Link
);
663 FreePool (MnpServiceData
);
669 Destroy all child of the MNP service data.
671 @param[in, out] MnpServiceData Pointer to the mnp service context data.
673 @retval EFI_SUCCESS All child are destroyed.
674 @retval Others Failed to destroy all child.
678 MnpDestroyServiceChild (
679 IN OUT MNP_SERVICE_DATA
*MnpServiceData
683 MNP_INSTANCE_DATA
*Instance
;
684 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
686 ServiceBinding
= &MnpServiceData
->ServiceBinding
;
687 while (!IsListEmpty (&MnpServiceData
->ChildrenList
)) {
689 // Don't use NetListRemoveHead here, the remove opreration will be done
690 // in ServiceBindingDestroyChild.
692 Instance
= NET_LIST_HEAD (
693 &MnpServiceData
->ChildrenList
,
698 Status
= ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
699 if (EFI_ERROR (Status
)) {
708 Find the MNP Service Data for given VLAN ID.
710 @param[in] MnpDeviceData Pointer to the mnp device context data.
711 @param[in] VlanId The VLAN ID.
713 @return A pointer to MNP_SERVICE_DATA or NULL if not found.
718 IN MNP_DEVICE_DATA
*MnpDeviceData
,
723 MNP_SERVICE_DATA
*MnpServiceData
;
725 NET_LIST_FOR_EACH (Entry
, &MnpDeviceData
->ServiceList
) {
727 // Check VLAN ID of each Mnp Service Data
729 MnpServiceData
= MNP_SERVICE_DATA_FROM_LINK (Entry
);
730 if (MnpServiceData
->VlanId
== VlanId
) {
731 return MnpServiceData
;
739 Initialize the mnp instance context data.
741 @param[in] MnpServiceData Pointer to the mnp service context data.
742 @param[in, out] Instance Pointer to the mnp instance context data
747 MnpInitializeInstanceData (
748 IN MNP_SERVICE_DATA
*MnpServiceData
,
749 IN OUT MNP_INSTANCE_DATA
*Instance
752 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
753 ASSERT (Instance
!= NULL
);
756 // Set the signature.
758 Instance
->Signature
= MNP_INSTANCE_DATA_SIGNATURE
;
761 // Copy the MNP Protocol interfaces from the template.
763 CopyMem (&Instance
->ManagedNetwork
, &mMnpProtocolTemplate
, sizeof (Instance
->ManagedNetwork
));
766 // Copy the default config data.
768 CopyMem (&Instance
->ConfigData
, &mMnpDefaultConfigData
, sizeof (Instance
->ConfigData
));
771 // Initialize the lists.
773 InitializeListHead (&Instance
->GroupCtrlBlkList
);
774 InitializeListHead (&Instance
->RcvdPacketQueue
);
775 InitializeListHead (&Instance
->RxDeliveredPacketQueue
);
778 // Initialize the RxToken Map.
780 NetMapInit (&Instance
->RxTokenMap
);
783 // Save the MnpServiceData info.
785 Instance
->MnpServiceData
= MnpServiceData
;
790 Check whether the token specified by Arg matches the token in Item.
792 @param[in] Map Pointer to the NET_MAP.
793 @param[in] Item Pointer to the NET_MAP_ITEM.
794 @param[in] Arg Pointer to the Arg, it's a pointer to the token to
797 @retval EFI_SUCCESS The token specified by Arg is different from the
799 @retval EFI_ACCESS_DENIED The token specified by Arg is the same as that in
806 IN NET_MAP_ITEM
*Item
,
810 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*Token
;
811 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*TokenInItem
;
813 Token
= (EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*) Arg
;
814 TokenInItem
= (EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*) Item
->Key
;
816 if ((Token
== TokenInItem
) || (Token
->Event
== TokenInItem
->Event
)) {
818 // The token is the same either the two tokens equals or the Events in
819 // the two tokens are the same.
821 return EFI_ACCESS_DENIED
;
828 Cancel the token specified by Arg if it matches the token in Item.
830 @param[in, out] Map Pointer to the NET_MAP.
831 @param[in, out] Item Pointer to the NET_MAP_ITEM.
832 @param[in] Arg Pointer to the Arg, it's a pointer to the
835 @retval EFI_SUCCESS The Arg is NULL, and the token in Item is cancelled,
836 or the Arg isn't NULL, and the token in Item is
837 different from the Arg.
838 @retval EFI_ABORTED The Arg isn't NULL, the token in Item mathces the
839 Arg, and the token is cancelled.
845 IN OUT NET_MAP_ITEM
*Item
,
849 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*TokenToCancel
;
851 if ((Arg
!= NULL
) && (Item
->Key
!= Arg
)) {
853 // The token in Item is not the token specified by Arg.
858 TokenToCancel
= (EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*) Item
->Key
;
861 // Remove the item from the map.
863 NetMapRemoveItem (Map
, Item
, NULL
);
866 // Cancel this token with status set to EFI_ABORTED.
868 TokenToCancel
->Status
= EFI_ABORTED
;
869 gBS
->SignalEvent (TokenToCancel
->Event
);
873 // Only abort the token specified by Arg if Arg isn't NULL.
883 Start and initialize the simple network.
885 @param[in] Snp Pointer to the simple network protocol.
887 @retval EFI_SUCCESS The simple network protocol is started.
888 @retval Others Other errors as indicated.
893 IN EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
898 ASSERT (Snp
!= NULL
);
901 // Start the simple network.
903 Status
= Snp
->Start (Snp
);
905 if (!EFI_ERROR (Status
)) {
907 // Initialize the simple network.
909 Status
= Snp
->Initialize (Snp
, 0, 0);
917 Stop the simple network.
919 @param[in] Snp Pointer to the simple network protocol.
921 @retval EFI_SUCCESS The simple network is stopped.
922 @retval Others Other errors as indicated.
927 IN EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
932 ASSERT (Snp
!= NULL
);
935 // Shut down the simple network.
937 Status
= Snp
->Shutdown (Snp
);
938 if (!EFI_ERROR (Status
)) {
940 // Stop the simple network.
942 Status
= Snp
->Stop (Snp
);
950 Start the managed network, this function is called when one instance is configured
953 @param[in, out] MnpServiceData Pointer to the mnp service context data.
954 @param[in] IsConfigUpdate The instance is reconfigured or it's the first
955 time the instanced is configured.
956 @param[in] EnableSystemPoll Enable the system polling or not.
958 @retval EFI_SUCCESS The managed network is started and some
959 configuration is updated.
960 @retval Others Other errors as indicated.
965 IN OUT MNP_SERVICE_DATA
*MnpServiceData
,
966 IN BOOLEAN IsConfigUpdate
,
967 IN BOOLEAN EnableSystemPoll
971 EFI_TIMER_DELAY TimerOpType
;
972 MNP_DEVICE_DATA
*MnpDeviceData
;
974 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
976 Status
= EFI_SUCCESS
;
977 MnpDeviceData
= MnpServiceData
->MnpDeviceData
;
979 if (!IsConfigUpdate
) {
981 // If it's not a configuration update, increase the configured children number.
983 MnpDeviceData
->ConfiguredChildrenNumber
++;
985 if (MnpDeviceData
->ConfiguredChildrenNumber
== 1) {
987 // It's the first configured child, start the simple network.
989 Status
= MnpStartSnp (MnpDeviceData
->Snp
);
990 if (EFI_ERROR (Status
)) {
991 DEBUG ((EFI_D_ERROR
, "MnpStart: MnpStartSnp failed, %r.\n", Status
));
997 // Start the timeout timer.
999 Status
= gBS
->SetTimer (
1000 MnpDeviceData
->TimeoutCheckTimer
,
1002 MNP_TIMEOUT_CHECK_INTERVAL
1004 if (EFI_ERROR (Status
)) {
1007 "MnpStart, gBS->SetTimer for TimeoutCheckTimer %r.\n",
1016 if (MnpDeviceData
->EnableSystemPoll
^ EnableSystemPoll
) {
1018 // The EnableSystemPoll differs with the current state, disable or enable
1021 TimerOpType
= EnableSystemPoll
? TimerPeriodic
: TimerCancel
;
1023 Status
= gBS
->SetTimer (MnpDeviceData
->PollTimer
, TimerOpType
, MNP_SYS_POLL_INTERVAL
);
1024 if (EFI_ERROR (Status
)) {
1025 DEBUG ((EFI_D_ERROR
, "MnpStart: gBS->SetTimer for PollTimer failed, %r.\n", Status
));
1030 MnpDeviceData
->EnableSystemPoll
= EnableSystemPoll
;
1034 // Change the receive filters if need.
1036 Status
= MnpConfigReceiveFilters (MnpDeviceData
);
1044 Stop the managed network.
1046 @param[in, out] MnpServiceData Pointer to the mnp service context data.
1048 @retval EFI_SUCCESS The managed network is stopped.
1049 @retval Others Other errors as indicated.
1054 IN OUT MNP_SERVICE_DATA
*MnpServiceData
1058 MNP_DEVICE_DATA
*MnpDeviceData
;
1060 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
1061 MnpDeviceData
= MnpServiceData
->MnpDeviceData
;
1062 ASSERT (MnpDeviceData
->ConfiguredChildrenNumber
> 0);
1065 // Configure the receive filters.
1067 MnpConfigReceiveFilters (MnpDeviceData
);
1070 // Decrease the children number.
1072 MnpDeviceData
->ConfiguredChildrenNumber
--;
1074 if (MnpDeviceData
->ConfiguredChildrenNumber
> 0) {
1076 // If there are other configured chilren, return and keep the timers and
1077 // simple network unchanged.
1083 // No configured children now.
1085 if (MnpDeviceData
->EnableSystemPoll
) {
1087 // The system poll in on, cancel the poll timer.
1089 Status
= gBS
->SetTimer (MnpDeviceData
->PollTimer
, TimerCancel
, 0);
1090 MnpDeviceData
->EnableSystemPoll
= FALSE
;
1094 // Cancel the timeout timer.
1096 Status
= gBS
->SetTimer (MnpDeviceData
->TimeoutCheckTimer
, TimerCancel
, 0);
1099 // Stop the simple network.
1101 Status
= MnpStopSnp (MnpDeviceData
->Snp
);
1107 Flush the instance's received data.
1109 @param[in, out] Instance Pointer to the mnp instance context data.
1113 MnpFlushRcvdDataQueue (
1114 IN OUT MNP_INSTANCE_DATA
*Instance
1118 MNP_RXDATA_WRAP
*RxDataWrap
;
1120 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
1122 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1124 while (!IsListEmpty (&Instance
->RcvdPacketQueue
)) {
1126 // Remove all the Wraps.
1128 RxDataWrap
= NET_LIST_HEAD (&Instance
->RcvdPacketQueue
, MNP_RXDATA_WRAP
, WrapEntry
);
1131 // Recycle the RxDataWrap.
1133 MnpRecycleRxData (NULL
, (VOID
*) RxDataWrap
);
1134 Instance
->RcvdPacketQueueSize
--;
1137 ASSERT (Instance
->RcvdPacketQueueSize
== 0);
1139 gBS
->RestoreTPL (OldTpl
);
1144 Configure the Instance using ConfigData.
1146 @param[in, out] Instance Pointer to the mnp instance context data.
1147 @param[in] ConfigData Pointer to the configuration data used to configure
1150 @retval EFI_SUCCESS The Instance is configured.
1151 @retval EFI_UNSUPPORTED EnableReceiveTimestamps is on and the
1152 implementation doesn't support it.
1153 @retval Others Other errors as indicated.
1157 MnpConfigureInstance (
1158 IN OUT MNP_INSTANCE_DATA
*Instance
,
1159 IN EFI_MANAGED_NETWORK_CONFIG_DATA
*ConfigData OPTIONAL
1163 MNP_SERVICE_DATA
*MnpServiceData
;
1164 MNP_DEVICE_DATA
*MnpDeviceData
;
1165 EFI_MANAGED_NETWORK_CONFIG_DATA
*OldConfigData
;
1166 EFI_MANAGED_NETWORK_CONFIG_DATA
*NewConfigData
;
1167 BOOLEAN IsConfigUpdate
;
1169 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
1171 if ((ConfigData
!= NULL
) && ConfigData
->EnableReceiveTimestamps
) {
1173 // Don't support timestamp.
1175 return EFI_UNSUPPORTED
;
1178 Status
= EFI_SUCCESS
;
1180 MnpServiceData
= Instance
->MnpServiceData
;
1181 MnpDeviceData
= MnpServiceData
->MnpDeviceData
;
1182 NET_CHECK_SIGNATURE (MnpDeviceData
, MNP_DEVICE_DATA_SIGNATURE
);
1184 IsConfigUpdate
= (BOOLEAN
) ((Instance
->Configured
) && (ConfigData
!= NULL
));
1186 OldConfigData
= &Instance
->ConfigData
;
1187 NewConfigData
= ConfigData
;
1188 if (NewConfigData
== NULL
) {
1190 // Restore back the default config data if a reset of this instance
1193 NewConfigData
= &mMnpDefaultConfigData
;
1197 // Reset the instance's receive filter.
1199 Instance
->ReceiveFilter
= 0;
1202 // Clear the receive counters according to the old ConfigData.
1204 if (OldConfigData
->EnableUnicastReceive
) {
1205 MnpDeviceData
->UnicastCount
--;
1208 if (OldConfigData
->EnableMulticastReceive
) {
1209 MnpDeviceData
->MulticastCount
--;
1212 if (OldConfigData
->EnableBroadcastReceive
) {
1213 MnpDeviceData
->BroadcastCount
--;
1216 if (OldConfigData
->EnablePromiscuousReceive
) {
1217 MnpDeviceData
->PromiscuousCount
--;
1221 // Set the receive filter counters and the receive filter of the
1222 // instance according to the new ConfigData.
1224 if (NewConfigData
->EnableUnicastReceive
) {
1225 MnpDeviceData
->UnicastCount
++;
1226 Instance
->ReceiveFilter
|= MNP_RECEIVE_UNICAST
;
1229 if (NewConfigData
->EnableMulticastReceive
) {
1230 MnpDeviceData
->MulticastCount
++;
1233 if (NewConfigData
->EnableBroadcastReceive
) {
1234 MnpDeviceData
->BroadcastCount
++;
1235 Instance
->ReceiveFilter
|= MNP_RECEIVE_BROADCAST
;
1238 if (NewConfigData
->EnablePromiscuousReceive
) {
1239 MnpDeviceData
->PromiscuousCount
++;
1242 if (OldConfigData
->FlushQueuesOnReset
) {
1243 MnpFlushRcvdDataQueue (Instance
);
1246 if (ConfigData
== NULL
) {
1247 Instance
->ManagedNetwork
.Cancel (&Instance
->ManagedNetwork
, NULL
);
1250 if (!NewConfigData
->EnableMulticastReceive
) {
1251 MnpGroupOp (Instance
, FALSE
, NULL
, NULL
);
1255 // Save the new configuration data.
1257 CopyMem (OldConfigData
, NewConfigData
, sizeof (*OldConfigData
));
1259 Instance
->Configured
= (BOOLEAN
) (ConfigData
!= NULL
);
1260 if (Instance
->Configured
) {
1262 // The instance is configured, start the Mnp.
1267 (BOOLEAN
) !NewConfigData
->DisableBackgroundPolling
1271 // The instance is changed to the unconfigured state, stop the Mnp.
1273 Status
= MnpStop (MnpServiceData
);
1280 Configure the Snp receive filters according to the instances' receive filter
1283 @param[in] MnpDeviceData Pointer to the mnp device context data.
1285 @retval EFI_SUCCESS The receive filters is configured.
1286 @retval EFI_OUT_OF_RESOURCES The receive filters can't be configured due
1287 to lack of memory resource.
1291 MnpConfigReceiveFilters (
1292 IN MNP_DEVICE_DATA
*MnpDeviceData
1296 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1297 EFI_MAC_ADDRESS
*MCastFilter
;
1298 UINT32 MCastFilterCnt
;
1299 UINT32 EnableFilterBits
;
1300 UINT32 DisableFilterBits
;
1301 BOOLEAN ResetMCastFilters
;
1304 MNP_GROUP_ADDRESS
*GroupAddress
;
1306 NET_CHECK_SIGNATURE (MnpDeviceData
, MNP_DEVICE_DATA_SIGNATURE
);
1308 Snp
= MnpDeviceData
->Snp
;
1311 // Initialize the enable filter and disable filter.
1313 EnableFilterBits
= 0;
1314 DisableFilterBits
= Snp
->Mode
->ReceiveFilterMask
;
1316 if (MnpDeviceData
->UnicastCount
!= 0) {
1318 // Enable unicast if any instance wants to receive unicast.
1320 EnableFilterBits
|= EFI_SIMPLE_NETWORK_RECEIVE_UNICAST
;
1323 if (MnpDeviceData
->BroadcastCount
!= 0) {
1325 // Enable broadcast if any instance wants to receive broadcast.
1327 EnableFilterBits
|= EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST
;
1332 ResetMCastFilters
= TRUE
;
1334 if ((MnpDeviceData
->MulticastCount
!= 0) && (MnpDeviceData
->GroupAddressCount
!= 0)) {
1336 // There are instances configured to receive multicast and already some group
1337 // addresses are joined.
1340 ResetMCastFilters
= FALSE
;
1342 if (MnpDeviceData
->GroupAddressCount
<= Snp
->Mode
->MaxMCastFilterCount
) {
1344 // The joind group address is less than simple network's maximum count.
1345 // Just configure the snp to do the multicast filtering.
1348 EnableFilterBits
|= EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST
;
1351 // Allocate pool for the mulicast addresses.
1353 MCastFilterCnt
= MnpDeviceData
->GroupAddressCount
;
1354 MCastFilter
= AllocatePool (sizeof (EFI_MAC_ADDRESS
) * MCastFilterCnt
);
1355 if (MCastFilter
== NULL
) {
1356 DEBUG ((EFI_D_ERROR
, "MnpConfigReceiveFilters: Failed to allocate memory resource for MCastFilter.\n"));
1358 return EFI_OUT_OF_RESOURCES
;
1362 // Fill the multicast HW address buffer.
1365 NET_LIST_FOR_EACH (Entry
, &MnpDeviceData
->GroupAddressList
) {
1367 GroupAddress
= NET_LIST_USER_STRUCT (Entry
, MNP_GROUP_ADDRESS
, AddrEntry
);
1368 CopyMem (MCastFilter
+ Index
, &GroupAddress
->Address
, sizeof (*(MCastFilter
+ Index
)));
1371 ASSERT (Index
<= MCastFilterCnt
);
1375 // The maximum multicast is reached, set the filter to be promiscuous
1379 if ((Snp
->Mode
->ReceiveFilterMask
& EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST
) != 0) {
1380 EnableFilterBits
|= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST
;
1383 // Either MULTICAST or PROMISCUOUS_MULTICAST is not supported by Snp,
1384 // set the NIC to be promiscuous although this will tremendously degrade
1387 EnableFilterBits
|= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS
;
1392 if (MnpDeviceData
->PromiscuousCount
!= 0) {
1394 // Enable promiscuous if any instance wants to receive promiscuous.
1396 EnableFilterBits
|= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS
;
1400 // Set the disable filter.
1402 DisableFilterBits
^= EnableFilterBits
;
1405 // Configure the receive filters of SNP.
1407 Status
= Snp
->ReceiveFilters (
1416 if (EFI_ERROR (Status
)) {
1419 "MnpConfigReceiveFilters: Snp->ReceiveFilters failed, %r.\n",
1425 if (MCastFilter
!= NULL
) {
1427 // Free the buffer used to hold the group addresses.
1429 FreePool (MCastFilter
);
1437 Add a group address control block which controls the MacAddress for
1440 @param[in, out] Instance Pointer to the mnp instance context data.
1441 @param[in, out] CtrlBlk Pointer to the group address control block.
1442 @param[in, out] GroupAddress Pointer to the group adress.
1443 @param[in] MacAddress Pointer to the mac address.
1444 @param[in] HwAddressSize The hardware address size.
1446 @retval EFI_SUCCESS The group address control block is added.
1447 @retval EFI_OUT_OF_RESOURCES Failed due to lack of memory resources.
1451 MnpGroupOpAddCtrlBlk (
1452 IN OUT MNP_INSTANCE_DATA
*Instance
,
1453 IN OUT MNP_GROUP_CONTROL_BLOCK
*CtrlBlk
,
1454 IN OUT MNP_GROUP_ADDRESS
*GroupAddress OPTIONAL
,
1455 IN EFI_MAC_ADDRESS
*MacAddress
,
1456 IN UINT32 HwAddressSize
1459 MNP_DEVICE_DATA
*MnpDeviceData
;
1461 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
1463 MnpDeviceData
= Instance
->MnpServiceData
->MnpDeviceData
;
1464 NET_CHECK_SIGNATURE (MnpDeviceData
, MNP_DEVICE_DATA_SIGNATURE
);
1466 if (GroupAddress
== NULL
) {
1467 ASSERT (MacAddress
!= NULL
);
1470 // Allocate a new GroupAddress to be added into MNP's GroupAddressList.
1472 GroupAddress
= AllocatePool (sizeof (MNP_GROUP_ADDRESS
));
1473 if (GroupAddress
== NULL
) {
1475 DEBUG ((EFI_D_ERROR
, "MnpGroupOpFormCtrlBlk: Failed to allocate memory resource.\n"));
1477 return EFI_OUT_OF_RESOURCES
;
1480 CopyMem (&GroupAddress
->Address
, MacAddress
, sizeof (GroupAddress
->Address
));
1481 GroupAddress
->RefCnt
= 0;
1483 &MnpDeviceData
->GroupAddressList
,
1484 &GroupAddress
->AddrEntry
1486 MnpDeviceData
->GroupAddressCount
++;
1490 // Increase the RefCnt.
1492 GroupAddress
->RefCnt
++;
1495 // Add the CtrlBlk into the instance's GroupCtrlBlkList.
1497 CtrlBlk
->GroupAddress
= GroupAddress
;
1498 InsertTailList (&Instance
->GroupCtrlBlkList
, &CtrlBlk
->CtrlBlkEntry
);
1505 Delete a group control block from the instance. If the controlled group address's
1506 reference count reaches zero, the group address is removed too.
1508 @param[in] Instance Pointer to the instance context data.
1509 @param[in] CtrlBlk Pointer to the group control block to delete.
1511 @return The group address controlled by the control block is no longer used or not.
1515 MnpGroupOpDelCtrlBlk (
1516 IN MNP_INSTANCE_DATA
*Instance
,
1517 IN MNP_GROUP_CONTROL_BLOCK
*CtrlBlk
1520 MNP_DEVICE_DATA
*MnpDeviceData
;
1521 MNP_GROUP_ADDRESS
*GroupAddress
;
1523 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
1525 MnpDeviceData
= Instance
->MnpServiceData
->MnpDeviceData
;
1526 NET_CHECK_SIGNATURE (MnpDeviceData
, MNP_DEVICE_DATA_SIGNATURE
);
1529 // Remove and free the CtrlBlk.
1531 GroupAddress
= CtrlBlk
->GroupAddress
;
1532 RemoveEntryList (&CtrlBlk
->CtrlBlkEntry
);
1535 ASSERT (GroupAddress
->RefCnt
> 0);
1538 // Count down the RefCnt.
1540 GroupAddress
->RefCnt
--;
1542 if (GroupAddress
->RefCnt
== 0) {
1544 // Free this GroupAddress entry if no instance uses it.
1546 MnpDeviceData
->GroupAddressCount
--;
1547 RemoveEntryList (&GroupAddress
->AddrEntry
);
1548 FreePool (GroupAddress
);
1558 Do the group operations for this instance.
1560 @param[in, out] Instance Pointer to the instance context data.
1561 @param[in] JoinFlag Set to TRUE to join a group. Set to TRUE to
1562 leave a group/groups.
1563 @param[in] MacAddress Pointer to the group address to join or leave.
1564 @param[in] CtrlBlk Pointer to the group control block if JoinFlag
1567 @retval EFI_SUCCESS The group operation finished.
1568 @retval EFI_OUT_OF_RESOURCES Failed due to lack of memory resources.
1569 @retval Others Other errors as indicated.
1574 IN OUT MNP_INSTANCE_DATA
*Instance
,
1575 IN BOOLEAN JoinFlag
,
1576 IN EFI_MAC_ADDRESS
*MacAddress OPTIONAL
,
1577 IN MNP_GROUP_CONTROL_BLOCK
*CtrlBlk OPTIONAL
1580 MNP_DEVICE_DATA
*MnpDeviceData
;
1582 LIST_ENTRY
*NextEntry
;
1583 MNP_GROUP_ADDRESS
*GroupAddress
;
1584 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
1585 MNP_GROUP_CONTROL_BLOCK
*NewCtrlBlk
;
1587 BOOLEAN AddressExist
;
1590 NET_CHECK_SIGNATURE (Instance
, MNP_INSTANCE_DATA_SIGNATURE
);
1592 MnpDeviceData
= Instance
->MnpServiceData
->MnpDeviceData
;
1593 SnpMode
= MnpDeviceData
->Snp
->Mode
;
1597 // A new gropu address is to be added.
1599 GroupAddress
= NULL
;
1600 AddressExist
= FALSE
;
1603 // Allocate memory for the control block.
1605 NewCtrlBlk
= AllocatePool (sizeof (MNP_GROUP_CONTROL_BLOCK
));
1606 if (NewCtrlBlk
== NULL
) {
1607 DEBUG ((EFI_D_ERROR
, "MnpGroupOp: Failed to allocate memory resource.\n"));
1609 return EFI_OUT_OF_RESOURCES
;
1612 NET_LIST_FOR_EACH (Entry
, &MnpDeviceData
->GroupAddressList
) {
1614 // Check whether the MacAddress is already joined by other instances.
1616 GroupAddress
= NET_LIST_USER_STRUCT (Entry
, MNP_GROUP_ADDRESS
, AddrEntry
);
1617 if (CompareMem (MacAddress
, &GroupAddress
->Address
, SnpMode
->HwAddressSize
) == 0) {
1618 AddressExist
= TRUE
;
1623 if (!AddressExist
) {
1624 GroupAddress
= NULL
;
1628 // Add the GroupAddress for this instance.
1630 Status
= MnpGroupOpAddCtrlBlk (
1635 SnpMode
->HwAddressSize
1637 if (EFI_ERROR (Status
)) {
1643 if (MacAddress
!= NULL
) {
1644 ASSERT (CtrlBlk
!= NULL
);
1647 // Leave the specific multicast mac address.
1649 NeedUpdate
= MnpGroupOpDelCtrlBlk (Instance
, CtrlBlk
);
1652 // Leave all multicast mac addresses.
1656 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Instance
->GroupCtrlBlkList
) {
1658 NewCtrlBlk
= NET_LIST_USER_STRUCT (
1660 MNP_GROUP_CONTROL_BLOCK
,
1664 // Update is required if the group address left is no longer used
1665 // by other instances.
1667 NeedUpdate
= MnpGroupOpDelCtrlBlk (Instance
, NewCtrlBlk
);
1672 Status
= EFI_SUCCESS
;
1676 // Reconfigure the receive filters if necessary.
1678 Status
= MnpConfigReceiveFilters (MnpDeviceData
);