3 Implementation of the SNP.Initialize() function and its private helpers if
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>
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
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.
20 #include <Library/BaseLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
25 #include "VirtioNet.h"
28 Initialize a virtio ring for a specific transfer direction of the virtio-net
31 This function may only be called by VirtioNetInitialize().
33 @param[in,out] Dev The VNET_DEV driver instance about to enter the
34 EfiSimpleNetworkInitialized state.
35 @param[in] Selector Identifies the transfer direction (virtio queue) of
37 @param[out] Ring The virtio-ring inside the VNET_DEV structure,
38 corresponding to Selector.
39 @param[out] Mapping A resulting token to pass to VirtioNetUninitRing()
41 @retval EFI_UNSUPPORTED The queue size reported by the virtio-net device is
43 @return Status codes from VIRTIO_CFG_WRITE(),
44 VIRTIO_CFG_READ(), VirtioRingInit() and
46 @retval EFI_SUCCESS Ring initialized.
65 // step 4b -- allocate selected queue
67 Status
= Dev
->VirtIo
->SetQueueSel (Dev
->VirtIo
, Selector
);
68 if (EFI_ERROR (Status
)) {
71 Status
= Dev
->VirtIo
->GetQueueNumMax (Dev
->VirtIo
, &QueueSize
);
72 if (EFI_ERROR (Status
)) {
77 // For each packet (RX and TX alike), we need two descriptors:
78 // one for the virtio-net request header, and another one for the data
81 return EFI_UNSUPPORTED
;
83 Status
= VirtioRingInit (Dev
->VirtIo
, QueueSize
, Ring
);
84 if (EFI_ERROR (Status
)) {
89 // If anything fails from here on, we must release the ring resources.
91 Status
= VirtioRingMap (Dev
->VirtIo
, Ring
, &RingBaseShift
, &MapInfo
);
92 if (EFI_ERROR (Status
)) {
97 // Additional steps for MMIO: align the queue appropriately, and set the
98 // size. If anything fails from here on, we must unmap the ring resources.
100 Status
= Dev
->VirtIo
->SetQueueNum (Dev
->VirtIo
, QueueSize
);
101 if (EFI_ERROR (Status
)) {
105 Status
= Dev
->VirtIo
->SetQueueAlign (Dev
->VirtIo
, EFI_PAGE_SIZE
);
106 if (EFI_ERROR (Status
)) {
111 // step 4c -- report GPFN (guest-physical frame number) of queue
113 Status
= Dev
->VirtIo
->SetQueueAddress (Dev
->VirtIo
, Ring
, RingBaseShift
);
114 if (EFI_ERROR (Status
)) {
123 Dev
->VirtIo
->UnmapSharedBuffer (Dev
->VirtIo
, MapInfo
);
126 VirtioRingUninit (Dev
->VirtIo
, Ring
);
133 Set up static scaffolding for the VirtioNetTransmit() and
134 VirtioNetGetStatus() SNP methods.
136 This function may only be called by VirtioNetInitialize().
138 The structures laid out and resources configured include:
139 - fully populate the TX queue with a static pattern of virtio descriptor
141 - tracking of heads of free descriptor chains from the above,
142 - one common virtio-net request header (never modified by the host) for all
144 - select polling over TX interrupt.
146 @param[in,out] Dev The VNET_DEV driver instance about to enter the
147 EfiSimpleNetworkInitialized state.
149 @retval EFI_OUT_OF_RESOURCES Failed to allocate the stack to track the heads
150 of free descriptor chains.
151 @return Status codes from VIRTIO_DEVICE_PROTOCOL.
152 AllocateSharedPages() or
153 VirtioMapAllBytesInSharedBuffer()
154 @retval EFI_SUCCESS TX setup successful.
164 UINTN TxSharedReqSize
;
167 EFI_PHYSICAL_ADDRESS DeviceAddress
;
168 VOID
*TxSharedReqBuffer
;
170 Dev
->TxMaxPending
= (UINT16
) MIN (Dev
->TxRing
.QueueSize
/ 2,
172 Dev
->TxCurPending
= 0;
173 Dev
->TxFreeStack
= AllocatePool (Dev
->TxMaxPending
*
174 sizeof *Dev
->TxFreeStack
);
175 if (Dev
->TxFreeStack
== NULL
) {
176 return EFI_OUT_OF_RESOURCES
;
180 // Allocate TxSharedReq header and map with BusMasterCommonBuffer so that it
181 // can be accessed equally by both processor and device.
183 Status
= Dev
->VirtIo
->AllocateSharedPages (
185 EFI_SIZE_TO_PAGES (sizeof *Dev
->TxSharedReq
),
188 if (EFI_ERROR (Status
)) {
189 goto FreeTxFreeStack
;
192 ZeroMem (TxSharedReqBuffer
, sizeof *Dev
->TxSharedReq
);
194 Status
= VirtioMapAllBytesInSharedBuffer (
196 VirtioOperationBusMasterCommonBuffer
,
198 sizeof *(Dev
->TxSharedReq
),
202 if (EFI_ERROR (Status
)) {
203 goto FreeTxSharedReqBuffer
;
206 Dev
->TxSharedReq
= TxSharedReqBuffer
;
210 // In VirtIo 1.0, the NumBuffers field is mandatory. In 0.9.5, it depends on
211 // VIRTIO_NET_F_MRG_RXBUF, which we never negotiate.
213 TxSharedReqSize
= (Dev
->VirtIo
->Revision
< VIRTIO_SPEC_REVISION (1, 0, 0)) ?
214 sizeof (Dev
->TxSharedReq
->V0_9_5
) :
215 sizeof *Dev
->TxSharedReq
;
217 for (PktIdx
= 0; PktIdx
< Dev
->TxMaxPending
; ++PktIdx
) {
220 DescIdx
= (UINT16
) (2 * PktIdx
);
221 Dev
->TxFreeStack
[PktIdx
] = DescIdx
;
224 // For each possibly pending packet, lay out the descriptor for the common
225 // (unmodified by the host) virtio-net request header.
227 Dev
->TxRing
.Desc
[DescIdx
].Addr
= DeviceAddress
;
228 Dev
->TxRing
.Desc
[DescIdx
].Len
= (UINT32
) TxSharedReqSize
;
229 Dev
->TxRing
.Desc
[DescIdx
].Flags
= VRING_DESC_F_NEXT
;
230 Dev
->TxRing
.Desc
[DescIdx
].Next
= (UINT16
) (DescIdx
+ 1);
233 // The second descriptor of each pending TX packet is updated on the fly,
234 // but it always terminates the descriptor chain of the packet.
236 Dev
->TxRing
.Desc
[DescIdx
+ 1].Flags
= 0;
240 // virtio-0.9.5, Appendix C, Packet Transmission
242 Dev
->TxSharedReq
->V0_9_5
.Flags
= 0;
243 Dev
->TxSharedReq
->V0_9_5
.GsoType
= VIRTIO_NET_HDR_GSO_NONE
;
246 // For VirtIo 1.0 only -- the field exists, but it is unused
248 Dev
->TxSharedReq
->NumBuffers
= 0;
251 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
254 Dev
->TxLastUsed
= *Dev
->TxRing
.Used
.Idx
;
255 ASSERT (Dev
->TxLastUsed
== 0);
258 // want no interrupt when a transmit completes
260 *Dev
->TxRing
.Avail
.Flags
= (UINT16
) VRING_AVAIL_F_NO_INTERRUPT
;
264 FreeTxSharedReqBuffer
:
265 Dev
->VirtIo
->FreeSharedPages (
267 EFI_SIZE_TO_PAGES (sizeof *(Dev
->TxSharedReq
)),
271 FreePool (Dev
->TxFreeStack
);
278 Set up static scaffolding for the VirtioNetReceive() SNP method and enable
279 live device operation.
281 This function may only be called as VirtioNetInitialize()'s final step.
283 The structures laid out and resources configured include:
284 - destination area for the host to write virtio-net request headers and
286 - select polling over RX interrupt,
287 - fully populate the RX queue with a static pattern of virtio descriptor
290 @param[in,out] Dev The VNET_DEV driver instance about to enter the
291 EfiSimpleNetworkInitialized state.
293 @return Status codes from VIRTIO_CFG_WRITE() or
294 VIRTIO_DEVICE_PROTOCOL.AllocateSharedPages or
295 VirtioMapAllBytesInSharedBuffer().
296 @retval EFI_SUCCESS RX setup successful. The device is live and may
297 already be writing to the receive area.
308 UINTN VirtioNetReqSize
;
310 UINT16 RxAlwaysPending
;
314 EFI_PHYSICAL_ADDRESS RxBufDeviceAddress
;
318 // In VirtIo 1.0, the NumBuffers field is mandatory. In 0.9.5, it depends on
319 // VIRTIO_NET_F_MRG_RXBUF, which we never negotiate.
321 VirtioNetReqSize
= (Dev
->VirtIo
->Revision
< VIRTIO_SPEC_REVISION (1, 0, 0)) ?
322 sizeof (VIRTIO_NET_REQ
) :
323 sizeof (VIRTIO_1_0_NET_REQ
);
326 // For each incoming packet we must supply two descriptors:
327 // - the recipient for the virtio-net request header, plus
328 // - the recipient for the network data (which consists of Ethernet header
329 // and Ethernet payload).
331 RxBufSize
= VirtioNetReqSize
+
332 (Dev
->Snm
.MediaHeaderSize
+ Dev
->Snm
.MaxPacketSize
);
335 // Limit the number of pending RX packets if the queue is big. The division
336 // by two is due to the above "two descriptors per packet" trait.
338 RxAlwaysPending
= (UINT16
) MIN (Dev
->RxRing
.QueueSize
/ 2, VNET_MAX_PENDING
);
341 // The RxBuf is shared between guest and hypervisor, use
342 // AllocateSharedPages() to allocate this memory region and map it with
343 // BusMasterCommonBuffer so that it can be accessed by both guest and
346 NumBytes
= RxAlwaysPending
* RxBufSize
;
347 Dev
->RxBufNrPages
= EFI_SIZE_TO_PAGES (NumBytes
);
348 Status
= Dev
->VirtIo
->AllocateSharedPages (
353 if (EFI_ERROR (Status
)) {
357 ZeroMem (RxBuffer
, NumBytes
);
359 Status
= VirtioMapAllBytesInSharedBuffer (
361 VirtioOperationBusMasterCommonBuffer
,
364 &Dev
->RxBufDeviceBase
,
367 if (EFI_ERROR (Status
)) {
368 goto FreeSharedBuffer
;
371 Dev
->RxBuf
= RxBuffer
;
374 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
377 Dev
->RxLastUsed
= *Dev
->RxRing
.Used
.Idx
;
378 ASSERT (Dev
->RxLastUsed
== 0);
381 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device:
382 // the host should not send interrupts, we'll poll in VirtioNetReceive()
383 // and VirtioNetIsPacketAvailable().
385 *Dev
->RxRing
.Avail
.Flags
= (UINT16
) VRING_AVAIL_F_NO_INTERRUPT
;
388 // now set up a separate, two-part descriptor chain for each RX packet, and
389 // link each chain into (from) the available ring as well
392 RxBufDeviceAddress
= Dev
->RxBufDeviceBase
;
393 for (PktIdx
= 0; PktIdx
< RxAlwaysPending
; ++PktIdx
) {
395 // virtio-0.9.5, 2.4.1.2 Updating the Available Ring
396 // invisible to the host until we update the Index Field
398 Dev
->RxRing
.Avail
.Ring
[PktIdx
] = DescIdx
;
401 // virtio-0.9.5, 2.4.1.1 Placing Buffers into the Descriptor Table
403 Dev
->RxRing
.Desc
[DescIdx
].Addr
= RxBufDeviceAddress
;
404 Dev
->RxRing
.Desc
[DescIdx
].Len
= (UINT32
) VirtioNetReqSize
;
405 Dev
->RxRing
.Desc
[DescIdx
].Flags
= VRING_DESC_F_WRITE
| VRING_DESC_F_NEXT
;
406 Dev
->RxRing
.Desc
[DescIdx
].Next
= (UINT16
) (DescIdx
+ 1);
407 RxBufDeviceAddress
+= Dev
->RxRing
.Desc
[DescIdx
++].Len
;
409 Dev
->RxRing
.Desc
[DescIdx
].Addr
= RxBufDeviceAddress
;
410 Dev
->RxRing
.Desc
[DescIdx
].Len
= (UINT32
) (RxBufSize
- VirtioNetReqSize
);
411 Dev
->RxRing
.Desc
[DescIdx
].Flags
= VRING_DESC_F_WRITE
;
412 RxBufDeviceAddress
+= Dev
->RxRing
.Desc
[DescIdx
++].Len
;
416 // virtio-0.9.5, 2.4.1.3 Updating the Index Field
419 *Dev
->RxRing
.Avail
.Idx
= RxAlwaysPending
;
422 // At this point reception may already be running. In order to make it sure,
423 // kick the hypervisor. If we fail to kick it, we must first abort reception
424 // before tearing down anything, because reception may have been already
425 // running even without the kick.
427 // virtio-0.9.5, 2.4.1.4 Notifying the Device
430 Status
= Dev
->VirtIo
->SetQueueNotify (Dev
->VirtIo
, VIRTIO_NET_Q_RX
);
431 if (EFI_ERROR (Status
)) {
432 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
433 goto UnmapSharedBuffer
;
439 Dev
->VirtIo
->UnmapSharedBuffer (Dev
->VirtIo
, Dev
->RxBufMap
);
442 Dev
->VirtIo
->FreeSharedPages (
452 Resets a network adapter and allocates the transmit and receive buffers
453 required by the network interface; optionally, also requests allocation of
454 additional transmit and receive buffers.
456 @param This The protocol instance pointer.
457 @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer
458 space that the driver should allocate for the
459 network interface. Some network interfaces will not
460 be able to use the extra buffer, and the caller
461 will not know if it is actually being used.
462 @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer
463 space that the driver should allocate for the
464 network interface. Some network interfaces will not
465 be able to use the extra buffer, and the caller
466 will not know if it is actually being used.
468 @retval EFI_SUCCESS The network interface was initialized.
469 @retval EFI_NOT_STARTED The network interface has not been started.
470 @retval EFI_OUT_OF_RESOURCES There was not enough memory for the transmit
472 @retval EFI_INVALID_PARAMETER One or more of the parameters has an
474 @retval EFI_DEVICE_ERROR The command could not be sent to the network
476 @retval EFI_UNSUPPORTED This function is not supported by the network
483 VirtioNetInitialize (
484 IN EFI_SIMPLE_NETWORK_PROTOCOL
*This
,
485 IN UINTN ExtraRxBufferSize OPTIONAL
,
486 IN UINTN ExtraTxBufferSize OPTIONAL
496 return EFI_INVALID_PARAMETER
;
498 if (ExtraRxBufferSize
> 0 || ExtraTxBufferSize
> 0) {
499 return EFI_UNSUPPORTED
;
502 Dev
= VIRTIO_NET_FROM_SNP (This
);
503 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
504 if (Dev
->Snm
.State
!= EfiSimpleNetworkStarted
) {
505 Status
= EFI_NOT_STARTED
;
510 // In the EfiSimpleNetworkStarted state the virtio-net device has status
511 // value 0 (= reset) -- see the state diagram, the full call chain to
512 // the end of VirtioNetGetFeatures() (considering we're here now),
513 // the DeviceFailed label below, and VirtioNetShutdown().
515 // Accordingly, the below is a subsequence of the steps found in the
516 // virtio-0.9.5 spec, 2.2.1 Device Initialization Sequence.
518 NextDevStat
= VSTAT_ACK
; // step 2 -- acknowledge device presence
519 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
520 if (EFI_ERROR (Status
)) {
524 NextDevStat
|= VSTAT_DRIVER
; // step 3 -- we know how to drive it
525 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
526 if (EFI_ERROR (Status
)) {
531 // Set Page Size - MMIO VirtIo Specific
533 Status
= Dev
->VirtIo
->SetPageSize (Dev
->VirtIo
, EFI_PAGE_SIZE
);
534 if (EFI_ERROR (Status
)) {
539 // step 4a -- retrieve features. Note that we're past validating required
540 // features in VirtioNetGetFeatures().
542 Status
= Dev
->VirtIo
->GetDeviceFeatures (Dev
->VirtIo
, &Features
);
543 if (EFI_ERROR (Status
)) {
547 ASSERT (Features
& VIRTIO_NET_F_MAC
);
548 ASSERT (Dev
->Snm
.MediaPresentSupported
==
549 !!(Features
& VIRTIO_NET_F_STATUS
));
551 Features
&= VIRTIO_NET_F_MAC
| VIRTIO_NET_F_STATUS
| VIRTIO_F_VERSION_1
;
554 // In virtio-1.0, feature negotiation is expected to complete before queue
555 // discovery, and the device can also reject the selected set of features.
557 if (Dev
->VirtIo
->Revision
>= VIRTIO_SPEC_REVISION (1, 0, 0)) {
558 Status
= Virtio10WriteFeatures (Dev
->VirtIo
, Features
, &NextDevStat
);
559 if (EFI_ERROR (Status
)) {
565 // step 4b, 4c -- allocate and report virtqueues
567 Status
= VirtioNetInitRing (
573 if (EFI_ERROR (Status
)) {
577 Status
= VirtioNetInitRing (
583 if (EFI_ERROR (Status
)) {
588 // step 5 -- keep only the features we want
590 if (Dev
->VirtIo
->Revision
< VIRTIO_SPEC_REVISION (1, 0, 0)) {
591 Features
&= ~(UINT64
)VIRTIO_F_VERSION_1
;
592 Status
= Dev
->VirtIo
->SetGuestFeatures (Dev
->VirtIo
, Features
);
593 if (EFI_ERROR (Status
)) {
599 // step 6 -- virtio-net initialization complete
601 NextDevStat
|= VSTAT_DRIVER_OK
;
602 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
603 if (EFI_ERROR (Status
)) {
607 Status
= VirtioNetInitTx (Dev
);
608 if (EFI_ERROR (Status
)) {
615 Status
= VirtioNetInitRx (Dev
);
616 if (EFI_ERROR (Status
)) {
620 Dev
->Snm
.State
= EfiSimpleNetworkInitialized
;
621 gBS
->RestoreTPL (OldTpl
);
625 VirtioNetShutdownTx (Dev
);
628 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
631 VirtioNetUninitRing (Dev
, &Dev
->TxRing
, Dev
->TxRingMap
);
634 VirtioNetUninitRing (Dev
, &Dev
->RxRing
, Dev
->RxRingMap
);
638 // restore device status invariant for the EfiSimpleNetworkStarted state
640 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
643 gBS
->RestoreTPL (OldTpl
);