]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/VirtioNetDxe/SnpReceive.c
OvmfPkg/AcpiPlatformDxe: Use Xen PVH RSDP if it exist
[mirror_edk2.git] / OvmfPkg / VirtioNetDxe / SnpReceive.c
CommitLineData
e12fadc3
LE
1/** @file\r
2\r
3 Implementation of the SNP.Receive() function and its private helpers if any.\r
4\r
5 Copyright (C) 2013, Red Hat, Inc.\r
8258c4e6 6 Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
e12fadc3 7\r
b26f0cf9 8 SPDX-License-Identifier: BSD-2-Clause-Patent\r
e12fadc3
LE
9\r
10**/\r
11\r
12#include <Library/BaseLib.h>\r
13#include <Library/BaseMemoryLib.h>\r
14#include <Library/UefiBootServicesTableLib.h>\r
15\r
16#include "VirtioNet.h"\r
17\r
18/**\r
19 Receives a packet from a network interface.\r
20\r
21 @param This The protocol instance pointer.\r
22 @param HeaderSize The size, in bytes, of the media header received on the\r
23 network interface. If this parameter is NULL, then the\r
24 media header size will not be returned.\r
25 @param BufferSize On entry, the size, in bytes, of Buffer. On exit, the\r
26 size, in bytes, of the packet that was received on the\r
27 network interface.\r
28 @param Buffer A pointer to the data buffer to receive both the media\r
29 header and the data.\r
30 @param SrcAddr The source HW MAC address. If this parameter is NULL, the\r
31 HW MAC source address will not be extracted from the media\r
32 header.\r
33 @param DestAddr The destination HW MAC address. If this parameter is NULL,\r
34 the HW MAC destination address will not be extracted from\r
35 the media header.\r
36 @param Protocol The media header type. If this parameter is NULL, then the\r
37 protocol will not be extracted from the media header. See\r
38 RFC 1700 section "Ether Types" for examples.\r
39\r
40 @retval EFI_SUCCESS The received data was stored in Buffer, and\r
41 BufferSize has been updated to the number of\r
42 bytes received.\r
43 @retval EFI_NOT_STARTED The network interface has not been started.\r
44 @retval EFI_NOT_READY The network interface is too busy to accept\r
45 this transmit request.\r
46 @retval EFI_BUFFER_TOO_SMALL The BufferSize parameter is too small.\r
47 @retval EFI_INVALID_PARAMETER One or more of the parameters has an\r
48 unsupported value.\r
49 @retval EFI_DEVICE_ERROR The command could not be sent to the network\r
50 interface.\r
51 @retval EFI_UNSUPPORTED This function is not supported by the network\r
52 interface.\r
53\r
54**/\r
55\r
56EFI_STATUS\r
57EFIAPI\r
58VirtioNetReceive (\r
59 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,\r
60 OUT UINTN *HeaderSize OPTIONAL,\r
61 IN OUT UINTN *BufferSize,\r
62 OUT VOID *Buffer,\r
63 OUT EFI_MAC_ADDRESS *SrcAddr OPTIONAL,\r
64 OUT EFI_MAC_ADDRESS *DestAddr OPTIONAL,\r
65 OUT UINT16 *Protocol OPTIONAL\r
66 )\r
67{\r
68 VNET_DEV *Dev;\r
69 EFI_TPL OldTpl;\r
70 EFI_STATUS Status;\r
71 UINT16 RxCurUsed;\r
72 UINT16 UsedElemIdx;\r
73 UINT32 DescIdx;\r
74 UINT32 RxLen;\r
75 UINTN OrigBufferSize;\r
76 UINT8 *RxPtr;\r
77 UINT16 AvailIdx;\r
78 EFI_STATUS NotifyStatus;\r
46b11f00 79 UINTN RxBufOffset;\r
e12fadc3
LE
80\r
81 if (This == NULL || BufferSize == NULL || Buffer == NULL) {\r
82 return EFI_INVALID_PARAMETER;\r
83 }\r
84\r
85 Dev = VIRTIO_NET_FROM_SNP (This);\r
86 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
87 switch (Dev->Snm.State) {\r
88 case EfiSimpleNetworkStopped:\r
89 Status = EFI_NOT_STARTED;\r
90 goto Exit;\r
91 case EfiSimpleNetworkStarted:\r
92 Status = EFI_DEVICE_ERROR;\r
93 goto Exit;\r
94 default:\r
95 break;\r
96 }\r
97\r
98 //\r
99 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device\r
100 //\r
101 MemoryFence ();\r
102 RxCurUsed = *Dev->RxRing.Used.Idx;\r
dc9447bd 103 MemoryFence ();\r
e12fadc3
LE
104\r
105 if (Dev->RxLastUsed == RxCurUsed) {\r
106 Status = EFI_NOT_READY;\r
107 goto Exit;\r
108 }\r
109\r
110 UsedElemIdx = Dev->RxLastUsed % Dev->RxRing.QueueSize;\r
111 DescIdx = Dev->RxRing.Used.UsedElem[UsedElemIdx].Id;\r
112 RxLen = Dev->RxRing.Used.UsedElem[UsedElemIdx].Len;\r
113\r
114 //\r
115 // the virtio-net request header must be complete; we skip it\r
116 //\r
117 ASSERT (RxLen >= Dev->RxRing.Desc[DescIdx].Len);\r
118 RxLen -= Dev->RxRing.Desc[DescIdx].Len;\r
119 //\r
120 // the host must not have filled in more data than requested\r
121 //\r
122 ASSERT (RxLen <= Dev->RxRing.Desc[DescIdx + 1].Len);\r
123\r
124 OrigBufferSize = *BufferSize;\r
125 *BufferSize = RxLen;\r
126\r
127 if (OrigBufferSize < RxLen) {\r
128 Status = EFI_BUFFER_TOO_SMALL;\r
129 goto Exit; // keep the packet\r
130 }\r
131\r
132 if (RxLen < Dev->Snm.MediaHeaderSize) {\r
133 Status = EFI_DEVICE_ERROR;\r
134 goto RecycleDesc; // drop useless short packet\r
135 }\r
136\r
137 if (HeaderSize != NULL) {\r
138 *HeaderSize = Dev->Snm.MediaHeaderSize;\r
139 }\r
140\r
46b11f00
BS
141 RxBufOffset = (UINTN)(Dev->RxRing.Desc[DescIdx + 1].Addr -\r
142 Dev->RxBufDeviceBase);\r
143 RxPtr = Dev->RxBuf + RxBufOffset;\r
e12fadc3
LE
144 CopyMem (Buffer, RxPtr, RxLen);\r
145\r
146 if (DestAddr != NULL) {\r
56f65ed8 147 CopyMem (DestAddr, RxPtr, SIZE_OF_VNET (Mac));\r
e12fadc3 148 }\r
56f65ed8 149 RxPtr += SIZE_OF_VNET (Mac);\r
e12fadc3
LE
150\r
151 if (SrcAddr != NULL) {\r
56f65ed8 152 CopyMem (SrcAddr, RxPtr, SIZE_OF_VNET (Mac));\r
e12fadc3 153 }\r
56f65ed8 154 RxPtr += SIZE_OF_VNET (Mac);\r
e12fadc3
LE
155\r
156 if (Protocol != NULL) {\r
9f3acbb5 157 *Protocol = (UINT16) ((RxPtr[0] << 8) | RxPtr[1]);\r
e12fadc3
LE
158 }\r
159 RxPtr += sizeof (UINT16);\r
160\r
161 Status = EFI_SUCCESS;\r
162\r
163RecycleDesc:\r
164 ++Dev->RxLastUsed;\r
165\r
166 //\r
167 // virtio-0.9.5, 2.4.1 Supplying Buffers to The Device\r
168 //\r
169 AvailIdx = *Dev->RxRing.Avail.Idx;\r
9f3acbb5
LE
170 Dev->RxRing.Avail.Ring[AvailIdx++ % Dev->RxRing.QueueSize] =\r
171 (UINT16) DescIdx;\r
e12fadc3
LE
172\r
173 MemoryFence ();\r
174 *Dev->RxRing.Avail.Idx = AvailIdx;\r
175\r
176 MemoryFence ();\r
56f65ed8 177 NotifyStatus = Dev->VirtIo->SetQueueNotify (Dev->VirtIo, VIRTIO_NET_Q_RX);\r
e12fadc3
LE
178 if (!EFI_ERROR (Status)) { // earlier error takes precedence\r
179 Status = NotifyStatus;\r
180 }\r
181\r
182Exit:\r
183 gBS->RestoreTPL (OldTpl);\r
184 return Status;\r
185}\r