]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/VirtioNetDxe/SnpReceive.c
Nt32Pkg: Forbid creation of non-spec variables in EFI_GLOBAL_VARIABLE namespace.
[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
108\r
109 if (Dev->RxLastUsed == RxCurUsed) {\r
110 Status = EFI_NOT_READY;\r
111 goto Exit;\r
112 }\r
113\r
114 UsedElemIdx = Dev->RxLastUsed % Dev->RxRing.QueueSize;\r
115 DescIdx = Dev->RxRing.Used.UsedElem[UsedElemIdx].Id;\r
116 RxLen = Dev->RxRing.Used.UsedElem[UsedElemIdx].Len;\r
117\r
118 //\r
119 // the virtio-net request header must be complete; we skip it\r
120 //\r
121 ASSERT (RxLen >= Dev->RxRing.Desc[DescIdx].Len);\r
122 RxLen -= Dev->RxRing.Desc[DescIdx].Len;\r
123 //\r
124 // the host must not have filled in more data than requested\r
125 //\r
126 ASSERT (RxLen <= Dev->RxRing.Desc[DescIdx + 1].Len);\r
127\r
128 OrigBufferSize = *BufferSize;\r
129 *BufferSize = RxLen;\r
130\r
131 if (OrigBufferSize < RxLen) {\r
132 Status = EFI_BUFFER_TOO_SMALL;\r
133 goto Exit; // keep the packet\r
134 }\r
135\r
136 if (RxLen < Dev->Snm.MediaHeaderSize) {\r
137 Status = EFI_DEVICE_ERROR;\r
138 goto RecycleDesc; // drop useless short packet\r
139 }\r
140\r
141 if (HeaderSize != NULL) {\r
142 *HeaderSize = Dev->Snm.MediaHeaderSize;\r
143 }\r
144\r
8258c4e6 145 RxPtr = (UINT8 *)(UINTN) Dev->RxRing.Desc[DescIdx + 1].Addr;\r
e12fadc3
LE
146 CopyMem (Buffer, RxPtr, RxLen);\r
147\r
148 if (DestAddr != NULL) {\r
149 CopyMem (DestAddr, RxPtr, SIZE_OF_VNET (VhdrMac));\r
150 }\r
151 RxPtr += SIZE_OF_VNET (VhdrMac);\r
152\r
153 if (SrcAddr != NULL) {\r
154 CopyMem (SrcAddr, RxPtr, SIZE_OF_VNET (VhdrMac));\r
155 }\r
156 RxPtr += SIZE_OF_VNET (VhdrMac);\r
157\r
158 if (Protocol != NULL) {\r
9f3acbb5 159 *Protocol = (UINT16) ((RxPtr[0] << 8) | RxPtr[1]);\r
e12fadc3
LE
160 }\r
161 RxPtr += sizeof (UINT16);\r
162\r
163 Status = EFI_SUCCESS;\r
164\r
165RecycleDesc:\r
166 ++Dev->RxLastUsed;\r
167\r
168 //\r
169 // virtio-0.9.5, 2.4.1 Supplying Buffers to The Device\r
170 //\r
171 AvailIdx = *Dev->RxRing.Avail.Idx;\r
9f3acbb5
LE
172 Dev->RxRing.Avail.Ring[AvailIdx++ % Dev->RxRing.QueueSize] =\r
173 (UINT16) DescIdx;\r
e12fadc3
LE
174\r
175 MemoryFence ();\r
176 *Dev->RxRing.Avail.Idx = AvailIdx;\r
177\r
178 MemoryFence ();\r
179 NotifyStatus = VIRTIO_CFG_WRITE (Dev, Generic.VhdrQueueNotify,\r
180 VIRTIO_NET_Q_RX);\r
181\r
182 if (!EFI_ERROR (Status)) { // earlier error takes precedence\r
183 Status = NotifyStatus;\r
184 }\r
185\r
186Exit:\r
187 gBS->RestoreTPL (OldTpl);\r
188 return Status;\r
189}\r