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 | * |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 8 | * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch> |
| 9 | * |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | */ |
| 15 | |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 16 | #include <linux/delay.h> |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 17 | #include <linux/etherdevice.h> |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 18 | #include <linux/ethtool.h> |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 19 | #include <linux/if_bridge.h> |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 20 | #include <linux/jiffies.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 21 | #include <linux/list.h> |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 22 | #include <linux/mdio.h> |
Paul Gortmaker | 2bbba27 | 2012-01-24 10:41:40 +0000 | [diff] [blame] | 23 | #include <linux/module.h> |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame^] | 24 | #include <linux/of_mdio.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 25 | #include <linux/netdevice.h> |
Andrew Lunn | c8c1b39a | 2015-11-20 03:56:24 +0100 | [diff] [blame] | 26 | #include <linux/gpio/consumer.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 27 | #include <linux/phy.h> |
Ben Hutchings | c8f0b86 | 2011-11-27 17:06:08 +0000 | [diff] [blame] | 28 | #include <net/dsa.h> |
Vivien Didelot | 1f36faf | 2015-10-08 11:35:13 -0400 | [diff] [blame] | 29 | #include <net/switchdev.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 30 | #include "mv88e6xxx.h" |
| 31 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 32 | static void assert_smi_lock(struct mv88e6xxx_priv_state *ps) |
Vivien Didelot | 3996a4f | 2015-10-30 18:56:45 -0400 | [diff] [blame] | 33 | { |
Vivien Didelot | 3996a4f | 2015-10-30 18:56:45 -0400 | [diff] [blame] | 34 | if (unlikely(!mutex_is_locked(&ps->smi_mutex))) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 35 | dev_err(ps->dev, "SMI lock not held!\n"); |
Vivien Didelot | 3996a4f | 2015-10-30 18:56:45 -0400 | [diff] [blame] | 36 | dump_stack(); |
| 37 | } |
| 38 | } |
| 39 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 40 | /* 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] | 41 | * use all 32 SMI bus addresses on its SMI bus, and all switch registers |
| 42 | * will be directly accessible on some {device address,register address} |
| 43 | * pair. If the ADDR[4:0] pins are not strapped to zero, the switch |
| 44 | * will only respond to SMI transactions to that specific address, and |
| 45 | * an indirect addressing mechanism needs to be used to access its |
| 46 | * registers. |
| 47 | */ |
| 48 | static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr) |
| 49 | { |
| 50 | int ret; |
| 51 | int i; |
| 52 | |
| 53 | for (i = 0; i < 16; i++) { |
Neil Armstrong | 6e899e6 | 2015-10-22 10:37:53 +0200 | [diff] [blame] | 54 | ret = mdiobus_read_nested(bus, sw_addr, SMI_CMD); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 55 | if (ret < 0) |
| 56 | return ret; |
| 57 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 58 | if ((ret & SMI_CMD_BUSY) == 0) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | return -ETIMEDOUT; |
| 63 | } |
| 64 | |
Vivien Didelot | b9b3771 | 2015-10-30 19:39:48 -0400 | [diff] [blame] | 65 | static int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr, |
| 66 | int reg) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 67 | { |
| 68 | int ret; |
| 69 | |
| 70 | if (sw_addr == 0) |
Neil Armstrong | 6e899e6 | 2015-10-22 10:37:53 +0200 | [diff] [blame] | 71 | return mdiobus_read_nested(bus, addr, reg); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 72 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 73 | /* Wait for the bus to become free. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 74 | ret = mv88e6xxx_reg_wait_ready(bus, sw_addr); |
| 75 | if (ret < 0) |
| 76 | return ret; |
| 77 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 78 | /* Transmit the read command. */ |
Neil Armstrong | 6e899e6 | 2015-10-22 10:37:53 +0200 | [diff] [blame] | 79 | ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD, |
| 80 | SMI_CMD_OP_22_READ | (addr << 5) | reg); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 81 | if (ret < 0) |
| 82 | return ret; |
| 83 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 84 | /* Wait for the read command to complete. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 85 | ret = mv88e6xxx_reg_wait_ready(bus, sw_addr); |
| 86 | if (ret < 0) |
| 87 | return ret; |
| 88 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 89 | /* Read the data. */ |
Neil Armstrong | 6e899e6 | 2015-10-22 10:37:53 +0200 | [diff] [blame] | 90 | ret = mdiobus_read_nested(bus, sw_addr, SMI_DATA); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 91 | if (ret < 0) |
| 92 | return ret; |
| 93 | |
| 94 | return ret & 0xffff; |
| 95 | } |
| 96 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 97 | static int _mv88e6xxx_reg_read(struct mv88e6xxx_priv_state *ps, |
| 98 | int addr, int reg) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 99 | { |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 100 | int ret; |
| 101 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 102 | assert_smi_lock(ps); |
Vivien Didelot | 3996a4f | 2015-10-30 18:56:45 -0400 | [diff] [blame] | 103 | |
Andrew Lunn | a77d43f | 2016-04-13 02:40:42 +0200 | [diff] [blame] | 104 | ret = __mv88e6xxx_reg_read(ps->bus, ps->sw_addr, addr, reg); |
Vivien Didelot | bb92ea5 | 2015-01-23 16:10:36 -0500 | [diff] [blame] | 105 | if (ret < 0) |
| 106 | return ret; |
| 107 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 108 | dev_dbg(ps->dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n", |
Vivien Didelot | bb92ea5 | 2015-01-23 16:10:36 -0500 | [diff] [blame] | 109 | addr, reg, ret); |
| 110 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 111 | return ret; |
| 112 | } |
| 113 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 114 | int mv88e6xxx_reg_read(struct mv88e6xxx_priv_state *ps, int addr, int reg) |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 115 | { |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 116 | int ret; |
| 117 | |
| 118 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 119 | ret = _mv88e6xxx_reg_read(ps, addr, reg); |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 120 | mutex_unlock(&ps->smi_mutex); |
| 121 | |
| 122 | return ret; |
| 123 | } |
| 124 | |
Vivien Didelot | b9b3771 | 2015-10-30 19:39:48 -0400 | [diff] [blame] | 125 | static int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr, |
| 126 | int reg, u16 val) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 127 | { |
| 128 | int ret; |
| 129 | |
| 130 | if (sw_addr == 0) |
Neil Armstrong | 6e899e6 | 2015-10-22 10:37:53 +0200 | [diff] [blame] | 131 | return mdiobus_write_nested(bus, addr, reg, val); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 132 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 133 | /* Wait for the bus to become free. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 134 | ret = mv88e6xxx_reg_wait_ready(bus, sw_addr); |
| 135 | if (ret < 0) |
| 136 | return ret; |
| 137 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 138 | /* Transmit the data to write. */ |
Neil Armstrong | 6e899e6 | 2015-10-22 10:37:53 +0200 | [diff] [blame] | 139 | ret = mdiobus_write_nested(bus, sw_addr, SMI_DATA, val); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 140 | if (ret < 0) |
| 141 | return ret; |
| 142 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 143 | /* Transmit the write command. */ |
Neil Armstrong | 6e899e6 | 2015-10-22 10:37:53 +0200 | [diff] [blame] | 144 | ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD, |
| 145 | SMI_CMD_OP_22_WRITE | (addr << 5) | reg); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 146 | if (ret < 0) |
| 147 | return ret; |
| 148 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 149 | /* Wait for the write command to complete. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 150 | ret = mv88e6xxx_reg_wait_ready(bus, sw_addr); |
| 151 | if (ret < 0) |
| 152 | return ret; |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 157 | static int _mv88e6xxx_reg_write(struct mv88e6xxx_priv_state *ps, int addr, |
| 158 | int reg, u16 val) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 159 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 160 | assert_smi_lock(ps); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 161 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 162 | dev_dbg(ps->dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n", |
Vivien Didelot | bb92ea5 | 2015-01-23 16:10:36 -0500 | [diff] [blame] | 163 | addr, reg, val); |
| 164 | |
Andrew Lunn | a77d43f | 2016-04-13 02:40:42 +0200 | [diff] [blame] | 165 | return __mv88e6xxx_reg_write(ps->bus, ps->sw_addr, addr, reg, val); |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 168 | int mv88e6xxx_reg_write(struct mv88e6xxx_priv_state *ps, int addr, |
| 169 | int reg, u16 val) |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 170 | { |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 171 | int ret; |
| 172 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 173 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 174 | ret = _mv88e6xxx_reg_write(ps, addr, reg, val); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 175 | mutex_unlock(&ps->smi_mutex); |
| 176 | |
| 177 | return ret; |
| 178 | } |
| 179 | |
Vivien Didelot | 1d13a06 | 2016-05-09 13:22:43 -0400 | [diff] [blame] | 180 | static int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr) |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 181 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 182 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 183 | int err; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 184 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 185 | err = mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_01, |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 186 | (addr[0] << 8) | addr[1]); |
| 187 | if (err) |
| 188 | return err; |
| 189 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 190 | err = mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_23, |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 191 | (addr[2] << 8) | addr[3]); |
| 192 | if (err) |
| 193 | return err; |
| 194 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 195 | return mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_45, |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 196 | (addr[4] << 8) | addr[5]); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Vivien Didelot | 1d13a06 | 2016-05-09 13:22:43 -0400 | [diff] [blame] | 199 | static int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 200 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 201 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 202 | int ret; |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 203 | int i; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 204 | |
| 205 | for (i = 0; i < 6; i++) { |
| 206 | int j; |
| 207 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 208 | /* Write the MAC address byte. */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 209 | ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SWITCH_MAC, |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 210 | GLOBAL2_SWITCH_MAC_BUSY | |
| 211 | (i << 8) | addr[i]); |
| 212 | if (ret) |
| 213 | return ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 214 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 215 | /* Wait for the write to complete. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 216 | for (j = 0; j < 16; j++) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 217 | ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2, |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 218 | GLOBAL2_SWITCH_MAC); |
| 219 | if (ret < 0) |
| 220 | return ret; |
| 221 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 222 | if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 223 | break; |
| 224 | } |
| 225 | if (j == 16) |
| 226 | return -ETIMEDOUT; |
| 227 | } |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
Vivien Didelot | 1d13a06 | 2016-05-09 13:22:43 -0400 | [diff] [blame] | 232 | int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr) |
| 233 | { |
| 234 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 235 | |
| 236 | if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SWITCH_MAC)) |
| 237 | return mv88e6xxx_set_addr_indirect(ds, addr); |
| 238 | else |
| 239 | return mv88e6xxx_set_addr_direct(ds, addr); |
| 240 | } |
| 241 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 242 | static int mv88e6xxx_mdio_read_direct(struct mv88e6xxx_priv_state *ps, |
| 243 | int addr, int regnum) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 244 | { |
| 245 | if (addr >= 0) |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 246 | return _mv88e6xxx_reg_read(ps, addr, regnum); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 247 | return 0xffff; |
| 248 | } |
| 249 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 250 | static int mv88e6xxx_mdio_write_direct(struct mv88e6xxx_priv_state *ps, |
| 251 | int addr, int regnum, u16 val) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 252 | { |
| 253 | if (addr >= 0) |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 254 | return _mv88e6xxx_reg_write(ps, addr, regnum, val); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 258 | static int mv88e6xxx_ppu_disable(struct mv88e6xxx_priv_state *ps) |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 259 | { |
| 260 | int ret; |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 261 | unsigned long timeout; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 262 | |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 263 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_CONTROL); |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 264 | if (ret < 0) |
| 265 | return ret; |
| 266 | |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 267 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL, |
| 268 | ret & ~GLOBAL_CONTROL_PPU_ENABLE); |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 269 | if (ret) |
| 270 | return ret; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 271 | |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 272 | timeout = jiffies + 1 * HZ; |
| 273 | while (time_before(jiffies, timeout)) { |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 274 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATUS); |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 275 | if (ret < 0) |
| 276 | return ret; |
| 277 | |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 278 | usleep_range(1000, 2000); |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 279 | if ((ret & GLOBAL_STATUS_PPU_MASK) != |
| 280 | GLOBAL_STATUS_PPU_POLLING) |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 281 | return 0; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | return -ETIMEDOUT; |
| 285 | } |
| 286 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 287 | static int mv88e6xxx_ppu_enable(struct mv88e6xxx_priv_state *ps) |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 288 | { |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 289 | int ret, err; |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 290 | unsigned long timeout; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 291 | |
Vivien Didelot | 762eb67 | 2016-06-04 21:16:54 +0200 | [diff] [blame] | 292 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_CONTROL); |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 293 | if (ret < 0) |
| 294 | return ret; |
| 295 | |
Vivien Didelot | 762eb67 | 2016-06-04 21:16:54 +0200 | [diff] [blame] | 296 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL, |
| 297 | ret | GLOBAL_CONTROL_PPU_ENABLE); |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 298 | if (err) |
| 299 | return err; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 300 | |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 301 | timeout = jiffies + 1 * HZ; |
| 302 | while (time_before(jiffies, timeout)) { |
Vivien Didelot | 762eb67 | 2016-06-04 21:16:54 +0200 | [diff] [blame] | 303 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATUS); |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 304 | if (ret < 0) |
| 305 | return ret; |
| 306 | |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 307 | usleep_range(1000, 2000); |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 308 | if ((ret & GLOBAL_STATUS_PPU_MASK) == |
| 309 | GLOBAL_STATUS_PPU_POLLING) |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 310 | return 0; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | return -ETIMEDOUT; |
| 314 | } |
| 315 | |
| 316 | static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly) |
| 317 | { |
| 318 | struct mv88e6xxx_priv_state *ps; |
| 319 | |
| 320 | ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work); |
Vivien Didelot | 762eb67 | 2016-06-04 21:16:54 +0200 | [diff] [blame] | 321 | |
| 322 | mutex_lock(&ps->smi_mutex); |
| 323 | |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 324 | if (mutex_trylock(&ps->ppu_mutex)) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 325 | if (mv88e6xxx_ppu_enable(ps) == 0) |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 326 | ps->ppu_disabled = 0; |
| 327 | mutex_unlock(&ps->ppu_mutex); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 328 | } |
Vivien Didelot | 762eb67 | 2016-06-04 21:16:54 +0200 | [diff] [blame] | 329 | |
| 330 | mutex_unlock(&ps->smi_mutex); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps) |
| 334 | { |
| 335 | struct mv88e6xxx_priv_state *ps = (void *)_ps; |
| 336 | |
| 337 | schedule_work(&ps->ppu_work); |
| 338 | } |
| 339 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 340 | static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_priv_state *ps) |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 341 | { |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 342 | int ret; |
| 343 | |
| 344 | mutex_lock(&ps->ppu_mutex); |
| 345 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 346 | /* If the PHY polling unit is enabled, disable it so that |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 347 | * we can access the PHY registers. If it was already |
| 348 | * disabled, cancel the timer that is going to re-enable |
| 349 | * it. |
| 350 | */ |
| 351 | if (!ps->ppu_disabled) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 352 | ret = mv88e6xxx_ppu_disable(ps); |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 353 | if (ret < 0) { |
| 354 | mutex_unlock(&ps->ppu_mutex); |
| 355 | return ret; |
| 356 | } |
| 357 | ps->ppu_disabled = 1; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 358 | } else { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 359 | del_timer(&ps->ppu_timer); |
| 360 | ret = 0; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | return ret; |
| 364 | } |
| 365 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 366 | static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_priv_state *ps) |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 367 | { |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 368 | /* Schedule a timer to re-enable the PHY polling unit. */ |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 369 | mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10)); |
| 370 | mutex_unlock(&ps->ppu_mutex); |
| 371 | } |
| 372 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 373 | void mv88e6xxx_ppu_state_init(struct mv88e6xxx_priv_state *ps) |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 374 | { |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 375 | mutex_init(&ps->ppu_mutex); |
| 376 | INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work); |
| 377 | init_timer(&ps->ppu_timer); |
| 378 | ps->ppu_timer.data = (unsigned long)ps; |
| 379 | ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer; |
| 380 | } |
| 381 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 382 | static int mv88e6xxx_mdio_read_ppu(struct mv88e6xxx_priv_state *ps, int addr, |
| 383 | int regnum) |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 384 | { |
| 385 | int ret; |
| 386 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 387 | ret = mv88e6xxx_ppu_access_get(ps); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 388 | if (ret >= 0) { |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 389 | ret = _mv88e6xxx_reg_read(ps, addr, regnum); |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 390 | mv88e6xxx_ppu_access_put(ps); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | return ret; |
| 394 | } |
| 395 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 396 | static int mv88e6xxx_mdio_write_ppu(struct mv88e6xxx_priv_state *ps, int addr, |
| 397 | int regnum, u16 val) |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 398 | { |
| 399 | int ret; |
| 400 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 401 | ret = mv88e6xxx_ppu_access_get(ps); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 402 | if (ret >= 0) { |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 403 | ret = _mv88e6xxx_reg_write(ps, addr, regnum, val); |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 404 | mv88e6xxx_ppu_access_put(ps); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | return ret; |
| 408 | } |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 409 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 410 | static bool mv88e6xxx_6065_family(struct mv88e6xxx_priv_state *ps) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 411 | { |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 412 | return ps->info->family == MV88E6XXX_FAMILY_6065; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 413 | } |
| 414 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 415 | static bool mv88e6xxx_6095_family(struct mv88e6xxx_priv_state *ps) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 416 | { |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 417 | return ps->info->family == MV88E6XXX_FAMILY_6095; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 418 | } |
| 419 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 420 | static bool mv88e6xxx_6097_family(struct mv88e6xxx_priv_state *ps) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 421 | { |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 422 | return ps->info->family == MV88E6XXX_FAMILY_6097; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 423 | } |
| 424 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 425 | static bool mv88e6xxx_6165_family(struct mv88e6xxx_priv_state *ps) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 426 | { |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 427 | return ps->info->family == MV88E6XXX_FAMILY_6165; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 428 | } |
| 429 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 430 | static bool mv88e6xxx_6185_family(struct mv88e6xxx_priv_state *ps) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 431 | { |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 432 | return ps->info->family == MV88E6XXX_FAMILY_6185; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 433 | } |
| 434 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 435 | static bool mv88e6xxx_6320_family(struct mv88e6xxx_priv_state *ps) |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 436 | { |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 437 | return ps->info->family == MV88E6XXX_FAMILY_6320; |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 440 | static bool mv88e6xxx_6351_family(struct mv88e6xxx_priv_state *ps) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 441 | { |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 442 | return ps->info->family == MV88E6XXX_FAMILY_6351; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 443 | } |
| 444 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 445 | static bool mv88e6xxx_6352_family(struct mv88e6xxx_priv_state *ps) |
Andrew Lunn | f3a8b6b | 2015-04-02 04:06:40 +0200 | [diff] [blame] | 446 | { |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 447 | return ps->info->family == MV88E6XXX_FAMILY_6352; |
Andrew Lunn | f3a8b6b | 2015-04-02 04:06:40 +0200 | [diff] [blame] | 448 | } |
| 449 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 450 | static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_priv_state *ps) |
Vivien Didelot | f74df0b | 2016-03-31 16:53:43 -0400 | [diff] [blame] | 451 | { |
Vivien Didelot | cd5a2c8 | 2016-04-17 13:24:02 -0400 | [diff] [blame] | 452 | return ps->info->num_databases; |
Vivien Didelot | f74df0b | 2016-03-31 16:53:43 -0400 | [diff] [blame] | 453 | } |
| 454 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 455 | static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_priv_state *ps) |
Vivien Didelot | b426e5f | 2016-03-31 16:53:42 -0400 | [diff] [blame] | 456 | { |
| 457 | /* Does the device have dedicated FID registers for ATU and VTU ops? */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 458 | if (mv88e6xxx_6097_family(ps) || mv88e6xxx_6165_family(ps) || |
| 459 | mv88e6xxx_6351_family(ps) || mv88e6xxx_6352_family(ps)) |
Vivien Didelot | b426e5f | 2016-03-31 16:53:42 -0400 | [diff] [blame] | 460 | return true; |
| 461 | |
| 462 | return false; |
| 463 | } |
| 464 | |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 465 | /* We expect the switch to perform auto negotiation if there is a real |
| 466 | * phy. However, in the case of a fixed link phy, we force the port |
| 467 | * settings from the fixed link settings. |
| 468 | */ |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 469 | static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port, |
| 470 | struct phy_device *phydev) |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 471 | { |
| 472 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Andrew Lunn | 4905287 | 2015-09-29 01:53:48 +0200 | [diff] [blame] | 473 | u32 reg; |
| 474 | int ret; |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 475 | |
| 476 | if (!phy_is_pseudo_fixed_link(phydev)) |
| 477 | return; |
| 478 | |
| 479 | mutex_lock(&ps->smi_mutex); |
| 480 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 481 | ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_PCS_CTRL); |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 482 | if (ret < 0) |
| 483 | goto out; |
| 484 | |
| 485 | reg = ret & ~(PORT_PCS_CTRL_LINK_UP | |
| 486 | PORT_PCS_CTRL_FORCE_LINK | |
| 487 | PORT_PCS_CTRL_DUPLEX_FULL | |
| 488 | PORT_PCS_CTRL_FORCE_DUPLEX | |
| 489 | PORT_PCS_CTRL_UNFORCED); |
| 490 | |
| 491 | reg |= PORT_PCS_CTRL_FORCE_LINK; |
| 492 | if (phydev->link) |
| 493 | reg |= PORT_PCS_CTRL_LINK_UP; |
| 494 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 495 | if (mv88e6xxx_6065_family(ps) && phydev->speed > SPEED_100) |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 496 | goto out; |
| 497 | |
| 498 | switch (phydev->speed) { |
| 499 | case SPEED_1000: |
| 500 | reg |= PORT_PCS_CTRL_1000; |
| 501 | break; |
| 502 | case SPEED_100: |
| 503 | reg |= PORT_PCS_CTRL_100; |
| 504 | break; |
| 505 | case SPEED_10: |
| 506 | reg |= PORT_PCS_CTRL_10; |
| 507 | break; |
| 508 | default: |
| 509 | pr_info("Unknown speed"); |
| 510 | goto out; |
| 511 | } |
| 512 | |
| 513 | reg |= PORT_PCS_CTRL_FORCE_DUPLEX; |
| 514 | if (phydev->duplex == DUPLEX_FULL) |
| 515 | reg |= PORT_PCS_CTRL_DUPLEX_FULL; |
| 516 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 517 | if ((mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps)) && |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 518 | (port >= ps->info->num_ports - 2)) { |
Andrew Lunn | e7e72ac | 2015-08-31 15:56:51 +0200 | [diff] [blame] | 519 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) |
| 520 | reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK; |
| 521 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) |
| 522 | reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK; |
| 523 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) |
| 524 | reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK | |
| 525 | PORT_PCS_CTRL_RGMII_DELAY_TXCLK); |
| 526 | } |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 527 | _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_PCS_CTRL, reg); |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 528 | |
| 529 | out: |
| 530 | mutex_unlock(&ps->smi_mutex); |
| 531 | } |
| 532 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 533 | static int _mv88e6xxx_stats_wait(struct mv88e6xxx_priv_state *ps) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 534 | { |
| 535 | int ret; |
| 536 | int i; |
| 537 | |
| 538 | for (i = 0; i < 10; i++) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 539 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_OP); |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 540 | if ((ret & GLOBAL_STATS_OP_BUSY) == 0) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | return -ETIMEDOUT; |
| 545 | } |
| 546 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 547 | static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_priv_state *ps, |
| 548 | int port) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 549 | { |
| 550 | int ret; |
| 551 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 552 | if (mv88e6xxx_6320_family(ps) || mv88e6xxx_6352_family(ps)) |
Andrew Lunn | f3a8b6b | 2015-04-02 04:06:40 +0200 | [diff] [blame] | 553 | port = (port + 1) << 5; |
| 554 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 555 | /* Snapshot the hardware statistics counters for this port. */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 556 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP, |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 557 | GLOBAL_STATS_OP_CAPTURE_PORT | |
| 558 | GLOBAL_STATS_OP_HIST_RX_TX | port); |
| 559 | if (ret < 0) |
| 560 | return ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 561 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 562 | /* Wait for the snapshotting to complete. */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 563 | ret = _mv88e6xxx_stats_wait(ps); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 564 | if (ret < 0) |
| 565 | return ret; |
| 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 570 | static void _mv88e6xxx_stats_read(struct mv88e6xxx_priv_state *ps, |
| 571 | int stat, u32 *val) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 572 | { |
| 573 | u32 _val; |
| 574 | int ret; |
| 575 | |
| 576 | *val = 0; |
| 577 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 578 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP, |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 579 | GLOBAL_STATS_OP_READ_CAPTURED | |
| 580 | GLOBAL_STATS_OP_HIST_RX_TX | stat); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 581 | if (ret < 0) |
| 582 | return; |
| 583 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 584 | ret = _mv88e6xxx_stats_wait(ps); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 585 | if (ret < 0) |
| 586 | return; |
| 587 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 588 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_COUNTER_32); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 589 | if (ret < 0) |
| 590 | return; |
| 591 | |
| 592 | _val = ret << 16; |
| 593 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 594 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_COUNTER_01); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 595 | if (ret < 0) |
| 596 | return; |
| 597 | |
| 598 | *val = _val | ret; |
| 599 | } |
| 600 | |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 601 | static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = { |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 602 | { "in_good_octets", 8, 0x00, BANK0, }, |
| 603 | { "in_bad_octets", 4, 0x02, BANK0, }, |
| 604 | { "in_unicast", 4, 0x04, BANK0, }, |
| 605 | { "in_broadcasts", 4, 0x06, BANK0, }, |
| 606 | { "in_multicasts", 4, 0x07, BANK0, }, |
| 607 | { "in_pause", 4, 0x16, BANK0, }, |
| 608 | { "in_undersize", 4, 0x18, BANK0, }, |
| 609 | { "in_fragments", 4, 0x19, BANK0, }, |
| 610 | { "in_oversize", 4, 0x1a, BANK0, }, |
| 611 | { "in_jabber", 4, 0x1b, BANK0, }, |
| 612 | { "in_rx_error", 4, 0x1c, BANK0, }, |
| 613 | { "in_fcs_error", 4, 0x1d, BANK0, }, |
| 614 | { "out_octets", 8, 0x0e, BANK0, }, |
| 615 | { "out_unicast", 4, 0x10, BANK0, }, |
| 616 | { "out_broadcasts", 4, 0x13, BANK0, }, |
| 617 | { "out_multicasts", 4, 0x12, BANK0, }, |
| 618 | { "out_pause", 4, 0x15, BANK0, }, |
| 619 | { "excessive", 4, 0x11, BANK0, }, |
| 620 | { "collisions", 4, 0x1e, BANK0, }, |
| 621 | { "deferred", 4, 0x05, BANK0, }, |
| 622 | { "single", 4, 0x14, BANK0, }, |
| 623 | { "multiple", 4, 0x17, BANK0, }, |
| 624 | { "out_fcs_error", 4, 0x03, BANK0, }, |
| 625 | { "late", 4, 0x1f, BANK0, }, |
| 626 | { "hist_64bytes", 4, 0x08, BANK0, }, |
| 627 | { "hist_65_127bytes", 4, 0x09, BANK0, }, |
| 628 | { "hist_128_255bytes", 4, 0x0a, BANK0, }, |
| 629 | { "hist_256_511bytes", 4, 0x0b, BANK0, }, |
| 630 | { "hist_512_1023bytes", 4, 0x0c, BANK0, }, |
| 631 | { "hist_1024_max_bytes", 4, 0x0d, BANK0, }, |
| 632 | { "sw_in_discards", 4, 0x10, PORT, }, |
| 633 | { "sw_in_filtered", 2, 0x12, PORT, }, |
| 634 | { "sw_out_filtered", 2, 0x13, PORT, }, |
| 635 | { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 636 | { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 637 | { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 638 | { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 639 | { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 640 | { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 641 | { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 642 | { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 643 | { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 644 | { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 645 | { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 646 | { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 647 | { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 648 | { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 649 | { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 650 | { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 651 | { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 652 | { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 653 | { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 654 | { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 655 | { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 656 | { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 657 | { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 658 | { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 659 | { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 660 | { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 661 | }; |
| 662 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 663 | static bool mv88e6xxx_has_stat(struct mv88e6xxx_priv_state *ps, |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 664 | struct mv88e6xxx_hw_stat *stat) |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 665 | { |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 666 | switch (stat->type) { |
| 667 | case BANK0: |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 668 | return true; |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 669 | case BANK1: |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 670 | return mv88e6xxx_6320_family(ps); |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 671 | case PORT: |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 672 | return mv88e6xxx_6095_family(ps) || |
| 673 | mv88e6xxx_6185_family(ps) || |
| 674 | mv88e6xxx_6097_family(ps) || |
| 675 | mv88e6xxx_6165_family(ps) || |
| 676 | mv88e6xxx_6351_family(ps) || |
| 677 | mv88e6xxx_6352_family(ps); |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 678 | } |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 679 | return false; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 682 | static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_priv_state *ps, |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 683 | struct mv88e6xxx_hw_stat *s, |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 684 | int port) |
| 685 | { |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 686 | u32 low; |
| 687 | u32 high = 0; |
| 688 | int ret; |
| 689 | u64 value; |
| 690 | |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 691 | switch (s->type) { |
| 692 | case PORT: |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 693 | ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), s->reg); |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 694 | if (ret < 0) |
| 695 | return UINT64_MAX; |
| 696 | |
| 697 | low = ret; |
| 698 | if (s->sizeof_stat == 4) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 699 | ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 700 | s->reg + 1); |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 701 | if (ret < 0) |
| 702 | return UINT64_MAX; |
| 703 | high = ret; |
| 704 | } |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 705 | break; |
| 706 | case BANK0: |
| 707 | case BANK1: |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 708 | _mv88e6xxx_stats_read(ps, s->reg, &low); |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 709 | if (s->sizeof_stat == 8) |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 710 | _mv88e6xxx_stats_read(ps, s->reg + 1, &high); |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 711 | } |
| 712 | value = (((u64)high) << 16) | low; |
| 713 | return value; |
| 714 | } |
| 715 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 716 | static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port, |
| 717 | uint8_t *data) |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 718 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 719 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 720 | struct mv88e6xxx_hw_stat *stat; |
| 721 | int i, j; |
| 722 | |
| 723 | for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) { |
| 724 | stat = &mv88e6xxx_hw_stats[i]; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 725 | if (mv88e6xxx_has_stat(ps, stat)) { |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 726 | memcpy(data + j * ETH_GSTRING_LEN, stat->string, |
| 727 | ETH_GSTRING_LEN); |
| 728 | j++; |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 733 | static int mv88e6xxx_get_sset_count(struct dsa_switch *ds) |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 734 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 735 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 736 | struct mv88e6xxx_hw_stat *stat; |
| 737 | int i, j; |
| 738 | |
| 739 | for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) { |
| 740 | stat = &mv88e6xxx_hw_stats[i]; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 741 | if (mv88e6xxx_has_stat(ps, stat)) |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 742 | j++; |
| 743 | } |
| 744 | return j; |
| 745 | } |
| 746 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 747 | static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port, |
| 748 | uint64_t *data) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 749 | { |
Florian Fainelli | a22adce | 2014-04-28 11:14:28 -0700 | [diff] [blame] | 750 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 751 | struct mv88e6xxx_hw_stat *stat; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 752 | int ret; |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 753 | int i, j; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 754 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 755 | mutex_lock(&ps->smi_mutex); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 756 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 757 | ret = _mv88e6xxx_stats_snapshot(ps, port); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 758 | if (ret < 0) { |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 759 | mutex_unlock(&ps->smi_mutex); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 760 | return; |
| 761 | } |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 762 | for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) { |
| 763 | stat = &mv88e6xxx_hw_stats[i]; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 764 | if (mv88e6xxx_has_stat(ps, stat)) { |
| 765 | data[j] = _mv88e6xxx_get_ethtool_stat(ps, stat, port); |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 766 | j++; |
| 767 | } |
| 768 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 769 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 770 | mutex_unlock(&ps->smi_mutex); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 771 | } |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 772 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 773 | static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port) |
Guenter Roeck | a1ab91f | 2014-10-29 10:45:05 -0700 | [diff] [blame] | 774 | { |
| 775 | return 32 * sizeof(u16); |
| 776 | } |
| 777 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 778 | static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port, |
| 779 | struct ethtool_regs *regs, void *_p) |
Guenter Roeck | a1ab91f | 2014-10-29 10:45:05 -0700 | [diff] [blame] | 780 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 781 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Guenter Roeck | a1ab91f | 2014-10-29 10:45:05 -0700 | [diff] [blame] | 782 | u16 *p = _p; |
| 783 | int i; |
| 784 | |
| 785 | regs->version = 0; |
| 786 | |
| 787 | memset(p, 0xff, 32 * sizeof(u16)); |
| 788 | |
Vivien Didelot | 2306251 | 2016-05-09 13:22:45 -0400 | [diff] [blame] | 789 | mutex_lock(&ps->smi_mutex); |
| 790 | |
Guenter Roeck | a1ab91f | 2014-10-29 10:45:05 -0700 | [diff] [blame] | 791 | for (i = 0; i < 32; i++) { |
| 792 | int ret; |
| 793 | |
Vivien Didelot | 2306251 | 2016-05-09 13:22:45 -0400 | [diff] [blame] | 794 | ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), i); |
Guenter Roeck | a1ab91f | 2014-10-29 10:45:05 -0700 | [diff] [blame] | 795 | if (ret >= 0) |
| 796 | p[i] = ret; |
| 797 | } |
Vivien Didelot | 2306251 | 2016-05-09 13:22:45 -0400 | [diff] [blame] | 798 | |
| 799 | mutex_unlock(&ps->smi_mutex); |
Guenter Roeck | a1ab91f | 2014-10-29 10:45:05 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 802 | static int _mv88e6xxx_wait(struct mv88e6xxx_priv_state *ps, int reg, int offset, |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 803 | u16 mask) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 804 | { |
| 805 | unsigned long timeout = jiffies + HZ / 10; |
| 806 | |
| 807 | while (time_before(jiffies, timeout)) { |
| 808 | int ret; |
| 809 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 810 | ret = _mv88e6xxx_reg_read(ps, reg, offset); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 811 | if (ret < 0) |
| 812 | return ret; |
| 813 | if (!(ret & mask)) |
| 814 | return 0; |
| 815 | |
| 816 | usleep_range(1000, 2000); |
| 817 | } |
| 818 | return -ETIMEDOUT; |
| 819 | } |
| 820 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 821 | static int mv88e6xxx_wait(struct mv88e6xxx_priv_state *ps, int reg, |
| 822 | int offset, u16 mask) |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 823 | { |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 824 | int ret; |
| 825 | |
| 826 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 827 | ret = _mv88e6xxx_wait(ps, reg, offset, mask); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 828 | mutex_unlock(&ps->smi_mutex); |
| 829 | |
| 830 | return ret; |
| 831 | } |
| 832 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 833 | static int mv88e6xxx_mdio_wait(struct mv88e6xxx_priv_state *ps) |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 834 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 835 | return _mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_SMI_OP, |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 836 | GLOBAL2_SMI_OP_BUSY); |
| 837 | } |
| 838 | |
Vivien Didelot | d24645b | 2016-05-09 13:22:41 -0400 | [diff] [blame] | 839 | static int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds) |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 840 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 841 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 842 | |
| 843 | return mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP, |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 844 | GLOBAL2_EEPROM_OP_LOAD); |
| 845 | } |
| 846 | |
Vivien Didelot | d24645b | 2016-05-09 13:22:41 -0400 | [diff] [blame] | 847 | static int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds) |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 848 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 849 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 850 | |
| 851 | return mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP, |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 852 | GLOBAL2_EEPROM_OP_BUSY); |
| 853 | } |
| 854 | |
Vivien Didelot | d24645b | 2016-05-09 13:22:41 -0400 | [diff] [blame] | 855 | static int mv88e6xxx_read_eeprom_word(struct dsa_switch *ds, int addr) |
| 856 | { |
| 857 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 858 | int ret; |
| 859 | |
| 860 | mutex_lock(&ps->eeprom_mutex); |
| 861 | |
| 862 | ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP, |
| 863 | GLOBAL2_EEPROM_OP_READ | |
| 864 | (addr & GLOBAL2_EEPROM_OP_ADDR_MASK)); |
| 865 | if (ret < 0) |
| 866 | goto error; |
| 867 | |
| 868 | ret = mv88e6xxx_eeprom_busy_wait(ds); |
| 869 | if (ret < 0) |
| 870 | goto error; |
| 871 | |
| 872 | ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_EEPROM_DATA); |
| 873 | error: |
| 874 | mutex_unlock(&ps->eeprom_mutex); |
| 875 | return ret; |
| 876 | } |
| 877 | |
Andrew Lunn | f8cd875 | 2016-05-10 23:27:25 +0200 | [diff] [blame] | 878 | static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds) |
| 879 | { |
| 880 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 881 | |
| 882 | if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM)) |
| 883 | return ps->eeprom_len; |
| 884 | |
| 885 | return 0; |
| 886 | } |
| 887 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 888 | static int mv88e6xxx_get_eeprom(struct dsa_switch *ds, |
| 889 | struct ethtool_eeprom *eeprom, u8 *data) |
Vivien Didelot | d24645b | 2016-05-09 13:22:41 -0400 | [diff] [blame] | 890 | { |
| 891 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 892 | int offset; |
| 893 | int len; |
| 894 | int ret; |
| 895 | |
| 896 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM)) |
| 897 | return -EOPNOTSUPP; |
| 898 | |
| 899 | offset = eeprom->offset; |
| 900 | len = eeprom->len; |
| 901 | eeprom->len = 0; |
| 902 | |
| 903 | eeprom->magic = 0xc3ec4951; |
| 904 | |
| 905 | ret = mv88e6xxx_eeprom_load_wait(ds); |
| 906 | if (ret < 0) |
| 907 | return ret; |
| 908 | |
| 909 | if (offset & 1) { |
| 910 | int word; |
| 911 | |
| 912 | word = mv88e6xxx_read_eeprom_word(ds, offset >> 1); |
| 913 | if (word < 0) |
| 914 | return word; |
| 915 | |
| 916 | *data++ = (word >> 8) & 0xff; |
| 917 | |
| 918 | offset++; |
| 919 | len--; |
| 920 | eeprom->len++; |
| 921 | } |
| 922 | |
| 923 | while (len >= 2) { |
| 924 | int word; |
| 925 | |
| 926 | word = mv88e6xxx_read_eeprom_word(ds, offset >> 1); |
| 927 | if (word < 0) |
| 928 | return word; |
| 929 | |
| 930 | *data++ = word & 0xff; |
| 931 | *data++ = (word >> 8) & 0xff; |
| 932 | |
| 933 | offset += 2; |
| 934 | len -= 2; |
| 935 | eeprom->len += 2; |
| 936 | } |
| 937 | |
| 938 | if (len) { |
| 939 | int word; |
| 940 | |
| 941 | word = mv88e6xxx_read_eeprom_word(ds, offset >> 1); |
| 942 | if (word < 0) |
| 943 | return word; |
| 944 | |
| 945 | *data++ = word & 0xff; |
| 946 | |
| 947 | offset++; |
| 948 | len--; |
| 949 | eeprom->len++; |
| 950 | } |
| 951 | |
| 952 | return 0; |
| 953 | } |
| 954 | |
| 955 | static int mv88e6xxx_eeprom_is_readonly(struct dsa_switch *ds) |
| 956 | { |
| 957 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 958 | int ret; |
| 959 | |
| 960 | ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP); |
| 961 | if (ret < 0) |
| 962 | return ret; |
| 963 | |
| 964 | if (!(ret & GLOBAL2_EEPROM_OP_WRITE_EN)) |
| 965 | return -EROFS; |
| 966 | |
| 967 | return 0; |
| 968 | } |
| 969 | |
| 970 | static int mv88e6xxx_write_eeprom_word(struct dsa_switch *ds, int addr, |
| 971 | u16 data) |
| 972 | { |
| 973 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 974 | int ret; |
| 975 | |
| 976 | mutex_lock(&ps->eeprom_mutex); |
| 977 | |
| 978 | ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data); |
| 979 | if (ret < 0) |
| 980 | goto error; |
| 981 | |
| 982 | ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP, |
| 983 | GLOBAL2_EEPROM_OP_WRITE | |
| 984 | (addr & GLOBAL2_EEPROM_OP_ADDR_MASK)); |
| 985 | if (ret < 0) |
| 986 | goto error; |
| 987 | |
| 988 | ret = mv88e6xxx_eeprom_busy_wait(ds); |
| 989 | error: |
| 990 | mutex_unlock(&ps->eeprom_mutex); |
| 991 | return ret; |
| 992 | } |
| 993 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 994 | static int mv88e6xxx_set_eeprom(struct dsa_switch *ds, |
| 995 | struct ethtool_eeprom *eeprom, u8 *data) |
Vivien Didelot | d24645b | 2016-05-09 13:22:41 -0400 | [diff] [blame] | 996 | { |
| 997 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 998 | int offset; |
| 999 | int ret; |
| 1000 | int len; |
| 1001 | |
| 1002 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM)) |
| 1003 | return -EOPNOTSUPP; |
| 1004 | |
| 1005 | if (eeprom->magic != 0xc3ec4951) |
| 1006 | return -EINVAL; |
| 1007 | |
| 1008 | ret = mv88e6xxx_eeprom_is_readonly(ds); |
| 1009 | if (ret) |
| 1010 | return ret; |
| 1011 | |
| 1012 | offset = eeprom->offset; |
| 1013 | len = eeprom->len; |
| 1014 | eeprom->len = 0; |
| 1015 | |
| 1016 | ret = mv88e6xxx_eeprom_load_wait(ds); |
| 1017 | if (ret < 0) |
| 1018 | return ret; |
| 1019 | |
| 1020 | if (offset & 1) { |
| 1021 | int word; |
| 1022 | |
| 1023 | word = mv88e6xxx_read_eeprom_word(ds, offset >> 1); |
| 1024 | if (word < 0) |
| 1025 | return word; |
| 1026 | |
| 1027 | word = (*data++ << 8) | (word & 0xff); |
| 1028 | |
| 1029 | ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word); |
| 1030 | if (ret < 0) |
| 1031 | return ret; |
| 1032 | |
| 1033 | offset++; |
| 1034 | len--; |
| 1035 | eeprom->len++; |
| 1036 | } |
| 1037 | |
| 1038 | while (len >= 2) { |
| 1039 | int word; |
| 1040 | |
| 1041 | word = *data++; |
| 1042 | word |= *data++ << 8; |
| 1043 | |
| 1044 | ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word); |
| 1045 | if (ret < 0) |
| 1046 | return ret; |
| 1047 | |
| 1048 | offset += 2; |
| 1049 | len -= 2; |
| 1050 | eeprom->len += 2; |
| 1051 | } |
| 1052 | |
| 1053 | if (len) { |
| 1054 | int word; |
| 1055 | |
| 1056 | word = mv88e6xxx_read_eeprom_word(ds, offset >> 1); |
| 1057 | if (word < 0) |
| 1058 | return word; |
| 1059 | |
| 1060 | word = (word & 0xff00) | *data++; |
| 1061 | |
| 1062 | ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word); |
| 1063 | if (ret < 0) |
| 1064 | return ret; |
| 1065 | |
| 1066 | offset++; |
| 1067 | len--; |
| 1068 | eeprom->len++; |
| 1069 | } |
| 1070 | |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1074 | static int _mv88e6xxx_atu_wait(struct mv88e6xxx_priv_state *ps) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1075 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1076 | return _mv88e6xxx_wait(ps, REG_GLOBAL, GLOBAL_ATU_OP, |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1077 | GLOBAL_ATU_OP_BUSY); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 1080 | static int mv88e6xxx_mdio_read_indirect(struct mv88e6xxx_priv_state *ps, |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1081 | int addr, int regnum) |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 1082 | { |
| 1083 | int ret; |
| 1084 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1085 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_OP, |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1086 | GLOBAL2_SMI_OP_22_READ | (addr << 5) | |
| 1087 | regnum); |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 1088 | if (ret < 0) |
| 1089 | return ret; |
| 1090 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 1091 | ret = mv88e6xxx_mdio_wait(ps); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1092 | if (ret < 0) |
| 1093 | return ret; |
| 1094 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1095 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_SMI_DATA); |
| 1096 | |
| 1097 | return ret; |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 1098 | } |
| 1099 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 1100 | static int mv88e6xxx_mdio_write_indirect(struct mv88e6xxx_priv_state *ps, |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1101 | int addr, int regnum, u16 val) |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 1102 | { |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1103 | int ret; |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 1104 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1105 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_DATA, val); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1106 | if (ret < 0) |
| 1107 | return ret; |
| 1108 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1109 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_OP, |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1110 | GLOBAL2_SMI_OP_22_WRITE | (addr << 5) | |
| 1111 | regnum); |
| 1112 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 1113 | return mv88e6xxx_mdio_wait(ps); |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 1114 | } |
| 1115 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 1116 | static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port, |
| 1117 | struct ethtool_eee *e) |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1118 | { |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1119 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1120 | int reg; |
| 1121 | |
Vivien Didelot | aadbdb8 | 2016-05-09 13:22:44 -0400 | [diff] [blame] | 1122 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEE)) |
| 1123 | return -EOPNOTSUPP; |
| 1124 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1125 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1126 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 1127 | reg = mv88e6xxx_mdio_read_indirect(ps, port, 16); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1128 | if (reg < 0) |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1129 | goto out; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1130 | |
| 1131 | e->eee_enabled = !!(reg & 0x0200); |
| 1132 | e->tx_lpi_enabled = !!(reg & 0x0100); |
| 1133 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1134 | reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_STATUS); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1135 | if (reg < 0) |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1136 | goto out; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1137 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1138 | e->eee_active = !!(reg & PORT_STATUS_EEE); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1139 | reg = 0; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1140 | |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1141 | out: |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1142 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1143 | return reg; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1144 | } |
| 1145 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 1146 | static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port, |
| 1147 | struct phy_device *phydev, struct ethtool_eee *e) |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1148 | { |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1149 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1150 | int reg; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1151 | int ret; |
| 1152 | |
Vivien Didelot | aadbdb8 | 2016-05-09 13:22:44 -0400 | [diff] [blame] | 1153 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEE)) |
| 1154 | return -EOPNOTSUPP; |
| 1155 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1156 | mutex_lock(&ps->smi_mutex); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1157 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 1158 | ret = mv88e6xxx_mdio_read_indirect(ps, port, 16); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1159 | if (ret < 0) |
| 1160 | goto out; |
| 1161 | |
| 1162 | reg = ret & ~0x0300; |
| 1163 | if (e->eee_enabled) |
| 1164 | reg |= 0x0200; |
| 1165 | if (e->tx_lpi_enabled) |
| 1166 | reg |= 0x0100; |
| 1167 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 1168 | ret = mv88e6xxx_mdio_write_indirect(ps, port, 16, reg); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1169 | out: |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1170 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1171 | |
| 1172 | return ret; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1173 | } |
| 1174 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1175 | static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_priv_state *ps, u16 fid, u16 cmd) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1176 | { |
| 1177 | int ret; |
| 1178 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1179 | if (mv88e6xxx_has_fid_reg(ps)) { |
| 1180 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_FID, fid); |
Vivien Didelot | b426e5f | 2016-03-31 16:53:42 -0400 | [diff] [blame] | 1181 | if (ret < 0) |
| 1182 | return ret; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1183 | } else if (mv88e6xxx_num_databases(ps) == 256) { |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 1184 | /* ATU DBNum[7:4] are located in ATU Control 15:12 */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1185 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL); |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 1186 | if (ret < 0) |
| 1187 | return ret; |
| 1188 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1189 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL, |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 1190 | (ret & 0xfff) | |
| 1191 | ((fid << 8) & 0xf000)); |
| 1192 | if (ret < 0) |
| 1193 | return ret; |
| 1194 | |
| 1195 | /* ATU DBNum[3:0] are located in ATU Operation 3:0 */ |
| 1196 | cmd |= fid & 0xf; |
Vivien Didelot | b426e5f | 2016-03-31 16:53:42 -0400 | [diff] [blame] | 1197 | } |
| 1198 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1199 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_OP, cmd); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1200 | if (ret < 0) |
| 1201 | return ret; |
| 1202 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1203 | return _mv88e6xxx_atu_wait(ps); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1206 | static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_priv_state *ps, |
Vivien Didelot | 37705b7 | 2015-09-04 14:34:11 -0400 | [diff] [blame] | 1207 | struct mv88e6xxx_atu_entry *entry) |
| 1208 | { |
| 1209 | u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK; |
| 1210 | |
| 1211 | if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) { |
| 1212 | unsigned int mask, shift; |
| 1213 | |
| 1214 | if (entry->trunk) { |
| 1215 | data |= GLOBAL_ATU_DATA_TRUNK; |
| 1216 | mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK; |
| 1217 | shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT; |
| 1218 | } else { |
| 1219 | mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK; |
| 1220 | shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT; |
| 1221 | } |
| 1222 | |
| 1223 | data |= (entry->portv_trunkid << shift) & mask; |
| 1224 | } |
| 1225 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1226 | return _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_DATA, data); |
Vivien Didelot | 37705b7 | 2015-09-04 14:34:11 -0400 | [diff] [blame] | 1227 | } |
| 1228 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1229 | static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_priv_state *ps, |
Vivien Didelot | 7fb5e75 | 2015-09-04 14:34:12 -0400 | [diff] [blame] | 1230 | struct mv88e6xxx_atu_entry *entry, |
| 1231 | bool static_too) |
| 1232 | { |
| 1233 | int op; |
| 1234 | int err; |
| 1235 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1236 | err = _mv88e6xxx_atu_wait(ps); |
Vivien Didelot | 7fb5e75 | 2015-09-04 14:34:12 -0400 | [diff] [blame] | 1237 | if (err) |
| 1238 | return err; |
| 1239 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1240 | err = _mv88e6xxx_atu_data_write(ps, entry); |
Vivien Didelot | 7fb5e75 | 2015-09-04 14:34:12 -0400 | [diff] [blame] | 1241 | if (err) |
| 1242 | return err; |
| 1243 | |
| 1244 | if (entry->fid) { |
Vivien Didelot | 7fb5e75 | 2015-09-04 14:34:12 -0400 | [diff] [blame] | 1245 | op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB : |
| 1246 | GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB; |
| 1247 | } else { |
| 1248 | op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL : |
| 1249 | GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC; |
| 1250 | } |
| 1251 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1252 | return _mv88e6xxx_atu_cmd(ps, entry->fid, op); |
Vivien Didelot | 7fb5e75 | 2015-09-04 14:34:12 -0400 | [diff] [blame] | 1253 | } |
| 1254 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1255 | static int _mv88e6xxx_atu_flush(struct mv88e6xxx_priv_state *ps, |
| 1256 | u16 fid, bool static_too) |
Vivien Didelot | 7fb5e75 | 2015-09-04 14:34:12 -0400 | [diff] [blame] | 1257 | { |
| 1258 | struct mv88e6xxx_atu_entry entry = { |
| 1259 | .fid = fid, |
| 1260 | .state = 0, /* EntryState bits must be 0 */ |
| 1261 | }; |
| 1262 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1263 | return _mv88e6xxx_atu_flush_move(ps, &entry, static_too); |
Vivien Didelot | 7fb5e75 | 2015-09-04 14:34:12 -0400 | [diff] [blame] | 1264 | } |
| 1265 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1266 | static int _mv88e6xxx_atu_move(struct mv88e6xxx_priv_state *ps, u16 fid, |
| 1267 | int from_port, int to_port, bool static_too) |
Vivien Didelot | 9f4d55d | 2015-09-04 14:34:15 -0400 | [diff] [blame] | 1268 | { |
| 1269 | struct mv88e6xxx_atu_entry entry = { |
| 1270 | .trunk = false, |
| 1271 | .fid = fid, |
| 1272 | }; |
| 1273 | |
| 1274 | /* EntryState bits must be 0xF */ |
| 1275 | entry.state = GLOBAL_ATU_DATA_STATE_MASK; |
| 1276 | |
| 1277 | /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */ |
| 1278 | entry.portv_trunkid = (to_port & 0x0f) << 4; |
| 1279 | entry.portv_trunkid |= from_port & 0x0f; |
| 1280 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1281 | return _mv88e6xxx_atu_flush_move(ps, &entry, static_too); |
Vivien Didelot | 9f4d55d | 2015-09-04 14:34:15 -0400 | [diff] [blame] | 1282 | } |
| 1283 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1284 | static int _mv88e6xxx_atu_remove(struct mv88e6xxx_priv_state *ps, u16 fid, |
| 1285 | int port, bool static_too) |
Vivien Didelot | 9f4d55d | 2015-09-04 14:34:15 -0400 | [diff] [blame] | 1286 | { |
| 1287 | /* Destination port 0xF means remove the entries */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1288 | return _mv88e6xxx_atu_move(ps, fid, port, 0x0f, static_too); |
Vivien Didelot | 9f4d55d | 2015-09-04 14:34:15 -0400 | [diff] [blame] | 1289 | } |
| 1290 | |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1291 | static const char * const mv88e6xxx_port_state_names[] = { |
| 1292 | [PORT_CONTROL_STATE_DISABLED] = "Disabled", |
| 1293 | [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening", |
| 1294 | [PORT_CONTROL_STATE_LEARNING] = "Learning", |
| 1295 | [PORT_CONTROL_STATE_FORWARDING] = "Forwarding", |
| 1296 | }; |
| 1297 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1298 | static int _mv88e6xxx_port_state(struct mv88e6xxx_priv_state *ps, int port, |
| 1299 | u8 state) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1300 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1301 | struct dsa_switch *ds = ps->ds; |
Geert Uytterhoeven | c3ffe6d | 2015-04-16 20:49:14 +0200 | [diff] [blame] | 1302 | int reg, ret = 0; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1303 | u8 oldstate; |
| 1304 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1305 | reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL); |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1306 | if (reg < 0) |
| 1307 | return reg; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1308 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1309 | oldstate = reg & PORT_CONTROL_STATE_MASK; |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1310 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1311 | if (oldstate != state) { |
| 1312 | /* Flush forwarding database if we're moving a port |
| 1313 | * from Learning or Forwarding state to Disabled or |
| 1314 | * Blocking or Listening state. |
| 1315 | */ |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1316 | if ((oldstate == PORT_CONTROL_STATE_LEARNING || |
| 1317 | oldstate == PORT_CONTROL_STATE_FORWARDING) |
| 1318 | && (state == PORT_CONTROL_STATE_DISABLED || |
| 1319 | state == PORT_CONTROL_STATE_BLOCKING)) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1320 | ret = _mv88e6xxx_atu_remove(ps, 0, port, false); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1321 | if (ret) |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1322 | return ret; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1323 | } |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1324 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1325 | reg = (reg & ~PORT_CONTROL_STATE_MASK) | state; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1326 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL, |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1327 | reg); |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1328 | if (ret) |
| 1329 | return ret; |
| 1330 | |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 1331 | netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n", |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1332 | mv88e6xxx_port_state_names[state], |
| 1333 | mv88e6xxx_port_state_names[oldstate]); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1336 | return ret; |
| 1337 | } |
| 1338 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1339 | static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_priv_state *ps, |
| 1340 | int port) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1341 | { |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 1342 | struct net_device *bridge = ps->ports[port].bridge_dev; |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 1343 | const u16 mask = (1 << ps->info->num_ports) - 1; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1344 | struct dsa_switch *ds = ps->ds; |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 1345 | u16 output_ports = 0; |
Vivien Didelot | ede8098 | 2015-10-11 18:08:35 -0400 | [diff] [blame] | 1346 | int reg; |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 1347 | int i; |
| 1348 | |
| 1349 | /* allow CPU port or DSA link(s) to send frames to every port */ |
| 1350 | if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) { |
| 1351 | output_ports = mask; |
| 1352 | } else { |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 1353 | for (i = 0; i < ps->info->num_ports; ++i) { |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 1354 | /* allow sending frames to every group member */ |
| 1355 | if (bridge && ps->ports[i].bridge_dev == bridge) |
| 1356 | output_ports |= BIT(i); |
| 1357 | |
| 1358 | /* allow sending frames to CPU port and DSA link(s) */ |
| 1359 | if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)) |
| 1360 | output_ports |= BIT(i); |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | /* prevent frames from going back out of the port they came in on */ |
| 1365 | output_ports &= ~BIT(port); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1366 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1367 | reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_BASE_VLAN); |
Vivien Didelot | ede8098 | 2015-10-11 18:08:35 -0400 | [diff] [blame] | 1368 | if (reg < 0) |
| 1369 | return reg; |
| 1370 | |
| 1371 | reg &= ~mask; |
| 1372 | reg |= output_ports & mask; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1373 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1374 | return _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_BASE_VLAN, reg); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1375 | } |
| 1376 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 1377 | static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port, |
| 1378 | u8 state) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1379 | { |
| 1380 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1381 | int stp_state; |
Vivien Didelot | 553eb54 | 2016-05-13 20:38:23 -0400 | [diff] [blame] | 1382 | int err; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1383 | |
Vivien Didelot | 936f234 | 2016-05-09 13:22:46 -0400 | [diff] [blame] | 1384 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_PORTSTATE)) |
| 1385 | return; |
| 1386 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1387 | switch (state) { |
| 1388 | case BR_STATE_DISABLED: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1389 | stp_state = PORT_CONTROL_STATE_DISABLED; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1390 | break; |
| 1391 | case BR_STATE_BLOCKING: |
| 1392 | case BR_STATE_LISTENING: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1393 | stp_state = PORT_CONTROL_STATE_BLOCKING; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1394 | break; |
| 1395 | case BR_STATE_LEARNING: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1396 | stp_state = PORT_CONTROL_STATE_LEARNING; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1397 | break; |
| 1398 | case BR_STATE_FORWARDING: |
| 1399 | default: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1400 | stp_state = PORT_CONTROL_STATE_FORWARDING; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1401 | break; |
| 1402 | } |
| 1403 | |
Vivien Didelot | 553eb54 | 2016-05-13 20:38:23 -0400 | [diff] [blame] | 1404 | mutex_lock(&ps->smi_mutex); |
| 1405 | err = _mv88e6xxx_port_state(ps, port, stp_state); |
| 1406 | mutex_unlock(&ps->smi_mutex); |
| 1407 | |
| 1408 | if (err) |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 1409 | netdev_err(ds->ports[port].netdev, |
| 1410 | "failed to update state to %s\n", |
Vivien Didelot | 553eb54 | 2016-05-13 20:38:23 -0400 | [diff] [blame] | 1411 | mv88e6xxx_port_state_names[stp_state]); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1412 | } |
| 1413 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1414 | static int _mv88e6xxx_port_pvid(struct mv88e6xxx_priv_state *ps, int port, |
| 1415 | u16 *new, u16 *old) |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1416 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1417 | struct dsa_switch *ds = ps->ds; |
Vivien Didelot | 5da9603 | 2016-03-07 18:24:39 -0500 | [diff] [blame] | 1418 | u16 pvid; |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1419 | int ret; |
| 1420 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1421 | ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_DEFAULT_VLAN); |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1422 | if (ret < 0) |
| 1423 | return ret; |
| 1424 | |
Vivien Didelot | 5da9603 | 2016-03-07 18:24:39 -0500 | [diff] [blame] | 1425 | pvid = ret & PORT_DEFAULT_VLAN_MASK; |
| 1426 | |
| 1427 | if (new) { |
| 1428 | ret &= ~PORT_DEFAULT_VLAN_MASK; |
| 1429 | ret |= *new & PORT_DEFAULT_VLAN_MASK; |
| 1430 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1431 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Vivien Didelot | 5da9603 | 2016-03-07 18:24:39 -0500 | [diff] [blame] | 1432 | PORT_DEFAULT_VLAN, ret); |
| 1433 | if (ret < 0) |
| 1434 | return ret; |
| 1435 | |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 1436 | netdev_dbg(ds->ports[port].netdev, |
| 1437 | "DefaultVID %d (was %d)\n", *new, pvid); |
Vivien Didelot | 5da9603 | 2016-03-07 18:24:39 -0500 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | if (old) |
| 1441 | *old = pvid; |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1442 | |
| 1443 | return 0; |
| 1444 | } |
| 1445 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1446 | static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_priv_state *ps, |
| 1447 | int port, u16 *pvid) |
Vivien Didelot | 5da9603 | 2016-03-07 18:24:39 -0500 | [diff] [blame] | 1448 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1449 | return _mv88e6xxx_port_pvid(ps, port, NULL, pvid); |
Vivien Didelot | 5da9603 | 2016-03-07 18:24:39 -0500 | [diff] [blame] | 1450 | } |
| 1451 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1452 | static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_priv_state *ps, |
| 1453 | int port, u16 pvid) |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1454 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1455 | return _mv88e6xxx_port_pvid(ps, port, &pvid, NULL); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1456 | } |
| 1457 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1458 | static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_priv_state *ps) |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 1459 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1460 | return _mv88e6xxx_wait(ps, REG_GLOBAL, GLOBAL_VTU_OP, |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 1461 | GLOBAL_VTU_OP_BUSY); |
| 1462 | } |
| 1463 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1464 | static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_priv_state *ps, u16 op) |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 1465 | { |
| 1466 | int ret; |
| 1467 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1468 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_OP, op); |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 1469 | if (ret < 0) |
| 1470 | return ret; |
| 1471 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1472 | return _mv88e6xxx_vtu_wait(ps); |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 1473 | } |
| 1474 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1475 | static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_priv_state *ps) |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 1476 | { |
| 1477 | int ret; |
| 1478 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1479 | ret = _mv88e6xxx_vtu_wait(ps); |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 1480 | if (ret < 0) |
| 1481 | return ret; |
| 1482 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1483 | return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_FLUSH_ALL); |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 1484 | } |
| 1485 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1486 | static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_priv_state *ps, |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1487 | struct mv88e6xxx_vtu_stu_entry *entry, |
| 1488 | unsigned int nibble_offset) |
| 1489 | { |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1490 | u16 regs[3]; |
| 1491 | int i; |
| 1492 | int ret; |
| 1493 | |
| 1494 | for (i = 0; i < 3; ++i) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1495 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1496 | GLOBAL_VTU_DATA_0_3 + i); |
| 1497 | if (ret < 0) |
| 1498 | return ret; |
| 1499 | |
| 1500 | regs[i] = ret; |
| 1501 | } |
| 1502 | |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 1503 | for (i = 0; i < ps->info->num_ports; ++i) { |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1504 | unsigned int shift = (i % 4) * 4 + nibble_offset; |
| 1505 | u16 reg = regs[i / 4]; |
| 1506 | |
| 1507 | entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK; |
| 1508 | } |
| 1509 | |
| 1510 | return 0; |
| 1511 | } |
| 1512 | |
Vivien Didelot | 15d7d7d | 2016-05-10 15:44:28 -0400 | [diff] [blame] | 1513 | static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_priv_state *ps, |
| 1514 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1515 | { |
| 1516 | return _mv88e6xxx_vtu_stu_data_read(ps, entry, 0); |
| 1517 | } |
| 1518 | |
| 1519 | static int mv88e6xxx_stu_data_read(struct mv88e6xxx_priv_state *ps, |
| 1520 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1521 | { |
| 1522 | return _mv88e6xxx_vtu_stu_data_read(ps, entry, 2); |
| 1523 | } |
| 1524 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1525 | static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_priv_state *ps, |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1526 | struct mv88e6xxx_vtu_stu_entry *entry, |
| 1527 | unsigned int nibble_offset) |
| 1528 | { |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1529 | u16 regs[3] = { 0 }; |
| 1530 | int i; |
| 1531 | int ret; |
| 1532 | |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 1533 | for (i = 0; i < ps->info->num_ports; ++i) { |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1534 | unsigned int shift = (i % 4) * 4 + nibble_offset; |
| 1535 | u8 data = entry->data[i]; |
| 1536 | |
| 1537 | regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift; |
| 1538 | } |
| 1539 | |
| 1540 | for (i = 0; i < 3; ++i) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1541 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1542 | GLOBAL_VTU_DATA_0_3 + i, regs[i]); |
| 1543 | if (ret < 0) |
| 1544 | return ret; |
| 1545 | } |
| 1546 | |
| 1547 | return 0; |
| 1548 | } |
| 1549 | |
Vivien Didelot | 15d7d7d | 2016-05-10 15:44:28 -0400 | [diff] [blame] | 1550 | static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_priv_state *ps, |
| 1551 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1552 | { |
| 1553 | return _mv88e6xxx_vtu_stu_data_write(ps, entry, 0); |
| 1554 | } |
| 1555 | |
| 1556 | static int mv88e6xxx_stu_data_write(struct mv88e6xxx_priv_state *ps, |
| 1557 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1558 | { |
| 1559 | return _mv88e6xxx_vtu_stu_data_write(ps, entry, 2); |
| 1560 | } |
| 1561 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1562 | static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_priv_state *ps, u16 vid) |
Vivien Didelot | 36d04ba | 2015-10-22 09:34:39 -0400 | [diff] [blame] | 1563 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1564 | return _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, |
Vivien Didelot | 36d04ba | 2015-10-22 09:34:39 -0400 | [diff] [blame] | 1565 | vid & GLOBAL_VTU_VID_MASK); |
| 1566 | } |
| 1567 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1568 | static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_priv_state *ps, |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1569 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1570 | { |
| 1571 | struct mv88e6xxx_vtu_stu_entry next = { 0 }; |
| 1572 | int ret; |
| 1573 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1574 | ret = _mv88e6xxx_vtu_wait(ps); |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1575 | if (ret < 0) |
| 1576 | return ret; |
| 1577 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1578 | ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_VTU_GET_NEXT); |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1579 | if (ret < 0) |
| 1580 | return ret; |
| 1581 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1582 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID); |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1583 | if (ret < 0) |
| 1584 | return ret; |
| 1585 | |
| 1586 | next.vid = ret & GLOBAL_VTU_VID_MASK; |
| 1587 | next.valid = !!(ret & GLOBAL_VTU_VID_VALID); |
| 1588 | |
| 1589 | if (next.valid) { |
Vivien Didelot | 15d7d7d | 2016-05-10 15:44:28 -0400 | [diff] [blame] | 1590 | ret = mv88e6xxx_vtu_data_read(ps, &next); |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1591 | if (ret < 0) |
| 1592 | return ret; |
| 1593 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1594 | if (mv88e6xxx_has_fid_reg(ps)) { |
| 1595 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1596 | GLOBAL_VTU_FID); |
| 1597 | if (ret < 0) |
| 1598 | return ret; |
| 1599 | |
| 1600 | next.fid = ret & GLOBAL_VTU_FID_MASK; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1601 | } else if (mv88e6xxx_num_databases(ps) == 256) { |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 1602 | /* VTU DBNum[7:4] are located in VTU Operation 11:8, and |
| 1603 | * VTU DBNum[3:0] are located in VTU Operation 3:0 |
| 1604 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1605 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 1606 | GLOBAL_VTU_OP); |
| 1607 | if (ret < 0) |
| 1608 | return ret; |
| 1609 | |
| 1610 | next.fid = (ret & 0xf00) >> 4; |
| 1611 | next.fid |= ret & 0xf; |
Vivien Didelot | 2e7bd5e | 2016-03-31 16:53:41 -0400 | [diff] [blame] | 1612 | } |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1613 | |
Vivien Didelot | cb9b902 | 2016-05-10 15:44:29 -0400 | [diff] [blame] | 1614 | if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_STU)) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1615 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1616 | GLOBAL_VTU_SID); |
| 1617 | if (ret < 0) |
| 1618 | return ret; |
| 1619 | |
| 1620 | next.sid = ret & GLOBAL_VTU_SID_MASK; |
| 1621 | } |
| 1622 | } |
| 1623 | |
| 1624 | *entry = next; |
| 1625 | return 0; |
| 1626 | } |
| 1627 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 1628 | static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port, |
| 1629 | struct switchdev_obj_port_vlan *vlan, |
| 1630 | int (*cb)(struct switchdev_obj *obj)) |
Vivien Didelot | ceff5ef | 2016-02-23 12:13:55 -0500 | [diff] [blame] | 1631 | { |
| 1632 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1633 | struct mv88e6xxx_vtu_stu_entry next; |
| 1634 | u16 pvid; |
| 1635 | int err; |
| 1636 | |
Vivien Didelot | 54d77b5 | 2016-05-09 13:22:47 -0400 | [diff] [blame] | 1637 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU)) |
| 1638 | return -EOPNOTSUPP; |
| 1639 | |
Vivien Didelot | ceff5ef | 2016-02-23 12:13:55 -0500 | [diff] [blame] | 1640 | mutex_lock(&ps->smi_mutex); |
| 1641 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1642 | err = _mv88e6xxx_port_pvid_get(ps, port, &pvid); |
Vivien Didelot | ceff5ef | 2016-02-23 12:13:55 -0500 | [diff] [blame] | 1643 | if (err) |
| 1644 | goto unlock; |
| 1645 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1646 | err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK); |
Vivien Didelot | ceff5ef | 2016-02-23 12:13:55 -0500 | [diff] [blame] | 1647 | if (err) |
| 1648 | goto unlock; |
| 1649 | |
| 1650 | do { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1651 | err = _mv88e6xxx_vtu_getnext(ps, &next); |
Vivien Didelot | ceff5ef | 2016-02-23 12:13:55 -0500 | [diff] [blame] | 1652 | if (err) |
| 1653 | break; |
| 1654 | |
| 1655 | if (!next.valid) |
| 1656 | break; |
| 1657 | |
| 1658 | if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) |
| 1659 | continue; |
| 1660 | |
| 1661 | /* reinit and dump this VLAN obj */ |
| 1662 | vlan->vid_begin = vlan->vid_end = next.vid; |
| 1663 | vlan->flags = 0; |
| 1664 | |
| 1665 | if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED) |
| 1666 | vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED; |
| 1667 | |
| 1668 | if (next.vid == pvid) |
| 1669 | vlan->flags |= BRIDGE_VLAN_INFO_PVID; |
| 1670 | |
| 1671 | err = cb(&vlan->obj); |
| 1672 | if (err) |
| 1673 | break; |
| 1674 | } while (next.vid < GLOBAL_VTU_VID_MASK); |
| 1675 | |
| 1676 | unlock: |
| 1677 | mutex_unlock(&ps->smi_mutex); |
| 1678 | |
| 1679 | return err; |
| 1680 | } |
| 1681 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1682 | static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_priv_state *ps, |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1683 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1684 | { |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 1685 | u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE; |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1686 | u16 reg = 0; |
| 1687 | int ret; |
| 1688 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1689 | ret = _mv88e6xxx_vtu_wait(ps); |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1690 | if (ret < 0) |
| 1691 | return ret; |
| 1692 | |
| 1693 | if (!entry->valid) |
| 1694 | goto loadpurge; |
| 1695 | |
| 1696 | /* Write port member tags */ |
Vivien Didelot | 15d7d7d | 2016-05-10 15:44:28 -0400 | [diff] [blame] | 1697 | ret = mv88e6xxx_vtu_data_write(ps, entry); |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1698 | if (ret < 0) |
| 1699 | return ret; |
| 1700 | |
Vivien Didelot | cb9b902 | 2016-05-10 15:44:29 -0400 | [diff] [blame] | 1701 | if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_STU)) { |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1702 | reg = entry->sid & GLOBAL_VTU_SID_MASK; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1703 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg); |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1704 | if (ret < 0) |
| 1705 | return ret; |
Vivien Didelot | b426e5f | 2016-03-31 16:53:42 -0400 | [diff] [blame] | 1706 | } |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1707 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1708 | if (mv88e6xxx_has_fid_reg(ps)) { |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1709 | reg = entry->fid & GLOBAL_VTU_FID_MASK; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1710 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_FID, reg); |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1711 | if (ret < 0) |
| 1712 | return ret; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1713 | } else if (mv88e6xxx_num_databases(ps) == 256) { |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 1714 | /* VTU DBNum[7:4] are located in VTU Operation 11:8, and |
| 1715 | * VTU DBNum[3:0] are located in VTU Operation 3:0 |
| 1716 | */ |
| 1717 | op |= (entry->fid & 0xf0) << 8; |
| 1718 | op |= entry->fid & 0xf; |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | reg = GLOBAL_VTU_VID_VALID; |
| 1722 | loadpurge: |
| 1723 | reg |= entry->vid & GLOBAL_VTU_VID_MASK; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1724 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg); |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1725 | if (ret < 0) |
| 1726 | return ret; |
| 1727 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1728 | return _mv88e6xxx_vtu_cmd(ps, op); |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1729 | } |
| 1730 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1731 | static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_priv_state *ps, u8 sid, |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1732 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1733 | { |
| 1734 | struct mv88e6xxx_vtu_stu_entry next = { 0 }; |
| 1735 | int ret; |
| 1736 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1737 | ret = _mv88e6xxx_vtu_wait(ps); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1738 | if (ret < 0) |
| 1739 | return ret; |
| 1740 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1741 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1742 | sid & GLOBAL_VTU_SID_MASK); |
| 1743 | if (ret < 0) |
| 1744 | return ret; |
| 1745 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1746 | ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_GET_NEXT); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1747 | if (ret < 0) |
| 1748 | return ret; |
| 1749 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1750 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_SID); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1751 | if (ret < 0) |
| 1752 | return ret; |
| 1753 | |
| 1754 | next.sid = ret & GLOBAL_VTU_SID_MASK; |
| 1755 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1756 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1757 | if (ret < 0) |
| 1758 | return ret; |
| 1759 | |
| 1760 | next.valid = !!(ret & GLOBAL_VTU_VID_VALID); |
| 1761 | |
| 1762 | if (next.valid) { |
Vivien Didelot | 15d7d7d | 2016-05-10 15:44:28 -0400 | [diff] [blame] | 1763 | ret = mv88e6xxx_stu_data_read(ps, &next); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1764 | if (ret < 0) |
| 1765 | return ret; |
| 1766 | } |
| 1767 | |
| 1768 | *entry = next; |
| 1769 | return 0; |
| 1770 | } |
| 1771 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1772 | static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_priv_state *ps, |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1773 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1774 | { |
| 1775 | u16 reg = 0; |
| 1776 | int ret; |
| 1777 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1778 | ret = _mv88e6xxx_vtu_wait(ps); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1779 | if (ret < 0) |
| 1780 | return ret; |
| 1781 | |
| 1782 | if (!entry->valid) |
| 1783 | goto loadpurge; |
| 1784 | |
| 1785 | /* Write port states */ |
Vivien Didelot | 15d7d7d | 2016-05-10 15:44:28 -0400 | [diff] [blame] | 1786 | ret = mv88e6xxx_stu_data_write(ps, entry); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1787 | if (ret < 0) |
| 1788 | return ret; |
| 1789 | |
| 1790 | reg = GLOBAL_VTU_VID_VALID; |
| 1791 | loadpurge: |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1792 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1793 | if (ret < 0) |
| 1794 | return ret; |
| 1795 | |
| 1796 | reg = entry->sid & GLOBAL_VTU_SID_MASK; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1797 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1798 | if (ret < 0) |
| 1799 | return ret; |
| 1800 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1801 | return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_LOAD_PURGE); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1802 | } |
| 1803 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1804 | static int _mv88e6xxx_port_fid(struct mv88e6xxx_priv_state *ps, int port, |
| 1805 | u16 *new, u16 *old) |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1806 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1807 | struct dsa_switch *ds = ps->ds; |
Vivien Didelot | f74df0b | 2016-03-31 16:53:43 -0400 | [diff] [blame] | 1808 | u16 upper_mask; |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1809 | u16 fid; |
| 1810 | int ret; |
| 1811 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1812 | if (mv88e6xxx_num_databases(ps) == 4096) |
Vivien Didelot | f74df0b | 2016-03-31 16:53:43 -0400 | [diff] [blame] | 1813 | upper_mask = 0xff; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1814 | else if (mv88e6xxx_num_databases(ps) == 256) |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 1815 | upper_mask = 0xf; |
Vivien Didelot | f74df0b | 2016-03-31 16:53:43 -0400 | [diff] [blame] | 1816 | else |
| 1817 | return -EOPNOTSUPP; |
| 1818 | |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1819 | /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1820 | ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_BASE_VLAN); |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1821 | if (ret < 0) |
| 1822 | return ret; |
| 1823 | |
| 1824 | fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12; |
| 1825 | |
| 1826 | if (new) { |
| 1827 | ret &= ~PORT_BASE_VLAN_FID_3_0_MASK; |
| 1828 | ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK; |
| 1829 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1830 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_BASE_VLAN, |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1831 | ret); |
| 1832 | if (ret < 0) |
| 1833 | return ret; |
| 1834 | } |
| 1835 | |
| 1836 | /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1837 | ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_1); |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1838 | if (ret < 0) |
| 1839 | return ret; |
| 1840 | |
Vivien Didelot | f74df0b | 2016-03-31 16:53:43 -0400 | [diff] [blame] | 1841 | fid |= (ret & upper_mask) << 4; |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1842 | |
| 1843 | if (new) { |
Vivien Didelot | f74df0b | 2016-03-31 16:53:43 -0400 | [diff] [blame] | 1844 | ret &= ~upper_mask; |
| 1845 | ret |= (*new >> 4) & upper_mask; |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1846 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1847 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1, |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1848 | ret); |
| 1849 | if (ret < 0) |
| 1850 | return ret; |
| 1851 | |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 1852 | netdev_dbg(ds->ports[port].netdev, |
| 1853 | "FID %d (was %d)\n", *new, fid); |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | if (old) |
| 1857 | *old = fid; |
| 1858 | |
| 1859 | return 0; |
| 1860 | } |
| 1861 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1862 | static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_priv_state *ps, |
| 1863 | int port, u16 *fid) |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1864 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1865 | return _mv88e6xxx_port_fid(ps, port, NULL, fid); |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1866 | } |
| 1867 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1868 | static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_priv_state *ps, |
| 1869 | int port, u16 fid) |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1870 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1871 | return _mv88e6xxx_port_fid(ps, port, &fid, NULL); |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1872 | } |
| 1873 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1874 | static int _mv88e6xxx_fid_new(struct mv88e6xxx_priv_state *ps, u16 *fid) |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1875 | { |
| 1876 | DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID); |
| 1877 | struct mv88e6xxx_vtu_stu_entry vlan; |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1878 | int i, err; |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1879 | |
| 1880 | bitmap_zero(fid_bitmap, MV88E6XXX_N_FID); |
| 1881 | |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1882 | /* Set every FID bit used by the (un)bridged ports */ |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 1883 | for (i = 0; i < ps->info->num_ports; ++i) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1884 | err = _mv88e6xxx_port_fid_get(ps, i, fid); |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1885 | if (err) |
| 1886 | return err; |
| 1887 | |
| 1888 | set_bit(*fid, fid_bitmap); |
| 1889 | } |
| 1890 | |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1891 | /* Set every FID bit used by the VLAN entries */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1892 | err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK); |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1893 | if (err) |
| 1894 | return err; |
| 1895 | |
| 1896 | do { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1897 | err = _mv88e6xxx_vtu_getnext(ps, &vlan); |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1898 | if (err) |
| 1899 | return err; |
| 1900 | |
| 1901 | if (!vlan.valid) |
| 1902 | break; |
| 1903 | |
| 1904 | set_bit(vlan.fid, fid_bitmap); |
| 1905 | } while (vlan.vid < GLOBAL_VTU_VID_MASK); |
| 1906 | |
| 1907 | /* The reset value 0x000 is used to indicate that multiple address |
| 1908 | * databases are not needed. Return the next positive available. |
| 1909 | */ |
| 1910 | *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1); |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1911 | if (unlikely(*fid >= mv88e6xxx_num_databases(ps))) |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1912 | return -ENOSPC; |
| 1913 | |
| 1914 | /* Clear the database */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1915 | return _mv88e6xxx_atu_flush(ps, *fid, true); |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1916 | } |
| 1917 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1918 | static int _mv88e6xxx_vtu_new(struct mv88e6xxx_priv_state *ps, u16 vid, |
Vivien Didelot | 2fb5ef0 | 2016-02-26 13:16:01 -0500 | [diff] [blame] | 1919 | struct mv88e6xxx_vtu_stu_entry *entry) |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1920 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1921 | struct dsa_switch *ds = ps->ds; |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1922 | struct mv88e6xxx_vtu_stu_entry vlan = { |
| 1923 | .valid = true, |
| 1924 | .vid = vid, |
| 1925 | }; |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1926 | int i, err; |
| 1927 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1928 | err = _mv88e6xxx_fid_new(ps, &vlan.fid); |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1929 | if (err) |
| 1930 | return err; |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1931 | |
Vivien Didelot | 3d131f0 | 2015-11-03 10:52:52 -0500 | [diff] [blame] | 1932 | /* exclude all ports except the CPU and DSA ports */ |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 1933 | for (i = 0; i < ps->info->num_ports; ++i) |
Vivien Didelot | 3d131f0 | 2015-11-03 10:52:52 -0500 | [diff] [blame] | 1934 | vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i) |
| 1935 | ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED |
| 1936 | : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER; |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1937 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1938 | if (mv88e6xxx_6097_family(ps) || mv88e6xxx_6165_family(ps) || |
| 1939 | mv88e6xxx_6351_family(ps) || mv88e6xxx_6352_family(ps)) { |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1940 | struct mv88e6xxx_vtu_stu_entry vstp; |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1941 | |
| 1942 | /* Adding a VTU entry requires a valid STU entry. As VSTP is not |
| 1943 | * implemented, only one STU entry is needed to cover all VTU |
| 1944 | * entries. Thus, validate the SID 0. |
| 1945 | */ |
| 1946 | vlan.sid = 0; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1947 | err = _mv88e6xxx_stu_getnext(ps, GLOBAL_VTU_SID_MASK, &vstp); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1948 | if (err) |
| 1949 | return err; |
| 1950 | |
| 1951 | if (vstp.sid != vlan.sid || !vstp.valid) { |
| 1952 | memset(&vstp, 0, sizeof(vstp)); |
| 1953 | vstp.valid = true; |
| 1954 | vstp.sid = vlan.sid; |
| 1955 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1956 | err = _mv88e6xxx_stu_loadpurge(ps, &vstp); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1957 | if (err) |
| 1958 | return err; |
| 1959 | } |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | *entry = vlan; |
| 1963 | return 0; |
| 1964 | } |
| 1965 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1966 | static int _mv88e6xxx_vtu_get(struct mv88e6xxx_priv_state *ps, u16 vid, |
Vivien Didelot | 2fb5ef0 | 2016-02-26 13:16:01 -0500 | [diff] [blame] | 1967 | struct mv88e6xxx_vtu_stu_entry *entry, bool creat) |
| 1968 | { |
| 1969 | int err; |
| 1970 | |
| 1971 | if (!vid) |
| 1972 | return -EINVAL; |
| 1973 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1974 | err = _mv88e6xxx_vtu_vid_write(ps, vid - 1); |
Vivien Didelot | 2fb5ef0 | 2016-02-26 13:16:01 -0500 | [diff] [blame] | 1975 | if (err) |
| 1976 | return err; |
| 1977 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1978 | err = _mv88e6xxx_vtu_getnext(ps, entry); |
Vivien Didelot | 2fb5ef0 | 2016-02-26 13:16:01 -0500 | [diff] [blame] | 1979 | if (err) |
| 1980 | return err; |
| 1981 | |
| 1982 | if (entry->vid != vid || !entry->valid) { |
| 1983 | if (!creat) |
| 1984 | return -EOPNOTSUPP; |
| 1985 | /* -ENOENT would've been more appropriate, but switchdev expects |
| 1986 | * -EOPNOTSUPP to inform bridge about an eventual software VLAN. |
| 1987 | */ |
| 1988 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1989 | err = _mv88e6xxx_vtu_new(ps, vid, entry); |
Vivien Didelot | 2fb5ef0 | 2016-02-26 13:16:01 -0500 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | return err; |
| 1993 | } |
| 1994 | |
Vivien Didelot | da9c359 | 2016-02-12 12:09:40 -0500 | [diff] [blame] | 1995 | static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port, |
| 1996 | u16 vid_begin, u16 vid_end) |
| 1997 | { |
| 1998 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1999 | struct mv88e6xxx_vtu_stu_entry vlan; |
| 2000 | int i, err; |
| 2001 | |
| 2002 | if (!vid_begin) |
| 2003 | return -EOPNOTSUPP; |
| 2004 | |
| 2005 | mutex_lock(&ps->smi_mutex); |
| 2006 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2007 | err = _mv88e6xxx_vtu_vid_write(ps, vid_begin - 1); |
Vivien Didelot | da9c359 | 2016-02-12 12:09:40 -0500 | [diff] [blame] | 2008 | if (err) |
| 2009 | goto unlock; |
| 2010 | |
| 2011 | do { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2012 | err = _mv88e6xxx_vtu_getnext(ps, &vlan); |
Vivien Didelot | da9c359 | 2016-02-12 12:09:40 -0500 | [diff] [blame] | 2013 | if (err) |
| 2014 | goto unlock; |
| 2015 | |
| 2016 | if (!vlan.valid) |
| 2017 | break; |
| 2018 | |
| 2019 | if (vlan.vid > vid_end) |
| 2020 | break; |
| 2021 | |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 2022 | for (i = 0; i < ps->info->num_ports; ++i) { |
Vivien Didelot | da9c359 | 2016-02-12 12:09:40 -0500 | [diff] [blame] | 2023 | if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i)) |
| 2024 | continue; |
| 2025 | |
| 2026 | if (vlan.data[i] == |
| 2027 | GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) |
| 2028 | continue; |
| 2029 | |
| 2030 | if (ps->ports[i].bridge_dev == |
| 2031 | ps->ports[port].bridge_dev) |
| 2032 | break; /* same bridge, check next VLAN */ |
| 2033 | |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 2034 | netdev_warn(ds->ports[port].netdev, |
Vivien Didelot | da9c359 | 2016-02-12 12:09:40 -0500 | [diff] [blame] | 2035 | "hardware VLAN %d already used by %s\n", |
| 2036 | vlan.vid, |
| 2037 | netdev_name(ps->ports[i].bridge_dev)); |
| 2038 | err = -EOPNOTSUPP; |
| 2039 | goto unlock; |
| 2040 | } |
| 2041 | } while (vlan.vid < vid_end); |
| 2042 | |
| 2043 | unlock: |
| 2044 | mutex_unlock(&ps->smi_mutex); |
| 2045 | |
| 2046 | return err; |
| 2047 | } |
| 2048 | |
Vivien Didelot | 214cdb9 | 2016-02-26 13:16:08 -0500 | [diff] [blame] | 2049 | static const char * const mv88e6xxx_port_8021q_mode_names[] = { |
| 2050 | [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled", |
| 2051 | [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback", |
| 2052 | [PORT_CONTROL_2_8021Q_CHECK] = "Check", |
| 2053 | [PORT_CONTROL_2_8021Q_SECURE] = "Secure", |
| 2054 | }; |
| 2055 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 2056 | static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port, |
| 2057 | bool vlan_filtering) |
Vivien Didelot | 214cdb9 | 2016-02-26 13:16:08 -0500 | [diff] [blame] | 2058 | { |
| 2059 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2060 | u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE : |
| 2061 | PORT_CONTROL_2_8021Q_DISABLED; |
| 2062 | int ret; |
| 2063 | |
Vivien Didelot | 54d77b5 | 2016-05-09 13:22:47 -0400 | [diff] [blame] | 2064 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU)) |
| 2065 | return -EOPNOTSUPP; |
| 2066 | |
Vivien Didelot | 214cdb9 | 2016-02-26 13:16:08 -0500 | [diff] [blame] | 2067 | mutex_lock(&ps->smi_mutex); |
| 2068 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2069 | ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_2); |
Vivien Didelot | 214cdb9 | 2016-02-26 13:16:08 -0500 | [diff] [blame] | 2070 | if (ret < 0) |
| 2071 | goto unlock; |
| 2072 | |
| 2073 | old = ret & PORT_CONTROL_2_8021Q_MASK; |
| 2074 | |
Vivien Didelot | 5220ef1 | 2016-03-07 18:24:52 -0500 | [diff] [blame] | 2075 | if (new != old) { |
| 2076 | ret &= ~PORT_CONTROL_2_8021Q_MASK; |
| 2077 | ret |= new & PORT_CONTROL_2_8021Q_MASK; |
Vivien Didelot | 214cdb9 | 2016-02-26 13:16:08 -0500 | [diff] [blame] | 2078 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2079 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_2, |
Vivien Didelot | 5220ef1 | 2016-03-07 18:24:52 -0500 | [diff] [blame] | 2080 | ret); |
| 2081 | if (ret < 0) |
| 2082 | goto unlock; |
Vivien Didelot | 214cdb9 | 2016-02-26 13:16:08 -0500 | [diff] [blame] | 2083 | |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 2084 | netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n", |
Vivien Didelot | 5220ef1 | 2016-03-07 18:24:52 -0500 | [diff] [blame] | 2085 | mv88e6xxx_port_8021q_mode_names[new], |
| 2086 | mv88e6xxx_port_8021q_mode_names[old]); |
| 2087 | } |
| 2088 | |
| 2089 | ret = 0; |
Vivien Didelot | 214cdb9 | 2016-02-26 13:16:08 -0500 | [diff] [blame] | 2090 | unlock: |
| 2091 | mutex_unlock(&ps->smi_mutex); |
| 2092 | |
| 2093 | return ret; |
| 2094 | } |
| 2095 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 2096 | static int mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port, |
| 2097 | const struct switchdev_obj_port_vlan *vlan, |
| 2098 | struct switchdev_trans *trans) |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 2099 | { |
Vivien Didelot | 54d77b5 | 2016-05-09 13:22:47 -0400 | [diff] [blame] | 2100 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | da9c359 | 2016-02-12 12:09:40 -0500 | [diff] [blame] | 2101 | int err; |
| 2102 | |
Vivien Didelot | 54d77b5 | 2016-05-09 13:22:47 -0400 | [diff] [blame] | 2103 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU)) |
| 2104 | return -EOPNOTSUPP; |
| 2105 | |
Vivien Didelot | da9c359 | 2016-02-12 12:09:40 -0500 | [diff] [blame] | 2106 | /* If the requested port doesn't belong to the same bridge as the VLAN |
| 2107 | * members, do not support it (yet) and fallback to software VLAN. |
| 2108 | */ |
| 2109 | err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin, |
| 2110 | vlan->vid_end); |
| 2111 | if (err) |
| 2112 | return err; |
| 2113 | |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2114 | /* We don't need any dynamic resource from the kernel (yet), |
| 2115 | * so skip the prepare phase. |
| 2116 | */ |
| 2117 | return 0; |
| 2118 | } |
| 2119 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2120 | static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_priv_state *ps, int port, |
| 2121 | u16 vid, bool untagged) |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2122 | { |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 2123 | struct mv88e6xxx_vtu_stu_entry vlan; |
| 2124 | int err; |
| 2125 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2126 | err = _mv88e6xxx_vtu_get(ps, vid, &vlan, true); |
Vivien Didelot | 36d04ba | 2015-10-22 09:34:39 -0400 | [diff] [blame] | 2127 | if (err) |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2128 | return err; |
Vivien Didelot | 36d04ba | 2015-10-22 09:34:39 -0400 | [diff] [blame] | 2129 | |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 2130 | vlan.data[port] = untagged ? |
| 2131 | GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED : |
| 2132 | GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED; |
| 2133 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2134 | return _mv88e6xxx_vtu_loadpurge(ps, &vlan); |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2135 | } |
| 2136 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 2137 | static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port, |
| 2138 | const struct switchdev_obj_port_vlan *vlan, |
| 2139 | struct switchdev_trans *trans) |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2140 | { |
| 2141 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2142 | bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; |
| 2143 | bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID; |
| 2144 | u16 vid; |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2145 | |
Vivien Didelot | 54d77b5 | 2016-05-09 13:22:47 -0400 | [diff] [blame] | 2146 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU)) |
| 2147 | return; |
| 2148 | |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2149 | mutex_lock(&ps->smi_mutex); |
| 2150 | |
Vivien Didelot | 4d5770b | 2016-04-06 11:55:05 -0400 | [diff] [blame] | 2151 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2152 | if (_mv88e6xxx_port_vlan_add(ps, port, vid, untagged)) |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 2153 | netdev_err(ds->ports[port].netdev, |
| 2154 | "failed to add VLAN %d%c\n", |
Vivien Didelot | 4d5770b | 2016-04-06 11:55:05 -0400 | [diff] [blame] | 2155 | vid, untagged ? 'u' : 't'); |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2156 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2157 | if (pvid && _mv88e6xxx_port_pvid_set(ps, port, vlan->vid_end)) |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 2158 | netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n", |
Vivien Didelot | 4d5770b | 2016-04-06 11:55:05 -0400 | [diff] [blame] | 2159 | vlan->vid_end); |
| 2160 | |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 2161 | mutex_unlock(&ps->smi_mutex); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 2162 | } |
| 2163 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2164 | static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_priv_state *ps, |
| 2165 | int port, u16 vid) |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 2166 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2167 | struct dsa_switch *ds = ps->ds; |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 2168 | struct mv88e6xxx_vtu_stu_entry vlan; |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 2169 | int i, err; |
| 2170 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2171 | err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false); |
Vivien Didelot | 36d04ba | 2015-10-22 09:34:39 -0400 | [diff] [blame] | 2172 | if (err) |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2173 | return err; |
Vivien Didelot | 36d04ba | 2015-10-22 09:34:39 -0400 | [diff] [blame] | 2174 | |
Vivien Didelot | 2fb5ef0 | 2016-02-26 13:16:01 -0500 | [diff] [blame] | 2175 | /* Tell switchdev if this VLAN is handled in software */ |
| 2176 | if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) |
Vivien Didelot | 3c06f08 | 2016-02-05 14:04:39 -0500 | [diff] [blame] | 2177 | return -EOPNOTSUPP; |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 2178 | |
| 2179 | vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER; |
| 2180 | |
| 2181 | /* keep the VLAN unless all ports are excluded */ |
Vivien Didelot | f02bdff | 2015-10-11 18:08:36 -0400 | [diff] [blame] | 2182 | vlan.valid = false; |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 2183 | for (i = 0; i < ps->info->num_ports; ++i) { |
Vivien Didelot | 3d131f0 | 2015-11-03 10:52:52 -0500 | [diff] [blame] | 2184 | if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)) |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 2185 | continue; |
| 2186 | |
| 2187 | if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) { |
Vivien Didelot | f02bdff | 2015-10-11 18:08:36 -0400 | [diff] [blame] | 2188 | vlan.valid = true; |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 2189 | break; |
| 2190 | } |
| 2191 | } |
| 2192 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2193 | err = _mv88e6xxx_vtu_loadpurge(ps, &vlan); |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 2194 | if (err) |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2195 | return err; |
| 2196 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2197 | return _mv88e6xxx_atu_remove(ps, vlan.fid, port, false); |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2198 | } |
| 2199 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 2200 | static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port, |
| 2201 | const struct switchdev_obj_port_vlan *vlan) |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2202 | { |
| 2203 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2204 | u16 pvid, vid; |
| 2205 | int err = 0; |
| 2206 | |
Vivien Didelot | 54d77b5 | 2016-05-09 13:22:47 -0400 | [diff] [blame] | 2207 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU)) |
| 2208 | return -EOPNOTSUPP; |
| 2209 | |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2210 | mutex_lock(&ps->smi_mutex); |
| 2211 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2212 | err = _mv88e6xxx_port_pvid_get(ps, port, &pvid); |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2213 | if (err) |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 2214 | goto unlock; |
| 2215 | |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2216 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2217 | err = _mv88e6xxx_port_vlan_del(ps, port, vid); |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2218 | if (err) |
| 2219 | goto unlock; |
| 2220 | |
| 2221 | if (vid == pvid) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2222 | err = _mv88e6xxx_port_pvid_set(ps, port, 0); |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2223 | if (err) |
| 2224 | goto unlock; |
| 2225 | } |
| 2226 | } |
| 2227 | |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 2228 | unlock: |
| 2229 | mutex_unlock(&ps->smi_mutex); |
| 2230 | |
| 2231 | return err; |
| 2232 | } |
| 2233 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2234 | static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_priv_state *ps, |
Vivien Didelot | c5723ac | 2015-08-10 09:09:48 -0400 | [diff] [blame] | 2235 | const unsigned char *addr) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2236 | { |
| 2237 | int i, ret; |
| 2238 | |
| 2239 | for (i = 0; i < 3; i++) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 2240 | ret = _mv88e6xxx_reg_write( |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2241 | ps, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i, |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 2242 | (addr[i * 2] << 8) | addr[i * 2 + 1]); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2243 | if (ret < 0) |
| 2244 | return ret; |
| 2245 | } |
| 2246 | |
| 2247 | return 0; |
| 2248 | } |
| 2249 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2250 | static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_priv_state *ps, |
| 2251 | unsigned char *addr) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2252 | { |
| 2253 | int i, ret; |
| 2254 | |
| 2255 | for (i = 0; i < 3; i++) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2256 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 2257 | GLOBAL_ATU_MAC_01 + i); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2258 | if (ret < 0) |
| 2259 | return ret; |
| 2260 | addr[i * 2] = ret >> 8; |
| 2261 | addr[i * 2 + 1] = ret & 0xff; |
| 2262 | } |
| 2263 | |
| 2264 | return 0; |
| 2265 | } |
| 2266 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2267 | static int _mv88e6xxx_atu_load(struct mv88e6xxx_priv_state *ps, |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 2268 | struct mv88e6xxx_atu_entry *entry) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2269 | { |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2270 | int ret; |
| 2271 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2272 | ret = _mv88e6xxx_atu_wait(ps); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2273 | if (ret < 0) |
| 2274 | return ret; |
| 2275 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2276 | ret = _mv88e6xxx_atu_mac_write(ps, entry->mac); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2277 | if (ret < 0) |
| 2278 | return ret; |
| 2279 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2280 | ret = _mv88e6xxx_atu_data_write(ps, entry); |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 2281 | if (ret < 0) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2282 | return ret; |
| 2283 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2284 | return _mv88e6xxx_atu_cmd(ps, entry->fid, GLOBAL_ATU_OP_LOAD_DB); |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 2285 | } |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2286 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2287 | static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_priv_state *ps, int port, |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 2288 | const unsigned char *addr, u16 vid, |
| 2289 | u8 state) |
| 2290 | { |
| 2291 | struct mv88e6xxx_atu_entry entry = { 0 }; |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 2292 | struct mv88e6xxx_vtu_stu_entry vlan; |
| 2293 | int err; |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 2294 | |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 2295 | /* Null VLAN ID corresponds to the port private database */ |
| 2296 | if (vid == 0) |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2297 | err = _mv88e6xxx_port_fid_get(ps, port, &vlan.fid); |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 2298 | else |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2299 | err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false); |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 2300 | if (err) |
| 2301 | return err; |
| 2302 | |
| 2303 | entry.fid = vlan.fid; |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 2304 | entry.state = state; |
| 2305 | ether_addr_copy(entry.mac, addr); |
| 2306 | if (state != GLOBAL_ATU_DATA_STATE_UNUSED) { |
| 2307 | entry.trunk = false; |
| 2308 | entry.portv_trunkid = BIT(port); |
| 2309 | } |
| 2310 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2311 | return _mv88e6xxx_atu_load(ps, &entry); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2312 | } |
| 2313 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 2314 | static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port, |
| 2315 | const struct switchdev_obj_port_fdb *fdb, |
| 2316 | struct switchdev_trans *trans) |
Vivien Didelot | 146a320 | 2015-10-08 11:35:12 -0400 | [diff] [blame] | 2317 | { |
Vivien Didelot | 2672f82 | 2016-05-09 13:22:48 -0400 | [diff] [blame] | 2318 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2319 | |
| 2320 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU)) |
| 2321 | return -EOPNOTSUPP; |
| 2322 | |
Vivien Didelot | 146a320 | 2015-10-08 11:35:12 -0400 | [diff] [blame] | 2323 | /* We don't need any dynamic resource from the kernel (yet), |
| 2324 | * so skip the prepare phase. |
| 2325 | */ |
| 2326 | return 0; |
| 2327 | } |
| 2328 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 2329 | static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port, |
| 2330 | const struct switchdev_obj_port_fdb *fdb, |
| 2331 | struct switchdev_trans *trans) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2332 | { |
Vivien Didelot | 1f36faf | 2015-10-08 11:35:13 -0400 | [diff] [blame] | 2333 | int state = is_multicast_ether_addr(fdb->addr) ? |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2334 | GLOBAL_ATU_DATA_STATE_MC_STATIC : |
| 2335 | GLOBAL_ATU_DATA_STATE_UC_STATIC; |
| 2336 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | 6630e23 | 2015-08-06 01:44:07 -0400 | [diff] [blame] | 2337 | |
Vivien Didelot | 2672f82 | 2016-05-09 13:22:48 -0400 | [diff] [blame] | 2338 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU)) |
| 2339 | return; |
| 2340 | |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2341 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2342 | if (_mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid, state)) |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 2343 | netdev_err(ds->ports[port].netdev, |
| 2344 | "failed to load MAC address\n"); |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2345 | mutex_unlock(&ps->smi_mutex); |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2346 | } |
| 2347 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 2348 | static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port, |
| 2349 | const struct switchdev_obj_port_fdb *fdb) |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2350 | { |
| 2351 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2352 | int ret; |
| 2353 | |
Vivien Didelot | 2672f82 | 2016-05-09 13:22:48 -0400 | [diff] [blame] | 2354 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU)) |
| 2355 | return -EOPNOTSUPP; |
| 2356 | |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2357 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2358 | ret = _mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid, |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2359 | GLOBAL_ATU_DATA_STATE_UNUSED); |
| 2360 | mutex_unlock(&ps->smi_mutex); |
| 2361 | |
| 2362 | return ret; |
| 2363 | } |
| 2364 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2365 | static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_priv_state *ps, u16 fid, |
Vivien Didelot | 1d19404 | 2015-08-10 09:09:51 -0400 | [diff] [blame] | 2366 | struct mv88e6xxx_atu_entry *entry) |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2367 | { |
Vivien Didelot | 1d19404 | 2015-08-10 09:09:51 -0400 | [diff] [blame] | 2368 | struct mv88e6xxx_atu_entry next = { 0 }; |
| 2369 | int ret; |
| 2370 | |
| 2371 | next.fid = fid; |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2372 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2373 | ret = _mv88e6xxx_atu_wait(ps); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2374 | if (ret < 0) |
| 2375 | return ret; |
| 2376 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2377 | ret = _mv88e6xxx_atu_cmd(ps, fid, GLOBAL_ATU_OP_GET_NEXT_DB); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2378 | if (ret < 0) |
| 2379 | return ret; |
| 2380 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2381 | ret = _mv88e6xxx_atu_mac_read(ps, next.mac); |
Vivien Didelot | 1d19404 | 2015-08-10 09:09:51 -0400 | [diff] [blame] | 2382 | if (ret < 0) |
| 2383 | return ret; |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2384 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2385 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_ATU_DATA); |
Vivien Didelot | 1d19404 | 2015-08-10 09:09:51 -0400 | [diff] [blame] | 2386 | if (ret < 0) |
| 2387 | return ret; |
| 2388 | |
| 2389 | next.state = ret & GLOBAL_ATU_DATA_STATE_MASK; |
| 2390 | if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) { |
| 2391 | unsigned int mask, shift; |
| 2392 | |
| 2393 | if (ret & GLOBAL_ATU_DATA_TRUNK) { |
| 2394 | next.trunk = true; |
| 2395 | mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK; |
| 2396 | shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT; |
| 2397 | } else { |
| 2398 | next.trunk = false; |
| 2399 | mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK; |
| 2400 | shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT; |
| 2401 | } |
| 2402 | |
| 2403 | next.portv_trunkid = (ret & mask) >> shift; |
| 2404 | } |
| 2405 | |
| 2406 | *entry = next; |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2407 | return 0; |
| 2408 | } |
| 2409 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2410 | static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_priv_state *ps, |
| 2411 | u16 fid, u16 vid, int port, |
Vivien Didelot | 74b6ba0 | 2016-02-26 13:16:02 -0500 | [diff] [blame] | 2412 | struct switchdev_obj_port_fdb *fdb, |
| 2413 | int (*cb)(struct switchdev_obj *obj)) |
| 2414 | { |
| 2415 | struct mv88e6xxx_atu_entry addr = { |
| 2416 | .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, |
| 2417 | }; |
| 2418 | int err; |
| 2419 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2420 | err = _mv88e6xxx_atu_mac_write(ps, addr.mac); |
Vivien Didelot | 74b6ba0 | 2016-02-26 13:16:02 -0500 | [diff] [blame] | 2421 | if (err) |
| 2422 | return err; |
| 2423 | |
| 2424 | do { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2425 | err = _mv88e6xxx_atu_getnext(ps, fid, &addr); |
Vivien Didelot | 74b6ba0 | 2016-02-26 13:16:02 -0500 | [diff] [blame] | 2426 | if (err) |
| 2427 | break; |
| 2428 | |
| 2429 | if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED) |
| 2430 | break; |
| 2431 | |
| 2432 | if (!addr.trunk && addr.portv_trunkid & BIT(port)) { |
| 2433 | bool is_static = addr.state == |
| 2434 | (is_multicast_ether_addr(addr.mac) ? |
| 2435 | GLOBAL_ATU_DATA_STATE_MC_STATIC : |
| 2436 | GLOBAL_ATU_DATA_STATE_UC_STATIC); |
| 2437 | |
| 2438 | fdb->vid = vid; |
| 2439 | ether_addr_copy(fdb->addr, addr.mac); |
| 2440 | fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; |
| 2441 | |
| 2442 | err = cb(&fdb->obj); |
| 2443 | if (err) |
| 2444 | break; |
| 2445 | } |
| 2446 | } while (!is_broadcast_ether_addr(addr.mac)); |
| 2447 | |
| 2448 | return err; |
| 2449 | } |
| 2450 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 2451 | static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port, |
| 2452 | struct switchdev_obj_port_fdb *fdb, |
| 2453 | int (*cb)(struct switchdev_obj *obj)) |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2454 | { |
| 2455 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2456 | struct mv88e6xxx_vtu_stu_entry vlan = { |
| 2457 | .vid = GLOBAL_VTU_VID_MASK, /* all ones */ |
| 2458 | }; |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 2459 | u16 fid; |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2460 | int err; |
| 2461 | |
Vivien Didelot | 2672f82 | 2016-05-09 13:22:48 -0400 | [diff] [blame] | 2462 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU)) |
| 2463 | return -EOPNOTSUPP; |
| 2464 | |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2465 | mutex_lock(&ps->smi_mutex); |
| 2466 | |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 2467 | /* Dump port's default Filtering Information Database (VLAN ID 0) */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2468 | err = _mv88e6xxx_port_fid_get(ps, port, &fid); |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 2469 | if (err) |
| 2470 | goto unlock; |
| 2471 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2472 | err = _mv88e6xxx_port_fdb_dump_one(ps, fid, 0, port, fdb, cb); |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 2473 | if (err) |
| 2474 | goto unlock; |
| 2475 | |
Vivien Didelot | 74b6ba0 | 2016-02-26 13:16:02 -0500 | [diff] [blame] | 2476 | /* Dump VLANs' Filtering Information Databases */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2477 | err = _mv88e6xxx_vtu_vid_write(ps, vlan.vid); |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2478 | if (err) |
| 2479 | goto unlock; |
| 2480 | |
| 2481 | do { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2482 | err = _mv88e6xxx_vtu_getnext(ps, &vlan); |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2483 | if (err) |
Vivien Didelot | 74b6ba0 | 2016-02-26 13:16:02 -0500 | [diff] [blame] | 2484 | break; |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2485 | |
| 2486 | if (!vlan.valid) |
| 2487 | break; |
| 2488 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2489 | err = _mv88e6xxx_port_fdb_dump_one(ps, vlan.fid, vlan.vid, port, |
Vivien Didelot | 74b6ba0 | 2016-02-26 13:16:02 -0500 | [diff] [blame] | 2490 | fdb, cb); |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2491 | if (err) |
Vivien Didelot | 74b6ba0 | 2016-02-26 13:16:02 -0500 | [diff] [blame] | 2492 | break; |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2493 | } while (vlan.vid < GLOBAL_VTU_VID_MASK); |
| 2494 | |
| 2495 | unlock: |
| 2496 | mutex_unlock(&ps->smi_mutex); |
| 2497 | |
| 2498 | return err; |
| 2499 | } |
| 2500 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 2501 | static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port, |
| 2502 | struct net_device *bridge) |
Vivien Didelot | e79a8bc | 2015-11-04 17:23:40 -0500 | [diff] [blame] | 2503 | { |
Vivien Didelot | a669275 | 2016-02-12 12:09:39 -0500 | [diff] [blame] | 2504 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Colin Ian King | 1d9619d | 2016-04-25 23:11:22 +0100 | [diff] [blame] | 2505 | int i, err = 0; |
Vivien Didelot | 466dfa0 | 2016-02-26 13:16:05 -0500 | [diff] [blame] | 2506 | |
Vivien Didelot | 936f234 | 2016-05-09 13:22:46 -0400 | [diff] [blame] | 2507 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VLANTABLE)) |
| 2508 | return -EOPNOTSUPP; |
| 2509 | |
Vivien Didelot | 466dfa0 | 2016-02-26 13:16:05 -0500 | [diff] [blame] | 2510 | mutex_lock(&ps->smi_mutex); |
| 2511 | |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2512 | /* Assign the bridge and remap each port's VLANTable */ |
Vivien Didelot | a669275 | 2016-02-12 12:09:39 -0500 | [diff] [blame] | 2513 | ps->ports[port].bridge_dev = bridge; |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2514 | |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 2515 | for (i = 0; i < ps->info->num_ports; ++i) { |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2516 | if (ps->ports[i].bridge_dev == bridge) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2517 | err = _mv88e6xxx_port_based_vlan_map(ps, i); |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2518 | if (err) |
| 2519 | break; |
| 2520 | } |
| 2521 | } |
| 2522 | |
Vivien Didelot | 466dfa0 | 2016-02-26 13:16:05 -0500 | [diff] [blame] | 2523 | mutex_unlock(&ps->smi_mutex); |
Vivien Didelot | a669275 | 2016-02-12 12:09:39 -0500 | [diff] [blame] | 2524 | |
Vivien Didelot | 466dfa0 | 2016-02-26 13:16:05 -0500 | [diff] [blame] | 2525 | return err; |
Vivien Didelot | e79a8bc | 2015-11-04 17:23:40 -0500 | [diff] [blame] | 2526 | } |
| 2527 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 2528 | static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port) |
Vivien Didelot | e79a8bc | 2015-11-04 17:23:40 -0500 | [diff] [blame] | 2529 | { |
Vivien Didelot | a669275 | 2016-02-12 12:09:39 -0500 | [diff] [blame] | 2530 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2531 | struct net_device *bridge = ps->ports[port].bridge_dev; |
Vivien Didelot | 16bfa70 | 2016-03-13 16:21:33 -0400 | [diff] [blame] | 2532 | int i; |
Vivien Didelot | 466dfa0 | 2016-02-26 13:16:05 -0500 | [diff] [blame] | 2533 | |
Vivien Didelot | 936f234 | 2016-05-09 13:22:46 -0400 | [diff] [blame] | 2534 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VLANTABLE)) |
| 2535 | return; |
| 2536 | |
Vivien Didelot | 466dfa0 | 2016-02-26 13:16:05 -0500 | [diff] [blame] | 2537 | mutex_lock(&ps->smi_mutex); |
| 2538 | |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2539 | /* Unassign the bridge and remap each port's VLANTable */ |
Vivien Didelot | a669275 | 2016-02-12 12:09:39 -0500 | [diff] [blame] | 2540 | ps->ports[port].bridge_dev = NULL; |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2541 | |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 2542 | for (i = 0; i < ps->info->num_ports; ++i) |
Vivien Didelot | 16bfa70 | 2016-03-13 16:21:33 -0400 | [diff] [blame] | 2543 | if (i == port || ps->ports[i].bridge_dev == bridge) |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2544 | if (_mv88e6xxx_port_based_vlan_map(ps, i)) |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 2545 | netdev_warn(ds->ports[i].netdev, |
| 2546 | "failed to remap\n"); |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2547 | |
Vivien Didelot | 466dfa0 | 2016-02-26 13:16:05 -0500 | [diff] [blame] | 2548 | mutex_unlock(&ps->smi_mutex); |
Vivien Didelot | 66d9cd0 | 2016-02-05 14:07:14 -0500 | [diff] [blame] | 2549 | } |
| 2550 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 2551 | static int _mv88e6xxx_mdio_page_write(struct mv88e6xxx_priv_state *ps, |
| 2552 | int port, int page, int reg, int val) |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 2553 | { |
| 2554 | int ret; |
| 2555 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 2556 | ret = mv88e6xxx_mdio_write_indirect(ps, port, 0x16, page); |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 2557 | if (ret < 0) |
| 2558 | goto restore_page_0; |
| 2559 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 2560 | ret = mv88e6xxx_mdio_write_indirect(ps, port, reg, val); |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 2561 | restore_page_0: |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 2562 | mv88e6xxx_mdio_write_indirect(ps, port, 0x16, 0x0); |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 2563 | |
| 2564 | return ret; |
| 2565 | } |
| 2566 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 2567 | static int _mv88e6xxx_mdio_page_read(struct mv88e6xxx_priv_state *ps, |
| 2568 | int port, int page, int reg) |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 2569 | { |
| 2570 | int ret; |
| 2571 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 2572 | ret = mv88e6xxx_mdio_write_indirect(ps, port, 0x16, page); |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 2573 | if (ret < 0) |
| 2574 | goto restore_page_0; |
| 2575 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 2576 | ret = mv88e6xxx_mdio_read_indirect(ps, port, reg); |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 2577 | restore_page_0: |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 2578 | mv88e6xxx_mdio_write_indirect(ps, port, 0x16, 0x0); |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 2579 | |
| 2580 | return ret; |
| 2581 | } |
| 2582 | |
Vivien Didelot | 552238b | 2016-05-09 13:22:49 -0400 | [diff] [blame] | 2583 | static int mv88e6xxx_switch_reset(struct mv88e6xxx_priv_state *ps) |
| 2584 | { |
| 2585 | bool ppu_active = mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU_ACTIVE); |
| 2586 | u16 is_reset = (ppu_active ? 0x8800 : 0xc800); |
Andrew Lunn | 52638f7 | 2016-05-10 23:27:22 +0200 | [diff] [blame] | 2587 | struct gpio_desc *gpiod = ps->reset; |
Vivien Didelot | 552238b | 2016-05-09 13:22:49 -0400 | [diff] [blame] | 2588 | unsigned long timeout; |
| 2589 | int ret; |
| 2590 | int i; |
| 2591 | |
| 2592 | /* Set all ports to the disabled state. */ |
| 2593 | for (i = 0; i < ps->info->num_ports; i++) { |
| 2594 | ret = _mv88e6xxx_reg_read(ps, REG_PORT(i), PORT_CONTROL); |
| 2595 | if (ret < 0) |
| 2596 | return ret; |
| 2597 | |
| 2598 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(i), PORT_CONTROL, |
| 2599 | ret & 0xfffc); |
| 2600 | if (ret) |
| 2601 | return ret; |
| 2602 | } |
| 2603 | |
| 2604 | /* Wait for transmit queues to drain. */ |
| 2605 | usleep_range(2000, 4000); |
| 2606 | |
| 2607 | /* If there is a gpio connected to the reset pin, toggle it */ |
| 2608 | if (gpiod) { |
| 2609 | gpiod_set_value_cansleep(gpiod, 1); |
| 2610 | usleep_range(10000, 20000); |
| 2611 | gpiod_set_value_cansleep(gpiod, 0); |
| 2612 | usleep_range(10000, 20000); |
| 2613 | } |
| 2614 | |
| 2615 | /* Reset the switch. Keep the PPU active if requested. The PPU |
| 2616 | * needs to be active to support indirect phy register access |
| 2617 | * through global registers 0x18 and 0x19. |
| 2618 | */ |
| 2619 | if (ppu_active) |
| 2620 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc000); |
| 2621 | else |
| 2622 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc400); |
| 2623 | if (ret) |
| 2624 | return ret; |
| 2625 | |
| 2626 | /* Wait up to one second for reset to complete. */ |
| 2627 | timeout = jiffies + 1 * HZ; |
| 2628 | while (time_before(jiffies, timeout)) { |
| 2629 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, 0x00); |
| 2630 | if (ret < 0) |
| 2631 | return ret; |
| 2632 | |
| 2633 | if ((ret & is_reset) == is_reset) |
| 2634 | break; |
| 2635 | usleep_range(1000, 2000); |
| 2636 | } |
| 2637 | if (time_after(jiffies, timeout)) |
| 2638 | ret = -ETIMEDOUT; |
| 2639 | else |
| 2640 | ret = 0; |
| 2641 | |
| 2642 | return ret; |
| 2643 | } |
| 2644 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2645 | static int mv88e6xxx_power_on_serdes(struct mv88e6xxx_priv_state *ps) |
Patrick Uiterwijk | 13a7ebb | 2016-03-30 01:39:41 +0000 | [diff] [blame] | 2646 | { |
| 2647 | int ret; |
| 2648 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 2649 | ret = _mv88e6xxx_mdio_page_read(ps, REG_FIBER_SERDES, |
| 2650 | PAGE_FIBER_SERDES, MII_BMCR); |
Patrick Uiterwijk | 13a7ebb | 2016-03-30 01:39:41 +0000 | [diff] [blame] | 2651 | if (ret < 0) |
| 2652 | return ret; |
| 2653 | |
| 2654 | if (ret & BMCR_PDOWN) { |
| 2655 | ret &= ~BMCR_PDOWN; |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 2656 | ret = _mv88e6xxx_mdio_page_write(ps, REG_FIBER_SERDES, |
| 2657 | PAGE_FIBER_SERDES, MII_BMCR, |
| 2658 | ret); |
Patrick Uiterwijk | 13a7ebb | 2016-03-30 01:39:41 +0000 | [diff] [blame] | 2659 | } |
| 2660 | |
| 2661 | return ret; |
| 2662 | } |
| 2663 | |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2664 | static int mv88e6xxx_setup_port(struct mv88e6xxx_priv_state *ps, int port) |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2665 | { |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2666 | struct dsa_switch *ds = ps->ds; |
Vivien Didelot | f02bdff | 2015-10-11 18:08:36 -0400 | [diff] [blame] | 2667 | int ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2668 | u16 reg; |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2669 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2670 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 2671 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) || |
| 2672 | mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) || |
| 2673 | mv88e6xxx_6065_family(ps) || mv88e6xxx_6320_family(ps)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2674 | /* MAC Forcing register: don't force link, speed, |
| 2675 | * duplex or flow control state to any particular |
| 2676 | * values on physical ports, but force the CPU port |
| 2677 | * and all DSA ports to their maximum bandwidth and |
| 2678 | * full duplex. |
| 2679 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2680 | reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_PCS_CTRL); |
Andrew Lunn | 60045cb | 2015-08-17 23:52:51 +0200 | [diff] [blame] | 2681 | 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] | 2682 | reg &= ~PORT_PCS_CTRL_UNFORCED; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2683 | reg |= PORT_PCS_CTRL_FORCE_LINK | |
| 2684 | PORT_PCS_CTRL_LINK_UP | |
| 2685 | PORT_PCS_CTRL_DUPLEX_FULL | |
| 2686 | PORT_PCS_CTRL_FORCE_DUPLEX; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2687 | if (mv88e6xxx_6065_family(ps)) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2688 | reg |= PORT_PCS_CTRL_100; |
| 2689 | else |
| 2690 | reg |= PORT_PCS_CTRL_1000; |
| 2691 | } else { |
| 2692 | reg |= PORT_PCS_CTRL_UNFORCED; |
| 2693 | } |
| 2694 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2695 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2696 | PORT_PCS_CTRL, reg); |
| 2697 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2698 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2699 | } |
| 2700 | |
| 2701 | /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock, |
| 2702 | * disable Header mode, enable IGMP/MLD snooping, disable VLAN |
| 2703 | * tunneling, determine priority by looking at 802.1p and IP |
| 2704 | * priority fields (IP prio has precedence), and set STP state |
| 2705 | * to Forwarding. |
| 2706 | * |
| 2707 | * If this is the CPU link, use DSA or EDSA tagging depending |
| 2708 | * on which tagging mode was configured. |
| 2709 | * |
| 2710 | * If this is a link to another switch, use DSA tagging mode. |
| 2711 | * |
| 2712 | * If this is the upstream port for this switch, enable |
| 2713 | * forwarding of unknown unicasts and multicasts. |
| 2714 | */ |
| 2715 | reg = 0; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2716 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 2717 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) || |
| 2718 | mv88e6xxx_6095_family(ps) || mv88e6xxx_6065_family(ps) || |
| 2719 | mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps)) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2720 | reg = PORT_CONTROL_IGMP_MLD_SNOOP | |
| 2721 | PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP | |
| 2722 | PORT_CONTROL_STATE_FORWARDING; |
| 2723 | if (dsa_is_cpu_port(ds, port)) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2724 | if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps)) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2725 | reg |= PORT_CONTROL_DSA_TAG; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2726 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 2727 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) || |
| 2728 | mv88e6xxx_6320_family(ps)) { |
Andrew Lunn | 5377b80 | 2016-06-04 21:17:02 +0200 | [diff] [blame] | 2729 | reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA | |
| 2730 | PORT_CONTROL_FORWARD_UNKNOWN | |
Andrew Lunn | c047a1f | 2015-09-29 01:50:56 +0200 | [diff] [blame] | 2731 | PORT_CONTROL_FORWARD_UNKNOWN_MC; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2732 | } |
| 2733 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2734 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 2735 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) || |
| 2736 | mv88e6xxx_6095_family(ps) || mv88e6xxx_6065_family(ps) || |
| 2737 | mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2738 | reg |= PORT_CONTROL_EGRESS_ADD_TAG; |
| 2739 | } |
| 2740 | } |
Andrew Lunn | 6083ce7 | 2015-08-17 23:52:52 +0200 | [diff] [blame] | 2741 | if (dsa_is_dsa_port(ds, port)) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2742 | if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps)) |
Andrew Lunn | 6083ce7 | 2015-08-17 23:52:52 +0200 | [diff] [blame] | 2743 | reg |= PORT_CONTROL_DSA_TAG; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2744 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 2745 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) || |
| 2746 | mv88e6xxx_6320_family(ps)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2747 | reg |= PORT_CONTROL_FRAME_MODE_DSA; |
Andrew Lunn | 6083ce7 | 2015-08-17 23:52:52 +0200 | [diff] [blame] | 2748 | } |
| 2749 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2750 | if (port == dsa_upstream_port(ds)) |
| 2751 | reg |= PORT_CONTROL_FORWARD_UNKNOWN | |
| 2752 | PORT_CONTROL_FORWARD_UNKNOWN_MC; |
| 2753 | } |
| 2754 | if (reg) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2755 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2756 | PORT_CONTROL, reg); |
| 2757 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2758 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2759 | } |
| 2760 | |
Patrick Uiterwijk | 13a7ebb | 2016-03-30 01:39:41 +0000 | [diff] [blame] | 2761 | /* If this port is connected to a SerDes, make sure the SerDes is not |
| 2762 | * powered down. |
| 2763 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2764 | if (mv88e6xxx_6352_family(ps)) { |
| 2765 | ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_STATUS); |
Patrick Uiterwijk | 13a7ebb | 2016-03-30 01:39:41 +0000 | [diff] [blame] | 2766 | if (ret < 0) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2767 | return ret; |
Patrick Uiterwijk | 13a7ebb | 2016-03-30 01:39:41 +0000 | [diff] [blame] | 2768 | ret &= PORT_STATUS_CMODE_MASK; |
| 2769 | if ((ret == PORT_STATUS_CMODE_100BASE_X) || |
| 2770 | (ret == PORT_STATUS_CMODE_1000BASE_X) || |
| 2771 | (ret == PORT_STATUS_CMODE_SGMII)) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2772 | ret = mv88e6xxx_power_on_serdes(ps); |
Patrick Uiterwijk | 13a7ebb | 2016-03-30 01:39:41 +0000 | [diff] [blame] | 2773 | if (ret < 0) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2774 | return ret; |
Patrick Uiterwijk | 13a7ebb | 2016-03-30 01:39:41 +0000 | [diff] [blame] | 2775 | } |
| 2776 | } |
| 2777 | |
Vivien Didelot | 8efdda4 | 2015-08-13 12:52:23 -0400 | [diff] [blame] | 2778 | /* Port Control 2: don't force a good FCS, set the maximum frame size to |
Vivien Didelot | 46fbe5e | 2016-02-26 13:16:07 -0500 | [diff] [blame] | 2779 | * 10240 bytes, disable 802.1q tags checking, don't discard tagged or |
Vivien Didelot | 8efdda4 | 2015-08-13 12:52:23 -0400 | [diff] [blame] | 2780 | * untagged frames on this port, do a destination address lookup on all |
| 2781 | * received packets as usual, disable ARP mirroring and don't send a |
| 2782 | * copy of all transmitted/received frames on this port to the CPU. |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2783 | */ |
| 2784 | reg = 0; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2785 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 2786 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) || |
| 2787 | mv88e6xxx_6095_family(ps) || mv88e6xxx_6320_family(ps) || |
| 2788 | mv88e6xxx_6185_family(ps)) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2789 | reg = PORT_CONTROL_2_MAP_DA; |
| 2790 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2791 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 2792 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6320_family(ps)) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2793 | reg |= PORT_CONTROL_2_JUMBO_10240; |
| 2794 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2795 | if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2796 | /* Set the upstream port this port should use */ |
| 2797 | reg |= dsa_upstream_port(ds); |
| 2798 | /* enable forwarding of unknown multicast addresses to |
| 2799 | * the upstream port |
| 2800 | */ |
| 2801 | if (port == dsa_upstream_port(ds)) |
| 2802 | reg |= PORT_CONTROL_2_FORWARD_UNKNOWN; |
| 2803 | } |
| 2804 | |
Vivien Didelot | 46fbe5e | 2016-02-26 13:16:07 -0500 | [diff] [blame] | 2805 | reg |= PORT_CONTROL_2_8021Q_DISABLED; |
Vivien Didelot | 8efdda4 | 2015-08-13 12:52:23 -0400 | [diff] [blame] | 2806 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2807 | if (reg) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2808 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2809 | PORT_CONTROL_2, reg); |
| 2810 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2811 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2812 | } |
| 2813 | |
| 2814 | /* Port Association Vector: when learning source addresses |
| 2815 | * of packets, add the address to the address database using |
| 2816 | * a port bitmap that has only the bit for this port set and |
| 2817 | * the other bits clear. |
| 2818 | */ |
Andrew Lunn | 4c7ea3c | 2015-11-03 10:52:36 -0500 | [diff] [blame] | 2819 | reg = 1 << port; |
Vivien Didelot | 996ecb8 | 2016-04-14 14:42:08 -0400 | [diff] [blame] | 2820 | /* Disable learning for CPU port */ |
| 2821 | if (dsa_is_cpu_port(ds, port)) |
Vivien Didelot | 65fa402 | 2016-04-14 14:42:07 -0400 | [diff] [blame] | 2822 | reg = 0; |
Andrew Lunn | 4c7ea3c | 2015-11-03 10:52:36 -0500 | [diff] [blame] | 2823 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2824 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_ASSOC_VECTOR, reg); |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2825 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2826 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2827 | |
| 2828 | /* Egress rate control 2: disable egress rate control. */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2829 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_RATE_CONTROL_2, |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2830 | 0x0000); |
| 2831 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2832 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2833 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2834 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 2835 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) || |
| 2836 | mv88e6xxx_6320_family(ps)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2837 | /* Do not limit the period of time that this port can |
| 2838 | * be paused for by the remote end or the period of |
| 2839 | * time that this port can pause the remote end. |
| 2840 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2841 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2842 | PORT_PAUSE_CTRL, 0x0000); |
| 2843 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2844 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2845 | |
| 2846 | /* Port ATU control: disable limiting the number of |
| 2847 | * address database entries that this port is allowed |
| 2848 | * to use. |
| 2849 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2850 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2851 | PORT_ATU_CONTROL, 0x0000); |
| 2852 | /* Priority Override: disable DA, SA and VTU priority |
| 2853 | * override. |
| 2854 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2855 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2856 | PORT_PRI_OVERRIDE, 0x0000); |
| 2857 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2858 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2859 | |
| 2860 | /* Port Ethertype: use the Ethertype DSA Ethertype |
| 2861 | * value. |
| 2862 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2863 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2864 | PORT_ETH_TYPE, ETH_P_EDSA); |
| 2865 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2866 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2867 | /* Tag Remap: use an identity 802.1p prio -> switch |
| 2868 | * prio mapping. |
| 2869 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2870 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2871 | PORT_TAG_REGMAP_0123, 0x3210); |
| 2872 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2873 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2874 | |
| 2875 | /* Tag Remap 2: use an identity 802.1p prio -> switch |
| 2876 | * prio mapping. |
| 2877 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2878 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2879 | PORT_TAG_REGMAP_4567, 0x7654); |
| 2880 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2881 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2882 | } |
| 2883 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2884 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 2885 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) || |
| 2886 | mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) || |
| 2887 | mv88e6xxx_6320_family(ps)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2888 | /* Rate Control: disable ingress rate limiting. */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2889 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2890 | PORT_RATE_CONTROL, 0x0001); |
| 2891 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2892 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2893 | } |
| 2894 | |
Guenter Roeck | 366f0a0 | 2015-03-26 18:36:30 -0700 | [diff] [blame] | 2895 | /* Port Control 1: disable trunking, disable sending |
| 2896 | * learning messages to this port. |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2897 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2898 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1, 0x0000); |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2899 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2900 | return ret; |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2901 | |
Vivien Didelot | 207afda | 2016-04-14 14:42:09 -0400 | [diff] [blame] | 2902 | /* Port based VLAN map: give each port the same default address |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2903 | * database, and allow bidirectional communication between the |
| 2904 | * CPU and DSA port(s), and the other ports. |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2905 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2906 | ret = _mv88e6xxx_port_fid_set(ps, port, 0); |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 2907 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2908 | return ret; |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 2909 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2910 | ret = _mv88e6xxx_port_based_vlan_map(ps, port); |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2911 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2912 | return ret; |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2913 | |
| 2914 | /* Default VLAN ID and priority: don't set a default VLAN |
| 2915 | * ID, and set the default packet priority to zero. |
| 2916 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2917 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_DEFAULT_VLAN, |
Vivien Didelot | 47cf1e65 | 2015-04-20 17:43:26 -0400 | [diff] [blame] | 2918 | 0x0000); |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2919 | if (ret) |
| 2920 | return ret; |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2921 | |
Andrew Lunn | dbde9e6 | 2015-05-06 01:09:48 +0200 | [diff] [blame] | 2922 | return 0; |
| 2923 | } |
| 2924 | |
Vivien Didelot | 08a0126 | 2016-05-09 13:22:50 -0400 | [diff] [blame] | 2925 | static int mv88e6xxx_setup_global(struct mv88e6xxx_priv_state *ps) |
| 2926 | { |
Vivien Didelot | b0745e87 | 2016-05-09 13:22:53 -0400 | [diff] [blame] | 2927 | struct dsa_switch *ds = ps->ds; |
| 2928 | u32 upstream_port = dsa_upstream_port(ds); |
Vivien Didelot | 119477b | 2016-05-09 13:22:51 -0400 | [diff] [blame] | 2929 | u16 reg; |
Vivien Didelot | 08a0126 | 2016-05-09 13:22:50 -0400 | [diff] [blame] | 2930 | int err; |
| 2931 | int i; |
| 2932 | |
Vivien Didelot | 119477b | 2016-05-09 13:22:51 -0400 | [diff] [blame] | 2933 | /* Enable the PHY Polling Unit if present, don't discard any packets, |
| 2934 | * and mask all interrupt sources. |
| 2935 | */ |
| 2936 | reg = 0; |
| 2937 | if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU) || |
| 2938 | mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU_ACTIVE)) |
| 2939 | reg |= GLOBAL_CONTROL_PPU_ENABLE; |
| 2940 | |
| 2941 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL, reg); |
| 2942 | if (err) |
| 2943 | return err; |
| 2944 | |
Vivien Didelot | b0745e87 | 2016-05-09 13:22:53 -0400 | [diff] [blame] | 2945 | /* Configure the upstream port, and configure it as the port to which |
| 2946 | * ingress and egress and ARP monitor frames are to be sent. |
| 2947 | */ |
| 2948 | reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT | |
| 2949 | upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT | |
| 2950 | upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT; |
| 2951 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MONITOR_CONTROL, reg); |
| 2952 | if (err) |
| 2953 | return err; |
| 2954 | |
Vivien Didelot | 50484ff | 2016-05-09 13:22:54 -0400 | [diff] [blame] | 2955 | /* Disable remote management, and set the switch's DSA device number. */ |
| 2956 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL_2, |
| 2957 | GLOBAL_CONTROL_2_MULTIPLE_CASCADE | |
| 2958 | (ds->index & 0x1f)); |
| 2959 | if (err) |
| 2960 | return err; |
| 2961 | |
Vivien Didelot | 08a0126 | 2016-05-09 13:22:50 -0400 | [diff] [blame] | 2962 | /* Set the default address aging time to 5 minutes, and |
| 2963 | * enable address learn messages to be sent to all message |
| 2964 | * ports. |
| 2965 | */ |
| 2966 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL, |
| 2967 | 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL); |
| 2968 | if (err) |
| 2969 | return err; |
| 2970 | |
| 2971 | /* Configure the IP ToS mapping registers. */ |
| 2972 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000); |
| 2973 | if (err) |
| 2974 | return err; |
| 2975 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000); |
| 2976 | if (err) |
| 2977 | return err; |
| 2978 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555); |
| 2979 | if (err) |
| 2980 | return err; |
| 2981 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555); |
| 2982 | if (err) |
| 2983 | return err; |
| 2984 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa); |
| 2985 | if (err) |
| 2986 | return err; |
| 2987 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa); |
| 2988 | if (err) |
| 2989 | return err; |
| 2990 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff); |
| 2991 | if (err) |
| 2992 | return err; |
| 2993 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff); |
| 2994 | if (err) |
| 2995 | return err; |
| 2996 | |
| 2997 | /* Configure the IEEE 802.1p priority mapping register. */ |
| 2998 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41); |
| 2999 | if (err) |
| 3000 | return err; |
| 3001 | |
| 3002 | /* Send all frames with destination addresses matching |
| 3003 | * 01:80:c2:00:00:0x to the CPU port. |
| 3004 | */ |
| 3005 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff); |
| 3006 | if (err) |
| 3007 | return err; |
| 3008 | |
| 3009 | /* Ignore removed tag data on doubly tagged packets, disable |
| 3010 | * flow control messages, force flow control priority to the |
| 3011 | * highest, and send all special multicast frames to the CPU |
| 3012 | * port at the highest priority. |
| 3013 | */ |
| 3014 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT, |
| 3015 | 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 | |
| 3016 | GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI); |
| 3017 | if (err) |
| 3018 | return err; |
| 3019 | |
| 3020 | /* Program the DSA routing table. */ |
| 3021 | for (i = 0; i < 32; i++) { |
| 3022 | int nexthop = 0x1f; |
| 3023 | |
Andrew Lunn | 66472fc | 2016-06-04 21:17:00 +0200 | [diff] [blame] | 3024 | if (i != ds->index && i < DSA_MAX_SWITCHES) |
| 3025 | nexthop = ds->rtable[i] & 0x1f; |
Vivien Didelot | 08a0126 | 2016-05-09 13:22:50 -0400 | [diff] [blame] | 3026 | |
| 3027 | err = _mv88e6xxx_reg_write( |
| 3028 | ps, REG_GLOBAL2, |
| 3029 | GLOBAL2_DEVICE_MAPPING, |
| 3030 | GLOBAL2_DEVICE_MAPPING_UPDATE | |
| 3031 | (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) | nexthop); |
| 3032 | if (err) |
| 3033 | return err; |
| 3034 | } |
| 3035 | |
| 3036 | /* Clear all trunk masks. */ |
| 3037 | for (i = 0; i < 8; i++) { |
| 3038 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_TRUNK_MASK, |
| 3039 | 0x8000 | |
| 3040 | (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) | |
| 3041 | ((1 << ps->info->num_ports) - 1)); |
| 3042 | if (err) |
| 3043 | return err; |
| 3044 | } |
| 3045 | |
| 3046 | /* Clear all trunk mappings. */ |
| 3047 | for (i = 0; i < 16; i++) { |
| 3048 | err = _mv88e6xxx_reg_write( |
| 3049 | ps, REG_GLOBAL2, |
| 3050 | GLOBAL2_TRUNK_MAPPING, |
| 3051 | GLOBAL2_TRUNK_MAPPING_UPDATE | |
| 3052 | (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT)); |
| 3053 | if (err) |
| 3054 | return err; |
| 3055 | } |
| 3056 | |
| 3057 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 3058 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) || |
| 3059 | mv88e6xxx_6320_family(ps)) { |
| 3060 | /* Send all frames with destination addresses matching |
| 3061 | * 01:80:c2:00:00:2x to the CPU port. |
| 3062 | */ |
| 3063 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, |
| 3064 | GLOBAL2_MGMT_EN_2X, 0xffff); |
| 3065 | if (err) |
| 3066 | return err; |
| 3067 | |
| 3068 | /* Initialise cross-chip port VLAN table to reset |
| 3069 | * defaults. |
| 3070 | */ |
| 3071 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, |
| 3072 | GLOBAL2_PVT_ADDR, 0x9000); |
| 3073 | if (err) |
| 3074 | return err; |
| 3075 | |
| 3076 | /* Clear the priority override table. */ |
| 3077 | for (i = 0; i < 16; i++) { |
| 3078 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, |
| 3079 | GLOBAL2_PRIO_OVERRIDE, |
| 3080 | 0x8000 | (i << 8)); |
| 3081 | if (err) |
| 3082 | return err; |
| 3083 | } |
| 3084 | } |
| 3085 | |
| 3086 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 3087 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) || |
| 3088 | mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) || |
| 3089 | mv88e6xxx_6320_family(ps)) { |
| 3090 | /* Disable ingress rate limiting by resetting all |
| 3091 | * ingress rate limit registers to their initial |
| 3092 | * state. |
| 3093 | */ |
| 3094 | for (i = 0; i < ps->info->num_ports; i++) { |
| 3095 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, |
| 3096 | GLOBAL2_INGRESS_OP, |
| 3097 | 0x9000 | (i << 8)); |
| 3098 | if (err) |
| 3099 | return err; |
| 3100 | } |
| 3101 | } |
| 3102 | |
| 3103 | /* Clear the statistics counters for all ports */ |
| 3104 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP, |
| 3105 | GLOBAL_STATS_OP_FLUSH_ALL); |
| 3106 | if (err) |
| 3107 | return err; |
| 3108 | |
| 3109 | /* Wait for the flush to complete. */ |
| 3110 | err = _mv88e6xxx_stats_wait(ps); |
| 3111 | if (err) |
| 3112 | return err; |
| 3113 | |
| 3114 | /* Clear all ATU entries */ |
| 3115 | err = _mv88e6xxx_atu_flush(ps, 0, true); |
| 3116 | if (err) |
| 3117 | return err; |
| 3118 | |
| 3119 | /* Clear all the VTU and STU entries */ |
| 3120 | err = _mv88e6xxx_vtu_stu_flush(ps); |
| 3121 | if (err < 0) |
| 3122 | return err; |
| 3123 | |
| 3124 | return err; |
| 3125 | } |
| 3126 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3127 | static int mv88e6xxx_setup(struct dsa_switch *ds) |
Guenter Roeck | acdaffc | 2015-03-26 18:36:28 -0700 | [diff] [blame] | 3128 | { |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 3129 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | 552238b | 2016-05-09 13:22:49 -0400 | [diff] [blame] | 3130 | int err; |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 3131 | int i; |
| 3132 | |
| 3133 | ps->ds = ds; |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame^] | 3134 | ds->slave_mii_bus = ps->mdio_bus; |
Vivien Didelot | 552238b | 2016-05-09 13:22:49 -0400 | [diff] [blame] | 3135 | |
Vivien Didelot | d24645b | 2016-05-09 13:22:41 -0400 | [diff] [blame] | 3136 | if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM)) |
| 3137 | mutex_init(&ps->eeprom_mutex); |
| 3138 | |
Vivien Didelot | 552238b | 2016-05-09 13:22:49 -0400 | [diff] [blame] | 3139 | mutex_lock(&ps->smi_mutex); |
| 3140 | |
| 3141 | err = mv88e6xxx_switch_reset(ps); |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 3142 | if (err) |
| 3143 | goto unlock; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 3144 | |
Vivien Didelot | 08a0126 | 2016-05-09 13:22:50 -0400 | [diff] [blame] | 3145 | err = mv88e6xxx_setup_global(ps); |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 3146 | if (err) |
| 3147 | goto unlock; |
| 3148 | |
| 3149 | for (i = 0; i < ps->info->num_ports; i++) { |
| 3150 | err = mv88e6xxx_setup_port(ps, i); |
| 3151 | if (err) |
| 3152 | goto unlock; |
| 3153 | } |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 3154 | |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 3155 | unlock: |
Vivien Didelot | 24751e2 | 2015-08-03 09:17:44 -0400 | [diff] [blame] | 3156 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | db687a5 | 2015-06-20 21:31:29 +0200 | [diff] [blame] | 3157 | |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 3158 | return err; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 3159 | } |
| 3160 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3161 | int mv88e6xxx_mdio_page_read(struct dsa_switch *ds, int port, int page, int reg) |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 3162 | { |
| 3163 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 3164 | int ret; |
| 3165 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 3166 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3167 | ret = _mv88e6xxx_mdio_page_read(ps, port, page, reg); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 3168 | mutex_unlock(&ps->smi_mutex); |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 3169 | |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 3170 | return ret; |
| 3171 | } |
| 3172 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3173 | int mv88e6xxx_mdio_page_write(struct dsa_switch *ds, int port, int page, |
| 3174 | int reg, int val) |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 3175 | { |
| 3176 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 3177 | int ret; |
| 3178 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 3179 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3180 | ret = _mv88e6xxx_mdio_page_write(ps, port, page, reg, val); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 3181 | mutex_unlock(&ps->smi_mutex); |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 3182 | |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3183 | return ret; |
| 3184 | } |
| 3185 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3186 | static int mv88e6xxx_port_to_mdio_addr(struct mv88e6xxx_priv_state *ps, |
| 3187 | int port) |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3188 | { |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 3189 | if (port >= 0 && port < ps->info->num_ports) |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3190 | return port; |
| 3191 | return -EINVAL; |
| 3192 | } |
| 3193 | |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame^] | 3194 | static int mv88e6xxx_mdio_read(struct mii_bus *bus, int port, int regnum) |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3195 | { |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame^] | 3196 | struct mv88e6xxx_priv_state *ps = bus->priv; |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3197 | int addr = mv88e6xxx_port_to_mdio_addr(ps, port); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3198 | int ret; |
| 3199 | |
| 3200 | if (addr < 0) |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 3201 | return 0xffff; |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3202 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 3203 | mutex_lock(&ps->smi_mutex); |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 3204 | |
| 3205 | if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU)) |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3206 | ret = mv88e6xxx_mdio_read_ppu(ps, addr, regnum); |
Vivien Didelot | 6d5834a | 2016-05-09 13:22:40 -0400 | [diff] [blame] | 3207 | else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY)) |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3208 | ret = mv88e6xxx_mdio_read_indirect(ps, addr, regnum); |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 3209 | else |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3210 | ret = mv88e6xxx_mdio_read_direct(ps, addr, regnum); |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 3211 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 3212 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3213 | return ret; |
| 3214 | } |
| 3215 | |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame^] | 3216 | static int mv88e6xxx_mdio_write(struct mii_bus *bus, int port, int regnum, |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3217 | u16 val) |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3218 | { |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame^] | 3219 | struct mv88e6xxx_priv_state *ps = bus->priv; |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3220 | int addr = mv88e6xxx_port_to_mdio_addr(ps, port); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3221 | int ret; |
| 3222 | |
| 3223 | if (addr < 0) |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 3224 | return 0xffff; |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3225 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 3226 | mutex_lock(&ps->smi_mutex); |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 3227 | |
| 3228 | if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU)) |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3229 | ret = mv88e6xxx_mdio_write_ppu(ps, addr, regnum, val); |
Vivien Didelot | 6d5834a | 2016-05-09 13:22:40 -0400 | [diff] [blame] | 3230 | else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY)) |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3231 | ret = mv88e6xxx_mdio_write_indirect(ps, addr, regnum, val); |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 3232 | else |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3233 | ret = mv88e6xxx_mdio_write_direct(ps, addr, regnum, val); |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 3234 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 3235 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3236 | return ret; |
| 3237 | } |
| 3238 | |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame^] | 3239 | static int mv88e6xxx_mdio_register(struct mv88e6xxx_priv_state *ps, |
| 3240 | struct device_node *np) |
| 3241 | { |
| 3242 | static int index; |
| 3243 | struct mii_bus *bus; |
| 3244 | int err; |
| 3245 | |
| 3246 | if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU)) |
| 3247 | mv88e6xxx_ppu_state_init(ps); |
| 3248 | |
| 3249 | if (np) |
| 3250 | ps->mdio_np = of_get_child_by_name(np, "mdio"); |
| 3251 | |
| 3252 | bus = devm_mdiobus_alloc(ps->dev); |
| 3253 | if (!bus) |
| 3254 | return -ENOMEM; |
| 3255 | |
| 3256 | bus->priv = (void *)ps; |
| 3257 | if (np) { |
| 3258 | bus->name = np->full_name; |
| 3259 | snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name); |
| 3260 | } else { |
| 3261 | bus->name = "mv88e6xxx SMI"; |
| 3262 | snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++); |
| 3263 | } |
| 3264 | |
| 3265 | bus->read = mv88e6xxx_mdio_read; |
| 3266 | bus->write = mv88e6xxx_mdio_write; |
| 3267 | bus->parent = ps->dev; |
| 3268 | |
| 3269 | if (ps->mdio_np) |
| 3270 | err = of_mdiobus_register(bus, ps->mdio_np); |
| 3271 | else |
| 3272 | err = mdiobus_register(bus); |
| 3273 | if (err) { |
| 3274 | dev_err(ps->dev, "Cannot register MDIO bus (%d)\n", err); |
| 3275 | goto out; |
| 3276 | } |
| 3277 | ps->mdio_bus = bus; |
| 3278 | |
| 3279 | return 0; |
| 3280 | |
| 3281 | out: |
| 3282 | if (ps->mdio_np) |
| 3283 | of_node_put(ps->mdio_np); |
| 3284 | |
| 3285 | return err; |
| 3286 | } |
| 3287 | |
| 3288 | static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_priv_state *ps) |
| 3289 | |
| 3290 | { |
| 3291 | struct mii_bus *bus = ps->mdio_bus; |
| 3292 | |
| 3293 | mdiobus_unregister(bus); |
| 3294 | |
| 3295 | if (ps->mdio_np) |
| 3296 | of_node_put(ps->mdio_np); |
| 3297 | } |
| 3298 | |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3299 | #ifdef CONFIG_NET_DSA_HWMON |
| 3300 | |
| 3301 | static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp) |
| 3302 | { |
| 3303 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 3304 | int ret; |
| 3305 | int val; |
| 3306 | |
| 3307 | *temp = 0; |
| 3308 | |
| 3309 | mutex_lock(&ps->smi_mutex); |
| 3310 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3311 | ret = mv88e6xxx_mdio_write_direct(ps, 0x0, 0x16, 0x6); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3312 | if (ret < 0) |
| 3313 | goto error; |
| 3314 | |
| 3315 | /* Enable temperature sensor */ |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3316 | ret = mv88e6xxx_mdio_read_direct(ps, 0x0, 0x1a); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3317 | if (ret < 0) |
| 3318 | goto error; |
| 3319 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3320 | ret = mv88e6xxx_mdio_write_direct(ps, 0x0, 0x1a, ret | (1 << 5)); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3321 | if (ret < 0) |
| 3322 | goto error; |
| 3323 | |
| 3324 | /* Wait for temperature to stabilize */ |
| 3325 | usleep_range(10000, 12000); |
| 3326 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3327 | val = mv88e6xxx_mdio_read_direct(ps, 0x0, 0x1a); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3328 | if (val < 0) { |
| 3329 | ret = val; |
| 3330 | goto error; |
| 3331 | } |
| 3332 | |
| 3333 | /* Disable temperature sensor */ |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3334 | ret = mv88e6xxx_mdio_write_direct(ps, 0x0, 0x1a, ret & ~(1 << 5)); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3335 | if (ret < 0) |
| 3336 | goto error; |
| 3337 | |
| 3338 | *temp = ((val & 0x1f) - 5) * 5; |
| 3339 | |
| 3340 | error: |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3341 | mv88e6xxx_mdio_write_direct(ps, 0x0, 0x16, 0x0); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3342 | mutex_unlock(&ps->smi_mutex); |
| 3343 | return ret; |
| 3344 | } |
| 3345 | |
| 3346 | static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp) |
| 3347 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 3348 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 3349 | int phy = mv88e6xxx_6320_family(ps) ? 3 : 0; |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3350 | int ret; |
| 3351 | |
| 3352 | *temp = 0; |
| 3353 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3354 | ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 27); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3355 | if (ret < 0) |
| 3356 | return ret; |
| 3357 | |
| 3358 | *temp = (ret & 0xff) - 25; |
| 3359 | |
| 3360 | return 0; |
| 3361 | } |
| 3362 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3363 | static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp) |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3364 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 3365 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 3366 | |
Vivien Didelot | 6594f61 | 2016-05-09 13:22:42 -0400 | [diff] [blame] | 3367 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP)) |
| 3368 | return -EOPNOTSUPP; |
| 3369 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 3370 | if (mv88e6xxx_6320_family(ps) || mv88e6xxx_6352_family(ps)) |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3371 | return mv88e63xx_get_temp(ds, temp); |
| 3372 | |
| 3373 | return mv88e61xx_get_temp(ds, temp); |
| 3374 | } |
| 3375 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3376 | static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp) |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3377 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 3378 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 3379 | int phy = mv88e6xxx_6320_family(ps) ? 3 : 0; |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3380 | int ret; |
| 3381 | |
Vivien Didelot | 6594f61 | 2016-05-09 13:22:42 -0400 | [diff] [blame] | 3382 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT)) |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3383 | return -EOPNOTSUPP; |
| 3384 | |
| 3385 | *temp = 0; |
| 3386 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3387 | ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3388 | if (ret < 0) |
| 3389 | return ret; |
| 3390 | |
| 3391 | *temp = (((ret >> 8) & 0x1f) * 5) - 25; |
| 3392 | |
| 3393 | return 0; |
| 3394 | } |
| 3395 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3396 | static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp) |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3397 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 3398 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 3399 | int phy = mv88e6xxx_6320_family(ps) ? 3 : 0; |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3400 | int ret; |
| 3401 | |
Vivien Didelot | 6594f61 | 2016-05-09 13:22:42 -0400 | [diff] [blame] | 3402 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT)) |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3403 | return -EOPNOTSUPP; |
| 3404 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3405 | ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3406 | if (ret < 0) |
| 3407 | return ret; |
| 3408 | temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f); |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3409 | return mv88e6xxx_mdio_page_write(ds, phy, 6, 26, |
| 3410 | (ret & 0xe0ff) | (temp << 8)); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3411 | } |
| 3412 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3413 | static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm) |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3414 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 3415 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 3416 | int phy = mv88e6xxx_6320_family(ps) ? 3 : 0; |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3417 | int ret; |
| 3418 | |
Vivien Didelot | 6594f61 | 2016-05-09 13:22:42 -0400 | [diff] [blame] | 3419 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT)) |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3420 | return -EOPNOTSUPP; |
| 3421 | |
| 3422 | *alarm = false; |
| 3423 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3424 | ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3425 | if (ret < 0) |
| 3426 | return ret; |
| 3427 | |
| 3428 | *alarm = !!(ret & 0x40); |
| 3429 | |
| 3430 | return 0; |
| 3431 | } |
| 3432 | #endif /* CONFIG_NET_DSA_HWMON */ |
| 3433 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3434 | static const struct mv88e6xxx_info mv88e6xxx_table[] = { |
| 3435 | [MV88E6085] = { |
| 3436 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6085, |
| 3437 | .family = MV88E6XXX_FAMILY_6097, |
| 3438 | .name = "Marvell 88E6085", |
| 3439 | .num_databases = 4096, |
| 3440 | .num_ports = 10, |
| 3441 | .flags = MV88E6XXX_FLAGS_FAMILY_6097, |
| 3442 | }, |
| 3443 | |
| 3444 | [MV88E6095] = { |
| 3445 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6095, |
| 3446 | .family = MV88E6XXX_FAMILY_6095, |
| 3447 | .name = "Marvell 88E6095/88E6095F", |
| 3448 | .num_databases = 256, |
| 3449 | .num_ports = 11, |
| 3450 | .flags = MV88E6XXX_FLAGS_FAMILY_6095, |
| 3451 | }, |
| 3452 | |
| 3453 | [MV88E6123] = { |
| 3454 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6123, |
| 3455 | .family = MV88E6XXX_FAMILY_6165, |
| 3456 | .name = "Marvell 88E6123", |
| 3457 | .num_databases = 4096, |
| 3458 | .num_ports = 3, |
| 3459 | .flags = MV88E6XXX_FLAGS_FAMILY_6165, |
| 3460 | }, |
| 3461 | |
| 3462 | [MV88E6131] = { |
| 3463 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6131, |
| 3464 | .family = MV88E6XXX_FAMILY_6185, |
| 3465 | .name = "Marvell 88E6131", |
| 3466 | .num_databases = 256, |
| 3467 | .num_ports = 8, |
| 3468 | .flags = MV88E6XXX_FLAGS_FAMILY_6185, |
| 3469 | }, |
| 3470 | |
| 3471 | [MV88E6161] = { |
| 3472 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6161, |
| 3473 | .family = MV88E6XXX_FAMILY_6165, |
| 3474 | .name = "Marvell 88E6161", |
| 3475 | .num_databases = 4096, |
| 3476 | .num_ports = 6, |
| 3477 | .flags = MV88E6XXX_FLAGS_FAMILY_6165, |
| 3478 | }, |
| 3479 | |
| 3480 | [MV88E6165] = { |
| 3481 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6165, |
| 3482 | .family = MV88E6XXX_FAMILY_6165, |
| 3483 | .name = "Marvell 88E6165", |
| 3484 | .num_databases = 4096, |
| 3485 | .num_ports = 6, |
| 3486 | .flags = MV88E6XXX_FLAGS_FAMILY_6165, |
| 3487 | }, |
| 3488 | |
| 3489 | [MV88E6171] = { |
| 3490 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6171, |
| 3491 | .family = MV88E6XXX_FAMILY_6351, |
| 3492 | .name = "Marvell 88E6171", |
| 3493 | .num_databases = 4096, |
| 3494 | .num_ports = 7, |
| 3495 | .flags = MV88E6XXX_FLAGS_FAMILY_6351, |
| 3496 | }, |
| 3497 | |
| 3498 | [MV88E6172] = { |
| 3499 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6172, |
| 3500 | .family = MV88E6XXX_FAMILY_6352, |
| 3501 | .name = "Marvell 88E6172", |
| 3502 | .num_databases = 4096, |
| 3503 | .num_ports = 7, |
| 3504 | .flags = MV88E6XXX_FLAGS_FAMILY_6352, |
| 3505 | }, |
| 3506 | |
| 3507 | [MV88E6175] = { |
| 3508 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6175, |
| 3509 | .family = MV88E6XXX_FAMILY_6351, |
| 3510 | .name = "Marvell 88E6175", |
| 3511 | .num_databases = 4096, |
| 3512 | .num_ports = 7, |
| 3513 | .flags = MV88E6XXX_FLAGS_FAMILY_6351, |
| 3514 | }, |
| 3515 | |
| 3516 | [MV88E6176] = { |
| 3517 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6176, |
| 3518 | .family = MV88E6XXX_FAMILY_6352, |
| 3519 | .name = "Marvell 88E6176", |
| 3520 | .num_databases = 4096, |
| 3521 | .num_ports = 7, |
| 3522 | .flags = MV88E6XXX_FLAGS_FAMILY_6352, |
| 3523 | }, |
| 3524 | |
| 3525 | [MV88E6185] = { |
| 3526 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6185, |
| 3527 | .family = MV88E6XXX_FAMILY_6185, |
| 3528 | .name = "Marvell 88E6185", |
| 3529 | .num_databases = 256, |
| 3530 | .num_ports = 10, |
| 3531 | .flags = MV88E6XXX_FLAGS_FAMILY_6185, |
| 3532 | }, |
| 3533 | |
| 3534 | [MV88E6240] = { |
| 3535 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6240, |
| 3536 | .family = MV88E6XXX_FAMILY_6352, |
| 3537 | .name = "Marvell 88E6240", |
| 3538 | .num_databases = 4096, |
| 3539 | .num_ports = 7, |
| 3540 | .flags = MV88E6XXX_FLAGS_FAMILY_6352, |
| 3541 | }, |
| 3542 | |
| 3543 | [MV88E6320] = { |
| 3544 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6320, |
| 3545 | .family = MV88E6XXX_FAMILY_6320, |
| 3546 | .name = "Marvell 88E6320", |
| 3547 | .num_databases = 4096, |
| 3548 | .num_ports = 7, |
| 3549 | .flags = MV88E6XXX_FLAGS_FAMILY_6320, |
| 3550 | }, |
| 3551 | |
| 3552 | [MV88E6321] = { |
| 3553 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6321, |
| 3554 | .family = MV88E6XXX_FAMILY_6320, |
| 3555 | .name = "Marvell 88E6321", |
| 3556 | .num_databases = 4096, |
| 3557 | .num_ports = 7, |
| 3558 | .flags = MV88E6XXX_FLAGS_FAMILY_6320, |
| 3559 | }, |
| 3560 | |
| 3561 | [MV88E6350] = { |
| 3562 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6350, |
| 3563 | .family = MV88E6XXX_FAMILY_6351, |
| 3564 | .name = "Marvell 88E6350", |
| 3565 | .num_databases = 4096, |
| 3566 | .num_ports = 7, |
| 3567 | .flags = MV88E6XXX_FLAGS_FAMILY_6351, |
| 3568 | }, |
| 3569 | |
| 3570 | [MV88E6351] = { |
| 3571 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6351, |
| 3572 | .family = MV88E6XXX_FAMILY_6351, |
| 3573 | .name = "Marvell 88E6351", |
| 3574 | .num_databases = 4096, |
| 3575 | .num_ports = 7, |
| 3576 | .flags = MV88E6XXX_FLAGS_FAMILY_6351, |
| 3577 | }, |
| 3578 | |
| 3579 | [MV88E6352] = { |
| 3580 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6352, |
| 3581 | .family = MV88E6XXX_FAMILY_6352, |
| 3582 | .name = "Marvell 88E6352", |
| 3583 | .num_databases = 4096, |
| 3584 | .num_ports = 7, |
| 3585 | .flags = MV88E6XXX_FLAGS_FAMILY_6352, |
| 3586 | }, |
| 3587 | }; |
| 3588 | |
Vivien Didelot | f6271e6 | 2016-04-17 13:23:59 -0400 | [diff] [blame] | 3589 | static const struct mv88e6xxx_info * |
| 3590 | mv88e6xxx_lookup_info(unsigned int prod_num, const struct mv88e6xxx_info *table, |
Vivien Didelot | 0209d14 | 2016-04-17 13:23:55 -0400 | [diff] [blame] | 3591 | unsigned int num) |
Vivien Didelot | b9b3771 | 2015-10-30 19:39:48 -0400 | [diff] [blame] | 3592 | { |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3593 | int i; |
Vivien Didelot | b9b3771 | 2015-10-30 19:39:48 -0400 | [diff] [blame] | 3594 | |
Vivien Didelot | b9b3771 | 2015-10-30 19:39:48 -0400 | [diff] [blame] | 3595 | for (i = 0; i < num; ++i) |
Vivien Didelot | f6271e6 | 2016-04-17 13:23:59 -0400 | [diff] [blame] | 3596 | if (table[i].prod_num == prod_num) |
| 3597 | return &table[i]; |
Vivien Didelot | b9b3771 | 2015-10-30 19:39:48 -0400 | [diff] [blame] | 3598 | |
Vivien Didelot | b9b3771 | 2015-10-30 19:39:48 -0400 | [diff] [blame] | 3599 | return NULL; |
| 3600 | } |
| 3601 | |
Andrew Lunn | fcdce7d | 2016-05-10 23:27:20 +0200 | [diff] [blame] | 3602 | static const char *mv88e6xxx_drv_probe(struct device *dsa_dev, |
| 3603 | struct device *host_dev, int sw_addr, |
| 3604 | void **priv) |
Andrew Lunn | a77d43f | 2016-04-13 02:40:42 +0200 | [diff] [blame] | 3605 | { |
Vivien Didelot | f6271e6 | 2016-04-17 13:23:59 -0400 | [diff] [blame] | 3606 | const struct mv88e6xxx_info *info; |
Andrew Lunn | a77d43f | 2016-04-13 02:40:42 +0200 | [diff] [blame] | 3607 | struct mv88e6xxx_priv_state *ps; |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3608 | struct mii_bus *bus; |
Vivien Didelot | 0209d14 | 2016-04-17 13:23:55 -0400 | [diff] [blame] | 3609 | const char *name; |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3610 | int id, prod_num, rev; |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame^] | 3611 | int err; |
Andrew Lunn | a77d43f | 2016-04-13 02:40:42 +0200 | [diff] [blame] | 3612 | |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3613 | bus = dsa_host_dev_to_mii_bus(host_dev); |
Andrew Lunn | c156913 | 2016-04-13 02:40:45 +0200 | [diff] [blame] | 3614 | if (!bus) |
| 3615 | return NULL; |
| 3616 | |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3617 | id = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), PORT_SWITCH_ID); |
| 3618 | if (id < 0) |
| 3619 | return NULL; |
| 3620 | |
| 3621 | prod_num = (id & 0xfff0) >> 4; |
| 3622 | rev = id & 0x000f; |
| 3623 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3624 | info = mv88e6xxx_lookup_info(prod_num, mv88e6xxx_table, |
| 3625 | ARRAY_SIZE(mv88e6xxx_table)); |
Vivien Didelot | f6271e6 | 2016-04-17 13:23:59 -0400 | [diff] [blame] | 3626 | if (!info) |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3627 | return NULL; |
| 3628 | |
Vivien Didelot | f6271e6 | 2016-04-17 13:23:59 -0400 | [diff] [blame] | 3629 | name = info->name; |
| 3630 | |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3631 | ps = devm_kzalloc(dsa_dev, sizeof(*ps), GFP_KERNEL); |
| 3632 | if (!ps) |
| 3633 | return NULL; |
| 3634 | |
| 3635 | ps->bus = bus; |
| 3636 | ps->sw_addr = sw_addr; |
Vivien Didelot | f6271e6 | 2016-04-17 13:23:59 -0400 | [diff] [blame] | 3637 | ps->info = info; |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame^] | 3638 | ps->dev = dsa_dev; |
Andrew Lunn | b681957 | 2016-05-10 23:27:19 +0200 | [diff] [blame] | 3639 | mutex_init(&ps->smi_mutex); |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3640 | |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame^] | 3641 | err = mv88e6xxx_mdio_register(ps, NULL); |
| 3642 | if (err) |
| 3643 | return NULL; |
| 3644 | |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3645 | *priv = ps; |
| 3646 | |
| 3647 | dev_info(&ps->bus->dev, "switch 0x%x probed: %s, revision %u\n", |
| 3648 | prod_num, name, rev); |
| 3649 | |
Andrew Lunn | a77d43f | 2016-04-13 02:40:42 +0200 | [diff] [blame] | 3650 | return name; |
| 3651 | } |
| 3652 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3653 | struct dsa_switch_driver mv88e6xxx_switch_driver = { |
| 3654 | .tag_protocol = DSA_TAG_PROTO_EDSA, |
Andrew Lunn | fcdce7d | 2016-05-10 23:27:20 +0200 | [diff] [blame] | 3655 | .probe = mv88e6xxx_drv_probe, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3656 | .setup = mv88e6xxx_setup, |
| 3657 | .set_addr = mv88e6xxx_set_addr, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3658 | .adjust_link = mv88e6xxx_adjust_link, |
| 3659 | .get_strings = mv88e6xxx_get_strings, |
| 3660 | .get_ethtool_stats = mv88e6xxx_get_ethtool_stats, |
| 3661 | .get_sset_count = mv88e6xxx_get_sset_count, |
| 3662 | .set_eee = mv88e6xxx_set_eee, |
| 3663 | .get_eee = mv88e6xxx_get_eee, |
| 3664 | #ifdef CONFIG_NET_DSA_HWMON |
| 3665 | .get_temp = mv88e6xxx_get_temp, |
| 3666 | .get_temp_limit = mv88e6xxx_get_temp_limit, |
| 3667 | .set_temp_limit = mv88e6xxx_set_temp_limit, |
| 3668 | .get_temp_alarm = mv88e6xxx_get_temp_alarm, |
| 3669 | #endif |
Andrew Lunn | f8cd875 | 2016-05-10 23:27:25 +0200 | [diff] [blame] | 3670 | .get_eeprom_len = mv88e6xxx_get_eeprom_len, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3671 | .get_eeprom = mv88e6xxx_get_eeprom, |
| 3672 | .set_eeprom = mv88e6xxx_set_eeprom, |
| 3673 | .get_regs_len = mv88e6xxx_get_regs_len, |
| 3674 | .get_regs = mv88e6xxx_get_regs, |
| 3675 | .port_bridge_join = mv88e6xxx_port_bridge_join, |
| 3676 | .port_bridge_leave = mv88e6xxx_port_bridge_leave, |
| 3677 | .port_stp_state_set = mv88e6xxx_port_stp_state_set, |
| 3678 | .port_vlan_filtering = mv88e6xxx_port_vlan_filtering, |
| 3679 | .port_vlan_prepare = mv88e6xxx_port_vlan_prepare, |
| 3680 | .port_vlan_add = mv88e6xxx_port_vlan_add, |
| 3681 | .port_vlan_del = mv88e6xxx_port_vlan_del, |
| 3682 | .port_vlan_dump = mv88e6xxx_port_vlan_dump, |
| 3683 | .port_fdb_prepare = mv88e6xxx_port_fdb_prepare, |
| 3684 | .port_fdb_add = mv88e6xxx_port_fdb_add, |
| 3685 | .port_fdb_del = mv88e6xxx_port_fdb_del, |
| 3686 | .port_fdb_dump = mv88e6xxx_port_fdb_dump, |
| 3687 | }; |
| 3688 | |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 3689 | int mv88e6xxx_probe(struct mdio_device *mdiodev) |
| 3690 | { |
| 3691 | struct device *dev = &mdiodev->dev; |
Andrew Lunn | f8cd875 | 2016-05-10 23:27:25 +0200 | [diff] [blame] | 3692 | struct device_node *np = dev->of_node; |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 3693 | struct mv88e6xxx_priv_state *ps; |
| 3694 | int id, prod_num, rev; |
| 3695 | struct dsa_switch *ds; |
Andrew Lunn | f8cd875 | 2016-05-10 23:27:25 +0200 | [diff] [blame] | 3696 | u32 eeprom_len; |
Andrew Lunn | 52638f7 | 2016-05-10 23:27:22 +0200 | [diff] [blame] | 3697 | int err; |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 3698 | |
| 3699 | ds = devm_kzalloc(dev, sizeof(*ds) + sizeof(*ps), GFP_KERNEL); |
| 3700 | if (!ds) |
| 3701 | return -ENOMEM; |
| 3702 | |
| 3703 | ps = (struct mv88e6xxx_priv_state *)(ds + 1); |
| 3704 | ds->priv = ps; |
Andrew Lunn | c33063d | 2016-05-10 23:27:23 +0200 | [diff] [blame] | 3705 | ds->dev = dev; |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 3706 | ps->dev = dev; |
| 3707 | ps->ds = ds; |
| 3708 | ps->bus = mdiodev->bus; |
| 3709 | ps->sw_addr = mdiodev->addr; |
| 3710 | mutex_init(&ps->smi_mutex); |
| 3711 | |
| 3712 | get_device(&ps->bus->dev); |
| 3713 | |
| 3714 | ds->drv = &mv88e6xxx_switch_driver; |
| 3715 | |
| 3716 | id = mv88e6xxx_reg_read(ps, REG_PORT(0), PORT_SWITCH_ID); |
| 3717 | if (id < 0) |
| 3718 | return id; |
| 3719 | |
| 3720 | prod_num = (id & 0xfff0) >> 4; |
| 3721 | rev = id & 0x000f; |
| 3722 | |
| 3723 | ps->info = mv88e6xxx_lookup_info(prod_num, mv88e6xxx_table, |
| 3724 | ARRAY_SIZE(mv88e6xxx_table)); |
| 3725 | if (!ps->info) |
| 3726 | return -ENODEV; |
| 3727 | |
Andrew Lunn | 52638f7 | 2016-05-10 23:27:22 +0200 | [diff] [blame] | 3728 | ps->reset = devm_gpiod_get(&mdiodev->dev, "reset", GPIOD_ASIS); |
| 3729 | if (IS_ERR(ps->reset)) { |
| 3730 | err = PTR_ERR(ps->reset); |
| 3731 | if (err == -ENOENT) { |
| 3732 | /* Optional, so not an error */ |
| 3733 | ps->reset = NULL; |
| 3734 | } else { |
| 3735 | return err; |
| 3736 | } |
| 3737 | } |
| 3738 | |
Andrew Lunn | f8cd875 | 2016-05-10 23:27:25 +0200 | [diff] [blame] | 3739 | if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM) && |
| 3740 | !of_property_read_u32(np, "eeprom-length", &eeprom_len)) |
| 3741 | ps->eeprom_len = eeprom_len; |
| 3742 | |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame^] | 3743 | err = mv88e6xxx_mdio_register(ps, mdiodev->dev.of_node); |
| 3744 | if (err) |
| 3745 | return err; |
| 3746 | |
| 3747 | ds->slave_mii_bus = ps->mdio_bus; |
| 3748 | |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 3749 | dev_set_drvdata(dev, ds); |
| 3750 | |
| 3751 | dev_info(dev, "switch 0x%x probed: %s, revision %u\n", |
| 3752 | prod_num, ps->info->name, rev); |
| 3753 | |
| 3754 | return 0; |
| 3755 | } |
| 3756 | |
| 3757 | static void mv88e6xxx_remove(struct mdio_device *mdiodev) |
| 3758 | { |
| 3759 | struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev); |
| 3760 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 3761 | |
| 3762 | put_device(&ps->bus->dev); |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame^] | 3763 | |
| 3764 | mv88e6xxx_mdio_unregister(ps); |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 3765 | } |
| 3766 | |
| 3767 | static const struct of_device_id mv88e6xxx_of_match[] = { |
| 3768 | { .compatible = "marvell,mv88e6085" }, |
| 3769 | { /* sentinel */ }, |
| 3770 | }; |
| 3771 | |
| 3772 | MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match); |
| 3773 | |
| 3774 | static struct mdio_driver mv88e6xxx_driver = { |
| 3775 | .probe = mv88e6xxx_probe, |
| 3776 | .remove = mv88e6xxx_remove, |
| 3777 | .mdiodrv.driver = { |
| 3778 | .name = "mv88e6085", |
| 3779 | .of_match_table = mv88e6xxx_of_match, |
| 3780 | }, |
| 3781 | }; |
| 3782 | |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 3783 | static int __init mv88e6xxx_init(void) |
| 3784 | { |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3785 | register_switch_driver(&mv88e6xxx_switch_driver); |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 3786 | return mdio_driver_register(&mv88e6xxx_driver); |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 3787 | } |
| 3788 | module_init(mv88e6xxx_init); |
| 3789 | |
| 3790 | static void __exit mv88e6xxx_cleanup(void) |
| 3791 | { |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 3792 | mdio_driver_unregister(&mv88e6xxx_driver); |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3793 | unregister_switch_driver(&mv88e6xxx_switch_driver); |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 3794 | } |
| 3795 | module_exit(mv88e6xxx_cleanup); |
Ben Hutchings | 3d825ed | 2011-11-25 14:37:16 +0000 | [diff] [blame] | 3796 | |
| 3797 | MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>"); |
| 3798 | MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips"); |
| 3799 | MODULE_LICENSE("GPL"); |