]> git.proxmox.com Git - mirror_edk2.git/blob - EmbeddedPkg/Drivers/Lan9118Dxe/Lan9118Dxe.c
EmbeddedPkg: Fix various typos
[mirror_edk2.git] / EmbeddedPkg / Drivers / Lan9118Dxe / Lan9118Dxe.c
1 /** @file
2 *
3 * Copyright (c) 2012-2014, ARM Limited. All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-2-Clause-Patent
6 *
7 **/
8
9 #include "Lan9118Dxe.h"
10
11 typedef struct {
12 MAC_ADDR_DEVICE_PATH Lan9118;
13 EFI_DEVICE_PATH_PROTOCOL End;
14 } LAN9118_DEVICE_PATH;
15
16 LAN9118_DEVICE_PATH Lan9118PathTemplate = {
17 {
18 {
19 MESSAGING_DEVICE_PATH, MSG_MAC_ADDR_DP,
20 { (UINT8) (sizeof(MAC_ADDR_DEVICE_PATH)), (UINT8) ((sizeof(MAC_ADDR_DEVICE_PATH)) >> 8) }
21 },
22 { { 0 } },
23 0
24 },
25 {
26 END_DEVICE_PATH_TYPE,
27 END_ENTIRE_DEVICE_PATH_SUBTYPE,
28 { sizeof(EFI_DEVICE_PATH_PROTOCOL), 0 }
29 }
30 };
31
32 /*
33 ** Entry point for the LAN9118 driver
34 **
35 */
36 EFI_STATUS
37 Lan9118DxeEntry (
38 IN EFI_HANDLE Handle,
39 IN EFI_SYSTEM_TABLE *SystemTable
40 )
41 {
42 EFI_STATUS Status;
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;
48
49 // The PcdLan9118DxeBaseAddress PCD must be defined
50 ASSERT (PcdGet32 (PcdLan9118DxeBaseAddress) != 0);
51
52 // Allocate Resources
53 LanDriver = AllocateZeroPool (sizeof (LAN9118_DRIVER));
54 if (LanDriver == NULL) {
55 return EFI_OUT_OF_RESOURCES;
56 }
57 Lan9118Path = (LAN9118_DEVICE_PATH*)AllocateCopyPool (sizeof (LAN9118_DEVICE_PATH), &Lan9118PathTemplate);
58 if (Lan9118Path == NULL) {
59 return EFI_OUT_OF_RESOURCES;
60 }
61
62 // Initialize pointers
63 Snp = &(LanDriver->Snp);
64 SnpMode = &(LanDriver->SnpMode);
65 Snp->Mode = SnpMode;
66
67 // Set the signature of the LAN Driver structure
68 LanDriver->Signature = LAN9118_SIGNATURE;
69
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;
75 Snp->Stop = SnpStop;
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;
86
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
94
95 //
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.
105 //
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;
111
112 // We do not intend to receive anything for the time being.
113 SnpMode->ReceiveFilterSetting = 0;
114
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));
119
120 // Set the interface type (1: Ethernet or 6: IEEE 802 Networks)
121 SnpMode->IfType = NET_IFTYPE_ETHERNET;
122
123 // Mac address is changeable as it is loaded from erasable memory
124 SnpMode->MacAddressChangeable = TRUE;
125
126 // Can only transmit one packet at a time
127 SnpMode->MultipleTxSupported = FALSE;
128
129 // MediaPresent checks for cable connection and partner link
130 SnpMode->MediaPresentSupported = TRUE;
131 SnpMode->MediaPresent = FALSE;
132
133 // Set broadcast address
134 SetMem (&SnpMode->BroadcastAddress, sizeof (EFI_MAC_ADDRESS), 0xFF);
135
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;
141 }
142
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;
146
147 // Initialise the protocol
148 ControllerHandle = NULL;
149 Status = gBS->InstallMultipleProtocolInterfaces (
150 &ControllerHandle,
151 &gEfiSimpleNetworkProtocolGuid, Snp,
152 &gEfiDevicePathProtocolGuid, Lan9118Path,
153 NULL
154 );
155 // Say what the status of loading the protocol structure is
156 if (EFI_ERROR(Status)) {
157 FreePool (LanDriver);
158 } else {
159 LanDriver->ControllerHandle = ControllerHandle;
160 }
161
162 return Status;
163 }
164
165 /*
166 * UEFI Start() function
167 *
168 * Parameters:
169 *
170 * @param Snp: A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance.
171 *
172 * Description:
173 *
174 * This function starts a network interface. If the network interface successfully starts, then
175 * EFI_SUCCESS will be returned.
176 */
177 EFI_STATUS
178 EFIAPI
179 SnpStart (
180 IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp
181 )
182 {
183 // Check Snp instance
184 if (Snp == NULL) {
185 return EFI_INVALID_PARAMETER;
186 }
187
188 // Check state
189 if ((Snp->Mode->State == EfiSimpleNetworkStarted) ||
190 (Snp->Mode->State == EfiSimpleNetworkInitialized) ) {
191 return EFI_ALREADY_STARTED;
192 }
193
194 // Change state
195 Snp->Mode->State = EfiSimpleNetworkStarted;
196 return EFI_SUCCESS;
197 }
198
199 /*
200 * UEFI Stop() function
201 *
202 */
203 EFI_STATUS
204 EFIAPI
205 SnpStop (
206 IN EFI_SIMPLE_NETWORK_PROTOCOL* Snp
207 )
208 {
209 // Check Snp Instance
210 if (Snp == NULL) {
211 return EFI_INVALID_PARAMETER;
212 }
213
214 // Check state of the driver
215 if (Snp->Mode->State == EfiSimpleNetworkStopped) {
216 return EFI_NOT_STARTED;
217 }
218
219 // Stop the Tx and Rx
220 StopTx (STOP_TX_CFG | STOP_TX_MAC, Snp);
221 StopRx (0, Snp);
222
223 // Change the state
224 switch (Snp->Mode->State) {
225 case EfiSimpleNetworkStarted:
226 case EfiSimpleNetworkInitialized:
227 Snp->Mode->State = EfiSimpleNetworkStopped;
228 break;
229 default:
230 return EFI_DEVICE_ERROR;
231 }
232
233 // Put the device into a power saving mode ?
234 return EFI_SUCCESS;
235 }
236
237
238 // Allocated receive and transmit buffers
239 STATIC UINT32 gTxBuffer = 0;
240
241 /*
242 * UEFI Initialize() function
243 *
244 */
245 EFI_STATUS
246 EFIAPI
247 SnpInitialize (
248 IN EFI_SIMPLE_NETWORK_PROTOCOL* Snp,
249 IN UINTN RxBufferSize OPTIONAL,
250 IN UINTN TxBufferSize OPTIONAL
251 )
252 {
253 EFI_STATUS Status;
254 UINT32 PmConf;
255 INT32 AllocResult;
256 UINT32 RxStatusSize;
257 UINT32 TxStatusSize;
258
259 // Initialize variables
260 // Global variables to hold tx and rx FIFO allocation
261 gTxBuffer = 0;
262
263 // Check Snp Instance
264 if (Snp == NULL) {
265 return EFI_INVALID_PARAMETER;
266 }
267
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"));
271 return EFI_SUCCESS;
272 } else
273 if (Snp->Mode->State == EfiSimpleNetworkStopped) {
274 DEBUG ((EFI_D_WARN, "LAN9118 Driver not started\n"));
275 return EFI_NOT_STARTED;
276 }
277
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;
284 }
285
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;
291 }
292
293 // Read the PM register
294 PmConf = Lan9118MmioRead32 (LAN9118_PMT_CTRL);
295
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
299 PmConf = 0;
300 PmConf |= (MPTCTRL_WOL_EN | MPTCTRL_ED_EN | MPTCTRL_PME_EN);
301
302 // Write the current configuration to the register
303 Lan9118MmioWrite32 (LAN9118_PMT_CTRL, PmConf);
304
305 // Configure GPIO and HW
306 Status = ConfigureHardware (HW_CONF_USE_LEDS, Snp);
307 if (EFI_ERROR(Status)) {
308 return Status;
309 }
310
311 // Assign the transmitter buffer size (default values)
312 TxStatusSize = LAN9118_TX_STATUS_SIZE;
313 RxStatusSize = LAN9118_RX_STATUS_SIZE;
314
315 // Check that a buff size was specified
316 if (TxBufferSize > 0) {
317 if (RxBufferSize == 0) {
318 RxBufferSize = LAN9118_RX_DATA_SIZE;
319 }
320
321 AllocResult = ChangeFifoAllocation (
322 ALLOC_USE_FIFOS,
323 &TxBufferSize,
324 &RxBufferSize,
325 &TxStatusSize,
326 &RxStatusSize,
327 Snp
328 );
329
330 if (AllocResult < 0) {
331 return EFI_OUT_OF_RESOURCES;
332 }
333 }
334
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"));
339 }
340
341 // Configure flow control depending on speed capabilities
342 Status = ConfigureFlow (0, 0, 0, 0, Snp);
343 if (EFI_ERROR(Status)) {
344 return Status;
345 }
346
347 // Enable the transmitter
348 Status = StartTx (START_TX_MAC | START_TX_CFG, Snp);
349 if (EFI_ERROR(Status)) {
350 return Status;
351 }
352
353 // Now acknowledge all interrupts
354 Lan9118MmioWrite32 (LAN9118_INT_STS, ~0);
355
356 // Declare the driver as initialized
357 Snp->Mode->State = EfiSimpleNetworkInitialized;
358
359 return Status;
360 }
361
362 /*
363 * UEFI Reset () function
364 *
365 */
366 EFI_STATUS
367 EFIAPI
368 SnpReset (
369 IN EFI_SIMPLE_NETWORK_PROTOCOL* Snp,
370 IN BOOLEAN Verification
371 )
372 {
373 UINT32 PmConf;
374 UINT32 HwConf;
375 UINT32 ResetFlags;
376 EFI_STATUS Status;
377
378 PmConf = 0;
379 HwConf = 0;
380 ResetFlags = 0;
381
382 // Check Snp Instance
383 if (Snp == NULL) {
384 return EFI_INVALID_PARAMETER;
385 }
386
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;
394 }
395
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;
401 }
402
403 // Initiate a software reset
404 ResetFlags |= SOFT_RESET_CHECK_MAC_ADDR_LOAD | SOFT_RESET_CLEAR_INT;
405
406 if (Verification) {
407 ResetFlags |= SOFT_RESET_SELF_TEST;
408 }
409
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;
414 }
415
416 // Read the PM register
417 PmConf = Lan9118MmioRead32 (LAN9118_PMT_CTRL);
418
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);
423
424 // Write the current configuration to the register
425 Lan9118MmioWrite32 (LAN9118_PMT_CTRL, PmConf);
426
427 // Reactivate the LEDs
428 Status = ConfigureHardware (HW_CONF_USE_LEDS, Snp);
429 if (EFI_ERROR (Status)) {
430 return Status;
431 }
432
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
438
439 Lan9118MmioWrite32 (LAN9118_HW_CFG, HwConf); // Write the conf
440 }
441
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);
445
446 // Now acknowledge all interrupts
447 Lan9118MmioWrite32 (LAN9118_INT_STS, ~0);
448
449 return EFI_SUCCESS;
450 }
451
452 /*
453 * UEFI Shutdown () function
454 *
455 */
456 EFI_STATUS
457 EFIAPI
458 SnpShutdown (
459 IN EFI_SIMPLE_NETWORK_PROTOCOL* Snp
460 )
461 {
462 EFI_STATUS Status;
463
464 // Check Snp Instance
465 if (Snp == NULL) {
466 return EFI_INVALID_PARAMETER;
467 }
468
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;
476 }
477
478 // Initiate a PHY reset
479 Status = PhySoftReset (PHY_RESET_PMT, Snp);
480 if (EFI_ERROR (Status)) {
481 return Status;
482 }
483
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"));
488 return Status;
489 }
490
491 // Back to the started and thus not initialized state
492 Snp->Mode->State = EfiSimpleNetworkStarted;
493
494 return EFI_SUCCESS;
495 }
496
497 /**
498 Enable and/or disable the receive filters of the LAN9118
499
500 Please refer to the UEFI specification for the precedence rules among the
501 Enable, Disable and ResetMCastFilter parameters.
502
503 @param[in] Snp A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL
504 instance.
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.
519
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 :
523 . This is NULL
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.
535
536 **/
537 EFI_STATUS
538 EFIAPI
539 SnpReceiveFilters (
540 IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp,
541 IN UINT32 Enable,
542 IN UINT32 Disable,
543 IN BOOLEAN ResetMCastFilter,
544 IN UINTN MCastFilterCnt OPTIONAL,
545 IN EFI_MAC_ADDRESS *MCastFilter OPTIONAL
546 )
547 {
548 EFI_SIMPLE_NETWORK_MODE *Mode;
549 UINT32 MultHashTableHigh;
550 UINT32 MultHashTableLow;
551 UINT32 Count;
552 UINT32 Crc;
553 UINT8 HashValue;
554 UINT32 MacCSRValue;
555 UINT32 ReceiveFilterSetting;
556 EFI_MAC_ADDRESS *Mac;
557 EFI_MAC_ADDRESS ZeroMac;
558
559 // Check Snp Instance
560 if (Snp == NULL) {
561 return EFI_INVALID_PARAMETER;
562 }
563 Mode = Snp->Mode;
564
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;
572 }
573
574 if ((Enable & (~Mode->ReceiveFilterMask)) ||
575 (Disable & (~Mode->ReceiveFilterMask)) ) {
576 return EFI_INVALID_PARAMETER;
577 }
578
579 //
580 // Check the validity of the multicast setting and compute the
581 // hash values of the multicast mac addresses to listen to.
582 //
583
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;
593 }
594 //
595 // Check the validity of all multicast addresses before to change
596 // anything.
597 //
598 for (Count = 0; Count < MCastFilterCnt; Count++) {
599 if ((MCastFilter[Count].Addr[0] & 1) == 0) {
600 return EFI_INVALID_PARAMETER;
601 }
602 }
603
604 //
605 // Go through each filter address and set appropriate bits on hash table
606 //
607 for (Count = 0; Count < MCastFilterCnt; Count++) {
608 Mac = &(MCastFilter[Count]);
609 CopyMem (&Mode->MCastFilter[Count], Mac, sizeof(EFI_MAC_ADDRESS));
610
611 Crc = GenEtherCrc32 (Mac, NET_ETHER_ADDR_LEN);
612 //gBS->CalculateCrc32 ((VOID*)&Mfilter[Count],6,&Crc); <-- doesn't work as desired
613
614 //
615 // The most significant 6 bits of the MAC address CRC constitute the hash
616 // value of the MAC address.
617 //
618 HashValue = (Crc >> 26) & 0x3F;
619
620 // Select hashlow register if MSB is not set
621 if ((HashValue & 0x20) == 0) {
622 MultHashTableLow |= (1 << HashValue);
623 } else {
624 MultHashTableHigh |= (1 << (HashValue & 0x1F));
625 }
626 }
627 Mode->MCastFilterCount = MCastFilterCnt;
628 } else if (ResetMCastFilter) {
629 Mode->MCastFilterCount = 0;
630 } else {
631 MultHashTableLow = IndirectMACRead32 (INDIRECT_MAC_INDEX_HASHL);
632 MultHashTableHigh = IndirectMACRead32 (INDIRECT_MAC_INDEX_HASHH);
633 }
634
635 //
636 // Before to change anything, stop and reset the reception of
637 // packets.
638 //
639 StopRx (STOP_RX_CLEAR, Snp);
640
641 //
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.
644 //
645 IndirectMACWrite32 (INDIRECT_MAC_INDEX_HASHL, MultHashTableLow);
646 IndirectMACWrite32 (INDIRECT_MAC_INDEX_HASHH, MultHashTableHigh);
647
648 ReceiveFilterSetting = (Mode->ReceiveFilterSetting | Enable) & (~Disable);
649
650 //
651 // Read MAC controller
652 //
653 MacCSRValue = IndirectMACRead32 (INDIRECT_MAC_INDEX_CR);
654 MacCSRValue &= ~(MACCR_HPFILT | MACCR_BCAST | MACCR_PRMS | MACCR_MCPAS);
655
656 if (ReceiveFilterSetting & EFI_SIMPLE_NETWORK_RECEIVE_UNICAST) {
657 Lan9118SetMacAddress (&Mode->CurrentAddress, Snp);
658 DEBUG ((DEBUG_NET, "Allowing Unicast Frame Reception\n"));
659 } else {
660 //
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.
664 //
665 ZeroMem (&ZeroMac, NET_ETHER_ADDR_LEN);
666 Lan9118SetMacAddress (&ZeroMac, Snp);
667 }
668
669 if (ReceiveFilterSetting & EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST) {
670 MacCSRValue |= MACCR_HPFILT;
671 DEBUG ((DEBUG_NET, "Allowing Multicast Frame Reception\n"));
672 }
673
674 if (ReceiveFilterSetting & EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST) {
675 MacCSRValue |= MACCR_MCPAS;
676 DEBUG ((DEBUG_NET, "Enabling Promiscuous Multicast Mode\n"));
677 }
678
679 if ((ReceiveFilterSetting & EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST) == 0) {
680 MacCSRValue |= MACCR_BCAST;
681 } else {
682 DEBUG ((DEBUG_NET, "Allowing Broadcast Frame Reception\n"));
683 }
684
685 if (ReceiveFilterSetting & EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS) {
686 MacCSRValue |= MACCR_PRMS;
687 DEBUG ((DEBUG_NET, "Enabling Promiscuous Mode\n"));
688 }
689
690 //
691 // Write the options to the MAC_CSR
692 //
693 IndirectMACWrite32 (INDIRECT_MAC_INDEX_CR, MacCSRValue);
694
695 //
696 // If we have to retrieve something, start packet reception.
697 //
698 Mode->ReceiveFilterSetting = ReceiveFilterSetting;
699 if (ReceiveFilterSetting != 0) {
700 StartRx (0, Snp);
701 }
702
703 return EFI_SUCCESS;
704 }
705
706 /**
707 Modify of reset the current station address
708
709 @param[in] Snp A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL
710 instance.
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.
714
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.
721
722 **/
723 EFI_STATUS
724 EFIAPI
725 SnpStationAddress (
726 IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp,
727 IN BOOLEAN Reset,
728 IN EFI_MAC_ADDRESS *New
729 )
730 {
731 UINT32 Count;
732 UINT8 PermAddr[NET_ETHER_ADDR_LEN];
733
734 DEBUG ((DEBUG_NET, "SnpStationAddress()\n"));
735
736 // Check Snp instance
737 if (Snp == NULL) {
738 return EFI_INVALID_PARAMETER;
739 }
740
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;
748 }
749
750 // Get the Permanent MAC address if need reset
751 if (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);
756 }
757 New = (EFI_MAC_ADDRESS *) PermAddr;
758 Lan9118SetMacAddress ((EFI_MAC_ADDRESS *) PermAddr, Snp);
759 } else {
760 DEBUG ((EFI_D_ERROR, "LAN9118: Warning: No valid MAC address in EEPROM, using fallback\n"));
761 New = (EFI_MAC_ADDRESS*) (FixedPcdGet64 (PcdLan9118DefaultMacAddress));
762 }
763 } else {
764 // Otherwise use the specified new MAC address
765 if (New == NULL) {
766 return EFI_INVALID_PARAMETER;
767 }
768 //
769 // If it is a multicast address, it is not valid.
770 //
771 if (New->Addr[0] & 0x01) {
772 return EFI_INVALID_PARAMETER;
773 }
774 }
775
776 CopyMem (&Snp->Mode->CurrentAddress, New, NET_ETHER_ADDR_LEN);
777
778 //
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
783 // activated.
784 //
785 if (Snp->Mode->ReceiveFilterSetting != 0) {
786 StopRx (STOP_RX_CLEAR, Snp);
787 Lan9118SetMacAddress (New, Snp);
788 StartRx (0, Snp);
789 }
790
791 return EFI_SUCCESS;
792 }
793
794 /*
795 * UEFI Statistics() function
796 *
797 */
798 EFI_STATUS
799 EFIAPI
800 SnpStatistics (
801 IN EFI_SIMPLE_NETWORK_PROTOCOL* Snp,
802 IN BOOLEAN Reset,
803 IN OUT UINTN *StatSize,
804 OUT EFI_NETWORK_STATISTICS *Statistics
805 )
806 {
807 LAN9118_DRIVER *LanDriver;
808 EFI_STATUS Status;
809
810 LanDriver = INSTANCE_FROM_SNP_THIS (Snp);
811
812 DEBUG ((DEBUG_NET, "SnpStatistics()\n"));
813
814 // Check Snp instance
815 if (Snp == NULL) {
816 return EFI_INVALID_PARAMETER;
817 }
818
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;
826 }
827
828 //
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".
834 //
835 if (Reset) {
836 ZeroMem (&LanDriver->Stats, sizeof(EFI_NETWORK_STATISTICS));
837 }
838
839 Status = EFI_SUCCESS;
840 if (StatSize == NULL) {
841 if (Statistics != NULL) {
842 return EFI_INVALID_PARAMETER;
843 }
844 } else {
845 if (Statistics == NULL) {
846 Status = EFI_BUFFER_TOO_SMALL;
847 } else {
848 // Fill in the statistics
849 CopyMem (
850 Statistics, &LanDriver->Stats,
851 MIN (*StatSize, sizeof (EFI_NETWORK_STATISTICS))
852 );
853 if (*StatSize < sizeof (EFI_NETWORK_STATISTICS)) {
854 Status = EFI_BUFFER_TOO_SMALL;
855 }
856 }
857 *StatSize = sizeof (EFI_NETWORK_STATISTICS);
858 }
859
860 return Status;
861 }
862
863 /*
864 * UEFI MCastIPtoMAC() function
865 *
866 */
867 EFI_STATUS
868 EFIAPI
869 SnpMcastIptoMac (
870 IN EFI_SIMPLE_NETWORK_PROTOCOL* Snp,
871 IN BOOLEAN IsIpv6,
872 IN EFI_IP_ADDRESS *Ip,
873 OUT EFI_MAC_ADDRESS *McastMac
874 )
875 {
876 DEBUG ((DEBUG_NET, "SnpMcastIptoMac()\n"));
877
878 // Check Snp instance
879 if (Snp == NULL) {
880 return EFI_INVALID_PARAMETER;
881 }
882
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;
890 }
891
892 // Check parameters
893 if ((McastMac == NULL) || (Ip == NULL)) {
894 return EFI_INVALID_PARAMETER;
895 }
896
897 // Make sure MAC address is empty
898 ZeroMem (McastMac, sizeof(EFI_MAC_ADDRESS));
899
900 // If we need ipv4 address
901 if (!IsIpv6) {
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;
907
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];
912 } else {
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;
917
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];
923 }
924
925 return EFI_SUCCESS;
926 }
927
928 /*
929 * UEFI NvData() function
930 *
931 */
932 EFI_STATUS
933 EFIAPI
934 SnpNvData (
935 IN EFI_SIMPLE_NETWORK_PROTOCOL* pobj,
936 IN BOOLEAN read_write,
937 IN UINTN offset,
938 IN UINTN buff_size,
939 IN OUT VOID *data
940 )
941 {
942 DEBUG ((DEBUG_NET, "SnpNvData()\n"));
943
944 return EFI_UNSUPPORTED;
945 }
946
947
948 /*
949 * UEFI GetStatus () function
950 *
951 */
952 EFI_STATUS
953 EFIAPI
954 SnpGetStatus (
955 IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp,
956 OUT UINT32 *IrqStat OPTIONAL,
957 OUT VOID **TxBuff OPTIONAL
958 )
959 {
960 UINT32 FifoInt;
961 EFI_STATUS Status;
962 UINTN NumTxStatusEntries;
963 UINT32 TxStatus;
964 UINT16 PacketTag;
965 UINT32 Interrupts;
966 LAN9118_DRIVER *LanDriver;
967
968 LanDriver = INSTANCE_FROM_SNP_THIS (Snp);
969
970 // Check preliminaries
971 if (Snp == NULL) {
972 return EFI_INVALID_PARAMETER;
973 }
974
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;
982 }
983
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);
989
990 // Clear the TX Status FIFO Overflow
991 if ((FifoInt & INSTS_TXSO) == 0) {
992 FifoInt |= INSTS_TXSO;
993 Lan9118MmioWrite32 (LAN9118_FIFO_INT, FifoInt);
994 }
995
996 // Read interrupt status if IrqStat is not NULL
997 if (IrqStat != NULL) {
998 *IrqStat = 0;
999
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);
1004 }
1005
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);
1010 }
1011
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);
1016 }
1017 }
1018
1019 // Check Status of transmitted packets
1020 // (We ignore TXSTATUS_NO_CA has it might happen in Full Duplex)
1021
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"));
1031 }
1032 if (TxStatus & TXSTATUS_DEF) {
1033 DEBUG ((EFI_D_ERROR, "- Packet tx was deferred\n"));
1034 }
1035 if (TxStatus & TXSTATUS_EDEF) {
1036 DEBUG ((EFI_D_ERROR, "- Tx ended because of excessive deferral\n"));
1037 }
1038 if (TxStatus & TXSTATUS_ECOLL) {
1039 DEBUG ((EFI_D_ERROR, "- Tx ended because of Excessive Collisions\n"));
1040 }
1041 if (TxStatus & TXSTATUS_LCOLL) {
1042 DEBUG ((EFI_D_ERROR, "- Packet Tx aborted after coll window of 64 bytes\n"));
1043 }
1044 if (TxStatus & TXSTATUS_LOST_CA) {
1045 DEBUG ((EFI_D_ERROR, "- Lost carrier during Tx\n"));
1046 }
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];
1051 }
1052 } else if (TxBuff != NULL) {
1053 *TxBuff = NULL;
1054 }
1055
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..."));
1060
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;
1066 }
1067
1068 // Reactivate the LEDs
1069 Status = ConfigureHardware (HW_CONF_USE_LEDS, Snp);
1070 if (EFI_ERROR (Status)) {
1071 return Status;
1072 }
1073
1074 //
1075 // Restart the transmitter and if necessary the receiver.
1076 // Do not ask for FIFO reset as it has already been done
1077 // by SoftReset().
1078 //
1079 StartTx (START_TX_MAC | START_TX_CFG, Snp);
1080 if (Snp->Mode->ReceiveFilterSetting != 0) {
1081 StartRx (0, Snp);
1082 }
1083 }
1084
1085 // Update the media status
1086 Status = CheckLinkStatus (0, Snp);
1087 if (EFI_ERROR(Status)) {
1088 Snp->Mode->MediaPresent = FALSE;
1089 } else {
1090 Snp->Mode->MediaPresent = TRUE;
1091 }
1092
1093 return EFI_SUCCESS;
1094 }
1095
1096
1097 /*
1098 * UEFI Transmit() function
1099 *
1100 */
1101 EFI_STATUS
1102 EFIAPI
1103 SnpTransmit (
1104 IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp,
1105 IN UINTN HdrSize,
1106 IN UINTN BuffSize,
1107 IN VOID* Data,
1108 IN EFI_MAC_ADDRESS *SrcAddr OPTIONAL,
1109 IN EFI_MAC_ADDRESS *DstAddr OPTIONAL,
1110 IN UINT16 *Protocol OPTIONAL
1111 )
1112 {
1113 LAN9118_DRIVER *LanDriver;
1114 UINT32 TxFreeSpace;
1115 UINT32 TxStatusSpace;
1116 INT32 Count;
1117 UINT32 CommandA;
1118 UINT32 CommandB;
1119 UINT16 LocalProtocol;
1120 UINT32 *LocalData;
1121 UINT16 PacketTag;
1122
1123 #if defined(EVAL_PERFORMANCE)
1124 UINT64 Perf;
1125 UINT64 StartClock;
1126 UINT64 EndClock;
1127
1128 Perf = GetPerformanceCounterProperties (NULL, NULL);
1129 StartClock = GetPerformanceCounter ();
1130 #endif
1131
1132 LanDriver = INSTANCE_FROM_SNP_THIS (Snp);
1133
1134 // Check preliminaries
1135 if ((Snp == NULL) || (Data == NULL)) {
1136 return EFI_INVALID_PARAMETER;
1137 }
1138
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;
1146 }
1147
1148 // Ensure header is correct size if non-zero
1149 if (HdrSize) {
1150 if (HdrSize != Snp->Mode->MediaHeaderSize) {
1151 return EFI_INVALID_PARAMETER;
1152 }
1153
1154 if ((DstAddr == NULL) || (Protocol == NULL)) {
1155 return EFI_INVALID_PARAMETER;
1156 }
1157 }
1158
1159 //
1160 // Check validity of BufferSize
1161 //
1162 if (BuffSize < Snp->Mode->MediaHeaderSize) {
1163 return EFI_BUFFER_TOO_SMALL;
1164 }
1165
1166 // Before transmitting check the link status
1167 /*if (CheckLinkStatus (0, Snp) < 0) {
1168 return EFI_NOT_READY;
1169 }*/
1170
1171 // Get DATA FIFO free space in bytes
1172 TxFreeSpace = TxDataFreeSpace (0, Snp);
1173 if (TxFreeSpace < BuffSize) {
1174 return EFI_NOT_READY;
1175 }
1176
1177 // Get STATUS FIFO used space in bytes
1178 TxStatusSpace = TxStatusUsedSpace (0, Snp);
1179 if (TxStatusSpace > 500) {
1180 return EFI_NOT_READY;
1181 }
1182
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;
1187 }
1188
1189 // Check for the nature of the frame
1190 if ((DstAddr->Addr[0] & 0x1) == 1) {
1191 LanDriver->Stats.TxMulticastFrames += 1;
1192 } else {
1193 LanDriver->Stats.TxUnicastFrames += 1;
1194 }
1195
1196 // Check if broadcast
1197 if (DstAddr->Addr[0] == 0xFF) {
1198 LanDriver->Stats.TxBroadcastFrames += 1;
1199 }
1200
1201 PacketTag = LanDriver->NextPacketTag;
1202 LanDriver->NextPacketTag++;
1203
1204 if (HdrSize) {
1205
1206 // Format pointer
1207 LocalData = (UINT32*) Data;
1208 LocalProtocol = *Protocol;
1209
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);
1213
1214 // Write the commands first
1215 Lan9118MmioWrite32 (LAN9118_TX_DATA, CommandA);
1216 Lan9118MmioWrite32 (LAN9118_TX_DATA, CommandB);
1217
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)
1224 );
1225
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)
1231 );
1232
1233 Lan9118MmioWrite32 (LAN9118_TX_DATA,
1234 (SrcAddr->Addr[2]) |
1235 (SrcAddr->Addr[3] << 8) |
1236 (SrcAddr->Addr[4] << 16) |
1237 (SrcAddr->Addr[5] << 24)
1238 );
1239
1240 // Write the Protocol
1241 Lan9118MmioWrite32 (LAN9118_TX_DATA, (UINT32)(HTONS (LocalProtocol)));
1242
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
1245
1246 // Write the commands
1247 Lan9118MmioWrite32 (LAN9118_TX_DATA, CommandA);
1248 Lan9118MmioWrite32 (LAN9118_TX_DATA, CommandB);
1249
1250 // Write the payload
1251 for (Count = 0; Count < ((BuffSize + 3) >> 2) - 3; Count++) {
1252 Lan9118MmioWrite32 (LAN9118_TX_DATA, LocalData[Count + 3]);
1253 }
1254 } else {
1255 // Format pointer
1256 LocalData = (UINT32*) Data;
1257
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);
1261
1262 // Write the commands first
1263 Lan9118MmioWrite32 (LAN9118_TX_DATA, CommandA);
1264 Lan9118MmioWrite32 (LAN9118_TX_DATA, CommandB);
1265
1266 // Write all the data
1267 for (Count = 0; Count < ((BuffSize + 3) >> 2); Count++) {
1268 Lan9118MmioWrite32 (LAN9118_TX_DATA, LocalData[Count]);
1269 }
1270 }
1271
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;
1276
1277 #if defined(EVAL_PERFORMANCE)
1278 EndClock = GetPerformanceCounter ();
1279 DEBUG ((EFI_D_ERROR, "Time processing: %d counts @ %d Hz\n", StartClock - EndClock,Perf));
1280 #endif
1281
1282 LanDriver->Stats.TxGoodFrames += 1;
1283
1284 return EFI_SUCCESS;
1285 }
1286
1287
1288 /*
1289 * UEFI Receive() function
1290 *
1291 */
1292 EFI_STATUS
1293 EFIAPI
1294 SnpReceive (
1295 IN EFI_SIMPLE_NETWORK_PROTOCOL* Snp,
1296 OUT UINTN *HdrSize OPTIONAL,
1297 IN OUT UINTN *BuffSize,
1298 OUT VOID *Data,
1299 OUT EFI_MAC_ADDRESS *SrcAddr OPTIONAL,
1300 OUT EFI_MAC_ADDRESS *DstAddr OPTIONAL,
1301 OUT UINT16 *Protocol OPTIONAL
1302 )
1303 {
1304 LAN9118_DRIVER *LanDriver;
1305 UINT32 IntSts;
1306 UINT32 RxFifoStatus;
1307 UINT32 NumPackets;
1308 UINT32 RxCfgValue;
1309 UINT32 PLength; // Packet length
1310 UINT32 ReadLimit;
1311 UINT32 Count;
1312 UINT32 Padding;
1313 UINT32 *RawData;
1314 EFI_MAC_ADDRESS Dst;
1315 EFI_MAC_ADDRESS Src;
1316 UINTN DroppedFrames;
1317 EFI_STATUS Status;
1318
1319 LanDriver = INSTANCE_FROM_SNP_THIS (Snp);
1320
1321 #if defined(EVAL_PERFORMANCE)
1322 UINT64 Perf = GetPerformanceCounterProperties (NULL, NULL);
1323 UINT64 StartClock = GetPerformanceCounter ();
1324 #endif
1325
1326 // Check preliminaries
1327 if ((Snp == NULL) || (Data == NULL) || (BuffSize == NULL)) {
1328 return EFI_INVALID_PARAMETER;
1329 }
1330
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;
1338 }
1339
1340 //
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.
1355 //
1356 IntSts = Lan9118MmioRead32 (LAN9118_INT_STS);
1357 if ((IntSts & INSTS_RXE) && (!(IntSts & INSTS_RSFF))) {
1358 Lan9118MmioWrite32 (LAN9118_INT_STS, INSTS_RXE);
1359 }
1360
1361 // Count dropped frames
1362 DroppedFrames = Lan9118MmioRead32 (LAN9118_RX_DROP);
1363 LanDriver->Stats.RxDroppedFrames += DroppedFrames;
1364
1365 NumPackets = RxStatusUsedSpace (0, Snp) / 4;
1366 if (!NumPackets) {
1367 return EFI_NOT_READY;
1368 }
1369
1370 // Read Rx Status (only if not empty)
1371 RxFifoStatus = Lan9118MmioRead32 (LAN9118_RX_STATUS);
1372 LanDriver->Stats.RxTotalFrames += 1;
1373
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))
1381 {
1382 DEBUG ((EFI_D_WARN, "Warning: There was an error on frame reception.\n"));
1383 return EFI_DEVICE_ERROR;
1384 }
1385
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;
1392 }
1393
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;
1400 }
1401
1402 // Check filtering status for this packet
1403 if (RxFifoStatus & RXSTATUS_FILT_FAIL) {
1404 DEBUG ((EFI_D_WARN, "Warning: Frame Failed Filtering\n"));
1405 // fast forward?
1406 }
1407
1408 // Check if we got a broadcast frame
1409 if (RxFifoStatus & RXSTATUS_BCF) {
1410 LanDriver->Stats.RxBroadcastFrames += 1;
1411 }
1412
1413 // Check if we got a multicast frame
1414 if (RxFifoStatus & RXSTATUS_MCF) {
1415 LanDriver->Stats.RxMulticastFrames += 1;
1416 }
1417
1418 // Check if we got a unicast frame
1419 if ((RxFifoStatus & RXSTATUS_BCF) && ((RxFifoStatus & RXSTATUS_MCF) == 0)) {
1420 LanDriver->Stats.RxUnicastFrames += 1;
1421 }
1422
1423 // Get the received packet length
1424 PLength = GET_RXSTATUS_PACKET_LENGTH(RxFifoStatus);
1425 LanDriver->Stats.RxTotalBytes += (PLength - 4);
1426
1427 // If padding is applied, read more DWORDs
1428 if (PLength % 4) {
1429 Padding = 4 - (PLength % 4);
1430 ReadLimit = (PLength + Padding)/4;
1431 } else {
1432 ReadLimit = PLength/4;
1433 Padding = 0;
1434 }
1435
1436 // Check buffer size
1437 if (*BuffSize < (PLength + Padding)) {
1438 *BuffSize = PLength + Padding;
1439 return EFI_BUFFER_TOO_SMALL;
1440 }
1441
1442 // Set the amount of data to be transferred 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);
1447
1448 // Set end alignment to 4-bytes
1449 RxCfgValue &= ~(RXCFG_RX_END_ALIGN_MASK);
1450 Lan9118MmioWrite32 (LAN9118_RX_CFG, RxCfgValue);
1451
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
1456
1457 if (HdrSize != NULL)
1458 *HdrSize = Snp->Mode->MediaHeaderSize;
1459
1460 // Format the pointer
1461 RawData = (UINT32*)Data;
1462
1463 // Read Rx Packet
1464 for (Count = 0; Count < ReadLimit; Count++) {
1465 RawData[Count] = Lan9118MmioRead32 (LAN9118_RX_DATA);
1466 }
1467
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);
1477 }
1478
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);
1488 }
1489
1490 // Get the protocol
1491 if (Protocol != NULL) {
1492 *Protocol = NTOHS (RawData[3] & 0xFFFF);
1493 }
1494
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"));
1498
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;
1504 }
1505
1506 // Reactivate the LEDs
1507 Status = ConfigureHardware (HW_CONF_USE_LEDS, Snp);
1508 if (EFI_ERROR (Status)) {
1509 return Status;
1510 }
1511
1512 //
1513 // Restart the receiver and the transmitter without resetting the FIFOs
1514 // as it has been done by SoftReset().
1515 //
1516 StartRx (0, Snp);
1517 StartTx (START_TX_MAC | START_TX_CFG, Snp);
1518
1519 // Say that command could not be sent
1520 return EFI_DEVICE_ERROR;
1521 }
1522
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));
1526 #endif
1527
1528 LanDriver->Stats.RxGoodFrames += 1;
1529
1530 return EFI_SUCCESS;
1531 }