]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/MnpDxe/MnpDriver.c
Change the file headers and some function comments.
[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 are licensed
6 and made available under the terms and conditions of the BSD License which
7 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 This Protocol instance pointer.
37 @param ControllerHandle Handle of device to test
38 @param 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 This Protocol instance pointer.
111 @param ControllerHandle Handle of device to bind driver to.
112 @param 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 This Protocol instance pointer.
189 @param ControllerHandle Handle of device to stop driver on
190 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
191 children is zero stop the entire bus driver.
192 @param 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 with a set of I/O services.
274
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.
280
281 @retval EFI_SUCCES The protocol was added to ChildHandle.
282 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
283 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to
284 create the child.
285 @retval other The child handle was not created.
286
287 **/
288 EFI_STATUS
289 EFIAPI
290 MnpServiceBindingCreateChild (
291 IN EFI_SERVICE_BINDING_PROTOCOL *This,
292 IN OUT EFI_HANDLE *ChildHandle
293 )
294 {
295 EFI_STATUS Status;
296 MNP_SERVICE_DATA *MnpServiceData;
297 MNP_INSTANCE_DATA *Instance;
298 VOID *Snp;
299 EFI_TPL OldTpl;
300
301 if ((This == NULL) || (ChildHandle == NULL)) {
302
303 return EFI_INVALID_PARAMETER;
304 }
305
306 MnpServiceData = MNP_SERVICE_DATA_FROM_THIS (This);
307
308 //
309 // Allocate buffer for the new instance.
310 //
311 Instance = AllocateZeroPool (sizeof (MNP_INSTANCE_DATA));
312 if (Instance == NULL) {
313
314 DEBUG ((EFI_D_ERROR, "MnpServiceBindingCreateChild: Faild to allocate memory for the new instance.\n"));
315 return EFI_OUT_OF_RESOURCES;
316 }
317
318 //
319 // Init the instance data.
320 //
321 MnpInitializeInstanceData (MnpServiceData, Instance);
322
323 Status = gBS->InstallMultipleProtocolInterfaces (
324 ChildHandle,
325 &gEfiManagedNetworkProtocolGuid,
326 &Instance->ManagedNetwork,
327 NULL
328 );
329 if (EFI_ERROR (Status)) {
330
331 DEBUG (
332 (EFI_D_ERROR,
333 "MnpServiceBindingCreateChild: Failed to install the MNP protocol, %r.\n",
334 Status)
335 );
336 goto ErrorExit;
337 }
338
339 //
340 // Save the instance's childhandle.
341 //
342 Instance->Handle = *ChildHandle;
343
344 Status = gBS->OpenProtocol (
345 MnpServiceData->ControllerHandle,
346 &gEfiSimpleNetworkProtocolGuid,
347 (VOID **) &Snp,
348 gMnpDriverBinding.DriverBindingHandle,
349 Instance->Handle,
350 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
351 );
352 if (EFI_ERROR (Status)) {
353 goto ErrorExit;
354 }
355
356 //
357 // Add the child instance into ChildrenList.
358 //
359 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
360
361 InsertTailList (&MnpServiceData->ChildrenList, &Instance->InstEntry);
362 MnpServiceData->ChildrenNumber++;
363
364 gBS->RestoreTPL (OldTpl);
365
366 ErrorExit:
367
368 if (EFI_ERROR (Status)) {
369
370 if (Instance->Handle != NULL) {
371
372 gBS->UninstallMultipleProtocolInterfaces (
373 &gEfiManagedNetworkProtocolGuid,
374 &Instance->ManagedNetwork,
375 NULL
376 );
377 }
378
379 gBS->FreePool (Instance);
380 }
381
382 return Status;
383 }
384
385
386 /**
387 Destroys a child handle with a set of I/O services.
388
389 The DestroyChild() function does the opposite of CreateChild(). It removes a
390 protocol that was installed by CreateChild() from ChildHandle. If the removed
391 protocol is the last protocol on ChildHandle, then ChildHandle is destroyed.
392
393 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL
394 instance.
395 @param ChildHandle Handle of the child to destroy.
396
397 @retval EFI_SUCCES The protocol was removed from ChildHandle.
398 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that
399 is being removed.
400 @retval EFI_INVALID_PARAMETER ChildHandle is not a valid UEFI handle.
401 @retval EFI_ACCESS_DENIED The protocol could not be removed from the
402 ChildHandle because its services are being
403 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 ImageHandle The image handle of the driver.
524 @param 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 }