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