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