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