]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/VirtioNetDxe/SnpInitialize.c
OvmfPkg: VIRTIO_DEVICE_PROTOCOL: pass VRING object to SetQueueAddress()
[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 = Dev->VirtIo->SetQueueSel (Dev->VirtIo, Selector);
61 if (EFI_ERROR (Status)) {
62 return Status;
63 }
64 Status = Dev->VirtIo->GetQueueNumMax (Dev->VirtIo, &QueueSize);
65 if (EFI_ERROR (Status)) {
66 return Status;
67 }
68
69 //
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
72 //
73 if (QueueSize < 2) {
74 return EFI_UNSUPPORTED;
75 }
76 Status = VirtioRingInit (QueueSize, Ring);
77 if (EFI_ERROR (Status)) {
78 return Status;
79 }
80
81 //
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.
84 //
85 Status = Dev->VirtIo->SetQueueNum (Dev->VirtIo, QueueSize);
86 if (EFI_ERROR (Status)) {
87 goto ReleaseQueue;
88 }
89
90 Status = Dev->VirtIo->SetQueueAlign (Dev->VirtIo, EFI_PAGE_SIZE);
91 if (EFI_ERROR (Status)) {
92 goto ReleaseQueue;
93 }
94
95 //
96 // step 4c -- report GPFN (guest-physical frame number) of queue
97 //
98 Status = Dev->VirtIo->SetQueueAddress (Dev->VirtIo, Ring);
99 if (EFI_ERROR (Status)) {
100 goto ReleaseQueue;
101 }
102
103 return EFI_SUCCESS;
104
105 ReleaseQueue:
106 VirtioRingUninit (Ring);
107
108 return Status;
109 }
110
111
112 /**
113 Set up static scaffolding for the VirtioNetTransmit() and
114 VirtioNetGetStatus() SNP methods.
115
116 This function may only be called by VirtioNetInitialize().
117
118 The structures laid out and resources configured include:
119 - fully populate the TX queue with a static pattern of virtio descriptor
120 chains,
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
123 pending TX packets,
124 - select polling over TX interrupt.
125
126 @param[in,out] Dev The VNET_DEV driver instance about to enter the
127 EfiSimpleNetworkInitialized state.
128
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.
132 */
133
134 STATIC
135 EFI_STATUS
136 EFIAPI
137 VirtioNetInitTx (
138 IN OUT VNET_DEV *Dev
139 )
140 {
141 UINTN PktIdx;
142
143 Dev->TxMaxPending = (UINT16) MIN (Dev->TxRing.QueueSize / 2,
144 VNET_MAX_PENDING);
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;
150 }
151
152 for (PktIdx = 0; PktIdx < Dev->TxMaxPending; ++PktIdx) {
153 UINT16 DescIdx;
154
155 DescIdx = (UINT16) (2 * PktIdx);
156 Dev->TxFreeStack[PktIdx] = DescIdx;
157
158 //
159 // For each possibly pending packet, lay out the descriptor for the common
160 // (unmodified by the host) virtio-net request header.
161 //
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);
166
167 //
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.
170 //
171 Dev->TxRing.Desc[DescIdx + 1].Flags = 0;
172 }
173
174 //
175 // virtio-0.9.5, Appendix C, Packet Transmission
176 //
177 Dev->TxSharedReq.Flags = 0;
178 Dev->TxSharedReq.GsoType = VIRTIO_NET_HDR_GSO_NONE;
179
180 //
181 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
182 //
183 MemoryFence ();
184 Dev->TxLastUsed = *Dev->TxRing.Used.Idx;
185 ASSERT (Dev->TxLastUsed == 0);
186
187 //
188 // want no interrupt when a transmit completes
189 //
190 *Dev->TxRing.Avail.Flags = (UINT16) VRING_AVAIL_F_NO_INTERRUPT;
191
192 return EFI_SUCCESS;
193 }
194
195
196 /**
197 Set up static scaffolding for the VirtioNetReceive() SNP method and enable
198 live device operation.
199
200 This function may only be called as VirtioNetInitialize()'s final step.
201
202 The structures laid out and resources configured include:
203 - destination area for the host to write virtio-net request headers and
204 packet data into,
205 - select polling over RX interrupt,
206 - fully populate the RX queue with a static pattern of virtio descriptor
207 chains.
208
209 @param[in,out] Dev The VNET_DEV driver instance about to enter the
210 EfiSimpleNetworkInitialized state.
211
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.
216 */
217
218 STATIC
219 EFI_STATUS
220 EFIAPI
221 VirtioNetInitRx (
222 IN OUT VNET_DEV *Dev
223 )
224 {
225 EFI_STATUS Status;
226 UINTN RxBufSize;
227 UINT16 RxAlwaysPending;
228 UINTN PktIdx;
229 UINT16 DescIdx;
230 UINT8 *RxPtr;
231
232 //
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).
237 //
238 RxBufSize = sizeof (VIRTIO_NET_REQ) +
239 (Dev->Snm.MediaHeaderSize + Dev->Snm.MaxPacketSize);
240
241 //
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.
244 //
245 RxAlwaysPending = (UINT16) MIN (Dev->RxRing.QueueSize / 2, VNET_MAX_PENDING);
246
247 Dev->RxBuf = AllocatePool (RxAlwaysPending * RxBufSize);
248 if (Dev->RxBuf == NULL) {
249 return EFI_OUT_OF_RESOURCES;
250 }
251
252 //
253 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
254 //
255 MemoryFence ();
256 Dev->RxLastUsed = *Dev->RxRing.Used.Idx;
257 ASSERT (Dev->RxLastUsed == 0);
258
259 //
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().
263 //
264 *Dev->RxRing.Avail.Flags = (UINT16) VRING_AVAIL_F_NO_INTERRUPT;
265
266 //
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
269 //
270 DescIdx = 0;
271 RxPtr = Dev->RxBuf;
272 for (PktIdx = 0; PktIdx < RxAlwaysPending; ++PktIdx) {
273 //
274 // virtio-0.9.5, 2.4.1.2 Updating the Available Ring
275 // invisible to the host until we update the Index Field
276 //
277 Dev->RxRing.Avail.Ring[PktIdx] = DescIdx;
278
279 //
280 // virtio-0.9.5, 2.4.1.1 Placing Buffers into the Descriptor Table
281 //
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;
287
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;
293 }
294
295 //
296 // virtio-0.9.5, 2.4.1.3 Updating the Index Field
297 //
298 MemoryFence ();
299 *Dev->RxRing.Avail.Idx = RxAlwaysPending;
300
301 //
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.
306 //
307 // virtio-0.9.5, 2.4.1.4 Notifying the Device
308 //
309 MemoryFence ();
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);
314 }
315
316 return Status;
317 }
318
319
320 /**
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.
324
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.
336
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
340 and receive buffers.
341 @retval EFI_INVALID_PARAMETER One or more of the parameters has an
342 unsupported value.
343 @retval EFI_DEVICE_ERROR The command could not be sent to the network
344 interface.
345 @retval EFI_UNSUPPORTED This function is not supported by the network
346 interface.
347
348 **/
349
350 EFI_STATUS
351 EFIAPI
352 VirtioNetInitialize (
353 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
354 IN UINTN ExtraRxBufferSize OPTIONAL,
355 IN UINTN ExtraTxBufferSize OPTIONAL
356 )
357 {
358 VNET_DEV *Dev;
359 EFI_TPL OldTpl;
360 EFI_STATUS Status;
361 UINT8 NextDevStat;
362 UINT64 Features;
363
364 if (This == NULL) {
365 return EFI_INVALID_PARAMETER;
366 }
367 if (ExtraRxBufferSize > 0 || ExtraTxBufferSize > 0) {
368 return EFI_UNSUPPORTED;
369 }
370
371 Dev = VIRTIO_NET_FROM_SNP (This);
372 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
373 if (Dev->Snm.State != EfiSimpleNetworkStarted) {
374 Status = EFI_NOT_STARTED;
375 goto InitFailed;
376 }
377
378 //
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().
383 //
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.
386 //
387 NextDevStat = VSTAT_ACK; // step 2 -- acknowledge device presence
388 Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);
389 if (EFI_ERROR (Status)) {
390 goto InitFailed;
391 }
392
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)) {
396 goto DeviceFailed;
397 }
398
399 //
400 // Set Page Size - MMIO VirtIo Specific
401 //
402 Status = Dev->VirtIo->SetPageSize (Dev->VirtIo, EFI_PAGE_SIZE);
403 if (EFI_ERROR (Status)) {
404 goto DeviceFailed;
405 }
406
407 //
408 // step 4a -- retrieve features. Note that we're past validating required
409 // features in VirtioNetGetFeatures().
410 //
411 Status = Dev->VirtIo->GetDeviceFeatures (Dev->VirtIo, &Features);
412 if (EFI_ERROR (Status)) {
413 goto DeviceFailed;
414 }
415
416 ASSERT (Features & VIRTIO_NET_F_MAC);
417 ASSERT (Dev->Snm.MediaPresentSupported ==
418 !!(Features & VIRTIO_NET_F_STATUS));
419
420 //
421 // step 4b, 4c -- allocate and report virtqueues
422 //
423 Status = VirtioNetInitRing (Dev, VIRTIO_NET_Q_RX, &Dev->RxRing);
424 if (EFI_ERROR (Status)) {
425 goto DeviceFailed;
426 }
427
428 Status = VirtioNetInitRing (Dev, VIRTIO_NET_Q_TX, &Dev->TxRing);
429 if (EFI_ERROR (Status)) {
430 goto ReleaseRxRing;
431 }
432
433 //
434 // step 5 -- keep only the features we want
435 //
436 Features &= VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS;
437 Status = Dev->VirtIo->SetGuestFeatures (Dev->VirtIo, Features);
438 if (EFI_ERROR (Status)) {
439 goto ReleaseTxRing;
440 }
441
442 //
443 // step 6 -- virtio-net initialization complete
444 //
445 NextDevStat |= VSTAT_DRIVER_OK;
446 Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);
447 if (EFI_ERROR (Status)) {
448 goto ReleaseTxRing;
449 }
450
451 Status = VirtioNetInitTx (Dev);
452 if (EFI_ERROR (Status)) {
453 goto AbortDevice;
454 }
455
456 //
457 // start receiving
458 //
459 Status = VirtioNetInitRx (Dev);
460 if (EFI_ERROR (Status)) {
461 goto ReleaseTxAux;
462 }
463
464 Dev->Snm.State = EfiSimpleNetworkInitialized;
465 gBS->RestoreTPL (OldTpl);
466 return EFI_SUCCESS;
467
468 ReleaseTxAux:
469 VirtioNetShutdownTx (Dev);
470
471 AbortDevice:
472 Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);
473
474 ReleaseTxRing:
475 VirtioRingUninit (&Dev->TxRing);
476
477 ReleaseRxRing:
478 VirtioRingUninit (&Dev->RxRing);
479
480 DeviceFailed:
481 //
482 // restore device status invariant for the EfiSimpleNetworkStarted state
483 //
484 Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);
485
486 InitFailed:
487 gBS->RestoreTPL (OldTpl);
488 return Status;
489 }