4 Copyright (c) 2005 - 2009, 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
;
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 (
307 This
->DriverBindingHandle
,
311 if (NULL
== TcpServiceData
->IpIo
) {
313 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingStart: Have no enough"
314 " resource to create an Ip Io\n"));
316 Status
= EFI_OUT_OF_RESOURCES
;
321 // Configure and start IpIo.
323 ZeroMem (&OpenData
, sizeof (IP_IO_OPEN_DATA
));
326 &OpenData
.IpConfigData
.Ip4CfgData
,
327 &mIp4IoDefaultIpConfigData
,
328 sizeof (EFI_IP4_CONFIG_DATA
)
331 OpenData
.IpConfigData
.Ip4CfgData
.DefaultProtocol
= EFI_IP_PROTO_TCP
;
333 OpenData
.PktRcvdNotify
= Tcp4RxCallback
;
334 Status
= IpIoOpen (TcpServiceData
->IpIo
, &OpenData
);
336 if (EFI_ERROR (Status
)) {
341 // Create the timer event used by TCP driver
343 Status
= Tcp4CreateTimer ();
344 if (EFI_ERROR (Status
)) {
346 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingStart: Create TcpTimer"
347 " Event failed with %r\n", Status
));
353 // Install the Tcp4ServiceBinding Protocol on the
356 TcpServiceData
->Tcp4ServiceBinding
= mTcp4ServiceBinding
;
358 Status
= gBS
->InstallMultipleProtocolInterfaces (
360 &gEfiTcp4ServiceBindingProtocolGuid
,
361 &TcpServiceData
->Tcp4ServiceBinding
,
364 if (EFI_ERROR (Status
)) {
366 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingStart: Install Tcp4 Service Binding"
367 " Protocol failed for %r\n", Status
));
374 // Initialize member in TcpServiceData
376 TcpServiceData
->ControllerHandle
= ControllerHandle
;
377 TcpServiceData
->Signature
= TCP4_DRIVER_SIGNATURE
;
378 TcpServiceData
->DriverBindingHandle
= This
->DriverBindingHandle
;
380 InitializeListHead (&TcpServiceData
->SocketList
);
382 TcpSetVariableData (TcpServiceData
);
388 if (TcpServiceData
->IpIo
!= NULL
) {
389 IpIoDestroy (TcpServiceData
->IpIo
);
392 FreePool (TcpServiceData
);
399 Stop this driver on ControllerHandle.
401 The Stop() function is designed to be invoked from the EFI boot service
402 DisconnectController(). As a result, much of the error checking on the parameters
403 to Stop() has been moved into this common boot service. It is legal to call Stop()
404 from other locations, but the following calling restrictions must be followed
405 or the system behavior will not be deterministic.
406 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call
407 to this same driver's Start() function.
408 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
409 EFI_HANDLE. In addition, all of these handles must have been created in this
410 driver's Start() function, and the Start() function must have called OpenProtocol()
411 on ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
413 @param This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
414 @param ControllerHandle A handle to the device being stopped. The handle must
415 support a bus specific I/O protocol for the driver
416 to use to stop the device.
417 @param NumberOfChildren The number of child device handles in ChildHandleBuffer.
418 @param ChildHandleBuffer An array of child handles to be freed. May be NULL if
419 NumberOfChildren is 0.
421 @retval EFI_SUCCESS The device was stopped.
422 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
427 Tcp4DriverBindingStop (
428 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
429 IN EFI_HANDLE ControllerHandle
,
430 IN UINTN NumberOfChildren
,
431 IN EFI_HANDLE
*ChildHandleBuffer
435 EFI_HANDLE NicHandle
;
436 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
437 TCP4_SERVICE_DATA
*TcpServiceData
;
440 // Find the NicHandle where Tcp4 ServiceBinding Protocol is installed.
442 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiIp4ProtocolGuid
);
443 if (NicHandle
== NULL
) {
444 return EFI_DEVICE_ERROR
;
448 // Retrieve the TCP driver Data Structure
450 Status
= gBS
->OpenProtocol (
452 &gEfiTcp4ServiceBindingProtocolGuid
,
453 (VOID
**) &ServiceBinding
,
454 This
->DriverBindingHandle
,
456 EFI_OPEN_PROTOCOL_GET_PROTOCOL
458 if (EFI_ERROR (Status
)) {
460 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingStop: Locate Tcp4 Service "
461 " Binding Protocol failed with %r\n", Status
));
463 return EFI_DEVICE_ERROR
;
466 TcpServiceData
= TCP4_FROM_THIS (ServiceBinding
);
468 if (NumberOfChildren
== 0) {
470 // Uninstall TCP servicebinding protocol
472 gBS
->UninstallMultipleProtocolInterfaces (
474 &gEfiTcp4ServiceBindingProtocolGuid
,
480 // Destroy the IpIO consumed by TCP driver
482 IpIoDestroy (TcpServiceData
->IpIo
);
485 // Destroy the heartbeat timer.
490 // Clear the variable.
492 TcpClearVariableData (TcpServiceData
);
495 // Release the TCP service data
497 FreePool (TcpServiceData
);
500 while (!IsListEmpty (&TcpServiceData
->SocketList
)) {
501 Sock
= NET_LIST_HEAD (&TcpServiceData
->SocketList
, SOCKET
, Link
);
503 ServiceBinding
->DestroyChild (ServiceBinding
, Sock
->SockHandle
);
511 Open Ip4 and device path protocols for a created socket, and insert it in
514 @param This Pointer to the socket just created
515 @param Context Context of the socket
517 @retval EFI_SUCCESS This protocol is installed successfully.
518 @retval other Some error occured.
522 Tcp4CreateSocketCallback (
528 TCP4_SERVICE_DATA
*TcpServiceData
;
529 EFI_IP4_PROTOCOL
*Ip4
;
531 TcpServiceData
= ((TCP4_PROTO_DATA
*) This
->ProtoReserved
)->TcpService
;
534 // Open the default Ip4 protocol of IP_IO BY_DRIVER.
536 Status
= gBS
->OpenProtocol (
537 TcpServiceData
->IpIo
->ChildHandle
,
538 &gEfiIp4ProtocolGuid
,
540 TcpServiceData
->DriverBindingHandle
,
542 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
544 if (EFI_ERROR (Status
)) {
549 // Open the device path on the handle where service binding resides on.
551 Status
= gBS
->OpenProtocol (
552 TcpServiceData
->ControllerHandle
,
553 &gEfiDevicePathProtocolGuid
,
554 (VOID
**) &This
->ParentDevicePath
,
555 TcpServiceData
->DriverBindingHandle
,
557 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
559 if (EFI_ERROR (Status
)) {
561 TcpServiceData
->IpIo
->ChildHandle
,
562 &gEfiIp4ProtocolGuid
,
563 TcpServiceData
->DriverBindingHandle
,
568 // Insert this socket into the SocketList.
570 InsertTailList (&TcpServiceData
->SocketList
, &This
->Link
);
577 Close Ip4 and device path protocols for a socket, and remove it from socket list.
579 @param This Pointer to the socket to be removed
580 @param Context Context of the socket
584 Tcp4DestroySocketCallback (
589 TCP4_SERVICE_DATA
*TcpServiceData
;
591 TcpServiceData
= ((TCP4_PROTO_DATA
*) This
->ProtoReserved
)->TcpService
;
594 // Remove this node from the list.
596 RemoveEntryList (&This
->Link
);
599 // Close the device path protocol
602 TcpServiceData
->ControllerHandle
,
603 &gEfiDevicePathProtocolGuid
,
604 TcpServiceData
->DriverBindingHandle
,
609 // Close the Ip4 protocol.
612 TcpServiceData
->IpIo
->ChildHandle
,
613 &gEfiIp4ProtocolGuid
,
614 TcpServiceData
->DriverBindingHandle
,
620 Creates a child handle and installs a protocol.
622 The CreateChild() function installs a protocol on ChildHandle. If ChildHandle
623 is a pointer to NULL, then a new handle is created and returned in ChildHandle.
624 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing
627 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
628 @param ChildHandle Pointer to the handle of the child to create. If it is NULL, then
629 a new handle is created. If it is a pointer to an existing UEFI
630 handle, then the protocol is added to the existing UEFI handle.
632 @retval EFI_SUCCES The protocol was added to ChildHandle.
633 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
634 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
636 @retval other The child handle was not created.
641 Tcp4ServiceBindingCreateChild (
642 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
643 IN OUT EFI_HANDLE
*ChildHandle
647 TCP4_SERVICE_DATA
*TcpServiceData
;
648 TCP4_PROTO_DATA TcpProto
;
652 if (NULL
== This
|| NULL
== ChildHandle
) {
653 return EFI_INVALID_PARAMETER
;
656 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
657 Status
= EFI_SUCCESS
;
658 TcpServiceData
= TCP4_FROM_THIS (This
);
659 TcpProto
.TcpService
= TcpServiceData
;
660 TcpProto
.TcpPcb
= NULL
;
663 // Create a tcp instance with defualt Tcp default
664 // sock init data and TcpProto
666 mTcp4DefaultSockData
.ProtoData
= &TcpProto
;
667 mTcp4DefaultSockData
.DataSize
= sizeof (TCP4_PROTO_DATA
);
668 mTcp4DefaultSockData
.DriverBinding
= TcpServiceData
->DriverBindingHandle
;
670 Sock
= SockCreateChild (&mTcp4DefaultSockData
);
672 DEBUG ((EFI_D_ERROR
, "Tcp4DriverBindingCreateChild: "
673 "No resource to create a Tcp Child\n"));
675 Status
= EFI_OUT_OF_RESOURCES
;
677 *ChildHandle
= Sock
->SockHandle
;
680 gBS
->RestoreTPL (OldTpl
);
686 Destroys a child handle with a protocol installed on it.
688 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
689 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
690 last protocol on ChildHandle, then ChildHandle is destroyed.
692 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
693 @param ChildHandle Handle of the child to destroy
695 @retval EFI_SUCCES The protocol was removed from ChildHandle.
696 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is
698 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.
699 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
700 because its services are being used.
701 @retval other The child handle was not destroyed.
706 Tcp4ServiceBindingDestroyChild (
707 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
708 IN EFI_HANDLE ChildHandle
712 EFI_TCP4_PROTOCOL
*Tcp4
;
716 if (NULL
== This
|| NULL
== ChildHandle
) {
717 return EFI_INVALID_PARAMETER
;
720 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
723 // retrieve the Tcp4 protocol from ChildHandle
725 Status
= gBS
->OpenProtocol (
727 &gEfiTcp4ProtocolGuid
,
729 mTcp4DriverBinding
.DriverBindingHandle
,
731 EFI_OPEN_PROTOCOL_GET_PROTOCOL
733 if (EFI_ERROR (Status
)) {
734 Status
= EFI_UNSUPPORTED
;
737 // destroy this sock and related Tcp protocol control
740 Sock
= SOCK_FROM_THIS (Tcp4
);
742 SockDestroyChild (Sock
);
745 gBS
->RestoreTPL (OldTpl
);