]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/Udp6Dxe/Udp6Driver.c
1. Add EFI_COMPONENT_NAME2_PROTOCOL support for UNDI driver.
[mirror_edk2.git] / NetworkPkg / Udp6Dxe / Udp6Driver.c
CommitLineData
a3bcde70
HT
1/** @file\r
2 Driver Binding functions and Service Binding functions for the Network driver module.\r
3\r
15ee13fc 4 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
a3bcde70
HT
5\r
6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php.\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "Udp6Impl.h"\r
17\r
18EFI_DRIVER_BINDING_PROTOCOL gUdp6DriverBinding = {\r
19 Udp6DriverBindingSupported,\r
20 Udp6DriverBindingStart,\r
21 Udp6DriverBindingStop,\r
22 0xa,\r
23 NULL,\r
24 NULL\r
25};\r
26\r
27EFI_SERVICE_BINDING_PROTOCOL mUdp6ServiceBinding = {\r
28 Udp6ServiceBindingCreateChild,\r
29 Udp6ServiceBindingDestroyChild\r
30};\r
31\r
32/**\r
33 Tests to see if this driver supports a given controller. If a child device is provided,\r
34 it further tests to see if this driver supports creating a handle for the specified child device.\r
35\r
36 This function checks to see if the driver specified by This supports the device specified by\r
37 ControllerHandle. Drivers will typically use the device path attached to\r
38 ControllerHandle and/or the services from the bus I/O abstraction attached to\r
39 ControllerHandle to determine if the driver supports ControllerHandle. This function\r
40 may be called many times during platform initialization. In order to reduce boot times, the tests\r
41 performed by this function must be very small, and take as little time as possible to execute. This\r
42 function must not change the state of any hardware devices, and this function must be aware that the\r
43 device specified by ControllerHandle may already be managed by the same driver or a\r
44 different driver. This function must match its calls to AllocatePages() with FreePages(),\r
45 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().\r
46 Because ControllerHandle may have been previously started by the same driver, if a protocol is\r
47 already in the opened state, then it must not be closed with CloseProtocol(). This is required\r
48 to guarantee the state of ControllerHandle is not modified by this function.\r
49\r
50 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
51 @param[in] ControllerHandle The handle of the controller to test. This handle\r
52 must support a protocol interface that supplies\r
53 an I/O abstraction to the driver.\r
54 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
55 parameter is ignored by device drivers, and is optional for bus\r
56 drivers. For bus drivers, if this parameter is not NULL, then\r
57 the bus driver must determine if the bus controller specified\r
58 by ControllerHandle and the child controller specified\r
59 by RemainingDevicePath are both supported by this\r
60 bus driver.\r
61\r
62 @retval EFI_SUCCESS The device specified by ControllerHandle and\r
63 RemainingDevicePath is supported by the driver specified by This.\r
64 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and\r
65 RemainingDevicePath is already being managed by the driver\r
66 specified by This.\r
67 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and\r
68 RemainingDevicePath is already being managed by a different\r
69 driver or an application that requires exclusive access.\r
70 Currently not implemented.\r
71 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and\r
72 RemainingDevicePath is not supported by the driver specified by This.\r
73**/\r
74EFI_STATUS\r
75EFIAPI\r
76Udp6DriverBindingSupported (\r
77 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
78 IN EFI_HANDLE ControllerHandle,\r
79 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
80 )\r
81{\r
82 EFI_STATUS Status;\r
83 //\r
84 // Test for the Udp6ServiceBinding Protocol\r
85 //\r
86 Status = gBS->OpenProtocol (\r
87 ControllerHandle,\r
88 &gEfiUdp6ServiceBindingProtocolGuid,\r
89 NULL,\r
90 This->DriverBindingHandle,\r
91 ControllerHandle,\r
92 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
93 );\r
94 if (!EFI_ERROR (Status)) {\r
95 return EFI_ALREADY_STARTED;\r
96 }\r
97 //\r
98 // Test for the Ip6ServiceBinding Protocol\r
99 //\r
100 Status = gBS->OpenProtocol (\r
101 ControllerHandle,\r
102 &gEfiIp6ServiceBindingProtocolGuid,\r
103 NULL,\r
104 This->DriverBindingHandle,\r
105 ControllerHandle,\r
106 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
107 );\r
108\r
109 return Status;\r
110}\r
111\r
112/**\r
113 Start this driver on ControllerHandle.\r
114\r
115 This service is called by the EFI boot service ConnectController(). In order to make\r
116 drivers as small as possible, there are a few calling restrictions for\r
117 this service. ConnectController() must follow these\r
118 calling restrictions. If any other agent wishes to call Start() it\r
119 must also follow these calling restrictions.\r
120\r
121 @param[in] This Protocol instance pointer.\r
122 @param[in] ControllerHandle Handle of device to bind the driver to.\r
123 @param[in] RemainingDevicePath Optional parameter use to pick a specific child\r
124 device to start.\r
125\r
126 @retval EFI_SUCCES This driver is added to ControllerHandle.\r
127 @retval EFI_OUT_OF_RESOURCES The required system resource can't be allocated.\r
128 @retval other This driver does not support this device.\r
129\r
130**/\r
131EFI_STATUS\r
132EFIAPI\r
133Udp6DriverBindingStart (\r
134 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
135 IN EFI_HANDLE ControllerHandle,\r
136 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
137 )\r
138{\r
139 EFI_STATUS Status;\r
140 UDP6_SERVICE_DATA *Udp6Service;\r
141\r
142 //\r
143 // Allocate Private Context Data Structure.\r
144 //\r
145 Udp6Service = AllocateZeroPool (sizeof (UDP6_SERVICE_DATA));\r
146 if (Udp6Service == NULL) {\r
147 Status = EFI_OUT_OF_RESOURCES;\r
148 goto EXIT;\r
149 }\r
150\r
151 Status = Udp6CreateService (Udp6Service, This->DriverBindingHandle, ControllerHandle);\r
152 if (EFI_ERROR (Status)) {\r
153 goto EXIT;\r
154 }\r
155\r
156 //\r
157 // Install the Udp6ServiceBindingProtocol on the ControllerHandle.\r
158 //\r
159 Status = gBS->InstallMultipleProtocolInterfaces (\r
160 &ControllerHandle,\r
161 &gEfiUdp6ServiceBindingProtocolGuid,\r
162 &Udp6Service->ServiceBinding,\r
163 NULL\r
164 );\r
165 if (EFI_ERROR (Status)) {\r
166 Udp6CleanService (Udp6Service);\r
167 goto EXIT;\r
168 } else {\r
169 Status = Udp6SetVariableData (Udp6Service);\r
170 }\r
171\r
172EXIT:\r
173 if (EFI_ERROR (Status)) {\r
174 if (Udp6Service != NULL) {\r
175 FreePool (Udp6Service);\r
176 }\r
177 }\r
178 return Status;\r
179}\r
180\r
181/**\r
182 Stop this driver on ControllerHandle.\r
183\r
184 This service is called by the EFI boot service DisconnectController(). In order to\r
185 make drivers as small as possible, there are a few calling\r
186 restrictions for this service. DisconnectController()\r
187 must follow these calling restrictions. If any other agent wishes\r
188 to call Stop(), it must also follow these calling restrictions.\r
189\r
190 @param[in] This Protocol instance pointer.\r
191 @param[in] ControllerHandle Handle of device to stop the driver on.\r
192 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If the number\r
193 of children is zero stop the entire bus driver.\r
194 @param[in] ChildHandleBuffer List of Child Handles to Stop. It is optional.\r
195\r
196 @retval EFI_SUCCES This driver is removed ControllerHandle.\r
197 @retval EFI_DEVICE_ERROR Can't find the NicHandle from the ControllerHandle and specified GUID.\r
198 @retval other This driver was not removed from this device.\r
199\r
200**/\r
201EFI_STATUS\r
202EFIAPI\r
203Udp6DriverBindingStop (\r
204 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
205 IN EFI_HANDLE ControllerHandle,\r
206 IN UINTN NumberOfChildren,\r
207 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL\r
208 )\r
209{\r
210 EFI_STATUS Status;\r
211 EFI_HANDLE NicHandle;\r
212 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;\r
213 UDP6_SERVICE_DATA *Udp6Service;\r
214 UDP6_INSTANCE_DATA *Instance;\r
215\r
216 //\r
217 // Find the NicHandle where UDP6 ServiceBinding Protocol is installed.\r
218 //\r
219 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiIp6ProtocolGuid);\r
220 if (NicHandle == NULL) {\r
221 return EFI_DEVICE_ERROR;\r
222 }\r
223\r
224 //\r
225 // Retrieve the UDP6 ServiceBinding Protocol.\r
226 //\r
227 Status = gBS->OpenProtocol (\r
228 NicHandle,\r
229 &gEfiUdp6ServiceBindingProtocolGuid,\r
230 (VOID **) &ServiceBinding,\r
231 This->DriverBindingHandle,\r
232 NicHandle,\r
233 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
234 );\r
235 if (EFI_ERROR (Status)) {\r
236 return EFI_DEVICE_ERROR;\r
237 }\r
238\r
239 Udp6Service = UDP6_SERVICE_DATA_FROM_THIS (ServiceBinding);\r
240\r
241 if (NumberOfChildren == 0) {\r
242\r
243 gBS->UninstallMultipleProtocolInterfaces (\r
244 NicHandle,\r
245 &gEfiUdp6ServiceBindingProtocolGuid,\r
246 &Udp6Service->ServiceBinding,\r
247 NULL\r
248 );\r
249\r
250 Udp6ClearVariableData (Udp6Service);\r
251\r
252 Udp6CleanService (Udp6Service);\r
253\r
254 FreePool (Udp6Service);\r
255 } else {\r
256\r
257 while (!IsListEmpty (&Udp6Service->ChildrenList)) {\r
258 Instance = NET_LIST_HEAD (&Udp6Service->ChildrenList, UDP6_INSTANCE_DATA, Link);\r
259\r
260 Status = ServiceBinding->DestroyChild (ServiceBinding, Instance->ChildHandle);\r
261 }\r
262 }\r
263\r
264 return Status;\r
265}\r
266\r
267/**\r
268 Creates a child handle and installs a protocol.\r
269\r
270 The CreateChild() function installs a protocol on ChildHandle.\r
271 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.\r
272 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.\r
273\r
274 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r
275 @param[in, out] ChildHandle Pointer to the handle of the child to create. If it is NULL,\r
276 then a new handle is created. If it is a pointer to an existing UEFI handle,\r
277 then the protocol is added to the existing UEFI handle.\r
278\r
279 @retval EFI_SUCCES The protocol was added to ChildHandle.\r
280 @retval EFI_INVALID_PARAMETER This is NULL or ChildHandle is NULL.\r
281 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create\r
282 the child.\r
283 @retval other The child handle was not created.\r
284\r
285**/\r
286EFI_STATUS\r
287EFIAPI\r
288Udp6ServiceBindingCreateChild (\r
289 IN EFI_SERVICE_BINDING_PROTOCOL *This,\r
290 IN OUT EFI_HANDLE *ChildHandle\r
291 )\r
292{\r
293 EFI_STATUS Status;\r
294 UDP6_SERVICE_DATA *Udp6Service;\r
295 UDP6_INSTANCE_DATA *Instance;\r
296 EFI_TPL OldTpl;\r
297 VOID *Ip6;\r
298\r
299 if ((This == NULL) || (ChildHandle == NULL)) {\r
300 return EFI_INVALID_PARAMETER;\r
301 }\r
302\r
303 Udp6Service = UDP6_SERVICE_DATA_FROM_THIS (This);\r
304\r
305 //\r
306 // Allocate the instance private data structure.\r
307 //\r
308 Instance = AllocateZeroPool (sizeof (UDP6_INSTANCE_DATA));\r
309 if (Instance == NULL) {\r
310 return EFI_OUT_OF_RESOURCES;\r
311 }\r
312\r
313 Udp6InitInstance (Udp6Service, Instance);\r
314\r
315 //\r
316 // Add an IpInfo for this instance.\r
317 //\r
318 Instance->IpInfo = IpIoAddIp (Udp6Service->IpIo);\r
319 if (Instance->IpInfo == NULL) {\r
320 Status = EFI_OUT_OF_RESOURCES;\r
321 goto ON_ERROR;\r
322 }\r
323\r
324 //\r
325 // Install the Udp6Protocol for this instance.\r
326 //\r
327 Status = gBS->InstallMultipleProtocolInterfaces (\r
328 ChildHandle,\r
329 &gEfiUdp6ProtocolGuid,\r
330 &Instance->Udp6Proto,\r
331 NULL\r
332 );\r
333 if (EFI_ERROR (Status)) {\r
334 goto ON_ERROR;\r
335 }\r
336\r
337 Instance->ChildHandle = *ChildHandle;\r
338\r
339 //\r
340 // Open the default Ip6 protocol in the IP_IO BY_CHILD.\r
341 //\r
342 Status = gBS->OpenProtocol (\r
343 Udp6Service->IpIo->ChildHandle,\r
344 &gEfiIp6ProtocolGuid,\r
345 (VOID **) &Ip6,\r
346 gUdp6DriverBinding.DriverBindingHandle,\r
347 Instance->ChildHandle,\r
348 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
349 );\r
350 if (EFI_ERROR (Status)) {\r
351 goto ON_ERROR;\r
352 }\r
353\r
354 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
355\r
356 //\r
357 // Link this instance into the service context data and increase the ChildrenNumber.\r
358 //\r
359 InsertTailList (&Udp6Service->ChildrenList, &Instance->Link);\r
360 Udp6Service->ChildrenNumber++;\r
361\r
362 gBS->RestoreTPL (OldTpl);\r
363\r
364 return EFI_SUCCESS;\r
365\r
366ON_ERROR:\r
367\r
368 if (Instance->ChildHandle != NULL) {\r
369 gBS->UninstallMultipleProtocolInterfaces (\r
370 Instance->ChildHandle,\r
371 &gEfiUdp6ProtocolGuid,\r
372 &Instance->Udp6Proto,\r
373 NULL\r
374 );\r
375 }\r
376\r
377 if (Instance->IpInfo != NULL) {\r
378 IpIoRemoveIp (Udp6Service->IpIo, Instance->IpInfo);\r
379 }\r
380\r
381 Udp6CleanInstance (Instance);\r
382\r
383 FreePool (Instance);\r
384\r
385 return Status;\r
386}\r
387\r
388/**\r
389 Destroys a child handle with a set of I/O services.\r
390 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol\r
391 that was installed by CreateChild() from ChildHandle. If the removed protocol is the\r
392 last protocol on ChildHandle, then ChildHandle is destroyed.\r
393\r
394 @param[in] This Protocol instance pointer.\r
395 @param[in] ChildHandle Handle of the child to destroy.\r
396\r
397 @retval EFI_SUCCES The I/O services were removed from the child\r
398 handle.\r
399 @retval EFI_UNSUPPORTED The child handle does not support the I/O services\r
400 that are being removed.\r
15ee13fc 401 @retval EFI_INVALID_PARAMETER Child handle is NULL.\r
a3bcde70
HT
402 @retval EFI_ACCESS_DENIED The child handle could not be destroyed because\r
403 its I/O services are being used.\r
404 @retval other The child handle was not destroyed.\r
405\r
406**/\r
407EFI_STATUS\r
408EFIAPI\r
409Udp6ServiceBindingDestroyChild (\r
410 IN EFI_SERVICE_BINDING_PROTOCOL *This,\r
411 IN EFI_HANDLE ChildHandle\r
412 )\r
413{\r
414 EFI_STATUS Status;\r
415 UDP6_SERVICE_DATA *Udp6Service;\r
416 EFI_UDP6_PROTOCOL *Udp6Proto;\r
417 UDP6_INSTANCE_DATA *Instance;\r
418 EFI_TPL OldTpl;\r
419\r
420 if ((This == NULL) || (ChildHandle == NULL)) {\r
421 return EFI_INVALID_PARAMETER;\r
422 }\r
423\r
424 Udp6Service = UDP6_SERVICE_DATA_FROM_THIS (This);\r
425\r
426 //\r
427 // Try to get the Udp6 protocol from the ChildHandle.\r
428 //\r
429 Status = gBS->OpenProtocol (\r
430 ChildHandle,\r
431 &gEfiUdp6ProtocolGuid,\r
432 (VOID **) &Udp6Proto,\r
433 gUdp6DriverBinding.DriverBindingHandle,\r
434 ChildHandle,\r
435 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
436 );\r
437 if (EFI_ERROR (Status)) {\r
438 return EFI_UNSUPPORTED;\r
439 }\r
440\r
441 Instance = UDP6_INSTANCE_DATA_FROM_THIS (Udp6Proto);\r
442\r
443 if (Instance->Destroyed) {\r
444 return EFI_SUCCESS;\r
445 }\r
446\r
447 //\r
448 // Use the Destroyed flag to avoid the re-entering of the following code.\r
449 //\r
450 Instance->Destroyed = TRUE;\r
451\r
452 //\r
453 // Close the Ip6 protocol.\r
454 //\r
455 gBS->CloseProtocol (\r
456 Udp6Service->IpIo->ChildHandle,\r
457 &gEfiIp6ProtocolGuid,\r
458 gUdp6DriverBinding.DriverBindingHandle,\r
459 Instance->ChildHandle\r
460 );\r
461\r
462 //\r
463 // Uninstall the Udp6Protocol previously installed on the ChildHandle.\r
464 //\r
465 Status = gBS->UninstallMultipleProtocolInterfaces (\r
466 ChildHandle,\r
467 &gEfiUdp6ProtocolGuid,\r
468 (VOID *) &Instance->Udp6Proto,\r
469 NULL\r
470 );\r
471 if (EFI_ERROR (Status)) {\r
472 Instance->Destroyed = FALSE;\r
473 return Status;\r
474 }\r
475\r
476 //\r
477 // Reset the configuration in case the instance's consumer forgets to do this.\r
478 //\r
479 Udp6Proto->Configure (Udp6Proto, NULL);\r
480\r
481 //\r
482 // Remove the IpInfo this instance consumes.\r
483 //\r
484 IpIoRemoveIp (Udp6Service->IpIo, Instance->IpInfo);\r
485\r
486 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
487\r
488 //\r
489 // Remove this instance from the service context data's ChildrenList.\r
490 //\r
491 RemoveEntryList (&Instance->Link);\r
492 Udp6Service->ChildrenNumber--;\r
493\r
494 //\r
495 // Clean the instance.\r
496 //\r
497 Udp6CleanInstance (Instance);\r
498\r
499 gBS->RestoreTPL (OldTpl);\r
500\r
501 FreePool (Instance);\r
502\r
503 return EFI_SUCCESS;\r
504}\r
505\r
506/**\r
507 This is the declaration of an EFI image entry point. This entry point is\r
508 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers, including\r
509 both device drivers and bus drivers.\r
510\r
511 The entry point for Udp6 driver that installs the driver binding\r
512 and component name protocol on its ImageHandle.\r
513\r
514 @param[in] ImageHandle The firmware allocated handle for the UEFI image.\r
515 @param[in] SystemTable A pointer to the EFI System Table.\r
516\r
517 @retval EFI_SUCCESS The operation completed successfully.\r
518 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
519\r
520**/\r
521EFI_STATUS\r
522EFIAPI\r
523Udp6DriverEntryPoint (\r
524 IN EFI_HANDLE ImageHandle,\r
525 IN EFI_SYSTEM_TABLE *SystemTable\r
526 )\r
527{\r
528 EFI_STATUS Status;\r
529\r
530 //\r
531 // Install the Udp6DriverBinding and Udp6ComponentName protocols.\r
532 //\r
533\r
534 Status = EfiLibInstallDriverBindingComponentName2 (\r
535 ImageHandle,\r
536 SystemTable,\r
537 &gUdp6DriverBinding,\r
538 ImageHandle,\r
539 &gUdp6ComponentName,\r
540 &gUdp6ComponentName2\r
541 );\r
542 if (!EFI_ERROR (Status)) {\r
543 //\r
544 // Initialize the UDP random port.\r
545 //\r
546 mUdp6RandomPort = (UINT16)(\r
547 ((UINT16) NetRandomInitSeed ()) %\r
548 UDP6_PORT_KNOWN +\r
549 UDP6_PORT_KNOWN\r
550 );\r
551 }\r
552\r
553 return Status;\r
554}\r
555\r
556\r