]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/SnpDxe/Transmit.c
NetworkPkg: Move Network library and drivers from MdeModulePkg to NetworkPkg
[mirror_edk2.git] / NetworkPkg / SnpDxe / Transmit.c
1 /** @file
2 Implementation of transmitting a packet.
3
4 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "Snp.h"
10
11
12 /**
13 Call UNDI to create the meadia header for the given data buffer.
14
15 @param Snp Pointer to SNP driver structure.
16 @param MacHeaderPtr Address where the media header will be filled in.
17 @param HeaderSize Size of the memory at MacHeaderPtr.
18 @param Buffer Data buffer pointer.
19 @param BufferSize Size of data in the Buffer
20 @param DestAddr Address of the destination mac address buffer.
21 @param SrcAddr Address of the source mac address buffer.
22 @param ProtocolPtr Address of the protocol type.
23
24 @retval EFI_SUCCESS Successfully completed the undi call.
25 @retval Other Error return from undi call.
26
27 **/
28 EFI_STATUS
29 PxeFillHeader (
30 SNP_DRIVER *Snp,
31 VOID *MacHeaderPtr,
32 UINTN HeaderSize,
33 VOID *Buffer,
34 UINTN BufferSize,
35 EFI_MAC_ADDRESS *DestAddr,
36 EFI_MAC_ADDRESS *SrcAddr,
37 UINT16 *ProtocolPtr
38 )
39 {
40 PXE_CPB_FILL_HEADER_FRAGMENTED *Cpb;
41
42 Cpb = Snp->Cpb;
43 if (SrcAddr != NULL) {
44 CopyMem (
45 (VOID *) Cpb->SrcAddr,
46 (VOID *) SrcAddr,
47 Snp->Mode.HwAddressSize
48 );
49 } else {
50 CopyMem (
51 (VOID *) Cpb->SrcAddr,
52 (VOID *) &(Snp->Mode.CurrentAddress),
53 Snp->Mode.HwAddressSize
54 );
55 }
56
57 CopyMem (
58 (VOID *) Cpb->DestAddr,
59 (VOID *) DestAddr,
60 Snp->Mode.HwAddressSize
61 );
62
63 //
64 // we need to do the byte swapping
65 //
66 Cpb->Protocol = (UINT16) PXE_SWAP_UINT16 (*ProtocolPtr);
67
68 Cpb->PacketLen = (UINT32) (BufferSize);
69 Cpb->MediaHeaderLen = (UINT16) HeaderSize;
70
71 Cpb->FragCnt = 2;
72 Cpb->reserved = 0;
73
74 Cpb->FragDesc[0].FragAddr = (UINT64)(UINTN) MacHeaderPtr;
75 Cpb->FragDesc[0].FragLen = (UINT32) HeaderSize;
76 Cpb->FragDesc[1].FragAddr = (UINT64)(UINTN) Buffer;
77 Cpb->FragDesc[1].FragLen = (UINT32) BufferSize;
78
79 Cpb->FragDesc[0].reserved = Cpb->FragDesc[1].reserved = 0;
80
81 Snp->Cdb.OpCode = PXE_OPCODE_FILL_HEADER;
82 Snp->Cdb.OpFlags = PXE_OPFLAGS_FILL_HEADER_FRAGMENTED;
83
84 Snp->Cdb.DBsize = PXE_DBSIZE_NOT_USED;
85 Snp->Cdb.DBaddr = PXE_DBADDR_NOT_USED;
86
87 Snp->Cdb.CPBsize = (UINT16) sizeof (PXE_CPB_FILL_HEADER_FRAGMENTED);
88 Snp->Cdb.CPBaddr = (UINT64)(UINTN) Cpb;
89
90 Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE;
91 Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE;
92 Snp->Cdb.IFnum = Snp->IfNum;
93 Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST;
94
95 //
96 // Issue UNDI command and check result.
97 //
98 DEBUG ((EFI_D_NET, "\nSnp->undi.fill_header() "));
99
100 (*Snp->IssueUndi32Command) ((UINT64) (UINTN) &Snp->Cdb);
101
102 switch (Snp->Cdb.StatCode) {
103 case PXE_STATCODE_SUCCESS:
104 return EFI_SUCCESS;
105
106 case PXE_STATCODE_INVALID_PARAMETER:
107 DEBUG (
108 (EFI_D_ERROR,
109 "\nSnp->undi.fill_header() %xh:%xh\n",
110 Snp->Cdb.StatFlags,
111 Snp->Cdb.StatCode)
112 );
113
114 return EFI_INVALID_PARAMETER;
115
116 default:
117 DEBUG (
118 (EFI_D_ERROR,
119 "\nSnp->undi.fill_header() %xh:%xh\n",
120 Snp->Cdb.StatFlags,
121 Snp->Cdb.StatCode)
122 );
123
124 return EFI_DEVICE_ERROR;
125 }
126 }
127
128
129 /**
130 This routine calls undi to transmit the given data buffer
131
132 @param Snp pointer to SNP driver structure
133 @param Buffer data buffer pointer
134 @param BufferSize Size of data in the Buffer
135
136 @retval EFI_SUCCESS if successfully completed the undi call
137 @retval Other error return from undi call.
138
139 **/
140 EFI_STATUS
141 PxeTransmit (
142 SNP_DRIVER *Snp,
143 VOID *Buffer,
144 UINTN BufferSize
145 )
146 {
147 PXE_CPB_TRANSMIT *Cpb;
148 EFI_STATUS Status;
149
150 Cpb = Snp->Cpb;
151 Cpb->FrameAddr = (UINT64) (UINTN) Buffer;
152 Cpb->DataLen = (UINT32) BufferSize;
153
154 Cpb->MediaheaderLen = 0;
155 Cpb->reserved = 0;
156
157 Snp->Cdb.OpFlags = PXE_OPFLAGS_TRANSMIT_WHOLE;
158
159 Snp->Cdb.CPBsize = (UINT16) sizeof (PXE_CPB_TRANSMIT);
160 Snp->Cdb.CPBaddr = (UINT64)(UINTN) Cpb;
161
162 Snp->Cdb.OpCode = PXE_OPCODE_TRANSMIT;
163 Snp->Cdb.DBsize = PXE_DBSIZE_NOT_USED;
164 Snp->Cdb.DBaddr = PXE_DBADDR_NOT_USED;
165
166 Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE;
167 Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE;
168 Snp->Cdb.IFnum = Snp->IfNum;
169 Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST;
170
171 //
172 // Issue UNDI command and check result.
173 //
174 DEBUG ((EFI_D_NET, "\nSnp->undi.transmit() "));
175 DEBUG ((EFI_D_NET, "\nSnp->Cdb.OpCode == %x", Snp->Cdb.OpCode));
176 DEBUG ((EFI_D_NET, "\nSnp->Cdb.CPBaddr == %LX", Snp->Cdb.CPBaddr));
177 DEBUG ((EFI_D_NET, "\nSnp->Cdb.DBaddr == %LX", Snp->Cdb.DBaddr));
178 DEBUG ((EFI_D_NET, "\nCpb->FrameAddr == %LX\n", Cpb->FrameAddr));
179
180 (*Snp->IssueUndi32Command) ((UINT64) (UINTN) &Snp->Cdb);
181
182 DEBUG ((EFI_D_NET, "\nexit Snp->undi.transmit() "));
183
184 //
185 // we will unmap the buffers in get_status call, not here
186 //
187 switch (Snp->Cdb.StatCode) {
188 case PXE_STATCODE_SUCCESS:
189 return EFI_SUCCESS;
190
191 case PXE_STATCODE_BUFFER_FULL:
192 case PXE_STATCODE_QUEUE_FULL:
193 case PXE_STATCODE_BUSY:
194 Status = EFI_NOT_READY;
195 DEBUG (
196 (EFI_D_NET,
197 "\nSnp->undi.transmit() %xh:%xh\n",
198 Snp->Cdb.StatFlags,
199 Snp->Cdb.StatCode)
200 );
201 break;
202
203 default:
204 DEBUG (
205 (EFI_D_ERROR,
206 "\nSnp->undi.transmit() %xh:%xh\n",
207 Snp->Cdb.StatFlags,
208 Snp->Cdb.StatCode)
209 );
210 Status = EFI_DEVICE_ERROR;
211 }
212
213 return Status;
214 }
215
216 /**
217 Places a packet in the transmit queue of a network interface.
218
219 This function places the packet specified by Header and Buffer on the transmit
220 queue. If HeaderSize is nonzero and HeaderSize is not equal to
221 This->Mode->MediaHeaderSize, then EFI_INVALID_PARAMETER will be returned. If
222 BufferSize is less than This->Mode->MediaHeaderSize, then EFI_BUFFER_TOO_SMALL
223 will be returned. If Buffer is NULL, then EFI_INVALID_PARAMETER will be
224 returned. If HeaderSize is nonzero and DestAddr or Protocol is NULL, then
225 EFI_INVALID_PARAMETER will be returned. If the transmit engine of the network
226 interface is busy, then EFI_NOT_READY will be returned. If this packet can be
227 accepted by the transmit engine of the network interface, the packet contents
228 specified by Buffer will be placed on the transmit queue of the network
229 interface, and EFI_SUCCESS will be returned. GetStatus() can be used to
230 determine when the packet has actually been transmitted. The contents of the
231 Buffer must not be modified until the packet has actually been transmitted.
232 The Transmit() function performs nonblocking I/O. A caller who wants to perform
233 blocking I/O, should call Transmit(), and then GetStatus() until the
234 transmitted buffer shows up in the recycled transmit buffer.
235 If the driver has not been initialized, EFI_DEVICE_ERROR will be returned.
236
237 @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance.
238 @param HeaderSize The size, in bytes, of the media header to be filled in by the
239 Transmit() function. If HeaderSize is nonzero, then it must
240 be equal to This->Mode->MediaHeaderSize and the DestAddr and
241 Protocol parameters must not be NULL.
242 @param BufferSize The size, in bytes, of the entire packet (media header and
243 data) to be transmitted through the network interface.
244 @param Buffer A pointer to the packet (media header followed by data) to be
245 transmitted. This parameter cannot be NULL. If HeaderSize is
246 zero, then the media header in Buffer must already be filled
247 in by the caller. If HeaderSize is nonzero, then the media
248 header will be filled in by the Transmit() function.
249 @param SrcAddr The source HW MAC address. If HeaderSize is zero, then this
250 parameter is ignored. If HeaderSize is nonzero and SrcAddr
251 is NULL, then This->Mode->CurrentAddress is used for the
252 source HW MAC address.
253 @param DestAddr The destination HW MAC address. If HeaderSize is zero, then
254 this parameter is ignored.
255 @param Protocol The type of header to build. If HeaderSize is zero, then this
256 parameter is ignored. See RFC 1700, section "Ether Types,"
257 for examples.
258
259 @retval EFI_SUCCESS The packet was placed on the transmit queue.
260 @retval EFI_NOT_STARTED The network interface has not been started.
261 @retval EFI_NOT_READY The network interface is too busy to accept this
262 transmit request.
263 @retval EFI_BUFFER_TOO_SMALL The BufferSize parameter is too small.
264 @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported
265 value.
266 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
267 @retval EFI_UNSUPPORTED This function is not supported by the network interface.
268
269 **/
270 EFI_STATUS
271 EFIAPI
272 SnpUndi32Transmit (
273 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
274 IN UINTN HeaderSize,
275 IN UINTN BufferSize,
276 IN VOID *Buffer,
277 IN EFI_MAC_ADDRESS *SrcAddr, OPTIONAL
278 IN EFI_MAC_ADDRESS *DestAddr, OPTIONAL
279 IN UINT16 *Protocol OPTIONAL
280 )
281 {
282 SNP_DRIVER *Snp;
283 EFI_STATUS Status;
284 EFI_TPL OldTpl;
285
286 if (This == NULL) {
287 return EFI_INVALID_PARAMETER;
288 }
289
290 Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This);
291
292 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
293
294 if (Snp == NULL) {
295 return EFI_DEVICE_ERROR;
296 }
297
298 switch (Snp->Mode.State) {
299 case EfiSimpleNetworkInitialized:
300 break;
301
302 case EfiSimpleNetworkStopped:
303 Status = EFI_NOT_STARTED;
304 goto ON_EXIT;
305
306 default:
307 Status = EFI_DEVICE_ERROR;
308 goto ON_EXIT;
309 }
310
311 if (Buffer == NULL) {
312 Status = EFI_INVALID_PARAMETER;
313 goto ON_EXIT;
314 }
315
316 if (BufferSize < Snp->Mode.MediaHeaderSize) {
317 Status = EFI_BUFFER_TOO_SMALL;
318 goto ON_EXIT;
319 }
320
321 //
322 // if the HeaderSize is non-zero, we need to fill up the header and for that
323 // we need the destination address and the protocol
324 //
325 if (HeaderSize != 0) {
326 if (HeaderSize != Snp->Mode.MediaHeaderSize || DestAddr == 0 || Protocol == 0) {
327 Status = EFI_INVALID_PARAMETER;
328 goto ON_EXIT;
329 }
330
331 Status = PxeFillHeader (
332 Snp,
333 Buffer,
334 HeaderSize,
335 (UINT8 *) Buffer + HeaderSize,
336 BufferSize - HeaderSize,
337 DestAddr,
338 SrcAddr,
339 Protocol
340 );
341
342 if (EFI_ERROR (Status)) {
343 goto ON_EXIT;
344 }
345 }
346
347 Status = PxeTransmit (Snp, Buffer, BufferSize);
348
349 ON_EXIT:
350 gBS->RestoreTPL (OldTpl);
351
352 return Status;
353 }