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 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | */ |
| 10 | |
Andrew Lunn | 87c8cef | 2015-06-20 18:42:28 +0200 | [diff] [blame] | 11 | #include <linux/debugfs.h> |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 12 | #include <linux/delay.h> |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 13 | #include <linux/etherdevice.h> |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 14 | #include <linux/if_bridge.h> |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 15 | #include <linux/jiffies.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 16 | #include <linux/list.h> |
Paul Gortmaker | 2bbba27 | 2012-01-24 10:41:40 +0000 | [diff] [blame] | 17 | #include <linux/module.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 18 | #include <linux/netdevice.h> |
| 19 | #include <linux/phy.h> |
Andrew Lunn | 87c8cef | 2015-06-20 18:42:28 +0200 | [diff] [blame] | 20 | #include <linux/seq_file.h> |
Ben Hutchings | c8f0b86 | 2011-11-27 17:06:08 +0000 | [diff] [blame] | 21 | #include <net/dsa.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 22 | #include "mv88e6xxx.h" |
| 23 | |
Andrew Lunn | 16fe24f | 2015-05-06 01:09:55 +0200 | [diff] [blame] | 24 | /* MDIO bus access can be nested in the case of PHYs connected to the |
| 25 | * internal MDIO bus of the switch, which is accessed via MDIO bus of |
| 26 | * the Ethernet interface. Avoid lockdep false positives by using |
| 27 | * mutex_lock_nested(). |
| 28 | */ |
| 29 | static int mv88e6xxx_mdiobus_read(struct mii_bus *bus, int addr, u32 regnum) |
| 30 | { |
| 31 | int ret; |
| 32 | |
| 33 | mutex_lock_nested(&bus->mdio_lock, SINGLE_DEPTH_NESTING); |
| 34 | ret = bus->read(bus, addr, regnum); |
| 35 | mutex_unlock(&bus->mdio_lock); |
| 36 | |
| 37 | return ret; |
| 38 | } |
| 39 | |
| 40 | static int mv88e6xxx_mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, |
| 41 | u16 val) |
| 42 | { |
| 43 | int ret; |
| 44 | |
| 45 | mutex_lock_nested(&bus->mdio_lock, SINGLE_DEPTH_NESTING); |
| 46 | ret = bus->write(bus, addr, regnum, val); |
| 47 | mutex_unlock(&bus->mdio_lock); |
| 48 | |
| 49 | return ret; |
| 50 | } |
| 51 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 52 | /* 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] | 53 | * use all 32 SMI bus addresses on its SMI bus, and all switch registers |
| 54 | * will be directly accessible on some {device address,register address} |
| 55 | * pair. If the ADDR[4:0] pins are not strapped to zero, the switch |
| 56 | * will only respond to SMI transactions to that specific address, and |
| 57 | * an indirect addressing mechanism needs to be used to access its |
| 58 | * registers. |
| 59 | */ |
| 60 | static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr) |
| 61 | { |
| 62 | int ret; |
| 63 | int i; |
| 64 | |
| 65 | for (i = 0; i < 16; i++) { |
Andrew Lunn | 16fe24f | 2015-05-06 01:09:55 +0200 | [diff] [blame] | 66 | ret = mv88e6xxx_mdiobus_read(bus, sw_addr, SMI_CMD); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 67 | if (ret < 0) |
| 68 | return ret; |
| 69 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 70 | if ((ret & SMI_CMD_BUSY) == 0) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | return -ETIMEDOUT; |
| 75 | } |
| 76 | |
| 77 | int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr, int reg) |
| 78 | { |
| 79 | int ret; |
| 80 | |
| 81 | if (sw_addr == 0) |
Andrew Lunn | 16fe24f | 2015-05-06 01:09:55 +0200 | [diff] [blame] | 82 | return mv88e6xxx_mdiobus_read(bus, addr, reg); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 83 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 84 | /* Wait for the bus to become free. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 85 | ret = mv88e6xxx_reg_wait_ready(bus, sw_addr); |
| 86 | if (ret < 0) |
| 87 | return ret; |
| 88 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 89 | /* Transmit the read command. */ |
Andrew Lunn | 16fe24f | 2015-05-06 01:09:55 +0200 | [diff] [blame] | 90 | ret = mv88e6xxx_mdiobus_write(bus, sw_addr, SMI_CMD, |
| 91 | SMI_CMD_OP_22_READ | (addr << 5) | reg); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 92 | if (ret < 0) |
| 93 | return ret; |
| 94 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 95 | /* Wait for the read command to complete. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 96 | ret = mv88e6xxx_reg_wait_ready(bus, sw_addr); |
| 97 | if (ret < 0) |
| 98 | return ret; |
| 99 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 100 | /* Read the data. */ |
Andrew Lunn | 16fe24f | 2015-05-06 01:09:55 +0200 | [diff] [blame] | 101 | ret = mv88e6xxx_mdiobus_read(bus, sw_addr, SMI_DATA); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 102 | if (ret < 0) |
| 103 | return ret; |
| 104 | |
| 105 | return ret & 0xffff; |
| 106 | } |
| 107 | |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 108 | /* Must be called with SMI mutex held */ |
| 109 | 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] | 110 | { |
Guenter Roeck | b184e49 | 2014-10-17 12:30:58 -0700 | [diff] [blame] | 111 | 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] | 112 | int ret; |
| 113 | |
Guenter Roeck | b184e49 | 2014-10-17 12:30:58 -0700 | [diff] [blame] | 114 | if (bus == NULL) |
| 115 | return -EINVAL; |
| 116 | |
Guenter Roeck | b184e49 | 2014-10-17 12:30:58 -0700 | [diff] [blame] | 117 | ret = __mv88e6xxx_reg_read(bus, ds->pd->sw_addr, addr, reg); |
Vivien Didelot | bb92ea5 | 2015-01-23 16:10:36 -0500 | [diff] [blame] | 118 | if (ret < 0) |
| 119 | return ret; |
| 120 | |
| 121 | dev_dbg(ds->master_dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n", |
| 122 | addr, reg, ret); |
| 123 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 124 | return ret; |
| 125 | } |
| 126 | |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 127 | int mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg) |
| 128 | { |
| 129 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 130 | int ret; |
| 131 | |
| 132 | mutex_lock(&ps->smi_mutex); |
| 133 | ret = _mv88e6xxx_reg_read(ds, addr, reg); |
| 134 | mutex_unlock(&ps->smi_mutex); |
| 135 | |
| 136 | return ret; |
| 137 | } |
| 138 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 139 | int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr, |
| 140 | int reg, u16 val) |
| 141 | { |
| 142 | int ret; |
| 143 | |
| 144 | if (sw_addr == 0) |
Andrew Lunn | 16fe24f | 2015-05-06 01:09:55 +0200 | [diff] [blame] | 145 | return mv88e6xxx_mdiobus_write(bus, addr, reg, val); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 146 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 147 | /* Wait for the bus to become free. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 148 | ret = mv88e6xxx_reg_wait_ready(bus, sw_addr); |
| 149 | if (ret < 0) |
| 150 | return ret; |
| 151 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 152 | /* Transmit the data to write. */ |
Andrew Lunn | 16fe24f | 2015-05-06 01:09:55 +0200 | [diff] [blame] | 153 | ret = mv88e6xxx_mdiobus_write(bus, sw_addr, SMI_DATA, val); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 154 | if (ret < 0) |
| 155 | return ret; |
| 156 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 157 | /* Transmit the write command. */ |
Andrew Lunn | 16fe24f | 2015-05-06 01:09:55 +0200 | [diff] [blame] | 158 | ret = mv88e6xxx_mdiobus_write(bus, sw_addr, SMI_CMD, |
| 159 | SMI_CMD_OP_22_WRITE | (addr << 5) | reg); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 160 | if (ret < 0) |
| 161 | return ret; |
| 162 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 163 | /* Wait for the write command to complete. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 164 | ret = mv88e6xxx_reg_wait_ready(bus, sw_addr); |
| 165 | if (ret < 0) |
| 166 | return ret; |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 171 | /* Must be called with SMI mutex held */ |
| 172 | static int _mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, |
| 173 | u16 val) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 174 | { |
Guenter Roeck | b184e49 | 2014-10-17 12:30:58 -0700 | [diff] [blame] | 175 | 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] | 176 | |
Guenter Roeck | b184e49 | 2014-10-17 12:30:58 -0700 | [diff] [blame] | 177 | if (bus == NULL) |
| 178 | return -EINVAL; |
| 179 | |
Vivien Didelot | bb92ea5 | 2015-01-23 16:10:36 -0500 | [diff] [blame] | 180 | dev_dbg(ds->master_dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n", |
| 181 | addr, reg, val); |
| 182 | |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 183 | return __mv88e6xxx_reg_write(bus, ds->pd->sw_addr, addr, reg, val); |
| 184 | } |
| 185 | |
| 186 | int mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, u16 val) |
| 187 | { |
| 188 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 189 | int ret; |
| 190 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 191 | mutex_lock(&ps->smi_mutex); |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 192 | ret = _mv88e6xxx_reg_write(ds, addr, reg, val); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 193 | mutex_unlock(&ps->smi_mutex); |
| 194 | |
| 195 | return ret; |
| 196 | } |
| 197 | |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 198 | int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr) |
| 199 | { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 200 | REG_WRITE(REG_GLOBAL, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]); |
| 201 | REG_WRITE(REG_GLOBAL, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]); |
| 202 | REG_WRITE(REG_GLOBAL, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 207 | int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr) |
| 208 | { |
| 209 | int i; |
| 210 | int ret; |
| 211 | |
| 212 | for (i = 0; i < 6; i++) { |
| 213 | int j; |
| 214 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 215 | /* Write the MAC address byte. */ |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 216 | REG_WRITE(REG_GLOBAL2, GLOBAL2_SWITCH_MAC, |
| 217 | GLOBAL2_SWITCH_MAC_BUSY | (i << 8) | addr[i]); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 218 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 219 | /* Wait for the write to complete. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 220 | for (j = 0; j < 16; j++) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 221 | ret = REG_READ(REG_GLOBAL2, GLOBAL2_SWITCH_MAC); |
| 222 | if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 223 | break; |
| 224 | } |
| 225 | if (j == 16) |
| 226 | return -ETIMEDOUT; |
| 227 | } |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 232 | /* Must be called with SMI mutex held */ |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 233 | 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] | 234 | { |
| 235 | if (addr >= 0) |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 236 | return _mv88e6xxx_reg_read(ds, addr, regnum); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 237 | return 0xffff; |
| 238 | } |
| 239 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 240 | /* Must be called with SMI mutex held */ |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 241 | static int _mv88e6xxx_phy_write(struct dsa_switch *ds, int addr, int regnum, |
| 242 | u16 val) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 243 | { |
| 244 | if (addr >= 0) |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 245 | return _mv88e6xxx_reg_write(ds, addr, regnum, val); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 249 | #ifdef CONFIG_NET_DSA_MV88E6XXX_NEED_PPU |
| 250 | static int mv88e6xxx_ppu_disable(struct dsa_switch *ds) |
| 251 | { |
| 252 | int ret; |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 253 | unsigned long timeout; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 254 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 255 | ret = REG_READ(REG_GLOBAL, GLOBAL_CONTROL); |
| 256 | REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL, |
| 257 | ret & ~GLOBAL_CONTROL_PPU_ENABLE); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 258 | |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 259 | timeout = jiffies + 1 * HZ; |
| 260 | while (time_before(jiffies, timeout)) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 261 | ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS); |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 262 | usleep_range(1000, 2000); |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 263 | if ((ret & GLOBAL_STATUS_PPU_MASK) != |
| 264 | GLOBAL_STATUS_PPU_POLLING) |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 265 | return 0; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | return -ETIMEDOUT; |
| 269 | } |
| 270 | |
| 271 | static int mv88e6xxx_ppu_enable(struct dsa_switch *ds) |
| 272 | { |
| 273 | int ret; |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 274 | unsigned long timeout; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 275 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 276 | ret = REG_READ(REG_GLOBAL, GLOBAL_CONTROL); |
| 277 | REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL, ret | GLOBAL_CONTROL_PPU_ENABLE); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 278 | |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 279 | timeout = jiffies + 1 * HZ; |
| 280 | while (time_before(jiffies, timeout)) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 281 | ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS); |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 282 | usleep_range(1000, 2000); |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 283 | if ((ret & GLOBAL_STATUS_PPU_MASK) == |
| 284 | GLOBAL_STATUS_PPU_POLLING) |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 285 | return 0; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | return -ETIMEDOUT; |
| 289 | } |
| 290 | |
| 291 | static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly) |
| 292 | { |
| 293 | struct mv88e6xxx_priv_state *ps; |
| 294 | |
| 295 | ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work); |
| 296 | if (mutex_trylock(&ps->ppu_mutex)) { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 297 | struct dsa_switch *ds = ((struct dsa_switch *)ps) - 1; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 298 | |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 299 | if (mv88e6xxx_ppu_enable(ds) == 0) |
| 300 | ps->ppu_disabled = 0; |
| 301 | mutex_unlock(&ps->ppu_mutex); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps) |
| 306 | { |
| 307 | struct mv88e6xxx_priv_state *ps = (void *)_ps; |
| 308 | |
| 309 | schedule_work(&ps->ppu_work); |
| 310 | } |
| 311 | |
| 312 | static int mv88e6xxx_ppu_access_get(struct dsa_switch *ds) |
| 313 | { |
Florian Fainelli | a22adce | 2014-04-28 11:14:28 -0700 | [diff] [blame] | 314 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 315 | int ret; |
| 316 | |
| 317 | mutex_lock(&ps->ppu_mutex); |
| 318 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 319 | /* If the PHY polling unit is enabled, disable it so that |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 320 | * we can access the PHY registers. If it was already |
| 321 | * disabled, cancel the timer that is going to re-enable |
| 322 | * it. |
| 323 | */ |
| 324 | if (!ps->ppu_disabled) { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 325 | ret = mv88e6xxx_ppu_disable(ds); |
| 326 | if (ret < 0) { |
| 327 | mutex_unlock(&ps->ppu_mutex); |
| 328 | return ret; |
| 329 | } |
| 330 | ps->ppu_disabled = 1; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 331 | } else { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 332 | del_timer(&ps->ppu_timer); |
| 333 | ret = 0; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | return ret; |
| 337 | } |
| 338 | |
| 339 | static void mv88e6xxx_ppu_access_put(struct dsa_switch *ds) |
| 340 | { |
Florian Fainelli | a22adce | 2014-04-28 11:14:28 -0700 | [diff] [blame] | 341 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 342 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 343 | /* Schedule a timer to re-enable the PHY polling unit. */ |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 344 | mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10)); |
| 345 | mutex_unlock(&ps->ppu_mutex); |
| 346 | } |
| 347 | |
| 348 | void mv88e6xxx_ppu_state_init(struct dsa_switch *ds) |
| 349 | { |
Florian Fainelli | a22adce | 2014-04-28 11:14:28 -0700 | [diff] [blame] | 350 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 351 | |
| 352 | mutex_init(&ps->ppu_mutex); |
| 353 | INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work); |
| 354 | init_timer(&ps->ppu_timer); |
| 355 | ps->ppu_timer.data = (unsigned long)ps; |
| 356 | ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer; |
| 357 | } |
| 358 | |
| 359 | int mv88e6xxx_phy_read_ppu(struct dsa_switch *ds, int addr, int regnum) |
| 360 | { |
| 361 | int ret; |
| 362 | |
| 363 | ret = mv88e6xxx_ppu_access_get(ds); |
| 364 | if (ret >= 0) { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 365 | ret = mv88e6xxx_reg_read(ds, addr, regnum); |
| 366 | mv88e6xxx_ppu_access_put(ds); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | return ret; |
| 370 | } |
| 371 | |
| 372 | int mv88e6xxx_phy_write_ppu(struct dsa_switch *ds, int addr, |
| 373 | int regnum, u16 val) |
| 374 | { |
| 375 | int ret; |
| 376 | |
| 377 | ret = mv88e6xxx_ppu_access_get(ds); |
| 378 | if (ret >= 0) { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 379 | ret = mv88e6xxx_reg_write(ds, addr, regnum, val); |
| 380 | mv88e6xxx_ppu_access_put(ds); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | return ret; |
| 384 | } |
| 385 | #endif |
| 386 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 387 | void mv88e6xxx_poll_link(struct dsa_switch *ds) |
| 388 | { |
| 389 | int i; |
| 390 | |
| 391 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
| 392 | struct net_device *dev; |
Ingo Molnar | 2a9e797 | 2008-11-25 16:50:49 -0800 | [diff] [blame] | 393 | int uninitialized_var(port_status); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 394 | int link; |
| 395 | int speed; |
| 396 | int duplex; |
| 397 | int fc; |
| 398 | |
| 399 | dev = ds->ports[i]; |
| 400 | if (dev == NULL) |
| 401 | continue; |
| 402 | |
| 403 | link = 0; |
| 404 | if (dev->flags & IFF_UP) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 405 | port_status = mv88e6xxx_reg_read(ds, REG_PORT(i), |
| 406 | PORT_STATUS); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 407 | if (port_status < 0) |
| 408 | continue; |
| 409 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 410 | link = !!(port_status & PORT_STATUS_LINK); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | if (!link) { |
| 414 | if (netif_carrier_ok(dev)) { |
Barry Grussling | ab381a9 | 2013-01-08 16:05:55 +0000 | [diff] [blame] | 415 | netdev_info(dev, "link down\n"); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 416 | netif_carrier_off(dev); |
| 417 | } |
| 418 | continue; |
| 419 | } |
| 420 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 421 | switch (port_status & PORT_STATUS_SPEED_MASK) { |
| 422 | case PORT_STATUS_SPEED_10: |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 423 | speed = 10; |
| 424 | break; |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 425 | case PORT_STATUS_SPEED_100: |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 426 | speed = 100; |
| 427 | break; |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 428 | case PORT_STATUS_SPEED_1000: |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 429 | speed = 1000; |
| 430 | break; |
| 431 | default: |
| 432 | speed = -1; |
| 433 | break; |
| 434 | } |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 435 | duplex = (port_status & PORT_STATUS_DUPLEX) ? 1 : 0; |
| 436 | fc = (port_status & PORT_STATUS_PAUSE_EN) ? 1 : 0; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 437 | |
| 438 | if (!netif_carrier_ok(dev)) { |
Barry Grussling | ab381a9 | 2013-01-08 16:05:55 +0000 | [diff] [blame] | 439 | netdev_info(dev, |
| 440 | "link up, %d Mb/s, %s duplex, flow control %sabled\n", |
| 441 | speed, |
| 442 | duplex ? "full" : "half", |
| 443 | fc ? "en" : "dis"); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 444 | netif_carrier_on(dev); |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 449 | static bool mv88e6xxx_6065_family(struct dsa_switch *ds) |
| 450 | { |
| 451 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 452 | |
| 453 | switch (ps->id) { |
| 454 | case PORT_SWITCH_ID_6031: |
| 455 | case PORT_SWITCH_ID_6061: |
| 456 | case PORT_SWITCH_ID_6035: |
| 457 | case PORT_SWITCH_ID_6065: |
| 458 | return true; |
| 459 | } |
| 460 | return false; |
| 461 | } |
| 462 | |
| 463 | static bool mv88e6xxx_6095_family(struct dsa_switch *ds) |
| 464 | { |
| 465 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 466 | |
| 467 | switch (ps->id) { |
| 468 | case PORT_SWITCH_ID_6092: |
| 469 | case PORT_SWITCH_ID_6095: |
| 470 | return true; |
| 471 | } |
| 472 | return false; |
| 473 | } |
| 474 | |
| 475 | static bool mv88e6xxx_6097_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_6046: |
| 481 | case PORT_SWITCH_ID_6085: |
| 482 | case PORT_SWITCH_ID_6096: |
| 483 | case PORT_SWITCH_ID_6097: |
| 484 | return true; |
| 485 | } |
| 486 | return false; |
| 487 | } |
| 488 | |
| 489 | static bool mv88e6xxx_6165_family(struct dsa_switch *ds) |
| 490 | { |
| 491 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 492 | |
| 493 | switch (ps->id) { |
| 494 | case PORT_SWITCH_ID_6123: |
| 495 | case PORT_SWITCH_ID_6161: |
| 496 | case PORT_SWITCH_ID_6165: |
| 497 | return true; |
| 498 | } |
| 499 | return false; |
| 500 | } |
| 501 | |
| 502 | static bool mv88e6xxx_6185_family(struct dsa_switch *ds) |
| 503 | { |
| 504 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 505 | |
| 506 | switch (ps->id) { |
| 507 | case PORT_SWITCH_ID_6121: |
| 508 | case PORT_SWITCH_ID_6122: |
| 509 | case PORT_SWITCH_ID_6152: |
| 510 | case PORT_SWITCH_ID_6155: |
| 511 | case PORT_SWITCH_ID_6182: |
| 512 | case PORT_SWITCH_ID_6185: |
| 513 | case PORT_SWITCH_ID_6108: |
| 514 | case PORT_SWITCH_ID_6131: |
| 515 | return true; |
| 516 | } |
| 517 | return false; |
| 518 | } |
| 519 | |
| 520 | static bool mv88e6xxx_6351_family(struct dsa_switch *ds) |
| 521 | { |
| 522 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 523 | |
| 524 | switch (ps->id) { |
| 525 | case PORT_SWITCH_ID_6171: |
| 526 | case PORT_SWITCH_ID_6175: |
| 527 | case PORT_SWITCH_ID_6350: |
| 528 | case PORT_SWITCH_ID_6351: |
| 529 | return true; |
| 530 | } |
| 531 | return false; |
| 532 | } |
| 533 | |
Andrew Lunn | f3a8b6b | 2015-04-02 04:06:40 +0200 | [diff] [blame] | 534 | static bool mv88e6xxx_6352_family(struct dsa_switch *ds) |
| 535 | { |
| 536 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 537 | |
| 538 | switch (ps->id) { |
Andrew Lunn | f3a8b6b | 2015-04-02 04:06:40 +0200 | [diff] [blame] | 539 | case PORT_SWITCH_ID_6172: |
| 540 | case PORT_SWITCH_ID_6176: |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 541 | case PORT_SWITCH_ID_6240: |
| 542 | case PORT_SWITCH_ID_6352: |
Andrew Lunn | f3a8b6b | 2015-04-02 04:06:40 +0200 | [diff] [blame] | 543 | return true; |
| 544 | } |
| 545 | return false; |
| 546 | } |
| 547 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 548 | /* Must be called with SMI mutex held */ |
| 549 | static int _mv88e6xxx_stats_wait(struct dsa_switch *ds) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 550 | { |
| 551 | int ret; |
| 552 | int i; |
| 553 | |
| 554 | for (i = 0; i < 10; i++) { |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 555 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_OP); |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 556 | if ((ret & GLOBAL_STATS_OP_BUSY) == 0) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | return -ETIMEDOUT; |
| 561 | } |
| 562 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 563 | /* Must be called with SMI mutex held */ |
| 564 | static int _mv88e6xxx_stats_snapshot(struct dsa_switch *ds, int port) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 565 | { |
| 566 | int ret; |
| 567 | |
Andrew Lunn | f3a8b6b | 2015-04-02 04:06:40 +0200 | [diff] [blame] | 568 | if (mv88e6xxx_6352_family(ds)) |
| 569 | port = (port + 1) << 5; |
| 570 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 571 | /* Snapshot the hardware statistics counters for this port. */ |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 572 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP, |
| 573 | GLOBAL_STATS_OP_CAPTURE_PORT | |
| 574 | GLOBAL_STATS_OP_HIST_RX_TX | port); |
| 575 | if (ret < 0) |
| 576 | return ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 577 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 578 | /* Wait for the snapshotting to complete. */ |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 579 | ret = _mv88e6xxx_stats_wait(ds); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 580 | if (ret < 0) |
| 581 | return ret; |
| 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 586 | /* Must be called with SMI mutex held */ |
| 587 | 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] | 588 | { |
| 589 | u32 _val; |
| 590 | int ret; |
| 591 | |
| 592 | *val = 0; |
| 593 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 594 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP, |
| 595 | GLOBAL_STATS_OP_READ_CAPTURED | |
| 596 | GLOBAL_STATS_OP_HIST_RX_TX | stat); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 597 | if (ret < 0) |
| 598 | return; |
| 599 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 600 | ret = _mv88e6xxx_stats_wait(ds); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 601 | if (ret < 0) |
| 602 | return; |
| 603 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 604 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_32); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 605 | if (ret < 0) |
| 606 | return; |
| 607 | |
| 608 | _val = ret << 16; |
| 609 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 610 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_01); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 611 | if (ret < 0) |
| 612 | return; |
| 613 | |
| 614 | *val = _val | ret; |
| 615 | } |
| 616 | |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 617 | static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = { |
| 618 | { "in_good_octets", 8, 0x00, }, |
| 619 | { "in_bad_octets", 4, 0x02, }, |
| 620 | { "in_unicast", 4, 0x04, }, |
| 621 | { "in_broadcasts", 4, 0x06, }, |
| 622 | { "in_multicasts", 4, 0x07, }, |
| 623 | { "in_pause", 4, 0x16, }, |
| 624 | { "in_undersize", 4, 0x18, }, |
| 625 | { "in_fragments", 4, 0x19, }, |
| 626 | { "in_oversize", 4, 0x1a, }, |
| 627 | { "in_jabber", 4, 0x1b, }, |
| 628 | { "in_rx_error", 4, 0x1c, }, |
| 629 | { "in_fcs_error", 4, 0x1d, }, |
| 630 | { "out_octets", 8, 0x0e, }, |
| 631 | { "out_unicast", 4, 0x10, }, |
| 632 | { "out_broadcasts", 4, 0x13, }, |
| 633 | { "out_multicasts", 4, 0x12, }, |
| 634 | { "out_pause", 4, 0x15, }, |
| 635 | { "excessive", 4, 0x11, }, |
| 636 | { "collisions", 4, 0x1e, }, |
| 637 | { "deferred", 4, 0x05, }, |
| 638 | { "single", 4, 0x14, }, |
| 639 | { "multiple", 4, 0x17, }, |
| 640 | { "out_fcs_error", 4, 0x03, }, |
| 641 | { "late", 4, 0x1f, }, |
| 642 | { "hist_64bytes", 4, 0x08, }, |
| 643 | { "hist_65_127bytes", 4, 0x09, }, |
| 644 | { "hist_128_255bytes", 4, 0x0a, }, |
| 645 | { "hist_256_511bytes", 4, 0x0b, }, |
| 646 | { "hist_512_1023bytes", 4, 0x0c, }, |
| 647 | { "hist_1024_max_bytes", 4, 0x0d, }, |
| 648 | /* Not all devices have the following counters */ |
| 649 | { "sw_in_discards", 4, 0x110, }, |
| 650 | { "sw_in_filtered", 2, 0x112, }, |
| 651 | { "sw_out_filtered", 2, 0x113, }, |
| 652 | |
| 653 | }; |
| 654 | |
| 655 | static bool have_sw_in_discards(struct dsa_switch *ds) |
| 656 | { |
| 657 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 658 | |
| 659 | switch (ps->id) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 660 | case PORT_SWITCH_ID_6095: case PORT_SWITCH_ID_6161: |
| 661 | case PORT_SWITCH_ID_6165: case PORT_SWITCH_ID_6171: |
| 662 | case PORT_SWITCH_ID_6172: case PORT_SWITCH_ID_6176: |
| 663 | case PORT_SWITCH_ID_6182: case PORT_SWITCH_ID_6185: |
| 664 | case PORT_SWITCH_ID_6352: |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 665 | return true; |
| 666 | default: |
| 667 | return false; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | static void _mv88e6xxx_get_strings(struct dsa_switch *ds, |
| 672 | int nr_stats, |
| 673 | struct mv88e6xxx_hw_stat *stats, |
| 674 | int port, uint8_t *data) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 675 | { |
| 676 | int i; |
| 677 | |
| 678 | for (i = 0; i < nr_stats; i++) { |
| 679 | memcpy(data + i * ETH_GSTRING_LEN, |
| 680 | stats[i].string, ETH_GSTRING_LEN); |
| 681 | } |
| 682 | } |
| 683 | |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 684 | static void _mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, |
| 685 | int nr_stats, |
| 686 | struct mv88e6xxx_hw_stat *stats, |
| 687 | int port, uint64_t *data) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 688 | { |
Florian Fainelli | a22adce | 2014-04-28 11:14:28 -0700 | [diff] [blame] | 689 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 690 | int ret; |
| 691 | int i; |
| 692 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 693 | mutex_lock(&ps->smi_mutex); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 694 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 695 | ret = _mv88e6xxx_stats_snapshot(ds, port); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 696 | if (ret < 0) { |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 697 | mutex_unlock(&ps->smi_mutex); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 698 | return; |
| 699 | } |
| 700 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 701 | /* Read each of the counters. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 702 | for (i = 0; i < nr_stats; i++) { |
| 703 | struct mv88e6xxx_hw_stat *s = stats + i; |
| 704 | u32 low; |
Guenter Roeck | 17ee3e0 | 2014-10-29 10:45:07 -0700 | [diff] [blame] | 705 | u32 high = 0; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 706 | |
Guenter Roeck | 17ee3e0 | 2014-10-29 10:45:07 -0700 | [diff] [blame] | 707 | if (s->reg >= 0x100) { |
Andrew Lunn | 80edb72 | 2015-06-05 01:10:09 +0200 | [diff] [blame] | 708 | ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), |
| 709 | s->reg - 0x100); |
Guenter Roeck | 17ee3e0 | 2014-10-29 10:45:07 -0700 | [diff] [blame] | 710 | if (ret < 0) |
| 711 | goto error; |
| 712 | low = ret; |
| 713 | if (s->sizeof_stat == 4) { |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 714 | ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), |
| 715 | s->reg - 0x100 + 1); |
Guenter Roeck | 17ee3e0 | 2014-10-29 10:45:07 -0700 | [diff] [blame] | 716 | if (ret < 0) |
| 717 | goto error; |
| 718 | high = ret; |
| 719 | } |
| 720 | data[i] = (((u64)high) << 16) | low; |
| 721 | continue; |
| 722 | } |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 723 | _mv88e6xxx_stats_read(ds, s->reg, &low); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 724 | if (s->sizeof_stat == 8) |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 725 | _mv88e6xxx_stats_read(ds, s->reg + 1, &high); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 726 | |
| 727 | data[i] = (((u64)high) << 32) | low; |
| 728 | } |
Guenter Roeck | 17ee3e0 | 2014-10-29 10:45:07 -0700 | [diff] [blame] | 729 | error: |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 730 | mutex_unlock(&ps->smi_mutex); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 731 | } |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 732 | |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 733 | /* All the statistics in the table */ |
| 734 | void |
| 735 | mv88e6xxx_get_strings(struct dsa_switch *ds, int port, uint8_t *data) |
| 736 | { |
| 737 | if (have_sw_in_discards(ds)) |
| 738 | _mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6xxx_hw_stats), |
| 739 | mv88e6xxx_hw_stats, port, data); |
| 740 | else |
| 741 | _mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6xxx_hw_stats) - 3, |
| 742 | mv88e6xxx_hw_stats, port, data); |
| 743 | } |
| 744 | |
| 745 | int mv88e6xxx_get_sset_count(struct dsa_switch *ds) |
| 746 | { |
| 747 | if (have_sw_in_discards(ds)) |
| 748 | return ARRAY_SIZE(mv88e6xxx_hw_stats); |
| 749 | return ARRAY_SIZE(mv88e6xxx_hw_stats) - 3; |
| 750 | } |
| 751 | |
| 752 | void |
| 753 | mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, |
| 754 | int port, uint64_t *data) |
| 755 | { |
| 756 | if (have_sw_in_discards(ds)) |
| 757 | _mv88e6xxx_get_ethtool_stats( |
| 758 | ds, ARRAY_SIZE(mv88e6xxx_hw_stats), |
| 759 | mv88e6xxx_hw_stats, port, data); |
| 760 | else |
| 761 | _mv88e6xxx_get_ethtool_stats( |
| 762 | ds, ARRAY_SIZE(mv88e6xxx_hw_stats) - 3, |
| 763 | mv88e6xxx_hw_stats, port, data); |
| 764 | } |
| 765 | |
Guenter Roeck | a1ab91f | 2014-10-29 10:45:05 -0700 | [diff] [blame] | 766 | int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port) |
| 767 | { |
| 768 | return 32 * sizeof(u16); |
| 769 | } |
| 770 | |
| 771 | void mv88e6xxx_get_regs(struct dsa_switch *ds, int port, |
| 772 | struct ethtool_regs *regs, void *_p) |
| 773 | { |
| 774 | u16 *p = _p; |
| 775 | int i; |
| 776 | |
| 777 | regs->version = 0; |
| 778 | |
| 779 | memset(p, 0xff, 32 * sizeof(u16)); |
| 780 | |
| 781 | for (i = 0; i < 32; i++) { |
| 782 | int ret; |
| 783 | |
| 784 | ret = mv88e6xxx_reg_read(ds, REG_PORT(port), i); |
| 785 | if (ret >= 0) |
| 786 | p[i] = ret; |
| 787 | } |
| 788 | } |
| 789 | |
Andrew Lunn | eaa2376 | 2014-11-15 22:24:51 +0100 | [diff] [blame] | 790 | #ifdef CONFIG_NET_DSA_HWMON |
| 791 | |
| 792 | int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp) |
| 793 | { |
| 794 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 795 | int ret; |
| 796 | int val; |
| 797 | |
| 798 | *temp = 0; |
| 799 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 800 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | eaa2376 | 2014-11-15 22:24:51 +0100 | [diff] [blame] | 801 | |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 802 | ret = _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x6); |
Andrew Lunn | eaa2376 | 2014-11-15 22:24:51 +0100 | [diff] [blame] | 803 | if (ret < 0) |
| 804 | goto error; |
| 805 | |
| 806 | /* Enable temperature sensor */ |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 807 | ret = _mv88e6xxx_phy_read(ds, 0x0, 0x1a); |
Andrew Lunn | eaa2376 | 2014-11-15 22:24:51 +0100 | [diff] [blame] | 808 | if (ret < 0) |
| 809 | goto error; |
| 810 | |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 811 | ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret | (1 << 5)); |
Andrew Lunn | eaa2376 | 2014-11-15 22:24:51 +0100 | [diff] [blame] | 812 | if (ret < 0) |
| 813 | goto error; |
| 814 | |
| 815 | /* Wait for temperature to stabilize */ |
| 816 | usleep_range(10000, 12000); |
| 817 | |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 818 | val = _mv88e6xxx_phy_read(ds, 0x0, 0x1a); |
Andrew Lunn | eaa2376 | 2014-11-15 22:24:51 +0100 | [diff] [blame] | 819 | if (val < 0) { |
| 820 | ret = val; |
| 821 | goto error; |
| 822 | } |
| 823 | |
| 824 | /* Disable temperature sensor */ |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 825 | ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret & ~(1 << 5)); |
Andrew Lunn | eaa2376 | 2014-11-15 22:24:51 +0100 | [diff] [blame] | 826 | if (ret < 0) |
| 827 | goto error; |
| 828 | |
| 829 | *temp = ((val & 0x1f) - 5) * 5; |
| 830 | |
| 831 | error: |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 832 | _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x0); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 833 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | eaa2376 | 2014-11-15 22:24:51 +0100 | [diff] [blame] | 834 | return ret; |
| 835 | } |
| 836 | #endif /* CONFIG_NET_DSA_HWMON */ |
| 837 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 838 | /* Must be called with SMI lock held */ |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 839 | static int _mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset, |
| 840 | u16 mask) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 841 | { |
| 842 | unsigned long timeout = jiffies + HZ / 10; |
| 843 | |
| 844 | while (time_before(jiffies, timeout)) { |
| 845 | int ret; |
| 846 | |
| 847 | ret = _mv88e6xxx_reg_read(ds, reg, offset); |
| 848 | if (ret < 0) |
| 849 | return ret; |
| 850 | if (!(ret & mask)) |
| 851 | return 0; |
| 852 | |
| 853 | usleep_range(1000, 2000); |
| 854 | } |
| 855 | return -ETIMEDOUT; |
| 856 | } |
| 857 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 858 | static int mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset, u16 mask) |
| 859 | { |
| 860 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 861 | int ret; |
| 862 | |
| 863 | mutex_lock(&ps->smi_mutex); |
| 864 | ret = _mv88e6xxx_wait(ds, reg, offset, mask); |
| 865 | mutex_unlock(&ps->smi_mutex); |
| 866 | |
| 867 | return ret; |
| 868 | } |
| 869 | |
| 870 | static int _mv88e6xxx_phy_wait(struct dsa_switch *ds) |
| 871 | { |
| 872 | return _mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_SMI_OP, |
| 873 | GLOBAL2_SMI_OP_BUSY); |
| 874 | } |
| 875 | |
| 876 | int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds) |
| 877 | { |
| 878 | return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP, |
| 879 | GLOBAL2_EEPROM_OP_LOAD); |
| 880 | } |
| 881 | |
| 882 | int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds) |
| 883 | { |
| 884 | return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP, |
| 885 | GLOBAL2_EEPROM_OP_BUSY); |
| 886 | } |
| 887 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 888 | /* Must be called with SMI lock held */ |
| 889 | static int _mv88e6xxx_atu_wait(struct dsa_switch *ds) |
| 890 | { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 891 | return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_ATU_OP, |
| 892 | GLOBAL_ATU_OP_BUSY); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 895 | /* Must be called with SMI mutex held */ |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 896 | static int _mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int addr, |
| 897 | int regnum) |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 898 | { |
| 899 | int ret; |
| 900 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 901 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP, |
| 902 | GLOBAL2_SMI_OP_22_READ | (addr << 5) | |
| 903 | regnum); |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 904 | if (ret < 0) |
| 905 | return ret; |
| 906 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 907 | ret = _mv88e6xxx_phy_wait(ds); |
| 908 | if (ret < 0) |
| 909 | return ret; |
| 910 | |
| 911 | return _mv88e6xxx_reg_read(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA); |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 912 | } |
| 913 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 914 | /* Must be called with SMI mutex held */ |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 915 | static int _mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int addr, |
| 916 | int regnum, u16 val) |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 917 | { |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 918 | int ret; |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 919 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 920 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA, val); |
| 921 | if (ret < 0) |
| 922 | return ret; |
| 923 | |
| 924 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP, |
| 925 | GLOBAL2_SMI_OP_22_WRITE | (addr << 5) | |
| 926 | regnum); |
| 927 | |
| 928 | return _mv88e6xxx_phy_wait(ds); |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 929 | } |
| 930 | |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 931 | int mv88e6xxx_get_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e) |
| 932 | { |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 933 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 934 | int reg; |
| 935 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 936 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 937 | |
| 938 | reg = _mv88e6xxx_phy_read_indirect(ds, port, 16); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 939 | if (reg < 0) |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 940 | goto out; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 941 | |
| 942 | e->eee_enabled = !!(reg & 0x0200); |
| 943 | e->tx_lpi_enabled = !!(reg & 0x0100); |
| 944 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 945 | reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_STATUS); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 946 | if (reg < 0) |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 947 | goto out; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 948 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 949 | e->eee_active = !!(reg & PORT_STATUS_EEE); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 950 | reg = 0; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 951 | |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 952 | out: |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 953 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 954 | return reg; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | int mv88e6xxx_set_eee(struct dsa_switch *ds, int port, |
| 958 | struct phy_device *phydev, struct ethtool_eee *e) |
| 959 | { |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 960 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 961 | int reg; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 962 | int ret; |
| 963 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 964 | mutex_lock(&ps->smi_mutex); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 965 | |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 966 | ret = _mv88e6xxx_phy_read_indirect(ds, port, 16); |
| 967 | if (ret < 0) |
| 968 | goto out; |
| 969 | |
| 970 | reg = ret & ~0x0300; |
| 971 | if (e->eee_enabled) |
| 972 | reg |= 0x0200; |
| 973 | if (e->tx_lpi_enabled) |
| 974 | reg |= 0x0100; |
| 975 | |
| 976 | ret = _mv88e6xxx_phy_write_indirect(ds, port, 16, reg); |
| 977 | out: |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 978 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 979 | |
| 980 | return ret; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 981 | } |
| 982 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 983 | static int _mv88e6xxx_atu_cmd(struct dsa_switch *ds, int fid, u16 cmd) |
| 984 | { |
| 985 | int ret; |
| 986 | |
| 987 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, 0x01, fid); |
| 988 | if (ret < 0) |
| 989 | return ret; |
| 990 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 991 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_OP, cmd); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 992 | if (ret < 0) |
| 993 | return ret; |
| 994 | |
| 995 | return _mv88e6xxx_atu_wait(ds); |
| 996 | } |
| 997 | |
| 998 | static int _mv88e6xxx_flush_fid(struct dsa_switch *ds, int fid) |
| 999 | { |
| 1000 | int ret; |
| 1001 | |
| 1002 | ret = _mv88e6xxx_atu_wait(ds); |
| 1003 | if (ret < 0) |
| 1004 | return ret; |
| 1005 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1006 | return _mv88e6xxx_atu_cmd(ds, fid, GLOBAL_ATU_OP_FLUSH_NON_STATIC_DB); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | static int mv88e6xxx_set_port_state(struct dsa_switch *ds, int port, u8 state) |
| 1010 | { |
| 1011 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Geert Uytterhoeven | c3ffe6d | 2015-04-16 20:49:14 +0200 | [diff] [blame] | 1012 | int reg, ret = 0; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1013 | u8 oldstate; |
| 1014 | |
| 1015 | mutex_lock(&ps->smi_mutex); |
| 1016 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1017 | reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL); |
Guenter Roeck | 538cc28 | 2015-04-15 22:12:42 -0700 | [diff] [blame] | 1018 | if (reg < 0) { |
| 1019 | ret = reg; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1020 | goto abort; |
Guenter Roeck | 538cc28 | 2015-04-15 22:12:42 -0700 | [diff] [blame] | 1021 | } |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1022 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1023 | oldstate = reg & PORT_CONTROL_STATE_MASK; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1024 | if (oldstate != state) { |
| 1025 | /* Flush forwarding database if we're moving a port |
| 1026 | * from Learning or Forwarding state to Disabled or |
| 1027 | * Blocking or Listening state. |
| 1028 | */ |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1029 | if (oldstate >= PORT_CONTROL_STATE_LEARNING && |
| 1030 | state <= PORT_CONTROL_STATE_BLOCKING) { |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1031 | ret = _mv88e6xxx_flush_fid(ds, ps->fid[port]); |
| 1032 | if (ret) |
| 1033 | goto abort; |
| 1034 | } |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1035 | reg = (reg & ~PORT_CONTROL_STATE_MASK) | state; |
| 1036 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL, |
| 1037 | reg); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | abort: |
| 1041 | mutex_unlock(&ps->smi_mutex); |
| 1042 | return ret; |
| 1043 | } |
| 1044 | |
| 1045 | /* Must be called with smi lock held */ |
| 1046 | static int _mv88e6xxx_update_port_config(struct dsa_switch *ds, int port) |
| 1047 | { |
| 1048 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1049 | u8 fid = ps->fid[port]; |
| 1050 | u16 reg = fid << 12; |
| 1051 | |
| 1052 | if (dsa_is_cpu_port(ds, port)) |
| 1053 | reg |= ds->phys_port_mask; |
| 1054 | else |
| 1055 | reg |= (ps->bridge_mask[fid] | |
| 1056 | (1 << dsa_upstream_port(ds))) & ~(1 << port); |
| 1057 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1058 | return _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_BASE_VLAN, reg); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | /* Must be called with smi lock held */ |
| 1062 | static int _mv88e6xxx_update_bridge_config(struct dsa_switch *ds, int fid) |
| 1063 | { |
| 1064 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1065 | int port; |
| 1066 | u32 mask; |
| 1067 | int ret; |
| 1068 | |
| 1069 | mask = ds->phys_port_mask; |
| 1070 | while (mask) { |
| 1071 | port = __ffs(mask); |
| 1072 | mask &= ~(1 << port); |
| 1073 | if (ps->fid[port] != fid) |
| 1074 | continue; |
| 1075 | |
| 1076 | ret = _mv88e6xxx_update_port_config(ds, port); |
| 1077 | if (ret) |
| 1078 | return ret; |
| 1079 | } |
| 1080 | |
| 1081 | return _mv88e6xxx_flush_fid(ds, fid); |
| 1082 | } |
| 1083 | |
| 1084 | /* Bridge handling functions */ |
| 1085 | |
| 1086 | int mv88e6xxx_join_bridge(struct dsa_switch *ds, int port, u32 br_port_mask) |
| 1087 | { |
| 1088 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1089 | int ret = 0; |
| 1090 | u32 nmask; |
| 1091 | int fid; |
| 1092 | |
| 1093 | /* If the bridge group is not empty, join that group. |
| 1094 | * Otherwise create a new group. |
| 1095 | */ |
| 1096 | fid = ps->fid[port]; |
| 1097 | nmask = br_port_mask & ~(1 << port); |
| 1098 | if (nmask) |
| 1099 | fid = ps->fid[__ffs(nmask)]; |
| 1100 | |
| 1101 | nmask = ps->bridge_mask[fid] | (1 << port); |
| 1102 | if (nmask != br_port_mask) { |
| 1103 | netdev_err(ds->ports[port], |
| 1104 | "join: Bridge port mask mismatch fid=%d mask=0x%x expected 0x%x\n", |
| 1105 | fid, br_port_mask, nmask); |
| 1106 | return -EINVAL; |
| 1107 | } |
| 1108 | |
| 1109 | mutex_lock(&ps->smi_mutex); |
| 1110 | |
| 1111 | ps->bridge_mask[fid] = br_port_mask; |
| 1112 | |
| 1113 | if (fid != ps->fid[port]) { |
| 1114 | ps->fid_mask |= 1 << ps->fid[port]; |
| 1115 | ps->fid[port] = fid; |
| 1116 | ret = _mv88e6xxx_update_bridge_config(ds, fid); |
| 1117 | } |
| 1118 | |
| 1119 | mutex_unlock(&ps->smi_mutex); |
| 1120 | |
| 1121 | return ret; |
| 1122 | } |
| 1123 | |
| 1124 | int mv88e6xxx_leave_bridge(struct dsa_switch *ds, int port, u32 br_port_mask) |
| 1125 | { |
| 1126 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1127 | u8 fid, newfid; |
| 1128 | int ret; |
| 1129 | |
| 1130 | fid = ps->fid[port]; |
| 1131 | |
| 1132 | if (ps->bridge_mask[fid] != br_port_mask) { |
| 1133 | netdev_err(ds->ports[port], |
| 1134 | "leave: Bridge port mask mismatch fid=%d mask=0x%x expected 0x%x\n", |
| 1135 | fid, br_port_mask, ps->bridge_mask[fid]); |
| 1136 | return -EINVAL; |
| 1137 | } |
| 1138 | |
| 1139 | /* If the port was the last port of a bridge, we are done. |
| 1140 | * Otherwise assign a new fid to the port, and fix up |
| 1141 | * the bridge configuration. |
| 1142 | */ |
| 1143 | if (br_port_mask == (1 << port)) |
| 1144 | return 0; |
| 1145 | |
| 1146 | mutex_lock(&ps->smi_mutex); |
| 1147 | |
| 1148 | newfid = __ffs(ps->fid_mask); |
| 1149 | ps->fid[port] = newfid; |
| 1150 | ps->fid_mask &= (1 << newfid); |
| 1151 | ps->bridge_mask[fid] &= ~(1 << port); |
| 1152 | ps->bridge_mask[newfid] = 1 << port; |
| 1153 | |
| 1154 | ret = _mv88e6xxx_update_bridge_config(ds, fid); |
| 1155 | if (!ret) |
| 1156 | ret = _mv88e6xxx_update_bridge_config(ds, newfid); |
| 1157 | |
| 1158 | mutex_unlock(&ps->smi_mutex); |
| 1159 | |
| 1160 | return ret; |
| 1161 | } |
| 1162 | |
| 1163 | int mv88e6xxx_port_stp_update(struct dsa_switch *ds, int port, u8 state) |
| 1164 | { |
| 1165 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1166 | int stp_state; |
| 1167 | |
| 1168 | switch (state) { |
| 1169 | case BR_STATE_DISABLED: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1170 | stp_state = PORT_CONTROL_STATE_DISABLED; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1171 | break; |
| 1172 | case BR_STATE_BLOCKING: |
| 1173 | case BR_STATE_LISTENING: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1174 | stp_state = PORT_CONTROL_STATE_BLOCKING; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1175 | break; |
| 1176 | case BR_STATE_LEARNING: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1177 | stp_state = PORT_CONTROL_STATE_LEARNING; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1178 | break; |
| 1179 | case BR_STATE_FORWARDING: |
| 1180 | default: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1181 | stp_state = PORT_CONTROL_STATE_FORWARDING; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1182 | break; |
| 1183 | } |
| 1184 | |
| 1185 | netdev_dbg(ds->ports[port], "port state %d [%d]\n", state, stp_state); |
| 1186 | |
| 1187 | /* mv88e6xxx_port_stp_update may be called with softirqs disabled, |
| 1188 | * so we can not update the port state directly but need to schedule it. |
| 1189 | */ |
| 1190 | ps->port_state[port] = stp_state; |
| 1191 | set_bit(port, &ps->port_state_update_mask); |
| 1192 | schedule_work(&ps->bridge_work); |
| 1193 | |
| 1194 | return 0; |
| 1195 | } |
| 1196 | |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1197 | static int __mv88e6xxx_write_addr(struct dsa_switch *ds, |
| 1198 | const unsigned char *addr) |
| 1199 | { |
| 1200 | int i, ret; |
| 1201 | |
| 1202 | for (i = 0; i < 3; i++) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1203 | ret = _mv88e6xxx_reg_write( |
| 1204 | ds, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i, |
| 1205 | (addr[i * 2] << 8) | addr[i * 2 + 1]); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1206 | if (ret < 0) |
| 1207 | return ret; |
| 1208 | } |
| 1209 | |
| 1210 | return 0; |
| 1211 | } |
| 1212 | |
| 1213 | static int __mv88e6xxx_read_addr(struct dsa_switch *ds, unsigned char *addr) |
| 1214 | { |
| 1215 | int i, ret; |
| 1216 | |
| 1217 | for (i = 0; i < 3; i++) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1218 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, |
| 1219 | GLOBAL_ATU_MAC_01 + i); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1220 | if (ret < 0) |
| 1221 | return ret; |
| 1222 | addr[i * 2] = ret >> 8; |
| 1223 | addr[i * 2 + 1] = ret & 0xff; |
| 1224 | } |
| 1225 | |
| 1226 | return 0; |
| 1227 | } |
| 1228 | |
| 1229 | static int __mv88e6xxx_port_fdb_cmd(struct dsa_switch *ds, int port, |
| 1230 | const unsigned char *addr, int state) |
| 1231 | { |
| 1232 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1233 | u8 fid = ps->fid[port]; |
| 1234 | int ret; |
| 1235 | |
| 1236 | ret = _mv88e6xxx_atu_wait(ds); |
| 1237 | if (ret < 0) |
| 1238 | return ret; |
| 1239 | |
| 1240 | ret = __mv88e6xxx_write_addr(ds, addr); |
| 1241 | if (ret < 0) |
| 1242 | return ret; |
| 1243 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1244 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_DATA, |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1245 | (0x10 << port) | state); |
| 1246 | if (ret) |
| 1247 | return ret; |
| 1248 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1249 | ret = _mv88e6xxx_atu_cmd(ds, fid, GLOBAL_ATU_OP_LOAD_DB); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1250 | |
| 1251 | return ret; |
| 1252 | } |
| 1253 | |
| 1254 | int mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port, |
| 1255 | const unsigned char *addr, u16 vid) |
| 1256 | { |
| 1257 | int state = is_multicast_ether_addr(addr) ? |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1258 | GLOBAL_ATU_DATA_STATE_MC_STATIC : |
| 1259 | GLOBAL_ATU_DATA_STATE_UC_STATIC; |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1260 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1261 | int ret; |
| 1262 | |
| 1263 | mutex_lock(&ps->smi_mutex); |
| 1264 | ret = __mv88e6xxx_port_fdb_cmd(ds, port, addr, state); |
| 1265 | mutex_unlock(&ps->smi_mutex); |
| 1266 | |
| 1267 | return ret; |
| 1268 | } |
| 1269 | |
| 1270 | int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port, |
| 1271 | const unsigned char *addr, u16 vid) |
| 1272 | { |
| 1273 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1274 | int ret; |
| 1275 | |
| 1276 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1277 | ret = __mv88e6xxx_port_fdb_cmd(ds, port, addr, |
| 1278 | GLOBAL_ATU_DATA_STATE_UNUSED); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1279 | mutex_unlock(&ps->smi_mutex); |
| 1280 | |
| 1281 | return ret; |
| 1282 | } |
| 1283 | |
| 1284 | static int __mv88e6xxx_port_getnext(struct dsa_switch *ds, int port, |
| 1285 | unsigned char *addr, bool *is_static) |
| 1286 | { |
| 1287 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1288 | u8 fid = ps->fid[port]; |
| 1289 | int ret, state; |
| 1290 | |
| 1291 | ret = _mv88e6xxx_atu_wait(ds); |
| 1292 | if (ret < 0) |
| 1293 | return ret; |
| 1294 | |
| 1295 | ret = __mv88e6xxx_write_addr(ds, addr); |
| 1296 | if (ret < 0) |
| 1297 | return ret; |
| 1298 | |
| 1299 | do { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1300 | ret = _mv88e6xxx_atu_cmd(ds, fid, GLOBAL_ATU_OP_GET_NEXT_DB); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1301 | if (ret < 0) |
| 1302 | return ret; |
| 1303 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1304 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_DATA); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1305 | if (ret < 0) |
| 1306 | return ret; |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1307 | state = ret & GLOBAL_ATU_DATA_STATE_MASK; |
| 1308 | if (state == GLOBAL_ATU_DATA_STATE_UNUSED) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1309 | return -ENOENT; |
| 1310 | } while (!(((ret >> 4) & 0xff) & (1 << port))); |
| 1311 | |
| 1312 | ret = __mv88e6xxx_read_addr(ds, addr); |
| 1313 | if (ret < 0) |
| 1314 | return ret; |
| 1315 | |
| 1316 | *is_static = state == (is_multicast_ether_addr(addr) ? |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1317 | GLOBAL_ATU_DATA_STATE_MC_STATIC : |
| 1318 | GLOBAL_ATU_DATA_STATE_UC_STATIC); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1319 | |
| 1320 | return 0; |
| 1321 | } |
| 1322 | |
| 1323 | /* get next entry for port */ |
| 1324 | int mv88e6xxx_port_fdb_getnext(struct dsa_switch *ds, int port, |
| 1325 | unsigned char *addr, bool *is_static) |
| 1326 | { |
| 1327 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1328 | int ret; |
| 1329 | |
| 1330 | mutex_lock(&ps->smi_mutex); |
| 1331 | ret = __mv88e6xxx_port_getnext(ds, port, addr, is_static); |
| 1332 | mutex_unlock(&ps->smi_mutex); |
| 1333 | |
| 1334 | return ret; |
| 1335 | } |
| 1336 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1337 | static void mv88e6xxx_bridge_work(struct work_struct *work) |
| 1338 | { |
| 1339 | struct mv88e6xxx_priv_state *ps; |
| 1340 | struct dsa_switch *ds; |
| 1341 | int port; |
| 1342 | |
| 1343 | ps = container_of(work, struct mv88e6xxx_priv_state, bridge_work); |
| 1344 | ds = ((struct dsa_switch *)ps) - 1; |
| 1345 | |
| 1346 | while (ps->port_state_update_mask) { |
| 1347 | port = __ffs(ps->port_state_update_mask); |
| 1348 | clear_bit(port, &ps->port_state_update_mask); |
| 1349 | mv88e6xxx_set_port_state(ds, port, ps->port_state[port]); |
| 1350 | } |
| 1351 | } |
| 1352 | |
Andrew Lunn | dbde9e6 | 2015-05-06 01:09:48 +0200 | [diff] [blame] | 1353 | static int mv88e6xxx_setup_port(struct dsa_switch *ds, int port) |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 1354 | { |
| 1355 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1356 | int ret, fid; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1357 | u16 reg; |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 1358 | |
| 1359 | mutex_lock(&ps->smi_mutex); |
| 1360 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1361 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 1362 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 1363 | mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) || |
| 1364 | mv88e6xxx_6065_family(ds)) { |
| 1365 | /* MAC Forcing register: don't force link, speed, |
| 1366 | * duplex or flow control state to any particular |
| 1367 | * values on physical ports, but force the CPU port |
| 1368 | * and all DSA ports to their maximum bandwidth and |
| 1369 | * full duplex. |
| 1370 | */ |
| 1371 | reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_PCS_CTRL); |
| 1372 | if (dsa_is_cpu_port(ds, port) || |
| 1373 | ds->dsa_port_mask & (1 << port)) { |
| 1374 | reg |= PORT_PCS_CTRL_FORCE_LINK | |
| 1375 | PORT_PCS_CTRL_LINK_UP | |
| 1376 | PORT_PCS_CTRL_DUPLEX_FULL | |
| 1377 | PORT_PCS_CTRL_FORCE_DUPLEX; |
| 1378 | if (mv88e6xxx_6065_family(ds)) |
| 1379 | reg |= PORT_PCS_CTRL_100; |
| 1380 | else |
| 1381 | reg |= PORT_PCS_CTRL_1000; |
| 1382 | } else { |
| 1383 | reg |= PORT_PCS_CTRL_UNFORCED; |
| 1384 | } |
| 1385 | |
| 1386 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1387 | PORT_PCS_CTRL, reg); |
| 1388 | if (ret) |
| 1389 | goto abort; |
| 1390 | } |
| 1391 | |
| 1392 | /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock, |
| 1393 | * disable Header mode, enable IGMP/MLD snooping, disable VLAN |
| 1394 | * tunneling, determine priority by looking at 802.1p and IP |
| 1395 | * priority fields (IP prio has precedence), and set STP state |
| 1396 | * to Forwarding. |
| 1397 | * |
| 1398 | * If this is the CPU link, use DSA or EDSA tagging depending |
| 1399 | * on which tagging mode was configured. |
| 1400 | * |
| 1401 | * If this is a link to another switch, use DSA tagging mode. |
| 1402 | * |
| 1403 | * If this is the upstream port for this switch, enable |
| 1404 | * forwarding of unknown unicasts and multicasts. |
| 1405 | */ |
| 1406 | reg = 0; |
| 1407 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 1408 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 1409 | mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) || |
| 1410 | mv88e6xxx_6185_family(ds)) |
| 1411 | reg = PORT_CONTROL_IGMP_MLD_SNOOP | |
| 1412 | PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP | |
| 1413 | PORT_CONTROL_STATE_FORWARDING; |
| 1414 | if (dsa_is_cpu_port(ds, port)) { |
| 1415 | if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds)) |
| 1416 | reg |= PORT_CONTROL_DSA_TAG; |
| 1417 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 1418 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds)) { |
| 1419 | if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA) |
| 1420 | reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA; |
| 1421 | else |
| 1422 | reg |= PORT_CONTROL_FRAME_MODE_DSA; |
| 1423 | } |
| 1424 | |
| 1425 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 1426 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 1427 | mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) || |
| 1428 | mv88e6xxx_6185_family(ds)) { |
| 1429 | if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA) |
| 1430 | reg |= PORT_CONTROL_EGRESS_ADD_TAG; |
| 1431 | } |
| 1432 | } |
| 1433 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 1434 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 1435 | mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds)) { |
| 1436 | if (ds->dsa_port_mask & (1 << port)) |
| 1437 | reg |= PORT_CONTROL_FRAME_MODE_DSA; |
| 1438 | if (port == dsa_upstream_port(ds)) |
| 1439 | reg |= PORT_CONTROL_FORWARD_UNKNOWN | |
| 1440 | PORT_CONTROL_FORWARD_UNKNOWN_MC; |
| 1441 | } |
| 1442 | if (reg) { |
| 1443 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1444 | PORT_CONTROL, reg); |
| 1445 | if (ret) |
| 1446 | goto abort; |
| 1447 | } |
| 1448 | |
| 1449 | /* Port Control 2: don't force a good FCS, set the maximum |
| 1450 | * frame size to 10240 bytes, don't let the switch add or |
| 1451 | * strip 802.1q tags, don't discard tagged or untagged frames |
| 1452 | * on this port, do a destination address lookup on all |
| 1453 | * received packets as usual, disable ARP mirroring and don't |
| 1454 | * send a copy of all transmitted/received frames on this port |
| 1455 | * to the CPU. |
| 1456 | */ |
| 1457 | reg = 0; |
| 1458 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 1459 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 1460 | mv88e6xxx_6095_family(ds)) |
| 1461 | reg = PORT_CONTROL_2_MAP_DA; |
| 1462 | |
| 1463 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 1464 | mv88e6xxx_6165_family(ds)) |
| 1465 | reg |= PORT_CONTROL_2_JUMBO_10240; |
| 1466 | |
| 1467 | if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds)) { |
| 1468 | /* Set the upstream port this port should use */ |
| 1469 | reg |= dsa_upstream_port(ds); |
| 1470 | /* enable forwarding of unknown multicast addresses to |
| 1471 | * the upstream port |
| 1472 | */ |
| 1473 | if (port == dsa_upstream_port(ds)) |
| 1474 | reg |= PORT_CONTROL_2_FORWARD_UNKNOWN; |
| 1475 | } |
| 1476 | |
| 1477 | if (reg) { |
| 1478 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1479 | PORT_CONTROL_2, reg); |
| 1480 | if (ret) |
| 1481 | goto abort; |
| 1482 | } |
| 1483 | |
| 1484 | /* Port Association Vector: when learning source addresses |
| 1485 | * of packets, add the address to the address database using |
| 1486 | * a port bitmap that has only the bit for this port set and |
| 1487 | * the other bits clear. |
| 1488 | */ |
| 1489 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_ASSOC_VECTOR, |
| 1490 | 1 << port); |
| 1491 | if (ret) |
| 1492 | goto abort; |
| 1493 | |
| 1494 | /* Egress rate control 2: disable egress rate control. */ |
| 1495 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_RATE_CONTROL_2, |
| 1496 | 0x0000); |
| 1497 | if (ret) |
| 1498 | goto abort; |
| 1499 | |
| 1500 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 1501 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds)) { |
| 1502 | /* Do not limit the period of time that this port can |
| 1503 | * be paused for by the remote end or the period of |
| 1504 | * time that this port can pause the remote end. |
| 1505 | */ |
| 1506 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1507 | PORT_PAUSE_CTRL, 0x0000); |
| 1508 | if (ret) |
| 1509 | goto abort; |
| 1510 | |
| 1511 | /* Port ATU control: disable limiting the number of |
| 1512 | * address database entries that this port is allowed |
| 1513 | * to use. |
| 1514 | */ |
| 1515 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1516 | PORT_ATU_CONTROL, 0x0000); |
| 1517 | /* Priority Override: disable DA, SA and VTU priority |
| 1518 | * override. |
| 1519 | */ |
| 1520 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1521 | PORT_PRI_OVERRIDE, 0x0000); |
| 1522 | if (ret) |
| 1523 | goto abort; |
| 1524 | |
| 1525 | /* Port Ethertype: use the Ethertype DSA Ethertype |
| 1526 | * value. |
| 1527 | */ |
| 1528 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1529 | PORT_ETH_TYPE, ETH_P_EDSA); |
| 1530 | if (ret) |
| 1531 | goto abort; |
| 1532 | /* Tag Remap: use an identity 802.1p prio -> switch |
| 1533 | * prio mapping. |
| 1534 | */ |
| 1535 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1536 | PORT_TAG_REGMAP_0123, 0x3210); |
| 1537 | if (ret) |
| 1538 | goto abort; |
| 1539 | |
| 1540 | /* Tag Remap 2: use an identity 802.1p prio -> switch |
| 1541 | * prio mapping. |
| 1542 | */ |
| 1543 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1544 | PORT_TAG_REGMAP_4567, 0x7654); |
| 1545 | if (ret) |
| 1546 | goto abort; |
| 1547 | } |
| 1548 | |
| 1549 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 1550 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 1551 | mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds)) { |
| 1552 | /* Rate Control: disable ingress rate limiting. */ |
| 1553 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1554 | PORT_RATE_CONTROL, 0x0001); |
| 1555 | if (ret) |
| 1556 | goto abort; |
| 1557 | } |
| 1558 | |
Guenter Roeck | 366f0a0 | 2015-03-26 18:36:30 -0700 | [diff] [blame] | 1559 | /* Port Control 1: disable trunking, disable sending |
| 1560 | * learning messages to this port. |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 1561 | */ |
Vivien Didelot | 614f03f | 2015-04-20 17:19:23 -0400 | [diff] [blame] | 1562 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_1, 0x0000); |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 1563 | if (ret) |
| 1564 | goto abort; |
| 1565 | |
| 1566 | /* Port based VLAN map: give each port its own address |
| 1567 | * database, allow the CPU port to talk to each of the 'real' |
| 1568 | * ports, and allow each of the 'real' ports to only talk to |
| 1569 | * the upstream port. |
| 1570 | */ |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1571 | fid = __ffs(ps->fid_mask); |
| 1572 | ps->fid[port] = fid; |
| 1573 | ps->fid_mask &= ~(1 << fid); |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 1574 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1575 | if (!dsa_is_cpu_port(ds, port)) |
| 1576 | ps->bridge_mask[fid] = 1 << port; |
| 1577 | |
| 1578 | ret = _mv88e6xxx_update_port_config(ds, port); |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 1579 | if (ret) |
| 1580 | goto abort; |
| 1581 | |
| 1582 | /* Default VLAN ID and priority: don't set a default VLAN |
| 1583 | * ID, and set the default packet priority to zero. |
| 1584 | */ |
Vivien Didelot | 47cf1e65 | 2015-04-20 17:43:26 -0400 | [diff] [blame] | 1585 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_DEFAULT_VLAN, |
| 1586 | 0x0000); |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 1587 | abort: |
| 1588 | mutex_unlock(&ps->smi_mutex); |
| 1589 | return ret; |
| 1590 | } |
| 1591 | |
Andrew Lunn | dbde9e6 | 2015-05-06 01:09:48 +0200 | [diff] [blame] | 1592 | int mv88e6xxx_setup_ports(struct dsa_switch *ds) |
| 1593 | { |
| 1594 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1595 | int ret; |
| 1596 | int i; |
| 1597 | |
| 1598 | for (i = 0; i < ps->num_ports; i++) { |
| 1599 | ret = mv88e6xxx_setup_port(ds, i); |
| 1600 | if (ret < 0) |
| 1601 | return ret; |
| 1602 | } |
| 1603 | return 0; |
| 1604 | } |
| 1605 | |
Andrew Lunn | 87c8cef | 2015-06-20 18:42:28 +0200 | [diff] [blame] | 1606 | static int mv88e6xxx_regs_show(struct seq_file *s, void *p) |
| 1607 | { |
| 1608 | struct dsa_switch *ds = s->private; |
| 1609 | |
| 1610 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1611 | int reg, port; |
| 1612 | |
| 1613 | seq_puts(s, " GLOBAL GLOBAL2 "); |
| 1614 | for (port = 0 ; port < ps->num_ports; port++) |
| 1615 | seq_printf(s, " %2d ", port); |
| 1616 | seq_puts(s, "\n"); |
| 1617 | |
| 1618 | for (reg = 0; reg < 32; reg++) { |
| 1619 | seq_printf(s, "%2x: ", reg); |
| 1620 | seq_printf(s, " %4x %4x ", |
| 1621 | mv88e6xxx_reg_read(ds, REG_GLOBAL, reg), |
| 1622 | mv88e6xxx_reg_read(ds, REG_GLOBAL2, reg)); |
| 1623 | |
| 1624 | for (port = 0 ; port < ps->num_ports; port++) |
| 1625 | seq_printf(s, "%4x ", |
| 1626 | mv88e6xxx_reg_read(ds, REG_PORT(port), reg)); |
| 1627 | seq_puts(s, "\n"); |
| 1628 | } |
| 1629 | |
| 1630 | return 0; |
| 1631 | } |
| 1632 | |
| 1633 | static int mv88e6xxx_regs_open(struct inode *inode, struct file *file) |
| 1634 | { |
| 1635 | return single_open(file, mv88e6xxx_regs_show, inode->i_private); |
| 1636 | } |
| 1637 | |
| 1638 | static const struct file_operations mv88e6xxx_regs_fops = { |
| 1639 | .open = mv88e6xxx_regs_open, |
| 1640 | .read = seq_read, |
| 1641 | .llseek = no_llseek, |
| 1642 | .release = single_release, |
| 1643 | .owner = THIS_MODULE, |
| 1644 | }; |
| 1645 | |
Andrew Lunn | 8a0a265 | 2015-06-20 18:42:29 +0200 | [diff] [blame^] | 1646 | static void mv88e6xxx_atu_show_header(struct seq_file *s) |
| 1647 | { |
| 1648 | seq_puts(s, "DB T/P Vec State Addr\n"); |
| 1649 | } |
| 1650 | |
| 1651 | static void mv88e6xxx_atu_show_entry(struct seq_file *s, int dbnum, |
| 1652 | unsigned char *addr, int data) |
| 1653 | { |
| 1654 | bool trunk = !!(data & GLOBAL_ATU_DATA_TRUNK); |
| 1655 | int portvec = ((data & GLOBAL_ATU_DATA_PORT_VECTOR_MASK) >> |
| 1656 | GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT); |
| 1657 | int state = data & GLOBAL_ATU_DATA_STATE_MASK; |
| 1658 | |
| 1659 | seq_printf(s, "%03x %5s %10pb %x %pM\n", |
| 1660 | dbnum, (trunk ? "Trunk" : "Port"), &portvec, state, addr); |
| 1661 | } |
| 1662 | |
| 1663 | static int mv88e6xxx_atu_show_db(struct seq_file *s, struct dsa_switch *ds, |
| 1664 | int dbnum) |
| 1665 | { |
| 1666 | unsigned char bcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
| 1667 | unsigned char addr[6]; |
| 1668 | int ret, data, state; |
| 1669 | |
| 1670 | ret = __mv88e6xxx_write_addr(ds, bcast); |
| 1671 | if (ret < 0) |
| 1672 | return ret; |
| 1673 | |
| 1674 | do { |
| 1675 | ret = _mv88e6xxx_atu_cmd(ds, dbnum, GLOBAL_ATU_OP_GET_NEXT_DB); |
| 1676 | if (ret < 0) |
| 1677 | return ret; |
| 1678 | data = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_DATA); |
| 1679 | if (data < 0) |
| 1680 | return data; |
| 1681 | |
| 1682 | state = data & GLOBAL_ATU_DATA_STATE_MASK; |
| 1683 | if (state == GLOBAL_ATU_DATA_STATE_UNUSED) |
| 1684 | break; |
| 1685 | ret = __mv88e6xxx_read_addr(ds, addr); |
| 1686 | if (ret < 0) |
| 1687 | return ret; |
| 1688 | mv88e6xxx_atu_show_entry(s, dbnum, addr, data); |
| 1689 | } while (state != GLOBAL_ATU_DATA_STATE_UNUSED); |
| 1690 | |
| 1691 | return 0; |
| 1692 | } |
| 1693 | |
| 1694 | static int mv88e6xxx_atu_show(struct seq_file *s, void *p) |
| 1695 | { |
| 1696 | struct dsa_switch *ds = s->private; |
| 1697 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1698 | int dbnum; |
| 1699 | |
| 1700 | mv88e6xxx_atu_show_header(s); |
| 1701 | |
| 1702 | for (dbnum = 0; dbnum < 255; dbnum++) { |
| 1703 | mutex_lock(&ps->smi_mutex); |
| 1704 | mv88e6xxx_atu_show_db(s, ds, dbnum); |
| 1705 | mutex_unlock(&ps->smi_mutex); |
| 1706 | } |
| 1707 | |
| 1708 | return 0; |
| 1709 | } |
| 1710 | |
| 1711 | static int mv88e6xxx_atu_open(struct inode *inode, struct file *file) |
| 1712 | { |
| 1713 | return single_open(file, mv88e6xxx_atu_show, inode->i_private); |
| 1714 | } |
| 1715 | |
| 1716 | static const struct file_operations mv88e6xxx_atu_fops = { |
| 1717 | .open = mv88e6xxx_atu_open, |
| 1718 | .read = seq_read, |
| 1719 | .llseek = no_llseek, |
| 1720 | .release = single_release, |
| 1721 | .owner = THIS_MODULE, |
| 1722 | }; |
| 1723 | |
Guenter Roeck | acdaffc | 2015-03-26 18:36:28 -0700 | [diff] [blame] | 1724 | int mv88e6xxx_setup_common(struct dsa_switch *ds) |
| 1725 | { |
| 1726 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Andrew Lunn | 87c8cef | 2015-06-20 18:42:28 +0200 | [diff] [blame] | 1727 | char *name; |
Guenter Roeck | acdaffc | 2015-03-26 18:36:28 -0700 | [diff] [blame] | 1728 | |
| 1729 | mutex_init(&ps->smi_mutex); |
Guenter Roeck | acdaffc | 2015-03-26 18:36:28 -0700 | [diff] [blame] | 1730 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1731 | ps->id = REG_READ(REG_PORT(0), PORT_SWITCH_ID) & 0xfff0; |
Andrew Lunn | a8f064c | 2015-03-26 18:36:40 -0700 | [diff] [blame] | 1732 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1733 | ps->fid_mask = (1 << DSA_MAX_PORTS) - 1; |
| 1734 | |
| 1735 | INIT_WORK(&ps->bridge_work, mv88e6xxx_bridge_work); |
| 1736 | |
Andrew Lunn | 87c8cef | 2015-06-20 18:42:28 +0200 | [diff] [blame] | 1737 | name = kasprintf(GFP_KERNEL, "dsa%d", ds->index); |
| 1738 | ps->dbgfs = debugfs_create_dir(name, NULL); |
| 1739 | kfree(name); |
| 1740 | |
| 1741 | debugfs_create_file("regs", S_IRUGO, ps->dbgfs, ds, |
| 1742 | &mv88e6xxx_regs_fops); |
| 1743 | |
Andrew Lunn | 8a0a265 | 2015-06-20 18:42:29 +0200 | [diff] [blame^] | 1744 | debugfs_create_file("atu", S_IRUGO, ps->dbgfs, ds, |
| 1745 | &mv88e6xxx_atu_fops); |
| 1746 | |
Guenter Roeck | acdaffc | 2015-03-26 18:36:28 -0700 | [diff] [blame] | 1747 | return 0; |
| 1748 | } |
| 1749 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1750 | int mv88e6xxx_setup_global(struct dsa_switch *ds) |
| 1751 | { |
| 1752 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1753 | int i; |
| 1754 | |
| 1755 | /* Set the default address aging time to 5 minutes, and |
| 1756 | * enable address learn messages to be sent to all message |
| 1757 | * ports. |
| 1758 | */ |
| 1759 | REG_WRITE(REG_GLOBAL, GLOBAL_ATU_CONTROL, |
| 1760 | 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL); |
| 1761 | |
| 1762 | /* Configure the IP ToS mapping registers. */ |
| 1763 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000); |
| 1764 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000); |
| 1765 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555); |
| 1766 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555); |
| 1767 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa); |
| 1768 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa); |
| 1769 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff); |
| 1770 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff); |
| 1771 | |
| 1772 | /* Configure the IEEE 802.1p priority mapping register. */ |
| 1773 | REG_WRITE(REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41); |
| 1774 | |
| 1775 | /* Send all frames with destination addresses matching |
| 1776 | * 01:80:c2:00:00:0x to the CPU port. |
| 1777 | */ |
| 1778 | REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff); |
| 1779 | |
| 1780 | /* Ignore removed tag data on doubly tagged packets, disable |
| 1781 | * flow control messages, force flow control priority to the |
| 1782 | * highest, and send all special multicast frames to the CPU |
| 1783 | * port at the highest priority. |
| 1784 | */ |
| 1785 | REG_WRITE(REG_GLOBAL2, GLOBAL2_SWITCH_MGMT, |
| 1786 | 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 | |
| 1787 | GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI); |
| 1788 | |
| 1789 | /* Program the DSA routing table. */ |
| 1790 | for (i = 0; i < 32; i++) { |
| 1791 | int nexthop = 0x1f; |
| 1792 | |
| 1793 | if (ds->pd->rtable && |
| 1794 | i != ds->index && i < ds->dst->pd->nr_chips) |
| 1795 | nexthop = ds->pd->rtable[i] & 0x1f; |
| 1796 | |
| 1797 | REG_WRITE(REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING, |
| 1798 | GLOBAL2_DEVICE_MAPPING_UPDATE | |
| 1799 | (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) | |
| 1800 | nexthop); |
| 1801 | } |
| 1802 | |
| 1803 | /* Clear all trunk masks. */ |
| 1804 | for (i = 0; i < 8; i++) |
| 1805 | REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MASK, |
| 1806 | 0x8000 | (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) | |
| 1807 | ((1 << ps->num_ports) - 1)); |
| 1808 | |
| 1809 | /* Clear all trunk mappings. */ |
| 1810 | for (i = 0; i < 16; i++) |
| 1811 | REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MAPPING, |
| 1812 | GLOBAL2_TRUNK_MAPPING_UPDATE | |
| 1813 | (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT)); |
| 1814 | |
| 1815 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 1816 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds)) { |
| 1817 | /* Send all frames with destination addresses matching |
| 1818 | * 01:80:c2:00:00:2x to the CPU port. |
| 1819 | */ |
| 1820 | REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_2X, 0xffff); |
| 1821 | |
| 1822 | /* Initialise cross-chip port VLAN table to reset |
| 1823 | * defaults. |
| 1824 | */ |
| 1825 | REG_WRITE(REG_GLOBAL2, GLOBAL2_PVT_ADDR, 0x9000); |
| 1826 | |
| 1827 | /* Clear the priority override table. */ |
| 1828 | for (i = 0; i < 16; i++) |
| 1829 | REG_WRITE(REG_GLOBAL2, GLOBAL2_PRIO_OVERRIDE, |
| 1830 | 0x8000 | (i << 8)); |
| 1831 | } |
| 1832 | |
| 1833 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 1834 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 1835 | mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds)) { |
| 1836 | /* Disable ingress rate limiting by resetting all |
| 1837 | * ingress rate limit registers to their initial |
| 1838 | * state. |
| 1839 | */ |
| 1840 | for (i = 0; i < ps->num_ports; i++) |
| 1841 | REG_WRITE(REG_GLOBAL2, GLOBAL2_INGRESS_OP, |
| 1842 | 0x9000 | (i << 8)); |
| 1843 | } |
| 1844 | |
| 1845 | return 0; |
| 1846 | } |
| 1847 | |
Andrew Lunn | 143a830 | 2015-04-02 04:06:34 +0200 | [diff] [blame] | 1848 | int mv88e6xxx_switch_reset(struct dsa_switch *ds, bool ppu_active) |
| 1849 | { |
| 1850 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1851 | u16 is_reset = (ppu_active ? 0x8800 : 0xc800); |
| 1852 | unsigned long timeout; |
| 1853 | int ret; |
| 1854 | int i; |
| 1855 | |
| 1856 | /* Set all ports to the disabled state. */ |
| 1857 | for (i = 0; i < ps->num_ports; i++) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1858 | ret = REG_READ(REG_PORT(i), PORT_CONTROL); |
| 1859 | REG_WRITE(REG_PORT(i), PORT_CONTROL, ret & 0xfffc); |
Andrew Lunn | 143a830 | 2015-04-02 04:06:34 +0200 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | /* Wait for transmit queues to drain. */ |
| 1863 | usleep_range(2000, 4000); |
| 1864 | |
| 1865 | /* Reset the switch. Keep the PPU active if requested. The PPU |
| 1866 | * needs to be active to support indirect phy register access |
| 1867 | * through global registers 0x18 and 0x19. |
| 1868 | */ |
| 1869 | if (ppu_active) |
| 1870 | REG_WRITE(REG_GLOBAL, 0x04, 0xc000); |
| 1871 | else |
| 1872 | REG_WRITE(REG_GLOBAL, 0x04, 0xc400); |
| 1873 | |
| 1874 | /* Wait up to one second for reset to complete. */ |
| 1875 | timeout = jiffies + 1 * HZ; |
| 1876 | while (time_before(jiffies, timeout)) { |
| 1877 | ret = REG_READ(REG_GLOBAL, 0x00); |
| 1878 | if ((ret & is_reset) == is_reset) |
| 1879 | break; |
| 1880 | usleep_range(1000, 2000); |
| 1881 | } |
| 1882 | if (time_after(jiffies, timeout)) |
| 1883 | return -ETIMEDOUT; |
| 1884 | |
| 1885 | return 0; |
| 1886 | } |
| 1887 | |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 1888 | int mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page, int reg) |
| 1889 | { |
| 1890 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1891 | int ret; |
| 1892 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1893 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 1894 | ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page); |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 1895 | if (ret < 0) |
| 1896 | goto error; |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 1897 | ret = _mv88e6xxx_phy_read_indirect(ds, port, reg); |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 1898 | error: |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 1899 | _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1900 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 1901 | return ret; |
| 1902 | } |
| 1903 | |
| 1904 | int mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page, |
| 1905 | int reg, int val) |
| 1906 | { |
| 1907 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1908 | int ret; |
| 1909 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1910 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 1911 | ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page); |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 1912 | if (ret < 0) |
| 1913 | goto error; |
| 1914 | |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 1915 | ret = _mv88e6xxx_phy_write_indirect(ds, port, reg, val); |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 1916 | error: |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 1917 | _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1918 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 1919 | return ret; |
| 1920 | } |
| 1921 | |
| 1922 | static int mv88e6xxx_port_to_phy_addr(struct dsa_switch *ds, int port) |
| 1923 | { |
| 1924 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1925 | |
| 1926 | if (port >= 0 && port < ps->num_ports) |
| 1927 | return port; |
| 1928 | return -EINVAL; |
| 1929 | } |
| 1930 | |
| 1931 | int |
| 1932 | mv88e6xxx_phy_read(struct dsa_switch *ds, int port, int regnum) |
| 1933 | { |
| 1934 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1935 | int addr = mv88e6xxx_port_to_phy_addr(ds, port); |
| 1936 | int ret; |
| 1937 | |
| 1938 | if (addr < 0) |
| 1939 | return addr; |
| 1940 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1941 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 1942 | ret = _mv88e6xxx_phy_read(ds, addr, regnum); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1943 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 1944 | return ret; |
| 1945 | } |
| 1946 | |
| 1947 | int |
| 1948 | mv88e6xxx_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val) |
| 1949 | { |
| 1950 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1951 | int addr = mv88e6xxx_port_to_phy_addr(ds, port); |
| 1952 | int ret; |
| 1953 | |
| 1954 | if (addr < 0) |
| 1955 | return addr; |
| 1956 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1957 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 1958 | ret = _mv88e6xxx_phy_write(ds, addr, regnum, val); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1959 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 1960 | return ret; |
| 1961 | } |
| 1962 | |
| 1963 | int |
| 1964 | mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int port, int regnum) |
| 1965 | { |
| 1966 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1967 | int addr = mv88e6xxx_port_to_phy_addr(ds, port); |
| 1968 | int ret; |
| 1969 | |
| 1970 | if (addr < 0) |
| 1971 | return addr; |
| 1972 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1973 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 1974 | ret = _mv88e6xxx_phy_read_indirect(ds, addr, regnum); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1975 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 1976 | return ret; |
| 1977 | } |
| 1978 | |
| 1979 | int |
| 1980 | mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int port, int regnum, |
| 1981 | u16 val) |
| 1982 | { |
| 1983 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1984 | int addr = mv88e6xxx_port_to_phy_addr(ds, port); |
| 1985 | int ret; |
| 1986 | |
| 1987 | if (addr < 0) |
| 1988 | return addr; |
| 1989 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1990 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 1991 | ret = _mv88e6xxx_phy_write_indirect(ds, addr, regnum, val); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1992 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 1993 | return ret; |
| 1994 | } |
| 1995 | |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 1996 | static int __init mv88e6xxx_init(void) |
| 1997 | { |
| 1998 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131) |
| 1999 | register_switch_driver(&mv88e6131_switch_driver); |
| 2000 | #endif |
| 2001 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65) |
| 2002 | register_switch_driver(&mv88e6123_61_65_switch_driver); |
| 2003 | #endif |
Guenter Roeck | 3ad50cc | 2014-10-29 10:44:56 -0700 | [diff] [blame] | 2004 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352) |
| 2005 | register_switch_driver(&mv88e6352_switch_driver); |
| 2006 | #endif |
Andrew Lunn | 42f2725 | 2014-09-12 23:58:44 +0200 | [diff] [blame] | 2007 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171) |
| 2008 | register_switch_driver(&mv88e6171_switch_driver); |
| 2009 | #endif |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 2010 | return 0; |
| 2011 | } |
| 2012 | module_init(mv88e6xxx_init); |
| 2013 | |
| 2014 | static void __exit mv88e6xxx_cleanup(void) |
| 2015 | { |
Andrew Lunn | 42f2725 | 2014-09-12 23:58:44 +0200 | [diff] [blame] | 2016 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171) |
| 2017 | unregister_switch_driver(&mv88e6171_switch_driver); |
| 2018 | #endif |
Vivien Didelot | 4212b54 | 2015-05-01 10:43:52 -0400 | [diff] [blame] | 2019 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352) |
| 2020 | unregister_switch_driver(&mv88e6352_switch_driver); |
| 2021 | #endif |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 2022 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65) |
| 2023 | unregister_switch_driver(&mv88e6123_61_65_switch_driver); |
| 2024 | #endif |
| 2025 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131) |
| 2026 | unregister_switch_driver(&mv88e6131_switch_driver); |
| 2027 | #endif |
| 2028 | } |
| 2029 | module_exit(mv88e6xxx_cleanup); |
Ben Hutchings | 3d825ed | 2011-11-25 14:37:16 +0000 | [diff] [blame] | 2030 | |
| 2031 | MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>"); |
| 2032 | MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips"); |
| 2033 | MODULE_LICENSE("GPL"); |