4 Copyright (c) 2006 - 2010, 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 Tests to see if this driver supports a given controller.
247 If a child device is provided, it further tests to see if this driver supports
248 creating a handle for the specified child device.
250 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
251 @param[in] ControllerHandle The handle of the controller to test. This handle
252 must support a protocol interface that supplies
253 an I/O abstraction to the driver.
254 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.
255 This parameter is ignored by device drivers,
256 and is optional for bus drivers.
258 @retval EFI_SUCCESS The device specified by ControllerHandle and
259 RemainingDevicePath is supported by the driver
261 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
262 RemainingDevicePath is already being managed
263 by the driver specified by This.
264 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
265 RemainingDevicePath is already being managed by
266 a different driver or an application that
267 requires exclusive acces. Currently not implemented.
268 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
269 RemainingDevicePath is not supported by the
270 driver specified by This.
275 ArpDriverBindingSupported (
276 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
277 IN EFI_HANDLE ControllerHandle
,
278 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
284 // Test to see if Arp SB is already installed.
286 Status
= gBS
->OpenProtocol (
288 &gEfiArpServiceBindingProtocolGuid
,
290 This
->DriverBindingHandle
,
292 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
294 if (Status
== EFI_SUCCESS
) {
295 return EFI_ALREADY_STARTED
;
299 // Test to see if MNP SB is installed.
301 Status
= gBS
->OpenProtocol (
303 &gEfiManagedNetworkServiceBindingProtocolGuid
,
305 This
->DriverBindingHandle
,
307 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
315 Start this driver on ControllerHandle.
317 The Start() function is designed to be invoked from the EFI boot service ConnectController().
318 As a result, much of the error checking on the parameters to Start() has been
319 moved into this common boot service. It is legal to call Start() from other locations,
320 but the following calling restrictions must be followed or the system behavior
321 will not be deterministic.
322 1. ControllerHandle must be a valid EFI_HANDLE.
323 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally
324 aligned EFI_DEVICE_PATH_PROTOCOL.
325 3. Prior to calling Start(), the Supported() function for the driver specified
326 by This must have been called with the same calling parameters, and Supported()
327 must have returned EFI_SUCCESS.
329 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
330 @param[in] ControllerHandle The handle of the controller to start. This handle
331 must support a protocol interface that supplies
332 an I/O abstraction to the driver.
333 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.
334 This parameter is ignored by device drivers,
335 and is optional for bus drivers.
337 @retval EFI_SUCCESS The device was started.
338 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.
339 Currently not implemented.
340 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of
342 @retval Others The driver failded to start the device.
347 ArpDriverBindingStart (
348 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
349 IN EFI_HANDLE ControllerHandle
,
350 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
354 ARP_SERVICE_DATA
*ArpService
;
357 // Allocate a zero pool for ArpService.
359 ArpService
= AllocateZeroPool (sizeof(ARP_SERVICE_DATA
));
360 if (ArpService
== NULL
) {
361 return EFI_OUT_OF_RESOURCES
;
365 // Initialize the arp service context data.
367 Status
= ArpCreateService (This
->DriverBindingHandle
, ControllerHandle
, ArpService
);
368 if (EFI_ERROR (Status
)) {
373 // Install the ARP service binding protocol.
375 Status
= gBS
->InstallMultipleProtocolInterfaces (
377 &gEfiArpServiceBindingProtocolGuid
,
378 &ArpService
->ServiceBinding
,
381 if (EFI_ERROR (Status
)) {
386 // OK, start to receive arp packets from Mnp.
388 Status
= ArpService
->Mnp
->Receive (ArpService
->Mnp
, &ArpService
->RxToken
);
389 if (EFI_ERROR (Status
)) {
398 // On error, clean the arp service context data, and free the memory allocated.
400 ArpCleanService (ArpService
);
401 FreePool (ArpService
);
408 Stop this driver on ControllerHandle.
410 Release the control of this controller and remove the IScsi functions. The Stop()
411 function is designed to be invoked from the EFI boot service DisconnectController().
412 As a result, much of the error checking on the parameters to Stop() has been moved
413 into this common boot service. It is legal to call Stop() from other locations,
414 but the following calling restrictions must be followed or the system behavior
415 will not be deterministic.
416 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
417 same driver's Start() function.
418 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
419 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
420 Start() function, and the Start() function must have called OpenProtocol() on
421 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
423 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
424 @param[in] ControllerHandle A handle to the device being stopped. The handle must
425 support a bus specific I/O protocol for the driver
426 to use to stop the device.
427 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
429 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
430 if NumberOfChildren is 0.Not used.
432 @retval EFI_SUCCESS The device was stopped.
433 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
438 ArpDriverBindingStop (
439 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
440 IN EFI_HANDLE ControllerHandle
,
441 IN UINTN NumberOfChildren
,
442 IN EFI_HANDLE
*ChildHandleBuffer
446 EFI_HANDLE NicHandle
;
447 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
448 ARP_SERVICE_DATA
*ArpService
;
449 ARP_INSTANCE_DATA
*Instance
;
452 // Get the NicHandle which the arp servicebinding is installed on.
454 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiManagedNetworkProtocolGuid
);
455 if (NicHandle
== NULL
) {
456 return EFI_DEVICE_ERROR
;
460 // Try to get the arp servicebinding protocol on the NicHandle.
462 Status
= gBS
->OpenProtocol (
464 &gEfiArpServiceBindingProtocolGuid
,
465 (VOID
**)&ServiceBinding
,
466 This
->DriverBindingHandle
,
468 EFI_OPEN_PROTOCOL_GET_PROTOCOL
470 if (EFI_ERROR (Status
)) {
471 DEBUG ((EFI_D_ERROR
, "ArpDriverBindingStop: Open ArpSb failed, %r.\n", Status
));
472 return EFI_DEVICE_ERROR
;
475 ArpService
= ARP_SERVICE_DATA_FROM_THIS (ServiceBinding
);
477 if (NumberOfChildren
== 0) {
479 // Uninstall the ARP ServiceBinding protocol.
481 gBS
->UninstallMultipleProtocolInterfaces (
483 &gEfiArpServiceBindingProtocolGuid
,
484 &ArpService
->ServiceBinding
,
489 // Clean the arp servicebinding context data and free the memory allocated.
491 ArpCleanService (ArpService
);
493 FreePool (ArpService
);
496 while (!IsListEmpty (&ArpService
->ChildrenList
)) {
497 Instance
= NET_LIST_HEAD (&ArpService
->ChildrenList
, ARP_INSTANCE_DATA
, List
);
499 ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
502 ASSERT (IsListEmpty (&ArpService
->PendingRequestTable
));
503 ASSERT (IsListEmpty (&ArpService
->DeniedCacheTable
));
504 ASSERT (IsListEmpty (&ArpService
->ResolvedCacheTable
));
511 Creates a child handle and installs a protocol.
513 The CreateChild() function installs a protocol on ChildHandle.
514 If ChildHandle is a pointer to NULL, then a new handle is created and returned
515 in ChildHandle. If ChildHandle is not a pointer to NULL, then the protocol
516 installs on the existing ChildHandle.
518 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
519 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
520 then a new handle is created. If it is a pointer to an existing
521 UEFI handle, then the protocol is added to the existing UEFI handle.
523 @retval EFI_SUCCES The protocol was added to ChildHandle.
524 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
525 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
527 @retval other The child handle was not created
532 ArpServiceBindingCreateChild (
533 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
534 IN EFI_HANDLE
*ChildHandle
538 ARP_SERVICE_DATA
*ArpService
;
539 ARP_INSTANCE_DATA
*Instance
;
543 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
544 return EFI_INVALID_PARAMETER
;
547 ArpService
= ARP_SERVICE_DATA_FROM_THIS (This
);
550 // Allocate memory for the instance context data.
552 Instance
= AllocateZeroPool (sizeof(ARP_INSTANCE_DATA
));
553 if (Instance
== NULL
) {
554 DEBUG ((EFI_D_ERROR
, "ArpSBCreateChild: Failed to allocate memory for Instance.\n"));
556 return EFI_OUT_OF_RESOURCES
;
560 // Init the instance context data.
562 ArpInitInstance (ArpService
, Instance
);
565 // Install the ARP protocol onto the ChildHandle.
567 Status
= gBS
->InstallMultipleProtocolInterfaces (
569 &gEfiArpProtocolGuid
,
570 (VOID
*)&Instance
->ArpProto
,
573 if (EFI_ERROR (Status
)) {
574 DEBUG ((EFI_D_ERROR
, "ArpSBCreateChild: faild to install ARP protocol, %r.\n", Status
));
581 // Save the ChildHandle.
583 Instance
->Handle
= *ChildHandle
;
586 // Open the Managed Network protocol BY_CHILD.
588 Status
= gBS
->OpenProtocol (
589 ArpService
->MnpChildHandle
,
590 &gEfiManagedNetworkProtocolGuid
,
592 gArpDriverBinding
.DriverBindingHandle
,
594 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
596 if (EFI_ERROR (Status
)) {
600 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
603 // Insert the instance into children list managed by the arp service context data.
605 InsertTailList (&ArpService
->ChildrenList
, &Instance
->List
);
606 ArpService
->ChildrenNumber
++;
608 gBS
->RestoreTPL (OldTpl
);
612 if (EFI_ERROR (Status
)) {
615 ArpService
->MnpChildHandle
,
616 &gEfiManagedNetworkProtocolGuid
,
617 gArpDriverBinding
.DriverBindingHandle
,
621 gBS
->UninstallMultipleProtocolInterfaces (
623 &gEfiArpProtocolGuid
,
629 // Free the allocated memory.
639 Destroys a child handle with a protocol installed on it.
641 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
642 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
643 last protocol on ChildHandle, then ChildHandle is destroyed.
645 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
646 @param ChildHandle Handle of the child to destroy
648 @retval EFI_SUCCES The protocol was removed from ChildHandle.
649 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is
651 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.
652 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
653 because its services are being used.
654 @retval other The child handle was not destroyed
659 ArpServiceBindingDestroyChild (
660 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
661 IN EFI_HANDLE ChildHandle
665 ARP_SERVICE_DATA
*ArpService
;
666 ARP_INSTANCE_DATA
*Instance
;
667 EFI_ARP_PROTOCOL
*Arp
;
670 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
671 return EFI_INVALID_PARAMETER
;
674 ArpService
= ARP_SERVICE_DATA_FROM_THIS (This
);
677 // Get the arp protocol.
679 Status
= gBS
->OpenProtocol (
681 &gEfiArpProtocolGuid
,
683 ArpService
->ImageHandle
,
685 EFI_OPEN_PROTOCOL_GET_PROTOCOL
687 if (EFI_ERROR (Status
)) {
688 return EFI_UNSUPPORTED
;
691 Instance
= ARP_INSTANCE_DATA_FROM_THIS (Arp
);
693 if (Instance
->Destroyed
) {
698 // Use the Destroyed as a flag to avoid re-entrance.
700 Instance
->Destroyed
= TRUE
;
703 // Close the Managed Network protocol.
706 ArpService
->MnpChildHandle
,
707 &gEfiManagedNetworkProtocolGuid
,
708 gArpDriverBinding
.DriverBindingHandle
,
713 // Uninstall the ARP protocol.
715 Status
= gBS
->UninstallMultipleProtocolInterfaces (
717 &gEfiArpProtocolGuid
,
721 if (EFI_ERROR (Status
)) {
722 DEBUG ((EFI_D_ERROR
, "ArpSBDestroyChild: Failed to uninstall the arp protocol, %r.\n",
725 Instance
->Destroyed
= FALSE
;
729 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
731 if (Instance
->Configured
) {
733 // Delete the related cache entry.
735 ArpDeleteCacheEntry (Instance
, FALSE
, NULL
, TRUE
);
738 // Reset the instance configuration.
740 ArpConfigureInstance (Instance
, NULL
);
744 // Remove this instance from the ChildrenList.
746 RemoveEntryList (&Instance
->List
);
747 ArpService
->ChildrenNumber
--;
749 gBS
->RestoreTPL (OldTpl
);
757 The entry point for Arp driver which installs the driver binding and component name
758 protocol on its ImageHandle.
760 @param[in] ImageHandle The image handle of the driver.
761 @param[in] SystemTable The system table.
763 @retval EFI_SUCCESS if the driver binding and component name protocols
765 @retval Others Failed to install the protocols.
770 ArpDriverEntryPoint (
771 IN EFI_HANDLE ImageHandle
,
772 IN EFI_SYSTEM_TABLE
*SystemTable
775 return EfiLibInstallDriverBindingComponentName2 (