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 | |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 520 | static bool mv88e6xxx_6320_family(struct dsa_switch *ds) |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 521 | { |
| 522 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 523 | |
| 524 | switch (ps->id) { |
| 525 | case PORT_SWITCH_ID_6320: |
| 526 | case PORT_SWITCH_ID_6321: |
| 527 | return true; |
| 528 | } |
| 529 | return false; |
| 530 | } |
| 531 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 532 | static bool mv88e6xxx_6351_family(struct dsa_switch *ds) |
| 533 | { |
| 534 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 535 | |
| 536 | switch (ps->id) { |
| 537 | case PORT_SWITCH_ID_6171: |
| 538 | case PORT_SWITCH_ID_6175: |
| 539 | case PORT_SWITCH_ID_6350: |
| 540 | case PORT_SWITCH_ID_6351: |
| 541 | return true; |
| 542 | } |
| 543 | return false; |
| 544 | } |
| 545 | |
Andrew Lunn | f3a8b6b | 2015-04-02 04:06:40 +0200 | [diff] [blame] | 546 | static bool mv88e6xxx_6352_family(struct dsa_switch *ds) |
| 547 | { |
| 548 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 549 | |
| 550 | switch (ps->id) { |
Andrew Lunn | f3a8b6b | 2015-04-02 04:06:40 +0200 | [diff] [blame] | 551 | case PORT_SWITCH_ID_6172: |
| 552 | case PORT_SWITCH_ID_6176: |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 553 | case PORT_SWITCH_ID_6240: |
| 554 | case PORT_SWITCH_ID_6352: |
Andrew Lunn | f3a8b6b | 2015-04-02 04:06:40 +0200 | [diff] [blame] | 555 | return true; |
| 556 | } |
| 557 | return false; |
| 558 | } |
| 559 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 560 | /* Must be called with SMI mutex held */ |
| 561 | static int _mv88e6xxx_stats_wait(struct dsa_switch *ds) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 562 | { |
| 563 | int ret; |
| 564 | int i; |
| 565 | |
| 566 | for (i = 0; i < 10; i++) { |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 567 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_OP); |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 568 | if ((ret & GLOBAL_STATS_OP_BUSY) == 0) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | return -ETIMEDOUT; |
| 573 | } |
| 574 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 575 | /* Must be called with SMI mutex held */ |
| 576 | static int _mv88e6xxx_stats_snapshot(struct dsa_switch *ds, int port) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 577 | { |
| 578 | int ret; |
| 579 | |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 580 | if (mv88e6xxx_6320_family(ds) || mv88e6xxx_6352_family(ds)) |
Andrew Lunn | f3a8b6b | 2015-04-02 04:06:40 +0200 | [diff] [blame] | 581 | port = (port + 1) << 5; |
| 582 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 583 | /* Snapshot the hardware statistics counters for this port. */ |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 584 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP, |
| 585 | GLOBAL_STATS_OP_CAPTURE_PORT | |
| 586 | GLOBAL_STATS_OP_HIST_RX_TX | port); |
| 587 | if (ret < 0) |
| 588 | return ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 589 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 590 | /* Wait for the snapshotting to complete. */ |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 591 | ret = _mv88e6xxx_stats_wait(ds); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 592 | if (ret < 0) |
| 593 | return ret; |
| 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 598 | /* Must be called with SMI mutex held */ |
| 599 | 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] | 600 | { |
| 601 | u32 _val; |
| 602 | int ret; |
| 603 | |
| 604 | *val = 0; |
| 605 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 606 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP, |
| 607 | GLOBAL_STATS_OP_READ_CAPTURED | |
| 608 | GLOBAL_STATS_OP_HIST_RX_TX | stat); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 609 | if (ret < 0) |
| 610 | return; |
| 611 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 612 | ret = _mv88e6xxx_stats_wait(ds); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 613 | if (ret < 0) |
| 614 | return; |
| 615 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 616 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_32); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 617 | if (ret < 0) |
| 618 | return; |
| 619 | |
| 620 | _val = ret << 16; |
| 621 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 622 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_01); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 623 | if (ret < 0) |
| 624 | return; |
| 625 | |
| 626 | *val = _val | ret; |
| 627 | } |
| 628 | |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 629 | static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = { |
| 630 | { "in_good_octets", 8, 0x00, }, |
| 631 | { "in_bad_octets", 4, 0x02, }, |
| 632 | { "in_unicast", 4, 0x04, }, |
| 633 | { "in_broadcasts", 4, 0x06, }, |
| 634 | { "in_multicasts", 4, 0x07, }, |
| 635 | { "in_pause", 4, 0x16, }, |
| 636 | { "in_undersize", 4, 0x18, }, |
| 637 | { "in_fragments", 4, 0x19, }, |
| 638 | { "in_oversize", 4, 0x1a, }, |
| 639 | { "in_jabber", 4, 0x1b, }, |
| 640 | { "in_rx_error", 4, 0x1c, }, |
| 641 | { "in_fcs_error", 4, 0x1d, }, |
| 642 | { "out_octets", 8, 0x0e, }, |
| 643 | { "out_unicast", 4, 0x10, }, |
| 644 | { "out_broadcasts", 4, 0x13, }, |
| 645 | { "out_multicasts", 4, 0x12, }, |
| 646 | { "out_pause", 4, 0x15, }, |
| 647 | { "excessive", 4, 0x11, }, |
| 648 | { "collisions", 4, 0x1e, }, |
| 649 | { "deferred", 4, 0x05, }, |
| 650 | { "single", 4, 0x14, }, |
| 651 | { "multiple", 4, 0x17, }, |
| 652 | { "out_fcs_error", 4, 0x03, }, |
| 653 | { "late", 4, 0x1f, }, |
| 654 | { "hist_64bytes", 4, 0x08, }, |
| 655 | { "hist_65_127bytes", 4, 0x09, }, |
| 656 | { "hist_128_255bytes", 4, 0x0a, }, |
| 657 | { "hist_256_511bytes", 4, 0x0b, }, |
| 658 | { "hist_512_1023bytes", 4, 0x0c, }, |
| 659 | { "hist_1024_max_bytes", 4, 0x0d, }, |
| 660 | /* Not all devices have the following counters */ |
| 661 | { "sw_in_discards", 4, 0x110, }, |
| 662 | { "sw_in_filtered", 2, 0x112, }, |
| 663 | { "sw_out_filtered", 2, 0x113, }, |
| 664 | |
| 665 | }; |
| 666 | |
| 667 | static bool have_sw_in_discards(struct dsa_switch *ds) |
| 668 | { |
| 669 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 670 | |
| 671 | switch (ps->id) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 672 | case PORT_SWITCH_ID_6095: case PORT_SWITCH_ID_6161: |
| 673 | case PORT_SWITCH_ID_6165: case PORT_SWITCH_ID_6171: |
| 674 | case PORT_SWITCH_ID_6172: case PORT_SWITCH_ID_6176: |
| 675 | case PORT_SWITCH_ID_6182: case PORT_SWITCH_ID_6185: |
| 676 | case PORT_SWITCH_ID_6352: |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 677 | return true; |
| 678 | default: |
| 679 | return false; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | static void _mv88e6xxx_get_strings(struct dsa_switch *ds, |
| 684 | int nr_stats, |
| 685 | struct mv88e6xxx_hw_stat *stats, |
| 686 | int port, uint8_t *data) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 687 | { |
| 688 | int i; |
| 689 | |
| 690 | for (i = 0; i < nr_stats; i++) { |
| 691 | memcpy(data + i * ETH_GSTRING_LEN, |
| 692 | stats[i].string, ETH_GSTRING_LEN); |
| 693 | } |
| 694 | } |
| 695 | |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 696 | static uint64_t _mv88e6xxx_get_ethtool_stat(struct dsa_switch *ds, |
| 697 | int stat, |
| 698 | struct mv88e6xxx_hw_stat *stats, |
| 699 | int port) |
| 700 | { |
| 701 | struct mv88e6xxx_hw_stat *s = stats + stat; |
| 702 | u32 low; |
| 703 | u32 high = 0; |
| 704 | int ret; |
| 705 | u64 value; |
| 706 | |
| 707 | if (s->reg >= 0x100) { |
| 708 | ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), |
| 709 | s->reg - 0x100); |
| 710 | if (ret < 0) |
| 711 | return UINT64_MAX; |
| 712 | |
| 713 | low = ret; |
| 714 | if (s->sizeof_stat == 4) { |
| 715 | ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), |
| 716 | s->reg - 0x100 + 1); |
| 717 | if (ret < 0) |
| 718 | return UINT64_MAX; |
| 719 | high = ret; |
| 720 | } |
| 721 | } else { |
| 722 | _mv88e6xxx_stats_read(ds, s->reg, &low); |
| 723 | if (s->sizeof_stat == 8) |
| 724 | _mv88e6xxx_stats_read(ds, s->reg + 1, &high); |
| 725 | } |
| 726 | value = (((u64)high) << 16) | low; |
| 727 | return value; |
| 728 | } |
| 729 | |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 730 | static void _mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, |
| 731 | int nr_stats, |
| 732 | struct mv88e6xxx_hw_stat *stats, |
| 733 | int port, uint64_t *data) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 734 | { |
Florian Fainelli | a22adce | 2014-04-28 11:14:28 -0700 | [diff] [blame] | 735 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 736 | int ret; |
| 737 | int i; |
| 738 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 739 | mutex_lock(&ps->smi_mutex); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 740 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 741 | ret = _mv88e6xxx_stats_snapshot(ds, port); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 742 | if (ret < 0) { |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 743 | mutex_unlock(&ps->smi_mutex); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 744 | return; |
| 745 | } |
| 746 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 747 | /* Read each of the counters. */ |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 748 | for (i = 0; i < nr_stats; i++) |
| 749 | data[i] = _mv88e6xxx_get_ethtool_stat(ds, i, stats, port); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 750 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 751 | mutex_unlock(&ps->smi_mutex); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 752 | } |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 753 | |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 754 | /* All the statistics in the table */ |
| 755 | void |
| 756 | mv88e6xxx_get_strings(struct dsa_switch *ds, int port, uint8_t *data) |
| 757 | { |
| 758 | if (have_sw_in_discards(ds)) |
| 759 | _mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6xxx_hw_stats), |
| 760 | mv88e6xxx_hw_stats, port, data); |
| 761 | else |
| 762 | _mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6xxx_hw_stats) - 3, |
| 763 | mv88e6xxx_hw_stats, port, data); |
| 764 | } |
| 765 | |
| 766 | int mv88e6xxx_get_sset_count(struct dsa_switch *ds) |
| 767 | { |
| 768 | if (have_sw_in_discards(ds)) |
| 769 | return ARRAY_SIZE(mv88e6xxx_hw_stats); |
| 770 | return ARRAY_SIZE(mv88e6xxx_hw_stats) - 3; |
| 771 | } |
| 772 | |
| 773 | void |
| 774 | mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, |
| 775 | int port, uint64_t *data) |
| 776 | { |
| 777 | if (have_sw_in_discards(ds)) |
| 778 | _mv88e6xxx_get_ethtool_stats( |
| 779 | ds, ARRAY_SIZE(mv88e6xxx_hw_stats), |
| 780 | mv88e6xxx_hw_stats, port, data); |
| 781 | else |
| 782 | _mv88e6xxx_get_ethtool_stats( |
| 783 | ds, ARRAY_SIZE(mv88e6xxx_hw_stats) - 3, |
| 784 | mv88e6xxx_hw_stats, port, data); |
| 785 | } |
| 786 | |
Guenter Roeck | a1ab91f | 2014-10-29 10:45:05 -0700 | [diff] [blame] | 787 | int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port) |
| 788 | { |
| 789 | return 32 * sizeof(u16); |
| 790 | } |
| 791 | |
| 792 | void mv88e6xxx_get_regs(struct dsa_switch *ds, int port, |
| 793 | struct ethtool_regs *regs, void *_p) |
| 794 | { |
| 795 | u16 *p = _p; |
| 796 | int i; |
| 797 | |
| 798 | regs->version = 0; |
| 799 | |
| 800 | memset(p, 0xff, 32 * sizeof(u16)); |
| 801 | |
| 802 | for (i = 0; i < 32; i++) { |
| 803 | int ret; |
| 804 | |
| 805 | ret = mv88e6xxx_reg_read(ds, REG_PORT(port), i); |
| 806 | if (ret >= 0) |
| 807 | p[i] = ret; |
| 808 | } |
| 809 | } |
| 810 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 811 | /* Must be called with SMI lock held */ |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 812 | static int _mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset, |
| 813 | u16 mask) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 814 | { |
| 815 | unsigned long timeout = jiffies + HZ / 10; |
| 816 | |
| 817 | while (time_before(jiffies, timeout)) { |
| 818 | int ret; |
| 819 | |
| 820 | ret = _mv88e6xxx_reg_read(ds, reg, offset); |
| 821 | if (ret < 0) |
| 822 | return ret; |
| 823 | if (!(ret & mask)) |
| 824 | return 0; |
| 825 | |
| 826 | usleep_range(1000, 2000); |
| 827 | } |
| 828 | return -ETIMEDOUT; |
| 829 | } |
| 830 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 831 | static int mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset, u16 mask) |
| 832 | { |
| 833 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 834 | int ret; |
| 835 | |
| 836 | mutex_lock(&ps->smi_mutex); |
| 837 | ret = _mv88e6xxx_wait(ds, reg, offset, mask); |
| 838 | mutex_unlock(&ps->smi_mutex); |
| 839 | |
| 840 | return ret; |
| 841 | } |
| 842 | |
| 843 | static int _mv88e6xxx_phy_wait(struct dsa_switch *ds) |
| 844 | { |
| 845 | return _mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_SMI_OP, |
| 846 | GLOBAL2_SMI_OP_BUSY); |
| 847 | } |
| 848 | |
| 849 | int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds) |
| 850 | { |
| 851 | return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP, |
| 852 | GLOBAL2_EEPROM_OP_LOAD); |
| 853 | } |
| 854 | |
| 855 | int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds) |
| 856 | { |
| 857 | return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP, |
| 858 | GLOBAL2_EEPROM_OP_BUSY); |
| 859 | } |
| 860 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 861 | /* Must be called with SMI lock held */ |
| 862 | static int _mv88e6xxx_atu_wait(struct dsa_switch *ds) |
| 863 | { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 864 | return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_ATU_OP, |
| 865 | GLOBAL_ATU_OP_BUSY); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Andrew Lunn | 56d95e2 | 2015-06-20 18:42:33 +0200 | [diff] [blame] | 868 | /* Must be called with SMI lock held */ |
| 869 | static int _mv88e6xxx_scratch_wait(struct dsa_switch *ds) |
| 870 | { |
| 871 | return _mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_SCRATCH_MISC, |
| 872 | GLOBAL2_SCRATCH_BUSY); |
| 873 | } |
| 874 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 875 | /* Must be called with SMI mutex held */ |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 876 | static int _mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int addr, |
| 877 | int regnum) |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 878 | { |
| 879 | int ret; |
| 880 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 881 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP, |
| 882 | GLOBAL2_SMI_OP_22_READ | (addr << 5) | |
| 883 | regnum); |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 884 | if (ret < 0) |
| 885 | return ret; |
| 886 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 887 | ret = _mv88e6xxx_phy_wait(ds); |
| 888 | if (ret < 0) |
| 889 | return ret; |
| 890 | |
| 891 | return _mv88e6xxx_reg_read(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA); |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 892 | } |
| 893 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 894 | /* Must be called with SMI mutex held */ |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 895 | static int _mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int addr, |
| 896 | int regnum, u16 val) |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 897 | { |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 898 | int ret; |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 899 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 900 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA, val); |
| 901 | if (ret < 0) |
| 902 | return ret; |
| 903 | |
| 904 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP, |
| 905 | GLOBAL2_SMI_OP_22_WRITE | (addr << 5) | |
| 906 | regnum); |
| 907 | |
| 908 | return _mv88e6xxx_phy_wait(ds); |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 909 | } |
| 910 | |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 911 | int mv88e6xxx_get_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e) |
| 912 | { |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 913 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 914 | int reg; |
| 915 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 916 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 917 | |
| 918 | reg = _mv88e6xxx_phy_read_indirect(ds, port, 16); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 919 | if (reg < 0) |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 920 | goto out; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 921 | |
| 922 | e->eee_enabled = !!(reg & 0x0200); |
| 923 | e->tx_lpi_enabled = !!(reg & 0x0100); |
| 924 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 925 | reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_STATUS); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 926 | if (reg < 0) |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 927 | goto out; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 928 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 929 | e->eee_active = !!(reg & PORT_STATUS_EEE); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 930 | reg = 0; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 931 | |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 932 | out: |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 933 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 934 | return reg; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | int mv88e6xxx_set_eee(struct dsa_switch *ds, int port, |
| 938 | struct phy_device *phydev, struct ethtool_eee *e) |
| 939 | { |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 940 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 941 | int reg; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 942 | int ret; |
| 943 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 944 | mutex_lock(&ps->smi_mutex); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 945 | |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 946 | ret = _mv88e6xxx_phy_read_indirect(ds, port, 16); |
| 947 | if (ret < 0) |
| 948 | goto out; |
| 949 | |
| 950 | reg = ret & ~0x0300; |
| 951 | if (e->eee_enabled) |
| 952 | reg |= 0x0200; |
| 953 | if (e->tx_lpi_enabled) |
| 954 | reg |= 0x0100; |
| 955 | |
| 956 | ret = _mv88e6xxx_phy_write_indirect(ds, port, 16, reg); |
| 957 | out: |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 958 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 959 | |
| 960 | return ret; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 961 | } |
| 962 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 963 | static int _mv88e6xxx_atu_cmd(struct dsa_switch *ds, int fid, u16 cmd) |
| 964 | { |
| 965 | int ret; |
| 966 | |
Vivien Didelot | a08df0f | 2015-08-10 09:09:46 -0400 | [diff] [blame] | 967 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID, fid); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 968 | if (ret < 0) |
| 969 | return ret; |
| 970 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 971 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_OP, cmd); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 972 | if (ret < 0) |
| 973 | return ret; |
| 974 | |
| 975 | return _mv88e6xxx_atu_wait(ds); |
| 976 | } |
| 977 | |
| 978 | static int _mv88e6xxx_flush_fid(struct dsa_switch *ds, int fid) |
| 979 | { |
| 980 | int ret; |
| 981 | |
| 982 | ret = _mv88e6xxx_atu_wait(ds); |
| 983 | if (ret < 0) |
| 984 | return ret; |
| 985 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 986 | 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] | 987 | } |
| 988 | |
| 989 | static int mv88e6xxx_set_port_state(struct dsa_switch *ds, int port, u8 state) |
| 990 | { |
| 991 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Geert Uytterhoeven | c3ffe6d | 2015-04-16 20:49:14 +0200 | [diff] [blame] | 992 | int reg, ret = 0; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 993 | u8 oldstate; |
| 994 | |
| 995 | mutex_lock(&ps->smi_mutex); |
| 996 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 997 | reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL); |
Guenter Roeck | 538cc28 | 2015-04-15 22:12:42 -0700 | [diff] [blame] | 998 | if (reg < 0) { |
| 999 | ret = reg; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1000 | goto abort; |
Guenter Roeck | 538cc28 | 2015-04-15 22:12:42 -0700 | [diff] [blame] | 1001 | } |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1002 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1003 | oldstate = reg & PORT_CONTROL_STATE_MASK; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1004 | if (oldstate != state) { |
| 1005 | /* Flush forwarding database if we're moving a port |
| 1006 | * from Learning or Forwarding state to Disabled or |
| 1007 | * Blocking or Listening state. |
| 1008 | */ |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1009 | if (oldstate >= PORT_CONTROL_STATE_LEARNING && |
| 1010 | state <= PORT_CONTROL_STATE_BLOCKING) { |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1011 | ret = _mv88e6xxx_flush_fid(ds, ps->fid[port]); |
| 1012 | if (ret) |
| 1013 | goto abort; |
| 1014 | } |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1015 | reg = (reg & ~PORT_CONTROL_STATE_MASK) | state; |
| 1016 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL, |
| 1017 | reg); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | abort: |
| 1021 | mutex_unlock(&ps->smi_mutex); |
| 1022 | return ret; |
| 1023 | } |
| 1024 | |
| 1025 | /* Must be called with smi lock held */ |
| 1026 | static int _mv88e6xxx_update_port_config(struct dsa_switch *ds, int port) |
| 1027 | { |
| 1028 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1029 | u8 fid = ps->fid[port]; |
| 1030 | u16 reg = fid << 12; |
| 1031 | |
| 1032 | if (dsa_is_cpu_port(ds, port)) |
| 1033 | reg |= ds->phys_port_mask; |
| 1034 | else |
| 1035 | reg |= (ps->bridge_mask[fid] | |
| 1036 | (1 << dsa_upstream_port(ds))) & ~(1 << port); |
| 1037 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1038 | return _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_BASE_VLAN, reg); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | /* Must be called with smi lock held */ |
| 1042 | static int _mv88e6xxx_update_bridge_config(struct dsa_switch *ds, int fid) |
| 1043 | { |
| 1044 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1045 | int port; |
| 1046 | u32 mask; |
| 1047 | int ret; |
| 1048 | |
| 1049 | mask = ds->phys_port_mask; |
| 1050 | while (mask) { |
| 1051 | port = __ffs(mask); |
| 1052 | mask &= ~(1 << port); |
| 1053 | if (ps->fid[port] != fid) |
| 1054 | continue; |
| 1055 | |
| 1056 | ret = _mv88e6xxx_update_port_config(ds, port); |
| 1057 | if (ret) |
| 1058 | return ret; |
| 1059 | } |
| 1060 | |
| 1061 | return _mv88e6xxx_flush_fid(ds, fid); |
| 1062 | } |
| 1063 | |
| 1064 | /* Bridge handling functions */ |
| 1065 | |
| 1066 | int mv88e6xxx_join_bridge(struct dsa_switch *ds, int port, u32 br_port_mask) |
| 1067 | { |
| 1068 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1069 | int ret = 0; |
| 1070 | u32 nmask; |
| 1071 | int fid; |
| 1072 | |
| 1073 | /* If the bridge group is not empty, join that group. |
| 1074 | * Otherwise create a new group. |
| 1075 | */ |
| 1076 | fid = ps->fid[port]; |
| 1077 | nmask = br_port_mask & ~(1 << port); |
| 1078 | if (nmask) |
| 1079 | fid = ps->fid[__ffs(nmask)]; |
| 1080 | |
| 1081 | nmask = ps->bridge_mask[fid] | (1 << port); |
| 1082 | if (nmask != br_port_mask) { |
| 1083 | netdev_err(ds->ports[port], |
| 1084 | "join: Bridge port mask mismatch fid=%d mask=0x%x expected 0x%x\n", |
| 1085 | fid, br_port_mask, nmask); |
| 1086 | return -EINVAL; |
| 1087 | } |
| 1088 | |
| 1089 | mutex_lock(&ps->smi_mutex); |
| 1090 | |
| 1091 | ps->bridge_mask[fid] = br_port_mask; |
| 1092 | |
| 1093 | if (fid != ps->fid[port]) { |
Vivien Didelot | 194fea7 | 2015-08-10 09:09:47 -0400 | [diff] [blame] | 1094 | clear_bit(ps->fid[port], ps->fid_bitmap); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1095 | ps->fid[port] = fid; |
| 1096 | ret = _mv88e6xxx_update_bridge_config(ds, fid); |
| 1097 | } |
| 1098 | |
| 1099 | mutex_unlock(&ps->smi_mutex); |
| 1100 | |
| 1101 | return ret; |
| 1102 | } |
| 1103 | |
| 1104 | int mv88e6xxx_leave_bridge(struct dsa_switch *ds, int port, u32 br_port_mask) |
| 1105 | { |
| 1106 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1107 | u8 fid, newfid; |
| 1108 | int ret; |
| 1109 | |
| 1110 | fid = ps->fid[port]; |
| 1111 | |
| 1112 | if (ps->bridge_mask[fid] != br_port_mask) { |
| 1113 | netdev_err(ds->ports[port], |
| 1114 | "leave: Bridge port mask mismatch fid=%d mask=0x%x expected 0x%x\n", |
| 1115 | fid, br_port_mask, ps->bridge_mask[fid]); |
| 1116 | return -EINVAL; |
| 1117 | } |
| 1118 | |
| 1119 | /* If the port was the last port of a bridge, we are done. |
| 1120 | * Otherwise assign a new fid to the port, and fix up |
| 1121 | * the bridge configuration. |
| 1122 | */ |
| 1123 | if (br_port_mask == (1 << port)) |
| 1124 | return 0; |
| 1125 | |
| 1126 | mutex_lock(&ps->smi_mutex); |
| 1127 | |
Vivien Didelot | 194fea7 | 2015-08-10 09:09:47 -0400 | [diff] [blame] | 1128 | newfid = find_next_zero_bit(ps->fid_bitmap, VLAN_N_VID, 1); |
| 1129 | if (unlikely(newfid > ps->num_ports)) { |
| 1130 | netdev_err(ds->ports[port], "all first %d FIDs are used\n", |
| 1131 | ps->num_ports); |
| 1132 | ret = -ENOSPC; |
| 1133 | goto unlock; |
| 1134 | } |
| 1135 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1136 | ps->fid[port] = newfid; |
Vivien Didelot | 194fea7 | 2015-08-10 09:09:47 -0400 | [diff] [blame] | 1137 | set_bit(newfid, ps->fid_bitmap); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1138 | ps->bridge_mask[fid] &= ~(1 << port); |
| 1139 | ps->bridge_mask[newfid] = 1 << port; |
| 1140 | |
| 1141 | ret = _mv88e6xxx_update_bridge_config(ds, fid); |
| 1142 | if (!ret) |
| 1143 | ret = _mv88e6xxx_update_bridge_config(ds, newfid); |
| 1144 | |
Vivien Didelot | 194fea7 | 2015-08-10 09:09:47 -0400 | [diff] [blame] | 1145 | unlock: |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1146 | mutex_unlock(&ps->smi_mutex); |
| 1147 | |
| 1148 | return ret; |
| 1149 | } |
| 1150 | |
| 1151 | int mv88e6xxx_port_stp_update(struct dsa_switch *ds, int port, u8 state) |
| 1152 | { |
| 1153 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1154 | int stp_state; |
| 1155 | |
| 1156 | switch (state) { |
| 1157 | case BR_STATE_DISABLED: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1158 | stp_state = PORT_CONTROL_STATE_DISABLED; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1159 | break; |
| 1160 | case BR_STATE_BLOCKING: |
| 1161 | case BR_STATE_LISTENING: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1162 | stp_state = PORT_CONTROL_STATE_BLOCKING; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1163 | break; |
| 1164 | case BR_STATE_LEARNING: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1165 | stp_state = PORT_CONTROL_STATE_LEARNING; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1166 | break; |
| 1167 | case BR_STATE_FORWARDING: |
| 1168 | default: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1169 | stp_state = PORT_CONTROL_STATE_FORWARDING; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1170 | break; |
| 1171 | } |
| 1172 | |
| 1173 | netdev_dbg(ds->ports[port], "port state %d [%d]\n", state, stp_state); |
| 1174 | |
| 1175 | /* mv88e6xxx_port_stp_update may be called with softirqs disabled, |
| 1176 | * so we can not update the port state directly but need to schedule it. |
| 1177 | */ |
| 1178 | ps->port_state[port] = stp_state; |
| 1179 | set_bit(port, &ps->port_state_update_mask); |
| 1180 | schedule_work(&ps->bridge_work); |
| 1181 | |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
Vivien Didelot | c5723ac | 2015-08-10 09:09:48 -0400 | [diff] [blame] | 1185 | static int _mv88e6xxx_atu_mac_write(struct dsa_switch *ds, |
| 1186 | const unsigned char *addr) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1187 | { |
| 1188 | int i, ret; |
| 1189 | |
| 1190 | for (i = 0; i < 3; i++) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1191 | ret = _mv88e6xxx_reg_write( |
| 1192 | ds, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i, |
| 1193 | (addr[i * 2] << 8) | addr[i * 2 + 1]); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1194 | if (ret < 0) |
| 1195 | return ret; |
| 1196 | } |
| 1197 | |
| 1198 | return 0; |
| 1199 | } |
| 1200 | |
Vivien Didelot | c5723ac | 2015-08-10 09:09:48 -0400 | [diff] [blame] | 1201 | static int _mv88e6xxx_atu_mac_read(struct dsa_switch *ds, unsigned char *addr) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1202 | { |
| 1203 | int i, ret; |
| 1204 | |
| 1205 | for (i = 0; i < 3; i++) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1206 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, |
| 1207 | GLOBAL_ATU_MAC_01 + i); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1208 | if (ret < 0) |
| 1209 | return ret; |
| 1210 | addr[i * 2] = ret >> 8; |
| 1211 | addr[i * 2 + 1] = ret & 0xff; |
| 1212 | } |
| 1213 | |
| 1214 | return 0; |
| 1215 | } |
| 1216 | |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame^] | 1217 | static int _mv88e6xxx_atu_load(struct dsa_switch *ds, |
| 1218 | struct mv88e6xxx_atu_entry *entry) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1219 | { |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame^] | 1220 | u16 reg = 0; |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1221 | int ret; |
| 1222 | |
| 1223 | ret = _mv88e6xxx_atu_wait(ds); |
| 1224 | if (ret < 0) |
| 1225 | return ret; |
| 1226 | |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame^] | 1227 | ret = _mv88e6xxx_atu_mac_write(ds, entry->mac); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1228 | if (ret < 0) |
| 1229 | return ret; |
| 1230 | |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame^] | 1231 | if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) { |
| 1232 | unsigned int mask, shift; |
| 1233 | |
| 1234 | if (entry->trunk) { |
| 1235 | reg |= GLOBAL_ATU_DATA_TRUNK; |
| 1236 | mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK; |
| 1237 | shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT; |
| 1238 | } else { |
| 1239 | mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK; |
| 1240 | shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT; |
| 1241 | } |
| 1242 | |
| 1243 | reg |= (entry->portv_trunkid << shift) & mask; |
| 1244 | } |
| 1245 | |
| 1246 | reg |= entry->state & GLOBAL_ATU_DATA_STATE_MASK; |
| 1247 | |
| 1248 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_DATA, reg); |
| 1249 | if (ret < 0) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1250 | return ret; |
| 1251 | |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame^] | 1252 | return _mv88e6xxx_atu_cmd(ds, entry->fid, GLOBAL_ATU_OP_LOAD_DB); |
| 1253 | } |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 1254 | |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame^] | 1255 | static int _mv88e6xxx_port_vid_to_fid(struct dsa_switch *ds, int port, u16 vid) |
| 1256 | { |
| 1257 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1258 | |
| 1259 | if (vid == 0) |
| 1260 | return ps->fid[port]; |
| 1261 | |
| 1262 | return -ENOENT; |
| 1263 | } |
| 1264 | |
| 1265 | static int _mv88e6xxx_port_fdb_load(struct dsa_switch *ds, int port, |
| 1266 | const unsigned char *addr, u16 vid, |
| 1267 | u8 state) |
| 1268 | { |
| 1269 | struct mv88e6xxx_atu_entry entry = { 0 }; |
| 1270 | int ret; |
| 1271 | |
| 1272 | ret = _mv88e6xxx_port_vid_to_fid(ds, port, vid); |
| 1273 | if (ret < 0) |
| 1274 | return ret; |
| 1275 | |
| 1276 | entry.fid = ret; |
| 1277 | entry.state = state; |
| 1278 | ether_addr_copy(entry.mac, addr); |
| 1279 | if (state != GLOBAL_ATU_DATA_STATE_UNUSED) { |
| 1280 | entry.trunk = false; |
| 1281 | entry.portv_trunkid = BIT(port); |
| 1282 | } |
| 1283 | |
| 1284 | return _mv88e6xxx_atu_load(ds, &entry); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1285 | } |
| 1286 | |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 1287 | int mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port, |
| 1288 | const unsigned char *addr, u16 vid) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1289 | { |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 1290 | int state = is_multicast_ether_addr(addr) ? |
| 1291 | GLOBAL_ATU_DATA_STATE_MC_STATIC : |
| 1292 | GLOBAL_ATU_DATA_STATE_UC_STATIC; |
| 1293 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | 6630e23 | 2015-08-06 01:44:07 -0400 | [diff] [blame] | 1294 | int ret; |
| 1295 | |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 1296 | mutex_lock(&ps->smi_mutex); |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame^] | 1297 | ret = _mv88e6xxx_port_fdb_load(ds, port, addr, vid, state); |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 1298 | mutex_unlock(&ps->smi_mutex); |
| 1299 | |
| 1300 | return ret; |
| 1301 | } |
| 1302 | |
| 1303 | int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port, |
| 1304 | const unsigned char *addr, u16 vid) |
| 1305 | { |
| 1306 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1307 | int ret; |
| 1308 | |
| 1309 | mutex_lock(&ps->smi_mutex); |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame^] | 1310 | ret = _mv88e6xxx_port_fdb_load(ds, port, addr, vid, |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 1311 | GLOBAL_ATU_DATA_STATE_UNUSED); |
| 1312 | mutex_unlock(&ps->smi_mutex); |
| 1313 | |
| 1314 | return ret; |
| 1315 | } |
| 1316 | |
| 1317 | static int __mv88e6xxx_port_getnext(struct dsa_switch *ds, int port, |
| 1318 | unsigned char *addr, bool *is_static) |
| 1319 | { |
| 1320 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1321 | u8 fid = ps->fid[port]; |
| 1322 | int ret, state; |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1323 | |
| 1324 | ret = _mv88e6xxx_atu_wait(ds); |
| 1325 | if (ret < 0) |
| 1326 | return ret; |
| 1327 | |
Vivien Didelot | c5723ac | 2015-08-10 09:09:48 -0400 | [diff] [blame] | 1328 | ret = _mv88e6xxx_atu_mac_write(ds, addr); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1329 | if (ret < 0) |
| 1330 | return ret; |
| 1331 | |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 1332 | do { |
| 1333 | ret = _mv88e6xxx_atu_cmd(ds, fid, GLOBAL_ATU_OP_GET_NEXT_DB); |
| 1334 | if (ret < 0) |
| 1335 | return ret; |
| 1336 | |
| 1337 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_DATA); |
| 1338 | if (ret < 0) |
| 1339 | return ret; |
| 1340 | state = ret & GLOBAL_ATU_DATA_STATE_MASK; |
| 1341 | if (state == GLOBAL_ATU_DATA_STATE_UNUSED) |
| 1342 | return -ENOENT; |
| 1343 | } while (!(((ret >> 4) & 0xff) & (1 << port))); |
| 1344 | |
Vivien Didelot | c5723ac | 2015-08-10 09:09:48 -0400 | [diff] [blame] | 1345 | ret = _mv88e6xxx_atu_mac_read(ds, addr); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1346 | if (ret < 0) |
| 1347 | return ret; |
| 1348 | |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 1349 | *is_static = state == (is_multicast_ether_addr(addr) ? |
| 1350 | GLOBAL_ATU_DATA_STATE_MC_STATIC : |
| 1351 | GLOBAL_ATU_DATA_STATE_UC_STATIC); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1352 | |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 1356 | /* get next entry for port */ |
| 1357 | int mv88e6xxx_port_fdb_getnext(struct dsa_switch *ds, int port, |
Vivien Didelot | 2a778e1 | 2015-08-10 09:09:49 -0400 | [diff] [blame] | 1358 | unsigned char *addr, u16 *vid, bool *is_static) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1359 | { |
| 1360 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | 8782051 | 2015-08-06 01:44:08 -0400 | [diff] [blame] | 1361 | int ret; |
| 1362 | |
| 1363 | mutex_lock(&ps->smi_mutex); |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 1364 | ret = __mv88e6xxx_port_getnext(ds, port, addr, is_static); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1365 | mutex_unlock(&ps->smi_mutex); |
| 1366 | |
| 1367 | return ret; |
| 1368 | } |
| 1369 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1370 | static void mv88e6xxx_bridge_work(struct work_struct *work) |
| 1371 | { |
| 1372 | struct mv88e6xxx_priv_state *ps; |
| 1373 | struct dsa_switch *ds; |
| 1374 | int port; |
| 1375 | |
| 1376 | ps = container_of(work, struct mv88e6xxx_priv_state, bridge_work); |
| 1377 | ds = ((struct dsa_switch *)ps) - 1; |
| 1378 | |
| 1379 | while (ps->port_state_update_mask) { |
| 1380 | port = __ffs(ps->port_state_update_mask); |
| 1381 | clear_bit(port, &ps->port_state_update_mask); |
| 1382 | mv88e6xxx_set_port_state(ds, port, ps->port_state[port]); |
| 1383 | } |
| 1384 | } |
| 1385 | |
Andrew Lunn | dbde9e6 | 2015-05-06 01:09:48 +0200 | [diff] [blame] | 1386 | static int mv88e6xxx_setup_port(struct dsa_switch *ds, int port) |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 1387 | { |
| 1388 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1389 | int ret, fid; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1390 | u16 reg; |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 1391 | |
| 1392 | mutex_lock(&ps->smi_mutex); |
| 1393 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1394 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 1395 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 1396 | mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 1397 | mv88e6xxx_6065_family(ds) || mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1398 | /* MAC Forcing register: don't force link, speed, |
| 1399 | * duplex or flow control state to any particular |
| 1400 | * values on physical ports, but force the CPU port |
| 1401 | * and all DSA ports to their maximum bandwidth and |
| 1402 | * full duplex. |
| 1403 | */ |
| 1404 | reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_PCS_CTRL); |
| 1405 | if (dsa_is_cpu_port(ds, port) || |
| 1406 | ds->dsa_port_mask & (1 << port)) { |
| 1407 | reg |= PORT_PCS_CTRL_FORCE_LINK | |
| 1408 | PORT_PCS_CTRL_LINK_UP | |
| 1409 | PORT_PCS_CTRL_DUPLEX_FULL | |
| 1410 | PORT_PCS_CTRL_FORCE_DUPLEX; |
| 1411 | if (mv88e6xxx_6065_family(ds)) |
| 1412 | reg |= PORT_PCS_CTRL_100; |
| 1413 | else |
| 1414 | reg |= PORT_PCS_CTRL_1000; |
| 1415 | } else { |
| 1416 | reg |= PORT_PCS_CTRL_UNFORCED; |
| 1417 | } |
| 1418 | |
| 1419 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1420 | PORT_PCS_CTRL, reg); |
| 1421 | if (ret) |
| 1422 | goto abort; |
| 1423 | } |
| 1424 | |
| 1425 | /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock, |
| 1426 | * disable Header mode, enable IGMP/MLD snooping, disable VLAN |
| 1427 | * tunneling, determine priority by looking at 802.1p and IP |
| 1428 | * priority fields (IP prio has precedence), and set STP state |
| 1429 | * to Forwarding. |
| 1430 | * |
| 1431 | * If this is the CPU link, use DSA or EDSA tagging depending |
| 1432 | * on which tagging mode was configured. |
| 1433 | * |
| 1434 | * If this is a link to another switch, use DSA tagging mode. |
| 1435 | * |
| 1436 | * If this is the upstream port for this switch, enable |
| 1437 | * forwarding of unknown unicasts and multicasts. |
| 1438 | */ |
| 1439 | reg = 0; |
| 1440 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 1441 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 1442 | mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 1443 | mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds)) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1444 | reg = PORT_CONTROL_IGMP_MLD_SNOOP | |
| 1445 | PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP | |
| 1446 | PORT_CONTROL_STATE_FORWARDING; |
| 1447 | if (dsa_is_cpu_port(ds, port)) { |
| 1448 | if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds)) |
| 1449 | reg |= PORT_CONTROL_DSA_TAG; |
| 1450 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 1451 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 1452 | mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1453 | if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA) |
| 1454 | reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA; |
| 1455 | else |
| 1456 | reg |= PORT_CONTROL_FRAME_MODE_DSA; |
| 1457 | } |
| 1458 | |
| 1459 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 1460 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 1461 | mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 1462 | mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1463 | if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA) |
| 1464 | reg |= PORT_CONTROL_EGRESS_ADD_TAG; |
| 1465 | } |
| 1466 | } |
| 1467 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 1468 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 1469 | mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) || |
| 1470 | mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1471 | if (ds->dsa_port_mask & (1 << port)) |
| 1472 | reg |= PORT_CONTROL_FRAME_MODE_DSA; |
| 1473 | if (port == dsa_upstream_port(ds)) |
| 1474 | reg |= PORT_CONTROL_FORWARD_UNKNOWN | |
| 1475 | PORT_CONTROL_FORWARD_UNKNOWN_MC; |
| 1476 | } |
| 1477 | if (reg) { |
| 1478 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1479 | PORT_CONTROL, reg); |
| 1480 | if (ret) |
| 1481 | goto abort; |
| 1482 | } |
| 1483 | |
| 1484 | /* Port Control 2: don't force a good FCS, set the maximum |
| 1485 | * frame size to 10240 bytes, don't let the switch add or |
| 1486 | * strip 802.1q tags, don't discard tagged or untagged frames |
| 1487 | * on this port, do a destination address lookup on all |
| 1488 | * received packets as usual, disable ARP mirroring and don't |
| 1489 | * send a copy of all transmitted/received frames on this port |
| 1490 | * to the CPU. |
| 1491 | */ |
| 1492 | reg = 0; |
| 1493 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 1494 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 1495 | mv88e6xxx_6095_family(ds) || mv88e6xxx_6320_family(ds)) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1496 | reg = PORT_CONTROL_2_MAP_DA; |
| 1497 | |
| 1498 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 1499 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6320_family(ds)) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1500 | reg |= PORT_CONTROL_2_JUMBO_10240; |
| 1501 | |
| 1502 | if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds)) { |
| 1503 | /* Set the upstream port this port should use */ |
| 1504 | reg |= dsa_upstream_port(ds); |
| 1505 | /* enable forwarding of unknown multicast addresses to |
| 1506 | * the upstream port |
| 1507 | */ |
| 1508 | if (port == dsa_upstream_port(ds)) |
| 1509 | reg |= PORT_CONTROL_2_FORWARD_UNKNOWN; |
| 1510 | } |
| 1511 | |
| 1512 | if (reg) { |
| 1513 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1514 | PORT_CONTROL_2, reg); |
| 1515 | if (ret) |
| 1516 | goto abort; |
| 1517 | } |
| 1518 | |
| 1519 | /* Port Association Vector: when learning source addresses |
| 1520 | * of packets, add the address to the address database using |
| 1521 | * a port bitmap that has only the bit for this port set and |
| 1522 | * the other bits clear. |
| 1523 | */ |
| 1524 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_ASSOC_VECTOR, |
| 1525 | 1 << port); |
| 1526 | if (ret) |
| 1527 | goto abort; |
| 1528 | |
| 1529 | /* Egress rate control 2: disable egress rate control. */ |
| 1530 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_RATE_CONTROL_2, |
| 1531 | 0x0000); |
| 1532 | if (ret) |
| 1533 | goto abort; |
| 1534 | |
| 1535 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 1536 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 1537 | mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1538 | /* Do not limit the period of time that this port can |
| 1539 | * be paused for by the remote end or the period of |
| 1540 | * time that this port can pause the remote end. |
| 1541 | */ |
| 1542 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1543 | PORT_PAUSE_CTRL, 0x0000); |
| 1544 | if (ret) |
| 1545 | goto abort; |
| 1546 | |
| 1547 | /* Port ATU control: disable limiting the number of |
| 1548 | * address database entries that this port is allowed |
| 1549 | * to use. |
| 1550 | */ |
| 1551 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1552 | PORT_ATU_CONTROL, 0x0000); |
| 1553 | /* Priority Override: disable DA, SA and VTU priority |
| 1554 | * override. |
| 1555 | */ |
| 1556 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1557 | PORT_PRI_OVERRIDE, 0x0000); |
| 1558 | if (ret) |
| 1559 | goto abort; |
| 1560 | |
| 1561 | /* Port Ethertype: use the Ethertype DSA Ethertype |
| 1562 | * value. |
| 1563 | */ |
| 1564 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1565 | PORT_ETH_TYPE, ETH_P_EDSA); |
| 1566 | if (ret) |
| 1567 | goto abort; |
| 1568 | /* Tag Remap: use an identity 802.1p prio -> switch |
| 1569 | * prio mapping. |
| 1570 | */ |
| 1571 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1572 | PORT_TAG_REGMAP_0123, 0x3210); |
| 1573 | if (ret) |
| 1574 | goto abort; |
| 1575 | |
| 1576 | /* Tag Remap 2: use an identity 802.1p prio -> switch |
| 1577 | * prio mapping. |
| 1578 | */ |
| 1579 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1580 | PORT_TAG_REGMAP_4567, 0x7654); |
| 1581 | if (ret) |
| 1582 | goto abort; |
| 1583 | } |
| 1584 | |
| 1585 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 1586 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 1587 | mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) || |
| 1588 | mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1589 | /* Rate Control: disable ingress rate limiting. */ |
| 1590 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1591 | PORT_RATE_CONTROL, 0x0001); |
| 1592 | if (ret) |
| 1593 | goto abort; |
| 1594 | } |
| 1595 | |
Guenter Roeck | 366f0a0 | 2015-03-26 18:36:30 -0700 | [diff] [blame] | 1596 | /* Port Control 1: disable trunking, disable sending |
| 1597 | * learning messages to this port. |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 1598 | */ |
Vivien Didelot | 614f03f | 2015-04-20 17:19:23 -0400 | [diff] [blame] | 1599 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_1, 0x0000); |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 1600 | if (ret) |
| 1601 | goto abort; |
| 1602 | |
| 1603 | /* Port based VLAN map: give each port its own address |
| 1604 | * database, allow the CPU port to talk to each of the 'real' |
| 1605 | * ports, and allow each of the 'real' ports to only talk to |
| 1606 | * the upstream port. |
| 1607 | */ |
Vivien Didelot | 194fea7 | 2015-08-10 09:09:47 -0400 | [diff] [blame] | 1608 | fid = port + 1; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1609 | ps->fid[port] = fid; |
Vivien Didelot | 194fea7 | 2015-08-10 09:09:47 -0400 | [diff] [blame] | 1610 | set_bit(fid, ps->fid_bitmap); |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 1611 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1612 | if (!dsa_is_cpu_port(ds, port)) |
| 1613 | ps->bridge_mask[fid] = 1 << port; |
| 1614 | |
| 1615 | ret = _mv88e6xxx_update_port_config(ds, port); |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 1616 | if (ret) |
| 1617 | goto abort; |
| 1618 | |
| 1619 | /* Default VLAN ID and priority: don't set a default VLAN |
| 1620 | * ID, and set the default packet priority to zero. |
| 1621 | */ |
Vivien Didelot | 47cf1e65 | 2015-04-20 17:43:26 -0400 | [diff] [blame] | 1622 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_DEFAULT_VLAN, |
| 1623 | 0x0000); |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 1624 | abort: |
| 1625 | mutex_unlock(&ps->smi_mutex); |
| 1626 | return ret; |
| 1627 | } |
| 1628 | |
Andrew Lunn | dbde9e6 | 2015-05-06 01:09:48 +0200 | [diff] [blame] | 1629 | int mv88e6xxx_setup_ports(struct dsa_switch *ds) |
| 1630 | { |
| 1631 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1632 | int ret; |
| 1633 | int i; |
| 1634 | |
| 1635 | for (i = 0; i < ps->num_ports; i++) { |
| 1636 | ret = mv88e6xxx_setup_port(ds, i); |
| 1637 | if (ret < 0) |
| 1638 | return ret; |
| 1639 | } |
| 1640 | return 0; |
| 1641 | } |
| 1642 | |
Andrew Lunn | 87c8cef | 2015-06-20 18:42:28 +0200 | [diff] [blame] | 1643 | static int mv88e6xxx_regs_show(struct seq_file *s, void *p) |
| 1644 | { |
| 1645 | struct dsa_switch *ds = s->private; |
| 1646 | |
| 1647 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1648 | int reg, port; |
| 1649 | |
| 1650 | seq_puts(s, " GLOBAL GLOBAL2 "); |
| 1651 | for (port = 0 ; port < ps->num_ports; port++) |
| 1652 | seq_printf(s, " %2d ", port); |
| 1653 | seq_puts(s, "\n"); |
| 1654 | |
| 1655 | for (reg = 0; reg < 32; reg++) { |
| 1656 | seq_printf(s, "%2x: ", reg); |
| 1657 | seq_printf(s, " %4x %4x ", |
| 1658 | mv88e6xxx_reg_read(ds, REG_GLOBAL, reg), |
| 1659 | mv88e6xxx_reg_read(ds, REG_GLOBAL2, reg)); |
| 1660 | |
| 1661 | for (port = 0 ; port < ps->num_ports; port++) |
| 1662 | seq_printf(s, "%4x ", |
| 1663 | mv88e6xxx_reg_read(ds, REG_PORT(port), reg)); |
| 1664 | seq_puts(s, "\n"); |
| 1665 | } |
| 1666 | |
| 1667 | return 0; |
| 1668 | } |
| 1669 | |
| 1670 | static int mv88e6xxx_regs_open(struct inode *inode, struct file *file) |
| 1671 | { |
| 1672 | return single_open(file, mv88e6xxx_regs_show, inode->i_private); |
| 1673 | } |
| 1674 | |
| 1675 | static const struct file_operations mv88e6xxx_regs_fops = { |
| 1676 | .open = mv88e6xxx_regs_open, |
| 1677 | .read = seq_read, |
| 1678 | .llseek = no_llseek, |
| 1679 | .release = single_release, |
| 1680 | .owner = THIS_MODULE, |
| 1681 | }; |
| 1682 | |
Andrew Lunn | 8a0a265 | 2015-06-20 18:42:29 +0200 | [diff] [blame] | 1683 | static void mv88e6xxx_atu_show_header(struct seq_file *s) |
| 1684 | { |
| 1685 | seq_puts(s, "DB T/P Vec State Addr\n"); |
| 1686 | } |
| 1687 | |
| 1688 | static void mv88e6xxx_atu_show_entry(struct seq_file *s, int dbnum, |
| 1689 | unsigned char *addr, int data) |
| 1690 | { |
| 1691 | bool trunk = !!(data & GLOBAL_ATU_DATA_TRUNK); |
| 1692 | int portvec = ((data & GLOBAL_ATU_DATA_PORT_VECTOR_MASK) >> |
| 1693 | GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT); |
| 1694 | int state = data & GLOBAL_ATU_DATA_STATE_MASK; |
| 1695 | |
| 1696 | seq_printf(s, "%03x %5s %10pb %x %pM\n", |
| 1697 | dbnum, (trunk ? "Trunk" : "Port"), &portvec, state, addr); |
| 1698 | } |
| 1699 | |
| 1700 | static int mv88e6xxx_atu_show_db(struct seq_file *s, struct dsa_switch *ds, |
| 1701 | int dbnum) |
| 1702 | { |
| 1703 | unsigned char bcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
| 1704 | unsigned char addr[6]; |
| 1705 | int ret, data, state; |
| 1706 | |
Vivien Didelot | c5723ac | 2015-08-10 09:09:48 -0400 | [diff] [blame] | 1707 | ret = _mv88e6xxx_atu_mac_write(ds, bcast); |
Andrew Lunn | 8a0a265 | 2015-06-20 18:42:29 +0200 | [diff] [blame] | 1708 | if (ret < 0) |
| 1709 | return ret; |
| 1710 | |
| 1711 | do { |
| 1712 | ret = _mv88e6xxx_atu_cmd(ds, dbnum, GLOBAL_ATU_OP_GET_NEXT_DB); |
| 1713 | if (ret < 0) |
| 1714 | return ret; |
| 1715 | data = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_DATA); |
| 1716 | if (data < 0) |
| 1717 | return data; |
| 1718 | |
| 1719 | state = data & GLOBAL_ATU_DATA_STATE_MASK; |
| 1720 | if (state == GLOBAL_ATU_DATA_STATE_UNUSED) |
| 1721 | break; |
Vivien Didelot | c5723ac | 2015-08-10 09:09:48 -0400 | [diff] [blame] | 1722 | ret = _mv88e6xxx_atu_mac_read(ds, addr); |
Andrew Lunn | 8a0a265 | 2015-06-20 18:42:29 +0200 | [diff] [blame] | 1723 | if (ret < 0) |
| 1724 | return ret; |
| 1725 | mv88e6xxx_atu_show_entry(s, dbnum, addr, data); |
| 1726 | } while (state != GLOBAL_ATU_DATA_STATE_UNUSED); |
| 1727 | |
| 1728 | return 0; |
| 1729 | } |
| 1730 | |
| 1731 | static int mv88e6xxx_atu_show(struct seq_file *s, void *p) |
| 1732 | { |
| 1733 | struct dsa_switch *ds = s->private; |
| 1734 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1735 | int dbnum; |
| 1736 | |
| 1737 | mv88e6xxx_atu_show_header(s); |
| 1738 | |
| 1739 | for (dbnum = 0; dbnum < 255; dbnum++) { |
| 1740 | mutex_lock(&ps->smi_mutex); |
| 1741 | mv88e6xxx_atu_show_db(s, ds, dbnum); |
| 1742 | mutex_unlock(&ps->smi_mutex); |
| 1743 | } |
| 1744 | |
| 1745 | return 0; |
| 1746 | } |
| 1747 | |
| 1748 | static int mv88e6xxx_atu_open(struct inode *inode, struct file *file) |
| 1749 | { |
| 1750 | return single_open(file, mv88e6xxx_atu_show, inode->i_private); |
| 1751 | } |
| 1752 | |
| 1753 | static const struct file_operations mv88e6xxx_atu_fops = { |
| 1754 | .open = mv88e6xxx_atu_open, |
| 1755 | .read = seq_read, |
| 1756 | .llseek = no_llseek, |
| 1757 | .release = single_release, |
| 1758 | .owner = THIS_MODULE, |
| 1759 | }; |
| 1760 | |
Andrew Lunn | 532c7a3 | 2015-06-20 18:42:31 +0200 | [diff] [blame] | 1761 | static void mv88e6xxx_stats_show_header(struct seq_file *s, |
| 1762 | struct mv88e6xxx_priv_state *ps) |
| 1763 | { |
| 1764 | int port; |
| 1765 | |
| 1766 | seq_puts(s, " Statistic "); |
| 1767 | for (port = 0 ; port < ps->num_ports; port++) |
| 1768 | seq_printf(s, "Port %2d ", port); |
| 1769 | seq_puts(s, "\n"); |
| 1770 | } |
| 1771 | |
| 1772 | static int mv88e6xxx_stats_show(struct seq_file *s, void *p) |
| 1773 | { |
| 1774 | struct dsa_switch *ds = s->private; |
| 1775 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1776 | struct mv88e6xxx_hw_stat *stats = mv88e6xxx_hw_stats; |
| 1777 | int port, stat, max_stats; |
| 1778 | uint64_t value; |
| 1779 | |
| 1780 | if (have_sw_in_discards(ds)) |
| 1781 | max_stats = ARRAY_SIZE(mv88e6xxx_hw_stats); |
| 1782 | else |
| 1783 | max_stats = ARRAY_SIZE(mv88e6xxx_hw_stats) - 3; |
| 1784 | |
| 1785 | mv88e6xxx_stats_show_header(s, ps); |
| 1786 | |
| 1787 | mutex_lock(&ps->smi_mutex); |
| 1788 | |
| 1789 | for (stat = 0; stat < max_stats; stat++) { |
| 1790 | seq_printf(s, "%19s: ", stats[stat].string); |
| 1791 | for (port = 0 ; port < ps->num_ports; port++) { |
| 1792 | _mv88e6xxx_stats_snapshot(ds, port); |
| 1793 | value = _mv88e6xxx_get_ethtool_stat(ds, stat, stats, |
| 1794 | port); |
| 1795 | seq_printf(s, "%8llu ", value); |
| 1796 | } |
| 1797 | seq_puts(s, "\n"); |
| 1798 | } |
| 1799 | mutex_unlock(&ps->smi_mutex); |
| 1800 | |
| 1801 | return 0; |
| 1802 | } |
| 1803 | |
| 1804 | static int mv88e6xxx_stats_open(struct inode *inode, struct file *file) |
| 1805 | { |
| 1806 | return single_open(file, mv88e6xxx_stats_show, inode->i_private); |
| 1807 | } |
| 1808 | |
| 1809 | static const struct file_operations mv88e6xxx_stats_fops = { |
| 1810 | .open = mv88e6xxx_stats_open, |
| 1811 | .read = seq_read, |
| 1812 | .llseek = no_llseek, |
| 1813 | .release = single_release, |
| 1814 | .owner = THIS_MODULE, |
| 1815 | }; |
| 1816 | |
Andrew Lunn | d35bd87 | 2015-06-20 18:42:32 +0200 | [diff] [blame] | 1817 | static int mv88e6xxx_device_map_show(struct seq_file *s, void *p) |
| 1818 | { |
| 1819 | struct dsa_switch *ds = s->private; |
| 1820 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1821 | int target, ret; |
| 1822 | |
| 1823 | seq_puts(s, "Target Port\n"); |
| 1824 | |
| 1825 | mutex_lock(&ps->smi_mutex); |
| 1826 | for (target = 0; target < 32; target++) { |
| 1827 | ret = _mv88e6xxx_reg_write( |
| 1828 | ds, REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING, |
| 1829 | target << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT); |
| 1830 | if (ret < 0) |
| 1831 | goto out; |
| 1832 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL2, |
| 1833 | GLOBAL2_DEVICE_MAPPING); |
| 1834 | seq_printf(s, " %2d %2d\n", target, |
| 1835 | ret & GLOBAL2_DEVICE_MAPPING_PORT_MASK); |
| 1836 | } |
| 1837 | out: |
| 1838 | mutex_unlock(&ps->smi_mutex); |
| 1839 | |
| 1840 | return 0; |
| 1841 | } |
| 1842 | |
| 1843 | static int mv88e6xxx_device_map_open(struct inode *inode, struct file *file) |
| 1844 | { |
| 1845 | return single_open(file, mv88e6xxx_device_map_show, inode->i_private); |
| 1846 | } |
| 1847 | |
| 1848 | static const struct file_operations mv88e6xxx_device_map_fops = { |
| 1849 | .open = mv88e6xxx_device_map_open, |
| 1850 | .read = seq_read, |
| 1851 | .llseek = no_llseek, |
| 1852 | .release = single_release, |
| 1853 | .owner = THIS_MODULE, |
| 1854 | }; |
| 1855 | |
Andrew Lunn | 56d95e2 | 2015-06-20 18:42:33 +0200 | [diff] [blame] | 1856 | static int mv88e6xxx_scratch_show(struct seq_file *s, void *p) |
| 1857 | { |
| 1858 | struct dsa_switch *ds = s->private; |
| 1859 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1860 | int reg, ret; |
| 1861 | |
| 1862 | seq_puts(s, "Register Value\n"); |
| 1863 | |
| 1864 | mutex_lock(&ps->smi_mutex); |
| 1865 | for (reg = 0; reg < 0x80; reg++) { |
| 1866 | ret = _mv88e6xxx_reg_write( |
| 1867 | ds, REG_GLOBAL2, GLOBAL2_SCRATCH_MISC, |
| 1868 | reg << GLOBAL2_SCRATCH_REGISTER_SHIFT); |
| 1869 | if (ret < 0) |
| 1870 | goto out; |
| 1871 | |
| 1872 | ret = _mv88e6xxx_scratch_wait(ds); |
| 1873 | if (ret < 0) |
| 1874 | goto out; |
| 1875 | |
| 1876 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL2, |
| 1877 | GLOBAL2_SCRATCH_MISC); |
| 1878 | seq_printf(s, " %2x %2x\n", reg, |
| 1879 | ret & GLOBAL2_SCRATCH_VALUE_MASK); |
| 1880 | } |
| 1881 | out: |
| 1882 | mutex_unlock(&ps->smi_mutex); |
| 1883 | |
| 1884 | return 0; |
| 1885 | } |
| 1886 | |
| 1887 | static int mv88e6xxx_scratch_open(struct inode *inode, struct file *file) |
| 1888 | { |
| 1889 | return single_open(file, mv88e6xxx_scratch_show, inode->i_private); |
| 1890 | } |
| 1891 | |
| 1892 | static const struct file_operations mv88e6xxx_scratch_fops = { |
| 1893 | .open = mv88e6xxx_scratch_open, |
| 1894 | .read = seq_read, |
| 1895 | .llseek = no_llseek, |
| 1896 | .release = single_release, |
| 1897 | .owner = THIS_MODULE, |
| 1898 | }; |
| 1899 | |
Guenter Roeck | acdaffc | 2015-03-26 18:36:28 -0700 | [diff] [blame] | 1900 | int mv88e6xxx_setup_common(struct dsa_switch *ds) |
| 1901 | { |
| 1902 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Andrew Lunn | 87c8cef | 2015-06-20 18:42:28 +0200 | [diff] [blame] | 1903 | char *name; |
Guenter Roeck | acdaffc | 2015-03-26 18:36:28 -0700 | [diff] [blame] | 1904 | |
| 1905 | mutex_init(&ps->smi_mutex); |
Guenter Roeck | acdaffc | 2015-03-26 18:36:28 -0700 | [diff] [blame] | 1906 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1907 | ps->id = REG_READ(REG_PORT(0), PORT_SWITCH_ID) & 0xfff0; |
Andrew Lunn | a8f064c | 2015-03-26 18:36:40 -0700 | [diff] [blame] | 1908 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1909 | INIT_WORK(&ps->bridge_work, mv88e6xxx_bridge_work); |
| 1910 | |
Andrew Lunn | 87c8cef | 2015-06-20 18:42:28 +0200 | [diff] [blame] | 1911 | name = kasprintf(GFP_KERNEL, "dsa%d", ds->index); |
| 1912 | ps->dbgfs = debugfs_create_dir(name, NULL); |
| 1913 | kfree(name); |
| 1914 | |
| 1915 | debugfs_create_file("regs", S_IRUGO, ps->dbgfs, ds, |
| 1916 | &mv88e6xxx_regs_fops); |
| 1917 | |
Andrew Lunn | 8a0a265 | 2015-06-20 18:42:29 +0200 | [diff] [blame] | 1918 | debugfs_create_file("atu", S_IRUGO, ps->dbgfs, ds, |
| 1919 | &mv88e6xxx_atu_fops); |
| 1920 | |
Andrew Lunn | 532c7a3 | 2015-06-20 18:42:31 +0200 | [diff] [blame] | 1921 | debugfs_create_file("stats", S_IRUGO, ps->dbgfs, ds, |
| 1922 | &mv88e6xxx_stats_fops); |
| 1923 | |
Andrew Lunn | d35bd87 | 2015-06-20 18:42:32 +0200 | [diff] [blame] | 1924 | debugfs_create_file("device_map", S_IRUGO, ps->dbgfs, ds, |
| 1925 | &mv88e6xxx_device_map_fops); |
Andrew Lunn | 56d95e2 | 2015-06-20 18:42:33 +0200 | [diff] [blame] | 1926 | |
| 1927 | debugfs_create_file("scratch", S_IRUGO, ps->dbgfs, ds, |
| 1928 | &mv88e6xxx_scratch_fops); |
Guenter Roeck | acdaffc | 2015-03-26 18:36:28 -0700 | [diff] [blame] | 1929 | return 0; |
| 1930 | } |
| 1931 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1932 | int mv88e6xxx_setup_global(struct dsa_switch *ds) |
| 1933 | { |
| 1934 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | 24751e2 | 2015-08-03 09:17:44 -0400 | [diff] [blame] | 1935 | int ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 1936 | int i; |
| 1937 | |
| 1938 | /* Set the default address aging time to 5 minutes, and |
| 1939 | * enable address learn messages to be sent to all message |
| 1940 | * ports. |
| 1941 | */ |
| 1942 | REG_WRITE(REG_GLOBAL, GLOBAL_ATU_CONTROL, |
| 1943 | 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL); |
| 1944 | |
| 1945 | /* Configure the IP ToS mapping registers. */ |
| 1946 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000); |
| 1947 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000); |
| 1948 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555); |
| 1949 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555); |
| 1950 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa); |
| 1951 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa); |
| 1952 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff); |
| 1953 | REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff); |
| 1954 | |
| 1955 | /* Configure the IEEE 802.1p priority mapping register. */ |
| 1956 | REG_WRITE(REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41); |
| 1957 | |
| 1958 | /* Send all frames with destination addresses matching |
| 1959 | * 01:80:c2:00:00:0x to the CPU port. |
| 1960 | */ |
| 1961 | REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff); |
| 1962 | |
| 1963 | /* Ignore removed tag data on doubly tagged packets, disable |
| 1964 | * flow control messages, force flow control priority to the |
| 1965 | * highest, and send all special multicast frames to the CPU |
| 1966 | * port at the highest priority. |
| 1967 | */ |
| 1968 | REG_WRITE(REG_GLOBAL2, GLOBAL2_SWITCH_MGMT, |
| 1969 | 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 | |
| 1970 | GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI); |
| 1971 | |
| 1972 | /* Program the DSA routing table. */ |
| 1973 | for (i = 0; i < 32; i++) { |
| 1974 | int nexthop = 0x1f; |
| 1975 | |
| 1976 | if (ds->pd->rtable && |
| 1977 | i != ds->index && i < ds->dst->pd->nr_chips) |
| 1978 | nexthop = ds->pd->rtable[i] & 0x1f; |
| 1979 | |
| 1980 | REG_WRITE(REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING, |
| 1981 | GLOBAL2_DEVICE_MAPPING_UPDATE | |
| 1982 | (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) | |
| 1983 | nexthop); |
| 1984 | } |
| 1985 | |
| 1986 | /* Clear all trunk masks. */ |
| 1987 | for (i = 0; i < 8; i++) |
| 1988 | REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MASK, |
| 1989 | 0x8000 | (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) | |
| 1990 | ((1 << ps->num_ports) - 1)); |
| 1991 | |
| 1992 | /* Clear all trunk mappings. */ |
| 1993 | for (i = 0; i < 16; i++) |
| 1994 | REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MAPPING, |
| 1995 | GLOBAL2_TRUNK_MAPPING_UPDATE | |
| 1996 | (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT)); |
| 1997 | |
| 1998 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 1999 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 2000 | mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2001 | /* Send all frames with destination addresses matching |
| 2002 | * 01:80:c2:00:00:2x to the CPU port. |
| 2003 | */ |
| 2004 | REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_2X, 0xffff); |
| 2005 | |
| 2006 | /* Initialise cross-chip port VLAN table to reset |
| 2007 | * defaults. |
| 2008 | */ |
| 2009 | REG_WRITE(REG_GLOBAL2, GLOBAL2_PVT_ADDR, 0x9000); |
| 2010 | |
| 2011 | /* Clear the priority override table. */ |
| 2012 | for (i = 0; i < 16; i++) |
| 2013 | REG_WRITE(REG_GLOBAL2, GLOBAL2_PRIO_OVERRIDE, |
| 2014 | 0x8000 | (i << 8)); |
| 2015 | } |
| 2016 | |
| 2017 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 2018 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 2019 | mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) || |
| 2020 | mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2021 | /* Disable ingress rate limiting by resetting all |
| 2022 | * ingress rate limit registers to their initial |
| 2023 | * state. |
| 2024 | */ |
| 2025 | for (i = 0; i < ps->num_ports; i++) |
| 2026 | REG_WRITE(REG_GLOBAL2, GLOBAL2_INGRESS_OP, |
| 2027 | 0x9000 | (i << 8)); |
| 2028 | } |
| 2029 | |
Andrew Lunn | db687a5 | 2015-06-20 21:31:29 +0200 | [diff] [blame] | 2030 | /* Clear the statistics counters for all ports */ |
| 2031 | REG_WRITE(REG_GLOBAL, GLOBAL_STATS_OP, GLOBAL_STATS_OP_FLUSH_ALL); |
| 2032 | |
| 2033 | /* Wait for the flush to complete. */ |
Vivien Didelot | 24751e2 | 2015-08-03 09:17:44 -0400 | [diff] [blame] | 2034 | mutex_lock(&ps->smi_mutex); |
| 2035 | ret = _mv88e6xxx_stats_wait(ds); |
| 2036 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | db687a5 | 2015-06-20 21:31:29 +0200 | [diff] [blame] | 2037 | |
Vivien Didelot | 24751e2 | 2015-08-03 09:17:44 -0400 | [diff] [blame] | 2038 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2039 | } |
| 2040 | |
Andrew Lunn | 143a830 | 2015-04-02 04:06:34 +0200 | [diff] [blame] | 2041 | int mv88e6xxx_switch_reset(struct dsa_switch *ds, bool ppu_active) |
| 2042 | { |
| 2043 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2044 | u16 is_reset = (ppu_active ? 0x8800 : 0xc800); |
| 2045 | unsigned long timeout; |
| 2046 | int ret; |
| 2047 | int i; |
| 2048 | |
| 2049 | /* Set all ports to the disabled state. */ |
| 2050 | for (i = 0; i < ps->num_ports; i++) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 2051 | ret = REG_READ(REG_PORT(i), PORT_CONTROL); |
| 2052 | REG_WRITE(REG_PORT(i), PORT_CONTROL, ret & 0xfffc); |
Andrew Lunn | 143a830 | 2015-04-02 04:06:34 +0200 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | /* Wait for transmit queues to drain. */ |
| 2056 | usleep_range(2000, 4000); |
| 2057 | |
| 2058 | /* Reset the switch. Keep the PPU active if requested. The PPU |
| 2059 | * needs to be active to support indirect phy register access |
| 2060 | * through global registers 0x18 and 0x19. |
| 2061 | */ |
| 2062 | if (ppu_active) |
| 2063 | REG_WRITE(REG_GLOBAL, 0x04, 0xc000); |
| 2064 | else |
| 2065 | REG_WRITE(REG_GLOBAL, 0x04, 0xc400); |
| 2066 | |
| 2067 | /* Wait up to one second for reset to complete. */ |
| 2068 | timeout = jiffies + 1 * HZ; |
| 2069 | while (time_before(jiffies, timeout)) { |
| 2070 | ret = REG_READ(REG_GLOBAL, 0x00); |
| 2071 | if ((ret & is_reset) == is_reset) |
| 2072 | break; |
| 2073 | usleep_range(1000, 2000); |
| 2074 | } |
| 2075 | if (time_after(jiffies, timeout)) |
| 2076 | return -ETIMEDOUT; |
| 2077 | |
| 2078 | return 0; |
| 2079 | } |
| 2080 | |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 2081 | int mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page, int reg) |
| 2082 | { |
| 2083 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2084 | int ret; |
| 2085 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2086 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2087 | ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page); |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 2088 | if (ret < 0) |
| 2089 | goto error; |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2090 | ret = _mv88e6xxx_phy_read_indirect(ds, port, reg); |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 2091 | error: |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2092 | _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2093 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 2094 | return ret; |
| 2095 | } |
| 2096 | |
| 2097 | int mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page, |
| 2098 | int reg, int val) |
| 2099 | { |
| 2100 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2101 | int ret; |
| 2102 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2103 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2104 | ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page); |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 2105 | if (ret < 0) |
| 2106 | goto error; |
| 2107 | |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2108 | ret = _mv88e6xxx_phy_write_indirect(ds, port, reg, val); |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 2109 | error: |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2110 | _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2111 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2112 | return ret; |
| 2113 | } |
| 2114 | |
| 2115 | static int mv88e6xxx_port_to_phy_addr(struct dsa_switch *ds, int port) |
| 2116 | { |
| 2117 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2118 | |
| 2119 | if (port >= 0 && port < ps->num_ports) |
| 2120 | return port; |
| 2121 | return -EINVAL; |
| 2122 | } |
| 2123 | |
| 2124 | int |
| 2125 | mv88e6xxx_phy_read(struct dsa_switch *ds, int port, int regnum) |
| 2126 | { |
| 2127 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2128 | int addr = mv88e6xxx_port_to_phy_addr(ds, port); |
| 2129 | int ret; |
| 2130 | |
| 2131 | if (addr < 0) |
| 2132 | return addr; |
| 2133 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2134 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2135 | ret = _mv88e6xxx_phy_read(ds, addr, regnum); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2136 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2137 | return ret; |
| 2138 | } |
| 2139 | |
| 2140 | int |
| 2141 | mv88e6xxx_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val) |
| 2142 | { |
| 2143 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2144 | int addr = mv88e6xxx_port_to_phy_addr(ds, port); |
| 2145 | int ret; |
| 2146 | |
| 2147 | if (addr < 0) |
| 2148 | return addr; |
| 2149 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2150 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2151 | ret = _mv88e6xxx_phy_write(ds, addr, regnum, val); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2152 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2153 | return ret; |
| 2154 | } |
| 2155 | |
| 2156 | int |
| 2157 | mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int port, int regnum) |
| 2158 | { |
| 2159 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2160 | int addr = mv88e6xxx_port_to_phy_addr(ds, port); |
| 2161 | int ret; |
| 2162 | |
| 2163 | if (addr < 0) |
| 2164 | return addr; |
| 2165 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2166 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2167 | ret = _mv88e6xxx_phy_read_indirect(ds, addr, regnum); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2168 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2169 | return ret; |
| 2170 | } |
| 2171 | |
| 2172 | int |
| 2173 | mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int port, int regnum, |
| 2174 | u16 val) |
| 2175 | { |
| 2176 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2177 | int addr = mv88e6xxx_port_to_phy_addr(ds, port); |
| 2178 | int ret; |
| 2179 | |
| 2180 | if (addr < 0) |
| 2181 | return addr; |
| 2182 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2183 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2184 | ret = _mv88e6xxx_phy_write_indirect(ds, addr, regnum, val); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2185 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 2186 | return ret; |
| 2187 | } |
| 2188 | |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 2189 | #ifdef CONFIG_NET_DSA_HWMON |
| 2190 | |
| 2191 | static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp) |
| 2192 | { |
| 2193 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2194 | int ret; |
| 2195 | int val; |
| 2196 | |
| 2197 | *temp = 0; |
| 2198 | |
| 2199 | mutex_lock(&ps->smi_mutex); |
| 2200 | |
| 2201 | ret = _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x6); |
| 2202 | if (ret < 0) |
| 2203 | goto error; |
| 2204 | |
| 2205 | /* Enable temperature sensor */ |
| 2206 | ret = _mv88e6xxx_phy_read(ds, 0x0, 0x1a); |
| 2207 | if (ret < 0) |
| 2208 | goto error; |
| 2209 | |
| 2210 | ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret | (1 << 5)); |
| 2211 | if (ret < 0) |
| 2212 | goto error; |
| 2213 | |
| 2214 | /* Wait for temperature to stabilize */ |
| 2215 | usleep_range(10000, 12000); |
| 2216 | |
| 2217 | val = _mv88e6xxx_phy_read(ds, 0x0, 0x1a); |
| 2218 | if (val < 0) { |
| 2219 | ret = val; |
| 2220 | goto error; |
| 2221 | } |
| 2222 | |
| 2223 | /* Disable temperature sensor */ |
| 2224 | ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret & ~(1 << 5)); |
| 2225 | if (ret < 0) |
| 2226 | goto error; |
| 2227 | |
| 2228 | *temp = ((val & 0x1f) - 5) * 5; |
| 2229 | |
| 2230 | error: |
| 2231 | _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x0); |
| 2232 | mutex_unlock(&ps->smi_mutex); |
| 2233 | return ret; |
| 2234 | } |
| 2235 | |
| 2236 | static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp) |
| 2237 | { |
| 2238 | int phy = mv88e6xxx_6320_family(ds) ? 3 : 0; |
| 2239 | int ret; |
| 2240 | |
| 2241 | *temp = 0; |
| 2242 | |
| 2243 | ret = mv88e6xxx_phy_page_read(ds, phy, 6, 27); |
| 2244 | if (ret < 0) |
| 2245 | return ret; |
| 2246 | |
| 2247 | *temp = (ret & 0xff) - 25; |
| 2248 | |
| 2249 | return 0; |
| 2250 | } |
| 2251 | |
| 2252 | int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp) |
| 2253 | { |
| 2254 | if (mv88e6xxx_6320_family(ds) || mv88e6xxx_6352_family(ds)) |
| 2255 | return mv88e63xx_get_temp(ds, temp); |
| 2256 | |
| 2257 | return mv88e61xx_get_temp(ds, temp); |
| 2258 | } |
| 2259 | |
| 2260 | int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp) |
| 2261 | { |
| 2262 | int phy = mv88e6xxx_6320_family(ds) ? 3 : 0; |
| 2263 | int ret; |
| 2264 | |
| 2265 | if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds)) |
| 2266 | return -EOPNOTSUPP; |
| 2267 | |
| 2268 | *temp = 0; |
| 2269 | |
| 2270 | ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26); |
| 2271 | if (ret < 0) |
| 2272 | return ret; |
| 2273 | |
| 2274 | *temp = (((ret >> 8) & 0x1f) * 5) - 25; |
| 2275 | |
| 2276 | return 0; |
| 2277 | } |
| 2278 | |
| 2279 | int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp) |
| 2280 | { |
| 2281 | int phy = mv88e6xxx_6320_family(ds) ? 3 : 0; |
| 2282 | int ret; |
| 2283 | |
| 2284 | if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds)) |
| 2285 | return -EOPNOTSUPP; |
| 2286 | |
| 2287 | ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26); |
| 2288 | if (ret < 0) |
| 2289 | return ret; |
| 2290 | temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f); |
| 2291 | return mv88e6xxx_phy_page_write(ds, phy, 6, 26, |
| 2292 | (ret & 0xe0ff) | (temp << 8)); |
| 2293 | } |
| 2294 | |
| 2295 | int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm) |
| 2296 | { |
| 2297 | int phy = mv88e6xxx_6320_family(ds) ? 3 : 0; |
| 2298 | int ret; |
| 2299 | |
| 2300 | if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds)) |
| 2301 | return -EOPNOTSUPP; |
| 2302 | |
| 2303 | *alarm = false; |
| 2304 | |
| 2305 | ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26); |
| 2306 | if (ret < 0) |
| 2307 | return ret; |
| 2308 | |
| 2309 | *alarm = !!(ret & 0x40); |
| 2310 | |
| 2311 | return 0; |
| 2312 | } |
| 2313 | #endif /* CONFIG_NET_DSA_HWMON */ |
| 2314 | |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 2315 | static int __init mv88e6xxx_init(void) |
| 2316 | { |
| 2317 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131) |
| 2318 | register_switch_driver(&mv88e6131_switch_driver); |
| 2319 | #endif |
| 2320 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65) |
| 2321 | register_switch_driver(&mv88e6123_61_65_switch_driver); |
| 2322 | #endif |
Guenter Roeck | 3ad50cc | 2014-10-29 10:44:56 -0700 | [diff] [blame] | 2323 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352) |
| 2324 | register_switch_driver(&mv88e6352_switch_driver); |
| 2325 | #endif |
Andrew Lunn | 42f2725 | 2014-09-12 23:58:44 +0200 | [diff] [blame] | 2326 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171) |
| 2327 | register_switch_driver(&mv88e6171_switch_driver); |
| 2328 | #endif |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 2329 | return 0; |
| 2330 | } |
| 2331 | module_init(mv88e6xxx_init); |
| 2332 | |
| 2333 | static void __exit mv88e6xxx_cleanup(void) |
| 2334 | { |
Andrew Lunn | 42f2725 | 2014-09-12 23:58:44 +0200 | [diff] [blame] | 2335 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171) |
| 2336 | unregister_switch_driver(&mv88e6171_switch_driver); |
| 2337 | #endif |
Vivien Didelot | 4212b54 | 2015-05-01 10:43:52 -0400 | [diff] [blame] | 2338 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352) |
| 2339 | unregister_switch_driver(&mv88e6352_switch_driver); |
| 2340 | #endif |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 2341 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65) |
| 2342 | unregister_switch_driver(&mv88e6123_61_65_switch_driver); |
| 2343 | #endif |
| 2344 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131) |
| 2345 | unregister_switch_driver(&mv88e6131_switch_driver); |
| 2346 | #endif |
| 2347 | } |
| 2348 | module_exit(mv88e6xxx_cleanup); |
Ben Hutchings | 3d825ed | 2011-11-25 14:37:16 +0000 | [diff] [blame] | 2349 | |
| 2350 | MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>"); |
| 2351 | MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips"); |
| 2352 | MODULE_LICENSE("GPL"); |