]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Driver.c
1b7fa4e60345009c1ec71ab9d37651ef1e500744
[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 - 2009, 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 IpSb->MacString = NULL;
295
296 *Service = IpSb;
297 return EFI_SUCCESS;
298
299 ON_ERROR:
300 Ip4CleanService (IpSb);
301 FreePool (IpSb);
302
303 return Status;
304 }
305
306
307 /**
308 Clean up a IP4 service binding instance. It will release all
309 the resource allocated by the instance. The instance may be
310 partly initialized, or partly destroyed. If a resource is
311 destroyed, it is marked as that in case the destory failed and
312 being called again later.
313
314 @param[in] IpSb The IP4 serviceing binding instance to clean up
315
316 @retval EFI_SUCCESS The resource used by the instance are cleaned up
317 @retval other Failed to clean up some of the resources.
318
319 **/
320 EFI_STATUS
321 Ip4CleanService (
322 IN IP4_SERVICE *IpSb
323 )
324 {
325 EFI_STATUS Status;
326
327 if (IpSb->DefaultInterface != NULL) {
328 Status = Ip4FreeInterface (IpSb->DefaultInterface, NULL);
329
330 if (EFI_ERROR (Status)) {
331 return Status;
332 }
333
334 IpSb->DefaultInterface = NULL;
335 }
336
337 if (IpSb->DefaultRouteTable != NULL) {
338 Ip4FreeRouteTable (IpSb->DefaultRouteTable);
339 IpSb->DefaultRouteTable = NULL;
340 }
341
342 Ip4CleanAssembleTable (&IpSb->Assemble);
343
344 if (IpSb->MnpChildHandle != NULL) {
345 if (IpSb->Mnp != NULL) {
346 gBS->CloseProtocol (
347 IpSb->MnpChildHandle,
348 &gEfiManagedNetworkProtocolGuid,
349 IpSb->Image,
350 IpSb->Controller
351 );
352
353 IpSb->Mnp = NULL;
354 }
355
356 NetLibDestroyServiceChild (
357 IpSb->Controller,
358 IpSb->Image,
359 &gEfiManagedNetworkServiceBindingProtocolGuid,
360 IpSb->MnpChildHandle
361 );
362
363 IpSb->MnpChildHandle = NULL;
364 }
365
366 if (IpSb->Timer != NULL) {
367 gBS->SetTimer (IpSb->Timer, TimerCancel, 0);
368 gBS->CloseEvent (IpSb->Timer);
369
370 IpSb->Timer = NULL;
371 }
372
373 if (IpSb->Ip4Config != NULL) {
374 IpSb->Ip4Config->Stop (IpSb->Ip4Config);
375
376 gBS->CloseProtocol (
377 IpSb->Controller,
378 &gEfiIp4ConfigProtocolGuid,
379 IpSb->Image,
380 IpSb->Controller
381 );
382
383 gBS->CloseEvent (IpSb->DoneEvent);
384 gBS->CloseEvent (IpSb->ReconfigEvent);
385 IpSb->ActiveEvent = NULL;
386 IpSb->Ip4Config = NULL;
387 }
388
389 return EFI_SUCCESS;
390 }
391
392
393 /**
394 Start this driver on ControllerHandle. This service is called by the
395 EFI boot service ConnectController(). In order to make
396 drivers as small as possible, there are a few calling restrictions for
397 this service. ConnectController() must follow these
398 calling restrictions. If any other agent wishes to call Start() it
399 must also follow these calling restrictions.
400
401 @param[in] This Protocol instance pointer.
402 @param[in] ControllerHandle Handle of device to bind driver to
403 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
404 device to start.
405
406 @retval EFI_SUCCESS This driver is added to ControllerHandle
407 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
408 @retval other This driver does not support this device
409
410 **/
411 EFI_STATUS
412 EFIAPI
413 Ip4DriverBindingStart (
414 IN EFI_DRIVER_BINDING_PROTOCOL * This,
415 IN EFI_HANDLE ControllerHandle,
416 IN EFI_DEVICE_PATH_PROTOCOL * RemainingDevicePath OPTIONAL
417 )
418 {
419 IP4_SERVICE *IpSb;
420 EFI_STATUS Status;
421
422 //
423 // Test for the Ip4 service binding protocol
424 //
425 Status = gBS->OpenProtocol (
426 ControllerHandle,
427 &gEfiIp4ServiceBindingProtocolGuid,
428 NULL,
429 This->DriverBindingHandle,
430 ControllerHandle,
431 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
432 );
433
434 if (Status == EFI_SUCCESS) {
435 return EFI_ALREADY_STARTED;
436 }
437
438 Status = Ip4CreateService (ControllerHandle, This->DriverBindingHandle, &IpSb);
439
440 if (EFI_ERROR (Status)) {
441 return Status;
442 }
443
444 //
445 // Install the Ip4ServiceBinding Protocol onto ControlerHandle
446 //
447 Status = gBS->InstallMultipleProtocolInterfaces (
448 &ControllerHandle,
449 &gEfiIp4ServiceBindingProtocolGuid,
450 &IpSb->ServiceBinding,
451 NULL
452 );
453
454 if (EFI_ERROR (Status)) {
455 goto FREE_SERVICE;
456 }
457
458 //
459 // ready to go: start the receiving and timer
460 //
461 Status = Ip4ReceiveFrame (IpSb->DefaultInterface, NULL, Ip4AccpetFrame, IpSb);
462
463 if (EFI_ERROR (Status)) {
464 goto UNINSTALL_PROTOCOL;
465 }
466
467 Status = gBS->SetTimer (IpSb->Timer, TimerPeriodic, TICKS_PER_SECOND);
468
469 if (EFI_ERROR (Status)) {
470 goto UNINSTALL_PROTOCOL;
471 }
472
473 //
474 // Initialize the IP4 ID
475 //
476 mIp4Id = (UINT16)NET_RANDOM (NetRandomInitSeed ());
477
478 Ip4SetVariableData (IpSb);
479
480 return Status;
481
482 UNINSTALL_PROTOCOL:
483 gBS->UninstallProtocolInterface (
484 ControllerHandle,
485 &gEfiIp4ServiceBindingProtocolGuid,
486 &IpSb->ServiceBinding
487 );
488
489 FREE_SERVICE:
490 Ip4CleanService (IpSb);
491 FreePool (IpSb);
492
493 return Status;
494 }
495
496
497 /**
498 Stop this driver on ControllerHandle. This service is called by the
499 EFI boot service DisconnectController(). In order to
500 make drivers as small as possible, there are a few calling
501 restrictions for this service. DisconnectController()
502 must follow these calling restrictions. If any other agent wishes
503 to call Stop() it must also follow these calling restrictions.
504
505 @param[in] This Protocol instance pointer.
506 @param[in] ControllerHandle Handle of device to stop driver on
507 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number
508 of children is zero stop the entire bus driver.
509 @param[in] ChildHandleBuffer List of Child Handles to Stop.
510
511 @retval EFI_SUCCESS This driver is removed ControllerHandle
512 @retval other This driver was not removed from this device
513
514 **/
515 EFI_STATUS
516 EFIAPI
517 Ip4DriverBindingStop (
518 IN EFI_DRIVER_BINDING_PROTOCOL *This,
519 IN EFI_HANDLE ControllerHandle,
520 IN UINTN NumberOfChildren,
521 IN EFI_HANDLE *ChildHandleBuffer
522 )
523 {
524 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
525 IP4_SERVICE *IpSb;
526 IP4_PROTOCOL *IpInstance;
527 EFI_HANDLE NicHandle;
528 EFI_STATUS Status;
529 EFI_TPL OldTpl;
530 INTN State;
531 BOOLEAN IsArp;
532
533 //
534 // IP4 driver opens the MNP child, ARP children or the IP4_CONFIG protocol
535 // by driver. So the ControllerHandle may be the MNP child handle, ARP child
536 // handle, or the NIC (UNDI) handle because IP4_CONFIG protocol is installed
537 // in the NIC handle.
538 //
539 //
540 // First, check whether it is the IP4_CONFIG protocol being uninstalled.
541 // IP4_CONFIG protocol is installed on the NIC handle. It isn't necessary
542 // to clean up the default configuration if IP4_CONFIG is being stopped.
543 //
544 Status = gBS->OpenProtocol (
545 ControllerHandle,
546 &gEfiIp4ConfigProtocolGuid,
547 NULL,
548 This->DriverBindingHandle,
549 ControllerHandle,
550 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
551 );
552
553 if (Status == EFI_SUCCESS) {
554 //
555 // Retrieve the IP4 service binding protocol. If failed, it is
556 // likely that Ip4 ServiceBinding is uninstalled already. In this
557 // case, return immediately.
558 //
559 Status = gBS->OpenProtocol (
560 ControllerHandle,
561 &gEfiIp4ServiceBindingProtocolGuid,
562 (VOID **) &ServiceBinding,
563 This->DriverBindingHandle,
564 ControllerHandle,
565 EFI_OPEN_PROTOCOL_GET_PROTOCOL
566 );
567
568 if (EFI_ERROR (Status)) {
569 return EFI_DEVICE_ERROR;
570 }
571
572 IpSb = IP4_SERVICE_FROM_PROTOCOL (ServiceBinding);
573
574 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
575
576 if (IpSb->Ip4Config != NULL && (IpSb->State != IP4_SERVICE_DESTORY)) {
577
578 IpSb->Ip4Config->Stop (IpSb->Ip4Config);
579
580 Status = gBS->CloseProtocol (
581 ControllerHandle,
582 &gEfiIp4ConfigProtocolGuid,
583 IpSb->Image,
584 ControllerHandle
585 );
586
587 if (EFI_ERROR (Status)) {
588 gBS->RestoreTPL (OldTpl);
589 return Status;
590 }
591
592 //
593 // If the auto configure hasn't complete, mark it as not started.
594 //
595 if (IpSb->State == IP4_SERVICE_STARTED) {
596 IpSb->State = IP4_SERVICE_UNSTARTED;
597 }
598
599 IpSb->Ip4Config = NULL;
600 gBS->CloseEvent (IpSb->DoneEvent);
601 gBS->CloseEvent (IpSb->ReconfigEvent);
602 }
603
604 gBS->RestoreTPL (OldTpl);
605 return EFI_SUCCESS;
606 }
607
608 //
609 // Either MNP or ARP protocol is being uninstalled. The controller
610 // handle is either the MNP child or ARP child. But, the IP4's
611 // service binding is installed on the NIC handle. So, need to open
612 // the protocol info to find the NIC handle.
613 //
614 IsArp = FALSE;
615 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiManagedNetworkProtocolGuid);
616
617 if (NicHandle == NULL) {
618 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiArpProtocolGuid);
619 IsArp = TRUE;
620 }
621
622 if (NicHandle == NULL) {
623 return EFI_DEVICE_ERROR;
624 }
625
626 //
627 // Retrieve the IP4 service binding protocol
628 //
629 Status = gBS->OpenProtocol (
630 NicHandle,
631 &gEfiIp4ServiceBindingProtocolGuid,
632 (VOID **) &ServiceBinding,
633 This->DriverBindingHandle,
634 NicHandle,
635 EFI_OPEN_PROTOCOL_GET_PROTOCOL
636 );
637
638 if (EFI_ERROR (Status)) {
639 return EFI_DEVICE_ERROR;
640 }
641
642 IpSb = IP4_SERVICE_FROM_PROTOCOL (ServiceBinding);
643
644 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
645
646 if (IpSb->InDestory) {
647 gBS->RestoreTPL (OldTpl);
648 return EFI_SUCCESS;
649 }
650
651 if (IsArp) {
652 while (!IsListEmpty (&IpSb->Children)) {
653 IpInstance = NET_LIST_HEAD (&IpSb->Children, IP4_PROTOCOL, Link);
654
655 ServiceBinding->DestroyChild (ServiceBinding, IpInstance->Handle);
656 }
657
658 if (IpSb->NumChildren != 0) {
659 Status = EFI_DEVICE_ERROR;
660 goto ON_ERROR;
661 }
662
663 IpSb->InDestory = TRUE;
664
665 State = IpSb->State;
666 IpSb->State = IP4_SERVICE_DESTORY;
667
668 //
669 // Clear the variable data.
670 //
671 Ip4ClearVariableData (IpSb);
672
673 //
674 // OK, clean other resources then uninstall the service binding protocol.
675 //
676 Status = Ip4CleanService (IpSb);
677
678 if (EFI_ERROR (Status)) {
679 IpSb->State = State;
680 goto ON_ERROR;
681 }
682
683 gBS->UninstallProtocolInterface (
684 NicHandle,
685 &gEfiIp4ServiceBindingProtocolGuid,
686 ServiceBinding
687 );
688
689 FreePool (IpSb);
690 } else if (NumberOfChildren == 0) {
691 IpSb->InDestory = TRUE;
692
693 State = IpSb->State;
694 IpSb->State = IP4_SERVICE_DESTORY;
695
696 //
697 // Clear the variable data.
698 //
699 Ip4ClearVariableData (IpSb);
700
701 //
702 // OK, clean other resources then uninstall the service binding protocol.
703 //
704 Status = Ip4CleanService (IpSb);
705
706 if (EFI_ERROR (Status)) {
707 IpSb->State = State;
708 goto ON_ERROR;
709 }
710
711 gBS->UninstallProtocolInterface (
712 NicHandle,
713 &gEfiIp4ServiceBindingProtocolGuid,
714 ServiceBinding
715 );
716
717 FreePool (IpSb);
718 } else {
719
720 while (!IsListEmpty (&IpSb->Children)) {
721 IpInstance = NET_LIST_HEAD (&IpSb->Children, IP4_PROTOCOL, Link);
722
723 ServiceBinding->DestroyChild (ServiceBinding, IpInstance->Handle);
724 }
725
726 if (IpSb->NumChildren != 0) {
727 Status = EFI_DEVICE_ERROR;
728 }
729 }
730
731 ON_ERROR:
732
733 gBS->RestoreTPL (OldTpl);
734 return Status;
735 }
736
737
738 /**
739 Creates a child handle and installs a protocol.
740
741 The CreateChild() function installs a protocol on ChildHandle.
742 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
743 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
744
745 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
746 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
747 then a new handle is created. If it is a pointer to an existing UEFI handle,
748 then the protocol is added to the existing UEFI handle.
749
750 @retval EFI_SUCCES The protocol was added to ChildHandle.
751 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
752 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
753 the child
754 @retval other The child handle was not created
755
756 **/
757 EFI_STATUS
758 EFIAPI
759 Ip4ServiceBindingCreateChild (
760 IN EFI_SERVICE_BINDING_PROTOCOL *This,
761 IN OUT EFI_HANDLE *ChildHandle
762 )
763 {
764 IP4_SERVICE *IpSb;
765 IP4_PROTOCOL *IpInstance;
766 EFI_TPL OldTpl;
767 EFI_STATUS Status;
768 VOID *Mnp;
769
770 if ((This == NULL) || (ChildHandle == NULL)) {
771 return EFI_INVALID_PARAMETER;
772 }
773
774 IpSb = IP4_SERVICE_FROM_PROTOCOL (This);
775 IpInstance = AllocatePool (sizeof (IP4_PROTOCOL));
776
777 if (IpInstance == NULL) {
778 return EFI_OUT_OF_RESOURCES;
779 }
780
781 Ip4InitProtocol (IpSb, IpInstance);
782
783 //
784 // Install Ip4 onto ChildHandle
785 //
786 Status = gBS->InstallMultipleProtocolInterfaces (
787 ChildHandle,
788 &gEfiIp4ProtocolGuid,
789 &IpInstance->Ip4Proto,
790 NULL
791 );
792
793 if (EFI_ERROR (Status)) {
794 goto ON_ERROR;
795 }
796
797 IpInstance->Handle = *ChildHandle;
798
799 //
800 // Open the Managed Network protocol BY_CHILD.
801 //
802 Status = gBS->OpenProtocol (
803 IpSb->MnpChildHandle,
804 &gEfiManagedNetworkProtocolGuid,
805 (VOID **) &Mnp,
806 gIp4DriverBinding.DriverBindingHandle,
807 IpInstance->Handle,
808 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
809 );
810 if (EFI_ERROR (Status)) {
811 gBS->UninstallMultipleProtocolInterfaces (
812 ChildHandle,
813 &gEfiIp4ProtocolGuid,
814 &IpInstance->Ip4Proto,
815 NULL
816 );
817
818 goto ON_ERROR;
819 }
820
821 //
822 // Insert it into the service binding instance.
823 //
824 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
825
826 InsertTailList (&IpSb->Children, &IpInstance->Link);
827 IpSb->NumChildren++;
828
829 gBS->RestoreTPL (OldTpl);
830
831 ON_ERROR:
832
833 if (EFI_ERROR (Status)) {
834
835 Ip4CleanProtocol (IpInstance);
836
837 FreePool (IpInstance);
838 }
839
840 return Status;
841 }
842
843
844 /**
845 Destroys a child handle with a protocol installed on it.
846
847 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
848 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
849 last protocol on ChildHandle, then ChildHandle is destroyed.
850
851 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
852 @param ChildHandle Handle of the child to destroy
853
854 @retval EFI_SUCCES The protocol was removed from ChildHandle.
855 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
856 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.
857 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
858 because its services are being used.
859 @retval other The child handle was not destroyed
860
861 **/
862 EFI_STATUS
863 EFIAPI
864 Ip4ServiceBindingDestroyChild (
865 IN EFI_SERVICE_BINDING_PROTOCOL *This,
866 IN EFI_HANDLE ChildHandle
867 )
868 {
869 EFI_STATUS Status;
870 IP4_SERVICE *IpSb;
871 IP4_PROTOCOL *IpInstance;
872 EFI_IP4_PROTOCOL *Ip4;
873 EFI_TPL OldTpl;
874 INTN State;
875
876 if ((This == NULL) || (ChildHandle == NULL)) {
877 return EFI_INVALID_PARAMETER;
878 }
879
880 //
881 // Retrieve the private context data structures
882 //
883 IpSb = IP4_SERVICE_FROM_PROTOCOL (This);
884
885 Status = gBS->OpenProtocol (
886 ChildHandle,
887 &gEfiIp4ProtocolGuid,
888 (VOID **) &Ip4,
889 gIp4DriverBinding.DriverBindingHandle,
890 ChildHandle,
891 EFI_OPEN_PROTOCOL_GET_PROTOCOL
892 );
893
894 if (EFI_ERROR (Status)) {
895 return EFI_UNSUPPORTED;
896 }
897
898 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (Ip4);
899
900 if (IpInstance->Service != IpSb) {
901 return EFI_INVALID_PARAMETER;
902 }
903
904 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
905
906 //
907 // A child can be destoried more than once. For example,
908 // Ip4DriverBindingStop will destory all of its children.
909 // when UDP driver is being stopped, it will destory all
910 // the IP child it opens.
911 //
912 if (IpInstance->State == IP4_STATE_DESTORY) {
913 gBS->RestoreTPL (OldTpl);
914 return EFI_SUCCESS;
915 }
916
917 State = IpInstance->State;
918 IpInstance->State = IP4_STATE_DESTORY;
919
920 //
921 // Close the Managed Network protocol.
922 //
923 gBS->CloseProtocol (
924 IpSb->MnpChildHandle,
925 &gEfiManagedNetworkProtocolGuid,
926 gIp4DriverBinding.DriverBindingHandle,
927 ChildHandle
928 );
929
930 //
931 // Uninstall the IP4 protocol first. Many thing happens during
932 // this:
933 // 1. The consumer of the IP4 protocol will be stopped if it
934 // opens the protocol BY_DRIVER. For eaxmple, if MNP driver is
935 // stopped, IP driver's stop function will be called, and uninstall
936 // EFI_IP4_PROTOCOL will trigger the UDP's stop function. This
937 // makes it possible to create the network stack bottom up, and
938 // stop it top down.
939 // 2. the upper layer will recycle the received packet. The recycle
940 // event's TPL is higher than this function. The recycle events
941 // will be called back before preceeding. If any packets not recycled,
942 // that means there is a resource leak.
943 //
944 Status = gBS->UninstallProtocolInterface (
945 ChildHandle,
946 &gEfiIp4ProtocolGuid,
947 &IpInstance->Ip4Proto
948 );
949
950 if (EFI_ERROR (Status)) {
951 goto ON_ERROR;
952 }
953
954 Status = Ip4CleanProtocol (IpInstance);
955
956 Ip4SetVariableData (IpSb);
957
958 if (EFI_ERROR (Status)) {
959 gBS->InstallMultipleProtocolInterfaces (
960 &ChildHandle,
961 &gEfiIp4ProtocolGuid,
962 Ip4,
963 NULL
964 );
965
966 goto ON_ERROR;
967 }
968
969 RemoveEntryList (&IpInstance->Link);
970 IpSb->NumChildren--;
971
972 gBS->RestoreTPL (OldTpl);
973
974 FreePool (IpInstance);
975 return EFI_SUCCESS;
976
977 ON_ERROR:
978 IpInstance->State = State;
979 gBS->RestoreTPL (OldTpl);
980
981 return Status;
982 }