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