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"
26 EFI_DRIVER_BINDING_PROTOCOL gMnpDriverBinding
= {
27 MnpDriverBindingSupported
,
28 MnpDriverBindingStart
,
37 Test to see if this driver supports ControllerHandle.
39 @param This Protocol instance pointer.
40 @param ControllerHandle Handle of device to test.
41 @param RemainingDevicePath Optional parameter use to pick a specific child
44 @retval EFI_SUCCES This driver supports this device.
45 @retval EFI_ALREADY_STARTED This driver is already running on this device.
46 @retval other This driver does not support this device.
51 MnpDriverBindingSupported (
52 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
53 IN EFI_HANDLE ControllerHandle
,
54 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
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
)) {
72 return EFI_ALREADY_STARTED
;
76 // Test to see if SNP is installed.
78 Status
= gBS
->OpenProtocol (
80 &gEfiSimpleNetworkProtocolGuid
,
82 This
->DriverBindingHandle
,
84 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
92 Start this driver on ControllerHandle.
94 @param This Protocol instance pointer.
95 @param ControllerHandle Handle of device to bind driver to.
96 @param RemainingDevicePath Optional parameter use to pick a specific child
99 @retval EFI_SUCCES This driver is added to ControllerHandle.
100 @retval EFI_ALREADY_STARTED This driver is already running on
102 @retval other This driver does not support this device.
107 MnpDriverBindingStart (
108 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
109 IN EFI_HANDLE ControllerHandle
,
110 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
114 MNP_SERVICE_DATA
*MnpServiceData
;
115 BOOLEAN MnpInitialized
;
117 MnpInitialized
= FALSE
;
119 MnpServiceData
= NetAllocateZeroPool (sizeof (MNP_SERVICE_DATA
));
120 if (MnpServiceData
== NULL
) {
121 MNP_DEBUG_ERROR (("MnpDriverBindingStart(): Failed to allocate the "
122 L
"Mnp Service Data.\n"));
124 return EFI_OUT_OF_RESOURCES
;
128 // Initialize the Mnp Service Data.
130 Status
= MnpInitializeServiceData (MnpServiceData
, This
->DriverBindingHandle
, ControllerHandle
);
131 if (EFI_ERROR (Status
)) {
133 MNP_DEBUG_ERROR (("MnpDriverBindingStart: MnpInitializeServiceData "
134 L
"failed, %r.\n",Status
));
138 MnpInitialized
= TRUE
;
141 // Install the MNP Service Binding Protocol.
143 Status
= gBS
->InstallMultipleProtocolInterfaces (
145 &gEfiManagedNetworkServiceBindingProtocolGuid
,
146 &MnpServiceData
->ServiceBinding
,
152 if (EFI_ERROR (Status
)) {
154 if (MnpInitialized
) {
156 // Flush the Mnp Service Data.
158 MnpFlushServiceData (MnpServiceData
);
162 // Close the Simple Network Protocol.
166 &gEfiSimpleNetworkProtocolGuid
,
167 This
->DriverBindingHandle
,
171 NetFreePool (MnpServiceData
);
179 Stop this driver on ControllerHandle.
181 @param This Protocol instance pointer.
182 @param ControllerHandle Handle of device to stop driver on.
183 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number
184 of children is zero stop the entire bus driver.
185 @param ChildHandleBuffer List of Child Handles to Stop.
187 @retval EFI_SUCCES This driver is removed ControllerHandle.
188 @retval other This driver was not removed from this device.
193 MnpDriverBindingStop (
194 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
195 IN EFI_HANDLE ControllerHandle
,
196 IN UINTN NumberOfChildren
,
197 IN EFI_HANDLE
*ChildHandleBuffer
201 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
202 MNP_SERVICE_DATA
*MnpServiceData
;
203 MNP_INSTANCE_DATA
*Instance
;
206 // Retrieve the MNP service binding protocol from the ControllerHandle.
208 Status
= gBS
->OpenProtocol (
210 &gEfiManagedNetworkServiceBindingProtocolGuid
,
211 (VOID
**) &ServiceBinding
,
212 This
->DriverBindingHandle
,
214 EFI_OPEN_PROTOCOL_GET_PROTOCOL
216 if (EFI_ERROR (Status
)) {
219 ("MnpDriverBindingStop: Locate MNP Service Binding Protocol failed, %r.\n",
225 MnpServiceData
= MNP_SERVICE_DATA_FROM_THIS (ServiceBinding
);
227 while (!NetListIsEmpty (&MnpServiceData
->ChildrenList
)) {
229 // Don't use NetListRemoveHead here, the remove opreration will be done
230 // in ServiceBindingDestroyChild.
232 Instance
= NET_LIST_HEAD (
233 &MnpServiceData
->ChildrenList
,
238 ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
242 // Uninstall the MNP Service Binding Protocol.
244 Status
= gBS
->UninstallMultipleProtocolInterfaces (
246 &gEfiManagedNetworkServiceBindingProtocolGuid
,
250 if (EFI_ERROR (Status
)) {
252 MNP_DEBUG_ERROR (("MnpDriverBindingStop: Uninstall MNP Service Binding Protocol failed, %r.\n"));
257 // Close the openned Snp protocol.
259 Status
= gBS
->CloseProtocol (
261 &gEfiSimpleNetworkProtocolGuid
,
262 This
->DriverBindingHandle
,
265 if (EFI_ERROR (Status
)) {
267 MNP_DEBUG_ERROR (("MnpDriverBindingStop: Close SNP Protocol failed, %r.\n", Status
));
272 // Flush the Mnp service data.
274 MnpFlushServiceData (MnpServiceData
);
276 NetFreePool (MnpServiceData
);
285 Creates a child handle with a set of I/O services.
287 @param This Protocol instance pointer.
288 @param ChildHandle Pointer to the handle of the child to create. If
289 it is NULL, then a new handle is created. If it is
290 not NULL, then the I/O services are added to the
291 existing child handle.
293 @retval EFI_SUCCES The child handle was created with the I/O
295 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
297 @retval other The child handle was not created.
302 MnpServiceBindingCreateChild (
303 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
304 IN EFI_HANDLE
*ChildHandle
308 MNP_SERVICE_DATA
*MnpServiceData
;
309 MNP_INSTANCE_DATA
*Instance
;
313 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
315 return EFI_INVALID_PARAMETER
;
318 MnpServiceData
= MNP_SERVICE_DATA_FROM_THIS (This
);
321 // Allocate buffer for the new instance.
323 Instance
= NetAllocateZeroPool (sizeof (MNP_INSTANCE_DATA
));
324 if (Instance
== NULL
) {
326 MNP_DEBUG_ERROR (("MnpServiceBindingCreateChild: Faild to allocate memory for the new instance.\n"));
327 return EFI_OUT_OF_RESOURCES
;
331 // Init the instance data.
333 MnpInitializeInstanceData (MnpServiceData
, Instance
);
335 Status
= gBS
->InstallMultipleProtocolInterfaces (
337 &gEfiManagedNetworkProtocolGuid
,
338 &Instance
->ManagedNetwork
,
341 if (EFI_ERROR (Status
)) {
344 ("MnpServiceBindingCreateChild: Failed to install the MNP protocol, %r.\n",
351 // Save the instance's childhandle.
353 Instance
->Handle
= *ChildHandle
;
355 Status
= gBS
->OpenProtocol (
356 MnpServiceData
->ControllerHandle
,
357 &gEfiSimpleNetworkProtocolGuid
,
359 gMnpDriverBinding
.DriverBindingHandle
,
361 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
363 if (EFI_ERROR (Status
)) {
368 // Add the child instance into ChildrenList.
370 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
372 NetListInsertTail (&MnpServiceData
->ChildrenList
, &Instance
->InstEntry
);
373 MnpServiceData
->ChildrenNumber
++;
375 NET_RESTORE_TPL (OldTpl
);
379 if (EFI_ERROR (Status
)) {
381 if (Instance
->Handle
!= NULL
) {
383 gBS
->UninstallMultipleProtocolInterfaces (
384 &gEfiManagedNetworkProtocolGuid
,
385 &Instance
->ManagedNetwork
,
390 NetFreePool (Instance
);
398 Destroys a child handle with a set of I/O services.
400 @param This Protocol instance pointer.
401 @param ChildHandle Handle of the child to destroy.
403 @retval EFI_SUCCES The I/O services were removed from the child
405 @retval EFI_UNSUPPORTED The child handle does not support the I/O services
406 that are being removed.
407 @retval EFI_INVALID_PARAMETER Child handle is not a valid EFI Handle.
408 @retval EFI_ACCESS_DENIED The child handle could not be destroyed because
409 its I/O services are being used.
410 @retval other The child handle was not destroyed.
415 MnpServiceBindingDestroyChild (
416 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
417 IN EFI_HANDLE ChildHandle
421 MNP_SERVICE_DATA
*MnpServiceData
;
422 EFI_MANAGED_NETWORK_PROTOCOL
*ManagedNetwork
;
423 MNP_INSTANCE_DATA
*Instance
;
426 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
428 return EFI_INVALID_PARAMETER
;
431 MnpServiceData
= MNP_SERVICE_DATA_FROM_THIS (This
);
434 // Try to retrieve ManagedNetwork Protocol from ChildHandle.
436 Status
= gBS
->OpenProtocol (
438 &gEfiManagedNetworkProtocolGuid
,
439 (VOID
**) &ManagedNetwork
,
440 gMnpDriverBinding
.DriverBindingHandle
,
442 EFI_OPEN_PROTOCOL_GET_PROTOCOL
444 if (EFI_ERROR (Status
)) {
446 return EFI_UNSUPPORTED
;
449 Instance
= MNP_INSTANCE_DATA_FROM_THIS (ManagedNetwork
);
452 // MnpServiceBindingDestroyChild may be called twice: first called by
453 // MnpServiceBindingStop, second called by uninstalling the MNP protocol
454 // in this ChildHandle. Use destroyed to make sure the resource clean code
455 // will only excecute once.
457 if (Instance
->Destroyed
) {
462 Instance
->Destroyed
= TRUE
;
465 // Close the Simple Network protocol.
468 MnpServiceData
->ControllerHandle
,
469 &gEfiSimpleNetworkProtocolGuid
,
470 gMnpDriverBinding
.DriverBindingHandle
,
475 // Uninstall the ManagedNetwork protocol.
477 Status
= gBS
->UninstallMultipleProtocolInterfaces (
479 &gEfiManagedNetworkProtocolGuid
,
480 &Instance
->ManagedNetwork
,
483 if (EFI_ERROR (Status
)) {
486 ("MnpServiceBindingDestroyChild: Failed to uninstall the ManagedNetwork protocol, %r.\n",
490 Instance
->Destroyed
= FALSE
;
494 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
497 // Reset the configuration.
499 ManagedNetwork
->Configure (ManagedNetwork
, NULL
);
502 // Try to flush the RcvdPacketQueue.
504 MnpFlushRcvdDataQueue (Instance
);
507 // Clean the RxTokenMap.
509 NetMapClean (&Instance
->RxTokenMap
);
512 // Remove this instance from the ChildrenList.
514 NetListRemoveEntry (&Instance
->InstEntry
);
515 MnpServiceData
->ChildrenNumber
--;
517 NET_RESTORE_TPL (OldTpl
);
519 NetFreePool (Instance
);
527 MnpDriverEntryPoint (
528 IN EFI_HANDLE ImageHandle
,
529 IN EFI_SYSTEM_TABLE
*SystemTable
535 The entry point for Mnp driver which installs the driver binding and component name
536 protocol on its ImageHandle.
540 ImageHandle - The image handle of the driver.
541 SystemTable - The system table.
545 EFI_SUCCESS - If the driver binding and component name protocols are successfully
546 installed, otherwise if failed.
550 return EfiLibInstallAllDriverProtocols (