4 Copyright (c) 2005 - 2013, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php<BR>
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 UINT16 mTcp4RandomPort
;
19 extern EFI_COMPONENT_NAME_PROTOCOL gTcp4ComponentName
;
20 extern EFI_COMPONENT_NAME2_PROTOCOL gTcp4ComponentName2
;
21 extern EFI_UNICODE_STRING_TABLE
*gTcpControllerNameTable
;
23 TCP4_HEARTBEAT_TIMER mTcp4Timer
= {
28 EFI_TCP4_PROTOCOL mTcp4ProtocolTemplate
= {
41 SOCK_INIT_DATA mTcp4DefaultSockData
= {
48 &mTcp4ProtocolTemplate
,
49 Tcp4CreateSocketCallback
,
50 Tcp4DestroySocketCallback
,
58 EFI_DRIVER_BINDING_PROTOCOL mTcp4DriverBinding
= {
59 Tcp4DriverBindingSupported
,
60 Tcp4DriverBindingStart
,
61 Tcp4DriverBindingStop
,
67 EFI_SERVICE_BINDING_PROTOCOL mTcp4ServiceBinding
= {
68 Tcp4ServiceBindingCreateChild
,
69 Tcp4ServiceBindingDestroyChild
74 Create and start the heartbeat timer for TCP driver.
76 @retval EFI_SUCCESS The timer is successfully created and started.
77 @retval other The timer is not created.
89 if (mTcp4Timer
.RefCnt
== 0) {
91 Status
= gBS
->CreateEvent (
92 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
96 &mTcp4Timer
.TimerEvent
98 if (!EFI_ERROR (Status
)) {
100 Status
= gBS
->SetTimer (
101 mTcp4Timer
.TimerEvent
,
103 (UINT64
) (TICKS_PER_SECOND
/ TCP_TICK_HZ
)
108 if (!EFI_ERROR (Status
)) {
118 Stop and destroy the heartbeat timer for TCP driver.
126 ASSERT (mTcp4Timer
.RefCnt
> 0);
130 if (mTcp4Timer
.RefCnt
> 0) {
134 gBS
->SetTimer (mTcp4Timer
.TimerEvent
, TimerCancel
, 0);
135 gBS
->CloseEvent (mTcp4Timer
.TimerEvent
);
136 mTcp4Timer
.TimerEvent
= NULL
;
140 Callback function which provided by user to remove one node in NetDestroyLinkList process.
142 @param[in] Entry The entry to be removed.
143 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
145 @retval EFI_SUCCESS The entry has been removed successfully.
146 @retval Others Fail to remove the entry.
151 Tcp4DestroyChildEntryInHandleBuffer (
152 IN LIST_ENTRY
*Entry
,
157 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
158 UINTN NumberOfChildren
;
159 EFI_HANDLE
*ChildHandleBuffer
;
161 if (Entry
== NULL
|| Context
== NULL
) {
162 return EFI_INVALID_PARAMETER
;
165 Sock
= NET_LIST_USER_STRUCT_S (Entry
, SOCKET
, Link
, SOCK_SIGNATURE
);
166 ServiceBinding
= ((TCP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
*) Context
)->ServiceBinding
;
167 NumberOfChildren
= ((TCP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
*) Context
)->NumberOfChildren
;
168 ChildHandleBuffer
= ((TCP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
*) Context
)->ChildHandleBuffer
;
170 if (!NetIsInHandleBuffer (Sock
->SockHandle
, NumberOfChildren
, ChildHandleBuffer
)) {
174 return ServiceBinding
->DestroyChild (ServiceBinding
, Sock
->SockHandle
);
178 The entry point for Tcp4 driver, used to install Tcp4 driver on the ImageHandle.
180 @param ImageHandle The firmware allocated handle for this
182 @param SystemTable Pointer to the EFI system table.
184 @retval EFI_SUCCESS Driver loaded.
185 @retval other Driver not loaded.
190 Tcp4DriverEntryPoint (
191 IN EFI_HANDLE ImageHandle
,
192 IN EFI_SYSTEM_TABLE
*SystemTable
199 // Install the TCP4 Driver Binding Protocol
201 Status
= EfiLibInstallDriverBindingComponentName2 (
209 ASSERT_EFI_ERROR (Status
);
211 // Initialize ISS and random port.
213 Seed
= NetRandomInitSeed ();
214 mTcpGlobalIss
= NET_RANDOM (Seed
) % mTcpGlobalIss
;
215 mTcp4RandomPort
= (UINT16
) (TCP4_PORT_KNOWN
+
216 (UINT16
) (NET_RANDOM(Seed
) % TCP4_PORT_KNOWN
));
223 Tests to see if this driver supports a given controller.
225 If a child device is provided, it further tests to see if this driver supports
226 creating a handle for the specified child device.
228 @param This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
229 @param ControllerHandle The handle of the controller to test. This handle
230 must support a protocol interface that supplies
231 an I/O abstraction to the driver.
232 @param RemainingDevicePath A pointer to the remaining portion of a device path.
233 This parameter is ignored by device drivers, and is optional for bus drivers.
236 @retval EFI_SUCCESS The device specified by ControllerHandle and
237 RemainingDevicePath is supported by the driver
239 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
240 RemainingDevicePath is already being managed by
241 the driver specified by This.
242 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
243 RemainingDevicePath is already being managed by a
244 different driver or an application that requires
246 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
247 RemainingDevicePath is not supported by the driver
253 Tcp4DriverBindingSupported (
254 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
255 IN EFI_HANDLE ControllerHandle
,
256 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
262 // Test for the Tcp4ServiceBinding Protocol
264 Status
= gBS
->OpenProtocol (
266 &gEfiTcp4ServiceBindingProtocolGuid
,
268 This
->DriverBindingHandle
,
270 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
272 if (!EFI_ERROR (Status
)) {
273 return EFI_ALREADY_STARTED
;
277 // Test for the Ip4 Protocol
279 Status
= gBS
->OpenProtocol (
281 &gEfiIp4ServiceBindingProtocolGuid
,
283 This
->DriverBindingHandle
,
285 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
293 Start this driver on ControllerHandle.
295 The Start() function is designed to be invoked from the EFI boot service
296 ConnectController(). As a result, much of the error checking on the parameters
297 to Start() has been moved into this common boot service. It is legal to call
298 Start() from other locations, but the following calling restrictions must be
299 followed or the system behavior will not be deterministic.
300 1. ControllerHandle must be a valid EFI_HANDLE.
301 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally
302 aligned EFI_DEVICE_PATH_PROTOCOL.
303 3. Prior to calling Start(), the Supported() function for the driver specified
304 by This must have been called with the same calling parameters, and Supported()
305 must have returned EFI_SUCCESS.
307 @param This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
308 @param ControllerHandle The handle of the controller to start. This handle
309 must support a protocol interface that supplies
310 an I/O abstraction to the driver.
311 @param RemainingDevicePath A pointer to the remaining portion of a device path.
312 This parameter is ignored by device drivers, and is
313 optional for bus drivers.
315 @retval EFI_SUCCESS The device was started.
316 @retval EFI_ALREADY_STARTED The device could not be started due to a device error.
317 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
323 Tcp4DriverBindingStart (
324 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
325 IN EFI_HANDLE ControllerHandle
,
326 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
330 TCP4_SERVICE_DATA
*TcpServiceData
;
331 IP_IO_OPEN_DATA OpenData
;
333 TcpServiceData
= AllocateZeroPool (sizeof (TCP4_SERVICE_DATA
));
335 if (NULL
== TcpServiceData
) {
336 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingStart: Have no enough"
337 " resource to create a Tcp Servcie Data\n"));
339 return EFI_OUT_OF_RESOURCES
;
343 // Create a new IP IO to Consume it
345 TcpServiceData
->IpIo
= IpIoCreate (
346 This
->DriverBindingHandle
,
350 if (NULL
== TcpServiceData
->IpIo
) {
352 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingStart: Have no enough"
353 " resource to create an Ip Io\n"));
355 Status
= EFI_OUT_OF_RESOURCES
;
360 // Configure and start IpIo.
362 ZeroMem (&OpenData
, sizeof (IP_IO_OPEN_DATA
));
365 &OpenData
.IpConfigData
.Ip4CfgData
,
366 &mIp4IoDefaultIpConfigData
,
367 sizeof (EFI_IP4_CONFIG_DATA
)
370 OpenData
.IpConfigData
.Ip4CfgData
.DefaultProtocol
= EFI_IP_PROTO_TCP
;
372 OpenData
.PktRcvdNotify
= Tcp4RxCallback
;
373 Status
= IpIoOpen (TcpServiceData
->IpIo
, &OpenData
);
375 if (EFI_ERROR (Status
)) {
380 // Create the timer event used by TCP driver
382 Status
= Tcp4CreateTimer ();
383 if (EFI_ERROR (Status
)) {
385 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingStart: Create TcpTimer"
386 " Event failed with %r\n", Status
));
392 // Install the Tcp4ServiceBinding Protocol on the
395 TcpServiceData
->Tcp4ServiceBinding
= mTcp4ServiceBinding
;
397 Status
= gBS
->InstallMultipleProtocolInterfaces (
399 &gEfiTcp4ServiceBindingProtocolGuid
,
400 &TcpServiceData
->Tcp4ServiceBinding
,
403 if (EFI_ERROR (Status
)) {
405 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingStart: Install Tcp4 Service Binding"
406 " Protocol failed for %r\n", Status
));
413 // Initialize member in TcpServiceData
415 TcpServiceData
->ControllerHandle
= ControllerHandle
;
416 TcpServiceData
->Signature
= TCP4_DRIVER_SIGNATURE
;
417 TcpServiceData
->DriverBindingHandle
= This
->DriverBindingHandle
;
419 InitializeListHead (&TcpServiceData
->SocketList
);
421 TcpSetVariableData (TcpServiceData
);
427 if (TcpServiceData
->IpIo
!= NULL
) {
428 IpIoDestroy (TcpServiceData
->IpIo
);
429 TcpServiceData
->IpIo
= NULL
;
432 FreePool (TcpServiceData
);
439 Stop this driver on ControllerHandle.
441 The Stop() function is designed to be invoked from the EFI boot service
442 DisconnectController(). As a result, much of the error checking on the parameters
443 to Stop() has been moved into this common boot service. It is legal to call Stop()
444 from other locations, but the following calling restrictions must be followed
445 or the system behavior will not be deterministic.
446 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call
447 to this same driver's Start() function.
448 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
449 EFI_HANDLE. In addition, all of these handles must have been created in this
450 driver's Start() function, and the Start() function must have called OpenProtocol()
451 on ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
453 @param This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
454 @param ControllerHandle A handle to the device being stopped. The handle must
455 support a bus specific I/O protocol for the driver
456 to use to stop the device.
457 @param NumberOfChildren The number of child device handles in ChildHandleBuffer.
458 @param ChildHandleBuffer An array of child handles to be freed. May be NULL if
459 NumberOfChildren is 0.
461 @retval EFI_SUCCESS The device was stopped.
462 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
467 Tcp4DriverBindingStop (
468 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
469 IN EFI_HANDLE ControllerHandle
,
470 IN UINTN NumberOfChildren
,
471 IN EFI_HANDLE
*ChildHandleBuffer
475 EFI_HANDLE NicHandle
;
476 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
477 TCP4_SERVICE_DATA
*TcpServiceData
;
479 TCP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context
;
481 // Find the NicHandle where Tcp4 ServiceBinding Protocol is installed.
483 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiIp4ProtocolGuid
);
484 if (NicHandle
== NULL
) {
489 // Retrieve the TCP driver Data Structure
491 Status
= gBS
->OpenProtocol (
493 &gEfiTcp4ServiceBindingProtocolGuid
,
494 (VOID
**) &ServiceBinding
,
495 This
->DriverBindingHandle
,
497 EFI_OPEN_PROTOCOL_GET_PROTOCOL
499 if (EFI_ERROR (Status
)) {
501 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingStop: Locate Tcp4 Service "
502 " Binding Protocol failed with %r\n", Status
));
504 return EFI_DEVICE_ERROR
;
507 TcpServiceData
= TCP4_FROM_THIS (ServiceBinding
);
509 if (NumberOfChildren
!= 0) {
510 List
= &TcpServiceData
->SocketList
;
511 Context
.ServiceBinding
= ServiceBinding
;
512 Context
.NumberOfChildren
= NumberOfChildren
;
513 Context
.ChildHandleBuffer
= ChildHandleBuffer
;
514 Status
= NetDestroyLinkList (
516 Tcp4DestroyChildEntryInHandleBuffer
,
520 } else if (IsListEmpty (&TcpServiceData
->SocketList
)) {
522 // Uninstall TCP servicebinding protocol
524 gBS
->UninstallMultipleProtocolInterfaces (
526 &gEfiTcp4ServiceBindingProtocolGuid
,
532 // Destroy the IpIO consumed by TCP driver
534 IpIoDestroy (TcpServiceData
->IpIo
);
535 TcpServiceData
->IpIo
= NULL
;
538 // Destroy the heartbeat timer.
543 // Clear the variable.
545 TcpClearVariableData (TcpServiceData
);
547 if (gTcpControllerNameTable
!= NULL
) {
548 FreeUnicodeStringTable (gTcpControllerNameTable
);
549 gTcpControllerNameTable
= NULL
;
553 // Release the TCP service data
555 FreePool (TcpServiceData
);
557 Status
= EFI_SUCCESS
;
564 Open Ip4 and device path protocols for a created socket, and insert it in
567 @param This Pointer to the socket just created
568 @param Context Context of the socket
570 @retval EFI_SUCCESS This protocol is installed successfully.
571 @retval other Some error occured.
575 Tcp4CreateSocketCallback (
581 TCP4_SERVICE_DATA
*TcpServiceData
;
582 EFI_IP4_PROTOCOL
*Ip4
;
584 TcpServiceData
= ((TCP4_PROTO_DATA
*) This
->ProtoReserved
)->TcpService
;
587 // Open the default Ip4 protocol of IP_IO BY_DRIVER.
589 Status
= gBS
->OpenProtocol (
590 TcpServiceData
->IpIo
->ChildHandle
,
591 &gEfiIp4ProtocolGuid
,
593 TcpServiceData
->DriverBindingHandle
,
595 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
597 if (EFI_ERROR (Status
)) {
602 // Open the device path on the handle where service binding resides on.
604 Status
= gBS
->OpenProtocol (
605 TcpServiceData
->ControllerHandle
,
606 &gEfiDevicePathProtocolGuid
,
607 (VOID
**) &This
->ParentDevicePath
,
608 TcpServiceData
->DriverBindingHandle
,
610 EFI_OPEN_PROTOCOL_GET_PROTOCOL
612 if (EFI_ERROR (Status
)) {
614 TcpServiceData
->IpIo
->ChildHandle
,
615 &gEfiIp4ProtocolGuid
,
616 TcpServiceData
->DriverBindingHandle
,
621 // Insert this socket into the SocketList.
623 InsertTailList (&TcpServiceData
->SocketList
, &This
->Link
);
630 Close Ip4 and device path protocols for a socket, and remove it from socket list.
632 @param This Pointer to the socket to be removed
633 @param Context Context of the socket
637 Tcp4DestroySocketCallback (
642 TCP4_SERVICE_DATA
*TcpServiceData
;
644 TcpServiceData
= ((TCP4_PROTO_DATA
*) This
->ProtoReserved
)->TcpService
;
647 // Remove this node from the list.
649 RemoveEntryList (&This
->Link
);
652 // Close the Ip4 protocol.
655 TcpServiceData
->IpIo
->ChildHandle
,
656 &gEfiIp4ProtocolGuid
,
657 TcpServiceData
->DriverBindingHandle
,
663 Creates a child handle and installs a protocol.
665 The CreateChild() function installs a protocol on ChildHandle. If ChildHandle
666 is a pointer to NULL, then a new handle is created and returned in ChildHandle.
667 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing
670 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
671 @param ChildHandle Pointer to the handle of the child to create. If it is NULL, then
672 a new handle is created. If it is a pointer to an existing UEFI
673 handle, then the protocol is added to the existing UEFI handle.
675 @retval EFI_SUCCES The protocol was added to ChildHandle.
676 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
677 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
679 @retval other The child handle was not created.
684 Tcp4ServiceBindingCreateChild (
685 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
686 IN OUT EFI_HANDLE
*ChildHandle
690 TCP4_SERVICE_DATA
*TcpServiceData
;
691 TCP4_PROTO_DATA TcpProto
;
695 if (NULL
== This
|| NULL
== ChildHandle
) {
696 return EFI_INVALID_PARAMETER
;
699 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
700 Status
= EFI_SUCCESS
;
701 TcpServiceData
= TCP4_FROM_THIS (This
);
702 TcpProto
.TcpService
= TcpServiceData
;
703 TcpProto
.TcpPcb
= NULL
;
706 // Create a tcp instance with defualt Tcp default
707 // sock init data and TcpProto
709 mTcp4DefaultSockData
.ProtoData
= &TcpProto
;
710 mTcp4DefaultSockData
.DataSize
= sizeof (TCP4_PROTO_DATA
);
711 mTcp4DefaultSockData
.DriverBinding
= TcpServiceData
->DriverBindingHandle
;
713 Sock
= SockCreateChild (&mTcp4DefaultSockData
);
715 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingCreateChild: "
716 "No resource to create a Tcp Child\n"));
718 Status
= EFI_OUT_OF_RESOURCES
;
720 *ChildHandle
= Sock
->SockHandle
;
723 mTcp4DefaultSockData
.ProtoData
= NULL
;
725 gBS
->RestoreTPL (OldTpl
);
731 Destroys a child handle with a protocol installed on it.
733 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
734 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
735 last protocol on ChildHandle, then ChildHandle is destroyed.
737 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
738 @param ChildHandle Handle of the child to destroy
740 @retval EFI_SUCCES The protocol was removed from ChildHandle.
741 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is
743 @retval EFI_INVALID_PARAMETER Child handle is NULL.
744 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
745 because its services are being used.
746 @retval other The child handle was not destroyed.
751 Tcp4ServiceBindingDestroyChild (
752 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
753 IN EFI_HANDLE ChildHandle
757 EFI_TCP4_PROTOCOL
*Tcp4
;
760 if (NULL
== This
|| NULL
== ChildHandle
) {
761 return EFI_INVALID_PARAMETER
;
765 // retrieve the Tcp4 protocol from ChildHandle
767 Status
= gBS
->OpenProtocol (
769 &gEfiTcp4ProtocolGuid
,
771 mTcp4DriverBinding
.DriverBindingHandle
,
773 EFI_OPEN_PROTOCOL_GET_PROTOCOL
775 if (EFI_ERROR (Status
)) {
776 Status
= EFI_UNSUPPORTED
;
779 // destroy this sock and related Tcp protocol control
782 Sock
= SOCK_FROM_THIS (Tcp4
);
784 SockDestroyChild (Sock
);