1 | /** @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 | Copyright (c) 2017, AMD Inc, All rights reserved.<BR>
|
---|
9 |
|
---|
10 | This program and the accompanying materials are licensed and made available
|
---|
11 | under the terms and conditions of the BSD License which accompanies this
|
---|
12 | distribution. The full text of the license may be found at
|
---|
13 | http://opensource.org/licenses/bsd-license.php
|
---|
14 |
|
---|
15 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
|
---|
16 | WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
---|
17 |
|
---|
18 | **/
|
---|
19 |
|
---|
20 | #include <Library/BaseLib.h>
|
---|
21 | #include <Library/BaseMemoryLib.h>
|
---|
22 | #include <Library/MemoryAllocationLib.h>
|
---|
23 | #include <Library/UefiBootServicesTableLib.h>
|
---|
24 |
|
---|
25 | #include "VirtioNet.h"
|
---|
26 |
|
---|
27 | /**
|
---|
28 | Initialize a virtio ring for a specific transfer direction of the virtio-net
|
---|
29 | device.
|
---|
30 |
|
---|
31 | This function may only be called by VirtioNetInitialize().
|
---|
32 |
|
---|
33 | @param[in,out] Dev The VNET_DEV driver instance about to enter the
|
---|
34 | EfiSimpleNetworkInitialized state.
|
---|
35 | @param[in] Selector Identifies the transfer direction (virtio queue) of
|
---|
36 | the network device.
|
---|
37 | @param[out] Ring The virtio-ring inside the VNET_DEV structure,
|
---|
38 | corresponding to Selector.
|
---|
39 | @param[out] Mapping A resulting token to pass to VirtioNetUninitRing()
|
---|
40 |
|
---|
41 | @retval EFI_UNSUPPORTED The queue size reported by the virtio-net device is
|
---|
42 | too small.
|
---|
43 | @return Status codes from VIRTIO_CFG_WRITE(),
|
---|
44 | VIRTIO_CFG_READ(), VirtioRingInit() and
|
---|
45 | VirtioRingMap().
|
---|
46 | @retval EFI_SUCCESS Ring initialized.
|
---|
47 | */
|
---|
48 |
|
---|
49 | STATIC
|
---|
50 | EFI_STATUS
|
---|
51 | EFIAPI
|
---|
52 | VirtioNetInitRing (
|
---|
53 | IN OUT VNET_DEV *Dev,
|
---|
54 | IN UINT16 Selector,
|
---|
55 | OUT VRING *Ring,
|
---|
56 | OUT VOID **Mapping
|
---|
57 | )
|
---|
58 | {
|
---|
59 | EFI_STATUS Status;
|
---|
60 | UINT16 QueueSize;
|
---|
61 | UINT64 RingBaseShift;
|
---|
62 | VOID *MapInfo;
|
---|
63 |
|
---|
64 | //
|
---|
65 | // step 4b -- allocate selected queue
|
---|
66 | //
|
---|
67 | Status = Dev->VirtIo->SetQueueSel (Dev->VirtIo, Selector);
|
---|
68 | if (EFI_ERROR (Status)) {
|
---|
69 | return Status;
|
---|
70 | }
|
---|
71 | Status = Dev->VirtIo->GetQueueNumMax (Dev->VirtIo, &QueueSize);
|
---|
72 | if (EFI_ERROR (Status)) {
|
---|
73 | return Status;
|
---|
74 | }
|
---|
75 |
|
---|
76 | //
|
---|
77 | // For each packet (RX and TX alike), we need two descriptors:
|
---|
78 | // one for the virtio-net request header, and another one for the data
|
---|
79 | //
|
---|
80 | if (QueueSize < 2) {
|
---|
81 | return EFI_UNSUPPORTED;
|
---|
82 | }
|
---|
83 | Status = VirtioRingInit (Dev->VirtIo, QueueSize, Ring);
|
---|
84 | if (EFI_ERROR (Status)) {
|
---|
85 | return Status;
|
---|
86 | }
|
---|
87 |
|
---|
88 | //
|
---|
89 | // If anything fails from here on, we must release the ring resources.
|
---|
90 | //
|
---|
91 | Status = VirtioRingMap (Dev->VirtIo, Ring, &RingBaseShift, &MapInfo);
|
---|
92 | if (EFI_ERROR (Status)) {
|
---|
93 | goto ReleaseQueue;
|
---|
94 | }
|
---|
95 |
|
---|
96 | //
|
---|
97 | // Additional steps for MMIO: align the queue appropriately, and set the
|
---|
98 | // size. If anything fails from here on, we must unmap the ring resources.
|
---|
99 | //
|
---|
100 | Status = Dev->VirtIo->SetQueueNum (Dev->VirtIo, QueueSize);
|
---|
101 | if (EFI_ERROR (Status)) {
|
---|
102 | goto UnmapQueue;
|
---|
103 | }
|
---|
104 |
|
---|
105 | Status = Dev->VirtIo->SetQueueAlign (Dev->VirtIo, EFI_PAGE_SIZE);
|
---|
106 | if (EFI_ERROR (Status)) {
|
---|
107 | goto UnmapQueue;
|
---|
108 | }
|
---|
109 |
|
---|
110 | //
|
---|
111 | // step 4c -- report GPFN (guest-physical frame number) of queue
|
---|
112 | //
|
---|
113 | Status = Dev->VirtIo->SetQueueAddress (Dev->VirtIo, Ring, RingBaseShift);
|
---|
114 | if (EFI_ERROR (Status)) {
|
---|
115 | goto UnmapQueue;
|
---|
116 | }
|
---|
117 |
|
---|
118 | *Mapping = MapInfo;
|
---|
119 |
|
---|
120 | return EFI_SUCCESS;
|
---|
121 |
|
---|
122 | UnmapQueue:
|
---|
123 | Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, MapInfo);
|
---|
124 |
|
---|
125 | ReleaseQueue:
|
---|
126 | VirtioRingUninit (Dev->VirtIo, Ring);
|
---|
127 |
|
---|
128 | return Status;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | /**
|
---|
133 | Set up static scaffolding for the VirtioNetTransmit() and
|
---|
134 | VirtioNetGetStatus() SNP methods.
|
---|
135 |
|
---|
136 | This function may only be called by VirtioNetInitialize().
|
---|
137 |
|
---|
138 | The structures laid out and resources configured include:
|
---|
139 | - fully populate the TX queue with a static pattern of virtio descriptor
|
---|
140 | chains,
|
---|
141 | - tracking of heads of free descriptor chains from the above,
|
---|
142 | - one common virtio-net request header (never modified by the host) for all
|
---|
143 | pending TX packets,
|
---|
144 | - select polling over TX interrupt.
|
---|
145 |
|
---|
146 | @param[in,out] Dev The VNET_DEV driver instance about to enter the
|
---|
147 | EfiSimpleNetworkInitialized state.
|
---|
148 |
|
---|
149 | @retval EFI_OUT_OF_RESOURCES Failed to allocate the stack to track the heads
|
---|
150 | of free descriptor chains or failed to init
|
---|
151 | TxBufCollection.
|
---|
152 | @return Status codes from VIRTIO_DEVICE_PROTOCOL.
|
---|
153 | AllocateSharedPages() or
|
---|
154 | VirtioMapAllBytesInSharedBuffer()
|
---|
155 | @retval EFI_SUCCESS TX setup successful.
|
---|
156 | */
|
---|
157 |
|
---|
158 | STATIC
|
---|
159 | EFI_STATUS
|
---|
160 | EFIAPI
|
---|
161 | VirtioNetInitTx (
|
---|
162 | IN OUT VNET_DEV *Dev
|
---|
163 | )
|
---|
164 | {
|
---|
165 | UINTN TxSharedReqSize;
|
---|
166 | UINTN PktIdx;
|
---|
167 | EFI_STATUS Status;
|
---|
168 | EFI_PHYSICAL_ADDRESS DeviceAddress;
|
---|
169 | VOID *TxSharedReqBuffer;
|
---|
170 |
|
---|
171 | Dev->TxMaxPending = (UINT16) MIN (Dev->TxRing.QueueSize / 2,
|
---|
172 | VNET_MAX_PENDING);
|
---|
173 | Dev->TxCurPending = 0;
|
---|
174 | Dev->TxFreeStack = AllocatePool (Dev->TxMaxPending *
|
---|
175 | sizeof *Dev->TxFreeStack);
|
---|
176 | if (Dev->TxFreeStack == NULL) {
|
---|
177 | return EFI_OUT_OF_RESOURCES;
|
---|
178 | }
|
---|
179 |
|
---|
180 | Dev->TxBufCollection = OrderedCollectionInit (
|
---|
181 | VirtioNetTxBufMapInfoCompare,
|
---|
182 | VirtioNetTxBufDeviceAddressCompare
|
---|
183 | );
|
---|
184 | if (Dev->TxBufCollection == NULL) {
|
---|
185 | Status = EFI_OUT_OF_RESOURCES;
|
---|
186 | goto FreeTxFreeStack;
|
---|
187 | }
|
---|
188 |
|
---|
189 | //
|
---|
190 | // Allocate TxSharedReq header and map with BusMasterCommonBuffer so that it
|
---|
191 | // can be accessed equally by both processor and device.
|
---|
192 | //
|
---|
193 | Status = Dev->VirtIo->AllocateSharedPages (
|
---|
194 | Dev->VirtIo,
|
---|
195 | EFI_SIZE_TO_PAGES (sizeof *Dev->TxSharedReq),
|
---|
196 | &TxSharedReqBuffer
|
---|
197 | );
|
---|
198 | if (EFI_ERROR (Status)) {
|
---|
199 | goto UninitTxBufCollection;
|
---|
200 | }
|
---|
201 |
|
---|
202 | ZeroMem (TxSharedReqBuffer, sizeof *Dev->TxSharedReq);
|
---|
203 |
|
---|
204 | Status = VirtioMapAllBytesInSharedBuffer (
|
---|
205 | Dev->VirtIo,
|
---|
206 | VirtioOperationBusMasterCommonBuffer,
|
---|
207 | TxSharedReqBuffer,
|
---|
208 | sizeof *(Dev->TxSharedReq),
|
---|
209 | &DeviceAddress,
|
---|
210 | &Dev->TxSharedReqMap
|
---|
211 | );
|
---|
212 | if (EFI_ERROR (Status)) {
|
---|
213 | goto FreeTxSharedReqBuffer;
|
---|
214 | }
|
---|
215 |
|
---|
216 | Dev->TxSharedReq = TxSharedReqBuffer;
|
---|
217 |
|
---|
218 |
|
---|
219 | //
|
---|
220 | // In VirtIo 1.0, the NumBuffers field is mandatory. In 0.9.5, it depends on
|
---|
221 | // VIRTIO_NET_F_MRG_RXBUF, which we never negotiate.
|
---|
222 | //
|
---|
223 | TxSharedReqSize = (Dev->VirtIo->Revision < VIRTIO_SPEC_REVISION (1, 0, 0)) ?
|
---|
224 | sizeof (Dev->TxSharedReq->V0_9_5) :
|
---|
225 | sizeof *Dev->TxSharedReq;
|
---|
226 |
|
---|
227 | for (PktIdx = 0; PktIdx < Dev->TxMaxPending; ++PktIdx) {
|
---|
228 | UINT16 DescIdx;
|
---|
229 |
|
---|
230 | DescIdx = (UINT16) (2 * PktIdx);
|
---|
231 | Dev->TxFreeStack[PktIdx] = DescIdx;
|
---|
232 |
|
---|
233 | //
|
---|
234 | // For each possibly pending packet, lay out the descriptor for the common
|
---|
235 | // (unmodified by the host) virtio-net request header.
|
---|
236 | //
|
---|
237 | Dev->TxRing.Desc[DescIdx].Addr = DeviceAddress;
|
---|
238 | Dev->TxRing.Desc[DescIdx].Len = (UINT32) TxSharedReqSize;
|
---|
239 | Dev->TxRing.Desc[DescIdx].Flags = VRING_DESC_F_NEXT;
|
---|
240 | Dev->TxRing.Desc[DescIdx].Next = (UINT16) (DescIdx + 1);
|
---|
241 |
|
---|
242 | //
|
---|
243 | // The second descriptor of each pending TX packet is updated on the fly,
|
---|
244 | // but it always terminates the descriptor chain of the packet.
|
---|
245 | //
|
---|
246 | Dev->TxRing.Desc[DescIdx + 1].Flags = 0;
|
---|
247 | }
|
---|
248 |
|
---|
249 | //
|
---|
250 | // virtio-0.9.5, Appendix C, Packet Transmission
|
---|
251 | //
|
---|
252 | Dev->TxSharedReq->V0_9_5.Flags = 0;
|
---|
253 | Dev->TxSharedReq->V0_9_5.GsoType = VIRTIO_NET_HDR_GSO_NONE;
|
---|
254 |
|
---|
255 | //
|
---|
256 | // For VirtIo 1.0 only -- the field exists, but it is unused
|
---|
257 | //
|
---|
258 | Dev->TxSharedReq->NumBuffers = 0;
|
---|
259 |
|
---|
260 | //
|
---|
261 | // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
|
---|
262 | //
|
---|
263 | MemoryFence ();
|
---|
264 | Dev->TxLastUsed = *Dev->TxRing.Used.Idx;
|
---|
265 | ASSERT (Dev->TxLastUsed == 0);
|
---|
266 |
|
---|
267 | //
|
---|
268 | // want no interrupt when a transmit completes
|
---|
269 | //
|
---|
270 | *Dev->TxRing.Avail.Flags = (UINT16) VRING_AVAIL_F_NO_INTERRUPT;
|
---|
271 |
|
---|
272 | return EFI_SUCCESS;
|
---|
273 |
|
---|
274 | FreeTxSharedReqBuffer:
|
---|
275 | Dev->VirtIo->FreeSharedPages (
|
---|
276 | Dev->VirtIo,
|
---|
277 | EFI_SIZE_TO_PAGES (sizeof *(Dev->TxSharedReq)),
|
---|
278 | TxSharedReqBuffer
|
---|
279 | );
|
---|
280 |
|
---|
281 | UninitTxBufCollection:
|
---|
282 | OrderedCollectionUninit (Dev->TxBufCollection);
|
---|
283 |
|
---|
284 | FreeTxFreeStack:
|
---|
285 | FreePool (Dev->TxFreeStack);
|
---|
286 |
|
---|
287 | return Status;
|
---|
288 | }
|
---|
289 |
|
---|
290 |
|
---|
291 | /**
|
---|
292 | Set up static scaffolding for the VirtioNetReceive() SNP method and enable
|
---|
293 | live device operation.
|
---|
294 |
|
---|
295 | This function may only be called as VirtioNetInitialize()'s final step.
|
---|
296 |
|
---|
297 | The structures laid out and resources configured include:
|
---|
298 | - destination area for the host to write virtio-net request headers and
|
---|
299 | packet data into,
|
---|
300 | - select polling over RX interrupt,
|
---|
301 | - fully populate the RX queue with a static pattern of virtio descriptor
|
---|
302 | chains.
|
---|
303 |
|
---|
304 | @param[in,out] Dev The VNET_DEV driver instance about to enter the
|
---|
305 | EfiSimpleNetworkInitialized state.
|
---|
306 |
|
---|
307 | @return Status codes from VIRTIO_CFG_WRITE() or
|
---|
308 | VIRTIO_DEVICE_PROTOCOL.AllocateSharedPages or
|
---|
309 | VirtioMapAllBytesInSharedBuffer().
|
---|
310 | @retval EFI_SUCCESS RX setup successful. The device is live and may
|
---|
311 | already be writing to the receive area.
|
---|
312 | */
|
---|
313 |
|
---|
314 | STATIC
|
---|
315 | EFI_STATUS
|
---|
316 | EFIAPI
|
---|
317 | VirtioNetInitRx (
|
---|
318 | IN OUT VNET_DEV *Dev
|
---|
319 | )
|
---|
320 | {
|
---|
321 | EFI_STATUS Status;
|
---|
322 | UINTN VirtioNetReqSize;
|
---|
323 | UINTN RxBufSize;
|
---|
324 | UINT16 RxAlwaysPending;
|
---|
325 | UINTN PktIdx;
|
---|
326 | UINT16 DescIdx;
|
---|
327 | UINTN NumBytes;
|
---|
328 | EFI_PHYSICAL_ADDRESS RxBufDeviceAddress;
|
---|
329 | VOID *RxBuffer;
|
---|
330 |
|
---|
331 | //
|
---|
332 | // In VirtIo 1.0, the NumBuffers field is mandatory. In 0.9.5, it depends on
|
---|
333 | // VIRTIO_NET_F_MRG_RXBUF, which we never negotiate.
|
---|
334 | //
|
---|
335 | VirtioNetReqSize = (Dev->VirtIo->Revision < VIRTIO_SPEC_REVISION (1, 0, 0)) ?
|
---|
336 | sizeof (VIRTIO_NET_REQ) :
|
---|
337 | sizeof (VIRTIO_1_0_NET_REQ);
|
---|
338 |
|
---|
339 | //
|
---|
340 | // For each incoming packet we must supply two descriptors:
|
---|
341 | // - the recipient for the virtio-net request header, plus
|
---|
342 | // - the recipient for the network data (which consists of Ethernet header
|
---|
343 | // and Ethernet payload).
|
---|
344 | //
|
---|
345 | RxBufSize = VirtioNetReqSize +
|
---|
346 | (Dev->Snm.MediaHeaderSize + Dev->Snm.MaxPacketSize);
|
---|
347 |
|
---|
348 | //
|
---|
349 | // Limit the number of pending RX packets if the queue is big. The division
|
---|
350 | // by two is due to the above "two descriptors per packet" trait.
|
---|
351 | //
|
---|
352 | RxAlwaysPending = (UINT16) MIN (Dev->RxRing.QueueSize / 2, VNET_MAX_PENDING);
|
---|
353 |
|
---|
354 | //
|
---|
355 | // The RxBuf is shared between guest and hypervisor, use
|
---|
356 | // AllocateSharedPages() to allocate this memory region and map it with
|
---|
357 | // BusMasterCommonBuffer so that it can be accessed by both guest and
|
---|
358 | // hypervisor.
|
---|
359 | //
|
---|
360 | NumBytes = RxAlwaysPending * RxBufSize;
|
---|
361 | Dev->RxBufNrPages = EFI_SIZE_TO_PAGES (NumBytes);
|
---|
362 | Status = Dev->VirtIo->AllocateSharedPages (
|
---|
363 | Dev->VirtIo,
|
---|
364 | Dev->RxBufNrPages,
|
---|
365 | &RxBuffer
|
---|
366 | );
|
---|
367 | if (EFI_ERROR (Status)) {
|
---|
368 | return Status;
|
---|
369 | }
|
---|
370 |
|
---|
371 | ZeroMem (RxBuffer, NumBytes);
|
---|
372 |
|
---|
373 | Status = VirtioMapAllBytesInSharedBuffer (
|
---|
374 | Dev->VirtIo,
|
---|
375 | VirtioOperationBusMasterCommonBuffer,
|
---|
376 | RxBuffer,
|
---|
377 | NumBytes,
|
---|
378 | &Dev->RxBufDeviceBase,
|
---|
379 | &Dev->RxBufMap
|
---|
380 | );
|
---|
381 | if (EFI_ERROR (Status)) {
|
---|
382 | goto FreeSharedBuffer;
|
---|
383 | }
|
---|
384 |
|
---|
385 | Dev->RxBuf = RxBuffer;
|
---|
386 |
|
---|
387 | //
|
---|
388 | // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
|
---|
389 | //
|
---|
390 | MemoryFence ();
|
---|
391 | Dev->RxLastUsed = *Dev->RxRing.Used.Idx;
|
---|
392 | ASSERT (Dev->RxLastUsed == 0);
|
---|
393 |
|
---|
394 | //
|
---|
395 | // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device:
|
---|
396 | // the host should not send interrupts, we'll poll in VirtioNetReceive()
|
---|
397 | // and VirtioNetIsPacketAvailable().
|
---|
398 | //
|
---|
399 | *Dev->RxRing.Avail.Flags = (UINT16) VRING_AVAIL_F_NO_INTERRUPT;
|
---|
400 |
|
---|
401 | //
|
---|
402 | // now set up a separate, two-part descriptor chain for each RX packet, and
|
---|
403 | // link each chain into (from) the available ring as well
|
---|
404 | //
|
---|
405 | DescIdx = 0;
|
---|
406 | RxBufDeviceAddress = Dev->RxBufDeviceBase;
|
---|
407 | for (PktIdx = 0; PktIdx < RxAlwaysPending; ++PktIdx) {
|
---|
408 | //
|
---|
409 | // virtio-0.9.5, 2.4.1.2 Updating the Available Ring
|
---|
410 | // invisible to the host until we update the Index Field
|
---|
411 | //
|
---|
412 | Dev->RxRing.Avail.Ring[PktIdx] = DescIdx;
|
---|
413 |
|
---|
414 | //
|
---|
415 | // virtio-0.9.5, 2.4.1.1 Placing Buffers into the Descriptor Table
|
---|
416 | //
|
---|
417 | Dev->RxRing.Desc[DescIdx].Addr = RxBufDeviceAddress;
|
---|
418 | Dev->RxRing.Desc[DescIdx].Len = (UINT32) VirtioNetReqSize;
|
---|
419 | Dev->RxRing.Desc[DescIdx].Flags = VRING_DESC_F_WRITE | VRING_DESC_F_NEXT;
|
---|
420 | Dev->RxRing.Desc[DescIdx].Next = (UINT16) (DescIdx + 1);
|
---|
421 | RxBufDeviceAddress += Dev->RxRing.Desc[DescIdx++].Len;
|
---|
422 |
|
---|
423 | Dev->RxRing.Desc[DescIdx].Addr = RxBufDeviceAddress;
|
---|
424 | Dev->RxRing.Desc[DescIdx].Len = (UINT32) (RxBufSize - VirtioNetReqSize);
|
---|
425 | Dev->RxRing.Desc[DescIdx].Flags = VRING_DESC_F_WRITE;
|
---|
426 | RxBufDeviceAddress += Dev->RxRing.Desc[DescIdx++].Len;
|
---|
427 | }
|
---|
428 |
|
---|
429 | //
|
---|
430 | // virtio-0.9.5, 2.4.1.3 Updating the Index Field
|
---|
431 | //
|
---|
432 | MemoryFence ();
|
---|
433 | *Dev->RxRing.Avail.Idx = RxAlwaysPending;
|
---|
434 |
|
---|
435 | //
|
---|
436 | // At this point reception may already be running. In order to make it sure,
|
---|
437 | // kick the hypervisor. If we fail to kick it, we must first abort reception
|
---|
438 | // before tearing down anything, because reception may have been already
|
---|
439 | // running even without the kick.
|
---|
440 | //
|
---|
441 | // virtio-0.9.5, 2.4.1.4 Notifying the Device
|
---|
442 | //
|
---|
443 | MemoryFence ();
|
---|
444 | Status = Dev->VirtIo->SetQueueNotify (Dev->VirtIo, VIRTIO_NET_Q_RX);
|
---|
445 | if (EFI_ERROR (Status)) {
|
---|
446 | Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);
|
---|
447 | goto UnmapSharedBuffer;
|
---|
448 | }
|
---|
449 |
|
---|
450 | return Status;
|
---|
451 |
|
---|
452 | UnmapSharedBuffer:
|
---|
453 | Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Dev->RxBufMap);
|
---|
454 |
|
---|
455 | FreeSharedBuffer:
|
---|
456 | Dev->VirtIo->FreeSharedPages (
|
---|
457 | Dev->VirtIo,
|
---|
458 | Dev->RxBufNrPages,
|
---|
459 | RxBuffer
|
---|
460 | );
|
---|
461 | return Status;
|
---|
462 | }
|
---|
463 |
|
---|
464 |
|
---|
465 | /**
|
---|
466 | Resets a network adapter and allocates the transmit and receive buffers
|
---|
467 | required by the network interface; optionally, also requests allocation of
|
---|
468 | additional transmit and receive buffers.
|
---|
469 |
|
---|
470 | @param This The protocol instance pointer.
|
---|
471 | @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer
|
---|
472 | space that the driver should allocate for the
|
---|
473 | network interface. Some network interfaces will not
|
---|
474 | be able to use the extra buffer, and the caller
|
---|
475 | will not know if it is actually being used.
|
---|
476 | @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer
|
---|
477 | space that the driver should allocate for the
|
---|
478 | network interface. Some network interfaces will not
|
---|
479 | be able to use the extra buffer, and the caller
|
---|
480 | will not know if it is actually being used.
|
---|
481 |
|
---|
482 | @retval EFI_SUCCESS The network interface was initialized.
|
---|
483 | @retval EFI_NOT_STARTED The network interface has not been started.
|
---|
484 | @retval EFI_OUT_OF_RESOURCES There was not enough memory for the transmit
|
---|
485 | and receive buffers.
|
---|
486 | @retval EFI_INVALID_PARAMETER One or more of the parameters has an
|
---|
487 | unsupported value.
|
---|
488 | @retval EFI_DEVICE_ERROR The command could not be sent to the network
|
---|
489 | interface.
|
---|
490 | @retval EFI_UNSUPPORTED This function is not supported by the network
|
---|
491 | interface.
|
---|
492 |
|
---|
493 | **/
|
---|
494 |
|
---|
495 | EFI_STATUS
|
---|
496 | EFIAPI
|
---|
497 | VirtioNetInitialize (
|
---|
498 | IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
|
---|
499 | IN UINTN ExtraRxBufferSize OPTIONAL,
|
---|
500 | IN UINTN ExtraTxBufferSize OPTIONAL
|
---|
501 | )
|
---|
502 | {
|
---|
503 | VNET_DEV *Dev;
|
---|
504 | EFI_TPL OldTpl;
|
---|
505 | EFI_STATUS Status;
|
---|
506 | UINT8 NextDevStat;
|
---|
507 | UINT64 Features;
|
---|
508 |
|
---|
509 | if (This == NULL) {
|
---|
510 | return EFI_INVALID_PARAMETER;
|
---|
511 | }
|
---|
512 | if (ExtraRxBufferSize > 0 || ExtraTxBufferSize > 0) {
|
---|
513 | return EFI_UNSUPPORTED;
|
---|
514 | }
|
---|
515 |
|
---|
516 | Dev = VIRTIO_NET_FROM_SNP (This);
|
---|
517 | OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
|
---|
518 | if (Dev->Snm.State != EfiSimpleNetworkStarted) {
|
---|
519 | Status = EFI_NOT_STARTED;
|
---|
520 | goto InitFailed;
|
---|
521 | }
|
---|
522 |
|
---|
523 | //
|
---|
524 | // In the EfiSimpleNetworkStarted state the virtio-net device has status
|
---|
525 | // value 0 (= reset) -- see the state diagram, the full call chain to
|
---|
526 | // the end of VirtioNetGetFeatures() (considering we're here now),
|
---|
527 | // the DeviceFailed label below, and VirtioNetShutdown().
|
---|
528 | //
|
---|
529 | // Accordingly, the below is a subsequence of the steps found in the
|
---|
530 | // virtio-0.9.5 spec, 2.2.1 Device Initialization Sequence.
|
---|
531 | //
|
---|
532 | NextDevStat = VSTAT_ACK; // step 2 -- acknowledge device presence
|
---|
533 | Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);
|
---|
534 | if (EFI_ERROR (Status)) {
|
---|
535 | goto InitFailed;
|
---|
536 | }
|
---|
537 |
|
---|
538 | NextDevStat |= VSTAT_DRIVER; // step 3 -- we know how to drive it
|
---|
539 | Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);
|
---|
540 | if (EFI_ERROR (Status)) {
|
---|
541 | goto DeviceFailed;
|
---|
542 | }
|
---|
543 |
|
---|
544 | //
|
---|
545 | // Set Page Size - MMIO VirtIo Specific
|
---|
546 | //
|
---|
547 | Status = Dev->VirtIo->SetPageSize (Dev->VirtIo, EFI_PAGE_SIZE);
|
---|
548 | if (EFI_ERROR (Status)) {
|
---|
549 | goto DeviceFailed;
|
---|
550 | }
|
---|
551 |
|
---|
552 | //
|
---|
553 | // step 4a -- retrieve features. Note that we're past validating required
|
---|
554 | // features in VirtioNetGetFeatures().
|
---|
555 | //
|
---|
556 | Status = Dev->VirtIo->GetDeviceFeatures (Dev->VirtIo, &Features);
|
---|
557 | if (EFI_ERROR (Status)) {
|
---|
558 | goto DeviceFailed;
|
---|
559 | }
|
---|
560 |
|
---|
561 | ASSERT (Features & VIRTIO_NET_F_MAC);
|
---|
562 | ASSERT (Dev->Snm.MediaPresentSupported ==
|
---|
563 | !!(Features & VIRTIO_NET_F_STATUS));
|
---|
564 |
|
---|
565 | Features &= VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS | VIRTIO_F_VERSION_1 |
|
---|
566 | VIRTIO_F_IOMMU_PLATFORM;
|
---|
567 |
|
---|
568 | //
|
---|
569 | // In virtio-1.0, feature negotiation is expected to complete before queue
|
---|
570 | // discovery, and the device can also reject the selected set of features.
|
---|
571 | //
|
---|
572 | if (Dev->VirtIo->Revision >= VIRTIO_SPEC_REVISION (1, 0, 0)) {
|
---|
573 | Status = Virtio10WriteFeatures (Dev->VirtIo, Features, &NextDevStat);
|
---|
574 | if (EFI_ERROR (Status)) {
|
---|
575 | goto DeviceFailed;
|
---|
576 | }
|
---|
577 | }
|
---|
578 |
|
---|
579 | //
|
---|
580 | // step 4b, 4c -- allocate and report virtqueues
|
---|
581 | //
|
---|
582 | Status = VirtioNetInitRing (
|
---|
583 | Dev,
|
---|
584 | VIRTIO_NET_Q_RX,
|
---|
585 | &Dev->RxRing,
|
---|
586 | &Dev->RxRingMap
|
---|
587 | );
|
---|
588 | if (EFI_ERROR (Status)) {
|
---|
589 | goto DeviceFailed;
|
---|
590 | }
|
---|
591 |
|
---|
592 | Status = VirtioNetInitRing (
|
---|
593 | Dev,
|
---|
594 | VIRTIO_NET_Q_TX,
|
---|
595 | &Dev->TxRing,
|
---|
596 | &Dev->TxRingMap
|
---|
597 | );
|
---|
598 | if (EFI_ERROR (Status)) {
|
---|
599 | goto ReleaseRxRing;
|
---|
600 | }
|
---|
601 |
|
---|
602 | //
|
---|
603 | // step 5 -- keep only the features we want
|
---|
604 | //
|
---|
605 | if (Dev->VirtIo->Revision < VIRTIO_SPEC_REVISION (1, 0, 0)) {
|
---|
606 | Features &= ~(UINT64)(VIRTIO_F_VERSION_1 | VIRTIO_F_IOMMU_PLATFORM);
|
---|
607 | Status = Dev->VirtIo->SetGuestFeatures (Dev->VirtIo, Features);
|
---|
608 | if (EFI_ERROR (Status)) {
|
---|
609 | goto ReleaseTxRing;
|
---|
610 | }
|
---|
611 | }
|
---|
612 |
|
---|
613 | //
|
---|
614 | // step 6 -- virtio-net initialization complete
|
---|
615 | //
|
---|
616 | NextDevStat |= VSTAT_DRIVER_OK;
|
---|
617 | Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);
|
---|
618 | if (EFI_ERROR (Status)) {
|
---|
619 | goto ReleaseTxRing;
|
---|
620 | }
|
---|
621 |
|
---|
622 | Status = VirtioNetInitTx (Dev);
|
---|
623 | if (EFI_ERROR (Status)) {
|
---|
624 | goto AbortDevice;
|
---|
625 | }
|
---|
626 |
|
---|
627 | //
|
---|
628 | // start receiving
|
---|
629 | //
|
---|
630 | Status = VirtioNetInitRx (Dev);
|
---|
631 | if (EFI_ERROR (Status)) {
|
---|
632 | goto ReleaseTxAux;
|
---|
633 | }
|
---|
634 |
|
---|
635 | Dev->Snm.State = EfiSimpleNetworkInitialized;
|
---|
636 | gBS->RestoreTPL (OldTpl);
|
---|
637 | return EFI_SUCCESS;
|
---|
638 |
|
---|
639 | ReleaseTxAux:
|
---|
640 | VirtioNetShutdownTx (Dev);
|
---|
641 |
|
---|
642 | AbortDevice:
|
---|
643 | Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);
|
---|
644 |
|
---|
645 | ReleaseTxRing:
|
---|
646 | VirtioNetUninitRing (Dev, &Dev->TxRing, Dev->TxRingMap);
|
---|
647 |
|
---|
648 | ReleaseRxRing:
|
---|
649 | VirtioNetUninitRing (Dev, &Dev->RxRing, Dev->RxRingMap);
|
---|
650 |
|
---|
651 | DeviceFailed:
|
---|
652 | //
|
---|
653 | // restore device status invariant for the EfiSimpleNetworkStarted state
|
---|
654 | //
|
---|
655 | Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);
|
---|
656 |
|
---|
657 | InitFailed:
|
---|
658 | gBS->RestoreTPL (OldTpl);
|
---|
659 | return Status;
|
---|
660 | }
|
---|