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