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>
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
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.
19 #include <Library/BaseLib.h>
20 #include <Library/MemoryAllocationLib.h>
21 #include <Library/UefiBootServicesTableLib.h>
23 #include "VirtioNet.h"
26 Initialize a virtio ring for a specific transfer direction of the virtio-net
29 This function may only be called by VirtioNetInitialize().
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
35 @param[out] Ring The virtio-ring inside the VNET_DEV structure,
36 corresponding to Selector.
38 @retval EFI_UNSUPPORTED The queue size reported by the virtio-net device is
40 @return Status codes from VIRTIO_CFG_WRITE(),
41 VIRTIO_CFG_READ() and VirtioRingInit().
42 @retval EFI_SUCCESS Ring initialized.
58 // step 4b -- allocate selected queue
60 Status
= Dev
->VirtIo
->SetQueueSel (Dev
->VirtIo
, Selector
);
61 if (EFI_ERROR (Status
)) {
64 Status
= Dev
->VirtIo
->GetQueueNumMax (Dev
->VirtIo
, &QueueSize
);
65 if (EFI_ERROR (Status
)) {
70 // For each packet (RX and TX alike), we need two descriptors:
71 // one for the virtio-net request header, and another one for the data
74 return EFI_UNSUPPORTED
;
76 Status
= VirtioRingInit (QueueSize
, Ring
);
77 if (EFI_ERROR (Status
)) {
82 // Additional steps for MMIO: align the queue appropriately, and set the
83 // size. If anything fails from here on, we must release the ring resources.
85 Status
= Dev
->VirtIo
->SetQueueNum (Dev
->VirtIo
, QueueSize
);
86 if (EFI_ERROR (Status
)) {
90 Status
= Dev
->VirtIo
->SetQueueAlign (Dev
->VirtIo
, EFI_PAGE_SIZE
);
91 if (EFI_ERROR (Status
)) {
96 // step 4c -- report GPFN (guest-physical frame number) of queue
98 Status
= Dev
->VirtIo
->SetQueueAddress (Dev
->VirtIo
, Ring
);
99 if (EFI_ERROR (Status
)) {
106 VirtioRingUninit (Ring
);
113 Set up static scaffolding for the VirtioNetTransmit() and
114 VirtioNetGetStatus() SNP methods.
116 This function may only be called by VirtioNetInitialize().
118 The structures laid out and resources configured include:
119 - fully populate the TX queue with a static pattern of virtio descriptor
121 - tracking of heads of free descriptor chains from the above,
122 - one common virtio-net request header (never modified by the host) for all
124 - select polling over TX interrupt.
126 @param[in,out] Dev The VNET_DEV driver instance about to enter the
127 EfiSimpleNetworkInitialized state.
129 @retval EFI_OUT_OF_RESOURCES Failed to allocate the stack to track the heads
130 of free descriptor chains.
131 @retval EFI_SUCCESS TX setup successful.
141 UINTN TxSharedReqSize
;
144 Dev
->TxMaxPending
= (UINT16
) MIN (Dev
->TxRing
.QueueSize
/ 2,
146 Dev
->TxCurPending
= 0;
147 Dev
->TxFreeStack
= AllocatePool (Dev
->TxMaxPending
*
148 sizeof *Dev
->TxFreeStack
);
149 if (Dev
->TxFreeStack
== NULL
) {
150 return EFI_OUT_OF_RESOURCES
;
154 // In VirtIo 1.0, the NumBuffers field is mandatory. In 0.9.5, it depends on
155 // VIRTIO_NET_F_MRG_RXBUF, which we never negotiate.
157 TxSharedReqSize
= (Dev
->VirtIo
->Revision
< VIRTIO_SPEC_REVISION (1, 0, 0)) ?
158 sizeof Dev
->TxSharedReq
.V0_9_5
:
159 sizeof Dev
->TxSharedReq
;
161 for (PktIdx
= 0; PktIdx
< Dev
->TxMaxPending
; ++PktIdx
) {
164 DescIdx
= (UINT16
) (2 * PktIdx
);
165 Dev
->TxFreeStack
[PktIdx
] = DescIdx
;
168 // For each possibly pending packet, lay out the descriptor for the common
169 // (unmodified by the host) virtio-net request header.
171 Dev
->TxRing
.Desc
[DescIdx
].Addr
= (UINTN
) &Dev
->TxSharedReq
;
172 Dev
->TxRing
.Desc
[DescIdx
].Len
= (UINT32
) TxSharedReqSize
;
173 Dev
->TxRing
.Desc
[DescIdx
].Flags
= VRING_DESC_F_NEXT
;
174 Dev
->TxRing
.Desc
[DescIdx
].Next
= (UINT16
) (DescIdx
+ 1);
177 // The second descriptor of each pending TX packet is updated on the fly,
178 // but it always terminates the descriptor chain of the packet.
180 Dev
->TxRing
.Desc
[DescIdx
+ 1].Flags
= 0;
184 // virtio-0.9.5, Appendix C, Packet Transmission
186 Dev
->TxSharedReq
.V0_9_5
.Flags
= 0;
187 Dev
->TxSharedReq
.V0_9_5
.GsoType
= VIRTIO_NET_HDR_GSO_NONE
;
190 // For VirtIo 1.0 only -- the field exists, but it is unused
192 Dev
->TxSharedReq
.NumBuffers
= 0;
195 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
198 Dev
->TxLastUsed
= *Dev
->TxRing
.Used
.Idx
;
199 ASSERT (Dev
->TxLastUsed
== 0);
202 // want no interrupt when a transmit completes
204 *Dev
->TxRing
.Avail
.Flags
= (UINT16
) VRING_AVAIL_F_NO_INTERRUPT
;
211 Set up static scaffolding for the VirtioNetReceive() SNP method and enable
212 live device operation.
214 This function may only be called as VirtioNetInitialize()'s final step.
216 The structures laid out and resources configured include:
217 - destination area for the host to write virtio-net request headers and
219 - select polling over RX interrupt,
220 - fully populate the RX queue with a static pattern of virtio descriptor
223 @param[in,out] Dev The VNET_DEV driver instance about to enter the
224 EfiSimpleNetworkInitialized state.
226 @retval EFI_OUT_OF_RESOURCES Failed to allocate RX destination area.
227 @return Status codes from VIRTIO_CFG_WRITE().
228 @retval EFI_SUCCESS RX setup successful. The device is live and may
229 already be writing to the receive area.
240 UINTN VirtioNetReqSize
;
242 UINT16 RxAlwaysPending
;
248 // In VirtIo 1.0, the NumBuffers field is mandatory. In 0.9.5, it depends on
249 // VIRTIO_NET_F_MRG_RXBUF, which we never negotiate.
251 VirtioNetReqSize
= (Dev
->VirtIo
->Revision
< VIRTIO_SPEC_REVISION (1, 0, 0)) ?
252 sizeof (VIRTIO_NET_REQ
) :
253 sizeof (VIRTIO_1_0_NET_REQ
);
256 // For each incoming packet we must supply two descriptors:
257 // - the recipient for the virtio-net request header, plus
258 // - the recipient for the network data (which consists of Ethernet header
259 // and Ethernet payload).
261 RxBufSize
= VirtioNetReqSize
+
262 (Dev
->Snm
.MediaHeaderSize
+ Dev
->Snm
.MaxPacketSize
);
265 // Limit the number of pending RX packets if the queue is big. The division
266 // by two is due to the above "two descriptors per packet" trait.
268 RxAlwaysPending
= (UINT16
) MIN (Dev
->RxRing
.QueueSize
/ 2, VNET_MAX_PENDING
);
270 Dev
->RxBuf
= AllocatePool (RxAlwaysPending
* RxBufSize
);
271 if (Dev
->RxBuf
== NULL
) {
272 return EFI_OUT_OF_RESOURCES
;
276 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
279 Dev
->RxLastUsed
= *Dev
->RxRing
.Used
.Idx
;
280 ASSERT (Dev
->RxLastUsed
== 0);
283 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device:
284 // the host should not send interrupts, we'll poll in VirtioNetReceive()
285 // and VirtioNetIsPacketAvailable().
287 *Dev
->RxRing
.Avail
.Flags
= (UINT16
) VRING_AVAIL_F_NO_INTERRUPT
;
290 // now set up a separate, two-part descriptor chain for each RX packet, and
291 // link each chain into (from) the available ring as well
295 for (PktIdx
= 0; PktIdx
< RxAlwaysPending
; ++PktIdx
) {
297 // virtio-0.9.5, 2.4.1.2 Updating the Available Ring
298 // invisible to the host until we update the Index Field
300 Dev
->RxRing
.Avail
.Ring
[PktIdx
] = DescIdx
;
303 // virtio-0.9.5, 2.4.1.1 Placing Buffers into the Descriptor Table
305 Dev
->RxRing
.Desc
[DescIdx
].Addr
= (UINTN
) RxPtr
;
306 Dev
->RxRing
.Desc
[DescIdx
].Len
= (UINT32
) VirtioNetReqSize
;
307 Dev
->RxRing
.Desc
[DescIdx
].Flags
= VRING_DESC_F_WRITE
| VRING_DESC_F_NEXT
;
308 Dev
->RxRing
.Desc
[DescIdx
].Next
= (UINT16
) (DescIdx
+ 1);
309 RxPtr
+= Dev
->RxRing
.Desc
[DescIdx
++].Len
;
311 Dev
->RxRing
.Desc
[DescIdx
].Addr
= (UINTN
) RxPtr
;
312 Dev
->RxRing
.Desc
[DescIdx
].Len
= (UINT32
) (RxBufSize
- VirtioNetReqSize
);
313 Dev
->RxRing
.Desc
[DescIdx
].Flags
= VRING_DESC_F_WRITE
;
314 RxPtr
+= Dev
->RxRing
.Desc
[DescIdx
++].Len
;
318 // virtio-0.9.5, 2.4.1.3 Updating the Index Field
321 *Dev
->RxRing
.Avail
.Idx
= RxAlwaysPending
;
324 // At this point reception may already be running. In order to make it sure,
325 // kick the hypervisor. If we fail to kick it, we must first abort reception
326 // before tearing down anything, because reception may have been already
327 // running even without the kick.
329 // virtio-0.9.5, 2.4.1.4 Notifying the Device
332 Status
= Dev
->VirtIo
->SetQueueNotify (Dev
->VirtIo
, VIRTIO_NET_Q_RX
);
333 if (EFI_ERROR (Status
)) {
334 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
335 FreePool (Dev
->RxBuf
);
343 Resets a network adapter and allocates the transmit and receive buffers
344 required by the network interface; optionally, also requests allocation of
345 additional transmit and receive buffers.
347 @param This The protocol instance pointer.
348 @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer
349 space that the driver should allocate for the
350 network interface. Some network interfaces will not
351 be able to use the extra buffer, and the caller
352 will not know if it is actually being used.
353 @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer
354 space that the driver should allocate for the
355 network interface. Some network interfaces will not
356 be able to use the extra buffer, and the caller
357 will not know if it is actually being used.
359 @retval EFI_SUCCESS The network interface was initialized.
360 @retval EFI_NOT_STARTED The network interface has not been started.
361 @retval EFI_OUT_OF_RESOURCES There was not enough memory for the transmit
363 @retval EFI_INVALID_PARAMETER One or more of the parameters has an
365 @retval EFI_DEVICE_ERROR The command could not be sent to the network
367 @retval EFI_UNSUPPORTED This function is not supported by the network
374 VirtioNetInitialize (
375 IN EFI_SIMPLE_NETWORK_PROTOCOL
*This
,
376 IN UINTN ExtraRxBufferSize OPTIONAL
,
377 IN UINTN ExtraTxBufferSize OPTIONAL
387 return EFI_INVALID_PARAMETER
;
389 if (ExtraRxBufferSize
> 0 || ExtraTxBufferSize
> 0) {
390 return EFI_UNSUPPORTED
;
393 Dev
= VIRTIO_NET_FROM_SNP (This
);
394 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
395 if (Dev
->Snm
.State
!= EfiSimpleNetworkStarted
) {
396 Status
= EFI_NOT_STARTED
;
401 // In the EfiSimpleNetworkStarted state the virtio-net device has status
402 // value 0 (= reset) -- see the state diagram, the full call chain to
403 // the end of VirtioNetGetFeatures() (considering we're here now),
404 // the DeviceFailed label below, and VirtioNetShutdown().
406 // Accordingly, the below is a subsequence of the steps found in the
407 // virtio-0.9.5 spec, 2.2.1 Device Initialization Sequence.
409 NextDevStat
= VSTAT_ACK
; // step 2 -- acknowledge device presence
410 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
411 if (EFI_ERROR (Status
)) {
415 NextDevStat
|= VSTAT_DRIVER
; // step 3 -- we know how to drive it
416 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
417 if (EFI_ERROR (Status
)) {
422 // Set Page Size - MMIO VirtIo Specific
424 Status
= Dev
->VirtIo
->SetPageSize (Dev
->VirtIo
, EFI_PAGE_SIZE
);
425 if (EFI_ERROR (Status
)) {
430 // step 4a -- retrieve features. Note that we're past validating required
431 // features in VirtioNetGetFeatures().
433 Status
= Dev
->VirtIo
->GetDeviceFeatures (Dev
->VirtIo
, &Features
);
434 if (EFI_ERROR (Status
)) {
438 ASSERT (Features
& VIRTIO_NET_F_MAC
);
439 ASSERT (Dev
->Snm
.MediaPresentSupported
==
440 !!(Features
& VIRTIO_NET_F_STATUS
));
442 Features
&= VIRTIO_NET_F_MAC
| VIRTIO_NET_F_STATUS
| VIRTIO_F_VERSION_1
;
445 // In virtio-1.0, feature negotiation is expected to complete before queue
446 // discovery, and the device can also reject the selected set of features.
448 if (Dev
->VirtIo
->Revision
>= VIRTIO_SPEC_REVISION (1, 0, 0)) {
449 Status
= Virtio10WriteFeatures (Dev
->VirtIo
, Features
, &NextDevStat
);
450 if (EFI_ERROR (Status
)) {
456 // step 4b, 4c -- allocate and report virtqueues
458 Status
= VirtioNetInitRing (Dev
, VIRTIO_NET_Q_RX
, &Dev
->RxRing
);
459 if (EFI_ERROR (Status
)) {
463 Status
= VirtioNetInitRing (Dev
, VIRTIO_NET_Q_TX
, &Dev
->TxRing
);
464 if (EFI_ERROR (Status
)) {
469 // step 5 -- keep only the features we want
471 if (Dev
->VirtIo
->Revision
< VIRTIO_SPEC_REVISION (1, 0, 0)) {
472 Features
&= ~(UINT64
)VIRTIO_F_VERSION_1
;
473 Status
= Dev
->VirtIo
->SetGuestFeatures (Dev
->VirtIo
, Features
);
474 if (EFI_ERROR (Status
)) {
480 // step 6 -- virtio-net initialization complete
482 NextDevStat
|= VSTAT_DRIVER_OK
;
483 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
484 if (EFI_ERROR (Status
)) {
488 Status
= VirtioNetInitTx (Dev
);
489 if (EFI_ERROR (Status
)) {
496 Status
= VirtioNetInitRx (Dev
);
497 if (EFI_ERROR (Status
)) {
501 Dev
->Snm
.State
= EfiSimpleNetworkInitialized
;
502 gBS
->RestoreTPL (OldTpl
);
506 VirtioNetShutdownTx (Dev
);
509 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
512 VirtioRingUninit (&Dev
->TxRing
);
515 VirtioRingUninit (&Dev
->RxRing
);
519 // restore device status invariant for the EfiSimpleNetworkStarted state
521 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
524 gBS
->RestoreTPL (OldTpl
);