]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/VirtioNetDxe/SnpTransmit.c
ShellPkg/for: Fix potential null pointer deference
[mirror_edk2.git] / OvmfPkg / VirtioNetDxe / SnpTransmit.c
CommitLineData
b6dfc654
LE
1/** @file\r
2\r
3 Implementation of the SNP.Transmit() function and its private helpers if any.\r
4\r
5 Copyright (C) 2013, Red Hat, Inc.\r
8258c4e6 6 Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
b6dfc654
LE
7\r
8 This program and the accompanying materials are licensed and made available\r
9 under the terms and conditions of the BSD License which accompanies this\r
10 distribution. The full text of the license may be found at\r
11 http://opensource.org/licenses/bsd-license.php\r
12\r
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
14 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17\r
18#include <Library/BaseLib.h>\r
19#include <Library/BaseMemoryLib.h>\r
20#include <Library/UefiBootServicesTableLib.h>\r
21\r
22#include "VirtioNet.h"\r
23\r
24/**\r
25 Places a packet in the transmit queue of a network interface.\r
26\r
27 @param This The protocol instance pointer.\r
28 @param HeaderSize The size, in bytes, of the media header to be filled in by\r
29 the Transmit() function. If HeaderSize is non-zero, then\r
30 it must be equal to This->Mode->MediaHeaderSize and the\r
31 DestAddr and Protocol parameters must not be NULL.\r
32 @param BufferSize The size, in bytes, of the entire packet (media header and\r
33 data) to be transmitted through the network interface.\r
34 @param Buffer A pointer to the packet (media header followed by data) to\r
35 be transmitted. This parameter cannot be NULL. If\r
36 HeaderSize is zero, then the media header in Buffer must\r
37 already be filled in by the caller. If HeaderSize is\r
38 non-zero, then the media header will be filled in by the\r
39 Transmit() function.\r
40 @param SrcAddr The source HW MAC address. If HeaderSize is zero, then\r
41 this parameter is ignored. If HeaderSize is non-zero and\r
42 SrcAddr is NULL, then This->Mode->CurrentAddress is used\r
43 for the source HW MAC address.\r
44 @param DestAddr The destination HW MAC address. If HeaderSize is zero,\r
45 then this parameter is ignored.\r
46 @param Protocol The type of header to build. If HeaderSize is zero, then\r
47 this parameter is ignored. See RFC 1700, section "Ether\r
48 Types", for examples.\r
49\r
50 @retval EFI_SUCCESS The packet was placed on the transmit queue.\r
51 @retval EFI_NOT_STARTED The network interface has not been started.\r
52 @retval EFI_NOT_READY The network interface is too busy to accept\r
53 this transmit request.\r
54 @retval EFI_BUFFER_TOO_SMALL The BufferSize parameter is too small.\r
55 @retval EFI_INVALID_PARAMETER One or more of the parameters has an\r
56 unsupported value.\r
57 @retval EFI_DEVICE_ERROR The command could not be sent to the network\r
58 interface.\r
59 @retval EFI_UNSUPPORTED This function is not supported by the network\r
60 interface.\r
61\r
62**/\r
63\r
64EFI_STATUS\r
65EFIAPI\r
66VirtioNetTransmit (\r
67 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,\r
68 IN UINTN HeaderSize,\r
69 IN UINTN BufferSize,\r
70 IN /* +OUT! */ VOID *Buffer,\r
71 IN EFI_MAC_ADDRESS *SrcAddr OPTIONAL,\r
72 IN EFI_MAC_ADDRESS *DestAddr OPTIONAL,\r
73 IN UINT16 *Protocol OPTIONAL\r
74 )\r
75{\r
8fa54a8a
BS
76 VNET_DEV *Dev;\r
77 EFI_TPL OldTpl;\r
78 EFI_STATUS Status;\r
79 UINT16 DescIdx;\r
80 UINT16 AvailIdx;\r
81 EFI_PHYSICAL_ADDRESS DeviceAddress;\r
b6dfc654
LE
82\r
83 if (This == NULL || BufferSize == 0 || Buffer == NULL) {\r
84 return EFI_INVALID_PARAMETER;\r
85 }\r
86\r
87 Dev = VIRTIO_NET_FROM_SNP (This);\r
88 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
89 switch (Dev->Snm.State) {\r
90 case EfiSimpleNetworkStopped:\r
91 Status = EFI_NOT_STARTED;\r
92 goto Exit;\r
93 case EfiSimpleNetworkStarted:\r
94 Status = EFI_DEVICE_ERROR;\r
95 goto Exit;\r
96 default:\r
97 break;\r
98 }\r
99\r
100 if (BufferSize < Dev->Snm.MediaHeaderSize) {\r
101 Status = EFI_BUFFER_TOO_SMALL;\r
102 goto Exit;\r
103 }\r
104 if (BufferSize > Dev->Snm.MediaHeaderSize + Dev->Snm.MaxPacketSize) {\r
105 Status = EFI_INVALID_PARAMETER;\r
106 goto Exit;\r
107 }\r
108\r
109 //\r
110 // check if we have room for transmission\r
111 //\r
112 ASSERT (Dev->TxCurPending <= Dev->TxMaxPending);\r
113 if (Dev->TxCurPending == Dev->TxMaxPending) {\r
114 Status = EFI_NOT_READY;\r
115 goto Exit;\r
116 }\r
117\r
118 //\r
119 // the caller may want us to fill in the media header:\r
120 // dst MAC, src MAC, Ethertype\r
121 //\r
122 if (HeaderSize != 0) {\r
123 UINT8 *Ptr;\r
124\r
125 if (HeaderSize != Dev->Snm.MediaHeaderSize ||\r
126 DestAddr == NULL || Protocol == NULL) {\r
127 Status = EFI_INVALID_PARAMETER;\r
128 goto Exit;\r
129 }\r
130 Ptr = Buffer;\r
56f65ed8 131 ASSERT (SIZE_OF_VNET (Mac) <= sizeof (EFI_MAC_ADDRESS));\r
b6dfc654 132\r
56f65ed8
OM
133 CopyMem (Ptr, DestAddr, SIZE_OF_VNET (Mac));\r
134 Ptr += SIZE_OF_VNET (Mac);\r
b6dfc654
LE
135\r
136 CopyMem (Ptr,\r
137 (SrcAddr == NULL) ? &Dev->Snm.CurrentAddress : SrcAddr,\r
56f65ed8
OM
138 SIZE_OF_VNET (Mac));\r
139 Ptr += SIZE_OF_VNET (Mac);\r
b6dfc654
LE
140\r
141 *Ptr++ = (UINT8) (*Protocol >> 8);\r
142 *Ptr++ = (UINT8) *Protocol;\r
143\r
8258c4e6 144 ASSERT ((UINTN) (Ptr - (UINT8 *) Buffer) == Dev->Snm.MediaHeaderSize);\r
b6dfc654
LE
145 }\r
146\r
8fa54a8a
BS
147 //\r
148 // Map the transmit buffer system physical address to device address.\r
149 //\r
150 Status = VirtioNetMapTxBuf (\r
151 Dev,\r
152 Buffer,\r
153 BufferSize,\r
154 &DeviceAddress\r
155 );\r
156 if (EFI_ERROR (Status)) {\r
157 Status = EFI_DEVICE_ERROR;\r
158 goto Exit;\r
159 }\r
160\r
b6dfc654
LE
161 //\r
162 // virtio-0.9.5, 2.4.1 Supplying Buffers to The Device\r
163 //\r
164 DescIdx = Dev->TxFreeStack[Dev->TxCurPending++];\r
8fa54a8a 165 Dev->TxRing.Desc[DescIdx + 1].Addr = DeviceAddress;\r
b6dfc654
LE
166 Dev->TxRing.Desc[DescIdx + 1].Len = (UINT32) BufferSize;\r
167\r
168 //\r
169 // the available index is never written by the host, we can read it back\r
170 // without a barrier\r
171 //\r
172 AvailIdx = *Dev->TxRing.Avail.Idx;\r
173 Dev->TxRing.Avail.Ring[AvailIdx++ % Dev->TxRing.QueueSize] = DescIdx;\r
174\r
175 MemoryFence ();\r
176 *Dev->TxRing.Avail.Idx = AvailIdx;\r
177\r
178 MemoryFence ();\r
56f65ed8 179 Status = Dev->VirtIo->SetQueueNotify (Dev->VirtIo, VIRTIO_NET_Q_TX);\r
b6dfc654
LE
180\r
181Exit:\r
182 gBS->RestoreTPL (OldTpl);\r
183 return Status;\r
184}\r