3 Copyright (c) 2005 - 2007, Intel Corporation
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
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.
24 UINT16 mTcp4RandomPort
;
25 extern EFI_COMPONENT_NAME_PROTOCOL gTcp4ComponentName
;
26 extern EFI_COMPONENT_NAME2_PROTOCOL gTcp4ComponentName2
;
28 TCP4_HEARTBEAT_TIMER mTcp4Timer
= {
33 EFI_TCP4_PROTOCOL mTcp4ProtocolTemplate
= {
46 SOCK_INIT_DATA mTcp4DefaultSockData
= {
53 &mTcp4ProtocolTemplate
,
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.
78 @retval EFI_SUCCESS The timer is successfully created and started.
79 @retval other The timer is not created.
92 if (mTcp4Timer
.RefCnt
== 0) {
94 Status
= gBS
->CreateEvent (
95 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
99 &mTcp4Timer
.TimerEvent
101 if (!EFI_ERROR (Status
)) {
103 Status
= gBS
->SetTimer (
104 mTcp4Timer
.TimerEvent
,
106 (UINT64
) (TICKS_PER_SECOND
/ TCP_TICK_HZ
)
111 if (!EFI_ERROR (Status
)) {
121 Stop and destroy the heartbeat timer for TCP driver.
134 ASSERT (mTcp4Timer
.RefCnt
> 0);
138 if (mTcp4Timer
.RefCnt
> 0) {
142 gBS
->SetTimer (mTcp4Timer
.TimerEvent
, TimerCancel
, 0);
143 gBS
->CloseEvent (mTcp4Timer
.TimerEvent
);
144 mTcp4Timer
.TimerEvent
= NULL
;
150 Tcp4DriverEntryPoint (
151 IN EFI_HANDLE ImageHandle
,
152 IN EFI_SYSTEM_TABLE
*SystemTable
158 The entry point for Tcp4 driver. used to install
159 Tcp4 driver on the ImageHandle.
163 ImageHandle - The firmware allocated handle for this
165 SystemTable - Pointer to the EFI system table.
169 EFI_SUCCESS - Driver loaded.
170 other - Driver not loaded.
178 // Install the TCP4 Driver Binding Protocol
180 Status
= EfiLibInstallDriverBindingComponentName2 (
188 ASSERT_EFI_ERROR (Status
);
190 // Initialize ISS and random port.
192 Seed
= NetRandomInitSeed ();
193 mTcpGlobalIss
= NET_RANDOM (Seed
) % mTcpGlobalIss
;
194 mTcp4RandomPort
= (UINT16
) ( TCP4_PORT_KNOWN
+
195 (UINT16
) (NET_RANDOM(Seed
) % TCP4_PORT_KNOWN
));
202 Test to see if this driver supports ControllerHandle.
204 @param This Protocol instance pointer.
205 @param ControllerHandle Handle of device to test.
206 @param RemainingDevicePath Optional parameter use to pick a specific child
209 @retval EFI_SUCCESS This driver supports this device.
210 @retval EFI_ALREADY_STARTED This driver is already running on this device.
211 @retval other This driver does not support this device.
216 Tcp4DriverBindingSupported (
217 IN EFI_DRIVER_BINDING_PROTOCOL
* This
,
218 IN EFI_HANDLE ControllerHandle
,
219 IN EFI_DEVICE_PATH_PROTOCOL
* RemainingDevicePath OPTIONAL
225 // Test for the Tcp4ServiceBinding Protocol
227 Status
= gBS
->OpenProtocol (
229 &gEfiTcp4ServiceBindingProtocolGuid
,
231 This
->DriverBindingHandle
,
233 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
235 if (!EFI_ERROR (Status
)) {
236 return EFI_ALREADY_STARTED
;
240 // Test for the Ip4 Protocol
242 Status
= gBS
->OpenProtocol (
244 &gEfiIp4ServiceBindingProtocolGuid
,
246 This
->DriverBindingHandle
,
248 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
256 Start this driver on ControllerHandle.
258 @param This Protocol instance pointer.
259 @param ControllerHandle Handle of device to bind driver to.
260 @param RemainingDevicePath Optional parameter use to pick a specific child
263 @retval EFI_SUCCESS The driver is added to ControllerHandle.
264 @retval EFI_OUT_OF_RESOURCES There are not enough resources to start the
266 @retval other The driver cannot be added to ControllerHandle.
271 Tcp4DriverBindingStart (
272 IN EFI_DRIVER_BINDING_PROTOCOL
* This
,
273 IN EFI_HANDLE ControllerHandle
,
274 IN EFI_DEVICE_PATH_PROTOCOL
* RemainingDevicePath OPTIONAL
278 TCP4_SERVICE_DATA
*TcpServiceData
;
279 IP_IO_OPEN_DATA OpenData
;
281 TcpServiceData
= NetAllocateZeroPool (sizeof (TCP4_SERVICE_DATA
));
283 if (NULL
== TcpServiceData
) {
284 TCP4_DEBUG_ERROR (("Tcp4DriverBindingStart: Have no enough"
285 " resource to create a Tcp Servcie Data!\n"));
287 return EFI_OUT_OF_RESOURCES
;
291 // Create a new IP IO to Consume it
293 TcpServiceData
->IpIo
= IpIoCreate (This
->DriverBindingHandle
, ControllerHandle
);
294 if (NULL
== TcpServiceData
->IpIo
) {
296 TCP4_DEBUG_ERROR (("Tcp4DriverBindingStart: Have no enough"
297 " resource to create an Ip Io!\n"));
299 Status
= EFI_OUT_OF_RESOURCES
;
304 // Configure and start IpIo.
306 NetZeroMem (&OpenData
, sizeof (IP_IO_OPEN_DATA
));
308 CopyMem (&OpenData
.IpConfigData
, &mIpIoDefaultIpConfigData
, sizeof (OpenData
.IpConfigData
));
309 OpenData
.IpConfigData
.DefaultProtocol
= EFI_IP_PROTO_TCP
;
311 OpenData
.PktRcvdNotify
= Tcp4RxCallback
;
312 Status
= IpIoOpen (TcpServiceData
->IpIo
, &OpenData
);
314 if (EFI_ERROR (Status
)) {
319 // Create the timer event used by TCP driver
321 Status
= Tcp4CreateTimer ();
322 if (EFI_ERROR (Status
)) {
324 TCP4_DEBUG_ERROR (("Tcp4DriverBindingStart: Create TcpTimer"
325 " Event failed with %r\n", Status
));
331 // Install the Tcp4ServiceBinding Protocol on the
334 TcpServiceData
->Tcp4ServiceBinding
= mTcp4ServiceBinding
;
336 Status
= gBS
->InstallMultipleProtocolInterfaces (
338 &gEfiTcp4ServiceBindingProtocolGuid
,
339 &TcpServiceData
->Tcp4ServiceBinding
,
342 if (EFI_ERROR (Status
)) {
344 TCP4_DEBUG_ERROR (("Tcp4DriverBindingStart: Install Tcp4 Service Binding"
345 " Protocol failed for %r\n", Status
));
352 // Initialize member in TcpServiceData
354 TcpServiceData
->ControllerHandle
= ControllerHandle
;
355 TcpServiceData
->Signature
= TCP4_DRIVER_SIGNATURE
;
356 TcpServiceData
->DriverBindingHandle
= This
->DriverBindingHandle
;
358 NetListInit (&TcpServiceData
->SocketList
);
360 TcpSetVariableData (TcpServiceData
);
366 if (TcpServiceData
->IpIo
!= NULL
) {
367 IpIoDestroy (TcpServiceData
->IpIo
);
370 NetFreePool (TcpServiceData
);
377 Stop this driver on ControllerHandle.
379 @param This Protocol instance pointer.
380 @param ControllerHandle Handle of device to stop driver on.
381 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number
382 of children is zero stop the entire bus driver.
383 @param ChildHandleBuffer List of Child Handles to Stop.
385 @retval EFI_SUCCESS This driver is removed from ControllerHandle.
386 @retval other This driver is not removed from ControllerHandle.
391 Tcp4DriverBindingStop (
392 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
393 IN EFI_HANDLE ControllerHandle
,
394 IN UINTN NumberOfChildren
,
395 IN EFI_HANDLE
*ChildHandleBuffer
399 EFI_HANDLE NicHandle
;
400 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
401 TCP4_SERVICE_DATA
*TcpServiceData
;
404 // Find the NicHandle where Tcp4 ServiceBinding Protocol is installed.
406 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiIp4ProtocolGuid
);
407 if (NicHandle
== NULL
) {
408 return EFI_DEVICE_ERROR
;
412 // Retrieve the TCP driver Data Structure
414 Status
= gBS
->OpenProtocol (
416 &gEfiTcp4ServiceBindingProtocolGuid
,
417 (VOID
**) &ServiceBinding
,
418 This
->DriverBindingHandle
,
420 EFI_OPEN_PROTOCOL_GET_PROTOCOL
422 if (EFI_ERROR (Status
)) {
424 TCP4_DEBUG_ERROR (("Tcp4DriverBindingStop: Locate Tcp4 Service "
425 " Binding Protocol failed with %r\n", Status
));
427 return EFI_DEVICE_ERROR
;
430 TcpServiceData
= TCP4_FROM_THIS (ServiceBinding
);
432 if (NumberOfChildren
== 0) {
434 // Uninstall TCP servicebinding protocol
436 gBS
->UninstallMultipleProtocolInterfaces (
438 &gEfiTcp4ServiceBindingProtocolGuid
,
444 // Destroy the IpIO consumed by TCP driver
446 IpIoDestroy (TcpServiceData
->IpIo
);
449 // Destroy the heartbeat timer.
454 // Clear the variable.
456 TcpClearVariableData (TcpServiceData
);
459 // Release the TCP service data
461 NetFreePool (TcpServiceData
);
464 while (!NetListIsEmpty (&TcpServiceData
->SocketList
)) {
465 Sock
= NET_LIST_HEAD (&TcpServiceData
->SocketList
, SOCKET
, Link
);
467 ServiceBinding
->DestroyChild (ServiceBinding
, Sock
->SockHandle
);
475 Creates a child handle with a set of TCP4 services.
477 @param This Protocol instance pointer.
478 @param ChildHandle Pointer to the handle of the child to create. If
479 it is NULL, then a new handle is created. If it is
480 not NULL, then the I/O services are added to the
481 existing child handle.
483 @retval EFI_SUCCESS The child handle is created.
484 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
485 @retval EFI_OUT_OF_RESOURCES There are not enough resources to create the
491 Tcp4ServiceBindingCreateChild (
492 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
493 IN EFI_HANDLE
*ChildHandle
497 TCP4_SERVICE_DATA
*TcpServiceData
;
498 TCP4_PROTO_DATA TcpProto
;
503 if (NULL
== This
|| NULL
== ChildHandle
) {
504 return EFI_INVALID_PARAMETER
;
507 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
508 TcpServiceData
= TCP4_FROM_THIS (This
);
509 TcpProto
.TcpService
= TcpServiceData
;
510 TcpProto
.TcpPcb
= NULL
;
513 // Create a tcp instance with defualt Tcp default
514 // sock init data and TcpProto
516 mTcp4DefaultSockData
.DriverBinding
= TcpServiceData
->DriverBindingHandle
;
518 Sock
= SockCreateChild (&mTcp4DefaultSockData
, &TcpProto
, sizeof (TCP4_PROTO_DATA
));
520 TCP4_DEBUG_ERROR (("Tcp4DriverBindingCreateChild: "
521 "No resource to create a Tcp Child\n"));
523 Status
= EFI_OUT_OF_RESOURCES
;
527 *ChildHandle
= Sock
->SockHandle
;
530 // Open the default Ip4 protocol of IP_IO BY_DRIVER.
532 Status
= gBS
->OpenProtocol (
533 TcpServiceData
->IpIo
->ChildHandle
,
534 &gEfiIp4ProtocolGuid
,
536 TcpServiceData
->DriverBindingHandle
,
538 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
540 if (EFI_ERROR (Status
)) {
541 SockDestroyChild (Sock
);
546 // Open the device path on the handle where service binding resides on.
548 Status
= gBS
->OpenProtocol (
549 TcpServiceData
->ControllerHandle
,
550 &gEfiDevicePathProtocolGuid
,
551 (VOID
**) &Sock
->ParentDevicePath
,
552 TcpServiceData
->DriverBindingHandle
,
554 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
556 if (EFI_ERROR (Status
)) {
558 TcpServiceData
->IpIo
->ChildHandle
,
559 &gEfiIp4ProtocolGuid
,
560 TcpServiceData
->DriverBindingHandle
,
563 SockDestroyChild (Sock
);
565 NetListInsertTail (&TcpServiceData
->SocketList
, &Sock
->Link
);
570 NET_RESTORE_TPL (OldTpl
);
576 Destroys a child handle with a set of UDP4 services.
578 @param This Protocol instance pointer.
579 @param ChildHandle Handle of the child to be destroyed.
581 @retval EFI_SUCCESS The TCP4 services are removed from the child
583 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
584 @retval other The child handle is not destroyed.
589 Tcp4ServiceBindingDestroyChild (
590 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
591 IN EFI_HANDLE ChildHandle
595 EFI_TCP4_PROTOCOL
*Tcp4
;
597 TCP4_PROTO_DATA
*TcpProtoData
;
598 TCP4_SERVICE_DATA
*TcpServiceData
;
601 if (NULL
== This
|| NULL
== ChildHandle
) {
602 return EFI_INVALID_PARAMETER
;
605 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
608 // retrieve the Tcp4 protocol from ChildHandle
610 Status
= gBS
->OpenProtocol (
612 &gEfiTcp4ProtocolGuid
,
614 mTcp4DriverBinding
.DriverBindingHandle
,
616 EFI_OPEN_PROTOCOL_GET_PROTOCOL
618 if (EFI_ERROR (Status
)) {
619 Status
= EFI_UNSUPPORTED
;
624 // destroy this sock and related Tcp protocol control
627 Sock
= SOCK_FROM_THIS (Tcp4
);
628 TcpProtoData
= (TCP4_PROTO_DATA
*) Sock
->ProtoReserved
;
629 TcpServiceData
= TcpProtoData
->TcpService
;
631 NetListRemoveEntry (&Sock
->Link
);
633 SockDestroyChild (Sock
);
636 // Close the device path protocol
639 TcpServiceData
->ControllerHandle
,
640 &gEfiDevicePathProtocolGuid
,
641 TcpServiceData
->DriverBindingHandle
,
646 // Close the Ip4 protocol.
649 TcpServiceData
->IpIo
->ChildHandle
,
650 &gEfiIp4ProtocolGuid
,
651 TcpServiceData
->DriverBindingHandle
,
656 NET_RESTORE_TPL (OldTpl
);