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