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