]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/VirtioNetDxe/SnpInitialize.c
OvmfPkg: VirtioNetDxe: complete driver with INF file
[mirror_edk2.git] / OvmfPkg / VirtioNetDxe / SnpInitialize.c
1 /** @file
2
3 Implementation of the SNP.Initialize() function and its private helpers if
4 any.
5
6 Copyright (C) 2013, Red Hat, Inc.
7 Copyright (c) 2006 - 2010, 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/MemoryAllocationLib.h>
21 #include <Library/UefiBootServicesTableLib.h>
22
23 #include "VirtioNet.h"
24
25 /**
26 Initialize a virtio ring for a specific transfer direction of the virtio-net
27 device.
28
29 This function may only be called by VirtioNetInitialize().
30
31 @param[in,out] Dev The VNET_DEV driver instance about to enter the
32 EfiSimpleNetworkInitialized state.
33 @param[in] Selector Identifies the transfer direction (virtio queue) of
34 the network device.
35 @param[out] Ring The virtio-ring inside the VNET_DEV structure,
36 corresponding to Selector.
37
38 @retval EFI_UNSUPPORTED The queue size reported by the virtio-net device is
39 too small.
40 @return Status codes from VIRTIO_CFG_WRITE(),
41 VIRTIO_CFG_READ() and VirtioRingInit().
42 @retval EFI_SUCCESS Ring initialized.
43 */
44
45 STATIC
46 EFI_STATUS
47 EFIAPI
48 VirtioNetInitRing (
49 IN OUT VNET_DEV *Dev,
50 IN UINT16 Selector,
51 OUT VRING *Ring
52 )
53 {
54 EFI_STATUS Status;
55 UINT16 QueueSize;
56
57 //
58 // step 4b -- allocate selected queue
59 //
60 Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrQueueSelect, Selector);
61 if (EFI_ERROR (Status)) {
62 return Status;
63 }
64 Status = VIRTIO_CFG_READ (Dev, Generic.VhdrQueueSize, &QueueSize);
65 if (EFI_ERROR (Status)) {
66 return Status;
67 }
68 //
69 // For each packet (RX and TX alike), we need two descriptors:
70 // one for the virtio-net request header, and another one for the data
71 //
72 if (QueueSize < 2) {
73 return EFI_UNSUPPORTED;
74 }
75 Status = VirtioRingInit (QueueSize, Ring);
76 if (EFI_ERROR (Status)) {
77 return Status;
78 }
79
80 //
81 // step 4c -- report GPFN (guest-physical frame number) of queue
82 //
83 Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrQueueAddress,
84 (UINTN) Ring->Base >> EFI_PAGE_SHIFT);
85 if (EFI_ERROR (Status)) {
86 VirtioRingUninit (Ring);
87 }
88 return Status;
89 }
90
91
92 /**
93 Set up static scaffolding for the VirtioNetTransmit() and
94 VirtioNetGetStatus() SNP methods.
95
96 This function may only be called by VirtioNetInitialize().
97
98 The structures laid out and resources configured include:
99 - fully populate the TX queue with a static pattern of virtio descriptor
100 chains,
101 - tracking of heads of free descriptor chains from the above,
102 - one common virtio-net request header (never modified by the host) for all
103 pending TX packets,
104 - select polling over TX interrupt.
105
106 @param[in,out] Dev The VNET_DEV driver instance about to enter the
107 EfiSimpleNetworkInitialized state.
108
109 @retval EFI_OUT_OF_RESOURCES Failed to allocate the stack to track the heads
110 of free descriptor chains.
111 @retval EFI_SUCCESS TX setup successful.
112 */
113
114 STATIC
115 EFI_STATUS
116 EFIAPI
117 VirtioNetInitTx (
118 IN OUT VNET_DEV *Dev
119 )
120 {
121 UINTN PktIdx;
122
123 Dev->TxMaxPending = MIN (Dev->TxRing.QueueSize / 2, VNET_MAX_PENDING);
124 Dev->TxCurPending = 0;
125 Dev->TxFreeStack = AllocatePool (Dev->TxMaxPending *
126 sizeof *Dev->TxFreeStack);
127 if (Dev->TxFreeStack == NULL) {
128 return EFI_OUT_OF_RESOURCES;
129 }
130
131 for (PktIdx = 0; PktIdx < Dev->TxMaxPending; ++PktIdx) {
132 UINT16 DescIdx;
133
134 DescIdx = (UINT16) (2 * PktIdx);
135 Dev->TxFreeStack[PktIdx] = DescIdx;
136
137 //
138 // For each possibly pending packet, lay out the descriptor for the common
139 // (unmodified by the host) virtio-net request header.
140 //
141 Dev->TxRing.Desc[DescIdx].Addr = (UINTN) &Dev->TxSharedReq;
142 Dev->TxRing.Desc[DescIdx].Len = sizeof Dev->TxSharedReq;
143 Dev->TxRing.Desc[DescIdx].Flags = VRING_DESC_F_NEXT;
144 Dev->TxRing.Desc[DescIdx].Next = DescIdx + 1;
145
146 //
147 // The second descriptor of each pending TX packet is updated on the fly,
148 // but it always terminates the descriptor chain of the packet.
149 //
150 Dev->TxRing.Desc[DescIdx + 1].Flags = 0;
151 }
152
153 //
154 // virtio-0.9.5, Appendix C, Packet Transmission
155 //
156 Dev->TxSharedReq.Flags = 0;
157 Dev->TxSharedReq.GsoType = VIRTIO_NET_HDR_GSO_NONE;
158
159 //
160 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
161 //
162 MemoryFence ();
163 Dev->TxLastUsed = *Dev->TxRing.Used.Idx;
164 ASSERT (Dev->TxLastUsed == 0);
165
166 //
167 // want no interrupt when a transmit completes
168 //
169 *Dev->TxRing.Avail.Flags = (UINT16) VRING_AVAIL_F_NO_INTERRUPT;
170
171 return EFI_SUCCESS;
172 }
173
174
175 /**
176 Set up static scaffolding for the VirtioNetReceive() SNP method and enable
177 live device operation.
178
179 This function may only be called as VirtioNetInitialize()'s final step.
180
181 The structures laid out and resources configured include:
182 - destination area for the host to write virtio-net request headers and
183 packet data into,
184 - select polling over RX interrupt,
185 - fully populate the RX queue with a static pattern of virtio descriptor
186 chains.
187
188 @param[in,out] Dev The VNET_DEV driver instance about to enter the
189 EfiSimpleNetworkInitialized state.
190
191 @retval EFI_OUT_OF_RESOURCES Failed to allocate RX destination area.
192 @return Status codes from VIRTIO_CFG_WRITE().
193 @retval EFI_SUCCESS RX setup successful. The device is live and may
194 already be writing to the receive area.
195 */
196
197 STATIC
198 EFI_STATUS
199 EFIAPI
200 VirtioNetInitRx (
201 IN OUT VNET_DEV *Dev
202 )
203 {
204 EFI_STATUS Status;
205 UINTN RxBufSize;
206 UINT16 RxAlwaysPending;
207 UINTN PktIdx;
208 UINT16 DescIdx;
209 UINT8 *RxPtr;
210
211 //
212 // For each incoming packet we must supply two descriptors:
213 // - the recipient for the virtio-net request header, plus
214 // - the recipient for the network data (which consists of Ethernet header
215 // and Ethernet payload).
216 //
217 RxBufSize = sizeof (VIRTIO_NET_REQ) +
218 (Dev->Snm.MediaHeaderSize + Dev->Snm.MaxPacketSize);
219
220 //
221 // Limit the number of pending RX packets if the queue is big. The division
222 // by two is due to the above "two descriptors per packet" trait.
223 //
224 RxAlwaysPending = MIN (Dev->RxRing.QueueSize / 2, VNET_MAX_PENDING);
225
226 Dev->RxBuf = AllocatePool (RxAlwaysPending * RxBufSize);
227 if (Dev->RxBuf == NULL) {
228 return EFI_OUT_OF_RESOURCES;
229 }
230
231 //
232 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
233 //
234 MemoryFence ();
235 Dev->RxLastUsed = *Dev->RxRing.Used.Idx;
236 ASSERT (Dev->RxLastUsed == 0);
237
238 //
239 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device:
240 // the host should not send interrupts, we'll poll in VirtioNetReceive()
241 // and VirtioNetIsPacketAvailable().
242 //
243 *Dev->RxRing.Avail.Flags = (UINT16) VRING_AVAIL_F_NO_INTERRUPT;
244
245 //
246 // now set up a separate, two-part descriptor chain for each RX packet, and
247 // link each chain into (from) the available ring as well
248 //
249 DescIdx = 0;
250 RxPtr = Dev->RxBuf;
251 for (PktIdx = 0; PktIdx < RxAlwaysPending; ++PktIdx) {
252 //
253 // virtio-0.9.5, 2.4.1.2 Updating the Available Ring
254 // invisible to the host until we update the Index Field
255 //
256 Dev->RxRing.Avail.Ring[PktIdx] = DescIdx;
257
258 //
259 // virtio-0.9.5, 2.4.1.1 Placing Buffers into the Descriptor Table
260 //
261 Dev->RxRing.Desc[DescIdx].Addr = (UINTN) RxPtr;
262 Dev->RxRing.Desc[DescIdx].Len = sizeof (VIRTIO_NET_REQ);
263 Dev->RxRing.Desc[DescIdx].Flags = VRING_DESC_F_WRITE | VRING_DESC_F_NEXT;
264 Dev->RxRing.Desc[DescIdx].Next = DescIdx + 1;
265 RxPtr += Dev->RxRing.Desc[DescIdx++].Len;
266
267 Dev->RxRing.Desc[DescIdx].Addr = (UINTN) RxPtr;
268 Dev->RxRing.Desc[DescIdx].Len = RxBufSize - sizeof (VIRTIO_NET_REQ);
269 Dev->RxRing.Desc[DescIdx].Flags = VRING_DESC_F_WRITE;
270 RxPtr += Dev->RxRing.Desc[DescIdx++].Len;
271 }
272
273 //
274 // virtio-0.9.5, 2.4.1.3 Updating the Index Field
275 //
276 MemoryFence ();
277 *Dev->RxRing.Avail.Idx = RxAlwaysPending;
278
279 //
280 // At this point reception may already be running. In order to make it sure,
281 // kick the hypervisor. If we fail to kick it, we must first abort reception
282 // before tearing down anything, because reception may have been already
283 // running even without the kick.
284 //
285 // virtio-0.9.5, 2.4.1.4 Notifying the Device
286 //
287 MemoryFence ();
288 Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrQueueNotify, VIRTIO_NET_Q_RX);
289
290 if (EFI_ERROR (Status)) {
291 VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, 0);
292 FreePool (Dev->RxBuf);
293 }
294
295 return Status;
296 }
297
298
299 /**
300 Resets a network adapter and allocates the transmit and receive buffers
301 required by the network interface; optionally, also requests allocation of
302 additional transmit and receive buffers.
303
304 @param This The protocol instance pointer.
305 @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer
306 space that the driver should allocate for the
307 network interface. Some network interfaces will not
308 be able to use the extra buffer, and the caller
309 will not know if it is actually being used.
310 @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer
311 space that the driver should allocate for the
312 network interface. Some network interfaces will not
313 be able to use the extra buffer, and the caller
314 will not know if it is actually being used.
315
316 @retval EFI_SUCCESS The network interface was initialized.
317 @retval EFI_NOT_STARTED The network interface has not been started.
318 @retval EFI_OUT_OF_RESOURCES There was not enough memory for the transmit
319 and receive buffers.
320 @retval EFI_INVALID_PARAMETER One or more of the parameters has an
321 unsupported value.
322 @retval EFI_DEVICE_ERROR The command could not be sent to the network
323 interface.
324 @retval EFI_UNSUPPORTED This function is not supported by the network
325 interface.
326
327 **/
328
329 EFI_STATUS
330 EFIAPI
331 VirtioNetInitialize (
332 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
333 IN UINTN ExtraRxBufferSize OPTIONAL,
334 IN UINTN ExtraTxBufferSize OPTIONAL
335 )
336 {
337 VNET_DEV *Dev;
338 EFI_TPL OldTpl;
339 EFI_STATUS Status;
340 UINT8 NextDevStat;
341 UINT32 Features;
342
343 if (This == NULL) {
344 return EFI_INVALID_PARAMETER;
345 }
346 if (ExtraRxBufferSize > 0 || ExtraTxBufferSize > 0) {
347 return EFI_UNSUPPORTED;
348 }
349
350 Dev = VIRTIO_NET_FROM_SNP (This);
351 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
352 if (Dev->Snm.State != EfiSimpleNetworkStarted) {
353 Status = EFI_NOT_STARTED;
354 goto InitFailed;
355 }
356
357 //
358 // In the EfiSimpleNetworkStarted state the virtio-net device has status
359 // value 0 (= reset) -- see the state diagram, the full call chain to
360 // the end of VirtioNetGetFeatures() (considering we're here now),
361 // the DeviceFailed label below, and VirtioNetShutdown().
362 //
363 // Accordingly, the below is a subsequence of the steps found in the
364 // virtio-0.9.5 spec, 2.2.1 Device Initialization Sequence.
365 //
366 NextDevStat = VSTAT_ACK; // step 2 -- acknowledge device presence
367 Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, NextDevStat);
368 if (EFI_ERROR (Status)) {
369 goto InitFailed;
370 }
371
372 NextDevStat |= VSTAT_DRIVER; // step 3 -- we know how to drive it
373 Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, NextDevStat);
374 if (EFI_ERROR (Status)) {
375 goto DeviceFailed;
376 }
377
378 //
379 // step 4a -- retrieve features. Note that we're past validating required
380 // features in VirtioNetGetFeatures().
381 //
382 Status = VIRTIO_CFG_READ (Dev, Generic.VhdrDeviceFeatureBits, &Features);
383 if (EFI_ERROR (Status)) {
384 goto DeviceFailed;
385 }
386 ASSERT (Features & VIRTIO_NET_F_MAC);
387 ASSERT (Dev->Snm.MediaPresentSupported ==
388 !!(Features & VIRTIO_NET_F_STATUS));
389
390 //
391 // step 4b, 4c -- allocate and report virtqueues
392 //
393 Status = VirtioNetInitRing (Dev, VIRTIO_NET_Q_RX, &Dev->RxRing);
394 if (EFI_ERROR (Status)) {
395 goto DeviceFailed;
396 }
397
398 Status = VirtioNetInitRing (Dev, VIRTIO_NET_Q_TX, &Dev->TxRing);
399 if (EFI_ERROR (Status)) {
400 goto ReleaseRxRing;
401 }
402
403 //
404 // step 5 -- keep only the features we want
405 //
406 Features &= VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS;
407 Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrGuestFeatureBits, Features);
408 if (EFI_ERROR (Status)) {
409 goto ReleaseTxRing;
410 }
411
412 //
413 // step 6 -- virtio-net initialization complete
414 //
415 NextDevStat |= VSTAT_DRIVER_OK;
416 Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, NextDevStat);
417 if (EFI_ERROR (Status)) {
418 goto ReleaseTxRing;
419 }
420
421 Status = VirtioNetInitTx (Dev);
422 if (EFI_ERROR (Status)) {
423 goto AbortDevice;
424 }
425
426 //
427 // start receiving
428 //
429 Status = VirtioNetInitRx (Dev);
430 if (EFI_ERROR (Status)) {
431 goto ReleaseTxAux;
432 }
433
434 Dev->Snm.State = EfiSimpleNetworkInitialized;
435 gBS->RestoreTPL (OldTpl);
436 return EFI_SUCCESS;
437
438 ReleaseTxAux:
439 VirtioNetShutdownTx (Dev);
440
441 AbortDevice:
442 VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, 0);
443
444 ReleaseTxRing:
445 VirtioRingUninit (&Dev->TxRing);
446
447 ReleaseRxRing:
448 VirtioRingUninit (&Dev->RxRing);
449
450 DeviceFailed:
451 //
452 // restore device status invariant for the EfiSimpleNetworkStarted state
453 //
454 VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, 0);
455
456 InitFailed:
457 gBS->RestoreTPL (OldTpl);
458 return Status;
459 }