2 Implementation of initializing a network adapter.
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
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.
19 Call UNDI to initialize the interface.
21 @param Snp Pointer to snp driver structure.
22 @param CableDetectFlag Do/don't detect the cable (depending on what
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.
33 UINT16 CableDetectFlag
36 PXE_CPB_INITIALIZE
*Cpb
;
43 if (Snp
->TxRxBufferSize
!= 0) {
44 Status
= Snp
->PciIo
->AllocateBuffer (
48 SNP_MEM_PAGES (Snp
->TxRxBufferSize
),
53 if (Status
!= EFI_SUCCESS
) {
56 "\nSnp->PxeInit() AllocateBuffer %xh (%r)\n",
66 Snp
->TxRxBuffer
= Addr
;
69 Cpb
->MemoryAddr
= (UINT64
)(UINTN
) Snp
->TxRxBuffer
;
71 Cpb
->MemoryLength
= Snp
->TxRxBufferSize
;
74 // let UNDI decide/detect these values
82 Cpb
->DuplexMode
= PXE_DUPLEX_DEFAULT
;
84 Cpb
->LoopBackMode
= LOOPBACK_NORMAL
;
86 Snp
->Cdb
.OpCode
= PXE_OPCODE_INITIALIZE
;
87 Snp
->Cdb
.OpFlags
= CableDetectFlag
;
89 Snp
->Cdb
.CPBsize
= (UINT16
) sizeof (PXE_CPB_INITIALIZE
);
90 Snp
->Cdb
.DBsize
= (UINT16
) sizeof (PXE_DB_INITIALIZE
);
92 Snp
->Cdb
.CPBaddr
= (UINT64
)(UINTN
) Snp
->Cpb
;
93 Snp
->Cdb
.DBaddr
= (UINT64
)(UINTN
) Snp
->Db
;
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
;
100 DEBUG ((EFI_D_NET
, "\nSnp->undi.initialize() "));
102 (*Snp
->IssueUndi32Command
) ((UINT64
)(UINTN
) &Snp
->Cdb
);
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.
111 if ((((Snp
->Cdb
.StatFlags
) & PXE_STATFLAGS_STATUS_MASK
) == PXE_STATFLAGS_COMMAND_COMPLETE
) &&
112 (Snp
->Cdb
.StatCode
== PXE_STATCODE_SUCCESS
)) {
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.
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
;
122 Snp
->Mode
.MediaPresent
= FALSE
;
126 Snp
->Mode
.State
= EfiSimpleNetworkInitialized
;
127 Status
= EFI_SUCCESS
;
131 "\nSnp->undi.initialize() %xh:%xh\n",
136 if (Snp
->TxRxBuffer
!= NULL
) {
137 Snp
->PciIo
->FreeBuffer (
139 SNP_MEM_PAGES (Snp
->TxRxBufferSize
),
140 (VOID
*) Snp
->TxRxBuffer
144 Snp
->TxRxBuffer
= NULL
;
146 Status
= EFI_DEVICE_ERROR
;
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.
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.
163 @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance.
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
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
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
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.
188 SnpUndi32Initialize (
189 IN EFI_SIMPLE_NETWORK_PROTOCOL
*This
,
190 IN UINTN ExtraRxBufferSize OPTIONAL
,
191 IN UINTN ExtraTxBufferSize OPTIONAL
194 EFI_STATUS EfiStatus
;
199 return EFI_INVALID_PARAMETER
;
202 Snp
= EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This
);
204 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
207 EfiStatus
= EFI_INVALID_PARAMETER
;
211 switch (Snp
->Mode
.State
) {
212 case EfiSimpleNetworkStarted
:
215 case EfiSimpleNetworkStopped
:
216 EfiStatus
= EFI_NOT_STARTED
;
220 EfiStatus
= EFI_DEVICE_ERROR
;
224 EfiStatus
= gBS
->CreateEvent (
227 &SnpWaitForPacketNotify
,
229 &Snp
->Snp
.WaitForPacket
232 if (EFI_ERROR (EfiStatus
)) {
233 Snp
->Snp
.WaitForPacket
= NULL
;
234 EfiStatus
= EFI_DEVICE_ERROR
;
240 Snp
->Mode
.MCastFilterCount
= 0;
241 Snp
->Mode
.ReceiveFilterSetting
= 0;
242 ZeroMem (Snp
->Mode
.MCastFilter
, sizeof Snp
->Mode
.MCastFilter
);
244 &Snp
->Mode
.CurrentAddress
,
245 &Snp
->Mode
.PermanentAddress
,
246 sizeof (EFI_MAC_ADDRESS
)
250 // Compute tx/rx buffer sizes based on UNDI init info and parameters.
252 Snp
->TxRxBufferSize
= (UINT32
) (Snp
->InitInfo
.MemoryRequired
+ ExtraRxBufferSize
+ ExtraTxBufferSize
);
255 // If UNDI support cable detect for INITIALIZE command, try it first.
257 if (Snp
->CableDetectSupported
) {
258 if (PxeInit (Snp
, PXE_OPFLAGS_INITIALIZE_DETECT_CABLE
) == EFI_SUCCESS
) {
263 Snp
->Mode
.MediaPresent
= FALSE
;
265 EfiStatus
= PxeInit (Snp
, PXE_OPFLAGS_INITIALIZE_DO_NOT_DETECT_CABLE
);
267 if (EFI_ERROR (EfiStatus
)) {
268 gBS
->CloseEvent (Snp
->Snp
.WaitForPacket
);
273 // Try to update the MediaPresent field of EFI_SIMPLE_NETWORK_MODE if the UNDI support it.
275 if (Snp
->MediaStatusSupported
) {
276 PxeGetStatus (Snp
, NULL
, FALSE
);
280 gBS
->RestoreTPL (OldTpl
);