]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/VirtioNetDxe/SnpTransmit.c
OvmfPkg/EnrollDefaultKeys: enroll PK/KEK1 from the Type 11 SMBIOS table
[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 SPDX-License-Identifier: BSD-2-Clause-Patent
9
10 **/
11
12 #include <Library/BaseLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/UefiBootServicesTableLib.h>
15
16 #include "VirtioNet.h"
17
18 /**
19 Places a packet in the transmit queue of a network interface.
20
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
33 Transmit() function.
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
42 Types", for examples.
43
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
50 unsupported value.
51 @retval EFI_DEVICE_ERROR The command could not be sent to the network
52 interface.
53 @retval EFI_UNSUPPORTED This function is not supported by the network
54 interface.
55
56 **/
57
58 EFI_STATUS
59 EFIAPI
60 VirtioNetTransmit (
61 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
62 IN UINTN HeaderSize,
63 IN UINTN BufferSize,
64 IN /* +OUT! */ VOID *Buffer,
65 IN EFI_MAC_ADDRESS *SrcAddr OPTIONAL,
66 IN EFI_MAC_ADDRESS *DestAddr OPTIONAL,
67 IN UINT16 *Protocol OPTIONAL
68 )
69 {
70 VNET_DEV *Dev;
71 EFI_TPL OldTpl;
72 EFI_STATUS Status;
73 UINT16 DescIdx;
74 UINT16 AvailIdx;
75 EFI_PHYSICAL_ADDRESS DeviceAddress;
76
77 if (This == NULL || BufferSize == 0 || Buffer == NULL) {
78 return EFI_INVALID_PARAMETER;
79 }
80
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;
86 goto Exit;
87 case EfiSimpleNetworkStarted:
88 Status = EFI_DEVICE_ERROR;
89 goto Exit;
90 default:
91 break;
92 }
93
94 if (BufferSize < Dev->Snm.MediaHeaderSize) {
95 Status = EFI_BUFFER_TOO_SMALL;
96 goto Exit;
97 }
98 if (BufferSize > Dev->Snm.MediaHeaderSize + Dev->Snm.MaxPacketSize) {
99 Status = EFI_INVALID_PARAMETER;
100 goto Exit;
101 }
102
103 //
104 // check if we have room for transmission
105 //
106 ASSERT (Dev->TxCurPending <= Dev->TxMaxPending);
107 if (Dev->TxCurPending == Dev->TxMaxPending) {
108 Status = EFI_NOT_READY;
109 goto Exit;
110 }
111
112 //
113 // the caller may want us to fill in the media header:
114 // dst MAC, src MAC, Ethertype
115 //
116 if (HeaderSize != 0) {
117 UINT8 *Ptr;
118
119 if (HeaderSize != Dev->Snm.MediaHeaderSize ||
120 DestAddr == NULL || Protocol == NULL) {
121 Status = EFI_INVALID_PARAMETER;
122 goto Exit;
123 }
124 Ptr = Buffer;
125 ASSERT (SIZE_OF_VNET (Mac) <= sizeof (EFI_MAC_ADDRESS));
126
127 CopyMem (Ptr, DestAddr, SIZE_OF_VNET (Mac));
128 Ptr += SIZE_OF_VNET (Mac);
129
130 CopyMem (Ptr,
131 (SrcAddr == NULL) ? &Dev->Snm.CurrentAddress : SrcAddr,
132 SIZE_OF_VNET (Mac));
133 Ptr += SIZE_OF_VNET (Mac);
134
135 *Ptr++ = (UINT8) (*Protocol >> 8);
136 *Ptr++ = (UINT8) *Protocol;
137
138 ASSERT ((UINTN) (Ptr - (UINT8 *) Buffer) == Dev->Snm.MediaHeaderSize);
139 }
140
141 //
142 // Map the transmit buffer system physical address to device address.
143 //
144 Status = VirtioNetMapTxBuf (
145 Dev,
146 Buffer,
147 BufferSize,
148 &DeviceAddress
149 );
150 if (EFI_ERROR (Status)) {
151 Status = EFI_DEVICE_ERROR;
152 goto Exit;
153 }
154
155 //
156 // virtio-0.9.5, 2.4.1 Supplying Buffers to The Device
157 //
158 DescIdx = Dev->TxFreeStack[Dev->TxCurPending++];
159 Dev->TxRing.Desc[DescIdx + 1].Addr = DeviceAddress;
160 Dev->TxRing.Desc[DescIdx + 1].Len = (UINT32) BufferSize;
161
162 //
163 // the available index is never written by the host, we can read it back
164 // without a barrier
165 //
166 AvailIdx = *Dev->TxRing.Avail.Idx;
167 Dev->TxRing.Avail.Ring[AvailIdx++ % Dev->TxRing.QueueSize] = DescIdx;
168
169 MemoryFence ();
170 *Dev->TxRing.Avail.Idx = AvailIdx;
171
172 MemoryFence ();
173 Status = Dev->VirtIo->SetQueueNotify (Dev->VirtIo, VIRTIO_NET_Q_TX);
174
175 Exit:
176 gBS->RestoreTPL (OldTpl);
177 return Status;
178 }