]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/VirtioNetDxe/SnpTransmit.c
OvmfPkg/VirtioNetDxe: list "VirtioNet.h" in the INF file
[mirror_edk2.git] / OvmfPkg / VirtioNetDxe / SnpTransmit.c
1 /** @file
2
3 Implementation of the SNP.Transmit() function and its private helpers if any.
4
5 Copyright (C) 2013, Red Hat, Inc.
6 Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
7
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
12
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.
15
16 **/
17
18 #include <Library/BaseLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/UefiBootServicesTableLib.h>
21
22 #include "VirtioNet.h"
23
24 /**
25 Places a packet in the transmit queue of a network interface.
26
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
39 Transmit() function.
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
48 Types", for examples.
49
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
56 unsupported value.
57 @retval EFI_DEVICE_ERROR The command could not be sent to the network
58 interface.
59 @retval EFI_UNSUPPORTED This function is not supported by the network
60 interface.
61
62 **/
63
64 EFI_STATUS
65 EFIAPI
66 VirtioNetTransmit (
67 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
68 IN UINTN HeaderSize,
69 IN UINTN BufferSize,
70 IN /* +OUT! */ VOID *Buffer,
71 IN EFI_MAC_ADDRESS *SrcAddr OPTIONAL,
72 IN EFI_MAC_ADDRESS *DestAddr OPTIONAL,
73 IN UINT16 *Protocol OPTIONAL
74 )
75 {
76 VNET_DEV *Dev;
77 EFI_TPL OldTpl;
78 EFI_STATUS Status;
79 UINT16 DescIdx;
80 UINT16 AvailIdx;
81 EFI_PHYSICAL_ADDRESS DeviceAddress;
82
83 if (This == NULL || BufferSize == 0 || Buffer == NULL) {
84 return EFI_INVALID_PARAMETER;
85 }
86
87 Dev = VIRTIO_NET_FROM_SNP (This);
88 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
89 switch (Dev->Snm.State) {
90 case EfiSimpleNetworkStopped:
91 Status = EFI_NOT_STARTED;
92 goto Exit;
93 case EfiSimpleNetworkStarted:
94 Status = EFI_DEVICE_ERROR;
95 goto Exit;
96 default:
97 break;
98 }
99
100 if (BufferSize < Dev->Snm.MediaHeaderSize) {
101 Status = EFI_BUFFER_TOO_SMALL;
102 goto Exit;
103 }
104 if (BufferSize > Dev->Snm.MediaHeaderSize + Dev->Snm.MaxPacketSize) {
105 Status = EFI_INVALID_PARAMETER;
106 goto Exit;
107 }
108
109 //
110 // check if we have room for transmission
111 //
112 ASSERT (Dev->TxCurPending <= Dev->TxMaxPending);
113 if (Dev->TxCurPending == Dev->TxMaxPending) {
114 Status = EFI_NOT_READY;
115 goto Exit;
116 }
117
118 //
119 // the caller may want us to fill in the media header:
120 // dst MAC, src MAC, Ethertype
121 //
122 if (HeaderSize != 0) {
123 UINT8 *Ptr;
124
125 if (HeaderSize != Dev->Snm.MediaHeaderSize ||
126 DestAddr == NULL || Protocol == NULL) {
127 Status = EFI_INVALID_PARAMETER;
128 goto Exit;
129 }
130 Ptr = Buffer;
131 ASSERT (SIZE_OF_VNET (Mac) <= sizeof (EFI_MAC_ADDRESS));
132
133 CopyMem (Ptr, DestAddr, SIZE_OF_VNET (Mac));
134 Ptr += SIZE_OF_VNET (Mac);
135
136 CopyMem (Ptr,
137 (SrcAddr == NULL) ? &Dev->Snm.CurrentAddress : SrcAddr,
138 SIZE_OF_VNET (Mac));
139 Ptr += SIZE_OF_VNET (Mac);
140
141 *Ptr++ = (UINT8) (*Protocol >> 8);
142 *Ptr++ = (UINT8) *Protocol;
143
144 ASSERT ((UINTN) (Ptr - (UINT8 *) Buffer) == Dev->Snm.MediaHeaderSize);
145 }
146
147 //
148 // Map the transmit buffer system physical address to device address.
149 //
150 Status = VirtioNetMapTxBuf (
151 Dev,
152 Buffer,
153 BufferSize,
154 &DeviceAddress
155 );
156 if (EFI_ERROR (Status)) {
157 Status = EFI_DEVICE_ERROR;
158 goto Exit;
159 }
160
161 //
162 // virtio-0.9.5, 2.4.1 Supplying Buffers to The Device
163 //
164 DescIdx = Dev->TxFreeStack[Dev->TxCurPending++];
165 Dev->TxRing.Desc[DescIdx + 1].Addr = DeviceAddress;
166 Dev->TxRing.Desc[DescIdx + 1].Len = (UINT32) BufferSize;
167
168 //
169 // the available index is never written by the host, we can read it back
170 // without a barrier
171 //
172 AvailIdx = *Dev->TxRing.Avail.Idx;
173 Dev->TxRing.Avail.Ring[AvailIdx++ % Dev->TxRing.QueueSize] = DescIdx;
174
175 MemoryFence ();
176 *Dev->TxRing.Avail.Idx = AvailIdx;
177
178 MemoryFence ();
179 Status = Dev->VirtIo->SetQueueNotify (Dev->VirtIo, VIRTIO_NET_Q_TX);
180
181 Exit:
182 gBS->RestoreTPL (OldTpl);
183 return Status;
184 }