]>
Commit | Line | Data |
---|---|---|
46f2c53b OM |
1 | /** @file\r |
2 | *\r | |
3 | * Copyright (c) 2012-2014, ARM Limited. All rights reserved.\r | |
4 | *\r | |
878b807a | 5 | * SPDX-License-Identifier: BSD-2-Clause-Patent\r |
46f2c53b OM |
6 | *\r |
7 | **/\r | |
8 | \r | |
9 | #include "Lan9118Dxe.h"\r | |
10 | \r | |
46f2c53b OM |
11 | typedef struct {\r |
12 | MAC_ADDR_DEVICE_PATH Lan9118;\r | |
13 | EFI_DEVICE_PATH_PROTOCOL End;\r | |
14 | } LAN9118_DEVICE_PATH;\r | |
15 | \r | |
16 | LAN9118_DEVICE_PATH Lan9118PathTemplate = {\r | |
17 | {\r | |
18 | {\r | |
19 | MESSAGING_DEVICE_PATH, MSG_MAC_ADDR_DP,\r | |
20 | { (UINT8) (sizeof(MAC_ADDR_DEVICE_PATH)), (UINT8) ((sizeof(MAC_ADDR_DEVICE_PATH)) >> 8) }\r | |
21 | },\r | |
b0fdce95 | 22 | { { 0 } },\r |
46f2c53b OM |
23 | 0\r |
24 | },\r | |
25 | {\r | |
26 | END_DEVICE_PATH_TYPE,\r | |
27 | END_ENTIRE_DEVICE_PATH_SUBTYPE,\r | |
b0fdce95 | 28 | { sizeof(EFI_DEVICE_PATH_PROTOCOL), 0 }\r |
46f2c53b OM |
29 | }\r |
30 | };\r | |
31 | \r | |
32 | /*\r | |
33 | ** Entry point for the LAN9118 driver\r | |
34 | **\r | |
35 | */\r | |
36 | EFI_STATUS\r | |
37 | Lan9118DxeEntry (\r | |
38 | IN EFI_HANDLE Handle,\r | |
39 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
40 | )\r | |
41 | {\r | |
42 | EFI_STATUS Status;\r | |
43 | LAN9118_DRIVER *LanDriver;\r | |
44 | EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r | |
45 | EFI_SIMPLE_NETWORK_MODE *SnpMode;\r | |
46 | LAN9118_DEVICE_PATH *Lan9118Path;\r | |
47 | EFI_HANDLE ControllerHandle;\r | |
48 | \r | |
49 | // The PcdLan9118DxeBaseAddress PCD must be defined\r | |
50 | ASSERT (PcdGet32 (PcdLan9118DxeBaseAddress) != 0);\r | |
51 | \r | |
52 | // Allocate Resources\r | |
53 | LanDriver = AllocateZeroPool (sizeof (LAN9118_DRIVER));\r | |
54 | if (LanDriver == NULL) {\r | |
55 | return EFI_OUT_OF_RESOURCES;\r | |
56 | }\r | |
57 | Lan9118Path = (LAN9118_DEVICE_PATH*)AllocateCopyPool (sizeof (LAN9118_DEVICE_PATH), &Lan9118PathTemplate);\r | |
58 | if (Lan9118Path == NULL) {\r | |
59 | return EFI_OUT_OF_RESOURCES;\r | |
60 | }\r | |
61 | \r | |
62 | // Initialize pointers\r | |
63 | Snp = &(LanDriver->Snp);\r | |
64 | SnpMode = &(LanDriver->SnpMode);\r | |
65 | Snp->Mode = SnpMode;\r | |
66 | \r | |
67 | // Set the signature of the LAN Driver structure\r | |
68 | LanDriver->Signature = LAN9118_SIGNATURE;\r | |
69 | \r | |
70 | // Assign fields and func pointers\r | |
71 | Snp->Revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;\r | |
72 | Snp->WaitForPacket = NULL;\r | |
73 | Snp->Initialize = SnpInitialize;\r | |
74 | Snp->Start = SnpStart;\r | |
75 | Snp->Stop = SnpStop;\r | |
76 | Snp->Reset = SnpReset;\r | |
77 | Snp->Shutdown = SnpShutdown;\r | |
78 | Snp->ReceiveFilters = SnpReceiveFilters;\r | |
79 | Snp->StationAddress = SnpStationAddress;\r | |
80 | Snp->Statistics = SnpStatistics;\r | |
81 | Snp->MCastIpToMac = SnpMcastIptoMac;\r | |
82 | Snp->NvData = SnpNvData;\r | |
83 | Snp->GetStatus = SnpGetStatus;\r | |
84 | Snp->Transmit = SnpTransmit;\r | |
85 | Snp->Receive = SnpReceive;\r | |
86 | \r | |
87 | // Start completing simple network mode structure\r | |
88 | SnpMode->State = EfiSimpleNetworkStopped;\r | |
89 | SnpMode->HwAddressSize = NET_ETHER_ADDR_LEN; // HW address is 6 bytes\r | |
90 | SnpMode->MediaHeaderSize = sizeof(ETHER_HEAD); // Not sure of this\r | |
91 | SnpMode->MaxPacketSize = EFI_PAGE_SIZE; // Preamble + SOF + Ether Frame (with VLAN tag +4bytes)\r | |
92 | SnpMode->NvRamSize = 0; // No NVRAM with this device\r | |
93 | SnpMode->NvRamAccessSize = 0; // No NVRAM with this device\r | |
94 | \r | |
11bbc257 RC |
95 | //\r |
96 | // Claim that all receive filter settings are supported, though the MULTICAST mode\r | |
97 | // is not completely supported. The LAN9118 Ethernet controller is only able to\r | |
98 | // do a "hash filtering" and not a perfect filtering on multicast addresses. The\r | |
99 | // controller does not filter the multicast addresses directly but a hash value\r | |
100 | // of them. The hash value of a multicast address is derived from its CRC and\r | |
101 | // ranges from 0 to 63 included.\r | |
102 | // We claim that the perfect MULTICAST filtering mode is supported because\r | |
103 | // we do not want the user to switch directly to the PROMISCOUS_MULTICAST mode\r | |
104 | // and thus not being able to take advantage of the hash filtering.\r | |
105 | //\r | |
106 | SnpMode->ReceiveFilterMask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |\r | |
107 | EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST |\r | |
108 | EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST |\r | |
109 | EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS |\r | |
110 | EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST;\r | |
111 | \r | |
112 | // We do not intend to receive anything for the time being.\r | |
113 | SnpMode->ReceiveFilterSetting = 0;\r | |
46f2c53b OM |
114 | \r |
115 | // LAN9118 has 64bit hash table, can filter 64 MCast MAC Addresses\r | |
116 | SnpMode->MaxMCastFilterCount = MAX_MCAST_FILTER_CNT;\r | |
117 | SnpMode->MCastFilterCount = 0;\r | |
118 | ZeroMem (&SnpMode->MCastFilter, MAX_MCAST_FILTER_CNT * sizeof(EFI_MAC_ADDRESS));\r | |
119 | \r | |
120 | // Set the interface type (1: Ethernet or 6: IEEE 802 Networks)\r | |
121 | SnpMode->IfType = NET_IFTYPE_ETHERNET;\r | |
122 | \r | |
123 | // Mac address is changeable as it is loaded from erasable memory\r | |
124 | SnpMode->MacAddressChangeable = TRUE;\r | |
125 | \r | |
126 | // Can only transmit one packet at a time\r | |
127 | SnpMode->MultipleTxSupported = FALSE;\r | |
128 | \r | |
129 | // MediaPresent checks for cable connection and partner link\r | |
130 | SnpMode->MediaPresentSupported = TRUE;\r | |
131 | SnpMode->MediaPresent = FALSE;\r | |
132 | \r | |
133 | // Set broadcast address\r | |
134 | SetMem (&SnpMode->BroadcastAddress, sizeof (EFI_MAC_ADDRESS), 0xFF);\r | |
135 | \r | |
136 | // Power up the device so we can find the MAC address\r | |
137 | Status = Lan9118Initialize (Snp);\r | |
138 | if (EFI_ERROR (Status)) {\r | |
c64aa7c4 | 139 | DEBUG ((EFI_D_ERROR, "LAN9118: Error initialising hardware\n"));\r |
46f2c53b OM |
140 | return EFI_DEVICE_ERROR;\r |
141 | }\r | |
142 | \r | |
143 | // Assign fields for device path\r | |
144 | CopyMem (&Lan9118Path->Lan9118.MacAddress, &Snp->Mode->CurrentAddress, NET_ETHER_ADDR_LEN);\r | |
145 | Lan9118Path->Lan9118.IfType = Snp->Mode->IfType;\r | |
146 | \r | |
147 | // Initialise the protocol\r | |
148 | ControllerHandle = NULL;\r | |
149 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
150 | &ControllerHandle,\r | |
151 | &gEfiSimpleNetworkProtocolGuid, Snp,\r | |
152 | &gEfiDevicePathProtocolGuid, Lan9118Path,\r | |
153 | NULL\r | |
154 | );\r | |
155 | // Say what the status of loading the protocol structure is\r | |
156 | if (EFI_ERROR(Status)) {\r | |
157 | FreePool (LanDriver);\r | |
158 | } else {\r | |
159 | LanDriver->ControllerHandle = ControllerHandle;\r | |
160 | }\r | |
161 | \r | |
162 | return Status;\r | |
163 | }\r | |
164 | \r | |
165 | /*\r | |
166 | * UEFI Start() function\r | |
167 | *\r | |
168 | * Parameters:\r | |
169 | *\r | |
170 | * @param Snp: A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance.\r | |
171 | *\r | |
172 | * Description:\r | |
173 | *\r | |
174 | * This function starts a network interface. If the network interface successfully starts, then\r | |
175 | * EFI_SUCCESS will be returned.\r | |
176 | */\r | |
177 | EFI_STATUS\r | |
178 | EFIAPI\r | |
179 | SnpStart (\r | |
0150e14d | 180 | IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp\r |
46f2c53b OM |
181 | )\r |
182 | {\r | |
183 | // Check Snp instance\r | |
184 | if (Snp == NULL) {\r | |
185 | return EFI_INVALID_PARAMETER;\r | |
186 | }\r | |
187 | \r | |
188 | // Check state\r | |
0150e14d RC |
189 | if ((Snp->Mode->State == EfiSimpleNetworkStarted) ||\r |
190 | (Snp->Mode->State == EfiSimpleNetworkInitialized) ) {\r | |
46f2c53b | 191 | return EFI_ALREADY_STARTED;\r |
46f2c53b OM |
192 | }\r |
193 | \r | |
194 | // Change state\r | |
195 | Snp->Mode->State = EfiSimpleNetworkStarted;\r | |
196 | return EFI_SUCCESS;\r | |
197 | }\r | |
198 | \r | |
199 | /*\r | |
200 | * UEFI Stop() function\r | |
201 | *\r | |
202 | */\r | |
203 | EFI_STATUS\r | |
204 | EFIAPI\r | |
205 | SnpStop (\r | |
206 | IN EFI_SIMPLE_NETWORK_PROTOCOL* Snp\r | |
207 | )\r | |
208 | {\r | |
209 | // Check Snp Instance\r | |
210 | if (Snp == NULL) {\r | |
211 | return EFI_INVALID_PARAMETER;\r | |
212 | }\r | |
213 | \r | |
214 | // Check state of the driver\r | |
0150e14d | 215 | if (Snp->Mode->State == EfiSimpleNetworkStopped) {\r |
46f2c53b OM |
216 | return EFI_NOT_STARTED;\r |
217 | }\r | |
218 | \r | |
219 | // Stop the Tx and Rx\r | |
220 | StopTx (STOP_TX_CFG | STOP_TX_MAC, Snp);\r | |
221 | StopRx (0, Snp);\r | |
222 | \r | |
223 | // Change the state\r | |
224 | switch (Snp->Mode->State) {\r | |
225 | case EfiSimpleNetworkStarted:\r | |
226 | case EfiSimpleNetworkInitialized:\r | |
227 | Snp->Mode->State = EfiSimpleNetworkStopped;\r | |
228 | break;\r | |
229 | default:\r | |
230 | return EFI_DEVICE_ERROR;\r | |
231 | }\r | |
232 | \r | |
233 | // Put the device into a power saving mode ?\r | |
234 | return EFI_SUCCESS;\r | |
235 | }\r | |
236 | \r | |
237 | \r | |
238 | // Allocated receive and transmit buffers\r | |
239 | STATIC UINT32 gTxBuffer = 0;\r | |
240 | \r | |
241 | /*\r | |
242 | * UEFI Initialize() function\r | |
243 | *\r | |
244 | */\r | |
245 | EFI_STATUS\r | |
246 | EFIAPI\r | |
247 | SnpInitialize (\r | |
248 | IN EFI_SIMPLE_NETWORK_PROTOCOL* Snp,\r | |
249 | IN UINTN RxBufferSize OPTIONAL,\r | |
250 | IN UINTN TxBufferSize OPTIONAL\r | |
251 | )\r | |
252 | {\r | |
253 | EFI_STATUS Status;\r | |
254 | UINT32 PmConf;\r | |
255 | INT32 AllocResult;\r | |
256 | UINT32 RxStatusSize;\r | |
257 | UINT32 TxStatusSize;\r | |
258 | \r | |
259 | // Initialize variables\r | |
260 | // Global variables to hold tx and rx FIFO allocation\r | |
261 | gTxBuffer = 0;\r | |
262 | \r | |
263 | // Check Snp Instance\r | |
264 | if (Snp == NULL) {\r | |
265 | return EFI_INVALID_PARAMETER;\r | |
266 | }\r | |
267 | \r | |
268 | // First check that driver has not already been initialized\r | |
269 | if (Snp->Mode->State == EfiSimpleNetworkInitialized) {\r | |
270 | DEBUG ((EFI_D_WARN, "LAN9118 Driver already initialized\n"));\r | |
271 | return EFI_SUCCESS;\r | |
272 | } else\r | |
273 | if (Snp->Mode->State == EfiSimpleNetworkStopped) {\r | |
274 | DEBUG ((EFI_D_WARN, "LAN9118 Driver not started\n"));\r | |
275 | return EFI_NOT_STARTED;\r | |
276 | }\r | |
277 | \r | |
278 | // Initiate a PHY reset\r | |
6382e5df | 279 | Status = PhySoftReset (PHY_RESET_PMT, Snp);\r |
42589b9a | 280 | if (EFI_ERROR (Status)) {\r |
46f2c53b OM |
281 | Snp->Mode->State = EfiSimpleNetworkStopped;\r |
282 | DEBUG ((EFI_D_WARN, "Warning: Link not ready after TimeOut. Check ethernet cable\n"));\r | |
283 | return EFI_NOT_STARTED;\r | |
284 | }\r | |
285 | \r | |
286 | // Initiate a software reset\r | |
287 | Status = SoftReset (0, Snp);\r | |
288 | if (EFI_ERROR(Status)) {\r | |
289 | DEBUG ((EFI_D_WARN, "Soft Reset Failed: Hardware Error\n"));\r | |
290 | return EFI_DEVICE_ERROR;\r | |
291 | }\r | |
292 | \r | |
293 | // Read the PM register\r | |
e68449c9 | 294 | PmConf = Lan9118MmioRead32 (LAN9118_PMT_CTRL);\r |
46f2c53b OM |
295 | \r |
296 | // MPTCTRL_WOL_EN: Allow Wake-On-Lan to detect wake up frames or magic packets\r | |
297 | // MPTCTRL_ED_EN: Allow energy detection to allow lowest power consumption mode\r | |
298 | // MPTCTRL_PME_EN: Allow Power Management Events\r | |
299 | PmConf = 0;\r | |
300 | PmConf |= (MPTCTRL_WOL_EN | MPTCTRL_ED_EN | MPTCTRL_PME_EN);\r | |
301 | \r | |
302 | // Write the current configuration to the register\r | |
e68449c9 | 303 | Lan9118MmioWrite32 (LAN9118_PMT_CTRL, PmConf);\r |
46f2c53b OM |
304 | \r |
305 | // Configure GPIO and HW\r | |
306 | Status = ConfigureHardware (HW_CONF_USE_LEDS, Snp);\r | |
307 | if (EFI_ERROR(Status)) {\r | |
308 | return Status;\r | |
309 | }\r | |
310 | \r | |
311 | // Assign the transmitter buffer size (default values)\r | |
312 | TxStatusSize = LAN9118_TX_STATUS_SIZE;\r | |
313 | RxStatusSize = LAN9118_RX_STATUS_SIZE;\r | |
314 | \r | |
315 | // Check that a buff size was specified\r | |
316 | if (TxBufferSize > 0) {\r | |
317 | if (RxBufferSize == 0) {\r | |
318 | RxBufferSize = LAN9118_RX_DATA_SIZE;\r | |
319 | }\r | |
320 | \r | |
321 | AllocResult = ChangeFifoAllocation (\r | |
322 | ALLOC_USE_FIFOS,\r | |
323 | &TxBufferSize,\r | |
324 | &RxBufferSize,\r | |
325 | &TxStatusSize,\r | |
326 | &RxStatusSize,\r | |
327 | Snp\r | |
328 | );\r | |
329 | \r | |
330 | if (AllocResult < 0) {\r | |
331 | return EFI_OUT_OF_RESOURCES;\r | |
332 | }\r | |
333 | }\r | |
334 | \r | |
335 | // Do auto-negotiation if supported\r | |
336 | Status = AutoNegotiate (AUTO_NEGOTIATE_ADVERTISE_ALL, Snp);\r | |
337 | if (EFI_ERROR(Status)) {\r | |
c64aa7c4 | 338 | DEBUG ((EFI_D_WARN, "LAN9118: Auto Negotiation failed.\n"));\r |
46f2c53b OM |
339 | }\r |
340 | \r | |
341 | // Configure flow control depending on speed capabilities\r | |
342 | Status = ConfigureFlow (0, 0, 0, 0, Snp);\r | |
343 | if (EFI_ERROR(Status)) {\r | |
344 | return Status;\r | |
345 | }\r | |
346 | \r | |
11bbc257 | 347 | // Enable the transmitter\r |
46f2c53b OM |
348 | Status = StartTx (START_TX_MAC | START_TX_CFG, Snp);\r |
349 | if (EFI_ERROR(Status)) {\r | |
350 | return Status;\r | |
351 | }\r | |
352 | \r | |
353 | // Now acknowledge all interrupts\r | |
e68449c9 | 354 | Lan9118MmioWrite32 (LAN9118_INT_STS, ~0);\r |
46f2c53b OM |
355 | \r |
356 | // Declare the driver as initialized\r | |
357 | Snp->Mode->State = EfiSimpleNetworkInitialized;\r | |
358 | \r | |
359 | return Status;\r | |
360 | }\r | |
361 | \r | |
362 | /*\r | |
363 | * UEFI Reset () function\r | |
364 | *\r | |
365 | */\r | |
366 | EFI_STATUS\r | |
367 | EFIAPI\r | |
368 | SnpReset (\r | |
369 | IN EFI_SIMPLE_NETWORK_PROTOCOL* Snp,\r | |
370 | IN BOOLEAN Verification\r | |
371 | )\r | |
372 | {\r | |
42589b9a OM |
373 | UINT32 PmConf;\r |
374 | UINT32 HwConf;\r | |
375 | UINT32 ResetFlags;\r | |
376 | EFI_STATUS Status;\r | |
46f2c53b OM |
377 | \r |
378 | PmConf = 0;\r | |
379 | HwConf = 0;\r | |
380 | ResetFlags = 0;\r | |
381 | \r | |
382 | // Check Snp Instance\r | |
383 | if (Snp == NULL) {\r | |
384 | return EFI_INVALID_PARAMETER;\r | |
385 | }\r | |
386 | \r | |
387 | // First check that driver has not already been initialized\r | |
388 | if (Snp->Mode->State == EfiSimpleNetworkStarted) {\r | |
389 | DEBUG ((EFI_D_WARN, "Warning: LAN9118 Driver not yet initialized\n"));\r | |
390 | return EFI_DEVICE_ERROR;\r | |
391 | } else if (Snp->Mode->State == EfiSimpleNetworkStopped) {\r | |
392 | DEBUG ((EFI_D_WARN, "Warning: LAN9118 Driver not started\n"));\r | |
393 | return EFI_NOT_STARTED;\r | |
394 | }\r | |
395 | \r | |
396 | // Initiate a PHY reset\r | |
6382e5df | 397 | Status = PhySoftReset (PHY_RESET_PMT, Snp);\r |
42589b9a | 398 | if (EFI_ERROR (Status)) {\r |
46f2c53b OM |
399 | Snp->Mode->State = EfiSimpleNetworkStopped;\r |
400 | return EFI_NOT_STARTED;\r | |
401 | }\r | |
402 | \r | |
403 | // Initiate a software reset\r | |
404 | ResetFlags |= SOFT_RESET_CHECK_MAC_ADDR_LOAD | SOFT_RESET_CLEAR_INT;\r | |
405 | \r | |
406 | if (Verification) {\r | |
407 | ResetFlags |= SOFT_RESET_SELF_TEST;\r | |
408 | }\r | |
409 | \r | |
42589b9a OM |
410 | Status = SoftReset (ResetFlags, Snp);\r |
411 | if (EFI_ERROR (Status)) {\r | |
46f2c53b OM |
412 | DEBUG ((EFI_D_WARN, "Warning: Soft Reset Failed: Hardware Error\n"));\r |
413 | return EFI_DEVICE_ERROR;\r | |
414 | }\r | |
415 | \r | |
416 | // Read the PM register\r | |
e68449c9 | 417 | PmConf = Lan9118MmioRead32 (LAN9118_PMT_CTRL);\r |
46f2c53b OM |
418 | \r |
419 | // MPTCTRL_WOL_EN: Allow Wake-On-Lan to detect wake up frames or magic packets\r | |
420 | // MPTCTRL_ED_EN: Allow energy detection to allow lowest power consumption mode\r | |
421 | // MPTCTRL_PME_EN: Allow Power Management Events\r | |
422 | PmConf |= (MPTCTRL_WOL_EN | MPTCTRL_ED_EN | MPTCTRL_PME_EN);\r | |
423 | \r | |
424 | // Write the current configuration to the register\r | |
e68449c9 | 425 | Lan9118MmioWrite32 (LAN9118_PMT_CTRL, PmConf);\r |
46f2c53b | 426 | \r |
ac8f1e10 RC |
427 | // Reactivate the LEDs\r |
428 | Status = ConfigureHardware (HW_CONF_USE_LEDS, Snp);\r | |
429 | if (EFI_ERROR (Status)) {\r | |
430 | return Status;\r | |
431 | }\r | |
432 | \r | |
46f2c53b OM |
433 | // Check that a buffer size was specified in SnpInitialize\r |
434 | if (gTxBuffer != 0) {\r | |
e68449c9 | 435 | HwConf = Lan9118MmioRead32 (LAN9118_HW_CFG); // Read the HW register\r |
46f2c53b OM |
436 | HwConf &= ~HW_CFG_TX_FIFO_SIZE_MASK; // Clear buffer bits first\r |
437 | HwConf |= HW_CFG_TX_FIFO_SIZE(gTxBuffer); // assign size chosen in SnpInitialize\r | |
438 | \r | |
e68449c9 | 439 | Lan9118MmioWrite32 (LAN9118_HW_CFG, HwConf); // Write the conf\r |
46f2c53b OM |
440 | }\r |
441 | \r | |
442 | // Enable the receiver and transmitter and clear their contents\r | |
443 | StartRx (START_RX_CLEAR, Snp);\r | |
444 | StartTx (START_TX_MAC | START_TX_CFG | START_TX_CLEAR, Snp);\r | |
445 | \r | |
446 | // Now acknowledge all interrupts\r | |
e68449c9 | 447 | Lan9118MmioWrite32 (LAN9118_INT_STS, ~0);\r |
46f2c53b OM |
448 | \r |
449 | return EFI_SUCCESS;\r | |
450 | }\r | |
451 | \r | |
452 | /*\r | |
453 | * UEFI Shutdown () function\r | |
454 | *\r | |
455 | */\r | |
456 | EFI_STATUS\r | |
457 | EFIAPI\r | |
458 | SnpShutdown (\r | |
459 | IN EFI_SIMPLE_NETWORK_PROTOCOL* Snp\r | |
460 | )\r | |
461 | {\r | |
42589b9a OM |
462 | EFI_STATUS Status;\r |
463 | \r | |
46f2c53b OM |
464 | // Check Snp Instance\r |
465 | if (Snp == NULL) {\r | |
466 | return EFI_INVALID_PARAMETER;\r | |
467 | }\r | |
468 | \r | |
469 | // First check that driver has not already been initialized\r | |
470 | if (Snp->Mode->State == EfiSimpleNetworkStarted) {\r | |
471 | DEBUG ((EFI_D_WARN, "Warning: LAN9118 Driver not yet initialized\n"));\r | |
472 | return EFI_DEVICE_ERROR;\r | |
473 | } else if (Snp->Mode->State == EfiSimpleNetworkStopped) {\r | |
0150e14d | 474 | DEBUG ((EFI_D_WARN, "Warning: LAN9118 Driver not started\n"));\r |
46f2c53b OM |
475 | return EFI_NOT_STARTED;\r |
476 | }\r | |
477 | \r | |
478 | // Initiate a PHY reset\r | |
42589b9a OM |
479 | Status = PhySoftReset (PHY_RESET_PMT, Snp);\r |
480 | if (EFI_ERROR (Status)) {\r | |
481 | return Status;\r | |
482 | }\r | |
46f2c53b OM |
483 | \r |
484 | // Initiate a software reset\r | |
42589b9a OM |
485 | Status = SoftReset (0, Snp);\r |
486 | if (EFI_ERROR (Status)) {\r | |
46f2c53b | 487 | DEBUG ((EFI_D_WARN, "Warning: Soft Reset Failed: Hardware Error\n"));\r |
42589b9a | 488 | return Status;\r |
46f2c53b OM |
489 | }\r |
490 | \r | |
0f0a6fe9 RC |
491 | // Back to the started and thus not initialized state\r |
492 | Snp->Mode->State = EfiSimpleNetworkStarted;\r | |
493 | \r | |
46f2c53b OM |
494 | return EFI_SUCCESS;\r |
495 | }\r | |
496 | \r | |
0150e14d RC |
497 | /**\r |
498 | Enable and/or disable the receive filters of the LAN9118\r | |
499 | \r | |
500 | Please refer to the UEFI specification for the precedence rules among the\r | |
501 | Enable, Disable and ResetMCastFilter parameters.\r | |
502 | \r | |
503 | @param[in] Snp A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL\r | |
504 | instance.\r | |
505 | @param[in] Enable A bit mask of receive filters to enable.\r | |
506 | @param[in] Disable A bit mask of receive filters to disable.\r | |
507 | @param[in] ResetMCastFilter Set to TRUE to reset the contents of the multicast\r | |
508 | receive filters on the network interface to\r | |
509 | their default values.\r | |
510 | @param[in] MCastFilterCnt Number of multicast HW MAC addresses in the new\r | |
511 | MCastFilter list. This value must be less than or\r | |
512 | equal to the MCastFilterCnt field of\r | |
513 | EFI_SIMPLE_NETWORK_MODE. This field is optional if\r | |
514 | ResetMCastFilter is TRUE.\r | |
515 | @param[in] MCastFilter A pointer to a list of new multicast receive\r | |
516 | filter HW MAC addresses. This list will replace\r | |
517 | any existing multicast HW MAC address list. This\r | |
518 | field is optional if ResetMCastFilter is TRUE.\r | |
519 | \r | |
520 | @retval EFI_SUCCESS The receive filters of the LAN9118 were updated.\r | |
521 | @retval EFI_NOT_STARTED The LAN9118 has not been started.\r | |
522 | @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE :\r | |
523 | . This is NULL\r | |
524 | . Multicast is being enabled (the\r | |
525 | EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST bit is set in\r | |
526 | Enable, it is not set in Disable, and ResetMCastFilter\r | |
527 | is FALSE) and MCastFilterCount is zero.\r | |
528 | . Multicast is being enabled and MCastFilterCount is\r | |
529 | greater than Snp->Mode->MaxMCastFilterCount.\r | |
530 | . Multicast is being enabled and MCastFilter is NULL\r | |
531 | . Multicast is being enabled and one or more of the\r | |
532 | addresses in the MCastFilter list are not valid\r | |
533 | multicast MAC addresses.\r | |
534 | @retval EFI_DEVICE_ERROR The LAN9118 has been started but not initialized.\r | |
46f2c53b | 535 | \r |
0150e14d | 536 | **/\r |
46f2c53b OM |
537 | EFI_STATUS\r |
538 | EFIAPI\r | |
539 | SnpReceiveFilters (\r | |
0150e14d RC |
540 | IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp,\r |
541 | IN UINT32 Enable,\r | |
542 | IN UINT32 Disable,\r | |
543 | IN BOOLEAN ResetMCastFilter,\r | |
544 | IN UINTN MCastFilterCnt OPTIONAL,\r | |
545 | IN EFI_MAC_ADDRESS *MCastFilter OPTIONAL\r | |
46f2c53b OM |
546 | )\r |
547 | {\r | |
0150e14d RC |
548 | EFI_SIMPLE_NETWORK_MODE *Mode;\r |
549 | UINT32 MultHashTableHigh;\r | |
550 | UINT32 MultHashTableLow;\r | |
551 | UINT32 Count;\r | |
552 | UINT32 Crc;\r | |
553 | UINT8 HashValue;\r | |
554 | UINT32 MacCSRValue;\r | |
11bbc257 | 555 | UINT32 ReceiveFilterSetting;\r |
0150e14d | 556 | EFI_MAC_ADDRESS *Mac;\r |
11bbc257 | 557 | EFI_MAC_ADDRESS ZeroMac;\r |
46f2c53b | 558 | \r |
0150e14d RC |
559 | // Check Snp Instance\r |
560 | if (Snp == NULL) {\r | |
561 | return EFI_INVALID_PARAMETER;\r | |
562 | }\r | |
563 | Mode = Snp->Mode;\r | |
46f2c53b OM |
564 | \r |
565 | // Check that driver was started and initialised\r | |
0150e14d | 566 | if (Mode->State == EfiSimpleNetworkStarted) {\r |
46f2c53b OM |
567 | DEBUG ((EFI_D_WARN, "Warning: LAN9118 Driver not initialized\n"));\r |
568 | return EFI_DEVICE_ERROR;\r | |
0150e14d | 569 | } else if (Mode->State == EfiSimpleNetworkStopped) {\r |
46f2c53b OM |
570 | DEBUG ((EFI_D_WARN, "Warning: LAN9118 Driver in stopped state\n"));\r |
571 | return EFI_NOT_STARTED;\r | |
572 | }\r | |
573 | \r | |
0150e14d RC |
574 | if ((Enable & (~Mode->ReceiveFilterMask)) ||\r |
575 | (Disable & (~Mode->ReceiveFilterMask)) ) {\r | |
576 | return EFI_INVALID_PARAMETER;\r | |
46f2c53b OM |
577 | }\r |
578 | \r | |
0150e14d RC |
579 | //\r |
580 | // Check the validity of the multicast setting and compute the\r | |
581 | // hash values of the multicast mac addresses to listen to.\r | |
582 | //\r | |
46f2c53b | 583 | \r |
0150e14d RC |
584 | MultHashTableHigh = 0;\r |
585 | MultHashTableLow = 0;\r | |
586 | if ((!ResetMCastFilter) &&\r | |
587 | ((Disable & EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST) == 0) &&\r | |
588 | ((Enable & EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST) != 0) ) {\r | |
589 | if ((MCastFilterCnt == 0) ||\r | |
590 | (MCastFilterCnt > Mode->MaxMCastFilterCount) ||\r | |
591 | (MCastFilter == NULL) ) {\r | |
592 | return EFI_INVALID_PARAMETER;\r | |
593 | }\r | |
594 | //\r | |
595 | // Check the validity of all multicast addresses before to change\r | |
596 | // anything.\r | |
597 | //\r | |
598 | for (Count = 0; Count < MCastFilterCnt; Count++) {\r | |
599 | if ((MCastFilter[Count].Addr[0] & 1) == 0) {\r | |
600 | return EFI_INVALID_PARAMETER;\r | |
601 | }\r | |
602 | }\r | |
46f2c53b | 603 | \r |
0150e14d | 604 | //\r |
46f2c53b | 605 | // Go through each filter address and set appropriate bits on hash table\r |
0150e14d RC |
606 | //\r |
607 | for (Count = 0; Count < MCastFilterCnt; Count++) {\r | |
608 | Mac = &(MCastFilter[Count]);\r | |
609 | CopyMem (&Mode->MCastFilter[Count], Mac, sizeof(EFI_MAC_ADDRESS));\r | |
46f2c53b | 610 | \r |
0150e14d | 611 | Crc = GenEtherCrc32 (Mac, NET_ETHER_ADDR_LEN);\r |
46f2c53b OM |
612 | //gBS->CalculateCrc32 ((VOID*)&Mfilter[Count],6,&Crc); <-- doesn't work as desired\r |
613 | \r | |
0150e14d RC |
614 | //\r |
615 | // The most significant 6 bits of the MAC address CRC constitute the hash\r | |
616 | // value of the MAC address.\r | |
617 | //\r | |
618 | HashValue = (Crc >> 26) & 0x3F;\r | |
46f2c53b OM |
619 | \r |
620 | // Select hashlow register if MSB is not set\r | |
0150e14d RC |
621 | if ((HashValue & 0x20) == 0) {\r |
622 | MultHashTableLow |= (1 << HashValue);\r | |
46f2c53b | 623 | } else {\r |
0150e14d | 624 | MultHashTableHigh |= (1 << (HashValue & 0x1F));\r |
46f2c53b OM |
625 | }\r |
626 | }\r | |
0150e14d RC |
627 | Mode->MCastFilterCount = MCastFilterCnt;\r |
628 | } else if (ResetMCastFilter) {\r | |
629 | Mode->MCastFilterCount = 0;\r | |
630 | } else {\r | |
631 | MultHashTableLow = IndirectMACRead32 (INDIRECT_MAC_INDEX_HASHL);\r | |
632 | MultHashTableHigh = IndirectMACRead32 (INDIRECT_MAC_INDEX_HASHH);\r | |
46f2c53b OM |
633 | }\r |
634 | \r | |
11bbc257 RC |
635 | //\r |
636 | // Before to change anything, stop and reset the reception of\r | |
637 | // packets.\r | |
638 | //\r | |
639 | StopRx (STOP_RX_CLEAR, Snp);\r | |
640 | \r | |
0150e14d RC |
641 | //\r |
642 | // Write the mask of the selected hash values for the multicast filtering.\r | |
643 | // The two masks are set to zero if the multicast filtering is not enabled.\r | |
644 | //\r | |
645 | IndirectMACWrite32 (INDIRECT_MAC_INDEX_HASHL, MultHashTableLow);\r | |
646 | IndirectMACWrite32 (INDIRECT_MAC_INDEX_HASHH, MultHashTableHigh);\r | |
647 | \r | |
11bbc257 RC |
648 | ReceiveFilterSetting = (Mode->ReceiveFilterSetting | Enable) & (~Disable);\r |
649 | \r | |
650 | //\r | |
46f2c53b | 651 | // Read MAC controller\r |
11bbc257 RC |
652 | //\r |
653 | MacCSRValue = IndirectMACRead32 (INDIRECT_MAC_INDEX_CR);\r | |
654 | MacCSRValue &= ~(MACCR_HPFILT | MACCR_BCAST | MACCR_PRMS | MACCR_MCPAS);\r | |
46f2c53b | 655 | \r |
11bbc257 RC |
656 | if (ReceiveFilterSetting & EFI_SIMPLE_NETWORK_RECEIVE_UNICAST) {\r |
657 | Lan9118SetMacAddress (&Mode->CurrentAddress, Snp);\r | |
46f2c53b | 658 | DEBUG ((DEBUG_NET, "Allowing Unicast Frame Reception\n"));\r |
11bbc257 RC |
659 | } else {\r |
660 | //\r | |
661 | // The Unicast packets do not have to be listen to, set the MAC\r | |
662 | // address of the LAN9118 to be the "not configured" all zeroes\r | |
663 | // ethernet MAC address.\r | |
664 | //\r | |
665 | ZeroMem (&ZeroMac, NET_ETHER_ADDR_LEN);\r | |
666 | Lan9118SetMacAddress (&ZeroMac, Snp);\r | |
46f2c53b OM |
667 | }\r |
668 | \r | |
11bbc257 | 669 | if (ReceiveFilterSetting & EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST) {\r |
46f2c53b OM |
670 | MacCSRValue |= MACCR_HPFILT;\r |
671 | DEBUG ((DEBUG_NET, "Allowing Multicast Frame Reception\n"));\r | |
672 | }\r | |
673 | \r | |
11bbc257 RC |
674 | if (ReceiveFilterSetting & EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST) {\r |
675 | MacCSRValue |= MACCR_MCPAS;\r | |
676 | DEBUG ((DEBUG_NET, "Enabling Promiscuous Multicast Mode\n"));\r | |
46f2c53b OM |
677 | }\r |
678 | \r | |
11bbc257 | 679 | if ((ReceiveFilterSetting & EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST) == 0) {\r |
46f2c53b | 680 | MacCSRValue |= MACCR_BCAST;\r |
11bbc257 RC |
681 | } else {\r |
682 | DEBUG ((DEBUG_NET, "Allowing Broadcast Frame Reception\n"));\r | |
46f2c53b OM |
683 | }\r |
684 | \r | |
11bbc257 | 685 | if (ReceiveFilterSetting & EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS) {\r |
46f2c53b OM |
686 | MacCSRValue |= MACCR_PRMS;\r |
687 | DEBUG ((DEBUG_NET, "Enabling Promiscuous Mode\n"));\r | |
688 | }\r | |
689 | \r | |
11bbc257 | 690 | //\r |
46f2c53b | 691 | // Write the options to the MAC_CSR\r |
11bbc257 | 692 | //\r |
46f2c53b | 693 | IndirectMACWrite32 (INDIRECT_MAC_INDEX_CR, MacCSRValue);\r |
46f2c53b | 694 | \r |
11bbc257 RC |
695 | //\r |
696 | // If we have to retrieve something, start packet reception.\r | |
697 | //\r | |
698 | Mode->ReceiveFilterSetting = ReceiveFilterSetting;\r | |
699 | if (ReceiveFilterSetting != 0) {\r | |
700 | StartRx (0, Snp);\r | |
701 | }\r | |
702 | \r | |
46f2c53b OM |
703 | return EFI_SUCCESS;\r |
704 | }\r | |
705 | \r | |
0150e14d RC |
706 | /**\r |
707 | Modify of reset the current station address\r | |
708 | \r | |
709 | @param[in] Snp A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL\r | |
710 | instance.\r | |
711 | @param[in] Reset Flag used to reset the station address to the\r | |
712 | LAN9118's permanent address.\r | |
713 | @param[in] New New station address to be used for the network interface.\r | |
714 | \r | |
715 | @retval EFI_SUCCESS The LAN9118's station address was updated.\r | |
716 | @retval EFI_NOT_STARTED The LAN9118 has not been started.\r | |
717 | @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE :\r | |
718 | . The "New" station address is invalid.\r | |
719 | . "Reset" is FALSE and "New" is NULL.\r | |
720 | @retval EFI_DEVICE_ERROR The LAN9118 has been started but not initialized.\r | |
721 | \r | |
722 | **/\r | |
46f2c53b OM |
723 | EFI_STATUS\r |
724 | EFIAPI\r | |
725 | SnpStationAddress (\r | |
0150e14d RC |
726 | IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp,\r |
727 | IN BOOLEAN Reset,\r | |
728 | IN EFI_MAC_ADDRESS *New\r | |
46f2c53b OM |
729 | )\r |
730 | {\r | |
46f2c53b | 731 | UINT32 Count;\r |
0150e14d | 732 | UINT8 PermAddr[NET_ETHER_ADDR_LEN];\r |
46f2c53b | 733 | \r |
0150e14d | 734 | DEBUG ((DEBUG_NET, "SnpStationAddress()\n"));\r |
46f2c53b OM |
735 | \r |
736 | // Check Snp instance\r | |
737 | if (Snp == NULL) {\r | |
738 | return EFI_INVALID_PARAMETER;\r | |
739 | }\r | |
740 | \r | |
741 | // Check that driver was started and initialised\r | |
742 | if (Snp->Mode->State == EfiSimpleNetworkStarted) {\r | |
743 | DEBUG ((EFI_D_WARN, "Warning: LAN9118 Driver not initialized\n"));\r | |
744 | return EFI_DEVICE_ERROR;\r | |
745 | } else if (Snp->Mode->State == EfiSimpleNetworkStopped) {\r | |
746 | DEBUG ((EFI_D_WARN, "Warning: LAN9118 Driver in stopped state\n"));\r | |
747 | return EFI_NOT_STARTED;\r | |
748 | }\r | |
749 | \r | |
750 | // Get the Permanent MAC address if need reset\r | |
751 | if (Reset) {\r | |
752 | // Try using EEPROM first. Read the first byte of data from EEPROM at the address 0x0\r | |
753 | if ((IndirectEEPROMRead32 (0) & 0xFF) == EEPROM_EXTERNAL_SERIAL_EEPROM) {\r | |
0150e14d RC |
754 | for (Count = 0; Count < NET_ETHER_ADDR_LEN; Count++) {\r |
755 | PermAddr[Count] = IndirectEEPROMRead32 (Count + 1);\r | |
46f2c53b | 756 | }\r |
0150e14d | 757 | New = (EFI_MAC_ADDRESS *) PermAddr;\r |
46f2c53b OM |
758 | Lan9118SetMacAddress ((EFI_MAC_ADDRESS *) PermAddr, Snp);\r |
759 | } else {\r | |
c64aa7c4 | 760 | DEBUG ((EFI_D_ERROR, "LAN9118: Warning: No valid MAC address in EEPROM, using fallback\n"));\r |
0150e14d | 761 | New = (EFI_MAC_ADDRESS*) (FixedPcdGet64 (PcdLan9118DefaultMacAddress));\r |
46f2c53b OM |
762 | }\r |
763 | } else {\r | |
764 | // Otherwise use the specified new MAC address\r | |
0150e14d RC |
765 | if (New == NULL) {\r |
766 | return EFI_INVALID_PARAMETER;\r | |
767 | }\r | |
768 | //\r | |
769 | // If it is a multicast address, it is not valid.\r | |
770 | //\r | |
771 | if (New->Addr[0] & 0x01) {\r | |
46f2c53b OM |
772 | return EFI_INVALID_PARAMETER;\r |
773 | }\r | |
46f2c53b OM |
774 | }\r |
775 | \r | |
11bbc257 RC |
776 | CopyMem (&Snp->Mode->CurrentAddress, New, NET_ETHER_ADDR_LEN);\r |
777 | \r | |
778 | //\r | |
779 | // If packet reception is currently activated, stop and reset it,\r | |
780 | // set the new ethernet address and restart the packet reception.\r | |
781 | // Otherwise, nothing to do, the MAC address will be updated in\r | |
782 | // SnpReceiveFilters() when the UNICAST packet reception will be\r | |
783 | // activated.\r | |
784 | //\r | |
785 | if (Snp->Mode->ReceiveFilterSetting != 0) {\r | |
786 | StopRx (STOP_RX_CLEAR, Snp);\r | |
787 | Lan9118SetMacAddress (New, Snp);\r | |
788 | StartRx (0, Snp);\r | |
789 | }\r | |
0150e14d | 790 | \r |
46f2c53b OM |
791 | return EFI_SUCCESS;\r |
792 | }\r | |
793 | \r | |
794 | /*\r | |
795 | * UEFI Statistics() function\r | |
796 | *\r | |
797 | */\r | |
798 | EFI_STATUS\r | |
799 | EFIAPI\r | |
800 | SnpStatistics (\r | |
801 | IN EFI_SIMPLE_NETWORK_PROTOCOL* Snp,\r | |
802 | IN BOOLEAN Reset,\r | |
803 | IN OUT UINTN *StatSize,\r | |
804 | OUT EFI_NETWORK_STATISTICS *Statistics\r | |
805 | )\r | |
806 | {\r | |
0150e14d RC |
807 | LAN9118_DRIVER *LanDriver;\r |
808 | EFI_STATUS Status;\r | |
46f2c53b OM |
809 | \r |
810 | LanDriver = INSTANCE_FROM_SNP_THIS (Snp);\r | |
811 | \r | |
812 | DEBUG ((DEBUG_NET, "SnpStatistics()\n"));\r | |
813 | \r | |
814 | // Check Snp instance\r | |
815 | if (Snp == NULL) {\r | |
816 | return EFI_INVALID_PARAMETER;\r | |
817 | }\r | |
818 | \r | |
819 | // Check that driver was started and initialised\r | |
820 | if (Snp->Mode->State == EfiSimpleNetworkStarted) {\r | |
821 | DEBUG ((EFI_D_WARN, "Warning: LAN9118 Driver not initialized\n"));\r | |
822 | return EFI_DEVICE_ERROR;\r | |
823 | } else if (Snp->Mode->State == EfiSimpleNetworkStopped) {\r | |
824 | DEBUG ((EFI_D_WARN, "Warning: LAN9118 Driver in stopped state\n"));\r | |
825 | return EFI_NOT_STARTED;\r | |
826 | }\r | |
827 | \r | |
0150e14d RC |
828 | //\r |
829 | // Do a reset if required. It is not clearly stated in the UEFI specification\r | |
830 | // whether the reset has to be done before to copy the statistics in "Statictics"\r | |
831 | // or after. It is a bit strange to do it before but that is what is expected by\r | |
832 | // the SCT test on Statistics() with reset : "0x3de76704,0x4bf5,0x42cd,0x8c,0x89,\r | |
833 | // 0x54,0x7e,0x4f,0xad,0x4f,0x24".\r | |
834 | //\r | |
46f2c53b OM |
835 | if (Reset) {\r |
836 | ZeroMem (&LanDriver->Stats, sizeof(EFI_NETWORK_STATISTICS));\r | |
837 | }\r | |
838 | \r | |
0150e14d RC |
839 | Status = EFI_SUCCESS;\r |
840 | if (StatSize == NULL) {\r | |
841 | if (Statistics != NULL) {\r | |
842 | return EFI_INVALID_PARAMETER;\r | |
843 | }\r | |
844 | } else {\r | |
845 | if (Statistics == NULL) {\r | |
846 | Status = EFI_BUFFER_TOO_SMALL;\r | |
847 | } else {\r | |
848 | // Fill in the statistics\r | |
849 | CopyMem (\r | |
850 | Statistics, &LanDriver->Stats,\r | |
851 | MIN (*StatSize, sizeof (EFI_NETWORK_STATISTICS))\r | |
852 | );\r | |
853 | if (*StatSize < sizeof (EFI_NETWORK_STATISTICS)) {\r | |
854 | Status = EFI_BUFFER_TOO_SMALL;\r | |
855 | }\r | |
856 | }\r | |
857 | *StatSize = sizeof (EFI_NETWORK_STATISTICS);\r | |
46f2c53b OM |
858 | }\r |
859 | \r | |
0150e14d | 860 | return Status;\r |
46f2c53b OM |
861 | }\r |
862 | \r | |
863 | /*\r | |
864 | * UEFI MCastIPtoMAC() function\r | |
865 | *\r | |
866 | */\r | |
867 | EFI_STATUS\r | |
868 | EFIAPI\r | |
869 | SnpMcastIptoMac (\r | |
870 | IN EFI_SIMPLE_NETWORK_PROTOCOL* Snp,\r | |
871 | IN BOOLEAN IsIpv6,\r | |
872 | IN EFI_IP_ADDRESS *Ip,\r | |
873 | OUT EFI_MAC_ADDRESS *McastMac\r | |
874 | )\r | |
875 | {\r | |
876 | DEBUG ((DEBUG_NET, "SnpMcastIptoMac()\n"));\r | |
877 | \r | |
878 | // Check Snp instance\r | |
879 | if (Snp == NULL) {\r | |
880 | return EFI_INVALID_PARAMETER;\r | |
881 | }\r | |
882 | \r | |
883 | // Check that driver was started and initialised\r | |
884 | if (Snp->Mode->State == EfiSimpleNetworkStarted) {\r | |
885 | DEBUG ((EFI_D_WARN, "Warning: LAN9118 Driver not initialized\n"));\r | |
886 | return EFI_DEVICE_ERROR;\r | |
887 | } else if (Snp->Mode->State == EfiSimpleNetworkStopped) {\r | |
888 | DEBUG ((EFI_D_WARN, "Warning: LAN9118 Driver in stopped state\n"));\r | |
889 | return EFI_NOT_STARTED;\r | |
890 | }\r | |
891 | \r | |
892 | // Check parameters\r | |
893 | if ((McastMac == NULL) || (Ip == NULL)) {\r | |
894 | return EFI_INVALID_PARAMETER;\r | |
895 | }\r | |
896 | \r | |
897 | // Make sure MAC address is empty\r | |
898 | ZeroMem (McastMac, sizeof(EFI_MAC_ADDRESS));\r | |
899 | \r | |
900 | // If we need ipv4 address\r | |
901 | if (!IsIpv6) {\r | |
902 | // Most significant 25 bits of a multicast HW address are set.\r | |
903 | // 01-00-5E is the IPv4 Ethernet Multicast Address (see RFC 1112)\r | |
904 | McastMac->Addr[0] = 0x01;\r | |
905 | McastMac->Addr[1] = 0x00;\r | |
906 | McastMac->Addr[2] = 0x5E;\r | |
907 | \r | |
908 | // Lower 23 bits from ipv4 address\r | |
909 | McastMac->Addr[3] = (Ip->v4.Addr[1] & 0x7F); // Clear the most significant bit (25th bit of MAC must be 0)\r | |
910 | McastMac->Addr[4] = Ip->v4.Addr[2];\r | |
911 | McastMac->Addr[5] = Ip->v4.Addr[3];\r | |
912 | } else {\r | |
913 | // Most significant 16 bits of multicast v6 HW address are set\r | |
914 | // 33-33 is the IPv6 Ethernet Multicast Address (see RFC 2464)\r | |
915 | McastMac->Addr[0] = 0x33;\r | |
916 | McastMac->Addr[1] = 0x33;\r | |
917 | \r | |
918 | // lower four octets are taken from ipv6 address\r | |
919 | McastMac->Addr[2] = Ip->v6.Addr[8];\r | |
920 | McastMac->Addr[3] = Ip->v6.Addr[9];\r | |
921 | McastMac->Addr[4] = Ip->v6.Addr[10];\r | |
922 | McastMac->Addr[5] = Ip->v6.Addr[11];\r | |
923 | }\r | |
924 | \r | |
925 | return EFI_SUCCESS;\r | |
926 | }\r | |
927 | \r | |
928 | /*\r | |
929 | * UEFI NvData() function\r | |
930 | *\r | |
931 | */\r | |
932 | EFI_STATUS\r | |
933 | EFIAPI\r | |
934 | SnpNvData (\r | |
935 | IN EFI_SIMPLE_NETWORK_PROTOCOL* pobj,\r | |
936 | IN BOOLEAN read_write,\r | |
937 | IN UINTN offset,\r | |
938 | IN UINTN buff_size,\r | |
939 | IN OUT VOID *data\r | |
940 | )\r | |
941 | {\r | |
942 | DEBUG ((DEBUG_NET, "SnpNvData()\n"));\r | |
943 | \r | |
944 | return EFI_UNSUPPORTED;\r | |
945 | }\r | |
946 | \r | |
947 | \r | |
948 | /*\r | |
949 | * UEFI GetStatus () function\r | |
950 | *\r | |
951 | */\r | |
952 | EFI_STATUS\r | |
953 | EFIAPI\r | |
954 | SnpGetStatus (\r | |
0150e14d RC |
955 | IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp,\r |
956 | OUT UINT32 *IrqStat OPTIONAL,\r | |
957 | OUT VOID **TxBuff OPTIONAL\r | |
46f2c53b OM |
958 | )\r |
959 | {\r | |
960 | UINT32 FifoInt;\r | |
961 | EFI_STATUS Status;\r | |
962 | UINTN NumTxStatusEntries;\r | |
963 | UINT32 TxStatus;\r | |
964 | UINT16 PacketTag;\r | |
965 | UINT32 Interrupts;\r | |
966 | LAN9118_DRIVER *LanDriver;\r | |
967 | \r | |
968 | LanDriver = INSTANCE_FROM_SNP_THIS (Snp);\r | |
969 | \r | |
970 | // Check preliminaries\r | |
971 | if (Snp == NULL) {\r | |
972 | return EFI_INVALID_PARAMETER;\r | |
973 | }\r | |
974 | \r | |
0150e14d RC |
975 | // Check that driver was started and initialised\r |
976 | if (Snp->Mode->State == EfiSimpleNetworkStarted) {\r | |
977 | DEBUG ((EFI_D_WARN, "Warning: LAN9118 Driver not initialized\n"));\r | |
978 | return EFI_DEVICE_ERROR;\r | |
979 | } else if (Snp->Mode->State == EfiSimpleNetworkStopped) {\r | |
980 | DEBUG ((EFI_D_WARN, "Warning: LAN9118 Driver in stopped state\n"));\r | |
46f2c53b OM |
981 | return EFI_NOT_STARTED;\r |
982 | }\r | |
983 | \r | |
984 | // Check and acknowledge TX Status interrupt (this will happen if the\r | |
985 | // consumer of SNP does not call GetStatus.)\r | |
986 | // TODO will we lose TxStatuses if this happens? Maybe in SnpTransmit we\r | |
987 | // should check for it and dump the TX Status FIFO.\r | |
e68449c9 | 988 | FifoInt = Lan9118MmioRead32 (LAN9118_FIFO_INT);\r |
46f2c53b OM |
989 | \r |
990 | // Clear the TX Status FIFO Overflow\r | |
991 | if ((FifoInt & INSTS_TXSO) == 0) {\r | |
992 | FifoInt |= INSTS_TXSO;\r | |
e68449c9 | 993 | Lan9118MmioWrite32 (LAN9118_FIFO_INT, FifoInt);\r |
46f2c53b OM |
994 | }\r |
995 | \r | |
996 | // Read interrupt status if IrqStat is not NULL\r | |
997 | if (IrqStat != NULL) {\r | |
e52aee5d | 998 | *IrqStat = 0;\r |
46f2c53b OM |
999 | \r |
1000 | // Check for receive interrupt\r | |
e68449c9 | 1001 | if (Lan9118MmioRead32 (LAN9118_INT_STS) & INSTS_RSFL) { // Data moved from rx FIFO\r |
46f2c53b | 1002 | *IrqStat |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;\r |
e68449c9 | 1003 | Lan9118MmioWrite32 (LAN9118_INT_STS,INSTS_RSFL);\r |
46f2c53b OM |
1004 | }\r |
1005 | \r | |
1006 | // Check for transmit interrupt\r | |
e68449c9 | 1007 | if (Lan9118MmioRead32 (LAN9118_INT_STS) & INSTS_TSFL) {\r |
46f2c53b | 1008 | *IrqStat |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;\r |
e68449c9 | 1009 | Lan9118MmioWrite32 (LAN9118_INT_STS,INSTS_TSFL);\r |
46f2c53b OM |
1010 | }\r |
1011 | \r | |
1012 | // Check for software interrupt\r | |
e68449c9 | 1013 | if (Lan9118MmioRead32 (LAN9118_INT_STS) & INSTS_SW_INT) {\r |
46f2c53b | 1014 | *IrqStat |= EFI_SIMPLE_NETWORK_SOFTWARE_INTERRUPT;\r |
e68449c9 | 1015 | Lan9118MmioWrite32 (LAN9118_INT_STS,INSTS_SW_INT);\r |
46f2c53b OM |
1016 | }\r |
1017 | }\r | |
1018 | \r | |
1019 | // Check Status of transmitted packets\r | |
1020 | // (We ignore TXSTATUS_NO_CA has it might happen in Full Duplex)\r | |
1021 | \r | |
e68449c9 | 1022 | NumTxStatusEntries = Lan9118MmioRead32(LAN9118_TX_FIFO_INF) & TXFIFOINF_TXSUSED_MASK;\r |
46f2c53b | 1023 | if (NumTxStatusEntries > 0) {\r |
e68449c9 | 1024 | TxStatus = Lan9118MmioRead32 (LAN9118_TX_STATUS);\r |
46f2c53b OM |
1025 | PacketTag = TxStatus >> 16;\r |
1026 | TxStatus = TxStatus & 0xFFFF;\r | |
a537c717 | 1027 | if ((TxStatus & TXSTATUS_ES) && (TxStatus != (TXSTATUS_ES | TXSTATUS_NO_CA))) {\r |
46f2c53b OM |
1028 | DEBUG ((EFI_D_ERROR, "LAN9118: There was an error transmitting. TxStatus=0x%08x:", TxStatus));\r |
1029 | if (TxStatus & TXSTATUS_NO_CA) {\r | |
1030 | DEBUG ((EFI_D_ERROR, "- No carrier\n"));\r | |
1031 | }\r | |
1032 | if (TxStatus & TXSTATUS_DEF) {\r | |
1033 | DEBUG ((EFI_D_ERROR, "- Packet tx was deferred\n"));\r | |
1034 | }\r | |
1035 | if (TxStatus & TXSTATUS_EDEF) {\r | |
1036 | DEBUG ((EFI_D_ERROR, "- Tx ended because of excessive deferral\n"));\r | |
1037 | }\r | |
1038 | if (TxStatus & TXSTATUS_ECOLL) {\r | |
1039 | DEBUG ((EFI_D_ERROR, "- Tx ended because of Excessive Collisions\n"));\r | |
1040 | }\r | |
1041 | if (TxStatus & TXSTATUS_LCOLL) {\r | |
1042 | DEBUG ((EFI_D_ERROR, "- Packet Tx aborted after coll window of 64 bytes\n"));\r | |
1043 | }\r | |
1044 | if (TxStatus & TXSTATUS_LOST_CA) {\r | |
1045 | DEBUG ((EFI_D_ERROR, "- Lost carrier during Tx\n"));\r | |
1046 | }\r | |
1047 | return EFI_DEVICE_ERROR;\r | |
a537c717 | 1048 | } else if (TxBuff != NULL) {\r |
46f2c53b OM |
1049 | LanDriver->Stats.TxTotalFrames += 1;\r |
1050 | *TxBuff = LanDriver->TxRing[PacketTag % LAN9118_TX_RING_NUM_ENTRIES];\r | |
1051 | }\r | |
e117c894 MB |
1052 | } else if (TxBuff != NULL) {\r |
1053 | *TxBuff = NULL;\r | |
46f2c53b OM |
1054 | }\r |
1055 | \r | |
1056 | // Check for a TX Error interrupt\r | |
e68449c9 | 1057 | Interrupts = Lan9118MmioRead32 (LAN9118_INT_STS);\r |
46f2c53b OM |
1058 | if (Interrupts & INSTS_TXE) {\r |
1059 | DEBUG ((EFI_D_ERROR, "LAN9118: Transmitter error. Restarting..."));\r | |
1060 | \r | |
ac8f1e10 | 1061 | // Software reset, the TXE interrupt is cleared by the reset.\r |
42589b9a OM |
1062 | Status = SoftReset (0, Snp);\r |
1063 | if (EFI_ERROR (Status)) {\r | |
46f2c53b OM |
1064 | DEBUG ((EFI_D_ERROR, "\n\tSoft Reset Failed: Hardware Error\n"));\r |
1065 | return EFI_DEVICE_ERROR;\r | |
1066 | }\r | |
1067 | \r | |
ac8f1e10 RC |
1068 | // Reactivate the LEDs\r |
1069 | Status = ConfigureHardware (HW_CONF_USE_LEDS, Snp);\r | |
1070 | if (EFI_ERROR (Status)) {\r | |
1071 | return Status;\r | |
1072 | }\r | |
46f2c53b | 1073 | \r |
ac8f1e10 RC |
1074 | //\r |
1075 | // Restart the transmitter and if necessary the receiver.\r | |
1076 | // Do not ask for FIFO reset as it has already been done\r | |
1077 | // by SoftReset().\r | |
1078 | //\r | |
46f2c53b | 1079 | StartTx (START_TX_MAC | START_TX_CFG, Snp);\r |
ac8f1e10 RC |
1080 | if (Snp->Mode->ReceiveFilterSetting != 0) {\r |
1081 | StartRx (0, Snp);\r | |
1082 | }\r | |
46f2c53b OM |
1083 | }\r |
1084 | \r | |
1085 | // Update the media status\r | |
1086 | Status = CheckLinkStatus (0, Snp);\r | |
1087 | if (EFI_ERROR(Status)) {\r | |
1088 | Snp->Mode->MediaPresent = FALSE;\r | |
1089 | } else {\r | |
1090 | Snp->Mode->MediaPresent = TRUE;\r | |
1091 | }\r | |
1092 | \r | |
1093 | return EFI_SUCCESS;\r | |
1094 | }\r | |
1095 | \r | |
1096 | \r | |
1097 | /*\r | |
1098 | * UEFI Transmit() function\r | |
1099 | *\r | |
1100 | */\r | |
1101 | EFI_STATUS\r | |
1102 | EFIAPI\r | |
1103 | SnpTransmit (\r | |
1104 | IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp,\r | |
1105 | IN UINTN HdrSize,\r | |
1106 | IN UINTN BuffSize,\r | |
1107 | IN VOID* Data,\r | |
1108 | IN EFI_MAC_ADDRESS *SrcAddr OPTIONAL,\r | |
1109 | IN EFI_MAC_ADDRESS *DstAddr OPTIONAL,\r | |
1110 | IN UINT16 *Protocol OPTIONAL\r | |
1111 | )\r | |
1112 | {\r | |
1113 | LAN9118_DRIVER *LanDriver;\r | |
1114 | UINT32 TxFreeSpace;\r | |
1115 | UINT32 TxStatusSpace;\r | |
1116 | INT32 Count;\r | |
1117 | UINT32 CommandA;\r | |
1118 | UINT32 CommandB;\r | |
1119 | UINT16 LocalProtocol;\r | |
1120 | UINT32 *LocalData;\r | |
1121 | UINT16 PacketTag;\r | |
1122 | \r | |
1123 | #if defined(EVAL_PERFORMANCE)\r | |
1124 | UINT64 Perf;\r | |
1125 | UINT64 StartClock;\r | |
1126 | UINT64 EndClock;\r | |
1127 | \r | |
1128 | Perf = GetPerformanceCounterProperties (NULL, NULL);\r | |
1129 | StartClock = GetPerformanceCounter ();\r | |
1130 | #endif\r | |
1131 | \r | |
1132 | LanDriver = INSTANCE_FROM_SNP_THIS (Snp);\r | |
1133 | \r | |
1134 | // Check preliminaries\r | |
1135 | if ((Snp == NULL) || (Data == NULL)) {\r | |
1136 | return EFI_INVALID_PARAMETER;\r | |
1137 | }\r | |
0150e14d RC |
1138 | \r |
1139 | // Check that driver was started and initialised\r | |
1140 | if (Snp->Mode->State == EfiSimpleNetworkStarted) {\r | |
1141 | DEBUG ((EFI_D_WARN, "Warning: LAN9118 Driver not initialized\n"));\r | |
1142 | return EFI_DEVICE_ERROR;\r | |
1143 | } else if (Snp->Mode->State == EfiSimpleNetworkStopped) {\r | |
1144 | DEBUG ((EFI_D_WARN, "Warning: LAN9118 Driver in stopped state\n"));\r | |
46f2c53b OM |
1145 | return EFI_NOT_STARTED;\r |
1146 | }\r | |
1147 | \r | |
1148 | // Ensure header is correct size if non-zero\r | |
1149 | if (HdrSize) {\r | |
1150 | if (HdrSize != Snp->Mode->MediaHeaderSize) {\r | |
1151 | return EFI_INVALID_PARAMETER;\r | |
1152 | }\r | |
1153 | \r | |
1154 | if ((DstAddr == NULL) || (Protocol == NULL)) {\r | |
1155 | return EFI_INVALID_PARAMETER;\r | |
1156 | }\r | |
1157 | }\r | |
1158 | \r | |
0150e14d RC |
1159 | //\r |
1160 | // Check validity of BufferSize\r | |
1161 | //\r | |
1162 | if (BuffSize < Snp->Mode->MediaHeaderSize) {\r | |
1163 | return EFI_BUFFER_TOO_SMALL;\r | |
1164 | }\r | |
1165 | \r | |
46f2c53b OM |
1166 | // Before transmitting check the link status\r |
1167 | /*if (CheckLinkStatus (0, Snp) < 0) {\r | |
1168 | return EFI_NOT_READY;\r | |
1169 | }*/\r | |
1170 | \r | |
1171 | // Get DATA FIFO free space in bytes\r | |
1172 | TxFreeSpace = TxDataFreeSpace (0, Snp);\r | |
1173 | if (TxFreeSpace < BuffSize) {\r | |
1174 | return EFI_NOT_READY;\r | |
1175 | }\r | |
1176 | \r | |
1177 | // Get STATUS FIFO used space in bytes\r | |
1178 | TxStatusSpace = TxStatusUsedSpace (0, Snp);\r | |
1179 | if (TxStatusSpace > 500) {\r | |
1180 | return EFI_NOT_READY;\r | |
1181 | }\r | |
1182 | \r | |
1183 | // If DstAddr is not provided, get it from Buffer (we trust that the caller\r | |
1184 | // has provided a well-formed frame).\r | |
1185 | if (DstAddr == NULL) {\r | |
1186 | DstAddr = (EFI_MAC_ADDRESS *) Data;\r | |
1187 | }\r | |
1188 | \r | |
1189 | // Check for the nature of the frame\r | |
1190 | if ((DstAddr->Addr[0] & 0x1) == 1) {\r | |
1191 | LanDriver->Stats.TxMulticastFrames += 1;\r | |
1192 | } else {\r | |
1193 | LanDriver->Stats.TxUnicastFrames += 1;\r | |
1194 | }\r | |
1195 | \r | |
1196 | // Check if broadcast\r | |
1197 | if (DstAddr->Addr[0] == 0xFF) {\r | |
1198 | LanDriver->Stats.TxBroadcastFrames += 1;\r | |
1199 | }\r | |
1200 | \r | |
1201 | PacketTag = LanDriver->NextPacketTag;\r | |
1202 | LanDriver->NextPacketTag++;\r | |
1203 | \r | |
1204 | if (HdrSize) {\r | |
1205 | \r | |
1206 | // Format pointer\r | |
1207 | LocalData = (UINT32*) Data;\r | |
1208 | LocalProtocol = *Protocol;\r | |
1209 | \r | |
1210 | // Create first buffer to pass to controller (for the header)\r | |
1211 | CommandA = TX_CMD_A_FIRST_SEGMENT | TX_CMD_A_BUFF_SIZE (HdrSize);\r | |
1212 | CommandB = TX_CMD_B_PACKET_TAG (PacketTag) | TX_CMD_B_PACKET_LENGTH (BuffSize);\r | |
1213 | \r | |
1214 | // Write the commands first\r | |
e68449c9 MR |
1215 | Lan9118MmioWrite32 (LAN9118_TX_DATA, CommandA);\r |
1216 | Lan9118MmioWrite32 (LAN9118_TX_DATA, CommandB);\r | |
46f2c53b OM |
1217 | \r |
1218 | // Write the destination address\r | |
e68449c9 | 1219 | Lan9118MmioWrite32 (LAN9118_TX_DATA,\r |
46f2c53b OM |
1220 | (DstAddr->Addr[0]) |\r |
1221 | (DstAddr->Addr[1] << 8) |\r | |
1222 | (DstAddr->Addr[2] << 16) |\r | |
1223 | (DstAddr->Addr[3] << 24)\r | |
1224 | );\r | |
1225 | \r | |
e68449c9 | 1226 | Lan9118MmioWrite32 (LAN9118_TX_DATA,\r |
46f2c53b OM |
1227 | (DstAddr->Addr[4]) |\r |
1228 | (DstAddr->Addr[5] << 8) |\r | |
1229 | (SrcAddr->Addr[0] << 16) | // Write the Source Address\r | |
1230 | (SrcAddr->Addr[1] << 24)\r | |
1231 | );\r | |
1232 | \r | |
e68449c9 | 1233 | Lan9118MmioWrite32 (LAN9118_TX_DATA,\r |
46f2c53b OM |
1234 | (SrcAddr->Addr[2]) |\r |
1235 | (SrcAddr->Addr[3] << 8) |\r | |
1236 | (SrcAddr->Addr[4] << 16) |\r | |
1237 | (SrcAddr->Addr[5] << 24)\r | |
1238 | );\r | |
1239 | \r | |
1240 | // Write the Protocol\r | |
e68449c9 | 1241 | Lan9118MmioWrite32 (LAN9118_TX_DATA, (UINT32)(HTONS (LocalProtocol)));\r |
46f2c53b OM |
1242 | \r |
1243 | // Next buffer is the payload\r | |
1244 | CommandA = TX_CMD_A_LAST_SEGMENT | TX_CMD_A_BUFF_SIZE (BuffSize - HdrSize) | TX_CMD_A_COMPLETION_INT | TX_CMD_A_DATA_START_OFFSET (2); // 2 bytes beginning offset\r | |
1245 | \r | |
1246 | // Write the commands\r | |
e68449c9 MR |
1247 | Lan9118MmioWrite32 (LAN9118_TX_DATA, CommandA);\r |
1248 | Lan9118MmioWrite32 (LAN9118_TX_DATA, CommandB);\r | |
46f2c53b OM |
1249 | \r |
1250 | // Write the payload\r | |
1251 | for (Count = 0; Count < ((BuffSize + 3) >> 2) - 3; Count++) {\r | |
e68449c9 | 1252 | Lan9118MmioWrite32 (LAN9118_TX_DATA, LocalData[Count + 3]);\r |
46f2c53b OM |
1253 | }\r |
1254 | } else {\r | |
1255 | // Format pointer\r | |
1256 | LocalData = (UINT32*) Data;\r | |
1257 | \r | |
1258 | // Create a buffer to pass to controller\r | |
1259 | CommandA = TX_CMD_A_FIRST_SEGMENT | TX_CMD_A_LAST_SEGMENT | TX_CMD_A_BUFF_SIZE (BuffSize) | TX_CMD_A_COMPLETION_INT;\r | |
1260 | CommandB = TX_CMD_B_PACKET_TAG (PacketTag) | TX_CMD_B_PACKET_LENGTH (BuffSize);\r | |
1261 | \r | |
1262 | // Write the commands first\r | |
e68449c9 MR |
1263 | Lan9118MmioWrite32 (LAN9118_TX_DATA, CommandA);\r |
1264 | Lan9118MmioWrite32 (LAN9118_TX_DATA, CommandB);\r | |
46f2c53b OM |
1265 | \r |
1266 | // Write all the data\r | |
1267 | for (Count = 0; Count < ((BuffSize + 3) >> 2); Count++) {\r | |
e68449c9 | 1268 | Lan9118MmioWrite32 (LAN9118_TX_DATA, LocalData[Count]);\r |
46f2c53b OM |
1269 | }\r |
1270 | }\r | |
1271 | \r | |
1272 | // Save the address of the submitted packet so we can notify the consumer that\r | |
1273 | // it has been sent in GetStatus. When the packet tag appears in the Tx Status\r | |
1274 | // Fifo, we will return Buffer in the TxBuff parameter of GetStatus.\r | |
1275 | LanDriver->TxRing[PacketTag % LAN9118_TX_RING_NUM_ENTRIES] = Data;\r | |
1276 | \r | |
1277 | #if defined(EVAL_PERFORMANCE)\r | |
1278 | EndClock = GetPerformanceCounter ();\r | |
1279 | DEBUG ((EFI_D_ERROR, "Time processing: %d counts @ %d Hz\n", StartClock - EndClock,Perf));\r | |
1280 | #endif\r | |
1281 | \r | |
1282 | LanDriver->Stats.TxGoodFrames += 1;\r | |
1283 | \r | |
1284 | return EFI_SUCCESS;\r | |
1285 | }\r | |
1286 | \r | |
1287 | \r | |
1288 | /*\r | |
1289 | * UEFI Receive() function\r | |
1290 | *\r | |
1291 | */\r | |
1292 | EFI_STATUS\r | |
1293 | EFIAPI\r | |
1294 | SnpReceive (\r | |
1295 | IN EFI_SIMPLE_NETWORK_PROTOCOL* Snp,\r | |
1296 | OUT UINTN *HdrSize OPTIONAL,\r | |
1297 | IN OUT UINTN *BuffSize,\r | |
1298 | OUT VOID *Data,\r | |
1299 | OUT EFI_MAC_ADDRESS *SrcAddr OPTIONAL,\r | |
1300 | OUT EFI_MAC_ADDRESS *DstAddr OPTIONAL,\r | |
1301 | OUT UINT16 *Protocol OPTIONAL\r | |
1302 | )\r | |
1303 | {\r | |
42589b9a | 1304 | LAN9118_DRIVER *LanDriver;\r |
1130106e | 1305 | UINT32 IntSts;\r |
42589b9a OM |
1306 | UINT32 RxFifoStatus;\r |
1307 | UINT32 NumPackets;\r | |
1308 | UINT32 RxCfgValue;\r | |
1309 | UINT32 PLength; // Packet length\r | |
1310 | UINT32 ReadLimit;\r | |
1311 | UINT32 Count;\r | |
1312 | UINT32 Padding;\r | |
1313 | UINT32 *RawData;\r | |
46f2c53b OM |
1314 | EFI_MAC_ADDRESS Dst;\r |
1315 | EFI_MAC_ADDRESS Src;\r | |
42589b9a OM |
1316 | UINTN DroppedFrames;\r |
1317 | EFI_STATUS Status;\r | |
46f2c53b OM |
1318 | \r |
1319 | LanDriver = INSTANCE_FROM_SNP_THIS (Snp);\r | |
1320 | \r | |
1321 | #if defined(EVAL_PERFORMANCE)\r | |
1322 | UINT64 Perf = GetPerformanceCounterProperties (NULL, NULL);\r | |
1323 | UINT64 StartClock = GetPerformanceCounter ();\r | |
1324 | #endif\r | |
1325 | \r | |
1326 | // Check preliminaries\r | |
0150e14d | 1327 | if ((Snp == NULL) || (Data == NULL) || (BuffSize == NULL)) {\r |
46f2c53b OM |
1328 | return EFI_INVALID_PARAMETER;\r |
1329 | }\r | |
1330 | \r | |
0150e14d RC |
1331 | // Check that driver was started and initialised\r |
1332 | if (Snp->Mode->State == EfiSimpleNetworkStarted) {\r | |
1333 | DEBUG ((EFI_D_WARN, "Warning: LAN9118 Driver not initialized\n"));\r | |
1334 | return EFI_DEVICE_ERROR;\r | |
1335 | } else if (Snp->Mode->State == EfiSimpleNetworkStopped) {\r | |
1336 | DEBUG ((EFI_D_WARN, "Warning: LAN9118 Driver in stopped state\n"));\r | |
46f2c53b OM |
1337 | return EFI_NOT_STARTED;\r |
1338 | }\r | |
1339 | \r | |
1130106e RC |
1340 | //\r |
1341 | // If the receiver raised the RXE error bit, check if the receiver status\r | |
1342 | // FIFO is full and if not just acknowledge the error. The two other\r | |
1343 | // conditions to get a RXE error are :\r | |
1344 | // . the RX data FIFO is read whereas being empty.\r | |
1345 | // . the RX status FIFO is read whereas being empty.\r | |
1346 | // The RX data and status FIFO are read by this driver only in the following\r | |
1347 | // code of this function. After the readings, the RXE error bit is checked\r | |
1348 | // and if raised, the controller is reset. Thus, at this point, we consider\r | |
1349 | // that the only valid reason to get an RXE error is the receiver status\r | |
1350 | // FIFO being full. And if this is not the case, we consider that this is\r | |
1351 | // a spurious error and we just get rid of it. We experienced such 'spurious'\r | |
1352 | // errors when running the driver on an A57 on Juno. No valid reason to\r | |
1353 | // explain those errors has been found so far and everything seems to\r | |
1354 | // work perfectly when they are just ignored.\r | |
1355 | //\r | |
e68449c9 | 1356 | IntSts = Lan9118MmioRead32 (LAN9118_INT_STS);\r |
1130106e | 1357 | if ((IntSts & INSTS_RXE) && (!(IntSts & INSTS_RSFF))) {\r |
e68449c9 | 1358 | Lan9118MmioWrite32 (LAN9118_INT_STS, INSTS_RXE);\r |
1130106e RC |
1359 | }\r |
1360 | \r | |
46f2c53b | 1361 | // Count dropped frames\r |
e68449c9 | 1362 | DroppedFrames = Lan9118MmioRead32 (LAN9118_RX_DROP);\r |
46f2c53b OM |
1363 | LanDriver->Stats.RxDroppedFrames += DroppedFrames;\r |
1364 | \r | |
1365 | NumPackets = RxStatusUsedSpace (0, Snp) / 4;\r | |
1366 | if (!NumPackets) {\r | |
1367 | return EFI_NOT_READY;\r | |
1368 | }\r | |
1369 | \r | |
1370 | // Read Rx Status (only if not empty)\r | |
e68449c9 | 1371 | RxFifoStatus = Lan9118MmioRead32 (LAN9118_RX_STATUS);\r |
46f2c53b OM |
1372 | LanDriver->Stats.RxTotalFrames += 1;\r |
1373 | \r | |
1374 | // First check for errors\r | |
1375 | if ((RxFifoStatus & RXSTATUS_MII_ERROR) ||\r | |
1376 | (RxFifoStatus & RXSTATUS_RXW_TO) ||\r | |
1377 | (RxFifoStatus & RXSTATUS_FTL) ||\r | |
1378 | (RxFifoStatus & RXSTATUS_LCOLL) ||\r | |
1379 | (RxFifoStatus & RXSTATUS_LE) ||\r | |
1380 | (RxFifoStatus & RXSTATUS_DB))\r | |
1381 | {\r | |
1382 | DEBUG ((EFI_D_WARN, "Warning: There was an error on frame reception.\n"));\r | |
1383 | return EFI_DEVICE_ERROR;\r | |
1384 | }\r | |
1385 | \r | |
1386 | // Check if we got a CRC error\r | |
1387 | if (RxFifoStatus & RXSTATUS_CRC_ERROR) {\r | |
1388 | DEBUG ((EFI_D_WARN, "Warning: Crc Error\n"));\r | |
1389 | LanDriver->Stats.RxCrcErrorFrames += 1;\r | |
1390 | LanDriver->Stats.RxDroppedFrames += 1;\r | |
1391 | return EFI_DEVICE_ERROR;\r | |
1392 | }\r | |
1393 | \r | |
1394 | // Check if we got a runt frame\r | |
1395 | if (RxFifoStatus & RXSTATUS_RUNT) {\r | |
1396 | DEBUG ((EFI_D_WARN, "Warning: Runt Frame\n"));\r | |
1397 | LanDriver->Stats.RxUndersizeFrames += 1;\r | |
1398 | LanDriver->Stats.RxDroppedFrames += 1;\r | |
1399 | return EFI_DEVICE_ERROR;\r | |
1400 | }\r | |
1401 | \r | |
1402 | // Check filtering status for this packet\r | |
1403 | if (RxFifoStatus & RXSTATUS_FILT_FAIL) {\r | |
1404 | DEBUG ((EFI_D_WARN, "Warning: Frame Failed Filtering\n"));\r | |
1405 | // fast forward?\r | |
1406 | }\r | |
1407 | \r | |
1408 | // Check if we got a broadcast frame\r | |
1409 | if (RxFifoStatus & RXSTATUS_BCF) {\r | |
1410 | LanDriver->Stats.RxBroadcastFrames += 1;\r | |
1411 | }\r | |
1412 | \r | |
1413 | // Check if we got a multicast frame\r | |
1414 | if (RxFifoStatus & RXSTATUS_MCF) {\r | |
1415 | LanDriver->Stats.RxMulticastFrames += 1;\r | |
1416 | }\r | |
1417 | \r | |
1418 | // Check if we got a unicast frame\r | |
1419 | if ((RxFifoStatus & RXSTATUS_BCF) && ((RxFifoStatus & RXSTATUS_MCF) == 0)) {\r | |
1420 | LanDriver->Stats.RxUnicastFrames += 1;\r | |
1421 | }\r | |
1422 | \r | |
1423 | // Get the received packet length\r | |
1424 | PLength = GET_RXSTATUS_PACKET_LENGTH(RxFifoStatus);\r | |
1425 | LanDriver->Stats.RxTotalBytes += (PLength - 4);\r | |
1426 | \r | |
46f2c53b OM |
1427 | // If padding is applied, read more DWORDs\r |
1428 | if (PLength % 4) {\r | |
1429 | Padding = 4 - (PLength % 4);\r | |
1430 | ReadLimit = (PLength + Padding)/4;\r | |
1431 | } else {\r | |
1432 | ReadLimit = PLength/4;\r | |
1433 | Padding = 0;\r | |
1434 | }\r | |
1435 | \r | |
f22e9658 OM |
1436 | // Check buffer size\r |
1437 | if (*BuffSize < (PLength + Padding)) {\r | |
1438 | *BuffSize = PLength + Padding;\r | |
1439 | return EFI_BUFFER_TOO_SMALL;\r | |
1440 | }\r | |
1441 | \r | |
c6a72cd7 | 1442 | // Set the amount of data to be transferred out of FIFO for THIS packet\r |
46f2c53b | 1443 | // This can be used to trigger an interrupt, and status can be checked\r |
e68449c9 | 1444 | RxCfgValue = Lan9118MmioRead32 (LAN9118_RX_CFG);\r |
46f2c53b OM |
1445 | RxCfgValue &= ~(RXCFG_RX_DMA_CNT_MASK);\r |
1446 | RxCfgValue |= RXCFG_RX_DMA_CNT (ReadLimit);\r | |
1447 | \r | |
1448 | // Set end alignment to 4-bytes\r | |
1449 | RxCfgValue &= ~(RXCFG_RX_END_ALIGN_MASK);\r | |
e68449c9 | 1450 | Lan9118MmioWrite32 (LAN9118_RX_CFG, RxCfgValue);\r |
46f2c53b OM |
1451 | \r |
1452 | // Update buffer size\r | |
1453 | *BuffSize = PLength; // -4 bytes may be needed: Received in buffer as\r | |
1454 | // 4 bytes longer than packet actually is, unless\r | |
1455 | // packet is < 64 bytes\r | |
1456 | \r | |
1457 | if (HdrSize != NULL)\r | |
1458 | *HdrSize = Snp->Mode->MediaHeaderSize;\r | |
1459 | \r | |
1460 | // Format the pointer\r | |
1461 | RawData = (UINT32*)Data;\r | |
1462 | \r | |
1463 | // Read Rx Packet\r | |
1464 | for (Count = 0; Count < ReadLimit; Count++) {\r | |
e68449c9 | 1465 | RawData[Count] = Lan9118MmioRead32 (LAN9118_RX_DATA);\r |
46f2c53b OM |
1466 | }\r |
1467 | \r | |
1130106e RC |
1468 | // Get the destination address\r |
1469 | if (DstAddr != NULL) {\r | |
1470 | Dst.Addr[0] = (RawData[0] & 0xFF);\r | |
1471 | Dst.Addr[1] = (RawData[0] & 0xFF00) >> 8;\r | |
1472 | Dst.Addr[2] = (RawData[0] & 0xFF0000) >> 16;\r | |
1473 | Dst.Addr[3] = (RawData[0] & 0xFF000000) >> 24;\r | |
1474 | Dst.Addr[4] = (RawData[1] & 0xFF);\r | |
1475 | Dst.Addr[5] = (RawData[1] & 0xFF00) >> 8;\r | |
1476 | CopyMem (DstAddr, &Dst, NET_ETHER_ADDR_LEN);\r | |
1477 | }\r | |
1478 | \r | |
1479 | // Get the source address\r | |
1480 | if (SrcAddr != NULL) {\r | |
1481 | Src.Addr[0] = (RawData[1] & 0xFF0000) >> 16;\r | |
1482 | Src.Addr[1] = (RawData[1] & 0xFF000000) >> 24;\r | |
1483 | Src.Addr[2] = (RawData[2] & 0xFF);\r | |
1484 | Src.Addr[3] = (RawData[2] & 0xFF00) >> 8;\r | |
1485 | Src.Addr[4] = (RawData[2] & 0xFF0000) >> 16;\r | |
1486 | Src.Addr[5] = (RawData[2] & 0xFF000000) >> 24;\r | |
1487 | CopyMem (SrcAddr, &Src, NET_ETHER_ADDR_LEN);\r | |
1488 | }\r | |
1489 | \r | |
1490 | // Get the protocol\r | |
1491 | if (Protocol != NULL) {\r | |
1492 | *Protocol = NTOHS (RawData[3] & 0xFFFF);\r | |
1493 | }\r | |
1494 | \r | |
46f2c53b | 1495 | // Check for Rx errors (worst possible error)\r |
e68449c9 | 1496 | if (Lan9118MmioRead32 (LAN9118_INT_STS) & INSTS_RXE) {\r |
46f2c53b OM |
1497 | DEBUG ((EFI_D_WARN, "Warning: Receiver Error. Restarting...\n"));\r |
1498 | \r | |
ac8f1e10 | 1499 | // Software reset, the RXE interrupt is cleared by the reset.\r |
42589b9a OM |
1500 | Status = SoftReset (0, Snp);\r |
1501 | if (EFI_ERROR (Status)) {\r | |
46f2c53b OM |
1502 | DEBUG ((EFI_D_ERROR, "Error: Soft Reset Failed: Hardware Error.\n"));\r |
1503 | return EFI_DEVICE_ERROR;\r | |
1504 | }\r | |
1505 | \r | |
ac8f1e10 RC |
1506 | // Reactivate the LEDs\r |
1507 | Status = ConfigureHardware (HW_CONF_USE_LEDS, Snp);\r | |
1508 | if (EFI_ERROR (Status)) {\r | |
1509 | return Status;\r | |
1510 | }\r | |
46f2c53b | 1511 | \r |
ac8f1e10 | 1512 | //\r |
4f0624e8 | 1513 | // Restart the receiver and the transmitter without resetting the FIFOs\r |
ac8f1e10 RC |
1514 | // as it has been done by SoftReset().\r |
1515 | //\r | |
46f2c53b | 1516 | StartRx (0, Snp);\r |
ac8f1e10 | 1517 | StartTx (START_TX_MAC | START_TX_CFG, Snp);\r |
46f2c53b OM |
1518 | \r |
1519 | // Say that command could not be sent\r | |
1520 | return EFI_DEVICE_ERROR;\r | |
1521 | }\r | |
1522 | \r | |
46f2c53b OM |
1523 | #if defined(EVAL_PERFORMANCE)\r |
1524 | UINT64 EndClock = GetPerformanceCounter ();\r | |
1525 | DEBUG ((EFI_D_ERROR, "Receive Time processing: %d counts @ %d Hz\n", StartClock - EndClock,Perf));\r | |
1526 | #endif\r | |
1527 | \r | |
1528 | LanDriver->Stats.RxGoodFrames += 1;\r | |
1529 | \r | |
1530 | return EFI_SUCCESS;\r | |
1531 | }\r |