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