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