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 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <Library/BaseLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/UefiBootServicesTableLib.h>
16 #include "VirtioNet.h"
19 Places a packet in the transmit queue of a network interface.
21 @param This The protocol instance pointer.
22 @param HeaderSize The size, in bytes, of the media header to be filled in by
23 the Transmit() function. If HeaderSize is non-zero, then
24 it must be equal to This->Mode->MediaHeaderSize and the
25 DestAddr and Protocol parameters must not be NULL.
26 @param BufferSize The size, in bytes, of the entire packet (media header and
27 data) to be transmitted through the network interface.
28 @param Buffer A pointer to the packet (media header followed by data) to
29 be transmitted. This parameter cannot be NULL. If
30 HeaderSize is zero, then the media header in Buffer must
31 already be filled in by the caller. If HeaderSize is
32 non-zero, then the media header will be filled in by the
34 @param SrcAddr The source HW MAC address. If HeaderSize is zero, then
35 this parameter is ignored. If HeaderSize is non-zero and
36 SrcAddr is NULL, then This->Mode->CurrentAddress is used
37 for the source HW MAC address.
38 @param DestAddr The destination HW MAC address. If HeaderSize is zero,
39 then this parameter is ignored.
40 @param Protocol The type of header to build. If HeaderSize is zero, then
41 this parameter is ignored. See RFC 1700, section "Ether
44 @retval EFI_SUCCESS The packet was placed on the transmit queue.
45 @retval EFI_NOT_STARTED The network interface has not been started.
46 @retval EFI_NOT_READY The network interface is too busy to accept
47 this transmit request.
48 @retval EFI_BUFFER_TOO_SMALL The BufferSize parameter is too small.
49 @retval EFI_INVALID_PARAMETER One or more of the parameters has an
51 @retval EFI_DEVICE_ERROR The command could not be sent to the network
53 @retval EFI_UNSUPPORTED This function is not supported by the network
61 IN EFI_SIMPLE_NETWORK_PROTOCOL
*This
,
64 IN
/* +OUT! */ VOID
*Buffer
,
65 IN EFI_MAC_ADDRESS
*SrcAddr OPTIONAL
,
66 IN EFI_MAC_ADDRESS
*DestAddr OPTIONAL
,
67 IN UINT16
*Protocol OPTIONAL
75 EFI_PHYSICAL_ADDRESS DeviceAddress
;
77 if (This
== NULL
|| BufferSize
== 0 || Buffer
== NULL
) {
78 return EFI_INVALID_PARAMETER
;
81 Dev
= VIRTIO_NET_FROM_SNP (This
);
82 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
83 switch (Dev
->Snm
.State
) {
84 case EfiSimpleNetworkStopped
:
85 Status
= EFI_NOT_STARTED
;
87 case EfiSimpleNetworkStarted
:
88 Status
= EFI_DEVICE_ERROR
;
94 if (BufferSize
< Dev
->Snm
.MediaHeaderSize
) {
95 Status
= EFI_BUFFER_TOO_SMALL
;
98 if (BufferSize
> Dev
->Snm
.MediaHeaderSize
+ Dev
->Snm
.MaxPacketSize
) {
99 Status
= EFI_INVALID_PARAMETER
;
104 // check if we have room for transmission
106 ASSERT (Dev
->TxCurPending
<= Dev
->TxMaxPending
);
107 if (Dev
->TxCurPending
== Dev
->TxMaxPending
) {
108 Status
= EFI_NOT_READY
;
113 // the caller may want us to fill in the media header:
114 // dst MAC, src MAC, Ethertype
116 if (HeaderSize
!= 0) {
119 if (HeaderSize
!= Dev
->Snm
.MediaHeaderSize
||
120 DestAddr
== NULL
|| Protocol
== NULL
) {
121 Status
= EFI_INVALID_PARAMETER
;
125 ASSERT (SIZE_OF_VNET (Mac
) <= sizeof (EFI_MAC_ADDRESS
));
127 CopyMem (Ptr
, DestAddr
, SIZE_OF_VNET (Mac
));
128 Ptr
+= SIZE_OF_VNET (Mac
);
131 (SrcAddr
== NULL
) ? &Dev
->Snm
.CurrentAddress
: SrcAddr
,
133 Ptr
+= SIZE_OF_VNET (Mac
);
135 *Ptr
++ = (UINT8
) (*Protocol
>> 8);
136 *Ptr
++ = (UINT8
) *Protocol
;
138 ASSERT ((UINTN
) (Ptr
- (UINT8
*) Buffer
) == Dev
->Snm
.MediaHeaderSize
);
142 // Map the transmit buffer system physical address to device address.
144 Status
= VirtioNetMapTxBuf (
150 if (EFI_ERROR (Status
)) {
151 Status
= EFI_DEVICE_ERROR
;
156 // virtio-0.9.5, 2.4.1 Supplying Buffers to The Device
158 DescIdx
= Dev
->TxFreeStack
[Dev
->TxCurPending
++];
159 Dev
->TxRing
.Desc
[DescIdx
+ 1].Addr
= DeviceAddress
;
160 Dev
->TxRing
.Desc
[DescIdx
+ 1].Len
= (UINT32
) BufferSize
;
163 // the available index is never written by the host, we can read it back
166 AvailIdx
= *Dev
->TxRing
.Avail
.Idx
;
167 Dev
->TxRing
.Avail
.Ring
[AvailIdx
++ % Dev
->TxRing
.QueueSize
] = DescIdx
;
170 *Dev
->TxRing
.Avail
.Idx
= AvailIdx
;
173 Status
= Dev
->VirtIo
->SetQueueNotify (Dev
->VirtIo
, VIRTIO_NET_Q_TX
);
176 gBS
->RestoreTPL (OldTpl
);