OvmfPkg: VirtioScsiDxe: adapt feature negotiation to virtio-1.0
[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
141 UINTN PktIdx;\r
142\r
9f3acbb5
LE
143 Dev->TxMaxPending = (UINT16) MIN (Dev->TxRing.QueueSize / 2,\r
144 VNET_MAX_PENDING);\r
12384f9b
LE
145 Dev->TxCurPending = 0;\r
146 Dev->TxFreeStack = AllocatePool (Dev->TxMaxPending *\r
147 sizeof *Dev->TxFreeStack);\r
148 if (Dev->TxFreeStack == NULL) {\r
149 return EFI_OUT_OF_RESOURCES;\r
150 }\r
151\r
152 for (PktIdx = 0; PktIdx < Dev->TxMaxPending; ++PktIdx) {\r
153 UINT16 DescIdx;\r
154\r
155 DescIdx = (UINT16) (2 * PktIdx);\r
156 Dev->TxFreeStack[PktIdx] = DescIdx;\r
157\r
158 //\r
159 // For each possibly pending packet, lay out the descriptor for the common\r
160 // (unmodified by the host) virtio-net request header.\r
161 //\r
162 Dev->TxRing.Desc[DescIdx].Addr = (UINTN) &Dev->TxSharedReq;\r
163 Dev->TxRing.Desc[DescIdx].Len = sizeof Dev->TxSharedReq;\r
164 Dev->TxRing.Desc[DescIdx].Flags = VRING_DESC_F_NEXT;\r
9f3acbb5 165 Dev->TxRing.Desc[DescIdx].Next = (UINT16) (DescIdx + 1);\r
12384f9b
LE
166\r
167 //\r
168 // The second descriptor of each pending TX packet is updated on the fly,\r
169 // but it always terminates the descriptor chain of the packet.\r
170 //\r
171 Dev->TxRing.Desc[DescIdx + 1].Flags = 0;\r
172 }\r
173\r
174 //\r
175 // virtio-0.9.5, Appendix C, Packet Transmission\r
176 //\r
177 Dev->TxSharedReq.Flags = 0;\r
178 Dev->TxSharedReq.GsoType = VIRTIO_NET_HDR_GSO_NONE;\r
179\r
180 //\r
181 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device\r
182 //\r
183 MemoryFence ();\r
184 Dev->TxLastUsed = *Dev->TxRing.Used.Idx;\r
185 ASSERT (Dev->TxLastUsed == 0);\r
186\r
187 //\r
188 // want no interrupt when a transmit completes\r
189 //\r
190 *Dev->TxRing.Avail.Flags = (UINT16) VRING_AVAIL_F_NO_INTERRUPT;\r
191\r
192 return EFI_SUCCESS;\r
193}\r
194\r
195\r
196/**\r
197 Set up static scaffolding for the VirtioNetReceive() SNP method and enable\r
198 live device operation.\r
199\r
200 This function may only be called as VirtioNetInitialize()'s final step.\r
201\r
202 The structures laid out and resources configured include:\r
203 - destination area for the host to write virtio-net request headers and\r
204 packet data into,\r
205 - select polling over RX interrupt,\r
206 - fully populate the RX queue with a static pattern of virtio descriptor\r
207 chains.\r
208\r
209 @param[in,out] Dev The VNET_DEV driver instance about to enter the\r
210 EfiSimpleNetworkInitialized state.\r
211\r
212 @retval EFI_OUT_OF_RESOURCES Failed to allocate RX destination area.\r
213 @return Status codes from VIRTIO_CFG_WRITE().\r
214 @retval EFI_SUCCESS RX setup successful. The device is live and may\r
215 already be writing to the receive area.\r
216*/\r
217\r
218STATIC\r
219EFI_STATUS\r
220EFIAPI\r
221VirtioNetInitRx (\r
222 IN OUT VNET_DEV *Dev\r
223 )\r
224{\r
225 EFI_STATUS Status;\r
226 UINTN RxBufSize;\r
227 UINT16 RxAlwaysPending;\r
228 UINTN PktIdx;\r
229 UINT16 DescIdx;\r
230 UINT8 *RxPtr;\r
231\r
232 //\r
233 // For each incoming packet we must supply two descriptors:\r
234 // - the recipient for the virtio-net request header, plus\r
235 // - the recipient for the network data (which consists of Ethernet header\r
236 // and Ethernet payload).\r
237 //\r
238 RxBufSize = sizeof (VIRTIO_NET_REQ) +\r
239 (Dev->Snm.MediaHeaderSize + Dev->Snm.MaxPacketSize);\r
240\r
241 //\r
242 // Limit the number of pending RX packets if the queue is big. The division\r
243 // by two is due to the above "two descriptors per packet" trait.\r
244 //\r
9f3acbb5 245 RxAlwaysPending = (UINT16) MIN (Dev->RxRing.QueueSize / 2, VNET_MAX_PENDING);\r
12384f9b
LE
246\r
247 Dev->RxBuf = AllocatePool (RxAlwaysPending * RxBufSize);\r
248 if (Dev->RxBuf == NULL) {\r
249 return EFI_OUT_OF_RESOURCES;\r
250 }\r
251\r
252 //\r
253 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device\r
254 //\r
255 MemoryFence ();\r
256 Dev->RxLastUsed = *Dev->RxRing.Used.Idx;\r
257 ASSERT (Dev->RxLastUsed == 0);\r
258\r
259 //\r
260 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device:\r
261 // the host should not send interrupts, we'll poll in VirtioNetReceive()\r
262 // and VirtioNetIsPacketAvailable().\r
263 //\r
264 *Dev->RxRing.Avail.Flags = (UINT16) VRING_AVAIL_F_NO_INTERRUPT;\r
265\r
266 //\r
267 // now set up a separate, two-part descriptor chain for each RX packet, and\r
268 // link each chain into (from) the available ring as well\r
269 //\r
270 DescIdx = 0;\r
271 RxPtr = Dev->RxBuf;\r
272 for (PktIdx = 0; PktIdx < RxAlwaysPending; ++PktIdx) {\r
273 //\r
274 // virtio-0.9.5, 2.4.1.2 Updating the Available Ring\r
275 // invisible to the host until we update the Index Field\r
276 //\r
277 Dev->RxRing.Avail.Ring[PktIdx] = DescIdx;\r
278\r
279 //\r
280 // virtio-0.9.5, 2.4.1.1 Placing Buffers into the Descriptor Table\r
281 //\r
282 Dev->RxRing.Desc[DescIdx].Addr = (UINTN) RxPtr;\r
283 Dev->RxRing.Desc[DescIdx].Len = sizeof (VIRTIO_NET_REQ);\r
284 Dev->RxRing.Desc[DescIdx].Flags = VRING_DESC_F_WRITE | VRING_DESC_F_NEXT;\r
9f3acbb5 285 Dev->RxRing.Desc[DescIdx].Next = (UINT16) (DescIdx + 1);\r
12384f9b
LE
286 RxPtr += Dev->RxRing.Desc[DescIdx++].Len;\r
287\r
288 Dev->RxRing.Desc[DescIdx].Addr = (UINTN) RxPtr;\r
9f3acbb5
LE
289 Dev->RxRing.Desc[DescIdx].Len = (UINT32) (RxBufSize -\r
290 sizeof (VIRTIO_NET_REQ));\r
12384f9b
LE
291 Dev->RxRing.Desc[DescIdx].Flags = VRING_DESC_F_WRITE;\r
292 RxPtr += Dev->RxRing.Desc[DescIdx++].Len;\r
293 }\r
294\r
295 //\r
296 // virtio-0.9.5, 2.4.1.3 Updating the Index Field\r
297 //\r
298 MemoryFence ();\r
299 *Dev->RxRing.Avail.Idx = RxAlwaysPending;\r
300\r
301 //\r
302 // At this point reception may already be running. In order to make it sure,\r
303 // kick the hypervisor. If we fail to kick it, we must first abort reception\r
304 // before tearing down anything, because reception may have been already\r
305 // running even without the kick.\r
306 //\r
307 // virtio-0.9.5, 2.4.1.4 Notifying the Device\r
308 //\r
309 MemoryFence ();\r
56f65ed8 310 Status = Dev->VirtIo->SetQueueNotify (Dev->VirtIo, VIRTIO_NET_Q_RX);\r
12384f9b 311 if (EFI_ERROR (Status)) {\r
56f65ed8 312 Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);\r
12384f9b
LE
313 FreePool (Dev->RxBuf);\r
314 }\r
315\r
316 return Status;\r
317}\r
318\r
319\r
320/**\r
321 Resets a network adapter and allocates the transmit and receive buffers\r
322 required by the network interface; optionally, also requests allocation of\r
323 additional transmit and receive buffers.\r
324\r
325 @param This The protocol instance pointer.\r
326 @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer\r
327 space that the driver should allocate for the\r
328 network interface. Some network interfaces will not\r
329 be able to use the extra buffer, and the caller\r
330 will not know if it is actually being used.\r
331 @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer\r
332 space that the driver should allocate for the\r
333 network interface. Some network interfaces will not\r
334 be able to use the extra buffer, and the caller\r
335 will not know if it is actually being used.\r
336\r
337 @retval EFI_SUCCESS The network interface was initialized.\r
338 @retval EFI_NOT_STARTED The network interface has not been started.\r
339 @retval EFI_OUT_OF_RESOURCES There was not enough memory for the transmit\r
340 and receive buffers.\r
341 @retval EFI_INVALID_PARAMETER One or more of the parameters has an\r
342 unsupported value.\r
343 @retval EFI_DEVICE_ERROR The command could not be sent to the network\r
344 interface.\r
345 @retval EFI_UNSUPPORTED This function is not supported by the network\r
346 interface.\r
347\r
348**/\r
349\r
350EFI_STATUS\r
351EFIAPI\r
352VirtioNetInitialize (\r
353 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,\r
354 IN UINTN ExtraRxBufferSize OPTIONAL,\r
355 IN UINTN ExtraTxBufferSize OPTIONAL\r
356 )\r
357{\r
358 VNET_DEV *Dev;\r
359 EFI_TPL OldTpl;\r
360 EFI_STATUS Status;\r
361 UINT8 NextDevStat;\r
bc8fde6f 362 UINT64 Features;\r
12384f9b
LE
363\r
364 if (This == NULL) {\r
365 return EFI_INVALID_PARAMETER;\r
366 }\r
367 if (ExtraRxBufferSize > 0 || ExtraTxBufferSize > 0) {\r
368 return EFI_UNSUPPORTED;\r
369 }\r
370\r
371 Dev = VIRTIO_NET_FROM_SNP (This);\r
372 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
373 if (Dev->Snm.State != EfiSimpleNetworkStarted) {\r
374 Status = EFI_NOT_STARTED;\r
375 goto InitFailed;\r
376 }\r
377\r
378 //\r
379 // In the EfiSimpleNetworkStarted state the virtio-net device has status\r
380 // value 0 (= reset) -- see the state diagram, the full call chain to\r
381 // the end of VirtioNetGetFeatures() (considering we're here now),\r
382 // the DeviceFailed label below, and VirtioNetShutdown().\r
383 //\r
384 // Accordingly, the below is a subsequence of the steps found in the\r
385 // virtio-0.9.5 spec, 2.2.1 Device Initialization Sequence.\r
386 //\r
387 NextDevStat = VSTAT_ACK; // step 2 -- acknowledge device presence\r
56f65ed8 388 Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
12384f9b
LE
389 if (EFI_ERROR (Status)) {\r
390 goto InitFailed;\r
391 }\r
392\r
393 NextDevStat |= VSTAT_DRIVER; // step 3 -- we know how to drive it\r
56f65ed8
OM
394 Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
395 if (EFI_ERROR (Status)) {\r
396 goto DeviceFailed;\r
397 }\r
398\r
399 //\r
400 // Set Page Size - MMIO VirtIo Specific\r
401 //\r
402 Status = Dev->VirtIo->SetPageSize (Dev->VirtIo, EFI_PAGE_SIZE);\r
12384f9b
LE
403 if (EFI_ERROR (Status)) {\r
404 goto DeviceFailed;\r
405 }\r
406\r
407 //\r
408 // step 4a -- retrieve features. Note that we're past validating required\r
409 // features in VirtioNetGetFeatures().\r
410 //\r
56f65ed8 411 Status = Dev->VirtIo->GetDeviceFeatures (Dev->VirtIo, &Features);\r
12384f9b
LE
412 if (EFI_ERROR (Status)) {\r
413 goto DeviceFailed;\r
414 }\r
56f65ed8 415\r
12384f9b
LE
416 ASSERT (Features & VIRTIO_NET_F_MAC);\r
417 ASSERT (Dev->Snm.MediaPresentSupported ==\r
418 !!(Features & VIRTIO_NET_F_STATUS));\r
419\r
33c6b934
LE
420 Features &= VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS | VIRTIO_F_VERSION_1;\r
421\r
422 //\r
423 // In virtio-1.0, feature negotiation is expected to complete before queue\r
424 // discovery, and the device can also reject the selected set of features.\r
425 //\r
426 if (Dev->VirtIo->Revision >= VIRTIO_SPEC_REVISION (1, 0, 0)) {\r
427 Status = Virtio10WriteFeatures (Dev->VirtIo, Features, &NextDevStat);\r
428 if (EFI_ERROR (Status)) {\r
429 goto DeviceFailed;\r
430 }\r
431 }\r
432\r
12384f9b
LE
433 //\r
434 // step 4b, 4c -- allocate and report virtqueues\r
435 //\r
436 Status = VirtioNetInitRing (Dev, VIRTIO_NET_Q_RX, &Dev->RxRing);\r
437 if (EFI_ERROR (Status)) {\r
438 goto DeviceFailed;\r
439 }\r
440\r
441 Status = VirtioNetInitRing (Dev, VIRTIO_NET_Q_TX, &Dev->TxRing);\r
442 if (EFI_ERROR (Status)) {\r
443 goto ReleaseRxRing;\r
444 }\r
445\r
446 //\r
447 // step 5 -- keep only the features we want\r
448 //\r
33c6b934
LE
449 if (Dev->VirtIo->Revision < VIRTIO_SPEC_REVISION (1, 0, 0)) {\r
450 Features &= ~(UINT64)VIRTIO_F_VERSION_1;\r
451 Status = Dev->VirtIo->SetGuestFeatures (Dev->VirtIo, Features);\r
452 if (EFI_ERROR (Status)) {\r
453 goto ReleaseTxRing;\r
454 }\r
12384f9b
LE
455 }\r
456\r
457 //\r
458 // step 6 -- virtio-net initialization complete\r
459 //\r
460 NextDevStat |= VSTAT_DRIVER_OK;\r
56f65ed8 461 Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
12384f9b
LE
462 if (EFI_ERROR (Status)) {\r
463 goto ReleaseTxRing;\r
464 }\r
465\r
466 Status = VirtioNetInitTx (Dev);\r
467 if (EFI_ERROR (Status)) {\r
468 goto AbortDevice;\r
469 }\r
470\r
471 //\r
472 // start receiving\r
473 //\r
474 Status = VirtioNetInitRx (Dev);\r
475 if (EFI_ERROR (Status)) {\r
476 goto ReleaseTxAux;\r
477 }\r
478\r
479 Dev->Snm.State = EfiSimpleNetworkInitialized;\r
480 gBS->RestoreTPL (OldTpl);\r
481 return EFI_SUCCESS;\r
482\r
483ReleaseTxAux:\r
484 VirtioNetShutdownTx (Dev);\r
485\r
486AbortDevice:\r
56f65ed8 487 Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);\r
12384f9b
LE
488\r
489ReleaseTxRing:\r
490 VirtioRingUninit (&Dev->TxRing);\r
491\r
492ReleaseRxRing:\r
493 VirtioRingUninit (&Dev->RxRing);\r
494\r
495DeviceFailed:\r
496 //\r
497 // restore device status invariant for the EfiSimpleNetworkStarted state\r
498 //\r
56f65ed8 499 Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);\r
12384f9b
LE
500\r
501InitFailed:\r
502 gBS->RestoreTPL (OldTpl);\r
503 return Status;\r
504}\r