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