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