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
= VIRTIO_CFG_WRITE (Dev
, Generic
.VhdrQueueSelect
, Selector
);
61 if (EFI_ERROR (Status
)) {
64 Status
= VIRTIO_CFG_READ (Dev
, Generic
.VhdrQueueSize
, &QueueSize
);
65 if (EFI_ERROR (Status
)) {
69 // For each packet (RX and TX alike), we need two descriptors:
70 // one for the virtio-net request header, and another one for the data
73 return EFI_UNSUPPORTED
;
75 Status
= VirtioRingInit (QueueSize
, Ring
);
76 if (EFI_ERROR (Status
)) {
81 // step 4c -- report GPFN (guest-physical frame number) of queue
83 Status
= VIRTIO_CFG_WRITE (Dev
, Generic
.VhdrQueueAddress
,
84 (UINTN
) Ring
->Base
>> EFI_PAGE_SHIFT
);
85 if (EFI_ERROR (Status
)) {
86 VirtioRingUninit (Ring
);
93 Set up static scaffolding for the VirtioNetTransmit() and
94 VirtioNetGetStatus() SNP methods.
96 This function may only be called by VirtioNetInitialize().
98 The structures laid out and resources configured include:
99 - fully populate the TX queue with a static pattern of virtio descriptor
101 - tracking of heads of free descriptor chains from the above,
102 - one common virtio-net request header (never modified by the host) for all
104 - select polling over TX interrupt.
106 @param[in,out] Dev The VNET_DEV driver instance about to enter the
107 EfiSimpleNetworkInitialized state.
109 @retval EFI_OUT_OF_RESOURCES Failed to allocate the stack to track the heads
110 of free descriptor chains.
111 @retval EFI_SUCCESS TX setup successful.
123 Dev
->TxMaxPending
= (UINT16
) MIN (Dev
->TxRing
.QueueSize
/ 2,
125 Dev
->TxCurPending
= 0;
126 Dev
->TxFreeStack
= AllocatePool (Dev
->TxMaxPending
*
127 sizeof *Dev
->TxFreeStack
);
128 if (Dev
->TxFreeStack
== NULL
) {
129 return EFI_OUT_OF_RESOURCES
;
132 for (PktIdx
= 0; PktIdx
< Dev
->TxMaxPending
; ++PktIdx
) {
135 DescIdx
= (UINT16
) (2 * PktIdx
);
136 Dev
->TxFreeStack
[PktIdx
] = DescIdx
;
139 // For each possibly pending packet, lay out the descriptor for the common
140 // (unmodified by the host) virtio-net request header.
142 Dev
->TxRing
.Desc
[DescIdx
].Addr
= (UINTN
) &Dev
->TxSharedReq
;
143 Dev
->TxRing
.Desc
[DescIdx
].Len
= sizeof Dev
->TxSharedReq
;
144 Dev
->TxRing
.Desc
[DescIdx
].Flags
= VRING_DESC_F_NEXT
;
145 Dev
->TxRing
.Desc
[DescIdx
].Next
= (UINT16
) (DescIdx
+ 1);
148 // The second descriptor of each pending TX packet is updated on the fly,
149 // but it always terminates the descriptor chain of the packet.
151 Dev
->TxRing
.Desc
[DescIdx
+ 1].Flags
= 0;
155 // virtio-0.9.5, Appendix C, Packet Transmission
157 Dev
->TxSharedReq
.Flags
= 0;
158 Dev
->TxSharedReq
.GsoType
= VIRTIO_NET_HDR_GSO_NONE
;
161 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
164 Dev
->TxLastUsed
= *Dev
->TxRing
.Used
.Idx
;
165 ASSERT (Dev
->TxLastUsed
== 0);
168 // want no interrupt when a transmit completes
170 *Dev
->TxRing
.Avail
.Flags
= (UINT16
) VRING_AVAIL_F_NO_INTERRUPT
;
177 Set up static scaffolding for the VirtioNetReceive() SNP method and enable
178 live device operation.
180 This function may only be called as VirtioNetInitialize()'s final step.
182 The structures laid out and resources configured include:
183 - destination area for the host to write virtio-net request headers and
185 - select polling over RX interrupt,
186 - fully populate the RX queue with a static pattern of virtio descriptor
189 @param[in,out] Dev The VNET_DEV driver instance about to enter the
190 EfiSimpleNetworkInitialized state.
192 @retval EFI_OUT_OF_RESOURCES Failed to allocate RX destination area.
193 @return Status codes from VIRTIO_CFG_WRITE().
194 @retval EFI_SUCCESS RX setup successful. The device is live and may
195 already be writing to the receive area.
207 UINT16 RxAlwaysPending
;
213 // For each incoming packet we must supply two descriptors:
214 // - the recipient for the virtio-net request header, plus
215 // - the recipient for the network data (which consists of Ethernet header
216 // and Ethernet payload).
218 RxBufSize
= sizeof (VIRTIO_NET_REQ
) +
219 (Dev
->Snm
.MediaHeaderSize
+ Dev
->Snm
.MaxPacketSize
);
222 // Limit the number of pending RX packets if the queue is big. The division
223 // by two is due to the above "two descriptors per packet" trait.
225 RxAlwaysPending
= (UINT16
) MIN (Dev
->RxRing
.QueueSize
/ 2, VNET_MAX_PENDING
);
227 Dev
->RxBuf
= AllocatePool (RxAlwaysPending
* RxBufSize
);
228 if (Dev
->RxBuf
== NULL
) {
229 return EFI_OUT_OF_RESOURCES
;
233 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
236 Dev
->RxLastUsed
= *Dev
->RxRing
.Used
.Idx
;
237 ASSERT (Dev
->RxLastUsed
== 0);
240 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device:
241 // the host should not send interrupts, we'll poll in VirtioNetReceive()
242 // and VirtioNetIsPacketAvailable().
244 *Dev
->RxRing
.Avail
.Flags
= (UINT16
) VRING_AVAIL_F_NO_INTERRUPT
;
247 // now set up a separate, two-part descriptor chain for each RX packet, and
248 // link each chain into (from) the available ring as well
252 for (PktIdx
= 0; PktIdx
< RxAlwaysPending
; ++PktIdx
) {
254 // virtio-0.9.5, 2.4.1.2 Updating the Available Ring
255 // invisible to the host until we update the Index Field
257 Dev
->RxRing
.Avail
.Ring
[PktIdx
] = DescIdx
;
260 // virtio-0.9.5, 2.4.1.1 Placing Buffers into the Descriptor Table
262 Dev
->RxRing
.Desc
[DescIdx
].Addr
= (UINTN
) RxPtr
;
263 Dev
->RxRing
.Desc
[DescIdx
].Len
= sizeof (VIRTIO_NET_REQ
);
264 Dev
->RxRing
.Desc
[DescIdx
].Flags
= VRING_DESC_F_WRITE
| VRING_DESC_F_NEXT
;
265 Dev
->RxRing
.Desc
[DescIdx
].Next
= (UINT16
) (DescIdx
+ 1);
266 RxPtr
+= Dev
->RxRing
.Desc
[DescIdx
++].Len
;
268 Dev
->RxRing
.Desc
[DescIdx
].Addr
= (UINTN
) RxPtr
;
269 Dev
->RxRing
.Desc
[DescIdx
].Len
= (UINT32
) (RxBufSize
-
270 sizeof (VIRTIO_NET_REQ
));
271 Dev
->RxRing
.Desc
[DescIdx
].Flags
= VRING_DESC_F_WRITE
;
272 RxPtr
+= Dev
->RxRing
.Desc
[DescIdx
++].Len
;
276 // virtio-0.9.5, 2.4.1.3 Updating the Index Field
279 *Dev
->RxRing
.Avail
.Idx
= RxAlwaysPending
;
282 // At this point reception may already be running. In order to make it sure,
283 // kick the hypervisor. If we fail to kick it, we must first abort reception
284 // before tearing down anything, because reception may have been already
285 // running even without the kick.
287 // virtio-0.9.5, 2.4.1.4 Notifying the Device
290 Status
= VIRTIO_CFG_WRITE (Dev
, Generic
.VhdrQueueNotify
, VIRTIO_NET_Q_RX
);
292 if (EFI_ERROR (Status
)) {
293 VIRTIO_CFG_WRITE (Dev
, Generic
.VhdrDeviceStatus
, 0);
294 FreePool (Dev
->RxBuf
);
302 Resets a network adapter and allocates the transmit and receive buffers
303 required by the network interface; optionally, also requests allocation of
304 additional transmit and receive buffers.
306 @param This The protocol instance pointer.
307 @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer
308 space that the driver should allocate for the
309 network interface. Some network interfaces will not
310 be able to use the extra buffer, and the caller
311 will not know if it is actually being used.
312 @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer
313 space that the driver should allocate for the
314 network interface. Some network interfaces will not
315 be able to use the extra buffer, and the caller
316 will not know if it is actually being used.
318 @retval EFI_SUCCESS The network interface was initialized.
319 @retval EFI_NOT_STARTED The network interface has not been started.
320 @retval EFI_OUT_OF_RESOURCES There was not enough memory for the transmit
322 @retval EFI_INVALID_PARAMETER One or more of the parameters has an
324 @retval EFI_DEVICE_ERROR The command could not be sent to the network
326 @retval EFI_UNSUPPORTED This function is not supported by the network
333 VirtioNetInitialize (
334 IN EFI_SIMPLE_NETWORK_PROTOCOL
*This
,
335 IN UINTN ExtraRxBufferSize OPTIONAL
,
336 IN UINTN ExtraTxBufferSize OPTIONAL
346 return EFI_INVALID_PARAMETER
;
348 if (ExtraRxBufferSize
> 0 || ExtraTxBufferSize
> 0) {
349 return EFI_UNSUPPORTED
;
352 Dev
= VIRTIO_NET_FROM_SNP (This
);
353 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
354 if (Dev
->Snm
.State
!= EfiSimpleNetworkStarted
) {
355 Status
= EFI_NOT_STARTED
;
360 // In the EfiSimpleNetworkStarted state the virtio-net device has status
361 // value 0 (= reset) -- see the state diagram, the full call chain to
362 // the end of VirtioNetGetFeatures() (considering we're here now),
363 // the DeviceFailed label below, and VirtioNetShutdown().
365 // Accordingly, the below is a subsequence of the steps found in the
366 // virtio-0.9.5 spec, 2.2.1 Device Initialization Sequence.
368 NextDevStat
= VSTAT_ACK
; // step 2 -- acknowledge device presence
369 Status
= VIRTIO_CFG_WRITE (Dev
, Generic
.VhdrDeviceStatus
, NextDevStat
);
370 if (EFI_ERROR (Status
)) {
374 NextDevStat
|= VSTAT_DRIVER
; // step 3 -- we know how to drive it
375 Status
= VIRTIO_CFG_WRITE (Dev
, Generic
.VhdrDeviceStatus
, NextDevStat
);
376 if (EFI_ERROR (Status
)) {
381 // step 4a -- retrieve features. Note that we're past validating required
382 // features in VirtioNetGetFeatures().
384 Status
= VIRTIO_CFG_READ (Dev
, Generic
.VhdrDeviceFeatureBits
, &Features
);
385 if (EFI_ERROR (Status
)) {
388 ASSERT (Features
& VIRTIO_NET_F_MAC
);
389 ASSERT (Dev
->Snm
.MediaPresentSupported
==
390 !!(Features
& VIRTIO_NET_F_STATUS
));
393 // step 4b, 4c -- allocate and report virtqueues
395 Status
= VirtioNetInitRing (Dev
, VIRTIO_NET_Q_RX
, &Dev
->RxRing
);
396 if (EFI_ERROR (Status
)) {
400 Status
= VirtioNetInitRing (Dev
, VIRTIO_NET_Q_TX
, &Dev
->TxRing
);
401 if (EFI_ERROR (Status
)) {
406 // step 5 -- keep only the features we want
408 Features
&= VIRTIO_NET_F_MAC
| VIRTIO_NET_F_STATUS
;
409 Status
= VIRTIO_CFG_WRITE (Dev
, Generic
.VhdrGuestFeatureBits
, Features
);
410 if (EFI_ERROR (Status
)) {
415 // step 6 -- virtio-net initialization complete
417 NextDevStat
|= VSTAT_DRIVER_OK
;
418 Status
= VIRTIO_CFG_WRITE (Dev
, Generic
.VhdrDeviceStatus
, NextDevStat
);
419 if (EFI_ERROR (Status
)) {
423 Status
= VirtioNetInitTx (Dev
);
424 if (EFI_ERROR (Status
)) {
431 Status
= VirtioNetInitRx (Dev
);
432 if (EFI_ERROR (Status
)) {
436 Dev
->Snm
.State
= EfiSimpleNetworkInitialized
;
437 gBS
->RestoreTPL (OldTpl
);
441 VirtioNetShutdownTx (Dev
);
444 VIRTIO_CFG_WRITE (Dev
, Generic
.VhdrDeviceStatus
, 0);
447 VirtioRingUninit (&Dev
->TxRing
);
450 VirtioRingUninit (&Dev
->RxRing
);
454 // restore device status invariant for the EfiSimpleNetworkStarted state
456 VIRTIO_CFG_WRITE (Dev
, Generic
.VhdrDeviceStatus
, 0);
459 gBS
->RestoreTPL (OldTpl
);