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 or failed to init
152 @return Status codes from VIRTIO_DEVICE_PROTOCOL.
153 AllocateSharedPages() or
154 VirtioMapAllBytesInSharedBuffer()
155 @retval EFI_SUCCESS TX setup successful.
165 UINTN TxSharedReqSize
;
168 EFI_PHYSICAL_ADDRESS DeviceAddress
;
169 VOID
*TxSharedReqBuffer
;
171 Dev
->TxMaxPending
= (UINT16
) MIN (Dev
->TxRing
.QueueSize
/ 2,
173 Dev
->TxCurPending
= 0;
174 Dev
->TxFreeStack
= AllocatePool (Dev
->TxMaxPending
*
175 sizeof *Dev
->TxFreeStack
);
176 if (Dev
->TxFreeStack
== NULL
) {
177 return EFI_OUT_OF_RESOURCES
;
180 Dev
->TxBufCollection
= OrderedCollectionInit (
181 VirtioNetTxBufMapInfoCompare
,
182 VirtioNetTxBufDeviceAddressCompare
184 if (Dev
->TxBufCollection
== NULL
) {
185 Status
= EFI_OUT_OF_RESOURCES
;
186 goto FreeTxFreeStack
;
190 // Allocate TxSharedReq header and map with BusMasterCommonBuffer so that it
191 // can be accessed equally by both processor and device.
193 Status
= Dev
->VirtIo
->AllocateSharedPages (
195 EFI_SIZE_TO_PAGES (sizeof *Dev
->TxSharedReq
),
198 if (EFI_ERROR (Status
)) {
199 goto UninitTxBufCollection
;
202 ZeroMem (TxSharedReqBuffer
, sizeof *Dev
->TxSharedReq
);
204 Status
= VirtioMapAllBytesInSharedBuffer (
206 VirtioOperationBusMasterCommonBuffer
,
208 sizeof *(Dev
->TxSharedReq
),
212 if (EFI_ERROR (Status
)) {
213 goto FreeTxSharedReqBuffer
;
216 Dev
->TxSharedReq
= TxSharedReqBuffer
;
220 // In VirtIo 1.0, the NumBuffers field is mandatory. In 0.9.5, it depends on
221 // VIRTIO_NET_F_MRG_RXBUF, which we never negotiate.
223 TxSharedReqSize
= (Dev
->VirtIo
->Revision
< VIRTIO_SPEC_REVISION (1, 0, 0)) ?
224 sizeof (Dev
->TxSharedReq
->V0_9_5
) :
225 sizeof *Dev
->TxSharedReq
;
227 for (PktIdx
= 0; PktIdx
< Dev
->TxMaxPending
; ++PktIdx
) {
230 DescIdx
= (UINT16
) (2 * PktIdx
);
231 Dev
->TxFreeStack
[PktIdx
] = DescIdx
;
234 // For each possibly pending packet, lay out the descriptor for the common
235 // (unmodified by the host) virtio-net request header.
237 Dev
->TxRing
.Desc
[DescIdx
].Addr
= DeviceAddress
;
238 Dev
->TxRing
.Desc
[DescIdx
].Len
= (UINT32
) TxSharedReqSize
;
239 Dev
->TxRing
.Desc
[DescIdx
].Flags
= VRING_DESC_F_NEXT
;
240 Dev
->TxRing
.Desc
[DescIdx
].Next
= (UINT16
) (DescIdx
+ 1);
243 // The second descriptor of each pending TX packet is updated on the fly,
244 // but it always terminates the descriptor chain of the packet.
246 Dev
->TxRing
.Desc
[DescIdx
+ 1].Flags
= 0;
250 // virtio-0.9.5, Appendix C, Packet Transmission
252 Dev
->TxSharedReq
->V0_9_5
.Flags
= 0;
253 Dev
->TxSharedReq
->V0_9_5
.GsoType
= VIRTIO_NET_HDR_GSO_NONE
;
256 // For VirtIo 1.0 only -- the field exists, but it is unused
258 Dev
->TxSharedReq
->NumBuffers
= 0;
261 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
264 Dev
->TxLastUsed
= *Dev
->TxRing
.Used
.Idx
;
265 ASSERT (Dev
->TxLastUsed
== 0);
268 // want no interrupt when a transmit completes
270 *Dev
->TxRing
.Avail
.Flags
= (UINT16
) VRING_AVAIL_F_NO_INTERRUPT
;
274 FreeTxSharedReqBuffer
:
275 Dev
->VirtIo
->FreeSharedPages (
277 EFI_SIZE_TO_PAGES (sizeof *(Dev
->TxSharedReq
)),
281 UninitTxBufCollection
:
282 OrderedCollectionUninit (Dev
->TxBufCollection
);
285 FreePool (Dev
->TxFreeStack
);
292 Set up static scaffolding for the VirtioNetReceive() SNP method and enable
293 live device operation.
295 This function may only be called as VirtioNetInitialize()'s final step.
297 The structures laid out and resources configured include:
298 - destination area for the host to write virtio-net request headers and
300 - select polling over RX interrupt,
301 - fully populate the RX queue with a static pattern of virtio descriptor
304 @param[in,out] Dev The VNET_DEV driver instance about to enter the
305 EfiSimpleNetworkInitialized state.
307 @return Status codes from VIRTIO_CFG_WRITE() or
308 VIRTIO_DEVICE_PROTOCOL.AllocateSharedPages or
309 VirtioMapAllBytesInSharedBuffer().
310 @retval EFI_SUCCESS RX setup successful. The device is live and may
311 already be writing to the receive area.
322 UINTN VirtioNetReqSize
;
324 UINT16 RxAlwaysPending
;
328 EFI_PHYSICAL_ADDRESS RxBufDeviceAddress
;
332 // In VirtIo 1.0, the NumBuffers field is mandatory. In 0.9.5, it depends on
333 // VIRTIO_NET_F_MRG_RXBUF, which we never negotiate.
335 VirtioNetReqSize
= (Dev
->VirtIo
->Revision
< VIRTIO_SPEC_REVISION (1, 0, 0)) ?
336 sizeof (VIRTIO_NET_REQ
) :
337 sizeof (VIRTIO_1_0_NET_REQ
);
340 // For each incoming packet we must supply two descriptors:
341 // - the recipient for the virtio-net request header, plus
342 // - the recipient for the network data (which consists of Ethernet header
343 // and Ethernet payload).
345 RxBufSize
= VirtioNetReqSize
+
346 (Dev
->Snm
.MediaHeaderSize
+ Dev
->Snm
.MaxPacketSize
);
349 // Limit the number of pending RX packets if the queue is big. The division
350 // by two is due to the above "two descriptors per packet" trait.
352 RxAlwaysPending
= (UINT16
) MIN (Dev
->RxRing
.QueueSize
/ 2, VNET_MAX_PENDING
);
355 // The RxBuf is shared between guest and hypervisor, use
356 // AllocateSharedPages() to allocate this memory region and map it with
357 // BusMasterCommonBuffer so that it can be accessed by both guest and
360 NumBytes
= RxAlwaysPending
* RxBufSize
;
361 Dev
->RxBufNrPages
= EFI_SIZE_TO_PAGES (NumBytes
);
362 Status
= Dev
->VirtIo
->AllocateSharedPages (
367 if (EFI_ERROR (Status
)) {
371 ZeroMem (RxBuffer
, NumBytes
);
373 Status
= VirtioMapAllBytesInSharedBuffer (
375 VirtioOperationBusMasterCommonBuffer
,
378 &Dev
->RxBufDeviceBase
,
381 if (EFI_ERROR (Status
)) {
382 goto FreeSharedBuffer
;
385 Dev
->RxBuf
= RxBuffer
;
388 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
391 Dev
->RxLastUsed
= *Dev
->RxRing
.Used
.Idx
;
392 ASSERT (Dev
->RxLastUsed
== 0);
395 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device:
396 // the host should not send interrupts, we'll poll in VirtioNetReceive()
397 // and VirtioNetIsPacketAvailable().
399 *Dev
->RxRing
.Avail
.Flags
= (UINT16
) VRING_AVAIL_F_NO_INTERRUPT
;
402 // now set up a separate, two-part descriptor chain for each RX packet, and
403 // link each chain into (from) the available ring as well
406 RxBufDeviceAddress
= Dev
->RxBufDeviceBase
;
407 for (PktIdx
= 0; PktIdx
< RxAlwaysPending
; ++PktIdx
) {
409 // virtio-0.9.5, 2.4.1.2 Updating the Available Ring
410 // invisible to the host until we update the Index Field
412 Dev
->RxRing
.Avail
.Ring
[PktIdx
] = DescIdx
;
415 // virtio-0.9.5, 2.4.1.1 Placing Buffers into the Descriptor Table
417 Dev
->RxRing
.Desc
[DescIdx
].Addr
= RxBufDeviceAddress
;
418 Dev
->RxRing
.Desc
[DescIdx
].Len
= (UINT32
) VirtioNetReqSize
;
419 Dev
->RxRing
.Desc
[DescIdx
].Flags
= VRING_DESC_F_WRITE
| VRING_DESC_F_NEXT
;
420 Dev
->RxRing
.Desc
[DescIdx
].Next
= (UINT16
) (DescIdx
+ 1);
421 RxBufDeviceAddress
+= Dev
->RxRing
.Desc
[DescIdx
++].Len
;
423 Dev
->RxRing
.Desc
[DescIdx
].Addr
= RxBufDeviceAddress
;
424 Dev
->RxRing
.Desc
[DescIdx
].Len
= (UINT32
) (RxBufSize
- VirtioNetReqSize
);
425 Dev
->RxRing
.Desc
[DescIdx
].Flags
= VRING_DESC_F_WRITE
;
426 RxBufDeviceAddress
+= Dev
->RxRing
.Desc
[DescIdx
++].Len
;
430 // virtio-0.9.5, 2.4.1.3 Updating the Index Field
433 *Dev
->RxRing
.Avail
.Idx
= RxAlwaysPending
;
436 // At this point reception may already be running. In order to make it sure,
437 // kick the hypervisor. If we fail to kick it, we must first abort reception
438 // before tearing down anything, because reception may have been already
439 // running even without the kick.
441 // virtio-0.9.5, 2.4.1.4 Notifying the Device
444 Status
= Dev
->VirtIo
->SetQueueNotify (Dev
->VirtIo
, VIRTIO_NET_Q_RX
);
445 if (EFI_ERROR (Status
)) {
446 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
447 goto UnmapSharedBuffer
;
453 Dev
->VirtIo
->UnmapSharedBuffer (Dev
->VirtIo
, Dev
->RxBufMap
);
456 Dev
->VirtIo
->FreeSharedPages (
466 Resets a network adapter and allocates the transmit and receive buffers
467 required by the network interface; optionally, also requests allocation of
468 additional transmit and receive buffers.
470 @param This The protocol instance pointer.
471 @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer
472 space that the driver should allocate for the
473 network interface. Some network interfaces will not
474 be able to use the extra buffer, and the caller
475 will not know if it is actually being used.
476 @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer
477 space that the driver should allocate for the
478 network interface. Some network interfaces will not
479 be able to use the extra buffer, and the caller
480 will not know if it is actually being used.
482 @retval EFI_SUCCESS The network interface was initialized.
483 @retval EFI_NOT_STARTED The network interface has not been started.
484 @retval EFI_OUT_OF_RESOURCES There was not enough memory for the transmit
486 @retval EFI_INVALID_PARAMETER One or more of the parameters has an
488 @retval EFI_DEVICE_ERROR The command could not be sent to the network
490 @retval EFI_UNSUPPORTED This function is not supported by the network
497 VirtioNetInitialize (
498 IN EFI_SIMPLE_NETWORK_PROTOCOL
*This
,
499 IN UINTN ExtraRxBufferSize OPTIONAL
,
500 IN UINTN ExtraTxBufferSize OPTIONAL
510 return EFI_INVALID_PARAMETER
;
512 if (ExtraRxBufferSize
> 0 || ExtraTxBufferSize
> 0) {
513 return EFI_UNSUPPORTED
;
516 Dev
= VIRTIO_NET_FROM_SNP (This
);
517 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
518 if (Dev
->Snm
.State
!= EfiSimpleNetworkStarted
) {
519 Status
= EFI_NOT_STARTED
;
524 // In the EfiSimpleNetworkStarted state the virtio-net device has status
525 // value 0 (= reset) -- see the state diagram, the full call chain to
526 // the end of VirtioNetGetFeatures() (considering we're here now),
527 // the DeviceFailed label below, and VirtioNetShutdown().
529 // Accordingly, the below is a subsequence of the steps found in the
530 // virtio-0.9.5 spec, 2.2.1 Device Initialization Sequence.
532 NextDevStat
= VSTAT_ACK
; // step 2 -- acknowledge device presence
533 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
534 if (EFI_ERROR (Status
)) {
538 NextDevStat
|= VSTAT_DRIVER
; // step 3 -- we know how to drive it
539 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
540 if (EFI_ERROR (Status
)) {
545 // Set Page Size - MMIO VirtIo Specific
547 Status
= Dev
->VirtIo
->SetPageSize (Dev
->VirtIo
, EFI_PAGE_SIZE
);
548 if (EFI_ERROR (Status
)) {
553 // step 4a -- retrieve features. Note that we're past validating required
554 // features in VirtioNetGetFeatures().
556 Status
= Dev
->VirtIo
->GetDeviceFeatures (Dev
->VirtIo
, &Features
);
557 if (EFI_ERROR (Status
)) {
561 ASSERT (Features
& VIRTIO_NET_F_MAC
);
562 ASSERT (Dev
->Snm
.MediaPresentSupported
==
563 !!(Features
& VIRTIO_NET_F_STATUS
));
565 Features
&= VIRTIO_NET_F_MAC
| VIRTIO_NET_F_STATUS
| VIRTIO_F_VERSION_1
|
566 VIRTIO_F_IOMMU_PLATFORM
;
569 // In virtio-1.0, feature negotiation is expected to complete before queue
570 // discovery, and the device can also reject the selected set of features.
572 if (Dev
->VirtIo
->Revision
>= VIRTIO_SPEC_REVISION (1, 0, 0)) {
573 Status
= Virtio10WriteFeatures (Dev
->VirtIo
, Features
, &NextDevStat
);
574 if (EFI_ERROR (Status
)) {
580 // step 4b, 4c -- allocate and report virtqueues
582 Status
= VirtioNetInitRing (
588 if (EFI_ERROR (Status
)) {
592 Status
= VirtioNetInitRing (
598 if (EFI_ERROR (Status
)) {
603 // step 5 -- keep only the features we want
605 if (Dev
->VirtIo
->Revision
< VIRTIO_SPEC_REVISION (1, 0, 0)) {
606 Features
&= ~(UINT64
)(VIRTIO_F_VERSION_1
| VIRTIO_F_IOMMU_PLATFORM
);
607 Status
= Dev
->VirtIo
->SetGuestFeatures (Dev
->VirtIo
, Features
);
608 if (EFI_ERROR (Status
)) {
614 // step 6 -- virtio-net initialization complete
616 NextDevStat
|= VSTAT_DRIVER_OK
;
617 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
618 if (EFI_ERROR (Status
)) {
622 Status
= VirtioNetInitTx (Dev
);
623 if (EFI_ERROR (Status
)) {
630 Status
= VirtioNetInitRx (Dev
);
631 if (EFI_ERROR (Status
)) {
635 Dev
->Snm
.State
= EfiSimpleNetworkInitialized
;
636 gBS
->RestoreTPL (OldTpl
);
640 VirtioNetShutdownTx (Dev
);
643 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
646 VirtioNetUninitRing (Dev
, &Dev
->TxRing
, Dev
->TxRingMap
);
649 VirtioNetUninitRing (Dev
, &Dev
->RxRing
, Dev
->RxRingMap
);
653 // restore device status invariant for the EfiSimpleNetworkStarted state
655 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
658 gBS
->RestoreTPL (OldTpl
);