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
;
211 IpSb
->ActiveEvent
= NULL
;
214 // Create various resources. First create the route table, timer
215 // event and MNP child. IGMP, interface's initialization depend
218 IpSb
->DefaultRouteTable
= Ip4CreateRouteTable ();
220 if (IpSb
->DefaultRouteTable
== NULL
) {
221 Status
= EFI_OUT_OF_RESOURCES
;
225 Status
= gBS
->CreateEvent (
226 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
233 if (EFI_ERROR (Status
)) {
237 Status
= NetLibCreateServiceChild (
240 &gEfiManagedNetworkServiceBindingProtocolGuid
,
241 &IpSb
->MnpChildHandle
244 if (EFI_ERROR (Status
)) {
248 Status
= gBS
->OpenProtocol (
249 IpSb
->MnpChildHandle
,
250 &gEfiManagedNetworkProtocolGuid
,
251 (VOID
**) &IpSb
->Mnp
,
254 EFI_OPEN_PROTOCOL_BY_DRIVER
257 if (EFI_ERROR (Status
)) {
261 Status
= Ip4ServiceConfigMnp (IpSb
, TRUE
);
263 if (EFI_ERROR (Status
)) {
267 Status
= IpSb
->Mnp
->GetModeData (IpSb
->Mnp
, NULL
, &IpSb
->SnpMode
);
269 if (EFI_ERROR (Status
)) {
273 Status
= Ip4InitIgmp (IpSb
);
275 if (EFI_ERROR (Status
)) {
279 IpSb
->DefaultInterface
= Ip4CreateInterface (IpSb
->Mnp
, Controller
, ImageHandle
);
281 if (IpSb
->DefaultInterface
== NULL
) {
282 Status
= EFI_OUT_OF_RESOURCES
;
286 NetListInsertHead (&IpSb
->Interfaces
, &IpSb
->DefaultInterface
->Link
);
288 IpSb
->MacString
= NULL
;
294 Ip4CleanService (IpSb
);
302 Clean up a IP4 service binding instance. It will release all
303 the resource allocated by the instance. The instance may be
304 partly initialized, or partly destoried. If a resource is
305 destoried, it is marked as that in case the destory failed and
306 being called again later.
308 @param IpSb The IP4 serviceing binding instance to clean up
310 @retval EFI_SUCCESS The resource used by the instance are cleaned up
311 @retval Others Failed to clean up some of the resources.
321 if (IpSb
->DefaultInterface
!= NULL
) {
322 Status
= Ip4FreeInterface (IpSb
->DefaultInterface
, NULL
);
324 if (EFI_ERROR (Status
)) {
328 IpSb
->DefaultInterface
= NULL
;
331 if (IpSb
->DefaultRouteTable
!= NULL
) {
332 Ip4FreeRouteTable (IpSb
->DefaultRouteTable
);
333 IpSb
->DefaultRouteTable
= NULL
;
336 Ip4CleanAssembleTable (&IpSb
->Assemble
);
338 if (IpSb
->MnpChildHandle
!= NULL
) {
341 IpSb
->MnpChildHandle
,
342 &gEfiManagedNetworkProtocolGuid
,
350 NetLibDestroyServiceChild (
353 &gEfiManagedNetworkServiceBindingProtocolGuid
,
357 IpSb
->MnpChildHandle
= NULL
;
360 if (IpSb
->Timer
!= NULL
) {
361 gBS
->SetTimer (IpSb
->Timer
, TimerCancel
, 0);
362 gBS
->CloseEvent (IpSb
->Timer
);
367 if (IpSb
->Ip4Config
!= NULL
) {
368 IpSb
->Ip4Config
->Stop (IpSb
->Ip4Config
);
372 &gEfiIp4ConfigProtocolGuid
,
377 gBS
->CloseEvent (IpSb
->DoneEvent
);
378 gBS
->CloseEvent (IpSb
->ReconfigEvent
);
379 IpSb
->ActiveEvent
= NULL
;
380 IpSb
->Ip4Config
= NULL
;
388 Start this driver on ControllerHandle.
390 @param This Protocol instance pointer.
391 @param ControllerHandle Handle of device to bind driver to
392 @param RemainingDevicePath Optional parameter use to pick a specific child
395 @retval EFI_SUCCES This driver is added to ControllerHandle
396 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
397 @retval other This driver does not support this device
402 Ip4DriverBindingStart (
403 IN EFI_DRIVER_BINDING_PROTOCOL
* This
,
404 IN EFI_HANDLE ControllerHandle
,
405 IN EFI_DEVICE_PATH_PROTOCOL
* RemainingDevicePath OPTIONAL
412 // Test for the Ip4 service binding protocol
414 Status
= gBS
->OpenProtocol (
416 &gEfiIp4ServiceBindingProtocolGuid
,
418 This
->DriverBindingHandle
,
420 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
423 if (Status
== EFI_SUCCESS
) {
424 return EFI_ALREADY_STARTED
;
427 Status
= Ip4CreateService (ControllerHandle
, This
->DriverBindingHandle
, &IpSb
);
429 if (EFI_ERROR (Status
)) {
434 // Install the Ip4ServiceBinding Protocol onto ControlerHandle
436 Status
= gBS
->InstallMultipleProtocolInterfaces (
438 &gEfiIp4ServiceBindingProtocolGuid
,
439 &IpSb
->ServiceBinding
,
443 if (EFI_ERROR (Status
)) {
448 // ready to go: start the receiving and timer
450 Status
= Ip4ReceiveFrame (IpSb
->DefaultInterface
, NULL
, Ip4AccpetFrame
, IpSb
);
452 if (EFI_ERROR (Status
)) {
453 goto UNINSTALL_PROTOCOL
;
456 Status
= gBS
->SetTimer (IpSb
->Timer
, TimerPeriodic
, TICKS_PER_SECOND
);
458 if (EFI_ERROR (Status
)) {
459 goto UNINSTALL_PROTOCOL
;
463 // Initialize the IP4 ID
465 mIp4Id
= (UINT16
)NET_RANDOM (NetRandomInitSeed ());
467 Ip4SetVariableData (IpSb
);
472 gBS
->UninstallProtocolInterface (
474 &gEfiIp4ServiceBindingProtocolGuid
,
475 &IpSb
->ServiceBinding
479 Ip4CleanService (IpSb
);
487 Stop this driver on ControllerHandle.
489 @param This Protocol instance pointer.
490 @param ControllerHandle Handle of device to stop driver on
491 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number
492 of children is zero stop the entire bus driver.
493 @param ChildHandleBuffer List of Child Handles to Stop.
495 @retval EFI_SUCCES This driver is removed ControllerHandle
496 @retval other This driver was not removed from this device
501 Ip4DriverBindingStop (
502 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
503 IN EFI_HANDLE ControllerHandle
,
504 IN UINTN NumberOfChildren
,
505 IN EFI_HANDLE
*ChildHandleBuffer
508 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
510 IP4_PROTOCOL
*IpInstance
;
511 EFI_HANDLE NicHandle
;
517 // IP4 driver opens the MNP child, ARP children or the IP4_CONFIG protocol
518 // by driver. So the ControllerHandle may be the MNP child handle, ARP child
519 // handle, or the NIC (UNDI) handle because IP4_CONFIG protocol is installed
520 // in the NIC handle.
523 // First, check whether it is the IP4_CONFIG protocol being uninstalled.
524 // IP4_CONFIG protocol is installed on the NIC handle. It isn't necessary
525 // to clean up the default configuration if IP4_CONFIG is being stopped.
527 Status
= gBS
->OpenProtocol (
529 &gEfiIp4ConfigProtocolGuid
,
531 This
->DriverBindingHandle
,
533 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
536 if (Status
== EFI_SUCCESS
) {
538 // Retrieve the IP4 service binding protocol. If failed, it is
539 // likely that Ip4 ServiceBinding is uninstalled already. In this
540 // case, return immediately.
542 Status
= gBS
->OpenProtocol (
544 &gEfiIp4ServiceBindingProtocolGuid
,
545 (VOID
**) &ServiceBinding
,
546 This
->DriverBindingHandle
,
548 EFI_OPEN_PROTOCOL_GET_PROTOCOL
551 if (EFI_ERROR (Status
)) {
555 IpSb
= IP4_SERVICE_FROM_PROTOCOL (ServiceBinding
);
557 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
559 if (IpSb
->Ip4Config
&& (IpSb
->State
!= IP4_SERVICE_DESTORY
)) {
561 IpSb
->Ip4Config
->Stop (IpSb
->Ip4Config
);
563 Status
= gBS
->CloseProtocol (
565 &gEfiIp4ConfigProtocolGuid
,
570 if (EFI_ERROR (Status
)) {
571 NET_RESTORE_TPL (OldTpl
);
576 // If the auto configure hasn't complete, mark it as not started.
578 if (IpSb
->State
== IP4_SERVICE_STARTED
) {
579 IpSb
->State
= IP4_SERVICE_UNSTARTED
;
582 IpSb
->Ip4Config
= NULL
;
583 gBS
->CloseEvent (IpSb
->DoneEvent
);
584 gBS
->CloseEvent (IpSb
->ReconfigEvent
);
587 NET_RESTORE_TPL (OldTpl
);
592 // Either MNP or ARP protocol is being uninstalled. The controller
593 // handle is either the MNP child or ARP child. But, the IP4's
594 // service binding is installed on the NIC handle. So, need to open
595 // the protocol info to find the NIC handle.
597 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiManagedNetworkProtocolGuid
);
599 if (NicHandle
== NULL
) {
600 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiArpProtocolGuid
);
603 if (NicHandle
== NULL
) {
608 // Retrieve the IP4 service binding protocol
610 Status
= gBS
->OpenProtocol (
612 &gEfiIp4ServiceBindingProtocolGuid
,
613 (VOID
**) &ServiceBinding
,
614 This
->DriverBindingHandle
,
616 EFI_OPEN_PROTOCOL_GET_PROTOCOL
619 if (EFI_ERROR (Status
)) {
620 return EFI_DEVICE_ERROR
;
623 IpSb
= IP4_SERVICE_FROM_PROTOCOL (ServiceBinding
);
625 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
627 if (IpSb
->InDestory
) {
628 NET_RESTORE_TPL (OldTpl
);
632 IpSb
->InDestory
= TRUE
;
635 IpSb
->State
= IP4_SERVICE_DESTORY
;
638 // Destory all the children first. If not all children are destoried,
639 // the IP driver can operate correctly, so restore it state. Don't
640 // use NET_LIST_FOR_EACH_SAFE here, because it will cache the next
641 // pointer, which may point to the child that has already been destoried.
642 // For example, if there are two child in the list, the first is UDP
643 // listen child, the send is the MTFTP's child. When Udp child is
644 // destoried, it will destory the MTFTP's child. Then Next point to
647 while (!NetListIsEmpty (&IpSb
->Children
)) {
648 IpInstance
= NET_LIST_HEAD (&IpSb
->Children
, IP4_PROTOCOL
, Link
);
649 Ip4ServiceBindingDestroyChild (ServiceBinding
, IpInstance
->Handle
);
652 if (IpSb
->NumChildren
!= 0) {
654 Status
= EFI_DEVICE_ERROR
;
659 // Clear the variable data.
661 Ip4ClearVariableData (IpSb
);
664 // OK, clean other resources then uninstall the service binding protocol.
666 Status
= Ip4CleanService (IpSb
);
668 if (EFI_ERROR (Status
)) {
672 Status
= gBS
->UninstallProtocolInterface (
674 &gEfiIp4ServiceBindingProtocolGuid
,
678 if (EFI_ERROR (Status
)) {
682 NET_RESTORE_TPL (OldTpl
);
687 IpSb
->InDestory
= FALSE
;
688 NET_RESTORE_TPL (OldTpl
);
694 Creates a child handle with a set of I/O services.
696 @param This Protocol instance pointer.
697 @param ChildHandle Pointer to the handle of the child to create. If
698 it is NULL, then a new handle is created. If it
699 is not NULL, then the I/O services are added to
700 the existing child handle.
702 @retval EFI_SUCCES The child handle was created with the I/O services
703 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
705 @retval other The child handle was not created
710 Ip4ServiceBindingCreateChild (
711 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
712 IN EFI_HANDLE
*ChildHandle
716 IP4_PROTOCOL
*IpInstance
;
721 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
722 return EFI_INVALID_PARAMETER
;
725 IpSb
= IP4_SERVICE_FROM_PROTOCOL (This
);
726 IpInstance
= NetAllocatePool (sizeof (IP4_PROTOCOL
));
728 if (IpInstance
== NULL
) {
729 return EFI_OUT_OF_RESOURCES
;
732 Ip4InitProtocol (IpSb
, IpInstance
);
735 // Install Ip4 onto ChildHandle
737 Status
= gBS
->InstallMultipleProtocolInterfaces (
739 &gEfiIp4ProtocolGuid
,
740 &IpInstance
->Ip4Proto
,
744 if (EFI_ERROR (Status
)) {
748 IpInstance
->Handle
= *ChildHandle
;
751 // Open the Managed Network protocol BY_CHILD.
753 Status
= gBS
->OpenProtocol (
754 IpSb
->MnpChildHandle
,
755 &gEfiManagedNetworkProtocolGuid
,
757 gIp4DriverBinding
.DriverBindingHandle
,
759 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
761 if (EFI_ERROR (Status
)) {
762 gBS
->UninstallMultipleProtocolInterfaces (
764 &gEfiIp4ProtocolGuid
,
765 &IpInstance
->Ip4Proto
,
773 // Insert it into the service binding instance.
775 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
777 NetListInsertTail (&IpSb
->Children
, &IpInstance
->Link
);
780 NET_RESTORE_TPL (OldTpl
);
784 if (EFI_ERROR (Status
)) {
786 Ip4CleanProtocol (IpInstance
);
788 NetFreePool (IpInstance
);
796 Destroys a child handle with a set of I/O services.
798 @param This Protocol instance pointer.
799 @param ChildHandle Handle of the child to destroy
801 @retval EFI_SUCCES The I/O services were removed from the child
803 @retval EFI_UNSUPPORTED The child handle does not support the I/O services
804 that are being removed
805 @retval EFI_INVALID_PARAMETER Child handle is not a valid EFI Handle.
806 @retval EFI_ACCESS_DENIED The child handle could not be destroyed because
807 its I/O services are being used.
808 @retval other The child handle was not destroyed
813 Ip4ServiceBindingDestroyChild (
814 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
815 IN EFI_HANDLE ChildHandle
820 IP4_PROTOCOL
*IpInstance
;
821 EFI_IP4_PROTOCOL
*Ip4
;
825 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
826 return EFI_INVALID_PARAMETER
;
830 // Retrieve the private context data structures
832 IpSb
= IP4_SERVICE_FROM_PROTOCOL (This
);
834 Status
= gBS
->OpenProtocol (
836 &gEfiIp4ProtocolGuid
,
838 gIp4DriverBinding
.DriverBindingHandle
,
840 EFI_OPEN_PROTOCOL_GET_PROTOCOL
843 if (EFI_ERROR (Status
)) {
844 return EFI_UNSUPPORTED
;
847 IpInstance
= IP4_INSTANCE_FROM_PROTOCOL (Ip4
);
849 if (IpInstance
->Service
!= IpSb
) {
850 return EFI_INVALID_PARAMETER
;
853 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
856 // A child can be destoried more than once. For example,
857 // Ip4DriverBindingStop will destory all of its children.
858 // when UDP driver is being stopped, it will destory all
859 // the IP child it opens.
861 if (IpInstance
->State
== IP4_STATE_DESTORY
) {
862 NET_RESTORE_TPL (OldTpl
);
866 State
= IpInstance
->State
;
867 IpInstance
->State
= IP4_STATE_DESTORY
;
870 // Close the Managed Network protocol.
873 IpSb
->MnpChildHandle
,
874 &gEfiManagedNetworkProtocolGuid
,
875 gIp4DriverBinding
.DriverBindingHandle
,
880 // Uninstall the IP4 protocol first. Many thing happens during
882 // 1. The consumer of the IP4 protocol will be stopped if it
883 // opens the protocol BY_DRIVER. For eaxmple, if MNP driver is
884 // stopped, IP driver's stop function will be called, and uninstall
885 // EFI_IP4_PROTOCOL will trigger the UDP's stop function. This
886 // makes it possible to create the network stack bottom up, and
888 // 2. the upper layer will recycle the received packet. The recycle
889 // event's TPL is higher than this function. The recycle events
890 // will be called back before preceeding. If any packets not recycled,
891 // that means there is a resource leak.
893 Status
= gBS
->UninstallProtocolInterface (
895 &gEfiIp4ProtocolGuid
,
896 &IpInstance
->Ip4Proto
899 if (EFI_ERROR (Status
)) {
903 Status
= Ip4CleanProtocol (IpInstance
);
905 Ip4SetVariableData (IpSb
);
907 if (EFI_ERROR (Status
)) {
908 gBS
->InstallMultipleProtocolInterfaces (
910 &gEfiIp4ProtocolGuid
,
918 NetListRemoveEntry (&IpInstance
->Link
);
921 NET_RESTORE_TPL (OldTpl
);
923 NetFreePool (IpInstance
);
927 IpInstance
->State
= State
;
928 NET_RESTORE_TPL (OldTpl
);