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.
143 Dev
->TxMaxPending
= (UINT16
) MIN (Dev
->TxRing
.QueueSize
/ 2,
145 Dev
->TxCurPending
= 0;
146 Dev
->TxFreeStack
= AllocatePool (Dev
->TxMaxPending
*
147 sizeof *Dev
->TxFreeStack
);
148 if (Dev
->TxFreeStack
== NULL
) {
149 return EFI_OUT_OF_RESOURCES
;
152 for (PktIdx
= 0; PktIdx
< Dev
->TxMaxPending
; ++PktIdx
) {
155 DescIdx
= (UINT16
) (2 * PktIdx
);
156 Dev
->TxFreeStack
[PktIdx
] = DescIdx
;
159 // For each possibly pending packet, lay out the descriptor for the common
160 // (unmodified by the host) virtio-net request header.
162 Dev
->TxRing
.Desc
[DescIdx
].Addr
= (UINTN
) &Dev
->TxSharedReq
;
163 Dev
->TxRing
.Desc
[DescIdx
].Len
= sizeof Dev
->TxSharedReq
;
164 Dev
->TxRing
.Desc
[DescIdx
].Flags
= VRING_DESC_F_NEXT
;
165 Dev
->TxRing
.Desc
[DescIdx
].Next
= (UINT16
) (DescIdx
+ 1);
168 // The second descriptor of each pending TX packet is updated on the fly,
169 // but it always terminates the descriptor chain of the packet.
171 Dev
->TxRing
.Desc
[DescIdx
+ 1].Flags
= 0;
175 // virtio-0.9.5, Appendix C, Packet Transmission
177 Dev
->TxSharedReq
.Flags
= 0;
178 Dev
->TxSharedReq
.GsoType
= VIRTIO_NET_HDR_GSO_NONE
;
181 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
184 Dev
->TxLastUsed
= *Dev
->TxRing
.Used
.Idx
;
185 ASSERT (Dev
->TxLastUsed
== 0);
188 // want no interrupt when a transmit completes
190 *Dev
->TxRing
.Avail
.Flags
= (UINT16
) VRING_AVAIL_F_NO_INTERRUPT
;
197 Set up static scaffolding for the VirtioNetReceive() SNP method and enable
198 live device operation.
200 This function may only be called as VirtioNetInitialize()'s final step.
202 The structures laid out and resources configured include:
203 - destination area for the host to write virtio-net request headers and
205 - select polling over RX interrupt,
206 - fully populate the RX queue with a static pattern of virtio descriptor
209 @param[in,out] Dev The VNET_DEV driver instance about to enter the
210 EfiSimpleNetworkInitialized state.
212 @retval EFI_OUT_OF_RESOURCES Failed to allocate RX destination area.
213 @return Status codes from VIRTIO_CFG_WRITE().
214 @retval EFI_SUCCESS RX setup successful. The device is live and may
215 already be writing to the receive area.
227 UINT16 RxAlwaysPending
;
233 // For each incoming packet we must supply two descriptors:
234 // - the recipient for the virtio-net request header, plus
235 // - the recipient for the network data (which consists of Ethernet header
236 // and Ethernet payload).
238 RxBufSize
= sizeof (VIRTIO_NET_REQ
) +
239 (Dev
->Snm
.MediaHeaderSize
+ Dev
->Snm
.MaxPacketSize
);
242 // Limit the number of pending RX packets if the queue is big. The division
243 // by two is due to the above "two descriptors per packet" trait.
245 RxAlwaysPending
= (UINT16
) MIN (Dev
->RxRing
.QueueSize
/ 2, VNET_MAX_PENDING
);
247 Dev
->RxBuf
= AllocatePool (RxAlwaysPending
* RxBufSize
);
248 if (Dev
->RxBuf
== NULL
) {
249 return EFI_OUT_OF_RESOURCES
;
253 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
256 Dev
->RxLastUsed
= *Dev
->RxRing
.Used
.Idx
;
257 ASSERT (Dev
->RxLastUsed
== 0);
260 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device:
261 // the host should not send interrupts, we'll poll in VirtioNetReceive()
262 // and VirtioNetIsPacketAvailable().
264 *Dev
->RxRing
.Avail
.Flags
= (UINT16
) VRING_AVAIL_F_NO_INTERRUPT
;
267 // now set up a separate, two-part descriptor chain for each RX packet, and
268 // link each chain into (from) the available ring as well
272 for (PktIdx
= 0; PktIdx
< RxAlwaysPending
; ++PktIdx
) {
274 // virtio-0.9.5, 2.4.1.2 Updating the Available Ring
275 // invisible to the host until we update the Index Field
277 Dev
->RxRing
.Avail
.Ring
[PktIdx
] = DescIdx
;
280 // virtio-0.9.5, 2.4.1.1 Placing Buffers into the Descriptor Table
282 Dev
->RxRing
.Desc
[DescIdx
].Addr
= (UINTN
) RxPtr
;
283 Dev
->RxRing
.Desc
[DescIdx
].Len
= sizeof (VIRTIO_NET_REQ
);
284 Dev
->RxRing
.Desc
[DescIdx
].Flags
= VRING_DESC_F_WRITE
| VRING_DESC_F_NEXT
;
285 Dev
->RxRing
.Desc
[DescIdx
].Next
= (UINT16
) (DescIdx
+ 1);
286 RxPtr
+= Dev
->RxRing
.Desc
[DescIdx
++].Len
;
288 Dev
->RxRing
.Desc
[DescIdx
].Addr
= (UINTN
) RxPtr
;
289 Dev
->RxRing
.Desc
[DescIdx
].Len
= (UINT32
) (RxBufSize
-
290 sizeof (VIRTIO_NET_REQ
));
291 Dev
->RxRing
.Desc
[DescIdx
].Flags
= VRING_DESC_F_WRITE
;
292 RxPtr
+= Dev
->RxRing
.Desc
[DescIdx
++].Len
;
296 // virtio-0.9.5, 2.4.1.3 Updating the Index Field
299 *Dev
->RxRing
.Avail
.Idx
= RxAlwaysPending
;
302 // At this point reception may already be running. In order to make it sure,
303 // kick the hypervisor. If we fail to kick it, we must first abort reception
304 // before tearing down anything, because reception may have been already
305 // running even without the kick.
307 // virtio-0.9.5, 2.4.1.4 Notifying the Device
310 Status
= Dev
->VirtIo
->SetQueueNotify (Dev
->VirtIo
, VIRTIO_NET_Q_RX
);
311 if (EFI_ERROR (Status
)) {
312 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
313 FreePool (Dev
->RxBuf
);
321 Resets a network adapter and allocates the transmit and receive buffers
322 required by the network interface; optionally, also requests allocation of
323 additional transmit and receive buffers.
325 @param This The protocol instance pointer.
326 @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer
327 space that the driver should allocate for the
328 network interface. Some network interfaces will not
329 be able to use the extra buffer, and the caller
330 will not know if it is actually being used.
331 @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer
332 space that the driver should allocate for the
333 network interface. Some network interfaces will not
334 be able to use the extra buffer, and the caller
335 will not know if it is actually being used.
337 @retval EFI_SUCCESS The network interface was initialized.
338 @retval EFI_NOT_STARTED The network interface has not been started.
339 @retval EFI_OUT_OF_RESOURCES There was not enough memory for the transmit
341 @retval EFI_INVALID_PARAMETER One or more of the parameters has an
343 @retval EFI_DEVICE_ERROR The command could not be sent to the network
345 @retval EFI_UNSUPPORTED This function is not supported by the network
352 VirtioNetInitialize (
353 IN EFI_SIMPLE_NETWORK_PROTOCOL
*This
,
354 IN UINTN ExtraRxBufferSize OPTIONAL
,
355 IN UINTN ExtraTxBufferSize OPTIONAL
365 return EFI_INVALID_PARAMETER
;
367 if (ExtraRxBufferSize
> 0 || ExtraTxBufferSize
> 0) {
368 return EFI_UNSUPPORTED
;
371 Dev
= VIRTIO_NET_FROM_SNP (This
);
372 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
373 if (Dev
->Snm
.State
!= EfiSimpleNetworkStarted
) {
374 Status
= EFI_NOT_STARTED
;
379 // In the EfiSimpleNetworkStarted state the virtio-net device has status
380 // value 0 (= reset) -- see the state diagram, the full call chain to
381 // the end of VirtioNetGetFeatures() (considering we're here now),
382 // the DeviceFailed label below, and VirtioNetShutdown().
384 // Accordingly, the below is a subsequence of the steps found in the
385 // virtio-0.9.5 spec, 2.2.1 Device Initialization Sequence.
387 NextDevStat
= VSTAT_ACK
; // step 2 -- acknowledge device presence
388 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
389 if (EFI_ERROR (Status
)) {
393 NextDevStat
|= VSTAT_DRIVER
; // step 3 -- we know how to drive it
394 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
395 if (EFI_ERROR (Status
)) {
400 // Set Page Size - MMIO VirtIo Specific
402 Status
= Dev
->VirtIo
->SetPageSize (Dev
->VirtIo
, EFI_PAGE_SIZE
);
403 if (EFI_ERROR (Status
)) {
408 // step 4a -- retrieve features. Note that we're past validating required
409 // features in VirtioNetGetFeatures().
411 Status
= Dev
->VirtIo
->GetDeviceFeatures (Dev
->VirtIo
, &Features
);
412 if (EFI_ERROR (Status
)) {
416 ASSERT (Features
& VIRTIO_NET_F_MAC
);
417 ASSERT (Dev
->Snm
.MediaPresentSupported
==
418 !!(Features
& VIRTIO_NET_F_STATUS
));
421 // step 4b, 4c -- allocate and report virtqueues
423 Status
= VirtioNetInitRing (Dev
, VIRTIO_NET_Q_RX
, &Dev
->RxRing
);
424 if (EFI_ERROR (Status
)) {
428 Status
= VirtioNetInitRing (Dev
, VIRTIO_NET_Q_TX
, &Dev
->TxRing
);
429 if (EFI_ERROR (Status
)) {
434 // step 5 -- keep only the features we want
436 Features
&= VIRTIO_NET_F_MAC
| VIRTIO_NET_F_STATUS
;
437 Status
= Dev
->VirtIo
->SetGuestFeatures (Dev
->VirtIo
, Features
);
438 if (EFI_ERROR (Status
)) {
443 // step 6 -- virtio-net initialization complete
445 NextDevStat
|= VSTAT_DRIVER_OK
;
446 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
447 if (EFI_ERROR (Status
)) {
451 Status
= VirtioNetInitTx (Dev
);
452 if (EFI_ERROR (Status
)) {
459 Status
= VirtioNetInitRx (Dev
);
460 if (EFI_ERROR (Status
)) {
464 Dev
->Snm
.State
= EfiSimpleNetworkInitialized
;
465 gBS
->RestoreTPL (OldTpl
);
469 VirtioNetShutdownTx (Dev
);
472 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
475 VirtioRingUninit (&Dev
->TxRing
);
478 VirtioRingUninit (&Dev
->RxRing
);
482 // restore device status invariant for the EfiSimpleNetworkStarted state
484 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
487 gBS
->RestoreTPL (OldTpl
);