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