]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/SnpDxe/Initialize.c
MdeModulePkg: Fix SNP.Initialize() spec conformance issue
[mirror_edk2.git] / MdeModulePkg / Universal / Network / SnpDxe / Initialize.c
1 /** @file
2 Implementation of initializing a network adapter.
3
4 Copyright (c) 2004 - 2016, 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
16 #include "Snp.h"
17
18 /**
19 Call UNDI to initialize the interface.
20
21 @param Snp Pointer to snp driver structure.
22 @param CableDetectFlag Do/don't detect the cable (depending on what
23 undi supports).
24
25 @retval EFI_SUCCESS UNDI is initialized successfully.
26 @retval EFI_DEVICE_ERROR UNDI could not be initialized.
27 @retval Other Other errors as indicated.
28
29 **/
30 EFI_STATUS
31 PxeInit (
32 SNP_DRIVER *Snp,
33 UINT16 CableDetectFlag
34 )
35 {
36 PXE_CPB_INITIALIZE *Cpb;
37 VOID *Addr;
38 EFI_STATUS Status;
39
40 Status = EFI_SUCCESS;
41
42 Cpb = Snp->Cpb;
43 if (Snp->TxRxBufferSize != 0) {
44 Status = Snp->PciIo->AllocateBuffer (
45 Snp->PciIo,
46 AllocateAnyPages,
47 EfiBootServicesData,
48 SNP_MEM_PAGES (Snp->TxRxBufferSize),
49 &Addr,
50 0
51 );
52
53 if (Status != EFI_SUCCESS) {
54 DEBUG (
55 (EFI_D_ERROR,
56 "\nSnp->PxeInit() AllocateBuffer %xh (%r)\n",
57 Status,
58 Status)
59 );
60
61 return Status;
62 }
63
64 ASSERT (Addr);
65
66 Snp->TxRxBuffer = Addr;
67 }
68
69 Cpb->MemoryAddr = (UINT64)(UINTN) Snp->TxRxBuffer;
70
71 Cpb->MemoryLength = Snp->TxRxBufferSize;
72
73 //
74 // let UNDI decide/detect these values
75 //
76 Cpb->LinkSpeed = 0;
77 Cpb->TxBufCnt = 0;
78 Cpb->TxBufSize = 0;
79 Cpb->RxBufCnt = 0;
80 Cpb->RxBufSize = 0;
81
82 Cpb->DuplexMode = PXE_DUPLEX_DEFAULT;
83
84 Cpb->LoopBackMode = LOOPBACK_NORMAL;
85
86 Snp->Cdb.OpCode = PXE_OPCODE_INITIALIZE;
87 Snp->Cdb.OpFlags = CableDetectFlag;
88
89 Snp->Cdb.CPBsize = (UINT16) sizeof (PXE_CPB_INITIALIZE);
90 Snp->Cdb.DBsize = (UINT16) sizeof (PXE_DB_INITIALIZE);
91
92 Snp->Cdb.CPBaddr = (UINT64)(UINTN) Snp->Cpb;
93 Snp->Cdb.DBaddr = (UINT64)(UINTN) Snp->Db;
94
95 Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE;
96 Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE;
97 Snp->Cdb.IFnum = Snp->IfNum;
98 Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST;
99
100 DEBUG ((EFI_D_NET, "\nSnp->undi.initialize() "));
101
102 (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb);
103
104 //
105 // There are two fields need to be checked here:
106 // First is the upper two bits (14 & 15) in the CDB.StatFlags field. Until these bits change to report
107 // PXE_STATFLAGS_COMMAND_COMPLETE or PXE_STATFLAGS_COMMAND_FAILED, the command has not been executed by the UNDI.
108 // Second is the CDB.StatCode field. After command execution completes, either successfully or not,
109 // the CDB.StatCode field contains the result of the command execution.
110 //
111 if ((((Snp->Cdb.StatFlags) & PXE_STATFLAGS_STATUS_MASK) == PXE_STATFLAGS_COMMAND_COMPLETE) &&
112 (Snp->Cdb.StatCode == PXE_STATCODE_SUCCESS)) {
113 //
114 // If cable detect feature is enabled in CDB.OpFlags, check the CDB.StatFlags to see if there is an
115 // active connection to this network device. If the no media StatFlag is set, the UNDI and network
116 // device are still initialized.
117 //
118 if (CableDetectFlag == PXE_OPFLAGS_INITIALIZE_DETECT_CABLE) {
119 if(((Snp->Cdb.StatFlags) & PXE_STATFLAGS_INITIALIZED_NO_MEDIA) != PXE_STATFLAGS_INITIALIZED_NO_MEDIA) {
120 Snp->Mode.MediaPresent = TRUE;
121 } else {
122 Snp->Mode.MediaPresent = FALSE;
123 }
124 }
125
126 Snp->Mode.State = EfiSimpleNetworkInitialized;
127 Status = EFI_SUCCESS;
128 } else {
129 DEBUG (
130 (EFI_D_WARN,
131 "\nSnp->undi.initialize() %xh:%xh\n",
132 Snp->Cdb.StatFlags,
133 Snp->Cdb.StatCode)
134 );
135
136 if (Snp->TxRxBuffer != NULL) {
137 Snp->PciIo->FreeBuffer (
138 Snp->PciIo,
139 SNP_MEM_PAGES (Snp->TxRxBufferSize),
140 (VOID *) Snp->TxRxBuffer
141 );
142 }
143
144 Snp->TxRxBuffer = NULL;
145
146 Status = EFI_DEVICE_ERROR;
147 }
148
149 return Status;
150 }
151
152
153 /**
154 Resets a network adapter and allocates the transmit and receive buffers
155 required by the network interface; optionally, also requests allocation of
156 additional transmit and receive buffers.
157
158 This function allocates the transmit and receive buffers required by the network
159 interface. If this allocation fails, then EFI_OUT_OF_RESOURCES is returned.
160 If the allocation succeeds and the network interface is successfully initialized,
161 then EFI_SUCCESS will be returned.
162
163 @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance.
164
165 @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer space
166 that the driver should allocate for the network interface.
167 Some network interfaces will not be able to use the
168 extra buffer, and the caller will not know if it is
169 actually being used.
170 @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer space
171 that the driver should allocate for the network interface.
172 Some network interfaces will not be able to use the
173 extra buffer, and the caller will not know if it is
174 actually being used.
175
176 @retval EFI_SUCCESS The network interface was initialized.
177 @retval EFI_NOT_STARTED The network interface has not been started.
178 @retval EFI_OUT_OF_RESOURCES There was not enough memory for the transmit and
179 receive buffers.
180 @retval EFI_INVALID_PARAMETER This parameter was NULL or did not point to a valid
181 EFI_SIMPLE_NETWORK_PROTOCOL structure.
182 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
183 @retval EFI_UNSUPPORTED The increased buffer size feature is not supported.
184
185 **/
186 EFI_STATUS
187 EFIAPI
188 SnpUndi32Initialize (
189 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
190 IN UINTN ExtraRxBufferSize OPTIONAL,
191 IN UINTN ExtraTxBufferSize OPTIONAL
192 )
193 {
194 EFI_STATUS EfiStatus;
195 SNP_DRIVER *Snp;
196 EFI_TPL OldTpl;
197
198 if (This == NULL) {
199 return EFI_INVALID_PARAMETER;
200 }
201
202 Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This);
203
204 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
205
206 if (Snp == NULL) {
207 EfiStatus = EFI_INVALID_PARAMETER;
208 goto ON_EXIT;
209 }
210
211 switch (Snp->Mode.State) {
212 case EfiSimpleNetworkStarted:
213 break;
214
215 case EfiSimpleNetworkStopped:
216 EfiStatus = EFI_NOT_STARTED;
217 goto ON_EXIT;
218
219 default:
220 EfiStatus = EFI_DEVICE_ERROR;
221 goto ON_EXIT;
222 }
223
224 EfiStatus = gBS->CreateEvent (
225 EVT_NOTIFY_WAIT,
226 TPL_NOTIFY,
227 &SnpWaitForPacketNotify,
228 Snp,
229 &Snp->Snp.WaitForPacket
230 );
231
232 if (EFI_ERROR (EfiStatus)) {
233 Snp->Snp.WaitForPacket = NULL;
234 EfiStatus = EFI_DEVICE_ERROR;
235 goto ON_EXIT;
236 }
237 //
238 //
239 //
240 Snp->Mode.MCastFilterCount = 0;
241 Snp->Mode.ReceiveFilterSetting = 0;
242 ZeroMem (Snp->Mode.MCastFilter, sizeof Snp->Mode.MCastFilter);
243 CopyMem (
244 &Snp->Mode.CurrentAddress,
245 &Snp->Mode.PermanentAddress,
246 sizeof (EFI_MAC_ADDRESS)
247 );
248
249 //
250 // Compute tx/rx buffer sizes based on UNDI init info and parameters.
251 //
252 Snp->TxRxBufferSize = (UINT32) (Snp->InitInfo.MemoryRequired + ExtraRxBufferSize + ExtraTxBufferSize);
253
254 //
255 // If UNDI support cable detect for INITIALIZE command, try it first.
256 //
257 if (Snp->CableDetectSupported) {
258 if (PxeInit (Snp, PXE_OPFLAGS_INITIALIZE_DETECT_CABLE) == EFI_SUCCESS) {
259 goto ON_EXIT;
260 }
261 }
262
263 Snp->Mode.MediaPresent = FALSE;
264
265 EfiStatus = PxeInit (Snp, PXE_OPFLAGS_INITIALIZE_DO_NOT_DETECT_CABLE);
266
267 if (EFI_ERROR (EfiStatus)) {
268 gBS->CloseEvent (Snp->Snp.WaitForPacket);
269 goto ON_EXIT;
270 }
271
272 //
273 // Try to update the MediaPresent field of EFI_SIMPLE_NETWORK_MODE if the UNDI support it.
274 //
275 if (Snp->MediaStatusSupported) {
276 PxeGetStatus (Snp, NULL, FALSE);
277 }
278
279 ON_EXIT:
280 gBS->RestoreTPL (OldTpl);
281
282 return EfiStatus;
283 }