]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Driver.c
cccaab6dee285b09bebba21c4ed830ed1ef8c4df
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Driver.c
1 /** @file
2 The driver binding and service binding protocol for IP4 driver.
3
4 Copyright (c) 2005 - 2010, 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 "Ip4Impl.h"
16
17 EFI_DRIVER_BINDING_PROTOCOL gIp4DriverBinding = {
18 Ip4DriverBindingSupported,
19 Ip4DriverBindingStart,
20 Ip4DriverBindingStop,
21 0xa,
22 NULL,
23 NULL
24 };
25
26 /**
27 This is the declaration of an EFI image entry point. This entry point is
28 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
29 both device drivers and bus drivers.
30
31 The entry point for IP4 driver which install the driver
32 binding and component name protocol on its image.
33
34 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
35 @param[in] SystemTable A pointer to the EFI System Table.
36
37 @retval EFI_SUCCESS The operation completed successfully.
38 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
39
40 **/
41 EFI_STATUS
42 EFIAPI
43 Ip4DriverEntryPoint (
44 IN EFI_HANDLE ImageHandle,
45 IN EFI_SYSTEM_TABLE *SystemTable
46 )
47 {
48 return EfiLibInstallDriverBindingComponentName2 (
49 ImageHandle,
50 SystemTable,
51 &gIp4DriverBinding,
52 ImageHandle,
53 &gIp4ComponentName,
54 &gIp4ComponentName2
55 );
56 }
57
58 /**
59 Test to see if this driver supports ControllerHandle. This service
60 is called by the EFI boot service ConnectController(). In
61 order to make drivers as small as possible, there are a few calling
62 restrictions for this service. ConnectController() must
63 follow these calling restrictions. If any other agent wishes to call
64 Supported() it must also follow these calling restrictions.
65
66 @param[in] This Protocol instance pointer.
67 @param[in] ControllerHandle Handle of device to test
68 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
69 device to start.
70
71 @retval EFI_SUCCESS This driver supports this device
72 @retval EFI_ALREADY_STARTED This driver is already running on this device
73 @retval other This driver does not support this device
74
75 **/
76 EFI_STATUS
77 EFIAPI
78 Ip4DriverBindingSupported (
79 IN EFI_DRIVER_BINDING_PROTOCOL * This,
80 IN EFI_HANDLE ControllerHandle,
81 IN EFI_DEVICE_PATH_PROTOCOL * RemainingDevicePath OPTIONAL
82 )
83 {
84 EFI_STATUS Status;
85
86 //
87 // Test for the MNP service binding Protocol
88 //
89 Status = gBS->OpenProtocol (
90 ControllerHandle,
91 &gEfiManagedNetworkServiceBindingProtocolGuid,
92 NULL,
93 This->DriverBindingHandle,
94 ControllerHandle,
95 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
96 );
97
98 if (EFI_ERROR (Status)) {
99 return Status;
100 }
101
102 //
103 // Test for the Arp service binding Protocol
104 //
105 Status = gBS->OpenProtocol (
106 ControllerHandle,
107 &gEfiArpServiceBindingProtocolGuid,
108 NULL,
109 This->DriverBindingHandle,
110 ControllerHandle,
111 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
112 );
113
114 return Status;
115 }
116
117 /**
118 Clean up a IP4 service binding instance. It will release all
119 the resource allocated by the instance. The instance may be
120 partly initialized, or partly destroyed. If a resource is
121 destroyed, it is marked as that in case the destory failed and
122 being called again later.
123
124 @param[in] IpSb The IP4 serviceing binding instance to clean up
125
126 @retval EFI_SUCCESS The resource used by the instance are cleaned up
127 @retval other Failed to clean up some of the resources.
128
129 **/
130 EFI_STATUS
131 Ip4CleanService (
132 IN IP4_SERVICE *IpSb
133 );
134
135
136 /**
137 Create a new IP4 driver service binding private instance.
138
139 @param Controller The controller that has MNP service binding
140 installed
141 @param ImageHandle The IP4 driver's image handle
142 @param Service The variable to receive the newly created IP4
143 service.
144
145 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
146 @retval EFI_SUCCESS A new IP4 service binding private is created.
147 @retval other Other error occurs.
148
149 **/
150 EFI_STATUS
151 Ip4CreateService (
152 IN EFI_HANDLE Controller,
153 IN EFI_HANDLE ImageHandle,
154 OUT IP4_SERVICE **Service
155 )
156 {
157 IP4_SERVICE *IpSb;
158 EFI_STATUS Status;
159
160 ASSERT (Service != NULL);
161
162 *Service = NULL;
163
164 //
165 // allocate a service private data then initialize all the filed to
166 // empty resources, so if any thing goes wrong when allocating
167 // resources, Ip4CleanService can be called to clean it up.
168 //
169 IpSb = AllocatePool (sizeof (IP4_SERVICE));
170
171 if (IpSb == NULL) {
172 return EFI_OUT_OF_RESOURCES;
173 }
174
175 IpSb->Signature = IP4_SERVICE_SIGNATURE;
176 IpSb->ServiceBinding.CreateChild = Ip4ServiceBindingCreateChild;
177 IpSb->ServiceBinding.DestroyChild = Ip4ServiceBindingDestroyChild;
178 IpSb->State = IP4_SERVICE_UNSTARTED;
179 IpSb->InDestory = FALSE;
180
181 IpSb->NumChildren = 0;
182 InitializeListHead (&IpSb->Children);
183
184 InitializeListHead (&IpSb->Interfaces);
185 IpSb->DefaultInterface = NULL;
186 IpSb->DefaultRouteTable = NULL;
187
188 Ip4InitAssembleTable (&IpSb->Assemble);
189
190 IpSb->IgmpCtrl.Igmpv1QuerySeen = 0;
191 InitializeListHead (&IpSb->IgmpCtrl.Groups);
192
193 IpSb->Image = ImageHandle;
194 IpSb->Controller = Controller;
195
196 IpSb->MnpChildHandle = NULL;
197 IpSb->Mnp = NULL;
198
199 IpSb->MnpConfigData.ReceivedQueueTimeoutValue = 0;
200 IpSb->MnpConfigData.TransmitQueueTimeoutValue = 0;
201 IpSb->MnpConfigData.ProtocolTypeFilter = IP4_ETHER_PROTO;
202 IpSb->MnpConfigData.EnableUnicastReceive = TRUE;
203 IpSb->MnpConfigData.EnableMulticastReceive = TRUE;
204 IpSb->MnpConfigData.EnableBroadcastReceive = TRUE;
205 IpSb->MnpConfigData.EnablePromiscuousReceive = FALSE;
206 IpSb->MnpConfigData.FlushQueuesOnReset = TRUE;
207 IpSb->MnpConfigData.EnableReceiveTimestamps = FALSE;
208 IpSb->MnpConfigData.DisableBackgroundPolling = FALSE;
209
210 ZeroMem (&IpSb->SnpMode, sizeof (EFI_SIMPLE_NETWORK_MODE));
211
212 IpSb->Timer = NULL;
213 IpSb->Ip4Config = NULL;
214 IpSb->DoneEvent = NULL;
215 IpSb->ReconfigEvent = NULL;
216 IpSb->ActiveEvent = NULL;
217
218 //
219 // Create various resources. First create the route table, timer
220 // event and MNP child. IGMP, interface's initialization depend
221 // on the MNP child.
222 //
223 IpSb->DefaultRouteTable = Ip4CreateRouteTable ();
224
225 if (IpSb->DefaultRouteTable == NULL) {
226 Status = EFI_OUT_OF_RESOURCES;
227 goto ON_ERROR;
228 }
229
230 Status = gBS->CreateEvent (
231 EVT_NOTIFY_SIGNAL | EVT_TIMER,
232 TPL_CALLBACK,
233 Ip4TimerTicking,
234 IpSb,
235 &IpSb->Timer
236 );
237
238 if (EFI_ERROR (Status)) {
239 goto ON_ERROR;
240 }
241
242 Status = NetLibCreateServiceChild (
243 Controller,
244 ImageHandle,
245 &gEfiManagedNetworkServiceBindingProtocolGuid,
246 &IpSb->MnpChildHandle
247 );
248
249 if (EFI_ERROR (Status)) {
250 goto ON_ERROR;
251 }
252
253 Status = gBS->OpenProtocol (
254 IpSb->MnpChildHandle,
255 &gEfiManagedNetworkProtocolGuid,
256 (VOID **) &IpSb->Mnp,
257 ImageHandle,
258 Controller,
259 EFI_OPEN_PROTOCOL_BY_DRIVER
260 );
261
262 if (EFI_ERROR (Status)) {
263 goto ON_ERROR;
264 }
265
266 Status = Ip4ServiceConfigMnp (IpSb, TRUE);
267
268 if (EFI_ERROR (Status)) {
269 goto ON_ERROR;
270 }
271
272 Status = IpSb->Mnp->GetModeData (IpSb->Mnp, NULL, &IpSb->SnpMode);
273
274 if (EFI_ERROR (Status)) {
275 goto ON_ERROR;
276 }
277
278 Status = Ip4InitIgmp (IpSb);
279
280 if (EFI_ERROR (Status)) {
281 goto ON_ERROR;
282 }
283
284 IpSb->DefaultInterface = Ip4CreateInterface (IpSb->Mnp, Controller, ImageHandle);
285
286 if (IpSb->DefaultInterface == NULL) {
287 Status = EFI_OUT_OF_RESOURCES;
288 goto ON_ERROR;
289 }
290
291 InsertHeadList (&IpSb->Interfaces, &IpSb->DefaultInterface->Link);
292
293 IpSb->MaxPacketSize = IpSb->SnpMode.MaxPacketSize - sizeof (IP4_HEAD);
294 if (NetLibGetVlanId (IpSb->Controller) != 0) {
295 //
296 // This is a VLAN device, reduce MTU by VLAN tag length
297 //
298 IpSb->MaxPacketSize -= NET_VLAN_TAG_LEN;
299 }
300 IpSb->MacString = NULL;
301
302 *Service = IpSb;
303 return EFI_SUCCESS;
304
305 ON_ERROR:
306 Ip4CleanService (IpSb);
307 FreePool (IpSb);
308
309 return Status;
310 }
311
312
313 /**
314 Clean up a IP4 service binding instance. It will release all
315 the resource allocated by the instance. The instance may be
316 partly initialized, or partly destroyed. If a resource is
317 destroyed, it is marked as that in case the destory failed and
318 being called again later.
319
320 @param[in] IpSb The IP4 serviceing binding instance to clean up
321
322 @retval EFI_SUCCESS The resource used by the instance are cleaned up
323 @retval other Failed to clean up some of the resources.
324
325 **/
326 EFI_STATUS
327 Ip4CleanService (
328 IN IP4_SERVICE *IpSb
329 )
330 {
331 EFI_STATUS Status;
332
333 if (IpSb->DefaultInterface != NULL) {
334 Status = Ip4FreeInterface (IpSb->DefaultInterface, NULL);
335
336 if (EFI_ERROR (Status)) {
337 return Status;
338 }
339
340 IpSb->DefaultInterface = NULL;
341 }
342
343 if (IpSb->DefaultRouteTable != NULL) {
344 Ip4FreeRouteTable (IpSb->DefaultRouteTable);
345 IpSb->DefaultRouteTable = NULL;
346 }
347
348 Ip4CleanAssembleTable (&IpSb->Assemble);
349
350 if (IpSb->MnpChildHandle != NULL) {
351 if (IpSb->Mnp != NULL) {
352 gBS->CloseProtocol (
353 IpSb->MnpChildHandle,
354 &gEfiManagedNetworkProtocolGuid,
355 IpSb->Image,
356 IpSb->Controller
357 );
358
359 IpSb->Mnp = NULL;
360 }
361
362 NetLibDestroyServiceChild (
363 IpSb->Controller,
364 IpSb->Image,
365 &gEfiManagedNetworkServiceBindingProtocolGuid,
366 IpSb->MnpChildHandle
367 );
368
369 IpSb->MnpChildHandle = NULL;
370 }
371
372 if (IpSb->Timer != NULL) {
373 gBS->SetTimer (IpSb->Timer, TimerCancel, 0);
374 gBS->CloseEvent (IpSb->Timer);
375
376 IpSb->Timer = NULL;
377 }
378
379 if (IpSb->Ip4Config != NULL) {
380 IpSb->Ip4Config->Stop (IpSb->Ip4Config);
381
382 gBS->CloseProtocol (
383 IpSb->Controller,
384 &gEfiIp4ConfigProtocolGuid,
385 IpSb->Image,
386 IpSb->Controller
387 );
388
389 gBS->CloseEvent (IpSb->DoneEvent);
390 gBS->CloseEvent (IpSb->ReconfigEvent);
391 IpSb->ActiveEvent = NULL;
392 IpSb->Ip4Config = NULL;
393 }
394
395 return EFI_SUCCESS;
396 }
397
398
399 /**
400 Start this driver on ControllerHandle. This service is called by the
401 EFI boot service ConnectController(). In order to make
402 drivers as small as possible, there are a few calling restrictions for
403 this service. ConnectController() must follow these
404 calling restrictions. If any other agent wishes to call Start() it
405 must also follow these calling restrictions.
406
407 @param[in] This Protocol instance pointer.
408 @param[in] ControllerHandle Handle of device to bind driver to
409 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
410 device to start.
411
412 @retval EFI_SUCCESS This driver is added to ControllerHandle
413 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
414 @retval other This driver does not support this device
415
416 **/
417 EFI_STATUS
418 EFIAPI
419 Ip4DriverBindingStart (
420 IN EFI_DRIVER_BINDING_PROTOCOL * This,
421 IN EFI_HANDLE ControllerHandle,
422 IN EFI_DEVICE_PATH_PROTOCOL * RemainingDevicePath OPTIONAL
423 )
424 {
425 IP4_SERVICE *IpSb;
426 EFI_STATUS Status;
427
428 //
429 // Test for the Ip4 service binding protocol
430 //
431 Status = gBS->OpenProtocol (
432 ControllerHandle,
433 &gEfiIp4ServiceBindingProtocolGuid,
434 NULL,
435 This->DriverBindingHandle,
436 ControllerHandle,
437 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
438 );
439
440 if (Status == EFI_SUCCESS) {
441 return EFI_ALREADY_STARTED;
442 }
443
444 Status = Ip4CreateService (ControllerHandle, This->DriverBindingHandle, &IpSb);
445
446 if (EFI_ERROR (Status)) {
447 return Status;
448 }
449 ASSERT (IpSb != NULL);
450
451 //
452 // Install the Ip4ServiceBinding Protocol onto ControlerHandle
453 //
454 Status = gBS->InstallMultipleProtocolInterfaces (
455 &ControllerHandle,
456 &gEfiIp4ServiceBindingProtocolGuid,
457 &IpSb->ServiceBinding,
458 NULL
459 );
460
461 if (EFI_ERROR (Status)) {
462 goto FREE_SERVICE;
463 }
464
465 //
466 // ready to go: start the receiving and timer
467 //
468 Status = Ip4ReceiveFrame (IpSb->DefaultInterface, NULL, Ip4AccpetFrame, IpSb);
469
470 if (EFI_ERROR (Status)) {
471 goto UNINSTALL_PROTOCOL;
472 }
473
474 Status = gBS->SetTimer (IpSb->Timer, TimerPeriodic, TICKS_PER_SECOND);
475
476 if (EFI_ERROR (Status)) {
477 goto UNINSTALL_PROTOCOL;
478 }
479
480 //
481 // Initialize the IP4 ID
482 //
483 mIp4Id = (UINT16)NET_RANDOM (NetRandomInitSeed ());
484
485 Ip4SetVariableData (IpSb);
486
487 return Status;
488
489 UNINSTALL_PROTOCOL:
490 gBS->UninstallProtocolInterface (
491 ControllerHandle,
492 &gEfiIp4ServiceBindingProtocolGuid,
493 &IpSb->ServiceBinding
494 );
495
496 FREE_SERVICE:
497 Ip4CleanService (IpSb);
498 FreePool (IpSb);
499
500 return Status;
501 }
502
503
504 /**
505 Stop this driver on ControllerHandle. This service is called by the
506 EFI boot service DisconnectController(). In order to
507 make drivers as small as possible, there are a few calling
508 restrictions for this service. DisconnectController()
509 must follow these calling restrictions. If any other agent wishes
510 to call Stop() it must also follow these calling restrictions.
511
512 @param[in] This Protocol instance pointer.
513 @param[in] ControllerHandle Handle of device to stop driver on
514 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number
515 of children is zero stop the entire bus driver.
516 @param[in] ChildHandleBuffer List of Child Handles to Stop.
517
518 @retval EFI_SUCCESS This driver is removed ControllerHandle
519 @retval other This driver was not removed from this device
520
521 **/
522 EFI_STATUS
523 EFIAPI
524 Ip4DriverBindingStop (
525 IN EFI_DRIVER_BINDING_PROTOCOL *This,
526 IN EFI_HANDLE ControllerHandle,
527 IN UINTN NumberOfChildren,
528 IN EFI_HANDLE *ChildHandleBuffer
529 )
530 {
531 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
532 IP4_SERVICE *IpSb;
533 IP4_PROTOCOL *IpInstance;
534 EFI_HANDLE NicHandle;
535 EFI_STATUS Status;
536 EFI_TPL OldTpl;
537 INTN State;
538 BOOLEAN IsArp;
539
540 //
541 // IP4 driver opens the MNP child, ARP children or the IP4_CONFIG protocol
542 // by driver. So the ControllerHandle may be the MNP child handle, ARP child
543 // handle, or the NIC (UNDI) handle because IP4_CONFIG protocol is installed
544 // in the NIC handle.
545 //
546 //
547 // First, check whether it is the IP4_CONFIG protocol being uninstalled.
548 // IP4_CONFIG protocol is installed on the NIC handle. It isn't necessary
549 // to clean up the default configuration if IP4_CONFIG is being stopped.
550 //
551 Status = gBS->OpenProtocol (
552 ControllerHandle,
553 &gEfiIp4ConfigProtocolGuid,
554 NULL,
555 This->DriverBindingHandle,
556 ControllerHandle,
557 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
558 );
559
560 if (Status == EFI_SUCCESS) {
561 //
562 // Retrieve the IP4 service binding protocol. If failed, it is
563 // likely that Ip4 ServiceBinding is uninstalled already. In this
564 // case, return immediately.
565 //
566 Status = gBS->OpenProtocol (
567 ControllerHandle,
568 &gEfiIp4ServiceBindingProtocolGuid,
569 (VOID **) &ServiceBinding,
570 This->DriverBindingHandle,
571 ControllerHandle,
572 EFI_OPEN_PROTOCOL_GET_PROTOCOL
573 );
574
575 if (EFI_ERROR (Status)) {
576 return EFI_DEVICE_ERROR;
577 }
578
579 IpSb = IP4_SERVICE_FROM_PROTOCOL (ServiceBinding);
580
581 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
582
583 if (IpSb->Ip4Config != NULL && (IpSb->State != IP4_SERVICE_DESTORY)) {
584
585 IpSb->Ip4Config->Stop (IpSb->Ip4Config);
586
587 Status = gBS->CloseProtocol (
588 ControllerHandle,
589 &gEfiIp4ConfigProtocolGuid,
590 IpSb->Image,
591 ControllerHandle
592 );
593
594 if (EFI_ERROR (Status)) {
595 gBS->RestoreTPL (OldTpl);
596 return Status;
597 }
598
599 //
600 // If the auto configure hasn't complete, mark it as not started.
601 //
602 if (IpSb->State == IP4_SERVICE_STARTED) {
603 IpSb->State = IP4_SERVICE_UNSTARTED;
604 }
605
606 IpSb->Ip4Config = NULL;
607 gBS->CloseEvent (IpSb->DoneEvent);
608 gBS->CloseEvent (IpSb->ReconfigEvent);
609 }
610
611 gBS->RestoreTPL (OldTpl);
612 return EFI_SUCCESS;
613 }
614
615 //
616 // Either MNP or ARP protocol is being uninstalled. The controller
617 // handle is either the MNP child or ARP child. But, the IP4's
618 // service binding is installed on the NIC handle. So, need to open
619 // the protocol info to find the NIC handle.
620 //
621 IsArp = FALSE;
622 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiManagedNetworkProtocolGuid);
623
624 if (NicHandle == NULL) {
625 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiArpProtocolGuid);
626 IsArp = TRUE;
627 }
628
629 if (NicHandle == NULL) {
630 return EFI_DEVICE_ERROR;
631 }
632
633 //
634 // Retrieve the IP4 service binding protocol
635 //
636 Status = gBS->OpenProtocol (
637 NicHandle,
638 &gEfiIp4ServiceBindingProtocolGuid,
639 (VOID **) &ServiceBinding,
640 This->DriverBindingHandle,
641 NicHandle,
642 EFI_OPEN_PROTOCOL_GET_PROTOCOL
643 );
644
645 if (EFI_ERROR (Status)) {
646 return EFI_DEVICE_ERROR;
647 }
648
649 IpSb = IP4_SERVICE_FROM_PROTOCOL (ServiceBinding);
650
651 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
652
653 if (IpSb->InDestory) {
654 gBS->RestoreTPL (OldTpl);
655 return EFI_SUCCESS;
656 }
657
658 if (IsArp) {
659 while (!IsListEmpty (&IpSb->Children)) {
660 IpInstance = NET_LIST_HEAD (&IpSb->Children, IP4_PROTOCOL, Link);
661
662 ServiceBinding->DestroyChild (ServiceBinding, IpInstance->Handle);
663 }
664
665 if (IpSb->NumChildren != 0) {
666 Status = EFI_DEVICE_ERROR;
667 goto ON_ERROR;
668 }
669
670 IpSb->InDestory = TRUE;
671
672 State = IpSb->State;
673 IpSb->State = IP4_SERVICE_DESTORY;
674
675 //
676 // Clear the variable data.
677 //
678 Ip4ClearVariableData (IpSb);
679
680 //
681 // OK, clean other resources then uninstall the service binding protocol.
682 //
683 Status = Ip4CleanService (IpSb);
684
685 if (EFI_ERROR (Status)) {
686 IpSb->State = State;
687 goto ON_ERROR;
688 }
689
690 gBS->UninstallProtocolInterface (
691 NicHandle,
692 &gEfiIp4ServiceBindingProtocolGuid,
693 ServiceBinding
694 );
695
696 FreePool (IpSb);
697 } else if (NumberOfChildren == 0) {
698 IpSb->InDestory = TRUE;
699
700 State = IpSb->State;
701 IpSb->State = IP4_SERVICE_DESTORY;
702
703 //
704 // Clear the variable data.
705 //
706 Ip4ClearVariableData (IpSb);
707
708 //
709 // OK, clean other resources then uninstall the service binding protocol.
710 //
711 Status = Ip4CleanService (IpSb);
712
713 if (EFI_ERROR (Status)) {
714 IpSb->State = State;
715 goto ON_ERROR;
716 }
717
718 gBS->UninstallProtocolInterface (
719 NicHandle,
720 &gEfiIp4ServiceBindingProtocolGuid,
721 ServiceBinding
722 );
723
724 FreePool (IpSb);
725 } else {
726
727 while (!IsListEmpty (&IpSb->Children)) {
728 IpInstance = NET_LIST_HEAD (&IpSb->Children, IP4_PROTOCOL, Link);
729
730 ServiceBinding->DestroyChild (ServiceBinding, IpInstance->Handle);
731 }
732
733 if (IpSb->NumChildren != 0) {
734 Status = EFI_DEVICE_ERROR;
735 }
736 }
737
738 ON_ERROR:
739
740 gBS->RestoreTPL (OldTpl);
741 return Status;
742 }
743
744
745 /**
746 Creates a child handle and installs a protocol.
747
748 The CreateChild() function installs a protocol on ChildHandle.
749 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
750 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
751
752 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
753 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
754 then a new handle is created. If it is a pointer to an existing UEFI handle,
755 then the protocol is added to the existing UEFI handle.
756
757 @retval EFI_SUCCES The protocol was added to ChildHandle.
758 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
759 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
760 the child
761 @retval other The child handle was not created
762
763 **/
764 EFI_STATUS
765 EFIAPI
766 Ip4ServiceBindingCreateChild (
767 IN EFI_SERVICE_BINDING_PROTOCOL *This,
768 IN OUT EFI_HANDLE *ChildHandle
769 )
770 {
771 IP4_SERVICE *IpSb;
772 IP4_PROTOCOL *IpInstance;
773 EFI_TPL OldTpl;
774 EFI_STATUS Status;
775 VOID *Mnp;
776
777 if ((This == NULL) || (ChildHandle == NULL)) {
778 return EFI_INVALID_PARAMETER;
779 }
780
781 IpSb = IP4_SERVICE_FROM_PROTOCOL (This);
782 IpInstance = AllocatePool (sizeof (IP4_PROTOCOL));
783
784 if (IpInstance == NULL) {
785 return EFI_OUT_OF_RESOURCES;
786 }
787
788 Ip4InitProtocol (IpSb, IpInstance);
789
790 //
791 // Install Ip4 onto ChildHandle
792 //
793 Status = gBS->InstallMultipleProtocolInterfaces (
794 ChildHandle,
795 &gEfiIp4ProtocolGuid,
796 &IpInstance->Ip4Proto,
797 NULL
798 );
799
800 if (EFI_ERROR (Status)) {
801 goto ON_ERROR;
802 }
803
804 IpInstance->Handle = *ChildHandle;
805
806 //
807 // Open the Managed Network protocol BY_CHILD.
808 //
809 Status = gBS->OpenProtocol (
810 IpSb->MnpChildHandle,
811 &gEfiManagedNetworkProtocolGuid,
812 (VOID **) &Mnp,
813 gIp4DriverBinding.DriverBindingHandle,
814 IpInstance->Handle,
815 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
816 );
817 if (EFI_ERROR (Status)) {
818 gBS->UninstallMultipleProtocolInterfaces (
819 ChildHandle,
820 &gEfiIp4ProtocolGuid,
821 &IpInstance->Ip4Proto,
822 NULL
823 );
824
825 goto ON_ERROR;
826 }
827
828 //
829 // Insert it into the service binding instance.
830 //
831 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
832
833 InsertTailList (&IpSb->Children, &IpInstance->Link);
834 IpSb->NumChildren++;
835
836 gBS->RestoreTPL (OldTpl);
837
838 ON_ERROR:
839
840 if (EFI_ERROR (Status)) {
841
842 Ip4CleanProtocol (IpInstance);
843
844 FreePool (IpInstance);
845 }
846
847 return Status;
848 }
849
850
851 /**
852 Destroys a child handle with a protocol installed on it.
853
854 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
855 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
856 last protocol on ChildHandle, then ChildHandle is destroyed.
857
858 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
859 @param ChildHandle Handle of the child to destroy
860
861 @retval EFI_SUCCES The protocol was removed from ChildHandle.
862 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
863 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.
864 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
865 because its services are being used.
866 @retval other The child handle was not destroyed
867
868 **/
869 EFI_STATUS
870 EFIAPI
871 Ip4ServiceBindingDestroyChild (
872 IN EFI_SERVICE_BINDING_PROTOCOL *This,
873 IN EFI_HANDLE ChildHandle
874 )
875 {
876 EFI_STATUS Status;
877 IP4_SERVICE *IpSb;
878 IP4_PROTOCOL *IpInstance;
879 EFI_IP4_PROTOCOL *Ip4;
880 EFI_TPL OldTpl;
881 INTN State;
882
883 if ((This == NULL) || (ChildHandle == NULL)) {
884 return EFI_INVALID_PARAMETER;
885 }
886
887 //
888 // Retrieve the private context data structures
889 //
890 IpSb = IP4_SERVICE_FROM_PROTOCOL (This);
891
892 Status = gBS->OpenProtocol (
893 ChildHandle,
894 &gEfiIp4ProtocolGuid,
895 (VOID **) &Ip4,
896 gIp4DriverBinding.DriverBindingHandle,
897 ChildHandle,
898 EFI_OPEN_PROTOCOL_GET_PROTOCOL
899 );
900
901 if (EFI_ERROR (Status)) {
902 return EFI_UNSUPPORTED;
903 }
904
905 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (Ip4);
906
907 if (IpInstance->Service != IpSb) {
908 return EFI_INVALID_PARAMETER;
909 }
910
911 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
912
913 //
914 // A child can be destoried more than once. For example,
915 // Ip4DriverBindingStop will destory all of its children.
916 // when UDP driver is being stopped, it will destory all
917 // the IP child it opens.
918 //
919 if (IpInstance->State == IP4_STATE_DESTORY) {
920 gBS->RestoreTPL (OldTpl);
921 return EFI_SUCCESS;
922 }
923
924 State = IpInstance->State;
925 IpInstance->State = IP4_STATE_DESTORY;
926
927 //
928 // Close the Managed Network protocol.
929 //
930 gBS->CloseProtocol (
931 IpSb->MnpChildHandle,
932 &gEfiManagedNetworkProtocolGuid,
933 gIp4DriverBinding.DriverBindingHandle,
934 ChildHandle
935 );
936
937 //
938 // Uninstall the IP4 protocol first. Many thing happens during
939 // this:
940 // 1. The consumer of the IP4 protocol will be stopped if it
941 // opens the protocol BY_DRIVER. For eaxmple, if MNP driver is
942 // stopped, IP driver's stop function will be called, and uninstall
943 // EFI_IP4_PROTOCOL will trigger the UDP's stop function. This
944 // makes it possible to create the network stack bottom up, and
945 // stop it top down.
946 // 2. the upper layer will recycle the received packet. The recycle
947 // event's TPL is higher than this function. The recycle events
948 // will be called back before preceeding. If any packets not recycled,
949 // that means there is a resource leak.
950 //
951 Status = gBS->UninstallProtocolInterface (
952 ChildHandle,
953 &gEfiIp4ProtocolGuid,
954 &IpInstance->Ip4Proto
955 );
956
957 if (EFI_ERROR (Status)) {
958 goto ON_ERROR;
959 }
960
961 Status = Ip4CleanProtocol (IpInstance);
962
963 Ip4SetVariableData (IpSb);
964
965 if (EFI_ERROR (Status)) {
966 gBS->InstallMultipleProtocolInterfaces (
967 &ChildHandle,
968 &gEfiIp4ProtocolGuid,
969 Ip4,
970 NULL
971 );
972
973 goto ON_ERROR;
974 }
975
976 RemoveEntryList (&IpInstance->Link);
977 IpSb->NumChildren--;
978
979 gBS->RestoreTPL (OldTpl);
980
981 FreePool (IpInstance);
982 return EFI_SUCCESS;
983
984 ON_ERROR:
985 IpInstance->State = State;
986 gBS->RestoreTPL (OldTpl);
987
988 return Status;
989 }