]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/VirtioNetDxe/SnpGetStatus.c
ShellPkg/for: Fix potential null pointer deference
[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
c4046161 7 Copyright (c) 2006 - 2014, 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
8fa54a8a
BS
64 VNET_DEV *Dev;\r
65 EFI_TPL OldTpl;\r
66 EFI_STATUS Status;\r
67 UINT16 RxCurUsed;\r
68 UINT16 TxCurUsed;\r
69 EFI_PHYSICAL_ADDRESS DeviceAddress;\r
b6dfc654
LE
70\r
71 if (This == NULL) {\r
72 return EFI_INVALID_PARAMETER;\r
73 }\r
74\r
75 Dev = VIRTIO_NET_FROM_SNP (This);\r
76 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
77 switch (Dev->Snm.State) {\r
78 case EfiSimpleNetworkStopped:\r
79 Status = EFI_NOT_STARTED;\r
80 goto Exit;\r
81 case EfiSimpleNetworkStarted:\r
82 Status = EFI_DEVICE_ERROR;\r
83 goto Exit;\r
84 default:\r
85 break;\r
86 }\r
87\r
88 //\r
89 // update link status\r
90 //\r
91 if (Dev->Snm.MediaPresentSupported) {\r
92 UINT16 LinkStatus;\r
93\r
56f65ed8 94 Status = VIRTIO_CFG_READ (Dev, LinkStatus, &LinkStatus);\r
b6dfc654
LE
95 if (EFI_ERROR (Status)) {\r
96 goto Exit;\r
97 }\r
c4046161
JJ
98 Dev->Snm.MediaPresent =\r
99 (BOOLEAN) ((LinkStatus & VIRTIO_NET_S_LINK_UP) != 0);\r
b6dfc654
LE
100 }\r
101\r
102 //\r
103 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device\r
104 //\r
105 MemoryFence ();\r
106 RxCurUsed = *Dev->RxRing.Used.Idx;\r
107 TxCurUsed = *Dev->TxRing.Used.Idx;\r
dc9447bd 108 MemoryFence ();\r
b6dfc654
LE
109\r
110 if (InterruptStatus != NULL) {\r
111 //\r
112 // report the receive interrupt if there is data available for reception,\r
113 // report the transmit interrupt if we have transmitted at least one buffer\r
114 //\r
115 *InterruptStatus = 0;\r
116 if (Dev->RxLastUsed != RxCurUsed) {\r
117 *InterruptStatus |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;\r
118 }\r
119 if (Dev->TxLastUsed != TxCurUsed) {\r
120 ASSERT (Dev->TxCurPending > 0);\r
121 *InterruptStatus |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;\r
122 }\r
123 }\r
124\r
125 if (TxBuf != NULL) {\r
126 if (Dev->TxLastUsed == TxCurUsed) {\r
127 *TxBuf = NULL;\r
128 }\r
129 else {\r
130 UINT16 UsedElemIdx;\r
131 UINT32 DescIdx;\r
132\r
133 //\r
134 // fetch the first descriptor among those that the hypervisor reports\r
135 // completed\r
136 //\r
137 ASSERT (Dev->TxCurPending > 0);\r
138 ASSERT (Dev->TxCurPending <= Dev->TxMaxPending);\r
139\r
140 UsedElemIdx = Dev->TxLastUsed++ % Dev->TxRing.QueueSize;\r
141 DescIdx = Dev->TxRing.Used.UsedElem[UsedElemIdx].Id;\r
8258c4e6 142 ASSERT (DescIdx < (UINT32) (2 * Dev->TxMaxPending - 1));\r
b6dfc654
LE
143\r
144 //\r
8fa54a8a
BS
145 // get the device address that has been enqueued for the caller's\r
146 // transmit buffer\r
b6dfc654 147 //\r
8fa54a8a 148 DeviceAddress = Dev->TxRing.Desc[DescIdx + 1].Addr;\r
b6dfc654
LE
149\r
150 //\r
151 // now this descriptor can be used again to enqueue a transmit buffer\r
152 //\r
153 Dev->TxFreeStack[--Dev->TxCurPending] = (UINT16) DescIdx;\r
8fa54a8a
BS
154\r
155 //\r
156 // Unmap the device address and perform the reverse mapping to find the\r
157 // caller buffer address.\r
158 //\r
159 Status = VirtioNetUnmapTxBuf (\r
160 Dev,\r
161 TxBuf,\r
162 DeviceAddress\r
163 );\r
164 if (EFI_ERROR (Status)) {\r
165 //\r
166 // VirtioNetUnmapTxBuf should never fail, if we have reached here\r
167 // that means our internal state has been corrupted\r
168 //\r
169 ASSERT (FALSE);\r
170 Status = EFI_DEVICE_ERROR;\r
171 goto Exit;\r
172 }\r
b6dfc654
LE
173 }\r
174 }\r
175\r
176 Status = EFI_SUCCESS;\r
177\r
178Exit:\r
179 gBS->RestoreTPL (OldTpl);\r
180 return Status;\r
181}\r