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 | EFI_STATUS
|
---|
56 | EFIAPI
|
---|
57 | VirtioNetReceive (
|
---|
58 | IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
|
---|
59 | OUT UINTN *HeaderSize OPTIONAL,
|
---|
60 | IN OUT UINTN *BufferSize,
|
---|
61 | OUT VOID *Buffer,
|
---|
62 | OUT EFI_MAC_ADDRESS *SrcAddr OPTIONAL,
|
---|
63 | OUT EFI_MAC_ADDRESS *DestAddr OPTIONAL,
|
---|
64 | OUT UINT16 *Protocol OPTIONAL
|
---|
65 | )
|
---|
66 | {
|
---|
67 | VNET_DEV *Dev;
|
---|
68 | EFI_TPL OldTpl;
|
---|
69 | EFI_STATUS Status;
|
---|
70 | UINT16 RxCurUsed;
|
---|
71 | UINT16 UsedElemIdx;
|
---|
72 | UINT32 DescIdx;
|
---|
73 | UINT32 RxLen;
|
---|
74 | UINTN OrigBufferSize;
|
---|
75 | UINT8 *RxPtr;
|
---|
76 | UINT16 AvailIdx;
|
---|
77 | EFI_STATUS NotifyStatus;
|
---|
78 | UINTN RxBufOffset;
|
---|
79 |
|
---|
80 | if ((This == NULL) || (BufferSize == NULL) || (Buffer == NULL)) {
|
---|
81 | return EFI_INVALID_PARAMETER;
|
---|
82 | }
|
---|
83 |
|
---|
84 | Dev = VIRTIO_NET_FROM_SNP (This);
|
---|
85 | OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
|
---|
86 | switch (Dev->Snm.State) {
|
---|
87 | case EfiSimpleNetworkStopped:
|
---|
88 | Status = EFI_NOT_STARTED;
|
---|
89 | goto Exit;
|
---|
90 | case EfiSimpleNetworkStarted:
|
---|
91 | Status = EFI_DEVICE_ERROR;
|
---|
92 | goto Exit;
|
---|
93 | default:
|
---|
94 | break;
|
---|
95 | }
|
---|
96 |
|
---|
97 | //
|
---|
98 | // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
|
---|
99 | //
|
---|
100 | MemoryFence ();
|
---|
101 | RxCurUsed = *Dev->RxRing.Used.Idx;
|
---|
102 | MemoryFence ();
|
---|
103 |
|
---|
104 | if (Dev->RxLastUsed == RxCurUsed) {
|
---|
105 | Status = EFI_NOT_READY;
|
---|
106 | goto Exit;
|
---|
107 | }
|
---|
108 |
|
---|
109 | UsedElemIdx = Dev->RxLastUsed % Dev->RxRing.QueueSize;
|
---|
110 | DescIdx = Dev->RxRing.Used.UsedElem[UsedElemIdx].Id;
|
---|
111 | RxLen = Dev->RxRing.Used.UsedElem[UsedElemIdx].Len;
|
---|
112 |
|
---|
113 | //
|
---|
114 | // the virtio-net request header must be complete; we skip it
|
---|
115 | //
|
---|
116 | ASSERT (RxLen >= Dev->RxRing.Desc[DescIdx].Len);
|
---|
117 | RxLen -= Dev->RxRing.Desc[DescIdx].Len;
|
---|
118 | //
|
---|
119 | // the host must not have filled in more data than requested
|
---|
120 | //
|
---|
121 | ASSERT (RxLen <= Dev->RxRing.Desc[DescIdx + 1].Len);
|
---|
122 |
|
---|
123 | OrigBufferSize = *BufferSize;
|
---|
124 | *BufferSize = RxLen;
|
---|
125 |
|
---|
126 | if (OrigBufferSize < RxLen) {
|
---|
127 | Status = EFI_BUFFER_TOO_SMALL;
|
---|
128 | goto Exit; // keep the packet
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (RxLen < Dev->Snm.MediaHeaderSize) {
|
---|
132 | Status = EFI_DEVICE_ERROR;
|
---|
133 | goto RecycleDesc; // drop useless short packet
|
---|
134 | }
|
---|
135 |
|
---|
136 | if (HeaderSize != NULL) {
|
---|
137 | *HeaderSize = Dev->Snm.MediaHeaderSize;
|
---|
138 | }
|
---|
139 |
|
---|
140 | RxBufOffset = (UINTN)(Dev->RxRing.Desc[DescIdx + 1].Addr -
|
---|
141 | Dev->RxBufDeviceBase);
|
---|
142 | RxPtr = Dev->RxBuf + RxBufOffset;
|
---|
143 | CopyMem (Buffer, RxPtr, RxLen);
|
---|
144 |
|
---|
145 | if (DestAddr != NULL) {
|
---|
146 | CopyMem (DestAddr, RxPtr, SIZE_OF_VNET (Mac));
|
---|
147 | }
|
---|
148 |
|
---|
149 | RxPtr += SIZE_OF_VNET (Mac);
|
---|
150 |
|
---|
151 | if (SrcAddr != NULL) {
|
---|
152 | CopyMem (SrcAddr, RxPtr, SIZE_OF_VNET (Mac));
|
---|
153 | }
|
---|
154 |
|
---|
155 | RxPtr += SIZE_OF_VNET (Mac);
|
---|
156 |
|
---|
157 | if (Protocol != NULL) {
|
---|
158 | *Protocol = (UINT16)((RxPtr[0] << 8) | RxPtr[1]);
|
---|
159 | }
|
---|
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 = Dev->VirtIo->SetQueueNotify (Dev->VirtIo, VIRTIO_NET_Q_RX);
|
---|
180 | if (!EFI_ERROR (Status)) {
|
---|
181 | // earlier error takes precedence
|
---|
182 | Status = NotifyStatus;
|
---|
183 | }
|
---|
184 |
|
---|
185 | Exit:
|
---|
186 | gBS->RestoreTPL (OldTpl);
|
---|
187 | return Status;
|
---|
188 | }
|
---|