3 * Copyright (c) 2012-2014, ARM Limited. All rights reserved.
5 * SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "Lan9118Dxe.h"
12 MAC_ADDR_DEVICE_PATH Lan9118
;
13 EFI_DEVICE_PATH_PROTOCOL End
;
14 } LAN9118_DEVICE_PATH
;
16 LAN9118_DEVICE_PATH Lan9118PathTemplate
= {
19 MESSAGING_DEVICE_PATH
, MSG_MAC_ADDR_DP
,
20 { (UINT8
) (sizeof(MAC_ADDR_DEVICE_PATH
)), (UINT8
) ((sizeof(MAC_ADDR_DEVICE_PATH
)) >> 8) }
27 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
28 { sizeof(EFI_DEVICE_PATH_PROTOCOL
), 0 }
33 ** Entry point for the LAN9118 driver
39 IN EFI_SYSTEM_TABLE
*SystemTable
43 LAN9118_DRIVER
*LanDriver
;
44 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
45 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
46 LAN9118_DEVICE_PATH
*Lan9118Path
;
47 EFI_HANDLE ControllerHandle
;
49 // The PcdLan9118DxeBaseAddress PCD must be defined
50 ASSERT (PcdGet32 (PcdLan9118DxeBaseAddress
) != 0);
53 LanDriver
= AllocateZeroPool (sizeof (LAN9118_DRIVER
));
54 if (LanDriver
== NULL
) {
55 return EFI_OUT_OF_RESOURCES
;
57 Lan9118Path
= (LAN9118_DEVICE_PATH
*)AllocateCopyPool (sizeof (LAN9118_DEVICE_PATH
), &Lan9118PathTemplate
);
58 if (Lan9118Path
== NULL
) {
59 return EFI_OUT_OF_RESOURCES
;
62 // Initialize pointers
63 Snp
= &(LanDriver
->Snp
);
64 SnpMode
= &(LanDriver
->SnpMode
);
67 // Set the signature of the LAN Driver structure
68 LanDriver
->Signature
= LAN9118_SIGNATURE
;
70 // Assign fields and func pointers
71 Snp
->Revision
= EFI_SIMPLE_NETWORK_PROTOCOL_REVISION
;
72 Snp
->WaitForPacket
= NULL
;
73 Snp
->Initialize
= SnpInitialize
;
74 Snp
->Start
= SnpStart
;
76 Snp
->Reset
= SnpReset
;
77 Snp
->Shutdown
= SnpShutdown
;
78 Snp
->ReceiveFilters
= SnpReceiveFilters
;
79 Snp
->StationAddress
= SnpStationAddress
;
80 Snp
->Statistics
= SnpStatistics
;
81 Snp
->MCastIpToMac
= SnpMcastIptoMac
;
82 Snp
->NvData
= SnpNvData
;
83 Snp
->GetStatus
= SnpGetStatus
;
84 Snp
->Transmit
= SnpTransmit
;
85 Snp
->Receive
= SnpReceive
;
87 // Start completing simple network mode structure
88 SnpMode
->State
= EfiSimpleNetworkStopped
;
89 SnpMode
->HwAddressSize
= NET_ETHER_ADDR_LEN
; // HW address is 6 bytes
90 SnpMode
->MediaHeaderSize
= sizeof(ETHER_HEAD
); // Not sure of this
91 SnpMode
->MaxPacketSize
= EFI_PAGE_SIZE
; // Preamble + SOF + Ether Frame (with VLAN tag +4bytes)
92 SnpMode
->NvRamSize
= 0; // No NVRAM with this device
93 SnpMode
->NvRamAccessSize
= 0; // No NVRAM with this device
96 // Claim that all receive filter settings are supported, though the MULTICAST mode
97 // is not completely supported. The LAN9118 Ethernet controller is only able to
98 // do a "hash filtering" and not a perfect filtering on multicast addresses. The
99 // controller does not filter the multicast addresses directly but a hash value
100 // of them. The hash value of a multicast address is derived from its CRC and
101 // ranges from 0 to 63 included.
102 // We claim that the perfect MULTICAST filtering mode is supported because
103 // we do not want the user to switch directly to the PROMISCOUS_MULTICAST mode
104 // and thus not being able to take advantage of the hash filtering.
106 SnpMode
->ReceiveFilterMask
= EFI_SIMPLE_NETWORK_RECEIVE_UNICAST
|
107 EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST
|
108 EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST
|
109 EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS
|
110 EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST
;
112 // We do not intend to receive anything for the time being.
113 SnpMode
->ReceiveFilterSetting
= 0;
115 // LAN9118 has 64bit hash table, can filter 64 MCast MAC Addresses
116 SnpMode
->MaxMCastFilterCount
= MAX_MCAST_FILTER_CNT
;
117 SnpMode
->MCastFilterCount
= 0;
118 ZeroMem (&SnpMode
->MCastFilter
, MAX_MCAST_FILTER_CNT
* sizeof(EFI_MAC_ADDRESS
));
120 // Set the interface type (1: Ethernet or 6: IEEE 802 Networks)
121 SnpMode
->IfType
= NET_IFTYPE_ETHERNET
;
123 // Mac address is changeable as it is loaded from erasable memory
124 SnpMode
->MacAddressChangeable
= TRUE
;
126 // Can only transmit one packet at a time
127 SnpMode
->MultipleTxSupported
= FALSE
;
129 // MediaPresent checks for cable connection and partner link
130 SnpMode
->MediaPresentSupported
= TRUE
;
131 SnpMode
->MediaPresent
= FALSE
;
133 // Set broadcast address
134 SetMem (&SnpMode
->BroadcastAddress
, sizeof (EFI_MAC_ADDRESS
), 0xFF);
136 // Power up the device so we can find the MAC address
137 Status
= Lan9118Initialize (Snp
);
138 if (EFI_ERROR (Status
)) {
139 DEBUG ((EFI_D_ERROR
, "LAN9118: Error initialising hardware\n"));
140 return EFI_DEVICE_ERROR
;
143 // Assign fields for device path
144 CopyMem (&Lan9118Path
->Lan9118
.MacAddress
, &Snp
->Mode
->CurrentAddress
, NET_ETHER_ADDR_LEN
);
145 Lan9118Path
->Lan9118
.IfType
= Snp
->Mode
->IfType
;
147 // Initialise the protocol
148 ControllerHandle
= NULL
;
149 Status
= gBS
->InstallMultipleProtocolInterfaces (
151 &gEfiSimpleNetworkProtocolGuid
, Snp
,
152 &gEfiDevicePathProtocolGuid
, Lan9118Path
,
155 // Say what the status of loading the protocol structure is
156 if (EFI_ERROR(Status
)) {
157 FreePool (LanDriver
);
159 LanDriver
->ControllerHandle
= ControllerHandle
;
166 * UEFI Start() function
170 * @param Snp: A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance.
174 * This function starts a network interface. If the network interface successfully starts, then
175 * EFI_SUCCESS will be returned.
180 IN EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
183 // Check Snp instance
185 return EFI_INVALID_PARAMETER
;
189 if ((Snp
->Mode
->State
== EfiSimpleNetworkStarted
) ||
190 (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) ) {
191 return EFI_ALREADY_STARTED
;
195 Snp
->Mode
->State
= EfiSimpleNetworkStarted
;
200 * UEFI Stop() function
206 IN EFI_SIMPLE_NETWORK_PROTOCOL
* Snp
209 // Check Snp Instance
211 return EFI_INVALID_PARAMETER
;
214 // Check state of the driver
215 if (Snp
->Mode
->State
== EfiSimpleNetworkStopped
) {
216 return EFI_NOT_STARTED
;
219 // Stop the Tx and Rx
220 StopTx (STOP_TX_CFG
| STOP_TX_MAC
, Snp
);
224 switch (Snp
->Mode
->State
) {
225 case EfiSimpleNetworkStarted
:
226 case EfiSimpleNetworkInitialized
:
227 Snp
->Mode
->State
= EfiSimpleNetworkStopped
;
230 return EFI_DEVICE_ERROR
;
233 // Put the device into a power saving mode ?
238 // Allocated receive and transmit buffers
239 STATIC UINT32 gTxBuffer
= 0;
242 * UEFI Initialize() function
248 IN EFI_SIMPLE_NETWORK_PROTOCOL
* Snp
,
249 IN UINTN RxBufferSize OPTIONAL
,
250 IN UINTN TxBufferSize OPTIONAL
259 // Initialize variables
260 // Global variables to hold tx and rx FIFO allocation
263 // Check Snp Instance
265 return EFI_INVALID_PARAMETER
;
268 // First check that driver has not already been initialized
269 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
270 DEBUG ((EFI_D_WARN
, "LAN9118 Driver already initialized\n"));
273 if (Snp
->Mode
->State
== EfiSimpleNetworkStopped
) {
274 DEBUG ((EFI_D_WARN
, "LAN9118 Driver not started\n"));
275 return EFI_NOT_STARTED
;
278 // Initiate a PHY reset
279 Status
= PhySoftReset (PHY_RESET_PMT
, Snp
);
280 if (EFI_ERROR (Status
)) {
281 Snp
->Mode
->State
= EfiSimpleNetworkStopped
;
282 DEBUG ((EFI_D_WARN
, "Warning: Link not ready after TimeOut. Check ethernet cable\n"));
283 return EFI_NOT_STARTED
;
286 // Initiate a software reset
287 Status
= SoftReset (0, Snp
);
288 if (EFI_ERROR(Status
)) {
289 DEBUG ((EFI_D_WARN
, "Soft Reset Failed: Hardware Error\n"));
290 return EFI_DEVICE_ERROR
;
293 // Read the PM register
294 PmConf
= Lan9118MmioRead32 (LAN9118_PMT_CTRL
);
296 // MPTCTRL_WOL_EN: Allow Wake-On-Lan to detect wake up frames or magic packets
297 // MPTCTRL_ED_EN: Allow energy detection to allow lowest power consumption mode
298 // MPTCTRL_PME_EN: Allow Power Management Events
300 PmConf
|= (MPTCTRL_WOL_EN
| MPTCTRL_ED_EN
| MPTCTRL_PME_EN
);
302 // Write the current configuration to the register
303 Lan9118MmioWrite32 (LAN9118_PMT_CTRL
, PmConf
);
305 // Configure GPIO and HW
306 Status
= ConfigureHardware (HW_CONF_USE_LEDS
, Snp
);
307 if (EFI_ERROR(Status
)) {
311 // Assign the transmitter buffer size (default values)
312 TxStatusSize
= LAN9118_TX_STATUS_SIZE
;
313 RxStatusSize
= LAN9118_RX_STATUS_SIZE
;
315 // Check that a buff size was specified
316 if (TxBufferSize
> 0) {
317 if (RxBufferSize
== 0) {
318 RxBufferSize
= LAN9118_RX_DATA_SIZE
;
321 AllocResult
= ChangeFifoAllocation (
330 if (AllocResult
< 0) {
331 return EFI_OUT_OF_RESOURCES
;
335 // Do auto-negotiation if supported
336 Status
= AutoNegotiate (AUTO_NEGOTIATE_ADVERTISE_ALL
, Snp
);
337 if (EFI_ERROR(Status
)) {
338 DEBUG ((EFI_D_WARN
, "LAN9118: Auto Negotiation failed.\n"));
341 // Configure flow control depending on speed capabilities
342 Status
= ConfigureFlow (0, 0, 0, 0, Snp
);
343 if (EFI_ERROR(Status
)) {
347 // Enable the transmitter
348 Status
= StartTx (START_TX_MAC
| START_TX_CFG
, Snp
);
349 if (EFI_ERROR(Status
)) {
353 // Now acknowledge all interrupts
354 Lan9118MmioWrite32 (LAN9118_INT_STS
, ~0);
356 // Declare the driver as initialized
357 Snp
->Mode
->State
= EfiSimpleNetworkInitialized
;
363 * UEFI Reset () function
369 IN EFI_SIMPLE_NETWORK_PROTOCOL
* Snp
,
370 IN BOOLEAN Verification
382 // Check Snp Instance
384 return EFI_INVALID_PARAMETER
;
387 // First check that driver has not already been initialized
388 if (Snp
->Mode
->State
== EfiSimpleNetworkStarted
) {
389 DEBUG ((EFI_D_WARN
, "Warning: LAN9118 Driver not yet initialized\n"));
390 return EFI_DEVICE_ERROR
;
391 } else if (Snp
->Mode
->State
== EfiSimpleNetworkStopped
) {
392 DEBUG ((EFI_D_WARN
, "Warning: LAN9118 Driver not started\n"));
393 return EFI_NOT_STARTED
;
396 // Initiate a PHY reset
397 Status
= PhySoftReset (PHY_RESET_PMT
, Snp
);
398 if (EFI_ERROR (Status
)) {
399 Snp
->Mode
->State
= EfiSimpleNetworkStopped
;
400 return EFI_NOT_STARTED
;
403 // Initiate a software reset
404 ResetFlags
|= SOFT_RESET_CHECK_MAC_ADDR_LOAD
| SOFT_RESET_CLEAR_INT
;
407 ResetFlags
|= SOFT_RESET_SELF_TEST
;
410 Status
= SoftReset (ResetFlags
, Snp
);
411 if (EFI_ERROR (Status
)) {
412 DEBUG ((EFI_D_WARN
, "Warning: Soft Reset Failed: Hardware Error\n"));
413 return EFI_DEVICE_ERROR
;
416 // Read the PM register
417 PmConf
= Lan9118MmioRead32 (LAN9118_PMT_CTRL
);
419 // MPTCTRL_WOL_EN: Allow Wake-On-Lan to detect wake up frames or magic packets
420 // MPTCTRL_ED_EN: Allow energy detection to allow lowest power consumption mode
421 // MPTCTRL_PME_EN: Allow Power Management Events
422 PmConf
|= (MPTCTRL_WOL_EN
| MPTCTRL_ED_EN
| MPTCTRL_PME_EN
);
424 // Write the current configuration to the register
425 Lan9118MmioWrite32 (LAN9118_PMT_CTRL
, PmConf
);
427 // Reactivate the LEDs
428 Status
= ConfigureHardware (HW_CONF_USE_LEDS
, Snp
);
429 if (EFI_ERROR (Status
)) {
433 // Check that a buffer size was specified in SnpInitialize
434 if (gTxBuffer
!= 0) {
435 HwConf
= Lan9118MmioRead32 (LAN9118_HW_CFG
); // Read the HW register
436 HwConf
&= ~HW_CFG_TX_FIFO_SIZE_MASK
; // Clear buffer bits first
437 HwConf
|= HW_CFG_TX_FIFO_SIZE(gTxBuffer
); // assign size chosen in SnpInitialize
439 Lan9118MmioWrite32 (LAN9118_HW_CFG
, HwConf
); // Write the conf
442 // Enable the receiver and transmitter and clear their contents
443 StartRx (START_RX_CLEAR
, Snp
);
444 StartTx (START_TX_MAC
| START_TX_CFG
| START_TX_CLEAR
, Snp
);
446 // Now acknowledge all interrupts
447 Lan9118MmioWrite32 (LAN9118_INT_STS
, ~0);
453 * UEFI Shutdown () function
459 IN EFI_SIMPLE_NETWORK_PROTOCOL
* Snp
464 // Check Snp Instance
466 return EFI_INVALID_PARAMETER
;
469 // First check that driver has not already been initialized
470 if (Snp
->Mode
->State
== EfiSimpleNetworkStarted
) {
471 DEBUG ((EFI_D_WARN
, "Warning: LAN9118 Driver not yet initialized\n"));
472 return EFI_DEVICE_ERROR
;
473 } else if (Snp
->Mode
->State
== EfiSimpleNetworkStopped
) {
474 DEBUG ((EFI_D_WARN
, "Warning: LAN9118 Driver not started\n"));
475 return EFI_NOT_STARTED
;
478 // Initiate a PHY reset
479 Status
= PhySoftReset (PHY_RESET_PMT
, Snp
);
480 if (EFI_ERROR (Status
)) {
484 // Initiate a software reset
485 Status
= SoftReset (0, Snp
);
486 if (EFI_ERROR (Status
)) {
487 DEBUG ((EFI_D_WARN
, "Warning: Soft Reset Failed: Hardware Error\n"));
491 // Back to the started and thus not initialized state
492 Snp
->Mode
->State
= EfiSimpleNetworkStarted
;
498 Enable and/or disable the receive filters of the LAN9118
500 Please refer to the UEFI specification for the precedence rules among the
501 Enable, Disable and ResetMCastFilter parameters.
503 @param[in] Snp A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL
505 @param[in] Enable A bit mask of receive filters to enable.
506 @param[in] Disable A bit mask of receive filters to disable.
507 @param[in] ResetMCastFilter Set to TRUE to reset the contents of the multicast
508 receive filters on the network interface to
509 their default values.
510 @param[in] MCastFilterCnt Number of multicast HW MAC addresses in the new
511 MCastFilter list. This value must be less than or
512 equal to the MCastFilterCnt field of
513 EFI_SIMPLE_NETWORK_MODE. This field is optional if
514 ResetMCastFilter is TRUE.
515 @param[in] MCastFilter A pointer to a list of new multicast receive
516 filter HW MAC addresses. This list will replace
517 any existing multicast HW MAC address list. This
518 field is optional if ResetMCastFilter is TRUE.
520 @retval EFI_SUCCESS The receive filters of the LAN9118 were updated.
521 @retval EFI_NOT_STARTED The LAN9118 has not been started.
522 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE :
524 . Multicast is being enabled (the
525 EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST bit is set in
526 Enable, it is not set in Disable, and ResetMCastFilter
527 is FALSE) and MCastFilterCount is zero.
528 . Multicast is being enabled and MCastFilterCount is
529 greater than Snp->Mode->MaxMCastFilterCount.
530 . Multicast is being enabled and MCastFilter is NULL
531 . Multicast is being enabled and one or more of the
532 addresses in the MCastFilter list are not valid
533 multicast MAC addresses.
534 @retval EFI_DEVICE_ERROR The LAN9118 has been started but not initialized.
540 IN EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
,
543 IN BOOLEAN ResetMCastFilter
,
544 IN UINTN MCastFilterCnt OPTIONAL
,
545 IN EFI_MAC_ADDRESS
*MCastFilter OPTIONAL
548 EFI_SIMPLE_NETWORK_MODE
*Mode
;
549 UINT32 MultHashTableHigh
;
550 UINT32 MultHashTableLow
;
555 UINT32 ReceiveFilterSetting
;
556 EFI_MAC_ADDRESS
*Mac
;
557 EFI_MAC_ADDRESS ZeroMac
;
559 // Check Snp Instance
561 return EFI_INVALID_PARAMETER
;
565 // Check that driver was started and initialised
566 if (Mode
->State
== EfiSimpleNetworkStarted
) {
567 DEBUG ((EFI_D_WARN
, "Warning: LAN9118 Driver not initialized\n"));
568 return EFI_DEVICE_ERROR
;
569 } else if (Mode
->State
== EfiSimpleNetworkStopped
) {
570 DEBUG ((EFI_D_WARN
, "Warning: LAN9118 Driver in stopped state\n"));
571 return EFI_NOT_STARTED
;
574 if ((Enable
& (~Mode
->ReceiveFilterMask
)) ||
575 (Disable
& (~Mode
->ReceiveFilterMask
)) ) {
576 return EFI_INVALID_PARAMETER
;
580 // Check the validity of the multicast setting and compute the
581 // hash values of the multicast mac addresses to listen to.
584 MultHashTableHigh
= 0;
585 MultHashTableLow
= 0;
586 if ((!ResetMCastFilter
) &&
587 ((Disable
& EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST
) == 0) &&
588 ((Enable
& EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST
) != 0) ) {
589 if ((MCastFilterCnt
== 0) ||
590 (MCastFilterCnt
> Mode
->MaxMCastFilterCount
) ||
591 (MCastFilter
== NULL
) ) {
592 return EFI_INVALID_PARAMETER
;
595 // Check the validity of all multicast addresses before to change
598 for (Count
= 0; Count
< MCastFilterCnt
; Count
++) {
599 if ((MCastFilter
[Count
].Addr
[0] & 1) == 0) {
600 return EFI_INVALID_PARAMETER
;
605 // Go through each filter address and set appropriate bits on hash table
607 for (Count
= 0; Count
< MCastFilterCnt
; Count
++) {
608 Mac
= &(MCastFilter
[Count
]);
609 CopyMem (&Mode
->MCastFilter
[Count
], Mac
, sizeof(EFI_MAC_ADDRESS
));
611 Crc
= GenEtherCrc32 (Mac
, NET_ETHER_ADDR_LEN
);
612 //gBS->CalculateCrc32 ((VOID*)&Mfilter[Count],6,&Crc); <-- doesn't work as desired
615 // The most significant 6 bits of the MAC address CRC constitute the hash
616 // value of the MAC address.
618 HashValue
= (Crc
>> 26) & 0x3F;
620 // Select hashlow register if MSB is not set
621 if ((HashValue
& 0x20) == 0) {
622 MultHashTableLow
|= (1 << HashValue
);
624 MultHashTableHigh
|= (1 << (HashValue
& 0x1F));
627 Mode
->MCastFilterCount
= MCastFilterCnt
;
628 } else if (ResetMCastFilter
) {
629 Mode
->MCastFilterCount
= 0;
631 MultHashTableLow
= IndirectMACRead32 (INDIRECT_MAC_INDEX_HASHL
);
632 MultHashTableHigh
= IndirectMACRead32 (INDIRECT_MAC_INDEX_HASHH
);
636 // Before to change anything, stop and reset the reception of
639 StopRx (STOP_RX_CLEAR
, Snp
);
642 // Write the mask of the selected hash values for the multicast filtering.
643 // The two masks are set to zero if the multicast filtering is not enabled.
645 IndirectMACWrite32 (INDIRECT_MAC_INDEX_HASHL
, MultHashTableLow
);
646 IndirectMACWrite32 (INDIRECT_MAC_INDEX_HASHH
, MultHashTableHigh
);
648 ReceiveFilterSetting
= (Mode
->ReceiveFilterSetting
| Enable
) & (~Disable
);
651 // Read MAC controller
653 MacCSRValue
= IndirectMACRead32 (INDIRECT_MAC_INDEX_CR
);
654 MacCSRValue
&= ~(MACCR_HPFILT
| MACCR_BCAST
| MACCR_PRMS
| MACCR_MCPAS
);
656 if (ReceiveFilterSetting
& EFI_SIMPLE_NETWORK_RECEIVE_UNICAST
) {
657 Lan9118SetMacAddress (&Mode
->CurrentAddress
, Snp
);
658 DEBUG ((DEBUG_NET
, "Allowing Unicast Frame Reception\n"));
661 // The Unicast packets do not have to be listen to, set the MAC
662 // address of the LAN9118 to be the "not configured" all zeroes
663 // ethernet MAC address.
665 ZeroMem (&ZeroMac
, NET_ETHER_ADDR_LEN
);
666 Lan9118SetMacAddress (&ZeroMac
, Snp
);
669 if (ReceiveFilterSetting
& EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST
) {
670 MacCSRValue
|= MACCR_HPFILT
;
671 DEBUG ((DEBUG_NET
, "Allowing Multicast Frame Reception\n"));
674 if (ReceiveFilterSetting
& EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST
) {
675 MacCSRValue
|= MACCR_MCPAS
;
676 DEBUG ((DEBUG_NET
, "Enabling Promiscuous Multicast Mode\n"));
679 if ((ReceiveFilterSetting
& EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST
) == 0) {
680 MacCSRValue
|= MACCR_BCAST
;
682 DEBUG ((DEBUG_NET
, "Allowing Broadcast Frame Reception\n"));
685 if (ReceiveFilterSetting
& EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS
) {
686 MacCSRValue
|= MACCR_PRMS
;
687 DEBUG ((DEBUG_NET
, "Enabling Promiscuous Mode\n"));
691 // Write the options to the MAC_CSR
693 IndirectMACWrite32 (INDIRECT_MAC_INDEX_CR
, MacCSRValue
);
696 // If we have to retrieve something, start packet reception.
698 Mode
->ReceiveFilterSetting
= ReceiveFilterSetting
;
699 if (ReceiveFilterSetting
!= 0) {
707 Modify of reset the current station address
709 @param[in] Snp A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL
711 @param[in] Reset Flag used to reset the station address to the
712 LAN9118's permanent address.
713 @param[in] New New station address to be used for the network interface.
715 @retval EFI_SUCCESS The LAN9118's station address was updated.
716 @retval EFI_NOT_STARTED The LAN9118 has not been started.
717 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE :
718 . The "New" station address is invalid.
719 . "Reset" is FALSE and "New" is NULL.
720 @retval EFI_DEVICE_ERROR The LAN9118 has been started but not initialized.
726 IN EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
,
728 IN EFI_MAC_ADDRESS
*New
732 UINT8 PermAddr
[NET_ETHER_ADDR_LEN
];
734 DEBUG ((DEBUG_NET
, "SnpStationAddress()\n"));
736 // Check Snp instance
738 return EFI_INVALID_PARAMETER
;
741 // Check that driver was started and initialised
742 if (Snp
->Mode
->State
== EfiSimpleNetworkStarted
) {
743 DEBUG ((EFI_D_WARN
, "Warning: LAN9118 Driver not initialized\n"));
744 return EFI_DEVICE_ERROR
;
745 } else if (Snp
->Mode
->State
== EfiSimpleNetworkStopped
) {
746 DEBUG ((EFI_D_WARN
, "Warning: LAN9118 Driver in stopped state\n"));
747 return EFI_NOT_STARTED
;
750 // Get the Permanent MAC address if need reset
752 // Try using EEPROM first. Read the first byte of data from EEPROM at the address 0x0
753 if ((IndirectEEPROMRead32 (0) & 0xFF) == EEPROM_EXTERNAL_SERIAL_EEPROM
) {
754 for (Count
= 0; Count
< NET_ETHER_ADDR_LEN
; Count
++) {
755 PermAddr
[Count
] = IndirectEEPROMRead32 (Count
+ 1);
757 New
= (EFI_MAC_ADDRESS
*) PermAddr
;
758 Lan9118SetMacAddress ((EFI_MAC_ADDRESS
*) PermAddr
, Snp
);
760 DEBUG ((EFI_D_ERROR
, "LAN9118: Warning: No valid MAC address in EEPROM, using fallback\n"));
761 New
= (EFI_MAC_ADDRESS
*) (FixedPcdGet64 (PcdLan9118DefaultMacAddress
));
764 // Otherwise use the specified new MAC address
766 return EFI_INVALID_PARAMETER
;
769 // If it is a multicast address, it is not valid.
771 if (New
->Addr
[0] & 0x01) {
772 return EFI_INVALID_PARAMETER
;
776 CopyMem (&Snp
->Mode
->CurrentAddress
, New
, NET_ETHER_ADDR_LEN
);
779 // If packet reception is currently activated, stop and reset it,
780 // set the new ethernet address and restart the packet reception.
781 // Otherwise, nothing to do, the MAC address will be updated in
782 // SnpReceiveFilters() when the UNICAST packet reception will be
785 if (Snp
->Mode
->ReceiveFilterSetting
!= 0) {
786 StopRx (STOP_RX_CLEAR
, Snp
);
787 Lan9118SetMacAddress (New
, Snp
);
795 * UEFI Statistics() function
801 IN EFI_SIMPLE_NETWORK_PROTOCOL
* Snp
,
803 IN OUT UINTN
*StatSize
,
804 OUT EFI_NETWORK_STATISTICS
*Statistics
807 LAN9118_DRIVER
*LanDriver
;
810 LanDriver
= INSTANCE_FROM_SNP_THIS (Snp
);
812 DEBUG ((DEBUG_NET
, "SnpStatistics()\n"));
814 // Check Snp instance
816 return EFI_INVALID_PARAMETER
;
819 // Check that driver was started and initialised
820 if (Snp
->Mode
->State
== EfiSimpleNetworkStarted
) {
821 DEBUG ((EFI_D_WARN
, "Warning: LAN9118 Driver not initialized\n"));
822 return EFI_DEVICE_ERROR
;
823 } else if (Snp
->Mode
->State
== EfiSimpleNetworkStopped
) {
824 DEBUG ((EFI_D_WARN
, "Warning: LAN9118 Driver in stopped state\n"));
825 return EFI_NOT_STARTED
;
829 // Do a reset if required. It is not clearly stated in the UEFI specification
830 // whether the reset has to be done before to copy the statistics in "Statictics"
831 // or after. It is a bit strange to do it before but that is what is expected by
832 // the SCT test on Statistics() with reset : "0x3de76704,0x4bf5,0x42cd,0x8c,0x89,
833 // 0x54,0x7e,0x4f,0xad,0x4f,0x24".
836 ZeroMem (&LanDriver
->Stats
, sizeof(EFI_NETWORK_STATISTICS
));
839 Status
= EFI_SUCCESS
;
840 if (StatSize
== NULL
) {
841 if (Statistics
!= NULL
) {
842 return EFI_INVALID_PARAMETER
;
845 if (Statistics
== NULL
) {
846 Status
= EFI_BUFFER_TOO_SMALL
;
848 // Fill in the statistics
850 Statistics
, &LanDriver
->Stats
,
851 MIN (*StatSize
, sizeof (EFI_NETWORK_STATISTICS
))
853 if (*StatSize
< sizeof (EFI_NETWORK_STATISTICS
)) {
854 Status
= EFI_BUFFER_TOO_SMALL
;
857 *StatSize
= sizeof (EFI_NETWORK_STATISTICS
);
864 * UEFI MCastIPtoMAC() function
870 IN EFI_SIMPLE_NETWORK_PROTOCOL
* Snp
,
872 IN EFI_IP_ADDRESS
*Ip
,
873 OUT EFI_MAC_ADDRESS
*McastMac
876 DEBUG ((DEBUG_NET
, "SnpMcastIptoMac()\n"));
878 // Check Snp instance
880 return EFI_INVALID_PARAMETER
;
883 // Check that driver was started and initialised
884 if (Snp
->Mode
->State
== EfiSimpleNetworkStarted
) {
885 DEBUG ((EFI_D_WARN
, "Warning: LAN9118 Driver not initialized\n"));
886 return EFI_DEVICE_ERROR
;
887 } else if (Snp
->Mode
->State
== EfiSimpleNetworkStopped
) {
888 DEBUG ((EFI_D_WARN
, "Warning: LAN9118 Driver in stopped state\n"));
889 return EFI_NOT_STARTED
;
893 if ((McastMac
== NULL
) || (Ip
== NULL
)) {
894 return EFI_INVALID_PARAMETER
;
897 // Make sure MAC address is empty
898 ZeroMem (McastMac
, sizeof(EFI_MAC_ADDRESS
));
900 // If we need ipv4 address
902 // Most significant 25 bits of a multicast HW address are set.
903 // 01-00-5E is the IPv4 Ethernet Multicast Address (see RFC 1112)
904 McastMac
->Addr
[0] = 0x01;
905 McastMac
->Addr
[1] = 0x00;
906 McastMac
->Addr
[2] = 0x5E;
908 // Lower 23 bits from ipv4 address
909 McastMac
->Addr
[3] = (Ip
->v4
.Addr
[1] & 0x7F); // Clear the most significant bit (25th bit of MAC must be 0)
910 McastMac
->Addr
[4] = Ip
->v4
.Addr
[2];
911 McastMac
->Addr
[5] = Ip
->v4
.Addr
[3];
913 // Most significant 16 bits of multicast v6 HW address are set
914 // 33-33 is the IPv6 Ethernet Multicast Address (see RFC 2464)
915 McastMac
->Addr
[0] = 0x33;
916 McastMac
->Addr
[1] = 0x33;
918 // lower four octets are taken from ipv6 address
919 McastMac
->Addr
[2] = Ip
->v6
.Addr
[8];
920 McastMac
->Addr
[3] = Ip
->v6
.Addr
[9];
921 McastMac
->Addr
[4] = Ip
->v6
.Addr
[10];
922 McastMac
->Addr
[5] = Ip
->v6
.Addr
[11];
929 * UEFI NvData() function
935 IN EFI_SIMPLE_NETWORK_PROTOCOL
* pobj
,
936 IN BOOLEAN read_write
,
942 DEBUG ((DEBUG_NET
, "SnpNvData()\n"));
944 return EFI_UNSUPPORTED
;
949 * UEFI GetStatus () function
955 IN EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
,
956 OUT UINT32
*IrqStat OPTIONAL
,
957 OUT VOID
**TxBuff OPTIONAL
962 UINTN NumTxStatusEntries
;
966 LAN9118_DRIVER
*LanDriver
;
968 LanDriver
= INSTANCE_FROM_SNP_THIS (Snp
);
970 // Check preliminaries
972 return EFI_INVALID_PARAMETER
;
975 // Check that driver was started and initialised
976 if (Snp
->Mode
->State
== EfiSimpleNetworkStarted
) {
977 DEBUG ((EFI_D_WARN
, "Warning: LAN9118 Driver not initialized\n"));
978 return EFI_DEVICE_ERROR
;
979 } else if (Snp
->Mode
->State
== EfiSimpleNetworkStopped
) {
980 DEBUG ((EFI_D_WARN
, "Warning: LAN9118 Driver in stopped state\n"));
981 return EFI_NOT_STARTED
;
984 // Check and acknowledge TX Status interrupt (this will happen if the
985 // consumer of SNP does not call GetStatus.)
986 // TODO will we lose TxStatuses if this happens? Maybe in SnpTransmit we
987 // should check for it and dump the TX Status FIFO.
988 FifoInt
= Lan9118MmioRead32 (LAN9118_FIFO_INT
);
990 // Clear the TX Status FIFO Overflow
991 if ((FifoInt
& INSTS_TXSO
) == 0) {
992 FifoInt
|= INSTS_TXSO
;
993 Lan9118MmioWrite32 (LAN9118_FIFO_INT
, FifoInt
);
996 // Read interrupt status if IrqStat is not NULL
997 if (IrqStat
!= NULL
) {
1000 // Check for receive interrupt
1001 if (Lan9118MmioRead32 (LAN9118_INT_STS
) & INSTS_RSFL
) { // Data moved from rx FIFO
1002 *IrqStat
|= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT
;
1003 Lan9118MmioWrite32 (LAN9118_INT_STS
,INSTS_RSFL
);
1006 // Check for transmit interrupt
1007 if (Lan9118MmioRead32 (LAN9118_INT_STS
) & INSTS_TSFL
) {
1008 *IrqStat
|= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT
;
1009 Lan9118MmioWrite32 (LAN9118_INT_STS
,INSTS_TSFL
);
1012 // Check for software interrupt
1013 if (Lan9118MmioRead32 (LAN9118_INT_STS
) & INSTS_SW_INT
) {
1014 *IrqStat
|= EFI_SIMPLE_NETWORK_SOFTWARE_INTERRUPT
;
1015 Lan9118MmioWrite32 (LAN9118_INT_STS
,INSTS_SW_INT
);
1019 // Check Status of transmitted packets
1020 // (We ignore TXSTATUS_NO_CA has it might happen in Full Duplex)
1022 NumTxStatusEntries
= Lan9118MmioRead32(LAN9118_TX_FIFO_INF
) & TXFIFOINF_TXSUSED_MASK
;
1023 if (NumTxStatusEntries
> 0) {
1024 TxStatus
= Lan9118MmioRead32 (LAN9118_TX_STATUS
);
1025 PacketTag
= TxStatus
>> 16;
1026 TxStatus
= TxStatus
& 0xFFFF;
1027 if ((TxStatus
& TXSTATUS_ES
) && (TxStatus
!= (TXSTATUS_ES
| TXSTATUS_NO_CA
))) {
1028 DEBUG ((EFI_D_ERROR
, "LAN9118: There was an error transmitting. TxStatus=0x%08x:", TxStatus
));
1029 if (TxStatus
& TXSTATUS_NO_CA
) {
1030 DEBUG ((EFI_D_ERROR
, "- No carrier\n"));
1032 if (TxStatus
& TXSTATUS_DEF
) {
1033 DEBUG ((EFI_D_ERROR
, "- Packet tx was deferred\n"));
1035 if (TxStatus
& TXSTATUS_EDEF
) {
1036 DEBUG ((EFI_D_ERROR
, "- Tx ended because of excessive deferral\n"));
1038 if (TxStatus
& TXSTATUS_ECOLL
) {
1039 DEBUG ((EFI_D_ERROR
, "- Tx ended because of Excessive Collisions\n"));
1041 if (TxStatus
& TXSTATUS_LCOLL
) {
1042 DEBUG ((EFI_D_ERROR
, "- Packet Tx aborted after coll window of 64 bytes\n"));
1044 if (TxStatus
& TXSTATUS_LOST_CA
) {
1045 DEBUG ((EFI_D_ERROR
, "- Lost carrier during Tx\n"));
1047 return EFI_DEVICE_ERROR
;
1048 } else if (TxBuff
!= NULL
) {
1049 LanDriver
->Stats
.TxTotalFrames
+= 1;
1050 *TxBuff
= LanDriver
->TxRing
[PacketTag
% LAN9118_TX_RING_NUM_ENTRIES
];
1052 } else if (TxBuff
!= NULL
) {
1056 // Check for a TX Error interrupt
1057 Interrupts
= Lan9118MmioRead32 (LAN9118_INT_STS
);
1058 if (Interrupts
& INSTS_TXE
) {
1059 DEBUG ((EFI_D_ERROR
, "LAN9118: Transmitter error. Restarting..."));
1061 // Software reset, the TXE interrupt is cleared by the reset.
1062 Status
= SoftReset (0, Snp
);
1063 if (EFI_ERROR (Status
)) {
1064 DEBUG ((EFI_D_ERROR
, "\n\tSoft Reset Failed: Hardware Error\n"));
1065 return EFI_DEVICE_ERROR
;
1068 // Reactivate the LEDs
1069 Status
= ConfigureHardware (HW_CONF_USE_LEDS
, Snp
);
1070 if (EFI_ERROR (Status
)) {
1075 // Restart the transmitter and if necessary the receiver.
1076 // Do not ask for FIFO reset as it has already been done
1079 StartTx (START_TX_MAC
| START_TX_CFG
, Snp
);
1080 if (Snp
->Mode
->ReceiveFilterSetting
!= 0) {
1085 // Update the media status
1086 Status
= CheckLinkStatus (0, Snp
);
1087 if (EFI_ERROR(Status
)) {
1088 Snp
->Mode
->MediaPresent
= FALSE
;
1090 Snp
->Mode
->MediaPresent
= TRUE
;
1098 * UEFI Transmit() function
1104 IN EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
,
1108 IN EFI_MAC_ADDRESS
*SrcAddr OPTIONAL
,
1109 IN EFI_MAC_ADDRESS
*DstAddr OPTIONAL
,
1110 IN UINT16
*Protocol OPTIONAL
1113 LAN9118_DRIVER
*LanDriver
;
1115 UINT32 TxStatusSpace
;
1119 UINT16 LocalProtocol
;
1123 #if defined(EVAL_PERFORMANCE)
1128 Perf
= GetPerformanceCounterProperties (NULL
, NULL
);
1129 StartClock
= GetPerformanceCounter ();
1132 LanDriver
= INSTANCE_FROM_SNP_THIS (Snp
);
1134 // Check preliminaries
1135 if ((Snp
== NULL
) || (Data
== NULL
)) {
1136 return EFI_INVALID_PARAMETER
;
1139 // Check that driver was started and initialised
1140 if (Snp
->Mode
->State
== EfiSimpleNetworkStarted
) {
1141 DEBUG ((EFI_D_WARN
, "Warning: LAN9118 Driver not initialized\n"));
1142 return EFI_DEVICE_ERROR
;
1143 } else if (Snp
->Mode
->State
== EfiSimpleNetworkStopped
) {
1144 DEBUG ((EFI_D_WARN
, "Warning: LAN9118 Driver in stopped state\n"));
1145 return EFI_NOT_STARTED
;
1148 // Ensure header is correct size if non-zero
1150 if (HdrSize
!= Snp
->Mode
->MediaHeaderSize
) {
1151 return EFI_INVALID_PARAMETER
;
1154 if ((DstAddr
== NULL
) || (Protocol
== NULL
)) {
1155 return EFI_INVALID_PARAMETER
;
1160 // Check validity of BufferSize
1162 if (BuffSize
< Snp
->Mode
->MediaHeaderSize
) {
1163 return EFI_BUFFER_TOO_SMALL
;
1166 // Before transmitting check the link status
1167 /*if (CheckLinkStatus (0, Snp) < 0) {
1168 return EFI_NOT_READY;
1171 // Get DATA FIFO free space in bytes
1172 TxFreeSpace
= TxDataFreeSpace (0, Snp
);
1173 if (TxFreeSpace
< BuffSize
) {
1174 return EFI_NOT_READY
;
1177 // Get STATUS FIFO used space in bytes
1178 TxStatusSpace
= TxStatusUsedSpace (0, Snp
);
1179 if (TxStatusSpace
> 500) {
1180 return EFI_NOT_READY
;
1183 // If DstAddr is not provided, get it from Buffer (we trust that the caller
1184 // has provided a well-formed frame).
1185 if (DstAddr
== NULL
) {
1186 DstAddr
= (EFI_MAC_ADDRESS
*) Data
;
1189 // Check for the nature of the frame
1190 if ((DstAddr
->Addr
[0] & 0x1) == 1) {
1191 LanDriver
->Stats
.TxMulticastFrames
+= 1;
1193 LanDriver
->Stats
.TxUnicastFrames
+= 1;
1196 // Check if broadcast
1197 if (DstAddr
->Addr
[0] == 0xFF) {
1198 LanDriver
->Stats
.TxBroadcastFrames
+= 1;
1201 PacketTag
= LanDriver
->NextPacketTag
;
1202 LanDriver
->NextPacketTag
++;
1207 LocalData
= (UINT32
*) Data
;
1208 LocalProtocol
= *Protocol
;
1210 // Create first buffer to pass to controller (for the header)
1211 CommandA
= TX_CMD_A_FIRST_SEGMENT
| TX_CMD_A_BUFF_SIZE (HdrSize
);
1212 CommandB
= TX_CMD_B_PACKET_TAG (PacketTag
) | TX_CMD_B_PACKET_LENGTH (BuffSize
);
1214 // Write the commands first
1215 Lan9118MmioWrite32 (LAN9118_TX_DATA
, CommandA
);
1216 Lan9118MmioWrite32 (LAN9118_TX_DATA
, CommandB
);
1218 // Write the destination address
1219 Lan9118MmioWrite32 (LAN9118_TX_DATA
,
1220 (DstAddr
->Addr
[0]) |
1221 (DstAddr
->Addr
[1] << 8) |
1222 (DstAddr
->Addr
[2] << 16) |
1223 (DstAddr
->Addr
[3] << 24)
1226 Lan9118MmioWrite32 (LAN9118_TX_DATA
,
1227 (DstAddr
->Addr
[4]) |
1228 (DstAddr
->Addr
[5] << 8) |
1229 (SrcAddr
->Addr
[0] << 16) | // Write the Source Address
1230 (SrcAddr
->Addr
[1] << 24)
1233 Lan9118MmioWrite32 (LAN9118_TX_DATA
,
1234 (SrcAddr
->Addr
[2]) |
1235 (SrcAddr
->Addr
[3] << 8) |
1236 (SrcAddr
->Addr
[4] << 16) |
1237 (SrcAddr
->Addr
[5] << 24)
1240 // Write the Protocol
1241 Lan9118MmioWrite32 (LAN9118_TX_DATA
, (UINT32
)(HTONS (LocalProtocol
)));
1243 // Next buffer is the payload
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
1246 // Write the commands
1247 Lan9118MmioWrite32 (LAN9118_TX_DATA
, CommandA
);
1248 Lan9118MmioWrite32 (LAN9118_TX_DATA
, CommandB
);
1250 // Write the payload
1251 for (Count
= 0; Count
< ((BuffSize
+ 3) >> 2) - 3; Count
++) {
1252 Lan9118MmioWrite32 (LAN9118_TX_DATA
, LocalData
[Count
+ 3]);
1256 LocalData
= (UINT32
*) Data
;
1258 // Create a buffer to pass to controller
1259 CommandA
= TX_CMD_A_FIRST_SEGMENT
| TX_CMD_A_LAST_SEGMENT
| TX_CMD_A_BUFF_SIZE (BuffSize
) | TX_CMD_A_COMPLETION_INT
;
1260 CommandB
= TX_CMD_B_PACKET_TAG (PacketTag
) | TX_CMD_B_PACKET_LENGTH (BuffSize
);
1262 // Write the commands first
1263 Lan9118MmioWrite32 (LAN9118_TX_DATA
, CommandA
);
1264 Lan9118MmioWrite32 (LAN9118_TX_DATA
, CommandB
);
1266 // Write all the data
1267 for (Count
= 0; Count
< ((BuffSize
+ 3) >> 2); Count
++) {
1268 Lan9118MmioWrite32 (LAN9118_TX_DATA
, LocalData
[Count
]);
1272 // Save the address of the submitted packet so we can notify the consumer that
1273 // it has been sent in GetStatus. When the packet tag appears in the Tx Status
1274 // Fifo, we will return Buffer in the TxBuff parameter of GetStatus.
1275 LanDriver
->TxRing
[PacketTag
% LAN9118_TX_RING_NUM_ENTRIES
] = Data
;
1277 #if defined(EVAL_PERFORMANCE)
1278 EndClock
= GetPerformanceCounter ();
1279 DEBUG ((EFI_D_ERROR
, "Time processing: %d counts @ %d Hz\n", StartClock
- EndClock
,Perf
));
1282 LanDriver
->Stats
.TxGoodFrames
+= 1;
1289 * UEFI Receive() function
1295 IN EFI_SIMPLE_NETWORK_PROTOCOL
* Snp
,
1296 OUT UINTN
*HdrSize OPTIONAL
,
1297 IN OUT UINTN
*BuffSize
,
1299 OUT EFI_MAC_ADDRESS
*SrcAddr OPTIONAL
,
1300 OUT EFI_MAC_ADDRESS
*DstAddr OPTIONAL
,
1301 OUT UINT16
*Protocol OPTIONAL
1304 LAN9118_DRIVER
*LanDriver
;
1306 UINT32 RxFifoStatus
;
1309 UINT32 PLength
; // Packet length
1314 EFI_MAC_ADDRESS Dst
;
1315 EFI_MAC_ADDRESS Src
;
1316 UINTN DroppedFrames
;
1319 LanDriver
= INSTANCE_FROM_SNP_THIS (Snp
);
1321 #if defined(EVAL_PERFORMANCE)
1322 UINT64 Perf
= GetPerformanceCounterProperties (NULL
, NULL
);
1323 UINT64 StartClock
= GetPerformanceCounter ();
1326 // Check preliminaries
1327 if ((Snp
== NULL
) || (Data
== NULL
) || (BuffSize
== NULL
)) {
1328 return EFI_INVALID_PARAMETER
;
1331 // Check that driver was started and initialised
1332 if (Snp
->Mode
->State
== EfiSimpleNetworkStarted
) {
1333 DEBUG ((EFI_D_WARN
, "Warning: LAN9118 Driver not initialized\n"));
1334 return EFI_DEVICE_ERROR
;
1335 } else if (Snp
->Mode
->State
== EfiSimpleNetworkStopped
) {
1336 DEBUG ((EFI_D_WARN
, "Warning: LAN9118 Driver in stopped state\n"));
1337 return EFI_NOT_STARTED
;
1341 // If the receiver raised the RXE error bit, check if the receiver status
1342 // FIFO is full and if not just acknowledge the error. The two other
1343 // conditions to get a RXE error are :
1344 // . the RX data FIFO is read whereas being empty.
1345 // . the RX status FIFO is read whereas being empty.
1346 // The RX data and status FIFO are read by this driver only in the following
1347 // code of this function. After the readings, the RXE error bit is checked
1348 // and if raised, the controller is reset. Thus, at this point, we consider
1349 // that the only valid reason to get an RXE error is the receiver status
1350 // FIFO being full. And if this is not the case, we consider that this is
1351 // a spurious error and we just get rid of it. We experienced such 'spurious'
1352 // errors when running the driver on an A57 on Juno. No valid reason to
1353 // explain those errors has been found so far and everything seems to
1354 // work perfectly when they are just ignored.
1356 IntSts
= Lan9118MmioRead32 (LAN9118_INT_STS
);
1357 if ((IntSts
& INSTS_RXE
) && (!(IntSts
& INSTS_RSFF
))) {
1358 Lan9118MmioWrite32 (LAN9118_INT_STS
, INSTS_RXE
);
1361 // Count dropped frames
1362 DroppedFrames
= Lan9118MmioRead32 (LAN9118_RX_DROP
);
1363 LanDriver
->Stats
.RxDroppedFrames
+= DroppedFrames
;
1365 NumPackets
= RxStatusUsedSpace (0, Snp
) / 4;
1367 return EFI_NOT_READY
;
1370 // Read Rx Status (only if not empty)
1371 RxFifoStatus
= Lan9118MmioRead32 (LAN9118_RX_STATUS
);
1372 LanDriver
->Stats
.RxTotalFrames
+= 1;
1374 // First check for errors
1375 if ((RxFifoStatus
& RXSTATUS_MII_ERROR
) ||
1376 (RxFifoStatus
& RXSTATUS_RXW_TO
) ||
1377 (RxFifoStatus
& RXSTATUS_FTL
) ||
1378 (RxFifoStatus
& RXSTATUS_LCOLL
) ||
1379 (RxFifoStatus
& RXSTATUS_LE
) ||
1380 (RxFifoStatus
& RXSTATUS_DB
))
1382 DEBUG ((EFI_D_WARN
, "Warning: There was an error on frame reception.\n"));
1383 return EFI_DEVICE_ERROR
;
1386 // Check if we got a CRC error
1387 if (RxFifoStatus
& RXSTATUS_CRC_ERROR
) {
1388 DEBUG ((EFI_D_WARN
, "Warning: Crc Error\n"));
1389 LanDriver
->Stats
.RxCrcErrorFrames
+= 1;
1390 LanDriver
->Stats
.RxDroppedFrames
+= 1;
1391 return EFI_DEVICE_ERROR
;
1394 // Check if we got a runt frame
1395 if (RxFifoStatus
& RXSTATUS_RUNT
) {
1396 DEBUG ((EFI_D_WARN
, "Warning: Runt Frame\n"));
1397 LanDriver
->Stats
.RxUndersizeFrames
+= 1;
1398 LanDriver
->Stats
.RxDroppedFrames
+= 1;
1399 return EFI_DEVICE_ERROR
;
1402 // Check filtering status for this packet
1403 if (RxFifoStatus
& RXSTATUS_FILT_FAIL
) {
1404 DEBUG ((EFI_D_WARN
, "Warning: Frame Failed Filtering\n"));
1408 // Check if we got a broadcast frame
1409 if (RxFifoStatus
& RXSTATUS_BCF
) {
1410 LanDriver
->Stats
.RxBroadcastFrames
+= 1;
1413 // Check if we got a multicast frame
1414 if (RxFifoStatus
& RXSTATUS_MCF
) {
1415 LanDriver
->Stats
.RxMulticastFrames
+= 1;
1418 // Check if we got a unicast frame
1419 if ((RxFifoStatus
& RXSTATUS_BCF
) && ((RxFifoStatus
& RXSTATUS_MCF
) == 0)) {
1420 LanDriver
->Stats
.RxUnicastFrames
+= 1;
1423 // Get the received packet length
1424 PLength
= GET_RXSTATUS_PACKET_LENGTH(RxFifoStatus
);
1425 LanDriver
->Stats
.RxTotalBytes
+= (PLength
- 4);
1427 // If padding is applied, read more DWORDs
1429 Padding
= 4 - (PLength
% 4);
1430 ReadLimit
= (PLength
+ Padding
)/4;
1432 ReadLimit
= PLength
/4;
1436 // Check buffer size
1437 if (*BuffSize
< (PLength
+ Padding
)) {
1438 *BuffSize
= PLength
+ Padding
;
1439 return EFI_BUFFER_TOO_SMALL
;
1442 // Set the amount of data to be transfered out of FIFO for THIS packet
1443 // This can be used to trigger an interrupt, and status can be checked
1444 RxCfgValue
= Lan9118MmioRead32 (LAN9118_RX_CFG
);
1445 RxCfgValue
&= ~(RXCFG_RX_DMA_CNT_MASK
);
1446 RxCfgValue
|= RXCFG_RX_DMA_CNT (ReadLimit
);
1448 // Set end alignment to 4-bytes
1449 RxCfgValue
&= ~(RXCFG_RX_END_ALIGN_MASK
);
1450 Lan9118MmioWrite32 (LAN9118_RX_CFG
, RxCfgValue
);
1452 // Update buffer size
1453 *BuffSize
= PLength
; // -4 bytes may be needed: Received in buffer as
1454 // 4 bytes longer than packet actually is, unless
1455 // packet is < 64 bytes
1457 if (HdrSize
!= NULL
)
1458 *HdrSize
= Snp
->Mode
->MediaHeaderSize
;
1460 // Format the pointer
1461 RawData
= (UINT32
*)Data
;
1464 for (Count
= 0; Count
< ReadLimit
; Count
++) {
1465 RawData
[Count
] = Lan9118MmioRead32 (LAN9118_RX_DATA
);
1468 // Get the destination address
1469 if (DstAddr
!= NULL
) {
1470 Dst
.Addr
[0] = (RawData
[0] & 0xFF);
1471 Dst
.Addr
[1] = (RawData
[0] & 0xFF00) >> 8;
1472 Dst
.Addr
[2] = (RawData
[0] & 0xFF0000) >> 16;
1473 Dst
.Addr
[3] = (RawData
[0] & 0xFF000000) >> 24;
1474 Dst
.Addr
[4] = (RawData
[1] & 0xFF);
1475 Dst
.Addr
[5] = (RawData
[1] & 0xFF00) >> 8;
1476 CopyMem (DstAddr
, &Dst
, NET_ETHER_ADDR_LEN
);
1479 // Get the source address
1480 if (SrcAddr
!= NULL
) {
1481 Src
.Addr
[0] = (RawData
[1] & 0xFF0000) >> 16;
1482 Src
.Addr
[1] = (RawData
[1] & 0xFF000000) >> 24;
1483 Src
.Addr
[2] = (RawData
[2] & 0xFF);
1484 Src
.Addr
[3] = (RawData
[2] & 0xFF00) >> 8;
1485 Src
.Addr
[4] = (RawData
[2] & 0xFF0000) >> 16;
1486 Src
.Addr
[5] = (RawData
[2] & 0xFF000000) >> 24;
1487 CopyMem (SrcAddr
, &Src
, NET_ETHER_ADDR_LEN
);
1491 if (Protocol
!= NULL
) {
1492 *Protocol
= NTOHS (RawData
[3] & 0xFFFF);
1495 // Check for Rx errors (worst possible error)
1496 if (Lan9118MmioRead32 (LAN9118_INT_STS
) & INSTS_RXE
) {
1497 DEBUG ((EFI_D_WARN
, "Warning: Receiver Error. Restarting...\n"));
1499 // Software reset, the RXE interrupt is cleared by the reset.
1500 Status
= SoftReset (0, Snp
);
1501 if (EFI_ERROR (Status
)) {
1502 DEBUG ((EFI_D_ERROR
, "Error: Soft Reset Failed: Hardware Error.\n"));
1503 return EFI_DEVICE_ERROR
;
1506 // Reactivate the LEDs
1507 Status
= ConfigureHardware (HW_CONF_USE_LEDS
, Snp
);
1508 if (EFI_ERROR (Status
)) {
1513 // Restart the receiver and the transmitter without resetting the FIFOs
1514 // as it has been done by SoftReset().
1517 StartTx (START_TX_MAC
| START_TX_CFG
, Snp
);
1519 // Say that command could not be sent
1520 return EFI_DEVICE_ERROR
;
1523 #if defined(EVAL_PERFORMANCE)
1524 UINT64 EndClock
= GetPerformanceCounter ();
1525 DEBUG ((EFI_D_ERROR
, "Receive Time processing: %d counts @ %d Hz\n", StartClock
- EndClock
,Perf
));
1528 LanDriver
->Stats
.RxGoodFrames
+= 1;