2 * Copyright 2008-2012 Freescale Semiconductor Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * * Neither the name of Freescale Semiconductor nor the
12 * names of its contributors may be used to endorse or promote products
13 * derived from this software without specific prior written permission.
16 * ALTERNATIVELY, this software may be distributed under the terms of the
17 * GNU General Public License ("GPL") as published by the Free Software
18 * Foundation, either version 2 of that License or (at your option) any
21 * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 /******************************************************************************
37 @Description FM mEMAC driver
38 *//***************************************************************************/
41 #include "string_ext.h"
42 #include "error_ext.h"
44 #include "endian_ext.h"
45 #include "debug_ext.h"
47 #include "fm_common.h"
51 /*****************************************************************************/
52 /* Internal routines */
53 /*****************************************************************************/
55 /* ......................................................................... */
57 static uint32_t GetMacAddrHashCode(uint64_t ethAddr)
59 uint64_t mask1, mask2;
65 mask1 = ethAddr & (uint64_t)0x01;
70 mask2 = ethAddr & (uint64_t)0x01;
75 xorVal |= (mask1 << (5-i));
81 /* ......................................................................... */
83 static void SetupSgmiiInternalPhy(t_Memac *p_Memac, uint8_t phyAddr)
88 /* In case the higher MACs are used (i.e. the MACs that should support 10G),
89 speed=10000 is provided for SGMII ports. Temporary modify enet mode
90 to 1G one, so MII functions can work correctly. */
91 enetMode = p_Memac->enetMode;
93 /* SGMII mode + AN enable */
94 tmpReg16 = PHY_SGMII_IF_MODE_AN | PHY_SGMII_IF_MODE_SGMII;
95 if ((p_Memac->enetMode) == e_ENET_MODE_SGMII_2500)
96 tmpReg16 = PHY_SGMII_CR_PHY_RESET | PHY_SGMII_IF_SPEED_GIGABIT | PHY_SGMII_IF_MODE_SGMII;
98 p_Memac->enetMode = MAKE_ENET_MODE(ENET_INTERFACE_FROM_MODE(p_Memac->enetMode), e_ENET_SPEED_1000);
99 MEMAC_MII_WritePhyReg(p_Memac, phyAddr, 0x14, tmpReg16);
101 /* Device ability according to SGMII specification */
102 tmpReg16 = PHY_SGMII_DEV_ABILITY_SGMII;
103 MEMAC_MII_WritePhyReg(p_Memac, phyAddr, 0x4, tmpReg16);
105 /* Adjust link timer for SGMII -
106 According to Cisco SGMII specification the timer should be 1.6 ms.
107 The link_timer register is configured in units of the clock.
108 - When running as 1G SGMII, Serdes clock is 125 MHz, so
109 unit = 1 / (125*10^6 Hz) = 8 ns.
110 1.6 ms in units of 8 ns = 1.6ms / 8ns = 2 * 10^5 = 0x30d40
111 - When running as 2.5G SGMII, Serdes clock is 312.5 MHz, so
112 unit = 1 / (312.5*10^6 Hz) = 3.2 ns.
113 1.6 ms in units of 3.2 ns = 1.6ms / 3.2ns = 5 * 10^5 = 0x7a120.
114 Since link_timer value of 1G SGMII will be too short for 2.5 SGMII,
115 we always set up here a value of 2.5 SGMII. */
116 MEMAC_MII_WritePhyReg(p_Memac, phyAddr, 0x13, 0x0007);
117 MEMAC_MII_WritePhyReg(p_Memac, phyAddr, 0x12, 0xa120);
120 tmpReg16 = PHY_SGMII_CR_DEF_VAL | PHY_SGMII_CR_RESET_AN;
121 MEMAC_MII_WritePhyReg(p_Memac, phyAddr, 0x0, tmpReg16);
123 /* Restore original enet mode */
124 p_Memac->enetMode = enetMode;
127 /* ......................................................................... */
129 static void SetupSgmiiInternalPhyBaseX(t_Memac *p_Memac, uint8_t phyAddr)
134 /* In case the higher MACs are used (i.e. the MACs that should support 10G),
135 speed=10000 is provided for SGMII ports. Temporary modify enet mode
136 to 1G one, so MII functions can work correctly. */
137 enetMode = p_Memac->enetMode;
138 p_Memac->enetMode = MAKE_ENET_MODE(ENET_INTERFACE_FROM_MODE(p_Memac->enetMode), e_ENET_SPEED_1000);
141 tmpReg16 = PHY_SGMII_IF_MODE_1000X;
142 MEMAC_MII_WritePhyReg(p_Memac, phyAddr, 0x14, tmpReg16);
144 /* AN Device capability */
145 tmpReg16 = PHY_SGMII_DEV_ABILITY_1000X;
146 MEMAC_MII_WritePhyReg(p_Memac, phyAddr, 0x4, tmpReg16);
148 /* Adjust link timer for SGMII -
149 For Serdes 1000BaseX auto-negotiation the timer should be 10 ms.
150 The link_timer register is configured in units of the clock.
151 - When running as 1G SGMII, Serdes clock is 125 MHz, so
152 unit = 1 / (125*10^6 Hz) = 8 ns.
153 10 ms in units of 8 ns = 10ms / 8ns = 1250000 = 0x1312d0
154 - When running as 2.5G SGMII, Serdes clock is 312.5 MHz, so
155 unit = 1 / (312.5*10^6 Hz) = 3.2 ns.
156 10 ms in units of 3.2 ns = 10ms / 3.2ns = 3125000 = 0x2faf08.
157 Since link_timer value of 1G SGMII will be too short for 2.5 SGMII,
158 we always set up here a value of 2.5 SGMII. */
159 MEMAC_MII_WritePhyReg(p_Memac, phyAddr, 0x13, 0x002f);
160 MEMAC_MII_WritePhyReg(p_Memac, phyAddr, 0x12, 0xaf08);
163 tmpReg16 = PHY_SGMII_CR_DEF_VAL | PHY_SGMII_CR_RESET_AN;
164 MEMAC_MII_WritePhyReg(p_Memac, phyAddr, 0x0, tmpReg16);
166 /* Restore original enet mode */
167 p_Memac->enetMode = enetMode;
170 /* ......................................................................... */
172 static t_Error CheckInitParameters(t_Memac *p_Memac)
174 e_FmMacType portType;
176 portType = ((ENET_SPEED_FROM_MODE(p_Memac->enetMode) < e_ENET_SPEED_10000) ? e_FM_MAC_1G : e_FM_MAC_10G);
178 #if (FM_MAX_NUM_OF_10G_MACS > 0)
179 if ((portType == e_FM_MAC_10G) && (p_Memac->macId >= FM_MAX_NUM_OF_10G_MACS))
180 RETURN_ERROR(MAJOR, E_INVALID_VALUE, ("10G MAC ID must be less than %d", FM_MAX_NUM_OF_10G_MACS));
181 #endif /* (FM_MAX_NUM_OF_10G_MACS > 0) */
183 if ((portType == e_FM_MAC_1G) && (p_Memac->macId >= FM_MAX_NUM_OF_1G_MACS))
184 RETURN_ERROR(MAJOR, E_INVALID_VALUE, ("1G MAC ID must be less than %d", FM_MAX_NUM_OF_1G_MACS));
185 if (p_Memac->addr == 0)
186 RETURN_ERROR(MAJOR, E_INVALID_VALUE, ("Ethernet MAC must have a valid MAC address"));
187 if (!p_Memac->f_Exception)
188 RETURN_ERROR(MAJOR, E_INVALID_VALUE, ("Uninitialized f_Exception"));
189 if (!p_Memac->f_Event)
190 RETURN_ERROR(MAJOR, E_INVALID_VALUE, ("Uninitialized f_Event"));
191 #ifdef FM_LEN_CHECK_ERRATA_FMAN_SW002
192 if (!p_Memac->p_MemacDriverParam->no_length_check_enable)
193 RETURN_ERROR(MINOR, E_NOT_SUPPORTED, ("LengthCheck!"));
194 #endif /* FM_LEN_CHECK_ERRATA_FMAN_SW002 */
199 /* ........................................................................... */
201 static void MemacErrException(t_Handle h_Memac)
203 t_Memac *p_Memac = (t_Memac *)h_Memac;
204 uint32_t event, imask;
206 event = fman_memac_get_event(p_Memac->p_MemMap, 0xffffffff);
207 imask = fman_memac_get_interrupt_mask(p_Memac->p_MemMap);
209 /* Imask include both error and notification/event bits.
210 Leaving only error bits enabled by imask.
211 The imask error bits are shifted by 16 bits offset from
212 their corresponding location in the ievent - hence the >> 16 */
213 event &= ((imask & MEMAC_ALL_ERRS_IMASK) >> 16);
215 fman_memac_ack_event(p_Memac->p_MemMap, event);
217 if (event & MEMAC_IEVNT_TS_ECC_ER)
218 p_Memac->f_Exception(p_Memac->h_App, e_FM_MAC_EX_TS_FIFO_ECC_ERR);
219 if (event & MEMAC_IEVNT_TX_ECC_ER)
220 p_Memac->f_Exception(p_Memac->h_App, e_FM_MAC_EX_10G_1TX_ECC_ER);
221 if (event & MEMAC_IEVNT_RX_ECC_ER)
222 p_Memac->f_Exception(p_Memac->h_App, e_FM_MAC_EX_10G_RX_ECC_ER);
225 static void MemacException(t_Handle h_Memac)
227 t_Memac *p_Memac = (t_Memac *)h_Memac;
228 uint32_t event, imask;
230 event = fman_memac_get_event(p_Memac->p_MemMap, 0xffffffff);
231 imask = fman_memac_get_interrupt_mask(p_Memac->p_MemMap);
233 /* Imask include both error and notification/event bits.
234 Leaving only error bits enabled by imask.
235 The imask error bits are shifted by 16 bits offset from
236 their corresponding location in the ievent - hence the >> 16 */
237 event &= ((imask & MEMAC_ALL_ERRS_IMASK) >> 16);
239 fman_memac_ack_event(p_Memac->p_MemMap, event);
241 if (event & MEMAC_IEVNT_MGI)
242 p_Memac->f_Exception(p_Memac->h_App, e_FM_MAC_EX_MAGIC_PACKET_INDICATION);
245 /* ......................................................................... */
247 static void FreeInitResources(t_Memac *p_Memac)
249 e_FmMacType portType;
252 ((ENET_SPEED_FROM_MODE(p_Memac->enetMode) < e_ENET_SPEED_10000) ? e_FM_MAC_1G : e_FM_MAC_10G);
254 if (portType == e_FM_MAC_10G)
255 FmUnregisterIntr(p_Memac->fmMacControllerDriver.h_Fm, e_FM_MOD_10G_MAC, p_Memac->macId, e_FM_INTR_TYPE_ERR);
257 FmUnregisterIntr(p_Memac->fmMacControllerDriver.h_Fm, e_FM_MOD_1G_MAC, p_Memac->macId, e_FM_INTR_TYPE_ERR);
259 /* release the driver's group hash table */
260 FreeHashTable(p_Memac->p_MulticastAddrHash);
261 p_Memac->p_MulticastAddrHash = NULL;
263 /* release the driver's individual hash table */
264 FreeHashTable(p_Memac->p_UnicastAddrHash);
265 p_Memac->p_UnicastAddrHash = NULL;
269 /*****************************************************************************/
270 /* mEMAC API routines */
271 /*****************************************************************************/
273 /* ......................................................................... */
275 static t_Error MemacEnable(t_Handle h_Memac, e_CommMode mode)
277 t_Memac *p_Memac = (t_Memac *)h_Memac;
279 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_HANDLE);
280 SANITY_CHECK_RETURN_ERROR(!p_Memac->p_MemacDriverParam, E_INVALID_STATE);
282 fman_memac_enable(p_Memac->p_MemMap, (mode & e_COMM_MODE_RX), (mode & e_COMM_MODE_TX));
287 /* ......................................................................... */
289 static t_Error MemacDisable (t_Handle h_Memac, e_CommMode mode)
291 t_Memac *p_Memac = (t_Memac *)h_Memac;
293 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_HANDLE);
294 SANITY_CHECK_RETURN_ERROR(!p_Memac->p_MemacDriverParam, E_INVALID_STATE);
296 fman_memac_disable(p_Memac->p_MemMap, (mode & e_COMM_MODE_RX), (mode & e_COMM_MODE_TX));
301 /* ......................................................................... */
303 static t_Error MemacSetPromiscuous(t_Handle h_Memac, bool newVal)
305 t_Memac *p_Memac = (t_Memac *)h_Memac;
307 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_HANDLE);
308 SANITY_CHECK_RETURN_ERROR(!p_Memac->p_MemacDriverParam, E_INVALID_STATE);
310 fman_memac_set_promiscuous(p_Memac->p_MemMap, newVal);
315 /* .............................................................................. */
317 static t_Error MemacAdjustLink(t_Handle h_Memac, e_EnetSpeed speed, bool fullDuplex)
319 t_Memac *p_Memac = (t_Memac *)h_Memac;
321 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_HANDLE);
322 SANITY_CHECK_RETURN_ERROR(!p_Memac->p_MemacDriverParam, E_INVALID_STATE);
324 if ((speed >= e_ENET_SPEED_1000) && (!fullDuplex))
325 RETURN_ERROR(MAJOR, E_CONFLICT,
326 ("Ethernet MAC 1G or 10G does not support half-duplex"));
328 fman_memac_adjust_link(p_Memac->p_MemMap,
329 (enum enet_interface)ENET_INTERFACE_FROM_MODE(p_Memac->enetMode),
330 (enum enet_speed)speed,
336 /*****************************************************************************/
337 /* Memac Configs modification functions */
338 /*****************************************************************************/
340 /* ......................................................................... */
342 static t_Error MemacConfigLoopback(t_Handle h_Memac, bool newVal)
344 t_Memac *p_Memac = (t_Memac *)h_Memac;
346 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_HANDLE);
347 SANITY_CHECK_RETURN_ERROR(p_Memac->p_MemacDriverParam, E_INVALID_STATE);
349 p_Memac->p_MemacDriverParam->loopback_enable = newVal;
354 /* ......................................................................... */
356 static t_Error MemacConfigWan(t_Handle h_Memac, bool newVal)
358 t_Memac *p_Memac = (t_Memac *)h_Memac;
360 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_HANDLE);
361 SANITY_CHECK_RETURN_ERROR(p_Memac->p_MemacDriverParam, E_INVALID_STATE);
363 p_Memac->p_MemacDriverParam->wan_mode_enable = newVal;
368 /* ......................................................................... */
370 static t_Error MemacConfigMaxFrameLength(t_Handle h_Memac, uint16_t newVal)
372 t_Memac *p_Memac = (t_Memac *)h_Memac;
374 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_HANDLE);
375 SANITY_CHECK_RETURN_ERROR(p_Memac->p_MemacDriverParam, E_INVALID_STATE);
377 p_Memac->p_MemacDriverParam->max_frame_length = newVal;
382 /* ......................................................................... */
384 static t_Error MemacConfigPad(t_Handle h_Memac, bool newVal)
386 t_Memac *p_Memac = (t_Memac *)h_Memac;
388 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_HANDLE);
389 SANITY_CHECK_RETURN_ERROR(p_Memac->p_MemacDriverParam, E_INVALID_STATE);
391 p_Memac->p_MemacDriverParam->pad_enable = newVal;
396 /* ......................................................................... */
398 static t_Error MemacConfigLengthCheck(t_Handle h_Memac, bool newVal)
400 t_Memac *p_Memac = (t_Memac *)h_Memac;
402 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_HANDLE);
403 SANITY_CHECK_RETURN_ERROR(p_Memac->p_MemacDriverParam, E_INVALID_STATE);
405 p_Memac->p_MemacDriverParam->no_length_check_enable = !newVal;
410 /* ......................................................................... */
412 static t_Error MemacConfigException(t_Handle h_Memac, e_FmMacExceptions exception, bool enable)
414 t_Memac *p_Memac = (t_Memac *)h_Memac;
415 uint32_t bitMask = 0;
417 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_HANDLE);
418 SANITY_CHECK_RETURN_ERROR(p_Memac->p_MemacDriverParam, E_INVALID_STATE);
420 GET_EXCEPTION_FLAG(bitMask, exception);
424 p_Memac->exceptions |= bitMask;
426 p_Memac->exceptions &= ~bitMask;
429 RETURN_ERROR(MAJOR, E_INVALID_VALUE, ("Undefined exception"));
434 /* ......................................................................... */
436 static t_Error MemacConfigResetOnInit(t_Handle h_Memac, bool enable)
438 t_Memac *p_Memac = (t_Memac *)h_Memac;
440 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_HANDLE);
441 SANITY_CHECK_RETURN_ERROR(p_Memac->p_MemacDriverParam, E_INVALID_STATE);
443 p_Memac->p_MemacDriverParam->reset_on_init = enable;
449 /*****************************************************************************/
450 /* Memac Run Time API functions */
451 /*****************************************************************************/
453 /* ......................................................................... */
455 static t_Error MemacSetTxPauseFrames(t_Handle h_Memac,
460 t_Memac *p_Memac = (t_Memac *)h_Memac;
462 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_STATE);
463 SANITY_CHECK_RETURN_ERROR(!p_Memac->p_MemacDriverParam, E_INVALID_STATE);
465 if (priority != 0xFF)
467 bool PortConfigured, PreFetchEnabled;
469 if (FmGetTnumAgingPeriod(p_Memac->fmMacControllerDriver.h_Fm) == 0)
470 RETURN_ERROR(MAJOR, E_CONFLICT, ("For PFC operation, TNUM aging must be enabled"));
472 FmGetPortPreFetchConfiguration(p_Memac->fmMacControllerDriver.h_Fm,
473 p_Memac->fmMacControllerDriver.macId,
477 if ((ENET_SPEED_FROM_MODE(p_Memac->fmMacControllerDriver.enetMode) == e_ENET_SPEED_1000) && !PortConfigured)
478 DBG(INFO, ("For PFC correct operation, prefetch must be configured on the FM Tx PORT"));
480 if ((ENET_SPEED_FROM_MODE(p_Memac->fmMacControllerDriver.enetMode) == e_ENET_SPEED_1000) && PortConfigured && !PreFetchEnabled)
481 DBG(WARNING, ("For PFC correct operation, prefetch must be configured on the FM Tx PORT"));
484 fman_memac_set_tx_pause_frames(p_Memac->p_MemMap, priority, pauseTime, threshTime);
489 /* ......................................................................... */
491 static t_Error MemacSetTxAutoPauseFrames(t_Handle h_Memac,
494 return MemacSetTxPauseFrames(h_Memac, FM_MAC_NO_PFC, pauseTime, 0);
497 /* ......................................................................... */
499 static t_Error MemacSetRxIgnorePauseFrames(t_Handle h_Memac, bool en)
501 t_Memac *p_Memac = (t_Memac *)h_Memac;
503 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_STATE);
504 SANITY_CHECK_RETURN_ERROR(!p_Memac->p_MemacDriverParam, E_INVALID_STATE);
506 fman_memac_set_rx_ignore_pause_frames(p_Memac->p_MemMap, en);
511 /* ......................................................................... */
513 static t_Error MemacSetWakeOnLan(t_Handle h_Memac, bool en)
515 t_Memac *p_Memac = (t_Memac *)h_Memac;
517 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_STATE);
518 SANITY_CHECK_RETURN_ERROR(!p_Memac->p_MemacDriverParam, E_INVALID_STATE);
520 fman_memac_set_wol(p_Memac->p_MemMap, en);
525 /* .............................................................................. */
527 static t_Error MemacEnable1588TimeStamp(t_Handle h_Memac)
529 t_Memac *p_Memac = (t_Memac *)h_Memac;
531 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_HANDLE);
532 SANITY_CHECK_RETURN_ERROR(!p_Memac->p_MemacDriverParam, E_INVALID_STATE);
534 DBG(WARNING, ("mEMAC has 1588 always enabled!"));
539 /* Counters handling */
540 /* ......................................................................... */
542 static t_Error MemacGetStatistics(t_Handle h_Memac, t_FmMacStatistics *p_Statistics)
544 t_Memac *p_Memac = (t_Memac *)h_Memac;
546 SANITY_CHECK_RETURN_ERROR(p_Memac, E_NULL_POINTER);
547 SANITY_CHECK_RETURN_ERROR(!p_Memac->p_MemacDriverParam, E_INVALID_STATE);
548 SANITY_CHECK_RETURN_ERROR(p_Statistics, E_NULL_POINTER);
550 p_Statistics->eStatPkts64 = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_R64);
551 p_Statistics->eStatPkts65to127 = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_R127);
552 p_Statistics->eStatPkts128to255 = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_R255);
553 p_Statistics->eStatPkts256to511 = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_R511);
554 p_Statistics->eStatPkts512to1023 = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_R1023);
555 p_Statistics->eStatPkts1024to1518 = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_R1518);
556 p_Statistics->eStatPkts1519to1522 = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_R1519X);
558 p_Statistics->eStatFragments = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_RFRG);
559 p_Statistics->eStatJabbers = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_RJBR);
561 p_Statistics->eStatsDropEvents = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_RDRP);
562 p_Statistics->eStatCRCAlignErrors = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_RALN);
564 p_Statistics->eStatUndersizePkts = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_TUND);
565 p_Statistics->eStatOversizePkts = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_ROVR);
567 p_Statistics->reStatPause = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_RXPF);
568 p_Statistics->teStatPause = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_TXPF);
571 p_Statistics->ifInOctets = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_ROCT);
572 p_Statistics->ifInUcastPkts = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_RUCA);
573 p_Statistics->ifInMcastPkts = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_RMCA);
574 p_Statistics->ifInBcastPkts = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_RBCA);
575 p_Statistics->ifInPkts = p_Statistics->ifInUcastPkts
576 + p_Statistics->ifInMcastPkts
577 + p_Statistics->ifInBcastPkts;
578 p_Statistics->ifInDiscards = 0;
579 p_Statistics->ifInErrors = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_RERR);
581 p_Statistics->ifOutOctets = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_TOCT);
582 p_Statistics->ifOutUcastPkts = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_TUCA);
583 p_Statistics->ifOutMcastPkts = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_TMCA);
584 p_Statistics->ifOutBcastPkts = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_TBCA);
585 p_Statistics->ifOutPkts = p_Statistics->ifOutUcastPkts
586 + p_Statistics->ifOutMcastPkts
587 + p_Statistics->ifOutBcastPkts;
588 p_Statistics->ifOutDiscards = 0;
589 p_Statistics->ifOutErrors = fman_memac_get_counter(p_Memac->p_MemMap, E_MEMAC_COUNTER_TERR);
594 /* ......................................................................... */
596 static t_Error MemacModifyMacAddress (t_Handle h_Memac, t_EnetAddr *p_EnetAddr)
598 t_Memac *p_Memac = (t_Memac *)h_Memac;
600 SANITY_CHECK_RETURN_ERROR(p_Memac, E_NULL_POINTER);
601 SANITY_CHECK_RETURN_ERROR(!p_Memac->p_MemacDriverParam, E_INVALID_STATE);
603 fman_memac_add_addr_in_paddr(p_Memac->p_MemMap, (uint8_t *)(*p_EnetAddr), 0);
608 /* ......................................................................... */
610 static t_Error MemacResetCounters (t_Handle h_Memac)
612 t_Memac *p_Memac = (t_Memac *)h_Memac;
614 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_HANDLE);
615 SANITY_CHECK_RETURN_ERROR(!p_Memac->p_MemacDriverParam, E_INVALID_STATE);
617 fman_memac_reset_stat(p_Memac->p_MemMap);
622 /* ......................................................................... */
624 static t_Error MemacAddExactMatchMacAddress(t_Handle h_Memac, t_EnetAddr *p_EthAddr)
626 t_Memac *p_Memac = (t_Memac *) h_Memac;
630 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_HANDLE);
631 SANITY_CHECK_RETURN_ERROR(!p_Memac->p_MemacDriverParam, E_INVALID_STATE);
633 ethAddr = ENET_ADDR_TO_UINT64(*p_EthAddr);
635 if (ethAddr & GROUP_ADDRESS)
636 /* Multicast address has no effect in PADDR */
637 RETURN_ERROR(MAJOR, E_INVALID_VALUE, ("Multicast address"));
639 /* Make sure no PADDR contains this address */
640 for (paddrNum = 0; paddrNum < MEMAC_NUM_OF_PADDRS; paddrNum++)
641 if (p_Memac->indAddrRegUsed[paddrNum])
642 if (p_Memac->paddr[paddrNum] == ethAddr)
643 RETURN_ERROR(MAJOR, E_ALREADY_EXISTS, NO_MSG);
645 /* Find first unused PADDR */
646 for (paddrNum = 0; paddrNum < MEMAC_NUM_OF_PADDRS; paddrNum++)
647 if (!(p_Memac->indAddrRegUsed[paddrNum]))
649 /* mark this PADDR as used */
650 p_Memac->indAddrRegUsed[paddrNum] = TRUE;
652 p_Memac->paddr[paddrNum] = ethAddr;
654 /* put in hardware */
655 fman_memac_add_addr_in_paddr(p_Memac->p_MemMap, (uint8_t*)(*p_EthAddr), paddrNum);
656 p_Memac->numOfIndAddrInRegs++;
662 RETURN_ERROR(MAJOR, E_FULL, NO_MSG);
665 /* ......................................................................... */
667 static t_Error MemacDelExactMatchMacAddress(t_Handle h_Memac, t_EnetAddr *p_EthAddr)
669 t_Memac *p_Memac = (t_Memac *) h_Memac;
673 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_HANDLE);
674 SANITY_CHECK_RETURN_ERROR(!p_Memac->p_MemacDriverParam, E_INVALID_STATE);
676 ethAddr = ENET_ADDR_TO_UINT64(*p_EthAddr);
678 /* Find used PADDR containing this address */
679 for (paddrNum = 0; paddrNum < MEMAC_NUM_OF_PADDRS; paddrNum++)
681 if ((p_Memac->indAddrRegUsed[paddrNum]) &&
682 (p_Memac->paddr[paddrNum] == ethAddr))
684 /* mark this PADDR as not used */
685 p_Memac->indAddrRegUsed[paddrNum] = FALSE;
686 /* clear in hardware */
687 fman_memac_clear_addr_in_paddr(p_Memac->p_MemMap, paddrNum);
688 p_Memac->numOfIndAddrInRegs--;
694 RETURN_ERROR(MAJOR, E_NOT_FOUND, NO_MSG);
697 /* ......................................................................... */
699 static t_Error MemacGetId(t_Handle h_Memac, uint32_t *macId)
701 t_Memac *p_Memac = (t_Memac *)h_Memac;
703 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_HANDLE);
704 SANITY_CHECK_RETURN_ERROR(!p_Memac->p_MemacDriverParam, E_INVALID_STATE);
706 *macId = p_Memac->macId;
711 /* ......................................................................... */
714 static t_Error MemacAddHashMacAddress(t_Handle h_Memac, t_EnetAddr *p_EthAddr)
716 t_Memac *p_Memac = (t_Memac *)h_Memac;
717 t_EthHashEntry *p_HashEntry;
721 SANITY_CHECK_RETURN_ERROR(p_Memac, E_NULL_POINTER);
722 SANITY_CHECK_RETURN_ERROR(!p_Memac->p_MemacDriverParam, E_INVALID_STATE);
724 ethAddr = ENET_ADDR_TO_UINT64(*p_EthAddr);
726 if (!(ethAddr & GROUP_ADDRESS))
727 /* Unicast addresses not supported in hash */
728 RETURN_ERROR(MAJOR, E_NOT_SUPPORTED, ("Unicast Address"));
730 hash = GetMacAddrHashCode(ethAddr) & HASH_CTRL_ADDR_MASK;
732 /* Create element to be added to the driver hash table */
733 p_HashEntry = (t_EthHashEntry *)XX_Malloc(sizeof(t_EthHashEntry));
734 p_HashEntry->addr = ethAddr;
735 INIT_LIST(&p_HashEntry->node);
737 LIST_AddToTail(&(p_HashEntry->node), &(p_Memac->p_MulticastAddrHash->p_Lsts[hash]));
738 fman_memac_set_hash_table(p_Memac->p_MemMap, (hash | HASH_CTRL_MCAST_EN));
743 /* ......................................................................... */
745 static t_Error MemacDelHashMacAddress(t_Handle h_Memac, t_EnetAddr *p_EthAddr)
747 t_Memac *p_Memac = (t_Memac *)h_Memac;
748 t_EthHashEntry *p_HashEntry = NULL;
753 SANITY_CHECK_RETURN_ERROR(p_Memac, E_NULL_POINTER);
754 SANITY_CHECK_RETURN_ERROR(!p_Memac->p_MemacDriverParam, E_INVALID_STATE);
756 ethAddr = ENET_ADDR_TO_UINT64(*p_EthAddr);
758 hash = GetMacAddrHashCode(ethAddr) & HASH_CTRL_ADDR_MASK;
760 LIST_FOR_EACH(p_Pos, &(p_Memac->p_MulticastAddrHash->p_Lsts[hash]))
762 p_HashEntry = ETH_HASH_ENTRY_OBJ(p_Pos);
763 if (p_HashEntry->addr == ethAddr)
765 LIST_DelAndInit(&p_HashEntry->node);
766 XX_Free(p_HashEntry);
770 if (LIST_IsEmpty(&p_Memac->p_MulticastAddrHash->p_Lsts[hash]))
771 fman_memac_set_hash_table(p_Memac->p_MemMap, (hash & ~HASH_CTRL_MCAST_EN));
777 /* ......................................................................... */
779 static t_Error MemacSetException(t_Handle h_Memac, e_FmMacExceptions exception, bool enable)
781 t_Memac *p_Memac = (t_Memac *)h_Memac;
782 uint32_t bitMask = 0;
784 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_HANDLE);
785 SANITY_CHECK_RETURN_ERROR(!p_Memac->p_MemacDriverParam, E_INVALID_STATE);
787 GET_EXCEPTION_FLAG(bitMask, exception);
791 p_Memac->exceptions |= bitMask;
793 p_Memac->exceptions &= ~bitMask;
796 RETURN_ERROR(MAJOR, E_INVALID_VALUE, ("Undefined exception"));
798 fman_memac_set_exception(p_Memac->p_MemMap, bitMask, enable);
803 /* ......................................................................... */
805 static uint16_t MemacGetMaxFrameLength(t_Handle h_Memac)
807 t_Memac *p_Memac = (t_Memac *)h_Memac;
809 SANITY_CHECK_RETURN_VALUE(p_Memac, E_INVALID_HANDLE, 0);
810 SANITY_CHECK_RETURN_VALUE(!p_Memac->p_MemacDriverParam, E_INVALID_STATE, 0);
812 return fman_memac_get_max_frame_len(p_Memac->p_MemMap);
815 static t_Error MemacInitInternalPhy(t_Handle h_Memac)
817 t_Memac *p_Memac = (t_Memac *)h_Memac;
820 if (ENET_INTERFACE_FROM_MODE(p_Memac->enetMode) == e_ENET_IF_SGMII)
822 /* Configure internal SGMII PHY */
823 if (p_Memac->enetMode & ENET_IF_SGMII_BASEX)
824 SetupSgmiiInternalPhyBaseX(p_Memac, PHY_MDIO_ADDR);
826 SetupSgmiiInternalPhy(p_Memac, PHY_MDIO_ADDR);
828 else if (ENET_INTERFACE_FROM_MODE(p_Memac->enetMode) == e_ENET_IF_QSGMII)
830 /* Configure 4 internal SGMII PHYs */
831 for (i = 0; i < 4; i++)
833 /* QSGMII PHY address occupies 3 upper bits of 5-bit
834 phyAddress; the lower 2 bits are used to extend
835 register address space and access each one of 4
836 ports inside QSGMII. */
837 phyAddr = (uint8_t)((PHY_MDIO_ADDR << 2) | i);
838 if (p_Memac->enetMode & ENET_IF_SGMII_BASEX)
839 SetupSgmiiInternalPhyBaseX(p_Memac, phyAddr);
841 SetupSgmiiInternalPhy(p_Memac, phyAddr);
847 /*****************************************************************************/
848 /* mEMAC Init & Free API */
849 /*****************************************************************************/
851 /* ......................................................................... */
853 static t_Error MemacInit(t_Handle h_Memac)
855 t_Memac *p_Memac = (t_Memac *)h_Memac;
856 struct memac_cfg *p_MemacDriverParam;
857 enum enet_interface enet_interface;
858 enum enet_speed enet_speed;
860 e_FmMacType portType;
862 bool slow_10g_if = FALSE;
863 if (p_Memac->macId == 3) /* This is a quick WA */
864 g_MemacRegs = p_Memac->p_MemMap;
866 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_HANDLE);
867 SANITY_CHECK_RETURN_ERROR(p_Memac->p_MemacDriverParam, E_INVALID_STATE);
868 SANITY_CHECK_RETURN_ERROR(p_Memac->fmMacControllerDriver.h_Fm, E_INVALID_HANDLE);
870 FM_GetRevision(p_Memac->fmMacControllerDriver.h_Fm, &p_Memac->fmMacControllerDriver.fmRevInfo);
871 if (p_Memac->fmMacControllerDriver.fmRevInfo.majorRev == 6 &&
872 p_Memac->fmMacControllerDriver.fmRevInfo.minorRev == 4)
875 CHECK_INIT_PARAMETERS(p_Memac, CheckInitParameters);
877 p_MemacDriverParam = p_Memac->p_MemacDriverParam;
880 ((ENET_SPEED_FROM_MODE(p_Memac->enetMode) < e_ENET_SPEED_10000) ? e_FM_MAC_1G : e_FM_MAC_10G);
882 /* First, reset the MAC if desired. */
883 if (p_MemacDriverParam->reset_on_init)
884 fman_memac_reset(p_Memac->p_MemMap);
887 MAKE_ENET_ADDR_FROM_UINT64(p_Memac->addr, ethAddr);
888 fman_memac_add_addr_in_paddr(p_Memac->p_MemMap, (uint8_t*)ethAddr, 0);
890 enet_interface = (enum enet_interface) ENET_INTERFACE_FROM_MODE(p_Memac->enetMode);
891 enet_speed = (enum enet_speed) ENET_SPEED_FROM_MODE(p_Memac->enetMode);
893 fman_memac_init(p_Memac->p_MemMap,
894 p_Memac->p_MemacDriverParam,
898 p_Memac->exceptions);
900 #ifdef FM_RX_FIFO_CORRUPT_ERRATA_10GMAC_A006320
904 FM_GetRevision(p_Memac->fmMacControllerDriver.h_Fm, &p_Memac->fmMacControllerDriver.fmRevInfo);
905 /* check the FMAN version - the bug exists only in rev1 */
906 if ((p_Memac->fmMacControllerDriver.fmRevInfo.majorRev == 6) &&
907 (p_Memac->fmMacControllerDriver.fmRevInfo.minorRev == 0))
909 /* MAC strips CRC from received frames - this workaround should
910 decrease the likelihood of bug appearance
912 tmpReg = GET_UINT32(p_Memac->p_MemMap->command_config);
913 tmpReg &= ~CMD_CFG_CRC_FWD;
914 WRITE_UINT32(p_Memac->p_MemMap->command_config, tmpReg);
915 /* DBG(WARNING, ("mEMAC strips CRC from received frames as part of A006320 errata workaround"));*/
918 #endif /* FM_RX_FIFO_CORRUPT_ERRATA_10GMAC_A006320 */
920 MemacInitInternalPhy(h_Memac);
922 /* Max Frame Length */
923 err = FmSetMacMaxFrame(p_Memac->fmMacControllerDriver.h_Fm,
925 p_Memac->fmMacControllerDriver.macId,
926 p_MemacDriverParam->max_frame_length);
928 RETURN_ERROR(MAJOR, err, ("settings Mac max frame length is FAILED"));
930 p_Memac->p_MulticastAddrHash = AllocHashTable(HASH_TABLE_SIZE);
931 if (!p_Memac->p_MulticastAddrHash)
933 FreeInitResources(p_Memac);
934 RETURN_ERROR(MAJOR, E_NO_MEMORY, ("allocation hash table is FAILED"));
937 p_Memac->p_UnicastAddrHash = AllocHashTable(HASH_TABLE_SIZE);
938 if (!p_Memac->p_UnicastAddrHash)
940 FreeInitResources(p_Memac);
941 RETURN_ERROR(MAJOR, E_NO_MEMORY, ("allocation hash table is FAILED"));
944 FmRegisterIntr(p_Memac->fmMacControllerDriver.h_Fm,
945 (portType == e_FM_MAC_10G) ? e_FM_MOD_10G_MAC : e_FM_MOD_1G_MAC,
951 FmRegisterIntr(p_Memac->fmMacControllerDriver.h_Fm,
952 (portType == e_FM_MAC_10G) ? e_FM_MOD_10G_MAC : e_FM_MOD_1G_MAC,
954 e_FM_INTR_TYPE_NORMAL,
958 XX_Free(p_MemacDriverParam);
959 p_Memac->p_MemacDriverParam = NULL;
964 /* ......................................................................... */
966 static t_Error MemacFree(t_Handle h_Memac)
968 t_Memac *p_Memac = (t_Memac *)h_Memac;
970 SANITY_CHECK_RETURN_ERROR(p_Memac, E_INVALID_HANDLE);
972 if (p_Memac->p_MemacDriverParam)
974 /* Called after config */
975 XX_Free(p_Memac->p_MemacDriverParam);
976 p_Memac->p_MemacDriverParam = NULL;
979 /* Called after init */
980 FreeInitResources(p_Memac);
987 /* ......................................................................... */
989 static void InitFmMacControllerDriver(t_FmMacControllerDriver *p_FmMacControllerDriver)
991 p_FmMacControllerDriver->f_FM_MAC_Init = MemacInit;
992 p_FmMacControllerDriver->f_FM_MAC_Free = MemacFree;
994 p_FmMacControllerDriver->f_FM_MAC_SetStatistics = NULL;
995 p_FmMacControllerDriver->f_FM_MAC_ConfigLoopback = MemacConfigLoopback;
996 p_FmMacControllerDriver->f_FM_MAC_ConfigMaxFrameLength = MemacConfigMaxFrameLength;
998 p_FmMacControllerDriver->f_FM_MAC_ConfigWan = MemacConfigWan;
1000 p_FmMacControllerDriver->f_FM_MAC_ConfigPadAndCrc = MemacConfigPad;
1001 p_FmMacControllerDriver->f_FM_MAC_ConfigHalfDuplex = NULL; /* half-duplex is detected automatically */
1002 p_FmMacControllerDriver->f_FM_MAC_ConfigLengthCheck = MemacConfigLengthCheck;
1004 p_FmMacControllerDriver->f_FM_MAC_ConfigException = MemacConfigException;
1005 p_FmMacControllerDriver->f_FM_MAC_ConfigResetOnInit = MemacConfigResetOnInit;
1007 p_FmMacControllerDriver->f_FM_MAC_SetException = MemacSetException;
1009 p_FmMacControllerDriver->f_FM_MAC_Enable1588TimeStamp = MemacEnable1588TimeStamp; /* always enabled */
1010 p_FmMacControllerDriver->f_FM_MAC_Disable1588TimeStamp = NULL;
1012 p_FmMacControllerDriver->f_FM_MAC_SetPromiscuous = MemacSetPromiscuous;
1013 p_FmMacControllerDriver->f_FM_MAC_AdjustLink = MemacAdjustLink;
1014 p_FmMacControllerDriver->f_FM_MAC_RestartAutoneg = NULL;
1016 p_FmMacControllerDriver->f_FM_MAC_Enable = MemacEnable;
1017 p_FmMacControllerDriver->f_FM_MAC_Disable = MemacDisable;
1018 p_FmMacControllerDriver->f_FM_MAC_Resume = MemacInitInternalPhy;
1020 p_FmMacControllerDriver->f_FM_MAC_SetTxAutoPauseFrames = MemacSetTxAutoPauseFrames;
1021 p_FmMacControllerDriver->f_FM_MAC_SetTxPauseFrames = MemacSetTxPauseFrames;
1022 p_FmMacControllerDriver->f_FM_MAC_SetRxIgnorePauseFrames = MemacSetRxIgnorePauseFrames;
1024 p_FmMacControllerDriver->f_FM_MAC_SetWakeOnLan = MemacSetWakeOnLan;
1026 p_FmMacControllerDriver->f_FM_MAC_ResetCounters = MemacResetCounters;
1027 p_FmMacControllerDriver->f_FM_MAC_GetStatistics = MemacGetStatistics;
1029 p_FmMacControllerDriver->f_FM_MAC_ModifyMacAddr = MemacModifyMacAddress;
1030 p_FmMacControllerDriver->f_FM_MAC_AddHashMacAddr = MemacAddHashMacAddress;
1031 p_FmMacControllerDriver->f_FM_MAC_RemoveHashMacAddr = MemacDelHashMacAddress;
1032 p_FmMacControllerDriver->f_FM_MAC_AddExactMatchMacAddr = MemacAddExactMatchMacAddress;
1033 p_FmMacControllerDriver->f_FM_MAC_RemovelExactMatchMacAddr = MemacDelExactMatchMacAddress;
1034 p_FmMacControllerDriver->f_FM_MAC_GetId = MemacGetId;
1035 p_FmMacControllerDriver->f_FM_MAC_GetVersion = NULL;
1036 p_FmMacControllerDriver->f_FM_MAC_GetMaxFrameLength = MemacGetMaxFrameLength;
1038 p_FmMacControllerDriver->f_FM_MAC_MII_WritePhyReg = MEMAC_MII_WritePhyReg;
1039 p_FmMacControllerDriver->f_FM_MAC_MII_ReadPhyReg = MEMAC_MII_ReadPhyReg;
1043 /*****************************************************************************/
1044 /* mEMAC Config Main Entry */
1045 /*****************************************************************************/
1047 /* ......................................................................... */
1049 t_Handle MEMAC_Config(t_FmMacParams *p_FmMacParam)
1052 struct memac_cfg *p_MemacDriverParam;
1055 SANITY_CHECK_RETURN_VALUE(p_FmMacParam, E_NULL_POINTER, NULL);
1057 baseAddr = p_FmMacParam->baseAddr;
1058 /* Allocate memory for the mEMAC data structure */
1059 p_Memac = (t_Memac *)XX_Malloc(sizeof(t_Memac));
1062 REPORT_ERROR(MAJOR, E_NO_MEMORY, ("mEMAC driver structure"));
1065 memset(p_Memac, 0, sizeof(t_Memac));
1066 InitFmMacControllerDriver(&p_Memac->fmMacControllerDriver);
1068 /* Allocate memory for the mEMAC driver parameters data structure */
1069 p_MemacDriverParam = (struct memac_cfg *)XX_Malloc(sizeof(struct memac_cfg));
1070 if (!p_MemacDriverParam)
1072 REPORT_ERROR(MAJOR, E_NO_MEMORY, ("mEMAC driver parameters"));
1076 memset(p_MemacDriverParam, 0, sizeof(struct memac_cfg));
1078 /* Plant parameter structure pointer */
1079 p_Memac->p_MemacDriverParam = p_MemacDriverParam;
1081 fman_memac_defconfig(p_MemacDriverParam);
1083 p_Memac->addr = ENET_ADDR_TO_UINT64(p_FmMacParam->addr);
1085 p_Memac->p_MemMap = (struct memac_regs *)UINT_TO_PTR(baseAddr);
1086 p_Memac->p_MiiMemMap = (struct memac_mii_access_mem_map*)UINT_TO_PTR(baseAddr + MEMAC_TO_MII_OFFSET);
1088 p_Memac->enetMode = p_FmMacParam->enetMode;
1089 p_Memac->macId = p_FmMacParam->macId;
1090 p_Memac->exceptions = MEMAC_default_exceptions;
1091 p_Memac->f_Exception = p_FmMacParam->f_Exception;
1092 p_Memac->f_Event = p_FmMacParam->f_Event;
1093 p_Memac->h_App = p_FmMacParam->h_App;