4 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at<BR>
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.
15 #include "ArpDriver.h"
18 EFI_DRIVER_BINDING_PROTOCOL gArpDriverBinding
= {
19 ArpDriverBindingSupported
,
20 ArpDriverBindingStart
,
29 Create and initialize the arp service context data.
31 @param[in] ImageHandle The image handle representing the loaded driver
33 @param[in] ControllerHandle The controller handle the driver binds to.
34 @param[in, out] ArpService Pointer to the buffer containing the arp service
37 @retval EFI_SUCCESS The arp service context is initialized.
39 @retval EFI_UNSUPPORTED The underlayer Snp mode type is not ethernet.
40 Failed to initialize the service context.
41 @retval other Failed to initialize the arp service context.
46 IN EFI_HANDLE ImageHandle
,
47 IN EFI_HANDLE ControllerHandle
,
48 IN OUT ARP_SERVICE_DATA
*ArpService
53 ASSERT (ArpService
!= NULL
);
55 ArpService
->Signature
= ARP_SERVICE_DATA_SIGNATURE
;
60 InitializeListHead (&ArpService
->ChildrenList
);
61 InitializeListHead (&ArpService
->PendingRequestTable
);
62 InitializeListHead (&ArpService
->DeniedCacheTable
);
63 InitializeListHead (&ArpService
->ResolvedCacheTable
);
66 // Init the servicebinding protocol members.
68 ArpService
->ServiceBinding
.CreateChild
= ArpServiceBindingCreateChild
;
69 ArpService
->ServiceBinding
.DestroyChild
= ArpServiceBindingDestroyChild
;
74 ArpService
->ImageHandle
= ImageHandle
;
75 ArpService
->ControllerHandle
= ControllerHandle
;
78 // Create a MNP child instance.
80 Status
= NetLibCreateServiceChild (
83 &gEfiManagedNetworkServiceBindingProtocolGuid
,
84 &ArpService
->MnpChildHandle
86 if (EFI_ERROR (Status
)) {
91 // Open the MNP protocol.
93 Status
= gBS
->OpenProtocol (
94 ArpService
->MnpChildHandle
,
95 &gEfiManagedNetworkProtocolGuid
,
96 (VOID
**)&ArpService
->Mnp
,
99 EFI_OPEN_PROTOCOL_BY_DRIVER
101 if (EFI_ERROR (Status
)) {
106 // Get the underlayer Snp mode data.
108 Status
= ArpService
->Mnp
->GetModeData (ArpService
->Mnp
, NULL
, &ArpService
->SnpMode
);
109 if ((Status
!= EFI_NOT_STARTED
) && EFI_ERROR (Status
)) {
113 if (ArpService
->SnpMode
.IfType
!= NET_IFTYPE_ETHERNET
) {
115 // Only support the ethernet.
117 Status
= EFI_UNSUPPORTED
;
122 // Set the Mnp config parameters.
124 ArpService
->MnpConfigData
.ReceivedQueueTimeoutValue
= 0;
125 ArpService
->MnpConfigData
.TransmitQueueTimeoutValue
= 0;
126 ArpService
->MnpConfigData
.ProtocolTypeFilter
= ARP_ETHER_PROTO_TYPE
;
127 ArpService
->MnpConfigData
.EnableUnicastReceive
= TRUE
;
128 ArpService
->MnpConfigData
.EnableMulticastReceive
= FALSE
;
129 ArpService
->MnpConfigData
.EnableBroadcastReceive
= TRUE
;
130 ArpService
->MnpConfigData
.EnablePromiscuousReceive
= FALSE
;
131 ArpService
->MnpConfigData
.FlushQueuesOnReset
= TRUE
;
132 ArpService
->MnpConfigData
.EnableReceiveTimestamps
= FALSE
;
133 ArpService
->MnpConfigData
.DisableBackgroundPolling
= FALSE
;
136 // Configure the Mnp child.
138 Status
= ArpService
->Mnp
->Configure (ArpService
->Mnp
, &ArpService
->MnpConfigData
);
139 if (EFI_ERROR (Status
)) {
144 // Create the event used in the RxToken.
146 Status
= gBS
->CreateEvent (
151 &ArpService
->RxToken
.Event
153 if (EFI_ERROR (Status
)) {
158 // Create the Arp heartbeat timer.
160 Status
= gBS
->CreateEvent (
161 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
165 &ArpService
->PeriodicTimer
167 if (EFI_ERROR (Status
)) {
172 // Start the heartbeat timer.
174 Status
= gBS
->SetTimer (
175 ArpService
->PeriodicTimer
,
177 ARP_PERIODIC_TIMER_INTERVAL
187 Clean the arp service context data.
189 @param[in, out] ArpService Pointer to the buffer containing the arp service
197 IN OUT ARP_SERVICE_DATA
*ArpService
200 NET_CHECK_SIGNATURE (ArpService
, ARP_SERVICE_DATA_SIGNATURE
);
202 if (ArpService
->PeriodicTimer
!= NULL
) {
204 // Cancle and close the PeriodicTimer.
206 gBS
->SetTimer (ArpService
->PeriodicTimer
, TimerCancel
, 0);
207 gBS
->CloseEvent (ArpService
->PeriodicTimer
);
210 if (ArpService
->RxToken
.Event
!= NULL
) {
212 // Cancle the RxToken and close the event in the RxToken.
214 ArpService
->Mnp
->Cancel (ArpService
->Mnp
, NULL
);
215 gBS
->CloseEvent (ArpService
->RxToken
.Event
);
218 if (ArpService
->Mnp
!= NULL
) {
220 // Reset the Mnp child and close the Mnp protocol.
222 ArpService
->Mnp
->Configure (ArpService
->Mnp
, NULL
);
224 ArpService
->MnpChildHandle
,
225 &gEfiManagedNetworkProtocolGuid
,
226 ArpService
->ImageHandle
,
227 ArpService
->ControllerHandle
231 if (ArpService
->MnpChildHandle
!= NULL
) {
233 // Destroy the mnp child.
235 NetLibDestroyServiceChild(
236 ArpService
->ControllerHandle
,
237 ArpService
->ImageHandle
,
238 &gEfiManagedNetworkServiceBindingProtocolGuid
,
239 ArpService
->MnpChildHandle
245 Callback function which provided by user to remove one node in NetDestroyLinkList process.
247 @param[in] Entry The entry to be removed.
248 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
250 @retval EFI_SUCCESS The entry has been removed successfully.
251 @retval Others Fail to remove the entry.
256 ArpDestroyChildEntryInHandleBuffer (
257 IN LIST_ENTRY
*Entry
,
261 ARP_INSTANCE_DATA
*Instance
;
262 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
264 if (Entry
== NULL
|| Context
== NULL
) {
265 return EFI_INVALID_PARAMETER
;
268 Instance
= NET_LIST_USER_STRUCT_S (Entry
, ARP_INSTANCE_DATA
, List
, ARP_INSTANCE_DATA_SIGNATURE
);
269 ServiceBinding
= (EFI_SERVICE_BINDING_PROTOCOL
*) Context
;
271 return ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
275 Tests to see if this driver supports a given controller.
277 If a child device is provided, it further tests to see if this driver supports
278 creating a handle for the specified child device.
280 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
281 @param[in] ControllerHandle The handle of the controller to test. This handle
282 must support a protocol interface that supplies
283 an I/O abstraction to the driver.
284 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.
285 This parameter is ignored by device drivers,
286 and is optional for bus drivers.
288 @retval EFI_SUCCESS The device specified by ControllerHandle and
289 RemainingDevicePath is supported by the driver
291 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
292 RemainingDevicePath is already being managed
293 by the driver specified by This.
294 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
295 RemainingDevicePath is already being managed by
296 a different driver or an application that
297 requires exclusive acces. Currently not implemented.
298 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
299 RemainingDevicePath is not supported by the
300 driver specified by This.
305 ArpDriverBindingSupported (
306 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
307 IN EFI_HANDLE ControllerHandle
,
308 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
314 // Test to see if Arp SB is already installed.
316 Status
= gBS
->OpenProtocol (
318 &gEfiArpServiceBindingProtocolGuid
,
320 This
->DriverBindingHandle
,
322 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
324 if (Status
== EFI_SUCCESS
) {
325 return EFI_ALREADY_STARTED
;
329 // Test to see if MNP SB is installed.
331 Status
= gBS
->OpenProtocol (
333 &gEfiManagedNetworkServiceBindingProtocolGuid
,
335 This
->DriverBindingHandle
,
337 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
345 Start this driver on ControllerHandle.
347 The Start() function is designed to be invoked from the EFI boot service ConnectController().
348 As a result, much of the error checking on the parameters to Start() has been
349 moved into this common boot service. It is legal to call Start() from other locations,
350 but the following calling restrictions must be followed or the system behavior
351 will not be deterministic.
352 1. ControllerHandle must be a valid EFI_HANDLE.
353 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally
354 aligned EFI_DEVICE_PATH_PROTOCOL.
355 3. Prior to calling Start(), the Supported() function for the driver specified
356 by This must have been called with the same calling parameters, and Supported()
357 must have returned EFI_SUCCESS.
359 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
360 @param[in] ControllerHandle The handle of the controller to start. This handle
361 must support a protocol interface that supplies
362 an I/O abstraction to the driver.
363 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.
364 This parameter is ignored by device drivers,
365 and is optional for bus drivers.
367 @retval EFI_SUCCESS The device was started.
368 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.
369 Currently not implemented.
370 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of
372 @retval Others The driver failded to start the device.
377 ArpDriverBindingStart (
378 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
379 IN EFI_HANDLE ControllerHandle
,
380 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
384 ARP_SERVICE_DATA
*ArpService
;
387 // Allocate a zero pool for ArpService.
389 ArpService
= AllocateZeroPool (sizeof(ARP_SERVICE_DATA
));
390 if (ArpService
== NULL
) {
391 return EFI_OUT_OF_RESOURCES
;
395 // Initialize the arp service context data.
397 Status
= ArpCreateService (This
->DriverBindingHandle
, ControllerHandle
, ArpService
);
398 if (EFI_ERROR (Status
)) {
403 // Install the ARP service binding protocol.
405 Status
= gBS
->InstallMultipleProtocolInterfaces (
407 &gEfiArpServiceBindingProtocolGuid
,
408 &ArpService
->ServiceBinding
,
411 if (EFI_ERROR (Status
)) {
416 // OK, start to receive arp packets from Mnp.
418 Status
= ArpService
->Mnp
->Receive (ArpService
->Mnp
, &ArpService
->RxToken
);
419 if (EFI_ERROR (Status
)) {
428 // On error, clean the arp service context data, and free the memory allocated.
430 ArpCleanService (ArpService
);
431 FreePool (ArpService
);
438 Stop this driver on ControllerHandle.
440 Release the control of this controller and remove the IScsi functions. The Stop()
441 function is designed to be invoked from the EFI boot service DisconnectController().
442 As a result, much of the error checking on the parameters to Stop() has been moved
443 into this common boot service. It is legal to call Stop() from other locations,
444 but the following calling restrictions must be followed or the system behavior
445 will not be deterministic.
446 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
447 same driver's Start() function.
448 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
449 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
450 Start() function, and the Start() function must have called OpenProtocol() on
451 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
453 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
454 @param[in] ControllerHandle A handle to the device being stopped. The handle must
455 support a bus specific I/O protocol for the driver
456 to use to stop the device.
457 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
459 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
460 if NumberOfChildren is 0.Not used.
462 @retval EFI_SUCCESS The device was stopped.
463 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
468 ArpDriverBindingStop (
469 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
470 IN EFI_HANDLE ControllerHandle
,
471 IN UINTN NumberOfChildren
,
472 IN EFI_HANDLE
*ChildHandleBuffer
476 EFI_HANDLE NicHandle
;
477 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
478 ARP_SERVICE_DATA
*ArpService
;
482 // Get the NicHandle which the arp servicebinding is installed on.
484 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiManagedNetworkProtocolGuid
);
485 if (NicHandle
== NULL
) {
490 // Try to get the arp servicebinding protocol on the NicHandle.
492 Status
= gBS
->OpenProtocol (
494 &gEfiArpServiceBindingProtocolGuid
,
495 (VOID
**)&ServiceBinding
,
496 This
->DriverBindingHandle
,
498 EFI_OPEN_PROTOCOL_GET_PROTOCOL
500 if (EFI_ERROR (Status
)) {
501 DEBUG ((EFI_D_ERROR
, "ArpDriverBindingStop: Open ArpSb failed, %r.\n", Status
));
502 return EFI_DEVICE_ERROR
;
505 ArpService
= ARP_SERVICE_DATA_FROM_THIS (ServiceBinding
);
507 if (NumberOfChildren
!= 0) {
509 // NumberOfChildren is not zero, destroy all the ARP children instances.
511 List
= &ArpService
->ChildrenList
;
512 Status
= NetDestroyLinkList (
514 ArpDestroyChildEntryInHandleBuffer
,
518 ASSERT (IsListEmpty (&ArpService
->PendingRequestTable
));
519 ASSERT (IsListEmpty (&ArpService
->DeniedCacheTable
));
520 ASSERT (IsListEmpty (&ArpService
->ResolvedCacheTable
));
521 } else if (IsListEmpty (&ArpService
->ChildrenList
)) {
523 // Uninstall the ARP ServiceBinding protocol.
525 gBS
->UninstallMultipleProtocolInterfaces (
527 &gEfiArpServiceBindingProtocolGuid
,
528 &ArpService
->ServiceBinding
,
533 // Clean the arp servicebinding context data and free the memory allocated.
535 ArpCleanService (ArpService
);
537 FreePool (ArpService
);
544 Creates a child handle and installs a protocol.
546 The CreateChild() function installs a protocol on ChildHandle.
547 If ChildHandle is a pointer to NULL, then a new handle is created and returned
548 in ChildHandle. If ChildHandle is not a pointer to NULL, then the protocol
549 installs on the existing ChildHandle.
551 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
552 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
553 then a new handle is created. If it is a pointer to an existing
554 UEFI handle, then the protocol is added to the existing UEFI handle.
556 @retval EFI_SUCCES The protocol was added to ChildHandle.
557 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
558 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
560 @retval other The child handle was not created
565 ArpServiceBindingCreateChild (
566 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
567 IN EFI_HANDLE
*ChildHandle
571 ARP_SERVICE_DATA
*ArpService
;
572 ARP_INSTANCE_DATA
*Instance
;
576 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
577 return EFI_INVALID_PARAMETER
;
580 ArpService
= ARP_SERVICE_DATA_FROM_THIS (This
);
583 // Allocate memory for the instance context data.
585 Instance
= AllocateZeroPool (sizeof(ARP_INSTANCE_DATA
));
586 if (Instance
== NULL
) {
587 DEBUG ((EFI_D_ERROR
, "ArpSBCreateChild: Failed to allocate memory for Instance.\n"));
589 return EFI_OUT_OF_RESOURCES
;
593 // Init the instance context data.
595 ArpInitInstance (ArpService
, Instance
);
598 // Install the ARP protocol onto the ChildHandle.
600 Status
= gBS
->InstallMultipleProtocolInterfaces (
602 &gEfiArpProtocolGuid
,
603 (VOID
*)&Instance
->ArpProto
,
606 if (EFI_ERROR (Status
)) {
607 DEBUG ((EFI_D_ERROR
, "ArpSBCreateChild: faild to install ARP protocol, %r.\n", Status
));
614 // Save the ChildHandle.
616 Instance
->Handle
= *ChildHandle
;
619 // Open the Managed Network protocol BY_CHILD.
621 Status
= gBS
->OpenProtocol (
622 ArpService
->MnpChildHandle
,
623 &gEfiManagedNetworkProtocolGuid
,
625 gArpDriverBinding
.DriverBindingHandle
,
627 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
629 if (EFI_ERROR (Status
)) {
633 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
636 // Insert the instance into children list managed by the arp service context data.
638 InsertTailList (&ArpService
->ChildrenList
, &Instance
->List
);
639 ArpService
->ChildrenNumber
++;
641 gBS
->RestoreTPL (OldTpl
);
645 if (EFI_ERROR (Status
)) {
648 ArpService
->MnpChildHandle
,
649 &gEfiManagedNetworkProtocolGuid
,
650 gArpDriverBinding
.DriverBindingHandle
,
654 gBS
->UninstallMultipleProtocolInterfaces (
656 &gEfiArpProtocolGuid
,
662 // Free the allocated memory.
672 Destroys a child handle with a protocol installed on it.
674 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
675 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
676 last protocol on ChildHandle, then ChildHandle is destroyed.
678 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
679 @param ChildHandle Handle of the child to destroy
681 @retval EFI_SUCCES The protocol was removed from ChildHandle.
682 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is
684 @retval EFI_INVALID_PARAMETER Child handle is NULL.
685 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
686 because its services are being used.
687 @retval other The child handle was not destroyed
692 ArpServiceBindingDestroyChild (
693 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
694 IN EFI_HANDLE ChildHandle
698 ARP_SERVICE_DATA
*ArpService
;
699 ARP_INSTANCE_DATA
*Instance
;
700 EFI_ARP_PROTOCOL
*Arp
;
703 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
704 return EFI_INVALID_PARAMETER
;
707 ArpService
= ARP_SERVICE_DATA_FROM_THIS (This
);
710 // Get the arp protocol.
712 Status
= gBS
->OpenProtocol (
714 &gEfiArpProtocolGuid
,
716 ArpService
->ImageHandle
,
718 EFI_OPEN_PROTOCOL_GET_PROTOCOL
720 if (EFI_ERROR (Status
)) {
721 return EFI_UNSUPPORTED
;
724 Instance
= ARP_INSTANCE_DATA_FROM_THIS (Arp
);
726 if (Instance
->InDestroy
) {
731 // Use the InDestroy as a flag to avoid re-entrance.
733 Instance
->InDestroy
= TRUE
;
736 // Close the Managed Network protocol.
739 ArpService
->MnpChildHandle
,
740 &gEfiManagedNetworkProtocolGuid
,
741 gArpDriverBinding
.DriverBindingHandle
,
746 // Uninstall the ARP protocol.
748 Status
= gBS
->UninstallMultipleProtocolInterfaces (
750 &gEfiArpProtocolGuid
,
754 if (EFI_ERROR (Status
)) {
755 DEBUG ((EFI_D_ERROR
, "ArpSBDestroyChild: Failed to uninstall the arp protocol, %r.\n",
758 Instance
->InDestroy
= FALSE
;
762 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
764 if (Instance
->Configured
) {
766 // Delete the related cache entry.
768 ArpDeleteCacheEntry (Instance
, FALSE
, NULL
, TRUE
);
771 // Reset the instance configuration.
773 ArpConfigureInstance (Instance
, NULL
);
777 // Remove this instance from the ChildrenList.
779 RemoveEntryList (&Instance
->List
);
780 ArpService
->ChildrenNumber
--;
782 gBS
->RestoreTPL (OldTpl
);
790 The entry point for Arp driver which installs the driver binding and component name
791 protocol on its ImageHandle.
793 @param[in] ImageHandle The image handle of the driver.
794 @param[in] SystemTable The system table.
796 @retval EFI_SUCCESS if the driver binding and component name protocols
798 @retval Others Failed to install the protocols.
803 ArpDriverEntryPoint (
804 IN EFI_HANDLE ImageHandle
,
805 IN EFI_SYSTEM_TABLE
*SystemTable
808 return EfiLibInstallDriverBindingComponentName2 (