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