1 // SPDX-License-Identifier: GPL-2.0 2 /* DSA driver for: 3 * Vitesse VSC7385 SparX-G5 5+1-port Integrated Gigabit Ethernet Switch 4 * Vitesse VSC7388 SparX-G8 8-port Integrated Gigabit Ethernet Switch 5 * Vitesse VSC7395 SparX-G5e 5+1-port Integrated Gigabit Ethernet Switch 6 * Vitesse VSC7398 SparX-G8e 8-port Integrated Gigabit Ethernet Switch 7 * 8 * These switches have a built-in 8051 CPU and can download and execute a 9 * firmware in this CPU. They can also be configured to use an external CPU 10 * handling the switch in a memory-mapped manner by connecting to that external 11 * CPU's memory bus. 12 * 13 * Copyright (C) 2018 Linus Wallej <linus.walleij@linaro.org> 14 * Includes portions of code from the firmware uploader by: 15 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org> 16 */ 17 #include <linux/kernel.h> 18 #include <linux/module.h> 19 #include <linux/device.h> 20 #include <linux/iopoll.h> 21 #include <linux/of.h> 22 #include <linux/of_mdio.h> 23 #include <linux/bitops.h> 24 #include <linux/bitfield.h> 25 #include <linux/if_bridge.h> 26 #include <linux/if_vlan.h> 27 #include <linux/etherdevice.h> 28 #include <linux/gpio/consumer.h> 29 #include <linux/gpio/driver.h> 30 #include <linux/dsa/8021q.h> 31 #include <linux/random.h> 32 #include <net/dsa.h> 33 34 #include "vitesse-vsc73xx.h" 35 36 #define VSC73XX_BLOCK_MAC 0x1 /* Subblocks 0-4, 6 (CPU port) */ 37 #define VSC73XX_BLOCK_ANALYZER 0x2 /* Only subblock 0 */ 38 #define VSC73XX_BLOCK_MII 0x3 /* Subblocks 0 and 1 */ 39 #define VSC73XX_BLOCK_MEMINIT 0x3 /* Only subblock 2 */ 40 #define VSC73XX_BLOCK_CAPTURE 0x4 /* Only subblock 2 */ 41 #define VSC73XX_BLOCK_ARBITER 0x5 /* Only subblock 0 */ 42 #define VSC73XX_BLOCK_SYSTEM 0x7 /* Only subblock 0 */ 43 44 /* MII Block subblock */ 45 #define VSC73XX_BLOCK_MII_INTERNAL 0x0 /* Internal MDIO subblock */ 46 #define VSC73XX_BLOCK_MII_EXTERNAL 0x1 /* External MDIO subblock */ 47 48 #define CPU_PORT 6 /* CPU port */ 49 #define VSC73XX_NUM_FDB_ROWS 2048 50 #define VSC73XX_NUM_BUCKETS 4 51 52 /* MAC Block registers */ 53 #define VSC73XX_MAC_CFG 0x00 54 #define VSC73XX_MACHDXGAP 0x02 55 #define VSC73XX_FCCONF 0x04 56 #define VSC73XX_FCMACHI 0x08 57 #define VSC73XX_FCMACLO 0x0c 58 #define VSC73XX_MAXLEN 0x10 59 #define VSC73XX_ADVPORTM 0x19 60 #define VSC73XX_TXUPDCFG 0x24 61 #define VSC73XX_TXQ_SELECT_CFG 0x28 62 #define VSC73XX_RXOCT 0x50 63 #define VSC73XX_TXOCT 0x51 64 #define VSC73XX_C_RX0 0x52 65 #define VSC73XX_C_RX1 0x53 66 #define VSC73XX_C_RX2 0x54 67 #define VSC73XX_C_TX0 0x55 68 #define VSC73XX_C_TX1 0x56 69 #define VSC73XX_C_TX2 0x57 70 #define VSC73XX_C_CFG 0x58 71 #define VSC73XX_CAT_DROP 0x6e 72 #define VSC73XX_CAT_PR_MISC_L2 0x6f 73 #define VSC73XX_CAT_PR_USR_PRIO 0x75 74 #define VSC73XX_CAT_VLAN_MISC 0x79 75 #define VSC73XX_CAT_PORT_VLAN 0x7a 76 #define VSC73XX_Q_MISC_CONF 0xdf 77 78 /* MAC_CFG register bits */ 79 #define VSC73XX_MAC_CFG_WEXC_DIS BIT(31) 80 #define VSC73XX_MAC_CFG_PORT_RST BIT(29) 81 #define VSC73XX_MAC_CFG_TX_EN BIT(28) 82 #define VSC73XX_MAC_CFG_SEED_LOAD BIT(27) 83 #define VSC73XX_MAC_CFG_SEED_MASK GENMASK(26, 19) 84 #define VSC73XX_MAC_CFG_SEED_OFFSET 19 85 #define VSC73XX_MAC_CFG_FDX BIT(18) 86 #define VSC73XX_MAC_CFG_GIGA_MODE BIT(17) 87 #define VSC73XX_MAC_CFG_RX_EN BIT(16) 88 #define VSC73XX_MAC_CFG_VLAN_DBLAWR BIT(15) 89 #define VSC73XX_MAC_CFG_VLAN_AWR BIT(14) 90 #define VSC73XX_MAC_CFG_100_BASE_T BIT(13) /* Not in manual */ 91 #define VSC73XX_MAC_CFG_TX_IPG_MASK GENMASK(10, 6) 92 #define VSC73XX_MAC_CFG_TX_IPG_OFFSET 6 93 #define VSC73XX_MAC_CFG_TX_IPG_1000M (6 << VSC73XX_MAC_CFG_TX_IPG_OFFSET) 94 #define VSC73XX_MAC_CFG_TX_IPG_100_10M (17 << VSC73XX_MAC_CFG_TX_IPG_OFFSET) 95 #define VSC73XX_MAC_CFG_MAC_RX_RST BIT(5) 96 #define VSC73XX_MAC_CFG_MAC_TX_RST BIT(4) 97 #define VSC73XX_MAC_CFG_CLK_SEL_MASK GENMASK(2, 0) 98 #define VSC73XX_MAC_CFG_CLK_SEL_OFFSET 0 99 #define VSC73XX_MAC_CFG_CLK_SEL_1000M 1 100 #define VSC73XX_MAC_CFG_CLK_SEL_100M 2 101 #define VSC73XX_MAC_CFG_CLK_SEL_10M 3 102 #define VSC73XX_MAC_CFG_CLK_SEL_EXT 4 103 104 #define VSC73XX_MAC_CFG_1000M_F_PHY (VSC73XX_MAC_CFG_FDX | \ 105 VSC73XX_MAC_CFG_GIGA_MODE | \ 106 VSC73XX_MAC_CFG_TX_IPG_1000M | \ 107 VSC73XX_MAC_CFG_CLK_SEL_EXT) 108 #define VSC73XX_MAC_CFG_100_10M_F_PHY (VSC73XX_MAC_CFG_FDX | \ 109 VSC73XX_MAC_CFG_TX_IPG_100_10M | \ 110 VSC73XX_MAC_CFG_CLK_SEL_EXT) 111 #define VSC73XX_MAC_CFG_100_10M_H_PHY (VSC73XX_MAC_CFG_TX_IPG_100_10M | \ 112 VSC73XX_MAC_CFG_CLK_SEL_EXT) 113 #define VSC73XX_MAC_CFG_1000M_F_RGMII (VSC73XX_MAC_CFG_FDX | \ 114 VSC73XX_MAC_CFG_GIGA_MODE | \ 115 VSC73XX_MAC_CFG_TX_IPG_1000M | \ 116 VSC73XX_MAC_CFG_CLK_SEL_1000M) 117 #define VSC73XX_MAC_CFG_RESET (VSC73XX_MAC_CFG_PORT_RST | \ 118 VSC73XX_MAC_CFG_MAC_RX_RST | \ 119 VSC73XX_MAC_CFG_MAC_TX_RST) 120 121 /* Flow control register bits */ 122 #define VSC73XX_FCCONF_ZERO_PAUSE_EN BIT(17) 123 #define VSC73XX_FCCONF_FLOW_CTRL_OBEY BIT(16) 124 #define VSC73XX_FCCONF_PAUSE_VAL_MASK GENMASK(15, 0) 125 126 /* ADVPORTM advanced port setup register bits */ 127 #define VSC73XX_ADVPORTM_IFG_PPM BIT(7) 128 #define VSC73XX_ADVPORTM_EXC_COL_CONT BIT(6) 129 #define VSC73XX_ADVPORTM_EXT_PORT BIT(5) 130 #define VSC73XX_ADVPORTM_INV_GTX BIT(4) 131 #define VSC73XX_ADVPORTM_ENA_GTX BIT(3) 132 #define VSC73XX_ADVPORTM_DDR_MODE BIT(2) 133 #define VSC73XX_ADVPORTM_IO_LOOPBACK BIT(1) 134 #define VSC73XX_ADVPORTM_HOST_LOOPBACK BIT(0) 135 136 /* TXUPDCFG transmit modify setup bits */ 137 #define VSC73XX_TXUPDCFG_DSCP_REWR_MODE GENMASK(20, 19) 138 #define VSC73XX_TXUPDCFG_DSCP_REWR_ENA BIT(18) 139 #define VSC73XX_TXUPDCFG_TX_INT_TO_USRPRIO_ENA BIT(17) 140 #define VSC73XX_TXUPDCFG_TX_UNTAGGED_VID GENMASK(15, 4) 141 #define VSC73XX_TXUPDCFG_TX_UNTAGGED_VID_ENA BIT(3) 142 #define VSC73XX_TXUPDCFG_TX_UPDATE_CRC_CPU_ENA BIT(1) 143 #define VSC73XX_TXUPDCFG_TX_INSERT_TAG BIT(0) 144 145 #define VSC73XX_TXUPDCFG_TX_UNTAGGED_VID_SHIFT 4 146 147 /* CAT_DROP categorizer frame dropping register bits */ 148 #define VSC73XX_CAT_DROP_DROP_MC_SMAC_ENA BIT(6) 149 #define VSC73XX_CAT_DROP_FWD_CTRL_ENA BIT(4) 150 #define VSC73XX_CAT_DROP_FWD_PAUSE_ENA BIT(3) 151 #define VSC73XX_CAT_DROP_UNTAGGED_ENA BIT(2) 152 #define VSC73XX_CAT_DROP_TAGGED_ENA BIT(1) 153 #define VSC73XX_CAT_DROP_NULL_MAC_ENA BIT(0) 154 155 #define VSC73XX_Q_MISC_CONF_EXTENT_MEM BIT(31) 156 #define VSC73XX_Q_MISC_CONF_EARLY_TX_MASK GENMASK(4, 1) 157 #define VSC73XX_Q_MISC_CONF_EARLY_TX_512 (1 << 1) 158 #define VSC73XX_Q_MISC_CONF_MAC_PAUSE_MODE BIT(0) 159 160 /* CAT_VLAN_MISC categorizer VLAN miscellaneous bits */ 161 #define VSC73XX_CAT_VLAN_MISC_VLAN_TCI_IGNORE_ENA BIT(8) 162 #define VSC73XX_CAT_VLAN_MISC_VLAN_KEEP_TAG_ENA BIT(7) 163 164 /* CAT_PORT_VLAN categorizer port VLAN */ 165 #define VSC73XX_CAT_PORT_VLAN_VLAN_CFI BIT(15) 166 #define VSC73XX_CAT_PORT_VLAN_VLAN_USR_PRIO GENMASK(14, 12) 167 #define VSC73XX_CAT_PORT_VLAN_VLAN_VID GENMASK(11, 0) 168 169 /* Frame analyzer block 2 registers */ 170 #define VSC73XX_STORMLIMIT 0x02 171 #define VSC73XX_ADVLEARN 0x03 172 #define VSC73XX_IFLODMSK 0x04 173 #define VSC73XX_VLANMASK 0x05 174 #define VSC73XX_MACHDATA 0x06 175 #define VSC73XX_MACLDATA 0x07 176 #define VSC73XX_ANMOVED 0x08 177 #define VSC73XX_ANAGEFIL 0x09 178 #define VSC73XX_ANEVENTS 0x0a 179 #define VSC73XX_ANCNTMASK 0x0b 180 #define VSC73XX_ANCNTVAL 0x0c 181 #define VSC73XX_LEARNMASK 0x0d 182 #define VSC73XX_UFLODMASK 0x0e 183 #define VSC73XX_MFLODMASK 0x0f 184 #define VSC73XX_RECVMASK 0x10 185 #define VSC73XX_AGGRCTRL 0x20 186 #define VSC73XX_AGGRMSKS 0x30 /* Until 0x3f */ 187 #define VSC73XX_DSTMASKS 0x40 /* Until 0x7f */ 188 #define VSC73XX_SRCMASKS 0x80 /* Until 0x87 */ 189 #define VSC73XX_CAPENAB 0xa0 190 #define VSC73XX_MACACCESS 0xb0 191 #define VSC73XX_IPMCACCESS 0xb1 192 #define VSC73XX_MACTINDX 0xc0 193 #define VSC73XX_VLANACCESS 0xd0 194 #define VSC73XX_VLANTIDX 0xe0 195 #define VSC73XX_AGENCTRL 0xf0 196 #define VSC73XX_CAPRST 0xff 197 198 #define VSC73XX_SRCMASKS_CPU_COPY BIT(27) 199 #define VSC73XX_SRCMASKS_MIRROR BIT(26) 200 #define VSC73XX_SRCMASKS_PORTS_MASK GENMASK(7, 0) 201 202 #define VSC73XX_MACHDATA_VID GENMASK(27, 16) 203 #define VSC73XX_MACHDATA_MAC0 GENMASK(15, 8) 204 #define VSC73XX_MACHDATA_MAC1 GENMASK(7, 0) 205 #define VSC73XX_MACLDATA_MAC2 GENMASK(31, 24) 206 #define VSC73XX_MACLDATA_MAC3 GENMASK(23, 16) 207 #define VSC73XX_MACLDATA_MAC4 GENMASK(15, 8) 208 #define VSC73XX_MACLDATA_MAC5 GENMASK(7, 0) 209 210 #define VSC73XX_HASH0_VID_FROM_MASK GENMASK(5, 0) 211 #define VSC73XX_HASH0_MAC0_FROM_MASK GENMASK(7, 4) 212 #define VSC73XX_HASH1_MAC0_FROM_MASK GENMASK(3, 0) 213 #define VSC73XX_HASH1_MAC1_FROM_MASK GENMASK(7, 1) 214 #define VSC73XX_HASH2_MAC1_FROM_MASK BIT(0) 215 #define VSC73XX_HASH2_MAC2_FROM_MASK GENMASK(7, 0) 216 #define VSC73XX_HASH2_MAC3_FROM_MASK GENMASK(7, 6) 217 #define VSC73XX_HASH3_MAC3_FROM_MASK GENMASK(5, 0) 218 #define VSC73XX_HASH3_MAC4_FROM_MASK GENMASK(7, 3) 219 #define VSC73XX_HASH4_MAC4_FROM_MASK GENMASK(2, 0) 220 221 #define VSC73XX_HASH0_VID_TO_MASK GENMASK(9, 4) 222 #define VSC73XX_HASH0_MAC0_TO_MASK GENMASK(3, 0) 223 #define VSC73XX_HASH1_MAC0_TO_MASK GENMASK(10, 7) 224 #define VSC73XX_HASH1_MAC1_TO_MASK GENMASK(6, 0) 225 #define VSC73XX_HASH2_MAC1_TO_MASK BIT(10) 226 #define VSC73XX_HASH2_MAC2_TO_MASK GENMASK(9, 2) 227 #define VSC73XX_HASH2_MAC3_TO_MASK GENMASK(1, 0) 228 #define VSC73XX_HASH3_MAC3_TO_MASK GENMASK(10, 5) 229 #define VSC73XX_HASH3_MAC4_TO_MASK GENMASK(4, 0) 230 #define VSC73XX_HASH4_MAC4_TO_MASK GENMASK(10, 8) 231 232 #define VSC73XX_MACTINDX_SHADOW BIT(13) 233 #define VSC73XX_MACTINDX_BUCKET_MSK GENMASK(12, 11) 234 #define VSC73XX_MACTINDX_INDEX_MSK GENMASK(10, 0) 235 236 #define VSC73XX_MACACCESS_CPU_COPY BIT(14) 237 #define VSC73XX_MACACCESS_FWD_KILL BIT(13) 238 #define VSC73XX_MACACCESS_IGNORE_VLAN BIT(12) 239 #define VSC73XX_MACACCESS_AGED_FLAG BIT(11) 240 #define VSC73XX_MACACCESS_VALID BIT(10) 241 #define VSC73XX_MACACCESS_LOCKED BIT(9) 242 #define VSC73XX_MACACCESS_DEST_IDX_MASK GENMASK(8, 3) 243 #define VSC73XX_MACACCESS_CMD_MASK GENMASK(2, 0) 244 #define VSC73XX_MACACCESS_CMD_IDLE 0 245 #define VSC73XX_MACACCESS_CMD_LEARN 1 246 #define VSC73XX_MACACCESS_CMD_FORGET 2 247 #define VSC73XX_MACACCESS_CMD_AGE_TABLE 3 248 #define VSC73XX_MACACCESS_CMD_FLUSH_TABLE 4 249 #define VSC73XX_MACACCESS_CMD_CLEAR_TABLE 5 250 #define VSC73XX_MACACCESS_CMD_READ_ENTRY 6 251 #define VSC73XX_MACACCESS_CMD_WRITE_ENTRY 7 252 253 #define VSC73XX_VLANACCESS_LEARN_DISABLED BIT(30) 254 #define VSC73XX_VLANACCESS_VLAN_MIRROR BIT(29) 255 #define VSC73XX_VLANACCESS_VLAN_SRC_CHECK BIT(28) 256 #define VSC73XX_VLANACCESS_VLAN_PORT_MASK GENMASK(9, 2) 257 #define VSC73XX_VLANACCESS_VLAN_PORT_MASK_SHIFT 2 258 #define VSC73XX_VLANACCESS_VLAN_TBL_CMD_MASK GENMASK(1, 0) 259 #define VSC73XX_VLANACCESS_VLAN_TBL_CMD_IDLE 0 260 #define VSC73XX_VLANACCESS_VLAN_TBL_CMD_READ_ENTRY 1 261 #define VSC73XX_VLANACCESS_VLAN_TBL_CMD_WRITE_ENTRY 2 262 #define VSC73XX_VLANACCESS_VLAN_TBL_CMD_CLEAR_TABLE 3 263 264 /* MII block 3 registers */ 265 #define VSC73XX_MII_STAT 0x0 266 #define VSC73XX_MII_CMD 0x1 267 #define VSC73XX_MII_DATA 0x2 268 #define VSC73XX_MII_MPRES 0x3 269 270 #define VSC73XX_MII_STAT_BUSY BIT(3) 271 #define VSC73XX_MII_STAT_READ BIT(2) 272 #define VSC73XX_MII_STAT_WRITE BIT(1) 273 274 #define VSC73XX_MII_CMD_SCAN BIT(27) 275 #define VSC73XX_MII_CMD_OPERATION BIT(26) 276 #define VSC73XX_MII_CMD_PHY_ADDR GENMASK(25, 21) 277 #define VSC73XX_MII_CMD_PHY_REG GENMASK(20, 16) 278 #define VSC73XX_MII_CMD_WRITE_DATA GENMASK(15, 0) 279 280 #define VSC73XX_MII_DATA_FAILURE BIT(16) 281 #define VSC73XX_MII_DATA_READ_DATA GENMASK(15, 0) 282 283 #define VSC73XX_MII_MPRES_NOPREAMBLE BIT(6) 284 #define VSC73XX_MII_MPRES_PRESCALEVAL GENMASK(5, 0) 285 #define VSC73XX_MII_PRESCALEVAL_MIN 3 /* min allowed mdio clock prescaler */ 286 287 #define VSC73XX_MII_STAT_BUSY BIT(3) 288 289 /* Arbiter block 5 registers */ 290 #define VSC73XX_ARBEMPTY 0x0c 291 #define VSC73XX_ARBDISC 0x0e 292 #define VSC73XX_SBACKWDROP 0x12 293 #define VSC73XX_DBACKWDROP 0x13 294 #define VSC73XX_ARBBURSTPROB 0x15 295 296 /* System block 7 registers */ 297 #define VSC73XX_ICPU_SIPAD 0x01 298 #define VSC73XX_GMIIDELAY 0x05 299 #define VSC73XX_ICPU_CTRL 0x10 300 #define VSC73XX_ICPU_ADDR 0x11 301 #define VSC73XX_ICPU_SRAM 0x12 302 #define VSC73XX_HWSEM 0x13 303 #define VSC73XX_GLORESET 0x14 304 #define VSC73XX_ICPU_MBOX_VAL 0x15 305 #define VSC73XX_ICPU_MBOX_SET 0x16 306 #define VSC73XX_ICPU_MBOX_CLR 0x17 307 #define VSC73XX_CHIPID 0x18 308 #define VSC73XX_GPIO 0x34 309 310 #define VSC73XX_GMIIDELAY_GMII0_GTXDELAY_NONE 0 311 #define VSC73XX_GMIIDELAY_GMII0_GTXDELAY_1_4_NS 1 312 #define VSC73XX_GMIIDELAY_GMII0_GTXDELAY_1_7_NS 2 313 #define VSC73XX_GMIIDELAY_GMII0_GTXDELAY_2_0_NS 3 314 315 #define VSC73XX_GMIIDELAY_GMII0_RXDELAY_NONE (0 << 4) 316 #define VSC73XX_GMIIDELAY_GMII0_RXDELAY_1_4_NS (1 << 4) 317 #define VSC73XX_GMIIDELAY_GMII0_RXDELAY_1_7_NS (2 << 4) 318 #define VSC73XX_GMIIDELAY_GMII0_RXDELAY_2_0_NS (3 << 4) 319 320 #define VSC73XX_ICPU_CTRL_WATCHDOG_RST BIT(31) 321 #define VSC73XX_ICPU_CTRL_CLK_DIV_MASK GENMASK(12, 8) 322 #define VSC73XX_ICPU_CTRL_SRST_HOLD BIT(7) 323 #define VSC73XX_ICPU_CTRL_ICPU_PI_EN BIT(6) 324 #define VSC73XX_ICPU_CTRL_BOOT_EN BIT(3) 325 #define VSC73XX_ICPU_CTRL_EXT_ACC_EN BIT(2) 326 #define VSC73XX_ICPU_CTRL_CLK_EN BIT(1) 327 #define VSC73XX_ICPU_CTRL_SRST BIT(0) 328 329 #define VSC73XX_CHIPID_ID_SHIFT 12 330 #define VSC73XX_CHIPID_ID_MASK 0xffff 331 #define VSC73XX_CHIPID_REV_SHIFT 28 332 #define VSC73XX_CHIPID_REV_MASK 0xf 333 #define VSC73XX_CHIPID_ID_7385 0x7385 334 #define VSC73XX_CHIPID_ID_7388 0x7388 335 #define VSC73XX_CHIPID_ID_7395 0x7395 336 #define VSC73XX_CHIPID_ID_7398 0x7398 337 338 #define VSC73XX_GLORESET_STROBE BIT(4) 339 #define VSC73XX_GLORESET_ICPU_LOCK BIT(3) 340 #define VSC73XX_GLORESET_MEM_LOCK BIT(2) 341 #define VSC73XX_GLORESET_PHY_RESET BIT(1) 342 #define VSC73XX_GLORESET_MASTER_RESET BIT(0) 343 344 #define VSC7385_CLOCK_DELAY ((3 << 4) | 3) 345 #define VSC7385_CLOCK_DELAY_MASK ((3 << 4) | 3) 346 347 #define VSC73XX_ICPU_CTRL_STOP (VSC73XX_ICPU_CTRL_SRST_HOLD | \ 348 VSC73XX_ICPU_CTRL_BOOT_EN | \ 349 VSC73XX_ICPU_CTRL_EXT_ACC_EN) 350 351 #define VSC73XX_ICPU_CTRL_START (VSC73XX_ICPU_CTRL_CLK_DIV | \ 352 VSC73XX_ICPU_CTRL_BOOT_EN | \ 353 VSC73XX_ICPU_CTRL_CLK_EN | \ 354 VSC73XX_ICPU_CTRL_SRST) 355 356 #define IS_7385(a) ((a)->chipid == VSC73XX_CHIPID_ID_7385) 357 #define IS_7388(a) ((a)->chipid == VSC73XX_CHIPID_ID_7388) 358 #define IS_7395(a) ((a)->chipid == VSC73XX_CHIPID_ID_7395) 359 #define IS_7398(a) ((a)->chipid == VSC73XX_CHIPID_ID_7398) 360 #define IS_739X(a) (IS_7395(a) || IS_7398(a)) 361 362 #define VSC73XX_POLL_SLEEP_US 1000 363 #define VSC73XX_MDIO_POLL_SLEEP_US 5 364 #define VSC73XX_POLL_TIMEOUT_US 10000 365 366 struct vsc73xx_counter { 367 u8 counter; 368 const char *name; 369 }; 370 371 struct vsc73xx_fdb { 372 u16 vid; 373 u8 port; 374 u8 mac[ETH_ALEN]; 375 bool valid; 376 }; 377 378 /* Counters are named according to the MIB standards where applicable. 379 * Some counters are custom, non-standard. The standard counters are 380 * named in accordance with RFC2819, RFC2021 and IEEE Std 802.3-2002 Annex 381 * 30A Counters. 382 */ 383 static const struct vsc73xx_counter vsc73xx_rx_counters[] = { 384 { 0, "RxEtherStatsPkts" }, 385 { 1, "RxBroadcast+MulticastPkts" }, /* non-standard counter */ 386 { 2, "RxTotalErrorPackets" }, /* non-standard counter */ 387 { 3, "RxEtherStatsBroadcastPkts" }, 388 { 4, "RxEtherStatsMulticastPkts" }, 389 { 5, "RxEtherStatsPkts64Octets" }, 390 { 6, "RxEtherStatsPkts65to127Octets" }, 391 { 7, "RxEtherStatsPkts128to255Octets" }, 392 { 8, "RxEtherStatsPkts256to511Octets" }, 393 { 9, "RxEtherStatsPkts512to1023Octets" }, 394 { 10, "RxEtherStatsPkts1024to1518Octets" }, 395 { 11, "RxJumboFrames" }, /* non-standard counter */ 396 { 12, "RxaPauseMACControlFramesTransmitted" }, 397 { 13, "RxFIFODrops" }, /* non-standard counter */ 398 { 14, "RxBackwardDrops" }, /* non-standard counter */ 399 { 15, "RxClassifierDrops" }, /* non-standard counter */ 400 { 16, "RxEtherStatsCRCAlignErrors" }, 401 { 17, "RxEtherStatsUndersizePkts" }, 402 { 18, "RxEtherStatsOversizePkts" }, 403 { 19, "RxEtherStatsFragments" }, 404 { 20, "RxEtherStatsJabbers" }, 405 { 21, "RxaMACControlFramesReceived" }, 406 /* 22-24 are undefined */ 407 { 25, "RxaFramesReceivedOK" }, 408 { 26, "RxQoSClass0" }, /* non-standard counter */ 409 { 27, "RxQoSClass1" }, /* non-standard counter */ 410 { 28, "RxQoSClass2" }, /* non-standard counter */ 411 { 29, "RxQoSClass3" }, /* non-standard counter */ 412 }; 413 414 static const struct vsc73xx_counter vsc73xx_tx_counters[] = { 415 { 0, "TxEtherStatsPkts" }, 416 { 1, "TxBroadcast+MulticastPkts" }, /* non-standard counter */ 417 { 2, "TxTotalErrorPackets" }, /* non-standard counter */ 418 { 3, "TxEtherStatsBroadcastPkts" }, 419 { 4, "TxEtherStatsMulticastPkts" }, 420 { 5, "TxEtherStatsPkts64Octets" }, 421 { 6, "TxEtherStatsPkts65to127Octets" }, 422 { 7, "TxEtherStatsPkts128to255Octets" }, 423 { 8, "TxEtherStatsPkts256to511Octets" }, 424 { 9, "TxEtherStatsPkts512to1023Octets" }, 425 { 10, "TxEtherStatsPkts1024to1518Octets" }, 426 { 11, "TxJumboFrames" }, /* non-standard counter */ 427 { 12, "TxaPauseMACControlFramesTransmitted" }, 428 { 13, "TxFIFODrops" }, /* non-standard counter */ 429 { 14, "TxDrops" }, /* non-standard counter */ 430 { 15, "TxEtherStatsCollisions" }, 431 { 16, "TxEtherStatsCRCAlignErrors" }, 432 { 17, "TxEtherStatsUndersizePkts" }, 433 { 18, "TxEtherStatsOversizePkts" }, 434 { 19, "TxEtherStatsFragments" }, 435 { 20, "TxEtherStatsJabbers" }, 436 /* 21-24 are undefined */ 437 { 25, "TxaFramesReceivedOK" }, 438 { 26, "TxQoSClass0" }, /* non-standard counter */ 439 { 27, "TxQoSClass1" }, /* non-standard counter */ 440 { 28, "TxQoSClass2" }, /* non-standard counter */ 441 { 29, "TxQoSClass3" }, /* non-standard counter */ 442 }; 443 444 struct vsc73xx_vlan_summary { 445 size_t num_tagged; 446 size_t num_untagged; 447 }; 448 449 enum vsc73xx_port_vlan_conf { 450 VSC73XX_VLAN_FILTER, 451 VSC73XX_VLAN_FILTER_UNTAG_ALL, 452 VSC73XX_VLAN_IGNORE, 453 }; 454 455 int vsc73xx_is_addr_valid(u8 block, u8 subblock) 456 { 457 switch (block) { 458 case VSC73XX_BLOCK_MAC: 459 switch (subblock) { 460 case 0 ... 4: 461 case 6: 462 return 1; 463 } 464 break; 465 466 case VSC73XX_BLOCK_ANALYZER: 467 case VSC73XX_BLOCK_SYSTEM: 468 switch (subblock) { 469 case 0: 470 return 1; 471 } 472 break; 473 474 case VSC73XX_BLOCK_MII: 475 case VSC73XX_BLOCK_CAPTURE: 476 case VSC73XX_BLOCK_ARBITER: 477 switch (subblock) { 478 case 0 ... 1: 479 return 1; 480 } 481 break; 482 } 483 484 return 0; 485 } 486 EXPORT_SYMBOL(vsc73xx_is_addr_valid); 487 488 static int vsc73xx_read(struct vsc73xx *vsc, u8 block, u8 subblock, u8 reg, 489 u32 *val) 490 { 491 return vsc->ops->read(vsc, block, subblock, reg, val); 492 } 493 494 static int vsc73xx_write(struct vsc73xx *vsc, u8 block, u8 subblock, u8 reg, 495 u32 val) 496 { 497 return vsc->ops->write(vsc, block, subblock, reg, val); 498 } 499 500 static int vsc73xx_update_bits(struct vsc73xx *vsc, u8 block, u8 subblock, 501 u8 reg, u32 mask, u32 val) 502 { 503 u32 tmp, orig; 504 int ret; 505 506 /* Same read-modify-write algorithm as e.g. regmap */ 507 ret = vsc73xx_read(vsc, block, subblock, reg, &orig); 508 if (ret) 509 return ret; 510 tmp = orig & ~mask; 511 tmp |= val & mask; 512 return vsc73xx_write(vsc, block, subblock, reg, tmp); 513 } 514 515 static int vsc73xx_detect(struct vsc73xx *vsc) 516 { 517 bool icpu_si_boot_en; 518 bool icpu_pi_en; 519 u32 val; 520 u32 rev; 521 int ret; 522 u32 id; 523 524 ret = vsc73xx_read(vsc, VSC73XX_BLOCK_SYSTEM, 0, 525 VSC73XX_ICPU_MBOX_VAL, &val); 526 if (ret) { 527 dev_err(vsc->dev, "unable to read mailbox (%d)\n", ret); 528 return ret; 529 } 530 531 if (val == 0xffffffff) { 532 dev_info(vsc->dev, "chip seems dead.\n"); 533 return -EAGAIN; 534 } 535 536 ret = vsc73xx_read(vsc, VSC73XX_BLOCK_SYSTEM, 0, 537 VSC73XX_CHIPID, &val); 538 if (ret) { 539 dev_err(vsc->dev, "unable to read chip id (%d)\n", ret); 540 return ret; 541 } 542 543 id = (val >> VSC73XX_CHIPID_ID_SHIFT) & 544 VSC73XX_CHIPID_ID_MASK; 545 switch (id) { 546 case VSC73XX_CHIPID_ID_7385: 547 case VSC73XX_CHIPID_ID_7388: 548 case VSC73XX_CHIPID_ID_7395: 549 case VSC73XX_CHIPID_ID_7398: 550 break; 551 default: 552 dev_err(vsc->dev, "unsupported chip, id=%04x\n", id); 553 return -ENODEV; 554 } 555 556 vsc->chipid = id; 557 rev = (val >> VSC73XX_CHIPID_REV_SHIFT) & 558 VSC73XX_CHIPID_REV_MASK; 559 dev_info(vsc->dev, "VSC%04X (rev: %d) switch found\n", id, rev); 560 561 ret = vsc73xx_read(vsc, VSC73XX_BLOCK_SYSTEM, 0, 562 VSC73XX_ICPU_CTRL, &val); 563 if (ret) { 564 dev_err(vsc->dev, "unable to read iCPU control\n"); 565 return ret; 566 } 567 568 /* The iCPU can always be used but can boot in different ways. 569 * If it is initially disabled and has no external memory, 570 * we are in control and can do whatever we like, else we 571 * are probably in trouble (we need some way to communicate 572 * with the running firmware) so we bail out for now. 573 */ 574 icpu_pi_en = !!(val & VSC73XX_ICPU_CTRL_ICPU_PI_EN); 575 icpu_si_boot_en = !!(val & VSC73XX_ICPU_CTRL_BOOT_EN); 576 if (icpu_si_boot_en && icpu_pi_en) { 577 dev_err(vsc->dev, 578 "iCPU enabled boots from SI, has external memory\n"); 579 dev_err(vsc->dev, "no idea how to deal with this\n"); 580 return -ENODEV; 581 } 582 if (icpu_si_boot_en && !icpu_pi_en) { 583 dev_err(vsc->dev, 584 "iCPU enabled boots from PI/SI, no external memory\n"); 585 return -EAGAIN; 586 } 587 if (!icpu_si_boot_en && icpu_pi_en) { 588 dev_err(vsc->dev, 589 "iCPU enabled, boots from PI external memory\n"); 590 dev_err(vsc->dev, "no idea how to deal with this\n"); 591 return -ENODEV; 592 } 593 /* !icpu_si_boot_en && !cpu_pi_en */ 594 dev_info(vsc->dev, "iCPU disabled, no external memory\n"); 595 596 return 0; 597 } 598 599 static int vsc73xx_mdio_busy_check(struct vsc73xx *vsc) 600 { 601 int ret, err; 602 u32 val; 603 604 ret = read_poll_timeout(vsc73xx_read, err, 605 err < 0 || !(val & VSC73XX_MII_STAT_BUSY), 606 VSC73XX_MDIO_POLL_SLEEP_US, 607 VSC73XX_POLL_TIMEOUT_US, false, vsc, 608 VSC73XX_BLOCK_MII, VSC73XX_BLOCK_MII_INTERNAL, 609 VSC73XX_MII_STAT, &val); 610 if (ret) 611 return ret; 612 return err; 613 } 614 615 static int vsc73xx_phy_read(struct dsa_switch *ds, int phy, int regnum) 616 { 617 struct vsc73xx *vsc = ds->priv; 618 u32 cmd; 619 u32 val; 620 int ret; 621 622 ret = vsc73xx_mdio_busy_check(vsc); 623 if (ret) 624 return ret; 625 626 /* Setting bit 26 means "read" */ 627 cmd = VSC73XX_MII_CMD_OPERATION | 628 FIELD_PREP(VSC73XX_MII_CMD_PHY_ADDR, phy) | 629 FIELD_PREP(VSC73XX_MII_CMD_PHY_REG, regnum); 630 ret = vsc73xx_write(vsc, VSC73XX_BLOCK_MII, VSC73XX_BLOCK_MII_INTERNAL, 631 VSC73XX_MII_CMD, cmd); 632 if (ret) 633 return ret; 634 635 ret = vsc73xx_mdio_busy_check(vsc); 636 if (ret) 637 return ret; 638 639 ret = vsc73xx_read(vsc, VSC73XX_BLOCK_MII, VSC73XX_BLOCK_MII_INTERNAL, 640 VSC73XX_MII_DATA, &val); 641 if (ret) 642 return ret; 643 if (val & VSC73XX_MII_DATA_FAILURE) { 644 dev_err(vsc->dev, "reading reg %02x from phy%d failed\n", 645 regnum, phy); 646 return -EIO; 647 } 648 val &= VSC73XX_MII_DATA_READ_DATA; 649 650 dev_dbg(vsc->dev, "read reg %02x from phy%d = %04x\n", 651 regnum, phy, val); 652 653 return val; 654 } 655 656 static int vsc73xx_phy_write(struct dsa_switch *ds, int phy, int regnum, 657 u16 val) 658 { 659 struct vsc73xx *vsc = ds->priv; 660 u32 cmd; 661 int ret; 662 663 ret = vsc73xx_mdio_busy_check(vsc); 664 if (ret) 665 return ret; 666 667 cmd = FIELD_PREP(VSC73XX_MII_CMD_PHY_ADDR, phy) | 668 FIELD_PREP(VSC73XX_MII_CMD_PHY_REG, regnum) | 669 FIELD_PREP(VSC73XX_MII_CMD_WRITE_DATA, val); 670 ret = vsc73xx_write(vsc, VSC73XX_BLOCK_MII, VSC73XX_BLOCK_MII_INTERNAL, 671 VSC73XX_MII_CMD, cmd); 672 if (ret) 673 return ret; 674 675 dev_dbg(vsc->dev, "write %04x to reg %02x in phy%d\n", 676 val, regnum, phy); 677 return 0; 678 } 679 680 static enum dsa_tag_protocol vsc73xx_get_tag_protocol(struct dsa_switch *ds, 681 int port, 682 enum dsa_tag_protocol mp) 683 { 684 /* The switch internally uses a 8 byte header with length, 685 * source port, tag, LPA and priority. This is supposedly 686 * only accessible when operating the switch using the internal 687 * CPU or with an external CPU mapping the device in, but not 688 * when operating the switch over SPI and putting frames in/out 689 * on port 6 (the CPU port). So far we must assume that we 690 * cannot access the tag. (See "Internal frame header" section 691 * 3.9.1 in the manual.) 692 */ 693 return DSA_TAG_PROTO_VSC73XX_8021Q; 694 } 695 696 static int vsc73xx_wait_for_vlan_table_cmd(struct vsc73xx *vsc) 697 { 698 int ret, err; 699 u32 val; 700 701 ret = read_poll_timeout(vsc73xx_read, err, 702 err < 0 || 703 ((val & VSC73XX_VLANACCESS_VLAN_TBL_CMD_MASK) == 704 VSC73XX_VLANACCESS_VLAN_TBL_CMD_IDLE), 705 VSC73XX_POLL_SLEEP_US, VSC73XX_POLL_TIMEOUT_US, 706 false, vsc, VSC73XX_BLOCK_ANALYZER, 707 0, VSC73XX_VLANACCESS, &val); 708 if (ret) 709 return ret; 710 return err; 711 } 712 713 static int 714 vsc73xx_read_vlan_table_entry(struct vsc73xx *vsc, u16 vid, u8 *portmap) 715 { 716 u32 val; 717 int ret; 718 719 vsc73xx_write(vsc, VSC73XX_BLOCK_ANALYZER, 0, VSC73XX_VLANTIDX, vid); 720 721 ret = vsc73xx_wait_for_vlan_table_cmd(vsc); 722 if (ret) 723 return ret; 724 725 vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ANALYZER, 0, VSC73XX_VLANACCESS, 726 VSC73XX_VLANACCESS_VLAN_TBL_CMD_MASK, 727 VSC73XX_VLANACCESS_VLAN_TBL_CMD_READ_ENTRY); 728 729 ret = vsc73xx_wait_for_vlan_table_cmd(vsc); 730 if (ret) 731 return ret; 732 733 vsc73xx_read(vsc, VSC73XX_BLOCK_ANALYZER, 0, VSC73XX_VLANACCESS, &val); 734 *portmap = (val & VSC73XX_VLANACCESS_VLAN_PORT_MASK) >> 735 VSC73XX_VLANACCESS_VLAN_PORT_MASK_SHIFT; 736 737 return 0; 738 } 739 740 static int 741 vsc73xx_write_vlan_table_entry(struct vsc73xx *vsc, u16 vid, u8 portmap) 742 { 743 int ret; 744 745 vsc73xx_write(vsc, VSC73XX_BLOCK_ANALYZER, 0, VSC73XX_VLANTIDX, vid); 746 747 ret = vsc73xx_wait_for_vlan_table_cmd(vsc); 748 if (ret) 749 return ret; 750 751 vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ANALYZER, 0, VSC73XX_VLANACCESS, 752 VSC73XX_VLANACCESS_VLAN_TBL_CMD_MASK | 753 VSC73XX_VLANACCESS_VLAN_SRC_CHECK | 754 VSC73XX_VLANACCESS_VLAN_PORT_MASK, 755 VSC73XX_VLANACCESS_VLAN_TBL_CMD_WRITE_ENTRY | 756 VSC73XX_VLANACCESS_VLAN_SRC_CHECK | 757 (portmap << VSC73XX_VLANACCESS_VLAN_PORT_MASK_SHIFT)); 758 759 return vsc73xx_wait_for_vlan_table_cmd(vsc); 760 } 761 762 static int 763 vsc73xx_update_vlan_table(struct vsc73xx *vsc, int port, u16 vid, bool set) 764 { 765 u8 portmap; 766 int ret; 767 768 ret = vsc73xx_read_vlan_table_entry(vsc, vid, &portmap); 769 if (ret) 770 return ret; 771 772 if (set) 773 portmap |= BIT(port); 774 else 775 portmap &= ~BIT(port); 776 777 return vsc73xx_write_vlan_table_entry(vsc, vid, portmap); 778 } 779 780 static int vsc73xx_configure_rgmii_port_delay(struct dsa_switch *ds) 781 { 782 /* Keep 2.0 ns delay for backward complatibility */ 783 u32 tx_delay = VSC73XX_GMIIDELAY_GMII0_GTXDELAY_2_0_NS; 784 u32 rx_delay = VSC73XX_GMIIDELAY_GMII0_RXDELAY_2_0_NS; 785 struct dsa_port *dp = dsa_to_port(ds, CPU_PORT); 786 struct device_node *port_dn = dp->dn; 787 struct vsc73xx *vsc = ds->priv; 788 u32 delay; 789 790 if (!of_property_read_u32(port_dn, "tx-internal-delay-ps", &delay)) { 791 switch (delay) { 792 case 0: 793 tx_delay = VSC73XX_GMIIDELAY_GMII0_GTXDELAY_NONE; 794 break; 795 case 1400: 796 tx_delay = VSC73XX_GMIIDELAY_GMII0_GTXDELAY_1_4_NS; 797 break; 798 case 1700: 799 tx_delay = VSC73XX_GMIIDELAY_GMII0_GTXDELAY_1_7_NS; 800 break; 801 case 2000: 802 break; 803 default: 804 dev_err(vsc->dev, 805 "Unsupported RGMII Transmit Clock Delay\n"); 806 return -EINVAL; 807 } 808 } else { 809 dev_dbg(vsc->dev, 810 "RGMII Transmit Clock Delay isn't configured, set to 2.0 ns\n"); 811 } 812 813 if (!of_property_read_u32(port_dn, "rx-internal-delay-ps", &delay)) { 814 switch (delay) { 815 case 0: 816 rx_delay = VSC73XX_GMIIDELAY_GMII0_RXDELAY_NONE; 817 break; 818 case 1400: 819 rx_delay = VSC73XX_GMIIDELAY_GMII0_RXDELAY_1_4_NS; 820 break; 821 case 1700: 822 rx_delay = VSC73XX_GMIIDELAY_GMII0_RXDELAY_1_7_NS; 823 break; 824 case 2000: 825 break; 826 default: 827 dev_err(vsc->dev, 828 "Unsupported RGMII Receive Clock Delay value\n"); 829 return -EINVAL; 830 } 831 } else { 832 dev_dbg(vsc->dev, 833 "RGMII Receive Clock Delay isn't configured, set to 2.0 ns\n"); 834 } 835 836 /* MII delay, set both GTX and RX delay */ 837 return vsc73xx_write(vsc, VSC73XX_BLOCK_SYSTEM, 0, VSC73XX_GMIIDELAY, 838 tx_delay | rx_delay); 839 } 840 841 static int vsc73xx_setup(struct dsa_switch *ds) 842 { 843 struct vsc73xx *vsc = ds->priv; 844 int i, ret, val; 845 846 dev_info(vsc->dev, "set up the switch\n"); 847 848 ds->untag_bridge_pvid = true; 849 ds->max_num_bridges = DSA_TAG_8021Q_MAX_NUM_BRIDGES; 850 ds->fdb_isolation = true; 851 852 /* Issue RESET */ 853 vsc73xx_write(vsc, VSC73XX_BLOCK_SYSTEM, 0, VSC73XX_GLORESET, 854 VSC73XX_GLORESET_MASTER_RESET); 855 usleep_range(125, 200); 856 857 /* Initialize memory, initialize RAM bank 0..15 except 6 and 7 858 * This sequence appears in the 859 * VSC7385 SparX-G5 datasheet section 6.6.1 860 * VSC7395 SparX-G5e datasheet section 6.6.1 861 * "initialization sequence". 862 * No explanation is given to the 0x1010400 magic number. 863 */ 864 for (i = 0; i <= 15; i++) { 865 if (i != 6 && i != 7) { 866 vsc73xx_write(vsc, VSC73XX_BLOCK_MEMINIT, 867 2, 868 0, 0x1010400 + i); 869 mdelay(1); 870 } 871 } 872 mdelay(30); 873 874 /* Clear MAC table */ 875 vsc73xx_write(vsc, VSC73XX_BLOCK_ANALYZER, 0, 876 VSC73XX_MACACCESS, 877 VSC73XX_MACACCESS_CMD_CLEAR_TABLE); 878 879 /* Set VLAN table to default values */ 880 vsc73xx_write(vsc, VSC73XX_BLOCK_ANALYZER, 0, 881 VSC73XX_VLANACCESS, 882 VSC73XX_VLANACCESS_VLAN_TBL_CMD_CLEAR_TABLE); 883 884 msleep(40); 885 886 /* Use 20KiB buffers on all ports on VSC7395 887 * The VSC7385 has 16KiB buffers and that is the 888 * default if we don't set this up explicitly. 889 * Port "31" is "all ports". 890 */ 891 if (IS_739X(vsc)) 892 vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 0x1f, 893 VSC73XX_Q_MISC_CONF, 894 VSC73XX_Q_MISC_CONF_EXTENT_MEM); 895 896 /* Put all ports into reset until enabled */ 897 for (i = 0; i < 7; i++) { 898 if (i == 5) 899 continue; 900 vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 4, 901 VSC73XX_MAC_CFG, VSC73XX_MAC_CFG_RESET); 902 } 903 904 /* Configure RGMII delay */ 905 ret = vsc73xx_configure_rgmii_port_delay(ds); 906 if (ret) 907 return ret; 908 909 /* Ingess VLAN reception mask (table 145) */ 910 vsc73xx_write(vsc, VSC73XX_BLOCK_ANALYZER, 0, VSC73XX_VLANMASK, 911 0xff); 912 /* IP multicast flood mask (table 144) */ 913 vsc73xx_write(vsc, VSC73XX_BLOCK_ANALYZER, 0, VSC73XX_IFLODMSK, 914 0xff); 915 916 mdelay(50); 917 918 /* Disable preamble and use maximum allowed clock for the internal 919 * mdio bus, used for communication with internal PHYs only. 920 */ 921 val = VSC73XX_MII_MPRES_NOPREAMBLE | 922 FIELD_PREP(VSC73XX_MII_MPRES_PRESCALEVAL, 923 VSC73XX_MII_PRESCALEVAL_MIN); 924 vsc73xx_write(vsc, VSC73XX_BLOCK_MII, VSC73XX_BLOCK_MII_INTERNAL, 925 VSC73XX_MII_MPRES, val); 926 927 /* Release reset from the internal PHYs */ 928 vsc73xx_write(vsc, VSC73XX_BLOCK_SYSTEM, 0, VSC73XX_GLORESET, 929 VSC73XX_GLORESET_PHY_RESET); 930 931 udelay(4); 932 933 /* Clear VLAN table */ 934 for (i = 0; i < VLAN_N_VID; i++) 935 vsc73xx_write_vlan_table_entry(vsc, i, 0); 936 937 INIT_LIST_HEAD(&vsc->vlans); 938 939 rtnl_lock(); 940 ret = dsa_tag_8021q_register(ds, htons(ETH_P_8021Q)); 941 rtnl_unlock(); 942 943 return ret; 944 } 945 946 static void vsc73xx_teardown(struct dsa_switch *ds) 947 { 948 rtnl_lock(); 949 dsa_tag_8021q_unregister(ds); 950 rtnl_unlock(); 951 } 952 953 static void vsc73xx_init_port(struct vsc73xx *vsc, int port) 954 { 955 u32 val; 956 957 /* MAC configure, first reset the port and then write defaults */ 958 vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 959 port, 960 VSC73XX_MAC_CFG, 961 VSC73XX_MAC_CFG_RESET); 962 963 /* Take up the port in 1Gbit mode by default, this will be 964 * augmented after auto-negotiation on the PHY-facing 965 * ports. 966 */ 967 if (port == CPU_PORT) 968 val = VSC73XX_MAC_CFG_1000M_F_RGMII; 969 else 970 val = VSC73XX_MAC_CFG_1000M_F_PHY; 971 972 vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 973 port, 974 VSC73XX_MAC_CFG, 975 val | 976 VSC73XX_MAC_CFG_TX_EN | 977 VSC73XX_MAC_CFG_RX_EN); 978 979 /* Flow control for the CPU port: 980 * Use a zero delay pause frame when pause condition is left 981 * Obey pause control frames 982 */ 983 vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 984 port, 985 VSC73XX_FCCONF, 986 VSC73XX_FCCONF_ZERO_PAUSE_EN | 987 VSC73XX_FCCONF_FLOW_CTRL_OBEY); 988 989 /* Issue pause control frames on PHY facing ports. 990 * Allow early initiation of MAC transmission if the amount 991 * of egress data is below 512 bytes on CPU port. 992 * FIXME: enable 20KiB buffers? 993 */ 994 if (port == CPU_PORT) 995 val = VSC73XX_Q_MISC_CONF_EARLY_TX_512; 996 else 997 val = VSC73XX_Q_MISC_CONF_MAC_PAUSE_MODE; 998 val |= VSC73XX_Q_MISC_CONF_EXTENT_MEM; 999 vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 1000 port, 1001 VSC73XX_Q_MISC_CONF, 1002 val); 1003 1004 /* Flow control MAC: a MAC address used in flow control frames */ 1005 val = (vsc->addr[5] << 16) | (vsc->addr[4] << 8) | (vsc->addr[3]); 1006 vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 1007 port, 1008 VSC73XX_FCMACHI, 1009 val); 1010 val = (vsc->addr[2] << 16) | (vsc->addr[1] << 8) | (vsc->addr[0]); 1011 vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 1012 port, 1013 VSC73XX_FCMACLO, 1014 val); 1015 1016 /* Tell the categorizer to forward pause frames, not control 1017 * frame. Do not drop anything. 1018 */ 1019 vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 1020 port, 1021 VSC73XX_CAT_DROP, 1022 VSC73XX_CAT_DROP_FWD_PAUSE_ENA); 1023 1024 /* Clear all counters */ 1025 vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 1026 port, VSC73XX_C_RX0, 0); 1027 } 1028 1029 static void vsc73xx_reset_port(struct vsc73xx *vsc, int port, u32 initval) 1030 { 1031 int ret, err; 1032 u32 val; 1033 1034 /* Disable RX on this port */ 1035 vsc73xx_update_bits(vsc, VSC73XX_BLOCK_MAC, port, 1036 VSC73XX_MAC_CFG, 1037 VSC73XX_MAC_CFG_RX_EN, 0); 1038 1039 /* Discard packets */ 1040 vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ARBITER, 0, 1041 VSC73XX_ARBDISC, BIT(port), BIT(port)); 1042 1043 /* Wait until queue is empty */ 1044 ret = read_poll_timeout(vsc73xx_read, err, 1045 err < 0 || (val & BIT(port)), 1046 VSC73XX_POLL_SLEEP_US, 1047 VSC73XX_POLL_TIMEOUT_US, false, 1048 vsc, VSC73XX_BLOCK_ARBITER, 0, 1049 VSC73XX_ARBEMPTY, &val); 1050 if (ret) 1051 dev_err(vsc->dev, 1052 "timeout waiting for block arbiter\n"); 1053 else if (err < 0) 1054 dev_err(vsc->dev, "error reading arbiter\n"); 1055 1056 /* Put this port into reset */ 1057 vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port, VSC73XX_MAC_CFG, 1058 VSC73XX_MAC_CFG_RESET | initval); 1059 } 1060 1061 static void vsc73xx_mac_config(struct phylink_config *config, unsigned int mode, 1062 const struct phylink_link_state *state) 1063 { 1064 struct dsa_port *dp = dsa_phylink_to_port(config); 1065 struct vsc73xx *vsc = dp->ds->priv; 1066 int port = dp->index; 1067 1068 /* Special handling of the CPU-facing port */ 1069 if (port == CPU_PORT) { 1070 /* Other ports are already initialized but not this one */ 1071 vsc73xx_init_port(vsc, CPU_PORT); 1072 /* Select the external port for this interface (EXT_PORT) 1073 * Enable the GMII GTX external clock 1074 * Use double data rate (DDR mode) 1075 */ 1076 vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 1077 CPU_PORT, 1078 VSC73XX_ADVPORTM, 1079 VSC73XX_ADVPORTM_EXT_PORT | 1080 VSC73XX_ADVPORTM_ENA_GTX | 1081 VSC73XX_ADVPORTM_DDR_MODE); 1082 } 1083 } 1084 1085 static void vsc73xx_mac_link_down(struct phylink_config *config, 1086 unsigned int mode, phy_interface_t interface) 1087 { 1088 struct dsa_port *dp = dsa_phylink_to_port(config); 1089 struct vsc73xx *vsc = dp->ds->priv; 1090 int port = dp->index; 1091 1092 /* This routine is described in the datasheet (below ARBDISC register 1093 * description) 1094 */ 1095 vsc73xx_reset_port(vsc, port, 0); 1096 1097 /* Allow backward dropping of frames from this port */ 1098 vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ARBITER, 0, 1099 VSC73XX_SBACKWDROP, BIT(port), BIT(port)); 1100 } 1101 1102 static void vsc73xx_mac_link_up(struct phylink_config *config, 1103 struct phy_device *phy, unsigned int mode, 1104 phy_interface_t interface, int speed, 1105 int duplex, bool tx_pause, bool rx_pause) 1106 { 1107 struct dsa_port *dp = dsa_phylink_to_port(config); 1108 struct vsc73xx *vsc = dp->ds->priv; 1109 int port = dp->index; 1110 u32 val; 1111 u8 seed; 1112 1113 if (speed == SPEED_1000) 1114 val = VSC73XX_MAC_CFG_GIGA_MODE | VSC73XX_MAC_CFG_TX_IPG_1000M; 1115 else 1116 val = VSC73XX_MAC_CFG_TX_IPG_100_10M; 1117 1118 if (phy_interface_mode_is_rgmii(interface)) 1119 val |= VSC73XX_MAC_CFG_CLK_SEL_1000M; 1120 else 1121 val |= VSC73XX_MAC_CFG_CLK_SEL_EXT; 1122 1123 if (duplex == DUPLEX_FULL) 1124 val |= VSC73XX_MAC_CFG_FDX; 1125 else 1126 /* In datasheet description ("Port Mode Procedure" in 5.6.2) 1127 * this bit is configured only for half duplex. 1128 */ 1129 val |= VSC73XX_MAC_CFG_WEXC_DIS; 1130 1131 /* This routine is described in the datasheet (below ARBDISC register 1132 * description) 1133 */ 1134 vsc73xx_reset_port(vsc, port, val); 1135 1136 /* Seed the port randomness with randomness */ 1137 get_random_bytes(&seed, 1); 1138 val |= seed << VSC73XX_MAC_CFG_SEED_OFFSET; 1139 val |= VSC73XX_MAC_CFG_SEED_LOAD; 1140 1141 /* Those bits are responsible for MTU only. Kernel takes care about MTU, 1142 * let's enable +8 bytes frame length unconditionally. 1143 */ 1144 val |= VSC73XX_MAC_CFG_VLAN_AWR | VSC73XX_MAC_CFG_VLAN_DBLAWR; 1145 1146 vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port, VSC73XX_MAC_CFG, val); 1147 1148 /* Flow control for the PHY facing ports: 1149 * Use a zero delay pause frame when pause condition is left 1150 * Obey pause control frames 1151 * When generating pause frames, use 0xff as pause value 1152 */ 1153 vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port, VSC73XX_FCCONF, 1154 VSC73XX_FCCONF_ZERO_PAUSE_EN | 1155 VSC73XX_FCCONF_FLOW_CTRL_OBEY | 1156 0xff); 1157 1158 /* Accept packets again */ 1159 vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ARBITER, 0, 1160 VSC73XX_ARBDISC, BIT(port), 0); 1161 1162 /* Disallow backward dropping of frames from this port */ 1163 vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ARBITER, 0, 1164 VSC73XX_SBACKWDROP, BIT(port), 0); 1165 1166 /* Enable TX, RX, deassert reset, stop loading seed */ 1167 vsc73xx_update_bits(vsc, VSC73XX_BLOCK_MAC, port, 1168 VSC73XX_MAC_CFG, 1169 VSC73XX_MAC_CFG_RESET | VSC73XX_MAC_CFG_SEED_LOAD | 1170 VSC73XX_MAC_CFG_TX_EN | VSC73XX_MAC_CFG_RX_EN, 1171 VSC73XX_MAC_CFG_TX_EN | VSC73XX_MAC_CFG_RX_EN); 1172 } 1173 1174 static bool vsc73xx_tag_8021q_active(struct dsa_port *dp) 1175 { 1176 return !dsa_port_is_vlan_filtering(dp); 1177 } 1178 1179 static struct vsc73xx_bridge_vlan * 1180 vsc73xx_bridge_vlan_find(struct vsc73xx *vsc, u16 vid) 1181 { 1182 struct vsc73xx_bridge_vlan *vlan; 1183 1184 list_for_each_entry(vlan, &vsc->vlans, list) 1185 if (vlan->vid == vid) 1186 return vlan; 1187 1188 return NULL; 1189 } 1190 1191 static void 1192 vsc73xx_bridge_vlan_remove_port(struct vsc73xx_bridge_vlan *vsc73xx_vlan, 1193 int port) 1194 { 1195 vsc73xx_vlan->portmask &= ~BIT(port); 1196 1197 if (vsc73xx_vlan->portmask) 1198 return; 1199 1200 list_del(&vsc73xx_vlan->list); 1201 kfree(vsc73xx_vlan); 1202 } 1203 1204 static void vsc73xx_bridge_vlan_summary(struct vsc73xx *vsc, int port, 1205 struct vsc73xx_vlan_summary *summary, 1206 u16 ignored_vid) 1207 { 1208 size_t num_tagged = 0, num_untagged = 0; 1209 struct vsc73xx_bridge_vlan *vlan; 1210 1211 list_for_each_entry(vlan, &vsc->vlans, list) { 1212 if (!(vlan->portmask & BIT(port)) || vlan->vid == ignored_vid) 1213 continue; 1214 1215 if (vlan->untagged & BIT(port)) 1216 num_untagged++; 1217 else 1218 num_tagged++; 1219 } 1220 1221 summary->num_untagged = num_untagged; 1222 summary->num_tagged = num_tagged; 1223 } 1224 1225 static u16 vsc73xx_find_first_vlan_untagged(struct vsc73xx *vsc, int port) 1226 { 1227 struct vsc73xx_bridge_vlan *vlan; 1228 1229 list_for_each_entry(vlan, &vsc->vlans, list) 1230 if ((vlan->portmask & BIT(port)) && 1231 (vlan->untagged & BIT(port))) 1232 return vlan->vid; 1233 1234 return VLAN_N_VID; 1235 } 1236 1237 static int vsc73xx_set_vlan_conf(struct vsc73xx *vsc, int port, 1238 enum vsc73xx_port_vlan_conf port_vlan_conf) 1239 { 1240 u32 val = 0; 1241 int ret; 1242 1243 if (port_vlan_conf == VSC73XX_VLAN_IGNORE) 1244 val = VSC73XX_CAT_VLAN_MISC_VLAN_TCI_IGNORE_ENA | 1245 VSC73XX_CAT_VLAN_MISC_VLAN_KEEP_TAG_ENA; 1246 1247 ret = vsc73xx_update_bits(vsc, VSC73XX_BLOCK_MAC, port, 1248 VSC73XX_CAT_VLAN_MISC, 1249 VSC73XX_CAT_VLAN_MISC_VLAN_TCI_IGNORE_ENA | 1250 VSC73XX_CAT_VLAN_MISC_VLAN_KEEP_TAG_ENA, val); 1251 if (ret) 1252 return ret; 1253 1254 val = (port_vlan_conf == VSC73XX_VLAN_FILTER) ? 1255 VSC73XX_TXUPDCFG_TX_INSERT_TAG : 0; 1256 1257 return vsc73xx_update_bits(vsc, VSC73XX_BLOCK_MAC, port, 1258 VSC73XX_TXUPDCFG, 1259 VSC73XX_TXUPDCFG_TX_INSERT_TAG, val); 1260 } 1261 1262 /** 1263 * vsc73xx_vlan_commit_conf - Update VLAN configuration of a port 1264 * @vsc: Switch private data structure 1265 * @port: Port index on which to operate 1266 * 1267 * Update the VLAN behavior of a port to make sure that when it is under 1268 * a VLAN filtering bridge, the port is either filtering with tag 1269 * preservation, or filtering with all VLANs egress-untagged. Otherwise, 1270 * the port ignores VLAN tags from packets and applies the port-based 1271 * VID. 1272 * 1273 * Must be called when changes are made to: 1274 * - the bridge VLAN filtering state of the port 1275 * - the number or attributes of VLANs from the bridge VLAN table, 1276 * while the port is currently VLAN-aware 1277 * 1278 * Return: 0 on success, or negative errno on error. 1279 */ 1280 static int vsc73xx_vlan_commit_conf(struct vsc73xx *vsc, int port) 1281 { 1282 enum vsc73xx_port_vlan_conf port_vlan_conf = VSC73XX_VLAN_IGNORE; 1283 struct dsa_port *dp = dsa_to_port(vsc->ds, port); 1284 1285 if (port == CPU_PORT) { 1286 port_vlan_conf = VSC73XX_VLAN_FILTER; 1287 } else if (dsa_port_is_vlan_filtering(dp)) { 1288 struct vsc73xx_vlan_summary summary; 1289 1290 port_vlan_conf = VSC73XX_VLAN_FILTER; 1291 1292 vsc73xx_bridge_vlan_summary(vsc, port, &summary, VLAN_N_VID); 1293 if (summary.num_tagged == 0) 1294 port_vlan_conf = VSC73XX_VLAN_FILTER_UNTAG_ALL; 1295 } 1296 1297 return vsc73xx_set_vlan_conf(vsc, port, port_vlan_conf); 1298 } 1299 1300 static int 1301 vsc73xx_vlan_change_untagged(struct vsc73xx *vsc, int port, u16 vid, bool set) 1302 { 1303 u32 val = 0; 1304 1305 if (set) 1306 val = VSC73XX_TXUPDCFG_TX_UNTAGGED_VID_ENA | 1307 ((vid << VSC73XX_TXUPDCFG_TX_UNTAGGED_VID_SHIFT) & 1308 VSC73XX_TXUPDCFG_TX_UNTAGGED_VID); 1309 1310 return vsc73xx_update_bits(vsc, VSC73XX_BLOCK_MAC, port, 1311 VSC73XX_TXUPDCFG, 1312 VSC73XX_TXUPDCFG_TX_UNTAGGED_VID_ENA | 1313 VSC73XX_TXUPDCFG_TX_UNTAGGED_VID, val); 1314 } 1315 1316 /** 1317 * vsc73xx_vlan_commit_untagged - Update native VLAN of a port 1318 * @vsc: Switch private data structure 1319 * @port: Port index on which to operate 1320 * 1321 * Update the native VLAN of a port (the one VLAN which is transmitted 1322 * as egress-tagged on a trunk port) when port is in VLAN filtering mode and 1323 * only one untagged vid is configured. 1324 * In other cases no need to configure it because switch can untag all vlans on 1325 * the port. 1326 * 1327 * Return: 0 on success, or negative errno on error. 1328 */ 1329 static int vsc73xx_vlan_commit_untagged(struct vsc73xx *vsc, int port) 1330 { 1331 struct dsa_port *dp = dsa_to_port(vsc->ds, port); 1332 struct vsc73xx_vlan_summary summary; 1333 u16 vid = 0; 1334 bool valid; 1335 1336 if (!dsa_port_is_vlan_filtering(dp)) 1337 /* Port is configured to untag all vlans in that case. 1338 * No need to commit untagged config change. 1339 */ 1340 return 0; 1341 1342 vsc73xx_bridge_vlan_summary(vsc, port, &summary, VLAN_N_VID); 1343 1344 if (summary.num_untagged > 1) 1345 /* Port must untag all vlans in that case. 1346 * No need to commit untagged config change. 1347 */ 1348 return 0; 1349 1350 valid = (summary.num_untagged == 1); 1351 if (valid) 1352 vid = vsc73xx_find_first_vlan_untagged(vsc, port); 1353 1354 return vsc73xx_vlan_change_untagged(vsc, port, vid, valid); 1355 } 1356 1357 static int 1358 vsc73xx_vlan_change_pvid(struct vsc73xx *vsc, int port, u16 vid, bool set) 1359 { 1360 u32 val = 0; 1361 int ret; 1362 1363 val = set ? 0 : VSC73XX_CAT_DROP_UNTAGGED_ENA; 1364 1365 ret = vsc73xx_update_bits(vsc, VSC73XX_BLOCK_MAC, port, 1366 VSC73XX_CAT_DROP, 1367 VSC73XX_CAT_DROP_UNTAGGED_ENA, val); 1368 if (!set || ret) 1369 return ret; 1370 1371 return vsc73xx_update_bits(vsc, VSC73XX_BLOCK_MAC, port, 1372 VSC73XX_CAT_PORT_VLAN, 1373 VSC73XX_CAT_PORT_VLAN_VLAN_VID, 1374 vid & VSC73XX_CAT_PORT_VLAN_VLAN_VID); 1375 } 1376 1377 /** 1378 * vsc73xx_vlan_commit_pvid - Update port-based default VLAN of a port 1379 * @vsc: Switch private data structure 1380 * @port: Port index on which to operate 1381 * 1382 * Update the PVID of a port so that it follows either the bridge PVID 1383 * configuration, when the bridge is currently VLAN-aware, or the PVID 1384 * from tag_8021q, when the port is standalone or under a VLAN-unaware 1385 * bridge. A port with no PVID drops all untagged and VID 0 tagged 1386 * traffic. 1387 * 1388 * Must be called when changes are made to: 1389 * - the bridge VLAN filtering state of the port 1390 * - the number or attributes of VLANs from the bridge VLAN table, 1391 * while the port is currently VLAN-aware 1392 * 1393 * Return: 0 on success, or negative errno on error. 1394 */ 1395 static int vsc73xx_vlan_commit_pvid(struct vsc73xx *vsc, int port) 1396 { 1397 struct vsc73xx_portinfo *portinfo = &vsc->portinfo[port]; 1398 bool valid = portinfo->pvid_tag_8021q_configured; 1399 struct dsa_port *dp = dsa_to_port(vsc->ds, port); 1400 u16 vid = portinfo->pvid_tag_8021q; 1401 1402 if (dsa_port_is_vlan_filtering(dp)) { 1403 vid = portinfo->pvid_vlan_filtering; 1404 valid = portinfo->pvid_vlan_filtering_configured; 1405 } 1406 1407 return vsc73xx_vlan_change_pvid(vsc, port, vid, valid); 1408 } 1409 1410 static int vsc73xx_vlan_commit_settings(struct vsc73xx *vsc, int port) 1411 { 1412 int ret; 1413 1414 ret = vsc73xx_vlan_commit_untagged(vsc, port); 1415 if (ret) 1416 return ret; 1417 1418 ret = vsc73xx_vlan_commit_pvid(vsc, port); 1419 if (ret) 1420 return ret; 1421 1422 return vsc73xx_vlan_commit_conf(vsc, port); 1423 } 1424 1425 static int vsc73xx_port_enable(struct dsa_switch *ds, int port, 1426 struct phy_device *phy) 1427 { 1428 struct vsc73xx *vsc = ds->priv; 1429 1430 dev_info(vsc->dev, "enable port %d\n", port); 1431 vsc73xx_init_port(vsc, port); 1432 1433 return vsc73xx_vlan_commit_settings(vsc, port); 1434 } 1435 1436 static void vsc73xx_port_disable(struct dsa_switch *ds, int port) 1437 { 1438 struct vsc73xx *vsc = ds->priv; 1439 1440 /* Just put the port into reset */ 1441 vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port, 1442 VSC73XX_MAC_CFG, VSC73XX_MAC_CFG_RESET); 1443 } 1444 1445 static const struct vsc73xx_counter * 1446 vsc73xx_find_counter(struct vsc73xx *vsc, 1447 u8 counter, 1448 bool tx) 1449 { 1450 const struct vsc73xx_counter *cnts; 1451 int num_cnts; 1452 int i; 1453 1454 if (tx) { 1455 cnts = vsc73xx_tx_counters; 1456 num_cnts = ARRAY_SIZE(vsc73xx_tx_counters); 1457 } else { 1458 cnts = vsc73xx_rx_counters; 1459 num_cnts = ARRAY_SIZE(vsc73xx_rx_counters); 1460 } 1461 1462 for (i = 0; i < num_cnts; i++) { 1463 const struct vsc73xx_counter *cnt; 1464 1465 cnt = &cnts[i]; 1466 if (cnt->counter == counter) 1467 return cnt; 1468 } 1469 1470 return NULL; 1471 } 1472 1473 static void vsc73xx_get_strings(struct dsa_switch *ds, int port, u32 stringset, 1474 uint8_t *data) 1475 { 1476 const struct vsc73xx_counter *cnt; 1477 struct vsc73xx *vsc = ds->priv; 1478 u8 indices[6]; 1479 u8 *buf = data; 1480 int i; 1481 u32 val; 1482 int ret; 1483 1484 if (stringset != ETH_SS_STATS) 1485 return; 1486 1487 ret = vsc73xx_read(vsc, VSC73XX_BLOCK_MAC, port, 1488 VSC73XX_C_CFG, &val); 1489 if (ret) 1490 return; 1491 1492 indices[0] = (val & 0x1f); /* RX counter 0 */ 1493 indices[1] = ((val >> 5) & 0x1f); /* RX counter 1 */ 1494 indices[2] = ((val >> 10) & 0x1f); /* RX counter 2 */ 1495 indices[3] = ((val >> 16) & 0x1f); /* TX counter 0 */ 1496 indices[4] = ((val >> 21) & 0x1f); /* TX counter 1 */ 1497 indices[5] = ((val >> 26) & 0x1f); /* TX counter 2 */ 1498 1499 /* The first counters is the RX octets */ 1500 ethtool_puts(&buf, "RxEtherStatsOctets"); 1501 1502 /* Each port supports recording 3 RX counters and 3 TX counters, 1503 * figure out what counters we use in this set-up and return the 1504 * names of them. The hardware default counters will be number of 1505 * packets on RX/TX, combined broadcast+multicast packets RX/TX and 1506 * total error packets RX/TX. 1507 */ 1508 for (i = 0; i < 3; i++) { 1509 cnt = vsc73xx_find_counter(vsc, indices[i], false); 1510 ethtool_puts(&buf, cnt ? cnt->name : ""); 1511 } 1512 1513 /* TX stats begins with the number of TX octets */ 1514 ethtool_puts(&buf, "TxEtherStatsOctets"); 1515 1516 for (i = 3; i < 6; i++) { 1517 cnt = vsc73xx_find_counter(vsc, indices[i], true); 1518 ethtool_puts(&buf, cnt ? cnt->name : ""); 1519 1520 } 1521 } 1522 1523 static int vsc73xx_get_sset_count(struct dsa_switch *ds, int port, int sset) 1524 { 1525 /* We only support SS_STATS */ 1526 if (sset != ETH_SS_STATS) 1527 return 0; 1528 /* RX and TX packets, then 3 RX counters, 3 TX counters */ 1529 return 8; 1530 } 1531 1532 static void vsc73xx_get_ethtool_stats(struct dsa_switch *ds, int port, 1533 uint64_t *data) 1534 { 1535 struct vsc73xx *vsc = ds->priv; 1536 u8 regs[] = { 1537 VSC73XX_RXOCT, 1538 VSC73XX_C_RX0, 1539 VSC73XX_C_RX1, 1540 VSC73XX_C_RX2, 1541 VSC73XX_TXOCT, 1542 VSC73XX_C_TX0, 1543 VSC73XX_C_TX1, 1544 VSC73XX_C_TX2, 1545 }; 1546 u32 val; 1547 int ret; 1548 int i; 1549 1550 for (i = 0; i < ARRAY_SIZE(regs); i++) { 1551 ret = vsc73xx_read(vsc, VSC73XX_BLOCK_MAC, port, 1552 regs[i], &val); 1553 if (ret) { 1554 dev_err(vsc->dev, "error reading counter %d\n", i); 1555 return; 1556 } 1557 data[i] = val; 1558 } 1559 } 1560 1561 static int vsc73xx_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 1562 { 1563 struct vsc73xx *vsc = ds->priv; 1564 1565 return vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port, 1566 VSC73XX_MAXLEN, new_mtu + ETH_HLEN + ETH_FCS_LEN); 1567 } 1568 1569 /* According to application not "VSC7398 Jumbo Frames" setting 1570 * up the frame size to 9.6 KB does not affect the performance on standard 1571 * frames. It is clear from the application note that 1572 * "9.6 kilobytes" == 9600 bytes. 1573 */ 1574 static int vsc73xx_get_max_mtu(struct dsa_switch *ds, int port) 1575 { 1576 return 9600 - ETH_HLEN - ETH_FCS_LEN; 1577 } 1578 1579 static void vsc73xx_phylink_get_caps(struct dsa_switch *dsa, int port, 1580 struct phylink_config *config) 1581 { 1582 unsigned long *interfaces = config->supported_interfaces; 1583 1584 if (port == 5) 1585 return; 1586 1587 if (port == CPU_PORT) { 1588 __set_bit(PHY_INTERFACE_MODE_MII, interfaces); 1589 __set_bit(PHY_INTERFACE_MODE_REVMII, interfaces); 1590 __set_bit(PHY_INTERFACE_MODE_GMII, interfaces); 1591 __set_bit(PHY_INTERFACE_MODE_RGMII, interfaces); 1592 } 1593 1594 if (port <= 4) { 1595 /* Internal PHYs */ 1596 __set_bit(PHY_INTERFACE_MODE_INTERNAL, interfaces); 1597 /* phylib default */ 1598 __set_bit(PHY_INTERFACE_MODE_GMII, interfaces); 1599 } 1600 1601 config->mac_capabilities = MAC_SYM_PAUSE | MAC_10 | MAC_100 | MAC_1000; 1602 } 1603 1604 static int 1605 vsc73xx_port_vlan_filtering(struct dsa_switch *ds, int port, 1606 bool vlan_filtering, struct netlink_ext_ack *extack) 1607 { 1608 struct vsc73xx *vsc = ds->priv; 1609 1610 /* The commit to hardware processed below is required because vsc73xx 1611 * is using tag_8021q. When vlan_filtering is disabled, tag_8021q uses 1612 * pvid/untagged vlans for port recognition. The values configured for 1613 * vlans and pvid/untagged states are stored in portinfo structure. 1614 * When vlan_filtering is enabled, we need to restore pvid/untagged from 1615 * portinfo structure. Analogous routine is processed when 1616 * vlan_filtering is disabled, but values used for tag_8021q are 1617 * restored. 1618 */ 1619 1620 return vsc73xx_vlan_commit_settings(vsc, port); 1621 } 1622 1623 static int vsc73xx_port_vlan_add(struct dsa_switch *ds, int port, 1624 const struct switchdev_obj_port_vlan *vlan, 1625 struct netlink_ext_ack *extack) 1626 { 1627 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; 1628 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID; 1629 struct dsa_port *dp = dsa_to_port(ds, port); 1630 struct vsc73xx_bridge_vlan *vsc73xx_vlan; 1631 struct vsc73xx_vlan_summary summary; 1632 struct vsc73xx_portinfo *portinfo; 1633 struct vsc73xx *vsc = ds->priv; 1634 bool commit_to_hardware; 1635 int ret = 0; 1636 1637 /* Be sure to deny alterations to the configuration done by tag_8021q. 1638 */ 1639 if (vid_is_dsa_8021q(vlan->vid)) { 1640 NL_SET_ERR_MSG_MOD(extack, 1641 "Range 3072-4095 reserved for dsa_8021q operation"); 1642 return -EBUSY; 1643 } 1644 1645 /* The processed vlan->vid is excluded from the search because the VLAN 1646 * can be re-added with a different set of flags, so it's easiest to 1647 * ignore its old flags from the VLAN database software copy. 1648 */ 1649 vsc73xx_bridge_vlan_summary(vsc, port, &summary, vlan->vid); 1650 1651 /* VSC73XX allows only three untagged states: none, one or all */ 1652 if ((untagged && summary.num_tagged > 0 && summary.num_untagged > 0) || 1653 (!untagged && summary.num_untagged > 1)) { 1654 NL_SET_ERR_MSG_MOD(extack, 1655 "Port can have only none, one or all untagged vlan"); 1656 return -EBUSY; 1657 } 1658 1659 vsc73xx_vlan = vsc73xx_bridge_vlan_find(vsc, vlan->vid); 1660 1661 if (!vsc73xx_vlan) { 1662 vsc73xx_vlan = kzalloc(sizeof(*vsc73xx_vlan), GFP_KERNEL); 1663 if (!vsc73xx_vlan) 1664 return -ENOMEM; 1665 1666 vsc73xx_vlan->vid = vlan->vid; 1667 1668 list_add_tail(&vsc73xx_vlan->list, &vsc->vlans); 1669 } 1670 1671 vsc73xx_vlan->portmask |= BIT(port); 1672 1673 /* CPU port must be always tagged because source port identification is 1674 * based on tag_8021q. 1675 */ 1676 if (port == CPU_PORT) 1677 goto update_vlan_table; 1678 1679 if (untagged) 1680 vsc73xx_vlan->untagged |= BIT(port); 1681 else 1682 vsc73xx_vlan->untagged &= ~BIT(port); 1683 1684 portinfo = &vsc->portinfo[port]; 1685 1686 if (pvid) { 1687 portinfo->pvid_vlan_filtering_configured = true; 1688 portinfo->pvid_vlan_filtering = vlan->vid; 1689 } else if (portinfo->pvid_vlan_filtering_configured && 1690 portinfo->pvid_vlan_filtering == vlan->vid) { 1691 portinfo->pvid_vlan_filtering_configured = false; 1692 } 1693 1694 commit_to_hardware = !vsc73xx_tag_8021q_active(dp); 1695 if (commit_to_hardware) { 1696 ret = vsc73xx_vlan_commit_settings(vsc, port); 1697 if (ret) 1698 goto err; 1699 } 1700 1701 update_vlan_table: 1702 ret = vsc73xx_update_vlan_table(vsc, port, vlan->vid, true); 1703 if (!ret) 1704 return 0; 1705 err: 1706 vsc73xx_bridge_vlan_remove_port(vsc73xx_vlan, port); 1707 return ret; 1708 } 1709 1710 static int vsc73xx_port_vlan_del(struct dsa_switch *ds, int port, 1711 const struct switchdev_obj_port_vlan *vlan) 1712 { 1713 struct vsc73xx_bridge_vlan *vsc73xx_vlan; 1714 struct vsc73xx_portinfo *portinfo; 1715 struct vsc73xx *vsc = ds->priv; 1716 bool commit_to_hardware; 1717 int ret; 1718 1719 ret = vsc73xx_update_vlan_table(vsc, port, vlan->vid, false); 1720 if (ret) 1721 return ret; 1722 1723 portinfo = &vsc->portinfo[port]; 1724 1725 if (portinfo->pvid_vlan_filtering_configured && 1726 portinfo->pvid_vlan_filtering == vlan->vid) 1727 portinfo->pvid_vlan_filtering_configured = false; 1728 1729 vsc73xx_vlan = vsc73xx_bridge_vlan_find(vsc, vlan->vid); 1730 1731 if (vsc73xx_vlan) 1732 vsc73xx_bridge_vlan_remove_port(vsc73xx_vlan, port); 1733 1734 commit_to_hardware = !vsc73xx_tag_8021q_active(dsa_to_port(ds, port)); 1735 1736 if (commit_to_hardware) 1737 return vsc73xx_vlan_commit_settings(vsc, port); 1738 1739 return 0; 1740 } 1741 1742 static int vsc73xx_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid, 1743 u16 flags) 1744 { 1745 bool pvid = flags & BRIDGE_VLAN_INFO_PVID; 1746 struct vsc73xx_portinfo *portinfo; 1747 struct vsc73xx *vsc = ds->priv; 1748 bool commit_to_hardware; 1749 int ret; 1750 1751 portinfo = &vsc->portinfo[port]; 1752 1753 if (pvid) { 1754 portinfo->pvid_tag_8021q_configured = true; 1755 portinfo->pvid_tag_8021q = vid; 1756 } 1757 1758 commit_to_hardware = vsc73xx_tag_8021q_active(dsa_to_port(ds, port)); 1759 if (commit_to_hardware) { 1760 ret = vsc73xx_vlan_commit_settings(vsc, port); 1761 if (ret) 1762 return ret; 1763 } 1764 1765 return vsc73xx_update_vlan_table(vsc, port, vid, true); 1766 } 1767 1768 static int vsc73xx_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid) 1769 { 1770 struct vsc73xx_portinfo *portinfo; 1771 struct vsc73xx *vsc = ds->priv; 1772 1773 portinfo = &vsc->portinfo[port]; 1774 1775 if (portinfo->pvid_tag_8021q_configured && 1776 portinfo->pvid_tag_8021q == vid) { 1777 struct dsa_port *dp = dsa_to_port(ds, port); 1778 bool commit_to_hardware; 1779 int err; 1780 1781 portinfo->pvid_tag_8021q_configured = false; 1782 1783 commit_to_hardware = vsc73xx_tag_8021q_active(dp); 1784 if (commit_to_hardware) { 1785 err = vsc73xx_vlan_commit_settings(vsc, port); 1786 if (err) 1787 return err; 1788 } 1789 } 1790 1791 return vsc73xx_update_vlan_table(vsc, port, vid, false); 1792 } 1793 1794 static int vsc73xx_port_pre_bridge_flags(struct dsa_switch *ds, int port, 1795 struct switchdev_brport_flags flags, 1796 struct netlink_ext_ack *extack) 1797 { 1798 if (flags.mask & ~BR_LEARNING) 1799 return -EINVAL; 1800 1801 return 0; 1802 } 1803 1804 static int vsc73xx_port_bridge_flags(struct dsa_switch *ds, int port, 1805 struct switchdev_brport_flags flags, 1806 struct netlink_ext_ack *extack) 1807 { 1808 if (flags.mask & BR_LEARNING) { 1809 u32 val = flags.val & BR_LEARNING ? BIT(port) : 0; 1810 struct vsc73xx *vsc = ds->priv; 1811 1812 return vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ANALYZER, 0, 1813 VSC73XX_LEARNMASK, BIT(port), val); 1814 } 1815 1816 return 0; 1817 } 1818 1819 static void vsc73xx_refresh_fwd_map(struct dsa_switch *ds, int port, u8 state) 1820 { 1821 struct dsa_port *other_dp, *dp = dsa_to_port(ds, port); 1822 struct vsc73xx *vsc = ds->priv; 1823 u16 mask; 1824 1825 if (state != BR_STATE_FORWARDING) { 1826 /* Ports that aren't in the forwarding state must not 1827 * forward packets anywhere. 1828 */ 1829 vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ANALYZER, 0, 1830 VSC73XX_SRCMASKS + port, 1831 VSC73XX_SRCMASKS_PORTS_MASK, 0); 1832 1833 dsa_switch_for_each_available_port(other_dp, ds) { 1834 if (other_dp == dp) 1835 continue; 1836 vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ANALYZER, 0, 1837 VSC73XX_SRCMASKS + other_dp->index, 1838 BIT(port), 0); 1839 } 1840 1841 return; 1842 } 1843 1844 /* Forwarding ports must forward to the CPU and to other ports 1845 * in the same bridge 1846 */ 1847 vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ANALYZER, 0, 1848 VSC73XX_SRCMASKS + CPU_PORT, BIT(port), BIT(port)); 1849 1850 mask = BIT(CPU_PORT); 1851 1852 dsa_switch_for_each_user_port(other_dp, ds) { 1853 int other_port = other_dp->index; 1854 1855 if (port == other_port || !dsa_port_bridge_same(dp, other_dp) || 1856 other_dp->stp_state != BR_STATE_FORWARDING) 1857 continue; 1858 1859 mask |= BIT(other_port); 1860 1861 vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ANALYZER, 0, 1862 VSC73XX_SRCMASKS + other_port, 1863 BIT(port), BIT(port)); 1864 } 1865 1866 vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ANALYZER, 0, 1867 VSC73XX_SRCMASKS + port, 1868 VSC73XX_SRCMASKS_PORTS_MASK, mask); 1869 } 1870 1871 /* FIXME: STP frames aren't forwarded at this moment. BPDU frames are 1872 * forwarded only from and to PI/SI interface. For more info see chapter 1873 * 2.7.1 (CPU Forwarding) in datasheet. 1874 * This function is required for tag_8021q operations. 1875 */ 1876 static void vsc73xx_port_stp_state_set(struct dsa_switch *ds, int port, 1877 u8 state) 1878 { 1879 struct dsa_port *dp = dsa_to_port(ds, port); 1880 struct vsc73xx *vsc = ds->priv; 1881 u32 val = 0; 1882 1883 if (state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) 1884 val = dp->learning ? BIT(port) : 0; 1885 1886 vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ANALYZER, 0, 1887 VSC73XX_LEARNMASK, BIT(port), val); 1888 1889 val = (state == BR_STATE_BLOCKING || state == BR_STATE_DISABLED) ? 1890 0 : BIT(port); 1891 vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ANALYZER, 0, 1892 VSC73XX_RECVMASK, BIT(port), val); 1893 1894 /* CPU Port should always forward packets when user ports are forwarding 1895 * so let's configure it from other ports only. 1896 */ 1897 if (port != CPU_PORT) 1898 vsc73xx_refresh_fwd_map(ds, port, state); 1899 } 1900 1901 static u16 vsc73xx_calc_hash(const unsigned char *addr, u16 vid) 1902 { 1903 /* VID 5-0, MAC 47-44 */ 1904 u16 hash = FIELD_PREP(VSC73XX_HASH0_VID_TO_MASK, 1905 FIELD_GET(VSC73XX_HASH0_VID_FROM_MASK, vid)) | 1906 FIELD_PREP(VSC73XX_HASH0_MAC0_TO_MASK, 1907 FIELD_GET(VSC73XX_HASH0_MAC0_FROM_MASK, addr[0])); 1908 /* MAC 43-33 */ 1909 hash ^= FIELD_PREP(VSC73XX_HASH1_MAC0_TO_MASK, 1910 FIELD_GET(VSC73XX_HASH1_MAC0_FROM_MASK, addr[0])) | 1911 FIELD_PREP(VSC73XX_HASH1_MAC1_TO_MASK, 1912 FIELD_GET(VSC73XX_HASH1_MAC1_FROM_MASK, addr[1])); 1913 /* MAC 32-22 */ 1914 hash ^= FIELD_PREP(VSC73XX_HASH2_MAC1_TO_MASK, 1915 FIELD_GET(VSC73XX_HASH2_MAC1_FROM_MASK, addr[1])) | 1916 FIELD_PREP(VSC73XX_HASH2_MAC2_TO_MASK, 1917 FIELD_GET(VSC73XX_HASH2_MAC2_FROM_MASK, addr[2])) | 1918 FIELD_PREP(VSC73XX_HASH2_MAC3_TO_MASK, 1919 FIELD_GET(VSC73XX_HASH2_MAC3_FROM_MASK, addr[3])); 1920 /* MAC 21-11 */ 1921 hash ^= FIELD_PREP(VSC73XX_HASH3_MAC3_TO_MASK, 1922 FIELD_GET(VSC73XX_HASH3_MAC3_FROM_MASK, addr[3])) | 1923 FIELD_PREP(VSC73XX_HASH3_MAC4_TO_MASK, 1924 FIELD_GET(VSC73XX_HASH3_MAC4_FROM_MASK, addr[4])); 1925 /* MAC 10-0 */ 1926 hash ^= FIELD_PREP(VSC73XX_HASH4_MAC4_TO_MASK, 1927 FIELD_GET(VSC73XX_HASH4_MAC4_FROM_MASK, addr[4])) | 1928 addr[5]; 1929 1930 return hash; 1931 } 1932 1933 static int 1934 vsc73xx_port_wait_for_mac_table_cmd(struct vsc73xx *vsc) 1935 { 1936 int ret, err; 1937 u32 val; 1938 1939 ret = read_poll_timeout(vsc73xx_read, err, 1940 err < 0 || 1941 ((val & VSC73XX_MACACCESS_CMD_MASK) == 1942 VSC73XX_MACACCESS_CMD_IDLE), 1943 VSC73XX_POLL_SLEEP_US, VSC73XX_POLL_TIMEOUT_US, 1944 false, vsc, VSC73XX_BLOCK_ANALYZER, 1945 0, VSC73XX_MACACCESS, &val); 1946 if (ret) 1947 return ret; 1948 return err; 1949 } 1950 1951 static int vsc73xx_port_read_mac_table_row(struct vsc73xx *vsc, u16 index, 1952 struct vsc73xx_fdb *fdb) 1953 { 1954 int ret, i; 1955 u32 val; 1956 1957 if (!fdb) 1958 return -EINVAL; 1959 if (index >= VSC73XX_NUM_FDB_ROWS) 1960 return -EINVAL; 1961 1962 for (i = 0; i < VSC73XX_NUM_BUCKETS; i++) { 1963 ret = vsc73xx_write(vsc, VSC73XX_BLOCK_ANALYZER, 0, 1964 VSC73XX_MACTINDX, 1965 (i ? 0 : VSC73XX_MACTINDX_SHADOW) | 1966 FIELD_PREP(VSC73XX_MACTINDX_BUCKET_MSK, i) | 1967 index); 1968 if (ret) 1969 return ret; 1970 1971 ret = vsc73xx_port_wait_for_mac_table_cmd(vsc); 1972 if (ret) 1973 return ret; 1974 1975 ret = vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ANALYZER, 0, 1976 VSC73XX_MACACCESS, 1977 VSC73XX_MACACCESS_CMD_MASK, 1978 VSC73XX_MACACCESS_CMD_READ_ENTRY); 1979 if (ret) 1980 return ret; 1981 1982 ret = vsc73xx_port_wait_for_mac_table_cmd(vsc); 1983 if (ret) 1984 return ret; 1985 1986 ret = vsc73xx_read(vsc, VSC73XX_BLOCK_ANALYZER, 0, 1987 VSC73XX_MACACCESS, &val); 1988 if (ret) 1989 return ret; 1990 1991 fdb[i].valid = FIELD_GET(VSC73XX_MACACCESS_VALID, val); 1992 if (!fdb[i].valid) 1993 continue; 1994 1995 fdb[i].port = FIELD_GET(VSC73XX_MACACCESS_DEST_IDX_MASK, val); 1996 1997 ret = vsc73xx_read(vsc, VSC73XX_BLOCK_ANALYZER, 0, 1998 VSC73XX_MACHDATA, &val); 1999 if (ret) 2000 return ret; 2001 2002 fdb[i].vid = FIELD_GET(VSC73XX_MACHDATA_VID, val); 2003 fdb[i].mac[0] = FIELD_GET(VSC73XX_MACHDATA_MAC0, val); 2004 fdb[i].mac[1] = FIELD_GET(VSC73XX_MACHDATA_MAC1, val); 2005 2006 ret = vsc73xx_read(vsc, VSC73XX_BLOCK_ANALYZER, 0, 2007 VSC73XX_MACLDATA, &val); 2008 if (ret) 2009 return ret; 2010 2011 fdb[i].mac[2] = FIELD_GET(VSC73XX_MACLDATA_MAC2, val); 2012 fdb[i].mac[3] = FIELD_GET(VSC73XX_MACLDATA_MAC3, val); 2013 fdb[i].mac[4] = FIELD_GET(VSC73XX_MACLDATA_MAC4, val); 2014 fdb[i].mac[5] = FIELD_GET(VSC73XX_MACLDATA_MAC5, val); 2015 } 2016 2017 return ret; 2018 } 2019 2020 static int 2021 vsc73xx_fdb_operation(struct vsc73xx *vsc, const unsigned char *addr, u16 vid, 2022 u16 hash, u16 cmd_mask, u16 cmd_val) 2023 { 2024 int ret; 2025 u32 val; 2026 2027 val = FIELD_PREP(VSC73XX_MACHDATA_VID, vid) | 2028 FIELD_PREP(VSC73XX_MACHDATA_MAC0, addr[0]) | 2029 FIELD_PREP(VSC73XX_MACHDATA_MAC1, addr[1]); 2030 ret = vsc73xx_write(vsc, VSC73XX_BLOCK_ANALYZER, 0, VSC73XX_MACHDATA, 2031 val); 2032 if (ret) 2033 return ret; 2034 2035 val = FIELD_PREP(VSC73XX_MACLDATA_MAC2, addr[2]) | 2036 FIELD_PREP(VSC73XX_MACLDATA_MAC3, addr[3]) | 2037 FIELD_PREP(VSC73XX_MACLDATA_MAC4, addr[4]) | 2038 FIELD_PREP(VSC73XX_MACLDATA_MAC5, addr[5]); 2039 ret = vsc73xx_write(vsc, VSC73XX_BLOCK_ANALYZER, 0, VSC73XX_MACLDATA, 2040 val); 2041 if (ret) 2042 return ret; 2043 2044 ret = vsc73xx_write(vsc, VSC73XX_BLOCK_ANALYZER, 0, VSC73XX_MACTINDX, 2045 hash); 2046 if (ret) 2047 return ret; 2048 2049 ret = vsc73xx_port_wait_for_mac_table_cmd(vsc); 2050 if (ret) 2051 return ret; 2052 2053 ret = vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ANALYZER, 0, 2054 VSC73XX_MACACCESS, cmd_mask, cmd_val); 2055 if (ret) 2056 return ret; 2057 2058 return vsc73xx_port_wait_for_mac_table_cmd(vsc); 2059 } 2060 2061 static int vsc73xx_fdb_del_entry(struct vsc73xx *vsc, int port, 2062 const unsigned char *addr, u16 vid) 2063 { 2064 struct vsc73xx_fdb fdb[VSC73XX_NUM_BUCKETS]; 2065 u16 hash = vsc73xx_calc_hash(addr, vid); 2066 int bucket, ret; 2067 2068 mutex_lock(&vsc->fdb_lock); 2069 2070 ret = vsc73xx_port_read_mac_table_row(vsc, hash, fdb); 2071 if (ret) 2072 goto err; 2073 2074 for (bucket = 0; bucket < VSC73XX_NUM_BUCKETS; bucket++) { 2075 if (fdb[bucket].valid && fdb[bucket].port == port && 2076 ether_addr_equal(addr, fdb[bucket].mac)) 2077 break; 2078 } 2079 2080 if (bucket == VSC73XX_NUM_BUCKETS) { 2081 /* Can't find MAC in MAC table */ 2082 ret = -ENODATA; 2083 goto err; 2084 } 2085 2086 ret = vsc73xx_fdb_operation(vsc, addr, vid, hash, 2087 VSC73XX_MACACCESS_CMD_MASK, 2088 VSC73XX_MACACCESS_CMD_FORGET); 2089 err: 2090 mutex_unlock(&vsc->fdb_lock); 2091 return ret; 2092 } 2093 2094 static int vsc73xx_fdb_add_entry(struct vsc73xx *vsc, int port, 2095 const unsigned char *addr, u16 vid) 2096 { 2097 struct vsc73xx_fdb fdb[VSC73XX_NUM_BUCKETS]; 2098 u16 hash = vsc73xx_calc_hash(addr, vid); 2099 int bucket, ret; 2100 u32 val; 2101 2102 mutex_lock(&vsc->fdb_lock); 2103 2104 ret = vsc73xx_port_read_mac_table_row(vsc, hash, fdb); 2105 if (ret) 2106 goto err; 2107 2108 for (bucket = 0; bucket < VSC73XX_NUM_BUCKETS; bucket++) { 2109 if (!fdb[bucket].valid) 2110 break; 2111 } 2112 2113 if (bucket == VSC73XX_NUM_BUCKETS) { 2114 /* Bucket is full */ 2115 ret = -EOVERFLOW; 2116 goto err; 2117 } 2118 2119 val = VSC73XX_MACACCESS_VALID | VSC73XX_MACACCESS_LOCKED | 2120 FIELD_PREP(VSC73XX_MACACCESS_DEST_IDX_MASK, port) | 2121 VSC73XX_MACACCESS_CMD_LEARN; 2122 ret = vsc73xx_fdb_operation(vsc, addr, vid, hash, 2123 VSC73XX_MACACCESS_VALID | 2124 VSC73XX_MACACCESS_LOCKED | 2125 VSC73XX_MACACCESS_DEST_IDX_MASK | 2126 VSC73XX_MACACCESS_CMD_MASK, val); 2127 err: 2128 mutex_unlock(&vsc->fdb_lock); 2129 return ret; 2130 } 2131 2132 static int vsc73xx_fdb_add(struct dsa_switch *ds, int port, 2133 const unsigned char *addr, u16 vid, struct dsa_db db) 2134 { 2135 struct vsc73xx *vsc = ds->priv; 2136 2137 if (!vid) { 2138 switch (db.type) { 2139 case DSA_DB_PORT: 2140 vid = dsa_tag_8021q_standalone_vid(db.dp); 2141 break; 2142 case DSA_DB_BRIDGE: 2143 vid = dsa_tag_8021q_bridge_vid(db.bridge.num); 2144 break; 2145 default: 2146 return -EOPNOTSUPP; 2147 } 2148 } 2149 2150 return vsc73xx_fdb_add_entry(vsc, port, addr, vid); 2151 } 2152 2153 static int vsc73xx_fdb_del(struct dsa_switch *ds, int port, 2154 const unsigned char *addr, u16 vid, struct dsa_db db) 2155 { 2156 struct vsc73xx *vsc = ds->priv; 2157 2158 if (!vid) { 2159 switch (db.type) { 2160 case DSA_DB_PORT: 2161 vid = dsa_tag_8021q_standalone_vid(db.dp); 2162 break; 2163 case DSA_DB_BRIDGE: 2164 vid = dsa_tag_8021q_bridge_vid(db.bridge.num); 2165 break; 2166 default: 2167 return -EOPNOTSUPP; 2168 } 2169 } 2170 2171 return vsc73xx_fdb_del_entry(vsc, port, addr, vid); 2172 } 2173 2174 static int vsc73xx_port_fdb_dump(struct dsa_switch *ds, 2175 int port, dsa_fdb_dump_cb_t *cb, void *data) 2176 { 2177 struct vsc73xx_fdb fdb[VSC73XX_NUM_BUCKETS]; 2178 struct vsc73xx *vsc = ds->priv; 2179 u16 i, bucket; 2180 int err = 0; 2181 2182 mutex_lock(&vsc->fdb_lock); 2183 2184 for (i = 0; i < VSC73XX_NUM_FDB_ROWS; i++) { 2185 err = vsc73xx_port_read_mac_table_row(vsc, i, fdb); 2186 if (err) 2187 goto unlock; 2188 2189 for (bucket = 0; bucket < VSC73XX_NUM_BUCKETS; bucket++) { 2190 if (!fdb[bucket].valid || fdb[bucket].port != port) 2191 continue; 2192 2193 /* We need to hide dsa_8021q VLANs from the user */ 2194 if (vid_is_dsa_8021q(fdb[bucket].vid)) 2195 fdb[bucket].vid = 0; 2196 2197 err = cb(fdb[bucket].mac, fdb[bucket].vid, false, data); 2198 if (err) 2199 goto unlock; 2200 } 2201 } 2202 unlock: 2203 mutex_unlock(&vsc->fdb_lock); 2204 return err; 2205 } 2206 2207 static const struct phylink_mac_ops vsc73xx_phylink_mac_ops = { 2208 .mac_config = vsc73xx_mac_config, 2209 .mac_link_down = vsc73xx_mac_link_down, 2210 .mac_link_up = vsc73xx_mac_link_up, 2211 }; 2212 2213 static const struct dsa_switch_ops vsc73xx_ds_ops = { 2214 .get_tag_protocol = vsc73xx_get_tag_protocol, 2215 .setup = vsc73xx_setup, 2216 .teardown = vsc73xx_teardown, 2217 .phy_read = vsc73xx_phy_read, 2218 .phy_write = vsc73xx_phy_write, 2219 .get_strings = vsc73xx_get_strings, 2220 .get_ethtool_stats = vsc73xx_get_ethtool_stats, 2221 .get_sset_count = vsc73xx_get_sset_count, 2222 .port_enable = vsc73xx_port_enable, 2223 .port_disable = vsc73xx_port_disable, 2224 .port_pre_bridge_flags = vsc73xx_port_pre_bridge_flags, 2225 .port_bridge_flags = vsc73xx_port_bridge_flags, 2226 .port_bridge_join = dsa_tag_8021q_bridge_join, 2227 .port_bridge_leave = dsa_tag_8021q_bridge_leave, 2228 .port_change_mtu = vsc73xx_change_mtu, 2229 .port_fdb_add = vsc73xx_fdb_add, 2230 .port_fdb_del = vsc73xx_fdb_del, 2231 .port_fdb_dump = vsc73xx_port_fdb_dump, 2232 .port_max_mtu = vsc73xx_get_max_mtu, 2233 .port_stp_state_set = vsc73xx_port_stp_state_set, 2234 .port_vlan_filtering = vsc73xx_port_vlan_filtering, 2235 .port_vlan_add = vsc73xx_port_vlan_add, 2236 .port_vlan_del = vsc73xx_port_vlan_del, 2237 .phylink_get_caps = vsc73xx_phylink_get_caps, 2238 .tag_8021q_vlan_add = vsc73xx_tag_8021q_vlan_add, 2239 .tag_8021q_vlan_del = vsc73xx_tag_8021q_vlan_del, 2240 }; 2241 2242 static int vsc73xx_gpio_get(struct gpio_chip *chip, unsigned int offset) 2243 { 2244 struct vsc73xx *vsc = gpiochip_get_data(chip); 2245 u32 val; 2246 int ret; 2247 2248 ret = vsc73xx_read(vsc, VSC73XX_BLOCK_SYSTEM, 0, 2249 VSC73XX_GPIO, &val); 2250 if (ret) 2251 return ret; 2252 2253 return !!(val & BIT(offset)); 2254 } 2255 2256 static void vsc73xx_gpio_set(struct gpio_chip *chip, unsigned int offset, 2257 int val) 2258 { 2259 struct vsc73xx *vsc = gpiochip_get_data(chip); 2260 u32 tmp = val ? BIT(offset) : 0; 2261 2262 vsc73xx_update_bits(vsc, VSC73XX_BLOCK_SYSTEM, 0, 2263 VSC73XX_GPIO, BIT(offset), tmp); 2264 } 2265 2266 static int vsc73xx_gpio_direction_output(struct gpio_chip *chip, 2267 unsigned int offset, int val) 2268 { 2269 struct vsc73xx *vsc = gpiochip_get_data(chip); 2270 u32 tmp = val ? BIT(offset) : 0; 2271 2272 return vsc73xx_update_bits(vsc, VSC73XX_BLOCK_SYSTEM, 0, 2273 VSC73XX_GPIO, BIT(offset + 4) | BIT(offset), 2274 BIT(offset + 4) | tmp); 2275 } 2276 2277 static int vsc73xx_gpio_direction_input(struct gpio_chip *chip, 2278 unsigned int offset) 2279 { 2280 struct vsc73xx *vsc = gpiochip_get_data(chip); 2281 2282 return vsc73xx_update_bits(vsc, VSC73XX_BLOCK_SYSTEM, 0, 2283 VSC73XX_GPIO, BIT(offset + 4), 2284 0); 2285 } 2286 2287 static int vsc73xx_gpio_get_direction(struct gpio_chip *chip, 2288 unsigned int offset) 2289 { 2290 struct vsc73xx *vsc = gpiochip_get_data(chip); 2291 u32 val; 2292 int ret; 2293 2294 ret = vsc73xx_read(vsc, VSC73XX_BLOCK_SYSTEM, 0, 2295 VSC73XX_GPIO, &val); 2296 if (ret) 2297 return ret; 2298 2299 return !(val & BIT(offset + 4)); 2300 } 2301 2302 static int vsc73xx_gpio_probe(struct vsc73xx *vsc) 2303 { 2304 int ret; 2305 2306 vsc->gc.label = devm_kasprintf(vsc->dev, GFP_KERNEL, "VSC%04x", 2307 vsc->chipid); 2308 if (!vsc->gc.label) 2309 return -ENOMEM; 2310 vsc->gc.ngpio = 4; 2311 vsc->gc.owner = THIS_MODULE; 2312 vsc->gc.parent = vsc->dev; 2313 vsc->gc.base = -1; 2314 vsc->gc.get = vsc73xx_gpio_get; 2315 vsc->gc.set = vsc73xx_gpio_set; 2316 vsc->gc.direction_input = vsc73xx_gpio_direction_input; 2317 vsc->gc.direction_output = vsc73xx_gpio_direction_output; 2318 vsc->gc.get_direction = vsc73xx_gpio_get_direction; 2319 vsc->gc.can_sleep = true; 2320 ret = devm_gpiochip_add_data(vsc->dev, &vsc->gc, vsc); 2321 if (ret) { 2322 dev_err(vsc->dev, "unable to register GPIO chip\n"); 2323 return ret; 2324 } 2325 return 0; 2326 } 2327 2328 int vsc73xx_probe(struct vsc73xx *vsc) 2329 { 2330 struct device *dev = vsc->dev; 2331 int ret; 2332 2333 /* Release reset, if any */ 2334 vsc->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); 2335 if (IS_ERR(vsc->reset)) { 2336 dev_err(dev, "failed to get RESET GPIO\n"); 2337 return PTR_ERR(vsc->reset); 2338 } 2339 if (vsc->reset) 2340 /* Wait 20ms according to datasheet table 245 */ 2341 msleep(20); 2342 2343 ret = vsc73xx_detect(vsc); 2344 if (ret == -EAGAIN) { 2345 dev_err(vsc->dev, 2346 "Chip seems to be out of control. Assert reset and try again.\n"); 2347 gpiod_set_value_cansleep(vsc->reset, 1); 2348 /* Reset pulse should be 20ns minimum, according to datasheet 2349 * table 245, so 10us should be fine 2350 */ 2351 usleep_range(10, 100); 2352 gpiod_set_value_cansleep(vsc->reset, 0); 2353 /* Wait 20ms according to datasheet table 245 */ 2354 msleep(20); 2355 ret = vsc73xx_detect(vsc); 2356 } 2357 if (ret) { 2358 dev_err(dev, "no chip found (%d)\n", ret); 2359 return -ENODEV; 2360 } 2361 2362 mutex_init(&vsc->fdb_lock); 2363 2364 eth_random_addr(vsc->addr); 2365 dev_info(vsc->dev, 2366 "MAC for control frames: %02X:%02X:%02X:%02X:%02X:%02X\n", 2367 vsc->addr[0], vsc->addr[1], vsc->addr[2], 2368 vsc->addr[3], vsc->addr[4], vsc->addr[5]); 2369 2370 vsc->ds = devm_kzalloc(dev, sizeof(*vsc->ds), GFP_KERNEL); 2371 if (!vsc->ds) 2372 return -ENOMEM; 2373 2374 vsc->ds->dev = dev; 2375 vsc->ds->num_ports = VSC73XX_MAX_NUM_PORTS; 2376 vsc->ds->priv = vsc; 2377 2378 vsc->ds->ops = &vsc73xx_ds_ops; 2379 vsc->ds->phylink_mac_ops = &vsc73xx_phylink_mac_ops; 2380 ret = dsa_register_switch(vsc->ds); 2381 if (ret) { 2382 dev_err(dev, "unable to register switch (%d)\n", ret); 2383 return ret; 2384 } 2385 2386 ret = vsc73xx_gpio_probe(vsc); 2387 if (ret) { 2388 dsa_unregister_switch(vsc->ds); 2389 return ret; 2390 } 2391 2392 return 0; 2393 } 2394 EXPORT_SYMBOL(vsc73xx_probe); 2395 2396 void vsc73xx_remove(struct vsc73xx *vsc) 2397 { 2398 dsa_unregister_switch(vsc->ds); 2399 gpiod_set_value(vsc->reset, 1); 2400 } 2401 EXPORT_SYMBOL(vsc73xx_remove); 2402 2403 void vsc73xx_shutdown(struct vsc73xx *vsc) 2404 { 2405 dsa_switch_shutdown(vsc->ds); 2406 } 2407 EXPORT_SYMBOL(vsc73xx_shutdown); 2408 2409 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>"); 2410 MODULE_DESCRIPTION("Vitesse VSC7385/7388/7395/7398 driver"); 2411 MODULE_LICENSE("GPL v2"); 2412