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