3 Copyright (c) 2005 - 2007, Intel Corporation<BR>
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php<BR>
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 UINT16 mTcp4RandomPort
;
18 extern EFI_COMPONENT_NAME_PROTOCOL gTcp4ComponentName
;
19 extern EFI_COMPONENT_NAME2_PROTOCOL gTcp4ComponentName2
;
21 TCP4_HEARTBEAT_TIMER mTcp4Timer
= {
26 EFI_TCP4_PROTOCOL mTcp4ProtocolTemplate
= {
39 SOCK_INIT_DATA mTcp4DefaultSockData
= {
46 &mTcp4ProtocolTemplate
,
47 Tcp4CreateSocketCallback
,
48 Tcp4DestroySocketCallback
,
56 EFI_DRIVER_BINDING_PROTOCOL mTcp4DriverBinding
= {
57 Tcp4DriverBindingSupported
,
58 Tcp4DriverBindingStart
,
59 Tcp4DriverBindingStop
,
65 EFI_SERVICE_BINDING_PROTOCOL mTcp4ServiceBinding
= {
66 Tcp4ServiceBindingCreateChild
,
67 Tcp4ServiceBindingDestroyChild
72 Create and start the heartbeat timer for TCP driver.
74 @retval EFI_SUCCESS The timer is successfully created and started.
75 @retval other The timer is not created.
87 if (mTcp4Timer
.RefCnt
== 0) {
89 Status
= gBS
->CreateEvent (
90 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
94 &mTcp4Timer
.TimerEvent
96 if (!EFI_ERROR (Status
)) {
98 Status
= gBS
->SetTimer (
99 mTcp4Timer
.TimerEvent
,
101 (UINT64
) (TICKS_PER_SECOND
/ TCP_TICK_HZ
)
106 if (!EFI_ERROR (Status
)) {
116 Stop and destroy the heartbeat timer for TCP driver.
124 ASSERT (mTcp4Timer
.RefCnt
> 0);
128 if (mTcp4Timer
.RefCnt
> 0) {
132 gBS
->SetTimer (mTcp4Timer
.TimerEvent
, TimerCancel
, 0);
133 gBS
->CloseEvent (mTcp4Timer
.TimerEvent
);
134 mTcp4Timer
.TimerEvent
= NULL
;
138 The entry point for Tcp4 driver, used to install Tcp4 driver on the ImageHandle.
140 @param ImageHandle The firmware allocated handle for this
142 @param SystemTable Pointer to the EFI system table.
144 @retval EFI_SUCCESS Driver loaded.
145 @retval other Driver not loaded.
150 Tcp4DriverEntryPoint (
151 IN EFI_HANDLE ImageHandle
,
152 IN EFI_SYSTEM_TABLE
*SystemTable
159 // Install the TCP4 Driver Binding Protocol
161 Status
= EfiLibInstallDriverBindingComponentName2 (
169 ASSERT_EFI_ERROR (Status
);
171 // Initialize ISS and random port.
173 Seed
= NetRandomInitSeed ();
174 mTcpGlobalIss
= NET_RANDOM (Seed
) % mTcpGlobalIss
;
175 mTcp4RandomPort
= (UINT16
) (TCP4_PORT_KNOWN
+
176 (UINT16
) (NET_RANDOM(Seed
) % TCP4_PORT_KNOWN
));
183 Tests to see if this driver supports a given controller.
185 If a child device is provided, it further tests to see if this driver supports
186 creating a handle for the specified child device.
188 @param This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
189 @param ControllerHandle The handle of the controller to test. This handle
190 must support a protocol interface that supplies
191 an I/O abstraction to the driver.
192 @param RemainingDevicePath A pointer to the remaining portion of a device path.
193 This parameter is ignored by device drivers, and is optional for bus drivers.
196 @retval EFI_SUCCESS The device specified by ControllerHandle and
197 RemainingDevicePath is supported by the driver
199 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
200 RemainingDevicePath is already being managed by
201 the driver specified by This.
202 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
203 RemainingDevicePath is already being managed by a
204 different driver or an application that requires
206 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
207 RemainingDevicePath is not supported by the driver
213 Tcp4DriverBindingSupported (
214 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
215 IN EFI_HANDLE ControllerHandle
,
216 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
222 // Test for the Tcp4ServiceBinding Protocol
224 Status
= gBS
->OpenProtocol (
226 &gEfiTcp4ServiceBindingProtocolGuid
,
228 This
->DriverBindingHandle
,
230 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
232 if (!EFI_ERROR (Status
)) {
233 return EFI_ALREADY_STARTED
;
237 // Test for the Ip4 Protocol
239 Status
= gBS
->OpenProtocol (
241 &gEfiIp4ServiceBindingProtocolGuid
,
243 This
->DriverBindingHandle
,
245 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
253 Start this driver on ControllerHandle.
255 The Start() function is designed to be invoked from the EFI boot service
256 ConnectController(). As a result, much of the error checking on the parameters
257 to Start() has been moved into this common boot service. It is legal to call
258 Start() from other locations, but the following calling restrictions must be
259 followed or the system behavior will not be deterministic.
260 1. ControllerHandle must be a valid EFI_HANDLE.
261 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally
262 aligned EFI_DEVICE_PATH_PROTOCOL.
263 3. Prior to calling Start(), the Supported() function for the driver specified
264 by This must have been called with the same calling parameters, and Supported()
265 must have returned EFI_SUCCESS.
267 @param This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
268 @param ControllerHandle The handle of the controller to start. This handle
269 must support a protocol interface that supplies
270 an I/O abstraction to the driver.
271 @param RemainingDevicePath A pointer to the remaining portion of a device path.
272 This parameter is ignored by device drivers, and is
273 optional for bus drivers.
275 @retval EFI_SUCCESS The device was started.
276 @retval EFI_ALREADY_STARTED The device could not be started due to a device error.
277 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
283 Tcp4DriverBindingStart (
284 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
285 IN EFI_HANDLE ControllerHandle
,
286 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
290 TCP4_SERVICE_DATA
*TcpServiceData
;
291 IP_IO_OPEN_DATA OpenData
;
293 TcpServiceData
= AllocateZeroPool (sizeof (TCP4_SERVICE_DATA
));
295 if (NULL
== TcpServiceData
) {
296 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingStart: Have no enough"
297 " resource to create a Tcp Servcie Data\n"));
299 return EFI_OUT_OF_RESOURCES
;
303 // Create a new IP IO to Consume it
305 TcpServiceData
->IpIo
= IpIoCreate (This
->DriverBindingHandle
, ControllerHandle
);
306 if (NULL
== TcpServiceData
->IpIo
) {
308 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingStart: Have no enough"
309 " resource to create an Ip Io\n"));
311 Status
= EFI_OUT_OF_RESOURCES
;
316 // Configure and start IpIo.
318 ZeroMem (&OpenData
, sizeof (IP_IO_OPEN_DATA
));
320 CopyMem (&OpenData
.IpConfigData
, &mIpIoDefaultIpConfigData
, sizeof (OpenData
.IpConfigData
));
321 OpenData
.IpConfigData
.DefaultProtocol
= EFI_IP_PROTO_TCP
;
323 OpenData
.PktRcvdNotify
= Tcp4RxCallback
;
324 Status
= IpIoOpen (TcpServiceData
->IpIo
, &OpenData
);
326 if (EFI_ERROR (Status
)) {
331 // Create the timer event used by TCP driver
333 Status
= Tcp4CreateTimer ();
334 if (EFI_ERROR (Status
)) {
336 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingStart: Create TcpTimer"
337 " Event failed with %r\n", Status
));
343 // Install the Tcp4ServiceBinding Protocol on the
346 TcpServiceData
->Tcp4ServiceBinding
= mTcp4ServiceBinding
;
348 Status
= gBS
->InstallMultipleProtocolInterfaces (
350 &gEfiTcp4ServiceBindingProtocolGuid
,
351 &TcpServiceData
->Tcp4ServiceBinding
,
354 if (EFI_ERROR (Status
)) {
356 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingStart: Install Tcp4 Service Binding"
357 " Protocol failed for %r\n", Status
));
364 // Initialize member in TcpServiceData
366 TcpServiceData
->ControllerHandle
= ControllerHandle
;
367 TcpServiceData
->Signature
= TCP4_DRIVER_SIGNATURE
;
368 TcpServiceData
->DriverBindingHandle
= This
->DriverBindingHandle
;
370 InitializeListHead (&TcpServiceData
->SocketList
);
372 TcpSetVariableData (TcpServiceData
);
378 if (TcpServiceData
->IpIo
!= NULL
) {
379 IpIoDestroy (TcpServiceData
->IpIo
);
382 gBS
->FreePool (TcpServiceData
);
389 Stop this driver on ControllerHandle.
391 The Stop() function is designed to be invoked from the EFI boot service
392 DisconnectController(). As a result, much of the error checking on the parameters
393 to Stop() has been moved into this common boot service. It is legal to call Stop()
394 from other locations, but the following calling restrictions must be followed
395 or the system behavior will not be deterministic.
396 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call
397 to this same driver's Start() function.
398 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
399 EFI_HANDLE. In addition, all of these handles must have been created in this
400 driver's Start() function, and the Start() function must have called OpenProtocol()
401 on ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
403 @param This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
404 @param ControllerHandle A handle to the device being stopped. The handle must
405 support a bus specific I/O protocol for the driver
406 to use to stop the device.
407 @param NumberOfChildren The number of child device handles in ChildHandleBuffer.
408 @param ChildHandleBuffer An array of child handles to be freed. May be NULL if
409 NumberOfChildren is 0.
411 @retval EFI_SUCCESS The device was stopped.
412 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
417 Tcp4DriverBindingStop (
418 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
419 IN EFI_HANDLE ControllerHandle
,
420 IN UINTN NumberOfChildren
,
421 IN EFI_HANDLE
*ChildHandleBuffer
425 EFI_HANDLE NicHandle
;
426 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
427 TCP4_SERVICE_DATA
*TcpServiceData
;
430 // Find the NicHandle where Tcp4 ServiceBinding Protocol is installed.
432 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiIp4ProtocolGuid
);
433 if (NicHandle
== NULL
) {
434 return EFI_DEVICE_ERROR
;
438 // Retrieve the TCP driver Data Structure
440 Status
= gBS
->OpenProtocol (
442 &gEfiTcp4ServiceBindingProtocolGuid
,
443 (VOID
**) &ServiceBinding
,
444 This
->DriverBindingHandle
,
446 EFI_OPEN_PROTOCOL_GET_PROTOCOL
448 if (EFI_ERROR (Status
)) {
450 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingStop: Locate Tcp4 Service "
451 " Binding Protocol failed with %r\n", Status
));
453 return EFI_DEVICE_ERROR
;
456 TcpServiceData
= TCP4_FROM_THIS (ServiceBinding
);
458 if (NumberOfChildren
== 0) {
460 // Uninstall TCP servicebinding protocol
462 gBS
->UninstallMultipleProtocolInterfaces (
464 &gEfiTcp4ServiceBindingProtocolGuid
,
470 // Destroy the IpIO consumed by TCP driver
472 IpIoDestroy (TcpServiceData
->IpIo
);
475 // Destroy the heartbeat timer.
480 // Clear the variable.
482 TcpClearVariableData (TcpServiceData
);
485 // Release the TCP service data
487 gBS
->FreePool (TcpServiceData
);
490 while (!IsListEmpty (&TcpServiceData
->SocketList
)) {
491 Sock
= NET_LIST_HEAD (&TcpServiceData
->SocketList
, SOCKET
, Link
);
493 ServiceBinding
->DestroyChild (ServiceBinding
, Sock
->SockHandle
);
501 Open Ip4 and device path protocols for a created socket, and insert it in
504 @param This Pointer to the socket just created
505 @param Context Context of the socket
507 @retval EFI_SUCCESS This protocol is installed successfully.
508 @retval other Some error occured.
512 Tcp4CreateSocketCallback (
518 TCP4_SERVICE_DATA
*TcpServiceData
;
519 EFI_IP4_PROTOCOL
*Ip4
;
521 TcpServiceData
= ((TCP4_PROTO_DATA
*) This
->ProtoReserved
)->TcpService
;
524 // Open the default Ip4 protocol of IP_IO BY_DRIVER.
526 Status
= gBS
->OpenProtocol (
527 TcpServiceData
->IpIo
->ChildHandle
,
528 &gEfiIp4ProtocolGuid
,
530 TcpServiceData
->DriverBindingHandle
,
532 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
534 if (EFI_ERROR (Status
)) {
539 // Open the device path on the handle where service binding resides on.
541 Status
= gBS
->OpenProtocol (
542 TcpServiceData
->ControllerHandle
,
543 &gEfiDevicePathProtocolGuid
,
544 (VOID
**) &This
->ParentDevicePath
,
545 TcpServiceData
->DriverBindingHandle
,
547 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
549 if (EFI_ERROR (Status
)) {
551 TcpServiceData
->IpIo
->ChildHandle
,
552 &gEfiIp4ProtocolGuid
,
553 TcpServiceData
->DriverBindingHandle
,
558 // Insert this socket into the SocketList.
560 InsertTailList (&TcpServiceData
->SocketList
, &This
->Link
);
567 Close Ip4 and device path protocols for a socket, and remove it from socket list.
569 @param This Pointer to the socket to be removed
570 @param Context Context of the socket
574 Tcp4DestroySocketCallback (
579 TCP4_SERVICE_DATA
*TcpServiceData
;
581 TcpServiceData
= ((TCP4_PROTO_DATA
*) This
->ProtoReserved
)->TcpService
;
584 // Remove this node from the list.
586 RemoveEntryList (&This
->Link
);
589 // Close the device path protocol
592 TcpServiceData
->ControllerHandle
,
593 &gEfiDevicePathProtocolGuid
,
594 TcpServiceData
->DriverBindingHandle
,
599 // Close the Ip4 protocol.
602 TcpServiceData
->IpIo
->ChildHandle
,
603 &gEfiIp4ProtocolGuid
,
604 TcpServiceData
->DriverBindingHandle
,
610 Creates a child handle and installs a protocol.
612 The CreateChild() function installs a protocol on ChildHandle. If ChildHandle
613 is a pointer to NULL, then a new handle is created and returned in ChildHandle.
614 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing
617 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
618 @param ChildHandle Pointer to the handle of the child to create. If it is NULL, then
619 a new handle is created. If it is a pointer to an existing UEFI
620 handle, then the protocol is added to the existing UEFI handle.
622 @retval EFI_SUCCES The protocol was added to ChildHandle.
623 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
624 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
626 @retval other The child handle was not created.
631 Tcp4ServiceBindingCreateChild (
632 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
633 IN EFI_HANDLE
*ChildHandle
637 TCP4_SERVICE_DATA
*TcpServiceData
;
638 TCP4_PROTO_DATA TcpProto
;
642 if (NULL
== This
|| NULL
== ChildHandle
) {
643 return EFI_INVALID_PARAMETER
;
646 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
647 Status
= EFI_SUCCESS
;
648 TcpServiceData
= TCP4_FROM_THIS (This
);
649 TcpProto
.TcpService
= TcpServiceData
;
650 TcpProto
.TcpPcb
= NULL
;
653 // Create a tcp instance with defualt Tcp default
654 // sock init data and TcpProto
656 mTcp4DefaultSockData
.ProtoData
= &TcpProto
;
657 mTcp4DefaultSockData
.DataSize
= sizeof (TCP4_PROTO_DATA
);
658 mTcp4DefaultSockData
.DriverBinding
= TcpServiceData
->DriverBindingHandle
;
660 Sock
= SockCreateChild (&mTcp4DefaultSockData
);
662 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingCreateChild: "
663 "No resource to create a Tcp Child\n"));
665 Status
= EFI_OUT_OF_RESOURCES
;
667 *ChildHandle
= Sock
->SockHandle
;
670 gBS
->RestoreTPL (OldTpl
);
676 Destroys a child handle with a protocol installed on it.
678 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
679 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
680 last protocol on ChildHandle, then ChildHandle is destroyed.
682 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
683 @param ChildHandle Handle of the child to destroy
685 @retval EFI_SUCCES The protocol was removed from ChildHandle.
686 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is
688 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.
689 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
690 because its services are being used.
691 @retval other The child handle was not destroyed.
696 Tcp4ServiceBindingDestroyChild (
697 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
698 IN EFI_HANDLE ChildHandle
702 EFI_TCP4_PROTOCOL
*Tcp4
;
706 if (NULL
== This
|| NULL
== ChildHandle
) {
707 return EFI_INVALID_PARAMETER
;
710 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
713 // retrieve the Tcp4 protocol from ChildHandle
715 Status
= gBS
->OpenProtocol (
717 &gEfiTcp4ProtocolGuid
,
719 mTcp4DriverBinding
.DriverBindingHandle
,
721 EFI_OPEN_PROTOCOL_GET_PROTOCOL
723 if (EFI_ERROR (Status
)) {
724 Status
= EFI_UNSUPPORTED
;
727 // destroy this sock and related Tcp protocol control
730 Sock
= SOCK_FROM_THIS (Tcp4
);
732 SockDestroyChild (Sock
);
735 gBS
->RestoreTPL (OldTpl
);