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/MemoryAllocationLib.h>
22 #include <Library/UefiBootServicesTableLib.h>
24 #include "VirtioNet.h"
27 Initialize a virtio ring for a specific transfer direction of the virtio-net
30 This function may only be called by VirtioNetInitialize().
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
36 @param[out] Ring The virtio-ring inside the VNET_DEV structure,
37 corresponding to Selector.
39 @retval EFI_UNSUPPORTED The queue size reported by the virtio-net device is
41 @return Status codes from VIRTIO_CFG_WRITE(),
42 VIRTIO_CFG_READ() and VirtioRingInit().
43 @retval EFI_SUCCESS Ring initialized.
59 // step 4b -- allocate selected queue
61 Status
= Dev
->VirtIo
->SetQueueSel (Dev
->VirtIo
, Selector
);
62 if (EFI_ERROR (Status
)) {
65 Status
= Dev
->VirtIo
->GetQueueNumMax (Dev
->VirtIo
, &QueueSize
);
66 if (EFI_ERROR (Status
)) {
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
75 return EFI_UNSUPPORTED
;
77 Status
= VirtioRingInit (Dev
->VirtIo
, QueueSize
, Ring
);
78 if (EFI_ERROR (Status
)) {
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.
86 Status
= Dev
->VirtIo
->SetQueueNum (Dev
->VirtIo
, QueueSize
);
87 if (EFI_ERROR (Status
)) {
91 Status
= Dev
->VirtIo
->SetQueueAlign (Dev
->VirtIo
, EFI_PAGE_SIZE
);
92 if (EFI_ERROR (Status
)) {
97 // step 4c -- report GPFN (guest-physical frame number) of queue
99 Status
= Dev
->VirtIo
->SetQueueAddress (Dev
->VirtIo
, Ring
, 0);
100 if (EFI_ERROR (Status
)) {
107 VirtioRingUninit (Dev
->VirtIo
, Ring
);
114 Set up static scaffolding for the VirtioNetTransmit() and
115 VirtioNetGetStatus() SNP methods.
117 This function may only be called by VirtioNetInitialize().
119 The structures laid out and resources configured include:
120 - fully populate the TX queue with a static pattern of virtio descriptor
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
125 - select polling over TX interrupt.
127 @param[in,out] Dev The VNET_DEV driver instance about to enter the
128 EfiSimpleNetworkInitialized state.
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.
142 UINTN TxSharedReqSize
;
145 Dev
->TxMaxPending
= (UINT16
) MIN (Dev
->TxRing
.QueueSize
/ 2,
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
;
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.
158 TxSharedReqSize
= (Dev
->VirtIo
->Revision
< VIRTIO_SPEC_REVISION (1, 0, 0)) ?
159 sizeof Dev
->TxSharedReq
.V0_9_5
:
160 sizeof Dev
->TxSharedReq
;
162 for (PktIdx
= 0; PktIdx
< Dev
->TxMaxPending
; ++PktIdx
) {
165 DescIdx
= (UINT16
) (2 * PktIdx
);
166 Dev
->TxFreeStack
[PktIdx
] = DescIdx
;
169 // For each possibly pending packet, lay out the descriptor for the common
170 // (unmodified by the host) virtio-net request header.
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);
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.
181 Dev
->TxRing
.Desc
[DescIdx
+ 1].Flags
= 0;
185 // virtio-0.9.5, Appendix C, Packet Transmission
187 Dev
->TxSharedReq
.V0_9_5
.Flags
= 0;
188 Dev
->TxSharedReq
.V0_9_5
.GsoType
= VIRTIO_NET_HDR_GSO_NONE
;
191 // For VirtIo 1.0 only -- the field exists, but it is unused
193 Dev
->TxSharedReq
.NumBuffers
= 0;
196 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
199 Dev
->TxLastUsed
= *Dev
->TxRing
.Used
.Idx
;
200 ASSERT (Dev
->TxLastUsed
== 0);
203 // want no interrupt when a transmit completes
205 *Dev
->TxRing
.Avail
.Flags
= (UINT16
) VRING_AVAIL_F_NO_INTERRUPT
;
212 Set up static scaffolding for the VirtioNetReceive() SNP method and enable
213 live device operation.
215 This function may only be called as VirtioNetInitialize()'s final step.
217 The structures laid out and resources configured include:
218 - destination area for the host to write virtio-net request headers and
220 - select polling over RX interrupt,
221 - fully populate the RX queue with a static pattern of virtio descriptor
224 @param[in,out] Dev The VNET_DEV driver instance about to enter the
225 EfiSimpleNetworkInitialized state.
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.
241 UINTN VirtioNetReqSize
;
243 UINT16 RxAlwaysPending
;
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.
252 VirtioNetReqSize
= (Dev
->VirtIo
->Revision
< VIRTIO_SPEC_REVISION (1, 0, 0)) ?
253 sizeof (VIRTIO_NET_REQ
) :
254 sizeof (VIRTIO_1_0_NET_REQ
);
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).
262 RxBufSize
= VirtioNetReqSize
+
263 (Dev
->Snm
.MediaHeaderSize
+ Dev
->Snm
.MaxPacketSize
);
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.
269 RxAlwaysPending
= (UINT16
) MIN (Dev
->RxRing
.QueueSize
/ 2, VNET_MAX_PENDING
);
271 Dev
->RxBuf
= AllocatePool (RxAlwaysPending
* RxBufSize
);
272 if (Dev
->RxBuf
== NULL
) {
273 return EFI_OUT_OF_RESOURCES
;
277 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
280 Dev
->RxLastUsed
= *Dev
->RxRing
.Used
.Idx
;
281 ASSERT (Dev
->RxLastUsed
== 0);
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().
288 *Dev
->RxRing
.Avail
.Flags
= (UINT16
) VRING_AVAIL_F_NO_INTERRUPT
;
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
296 for (PktIdx
= 0; PktIdx
< RxAlwaysPending
; ++PktIdx
) {
298 // virtio-0.9.5, 2.4.1.2 Updating the Available Ring
299 // invisible to the host until we update the Index Field
301 Dev
->RxRing
.Avail
.Ring
[PktIdx
] = DescIdx
;
304 // virtio-0.9.5, 2.4.1.1 Placing Buffers into the Descriptor Table
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
;
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
;
319 // virtio-0.9.5, 2.4.1.3 Updating the Index Field
322 *Dev
->RxRing
.Avail
.Idx
= RxAlwaysPending
;
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.
330 // virtio-0.9.5, 2.4.1.4 Notifying the Device
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
);
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.
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.
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
364 @retval EFI_INVALID_PARAMETER One or more of the parameters has an
366 @retval EFI_DEVICE_ERROR The command could not be sent to the network
368 @retval EFI_UNSUPPORTED This function is not supported by the network
375 VirtioNetInitialize (
376 IN EFI_SIMPLE_NETWORK_PROTOCOL
*This
,
377 IN UINTN ExtraRxBufferSize OPTIONAL
,
378 IN UINTN ExtraTxBufferSize OPTIONAL
388 return EFI_INVALID_PARAMETER
;
390 if (ExtraRxBufferSize
> 0 || ExtraTxBufferSize
> 0) {
391 return EFI_UNSUPPORTED
;
394 Dev
= VIRTIO_NET_FROM_SNP (This
);
395 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
396 if (Dev
->Snm
.State
!= EfiSimpleNetworkStarted
) {
397 Status
= EFI_NOT_STARTED
;
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().
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.
410 NextDevStat
= VSTAT_ACK
; // step 2 -- acknowledge device presence
411 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
412 if (EFI_ERROR (Status
)) {
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
)) {
423 // Set Page Size - MMIO VirtIo Specific
425 Status
= Dev
->VirtIo
->SetPageSize (Dev
->VirtIo
, EFI_PAGE_SIZE
);
426 if (EFI_ERROR (Status
)) {
431 // step 4a -- retrieve features. Note that we're past validating required
432 // features in VirtioNetGetFeatures().
434 Status
= Dev
->VirtIo
->GetDeviceFeatures (Dev
->VirtIo
, &Features
);
435 if (EFI_ERROR (Status
)) {
439 ASSERT (Features
& VIRTIO_NET_F_MAC
);
440 ASSERT (Dev
->Snm
.MediaPresentSupported
==
441 !!(Features
& VIRTIO_NET_F_STATUS
));
443 Features
&= VIRTIO_NET_F_MAC
| VIRTIO_NET_F_STATUS
| VIRTIO_F_VERSION_1
;
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.
449 if (Dev
->VirtIo
->Revision
>= VIRTIO_SPEC_REVISION (1, 0, 0)) {
450 Status
= Virtio10WriteFeatures (Dev
->VirtIo
, Features
, &NextDevStat
);
451 if (EFI_ERROR (Status
)) {
457 // step 4b, 4c -- allocate and report virtqueues
459 Status
= VirtioNetInitRing (Dev
, VIRTIO_NET_Q_RX
, &Dev
->RxRing
);
460 if (EFI_ERROR (Status
)) {
464 Status
= VirtioNetInitRing (Dev
, VIRTIO_NET_Q_TX
, &Dev
->TxRing
);
465 if (EFI_ERROR (Status
)) {
470 // step 5 -- keep only the features we want
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
)) {
481 // step 6 -- virtio-net initialization complete
483 NextDevStat
|= VSTAT_DRIVER_OK
;
484 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
485 if (EFI_ERROR (Status
)) {
489 Status
= VirtioNetInitTx (Dev
);
490 if (EFI_ERROR (Status
)) {
497 Status
= VirtioNetInitRx (Dev
);
498 if (EFI_ERROR (Status
)) {
502 Dev
->Snm
.State
= EfiSimpleNetworkInitialized
;
503 gBS
->RestoreTPL (OldTpl
);
507 VirtioNetShutdownTx (Dev
);
510 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
513 VirtioNetUninitRing (Dev
, &Dev
->TxRing
);
516 VirtioNetUninitRing (Dev
, &Dev
->RxRing
);
520 // restore device status invariant for the EfiSimpleNetworkStarted state
522 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
525 gBS
->RestoreTPL (OldTpl
);