3 Copyright (c) 2006 - 2007, 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.
22 #include "ArpDriver.h"
25 EFI_DRIVER_BINDING_PROTOCOL gArpDriverBinding
= {
26 ArpDriverBindingSupported
,
27 ArpDriverBindingStart
,
36 Create and initialize the arp service context data.
38 @param ImageHandle The image handle representing the loaded driver
40 @param ControllerHandle The controller handle the driver binds to.
41 @param ArpService Pointer to the buffer containing the arp service
44 @retval EFI_SUCCESS The arp service context is initialized.
45 @retval other Failed to initialize the arp service context.
51 IN EFI_HANDLE ImageHandle
,
52 IN EFI_HANDLE ControllerHandle
,
53 IN ARP_SERVICE_DATA
*ArpService
58 ASSERT (ArpService
!= NULL
);
60 ArpService
->Signature
= ARP_SERVICE_DATA_SIGNATURE
;
63 // Init the servicebinding protocol members.
65 ArpService
->ServiceBinding
.CreateChild
= ArpServiceBindingCreateChild
;
66 ArpService
->ServiceBinding
.DestroyChild
= ArpServiceBindingDestroyChild
;
71 ArpService
->ImageHandle
= ImageHandle
;
72 ArpService
->ControllerHandle
= ControllerHandle
;
75 // Create a MNP child instance.
77 Status
= NetLibCreateServiceChild (
80 &gEfiManagedNetworkServiceBindingProtocolGuid
,
81 &ArpService
->MnpChildHandle
83 if (EFI_ERROR (Status
)) {
88 // Open the MNP protocol.
90 Status
= gBS
->OpenProtocol (
91 ArpService
->MnpChildHandle
,
92 &gEfiManagedNetworkProtocolGuid
,
93 (VOID
**)&ArpService
->Mnp
,
96 EFI_OPEN_PROTOCOL_BY_DRIVER
98 if (EFI_ERROR (Status
)) {
103 // Get the underlayer Snp mode data.
105 Status
= ArpService
->Mnp
->GetModeData (ArpService
->Mnp
, NULL
, &ArpService
->SnpMode
);
106 if ((Status
!= EFI_NOT_STARTED
) && EFI_ERROR (Status
)) {
110 if (ArpService
->SnpMode
.IfType
!= NET_IFTYPE_ETHERNET
) {
112 // Only support the ethernet.
114 Status
= EFI_UNSUPPORTED
;
119 // Set the Mnp config parameters.
121 ArpService
->MnpConfigData
.ReceivedQueueTimeoutValue
= 0;
122 ArpService
->MnpConfigData
.TransmitQueueTimeoutValue
= 0;
123 ArpService
->MnpConfigData
.ProtocolTypeFilter
= ARP_ETHER_PROTO_TYPE
;
124 ArpService
->MnpConfigData
.EnableUnicastReceive
= TRUE
;
125 ArpService
->MnpConfigData
.EnableMulticastReceive
= FALSE
;
126 ArpService
->MnpConfigData
.EnableBroadcastReceive
= TRUE
;
127 ArpService
->MnpConfigData
.EnablePromiscuousReceive
= FALSE
;
128 ArpService
->MnpConfigData
.FlushQueuesOnReset
= TRUE
;
129 ArpService
->MnpConfigData
.EnableReceiveTimestamps
= FALSE
;
130 ArpService
->MnpConfigData
.DisableBackgroundPolling
= FALSE
;
133 // Configure the Mnp child.
135 Status
= ArpService
->Mnp
->Configure (ArpService
->Mnp
, &ArpService
->MnpConfigData
);
136 if (EFI_ERROR (Status
)) {
141 // Create the event used in the RxToken.
143 Status
= gBS
->CreateEvent (
148 &ArpService
->RxToken
.Event
150 if (EFI_ERROR (Status
)) {
155 // Create the Arp heartbeat timer.
157 Status
= gBS
->CreateEvent (
158 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
162 &ArpService
->PeriodicTimer
164 if (EFI_ERROR (Status
)) {
169 // Start the heartbeat timer.
171 Status
= gBS
->SetTimer (
172 ArpService
->PeriodicTimer
,
174 ARP_PERIODIC_TIMER_INTERVAL
176 if (EFI_ERROR (Status
)) {
183 NetListInit (&ArpService
->ChildrenList
);
184 NetListInit (&ArpService
->PendingRequestTable
);
185 NetListInit (&ArpService
->DeniedCacheTable
);
186 NetListInit (&ArpService
->ResolvedCacheTable
);
195 Clean the arp service context data.
197 @param ArpService Pointer to the buffer containing the arp service
206 IN ARP_SERVICE_DATA
*ArpService
209 NET_CHECK_SIGNATURE (ArpService
, ARP_SERVICE_DATA_SIGNATURE
);
211 if (ArpService
->PeriodicTimer
!= NULL
) {
213 // Cancle and close the PeriodicTimer.
215 gBS
->SetTimer (ArpService
->PeriodicTimer
, TimerCancel
, 0);
216 gBS
->CloseEvent (ArpService
->PeriodicTimer
);
219 if (ArpService
->RxToken
.Event
!= NULL
) {
221 // Cancle the RxToken and close the event in the RxToken.
223 ArpService
->Mnp
->Cancel (ArpService
->Mnp
, NULL
);
224 gBS
->CloseEvent (ArpService
->RxToken
.Event
);
227 if (ArpService
->Mnp
!= NULL
) {
229 // Reset the Mnp child and close the Mnp protocol.
231 ArpService
->Mnp
->Configure (ArpService
->Mnp
, NULL
);
233 ArpService
->MnpChildHandle
,
234 &gEfiManagedNetworkProtocolGuid
,
235 ArpService
->ImageHandle
,
236 ArpService
->ControllerHandle
240 if (ArpService
->MnpChildHandle
!= NULL
) {
242 // Destroy the mnp child.
244 NetLibDestroyServiceChild(
245 ArpService
->ControllerHandle
,
246 ArpService
->ImageHandle
,
247 &gEfiManagedNetworkServiceBindingProtocolGuid
,
248 ArpService
->MnpChildHandle
255 Test to see if this driver supports ControllerHandle.
257 @param This Protocol instance pointer.
258 @param ControllerHandle Handle of device to test.
259 @param RemainingDevicePath Optional parameter use to pick a specific child
262 @retval EFI_SUCCES This driver supports this device
263 @retval EFI_ALREADY_STARTED This driver is already running on this device.
264 @retval other This driver does not support this device.
269 ArpDriverBindingSupported (
270 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
271 IN EFI_HANDLE ControllerHandle
,
272 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
278 // Test to see if Arp SB is already installed.
280 Status
= gBS
->OpenProtocol (
282 &gEfiArpServiceBindingProtocolGuid
,
284 This
->DriverBindingHandle
,
286 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
288 if (Status
== EFI_SUCCESS
) {
289 return EFI_ALREADY_STARTED
;
293 // Test to see if MNP SB is installed.
295 Status
= gBS
->OpenProtocol (
297 &gEfiManagedNetworkServiceBindingProtocolGuid
,
299 This
->DriverBindingHandle
,
301 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
309 Start this driver on ControllerHandle.
311 @param This Protocol instance pointer.
312 @param ControllerHandle Handle of device to bind driver to
313 @param RemainingDevicePath Optional parameter use to pick a specific child
316 @retval EFI_SUCCES This driver is added to ControllerHandle
317 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
318 @retval other This driver does not support this device
323 ArpDriverBindingStart (
324 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
325 IN EFI_HANDLE ControllerHandle
,
326 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
330 ARP_SERVICE_DATA
*ArpService
;
333 // Allocate a zero pool for ArpService.
335 ArpService
= NetAllocateZeroPool (sizeof(ARP_SERVICE_DATA
));
336 if (ArpService
== NULL
) {
337 return EFI_OUT_OF_RESOURCES
;
341 // Initialize the arp service context data.
343 Status
= ArpCreateService (This
->DriverBindingHandle
, ControllerHandle
, ArpService
);
344 if (EFI_ERROR (Status
)) {
349 // Install the ARP service binding protocol.
351 Status
= gBS
->InstallMultipleProtocolInterfaces (
353 &gEfiArpServiceBindingProtocolGuid
,
354 &ArpService
->ServiceBinding
,
357 if (EFI_ERROR (Status
)) {
362 // OK, start to receive arp packets from Mnp.
364 Status
= ArpService
->Mnp
->Receive (ArpService
->Mnp
, &ArpService
->RxToken
);
365 if (EFI_ERROR (Status
)) {
374 // On error, clean the arp service context data, and free the memory allocated.
376 ArpCleanService (ArpService
);
377 NetFreePool (ArpService
);
384 Stop this driver on ControllerHandle.
386 @param This Protocol instance pointer.
387 @param ControllerHandle Handle of device to stop driver on
388 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number
389 of children is zero stop the entire bus driver.
390 @param ChildHandleBuffer List of Child Handles to Stop.
392 @retval EFI_SUCCES This driver is removed ControllerHandle
393 @retval other This driver was not removed from this device
398 ArpDriverBindingStop (
399 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
400 IN EFI_HANDLE ControllerHandle
,
401 IN UINTN NumberOfChildren
,
402 IN EFI_HANDLE
*ChildHandleBuffer
406 EFI_HANDLE NicHandle
;
407 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
408 ARP_SERVICE_DATA
*ArpService
;
409 ARP_INSTANCE_DATA
*Instance
;
412 // Get the NicHandle which the arp servicebinding is installed on.
414 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiManagedNetworkProtocolGuid
);
415 if (NicHandle
== NULL
) {
420 // Try to get the arp servicebinding protocol on the NicHandle.
422 Status
= gBS
->OpenProtocol (
424 &gEfiArpServiceBindingProtocolGuid
,
425 (VOID
**)&ServiceBinding
,
426 This
->DriverBindingHandle
,
428 EFI_OPEN_PROTOCOL_GET_PROTOCOL
430 if (EFI_ERROR (Status
)) {
431 ARP_DEBUG_ERROR (("ArpDriverBindingStop: Open ArpSb failed, %r.\n", Status
));
435 ArpService
= ARP_SERVICE_DATA_FROM_THIS (ServiceBinding
);
437 while (!NetListIsEmpty (&ArpService
->ChildrenList
)) {
439 // Iterate all the instances.
441 Instance
= NET_LIST_HEAD (&ArpService
->ChildrenList
, ARP_INSTANCE_DATA
, List
);
444 // Destroy this arp child.
446 ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
449 ASSERT (NetListIsEmpty (&ArpService
->PendingRequestTable
));
450 ASSERT (NetListIsEmpty (&ArpService
->DeniedCacheTable
));
451 ASSERT (NetListIsEmpty (&ArpService
->ResolvedCacheTable
));
454 // Uninstall the ARP ServiceBinding protocol.
456 Status
= gBS
->UninstallMultipleProtocolInterfaces (
458 &gEfiArpServiceBindingProtocolGuid
,
459 &ArpService
->ServiceBinding
,
462 if (EFI_ERROR (Status
)) {
463 ARP_DEBUG_ERROR (("ArpDriverBindingStop: Failed to uninstall ArpSb, %r.\n", Status
));
468 // Clean the arp servicebinding context data and free the memory allocated.
470 ArpCleanService (ArpService
);
471 NetFreePool (ArpService
);
478 Creates a child handle with a set of I/O services.
480 @param This Protocol instance pointer.
481 @param ChildHandle Pointer to the handle of the child to create. If
482 it is NULL, then a new handle is created. If it is
483 not NULL, then the I/O services are added to the
484 existing child handle.
486 @retval EFI_SUCCES The child handle was created with the I/O
488 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
490 @retval other The child handle was not created.
495 ArpServiceBindingCreateChild (
496 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
497 IN EFI_HANDLE
*ChildHandle
501 ARP_SERVICE_DATA
*ArpService
;
502 ARP_INSTANCE_DATA
*Instance
;
506 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
507 return EFI_INVALID_PARAMETER
;
510 ArpService
= ARP_SERVICE_DATA_FROM_THIS (This
);
513 // Allocate memory for the instance context data.
515 Instance
= NetAllocateZeroPool (sizeof(ARP_INSTANCE_DATA
));
516 if (Instance
== NULL
) {
517 ARP_DEBUG_ERROR (("ArpSBCreateChild: Failed to allocate memory for Instance.\n"));
519 return EFI_OUT_OF_RESOURCES
;
523 // Init the instance context data.
525 ArpInitInstance (ArpService
, Instance
);
528 // Install the ARP protocol onto the ChildHandle.
530 Status
= gBS
->InstallMultipleProtocolInterfaces (
532 &gEfiArpProtocolGuid
,
533 (VOID
*)&Instance
->ArpProto
,
536 if (EFI_ERROR (Status
)) {
537 ARP_DEBUG_ERROR (("ArpSBCreateChild: faild to install ARP protocol, %r.\n", Status
));
539 NetFreePool (Instance
);
544 // Save the ChildHandle.
546 Instance
->Handle
= *ChildHandle
;
549 // Open the Managed Network protocol BY_CHILD.
551 Status
= gBS
->OpenProtocol (
552 ArpService
->MnpChildHandle
,
553 &gEfiManagedNetworkProtocolGuid
,
555 gArpDriverBinding
.DriverBindingHandle
,
557 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
559 if (EFI_ERROR (Status
)) {
563 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
566 // Insert the instance into children list managed by the arp service context data.
568 NetListInsertTail (&ArpService
->ChildrenList
, &Instance
->List
);
569 ArpService
->ChildrenNumber
++;
571 NET_RESTORE_TPL (OldTpl
);
575 if (EFI_ERROR (Status
)) {
578 ArpService
->MnpChildHandle
,
579 &gEfiManagedNetworkProtocolGuid
,
580 gArpDriverBinding
.DriverBindingHandle
,
584 gBS
->UninstallMultipleProtocolInterfaces (
586 &gEfiArpProtocolGuid
,
592 // Free the allocated memory.
594 NetFreePool (Instance
);
602 Destroys a child handle with a set of I/O services.
604 @param This Protocol instance pointer.
605 @param ChildHandle Handle of the child to destroy.
607 @retval EFI_SUCCES The I/O services were removed from the child
609 @retval EFI_UNSUPPORTED The child handle does not support the I/O services
610 that are being removed.
611 @retval EFI_INVALID_PARAMETER Child handle is not a valid EFI Handle.
612 @retval EFI_ACCESS_DENIED The child handle could not be destroyed because
613 its I/O services are being used.
614 @retval other The child handle was not destroyed.
619 ArpServiceBindingDestroyChild (
620 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
621 IN EFI_HANDLE ChildHandle
625 ARP_SERVICE_DATA
*ArpService
;
626 ARP_INSTANCE_DATA
*Instance
;
627 EFI_ARP_PROTOCOL
*Arp
;
630 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
631 return EFI_INVALID_PARAMETER
;
634 ArpService
= ARP_SERVICE_DATA_FROM_THIS (This
);
637 // Get the arp protocol.
639 Status
= gBS
->OpenProtocol (
641 &gEfiArpProtocolGuid
,
643 ArpService
->ImageHandle
,
645 EFI_OPEN_PROTOCOL_GET_PROTOCOL
647 if (EFI_ERROR (Status
)) {
648 return EFI_UNSUPPORTED
;
651 Instance
= ARP_INSTANCE_DATA_FROM_THIS (Arp
);
653 if (Instance
->Destroyed
) {
658 // Use the Destroyed as a flag to avoid re-entrance.
660 Instance
->Destroyed
= TRUE
;
663 // Close the Managed Network protocol.
666 ArpService
->MnpChildHandle
,
667 &gEfiManagedNetworkProtocolGuid
,
668 gArpDriverBinding
.DriverBindingHandle
,
673 // Uninstall the ARP protocol.
675 Status
= gBS
->UninstallMultipleProtocolInterfaces (
677 &gEfiArpProtocolGuid
,
681 if (EFI_ERROR (Status
)) {
682 ARP_DEBUG_ERROR (("ArpSBDestroyChild: Failed to uninstall the arp protocol, %r.\n",
685 Instance
->Destroyed
= FALSE
;
689 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
691 if (Instance
->Configured
) {
693 // Delete the related cache entry.
695 ArpDeleteCacheEntry (Instance
, FALSE
, NULL
, TRUE
);
698 // Reset the instance configuration.
700 ArpConfigureInstance (Instance
, NULL
);
704 // Remove this instance from the ChildrenList.
706 NetListRemoveEntry (&Instance
->List
);
707 ArpService
->ChildrenNumber
--;
709 NET_RESTORE_TPL (OldTpl
);
711 NetFreePool (Instance
);
719 ArpDriverEntryPoint (
720 IN EFI_HANDLE ImageHandle
,
721 IN EFI_SYSTEM_TABLE
*SystemTable
727 The entry point for Arp driver which installs the driver binding and component name
728 protocol on its ImageHandle.
732 ImageHandle - The image handle of the driver.
733 SystemTable - The system table.
737 EFI_SUCCESS - if the driver binding and component name protocols are successfully
738 installed, otherwise if failed.
742 return EfiLibInstallDriverBindingComponentName2 (