Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * net/dsa/mv88e6xxx.c - Marvell 88e6xxx switch chip support |
| 3 | * Copyright (c) 2008 Marvell Semiconductor |
| 4 | * |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 5 | * Copyright (c) 2015 CMC Electronics, Inc. |
| 6 | * Added support for VLAN Table Unit operations |
| 7 | * |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | */ |
| 13 | |
Andrew Lunn | 87c8cef | 2015-06-20 18:42:28 +0200 | [diff] [blame] | 14 | #include <linux/debugfs.h> |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 15 | #include <linux/delay.h> |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 16 | #include <linux/etherdevice.h> |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 17 | #include <linux/ethtool.h> |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 18 | #include <linux/if_bridge.h> |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 19 | #include <linux/jiffies.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 20 | #include <linux/list.h> |
Paul Gortmaker | 2bbba27 | 2012-01-24 10:41:40 +0000 | [diff] [blame] | 21 | #include <linux/module.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 22 | #include <linux/netdevice.h> |
| 23 | #include <linux/phy.h> |
Andrew Lunn | 87c8cef | 2015-06-20 18:42:28 +0200 | [diff] [blame] | 24 | #include <linux/seq_file.h> |
Ben Hutchings | c8f0b86 | 2011-11-27 17:06:08 +0000 | [diff] [blame] | 25 | #include <net/dsa.h> |
Vivien Didelot | 1f36faf | 2015-10-08 11:35:13 -0400 | [diff] [blame] | 26 | #include <net/switchdev.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 27 | #include "mv88e6xxx.h" |
| 28 | |
Andrew Lunn | 16fe24f | 2015-05-06 01:09:55 +0200 | [diff] [blame] | 29 | /* MDIO bus access can be nested in the case of PHYs connected to the |
| 30 | * internal MDIO bus of the switch, which is accessed via MDIO bus of |
| 31 | * the Ethernet interface. Avoid lockdep false positives by using |
| 32 | * mutex_lock_nested(). |
| 33 | */ |
| 34 | static int mv88e6xxx_mdiobus_read(struct mii_bus *bus, int addr, u32 regnum) |
| 35 | { |
| 36 | int ret; |
| 37 | |
| 38 | mutex_lock_nested(&bus->mdio_lock, SINGLE_DEPTH_NESTING); |
| 39 | ret = bus->read(bus, addr, regnum); |
| 40 | mutex_unlock(&bus->mdio_lock); |
| 41 | |
| 42 | return ret; |
| 43 | } |
| 44 | |
| 45 | static int mv88e6xxx_mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, |
| 46 | u16 val) |
| 47 | { |
| 48 | int ret; |
| 49 | |
| 50 | mutex_lock_nested(&bus->mdio_lock, SINGLE_DEPTH_NESTING); |
| 51 | ret = bus->write(bus, addr, regnum, val); |
| 52 | mutex_unlock(&bus->mdio_lock); |
| 53 | |
| 54 | return ret; |
| 55 | } |
| 56 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 57 | /* If the switch's ADDR[4:0] strap pins are strapped to zero, it will |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 58 | * use all 32 SMI bus addresses on its SMI bus, and all switch registers |
| 59 | * will be directly accessible on some {device address,register address} |
| 60 | * pair. If the ADDR[4:0] pins are not strapped to zero, the switch |
| 61 | * will only respond to SMI transactions to that specific address, and |
| 62 | * an indirect addressing mechanism needs to be used to access its |
| 63 | * registers. |
| 64 | */ |
| 65 | static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr) |
| 66 | { |
| 67 | int ret; |
| 68 | int i; |
| 69 | |
| 70 | for (i = 0; i < 16; i++) { |
Andrew Lunn | 16fe24f | 2015-05-06 01:09:55 +0200 | [diff] [blame] | 71 | ret = mv88e6xxx_mdiobus_read(bus, sw_addr, SMI_CMD); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 72 | if (ret < 0) |
| 73 | return ret; |
| 74 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 75 | if ((ret & SMI_CMD_BUSY) == 0) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | return -ETIMEDOUT; |
| 80 | } |
| 81 | |
| 82 | int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr, int reg) |
| 83 | { |
| 84 | int ret; |
| 85 | |
| 86 | if (sw_addr == 0) |
Andrew Lunn | 16fe24f | 2015-05-06 01:09:55 +0200 | [diff] [blame] | 87 | return mv88e6xxx_mdiobus_read(bus, addr, reg); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 88 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 89 | /* Wait for the bus to become free. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 90 | ret = mv88e6xxx_reg_wait_ready(bus, sw_addr); |
| 91 | if (ret < 0) |
| 92 | return ret; |
| 93 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 94 | /* Transmit the read command. */ |
Andrew Lunn | 16fe24f | 2015-05-06 01:09:55 +0200 | [diff] [blame] | 95 | ret = mv88e6xxx_mdiobus_write(bus, sw_addr, SMI_CMD, |
| 96 | SMI_CMD_OP_22_READ | (addr << 5) | reg); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 97 | if (ret < 0) |
| 98 | return ret; |
| 99 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 100 | /* Wait for the read command to complete. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 101 | ret = mv88e6xxx_reg_wait_ready(bus, sw_addr); |
| 102 | if (ret < 0) |
| 103 | return ret; |
| 104 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 105 | /* Read the data. */ |
Andrew Lunn | 16fe24f | 2015-05-06 01:09:55 +0200 | [diff] [blame] | 106 | ret = mv88e6xxx_mdiobus_read(bus, sw_addr, SMI_DATA); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 107 | if (ret < 0) |
| 108 | return ret; |
| 109 | |
| 110 | return ret & 0xffff; |
| 111 | } |
| 112 | |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 113 | /* Must be called with SMI mutex held */ |
| 114 | static int _mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 115 | { |
Guenter Roeck | b184e49 | 2014-10-17 12:30:58 -0700 | [diff] [blame] | 116 | struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 117 | int ret; |
| 118 | |
Guenter Roeck | b184e49 | 2014-10-17 12:30:58 -0700 | [diff] [blame] | 119 | if (bus == NULL) |
| 120 | return -EINVAL; |
| 121 | |
Guenter Roeck | b184e49 | 2014-10-17 12:30:58 -0700 | [diff] [blame] | 122 | ret = __mv88e6xxx_reg_read(bus, ds->pd->sw_addr, addr, reg); |
Vivien Didelot | bb92ea5 | 2015-01-23 16:10:36 -0500 | [diff] [blame] | 123 | if (ret < 0) |
| 124 | return ret; |
| 125 | |
| 126 | dev_dbg(ds->master_dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n", |
| 127 | addr, reg, ret); |
| 128 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 129 | return ret; |
| 130 | } |
| 131 | |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 132 | int mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg) |
| 133 | { |
| 134 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 135 | int ret; |
| 136 | |
| 137 | mutex_lock(&ps->smi_mutex); |
| 138 | ret = _mv88e6xxx_reg_read(ds, addr, reg); |
| 139 | mutex_unlock(&ps->smi_mutex); |
| 140 | |
| 141 | return ret; |
| 142 | } |
| 143 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 144 | int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr, |
| 145 | int reg, u16 val) |
| 146 | { |
| 147 | int ret; |
| 148 | |
| 149 | if (sw_addr == 0) |
Andrew Lunn | 16fe24f | 2015-05-06 01:09:55 +0200 | [diff] [blame] | 150 | return mv88e6xxx_mdiobus_write(bus, addr, reg, val); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 151 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 152 | /* Wait for the bus to become free. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 153 | ret = mv88e6xxx_reg_wait_ready(bus, sw_addr); |
| 154 | if (ret < 0) |
| 155 | return ret; |
| 156 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 157 | /* Transmit the data to write. */ |
Andrew Lunn | 16fe24f | 2015-05-06 01:09:55 +0200 | [diff] [blame] | 158 | ret = mv88e6xxx_mdiobus_write(bus, sw_addr, SMI_DATA, val); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 159 | if (ret < 0) |
| 160 | return ret; |
| 161 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 162 | /* Transmit the write command. */ |
Andrew Lunn | 16fe24f | 2015-05-06 01:09:55 +0200 | [diff] [blame] | 163 | ret = mv88e6xxx_mdiobus_write(bus, sw_addr, SMI_CMD, |
| 164 | SMI_CMD_OP_22_WRITE | (addr << 5) | reg); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 165 | if (ret < 0) |
| 166 | return ret; |
| 167 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 168 | /* Wait for the write command to complete. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 169 | ret = mv88e6xxx_reg_wait_ready(bus, sw_addr); |
| 170 | if (ret < 0) |
| 171 | return ret; |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 176 | /* Must be called with SMI mutex held */ |
| 177 | static int _mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, |
| 178 | u16 val) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 179 | { |
Guenter Roeck | b184e49 | 2014-10-17 12:30:58 -0700 | [diff] [blame] | 180 | struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 181 | |
Guenter Roeck | b184e49 | 2014-10-17 12:30:58 -0700 | [diff] [blame] | 182 | if (bus == NULL) |
| 183 | return -EINVAL; |
| 184 | |
Vivien Didelot | bb92ea5 | 2015-01-23 16:10:36 -0500 | [diff] [blame] | 185 | dev_dbg(ds->master_dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n", |
| 186 | addr, reg, val); |
| 187 | |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 188 | return __mv88e6xxx_reg_write(bus, ds->pd->sw_addr, addr, reg, val); |
| 189 | } |
| 190 | |
| 191 | int mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, u16 val) |
| 192 | { |
| 193 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 194 | int ret; |
| 195 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 196 | mutex_lock(&ps->smi_mutex); |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 197 | ret = _mv88e6xxx_reg_write(ds, addr, reg, val); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 198 | mutex_unlock(&ps->smi_mutex); |
| 199 | |
| 200 | return ret; |
| 201 | } |
| 202 | |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 203 | int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr) |
| 204 | { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 205 | REG_WRITE(REG_GLOBAL, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]); |
| 206 | REG_WRITE(REG_GLOBAL, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]); |
| 207 | REG_WRITE(REG_GLOBAL, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 212 | int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr) |
| 213 | { |
| 214 | int i; |
| 215 | int ret; |
| 216 | |
| 217 | for (i = 0; i < 6; i++) { |
| 218 | int j; |
| 219 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 220 | /* Write the MAC address byte. */ |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 221 | REG_WRITE(REG_GLOBAL2, GLOBAL2_SWITCH_MAC, |
| 222 | GLOBAL2_SWITCH_MAC_BUSY | (i << 8) | addr[i]); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 223 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 224 | /* Wait for the write to complete. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 225 | for (j = 0; j < 16; j++) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 226 | ret = REG_READ(REG_GLOBAL2, GLOBAL2_SWITCH_MAC); |
| 227 | if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 228 | break; |
| 229 | } |
| 230 | if (j == 16) |
| 231 | return -ETIMEDOUT; |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 237 | /* Must be called with SMI mutex held */ |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 238 | static int _mv88e6xxx_phy_read(struct dsa_switch *ds, int addr, int regnum) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 239 | { |
| 240 | if (addr >= 0) |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 241 | return _mv88e6xxx_reg_read(ds, addr, regnum); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 242 | return 0xffff; |
| 243 | } |
| 244 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 245 | /* Must be called with SMI mutex held */ |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 246 | static int _mv88e6xxx_phy_write(struct dsa_switch *ds, int addr, int regnum, |
| 247 | u16 val) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 248 | { |
| 249 | if (addr >= 0) |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 250 | return _mv88e6xxx_reg_write(ds, addr, regnum, val); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 251 | return 0; |
| 252 | } |
| 253 | |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 254 | #ifdef CONFIG_NET_DSA_MV88E6XXX_NEED_PPU |
| 255 | static int mv88e6xxx_ppu_disable(struct dsa_switch *ds) |
| 256 | { |
| 257 | int ret; |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 258 | unsigned long timeout; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 259 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 260 | ret = REG_READ(REG_GLOBAL, GLOBAL_CONTROL); |
| 261 | REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL, |
| 262 | ret & ~GLOBAL_CONTROL_PPU_ENABLE); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 263 | |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 264 | timeout = jiffies + 1 * HZ; |
| 265 | while (time_before(jiffies, timeout)) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 266 | ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS); |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 267 | usleep_range(1000, 2000); |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 268 | if ((ret & GLOBAL_STATUS_PPU_MASK) != |
| 269 | GLOBAL_STATUS_PPU_POLLING) |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 270 | return 0; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | return -ETIMEDOUT; |
| 274 | } |
| 275 | |
| 276 | static int mv88e6xxx_ppu_enable(struct dsa_switch *ds) |
| 277 | { |
| 278 | int ret; |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 279 | unsigned long timeout; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 280 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 281 | ret = REG_READ(REG_GLOBAL, GLOBAL_CONTROL); |
| 282 | REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL, ret | GLOBAL_CONTROL_PPU_ENABLE); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 283 | |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 284 | timeout = jiffies + 1 * HZ; |
| 285 | while (time_before(jiffies, timeout)) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 286 | ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS); |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 287 | usleep_range(1000, 2000); |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 288 | if ((ret & GLOBAL_STATUS_PPU_MASK) == |
| 289 | GLOBAL_STATUS_PPU_POLLING) |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 290 | return 0; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | return -ETIMEDOUT; |
| 294 | } |
| 295 | |
| 296 | static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly) |
| 297 | { |
| 298 | struct mv88e6xxx_priv_state *ps; |
| 299 | |
| 300 | ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work); |
| 301 | if (mutex_trylock(&ps->ppu_mutex)) { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 302 | struct dsa_switch *ds = ((struct dsa_switch *)ps) - 1; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 303 | |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 304 | if (mv88e6xxx_ppu_enable(ds) == 0) |
| 305 | ps->ppu_disabled = 0; |
| 306 | mutex_unlock(&ps->ppu_mutex); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps) |
| 311 | { |
| 312 | struct mv88e6xxx_priv_state *ps = (void *)_ps; |
| 313 | |
| 314 | schedule_work(&ps->ppu_work); |
| 315 | } |
| 316 | |
| 317 | static int mv88e6xxx_ppu_access_get(struct dsa_switch *ds) |
| 318 | { |
Florian Fainelli | a22adce | 2014-04-28 11:14:28 -0700 | [diff] [blame] | 319 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 320 | int ret; |
| 321 | |
| 322 | mutex_lock(&ps->ppu_mutex); |
| 323 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 324 | /* If the PHY polling unit is enabled, disable it so that |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 325 | * we can access the PHY registers. If it was already |
| 326 | * disabled, cancel the timer that is going to re-enable |
| 327 | * it. |
| 328 | */ |
| 329 | if (!ps->ppu_disabled) { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 330 | ret = mv88e6xxx_ppu_disable(ds); |
| 331 | if (ret < 0) { |
| 332 | mutex_unlock(&ps->ppu_mutex); |
| 333 | return ret; |
| 334 | } |
| 335 | ps->ppu_disabled = 1; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 336 | } else { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 337 | del_timer(&ps->ppu_timer); |
| 338 | ret = 0; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | return ret; |
| 342 | } |
| 343 | |
| 344 | static void mv88e6xxx_ppu_access_put(struct dsa_switch *ds) |
| 345 | { |
Florian Fainelli | a22adce | 2014-04-28 11:14:28 -0700 | [diff] [blame] | 346 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 347 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 348 | /* Schedule a timer to re-enable the PHY polling unit. */ |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 349 | mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10)); |
| 350 | mutex_unlock(&ps->ppu_mutex); |
| 351 | } |
| 352 | |
| 353 | void mv88e6xxx_ppu_state_init(struct dsa_switch *ds) |
| 354 | { |
Florian Fainelli | a22adce | 2014-04-28 11:14:28 -0700 | [diff] [blame] | 355 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 356 | |
| 357 | mutex_init(&ps->ppu_mutex); |
| 358 | INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work); |
| 359 | init_timer(&ps->ppu_timer); |
| 360 | ps->ppu_timer.data = (unsigned long)ps; |
| 361 | ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer; |
| 362 | } |
| 363 | |
| 364 | int mv88e6xxx_phy_read_ppu(struct dsa_switch *ds, int addr, int regnum) |
| 365 | { |
| 366 | int ret; |
| 367 | |
| 368 | ret = mv88e6xxx_ppu_access_get(ds); |
| 369 | if (ret >= 0) { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 370 | ret = mv88e6xxx_reg_read(ds, addr, regnum); |
| 371 | mv88e6xxx_ppu_access_put(ds); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | return ret; |
| 375 | } |
| 376 | |
| 377 | int mv88e6xxx_phy_write_ppu(struct dsa_switch *ds, int addr, |
| 378 | int regnum, u16 val) |
| 379 | { |
| 380 | int ret; |
| 381 | |
| 382 | ret = mv88e6xxx_ppu_access_get(ds); |
| 383 | if (ret >= 0) { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 384 | ret = mv88e6xxx_reg_write(ds, addr, regnum, val); |
| 385 | mv88e6xxx_ppu_access_put(ds); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | return ret; |
| 389 | } |
| 390 | #endif |
| 391 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 392 | static bool mv88e6xxx_6065_family(struct dsa_switch *ds) |
| 393 | { |
| 394 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 395 | |
| 396 | switch (ps->id) { |
| 397 | case PORT_SWITCH_ID_6031: |
| 398 | case PORT_SWITCH_ID_6061: |
| 399 | case PORT_SWITCH_ID_6035: |
| 400 | case PORT_SWITCH_ID_6065: |
| 401 | return true; |
| 402 | } |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | static bool mv88e6xxx_6095_family(struct dsa_switch *ds) |
| 407 | { |
| 408 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 409 | |
| 410 | switch (ps->id) { |
| 411 | case PORT_SWITCH_ID_6092: |
| 412 | case PORT_SWITCH_ID_6095: |
| 413 | return true; |
| 414 | } |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | static bool mv88e6xxx_6097_family(struct dsa_switch *ds) |
| 419 | { |
| 420 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 421 | |
| 422 | switch (ps->id) { |
| 423 | case PORT_SWITCH_ID_6046: |
| 424 | case PORT_SWITCH_ID_6085: |
| 425 | case PORT_SWITCH_ID_6096: |
| 426 | case PORT_SWITCH_ID_6097: |
| 427 | return true; |
| 428 | } |
| 429 | return false; |
| 430 | } |
| 431 | |
| 432 | static bool mv88e6xxx_6165_family(struct dsa_switch *ds) |
| 433 | { |
| 434 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 435 | |
| 436 | switch (ps->id) { |
| 437 | case PORT_SWITCH_ID_6123: |
| 438 | case PORT_SWITCH_ID_6161: |
| 439 | case PORT_SWITCH_ID_6165: |
| 440 | return true; |
| 441 | } |
| 442 | return false; |
| 443 | } |
| 444 | |
| 445 | static bool mv88e6xxx_6185_family(struct dsa_switch *ds) |
| 446 | { |
| 447 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 448 | |
| 449 | switch (ps->id) { |
| 450 | case PORT_SWITCH_ID_6121: |
| 451 | case PORT_SWITCH_ID_6122: |
| 452 | case PORT_SWITCH_ID_6152: |
| 453 | case PORT_SWITCH_ID_6155: |
| 454 | case PORT_SWITCH_ID_6182: |
| 455 | case PORT_SWITCH_ID_6185: |
| 456 | case PORT_SWITCH_ID_6108: |
| 457 | case PORT_SWITCH_ID_6131: |
| 458 | return true; |
| 459 | } |
| 460 | return false; |
| 461 | } |
| 462 | |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 463 | static bool mv88e6xxx_6320_family(struct dsa_switch *ds) |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 464 | { |
| 465 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 466 | |
| 467 | switch (ps->id) { |
| 468 | case PORT_SWITCH_ID_6320: |
| 469 | case PORT_SWITCH_ID_6321: |
| 470 | return true; |
| 471 | } |
| 472 | return false; |
| 473 | } |
| 474 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 475 | static bool mv88e6xxx_6351_family(struct dsa_switch *ds) |
| 476 | { |
| 477 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 478 | |
| 479 | switch (ps->id) { |
| 480 | case PORT_SWITCH_ID_6171: |
| 481 | case PORT_SWITCH_ID_6175: |
| 482 | case PORT_SWITCH_ID_6350: |
| 483 | case PORT_SWITCH_ID_6351: |
| 484 | return true; |
| 485 | } |
| 486 | return false; |
| 487 | } |
| 488 | |
Andrew Lunn | f3a8b6b | 2015-04-02 04:06:40 +0200 | [diff] [blame] | 489 | static bool mv88e6xxx_6352_family(struct dsa_switch *ds) |
| 490 | { |
| 491 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 492 | |
| 493 | switch (ps->id) { |
Andrew Lunn | f3a8b6b | 2015-04-02 04:06:40 +0200 | [diff] [blame] | 494 | case PORT_SWITCH_ID_6172: |
| 495 | case PORT_SWITCH_ID_6176: |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 496 | case PORT_SWITCH_ID_6240: |
| 497 | case PORT_SWITCH_ID_6352: |
Andrew Lunn | f3a8b6b | 2015-04-02 04:06:40 +0200 | [diff] [blame] | 498 | return true; |
| 499 | } |
| 500 | return false; |
| 501 | } |
| 502 | |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 503 | /* We expect the switch to perform auto negotiation if there is a real |
| 504 | * phy. However, in the case of a fixed link phy, we force the port |
| 505 | * settings from the fixed link settings. |
| 506 | */ |
| 507 | void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port, |
| 508 | struct phy_device *phydev) |
| 509 | { |
| 510 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Andrew Lunn | 4905287 | 2015-09-29 01:53:48 +0200 | [diff] [blame] | 511 | u32 reg; |
| 512 | int ret; |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 513 | |
| 514 | if (!phy_is_pseudo_fixed_link(phydev)) |
| 515 | return; |
| 516 | |
| 517 | mutex_lock(&ps->smi_mutex); |
| 518 | |
| 519 | ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_PCS_CTRL); |
| 520 | if (ret < 0) |
| 521 | goto out; |
| 522 | |
| 523 | reg = ret & ~(PORT_PCS_CTRL_LINK_UP | |
| 524 | PORT_PCS_CTRL_FORCE_LINK | |
| 525 | PORT_PCS_CTRL_DUPLEX_FULL | |
| 526 | PORT_PCS_CTRL_FORCE_DUPLEX | |
| 527 | PORT_PCS_CTRL_UNFORCED); |
| 528 | |
| 529 | reg |= PORT_PCS_CTRL_FORCE_LINK; |
| 530 | if (phydev->link) |
| 531 | reg |= PORT_PCS_CTRL_LINK_UP; |
| 532 | |
| 533 | if (mv88e6xxx_6065_family(ds) && phydev->speed > SPEED_100) |
| 534 | goto out; |
| 535 | |
| 536 | switch (phydev->speed) { |
| 537 | case SPEED_1000: |
| 538 | reg |= PORT_PCS_CTRL_1000; |
| 539 | break; |
| 540 | case SPEED_100: |
| 541 | reg |= PORT_PCS_CTRL_100; |
| 542 | break; |
| 543 | case SPEED_10: |
| 544 | reg |= PORT_PCS_CTRL_10; |
| 545 | break; |
| 546 | default: |
| 547 | pr_info("Unknown speed"); |
| 548 | goto out; |
| 549 | } |
| 550 | |
| 551 | reg |= PORT_PCS_CTRL_FORCE_DUPLEX; |
| 552 | if (phydev->duplex == DUPLEX_FULL) |
| 553 | reg |= PORT_PCS_CTRL_DUPLEX_FULL; |
| 554 | |
Andrew Lunn | e7e72ac | 2015-08-31 15:56:51 +0200 | [diff] [blame] | 555 | if ((mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds)) && |
| 556 | (port >= ps->num_ports - 2)) { |
| 557 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) |
| 558 | reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK; |
| 559 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) |
| 560 | reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK; |
| 561 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) |
| 562 | reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK | |
| 563 | PORT_PCS_CTRL_RGMII_DELAY_TXCLK); |
| 564 | } |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 565 | _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_PCS_CTRL, reg); |
| 566 | |
| 567 | out: |
| 568 | mutex_unlock(&ps->smi_mutex); |
| 569 | } |
| 570 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 571 | /* Must be called with SMI mutex held */ |
| 572 | static int _mv88e6xxx_stats_wait(struct dsa_switch *ds) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 573 | { |
| 574 | int ret; |
| 575 | int i; |
| 576 | |
| 577 | for (i = 0; i < 10; i++) { |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 578 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_OP); |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 579 | if ((ret & GLOBAL_STATS_OP_BUSY) == 0) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | return -ETIMEDOUT; |
| 584 | } |
| 585 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 586 | /* Must be called with SMI mutex held */ |
| 587 | static int _mv88e6xxx_stats_snapshot(struct dsa_switch *ds, int port) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 588 | { |
| 589 | int ret; |
| 590 | |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 591 | if (mv88e6xxx_6320_family(ds) || mv88e6xxx_6352_family(ds)) |
Andrew Lunn | f3a8b6b | 2015-04-02 04:06:40 +0200 | [diff] [blame] | 592 | port = (port + 1) << 5; |
| 593 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 594 | /* Snapshot the hardware statistics counters for this port. */ |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 595 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP, |
| 596 | GLOBAL_STATS_OP_CAPTURE_PORT | |
| 597 | GLOBAL_STATS_OP_HIST_RX_TX | port); |
| 598 | if (ret < 0) |
| 599 | return ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 600 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 601 | /* Wait for the snapshotting to complete. */ |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 602 | ret = _mv88e6xxx_stats_wait(ds); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 603 | if (ret < 0) |
| 604 | return ret; |
| 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 609 | /* Must be called with SMI mutex held */ |
| 610 | static void _mv88e6xxx_stats_read(struct dsa_switch *ds, int stat, u32 *val) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 611 | { |
| 612 | u32 _val; |
| 613 | int ret; |
| 614 | |
| 615 | *val = 0; |
| 616 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 617 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP, |
| 618 | GLOBAL_STATS_OP_READ_CAPTURED | |
| 619 | GLOBAL_STATS_OP_HIST_RX_TX | stat); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 620 | if (ret < 0) |
| 621 | return; |
| 622 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 623 | ret = _mv88e6xxx_stats_wait(ds); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 624 | if (ret < 0) |
| 625 | return; |
| 626 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 627 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_32); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 628 | if (ret < 0) |
| 629 | return; |
| 630 | |
| 631 | _val = ret << 16; |
| 632 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 633 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_01); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 634 | if (ret < 0) |
| 635 | return; |
| 636 | |
| 637 | *val = _val | ret; |
| 638 | } |
| 639 | |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 640 | static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = { |
| 641 | { "in_good_octets", 8, 0x00, }, |
| 642 | { "in_bad_octets", 4, 0x02, }, |
| 643 | { "in_unicast", 4, 0x04, }, |
| 644 | { "in_broadcasts", 4, 0x06, }, |
| 645 | { "in_multicasts", 4, 0x07, }, |
| 646 | { "in_pause", 4, 0x16, }, |
| 647 | { "in_undersize", 4, 0x18, }, |
| 648 | { "in_fragments", 4, 0x19, }, |
| 649 | { "in_oversize", 4, 0x1a, }, |
| 650 | { "in_jabber", 4, 0x1b, }, |
| 651 | { "in_rx_error", 4, 0x1c, }, |
| 652 | { "in_fcs_error", 4, 0x1d, }, |
| 653 | { "out_octets", 8, 0x0e, }, |
| 654 | { "out_unicast", 4, 0x10, }, |
| 655 | { "out_broadcasts", 4, 0x13, }, |
| 656 | { "out_multicasts", 4, 0x12, }, |
| 657 | { "out_pause", 4, 0x15, }, |
| 658 | { "excessive", 4, 0x11, }, |
| 659 | { "collisions", 4, 0x1e, }, |
| 660 | { "deferred", 4, 0x05, }, |
| 661 | { "single", 4, 0x14, }, |
| 662 | { "multiple", 4, 0x17, }, |
| 663 | { "out_fcs_error", 4, 0x03, }, |
| 664 | { "late", 4, 0x1f, }, |
| 665 | { "hist_64bytes", 4, 0x08, }, |
| 666 | { "hist_65_127bytes", 4, 0x09, }, |
| 667 | { "hist_128_255bytes", 4, 0x0a, }, |
| 668 | { "hist_256_511bytes", 4, 0x0b, }, |
| 669 | { "hist_512_1023bytes", 4, 0x0c, }, |
| 670 | { "hist_1024_max_bytes", 4, 0x0d, }, |
| 671 | /* Not all devices have the following counters */ |
| 672 | { "sw_in_discards", 4, 0x110, }, |
| 673 | { "sw_in_filtered", 2, 0x112, }, |
| 674 | { "sw_out_filtered", 2, 0x113, }, |
| 675 | |
| 676 | }; |
| 677 | |
| 678 | static bool have_sw_in_discards(struct dsa_switch *ds) |
| 679 | { |
| 680 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 681 | |
| 682 | switch (ps->id) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 683 | case PORT_SWITCH_ID_6095: case PORT_SWITCH_ID_6161: |
| 684 | case PORT_SWITCH_ID_6165: case PORT_SWITCH_ID_6171: |
| 685 | case PORT_SWITCH_ID_6172: case PORT_SWITCH_ID_6176: |
| 686 | case PORT_SWITCH_ID_6182: case PORT_SWITCH_ID_6185: |
| 687 | case PORT_SWITCH_ID_6352: |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 688 | return true; |
| 689 | default: |
| 690 | return false; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | static void _mv88e6xxx_get_strings(struct dsa_switch *ds, |
| 695 | int nr_stats, |
| 696 | struct mv88e6xxx_hw_stat *stats, |
| 697 | int port, uint8_t *data) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 698 | { |
| 699 | int i; |
| 700 | |
| 701 | for (i = 0; i < nr_stats; i++) { |
| 702 | memcpy(data + i * ETH_GSTRING_LEN, |
| 703 | stats[i].string, ETH_GSTRING_LEN); |
| 704 | } |
| 705 | } |
| 706 | |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 707 | static uint64_t _mv88e6xxx_get_ethtool_stat(struct dsa_switch *ds, |
| 708 | int stat, |
| 709 | struct mv88e6xxx_hw_stat *stats, |
| 710 | int port) |
| 711 | { |
| 712 | struct mv88e6xxx_hw_stat *s = stats + stat; |
| 713 | u32 low; |
| 714 | u32 high = 0; |
| 715 | int ret; |
| 716 | u64 value; |
| 717 | |
| 718 | if (s->reg >= 0x100) { |
| 719 | ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), |
| 720 | s->reg - 0x100); |
| 721 | if (ret < 0) |
| 722 | return UINT64_MAX; |
| 723 | |
| 724 | low = ret; |
| 725 | if (s->sizeof_stat == 4) { |
| 726 | ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), |
| 727 | s->reg - 0x100 + 1); |
| 728 | if (ret < 0) |
| 729 | return UINT64_MAX; |
| 730 | high = ret; |
| 731 | } |
| 732 | } else { |
| 733 | _mv88e6xxx_stats_read(ds, s->reg, &low); |
| 734 | if (s->sizeof_stat == 8) |
| 735 | _mv88e6xxx_stats_read(ds, s->reg + 1, &high); |
| 736 | } |
| 737 | value = (((u64)high) << 16) | low; |
| 738 | return value; |
| 739 | } |
| 740 | |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 741 | static void _mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, |
| 742 | int nr_stats, |
| 743 | struct mv88e6xxx_hw_stat *stats, |
| 744 | int port, uint64_t *data) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 745 | { |
Florian Fainelli | a22adce | 2014-04-28 11:14:28 -0700 | [diff] [blame] | 746 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 747 | int ret; |
| 748 | int i; |
| 749 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 750 | mutex_lock(&ps->smi_mutex); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 751 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 752 | ret = _mv88e6xxx_stats_snapshot(ds, port); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 753 | if (ret < 0) { |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 754 | mutex_unlock(&ps->smi_mutex); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 755 | return; |
| 756 | } |
| 757 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 758 | /* Read each of the counters. */ |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 759 | for (i = 0; i < nr_stats; i++) |
| 760 | data[i] = _mv88e6xxx_get_ethtool_stat(ds, i, stats, port); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 761 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 762 | mutex_unlock(&ps->smi_mutex); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 763 | } |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 764 | |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 765 | /* All the statistics in the table */ |
| 766 | void |
| 767 | mv88e6xxx_get_strings(struct dsa_switch *ds, int port, uint8_t *data) |
| 768 | { |
| 769 | if (have_sw_in_discards(ds)) |
| 770 | _mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6xxx_hw_stats), |
| 771 | mv88e6xxx_hw_stats, port, data); |
| 772 | else |
| 773 | _mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6xxx_hw_stats) - 3, |
| 774 | mv88e6xxx_hw_stats, port, data); |
| 775 | } |
| 776 | |
| 777 | int mv88e6xxx_get_sset_count(struct dsa_switch *ds) |
| 778 | { |
| 779 | if (have_sw_in_discards(ds)) |
| 780 | return ARRAY_SIZE(mv88e6xxx_hw_stats); |
| 781 | return ARRAY_SIZE(mv88e6xxx_hw_stats) - 3; |
| 782 | } |
| 783 | |
| 784 | void |
| 785 | mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, |
| 786 | int port, uint64_t *data) |
| 787 | { |
| 788 | if (have_sw_in_discards(ds)) |
| 789 | _mv88e6xxx_get_ethtool_stats( |
| 790 | ds, ARRAY_SIZE(mv88e6xxx_hw_stats), |
| 791 | mv88e6xxx_hw_stats, port, data); |
| 792 | else |
| 793 | _mv88e6xxx_get_ethtool_stats( |
| 794 | ds, ARRAY_SIZE(mv88e6xxx_hw_stats) - 3, |
| 795 | mv88e6xxx_hw_stats, port, data); |
| 796 | } |
| 797 | |
Guenter Roeck | a1ab91f | 2014-10-29 10:45:05 -0700 | [diff] [blame] | 798 | int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port) |
| 799 | { |
| 800 | return 32 * sizeof(u16); |
| 801 | } |
| 802 | |
| 803 | void mv88e6xxx_get_regs(struct dsa_switch *ds, int port, |
| 804 | struct ethtool_regs *regs, void *_p) |
| 805 | { |
| 806 | u16 *p = _p; |
| 807 | int i; |
| 808 | |
| 809 | regs->version = 0; |
| 810 | |
| 811 | memset(p, 0xff, 32 * sizeof(u16)); |
| 812 | |
| 813 | for (i = 0; i < 32; i++) { |
| 814 | int ret; |
| 815 | |
| 816 | ret = mv88e6xxx_reg_read(ds, REG_PORT(port), i); |
| 817 | if (ret >= 0) |
| 818 | p[i] = ret; |
| 819 | } |
| 820 | } |
| 821 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 822 | /* Must be called with SMI lock held */ |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 823 | static int _mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset, |
| 824 | u16 mask) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 825 | { |
| 826 | unsigned long timeout = jiffies + HZ / 10; |
| 827 | |
| 828 | while (time_before(jiffies, timeout)) { |
| 829 | int ret; |
| 830 | |
| 831 | ret = _mv88e6xxx_reg_read(ds, reg, offset); |
| 832 | if (ret < 0) |
| 833 | return ret; |
| 834 | if (!(ret & mask)) |
| 835 | return 0; |
| 836 | |
| 837 | usleep_range(1000, 2000); |
| 838 | } |
| 839 | return -ETIMEDOUT; |
| 840 | } |
| 841 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 842 | static int mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset, u16 mask) |
| 843 | { |
| 844 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 845 | int ret; |
| 846 | |
| 847 | mutex_lock(&ps->smi_mutex); |
| 848 | ret = _mv88e6xxx_wait(ds, reg, offset, mask); |
| 849 | mutex_unlock(&ps->smi_mutex); |
| 850 | |
| 851 | return ret; |
| 852 | } |
| 853 | |
| 854 | static int _mv88e6xxx_phy_wait(struct dsa_switch *ds) |
| 855 | { |
| 856 | return _mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_SMI_OP, |
| 857 | GLOBAL2_SMI_OP_BUSY); |
| 858 | } |
| 859 | |
| 860 | int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds) |
| 861 | { |
| 862 | return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP, |
| 863 | GLOBAL2_EEPROM_OP_LOAD); |
| 864 | } |
| 865 | |
| 866 | int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds) |
| 867 | { |
| 868 | return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP, |
| 869 | GLOBAL2_EEPROM_OP_BUSY); |
| 870 | } |
| 871 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 872 | /* Must be called with SMI lock held */ |
| 873 | static int _mv88e6xxx_atu_wait(struct dsa_switch *ds) |
| 874 | { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 875 | return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_ATU_OP, |
| 876 | GLOBAL_ATU_OP_BUSY); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 877 | } |
| 878 | |
Andrew Lunn | 56d95e2 | 2015-06-20 18:42:33 +0200 | [diff] [blame] | 879 | /* Must be called with SMI lock held */ |
| 880 | static int _mv88e6xxx_scratch_wait(struct dsa_switch *ds) |
| 881 | { |
| 882 | return _mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_SCRATCH_MISC, |
| 883 | GLOBAL2_SCRATCH_BUSY); |
| 884 | } |
| 885 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 886 | /* Must be called with SMI mutex held */ |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 887 | static int _mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int addr, |
| 888 | int regnum) |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 889 | { |
| 890 | int ret; |
| 891 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 892 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP, |
| 893 | GLOBAL2_SMI_OP_22_READ | (addr << 5) | |
| 894 | regnum); |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 895 | if (ret < 0) |
| 896 | return ret; |
| 897 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 898 | ret = _mv88e6xxx_phy_wait(ds); |
| 899 | if (ret < 0) |
| 900 | return ret; |
| 901 | |
| 902 | return _mv88e6xxx_reg_read(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA); |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 903 | } |
| 904 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 905 | /* Must be called with SMI mutex held */ |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 906 | static int _mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int addr, |
| 907 | int regnum, u16 val) |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 908 | { |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 909 | int ret; |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 910 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 911 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA, val); |
| 912 | if (ret < 0) |
| 913 | return ret; |
| 914 | |
| 915 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP, |
| 916 | GLOBAL2_SMI_OP_22_WRITE | (addr << 5) | |
| 917 | regnum); |
| 918 | |
| 919 | return _mv88e6xxx_phy_wait(ds); |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 920 | } |
| 921 | |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 922 | int mv88e6xxx_get_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e) |
| 923 | { |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 924 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 925 | int reg; |
| 926 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 927 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 928 | |
| 929 | reg = _mv88e6xxx_phy_read_indirect(ds, port, 16); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 930 | if (reg < 0) |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 931 | goto out; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 932 | |
| 933 | e->eee_enabled = !!(reg & 0x0200); |
| 934 | e->tx_lpi_enabled = !!(reg & 0x0100); |
| 935 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 936 | reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_STATUS); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 937 | if (reg < 0) |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 938 | goto out; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 939 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 940 | e->eee_active = !!(reg & PORT_STATUS_EEE); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 941 | reg = 0; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 942 | |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 943 | out: |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 944 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 945 | return reg; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | int mv88e6xxx_set_eee(struct dsa_switch *ds, int port, |
| 949 | struct phy_device *phydev, struct ethtool_eee *e) |
| 950 | { |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 951 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 952 | int reg; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 953 | int ret; |
| 954 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 955 | mutex_lock(&ps->smi_mutex); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 956 | |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 957 | ret = _mv88e6xxx_phy_read_indirect(ds, port, 16); |
| 958 | if (ret < 0) |
| 959 | goto out; |
| 960 | |
| 961 | reg = ret & ~0x0300; |
| 962 | if (e->eee_enabled) |
| 963 | reg |= 0x0200; |
| 964 | if (e->tx_lpi_enabled) |
| 965 | reg |= 0x0100; |
| 966 | |
| 967 | ret = _mv88e6xxx_phy_write_indirect(ds, port, 16, reg); |
| 968 | out: |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 969 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 970 | |
| 971 | return ret; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 972 | } |
| 973 | |
Vivien Didelot | 70cc99d | 2015-09-04 14:34:10 -0400 | [diff] [blame] | 974 | static int _mv88e6xxx_atu_cmd(struct dsa_switch *ds, u16 cmd) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 975 | { |
| 976 | int ret; |
| 977 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 978 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_OP, cmd); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 979 | if (ret < 0) |
| 980 | return ret; |
| 981 | |
| 982 | return _mv88e6xxx_atu_wait(ds); |
| 983 | } |
| 984 | |
Vivien Didelot | 37705b7 | 2015-09-04 14:34:11 -0400 | [diff] [blame] | 985 | static int _mv88e6xxx_atu_data_write(struct dsa_switch *ds, |
| 986 | struct mv88e6xxx_atu_entry *entry) |
| 987 | { |
| 988 | u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK; |
| 989 | |
| 990 | if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) { |
| 991 | unsigned int mask, shift; |
| 992 | |
| 993 | if (entry->trunk) { |
| 994 | data |= GLOBAL_ATU_DATA_TRUNK; |
| 995 | mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK; |
| 996 | shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT; |
| 997 | } else { |
| 998 | mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK; |
| 999 | shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT; |
| 1000 | } |
| 1001 | |
| 1002 | data |= (entry->portv_trunkid << shift) & mask; |
| 1003 | } |
| 1004 | |
| 1005 | return _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_DATA, data); |
| 1006 | } |
| 1007 | |
Vivien Didelot | 7fb5e75 | 2015-09-04 14:34:12 -0400 | [diff] [blame] | 1008 | static int _mv88e6xxx_atu_flush_move(struct dsa_switch *ds, |
| 1009 | struct mv88e6xxx_atu_entry *entry, |
| 1010 | bool static_too) |
| 1011 | { |
| 1012 | int op; |
| 1013 | int err; |
| 1014 | |
| 1015 | err = _mv88e6xxx_atu_wait(ds); |
| 1016 | if (err) |
| 1017 | return err; |
| 1018 | |
| 1019 | err = _mv88e6xxx_atu_data_write(ds, entry); |
| 1020 | if (err) |
| 1021 | return err; |
| 1022 | |
| 1023 | if (entry->fid) { |
| 1024 | err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID, |
| 1025 | entry->fid); |
| 1026 | if (err) |
| 1027 | return err; |
| 1028 | |
| 1029 | op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB : |
| 1030 | GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB; |
| 1031 | } else { |
| 1032 | op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL : |
| 1033 | GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC; |
| 1034 | } |
| 1035 | |
| 1036 | return _mv88e6xxx_atu_cmd(ds, op); |
| 1037 | } |
| 1038 | |
| 1039 | static int _mv88e6xxx_atu_flush(struct dsa_switch *ds, u16 fid, bool static_too) |
| 1040 | { |
| 1041 | struct mv88e6xxx_atu_entry entry = { |
| 1042 | .fid = fid, |
| 1043 | .state = 0, /* EntryState bits must be 0 */ |
| 1044 | }; |
| 1045 | |
| 1046 | return _mv88e6xxx_atu_flush_move(ds, &entry, static_too); |
| 1047 | } |
| 1048 | |
Vivien Didelot | 9f4d55d | 2015-09-04 14:34:15 -0400 | [diff] [blame] | 1049 | static int _mv88e6xxx_atu_move(struct dsa_switch *ds, u16 fid, int from_port, |
| 1050 | int to_port, bool static_too) |
| 1051 | { |
| 1052 | struct mv88e6xxx_atu_entry entry = { |
| 1053 | .trunk = false, |
| 1054 | .fid = fid, |
| 1055 | }; |
| 1056 | |
| 1057 | /* EntryState bits must be 0xF */ |
| 1058 | entry.state = GLOBAL_ATU_DATA_STATE_MASK; |
| 1059 | |
| 1060 | /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */ |
| 1061 | entry.portv_trunkid = (to_port & 0x0f) << 4; |
| 1062 | entry.portv_trunkid |= from_port & 0x0f; |
| 1063 | |
| 1064 | return _mv88e6xxx_atu_flush_move(ds, &entry, static_too); |
| 1065 | } |
| 1066 | |
| 1067 | static int _mv88e6xxx_atu_remove(struct dsa_switch *ds, u16 fid, int port, |
| 1068 | bool static_too) |
| 1069 | { |
| 1070 | /* Destination port 0xF means remove the entries */ |
| 1071 | return _mv88e6xxx_atu_move(ds, fid, port, 0x0f, static_too); |
| 1072 | } |
| 1073 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1074 | static int mv88e6xxx_set_port_state(struct dsa_switch *ds, int port, u8 state) |
| 1075 | { |
| 1076 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Geert Uytterhoeven | c3ffe6d | 2015-04-16 20:49:14 +0200 | [diff] [blame] | 1077 | int reg, ret = 0; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1078 | u8 oldstate; |
| 1079 | |
| 1080 | mutex_lock(&ps->smi_mutex); |
| 1081 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1082 | reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL); |
Guenter Roeck | 538cc28 | 2015-04-15 22:12:42 -0700 | [diff] [blame] | 1083 | if (reg < 0) { |
| 1084 | ret = reg; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1085 | goto abort; |
Guenter Roeck | 538cc28 | 2015-04-15 22:12:42 -0700 | [diff] [blame] | 1086 | } |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1087 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1088 | oldstate = reg & PORT_CONTROL_STATE_MASK; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1089 | if (oldstate != state) { |
| 1090 | /* Flush forwarding database if we're moving a port |
| 1091 | * from Learning or Forwarding state to Disabled or |
| 1092 | * Blocking or Listening state. |
| 1093 | */ |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1094 | if (oldstate >= PORT_CONTROL_STATE_LEARNING && |
| 1095 | state <= PORT_CONTROL_STATE_BLOCKING) { |
Vivien Didelot | 2b8157b | 2015-09-04 14:34:16 -0400 | [diff] [blame] | 1096 | ret = _mv88e6xxx_atu_remove(ds, 0, port, false); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1097 | if (ret) |
| 1098 | goto abort; |
| 1099 | } |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1100 | reg = (reg & ~PORT_CONTROL_STATE_MASK) | state; |
| 1101 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL, |
| 1102 | reg); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | abort: |
| 1106 | mutex_unlock(&ps->smi_mutex); |
| 1107 | return ret; |
| 1108 | } |
| 1109 | |
Vivien Didelot | ede8098 | 2015-10-11 18:08:35 -0400 | [diff] [blame^] | 1110 | static int _mv88e6xxx_port_vlan_map_set(struct dsa_switch *ds, int port, |
| 1111 | u16 output_ports) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1112 | { |
| 1113 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | ede8098 | 2015-10-11 18:08:35 -0400 | [diff] [blame^] | 1114 | const u16 mask = (1 << ps->num_ports) - 1; |
| 1115 | int reg; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1116 | |
Vivien Didelot | ede8098 | 2015-10-11 18:08:35 -0400 | [diff] [blame^] | 1117 | reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_BASE_VLAN); |
| 1118 | if (reg < 0) |
| 1119 | return reg; |
| 1120 | |
| 1121 | reg &= ~mask; |
| 1122 | reg |= output_ports & mask; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1123 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1124 | return _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_BASE_VLAN, reg); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1127 | /* Bridge handling functions */ |
| 1128 | |
Vivien Didelot | ede8098 | 2015-10-11 18:08:35 -0400 | [diff] [blame^] | 1129 | static int mv88e6xxx_map_bridge(struct dsa_switch *ds, u16 members) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1130 | { |
| 1131 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | ede8098 | 2015-10-11 18:08:35 -0400 | [diff] [blame^] | 1132 | const unsigned long output = members | BIT(dsa_upstream_port(ds)); |
| 1133 | int port, err = 0; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1134 | |
| 1135 | mutex_lock(&ps->smi_mutex); |
| 1136 | |
Vivien Didelot | ede8098 | 2015-10-11 18:08:35 -0400 | [diff] [blame^] | 1137 | for_each_set_bit(port, &output, ps->num_ports) { |
| 1138 | if (dsa_is_cpu_port(ds, port)) |
| 1139 | continue; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1140 | |
Vivien Didelot | ede8098 | 2015-10-11 18:08:35 -0400 | [diff] [blame^] | 1141 | err = _mv88e6xxx_port_vlan_map_set(ds, port, output & ~port); |
| 1142 | if (err) |
| 1143 | break; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | mutex_unlock(&ps->smi_mutex); |
| 1147 | |
Vivien Didelot | ede8098 | 2015-10-11 18:08:35 -0400 | [diff] [blame^] | 1148 | return err; |
| 1149 | } |
| 1150 | |
| 1151 | |
| 1152 | int mv88e6xxx_join_bridge(struct dsa_switch *ds, int port, u32 br_port_mask) |
| 1153 | { |
| 1154 | return mv88e6xxx_map_bridge(ds, br_port_mask); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | int mv88e6xxx_leave_bridge(struct dsa_switch *ds, int port, u32 br_port_mask) |
| 1158 | { |
Vivien Didelot | ede8098 | 2015-10-11 18:08:35 -0400 | [diff] [blame^] | 1159 | return mv88e6xxx_map_bridge(ds, br_port_mask & ~port); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | int mv88e6xxx_port_stp_update(struct dsa_switch *ds, int port, u8 state) |
| 1163 | { |
| 1164 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1165 | int stp_state; |
| 1166 | |
| 1167 | switch (state) { |
| 1168 | case BR_STATE_DISABLED: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1169 | stp_state = PORT_CONTROL_STATE_DISABLED; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1170 | break; |
| 1171 | case BR_STATE_BLOCKING: |
| 1172 | case BR_STATE_LISTENING: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1173 | stp_state = PORT_CONTROL_STATE_BLOCKING; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1174 | break; |
| 1175 | case BR_STATE_LEARNING: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1176 | stp_state = PORT_CONTROL_STATE_LEARNING; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1177 | break; |
| 1178 | case BR_STATE_FORWARDING: |
| 1179 | default: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1180 | stp_state = PORT_CONTROL_STATE_FORWARDING; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1181 | break; |
| 1182 | } |
| 1183 | |
| 1184 | netdev_dbg(ds->ports[port], "port state %d [%d]\n", state, stp_state); |
| 1185 | |
| 1186 | /* mv88e6xxx_port_stp_update may be called with softirqs disabled, |
| 1187 | * so we can not update the port state directly but need to schedule it. |
| 1188 | */ |
| 1189 | ps->port_state[port] = stp_state; |
| 1190 | set_bit(port, &ps->port_state_update_mask); |
| 1191 | schedule_work(&ps->bridge_work); |
| 1192 | |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1196 | int mv88e6xxx_port_pvid_get(struct dsa_switch *ds, int port, u16 *pvid) |
| 1197 | { |
| 1198 | int ret; |
| 1199 | |
| 1200 | ret = mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_DEFAULT_VLAN); |
| 1201 | if (ret < 0) |
| 1202 | return ret; |
| 1203 | |
| 1204 | *pvid = ret & PORT_DEFAULT_VLAN_MASK; |
| 1205 | |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1209 | int mv88e6xxx_port_pvid_set(struct dsa_switch *ds, int port, u16 pvid) |
| 1210 | { |
| 1211 | return mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_DEFAULT_VLAN, |
| 1212 | pvid & PORT_DEFAULT_VLAN_MASK); |
| 1213 | } |
| 1214 | |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 1215 | static int _mv88e6xxx_vtu_wait(struct dsa_switch *ds) |
| 1216 | { |
| 1217 | return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_VTU_OP, |
| 1218 | GLOBAL_VTU_OP_BUSY); |
| 1219 | } |
| 1220 | |
| 1221 | static int _mv88e6xxx_vtu_cmd(struct dsa_switch *ds, u16 op) |
| 1222 | { |
| 1223 | int ret; |
| 1224 | |
| 1225 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_OP, op); |
| 1226 | if (ret < 0) |
| 1227 | return ret; |
| 1228 | |
| 1229 | return _mv88e6xxx_vtu_wait(ds); |
| 1230 | } |
| 1231 | |
| 1232 | static int _mv88e6xxx_vtu_stu_flush(struct dsa_switch *ds) |
| 1233 | { |
| 1234 | int ret; |
| 1235 | |
| 1236 | ret = _mv88e6xxx_vtu_wait(ds); |
| 1237 | if (ret < 0) |
| 1238 | return ret; |
| 1239 | |
| 1240 | return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_FLUSH_ALL); |
| 1241 | } |
| 1242 | |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1243 | static int _mv88e6xxx_vtu_stu_data_read(struct dsa_switch *ds, |
| 1244 | struct mv88e6xxx_vtu_stu_entry *entry, |
| 1245 | unsigned int nibble_offset) |
| 1246 | { |
| 1247 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1248 | u16 regs[3]; |
| 1249 | int i; |
| 1250 | int ret; |
| 1251 | |
| 1252 | for (i = 0; i < 3; ++i) { |
| 1253 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, |
| 1254 | GLOBAL_VTU_DATA_0_3 + i); |
| 1255 | if (ret < 0) |
| 1256 | return ret; |
| 1257 | |
| 1258 | regs[i] = ret; |
| 1259 | } |
| 1260 | |
| 1261 | for (i = 0; i < ps->num_ports; ++i) { |
| 1262 | unsigned int shift = (i % 4) * 4 + nibble_offset; |
| 1263 | u16 reg = regs[i / 4]; |
| 1264 | |
| 1265 | entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK; |
| 1266 | } |
| 1267 | |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1271 | static int _mv88e6xxx_vtu_stu_data_write(struct dsa_switch *ds, |
| 1272 | struct mv88e6xxx_vtu_stu_entry *entry, |
| 1273 | unsigned int nibble_offset) |
| 1274 | { |
| 1275 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1276 | u16 regs[3] = { 0 }; |
| 1277 | int i; |
| 1278 | int ret; |
| 1279 | |
| 1280 | for (i = 0; i < ps->num_ports; ++i) { |
| 1281 | unsigned int shift = (i % 4) * 4 + nibble_offset; |
| 1282 | u8 data = entry->data[i]; |
| 1283 | |
| 1284 | regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift; |
| 1285 | } |
| 1286 | |
| 1287 | for (i = 0; i < 3; ++i) { |
| 1288 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, |
| 1289 | GLOBAL_VTU_DATA_0_3 + i, regs[i]); |
| 1290 | if (ret < 0) |
| 1291 | return ret; |
| 1292 | } |
| 1293 | |
| 1294 | return 0; |
| 1295 | } |
| 1296 | |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1297 | static int _mv88e6xxx_vtu_getnext(struct dsa_switch *ds, u16 vid, |
| 1298 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1299 | { |
| 1300 | struct mv88e6xxx_vtu_stu_entry next = { 0 }; |
| 1301 | int ret; |
| 1302 | |
| 1303 | ret = _mv88e6xxx_vtu_wait(ds); |
| 1304 | if (ret < 0) |
| 1305 | return ret; |
| 1306 | |
| 1307 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID, |
| 1308 | vid & GLOBAL_VTU_VID_MASK); |
| 1309 | if (ret < 0) |
| 1310 | return ret; |
| 1311 | |
| 1312 | ret = _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_VTU_GET_NEXT); |
| 1313 | if (ret < 0) |
| 1314 | return ret; |
| 1315 | |
| 1316 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_VID); |
| 1317 | if (ret < 0) |
| 1318 | return ret; |
| 1319 | |
| 1320 | next.vid = ret & GLOBAL_VTU_VID_MASK; |
| 1321 | next.valid = !!(ret & GLOBAL_VTU_VID_VALID); |
| 1322 | |
| 1323 | if (next.valid) { |
| 1324 | ret = _mv88e6xxx_vtu_stu_data_read(ds, &next, 0); |
| 1325 | if (ret < 0) |
| 1326 | return ret; |
| 1327 | |
| 1328 | if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) || |
| 1329 | mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) { |
| 1330 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, |
| 1331 | GLOBAL_VTU_FID); |
| 1332 | if (ret < 0) |
| 1333 | return ret; |
| 1334 | |
| 1335 | next.fid = ret & GLOBAL_VTU_FID_MASK; |
| 1336 | |
| 1337 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, |
| 1338 | GLOBAL_VTU_SID); |
| 1339 | if (ret < 0) |
| 1340 | return ret; |
| 1341 | |
| 1342 | next.sid = ret & GLOBAL_VTU_SID_MASK; |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | *entry = next; |
| 1347 | return 0; |
| 1348 | } |
| 1349 | |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1350 | static int _mv88e6xxx_vtu_loadpurge(struct dsa_switch *ds, |
| 1351 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1352 | { |
| 1353 | u16 reg = 0; |
| 1354 | int ret; |
| 1355 | |
| 1356 | ret = _mv88e6xxx_vtu_wait(ds); |
| 1357 | if (ret < 0) |
| 1358 | return ret; |
| 1359 | |
| 1360 | if (!entry->valid) |
| 1361 | goto loadpurge; |
| 1362 | |
| 1363 | /* Write port member tags */ |
| 1364 | ret = _mv88e6xxx_vtu_stu_data_write(ds, entry, 0); |
| 1365 | if (ret < 0) |
| 1366 | return ret; |
| 1367 | |
| 1368 | if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) || |
| 1369 | mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) { |
| 1370 | reg = entry->sid & GLOBAL_VTU_SID_MASK; |
| 1371 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID, reg); |
| 1372 | if (ret < 0) |
| 1373 | return ret; |
| 1374 | |
| 1375 | reg = entry->fid & GLOBAL_VTU_FID_MASK; |
| 1376 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_FID, reg); |
| 1377 | if (ret < 0) |
| 1378 | return ret; |
| 1379 | } |
| 1380 | |
| 1381 | reg = GLOBAL_VTU_VID_VALID; |
| 1382 | loadpurge: |
| 1383 | reg |= entry->vid & GLOBAL_VTU_VID_MASK; |
| 1384 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID, reg); |
| 1385 | if (ret < 0) |
| 1386 | return ret; |
| 1387 | |
| 1388 | return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_VTU_LOAD_PURGE); |
| 1389 | } |
| 1390 | |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1391 | static int _mv88e6xxx_stu_getnext(struct dsa_switch *ds, u8 sid, |
| 1392 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1393 | { |
| 1394 | struct mv88e6xxx_vtu_stu_entry next = { 0 }; |
| 1395 | int ret; |
| 1396 | |
| 1397 | ret = _mv88e6xxx_vtu_wait(ds); |
| 1398 | if (ret < 0) |
| 1399 | return ret; |
| 1400 | |
| 1401 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID, |
| 1402 | sid & GLOBAL_VTU_SID_MASK); |
| 1403 | if (ret < 0) |
| 1404 | return ret; |
| 1405 | |
| 1406 | ret = _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_STU_GET_NEXT); |
| 1407 | if (ret < 0) |
| 1408 | return ret; |
| 1409 | |
| 1410 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_SID); |
| 1411 | if (ret < 0) |
| 1412 | return ret; |
| 1413 | |
| 1414 | next.sid = ret & GLOBAL_VTU_SID_MASK; |
| 1415 | |
| 1416 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_VID); |
| 1417 | if (ret < 0) |
| 1418 | return ret; |
| 1419 | |
| 1420 | next.valid = !!(ret & GLOBAL_VTU_VID_VALID); |
| 1421 | |
| 1422 | if (next.valid) { |
| 1423 | ret = _mv88e6xxx_vtu_stu_data_read(ds, &next, 2); |
| 1424 | if (ret < 0) |
| 1425 | return ret; |
| 1426 | } |
| 1427 | |
| 1428 | *entry = next; |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
| 1432 | static int _mv88e6xxx_stu_loadpurge(struct dsa_switch *ds, |
| 1433 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1434 | { |
| 1435 | u16 reg = 0; |
| 1436 | int ret; |
| 1437 | |
| 1438 | ret = _mv88e6xxx_vtu_wait(ds); |
| 1439 | if (ret < 0) |
| 1440 | return ret; |
| 1441 | |
| 1442 | if (!entry->valid) |
| 1443 | goto loadpurge; |
| 1444 | |
| 1445 | /* Write port states */ |
| 1446 | ret = _mv88e6xxx_vtu_stu_data_write(ds, entry, 2); |
| 1447 | if (ret < 0) |
| 1448 | return ret; |
| 1449 | |
| 1450 | reg = GLOBAL_VTU_VID_VALID; |
| 1451 | loadpurge: |
| 1452 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID, reg); |
| 1453 | if (ret < 0) |
| 1454 | return ret; |
| 1455 | |
| 1456 | reg = entry->sid & GLOBAL_VTU_SID_MASK; |
| 1457 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID, reg); |
| 1458 | if (ret < 0) |
| 1459 | return ret; |
| 1460 | |
| 1461 | return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_STU_LOAD_PURGE); |
| 1462 | } |
| 1463 | |
| 1464 | static int _mv88e6xxx_vlan_init(struct dsa_switch *ds, u16 vid, |
| 1465 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1466 | { |
| 1467 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1468 | struct mv88e6xxx_vtu_stu_entry vlan = { |
| 1469 | .valid = true, |
| 1470 | .vid = vid, |
| 1471 | }; |
| 1472 | int i; |
| 1473 | |
| 1474 | /* exclude all ports except the CPU */ |
| 1475 | for (i = 0; i < ps->num_ports; ++i) |
| 1476 | vlan.data[i] = dsa_is_cpu_port(ds, i) ? |
| 1477 | GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED : |
| 1478 | GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER; |
| 1479 | |
| 1480 | if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) || |
| 1481 | mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) { |
| 1482 | struct mv88e6xxx_vtu_stu_entry vstp; |
| 1483 | int err; |
| 1484 | |
| 1485 | /* Adding a VTU entry requires a valid STU entry. As VSTP is not |
| 1486 | * implemented, only one STU entry is needed to cover all VTU |
| 1487 | * entries. Thus, validate the SID 0. |
| 1488 | */ |
| 1489 | vlan.sid = 0; |
| 1490 | err = _mv88e6xxx_stu_getnext(ds, GLOBAL_VTU_SID_MASK, &vstp); |
| 1491 | if (err) |
| 1492 | return err; |
| 1493 | |
| 1494 | if (vstp.sid != vlan.sid || !vstp.valid) { |
| 1495 | memset(&vstp, 0, sizeof(vstp)); |
| 1496 | vstp.valid = true; |
| 1497 | vstp.sid = vlan.sid; |
| 1498 | |
| 1499 | err = _mv88e6xxx_stu_loadpurge(ds, &vstp); |
| 1500 | if (err) |
| 1501 | return err; |
| 1502 | } |
| 1503 | |
| 1504 | /* Non-bridged ports and bridge groups use FIDs from 1 to |
| 1505 | * num_ports; VLANs use FIDs from num_ports+1 to 4095. |
| 1506 | */ |
| 1507 | vlan.fid = find_next_zero_bit(ps->fid_bitmap, VLAN_N_VID, |
| 1508 | ps->num_ports + 1); |
| 1509 | if (unlikely(vlan.fid == VLAN_N_VID)) { |
| 1510 | pr_err("no more FID available for VLAN %d\n", vid); |
| 1511 | return -ENOSPC; |
| 1512 | } |
| 1513 | |
Vivien Didelot | 7c40001 | 2015-09-04 14:34:14 -0400 | [diff] [blame] | 1514 | /* Clear all MAC addresses from the new database */ |
| 1515 | err = _mv88e6xxx_atu_flush(ds, vlan.fid, true); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1516 | if (err) |
| 1517 | return err; |
| 1518 | |
| 1519 | set_bit(vlan.fid, ps->fid_bitmap); |
| 1520 | } |
| 1521 | |
| 1522 | *entry = vlan; |
| 1523 | return 0; |
| 1524 | } |
| 1525 | |
| 1526 | int mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port, u16 vid, |
| 1527 | bool untagged) |
| 1528 | { |
| 1529 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1530 | struct mv88e6xxx_vtu_stu_entry vlan; |
| 1531 | int err; |
| 1532 | |
| 1533 | mutex_lock(&ps->smi_mutex); |
| 1534 | err = _mv88e6xxx_vtu_getnext(ds, vid - 1, &vlan); |
| 1535 | if (err) |
| 1536 | goto unlock; |
| 1537 | |
| 1538 | if (vlan.vid != vid || !vlan.valid) { |
| 1539 | err = _mv88e6xxx_vlan_init(ds, vid, &vlan); |
| 1540 | if (err) |
| 1541 | goto unlock; |
| 1542 | } |
| 1543 | |
| 1544 | vlan.data[port] = untagged ? |
| 1545 | GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED : |
| 1546 | GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED; |
| 1547 | |
| 1548 | err = _mv88e6xxx_vtu_loadpurge(ds, &vlan); |
| 1549 | unlock: |
| 1550 | mutex_unlock(&ps->smi_mutex); |
| 1551 | |
| 1552 | return err; |
| 1553 | } |
| 1554 | |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1555 | int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port, u16 vid) |
| 1556 | { |
| 1557 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1558 | struct mv88e6xxx_vtu_stu_entry vlan; |
| 1559 | bool keep = false; |
| 1560 | int i, err; |
| 1561 | |
| 1562 | mutex_lock(&ps->smi_mutex); |
| 1563 | |
| 1564 | err = _mv88e6xxx_vtu_getnext(ds, vid - 1, &vlan); |
| 1565 | if (err) |
| 1566 | goto unlock; |
| 1567 | |
| 1568 | if (vlan.vid != vid || !vlan.valid || |
| 1569 | vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) { |
| 1570 | err = -ENOENT; |
| 1571 | goto unlock; |
| 1572 | } |
| 1573 | |
| 1574 | vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER; |
| 1575 | |
| 1576 | /* keep the VLAN unless all ports are excluded */ |
| 1577 | for (i = 0; i < ps->num_ports; ++i) { |
| 1578 | if (dsa_is_cpu_port(ds, i)) |
| 1579 | continue; |
| 1580 | |
| 1581 | if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) { |
| 1582 | keep = true; |
| 1583 | break; |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | vlan.valid = keep; |
| 1588 | err = _mv88e6xxx_vtu_loadpurge(ds, &vlan); |
| 1589 | if (err) |
| 1590 | goto unlock; |
| 1591 | |
Vivien Didelot | 9f4d55d | 2015-09-04 14:34:15 -0400 | [diff] [blame] | 1592 | err = _mv88e6xxx_atu_remove(ds, vlan.fid, port, false); |
| 1593 | if (err) |
| 1594 | goto unlock; |
| 1595 | |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1596 | if (!keep) |
| 1597 | clear_bit(vlan.fid, ps->fid_bitmap); |
| 1598 | |
| 1599 | unlock: |
| 1600 | mutex_unlock(&ps->smi_mutex); |
| 1601 | |
| 1602 | return err; |
| 1603 | } |
| 1604 | |
Vivien Didelot | 02512b6 | 2015-08-13 12:52:20 -0400 | [diff] [blame] | 1605 | static int _mv88e6xxx_port_vtu_getnext(struct dsa_switch *ds, int port, u16 vid, |
| 1606 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1607 | { |
| 1608 | int err; |
| 1609 | |
| 1610 | do { |
| 1611 | if (vid == 4095) |
| 1612 | return -ENOENT; |
| 1613 | |
| 1614 | err = _mv88e6xxx_vtu_getnext(ds, vid, entry); |
| 1615 | if (err) |
| 1616 | return err; |
| 1617 | |
| 1618 | if (!entry->valid) |
| 1619 | return -ENOENT; |
| 1620 | |
| 1621 | vid = entry->vid; |
| 1622 | } while (entry->data[port] != GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED && |
| 1623 | entry->data[port] != GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED); |
| 1624 | |
| 1625 | return 0; |
| 1626 | } |
| 1627 | |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1628 | int mv88e6xxx_vlan_getnext(struct dsa_switch *ds, u16 *vid, |
| 1629 | unsigned long *ports, unsigned long *untagged) |
| 1630 | { |
| 1631 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1632 | struct mv88e6xxx_vtu_stu_entry next; |
| 1633 | int port; |
| 1634 | int err; |
| 1635 | |
| 1636 | if (*vid == 4095) |
| 1637 | return -ENOENT; |
| 1638 | |
| 1639 | mutex_lock(&ps->smi_mutex); |
| 1640 | err = _mv88e6xxx_vtu_getnext(ds, *vid, &next); |
| 1641 | mutex_unlock(&ps->smi_mutex); |
| 1642 | |
| 1643 | if (err) |
| 1644 | return err; |
| 1645 | |
| 1646 | if (!next.valid) |
| 1647 | return -ENOENT; |
| 1648 | |
| 1649 | *vid = next.vid; |
| 1650 | |
| 1651 | for (port = 0; port < ps->num_ports; ++port) { |
| 1652 | clear_bit(port, ports); |
| 1653 | clear_bit(port, untagged); |
| 1654 | |
| 1655 | if (dsa_is_cpu_port(ds, port)) |
| 1656 | continue; |
| 1657 | |
| 1658 | if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED || |
| 1659 | next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED) |
| 1660 | set_bit(port, ports); |
| 1661 | |
| 1662 | if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED) |
| 1663 | set_bit(port, untagged); |
| 1664 | } |
| 1665 | |
| 1666 | return 0; |
| 1667 | } |
| 1668 | |
Vivien Didelot | c5723ac | 2015-08-10 09:09:48 -0400 | [diff] [blame] | 1669 | static int _mv88e6xxx_atu_mac_write(struct dsa_switch *ds, |
| 1670 | const unsigned char *addr) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1671 | { |
| 1672 | int i, ret; |
| 1673 | |
| 1674 | for (i = 0; i < 3; i++) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1675 | ret = _mv88e6xxx_reg_write( |
| 1676 | ds, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i, |
| 1677 | (addr[i * 2] << 8) | addr[i * 2 + 1]); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1678 | if (ret < 0) |
| 1679 | return ret; |
| 1680 | } |
| 1681 | |
| 1682 | return 0; |
| 1683 | } |
| 1684 | |
Vivien Didelot | c5723ac | 2015-08-10 09:09:48 -0400 | [diff] [blame] | 1685 | static int _mv88e6xxx_atu_mac_read(struct dsa_switch *ds, unsigned char *addr) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1686 | { |
| 1687 | int i, ret; |
| 1688 | |
| 1689 | for (i = 0; i < 3; i++) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1690 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, |
| 1691 | GLOBAL_ATU_MAC_01 + i); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1692 | if (ret < 0) |
| 1693 | return ret; |
| 1694 | addr[i * 2] = ret >> 8; |
| 1695 | addr[i * 2 + 1] = ret & 0xff; |
| 1696 | } |
| 1697 | |
| 1698 | return 0; |
| 1699 | } |
| 1700 | |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 1701 | static int _mv88e6xxx_atu_load(struct dsa_switch *ds, |
| 1702 | struct mv88e6xxx_atu_entry *entry) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1703 | { |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1704 | int ret; |
| 1705 | |
| 1706 | ret = _mv88e6xxx_atu_wait(ds); |
| 1707 | if (ret < 0) |
| 1708 | return ret; |
| 1709 | |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 1710 | ret = _mv88e6xxx_atu_mac_write(ds, entry->mac); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1711 | if (ret < 0) |
| 1712 | return ret; |
| 1713 | |
Vivien Didelot | 37705b7 | 2015-09-04 14:34:11 -0400 | [diff] [blame] | 1714 | ret = _mv88e6xxx_atu_data_write(ds, entry); |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 1715 | if (ret < 0) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1716 | return ret; |
| 1717 | |
Vivien Didelot | 70cc99d | 2015-09-04 14:34:10 -0400 | [diff] [blame] | 1718 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID, entry->fid); |
| 1719 | if (ret < 0) |
| 1720 | return ret; |
| 1721 | |
| 1722 | return _mv88e6xxx_atu_cmd(ds, GLOBAL_ATU_OP_LOAD_DB); |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 1723 | } |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 1724 | |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 1725 | static int _mv88e6xxx_port_vid_to_fid(struct dsa_switch *ds, int port, u16 vid) |
| 1726 | { |
| 1727 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | 02512b6 | 2015-08-13 12:52:20 -0400 | [diff] [blame] | 1728 | struct mv88e6xxx_vtu_stu_entry vlan; |
| 1729 | int err; |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 1730 | |
| 1731 | if (vid == 0) |
| 1732 | return ps->fid[port]; |
| 1733 | |
Vivien Didelot | 02512b6 | 2015-08-13 12:52:20 -0400 | [diff] [blame] | 1734 | err = _mv88e6xxx_port_vtu_getnext(ds, port, vid - 1, &vlan); |
| 1735 | if (err) |
| 1736 | return err; |
| 1737 | |
| 1738 | if (vlan.vid == vid) |
| 1739 | return vlan.fid; |
| 1740 | |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 1741 | return -ENOENT; |
| 1742 | } |
| 1743 | |
| 1744 | static int _mv88e6xxx_port_fdb_load(struct dsa_switch *ds, int port, |
| 1745 | const unsigned char *addr, u16 vid, |
| 1746 | u8 state) |
| 1747 | { |
| 1748 | struct mv88e6xxx_atu_entry entry = { 0 }; |
| 1749 | int ret; |
| 1750 | |
| 1751 | ret = _mv88e6xxx_port_vid_to_fid(ds, port, vid); |
| 1752 | if (ret < 0) |
| 1753 | return ret; |
| 1754 | |
| 1755 | entry.fid = ret; |
| 1756 | entry.state = state; |
| 1757 | ether_addr_copy(entry.mac, addr); |
| 1758 | if (state != GLOBAL_ATU_DATA_STATE_UNUSED) { |
| 1759 | entry.trunk = false; |
| 1760 | entry.portv_trunkid = BIT(port); |
| 1761 | } |
| 1762 | |
| 1763 | return _mv88e6xxx_atu_load(ds, &entry); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1764 | } |
| 1765 | |
Vivien Didelot | 146a320 | 2015-10-08 11:35:12 -0400 | [diff] [blame] | 1766 | int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port, |
| 1767 | const struct switchdev_obj_port_fdb *fdb, |
| 1768 | struct switchdev_trans *trans) |
| 1769 | { |
| 1770 | /* We don't need any dynamic resource from the kernel (yet), |
| 1771 | * so skip the prepare phase. |
| 1772 | */ |
| 1773 | return 0; |
| 1774 | } |
| 1775 | |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 1776 | int mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port, |
Vivien Didelot | 1f36faf | 2015-10-08 11:35:13 -0400 | [diff] [blame] | 1777 | const struct switchdev_obj_port_fdb *fdb, |
| 1778 | struct switchdev_trans *trans) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1779 | { |
Vivien Didelot | 1f36faf | 2015-10-08 11:35:13 -0400 | [diff] [blame] | 1780 | int state = is_multicast_ether_addr(fdb->addr) ? |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 1781 | GLOBAL_ATU_DATA_STATE_MC_STATIC : |
| 1782 | GLOBAL_ATU_DATA_STATE_UC_STATIC; |
| 1783 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | 6630e23 | 2015-08-06 01:44:07 -0400 | [diff] [blame] | 1784 | int ret; |
| 1785 | |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 1786 | mutex_lock(&ps->smi_mutex); |
Vivien Didelot | 1f36faf | 2015-10-08 11:35:13 -0400 | [diff] [blame] | 1787 | ret = _mv88e6xxx_port_fdb_load(ds, port, fdb->addr, fdb->vid, state); |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 1788 | mutex_unlock(&ps->smi_mutex); |
| 1789 | |
| 1790 | return ret; |
| 1791 | } |
| 1792 | |
| 1793 | int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port, |
Vivien Didelot | 8057b3e | 2015-10-08 11:35:14 -0400 | [diff] [blame] | 1794 | const struct switchdev_obj_port_fdb *fdb) |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 1795 | { |
| 1796 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1797 | int ret; |
| 1798 | |
| 1799 | mutex_lock(&ps->smi_mutex); |
Vivien Didelot | 8057b3e | 2015-10-08 11:35:14 -0400 | [diff] [blame] | 1800 | ret = _mv88e6xxx_port_fdb_load(ds, port, fdb->addr, fdb->vid, |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 1801 | GLOBAL_ATU_DATA_STATE_UNUSED); |
| 1802 | mutex_unlock(&ps->smi_mutex); |
| 1803 | |
| 1804 | return ret; |
| 1805 | } |
| 1806 | |
Vivien Didelot | 1d19404 | 2015-08-10 09:09:51 -0400 | [diff] [blame] | 1807 | static int _mv88e6xxx_atu_getnext(struct dsa_switch *ds, u16 fid, |
| 1808 | const unsigned char *addr, |
| 1809 | struct mv88e6xxx_atu_entry *entry) |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 1810 | { |
Vivien Didelot | 1d19404 | 2015-08-10 09:09:51 -0400 | [diff] [blame] | 1811 | struct mv88e6xxx_atu_entry next = { 0 }; |
| 1812 | int ret; |
| 1813 | |
| 1814 | next.fid = fid; |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1815 | |
| 1816 | ret = _mv88e6xxx_atu_wait(ds); |
| 1817 | if (ret < 0) |
| 1818 | return ret; |
| 1819 | |
Vivien Didelot | c5723ac | 2015-08-10 09:09:48 -0400 | [diff] [blame] | 1820 | ret = _mv88e6xxx_atu_mac_write(ds, addr); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1821 | if (ret < 0) |
| 1822 | return ret; |
| 1823 | |
Vivien Didelot | 70cc99d | 2015-09-04 14:34:10 -0400 | [diff] [blame] | 1824 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID, fid); |
| 1825 | if (ret < 0) |
| 1826 | return ret; |
| 1827 | |
| 1828 | ret = _mv88e6xxx_atu_cmd(ds, GLOBAL_ATU_OP_GET_NEXT_DB); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1829 | if (ret < 0) |
| 1830 | return ret; |
| 1831 | |
Vivien Didelot | 1d19404 | 2015-08-10 09:09:51 -0400 | [diff] [blame] | 1832 | ret = _mv88e6xxx_atu_mac_read(ds, next.mac); |
| 1833 | if (ret < 0) |
| 1834 | return ret; |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1835 | |
Vivien Didelot | 1d19404 | 2015-08-10 09:09:51 -0400 | [diff] [blame] | 1836 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_DATA); |
| 1837 | if (ret < 0) |
| 1838 | return ret; |
| 1839 | |
| 1840 | next.state = ret & GLOBAL_ATU_DATA_STATE_MASK; |
| 1841 | if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) { |
| 1842 | unsigned int mask, shift; |
| 1843 | |
| 1844 | if (ret & GLOBAL_ATU_DATA_TRUNK) { |
| 1845 | next.trunk = true; |
| 1846 | mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK; |
| 1847 | shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT; |
| 1848 | } else { |
| 1849 | next.trunk = false; |
| 1850 | mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK; |
| 1851 | shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT; |
| 1852 | } |
| 1853 | |
| 1854 | next.portv_trunkid = (ret & mask) >> shift; |
| 1855 | } |
| 1856 | |
| 1857 | *entry = next; |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1858 | return 0; |
| 1859 | } |
| 1860 | |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 1861 | /* get next entry for port */ |
| 1862 | int mv88e6xxx_port_fdb_getnext(struct dsa_switch *ds, int port, |
Vivien Didelot | 2a778e1 | 2015-08-10 09:09:49 -0400 | [diff] [blame] | 1863 | unsigned char *addr, u16 *vid, bool *is_static) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1864 | { |
| 1865 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | 1d19404 | 2015-08-10 09:09:51 -0400 | [diff] [blame] | 1866 | struct mv88e6xxx_atu_entry next; |
| 1867 | u16 fid; |
Vivien Didelot | 8782051 | 2015-08-06 01:44:08 -0400 | [diff] [blame] | 1868 | int ret; |
| 1869 | |
| 1870 | mutex_lock(&ps->smi_mutex); |
Vivien Didelot | 1d19404 | 2015-08-10 09:09:51 -0400 | [diff] [blame] | 1871 | |
| 1872 | ret = _mv88e6xxx_port_vid_to_fid(ds, port, *vid); |
| 1873 | if (ret < 0) |
| 1874 | goto unlock; |
| 1875 | fid = ret; |
| 1876 | |
| 1877 | do { |
| 1878 | if (is_broadcast_ether_addr(addr)) { |
Vivien Didelot | 02512b6 | 2015-08-13 12:52:20 -0400 | [diff] [blame] | 1879 | struct mv88e6xxx_vtu_stu_entry vtu; |
| 1880 | |
| 1881 | ret = _mv88e6xxx_port_vtu_getnext(ds, port, *vid, &vtu); |
| 1882 | if (ret < 0) |
| 1883 | goto unlock; |
| 1884 | |
| 1885 | *vid = vtu.vid; |
| 1886 | fid = vtu.fid; |
Vivien Didelot | 1d19404 | 2015-08-10 09:09:51 -0400 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | ret = _mv88e6xxx_atu_getnext(ds, fid, addr, &next); |
| 1890 | if (ret < 0) |
| 1891 | goto unlock; |
| 1892 | |
| 1893 | ether_addr_copy(addr, next.mac); |
| 1894 | |
| 1895 | if (next.state == GLOBAL_ATU_DATA_STATE_UNUSED) |
| 1896 | continue; |
| 1897 | } while (next.trunk || (next.portv_trunkid & BIT(port)) == 0); |
| 1898 | |
| 1899 | *is_static = next.state == (is_multicast_ether_addr(addr) ? |
| 1900 | GLOBAL_ATU_DATA_STATE_MC_STATIC : |
| 1901 | GLOBAL_ATU_DATA_STATE_UC_STATIC); |
| 1902 | unlock: |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1903 | mutex_unlock(&ps->smi_mutex); |
| 1904 | |
| 1905 | return ret; |
| 1906 | } |
| 1907 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1908 | static void mv88e6xxx_bridge_work(struct work_struct *work) |
| 1909 | { |
| 1910 | struct mv88e6xxx_priv_state *ps; |
| 1911 | struct dsa_switch *ds; |
| 1912 | int port; |
| 1913 | |
| 1914 | ps = container_of(work, struct mv88e6xxx_priv_state, bridge_work); |
| 1915 | ds = ((struct dsa_switch *)ps) - 1; |
| 1916 | |
| 1917 | while (ps->port_state_update_mask) { |
| 1918 | port = __ffs(ps->port_state_update_mask); |
| 1919 | clear_bit(port, &ps->port_state_update_mask); |
| 1920 | mv88e6xxx_set_port_state(ds, port, ps->port_state[port]); |
| 1921 | } |
| 1922 | } |
| 1923 | |
Andrew Lunn | dbde9e6 | 2015-05-06 01:09:48 +0200 | [diff] [blame] | 1924 | static int mv88e6xxx_setup_port(struct dsa_switch *ds, int port) |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 1925 | { |
| 1926 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1927 | int ret, fid; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1928 | u16 reg; |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 1929 | |
| 1930 | mutex_lock(&ps->smi_mutex); |
| 1931 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1932 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 1933 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 1934 | mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 1935 | mv88e6xxx_6065_family(ds) || mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1936 | /* MAC Forcing register: don't force link, speed, |
| 1937 | * duplex or flow control state to any particular |
| 1938 | * values on physical ports, but force the CPU port |
| 1939 | * and all DSA ports to their maximum bandwidth and |
| 1940 | * full duplex. |
| 1941 | */ |
| 1942 | reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_PCS_CTRL); |
Andrew Lunn | 60045cb | 2015-08-17 23:52:51 +0200 | [diff] [blame] | 1943 | if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) { |
Russell King | 53adc9e | 2015-09-21 21:42:59 +0100 | [diff] [blame] | 1944 | reg &= ~PORT_PCS_CTRL_UNFORCED; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1945 | reg |= PORT_PCS_CTRL_FORCE_LINK | |
| 1946 | PORT_PCS_CTRL_LINK_UP | |
| 1947 | PORT_PCS_CTRL_DUPLEX_FULL | |
| 1948 | PORT_PCS_CTRL_FORCE_DUPLEX; |
| 1949 | if (mv88e6xxx_6065_family(ds)) |
| 1950 | reg |= PORT_PCS_CTRL_100; |
| 1951 | else |
| 1952 | reg |= PORT_PCS_CTRL_1000; |
| 1953 | } else { |
| 1954 | reg |= PORT_PCS_CTRL_UNFORCED; |
| 1955 | } |
| 1956 | |
| 1957 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1958 | PORT_PCS_CTRL, reg); |
| 1959 | if (ret) |
| 1960 | goto abort; |
| 1961 | } |
| 1962 | |
| 1963 | /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock, |
| 1964 | * disable Header mode, enable IGMP/MLD snooping, disable VLAN |
| 1965 | * tunneling, determine priority by looking at 802.1p and IP |
| 1966 | * priority fields (IP prio has precedence), and set STP state |
| 1967 | * to Forwarding. |
| 1968 | * |
| 1969 | * If this is the CPU link, use DSA or EDSA tagging depending |
| 1970 | * on which tagging mode was configured. |
| 1971 | * |
| 1972 | * If this is a link to another switch, use DSA tagging mode. |
| 1973 | * |
| 1974 | * If this is the upstream port for this switch, enable |
| 1975 | * forwarding of unknown unicasts and multicasts. |
| 1976 | */ |
| 1977 | reg = 0; |
| 1978 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 1979 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 1980 | mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 1981 | mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds)) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1982 | reg = PORT_CONTROL_IGMP_MLD_SNOOP | |
| 1983 | PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP | |
| 1984 | PORT_CONTROL_STATE_FORWARDING; |
| 1985 | if (dsa_is_cpu_port(ds, port)) { |
| 1986 | if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds)) |
| 1987 | reg |= PORT_CONTROL_DSA_TAG; |
| 1988 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 1989 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 1990 | mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1991 | if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA) |
| 1992 | reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA; |
| 1993 | else |
| 1994 | reg |= PORT_CONTROL_FRAME_MODE_DSA; |
Andrew Lunn | c047a1f | 2015-09-29 01:50:56 +0200 | [diff] [blame] | 1995 | reg |= PORT_CONTROL_FORWARD_UNKNOWN | |
| 1996 | PORT_CONTROL_FORWARD_UNKNOWN_MC; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 2000 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 2001 | mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 2002 | mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2003 | if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA) |
| 2004 | reg |= PORT_CONTROL_EGRESS_ADD_TAG; |
| 2005 | } |
| 2006 | } |
Andrew Lunn | 6083ce7 | 2015-08-17 23:52:52 +0200 | [diff] [blame] | 2007 | if (dsa_is_dsa_port(ds, port)) { |
| 2008 | if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds)) |
| 2009 | reg |= PORT_CONTROL_DSA_TAG; |
| 2010 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 2011 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 2012 | mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2013 | reg |= PORT_CONTROL_FRAME_MODE_DSA; |
Andrew Lunn | 6083ce7 | 2015-08-17 23:52:52 +0200 | [diff] [blame] | 2014 | } |
| 2015 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2016 | if (port == dsa_upstream_port(ds)) |
| 2017 | reg |= PORT_CONTROL_FORWARD_UNKNOWN | |
| 2018 | PORT_CONTROL_FORWARD_UNKNOWN_MC; |
| 2019 | } |
| 2020 | if (reg) { |
| 2021 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 2022 | PORT_CONTROL, reg); |
| 2023 | if (ret) |
| 2024 | goto abort; |
| 2025 | } |
| 2026 | |
Vivien Didelot | 8efdda4 | 2015-08-13 12:52:23 -0400 | [diff] [blame] | 2027 | /* Port Control 2: don't force a good FCS, set the maximum frame size to |
| 2028 | * 10240 bytes, enable secure 802.1q tags, don't discard tagged or |
| 2029 | * untagged frames on this port, do a destination address lookup on all |
| 2030 | * received packets as usual, disable ARP mirroring and don't send a |
| 2031 | * copy of all transmitted/received frames on this port to the CPU. |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2032 | */ |
| 2033 | reg = 0; |
| 2034 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 2035 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 2036 | mv88e6xxx_6095_family(ds) || mv88e6xxx_6320_family(ds)) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2037 | reg = PORT_CONTROL_2_MAP_DA; |
| 2038 | |
| 2039 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 2040 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6320_family(ds)) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2041 | reg |= PORT_CONTROL_2_JUMBO_10240; |
| 2042 | |
| 2043 | if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds)) { |
| 2044 | /* Set the upstream port this port should use */ |
| 2045 | reg |= dsa_upstream_port(ds); |
| 2046 | /* enable forwarding of unknown multicast addresses to |
| 2047 | * the upstream port |
| 2048 | */ |
| 2049 | if (port == dsa_upstream_port(ds)) |
| 2050 | reg |= PORT_CONTROL_2_FORWARD_UNKNOWN; |
| 2051 | } |
| 2052 | |
Vivien Didelot | f5117ce | 2015-08-19 18:54:55 -0400 | [diff] [blame] | 2053 | reg |= PORT_CONTROL_2_8021Q_FALLBACK; |
Vivien Didelot | 8efdda4 | 2015-08-13 12:52:23 -0400 | [diff] [blame] | 2054 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2055 | if (reg) { |
| 2056 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 2057 | PORT_CONTROL_2, reg); |
| 2058 | if (ret) |
| 2059 | goto abort; |
| 2060 | } |
| 2061 | |
| 2062 | /* Port Association Vector: when learning source addresses |
| 2063 | * of packets, add the address to the address database using |
| 2064 | * a port bitmap that has only the bit for this port set and |
| 2065 | * the other bits clear. |
| 2066 | */ |
| 2067 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_ASSOC_VECTOR, |
| 2068 | 1 << port); |
| 2069 | if (ret) |
| 2070 | goto abort; |
| 2071 | |
| 2072 | /* Egress rate control 2: disable egress rate control. */ |
| 2073 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_RATE_CONTROL_2, |
| 2074 | 0x0000); |
| 2075 | if (ret) |
| 2076 | goto abort; |
| 2077 | |
| 2078 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 2079 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 2080 | mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2081 | /* Do not limit the period of time that this port can |
| 2082 | * be paused for by the remote end or the period of |
| 2083 | * time that this port can pause the remote end. |
| 2084 | */ |
| 2085 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 2086 | PORT_PAUSE_CTRL, 0x0000); |
| 2087 | if (ret) |
| 2088 | goto abort; |
| 2089 | |
| 2090 | /* Port ATU control: disable limiting the number of |
| 2091 | * address database entries that this port is allowed |
| 2092 | * to use. |
| 2093 | */ |
| 2094 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 2095 | PORT_ATU_CONTROL, 0x0000); |
| 2096 | /* Priority Override: disable DA, SA and VTU priority |
| 2097 | * override. |
| 2098 | */ |
| 2099 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 2100 | PORT_PRI_OVERRIDE, 0x0000); |
| 2101 | if (ret) |
| 2102 | goto abort; |
| 2103 | |
| 2104 | /* Port Ethertype: use the Ethertype DSA Ethertype |
| 2105 | * value. |
| 2106 | */ |
| 2107 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 2108 | PORT_ETH_TYPE, ETH_P_EDSA); |
| 2109 | if (ret) |
| 2110 | goto abort; |
| 2111 | /* Tag Remap: use an identity 802.1p prio -> switch |
| 2112 | * prio mapping. |
| 2113 | */ |
| 2114 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 2115 | PORT_TAG_REGMAP_0123, 0x3210); |
| 2116 | if (ret) |
| 2117 | goto abort; |
| 2118 | |
| 2119 | /* Tag Remap 2: use an identity 802.1p prio -> switch |
| 2120 | * prio mapping. |
| 2121 | */ |
| 2122 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 2123 | PORT_TAG_REGMAP_4567, 0x7654); |
| 2124 | if (ret) |
| 2125 | goto abort; |
| 2126 | } |
| 2127 | |
| 2128 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 2129 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 2130 | mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) || |
| 2131 | mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2132 | /* Rate Control: disable ingress rate limiting. */ |
| 2133 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 2134 | PORT_RATE_CONTROL, 0x0001); |
| 2135 | if (ret) |
| 2136 | goto abort; |
| 2137 | } |
| 2138 | |
Guenter Roeck | 366f0a0 | 2015-03-26 18:36:30 -0700 | [diff] [blame] | 2139 | /* Port Control 1: disable trunking, disable sending |
| 2140 | * learning messages to this port. |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2141 | */ |
Vivien Didelot | 614f03f | 2015-04-20 17:19:23 -0400 | [diff] [blame] | 2142 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_1, 0x0000); |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2143 | if (ret) |
| 2144 | goto abort; |
| 2145 | |
| 2146 | /* Port based VLAN map: give each port its own address |
| 2147 | * database, allow the CPU port to talk to each of the 'real' |
| 2148 | * ports, and allow each of the 'real' ports to only talk to |
| 2149 | * the upstream port. |
| 2150 | */ |
Vivien Didelot | 194fea7 | 2015-08-10 09:09:47 -0400 | [diff] [blame] | 2151 | fid = port + 1; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 2152 | ps->fid[port] = fid; |
Vivien Didelot | 194fea7 | 2015-08-10 09:09:47 -0400 | [diff] [blame] | 2153 | set_bit(fid, ps->fid_bitmap); |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2154 | |
Vivien Didelot | ede8098 | 2015-10-11 18:08:35 -0400 | [diff] [blame^] | 2155 | if (dsa_is_cpu_port(ds, port)) |
| 2156 | reg = BIT(ps->num_ports) - 1; |
| 2157 | else |
| 2158 | reg = BIT(dsa_upstream_port(ds)); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 2159 | |
Vivien Didelot | ede8098 | 2015-10-11 18:08:35 -0400 | [diff] [blame^] | 2160 | ret = _mv88e6xxx_port_vlan_map_set(ds, port, reg & ~port); |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2161 | if (ret) |
| 2162 | goto abort; |
| 2163 | |
| 2164 | /* Default VLAN ID and priority: don't set a default VLAN |
| 2165 | * ID, and set the default packet priority to zero. |
| 2166 | */ |
Vivien Didelot | 47cf1e65 | 2015-04-20 17:43:26 -0400 | [diff] [blame] | 2167 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_DEFAULT_VLAN, |
| 2168 | 0x0000); |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2169 | abort: |
| 2170 | mutex_unlock(&ps->smi_mutex); |
| 2171 | return ret; |
| 2172 | } |
| 2173 | |
Andrew Lunn | dbde9e6 | 2015-05-06 01:09:48 +0200 | [diff] [blame] | 2174 | int mv88e6xxx_setup_ports(struct dsa_switch *ds) |
| 2175 | { |
| 2176 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2177 | int ret; |
| 2178 | int i; |
| 2179 | |
| 2180 | for (i = 0; i < ps->num_ports; i++) { |
| 2181 | ret = mv88e6xxx_setup_port(ds, i); |
| 2182 | if (ret < 0) |
| 2183 | return ret; |
| 2184 | } |
| 2185 | return 0; |
| 2186 | } |
| 2187 | |
Andrew Lunn | 87c8cef | 2015-06-20 18:42:28 +0200 | [diff] [blame] | 2188 | static int mv88e6xxx_regs_show(struct seq_file *s, void *p) |
| 2189 | { |
| 2190 | struct dsa_switch *ds = s->private; |
| 2191 | |
| 2192 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2193 | int reg, port; |
| 2194 | |
| 2195 | seq_puts(s, " GLOBAL GLOBAL2 "); |
| 2196 | for (port = 0 ; port < ps->num_ports; port++) |
| 2197 | seq_printf(s, " %2d ", port); |
| 2198 | seq_puts(s, "\n"); |
| 2199 | |
| 2200 | for (reg = 0; reg < 32; reg++) { |
| 2201 | seq_printf(s, "%2x: ", reg); |
| 2202 | seq_printf(s, " %4x %4x ", |
| 2203 | mv88e6xxx_reg_read(ds, REG_GLOBAL, reg), |
| 2204 | mv88e6xxx_reg_read(ds, REG_GLOBAL2, reg)); |
| 2205 | |
| 2206 | for (port = 0 ; port < ps->num_ports; port++) |
| 2207 | seq_printf(s, "%4x ", |
| 2208 | mv88e6xxx_reg_read(ds, REG_PORT(port), reg)); |
| 2209 | seq_puts(s, "\n"); |
| 2210 | } |
| 2211 | |
| 2212 | return 0; |
| 2213 | } |
| 2214 | |
| 2215 | static int mv88e6xxx_regs_open(struct inode *inode, struct file *file) |
| 2216 | { |
| 2217 | return single_open(file, mv88e6xxx_regs_show, inode->i_private); |
| 2218 | } |
| 2219 | |
| 2220 | static const struct file_operations mv88e6xxx_regs_fops = { |
| 2221 | .open = mv88e6xxx_regs_open, |
| 2222 | .read = seq_read, |
| 2223 | .llseek = no_llseek, |
| 2224 | .release = single_release, |
| 2225 | .owner = THIS_MODULE, |
| 2226 | }; |
| 2227 | |
Andrew Lunn | 8a0a265 | 2015-06-20 18:42:29 +0200 | [diff] [blame] | 2228 | static void mv88e6xxx_atu_show_header(struct seq_file *s) |
| 2229 | { |
| 2230 | seq_puts(s, "DB T/P Vec State Addr\n"); |
| 2231 | } |
| 2232 | |
| 2233 | static void mv88e6xxx_atu_show_entry(struct seq_file *s, int dbnum, |
| 2234 | unsigned char *addr, int data) |
| 2235 | { |
| 2236 | bool trunk = !!(data & GLOBAL_ATU_DATA_TRUNK); |
| 2237 | int portvec = ((data & GLOBAL_ATU_DATA_PORT_VECTOR_MASK) >> |
| 2238 | GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT); |
| 2239 | int state = data & GLOBAL_ATU_DATA_STATE_MASK; |
| 2240 | |
| 2241 | seq_printf(s, "%03x %5s %10pb %x %pM\n", |
| 2242 | dbnum, (trunk ? "Trunk" : "Port"), &portvec, state, addr); |
| 2243 | } |
| 2244 | |
| 2245 | static int mv88e6xxx_atu_show_db(struct seq_file *s, struct dsa_switch *ds, |
| 2246 | int dbnum) |
| 2247 | { |
| 2248 | unsigned char bcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
| 2249 | unsigned char addr[6]; |
| 2250 | int ret, data, state; |
| 2251 | |
Vivien Didelot | c5723ac | 2015-08-10 09:09:48 -0400 | [diff] [blame] | 2252 | ret = _mv88e6xxx_atu_mac_write(ds, bcast); |
Andrew Lunn | 8a0a265 | 2015-06-20 18:42:29 +0200 | [diff] [blame] | 2253 | if (ret < 0) |
| 2254 | return ret; |
| 2255 | |
| 2256 | do { |
Vivien Didelot | 70cc99d | 2015-09-04 14:34:10 -0400 | [diff] [blame] | 2257 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID, |
| 2258 | dbnum); |
Andrew Lunn | 8a0a265 | 2015-06-20 18:42:29 +0200 | [diff] [blame] | 2259 | if (ret < 0) |
| 2260 | return ret; |
Vivien Didelot | 70cc99d | 2015-09-04 14:34:10 -0400 | [diff] [blame] | 2261 | |
| 2262 | ret = _mv88e6xxx_atu_cmd(ds, GLOBAL_ATU_OP_GET_NEXT_DB); |
| 2263 | if (ret < 0) |
| 2264 | return ret; |
| 2265 | |
Andrew Lunn | 8a0a265 | 2015-06-20 18:42:29 +0200 | [diff] [blame] | 2266 | data = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_DATA); |
| 2267 | if (data < 0) |
| 2268 | return data; |
| 2269 | |
| 2270 | state = data & GLOBAL_ATU_DATA_STATE_MASK; |
| 2271 | if (state == GLOBAL_ATU_DATA_STATE_UNUSED) |
| 2272 | break; |
Vivien Didelot | c5723ac | 2015-08-10 09:09:48 -0400 | [diff] [blame] | 2273 | ret = _mv88e6xxx_atu_mac_read(ds, addr); |
Andrew Lunn | 8a0a265 | 2015-06-20 18:42:29 +0200 | [diff] [blame] | 2274 | if (ret < 0) |
| 2275 | return ret; |
| 2276 | mv88e6xxx_atu_show_entry(s, dbnum, addr, data); |
| 2277 | } while (state != GLOBAL_ATU_DATA_STATE_UNUSED); |
| 2278 | |
| 2279 | return 0; |
| 2280 | } |
| 2281 | |
| 2282 | static int mv88e6xxx_atu_show(struct seq_file *s, void *p) |
| 2283 | { |
| 2284 | struct dsa_switch *ds = s->private; |
| 2285 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2286 | int dbnum; |
| 2287 | |
| 2288 | mv88e6xxx_atu_show_header(s); |
| 2289 | |
| 2290 | for (dbnum = 0; dbnum < 255; dbnum++) { |
| 2291 | mutex_lock(&ps->smi_mutex); |
| 2292 | mv88e6xxx_atu_show_db(s, ds, dbnum); |
| 2293 | mutex_unlock(&ps->smi_mutex); |
| 2294 | } |
| 2295 | |
| 2296 | return 0; |
| 2297 | } |
| 2298 | |
| 2299 | static int mv88e6xxx_atu_open(struct inode *inode, struct file *file) |
| 2300 | { |
| 2301 | return single_open(file, mv88e6xxx_atu_show, inode->i_private); |
| 2302 | } |
| 2303 | |
| 2304 | static const struct file_operations mv88e6xxx_atu_fops = { |
| 2305 | .open = mv88e6xxx_atu_open, |
| 2306 | .read = seq_read, |
| 2307 | .llseek = no_llseek, |
| 2308 | .release = single_release, |
| 2309 | .owner = THIS_MODULE, |
| 2310 | }; |
| 2311 | |
Andrew Lunn | 532c7a3 | 2015-06-20 18:42:31 +0200 | [diff] [blame] | 2312 | static void mv88e6xxx_stats_show_header(struct seq_file *s, |
| 2313 | struct mv88e6xxx_priv_state *ps) |
| 2314 | { |
| 2315 | int port; |
| 2316 | |
| 2317 | seq_puts(s, " Statistic "); |
| 2318 | for (port = 0 ; port < ps->num_ports; port++) |
| 2319 | seq_printf(s, "Port %2d ", port); |
| 2320 | seq_puts(s, "\n"); |
| 2321 | } |
| 2322 | |
| 2323 | static int mv88e6xxx_stats_show(struct seq_file *s, void *p) |
| 2324 | { |
| 2325 | struct dsa_switch *ds = s->private; |
| 2326 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2327 | struct mv88e6xxx_hw_stat *stats = mv88e6xxx_hw_stats; |
| 2328 | int port, stat, max_stats; |
| 2329 | uint64_t value; |
| 2330 | |
| 2331 | if (have_sw_in_discards(ds)) |
| 2332 | max_stats = ARRAY_SIZE(mv88e6xxx_hw_stats); |
| 2333 | else |
| 2334 | max_stats = ARRAY_SIZE(mv88e6xxx_hw_stats) - 3; |
| 2335 | |
| 2336 | mv88e6xxx_stats_show_header(s, ps); |
| 2337 | |
| 2338 | mutex_lock(&ps->smi_mutex); |
| 2339 | |
| 2340 | for (stat = 0; stat < max_stats; stat++) { |
| 2341 | seq_printf(s, "%19s: ", stats[stat].string); |
| 2342 | for (port = 0 ; port < ps->num_ports; port++) { |
| 2343 | _mv88e6xxx_stats_snapshot(ds, port); |
| 2344 | value = _mv88e6xxx_get_ethtool_stat(ds, stat, stats, |
| 2345 | port); |
| 2346 | seq_printf(s, "%8llu ", value); |
| 2347 | } |
| 2348 | seq_puts(s, "\n"); |
| 2349 | } |
| 2350 | mutex_unlock(&ps->smi_mutex); |
| 2351 | |
| 2352 | return 0; |
| 2353 | } |
| 2354 | |
| 2355 | static int mv88e6xxx_stats_open(struct inode *inode, struct file *file) |
| 2356 | { |
| 2357 | return single_open(file, mv88e6xxx_stats_show, inode->i_private); |
| 2358 | } |
| 2359 | |
| 2360 | static const struct file_operations mv88e6xxx_stats_fops = { |
| 2361 | .open = mv88e6xxx_stats_open, |
| 2362 | .read = seq_read, |
| 2363 | .llseek = no_llseek, |
| 2364 | .release = single_release, |
| 2365 | .owner = THIS_MODULE, |
| 2366 | }; |
| 2367 | |
Andrew Lunn | d35bd87 | 2015-06-20 18:42:32 +0200 | [diff] [blame] | 2368 | static int mv88e6xxx_device_map_show(struct seq_file *s, void *p) |
| 2369 | { |
| 2370 | struct dsa_switch *ds = s->private; |
| 2371 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2372 | int target, ret; |
| 2373 | |
| 2374 | seq_puts(s, "Target Port\n"); |
| 2375 | |
| 2376 | mutex_lock(&ps->smi_mutex); |
| 2377 | for (target = 0; target < 32; target++) { |
| 2378 | ret = _mv88e6xxx_reg_write( |
| 2379 | ds, REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING, |
| 2380 | target << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT); |
| 2381 | if (ret < 0) |
| 2382 | goto out; |
| 2383 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL2, |
| 2384 | GLOBAL2_DEVICE_MAPPING); |
| 2385 | seq_printf(s, " %2d %2d\n", target, |
| 2386 | ret & GLOBAL2_DEVICE_MAPPING_PORT_MASK); |
| 2387 | } |
| 2388 | out: |
| 2389 | mutex_unlock(&ps->smi_mutex); |
| 2390 | |
| 2391 | return 0; |
| 2392 | } |
| 2393 | |
| 2394 | static int mv88e6xxx_device_map_open(struct inode *inode, struct file *file) |
| 2395 | { |
| 2396 | return single_open(file, mv88e6xxx_device_map_show, inode->i_private); |
| 2397 | } |
| 2398 | |
| 2399 | static const struct file_operations mv88e6xxx_device_map_fops = { |
| 2400 | .open = mv88e6xxx_device_map_open, |
| 2401 | .read = seq_read, |
| 2402 | .llseek = no_llseek, |
| 2403 | .release = single_release, |
| 2404 | .owner = THIS_MODULE, |
| 2405 | }; |
| 2406 | |
Andrew Lunn | 56d95e2 | 2015-06-20 18:42:33 +0200 | [diff] [blame] | 2407 | static int mv88e6xxx_scratch_show(struct seq_file *s, void *p) |
| 2408 | { |
| 2409 | struct dsa_switch *ds = s->private; |
| 2410 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2411 | int reg, ret; |
| 2412 | |
| 2413 | seq_puts(s, "Register Value\n"); |
| 2414 | |
| 2415 | mutex_lock(&ps->smi_mutex); |
| 2416 | for (reg = 0; reg < 0x80; reg++) { |
| 2417 | ret = _mv88e6xxx_reg_write( |
| 2418 | ds, REG_GLOBAL2, GLOBAL2_SCRATCH_MISC, |
| 2419 | reg << GLOBAL2_SCRATCH_REGISTER_SHIFT); |
| 2420 | if (ret < 0) |
| 2421 | goto out; |
| 2422 | |
| 2423 | ret = _mv88e6xxx_scratch_wait(ds); |
| 2424 | if (ret < 0) |
| 2425 | goto out; |
| 2426 | |
| 2427 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL2, |
| 2428 | GLOBAL2_SCRATCH_MISC); |
| 2429 | seq_printf(s, " %2x %2x\n", reg, |
| 2430 | ret & GLOBAL2_SCRATCH_VALUE_MASK); |
| 2431 | } |
| 2432 | out: |
| 2433 | mutex_unlock(&ps->smi_mutex); |
| 2434 | |
| 2435 | return 0; |
| 2436 | } |
| 2437 | |
| 2438 | static int mv88e6xxx_scratch_open(struct inode *inode, struct file *file) |
| 2439 | { |
| 2440 | return single_open(file, mv88e6xxx_scratch_show, inode->i_private); |
| 2441 | } |
| 2442 | |
| 2443 | static const struct file_operations mv88e6xxx_scratch_fops = { |
| 2444 | .open = mv88e6xxx_scratch_open, |
| 2445 | .read = seq_read, |
| 2446 | .llseek = no_llseek, |
| 2447 | .release = single_release, |
| 2448 | .owner = THIS_MODULE, |
| 2449 | }; |
| 2450 | |
Guenter Roeck | acdaffc | 2015-03-26 18:36:28 -0700 | [diff] [blame] | 2451 | int mv88e6xxx_setup_common(struct dsa_switch *ds) |
| 2452 | { |
| 2453 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Andrew Lunn | 87c8cef | 2015-06-20 18:42:28 +0200 | [diff] [blame] | 2454 | char *name; |
Guenter Roeck | acdaffc | 2015-03-26 18:36:28 -0700 | [diff] [blame] | 2455 | |
| 2456 | mutex_init(&ps->smi_mutex); |
Guenter Roeck | acdaffc | 2015-03-26 18:36:28 -0700 | [diff] [blame] | 2457 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 2458 | ps->id = REG_READ(REG_PORT(0), PORT_SWITCH_ID) & 0xfff0; |
Andrew Lunn | a8f064c | 2015-03-26 18:36:40 -0700 | [diff] [blame] | 2459 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 2460 | INIT_WORK(&ps->bridge_work, mv88e6xxx_bridge_work); |
| 2461 | |
Andrew Lunn | 87c8cef | 2015-06-20 18:42:28 +0200 | [diff] [blame] | 2462 | name = kasprintf(GFP_KERNEL, "dsa%d", ds->index); |
| 2463 | ps->dbgfs = debugfs_create_dir(name, NULL); |
| 2464 | kfree(name); |
| 2465 | |
| 2466 | debugfs_create_file("regs", S_IRUGO, ps->dbgfs, ds, |
| 2467 | &mv88e6xxx_regs_fops); |
| 2468 | |
Andrew Lunn | 8a0a265 | 2015-06-20 18:42:29 +0200 | [diff] [blame] | 2469 | debugfs_create_file("atu", S_IRUGO, ps->dbgfs, ds, |
| 2470 | &mv88e6xxx_atu_fops); |
| 2471 | |
Andrew Lunn | 532c7a3 | 2015-06-20 18:42:31 +0200 | [diff] [blame] | 2472 | debugfs_create_file("stats", S_IRUGO, ps->dbgfs, ds, |
| 2473 | &mv88e6xxx_stats_fops); |
| 2474 | |
Andrew Lunn | d35bd87 | 2015-06-20 18:42:32 +0200 | [diff] [blame] | 2475 | debugfs_create_file("device_map", S_IRUGO, ps->dbgfs, ds, |
| 2476 | &mv88e6xxx_device_map_fops); |
Andrew Lunn | 56d95e2 | 2015-06-20 18:42:33 +0200 | [diff] [blame] | 2477 | |
| 2478 | debugfs_create_file("scratch", S_IRUGO, ps->dbgfs, ds, |
| 2479 | &mv88e6xxx_scratch_fops); |
Guenter Roeck | acdaffc | 2015-03-26 18:36:28 -0700 | [diff] [blame] | 2480 | return 0; |
| 2481 | } |
| 2482 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2483 | int mv88e6xxx_setup_global(struct dsa_switch *ds) |
| 2484 | { |
| 2485 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | 24751e2 | 2015-08-03 09:17:44 -0400 | [diff] [blame] | 2486 | int ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2487 | int i; |
| 2488 | |
| 2489 | /* Set the default address aging time to 5 minutes, and |
| 2490 | * enable address learn messages to be sent to all message |
| 2491 | * ports. |
| 2492 | */ |
| 2493 | REG_WRITE(REG_GLOBAL, GLOBAL_ATU_CONTROL, |
| 2494 | 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL); |
| 2495 | |
| 2496 | /* Configure the IP ToS mapping registers. */ |
| 2497 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000); |
| 2498 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000); |
| 2499 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555); |
| 2500 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555); |
| 2501 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa); |
| 2502 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa); |
| 2503 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff); |
| 2504 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff); |
| 2505 | |
| 2506 | /* Configure the IEEE 802.1p priority mapping register. */ |
| 2507 | REG_WRITE(REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41); |
| 2508 | |
| 2509 | /* Send all frames with destination addresses matching |
| 2510 | * 01:80:c2:00:00:0x to the CPU port. |
| 2511 | */ |
| 2512 | REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff); |
| 2513 | |
| 2514 | /* Ignore removed tag data on doubly tagged packets, disable |
| 2515 | * flow control messages, force flow control priority to the |
| 2516 | * highest, and send all special multicast frames to the CPU |
| 2517 | * port at the highest priority. |
| 2518 | */ |
| 2519 | REG_WRITE(REG_GLOBAL2, GLOBAL2_SWITCH_MGMT, |
| 2520 | 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 | |
| 2521 | GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI); |
| 2522 | |
| 2523 | /* Program the DSA routing table. */ |
| 2524 | for (i = 0; i < 32; i++) { |
| 2525 | int nexthop = 0x1f; |
| 2526 | |
| 2527 | if (ds->pd->rtable && |
| 2528 | i != ds->index && i < ds->dst->pd->nr_chips) |
| 2529 | nexthop = ds->pd->rtable[i] & 0x1f; |
| 2530 | |
| 2531 | REG_WRITE(REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING, |
| 2532 | GLOBAL2_DEVICE_MAPPING_UPDATE | |
| 2533 | (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) | |
| 2534 | nexthop); |
| 2535 | } |
| 2536 | |
| 2537 | /* Clear all trunk masks. */ |
| 2538 | for (i = 0; i < 8; i++) |
| 2539 | REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MASK, |
| 2540 | 0x8000 | (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) | |
| 2541 | ((1 << ps->num_ports) - 1)); |
| 2542 | |
| 2543 | /* Clear all trunk mappings. */ |
| 2544 | for (i = 0; i < 16; i++) |
| 2545 | REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MAPPING, |
| 2546 | GLOBAL2_TRUNK_MAPPING_UPDATE | |
| 2547 | (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT)); |
| 2548 | |
| 2549 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 2550 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 2551 | mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2552 | /* Send all frames with destination addresses matching |
| 2553 | * 01:80:c2:00:00:2x to the CPU port. |
| 2554 | */ |
| 2555 | REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_2X, 0xffff); |
| 2556 | |
| 2557 | /* Initialise cross-chip port VLAN table to reset |
| 2558 | * defaults. |
| 2559 | */ |
| 2560 | REG_WRITE(REG_GLOBAL2, GLOBAL2_PVT_ADDR, 0x9000); |
| 2561 | |
| 2562 | /* Clear the priority override table. */ |
| 2563 | for (i = 0; i < 16; i++) |
| 2564 | REG_WRITE(REG_GLOBAL2, GLOBAL2_PRIO_OVERRIDE, |
| 2565 | 0x8000 | (i << 8)); |
| 2566 | } |
| 2567 | |
| 2568 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 2569 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 2570 | mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) || |
| 2571 | mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2572 | /* Disable ingress rate limiting by resetting all |
| 2573 | * ingress rate limit registers to their initial |
| 2574 | * state. |
| 2575 | */ |
| 2576 | for (i = 0; i < ps->num_ports; i++) |
| 2577 | REG_WRITE(REG_GLOBAL2, GLOBAL2_INGRESS_OP, |
| 2578 | 0x9000 | (i << 8)); |
| 2579 | } |
| 2580 | |
Andrew Lunn | db687a5 | 2015-06-20 21:31:29 +0200 | [diff] [blame] | 2581 | /* Clear the statistics counters for all ports */ |
| 2582 | REG_WRITE(REG_GLOBAL, GLOBAL_STATS_OP, GLOBAL_STATS_OP_FLUSH_ALL); |
| 2583 | |
| 2584 | /* Wait for the flush to complete. */ |
Vivien Didelot | 24751e2 | 2015-08-03 09:17:44 -0400 | [diff] [blame] | 2585 | mutex_lock(&ps->smi_mutex); |
| 2586 | ret = _mv88e6xxx_stats_wait(ds); |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 2587 | if (ret < 0) |
| 2588 | goto unlock; |
| 2589 | |
Vivien Didelot | c161d0a | 2015-09-04 14:34:13 -0400 | [diff] [blame] | 2590 | /* Clear all ATU entries */ |
| 2591 | ret = _mv88e6xxx_atu_flush(ds, 0, true); |
| 2592 | if (ret < 0) |
| 2593 | goto unlock; |
| 2594 | |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 2595 | /* Clear all the VTU and STU entries */ |
| 2596 | ret = _mv88e6xxx_vtu_stu_flush(ds); |
| 2597 | unlock: |
Vivien Didelot | 24751e2 | 2015-08-03 09:17:44 -0400 | [diff] [blame] | 2598 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | db687a5 | 2015-06-20 21:31:29 +0200 | [diff] [blame] | 2599 | |
Vivien Didelot | 24751e2 | 2015-08-03 09:17:44 -0400 | [diff] [blame] | 2600 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2601 | } |
| 2602 | |
Andrew Lunn | 143a830 | 2015-04-02 04:06:34 +0200 | [diff] [blame] | 2603 | int mv88e6xxx_switch_reset(struct dsa_switch *ds, bool ppu_active) |
| 2604 | { |
| 2605 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2606 | u16 is_reset = (ppu_active ? 0x8800 : 0xc800); |
| 2607 | unsigned long timeout; |
| 2608 | int ret; |
| 2609 | int i; |
| 2610 | |
| 2611 | /* Set all ports to the disabled state. */ |
| 2612 | for (i = 0; i < ps->num_ports; i++) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 2613 | ret = REG_READ(REG_PORT(i), PORT_CONTROL); |
| 2614 | REG_WRITE(REG_PORT(i), PORT_CONTROL, ret & 0xfffc); |
Andrew Lunn | 143a830 | 2015-04-02 04:06:34 +0200 | [diff] [blame] | 2615 | } |
| 2616 | |
| 2617 | /* Wait for transmit queues to drain. */ |
| 2618 | usleep_range(2000, 4000); |
| 2619 | |
| 2620 | /* Reset the switch. Keep the PPU active if requested. The PPU |
| 2621 | * needs to be active to support indirect phy register access |
| 2622 | * through global registers 0x18 and 0x19. |
| 2623 | */ |
| 2624 | if (ppu_active) |
| 2625 | REG_WRITE(REG_GLOBAL, 0x04, 0xc000); |
| 2626 | else |
| 2627 | REG_WRITE(REG_GLOBAL, 0x04, 0xc400); |
| 2628 | |
| 2629 | /* Wait up to one second for reset to complete. */ |
| 2630 | timeout = jiffies + 1 * HZ; |
| 2631 | while (time_before(jiffies, timeout)) { |
| 2632 | ret = REG_READ(REG_GLOBAL, 0x00); |
| 2633 | if ((ret & is_reset) == is_reset) |
| 2634 | break; |
| 2635 | usleep_range(1000, 2000); |
| 2636 | } |
| 2637 | if (time_after(jiffies, timeout)) |
| 2638 | return -ETIMEDOUT; |
| 2639 | |
| 2640 | return 0; |
| 2641 | } |
| 2642 | |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 2643 | int mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page, int reg) |
| 2644 | { |
| 2645 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2646 | int ret; |
| 2647 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2648 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2649 | ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page); |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 2650 | if (ret < 0) |
| 2651 | goto error; |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2652 | ret = _mv88e6xxx_phy_read_indirect(ds, port, reg); |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 2653 | error: |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2654 | _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2655 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 2656 | return ret; |
| 2657 | } |
| 2658 | |
| 2659 | int mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page, |
| 2660 | int reg, int val) |
| 2661 | { |
| 2662 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2663 | int ret; |
| 2664 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2665 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2666 | ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page); |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 2667 | if (ret < 0) |
| 2668 | goto error; |
| 2669 | |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2670 | ret = _mv88e6xxx_phy_write_indirect(ds, port, reg, val); |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 2671 | error: |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2672 | _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2673 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2674 | return ret; |
| 2675 | } |
| 2676 | |
| 2677 | static int mv88e6xxx_port_to_phy_addr(struct dsa_switch *ds, int port) |
| 2678 | { |
| 2679 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2680 | |
| 2681 | if (port >= 0 && port < ps->num_ports) |
| 2682 | return port; |
| 2683 | return -EINVAL; |
| 2684 | } |
| 2685 | |
| 2686 | int |
| 2687 | mv88e6xxx_phy_read(struct dsa_switch *ds, int port, int regnum) |
| 2688 | { |
| 2689 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2690 | int addr = mv88e6xxx_port_to_phy_addr(ds, port); |
| 2691 | int ret; |
| 2692 | |
| 2693 | if (addr < 0) |
| 2694 | return addr; |
| 2695 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2696 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2697 | ret = _mv88e6xxx_phy_read(ds, addr, regnum); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2698 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2699 | return ret; |
| 2700 | } |
| 2701 | |
| 2702 | int |
| 2703 | mv88e6xxx_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val) |
| 2704 | { |
| 2705 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2706 | int addr = mv88e6xxx_port_to_phy_addr(ds, port); |
| 2707 | int ret; |
| 2708 | |
| 2709 | if (addr < 0) |
| 2710 | return addr; |
| 2711 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2712 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2713 | ret = _mv88e6xxx_phy_write(ds, addr, regnum, val); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2714 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2715 | return ret; |
| 2716 | } |
| 2717 | |
| 2718 | int |
| 2719 | mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int port, int regnum) |
| 2720 | { |
| 2721 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2722 | int addr = mv88e6xxx_port_to_phy_addr(ds, port); |
| 2723 | int ret; |
| 2724 | |
| 2725 | if (addr < 0) |
| 2726 | return addr; |
| 2727 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2728 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2729 | ret = _mv88e6xxx_phy_read_indirect(ds, addr, regnum); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2730 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2731 | return ret; |
| 2732 | } |
| 2733 | |
| 2734 | int |
| 2735 | mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int port, int regnum, |
| 2736 | u16 val) |
| 2737 | { |
| 2738 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2739 | int addr = mv88e6xxx_port_to_phy_addr(ds, port); |
| 2740 | int ret; |
| 2741 | |
| 2742 | if (addr < 0) |
| 2743 | return addr; |
| 2744 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2745 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2746 | ret = _mv88e6xxx_phy_write_indirect(ds, addr, regnum, val); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2747 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 2748 | return ret; |
| 2749 | } |
| 2750 | |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 2751 | #ifdef CONFIG_NET_DSA_HWMON |
| 2752 | |
| 2753 | static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp) |
| 2754 | { |
| 2755 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2756 | int ret; |
| 2757 | int val; |
| 2758 | |
| 2759 | *temp = 0; |
| 2760 | |
| 2761 | mutex_lock(&ps->smi_mutex); |
| 2762 | |
| 2763 | ret = _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x6); |
| 2764 | if (ret < 0) |
| 2765 | goto error; |
| 2766 | |
| 2767 | /* Enable temperature sensor */ |
| 2768 | ret = _mv88e6xxx_phy_read(ds, 0x0, 0x1a); |
| 2769 | if (ret < 0) |
| 2770 | goto error; |
| 2771 | |
| 2772 | ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret | (1 << 5)); |
| 2773 | if (ret < 0) |
| 2774 | goto error; |
| 2775 | |
| 2776 | /* Wait for temperature to stabilize */ |
| 2777 | usleep_range(10000, 12000); |
| 2778 | |
| 2779 | val = _mv88e6xxx_phy_read(ds, 0x0, 0x1a); |
| 2780 | if (val < 0) { |
| 2781 | ret = val; |
| 2782 | goto error; |
| 2783 | } |
| 2784 | |
| 2785 | /* Disable temperature sensor */ |
| 2786 | ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret & ~(1 << 5)); |
| 2787 | if (ret < 0) |
| 2788 | goto error; |
| 2789 | |
| 2790 | *temp = ((val & 0x1f) - 5) * 5; |
| 2791 | |
| 2792 | error: |
| 2793 | _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x0); |
| 2794 | mutex_unlock(&ps->smi_mutex); |
| 2795 | return ret; |
| 2796 | } |
| 2797 | |
| 2798 | static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp) |
| 2799 | { |
| 2800 | int phy = mv88e6xxx_6320_family(ds) ? 3 : 0; |
| 2801 | int ret; |
| 2802 | |
| 2803 | *temp = 0; |
| 2804 | |
| 2805 | ret = mv88e6xxx_phy_page_read(ds, phy, 6, 27); |
| 2806 | if (ret < 0) |
| 2807 | return ret; |
| 2808 | |
| 2809 | *temp = (ret & 0xff) - 25; |
| 2810 | |
| 2811 | return 0; |
| 2812 | } |
| 2813 | |
| 2814 | int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp) |
| 2815 | { |
| 2816 | if (mv88e6xxx_6320_family(ds) || mv88e6xxx_6352_family(ds)) |
| 2817 | return mv88e63xx_get_temp(ds, temp); |
| 2818 | |
| 2819 | return mv88e61xx_get_temp(ds, temp); |
| 2820 | } |
| 2821 | |
| 2822 | int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp) |
| 2823 | { |
| 2824 | int phy = mv88e6xxx_6320_family(ds) ? 3 : 0; |
| 2825 | int ret; |
| 2826 | |
| 2827 | if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds)) |
| 2828 | return -EOPNOTSUPP; |
| 2829 | |
| 2830 | *temp = 0; |
| 2831 | |
| 2832 | ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26); |
| 2833 | if (ret < 0) |
| 2834 | return ret; |
| 2835 | |
| 2836 | *temp = (((ret >> 8) & 0x1f) * 5) - 25; |
| 2837 | |
| 2838 | return 0; |
| 2839 | } |
| 2840 | |
| 2841 | int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp) |
| 2842 | { |
| 2843 | int phy = mv88e6xxx_6320_family(ds) ? 3 : 0; |
| 2844 | int ret; |
| 2845 | |
| 2846 | if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds)) |
| 2847 | return -EOPNOTSUPP; |
| 2848 | |
| 2849 | ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26); |
| 2850 | if (ret < 0) |
| 2851 | return ret; |
| 2852 | temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f); |
| 2853 | return mv88e6xxx_phy_page_write(ds, phy, 6, 26, |
| 2854 | (ret & 0xe0ff) | (temp << 8)); |
| 2855 | } |
| 2856 | |
| 2857 | int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm) |
| 2858 | { |
| 2859 | int phy = mv88e6xxx_6320_family(ds) ? 3 : 0; |
| 2860 | int ret; |
| 2861 | |
| 2862 | if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds)) |
| 2863 | return -EOPNOTSUPP; |
| 2864 | |
| 2865 | *alarm = false; |
| 2866 | |
| 2867 | ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26); |
| 2868 | if (ret < 0) |
| 2869 | return ret; |
| 2870 | |
| 2871 | *alarm = !!(ret & 0x40); |
| 2872 | |
| 2873 | return 0; |
| 2874 | } |
| 2875 | #endif /* CONFIG_NET_DSA_HWMON */ |
| 2876 | |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 2877 | static int __init mv88e6xxx_init(void) |
| 2878 | { |
| 2879 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131) |
| 2880 | register_switch_driver(&mv88e6131_switch_driver); |
| 2881 | #endif |
| 2882 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65) |
| 2883 | register_switch_driver(&mv88e6123_61_65_switch_driver); |
| 2884 | #endif |
Guenter Roeck | 3ad50cc | 2014-10-29 10:44:56 -0700 | [diff] [blame] | 2885 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352) |
| 2886 | register_switch_driver(&mv88e6352_switch_driver); |
| 2887 | #endif |
Andrew Lunn | 42f2725 | 2014-09-12 23:58:44 +0200 | [diff] [blame] | 2888 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171) |
| 2889 | register_switch_driver(&mv88e6171_switch_driver); |
| 2890 | #endif |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 2891 | return 0; |
| 2892 | } |
| 2893 | module_init(mv88e6xxx_init); |
| 2894 | |
| 2895 | static void __exit mv88e6xxx_cleanup(void) |
| 2896 | { |
Andrew Lunn | 42f2725 | 2014-09-12 23:58:44 +0200 | [diff] [blame] | 2897 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171) |
| 2898 | unregister_switch_driver(&mv88e6171_switch_driver); |
| 2899 | #endif |
Vivien Didelot | 4212b54 | 2015-05-01 10:43:52 -0400 | [diff] [blame] | 2900 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352) |
| 2901 | unregister_switch_driver(&mv88e6352_switch_driver); |
| 2902 | #endif |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 2903 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65) |
| 2904 | unregister_switch_driver(&mv88e6123_61_65_switch_driver); |
| 2905 | #endif |
| 2906 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131) |
| 2907 | unregister_switch_driver(&mv88e6131_switch_driver); |
| 2908 | #endif |
| 2909 | } |
| 2910 | module_exit(mv88e6xxx_cleanup); |
Ben Hutchings | 3d825ed | 2011-11-25 14:37:16 +0000 | [diff] [blame] | 2911 | |
| 2912 | MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>"); |
| 2913 | MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips"); |
| 2914 | MODULE_LICENSE("GPL"); |