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