TianoCore EDK2 master
Loading...
Searching...
No Matches
SnpSharedHelpers.c
Go to the documentation of this file.
1
12
13#include "VirtioNet.h"
14
15//
16// The user structure for the ordered collection that will track the mapping
17// info of the packets queued in TxRing
18//
19typedef struct {
20 VOID *Buffer;
21 EFI_PHYSICAL_ADDRESS DeviceAddress; // lookup key for reverse mapping
22 VOID *BufMap;
24
39VOID
40EFIAPI
42 IN OUT VNET_DEV *Dev
43 )
44{
45 Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Dev->RxBufMap);
46 Dev->VirtIo->FreeSharedPages (
47 Dev->VirtIo,
48 Dev->RxBufNrPages,
49 Dev->RxBuf
50 );
51}
52
53VOID
54EFIAPI
55VirtioNetShutdownTx (
56 IN OUT VNET_DEV *Dev
57 )
58{
59 ORDERED_COLLECTION_ENTRY *Entry, *Entry2;
60 TX_BUF_MAP_INFO *TxBufMapInfo;
61 VOID *UserStruct;
62
63 Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Dev->TxSharedReqMap);
64 Dev->VirtIo->FreeSharedPages (
65 Dev->VirtIo,
66 EFI_SIZE_TO_PAGES (sizeof *(Dev->TxSharedReq)),
67 Dev->TxSharedReq
68 );
69
70 for (Entry = OrderedCollectionMin (Dev->TxBufCollection);
71 Entry != NULL;
72 Entry = Entry2)
73 {
74 Entry2 = OrderedCollectionNext (Entry);
75 OrderedCollectionDelete (Dev->TxBufCollection, Entry, &UserStruct);
76 TxBufMapInfo = UserStruct;
77 Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, TxBufMapInfo->BufMap);
78 FreePool (TxBufMapInfo);
79 }
80
81 OrderedCollectionUninit (Dev->TxBufCollection);
82
83 FreePool (Dev->TxFreeStack);
84}
85
94VOID
95EFIAPI
97 IN OUT VNET_DEV *Dev,
98 IN OUT VRING *Ring,
99 IN VOID *RingMap
100 )
101{
102 Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, RingMap);
103 VirtioRingUninit (Dev->VirtIo, Ring);
104}
105
123EFIAPI
125 IN VNET_DEV *Dev,
126 IN VOID *Buffer,
127 IN UINTN NumberOfBytes,
128 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress
129 )
130{
131 EFI_STATUS Status;
132 TX_BUF_MAP_INFO *TxBufMapInfo;
133 EFI_PHYSICAL_ADDRESS Address;
134 VOID *Mapping;
135
136 TxBufMapInfo = AllocatePool (sizeof (*TxBufMapInfo));
137 if (TxBufMapInfo == NULL) {
138 return EFI_OUT_OF_RESOURCES;
139 }
140
142 Dev->VirtIo,
143 VirtioOperationBusMasterRead,
144 Buffer,
145 NumberOfBytes,
146 &Address,
147 &Mapping
148 );
149 if (EFI_ERROR (Status)) {
150 goto FreeTxBufMapInfo;
151 }
152
153 TxBufMapInfo->Buffer = Buffer;
154 TxBufMapInfo->DeviceAddress = Address;
155 TxBufMapInfo->BufMap = Mapping;
156
157 Status = OrderedCollectionInsert (
158 Dev->TxBufCollection,
159 NULL,
160 TxBufMapInfo
161 );
162 switch (Status) {
163 case EFI_OUT_OF_RESOURCES:
164 goto UnmapTxBuf;
165 case EFI_ALREADY_STARTED:
166 //
167 // This should never happen: it implies
168 //
169 // - an identity-mapping VIRTIO_DEVICE_PROTOCOL.MapSharedBuffer()
170 // implementation -- which is fine,
171 //
172 // - and an SNP client that queues multiple instances of the exact same
173 // buffer address with SNP.Transmit() -- which is undefined behavior,
174 // based on the TxBuf language in UEFI-2.7,
175 // EFI_SIMPLE_NETWORK.GetStatus().
176 //
177 ASSERT (FALSE);
178 Status = EFI_INVALID_PARAMETER;
179 goto UnmapTxBuf;
180 default:
181 ASSERT_EFI_ERROR (Status);
182 break;
183 }
184
185 *DeviceAddress = Address;
186 return EFI_SUCCESS;
187
188UnmapTxBuf:
189 Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Mapping);
190
191FreeTxBufMapInfo:
192 FreePool (TxBufMapInfo);
193 return Status;
194}
195
212EFIAPI
214 IN VNET_DEV *Dev,
215 OUT VOID **Buffer,
216 IN EFI_PHYSICAL_ADDRESS DeviceAddress
217 )
218{
220 TX_BUF_MAP_INFO *TxBufMapInfo;
221 VOID *UserStruct;
222
223 Entry = OrderedCollectionFind (Dev->TxBufCollection, &DeviceAddress);
224 if (Entry == NULL) {
225 return EFI_INVALID_PARAMETER;
226 }
227
228 OrderedCollectionDelete (Dev->TxBufCollection, Entry, &UserStruct);
229
230 TxBufMapInfo = UserStruct;
231
232 *Buffer = TxBufMapInfo->Buffer;
233 Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, TxBufMapInfo->BufMap);
234 FreePool (TxBufMapInfo);
235
236 return EFI_SUCCESS;
237}
238
252INTN
253EFIAPI
255 IN CONST VOID *UserStruct1,
256 IN CONST VOID *UserStruct2
257 )
258{
259 CONST TX_BUF_MAP_INFO *MapInfo1;
260 CONST TX_BUF_MAP_INFO *MapInfo2;
261
262 MapInfo1 = UserStruct1;
263 MapInfo2 = UserStruct2;
264
265 return MapInfo1->DeviceAddress < MapInfo2->DeviceAddress ? -1 :
266 MapInfo1->DeviceAddress > MapInfo2->DeviceAddress ? 1 :
267 0;
268}
269
286INTN
287EFIAPI
289 IN CONST VOID *StandaloneKey,
290 IN CONST VOID *UserStruct
291 )
292{
293 CONST EFI_PHYSICAL_ADDRESS *DeviceAddress;
294 CONST TX_BUF_MAP_INFO *MapInfo;
295
296 DeviceAddress = StandaloneKey;
297 MapInfo = UserStruct;
298
299 return *DeviceAddress < MapInfo->DeviceAddress ? -1 :
300 *DeviceAddress > MapInfo->DeviceAddress ? 1 :
301 0;
302}
UINT64 UINTN
INT64 INTN
VOID EFIAPI FreePool(IN VOID *Buffer)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
ORDERED_COLLECTION_ENTRY *EFIAPI OrderedCollectionFind(IN CONST ORDERED_COLLECTION *Collection, IN CONST VOID *StandaloneKey)
ORDERED_COLLECTION_ENTRY *EFIAPI OrderedCollectionMin(IN CONST ORDERED_COLLECTION *Collection)
RETURN_STATUS EFIAPI OrderedCollectionInsert(IN OUT ORDERED_COLLECTION *Collection, OUT ORDERED_COLLECTION_ENTRY **Entry OPTIONAL, IN VOID *UserStruct)
VOID EFIAPI OrderedCollectionUninit(IN ORDERED_COLLECTION *Collection)
ORDERED_COLLECTION_ENTRY *EFIAPI OrderedCollectionNext(IN CONST ORDERED_COLLECTION_ENTRY *Entry)
VOID EFIAPI OrderedCollectionDelete(IN OUT ORDERED_COLLECTION *Collection, IN ORDERED_COLLECTION_ENTRY *Entry, OUT VOID **UserStruct OPTIONAL)
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
EFI_STATUS EFIAPI VirtioNetMapTxBuf(IN VNET_DEV *Dev, IN VOID *Buffer, IN UINTN NumberOfBytes, OUT EFI_PHYSICAL_ADDRESS *DeviceAddress)
INTN EFIAPI VirtioNetTxBufMapInfoCompare(IN CONST VOID *UserStruct1, IN CONST VOID *UserStruct2)
VOID EFIAPI VirtioNetShutdownRx(IN OUT VNET_DEV *Dev)
EFI_STATUS EFIAPI VirtioNetUnmapTxBuf(IN VNET_DEV *Dev, OUT VOID **Buffer, IN EFI_PHYSICAL_ADDRESS DeviceAddress)
INTN EFIAPI VirtioNetTxBufDeviceAddressCompare(IN CONST VOID *StandaloneKey, IN CONST VOID *UserStruct)
VOID EFIAPI VirtioNetUninitRing(IN OUT VNET_DEV *Dev, IN OUT VRING *Ring, IN VOID *RingMap)
UINT64 EFI_PHYSICAL_ADDRESS
Definition: UefiBaseType.h:50
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SIZE_TO_PAGES(Size)
Definition: UefiBaseType.h:200
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_STATUS EFIAPI VirtioMapAllBytesInSharedBuffer(IN VIRTIO_DEVICE_PROTOCOL *VirtIo, IN VIRTIO_MAP_OPERATION Operation, IN VOID *HostAddress, IN UINTN NumberOfBytes, OUT EFI_PHYSICAL_ADDRESS *DeviceAddress, OUT VOID **Mapping)
Definition: VirtioLib.c:469
VOID EFIAPI VirtioRingUninit(IN VIRTIO_DEVICE_PROTOCOL *VirtIo, IN OUT VRING *Ring)
Definition: VirtioLib.c:144