3 Copyright (c) 2005 - 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.
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
59 // Test to see if MNP is already installed.
61 Status
= gBS
->OpenProtocol (
63 &gEfiManagedNetworkServiceBindingProtocolGuid
,
65 This
->DriverBindingHandle
,
67 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
69 if (!EFI_ERROR (Status
)) {
71 return EFI_ALREADY_STARTED
;
75 // Test to see if SNP is installed.
77 Status
= gBS
->OpenProtocol (
79 &gEfiSimpleNetworkProtocolGuid
,
81 This
->DriverBindingHandle
,
83 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
91 Start this driver on ControllerHandle.
93 @param This Protocol instance pointer.
94 @param ControllerHandle Handle of device to bind driver to.
95 @param RemainingDevicePath Optional parameter use to pick a specific child
98 @retval EFI_SUCCES This driver is added to ControllerHandle.
99 @retval EFI_ALREADY_STARTED This driver is already running on
101 @retval other This driver does not support this device.
106 MnpDriverBindingStart (
107 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
108 IN EFI_HANDLE ControllerHandle
,
109 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
113 MNP_SERVICE_DATA
*MnpServiceData
;
114 BOOLEAN MnpInitialized
;
116 MnpInitialized
= FALSE
;
118 MnpServiceData
= AllocateZeroPool (sizeof (MNP_SERVICE_DATA
));
119 if (MnpServiceData
== NULL
) {
120 DEBUG ((EFI_D_ERROR
, "MnpDriverBindingStart(): Failed to allocate the Mnp Service Data.\n"));
122 return EFI_OUT_OF_RESOURCES
;
126 // Initialize the Mnp Service Data.
128 Status
= MnpInitializeServiceData (MnpServiceData
, This
->DriverBindingHandle
, ControllerHandle
);
129 if (EFI_ERROR (Status
)) {
131 DEBUG ((EFI_D_ERROR
, "MnpDriverBindingStart: MnpInitializeServiceData failed, %r.\n",Status
));
135 MnpInitialized
= TRUE
;
138 // Install the MNP Service Binding Protocol.
140 Status
= gBS
->InstallMultipleProtocolInterfaces (
142 &gEfiManagedNetworkServiceBindingProtocolGuid
,
143 &MnpServiceData
->ServiceBinding
,
149 if (EFI_ERROR (Status
)) {
151 if (MnpInitialized
) {
153 // Flush the Mnp Service Data.
155 MnpFlushServiceData (MnpServiceData
);
159 // Close the Simple Network Protocol.
163 &gEfiSimpleNetworkProtocolGuid
,
164 This
->DriverBindingHandle
,
168 gBS
->FreePool (MnpServiceData
);
176 Stop this driver on ControllerHandle.
178 @param This Protocol instance pointer.
179 @param ControllerHandle Handle of device to stop driver on.
180 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number
181 of children is zero stop the entire bus driver.
182 @param ChildHandleBuffer List of Child Handles to Stop.
184 @retval EFI_SUCCES This driver is removed ControllerHandle.
185 @retval other This driver was not removed from this device.
190 MnpDriverBindingStop (
191 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
192 IN EFI_HANDLE ControllerHandle
,
193 IN UINTN NumberOfChildren
,
194 IN EFI_HANDLE
*ChildHandleBuffer
198 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
199 MNP_SERVICE_DATA
*MnpServiceData
;
200 MNP_INSTANCE_DATA
*Instance
;
203 // Retrieve the MNP service binding protocol from the ControllerHandle.
205 Status
= gBS
->OpenProtocol (
207 &gEfiManagedNetworkServiceBindingProtocolGuid
,
208 (VOID
**) &ServiceBinding
,
209 This
->DriverBindingHandle
,
211 EFI_OPEN_PROTOCOL_GET_PROTOCOL
213 if (EFI_ERROR (Status
)) {
217 "MnpDriverBindingStop: Locate MNP Service Binding Protocol failed, %r.\n",
220 return EFI_DEVICE_ERROR
;
223 MnpServiceData
= MNP_SERVICE_DATA_FROM_THIS (ServiceBinding
);
225 if (NumberOfChildren
== 0) {
227 // Uninstall the MNP Service Binding Protocol.
229 gBS
->UninstallMultipleProtocolInterfaces (
231 &gEfiManagedNetworkServiceBindingProtocolGuid
,
237 // Close the openned Snp protocol.
241 &gEfiSimpleNetworkProtocolGuid
,
242 This
->DriverBindingHandle
,
247 // Flush the Mnp service data.
249 MnpFlushServiceData (MnpServiceData
);
251 gBS
->FreePool (MnpServiceData
);
253 while (!IsListEmpty (&MnpServiceData
->ChildrenList
)) {
255 // Don't use NetListRemoveHead here, the remove opreration will be done
256 // in ServiceBindingDestroyChild.
258 Instance
= NET_LIST_HEAD (
259 &MnpServiceData
->ChildrenList
,
264 ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
273 Creates a child handle with a set of I/O services.
275 @param This Protocol instance pointer.
276 @param ChildHandle Pointer to the handle of the child to create. If
277 it is NULL, then a new handle is created. If it is
278 not NULL, then the I/O services are added to the
279 existing child handle.
281 @retval EFI_SUCCES The child handle was created with the I/O
283 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
285 @retval other The child handle was not created.
290 MnpServiceBindingCreateChild (
291 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
292 IN EFI_HANDLE
*ChildHandle
296 MNP_SERVICE_DATA
*MnpServiceData
;
297 MNP_INSTANCE_DATA
*Instance
;
301 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
303 return EFI_INVALID_PARAMETER
;
306 MnpServiceData
= MNP_SERVICE_DATA_FROM_THIS (This
);
309 // Allocate buffer for the new instance.
311 Instance
= AllocateZeroPool (sizeof (MNP_INSTANCE_DATA
));
312 if (Instance
== NULL
) {
314 DEBUG ((EFI_D_ERROR
, "MnpServiceBindingCreateChild: Faild to allocate memory for the new instance.\n"));
315 return EFI_OUT_OF_RESOURCES
;
319 // Init the instance data.
321 MnpInitializeInstanceData (MnpServiceData
, Instance
);
323 Status
= gBS
->InstallMultipleProtocolInterfaces (
325 &gEfiManagedNetworkProtocolGuid
,
326 &Instance
->ManagedNetwork
,
329 if (EFI_ERROR (Status
)) {
333 "MnpServiceBindingCreateChild: Failed to install the MNP protocol, %r.\n",
340 // Save the instance's childhandle.
342 Instance
->Handle
= *ChildHandle
;
344 Status
= gBS
->OpenProtocol (
345 MnpServiceData
->ControllerHandle
,
346 &gEfiSimpleNetworkProtocolGuid
,
348 gMnpDriverBinding
.DriverBindingHandle
,
350 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
352 if (EFI_ERROR (Status
)) {
357 // Add the child instance into ChildrenList.
359 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
361 InsertTailList (&MnpServiceData
->ChildrenList
, &Instance
->InstEntry
);
362 MnpServiceData
->ChildrenNumber
++;
364 gBS
->RestoreTPL (OldTpl
);
368 if (EFI_ERROR (Status
)) {
370 if (Instance
->Handle
!= NULL
) {
372 gBS
->UninstallMultipleProtocolInterfaces (
373 &gEfiManagedNetworkProtocolGuid
,
374 &Instance
->ManagedNetwork
,
379 gBS
->FreePool (Instance
);
387 Destroys a child handle with a set of I/O services.
389 @param This Protocol instance pointer.
390 @param ChildHandle Handle of the child to destroy.
392 @retval EFI_SUCCES The I/O services were removed from the child
394 @retval EFI_UNSUPPORTED The child handle does not support the I/O services
395 that are being removed.
396 @retval EFI_INVALID_PARAMETER Child handle is not a valid EFI Handle.
397 @retval EFI_ACCESS_DENIED The child handle could not be destroyed because
398 its I/O services are being used.
399 @retval other The child handle was not destroyed.
404 MnpServiceBindingDestroyChild (
405 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
406 IN EFI_HANDLE ChildHandle
410 MNP_SERVICE_DATA
*MnpServiceData
;
411 EFI_MANAGED_NETWORK_PROTOCOL
*ManagedNetwork
;
412 MNP_INSTANCE_DATA
*Instance
;
415 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
417 return EFI_INVALID_PARAMETER
;
420 MnpServiceData
= MNP_SERVICE_DATA_FROM_THIS (This
);
423 // Try to retrieve ManagedNetwork Protocol from ChildHandle.
425 Status
= gBS
->OpenProtocol (
427 &gEfiManagedNetworkProtocolGuid
,
428 (VOID
**) &ManagedNetwork
,
429 gMnpDriverBinding
.DriverBindingHandle
,
431 EFI_OPEN_PROTOCOL_GET_PROTOCOL
433 if (EFI_ERROR (Status
)) {
435 return EFI_UNSUPPORTED
;
438 Instance
= MNP_INSTANCE_DATA_FROM_THIS (ManagedNetwork
);
441 // MnpServiceBindingDestroyChild may be called twice: first called by
442 // MnpServiceBindingStop, second called by uninstalling the MNP protocol
443 // in this ChildHandle. Use destroyed to make sure the resource clean code
444 // will only excecute once.
446 if (Instance
->Destroyed
) {
451 Instance
->Destroyed
= TRUE
;
454 // Close the Simple Network protocol.
457 MnpServiceData
->ControllerHandle
,
458 &gEfiSimpleNetworkProtocolGuid
,
459 gMnpDriverBinding
.DriverBindingHandle
,
464 // Uninstall the ManagedNetwork protocol.
466 Status
= gBS
->UninstallMultipleProtocolInterfaces (
468 &gEfiManagedNetworkProtocolGuid
,
469 &Instance
->ManagedNetwork
,
472 if (EFI_ERROR (Status
)) {
476 "MnpServiceBindingDestroyChild: Failed to uninstall the ManagedNetwork protocol, %r.\n",
480 Instance
->Destroyed
= FALSE
;
484 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
487 // Reset the configuration.
489 ManagedNetwork
->Configure (ManagedNetwork
, NULL
);
492 // Try to flush the RcvdPacketQueue.
494 MnpFlushRcvdDataQueue (Instance
);
497 // Clean the RxTokenMap.
499 NetMapClean (&Instance
->RxTokenMap
);
502 // Remove this instance from the ChildrenList.
504 RemoveEntryList (&Instance
->InstEntry
);
505 MnpServiceData
->ChildrenNumber
--;
507 gBS
->RestoreTPL (OldTpl
);
509 gBS
->FreePool (Instance
);
517 MnpDriverEntryPoint (
518 IN EFI_HANDLE ImageHandle
,
519 IN EFI_SYSTEM_TABLE
*SystemTable
525 The entry point for Mnp driver which installs the driver binding and component name
526 protocol on its ImageHandle.
530 ImageHandle - The image handle of the driver.
531 SystemTable - The system table.
535 EFI_SUCCESS - If the driver binding and component name protocols are successfully
536 installed, otherwise if failed.
540 return EfiLibInstallDriverBindingComponentName2 (