3 Copyright (c) 2005 - 2008, 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.
21 #include "MnpDriver.h"
25 EFI_DRIVER_BINDING_PROTOCOL gMnpDriverBinding
= {
26 MnpDriverBindingSupported
,
27 MnpDriverBindingStart
,
36 Test to see if this driver supports ControllerHandle.
38 @param This Protocol instance pointer.
39 @param ControllerHandle Handle of device to test.
40 @param RemainingDevicePath Optional parameter use to pick a specific child
43 @retval EFI_SUCCES This driver supports this device.
44 @retval EFI_ALREADY_STARTED This driver is already running on this device.
45 @retval other This driver does not support this device.
50 MnpDriverBindingSupported (
51 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
52 IN EFI_HANDLE ControllerHandle
,
53 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
57 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
60 // Test to see if MNP is already installed.
62 Status
= gBS
->OpenProtocol (
64 &gEfiManagedNetworkServiceBindingProtocolGuid
,
66 This
->DriverBindingHandle
,
68 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
70 if (!EFI_ERROR (Status
)) {
71 return EFI_ALREADY_STARTED
;
75 // Test to open the Simple Network protocol BY_DRIVER.
77 Status
= gBS
->OpenProtocol (
79 &gEfiSimpleNetworkProtocolGuid
,
81 This
->DriverBindingHandle
,
83 EFI_OPEN_PROTOCOL_BY_DRIVER
86 if (EFI_ERROR (Status
)) {
91 // Close the openned SNP protocol.
95 &gEfiSimpleNetworkProtocolGuid
,
96 This
->DriverBindingHandle
,
105 Start this driver on ControllerHandle.
107 @param This Protocol instance pointer.
108 @param ControllerHandle Handle of device to bind driver to.
109 @param RemainingDevicePath Optional parameter use to pick a specific child
112 @retval EFI_SUCCES This driver is added to ControllerHandle.
113 @retval EFI_ALREADY_STARTED This driver is already running on
115 @retval other This driver does not support this device.
120 MnpDriverBindingStart (
121 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
122 IN EFI_HANDLE ControllerHandle
,
123 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
127 MNP_SERVICE_DATA
*MnpServiceData
;
128 BOOLEAN MnpInitialized
;
130 MnpInitialized
= FALSE
;
132 MnpServiceData
= AllocateZeroPool (sizeof (MNP_SERVICE_DATA
));
133 if (MnpServiceData
== NULL
) {
134 DEBUG ((EFI_D_ERROR
, "MnpDriverBindingStart(): Failed to allocate the Mnp Service Data.\n"));
136 return EFI_OUT_OF_RESOURCES
;
140 // Initialize the Mnp Service Data.
142 Status
= MnpInitializeServiceData (MnpServiceData
, This
->DriverBindingHandle
, ControllerHandle
);
143 if (EFI_ERROR (Status
)) {
145 DEBUG ((EFI_D_ERROR
, "MnpDriverBindingStart: MnpInitializeServiceData failed, %r.\n",Status
));
149 MnpInitialized
= TRUE
;
152 // Install the MNP Service Binding Protocol.
154 Status
= gBS
->InstallMultipleProtocolInterfaces (
156 &gEfiManagedNetworkServiceBindingProtocolGuid
,
157 &MnpServiceData
->ServiceBinding
,
163 if (EFI_ERROR (Status
)) {
165 if (MnpInitialized
) {
167 // Flush the Mnp Service Data.
169 MnpFlushServiceData (MnpServiceData
, This
->DriverBindingHandle
);
172 gBS
->FreePool (MnpServiceData
);
180 Stop this driver on ControllerHandle.
182 @param This Protocol instance pointer.
183 @param ControllerHandle Handle of device to stop driver on.
184 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number
185 of children is zero stop the entire bus driver.
186 @param ChildHandleBuffer List of Child Handles to Stop.
188 @retval EFI_SUCCES This driver is removed ControllerHandle.
189 @retval other This driver was not removed from this device.
194 MnpDriverBindingStop (
195 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
196 IN EFI_HANDLE ControllerHandle
,
197 IN UINTN NumberOfChildren
,
198 IN EFI_HANDLE
*ChildHandleBuffer
202 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
203 MNP_SERVICE_DATA
*MnpServiceData
;
204 MNP_INSTANCE_DATA
*Instance
;
207 // Retrieve the MNP service binding protocol from the ControllerHandle.
209 Status
= gBS
->OpenProtocol (
211 &gEfiManagedNetworkServiceBindingProtocolGuid
,
212 (VOID
**) &ServiceBinding
,
213 This
->DriverBindingHandle
,
215 EFI_OPEN_PROTOCOL_GET_PROTOCOL
217 if (EFI_ERROR (Status
)) {
221 "MnpDriverBindingStop: Locate MNP Service Binding Protocol failed, %r.\n",
224 return EFI_DEVICE_ERROR
;
227 MnpServiceData
= MNP_SERVICE_DATA_FROM_THIS (ServiceBinding
);
229 if (NumberOfChildren
== 0) {
231 // Uninstall the MNP Service Binding Protocol.
233 gBS
->UninstallMultipleProtocolInterfaces (
235 &gEfiManagedNetworkServiceBindingProtocolGuid
,
241 // Flush the Mnp service data.
243 MnpFlushServiceData (MnpServiceData
, This
->DriverBindingHandle
);
245 gBS
->FreePool (MnpServiceData
);
247 while (!IsListEmpty (&MnpServiceData
->ChildrenList
)) {
249 // Don't use NetListRemoveHead here, the remove opreration will be done
250 // in ServiceBindingDestroyChild.
252 Instance
= NET_LIST_HEAD (
253 &MnpServiceData
->ChildrenList
,
258 ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
267 Creates a child handle with a set of I/O services.
269 @param This Protocol instance pointer.
270 @param ChildHandle Pointer to the handle of the child to create. If
271 it is NULL, then a new handle is created. If it is
272 not NULL, then the I/O services are added to the
273 existing child handle.
275 @retval EFI_SUCCES The child handle was created with the I/O
277 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
279 @retval other The child handle was not created.
284 MnpServiceBindingCreateChild (
285 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
286 IN EFI_HANDLE
*ChildHandle
290 MNP_SERVICE_DATA
*MnpServiceData
;
291 MNP_INSTANCE_DATA
*Instance
;
295 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
297 return EFI_INVALID_PARAMETER
;
300 MnpServiceData
= MNP_SERVICE_DATA_FROM_THIS (This
);
303 // Allocate buffer for the new instance.
305 Instance
= AllocateZeroPool (sizeof (MNP_INSTANCE_DATA
));
306 if (Instance
== NULL
) {
308 DEBUG ((EFI_D_ERROR
, "MnpServiceBindingCreateChild: Faild to allocate memory for the new instance.\n"));
309 return EFI_OUT_OF_RESOURCES
;
313 // Init the instance data.
315 MnpInitializeInstanceData (MnpServiceData
, Instance
);
317 Status
= gBS
->InstallMultipleProtocolInterfaces (
319 &gEfiManagedNetworkProtocolGuid
,
320 &Instance
->ManagedNetwork
,
323 if (EFI_ERROR (Status
)) {
327 "MnpServiceBindingCreateChild: Failed to install the MNP protocol, %r.\n",
334 // Save the instance's childhandle.
336 Instance
->Handle
= *ChildHandle
;
338 Status
= gBS
->OpenProtocol (
339 MnpServiceData
->ControllerHandle
,
340 &gEfiSimpleNetworkProtocolGuid
,
342 gMnpDriverBinding
.DriverBindingHandle
,
344 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
346 if (EFI_ERROR (Status
)) {
351 // Add the child instance into ChildrenList.
353 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
355 InsertTailList (&MnpServiceData
->ChildrenList
, &Instance
->InstEntry
);
356 MnpServiceData
->ChildrenNumber
++;
358 gBS
->RestoreTPL (OldTpl
);
362 if (EFI_ERROR (Status
)) {
364 if (Instance
->Handle
!= NULL
) {
366 gBS
->UninstallMultipleProtocolInterfaces (
367 &gEfiManagedNetworkProtocolGuid
,
368 &Instance
->ManagedNetwork
,
373 gBS
->FreePool (Instance
);
381 Destroys a child handle with a set of I/O services.
383 @param This Protocol instance pointer.
384 @param ChildHandle Handle of the child to destroy.
386 @retval EFI_SUCCES The I/O services were removed from the child
388 @retval EFI_UNSUPPORTED The child handle does not support the I/O services
389 that are being removed.
390 @retval EFI_INVALID_PARAMETER Child handle is not a valid EFI Handle.
391 @retval EFI_ACCESS_DENIED The child handle could not be destroyed because
392 its I/O services are being used.
393 @retval other The child handle was not destroyed.
398 MnpServiceBindingDestroyChild (
399 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
400 IN EFI_HANDLE ChildHandle
404 MNP_SERVICE_DATA
*MnpServiceData
;
405 EFI_MANAGED_NETWORK_PROTOCOL
*ManagedNetwork
;
406 MNP_INSTANCE_DATA
*Instance
;
409 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
411 return EFI_INVALID_PARAMETER
;
414 MnpServiceData
= MNP_SERVICE_DATA_FROM_THIS (This
);
417 // Try to retrieve ManagedNetwork Protocol from ChildHandle.
419 Status
= gBS
->OpenProtocol (
421 &gEfiManagedNetworkProtocolGuid
,
422 (VOID
**) &ManagedNetwork
,
423 gMnpDriverBinding
.DriverBindingHandle
,
425 EFI_OPEN_PROTOCOL_GET_PROTOCOL
427 if (EFI_ERROR (Status
)) {
429 return EFI_UNSUPPORTED
;
432 Instance
= MNP_INSTANCE_DATA_FROM_THIS (ManagedNetwork
);
435 // MnpServiceBindingDestroyChild may be called twice: first called by
436 // MnpServiceBindingStop, second called by uninstalling the MNP protocol
437 // in this ChildHandle. Use destroyed to make sure the resource clean code
438 // will only excecute once.
440 if (Instance
->Destroyed
) {
445 Instance
->Destroyed
= TRUE
;
448 // Close the Simple Network protocol.
451 MnpServiceData
->ControllerHandle
,
452 &gEfiSimpleNetworkProtocolGuid
,
453 gMnpDriverBinding
.DriverBindingHandle
,
458 // Uninstall the ManagedNetwork protocol.
460 Status
= gBS
->UninstallMultipleProtocolInterfaces (
462 &gEfiManagedNetworkProtocolGuid
,
463 &Instance
->ManagedNetwork
,
466 if (EFI_ERROR (Status
)) {
470 "MnpServiceBindingDestroyChild: Failed to uninstall the ManagedNetwork protocol, %r.\n",
474 Instance
->Destroyed
= FALSE
;
478 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
481 // Reset the configuration.
483 ManagedNetwork
->Configure (ManagedNetwork
, NULL
);
486 // Try to flush the RcvdPacketQueue.
488 MnpFlushRcvdDataQueue (Instance
);
491 // Clean the RxTokenMap.
493 NetMapClean (&Instance
->RxTokenMap
);
496 // Remove this instance from the ChildrenList.
498 RemoveEntryList (&Instance
->InstEntry
);
499 MnpServiceData
->ChildrenNumber
--;
501 gBS
->RestoreTPL (OldTpl
);
503 gBS
->FreePool (Instance
);
511 MnpDriverEntryPoint (
512 IN EFI_HANDLE ImageHandle
,
513 IN EFI_SYSTEM_TABLE
*SystemTable
519 The entry point for Mnp driver which installs the driver binding and component name
520 protocol on its ImageHandle.
524 ImageHandle - The image handle of the driver.
525 SystemTable - The system table.
529 EFI_SUCCESS - If the driver binding and component name protocols are successfully
530 installed, otherwise if failed.
534 return EfiLibInstallDriverBindingComponentName2 (