]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/MnpDxe/MnpDriver.c
727dc3bf2c718bb43093e424409b9af02b7fc5f2
[mirror_edk2.git] / MdeModulePkg / Universal / Network / MnpDxe / MnpDriver.c
1 /** @file
2 Implementation of driver entry point and driver binding protocol.
3
4 Copyright (c) 2005 - 2008, Intel Corporation.<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "MnpDriver.h"
16 #include "MnpImpl.h"
17
18
19 EFI_DRIVER_BINDING_PROTOCOL gMnpDriverBinding = {
20 MnpDriverBindingSupported,
21 MnpDriverBindingStart,
22 MnpDriverBindingStop,
23 0xa,
24 NULL,
25 NULL
26 };
27
28 /**
29 Test to see if this driver supports ControllerHandle. This service
30 is called by the EFI boot service ConnectController(). In
31 order to make drivers as small as possible, there are a few calling
32 restrictions for this service. ConnectController() must
33 follow these calling restrictions. If any other agent wishes to call
34 Supported() it must also follow these calling restrictions.
35
36 @param[in] This Protocol instance pointer.
37 @param[in] ControllerHandle Handle of device to test
38 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
39 device to start.
40
41 @retval EFI_SUCCESS This driver supports this device
42 @retval EFI_ALREADY_STARTED This driver is already running on this device
43 @retval other This driver does not support this device
44
45 **/
46 EFI_STATUS
47 EFIAPI
48 MnpDriverBindingSupported (
49 IN EFI_DRIVER_BINDING_PROTOCOL *This,
50 IN EFI_HANDLE ControllerHandle,
51 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
52 )
53 {
54 EFI_STATUS Status;
55 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
56
57 //
58 // Test to see if MNP is already installed.
59 //
60 Status = gBS->OpenProtocol (
61 ControllerHandle,
62 &gEfiManagedNetworkServiceBindingProtocolGuid,
63 NULL,
64 This->DriverBindingHandle,
65 ControllerHandle,
66 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
67 );
68 if (!EFI_ERROR (Status)) {
69 return EFI_ALREADY_STARTED;
70 }
71
72 //
73 // Test to open the Simple Network protocol BY_DRIVER.
74 //
75 Status = gBS->OpenProtocol (
76 ControllerHandle,
77 &gEfiSimpleNetworkProtocolGuid,
78 (VOID **) &Snp,
79 This->DriverBindingHandle,
80 ControllerHandle,
81 EFI_OPEN_PROTOCOL_BY_DRIVER
82 );
83
84 if (EFI_ERROR (Status)) {
85 return Status;
86 }
87
88 //
89 // Close the openned SNP protocol.
90 //
91 gBS->CloseProtocol (
92 ControllerHandle,
93 &gEfiSimpleNetworkProtocolGuid,
94 This->DriverBindingHandle,
95 ControllerHandle
96 );
97
98 return EFI_SUCCESS;
99 }
100
101
102 /**
103 Start this driver on ControllerHandle. This service is called by the
104 EFI boot service ConnectController(). In order to make
105 drivers as small as possible, there are a few calling restrictions for
106 this service. ConnectController() must follow these
107 calling restrictions. If any other agent wishes to call Start() it
108 must also follow these calling restrictions.
109
110 @param[in] This Protocol instance pointer.
111 @param[in] ControllerHandle Handle of device to bind driver to.
112 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
113 device to start.
114
115 @retval EFI_SUCCESS This driver is added to ControllerHandle
116 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
117 @retval other This driver does not support this device
118
119 **/
120 EFI_STATUS
121 EFIAPI
122 MnpDriverBindingStart (
123 IN EFI_DRIVER_BINDING_PROTOCOL *This,
124 IN EFI_HANDLE ControllerHandle,
125 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
126 )
127 {
128 EFI_STATUS Status;
129 MNP_SERVICE_DATA *MnpServiceData;
130 BOOLEAN MnpInitialized;
131
132 MnpInitialized = FALSE;
133
134 MnpServiceData = AllocateZeroPool (sizeof (MNP_SERVICE_DATA));
135 if (MnpServiceData == NULL) {
136 DEBUG ((EFI_D_ERROR, "MnpDriverBindingStart(): Failed to allocate the Mnp Service Data.\n"));
137
138 return EFI_OUT_OF_RESOURCES;
139 }
140
141 //
142 // Initialize the Mnp Service Data.
143 //
144 Status = MnpInitializeServiceData (MnpServiceData, This->DriverBindingHandle, ControllerHandle);
145 if (EFI_ERROR (Status)) {
146
147 DEBUG ((EFI_D_ERROR, "MnpDriverBindingStart: MnpInitializeServiceData failed, %r.\n",Status));
148 goto ErrorExit;
149 }
150
151 MnpInitialized = TRUE;
152
153 //
154 // Install the MNP Service Binding Protocol.
155 //
156 Status = gBS->InstallMultipleProtocolInterfaces (
157 &ControllerHandle,
158 &gEfiManagedNetworkServiceBindingProtocolGuid,
159 &MnpServiceData->ServiceBinding,
160 NULL
161 );
162
163 ErrorExit:
164
165 if (EFI_ERROR (Status)) {
166
167 if (MnpInitialized) {
168 //
169 // Flush the Mnp Service Data.
170 //
171 MnpFlushServiceData (MnpServiceData, This->DriverBindingHandle);
172 }
173
174 gBS->FreePool (MnpServiceData);
175 }
176
177 return Status;
178 }
179
180 /**
181 Stop this driver on ControllerHandle. This service is called by the
182 EFI boot service DisconnectController(). In order to
183 make drivers as small as possible, there are a few calling
184 restrictions for this service. DisconnectController()
185 must follow these calling restrictions. If any other agent wishes
186 to call Stop() it must also follow these calling restrictions.
187
188 @param[in] This Protocol instance pointer.
189 @param[in] ControllerHandle Handle of device to stop driver on
190 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
191 children is zero stop the entire bus driver.
192 @param[in] ChildHandleBuffer List of Child Handles to Stop.
193
194 @retval EFI_SUCCESS This driver is removed ControllerHandle
195 @retval other This driver was not removed from this device
196
197 **/
198 EFI_STATUS
199 EFIAPI
200 MnpDriverBindingStop (
201 IN EFI_DRIVER_BINDING_PROTOCOL *This,
202 IN EFI_HANDLE ControllerHandle,
203 IN UINTN NumberOfChildren,
204 IN EFI_HANDLE *ChildHandleBuffer
205 )
206 {
207 EFI_STATUS Status;
208 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
209 MNP_SERVICE_DATA *MnpServiceData;
210 MNP_INSTANCE_DATA *Instance;
211
212 //
213 // Retrieve the MNP service binding protocol from the ControllerHandle.
214 //
215 Status = gBS->OpenProtocol (
216 ControllerHandle,
217 &gEfiManagedNetworkServiceBindingProtocolGuid,
218 (VOID **) &ServiceBinding,
219 This->DriverBindingHandle,
220 ControllerHandle,
221 EFI_OPEN_PROTOCOL_GET_PROTOCOL
222 );
223 if (EFI_ERROR (Status)) {
224
225 DEBUG (
226 (EFI_D_ERROR,
227 "MnpDriverBindingStop: Locate MNP Service Binding Protocol failed, %r.\n",
228 Status)
229 );
230 return EFI_DEVICE_ERROR;
231 }
232
233 MnpServiceData = MNP_SERVICE_DATA_FROM_THIS (ServiceBinding);
234
235 if (NumberOfChildren == 0) {
236 //
237 // Uninstall the MNP Service Binding Protocol.
238 //
239 gBS->UninstallMultipleProtocolInterfaces (
240 ControllerHandle,
241 &gEfiManagedNetworkServiceBindingProtocolGuid,
242 ServiceBinding,
243 NULL
244 );
245
246 //
247 // Flush the Mnp service data.
248 //
249 MnpFlushServiceData (MnpServiceData, This->DriverBindingHandle);
250
251 gBS->FreePool (MnpServiceData);
252 } else {
253 while (!IsListEmpty (&MnpServiceData->ChildrenList)) {
254 //
255 // Don't use NetListRemoveHead here, the remove opreration will be done
256 // in ServiceBindingDestroyChild.
257 //
258 Instance = NET_LIST_HEAD (
259 &MnpServiceData->ChildrenList,
260 MNP_INSTANCE_DATA,
261 InstEntry
262 );
263
264 ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle);
265 }
266 }
267
268 return Status;
269 }
270
271
272 /**
273 Creates a child handle and installs a protocol.
274
275 The CreateChild() function installs a protocol on ChildHandle.
276 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
277 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
278
279 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
280 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
281 then a new handle is created. If it is a pointer to an existing UEFI handle,
282 then the protocol is added to the existing UEFI handle.
283
284 @retval EFI_SUCCES The protocol was added to ChildHandle.
285 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
286 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
287 the child
288 @retval other The child handle was not created
289
290 **/
291 EFI_STATUS
292 EFIAPI
293 MnpServiceBindingCreateChild (
294 IN EFI_SERVICE_BINDING_PROTOCOL *This,
295 IN OUT EFI_HANDLE *ChildHandle
296 )
297 {
298 EFI_STATUS Status;
299 MNP_SERVICE_DATA *MnpServiceData;
300 MNP_INSTANCE_DATA *Instance;
301 VOID *Snp;
302 EFI_TPL OldTpl;
303
304 if ((This == NULL) || (ChildHandle == NULL)) {
305
306 return EFI_INVALID_PARAMETER;
307 }
308
309 MnpServiceData = MNP_SERVICE_DATA_FROM_THIS (This);
310
311 //
312 // Allocate buffer for the new instance.
313 //
314 Instance = AllocateZeroPool (sizeof (MNP_INSTANCE_DATA));
315 if (Instance == NULL) {
316
317 DEBUG ((EFI_D_ERROR, "MnpServiceBindingCreateChild: Faild to allocate memory for the new instance.\n"));
318 return EFI_OUT_OF_RESOURCES;
319 }
320
321 //
322 // Init the instance data.
323 //
324 MnpInitializeInstanceData (MnpServiceData, Instance);
325
326 Status = gBS->InstallMultipleProtocolInterfaces (
327 ChildHandle,
328 &gEfiManagedNetworkProtocolGuid,
329 &Instance->ManagedNetwork,
330 NULL
331 );
332 if (EFI_ERROR (Status)) {
333
334 DEBUG (
335 (EFI_D_ERROR,
336 "MnpServiceBindingCreateChild: Failed to install the MNP protocol, %r.\n",
337 Status)
338 );
339 goto ErrorExit;
340 }
341
342 //
343 // Save the instance's childhandle.
344 //
345 Instance->Handle = *ChildHandle;
346
347 Status = gBS->OpenProtocol (
348 MnpServiceData->ControllerHandle,
349 &gEfiSimpleNetworkProtocolGuid,
350 (VOID **) &Snp,
351 gMnpDriverBinding.DriverBindingHandle,
352 Instance->Handle,
353 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
354 );
355 if (EFI_ERROR (Status)) {
356 goto ErrorExit;
357 }
358
359 //
360 // Add the child instance into ChildrenList.
361 //
362 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
363
364 InsertTailList (&MnpServiceData->ChildrenList, &Instance->InstEntry);
365 MnpServiceData->ChildrenNumber++;
366
367 gBS->RestoreTPL (OldTpl);
368
369 ErrorExit:
370
371 if (EFI_ERROR (Status)) {
372
373 if (Instance->Handle != NULL) {
374
375 gBS->UninstallMultipleProtocolInterfaces (
376 &gEfiManagedNetworkProtocolGuid,
377 &Instance->ManagedNetwork,
378 NULL
379 );
380 }
381
382 gBS->FreePool (Instance);
383 }
384
385 return Status;
386 }
387
388
389 /**
390 Destroys a child handle with a protocol installed on it.
391
392 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
393 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
394 last protocol on ChildHandle, then ChildHandle is destroyed.
395
396 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
397 @param ChildHandle Handle of the child to destroy
398
399 @retval EFI_SUCCES The protocol was removed from ChildHandle.
400 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
401 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.
402 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
403 because its services are being used.
404 @retval other The child handle was not destroyed
405
406 **/
407 EFI_STATUS
408 EFIAPI
409 MnpServiceBindingDestroyChild (
410 IN EFI_SERVICE_BINDING_PROTOCOL *This,
411 IN EFI_HANDLE ChildHandle
412 )
413 {
414 EFI_STATUS Status;
415 MNP_SERVICE_DATA *MnpServiceData;
416 EFI_MANAGED_NETWORK_PROTOCOL *ManagedNetwork;
417 MNP_INSTANCE_DATA *Instance;
418 EFI_TPL OldTpl;
419
420 if ((This == NULL) || (ChildHandle == NULL)) {
421
422 return EFI_INVALID_PARAMETER;
423 }
424
425 MnpServiceData = MNP_SERVICE_DATA_FROM_THIS (This);
426
427 //
428 // Try to retrieve ManagedNetwork Protocol from ChildHandle.
429 //
430 Status = gBS->OpenProtocol (
431 ChildHandle,
432 &gEfiManagedNetworkProtocolGuid,
433 (VOID **) &ManagedNetwork,
434 gMnpDriverBinding.DriverBindingHandle,
435 ChildHandle,
436 EFI_OPEN_PROTOCOL_GET_PROTOCOL
437 );
438 if (EFI_ERROR (Status)) {
439
440 return EFI_UNSUPPORTED;
441 }
442
443 Instance = MNP_INSTANCE_DATA_FROM_THIS (ManagedNetwork);
444
445 //
446 // MnpServiceBindingDestroyChild may be called twice: first called by
447 // MnpServiceBindingStop, second called by uninstalling the MNP protocol
448 // in this ChildHandle. Use destroyed to make sure the resource clean code
449 // will only excecute once.
450 //
451 if (Instance->Destroyed) {
452
453 return EFI_SUCCESS;
454 }
455
456 Instance->Destroyed = TRUE;
457
458 //
459 // Close the Simple Network protocol.
460 //
461 gBS->CloseProtocol (
462 MnpServiceData->ControllerHandle,
463 &gEfiSimpleNetworkProtocolGuid,
464 gMnpDriverBinding.DriverBindingHandle,
465 ChildHandle
466 );
467
468 //
469 // Uninstall the ManagedNetwork protocol.
470 //
471 Status = gBS->UninstallMultipleProtocolInterfaces (
472 ChildHandle,
473 &gEfiManagedNetworkProtocolGuid,
474 &Instance->ManagedNetwork,
475 NULL
476 );
477 if (EFI_ERROR (Status)) {
478
479 DEBUG (
480 (EFI_D_ERROR,
481 "MnpServiceBindingDestroyChild: Failed to uninstall the ManagedNetwork protocol, %r.\n",
482 Status)
483 );
484
485 Instance->Destroyed = FALSE;
486 return Status;
487 }
488
489 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
490
491 //
492 // Reset the configuration.
493 //
494 ManagedNetwork->Configure (ManagedNetwork, NULL);
495
496 //
497 // Try to flush the RcvdPacketQueue.
498 //
499 MnpFlushRcvdDataQueue (Instance);
500
501 //
502 // Clean the RxTokenMap.
503 //
504 NetMapClean (&Instance->RxTokenMap);
505
506 //
507 // Remove this instance from the ChildrenList.
508 //
509 RemoveEntryList (&Instance->InstEntry);
510 MnpServiceData->ChildrenNumber--;
511
512 gBS->RestoreTPL (OldTpl);
513
514 gBS->FreePool (Instance);
515
516 return Status;
517 }
518
519 /**
520 The entry point for Mnp driver which installs the driver binding and component
521 name protocol on its ImageHandle.
522
523 @param[in] ImageHandle The image handle of the driver.
524 @param[in] SystemTable The system table.
525
526 @retval EFI_SUCCES The driver binding and component name protocols are
527 successfully installed.
528 @retval other failed.
529
530 **/
531 EFI_STATUS
532 EFIAPI
533 MnpDriverEntryPoint (
534 IN EFI_HANDLE ImageHandle,
535 IN EFI_SYSTEM_TABLE *SystemTable
536 )
537 {
538 return EfiLibInstallDriverBindingComponentName2 (
539 ImageHandle,
540 SystemTable,
541 &gMnpDriverBinding,
542 ImageHandle,
543 &gMnpComponentName,
544 &gMnpComponentName2
545 );
546 }