3 Implementation of the SNP.Receive() function and its private helpers if any.
5 Copyright (C) 2013, Red Hat, Inc.
6 Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
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
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.
18 #include <Library/BaseLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/UefiBootServicesTableLib.h>
22 #include "VirtioNet.h"
25 Receives a packet from a network interface.
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
34 @param Buffer A pointer to the data buffer to receive both the media
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
39 @param DestAddr The destination HW MAC address. If this parameter is NULL,
40 the HW MAC destination address will not be extracted from
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.
46 @retval EFI_SUCCESS The received data was stored in Buffer, and
47 BufferSize has been updated to the number of
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
55 @retval EFI_DEVICE_ERROR The command could not be sent to the network
57 @retval EFI_UNSUPPORTED This function is not supported by the network
65 IN EFI_SIMPLE_NETWORK_PROTOCOL
*This
,
66 OUT UINTN
*HeaderSize OPTIONAL
,
67 IN OUT UINTN
*BufferSize
,
69 OUT EFI_MAC_ADDRESS
*SrcAddr OPTIONAL
,
70 OUT EFI_MAC_ADDRESS
*DestAddr OPTIONAL
,
71 OUT UINT16
*Protocol OPTIONAL
84 EFI_STATUS NotifyStatus
;
87 if (This
== NULL
|| BufferSize
== NULL
|| Buffer
== NULL
) {
88 return EFI_INVALID_PARAMETER
;
91 Dev
= VIRTIO_NET_FROM_SNP (This
);
92 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
93 switch (Dev
->Snm
.State
) {
94 case EfiSimpleNetworkStopped
:
95 Status
= EFI_NOT_STARTED
;
97 case EfiSimpleNetworkStarted
:
98 Status
= EFI_DEVICE_ERROR
;
105 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
108 RxCurUsed
= *Dev
->RxRing
.Used
.Idx
;
111 if (Dev
->RxLastUsed
== RxCurUsed
) {
112 Status
= EFI_NOT_READY
;
116 UsedElemIdx
= Dev
->RxLastUsed
% Dev
->RxRing
.QueueSize
;
117 DescIdx
= Dev
->RxRing
.Used
.UsedElem
[UsedElemIdx
].Id
;
118 RxLen
= Dev
->RxRing
.Used
.UsedElem
[UsedElemIdx
].Len
;
121 // the virtio-net request header must be complete; we skip it
123 ASSERT (RxLen
>= Dev
->RxRing
.Desc
[DescIdx
].Len
);
124 RxLen
-= Dev
->RxRing
.Desc
[DescIdx
].Len
;
126 // the host must not have filled in more data than requested
128 ASSERT (RxLen
<= Dev
->RxRing
.Desc
[DescIdx
+ 1].Len
);
130 OrigBufferSize
= *BufferSize
;
133 if (OrigBufferSize
< RxLen
) {
134 Status
= EFI_BUFFER_TOO_SMALL
;
135 goto Exit
; // keep the packet
138 if (RxLen
< Dev
->Snm
.MediaHeaderSize
) {
139 Status
= EFI_DEVICE_ERROR
;
140 goto RecycleDesc
; // drop useless short packet
143 if (HeaderSize
!= NULL
) {
144 *HeaderSize
= Dev
->Snm
.MediaHeaderSize
;
147 RxBufOffset
= (UINTN
)(Dev
->RxRing
.Desc
[DescIdx
+ 1].Addr
-
148 Dev
->RxBufDeviceBase
);
149 RxPtr
= Dev
->RxBuf
+ RxBufOffset
;
150 CopyMem (Buffer
, RxPtr
, RxLen
);
152 if (DestAddr
!= NULL
) {
153 CopyMem (DestAddr
, RxPtr
, SIZE_OF_VNET (Mac
));
155 RxPtr
+= SIZE_OF_VNET (Mac
);
157 if (SrcAddr
!= NULL
) {
158 CopyMem (SrcAddr
, RxPtr
, SIZE_OF_VNET (Mac
));
160 RxPtr
+= SIZE_OF_VNET (Mac
);
162 if (Protocol
!= NULL
) {
163 *Protocol
= (UINT16
) ((RxPtr
[0] << 8) | RxPtr
[1]);
165 RxPtr
+= sizeof (UINT16
);
167 Status
= EFI_SUCCESS
;
173 // virtio-0.9.5, 2.4.1 Supplying Buffers to The Device
175 AvailIdx
= *Dev
->RxRing
.Avail
.Idx
;
176 Dev
->RxRing
.Avail
.Ring
[AvailIdx
++ % Dev
->RxRing
.QueueSize
] =
180 *Dev
->RxRing
.Avail
.Idx
= AvailIdx
;
183 NotifyStatus
= Dev
->VirtIo
->SetQueueNotify (Dev
->VirtIo
, VIRTIO_NET_Q_RX
);
184 if (!EFI_ERROR (Status
)) { // earlier error takes precedence
185 Status
= NotifyStatus
;
189 gBS
->RestoreTPL (OldTpl
);