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 // Close the openned Snp protocol.
245 &gEfiSimpleNetworkProtocolGuid
,
246 This
->DriverBindingHandle
,
251 // Flush the Mnp service data.
253 MnpFlushServiceData (MnpServiceData
, This
->DriverBindingHandle
);
255 gBS
->FreePool (MnpServiceData
);
257 while (!IsListEmpty (&MnpServiceData
->ChildrenList
)) {
259 // Don't use NetListRemoveHead here, the remove opreration will be done
260 // in ServiceBindingDestroyChild.
262 Instance
= NET_LIST_HEAD (
263 &MnpServiceData
->ChildrenList
,
268 ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
277 Creates a child handle with a set of I/O services.
279 @param This Protocol instance pointer.
280 @param ChildHandle Pointer to the handle of the child to create. If
281 it is NULL, then a new handle is created. If it is
282 not NULL, then the I/O services are added to the
283 existing child handle.
285 @retval EFI_SUCCES The child handle was created with the I/O
287 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
289 @retval other The child handle was not created.
294 MnpServiceBindingCreateChild (
295 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
296 IN EFI_HANDLE
*ChildHandle
300 MNP_SERVICE_DATA
*MnpServiceData
;
301 MNP_INSTANCE_DATA
*Instance
;
305 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
307 return EFI_INVALID_PARAMETER
;
310 MnpServiceData
= MNP_SERVICE_DATA_FROM_THIS (This
);
313 // Allocate buffer for the new instance.
315 Instance
= AllocateZeroPool (sizeof (MNP_INSTANCE_DATA
));
316 if (Instance
== NULL
) {
318 DEBUG ((EFI_D_ERROR
, "MnpServiceBindingCreateChild: Faild to allocate memory for the new instance.\n"));
319 return EFI_OUT_OF_RESOURCES
;
323 // Init the instance data.
325 MnpInitializeInstanceData (MnpServiceData
, Instance
);
327 Status
= gBS
->InstallMultipleProtocolInterfaces (
329 &gEfiManagedNetworkProtocolGuid
,
330 &Instance
->ManagedNetwork
,
333 if (EFI_ERROR (Status
)) {
337 "MnpServiceBindingCreateChild: Failed to install the MNP protocol, %r.\n",
344 // Save the instance's childhandle.
346 Instance
->Handle
= *ChildHandle
;
348 Status
= gBS
->OpenProtocol (
349 MnpServiceData
->ControllerHandle
,
350 &gEfiSimpleNetworkProtocolGuid
,
352 gMnpDriverBinding
.DriverBindingHandle
,
354 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
356 if (EFI_ERROR (Status
)) {
361 // Add the child instance into ChildrenList.
363 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
365 InsertTailList (&MnpServiceData
->ChildrenList
, &Instance
->InstEntry
);
366 MnpServiceData
->ChildrenNumber
++;
368 gBS
->RestoreTPL (OldTpl
);
372 if (EFI_ERROR (Status
)) {
374 if (Instance
->Handle
!= NULL
) {
376 gBS
->UninstallMultipleProtocolInterfaces (
377 &gEfiManagedNetworkProtocolGuid
,
378 &Instance
->ManagedNetwork
,
383 gBS
->FreePool (Instance
);
391 Destroys a child handle with a set of I/O services.
393 @param This Protocol instance pointer.
394 @param ChildHandle Handle of the child to destroy.
396 @retval EFI_SUCCES The I/O services were removed from the child
398 @retval EFI_UNSUPPORTED The child handle does not support the I/O services
399 that are being removed.
400 @retval EFI_INVALID_PARAMETER Child handle is not a valid EFI Handle.
401 @retval EFI_ACCESS_DENIED The child handle could not be destroyed because
402 its I/O services are being used.
403 @retval other The child handle was not destroyed.
408 MnpServiceBindingDestroyChild (
409 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
410 IN EFI_HANDLE ChildHandle
414 MNP_SERVICE_DATA
*MnpServiceData
;
415 EFI_MANAGED_NETWORK_PROTOCOL
*ManagedNetwork
;
416 MNP_INSTANCE_DATA
*Instance
;
419 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
421 return EFI_INVALID_PARAMETER
;
424 MnpServiceData
= MNP_SERVICE_DATA_FROM_THIS (This
);
427 // Try to retrieve ManagedNetwork Protocol from ChildHandle.
429 Status
= gBS
->OpenProtocol (
431 &gEfiManagedNetworkProtocolGuid
,
432 (VOID
**) &ManagedNetwork
,
433 gMnpDriverBinding
.DriverBindingHandle
,
435 EFI_OPEN_PROTOCOL_GET_PROTOCOL
437 if (EFI_ERROR (Status
)) {
439 return EFI_UNSUPPORTED
;
442 Instance
= MNP_INSTANCE_DATA_FROM_THIS (ManagedNetwork
);
445 // MnpServiceBindingDestroyChild may be called twice: first called by
446 // MnpServiceBindingStop, second called by uninstalling the MNP protocol
447 // in this ChildHandle. Use destroyed to make sure the resource clean code
448 // will only excecute once.
450 if (Instance
->Destroyed
) {
455 Instance
->Destroyed
= TRUE
;
458 // Close the Simple Network protocol.
461 MnpServiceData
->ControllerHandle
,
462 &gEfiSimpleNetworkProtocolGuid
,
463 gMnpDriverBinding
.DriverBindingHandle
,
468 // Uninstall the ManagedNetwork protocol.
470 Status
= gBS
->UninstallMultipleProtocolInterfaces (
472 &gEfiManagedNetworkProtocolGuid
,
473 &Instance
->ManagedNetwork
,
476 if (EFI_ERROR (Status
)) {
480 "MnpServiceBindingDestroyChild: Failed to uninstall the ManagedNetwork protocol, %r.\n",
484 Instance
->Destroyed
= FALSE
;
488 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
491 // Reset the configuration.
493 ManagedNetwork
->Configure (ManagedNetwork
, NULL
);
496 // Try to flush the RcvdPacketQueue.
498 MnpFlushRcvdDataQueue (Instance
);
501 // Clean the RxTokenMap.
503 NetMapClean (&Instance
->RxTokenMap
);
506 // Remove this instance from the ChildrenList.
508 RemoveEntryList (&Instance
->InstEntry
);
509 MnpServiceData
->ChildrenNumber
--;
511 gBS
->RestoreTPL (OldTpl
);
513 gBS
->FreePool (Instance
);
521 MnpDriverEntryPoint (
522 IN EFI_HANDLE ImageHandle
,
523 IN EFI_SYSTEM_TABLE
*SystemTable
529 The entry point for Mnp driver which installs the driver binding and component name
530 protocol on its ImageHandle.
534 ImageHandle - The image handle of the driver.
535 SystemTable - The system table.
539 EFI_SUCCESS - If the driver binding and component name protocols are successfully
540 installed, otherwise if failed.
544 return EfiLibInstallDriverBindingComponentName2 (