2 * Copyright (C) 2013 Realtek Semiconductor Corp.
5 * Unless you and Realtek execute a separate written software license
6 * agreement governing use of this software, this software is licensed
7 * to you under the terms of the GNU General Public License version 2,
8 * available at https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
11 * $Date: 2017-03-09 10:41:21 +0800 (ι±ε, 09 δΈζ 2017) $
13 * Purpose : RTK switch high-level API
14 * Feature : Here is a list of all functions and variables in this module.
18 #ifndef __RTK_SWITCH_H__
19 #define __RTK_SWITCH_H__
21 #include <rtk_types.h>
23 #define UNDEFINE_PHY_PORT (0xFF)
24 #define RTK_SWITCH_PORT_NUM (32)
26 #define MAXPKTLEN_CFG_ID_MAX (1)
28 #define RTK_SWITCH_MAX_PKTLEN (0x3FFF)
30 typedef enum init_state_e
32 INIT_NOT_COMPLETED
= 0,
37 typedef enum switch_chip_e
46 typedef enum port_type_e
54 typedef struct rtk_switch_halCtrl_s
56 switch_chip_t switch_type
;
57 rtk_uint32 l2p_port
[RTK_SWITCH_PORT_NUM
];
58 rtk_uint32 p2l_port
[RTK_SWITCH_PORT_NUM
];
59 port_type_t log_port_type
[RTK_SWITCH_PORT_NUM
];
60 rtk_uint32 ptp_port
[RTK_SWITCH_PORT_NUM
];
61 rtk_uint32 valid_portmask
;
62 rtk_uint32 valid_utp_portmask
;
63 rtk_uint32 valid_ext_portmask
;
64 rtk_uint32 valid_cpu_portmask
;
65 rtk_uint32 min_phy_port
;
66 rtk_uint32 max_phy_port
;
67 rtk_uint32 phy_portmask
;
68 rtk_uint32 combo_logical_port
;
69 rtk_uint32 hsg_logical_port
;
70 rtk_uint32 sg_logical_portmask
;
71 rtk_uint32 max_meter_id
;
72 rtk_uint32 max_lut_addr_num
;
73 rtk_uint32 trunk_group_mask
;
75 }rtk_switch_halCtrl_t
;
77 typedef enum rtk_switch_maxPktLen_linkSpeed_e
{
78 MAXPKTLEN_LINK_SPEED_FE
= 0,
79 MAXPKTLEN_LINK_SPEED_GE
,
80 MAXPKTLEN_LINK_SPEED_END
,
81 } rtk_switch_maxPktLen_linkSpeed_t
;
85 #define RTK_CHK_INIT_STATE() \
88 if(rtk_switch_initialState_get() != INIT_COMPLETED) \
90 return RT_ERR_NOT_INIT; \
94 #define RTK_CHK_PORT_VALID(__port__) \
97 if(rtk_switch_logicalPortCheck(__port__) != RT_ERR_OK) \
99 return RT_ERR_PORT_ID; \
103 #define RTK_CHK_PORT_IS_UTP(__port__) \
106 if(rtk_switch_isUtpPort(__port__) != RT_ERR_OK) \
108 return RT_ERR_PORT_ID; \
112 #define RTK_CHK_PORT_IS_EXT(__port__) \
115 if(rtk_switch_isExtPort(__port__) != RT_ERR_OK) \
117 return RT_ERR_PORT_ID; \
121 #define RTK_CHK_PORT_IS_COMBO(__port__) \
124 if(rtk_switch_isComboPort(__port__) != RT_ERR_OK) \
126 return RT_ERR_PORT_ID; \
130 #define RTK_CHK_PORT_IS_PTP(__port__) \
133 if(rtk_switch_isPtpPort(__port__) != RT_ERR_OK) \
135 return RT_ERR_PORT_ID; \
139 #define RTK_CHK_PORTMASK_VALID(__portmask__) \
142 if(rtk_switch_isPortMaskValid(__portmask__) != RT_ERR_OK) \
144 return RT_ERR_PORT_MASK; \
148 #define RTK_CHK_PORTMASK_VALID_ONLY_UTP(__portmask__) \
151 if(rtk_switch_isPortMaskUtp(__portmask__) != RT_ERR_OK) \
153 return RT_ERR_PORT_MASK; \
157 #define RTK_CHK_PORTMASK_VALID_ONLY_EXT(__portmask__) \
160 if(rtk_switch_isPortMaskExt(__portmask__) != RT_ERR_OK) \
162 return RT_ERR_PORT_MASK; \
166 #define RTK_CHK_TRUNK_GROUP_VALID(__grpId__) \
169 if(rtk_switch_isValidTrunkGrpId(__grpId__) != RT_ERR_OK) \
171 return RT_ERR_LA_TRUNK_ID; \
175 #define RTK_PORTMASK_IS_PORT_SET(__portmask__, __port__) (((__portmask__).bits[0] & (0x00000001 << __port__)) ? 1 : 0)
176 #define RTK_PORTMASK_IS_EMPTY(__portmask__) (((__portmask__).bits[0] == 0) ? 1 : 0)
177 #define RTK_PORTMASK_CLEAR(__portmask__) ((__portmask__).bits[0] = 0)
178 #define RTK_PORTMASK_PORT_SET(__portmask__, __port__) ((__portmask__).bits[0] |= (0x00000001 << __port__))
179 #define RTK_PORTMASK_PORT_CLEAR(__portmask__, __port__) ((__portmask__).bits[0] &= ~(0x00000001 << __port__))
180 #define RTK_PORTMASK_ALLPORT_SET(__portmask__) (rtk_switch_logPortMask_get(&__portmask__))
181 #define RTK_PORTMASK_SCAN(__portmask__, __port__) for(__port__ = 0; __port__ < RTK_SWITCH_PORT_NUM; __port__++) if(RTK_PORTMASK_IS_PORT_SET(__portmask__, __port__))
182 #define RTK_PORTMASK_COMPARE(__portmask_A__, __portmask_B__) ((__portmask_A__).bits[0] - (__portmask_B__).bits[0])
184 #define RTK_SCAN_ALL_PHY_PORTMASK(__port__) for(__port__ = 0; __port__ < RTK_SWITCH_PORT_NUM; __port__++) if( (rtk_switch_phyPortMask_get() & (0x00000001 << __port__)))
185 #define RTK_SCAN_ALL_LOG_PORT(__port__) for(__port__ = 0; __port__ < RTK_SWITCH_PORT_NUM; __port__++) if( rtk_switch_logicalPortCheck(__port__) == RT_ERR_OK)
186 #define RTK_SCAN_ALL_LOG_PORTMASK(__portmask__) for((__portmask__).bits[0] = 0; (__portmask__).bits[0] < 0x7FFFF; (__portmask__).bits[0]++) if( rtk_switch_isPortMaskValid(&__portmask__) == RT_ERR_OK)
188 /* Port mask defination */
189 #define RTK_PHY_PORTMASK_ALL (rtk_switch_phyPortMask_get())
192 #define RTK_MAX_LOGICAL_PORT_ID (rtk_switch_maxLogicalPort_get())
203 * RT_ERR_OK - Switch probed
204 * RT_ERR_FAILED - Switch Unprobed.
208 extern rtk_api_ret_t
rtk_switch_probe(switch_chip_t
*pSwitchChip
);
211 * rtk_switch_initialState_set
215 * state - Initial state;
219 * RT_ERR_OK - Initialized
220 * RT_ERR_FAILED - Uninitialized
224 extern rtk_api_ret_t
rtk_switch_initialState_set(init_state_t state
);
227 * rtk_switch_initialState_get
235 * INIT_COMPLETED - Initialized
236 * INIT_NOT_COMPLETED - Uninitialized
240 extern init_state_t
rtk_switch_initialState_get(void);
243 * rtk_switch_logicalPortCheck
245 * Check logical port ID.
247 * logicalPort - logical port ID
251 * RT_ERR_OK - Port ID is correct
252 * RT_ERR_FAILED - Port ID is not correct
253 * RT_ERR_NOT_INIT - Not Initialize
257 extern rtk_api_ret_t
rtk_switch_logicalPortCheck(rtk_port_t logicalPort
);
260 * rtk_switch_isUtpPort
262 * Check is logical port a UTP port
264 * logicalPort - logical port ID
268 * RT_ERR_OK - Port ID is a UTP port
269 * RT_ERR_FAILED - Port ID is not a UTP port
270 * RT_ERR_NOT_INIT - Not Initialize
274 extern rtk_api_ret_t
rtk_switch_isUtpPort(rtk_port_t logicalPort
);
277 * rtk_switch_isExtPort
279 * Check is logical port a Extension port
281 * logicalPort - logical port ID
285 * RT_ERR_OK - Port ID is a EXT port
286 * RT_ERR_FAILED - Port ID is not a EXT port
287 * RT_ERR_NOT_INIT - Not Initialize
291 extern rtk_api_ret_t
rtk_switch_isExtPort(rtk_port_t logicalPort
);
294 * rtk_switch_isHsgPort
296 * Check is logical port a HSG port
298 * logicalPort - logical port ID
302 * RT_ERR_OK - Port ID is a HSG port
303 * RT_ERR_FAILED - Port ID is not a HSG port
304 * RT_ERR_NOT_INIT - Not Initialize
308 extern rtk_api_ret_t
rtk_switch_isHsgPort(rtk_port_t logicalPort
);
311 * rtk_switch_isSgmiiPort
313 * Check is logical port a SGMII port
315 * logicalPort - logical port ID
319 * RT_ERR_OK - Port ID is a SGMII port
320 * RT_ERR_FAILED - Port ID is not a SGMII port
321 * RT_ERR_NOT_INIT - Not Initialize
325 extern rtk_api_ret_t
rtk_switch_isSgmiiPort(rtk_port_t logicalPort
);
328 * rtk_switch_isCPUPort
330 * Check is logical port a CPU port
332 * logicalPort - logical port ID
336 * RT_ERR_OK - Port ID is a CPU port
337 * RT_ERR_FAILED - Port ID is not a CPU port
338 * RT_ERR_NOT_INIT - Not Initialize
342 extern rtk_api_ret_t
rtk_switch_isCPUPort(rtk_port_t logicalPort
);
345 * rtk_switch_isComboPort
347 * Check is logical port a Combo port
349 * logicalPort - logical port ID
353 * RT_ERR_OK - Port ID is a combo port
354 * RT_ERR_FAILED - Port ID is not a combo port
355 * RT_ERR_NOT_INIT - Not Initialize
359 extern rtk_api_ret_t
rtk_switch_isComboPort(rtk_port_t logicalPort
);
362 * rtk_switch_ComboPort_get
370 * Port ID of combo port
374 extern rtk_uint32
rtk_switch_ComboPort_get(void);
377 * rtk_switch_isPtpPort
379 * Check is logical port a PTP port
381 * logicalPort - logical port ID
385 * RT_ERR_OK - Port ID is a PTP port
386 * RT_ERR_FAILED - Port ID is not a PTP port
387 * RT_ERR_NOT_INIT - Not Initialize
391 extern rtk_api_ret_t
rtk_switch_isPtpPort(rtk_port_t logicalPort
);
394 * rtk_switch_port_L2P_get
396 * Get physical port ID
398 * logicalPort - logical port ID
406 extern rtk_uint32
rtk_switch_port_L2P_get(rtk_port_t logicalPort
);
409 * rtk_switch_port_P2L_get
411 * Get logical port ID
413 * physicalPort - physical port ID
421 extern rtk_port_t
rtk_switch_port_P2L_get(rtk_uint32 physicalPort
);
424 * rtk_switch_isPortMaskValid
426 * Check portmask is valid or not
428 * pPmask - logical port mask
432 * RT_ERR_OK - port mask is valid
433 * RT_ERR_FAILED - port mask is not valid
434 * RT_ERR_NOT_INIT - Not Initialize
435 * RT_ERR_NULL_POINTER - Null pointer
439 extern rtk_api_ret_t
rtk_switch_isPortMaskValid(rtk_portmask_t
*pPmask
);
442 * rtk_switch_isPortMaskUtp
444 * Check all ports in portmask are only UTP port
446 * pPmask - logical port mask
450 * RT_ERR_OK - Only UTP port in port mask
451 * RT_ERR_FAILED - Not only UTP port in port mask
452 * RT_ERR_NOT_INIT - Not Initialize
453 * RT_ERR_NULL_POINTER - Null pointer
457 extern rtk_api_ret_t
rtk_switch_isPortMaskUtp(rtk_portmask_t
*pPmask
);
460 * rtk_switch_isPortMaskExt
462 * Check all ports in portmask are only EXT port
464 * pPmask - logical port mask
468 * RT_ERR_OK - Only EXT port in port mask
469 * RT_ERR_FAILED - Not only EXT port in port mask
470 * RT_ERR_NOT_INIT - Not Initialize
471 * RT_ERR_NULL_POINTER - Null pointer
475 extern rtk_api_ret_t
rtk_switch_isPortMaskExt(rtk_portmask_t
*pPmask
);
478 * rtk_switch_portmask_L2P_get
480 * Get physicl portmask from logical portmask
482 * pLogicalPmask - logical port mask
484 * pPhysicalPortmask - physical port mask
487 * RT_ERR_NOT_INIT - Not Initialize
488 * RT_ERR_NULL_POINTER - Null pointer
489 * RT_ERR_PORT_MASK - Error port mask
493 extern rtk_api_ret_t
rtk_switch_portmask_L2P_get(rtk_portmask_t
*pLogicalPmask
, rtk_uint32
*pPhysicalPortmask
);
496 * rtk_switch_portmask_P2L_get
498 * Get logical portmask from physical portmask
500 * physicalPortmask - physical port mask
502 * pLogicalPmask - logical port mask
505 * RT_ERR_NOT_INIT - Not Initialize
506 * RT_ERR_NULL_POINTER - Null pointer
507 * RT_ERR_PORT_MASK - Error port mask
511 extern rtk_api_ret_t
rtk_switch_portmask_P2L_get(rtk_uint32 physicalPortmask
, rtk_portmask_t
*pLogicalPmask
);
514 * rtk_switch_phyPortMask_get
516 * Get physical portmask
522 * 0x00 - Not Initialize
523 * Other value - Physical port mask
527 rtk_uint32
rtk_switch_phyPortMask_get(void);
530 * rtk_switch_logPortMask_get
532 * Get Logical portmask
539 * RT_ERR_NOT_INIT - Not Initialize
540 * RT_ERR_NULL_POINTER - Null pointer
544 rtk_api_ret_t
rtk_switch_logPortMask_get(rtk_portmask_t
*pPortmask
);
549 * Set chip to default configuration enviroment
556 * RT_ERR_FAILED - Failed
557 * RT_ERR_SMI - SMI access error
559 * The API can set chip registers to default configuration for different release chip model.
561 extern rtk_api_ret_t
rtk_switch_init(void);
564 * rtk_switch_portMaxPktLen_set
566 * Set Max packet length
570 * cfgId - Configuration ID
575 * RT_ERR_FAILED - Failed
576 * RT_ERR_SMI - SMI access error
577 * RT_ERR_INPUT - Error Input
580 extern rtk_api_ret_t
rtk_switch_portMaxPktLen_set(rtk_port_t port
, rtk_switch_maxPktLen_linkSpeed_t speed
, rtk_uint32 cfgId
);
583 * rtk_switch_portMaxPktLen_get
585 * Get Max packet length
590 * pCfgId - Configuration ID
593 * RT_ERR_FAILED - Failed
594 * RT_ERR_SMI - SMI access error
595 * RT_ERR_INPUT - Error Input
598 extern rtk_api_ret_t
rtk_switch_portMaxPktLen_get(rtk_port_t port
, rtk_switch_maxPktLen_linkSpeed_t speed
, rtk_uint32
*pCfgId
);
601 * rtk_switch_maxPktLenCfg_set
603 * Set Max packet length configuration
605 * cfgId - Configuration ID
606 * pktLen - Max packet length
611 * RT_ERR_FAILED - Failed
612 * RT_ERR_SMI - SMI access error
613 * RT_ERR_INPUT - Error Input
616 extern rtk_api_ret_t
rtk_switch_maxPktLenCfg_set(rtk_uint32 cfgId
, rtk_uint32 pktLen
);
619 * rtk_switch_maxPktLenCfg_get
621 * Get Max packet length configuration
623 * cfgId - Configuration ID
624 * pPktLen - Max packet length
629 * RT_ERR_FAILED - Failed
630 * RT_ERR_SMI - SMI access error
631 * RT_ERR_INPUT - Error Input
634 extern rtk_api_ret_t
rtk_switch_maxPktLenCfg_get(rtk_uint32 cfgId
, rtk_uint32
*pPktLen
);
637 * rtk_switch_greenEthernet_set
639 * Set all Ports Green Ethernet state.
641 * enable - Green Ethernet state.
646 * RT_ERR_FAILED - Failed
647 * RT_ERR_SMI - SMI access error
648 * RT_ERR_ENABLE - Invalid enable input.
650 * This API can set all Ports Green Ethernet state.
651 * The configuration is as following:
655 extern rtk_api_ret_t
rtk_switch_greenEthernet_set(rtk_enable_t enable
);
658 * rtk_switch_greenEthernet_get
660 * Get all Ports Green Ethernet state.
664 * pEnable - Green Ethernet state.
667 * RT_ERR_FAILED - Failed
668 * RT_ERR_SMI - SMI access error
670 * This API can get Green Ethernet state.
672 extern rtk_api_ret_t
rtk_switch_greenEthernet_get(rtk_enable_t
*pEnable
);
675 * rtk_switch_maxLogicalPort_get
677 * Get Max logical port ID
685 * This API can get max logical port
687 extern rtk_port_t
rtk_switch_maxLogicalPort_get(void);
690 * rtk_switch_maxMeterId_get
698 * 0x00 - Not Initialize
699 * Other value - Max Meter ID
703 extern rtk_uint32
rtk_switch_maxMeterId_get(void);
706 * rtk_switch_maxLutAddrNumber_get
708 * Get Max LUT Address number
714 * 0x00 - Not Initialize
715 * Other value - Max LUT Address number
719 extern rtk_uint32
rtk_switch_maxLutAddrNumber_get(void);
722 * rtk_switch_isValidTrunkGrpId
724 * Check if trunk group is valid or not
730 * RT_ERR_OK - Trunk Group ID is valid
731 * RT_ERR_LA_TRUNK_ID - Trunk Group ID is not valid
735 rtk_uint32
rtk_switch_isValidTrunkGrpId(rtk_uint32 grpId
);