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