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