]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/VirtioNetDxe/SnpGetStatus.c
OvmfPkg: VirtioNetDxe: implement Tx: SNP.Transmit and SNP.GetStatus
[mirror_edk2.git] / OvmfPkg / VirtioNetDxe / SnpGetStatus.c
1 /** @file
2
3 Implementation of the SNP.GetStatus() function and its private helpers if
4 any.
5
6 Copyright (C) 2013, Red Hat, Inc.
7 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
8
9 This program and the accompanying materials are licensed and made available
10 under the terms and conditions of the BSD License which accompanies this
11 distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
13
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
15 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16
17 **/
18
19 #include <Library/BaseLib.h>
20 #include <Library/UefiBootServicesTableLib.h>
21
22 #include "VirtioNet.h"
23
24 /**
25 Reads the current interrupt status and recycled transmit buffer status from
26 a network interface.
27
28 @param This The protocol instance pointer.
29 @param InterruptStatus A pointer to the bit mask of the currently active
30 interrupts If this is NULL, the interrupt status will
31 not be read from the device. If this is not NULL, the
32 interrupt status will be read from the device. When
33 the interrupt status is read, it will also be
34 cleared. Clearing the transmit interrupt does not
35 empty the recycled transmit buffer array.
36 @param TxBuf Recycled transmit buffer address. The network
37 interface will not transmit if its internal recycled
38 transmit buffer array is full. Reading the transmit
39 buffer does not clear the transmit interrupt. If this
40 is NULL, then the transmit buffer status will not be
41 read. If there are no transmit buffers to recycle and
42 TxBuf is not NULL, * TxBuf will be set to NULL.
43
44 @retval EFI_SUCCESS The status of the network interface was
45 retrieved.
46 @retval EFI_NOT_STARTED The network interface has not been started.
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 VirtioNetGetStatus (
59 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
60 OUT UINT32 *InterruptStatus OPTIONAL,
61 OUT VOID **TxBuf OPTIONAL
62 )
63 {
64 VNET_DEV *Dev;
65 EFI_TPL OldTpl;
66 EFI_STATUS Status;
67 UINT16 RxCurUsed;
68 UINT16 TxCurUsed;
69
70 if (This == NULL) {
71 return EFI_INVALID_PARAMETER;
72 }
73
74 Dev = VIRTIO_NET_FROM_SNP (This);
75 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
76 switch (Dev->Snm.State) {
77 case EfiSimpleNetworkStopped:
78 Status = EFI_NOT_STARTED;
79 goto Exit;
80 case EfiSimpleNetworkStarted:
81 Status = EFI_DEVICE_ERROR;
82 goto Exit;
83 default:
84 break;
85 }
86
87 //
88 // update link status
89 //
90 if (Dev->Snm.MediaPresentSupported) {
91 UINT16 LinkStatus;
92
93 Status = VIRTIO_CFG_READ (Dev, VhdrLinkStatus, &LinkStatus);
94 if (EFI_ERROR (Status)) {
95 goto Exit;
96 }
97 Dev->Snm.MediaPresent = !!(LinkStatus & VIRTIO_NET_S_LINK_UP);
98 }
99
100 //
101 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
102 //
103 MemoryFence ();
104 RxCurUsed = *Dev->RxRing.Used.Idx;
105 TxCurUsed = *Dev->TxRing.Used.Idx;
106
107 if (InterruptStatus != NULL) {
108 //
109 // report the receive interrupt if there is data available for reception,
110 // report the transmit interrupt if we have transmitted at least one buffer
111 //
112 *InterruptStatus = 0;
113 if (Dev->RxLastUsed != RxCurUsed) {
114 *InterruptStatus |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
115 }
116 if (Dev->TxLastUsed != TxCurUsed) {
117 ASSERT (Dev->TxCurPending > 0);
118 *InterruptStatus |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
119 }
120 }
121
122 if (TxBuf != NULL) {
123 if (Dev->TxLastUsed == TxCurUsed) {
124 *TxBuf = NULL;
125 }
126 else {
127 UINT16 UsedElemIdx;
128 UINT32 DescIdx;
129
130 //
131 // fetch the first descriptor among those that the hypervisor reports
132 // completed
133 //
134 ASSERT (Dev->TxCurPending > 0);
135 ASSERT (Dev->TxCurPending <= Dev->TxMaxPending);
136
137 UsedElemIdx = Dev->TxLastUsed++ % Dev->TxRing.QueueSize;
138 DescIdx = Dev->TxRing.Used.UsedElem[UsedElemIdx].Id;
139 ASSERT (DescIdx < 2 * Dev->TxMaxPending - 1);
140
141 //
142 // report buffer address to caller that has been enqueued by caller
143 //
144 *TxBuf = (VOID *) Dev->TxRing.Desc[DescIdx + 1].Addr;
145
146 //
147 // now this descriptor can be used again to enqueue a transmit buffer
148 //
149 Dev->TxFreeStack[--Dev->TxCurPending] = (UINT16) DescIdx;
150 }
151 }
152
153 Status = EFI_SUCCESS;
154
155 Exit:
156 gBS->RestoreTPL (OldTpl);
157 return Status;
158 }