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
,
99 (UINT32
) ((UINTN
) Ring
->Base
>> EFI_PAGE_SHIFT
));
100 if (EFI_ERROR (Status
)) {
107 VirtioRingUninit (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.
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
;
153 for (PktIdx
= 0; PktIdx
< Dev
->TxMaxPending
; ++PktIdx
) {
156 DescIdx
= (UINT16
) (2 * PktIdx
);
157 Dev
->TxFreeStack
[PktIdx
] = DescIdx
;
160 // For each possibly pending packet, lay out the descriptor for the common
161 // (unmodified by the host) virtio-net request header.
163 Dev
->TxRing
.Desc
[DescIdx
].Addr
= (UINTN
) &Dev
->TxSharedReq
;
164 Dev
->TxRing
.Desc
[DescIdx
].Len
= sizeof Dev
->TxSharedReq
;
165 Dev
->TxRing
.Desc
[DescIdx
].Flags
= VRING_DESC_F_NEXT
;
166 Dev
->TxRing
.Desc
[DescIdx
].Next
= (UINT16
) (DescIdx
+ 1);
169 // The second descriptor of each pending TX packet is updated on the fly,
170 // but it always terminates the descriptor chain of the packet.
172 Dev
->TxRing
.Desc
[DescIdx
+ 1].Flags
= 0;
176 // virtio-0.9.5, Appendix C, Packet Transmission
178 Dev
->TxSharedReq
.Flags
= 0;
179 Dev
->TxSharedReq
.GsoType
= VIRTIO_NET_HDR_GSO_NONE
;
182 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
185 Dev
->TxLastUsed
= *Dev
->TxRing
.Used
.Idx
;
186 ASSERT (Dev
->TxLastUsed
== 0);
189 // want no interrupt when a transmit completes
191 *Dev
->TxRing
.Avail
.Flags
= (UINT16
) VRING_AVAIL_F_NO_INTERRUPT
;
198 Set up static scaffolding for the VirtioNetReceive() SNP method and enable
199 live device operation.
201 This function may only be called as VirtioNetInitialize()'s final step.
203 The structures laid out and resources configured include:
204 - destination area for the host to write virtio-net request headers and
206 - select polling over RX interrupt,
207 - fully populate the RX queue with a static pattern of virtio descriptor
210 @param[in,out] Dev The VNET_DEV driver instance about to enter the
211 EfiSimpleNetworkInitialized state.
213 @retval EFI_OUT_OF_RESOURCES Failed to allocate RX destination area.
214 @return Status codes from VIRTIO_CFG_WRITE().
215 @retval EFI_SUCCESS RX setup successful. The device is live and may
216 already be writing to the receive area.
228 UINT16 RxAlwaysPending
;
234 // For each incoming packet we must supply two descriptors:
235 // - the recipient for the virtio-net request header, plus
236 // - the recipient for the network data (which consists of Ethernet header
237 // and Ethernet payload).
239 RxBufSize
= sizeof (VIRTIO_NET_REQ
) +
240 (Dev
->Snm
.MediaHeaderSize
+ Dev
->Snm
.MaxPacketSize
);
243 // Limit the number of pending RX packets if the queue is big. The division
244 // by two is due to the above "two descriptors per packet" trait.
246 RxAlwaysPending
= (UINT16
) MIN (Dev
->RxRing
.QueueSize
/ 2, VNET_MAX_PENDING
);
248 Dev
->RxBuf
= AllocatePool (RxAlwaysPending
* RxBufSize
);
249 if (Dev
->RxBuf
== NULL
) {
250 return EFI_OUT_OF_RESOURCES
;
254 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
257 Dev
->RxLastUsed
= *Dev
->RxRing
.Used
.Idx
;
258 ASSERT (Dev
->RxLastUsed
== 0);
261 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device:
262 // the host should not send interrupts, we'll poll in VirtioNetReceive()
263 // and VirtioNetIsPacketAvailable().
265 *Dev
->RxRing
.Avail
.Flags
= (UINT16
) VRING_AVAIL_F_NO_INTERRUPT
;
268 // now set up a separate, two-part descriptor chain for each RX packet, and
269 // link each chain into (from) the available ring as well
273 for (PktIdx
= 0; PktIdx
< RxAlwaysPending
; ++PktIdx
) {
275 // virtio-0.9.5, 2.4.1.2 Updating the Available Ring
276 // invisible to the host until we update the Index Field
278 Dev
->RxRing
.Avail
.Ring
[PktIdx
] = DescIdx
;
281 // virtio-0.9.5, 2.4.1.1 Placing Buffers into the Descriptor Table
283 Dev
->RxRing
.Desc
[DescIdx
].Addr
= (UINTN
) RxPtr
;
284 Dev
->RxRing
.Desc
[DescIdx
].Len
= sizeof (VIRTIO_NET_REQ
);
285 Dev
->RxRing
.Desc
[DescIdx
].Flags
= VRING_DESC_F_WRITE
| VRING_DESC_F_NEXT
;
286 Dev
->RxRing
.Desc
[DescIdx
].Next
= (UINT16
) (DescIdx
+ 1);
287 RxPtr
+= Dev
->RxRing
.Desc
[DescIdx
++].Len
;
289 Dev
->RxRing
.Desc
[DescIdx
].Addr
= (UINTN
) RxPtr
;
290 Dev
->RxRing
.Desc
[DescIdx
].Len
= (UINT32
) (RxBufSize
-
291 sizeof (VIRTIO_NET_REQ
));
292 Dev
->RxRing
.Desc
[DescIdx
].Flags
= VRING_DESC_F_WRITE
;
293 RxPtr
+= Dev
->RxRing
.Desc
[DescIdx
++].Len
;
297 // virtio-0.9.5, 2.4.1.3 Updating the Index Field
300 *Dev
->RxRing
.Avail
.Idx
= RxAlwaysPending
;
303 // At this point reception may already be running. In order to make it sure,
304 // kick the hypervisor. If we fail to kick it, we must first abort reception
305 // before tearing down anything, because reception may have been already
306 // running even without the kick.
308 // virtio-0.9.5, 2.4.1.4 Notifying the Device
311 Status
= Dev
->VirtIo
->SetQueueNotify (Dev
->VirtIo
, VIRTIO_NET_Q_RX
);
312 if (EFI_ERROR (Status
)) {
313 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
314 FreePool (Dev
->RxBuf
);
322 Resets a network adapter and allocates the transmit and receive buffers
323 required by the network interface; optionally, also requests allocation of
324 additional transmit and receive buffers.
326 @param This The protocol instance pointer.
327 @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer
328 space that the driver should allocate for the
329 network interface. Some network interfaces will not
330 be able to use the extra buffer, and the caller
331 will not know if it is actually being used.
332 @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer
333 space that the driver should allocate for the
334 network interface. Some network interfaces will not
335 be able to use the extra buffer, and the caller
336 will not know if it is actually being used.
338 @retval EFI_SUCCESS The network interface was initialized.
339 @retval EFI_NOT_STARTED The network interface has not been started.
340 @retval EFI_OUT_OF_RESOURCES There was not enough memory for the transmit
342 @retval EFI_INVALID_PARAMETER One or more of the parameters has an
344 @retval EFI_DEVICE_ERROR The command could not be sent to the network
346 @retval EFI_UNSUPPORTED This function is not supported by the network
353 VirtioNetInitialize (
354 IN EFI_SIMPLE_NETWORK_PROTOCOL
*This
,
355 IN UINTN ExtraRxBufferSize OPTIONAL
,
356 IN UINTN ExtraTxBufferSize OPTIONAL
366 return EFI_INVALID_PARAMETER
;
368 if (ExtraRxBufferSize
> 0 || ExtraTxBufferSize
> 0) {
369 return EFI_UNSUPPORTED
;
372 Dev
= VIRTIO_NET_FROM_SNP (This
);
373 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
374 if (Dev
->Snm
.State
!= EfiSimpleNetworkStarted
) {
375 Status
= EFI_NOT_STARTED
;
380 // In the EfiSimpleNetworkStarted state the virtio-net device has status
381 // value 0 (= reset) -- see the state diagram, the full call chain to
382 // the end of VirtioNetGetFeatures() (considering we're here now),
383 // the DeviceFailed label below, and VirtioNetShutdown().
385 // Accordingly, the below is a subsequence of the steps found in the
386 // virtio-0.9.5 spec, 2.2.1 Device Initialization Sequence.
388 NextDevStat
= VSTAT_ACK
; // step 2 -- acknowledge device presence
389 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
390 if (EFI_ERROR (Status
)) {
394 NextDevStat
|= VSTAT_DRIVER
; // step 3 -- we know how to drive it
395 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
396 if (EFI_ERROR (Status
)) {
401 // Set Page Size - MMIO VirtIo Specific
403 Status
= Dev
->VirtIo
->SetPageSize (Dev
->VirtIo
, EFI_PAGE_SIZE
);
404 if (EFI_ERROR (Status
)) {
409 // step 4a -- retrieve features. Note that we're past validating required
410 // features in VirtioNetGetFeatures().
412 Status
= Dev
->VirtIo
->GetDeviceFeatures (Dev
->VirtIo
, &Features
);
413 if (EFI_ERROR (Status
)) {
417 ASSERT (Features
& VIRTIO_NET_F_MAC
);
418 ASSERT (Dev
->Snm
.MediaPresentSupported
==
419 !!(Features
& VIRTIO_NET_F_STATUS
));
422 // step 4b, 4c -- allocate and report virtqueues
424 Status
= VirtioNetInitRing (Dev
, VIRTIO_NET_Q_RX
, &Dev
->RxRing
);
425 if (EFI_ERROR (Status
)) {
429 Status
= VirtioNetInitRing (Dev
, VIRTIO_NET_Q_TX
, &Dev
->TxRing
);
430 if (EFI_ERROR (Status
)) {
435 // step 5 -- keep only the features we want
437 Features
&= VIRTIO_NET_F_MAC
| VIRTIO_NET_F_STATUS
;
438 Status
= Dev
->VirtIo
->SetGuestFeatures (Dev
->VirtIo
, Features
);
439 if (EFI_ERROR (Status
)) {
444 // step 6 -- virtio-net initialization complete
446 NextDevStat
|= VSTAT_DRIVER_OK
;
447 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
448 if (EFI_ERROR (Status
)) {
452 Status
= VirtioNetInitTx (Dev
);
453 if (EFI_ERROR (Status
)) {
460 Status
= VirtioNetInitRx (Dev
);
461 if (EFI_ERROR (Status
)) {
465 Dev
->Snm
.State
= EfiSimpleNetworkInitialized
;
466 gBS
->RestoreTPL (OldTpl
);
470 VirtioNetShutdownTx (Dev
);
473 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
476 VirtioRingUninit (&Dev
->TxRing
);
479 VirtioRingUninit (&Dev
->RxRing
);
483 // restore device status invariant for the EfiSimpleNetworkStarted state
485 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
488 gBS
->RestoreTPL (OldTpl
);