1. Sync Tcp4 protocol definitions to match UEFI 2.1
[mirror_edk2.git] / MdeModulePkg / Universal / Network / SnpDxe / receive.c
1 /** @file
2 Copyright (c) 2004 - 2007, Intel Corporation
3 All rights reserved. This program and the accompanying materials
4 are licensed and made available under the terms and conditions of the BSD License
5 which accompanies this distribution. The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.php
7
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
10
11 Module name:
12 receive.c
13
14 Abstract:
15
16 Revision history:
17 2000-Feb-03 M(f)J Genesis.
18
19 **/
20
21
22 #include "Snp.h"
23
24 /**
25 this routine calls undi to receive a packet and fills in the data in the
26 input pointers!
27
28 @param snp pointer to snp driver structure
29 @param BufferPtr pointer to the memory for the received data
30 @param BuffSizePtr is a pointer to the length of the buffer on entry and
31 contains the length of the received data on return
32 @param HeaderSizePtr pointer to the header portion of the data received.
33 @param SourceAddrPtr optional parameter, is a pointer to contain the
34 source ethernet address on return
35 @param DestinationAddrPtr optional parameter, is a pointer to contain the
36 destination ethernet address on return
37 @param ProtocolPtr optional parameter, is a pointer to contain the
38 protocol type from the ethernet header on return
39
40
41 **/
42 STATIC
43 EFI_STATUS
44 pxe_receive (
45 SNP_DRIVER *snp,
46 VOID *BufferPtr,
47 UINTN *BuffSizePtr,
48 UINTN *HeaderSizePtr,
49 EFI_MAC_ADDRESS *SourceAddrPtr,
50 EFI_MAC_ADDRESS *DestinationAddrPtr,
51 UINT16 *ProtocolPtr
52 )
53 {
54 PXE_CPB_RECEIVE *cpb;
55 PXE_DB_RECEIVE *db;
56 UINTN buf_size;
57 UINT64 TempData;
58
59 cpb = snp->cpb;
60 db = snp->db;
61 buf_size = *BuffSizePtr;
62 //
63 // IMPORTANT NOTE:
64 // In case of the older 3.0 UNDI, if the input buffer address is beyond 4GB,
65 // DO NOT call the map function on the given buffer, instead use
66 // a global buffer. The reason is that UNDI3.0 has some unnecessary check of
67 // making sure that all the addresses (whether or not they will be given
68 // to the NIC ) supplied to it are below 4GB. It may or may not use
69 // the mapped address after all (like in case of CPB and DB)!
70 // Instead of using the global buffer whose address is allocated within the
71 // 2GB limit if I start mapping the given buffer we lose the data, here is
72 // why!!!
73 // if our address is > 4GB, the map call creates another buffer below 2GB and
74 // copies data to/from the original buffer to the mapped buffer either at
75 // map time or unmap time depending on the map direction.
76 // UNDI will not complain since we already mapped the buffer to be
77 // within the 2GB limit but will not use (I know undi) the mapped address
78 // since it does not give the user buffers to the NIC's receive unit,
79 // It just copies the received packet into the user buffer using the virtual
80 // (CPU) address rather than the mapped (device or physical) address.
81 // When the UNDI call returns, if we then unmap the buffer, we will lose
82 // the contents because unmap copies the contents of the mapped buffer into
83 // the original buffer (since the direction is FROM_DEVICE) !!!
84 //
85 // this is not a problem in Undi 3.1 because this undi uses it's map callback
86 // routine to map a cpu address to device address and it does it only if
87 // it is giving the address to the device and unmaps it before using the cpu
88 // address!
89 //
90 TempData = (UINT64) (UINTN) BufferPtr;
91 if (snp->IsOldUndi && (TempData >= FOUR_GIGABYTES)) {
92 cpb->BufferAddr = (UINT64)(UINTN) snp->receive_buf;
93 cpb->BufferLen = (UINT32) (snp->init_info.MediaHeaderLen + snp->init_info.FrameDataLen);
94 } else {
95 cpb->BufferAddr = (UINT64)(UINTN) BufferPtr;
96 cpb->BufferLen = (UINT32) *BuffSizePtr;
97 }
98
99 cpb->reserved = 0;
100
101 snp->cdb.OpCode = PXE_OPCODE_RECEIVE;
102 snp->cdb.OpFlags = PXE_OPFLAGS_NOT_USED;
103
104 snp->cdb.CPBsize = sizeof (PXE_CPB_RECEIVE);
105 snp->cdb.CPBaddr = (UINT64)(UINTN) cpb;
106
107 snp->cdb.DBsize = sizeof (PXE_DB_RECEIVE);
108 snp->cdb.DBaddr = (UINT64)(UINTN) db;
109
110 snp->cdb.StatCode = PXE_STATCODE_INITIALIZE;
111 snp->cdb.StatFlags = PXE_STATFLAGS_INITIALIZE;
112 snp->cdb.IFnum = snp->if_num;
113 snp->cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST;
114
115 //
116 // Issue UNDI command and check result.
117 //
118 DEBUG ((EFI_D_NET, "\nsnp->undi.receive () "));
119
120 (*snp->issue_undi32_command) ((UINT64)(UINTN) &snp->cdb);
121
122 switch (snp->cdb.StatCode) {
123 case PXE_STATCODE_SUCCESS:
124 break;
125
126 case PXE_STATCODE_NO_DATA:
127 DEBUG (
128 (EFI_D_NET,
129 "\nsnp->undi.receive () %xh:%xh\n",
130 snp->cdb.StatFlags,
131 snp->cdb.StatCode)
132 );
133
134 return EFI_NOT_READY;
135
136 default:
137 DEBUG (
138 (EFI_D_ERROR,
139 "\nsnp->undi.receive() %xh:%xh\n",
140 snp->cdb.StatFlags,
141 snp->cdb.StatCode)
142 );
143
144 return EFI_DEVICE_ERROR;
145 }
146
147 *BuffSizePtr = db->FrameLen;
148
149 if (HeaderSizePtr != NULL) {
150 *HeaderSizePtr = db->MediaHeaderLen;
151 }
152
153 if (SourceAddrPtr != NULL) {
154 CopyMem (SourceAddrPtr, &db->SrcAddr, snp->mode.HwAddressSize);
155 }
156
157 if (DestinationAddrPtr != NULL) {
158 CopyMem (DestinationAddrPtr, &db->DestAddr, snp->mode.HwAddressSize);
159 }
160
161 if (ProtocolPtr != NULL) {
162 *ProtocolPtr = (UINT16) PXE_SWAP_UINT16 (db->Protocol); /* we need to do the byte swapping */
163 }
164
165 TempData = (UINT64) (UINTN) BufferPtr;
166 if (snp->IsOldUndi && (TempData >= FOUR_GIGABYTES)) {
167 CopyMem (BufferPtr, snp->receive_buf, snp->init_info.MediaHeaderLen + snp->init_info.FrameDataLen);
168 }
169
170 return (*BuffSizePtr <= buf_size) ? EFI_SUCCESS : EFI_BUFFER_TOO_SMALL;
171 }
172
173
174 /**
175 This is the SNP interface routine for receiving network data.
176 This routine basically retrieves snp structure, checks the SNP state and
177 calls the pxe_receive routine to actually do the receive!
178
179 @param this context pointer
180 @param HeaderSizePtr optional parameter and is a pointer to the header
181 portion of the data received.
182 @param BuffSizePtr is a pointer to the length of the buffer on entry and
183 contains the length of the received data on return
184 @param BufferPtr pointer to the memory for the received data
185 @param SourceAddrPtr optional parameter, is a pointer to contain the
186 source ethernet address on return
187 @param DestinationAddrPtr optional parameter, is a pointer to contain the
188 destination ethernet address on return
189 @param ProtocolPtr optional parameter, is a pointer to contain the
190 protocol type from the ethernet header on return
191
192
193 **/
194 EFI_STATUS
195 EFIAPI
196 snp_undi32_receive (
197 IN EFI_SIMPLE_NETWORK_PROTOCOL * this,
198 OUT UINTN *HeaderSizePtr OPTIONAL,
199 IN OUT UINTN *BuffSizePtr,
200 OUT VOID *BufferPtr,
201 OUT EFI_MAC_ADDRESS * SourceAddrPtr OPTIONAL,
202 OUT EFI_MAC_ADDRESS * DestinationAddrPtr OPTIONAL,
203 OUT UINT16 *ProtocolPtr OPTIONAL
204 )
205 {
206 SNP_DRIVER *snp;
207 EFI_TPL OldTpl;
208 EFI_STATUS Status;
209
210 if (this == NULL) {
211 return EFI_INVALID_PARAMETER;
212 }
213
214 snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (this);
215
216 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
217
218 switch (snp->mode.State) {
219 case EfiSimpleNetworkInitialized:
220 break;
221
222 case EfiSimpleNetworkStopped:
223 Status = EFI_NOT_STARTED;
224 goto ON_EXIT;
225
226 default:
227 Status = EFI_DEVICE_ERROR;
228 goto ON_EXIT;
229 }
230
231 if ((BuffSizePtr == NULL) || (BufferPtr == NULL)) {
232 Status = EFI_INVALID_PARAMETER;
233 goto ON_EXIT;
234 }
235
236 if (!snp->mode.ReceiveFilterSetting) {
237 Status = EFI_DEVICE_ERROR;
238 goto ON_EXIT;
239 }
240
241 Status = pxe_receive (
242 snp,
243 BufferPtr,
244 BuffSizePtr,
245 HeaderSizePtr,
246 SourceAddrPtr,
247 DestinationAddrPtr,
248 ProtocolPtr
249 );
250
251 ON_EXIT:
252 gBS->RestoreTPL (OldTpl);
253
254 return Status;
255 }