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