3 Implementation of the SNP.Transmit() function and its private helpers if any.
5 Copyright (C) 2013, Red Hat, Inc.
6 Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials are licensed and made available
9 under the terms and conditions of the BSD License which accompanies this
10 distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
14 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Library/BaseLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/UefiBootServicesTableLib.h>
22 #include "VirtioNet.h"
25 Places a packet in the transmit queue of a network interface.
27 @param This The protocol instance pointer.
28 @param HeaderSize The size, in bytes, of the media header to be filled in by
29 the Transmit() function. If HeaderSize is non-zero, then
30 it must be equal to This->Mode->MediaHeaderSize and the
31 DestAddr and Protocol parameters must not be NULL.
32 @param BufferSize The size, in bytes, of the entire packet (media header and
33 data) to be transmitted through the network interface.
34 @param Buffer A pointer to the packet (media header followed by data) to
35 be transmitted. This parameter cannot be NULL. If
36 HeaderSize is zero, then the media header in Buffer must
37 already be filled in by the caller. If HeaderSize is
38 non-zero, then the media header will be filled in by the
40 @param SrcAddr The source HW MAC address. If HeaderSize is zero, then
41 this parameter is ignored. If HeaderSize is non-zero and
42 SrcAddr is NULL, then This->Mode->CurrentAddress is used
43 for the source HW MAC address.
44 @param DestAddr The destination HW MAC address. If HeaderSize is zero,
45 then this parameter is ignored.
46 @param Protocol The type of header to build. If HeaderSize is zero, then
47 this parameter is ignored. See RFC 1700, section "Ether
50 @retval EFI_SUCCESS The packet was placed on the transmit queue.
51 @retval EFI_NOT_STARTED The network interface has not been started.
52 @retval EFI_NOT_READY The network interface is too busy to accept
53 this transmit request.
54 @retval EFI_BUFFER_TOO_SMALL The BufferSize parameter is too small.
55 @retval EFI_INVALID_PARAMETER One or more of the parameters has an
57 @retval EFI_DEVICE_ERROR The command could not be sent to the network
59 @retval EFI_UNSUPPORTED This function is not supported by the network
67 IN EFI_SIMPLE_NETWORK_PROTOCOL
*This
,
70 IN
/* +OUT! */ VOID
*Buffer
,
71 IN EFI_MAC_ADDRESS
*SrcAddr OPTIONAL
,
72 IN EFI_MAC_ADDRESS
*DestAddr OPTIONAL
,
73 IN UINT16
*Protocol OPTIONAL
82 if (This
== NULL
|| BufferSize
== 0 || Buffer
== NULL
) {
83 return EFI_INVALID_PARAMETER
;
86 Dev
= VIRTIO_NET_FROM_SNP (This
);
87 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
88 switch (Dev
->Snm
.State
) {
89 case EfiSimpleNetworkStopped
:
90 Status
= EFI_NOT_STARTED
;
92 case EfiSimpleNetworkStarted
:
93 Status
= EFI_DEVICE_ERROR
;
99 if (BufferSize
< Dev
->Snm
.MediaHeaderSize
) {
100 Status
= EFI_BUFFER_TOO_SMALL
;
103 if (BufferSize
> Dev
->Snm
.MediaHeaderSize
+ Dev
->Snm
.MaxPacketSize
) {
104 Status
= EFI_INVALID_PARAMETER
;
109 // check if we have room for transmission
111 ASSERT (Dev
->TxCurPending
<= Dev
->TxMaxPending
);
112 if (Dev
->TxCurPending
== Dev
->TxMaxPending
) {
113 Status
= EFI_NOT_READY
;
118 // the caller may want us to fill in the media header:
119 // dst MAC, src MAC, Ethertype
121 if (HeaderSize
!= 0) {
124 if (HeaderSize
!= Dev
->Snm
.MediaHeaderSize
||
125 DestAddr
== NULL
|| Protocol
== NULL
) {
126 Status
= EFI_INVALID_PARAMETER
;
130 ASSERT (SIZE_OF_VNET (Mac
) <= sizeof (EFI_MAC_ADDRESS
));
132 CopyMem (Ptr
, DestAddr
, SIZE_OF_VNET (Mac
));
133 Ptr
+= SIZE_OF_VNET (Mac
);
136 (SrcAddr
== NULL
) ? &Dev
->Snm
.CurrentAddress
: SrcAddr
,
138 Ptr
+= SIZE_OF_VNET (Mac
);
140 *Ptr
++ = (UINT8
) (*Protocol
>> 8);
141 *Ptr
++ = (UINT8
) *Protocol
;
143 ASSERT ((UINTN
) (Ptr
- (UINT8
*) Buffer
) == Dev
->Snm
.MediaHeaderSize
);
147 // virtio-0.9.5, 2.4.1 Supplying Buffers to The Device
149 DescIdx
= Dev
->TxFreeStack
[Dev
->TxCurPending
++];
150 Dev
->TxRing
.Desc
[DescIdx
+ 1].Addr
= (UINTN
) Buffer
;
151 Dev
->TxRing
.Desc
[DescIdx
+ 1].Len
= (UINT32
) BufferSize
;
154 // the available index is never written by the host, we can read it back
157 AvailIdx
= *Dev
->TxRing
.Avail
.Idx
;
158 Dev
->TxRing
.Avail
.Ring
[AvailIdx
++ % Dev
->TxRing
.QueueSize
] = DescIdx
;
161 *Dev
->TxRing
.Avail
.Idx
= AvailIdx
;
164 Status
= Dev
->VirtIo
->SetQueueNotify (Dev
->VirtIo
, VIRTIO_NET_Q_TX
);
167 gBS
->RestoreTPL (OldTpl
);