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