4 Copyright (c) 2005 - 2007, Intel Corporation<BR>
5 All rights reserved. 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
;
22 TCP4_HEARTBEAT_TIMER mTcp4Timer
= {
27 EFI_TCP4_PROTOCOL mTcp4ProtocolTemplate
= {
40 SOCK_INIT_DATA mTcp4DefaultSockData
= {
47 &mTcp4ProtocolTemplate
,
48 Tcp4CreateSocketCallback
,
49 Tcp4DestroySocketCallback
,
57 EFI_DRIVER_BINDING_PROTOCOL mTcp4DriverBinding
= {
58 Tcp4DriverBindingSupported
,
59 Tcp4DriverBindingStart
,
60 Tcp4DriverBindingStop
,
66 EFI_SERVICE_BINDING_PROTOCOL mTcp4ServiceBinding
= {
67 Tcp4ServiceBindingCreateChild
,
68 Tcp4ServiceBindingDestroyChild
73 Create and start the heartbeat timer for TCP driver.
75 @retval EFI_SUCCESS The timer is successfully created and started.
76 @retval other The timer is not created.
88 if (mTcp4Timer
.RefCnt
== 0) {
90 Status
= gBS
->CreateEvent (
91 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
95 &mTcp4Timer
.TimerEvent
97 if (!EFI_ERROR (Status
)) {
99 Status
= gBS
->SetTimer (
100 mTcp4Timer
.TimerEvent
,
102 (UINT64
) (TICKS_PER_SECOND
/ TCP_TICK_HZ
)
107 if (!EFI_ERROR (Status
)) {
117 Stop and destroy the heartbeat timer for TCP driver.
125 ASSERT (mTcp4Timer
.RefCnt
> 0);
129 if (mTcp4Timer
.RefCnt
> 0) {
133 gBS
->SetTimer (mTcp4Timer
.TimerEvent
, TimerCancel
, 0);
134 gBS
->CloseEvent (mTcp4Timer
.TimerEvent
);
135 mTcp4Timer
.TimerEvent
= NULL
;
139 The entry point for Tcp4 driver, used to install Tcp4 driver on the ImageHandle.
141 @param ImageHandle The firmware allocated handle for this
143 @param SystemTable Pointer to the EFI system table.
145 @retval EFI_SUCCESS Driver loaded.
146 @retval other Driver not loaded.
151 Tcp4DriverEntryPoint (
152 IN EFI_HANDLE ImageHandle
,
153 IN EFI_SYSTEM_TABLE
*SystemTable
160 // Install the TCP4 Driver Binding Protocol
162 Status
= EfiLibInstallDriverBindingComponentName2 (
170 ASSERT_EFI_ERROR (Status
);
172 // Initialize ISS and random port.
174 Seed
= NetRandomInitSeed ();
175 mTcpGlobalIss
= NET_RANDOM (Seed
) % mTcpGlobalIss
;
176 mTcp4RandomPort
= (UINT16
) (TCP4_PORT_KNOWN
+
177 (UINT16
) (NET_RANDOM(Seed
) % TCP4_PORT_KNOWN
));
184 Tests to see if this driver supports a given controller.
186 If a child device is provided, it further tests to see if this driver supports
187 creating a handle for the specified child device.
189 @param This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
190 @param ControllerHandle The handle of the controller to test. This handle
191 must support a protocol interface that supplies
192 an I/O abstraction to the driver.
193 @param RemainingDevicePath A pointer to the remaining portion of a device path.
194 This parameter is ignored by device drivers, and is optional for bus drivers.
197 @retval EFI_SUCCESS The device specified by ControllerHandle and
198 RemainingDevicePath is supported by the driver
200 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
201 RemainingDevicePath is already being managed by
202 the driver specified by This.
203 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
204 RemainingDevicePath is already being managed by a
205 different driver or an application that requires
207 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
208 RemainingDevicePath is not supported by the driver
214 Tcp4DriverBindingSupported (
215 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
216 IN EFI_HANDLE ControllerHandle
,
217 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
223 // Test for the Tcp4ServiceBinding Protocol
225 Status
= gBS
->OpenProtocol (
227 &gEfiTcp4ServiceBindingProtocolGuid
,
229 This
->DriverBindingHandle
,
231 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
233 if (!EFI_ERROR (Status
)) {
234 return EFI_ALREADY_STARTED
;
238 // Test for the Ip4 Protocol
240 Status
= gBS
->OpenProtocol (
242 &gEfiIp4ServiceBindingProtocolGuid
,
244 This
->DriverBindingHandle
,
246 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
254 Start this driver on ControllerHandle.
256 The Start() function is designed to be invoked from the EFI boot service
257 ConnectController(). As a result, much of the error checking on the parameters
258 to Start() has been moved into this common boot service. It is legal to call
259 Start() from other locations, but the following calling restrictions must be
260 followed or the system behavior will not be deterministic.
261 1. ControllerHandle must be a valid EFI_HANDLE.
262 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally
263 aligned EFI_DEVICE_PATH_PROTOCOL.
264 3. Prior to calling Start(), the Supported() function for the driver specified
265 by This must have been called with the same calling parameters, and Supported()
266 must have returned EFI_SUCCESS.
268 @param This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
269 @param ControllerHandle The handle of the controller to start. This handle
270 must support a protocol interface that supplies
271 an I/O abstraction to the driver.
272 @param RemainingDevicePath A pointer to the remaining portion of a device path.
273 This parameter is ignored by device drivers, and is
274 optional for bus drivers.
276 @retval EFI_SUCCESS The device was started.
277 @retval EFI_ALREADY_STARTED The device could not be started due to a device error.
278 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
284 Tcp4DriverBindingStart (
285 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
286 IN EFI_HANDLE ControllerHandle
,
287 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
291 TCP4_SERVICE_DATA
*TcpServiceData
;
292 IP_IO_OPEN_DATA OpenData
;
294 TcpServiceData
= AllocateZeroPool (sizeof (TCP4_SERVICE_DATA
));
296 if (NULL
== TcpServiceData
) {
297 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingStart: Have no enough"
298 " resource to create a Tcp Servcie Data\n"));
300 return EFI_OUT_OF_RESOURCES
;
304 // Create a new IP IO to Consume it
306 TcpServiceData
->IpIo
= IpIoCreate (This
->DriverBindingHandle
, ControllerHandle
);
307 if (NULL
== TcpServiceData
->IpIo
) {
309 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingStart: Have no enough"
310 " resource to create an Ip Io\n"));
312 Status
= EFI_OUT_OF_RESOURCES
;
317 // Configure and start IpIo.
319 ZeroMem (&OpenData
, sizeof (IP_IO_OPEN_DATA
));
321 CopyMem (&OpenData
.IpConfigData
, &mIpIoDefaultIpConfigData
, sizeof (OpenData
.IpConfigData
));
322 OpenData
.IpConfigData
.DefaultProtocol
= EFI_IP_PROTO_TCP
;
324 OpenData
.PktRcvdNotify
= Tcp4RxCallback
;
325 Status
= IpIoOpen (TcpServiceData
->IpIo
, &OpenData
);
327 if (EFI_ERROR (Status
)) {
332 // Create the timer event used by TCP driver
334 Status
= Tcp4CreateTimer ();
335 if (EFI_ERROR (Status
)) {
337 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingStart: Create TcpTimer"
338 " Event failed with %r\n", Status
));
344 // Install the Tcp4ServiceBinding Protocol on the
347 TcpServiceData
->Tcp4ServiceBinding
= mTcp4ServiceBinding
;
349 Status
= gBS
->InstallMultipleProtocolInterfaces (
351 &gEfiTcp4ServiceBindingProtocolGuid
,
352 &TcpServiceData
->Tcp4ServiceBinding
,
355 if (EFI_ERROR (Status
)) {
357 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingStart: Install Tcp4 Service Binding"
358 " Protocol failed for %r\n", Status
));
365 // Initialize member in TcpServiceData
367 TcpServiceData
->ControllerHandle
= ControllerHandle
;
368 TcpServiceData
->Signature
= TCP4_DRIVER_SIGNATURE
;
369 TcpServiceData
->DriverBindingHandle
= This
->DriverBindingHandle
;
371 InitializeListHead (&TcpServiceData
->SocketList
);
373 TcpSetVariableData (TcpServiceData
);
379 if (TcpServiceData
->IpIo
!= NULL
) {
380 IpIoDestroy (TcpServiceData
->IpIo
);
383 gBS
->FreePool (TcpServiceData
);
390 Stop this driver on ControllerHandle.
392 The Stop() function is designed to be invoked from the EFI boot service
393 DisconnectController(). As a result, much of the error checking on the parameters
394 to Stop() has been moved into this common boot service. It is legal to call Stop()
395 from other locations, but the following calling restrictions must be followed
396 or the system behavior will not be deterministic.
397 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call
398 to this same driver's Start() function.
399 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
400 EFI_HANDLE. In addition, all of these handles must have been created in this
401 driver's Start() function, and the Start() function must have called OpenProtocol()
402 on ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
404 @param This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
405 @param ControllerHandle A handle to the device being stopped. The handle must
406 support a bus specific I/O protocol for the driver
407 to use to stop the device.
408 @param NumberOfChildren The number of child device handles in ChildHandleBuffer.
409 @param ChildHandleBuffer An array of child handles to be freed. May be NULL if
410 NumberOfChildren is 0.
412 @retval EFI_SUCCESS The device was stopped.
413 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
418 Tcp4DriverBindingStop (
419 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
420 IN EFI_HANDLE ControllerHandle
,
421 IN UINTN NumberOfChildren
,
422 IN EFI_HANDLE
*ChildHandleBuffer
426 EFI_HANDLE NicHandle
;
427 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
428 TCP4_SERVICE_DATA
*TcpServiceData
;
431 // Find the NicHandle where Tcp4 ServiceBinding Protocol is installed.
433 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiIp4ProtocolGuid
);
434 if (NicHandle
== NULL
) {
435 return EFI_DEVICE_ERROR
;
439 // Retrieve the TCP driver Data Structure
441 Status
= gBS
->OpenProtocol (
443 &gEfiTcp4ServiceBindingProtocolGuid
,
444 (VOID
**) &ServiceBinding
,
445 This
->DriverBindingHandle
,
447 EFI_OPEN_PROTOCOL_GET_PROTOCOL
449 if (EFI_ERROR (Status
)) {
451 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingStop: Locate Tcp4 Service "
452 " Binding Protocol failed with %r\n", Status
));
454 return EFI_DEVICE_ERROR
;
457 TcpServiceData
= TCP4_FROM_THIS (ServiceBinding
);
459 if (NumberOfChildren
== 0) {
461 // Uninstall TCP servicebinding protocol
463 gBS
->UninstallMultipleProtocolInterfaces (
465 &gEfiTcp4ServiceBindingProtocolGuid
,
471 // Destroy the IpIO consumed by TCP driver
473 IpIoDestroy (TcpServiceData
->IpIo
);
476 // Destroy the heartbeat timer.
481 // Clear the variable.
483 TcpClearVariableData (TcpServiceData
);
486 // Release the TCP service data
488 gBS
->FreePool (TcpServiceData
);
491 while (!IsListEmpty (&TcpServiceData
->SocketList
)) {
492 Sock
= NET_LIST_HEAD (&TcpServiceData
->SocketList
, SOCKET
, Link
);
494 ServiceBinding
->DestroyChild (ServiceBinding
, Sock
->SockHandle
);
502 Open Ip4 and device path protocols for a created socket, and insert it in
505 @param This Pointer to the socket just created
506 @param Context Context of the socket
508 @retval EFI_SUCCESS This protocol is installed successfully.
509 @retval other Some error occured.
513 Tcp4CreateSocketCallback (
519 TCP4_SERVICE_DATA
*TcpServiceData
;
520 EFI_IP4_PROTOCOL
*Ip4
;
522 TcpServiceData
= ((TCP4_PROTO_DATA
*) This
->ProtoReserved
)->TcpService
;
525 // Open the default Ip4 protocol of IP_IO BY_DRIVER.
527 Status
= gBS
->OpenProtocol (
528 TcpServiceData
->IpIo
->ChildHandle
,
529 &gEfiIp4ProtocolGuid
,
531 TcpServiceData
->DriverBindingHandle
,
533 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
535 if (EFI_ERROR (Status
)) {
540 // Open the device path on the handle where service binding resides on.
542 Status
= gBS
->OpenProtocol (
543 TcpServiceData
->ControllerHandle
,
544 &gEfiDevicePathProtocolGuid
,
545 (VOID
**) &This
->ParentDevicePath
,
546 TcpServiceData
->DriverBindingHandle
,
548 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
550 if (EFI_ERROR (Status
)) {
552 TcpServiceData
->IpIo
->ChildHandle
,
553 &gEfiIp4ProtocolGuid
,
554 TcpServiceData
->DriverBindingHandle
,
559 // Insert this socket into the SocketList.
561 InsertTailList (&TcpServiceData
->SocketList
, &This
->Link
);
568 Close Ip4 and device path protocols for a socket, and remove it from socket list.
570 @param This Pointer to the socket to be removed
571 @param Context Context of the socket
575 Tcp4DestroySocketCallback (
580 TCP4_SERVICE_DATA
*TcpServiceData
;
582 TcpServiceData
= ((TCP4_PROTO_DATA
*) This
->ProtoReserved
)->TcpService
;
585 // Remove this node from the list.
587 RemoveEntryList (&This
->Link
);
590 // Close the device path protocol
593 TcpServiceData
->ControllerHandle
,
594 &gEfiDevicePathProtocolGuid
,
595 TcpServiceData
->DriverBindingHandle
,
600 // Close the Ip4 protocol.
603 TcpServiceData
->IpIo
->ChildHandle
,
604 &gEfiIp4ProtocolGuid
,
605 TcpServiceData
->DriverBindingHandle
,
611 Creates a child handle and installs a protocol.
613 The CreateChild() function installs a protocol on ChildHandle. If ChildHandle
614 is a pointer to NULL, then a new handle is created and returned in ChildHandle.
615 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing
618 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
619 @param ChildHandle Pointer to the handle of the child to create. If it is NULL, then
620 a new handle is created. If it is a pointer to an existing UEFI
621 handle, then the protocol is added to the existing UEFI handle.
623 @retval EFI_SUCCES The protocol was added to ChildHandle.
624 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
625 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
627 @retval other The child handle was not created.
632 Tcp4ServiceBindingCreateChild (
633 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
634 IN OUT EFI_HANDLE
*ChildHandle
638 TCP4_SERVICE_DATA
*TcpServiceData
;
639 TCP4_PROTO_DATA TcpProto
;
643 if (NULL
== This
|| NULL
== ChildHandle
) {
644 return EFI_INVALID_PARAMETER
;
647 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
648 Status
= EFI_SUCCESS
;
649 TcpServiceData
= TCP4_FROM_THIS (This
);
650 TcpProto
.TcpService
= TcpServiceData
;
651 TcpProto
.TcpPcb
= NULL
;
654 // Create a tcp instance with defualt Tcp default
655 // sock init data and TcpProto
657 mTcp4DefaultSockData
.ProtoData
= &TcpProto
;
658 mTcp4DefaultSockData
.DataSize
= sizeof (TCP4_PROTO_DATA
);
659 mTcp4DefaultSockData
.DriverBinding
= TcpServiceData
->DriverBindingHandle
;
661 Sock
= SockCreateChild (&mTcp4DefaultSockData
);
663 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingCreateChild: "
664 "No resource to create a Tcp Child\n"));
666 Status
= EFI_OUT_OF_RESOURCES
;
668 *ChildHandle
= Sock
->SockHandle
;
671 gBS
->RestoreTPL (OldTpl
);
677 Destroys a child handle with a protocol installed on it.
679 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
680 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
681 last protocol on ChildHandle, then ChildHandle is destroyed.
683 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
684 @param ChildHandle Handle of the child to destroy
686 @retval EFI_SUCCES The protocol was removed from ChildHandle.
687 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is
689 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.
690 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
691 because its services are being used.
692 @retval other The child handle was not destroyed.
697 Tcp4ServiceBindingDestroyChild (
698 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
699 IN EFI_HANDLE ChildHandle
703 EFI_TCP4_PROTOCOL
*Tcp4
;
707 if (NULL
== This
|| NULL
== ChildHandle
) {
708 return EFI_INVALID_PARAMETER
;
711 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
714 // retrieve the Tcp4 protocol from ChildHandle
716 Status
= gBS
->OpenProtocol (
718 &gEfiTcp4ProtocolGuid
,
720 mTcp4DriverBinding
.DriverBindingHandle
,
722 EFI_OPEN_PROTOCOL_GET_PROTOCOL
724 if (EFI_ERROR (Status
)) {
725 Status
= EFI_UNSUPPORTED
;
728 // destroy this sock and related Tcp protocol control
731 Sock
= SOCK_FROM_THIS (Tcp4
);
733 SockDestroyChild (Sock
);
736 gBS
->RestoreTPL (OldTpl
);