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