]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/SnpDxe/Get_status.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Universal / Network / SnpDxe / Get_status.c
1 /** @file
2 Implementation of reading the current interrupt status and recycled transmit
3 buffer status from a network interface.
4
5 Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "Snp.h"
11
12 /**
13 Call undi to get the status of the interrupts, get the list of recycled transmit
14 buffers that completed transmitting. The recycled transmit buffer address will
15 be saved into Snp->RecycledTxBuf. This function will also update the MediaPresent
16 field of EFI_SIMPLE_NETWORK_MODE if UNDI support it.
17
18 @param[in] Snp Pointer to snp driver structure.
19 @param[out] InterruptStatusPtr A non null pointer to contain the interrupt
20 status.
21 @param[in] GetTransmittedBuf Set to TRUE to retrieve the recycled transmit
22 buffer address.
23
24 @retval EFI_SUCCESS The status of the network interface was retrieved.
25 @retval EFI_DEVICE_ERROR The command could not be sent to the network
26 interface.
27
28 **/
29 EFI_STATUS
30 PxeGetStatus (
31 IN SNP_DRIVER *Snp,
32 OUT UINT32 *InterruptStatusPtr,
33 IN BOOLEAN GetTransmittedBuf
34 )
35 {
36 PXE_DB_GET_STATUS *Db;
37 UINT16 InterruptFlags;
38 UINT32 Index;
39 UINT64 *Tmp;
40
41 Tmp = NULL;
42 Db = Snp->Db;
43 Snp->Cdb.OpCode = PXE_OPCODE_GET_STATUS;
44
45 Snp->Cdb.OpFlags = 0;
46
47 if (GetTransmittedBuf) {
48 Snp->Cdb.OpFlags |= PXE_OPFLAGS_GET_TRANSMITTED_BUFFERS;
49 ZeroMem (Db->TxBuffer, sizeof (Db->TxBuffer));
50 }
51
52 if (InterruptStatusPtr != NULL) {
53 Snp->Cdb.OpFlags |= PXE_OPFLAGS_GET_INTERRUPT_STATUS;
54 }
55
56 if (Snp->MediaStatusSupported) {
57 Snp->Cdb.OpFlags |= PXE_OPFLAGS_GET_MEDIA_STATUS;
58 }
59
60 Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED;
61 Snp->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED;
62
63 Snp->Cdb.DBsize = (UINT16) sizeof (PXE_DB_GET_STATUS);
64 Snp->Cdb.DBaddr = (UINT64)(UINTN) Db;
65
66 Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE;
67 Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE;
68 Snp->Cdb.IFnum = Snp->IfNum;
69 Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST;
70
71 //
72 // Issue UNDI command and check result.
73 //
74 DEBUG ((EFI_D_NET, "\nSnp->undi.get_status() "));
75
76 (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb);
77
78 if (Snp->Cdb.StatCode != PXE_STATCODE_SUCCESS) {
79 DEBUG (
80 (EFI_D_NET,
81 "\nSnp->undi.get_status() %xh:%xh\n",
82 Snp->Cdb.StatFlags,
83 Snp->Cdb.StatCode)
84 );
85
86 return EFI_DEVICE_ERROR;
87 }
88 //
89 // report the values back..
90 //
91 if (InterruptStatusPtr != NULL) {
92 InterruptFlags = (UINT16) (Snp->Cdb.StatFlags & PXE_STATFLAGS_GET_STATUS_INTERRUPT_MASK);
93
94 *InterruptStatusPtr = 0;
95
96 if ((InterruptFlags & PXE_STATFLAGS_GET_STATUS_RECEIVE) == PXE_STATFLAGS_GET_STATUS_RECEIVE) {
97 *InterruptStatusPtr |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
98 }
99
100 if ((InterruptFlags & PXE_STATFLAGS_GET_STATUS_TRANSMIT) == PXE_STATFLAGS_GET_STATUS_TRANSMIT) {
101 *InterruptStatusPtr |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
102 }
103
104 if ((InterruptFlags & PXE_STATFLAGS_GET_STATUS_COMMAND) == PXE_STATFLAGS_GET_STATUS_COMMAND) {
105 *InterruptStatusPtr |= EFI_SIMPLE_NETWORK_COMMAND_INTERRUPT;
106 }
107
108 if ((InterruptFlags & PXE_STATFLAGS_GET_STATUS_SOFTWARE) == PXE_STATFLAGS_GET_STATUS_SOFTWARE) {
109 *InterruptStatusPtr |= EFI_SIMPLE_NETWORK_COMMAND_INTERRUPT;
110 }
111
112 }
113
114 if (GetTransmittedBuf) {
115 if ((Snp->Cdb.StatFlags & PXE_STATFLAGS_GET_STATUS_NO_TXBUFS_WRITTEN) == 0) {
116 //
117 // UNDI has written some transmitted buffer addresses into the DB. Store them into Snp->RecycledTxBuf.
118 //
119 for (Index = 0; Index < MAX_XMIT_BUFFERS; Index++) {
120 if (Db->TxBuffer[Index] != 0) {
121 if (Snp->RecycledTxBufCount == Snp->MaxRecycledTxBuf) {
122 //
123 // Snp->RecycledTxBuf is full, reallocate a new one.
124 //
125 if ((Snp->MaxRecycledTxBuf + SNP_TX_BUFFER_INCREASEMENT) >= SNP_MAX_TX_BUFFER_NUM) {
126 return EFI_DEVICE_ERROR;
127 }
128 Tmp = AllocatePool (sizeof (UINT64) * (Snp->MaxRecycledTxBuf + SNP_TX_BUFFER_INCREASEMENT));
129 if (Tmp == NULL) {
130 return EFI_DEVICE_ERROR;
131 }
132 CopyMem (Tmp, Snp->RecycledTxBuf, sizeof (UINT64) * Snp->RecycledTxBufCount);
133 FreePool (Snp->RecycledTxBuf);
134 Snp->RecycledTxBuf = Tmp;
135 Snp->MaxRecycledTxBuf += SNP_TX_BUFFER_INCREASEMENT;
136 }
137 Snp->RecycledTxBuf[Snp->RecycledTxBufCount] = Db->TxBuffer[Index];
138 Snp->RecycledTxBufCount++;
139 }
140 }
141 }
142 }
143
144 //
145 // Update MediaPresent field of EFI_SIMPLE_NETWORK_MODE if the UNDI support
146 // returning media status from GET_STATUS command
147 //
148 if (Snp->MediaStatusSupported) {
149 Snp->Snp.Mode->MediaPresent =
150 (BOOLEAN) (((Snp->Cdb.StatFlags & PXE_STATFLAGS_GET_STATUS_NO_MEDIA) != 0) ? FALSE : TRUE);
151 }
152
153 return EFI_SUCCESS;
154 }
155
156 /**
157 Reads the current interrupt status and recycled transmit buffer status from a
158 network interface.
159
160 This function gets the current interrupt and recycled transmit buffer status
161 from the network interface. The interrupt status is returned as a bit mask in
162 InterruptStatus. If InterruptStatus is NULL, the interrupt status will not be
163 read. If TxBuf is not NULL, a recycled transmit buffer address will be retrieved.
164 If a recycled transmit buffer address is returned in TxBuf, then the buffer has
165 been successfully transmitted, and the status for that buffer is cleared. If
166 the status of the network interface is successfully collected, EFI_SUCCESS
167 will be returned. If the driver has not been initialized, EFI_DEVICE_ERROR will
168 be returned.
169
170 @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance.
171 @param InterruptStatus A pointer to the bit mask of the currently active
172 interrupts (see "Related Definitions"). If this is NULL,
173 the interrupt status will not be read from the device.
174 If this is not NULL, the interrupt status will be read
175 from the device. When the interrupt status is read, it
176 will also be cleared. Clearing the transmit interrupt does
177 not empty the recycled transmit buffer array.
178 @param TxBuf Recycled transmit buffer address. The network interface
179 will not transmit if its internal recycled transmit
180 buffer array is full. Reading the transmit buffer does
181 not clear the transmit interrupt. If this is NULL, then
182 the transmit buffer status will not be read. If there
183 are no transmit buffers to recycle and TxBuf is not NULL,
184 TxBuf will be set to NULL.
185
186 @retval EFI_SUCCESS The status of the network interface was retrieved.
187 @retval EFI_NOT_STARTED The network interface has not been started.
188 @retval EFI_INVALID_PARAMETER This parameter was NULL or did not point to a valid
189 EFI_SIMPLE_NETWORK_PROTOCOL structure.
190 @retval EFI_DEVICE_ERROR The command could not be sent to the network
191 interface.
192
193 **/
194 EFI_STATUS
195 EFIAPI
196 SnpUndi32GetStatus (
197 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
198 OUT UINT32 *InterruptStatus, OPTIONAL
199 OUT VOID **TxBuf OPTIONAL
200 )
201 {
202 SNP_DRIVER *Snp;
203 EFI_TPL OldTpl;
204 EFI_STATUS Status;
205
206 if (This == NULL) {
207 return EFI_INVALID_PARAMETER;
208 }
209
210 if (InterruptStatus == NULL && TxBuf == 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 if (Snp == NULL) {
219 return EFI_DEVICE_ERROR;
220 }
221
222 switch (Snp->Mode.State) {
223 case EfiSimpleNetworkInitialized:
224 break;
225
226 case EfiSimpleNetworkStopped:
227 Status = EFI_NOT_STARTED;
228 goto ON_EXIT;
229
230 default:
231 Status = EFI_DEVICE_ERROR;
232 goto ON_EXIT;
233 }
234
235 if (Snp->RecycledTxBufCount == 0 && TxBuf != NULL) {
236 Status = PxeGetStatus (Snp, InterruptStatus, TRUE);
237 } else {
238 Status = PxeGetStatus (Snp, InterruptStatus, FALSE);
239 }
240
241 if (TxBuf != NULL) {
242 //
243 // Get a recycled buf from Snp->RecycledTxBuf
244 //
245 if (Snp->RecycledTxBufCount == 0) {
246 *TxBuf = NULL;
247 } else {
248 Snp->RecycledTxBufCount--;
249 *TxBuf = (VOID *) (UINTN) Snp->RecycledTxBuf[Snp->RecycledTxBufCount];
250 }
251 }
252
253 ON_EXIT:
254 gBS->RestoreTPL (OldTpl);
255
256 return Status;
257 }