3 Implementation of the SNP.GetStatus() function and its private helpers if
6 Copyright (C) 2013, Red Hat, Inc.
7 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
9 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <Library/BaseLib.h>
14 #include <Library/UefiBootServicesTableLib.h>
16 #include "VirtioNet.h"
19 Reads the current interrupt status and recycled transmit buffer status from
22 @param This The protocol instance pointer.
23 @param InterruptStatus A pointer to the bit mask of the currently active
24 interrupts If this is NULL, the interrupt status will
25 not be read from the device. If this is not NULL, the
26 interrupt status will be read from the device. When
27 the interrupt status is read, it will also be
28 cleared. Clearing the transmit interrupt does not
29 empty the recycled transmit buffer array.
30 @param TxBuf Recycled transmit buffer address. The network
31 interface will not transmit if its internal recycled
32 transmit buffer array is full. Reading the transmit
33 buffer does not clear the transmit interrupt. If this
34 is NULL, then the transmit buffer status will not be
35 read. If there are no transmit buffers to recycle and
36 TxBuf is not NULL, * TxBuf will be set to NULL.
38 @retval EFI_SUCCESS The status of the network interface was
40 @retval EFI_NOT_STARTED The network interface has not been started.
41 @retval EFI_INVALID_PARAMETER One or more of the parameters has an
43 @retval EFI_DEVICE_ERROR The command could not be sent to the network
45 @retval EFI_UNSUPPORTED This function is not supported by the network
53 IN EFI_SIMPLE_NETWORK_PROTOCOL
*This
,
54 OUT UINT32
*InterruptStatus OPTIONAL
,
55 OUT VOID
**TxBuf OPTIONAL
63 EFI_PHYSICAL_ADDRESS DeviceAddress
;
66 return EFI_INVALID_PARAMETER
;
69 Dev
= VIRTIO_NET_FROM_SNP (This
);
70 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
71 switch (Dev
->Snm
.State
) {
72 case EfiSimpleNetworkStopped
:
73 Status
= EFI_NOT_STARTED
;
75 case EfiSimpleNetworkStarted
:
76 Status
= EFI_DEVICE_ERROR
;
85 if (Dev
->Snm
.MediaPresentSupported
) {
88 Status
= VIRTIO_CFG_READ (Dev
, LinkStatus
, &LinkStatus
);
89 if (EFI_ERROR (Status
)) {
92 Dev
->Snm
.MediaPresent
=
93 (BOOLEAN
) ((LinkStatus
& VIRTIO_NET_S_LINK_UP
) != 0);
97 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
100 RxCurUsed
= *Dev
->RxRing
.Used
.Idx
;
101 TxCurUsed
= *Dev
->TxRing
.Used
.Idx
;
104 if (InterruptStatus
!= NULL
) {
106 // report the receive interrupt if there is data available for reception,
107 // report the transmit interrupt if we have transmitted at least one buffer
109 *InterruptStatus
= 0;
110 if (Dev
->RxLastUsed
!= RxCurUsed
) {
111 *InterruptStatus
|= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT
;
113 if (Dev
->TxLastUsed
!= TxCurUsed
) {
114 ASSERT (Dev
->TxCurPending
> 0);
115 *InterruptStatus
|= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT
;
120 if (Dev
->TxLastUsed
== TxCurUsed
) {
128 // fetch the first descriptor among those that the hypervisor reports
131 ASSERT (Dev
->TxCurPending
> 0);
132 ASSERT (Dev
->TxCurPending
<= Dev
->TxMaxPending
);
134 UsedElemIdx
= Dev
->TxLastUsed
++ % Dev
->TxRing
.QueueSize
;
135 DescIdx
= Dev
->TxRing
.Used
.UsedElem
[UsedElemIdx
].Id
;
136 ASSERT (DescIdx
< (UINT32
) (2 * Dev
->TxMaxPending
- 1));
139 // get the device address that has been enqueued for the caller's
142 DeviceAddress
= Dev
->TxRing
.Desc
[DescIdx
+ 1].Addr
;
145 // now this descriptor can be used again to enqueue a transmit buffer
147 Dev
->TxFreeStack
[--Dev
->TxCurPending
] = (UINT16
) DescIdx
;
150 // Unmap the device address and perform the reverse mapping to find the
151 // caller buffer address.
153 Status
= VirtioNetUnmapTxBuf (
158 if (EFI_ERROR (Status
)) {
160 // VirtioNetUnmapTxBuf should never fail, if we have reached here
161 // that means our internal state has been corrupted
164 Status
= EFI_DEVICE_ERROR
;
170 Status
= EFI_SUCCESS
;
173 gBS
->RestoreTPL (OldTpl
);