4 Copyright (c) 2006 - 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 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
;
58 // Init the servicebinding protocol members.
60 ArpService
->ServiceBinding
.CreateChild
= ArpServiceBindingCreateChild
;
61 ArpService
->ServiceBinding
.DestroyChild
= ArpServiceBindingDestroyChild
;
66 ArpService
->ImageHandle
= ImageHandle
;
67 ArpService
->ControllerHandle
= ControllerHandle
;
70 // Create a MNP child instance.
72 Status
= NetLibCreateServiceChild (
75 &gEfiManagedNetworkServiceBindingProtocolGuid
,
76 &ArpService
->MnpChildHandle
78 if (EFI_ERROR (Status
)) {
83 // Open the MNP protocol.
85 Status
= gBS
->OpenProtocol (
86 ArpService
->MnpChildHandle
,
87 &gEfiManagedNetworkProtocolGuid
,
88 (VOID
**)&ArpService
->Mnp
,
91 EFI_OPEN_PROTOCOL_BY_DRIVER
93 if (EFI_ERROR (Status
)) {
98 // Get the underlayer Snp mode data.
100 Status
= ArpService
->Mnp
->GetModeData (ArpService
->Mnp
, NULL
, &ArpService
->SnpMode
);
101 if ((Status
!= EFI_NOT_STARTED
) && EFI_ERROR (Status
)) {
105 if (ArpService
->SnpMode
.IfType
!= NET_IFTYPE_ETHERNET
) {
107 // Only support the ethernet.
109 Status
= EFI_UNSUPPORTED
;
114 // Set the Mnp config parameters.
116 ArpService
->MnpConfigData
.ReceivedQueueTimeoutValue
= 0;
117 ArpService
->MnpConfigData
.TransmitQueueTimeoutValue
= 0;
118 ArpService
->MnpConfigData
.ProtocolTypeFilter
= ARP_ETHER_PROTO_TYPE
;
119 ArpService
->MnpConfigData
.EnableUnicastReceive
= TRUE
;
120 ArpService
->MnpConfigData
.EnableMulticastReceive
= FALSE
;
121 ArpService
->MnpConfigData
.EnableBroadcastReceive
= TRUE
;
122 ArpService
->MnpConfigData
.EnablePromiscuousReceive
= FALSE
;
123 ArpService
->MnpConfigData
.FlushQueuesOnReset
= TRUE
;
124 ArpService
->MnpConfigData
.EnableReceiveTimestamps
= FALSE
;
125 ArpService
->MnpConfigData
.DisableBackgroundPolling
= FALSE
;
128 // Configure the Mnp child.
130 Status
= ArpService
->Mnp
->Configure (ArpService
->Mnp
, &ArpService
->MnpConfigData
);
131 if (EFI_ERROR (Status
)) {
136 // Create the event used in the RxToken.
138 Status
= gBS
->CreateEvent (
143 &ArpService
->RxToken
.Event
145 if (EFI_ERROR (Status
)) {
150 // Create the Arp heartbeat timer.
152 Status
= gBS
->CreateEvent (
153 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
157 &ArpService
->PeriodicTimer
159 if (EFI_ERROR (Status
)) {
164 // Start the heartbeat timer.
166 Status
= gBS
->SetTimer (
167 ArpService
->PeriodicTimer
,
169 ARP_PERIODIC_TIMER_INTERVAL
171 if (EFI_ERROR (Status
)) {
178 InitializeListHead (&ArpService
->ChildrenList
);
179 InitializeListHead (&ArpService
->PendingRequestTable
);
180 InitializeListHead (&ArpService
->DeniedCacheTable
);
181 InitializeListHead (&ArpService
->ResolvedCacheTable
);
190 Clean the arp service context data.
192 @param[in, out] ArpService Pointer to the buffer containing the arp service
200 IN OUT ARP_SERVICE_DATA
*ArpService
203 NET_CHECK_SIGNATURE (ArpService
, ARP_SERVICE_DATA_SIGNATURE
);
205 if (ArpService
->PeriodicTimer
!= NULL
) {
207 // Cancle and close the PeriodicTimer.
209 gBS
->SetTimer (ArpService
->PeriodicTimer
, TimerCancel
, 0);
210 gBS
->CloseEvent (ArpService
->PeriodicTimer
);
213 if (ArpService
->RxToken
.Event
!= NULL
) {
215 // Cancle the RxToken and close the event in the RxToken.
217 ArpService
->Mnp
->Cancel (ArpService
->Mnp
, NULL
);
218 gBS
->CloseEvent (ArpService
->RxToken
.Event
);
221 if (ArpService
->Mnp
!= NULL
) {
223 // Reset the Mnp child and close the Mnp protocol.
225 ArpService
->Mnp
->Configure (ArpService
->Mnp
, NULL
);
227 ArpService
->MnpChildHandle
,
228 &gEfiManagedNetworkProtocolGuid
,
229 ArpService
->ImageHandle
,
230 ArpService
->ControllerHandle
234 if (ArpService
->MnpChildHandle
!= NULL
) {
236 // Destroy the mnp child.
238 NetLibDestroyServiceChild(
239 ArpService
->ControllerHandle
,
240 ArpService
->ImageHandle
,
241 &gEfiManagedNetworkServiceBindingProtocolGuid
,
242 ArpService
->MnpChildHandle
248 Tests to see if this driver supports a given controller.
250 If a child device is provided, it further tests to see if this driver supports
251 creating a handle for the specified child device.
253 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
254 @param[in] ControllerHandle The handle of the controller to test. This handle
255 must support a protocol interface that supplies
256 an I/O abstraction to the driver.
257 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.
258 This parameter is ignored by device drivers,
259 and is optional for bus drivers.
261 @retval EFI_SUCCESS The device specified by ControllerHandle and
262 RemainingDevicePath is supported by the driver
264 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
265 RemainingDevicePath is already being managed
266 by the driver specified by This.
267 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
268 RemainingDevicePath is already being managed by
269 a different driver or an application that
270 requires exclusive acces. Currently not implemented.
271 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
272 RemainingDevicePath is not supported by the
273 driver specified by This.
278 ArpDriverBindingSupported (
279 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
280 IN EFI_HANDLE ControllerHandle
,
281 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
287 // Test to see if Arp SB is already installed.
289 Status
= gBS
->OpenProtocol (
291 &gEfiArpServiceBindingProtocolGuid
,
293 This
->DriverBindingHandle
,
295 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
297 if (Status
== EFI_SUCCESS
) {
298 return EFI_ALREADY_STARTED
;
302 // Test to see if MNP SB is installed.
304 Status
= gBS
->OpenProtocol (
306 &gEfiManagedNetworkServiceBindingProtocolGuid
,
308 This
->DriverBindingHandle
,
310 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
318 Start this driver on ControllerHandle.
320 The Start() function is designed to be invoked from the EFI boot service ConnectController().
321 As a result, much of the error checking on the parameters to Start() has been
322 moved into this common boot service. It is legal to call Start() from other locations,
323 but the following calling restrictions must be followed or the system behavior
324 will not be deterministic.
325 1. ControllerHandle must be a valid EFI_HANDLE.
326 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally
327 aligned EFI_DEVICE_PATH_PROTOCOL.
328 3. Prior to calling Start(), the Supported() function for the driver specified
329 by This must have been called with the same calling parameters, and Supported()
330 must have returned EFI_SUCCESS.
332 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
333 @param[in] ControllerHandle The handle of the controller to start. This handle
334 must support a protocol interface that supplies
335 an I/O abstraction to the driver.
336 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.
337 This parameter is ignored by device drivers,
338 and is optional for bus drivers.
340 @retval EFI_SUCCESS The device was started.
341 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.
342 Currently not implemented.
343 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of
345 @retval Others The driver failded to start the device.
350 ArpDriverBindingStart (
351 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
352 IN EFI_HANDLE ControllerHandle
,
353 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
357 ARP_SERVICE_DATA
*ArpService
;
360 // Allocate a zero pool for ArpService.
362 ArpService
= AllocateZeroPool (sizeof(ARP_SERVICE_DATA
));
363 if (ArpService
== NULL
) {
364 return EFI_OUT_OF_RESOURCES
;
368 // Initialize the arp service context data.
370 Status
= ArpCreateService (This
->DriverBindingHandle
, ControllerHandle
, ArpService
);
371 if (EFI_ERROR (Status
)) {
376 // Install the ARP service binding protocol.
378 Status
= gBS
->InstallMultipleProtocolInterfaces (
380 &gEfiArpServiceBindingProtocolGuid
,
381 &ArpService
->ServiceBinding
,
384 if (EFI_ERROR (Status
)) {
389 // OK, start to receive arp packets from Mnp.
391 Status
= ArpService
->Mnp
->Receive (ArpService
->Mnp
, &ArpService
->RxToken
);
392 if (EFI_ERROR (Status
)) {
401 // On error, clean the arp service context data, and free the memory allocated.
403 ArpCleanService (ArpService
);
404 FreePool (ArpService
);
411 Stop this driver on ControllerHandle.
413 Release the control of this controller and remove the IScsi functions. The Stop()
414 function is designed to be invoked from the EFI boot service DisconnectController().
415 As a result, much of the error checking on the parameters to Stop() has been moved
416 into this common boot service. It is legal to call Stop() from other locations,
417 but the following calling restrictions must be followed or the system behavior
418 will not be deterministic.
419 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
420 same driver's Start() function.
421 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
422 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
423 Start() function, and the Start() function must have called OpenProtocol() on
424 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
426 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
427 @param[in] ControllerHandle A handle to the device being stopped. The handle must
428 support a bus specific I/O protocol for the driver
429 to use to stop the device.
430 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
432 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
433 if NumberOfChildren is 0.Not used.
435 @retval EFI_SUCCESS The device was stopped.
436 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
441 ArpDriverBindingStop (
442 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
443 IN EFI_HANDLE ControllerHandle
,
444 IN UINTN NumberOfChildren
,
445 IN EFI_HANDLE
*ChildHandleBuffer
449 EFI_HANDLE NicHandle
;
450 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
451 ARP_SERVICE_DATA
*ArpService
;
452 ARP_INSTANCE_DATA
*Instance
;
455 // Get the NicHandle which the arp servicebinding is installed on.
457 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiManagedNetworkProtocolGuid
);
458 if (NicHandle
== NULL
) {
459 return EFI_DEVICE_ERROR
;
463 // Try to get the arp servicebinding protocol on the NicHandle.
465 Status
= gBS
->OpenProtocol (
467 &gEfiArpServiceBindingProtocolGuid
,
468 (VOID
**)&ServiceBinding
,
469 This
->DriverBindingHandle
,
471 EFI_OPEN_PROTOCOL_GET_PROTOCOL
473 if (EFI_ERROR (Status
)) {
474 DEBUG ((EFI_D_ERROR
, "ArpDriverBindingStop: Open ArpSb failed, %r.\n", Status
));
475 return EFI_DEVICE_ERROR
;
478 ArpService
= ARP_SERVICE_DATA_FROM_THIS (ServiceBinding
);
480 if (NumberOfChildren
== 0) {
482 // Uninstall the ARP ServiceBinding protocol.
484 gBS
->UninstallMultipleProtocolInterfaces (
486 &gEfiArpServiceBindingProtocolGuid
,
487 &ArpService
->ServiceBinding
,
492 // Clean the arp servicebinding context data and free the memory allocated.
494 ArpCleanService (ArpService
);
496 FreePool (ArpService
);
499 while (!IsListEmpty (&ArpService
->ChildrenList
)) {
500 Instance
= NET_LIST_HEAD (&ArpService
->ChildrenList
, ARP_INSTANCE_DATA
, List
);
502 ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
505 ASSERT (IsListEmpty (&ArpService
->PendingRequestTable
));
506 ASSERT (IsListEmpty (&ArpService
->DeniedCacheTable
));
507 ASSERT (IsListEmpty (&ArpService
->ResolvedCacheTable
));
514 Creates a child handle and installs a protocol.
516 The CreateChild() function installs a protocol on ChildHandle.
517 If ChildHandle is a pointer to NULL, then a new handle is created and returned
518 in ChildHandle. If ChildHandle is not a pointer to NULL, then the protocol
519 installs on the existing ChildHandle.
521 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
522 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
523 then a new handle is created. If it is a pointer to an existing
524 UEFI handle, then the protocol is added to the existing UEFI handle.
526 @retval EFI_SUCCES The protocol was added to ChildHandle.
527 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
528 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
530 @retval other The child handle was not created
535 ArpServiceBindingCreateChild (
536 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
537 IN EFI_HANDLE
*ChildHandle
541 ARP_SERVICE_DATA
*ArpService
;
542 ARP_INSTANCE_DATA
*Instance
;
546 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
547 return EFI_INVALID_PARAMETER
;
550 ArpService
= ARP_SERVICE_DATA_FROM_THIS (This
);
553 // Allocate memory for the instance context data.
555 Instance
= AllocateZeroPool (sizeof(ARP_INSTANCE_DATA
));
556 if (Instance
== NULL
) {
557 DEBUG ((EFI_D_ERROR
, "ArpSBCreateChild: Failed to allocate memory for Instance.\n"));
559 return EFI_OUT_OF_RESOURCES
;
563 // Init the instance context data.
565 ArpInitInstance (ArpService
, Instance
);
568 // Install the ARP protocol onto the ChildHandle.
570 Status
= gBS
->InstallMultipleProtocolInterfaces (
572 &gEfiArpProtocolGuid
,
573 (VOID
*)&Instance
->ArpProto
,
576 if (EFI_ERROR (Status
)) {
577 DEBUG ((EFI_D_ERROR
, "ArpSBCreateChild: faild to install ARP protocol, %r.\n", Status
));
584 // Save the ChildHandle.
586 Instance
->Handle
= *ChildHandle
;
589 // Open the Managed Network protocol BY_CHILD.
591 Status
= gBS
->OpenProtocol (
592 ArpService
->MnpChildHandle
,
593 &gEfiManagedNetworkProtocolGuid
,
595 gArpDriverBinding
.DriverBindingHandle
,
597 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
599 if (EFI_ERROR (Status
)) {
603 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
606 // Insert the instance into children list managed by the arp service context data.
608 InsertTailList (&ArpService
->ChildrenList
, &Instance
->List
);
609 ArpService
->ChildrenNumber
++;
611 gBS
->RestoreTPL (OldTpl
);
615 if (EFI_ERROR (Status
)) {
618 ArpService
->MnpChildHandle
,
619 &gEfiManagedNetworkProtocolGuid
,
620 gArpDriverBinding
.DriverBindingHandle
,
624 gBS
->UninstallMultipleProtocolInterfaces (
626 &gEfiArpProtocolGuid
,
632 // Free the allocated memory.
642 Destroys a child handle with a protocol installed on it.
644 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
645 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
646 last protocol on ChildHandle, then ChildHandle is destroyed.
648 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
649 @param ChildHandle Handle of the child to destroy
651 @retval EFI_SUCCES The protocol was removed from ChildHandle.
652 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is
654 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.
655 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
656 because its services are being used.
657 @retval other The child handle was not destroyed
662 ArpServiceBindingDestroyChild (
663 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
664 IN EFI_HANDLE ChildHandle
668 ARP_SERVICE_DATA
*ArpService
;
669 ARP_INSTANCE_DATA
*Instance
;
670 EFI_ARP_PROTOCOL
*Arp
;
673 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
674 return EFI_INVALID_PARAMETER
;
677 ArpService
= ARP_SERVICE_DATA_FROM_THIS (This
);
680 // Get the arp protocol.
682 Status
= gBS
->OpenProtocol (
684 &gEfiArpProtocolGuid
,
686 ArpService
->ImageHandle
,
688 EFI_OPEN_PROTOCOL_GET_PROTOCOL
690 if (EFI_ERROR (Status
)) {
691 return EFI_UNSUPPORTED
;
694 Instance
= ARP_INSTANCE_DATA_FROM_THIS (Arp
);
696 if (Instance
->Destroyed
) {
701 // Use the Destroyed as a flag to avoid re-entrance.
703 Instance
->Destroyed
= TRUE
;
706 // Close the Managed Network protocol.
709 ArpService
->MnpChildHandle
,
710 &gEfiManagedNetworkProtocolGuid
,
711 gArpDriverBinding
.DriverBindingHandle
,
716 // Uninstall the ARP protocol.
718 Status
= gBS
->UninstallMultipleProtocolInterfaces (
720 &gEfiArpProtocolGuid
,
724 if (EFI_ERROR (Status
)) {
725 DEBUG ((EFI_D_ERROR
, "ArpSBDestroyChild: Failed to uninstall the arp protocol, %r.\n",
728 Instance
->Destroyed
= FALSE
;
732 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
734 if (Instance
->Configured
) {
736 // Delete the related cache entry.
738 ArpDeleteCacheEntry (Instance
, FALSE
, NULL
, TRUE
);
741 // Reset the instance configuration.
743 ArpConfigureInstance (Instance
, NULL
);
747 // Remove this instance from the ChildrenList.
749 RemoveEntryList (&Instance
->List
);
750 ArpService
->ChildrenNumber
--;
752 gBS
->RestoreTPL (OldTpl
);
760 The entry point for Arp driver which installs the driver binding and component name
761 protocol on its ImageHandle.
763 @param[in] ImageHandle The image handle of the driver.
764 @param[in] SystemTable The system table.
766 @retval EFI_SUCCESS if the driver binding and component name protocols
768 @retval Others Failed to install the protocols.
773 ArpDriverEntryPoint (
774 IN EFI_HANDLE ImageHandle
,
775 IN EFI_SYSTEM_TABLE
*SystemTable
778 return EfiLibInstallDriverBindingComponentName2 (