]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - OvmfPkg/VirtioNetDxe/SnpInitialize.c
OvmfPkg/Virtio: take RingBaseShift in SetQueueAddress()
[mirror_edk2.git] / OvmfPkg / VirtioNetDxe / SnpInitialize.c
... / ...
CommitLineData
1/** @file\r
2\r
3 Implementation of the SNP.Initialize() function and its private helpers if\r
4 any.\r
5\r
6 Copyright (C) 2013, Red Hat, Inc.\r
7 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
8 Copyright (c) 2017, AMD Inc, All rights reserved.<BR>\r
9\r
10 This program and the accompanying materials are licensed and made available\r
11 under the terms and conditions of the BSD License which accompanies this\r
12 distribution. The full text of the license may be found at\r
13 http://opensource.org/licenses/bsd-license.php\r
14\r
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
16 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
17\r
18**/\r
19\r
20#include <Library/BaseLib.h>\r
21#include <Library/MemoryAllocationLib.h>\r
22#include <Library/UefiBootServicesTableLib.h>\r
23\r
24#include "VirtioNet.h"\r
25\r
26/**\r
27 Initialize a virtio ring for a specific transfer direction of the virtio-net\r
28 device.\r
29\r
30 This function may only be called by VirtioNetInitialize().\r
31\r
32 @param[in,out] Dev The VNET_DEV driver instance about to enter the\r
33 EfiSimpleNetworkInitialized state.\r
34 @param[in] Selector Identifies the transfer direction (virtio queue) of\r
35 the network device.\r
36 @param[out] Ring The virtio-ring inside the VNET_DEV structure,\r
37 corresponding to Selector.\r
38\r
39 @retval EFI_UNSUPPORTED The queue size reported by the virtio-net device is\r
40 too small.\r
41 @return Status codes from VIRTIO_CFG_WRITE(),\r
42 VIRTIO_CFG_READ() and VirtioRingInit().\r
43 @retval EFI_SUCCESS Ring initialized.\r
44*/\r
45\r
46STATIC\r
47EFI_STATUS\r
48EFIAPI\r
49VirtioNetInitRing (\r
50 IN OUT VNET_DEV *Dev,\r
51 IN UINT16 Selector,\r
52 OUT VRING *Ring\r
53 )\r
54{\r
55 EFI_STATUS Status;\r
56 UINT16 QueueSize;\r
57\r
58 //\r
59 // step 4b -- allocate selected queue\r
60 //\r
61 Status = Dev->VirtIo->SetQueueSel (Dev->VirtIo, Selector);\r
62 if (EFI_ERROR (Status)) {\r
63 return Status;\r
64 }\r
65 Status = Dev->VirtIo->GetQueueNumMax (Dev->VirtIo, &QueueSize);\r
66 if (EFI_ERROR (Status)) {\r
67 return Status;\r
68 }\r
69\r
70 //\r
71 // For each packet (RX and TX alike), we need two descriptors:\r
72 // one for the virtio-net request header, and another one for the data\r
73 //\r
74 if (QueueSize < 2) {\r
75 return EFI_UNSUPPORTED;\r
76 }\r
77 Status = VirtioRingInit (Dev->VirtIo, QueueSize, Ring);\r
78 if (EFI_ERROR (Status)) {\r
79 return Status;\r
80 }\r
81\r
82 //\r
83 // Additional steps for MMIO: align the queue appropriately, and set the\r
84 // size. If anything fails from here on, we must release the ring resources.\r
85 //\r
86 Status = Dev->VirtIo->SetQueueNum (Dev->VirtIo, QueueSize);\r
87 if (EFI_ERROR (Status)) {\r
88 goto ReleaseQueue;\r
89 }\r
90\r
91 Status = Dev->VirtIo->SetQueueAlign (Dev->VirtIo, EFI_PAGE_SIZE);\r
92 if (EFI_ERROR (Status)) {\r
93 goto ReleaseQueue;\r
94 }\r
95\r
96 //\r
97 // step 4c -- report GPFN (guest-physical frame number) of queue\r
98 //\r
99 Status = Dev->VirtIo->SetQueueAddress (Dev->VirtIo, Ring, 0);\r
100 if (EFI_ERROR (Status)) {\r
101 goto ReleaseQueue;\r
102 }\r
103\r
104 return EFI_SUCCESS;\r
105\r
106ReleaseQueue:\r
107 VirtioRingUninit (Dev->VirtIo, Ring);\r
108\r
109 return Status;\r
110}\r
111\r
112\r
113/**\r
114 Set up static scaffolding for the VirtioNetTransmit() and\r
115 VirtioNetGetStatus() SNP methods.\r
116\r
117 This function may only be called by VirtioNetInitialize().\r
118\r
119 The structures laid out and resources configured include:\r
120 - fully populate the TX queue with a static pattern of virtio descriptor\r
121 chains,\r
122 - tracking of heads of free descriptor chains from the above,\r
123 - one common virtio-net request header (never modified by the host) for all\r
124 pending TX packets,\r
125 - select polling over TX interrupt.\r
126\r
127 @param[in,out] Dev The VNET_DEV driver instance about to enter the\r
128 EfiSimpleNetworkInitialized state.\r
129\r
130 @retval EFI_OUT_OF_RESOURCES Failed to allocate the stack to track the heads\r
131 of free descriptor chains.\r
132 @retval EFI_SUCCESS TX setup successful.\r
133*/\r
134\r
135STATIC\r
136EFI_STATUS\r
137EFIAPI\r
138VirtioNetInitTx (\r
139 IN OUT VNET_DEV *Dev\r
140 )\r
141{\r
142 UINTN TxSharedReqSize;\r
143 UINTN PktIdx;\r
144\r
145 Dev->TxMaxPending = (UINT16) MIN (Dev->TxRing.QueueSize / 2,\r
146 VNET_MAX_PENDING);\r
147 Dev->TxCurPending = 0;\r
148 Dev->TxFreeStack = AllocatePool (Dev->TxMaxPending *\r
149 sizeof *Dev->TxFreeStack);\r
150 if (Dev->TxFreeStack == NULL) {\r
151 return EFI_OUT_OF_RESOURCES;\r
152 }\r
153\r
154 //\r
155 // In VirtIo 1.0, the NumBuffers field is mandatory. In 0.9.5, it depends on\r
156 // VIRTIO_NET_F_MRG_RXBUF, which we never negotiate.\r
157 //\r
158 TxSharedReqSize = (Dev->VirtIo->Revision < VIRTIO_SPEC_REVISION (1, 0, 0)) ?\r
159 sizeof Dev->TxSharedReq.V0_9_5 :\r
160 sizeof Dev->TxSharedReq;\r
161\r
162 for (PktIdx = 0; PktIdx < Dev->TxMaxPending; ++PktIdx) {\r
163 UINT16 DescIdx;\r
164\r
165 DescIdx = (UINT16) (2 * PktIdx);\r
166 Dev->TxFreeStack[PktIdx] = DescIdx;\r
167\r
168 //\r
169 // For each possibly pending packet, lay out the descriptor for the common\r
170 // (unmodified by the host) virtio-net request header.\r
171 //\r
172 Dev->TxRing.Desc[DescIdx].Addr = (UINTN) &Dev->TxSharedReq;\r
173 Dev->TxRing.Desc[DescIdx].Len = (UINT32) TxSharedReqSize;\r
174 Dev->TxRing.Desc[DescIdx].Flags = VRING_DESC_F_NEXT;\r
175 Dev->TxRing.Desc[DescIdx].Next = (UINT16) (DescIdx + 1);\r
176\r
177 //\r
178 // The second descriptor of each pending TX packet is updated on the fly,\r
179 // but it always terminates the descriptor chain of the packet.\r
180 //\r
181 Dev->TxRing.Desc[DescIdx + 1].Flags = 0;\r
182 }\r
183\r
184 //\r
185 // virtio-0.9.5, Appendix C, Packet Transmission\r
186 //\r
187 Dev->TxSharedReq.V0_9_5.Flags = 0;\r
188 Dev->TxSharedReq.V0_9_5.GsoType = VIRTIO_NET_HDR_GSO_NONE;\r
189\r
190 //\r
191 // For VirtIo 1.0 only -- the field exists, but it is unused\r
192 //\r
193 Dev->TxSharedReq.NumBuffers = 0;\r
194\r
195 //\r
196 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device\r
197 //\r
198 MemoryFence ();\r
199 Dev->TxLastUsed = *Dev->TxRing.Used.Idx;\r
200 ASSERT (Dev->TxLastUsed == 0);\r
201\r
202 //\r
203 // want no interrupt when a transmit completes\r
204 //\r
205 *Dev->TxRing.Avail.Flags = (UINT16) VRING_AVAIL_F_NO_INTERRUPT;\r
206\r
207 return EFI_SUCCESS;\r
208}\r
209\r
210\r
211/**\r
212 Set up static scaffolding for the VirtioNetReceive() SNP method and enable\r
213 live device operation.\r
214\r
215 This function may only be called as VirtioNetInitialize()'s final step.\r
216\r
217 The structures laid out and resources configured include:\r
218 - destination area for the host to write virtio-net request headers and\r
219 packet data into,\r
220 - select polling over RX interrupt,\r
221 - fully populate the RX queue with a static pattern of virtio descriptor\r
222 chains.\r
223\r
224 @param[in,out] Dev The VNET_DEV driver instance about to enter the\r
225 EfiSimpleNetworkInitialized state.\r
226\r
227 @retval EFI_OUT_OF_RESOURCES Failed to allocate RX destination area.\r
228 @return Status codes from VIRTIO_CFG_WRITE().\r
229 @retval EFI_SUCCESS RX setup successful. The device is live and may\r
230 already be writing to the receive area.\r
231*/\r
232\r
233STATIC\r
234EFI_STATUS\r
235EFIAPI\r
236VirtioNetInitRx (\r
237 IN OUT VNET_DEV *Dev\r
238 )\r
239{\r
240 EFI_STATUS Status;\r
241 UINTN VirtioNetReqSize;\r
242 UINTN RxBufSize;\r
243 UINT16 RxAlwaysPending;\r
244 UINTN PktIdx;\r
245 UINT16 DescIdx;\r
246 UINT8 *RxPtr;\r
247\r
248 //\r
249 // In VirtIo 1.0, the NumBuffers field is mandatory. In 0.9.5, it depends on\r
250 // VIRTIO_NET_F_MRG_RXBUF, which we never negotiate.\r
251 //\r
252 VirtioNetReqSize = (Dev->VirtIo->Revision < VIRTIO_SPEC_REVISION (1, 0, 0)) ?\r
253 sizeof (VIRTIO_NET_REQ) :\r
254 sizeof (VIRTIO_1_0_NET_REQ);\r
255\r
256 //\r
257 // For each incoming packet we must supply two descriptors:\r
258 // - the recipient for the virtio-net request header, plus\r
259 // - the recipient for the network data (which consists of Ethernet header\r
260 // and Ethernet payload).\r
261 //\r
262 RxBufSize = VirtioNetReqSize +\r
263 (Dev->Snm.MediaHeaderSize + Dev->Snm.MaxPacketSize);\r
264\r
265 //\r
266 // Limit the number of pending RX packets if the queue is big. The division\r
267 // by two is due to the above "two descriptors per packet" trait.\r
268 //\r
269 RxAlwaysPending = (UINT16) MIN (Dev->RxRing.QueueSize / 2, VNET_MAX_PENDING);\r
270\r
271 Dev->RxBuf = AllocatePool (RxAlwaysPending * RxBufSize);\r
272 if (Dev->RxBuf == NULL) {\r
273 return EFI_OUT_OF_RESOURCES;\r
274 }\r
275\r
276 //\r
277 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device\r
278 //\r
279 MemoryFence ();\r
280 Dev->RxLastUsed = *Dev->RxRing.Used.Idx;\r
281 ASSERT (Dev->RxLastUsed == 0);\r
282\r
283 //\r
284 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device:\r
285 // the host should not send interrupts, we'll poll in VirtioNetReceive()\r
286 // and VirtioNetIsPacketAvailable().\r
287 //\r
288 *Dev->RxRing.Avail.Flags = (UINT16) VRING_AVAIL_F_NO_INTERRUPT;\r
289\r
290 //\r
291 // now set up a separate, two-part descriptor chain for each RX packet, and\r
292 // link each chain into (from) the available ring as well\r
293 //\r
294 DescIdx = 0;\r
295 RxPtr = Dev->RxBuf;\r
296 for (PktIdx = 0; PktIdx < RxAlwaysPending; ++PktIdx) {\r
297 //\r
298 // virtio-0.9.5, 2.4.1.2 Updating the Available Ring\r
299 // invisible to the host until we update the Index Field\r
300 //\r
301 Dev->RxRing.Avail.Ring[PktIdx] = DescIdx;\r
302\r
303 //\r
304 // virtio-0.9.5, 2.4.1.1 Placing Buffers into the Descriptor Table\r
305 //\r
306 Dev->RxRing.Desc[DescIdx].Addr = (UINTN) RxPtr;\r
307 Dev->RxRing.Desc[DescIdx].Len = (UINT32) VirtioNetReqSize;\r
308 Dev->RxRing.Desc[DescIdx].Flags = VRING_DESC_F_WRITE | VRING_DESC_F_NEXT;\r
309 Dev->RxRing.Desc[DescIdx].Next = (UINT16) (DescIdx + 1);\r
310 RxPtr += Dev->RxRing.Desc[DescIdx++].Len;\r
311\r
312 Dev->RxRing.Desc[DescIdx].Addr = (UINTN) RxPtr;\r
313 Dev->RxRing.Desc[DescIdx].Len = (UINT32) (RxBufSize - VirtioNetReqSize);\r
314 Dev->RxRing.Desc[DescIdx].Flags = VRING_DESC_F_WRITE;\r
315 RxPtr += Dev->RxRing.Desc[DescIdx++].Len;\r
316 }\r
317\r
318 //\r
319 // virtio-0.9.5, 2.4.1.3 Updating the Index Field\r
320 //\r
321 MemoryFence ();\r
322 *Dev->RxRing.Avail.Idx = RxAlwaysPending;\r
323\r
324 //\r
325 // At this point reception may already be running. In order to make it sure,\r
326 // kick the hypervisor. If we fail to kick it, we must first abort reception\r
327 // before tearing down anything, because reception may have been already\r
328 // running even without the kick.\r
329 //\r
330 // virtio-0.9.5, 2.4.1.4 Notifying the Device\r
331 //\r
332 MemoryFence ();\r
333 Status = Dev->VirtIo->SetQueueNotify (Dev->VirtIo, VIRTIO_NET_Q_RX);\r
334 if (EFI_ERROR (Status)) {\r
335 Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);\r
336 FreePool (Dev->RxBuf);\r
337 }\r
338\r
339 return Status;\r
340}\r
341\r
342\r
343/**\r
344 Resets a network adapter and allocates the transmit and receive buffers\r
345 required by the network interface; optionally, also requests allocation of\r
346 additional transmit and receive buffers.\r
347\r
348 @param This The protocol instance pointer.\r
349 @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer\r
350 space that the driver should allocate for the\r
351 network interface. Some network interfaces will not\r
352 be able to use the extra buffer, and the caller\r
353 will not know if it is actually being used.\r
354 @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer\r
355 space that the driver should allocate for the\r
356 network interface. Some network interfaces will not\r
357 be able to use the extra buffer, and the caller\r
358 will not know if it is actually being used.\r
359\r
360 @retval EFI_SUCCESS The network interface was initialized.\r
361 @retval EFI_NOT_STARTED The network interface has not been started.\r
362 @retval EFI_OUT_OF_RESOURCES There was not enough memory for the transmit\r
363 and receive buffers.\r
364 @retval EFI_INVALID_PARAMETER One or more of the parameters has an\r
365 unsupported value.\r
366 @retval EFI_DEVICE_ERROR The command could not be sent to the network\r
367 interface.\r
368 @retval EFI_UNSUPPORTED This function is not supported by the network\r
369 interface.\r
370\r
371**/\r
372\r
373EFI_STATUS\r
374EFIAPI\r
375VirtioNetInitialize (\r
376 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,\r
377 IN UINTN ExtraRxBufferSize OPTIONAL,\r
378 IN UINTN ExtraTxBufferSize OPTIONAL\r
379 )\r
380{\r
381 VNET_DEV *Dev;\r
382 EFI_TPL OldTpl;\r
383 EFI_STATUS Status;\r
384 UINT8 NextDevStat;\r
385 UINT64 Features;\r
386\r
387 if (This == NULL) {\r
388 return EFI_INVALID_PARAMETER;\r
389 }\r
390 if (ExtraRxBufferSize > 0 || ExtraTxBufferSize > 0) {\r
391 return EFI_UNSUPPORTED;\r
392 }\r
393\r
394 Dev = VIRTIO_NET_FROM_SNP (This);\r
395 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
396 if (Dev->Snm.State != EfiSimpleNetworkStarted) {\r
397 Status = EFI_NOT_STARTED;\r
398 goto InitFailed;\r
399 }\r
400\r
401 //\r
402 // In the EfiSimpleNetworkStarted state the virtio-net device has status\r
403 // value 0 (= reset) -- see the state diagram, the full call chain to\r
404 // the end of VirtioNetGetFeatures() (considering we're here now),\r
405 // the DeviceFailed label below, and VirtioNetShutdown().\r
406 //\r
407 // Accordingly, the below is a subsequence of the steps found in the\r
408 // virtio-0.9.5 spec, 2.2.1 Device Initialization Sequence.\r
409 //\r
410 NextDevStat = VSTAT_ACK; // step 2 -- acknowledge device presence\r
411 Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
412 if (EFI_ERROR (Status)) {\r
413 goto InitFailed;\r
414 }\r
415\r
416 NextDevStat |= VSTAT_DRIVER; // step 3 -- we know how to drive it\r
417 Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
418 if (EFI_ERROR (Status)) {\r
419 goto DeviceFailed;\r
420 }\r
421\r
422 //\r
423 // Set Page Size - MMIO VirtIo Specific\r
424 //\r
425 Status = Dev->VirtIo->SetPageSize (Dev->VirtIo, EFI_PAGE_SIZE);\r
426 if (EFI_ERROR (Status)) {\r
427 goto DeviceFailed;\r
428 }\r
429\r
430 //\r
431 // step 4a -- retrieve features. Note that we're past validating required\r
432 // features in VirtioNetGetFeatures().\r
433 //\r
434 Status = Dev->VirtIo->GetDeviceFeatures (Dev->VirtIo, &Features);\r
435 if (EFI_ERROR (Status)) {\r
436 goto DeviceFailed;\r
437 }\r
438\r
439 ASSERT (Features & VIRTIO_NET_F_MAC);\r
440 ASSERT (Dev->Snm.MediaPresentSupported ==\r
441 !!(Features & VIRTIO_NET_F_STATUS));\r
442\r
443 Features &= VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS | VIRTIO_F_VERSION_1;\r
444\r
445 //\r
446 // In virtio-1.0, feature negotiation is expected to complete before queue\r
447 // discovery, and the device can also reject the selected set of features.\r
448 //\r
449 if (Dev->VirtIo->Revision >= VIRTIO_SPEC_REVISION (1, 0, 0)) {\r
450 Status = Virtio10WriteFeatures (Dev->VirtIo, Features, &NextDevStat);\r
451 if (EFI_ERROR (Status)) {\r
452 goto DeviceFailed;\r
453 }\r
454 }\r
455\r
456 //\r
457 // step 4b, 4c -- allocate and report virtqueues\r
458 //\r
459 Status = VirtioNetInitRing (Dev, VIRTIO_NET_Q_RX, &Dev->RxRing);\r
460 if (EFI_ERROR (Status)) {\r
461 goto DeviceFailed;\r
462 }\r
463\r
464 Status = VirtioNetInitRing (Dev, VIRTIO_NET_Q_TX, &Dev->TxRing);\r
465 if (EFI_ERROR (Status)) {\r
466 goto ReleaseRxRing;\r
467 }\r
468\r
469 //\r
470 // step 5 -- keep only the features we want\r
471 //\r
472 if (Dev->VirtIo->Revision < VIRTIO_SPEC_REVISION (1, 0, 0)) {\r
473 Features &= ~(UINT64)VIRTIO_F_VERSION_1;\r
474 Status = Dev->VirtIo->SetGuestFeatures (Dev->VirtIo, Features);\r
475 if (EFI_ERROR (Status)) {\r
476 goto ReleaseTxRing;\r
477 }\r
478 }\r
479\r
480 //\r
481 // step 6 -- virtio-net initialization complete\r
482 //\r
483 NextDevStat |= VSTAT_DRIVER_OK;\r
484 Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
485 if (EFI_ERROR (Status)) {\r
486 goto ReleaseTxRing;\r
487 }\r
488\r
489 Status = VirtioNetInitTx (Dev);\r
490 if (EFI_ERROR (Status)) {\r
491 goto AbortDevice;\r
492 }\r
493\r
494 //\r
495 // start receiving\r
496 //\r
497 Status = VirtioNetInitRx (Dev);\r
498 if (EFI_ERROR (Status)) {\r
499 goto ReleaseTxAux;\r
500 }\r
501\r
502 Dev->Snm.State = EfiSimpleNetworkInitialized;\r
503 gBS->RestoreTPL (OldTpl);\r
504 return EFI_SUCCESS;\r
505\r
506ReleaseTxAux:\r
507 VirtioNetShutdownTx (Dev);\r
508\r
509AbortDevice:\r
510 Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);\r
511\r
512ReleaseTxRing:\r
513 VirtioRingUninit (Dev->VirtIo, &Dev->TxRing);\r
514\r
515ReleaseRxRing:\r
516 VirtioRingUninit (Dev->VirtIo, &Dev->RxRing);\r
517\r
518DeviceFailed:\r
519 //\r
520 // restore device status invariant for the EfiSimpleNetworkStarted state\r
521 //\r
522 Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);\r
523\r
524InitFailed:\r
525 gBS->RestoreTPL (OldTpl);\r
526 return Status;\r
527}\r