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