]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/VirtioNetDxe/SnpInitialize.c
OvmfPkg/VirtioMmioDeviceLib: Implement VIRTIO_DEVICE_PROTOCOL for VirtIo Devices...
[mirror_edk2.git] / OvmfPkg / VirtioNetDxe / SnpInitialize.c
1 /** @file
2
3 Implementation of the SNP.Initialize() function and its private helpers if
4 any.
5
6 Copyright (C) 2013, Red Hat, Inc.
7 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
8
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
13
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.
16
17 **/
18
19 #include <Library/BaseLib.h>
20 #include <Library/MemoryAllocationLib.h>
21 #include <Library/UefiBootServicesTableLib.h>
22
23 #include "VirtioNet.h"
24
25 /**
26 Initialize a virtio ring for a specific transfer direction of the virtio-net
27 device.
28
29 This function may only be called by VirtioNetInitialize().
30
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
34 the network device.
35 @param[out] Ring The virtio-ring inside the VNET_DEV structure,
36 corresponding to Selector.
37
38 @retval EFI_UNSUPPORTED The queue size reported by the virtio-net device is
39 too small.
40 @return Status codes from VIRTIO_CFG_WRITE(),
41 VIRTIO_CFG_READ() and VirtioRingInit().
42 @retval EFI_SUCCESS Ring initialized.
43 */
44
45 STATIC
46 EFI_STATUS
47 EFIAPI
48 VirtioNetInitRing (
49 IN OUT VNET_DEV *Dev,
50 IN UINT16 Selector,
51 OUT VRING *Ring
52 )
53 {
54 EFI_STATUS Status;
55 UINT16 QueueSize;
56
57 //
58 // step 4b -- allocate selected queue
59 //
60 Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrQueueSelect, Selector);
61 if (EFI_ERROR (Status)) {
62 return Status;
63 }
64 Status = VIRTIO_CFG_READ (Dev, Generic.VhdrQueueSize, &QueueSize);
65 if (EFI_ERROR (Status)) {
66 return Status;
67 }
68 //
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
71 //
72 if (QueueSize < 2) {
73 return EFI_UNSUPPORTED;
74 }
75 Status = VirtioRingInit (QueueSize, Ring);
76 if (EFI_ERROR (Status)) {
77 return Status;
78 }
79
80 //
81 // step 4c -- report GPFN (guest-physical frame number) of queue
82 //
83 Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrQueueAddress,
84 (UINTN) Ring->Base >> EFI_PAGE_SHIFT);
85 if (EFI_ERROR (Status)) {
86 VirtioRingUninit (Ring);
87 }
88 return Status;
89 }
90
91
92 /**
93 Set up static scaffolding for the VirtioNetTransmit() and
94 VirtioNetGetStatus() SNP methods.
95
96 This function may only be called by VirtioNetInitialize().
97
98 The structures laid out and resources configured include:
99 - fully populate the TX queue with a static pattern of virtio descriptor
100 chains,
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
103 pending TX packets,
104 - select polling over TX interrupt.
105
106 @param[in,out] Dev The VNET_DEV driver instance about to enter the
107 EfiSimpleNetworkInitialized state.
108
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.
112 */
113
114 STATIC
115 EFI_STATUS
116 EFIAPI
117 VirtioNetInitTx (
118 IN OUT VNET_DEV *Dev
119 )
120 {
121 UINTN PktIdx;
122
123 Dev->TxMaxPending = (UINT16) MIN (Dev->TxRing.QueueSize / 2,
124 VNET_MAX_PENDING);
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;
130 }
131
132 for (PktIdx = 0; PktIdx < Dev->TxMaxPending; ++PktIdx) {
133 UINT16 DescIdx;
134
135 DescIdx = (UINT16) (2 * PktIdx);
136 Dev->TxFreeStack[PktIdx] = DescIdx;
137
138 //
139 // For each possibly pending packet, lay out the descriptor for the common
140 // (unmodified by the host) virtio-net request header.
141 //
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);
146
147 //
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.
150 //
151 Dev->TxRing.Desc[DescIdx + 1].Flags = 0;
152 }
153
154 //
155 // virtio-0.9.5, Appendix C, Packet Transmission
156 //
157 Dev->TxSharedReq.Flags = 0;
158 Dev->TxSharedReq.GsoType = VIRTIO_NET_HDR_GSO_NONE;
159
160 //
161 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
162 //
163 MemoryFence ();
164 Dev->TxLastUsed = *Dev->TxRing.Used.Idx;
165 ASSERT (Dev->TxLastUsed == 0);
166
167 //
168 // want no interrupt when a transmit completes
169 //
170 *Dev->TxRing.Avail.Flags = (UINT16) VRING_AVAIL_F_NO_INTERRUPT;
171
172 return EFI_SUCCESS;
173 }
174
175
176 /**
177 Set up static scaffolding for the VirtioNetReceive() SNP method and enable
178 live device operation.
179
180 This function may only be called as VirtioNetInitialize()'s final step.
181
182 The structures laid out and resources configured include:
183 - destination area for the host to write virtio-net request headers and
184 packet data into,
185 - select polling over RX interrupt,
186 - fully populate the RX queue with a static pattern of virtio descriptor
187 chains.
188
189 @param[in,out] Dev The VNET_DEV driver instance about to enter the
190 EfiSimpleNetworkInitialized state.
191
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.
196 */
197
198 STATIC
199 EFI_STATUS
200 EFIAPI
201 VirtioNetInitRx (
202 IN OUT VNET_DEV *Dev
203 )
204 {
205 EFI_STATUS Status;
206 UINTN RxBufSize;
207 UINT16 RxAlwaysPending;
208 UINTN PktIdx;
209 UINT16 DescIdx;
210 UINT8 *RxPtr;
211
212 //
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).
217 //
218 RxBufSize = sizeof (VIRTIO_NET_REQ) +
219 (Dev->Snm.MediaHeaderSize + Dev->Snm.MaxPacketSize);
220
221 //
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.
224 //
225 RxAlwaysPending = (UINT16) MIN (Dev->RxRing.QueueSize / 2, VNET_MAX_PENDING);
226
227 Dev->RxBuf = AllocatePool (RxAlwaysPending * RxBufSize);
228 if (Dev->RxBuf == NULL) {
229 return EFI_OUT_OF_RESOURCES;
230 }
231
232 //
233 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
234 //
235 MemoryFence ();
236 Dev->RxLastUsed = *Dev->RxRing.Used.Idx;
237 ASSERT (Dev->RxLastUsed == 0);
238
239 //
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().
243 //
244 *Dev->RxRing.Avail.Flags = (UINT16) VRING_AVAIL_F_NO_INTERRUPT;
245
246 //
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
249 //
250 DescIdx = 0;
251 RxPtr = Dev->RxBuf;
252 for (PktIdx = 0; PktIdx < RxAlwaysPending; ++PktIdx) {
253 //
254 // virtio-0.9.5, 2.4.1.2 Updating the Available Ring
255 // invisible to the host until we update the Index Field
256 //
257 Dev->RxRing.Avail.Ring[PktIdx] = DescIdx;
258
259 //
260 // virtio-0.9.5, 2.4.1.1 Placing Buffers into the Descriptor Table
261 //
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;
267
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;
273 }
274
275 //
276 // virtio-0.9.5, 2.4.1.3 Updating the Index Field
277 //
278 MemoryFence ();
279 *Dev->RxRing.Avail.Idx = RxAlwaysPending;
280
281 //
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.
286 //
287 // virtio-0.9.5, 2.4.1.4 Notifying the Device
288 //
289 MemoryFence ();
290 Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrQueueNotify, VIRTIO_NET_Q_RX);
291
292 if (EFI_ERROR (Status)) {
293 VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, 0);
294 FreePool (Dev->RxBuf);
295 }
296
297 return Status;
298 }
299
300
301 /**
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.
305
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.
317
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
321 and receive buffers.
322 @retval EFI_INVALID_PARAMETER One or more of the parameters has an
323 unsupported value.
324 @retval EFI_DEVICE_ERROR The command could not be sent to the network
325 interface.
326 @retval EFI_UNSUPPORTED This function is not supported by the network
327 interface.
328
329 **/
330
331 EFI_STATUS
332 EFIAPI
333 VirtioNetInitialize (
334 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
335 IN UINTN ExtraRxBufferSize OPTIONAL,
336 IN UINTN ExtraTxBufferSize OPTIONAL
337 )
338 {
339 VNET_DEV *Dev;
340 EFI_TPL OldTpl;
341 EFI_STATUS Status;
342 UINT8 NextDevStat;
343 UINT32 Features;
344
345 if (This == NULL) {
346 return EFI_INVALID_PARAMETER;
347 }
348 if (ExtraRxBufferSize > 0 || ExtraTxBufferSize > 0) {
349 return EFI_UNSUPPORTED;
350 }
351
352 Dev = VIRTIO_NET_FROM_SNP (This);
353 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
354 if (Dev->Snm.State != EfiSimpleNetworkStarted) {
355 Status = EFI_NOT_STARTED;
356 goto InitFailed;
357 }
358
359 //
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().
364 //
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.
367 //
368 NextDevStat = VSTAT_ACK; // step 2 -- acknowledge device presence
369 Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, NextDevStat);
370 if (EFI_ERROR (Status)) {
371 goto InitFailed;
372 }
373
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)) {
377 goto DeviceFailed;
378 }
379
380 //
381 // step 4a -- retrieve features. Note that we're past validating required
382 // features in VirtioNetGetFeatures().
383 //
384 Status = VIRTIO_CFG_READ (Dev, Generic.VhdrDeviceFeatureBits, &Features);
385 if (EFI_ERROR (Status)) {
386 goto DeviceFailed;
387 }
388 ASSERT (Features & VIRTIO_NET_F_MAC);
389 ASSERT (Dev->Snm.MediaPresentSupported ==
390 !!(Features & VIRTIO_NET_F_STATUS));
391
392 //
393 // step 4b, 4c -- allocate and report virtqueues
394 //
395 Status = VirtioNetInitRing (Dev, VIRTIO_NET_Q_RX, &Dev->RxRing);
396 if (EFI_ERROR (Status)) {
397 goto DeviceFailed;
398 }
399
400 Status = VirtioNetInitRing (Dev, VIRTIO_NET_Q_TX, &Dev->TxRing);
401 if (EFI_ERROR (Status)) {
402 goto ReleaseRxRing;
403 }
404
405 //
406 // step 5 -- keep only the features we want
407 //
408 Features &= VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS;
409 Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrGuestFeatureBits, Features);
410 if (EFI_ERROR (Status)) {
411 goto ReleaseTxRing;
412 }
413
414 //
415 // step 6 -- virtio-net initialization complete
416 //
417 NextDevStat |= VSTAT_DRIVER_OK;
418 Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, NextDevStat);
419 if (EFI_ERROR (Status)) {
420 goto ReleaseTxRing;
421 }
422
423 Status = VirtioNetInitTx (Dev);
424 if (EFI_ERROR (Status)) {
425 goto AbortDevice;
426 }
427
428 //
429 // start receiving
430 //
431 Status = VirtioNetInitRx (Dev);
432 if (EFI_ERROR (Status)) {
433 goto ReleaseTxAux;
434 }
435
436 Dev->Snm.State = EfiSimpleNetworkInitialized;
437 gBS->RestoreTPL (OldTpl);
438 return EFI_SUCCESS;
439
440 ReleaseTxAux:
441 VirtioNetShutdownTx (Dev);
442
443 AbortDevice:
444 VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, 0);
445
446 ReleaseTxRing:
447 VirtioRingUninit (&Dev->TxRing);
448
449 ReleaseRxRing:
450 VirtioRingUninit (&Dev->RxRing);
451
452 DeviceFailed:
453 //
454 // restore device status invariant for the EfiSimpleNetworkStarted state
455 //
456 VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, 0);
457
458 InitFailed:
459 gBS->RestoreTPL (OldTpl);
460 return Status;
461 }