]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/VirtioNetDxe/VirtioNet.h
ShellPkg/for: Fix potential null pointer deference
[mirror_edk2.git] / OvmfPkg / VirtioNetDxe / VirtioNet.h
1 /** @file
2
3 Internal definitions for the virtio-net driver, which produces Simple Network
4 Protocol instances for virtio-net devices.
5
6 Copyright (C) 2013, Red Hat, Inc.
7 Copyright (c) 2017, AMD Inc, 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 #ifndef _VIRTIO_NET_DXE_H_
19 #define _VIRTIO_NET_DXE_H_
20
21 #include <IndustryStandard/VirtioNet.h>
22 #include <Library/DebugLib.h>
23 #include <Library/VirtioLib.h>
24 #include <Protocol/ComponentName.h>
25 #include <Protocol/ComponentName2.h>
26 #include <Protocol/DevicePath.h>
27 #include <Protocol/DriverBinding.h>
28 #include <Protocol/SimpleNetwork.h>
29 #include <Library/OrderedCollectionLib.h>
30
31 #define VNET_SIG SIGNATURE_32 ('V', 'N', 'E', 'T')
32
33 //
34 // maximum number of pending packets, separately for each direction
35 //
36 #define VNET_MAX_PENDING 64
37
38 //
39 // State diagram:
40 //
41 // | ^
42 // | |
43 // BindingStart BindingStop
44 // +SnpPopulate |
45 // ++GetFeatures |
46 // | |
47 // v |
48 // +---------+ virtio-net device is reset, no resources are
49 // | stopped | allocated for traffic, but MAC address has
50 // +---------+ been retrieved
51 // | ^
52 // | |
53 // SNP.Start SNP.Stop
54 // | |
55 // v |
56 // +---------+
57 // | started | functionally identical to stopped
58 // +---------+
59 // | ^
60 // | |
61 // SNP.Initialize SNP.Shutdown
62 // | |
63 // v |
64 // +-------------+ Virtio-net setup complete, including DRIVER_OK
65 // | initialized | bit. The receive queue is populated with
66 // +-------------+ requests; McastIpToMac, GetStatus, Transmit,
67 // Receive are callable.
68 //
69
70 typedef struct {
71 //
72 // Parts of this structure are initialized / torn down in various functions
73 // at various call depths. The table to the right should make it easier to
74 // track them.
75 //
76 // field init function
77 // ------------------ ------------------------------
78 UINT32 Signature; // VirtioNetDriverBindingStart
79 VIRTIO_DEVICE_PROTOCOL *VirtIo; // VirtioNetDriverBindingStart
80 EFI_SIMPLE_NETWORK_PROTOCOL Snp; // VirtioNetSnpPopulate
81 EFI_SIMPLE_NETWORK_MODE Snm; // VirtioNetSnpPopulate
82 EFI_EVENT ExitBoot; // VirtioNetSnpPopulate
83 EFI_DEVICE_PATH_PROTOCOL *MacDevicePath; // VirtioNetDriverBindingStart
84 EFI_HANDLE MacHandle; // VirtioNetDriverBindingStart
85
86 VRING RxRing; // VirtioNetInitRing
87 VOID *RxRingMap; // VirtioRingMap and
88 // VirtioNetInitRing
89 UINT8 *RxBuf; // VirtioNetInitRx
90 UINT16 RxLastUsed; // VirtioNetInitRx
91 UINTN RxBufNrPages; // VirtioNetInitRx
92 EFI_PHYSICAL_ADDRESS RxBufDeviceBase; // VirtioNetInitRx
93 VOID *RxBufMap; // VirtioNetInitRx
94
95 VRING TxRing; // VirtioNetInitRing
96 VOID *TxRingMap; // VirtioRingMap and
97 // VirtioNetInitRing
98 UINT16 TxMaxPending; // VirtioNetInitTx
99 UINT16 TxCurPending; // VirtioNetInitTx
100 UINT16 *TxFreeStack; // VirtioNetInitTx
101 VIRTIO_1_0_NET_REQ *TxSharedReq; // VirtioNetInitTx
102 VOID *TxSharedReqMap; // VirtioNetInitTx
103 UINT16 TxLastUsed; // VirtioNetInitTx
104 ORDERED_COLLECTION *TxBufCollection; // VirtioNetInitTx
105 } VNET_DEV;
106
107
108 //
109 // In order to avoid duplication of interface documentation, please find all
110 // leading comments near the respective function / variable definitions (not
111 // the declarations here), which is where your code editor of choice takes you
112 // anyway when jumping to a function.
113 //
114
115 //
116 // utility macros
117 //
118 #define VIRTIO_NET_FROM_SNP(SnpPointer) \
119 CR (SnpPointer, VNET_DEV, Snp, VNET_SIG)
120
121 #define VIRTIO_CFG_WRITE(Dev, Field, Value) ((Dev)->VirtIo->WriteDevice ( \
122 (Dev)->VirtIo, \
123 OFFSET_OF_VNET (Field), \
124 SIZE_OF_VNET (Field), \
125 (Value) \
126 ))
127
128 #define VIRTIO_CFG_READ(Dev, Field, Pointer) ((Dev)->VirtIo->ReadDevice ( \
129 (Dev)->VirtIo, \
130 OFFSET_OF_VNET (Field), \
131 SIZE_OF_VNET (Field), \
132 sizeof *(Pointer), \
133 (Pointer) \
134 ))
135
136 //
137 // component naming
138 //
139 extern EFI_COMPONENT_NAME_PROTOCOL gVirtioNetComponentName;
140 extern EFI_COMPONENT_NAME2_PROTOCOL gVirtioNetComponentName2;
141
142 //
143 // driver binding
144 //
145 extern EFI_DRIVER_BINDING_PROTOCOL gVirtioNetDriverBinding;
146
147 //
148 // member functions implementing the Simple Network Protocol
149 //
150 EFI_STATUS
151 EFIAPI
152 VirtioNetStart (
153 IN EFI_SIMPLE_NETWORK_PROTOCOL *This
154 );
155
156 EFI_STATUS
157 EFIAPI
158 VirtioNetStop (
159 IN EFI_SIMPLE_NETWORK_PROTOCOL *This
160 );
161
162 EFI_STATUS
163 EFIAPI
164 VirtioNetInitialize (
165 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
166 IN UINTN ExtraRxBufferSize OPTIONAL,
167 IN UINTN ExtraTxBufferSize OPTIONAL
168 );
169
170 EFI_STATUS
171 EFIAPI
172 VirtioNetReset (
173 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
174 IN BOOLEAN ExtendedVerification
175 );
176
177 EFI_STATUS
178 EFIAPI
179 VirtioNetShutdown (
180 IN EFI_SIMPLE_NETWORK_PROTOCOL *This
181 );
182
183 EFI_STATUS
184 EFIAPI
185 VirtioNetReceiveFilters (
186 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
187 IN UINT32 Enable,
188 IN UINT32 Disable,
189 IN BOOLEAN ResetMCastFilter,
190 IN UINTN MCastFilterCnt OPTIONAL,
191 IN EFI_MAC_ADDRESS *MCastFilter OPTIONAL
192 );
193
194 EFI_STATUS
195 EFIAPI
196 VirtioNetStationAddress (
197 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
198 IN BOOLEAN Reset,
199 IN EFI_MAC_ADDRESS *New OPTIONAL
200 );
201
202 EFI_STATUS
203 EFIAPI
204 VirtioNetStatistics (
205 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
206 IN BOOLEAN Reset,
207 IN OUT UINTN *StatisticsSize OPTIONAL,
208 OUT EFI_NETWORK_STATISTICS *StatisticsTable OPTIONAL
209 );
210
211 EFI_STATUS
212 EFIAPI
213 VirtioNetMcastIpToMac (
214 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
215 IN BOOLEAN IPv6,
216 IN EFI_IP_ADDRESS *Ip,
217 OUT EFI_MAC_ADDRESS *Mac
218 );
219
220 EFI_STATUS
221 EFIAPI
222 VirtioNetNvData (
223 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
224 IN BOOLEAN ReadWrite,
225 IN UINTN Offset,
226 IN UINTN BufferSize,
227 IN OUT VOID *Buffer
228 );
229
230 EFI_STATUS
231 EFIAPI
232 VirtioNetGetStatus (
233 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
234 OUT UINT32 *InterruptStatus OPTIONAL,
235 OUT VOID **TxBuf OPTIONAL
236 );
237
238 EFI_STATUS
239 EFIAPI
240 VirtioNetTransmit (
241 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
242 IN UINTN HeaderSize,
243 IN UINTN BufferSize,
244 IN /* +OUT! */ VOID *Buffer,
245 IN EFI_MAC_ADDRESS *SrcAddr OPTIONAL,
246 IN EFI_MAC_ADDRESS *DestAddr OPTIONAL,
247 IN UINT16 *Protocol OPTIONAL
248 );
249
250 EFI_STATUS
251 EFIAPI
252 VirtioNetReceive (
253 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
254 OUT UINTN *HeaderSize OPTIONAL,
255 IN OUT UINTN *BufferSize,
256 OUT VOID *Buffer,
257 OUT EFI_MAC_ADDRESS *SrcAddr OPTIONAL,
258 OUT EFI_MAC_ADDRESS *DestAddr OPTIONAL,
259 OUT UINT16 *Protocol OPTIONAL
260 );
261
262 //
263 // utility functions shared by various SNP member functions
264 //
265 VOID
266 EFIAPI
267 VirtioNetShutdownRx (
268 IN OUT VNET_DEV *Dev
269 );
270
271 VOID
272 EFIAPI
273 VirtioNetShutdownTx (
274 IN OUT VNET_DEV *Dev
275 );
276
277 VOID
278 EFIAPI
279 VirtioNetUninitRing (
280 IN OUT VNET_DEV *Dev,
281 IN OUT VRING *Ring,
282 IN VOID *RingMap
283 );
284
285 //
286 // utility functions to map caller-supplied Tx buffer system physical address
287 // to a device address and vice versa
288 //
289 EFI_STATUS
290 EFIAPI
291 VirtioNetMapTxBuf (
292 IN VNET_DEV *Dev,
293 IN VOID *Buffer,
294 IN UINTN NumberOfBytes,
295 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress
296 );
297
298 EFI_STATUS
299 EFIAPI
300 VirtioNetUnmapTxBuf (
301 IN VNET_DEV *Dev,
302 OUT VOID **Buffer,
303 IN EFI_PHYSICAL_ADDRESS DeviceAddress
304 );
305
306 INTN
307 EFIAPI
308 VirtioNetTxBufMapInfoCompare (
309 IN CONST VOID *UserStruct1,
310 IN CONST VOID *UserStruct2
311 );
312
313 INTN
314 EFIAPI
315 VirtioNetTxBufDeviceAddressCompare (
316 IN CONST VOID *StandaloneKey,
317 IN CONST VOID *UserStruct
318 );
319
320
321 //
322 // event callbacks
323 //
324 VOID
325 EFIAPI
326 VirtioNetIsPacketAvailable (
327 IN EFI_EVENT Event,
328 IN VOID *Context
329 );
330
331 VOID
332 EFIAPI
333 VirtioNetExitBoot (
334 IN EFI_EVENT Event,
335 IN VOID *Context
336 );
337
338 #endif // _VIRTIO_NET_DXE_H_