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
= MIN (Dev
->TxRing
.QueueSize
/ 2, VNET_MAX_PENDING
);
124 Dev
->TxCurPending
= 0;
125 Dev
->TxFreeStack
= AllocatePool (Dev
->TxMaxPending
*
126 sizeof *Dev
->TxFreeStack
);
127 if (Dev
->TxFreeStack
== NULL
) {
128 return EFI_OUT_OF_RESOURCES
;
131 for (PktIdx
= 0; PktIdx
< Dev
->TxMaxPending
; ++PktIdx
) {
134 DescIdx
= (UINT16
) (2 * PktIdx
);
135 Dev
->TxFreeStack
[PktIdx
] = DescIdx
;
138 // For each possibly pending packet, lay out the descriptor for the common
139 // (unmodified by the host) virtio-net request header.
141 Dev
->TxRing
.Desc
[DescIdx
].Addr
= (UINTN
) &Dev
->TxSharedReq
;
142 Dev
->TxRing
.Desc
[DescIdx
].Len
= sizeof Dev
->TxSharedReq
;
143 Dev
->TxRing
.Desc
[DescIdx
].Flags
= VRING_DESC_F_NEXT
;
144 Dev
->TxRing
.Desc
[DescIdx
].Next
= DescIdx
+ 1;
147 // The second descriptor of each pending TX packet is updated on the fly,
148 // but it always terminates the descriptor chain of the packet.
150 Dev
->TxRing
.Desc
[DescIdx
+ 1].Flags
= 0;
154 // virtio-0.9.5, Appendix C, Packet Transmission
156 Dev
->TxSharedReq
.Flags
= 0;
157 Dev
->TxSharedReq
.GsoType
= VIRTIO_NET_HDR_GSO_NONE
;
160 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
163 Dev
->TxLastUsed
= *Dev
->TxRing
.Used
.Idx
;
164 ASSERT (Dev
->TxLastUsed
== 0);
167 // want no interrupt when a transmit completes
169 *Dev
->TxRing
.Avail
.Flags
= (UINT16
) VRING_AVAIL_F_NO_INTERRUPT
;
176 Set up static scaffolding for the VirtioNetReceive() SNP method and enable
177 live device operation.
179 This function may only be called as VirtioNetInitialize()'s final step.
181 The structures laid out and resources configured include:
182 - destination area for the host to write virtio-net request headers and
184 - select polling over RX interrupt,
185 - fully populate the RX queue with a static pattern of virtio descriptor
188 @param[in,out] Dev The VNET_DEV driver instance about to enter the
189 EfiSimpleNetworkInitialized state.
191 @retval EFI_OUT_OF_RESOURCES Failed to allocate RX destination area.
192 @return Status codes from VIRTIO_CFG_WRITE().
193 @retval EFI_SUCCESS RX setup successful. The device is live and may
194 already be writing to the receive area.
206 UINT16 RxAlwaysPending
;
212 // For each incoming packet we must supply two descriptors:
213 // - the recipient for the virtio-net request header, plus
214 // - the recipient for the network data (which consists of Ethernet header
215 // and Ethernet payload).
217 RxBufSize
= sizeof (VIRTIO_NET_REQ
) +
218 (Dev
->Snm
.MediaHeaderSize
+ Dev
->Snm
.MaxPacketSize
);
221 // Limit the number of pending RX packets if the queue is big. The division
222 // by two is due to the above "two descriptors per packet" trait.
224 RxAlwaysPending
= MIN (Dev
->RxRing
.QueueSize
/ 2, VNET_MAX_PENDING
);
226 Dev
->RxBuf
= AllocatePool (RxAlwaysPending
* RxBufSize
);
227 if (Dev
->RxBuf
== NULL
) {
228 return EFI_OUT_OF_RESOURCES
;
232 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
235 Dev
->RxLastUsed
= *Dev
->RxRing
.Used
.Idx
;
236 ASSERT (Dev
->RxLastUsed
== 0);
239 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device:
240 // the host should not send interrupts, we'll poll in VirtioNetReceive()
241 // and VirtioNetIsPacketAvailable().
243 *Dev
->RxRing
.Avail
.Flags
= (UINT16
) VRING_AVAIL_F_NO_INTERRUPT
;
246 // now set up a separate, two-part descriptor chain for each RX packet, and
247 // link each chain into (from) the available ring as well
251 for (PktIdx
= 0; PktIdx
< RxAlwaysPending
; ++PktIdx
) {
253 // virtio-0.9.5, 2.4.1.2 Updating the Available Ring
254 // invisible to the host until we update the Index Field
256 Dev
->RxRing
.Avail
.Ring
[PktIdx
] = DescIdx
;
259 // virtio-0.9.5, 2.4.1.1 Placing Buffers into the Descriptor Table
261 Dev
->RxRing
.Desc
[DescIdx
].Addr
= (UINTN
) RxPtr
;
262 Dev
->RxRing
.Desc
[DescIdx
].Len
= sizeof (VIRTIO_NET_REQ
);
263 Dev
->RxRing
.Desc
[DescIdx
].Flags
= VRING_DESC_F_WRITE
| VRING_DESC_F_NEXT
;
264 Dev
->RxRing
.Desc
[DescIdx
].Next
= DescIdx
+ 1;
265 RxPtr
+= Dev
->RxRing
.Desc
[DescIdx
++].Len
;
267 Dev
->RxRing
.Desc
[DescIdx
].Addr
= (UINTN
) RxPtr
;
268 Dev
->RxRing
.Desc
[DescIdx
].Len
= RxBufSize
- sizeof (VIRTIO_NET_REQ
);
269 Dev
->RxRing
.Desc
[DescIdx
].Flags
= VRING_DESC_F_WRITE
;
270 RxPtr
+= Dev
->RxRing
.Desc
[DescIdx
++].Len
;
274 // virtio-0.9.5, 2.4.1.3 Updating the Index Field
277 *Dev
->RxRing
.Avail
.Idx
= RxAlwaysPending
;
280 // At this point reception may already be running. In order to make it sure,
281 // kick the hypervisor. If we fail to kick it, we must first abort reception
282 // before tearing down anything, because reception may have been already
283 // running even without the kick.
285 // virtio-0.9.5, 2.4.1.4 Notifying the Device
288 Status
= VIRTIO_CFG_WRITE (Dev
, Generic
.VhdrQueueNotify
, VIRTIO_NET_Q_RX
);
290 if (EFI_ERROR (Status
)) {
291 VIRTIO_CFG_WRITE (Dev
, Generic
.VhdrDeviceStatus
, 0);
292 FreePool (Dev
->RxBuf
);
300 Resets a network adapter and allocates the transmit and receive buffers
301 required by the network interface; optionally, also requests allocation of
302 additional transmit and receive buffers.
304 @param This The protocol instance pointer.
305 @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer
306 space that the driver should allocate for the
307 network interface. Some network interfaces will not
308 be able to use the extra buffer, and the caller
309 will not know if it is actually being used.
310 @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer
311 space that the driver should allocate for the
312 network interface. Some network interfaces will not
313 be able to use the extra buffer, and the caller
314 will not know if it is actually being used.
316 @retval EFI_SUCCESS The network interface was initialized.
317 @retval EFI_NOT_STARTED The network interface has not been started.
318 @retval EFI_OUT_OF_RESOURCES There was not enough memory for the transmit
320 @retval EFI_INVALID_PARAMETER One or more of the parameters has an
322 @retval EFI_DEVICE_ERROR The command could not be sent to the network
324 @retval EFI_UNSUPPORTED This function is not supported by the network
331 VirtioNetInitialize (
332 IN EFI_SIMPLE_NETWORK_PROTOCOL
*This
,
333 IN UINTN ExtraRxBufferSize OPTIONAL
,
334 IN UINTN ExtraTxBufferSize OPTIONAL
344 return EFI_INVALID_PARAMETER
;
346 if (ExtraRxBufferSize
> 0 || ExtraTxBufferSize
> 0) {
347 return EFI_UNSUPPORTED
;
350 Dev
= VIRTIO_NET_FROM_SNP (This
);
351 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
352 if (Dev
->Snm
.State
!= EfiSimpleNetworkStarted
) {
353 Status
= EFI_NOT_STARTED
;
358 // In the EfiSimpleNetworkStarted state the virtio-net device has status
359 // value 0 (= reset) -- see the state diagram, the full call chain to
360 // the end of VirtioNetGetFeatures() (considering we're here now),
361 // the DeviceFailed label below, and VirtioNetShutdown().
363 // Accordingly, the below is a subsequence of the steps found in the
364 // virtio-0.9.5 spec, 2.2.1 Device Initialization Sequence.
366 NextDevStat
= VSTAT_ACK
; // step 2 -- acknowledge device presence
367 Status
= VIRTIO_CFG_WRITE (Dev
, Generic
.VhdrDeviceStatus
, NextDevStat
);
368 if (EFI_ERROR (Status
)) {
372 NextDevStat
|= VSTAT_DRIVER
; // step 3 -- we know how to drive it
373 Status
= VIRTIO_CFG_WRITE (Dev
, Generic
.VhdrDeviceStatus
, NextDevStat
);
374 if (EFI_ERROR (Status
)) {
379 // step 4a -- retrieve features. Note that we're past validating required
380 // features in VirtioNetGetFeatures().
382 Status
= VIRTIO_CFG_READ (Dev
, Generic
.VhdrDeviceFeatureBits
, &Features
);
383 if (EFI_ERROR (Status
)) {
386 ASSERT (Features
& VIRTIO_NET_F_MAC
);
387 ASSERT (Dev
->Snm
.MediaPresentSupported
==
388 !!(Features
& VIRTIO_NET_F_STATUS
));
391 // step 4b, 4c -- allocate and report virtqueues
393 Status
= VirtioNetInitRing (Dev
, VIRTIO_NET_Q_RX
, &Dev
->RxRing
);
394 if (EFI_ERROR (Status
)) {
398 Status
= VirtioNetInitRing (Dev
, VIRTIO_NET_Q_TX
, &Dev
->TxRing
);
399 if (EFI_ERROR (Status
)) {
404 // step 5 -- keep only the features we want
406 Features
&= VIRTIO_NET_F_MAC
| VIRTIO_NET_F_STATUS
;
407 Status
= VIRTIO_CFG_WRITE (Dev
, Generic
.VhdrGuestFeatureBits
, Features
);
408 if (EFI_ERROR (Status
)) {
413 // step 6 -- virtio-net initialization complete
415 NextDevStat
|= VSTAT_DRIVER_OK
;
416 Status
= VIRTIO_CFG_WRITE (Dev
, Generic
.VhdrDeviceStatus
, NextDevStat
);
417 if (EFI_ERROR (Status
)) {
421 Status
= VirtioNetInitTx (Dev
);
422 if (EFI_ERROR (Status
)) {
429 Status
= VirtioNetInitRx (Dev
);
430 if (EFI_ERROR (Status
)) {
434 Dev
->Snm
.State
= EfiSimpleNetworkInitialized
;
435 gBS
->RestoreTPL (OldTpl
);
439 VirtioNetShutdownTx (Dev
);
442 VIRTIO_CFG_WRITE (Dev
, Generic
.VhdrDeviceStatus
, 0);
445 VirtioRingUninit (&Dev
->TxRing
);
448 VirtioRingUninit (&Dev
->RxRing
);
452 // restore device status invariant for the EfiSimpleNetworkStarted state
454 VIRTIO_CFG_WRITE (Dev
, Generic
.VhdrDeviceStatus
, 0);
457 gBS
->RestoreTPL (OldTpl
);