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