3 Copyright (c) 2005 - 2006, 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 The driver binding and service binding protocol for IP4 driver.
25 EFI_DRIVER_BINDING_PROTOCOL gIp4DriverBinding
= {
26 Ip4DriverBindingSupported
,
27 Ip4DriverBindingStart
,
37 IN EFI_HANDLE ImageHandle
,
38 IN EFI_SYSTEM_TABLE
*SystemTable
44 The entry point for IP4 driver which install the driver
45 binding and component name protocol on its image.
49 ImageHandle - The image handle of the driver
50 SystemTable - The system table
54 EFI_SUCCESS if the driver binding and component name protocols
55 are successfully installed, otherwise if failed.
59 return EfiLibInstallDriverBindingComponentName2 (
71 Test to see if this driver supports ControllerHandle.
73 @param This Protocol instance pointer.
74 @param ControllerHandle Handle of device to test
75 @param RemainingDevicePath Optional parameter use to pick a specific child
78 @retval EFI_SUCCES This driver supports this device
79 @retval EFI_ALREADY_STARTED This driver is already running on this device
80 @retval other This driver does not support this device
85 Ip4DriverBindingSupported (
86 IN EFI_DRIVER_BINDING_PROTOCOL
* This
,
87 IN EFI_HANDLE ControllerHandle
,
88 IN EFI_DEVICE_PATH_PROTOCOL
* RemainingDevicePath OPTIONAL
94 // Test for the MNP service binding Protocol
96 Status
= gBS
->OpenProtocol (
98 &gEfiManagedNetworkServiceBindingProtocolGuid
,
100 This
->DriverBindingHandle
,
102 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
105 if (EFI_ERROR (Status
)) {
110 // Test for the Arp service binding Protocol
112 Status
= gBS
->OpenProtocol (
114 &gEfiArpServiceBindingProtocolGuid
,
116 This
->DriverBindingHandle
,
118 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
132 Create a new IP4 driver service binding protocol
134 @param Controller The controller that has MNP service binding
136 @param ImageHandle The IP4 driver's image handle
137 @param Service The variable to receive the newly created IP4
140 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
141 @retval EFI_SUCCESS A new IP4 service binding private is created.
147 IN EFI_HANDLE Controller
,
148 IN EFI_HANDLE ImageHandle
,
149 OUT IP4_SERVICE
**Service
155 ASSERT (Service
!= NULL
);
160 // allocate a service private data then initialize all the filed to
161 // empty resources, so if any thing goes wrong when allocating
162 // resources, Ip4CleanService can be called to clean it up.
164 IpSb
= NetAllocatePool (sizeof (IP4_SERVICE
));
167 return EFI_OUT_OF_RESOURCES
;
170 IpSb
->Signature
= IP4_SERVICE_SIGNATURE
;
171 IpSb
->ServiceBinding
.CreateChild
= Ip4ServiceBindingCreateChild
;
172 IpSb
->ServiceBinding
.DestroyChild
= Ip4ServiceBindingDestroyChild
;
173 IpSb
->State
= IP4_SERVICE_UNSTARTED
;
174 IpSb
->InDestory
= FALSE
;
176 IpSb
->NumChildren
= 0;
177 NetListInit (&IpSb
->Children
);
179 NetListInit (&IpSb
->Interfaces
);
180 IpSb
->DefaultInterface
= NULL
;
181 IpSb
->DefaultRouteTable
= NULL
;
183 Ip4InitAssembleTable (&IpSb
->Assemble
);
185 IpSb
->IgmpCtrl
.Igmpv1QuerySeen
= 0;
186 NetListInit (&IpSb
->IgmpCtrl
.Groups
);
188 IpSb
->Image
= ImageHandle
;
189 IpSb
->Controller
= Controller
;
191 IpSb
->MnpChildHandle
= NULL
;
194 IpSb
->MnpConfigData
.ReceivedQueueTimeoutValue
= 0;
195 IpSb
->MnpConfigData
.TransmitQueueTimeoutValue
= 0;
196 IpSb
->MnpConfigData
.ProtocolTypeFilter
= IP4_ETHER_PROTO
;
197 IpSb
->MnpConfigData
.EnableUnicastReceive
= TRUE
;
198 IpSb
->MnpConfigData
.EnableMulticastReceive
= TRUE
;
199 IpSb
->MnpConfigData
.EnableBroadcastReceive
= TRUE
;
200 IpSb
->MnpConfigData
.EnablePromiscuousReceive
= FALSE
;
201 IpSb
->MnpConfigData
.FlushQueuesOnReset
= TRUE
;
202 IpSb
->MnpConfigData
.EnableReceiveTimestamps
= FALSE
;
203 IpSb
->MnpConfigData
.DisableBackgroundPolling
= FALSE
;
205 NetZeroMem (&IpSb
->SnpMode
, sizeof (EFI_SIMPLE_NETWORK_MODE
));
208 IpSb
->Ip4Config
= NULL
;
209 IpSb
->DoneEvent
= NULL
;
210 IpSb
->ReconfigEvent
= NULL
;
213 // Create various resources. First create the route table, timer
214 // event and MNP child. IGMP, interface's initialization depend
217 IpSb
->DefaultRouteTable
= Ip4CreateRouteTable ();
219 if (IpSb
->DefaultRouteTable
== NULL
) {
220 Status
= EFI_OUT_OF_RESOURCES
;
224 Status
= gBS
->CreateEvent (
225 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
232 if (EFI_ERROR (Status
)) {
236 Status
= NetLibCreateServiceChild (
239 &gEfiManagedNetworkServiceBindingProtocolGuid
,
240 &IpSb
->MnpChildHandle
243 if (EFI_ERROR (Status
)) {
247 Status
= gBS
->OpenProtocol (
248 IpSb
->MnpChildHandle
,
249 &gEfiManagedNetworkProtocolGuid
,
250 (VOID
**) &IpSb
->Mnp
,
253 EFI_OPEN_PROTOCOL_BY_DRIVER
256 if (EFI_ERROR (Status
)) {
260 Status
= Ip4ServiceConfigMnp (IpSb
, TRUE
);
262 if (EFI_ERROR (Status
)) {
266 Status
= IpSb
->Mnp
->GetModeData (IpSb
->Mnp
, NULL
, &IpSb
->SnpMode
);
268 if (EFI_ERROR (Status
)) {
272 Status
= Ip4InitIgmp (IpSb
);
274 if (EFI_ERROR (Status
)) {
278 IpSb
->DefaultInterface
= Ip4CreateInterface (IpSb
->Mnp
, Controller
, ImageHandle
);
280 if (IpSb
->DefaultInterface
== NULL
) {
281 Status
= EFI_OUT_OF_RESOURCES
;
285 NetListInsertHead (&IpSb
->Interfaces
, &IpSb
->DefaultInterface
->Link
);
287 IpSb
->MacString
= NULL
;
293 Ip4CleanService (IpSb
);
301 Clean up a IP4 service binding instance. It will release all
302 the resource allocated by the instance. The instance may be
303 partly initialized, or partly destoried. If a resource is
304 destoried, it is marked as that in case the destory failed and
305 being called again later.
307 @param IpSb The IP4 serviceing binding instance to clean up
309 @retval EFI_SUCCESS The resource used by the instance are cleaned up
310 @retval Others Failed to clean up some of the resources.
320 if (IpSb
->DefaultInterface
!= NULL
) {
321 Status
= Ip4FreeInterface (IpSb
->DefaultInterface
, NULL
);
323 if (EFI_ERROR (Status
)) {
327 IpSb
->DefaultInterface
= NULL
;
330 if (IpSb
->DefaultRouteTable
!= NULL
) {
331 Ip4FreeRouteTable (IpSb
->DefaultRouteTable
);
332 IpSb
->DefaultRouteTable
= NULL
;
335 Ip4CleanAssembleTable (&IpSb
->Assemble
);
337 if (IpSb
->MnpChildHandle
!= NULL
) {
340 IpSb
->MnpChildHandle
,
341 &gEfiManagedNetworkProtocolGuid
,
349 NetLibDestroyServiceChild (
352 &gEfiManagedNetworkServiceBindingProtocolGuid
,
356 IpSb
->MnpChildHandle
= NULL
;
359 if (IpSb
->Timer
!= NULL
) {
360 gBS
->SetTimer (IpSb
->Timer
, TimerCancel
, 0);
361 gBS
->CloseEvent (IpSb
->Timer
);
366 if (IpSb
->Ip4Config
!= NULL
) {
367 IpSb
->Ip4Config
->Stop (IpSb
->Ip4Config
);
371 &gEfiIp4ConfigProtocolGuid
,
376 gBS
->CloseEvent (IpSb
->DoneEvent
);
377 gBS
->CloseEvent (IpSb
->ReconfigEvent
);
378 IpSb
->Ip4Config
= NULL
;
386 Start this driver on ControllerHandle.
388 @param This Protocol instance pointer.
389 @param ControllerHandle Handle of device to bind driver to
390 @param RemainingDevicePath Optional parameter use to pick a specific child
393 @retval EFI_SUCCES This driver is added to ControllerHandle
394 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
395 @retval other This driver does not support this device
400 Ip4DriverBindingStart (
401 IN EFI_DRIVER_BINDING_PROTOCOL
* This
,
402 IN EFI_HANDLE ControllerHandle
,
403 IN EFI_DEVICE_PATH_PROTOCOL
* RemainingDevicePath OPTIONAL
410 // Test for the Ip4 service binding protocol
412 Status
= gBS
->OpenProtocol (
414 &gEfiIp4ServiceBindingProtocolGuid
,
416 This
->DriverBindingHandle
,
418 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
421 if (Status
== EFI_SUCCESS
) {
422 return EFI_ALREADY_STARTED
;
425 Status
= Ip4CreateService (ControllerHandle
, This
->DriverBindingHandle
, &IpSb
);
427 if (EFI_ERROR (Status
)) {
432 // Install the Ip4ServiceBinding Protocol onto ControlerHandle
434 Status
= gBS
->InstallMultipleProtocolInterfaces (
436 &gEfiIp4ServiceBindingProtocolGuid
,
437 &IpSb
->ServiceBinding
,
441 if (EFI_ERROR (Status
)) {
446 // ready to go: start the receiving and timer
448 Status
= Ip4ReceiveFrame (IpSb
->DefaultInterface
, NULL
, Ip4AccpetFrame
, IpSb
);
450 if (EFI_ERROR (Status
)) {
451 goto UNINSTALL_PROTOCOL
;
454 Status
= gBS
->SetTimer (IpSb
->Timer
, TimerPeriodic
, TICKS_PER_SECOND
);
456 if (EFI_ERROR (Status
)) {
457 goto UNINSTALL_PROTOCOL
;
461 // Initialize the IP4 ID
463 mIp4Id
= (UINT16
)NET_RANDOM (NetRandomInitSeed ());
465 Ip4SetVariableData (IpSb
);
470 gBS
->UninstallProtocolInterface (
472 &gEfiIp4ServiceBindingProtocolGuid
,
473 &IpSb
->ServiceBinding
477 Ip4CleanService (IpSb
);
485 Stop this driver on ControllerHandle.
487 @param This Protocol instance pointer.
488 @param ControllerHandle Handle of device to stop driver on
489 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number
490 of children is zero stop the entire bus driver.
491 @param ChildHandleBuffer List of Child Handles to Stop.
493 @retval EFI_SUCCES This driver is removed ControllerHandle
494 @retval other This driver was not removed from this device
499 Ip4DriverBindingStop (
500 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
501 IN EFI_HANDLE ControllerHandle
,
502 IN UINTN NumberOfChildren
,
503 IN EFI_HANDLE
*ChildHandleBuffer
506 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
508 IP4_PROTOCOL
*IpInstance
;
509 EFI_HANDLE NicHandle
;
515 // IP4 driver opens the MNP child, ARP children or the IP4_CONFIG protocol
516 // by driver. So the ControllerHandle may be the MNP child handle, ARP child
517 // handle, or the NIC (UNDI) handle because IP4_CONFIG protocol is installed
518 // in the NIC handle.
521 // First, check whether it is the IP4_CONFIG protocol being uninstalled.
522 // IP4_CONFIG protocol is installed on the NIC handle. It isn't necessary
523 // to clean up the default configuration if IP4_CONFIG is being stopped.
525 Status
= gBS
->OpenProtocol (
527 &gEfiIp4ConfigProtocolGuid
,
529 This
->DriverBindingHandle
,
531 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
534 if (Status
== EFI_SUCCESS
) {
536 // Retrieve the IP4 service binding protocol. If failed, it is
537 // likely that Ip4 ServiceBinding is uninstalled already. In this
538 // case, return immediately.
540 Status
= gBS
->OpenProtocol (
542 &gEfiIp4ServiceBindingProtocolGuid
,
543 (VOID
**) &ServiceBinding
,
544 This
->DriverBindingHandle
,
546 EFI_OPEN_PROTOCOL_GET_PROTOCOL
549 if (EFI_ERROR (Status
)) {
553 IpSb
= IP4_SERVICE_FROM_PROTOCOL (ServiceBinding
);
555 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
557 if (IpSb
->Ip4Config
&& (IpSb
->State
!= IP4_SERVICE_DESTORY
)) {
559 IpSb
->Ip4Config
->Stop (IpSb
->Ip4Config
);
561 Status
= gBS
->CloseProtocol (
563 &gEfiIp4ConfigProtocolGuid
,
568 if (EFI_ERROR (Status
)) {
569 NET_RESTORE_TPL (OldTpl
);
574 // If the auto configure hasn't complete, mark it as not started.
576 if (IpSb
->State
== IP4_SERVICE_STARTED
) {
577 IpSb
->State
= IP4_SERVICE_UNSTARTED
;
580 IpSb
->Ip4Config
= NULL
;
581 gBS
->CloseEvent (IpSb
->DoneEvent
);
582 gBS
->CloseEvent (IpSb
->ReconfigEvent
);
585 NET_RESTORE_TPL (OldTpl
);
590 // Either MNP or ARP protocol is being uninstalled. The controller
591 // handle is either the MNP child or ARP child. But, the IP4's
592 // service binding is installed on the NIC handle. So, need to open
593 // the protocol info to find the NIC handle.
595 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiManagedNetworkProtocolGuid
);
597 if (NicHandle
== NULL
) {
598 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiArpProtocolGuid
);
601 if (NicHandle
== NULL
) {
606 // Retrieve the IP4 service binding protocol
608 Status
= gBS
->OpenProtocol (
610 &gEfiIp4ServiceBindingProtocolGuid
,
611 (VOID
**) &ServiceBinding
,
612 This
->DriverBindingHandle
,
614 EFI_OPEN_PROTOCOL_GET_PROTOCOL
617 if (EFI_ERROR (Status
)) {
618 return EFI_DEVICE_ERROR
;
621 IpSb
= IP4_SERVICE_FROM_PROTOCOL (ServiceBinding
);
623 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
625 if (IpSb
->InDestory
) {
626 NET_RESTORE_TPL (OldTpl
);
630 IpSb
->InDestory
= TRUE
;
633 IpSb
->State
= IP4_SERVICE_DESTORY
;
636 // Destory all the children first. If not all children are destoried,
637 // the IP driver can operate correctly, so restore it state. Don't
638 // use NET_LIST_FOR_EACH_SAFE here, because it will cache the next
639 // pointer, which may point to the child that has already been destoried.
640 // For example, if there are two child in the list, the first is UDP
641 // listen child, the send is the MTFTP's child. When Udp child is
642 // destoried, it will destory the MTFTP's child. Then Next point to
645 while (!NetListIsEmpty (&IpSb
->Children
)) {
646 IpInstance
= NET_LIST_HEAD (&IpSb
->Children
, IP4_PROTOCOL
, Link
);
647 Ip4ServiceBindingDestroyChild (ServiceBinding
, IpInstance
->Handle
);
650 if (IpSb
->NumChildren
!= 0) {
652 Status
= EFI_DEVICE_ERROR
;
657 // Clear the variable data.
659 Ip4ClearVariableData (IpSb
);
662 // OK, clean other resources then uninstall the service binding protocol.
664 Status
= Ip4CleanService (IpSb
);
666 if (EFI_ERROR (Status
)) {
670 Status
= gBS
->UninstallProtocolInterface (
672 &gEfiIp4ServiceBindingProtocolGuid
,
676 if (EFI_ERROR (Status
)) {
680 NET_RESTORE_TPL (OldTpl
);
685 IpSb
->InDestory
= FALSE
;
686 NET_RESTORE_TPL (OldTpl
);
692 Creates a child handle with a set of I/O services.
694 @param This Protocol instance pointer.
695 @param ChildHandle Pointer to the handle of the child to create. If
696 it is NULL, then a new handle is created. If it
697 is not NULL, then the I/O services are added to
698 the existing child handle.
700 @retval EFI_SUCCES The child handle was created with the I/O services
701 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
703 @retval other The child handle was not created
708 Ip4ServiceBindingCreateChild (
709 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
710 IN EFI_HANDLE
*ChildHandle
714 IP4_PROTOCOL
*IpInstance
;
719 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
720 return EFI_INVALID_PARAMETER
;
723 IpSb
= IP4_SERVICE_FROM_PROTOCOL (This
);
724 IpInstance
= NetAllocatePool (sizeof (IP4_PROTOCOL
));
726 if (IpInstance
== NULL
) {
727 return EFI_OUT_OF_RESOURCES
;
730 Ip4InitProtocol (IpSb
, IpInstance
);
733 // Install Ip4 onto ChildHandle
735 Status
= gBS
->InstallMultipleProtocolInterfaces (
737 &gEfiIp4ProtocolGuid
,
738 &IpInstance
->Ip4Proto
,
742 if (EFI_ERROR (Status
)) {
746 IpInstance
->Handle
= *ChildHandle
;
749 // Open the Managed Network protocol BY_CHILD.
751 Status
= gBS
->OpenProtocol (
752 IpSb
->MnpChildHandle
,
753 &gEfiManagedNetworkProtocolGuid
,
755 gIp4DriverBinding
.DriverBindingHandle
,
757 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
759 if (EFI_ERROR (Status
)) {
760 gBS
->UninstallMultipleProtocolInterfaces (
762 &gEfiIp4ProtocolGuid
,
763 &IpInstance
->Ip4Proto
,
771 // Insert it into the service binding instance.
773 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
775 NetListInsertTail (&IpSb
->Children
, &IpInstance
->Link
);
778 NET_RESTORE_TPL (OldTpl
);
782 if (EFI_ERROR (Status
)) {
784 Ip4CleanProtocol (IpInstance
);
786 NetFreePool (IpInstance
);
794 Destroys a child handle with a set of I/O services.
796 @param This Protocol instance pointer.
797 @param ChildHandle Handle of the child to destroy
799 @retval EFI_SUCCES The I/O services were removed from the child
801 @retval EFI_UNSUPPORTED The child handle does not support the I/O services
802 that are being removed
803 @retval EFI_INVALID_PARAMETER Child handle is not a valid EFI Handle.
804 @retval EFI_ACCESS_DENIED The child handle could not be destroyed because
805 its I/O services are being used.
806 @retval other The child handle was not destroyed
811 Ip4ServiceBindingDestroyChild (
812 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
813 IN EFI_HANDLE ChildHandle
818 IP4_PROTOCOL
*IpInstance
;
819 EFI_IP4_PROTOCOL
*Ip4
;
823 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
824 return EFI_INVALID_PARAMETER
;
828 // Retrieve the private context data structures
830 IpSb
= IP4_SERVICE_FROM_PROTOCOL (This
);
832 Status
= gBS
->OpenProtocol (
834 &gEfiIp4ProtocolGuid
,
836 gIp4DriverBinding
.DriverBindingHandle
,
838 EFI_OPEN_PROTOCOL_GET_PROTOCOL
841 if (EFI_ERROR (Status
)) {
842 return EFI_UNSUPPORTED
;
845 IpInstance
= IP4_INSTANCE_FROM_PROTOCOL (Ip4
);
847 if (IpInstance
->Service
!= IpSb
) {
848 return EFI_INVALID_PARAMETER
;
851 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
854 // A child can be destoried more than once. For example,
855 // Ip4DriverBindingStop will destory all of its children.
856 // when UDP driver is being stopped, it will destory all
857 // the IP child it opens.
859 if (IpInstance
->State
== IP4_STATE_DESTORY
) {
860 NET_RESTORE_TPL (OldTpl
);
864 State
= IpInstance
->State
;
865 IpInstance
->State
= IP4_STATE_DESTORY
;
868 // Close the Managed Network protocol.
871 IpSb
->MnpChildHandle
,
872 &gEfiManagedNetworkProtocolGuid
,
873 gIp4DriverBinding
.DriverBindingHandle
,
878 // Uninstall the IP4 protocol first. Many thing happens during
880 // 1. The consumer of the IP4 protocol will be stopped if it
881 // opens the protocol BY_DRIVER. For eaxmple, if MNP driver is
882 // stopped, IP driver's stop function will be called, and uninstall
883 // EFI_IP4_PROTOCOL will trigger the UDP's stop function. This
884 // makes it possible to create the network stack bottom up, and
886 // 2. the upper layer will recycle the received packet. The recycle
887 // event's TPL is higher than this function. The recycle events
888 // will be called back before preceeding. If any packets not recycled,
889 // that means there is a resource leak.
891 Status
= gBS
->UninstallProtocolInterface (
893 &gEfiIp4ProtocolGuid
,
894 &IpInstance
->Ip4Proto
897 if (EFI_ERROR (Status
)) {
901 Status
= Ip4CleanProtocol (IpInstance
);
903 Ip4SetVariableData (IpSb
);
905 if (EFI_ERROR (Status
)) {
906 gBS
->InstallMultipleProtocolInterfaces (
908 &gEfiIp4ProtocolGuid
,
916 NetListRemoveEntry (&IpInstance
->Link
);
919 NET_RESTORE_TPL (OldTpl
);
921 NetFreePool (IpInstance
);
925 IpInstance
->State
= State
;
926 NET_RESTORE_TPL (OldTpl
);